Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more types #1640

Merged
merged 2 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions slither/core/children/child_expression.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Union

if TYPE_CHECKING:
from slither.core.expressions.expression import Expression
from slither.slithir.operations import Operation


class ChildExpression:
def __init__(self) -> None:
super().__init__()
self._expression = None

def set_expression(self, expression: "Expression") -> None:
def set_expression(self, expression: Union["Expression", "Operation"]) -> None:
self._expression = expression

@property
def expression(self) -> "Expression":
def expression(self) -> Union["Expression", "Operation"]:
return self._expression
1 change: 1 addition & 0 deletions slither/core/declarations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
from .structure_top_level import StructureTopLevel
from .function_contract import FunctionContract
from .function_top_level import FunctionTopLevel
from .custom_error_contract import CustomErrorContract
16 changes: 9 additions & 7 deletions slither/core/declarations/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
EnumContract,
StructureContract,
FunctionContract,
CustomErrorContract,
)
from slither.slithir.variables.variable import SlithIRVariable
from slither.core.variables.variable import Variable
from slither.core.variables.state_variable import StateVariable
from slither.core.variables import Variable, StateVariable
from slither.core.compilation_unit import SlitherCompilationUnit
from slither.core.declarations.custom_error_contract import CustomErrorContract
from slither.core.scope.scope import FileScope
from slither.core.cfg.node import Node


