Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): use provided metadata to support feature flags #2167

Merged
merged 2 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions crates/dojo-lang/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ use std::collections::HashMap;

use cairo_lang_defs::patcher::{PatchBuilder, RewriteNode};
use cairo_lang_defs::plugin::{
DynGeneratedFileAuxData, PluginDiagnostic, PluginGeneratedFile, PluginResult,
DynGeneratedFileAuxData, MacroPluginMetadata, PluginDiagnostic, PluginGeneratedFile,
PluginResult,
};
use cairo_lang_diagnostics::Severity;
use cairo_lang_plugins::plugins::HasItemsInCfgEx;
use cairo_lang_syntax::node::ast::{
ArgClause, Expr, MaybeModuleBody, OptionArgListParenthesized, OptionReturnTypeClause,
};
Expand Down Expand Up @@ -39,6 +41,7 @@ impl DojoContract {
db: &dyn SyntaxGroup,
module_ast: &ast::ItemModule,
package_id: String,
metadata: &MacroPluginMetadata<'_>,
) -> PluginResult {
let name = module_ast.name(db).text(db);

Expand Down Expand Up @@ -81,28 +84,26 @@ impl DojoContract {

if let MaybeModuleBody::Some(body) = module_ast.body(db) {
let mut body_nodes: Vec<_> = body
.items(db)
.elements(db)
.iter()
.iter_items_in_cfg(db, metadata.cfg_set)
.flat_map(|el| {
if let ast::ModuleItem::Enum(enum_ast) = el {
if let ast::ModuleItem::Enum(ref enum_ast) = el {
if enum_ast.name(db).text(db).to_string() == "Event" {
has_event = true;
return system.merge_event(db, enum_ast.clone());
}
} else if let ast::ModuleItem::Struct(struct_ast) = el {
} else if let ast::ModuleItem::Struct(ref struct_ast) = el {
if struct_ast.name(db).text(db).to_string() == "Storage" {
has_storage = true;
return system.merge_storage(db, struct_ast.clone());
}
} else if let ast::ModuleItem::Impl(impl_ast) = el {
} else if let ast::ModuleItem::Impl(ref impl_ast) = el {
// If an implementation is not targetting the ContractState,
// the auto injection of self and world is not applied.
let trait_path = impl_ast.trait_path(db).node.get_text(db);
if trait_path.contains("<ContractState>") {
return system.rewrite_impl(db, impl_ast.clone());
return system.rewrite_impl(db, impl_ast.clone(), metadata);
}
} else if let ast::ModuleItem::FreeFunction(fn_ast) = el {
} else if let ast::ModuleItem::FreeFunction(ref fn_ast) = el {
let fn_decl = fn_ast.declaration(db);
let fn_name = fn_decl.name(db).text(db);

Expand Down Expand Up @@ -553,7 +554,12 @@ impl DojoContract {
}

/// Rewrites all the functions of a Impl block.
fn rewrite_impl(&mut self, db: &dyn SyntaxGroup, impl_ast: ast::ItemImpl) -> Vec<RewriteNode> {
fn rewrite_impl(
&mut self,
db: &dyn SyntaxGroup,
impl_ast: ast::ItemImpl,
metadata: &MacroPluginMetadata<'_>,
) -> Vec<RewriteNode> {
let generate_attrs = impl_ast.attributes(db).query_attr(db, "generate_trait");
let has_generate_trait = !generate_attrs.is_empty();

Expand All @@ -570,11 +576,9 @@ impl DojoContract {
};

let body_nodes: Vec<_> = body
.items(db)
.elements(db)
.iter()
.iter_items_in_cfg(db, metadata.cfg_set)
.flat_map(|el| {
if let ast::ImplItem::Function(fn_ast) = el {
if let ast::ImplItem::Function(ref fn_ast) = el {
return self.rewrite_function(db, fn_ast.clone(), has_generate_trait);
}
vec![RewriteNode::Copied(el.as_syntax_node())]
Expand Down
17 changes: 11 additions & 6 deletions crates/dojo-lang/src/interface.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use cairo_lang_defs::patcher::{PatchBuilder, RewriteNode};
use cairo_lang_defs::plugin::{PluginDiagnostic, PluginGeneratedFile, PluginResult};
use cairo_lang_defs::plugin::{
MacroPluginMetadata, PluginDiagnostic, PluginGeneratedFile, PluginResult,
};
use cairo_lang_diagnostics::Severity;
use cairo_lang_plugins::plugins::HasItemsInCfgEx;
use cairo_lang_syntax::node::db::SyntaxGroup;
use cairo_lang_syntax::node::{ast, ids, Terminal, TypedStablePtr, TypedSyntaxNode};
use cairo_lang_utils::unordered_hash_map::UnorderedHashMap;
Expand All @@ -14,18 +17,20 @@ pub struct DojoInterface {
}

impl DojoInterface {
pub fn from_trait(db: &dyn SyntaxGroup, trait_ast: ast::ItemTrait) -> PluginResult {
pub fn from_trait(
db: &dyn SyntaxGroup,
trait_ast: ast::ItemTrait,
metadata: &MacroPluginMetadata<'_>,
) -> PluginResult {
let name = trait_ast.name(db).text(db);
let mut system = DojoInterface { diagnostics: vec![] };
let mut builder = PatchBuilder::new(db, &trait_ast);

if let ast::MaybeTraitBody::Some(body) = trait_ast.body(db) {
let body_nodes: Vec<_> = body
.items(db)
.elements(db)
.iter()
.iter_items_in_cfg(db, metadata.cfg_set)
.flat_map(|el| {
if let ast::TraitItem::Function(fn_ast) = el {
if let ast::TraitItem::Function(ref fn_ast) = el {
return system.rewrite_function(db, fn_ast.clone());
}

Expand Down
22 changes: 14 additions & 8 deletions crates/dojo-lang/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,23 @@ impl BuiltinDojoPlugin {
db: &dyn SyntaxGroup,
module_ast: ast::ItemModule,
package_id: String,
metadata: &MacroPluginMetadata<'_>,
) -> PluginResult {
if module_ast.has_attr(db, DOJO_CONTRACT_ATTR) {
return DojoContract::from_module(db, &module_ast, package_id);
return DojoContract::from_module(db, &module_ast, package_id, metadata);
}

PluginResult::default()
}

fn handle_trait(&self, db: &dyn SyntaxGroup, trait_ast: ast::ItemTrait) -> PluginResult {
fn handle_trait(
&self,
db: &dyn SyntaxGroup,
trait_ast: ast::ItemTrait,
metadata: &MacroPluginMetadata<'_>,
) -> PluginResult {
if trait_ast.has_attr(db, DOJO_INTERFACE_ATTR) {
return DojoInterface::from_trait(db, trait_ast);
return DojoInterface::from_trait(db, trait_ast, metadata);
}

PluginResult::default()
Expand Down Expand Up @@ -330,13 +336,11 @@ fn get_additional_derive_attrs_for_model(derive_attr_names: &[String]) -> Vec<St
}

impl MacroPlugin for BuiltinDojoPlugin {
// New metadata field: <https://github.com/starkware-libs/cairo/blob/60340c801125b25baaaddce64dd89c6c1524b59d/crates/cairo-lang-defs/src/plugin.rs#L81>
// Not used for now, but it contains a key-value BTreeSet. TBD what we can do with this.
fn generate_code(
&self,
db: &dyn SyntaxGroup,
item_ast: ast::ModuleItem,
_metadata: &MacroPluginMetadata<'_>,
metadata: &MacroPluginMetadata<'_>,
) -> PluginResult {
let package_id = match get_package_id(db) {
Option::Some(x) => x,
Expand All @@ -356,8 +360,10 @@ impl MacroPlugin for BuiltinDojoPlugin {
};

match item_ast {
ast::ModuleItem::Module(module_ast) => self.handle_mod(db, module_ast, package_id),
ast::ModuleItem::Trait(trait_ast) => self.handle_trait(db, trait_ast),
ast::ModuleItem::Module(module_ast) => {
self.handle_mod(db, module_ast, package_id, metadata)
}
ast::ModuleItem::Trait(trait_ast) => self.handle_trait(db, trait_ast, metadata),
ast::ModuleItem::Enum(enum_ast) => {
let aux_data = DojoAuxData::default();
let mut rewrite_nodes = vec![];
Expand Down
Loading