Skip to content

Commit

Permalink
Implement C99 scoping rules for Solidity >= 0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiraldez committed Jun 24, 2024
1 parent ba05265 commit 46c95ff
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 69 deletions.
46 changes: 34 additions & 12 deletions crates/solidity/inputs/language/bindings/rules.msgb
Original file line number Diff line number Diff line change
Expand Up @@ -186,31 +186,53 @@ attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, i
@stmt [Statement] {
node @stmt.lexical_scope
node @stmt.defs
}

;; FIXME: For C99 scoping, we should link each statement's lexical_scope to the
;; previous lexical_scope, and to the statement's defs (to make the def
;; available to the statement itself).
;; Only the first statement in the block connects to the block's lexical_scope.
if (version-matches VERSION ">= 0.5.0") {
;; For Solidity >= 0.5.0, definitions are immediately available in the
;; statement scope. For < 0.5.0 this is also true, but resolved through the
;; enclosing block's lexical scope.
edge @stmt.lexical_scope -> @stmt.defs
attr (@stmt.lexical_scope -> @stmt.defs) precedence = 1
}
}

@block [Block ... [Statements ... @stmt [Statement]...] ...] {
edge @stmt.lexical_scope -> @block.lexical_scope
;; The first statement in a block
@block [Block ... [Statements @stmt [Statement] ...] ...] {
if (version-matches VERSION ">= 0.5.0") {
edge @stmt.lexical_scope -> @block.lexical_scope
}
}

;; FIXME: doesn't work for C99 scoping
edge @block.lexical_scope -> @stmt.defs
attr (@block.lexical_scope -> @stmt.defs) precedence = 1
;; Two consecutive statements
[Statements ... @left_stmt [Statement] @right_stmt [Statement] ...] {
if (version-matches VERSION ">= 0.5.0") {
edge @right_stmt.lexical_scope -> @left_stmt.lexical_scope
}
}

;; Hoist statement definitions (< 0.5.0)
@block [Block ... [Statements ... @stmt [Statement]...] ...] {
;; Hoist statement definitions for Solidity < 0.5.0
if (version-matches VERSION "< 0.5.0") {
;; definitions are carried over to the block
edge @block.defs -> @stmt.defs

;; resolution happens in the context of the block
edge @stmt.lexical_scope -> @block.lexical_scope

;; and the statement definitions are available block's scope
edge @block.lexical_scope -> @stmt.defs
attr (@block.lexical_scope -> @stmt.defs) precedence = 1
}
}

;; Statements of type block
@stmt [Statement @block variant: [Block]] {
edge @block.lexical_scope -> @stmt.lexical_scope

;; Hoist block definitions (< 0.5.0)
edge @stmt.defs -> @block.defs
if (version-matches VERSION "< 0.5.0") {
edge @stmt.defs -> @block.defs
}
}

;; Connect the function body's block lexical scope to the function
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
Expand Up @@ -19,8 +19,13 @@ contract Foo {
}
}

