sway_core/language/ty/declaration/
abi.rs

1use super::{TyDeclParsedType, TyTraitInterfaceItem, TyTraitItem};
2use crate::{
3    decl_engine::DeclEngineGet as _,
4    engine_threading::*,
5    language::parsed::{self, AbiDeclaration},
6    transform,
7    type_system::*,
8};
9use serde::{Deserialize, Serialize};
10use std::hash::{Hash, Hasher};
11use sway_error::{
12    error::CompileError,
13    handler::{ErrorEmitted, Handler},
14};
15use sway_types::{Ident, Named, Span, Spanned};
16
17/// A [TyAbiDecl] contains the type-checked version of the parse tree's
18/// [AbiDeclaration].
19#[derive(Clone, Debug, Serialize, Deserialize)]
20pub struct TyAbiDecl {
21    /// The name of the abi trait (also known as a "contract trait")
22    pub name: Ident,
23    /// The methods a contract is required to implement in order opt in to this interface
24    pub interface_surface: Vec<TyTraitInterfaceItem>,
25    pub supertraits: Vec<parsed::Supertrait>,
26    pub items: Vec<TyTraitItem>,
27    pub span: Span,
28    pub attributes: transform::Attributes,
29}
30
31impl TyAbiDecl {
32    pub(crate) fn forbid_const_generics(
33        &self,
34        handler: &Handler,
35        engines: &Engines,
36    ) -> Result<(), ErrorEmitted> {
37        for item in self.interface_surface.iter() {
38            match item {
39                TyTraitInterfaceItem::TraitFn(decl_ref) => {
40                    let decl = engines.de().get(decl_ref.id());
41
42                    if decl.return_type.type_id.has_const_generics(engines) {
43                        let err = handler.emit_err(CompileError::ConstGenericNotSupportedHere {
44                            span: decl.return_type.span(),
45                        });
46                        return Err(err);
47                    }
48
49                    for arg in decl.parameters.iter() {
50                        if arg.type_argument.type_id.has_const_generics(engines) {
51                            let err =
52                                handler.emit_err(CompileError::ConstGenericNotSupportedHere {
53                                    span: arg.type_argument.span.clone(),
54                                });
55                            return Err(err);
56                        }
57                    }
58                }
59                TyTraitInterfaceItem::Constant(_) => {}
60                TyTraitInterfaceItem::Type(_) => {}
61            }
62        }
63
64        for item in self.items.iter() {
65            match item {
66                TyTraitItem::Fn(decl_ref) => {
67                    let decl = engines.de().get(decl_ref.id());
68                    if decl.return_type.type_id.has_const_generics(engines) {
69                        let err = handler.emit_err(CompileError::ConstGenericNotSupportedHere {
70                            span: decl.return_type.span(),
71                        });
72                        return Err(err);
73                    }
74
75                    for arg in decl.parameters.iter() {
76                        if arg.type_argument.type_id.has_const_generics(engines) {
77                            let err =
78                                handler.emit_err(CompileError::ConstGenericNotSupportedHere {
79                                    span: arg.type_argument.span.clone(),
80                                });
81                            return Err(err);
82                        }
83                    }
84                }
85                TyTraitItem::Constant(_) => {}
86                TyTraitItem::Type(_) => {}
87            }
88        }
89
90        Ok(())
91    }
92}
93
94impl TyDeclParsedType for TyAbiDecl {
95    type ParsedType = AbiDeclaration;
96}
97
98impl EqWithEngines for TyAbiDecl {}
99impl PartialEqWithEngines for TyAbiDecl {
100    fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
101        let TyAbiDecl {
102            name: ln,
103            interface_surface: lis,
104            supertraits: ls,
105            items: li,
106            // these fields are not compared because they aren't relevant/a
107            // reliable source of obj v. obj distinction
108            attributes: _,
109            span: _,
110        } = self;
111        let TyAbiDecl {
112            name: rn,
113            interface_surface: ris,
114            supertraits: rs,
115            items: ri,
116            // these fields are not compared because they aren't relevant/a
117            // reliable source of obj v. obj distinction
118            attributes: _,
119            span: _,
120        } = other;
121        ln == rn && lis.eq(ris, ctx) && li.eq(ri, ctx) && ls.eq(rs, ctx)
122    }
123}
124
125impl HashWithEngines for TyAbiDecl {
126    fn hash<H: Hasher>(&self, state: &mut H, engines: &Engines) {
127        let TyAbiDecl {
128            name,
129            interface_surface,
130            items,
131            supertraits,
132            // these fields are not hashed because they aren't relevant/a
133            // reliable source of obj v. obj distinction
134            attributes: _,
135            span: _,
136        } = self;
137        name.hash(state);
138        interface_surface.hash(state, engines);
139        items.hash(state, engines);
140        supertraits.hash(state, engines);
141    }
142}
143
144impl CreateTypeId for TyAbiDecl {
145    fn create_type_id(&self, engines: &Engines) -> TypeId {
146        engines
147            .te()
148            .new_contract_caller(engines, AbiName::Known(self.name.clone().into()), None)
149    }
150}
151
152impl Spanned for TyAbiDecl {
153    fn span(&self) -> Span {
154        self.span.clone()
155    }
156}
157
158impl Named for TyAbiDecl {
159    fn name(&self) -> &Ident {
160        &self.name
161    }
162}