From 7edd227d6be8ebffa8a9677f365839532ac9987e Mon Sep 17 00:00:00 2001 From: kuat Date: Mon, 6 Jun 2022 22:46:32 +0000 Subject: [PATCH] GCC fixes. PiperOrigin-RevId: 453300107 --- base/internal/value.post.h | 10 ++----- base/value.h | 6 ++-- eval/public/cel_function_adapter.h | 11 ++++--- eval/public/cel_function_adapter_impl.h | 39 ++++++++++++------------- eval/public/cel_number.h | 33 +++++++++++---------- eval/public/cel_type_registry.cc | 9 +++--- eval/public/cel_value.h | 3 +- eval/public/cel_value_internal.h | 1 - 8 files changed, 54 insertions(+), 58 deletions(-) diff --git a/base/internal/value.post.h b/base/internal/value.post.h index 7dac549bc..2e78ba07a 100644 --- a/base/internal/value.post.h +++ b/base/internal/value.post.h @@ -311,17 +311,13 @@ class ValueHandleBase { Value* operator->() const { return std::addressof(get()); } // Called by internal accessors `base_internal::IsXHandle`. - constexpr bool IsManaged() const { - return (vptr() & kValueHandleManaged) != 0; - } + bool IsManaged() const { return (vptr() & kValueHandleManaged) != 0; } // Called by internal accessors `base_internal::IsXHandle`. - constexpr bool IsUnmanaged() const { - return (vptr() & kValueHandleUnmanaged) != 0; - } + bool IsUnmanaged() const { return (vptr() & kValueHandleUnmanaged) != 0; } // Called by internal accessors `base_internal::IsXHandle`. - constexpr bool IsInlined() const { return (vptr() & kValueHandleBits) == 0; } + bool IsInlined() const { return (vptr() & kValueHandleBits) == 0; } // Called by `Transient` and `Persistent` to implement the same function. template diff --git a/base/value.h b/base/value.h index 1fcb16b9a..2f2f1be7a 100644 --- a/base/value.h +++ b/base/value.h @@ -150,6 +150,10 @@ class NullValue final : public Value, public base_internal::ResourceInlined { // Note GCC does not consider a friend member as a member of a friend. ABSL_ATTRIBUTE_PURE_FUNCTION static const NullValue& Get(); + bool Equals(const Value& other) const override; + + void HashValue(absl::HashState state) const override; + private: friend class ValueFactory; template @@ -169,8 +173,6 @@ class NullValue final : public Value, public base_internal::ResourceInlined { // See comments for respective member functions on `Value`. void CopyTo(Value& address) const override; void MoveTo(Value& address) override; - bool Equals(const Value& other) const override; - void HashValue(absl::HashState state) const override; }; class ErrorValue final : public Value, public base_internal::ResourceInlined { diff --git a/eval/public/cel_function_adapter.h b/eval/public/cel_function_adapter.h index 2df1229dc..744668f87 100644 --- a/eval/public/cel_function_adapter.h +++ b/eval/public/cel_function_adapter.h @@ -19,12 +19,11 @@ namespace internal { struct ProtoAdapterTypeCodeMatcher { template constexpr std::optional type_code() { - return internal::TypeCodeMatcher().type_code(); - } - - template <> - constexpr std::optional type_code() { - return CelValue::Type::kMessage; + if constexpr (std::is_same_v) { + return CelValue::Type::kMessage; + } else { + return internal::TypeCodeMatcher().type_code(); + } } }; diff --git a/eval/public/cel_function_adapter_impl.h b/eval/public/cel_function_adapter_impl.h index 59b5872a5..ac44f8fad 100644 --- a/eval/public/cel_function_adapter_impl.h +++ b/eval/public/cel_function_adapter_impl.h @@ -35,20 +35,19 @@ namespace internal { struct TypeCodeMatcher { template constexpr std::optional type_code() { - int index = CelValue::IndexOf::value; - if (index < 0) return {}; - CelValue::Type arg_type = static_cast(index); - if (arg_type >= CelValue::Type::kAny) { - return {}; + if constexpr (std::is_same_v) { + // A bit of a trick - to pass Any kind of value, we use generic CelValue + // parameters. + return CelValue::Type::kAny; + } else { + int index = CelValue::IndexOf::value; + if (index < 0) return {}; + CelValue::Type arg_type = static_cast(index); + if (arg_type >= CelValue::Type::kAny) { + return {}; + } + return arg_type; } - return arg_type; - } - - // A bit of a trick - to pass Any kind of value, we use generic CelValue - // parameters. - template <> - constexpr std::optional type_code() { - return CelValue::Type::kAny; } }; @@ -82,14 +81,12 @@ struct ValueConverterBase { // Value to native uwraps a CelValue to a native type. template bool ValueToNative(CelValue value, T* result) { - return value.GetValue(result); - } - - // Specialization for CelValue (any typed) - template <> - bool ValueToNative(CelValue value, CelValue* result) { - *result = std::move(value); - return true; + if constexpr (std::is_same_v) { + *result = std::move(value); + return true; + } else { + return value.GetValue(result); + } } // Native to value wraps a native return type to a CelValue. diff --git a/eval/public/cel_number.h b/eval/public/cel_number.h index 54c76a057..f0b591009 100644 --- a/eval/public/cel_number.h +++ b/eval/public/cel_number.h @@ -45,6 +45,8 @@ constexpr double kMaxDoubleRepresentableAsInt = constexpr double kMaxDoubleRepresentableAsUint = static_cast(kUint64Max - RoundingError()); +#define CEL_ABSL_VISIT_CONSTEXPR + namespace internal { using NumberVariant = absl::variant; @@ -169,15 +171,15 @@ struct IntCompareVisitor { struct CompareVisitor { explicit constexpr CompareVisitor(NumberVariant rhs) : rhs(rhs) {} - constexpr ComparisonResult operator()(double v) { + CEL_ABSL_VISIT_CONSTEXPR ComparisonResult operator()(double v) { return absl::visit(DoubleCompareVisitor(v), rhs); } - constexpr ComparisonResult operator()(uint64_t v) { + CEL_ABSL_VISIT_CONSTEXPR ComparisonResult operator()(uint64_t v) { return absl::visit(UintCompareVisitor(v), rhs); } - constexpr ComparisonResult operator()(int64_t v) { + CEL_ABSL_VISIT_CONSTEXPR ComparisonResult operator()(int64_t v) { return absl::visit(IntCompareVisitor(v), rhs); } NumberVariant rhs; @@ -233,66 +235,67 @@ class CelNumber { constexpr explicit CelNumber(uint64_t uint_value) : value_(uint_value) {} // Return a double representation of the value. - constexpr double AsDouble() const { + CEL_ABSL_VISIT_CONSTEXPR double AsDouble() const { return absl::visit(internal::ConversionVisitor(), value_); } // Return signed int64_t representation for the value. // Caller must guarantee the underlying value is representatble as an // int. - constexpr int64_t AsInt() const { + CEL_ABSL_VISIT_CONSTEXPR int64_t AsInt() const { return absl::visit(internal::ConversionVisitor(), value_); } // Return unsigned int64_t representation for the value. // Caller must guarantee the underlying value is representable as an // uint. - constexpr uint64_t AsUint() const { + CEL_ABSL_VISIT_CONSTEXPR uint64_t AsUint() const { return absl::visit(internal::ConversionVisitor(), value_); } // For key lookups, check if the conversion to signed int is lossless. - constexpr bool LosslessConvertibleToInt() const { + CEL_ABSL_VISIT_CONSTEXPR bool LosslessConvertibleToInt() const { return absl::visit(internal::LosslessConvertibleToIntVisitor(), value_); } // For key lookups, check if the conversion to unsigned int is lossless. - constexpr bool LosslessConvertibleToUint() const { + CEL_ABSL_VISIT_CONSTEXPR bool LosslessConvertibleToUint() const { return absl::visit(internal::LosslessConvertibleToUintVisitor(), value_); } - constexpr bool operator<(CelNumber other) const { + CEL_ABSL_VISIT_CONSTEXPR bool operator<(CelNumber other) const { return Compare(other) == internal::ComparisonResult::kLesser; } - constexpr bool operator<=(CelNumber other) const { + CEL_ABSL_VISIT_CONSTEXPR bool operator<=(CelNumber other) const { internal::ComparisonResult cmp = Compare(other); return cmp != internal::ComparisonResult::kGreater && cmp != internal::ComparisonResult::kNanInequal; } - constexpr bool operator>(CelNumber other) const { + CEL_ABSL_VISIT_CONSTEXPR bool operator>(CelNumber other) const { return Compare(other) == internal::ComparisonResult::kGreater; } - constexpr bool operator>=(CelNumber other) const { + CEL_ABSL_VISIT_CONSTEXPR bool operator>=(CelNumber other) const { internal::ComparisonResult cmp = Compare(other); return cmp != internal::ComparisonResult::kLesser && cmp != internal::ComparisonResult::kNanInequal; } - constexpr bool operator==(CelNumber other) const { + CEL_ABSL_VISIT_CONSTEXPR bool operator==(CelNumber other) const { return Compare(other) == internal::ComparisonResult::kEqual; } - constexpr bool operator!=(CelNumber other) const { + CEL_ABSL_VISIT_CONSTEXPR bool operator!=(CelNumber other) const { return Compare(other) != internal::ComparisonResult::kEqual; } private: internal::NumberVariant value_; - constexpr internal::ComparisonResult Compare(CelNumber other) const { + CEL_ABSL_VISIT_CONSTEXPR internal::ComparisonResult Compare( + CelNumber other) const { return absl::visit(internal::CompareVisitor(other.value_), value_); } }; diff --git a/eval/public/cel_type_registry.cc b/eval/public/cel_type_registry.cc index f2925b09d..0f42f7e5a 100644 --- a/eval/public/cel_type_registry.cc +++ b/eval/public/cel_type_registry.cc @@ -55,11 +55,10 @@ struct EnumAdderT { void AddEnum(DescriptorSet&) {} template - void AddEnum(EnumMap&) {} - - template <> - void AddEnum(EnumMap& map) { - map["google.protobuf.NullValue"] = {{"NULL_VALUE", 0}}; + void AddEnum(EnumMap& map) { + if constexpr (std::is_same_v) { + map["google.protobuf.NullValue"] = {{"NULL_VALUE", 0}}; + } } }; diff --git a/eval/public/cel_value.h b/eval/public/cel_value.h index 7c6d5da7c..fe5a6f1dd 100644 --- a/eval/public/cel_value.h +++ b/eval/public/cel_value.h @@ -116,7 +116,8 @@ class CelValue { // absl::variant. using NullType = absl::monostate; - using MessageWrapper = MessageWrapper; + // GCC: fully qualified to avoid change of meaning error. + using MessageWrapper = google::api::expr::runtime::MessageWrapper; private: // CelError MUST BE the last in the declaration - it is a ceiling for Type diff --git a/eval/public/cel_value_internal.h b/eval/public/cel_value_internal.h index af1a5d949..301363b8c 100644 --- a/eval/public/cel_value_internal.h +++ b/eval/public/cel_value_internal.h @@ -94,7 +94,6 @@ struct MessageVisitAdapter { return op(arg); } - template <> T operator()(const MessageWrapper& wrapper) { ABSL_ASSERT(wrapper.HasFullProto()); return op(cel::internal::down_cast(