Skip to content

Commit

Permalink
Remove calls to isinstance to improve performances.
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkaMaul committed Apr 19, 2024
1 parent ae0cb5b commit 6074ab1
Showing 1 changed file with 29 additions and 50 deletions.
79 changes: 29 additions & 50 deletions slither/visitors/expression/expression.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from functools import cache

from slither.core.expressions.assignment_operation import AssignmentOperation
from slither.core.expressions.binary_operation import BinaryOperation
Expand All @@ -21,6 +22,28 @@
logger = logging.getLogger("ExpressionVisitor")


@cache
def get_visitor_mapping():
"""Returns a visitor mapping from expression type to visiting functions."""
return {
AssignmentOperation: '_visit_assignement_operation',
BinaryOperation: '_visit_binary_operation',
CallExpression: '_visit_call_expression',
ConditionalExpression: '_visit_conditional_expression',
ElementaryTypeNameExpression: '_visit_elementary_type_name_expression',
Identifier: '_visit_identifier',
IndexAccess: '_visit_index_access',
Literal: '_visit_literal',
MemberAccess: '_visit_member_access',
NewArray: '_visit_new_array',
NewContract: '_visit_new_contract',
NewElementaryType: '_visit_new_elementary_type',
TupleExpression: '_visit_tuple_expression',
TypeConversion: '_visit_type_conversion',
UnaryOperation: '_visit_unary_operation'
}


# pylint: disable=too-few-public-methods
class ExpressionVisitor:
def __init__(self, expression: Expression) -> None:
Expand All @@ -35,60 +58,16 @@ def expression(self) -> Expression:

# visit an expression
# call pre_visit, visit_expression_name, post_visit
# pylint: disable=too-many-branches
def _visit_expression(self, expression: Expression) -> None:
self._pre_visit(expression)

if isinstance(expression, AssignmentOperation):
self._visit_assignement_operation(expression)

elif isinstance(expression, BinaryOperation):
self._visit_binary_operation(expression)

elif isinstance(expression, CallExpression):
self._visit_call_expression(expression)

elif isinstance(expression, ConditionalExpression):
self._visit_conditional_expression(expression)

elif isinstance(expression, ElementaryTypeNameExpression):
self._visit_elementary_type_name_expression(expression)

elif isinstance(expression, Identifier):
self._visit_identifier(expression)

elif isinstance(expression, IndexAccess):
self._visit_index_access(expression)

elif isinstance(expression, Literal):
self._visit_literal(expression)

elif isinstance(expression, MemberAccess):
self._visit_member_access(expression)

elif isinstance(expression, NewArray):
self._visit_new_array(expression)

elif isinstance(expression, NewContract):
self._visit_new_contract(expression)

elif isinstance(expression, NewElementaryType):
self._visit_new_elementary_type(expression)

elif isinstance(expression, TupleExpression):
self._visit_tuple_expression(expression)
if expression is not None:
visitor_method = get_visitor_mapping().get(expression.__class__)
if not visitor_method:
raise SlitherError(f"Expression not handled: {expression}")

elif isinstance(expression, TypeConversion):
self._visit_type_conversion(expression)

elif isinstance(expression, UnaryOperation):
self._visit_unary_operation(expression)

elif expression is None:
pass

else:
raise SlitherError(f"Expression not handled: {expression}")
visitor = getattr(self, visitor_method)
visitor(expression)

self._post_visit(expression)

Expand Down

0 comments on commit 6074ab1

Please sign in to comment.