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