cairo_lang_semantic/
helper.rs1use cairo_lang_defs::ids::{ExternFunctionId, FreeFunctionId, ModuleId, ModuleItemId, TraitId};
2use cairo_lang_filesystem::ids::SmolStrId;
3use salsa::Database;
4
5use crate::corelib::CorelibSemantic;
6use crate::items::functions::GenericFunctionId;
7use crate::items::module::ModuleSemantic;
8use crate::{FunctionId, GenericArgumentId, TypeId, corelib};
9
10pub struct ModuleHelper<'a> {
12 pub db: &'a dyn Database,
14 pub id: ModuleId<'a>,
16}
17impl<'a> ModuleHelper<'a> {
18 pub fn core(db: &'a dyn Database) -> Self {
20 Self { db, id: db.core_module() }
21 }
22 pub fn submodule(&self, name: &'a str) -> Self {
24 let id = corelib::get_submodule(self.db, self.id, SmolStrId::from(self.db, name))
25 .unwrap_or_else(|| panic!("`{name}` missing in `{}`.", self.id.full_path(self.db)));
26 Self { db: self.db, id }
27 }
28 pub fn extern_function_id(&self, name: &str) -> ExternFunctionId<'a> {
30 let Ok(Some(ModuleItemId::ExternFunction(id))) =
31 self.db.module_item_by_name(self.id, SmolStrId::from(self.db, name))
32 else {
33 panic!("`{}` not found in `{}`.", name, self.id.full_path(self.db));
34 };
35 id
36 }
37 pub fn trait_id(&self, name: &'a str) -> TraitId<'a> {
39 let Ok(Some(ModuleItemId::Trait(id))) =
40 self.db.module_item_by_name(self.id, SmolStrId::from(self.db, name))
41 else {
42 panic!("`{name}` not found in `{}`.", self.id.full_path(self.db));
43 };
44 id
45 }
46 pub fn free_function_id(&self, name: &'a str) -> FreeFunctionId<'a> {
48 let Ok(Some(ModuleItemId::FreeFunction(id))) =
49 self.db.module_item_by_name(self.id, SmolStrId::from(self.db, name))
50 else {
51 panic!("`{name}` not found in `{}`.", self.id.full_path(self.db));
52 };
53 id
54 }
55 pub fn function_id(
58 &self,
59 name: &'a str,
60 generic_args: Vec<GenericArgumentId<'a>>,
61 ) -> FunctionId<'a> {
62 self.generic_function_id(name).concretize(self.db, generic_args)
63 }
64 pub fn generic_function_id(&self, name: &'a str) -> GenericFunctionId<'a> {
66 corelib::get_generic_function_id(self.db, self.id, SmolStrId::from(self.db, name))
67 }
68 pub fn ty(&self, name: &'a str, generic_args: Vec<GenericArgumentId<'a>>) -> TypeId<'a> {
71 corelib::get_ty_by_name(self.db, self.id, SmolStrId::from(self.db, name), generic_args)
72 }
73}