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
use std::hash::{Hash, Hasher};

use sway_types::{Ident, Span, Spanned};

use crate::{engine_threading::*, type_system::TypeId};

use super::TyExpression;

/// Describes the full storage access including all the subfields
#[derive(Clone, Debug)]
pub struct TyStorageAccess {
    pub fields: Vec<TyStorageAccessDescriptor>,
    pub storage_field_names: Vec<String>,
    pub struct_field_names: Vec<String>,
    pub key_expression: Option<Box<TyExpression>>,
    pub storage_keyword_span: Span,
}

impl EqWithEngines for TyStorageAccess {}
impl PartialEqWithEngines for TyStorageAccess {
    fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
        self.fields.len() == other.fields.len()
            && self.fields.eq(&other.fields, ctx)
            && self.storage_field_names.len() == other.storage_field_names.len()
            && self.storage_field_names.eq(&other.storage_field_names)
            && self.struct_field_names.len() == other.struct_field_names.len()
            && self.struct_field_names.eq(&other.struct_field_names)
            && self.key_expression.eq(&other.key_expression, ctx)
    }
}

impl HashWithEngines for TyStorageAccess {
    fn hash<H: Hasher>(&self, state: &mut H, engines: &Engines) {
        let TyStorageAccess {
            fields,
            storage_keyword_span,
            storage_field_names,
            struct_field_names,
            key_expression,
        } = self;
        fields.hash(state, engines);
        storage_field_names.hash(state);
        struct_field_names.hash(state);
        key_expression.hash(state, engines);
        storage_keyword_span.hash(state);
    }
}

impl Spanned for TyStorageAccess {
    fn span(&self) -> Span {
        // TODO: Use Span::join_all().
        self.fields
            .iter()
            .fold(self.fields[0].span.clone(), |acc, field| {
                Span::join(acc, &field.span)
            })
    }
}

impl TyStorageAccess {
    pub fn storage_field_name(&self) -> Ident {
        self.fields[0].name.clone()
    }
}

/// Describes a single subfield access in the sequence when accessing a subfield within storage.
#[derive(Clone, Debug)]
pub struct TyStorageAccessDescriptor {
    pub name: Ident,
    pub type_id: TypeId,
    pub(crate) span: Span,
}

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

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