/proc/self/cwd/common/ast_traverse.cc
Line | Count | Source |
1 | | // Copyright 2018 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include "common/ast_traverse.h" |
16 | | |
17 | | #include <memory> |
18 | | #include <stack> |
19 | | |
20 | | #include "absl/types/variant.h" |
21 | | #include "common/ast_visitor.h" |
22 | | #include "common/constant.h" |
23 | | #include "common/expr.h" |
24 | | |
25 | | namespace cel { |
26 | | |
27 | | namespace { |
28 | | |
29 | | struct ArgRecord { |
30 | | // Not null. |
31 | | const Expr* expr; |
32 | | |
33 | | // For records that are direct arguments to call, we need to call |
34 | | // the CallArg visitor immediately after the argument is evaluated. |
35 | | const Expr* calling_expr; |
36 | | int call_arg; |
37 | | }; |
38 | | |
39 | | struct ComprehensionRecord { |
40 | | // Not null. |
41 | | const Expr* expr; |
42 | | |
43 | | const ComprehensionExpr* comprehension; |
44 | | const Expr* comprehension_expr; |
45 | | ComprehensionArg comprehension_arg; |
46 | | bool use_comprehension_callbacks; |
47 | | }; |
48 | | |
49 | | struct ExprRecord { |
50 | | // Not null. |
51 | | const Expr* expr; |
52 | | }; |
53 | | |
54 | | using StackRecordKind = |
55 | | std::variant<ExprRecord, ArgRecord, ComprehensionRecord>; |
56 | | |
57 | | struct StackRecord { |
58 | | public: |
59 | | static constexpr int kTarget = -2; |
60 | | |
61 | 524k | explicit StackRecord(const Expr* e) { |
62 | 524k | ExprRecord record; |
63 | 524k | record.expr = e; |
64 | 524k | record_variant = record; |
65 | 524k | } |
66 | | |
67 | | StackRecord(const Expr* e, const ComprehensionExpr* comprehension, |
68 | | const Expr* comprehension_expr, |
69 | | ComprehensionArg comprehension_arg, |
70 | 100k | bool use_comprehension_callbacks) { |
71 | 100k | if (use_comprehension_callbacks) { |
72 | 100k | ComprehensionRecord record; |
73 | 100k | record.expr = e; |
74 | 100k | record.comprehension = comprehension; |
75 | 100k | record.comprehension_expr = comprehension_expr; |
76 | 100k | record.comprehension_arg = comprehension_arg; |
77 | 100k | record.use_comprehension_callbacks = use_comprehension_callbacks; |
78 | 100k | record_variant = record; |
79 | 100k | return; |
80 | 100k | } |
81 | 0 | ArgRecord record; |
82 | 0 | record.expr = e; |
83 | 0 | record.calling_expr = comprehension_expr; |
84 | 0 | record.call_arg = comprehension_arg; |
85 | 0 | record_variant = record; |
86 | 0 | } |
87 | | |
88 | 252k | StackRecord(const Expr* e, const Expr* call, int argnum) { |
89 | 252k | ArgRecord record; |
90 | 252k | record.expr = e; |
91 | 252k | record.calling_expr = call; |
92 | 252k | record.call_arg = argnum; |
93 | 252k | record_variant = record; |
94 | 252k | } |
95 | | StackRecordKind record_variant; |
96 | | bool visited = false; |
97 | | }; |
98 | | |
99 | | struct PreVisitor { |
100 | 524k | void operator()(const ExprRecord& record) { |
101 | 524k | const Expr* expr = record.expr; |
102 | 524k | visitor->PreVisitExpr(*expr); |
103 | 524k | if (expr->has_select_expr()) { |
104 | 19.4k | visitor->PreVisitSelect(*expr, expr->select_expr()); |
105 | 504k | } else if (expr->has_call_expr()) { |
106 | 131k | visitor->PreVisitCall(*expr, expr->call_expr()); |
107 | 373k | } else if (expr->has_comprehension_expr()) { |
108 | 20.1k | visitor->PreVisitComprehension(*expr, expr->comprehension_expr()); |
109 | 353k | } else { |
110 | | // No pre-visit action. |
111 | 353k | } |
112 | 524k | } |
113 | | |
114 | | // Do nothing for Arg variant. |
115 | 252k | void operator()(const ArgRecord&) {} |
116 | | |
117 | 100k | void operator()(const ComprehensionRecord& record) { |
118 | 100k | visitor->PreVisitComprehensionSubexpression(*record.comprehension_expr, |
119 | 100k | *record.comprehension, |
120 | 100k | record.comprehension_arg); |
121 | 100k | } |
122 | | |
123 | | AstVisitor* visitor; |
124 | | }; |
125 | | |
126 | 877k | void PreVisit(const StackRecord& record, AstVisitor* visitor) { |
127 | 877k | absl::visit(PreVisitor{visitor}, record.record_variant); |
128 | 877k | } |
129 | | |
130 | | struct PostVisitor { |
131 | 524k | void operator()(const ExprRecord& record) { |
132 | 524k | const Expr* expr = record.expr; |
133 | 524k | struct { |
134 | 524k | AstVisitor* visitor; |
135 | 524k | const Expr* expr; |
136 | 524k | void operator()(const Constant& constant) { |
137 | 169k | visitor->PostVisitConst(*expr, expr->const_expr()); |
138 | 169k | } |
139 | 524k | void operator()(const IdentExpr& ident) { |
140 | 112k | visitor->PostVisitIdent(*expr, expr->ident_expr()); |
141 | 112k | } |
142 | 524k | void operator()(const SelectExpr& select) { |
143 | 19.4k | visitor->PostVisitSelect(*expr, expr->select_expr()); |
144 | 19.4k | } |
145 | 524k | void operator()(const CallExpr& call) { |
146 | 131k | visitor->PostVisitCall(*expr, expr->call_expr()); |
147 | 131k | } |
148 | 524k | void operator()(const ListExpr& create_list) { |
149 | 63.0k | visitor->PostVisitList(*expr, expr->list_expr()); |
150 | 63.0k | } |
151 | 524k | void operator()(const StructExpr& create_struct) { |
152 | 3.78k | visitor->PostVisitStruct(*expr, expr->struct_expr()); |
153 | 3.78k | } |
154 | 524k | void operator()(const MapExpr& map_expr) { |
155 | 4.53k | visitor->PostVisitMap(*expr, expr->map_expr()); |
156 | 4.53k | } |
157 | 524k | void operator()(const ComprehensionExpr& comprehension) { |
158 | 20.1k | visitor->PostVisitComprehension(*expr, expr->comprehension_expr()); |
159 | 20.1k | } |
160 | 524k | void operator()(const UnspecifiedExpr&) {} |
161 | 524k | } handler{visitor, record.expr}; |
162 | 524k | absl::visit(handler, record.expr->kind()); |
163 | | |
164 | 524k | visitor->PostVisitExpr(*expr); |
165 | 524k | } |
166 | | |
167 | 252k | void operator()(const ArgRecord& record) { |
168 | 252k | if (record.call_arg == StackRecord::kTarget) { |
169 | 804 | visitor->PostVisitTarget(*record.calling_expr); |
170 | 251k | } else { |
171 | 251k | visitor->PostVisitArg(*record.calling_expr, record.call_arg); |
172 | 251k | } |
173 | 252k | } |
174 | | |
175 | 100k | void operator()(const ComprehensionRecord& record) { |
176 | 100k | visitor->PostVisitComprehensionSubexpression(*record.comprehension_expr, |
177 | 100k | *record.comprehension, |
178 | 100k | record.comprehension_arg); |
179 | 100k | } |
180 | | |
181 | | AstVisitor* visitor; |
182 | | }; |
183 | | |
184 | 877k | void PostVisit(const StackRecord& record, AstVisitor* visitor) { |
185 | 877k | absl::visit(PostVisitor{visitor}, record.record_variant); |
186 | 877k | } |
187 | | |
188 | | void PushSelectDeps(const SelectExpr* select_expr, |
189 | 19.4k | std::stack<StackRecord>* stack) { |
190 | 19.4k | if (select_expr->has_operand()) { |
191 | 19.4k | stack->push(StackRecord(&select_expr->operand())); |
192 | 19.4k | } |
193 | 19.4k | } |
194 | | |
195 | | void PushCallDeps(const CallExpr* call_expr, const Expr* expr, |
196 | 131k | std::stack<StackRecord>* stack) { |
197 | 131k | const int arg_size = call_expr->args().size(); |
198 | | // Our contract is that we visit arguments in order. To do that, we need |
199 | | // to push them onto the stack in reverse order. |
200 | 383k | for (int i = arg_size - 1; i >= 0; --i) { |
201 | 251k | stack->push(StackRecord(&call_expr->args()[i], expr, i)); |
202 | 251k | } |
203 | | // Are we receiver-style? |
204 | 131k | if (call_expr->has_target()) { |
205 | 804 | stack->push(StackRecord(&call_expr->target(), expr, StackRecord::kTarget)); |
206 | 804 | } |
207 | 131k | } |
208 | | |
209 | 63.0k | void PushListDeps(const ListExpr* list_expr, std::stack<StackRecord>* stack) { |
210 | 63.0k | const auto& elements = list_expr->elements(); |
211 | 182k | for (auto it = elements.rbegin(); it != elements.rend(); ++it) { |
212 | 119k | const auto& element = *it; |
213 | 119k | stack->push(StackRecord(&element.expr())); |
214 | 119k | } |
215 | 63.0k | } |
216 | | |
217 | | void PushStructDeps(const StructExpr* struct_expr, |
218 | 3.78k | std::stack<StackRecord>* stack) { |
219 | 3.78k | const auto& entries = struct_expr->fields(); |
220 | 6.83k | for (auto it = entries.rbegin(); it != entries.rend(); ++it) { |
221 | 3.05k | const auto& entry = *it; |
222 | | // The contract is to visit key, then value. So put them on the stack |
223 | | // in the opposite order. |
224 | 3.05k | if (entry.has_value()) { |
225 | 3.05k | stack->push(StackRecord(&entry.value())); |
226 | 3.05k | } |
227 | 3.05k | } |
228 | 3.78k | } |
229 | | |
230 | 4.53k | void PushMapDeps(const MapExpr* map_expr, std::stack<StackRecord>* stack) { |
231 | 4.53k | const auto& entries = map_expr->entries(); |
232 | 9.40k | for (auto it = entries.rbegin(); it != entries.rend(); ++it) { |
233 | 4.87k | const auto& entry = *it; |
234 | | // The contract is to visit key, then value. So put them on the stack |
235 | | // in the opposite order. |
236 | 4.87k | if (entry.has_value()) { |
237 | 4.87k | stack->push(StackRecord(&entry.value())); |
238 | 4.87k | } |
239 | | // The contract is to visit key, then value. So put them on the stack |
240 | | // in the opposite order. |
241 | 4.87k | if (entry.has_key()) { |
242 | 4.87k | stack->push(StackRecord(&entry.key())); |
243 | 4.87k | } |
244 | 4.87k | } |
245 | 4.53k | } |
246 | | |
247 | | void PushComprehensionDeps(const ComprehensionExpr* c, const Expr* expr, |
248 | | std::stack<StackRecord>* stack, |
249 | 20.1k | bool use_comprehension_callbacks) { |
250 | 20.1k | StackRecord iter_range(&c->iter_range(), c, expr, ITER_RANGE, |
251 | 20.1k | use_comprehension_callbacks); |
252 | 20.1k | StackRecord accu_init(&c->accu_init(), c, expr, ACCU_INIT, |
253 | 20.1k | use_comprehension_callbacks); |
254 | 20.1k | StackRecord loop_condition(&c->loop_condition(), c, expr, LOOP_CONDITION, |
255 | 20.1k | use_comprehension_callbacks); |
256 | 20.1k | StackRecord loop_step(&c->loop_step(), c, expr, LOOP_STEP, |
257 | 20.1k | use_comprehension_callbacks); |
258 | 20.1k | StackRecord result(&c->result(), c, expr, RESULT, |
259 | 20.1k | use_comprehension_callbacks); |
260 | | // Push them in reverse order. |
261 | 20.1k | stack->push(result); |
262 | 20.1k | stack->push(loop_step); |
263 | 20.1k | stack->push(loop_condition); |
264 | 20.1k | stack->push(accu_init); |
265 | 20.1k | stack->push(iter_range); |
266 | 20.1k | } |
267 | | |
268 | | struct PushDepsVisitor { |
269 | 524k | void operator()(const ExprRecord& record) { |
270 | 524k | struct { |
271 | 524k | std::stack<StackRecord>& stack; |
272 | 524k | const TraversalOptions& options; |
273 | 524k | const ExprRecord& record; |
274 | 524k | void operator()(const Constant& constant) {} |
275 | 524k | void operator()(const IdentExpr& ident) {} |
276 | 524k | void operator()(const SelectExpr& select) { |
277 | 19.4k | PushSelectDeps(&record.expr->select_expr(), &stack); |
278 | 19.4k | } |
279 | 524k | void operator()(const CallExpr& call) { |
280 | 131k | PushCallDeps(&record.expr->call_expr(), record.expr, &stack); |
281 | 131k | } |
282 | 524k | void operator()(const ListExpr& create_list) { |
283 | 63.0k | PushListDeps(&record.expr->list_expr(), &stack); |
284 | 63.0k | } |
285 | 524k | void operator()(const StructExpr& create_struct) { |
286 | 3.78k | PushStructDeps(&record.expr->struct_expr(), &stack); |
287 | 3.78k | } |
288 | 524k | void operator()(const MapExpr& map_expr) { |
289 | 4.53k | PushMapDeps(&record.expr->map_expr(), &stack); |
290 | 4.53k | } |
291 | 524k | void operator()(const ComprehensionExpr& comprehension) { |
292 | 20.1k | PushComprehensionDeps(&record.expr->comprehension_expr(), record.expr, |
293 | 20.1k | &stack, options.use_comprehension_callbacks); |
294 | 20.1k | } |
295 | 524k | void operator()(const UnspecifiedExpr&) {} |
296 | 524k | } handler{stack, options, record}; |
297 | 524k | absl::visit(handler, record.expr->kind()); |
298 | 524k | } |
299 | | |
300 | 252k | void operator()(const ArgRecord& record) { |
301 | 252k | stack.push(StackRecord(record.expr)); |
302 | 252k | } |
303 | | |
304 | 100k | void operator()(const ComprehensionRecord& record) { |
305 | 100k | stack.push(StackRecord(record.expr)); |
306 | 100k | } |
307 | | |
308 | | std::stack<StackRecord>& stack; |
309 | | const TraversalOptions& options; |
310 | | }; |
311 | | |
312 | | void PushDependencies(const StackRecord& record, std::stack<StackRecord>& stack, |
313 | 877k | const TraversalOptions& options) { |
314 | 877k | absl::visit(PushDepsVisitor{stack, options}, record.record_variant); |
315 | 877k | } |
316 | | |
317 | | } // namespace |
318 | | |
319 | | namespace common_internal { |
320 | | struct AstTraversalState { |
321 | | std::stack<StackRecord> stack; |
322 | | }; |
323 | | } // namespace common_internal |
324 | | |
325 | | AstTraversal AstTraversal::Create(const cel::Expr& ast, |
326 | 0 | const TraversalOptions& options) { |
327 | 0 | AstTraversal instance(options); |
328 | 0 | instance.state_ = std::make_unique<common_internal::AstTraversalState>(); |
329 | 0 | instance.state_->stack.push(StackRecord(&ast)); |
330 | 0 | return instance; |
331 | 0 | } |
332 | | |
333 | 0 | AstTraversal::AstTraversal(TraversalOptions options) : options_(options) {} |
334 | | |
335 | 0 | AstTraversal::~AstTraversal() = default; |
336 | | |
337 | 0 | bool AstTraversal::Step(AstVisitor& visitor) { |
338 | 0 | if (IsDone()) { |
339 | 0 | return false; |
340 | 0 | } |
341 | 0 | auto& stack = state_->stack; |
342 | 0 | StackRecord& record = stack.top(); |
343 | 0 | if (!record.visited) { |
344 | 0 | PreVisit(record, &visitor); |
345 | 0 | PushDependencies(record, stack, options_); |
346 | 0 | record.visited = true; |
347 | 0 | } else { |
348 | 0 | PostVisit(record, &visitor); |
349 | 0 | stack.pop(); |
350 | 0 | } |
351 | |
|
352 | 0 | return !stack.empty(); |
353 | 0 | } |
354 | | |
355 | 0 | bool AstTraversal::IsDone() { |
356 | 0 | return state_ == nullptr || state_->stack.empty(); |
357 | 0 | } |
358 | | |
359 | | void AstTraverse(const Expr& expr, AstVisitor& visitor, |
360 | 19.6k | TraversalOptions options) { |
361 | 19.6k | std::stack<StackRecord> stack; |
362 | 19.6k | stack.push(StackRecord(&expr)); |
363 | | |
364 | 1.77M | while (!stack.empty()) { |
365 | 1.75M | StackRecord& record = stack.top(); |
366 | 1.75M | if (!record.visited) { |
367 | 877k | PreVisit(record, &visitor); |
368 | 877k | PushDependencies(record, stack, options); |
369 | 877k | record.visited = true; |
370 | 877k | } else { |
371 | 877k | PostVisit(record, &visitor); |
372 | 877k | stack.pop(); |
373 | 877k | } |
374 | 1.75M | } |
375 | 19.6k | } |
376 | | |
377 | | } // namespace cel |