Skip to content

Commit

Permalink
Adding binding rules to typeof(<enum>).min and max, and to `<enum…
Browse files Browse the repository at this point in the history
…>.<member>` so they work with extensions (#1177)

Fixes #1158.
Fixes #1196.

Considerations:
 * ~~It's built on top of #1149~~ It's rebased on master.
 * ~~It's currently missing a test for `max`~~
* ~~I adapted an existing test, though maybe it's preferred to add a
different one?~~
* I use the same test for min and max, I think it makes sense to have
them together. I'm not so sure where to put it though. It's testing
these functions, but also in combination with `using for`, so it can
very well be moved to the `using` folder.
 
 EDITED
  • Loading branch information
beta-ziliani authored Jan 23, 2025
1 parent f0a0403 commit 72c0c76
Show file tree
Hide file tree
Showing 12 changed files with 511 additions and 0 deletions.
31 changes: 31 additions & 0 deletions crates/solidity/inputs/language/bindings/rules.msgb
Original file line number Diff line number Diff line change
Expand Up @@ -2050,6 +2050,36 @@ inherit .star_extension
edge @enum.def -> type
edge type -> type_enum_type
edge type_enum_type -> @enum.lexical_scope

; value_ref_typeof allows for an enum value to access the
; functions associated to this type
node @enum.value_ref_typeof
attr (@enum.value_ref_typeof) push_symbol = "@typeof"
node value_ref_type
attr (value_ref_type) push_symbol = (source-text @name)
edge @enum.value_ref_typeof -> value_ref_type
edge value_ref_type -> @enum.lexical_scope

; Path to link min and max to the enum's scope (#1158)
; It resolves paths of the form `type(<enum>).min.<something>`
if (version-matches ">= 0.6.8") {
node typeof
attr (typeof) pop_symbol = "@typeof"

node built_in_member
attr (built_in_member) pop_symbol = "."
node min_built_in
attr (min_built_in) pop_symbol = "min"
node max_built_in
attr (max_built_in) pop_symbol = "max"

edge type -> typeof
edge typeof -> built_in_member
edge built_in_member -> min_built_in
edge built_in_member -> max_built_in
edge min_built_in -> @enum.value_ref_typeof
edge max_built_in -> @enum.value_ref_typeof
}
}

@enum [EnumDefinition
Expand All @@ -2059,6 +2089,7 @@ inherit .star_extension
attr (def) node_definition = @item
attr (def) definiens_node = @item

edge def -> @enum.value_ref_typeof
edge @enum.members -> def
}

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.

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,14 @@
# This file is generated automatically by infrastructure scripts. Please don't edit by hand.

Parse errors:
Error: Expected ContractKeyword or ImportKeyword or InterfaceKeyword or LibraryKeyword or PragmaKeyword.
╭─[input.sol:1:1]
1 │ ╭─▶ enum Answer {
┆ ┆
18 │ ├─▶ }
│ │
│ ╰─────── Error occurred here.
────╯
References and definitions:
Definiens:
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# This file is generated automatically by infrastructure scripts. Please don't edit by hand.

