solidity_parser.ast.symtab

Attributes

Exceptions

TypeNotFound

Common base class for all non-exit exceptions.

Classes

Scopeable

Element that can be added as a child of a Scope

Symbol

Element that can be added as a child of a Scope

CrossScopeSymbolAlias

Element that can be added as a child of a Scope

Scope

Element that can be added as a child of a Scope

ScopeAndSymbol

Element that can be added as a child of a Scope

BuiltinObject

Element that can be added as a child of a Scope

BuiltinFunction

Element that can be added as a child of a Scope

BuiltinValue

Element that can be added as a child of a Scope

RootScope

Element that can be added as a child of a Scope

FileScope

Element that can be added as a child of a Scope

LibraryScope

Element that can be added as a child of a Scope

ContractOrInterfaceScope

Element that can be added as a child of a Scope

StructScope

Element that can be added as a child of a Scope

UserDefinedValueTypeScope

Element that can be added as a child of a Scope

LibraryScope

Element that can be added as a child of a Scope

EnumScope

Element that can be added as a child of a Scope

ModFunErrEvtScope

Element that can be added as a child of a Scope

ImportSymbol

Element that can be added as a child of a Scope

AliasImportSymbol

Element that can be added as a child of a Scope

UnitImportSymbol

Element that can be added as a child of a Scope

ProxyScope

Element that can be added as a child of a Scope

UsingDirectiveScope

Element that can be added as a child of a Scope

UsingFunctionSymbol

Symbol for a function that was added to the current scope by a Solidity using statement. Solidity docs state that

UsingOperatorSymbol

Similar to UsingFunctionSymbol except for operator overloads

Builder2

Functions

bytes()

bytesn(n)

bytes32()

uint([size])

ACCEPT(x)

unit_scope_of(s)

ACCEPT_INHERITABLE(base_scope)

is_top_level(node)

is_using_directive_scope(→ bool)

predicate_ignore_inherited_usings(base_scope)

predicate_accept_top_levels(→ bool)

ACCEPT_NO_INHERITED_USINGS(base_scope)

ACCEPT_CALLABLES(x)

ACCEPT_NOT(p)

ACCEPT_TOP_LEVEL_SCOPE(x)

ACCEPT_ALL(*predicates)

test_predicate(xs[, predicate])

_add_to_results(possible_symbols, results, found_already)

create_builtin_scope(key[, value, values, functions])

simple_name_resolver(scope, name)

type_key(→ str)

meta_type_key(→ str)

Module Contents

solidity_parser.ast.symtab.Aliases: TypeAlias = str | list[str]
solidity_parser.ast.symtab.bytes()
solidity_parser.ast.symtab.bytesn(n)
solidity_parser.ast.symtab.bytes32()
solidity_parser.ast.symtab.uint(size=256)
solidity_parser.ast.symtab.ACCEPT(x)
solidity_parser.ast.symtab.unit_scope_of(s)
solidity_parser.ast.symtab.ACCEPT_INHERITABLE(base_scope)
solidity_parser.ast.symtab.is_top_level(node: solidity_parser.ast.nodebase.Node)
solidity_parser.ast.symtab.is_using_directive_scope(sym: Symbol) bool
solidity_parser.ast.symtab.predicate_ignore_inherited_usings(base_scope)
solidity_parser.ast.symtab.predicate_accept_top_levels(sym: Symbol) bool
solidity_parser.ast.symtab.ACCEPT_NO_INHERITED_USINGS(base_scope)
solidity_parser.ast.symtab.ACCEPT_CALLABLES(x)
solidity_parser.ast.symtab.ACCEPT_NOT(p)
solidity_parser.ast.symtab.ACCEPT_TOP_LEVEL_SCOPE(x)
solidity_parser.ast.symtab.ACCEPT_ALL(*predicates)
solidity_parser.ast.symtab.test_predicate(xs, predicate=None)
solidity_parser.ast.symtab._add_to_results(possible_symbols: Collection, results: list, found_already: Set)
exception solidity_parser.ast.symtab.TypeNotFound

Bases: Exception

