From e51c286f8b42e2aa35e113f4336090121fa45d40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Gir=C3=A1ldez?= Date: Mon, 7 Oct 2024 15:25:17 -0400 Subject: [PATCH] Simplify built-ins import/export in the stack graph --- .../inputs/language/bindings/rules.msgb | 48 +++++++----------- .../bindings/generated/binding_rules.rs | 50 ++++++++----------- 2 files changed, 40 insertions(+), 58 deletions(-) diff --git a/crates/solidity/inputs/language/bindings/rules.msgb b/crates/solidity/inputs/language/bindings/rules.msgb index 3ee2ed552..de239c4d4 100644 --- a/crates/solidity/inputs/language/bindings/rules.msgb +++ b/crates/solidity/inputs/language/bindings/rules.msgb @@ -34,30 +34,10 @@ inherit .parent_scope ;; Automatically import language built-ins if (not (eq FILE_PATH BUILT_INS_FILE_PATH)) { - ;; All built-in symbols are defined in an internal contract named - ;; '$BuiltIns$' (see __SLANG_SOLIDITY_BUILT_INS_CONTRACT_NAME__) so we need - ;; to construct an equivalent import path to reach them. We should have - ;; access to both type members (eg. defined enums & structs) as well as - ;; functions and state variables (see special case below), hence why we're - ;; introducing a path through `@typeof`. node built_ins attr (built_ins) push_symbol = BUILT_INS_FILE_PATH - node built_in_contract - ; __SLANG_SOLIDITY_BUILT_INS_CONTRACT_NAME__ keep in sync with built-ins generation - attr (built_in_contract) push_symbol = "$BuiltIns$" - - node built_in_typeof - attr (built_in_typeof) push_symbol = "@typeof" - - node built_in_member - attr (built_in_member) push_symbol = "." - - edge @source_unit.lexical_scope -> built_in_member - edge built_in_member -> built_in_contract - edge built_in_member -> built_in_typeof - edge built_in_typeof -> built_in_contract - edge built_in_contract -> built_ins + edge @source_unit.lexical_scope -> built_ins edge built_ins -> ROOT_NODE } @@ -89,6 +69,21 @@ inherit .parent_scope edge @source_unit.defs -> @unit_member.def } +;; Special case for built-ins: we want to export all symbols in the contract: +;; functions, types and state variables. All built-in symbols are defined in an +;; internal contract named '$BuiltIns$' so we need to export all its members and +;; type members directly as a source unit definition. +;; __SLANG_SOLIDITY_BUILT_INS_CONTRACT_NAME__ keep in sync with built-ins generation. +@source_unit [SourceUnit [SourceUnitMembers + [SourceUnitMember @contract [ContractDefinition name: ["$BuiltIns$"]]] +]] { + if (eq FILE_PATH BUILT_INS_FILE_PATH) { + edge @source_unit.defs -> @contract.members + edge @source_unit.defs -> @contract.type_members + edge @source_unit.defs -> @contract.state_vars + } +} + @source_unit [SourceUnit [SourceUnitMembers [SourceUnitMember @using [UsingDirective]]]] { let @using.lexical_scope = @source_unit.lexical_scope edge @source_unit.lexical_scope -> @using.def @@ -288,9 +283,11 @@ inherit .parent_scope node @contract.members node @contract.type_members node @contract.modifiers + node @contract.state_vars edge @contract.lexical_scope -> @contract.members edge @contract.lexical_scope -> @contract.type_members + edge @contract.lexical_scope -> @contract.state_vars ;; Modifiers are available as a contract type members through a special '@modifier' symbol node modifier @@ -422,13 +419,7 @@ inherit .parent_scope | [StateVariableDefinition] )] ]] { - edge @contract.lexical_scope -> @member.def - - ;; Special case: for the built-ins file, export state variables in the - ;; contract's members so they can be seen as globals - if (eq FILE_PATH BUILT_INS_FILE_PATH) { - edge @contract.members -> @member.def - } + edge @contract.state_vars -> @member.def } ;; Public state variables are also exposed as external member functions @@ -1551,7 +1542,6 @@ inherit .parent_scope attr (@state_var.def) node_definition = @name attr (@state_var.def) definiens_node = @state_var - edge @state_var.def -> @state_var.def edge @type_name.type_ref -> @state_var.lexical_scope node typeof diff --git a/crates/solidity/outputs/cargo/slang_solidity/src/generated/bindings/generated/binding_rules.rs b/crates/solidity/outputs/cargo/slang_solidity/src/generated/bindings/generated/binding_rules.rs index 9ed56f171..f45c0b0a7 100644 --- a/crates/solidity/outputs/cargo/slang_solidity/src/generated/bindings/generated/binding_rules.rs +++ b/crates/solidity/outputs/cargo/slang_solidity/src/generated/bindings/generated/binding_rules.rs @@ -39,30 +39,10 @@ inherit .parent_scope ;; Automatically import language built-ins if (not (eq FILE_PATH BUILT_INS_FILE_PATH)) { - ;; All built-in symbols are defined in an internal contract named - ;; '$BuiltIns$' (see __SLANG_SOLIDITY_BUILT_INS_CONTRACT_NAME__) so we need - ;; to construct an equivalent import path to reach them. We should have - ;; access to both type members (eg. defined enums & structs) as well as - ;; functions and state variables (see special case below), hence why we're - ;; introducing a path through `@typeof`. node built_ins attr (built_ins) push_symbol = BUILT_INS_FILE_PATH - node built_in_contract - ; __SLANG_SOLIDITY_BUILT_INS_CONTRACT_NAME__ keep in sync with built-ins generation - attr (built_in_contract) push_symbol = "$BuiltIns$" - - node built_in_typeof - attr (built_in_typeof) push_symbol = "@typeof" - - node built_in_member - attr (built_in_member) push_symbol = "." - - edge @source_unit.lexical_scope -> built_in_member - edge built_in_member -> built_in_contract - edge built_in_member -> built_in_typeof - edge built_in_typeof -> built_in_contract - edge built_in_contract -> built_ins + edge @source_unit.lexical_scope -> built_ins edge built_ins -> ROOT_NODE } @@ -94,6 +74,23 @@ inherit .parent_scope edge @source_unit.defs -> @unit_member.def } +@source_unit [SourceUnit [SourceUnitMembers + [SourceUnitMember @contract [ContractDefinition name: ["$BuiltIns$"]]] +]] { + ; Special case for built-ins: we want to export all symbols in the contract: + ; functions, types and state variables + + ; All built-in symbols are defined in an internal contract named + ; '$BuiltIns$' so we need to export all its members and type members directly + ; as a source unit definition. + ; __SLANG_SOLIDITY_BUILT_INS_CONTRACT_NAME__ keep in sync with built-ins generation. + if (eq FILE_PATH BUILT_INS_FILE_PATH) { + edge @source_unit.defs -> @contract.members + edge @source_unit.defs -> @contract.type_members + edge @source_unit.defs -> @contract.state_vars + } +} + @source_unit [SourceUnit [SourceUnitMembers [SourceUnitMember @using [UsingDirective]]]] { let @using.lexical_scope = @source_unit.lexical_scope edge @source_unit.lexical_scope -> @using.def @@ -293,9 +290,11 @@ inherit .parent_scope node @contract.members node @contract.type_members node @contract.modifiers + node @contract.state_vars edge @contract.lexical_scope -> @contract.members edge @contract.lexical_scope -> @contract.type_members + edge @contract.lexical_scope -> @contract.state_vars ;; Modifiers are available as a contract type members through a special '@modifier' symbol node modifier @@ -427,13 +426,7 @@ inherit .parent_scope | [StateVariableDefinition] )] ]] { - edge @contract.lexical_scope -> @member.def - - ;; Special case: for the built-ins file, export state variables in the - ;; contract's members so they can be seen as globals - if (eq FILE_PATH BUILT_INS_FILE_PATH) { - edge @contract.members -> @member.def - } + edge @contract.state_vars -> @member.def } ;; Public state variables are also exposed as external member functions @@ -1556,7 +1549,6 @@ inherit .parent_scope attr (@state_var.def) node_definition = @name attr (@state_var.def) definiens_node = @state_var - edge @state_var.def -> @state_var.def edge @type_name.type_ref -> @state_var.lexical_scope node typeof