-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathtype.h
More file actions
396 lines (303 loc) · 10.9 KB
/
Copy pathtype.h
File metadata and controls
396 lines (303 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright 2022 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_BASE_TYPE_H_
#define THIRD_PARTY_CEL_CPP_BASE_TYPE_H_
#include <cstdint>
#include <string>
#include <type_traits>
#include <utility>
#include "absl/base/attributes.h"
#include "absl/base/optimization.h"
#include "absl/hash/hash.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "base/handle.h"
#include "base/internal/data.h"
#include "base/internal/type.h" // IWYU pragma: export
#include "base/kind.h"
#include "internal/casts.h" // IWYU pragma: keep
namespace cel {
class Value;
class EnumType;
class StructType;
class ListType;
class MapType;
class TypeFactory;
class TypeProvider;
class TypeManager;
class WrapperType;
class OpaqueType;
class ValueFactory;
// A representation of a CEL type that enables introspection, for program
// construction, of types.
class Type : public base_internal::Data {
public:
static bool Is(const Type& type ABSL_ATTRIBUTE_UNUSED) { return true; }
static const Type& Cast(const Type& type) { return type; }
// Returns the type kind.
Kind kind() const { return base_internal::Metadata::Kind(*this); }
// Returns the type name, i.e. "list".
absl::string_view name() const;
std::string DebugString() const;
void HashValue(absl::HashState state) const;
bool Equals(const Type& other) const;
template <typename T>
bool Is() const {
static_assert(!std::is_const_v<T>, "T must not be const");
static_assert(!std::is_volatile_v<T>, "T must not be volatile");
static_assert(!std::is_pointer_v<T>, "T must not be a pointer");
static_assert(!std::is_reference_v<T>, "T must not be a reference");
static_assert(std::is_base_of_v<Type, T>, "T must be derived from Type");
return T::Is(*this);
}
template <typename T>
const T& As() const {
static_assert(!std::is_const_v<T>, "T must not be const");
static_assert(!std::is_volatile_v<T>, "T must not be volatile");
static_assert(!std::is_pointer_v<T>, "T must not be a pointer");
static_assert(!std::is_reference_v<T>, "T must not be a reference");
static_assert(std::is_base_of_v<Type, T>, "T must be derived from Type");
return T::Cast(*this);
}
template <typename Sink>
friend void AbslStringify(Sink& sink, const Type& type) {
sink.Append(type.DebugString());
}
private:
friend class TypeManager;
friend class EnumType;
friend class StructType;
friend class ListType;
friend class MapType;
template <Kind K>
friend class base_internal::SimpleType;
friend class WrapperType;
friend class base_internal::TypeHandle;
friend class OpaqueType;
// This is used by TypeManager to determine whether a type has any known
// aliases. This is currently only used for JSON-like types. Pretend this
// doesn't exist.
absl::Span<const absl::string_view> aliases() const;
static bool Equals(const Type& lhs, const Type& rhs, Kind kind);
static bool Equals(const Type& lhs, const Type& rhs) {
if (&lhs == &rhs) {
return true;
}
Kind lhs_kind = lhs.kind();
return lhs_kind == rhs.kind() && Equals(lhs, rhs, lhs_kind);
}
static void HashValue(const Type& type, Kind kind, absl::HashState state);
static void HashValue(const Type& type, absl::HashState state) {
HashValue(type, type.kind(), std::move(state));
}
Type() = default;
Type(const Type&) = default;
Type(Type&&) = default;
Type& operator=(const Type&) = default;
Type& operator=(Type&&) = default;
};
} // namespace cel
// -----------------------------------------------------------------------------
// Internal implementation details.
namespace cel {
namespace base_internal {
class TypeMetadata final {
public:
TypeMetadata() = delete;
static void Ref(const Type& type);
static void Unref(const Type& type);
static bool IsReferenceCounted(const Type& type);
};
class TypeHandle final {
public:
using base_type = Type;
TypeHandle() = default;
template <typename T, typename... Args>
explicit TypeHandle(InPlaceStoredInline<T>, Args&&... args) {
data_.ConstructInline<T>(std::forward<Args>(args)...);
}
explicit TypeHandle(InPlaceArenaAllocated, Type& arg) {
data_.ConstructArenaAllocated(arg);
}
explicit TypeHandle(InPlaceReferenceCounted, Type& arg) {
data_.ConstructReferenceCounted(arg);
}
TypeHandle(const TypeHandle& other) { CopyFrom(other); }
TypeHandle(TypeHandle&& other) { MoveFrom(other); }
~TypeHandle() { Destruct(); }
TypeHandle& operator=(const TypeHandle& other) {
if (ABSL_PREDICT_TRUE(this != &other)) {
CopyAssign(other);
}
return *this;
}
TypeHandle& operator=(TypeHandle&& other) {
if (ABSL_PREDICT_TRUE(this != &other)) {
MoveAssign(other);
}
return *this;
}
Type* get() const { return static_cast<Type*>(data_.get()); }
explicit operator bool() const { return !data_.IsNull(); }
bool Equals(const TypeHandle& other) const;
void HashValue(absl::HashState state) const;
private:
static bool Equals(const Type& lhs, const Type& rhs, Kind kind);
static void HashValue(const Type& type, Kind kind, absl::HashState state);
void CopyFrom(const TypeHandle& other);
void MoveFrom(TypeHandle& other);
void CopyAssign(const TypeHandle& other);
void MoveAssign(TypeHandle& other);
void Ref() const { data_.Ref(); }
void Unref() const {
if (data_.Unref()) {
Delete();
}
}
void Destruct();
void Delete() const;
AnyType data_;
};
template <typename H>
H AbslHashValue(H state, const TypeHandle& handle) {
handle.HashValue(absl::HashState::Create(&state));
return state;
}
inline bool operator==(const TypeHandle& lhs, const TypeHandle& rhs) {
return lhs.Equals(rhs);
}
inline bool operator!=(const TypeHandle& lhs, const TypeHandle& rhs) {
return !operator==(lhs, rhs);
}
// Specialization for Type providing the implementation to `Handle`.
template <>
struct HandleTraits<Type> {
using handle_type = TypeHandle;
};
// Partial specialization for `Handle` for all classes derived from Type.
template <typename T>
struct HandleTraits<T, std::enable_if_t<(std::is_base_of_v<Type, T> &&
!std::is_same_v<Type, T>)>>
final : public HandleTraits<Type> {};
template <Kind K>
struct SimpleTypeName;
template <>
struct SimpleTypeName<Kind::kNullType> {
static constexpr absl::string_view value = "null_type";
};
template <>
struct SimpleTypeName<Kind::kError> {
static constexpr absl::string_view value = "*error*";
};
template <>
struct SimpleTypeName<Kind::kDyn> {
static constexpr absl::string_view value = "dyn";
};
template <>
struct SimpleTypeName<Kind::kAny> {
static constexpr absl::string_view value = "google.protobuf.Any";
};
template <>
struct SimpleTypeName<Kind::kBool> {
static constexpr absl::string_view value = "bool";
};
template <>
struct SimpleTypeName<Kind::kInt> {
static constexpr absl::string_view value = "int";
};
template <>
struct SimpleTypeName<Kind::kUint> {
static constexpr absl::string_view value = "uint";
};
template <>
struct SimpleTypeName<Kind::kDouble> {
static constexpr absl::string_view value = "double";
};
template <>
struct SimpleTypeName<Kind::kBytes> {
static constexpr absl::string_view value = "bytes";
};
template <>
struct SimpleTypeName<Kind::kString> {
static constexpr absl::string_view value = "string";
};
template <>
struct SimpleTypeName<Kind::kDuration> {
static constexpr absl::string_view value = "google.protobuf.Duration";
};
template <>
struct SimpleTypeName<Kind::kTimestamp> {
static constexpr absl::string_view value = "google.protobuf.Timestamp";
};
template <>
struct SimpleTypeName<Kind::kType> {
static constexpr absl::string_view value = "type";
};
template <>
struct SimpleTypeName<Kind::kUnknown> {
static constexpr absl::string_view value = "*unknown*";
};
template <Kind K>
class SimpleType : public Type, public InlineData {
public:
static constexpr Kind kKind = K;
static constexpr absl::string_view kName = SimpleTypeName<K>::value;
static bool Is(const Type& type) { return type.kind() == kKind; }
using Type::Is;
constexpr SimpleType() : InlineData(kMetadata) {}
SimpleType(const SimpleType&) = default;
SimpleType(SimpleType&&) = default;
SimpleType& operator=(const SimpleType&) = default;
SimpleType& operator=(SimpleType&&) = default;
constexpr Kind kind() const { return kKind; }
constexpr absl::string_view name() const { return kName; }
std::string DebugString() const { return std::string(name()); }
private:
friend class TypeHandle;
static constexpr uintptr_t kMetadata =
kStoredInline | kTrivial | (static_cast<uintptr_t>(kKind) << kKindShift);
};
template <>
struct TypeTraits<Type> {
using type = Type;
using value_type = Value;
};
} // namespace base_internal
CEL_INTERNAL_TYPE_DECL(Type);
} // namespace cel
#define CEL_INTERNAL_SIMPLE_TYPE_MEMBERS(type_class, value_class) \
private: \
friend class value_class; \
friend class TypeFactory; \
friend class base_internal::TypeHandle; \
template <typename T, typename U> \
friend class base_internal::SimpleValue; \
template <size_t Size, size_t Align> \
friend struct base_internal::AnyData; \
\
ABSL_ATTRIBUTE_PURE_FUNCTION static const Handle<type_class>& Get(); \
\
type_class() = default; \
type_class(const type_class&) = default; \
type_class(type_class&&) = default; \
type_class& operator=(const type_class&) = default; \
type_class& operator=(type_class&&) = default
#define CEL_INTERNAL_SIMPLE_TYPE_STANDALONES(type_class) \
static_assert(std::is_trivially_copyable_v<type_class>, \
#type_class " must be trivially copyable"); \
static_assert(std::is_trivially_destructible_v<type_class>, \
#type_class " must be trivially destructible"); \
\
CEL_INTERNAL_TYPE_DECL(type_class)
#endif // THIRD_PARTY_CEL_CPP_BASE_TYPE_H_