Skip to content

Commit

Permalink
Add and use a different binding rules set for system files (built-ins)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiraldez committed Nov 29, 2024
1 parent 1213db3 commit b347501
Show file tree
Hide file tree
Showing 14 changed files with 1,611 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#[allow(clippy::needless_raw_string_hashes)]
#[allow(dead_code)] // TODO(#982): use to create the graph
pub const BINDING_RULES_SOURCE: &str = r#####"
{{ model.bindings.binding_rules_source }}
pub const USER_BINDING_RULES_SOURCE: &str = r#####"
{{ model.bindings.user_binding_rules_source }}
"#####;

#[allow(clippy::needless_raw_string_hashes)]
pub const SYSTEM_BINDING_RULES_SOURCE: &str = r#####"
{{ model.bindings.system_binding_rules_source }}
"#####;

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

16 changes: 13 additions & 3 deletions crates/codegen/runtime/cargo/crate/src/runtime/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,22 @@ pub fn create_with_resolver(
version: Version,
resolver: Arc<dyn PathResolver + Sync + Send>,
) -> Bindings {
Bindings::create(version, binding_rules::BINDING_RULES_SOURCE, resolver)
Bindings::create(
version,
binding_rules::USER_BINDING_RULES_SOURCE,
binding_rules::SYSTEM_BINDING_RULES_SOURCE,
resolver,
)
}

#[cfg(feature = "__private_testing_utils")]
pub fn get_binding_rules() -> &'static str {
binding_rules::BINDING_RULES_SOURCE
pub fn get_user_binding_rules() -> &'static str {
binding_rules::USER_BINDING_RULES_SOURCE
}

#[cfg(feature = "__private_testing_utils")]
pub fn get_system_binding_rules() -> &'static str {
binding_rules::SYSTEM_BINDING_RULES_SOURCE
}

pub fn get_built_ins(version: &semver::Version) -> &'static str {
Expand Down
14 changes: 9 additions & 5 deletions crates/codegen/runtime/generator/src/bindings/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{collections::BTreeSet, path::Path};
use std::collections::BTreeSet;
use std::path::Path;

use anyhow::Result;
use codegen_language_definition::model;
Expand All @@ -8,28 +9,31 @@ use serde::Serialize;

#[derive(Default, Serialize)]
pub struct BindingsModel {
binding_rules_source: String,
user_binding_rules_source: String,
system_binding_rules_source: String,
built_ins_versions: BTreeSet<Version>,
file_extension: String,
}

impl BindingsModel {
pub fn from_language(language: &model::Language) -> Result<Self> {
let binding_rules_dir = &language.binding_rules_dir;
let binding_rules_source = build_rules(binding_rules_dir, "user-rules.parts")?;
let user_binding_rules_source = assemble_rules(binding_rules_dir, "user-rules.parts")?;
let system_binding_rules_source = assemble_rules(binding_rules_dir, "system-rules.parts")?;
let built_ins_versions = language.collect_built_ins_versions();
let file_extension = language.file_extension.clone().unwrap_or_default();

Ok(Self {
binding_rules_source,
user_binding_rules_source,
system_binding_rules_source,
built_ins_versions,
file_extension,
})
}
}

