aboutsummaryrefslogtreecommitdiffstats
path: root/libsolidity/ast/ASTVisitor.h
blob: 1a76103286478255387fd3a02389f984b1ad2afb (plain) (blame)
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
/*
    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/>.
*/
/**
 * @author Christian <c@ethdev.com>
 * @date 2014
 * AST visitor base class.
 */

#pragma once

#include <string>
#include <functional>
#include <vector>
#include <libsolidity/ast/AST.h>

namespace dev
{
namespace solidity
{

/**
 * Visitor interface for the abstract syntax tree. This class is tightly bound to the
 * implementation of @ref ASTNode::accept and its overrides. After a call to
 * @ref ASTNode::accept, the function visit for the appropriate parameter is called and then
 * (if it returns true) this continues recursively for all child nodes in document order
 * (there is an exception for contracts). After all child nodes have been visited, endVisit is
 * called for the node.
 */
class ASTVisitor
{
public:
    virtual ~ASTVisitor() = default;
    virtual bool visit(SourceUnit& _node) { return visitNode(_node); }
    virtual bool visit(PragmaDirective& _node) { return visitNode(_node); }
    virtual bool visit(ImportDirective& _node) { return visitNode(_node); }
    virtual bool visit(ContractDefinition& _node) { return visitNode(_node); }
    virtual bool visit(InheritanceSpecifier& _node) { return visitNode(_node); }
    virtual bool visit(UsingForDirective& _node) { return visitNode(_node); }
    virtual bool visit(StructDefinition& _node) { return visitNode(_node); }
    virtual bool visit(EnumDefinition& _node) { return visitNode(_node); }
    virtual bool visit(EnumValue& _node) { return visitNode(_node); }
    virtual bool visit(ParameterList& _node) { return visitNode(_node); }
    virtual bool visit(FunctionDefinition& _node) { return visitNode(_node); }
    virtual bool visit(VariableDeclaration& _node) { return visitNode(_node); }
    virtual bool visit(ModifierDefinition& _node) { return visitNode(_node); }
    virtual bool visit(ModifierInvocation& _node) { return visitNode(_node); }
    virtual bool visit(EventDefinition& _node) { return visitNode(_node); }
    virtual bool visit(ElementaryTypeName& _node) { return visitNode(_node); }
    virtual bool visit(UserDefinedTypeName& _node) { return visitNode(_node); }
    virtual bool visit(FunctionTypeName& _node) { return visitNode(_node); }
    virtual bool visit(Mapping& _node) { return visitNode(_node); }
    virtual bool visit(ArrayTypeName& _node) { return visitNode(_node); }
    virtual bool visit(InlineAssembly& _node) { return visitNode(_node); }
    virtual bool visit(Block& _node) { return visitNode(_node); }
    virtual bool visit(PlaceholderStatement& _node) { return visitNode(_node); }
    virtual bool visit(IfStatement& _node) { return visitNode(_node); }
    virtual bool visit(WhileStatement& _node) { return visitNode(_node); }
    virtual bool visit(ForStatement& _node) { return visitNode(_node); }
    virtual bool visit(Continue& _node) { return visitNode(_node); }
    virtual bool visit(Break& _node) { return visitNode(_node); }
    virtual bool visit(Return& _node) { return visitNode(_node); }
    virtual bool visit(Throw& _node) { return visitNode(_node); }
    virtual bool visit(EmitStatement& _node) { return visitNode(_node); }
    virtual bool visit(VariableDeclarationStatement& _node) { return visitNode(_node); }
    virtual bool visit(ExpressionStatement& _node) { return visitNode(_node); }
    virtual bool visit(Conditional& _node) { return visitNode(_node); }
    virtual bool visit(Assignment& _node) { return visitNode(_node); }
    virtual bool visit(TupleExpression& _node) { return visitNode(_node); }
    virtual bool visit(UnaryOperation& _node) { return visitNode(_node); }
    virtual bool visit(BinaryOperation& _node) { return visitNode(_node); }
    virtual bool visit(FunctionCall& _node) { return visitNode(_node); }
    virtual bool visit(NewExpression& _node) { return visitNode(_node); }
    virtual bool visit(MemberAccess& _node) { return visitNode(_node); }
    virtual bool visit(IndexAccess& _node) { return visitNode(_node); }
    virtual bool visit(Identifier& _node) { return visitNode(_node); }
    virtual bool visit(ElementaryTypeNameExpression& _node) { return visitNode(_node); }
    virtual bool visit(Literal& _node) { return visitNode(_node); }

