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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
/*
This file is part of cpp-ethereum.
cpp-ethereum 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.
cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @author Lefteris <lefteris@ethdev.com>
* @date 2015
* Converts the AST into json format
*/
#include <libsolidity/ast/ASTJsonConverter.h>
#include <boost/algorithm/string/join.hpp>
#include <libsolidity/ast/AST.h>
using namespace std;
namespace dev
{
namespace solidity
{
void ASTJsonConverter::addKeyValue(Json::Value& _obj, string const& _key, string const& _val)
{
// special handling for booleans
if (_key == "const" || _key == "public" || _key == "local" ||
_key == "lvalue" || _key == "local_lvalue" || _key == "prefix")
_obj[_key] = (_val == "1") ? true : false;
else
// else simply add it as a string
_obj[_key] = _val;
}
void ASTJsonConverter::addJsonNode(
ASTNode const& _node,
string const& _nodeName,
initializer_list<pair<string const, string const>> _list,
bool _hasChildren = false
)
{
Json::Value node;
node["id"] = reinterpret_cast<Json::UInt64>(&_node);
node["src"] = sourceLocationToString(_node.location());
node["name"] = _nodeName;
if (_list.size() != 0)
{
Json::Value attrs;
for (auto& e: _list)
addKeyValue(attrs, e.first, e.second);
node["attributes"] = attrs;
}
m_jsonNodePtrs.top()->append(node);
if (_hasChildren)
{
Json::Value& addedNode = (*m_jsonNodePtrs.top())[m_jsonNodePtrs.top()->size() - 1];
Json::Value children(Json::arrayValue);
addedNode["children"] = children;
m_jsonNodePtrs.push(&addedNode["children"]);
}
}
string ASTJsonConverter::sourceLocationToString(SourceLocation const& _location) const
{
int sourceIndex{-1};
if (_location.sourceName && m_sourceIndices.count(*_location.sourceName))
sourceIndex = m_sourceIndices.at(*_location.sourceName);
int length = -1;
if (_location.start >= 0 && _location.end >= 0)
length = _location.end - _location.start;
return std::to_string(_location.start) + ":" + std::to_string(length) + ":" + std::to_string(sourceIndex);
}
ASTJsonConverter::ASTJsonConverter(
ASTNode const& _ast,
map<string, unsigned> _sourceIndices
): m_ast(&_ast), m_sourceIndices(_sourceIndices)
{
Json::Value children(Json::arrayValue);
m_astJson["name"] = "root";
m_astJson["children"] = children;
m_jsonNodePtrs.push(&m_astJson["children"]);
}
void ASTJsonConverter::print(ostream& _stream)
{
process();
_stream << m_astJson;
}
Json::Value const& ASTJsonConverter::json()
{
process();
return m_astJson;
}
bool ASTJsonConverter::visit(ImportDirective const& _node)
{
addJsonNode(_node, "Import", { make_pair("file", _node.path())});
return true;
}
bool ASTJsonConverter::visit(ContractDefinition const& _node)
{
addJsonNode(_node, "Contract", { make_pair("name", _node.name()) }, true);
return true;
}
bool ASTJsonConverter::visit(InheritanceSpecifier const& _node)
{
addJsonNode(_node, "Inheritance", {}, true);
return true;
}
bool ASTJsonConverter::visit(UsingForDirective const& _node)
{
addJsonNode(_node, "UsingFor", {}, true);
return true;
}
bool ASTJsonConverter::visit(StructDefinition const& _node)
{
addJsonNode(_node, "Struct", { make_pair("name", _node.name()) }, true);
return true;
}
bool ASTJsonConverter::visit(EnumDefinition const& _node)
{
addJsonNode(_node, "Enum", { make_pair("name", _node.name()) }, true);
return true;
}
bool ASTJsonConverter::visit(ParameterList const& _node)
{
addJsonNode(_node, "ParameterList", {}, true);
return true;
}
bool ASTJsonConverter::visit(FunctionDefinition const& _node)
{
addJsonNode(_node, "Function",
{ make_pair("name", _node.name()),
make_pair("public", boost::lexical_cast<std::string>(_node.isPublic())),
make_pair("const", boost::lexical_cast<std::string>(_node.isDeclaredConst())) },
true);
return true;
}
bool ASTJsonConverter::visit(VariableDeclaration const& _node)
{
addJsonNode(_node, "VariableDeclaration", {
make_pair("name", _node.name()),
make_pair("name", _node.name()),
}, true);
return true;
}
bool ASTJsonConverter::visit(TypeName const&)
{
return true;
}
bool ASTJsonConverter::visit(ElementaryTypeName const& _node)
{
addJsonNode(_node, "ElementaryTypeName", { make_pair("name", _node.typeName().toString()) });
return true;
}
bool ASTJsonConverter::visit(UserDefinedTypeName const& _node)
{
addJsonNode(_node, "UserDefinedTypeName", {
make_pair("name", boost::algorithm::join(_node.namePath(), "."))
});
return true;
}
bool ASTJsonConverter::visit(Mapping const& _node)
{
addJsonNode(_node, "Mapping", {}, true);
return true;
}
bool ASTJsonConverter::visit(InlineAssembly const& _node)
{
addJsonNode(_node, "InlineAssembly", {}, true);
return true;
}
bool ASTJsonConverter::visit(Block const& _node)
{
addJsonNode(_node, "Block", {}, true);
return true;
}
bool ASTJsonConverter::visit(IfStatement const& _node)
{
addJsonNode(_node, "IfStatement", {}, true);
return true;
}
bool ASTJsonConverter::visit(WhileStatement const& _node)
{
addJsonNode(_node, "WhileStatement", {}, true);
return true;
}
bool ASTJsonConverter::visit(ForStatement const& _node)
{
addJsonNode(_node, "ForStatement", {}, true);
return true;
}
bool ASTJsonConverter::visit(Continue const& _node)
{
addJsonNode(_node, "Continue", {});
return true;
}
bool ASTJsonConverter::visit(Break const& _node)
{
addJsonNode(_node, "Break", {});
return true;
}
bool ASTJsonConverter::visit(Return const& _node)
{
addJsonNode(_node, "Return", {}, true);;
return true;
}
bool ASTJsonConverter::visit(Throw const& _node)
{
addJsonNode(_node, "Throw", {}, true);;
return true;
}
bool ASTJsonConverter::visit(VariableDeclarationStatement const& _node)
{
addJsonNode(_node, "VariableDefinition", {}, true);
return true;
}
bool ASTJsonConverter::visit(ExpressionStatement const& _node)
{
addJsonNode(_node, "ExpressionStatement", {}, true);
return true;
}
bool ASTJsonConverter::visit(Conditional const& _node)
{
addJsonNode(_node, "Conditional", {}, true);
return true;
}
bool ASTJsonConverter::visit(Assignment const& _node)
{
addJsonNode(_node, "Assignment",
{ make_pair("operator", Token::toString(_node.assignmentOperator())),
make_pair("type", type(_node)) },
true);
return true;
}
bool ASTJsonConverter::visit(TupleExpression const& _node)
{
addJsonNode(_node, "TupleExpression",{}, true);
return true;
}
bool ASTJsonConverter::visit(UnaryOperation const& _node)
{
addJsonNode(_node, "UnaryOperation",
{ make_pair("prefix", boost::lexical_cast<std::string>(_node.isPrefixOperation())),
make_pair("operator", Token::toString(_node.getOperator())),
make_pair("type", type(_node)) },
true);
return true;
}
bool ASTJsonConverter::visit(BinaryOperation const& _node)
{
addJsonNode(_node, "BinaryOperation", {
make_pair("operator", Token::toString(_node.getOperator())),
make_pair("type", type(_node))
}, true);
return true;
}
bool ASTJsonConverter::visit(FunctionCall const& _node)
{
addJsonNode(_node, "FunctionCall", {
make_pair("type_conversion", boost::lexical_cast<std::string>(_node.annotation().isTypeConversion)),
make_pair("type", type(_node))
}, true);
return true;
}
bool ASTJsonConverter::visit(NewExpression const& _node)
{
addJsonNode(_node, "NewExpression", { make_pair("type", type(_node)) }, true);
return true;
}
bool ASTJsonConverter::visit(MemberAccess const& _node)
{
addJsonNode(_node, "MemberAccess",
{ make_pair("member_name", _node.memberName()),
make_pair("type", type(_node)) },
true);
return true;
}
bool ASTJsonConverter::visit(IndexAccess const& _node)
{
addJsonNode(_node, "IndexAccess", { make_pair("type", type(_node)) }, true);
return true;
}
bool ASTJsonConverter::visit(Identifier const& _node)
{
addJsonNode(_node, "Identifier",
{ make_pair("value", _node.name()), make_pair("type", type(_node)) });
return true;
}
bool ASTJsonConverter::visit(ElementaryTypeNameExpression const& _node)
{
addJsonNode(_node, "ElementaryTypenameExpression",
{ make_pair("value", _node.typeName().toString()), make_pair("type", type(_node)) });
return true;
}
bool ASTJsonConverter::visit(Literal const& _node)
{
char const* tokenString = Token::toString(_node.token());
addJsonNode(_node, "Literal",
{ make_pair("string", (tokenString) ? tokenString : "null"),
make_pair("value", _node.value()),
make_pair("type", type(_node)) });
return true;
}
void ASTJsonConverter::endVisit(ImportDirective const&)
{
}
void ASTJsonConverter::endVisit(ContractDefinition const&)
{
goUp();
}
void ASTJsonConverter::endVisit(InheritanceSpecifier const&)
{
goUp();
}
void ASTJsonConverter::endVisit(UsingForDirective const&)
{
goUp();
}
void ASTJsonConverter::endVisit(StructDefinition const&)
{
goUp();
}
void ASTJsonConverter::endVisit(EnumDefinition const&)
{
goUp();
}
void ASTJsonConverter::endVisit(ParameterList const&)
{
goUp();
}
void ASTJsonConverter::endVisit(FunctionDefinition const&)
{
goUp();
}
void ASTJsonConverter::endVisit(VariableDeclaration const&)
{
goUp();
}
void ASTJsonConverter::endVisit(TypeName const&)
{
}
void ASTJsonConverter::endVisit(ElementaryTypeName const&)
{
}
void ASTJsonConverter::endVisit(UserDefinedTypeName const&)
{
}
void ASTJsonConverter::endVisit(Mapping const&)
{
goUp();
}
void ASTJsonConverter::endVisit(InlineAssembly const&)
{
goUp();
}
void ASTJsonConverter::endVisit(Block const&)
{
goUp();
}
void ASTJsonConverter::endVisit(IfStatement const&)
{
goUp();
}
void ASTJsonConverter::endVisit(WhileStatement const&)
{
goUp();
}
void ASTJsonConverter::endVisit(ForStatement const&)
{
goUp();
}
void ASTJsonConverter::endVisit(Continue const&)
{
}
void ASTJsonConverter::endVisit(Break const&)
{
}
void ASTJsonConverter::endVisit(Return const&)
{
goUp();
}
void ASTJsonConverter::endVisit(Throw const&)
{
goUp();
}
void ASTJsonConverter::endVisit(VariableDeclarationStatement const&)
{
goUp();
}
void ASTJsonConverter::endVisit(ExpressionStatement const&)
{
goUp();
}
void ASTJsonConverter::endVisit(Conditional const&)
{
goUp();
}
void ASTJsonConverter::endVisit(Assignment const&)
{
goUp();
}
void ASTJsonConverter::endVisit(TupleExpression const&)
{
goUp();
}
void ASTJsonConverter::endVisit(UnaryOperation const&)
{
goUp();
}
void ASTJsonConverter::endVisit(BinaryOperation const&)
{
goUp();
}
void ASTJsonConverter::endVisit(FunctionCall const&)
{
goUp();
}
void ASTJsonConverter::endVisit(NewExpression const&)
{
goUp();
}
void ASTJsonConverter::endVisit(MemberAccess const&)
{
goUp();
}
void ASTJsonConverter::endVisit(IndexAccess const&)
{
goUp();
}
void ASTJsonConverter::endVisit(Identifier const&)
{
}
void ASTJsonConverter::endVisit(ElementaryTypeNameExpression const&)
{
}
void ASTJsonConverter::endVisit(Literal const&)
{
}
void ASTJsonConverter::process()
{
if (!processed)
m_ast->accept(*this);
processed = true;
}
string ASTJsonConverter::type(Expression const& _expression)
{
return _expression.annotation().type ? _expression.annotation().type->toString() : "Unknown";
}
string ASTJsonConverter::type(VariableDeclaration const& _varDecl)
{
return _varDecl.annotation().type ? _varDecl.annotation().type->toString() : "Unknown";
}
}
}
|