-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathvalue.cc
More file actions
467 lines (447 loc) · 16.6 KB
/
Copy pathvalue.cc
File metadata and controls
467 lines (447 loc) · 16.6 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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// 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/value.h"
#include <string>
#include <utility>
#include "absl/base/optimization.h"
#include "base/handle.h"
#include "base/internal/message_wrapper.h"
#include "base/kind.h"
#include "base/type.h"
#include "base/values/bool_value.h"
#include "base/values/bytes_value.h"
#include "base/values/double_value.h"
#include "base/values/duration_value.h"
#include "base/values/enum_value.h"
#include "base/values/error_value.h"
#include "base/values/int_value.h"
#include "base/values/list_value.h"
#include "base/values/map_value.h"
#include "base/values/null_value.h"
#include "base/values/opaque_value.h"
#include "base/values/string_value.h"
#include "base/values/struct_value.h"
#include "base/values/timestamp_value.h"
#include "base/values/type_value.h"
#include "base/values/uint_value.h"
#include "base/values/unknown_value.h"
namespace cel {
CEL_INTERNAL_VALUE_IMPL(Value);
Handle<Type> Value::type() const {
switch (kind()) {
case Kind::kNullType:
return static_cast<const NullValue*>(this)->type().As<Type>();
case Kind::kError:
return static_cast<const ErrorValue*>(this)->type().As<Type>();
case Kind::kType:
return static_cast<const TypeValue*>(this)->type().As<Type>();
case Kind::kBool:
return static_cast<const BoolValue*>(this)->type().As<Type>();
case Kind::kInt:
return static_cast<const IntValue*>(this)->type().As<Type>();
case Kind::kUint:
return static_cast<const UintValue*>(this)->type().As<Type>();
case Kind::kDouble:
return static_cast<const DoubleValue*>(this)->type().As<Type>();
case Kind::kString:
return static_cast<const StringValue*>(this)->type().As<Type>();
case Kind::kBytes:
return static_cast<const BytesValue*>(this)->type().As<Type>();
case Kind::kEnum:
return static_cast<const EnumValue*>(this)->type().As<Type>();
case Kind::kDuration:
return static_cast<const DurationValue*>(this)->type().As<Type>();
case Kind::kTimestamp:
return static_cast<const TimestampValue*>(this)->type().As<Type>();
case Kind::kList:
return static_cast<const ListValue*>(this)->type().As<Type>();
case Kind::kMap:
return static_cast<const MapValue*>(this)->type().As<Type>();
case Kind::kStruct:
return static_cast<const StructValue*>(this)->type().As<Type>();
case Kind::kUnknown:
return static_cast<const UnknownValue*>(this)->type().As<Type>();
case Kind::kOpaque:
return static_cast<const OpaqueValue*>(this)->type().As<Type>();
default:
ABSL_UNREACHABLE();
}
}
std::string Value::DebugString() const {
switch (kind()) {
case Kind::kNullType:
return static_cast<const NullValue*>(this)->DebugString();
case Kind::kError:
return static_cast<const ErrorValue*>(this)->DebugString();
case Kind::kType:
return static_cast<const TypeValue*>(this)->DebugString();
case Kind::kBool:
return static_cast<const BoolValue*>(this)->DebugString();
case Kind::kInt:
return static_cast<const IntValue*>(this)->DebugString();
case Kind::kUint:
return static_cast<const UintValue*>(this)->DebugString();
case Kind::kDouble:
return static_cast<const DoubleValue*>(this)->DebugString();
case Kind::kString:
return static_cast<const StringValue*>(this)->DebugString();
case Kind::kBytes:
return static_cast<const BytesValue*>(this)->DebugString();
case Kind::kEnum:
return static_cast<const EnumValue*>(this)->DebugString();
case Kind::kDuration:
return static_cast<const DurationValue*>(this)->DebugString();
case Kind::kTimestamp:
return static_cast<const TimestampValue*>(this)->DebugString();
case Kind::kList:
return static_cast<const ListValue*>(this)->DebugString();
case Kind::kMap:
return static_cast<const MapValue*>(this)->DebugString();
case Kind::kStruct:
return static_cast<const StructValue*>(this)->DebugString();
case Kind::kUnknown:
return static_cast<const UnknownValue*>(this)->DebugString();
case Kind::kOpaque:
return static_cast<const OpaqueValue*>(this)->DebugString();
default:
ABSL_UNREACHABLE();
}
}
namespace base_internal {
bool ValueHandle::Equals(const Value& lhs, const Value& rhs, Kind kind) {
switch (kind) {
case Kind::kNullType:
return true;
case Kind::kError:
return static_cast<const ErrorValue&>(lhs).value() ==
static_cast<const ErrorValue&>(rhs).value();
case Kind::kType:
return static_cast<const TypeValue&>(lhs).Equals(
static_cast<const TypeValue&>(rhs));
case Kind::kBool:
return static_cast<const BoolValue&>(lhs).value() ==
static_cast<const BoolValue&>(rhs).value();
case Kind::kInt:
return static_cast<const IntValue&>(lhs).value() ==
static_cast<const IntValue&>(rhs).value();
case Kind::kUint:
return static_cast<const UintValue&>(lhs).value() ==
static_cast<const UintValue&>(rhs).value();
case Kind::kDouble:
return static_cast<const DoubleValue&>(lhs).value() ==
static_cast<const DoubleValue&>(rhs).value();
case Kind::kString:
return static_cast<const StringValue&>(lhs).Equals(
static_cast<const StringValue&>(rhs));
case Kind::kBytes:
return static_cast<const BytesValue&>(lhs).Equals(
static_cast<const BytesValue&>(rhs));
case Kind::kEnum:
return static_cast<const EnumValue&>(lhs).number() ==
static_cast<const EnumValue&>(rhs).number() &&
static_cast<const EnumValue&>(lhs).type() ==
static_cast<const EnumValue&>(rhs).type();
case Kind::kDuration:
return static_cast<const DurationValue&>(lhs).value() ==
static_cast<const DurationValue&>(rhs).value();
case Kind::kTimestamp:
return static_cast<const TimestampValue&>(lhs).value() ==
static_cast<const TimestampValue&>(rhs).value();
case Kind::kList: {
bool stored_inline = Metadata::IsStoredInline(lhs);
if (stored_inline != Metadata::IsStoredInline(rhs)) {
return false;
}
if (stored_inline) {
return static_cast<const LegacyListValue&>(lhs).impl_ ==
static_cast<const LegacyListValue&>(rhs).impl_;
}
return &lhs == &rhs;
}
case Kind::kMap: {
bool stored_inline = Metadata::IsStoredInline(lhs);
if (stored_inline != Metadata::IsStoredInline(rhs)) {
return false;
}
if (stored_inline) {
return static_cast<const LegacyMapValue&>(lhs).impl_ ==
static_cast<const LegacyMapValue&>(rhs).impl_;
}
return &lhs == &rhs;
}
case Kind::kStruct: {
bool stored_inline = Metadata::IsStoredInline(lhs);
if (stored_inline != Metadata::IsStoredInline(rhs)) {
return false;
}
if (stored_inline) {
return (static_cast<const LegacyStructValue&>(lhs).msg_ &
kMessageWrapperPtrMask) ==
(static_cast<const LegacyStructValue&>(rhs).msg_ &
kMessageWrapperPtrMask);
}
return &lhs == &rhs;
}
case Kind::kUnknown:
return static_cast<const UnknownValue&>(lhs).attribute_set() ==
static_cast<const UnknownValue&>(rhs).attribute_set() &&
static_cast<const UnknownValue&>(lhs).function_result_set() ==
static_cast<const UnknownValue&>(rhs).function_result_set();
case Kind::kOpaque:
return &lhs == &rhs;
default:
ABSL_UNREACHABLE();
}
}
bool ValueHandle::Equals(const ValueHandle& other) const {
const auto* self = static_cast<const Value*>(data_.get());
const auto* that = static_cast<const Value*>(other.data_.get());
if (self == that) {
return true;
}
if (self == nullptr || that == nullptr) {
return false;
}
Kind kind = self->kind();
return kind == that->kind() && Equals(*self, *that, kind);
}
void ValueHandle::CopyFrom(const ValueHandle& other) {
// data_ is currently uninitialized.
auto locality = other.data_.locality();
if (locality == DataLocality::kStoredInline) {
if (!other.data_.IsTrivial()) {
switch (other.data_.kind_inline()) {
case Kind::kError:
data_.ConstructInline<ErrorValue>(
*static_cast<const ErrorValue*>(other.data_.get_inline()));
return;
case Kind::kUnknown:
data_.ConstructInline<UnknownValue>(
*static_cast<const UnknownValue*>(other.data_.get_inline()));
return;
case Kind::kString:
switch (other.data_.inline_variant<InlinedStringValueVariant>()) {
case InlinedStringValueVariant::kCord:
data_.ConstructInline<InlinedCordStringValue>(
*static_cast<const InlinedCordStringValue*>(
other.data_.get_inline()));
break;
case InlinedStringValueVariant::kStringView:
data_.ConstructInline<InlinedStringViewStringValue>(
*static_cast<const InlinedStringViewStringValue*>(
other.data_.get_inline()));
break;
}
return;
case Kind::kBytes:
switch (other.data_.inline_variant<InlinedBytesValueVariant>()) {
case InlinedBytesValueVariant::kCord:
data_.ConstructInline<InlinedCordBytesValue>(
*static_cast<const InlinedCordBytesValue*>(
other.data_.get_inline()));
break;
case InlinedBytesValueVariant::kStringView:
data_.ConstructInline<InlinedStringViewBytesValue>(
*static_cast<const InlinedStringViewBytesValue*>(
other.data_.get_inline()));
break;
}
return;
case Kind::kType:
data_.ConstructInline<base_internal::ModernTypeValue>(
*static_cast<const base_internal::ModernTypeValue*>(
other.data_.get_inline()));
return;
case Kind::kEnum:
data_.ConstructInline<EnumValue>(
*static_cast<const EnumValue*>(other.data_.get_inline()));
return;
default:
ABSL_UNREACHABLE();
}
} else { // trivially copyable
// We can simply just copy the bytes.
data_.CopyFrom(other.data_);
}
} else { // not inline
data_.set_pointer(other.data_.pointer());
if (locality == DataLocality::kReferenceCounted) {
Ref();
}
}
}
void ValueHandle::MoveFrom(ValueHandle& other) {
// data_ is currently uninitialized.
if (other.data_.IsStoredInline()) {
if (!other.data_.IsTrivial()) {
switch (other.data_.kind_inline()) {
case Kind::kError:
data_.ConstructInline<ErrorValue>(
std::move(*static_cast<ErrorValue*>(other.data_.get_inline())));
other.data_.Destruct<ErrorValue>();
break;
case Kind::kUnknown:
data_.ConstructInline<UnknownValue>(
std::move(*static_cast<UnknownValue*>(other.data_.get_inline())));
other.data_.Destruct<UnknownValue>();
break;
case Kind::kString:
switch (other.data_.inline_variant<InlinedStringValueVariant>()) {
case InlinedStringValueVariant::kCord:
data_.ConstructInline<InlinedCordStringValue>(
std::move(*static_cast<InlinedCordStringValue*>(
other.data_.get_inline())));
other.data_.Destruct<InlinedCordStringValue>();
break;
case InlinedStringValueVariant::kStringView:
data_.ConstructInline<InlinedStringViewStringValue>(
std::move(*static_cast<InlinedStringViewStringValue*>(
other.data_.get_inline())));
other.data_.Destruct<InlinedStringViewStringValue>();
break;
}
break;
case Kind::kBytes:
switch (other.data_.inline_variant<InlinedBytesValueVariant>()) {
case InlinedBytesValueVariant::kCord:
data_.ConstructInline<InlinedCordBytesValue>(
std::move(*static_cast<InlinedCordBytesValue*>(
other.data_.get_inline())));
other.data_.Destruct<InlinedCordBytesValue>();
break;
case InlinedBytesValueVariant::kStringView:
data_.ConstructInline<InlinedStringViewBytesValue>(
std::move(*static_cast<InlinedStringViewBytesValue*>(
other.data_.get_inline())));
other.data_.Destruct<InlinedStringViewBytesValue>();
break;
}
break;
case Kind::kType:
data_.ConstructInline<ModernTypeValue>(std::move(
*static_cast<const ModernTypeValue*>(other.data_.get_inline())));
other.data_.Destruct<ModernTypeValue>();
break;
case Kind::kEnum:
data_.ConstructInline<EnumValue>(std::move(
*static_cast<const EnumValue*>(other.data_.get_inline())));
other.data_.Destruct<EnumValue>();
break;
default:
ABSL_UNREACHABLE();
}
} else { // trivially copyable
// We can simply just copy the bytes.
data_.CopyFrom(other.data_);
}
} else { // not inline
data_.set_pointer(other.data_.pointer());
}
other.data_.Clear();
}
void ValueHandle::CopyAssign(const ValueHandle& other) {
// data_ is initialized.
Destruct();
CopyFrom(other);
}
void ValueHandle::MoveAssign(ValueHandle& other) {
// data_ is initialized.
Destruct();
MoveFrom(other);
}
void ValueHandle::Destruct() {
switch (data_.locality()) {
case DataLocality::kNull:
return;
case DataLocality::kStoredInline:
if (!data_.IsTrivial()) {
switch (data_.kind_inline()) {
case Kind::kError:
data_.Destruct<ErrorValue>();
return;
case Kind::kUnknown:
data_.Destruct<UnknownValue>();
return;
case Kind::kString:
switch (data_.inline_variant<InlinedStringValueVariant>()) {
case InlinedStringValueVariant::kCord:
data_.Destruct<InlinedCordStringValue>();
break;
case InlinedStringValueVariant::kStringView:
data_.Destruct<InlinedStringViewStringValue>();
break;
}
return;
case Kind::kBytes:
switch (data_.inline_variant<InlinedBytesValueVariant>()) {
case InlinedBytesValueVariant::kCord:
data_.Destruct<InlinedCordBytesValue>();
break;
case InlinedBytesValueVariant::kStringView:
data_.Destruct<InlinedStringViewBytesValue>();
break;
}
return;
case Kind::kType:
data_.Destruct<ModernTypeValue>();
return;
case Kind::kEnum:
data_.Destruct<EnumValue>();
return;
default:
ABSL_UNREACHABLE();
}
}
return;
case DataLocality::kReferenceCounted:
Unref();
return;
case DataLocality::kArenaAllocated:
return;
}
}
void ValueHandle::Delete() const {
Delete(data_.kind_heap(), *static_cast<const Value*>(data_.get_heap()));
}
void ValueHandle::Delete(Kind kind, const Value& value) {
switch (kind) {
case Kind::kList:
delete static_cast<const AbstractListValue*>(&value);
return;
case Kind::kMap:
delete static_cast<const AbstractMapValue*>(&value);
return;
case Kind::kStruct:
delete static_cast<const AbstractStructValue*>(&value);
return;
case Kind::kString:
delete static_cast<const StringStringValue*>(&value);
return;
case Kind::kBytes:
delete static_cast<const StringBytesValue*>(&value);
return;
case Kind::kOpaque:
delete static_cast<const OpaqueValue*>(&value);
return;
default:
ABSL_UNREACHABLE();
}
}
void ValueMetadata::Unref(const Value& value) {
if (Metadata::Unref(value)) {
ValueHandle::Delete(Metadata::KindHeap(value), value);
}
}
} // namespace base_internal
} // namespace cel