Coverage Report

Created: 2026-11-21 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
489k
  explicit StackRecord(const Expr* e) {
62
489k
    ExprRecord record;
63
489k
    record.expr = e;
64
489k
    record_variant = record;
65
489k
  }
66
67
  StackRecord(const Expr* e, const ComprehensionExpr* comprehension,
68
              const Expr* comprehension_expr,
69
              ComprehensionArg comprehension_arg,
70
88.8k
              bool use_comprehension_callbacks) {
71
88.8k
    if (use_comprehension_callbacks) {
72
88.8k
      ComprehensionRecord record;
73
88.8k
      record.expr = e;
74
88.8k
      record.comprehension = comprehension;
75
88.8k
      record.comprehension_expr = comprehension_expr;
76
88.8k
      record.comprehension_arg = comprehension_arg;
77
88.8k
      record.use_comprehension_callbacks = use_comprehension_callbacks;
78
88.8k
      record_variant = record;
79
88.8k
      return;
80
88.8k
    }
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
240k
  StackRecord(const Expr* e, const Expr* call, int argnum) {
89
240k
    ArgRecord record;
90
240k
    record.expr = e;
91
240k
    record.calling_expr = call;
92
240k
    record.call_arg = argnum;
93
240k
    record_variant = record;
94
240k
  }
95
  StackRecordKind record_variant;
96
  bool visited = false;
97
};
98
99
struct PreVisitor {
100
489k
  void operator()(const ExprRecord& record) {
101
489k
    const Expr* expr = record.expr;
102
489k
    visitor->PreVisitExpr(*expr);
103
489k
    if (expr->has_select_expr()) {
104
18.7k
      visitor->PreVisitSelect(*expr, expr->select_expr());
105
470k
    } else if (expr->has_call_expr()) {
106
125k
      visitor->PreVisitCall(*expr, expr->call_expr());
107
344k
    } else if (expr->has_comprehension_expr()) {
108
17.7k
      visitor->PreVisitComprehension(*expr, expr->comprehension_expr());
109
327k
    } else {
110
      // No pre-visit action.
111
327k
    }
112
489k
  }
113
114
  // Do nothing for Arg variant.
115
240k
  void operator()(const ArgRecord&) {}
116
117
88.8k
  void operator()(const ComprehensionRecord& record) {
118
88.8k
    visitor->PreVisitComprehensionSubexpression(*record.comprehension_expr,
119
88.8k
                                                *record.comprehension,
120
88.8k
                                                record.comprehension_arg);
121
88.8k
  }
122
123
  AstVisitor* visitor;
124
};
125
126
819k
void PreVisit(const StackRecord& record, AstVisitor* visitor) {
127
819k
  absl::visit(PreVisitor{visitor}, record.record_variant);
128
819k
}
129
130
struct PostVisitor {
131
489k
  void operator()(const ExprRecord& record) {
132
489k
    const Expr* expr = record.expr;
133
489k
    struct {
134
489k
      AstVisitor* visitor;
135
489k
      const Expr* expr;
136
489k
      void operator()(const Constant& constant) {
137
159k
        visitor->PostVisitConst(*expr, expr->const_expr());
138
159k
      }
139
489k
      void operator()(const IdentExpr& ident) {
140
104k
        visitor->PostVisitIdent(*expr, expr->ident_expr());
141
104k
      }
142
489k
      void operator()(const SelectExpr& select) {
143
18.7k
        visitor->PostVisitSelect(*expr, expr->select_expr());
144
18.7k
      }
145
489k
      void operator()(const CallExpr& call) {
146
125k
        visitor->PostVisitCall(*expr, expr->call_expr());
147
125k
      }
148
489k
      void operator()(const ListExpr& create_list) {
149
55.6k
        visitor->PostVisitList(*expr, expr->list_expr());
150
55.6k
      }
151
489k
      void operator()(const StructExpr& create_struct) {
152
3.42k
        visitor->PostVisitStruct(*expr, expr->struct_expr());
153
3.42k
      }
154
489k
      void operator()(const MapExpr& map_expr) {
155
4.26k
        visitor->PostVisitMap(*expr, expr->map_expr());
156
4.26k
      }
157
489k
      void operator()(const ComprehensionExpr& comprehension) {
158
17.7k
        visitor->PostVisitComprehension(*expr, expr->comprehension_expr());
159
17.7k
      }
160
489k
      void operator()(const UnspecifiedExpr&) {}
161
489k
    } handler{visitor, record.expr};
162
489k
    absl::visit(handler, record.expr->kind());
163
164
489k
    visitor->PostVisitExpr(*expr);
165
489k
  }
166
167
240k
  void operator()(const ArgRecord& record) {
168
240k
    if (record.call_arg == StackRecord::kTarget) {
169
816
      visitor->PostVisitTarget(*record.calling_expr);
170
239k
    } else {
171
239k
      visitor->PostVisitArg(*record.calling_expr, record.call_arg);
172
239k
    }
173
240k
  }
174
175
88.8k
  void operator()(const ComprehensionRecord& record) {
176
88.8k
    visitor->PostVisitComprehensionSubexpression(*record.comprehension_expr,
177
88.8k
                                                 *record.comprehension,
178
88.8k
                                                 record.comprehension_arg);
179
88.8k
  }
180
181
  AstVisitor* visitor;
182
};
183
184
819k
void PostVisit(const StackRecord& record, AstVisitor* visitor) {
185
819k
  absl::visit(PostVisitor{visitor}, record.record_variant);
186
819k
}
187
188
void PushSelectDeps(const SelectExpr* select_expr,
189
18.7k
                    std::stack<StackRecord>* stack) {
190
18.7k
  if (select_expr->has_operand()) {
191
18.7k
    stack->push(StackRecord(&select_expr->operand()));
192
18.7k
  }
193
18.7k
}
194
195
void PushCallDeps(const CallExpr* call_expr, const Expr* expr,
196
125k
                  std::stack<StackRecord>* stack) {
197
125k
  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
365k
  for (int i = arg_size - 1; i >= 0; --i) {
201
239k
    stack->push(StackRecord(&call_expr->args()[i], expr, i));
202
239k
  }
203
  // Are we receiver-style?
204
125k
  if (call_expr->has_target()) {
205
816
    stack->push(StackRecord(&call_expr->target(), expr, StackRecord::kTarget));
206
816
  }
207
125k
}
208
209
55.6k
void PushListDeps(const ListExpr* list_expr, std::stack<StackRecord>* stack) {
210
55.6k
  const auto& elements = list_expr->elements();
211
166k
  for (auto it = elements.rbegin(); it != elements.rend(); ++it) {
212
110k
    const auto& element = *it;
213
110k
    stack->push(StackRecord(&element.expr()));
214
110k
  }
215
55.6k
}
216
217
void PushStructDeps(const StructExpr* struct_expr,
218
3.42k
                    std::stack<StackRecord>* stack) {
219
3.42k
  const auto& entries = struct_expr->fields();
220
5.97k
  for (auto it = entries.rbegin(); it != entries.rend(); ++it) {
221
2.55k
    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
2.55k
    if (entry.has_value()) {
225
2.55k
      stack->push(StackRecord(&entry.value()));
226
2.55k
    }
227
2.55k
  }
228
3.42k
}
229
230
4.26k
void PushMapDeps(const MapExpr* map_expr, std::stack<StackRecord>* stack) {
231
4.26k
  const auto& entries = map_expr->entries();
232
9.25k
  for (auto it = entries.rbegin(); it != entries.rend(); ++it) {
233
4.98k
    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.98k
    if (entry.has_value()) {
237
4.98k
      stack->push(StackRecord(&entry.value()));
238
4.98k
    }
239
    // The contract is to visit key, then value.  So put them on the stack
240
    // in the opposite order.
241
4.98k
    if (entry.has_key()) {
242
4.98k
      stack->push(StackRecord(&entry.key()));
243
4.98k
    }
244
4.98k
  }
245
4.26k
}
246
247
void PushComprehensionDeps(const ComprehensionExpr* c, const Expr* expr,
248
                           std::stack<StackRecord>* stack,
249
17.7k
                           bool use_comprehension_callbacks) {
250
17.7k
  StackRecord iter_range(&c->iter_range(), c, expr, ITER_RANGE,
251
17.7k
                         use_comprehension_callbacks);
252
17.7k
  StackRecord accu_init(&c->accu_init(), c, expr, ACCU_INIT,
253
17.7k
                        use_comprehension_callbacks);
254
17.7k
  StackRecord loop_condition(&c->loop_condition(), c, expr, LOOP_CONDITION,
255
17.7k
                             use_comprehension_callbacks);
256
17.7k
  StackRecord loop_step(&c->loop_step(), c, expr, LOOP_STEP,
257
17.7k
                        use_comprehension_callbacks);
258
17.7k
  StackRecord result(&c->result(), c, expr, RESULT,
259
17.7k
                     use_comprehension_callbacks);
260
  // Push them in reverse order.
261
17.7k
  stack->push(result);
262
17.7k
  stack->push(loop_step);
263
17.7k
  stack->push(loop_condition);
264
17.7k
  stack->push(accu_init);
265
17.7k
  stack->push(iter_range);
266
17.7k
}
267
268
struct PushDepsVisitor {
269
489k
  void operator()(const ExprRecord& record) {
270
489k
    struct {
271
489k
      std::stack<StackRecord>& stack;
272
489k
      const TraversalOptions& options;
273
489k
      const ExprRecord& record;
274
489k
      void operator()(const Constant& constant) {}
275
489k
      void operator()(const IdentExpr& ident) {}
276
489k
      void operator()(const SelectExpr& select) {
277
18.7k
        PushSelectDeps(&record.expr->select_expr(), &stack);
278
18.7k
      }
279
489k
      void operator()(const CallExpr& call) {
280
125k
        PushCallDeps(&record.expr->call_expr(), record.expr, &stack);
281
125k
      }
282
489k
      void operator()(const ListExpr& create_list) {
283
55.6k
        PushListDeps(&record.expr->list_expr(), &stack);
284
55.6k
      }
285
489k
      void operator()(const StructExpr& create_struct) {
286
3.42k
        PushStructDeps(&record.expr->struct_expr(), &stack);
287
3.42k
      }
288
489k
      void operator()(const MapExpr& map_expr) {
289
4.26k
        PushMapDeps(&record.expr->map_expr(), &stack);
290
4.26k
      }
291
489k
      void operator()(const ComprehensionExpr& comprehension) {
292
17.7k
        PushComprehensionDeps(&record.expr->comprehension_expr(), record.expr,
293
17.7k
                              &stack, options.use_comprehension_callbacks);
294
17.7k
      }
295
489k
      void operator()(const UnspecifiedExpr&) {}
296
489k
    } handler{stack, options, record};
297
489k
    absl::visit(handler, record.expr->kind());
298
489k
  }
299
300
240k
  void operator()(const ArgRecord& record) {
301
240k
    stack.push(StackRecord(record.expr));
302
240k
  }
303
304
88.8k
  void operator()(const ComprehensionRecord& record) {
305
88.8k
    stack.push(StackRecord(record.expr));
306
88.8k
  }
307
308
  std::stack<StackRecord>& stack;
309
  const TraversalOptions& options;
310
};
311
312
void PushDependencies(const StackRecord& record, std::stack<StackRecord>& stack,
313
819k
                      const TraversalOptions& options) {
314
819k
  absl::visit(PushDepsVisitor{stack, options}, record.record_variant);
315
819k
}
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
17.7k
                 TraversalOptions options) {
361
17.7k
  std::stack<StackRecord> stack;
362
17.7k
  stack.push(StackRecord(&expr));
363
364
1.65M
  while (!stack.empty()) {
365
1.63M
    StackRecord& record = stack.top();
366
1.63M
    if (!record.visited) {
367
819k
      PreVisit(record, &visitor);
368
819k
      PushDependencies(record, stack, options);
369
819k
      record.visited = true;
370
819k
    } else {
371
819k
      PostVisit(record, &visitor);
372
819k
      stack.pop();
373
819k
    }
374
1.63M
  }
375
17.7k
}
376
377
}  // namespace cel