sway_core/language/ty/declaration/
impl_trait.rs

1use super::{TyAbiDecl, TyDeclParsedType, TyTraitDecl, TyTraitItem};
2use crate::{
3    decl_engine::{DeclId, DeclRefMixedInterface, InterfaceDeclId},
4    engine_threading::*,
5    has_changes,
6    language::{parsed::ImplSelfOrTrait, CallPath},
7    type_system::*,
8};
9use serde::{Deserialize, Serialize};
10use std::hash::{Hash, Hasher};
11use sway_types::{Ident, Named, Span, Spanned};
12
13pub type TyImplItem = TyTraitItem;
14
15// impl <A, B, C> Trait<Arg, Arg> for Type<Arg, Arg>
16#[derive(Clone, Debug, Serialize, Deserialize)]
17pub struct TyImplSelfOrTrait {
18    pub impl_type_parameters: Vec<TypeParameter>,
19    pub trait_name: CallPath,
20    pub trait_type_arguments: Vec<GenericArgument>,
21    pub items: Vec<TyImplItem>,
22    pub supertrait_items: Vec<TyImplItem>,
23    pub trait_decl_ref: Option<DeclRefMixedInterface>,
24    pub implementing_for: GenericArgument,
25    pub span: Span,
26}
27
28impl TyImplSelfOrTrait {
29    pub fn is_impl_contract(&self, te: &TypeEngine) -> bool {
30        matches!(
31            &*te.get(self.implementing_for.type_id()),
32            TypeInfo::Contract
33        )
34    }
35
36    pub fn is_impl_self(&self) -> bool {
37        self.trait_decl_ref.is_none()
38    }
39
40    pub fn is_impl_trait(&self) -> bool {
41        match &self.trait_decl_ref {
42            Some(decl_ref) => matches!(decl_ref.id(), InterfaceDeclId::Trait(_)),
43            _ => false,
44        }
45    }
46
47    pub fn is_impl_abi(&self) -> bool {
48        match &self.trait_decl_ref {
49            Some(decl_ref) => matches!(decl_ref.id(), InterfaceDeclId::Abi(_)),
50            _ => false,
51        }
52    }
53
54    /// Returns [DeclId] of the trait implemented by `self`, if `self` implements a trait.
55    pub fn implemented_trait_decl_id(&self) -> Option<DeclId<TyTraitDecl>> {
56        match &self.trait_decl_ref {
57            Some(decl_ref) => match &decl_ref.id() {
58                InterfaceDeclId::Trait(decl_id) => Some(*decl_id),
59                InterfaceDeclId::Abi(_) => None,
60            },
61            _ => None,
62        }
63    }
64
65    /// Returns [DeclId] of the ABI implemented by `self`, if `self` implements an ABI for a contract.
66    pub fn implemented_abi_decl_id(&self) -> Option<DeclId<TyAbiDecl>> {
67        match &self.trait_decl_ref {
68            Some(decl_ref) => match &decl_ref.id() {
69                InterfaceDeclId::Abi(decl_id) => Some(*decl_id),
70                InterfaceDeclId::Trait(_) => None,
71            },
72            _ => None,
73        }
74    }
75}
76
77impl TyDeclParsedType for TyImplSelfOrTrait {
78    type ParsedType = ImplSelfOrTrait;
79}
80
81impl Named for TyImplSelfOrTrait {
82    fn name(&self) -> &Ident {
83        &self.trait_name.suffix
84    }
85}
86
87impl Spanned for TyImplSelfOrTrait {
88    fn span(&self) -> Span {
89        self.span.clone()
90    }
91}
92
93impl EqWithEngines for TyImplSelfOrTrait {}
94impl PartialEqWithEngines for TyImplSelfOrTrait {
95    fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
96        self.impl_type_parameters
97            .eq(&other.impl_type_parameters, ctx)
98            && self.trait_name == other.trait_name
99            && self
100                .trait_type_arguments
101                .eq(&other.trait_type_arguments, ctx)
102            && self.items.eq(&other.items, ctx)
103            && self.implementing_for.eq(&other.implementing_for, ctx)
104            && self.trait_decl_ref.eq(&other.trait_decl_ref, ctx)
105    }
106}
107
108impl HashWithEngines for TyImplSelfOrTrait {
109    fn hash<H: Hasher>(&self, state: &mut H, engines: &Engines) {
110        let TyImplSelfOrTrait {
111            impl_type_parameters,
112            trait_name,
113            trait_type_arguments,
114            items,
115            implementing_for,
116            trait_decl_ref,
117            // these fields are not hashed because they aren't relevant/a
118            // reliable source of obj v. obj distinction
119            span: _,
120            supertrait_items: _,
121        } = self;
122        trait_name.hash(state);
123        impl_type_parameters.hash(state, engines);
124        trait_type_arguments.hash(state, engines);
125        items.hash(state, engines);
126        implementing_for.hash(state, engines);
127        trait_decl_ref.hash(state, engines);
128    }
129}
130
131impl SubstTypes for TyImplSelfOrTrait {
132    fn subst_inner(&mut self, ctx: &SubstTypesContext) -> HasChanges {
133        has_changes! {
134            self.impl_type_parameters.subst(ctx);
135            self.implementing_for.subst_inner(ctx);
136            self.items.subst(ctx);
137        }
138    }
139}