From 36226f4e5e6d23322224694a11cc51cd05fab85a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sm=C3=B3=C5=82ka?= Date: Thu, 21 Nov 2024 17:08:32 +0100 Subject: [PATCH] Introduced queries for crate plugins in `DefsGroup` and `SemanticGroup` commit-id:5dc187d6 --- crates/cairo-lang-defs/src/db.rs | 64 ++++++++++++++- crates/cairo-lang-defs/src/ids.rs | 114 ++++++++++++++++++++++++++ crates/cairo-lang-semantic/src/db.rs | 21 +++++ crates/cairo-lang-semantic/src/ids.rs | 55 +++++++++++++ crates/cairo-lang-semantic/src/lib.rs | 1 + 5 files changed, 253 insertions(+), 2 deletions(-) create mode 100644 crates/cairo-lang-semantic/src/ids.rs diff --git a/crates/cairo-lang-defs/src/db.rs b/crates/cairo-lang-defs/src/db.rs index f95ff94fc40..7b9391786fd 100644 --- a/crates/cairo-lang-defs/src/db.rs +++ b/crates/cairo-lang-defs/src/db.rs @@ -96,10 +96,49 @@ pub trait DefsGroup: // Plugins. // ======== #[salsa::input] - fn macro_plugins(&self) -> Vec>; - #[salsa::input] + fn macro_plugins(&self) -> Vec>; // TODO: Delete in favour or [`default_macro_plugins`] + #[salsa::input] // TODO: Delete in favour or [`default_inline_macro_plugins`] fn inline_macro_plugins(&self) -> Arc>>; + #[salsa::input] + fn default_macro_plugins(&self) -> Vec; + + #[salsa::input] + fn override_crate_macro_plugins(&self, crate_id: CrateId) -> Vec; + + #[salsa::interned] + fn intern_macro_plugin(&self, plugin: MacroPluginLongId) -> MacroPluginId; + + /// Returns [`MacroPluginsId`]s of the plugins set for the crate with [`CrateId`]. + /// Provides an override if it has been set with + /// [`DefsGroup::set_override_crate_macro_plugins`] or the default + /// ([`DefsGroup::macro_plugins`]) otherwise. + fn crate_macro_plugins(&self, crate_id: CrateId) -> Vec; + + #[salsa::input] + fn default_inline_macro_plugins(&self) -> OrderedHashMap; + + #[salsa::input] + fn override_crate_inline_macro_plugins( + &self, + crate_id: CrateId, + ) -> OrderedHashMap; + + #[salsa::interned] + fn intern_inline_macro_plugin( + &self, + plugin: InlineMacroExprPluginLongId, + ) -> InlineMacroExprPluginId; + + /// Returns [`InlineMacroExprPluginsId`]s of the plugins set for the crate with [`CrateId`]. + /// Provides an override if it has been set with + /// [`DefsGroup::set_override_crate_inline_macro_plugins`] or the default + /// ([`DefsGroup::inline_macro_plugins`]) otherwise. + fn crate_inline_macro_plugins( + &self, + crate_id: CrateId, + ) -> OrderedHashMap; + /// Returns the set of attributes allowed anywhere. /// An attribute on any item that is not in this set will be handled as an unknown attribute. fn allowed_attributes(&self) -> Arc>; @@ -261,6 +300,27 @@ pub trait DefsGroup: ) -> Maybe>; } +fn crate_macro_plugins(db: &dyn DefsGroup, _crate_id: CrateId) -> Vec { + // Cannot do this at the moment + // catch_unwind(|| db.override_crate_macro_plugins(crate_id)) + // .unwrap_or_else(|_| db.default_macro_plugins()) + + // TODO: Change this + db.default_macro_plugins() +} + +fn crate_inline_macro_plugins( + db: &dyn DefsGroup, + _crate_id: CrateId, +) -> OrderedHashMap { + // Cannot do this at the moment + // catch_unwind(|| db.override_crate_inline_macro_plugins(crate_id)) + // .unwrap_or_else(|_| db.default_inline_macro_plugins()) + + // TODO: Change this + db.default_inline_macro_plugins() +} + fn allowed_attributes(db: &dyn DefsGroup) -> Arc> { let base_attrs = [ INLINE_ATTR, diff --git a/crates/cairo-lang-defs/src/ids.rs b/crates/cairo-lang-defs/src/ids.rs index b98af99c767..3f009e477c8 100644 --- a/crates/cairo-lang-defs/src/ids.rs +++ b/crates/cairo-lang-defs/src/ids.rs @@ -21,6 +21,9 @@ // // Call sites, variable usages, assignments, etc. are NOT definitions. +use std::hash::{Hash, Hasher}; +use std::sync::Arc; + use cairo_lang_debug::debug::DebugWithDb; use cairo_lang_diagnostics::Maybe; pub use cairo_lang_filesystem::ids::UnstableSalsaId; @@ -37,6 +40,7 @@ use smol_str::SmolStr; use crate::db::DefsGroup; use crate::diagnostic_utils::StableLocation; +use crate::plugin::{InlineMacroExprPlugin, MacroPlugin}; // A trait for an id for a language element. pub trait LanguageElementId { @@ -321,6 +325,116 @@ define_short_id!( intern_plugin_generated_file ); +/// An ID allowing for interning the [`MacroPlugin`] into Salsa database. +#[derive(Debug)] +pub struct MacroPluginLongId(pub Arc); + +impl MacroPlugin for MacroPluginLongId { + fn generate_code( + &self, + db: &dyn SyntaxGroup, + item_ast: ast::ModuleItem, + metadata: &crate::plugin::MacroPluginMetadata<'_>, + ) -> crate::plugin::PluginResult { + self.0.generate_code(db, item_ast, metadata) + } + + fn declared_attributes(&self) -> Vec { + self.0.declared_attributes() + } + + fn declared_derives(&self) -> Vec { + self.0.declared_derives() + } + + fn executable_attributes(&self) -> Vec { + self.0.executable_attributes() + } + + fn phantom_type_attributes(&self) -> Vec { + self.0.phantom_type_attributes() + } +} + +impl Clone for MacroPluginLongId { + fn clone(&self) -> Self { + Self(Arc::clone(&self.0)) + } +} + +// `PartialEq` and `Hash` cannot be derived on `Arc`, +// but pointer-based equality and hash semantics are enough in this case. +impl PartialEq for MacroPluginLongId { + fn eq(&self, other: &Self) -> bool { + Arc::ptr_eq(&self.0, &other.0) + } +} + +impl Eq for MacroPluginLongId {} + +impl Hash for MacroPluginLongId { + fn hash(&self, state: &mut H) { + Arc::as_ptr(&self.0).hash(state) + } +} + +define_short_id!( + MacroPluginId, + MacroPluginLongId, + DefsGroup, + lookup_intern_macro_plugin, + intern_macro_plugin +); + +/// An ID allowing for interning the [`InlineMacroExprPlugin`] into Salsa database. +#[derive(Debug)] +pub struct InlineMacroExprPluginLongId(pub Arc); + +impl InlineMacroExprPlugin for InlineMacroExprPluginLongId { + fn generate_code( + &self, + db: &dyn SyntaxGroup, + item_ast: &ast::ExprInlineMacro, + metadata: &crate::plugin::MacroPluginMetadata<'_>, + ) -> crate::plugin::InlinePluginResult { + self.0.generate_code(db, item_ast, metadata) + } + + fn documentation(&self) -> Option { + self.0.documentation() + } +} + +impl Clone for InlineMacroExprPluginLongId { + fn clone(&self) -> Self { + Self(Arc::clone(&self.0)) + } +} + +// `PartialEq` and `Hash` cannot be derived on `Arc`, +// but pointer-based equality and hash semantics are enough in this case. +impl PartialEq for InlineMacroExprPluginLongId { + fn eq(&self, other: &Self) -> bool { + Arc::ptr_eq(&self.0, &other.0) + } +} + +impl Eq for InlineMacroExprPluginLongId {} + +impl Hash for InlineMacroExprPluginLongId { + fn hash(&self, state: &mut H) { + Arc::as_ptr(&self.0).hash(state) + } +} + +define_short_id!( + InlineMacroExprPluginId, + InlineMacroExprPluginLongId, + DefsGroup, + lookup_intern_inline_macro_plugin, + intern_inline_macro_plugin +); + define_language_element_id_as_enum! { #[toplevel] /// Id for direct children of a module. diff --git a/crates/cairo-lang-semantic/src/db.rs b/crates/cairo-lang-semantic/src/db.rs index ba26a75dcfe..95b405c5687 100644 --- a/crates/cairo-lang-semantic/src/db.rs +++ b/crates/cairo-lang-semantic/src/db.rs @@ -23,6 +23,7 @@ use smol_str::SmolStr; use crate::diagnostic::SemanticDiagnosticKind; use crate::expr::inference::{self, ImplVar, ImplVarId}; +use crate::ids::{AnalyzerPluginId, AnalyzerPluginLongId}; use crate::items::constant::{ConstValueId, Constant, ImplConstantId}; use crate::items::function_with_body::FunctionBody; use crate::items::functions::{ImplicitPrecedence, InlineConfiguration}; @@ -1559,6 +1560,21 @@ pub trait SemanticGroup: #[salsa::input] fn analyzer_plugins(&self) -> Vec>; + #[salsa::input] + fn default_analyzer_plugins(&self) -> Vec; + + #[salsa::input] + fn override_crate_analyzer_plugins(&self, crate_id: CrateId) -> Vec; + + #[salsa::interned] + fn intern_analyzer_plugin(&self, plugin: AnalyzerPluginLongId) -> AnalyzerPluginId; + + /// Returns [`AnalyzerPluginsId`]s of the plugins set for the crate with [`CrateId`]. + /// Returns + /// [`SemanticGroup::override_crate_analyzer_plugins`] if it has been set, + /// or the ([`SemanticGroup::default_analyzer_plugins`]) otherwise. + fn crate_analyzer_plugins(&self, crate_id: CrateId) -> Vec; + /// Returns the set of `allow` that were declared as by a plugin. /// An allow that is not in this set will be handled as an unknown allow. fn declared_allows(&self) -> Arc>; @@ -1708,6 +1724,11 @@ fn module_semantic_diagnostics( Ok(diagnostics.build()) } +fn crate_analyzer_plugins(db: &dyn SemanticGroup, _crate_id: CrateId) -> Vec { + // TODO: Implement this properly + db.default_analyzer_plugins() +} + fn declared_allows(db: &dyn SemanticGroup) -> Arc> { Arc::new(OrderedHashSet::from_iter( db.analyzer_plugins().into_iter().flat_map(|plugin| plugin.declared_allows()), diff --git a/crates/cairo-lang-semantic/src/ids.rs b/crates/cairo-lang-semantic/src/ids.rs new file mode 100644 index 00000000000..d85472438a5 --- /dev/null +++ b/crates/cairo-lang-semantic/src/ids.rs @@ -0,0 +1,55 @@ +use std::hash::{Hash, Hasher}; +use std::sync::Arc; + +use cairo_lang_utils::define_short_id; + +use crate::db::SemanticGroup; +use crate::plugin::AnalyzerPlugin; + +/// An Id allowing interning [`AnalyzerPlugin`] into Salsa database. +#[derive(Debug)] +pub struct AnalyzerPluginLongId(pub Arc); + +impl AnalyzerPlugin for AnalyzerPluginLongId { + fn diagnostics( + &self, + db: &dyn crate::db::SemanticGroup, + module_id: cairo_lang_defs::ids::ModuleId, + ) -> Vec { + self.0.diagnostics(db, module_id) + } + + fn declared_allows(&self) -> Vec { + self.0.declared_allows() + } +} + +impl Clone for AnalyzerPluginLongId { + fn clone(&self) -> Self { + Self(Arc::clone(&self.0)) + } +} + +// `PartialEq` and `Hash` cannot be derived on `Arc`, +// but pointer-based equality and hash semantics are enough in this case. +impl PartialEq for AnalyzerPluginLongId { + fn eq(&self, other: &Self) -> bool { + Arc::ptr_eq(&self.0, &other.0) + } +} + +impl Eq for AnalyzerPluginLongId {} + +impl Hash for AnalyzerPluginLongId { + fn hash(&self, state: &mut H) { + Arc::as_ptr(&self.0).hash(state) + } +} + +define_short_id!( + AnalyzerPluginId, + AnalyzerPluginLongId, + SemanticGroup, + lookup_intern_analyzer_plugin, + intern_analyzer_plugin +); diff --git a/crates/cairo-lang-semantic/src/lib.rs b/crates/cairo-lang-semantic/src/lib.rs index 55bf65d53b8..b03cb9561aa 100644 --- a/crates/cairo-lang-semantic/src/lib.rs +++ b/crates/cairo-lang-semantic/src/lib.rs @@ -6,6 +6,7 @@ pub mod corelib; pub mod db; pub mod diagnostic; pub mod expr; +pub mod ids; pub mod inline_macros; pub mod items; pub mod literals;