Skip to content

Commit

Permalink
internal/core: add support for toposort to evalv2
Browse files Browse the repository at this point in the history
Very similar to evalv3, we need to ensure the pointer to the parent
declaration is populated in StructInfo. The same approach is taken, this
time modifying closeInfo rather than closeContext, but there seem to be
extra intermediate closeInfos sometimes added, so I need to inspect a
closeInfo's ancestors to find the first non-nil declaration.

Signed-off-by: Matthew Sackman <[email protected]>
Change-Id: I69cac611cd36c2376eaad785e1cc8bd24665c4c4
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202687
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Marcel van Lohuizen <[email protected]>
  • Loading branch information
cuematthew committed Nov 5, 2024
1 parent 65a15e2 commit c374e6c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
19 changes: 19 additions & 0 deletions internal/core/adt/closed.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func (c CloseInfo) SpawnEmbed(x Node) CloseInfo {
mode: closeEmbed,
root: EmbeddingSpan,
span: c.span() | EmbeddingSpan,
decl: c.closeInfo.Decl(),
}
return c
}
Expand All @@ -193,6 +194,7 @@ func (c CloseInfo) SpawnGroup(x Expr) CloseInfo {
parent: c.closeInfo,
location: x,
span: c.span(),
decl: c.closeInfo.Decl(),
}
return c
}
Expand All @@ -206,6 +208,7 @@ func (c CloseInfo) SpawnSpan(x Node, t SpanType) CloseInfo {
location: x,
root: t,
span: c.span() | t,
decl: c.closeInfo.Decl(),
}
return c
}
Expand All @@ -228,6 +231,7 @@ func (c CloseInfo) SpawnRef(arc *Vertex, isDef bool, x Expr) CloseInfo {
parent: c.closeInfo,
location: x,
span: span,
decl: c.closeInfo.Decl(),
}
}
if isDef {
Expand Down Expand Up @@ -294,6 +298,21 @@ type closeInfo struct {

root SpanType
span SpanType

// decl is the parent declaration which contains the conjuct which
// gave rise to this closeInfo.
decl Decl
}

// Returns the first non-nil Decl from c, or c's parents, if possible.
func (c *closeInfo) Decl() Decl {
for c != nil && c.decl == nil {
c = c.parent
}
if c == nil {
return nil
}
return c.decl
}

// closeStats holds the administrative fields for a closeInfo value. Each
Expand Down
2 changes: 2 additions & 0 deletions internal/core/adt/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,8 @@ func (v *Vertex) AddStruct(s *StructLit, env *Environment, ci CloseInfo) *Struct
}
if cc := ci.cc; cc != nil && cc.decl != nil {
info.Decl = cc.decl
} else if decl := ci.closeInfo.Decl(); decl != nil {
info.Decl = decl
}
for _, t := range v.Structs {
if *t == info { // TODO: check for different identity.
Expand Down
1 change: 1 addition & 0 deletions internal/core/adt/comprehension.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func (n *nodeContext) insertComprehension(
if !n.ctx.isDevVersion() {
ci = ci.SpawnEmbed(c)
ci.closeInfo.span |= ComprehensionSpan
ci.decl = c
}

var decls []Decl
Expand Down
1 change: 1 addition & 0 deletions internal/core/adt/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -2153,6 +2153,7 @@ func (n *nodeContext) addStruct(
// TODO(perf): only do this if addExprConjunct below will result in
// a fieldSet. Otherwise the entry will just be removed next.
id := closeInfo.SpawnEmbed(x)
id.decl = x

c := MakeConjunct(childEnv, x, id)
n.addExprConjunct(c, partial)
Expand Down

0 comments on commit c374e6c

Please sign in to comment.