-
Notifications
You must be signed in to change notification settings - Fork 288
/
dep.go
329 lines (273 loc) · 7.7 KB
/
dep.go
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
// Copyright 2020 CUE Authors
//
// 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
//
// http://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.
// Package dep analyzes dependencies between values.
package dep
import (
"errors"
"cuelang.org/go/internal/core/adt"
)
// A Dependency is a reference and the node that reference resolves to.
type Dependency struct {
// Node is the referenced node.
Node *adt.Vertex
// Reference is the expression that referenced the node.
Reference adt.Resolver
top bool
}
// Import returns the import reference or nil if the reference was within
// the same package as the visited Vertex.
func (d *Dependency) Import() *adt.ImportReference {
x, _ := d.Reference.(adt.Expr)
return importRef(x)
}
// IsRoot reports whether the dependency is referenced by the root of the
// original Vertex passed to any of the Visit* functions, and not one of its
// descendent arcs. This always returns true for Visit().
func (d *Dependency) IsRoot() bool {
return d.top
}
func (d *Dependency) Path() []adt.Feature {
return nil
}
func importRef(r adt.Expr) *adt.ImportReference {
switch x := r.(type) {
case *adt.ImportReference:
return x
case *adt.SelectorExpr:
return importRef(x.X)
case *adt.IndexExpr:
return importRef(x.X)
}
return nil
}
// VisitFunc is used for reporting dependencies.
type VisitFunc func(Dependency) error
// Visit calls f for all vertices referenced by the conjuncts of n without
// descending into the elements of list or fields of structs. Only references
// that do not refer to the conjuncts of n itself are reported.
func Visit(c *adt.OpContext, n *adt.Vertex, f VisitFunc) error {
return visit(c, n, f, false, true)
}
// VisitAll calls f for all vertices referenced by the conjuncts of n including
// those of descendant fields and elements. Only references that do not refer to
// the conjuncts of n itself are reported.
func VisitAll(c *adt.OpContext, n *adt.Vertex, f VisitFunc) error {
return visit(c, n, f, true, true)
}
// VisitFields calls f for n and all its descendent arcs that have a conjunct
// that originates from a conjunct in n. Only the conjuncts of n that ended up
// as a conjunct in an actual field are visited and they are visited for each
// field in which the occurs.
func VisitFields(c *adt.OpContext, n *adt.Vertex, f VisitFunc) error {
m := marked{}
m.markExpr(n)
dynamic(c, n, f, m, true)
return nil
}
var empty *adt.Vertex
func init() {
// TODO: Consider setting a non-nil BaseValue.
empty = &adt.Vertex{}
empty.UpdateStatus(adt.Finalized)
}
func visit(c *adt.OpContext, n *adt.Vertex, f VisitFunc, all, top bool) (err error) {
if c == nil {
panic("nil context")
}
v := visitor{
ctxt: c,
visit: f,
node: n,
all: all,
top: top,
}
defer func() {
switch x := recover(); x {
case nil:
case aborted:
err = v.err
default:
panic(x)
}
}()
for _, x := range n.Conjuncts {
v.markExpr(x.Env, x.Elem())
}
return nil
}
var aborted = errors.New("aborted")
type visitor struct {
ctxt *adt.OpContext
visit VisitFunc
node *adt.Vertex
err error
all bool
top bool
}
// TODO: factor out the below logic as either a low-level dependency analyzer or
// some walk functionality.
// markExpr visits all nodes in an expression to mark dependencies.
func (c *visitor) markExpr(env *adt.Environment, expr adt.Elem) {
switch x := expr.(type) {
case nil:
case adt.Resolver:
c.markResolver(env, x)
case *adt.BinaryExpr:
c.markExpr(env, x.X)
c.markExpr(env, x.Y)
case *adt.UnaryExpr:
c.markExpr(env, x.X)
case *adt.Interpolation:
for i := 1; i < len(x.Parts); i += 2 {
c.markExpr(env, x.Parts[i])
}
case *adt.BoundExpr:
c.markExpr(env, x.Expr)
case *adt.CallExpr:
c.markExpr(env, x.Fun)
saved := c.all
c.all = true
for _, a := range x.Args {
c.markExpr(env, a)
}
c.all = saved
case *adt.DisjunctionExpr:
for _, d := range x.Values {
c.markExpr(env, d.Val)
}
case *adt.SliceExpr:
c.markExpr(env, x.X)
c.markExpr(env, x.Lo)
c.markExpr(env, x.Hi)
c.markExpr(env, x.Stride)
case *adt.ListLit:
env := &adt.Environment{Up: env, Vertex: empty}
for _, e := range x.Elems {
switch x := e.(type) {
case *adt.Comprehension:
c.markComprehension(env, x)
case adt.Expr:
c.markSubExpr(env, x)
case *adt.Ellipsis:
if x.Value != nil {
c.markSubExpr(env, x.Value)
}
}
}
case *adt.StructLit:
env := &adt.Environment{Up: env, Vertex: empty}
for _, e := range x.Decls {
c.markDecl(env, e)
}
}
}
// markResolve resolves dependencies.
func (c *visitor) markResolver(env *adt.Environment, r adt.Resolver) {
switch x := r.(type) {
case nil:
case *adt.LetReference:
saved := c.ctxt.PushState(env, nil)
env := c.ctxt.Env(x.UpCount)
c.markExpr(env, x.X)
c.ctxt.PopState(saved)
return
}
if ref, _ := c.ctxt.Resolve(env, r); ref != nil {
if ref != c.node && ref != empty {
d := Dependency{
Node: ref,
Reference: r,
top: c.top,
}
if err := c.visit(d); err != nil {
c.err = err
panic(aborted)
}
}
return
}
// It is possible that a reference cannot be resolved because it is
// incomplete. In this case, we should check whether subexpressions of the
// reference can be resolved to mark those dependencies. For instance,
// prefix paths of selectors and the value or index of an index experssion
// may independently resolve to a valid dependency.
switch x := r.(type) {
case *adt.NodeLink:
panic("unreachable")
case *adt.IndexExpr:
c.markExpr(env, x.X)
c.markExpr(env, x.Index)
case *adt.SelectorExpr:
c.markExpr(env, x.X)
}
}
func (c *visitor) markSubExpr(env *adt.Environment, x adt.Expr) {
if c.all {
saved := c.top
c.top = false
c.markExpr(env, x)
c.top = saved
}
}
func (c *visitor) markDecl(env *adt.Environment, d adt.Decl) {
switch x := d.(type) {
case *adt.Field:
c.markSubExpr(env, x.Value)
case *adt.OptionalField:
// when dynamic, only continue if there is evidence of
// the field in the parallel actual evaluation.
c.markSubExpr(env, x.Value)
case *adt.BulkOptionalField:
c.markExpr(env, x.Filter)
// when dynamic, only continue if there is evidence of
// the field in the parallel actual evaluation.
c.markSubExpr(env, x.Value)
case *adt.DynamicField:
c.markExpr(env, x.Key)
// when dynamic, only continue if there is evidence of
// a matching field in the parallel actual evaluation.
c.markSubExpr(env, x.Value)
case *adt.Comprehension:
c.markComprehension(env, x)
case adt.Expr:
c.markExpr(env, x)
case *adt.Ellipsis:
if x.Value != nil {
c.markSubExpr(env, x.Value)
}
}
}
func (c *visitor) markComprehension(env *adt.Environment, y *adt.Comprehension) {
env = c.markYielder(env, y.Clauses)
c.markExpr(env, y.Value)
}
func (c *visitor) markYielder(env *adt.Environment, y adt.Yielder) *adt.Environment {
switch x := y.(type) {
case *adt.ForClause:
c.markExpr(env, x.Src)
env = &adt.Environment{Up: env, Vertex: empty}
env = c.markYielder(env, x.Dst)
// In dynamic mode, iterate over all actual value and
// evaluate.
case *adt.LetClause:
c.markExpr(env, x.Expr)
env = &adt.Environment{Up: env, Vertex: empty}
env = c.markYielder(env, x.Dst)
case *adt.IfClause:
c.markExpr(env, x.Condition)
// In dynamic mode, only continue if condition is true.
env = c.markYielder(env, x.Dst)
}
return env
}