forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsmJsonConverter.cpp
209 lines (185 loc) · 6.92 KB
/
AsmJsonConverter.cpp
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
/**
* @date 2019
* Converts inline assembly AST to JSON format
*/
#include <libyul/AST.h>
#include <libyul/AsmJsonConverter.h>
#include <libyul/Exceptions.h>
#include <libyul/Utilities.h>
#include <libsolutil/CommonData.h>
#include <libsolutil/UTF8.h>
namespace solidity::yul
{
Json AsmJsonConverter::operator()(Block const& _node) const
{
Json ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulBlock");
ret["statements"] = vectorOfVariantsToJson(_node.statements);
return ret;
}
Json AsmJsonConverter::operator()(NameWithDebugData const& _node) const
{
yulAssert(!_node.name.empty(), "Invalid variable name.");
Json ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulTypedName");
ret["name"] = _node.name.str();
// even though types are removed from Yul, we keep this field in the Json interface to not introduce
// a breaking change
// can be removed with the next breaking version
ret["type"] = "";
return ret;
}
Json AsmJsonConverter::operator()(Literal const& _node) const
{
yulAssert(validLiteral(_node));
Json ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulLiteral");
switch (_node.kind)
{
case LiteralKind::Number:
ret["kind"] = "number";
break;
case LiteralKind::Boolean:
ret["kind"] = "bool";
break;
case LiteralKind::String:
ret["kind"] = "string";
ret["hexValue"] = util::toHex(util::asBytes(formatLiteral(_node)));
break;
}
ret["type"] = "";
{
auto const formattedLiteral = formatLiteral(_node);
if (util::validateUTF8(formattedLiteral))
ret["value"] = formattedLiteral;
}
return ret;
}
Json AsmJsonConverter::operator()(Identifier const& _node) const
{
yulAssert(!_node.name.empty(), "Invalid identifier");
Json ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulIdentifier");
ret["name"] = _node.name.str();
return ret;
}
Json AsmJsonConverter::operator()(Assignment const& _node) const
{
yulAssert(_node.variableNames.size() >= 1, "Invalid assignment syntax");
Json ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulAssignment");
for (auto const& var: _node.variableNames)
ret["variableNames"].emplace_back((*this)(var));
ret["value"] = _node.value ? std::visit(*this, *_node.value) : Json();
return ret;
}
Json AsmJsonConverter::operator()(FunctionCall const& _node) const
{
Json ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulFunctionCall");
ret["functionName"] = (*this)(_node.functionName);
ret["arguments"] = vectorOfVariantsToJson(_node.arguments);
return ret;
}
Json AsmJsonConverter::operator()(ExpressionStatement const& _node) const
{
Json ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulExpressionStatement");
ret["expression"] = std::visit(*this, _node.expression);
return ret;
}
Json AsmJsonConverter::operator()(VariableDeclaration const& _node) const
{
Json ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulVariableDeclaration");
for (auto const& var: _node.variables)
ret["variables"].emplace_back((*this)(var));
ret["value"] = _node.value ? std::visit(*this, *_node.value) : Json();
return ret;
}
Json AsmJsonConverter::operator()(FunctionDefinition const& _node) const
{
yulAssert(!_node.name.empty(), "Invalid function name.");
Json ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulFunctionDefinition");
ret["name"] = _node.name.str();
for (auto const& var: _node.parameters)
ret["parameters"].emplace_back((*this)(var));
for (auto const& var: _node.returnVariables)
ret["returnVariables"].emplace_back((*this)(var));
ret["body"] = (*this)(_node.body);
return ret;
}
Json AsmJsonConverter::operator()(If const& _node) const
{
yulAssert(_node.condition, "Invalid if condition.");
Json ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulIf");
ret["condition"] = std::visit(*this, *_node.condition);
ret["body"] = (*this)(_node.body);
return ret;
}
Json AsmJsonConverter::operator()(Switch const& _node) const
{
yulAssert(_node.expression, "Invalid expression pointer.");
Json ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulSwitch");
ret["expression"] = std::visit(*this, *_node.expression);
for (auto const& var: _node.cases)
ret["cases"].emplace_back((*this)(var));
return ret;
}
Json AsmJsonConverter::operator()(Case const& _node) const
{
Json ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulCase");
ret["value"] = _node.value ? (*this)(*_node.value) : "default";
ret["body"] = (*this)(_node.body);
return ret;
}
Json AsmJsonConverter::operator()(ForLoop const& _node) const
{
yulAssert(_node.condition, "Invalid for loop condition.");
Json ret = createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulForLoop");
ret["pre"] = (*this)(_node.pre);
ret["condition"] = std::visit(*this, *_node.condition);
ret["post"] = (*this)(_node.post);
ret["body"] = (*this)(_node.body);
return ret;
}
Json AsmJsonConverter::operator()(Break const& _node) const
{
return createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulBreak");
}
Json AsmJsonConverter::operator()(Continue const& _node) const
{
return createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulContinue");
}
Json AsmJsonConverter::operator()(Leave const& _node) const
{
return createAstNode(originLocationOf(_node), nativeLocationOf(_node), "YulLeave");
}
Json AsmJsonConverter::createAstNode(langutil::SourceLocation const& _originLocation, langutil::SourceLocation const& _nativeLocation, std::string _nodeType) const
{
Json ret;
ret["nodeType"] = std::move(_nodeType);
auto srcLocation = [&](int start, int end) -> std::string
{
int length = (start >= 0 && end >= 0 && end >= start) ? end - start : -1;
return std::to_string(start) + ":" + std::to_string(length) + ":" + (m_sourceIndex.has_value() ? std::to_string(m_sourceIndex.value()) : "-1");
};
ret["src"] = srcLocation(_originLocation.start, _originLocation.end);
ret["nativeSrc"] = srcLocation(_nativeLocation.start, _nativeLocation.end);
return ret;
}
template <class T>
Json AsmJsonConverter::vectorOfVariantsToJson(std::vector<T> const& _vec) const
{
Json ret = Json::array();
for (auto const& var: _vec)
ret.emplace_back(std::visit(*this, var));
return ret;
}
}