sway_core/language/ty/declaration/
trait_fn.rs

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use std::{
    fmt,
    hash::{Hash, Hasher},
};

use monomorphization::MonomorphizeHelper;
use sway_types::{Ident, Named, Span, Spanned};

use crate::{
    engine_threading::*,
    has_changes,
    language::{parsed::TraitFn, ty::*, Purity},
    transform,
    type_system::*,
};

#[derive(Clone, Debug)]
pub struct TyTraitFn {
    pub name: Ident,
    pub(crate) span: Span,
    pub(crate) purity: Purity,
    pub parameters: Vec<TyFunctionParameter>,
    pub return_type: TypeArgument,
    pub attributes: transform::AttributesMap,
}

impl TyDeclParsedType for TyTraitFn {
    type ParsedType = TraitFn;
}

impl DebugWithEngines for TyTraitFn {
    fn fmt(&self, f: &mut fmt::Formatter<'_>, engines: &Engines) -> fmt::Result {
        write!(
            f,
            "{:?}({}):{}",
            self.name,
            self.parameters
                .iter()
                .map(|p| format!(
                    "{}:{}",
                    p.name.as_str(),
                    engines.help_out(p.type_argument.initial_type_id)
                ))
                .collect::<Vec<_>>()
                .join(", "),
            engines.help_out(self.return_type.initial_type_id),
        )
    }
}

impl Named for TyTraitFn {
    fn name(&self) -> &Ident {
        &self.name
    }
}

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

impl IsConcrete for TyTraitFn {
    fn is_concrete(&self, engines: &Engines) -> bool {
        self.type_parameters()
            .iter()
            .all(|tp| tp.is_concrete(engines))
            && self
                .return_type
                .type_id
                .is_concrete(engines, TreatNumericAs::Concrete)
            && self.parameters().iter().all(|t| {
                t.type_argument
                    .type_id
                    .is_concrete(engines, TreatNumericAs::Concrete)
            })
    }
}

impl declaration::FunctionSignature for TyTraitFn {
    fn parameters(&self) -> &Vec<TyFunctionParameter> {
        &self.parameters
    }

    fn return_type(&self) -> &TypeArgument {
        &self.return_type
    }
}

impl EqWithEngines for TyTraitFn {}
impl PartialEqWithEngines for TyTraitFn {
    fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
        let type_engine = ctx.engines().te();
        self.name == other.name
            && self.purity == other.purity
            && self.parameters.eq(&other.parameters, ctx)
            && type_engine
                .get(self.return_type.type_id)
                .eq(&type_engine.get(other.return_type.type_id), ctx)
            && self.attributes == other.attributes
    }
}

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

impl SubstTypes for TyTraitFn {
    fn subst_inner(&mut self, ctx: &SubstTypesContext) -> HasChanges {
        has_changes! {
            self.parameters.subst(ctx);
            self.return_type.subst(ctx);
        }
    }
}

impl MonomorphizeHelper for TyTraitFn {
    fn name(&self) -> &Ident {
        &self.name
    }

    fn type_parameters(&self) -> &[TypeParameter] {
        &[]
    }

    fn has_self_type_param(&self) -> bool {
        false
    }
}