    virtual void endVisit(SourceUnit& _node) { endVisitNode(_node); }
    virtual void endVisit(PragmaDirective& _node) { endVisitNode(_node); }
    virtual void endVisit(ImportDirective& _node) { endVisitNode(_node); }
    virtual void endVisit(ContractDefinition& _node) { endVisitNode(_node); }
    virtual void endVisit(InheritanceSpecifier& _node) { endVisitNode(_node); }
    virtual void endVisit(UsingForDirective& _node) { endVisitNode(_node); }
    virtual void endVisit(StructDefinition& _node) { endVisitNode(_node); }
    virtual void endVisit(EnumDefinition& _node) { endVisitNode(_node); }
    virtual void endVisit(EnumValue& _node) { endVisitNode(_node); }
    virtual void endVisit(ParameterList& _node) { endVisitNode(_node); }
    virtual void endVisit(FunctionDefinition& _node) { endVisitNode(_node); }
    virtual void endVisit(VariableDeclaration& _node) { endVisitNode(_node); }
    virtual void endVisit(ModifierDefinition& _node) { endVisitNode(_node); }
    virtual void endVisit(ModifierInvocation& _node) { endVisitNode(_node); }
    virtual void endVisit(EventDefinition& _node) { endVisitNode(_node); }
    virtual void endVisit(ElementaryTypeName& _node) { endVisitNode(_node); }
    virtual void endVisit(UserDefinedTypeName& _node) { endVisitNode(_node); }
    virtual void endVisit(FunctionTypeName& _node) { endVisitNode(_node); }
    virtual void endVisit(Mapping& _node) { endVisitNode(_node); }
    virtual void endVisit(ArrayTypeName& _node) { endVisitNode(_node); }
    virtual void endVisit(InlineAssembly& _node) { endVisitNode(_node); }
    virtual void endVisit(Block& _node) { endVisitNode(_node); }
    virtual void endVisit(PlaceholderStatement& _node) { endVisitNode(_node); }
    virtual void endVisit(IfStatement& _node) { endVisitNode(_node); }
    virtual void endVisit(WhileStatement& _node) { endVisitNode(_node); }
    virtual void endVisit(ForStatement& _node) { endVisitNode(_node); }
    virtual void endVisit(Continue& _node) { endVisitNode(_node); }
    virtual void endVisit(Break& _node) { endVisitNode(_node); }
    virtual void endVisit(Return& _node) { endVisitNode(_node); }
    virtual void endVisit(Throw& _node) { endVisitNode(_node); }
    virtual void endVisit(EmitStatement& _node) { endVisitNode(_node); }
    virtual void endVisit(VariableDeclarationStatement& _node) { endVisitNode(_node); }
    virtual void endVisit(ExpressionStatement& _node) { endVisitNode(_node); }
    virtual void endVisit(Conditional& _node) { endVisitNode(_node); }
    virtual void endVisit(Assignment& _node) { endVisitNode(_node); }
    virtual void endVisit(TupleExpression& _node) { endVisitNode(_node); }
    virtual void endVisit(UnaryOperation& _node) { endVisitNode(_node); }
    virtual void endVisit(BinaryOperation& _node) { endVisitNode(_node); }
    virtual void endVisit(FunctionCall& _node) { endVisitNode(_node); }
    virtual void endVisit(NewExpression& _node) { endVisitNode(_node); }
    virtual void endVisit(MemberAccess& _node) { endVisitNode(_node); }
    virtual void endVisit(IndexAccess& _node) { endVisitNode(_node); }
    virtual void endVisit(Identifier& _node) { endVisitNode(_node); }
    virtual void endVisit(ElementaryTypeNameExpression& _node) { endVisitNode(_node); }
    virtual void endVisit(Literal& _node) { endVisitNode(_node); }

protected:
    /// Generic function called by default for each node, to be overridden by derived classes
    /// if behaviour unspecific to a node type is desired.
    virtual bool visitNode(ASTNode&) { return true; }
    /// Generic function called by default for each node, to be overridden by derived classes
    /// if behaviour unspecific to a node type is desired.
    virtual void endVisitNode(ASTNode&) { }
};

class ASTConstVisitor
{
public:
    virtual ~ASTConstVisitor() = default;
    virtual bool visit(SourceUnit const& _node) { return visitNode(_node); }
    virtual bool visit(PragmaDirective const& _node) { return visitNode(_node); }
    virtual bool visit(ImportDirective const& _node) { return visitNode(_node); }
    virtual bool visit(ContractDefinition const& _node) { return visitNode(_node); }
    virtual bool visit(InheritanceSpecifier const& _node) { return visitNode(_node); }
    virtual bool visit(StructDefinition const& _node) { return visitNode(_node); }
    virtual bool visit(UsingForDirective const& _node) { return visitNode(_node); }
    virtual bool visit(EnumDefinition const& _node) { return visitNode(_node); }
    virtual bool visit(EnumValue const& _node) { return visitNode(_node); }
    virtual bool visit(ParameterList const& _node) { return visitNode(_node); }
    virtual bool visit(FunctionDefinition const& _node) { return visitNode(_node); }
    virtual bool visit(VariableDeclaration const& _node) { return visitNode(_node); }
    virtual bool visit(ModifierDefinition const& _node) { return visitNode(_node); }
    virtual bool visit(ModifierInvocation const& _node) { return visitNode(_node); }
    virtual bool visit(EventDefinition const& _node) { return visitNode(_node); }
    virtual bool visit(ElementaryTypeName const& _node) { return visitNode(_node); }
    virtual bool visit(UserDefinedTypeName const& _node) { return visitNode(_node); }
    virtual bool visit(FunctionTypeName const& _node) { return visitNode(_node); }
    virtual bool visit(Mapping const& _node) { return visitNode(_node); }
    virtual bool visit(ArrayTypeName const& _node) { return visitNode(_node); }
    virtual bool visit(Block const& _node) { return visitNode(_node); }
    virtual bool visit(PlaceholderStatement const& _node) { return visitNode(_node); }
    virtual bool visit(IfStatement const& _node) { return visitNode(_node); }
    virtual bool visit(WhileStatement const& _node) { return visitNode(_node); }
    virtual bool visit(ForStatement const& _node) { return visitNode(_node); }
    virtual bool visit(Continue const& _node) { return visitNode(_node); }
    virtual bool visit(InlineAssembly const& _node) { return visitNode(_node); }
    virtual bool visit(Break const& _node) { return visitNode(_node); }
    virtual bool visit(Return const& _node) { return visitNode(_node); }
    virtual bool visit(Throw const& _node) { return visitNode(_node); }
    virtual bool visit(EmitStatement const& _node) { return visitNode(_node); }
    virtual bool visit(VariableDeclarationStatement const& _node) { return visitNode(_node); }
    virtual bool visit(ExpressionStatement const& _node) { return visitNode(_node); }
    virtual bool visit(Conditional const& _node) { return visitNode(_node); }
    virtual bool visit(Assignment const& _node) { return visitNode(_node); }
    virtual bool visit(TupleExpression const& _node) { return visitNode(_node); }
    virtual bool visit(UnaryOperation const& _node) { return visitNode(_node); }
    virtual bool visit(BinaryOperation const& _node) { return visitNode(_node); }
    virtual bool visit(FunctionCall const& _node) { return visitNode(_node); }
    virtual bool visit(NewExpression const& _node) { return visitNode(_node); }
    virtual bool visit(MemberAccess const& _node) { return visitNode(_node); }
    virtual bool visit(IndexAccess const& _node) { return visitNode(_node); }
    virtual bool visit(Identifier const& _node) { return visitNode(_node); }
    virtual bool visit(ElementaryTypeNameExpression const& _node) { return visitNode(_node); }
    virtual bool visit(Literal const& _node) { return visitNode(_node); }

