Skip to content

Commit

Permalink
Added .intern() methods to all IDs (#5429)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuvalsw authored Apr 18, 2024
1 parent 0af364a commit f93926d
Show file tree
Hide file tree
Showing 73 changed files with 6,217 additions and 4,298 deletions.
12 changes: 5 additions & 7 deletions crates/bin/get-lowering/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use cairo_lang_semantic::items::functions::{
use cairo_lang_semantic::ConcreteImplLongId;
use cairo_lang_starknet::starknet_plugin_suite;
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use cairo_lang_utils::LookupIntern;
use cairo_lang_utils::{Intern, LookupIntern};
use clap::Parser;
use convert_case::Casing;
use itertools::Itertools;
Expand Down Expand Up @@ -147,10 +147,11 @@ fn get_all_funcs(
res.insert(
impl_func.full_path(db.upcast()),
GenericFunctionWithBodyId::Impl(ImplGenericFunctionWithBodyId {
concrete_impl_id: db.intern_concrete_impl(ConcreteImplLongId {
concrete_impl_id: ConcreteImplLongId {
impl_def_id: *impl_def_id,
generic_args: vec![],
}),
}
.intern(db),
function: *impl_func,
}),
);
Expand All @@ -175,10 +176,7 @@ fn get_func_id_by_name(

Ok(ConcreteFunctionWithBodyId::from_semantic(
db,
db.intern_concrete_function_with_body(ConcreteFunctionWithBody {
generic_function: *func_id,
generic_args: vec![],
}),
ConcreteFunctionWithBody { generic_function: *func_id, generic_args: vec![] }.intern(db),
))
}

Expand Down
5 changes: 3 additions & 2 deletions crates/cairo-lang-compiler/src/diagnostics_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use cairo_lang_filesystem::db::{CrateConfiguration, FilesGroup, FilesGroupEx};
use cairo_lang_filesystem::db::{CrateConfiguration, FilesGroupEx};
use cairo_lang_filesystem::ids::{CrateLongId, Directory};
use cairo_lang_utils::Intern;

use crate::db::RootDatabase;
use crate::diagnostics::get_diagnostics_as_string;
Expand All @@ -8,7 +9,7 @@ use crate::diagnostics::get_diagnostics_as_string;
fn test_diagnostics() {
let mut db = RootDatabase::default();

let crate_id = db.intern_crate(CrateLongId::Real("bad_create".into()));
let crate_id = CrateLongId::Real("bad_create".into()).intern(&db);
db.set_crate_config(
crate_id,
Some(CrateConfiguration::default_for_root(Directory::Real("no/such/path".into()))),
Expand Down
12 changes: 6 additions & 6 deletions crates/cairo-lang-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ pub fn compile(
/// # Arguments
/// * `db` - Preloaded compilation database.
/// * `main_crate_ids` - [`CrateId`]s to compile. Do not include dependencies here, only pass
/// top-level crates in order to eliminate unused code. Use
/// `db.intern_crate(CrateLongId::Real(name))` in order to obtain [`CrateId`] from its name.
/// top-level crates in order to eliminate unused code. Use `CrateLongId::Real(name).intern(db)`
/// in order to obtain [`CrateId`] from its name.
/// * `compiler_config` - The compiler configuration.
/// # Returns
/// * `Ok(Program)` - The compiled program.
Expand All @@ -104,8 +104,8 @@ pub fn compile_prepared_db_program(
/// # Arguments
/// * `db` - Preloaded compilation database.
/// * `main_crate_ids` - [`CrateId`]s to compile. Do not include dependencies here, only pass
/// top-level crates in order to eliminate unused code. Use
/// `db.intern_crate(CrateLongId::Real(name))` in order to obtain [`CrateId`] from its name.
/// top-level crates in order to eliminate unused code. Use `CrateLongId::Real(name).intern(db)`
/// in order to obtain [`CrateId`] from its name.
/// * `compiler_config` - The compiler configuration.
/// # Returns
/// * `Ok(SierraProgramWithDebug)` - The compiled program with debug info.
Expand Down Expand Up @@ -139,8 +139,8 @@ pub fn compile_prepared_db(
/// # Arguments
/// * `db` - Preloaded compilation database.
/// * `main_crate_ids` - [`CrateId`]s to compile. Do not include dependencies here, only pass
/// top-level crates in order to eliminate unused code. Use
/// `db.intern_crate(CrateLongId::Real(name))` in order to obtain [`CrateId`] from its name.
/// top-level crates in order to eliminate unused code. Use `CrateLongId::Real(name).intern(db)`
/// in order to obtain [`CrateId`] from its name.
/// * `compiler_config` - The compiler configuration.
/// # Returns
/// * `Ok(ProgramArtifact)` - The compiled program artifact with requested debug info.
Expand Down
9 changes: 5 additions & 4 deletions crates/cairo-lang-compiler/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use cairo_lang_filesystem::db::{CrateConfiguration, FilesGroupEx};
use cairo_lang_filesystem::ids::{CrateId, CrateLongId, Directory};
pub use cairo_lang_project::*;
use cairo_lang_semantic::db::SemanticGroup;
use cairo_lang_utils::Intern;
use smol_str::SmolStr;

#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -42,15 +43,15 @@ pub fn setup_single_file_project(
let file_stem = path.file_stem().and_then(OsStr::to_str).ok_or_else(bad_path_err)?;
if file_stem == "lib" {
let crate_name = file_dir.to_str().ok_or_else(bad_path_err)?;
let crate_id = db.intern_crate(CrateLongId::Real(crate_name.into()));
let crate_id = CrateLongId::Real(crate_name.into()).intern(db);
db.set_crate_config(
crate_id,
Some(CrateConfiguration::default_for_root(Directory::Real(file_dir.to_path_buf()))),
);
Ok(crate_id)
} else {
// If file_stem is not lib, create a fake lib file.
let crate_id = db.intern_crate(CrateLongId::Real(file_stem.into()));
let crate_id = CrateLongId::Real(file_stem.into()).intern(db);
db.set_crate_config(
crate_id,
Some(CrateConfiguration::default_for_root(Directory::Real(file_dir.to_path_buf()))),
Expand Down Expand Up @@ -86,7 +87,7 @@ pub fn update_crate_root(
root: Directory,
) {
let crate_settings = config.content.crates_config.get(&crate_name);
let crate_id = db.intern_crate(CrateLongId::Real(crate_name));
let crate_id = CrateLongId::Real(crate_name).intern(db);
db.set_crate_config(
crate_id,
Some(CrateConfiguration { root, settings: crate_settings.clone() }),
Expand Down Expand Up @@ -138,6 +139,6 @@ pub fn get_main_crate_ids_from_project(
.content
.crate_roots
.keys()
.map(|crate_id| db.intern_crate(CrateLongId::Real(crate_id.clone())))
.map(|crate_id| CrateLongId::Real(crate_id.clone()).intern(db))
.collect()
}
6 changes: 3 additions & 3 deletions crates/cairo-lang-debug/src/debug_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::Debug;

use cairo_lang_proc_macros::DebugWithDb;
use cairo_lang_utils::{define_short_id, Upcast};
use cairo_lang_utils::{define_short_id, Intern, Upcast};
use test_log::test;

use crate::debug as cairo_lang_debug;
Expand Down Expand Up @@ -31,7 +31,7 @@ impl Upcast<dyn TestGroup> for DatabaseForTesting {
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
struct DummyLongId(usize);

define_short_id!(DummyShortId, DummyLongId, TestGroup, lookup_intern_b);
define_short_id!(DummyShortId, DummyLongId, TestGroup, lookup_intern_b, intern_b);

#[derive(DebugWithDb)]
#[debug_db(dyn TestGroup + 'static)]
Expand All @@ -47,6 +47,6 @@ struct ComplexStruct {
#[test]
fn test_debug() {
let db = DatabaseForTesting::default();
let a = ComplexStruct { a: Some(5), b: db.intern_b(DummyLongId(6)), c: 7, d: 8 };
let a = ComplexStruct { a: Some(5), b: DummyLongId(6).intern(&db), c: 7, d: 8 };
assert_eq!(format!("{:?}", a.debug(&db)), "ComplexStruct { a: Some(5), b: DummyLongId(6) }");
}
55 changes: 22 additions & 33 deletions crates/cairo-lang-defs/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use cairo_lang_syntax::node::kind::SyntaxKind;
use cairo_lang_syntax::node::{ast, Terminal, TypedStablePtr, TypedSyntaxNode};
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use cairo_lang_utils::ordered_hash_set::OrderedHashSet;
use cairo_lang_utils::Upcast;
use cairo_lang_utils::{Intern, Upcast};
use itertools::Itertools;

use crate::ids::*;
Expand Down Expand Up @@ -543,13 +543,14 @@ fn priv_module_data(db: &dyn DefsGroup, module_id: ModuleId) -> Maybe<ModuleData
}

if let Some(generated) = result.code {
let new_file = db.intern_file(FileLongId::Virtual(VirtualFile {
let new_file = FileLongId::Virtual(VirtualFile {
parent: Some(module_file),
name: generated.name,
content: Arc::new(generated.content),
code_mappings: Arc::new(generated.code_mappings),
kind: FileKind::Module,
}));
})
.intern(db);
generated_file_infos.push(Some(GeneratedFileInfo {
aux_data: generated.aux_data,
origin: module_file_id,
Expand All @@ -574,84 +575,72 @@ fn priv_module_data(db: &dyn DefsGroup, module_id: ModuleId) -> Maybe<ModuleData
);
match item_ast {
ast::ModuleItem::Constant(constant) => {
let item_id =
db.intern_constant(ConstantLongId(module_file_id, constant.stable_ptr()));
let item_id = ConstantLongId(module_file_id, constant.stable_ptr()).intern(db);
constants.insert(item_id, constant);
items.push(ModuleItemId::Constant(item_id));
}
ast::ModuleItem::Module(module) => {
let item_id =
db.intern_submodule(SubmoduleLongId(module_file_id, module.stable_ptr()));
let item_id = SubmoduleLongId(module_file_id, module.stable_ptr()).intern(db);
submodules.insert(item_id, module);
items.push(ModuleItemId::Submodule(item_id));
}
ast::ModuleItem::Use(us) => {
let path_leaves = get_all_path_leaves(db.upcast(), us.use_path(syntax_db));
for path_leaf in path_leaves {
let path_leaf_id =
db.intern_use(UseLongId(module_file_id, path_leaf.stable_ptr()));
UseLongId(module_file_id, path_leaf.stable_ptr()).intern(db);
uses.insert(path_leaf_id, path_leaf);
items.push(ModuleItemId::Use(path_leaf_id));
}
}
ast::ModuleItem::FreeFunction(function) => {
let item_id = db.intern_free_function(FreeFunctionLongId(
module_file_id,
function.stable_ptr(),
));
let item_id =
FreeFunctionLongId(module_file_id, function.stable_ptr()).intern(db);
free_functions.insert(item_id, function);
items.push(ModuleItemId::FreeFunction(item_id));
}
ast::ModuleItem::ExternFunction(extern_function) => {
let item_id = db.intern_extern_function(ExternFunctionLongId(
module_file_id,
extern_function.stable_ptr(),
));
let item_id =
ExternFunctionLongId(module_file_id, extern_function.stable_ptr())
.intern(db);
extern_functions.insert(item_id, extern_function);
items.push(ModuleItemId::ExternFunction(item_id));
}
ast::ModuleItem::ExternType(extern_type) => {
let item_id = db.intern_extern_type(ExternTypeLongId(
module_file_id,
extern_type.stable_ptr(),
));
let item_id =
ExternTypeLongId(module_file_id, extern_type.stable_ptr()).intern(db);
extern_types.insert(item_id, extern_type);
items.push(ModuleItemId::ExternType(item_id));
}
ast::ModuleItem::Trait(trt) => {
let item_id = db.intern_trait(TraitLongId(module_file_id, trt.stable_ptr()));
let item_id = TraitLongId(module_file_id, trt.stable_ptr()).intern(db);
traits.insert(item_id, trt);
items.push(ModuleItemId::Trait(item_id));
}
ast::ModuleItem::Impl(imp) => {
let item_id = db.intern_impl(ImplDefLongId(module_file_id, imp.stable_ptr()));
let item_id = ImplDefLongId(module_file_id, imp.stable_ptr()).intern(db);
impls.insert(item_id, imp);
items.push(ModuleItemId::Impl(item_id));
}
ast::ModuleItem::Struct(structure) => {
let item_id =
db.intern_struct(StructLongId(module_file_id, structure.stable_ptr()));
let item_id = StructLongId(module_file_id, structure.stable_ptr()).intern(db);
structs.insert(item_id, structure);
items.push(ModuleItemId::Struct(item_id));
}
ast::ModuleItem::Enum(enm) => {
let item_id = db.intern_enum(EnumLongId(module_file_id, enm.stable_ptr()));
let item_id = EnumLongId(module_file_id, enm.stable_ptr()).intern(db);
enums.insert(item_id, enm);
items.push(ModuleItemId::Enum(item_id));
}
ast::ModuleItem::TypeAlias(type_alias) => {
let item_id = db.intern_module_type_alias(ModuleTypeAliasLongId(
module_file_id,
type_alias.stable_ptr(),
));
let item_id =
ModuleTypeAliasLongId(module_file_id, type_alias.stable_ptr()).intern(db);
type_aliases.insert(item_id, type_alias);
items.push(ModuleItemId::TypeAlias(item_id));
}
ast::ModuleItem::ImplAlias(impl_alias) => {
let item_id = db.intern_impl_alias(ImplAliasLongId(
module_file_id,
impl_alias.stable_ptr(),
));
let item_id =
ImplAliasLongId(module_file_id, impl_alias.stable_ptr()).intern(db);
impl_aliases.insert(item_id, impl_alias);
items.push(ModuleItemId::ImplAlias(item_id));
}
Expand Down
Loading

0 comments on commit f93926d

Please sign in to comment.