Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypePool and proto conversion utilities #885

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -960,3 +960,116 @@ cc_test(
"@com_google_protobuf//:protobuf",
],
)

cc_library(
name = "type_pool",
srcs = ["type_pool.cc"],
hdrs = ["type_pool.h"],
deps = [
":arena_string",
":arena_string_pool",
":type",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/types:span",
"@com_google_protobuf//:protobuf",
],
)

cc_test(
name = "type_pool_test",
srcs = ["type_pool_test.cc"],
deps = [
":arena_string_pool",
":type",
":type_pool",
"//internal:testing",
"//internal:testing_descriptor_pool",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/types:optional",
"@com_google_protobuf//:protobuf",
],
)

cc_library(
name = "type_proto",
srcs = ["type_proto.cc"],
hdrs = ["type_proto.h"],
deps = [
":type",
":type_kind",
":type_pool",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/container:inlined_vector",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:optional",
"@com_google_cel_spec//proto/cel/expr:checked_cc_proto",
"@com_google_protobuf//:protobuf",
],
)

cc_test(
name = "type_proto_test",
srcs = ["type_proto_test.cc"],
deps = [
":arena_string_pool",
":type",
":type_pool",
":type_proto",
"//internal:proto_matchers",
"//internal:testing",
"//internal:testing_descriptor_pool",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:status_matchers",
"@com_google_absl//absl/types:optional",
"@com_google_cel_spec//proto/cel/expr:checked_cc_proto",
"@com_google_protobuf//:protobuf",
],
)

cc_library(
name = "type_proto_v1alpha1",
srcs = ["type_proto_v1alpha1.cc"],
hdrs = ["type_proto_v1alpha1.h"],
deps = [
":type",
":type_kind",
":type_pool",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/container:inlined_vector",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:optional",
"@com_google_googleapis//google/api/expr/v1alpha1:checked_cc_proto",
"@com_google_protobuf//:protobuf",
],
)

cc_test(
name = "type_proto_v1alpha1_test",
srcs = ["type_proto_v1alpha1_test.cc"],
deps = [
":arena_string_pool",
":type",
":type_pool",
":type_proto_v1alpha1",
"//internal:proto_matchers",
"//internal:testing",
"//internal:testing_descriptor_pool",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:status_matchers",
"@com_google_absl//absl/types:optional",
"@com_google_googleapis//google/api/expr/v1alpha1:checked_cc_proto",
"@com_google_protobuf//:protobuf",
],
)
14 changes: 14 additions & 0 deletions common/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,20 @@ class TypeParameters final {
};
};

inline bool operator==(const TypeParameters& lhs, const TypeParameters& rhs) {
return absl::c_equal(lhs, rhs);
}

inline bool operator!=(const TypeParameters& lhs, const TypeParameters& rhs) {
return !operator==(lhs, rhs);
}

template <typename H>
H AbslHashValue(H state, const TypeParameters& parameters) {
return H::combine_contiguous(std::move(state), parameters.data(),
parameters.size());
}

// Now that TypeParameters is defined, we can define `GetParameters()` for most
// types.

Expand Down
62 changes: 62 additions & 0 deletions common/type_pool.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "common/type_pool.h"

#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "common/type.h"
#include "google/protobuf/descriptor.h"

namespace cel {

ListType TypePool::MakeListType(const Type& element) {
return list_type_pool_.InternListType(element);
}

MapType TypePool::MakeMapType(const Type& key, const Type& value) {
return map_type_pool_.InternMapType(key, value);
}

StructType TypePool::MakeStructType(absl::string_view name) {
if (descriptor_pool_ != nullptr) {
const google::protobuf::Descriptor* descriptor =
descriptor_pool_->FindMessageTypeByName(name);
if (descriptor != nullptr) {
return MessageType(descriptor);
}
}
return common_internal::MakeBasicStructType(string_pool_->InternString(name));
}

FunctionType TypePool::MakeFunctionType(const Type& result,
absl::Span<const Type> args) {
return function_type_pool_.InternFunctionType(result, args);
}

OpaqueType TypePool::MakeOpaqueType(absl::string_view name,
absl::Span<const Type> params) {
return opaque_type_pool_.InternOpaqueType(string_pool_->InternString(name),
params);
}

TypeParamType TypePool::MakeTypeParamType(absl::string_view name) {
return TypeParamType(string_pool_->InternString(name));
}

TypeType TypePool::MakeTypeType(const Type& type) {
return type_type_pool_.InternTypeType(type);
}

} // namespace cel
115 changes: 115 additions & 0 deletions common/type_pool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef THIRD_PARTY_CEL_CPP_COMMON_TYPE_POOL_H_
#define THIRD_PARTY_CEL_CPP_COMMON_TYPE_POOL_H_

