Debian Codegraph @

debian/missing-sources/github.com/google/cel-go/common/ast/expr.go

raw symbols SCIP
1 // Copyright 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package ast
16
17 import (
18 "github.com/google/cel-go/common/types/ref"
19 )
20
21 // ExprKind represents the expression node kind.
22 type ExprKind int
23
24 const (
25 // UnspecifiedExprKind represents an unset expression with no specified properties.
26 UnspecifiedExprKind ExprKind = iota
27
28 // CallKind represents a function call.
29 CallKind
30
31 // ComprehensionKind represents a comprehension expression generated by a macro.
32 ComprehensionKind
33
34 // IdentKind represents a simple variable, constant, or type identifier.
35 IdentKind
36
37 // ListKind represents a list literal expression.
38 ListKind
39
40 // LiteralKind represents a primitive scalar literal.
41 LiteralKind
42
43 // MapKind represents a map literal expression.
44 MapKind
45
46 // SelectKind represents a field selection expression.
47 SelectKind
48
49 // StructKind represents a struct literal expression.
50 StructKind
51 )
52
53 // Expr represents the base expression node in a CEL abstract syntax tree.
54 //
55 // Depending on the `Kind()` value, the Expr may be converted to a concrete expression types
56 // as indicated by the `As<Kind>` methods.
57 type Expr interface {
58 // ID of the expression as it appears in the AST
59 ID() int64
60
61 // Kind of the expression node. See ExprKind for the valid enum values.
62 Kind() ExprKind
63
64 // AsCall adapts the expr into a CallExpr
65 //
66 // The Kind() must be equal to a CallKind for the conversion to be well-defined.
67 AsCall() CallExpr
68
69 // AsComprehension adapts the expr into a ComprehensionExpr.
70 //
71 // The Kind() must be equal to a ComprehensionKind for the conversion to be well-defined.
72 AsComprehension() ComprehensionExpr
73
74 // AsIdent adapts the expr into an identifier string.
75 //
76 // The Kind() must be equal to an IdentKind for the conversion to be well-defined.
77 AsIdent() string
78
79 // AsLiteral adapts the expr into a constant ref.Val.
80 //
81 // The Kind() must be equal to a LiteralKind for the conversion to be well-defined.
82 AsLiteral() ref.Val
83
84 // AsList adapts the expr into a ListExpr.
85 //
86 // The Kind() must be equal to a ListKind for the conversion to be well-defined.
87 AsList() ListExpr
88
89 // AsMap adapts the expr into a MapExpr.
90 //
91 // The Kind() must be equal to a MapKind for the conversion to be well-defined.
92 AsMap() MapExpr
93
94 // AsSelect adapts the expr into a SelectExpr.
95 //
96 // The Kind() must be equal to a SelectKind for the conversion to be well-defined.
97 AsSelect() SelectExpr
98
99 // AsStruct adapts the expr into a StructExpr.
100 //
101 // The Kind() must be equal to a StructKind for the conversion to be well-defined.
102 AsStruct() StructExpr
103
104 // RenumberIDs performs an in-place update of the expression and all of its descendents numeric ids.
105 RenumberIDs(IDGenerator)
106
107 // SetKindCase replaces the contents of the current expression with the contents of the other.
108 //
109 // The SetKindCase takes ownership of any expression instances references within the input Expr.
110 // A shallow copy is made of the Expr value itself, but not a deep one.
111 //
112 // This method should only be used during AST rewrites using temporary Expr values.
113 SetKindCase(Expr)
114
115 // isExpr is a marker interface.
116 isExpr()
117 }
118
119 // EntryExprKind represents the possible EntryExpr kinds.
120 type EntryExprKind int
121
122 const (
123 // UnspecifiedEntryExprKind indicates that the entry expr is not set.
124 UnspecifiedEntryExprKind EntryExprKind = iota
125
126 // MapEntryKind indicates that the entry is a MapEntry type with key and value expressions.
127 MapEntryKind
128
129 // StructFieldKind indicates that the entry is a StructField with a field name and initializer
130 // expression.
131 StructFieldKind
132 )
133
134 // EntryExpr represents the base entry expression in a CEL map or struct literal.
135 type EntryExpr interface {
136 // ID of the entry as it appears in the AST.
137 ID() int64
138
139 // Kind of the entry expression node. See EntryExprKind for valid enum values.
140 Kind() EntryExprKind
141
142 // AsMapEntry casts the EntryExpr to a MapEntry.
143 //
144 // The Kind() must be equal to MapEntryKind for the conversion to be well-defined.
145 AsMapEntry() MapEntry
146
147 // AsStructField casts the EntryExpr to a StructField
148 //
149 // The Kind() must be equal to StructFieldKind for the conversion to be well-defined.
150 AsStructField() StructField
151
152 // RenumberIDs performs an in-place update of the expression and all of its descendents numeric ids.
153 RenumberIDs(IDGenerator)
154
155 isEntryExpr()
156 }
157
158 // IDGenerator produces unique ids suitable for tagging expression nodes
159 type IDGenerator func(originalID int64) int64
160
161 // CallExpr defines an interface for inspecting a function call and its arguments.
162 type CallExpr interface {
163 // FunctionName returns the name of the function.
164 FunctionName() string
165
166 // IsMemberFunction returns whether the call has a non-nil target indicating it is a member function
167 IsMemberFunction() bool
168
169 // Target returns the target of the expression if one is present.
170 Target() Expr
171
172 // Args returns the list of call arguments, excluding the target.
173 Args() []Expr
174
175 // marker interface method
176 isExpr()
177 }
178
179 // ListExpr defines an interface for inspecting a list literal expression.
180 type ListExpr interface {
181 // Elements returns the list elements as navigable expressions.
182 Elements() []Expr
183
184 // OptionalIndicies returns the list of optional indices in the list literal.
185 OptionalIndices() []int32
186
187 // IsOptional indicates whether the given element index is optional.
188 IsOptional(int32) bool
189
190 // Size returns the number of elements in the list.
191 Size() int
192
193 // marker interface method
194 isExpr()
195 }
196
197 // SelectExpr defines an interface for inspecting a select expression.
198 type SelectExpr interface {
199 // Operand returns the selection operand expression.
200 Operand() Expr
201
202 // FieldName returns the field name being selected from the operand.
203 FieldName() string
204
205 // IsTestOnly indicates whether the select expression is a presence test generated by a macro.
206 IsTestOnly() bool
207
208 // marker interface method
209 isExpr()
210 }
211
212 // MapExpr defines an interface for inspecting a map expression.
213 type MapExpr interface {
214 // Entries returns the map key value pairs as EntryExpr values.
215 Entries() []EntryExpr
216
217 // Size returns the number of entries in the map.
218 Size() int
219
220 // marker interface method
221 isExpr()
222 }
223
224 // MapEntry defines an interface for inspecting a map entry.
225 type MapEntry interface {
226 // Key returns the map entry key expression.
227 Key() Expr
228
229 // Value returns the map entry value expression.
230 Value() Expr
231
232 // IsOptional returns whether the entry is optional.
233 IsOptional() bool
234
235 // marker interface method
236 isEntryExpr()
237 }
238
239 // StructExpr defines an interfaces for inspecting a struct and its field initializers.
240 type StructExpr interface {
241 // TypeName returns the struct type name.
242 TypeName() string
243
244 // Fields returns the set of field initializers in the struct expression as EntryExpr values.
245 Fields() []EntryExpr
246
247 // marker interface method
248 isExpr()
249 }
250
251 // StructField defines an interface for inspecting a struct field initialization.
252 type StructField interface {
253 // Name returns the name of the field.
254 Name() string
255
256 // Value returns the field initialization expression.
257 Value() Expr
258
259 // IsOptional returns whether the field is optional.
260 IsOptional() bool
261
262 // marker interface method
263 isEntryExpr()
264 }
265
266 // ComprehensionExpr defines an interface for inspecting a comprehension expression.
267 type ComprehensionExpr interface {
268 // IterRange returns the iteration range expression.
269 IterRange() Expr
270
271 // IterVar returns the iteration variable name.
272 //
273 // For one-variable comprehensions, the iter var refers to the element value
274 // when iterating over a list, or the map key when iterating over a map.
275 //
276 // For two-variable comprehneions, the iter var refers to the list index or the
277 // map key.
278 IterVar() string
279
280 // IterVar2 returns the second iteration variable name.
281 //
282 // When the value is non-empty, the comprehension is a two-variable comprehension.
283 IterVar2() string
284
285 // HasIterVar2 returns true if the second iteration variable is non-empty.
286 HasIterVar2() bool
287
288 // AccuVar returns the accumulation variable name.
289 AccuVar() string
290
291 // AccuInit returns the accumulation variable initialization expression.
292 AccuInit() Expr
293
294 // LoopCondition returns the loop condition expression.
295 LoopCondition() Expr
296
297 // LoopStep returns the loop step expression.
298 LoopStep() Expr
299
300 // Result returns the comprehension result expression.
301 Result() Expr
302
303 // marker interface method
304 isExpr()
305 }
306
307 var _ Expr = &expr{}
308
309 type expr struct {
310 id int64
311 exprKindCase
312 }
313
314 type exprKindCase interface {
315 Kind() ExprKind
316
317 renumberIDs(IDGenerator)
318
319 isExpr()
320 }
321
322 func (e *expr) ID() int64 {
323 if e == nil {
324 return 0
325 }
326 return e.id
327 }
328
329 func (e *expr) Kind() ExprKind {
330 if e == nil || e.exprKindCase == nil {
331 return UnspecifiedExprKind
332 }
333 return e.exprKindCase.Kind()
334 }
335
336 func (e *expr) AsCall() CallExpr {
337 if e.Kind() != CallKind {
338 return nilCall
339 }
340 return e.exprKindCase.(CallExpr)
341 }
342
343 func (e *expr) AsComprehension() ComprehensionExpr {
344 if e.Kind() != ComprehensionKind {
345 return nilCompre
346 }
347 return e.exprKindCase.(ComprehensionExpr)
348 }
349
350 func (e *expr) AsIdent() string {
351 if e.Kind() != IdentKind {
352 return ""
353 }
354 return string(e.exprKindCase.(baseIdentExpr))
355 }
356
357 func (e *expr) AsLiteral() ref.Val {
358 if e.Kind() != LiteralKind {
359 return nil
360 }
361 return e.exprKindCase.(*baseLiteral).Val
362 }
363
364 func (e *expr) AsList() ListExpr {
365 if e.Kind() != ListKind {
366 return nilList
367 }
368 return e.exprKindCase.(ListExpr)
369 }
370
371 func (e *expr) AsMap() MapExpr {
372 if e.Kind() != MapKind {
373 return nilMap
374 }
375 return e.exprKindCase.(MapExpr)
376 }
377
378 func (e *expr) AsSelect() SelectExpr {
379 if e.Kind() != SelectKind {
380 return nilSel
381 }
382 return e.exprKindCase.(SelectExpr)
383 }
384
385 func (e *expr) AsStruct() StructExpr {
386 if e.Kind() != StructKind {
387 return nilStruct
388 }
389 return e.exprKindCase.(StructExpr)
390 }
391
392 func (e *expr) SetKindCase(other Expr) {
393 if e == nil {
394 return
395 }
396 if other == nil {
397 e.exprKindCase = nil
398 return
399 }
400 switch other.Kind() {
401 case CallKind:
402 c := other.AsCall()
403 e.exprKindCase = &baseCallExpr{
404 function: c.FunctionName(),
405 target: c.Target(),
406 args: c.Args(),
407 isMember: c.IsMemberFunction(),
408 }
409 case ComprehensionKind:
410 c := other.AsComprehension()
411 e.exprKindCase = &baseComprehensionExpr{
412 iterRange: c.IterRange(),
413 iterVar: c.IterVar(),
414 iterVar2: c.IterVar2(),
415 accuVar: c.AccuVar(),
416 accuInit: c.AccuInit(),
417 loopCond: c.LoopCondition(),
418 loopStep: c.LoopStep(),
419 result: c.Result(),
420 }
421 case IdentKind:
422 e.exprKindCase = baseIdentExpr(other.AsIdent())
423 case ListKind:
424 l := other.AsList()
425 optIndexMap := make(map[int32]struct{}, len(l.OptionalIndices()))
426 for _, idx := range l.OptionalIndices() {
427 optIndexMap[idx] = struct{}{}
428 }
429 e.exprKindCase = &baseListExpr{
430 elements: l.Elements(),
431 optIndices: l.OptionalIndices(),
432 optIndexMap: optIndexMap,
433 }
434 case LiteralKind:
435 e.exprKindCase = &baseLiteral{Val: other.AsLiteral()}
436 case MapKind:
437 e.exprKindCase = &baseMapExpr{
438 entries: other.AsMap().Entries(),
439 }
440 case SelectKind:
441 s := other.AsSelect()
442 e.exprKindCase = &baseSelectExpr{
443 operand: s.Operand(),
444 field: s.FieldName(),
445 testOnly: s.IsTestOnly(),
446 }
447 case StructKind:
448 s := other.AsStruct()
449 e.exprKindCase = &baseStructExpr{
450 typeName: s.TypeName(),
451 fields: s.Fields(),
452 }
453 case UnspecifiedExprKind:
454 e.exprKindCase = nil
455 }
456 }
457
458 func (e *expr) RenumberIDs(idGen IDGenerator) {
459 if e == nil {
460 return
461 }
462 e.id = idGen(e.id)
463 if e.exprKindCase != nil {
464 e.exprKindCase.renumberIDs(idGen)
465 }
466 }
467
468 type baseCallExpr struct {
469 function string
470 target Expr
471 args []Expr
472 isMember bool
473 }
474
475 func (*baseCallExpr) Kind() ExprKind {
476 return CallKind
477 }
478
479 func (e *baseCallExpr) FunctionName() string {
480 if e == nil {
481 return ""
482 }
483 return e.function
484 }
485
486 func (e *baseCallExpr) IsMemberFunction() bool {
487 if e == nil {
488 return false
489 }
490 return e.isMember
491 }
492
493 func (e *baseCallExpr) Target() Expr {
494 if e == nil || !e.IsMemberFunction() {
495 return nilExpr
496 }
497 return e.target
498 }
499
500 func (e *baseCallExpr) Args() []Expr {
501 if e == nil {
502 return []Expr{}
503 }
504 return e.args
505 }
506
507 func (e *baseCallExpr) renumberIDs(idGen IDGenerator) {
508 if e.IsMemberFunction() {
509 e.Target().RenumberIDs(idGen)
510 }
511 for _, arg := range e.Args() {
512 arg.RenumberIDs(idGen)
513 }
514 }
515
516 func (*baseCallExpr) isExpr() {}
517
518 var _ ComprehensionExpr = &baseComprehensionExpr{}
519
520 type baseComprehensionExpr struct {
521 iterRange Expr
522 iterVar string
523 iterVar2 string
524 accuVar string
525 accuInit Expr
526 loopCond Expr
527 loopStep Expr
528 result Expr
529 }
530
531 func (*baseComprehensionExpr) Kind() ExprKind {
532 return ComprehensionKind
533 }
534
535 func (e *baseComprehensionExpr) IterRange() Expr {
536 if e == nil {
537 return nilExpr
538 }
539 return e.iterRange
540 }
541
542 func (e *baseComprehensionExpr) IterVar() string {
543 return e.iterVar
544 }
545
546 func (e *baseComprehensionExpr) IterVar2() string {
547 return e.iterVar2
548 }
549
550 func (e *baseComprehensionExpr) HasIterVar2() bool {
551 return e.iterVar2 != ""
552 }
553
554 func (e *baseComprehensionExpr) AccuVar() string {
555 return e.accuVar
556 }
557
558 func (e *baseComprehensionExpr) AccuInit() Expr {
559 if e == nil {
560 return nilExpr
561 }
562 return e.accuInit
563 }
564
565 func (e *baseComprehensionExpr) LoopCondition() Expr {
566 if e == nil {
567 return nilExpr
568 }
569 return e.loopCond
570 }
571
572 func (e *baseComprehensionExpr) LoopStep() Expr {
573 if e == nil {
574 return nilExpr
575 }
576 return e.loopStep
577 }
578
579 func (e *baseComprehensionExpr) Result() Expr {
580 if e == nil {
581 return nilExpr
582 }
583 return e.result
584 }
585
586 func (e *baseComprehensionExpr) renumberIDs(idGen IDGenerator) {
587 e.IterRange().RenumberIDs(idGen)
588 e.AccuInit().RenumberIDs(idGen)
589 e.LoopCondition().RenumberIDs(idGen)
590 e.LoopStep().RenumberIDs(idGen)
591 e.Result().RenumberIDs(idGen)
592 }
593
594 func (*baseComprehensionExpr) isExpr() {}
595
596 var _ exprKindCase = baseIdentExpr("")
597
598 type baseIdentExpr string
599
600 func (baseIdentExpr) Kind() ExprKind {
601 return IdentKind
602 }
603
604 func (e baseIdentExpr) renumberIDs(IDGenerator) {}
605
606 func (baseIdentExpr) isExpr() {}
607
608 var _ exprKindCase = &baseLiteral{}
609 var _ ref.Val = &baseLiteral{}
610
611 type baseLiteral struct {
612 ref.Val
613 }
614
615 func (*baseLiteral) Kind() ExprKind {
616 return LiteralKind
617 }
618
619 func (l *baseLiteral) renumberIDs(IDGenerator) {}
620
621 func (*baseLiteral) isExpr() {}
622
623 var _ ListExpr = &baseListExpr{}
624
625 type baseListExpr struct {
626 elements []Expr
627 optIndices []int32
628 optIndexMap map[int32]struct{}
629 }
630
631 func (*baseListExpr) Kind() ExprKind {
632 return ListKind
633 }
634
635 func (e *baseListExpr) Elements() []Expr {
636 if e == nil {
637 return []Expr{}
638 }
639 return e.elements
640 }
641
642 func (e *baseListExpr) IsOptional(index int32) bool {
643 _, found := e.optIndexMap[index]
644 return found
645 }
646
647 func (e *baseListExpr) OptionalIndices() []int32 {
648 if e == nil {
649 return []int32{}
650 }
651 return e.optIndices
652 }
653
654 func (e *baseListExpr) Size() int {
655 return len(e.Elements())
656 }
657
658 func (e *baseListExpr) renumberIDs(idGen IDGenerator) {
659 for _, elem := range e.Elements() {
660 elem.RenumberIDs(idGen)
661 }
662 }
663
664 func (*baseListExpr) isExpr() {}
665
666 type baseMapExpr struct {
667 entries []EntryExpr
668 }
669
670 func (*baseMapExpr) Kind() ExprKind {
671 return MapKind
672 }
673
674 func (e *baseMapExpr) Entries() []EntryExpr {
675 if e == nil {
676 return []EntryExpr{}
677 }
678 return e.entries
679 }
680
681 func (e *baseMapExpr) Size() int {
682 return len(e.Entries())
683 }
684
685 func (e *baseMapExpr) renumberIDs(idGen IDGenerator) {
686 for _, entry := range e.Entries() {
687 entry.RenumberIDs(idGen)
688 }
689 }
690
691 func (*baseMapExpr) isExpr() {}
692
693 type baseSelectExpr struct {
694 operand Expr
695 field string
696 testOnly bool
697 }
698
699 func (*baseSelectExpr) Kind() ExprKind {
700 return SelectKind
701 }
702
703 func (e *baseSelectExpr) Operand() Expr {
704 if e == nil || e.operand == nil {
705 return nilExpr
706 }
707 return e.operand
708 }
709
710 func (e *baseSelectExpr) FieldName() string {
711 if e == nil {
712 return ""
713 }
714 return e.field
715 }
716
717 func (e *baseSelectExpr) IsTestOnly() bool {
718 if e == nil {
719 return false
720 }
721 return e.testOnly
722 }
723
724 func (e *baseSelectExpr) renumberIDs(idGen IDGenerator) {
725 e.Operand().RenumberIDs(idGen)
726 }
727
728 func (*baseSelectExpr) isExpr() {}
729
730 type baseStructExpr struct {
731 typeName string
732 fields []EntryExpr
733 }
734
735 func (*baseStructExpr) Kind() ExprKind {
736 return StructKind
737 }
738
739 func (e *baseStructExpr) TypeName() string {
740 if e == nil {
741 return ""
742 }
743 return e.typeName
744 }
745
746 func (e *baseStructExpr) Fields() []EntryExpr {
747 if e == nil {
748 return []EntryExpr{}
749 }
750 return e.fields
751 }
752
753 func (e *baseStructExpr) renumberIDs(idGen IDGenerator) {
754 for _, f := range e.Fields() {
755 f.RenumberIDs(idGen)
756 }
757 }
758
759 func (*baseStructExpr) isExpr() {}
760
761 type entryExprKindCase interface {
762 Kind() EntryExprKind
763
764 renumberIDs(IDGenerator)
765
766 isEntryExpr()
767 }
768
769 var _ EntryExpr = &entryExpr{}
770
771 type entryExpr struct {
772 id int64
773 entryExprKindCase
774 }
775
776 func (e *entryExpr) ID() int64 {
777 return e.id
778 }
779
780 func (e *entryExpr) AsMapEntry() MapEntry {
781 if e.Kind() != MapEntryKind {
782 return nilMapEntry
783 }
784 return e.entryExprKindCase.(MapEntry)
785 }
786
787 func (e *entryExpr) AsStructField() StructField {
788 if e.Kind() != StructFieldKind {
789 return nilStructField
790 }
791 return e.entryExprKindCase.(StructField)
792 }
793
794 func (e *entryExpr) RenumberIDs(idGen IDGenerator) {
795 e.id = idGen(e.id)
796 e.entryExprKindCase.renumberIDs(idGen)
797 }
798
799 type baseMapEntry struct {
800 key Expr
801 value Expr
802 isOptional bool
803 }
804
805 func (e *baseMapEntry) Kind() EntryExprKind {
806 return MapEntryKind
807 }
808
809 func (e *baseMapEntry) Key() Expr {
810 if e == nil {
811 return nilExpr
812 }
813 return e.key
814 }
815
816 func (e *baseMapEntry) Value() Expr {
817 if e == nil {
818 return nilExpr
819 }
820 return e.value
821 }
822
823 func (e *baseMapEntry) IsOptional() bool {
824 if e == nil {
825 return false
826 }
827 return e.isOptional
828 }
829
830 func (e *baseMapEntry) renumberIDs(idGen IDGenerator) {
831 e.Key().RenumberIDs(idGen)
832 e.Value().RenumberIDs(idGen)
833 }
834
835 func (*baseMapEntry) isEntryExpr() {}
836
837 type baseStructField struct {
838 field string
839 value Expr
840 isOptional bool
841 }
842
843 func (f *baseStructField) Kind() EntryExprKind {
844 return StructFieldKind
845 }
846
847 func (f *baseStructField) Name() string {
848 if f == nil {
849 return ""
850 }
851 return f.field
852 }
853
854 func (f *baseStructField) Value() Expr {
855 if f == nil {
856 return nilExpr
857 }
858 return f.value
859 }
860
861 func (f *baseStructField) IsOptional() bool {
862 if f == nil {
863 return false
864 }
865 return f.isOptional
866 }
867
868 func (f *baseStructField) renumberIDs(idGen IDGenerator) {
869 f.Value().RenumberIDs(idGen)
870 }
871
872 func (*baseStructField) isEntryExpr() {}
873
874 var (
875 nilExpr *expr = nil
876 nilCall *baseCallExpr = nil
877 nilCompre *baseComprehensionExpr = nil
878 nilList *baseListExpr = nil
879 nilMap *baseMapExpr = nil
880 nilMapEntry *baseMapEntry = nil
881 nilSel *baseSelectExpr = nil
882 nilStruct *baseStructExpr = nil
883 nilStructField *baseStructField = nil
884 )