function another_func() returns (uint) {
return x;
// ^ref:1
function another_func() returns (uint y) {
// ^def:5
y = x + w;
// ^ref:!
// ^ref:1
// ^ref:5
uint w = 5;
// ^def:6
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,12 @@ contract Foo {
return z + 2;
// ^ref:!
}

function quux(int x) returns (int y) {
// ^def:2
// ^def:3
y = x + 5;
// ^ref:3
// ^ref:2
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,81 +65,81 @@ line 109 column 15`"]
@block.lexical_scope
line 183 column 15`"]
N16 --> N5
N16 --> N21
N16 --> N23
N17["`**17** @(2, 34)
@block.defs
line 184 column 15`"]
N18["`**18** @(10, 33)
@block.lexical_scope
line 183 column 15`"]
N18 --> N7
N18 --> N25
N19["`**19** @(10, 33)
@block.defs
line 184 column 15`"]
N20["`**20** @(3, 1)
@stmt.lexical_scope
line 188 column 14`"]
N20 --> N16
N20 --> N21
N21["`**21** @(3, 1)
@stmt.defs
line 189 column 14`"]
N21 --> N26
N22["`**22** @(4, 1)
@stmt.lexical_scope
line 188 column 14`"]
N22 --> N16
N22 --> N20
N22 --> N23
N23["`**23** @(4, 1)
@stmt.defs
line 189 column 14`"]
N24["`**24** @(11, 1)
@stmt.lexical_scope
line 188 column 14`"]
N24 --> N18
N24 --> N25
N25["`**25** @(11, 1)
@stmt.defs
line 189 column 14`"]
N26[\"`**x** @(3, 1)
def
line 228 column 8`"/]
line 250 column 8`"/]
N27["`**27** @(3, 17)
@expr.lexical_scope
line 316 column 14`"]
line 338 column 14`"]
N27 --> N20
N28["`**28** @(6, 15)
@expr.lexical_scope
line 316 column 14`"]
line 338 column 14`"]
N28 --> N22
N29["`**29** @(6, 15)
@expr.lexical_scope
line 316 column 14`"]
line 338 column 14`"]
N29 --> N28
N30["`**30** @(6, 19)
@expr.lexical_scope
line 316 column 14`"]
line 338 column 14`"]
N30 --> N28
N31["`**31** @(11, 15)
@expr.lexical_scope
line 316 column 14`"]
line 338 column 14`"]
N31 --> N24
N32["`**32** @(11, 15)
@expr.lexical_scope
line 316 column 14`"]
line 338 column 14`"]
N32 --> N31
N33["`**33** @(11, 19)
@expr.lexical_scope
line 316 column 14`"]
line 338 column 14`"]
N33 --> N31
N34[/"`**x** @(6, 15)
ref
line 320 column 8`"\]
line 342 column 8`"\]
N34 --> N29
N35[/"`**w** @(11, 15)
ref
line 320 column 8`"\]
line 342 column 8`"\]
N35 --> N32
N36[/"`**x** @(11, 19)
ref
line 320 column 8`"\]
line 342 column 8`"\]
N36 --> N33
Original file line number Diff line number Diff line change
Expand Up @@ -77,61 +77,61 @@ line 113 column 8`"/]
@block.lexical_scope
line 183 column 15`"]
N19 --> N5
N19 --> N24
N20["`**20** @(2, 40)
@block.defs
line 184 column 15`"]
N21["`**21** @(9, 33)
@block.lexical_scope
line 183 column 15`"]
N21 --> N7
N21 --> N26
N22["`**22** @(9, 33)
@block.defs
line 184 column 15`"]
N23["`**23** @(3, 1)
@stmt.lexical_scope
line 188 column 14`"]
N23 --> N19
N23 --> N24
N24["`**24** @(3, 1)
@stmt.defs
line 189 column 14`"]
N25["`**25** @(10, 1)
@stmt.lexical_scope
line 188 column 14`"]
N25 --> N21
N25 --> N26
N26["`**26** @(10, 1)
@stmt.defs
line 189 column 14`"]
N27["`**27** @(5, 15)
@expr.lexical_scope
line 316 column 14`"]
line 338 column 14`"]
N27 --> N23
N28["`**28** @(5, 15)
@expr.lexical_scope
line 316 column 14`"]
line 338 column 14`"]
N28 --> N27
N29["`**29** @(5, 19)
@expr.lexical_scope
line 316 column 14`"]
line 338 column 14`"]
N29 --> N27
N30["`**30** @(10, 15)
@expr.lexical_scope
line 316 column 14`"]
line 338 column 14`"]
N30 --> N25
N31["`**31** @(10, 15)
@expr.lexical_scope
line 316 column 14`"]
line 338 column 14`"]
N31 --> N30
N32["`**32** @(10, 19)
@expr.lexical_scope
line 316 column 14`"]
line 338 column 14`"]
N32 --> N30
N33[/"`**z** @(5, 15)
ref
line 320 column 8`"\]
line 342 column 8`"\]
N33 --> N28
N34[/"`**z** @(10, 15)
ref
line 320 column 8`"\]
line 342 column 8`"\]
N34 --> N31
Loading

0 comments on commit 46c95ff

Please sign in to comment.