References and definitions:
╭─[input.sol:1:1]
1 │ enum Answer {
│ ───┬──
│ ╰──── name: 1
2 │ Yes
│ ─┬─
│ ╰─── name: 2
5 │ library Id {
│ ─┬
│ ╰── name: 3
6 │ function id(Answer ans) returns (Answer) {
│ ─┬ ───┬── ─┬─ ───┬──
│ ╰────────────────────────────── name: 4
│ │ │ │
│ ╰───────────────────────── ref: 1
│ │ │
│ ╰──────────────────── name: 5
│ │
│ ╰──── ref: 1
7 │ return ans;
│ ─┬─
│ ╰─── ref: 5
11 │ contract Test {
│ ──┬─
│ ╰─── name: 6
12 │ using Id for Answer;
│ ─┬ ───┬──
│ ╰───────────── ref: 3
│ │
│ ╰──── ref: 1
14 │ function testFunc() {
│ ────┬───
│ ╰───── name: 7
15 │ type(Answer).min.id();
│ ───┬── ─┬─ ─┬
│ ╰──────────── ref: 1
│ │ │
│ ╰────── unresolved
│ │
│ ╰── unresolved
16 │ type(Answer).max.id();
│ ───┬── ─┬─ ─┬
│ ╰──────────── ref: 1
│ │ │
│ ╰────── unresolved
│ │
│ ╰── unresolved
────╯
Definiens:
╭─[input.sol:1:1]
1 │ ╭─▶ enum Answer {
2 │ │ Yes
│ │ ─┬─
│ │ ╰─── definiens: 2
3 │ │ ├─▶ }
│ │ │
│ │ ╰─────── definiens: 1
4 │ ╭─────▶
┆ ┆ ┆
6 │ │ ╭───▶ function id(Answer ans) returns (Answer) {
│ │ │ ─────┬────
│ │ │ ╰────── definiens: 5
┆ ┆ ┆
8 │ │ ├───▶ }
│ │ │
│ │ ╰─────────── definiens: 4
9 │ │ ├─────▶ }
│ │ │
│ │ ╰─────────── definiens: 3
10 │ ╭─────────▶
┆ ┆
13 │ │ ╭───────▶
┆ ┆ ┆
17 │ │ ├───────▶ }
│ │ │
│ │ ╰─────────────── definiens: 7
18 │ ├─────────▶ }
│ │
│ ╰─────────────── definiens: 6
────╯
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# This file is generated automatically by infrastructure scripts. Please don't edit by hand.

References and definitions:
╭─[input.sol:1:1]
1 │ enum Answer {
│ ───┬──
│ ╰──── name: 1
2 │ Yes
│ ─┬─
│ ╰─── name: 2
5 │ library Id {
│ ─┬
│ ╰── name: 3
6 │ function id(Answer ans) returns (Answer) {
│ ─┬ ───┬── ─┬─ ───┬──
│ ╰────────────────────────────── name: 4
│ │ │ │
│ ╰───────────────────────── ref: 1
│ │ │
│ ╰──────────────────── name: 5
│ │
│ ╰──── ref: 1
7 │ return ans;
│ ─┬─
│ ╰─── ref: 5
11 │ contract Test {
│ ──┬─
│ ╰─── name: 6
12 │ using Id for Answer;
│ ─┬ ───┬──
│ ╰───────────── ref: 3
│ │
│ ╰──── ref: 1
14 │ function testFunc() {
│ ────┬───
│ ╰───── name: 7
15 │ type(Answer).min.id();
│ ───┬── ─┬─ ─┬
│ ╰──────────── ref: 1
│ │ │
│ ╰────── ref: built-in
│ │
│ ╰── ref: 4
16 │ type(Answer).max.id();
│ ───┬── ─┬─ ─┬
│ ╰──────────── ref: 1
│ │ │
│ ╰────── ref: built-in
│ │
│ ╰── ref: 4
────╯
Definiens:
╭─[input.sol:1:1]
1 │ ╭─▶ enum Answer {
2 │ │ Yes
│ │ ─┬─
│ │ ╰─── definiens: 2
3 │ │ ├─▶ }
│ │ │
│ │ ╰─────── definiens: 1
4 │ ╭─────▶
┆ ┆ ┆
6 │ │ ╭───▶ function id(Answer ans) returns (Answer) {
│ │ │ ─────┬────
│ │ │ ╰────── definiens: 5
┆ ┆ ┆
8 │ │ ├───▶ }
│ │ │
│ │ ╰─────────── definiens: 4
9 │ │ ├─────▶ }
│ │ │
│ │ ╰─────────── definiens: 3
10 │ ╭─────────▶
┆ ┆
13 │ │ ╭───────▶
┆ ┆ ┆
17 │ │ ├───────▶ }
│ │ │
│ │ ╰─────────────── definiens: 7
18 │ ├─────────▶ }
│ │
│ ╰─────────────── definiens: 6
────╯
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
enum Answer {
Yes
}

library Id {
function id(Answer ans) returns (Answer) {
return ans;
}
}

contract Test {
using Id for Answer;

function testFunc() {
type(Answer).min.id();
type(Answer).max.id();
}
}
Loading

0 comments on commit 72c0c76

Please sign in to comment.