forked from cuelang/cue
/
expr.go
420 lines (357 loc) · 9.35 KB
/
expr.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
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
// 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 export
import (
"fmt"
"sort"
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal/core/adt"
)
// Modes:
// raw: as is
// def: merge structs, print reset as is.
//
// Possible simplifications in def mode:
// - merge contents of multiple _literal_ structs.
// - this is not possible if some of the elements are bulk optional
// (or is it?).
// - still do not ever resolve references.
// - to do this, fields must be pre-linked to their destinations.
// - use astutil.Sanitize to resolve shadowing and imports.
//
//
// Categories of printing:
// - concrete
// - optionals
// - references
// - constraints
//
// Mixed mode is also not supported in the old implementation (at least not
// correctly). It requires references to resolve properly, backtracking to
// a common root and prefixing that to the reference. This is now possible
// with the Environment construct and could be done later.
func (e *exporter) expr(v adt.Expr) (result ast.Expr) {
switch x := v.(type) {
case nil:
return nil
case *adt.Vertex:
if len(x.Conjuncts) == 0 || x.IsData() {
// Treat as literal value.
return e.value(x)
} // Should this be the arcs label?
a := []conjunct{}
for _, c := range x.Conjuncts {
a = append(a, conjunct{c, 0})
}
return e.mergeValues(adt.InvalidLabel, x, a, x.Conjuncts...)
case *adt.StructLit:
c := adt.MakeRootConjunct(nil, x)
return e.mergeValues(adt.InvalidLabel, nil, []conjunct{{c: c, up: 0}}, c)
case adt.Value:
return e.value(x) // Use conjuncts.
default:
return e.adt(v, nil)
}
}
// Piece out values:
// For a struct, piece out conjuncts that are already values. Those can be
// unified. All other conjuncts are added verbatim.
func (x *exporter) mergeValues(label adt.Feature, src *adt.Vertex, a []conjunct, orig ...adt.Conjunct) ast.Expr {
e := conjuncts{
exporter: x,
values: &adt.Vertex{},
fields: map[adt.Feature]field{},
attrs: []*ast.Attribute{},
}
_, saved := e.pushFrame(orig)
defer e.popFrame(saved)
for _, c := range a {
e.top().upCount = c.up
x := c.c.Expr()
e.addExpr(c.c.Env, x, false)
}
s := x.top().scope
for _, a := range e.attrs {
s.Elts = append(s.Elts, a)
}
// Unify values only for one level.
if len(e.values.Conjuncts) > 0 {
e.values.Finalize(e.ctx)
e.embed = append(e.embed, e.value(e.values, e.values.Conjuncts...))
}
// Collect and order set of fields.
fields := []adt.Feature{}
for f := range e.fields {
fields = append(fields, f)
}
// Sort fields in case features lists are missing to ensure
// predictability. Also sort in reverse order, so that bugs
// are more likely exposed.
sort.Slice(fields, func(i, j int) bool {
return fields[i] > fields[j]
})
m := sortArcs(extractFeatures(e.structs))
sort.SliceStable(fields, func(i, j int) bool {
if m[fields[j]] == 0 {
return m[fields[i]] != 0
}
return m[fields[i]] > m[fields[j]]
})
if len(e.fields) == 0 && !e.hasEllipsis {
switch len(e.embed) + len(e.conjuncts) {
case 0:
if len(e.structs) > 0 {
return s
}
return ast.NewIdent("_")
case 1:
if len(e.conjuncts) == 1 {
return e.conjuncts[0]
}
return e.embed[0]
case 2:
// Simplify.
e.conjuncts = append(e.conjuncts, e.embed...)
return ast.NewBinExpr(token.AND, e.conjuncts...)
}
}
for _, x := range e.embed {
s.Elts = append(s.Elts, &ast.EmbedDecl{Expr: x})
}
for _, f := range fields {
field := e.fields[f]
c := field.conjuncts
label := e.stringLabel(f)
if f.IsDef() {
x.inDefinition++
}
a := []adt.Conjunct{}
for _, cc := range c {
a = append(a, cc.c)
}
d := &ast.Field{Label: label}
top := e.frame(0)
if fr, ok := top.fields[f]; ok && fr.alias != "" {
setFieldAlias(d, fr.alias)
fr.node = d
top.fields[f] = fr
}
d.Value = e.mergeValues(f, nil, c, a...)
if f.IsDef() {
x.inDefinition--
}
if isOptional(a) {
d.Optional = token.Blank.Pos()
}
if x.cfg.ShowDocs {
docs := extractDocs(src, a)
ast.SetComments(d, docs)
}
if x.cfg.ShowAttributes {
d.Attrs = ExtractFieldAttrs(a)
}
s.Elts = append(s.Elts, d)
}
if e.hasEllipsis {
s.Elts = append(s.Elts, &ast.Ellipsis{})
} else if src != nil && src.IsClosed(e.ctx) && e.inDefinition == 0 {
return ast.NewCall(ast.NewIdent("close"), s)
}
e.conjuncts = append(e.conjuncts, s)
return ast.NewBinExpr(token.AND, e.conjuncts...)
}
// Conjuncts if for collecting values of a single vertex.
type conjuncts struct {
*exporter
// Values is used to collect non-struct values.
values *adt.Vertex
embed []ast.Expr
conjuncts []ast.Expr
structs []*adt.StructInfo
fields map[adt.Feature]field
attrs []*ast.Attribute
hasEllipsis bool
}
func (c *conjuncts) addConjunct(f adt.Feature, env *adt.Environment, n adt.Node) {
x := c.fields[f]
v := adt.MakeRootConjunct(env, n)
x.conjuncts = append(x.conjuncts, conjunct{
c: v,
up: c.top().upCount,
})
// x.upCounts = append(x.upCounts, c.top().upCount)
c.fields[f] = x
}
type field struct {
docs []*ast.CommentGroup
conjuncts []conjunct
}
type conjunct struct {
c adt.Conjunct
up int32
}
func (e *conjuncts) addExpr(env *adt.Environment, x adt.Expr, isEmbed bool) {
switch x := x.(type) {
case *adt.StructLit:
e.top().upCount++
if e.cfg.ShowAttributes {
e.attrs = extractDeclAttrs(e.attrs, x.Src)
}
// Only add if it only has no bulk fields or elipsis.
if isComplexStruct(x) {
_, saved := e.pushFrame(nil)
e.embed = append(e.embed, e.adt(x, nil))
e.popFrame(saved)
return
}
// Used for sorting.
e.structs = append(e.structs, &adt.StructInfo{StructLit: x, Env: env})
for _, d := range x.Decls {
var label adt.Feature
switch f := d.(type) {
case *adt.Field:
label = f.Label
case *adt.OptionalField:
// TODO: mark optional here.
label = f.Label
case *adt.Ellipsis:
e.hasEllipsis = true
continue
case adt.Expr:
e.addExpr(env, f, true)
continue
// TODO: also handle dynamic fields
default:
panic(fmt.Sprintf("Unexpected type %T", d))
}
e.addConjunct(label, env, d)
}
e.top().upCount--
case adt.Value: // other values.
switch v := x.(type) {
case nil:
default:
e.values.AddConjunct(adt.MakeRootConjunct(env, x)) // GOBBLE TOP
case *adt.Vertex:
e.structs = append(e.structs, v.Structs...)
switch y := v.BaseValue.(type) {
case *adt.ListMarker:
a := []ast.Expr{}
for _, x := range v.Elems() {
a = append(a, e.expr(x))
}
if !v.IsClosed(e.ctx) {
v := &adt.Vertex{}
v.MatchAndInsert(e.ctx, v)
a = append(a, &ast.Ellipsis{Type: e.expr(v)})
}
e.embed = append(e.embed, ast.NewList(a...))
return
case *adt.StructMarker:
x = nil
case adt.Value:
e.values.AddConjunct(adt.MakeRootConjunct(env, y)) // GOBBLE TOP
}
// generated, only consider arcs.
for _, a := range v.Arcs {
a.Finalize(e.ctx) // TODO: should we do this?
e.addConjunct(a.Label, env, a)
}
// e.exprs = append(e.exprs, e.value(v, v.Conjuncts...))
}
case *adt.BinaryExpr:
switch {
case x.Op == adt.AndOp && !isEmbed:
e.addExpr(env, x.X, false)
e.addExpr(env, x.Y, false)
case isSelfContained(x):
e.values.AddConjunct(adt.MakeRootConjunct(env, x))
default:
if isEmbed {
e.embed = append(e.embed, e.expr(x))
} else {
e.conjuncts = append(e.conjuncts, e.expr(x))
}
}
default:
switch {
case isSelfContained(x):
e.values.AddConjunct(adt.MakeRootConjunct(env, x))
case isEmbed:
e.embed = append(e.embed, e.expr(x))
default:
e.conjuncts = append(e.conjuncts, e.expr(x))
}
}
}
// TODO: find a better way to annotate optionality. Maybe a special conjunct
// or store it in the field information?
func isOptional(a []adt.Conjunct) bool {
if len(a) == 0 {
return false
}
for _, c := range a {
switch f := c.Source().(type) {
case nil:
return false
case *ast.Field:
if f.Optional == token.NoPos {
return false
}
}
}
return true
}
func isComplexStruct(s *adt.StructLit) bool {
for _, d := range s.Decls {
switch x := d.(type) {
case *adt.Field:
// TODO: remove this and also handle field annotation in expr().
// This allows structs to be merged. Ditto below.
if x.Src != nil {
if _, ok := x.Src.Label.(*ast.Alias); ok {
return ok
}
}
case *adt.OptionalField:
if x.Src != nil {
if _, ok := x.Src.Label.(*ast.Alias); ok {
return ok
}
}
case adt.Expr:
case *adt.Ellipsis:
if x.Value != nil {
return true
}
default:
return true
}
}
return false
}
func isSelfContained(expr adt.Expr) bool {
switch x := expr.(type) {
case *adt.BinaryExpr:
return isSelfContained(x.X) && isSelfContained(x.Y)
case *adt.UnaryExpr:
return isSelfContained(x.X)
case *adt.BoundExpr:
return isSelfContained(x.Expr)
case adt.Value:
return true
}
return false
}