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
use core::fmt;

use super::{SourceSpan, Spanned};
use crate::{ConstantData, Ident, Linkage, Type};

/// This represents the declaration of a global variable
#[derive(Spanned)]
pub struct GlobalVarDeclaration {
    #[span]
    pub span: SourceSpan,
    pub id: crate::GlobalVariable,
    pub name: Ident,
    pub ty: Type,
    pub linkage: Linkage,
    pub init: Option<crate::Constant>,
}
impl GlobalVarDeclaration {
    pub fn new(
        span: SourceSpan,
        id: crate::GlobalVariable,
        name: Ident,
        ty: Type,
        linkage: Linkage,
        init: Option<crate::Constant>,
    ) -> Self {
        Self {
            span,
            id,
            name,
            ty,
            linkage,
            init,
        }
    }
}
impl fmt::Debug for GlobalVarDeclaration {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use crate::display::DisplayOptional;

        f.debug_struct("GlobalVarDeclaration")
            .field("id", &format_args!("{}", &self.id))
            .field("name", &self.name.as_symbol())
            .field("ty", &self.ty)
            .field("linkage", &self.linkage)
            .field("init", &DisplayOptional(self.init.as_ref()))
            .finish()
    }
}
impl PartialEq for GlobalVarDeclaration {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
            && self.name == other.name
            && self.ty == other.ty
            && self.linkage == other.linkage
            && self.init == other.init
    }
}

/// This represents the declaration of a constant
#[derive(Spanned)]
pub struct ConstantDeclaration {
    #[span]
    pub span: SourceSpan,
    pub id: crate::Constant,
    pub init: ConstantData,
}
impl ConstantDeclaration {
    pub fn new(span: SourceSpan, id: crate::Constant, init: ConstantData) -> Self {
        Self { span, id, init }
    }
}
impl fmt::Debug for ConstantDeclaration {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("ConstantDeclaration")
            .field("id", &format_args!("{}", &self.id))
            .field("init", &format_args!("{}", &self.init))
            .finish()
    }
}
impl PartialEq for ConstantDeclaration {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id && self.init == other.init
    }
}

/// This represents the declaration of a data segment
#[derive(Spanned)]
pub struct DataSegmentDeclaration {
    #[span]
    pub span: SourceSpan,
    pub readonly: bool,
    pub offset: u32,
    pub size: u32,
    pub data: ConstantData,
}
impl DataSegmentDeclaration {
    pub fn new(
        span: SourceSpan,
        offset: u32,
        size: u32,
        readonly: bool,
        data: ConstantData,
    ) -> Self {
        Self {
            span,
            readonly,
            offset,
            size,
            data,
        }
    }
}
impl fmt::Debug for DataSegmentDeclaration {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("DataSegmentDeclaration")
            .field("offset", &self.offset)
            .field("size", &self.size)
            .field("readonly", &self.readonly)
            .field("data", &format_args!("{}", &self.data))
            .finish()
    }
}
impl PartialEq for DataSegmentDeclaration {
    fn eq(&self, other: &Self) -> bool {
        self.offset == other.offset
            && self.size == other.size
            && self.readonly == other.readonly
            && self.data == other.data
    }
}