/proc/self/cwd/common/function_descriptor.h
Line | Count | Source |
1 | | // Copyright 2023 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 | | |
15 | | #ifndef THIRD_PARTY_CEL_CPP_COMMON_FUNCTION_DESCRIPTOR_H_ |
16 | | #define THIRD_PARTY_CEL_CPP_COMMON_FUNCTION_DESCRIPTOR_H_ |
17 | | |
18 | | #include <string> |
19 | | #include <utility> |
20 | | #include <vector> |
21 | | |
22 | | #include "absl/base/attributes.h" |
23 | | #include "absl/strings/string_view.h" |
24 | | #include "absl/types/span.h" |
25 | | #include "common/ast.h" |
26 | | #include "common/kind.h" |
27 | | |
28 | | namespace cel { |
29 | | |
30 | | struct FunctionDescriptorOptions { |
31 | | // If true (strict, default), error or unknown arguments are propagated |
32 | | // instead of calling the function. if false (non-strict), the function may |
33 | | // receive error or unknown values as arguments. |
34 | | bool is_strict = true; |
35 | | |
36 | | // Whether the function is impure or context-sensitive. |
37 | | // |
38 | | // Impure functions depend on state other than the arguments received during |
39 | | // the CEL expression evaluation or have visible side effects. This breaks |
40 | | // some of the assumptions of the CEL evaluation model. This flag is used as a |
41 | | // hint to the planner that some optimizations are not safe or not effective. |
42 | | bool is_contextual = false; |
43 | | }; |
44 | | |
45 | | // Coarsely describes a function for the purpose of runtime resolution of |
46 | | // overloads. |
47 | | class FunctionDescriptor { |
48 | | public: |
49 | | // Creates a FunctionDescriptor for a global function. |
50 | | [[nodiscard]] |
51 | | static FunctionDescriptor Global(absl::string_view name, |
52 | | absl::string_view overload_id, |
53 | | const FunctionTypeSpec& type, |
54 | 0 | FunctionDescriptorOptions options = {}) { |
55 | 0 | return FunctionDescriptor(name, overload_id, type, options, |
56 | 0 | /*is_receiver_style=*/false); |
57 | 0 | } |
58 | | |
59 | | // Creates a FunctionDescriptor for a member function. |
60 | | [[nodiscard]] |
61 | | static FunctionDescriptor Member(absl::string_view name, |
62 | | absl::string_view overload_id, |
63 | | const FunctionTypeSpec& type, |
64 | 0 | FunctionDescriptorOptions options = {}) { |
65 | 0 | return FunctionDescriptor(name, overload_id, type, options, |
66 | 0 | /*is_receiver_style=*/true); |
67 | 0 | } |
68 | | |
69 | 4.37M | FunctionDescriptor(const FunctionDescriptor&) = default; |
70 | | FunctionDescriptor(FunctionDescriptor&&) = default; |
71 | | FunctionDescriptor& operator=(const FunctionDescriptor&) = default; |
72 | | FunctionDescriptor& operator=(FunctionDescriptor&&) = default; |
73 | | |
74 | | FunctionDescriptor(absl::string_view name, bool receiver_style, |
75 | | std::vector<Kind> types, bool is_strict) |
76 | | : FunctionDescriptor(name, receiver_style, std::move(types), |
77 | | FunctionDescriptorOptions{is_strict, |
78 | 0 | /*is_contextual=*/false}) { |
79 | 0 | } |
80 | | |
81 | | FunctionDescriptor(absl::string_view name, bool receiver_style, |
82 | | std::vector<Kind> types, bool is_strict, |
83 | | bool is_contextual) |
84 | | : FunctionDescriptor( |
85 | | name, receiver_style, std::move(types), |
86 | 0 | FunctionDescriptorOptions{is_strict, is_contextual}) {} |
87 | | |
88 | | FunctionDescriptor(absl::string_view name, bool is_receiver_style, |
89 | | std::vector<Kind> types, |
90 | | FunctionDescriptorOptions options = {}) |
91 | 4.37M | : name_(name), |
92 | 4.37M | kinds_(std::move(types)), |
93 | 4.37M | is_receiver_style_(is_receiver_style), |
94 | 4.37M | options_(std::move(options)) {} |
95 | | |
96 | | // Function name. |
97 | 13.1M | const std::string& name() const { return name_; } |
98 | | |
99 | | // Function overload ID. This may be empty for legacy code. |
100 | | [[nodiscard]] |
101 | 0 | absl::string_view overload_id() const ABSL_ATTRIBUTE_LIFETIME_BOUND { |
102 | 0 | return overload_id_; |
103 | 0 | } |
104 | | |
105 | | // Whether function is receiver style i.e. true means arg0.name(args[1:]...). |
106 | 29.5M | bool receiver_style() const { return is_receiver_style_; } |
107 | | |
108 | | // The argument types the function accepts. |
109 | | // |
110 | | // TODO(uncreated-issue/17): make this kinds |
111 | 369M | const std::vector<Kind>& types() const { return kinds_; } |
112 | | |
113 | | // if true (strict, default), error or unknown arguments are propagated |
114 | | // instead of calling the function. if false (non-strict), the function may |
115 | | // receive error or unknown values as arguments. |
116 | 17.0M | bool is_strict() const { return options_.is_strict; } |
117 | | |
118 | | // Whether the function is contextual (impure). |
119 | | // |
120 | | // Contextual functions depend on state other than the arguments received in |
121 | | // the CEL expression evaluation or have visible side effects. This breaks |
122 | | // some of the assumptions of CEL. This flag is used as a hint to the planner |
123 | | // that some optimizations are not safe or not effective. |
124 | 10.4M | bool is_contextual() const { return options_.is_contextual; } |
125 | | |
126 | | // Helper for matching a descriptor. This tests that the shape is the same -- |
127 | | // |other| accepts the same number and types of arguments and is the same call |
128 | | // style). |
129 | 14.4M | bool ShapeMatches(const FunctionDescriptor& other) const { |
130 | 14.4M | return ShapeMatches(other.receiver_style(), other.types()); |
131 | 14.4M | } |
132 | | bool ShapeMatches(bool receiver_style, absl::Span<const Kind> types) const; |
133 | | |
134 | | bool operator==(const FunctionDescriptor& other) const; |
135 | | |
136 | | bool operator<(const FunctionDescriptor& other) const; |
137 | | |
138 | | private: |
139 | | [[nodiscard]] |
140 | | static std::vector<Kind> FunctionTypeSpecToArgumentKinds( |
141 | | const FunctionTypeSpec& spec); |
142 | | |
143 | | FunctionDescriptor(absl::string_view name, absl::string_view overload_id, |
144 | | const FunctionTypeSpec& type, |
145 | | FunctionDescriptorOptions options, bool is_receiver_style) |
146 | | : name_(name), |
147 | | overload_id_(overload_id), |
148 | | kinds_(FunctionTypeSpecToArgumentKinds(type)), |
149 | | is_receiver_style_(is_receiver_style), |
150 | 0 | options_(std::move(options)) {} |
151 | | |
152 | | std::string name_; |
153 | | std::string overload_id_; |
154 | | std::vector<Kind> kinds_; |
155 | | bool is_receiver_style_; |
156 | | FunctionDescriptorOptions options_; |
157 | | }; |
158 | | |
159 | | } // namespace cel |
160 | | |
161 | | #endif // THIRD_PARTY_CEL_CPP_COMMON_FUNCTION_DESCRIPTOR_H_ |