    virtual void endVisit(SourceUnit const& _node) { endVisitNode(_node); }
    virtual void endVisit(PragmaDirective const& _node) { endVisitNode(_node); }
    virtual void endVisit(ImportDirective const& _node) { endVisitNode(_node); }
    virtual void endVisit(ContractDefinition const& _node) { endVisitNode(_node); }
    virtual void endVisit(InheritanceSpecifier const& _node) { endVisitNode(_node); }
    virtual void endVisit(UsingForDirective const& _node) { endVisitNode(_node); }
    virtual void endVisit(StructDefinition const& _node) { endVisitNode(_node); }
    virtual void endVisit(EnumDefinition const& _node) { endVisitNode(_node); }
    virtual void endVisit(EnumValue const& _node) { endVisitNode(_node); }
    virtual void endVisit(ParameterList const& _node) { endVisitNode(_node); }
    virtual void endVisit(FunctionDefinition const& _node) { endVisitNode(_node); }
    virtual void endVisit(VariableDeclaration const& _node) { endVisitNode(_node); }
    virtual void endVisit(ModifierDefinition const& _node) { endVisitNode(_node); }
    virtual void endVisit(ModifierInvocation const& _node) { endVisitNode(_node); }
    virtual void endVisit(EventDefinition const& _node) { endVisitNode(_node); }
    virtual void endVisit(ElementaryTypeName const& _node) { endVisitNode(_node); }
    virtual void endVisit(UserDefinedTypeName const& _node) { endVisitNode(_node); }
    virtual void endVisit(FunctionTypeName const& _node) { endVisitNode(_node); }
    virtual void endVisit(Mapping const& _node) { endVisitNode(_node); }
    virtual void endVisit(ArrayTypeName const& _node) { endVisitNode(_node); }
    virtual void endVisit(Block const& _node) { endVisitNode(_node); }
    virtual void endVisit(PlaceholderStatement const& _node) { endVisitNode(_node); }
    virtual void endVisit(IfStatement const& _node) { endVisitNode(_node); }
    virtual void endVisit(WhileStatement const& _node) { endVisitNode(_node); }
    virtual void endVisit(ForStatement const& _node) { endVisitNode(_node); }
    virtual void endVisit(Continue const& _node) { endVisitNode(_node); }
    virtual void endVisit(InlineAssembly const& _node) { endVisitNode(_node); }
    virtual void endVisit(Break const& _node) { endVisitNode(_node); }
    virtual void endVisit(Return const& _node) { endVisitNode(_node); }
    virtual void endVisit(Throw const& _node) { endVisitNode(_node); }
    virtual void endVisit(EmitStatement const& _node) { endVisitNode(_node); }
    virtual void endVisit(VariableDeclarationStatement const& _node) { endVisitNode(_node); }
    virtual void endVisit(ExpressionStatement const& _node) { endVisitNode(_node); }
    virtual void endVisit(Conditional const& _node) { endVisitNode(_node); }
    virtual void endVisit(Assignment const& _node) { endVisitNode(_node); }
    virtual void endVisit(TupleExpression const& _node) { endVisitNode(_node); }
    virtual void endVisit(UnaryOperation const& _node) { endVisitNode(_node); }
    virtual void endVisit(BinaryOperation const& _node) { endVisitNode(_node); }
    virtual void endVisit(FunctionCall const& _node) { endVisitNode(_node); }
    virtual void endVisit(NewExpression const& _node) { endVisitNode(_node); }
    virtual void endVisit(MemberAccess const& _node) { endVisitNode(_node); }
    virtual void endVisit(IndexAccess const& _node) { endVisitNode(_node); }
    virtual void endVisit(Identifier const& _node) { endVisitNode(_node); }
    virtual void endVisit(ElementaryTypeNameExpression const& _node) { endVisitNode(_node); }
    virtual void endVisit(Literal const& _node) { endVisitNode(_node); }

protected:
    /// Generic function called by default for each node, to be overridden by derived classes
    /// if behaviour unspecific to a node type is desired.
    virtual bool visitNode(ASTNode const&) { return true; }
    /// Generic function called by default for each node, to be overridden by derived classes
    /// if behaviour unspecific to a node type is desired.
    virtual void endVisitNode(ASTNode const&) { }
};

/**
 * Utility class that accepts std::functions and calls them for visitNode and endVisitNode.
 */
class SimpleASTVisitor: public ASTConstVisitor
{
public:
    SimpleASTVisitor(
        std::function<bool(ASTNode const&)> _onVisit,
        std::function<void(ASTNode const&)> _onEndVisit
    ): m_onVisit(_onVisit), m_onEndVisit(_onEndVisit) {}

protected:
    bool visitNode(ASTNode const& _n) override { return m_onVisit ? m_onVisit(_n) : true; }
    void endVisitNode(ASTNode const& _n) override { m_onEndVisit(_n); }

private:
    std::function<bool(ASTNode const&)> m_onVisit;
    std::function<void(ASTNode const&)> m_onEndVisit;
};

/**
 * Utility class that visits the AST in depth-first order and calls a function on each node and each edge.
 * Child nodes are only visited if the node callback of the parent returns true.
 * The node callback of a parent is called before any edge or node callback involving the children.
 * The edge callbacks of all children are called before the edge callback of the parent.
 * This way, the node callback can be used as an initializing callback and the edge callbacks can be
 * used to compute a "reduce" function.
 */
class ASTReduce: public ASTConstVisitor
{
public:
    /**
     * Constructs a new ASTReduce object with the given callback functions.
     * @param _onNode called for each node, before its child edges and nodes, should return true to descend deeper
     * @param _onEdge called for each edge with (parent, child)
     */
    ASTReduce(
        std::function<bool(ASTNode const&)> _onNode,
        std::function<void(ASTNode const&, ASTNode const&)> _onEdge
    ): m_onNode(_onNode), m_onEdge(_onEdge)
    {
    }

protected:
    bool visitNode(ASTNode const& _node) override
    {
        m_parents.push_back(&_node);
        return m_onNode(_node);
    }
    void endVisitNode(ASTNode const& _node) override
    {
        m_parents.pop_back();
        if (!m_parents.empty())
            m_onEdge(*m_parents.back(), _node);
    }

private:
    std::vector<ASTNode const*> m_parents;
    std::function<bool(ASTNode const&)> m_onNode;
    std::function<void(ASTNode const&, ASTNode const&)> m_onEdge;
};

}
}