Skip to content

Commit

Permalink
GCC fixes.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 453300107
  • Loading branch information
kyessenov committed Jun 7, 2022
1 parent 71c1034 commit 7edd227
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 58 deletions.
10 changes: 3 additions & 7 deletions base/internal/value.post.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T>
Expand Down
6 changes: 4 additions & 2 deletions base/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename T>
Expand All @@ -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 {
Expand Down
11 changes: 5 additions & 6 deletions eval/public/cel_function_adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ namespace internal {
struct ProtoAdapterTypeCodeMatcher {
template <typename T>
constexpr std::optional<CelValue::Type> type_code() {
return internal::TypeCodeMatcher().type_code<T>();
}

template <>
constexpr std::optional<CelValue::Type> type_code<const google::protobuf::Message*>() {
return CelValue::Type::kMessage;
if constexpr (std::is_same_v<T, const google::protobuf::Message*>) {
return CelValue::Type::kMessage;
} else {
return internal::TypeCodeMatcher().type_code<T>();
}
}
};

Expand Down
39 changes: 18 additions & 21 deletions eval/public/cel_function_adapter_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,19 @@ namespace internal {
struct TypeCodeMatcher {
template <typename T>
constexpr std::optional<CelValue::Type> type_code() {
int index = CelValue::IndexOf<T>::value;
if (index < 0) return {};
CelValue::Type arg_type = static_cast<CelValue::Type>(index);
if (arg_type >= CelValue::Type::kAny) {
return {};
if constexpr (std::is_same_v<T, CelValue>) {
// 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<T>::value;
if (index < 0) return {};
CelValue::Type arg_type = static_cast<CelValue::Type>(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<CelValue::Type> type_code<CelValue>() {
return CelValue::Type::kAny;
}
};

Expand Down Expand Up @@ -82,14 +81,12 @@ struct ValueConverterBase {
// Value to native uwraps a CelValue to a native type.
template <typename T>
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<T, CelValue>) {
*result = std::move(value);
return true;
} else {
return value.GetValue(result);
}
}

// Native to value wraps a native return type to a CelValue.
Expand Down
33 changes: 18 additions & 15 deletions eval/public/cel_number.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ constexpr double kMaxDoubleRepresentableAsInt =
constexpr double kMaxDoubleRepresentableAsUint =
static_cast<double>(kUint64Max - RoundingError<uint64_t>());

#define CEL_ABSL_VISIT_CONSTEXPR

namespace internal {

using NumberVariant = absl::variant<double, uint64_t, int64_t>;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<double>(), 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<int64_t>(), 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<uint64_t>(), 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_);
}
};
Expand Down
9 changes: 4 additions & 5 deletions eval/public/cel_type_registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,10 @@ struct EnumAdderT {
void AddEnum(DescriptorSet&) {}

template <typename EnumT>
void AddEnum(EnumMap&) {}

template <>
void AddEnum<google::protobuf::NullValue>(EnumMap& map) {
map["google.protobuf.NullValue"] = {{"NULL_VALUE", 0}};
void AddEnum(EnumMap& map) {
if constexpr (std::is_same_v<EnumT, google::protobuf::NullValue>) {
map["google.protobuf.NullValue"] = {{"NULL_VALUE", 0}};
}
}
};

Expand Down
3 changes: 2 additions & 1 deletion eval/public/cel_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion eval/public/cel_value_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<const google::protobuf::Message*>(
Expand Down

0 comments on commit 7edd227

Please sign in to comment.