Common base class for all non-exit exceptions.

class solidity_parser.ast.symtab.Scopeable(aliases: Aliases | None)

Element that can be added as a child of a Scope

parent_scope: Scope | None = None
aliases: list[str]
set_parent_scope(parent_scope: Scope)

Sets the parent scope of this element, subclasses can check the type of the parent for sanity checks

_T
find_first_ancestor(predicate: Callable[[Scopeable], bool], get_parent: Callable[[Scopeable], Scope] | None = None) _T | None

Walks up the symbol tree and finds the first element that satisfies the given predicate

Parameters:
  • predicate – a function that takes a Scopeable and returns a bool to determine if it matches

  • get_parent – a function that takes a Scopeable and returns its parent, defaults to the parent_scope of a Scopeable

Returns:

the first Scopeable that satisfies the predicate

find_first_ancestor_of(ttype: Type[_T]) _T | None

Find the first ancestor that is of the given type, note: these are python types and not solc types.

Parameters:

ttype – e.g. ContractOrInterfaceScope

Returns:

the first ancestor that is of the given type

_check_single_symbol(name, results, default)
class solidity_parser.ast.symtab.Symbol(aliases: Aliases | None, value)

Bases: Scopeable

Element that can be added as a child of a Scope

value
order
get_as_dealiased_symbols() list[Symbol]
res_syms() list[Symbol]
res_syms_single()
set_parent_scope(parent_scope: Scope)

Sets the parent scope of this element, subclasses can check the type of the parent for sanity checks

str_type()
__str__(level=0)

Return str(self).

class solidity_parser.ast.symtab.CrossScopeSymbolAlias(aliases: Aliases, other_symbol: Symbol)

Bases: Symbol

Element that can be added as a child of a Scope

other_symbol
res_syms() list[Symbol]
get_as_dealiased_symbols() list[Symbol]
class solidity_parser.ast.symtab.Scope(aliases: Aliases | None)

Bases: Scopeable

Element that can be added as a child of a Scope

symbols: Dict[str, list[Symbol]]
imported_scopes: list[Scope] = []
is_defined(name: str) bool

Check if the name exists in the current scopes local table, i.e. whether it was declared in the current scope

get_direct_children() Collection[Symbol]

Get all children declared directly in this scope

get_all_children(collect_predicate, explore_branch_predicate)

Tree explorer for all DECLARED children and grandchildren, i.e. doesn’t look at imports

get_all_functions() list[ModFunErrEvtScope]

Gets all DECLARED functions in the current scope/descendant scopes

import_symbols_from_scope(other_scope: Scope)

Links the symbols in another scope to the current scope, i.e. makes the imported symbols visible in the current scope

Parameters:

other_scope – The scope whose symbols should be imported

add_global_symbol(symbol: Symbol)

Helper function to add a symbol to the global scope(RootScope)

add(symbol: Symbol)

Adds a symbol to the current scope and set its parent scope to this scope

find_current_level(name: str, predicate=None, visited_scopes: Set[Scope] = None) list[Symbol]

Finds symbols in this scope or any imported scopes at the current “level”. A level is roughly the scopes that are visible by an expression in the current scope.

find_imported(name: str, predicate=None, visited_scopes: Set = None) list[Symbol]

Finds the given name in all the imported scopes that are linked to the current scope. This can match many valid symbols so it is up to the caller to choose the right one, however, the results are deduplicated by checking that two symbols dealias to the same symbol.

find_local(name: str) list[Symbol]

Finds symbols in this scope’s symbol table only

find_from_parent(name: str, predicate=None) list[Symbol]
find_multi_part_symbol(name: str, find_base_symbol: bool = False, predicate=None)

Finds a potentially multi-part/qualified symbol (e.g. a.b.c)

find(name: str, find_base_symbol: bool = False, predicate=None, dealias: bool = True) list[Symbol]

Entry point for the symbol finder. Finds the given name in this scope and any imported scopes

Parameters:

name (str): The name to search for. find_base_symbol (bool): Whether to find base symbols. predicate (function): A function to filter symbols. dealias (bool): Whether to dealias symbols.

