Skip to content
Merged
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
1 change: 1 addition & 0 deletions common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ cc_library(
":unknown",
":value_kind",
"//base:attributes",
"//base/internal:message_wrapper",
"//common/internal:byte_string",
"//common/internal:reference_count",
"//eval/internal:cel_value_equal",
Expand Down
151 changes: 89 additions & 62 deletions common/legacy_value.cc

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions common/legacy_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ inline DoubleValue CreateDoubleValue(double value) {

inline ListValue CreateLegacyListValue(
const google::api::expr::runtime::CelList* value) {
return common_internal::LegacyListValue(value);
return common_internal::LegacyListValue{reinterpret_cast<uintptr_t>(value)};
}

inline MapValue CreateLegacyMapValue(
const google::api::expr::runtime::CelMap* value) {
return common_internal::LegacyMapValue(value);
return common_internal::LegacyMapValue{reinterpret_cast<uintptr_t>(value)};
}

inline Value CreateDurationValue(absl::Duration value, bool unchecked = false) {
Expand Down
10 changes: 6 additions & 4 deletions common/values/legacy_list_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "common/values/legacy_list_value.h"

#include <cstdint>

#include "absl/base/nullability.h"
#include "absl/log/absl_check.h"
#include "absl/status/status.h"
Expand Down Expand Up @@ -60,15 +62,15 @@ absl::optional<LegacyListValue> AsLegacyListValue(const Value& value) {
if (auto custom_list_value = value.AsCustomList(); custom_list_value) {
NativeTypeId native_type_id = NativeTypeId::Of(*custom_list_value);
if (native_type_id == NativeTypeId::For<CompatListValue>()) {
return LegacyListValue(
return LegacyListValue(reinterpret_cast<uintptr_t>(
static_cast<const google::api::expr::runtime::CelList*>(
cel::internal::down_cast<const CompatListValue*>(
(*custom_list_value).operator->())));
(*custom_list_value).operator->()))));
} else if (native_type_id == NativeTypeId::For<MutableCompatListValue>()) {
return LegacyListValue(
return LegacyListValue(reinterpret_cast<uintptr_t>(
static_cast<const google::api::expr::runtime::CelList*>(
cel::internal::down_cast<const MutableCompatListValue*>(
(*custom_list_value).operator->())));
(*custom_list_value).operator->()))));
}
}
return absl::nullopt;
Expand Down
27 changes: 12 additions & 15 deletions common/values/legacy_list_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define THIRD_PARTY_CEL_CPP_COMMON_VALUES_LEGACY_LIST_VALUE_H_

#include <cstddef>
#include <cstdint>
#include <ostream>
#include <string>

Expand All @@ -35,10 +36,6 @@
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"

namespace google::api::expr::runtime {
class CelList;
}

namespace cel {

class TypeManager;
Expand All @@ -53,9 +50,8 @@ class LegacyListValue final
public:
static constexpr ValueKind kKind = ValueKind::kList;

explicit LegacyListValue(
absl::NullabilityUnknown<const google::api::expr::runtime::CelList*> impl)
: impl_(impl) {}
// NOLINTNEXTLINE(google-explicit-constructor)
explicit LegacyListValue(uintptr_t impl) : impl_(impl) {}

// By default, this creates an empty list whose type is `list(dyn)`. Unless
// you can help it, you should use a more specific typed list value.
Expand Down Expand Up @@ -131,23 +127,24 @@ class LegacyListValue final
absl::Nonnull<google::protobuf::Arena*> arena, absl::Nonnull<Value*> result) const;
using ListValueMixin::Contains;

absl::NullabilityUnknown<const google::api::expr::runtime::CelList*>
cel_list() const {
return impl_;
}

friend void swap(LegacyListValue& lhs, LegacyListValue& rhs) noexcept {
void swap(LegacyListValue& other) noexcept {
using std::swap;
swap(lhs.impl_, rhs.impl_);
swap(impl_, other.impl_);
}

uintptr_t NativeValue() const { return impl_; }

private:
friend class common_internal::ValueMixin<LegacyListValue>;
friend class common_internal::ListValueMixin<LegacyListValue>;

absl::NullabilityUnknown<const google::api::expr::runtime::CelList*> impl_;
uintptr_t impl_;
};

inline void swap(LegacyListValue& lhs, LegacyListValue& rhs) noexcept {
lhs.swap(rhs);
}

