sway_core/language/ty/declaration/
constant.rs

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