Skip to content

Commit

Permalink
Introduced per-crate plugin queries to the compiler
Browse files Browse the repository at this point in the history
commit-id:f0989263
  • Loading branch information
integraledelebesgue committed Dec 11, 2024
1 parent 98416ed commit 39e8dc8
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 117 deletions.
52 changes: 31 additions & 21 deletions crates/cairo-lang-defs/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ use itertools::{Itertools, chain};
use salsa::InternKey;

use crate::ids::*;
use crate::plugin::{
DynGeneratedFileAuxData, InlineMacroExprPlugin, MacroPlugin, MacroPluginMetadata,
PluginDiagnostic,
};
use crate::plugin::{DynGeneratedFileAuxData, MacroPlugin, MacroPluginMetadata, PluginDiagnostic};

/// Salsa database interface.
/// See [`super::ids`] for further details.
Expand Down Expand Up @@ -95,10 +92,6 @@ pub trait DefsGroup:

// Plugins.
// ========
#[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) -> Arc<[MacroPluginId]>;
Expand Down Expand Up @@ -141,19 +134,19 @@ pub trait DefsGroup:

/// 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>>;
fn allowed_attributes(&self, crate_id: CrateId) -> Arc<OrderedHashSet<String>>;

/// Returns the set of attributes allowed on statements.
/// An attribute on a statement that is not in this set will be handled as an unknown attribute.
fn allowed_statement_attributes(&self) -> Arc<OrderedHashSet<String>>;

/// Returns the set of `derive` that were declared as by a plugin.
/// A derive that is not in this set will be handled as an unknown derive.
fn declared_derives(&self) -> Arc<OrderedHashSet<String>>;
fn declared_derives(&self, crate_id: CrateId) -> Arc<OrderedHashSet<String>>;

/// Returns the set of attributes that were declared as phantom type attributes by a plugin,
/// i.e. a type marked with this attribute is considered a phantom type.
fn declared_phantom_type_attributes(&self) -> Arc<OrderedHashSet<String>>;
fn declared_phantom_type_attributes(&self, crate_id: CrateId) -> Arc<OrderedHashSet<String>>;

/// Checks whether the submodule is defined as inline.
fn is_submodule_inline(&self, submodule_id: SubmoduleId) -> Maybe<bool>;
Expand Down Expand Up @@ -321,7 +314,7 @@ fn crate_inline_macro_plugins(
db.default_inline_macro_plugins()
}

