/
RamVisitor.h
363 lines (316 loc) · 11.3 KB
/
RamVisitor.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
/*
* 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 RamVisitor.h
*
* Provides some infrastructure for the implementation of operations
* on top of RAM structures.
*
***********************************************************************/
#pragma once
#include "ram/RamCondition.h"
#include "ram/RamExpression.h"
#include "ram/RamNode.h"
#include "ram/RamOperation.h"
#include "ram/RamProgram.h"
#include "ram/RamRelation.h"
#include "ram/RamStatement.h"
#include "utility/FunctionalUtil.h"
#include "utility/MiscUtil.h"
#include <cstddef>
#include <functional>
#include <type_traits>
#include <typeinfo>
#include <vector>
namespace souffle {
/** A tag type required for the is_ram_visitor type trait to identify RamVisitors */
struct ram_visitor_tag {};
/**
* The generic base type of all RamVisitors 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 RamNodes 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 RamVisitor : public ram_visitor_tag {
/** A virtual destructor */
virtual ~RamVisitor() = default;
/** The main entry for the user allowing visitors to be utilized as functions */
R operator()(const RamNode& node, Params... args) {
return visit(node, args...);
}
/** The main entry for the user allowing visitors to be utilized as functions */
R operator()(const RamNode* 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 RamNodes. Sub-classes may override
* this implementation to conduct pre-visit operations.
*
* Note that the order of this list is important. Sub-classes must be listed
* before their super-classes; otherwise sub-classes cannot be visited.
*
* @param node the node to be visited
* @param args a list of extra parameters to be forwarded
*/
virtual R visit(const RamNode& node, Params... args) {
// dispatch node processing based on dynamic type
#define FORWARD(Kind) \
if (const auto* n = dynamic_cast<const Ram##Kind*>(&node)) return visit##Kind(*n, args...);
// Relation
FORWARD(Relation);
FORWARD(RelationReference);
// Expressions
FORWARD(TupleElement);
FORWARD(SignedConstant);
FORWARD(UnsignedConstant);
FORWARD(FloatConstant);
FORWARD(Constant);
FORWARD(IntrinsicOperator);
FORWARD(UserDefinedOperator);
FORWARD(AutoIncrement);
FORWARD(PackRecord);
FORWARD(SubroutineArgument);
FORWARD(UndefValue);
// Conditions
FORWARD(True);
FORWARD(False);
FORWARD(EmptinessCheck);
FORWARD(ProvenanceExistenceCheck);
FORWARD(ExistenceCheck);
FORWARD(Conjunction);
FORWARD(Negation);
FORWARD(Constraint);
// Operations
FORWARD(Filter);
FORWARD(Break);
FORWARD(Project);
FORWARD(SubroutineReturn);
FORWARD(UnpackRecord);
FORWARD(NestedIntrinsicOperator);
FORWARD(ParallelScan);
FORWARD(Scan);
FORWARD(ParallelIndexScan);
FORWARD(IndexScan);
FORWARD(ParallelChoice);
FORWARD(Choice);
FORWARD(ParallelIndexChoice);
FORWARD(IndexChoice);
FORWARD(ParallelAggregate);
FORWARD(Aggregate);
FORWARD(ParallelIndexAggregate);
FORWARD(IndexAggregate);
// Statements
FORWARD(IO);
FORWARD(Query);
FORWARD(Clear);
FORWARD(LogSize);
FORWARD(Swap);
FORWARD(Extend);
// Control-flow
FORWARD(Program);
FORWARD(Sequence);
FORWARD(Loop);
FORWARD(Parallel);
FORWARD(Exit);
FORWARD(LogTimer);
FORWARD(LogRelationTimer);
FORWARD(DebugInfo);
FORWARD(Call);
#undef FORWARD
// did not work ...
fatal("unsupported type: %s", typeid(node).name());
}
virtual R visit(const RamNode* node, Params... args) {
return visit(*node, args...);
}
protected:
#define LINK(Node, Parent) \
virtual R visit##Node(const Ram##Node& n, Params... args) { \
return visit##Parent(n, args...); \
}
// -- statements --
LINK(IO, RelationStatement);
LINK(Query, Statement);
LINK(Clear, RelationStatement);
LINK(LogSize, RelationStatement);
LINK(RelationStatement, Statement);
LINK(Swap, BinRelationStatement);
LINK(Extend, BinRelationStatement);
LINK(BinRelationStatement, Statement);
LINK(Sequence, ListStatement);
LINK(Loop, Statement);
LINK(Parallel, ListStatement);
LINK(ListStatement, Statement);
LINK(Exit, Statement);
LINK(LogTimer, Statement);
LINK(LogRelationTimer, Statement);
LINK(DebugInfo, Statement);
LINK(Call, Statement);
LINK(Statement, Node);
// -- operations --
LINK(Project, Operation);
LINK(SubroutineReturn, Operation);
LINK(UnpackRecord, TupleOperation);
LINK(NestedIntrinsicOperator, TupleOperation)
LINK(Scan, RelationOperation);
LINK(ParallelScan, Scan);
LINK(IndexScan, IndexOperation);
LINK(ParallelIndexScan, IndexScan);
LINK(Choice, RelationOperation);
LINK(ParallelChoice, Choice);
LINK(IndexChoice, IndexOperation);
LINK(ParallelIndexChoice, IndexChoice);
LINK(RelationOperation, TupleOperation);
LINK(Aggregate, RelationOperation);
LINK(ParallelAggregate, Aggregate);
LINK(IndexAggregate, IndexOperation);
LINK(ParallelIndexAggregate, IndexAggregate);
LINK(IndexOperation, RelationOperation);
LINK(TupleOperation, NestedOperation);
LINK(Filter, AbstractConditional);
LINK(Break, AbstractConditional);
LINK(AbstractConditional, NestedOperation);
LINK(NestedOperation, Operation);
LINK(Operation, Node);
// -- conditions --
LINK(True, Condition);
LINK(False, Condition);
LINK(Conjunction, Condition);
LINK(Negation, Condition);
LINK(Constraint, Condition);
LINK(ProvenanceExistenceCheck, AbstractExistenceCheck);
LINK(ExistenceCheck, AbstractExistenceCheck);
LINK(EmptinessCheck, Condition);
LINK(AbstractExistenceCheck, Condition);
LINK(Condition, Node);
// -- values --
LINK(SignedConstant, Constant);
LINK(UnsignedConstant, Constant);
LINK(FloatConstant, Constant);
LINK(Constant, Expression);
LINK(UndefValue, Expression);
LINK(TupleElement, Expression);
LINK(IntrinsicOperator, AbstractOperator);
LINK(UserDefinedOperator, AbstractOperator);
LINK(AbstractOperator, Expression);
LINK(AutoIncrement, Expression);
LINK(PackRecord, Expression);
LINK(SubroutineArgument, Expression);
LINK(Expression, Node);
// -- program --
LINK(Program, Node);
// -- relation
LINK(Relation, Node);
LINK(RelationReference, Node);
#undef LINK
/** The base case for all visitors -- if no more specific overload was defined */
virtual R visitNode(const RamNode& /*node*/, Params... /*args*/) {
return R();
}
};
/**
* A utility function visiting all nodes within the RAM fragment 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 RAM fragment 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 RamNode& root, RamVisitor<R, Ps...>& visitor, Args&... args) {
visitor(root, args...);
for (const RamNode* cur : root.getChildNodes()) {
if (cur != nullptr) {
visitDepthFirstPreOrder(*cur, visitor, args...);
}
}
}
/**
* A utility function visiting all nodes within the RAM fragments 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 RAM fragments 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 RamNode& root, RamVisitor<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 LambdaRamVisitor : public RamVisitor<void> {
std::function<R(const N&)> lambda;
LambdaRamVisitor(std::function<R(const N&)> lambda) : lambda(std::move(lambda)) {}
void visit(const RamNode& node) override {
if (const auto* n = dynamic_cast<const N*>(&node)) {
lambda(*n);
}
}
};
/**
* A factory function for creating LambdaRamVisitor instances.
*/
template <typename R, typename N>
LambdaRamVisitor<R, N> makeLambdaRamVisitor(const std::function<R(const N&)>& fun) {
return LambdaRamVisitor<R, N>(fun);
}
/**
* A type trait determining whether a given type is a visitor or not.
*/
template <typename T>
struct is_ram_visitor {
static constexpr size_t value = std::is_base_of<ram_visitor_tag, T>::value;
};
template <typename T>
struct is_ram_visitor<const T> : public is_ram_visitor<T> {};
template <typename T>
struct is_ram_visitor<T&> : public is_ram_visitor<T> {};
} // namespace detail
/**
* A utility function visiting all nodes within the RAM fragment 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 RAM fragment 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 RamNode& root, const std::function<R(const N&)>& fun) {
auto visitor = detail::makeLambdaRamVisitor(fun);
visitDepthFirst<void>(root, visitor);
}
/**
* A utility function visiting all nodes within the RAM fragment 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 RAM fragment 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_ram_visitor<Lambda>::value, void>::type visitDepthFirst(
const RamNode& root, const Lambda& fun) {
visitDepthFirst(root, std::function<R(const N&)>(fun));
}
} // end of namespace souffle