Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
internal/core/adt: change closeInfo.embedded to a flag field
Browse files Browse the repository at this point in the history
This prepares for collecting more origin information into a
single field. The additional information will be helpful for
implementing trim and potentially other algorithms, and
will give more context for error messages (if we move to
a more dynamic model).

This could be implemented as more booleans, but grouping
them as flags seems clearer and less error-prone w.r.t.
deciding what needs to be copied from the parent closeInfo.
It probably will also be more performant.

Change-Id: Id131aa62300d8e989c22cf4f308aeb5fc53cf238
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/8236
Reviewed-by: CUE cueckoo <[email protected]>
Reviewed-by: Paul Jolly <[email protected]>
Reviewed-by: Marcel van Lohuizen <[email protected]>
  • Loading branch information
mpvl committed Jan 20, 2021
1 parent ca5d2db commit 8e6e795
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions internal/core/adt/closed.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,16 @@ func (c *CloseInfo) AddPositions(ctx *OpContext) {
// TODO(perf): use on StructInfo. Then if parent and expression are the same
// it is possible to use cached value.
func (c CloseInfo) SpawnEmbed(x Expr) CloseInfo {
var span SpanType
if c.closeInfo != nil {
span = c.span
}

c.closeInfo = &closeInfo{
parent: c.closeInfo,
location: x,
mode: closeEmbed,
embedded: true,
span: span | EmbeddingSpan,
}
return c
}
Expand All @@ -124,27 +129,27 @@ func (c CloseInfo) SpawnEmbed(x Expr) CloseInfo {
// a: {#foo} & {b: int}
//
func (c CloseInfo) SpawnGroup(x Expr) CloseInfo {
embedded := false
var span SpanType
if c.closeInfo != nil {
embedded = c.embedded
span = c.span
}
c.closeInfo = &closeInfo{
parent: c.closeInfo,
location: x,
embedded: embedded,
span: span,
}
return c
}

func (c CloseInfo) SpawnRef(arc *Vertex, isDef bool, x Expr) CloseInfo {
embedded := false
var span SpanType
if c.closeInfo != nil {
embedded = c.embedded
span = c.span
}
c.closeInfo = &closeInfo{
parent: c.closeInfo,
location: x,
embedded: embedded,
span: span,
}
if isDef {
c.mode = closeDef
Expand Down Expand Up @@ -174,6 +179,22 @@ func IsDef(x Expr) bool {
return false
}

// A SpanType is used to indicate whether a CUE value is within the scope of
// a certain CUE language construct, the span type.
type SpanType uint8

const (
// EmbeddingSpan means that this value was embedded at some point and should
// not be included as a possible root node in the todo field of OpContext.
EmbeddingSpan SpanType = 1 << iota

// TODO:
// from comprehension
// from template.
// from definition

)

type closeInfo struct {
// location records the expression that led to this node's introduction.
location Node
Expand All @@ -193,9 +214,7 @@ type closeInfo struct {
// - it is a sibling of a new definition.
noCheck bool // don't process for inclusion info

// embedded means that this value was embedded at some point and should
// not be included as a possible root node in the todo field of OpContext.
embedded bool
span SpanType
}

// closeStats holds the administrative fields for a closeInfo value. Each
Expand Down Expand Up @@ -303,7 +322,7 @@ func markRequired(ctx *OpContext, info *closeInfo) {
return
}

if !s.embedded {
if s.span&EmbeddingSpan == 0 {
x.next = ctx.todo
ctx.todo = x
}
Expand Down

0 comments on commit 8e6e795

Please sign in to comment.