/proc/self/cwd/eval/compiler/resolver.h
Line | Count | Source |
1 | | // Copyright 2021 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_EVAL_COMPILER_RESOLVER_H_ |
16 | | #define THIRD_PARTY_CEL_CPP_EVAL_COMPILER_RESOLVER_H_ |
17 | | |
18 | | #include <cstddef> |
19 | | #include <cstdint> |
20 | | #include <memory> |
21 | | #include <string> |
22 | | #include <utility> |
23 | | #include <vector> |
24 | | |
25 | | #include "absl/container/flat_hash_map.h" |
26 | | #include "absl/status/statusor.h" |
27 | | #include "absl/strings/string_view.h" |
28 | | #include "absl/types/optional.h" |
29 | | #include "absl/types/span.h" |
30 | | #include "common/kind.h" |
31 | | #include "common/type.h" |
32 | | #include "common/type_reflector.h" |
33 | | #include "common/value.h" |
34 | | #include "runtime/function_overload_reference.h" |
35 | | #include "runtime/function_registry.h" |
36 | | #include "runtime/type_registry.h" |
37 | | |
38 | | namespace google::api::expr::runtime { |
39 | | |
40 | | // Resolver assists with finding functions and types from the associated |
41 | | // registries within a container. |
42 | | // |
43 | | // container is used to construct the namespace lookup candidates. |
44 | | // e.g. for "cel.dev" -> {"cel.dev.", "cel.", ""} |
45 | | class Resolver { |
46 | | public: |
47 | | Resolver(absl::string_view container, |
48 | | const cel::FunctionRegistry& function_registry, |
49 | | const cel::TypeRegistry& type_registry, |
50 | | const cel::TypeReflector& type_reflector, |
51 | | bool resolve_qualified_type_identifiers = true); |
52 | | |
53 | | Resolver(const Resolver&) = delete; |
54 | | Resolver& operator=(const Resolver&) = delete; |
55 | | Resolver(Resolver&&) = delete; |
56 | | Resolver& operator=(Resolver&&) = delete; |
57 | | |
58 | 10.3k | ~Resolver() = default; |
59 | | |
60 | | // FindConstant will return an enum constant value or a type value if one |
61 | | // exists for the given name. An empty handle will be returned if none exists. |
62 | | // |
63 | | // Since enums and type identifiers are specified as (potentially) qualified |
64 | | // names within an expression, there is the chance that the name provided |
65 | | // is a variable name which happens to collide with an existing enum or proto |
66 | | // based type name. For this reason, within parsed only expressions, the |
67 | | // constant should be treated as a value that can be shadowed by a runtime |
68 | | // provided value. |
69 | | absl::optional<cel::Value> FindConstant(absl::string_view name, |
70 | | int64_t expr_id) const; |
71 | | |
72 | | absl::StatusOr<absl::optional<std::pair<std::string, cel::Type>>> FindType( |
73 | | absl::string_view name, int64_t expr_id) const; |
74 | | |
75 | | // FindLazyOverloads returns the set, possibly empty, of lazy overloads |
76 | | // matching the given function signature. |
77 | | std::vector<cel::FunctionRegistry::LazyOverload> FindLazyOverloads( |
78 | | absl::string_view name, bool receiver_style, |
79 | | const std::vector<cel::Kind>& types, int64_t expr_id = -1) const; |
80 | | |
81 | | std::vector<cel::FunctionRegistry::LazyOverload> FindLazyOverloads( |
82 | | absl::string_view name, bool receiver_style, size_t arity, |
83 | | int64_t expr_id = -1) const; |
84 | | |
85 | | // FindOverloads returns the set, possibly empty, of eager function overloads |
86 | | // matching the given function signature. |
87 | | std::vector<cel::FunctionOverloadReference> FindOverloads( |
88 | | absl::string_view name, bool receiver_style, |
89 | | const std::vector<cel::Kind>& types, int64_t expr_id = -1) const; |
90 | | |
91 | | std::vector<cel::FunctionOverloadReference> FindOverloads( |
92 | | absl::string_view name, bool receiver_style, size_t arity, |
93 | | int64_t expr_id = -1) const; |
94 | | |
95 | | // FullyQualifiedNames returns the set of fully qualified names which may be |
96 | | // derived from the base_name within the specified expression container. |
97 | | std::vector<std::string> FullyQualifiedNames(absl::string_view base_name, |
98 | | int64_t expr_id = -1) const; |
99 | | |
100 | | private: |
101 | | absl::Span<const std::string> GetPrefixesFor(absl::string_view& name) const; |
102 | | |
103 | | std::vector<std::string> namespace_prefixes_; |
104 | | std::shared_ptr<const absl::flat_hash_map<std::string, cel::Value>> |
105 | | enum_value_map_; |
106 | | const cel::FunctionRegistry& function_registry_; |
107 | | const cel::TypeReflector& type_reflector_; |
108 | | |
109 | | bool resolve_qualified_type_identifiers_; |
110 | | }; |
111 | | |
112 | | // ArgumentMatcher generates a function signature matcher for CelFunctions. |
113 | | // TODO(issues/91): this is the same behavior as parsed exprs in the CPP |
114 | | // evaluator (just check the right call style and number of arguments), but we |
115 | | // should have enough type information in a checked expr to find a more |
116 | | // specific candidate list. |
117 | 0 | inline std::vector<cel::Kind> ArgumentsMatcher(int argument_count) { |
118 | 0 | std::vector<cel::Kind> argument_matcher(argument_count); |
119 | 0 | for (int i = 0; i < argument_count; i++) { |
120 | 0 | argument_matcher[i] = cel::Kind::kAny; |
121 | 0 | } |
122 | 0 | return argument_matcher; |
123 | 0 | } |
124 | | |
125 | | } // namespace google::api::expr::runtime |
126 | | |
127 | | #endif // THIRD_PARTY_CEL_CPP_EVAL_COMPILER_RESOLVER_H_ |