Skip to content

Commit 8353a6e

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
Expose TypeInfoToType and NewCompilerBuilder APIs
PiperOrigin-RevId: 890090916
1 parent f6cd0c8 commit 8353a6e

9 files changed

Lines changed: 388 additions & 274 deletions

File tree

env/BUILD

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,28 @@ package(default_visibility = ["//visibility:public"])
1919

2020
cc_library(
2121
name = "config",
22-
srcs = ["config.cc"],
23-
hdrs = ["config.h"],
22+
srcs = [
23+
"config.cc",
24+
"type_info.cc",
25+
],
26+
hdrs = [
27+
"config.h",
28+
"type_info.h",
29+
],
2430
deps = [
2531
"//common:constant",
32+
"//common:type",
33+
"//common:type_kind",
2634
"//internal:status_macros",
35+
"@com_google_absl//absl/base:no_destructor",
36+
"@com_google_absl//absl/container:flat_hash_map",
2737
"@com_google_absl//absl/container:flat_hash_set",
2838
"@com_google_absl//absl/functional:overload",
2939
"@com_google_absl//absl/status",
40+
"@com_google_absl//absl/status:statusor",
3041
"@com_google_absl//absl/strings",
3142
"@com_google_absl//absl/time",
32-
"@com_google_absl//absl/types:variant",
43+
"@com_google_protobuf//:protobuf",
3344
],
3445
)
3546

@@ -43,23 +54,16 @@ cc_library(
4354
"//common:constant",
4455
"//common:decl",
4556
"//common:type",
46-
"//common:type_kind",
4757
"//compiler",
4858
"//compiler:compiler_factory",
4959
"//compiler:standard_library",
5060
"//env/internal:ext_registry",
5161
"//internal:status_macros",
5262
"//parser:macro",
53-
"@com_google_absl//absl/base:no_destructor",
5463
"@com_google_absl//absl/container:flat_hash_map",
55-
"@com_google_absl//absl/container:flat_hash_set",
5664
"@com_google_absl//absl/functional:any_invocable",
57-
"@com_google_absl//absl/functional:overload",
58-
"@com_google_absl//absl/status",
5965
"@com_google_absl//absl/status:statusor",
6066
"@com_google_absl//absl/strings",
61-
"@com_google_absl//absl/time",
62-
"@com_google_absl//absl/types:variant",
6367
"@com_google_protobuf//:protobuf",
6468
],
6569
)
@@ -163,6 +167,21 @@ cc_test(
163167
],
164168
)
165169

170+
cc_test(
171+
name = "type_info_test",
172+
srcs = ["type_info_test.cc"],
173+
deps = [
174+
":config",
175+
"//common:type",
176+
"//common:type_proto",
177+
"//internal:testing",
178+
"//internal:testing_descriptor_pool",
179+
"@com_google_cel_spec//proto/cel/expr:syntax_cc_proto",
180+
"@com_google_protobuf//:protobuf",
181+
"@com_google_protobuf//:type_cc_proto",
182+
],
183+
)
184+
166185
cc_test(
167186
name = "env_test",
168187
srcs = ["env_test.cc"],
@@ -173,7 +192,6 @@ cc_test(
173192
"//checker:type_checker_builder",
174193
"//checker:validation_result",
175194
"//common:ast",
176-
"//common:ast_proto",
177195
"//common:constant",
178196
"//common:decl",
179197
"//common:expr",

env/env.cc

Lines changed: 8 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,22 @@
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-
24196
absl::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

env/env.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ class Env {
5757

5858
void SetConfig(const Config& config) { config_ = config; }
5959

60+
absl::StatusOr<std::unique_ptr<CompilerBuilder>> NewCompilerBuilder();
61+
62+
// Shortcut for NewCompilerBuilder() followed by Build().
6063
absl::StatusOr<std::unique_ptr<Compiler>> NewCompiler();
6164

6265
private:

0 commit comments

Comments
 (0)