Skip to content

Commit

Permalink
Adding definitions for mapping parameters (#1221)
Browse files Browse the repository at this point in the history
Fixes #1213

I had to make the rule for `[MappingType]` fault-tolerant in order to
accommodate for older versions of Solidity, by splitting it in two.

Questions:
- Is it possible to avoid having to make a query for each key or value
parameter, and instead have `[Identifier]?` and query for its existence?
- The nodes for the parameters are "hanging in the air", as they don't
participate in anything useful. Should we attach them to the mapping
somehow?
  • Loading branch information
beta-ziliani authored Jan 23, 2025
1 parent 72c0c76 commit be914d9
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 10 deletions.
37 changes: 32 additions & 5 deletions crates/solidity/inputs/language/bindings/rules.msgb
Original file line number Diff line number Diff line change
Expand Up @@ -1033,13 +1033,19 @@ inherit .star_extension
;;; Mappings
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; When there's a parsing error (like a name in parameters for versions below 0.8.18)
;;; we still need the basics to get bindings working
@mapping [MappingType] {
node @mapping.lexical_scope
node @mapping.output
node @mapping.pop_begin
node @mapping.pop_end
}

@mapping [MappingType
[MappingKey [MappingKeyType @key_type ([IdentifierPath] | [ElementaryType])]]
[MappingValue @value_type [TypeName]]
] {
node @mapping.lexical_scope
node @mapping.output

; Define the pushing path of the mapping type
; ValueType <- top of the symbol stack
; KeyType
Expand Down Expand Up @@ -1081,12 +1087,33 @@ inherit .star_extension
node pop_mapping
attr (pop_mapping) pop_symbol = "%Mapping"

let @mapping.pop_begin = @value_type.pop_begin
edge @mapping.pop_begin -> @value_type.pop_begin
edge @value_type.pop_end -> @key_type.pop_begin
edge @key_type.pop_end -> pop_mapping
let @mapping.pop_end = pop_mapping
edge pop_mapping -> @mapping.pop_end

node @mapping.defs
}

@mapping [MappingType
@key [MappingKey [MappingKeyType] @key_name [Identifier]]
[MappingValue]
] {
node @key.def
attr (@key.def) node_definition = @key_name
attr (@key.def) definiens_node = @key
edge @key.def -> @mapping.defs
}

@mapping [MappingType
[MappingKey]
@value [MappingValue [TypeName] @value_name [Identifier]]
] {
node @value.def
attr (@value.def) node_definition = @value_name
attr (@value.def) definiens_node = @value
edge @value.def -> @mapping.defs
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Arrays types
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# This file is generated automatically by infrastructure scripts. Please don't edit by hand.

Parse errors:
Error: Expected EqualGreaterThan or PayableKeyword.
╭─[input.sol:2:19]
2 │ mapping(address name => uint256) public justNameInKey;
│ ───────┬───────
│ ╰───────── Error occurred here.
───╯
Error: Expected CloseParen or OpenBracket.
╭─[input.sol:4:30]
4 │ mapping(address => uint256 amount) public justNameInValue;
│ ───┬──
│ ╰──── Error occurred here.
───╯
Error: Expected EqualGreaterThan or PayableKeyword.
╭─[input.sol:6:19]
6 │ mapping(address name => uint256 amount) public nameInBoth;
│ ───────────┬──────────
│ ╰──────────── Error occurred here.
───╯
References and definitions:
╭─[input.sol:1:1]
1 │ contract NamedMapping {
│ ──────┬─────
│ ╰─────── name: 1
2 │ mapping(address name => uint256) public justNameInKey;
│ ──────┬──────
│ ╰──────── name: 2
4 │ mapping(address => uint256 amount) public justNameInValue;
│ ───────┬───────
│ ╰───────── name: 3
6 │ mapping(address name => uint256 amount) public nameInBoth;
│ ─────┬────
│ ╰────── name: 4
───╯
Definiens:
╭─[input.sol:1:1]
1 │ ╭─────▶ contract NamedMapping {
2 │ │ │ mapping(address name => uint256) public justNameInKey;
│ │ │ ────────────────────────────┬────────────────────────────
│ │ │ ╰────────────────────────────── definiens: 2
3 │ │ ╭───▶
4 │ │ ├─│ ▶ mapping(address => uint256 amount) public justNameInValue;
│ │ │ │
│ │ ╰──────────────────────────────────────────────────────────────────── definiens: 3
5 │ │ ╭─▶
6 │ │ ├─▶ mapping(address name => uint256 amount) public nameInBoth;
│ │ │
│ │ ╰────────────────────────────────────────────────────────────────── definiens: 4
7 │ ├─────▶ }
│ │
│ ╰─────────── definiens: 1
───╯
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# This file is generated automatically by infrastructure scripts. Please don't edit by hand.

References and definitions:
╭─[input.sol:1:1]
1 │ contract NamedMapping {
│ ──────┬─────
│ ╰─────── name: 1
2 │ mapping(address name => uint256) public justNameInKey;
│ ──┬─ ──────┬──────
│ ╰──────────────────────────────────── name: 3
│ │
│ ╰──────── name: 2
4 │ mapping(address => uint256 amount) public justNameInValue;
│ ───┬── ───────┬───────
│ ╰──────────────────────────── name: 5
│ │
│ ╰───────── name: 4
6 │ mapping(address name => uint256 amount) public nameInBoth;
│ ──┬─ ───┬── ─────┬────
│ ╰──────────────────────────────────────── name: 7
│ │ │
│ ╰─────────────────────── name: 8
│ │
│ ╰────── name: 6
───╯
Definiens:
╭─[input.sol:1:1]
1 │ ╭─────▶ contract NamedMapping {
2 │ │ │ mapping(address name => uint256) public justNameInKey;
│ │ │ ────────────────┬───────────┬────────────────────────────
│ │ │ ╰────────────────────────────────────────── definiens: 3
│ │ │ │
│ │ │ ╰────────────────────────────── definiens: 2
3 │ │ ╭───▶
4 │ │ ├─│ ▶ mapping(address => uint256 amount) public justNameInValue;
│ │ │ │ ───────┬───────
│ │ │ │ ╰─────────────────────────────────── definiens: 5
│ │ │ │
│ │ ╰──────────────────────────────────────────────────────────────────── definiens: 4
5 │ │ ╭─▶
6 │ │ ├─▶ mapping(address name => uint256 amount) public nameInBoth;
│ │ │ ──────┬───── ───────┬───────
│ │ │ ╰────────────────────────────────────────────── definiens: 7
│ │ │ │
│ │ │ ╰────────────────────────────── definiens: 8
│ │ │
│ │ ╰────────────────────────────────────────────────────────────────── definiens: 6
7 │ ├─────▶ }
│ │
│ ╰─────────── definiens: 1
───╯
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
contract NamedMapping {
mapping(address name => uint256) public justNameInKey;

mapping(address => uint256 amount) public justNameInValue;

mapping(address name => uint256 amount) public nameInBoth;
}

0 comments on commit be914d9

Please sign in to comment.