inline std::ostream& operator<<(std::ostream& out,
const LegacyListValue& type) {
return out << type.DebugString();
Expand Down
10 changes: 6 additions & 4 deletions common/values/legacy_map_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "common/values/legacy_map_value.h"

#include <cstdint>

#include "absl/base/nullability.h"
#include "absl/log/absl_check.h"
#include "absl/status/status.h"
Expand Down Expand Up @@ -60,15 +62,15 @@ absl::optional<LegacyMapValue> AsLegacyMapValue(const Value& value) {
if (auto custom_map_value = value.AsCustomMap(); custom_map_value) {
NativeTypeId native_type_id = NativeTypeId::Of(*custom_map_value);
if (native_type_id == NativeTypeId::For<CompatMapValue>()) {
return LegacyMapValue(
return LegacyMapValue(reinterpret_cast<uintptr_t>(
static_cast<const google::api::expr::runtime::CelMap*>(
cel::internal::down_cast<const CompatMapValue*>(
(*custom_map_value).operator->())));
(*custom_map_value).operator->()))));
} else if (native_type_id == NativeTypeId::For<MutableCompatMapValue>()) {
return LegacyMapValue(
return LegacyMapValue(reinterpret_cast<uintptr_t>(
static_cast<const google::api::expr::runtime::CelMap*>(
cel::internal::down_cast<const MutableCompatMapValue*>(
(*custom_map_value).operator->())));
(*custom_map_value).operator->()))));
}
}
return absl::nullopt;
Expand Down
26 changes: 12 additions & 14 deletions common/values/legacy_map_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define THIRD_PARTY_CEL_CPP_COMMON_VALUES_LEGACY_MAP_VALUE_H_

#include <cstddef>
#include <cstdint>
#include <ostream>
#include <string>

Expand All @@ -35,10 +36,6 @@
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"

namespace google::api::expr::runtime {
class CelMap;
}

