Skip to main content

cairo_lang_semantic/items/
implization.rs

1use cairo_lang_defs::ids::{ImplDefId, TraitTypeId};
2use cairo_lang_diagnostics::Maybe;
3use salsa::Database;
4
5use crate::TypeId;
6use crate::items::imp::ImplSemantic;
7
8/// Implementation of [ImplizationSemantic::trait_type_implized_by_context].
9fn trait_type_implized_by_context<'db>(
10    db: &'db dyn Database,
11    trait_type_id: TraitTypeId<'db>,
12    impl_def_id: ImplDefId<'db>,
13) -> Maybe<TypeId<'db>> {
14    let impl_type_def_id = db.impl_type_by_trait_type(impl_def_id, trait_type_id)?;
15
16    db.impl_type_def_resolved_type(impl_type_def_id)
17}
18
19/// Query implementation of [ImplizationSemantic::trait_type_implized_by_context].
20#[salsa::tracked(cycle_result=trait_type_implized_by_context_cycle)]
21fn trait_type_implized_by_context_tracked<'db>(
22    db: &'db dyn Database,
23    trait_type_id: TraitTypeId<'db>,
24    impl_def_id: ImplDefId<'db>,
25) -> Maybe<TypeId<'db>> {
26    trait_type_implized_by_context(db, trait_type_id, impl_def_id)
27}
28
29/// Cycle handling for [ImplizationSemantic::trait_type_implized_by_context].
30fn trait_type_implized_by_context_cycle<'db>(
31    db: &'db dyn Database,
32    _id: salsa::Id,
33    trait_type_id: TraitTypeId<'db>,
34    impl_def_id: ImplDefId<'db>,
35) -> Maybe<TypeId<'db>> {
36    // Forwarding cycle handling to `priv_impl_type_semantic_data` handler.
37    trait_type_implized_by_context(db, trait_type_id, impl_def_id)
38}
39
40/// Trait for implization-related semantic queries.
41pub trait ImplizationSemantic<'db>: Database {
42    /// Returns the impl type for the given trait type, by implization by the given impl context, if
43    /// the impl matches the trait of the trait type.
44    // TODO(Gil): Consider removing the cycle handling here if we will upgrade the salsa version.
45    fn trait_type_implized_by_context(
46        &'db self,
47        trait_type_def_id: TraitTypeId<'db>,
48        impl_def_id: ImplDefId<'db>,
49    ) -> Maybe<TypeId<'db>> {
50        trait_type_implized_by_context_tracked(
51            self.as_dyn_database(),
52            trait_type_def_id,
53            impl_def_id,
54        )
55    }
56}
57impl<'db, T: Database + ?Sized> ImplizationSemantic<'db> for T {}