From d2238658890a5b4a36de819406e5c388c662e87f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Gir=C3=A1ldez?= Date: Wed, 21 Aug 2024 08:43:59 -0300 Subject: [PATCH] Add binding rules for arrays, mappings and other types (#1082) This PR adds binding rules to allow support name resolution of: - Top-level constants (contract/library/interface constants were already supported because they are parsed as state variables) - Mappings: both resolving key and value types as well as computing the result type of indexing - Arrays: same as above; missing support for array type members, which will be covered when we implement built-ins support - User defined value types; same as above to member functions - Function types; ditto --- .../inputs/language/bindings/rules.msgb | 148 +++++++++++++++++- .../bindings/generated/binding_rules.rs | 148 +++++++++++++++++- .../bindings_assertions/generated/arrays.rs | 15 ++ .../generated/constants.rs | 15 ++ .../generated/function_types.rs | 15 ++ .../bindings_assertions/generated/mappings.rs | 20 +++ .../src/bindings_assertions/generated/mod.rs | 5 + .../generated/user_types.rs | 10 ++ .../src/bindings_output/generated/mappings.rs | 15 ++ .../src/bindings_output/generated/mod.rs | 1 + .../arrays/nested_custom.sol | 41 +++++ .../bindings_assertions/arrays/simple.sol | 15 ++ .../bindings_assertions/constants/simple.sol | 24 +++ .../constants/top_level.sol | 27 ++++ .../function_types/external.sol | 32 ++++ .../function_types/internal.sol | 21 +++ .../bindings_assertions/mappings/nested.sol | 33 ++++ .../mappings/nested_custom.sol | 35 +++++ .../bindings_assertions/mappings/simple.sol | 27 ++++ .../user_types/visibility.sol | 33 ++++ .../custom_types/generated/0.4.11-success.txt | 122 +++++++++++++++ .../mappings/custom_types/input.sol | 26 +++ .../indexing/generated/0.4.11-success.txt | 34 ++++ .../mappings/indexing/input.sol | 10 ++ .../sample/generated/0.6.0-success.txt | 4 +- 25 files changed, 868 insertions(+), 8 deletions(-) create mode 100644 crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/arrays.rs create mode 100644 crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/constants.rs create mode 100644 crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/function_types.rs create mode 100644 crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/mappings.rs create mode 100644 crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/user_types.rs create mode 100644 crates/solidity/outputs/cargo/tests/src/bindings_output/generated/mappings.rs create mode 100644 crates/solidity/testing/snapshots/bindings_assertions/arrays/nested_custom.sol create mode 100644 crates/solidity/testing/snapshots/bindings_assertions/arrays/simple.sol create mode 100644 crates/solidity/testing/snapshots/bindings_assertions/constants/simple.sol create mode 100644 crates/solidity/testing/snapshots/bindings_assertions/constants/top_level.sol create mode 100644 crates/solidity/testing/snapshots/bindings_assertions/function_types/external.sol create mode 100644 crates/solidity/testing/snapshots/bindings_assertions/function_types/internal.sol create mode 100644 crates/solidity/testing/snapshots/bindings_assertions/mappings/nested.sol create mode 100644 crates/solidity/testing/snapshots/bindings_assertions/mappings/nested_custom.sol create mode 100644 crates/solidity/testing/snapshots/bindings_assertions/mappings/simple.sol create mode 100644 crates/solidity/testing/snapshots/bindings_assertions/user_types/visibility.sol create mode 100644 crates/solidity/testing/snapshots/bindings_output/mappings/custom_types/generated/0.4.11-success.txt create mode 100644 crates/solidity/testing/snapshots/bindings_output/mappings/custom_types/input.sol create mode 100644 crates/solidity/testing/snapshots/bindings_output/mappings/indexing/generated/0.4.11-success.txt create mode 100644 crates/solidity/testing/snapshots/bindings_output/mappings/indexing/input.sol diff --git a/crates/solidity/inputs/language/bindings/rules.msgb b/crates/solidity/inputs/language/bindings/rules.msgb index aa558ea919..38dfd0bf63 100644 --- a/crates/solidity/inputs/language/bindings/rules.msgb +++ b/crates/solidity/inputs/language/bindings/rules.msgb @@ -213,6 +213,7 @@ attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, i | [StructDefinition] | [EventDefinition] | [ErrorDefinition] + | [UserDefinedValueTypeDefinition] )] ]] { edge @member.lexical_scope -> @contract.lexical_scope @@ -280,6 +281,7 @@ attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, i | [StructDefinition] | [EventDefinition] | [ErrorDefinition] + | [UserDefinedValueTypeDefinition] )] ]] { edge @member.lexical_scope -> @interface.lexical_scope @@ -327,6 +329,7 @@ attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, i | [StructDefinition] | [EventDefinition] | [ErrorDefinition] + | [UserDefinedValueTypeDefinition] )] ]] { edge @member.lexical_scope -> @library.lexical_scope @@ -358,6 +361,111 @@ attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, i edge @type_name.output -> @id_path.right } +@type_name [TypeName @mapping [MappingType]] { + edge @mapping.lexical_scope -> @type_name.type_ref + edge @type_name.output -> @mapping.output +} + +@type_name [TypeName @array [ArrayTypeName]] { + edge @array.lexical_scope -> @type_name.type_ref + edge @type_name.output -> @array.output +} + +@type_name [TypeName @ftype [FunctionType]] { + edge @ftype.lexical_scope -> @type_name.type_ref + edge @type_name.output -> @ftype.output +} + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; Mappings +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +@mapping [MappingType] { + node @mapping.lexical_scope + node @mapping.output +} + +@mapping [MappingType [MappingKey [MappingKeyType @key_ident [IdentifierPath]]]] { + edge @key_ident.left -> @mapping.lexical_scope +} + +@mapping [MappingType [MappingValue @value_type [TypeName]]] { + edge @value_type.type_ref -> @mapping.lexical_scope + + node typeof_input + attr (typeof_input) pop_symbol = "@typeof" + + node index + attr (index) pop_symbol = "[]" + + node typeof_output + attr (typeof_output) push_symbol = "@typeof" + + ;; The mapping's type exposes the `[]` operator that returns the value type + edge @mapping.output -> typeof_input + edge typeof_input -> index + edge index -> typeof_output + edge typeof_output -> @value_type.output +} + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; Arrays types +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +@array [ArrayTypeName] { + node @array.lexical_scope + node @array.output +} + +@array [ArrayTypeName @type_name [TypeName]] { + edge @type_name.type_ref -> @array.lexical_scope + + node typeof_input + attr (typeof_input) pop_symbol = "@typeof" + + node index + attr (index) pop_symbol = "[]" + + node typeof_output + attr (typeof_output) push_symbol = "@typeof" + + ;; The array type exposes the `[]` operator that returns the value type + edge @array.output -> typeof_input + edge typeof_input -> index + edge index -> typeof_output + edge typeof_output -> @type_name.output +} + +@array [ArrayTypeName @size index: [Expression]] { + edge @size.lexical_scope -> @array.lexical_scope +} + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; Function types +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +@ftype [FunctionType] { + node @ftype.lexical_scope + node @ftype.output +} + +@ftype [FunctionType @params [ParametersDeclaration]] { + edge @params.lexical_scope -> @ftype.lexical_scope +} + +@ftype [FunctionType [ReturnsDeclaration @return_params [ParametersDeclaration]]] { + edge @return_params.lexical_scope -> @ftype.lexical_scope +} + + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; Identifier Paths (aka. references to custom types) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; The identifier path constructs a path of nodes connected from right to left @id_path [IdentifierPath] { node @id_path.left @@ -970,9 +1078,32 @@ attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, i node @constant.def } -@value_type [UserDefinedValueTypeDefinition] { - node @value_type.lexical_scope - node @value_type.def +@constant [ConstantDefinition + @type_name type_name: [TypeName] + @name name: [Identifier] + @value value: [Expression] +] { + node def + attr (def) node_definition = @name + attr (def) definiens_node = @constant + + edge @constant.def -> def + + edge @value.lexical_scope -> @constant.lexical_scope + edge @type_name.type_ref -> @constant.lexical_scope +} + +@user_type [UserDefinedValueTypeDefinition] { + node @user_type.lexical_scope + node @user_type.def +} + +@user_type [UserDefinedValueTypeDefinition @name [Identifier]] { + node def + attr (def) node_definition = @name + attr (def) definiens_node = @user_type + + edge @user_type.def -> def } @@ -1024,6 +1155,17 @@ attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, i edge @expr.output -> ref } +;; Index access expressions +@expr [Expression [IndexAccessExpression + @operand operand: [Expression] +]] { + node index + attr (index) push_symbol = "[]" + + edge @expr.output -> index + edge index -> @operand.output +} + ;; Type expressions @type_expr [Expression [TypeExpression @type [TypeName]]] { edge @type.type_ref -> @type_expr.lexical_scope 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 edc47ecd01..9735349c8d 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 @@ -218,6 +218,7 @@ attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, i | [StructDefinition] | [EventDefinition] | [ErrorDefinition] + | [UserDefinedValueTypeDefinition] )] ]] { edge @member.lexical_scope -> @contract.lexical_scope @@ -285,6 +286,7 @@ attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, i | [StructDefinition] | [EventDefinition] | [ErrorDefinition] + | [UserDefinedValueTypeDefinition] )] ]] { edge @member.lexical_scope -> @interface.lexical_scope @@ -332,6 +334,7 @@ attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, i | [StructDefinition] | [EventDefinition] | [ErrorDefinition] + | [UserDefinedValueTypeDefinition] )] ]] { edge @member.lexical_scope -> @library.lexical_scope @@ -363,6 +366,111 @@ attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, i edge @type_name.output -> @id_path.right } +@type_name [TypeName @mapping [MappingType]] { + edge @mapping.lexical_scope -> @type_name.type_ref + edge @type_name.output -> @mapping.output +} + +@type_name [TypeName @array [ArrayTypeName]] { + edge @array.lexical_scope -> @type_name.type_ref + edge @type_name.output -> @array.output +} + +@type_name [TypeName @ftype [FunctionType]] { + edge @ftype.lexical_scope -> @type_name.type_ref + edge @type_name.output -> @ftype.output +} + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; Mappings +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +@mapping [MappingType] { + node @mapping.lexical_scope + node @mapping.output +} + +@mapping [MappingType [MappingKey [MappingKeyType @key_ident [IdentifierPath]]]] { + edge @key_ident.left -> @mapping.lexical_scope +} + +@mapping [MappingType [MappingValue @value_type [TypeName]]] { + edge @value_type.type_ref -> @mapping.lexical_scope + + node typeof_input + attr (typeof_input) pop_symbol = "@typeof" + + node index + attr (index) pop_symbol = "[]" + + node typeof_output + attr (typeof_output) push_symbol = "@typeof" + + ;; The mapping's type exposes the `[]` operator that returns the value type + edge @mapping.output -> typeof_input + edge typeof_input -> index + edge index -> typeof_output + edge typeof_output -> @value_type.output +} + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; Arrays types +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +@array [ArrayTypeName] { + node @array.lexical_scope + node @array.output +} + +@array [ArrayTypeName @type_name [TypeName]] { + edge @type_name.type_ref -> @array.lexical_scope + + node typeof_input + attr (typeof_input) pop_symbol = "@typeof" + + node index + attr (index) pop_symbol = "[]" + + node typeof_output + attr (typeof_output) push_symbol = "@typeof" + + ;; The array type exposes the `[]` operator that returns the value type + edge @array.output -> typeof_input + edge typeof_input -> index + edge index -> typeof_output + edge typeof_output -> @type_name.output +} + +@array [ArrayTypeName @size index: [Expression]] { + edge @size.lexical_scope -> @array.lexical_scope +} + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; Function types +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +@ftype [FunctionType] { + node @ftype.lexical_scope + node @ftype.output +} + +@ftype [FunctionType @params [ParametersDeclaration]] { + edge @params.lexical_scope -> @ftype.lexical_scope +} + +@ftype [FunctionType [ReturnsDeclaration @return_params [ParametersDeclaration]]] { + edge @return_params.lexical_scope -> @ftype.lexical_scope +} + + + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;; Identifier Paths (aka. references to custom types) +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + ;; The identifier path constructs a path of nodes connected from right to left @id_path [IdentifierPath] { node @id_path.left @@ -975,9 +1083,32 @@ attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, i node @constant.def } -@value_type [UserDefinedValueTypeDefinition] { - node @value_type.lexical_scope - node @value_type.def +@constant [ConstantDefinition + @type_name type_name: [TypeName] + @name name: [Identifier] + @value value: [Expression] +] { + node def + attr (def) node_definition = @name + attr (def) definiens_node = @constant + + edge @constant.def -> def + + edge @value.lexical_scope -> @constant.lexical_scope + edge @type_name.type_ref -> @constant.lexical_scope +} + +@user_type [UserDefinedValueTypeDefinition] { + node @user_type.lexical_scope + node @user_type.def +} + +@user_type [UserDefinedValueTypeDefinition @name [Identifier]] { + node def + attr (def) node_definition = @name + attr (def) definiens_node = @user_type + + edge @user_type.def -> def } @@ -1029,6 +1160,17 @@ attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, i edge @expr.output -> ref } +;; Index access expressions +@expr [Expression [IndexAccessExpression + @operand operand: [Expression] +]] { + node index + attr (index) push_symbol = "[]" + + edge @expr.output -> index + edge index -> @operand.output +} + ;; Type expressions @type_expr [Expression [TypeExpression @type [TypeName]]] { edge @type.type_ref -> @type_expr.lexical_scope diff --git a/crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/arrays.rs b/crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/arrays.rs new file mode 100644 index 0000000000..b0b4a18448 --- /dev/null +++ b/crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/arrays.rs @@ -0,0 +1,15 @@ +// This file is generated automatically by infrastructure scripts. Please don't edit by hand. + +use anyhow::Result; + +use crate::bindings_assertions::runner::run; + +#[test] +fn nested_custom() -> Result<()> { + run("arrays", "nested_custom") +} + +#[test] +fn simple() -> Result<()> { + run("arrays", "simple") +} diff --git a/crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/constants.rs b/crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/constants.rs new file mode 100644 index 0000000000..d8138e78b7 --- /dev/null +++ b/crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/constants.rs @@ -0,0 +1,15 @@ +// This file is generated automatically by infrastructure scripts. Please don't edit by hand. + +use anyhow::Result; + +use crate::bindings_assertions::runner::run; + +#[test] +fn simple() -> Result<()> { + run("constants", "simple") +} + +#[test] +fn top_level() -> Result<()> { + run("constants", "top_level") +} diff --git a/crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/function_types.rs b/crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/function_types.rs new file mode 100644 index 0000000000..2d54ed42c5 --- /dev/null +++ b/crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/function_types.rs @@ -0,0 +1,15 @@ +// This file is generated automatically by infrastructure scripts. Please don't edit by hand. + +use anyhow::Result; + +use crate::bindings_assertions::runner::run; + +#[test] +fn external() -> Result<()> { + run("function_types", "external") +} + +#[test] +fn internal() -> Result<()> { + run("function_types", "internal") +} diff --git a/crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/mappings.rs b/crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/mappings.rs new file mode 100644 index 0000000000..345164a53f --- /dev/null +++ b/crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/mappings.rs @@ -0,0 +1,20 @@ +// This file is generated automatically by infrastructure scripts. Please don't edit by hand. + +use anyhow::Result; + +use crate::bindings_assertions::runner::run; + +#[test] +fn nested() -> Result<()> { + run("mappings", "nested") +} + +#[test] +fn nested_custom() -> Result<()> { + run("mappings", "nested_custom") +} + +#[test] +fn simple() -> Result<()> { + run("mappings", "simple") +} diff --git a/crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/mod.rs b/crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/mod.rs index 90396f3cc8..67640a3eeb 100644 --- a/crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/mod.rs +++ b/crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/mod.rs @@ -1,11 +1,16 @@ // This file is generated automatically by infrastructure scripts. Please don't edit by hand. +mod arrays; +mod constants; mod contracts; mod control; mod enums; mod expressions; +mod function_types; mod functions; mod imports; mod interfaces; +mod mappings; mod scoping; +mod user_types; mod variables; diff --git a/crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/user_types.rs b/crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/user_types.rs new file mode 100644 index 0000000000..66cd48f377 --- /dev/null +++ b/crates/solidity/outputs/cargo/tests/src/bindings_assertions/generated/user_types.rs @@ -0,0 +1,10 @@ +// This file is generated automatically by infrastructure scripts. Please don't edit by hand. + +use anyhow::Result; + +use crate::bindings_assertions::runner::run; + +#[test] +fn visibility() -> Result<()> { + run("user_types", "visibility") +} diff --git a/crates/solidity/outputs/cargo/tests/src/bindings_output/generated/mappings.rs b/crates/solidity/outputs/cargo/tests/src/bindings_output/generated/mappings.rs new file mode 100644 index 0000000000..c9a660ac10 --- /dev/null +++ b/crates/solidity/outputs/cargo/tests/src/bindings_output/generated/mappings.rs @@ -0,0 +1,15 @@ +// This file is generated automatically by infrastructure scripts. Please don't edit by hand. + +use anyhow::Result; + +use crate::bindings_output::runner::run; + +#[test] +fn custom_types() -> Result<()> { + run("mappings", "custom_types") +} + +#[test] +fn indexing() -> Result<()> { + run("mappings", "indexing") +} diff --git a/crates/solidity/outputs/cargo/tests/src/bindings_output/generated/mod.rs b/crates/solidity/outputs/cargo/tests/src/bindings_output/generated/mod.rs index b38cc4af89..c52faedd76 100644 --- a/crates/solidity/outputs/cargo/tests/src/bindings_output/generated/mod.rs +++ b/crates/solidity/outputs/cargo/tests/src/bindings_output/generated/mod.rs @@ -7,5 +7,6 @@ mod expressions; mod imports; mod interfaces; mod libraries; +mod mappings; mod structs; mod variables; diff --git a/crates/solidity/testing/snapshots/bindings_assertions/arrays/nested_custom.sol b/crates/solidity/testing/snapshots/bindings_assertions/arrays/nested_custom.sol new file mode 100644 index 0000000000..b9d71162e7 --- /dev/null +++ b/crates/solidity/testing/snapshots/bindings_assertions/arrays/nested_custom.sol @@ -0,0 +1,41 @@ +contract CustomArrays { + uint constant SIZE = 5; + // ^def:7 + + struct Value { + // ^def:1 + uint value; + // ^def:2 + } + + Value[][SIZE] values; + // ^def:3 + // ^ref:7 + // 100); + // ^ref:4 + require(msg.sender == MY_ADDRESS); + // ^ref:3 + } +} diff --git a/crates/solidity/testing/snapshots/bindings_assertions/constants/top_level.sol b/crates/solidity/testing/snapshots/bindings_assertions/constants/top_level.sol new file mode 100644 index 0000000000..679939b45f --- /dev/null +++ b/crates/solidity/testing/snapshots/bindings_assertions/constants/top_level.sol @@ -0,0 +1,27 @@ +contract Constants { + function test() public { + assert(MY_DIRECTION != Direction.NORTH); + // ^ref:! (< 0.7.4) + // ^ref:5 (>= 0.7.4) + assert(MY_UINT > 100); + // ^ref:! (< 0.7.4) + // ^ref:4 (>= 0.7.4) + require(msg.sender == MY_ADDRESS); + // ^ref:! (< 0.7.4) + // ^ref:3 (>= 0.7.4) + } +} + +enum Direction { NORTH, SOUTH, EAST, WEST } +// ^def:1 (>= 0.6.0) +// ^def:2 (>= 0.6.0) + +address constant MY_ADDRESS = 0x777788889999AaAAbBbbCcccddDdeeeEfFFfCcCc; +// ^def:3 (>= 0.7.4) +uint256 constant MY_UINT = 123; +// ^def:4 (>= 0.7.4) +Direction constant MY_DIRECTION = Direction.EAST; +// ^ref:2 (>= 0.7.4) +// ^ref:1 (>= 0.7.4) +// ^def:5 (>= 0.7.4) +//= 0.7.4) diff --git a/crates/solidity/testing/snapshots/bindings_assertions/function_types/external.sol b/crates/solidity/testing/snapshots/bindings_assertions/function_types/external.sol new file mode 100644 index 0000000000..cf312ed300 --- /dev/null +++ b/crates/solidity/testing/snapshots/bindings_assertions/function_types/external.sol @@ -0,0 +1,32 @@ +contract Test { + struct Coords { + // ^def:1 + int x; + int y; + } + enum HitTest { + //^def:2 + Inside, + Outside + } + struct Box { + // ^def:3 + function (Coords memory) external returns (HitTest) hitTest; + // ^def:4 + // ^ref:2 + // ^ref:1 + } + Box[] boxes; + // ^def:5 + // mapping(uint256 => bool)) public nested; + // ^def:1 + + function get(address _addr1, uint256 _i) public view returns (bool) { + // ^def:2 + // ^def:3 + return nested[_addr1][_i]; + // ^ref:2 + // ^ref:3 + // ^ref:1 + } + + function set(address _addr1, uint256 _i, bool _boo) public { + // ^def:4 + // ^def:5 + // ^def:6 + nested[_addr1][_i] = _boo; + // ^ref:4 + // ^ref:5 + // ^ref:6 + // mapping(Kind => Values)) vaults; + // ^def:4 + // ^ref:2 + // ^ref:1 + + function store(address _addr, Kind _kind, uint _amount) public { + // ^def:5 + // ^def:6 + // ^def:7 + vaults[_addr][_kind].balance += _amount; + // ^ref:5 + // ^ref:3 + // ^ref:6 + // ^ref:7 + // uint256) public myMap; + // ^def:1 + + function get(address _addr) public view returns (uint256) { + // ^def:2 + return myMap[_addr]; + // ^ref:2 + // ^ref:1 + } + + function set(address _addr, uint256 _i) public { + // ^def:3 + // ^def:4 + myMap[_addr] = _i; + // ^ref:3 + // ^ref:4 + //= 0.8.8) + + function test() public returns (int32) { + Internal inter = Internal.wrap(10); + // ^ref:2 (>= 0.8.8) + //= 0.8.8) + TopLevel tl = TopLevel.wrap(20); + // ^ref:1 (>= 0.8.8) + //= 0.8.8) + return MyLib.LibType.unwrap(MyLib.create()); + // ^ref:3 + // ^ref:4 (>= 0.8.8) + // ^ref:3 (>= 0.8.8) + } +} + +type TopLevel is uint256; +// ^def:1 (>= 0.8.8) + +library MyLib { + // ^def:3 + type LibType is int32; + // ^def:4 (>= 0.8.8) + + function create() public returns (LibType) { + // ^ref:4 (>= 0.8.8) + return LibType.wrap(30); + // ^ref:4 (>= 0.8.8) + } +} diff --git a/crates/solidity/testing/snapshots/bindings_output/mappings/custom_types/generated/0.4.11-success.txt b/crates/solidity/testing/snapshots/bindings_output/mappings/custom_types/generated/0.4.11-success.txt new file mode 100644 index 0000000000..46a3331820 --- /dev/null +++ b/crates/solidity/testing/snapshots/bindings_output/mappings/custom_types/generated/0.4.11-success.txt @@ -0,0 +1,122 @@ +# This file is generated automatically by infrastructure scripts. Please don't edit by hand. + +References and definitions: + ╭─[input.sol:1:1] + │ + 1 │ contract Mappings { + │ ────┬─── + │ ╰───── def: 1 + 2 │ enum Direction { North, East, South, West } + │ ────┬──── ──┬── ──┬─ ──┬── ──┬─ + │ ╰───────────────────────────────── def: 2 + │ │ │ │ │ + │ ╰─────────────────────── def: 3 + │ │ │ │ + │ ╰──────────────── def: 4 + │ │ │ + │ ╰────────── def: 5 + │ │ + │ ╰─── def: 6 + 3 │ enum Kind { Zombie, Bat, Skeleton, Dragon } + │ ──┬─ ───┬── ─┬─ ────┬─── ───┬── + │ ╰─────────────────────────────────── def: 7 + │ │ │ │ │ + │ ╰─────────────────────────── def: 8 + │ │ │ │ + │ ╰───────────────────── def: 9 + │ │ │ + │ ╰───────────── def: 10 + │ │ + │ ╰──── def: 11 + 4 │ struct Monster { + │ ───┬─── + │ ╰───── def: 12 + 5 │ Kind kind; + │ ──┬─ ──┬─ + │ ╰──────── ref: 7 + │ │ + │ ╰─── def: 13 + 6 │ uint life; + │ ──┬─ + │ ╰─── def: 14 + │ + 9 │ mapping(Direction => Monster) monsters; + │ ────┬──── ───┬─── ────┬─── + │ ╰─────────────────────────── ref: 2 + │ │ │ + │ ╰─────────────── ref: 12 + │ │ + │ ╰───── def: 15 + │ + 11 │ function spawn(Direction _dir, Kind _kind) public { + │ ──┬── ────┬──── ──┬─ ──┬─ ──┬── + │ ╰─────────────────────────────── def: 16 + │ │ │ │ │ + │ ╰─────────────────────── ref: 2 + │ │ │ │ + │ ╰─────────────── def: 17 + │ │ │ + │ ╰───────── ref: 7 + │ │ + │ ╰──── def: 18 + 12 │ monsters[_dir] = Monster(_kind, 100); + │ ────┬─── ──┬─ ───┬─── ──┬── + │ ╰─────────────────────────── ref: 15 + │ │ │ │ + │ ╰──────────────────── ref: 17 + │ │ │ + │ ╰─────────── ref: 12 + │ │ + │ ╰──── ref: 18 + │ + 15 │ function attack(Direction _dir, uint _power) public { + │ ───┬── ────┬──── ──┬─ ───┬── + │ ╰──────────────────────────────── def: 19 + │ │ │ │ + │ ╰──────────────────────── ref: 2 + │ │ │ + │ ╰──────────────── def: 20 + │ │ + │ ╰──── def: 21 + 16 │ if (monsters[_dir].life > _power) { + │ ────┬─── ──┬─ ──┬─ ───┬── + │ ╰───────────────────────── ref: 15 + │ │ │ │ + │ ╰────────────────── ref: 20 + │ │ │ + │ ╰──────────── ref: 14 + │ │ + │ ╰──── ref: 21 + 17 │ monsters[_dir].life -= _power; + │ ────┬─── ──┬─ ──┬─ ───┬── + │ ╰────────────────────────── ref: 15 + │ │ │ │ + │ ╰─────────────────── ref: 20 + │ │ │ + │ ╰───────────── ref: 14 + │ │ + │ ╰──── ref: 21 + │ + 19 │ delete monsters[_dir]; + │ ────┬─── ──┬─ + │ ╰────────── ref: 15 + │ │ + │ ╰─── ref: 20 + │ + 23 │ function get_type(Direction _dir) public returns (Kind) { + │ ────┬─── ────┬──── ──┬─ ──┬─ + │ ╰────────────────────────────────────────── def: 22 + │ │ │ │ + │ ╰───────────────────────────────── ref: 2 + │ │ │ + │ ╰───────────────────────── def: 23 + │ │ + │ ╰─── ref: 7 + 24 │ return monsters[_dir].kind; + │ ────┬─── ──┬─ ──┬─ + │ ╰──────────────── ref: 15 + │ │ │ + │ ╰───────── ref: 23 + │ │ + │ ╰─── ref: 13 +────╯ diff --git a/crates/solidity/testing/snapshots/bindings_output/mappings/custom_types/input.sol b/crates/solidity/testing/snapshots/bindings_output/mappings/custom_types/input.sol new file mode 100644 index 0000000000..cffa624573 --- /dev/null +++ b/crates/solidity/testing/snapshots/bindings_output/mappings/custom_types/input.sol @@ -0,0 +1,26 @@ +contract Mappings { + enum Direction { North, East, South, West } + enum Kind { Zombie, Bat, Skeleton, Dragon } + struct Monster { + Kind kind; + uint life; + } + + mapping(Direction => Monster) monsters; + + function spawn(Direction _dir, Kind _kind) public { + monsters[_dir] = Monster(_kind, 100); + } + + function attack(Direction _dir, uint _power) public { + if (monsters[_dir].life > _power) { + monsters[_dir].life -= _power; + } else { + delete monsters[_dir]; + } + } + + function get_type(Direction _dir) public returns (Kind) { + return monsters[_dir].kind; + } +} diff --git a/crates/solidity/testing/snapshots/bindings_output/mappings/indexing/generated/0.4.11-success.txt b/crates/solidity/testing/snapshots/bindings_output/mappings/indexing/generated/0.4.11-success.txt new file mode 100644 index 0000000000..f8f34b9209 --- /dev/null +++ b/crates/solidity/testing/snapshots/bindings_output/mappings/indexing/generated/0.4.11-success.txt @@ -0,0 +1,34 @@ +# This file is generated automatically by infrastructure scripts. Please don't edit by hand. + +References and definitions: + ╭─[input.sol:1:1] + │ + 1 │ contract Test { + │ ──┬─ + │ ╰─── def: 1 + 2 │ struct Value { + │ ──┬── + │ ╰──── def: 2 + 3 │ uint value; + │ ──┬── + │ ╰──── def: 3 + │ + 5 │ mapping(uint => Value) values; + │ ──┬── ───┬── + │ ╰──────────── ref: 2 + │ │ + │ ╰──── def: 4 + │ + 7 │ function test(uint _id) public returns (uint) { + │ ──┬─ ─┬─ + │ ╰──────────── def: 5 + │ │ + │ ╰─── def: 6 + 8 │ return values[_id].value; + │ ───┬── ─┬─ ──┬── + │ ╰─────────────── ref: 4 + │ │ │ + │ ╰────────── ref: 6 + │ │ + │ ╰──── ref: 3 +───╯ diff --git a/crates/solidity/testing/snapshots/bindings_output/mappings/indexing/input.sol b/crates/solidity/testing/snapshots/bindings_output/mappings/indexing/input.sol new file mode 100644 index 0000000000..2d3cbd16b7 --- /dev/null +++ b/crates/solidity/testing/snapshots/bindings_output/mappings/indexing/input.sol @@ -0,0 +1,10 @@ +contract Test { + struct Value { + uint value; + } + mapping(uint => Value) values; + + function test(uint _id) public returns (uint) { + return values[_id].value; + } +} diff --git a/crates/solidity/testing/snapshots/bindings_output/structs/sample/generated/0.6.0-success.txt b/crates/solidity/testing/snapshots/bindings_output/structs/sample/generated/0.6.0-success.txt index c3d20c63e8..3d23f28e20 100644 --- a/crates/solidity/testing/snapshots/bindings_output/structs/sample/generated/0.6.0-success.txt +++ b/crates/solidity/testing/snapshots/bindings_output/structs/sample/generated/0.6.0-success.txt @@ -33,7 +33,7 @@ References and definitions: │ ╰──── def: 9 12 │ mapping(uint => Funder) funders; │ ───┬── ───┬─── - │ ╰───────────── unresolved + │ ╰───────────── ref: 1 │ │ │ ╰───── def: 10 │ @@ -42,7 +42,7 @@ References and definitions: │ ╰─────── def: 11 16 │ mapping(uint => Campaign) campaigns; │ ────┬─── ────┬──── - │ ╰──────────────── unresolved + │ ╰──────────────── ref: 5 │ │ │ ╰────── def: 12 │