LOGGER = logging.getLogger("Contract")
Expand Down Expand Up @@ -803,23 +803,25 @@ def get_state_variable_from_canonical_name(
"""
return next((v for v in self.state_variables if v.name == canonical_name), None)

def get_structure_from_name(self, structure_name: str) -> Optional["Structure"]:
def get_structure_from_name(self, structure_name: str) -> Optional["StructureContract"]:
"""
Return a structure from a name
Args:
structure_name (str): name of the structure
Returns:
Structure
StructureContract
"""
return next((st for st in self.structures if st.name == structure_name), None)

def get_structure_from_canonical_name(self, structure_name: str) -> Optional["Structure"]:
def get_structure_from_canonical_name(
self, structure_name: str
) -> Optional["StructureContract"]:
"""
Return a structure from a canonical name
Args:
structure_name (str): canonical name of the structure
Returns:
Structure
StructureContract
"""
return next((st for st in self.structures if st.canonical_name == structure_name), None)

Expand Down
2 changes: 1 addition & 1 deletion slither/core/declarations/using_for_top_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ def __init__(self, scope: "FileScope") -> None:
self.file_scope: "FileScope" = scope

@property
def using_for(self) -> Dict[Type, List[Type]]:
def using_for(self) -> Dict[Union[str, Type], List[Type]]:
return self._using_for
6 changes: 3 additions & 3 deletions slither/core/slither_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import posixpath
import re
from collections import defaultdict
from typing import Optional, Dict, List, Set, Union
from typing import Optional, Dict, List, Set, Union, Tuple

from crytic_compile import CryticCompile
from crytic_compile.utils.naming import Filename
Expand Down Expand Up @@ -73,8 +73,8 @@ def __init__(self) -> None:

# Maps from file to detector name to the start/end ranges for that detector.
# Infinity is used to signal a detector has no end range.
self._ignore_ranges: defaultdict[str, defaultdict[str, List[(int, int)]]] = defaultdict(
lambda: defaultdict(lambda: [])
self._ignore_ranges: Dict[str, Dict[str, List[Tuple[int, ...]]]] = defaultdict(
lambda: defaultdict(lambda: [-1, -1])
0xalpharush marked this conversation as resolved.
Show resolved Hide resolved
)

self._compilation_units: List[SlitherCompilationUnit] = []
Expand Down
2 changes: 1 addition & 1 deletion slither/core/solidity_types/array_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ArrayType(Type):
def __init__(
self,
t: Union["TypeAliasTopLevel", "ArrayType", "FunctionType", "ElementaryType"],
length: Optional[Union["Identifier", Literal, "BinaryOperation"]],
length: Optional[Union["Identifier", Literal, "BinaryOperation", int]],
) -> None:
assert isinstance(t, Type)
if length:
Expand Down
4 changes: 2 additions & 2 deletions slither/core/solidity_types/function_type.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Tuple
from typing import List, Tuple, Any

from slither.core.solidity_types.type import Type
from slither.core.variables.function_type_variable import FunctionTypeVariable
Expand Down Expand Up @@ -69,7 +69,7 @@ def signature(self) -> str:
return f"({params}) returns({return_values})"
return f"({params})"

def __eq__(self, other: ElementaryType) -> bool:
def __eq__(self, other: Any) -> bool:
if not isinstance(other, FunctionType):
return False
return self.params == other.params and self.return_values == other.return_values
Expand Down
6 changes: 3 additions & 3 deletions slither/core/solidity_types/type_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

from slither.core.children.child_contract import ChildContract
from slither.core.declarations.top_level import TopLevel
from slither.core.solidity_types import Type
from slither.core.solidity_types import Type, ElementaryType

if TYPE_CHECKING:
from slither.core.declarations import Contract
from slither.core.scope.scope import FileScope


class TypeAlias(Type):
def __init__(self, underlying_type: Type, name: str) -> None:
def __init__(self, underlying_type: ElementaryType, name: str) -> None:
super().__init__()
self.name = name
self.underlying_type = underlying_type

@property
def type(self) -> Type:
def type(self) -> ElementaryType:
"""
Return the underlying type. Alias for underlying_type

Expand Down
6 changes: 3 additions & 3 deletions slither/core/solidity_types/type_information.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

if TYPE_CHECKING:
from slither.core.declarations.contract import Contract
from slither.core.declarations.enum_top_level import EnumTopLevel
from slither.core.declarations.enum import Enum


# Use to model the Type(X) function, which returns an undefined type
# https://solidity.readthedocs.io/en/latest/units-and-global-variables.html#type-information
class TypeInformation(Type):
def __init__(self, c: Union[ElementaryType, "Contract", "EnumTopLevel"]) -> None:
def __init__(self, c: Union[ElementaryType, "Contract", "Enum"]) -> None:
# pylint: disable=import-outside-toplevel
from slither.core.declarations.contract import Contract
from slither.core.declarations.enum import Enum
Expand All @@ -21,7 +21,7 @@ def __init__(self, c: Union[ElementaryType, "Contract", "EnumTopLevel"]) -> None
self._type = c

@property
def type(self) -> "Contract":
def type(self) -> Union["Contract", ElementaryType, "Enum"]:
return self._type

@property
Expand Down
6 changes: 1 addition & 5 deletions slither/core/solidity_types/user_defined_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@
from slither.core.declarations.structure import Structure
from slither.core.declarations.enum import Enum
from slither.core.declarations.contract import Contract
from slither.core.declarations import EnumContract
from slither.core.declarations.structure_top_level import StructureTopLevel

# pylint: disable=import-outside-toplevel
class UserDefinedType(Type):
def __init__(
self, t: Union["EnumContract", "StructureTopLevel", "Contract", "StructureContract"]
) -> None:
def __init__(self, t: Union["Enum", "Contract", "Structure"]) -> None:
from slither.core.declarations.structure import Structure
from slither.core.declarations.enum import Enum
from slither.core.declarations.contract import Contract
Expand Down
2 changes: 2 additions & 0 deletions slither/core/variables/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .state_variable import StateVariable
from .variable import Variable
8 changes: 4 additions & 4 deletions slither/slithir/operations/phi_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ def read(self) -> List[StateIRVariable]:
def rvalues(self):
return self._rvalues

@rvalues.setter
def rvalues(self, vals):
self._rvalues = vals

@property
def rvalue_no_callback(self):
"""
rvalue if callback are not considered
"""
return self._rvalue_no_callback

@rvalues.setter
def rvalues(self, vals):
self._rvalues = vals

@property
def nodes(self):
return self._nodes
Expand Down
2 changes: 1 addition & 1 deletion slither/slithir/variables/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class Constant(SlithIRVariable):
def __init__(
self,
val: str,
val: Union[int, str],
constant_type: Optional[ElementaryType] = None,
subdenomination: Optional[str] = None,
) -> None: # pylint: disable=too-many-branches
Expand Down
3 changes: 2 additions & 1 deletion slither/slithir/variables/local_variable.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import Set
from slither.core.variables.local_variable import LocalVariable
from slither.slithir.variables.temporary import TemporaryVariable
from slither.slithir.variables.variable import SlithIRVariable
Expand Down Expand Up @@ -31,7 +32,7 @@ def __init__(self, local_variable: LocalVariable) -> None:

# Additional field
# points to state variables
self._refers_to = set()
self._refers_to: Set[StateIRVariable] = set()

# keep un-ssa version
if isinstance(local_variable, LocalIRVariable):
Expand Down
14 changes: 7 additions & 7 deletions slither/slithir/variables/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ def points_to(self):
"""
return self._points_to

@property
def points_to_origin(self):
points = self.points_to
while isinstance(points, ReferenceVariable):
points = points.points_to
return points

@points_to.setter
def points_to(self, points_to):
# Can only be a rvalue of
Expand All @@ -55,6 +48,13 @@ def points_to(self, points_to):

self._points_to = points_to

@property
def points_to_origin(self):
points = self.points_to
while isinstance(points, ReferenceVariable):
points = points.points_to
return points

@property
def name(self) -> str:
return f"REF_{self.index}"
Expand Down
6 changes: 3 additions & 3 deletions slither/solc_parsing/declarations/function.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Dict, Optional, Union, List, TYPE_CHECKING
from typing import Dict, Optional, Union, List, TYPE_CHECKING, Tuple

from slither.core.cfg.node import NodeType, link_nodes, insert_node, Node
from slither.core.cfg.scope import Scope
Expand Down Expand Up @@ -445,7 +445,7 @@ def _parse_while(self, whilte_statement: Dict, node: NodeSolc) -> NodeSolc:

def _parse_for_compact_ast( # pylint: disable=no-self-use
self, statement: Dict
) -> (Optional[Dict], Optional[Dict], Optional[Dict], Dict):
) -> Tuple[Optional[Dict], Optional[Dict], Optional[Dict], Dict]:
body = statement["body"]
init_expression = statement.get("initializationExpression", None)
condition = statement.get("condition", None)
Expand All @@ -455,7 +455,7 @@ def _parse_for_compact_ast( # pylint: disable=no-self-use

def _parse_for_legacy_ast(
self, statement: Dict
) -> (Optional[Dict], Optional[Dict], Optional[Dict], Dict):
) -> Tuple[Optional[Dict], Optional[Dict], Optional[Dict], Dict]:
# if we're using an old version of solc (anything below and including 0.4.11) or if the user
# explicitly enabled compact ast, we might need to make some best-effort guesses
children = statement[self.get_children("children")]
Expand Down
7 changes: 4 additions & 3 deletions slither/solc_parsing/solidity_types/type_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from slither.solc_parsing.expressions.expression_parsing import CallerContextExpression

if TYPE_CHECKING:
from slither.core.declarations import Structure, Enum
from slither.core.declarations import Structure, Enum, Function
from slither.core.declarations.contract import Contract
from slither.core.compilation_unit import SlitherCompilationUnit
from slither.solc_parsing.slither_compilation_unit_solc import SlitherCompilationUnitSolc
Expand Down Expand Up @@ -233,6 +233,7 @@ def parse_type(
sl: "SlitherCompilationUnit"
renaming: Dict[str, str]
user_defined_types: Dict[str, TypeAlias]
enums_direct_access: List["Enum"] = []
# Note: for convenicence top level functions use the same parser than function in contract
# but contract_parser is set to None
if isinstance(caller_context, SlitherCompilationUnitSolc) or (
Expand All @@ -254,7 +255,7 @@ def parse_type(
all_structuress = [c.structures for c in sl.contracts]
all_structures = [item for sublist in all_structuress for item in sublist]
all_structures += structures_direct_access
enums_direct_access = sl.enums_top_level
enums_direct_access += sl.enums_top_level
all_enumss = [c.enums for c in sl.contracts]
all_enums = [item for sublist in all_enumss for item in sublist]
all_enums += enums_direct_access
Expand Down Expand Up @@ -316,7 +317,7 @@ def parse_type(
all_structuress = [c.structures for c in contract.file_scope.contracts.values()]
all_structures = [item for sublist in all_structuress for item in sublist]
all_structures += contract.file_scope.structures.values()
enums_direct_access: List["Enum"] = contract.enums
enums_direct_access += contract.enums
enums_direct_access += contract.file_scope.enums.values()
all_enumss = [c.enums for c in contract.file_scope.contracts.values()]
all_enums = [item for sublist in all_enumss for item in sublist]
Expand Down
4 changes: 2 additions & 2 deletions slither/solc_parsing/variables/variable_declaration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import re
from typing import Dict
from typing import Dict, Optional

from slither.solc_parsing.declarations.caller_context import CallerContextExpression
from slither.solc_parsing.expressions.expression_parsing import parse_expression
Expand Down Expand Up @@ -127,7 +127,7 @@ def _analyze_variable_attributes(self, attributes: Dict) -> None:
self._variable.visibility = "internal"

def _init_from_declaration(
self, var: Dict, init: bool
self, var: Dict, init: Optional[bool]
) -> None: # pylint: disable=too-many-branches
if self._is_compact_ast:
attributes = var
Expand Down
6 changes: 5 additions & 1 deletion slither/visitors/expression/constants_folding.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
Identifier,
BinaryOperation,
UnaryOperation,
TupleExpression,
TypeConversion,
)

from slither.utils.integer_conversion import convert_string_to_fraction, convert_string_to_int
Expand All @@ -23,7 +25,9 @@ class NotConstant(Exception):

KEY = "ConstantFolding"

CONSTANT_TYPES_OPERATIONS = Union[Literal, BinaryOperation, UnaryOperation, Identifier]
CONSTANT_TYPES_OPERATIONS = Union[
Literal, BinaryOperation, UnaryOperation, Identifier, TupleExpression, TypeConversion
]


def get_val(expression: CONSTANT_TYPES_OPERATIONS) -> Union[bool, int, Fraction, str]:
Expand Down
Loading