1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package adt
16
17 import (
18 "fmt"
19
20 "cuelang.org/go/cue/ast"
21 "cuelang.org/go/cue/errors"
22 "cuelang.org/go/cue/token"
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 type Environment struct {
84 Up *Environment
85 Vertex *Vertex
86
87
88
89 DynamicLabel Feature
90
91
92
93
94
95
96 cache map[cacheKey]Value
97 }
98
99 type cacheKey struct {
100 Expr Expr
101 Arc *Vertex
102 }
103
104 func (e *Environment) up(ctx *OpContext, count int32) *Environment {
105 for ; count > 0; count-- {
106 e = e.Up
107 ctx.Assertf(ctx.Pos(), e.Vertex != nil, "Environment.up encountered a nil vertex")
108 }
109 return e
110 }
111
112 type ID int32
113
114
115 func (e *Environment) evalCached(c *OpContext, x Expr) Value {
116 if v, ok := x.(Value); ok {
117 return v
118 }
119 key := cacheKey{x, nil}
120 v, ok := e.cache[key]
121 if !ok {
122 if e.cache == nil {
123 e.cache = map[cacheKey]Value{}
124 }
125 env, src := c.e, c.src
126 c.e, c.src = e, x.Source()
127
128
129 err := c.errs
130 v = c.evalState(x, require(partial, allKnown))
131 c.e, c.src = env, src
132 c.errs = err
133 if b, ok := v.(*Bottom); !ok || !b.IsIncomplete() {
134 e.cache[key] = v
135 }
136 }
137 return v
138 }
139
140
141
142
143
144
145
146
147
148 type Vertex struct {
149
150
151
152 Parent *Vertex
153
154
155
156
157
158
159 state *nodeContext
160
161
162
163
164 cc *closeContext
165
166
167 Label Feature
168
169
170
171
172 status vertexStatus
173
174
175
176
177 hasAllConjuncts bool
178
179
180
181
182 isData bool
183
184
185
186
187 Closed bool
188
189
190
191
192 MultiLet bool
193
194
195
196
197 LockArcs bool
198
199
200
201
202 IsDynamic bool
203
204 nonRooted bool
205
206
207 hasPendingArc bool
208
209
210 ArcType ArcType
211
212
213
214 cyclicReferences *RefNode
215
216
217
218 BaseValue BaseValue
219
220
221 ChildErrors *Bottom
222
223
224
225
226 Arcs []*Vertex
227
228
229
230
231
232
233 PatternConstraints *Constraints
234
235
236
237
238
239
240 Conjuncts []Conjunct
241
242
243
244 Structs []*StructInfo
245 }
246
247
248
249 func (v *Vertex) rootCloseContext() *closeContext {
250 if v.cc == nil {
251 v.cc = &closeContext{
252 group: &ConjunctGroup{},
253 parent: nil,
254 src: v,
255 parentConjuncts: v,
256 }
257 v.cc.incDependent(ROOT, nil)
258 }
259 return v.cc
260 }
261
262
263
264 func (ctx *OpContext) newInlineVertex(parent *Vertex, v BaseValue, a ...Conjunct) *Vertex {
265 return &Vertex{
266 Parent: parent,
267 BaseValue: v,
268 IsDynamic: true,
269 ArcType: ArcMember,
270 Conjuncts: a,
271 }
272 }
273
274
275 func (v *Vertex) updateArcType(t ArcType) {
276 if t >= v.ArcType {
277 return
278 }
279 if v.ArcType == ArcNotPresent {
280 return
281 }
282 if s := v.state; s != nil && s.ctx.isDevVersion() {
283 c := s.ctx
284 if s.scheduler.frozen.meets(arcTypeKnown) {
285 parent := v.Parent
286 parent.reportFieldCycleError(c, c.Source().Pos(), v.Label)
287 return
288 }
289 }
290 v.ArcType = t
291 }
292
293
294
295 func (v *Vertex) isDefined() bool {
296 return v.ArcType == ArcMember
297 }
298
299
300 func (v *Vertex) IsConstraint() bool {
301 return v.ArcType == ArcOptional || v.ArcType == ArcRequired
302 }
303
304
305
306
307
308 func (v *Vertex) IsDefined(c *OpContext) bool {
309 if v.isDefined() {
310 return true
311 }
312 v.Finalize(c)
313 return v.isDefined()
314 }
315
316
317
318 func (v *Vertex) Rooted() bool {
319 return !v.nonRooted && !v.Label.IsLet() && !v.IsDynamic
320 }
321
322 type ArcType uint8
323
324 const (
325
326
327 ArcMember ArcType = iota
328
329
330
331 ArcRequired
332
333
334
335 ArcOptional
336
337
338
339
340
341 ArcPending
342
343
344
345 ArcNotPresent
346
347
348
349
350
351 )
352
353 func (a ArcType) String() string {
354 switch a {
355 case ArcMember:
356 return "Member"
357 case ArcOptional:
358 return "Optional"
359 case ArcRequired:
360 return "Required"
361 case ArcPending:
362 return "Pending"
363 case ArcNotPresent:
364 return "NotPresent"
365 }
366 return fmt.Sprintf("ArcType(%d)", a)
367 }
368
369
370
371
372 func (v *Vertex) definitelyExists() bool {
373 return v.ArcType < ArcPending
374 }
375
376
377
378 func ConstraintFromToken(t token.Token) ArcType {
379 switch t {
380 case token.OPTION:
381 return ArcOptional
382 case token.NOT:
383 return ArcRequired
384 }
385 return ArcMember
386 }
387
388
389
390 func (a ArcType) Token() (t token.Token) {
391 switch a {
392 case ArcOptional:
393 t = token.OPTION
394 case ArcRequired:
395 t = token.NOT
396 }
397 return t
398 }
399
400
401
402 func (a ArcType) Suffix() string {
403 switch a {
404 case ArcOptional:
405 return "?"
406 case ArcRequired:
407 return "!"
408
409
410 case ArcPending:
411 return "*"
412 case ArcNotPresent:
413 return "-"
414 }
415 return ""
416 }
417
418 func (v *Vertex) Clone() *Vertex {
419 c := *v
420 c.state = nil
421 return &c
422 }
423
424 type StructInfo struct {
425 *StructLit
426
427 Env *Environment
428
429 CloseInfo
430
431
432
433
434
435 Disable bool
436
437 Embedding bool
438 }
439
440
441
442 func (s *StructInfo) useForAccept() bool {
443 if c := s.closeInfo; c != nil {
444 return !c.noCheck
445 }
446 return true
447 }
448
449
450 type vertexStatus int8
451
452 const (
453
454
455 unprocessed vertexStatus = iota
456
457
458
459 evaluating
460
461
462
463
464
465
466 partial
467
468
469
470 conjuncts
471
472
473
474
475 evaluatingArcs
476
477
478
479 finalized
480 )
481
482 func (s vertexStatus) String() string {
483 switch s {
484 case unprocessed:
485 return "unprocessed"
486 case evaluating:
487 return "evaluating"
488 case partial:
489 return "partial"
490 case conjuncts:
491 return "conjuncts"
492 case evaluatingArcs:
493 return "evaluatingArcs"
494 case finalized:
495 return "finalized"
496 default:
497 return "unknown"
498 }
499 }
500
501 func (v *Vertex) Status() vertexStatus {
502 return v.status
503 }
504
505
506 func (v *Vertex) ForceDone() {
507 v.updateStatus(finalized)
508 }
509
510
511 func (v *Vertex) IsUnprocessed() bool {
512 return v.status == unprocessed
513 }
514
515 func (v *Vertex) updateStatus(s vertexStatus) {
516 Assertf(v.status <= s+1, "attempt to regress status from %d to %d", v.Status(), s)
517
518 if s == finalized && v.BaseValue == nil {
519
520
521 }
522 v.status = s
523 }
524
525
526
527
528
529
530
531
532
533
534
535
536 func (v *Vertex) setParentDone() {
537 v.hasAllConjuncts = true
538
539 if n := v.state; n != nil && len(n.conjuncts) == n.conjunctsPos {
540 for _, a := range v.Arcs {
541 a.setParentDone()
542 }
543 }
544 }
545
546
547
548 func (v *Vertex) Value() Value {
549 switch x := v.BaseValue.(type) {
550 case nil:
551 return nil
552 case *StructMarker, *ListMarker:
553 return v
554 case Value:
555
556 return x
557 default:
558 panic(fmt.Sprintf("unexpected type %T", v.BaseValue))
559 }
560 }
561
562
563 func (v *Vertex) isUndefined() bool {
564 if !v.isDefined() {
565 return true
566 }
567 switch v.BaseValue {
568 case nil, cycle:
569 return true
570 }
571 return false
572 }
573
574 func (x *Vertex) IsConcrete() bool {
575 return x.Concreteness() <= Concrete
576 }
577
578
579
580
581 func (v *Vertex) IsData() bool {
582 return v.isData || len(v.Conjuncts) == 0
583 }
584
585
586
587
588 func (v *Vertex) ToDataSingle() *Vertex {
589 w := *v
590 w.isData = true
591 w.state = nil
592 w.status = finalized
593 return &w
594 }
595
596
597
598 func (v *Vertex) ToDataAll(ctx *OpContext) *Vertex {
599 arcs := make([]*Vertex, 0, len(v.Arcs))
600 for _, a := range v.Arcs {
601 if !a.IsDefined(ctx) {
602 continue
603 }
604 if a.Label.IsRegular() {
605 arcs = append(arcs, a.ToDataAll(ctx))
606 }
607 }
608 w := *v
609 w.state = nil
610 w.status = finalized
611
612 w.BaseValue = toDataAll(ctx, w.BaseValue)
613 w.Arcs = arcs
614 w.isData = true
615 w.Conjuncts = make([]Conjunct, len(v.Conjuncts))
616
617
618 for _, s := range w.Structs {
619 s.Disable = true
620 }
621 copy(w.Conjuncts, v.Conjuncts)
622 for i, c := range w.Conjuncts {
623 if v, _ := c.x.(Value); v != nil {
624 w.Conjuncts[i].x = toDataAll(ctx, v).(Value)
625 }
626 }
627 return &w
628 }
629
630 func toDataAll(ctx *OpContext, v BaseValue) BaseValue {
631 switch x := v.(type) {
632 default:
633 return x
634
635 case *Vertex:
636 return x.ToDataAll(ctx)
637
638
639
640 case *Disjunction:
641 d := *x
642 d.Values = make([]Value, len(x.Values))
643 for i, v := range x.Values {
644 switch x := v.(type) {
645 case *Vertex:
646 d.Values[i] = x.ToDataAll(ctx)
647 default:
648 d.Values[i] = x
649 }
650 }
651 return &d
652
653 case *Conjunction:
654 c := *x
655 c.Values = make([]Value, len(x.Values))
656 for i, v := range x.Values {
657
658 c.Values[i] = toDataAll(ctx, v).(Value)
659 }
660 return &c
661 }
662 }
663
664
665
666
667
668 func (v *Vertex) IsErr() bool {
669
670 if _, ok := v.BaseValue.(*Bottom); ok {
671 return true
672 }
673
674 return false
675 }
676
677 func (v *Vertex) Err(c *OpContext) *Bottom {
678 v.Finalize(c)
679 if b, ok := v.BaseValue.(*Bottom); ok {
680 return b
681 }
682 return nil
683 }
684
685
686
687 func (v *Vertex) Finalize(c *OpContext) {
688
689
690 err := c.errs
691 c.errs = nil
692 c.unify(v, final(finalized, allKnown))
693 c.errs = err
694 }
695
696
697 func (v *Vertex) CompleteArcs(c *OpContext) {
698 c.unify(v, final(conjuncts, allKnown))
699 }
700
701 func (v *Vertex) AddErr(ctx *OpContext, b *Bottom) {
702 v.SetValue(ctx, CombineErrors(nil, v.Value(), b))
703 }
704
705
706 func (v *Vertex) SetValue(ctx *OpContext, value BaseValue) *Bottom {
707 return v.setValue(ctx, finalized, value)
708 }
709
710 func (v *Vertex) setValue(ctx *OpContext, state vertexStatus, value BaseValue) *Bottom {
711 v.BaseValue = value
712
713 v.updateStatus(state)
714 return nil
715 }
716
717
718 func ToVertex(v Value) *Vertex {
719 switch x := v.(type) {
720 case *Vertex:
721 return x
722 default:
723 n := &Vertex{
724 status: finalized,
725 BaseValue: x,
726 }
727 n.AddConjunct(MakeRootConjunct(nil, v))
728 return n
729 }
730 }
731
732
733
734 func Unwrap(v Value) Value {
735 x, ok := v.(*Vertex)
736 if !ok {
737 return v
738 }
739 x = x.Indirect()
740 if n := x.state; n != nil && isCyclePlaceholder(x.BaseValue) {
741 if n.errs != nil && !n.errs.IsIncomplete() {
742 return n.errs
743 }
744 if n.scalar != nil {
745 return n.scalar
746 }
747 }
748 return x.Value()
749 }
750
751
752
753
754 func (v *Vertex) Indirect() *Vertex {
755 for {
756 arc, ok := v.BaseValue.(*Vertex)
757 if !ok {
758 return v
759 }
760 v = arc
761 }
762 }
763
764
765
766 type OptionalType int8
767
768 const (
769 HasField OptionalType = 1 << iota
770 HasDynamic
771 HasPattern
772 HasComplexPattern
773 HasAdditional
774 IsOpen
775 )
776
777 func (v *Vertex) Kind() Kind {
778
779
780 switch {
781
782
783 case v.state != nil:
784 return v.state.kind
785 case v.BaseValue == nil:
786 return TopKind
787 default:
788 return v.BaseValue.Kind()
789 }
790 }
791
792 func (v *Vertex) OptionalTypes() OptionalType {
793 var mask OptionalType
794 for _, s := range v.Structs {
795 mask |= s.OptionalTypes()
796 }
797 return mask
798 }
799
800
801
802 func (v *Vertex) IsOptional(label Feature) bool {
803 for _, a := range v.Arcs {
804 if a.Label == label {
805 return a.IsConstraint()
806 }
807 }
808 return false
809 }
810
811 func (v *Vertex) accepts(ok, required bool) bool {
812 return ok || (!required && !v.Closed)
813 }
814
815 func (v *Vertex) IsClosedStruct() bool {
816 switch x := v.BaseValue.(type) {
817 default:
818 return false
819
820 case *StructMarker:
821 if x.NeedClose {
822 return true
823 }
824
825 case *Disjunction:
826 }
827 return isClosed(v)
828 }
829
830 func (v *Vertex) IsClosedList() bool {
831 if x, ok := v.BaseValue.(*ListMarker); ok {
832 return !x.IsOpen
833 }
834 return false
835 }
836
837
838 func (v *Vertex) Accept(ctx *OpContext, f Feature) bool {
839 if f.IsHidden() || f.IsLet() {
840 return true
841 }
842
843 if x, ok := v.BaseValue.(*Disjunction); ok {
844 for _, v := range x.Values {
845 if x, ok := v.(*Vertex); ok && x.Accept(ctx, f) {
846 return true
847 }
848 }
849 return false
850 }
851
852 if f.IsInt() {
853 switch v.BaseValue.(type) {
854 case *ListMarker:
855
856 if f.Index() < len(v.Elems()) {
857 return true
858 }
859 return !v.IsClosedList()
860
861 default:
862 return v.Kind()&ListKind != 0
863 }
864 }
865
866 if k := v.Kind(); k&StructKind == 0 && f.IsString() {
867
868
869 if k != BottomKind || len(v.Structs) == 0 {
870 return false
871 }
872 }
873
874 if !v.IsClosedStruct() || v.Lookup(f) != nil {
875 return true
876 }
877
878
879 defer ctx.ReleasePositions(ctx.MarkPositions())
880
881 return v.accepts(Accept(ctx, v, f))
882 }
883
884
885
886
887 func (v *Vertex) MatchAndInsert(ctx *OpContext, arc *Vertex) {
888 if !v.Accept(ctx, arc.Label) {
889 return
890 }
891
892
893 for i := len(v.Structs) - 1; i >= 0; i-- {
894 s := v.Structs[i]
895 if s.Disable {
896 continue
897 }
898 s.MatchAndInsert(ctx, arc)
899 }
900 }
901
902 func (v *Vertex) IsList() bool {
903 _, ok := v.BaseValue.(*ListMarker)
904 return ok
905 }
906
907
908 func (v *Vertex) Lookup(f Feature) *Vertex {
909 for _, a := range v.Arcs {
910 if a.Label == f {
911 a = a.Indirect()
912 return a
913 }
914 }
915 return nil
916 }
917
918
919 func (v *Vertex) Elems() []*Vertex {
920
921 a := make([]*Vertex, 0, len(v.Arcs))
922 for _, x := range v.Arcs {
923 if x.Label.IsInt() {
924 a = append(a, x)
925 }
926 }
927 return a
928 }
929
930
931
932 func (v *Vertex) GetArc(c *OpContext, f Feature, t ArcType) (arc *Vertex, isNew bool) {
933 unreachableForDev(c)
934
935 arc = v.Lookup(f)
936 if arc != nil {
937 arc.updateArcType(t)
938 return arc, false
939 }
940
941 if v.LockArcs {
942
943 if f.IsInt() {
944 c.addErrf(EvalError, token.NoPos,
945 "element at index %v not allowed by earlier comprehension or reference cycle", f)
946 } else {
947 c.addErrf(EvalError, token.NoPos,
948 "field %v not allowed by earlier comprehension or reference cycle", f)
949 }
950 }
951
952 arc = &Vertex{
953 Parent: v,
954 Label: f,
955 ArcType: t,
956 nonRooted: v.IsDynamic || v.Label.IsLet() || v.nonRooted,
957 }
958 v.Arcs = append(v.Arcs, arc)
959 if t == ArcPending {
960 v.hasPendingArc = true
961 }
962 return arc, true
963 }
964
965 func (v *Vertex) Source() ast.Node {
966 if v != nil {
967 if b, ok := v.BaseValue.(Value); ok {
968 return b.Source()
969 }
970 }
971 return nil
972 }
973
974
975 func (v *Vertex) AddConjunct(c Conjunct) *Bottom {
976 if v.BaseValue != nil && !isCyclePlaceholder(v.BaseValue) {
977
978
979
980
981 return &Bottom{Err: errors.Newf(token.NoPos, "cannot add conjunct")}
982 }
983 if !v.hasConjunct(c) {
984 v.addConjunctUnchecked(c)
985 }
986 return nil
987 }
988
989 func (v *Vertex) hasConjunct(c Conjunct) (added bool) {
990 switch f := c.x.(type) {
991 case *BulkOptionalField, *Ellipsis:
992 case *Field:
993 v.updateArcType(f.ArcType)
994 case *DynamicField:
995 v.updateArcType(f.ArcType)
996 default:
997 v.ArcType = ArcMember
998 }
999 return hasConjunct(v.Conjuncts, c)
1000 }
1001
1002 func hasConjunct(cs []Conjunct, c Conjunct) bool {
1003 for _, x := range cs {
1004
1005 if x.CloseInfo.closeInfo == c.CloseInfo.closeInfo &&
1006 x.x == c.x &&
1007 x.Env.Up == c.Env.Up && x.Env.Vertex == c.Env.Vertex {
1008 return true
1009 }
1010 }
1011 return false
1012 }
1013
1014 func (n *nodeContext) addConjunction(c Conjunct, index int) {
1015 unreachableForDev(n.ctx)
1016
1017
1018
1019 if x, ok := c.Elem().(*BinaryExpr); ok && x.Op == AndOp {
1020 c.x = x.X
1021 n.conjuncts = append(n.conjuncts, conjunct{C: c, index: index})
1022 c.x = x.Y
1023 n.conjuncts = append(n.conjuncts, conjunct{C: c, index: index})
1024 } else {
1025 n.conjuncts = append(n.conjuncts, conjunct{C: c, index: index})
1026 }
1027 }
1028
1029 func (v *Vertex) addConjunctUnchecked(c Conjunct) {
1030 index := len(v.Conjuncts)
1031 v.Conjuncts = append(v.Conjuncts, c)
1032 if n := v.state; n != nil && !n.ctx.isDevVersion() {
1033 n.addConjunction(c, index)
1034
1035
1036
1037
1038
1039 n.notifyConjunct(c)
1040 }
1041 }
1042
1043
1044
1045 func (n *nodeContext) addConjunctDynamic(c Conjunct) {
1046 unreachableForDev(n.ctx)
1047
1048 n.node.Conjuncts = append(n.node.Conjuncts, c)
1049 n.addExprConjunct(c, partial)
1050 n.notifyConjunct(c)
1051
1052 }
1053
1054 func (n *nodeContext) notifyConjunct(c Conjunct) {
1055 unreachableForDev(n.ctx)
1056
1057 for _, rec := range n.notify {
1058 arc := rec.v
1059 if !arc.hasConjunct(c) {
1060 if arc.state == nil {
1061
1062
1063
1064 n.ctx.Assertf(n.ctx.pos(), Debug, "unexpected nil state")
1065 n.ctx.addErrf(0, n.ctx.pos(), "cannot add to field %v", arc.Label)
1066 continue
1067 }
1068 arc.state.addConjunctDynamic(c)
1069 }
1070 }
1071 }
1072
1073 func (v *Vertex) AddStruct(s *StructLit, env *Environment, ci CloseInfo) *StructInfo {
1074 info := StructInfo{
1075 StructLit: s,
1076 Env: env,
1077 CloseInfo: ci,
1078 }
1079 for _, t := range v.Structs {
1080 if *t == info {
1081 return t
1082 }
1083 }
1084 t := &info
1085 v.Structs = append(v.Structs, t)
1086 return t
1087 }
1088
1089
1090
1091
1092
1093 func (v *Vertex) Path() []Feature {
1094 return appendPath(nil, v)
1095 }
1096
1097 func appendPath(a []Feature, v *Vertex) []Feature {
1098 if v.Parent == nil {
1099 return a
1100 }
1101 a = appendPath(a, v.Parent)
1102 if v.Label != 0 {
1103
1104 a = append(a, v.Label)
1105 }
1106 return a
1107 }
1108
1109
1110
1111 type Conjunct struct {
1112 Env *Environment
1113 x Node
1114
1115
1116
1117 CloseInfo CloseInfo
1118 }
1119
1120
1121
1122
1123
1124 func MakeRootConjunct(env *Environment, x Node) Conjunct {
1125 return MakeConjunct(env, x, CloseInfo{})
1126 }
1127
1128 func MakeConjunct(env *Environment, x Node, id CloseInfo) Conjunct {
1129 if env == nil {
1130
1131 env = &Environment{}
1132 }
1133 switch x.(type) {
1134 case Elem, interface{ expr() Expr }:
1135 default:
1136 panic(fmt.Sprintf("invalid Node type %T", x))
1137 }
1138 return Conjunct{env, x, id}
1139 }
1140
1141 func (c *Conjunct) Source() ast.Node {
1142 return c.x.Source()
1143 }
1144
1145 func (c *Conjunct) Field() Node {
1146 switch x := c.x.(type) {
1147 case *Comprehension:
1148 return x.Value
1149 default:
1150 return c.x
1151 }
1152 }
1153
1154
1155
1156 func (c *Conjunct) Elem() Elem {
1157 switch x := c.x.(type) {
1158 case interface{ expr() Expr }:
1159 return x.expr()
1160 case Elem:
1161 return x
1162 default:
1163 panic("unreachable")
1164 }
1165 }
1166
1167
1168
1169
1170
1171
1172
1173
1174 func (c *Conjunct) Expr() Expr {
1175 return ToExpr(c.x)
1176 }
1177
1178
1179
1180 func (c Conjunct) EnvExpr() (*Environment, Expr) {
1181 return EnvExpr(c.Env, c.Elem())
1182 }
1183
1184
1185
1186
1187 func EnvExpr(env *Environment, elem Elem) (*Environment, Expr) {
1188 for {
1189 if c, ok := elem.(*Comprehension); ok {
1190 env = linkChildren(env, c)
1191 c := MakeConjunct(env, c.Value, CloseInfo{})
1192 elem = c.Elem()
1193 continue
1194 }
1195 break
1196 }
1197 return env, ToExpr(elem)
1198 }
1199
1200
1201
1202
1203 func ToExpr(n Node) Expr {
1204 switch x := n.(type) {
1205 case Expr:
1206 return x
1207 case interface{ expr() Expr }:
1208 return x.expr()
1209 case *Comprehension:
1210 return ToExpr(x.Value)
1211 default:
1212 panic("unreachable")
1213 }
1214 }
1215
View as plain text