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
|
/*
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 Christian <c@ethdev.com>
* @date 2014
* Solidity abstract syntax tree.
*/
#pragma once
#include <string>
#include <vector>
#include <memory>
#include <libsolidity/BaseTypes.h>
#include <libsolidity/Token.h>
namespace dev {
namespace solidity {
// Used as pointers to AST nodes, to be replaced by more clever pointers, e.g. pointers which do
// not do reference counting but point to a special memory area that is completely released
// explicitly.
template <class T>
using ptr = std::shared_ptr<T>;
template <class T>
using vecptr = std::vector<ptr<T>>;
class VariableDeclaration;
class StructDefinition;
class FunctionDefinition;
class TypeName;
class Block;
class Expression;
class ASTNode
{
public:
explicit ASTNode(Location const& _location)
: m_location(_location)
{}
Location getLocation() const { return m_location; }
private:
Location m_location;
};
class ContractDefinition : public ASTNode
{
public:
ContractDefinition(Location const& _location,
std::string const& _name,
vecptr<StructDefinition> const& _definedStructs,
vecptr<VariableDeclaration> const& _stateVariables,
vecptr<FunctionDefinition> const& _definedFunctions)
: ASTNode(_location), m_name(_name),
m_definedStructs(_definedStructs),
m_stateVariables(_stateVariables),
m_definedFunctions(_definedFunctions)
{}
private:
std::string m_name;
vecptr<StructDefinition> m_definedStructs;
vecptr<VariableDeclaration> m_stateVariables;
vecptr<FunctionDefinition> m_definedFunctions;
};
class StructDefinition : public ASTNode
{
public:
StructDefinition(Location const& _location,
std::string const& _name,
vecptr<VariableDeclaration> const& _members)
: ASTNode(_location), m_name(_name), m_members(_members)
{}
private:
std::string m_name;
vecptr<VariableDeclaration> m_members;
};
/// Used as function parameter list and return list
/// None of the parameters is allowed to contain mappings (not even recursively
/// inside structs)
class ParameterList : public ASTNode
{
public:
ParameterList(Location const& _location, vecptr<VariableDeclaration> const& _parameters)
: ASTNode(_location), m_parameters(_parameters)
{}
private:
vecptr<VariableDeclaration> m_parameters;
};
class FunctionDefinition : public ASTNode
{
public:
FunctionDefinition(Location const& _location, std::string const& _name, bool _isPublic,
ptr<ParameterList> const& _parameters,
bool _isDeclaredConst,
ptr<ParameterList> const& _returnParameters,
ptr<Block> const& _body)
: ASTNode(_location), m_name(_name), m_isPublic(_isPublic), m_parameters(_parameters),
m_isDeclaredConst(_isDeclaredConst), m_returnParameters(_returnParameters),
m_body(_body)
{}
private:
std::string m_name;
bool m_isPublic;
ptr<ParameterList> m_parameters;
bool m_isDeclaredConst;
ptr<ParameterList> m_returnParameters;
ptr<Block> m_body;
};
class VariableDeclaration : public ASTNode
{
public:
VariableDeclaration(Location const& _location,
ptr<TypeName> const& _type,
std::string const& _name)
: ASTNode(_location), m_type(_type), m_name(_name)
{}
private:
ptr<TypeName> m_type; ///< can be empty ("var")
std::string m_name;
};
/// types
/// @{
class TypeName : public ASTNode
{
public:
explicit TypeName(Location const& _location)
: ASTNode(_location)
{}
};
/// any pre-defined type that is not a mapping
class ElementaryTypeName : public TypeName
{
public:
explicit ElementaryTypeName(Location const& _location, Token::Value _type)
: TypeName(_location), m_type(_type)
{}
private:
Token::Value m_type;
};
class UserDefinedTypeName : public TypeName
{
public:
UserDefinedTypeName(Location const& _location, std::string const& _name)
: TypeName(_location), m_name(_name)
{}
private:
std::string m_name;
};
class Mapping : public TypeName
{
public:
Mapping(Location const& _location, ptr<ElementaryTypeName> const& _keyType,
ptr<TypeName> const& _valueType)
: TypeName(_location), m_keyType(_keyType), m_valueType(_valueType)
{}
private:
ptr<ElementaryTypeName> m_keyType;
ptr<TypeName> m_valueType;
};
/// @}
/// Statements
/// @{
class Statement : public ASTNode
{
public:
explicit Statement(Location const& _location)
: ASTNode(_location)
{}
};
class Block : public Statement
{
public:
explicit Block(Location const& _location)
: Statement(_location)
{}
private:
vecptr<Statement> m_statements;
};
class IfStatement : public Statement
{
private:
ptr<Expression> m_condition;
ptr<Statement> m_trueBody;
ptr<Statement> m_falseBody;
};
class BreakableStatement : public Statement
{
};
class WhileStatement : public BreakableStatement
{
private:
ptr<Expression> m_condition;
ptr<Statement> m_body;
};
class Continue : public Statement
{
};
class Break : public Statement
{
};
class Return : public Statement
{
private:
ptr<Expression> m_expression;
};
class VariableAssignment : public Statement
{
private:
ptr<VariableDeclaration> m_variable;
Token::Value m_assigmentOperator;
ptr<Expression> m_rightHandSide; ///< can be missing
};
class Expression : public Statement
{
private:
};
/// @}
/// Expressions
/// @{
class Assignment : public Expression
{
private:
ptr<Expression> m_leftHandSide;
Token::Value m_assigmentOperator;
ptr<Expression> m_rightHandSide;
};
class UnaryOperation : public Expression
{
private:
Token::Value m_operator;
ptr<Expression> m_subExpression;
bool isPrefix;
};
class BinaryOperation : public Expression
{
private:
ptr<Expression> m_left;
ptr<Expression> m_right;
Token::Value m_operator;
};
/// Can be ordinary function call, type cast or struct construction.
class FunctionCall : public Expression
{
private:
// if m_functionName is the name of a type, store the token directly
std::string m_functionName; // "in place" calls of return values are not possible for now
vecptr<Expression> m_arguments;
};
class MemberAccess : public Expression
{
private:
ptr<Expression> m_expression;
std::string m_memberName;
};
class IndexAccess : public Expression
{
ptr<Expression> m_base;
ptr<Expression> m_index;
};
class PrimaryExpression : public Expression
{
};
class Identifier : public PrimaryExpression
{
private:
std::string m_name;
};
class Literal : public PrimaryExpression
{
private:
std::string m_value;
};
/// @}
} }
|