Returns:

list[Symbol]: A list of symbols that match the search criteria.

find_single(name: str, find_base_symbol: bool = False, default=None, predicate=None) Symbol | None
find_user_type_scope(name, find_base_symbol: bool = False, default=None, predicate=None) Scope | list[Scope]

Finds the scope of a user-defined type based on the given name. :param name: The name of the type :param find_base_symbol: Whether to find the base symbol or whether using scopes are acceptable results :param default: The default value to return if no matches are found :param predicate: Optional function to filter during the search

Returns:

A single scope if find_base_symbol is True, or a list of scopes if find_base_symbol is False

Raises:

TypeNotFound – if no type matching the name was found

find_type(ttype, predicate=None, as_single=False) Scope | None | list[Scope]

Finds the scope for the given type in the current scope. The type scope might be different to the scope of the type where the type was defined because of using statements. A scope is created if one isn’t visible in the current scope.

Parameters:
  • ttype – The type to search for, CANNOT be a user type, use find_user_type_scope for that case

  • predicate – Optional function to filter during the search

  • as_single – Whether to return a single scope or a list of scopes

Returns:

The scope if as_single is True or a list of scopes if as_single is False

find_metatype(ttype, is_interface, is_enum) Scope
str__symbols(level=0)

Returns a string representation of all symbols in this scope and its children with indentation

__str__(level=0)

Return str(self).

class solidity_parser.ast.symtab.ScopeAndSymbol(aliases: Aliases | None, ast_node)

Bases: Scope, Symbol

Element that can be added as a child of a Scope

set_parent_scope(parent_scope: Scope)

Sets the parent scope of this element, subclasses can check the type of the parent for sanity checks

__str__(level=0)

Return str(self).

class solidity_parser.ast.symtab.BuiltinObject(name: str, value=None)

Bases: ScopeAndSymbol

Element that can be added as a child of a Scope

name
class solidity_parser.ast.symtab.BuiltinFunction(name: str, input_types: list[solidity_parser.ast.types.Type] | None, output_types: list[solidity_parser.ast.types.Type] | None)

Bases: Symbol

Element that can be added as a child of a Scope

input_types
output_types
class solidity_parser.ast.symtab.BuiltinValue(name: str, ttype: solidity_parser.ast.types.Type)

Bases: Symbol

Element that can be added as a child of a Scope

ttype
solidity_parser.ast.symtab.create_builtin_scope(key, value=None, values=None, functions=None)
solidity_parser.ast.symtab.simple_name_resolver(scope, name)
solidity_parser.ast.symtab.type_key(ttype, name_resolver=simple_name_resolver) str
solidity_parser.ast.symtab.meta_type_key(ttype, name_resolver=simple_name_resolver) str
class solidity_parser.ast.symtab.RootScope(parser_version: solidity_parser.util.version_util.Version)

Bases: Scope

Element that can be added as a child of a Scope

compiler_version = None
msg_object
abi_object
block_object
tx_object
bytes_object
string_object
class solidity_parser.ast.symtab.FileScope(builder: Builder2, vfs: solidity_parser.filesys.VirtualFileSystem, source_unit_name: str, ast1_units: list[solidity_parser.ast.solnodes.SourceUnit])

Bases: ScopeAndSymbol

Element that can be added as a child of a Scope

static alias(source_unit_name: str)
builder
vfs
source_unit_name
get_imported_source_unit(import_path: str) FileScope | None
set_parent_scope(parent_scope: Scope)

Sets the parent scope of this element, subclasses can check the type of the parent for sanity checks

class solidity_parser.ast.symtab.LibraryScope(ast_node: solidity_parser.ast.solnodes.LibraryDefinition)

Bases: ScopeAndSymbol

Element that can be added as a child of a Scope

class solidity_parser.ast.symtab.ContractOrInterfaceScope(ast_node: solidity_parser.ast.solnodes.ContractDefinition | solidity_parser.ast.solnodes.InterfaceDefinition)

Bases: ScopeAndSymbol

Element that can be added as a child of a Scope