// Builds a rules file by concatenating the file parts listed in the given `parts_file`
fn build_rules(rules_dir: &Path, parts_file: &str) -> Result<String> {
fn assemble_rules(rules_dir: &Path, parts_file: &str) -> Result<String> {
// We use `CodegenFileSystem` here to ensure the rules are rebuilt if the rules file changes
let mut fs = CodegenFileSystem::new(rules_dir)?;
let parts_contents = fs.read_file(rules_dir.join(parts_file))?;
Expand Down
2 changes: 1 addition & 1 deletion crates/metaslang/bindings/generated/public_api.txt

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

54 changes: 43 additions & 11 deletions crates/metaslang/bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ pub(crate) struct ReferenceBindingInfo {
}

pub struct Bindings<KT: KindTypes + 'static> {
graph_builder_file: File<KT>,
user_graph_builder_file: File<KT>,
system_graph_builder_file: File<KT>,
functions: Functions<KT>,
stack_graph: StackGraph,
cursors: HashMap<GraphHandle, Cursor<KT>>,
Expand Down Expand Up @@ -119,16 +120,20 @@ pub trait PathResolver {
impl<KT: KindTypes + 'static> Bindings<KT> {
pub fn create(
version: Version,
binding_rules: &str,
user_binding_rules: &str,
system_binding_rules: &str,
path_resolver: Arc<dyn PathResolver + Sync + Send>,
) -> Self {
let graph_builder_file =
File::from_str(binding_rules).expect("Bindings stack graph builder parse error");
let user_graph_builder_file =
File::from_str(user_binding_rules).expect("Bindings stack graph builder parse error");
let system_graph_builder_file =
File::from_str(system_binding_rules).expect("Bindings stack graph builder parse error");
let stack_graph = StackGraph::new();
let functions = builder::default_functions(version, path_resolver);

Self {
graph_builder_file,
user_graph_builder_file,
system_graph_builder_file,
functions,
stack_graph,
cursors: HashMap::new(),
Expand All @@ -144,13 +149,13 @@ impl<KT: KindTypes + 'static> Bindings<KT> {
pub fn add_system_file(&mut self, file_path: &str, tree_cursor: Cursor<KT>) {
let file_kind = FileDescriptor::System(file_path.into());
let file = self.stack_graph.get_or_create_file(&file_kind.as_string());
_ = self.add_file_internal(file, tree_cursor);
_ = self.add_system_file_internal(file, tree_cursor);
}

pub fn add_user_file(&mut self, file_path: &str, tree_cursor: Cursor<KT>) {
let file_kind = FileDescriptor::User(file_path.into());
let file = self.stack_graph.get_or_create_file(&file_kind.as_string());
_ = self.add_file_internal(file, tree_cursor);
_ = self.add_user_file_internal(file, tree_cursor);
}

#[cfg(feature = "__private_testing_utils")]
Expand All @@ -161,22 +166,49 @@ impl<KT: KindTypes + 'static> Bindings<KT> {
) -> metaslang_graph_builder::graph::Graph<KT> {
let file_kind = FileDescriptor::User(file_path.into());
let file = self.stack_graph.get_or_create_file(&file_kind.as_string());
let result = self.add_file_internal(file, tree_cursor);
let result = self.add_user_file_internal(file, tree_cursor);
result.graph
}

fn add_file_internal(&mut self, file: FileHandle, tree_cursor: Cursor<KT>) -> BuildResult<KT> {
fn add_system_file_internal(
&mut self,
file: FileHandle,
tree_cursor: Cursor<KT>,
) -> BuildResult<KT> {
let builder = Builder::new(
&self.graph_builder_file,
&self.system_graph_builder_file,
&self.functions,
&mut self.stack_graph,
file,
tree_cursor,
);
let mut result = builder
let result = builder
.build(&builder::NoCancellation)
.expect("Internal error while building bindings");

self.add_graph_internal(result)
}

fn add_user_file_internal(
&mut self,
file: FileHandle,
tree_cursor: Cursor<KT>,
) -> BuildResult<KT> {
let builder = Builder::new(
&self.user_graph_builder_file,
&self.functions,
&mut self.stack_graph,
file,
tree_cursor,
);
let result = builder
.build(&builder::NoCancellation)
.expect("Internal error while building bindings");

self.add_graph_internal(result)
}

fn add_graph_internal(&mut self, mut result: BuildResult<KT>) -> BuildResult<KT> {
for (handle, cursor) in result.cursors.drain() {
let cursor_id = cursor.node().id();
if self.stack_graph[handle].is_definition() {
Expand Down
3 changes: 3 additions & 0 deletions crates/solidity/inputs/language/bindings/system-rules.parts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
00-top-level.msgb
01-definitions.msgb
03-types.msgb
3 changes: 2 additions & 1 deletion crates/solidity/outputs/cargo/crate/generated/public_api.txt

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

Loading

0 comments on commit b347501

Please sign in to comment.