#include <memory>

#include "absl/base/attributes.h"
#include "absl/base/nullability.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "common/arena_string.h"
#include "common/arena_string_pool.h"
#include "common/type.h"
#include "common/types/function_type_pool.h"
#include "common/types/list_type_pool.h"
#include "common/types/map_type_pool.h"
#include "common/types/opaque_type_pool.h"
#include "common/types/type_type_pool.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"

namespace cel {

class TypePool;

absl::Nonnull<std::unique_ptr<TypePool>> NewTypePool(
absl::Nonnull<google::protobuf::Arena*> arena ABSL_ATTRIBUTE_LIFETIME_BOUND,
absl::Nonnull<ArenaStringPool*> string_pool ABSL_ATTRIBUTE_LIFETIME_BOUND,
absl::Nullable<const google::protobuf::DescriptorPool*> descriptor_pool
ABSL_ATTRIBUTE_LIFETIME_BOUND);

class TypePool final {
public:
TypePool(const TypePool&) = delete;
TypePool(TypePool&&) = delete;
TypePool& operator=(const TypePool&) = delete;
TypePool& operator=(TypePool&&) = delete;

ListType MakeListType(const Type& element);

MapType MakeMapType(const Type& key, const Type& value);

StructType MakeStructType(absl::string_view name);

StructType MakeStructType(ArenaString) = delete;

FunctionType MakeFunctionType(const Type& result,
absl::Span<const Type> args);

OpaqueType MakeOpaqueType(absl::string_view name,
absl::Span<const Type> params);

OpaqueType MakeOpaqueType(ArenaString, absl::Span<const Type>) = delete;

OptionalType MakeOptionalType(const Type& param) {
return static_cast<OptionalType>(
MakeOpaqueType(OptionalType::kName, absl::MakeConstSpan(&param, 1)));
}

TypeParamType MakeTypeParamType(absl::string_view name);

TypeParamType MakeTypeParamType(ArenaString) = delete;

TypeType MakeTypeType(const Type& type);

private:
friend absl::Nonnull<std::unique_ptr<TypePool>> NewTypePool(
absl::Nonnull<google::protobuf::Arena*>, absl::Nonnull<ArenaStringPool*>,
absl::Nullable<const google::protobuf::DescriptorPool*>);

TypePool(absl::Nonnull<google::protobuf::Arena*> arena,
absl::Nonnull<ArenaStringPool*> string_pool,
absl::Nullable<const google::protobuf::DescriptorPool*> descriptor_pool)
: string_pool_(string_pool),
descriptor_pool_(descriptor_pool),
function_type_pool_(arena),
list_type_pool_(arena),
map_type_pool_(arena),
opaque_type_pool_(arena),
type_type_pool_(arena) {}

absl::Nonnull<ArenaStringPool*> const string_pool_;
absl::Nullable<const google::protobuf::DescriptorPool*> const descriptor_pool_;
common_internal::FunctionTypePool function_type_pool_;
common_internal::ListTypePool list_type_pool_;
common_internal::MapTypePool map_type_pool_;
common_internal::OpaqueTypePool opaque_type_pool_;
common_internal::TypeTypePool type_type_pool_;
};

inline absl::Nonnull<std::unique_ptr<TypePool>> NewTypePool(
absl::Nonnull<google::protobuf::Arena*> arena ABSL_ATTRIBUTE_LIFETIME_BOUND,
absl::Nonnull<ArenaStringPool*> string_pool ABSL_ATTRIBUTE_LIFETIME_BOUND,
absl::Nullable<const google::protobuf::DescriptorPool*> descriptor_pool
ABSL_ATTRIBUTE_LIFETIME_BOUND) {
return std::unique_ptr<TypePool>(
new TypePool(arena, string_pool, descriptor_pool));
}

} // namespace cel

#endif // THIRD_PARTY_CEL_CPP_COMMON_TYPE_POOL_H_
Loading