ethers_solc/artifacts/ast/
yul.rs

1use super::{macros::node_group, misc::SourceLocation};
2use crate::artifacts::serde_helpers;
3use serde::{Deserialize, Serialize};
4
5node_group! {
6    YulStatement;
7
8    YulAssignment,
9    YulBlock,
10    YulBreak,
11    YulContinue,
12    YulExpressionStatement,
13    YulLeave,
14    YulForLoop,
15    YulFunctionDefinition,
16    YulIf,
17    YulSwitch,
18    YulVariableDeclaration,
19}
20
21node_group! {
22    YulExpression;
23
24    YulFunctionCall,
25    YulIdentifier,
26    YulLiteral,
27}
28
29/// A Yul block.
30#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
31pub struct YulBlock {
32    #[serde(with = "serde_helpers::display_from_str")]
33    pub src: SourceLocation,
34    pub statements: Vec<YulStatement>,
35}
36
37/// A Yul assignment statement.
38#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
39#[serde(rename_all = "camelCase")]
40pub struct YulAssignment {
41    #[serde(with = "serde_helpers::display_from_str")]
42    pub src: SourceLocation,
43    pub value: YulExpression,
44    pub variable_names: Vec<YulIdentifier>,
45}
46
47/// A Yul function call.
48#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
49#[serde(rename_all = "camelCase")]
50pub struct YulFunctionCall {
51    #[serde(with = "serde_helpers::display_from_str")]
52    pub src: SourceLocation,
53    pub arguments: Vec<YulExpression>,
54    pub function_name: YulIdentifier,
55}
56
57/// A Yul identifier.
58#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
59pub struct YulIdentifier {
60    #[serde(with = "serde_helpers::display_from_str")]
61    pub src: SourceLocation,
62    pub name: String,
63}
64
65/// A literal Yul value.
66#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
67#[serde(rename_all = "camelCase")]
68pub struct YulLiteral {
69    #[serde(with = "serde_helpers::display_from_str")]
70    pub src: SourceLocation,
71    pub hex_value: Option<String>, // TODO
72    pub value: Option<String>,     // TODO
73    pub kind: YulLiteralKind,
74    pub type_name: Option<String>, // TODO
75}
76
77/// Yul literal value kinds.
78#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
79#[serde(rename_all = "camelCase")]
80pub enum YulLiteralKind {
81    /// A number literal.
82    Number,
83    /// A string literal.
84    String,
85    /// A boolean literal.
86    Bool,
87}
88
89/// A Yul keyword.
90#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
91pub struct YulKeyword {
92    #[serde(with = "serde_helpers::display_from_str")]
93    pub src: SourceLocation,
94}
95
96/// The Yul break keyword.
97pub type YulBreak = YulKeyword;
98/// The Yul continue keyword.
99pub type YulContinue = YulKeyword;
100/// The Yul leave keyword.
101pub type YulLeave = YulKeyword;
102
103/// A Yul expression statement.
104#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
105pub struct YulExpressionStatement {
106    #[serde(with = "serde_helpers::display_from_str")]
107    pub src: SourceLocation,
108    pub expression: YulExpression,
109}
110
111/// A Yul for loop.
112#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
113pub struct YulForLoop {
114    #[serde(with = "serde_helpers::display_from_str")]
115    pub src: SourceLocation,
116    pub body: YulBlock,
117    pub condition: YulExpression,
118    pub post: YulBlock,
119    pub pre: YulBlock,
120}
121
122/// A Yul function definition.
123#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
124#[serde(rename_all = "camelCase")]
125pub struct YulFunctionDefinition {
126    #[serde(with = "serde_helpers::display_from_str")]
127    pub src: SourceLocation,
128    pub body: YulBlock,
129    pub name: String,
130    #[serde(default)]
131    pub parameters: Vec<YulTypedName>,
132    #[serde(default)]
133    pub return_variables: Vec<YulTypedName>,
134}
135
136/// A Yul type name.
137#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
138#[serde(rename_all = "camelCase")]
139pub struct YulTypedName {
140    #[serde(with = "serde_helpers::display_from_str")]
141    pub src: SourceLocation,
142    pub name: String,
143    #[serde(rename = "type")]
144    pub type_name: String, // TODO
145}
146
147/// A Yul if statement.
148#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
149pub struct YulIf {
150    #[serde(with = "serde_helpers::display_from_str")]
151    pub src: SourceLocation,
152    pub body: YulBlock,
153    pub condition: YulExpression,
154}
155
156/// A Yul switch statement.
157#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
158pub struct YulSwitch {
159    #[serde(with = "serde_helpers::display_from_str")]
160    pub src: SourceLocation,
161    pub cases: Vec<YulCase>,
162    pub expression: YulExpression,
163}
164
165/// A Yul switch statement case.
166#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
167pub struct YulCase {
168    #[serde(with = "serde_helpers::display_from_str")]
169    pub src: SourceLocation,
170    pub body: YulBlock,
171    pub value: YulCaseValue,
172}
173
174/// A Yul switch case value.
175#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
176#[serde(untagged)]
177pub enum YulCaseValue {
178    /// A case defined by a literal value.
179    YulLiteral(YulLiteral),
180    /// The default case
181    // TODO: How do we make this only match "default"?
182    Default(String),
183}
184
185/// A Yul variable declaration.
186#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
187pub struct YulVariableDeclaration {
188    #[serde(with = "serde_helpers::display_from_str")]
189    pub src: SourceLocation,
190    pub value: Option<YulExpression>,
191    pub variables: Vec<YulTypedName>,
192}