Skip to content

Commit 51d9fd4

Browse files
jckingcopybara-github
authored andcommitted
Migrate Type::kind() to TypeKind
PiperOrigin-RevId: 534168362
1 parent 7d3b4de commit 51d9fd4

42 files changed

Lines changed: 325 additions & 321 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

base/BUILD

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ cc_library(
6565
srcs = ["kind.cc"],
6666
hdrs = ["kind.h"],
6767
deps = [
68-
"@com_google_absl//absl/base",
6968
"@com_google_absl//absl/base:core_headers",
70-
"@com_google_absl//absl/log:absl_check",
7169
"@com_google_absl//absl/strings",
7270
],
7371
)

base/internal/data.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ class HeapData /* : public Data */ {
184184
: metadata_and_reference_count_(static_cast<uintptr_t>(kind)
185185
<< kKindShift) {}
186186

187+
explicit HeapData(TypeKind kind) : HeapData(TypeKindToKind(kind)) {}
188+
187189
private:
188190
// Called by Arena-based memory managers to determine whether we actually need
189191
// our destructor called. Subclasses should override this if they want their

base/internal/type.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class LegacyMapType;
4545
class ModernMapType;
4646
struct FieldIdFactory;
4747

48-
template <Kind K>
48+
template <TypeKind K>
4949
class SimpleType;
5050
template <typename T, typename U>
5151
class SimpleValue;
@@ -108,7 +108,7 @@ struct TypeTraits;
108108
static_assert(!::std::is_abstract_v<derived>, "this must not be abstract"); \
109109
\
110110
bool derived::Is(const ::cel::Type& type) { \
111-
return type.kind() == ::cel::Kind::k##base && \
111+
return type.kind() == ::cel::TypeKind::k##base && \
112112
::cel::base_internal::Get##base##TypeTypeId( \
113113
static_cast<const ::cel::base##Type&>(type)) == \
114114
::cel::internal::TypeId<derived>(); \

base/kind.cc

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@
1616

1717
namespace cel {
1818

19-
bool KindIsTypeKind(Kind kind) {
20-
// Currently all Kind are valid TypeKind.
21-
return true;
22-
}
23-
24-
bool KindIsValueKind(Kind kind) {
25-
return kind != Kind::kWrapper && kind != Kind::kDyn && kind != Kind::kAny;
26-
}
27-
2819
absl::string_view KindToString(Kind kind) {
2920
switch (kind) {
3021
case Kind::kNullType:

base/kind.h

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
#include <type_traits>
1919

2020
#include "absl/base/attributes.h"
21-
#include "absl/base/casts.h"
22-
#include "absl/log/absl_check.h"
21+
#include "absl/base/macros.h"
2322
#include "absl/strings/string_view.h"
2423

2524
namespace cel {
@@ -103,21 +102,30 @@ enum class TypeKind : std::underlying_type_t<Kind> {
103102
static_cast<int>(Kind::kNotForUseWithExhaustiveSwitchStatements),
104103
};
105104

106-
inline Kind TypeKindToKind(TypeKind kind) { return absl::bit_cast<Kind>(kind); }
105+
constexpr Kind TypeKindToKind(TypeKind kind) {
106+
return static_cast<Kind>(static_cast<std::underlying_type_t<TypeKind>>(kind));
107+
}
107108

108-
ABSL_ATTRIBUTE_PURE_FUNCTION bool KindIsTypeKind(Kind kind);
109+
constexpr bool KindIsTypeKind(Kind kind ABSL_ATTRIBUTE_UNUSED) {
110+
// Currently all Kind are valid TypeKind.
111+
return true;
112+
}
109113

110-
inline bool operator==(Kind lhs, TypeKind rhs) {
114+
constexpr bool operator==(Kind lhs, TypeKind rhs) {
111115
return lhs == TypeKindToKind(rhs);
112116
}
113117

114-
inline bool operator==(TypeKind lhs, Kind rhs) {
118+
constexpr bool operator==(TypeKind lhs, Kind rhs) {
115119
return TypeKindToKind(lhs) == rhs;
116120
}
117121

118-
inline bool operator!=(Kind lhs, TypeKind rhs) { return !operator==(lhs, rhs); }
122+
constexpr bool operator!=(Kind lhs, TypeKind rhs) {
123+
return !operator==(lhs, rhs);
124+
}
119125

120-
inline bool operator!=(TypeKind lhs, Kind rhs) { return !operator==(lhs, rhs); }
126+
constexpr bool operator!=(TypeKind lhs, Kind rhs) {
127+
return !operator==(lhs, rhs);
128+
}
121129

122130
// `ValueKind` is a subset of `Kind`, representing all valid `Kind` for `Value`.
123131
// All `ValueKind` are valid `Kind`, but it is not guaranteed that all `Kind`
@@ -155,41 +163,44 @@ enum class ValueKind : std::underlying_type_t<Kind> {
155163
static_cast<int>(Kind::kNotForUseWithExhaustiveSwitchStatements),
156164
};
157165

158-
inline Kind ValueKindToKind(ValueKind kind) {
159-
return absl::bit_cast<Kind>(kind);
166+
constexpr Kind ValueKindToKind(ValueKind kind) {
167+
return static_cast<Kind>(
168+
static_cast<std::underlying_type_t<ValueKind>>(kind));
160169
}
161170

162-
ABSL_ATTRIBUTE_PURE_FUNCTION bool KindIsValueKind(Kind kind);
171+
constexpr bool KindIsValueKind(Kind kind) {
172+
return kind != Kind::kWrapper && kind != Kind::kDyn && kind != Kind::kAny;
173+
}
163174

164-
inline bool operator==(Kind lhs, ValueKind rhs) {
175+
constexpr bool operator==(Kind lhs, ValueKind rhs) {
165176
return lhs == ValueKindToKind(rhs);
166177
}
167178

168-
inline bool operator==(ValueKind lhs, Kind rhs) {
179+
constexpr bool operator==(ValueKind lhs, Kind rhs) {
169180
return ValueKindToKind(lhs) == rhs;
170181
}
171182

172-
inline bool operator==(TypeKind lhs, ValueKind rhs) {
183+
constexpr bool operator==(TypeKind lhs, ValueKind rhs) {
173184
return TypeKindToKind(lhs) == ValueKindToKind(rhs);
174185
}
175186

176-
inline bool operator==(ValueKind lhs, TypeKind rhs) {
187+
constexpr bool operator==(ValueKind lhs, TypeKind rhs) {
177188
return ValueKindToKind(lhs) == TypeKindToKind(rhs);
178189
}
179190

180-
inline bool operator!=(Kind lhs, ValueKind rhs) {
191+
constexpr bool operator!=(Kind lhs, ValueKind rhs) {
181192
return !operator==(lhs, rhs);
182193
}
183194

184-
inline bool operator!=(ValueKind lhs, Kind rhs) {
195+
constexpr bool operator!=(ValueKind lhs, Kind rhs) {
185196
return !operator==(lhs, rhs);
186197
}
187198

188-
inline bool operator!=(TypeKind lhs, ValueKind rhs) {
199+
constexpr bool operator!=(TypeKind lhs, ValueKind rhs) {
189200
return !operator==(lhs, rhs);
190201
}
191202

192-
inline bool operator!=(ValueKind lhs, TypeKind rhs) {
203+
constexpr bool operator!=(ValueKind lhs, TypeKind rhs) {
193204
return !operator==(lhs, rhs);
194205
}
195206

@@ -203,14 +214,15 @@ inline absl::string_view ValueKindToString(ValueKind kind) {
203214
return KindToString(ValueKindToKind(kind));
204215
}
205216

206-
inline TypeKind KindToTypeKind(Kind kind) {
207-
ABSL_DCHECK(KindIsTypeKind(kind)) << KindToString(kind);
208-
return absl::bit_cast<TypeKind>(kind);
217+
constexpr TypeKind KindToTypeKind(Kind kind) {
218+
ABSL_ASSERT(KindIsTypeKind(kind));
219+
return static_cast<TypeKind>(static_cast<std::underlying_type_t<Kind>>(kind));
209220
}
210221

211-
inline ValueKind KindToValueKind(Kind kind) {
212-
ABSL_DCHECK(KindIsValueKind(kind)) << KindToString(kind);
213-
return absl::bit_cast<ValueKind>(kind);
222+
constexpr ValueKind KindToValueKind(Kind kind) {
223+
ABSL_ASSERT(KindIsValueKind(kind));
224+
return static_cast<ValueKind>(
225+
static_cast<std::underlying_type_t<Kind>>(kind));
214226
}
215227

216228
} // namespace cel

0 commit comments

Comments
 (0)