find_current_level(name: str, predicate=None, visited_scopes: Set = None, check_hierarchy=True) list[Symbol] | None

Finds symbols in this scope or any imported scopes at the current “level”. A level is roughly the scopes that are visible by an expression in the current scope.

find_in_contract_hierarchy(name: str, predicate, visited_scopes)
get_supers() list[ContractOrInterfaceScope]
class solidity_parser.ast.symtab.StructScope(ast_node: solidity_parser.ast.solnodes.StructDefinition)

Bases: ScopeAndSymbol

Element that can be added as a child of a Scope

class solidity_parser.ast.symtab.UserDefinedValueTypeScope(ast_node: solidity_parser.ast.solnodes.UserValueType)

Bases: ScopeAndSymbol

Element that can be added as a child of a Scope

class solidity_parser.ast.symtab.LibraryScope(ast_node: solidity_parser.ast.solnodes.LibraryDefinition)

Bases: ScopeAndSymbol

Element that can be added as a child of a Scope

class solidity_parser.ast.symtab.EnumScope(ast_node: solidity_parser.ast.solnodes.EnumDefinition)

Bases: ScopeAndSymbol

Element that can be added as a child of a Scope

class solidity_parser.ast.symtab.ModFunErrEvtScope(ast_node: solidity_parser.ast.solnodes.FunctionDefinition | solidity_parser.ast.solnodes.EventDefinition | solidity_parser.ast.solnodes.ErrorDefinition | solidity_parser.ast.solnodes.ModifierDefinition)

Bases: ScopeAndSymbol

Element that can be added as a child of a Scope

class solidity_parser.ast.symtab.ImportSymbol(aliases: Aliases | None, ast_node: solidity_parser.ast.solnodes.ImportDirective)

Bases: ScopeAndSymbol

Element that can be added as a child of a Scope

get_as_dealiased_symbols() list[Symbol]
get_imported_scope() FileScope | None
_get_imported_symbols() list[Symbol]
res_syms() list[Symbol]
get_direct_children() Collection[Symbol]

Get all children declared directly in this scope

find(name: str, find_base_symbol: bool = False, predicate=None, dealias: bool = True) list[Symbol] | None

Entry point for the symbol finder. Finds the given name in this scope and any imported scopes

Parameters:

name (str): The name to search for. find_base_symbol (bool): Whether to find base symbols. predicate (function): A function to filter symbols. dealias (bool): Whether to dealias symbols.

Returns:

list[Symbol]: A list of symbols that match the search criteria.

find_metatype(ttype, is_interface, is_enum) Scope
find_local(name: str) list[Symbol] | None

Finds symbols in this scope’s symbol table only

find_first_ancestor(predicate, get_parent=None)

Walks up the symbol tree and finds the first element that satisfies the given predicate

Parameters:
  • predicate – a function that takes a Scopeable and returns a bool to determine if it matches

  • get_parent – a function that takes a Scopeable and returns its parent, defaults to the parent_scope of a Scopeable

Returns:

the first Scopeable that satisfies the predicate

find_first_ancestor_of(ttype: Type | Tuple[Type])

Find the first ancestor that is of the given type, note: these are python types and not solc types.

Parameters:

ttype – e.g. ContractOrInterfaceScope

Returns:

the first ancestor that is of the given type

find_imported(name: str, predicate=None, visited_scopes: Set = None) list[Symbol] | None

Finds the given name in all the imported scopes that are linked to the current scope. This can match many valid symbols so it is up to the caller to choose the right one, however, the results are deduplicated by checking that two symbols dealias to the same symbol.

find_current_level(name: str, predicate=None, visited_scopes: Set = None) list[Symbol] | None

Finds symbols in this scope or any imported scopes at the current “level”. A level is roughly the scopes that are visible by an expression in the current scope.

find_single(name: str, find_base_symbol: bool = False, default=None, predicate=None) Symbol | None
find_from_parent(name: str, predicate=None) list[Symbol]
class solidity_parser.ast.symtab.AliasImportSymbol(ast_node: solidity_parser.ast.solnodes.SymbolImportDirective, alias_index)

