/proc/self/cwd/eval/eval/direct_expression_step.h
Line | Count | Source |
1 | | // Copyright 2024 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 | | #ifndef THIRD_PARTY_CEL_CPP_EVAL_EVAL_DIRECT_EXPRESSION_STEP_H_ |
15 | | #define THIRD_PARTY_CEL_CPP_EVAL_EVAL_DIRECT_EXPRESSION_STEP_H_ |
16 | | |
17 | | #include <cstdint> |
18 | | #include <memory> |
19 | | #include <utility> |
20 | | #include <vector> |
21 | | |
22 | | #include "absl/status/status.h" |
23 | | #include "absl/types/optional.h" |
24 | | #include "common/native_type.h" |
25 | | #include "common/value.h" |
26 | | #include "eval/eval/attribute_trail.h" |
27 | | #include "eval/eval/evaluator_core.h" |
28 | | |
29 | | namespace google::api::expr::runtime { |
30 | | |
31 | | // Represents a directly evaluated CEL expression. |
32 | | // |
33 | | // Subexpressions assign to values on the C++ program stack and call their |
34 | | // dependencies directly. |
35 | | // |
36 | | // This reduces the setup overhead for evaluation and minimizes value churn |
37 | | // to / from a heap based value stack managed by the CEL runtime, but can't be |
38 | | // used for arbitrarily nested expressions. |
39 | | class DirectExpressionStep { |
40 | | public: |
41 | 0 | explicit DirectExpressionStep(int64_t expr_id) : expr_id_(expr_id) {} |
42 | 0 | DirectExpressionStep() : expr_id_(-1) {} |
43 | | |
44 | 0 | virtual ~DirectExpressionStep() = default; |
45 | | |
46 | 0 | int64_t expr_id() const { return expr_id_; } |
47 | 0 | bool comes_from_ast() const { return expr_id_ >= 0; } |
48 | | |
49 | | virtual absl::Status Evaluate(ExecutionFrameBase& frame, cel::Value& result, |
50 | | AttributeTrail& attribute) const = 0; |
51 | | |
52 | | // Return a type id for this node. |
53 | | // |
54 | | // Users must not make any assumptions about the type if the default value is |
55 | | // returned. |
56 | 0 | virtual cel::NativeTypeId GetNativeTypeId() const { |
57 | 0 | return cel::NativeTypeId(); |
58 | 0 | } |
59 | | |
60 | | // Implementations optionally support inspecting the program tree. |
61 | | virtual absl::optional<std::vector<const DirectExpressionStep*>> |
62 | 0 | GetDependencies() const { |
63 | 0 | return absl::nullopt; |
64 | 0 | } |
65 | | |
66 | | // Implementations optionally support extracting the program tree. |
67 | | // |
68 | | // Extract prevents the callee from functioning, and is only intended for use |
69 | | // when replacing a given expression step. |
70 | | virtual absl::optional<std::vector<std::unique_ptr<DirectExpressionStep>>> |
71 | 0 | ExtractDependencies() { |
72 | 0 | return absl::nullopt; |
73 | 0 | }; |
74 | | |
75 | | protected: |
76 | | int64_t expr_id_; |
77 | | }; |
78 | | |
79 | | // Wrapper for direct steps to work with the stack machine impl. |
80 | | class WrappedDirectStep : public ExpressionStep { |
81 | | public: |
82 | | WrappedDirectStep(std::unique_ptr<DirectExpressionStep> impl, int64_t expr_id) |
83 | 0 | : ExpressionStep(expr_id, false), impl_(std::move(impl)) {} |
84 | | |
85 | | absl::Status Evaluate(ExecutionFrame* frame) const override; |
86 | | |
87 | 0 | cel::NativeTypeId GetNativeTypeId() const override { |
88 | 0 | return cel::NativeTypeId::For<WrappedDirectStep>(); |
89 | 0 | } |
90 | | |
91 | 0 | const DirectExpressionStep* wrapped() const { return impl_.get(); } |
92 | | |
93 | | private: |
94 | | std::unique_ptr<DirectExpressionStep> impl_; |
95 | | }; |
96 | | |
97 | | } // namespace google::api::expr::runtime |
98 | | |
99 | | #endif // THIRD_PARTY_CEL_CPP_EVAL_EVAL_DIRECT_EXPRESSION_STEP_H_ |