Skip to content

Commit

Permalink
Binding rules for emit statements and basic event definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiraldez committed Aug 14, 2024
1 parent 4be812f commit de8b0e1
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 2 deletions.
47 changes: 47 additions & 0 deletions crates/solidity/inputs/language/bindings/rules.msgb
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,12 @@ attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, i
}
}

;; Emit statements

@stmt [Statement [EmitStatement @event_ident [IdentifierPath]]] {
edge @event_ident.left -> @stmt.lexical_scope
}


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; State Variables
Expand Down Expand Up @@ -847,6 +853,41 @@ attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, i
}


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Event definitions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

@event [EventDefinition @name name: [Identifier]] {
node def
attr (def) node_definition = @name
attr (def) definiens_node = @event

edge @event.def -> def
}

;; Make the enum available to the enclosing contract/interface/library.
;; NB. top-level enums (ie. those defined at the file's level) are already
;; covered above

@contract [ContractDefinition members: [ContractMembers
item: [ContractMember @event variant: [EventDefinition]]
]] {
edge @contract.type_members -> @event.def
}

@interface [InterfaceDefinition members: [InterfaceMembers
item: [ContractMember @event variant: [EventDefinition]]
]] {
edge @interface.type_members -> @event.def
}

@library [LibraryDefinition members: [LibraryMembers
item: [ContractMember @event variant: [EventDefinition]]
]] {
edge @library.members -> @event.def
}


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Expressions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down Expand Up @@ -982,6 +1023,7 @@ attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, i
attr (ref) node_reference = @name
;; TODO: bind the named argument to the parameters definition of the function
;; (is this possible given that function references are expressions?)
;; Ditto for events emissions (where it should be possible for sure)
}

@args [ArgumentsDeclaration [NamedArgumentsDeclaration
Expand All @@ -994,6 +1036,11 @@ attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, i
edge @args.lexical_scope -> @funcall.lexical_scope
}

;; Arguments to an event in an emit statement
@stmt [Statement [EmitStatement @args [ArgumentsDeclaration]]] {
edge @args.lexical_scope -> @stmt.lexical_scope
}


;;; Type expressions

Expand Down

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 @@ -368,8 +368,6 @@ fn check_reference_assertion(
version: &Version,
assertion: &ReferenceAssertion<'_>,
) -> Result<(), String> {
let resolution = find_and_resolve_reference(bindings, assertion)?;

let ReferenceAssertion {
id, version_req, ..
} = assertion;
Expand All @@ -380,6 +378,18 @@ fn check_reference_assertion(
true
};

let resolution = match find_and_resolve_reference(bindings, assertion) {
Ok(resolution) => resolution,
Err(err) => {
if version_matches {
return Err(err);
}
// the reference was not found, but that's ok if the assertion
// should not match for this version
None
}
};

match (version_matches, id) {
(true, None) => {
if let Some(resolved_handle) = resolution {
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,23 @@
contract Test {
event TestEvent(int id);
// ^def:1

function test_emit() public {
int x = 1;
// ^def:2

emit TestEvent(x);
// ^ref:2 (>= 0.4.21)
// ^ref:1 (>= 0.4.21)
emit Utils.Debug(x);
// ^ref:2 (>= 0.4.21)
// ^ref:4 (>= 0.4.21)
// ^ref:3 (>= 0.4.21)
}
}

library Utils {
// ^def:3
event Debug(int subject);
// ^def:4
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This file is generated automatically by infrastructure scripts. Please don't edit by hand.

Parse errors:
Error: Expected Equal or Semicolon.
╭─[input.sol:7:23]
7 │ emit TestEvent(x);
│ ─┬─
│ ╰─── Error occurred here.
───╯
References and definitions:
╭─[input.sol:1:1]
1 │ contract Test {
│ ──┬─
│ ╰─── def: 1
2 │ event TestEvent(int id);
│ ────┬────
│ ╰────── def: 2
4 │ function test_emit() public {
│ ────┬────
│ ╰────── def: 3
5 │ int x = 1;
│ ┬
│ ╰── def: 4
7 │ emit TestEvent(x);
│ ──┬─ ────┬────
│ ╰───────────── unresolved
│ │
│ ╰────── def: 5
───╯
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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 │ event TestEvent(int id);
│ ────┬────
│ ╰────── def: 2
4 │ function test_emit() public {
│ ────┬────
│ ╰────── def: 3
5 │ int x = 1;
│ ┬
│ ╰── def: 4
7 │ emit TestEvent(x);
│ ────┬──── ┬
│ ╰──────── ref: 2
│ │
│ ╰── ref: 4
───╯
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
contract Test {
event TestEvent(int id);

function test_emit() public {
int x = 1;

emit TestEvent(x);
}
}

0 comments on commit de8b0e1

Please sign in to comment.