cairo_lang_semantic/
helper.rs

1use 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
10/// Helper for getting functions in the corelib.
11pub struct ModuleHelper<'a> {
12    /// The db.
13    pub db: &'a dyn Database,
14    /// The current module id.
15    pub id: ModuleId<'a>,
16}
17impl<'a> ModuleHelper<'a> {
18    /// Returns a helper for the core module.
19    pub fn core(db: &'a dyn Database) -> Self {
20        Self { db, id: db.core_module() }
21    }
22    /// Returns a helper for a submodule named `name` of the current module.
23    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    /// Returns the id of an extern function named `name` in the current module.
29    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    /// Returns the id of a trait named `name` in the current module.
38    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    /// Returns the id of a free function named `name` in the current module.
47    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    /// Returns the id of a function named `name` in the current module, with the given
56    /// `generic_args`.
57    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    /// Returns the id of a generic function named `name` in the current module.
65    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    /// Returns the id of a type named `name` in the current module, with the given
69    /// `generic_args`.
70    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}