fn allowed_attributes(db: &dyn DefsGroup) -> Arc<OrderedHashSet<String>> {
fn allowed_attributes(db: &dyn DefsGroup, crate_id: CrateId) -> Arc<OrderedHashSet<String>> {
let base_attrs = [
INLINE_ATTR,
MUST_USE_ATTR,
Expand All @@ -336,9 +329,14 @@ fn allowed_attributes(db: &dyn DefsGroup) -> Arc<OrderedHashSet<String>> {
// TODO(orizi): Remove this once `starknet` is removed from corelib.
STARKNET_INTERFACE_ATTR,
];

let crate_plugins = db.crate_macro_plugins(crate_id);

Arc::new(OrderedHashSet::from_iter(chain!(
base_attrs.map(|attr| attr.into()),
db.macro_plugins().into_iter().flat_map(|plugin| plugin.declared_attributes())
crate_plugins
.iter()
.flat_map(|plugin| db.lookup_intern_macro_plugin(*plugin).declared_attributes())
)))
}

Expand All @@ -347,16 +345,25 @@ fn allowed_statement_attributes(_db: &dyn DefsGroup) -> Arc<OrderedHashSet<Strin
Arc::new(OrderedHashSet::from_iter(all_attributes.map(|attr| attr.into())))
}

fn declared_derives(db: &dyn DefsGroup) -> Arc<OrderedHashSet<String>> {
fn declared_derives(db: &dyn DefsGroup, crate_id: CrateId) -> Arc<OrderedHashSet<String>> {
Arc::new(OrderedHashSet::from_iter(
db.macro_plugins().into_iter().flat_map(|plugin| plugin.declared_derives()),
db.crate_macro_plugins(crate_id)
.iter()
.flat_map(|plugin| db.lookup_intern_macro_plugin(*plugin).declared_derives()),
))
}

fn declared_phantom_type_attributes(db: &dyn DefsGroup) -> Arc<OrderedHashSet<String>> {
fn declared_phantom_type_attributes(
db: &dyn DefsGroup,
crate_id: CrateId,
) -> Arc<OrderedHashSet<String>> {
let crate_plugins = db.crate_macro_plugins(crate_id);

Arc::new(OrderedHashSet::from_iter(chain!(
[PHANTOM_ATTR.into()],
db.macro_plugins().into_iter().flat_map(|plugin| plugin.phantom_type_attributes())
crate_plugins
.iter()
.flat_map(|plugin| db.lookup_intern_macro_plugin(*plugin).phantom_type_attributes())
)))
}

Expand Down Expand Up @@ -706,11 +713,12 @@ fn priv_module_sub_files(
}
.unwrap_or_else(|| file_syntax.items(syntax_db));

let allowed_attributes = db.allowed_attributes();
let crate_id = module_id.owning_crate(db);

let allowed_attributes = db.allowed_attributes(crate_id);
// TODO(orizi): Actually extract the allowed features per module.
let allowed_features = Default::default();

let crate_id = module_id.owning_crate(db);
let cfg_set = db
.crate_config(crate_id)
.and_then(|cfg| cfg.settings.cfg_set.map(Arc::new))
Expand All @@ -721,7 +729,7 @@ fn priv_module_sub_files(
.unwrap_or_default();
let metadata = MacroPluginMetadata {
cfg_set: &cfg_set,
declared_derives: &db.declared_derives(),
declared_derives: &db.declared_derives(crate_id),
allowed_features: &allowed_features,
edition,
};
Expand All @@ -736,7 +744,9 @@ fn priv_module_sub_files(
// Iterate the plugins by their order. The first one to change something (either
// generate new code, remove the original code, or both), breaks the loop. If more
// plugins might have act on the item, they can do it on the generated code.
for plugin in db.macro_plugins() {
for plugin_id in db.crate_macro_plugins(crate_id).iter() {
let plugin = db.lookup_intern_macro_plugin(*plugin_id);

let result = plugin.generate_code(db.upcast(), item_ast.clone(), &metadata);
plugin_diagnostics.extend(result.diagnostics);
if result.remove_original_item {
Expand Down
14 changes: 7 additions & 7 deletions crates/cairo-lang-defs/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use indoc::indoc;

use crate::db::{DefsDatabase, DefsGroup, try_ext_as_virtual_impl};
use crate::ids::{
FileIndex, GenericParamLongId, ModuleFileId, ModuleId, ModuleItemId, NamedLanguageElementId,
SubmoduleLongId,
FileIndex, GenericParamLongId, MacroPluginLongId, ModuleFileId, ModuleId, ModuleItemId,
NamedLanguageElementId, SubmoduleLongId,
};
use crate::plugin::{
MacroPlugin, MacroPluginMetadata, PluginDiagnostic, PluginGeneratedFile, PluginResult,
Expand All @@ -40,11 +40,11 @@ impl Default for DatabaseForTesting {
fn default() -> Self {
let mut res = Self { storage: Default::default() };
init_files_group(&mut res);
res.set_macro_plugins(vec![
Arc::new(FooToBarPlugin),
Arc::new(RemoveOrigPlugin),
Arc::new(DummyPlugin),
]);
res.set_default_macro_plugins(Arc::new([
res.intern_macro_plugin(MacroPluginLongId(Arc::new(FooToBarPlugin))),
res.intern_macro_plugin(MacroPluginLongId(Arc::new(RemoveOrigPlugin))),
res.intern_macro_plugin(MacroPluginLongId(Arc::new(DummyPlugin))),
]));
res
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/cairo-lang-doc/src/tests/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use cairo_lang_filesystem::db::{
use cairo_lang_filesystem::detect::detect_corelib;
use cairo_lang_filesystem::ids::{CrateId, Directory, FileLongId};
use cairo_lang_parser::db::{ParserDatabase, ParserGroup};
use cairo_lang_semantic::db::{SemanticDatabase, SemanticGroup};
use cairo_lang_semantic::db::{PluginSuiteInput, SemanticDatabase, SemanticGroup};
use cairo_lang_semantic::plugin::PluginSuite;
use cairo_lang_syntax::node::db::{SyntaxDatabase, SyntaxGroup};
use cairo_lang_utils::{Intern, Upcast};

Expand All @@ -33,7 +34,7 @@ impl Default for TestDatabase {
fn default() -> Self {
let mut res = Self { storage: Default::default() };
init_files_group(&mut res);
res.set_macro_plugins(vec![]);
res.set_default_plugins_from_suite(PluginSuite::default());
res
}
}
Expand Down
8 changes: 2 additions & 6 deletions crates/cairo-lang-lowering/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use cairo_lang_filesystem::db::{
use cairo_lang_filesystem::detect::detect_corelib;
use cairo_lang_filesystem::ids::VirtualFile;
use cairo_lang_parser::db::{ParserDatabase, ParserGroup};
use cairo_lang_semantic::db::{SemanticDatabase, SemanticGroup};
use cairo_lang_semantic::db::{PluginSuiteInput, SemanticDatabase, SemanticGroup};
use cairo_lang_semantic::inline_macros::get_default_plugin_suite;
use cairo_lang_syntax::node::db::{SyntaxDatabase, SyntaxGroup};
use cairo_lang_utils::Upcast;
Expand Down Expand Up @@ -41,11 +41,7 @@ impl LoweringDatabaseForTesting {
pub fn new() -> Self {
let mut res = LoweringDatabaseForTesting { storage: Default::default() };
init_files_group(&mut res);
let suite = get_default_plugin_suite();
res.set_macro_plugins(suite.plugins);
res.set_inline_macro_plugins(suite.inline_macro_plugins.into());
res.set_analyzer_plugins(suite.analyzer_plugins);

res.set_default_plugins_from_suite(get_default_plugin_suite());
let corelib_path = detect_corelib().expect("Corelib not found in default location.");
init_dev_corelib(&mut res, corelib_path);
init_lowering_group(&mut res, InliningStrategy::Default);
Expand Down
22 changes: 17 additions & 5 deletions crates/cairo-lang-plugins/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::default::Default;
use std::sync::Arc;

use cairo_lang_defs::db::{DefsDatabase, DefsGroup, try_ext_as_virtual_impl};
use cairo_lang_defs::ids::ModuleId;
use cairo_lang_defs::ids::{MacroPluginLongId, ModuleId};
use cairo_lang_defs::plugin::{
MacroPlugin, MacroPluginMetadata, PluginDiagnostic, PluginGeneratedFile, PluginResult,
};
Expand All @@ -23,6 +23,7 @@ use cairo_lang_test_utils::parse_test_file::TestRunnerResult;
use cairo_lang_test_utils::verify_diagnostics_expectation;
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use cairo_lang_utils::{Intern, Upcast};
use itertools::chain;

use crate::get_base_plugins;
use crate::test_utils::expand_module_text;
Expand Down Expand Up @@ -64,7 +65,12 @@ impl Default for DatabaseForTesting {
fn default() -> Self {
let mut res = Self { storage: Default::default() };
init_files_group(&mut res);
res.set_macro_plugins(get_base_plugins());
res.set_default_macro_plugins(
get_base_plugins()
.into_iter()
.map(|plugin| res.intern_macro_plugin(MacroPluginLongId(plugin)))
.collect(),
);
res
}
}
Expand Down Expand Up @@ -112,9 +118,15 @@ pub fn test_expand_plugin_inner(
extra_plugins: &[Arc<dyn MacroPlugin>],
) -> TestRunnerResult {
let db = &mut DatabaseForTesting::default();
let mut plugins = db.macro_plugins();
plugins.extend_from_slice(extra_plugins);
db.set_macro_plugins(plugins);

let extra_plugins = extra_plugins
.iter()
.cloned()
.map(|plugin| db.intern_macro_plugin(MacroPluginLongId(plugin)));

let default_plugins = db.default_macro_plugins();
let plugins = chain!(default_plugins.into_iter().cloned(), extra_plugins).collect::<Arc<[_]>>();
db.set_default_macro_plugins(plugins);

let cfg_set: Option<CfgSet> =
inputs.get("cfg").map(|s| serde_json::from_str(s.as_str()).unwrap());
Expand Down
20 changes: 9 additions & 11 deletions crates/cairo-lang-semantic/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1557,8 +1557,6 @@ pub trait SemanticGroup:

// Analyzer plugins.
// ========
#[salsa::input]
fn analyzer_plugins(&self) -> Vec<Arc<dyn AnalyzerPlugin>>;

#[salsa::input]
fn default_analyzer_plugins(&self) -> Arc<[AnalyzerPluginId]>;
Expand All @@ -1577,7 +1575,7 @@ pub trait SemanticGroup:

/// 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>>;
fn declared_allows(&self, crate_id: CrateId) -> Arc<OrderedHashSet<String>>;

// Helpers for language server.
// ============================
Expand Down Expand Up @@ -1712,7 +1710,10 @@ fn module_semantic_diagnostics(
diagnostics.extend(db.global_use_semantic_diagnostics(*global_use));
}
add_unused_item_diagnostics(db, module_id, &data, &mut diagnostics);
for analyzer_plugin in db.analyzer_plugins().iter() {
for analyzer_plugin_id in db.crate_analyzer_plugins(module_id.owning_crate(db.upcast())).iter()
{
let analyzer_plugin = db.lookup_intern_analyzer_plugin(*analyzer_plugin_id);

for diag in analyzer_plugin.diagnostics(db, module_id) {
diagnostics.add(SemanticDiagnostic::new(
StableLocation::new(diag.stable_ptr),
Expand All @@ -1729,9 +1730,11 @@ fn crate_analyzer_plugins(db: &dyn SemanticGroup, _crate_id: CrateId) -> Arc<[An
db.default_analyzer_plugins()
}

fn declared_allows(db: &dyn SemanticGroup) -> Arc<OrderedHashSet<String>> {
fn declared_allows(db: &dyn SemanticGroup, crate_id: CrateId) -> Arc<OrderedHashSet<String>> {
Arc::new(OrderedHashSet::from_iter(
db.analyzer_plugins().into_iter().flat_map(|plugin| plugin.declared_allows()),
db.crate_analyzer_plugins(crate_id)
.iter()
.flat_map(|plugin| db.lookup_intern_analyzer_plugin(*plugin).declared_allows()),
))
}

Expand Down Expand Up @@ -1880,11 +1883,6 @@ pub trait PluginSuiteInput: SemanticGroup {
fn intern_plugin_suite(&mut self, suite: PluginSuite) -> InternedPluginSuite {
let PluginSuite { plugins, inline_macro_plugins, analyzer_plugins } = suite;

// NOTE: kept for compatibility and testing, removed later in the stack.
self.set_macro_plugins(plugins.clone());
self.set_inline_macro_plugins(Arc::new(inline_macro_plugins.clone()));
self.set_analyzer_plugins(analyzer_plugins.clone());

let macro_plugins = plugins
.into_iter()
.map(|plugin| self.intern_macro_plugin(MacroPluginLongId(plugin)))
Expand Down
11 changes: 8 additions & 3 deletions crates/cairo-lang-semantic/src/diagnostic_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use cairo_lang_defs::db::DefsGroup;
use cairo_lang_defs::ids::{GenericTypeId, ModuleId, TopLevelLanguageElementId};
use cairo_lang_defs::ids::{GenericTypeId, MacroPluginLongId, ModuleId, TopLevelLanguageElementId};
use cairo_lang_defs::patcher::{PatchBuilder, RewriteNode};
use cairo_lang_defs::plugin::{
MacroPlugin, MacroPluginMetadata, PluginDiagnostic, PluginGeneratedFile, PluginResult,
Expand All @@ -14,6 +14,7 @@ use pretty_assertions::assert_eq;
use test_log::test;

use crate::db::SemanticGroup;
use crate::ids::AnalyzerPluginLongId;
use crate::items::us::SemanticUseEx;
use crate::plugin::AnalyzerPlugin;
use crate::resolve::ResolvedGenericItem;
Expand Down Expand Up @@ -139,7 +140,9 @@ impl MacroPlugin for AddInlineModuleDummyPlugin {
fn test_inline_module_diagnostics() {
let mut db_val = SemanticDatabaseForTesting::new_empty();
let db = &mut db_val;
db.set_macro_plugins(vec![Arc::new(AddInlineModuleDummyPlugin)]);
db.set_default_macro_plugins(Arc::new([
db.intern_macro_plugin(MacroPluginLongId(Arc::new(AddInlineModuleDummyPlugin)))
]));
let crate_id = setup_test_crate(db, indoc! {"
mod a {
#[test_change_return_type]
Expand Down Expand Up @@ -239,7 +242,9 @@ impl AnalyzerPlugin for NoU128RenameAnalyzerPlugin {
fn test_analyzer_diagnostics() {
let mut db_val = SemanticDatabaseForTesting::new_empty();
let db = &mut db_val;
db.set_analyzer_plugins(vec![Arc::new(NoU128RenameAnalyzerPlugin)]);
db.set_default_analyzer_plugins(Arc::new([
db.intern_analyzer_plugin(AnalyzerPluginLongId(Arc::new(NoU128RenameAnalyzerPlugin)))
]));
let crate_id = setup_test_crate(db, indoc! {"
mod inner {
use core::integer::u128 as long_u128_rename;
Expand Down
Loading

0 comments on commit 39e8dc8

Please sign in to comment.