/src/solidity/libyul/AsmJsonConverter.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | This file is part of solidity. |
3 | | |
4 | | solidity is free software: you can redistribute it and/or modify |
5 | | it under the terms of the GNU General Public License as published by |
6 | | the Free Software Foundation, either version 3 of the License, or |
7 | | (at your option) any later version. |
8 | | |
9 | | solidity is distributed in the hope that it will be useful, |
10 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | | GNU General Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU General Public License |
15 | | along with solidity. If not, see <http://www.gnu.org/licenses/>. |
16 | | */ |
17 | | // SPDX-License-Identifier: GPL-3.0 |
18 | | /** |
19 | | * @date 2019 |
20 | | * Converts inline assembly AST to JSON format |
21 | | */ |
22 | | |
23 | | #include <libyul/AsmJsonConverter.h> |
24 | | #include <libyul/AST.h> |
25 | | #include <libyul/Exceptions.h> |
26 | | #include <libsolutil/CommonData.h> |
27 | | #include <libsolutil/UTF8.h> |
28 | | |
29 | | using namespace std; |
30 | | |
31 | | namespace solidity::yul |
32 | | { |
33 | | |
34 | | Json::Value AsmJsonConverter::operator()(Block const& _node) const |
35 | 0 | { |
36 | 0 | Json::Value ret = createAstNode(nativeLocationOf(_node), "YulBlock"); |
37 | 0 | ret["statements"] = vectorOfVariantsToJson(_node.statements); |
38 | 0 | return ret; |
39 | 0 | } |
40 | | |
41 | | Json::Value AsmJsonConverter::operator()(TypedName const& _node) const |
42 | 0 | { |
43 | 0 | yulAssert(!_node.name.empty(), "Invalid variable name."); |
44 | 0 | Json::Value ret = createAstNode(nativeLocationOf(_node), "YulTypedName"); |
45 | 0 | ret["name"] = _node.name.str(); |
46 | 0 | ret["type"] = _node.type.str(); |
47 | 0 | return ret; |
48 | 0 | } |
49 | | |
50 | | Json::Value AsmJsonConverter::operator()(Literal const& _node) const |
51 | 0 | { |
52 | 0 | Json::Value ret = createAstNode(nativeLocationOf(_node), "YulLiteral"); |
53 | 0 | switch (_node.kind) |
54 | 0 | { |
55 | 0 | case LiteralKind::Number: |
56 | 0 | yulAssert( |
57 | 0 | util::isValidDecimal(_node.value.str()) || util::isValidHex(_node.value.str()), |
58 | 0 | "Invalid number literal" |
59 | 0 | ); |
60 | 0 | ret["kind"] = "number"; |
61 | 0 | break; |
62 | 0 | case LiteralKind::Boolean: |
63 | 0 | ret["kind"] = "bool"; |
64 | 0 | break; |
65 | 0 | case LiteralKind::String: |
66 | 0 | ret["kind"] = "string"; |
67 | 0 | ret["hexValue"] = util::toHex(util::asBytes(_node.value.str())); |
68 | 0 | break; |
69 | 0 | } |
70 | 0 | ret["type"] = _node.type.str(); |
71 | 0 | if (util::validateUTF8(_node.value.str())) |
72 | 0 | ret["value"] = _node.value.str(); |
73 | 0 | return ret; |
74 | 0 | } |
75 | | |
76 | | Json::Value AsmJsonConverter::operator()(Identifier const& _node) const |
77 | 0 | { |
78 | 0 | yulAssert(!_node.name.empty(), "Invalid identifier"); |
79 | 0 | Json::Value ret = createAstNode(nativeLocationOf(_node), "YulIdentifier"); |
80 | 0 | ret["name"] = _node.name.str(); |
81 | 0 | return ret; |
82 | 0 | } |
83 | | |
84 | | Json::Value AsmJsonConverter::operator()(Assignment const& _node) const |
85 | 0 | { |
86 | 0 | yulAssert(_node.variableNames.size() >= 1, "Invalid assignment syntax"); |
87 | 0 | Json::Value ret = createAstNode(nativeLocationOf(_node), "YulAssignment"); |
88 | 0 | for (auto const& var: _node.variableNames) |
89 | 0 | ret["variableNames"].append((*this)(var)); |
90 | 0 | ret["value"] = _node.value ? std::visit(*this, *_node.value) : Json::nullValue; |
91 | 0 | return ret; |
92 | 0 | } |
93 | | |
94 | | Json::Value AsmJsonConverter::operator()(FunctionCall const& _node) const |
95 | 0 | { |
96 | 0 | Json::Value ret = createAstNode(nativeLocationOf(_node), "YulFunctionCall"); |
97 | 0 | ret["functionName"] = (*this)(_node.functionName); |
98 | 0 | ret["arguments"] = vectorOfVariantsToJson(_node.arguments); |
99 | 0 | return ret; |
100 | 0 | } |
101 | | |
102 | | Json::Value AsmJsonConverter::operator()(ExpressionStatement const& _node) const |
103 | 0 | { |
104 | 0 | Json::Value ret = createAstNode(nativeLocationOf(_node), "YulExpressionStatement"); |
105 | 0 | ret["expression"] = std::visit(*this, _node.expression); |
106 | 0 | return ret; |
107 | 0 | } |
108 | | |
109 | | Json::Value AsmJsonConverter::operator()(VariableDeclaration const& _node) const |
110 | 0 | { |
111 | 0 | Json::Value ret = createAstNode(nativeLocationOf(_node), "YulVariableDeclaration"); |
112 | 0 | for (auto const& var: _node.variables) |
113 | 0 | ret["variables"].append((*this)(var)); |
114 | |
|
115 | 0 | ret["value"] = _node.value ? std::visit(*this, *_node.value) : Json::nullValue; |
116 | |
|
117 | 0 | return ret; |
118 | 0 | } |
119 | | |
120 | | Json::Value AsmJsonConverter::operator()(FunctionDefinition const& _node) const |
121 | 0 | { |
122 | 0 | yulAssert(!_node.name.empty(), "Invalid function name."); |
123 | 0 | Json::Value ret = createAstNode(nativeLocationOf(_node), "YulFunctionDefinition"); |
124 | 0 | ret["name"] = _node.name.str(); |
125 | 0 | for (auto const& var: _node.parameters) |
126 | 0 | ret["parameters"].append((*this)(var)); |
127 | 0 | for (auto const& var: _node.returnVariables) |
128 | 0 | ret["returnVariables"].append((*this)(var)); |
129 | 0 | ret["body"] = (*this)(_node.body); |
130 | 0 | return ret; |
131 | 0 | } |
132 | | |
133 | | Json::Value AsmJsonConverter::operator()(If const& _node) const |
134 | 0 | { |
135 | 0 | yulAssert(_node.condition, "Invalid if condition."); |
136 | 0 | Json::Value ret = createAstNode(nativeLocationOf(_node), "YulIf"); |
137 | 0 | ret["condition"] = std::visit(*this, *_node.condition); |
138 | 0 | ret["body"] = (*this)(_node.body); |
139 | 0 | return ret; |
140 | 0 | } |
141 | | |
142 | | Json::Value AsmJsonConverter::operator()(Switch const& _node) const |
143 | 0 | { |
144 | 0 | yulAssert(_node.expression, "Invalid expression pointer."); |
145 | 0 | Json::Value ret = createAstNode(nativeLocationOf(_node), "YulSwitch"); |
146 | 0 | ret["expression"] = std::visit(*this, *_node.expression); |
147 | 0 | for (auto const& var: _node.cases) |
148 | 0 | ret["cases"].append((*this)(var)); |
149 | 0 | return ret; |
150 | 0 | } |
151 | | |
152 | | Json::Value AsmJsonConverter::operator()(Case const& _node) const |
153 | 0 | { |
154 | 0 | Json::Value ret = createAstNode(nativeLocationOf(_node), "YulCase"); |
155 | 0 | ret["value"] = _node.value ? (*this)(*_node.value) : "default"; |
156 | 0 | ret["body"] = (*this)(_node.body); |
157 | 0 | return ret; |
158 | 0 | } |
159 | | |
160 | | Json::Value AsmJsonConverter::operator()(ForLoop const& _node) const |
161 | 0 | { |
162 | 0 | yulAssert(_node.condition, "Invalid for loop condition."); |
163 | 0 | Json::Value ret = createAstNode(nativeLocationOf(_node), "YulForLoop"); |
164 | 0 | ret["pre"] = (*this)(_node.pre); |
165 | 0 | ret["condition"] = std::visit(*this, *_node.condition); |
166 | 0 | ret["post"] = (*this)(_node.post); |
167 | 0 | ret["body"] = (*this)(_node.body); |
168 | 0 | return ret; |
169 | 0 | } |
170 | | |
171 | | Json::Value AsmJsonConverter::operator()(Break const& _node) const |
172 | 0 | { |
173 | 0 | return createAstNode(nativeLocationOf(_node), "YulBreak"); |
174 | 0 | } |
175 | | |
176 | | Json::Value AsmJsonConverter::operator()(Continue const& _node) const |
177 | 0 | { |
178 | 0 | return createAstNode(nativeLocationOf(_node), "YulContinue"); |
179 | 0 | } |
180 | | |
181 | | Json::Value AsmJsonConverter::operator()(Leave const& _node) const |
182 | 0 | { |
183 | 0 | return createAstNode(nativeLocationOf(_node), "YulLeave"); |
184 | 0 | } |
185 | | |
186 | | Json::Value AsmJsonConverter::createAstNode(langutil::SourceLocation const& _location, string _nodeType) const |
187 | 0 | { |
188 | 0 | Json::Value ret{Json::objectValue}; |
189 | 0 | ret["nodeType"] = std::move(_nodeType); |
190 | 0 | int length = -1; |
191 | 0 | if (_location.start >= 0 && _location.end >= 0) |
192 | 0 | length = _location.end - _location.start; |
193 | 0 | ret["src"] = to_string(_location.start) + ":" + to_string(length) + ":" + (m_sourceIndex.has_value() ? to_string(m_sourceIndex.value()) : "-1"); |
194 | 0 | return ret; |
195 | 0 | } |
196 | | |
197 | | template <class T> |
198 | | Json::Value AsmJsonConverter::vectorOfVariantsToJson(vector<T> const& _vec) const |
199 | 0 | { |
200 | 0 | Json::Value ret{Json::arrayValue}; |
201 | 0 | for (auto const& var: _vec) |
202 | 0 | ret.append(std::visit(*this, var)); |
203 | 0 | return ret; |
204 | 0 | } Unexecuted instantiation: Json::Value solidity::yul::AsmJsonConverter::vectorOfVariantsToJson<std::__1::variant<solidity::yul::ExpressionStatement, solidity::yul::Assignment, solidity::yul::VariableDeclaration, solidity::yul::FunctionDefinition, solidity::yul::If, solidity::yul::Switch, solidity::yul::ForLoop, solidity::yul::Break, solidity::yul::Continue, solidity::yul::Leave, solidity::yul::Block> >(std::__1::vector<std::__1::variant<solidity::yul::ExpressionStatement, solidity::yul::Assignment, solidity::yul::VariableDeclaration, solidity::yul::FunctionDefinition, solidity::yul::If, solidity::yul::Switch, solidity::yul::ForLoop, solidity::yul::Break, solidity::yul::Continue, solidity::yul::Leave, solidity::yul::Block>, std::__1::allocator<std::__1::variant<solidity::yul::ExpressionStatement, solidity::yul::Assignment, solidity::yul::VariableDeclaration, solidity::yul::FunctionDefinition, solidity::yul::If, solidity::yul::Switch, solidity::yul::ForLoop, solidity::yul::Break, solidity::yul::Continue, solidity::yul::Leave, solidity::yul::Block> > > const&) const Unexecuted instantiation: Json::Value solidity::yul::AsmJsonConverter::vectorOfVariantsToJson<std::__1::variant<solidity::yul::FunctionCall, solidity::yul::Identifier, solidity::yul::Literal> >(std::__1::vector<std::__1::variant<solidity::yul::FunctionCall, solidity::yul::Identifier, solidity::yul::Literal>, std::__1::allocator<std::__1::variant<solidity::yul::FunctionCall, solidity::yul::Identifier, solidity::yul::Literal> > > const&) const |
205 | | |
206 | | } |