Bases: ImportSymbol

Element that can be added as a child of a Scope

alias_index
_get_imported_symbols() list[Symbol]
class solidity_parser.ast.symtab.UnitImportSymbol(ast_node: solidity_parser.ast.solnodes.UnitImportDirective)

Bases: ImportSymbol

Element that can be added as a child of a Scope

_get_imported_symbols() list[Symbol]
class solidity_parser.ast.symtab.ProxyScope(name: str, base_scope: ScopeAndSymbol)

Bases: ScopeAndSymbol

Element that can be added as a child of a Scope

base_scope
created_by = None
res_syms() list[Symbol]
class solidity_parser.ast.symtab.UsingDirectiveScope(node: solidity_parser.ast.solnodes.UsingDirective)

Bases: ScopeAndSymbol

Element that can be added as a child of a Scope

class solidity_parser.ast.symtab.UsingFunctionSymbol(target: ModFunErrEvtScope, override_type: solidity_parser.ast.types.Type)

Bases: Symbol

Symbol for a function that was added to the current scope by a Solidity using statement. Solidity docs state that all functions, even those that don’t match the type specifier in the using statement have to be added to the scope the using statement is declared in. This symbol type is required instead of the usual ModFunErrEvtSymbol as we need to be able to associate the bound type from the using statement and the first parameter type of the “value” of this symbol(i.e. the FunctionDefinition) may or may not be the same as the specified one.

target
override_type
res_syms() list[Symbol]
class solidity_parser.ast.symtab.UsingOperatorSymbol(target: ModFunErrEvtScope, override_type: solidity_parser.ast.types.Type, operator: solidity_parser.ast.solnodes.UnaryOpCode | solidity_parser.ast.solnodes.BinaryOpCode)

Bases: Symbol

Similar to UsingFunctionSymbol except for operator overloads

target
override_type
operator
res_syms() list[Symbol]
class solidity_parser.ast.symtab.Builder2(vfs: solidity_parser.filesys.VirtualFileSystem, parser_version: solidity_parser.util.version_util.Version = None)
class Context(file_scope, unit_scope)
file_scope
unit_scope
root_scope
vfs
parser_version
process_or_find(loaded_source: solidity_parser.filesys.LoadedSource)
process_or_find_from_base_dir(relative_source_unit_name: str | pathlib.Path)
process_file(source_unit_name: str, source_units: list[solidity_parser.ast.solnodes.SourceUnit] = None)
sort_ast_nodes(nodes)
add_node_dfs(parent_scope, node, context: Context, build_skeletons, visit_index=0)

Recursively traverse a node and its children and create symbols and scopes in a nested hierarchy

This function adds newly created symbols and scopes to the given parent scope and does not return anything

make_using_scope(node: solidity_parser.ast.solnodes.UsingDirective)
make_var_decl_scope(node: solidity_parser.ast.solnodes.VarDecl)
add_to_scope(parent: Scope, *children: Symbol)
make_scope(node: solidity_parser.ast.nodebase.Node, name=None)
make_symbol(node: solidity_parser.ast.nodebase.Node, sym_type=Symbol, name=None)
scope_name(base_name, node)
find_using_target_scope_and_name(current_scope, target_type: solidity_parser.ast.types.Type)
make_proxy_scope(scope_name, creator_scope, base_scope, library_scope=None)
get_proxy_scope_for_type(cur_scope, target_type, target_scope_name, target_type_scope, library_scope=None, check_lib=True)
get_using_function_symbol_for_func(target_type, target_type_scope, symbol, operator=None)
process_using_any_type(context: Context, node: solidity_parser.ast.solnodes.UsingDirective)
process_using_library_type(context: Context, node: solidity_parser.ast.solnodes.UsingDirective)
find_using_current_scope(node, context)
process_using_functions(node: solidity_parser.ast.solnodes.UsingDirective, context: Context)
process_using_directive(node: solidity_parser.ast.solnodes.UsingDirective, context: Context)
make_symbols_for_node(node, context: Context, build_skeletons: bool, visit_index: int)