MCPcopy Create free account
hub / github.com/cel-expr/cel-cpp / IsOptimizableMapInsert

Function IsOptimizableMapInsert

eval/compiler/flat_expr_builder.cc:366–399  ·  view source on GitHub ↗

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.

Source from the content-addressed store, hash-verified

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.
366bool 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
401bool IsBind(const cel::ComprehensionExpr* comprehension) {
402 static constexpr absl::string_view kUnusedIterVar = "#unused";

Callers 1

PreVisitComprehensionMethod · 0.85

Calls 8

has_resultMethod · 0.80
has_ident_exprMethod · 0.80
has_map_exprMethod · 0.80
has_call_exprMethod · 0.80
argsMethod · 0.80
emptyMethod · 0.45
nameMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected