-
Notifications
You must be signed in to change notification settings - Fork 550
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract ModuleHelper from const folding. (#7099)
- Loading branch information
Showing
6 changed files
with
69 additions
and
63 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use cairo_lang_defs::ids::{ExternFunctionId, ModuleId, ModuleItemId}; | ||
use smol_str::SmolStr; | ||
|
||
use crate::db::SemanticGroup; | ||
use crate::{FunctionId, GenericArgumentId, corelib}; | ||
|
||
/// Helper for getting functions in the corelib. | ||
pub struct ModuleHelper<'a> { | ||
/// The db. | ||
pub db: &'a dyn SemanticGroup, | ||
/// The current module id. | ||
pub id: ModuleId, | ||
} | ||
impl<'a> ModuleHelper<'a> { | ||
/// Returns a helper for the core module. | ||
pub fn core(db: &'a dyn SemanticGroup) -> Self { | ||
Self { db, id: db.core_module() } | ||
} | ||
/// Returns a helper for a submodule named `name` of the current module. | ||
pub fn submodule(&self, name: &str) -> Self { | ||
let id = corelib::get_submodule(self.db, self.id, name).unwrap_or_else(|| { | ||
panic!("`{name}` missing in `{}`.", self.id.full_path(self.db.upcast())) | ||
}); | ||
Self { db: self.db, id } | ||
} | ||
/// Returns the id of an extern function named `name` in the current module. | ||
pub fn extern_function_id(&self, name: impl Into<SmolStr>) -> ExternFunctionId { | ||
let name = name.into(); | ||
let Ok(Some(ModuleItemId::ExternFunction(id))) = | ||
self.db.module_item_by_name(self.id, name.clone()) | ||
else { | ||
panic!("`{}` not found in `{}`.", name, self.id.full_path(self.db.upcast())); | ||
}; | ||
id | ||
} | ||
/// Returns the id of a function named `name` in the current module, with the given | ||
/// `generic_args`. | ||
pub fn function_id( | ||
&self, | ||
name: impl Into<SmolStr>, | ||
generic_args: Vec<GenericArgumentId>, | ||
) -> FunctionId { | ||
corelib::get_function_id(self.db, self.id, name.into(), generic_args) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters