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    trait_type_id: TraitTypeId<'db>,
33    impl_def_id: ImplDefId<'db>,
34) -> Maybe<TypeId<'db>> {
35    // Forwarding cycle handling to `priv_impl_type_semantic_data` handler.
36    trait_type_implized_by_context(db, trait_type_id, impl_def_id)
37}
38
39/// Trait for implization-related semantic queries.
40pub trait ImplizationSemantic<'db>: Database {
41    /// Returns the impl type for the given trait type, by implization by the given impl context, if
42    /// the impl matches the trait of the trait type.
43    // TODO(Gil): Consider removing the cycle handling here if we will upgrade the salsa version.
44    fn trait_type_implized_by_context(
45        &'db self,
46        trait_type_def_id: TraitTypeId<'db>,
47        impl_def_id: ImplDefId<'db>,
48    ) -> Maybe<TypeId<'db>> {
49        trait_type_implized_by_context_tracked(
50            self.as_dyn_database(),
51            trait_type_def_id,
52            impl_def_id,
53        )
54    }
55}
56impl<'db, T: Database + ?Sized> ImplizationSemantic<'db> for T {}