Skip to content

Commit f327daf

Browse files
jnthntatumcopybara-github
authored andcommitted
Add support for resolving types at plan time.
Add an option to prefetch field descriptors when type is known. PiperOrigin-RevId: 948520646
1 parent 2491ba9 commit f327daf

30 files changed

Lines changed: 683 additions & 471 deletions

checker/internal/BUILD

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ cc_library(
6565
srcs = ["type_check_env.cc"],
6666
hdrs = ["type_check_env.h"],
6767
deps = [
68-
":descriptor_pool_type_introspector",
6968
":proto_type_mask",
7069
":proto_type_mask_registry",
7170
"//common:constant",
7271
"//common:container",
7372
"//common:decl",
73+
"//common:descriptor_pool_type_introspector",
7474
"//common:type",
7575
"//internal:status_macros",
7676
"@com_google_absl//absl/base:core_headers",
@@ -275,39 +275,6 @@ cc_test(
275275
],
276276
)
277277

278-
cc_library(
279-
name = "descriptor_pool_type_introspector",
280-
srcs = ["descriptor_pool_type_introspector.cc"],
281-
hdrs = ["descriptor_pool_type_introspector.h"],
282-
deps = [
283-
"//common:type",
284-
"@com_google_absl//absl/base:core_headers",
285-
"@com_google_absl//absl/base:nullability",
286-
"@com_google_absl//absl/container:flat_hash_map",
287-
"@com_google_absl//absl/log:absl_check",
288-
"@com_google_absl//absl/status:statusor",
289-
"@com_google_absl//absl/strings:string_view",
290-
"@com_google_absl//absl/synchronization",
291-
"@com_google_absl//absl/types:optional",
292-
"@com_google_absl//absl/types:span",
293-
"@com_google_protobuf//:protobuf",
294-
],
295-
)
296-
297-
cc_test(
298-
name = "descriptor_pool_type_introspector_test",
299-
srcs = ["descriptor_pool_type_introspector_test.cc"],
300-
deps = [
301-
":descriptor_pool_type_introspector",
302-
"//common:type",
303-
"//internal:testing",
304-
"//internal:testing_descriptor_pool",
305-
"@com_google_absl//absl/status:status_matchers",
306-
"@com_google_absl//absl/status:statusor",
307-
"@com_google_absl//absl/types:optional",
308-
],
309-
)
310-
311278
cc_library(
312279
name = "field_path",
313280
srcs = ["field_path.cc"],

checker/internal/type_check_env.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@
3030
#include "absl/strings/string_view.h"
3131
#include "absl/types/optional.h"
3232
#include "absl/types/span.h"
33-
#include "checker/internal/descriptor_pool_type_introspector.h"
3433
#include "checker/internal/proto_type_mask.h"
3534
#include "checker/internal/proto_type_mask_registry.h"
3635
#include "common/constant.h"
3736
#include "common/container.h"
3837
#include "common/decl.h"
38+
#include "common/descriptor_pool_type_introspector.h"
3939
#include "common/type.h"
4040
#include "common/type_introspector.h"
4141
#include "internal/status_macros.h"

common/BUILD

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@ cc_library(
5252
hdrs = ["type_spec_resolver.h"],
5353
deps = [
5454
":ast",
55+
":descriptor_pool_type_introspector",
5556
":type",
5657
":type_kind",
5758
"//internal:status_macros",
5859
"@com_google_absl//absl/status",
5960
"@com_google_absl//absl/status:statusor",
6061
"@com_google_absl//absl/strings",
62+
"@com_google_absl//absl/types:optional",
6163
"@com_google_protobuf//:protobuf",
6264
],
6365
)
@@ -79,6 +81,39 @@ cc_test(
7981
],
8082
)
8183

84+
cc_library(
85+
name = "descriptor_pool_type_introspector",
86+
srcs = ["descriptor_pool_type_introspector.cc"],
87+
hdrs = ["descriptor_pool_type_introspector.h"],
88+
deps = [
89+
":type",
90+
"@com_google_absl//absl/base:core_headers",
91+
"@com_google_absl//absl/base:nullability",
92+
"@com_google_absl//absl/container:flat_hash_map",
93+
"@com_google_absl//absl/log:absl_check",
94+
"@com_google_absl//absl/status:statusor",
95+
"@com_google_absl//absl/strings:string_view",
96+
"@com_google_absl//absl/synchronization",
97+
"@com_google_absl//absl/types:optional",
98+
"@com_google_absl//absl/types:span",
99+
"@com_google_protobuf//:protobuf",
100+
],
101+
)
102+
103+
cc_test(
104+
name = "descriptor_pool_type_introspector_test",
105+
srcs = ["descriptor_pool_type_introspector_test.cc"],
106+
deps = [
107+
":descriptor_pool_type_introspector",
108+
":type",
109+
"//internal:testing",
110+
"//internal:testing_descriptor_pool",
111+
"@com_google_absl//absl/status:status_matchers",
112+
"@com_google_absl//absl/status:statusor",
113+
"@com_google_absl//absl/types:optional",
114+
],
115+
)
116+
82117
cc_library(
83118
name = "signature",
84119
srcs = ["signature.cc"],

checker/internal/descriptor_pool_type_introspector.cc renamed to common/descriptor_pool_type_introspector.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "checker/internal/descriptor_pool_type_introspector.h"
15+
#include "common/descriptor_pool_type_introspector.h"
1616

1717
#include <memory>
18+
#include <optional>
1819
#include <utility>
1920
#include <vector>
2021

@@ -24,13 +25,12 @@
2425
#include "absl/status/statusor.h"
2526
#include "absl/strings/string_view.h"
2627
#include "absl/synchronization/mutex.h"
27-
#include "absl/types/optional.h"
2828
#include "absl/types/span.h"
2929
#include "common/type.h"
3030
#include "common/type_introspector.h"
3131
#include "google/protobuf/descriptor.h"
3232

33-
namespace cel::checker_internal {
33+
namespace cel {
3434
namespace {
3535

3636
// Standard implementation for field lookups.
@@ -242,4 +242,4 @@ DescriptorPoolTypeIntrospector::CreateFieldTable(
242242
return result;
243243
}
244244

245-
} // namespace cel::checker_internal
245+
} // namespace cel

checker/internal/descriptor_pool_type_introspector.h renamed to common/descriptor_pool_type_introspector.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#ifndef THIRD_PARTY_CEL_CPP_CHECKER_INTERNAL_DESCRIPTOR_POOL_TYPE_INTROSPECTOR_H_
16-
#define THIRD_PARTY_CEL_CPP_CHECKER_INTERNAL_DESCRIPTOR_POOL_TYPE_INTROSPECTOR_H_
15+
#ifndef THIRD_PARTY_CEL_CPP_COMMON_DESCRIPTOR_POOL_TYPE_INTROSPECTOR_H_
16+
#define THIRD_PARTY_CEL_CPP_COMMON_DESCRIPTOR_POOL_TYPE_INTROSPECTOR_H_
1717

1818
#include <memory>
1919
#include <vector>
@@ -30,14 +30,15 @@
3030
#include "common/type_introspector.h"
3131
#include "google/protobuf/descriptor.h"
3232

33-
namespace cel::checker_internal {
33+
namespace cel {
3434

3535
// Implementation of `TypeIntrospector` that uses a `google::protobuf::DescriptorPool`.
3636
//
3737
// This is used by the type checker to resolve protobuf types and their fields
3838
// and apply any options like using JSON names.
3939
//
40-
// Neither copyable nor movable. Should be managed by a TypeCheckEnv.
40+
// Neither copyable nor movable. Should be managed by a TypeCheckEnv or a
41+
// runtime Environment.
4142
class DescriptorPoolTypeIntrospector : public TypeIntrospector {
4243
public:
4344
struct Field {
@@ -100,6 +101,6 @@ class DescriptorPoolTypeIntrospector : public TypeIntrospector {
100101
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool_;
101102
};
102103

103-
} // namespace cel::checker_internal
104+
} // namespace cel
104105

105-
#endif // THIRD_PARTY_CEL_CPP_CHECKER_INTERNAL_DESCRIPTOR_POOL_TYPE_INTROSPECTOR_H_
106+
#endif // THIRD_PARTY_CEL_CPP_COMMON_DESCRIPTOR_POOL_TYPE_INTROSPECTOR_H_

checker/internal/descriptor_pool_type_introspector_test.cc renamed to common/descriptor_pool_type_introspector_test.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include "checker/internal/descriptor_pool_type_introspector.h"
15+
#include "common/descriptor_pool_type_introspector.h"
1616

17+
#include <optional>
1718
#include <vector>
1819

1920
#include "absl/status/status_matchers.h"
2021
#include "absl/status/statusor.h"
21-
#include "absl/types/optional.h"
2222
#include "common/type.h"
2323
#include "common/type_introspector.h"
2424
#include "internal/testing.h"
2525
#include "internal/testing_descriptor_pool.h"
2626

27-
namespace cel::checker_internal {
27+
namespace cel {
2828
namespace {
2929

3030
using ::absl_testing::IsOkAndHolds;
@@ -172,4 +172,4 @@ TEST(DescriptorPoolTypeIntrospectorTest, ListFieldsForStructTypeNotFound) {
172172
}
173173

174174
} // namespace
175-
} // namespace cel::checker_internal
175+
} // namespace cel

common/signature.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ absl::StatusOr<TypeSpec> ParseTypeSpec(std::string_view signature) {
634634
absl::StatusOr<Type> ParseType(std::string_view signature, google::protobuf::Arena* arena,
635635
const google::protobuf::DescriptorPool& pool) {
636636
CEL_ASSIGN_OR_RETURN(auto type_spec, ParseTypeSpec(signature));
637-
return cel::ConvertTypeSpecToType(type_spec, arena, pool);
637+
return cel::ConvertTypeSpecToType(type_spec, pool, arena);
638638
}
639639

640640
} // namespace cel

common/signature_test.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ TEST_P(TypeSignatureTest, TypeSignature) {
8585
EXPECT_THAT(signature, IsOkAndHolds(param.expected_signature));
8686

8787
absl::StatusOr<Type> type = ConvertTypeSpecToType(
88-
param.type, GetTestArena(), *GetTestingDescriptorPool());
88+
param.type, *GetTestingDescriptorPool(), GetTestArena());
8989
ASSERT_THAT(type, ::absl_testing::IsOk());
9090
EXPECT_THAT(MakeTypeSignature(*type),
9191
IsOkAndHolds(param.expected_signature));
@@ -285,9 +285,10 @@ TEST_P(TypeSignatureTest, ParseTypeCheck) {
285285
auto parsed = ParseType(param.expected_signature, GetTestArena(),
286286
*GetTestingDescriptorPool());
287287
ASSERT_THAT(parsed, ::absl_testing::IsOk());
288-
ASSERT_OK_AND_ASSIGN(auto expected_type,
289-
ConvertTypeSpecToType(param.type, GetTestArena(),
290-
*GetTestingDescriptorPool()));
288+
ASSERT_OK_AND_ASSIGN(
289+
auto expected_type,
290+
ConvertTypeSpecToType(param.type, *GetTestingDescriptorPool(),
291+
GetTestArena()));
291292
VerifyTypesEqual(*parsed, expected_type);
292293
}
293294
}

common/type.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,11 @@ absl::optional<MessageTypeField> StructTypeField::AsMessage() const {
606606
return std::nullopt;
607607
}
608608

609+
MessageTypeField StructTypeField::GetMessage() const {
610+
ABSL_DCHECK(IsMessage());
611+
return absl::get<MessageTypeField>(variant_);
612+
}
613+
609614
StructTypeField::operator MessageTypeField() const {
610615
ABSL_DCHECK(IsMessage());
611616
return absl::get<MessageTypeField>(variant_);

common/type.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,7 @@ class StructTypeField final {
11401140
}
11411141

11421142
absl::optional<MessageTypeField> AsMessage() const;
1143+
MessageTypeField GetMessage() const;
11431144

11441145
explicit operator MessageTypeField() const;
11451146

0 commit comments

Comments
 (0)