Skip to content

Commit

Permalink
internal/core: move CloseID from Environment to Conjunct
Browse files Browse the repository at this point in the history
Change-Id: If83db9714764e30c764c8947cfb3936beafd8bc5
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7001
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
  • Loading branch information
mpvl committed Sep 10, 2020
1 parent d55e2b5 commit 3d863fc
Show file tree
Hide file tree
Showing 19 changed files with 171 additions and 173 deletions.
2 changes: 1 addition & 1 deletion cue/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func init() {
expr = &adt.Bottom{Err: err}
}
n := &adt.Vertex{}
n.AddConjunct(adt.MakeConjunct(nil, expr))
n.AddConjunct(adt.MakeRootConjunct(nil, expr))
return Value{r.idx, n}

// return convertType(runtime.(*Runtime), x)
Expand Down
8 changes: 4 additions & 4 deletions cue/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func init() {
st, ok := x.(*adt.Vertex)
if !ok {
st = &adt.Vertex{}
st.AddConjunct(adt.MakeConjunct(nil, x))
st.AddConjunct(adt.MakeRootConjunct(nil, x))
}
return v.ctx().index.addInst(&Instance{
root: st,
Expand Down Expand Up @@ -231,7 +231,7 @@ func Merge(inst ...*Instance) *Instance {

for _, i := range inst {
w := i.Value()
v.AddConjunct(adt.MakeConjunct(nil, w.v.ToDataAll()))
v.AddConjunct(adt.MakeRootConjunct(nil, w.v.ToDataAll()))
}
v.Finalize(ctx)

Expand All @@ -255,7 +255,7 @@ func (inst *Instance) Build(p *build.Instance) *Instance {

v, err := compile.Files(&compile.Config{Scope: inst.root}, r, p.Files...)

v.AddConjunct(adt.MakeConjunct(nil, inst.root))
v.AddConjunct(adt.MakeRootConjunct(nil, inst.root))

i := newInstance(idx, p, v)
if rErr != nil {
Expand Down Expand Up @@ -345,7 +345,7 @@ func (inst *Instance) Fill(x interface{}, path ...string) (*Instance, error) {
} else {
ctx := eval.NewContext(inst.index.Runtime, nil)
expr := convert.GoValueToExpr(ctx, true, x)
u.AddConjunct(adt.MakeConjunct(nil, expr))
u.AddConjunct(adt.MakeRootConjunct(nil, expr))
u.Finalize(ctx)
}
inst = inst.index.addInst(&Instance{
Expand Down
24 changes: 12 additions & 12 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ func newErrValue(v Value, b *bottom) Value {
node.Parent = v.v.Parent
}
node.UpdateStatus(adt.Finalized)
node.AddConjunct(adt.MakeConjunct(nil, b))
node.AddConjunct(adt.MakeRootConjunct(nil, b))
return makeValue(v.idx, node)
}

Expand All @@ -590,7 +590,7 @@ func newValueRoot(ctx *context, x value) Value {
return newVertexRoot(ctx, n)
}
node := &adt.Vertex{}
node.AddConjunct(adt.MakeConjunct(nil, x))
node.AddConjunct(adt.MakeRootConjunct(nil, x))
return newVertexRoot(ctx, node)
}

Expand Down Expand Up @@ -648,7 +648,7 @@ func remakeValue(base Value, env *adt.Environment, v value) Value {
return Value{base.idx, v}
}
n := &adt.Vertex{Parent: base.v.Parent, Label: base.v.Label}
n.AddConjunct(adt.MakeConjunct(env, v))
n.AddConjunct(adt.MakeRootConjunct(env, v))
n = base.ctx().manifest(n)
return makeValue(base.idx, n)
}
Expand Down Expand Up @@ -1556,7 +1556,7 @@ func (v Value) Fill(x interface{}, path ...string) Value {
n, _ := value.(*adt.Vertex)
if n == nil {
n = &adt.Vertex{Label: v.v.Label, Parent: v.v.Parent}
n.AddConjunct(adt.MakeConjunct(nil, value))
n.AddConjunct(adt.MakeRootConjunct(nil, value))
} else {
n.Label = v.v.Label
}
Expand Down Expand Up @@ -1653,8 +1653,8 @@ func (v Value) Unify(w Value) Value {
return v
}
n := &adt.Vertex{Parent: v.v.Parent, Label: v.v.Label}
n.AddConjunct(adt.MakeConjunct(nil, v.v))
n.AddConjunct(adt.MakeConjunct(nil, w.v))
n.AddConjunct(adt.MakeRootConjunct(nil, v.v))
n.AddConjunct(adt.MakeRootConjunct(nil, w.v))

ctx := v.idx.newContext()
n.Finalize(ctx.opCtx)
Expand All @@ -1675,8 +1675,8 @@ func (v Value) UnifyAccept(w Value, accept Value) Value {
}

n := &adt.Vertex{Parent: v.v.Parent, Label: v.v.Label}
n.AddConjunct(adt.MakeConjunct(nil, v.v))
n.AddConjunct(adt.MakeConjunct(nil, w.v))
n.AddConjunct(adt.MakeRootConjunct(nil, v.v))
n.AddConjunct(adt.MakeRootConjunct(nil, w.v))

e := eval.New(v.idx.Runtime)
ctx := e.NewContext(n)
Expand Down Expand Up @@ -2166,8 +2166,8 @@ func (v Value) Expr() (Op, []Value) {
Closed: v.v.Closed,
}
b := a
a.AddConjunct(adt.MakeConjunct(env, n.Val))
b.AddConjunct(adt.MakeConjunct(env, disjunct.Val))
a.AddConjunct(adt.MakeRootConjunct(env, n.Val))
b.AddConjunct(adt.MakeRootConjunct(env, disjunct.Val))

e := eval.New(v.idx.Runtime)
ctx := e.NewContext(nil)
Expand Down Expand Up @@ -2246,7 +2246,7 @@ func (v Value) Expr() (Op, []Value) {
n := &adt.Vertex{
Parent: v.v.Parent,
Label: v.v.Label}
c := adt.MakeConjunct(envEmbed, x)
c := adt.MakeRootConjunct(envEmbed, x)
n.AddConjunct(c)
n.Finalize(ctx)
a = append(a, makeValue(v.idx, n))
Expand All @@ -2265,7 +2265,7 @@ func (v Value) Expr() (Op, []Value) {
Parent: v.v.Parent,
Label: v.v.Label,
}
c := adt.MakeConjunct(env, &adt.StructLit{
c := adt.MakeRootConjunct(env, &adt.StructLit{
Decls: fields,
})
n.AddConjunct(c)
Expand Down
26 changes: 18 additions & 8 deletions internal/core/adt/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ type Environment struct {
// TODO(perf): make the following public fields a shareable struct as it
// mostly is going to be the same for child nodes.

// CloseID is a unique number that tracks a group of conjuncts that need
// belong to a single originating definition.
CloseID uint32

// Cyclic indicates a structural cycle was detected for this conjunct or one
// of its ancestors.
Cyclic bool
Expand Down Expand Up @@ -129,6 +125,8 @@ type Environment struct {
cache map[Expr]Value
}

type ID int32

// evalCached is used to look up let expressions. Caching let expressions
// prevents a possible combinatorial explosion.
func (e *Environment) evalCached(c *OpContext, x Expr) Value {
Expand Down Expand Up @@ -337,7 +335,7 @@ func ToVertex(v Value) *Vertex {
status: Finalized,
Value: x,
}
n.AddConjunct(MakeConjunct(nil, v))
n.AddConjunct(MakeRootConjunct(nil, v))
return n
}
}
Expand Down Expand Up @@ -557,13 +555,25 @@ func (v *Vertex) appendListArcs(arcs []*Vertex) (err *Bottom) {
type Conjunct struct {
Env *Environment
x Node

// CloseID is a unique number that tracks a group of conjuncts that need
// belong to a single originating definition.
CloseID ID
}

func (c *Conjunct) ID() ID {
return c.CloseID
}

// TODO(perf): replace with composite literal if this helps performance.

// MakeConjunct creates a conjunct from the given environment and node.
// MakeRootConjunct creates a conjunct from the given environment and node.
// It panics if x cannot be used as an expression.
func MakeConjunct(env *Environment, x Node) Conjunct {
func MakeRootConjunct(env *Environment, x Node) Conjunct {
return MakeConjunct(env, x, 0)
}

func MakeConjunct(env *Environment, x Node, id ID) Conjunct {
if env == nil {
// TODO: better is to pass one.
env = &Environment{}
Expand All @@ -573,7 +583,7 @@ func MakeConjunct(env *Environment, x Node) Conjunct {
default:
panic(fmt.Sprintf("invalid Node type %T", x))
}
return Conjunct{env, x}
return Conjunct{env, x, id}
}

func (c *Conjunct) Source() ast.Node {
Expand Down
7 changes: 1 addition & 6 deletions internal/core/adt/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,6 @@ func (c *OpContext) spawn(node *Vertex) *OpContext {
sub := *c
node.Parent = c.e.Vertex
sub.e = &Environment{Up: c.e, Vertex: node}
if c.e != nil {
sub.e.CloseID = c.e.CloseID
}
return &sub
}

Expand Down Expand Up @@ -290,9 +287,7 @@ func (c *OpContext) PushState(env *Environment, src ast.Node) (saved frame) {
if src != nil {
c.src = src
}
if env != nil {
c.e = env
}
c.e = env

return saved
}
Expand Down
2 changes: 1 addition & 1 deletion internal/core/adt/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (v *Vertex) Default() *Vertex {
for _, c := range v.Conjuncts {
// TODO: preserve field information.
expr, _ := stripNonDefaults(c.Expr())
w.Conjuncts = append(w.Conjuncts, MakeConjunct(c.Env, expr))
w.Conjuncts = append(w.Conjuncts, MakeRootConjunct(c.Env, expr))
}
return w

Expand Down
14 changes: 7 additions & 7 deletions internal/core/adt/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (x *StructLit) Source() ast.Node { return x.Src }

func (x *StructLit) evaluate(c *OpContext) Value {
e := c.Env(0)
v := &Vertex{Conjuncts: []Conjunct{{e, x}}}
v := &Vertex{Conjuncts: []Conjunct{{e, x, 0}}}
c.Unifier.Unify(c, v, Finalized) // TODO: also partial okay?
return v
}
Expand Down Expand Up @@ -168,7 +168,7 @@ func (x *ListLit) Source() ast.Node {

func (x *ListLit) evaluate(c *OpContext) Value {
e := c.Env(0)
v := &Vertex{Conjuncts: []Conjunct{{e, x}}}
v := &Vertex{Conjuncts: []Conjunct{{e, x, 0}}}
c.Unifier.Unify(c, v, Finalized) // TODO: also partial okay?
return v
}
Expand Down Expand Up @@ -547,7 +547,7 @@ func (x *LetReference) resolve(c *OpContext) *Vertex {
e := c.Env(x.UpCount)
label := e.Vertex.Label
// Anonymous arc.
return &Vertex{Parent: nil, Label: label, Conjuncts: []Conjunct{{e, x.X}}}
return &Vertex{Parent: nil, Label: label, Conjuncts: []Conjunct{{e, x.X, 0}}}
}

func (x *LetReference) evaluate(c *OpContext) Value {
Expand Down Expand Up @@ -822,7 +822,7 @@ func (x *BinaryExpr) evaluate(c *OpContext) Value {
env := c.Env(0)
if x.Op == AndOp {
// Anonymous Arc
v := Vertex{Conjuncts: []Conjunct{{env, x}}}
v := Vertex{Conjuncts: []Conjunct{{env, x, 0}}}
return c.Unifier.Evaluate(c, &v)
}

Expand Down Expand Up @@ -1094,7 +1094,7 @@ func (x *DisjunctionExpr) Source() ast.Node {

func (x *DisjunctionExpr) evaluate(c *OpContext) Value {
e := c.Env(0)
v := &Vertex{Conjuncts: []Conjunct{{e, x}}}
v := &Vertex{Conjuncts: []Conjunct{{e, x, 0}}}
c.Unifier.Unify(c, v, Finalized) // TODO: also partial okay?
// TODO: if the disjunction result originated from a literal value, we may
// consider the result closed to create more permanent errors.
Expand Down Expand Up @@ -1178,7 +1178,7 @@ func (x *ForClause) yield(c *OpContext, f YieldFunc) {
if x.Key != 0 {
v := &Vertex{Label: x.Key}
key := a.Label.ToValue(c)
v.AddConjunct(MakeConjunct(c.Env(0), key))
v.AddConjunct(MakeRootConjunct(c.Env(0), key))
v.SetValue(c, Finalized, key)
n.Arcs = append(n.Arcs, v)
}
Expand Down Expand Up @@ -1234,7 +1234,7 @@ func (x *LetClause) Source() ast.Node {

func (x *LetClause) yield(c *OpContext, f YieldFunc) {
n := &Vertex{Arcs: []*Vertex{
{Label: x.Label, Conjuncts: []Conjunct{{c.Env(0), x.Expr}}},
{Label: x.Label, Conjuncts: []Conjunct{{c.Env(0), x.Expr, 0}}},
}}
x.Dst.yield(c.spawn(n), f)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/core/compile/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ var orBuiltin = &adt.Builtin{
}
v := &adt.Vertex{}
// TODO: make a Disjunction.
v.AddConjunct(adt.MakeConjunct(nil,
v.AddConjunct(adt.MakeRootConjunct(nil,
&adt.DisjunctionExpr{Values: d, HasDefaults: false},
))
c.Unify(c, v, adt.Finalized)
Expand Down
4 changes: 2 additions & 2 deletions internal/core/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func (c *compiler) compileFiles(a []*ast.File) *adt.Vertex { // Or value?
c.pushScope(nil, 0, file) // File scope
v := &adt.StructLit{Src: file}
c.addDecls(v, file.Decls)
res.Conjuncts = append(res.Conjuncts, adt.MakeConjunct(nil, v))
res.Conjuncts = append(res.Conjuncts, adt.MakeRootConjunct(nil, v))
c.popScope()
}

Expand Down Expand Up @@ -278,7 +278,7 @@ func (c *compiler) compileExpr(x ast.Expr) adt.Conjunct {
// env = e
}

return adt.MakeConjunct(env, expr)
return adt.MakeRootConjunct(env, expr)
}

// resolve assumes that all existing resolutions are legal. Validation should
Expand Down
10 changes: 5 additions & 5 deletions internal/core/convert/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func toValue(e adt.Expr) adt.Value {
return v
}
obj := &adt.Vertex{}
obj.AddConjunct(adt.MakeConjunct(nil, e))
obj.AddConjunct(adt.MakeRootConjunct(nil, e))
return obj
}

Expand Down Expand Up @@ -400,7 +400,7 @@ func convertRec(ctx *adt.OpContext, nilIsTop bool, x interface{}) adt.Value {
case reflect.Struct:
obj := &adt.StructLit{Src: src}
v := &adt.Vertex{}
v.AddConjunct(adt.MakeConjunct(nil, obj))
v.AddConjunct(adt.MakeRootConjunct(nil, obj))
v.SetValue(ctx, adt.Finalized, &adt.StructMarker{})

t := value.Type()
Expand Down Expand Up @@ -441,7 +441,7 @@ func convertRec(ctx *adt.OpContext, nilIsTop bool, x interface{}) adt.Value {
arc.Label = f
} else {
arc = &adt.Vertex{Label: f, Value: sub}
arc.AddConjunct(adt.MakeConjunct(nil, sub))
arc.AddConjunct(adt.MakeRootConjunct(nil, sub))
}
v.Arcs = append(v.Arcs, arc)
}
Expand All @@ -451,7 +451,7 @@ func convertRec(ctx *adt.OpContext, nilIsTop bool, x interface{}) adt.Value {
case reflect.Map:
obj := &adt.StructLit{Src: src}
v := &adt.Vertex{Value: &adt.StructMarker{}}
v.AddConjunct(adt.MakeConjunct(nil, obj))
v.AddConjunct(adt.MakeRootConjunct(nil, obj))
v.SetValue(ctx, adt.Finalized, &adt.StructMarker{})

t := value.Type()
Expand Down Expand Up @@ -492,7 +492,7 @@ func convertRec(ctx *adt.OpContext, nilIsTop bool, x interface{}) adt.Value {
arc.Label = f
} else {
arc = &adt.Vertex{Label: f, Value: sub}
arc.AddConjunct(adt.MakeConjunct(nil, sub))
arc.AddConjunct(adt.MakeRootConjunct(nil, sub))
}
v.Arcs = append(v.Arcs, arc)
}
Expand Down

0 comments on commit 3d863fc

Please sign in to comment.