sway_core/language/ty/declaration/
abi.rs

1use super::{TyDeclParsedType, TyTraitInterfaceItem, TyTraitItem};
2use crate::{
3    engine_threading::*,
4    language::parsed::{self, AbiDeclaration},
5    transform,
6    type_system::*,
7};
8use serde::{Deserialize, Serialize};
9use std::hash::{Hash, Hasher};
10use sway_types::{Ident, Named, Span, Spanned};
11
12/// A [TyAbiDecl] contains the type-checked version of the parse tree's
13/// `AbiDeclaration`.
14#[derive(Clone, Debug, Serialize, Deserialize)]
15pub struct TyAbiDecl {
16    /// The name of the abi trait (also known as a "contract trait")
17    pub name: Ident,
18    /// The methods a contract is required to implement in order opt in to this interface
19    pub interface_surface: Vec<TyTraitInterfaceItem>,
20    pub supertraits: Vec<parsed::Supertrait>,
21    pub items: Vec<TyTraitItem>,
22    pub span: Span,
23    pub attributes: transform::AttributesMap,
24}
25
26impl TyDeclParsedType for TyAbiDecl {
27    type ParsedType = AbiDeclaration;
28}
29
30impl EqWithEngines for TyAbiDecl {}
31impl PartialEqWithEngines for TyAbiDecl {
32    fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
33        let TyAbiDecl {
34            name: ln,
35            interface_surface: lis,
36            supertraits: ls,
37            items: li,
38            // these fields are not compared because they aren't relevant/a
39            // reliable source of obj v. obj distinction
40            attributes: _,
41            span: _,
42        } = self;
43        let TyAbiDecl {
44            name: rn,
45            interface_surface: ris,
46            supertraits: rs,
47            items: ri,
48            // these fields are not compared because they aren't relevant/a
49            // reliable source of obj v. obj distinction
50            attributes: _,
51            span: _,
52        } = other;
53        ln == rn && lis.eq(ris, ctx) && li.eq(ri, ctx) && ls.eq(rs, ctx)
54    }
55}
56
57impl HashWithEngines for TyAbiDecl {
58    fn hash<H: Hasher>(&self, state: &mut H, engines: &Engines) {
59        let TyAbiDecl {
60            name,
61            interface_surface,
62            items,
63            supertraits,
64            // these fields are not hashed because they aren't relevant/a
65            // reliable source of obj v. obj distinction
66            attributes: _,
67            span: _,
68        } = self;
69        name.hash(state);
70        interface_surface.hash(state, engines);
71        items.hash(state, engines);
72        supertraits.hash(state, engines);
73    }
74}
75
76impl CreateTypeId for TyAbiDecl {
77    fn create_type_id(&self, engines: &Engines) -> TypeId {
78        engines
79            .te()
80            .new_contract_caller(engines, AbiName::Known(self.name.clone().into()), None)
81    }
82}
83
84impl Spanned for TyAbiDecl {
85    fn span(&self) -> Span {
86        self.span.clone()
87    }
88}
89
90impl Named for TyAbiDecl {
91    fn name(&self) -> &Ident {
92        &self.name
93    }
94}