/
AstVisitor.h
377 lines (329 loc) · 11.8 KB
/
AstVisitor.h
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
/*
* Souffle - A Datalog Compiler
* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved
* Licensed under the Universal Permissive License v 1.0 as shown at:
* - https://opensource.org/licenses/UPL
* - <souffle root>/licenses/SOUFFLE-UPL.txt
*/
/************************************************************************
*
* @file AstVisitor.h
*
* Provides some infrastructure for the implementation of operations
* on top of AST structures.
*
***********************************************************************/
#pragma once
#include "ast/AstArgument.h"
#include "ast/AstAttribute.h"
#include "ast/AstClause.h"
#include "ast/AstComponent.h"
#include "ast/AstLiteral.h"
#include "ast/AstNode.h"
#include "ast/AstPragma.h"
#include "ast/AstProgram.h"
#include "ast/AstRelation.h"
#include "ast/AstType.h"
#include "utility/FunctionalUtil.h"
#include "utility/MiscUtil.h"
#include <cstddef>
#include <functional>
#include <memory>
#include <type_traits>
#include <typeinfo>
#include <vector>
namespace souffle {
class AstArgument;
class AstConstraint;
class AstLiteral;
/** A tag type required for the is_ast_visitor type trait to identify AstVisitors */
struct ast_visitor_tag {};
/**
* The generic base type of all AstVisitors realizing the dispatching of
* visitor calls. Each visitor may define a return type R and a list of
* extra parameters to be passed along with the visited AstNodes to the
* corresponding visitor function.
*
* @tparam R the result type produced by a visit call
* @tparam Params extra parameters to be passed to the visit call
*/
template <typename R = void, typename... Params>
struct AstVisitor : public ast_visitor_tag {
/** A virtual destructor */
virtual ~AstVisitor() = default;
/** The main entry for the user allowing visitors to be utilized as functions */
R operator()(const AstNode& node, Params... args) {
return visit(node, args...);
}
/**
* The main entry for a visit process conducting the dispatching of
* a visit to the various sub-types of AstNodes. Sub-classes may override
* this implementation to conduct pre-visit operations.
*
* @param node the node to be visited
* @param args a list of extra parameters to be forwarded
*/
virtual R visit(const AstNode& node, Params... args) {
// dispatch node processing based on dynamic type
#define FORWARD(Kind) \
if (const auto* n = dynamic_cast<const Ast##Kind*>(&node)) return visit##Kind(*n, args...);
// types
FORWARD(SubsetType);
FORWARD(UnionType);
FORWARD(RecordType);
// arguments
FORWARD(Variable)
FORWARD(UnnamedVariable)
FORWARD(IntrinsicFunctor)
FORWARD(UserDefinedFunctor)
FORWARD(Counter)
FORWARD(NumericConstant)
FORWARD(StringConstant)
FORWARD(NilConstant)
FORWARD(TypeCast)
FORWARD(RecordInit)
FORWARD(Aggregator)
FORWARD(SubroutineArgument)
// literals
FORWARD(Atom)
FORWARD(ProvenanceNegation)
FORWARD(Negation)
FORWARD(BooleanConstraint)
FORWARD(BinaryConstraint)
// components
FORWARD(ComponentType);
FORWARD(ComponentInit);
FORWARD(Component);
// rest
FORWARD(Attribute);
FORWARD(Clause);
FORWARD(Relation);
FORWARD(Program);
FORWARD(Pragma);
#undef FORWARD
// did not work ...
fatal("unsupported type: %s", typeid(node).name());
}
protected:
#define LINK(Node, Parent) \
virtual R visit##Node(const Ast##Node& n, Params... args) { \
return visit##Parent(n, args...); \
}
// -- types --
LINK(SubsetType, Type);
LINK(RecordType, Type);
LINK(UnionType, Type);
LINK(Type, Node);
// -- arguments --
LINK(Variable, Argument)
LINK(UnnamedVariable, Argument)
LINK(Counter, Argument)
LINK(TypeCast, Argument)
LINK(SubroutineArgument, Argument)
LINK(NumericConstant, Constant)
LINK(StringConstant, Constant)
LINK(NilConstant, Constant)
LINK(Constant, Argument)
LINK(IntrinsicFunctor, Functor)
LINK(UserDefinedFunctor, Functor)
LINK(RecordInit, Term)
LINK(Functor, Term)
LINK(Term, Argument)
LINK(Aggregator, Argument)
LINK(Argument, Node);
// literals
LINK(Atom, Literal)
LINK(ProvenanceNegation, Negation)
LINK(Negation, Literal)
LINK(Literal, Node);
LINK(BooleanConstraint, Constraint)
LINK(BinaryConstraint, Constraint)
LINK(Constraint, Literal)
// components
LINK(ComponentType, Node);
LINK(ComponentInit, Node);
LINK(Component, Node);
// -- others --
LINK(Program, Node);
LINK(Attribute, Node);
LINK(Clause, Node);
LINK(Relation, Node);
LINK(Pragma, Node);
#undef LINK
/** The base case for all visitors -- if no more specific overload was defined */
virtual R visitNode(const AstNode& /*node*/, Params... /*args*/) {
return R();
}
};
/**
* A utility function visiting all nodes within the ast rooted by the given node
* recursively in a depth-first pre-order fashion applying the given visitor to each
* encountered node.
*
* @param root the root of the AST to be visited
* @param visitor the visitor to be applied on each node
* @param args a list of extra parameters to be forwarded to the visitor
*/
template <typename R, typename... Ps, typename... Args>
void visitDepthFirstPreOrder(const AstNode& root, AstVisitor<R, Ps...>& visitor, Args&... args) {
visitor(root, args...);
for (const AstNode* cur : root.getChildNodes()) {
if (cur != nullptr) {
visitDepthFirstPreOrder(*cur, visitor, args...);
}
}
}
/**
* A utility function visiting all nodes within the ast rooted by the given node
* recursively in a depth-first post-order fashion applying the given visitor to each
* encountered node.
*
* @param root the root of the AST to be visited
* @param visitor the visitor to be applied on each node
* @param args a list of extra parameters to be forwarded to the visitor
*/
template <typename R, typename... Ps, typename... Args>
void visitDepthFirstPostOrder(const AstNode& root, AstVisitor<R, Ps...>& visitor, Args&... args) {
for (const AstNode* cur : root.getChildNodes()) {
if (cur != nullptr) {
visitDepthFirstPostOrder(*cur, visitor, args...);
}
}
visitor(root, args...);
}
/**
* A utility function visiting all nodes within the ast rooted by the given node
* recursively in a depth-first pre-order fashion applying the given visitor to each
* encountered node.
*
* @param root the root of the AST to be visited
* @param visitor the visitor to be applied on each node
* @param args a list of extra parameters to be forwarded to the visitor
*/
template <typename R, typename... Ps, typename... Args>
void visitDepthFirst(const AstNode& root, AstVisitor<R, Ps...>& visitor, Args&... args) {
visitDepthFirstPreOrder(root, visitor, args...);
}
namespace detail {
/**
* A specialized visitor wrapping a lambda function -- an auxiliary type required
* for visitor convenience functions.
*/
template <typename R, typename N>
struct LambdaAstVisitor : public AstVisitor<void> {
std::function<R(const N&)> lambda;
LambdaAstVisitor(std::function<R(const N&)> lambda) : lambda(std::move(lambda)) {}
void visit(const AstNode& node) override {
if (const auto* n = dynamic_cast<const N*>(&node)) {
lambda(*n);
}
}
};
/**
* A factory function for creating LambdaAstVisitor instances.
*/
template <typename R, typename N>
LambdaAstVisitor<R, N> makeLambdaAstVisitor(const std::function<R(const N&)>& fun) {
return LambdaAstVisitor<R, N>(fun);
}
/**
* A type trait determining whether a given type is a visitor or not.
*/
template <typename T>
struct is_ast_visitor {
static constexpr size_t value = std::is_base_of<ast_visitor_tag, T>::value;
};
template <typename T>
struct is_ast_visitor<const T> : public is_ast_visitor<T> {};
template <typename T>
struct is_ast_visitor<T&> : public is_ast_visitor<T> {};
} // namespace detail
/**
* A utility function visiting all nodes within the ast rooted by the given node
* recursively in a depth-first pre-order fashion applying the given function to each
* encountered node.
*
* @param root the root of the AST to be visited
* @param fun the function to be applied
* @param args a list of extra parameters to be forwarded to the visitor
*/
template <typename R, typename N>
void visitDepthFirst(const AstNode& root, const std::function<R(const N&)>& fun) {
auto visitor = detail::makeLambdaAstVisitor(fun);
visitDepthFirst<void>(root, visitor);
}
/**
* A utility function visiting all nodes within the ast rooted by the given node
* recursively in a depth-first pre-order fashion applying the given function to each
* encountered node.
*
* @param root the root of the AST to be visited
* @param fun the function to be applied
* @param args a list of extra parameters to be forwarded to the visitor
*/
template <typename Lambda, typename R = typename lambda_traits<Lambda>::result_type,
typename N = typename lambda_traits<Lambda>::arg0_type>
typename std::enable_if<!detail::is_ast_visitor<Lambda>::value, void>::type visitDepthFirst(
const AstNode& root, const Lambda& fun) {
visitDepthFirst(root, std::function<R(const N&)>(fun));
}
/**
* A utility function visiting all nodes within a given list of AST root nodes
* recursively in a depth-first pre-order fashion applying the given function to each
* encountered node.
*
* @param list the list of roots of the ASTs to be visited
* @param fun the function to be applied
* @param args a list of extra parameters to be forwarded to the visitor
*/
template <typename T, typename Lambda>
void visitDepthFirst(const std::vector<T*>& list, const Lambda& fun) {
for (const auto& cur : list) {
visitDepthFirst(*cur, fun);
}
}
/**
* A utility function visiting all nodes within a given list of AST root nodes
* recursively in a depth-first pre-order fashion applying the given function to each
* encountered node.
*
* @param list the list of roots of the ASTs to be visited
* @param fun the function to be applied
* @param args a list of extra parameters to be forwarded to the visitor
*/
template <typename T, typename Lambda>
void visitDepthFirst(const std::vector<std::unique_ptr<T>>& list, const Lambda& fun) {
for (const auto& cur : list) {
visitDepthFirst(*cur, fun);
}
}
/**
* A utility function visiting all nodes within the ast rooted by the given node
* recursively in a depth-first post-order fashion applying the given function to each
* encountered node.
*
* @param root the root of the AST to be visited
* @param fun the function to be applied
* @param args a list of extra parameters to be forwarded to the visitor
*/
template <typename R, typename N>
void visitDepthFirstPostOrder(const AstNode& root, const std::function<R(const N&)>& fun) {
auto visitor = detail::makeLambdaAstVisitor(fun);
visitDepthFirstPostOrder<void>(root, visitor);
}
/**
* A utility function visiting all nodes within the ast rooted by the given node
* recursively in a depth-first post-order fashion applying the given function to each
* encountered node.
*
* @param root the root of the AST to be visited
* @param fun the function to be applied
* @param args a list of extra parameters to be forwarded to the visitor
*/
template <typename Lambda, typename R = typename lambda_traits<Lambda>::result_type,
typename N = typename lambda_traits<Lambda>::arg0_type>
typename std::enable_if<!detail::is_ast_visitor<Lambda>::value, void>::type visitDepthFirstPostOrder(
const AstNode& root, const Lambda& fun) {
visitDepthFirstPostOrder(root, std::function<R(const N&)>(fun));
}
} // namespace souffle