Skip to content

Commit

Permalink
Move graph snapshots into the bindings module; fix lexical scoping in…
Browse files Browse the repository at this point in the history
… rules
  • Loading branch information
ggiraldez committed Jun 18, 2024
1 parent 6fff22f commit 1d0a99e
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 18 deletions.
4 changes: 4 additions & 0 deletions crates/codegen/runtime/generator/src/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ pub struct BindingsModel {
impl BindingsModel {
pub fn from_language(language: &model::Language) -> Result<Self> {
let binding_rules_source = language.binding_rules_file.read_to_string()?;
println!(
"cargo:rerun-if-changed={}",
language.binding_rules_file.to_string_lossy()
);

Ok(Self {
binding_rules_source,
Expand Down
5 changes: 1 addition & 4 deletions crates/solidity/inputs/language/bindings/rules.msgb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, i
...
] {
edge @function.lexical_scope -> @param.defs
edge @function.defs -> @param.defs
}

;; Connect the function to the contract/interface/library they belong to
Expand All @@ -162,7 +161,6 @@ attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, i
]
] {
edge @unit_member.lexical_scope -> @function.defs
edge @unit_member.defs -> @function.defs
edge @function.lexical_scope -> @unit_member.lexical_scope
}

Expand All @@ -174,7 +172,6 @@ attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, i
;; Connect the function body to the function definition
@function [FunctionDefinition ... @body body: [FunctionBody] ...] {
edge @body.lexical_scope -> @function.lexical_scope
edge @function.defs -> @body.defs
}


Expand All @@ -185,6 +182,7 @@ attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, i
@block [Block] {
node @block.lexical_scope
node @block.defs
edge @block.lexical_scope -> @block.defs
}

@stmt [Statement] {
Expand All @@ -199,7 +197,6 @@ attribute symbol_reference = symbol => type = "push_symbol", symbol = symbol, i

@body [FunctionBody @block variant: [Block]] {
edge @block.lexical_scope -> @body.lexical_scope
edge @body.defs -> @block.defs
}


Expand Down

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

1 change: 1 addition & 0 deletions crates/solidity/outputs/cargo/tests/src/bindings/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod runner;
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ use std::fs::{self, create_dir_all};
use anyhow::Result;
use infra_utils::cargo::CargoWorkspace;
use infra_utils::paths::FileWalker;
use semver::Version;
use slang_solidity::bindings::Bindings;
use slang_solidity::bindings::graph_builder::{
ExecutionConfig, Functions, Graph, NoCancellation, Variables,
};
use slang_solidity::bindings::Bindings;
use slang_solidity::language::Language;

#[test]
pub fn run_all() -> Result<()> {
let data_dir =
CargoWorkspace::locate_source_crate("solidity_testing_snapshots")?.join("graph_output");
CargoWorkspace::locate_source_crate("solidity_testing_snapshots")?.join("bindings");

for file in FileWalker::from_directory(data_dir).find(["*.sol"])? {
run(file.file_name().unwrap().to_str().unwrap())?;
Expand All @@ -25,28 +24,33 @@ pub fn run_all() -> Result<()> {

fn run(file_name: &str) -> Result<()> {
let data_dir =
CargoWorkspace::locate_source_crate("solidity_testing_snapshots")?.join("graph_output");
CargoWorkspace::locate_source_crate("solidity_testing_snapshots")?.join("bindings");
let input_path = data_dir.join(file_name);
let input = fs::read_to_string(input_path)?;

// TODO: de-hardcode this and parse with different versions?
let language = Language::new(Version::new(0, 8, 22))?;
let latest_version = Language::SUPPORTED_VERSIONS.last().unwrap();
let language = Language::new(latest_version.clone())?;

let parse_output = language.parse(Language::ROOT_KIND, &input);
assert!(parse_output.is_valid());

let graph_builder = Bindings::get_graph_builder()?;

let functions = Functions::stdlib();
let variables = Variables::new();
let execution_config = ExecutionConfig::new(&functions, &variables);
let execution_config = ExecutionConfig::new(&functions, &variables).debug_attributes(
"__location".into(),
"__variable".into(),
"__match".into(),
);

let tree = parse_output.create_tree_cursor();
let graph = graph_builder.execute(&tree, &execution_config, &NoCancellation)?;

let output_dir = data_dir.join("generated");
create_dir_all(&output_dir)?;

let output_path = output_dir.join(format!("{file_name}.graph"));
fs::write(output_path, format!("{}", graph.pretty_print()))?;

let output_path = output_dir.join(format!("{file_name}.mmd"));
fs::write(output_path, format!("{}", print_graph_as_mermaid(&graph)))?;

Expand All @@ -67,6 +71,15 @@ fn print_graph_as_mermaid(graph: &Graph) -> impl fmt::Display + '_ {
} else {
format!("{}", node.index())
};
let source = gn.attributes.get("__match").unwrap().as_syntax_node_ref().unwrap();
let location = gn.attributes.get("__location").unwrap();

let node_label = format!(
"\"`**{node_label}** @{source}\n{variable}\n{location}`\"",
source = source.location(),
variable = gn.attributes.get("__variable").unwrap(),
location = location,
);
let node_type = gn.attributes.get("type").and_then(|x| x.as_str().ok());
match node_type {
Some("push_symbol") => writeln!(f, "\tN{}[/{}\\]", node.index(), node_label)?,
Expand Down
2 changes: 1 addition & 1 deletion crates/solidity/outputs/cargo/tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![cfg(test)]

mod bindings;
mod cst_output;
mod doc_examples;
mod graph_output;
mod trivia;
210 changes: 210 additions & 0 deletions crates/solidity/testing/snapshots/bindings/generated/lexical.sol.mmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
graph TD
N0["`**0** @(1, 1)
@source_unit.lexical_scope
line 25 column 21`"]
N1["`**1** @(1, 1)
@source_unit.defs
line 26 column 21`"]
N1 --> N3
N2["`**2** @(1, 1)
@contract.lexical_scope
line 35 column 18`"]
N2 --> N0
N2 --> N4
N2 --> N6
N2 --> N8
N2 --> N34
N3["`**3** @(1, 1)
@contract.defs
line 36 column 18`"]
N3 --> N4
N3 --> N34
N4[\"`**Foo** @(1, 1)
def
line 40 column 8`"/]
N5["`**5** @(3, 1)
@function.lexical_scope
line 119 column 18`"]
N5 --> N2
N5 --> N9
N5 --> N12
N6["`**6** @(3, 1)
@function.defs
line 120 column 18`"]
N6 --> N9
N7["`**7** @(15, 1)
@function.lexical_scope
line 119 column 18`"]
N7 --> N2
N7 --> N10
N8["`**8** @(15, 1)
@function.defs
line 120 column 18`"]
N8 --> N10
N9[\"`**bar** @(3, 1)
def
line 124 column 8`"/]
N10[\"`**baz** @(15, 1)
def
line 124 column 8`"/]
N11["`**11** @(5, 18)
@param.lexical_scope
line 132 column 15`"]
N11 --> N17
N12["`**12** @(5, 18)
@param.defs
line 133 column 15`"]
N12 --> N17
N13["`**13** @(5, 35)
@param.lexical_scope
line 132 column 15`"]
N14["`**14** @(5, 35)
@param.defs
line 133 column 15`"]
N15["`**15** @(16, 29)
@param.lexical_scope
line 132 column 15`"]
N16["`**16** @(16, 29)
@param.defs
line 133 column 15`"]
N17[\"`**z** @(5, 18)
def
line 137 column 8`"/]
N18["`**18** @(5, 40)
@body.lexical_scope
line 169 column 14`"]
N18 --> N5
N19["`**19** @(5, 40)
@body.defs
line 170 column 14`"]
N20["`**20** @(16, 33)
@body.lexical_scope
line 169 column 14`"]
N20 --> N7
N21["`**21** @(16, 33)
@body.defs
line 170 column 14`"]
N22["`**22** @(5, 40)
@block.lexical_scope
line 184 column 15`"]
N22 --> N18
N22 --> N23
N23["`**23** @(5, 40)
@block.defs
line 185 column 15`"]
N23 --> N27
N23 --> N29
N24["`**24** @(16, 33)
@block.lexical_scope
line 184 column 15`"]
N24 --> N20
N24 --> N25
N25["`**25** @(16, 33)
@block.defs
line 185 column 15`"]
N25 --> N31
N26["`**26** @(6, 1)
@stmt.lexical_scope
line 190 column 14`"]
N26 --> N22
N26 --> N32
N27["`**27** @(6, 1)
@stmt.defs
line 191 column 14`"]
N27 --> N32
N28["`**28** @(8, 1)
@stmt.lexical_scope
line 190 column 14`"]
N28 --> N22
N29["`**29** @(8, 1)
@stmt.defs
line 191 column 14`"]
N30["`**30** @(17, 1)
@stmt.lexical_scope
line 190 column 14`"]
N30 --> N24
N31["`**31** @(17, 1)
@stmt.defs
line 191 column 14`"]
N32[\"`**x** @(6, 1)
def
line 209 column 8`"/]
N33["`**33** @(2, 1)
@state_var.lexical_scope
line 238 column 19`"]
N33 --> N2
N33 --> N35
N34["`**34** @(2, 1)
@state_var.defs
line 239 column 19`"]
N34 --> N35
N35[\"`**y** @(2, 1)
def
line 243 column 8`"/]
N36["`**36** @(7, 17)
@expr.lexical_scope
line 320 column 14`"]
N37["`**37** @(10, 15)
@expr.lexical_scope
line 320 column 14`"]
N37 --> N28
N38["`**38** @(10, 15)
@expr.lexical_scope
line 320 column 14`"]
N38 --> N37
N39["`**39** @(10, 15)
@expr.lexical_scope
line 320 column 14`"]
N39 --> N38
N40["`**40** @(10, 19)
@expr.lexical_scope
line 320 column 14`"]
N40 --> N38
N41["`**41** @(10, 23)
@expr.lexical_scope
line 320 column 14`"]
N41 --> N37
N42["`**42** @(17, 15)
@expr.lexical_scope
line 320 column 14`"]
N42 --> N30
N43["`**43** @(17, 15)
@expr.lexical_scope
line 320 column 14`"]
N43 --> N42
N44["`**44** @(17, 15)
@expr.lexical_scope
line 320 column 14`"]
N44 --> N43
N45["`**45** @(17, 19)
@expr.lexical_scope
line 320 column 14`"]
N45 --> N43
N46["`**46** @(17, 23)
@expr.lexical_scope
line 320 column 14`"]
N46 --> N42
N47[/"`**x** @(10, 15)
ref
line 324 column 8`"\]
N47 --> N39
N48[/"`**y** @(10, 19)
ref
line 324 column 8`"\]
N48 --> N40
N49[/"`**z** @(10, 23)
ref
line 324 column 8`"\]
N49 --> N41
N50[/"`**w** @(17, 15)
ref
line 324 column 8`"\]
N50 --> N44
N51[/"`**z** @(17, 19)
ref
line 324 column 8`"\]
N51 --> N45
N52[/"`**x** @(17, 23)
ref
line 324 column 8`"\]
N52 --> N46
Loading

0 comments on commit 1d0a99e

Please sign in to comment.