Skip to content

Commit

Permalink
internal/core/adt: precompute optional field type info
Browse files Browse the repository at this point in the history
HasOptional now only refers to `...T`
(which is not implemented yet)

Change-Id: I5f885894218694dba57e821df6aaa1d42f0c6982
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/8202
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Jan 15, 2021
1 parent 9eee235 commit 355cccd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
34 changes: 14 additions & 20 deletions internal/core/adt/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ type StructLit struct {
IsOpen bool // has a ...
initialized bool

types OptionalType

// administrative fields like hasreferences.
// hasReferences bool
}
Expand All @@ -59,6 +61,10 @@ type FieldInfo struct {
Optional []Node
}

func (x *StructLit) HasOptional() bool {
return x.types&(HasField|HasPattern|HasAdditional) != 0
}

func (x *StructLit) Source() ast.Node { return x.Src }

func (x *StructLit) evaluate(c *OpContext) Value {
Expand Down Expand Up @@ -96,9 +102,11 @@ func (o *StructLit) Init() {
o.Fields = append(o.Fields, FieldInfo{Label: x.Label})
}
o.Fields[p].Optional = append(o.Fields[p].Optional, x)
o.types |= HasField

case *DynamicField:
o.Dynamic = append(o.Dynamic, x)
o.types |= HasDynamic

case Expr:
o.HasEmbed = true
Expand All @@ -107,12 +115,16 @@ func (o *StructLit) Init() {

case *BulkOptionalField:
o.Bulk = append(o.Bulk, x)
o.types |= HasPattern

case *Ellipsis:
expr := x.Value
if x.Value == nil {
o.IsOpen = true
o.types |= IsOpen
expr = &Top{}
} else {
o.types |= HasAdditional
}
o.Additional = append(o.Additional, expr)

Expand All @@ -131,26 +143,8 @@ func (o *StructLit) fieldIndex(f Feature) int {
return -1
}

func (o *StructLit) OptionalTypes() (mask OptionalType) {
for _, f := range o.Fields {
if len(f.Optional) > 0 {
mask = HasField
break
}
}
if len(o.Dynamic) > 0 {
mask |= HasDynamic
}
if len(o.Bulk) > 0 {
mask |= HasPattern
}
if o.Additional != nil {
mask |= HasAdditional
}
if o.IsOpen {
mask |= IsOpen
}
return mask
func (o *StructLit) OptionalTypes() OptionalType {
return o.types
}

func (o *StructLit) IsOptional(label Feature) bool {
Expand Down
2 changes: 1 addition & 1 deletion internal/core/adt/optional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestOptionalTypes(t *testing.T) {
in: `
...
`,
out: adt.HasAdditional | adt.IsOpen,
out: adt.IsOpen,
}, {
in: `
[string]: int
Expand Down

0 comments on commit 355cccd

Please sign in to comment.