Returns whether this comprehension appears to be a macro implementation for map transformations. It is not exhaustive, so it is unsafe to use with custom comprehensions outside of the standard macros or hand crafted ASTs.
| 364 | // map transformations. It is not exhaustive, so it is unsafe to use with custom |
| 365 | // comprehensions outside of the standard macros or hand crafted ASTs. |
| 366 | bool IsOptimizableMapInsert(const cel::ComprehensionExpr* comprehension, |
| 367 | bool enable_comprehension_mutable_map) { |
| 368 | if (!enable_comprehension_mutable_map) { |
| 369 | return false; |
| 370 | } |
| 371 | if (comprehension->iter_var().empty() || comprehension->iter_var2().empty()) { |
| 372 | return false; |
| 373 | } |
| 374 | absl::string_view accu_var = comprehension->accu_var(); |
| 375 | if (accu_var.empty() || !comprehension->has_result() || |
| 376 | !comprehension->result().has_ident_expr() || |
| 377 | comprehension->result().ident_expr().name() != accu_var) { |
| 378 | return false; |
| 379 | } |
| 380 | if (!comprehension->accu_init().has_map_expr()) { |
| 381 | return false; |
| 382 | } |
| 383 | if (!comprehension->loop_step().has_call_expr()) { |
| 384 | return false; |
| 385 | } |
| 386 | const auto* call_expr = &comprehension->loop_step().call_expr(); |
| 387 | |
| 388 | if (call_expr->function() == cel::builtin::kTernary && |
| 389 | call_expr->args().size() == 3) { |
| 390 | if (!call_expr->args()[1].has_call_expr()) { |
| 391 | return false; |
| 392 | } |
| 393 | call_expr = &(call_expr->args()[1].call_expr()); |
| 394 | } |
| 395 | return call_expr->function() == "cel.@mapInsert" && |
| 396 | (call_expr->args().size() == 2 || call_expr->args().size() == 3) && |
| 397 | call_expr->args()[0].has_ident_expr() && |
| 398 | call_expr->args()[0].ident_expr().name() == accu_var; |
| 399 | } |
| 400 | |
| 401 | bool IsBind(const cel::ComprehensionExpr* comprehension) { |
| 402 | static constexpr absl::string_view kUnusedIterVar = "#unused"; |
no test coverage detected