Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
447 lines (424 loc) · 14.8 KB

File metadata and controls

447 lines (424 loc) · 14.8 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// 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.
#include "base/type.h"
#include <algorithm>
#include <string>
#include <utility>
#include "absl/base/optimization.h"
#include "absl/strings/string_view.h"
#include "base/internal/data.h"
#include "base/types/any_type.h"
#include "base/types/bool_type.h"
#include "base/types/bytes_type.h"
#include "base/types/double_type.h"
#include "base/types/duration_type.h"
#include "base/types/dyn_type.h"
#include "base/types/enum_type.h"
#include "base/types/error_type.h"
#include "base/types/int_type.h"
#include "base/types/list_type.h"
#include "base/types/map_type.h"
#include "base/types/null_type.h"
#include "base/types/opaque_type.h"
#include "base/types/string_type.h"
#include "base/types/struct_type.h"
#include "base/types/timestamp_type.h"
#include "base/types/type_type.h"
#include "base/types/uint_type.h"
#include "base/types/unknown_type.h"
#include "base/types/wrapper_type.h"
namespace cel {
CEL_INTERNAL_TYPE_IMPL(Type);
absl::string_view Type::name() const {
switch (kind()) {
case Kind::kNullType:
return static_cast<const NullType*>(this)->name();
case Kind::kError:
return static_cast<const ErrorType*>(this)->name();
case Kind::kDyn:
return static_cast<const DynType*>(this)->name();
case Kind::kAny:
return static_cast<const AnyType*>(this)->name();
case Kind::kType:
return static_cast<const TypeType*>(this)->name();
case Kind::kBool:
return static_cast<const BoolType*>(this)->name();
case Kind::kInt:
return static_cast<const IntType*>(this)->name();
case Kind::kUint:
return static_cast<const UintType*>(this)->name();
case Kind::kDouble:
return static_cast<const DoubleType*>(this)->name();
case Kind::kString:
return static_cast<const StringType*>(this)->name();
case Kind::kBytes:
return static_cast<const BytesType*>(this)->name();
case Kind::kEnum:
return static_cast<const EnumType*>(this)->name();
case Kind::kDuration:
return static_cast<const DurationType*>(this)->name();
case Kind::kTimestamp:
return static_cast<const TimestampType*>(this)->name();
case Kind::kList:
return static_cast<const ListType*>(this)->name();
case Kind::kMap:
return static_cast<const MapType*>(this)->name();
case Kind::kStruct:
return static_cast<const StructType*>(this)->name();
case Kind::kUnknown:
return static_cast<const UnknownType*>(this)->name();
case Kind::kWrapper:
return static_cast<const WrapperType*>(this)->name();
case Kind::kOpaque:
return static_cast<const OpaqueType*>(this)->name();
default:
return "*unreachable*";
}
}
absl::Span<const absl::string_view> Type::aliases() const {
switch (kind()) {
case Kind::kDyn:
return static_cast<const DynType*>(this)->aliases();
case Kind::kList:
return static_cast<const ListType*>(this)->aliases();
case Kind::kMap:
return static_cast<const MapType*>(this)->aliases();
case Kind::kWrapper:
return static_cast<const WrapperType*>(this)->aliases();
default:
// Everything else does not support aliases.
return absl::Span<const absl::string_view>();
}
}
std::string Type::DebugString() const {
switch (kind()) {
case Kind::kNullType:
return static_cast<const NullType*>(this)->DebugString();
case Kind::kError:
return static_cast<const ErrorType*>(this)->DebugString();
case Kind::kDyn:
return static_cast<const DynType*>(this)->DebugString();
case Kind::kAny:
return static_cast<const AnyType*>(this)->DebugString();
case Kind::kType:
return static_cast<const TypeType*>(this)->DebugString();
case Kind::kBool:
return static_cast<const BoolType*>(this)->DebugString();
case Kind::kInt:
return static_cast<const IntType*>(this)->DebugString();
case Kind::kUint:
return static_cast<const UintType*>(this)->DebugString();
case Kind::kDouble:
return static_cast<const DoubleType*>(this)->DebugString();
case Kind::kString:
return static_cast<const StringType*>(this)->DebugString();
case Kind::kBytes:
return static_cast<const BytesType*>(this)->DebugString();
case Kind::kEnum:
return static_cast<const EnumType*>(this)->DebugString();
case Kind::kDuration:
return static_cast<const DurationType*>(this)->DebugString();
case Kind::kTimestamp:
return static_cast<const TimestampType*>(this)->DebugString();
case Kind::kList:
return static_cast<const ListType*>(this)->DebugString();
case Kind::kMap:
return static_cast<const MapType*>(this)->DebugString();
case Kind::kStruct:
return static_cast<const StructType*>(this)->DebugString();
case Kind::kUnknown:
return static_cast<const UnknownType*>(this)->DebugString();
case Kind::kWrapper:
return static_cast<const WrapperType*>(this)->DebugString();
case Kind::kOpaque:
return static_cast<const OpaqueType*>(this)->DebugString();
default:
return "*unreachable*";
}
}
bool Type::Equals(const Type& lhs, const Type& rhs, Kind kind) {
if (&lhs == &rhs) {
return true;
}
switch (kind) {
case Kind::kNullType:
return true;
case Kind::kError:
return true;
case Kind::kDyn:
return true;
case Kind::kAny:
return true;
case Kind::kType:
return true;
case Kind::kBool:
return true;
case Kind::kInt:
return true;
case Kind::kUint:
return true;
case Kind::kDouble:
return true;
case Kind::kString:
return true;
case Kind::kBytes:
return true;
case Kind::kEnum:
return static_cast<const EnumType&>(lhs).name() ==
static_cast<const EnumType&>(rhs).name();
case Kind::kDuration:
return true;
case Kind::kTimestamp:
return true;
case Kind::kList:
return static_cast<const ListType&>(lhs).element() ==
static_cast<const ListType&>(rhs).element();
case Kind::kMap:
return static_cast<const MapType&>(lhs).key() ==
static_cast<const MapType&>(rhs).key() &&
static_cast<const MapType&>(lhs).value() ==
static_cast<const MapType&>(rhs).value();
case Kind::kStruct:
return static_cast<const StructType&>(lhs).name() ==
static_cast<const StructType&>(rhs).name();
case Kind::kUnknown:
return true;
case Kind::kWrapper:
return static_cast<const WrapperType&>(lhs).wrapped() ==
static_cast<const WrapperType&>(rhs).wrapped();
case Kind::kOpaque: {
if (static_cast<const OpaqueType&>(lhs).name() !=
static_cast<const OpaqueType&>(rhs).name()) {
return false;
}
const auto& lhs_parameters =
static_cast<const OpaqueType&>(lhs).parameters();
const auto& rhs_parameters =
static_cast<const OpaqueType&>(rhs).parameters();
return lhs_parameters.size() == rhs_parameters.size() &&
std::equal(lhs_parameters.begin(), lhs_parameters.end(),
rhs_parameters.begin());
}
default:
return false;
}
}
void Type::HashValue(const Type& type, Kind kind, absl::HashState state) {
switch (kind) {
case Kind::kNullType:
absl::HashState::combine(std::move(state), kind,
static_cast<const NullType&>(type).name());
return;
case Kind::kError:
absl::HashState::combine(std::move(state), kind,
static_cast<const ErrorType&>(type).name());
return;
case Kind::kDyn:
absl::HashState::combine(std::move(state), kind,
static_cast<const DynType&>(type).name());
return;
case Kind::kAny:
absl::HashState::combine(std::move(state), kind,
static_cast<const AnyType&>(type).name());
return;
case Kind::kType:
absl::HashState::combine(std::move(state), kind,
static_cast<const TypeType&>(type).name());
return;
case Kind::kBool:
absl::HashState::combine(std::move(state), kind,
static_cast<const BoolType&>(type).name());
return;
case Kind::kInt:
absl::HashState::combine(std::move(state), kind,
static_cast<const IntType&>(type).name());
return;
case Kind::kUint:
absl::HashState::combine(std::move(state), kind,
static_cast<const UintType&>(type).name());
return;
case Kind::kDouble:
absl::HashState::combine(std::move(state), kind,
static_cast<const DoubleType&>(type).name());
return;
case Kind::kString:
absl::HashState::combine(std::move(state), kind,
static_cast<const StringType&>(type).name());
return;
case Kind::kBytes:
absl::HashState::combine(std::move(state), kind,
static_cast<const BytesType&>(type).name());
return;
case Kind::kEnum:
absl::HashState::combine(std::move(state), kind,
static_cast<const EnumType&>(type).name());
return;
case Kind::kDuration:
absl::HashState::combine(std::move(state), kind,
static_cast<const DurationType&>(type).name());
return;
case Kind::kTimestamp:
absl::HashState::combine(std::move(state), kind,
static_cast<const TimestampType&>(type).name());
return;
case Kind::kList:
absl::HashState::combine(std::move(state),
static_cast<const ListType&>(type).element(),
kind, static_cast<const ListType&>(type).name());
return;
case Kind::kMap:
absl::HashState::combine(std::move(state),
static_cast<const MapType&>(type).key(),
static_cast<const MapType&>(type).value(), kind,
static_cast<const MapType&>(type).name());
return;
case Kind::kStruct:
absl::HashState::combine(std::move(state), kind,
static_cast<const StructType&>(type).name());
return;
case Kind::kUnknown:
absl::HashState::combine(std::move(state), kind,
static_cast<const UnknownType&>(type).name());
return;
case Kind::kWrapper:
absl::HashState::combine(
std::move(state), static_cast<const WrapperType&>(type).wrapped(),
kind, static_cast<const WrapperType&>(type).name());
return;
case Kind::kOpaque: {
const auto& parameters =
static_cast<const OpaqueType&>(type).parameters();
for (const auto& parameter : parameters) {
state = absl::HashState::combine(std::move(state), parameter);
}
absl::HashState::combine(std::move(state), kind,
static_cast<const OpaqueType&>(type).name());
return;
}
default:
return;
}
}
bool Type::Equals(const Type& other) const { return Equals(*this, other); }
void Type::HashValue(absl::HashState state) const {
HashValue(*this, std::move(state));
}
namespace base_internal {
bool TypeHandle::Equals(const TypeHandle& other) const {
const auto* self = static_cast<const Type*>(data_.get());
const auto* that = static_cast<const Type*>(other.data_.get());
if (self == that) {
return true;
}
if (self == nullptr || that == nullptr) {
return false;
}
Kind kind = self->kind();
return kind == that->kind() && Type::Equals(*self, *that, kind);
}
void TypeHandle::HashValue(absl::HashState state) const {
if (const auto* pointer = static_cast<const Type*>(data_.get());
ABSL_PREDICT_TRUE(pointer != nullptr)) {
Type::HashValue(*pointer, pointer->kind(), std::move(state));
}
}
void TypeHandle::CopyFrom(const TypeHandle& other) {
// data_ is currently uninitialized.
auto locality = other.data_.locality();
if (locality == DataLocality::kStoredInline) {
if (ABSL_PREDICT_FALSE(!other.data_.IsTrivial())) {
// Type currently has only trivially copyable inline
// representations.
ABSL_UNREACHABLE();
} else {
// We can simply just copy the bytes.
data_.CopyFrom(other.data_);
}
} else {
data_.set_pointer(other.data_.pointer());
if (locality == DataLocality::kReferenceCounted) {
Ref();
}
}
}
void TypeHandle::MoveFrom(TypeHandle& other) {
// data_ is currently uninitialized.
if (other.data_.IsStoredInline()) {
if (ABSL_PREDICT_FALSE(!other.data_.IsTrivial())) {
// Type currently has only trivially copyable inline
// representations.
ABSL_UNREACHABLE();
} else {
// We can simply just copy the bytes.
data_.CopyFrom(other.data_);
}
} else {
data_.set_pointer(other.data_.pointer());
}
other.data_.Clear();
}
void TypeHandle::CopyAssign(const TypeHandle& other) {
// data_ is initialized.
Destruct();
CopyFrom(other);
}
void TypeHandle::MoveAssign(TypeHandle& other) {
// data_ is initialized.
Destruct();
MoveFrom(other);
}
void TypeHandle::Destruct() {
switch (data_.locality()) {
case DataLocality::kNull:
return;
case DataLocality::kStoredInline:
if (ABSL_PREDICT_FALSE(!data_.IsTrivial())) {
// Type currently has only trivially destructible inline
// representations.
ABSL_UNREACHABLE();
}
return;
case DataLocality::kReferenceCounted:
Unref();
return;
case DataLocality::kArenaAllocated:
return;
}
}
void TypeHandle::Delete() const {
switch (data_.kind_heap()) {
case Kind::kList:
delete static_cast<ModernListType*>(
static_cast<ListType*>(static_cast<Type*>(data_.get_heap())));
return;
case Kind::kMap:
delete static_cast<ModernMapType*>(
static_cast<MapType*>(static_cast<Type*>(data_.get_heap())));
return;
case Kind::kEnum:
delete static_cast<EnumType*>(static_cast<Type*>(data_.get_heap()));
return;
case Kind::kStruct:
delete static_cast<AbstractStructType*>(
static_cast<Type*>(data_.get_heap()));
return;
case Kind::kOpaque:
delete static_cast<OpaqueType*>(static_cast<Type*>(data_.get_heap()));
return;
default:
ABSL_UNREACHABLE();
}
}
} // namespace base_internal
} // namespace cel
Morty Proxy This is a proxified and sanitized view of the page, visit original site.