Skip to main content

sway_core/language/ty/declaration/
configurable.rs

1use crate::{
2    decl_engine::{DeclId, DeclMapping, DeclRef, ReplaceDecls},
3    engine_threading::*,
4    has_changes,
5    language::{parsed::ConfigurableDeclaration, ty::*, CallPath, Visibility},
6    semantic_analysis::TypeCheckContext,
7    transform,
8    type_system::*,
9    HasChanges,
10};
11use serde::{Deserialize, Serialize};
12use std::{
13    fmt,
14    hash::{Hash, Hasher},
15};
16use sway_error::handler::{ErrorEmitted, Handler};
17use sway_types::{Ident, Named, Span, Spanned};
18
19#[derive(Clone, Debug, Serialize, Deserialize)]
20pub struct TyConfigurableDecl {
21    pub call_path: CallPath,
22    pub value: Option<TyExpression>,
23    pub visibility: Visibility,
24    pub attributes: transform::Attributes,
25    pub return_type: TypeId,
26    pub type_ascription: GenericTypeArgument,
27    pub span: Span,
28    // Only encoding v1 has a decode_fn
29    pub decode_fn: Option<DeclRef<DeclId<TyFunctionDecl>>>,
30}
31
32impl TyDeclParsedType for TyConfigurableDecl {
33    type ParsedType = ConfigurableDeclaration;
34}
35
36impl DebugWithEngines for TyConfigurableDecl {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>, _engines: &Engines) -> fmt::Result {
38        write!(f, "{}", self.call_path)
39    }
40}
41
42impl EqWithEngines for TyConfigurableDecl {}
43impl PartialEqWithEngines for TyConfigurableDecl {
44    fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
45        let type_engine = ctx.engines().te();
46        self.call_path == other.call_path
47            && self.value.eq(&other.value, ctx)
48            && self.visibility == other.visibility
49            && self.type_ascription.eq(&other.type_ascription, ctx)
50            && type_engine
51                .get(self.return_type)
52                .eq(&type_engine.get(other.return_type), ctx)
53    }
54}
55
56impl HashWithEngines for TyConfigurableDecl {
57    fn hash<H: Hasher>(&self, state: &mut H, engines: &Engines) {
58        let type_engine = engines.te();
59        let TyConfigurableDecl {
60            call_path,
61            value,
62            visibility,
63            return_type,
64            type_ascription,
65            // these fields are not hashed because they aren't relevant/a
66            // reliable source of obj v. obj distinction
67            attributes: _,
68            span: _,
69            decode_fn: _, // this is defined entirely by the type ascription
70        } = self;
71        call_path.hash(state);
72        value.hash(state, engines);
73        visibility.hash(state);
74        type_engine.get(*return_type).hash(state, engines);
75        type_ascription.hash(state, engines);
76    }
77}
78
79impl Named for TyConfigurableDecl {
80    fn name(&self) -> &Ident {
81        &self.call_path.suffix
82    }
83}
84
85impl Spanned for TyConfigurableDecl {
86    fn span(&self) -> Span {
87        self.span.clone()
88    }
89}
90
91impl SubstTypes for TyConfigurableDecl {
92    fn subst_inner(&mut self, ctx: &SubstTypesContext) -> HasChanges {
93        has_changes! {
94            self.return_type.subst(ctx);
95            self.type_ascription.subst(ctx);
96            self.value.subst(ctx);
97        }
98    }
99}
100
101impl ReplaceDecls for TyConfigurableDecl {
102    fn replace_decls_inner(
103        &mut self,
104        decl_mapping: &DeclMapping,
105        handler: &Handler,
106        ctx: &mut TypeCheckContext,
107    ) -> Result<HasChanges, ErrorEmitted> {
108        if let Some(expr) = &mut self.value {
109            expr.replace_decls(decl_mapping, handler, ctx)
110        } else {
111            Ok(HasChanges::No)
112        }
113    }
114}