Skip to content

Commit

Permalink
Externalize built-ins file path from the graph building rules file
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiraldez committed Sep 20, 2024
1 parent c6275db commit 3757c60
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
10 changes: 8 additions & 2 deletions crates/metaslang/bindings/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ use stack_graphs::arena::Handle;
use stack_graphs::graph::{File, Node, NodeID, StackGraph};
use thiserror::Error;

use crate::{DefinitionBindingInfo, ReferenceBindingInfo, Tag};
use crate::{DefinitionBindingInfo, ReferenceBindingInfo, Tag, BUILT_INS_FILE_PATH};

// Node type values
static DROP_SCOPES_TYPE: &str = "drop_scopes";
Expand Down Expand Up @@ -347,6 +347,8 @@ static PRECEDENCE_ATTR: &str = "precedence";
pub const ROOT_NODE_VAR: &str = "ROOT_NODE";
/// Name of the variable used to pass the file path.
pub const FILE_PATH_VAR: &str = "FILE_PATH";
/// Name of the variable used to pass the built-ins file path.
pub const BUILT_INS_FILE_PATH_VAR: &str = "BUILT_INS_FILE_PATH";

pub(crate) struct Builder<'a, KT: KindTypes + 'static> {
msgb: &'a GraphBuilderFile<KT>,
Expand Down Expand Up @@ -398,7 +400,11 @@ impl<'a, KT: KindTypes + 'static> Builder<'a, KT> {
let file_path = self.stack_graph[self.file].name();
variables
.add(FILE_PATH_VAR.into(), file_path.into())
.expect("failed to add FILE_PATH variable");
.expect("Failed to add FILE_PATH variable");

variables
.add(BUILT_INS_FILE_PATH_VAR.into(), BUILT_INS_FILE_PATH.into())
.expect("Failed to add BUILT_INS_FILE_PATH variable");

let root_node = self.inject_node(NodeID::root());
variables
Expand Down
7 changes: 4 additions & 3 deletions crates/solidity/inputs/language/bindings/rules.msgb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
global ROOT_NODE
global FILE_PATH
global BUILT_INS_FILE_PATH

attribute node_definition = node => type = "pop_symbol", node_symbol = node, is_definition
attribute node_reference = node => type = "push_symbol", node_symbol = node, is_reference
Expand Down Expand Up @@ -31,14 +32,14 @@ inherit .enclosing_def
edge export -> @source_unit.defs

;; Automatically import language built-ins
if (not (eq FILE_PATH "@@built-ins@@")) {
if (not (eq FILE_PATH BUILT_INS_FILE_PATH)) {
;; All built-in symbols are defined in an internal contract named '$$'
;; so we need to construct an equivalent import path to reach them.
;; We should have access to both type members (eg. defined enums & structs)
;; as well as functions and state variables (see special case below), hence
;; why we're introducing a path through `@typeof`.
node built_ins
attr (built_ins) push_symbol = "@@built-ins@@"
attr (built_ins) push_symbol = BUILT_INS_FILE_PATH

node built_in_library
attr (built_in_library) push_symbol = "$$"
Expand Down Expand Up @@ -380,7 +381,7 @@ inherit .enclosing_def

;; Special case: for the built-ins file, export state variables in the
;; contract's members so they can be seen as globals
if (eq FILE_PATH "@@built-ins@@") {
if (eq FILE_PATH BUILT_INS_FILE_PATH) {
edge @contract.members -> @member.def
}
}
Expand Down

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

0 comments on commit 3757c60

Please sign in to comment.