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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use super::{macros::node_group, misc::SourceLocation};
use crate::artifacts::serde_helpers;
use serde::{Deserialize, Serialize};

node_group! {
    YulStatement;

    YulAssignment,
    YulBlock,
    YulBreak,
    YulContinue,
    YulExpressionStatement,
    YulLeave,
    YulForLoop,
    YulFunctionDefinition,
    YulIf,
    YulSwitch,
    YulVariableDeclaration,
}

node_group! {
    YulExpression;

    YulFunctionCall,
    YulIdentifier,
    YulLiteral,
}

/// A Yul block.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct YulBlock {
    #[serde(with = "serde_helpers::display_from_str")]
    pub src: SourceLocation,
    pub statements: Vec<YulStatement>,
}

/// A Yul assignment statement.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct YulAssignment {
    #[serde(with = "serde_helpers::display_from_str")]
    pub src: SourceLocation,
    pub value: YulExpression,
    pub variable_names: Vec<YulIdentifier>,
}

/// A Yul function call.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct YulFunctionCall {
    #[serde(with = "serde_helpers::display_from_str")]
    pub src: SourceLocation,
    pub arguments: Vec<YulExpression>,
    pub function_name: YulIdentifier,
}

/// A Yul identifier.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct YulIdentifier {
    #[serde(with = "serde_helpers::display_from_str")]
    pub src: SourceLocation,
    pub name: String,
}

/// A literal Yul value.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct YulLiteral {
    #[serde(with = "serde_helpers::display_from_str")]
    pub src: SourceLocation,
    pub hex_value: Option<String>, // TODO
    pub value: Option<String>,     // TODO
    pub kind: YulLiteralKind,
    pub type_name: Option<String>, // TODO
}

/// Yul literal value kinds.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum YulLiteralKind {
    /// A number literal.
    Number,
    /// A string literal.
    String,
    /// A boolean literal.
    Bool,
}

/// A Yul keyword.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct YulKeyword {
    #[serde(with = "serde_helpers::display_from_str")]
    pub src: SourceLocation,
}

/// The Yul break keyword.
pub type YulBreak = YulKeyword;
/// The Yul continue keyword.
pub type YulContinue = YulKeyword;
/// The Yul leave keyword.
pub type YulLeave = YulKeyword;

/// A Yul expression statement.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct YulExpressionStatement {
    #[serde(with = "serde_helpers::display_from_str")]
    pub src: SourceLocation,
    pub expression: YulExpression,
}

/// A Yul for loop.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct YulForLoop {
    #[serde(with = "serde_helpers::display_from_str")]
    pub src: SourceLocation,
    pub body: YulBlock,
    pub condition: YulExpression,
    pub post: YulBlock,
    pub pre: YulBlock,
}

/// A Yul function definition.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct YulFunctionDefinition {
    #[serde(with = "serde_helpers::display_from_str")]
    pub src: SourceLocation,
    pub body: YulBlock,
    pub name: String,
    #[serde(default)]
    pub parameters: Vec<YulTypedName>,
    #[serde(default)]
    pub return_variables: Vec<YulTypedName>,
}

/// A Yul type name.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct YulTypedName {
    #[serde(with = "serde_helpers::display_from_str")]
    pub src: SourceLocation,
    pub name: String,
    #[serde(rename = "type")]
    pub type_name: String, // TODO
}

/// A Yul if statement.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct YulIf {
    #[serde(with = "serde_helpers::display_from_str")]
    pub src: SourceLocation,
    pub body: YulBlock,
    pub condition: YulExpression,
}

/// A Yul switch statement.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct YulSwitch {
    #[serde(with = "serde_helpers::display_from_str")]
    pub src: SourceLocation,
    pub cases: Vec<YulCase>,
    pub expression: YulExpression,
}

/// A Yul switch statement case.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct YulCase {
    #[serde(with = "serde_helpers::display_from_str")]
    pub src: SourceLocation,
    pub body: YulBlock,
    pub value: YulCaseValue,
}

/// A Yul switch case value.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum YulCaseValue {
    /// A case defined by a literal value.
    YulLiteral(YulLiteral),
    /// The default case
    // TODO: How do we make this only match "default"?
    Default(String),
}

/// A Yul variable declaration.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct YulVariableDeclaration {
    #[serde(with = "serde_helpers::display_from_str")]
    pub src: SourceLocation,
    pub value: Option<YulExpression>,
    pub variables: Vec<YulTypedName>,
}