Skip to content

Commit

Permalink
Introduced queries for crate plugins in DefsGroup and SemanticGroup
Browse files Browse the repository at this point in the history
commit-id:5dc187d6
  • Loading branch information
integraledelebesgue committed Dec 9, 2024
1 parent 55db9be commit 36226f4
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 2 deletions.
64 changes: 62 additions & 2 deletions crates/cairo-lang-defs/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,49 @@ pub trait DefsGroup:
// Plugins.
// ========
#[salsa::input]
fn macro_plugins(&self) -> Vec<Arc<dyn MacroPlugin>>;
#[salsa::input]
fn macro_plugins(&self) -> Vec<Arc<dyn MacroPlugin>>; // 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<OrderedHashMap<String, Arc<dyn InlineMacroExprPlugin>>>;

#[salsa::input]
fn default_macro_plugins(&self) -> Vec<MacroPluginId>;

#[salsa::input]
fn override_crate_macro_plugins(&self, crate_id: CrateId) -> Vec<MacroPluginId>;

#[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<MacroPluginId>;

#[salsa::input]
fn default_inline_macro_plugins(&self) -> OrderedHashMap<String, InlineMacroExprPluginId>;

#[salsa::input]
fn override_crate_inline_macro_plugins(
&self,
crate_id: CrateId,
) -> OrderedHashMap<String, InlineMacroExprPluginId>;

#[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<String, InlineMacroExprPluginId>;

/// 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<OrderedHashSet<String>>;
Expand Down Expand Up @@ -261,6 +300,27 @@ pub trait DefsGroup:
) -> Maybe<Arc<PluginFileDiagnosticNotes>>;
}

fn crate_macro_plugins(db: &dyn DefsGroup, _crate_id: CrateId) -> Vec<MacroPluginId> {
// 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<String, InlineMacroExprPluginId> {
// 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<OrderedHashSet<String>> {
let base_attrs = [
INLINE_ATTR,
Expand Down
114 changes: 114 additions & 0 deletions crates/cairo-lang-defs/src/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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<dyn MacroPlugin>);

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<String> {
self.0.declared_attributes()
}

fn declared_derives(&self) -> Vec<String> {
self.0.declared_derives()
}

fn executable_attributes(&self) -> Vec<String> {
self.0.executable_attributes()
}

fn phantom_type_attributes(&self) -> Vec<String> {
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<dyn ...>`,
// 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<H: Hasher>(&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<dyn InlineMacroExprPlugin>);

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<String> {
self.0.documentation()
}
}

impl Clone for InlineMacroExprPluginLongId {
fn clone(&self) -> Self {
Self(Arc::clone(&self.0))
}
}

// `PartialEq` and `Hash` cannot be derived on `Arc<dyn ...>`,
// 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<H: Hasher>(&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.
Expand Down
21 changes: 21 additions & 0 deletions crates/cairo-lang-semantic/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -1559,6 +1560,21 @@ pub trait SemanticGroup:
#[salsa::input]
fn analyzer_plugins(&self) -> Vec<Arc<dyn AnalyzerPlugin>>;

#[salsa::input]
fn default_analyzer_plugins(&self) -> Vec<AnalyzerPluginId>;

#[salsa::input]
fn override_crate_analyzer_plugins(&self, crate_id: CrateId) -> Vec<AnalyzerPluginId>;

#[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<AnalyzerPluginId>;

/// 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<OrderedHashSet<String>>;
Expand Down Expand Up @@ -1708,6 +1724,11 @@ fn module_semantic_diagnostics(
Ok(diagnostics.build())
}

fn crate_analyzer_plugins(db: &dyn SemanticGroup, _crate_id: CrateId) -> Vec<AnalyzerPluginId> {
// TODO: Implement this properly
db.default_analyzer_plugins()
}

fn declared_allows(db: &dyn SemanticGroup) -> Arc<OrderedHashSet<String>> {
Arc::new(OrderedHashSet::from_iter(
db.analyzer_plugins().into_iter().flat_map(|plugin| plugin.declared_allows()),
Expand Down
55 changes: 55 additions & 0 deletions crates/cairo-lang-semantic/src/ids.rs
Original file line number Diff line number Diff line change
@@ -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<dyn AnalyzerPlugin>);

impl AnalyzerPlugin for AnalyzerPluginLongId {
fn diagnostics(
&self,
db: &dyn crate::db::SemanticGroup,
module_id: cairo_lang_defs::ids::ModuleId,
) -> Vec<cairo_lang_defs::plugin::PluginDiagnostic> {
self.0.diagnostics(db, module_id)
}

fn declared_allows(&self) -> Vec<String> {
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<dyn ...>`,
// 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<H: Hasher>(&self, state: &mut H) {
Arc::as_ptr(&self.0).hash(state)
}
}

define_short_id!(
AnalyzerPluginId,
AnalyzerPluginLongId,
SemanticGroup,
lookup_intern_analyzer_plugin,
intern_analyzer_plugin
);
1 change: 1 addition & 0 deletions crates/cairo-lang-semantic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 36226f4

Please sign in to comment.