-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathast_internal.h
More file actions
1639 lines (1310 loc) · 47 KB
/
Copy pathast_internal.h
File metadata and controls
1639 lines (1310 loc) · 47 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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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.
//
// Type definitions for internal AST representation.
// CEL users should not directly depend on the definitions here.
// TODO(issues/5): move to base/internal
#ifndef THIRD_PARTY_CEL_CPP_BASE_AST_INTERNAL_H_
#define THIRD_PARTY_CEL_CPP_BASE_AST_INTERNAL_H_
#include <cstdint>
#include <limits>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <variant>
#include <vector>
#include "absl/container/flat_hash_map.h"
#include "absl/time/time.h"
#include "absl/types/variant.h"
namespace cel::ast::internal {
enum class NullValue { kNullValue = 0 };
// A holder class to differentiate between CEL string and CEL bytes constants.
struct Bytes {
std::string bytes;
bool operator==(const Bytes& other) const { return bytes == other.bytes; }
};
// Represents a primitive literal.
//
// This is similar as the primitives supported in the well-known type
// `google.protobuf.Value`, but richer so it can represent CEL's full range of
// primitives.
//
// Lists and structs are not included as constants as these aggregate types may
// contain [Expr][] elements which require evaluation and are thus not constant.
//
// Examples of constants include: `"hello"`, `b'bytes'`, `1u`, `4.2`, `-2`,
// `true`, `null`.
//
// (--
// TODO(issues/5): Extend or replace the constant with a canonical Value
// message that can hold any constant object representation supplied or
// produced at evaluation time.
// --)
using ConstantKind =
absl::variant<NullValue, bool, int64_t, uint64_t, double, std::string,
Bytes, absl::Duration, absl::Time>;
class Constant {
public:
constexpr Constant() = default;
explicit Constant(ConstantKind constant_kind)
: constant_kind_(std::move(constant_kind)) {}
void set_constant_kind(ConstantKind constant_kind) {
constant_kind_ = std::move(constant_kind);
}
const ConstantKind& constant_kind() const { return constant_kind_; }
ConstantKind& mutable_constant_kind() { return constant_kind_; }
bool has_null_value() const {
return absl::holds_alternative<NullValue>(constant_kind_);
}
NullValue null_value() const {
auto* value = absl::get_if<NullValue>(&constant_kind_);
if (value != nullptr) {
return *value;
}
return NullValue::kNullValue;
}
void set_null_value(NullValue null_value) { constant_kind_ = null_value; }
bool has_bool_value() const {
return absl::holds_alternative<bool>(constant_kind_);
}
bool bool_value() const {
auto* value = absl::get_if<bool>(&constant_kind_);
if (value != nullptr) {
return *value;
}
return false;
}
void set_bool_value(bool bool_value) { constant_kind_ = bool_value; }
bool has_int64_value() const {
return absl::holds_alternative<int64_t>(constant_kind_);
}
int64_t int64_value() const {
auto* value = absl::get_if<int64_t>(&constant_kind_);
if (value != nullptr) {
return *value;
}
return 0;
}
void set_int64_value(int64_t int64_value) { constant_kind_ = int64_value; }
bool has_uint64_value() const {
return absl::holds_alternative<uint64_t>(constant_kind_);
}
uint64_t uint64_value() const {
auto* value = absl::get_if<uint64_t>(&constant_kind_);
if (value != nullptr) {
return *value;
}
return 0;
}
void set_uint64_value(uint64_t uint64_value) {
constant_kind_ = uint64_value;
}
bool has_double_value() const {
return absl::holds_alternative<double>(constant_kind_);
}
double double_value() const {
auto* value = absl::get_if<double>(&constant_kind_);
if (value != nullptr) {
return *value;
}
return 0;
}
void set_double_value(double double_value) { constant_kind_ = double_value; }
bool has_string_value() const {
return absl::holds_alternative<std::string>(constant_kind_);
}
const std::string& string_value() const {
auto* value = absl::get_if<std::string>(&constant_kind_);
if (value != nullptr) {
return *value;
}
static std::string* default_string_value_ = new std::string("");
return *default_string_value_;
}
void set_string_value(std::string string_value) {
constant_kind_ = string_value;
}
bool has_bytes_value() const {
return absl::holds_alternative<Bytes>(constant_kind_);
}
const std::string& bytes_value() const {
auto* value = absl::get_if<Bytes>(&constant_kind_);
if (value != nullptr) {
return value->bytes;
}
static std::string* default_string_value_ = new std::string("");
return *default_string_value_;
}
void set_bytes_value(std::string bytes_value) {
constant_kind_ = Bytes{std::move(bytes_value)};
}
bool has_duration_value() const {
return absl::holds_alternative<absl::Duration>(constant_kind_);
}
void set_duration_value(absl::Duration duration_value) {
constant_kind_ = std::move(duration_value);
}
const absl::Duration& duration_value() const {
auto* value = absl::get_if<absl::Duration>(&constant_kind_);
if (value != nullptr) {
return *value;
}
static absl::Duration default_duration_;
return default_duration_;
}
bool has_time_value() const {
return absl::holds_alternative<absl::Time>(constant_kind_);
}
const absl::Time& time_value() const {
auto* value = absl::get_if<absl::Time>(&constant_kind_);
if (value != nullptr) {
return *value;
}
static absl::Time default_time_;
return default_time_;
}
void set_time_value(absl::Time time_value) {
constant_kind_ = std::move(time_value);
}
bool operator==(const Constant& other) const {
return constant_kind_ == other.constant_kind_;
}
private:
ConstantKind constant_kind_;
};
class Expr;
// An identifier expression. e.g. `request`.
class Ident {
public:
Ident() = default;
explicit Ident(std::string name) : name_(std::move(name)) {}
void set_name(std::string name) { name_ = std::move(name); }
const std::string& name() const { return name_; }
bool operator==(const Ident& other) const { return name_ == other.name_; }
private:
// Required. Holds a single, unqualified identifier, possibly preceded by a
// '.'.
//
// Qualified names are represented by the [Expr.Select][] expression.
std::string name_;
};
// A field selection expression. e.g. `request.auth`.
class Select {
public:
Select() = default;
Select(std::unique_ptr<Expr> operand, std::string field,
bool test_only = false)
: operand_(std::move(operand)),
field_(std::move(field)),
test_only_(test_only) {}
void set_operand(std::unique_ptr<Expr> operand) {
operand_ = std::move(operand);
}
void set_field(std::string field) { field_ = std::move(field); }
void set_test_only(bool test_only) { test_only_ = test_only; }
bool has_operand() const { return operand_ != nullptr; }
const Expr& operand() const;
Expr& mutable_operand() {
if (operand_ == nullptr) {
operand_ = std::make_unique<Expr>();
}
return *operand_;
}
const std::string& field() const { return field_; }
bool test_only() const { return test_only_; }
bool operator==(const Select& other) const;
private:
// Required. The target of the selection expression.
//
// For example, in the select expression `request.auth`, the `request`
// portion of the expression is the `operand`.
std::unique_ptr<Expr> operand_;
// Required. The name of the field to select.
//
// For example, in the select expression `request.auth`, the `auth` portion
// of the expression would be the `field`.
std::string field_;
// Whether the select is to be interpreted as a field presence test.
//
// This results from the macro `has(request.auth)`.
bool test_only_ = false;
};
// A call expression, including calls to predefined functions and operators.
//
// For example, `value == 10`, `size(map_value)`.
// (-- TODO(issues/5): Convert built-in globals to instance methods --)
class Call {
public:
Call() = default;
Call(std::unique_ptr<Expr> target, std::string function,
std::vector<Expr> args);
void set_target(std::unique_ptr<Expr> target) { target_ = std::move(target); }
void set_function(std::string function) { function_ = std::move(function); }
void set_args(std::vector<Expr> args);
bool has_target() const { return target_ != nullptr; }
const Expr& target() const;
Expr& mutable_target() {
if (target_ == nullptr) {
target_ = std::make_unique<Expr>();
}
return *target_;
}
const std::string& function() const { return function_; }
const std::vector<Expr>& args() const { return args_; }
std::vector<Expr>& mutable_args() { return args_; }
bool operator==(const Call& other) const;
private:
// The target of an method call-style expression. For example, `x` in
// `x.f()`.
std::unique_ptr<Expr> target_;
// Required. The name of the function or method being called.
std::string function_;
// The arguments.
std::vector<Expr> args_;
};
// A list creation expression.
//
// Lists may either be homogenous, e.g. `[1, 2, 3]`, or heterogeneous, e.g.
// `dyn([1, 'hello', 2.0])`
// (--
// TODO(issues/5): Determine how to disable heterogeneous types as a feature
// of type-checking rather than through the language construct 'dyn'.
// --)
class CreateList {
public:
CreateList() = default;
explicit CreateList(std::vector<Expr> elements);
void set_elements(std::vector<Expr> elements);
const std::vector<Expr>& elements() const { return elements_; }
std::vector<Expr>& mutable_elements() { return elements_; }
bool operator==(const CreateList& other) const;
private:
// The elements part of the list.
std::vector<Expr> elements_;
};
// A map or message creation expression.
//
// Maps are constructed as `{'key_name': 'value'}`. Message construction is
// similar, but prefixed with a type name and composed of field ids:
// `types.MyType{field_id: 'value'}`.
class CreateStruct {
public:
// Represents an entry.
class Entry {
public:
using KeyKind = absl::variant<std::string, std::unique_ptr<Expr>>;
Entry() = default;
Entry(int64_t id, KeyKind key_kind, std::unique_ptr<Expr> value)
: id_(id), key_kind_(std::move(key_kind)), value_(std::move(value)) {}
void set_id(int64_t id) { id_ = id; }
void set_key_kind(KeyKind key_kind) { key_kind_ = std::move(key_kind); }
void set_value(std::unique_ptr<Expr> value) { value_ = std::move(value); }
int64_t id() const { return id_; }
const KeyKind& key_kind() const { return key_kind_; }
KeyKind& mutable_key_kind() { return key_kind_; }
bool has_field_key() const {
return absl::holds_alternative<std::string>(key_kind_);
}
bool has_map_key() const {
return absl::holds_alternative<std::unique_ptr<Expr>>(key_kind_);
}
const std::string& field_key() const {
auto* value = absl::get_if<std::string>(&key_kind_);
if (value != nullptr) {
return *value;
}
static const std::string* default_field_key = new std::string;
return *default_field_key;
}
void set_field_key(std::string field_key) {
key_kind_ = std::move(field_key);
}
const Expr& map_key() const;
Expr& mutable_map_key() {
auto* value = absl::get_if<std::unique_ptr<Expr>>(&key_kind_);
if (value != nullptr) {
if (*value != nullptr) return **value;
}
key_kind_.emplace<std::unique_ptr<Expr>>(std::make_unique<Expr>());
return *absl::get<std::unique_ptr<Expr>>(key_kind_);
}
bool has_value() const { return value_ != nullptr; }
const Expr& value() const;
Expr& mutable_value() {
if (value_ == nullptr) {
value_ = std::make_unique<Expr>();
}
return *value_;
}
bool operator==(const Entry& other) const;
bool operator!=(const Entry& other) const { return !operator==(other); }
private:
// Required. An id assigned to this node by the parser which is unique
// in a given expression tree. This is used to associate type
// information and other attributes to the node.
int64_t id_ = 0;
// The `Entry` key kinds.
KeyKind key_kind_;
// Required. The value assigned to the key.
std::unique_ptr<Expr> value_;
};
CreateStruct() = default;
CreateStruct(std::string message_name, std::vector<Entry> entries)
: message_name_(std::move(message_name)), entries_(std::move(entries)) {}
void set_message_name(std::string message_name) {
message_name_ = std::move(message_name);
}
void set_entries(std::vector<Entry> entries) {
entries_ = std::move(entries);
}
const std::vector<Entry>& entries() const { return entries_; }
std::vector<Entry>& mutable_entries() { return entries_; }
const std::string& message_name() const { return message_name_; }
bool operator==(const CreateStruct& other) const {
return message_name_ == other.message_name_ && entries_ == other.entries_;
}
private:
// The type name of the message to be created, empty when creating map
// literals.
std::string message_name_;
// The entries in the creation expression.
std::vector<Entry> entries_;
};
// A comprehension expression applied to a list or map.
//
// Comprehensions are not part of the core syntax, but enabled with macros.
// A macro matches a specific call signature within a parsed AST and replaces
// the call with an alternate AST block. Macro expansion happens at parse
// time.
//
// The following macros are supported within CEL:
//
// Aggregate type macros may be applied to all elements in a list or all keys
// in a map:
//
// * `all`, `exists`, `exists_one` - test a predicate expression against
// the inputs and return `true` if the predicate is satisfied for all,
// any, or only one value `list.all(x, x < 10)`.
// * `filter` - test a predicate expression against the inputs and return
// the subset of elements which satisfy the predicate:
// `payments.filter(p, p > 1000)`.
// * `map` - apply an expression to all elements in the input and return the
// output aggregate type: `[1, 2, 3].map(i, i * i)`.
//
// The `has(m.x)` macro tests whether the property `x` is present in struct
// `m`. The semantics of this macro depend on the type of `m`. For proto2
// messages `has(m.x)` is defined as 'defined, but not set`. For proto3, the
// macro tests whether the property is set to its default. For map and struct
// types, the macro tests whether the property `x` is defined on `m`.
//
// Comprehension evaluation can be best visualized as the following
// pseudocode:
//
// ```
// let `accu_var` = `accu_init`
// for (let `iter_var` in `iter_range`) {
// if (!`loop_condition`) {
// break
// }
// `accu_var` = `loop_step`
// }
// return `result`
// ```
//
// (--
// TODO(issues/5): ensure comprehensions work equally well on maps and
// messages.
// --)
class Comprehension {
public:
Comprehension() = default;
Comprehension(std::string iter_var, std::unique_ptr<Expr> iter_range,
std::string accu_var, std::unique_ptr<Expr> accu_init,
std::unique_ptr<Expr> loop_condition,
std::unique_ptr<Expr> loop_step, std::unique_ptr<Expr> result)
: iter_var_(std::move(iter_var)),
iter_range_(std::move(iter_range)),
accu_var_(std::move(accu_var)),
accu_init_(std::move(accu_init)),
loop_condition_(std::move(loop_condition)),
loop_step_(std::move(loop_step)),
result_(std::move(result)) {}
bool has_iter_range() const { return iter_range_ != nullptr; }
bool has_accu_init() const { return accu_init_ != nullptr; }
bool has_loop_condition() const { return loop_condition_ != nullptr; }
bool has_loop_step() const { return loop_step_ != nullptr; }
bool has_result() const { return result_ != nullptr; }
void set_iter_var(std::string iter_var) { iter_var_ = std::move(iter_var); }
void set_iter_range(std::unique_ptr<Expr> iter_range) {
iter_range_ = std::move(iter_range);
}
void set_accu_var(std::string accu_var) { accu_var_ = std::move(accu_var); }
void set_accu_init(std::unique_ptr<Expr> accu_init) {
accu_init_ = std::move(accu_init);
}
void set_loop_condition(std::unique_ptr<Expr> loop_condition) {
loop_condition_ = std::move(loop_condition);
}
void set_loop_step(std::unique_ptr<Expr> loop_step) {
loop_step_ = std::move(loop_step);
}
void set_result(std::unique_ptr<Expr> result) { result_ = std::move(result); }
const std::string& iter_var() const { return iter_var_; }
const Expr& iter_range() const;
Expr& mutable_iter_range() {
if (iter_range_ == nullptr) {
iter_range_ = std::make_unique<Expr>();
}
return *iter_range_;
}
const std::string& accu_var() const { return accu_var_; }
const Expr& accu_init() const;
Expr& mutable_accu_init() {
if (accu_init_ == nullptr) {
accu_init_ = std::make_unique<Expr>();
}
return *accu_init_;
}
const Expr& loop_condition() const;
Expr& mutable_loop_condition() {
if (loop_condition_ == nullptr) {
loop_condition_ = std::make_unique<Expr>();
}
return *loop_condition_;
}
const Expr& loop_step() const;
Expr& mutable_loop_step() {
if (loop_step_ == nullptr) {
loop_step_ = std::make_unique<Expr>();
}
return *loop_step_;
}
const Expr& result() const;
Expr& mutable_result() {
if (result_ == nullptr) {
result_ = std::make_unique<Expr>();
}
return *result_;
}
bool operator==(const Comprehension& other) const;
private:
// The name of the iteration variable.
std::string iter_var_;
// The range over which var iterates.
std::unique_ptr<Expr> iter_range_;
// The name of the variable used for accumulation of the result.
std::string accu_var_;
// The initial value of the accumulator.
std::unique_ptr<Expr> accu_init_;
// An expression which can contain iter_var and accu_var.
//
// Returns false when the result has been computed and may be used as
// a hint to short-circuit the remainder of the comprehension.
std::unique_ptr<Expr> loop_condition_;
// An expression which can contain iter_var and accu_var.
//
// Computes the next value of accu_var.
std::unique_ptr<Expr> loop_step_;
// An expression which can contain accu_var.
//
// Computes the result.
std::unique_ptr<Expr> result_;
};
// Even though, the Expr proto does not allow for an unset, macro calls in the
// way they are used today sometimes elide parts of the AST if its
// unchanged/uninteresting.
using ExprKind =
absl::variant<absl::monostate /* unset */, Constant, Ident, Select, Call,
CreateList, CreateStruct, Comprehension>;
// Analogous to google::api::expr::v1alpha1::Expr
// An abstract representation of a common expression.
//
// Expressions are abstractly represented as a collection of identifiers,
// select statements, function calls, literals, and comprehensions. All
// operators with the exception of the '.' operator are modelled as function
// calls. This makes it easy to represent new operators into the existing AST.
//
// All references within expressions must resolve to a [Decl][] provided at
// type-check for an expression to be valid. A reference may either be a bare
// identifier `name` or a qualified identifier `google.api.name`. References
// may either refer to a value or a function declaration.
//
// For example, the expression `google.api.name.startsWith('expr')` references
// the declaration `google.api.name` within a [Expr.Select][] expression, and
// the function declaration `startsWith`.
// Move-only type.
class Expr {
public:
Expr() = default;
Expr(int64_t id, ExprKind expr_kind)
: id_(id), expr_kind_(std::move(expr_kind)) {}
Expr(Expr&& rhs) = default;
Expr& operator=(Expr&& rhs) = default;
void set_id(int64_t id) { id_ = id; }
void set_expr_kind(ExprKind expr_kind) { expr_kind_ = std::move(expr_kind); }
int64_t id() const { return id_; }
const ExprKind& expr_kind() const { return expr_kind_; }
ExprKind& mutable_expr_kind() { return expr_kind_; }
bool has_const_expr() const {
return absl::holds_alternative<Constant>(expr_kind_);
}
bool has_ident_expr() const {
return absl::holds_alternative<Ident>(expr_kind_);
}
bool has_select_expr() const {
return absl::holds_alternative<Select>(expr_kind_);
}
bool has_call_expr() const {
return absl::holds_alternative<Call>(expr_kind_);
}
bool has_list_expr() const {
return absl::holds_alternative<CreateList>(expr_kind_);
}
bool has_struct_expr() const {
return absl::holds_alternative<CreateStruct>(expr_kind_);
}
bool has_comprehension_expr() const {
return absl::holds_alternative<Comprehension>(expr_kind_);
}
const Constant& const_expr() const {
auto* value = absl::get_if<Constant>(&expr_kind_);
if (value != nullptr) {
return *value;
}
static const Constant* default_constant = new Constant;
return *default_constant;
}
Constant& mutable_const_expr() {
auto* value = absl::get_if<Constant>(&expr_kind_);
if (value != nullptr) {
return *value;
}
expr_kind_.emplace<Constant>();
return absl::get<Constant>(expr_kind_);
}
const Ident& ident_expr() const {
auto* value = absl::get_if<Ident>(&expr_kind_);
if (value != nullptr) {
return *value;
}
static const Ident* default_ident = new Ident;
return *default_ident;
}
Ident& mutable_ident_expr() {
auto* value = absl::get_if<Ident>(&expr_kind_);
if (value != nullptr) {
return *value;
}
expr_kind_.emplace<Ident>();
return absl::get<Ident>(expr_kind_);
}
const Select& select_expr() const {
auto* value = absl::get_if<Select>(&expr_kind_);
if (value != nullptr) {
return *value;
}
static const Select* default_select = new Select;
return *default_select;
}
Select& mutable_select_expr() {
auto* value = absl::get_if<Select>(&expr_kind_);
if (value != nullptr) {
return *value;
}
expr_kind_.emplace<Select>();
return absl::get<Select>(expr_kind_);
}
const Call& call_expr() const {
auto* value = absl::get_if<Call>(&expr_kind_);
if (value != nullptr) {
return *value;
}
static const Call* default_call = new Call;
return *default_call;
}
Call& mutable_call_expr() {
auto* value = absl::get_if<Call>(&expr_kind_);
if (value != nullptr) {
return *value;
}
expr_kind_.emplace<Call>();
return absl::get<Call>(expr_kind_);
}
const CreateList& list_expr() const {
auto* value = absl::get_if<CreateList>(&expr_kind_);
if (value != nullptr) {
return *value;
}
static const CreateList* default_create_list = new CreateList;
return *default_create_list;
}
CreateList& mutable_list_expr() {
auto* value = absl::get_if<CreateList>(&expr_kind_);
if (value != nullptr) {
return *value;
}
expr_kind_.emplace<CreateList>();
return absl::get<CreateList>(expr_kind_);
}
const CreateStruct& struct_expr() const {
auto* value = absl::get_if<CreateStruct>(&expr_kind_);
if (value != nullptr) {
return *value;
}
static const CreateStruct* default_create_struct = new CreateStruct;
return *default_create_struct;
}
CreateStruct& mutable_struct_expr() {
auto* value = absl::get_if<CreateStruct>(&expr_kind_);
if (value != nullptr) {
return *value;
}
expr_kind_.emplace<CreateStruct>();
return absl::get<CreateStruct>(expr_kind_);
}
const Comprehension& comprehension_expr() const {
auto* value = absl::get_if<Comprehension>(&expr_kind_);
if (value != nullptr) {
return *value;
}
static const Comprehension* default_comprehension = new Comprehension;
return *default_comprehension;
}
Comprehension& mutable_comprehension_expr() {
auto* value = absl::get_if<Comprehension>(&expr_kind_);
if (value != nullptr) {
return *value;
}
expr_kind_.emplace<Comprehension>();
return absl::get<Comprehension>(expr_kind_);
}
bool operator==(const Expr& other) const {
return id_ == other.id_ && expr_kind_ == other.expr_kind_;
}
private:
// Required. An id assigned to this node by the parser which is unique in a
// given expression tree. This is used to associate type information and other
// attributes to a node in the parse tree.
int64_t id_ = 0;
// Required. Variants of expressions.
ExprKind expr_kind_;
};
// Source information collected at parse time.
class SourceInfo {
public:
SourceInfo() = default;
SourceInfo(std::string syntax_version, std::string location,
std::vector<int32_t> line_offsets,
absl::flat_hash_map<int64_t, int32_t> positions,
absl::flat_hash_map<int64_t, Expr> macro_calls)
: syntax_version_(std::move(syntax_version)),
location_(std::move(location)),
line_offsets_(std::move(line_offsets)),
positions_(std::move(positions)),
macro_calls_(std::move(macro_calls)) {}
void set_syntax_version(std::string syntax_version) {
syntax_version_ = std::move(syntax_version);
}
void set_location(std::string location) { location_ = std::move(location); }
void set_line_offsets(std::vector<int32_t> line_offsets) {
line_offsets_ = std::move(line_offsets);
}
void set_positions(absl::flat_hash_map<int64_t, int32_t> positions) {
positions_ = std::move(positions);
}
void set_macro_calls(absl::flat_hash_map<int64_t, Expr> macro_calls) {
macro_calls_ = std::move(macro_calls);
}
const std::string& syntax_version() const { return syntax_version_; }
const std::string& location() const { return location_; }
const std::vector<int32_t>& line_offsets() const { return line_offsets_; }
std::vector<int32_t>& mutable_line_offsets() { return line_offsets_; }
const absl::flat_hash_map<int64_t, int32_t>& positions() const {
return positions_;
}
absl::flat_hash_map<int64_t, int32_t>& mutable_positions() {
return positions_;
}
const absl::flat_hash_map<int64_t, Expr>& macro_calls() const {
return macro_calls_;
}
absl::flat_hash_map<int64_t, Expr>& mutable_macro_calls() {
return macro_calls_;
}
private:
// The syntax version of the source, e.g. `cel1`.
std::string syntax_version_;
// The location name. All position information attached to an expression is
// relative to this location.
//
// The location could be a file, UI element, or similar. For example,
// `acme/app/AnvilPolicy.cel`.
std::string location_;
// Monotonically increasing list of code point offsets where newlines
// `\n` appear.
//
// The line number of a given position is the index `i` where for a given
// `id` the `line_offsets[i] < id_positions[id] < line_offsets[i+1]`. The
// column may be derivd from `id_positions[id] - line_offsets[i]`.
//
// TODO(issues/5): clarify this documentation
std::vector<int32_t> line_offsets_;
// A map from the parse node id (e.g. `Expr.id`) to the code point offset
// within source.
absl::flat_hash_map<int64_t, int32_t> positions_;
// A map from the parse node id where a macro replacement was made to the
// call `Expr` that resulted in a macro expansion.
//
// For example, `has(value.field)` is a function call that is replaced by a
// `test_only` field selection in the AST. Likewise, the call
// `list.exists(e, e > 10)` translates to a comprehension expression. The key
// in the map corresponds to the expression id of the expanded macro, and the
// value is the call `Expr` that was replaced.
absl::flat_hash_map<int64_t, Expr> macro_calls_;
};
// Analogous to google::api::expr::v1alpha1::ParsedExpr
// An expression together with source information as returned by the parser.
// Move-only type.
class ParsedExpr {
public:
ParsedExpr() = default;
ParsedExpr(Expr expr, SourceInfo source_info)
: expr_(std::move(expr)), source_info_(std::move(source_info)) {}
ParsedExpr(ParsedExpr&& rhs) = default;
ParsedExpr& operator=(ParsedExpr&& rhs) = default;
void set_expr(Expr expr) { expr_ = std::move(expr); }
void set_source_info(SourceInfo source_info) {
source_info_ = std::move(source_info);
}
const Expr& expr() const { return expr_; }
Expr& mutable_expr() { return expr_; }
const SourceInfo& source_info() const { return source_info_; }
SourceInfo& mutable_source_info() { return source_info_; }
private:
// The parsed expression.
Expr expr_;
// The source info derived from input that generated the parsed `expr`.
SourceInfo source_info_;
};
// CEL primitive types.
enum class PrimitiveType {
// Unspecified type.
kPrimitiveTypeUnspecified = 0,
// Boolean type.