|
19 | 19 |
|
20 | 20 | #include "absl/base/no_destructor.h" |
21 | 21 | #include "absl/functional/overload.h" |
| 22 | +#include "absl/log/absl_check.h" |
22 | 23 | #include "absl/types/variant.h" |
23 | 24 | #include "common/constant.h" |
24 | 25 |
|
@@ -201,4 +202,119 @@ Expr& Expr::operator=(const Expr& other) { |
201 | 202 |
|
202 | 203 | Expr::Expr(const Expr& other) { CloneImpl(other, *this); } |
203 | 204 |
|
| 205 | +namespace common_internal { |
| 206 | +struct ExprEraseTag {}; |
| 207 | +} // namespace common_internal |
| 208 | + |
| 209 | +namespace { |
| 210 | +void Expand(Expr** tail, Expr* cur) { |
| 211 | + static common_internal::ExprEraseTag tag; |
| 212 | + switch (cur->kind_case()) { |
| 213 | + case ExprKindCase::kSelectExpr: { |
| 214 | + SelectExpr& select = cur->mutable_select_expr(); |
| 215 | + if (select.has_operand()) { |
| 216 | + select.mutable_operand().SetNext(tag, *tail); |
| 217 | + *tail = &select.mutable_operand(); |
| 218 | + } |
| 219 | + break; |
| 220 | + } |
| 221 | + case ExprKindCase::kCallExpr: { |
| 222 | + CallExpr& call = cur->mutable_call_expr(); |
| 223 | + if (call.has_target()) { |
| 224 | + call.mutable_target().SetNext(tag, *tail); |
| 225 | + *tail = &call.mutable_target(); |
| 226 | + } |
| 227 | + for (auto& arg : call.mutable_args()) { |
| 228 | + arg.SetNext(tag, *tail); |
| 229 | + *tail = &arg; |
| 230 | + } |
| 231 | + break; |
| 232 | + } |
| 233 | + case ExprKindCase::kListExpr: { |
| 234 | + for (auto& arg : cur->mutable_list_expr().mutable_elements()) { |
| 235 | + arg.mutable_expr().SetNext(tag, *tail); |
| 236 | + *tail = &arg.mutable_expr(); |
| 237 | + } |
| 238 | + break; |
| 239 | + } |
| 240 | + case ExprKindCase::kStructExpr: { |
| 241 | + for (auto& field : cur->mutable_struct_expr().mutable_fields()) { |
| 242 | + field.mutable_value().SetNext(tag, *tail); |
| 243 | + *tail = &field.mutable_value(); |
| 244 | + } |
| 245 | + break; |
| 246 | + } |
| 247 | + case ExprKindCase::kMapExpr: { |
| 248 | + for (auto& entry : cur->mutable_map_expr().mutable_entries()) { |
| 249 | + entry.mutable_key().SetNext(tag, *tail); |
| 250 | + *tail = &entry.mutable_key(); |
| 251 | + entry.mutable_value().SetNext(tag, *tail); |
| 252 | + *tail = &entry.mutable_value(); |
| 253 | + } |
| 254 | + break; |
| 255 | + } |
| 256 | + case ExprKindCase::kComprehensionExpr: { |
| 257 | + if (cur->comprehension_expr().has_accu_init()) { |
| 258 | + cur->mutable_comprehension_expr().mutable_accu_init().SetNext(tag, |
| 259 | + *tail); |
| 260 | + *tail = &cur->mutable_comprehension_expr().mutable_accu_init(); |
| 261 | + } |
| 262 | + if (cur->comprehension_expr().has_iter_range()) { |
| 263 | + cur->mutable_comprehension_expr().mutable_iter_range().SetNext(tag, |
| 264 | + *tail); |
| 265 | + *tail = &cur->mutable_comprehension_expr().mutable_iter_range(); |
| 266 | + } |
| 267 | + if (cur->comprehension_expr().has_loop_condition()) { |
| 268 | + cur->mutable_comprehension_expr().mutable_loop_condition().SetNext( |
| 269 | + tag, *tail); |
| 270 | + *tail = &cur->mutable_comprehension_expr().mutable_loop_condition(); |
| 271 | + } |
| 272 | + if (cur->comprehension_expr().has_loop_step()) { |
| 273 | + cur->mutable_comprehension_expr().mutable_loop_step().SetNext(tag, |
| 274 | + *tail); |
| 275 | + *tail = &cur->mutable_comprehension_expr().mutable_loop_step(); |
| 276 | + } |
| 277 | + if (cur->comprehension_expr().has_result()) { |
| 278 | + cur->mutable_comprehension_expr().mutable_result().SetNext(tag, *tail); |
| 279 | + *tail = &cur->mutable_comprehension_expr().mutable_result(); |
| 280 | + } |
| 281 | + break; |
| 282 | + } |
| 283 | + default: |
| 284 | + // Leaf node, nothing to expand. |
| 285 | + // Also a fallback in case we add a new node type. |
| 286 | + // Note: already in the deleter list so we can't delete now, will be |
| 287 | + // deleted after ordering the AST. |
| 288 | + break; |
| 289 | + } |
| 290 | +} |
| 291 | +} // namespace |
| 292 | + |
| 293 | +void Expr::FlattenedErase() { |
| 294 | + // High level idea is to build a topological ordering of the AST, then erase |
| 295 | + // leaf to root. |
| 296 | + this->u_.next = nullptr; |
| 297 | + Expr* prev_tail = nullptr; |
| 298 | + Expr* tail = this; |
| 299 | + |
| 300 | + while (tail != prev_tail) { |
| 301 | + Expr* next_prev_tail = tail; |
| 302 | + Expr* expand_ptr = tail; |
| 303 | + while (expand_ptr != prev_tail) { |
| 304 | + ABSL_DCHECK(expand_ptr != nullptr); // Linked list is broken or changed. |
| 305 | + Expr* next_expand_ptr = expand_ptr->u_.next; |
| 306 | + Expand(&tail, expand_ptr); |
| 307 | + expand_ptr = next_expand_ptr; |
| 308 | + } |
| 309 | + prev_tail = next_prev_tail; |
| 310 | + } |
| 311 | + |
| 312 | + Expr* node = tail; |
| 313 | + while (node != nullptr) { |
| 314 | + Expr* next = node->u_.next; |
| 315 | + node->Clear(); |
| 316 | + node = next; |
| 317 | + } |
| 318 | +} |
| 319 | + |
204 | 320 | } // namespace cel |
0 commit comments