Coverage Report

Created: 2026-05-27 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/eval/compiler/flat_expr_builder.h
Line
Count
Source
1
/*
2
 * Copyright 2021 Google LLC
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
#ifndef THIRD_PARTY_CEL_CPP_EVAL_COMPILER_FLAT_EXPR_BUILDER_H_
18
#define THIRD_PARTY_CEL_CPP_EVAL_COMPILER_FLAT_EXPR_BUILDER_H_
19
20
#include <memory>
21
#include <string>
22
#include <utility>
23
#include <vector>
24
25
#include "absl/base/nullability.h"
26
#include "absl/status/statusor.h"
27
#include "absl/strings/string_view.h"
28
#include "base/ast.h"
29
#include "base/type_provider.h"
30
#include "eval/compiler/flat_expr_builder_extensions.h"
31
#include "eval/eval/evaluator_core.h"
32
#include "runtime/function_registry.h"
33
#include "runtime/internal/runtime_env.h"
34
#include "runtime/runtime_issue.h"
35
#include "runtime/runtime_options.h"
36
#include "runtime/type_registry.h"
37
38
namespace google::api::expr::runtime {
39
40
// CelExpressionBuilder implementation.
41
// Builds instances of CelExpressionFlatImpl.
42
class FlatExprBuilder {
43
 public:
44
  FlatExprBuilder(
45
      absl_nonnull std::shared_ptr<const cel::runtime_internal::RuntimeEnv> env,
46
      const cel::RuntimeOptions& options, bool use_legacy_type_provider = false)
47
14.5k
      : env_(std::move(env)),
48
14.5k
        options_(options),
49
14.5k
        container_(options.container),
50
14.5k
        function_registry_(env_->function_registry),
51
14.5k
        type_registry_(env_->type_registry),
52
14.5k
        use_legacy_type_provider_(use_legacy_type_provider) {}
53
54
14.5k
  void AddAstTransform(std::unique_ptr<AstTransform> transform) {
55
14.5k
    ast_transforms_.push_back(std::move(transform));
56
14.5k
  }
57
58
0
  void AddProgramOptimizer(ProgramOptimizerFactory optimizer) {
59
0
    program_optimizers_.push_back(std::move(optimizer));
60
0
  }
61
62
0
  void set_container(std::string container) {
63
0
    container_ = std::move(container);
64
0
  }
65
66
0
  absl::string_view container() const { return container_; }
67
68
  // TODO(uncreated-issue/45): Add overload for cref AST. At the moment, all the users
69
  // can pass ownership of a freshly converted AST.
70
  absl::StatusOr<FlatExpression> CreateExpressionImpl(
71
      std::unique_ptr<cel::Ast> ast,
72
      std::vector<cel::RuntimeIssue>* issues) const;
73
74
0
  const cel::runtime_internal::RuntimeEnv& env() const { return *env_; }
75
76
9.92k
  const cel::RuntimeOptions& options() const { return options_; }
77
78
  // Called by `cel::extensions::EnableOptionalTypes` to indicate that special
79
  // `optional_type` handling is needed.
80
0
  void enable_optional_types() { enable_optional_types_ = true; }
81
82
0
  bool optional_types_enabled() const { return enable_optional_types_; }
83
84
 private:
85
  const cel::TypeProvider& GetTypeProvider() const;
86
87
  const absl_nonnull std::shared_ptr<const cel::runtime_internal::RuntimeEnv>
88
      env_;
89
90
  cel::RuntimeOptions options_;
91
  std::string container_;
92
  bool enable_optional_types_ = false;
93
  // TODO(uncreated-issue/45): evaluate whether we should use a shared_ptr here to
94
  // allow built expressions to keep the registries alive.
95
  const cel::FunctionRegistry& function_registry_;
96
  const cel::TypeRegistry& type_registry_;
97
  bool use_legacy_type_provider_;
98
  std::vector<std::unique_ptr<AstTransform>> ast_transforms_;
99
  std::vector<ProgramOptimizerFactory> program_optimizers_;
100
};
101
102
}  // namespace google::api::expr::runtime
103
104
#endif  // THIRD_PARTY_CEL_CPP_EVAL_COMPILER_FLAT_EXPR_BUILDER_H_