namespace cel {

class TypeManager;
Expand All @@ -53,9 +50,8 @@ class LegacyMapValue final
public:
static constexpr ValueKind kKind = ValueKind::kMap;

explicit LegacyMapValue(
absl::NullabilityUnknown<const google::api::expr::runtime::CelMap*> impl)
: impl_(impl) {}
// NOLINTNEXTLINE(google-explicit-constructor)
explicit LegacyMapValue(uintptr_t impl) : impl_(impl) {}

// By default, this creates an empty map whose type is `map(dyn, dyn)`.
// Unless you can help it, you should use a more specific typed map value.
Expand Down Expand Up @@ -152,22 +148,24 @@ class LegacyMapValue final

absl::StatusOr<absl::Nonnull<ValueIteratorPtr>> NewIterator() const;

absl::Nonnull<const google::api::expr::runtime::CelMap*> cel_map() const {
return impl_;
}

friend void swap(LegacyMapValue& lhs, LegacyMapValue& rhs) noexcept {
void swap(LegacyMapValue& other) noexcept {
using std::swap;
swap(lhs.impl_, rhs.impl_);
swap(impl_, other.impl_);
}

uintptr_t NativeValue() const { return impl_; }

private:
friend class common_internal::ValueMixin<LegacyMapValue>;
friend class common_internal::MapValueMixin<LegacyMapValue>;

absl::NullabilityUnknown<const google::api::expr::runtime::CelMap*> impl_;
uintptr_t impl_;
};

inline void swap(LegacyMapValue& lhs, LegacyMapValue& rhs) noexcept {
lhs.swap(rhs);
}

inline std::ostream& operator<<(std::ostream& out, const LegacyMapValue& type) {
return out << type.DebugString();
}
Expand Down
12 changes: 11 additions & 1 deletion common/values/legacy_struct_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,24 @@
#include "absl/log/absl_check.h"
#include "absl/types/optional.h"
#include "absl/types/variant.h"
#include "base/internal/message_wrapper.h"
#include "common/type.h"
#include "common/value.h"
#include "google/protobuf/message.h"
#include "google/protobuf/message_lite.h"

namespace cel::common_internal {

StructType LegacyStructValue::GetRuntimeType() const {
return MessageType(message_ptr_->GetDescriptor());
if ((message_ptr_ & ::cel::base_internal::kMessageWrapperTagMask) ==
::cel::base_internal::kMessageWrapperTagMessageValue) {
return MessageType(
google::protobuf::DownCastMessage<google::protobuf::Message>(
reinterpret_cast<const google::protobuf::MessageLite*>(
message_ptr_ & ::cel::base_internal::kMessageWrapperPtrMask))
->GetDescriptor());
}
return common_internal::MakeBasicStructType(GetTypeName());
}

bool IsLegacyStructValue(const Value& value) {
Expand Down
44 changes: 16 additions & 28 deletions common/values/legacy_struct_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"

namespace google::api::expr::runtime {
class LegacyTypeInfoApis;
}

namespace cel {

class Value;
Expand All @@ -61,12 +57,8 @@ class LegacyStructValue final
public:
static constexpr ValueKind kKind = ValueKind::kStruct;

LegacyStructValue(
absl::NullabilityUnknown<const google::protobuf::Message*> message_ptr,
absl::NullabilityUnknown<
const google::api::expr::runtime::LegacyTypeInfoApis*>
legacy_type_info)
: message_ptr_(message_ptr), legacy_type_info_(legacy_type_info) {}
LegacyStructValue(uintptr_t message_ptr, uintptr_t type_info)
: message_ptr_(message_ptr), type_info_(type_info) {}

LegacyStructValue(const LegacyStructValue&) = default;
LegacyStructValue& operator=(const LegacyStructValue&) = default;
Expand Down Expand Up @@ -106,6 +98,12 @@ class LegacyStructValue final

bool IsZeroValue() const;

void swap(LegacyStructValue& other) noexcept {
using std::swap;
swap(message_ptr_, other.message_ptr_);
swap(type_info_, other.type_info_);
}

absl::Status GetFieldByName(
absl::string_view name, ProtoWrapperTypeOptions unboxing_options,
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
Expand Down Expand Up @@ -140,32 +138,22 @@ class LegacyStructValue final
absl::Nonnull<int*> count) const;
using StructValueMixin::Qualify;

absl::NullabilityUnknown<const google::protobuf::Message*> message_ptr() const {
return message_ptr_;
}

absl::NullabilityUnknown<
const google::api::expr::runtime::LegacyTypeInfoApis*>
legacy_type_info() const {
return legacy_type_info_;
}
uintptr_t message_ptr() const { return message_ptr_; }

friend void swap(LegacyStructValue& lhs, LegacyStructValue& rhs) noexcept {
using std::swap;
swap(lhs.message_ptr_, rhs.message_ptr_);
swap(lhs.legacy_type_info_, rhs.legacy_type_info_);
}
uintptr_t legacy_type_info() const { return type_info_; }

private:
friend class common_internal::ValueMixin<LegacyStructValue>;
friend class common_internal::StructValueMixin<LegacyStructValue>;

absl::NullabilityUnknown<const google::protobuf::Message*> message_ptr_;
absl::NullabilityUnknown<
const google::api::expr::runtime::LegacyTypeInfoApis*>
legacy_type_info_;
uintptr_t message_ptr_;
uintptr_t type_info_;
};

inline void swap(LegacyStructValue& lhs, LegacyStructValue& rhs) noexcept {
lhs.swap(rhs);
}

inline std::ostream& operator<<(std::ostream& out,
const LegacyStructValue& value) {
return out << value.DebugString();
Expand Down
5 changes: 4 additions & 1 deletion common/values/struct_value_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "absl/strings/cord.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "base/internal/message_wrapper.h"
#include "common/allocator.h"
#include "common/any.h"
#include "common/memory.h"
Expand Down Expand Up @@ -295,7 +297,8 @@ absl::Status ProtoMessageFromValueImpl(
// Deal with legacy values.
if (auto legacy_value = common_internal::AsLegacyStructValue(value);
legacy_value) {
const auto* from_message = legacy_value->message_ptr();
const auto* from_message = reinterpret_cast<const google::protobuf::Message*>(
legacy_value->message_ptr() & base_internal::kMessageWrapperPtrMask);
return ProtoMessageCopy(message, to_desc, from_message);
}

Expand Down
10 changes: 6 additions & 4 deletions eval/public/structs/legacy_type_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include "internal/status_macros.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/message.h"
#include "google/protobuf/message_lite.h"

namespace google::api::expr::runtime {

Expand Down Expand Up @@ -80,9 +79,12 @@ class LegacyStructValueBuilder final : public cel::StructValueBuilder {
return absl::FailedPreconditionError("expected MessageWrapper");
}
auto message_wrapper = message.MessageWrapperOrDie();
return cel::common_internal::LegacyStructValue(
google::protobuf::DownCastMessage<google::protobuf::Message>(message_wrapper.message_ptr()),
message_wrapper.legacy_type_info());
return cel::common_internal::LegacyStructValue{
reinterpret_cast<uintptr_t>(message_wrapper.message_ptr()) |
(message_wrapper.HasFullProto()
? cel::base_internal::kMessageWrapperTagMessageValue
: uintptr_t{0}),
reinterpret_cast<uintptr_t>(message_wrapper.legacy_type_info())};
}

private:
Expand Down
1 change: 1 addition & 0 deletions extensions/protobuf/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ cc_library(
],
deps = [
":type",
"//base/internal:message_wrapper",
"//common:memory",
"//common:type",
"//common:value",
Expand Down
5 changes: 4 additions & 1 deletion extensions/protobuf/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "absl/status/statusor.h"
#include "absl/strings/cord.h"
#include "absl/strings/str_cat.h"
#include "base/internal/message_wrapper.h"
#include "common/memory.h"
#include "common/type.h"
#include "common/value.h"
Expand Down Expand Up @@ -63,7 +64,9 @@ inline absl::Status ProtoMessageFromValue(const Value& value,
if (auto legacy_struct_value =
cel::common_internal::AsLegacyStructValue(value);
legacy_struct_value) {
src_message = legacy_struct_value->message_ptr();
src_message = reinterpret_cast<const google::protobuf::Message*>(
legacy_struct_value->message_ptr() &
cel::base_internal::kMessageWrapperPtrMask);
}
if (auto parsed_message_value = value.AsParsedMessage();
parsed_message_value) {
Expand Down