sway_core/language/ty/declaration/
impl_trait.rs1use 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::{
11 fmt::Formatter,
12 hash::{Hash, Hasher},
13};
14use sway_types::{Ident, Named, Span, Spanned};
15
16pub type TyImplItem = TyTraitItem;
17
18#[derive(Clone, Debug, Serialize, Deserialize)]
21pub struct TyImplSelfOrTrait {
22 pub impl_type_parameters: Vec<TypeParameter>,
23 pub trait_name: CallPath,
24 pub trait_type_arguments: Vec<GenericArgument>,
25 pub items: Vec<TyImplItem>,
26 pub supertrait_items: Vec<TyImplItem>,
27 pub trait_decl_ref: Option<DeclRefMixedInterface>,
28 pub implementing_for: GenericArgument,
29 pub span: Span,
30}
31
32impl TyImplSelfOrTrait {
33 pub fn is_impl_contract(&self, te: &TypeEngine) -> bool {
34 matches!(
35 &*te.get(self.implementing_for.type_id()),
36 TypeInfo::Contract
37 )
38 }
39
40 pub fn is_impl_self(&self) -> bool {
41 self.trait_decl_ref.is_none()
42 }
43
44 pub fn is_impl_trait(&self) -> bool {
45 match &self.trait_decl_ref {
46 Some(decl_ref) => matches!(decl_ref.id(), InterfaceDeclId::Trait(_)),
47 _ => false,
48 }
49 }
50
51 pub fn is_impl_abi(&self) -> bool {
52 match &self.trait_decl_ref {
53 Some(decl_ref) => matches!(decl_ref.id(), InterfaceDeclId::Abi(_)),
54 _ => false,
55 }
56 }
57
58 pub fn implemented_trait_decl_id(&self) -> Option<DeclId<TyTraitDecl>> {
60 match &self.trait_decl_ref {
61 Some(decl_ref) => match &decl_ref.id() {
62 InterfaceDeclId::Trait(decl_id) => Some(*decl_id),
63 InterfaceDeclId::Abi(_) => None,
64 },
65 _ => None,
66 }
67 }
68
69 pub fn implemented_abi_decl_id(&self) -> Option<DeclId<TyAbiDecl>> {
71 match &self.trait_decl_ref {
72 Some(decl_ref) => match &decl_ref.id() {
73 InterfaceDeclId::Abi(decl_id) => Some(*decl_id),
74 InterfaceDeclId::Trait(_) => None,
75 },
76 _ => None,
77 }
78 }
79}
80
81impl TyDeclParsedType for TyImplSelfOrTrait {
82 type ParsedType = ImplSelfOrTrait;
83}
84
85impl Named for TyImplSelfOrTrait {
86 fn name(&self) -> &Ident {
87 &self.trait_name.suffix
88 }
89}
90
91impl Spanned for TyImplSelfOrTrait {
92 fn span(&self) -> Span {
93 self.span.clone()
94 }
95}
96
97impl EqWithEngines for TyImplSelfOrTrait {}
98impl PartialEqWithEngines for TyImplSelfOrTrait {
99 fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
100 self.impl_type_parameters
101 .eq(&other.impl_type_parameters, ctx)
102 && self.trait_name == other.trait_name
103 && self
104 .trait_type_arguments
105 .eq(&other.trait_type_arguments, ctx)
106 && self.items.eq(&other.items, ctx)
107 && self.implementing_for.eq(&other.implementing_for, ctx)
108 && self.trait_decl_ref.eq(&other.trait_decl_ref, ctx)
109 }
110}
111
112impl HashWithEngines for TyImplSelfOrTrait {
113 fn hash<H: Hasher>(&self, state: &mut H, engines: &Engines) {
114 let TyImplSelfOrTrait {
115 impl_type_parameters,
116 trait_name,
117 trait_type_arguments,
118 items,
119 implementing_for,
120 trait_decl_ref,
121 span: _,
124 supertrait_items: _,
125 } = self;
126 trait_name.hash(state);
127 impl_type_parameters.hash(state, engines);
128 trait_type_arguments.hash(state, engines);
129 items.hash(state, engines);
130 implementing_for.hash(state, engines);
131 trait_decl_ref.hash(state, engines);
132 }
133}
134
135impl SubstTypes for TyImplSelfOrTrait {
136 fn subst_inner(&mut self, ctx: &SubstTypesContext) -> HasChanges {
137 has_changes! {
138 self.impl_type_parameters.subst(ctx);
139 self.implementing_for.subst_inner(ctx);
140 self.items.subst(ctx);
141 }
142 }
143}
144
145impl DebugWithEngines for TyImplSelfOrTrait {
146 fn fmt(&self, f: &mut Formatter<'_>, engines: &Engines) -> std::fmt::Result {
147 if let Some(t) = self.trait_decl_ref.as_ref() {
148 write!(
149 f,
150 "impl<> {:?} for {:?} -> {:?}",
151 t.name().as_str(),
152 engines.help_out(self.implementing_for.initial_type_id()),
153 engines.help_out(self.implementing_for.type_id()),
154 )
155 } else {
156 write!(
157 f,
158 "impl<> {:?} -> {:?}",
159 engines.help_out(self.implementing_for.initial_type_id()),
160 engines.help_out(self.implementing_for.type_id()),
161 )
162 }
163 }
164}