1515#include " env/env.h"
1616
1717#include < memory>
18- #include < optional>
1918#include < string>
2019#include < utility>
2120#include < vector>
2221
23- #include " absl/base/no_destructor.h"
2422#include " absl/container/flat_hash_map.h"
2523#include " absl/status/statusor.h"
2624#include " absl/strings/string_view.h"
2725#include " checker/type_checker_builder.h"
2826#include " common/constant.h"
2927#include " common/decl.h"
3028#include " common/type.h"
31- #include " common/type_kind.h"
3229#include " compiler/compiler.h"
3330#include " compiler/compiler_factory.h"
3431#include " compiler/standard_library.h"
3532#include " env/config.h"
33+ #include " env/type_info.h"
3634#include " internal/status_macros.h"
3735#include " parser/macro.h"
3836#include " google/protobuf/arena.h"
@@ -95,149 +93,6 @@ absl::StatusOr<CompilerLibrarySubset> MakeStdlibSubset(
9593 return subset;
9694}
9795
98- std::optional<TypeKind> TypeNameToTypeKind (absl::string_view type_name) {
99- // Excluded types:
100- // kUnknown
101- // kError
102- // kTypeParam
103- // kFunction
104- // kEnum
105-
106- static const absl::NoDestructor<
107- absl::flat_hash_map<absl::string_view, TypeKind>>
108- kTypeNameToTypeKind ({
109- {" null" , TypeKind::kNull },
110- {" bool" , TypeKind::kBool },
111- {" int" , TypeKind::kInt },
112- {" uint" , TypeKind::kUint },
113- {" double" , TypeKind::kDouble },
114- {" string" , TypeKind::kString },
115- {" bytes" , TypeKind::kBytes },
116- {" timestamp" , TypeKind::kTimestamp },
117- {TimestampType::kName , TypeKind::kTimestamp },
118- {" duration" , TypeKind::kDuration },
119- {DurationType::kName , TypeKind::kDuration },
120- {" list" , TypeKind::kList },
121- {" map" , TypeKind::kMap },
122- {" " , TypeKind::kDyn },
123- {" any" , TypeKind::kAny },
124- {" dyn" , TypeKind::kDyn },
125- {BoolWrapperType::kName , TypeKind::kBoolWrapper },
126- {IntWrapperType::kName , TypeKind::kIntWrapper },
127- {UintWrapperType::kName , TypeKind::kUintWrapper },
128- {DoubleWrapperType::kName , TypeKind::kDoubleWrapper },
129- {StringWrapperType::kName , TypeKind::kStringWrapper },
130- {BytesWrapperType::kName , TypeKind::kBytesWrapper },
131- {" type" , TypeKind::kType },
132- });
133- if (auto it = kTypeNameToTypeKind ->find (type_name);
134- it != kTypeNameToTypeKind ->end ()) {
135- return it->second ;
136- }
137-
138- return std::nullopt ;
139- }
140-
141- absl::StatusOr<Type> TypeInfoToType (
142- const Config::TypeInfo& type_info, google::protobuf::Arena* arena,
143- const google::protobuf::DescriptorPool* descriptor_pool) {
144- if (type_info.is_type_param ) {
145- return TypeParamType (type_info.name );
146- }
147-
148- std::optional<TypeKind> type_kind = TypeNameToTypeKind (type_info.name );
149- if (!type_kind.has_value ()) {
150- if (type_info.params .empty () && descriptor_pool != nullptr ) {
151- const google::protobuf::Descriptor* type =
152- descriptor_pool->FindMessageTypeByName (type_info.name );
153- if (type != nullptr ) {
154- return MessageType (type);
155- }
156- }
157- // TODO(uncreated-issue/88): use a TypeIntrospector to validate opaque types
158- std::vector<Type> parameter_types;
159- for (const Config::TypeInfo& param : type_info.params ) {
160- CEL_ASSIGN_OR_RETURN (Type parameter_type,
161- TypeInfoToType (param, arena, descriptor_pool));
162- parameter_types.push_back (parameter_type);
163- }
164-
165- return OpaqueType (arena, type_info.name , parameter_types);
166- }
167-
168- switch (*type_kind) {
169- case TypeKind::kNull :
170- return NullType ();
171- case TypeKind::kBool :
172- return BoolType ();
173- case TypeKind::kInt :
174- return IntType ();
175- case TypeKind::kUint :
176- return UintType ();
177- case TypeKind::kDouble :
178- return DoubleType ();
179- case TypeKind::kString :
180- return StringType ();
181- case TypeKind::kBytes :
182- return BytesType ();
183- case TypeKind::kDuration :
184- return DurationType ();
185- case TypeKind::kTimestamp :
186- return TimestampType ();
187- case TypeKind::kList : {
188- Type element_type;
189- if (!type_info.params .empty ()) {
190- CEL_ASSIGN_OR_RETURN (
191- element_type,
192- TypeInfoToType (type_info.params [0 ], arena, descriptor_pool));
193- } else {
194- element_type = DynType ();
195- }
196- return ListType (arena, element_type);
197- }
198- case TypeKind::kMap : {
199- Type key_type = DynType ();
200- Type value_type = DynType ();
201- if (!type_info.params .empty ()) {
202- CEL_ASSIGN_OR_RETURN (key_type, TypeInfoToType (type_info.params [0 ],
203- arena, descriptor_pool));
204- }
205- if (type_info.params .size () > 1 ) {
206- CEL_ASSIGN_OR_RETURN (
207- value_type,
208- TypeInfoToType (type_info.params [1 ], arena, descriptor_pool));
209- }
210- return MapType (arena, key_type, value_type);
211- }
212- case TypeKind::kDyn :
213- return DynType ();
214- case TypeKind::kAny :
215- return AnyType ();
216- case TypeKind::kBoolWrapper :
217- return BoolWrapperType ();
218- case TypeKind::kIntWrapper :
219- return IntWrapperType ();
220- case TypeKind::kUintWrapper :
221- return UintWrapperType ();
222- case TypeKind::kDoubleWrapper :
223- return DoubleWrapperType ();
224- case TypeKind::kStringWrapper :
225- return StringWrapperType ();
226- case TypeKind::kBytesWrapper :
227- return BytesWrapperType ();
228- case TypeKind::kType : {
229- if (type_info.params .empty ()) {
230- return TypeType (arena, DynType ());
231- }
232- CEL_ASSIGN_OR_RETURN (Type type, TypeInfoToType (type_info.params [0 ], arena,
233- descriptor_pool));
234- return TypeType (arena, type);
235- }
236- default :
237- return DynType ();
238- }
239- }
240-
24196absl::StatusOr<FunctionDecl> FunctionConfigToFunctionDecl (
24297 const Config::FunctionConfig& function_config, google::protobuf::Arena* arena,
24398 const google::protobuf::DescriptorPool* descriptor_pool) {
@@ -264,7 +119,7 @@ absl::StatusOr<FunctionDecl> FunctionConfigToFunctionDecl(
264119
265120} // namespace
266121
267- absl::StatusOr<std::unique_ptr<Compiler >> Env::NewCompiler () {
122+ absl::StatusOr<std::unique_ptr<CompilerBuilder >> Env::NewCompilerBuilder () {
268123 CEL_ASSIGN_OR_RETURN (
269124 std::unique_ptr<CompilerBuilder> compiler_builder,
270125 cel::NewCompilerBuilder (descriptor_pool_, compiler_options_));
@@ -312,7 +167,12 @@ absl::StatusOr<std::unique_ptr<Compiler>> Env::NewCompiler() {
312167 CEL_RETURN_IF_ERROR (checker_builder.AddFunction (function_decl));
313168 }
314169
315- return compiler_builder-> Build () ;
170+ return compiler_builder;
316171}
317172
173+ absl::StatusOr<std::unique_ptr<Compiler>> Env::NewCompiler () {
174+ CEL_ASSIGN_OR_RETURN (std::unique_ptr<CompilerBuilder> compiler_builder,
175+ NewCompilerBuilder ());
176+ return compiler_builder->Build ();
177+ }
318178} // namespace cel
0 commit comments