1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use std::{
    fmt,
    hash::{Hash, Hasher},
};

use sway_error::handler::{ErrorEmitted, Handler};
use sway_types::{Ident, Named, Span, Spanned};

use crate::{
    decl_engine::{DeclMapping, ReplaceDecls},
    engine_threading::*,
    has_changes,
    language::{parsed::ConstantDeclaration, ty::*, CallPath, Visibility},
    semantic_analysis::TypeCheckContext,
    transform,
    type_system::*,
};

#[derive(Clone, Debug)]
pub struct TyConstantDecl {
    pub call_path: CallPath,
    pub value: Option<TyExpression>,
    pub visibility: Visibility,
    pub attributes: transform::AttributesMap,
    pub return_type: TypeId,
    pub type_ascription: TypeArgument,
    pub span: Span,
}

impl TyDeclParsedType for TyConstantDecl {
    type ParsedType = ConstantDeclaration;
}

impl DebugWithEngines for TyConstantDecl {
    fn fmt(&self, f: &mut fmt::Formatter<'_>, _engines: &Engines) -> fmt::Result {
        write!(f, "{}", self.call_path)
    }
}

impl EqWithEngines for TyConstantDecl {}
impl PartialEqWithEngines for TyConstantDecl {
    fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
        let type_engine = ctx.engines().te();
        self.call_path == other.call_path
            && self.value.eq(&other.value, ctx)
            && self.visibility == other.visibility
            && self.type_ascription.eq(&other.type_ascription, ctx)
            && type_engine
                .get(self.return_type)
                .eq(&type_engine.get(other.return_type), ctx)
    }
}

impl HashWithEngines for TyConstantDecl {
    fn hash<H: Hasher>(&self, state: &mut H, engines: &Engines) {
        let type_engine = engines.te();
        let TyConstantDecl {
            call_path,
            value,
            visibility,
            return_type,
            type_ascription,
            // these fields are not hashed because they aren't relevant/a
            // reliable source of obj v. obj distinction
            attributes: _,
            span: _,
        } = self;
        call_path.hash(state);
        value.hash(state, engines);
        visibility.hash(state);
        type_engine.get(*return_type).hash(state, engines);
        type_ascription.hash(state, engines);
    }
}

impl Named for TyConstantDecl {
    fn name(&self) -> &Ident {
        &self.call_path.suffix
    }
}

impl Spanned for TyConstantDecl {
    fn span(&self) -> Span {
        self.span.clone()
    }
}

impl IsConcrete for TyConstantDecl {
    fn is_concrete(&self, engines: &Engines) -> bool {
        self.return_type
            .is_concrete(engines, TreatNumericAs::Concrete)
    }
}

impl SubstTypes for TyConstantDecl {
    fn subst_inner(&mut self, type_mapping: &TypeSubstMap, ctx: &SubstTypesContext) -> HasChanges {
        has_changes! {
            self.return_type.subst(type_mapping, ctx);
            self.type_ascription.subst(type_mapping, ctx);
            self.value.subst(type_mapping, ctx);
        }
    }
}

impl ReplaceDecls for TyConstantDecl {
    fn replace_decls_inner(
        &mut self,
        decl_mapping: &DeclMapping,
        handler: &Handler,
        ctx: &mut TypeCheckContext,
    ) -> Result<bool, ErrorEmitted> {
        if let Some(expr) = &mut self.value {
            expr.replace_decls(decl_mapping, handler, ctx)
        } else {
            Ok(false)
        }
    }
}