sway_core/language/ty/declaration/
variable.rs1use crate::{
2 ast_elements::type_argument::GenericTypeArgument,
3 engine_threading::*,
4 language::{parsed::VariableDeclaration, ty::*},
5 type_system::*,
6 HasChanges,
7};
8use serde::{Deserialize, Serialize};
9use std::hash::{Hash, Hasher};
10use sway_types::{Ident, Named, Spanned};
11
12#[derive(Clone, Debug, Serialize, Deserialize)]
13pub struct TyVariableDecl {
14 pub name: Ident,
15 pub body: TyExpression,
16 pub mutability: VariableMutability,
17 pub return_type: TypeId,
18 pub type_ascription: GenericTypeArgument,
19}
20
21impl TyDeclParsedType for TyVariableDecl {
22 type ParsedType = VariableDeclaration;
23}
24
25impl Named for TyVariableDecl {
26 fn name(&self) -> &sway_types::BaseIdent {
27 &self.name
28 }
29}
30
31impl Spanned for TyVariableDecl {
32 fn span(&self) -> sway_types::Span {
33 self.name.span()
34 }
35}
36
37impl EqWithEngines for TyVariableDecl {}
38impl PartialEqWithEngines for TyVariableDecl {
39 fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
40 let type_engine = ctx.engines().te();
41 self.name == other.name
42 && self.body.eq(&other.body, ctx)
43 && self.mutability == other.mutability
44 && type_engine
45 .get(self.return_type)
46 .eq(&type_engine.get(other.return_type), ctx)
47 && self.type_ascription.eq(&other.type_ascription, ctx)
48 }
49}
50
51impl HashWithEngines for TyVariableDecl {
52 fn hash<H: Hasher>(&self, state: &mut H, engines: &Engines) {
53 let TyVariableDecl {
54 name,
55 body,
56 mutability,
57 return_type,
58 type_ascription,
59 } = self;
60 let type_engine = engines.te();
61 name.hash(state);
62 body.hash(state, engines);
63 type_engine.get(*return_type).hash(state, engines);
64 type_ascription.hash(state, engines);
65 mutability.hash(state);
66 }
67}
68
69impl SubstTypes for TyVariableDecl {
70 fn subst_inner(&mut self, ctx: &SubstTypesContext) -> HasChanges {
71 self.return_type.subst(ctx);
72 self.type_ascription.subst(ctx);
73 self.body.subst(ctx)
74 }
75}