Skip to content

Commit

Permalink
cue/ast/astutil: use generics to remove some reflect
Browse files Browse the repository at this point in the history
We don't need reflect to read and write nodePtr;
we can make it a pointer to a generic ast.Node instead.
While here, applyExprList didn't use its ptr argument.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: Ia28dfdcf6e371f9d1a2de9c0ce2302b83df4eb80
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1168830
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Roger Peppe <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
  • Loading branch information
mvdan committed Sep 14, 2023
1 parent 7389fe9 commit b8cb9a4
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions cue/ast/astutil/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ type applyVisitor interface {

// Helper functions for common node lists. They may be empty.

func applyExprList(v applyVisitor, parent Cursor, ptr interface{}, list []ast.Expr) {
func applyExprList(v applyVisitor, parent Cursor, list []ast.Expr) {
c := newCursor(parent, nil, nil)
for i, x := range list {
c.index = i
Expand Down Expand Up @@ -286,14 +286,12 @@ func applyDeclList(v applyVisitor, parent Cursor, list []ast.Decl) []ast.Decl {
return c.decls
}

func apply(v applyVisitor, parent Cursor, nodePtr interface{}) {
res := reflect.Indirect(reflect.ValueOf(nodePtr))
n := res.Interface()
node := n.(ast.Node)
func apply[N ast.Node](v applyVisitor, parent Cursor, nodePtr *N) {
node := *nodePtr
c := newCursor(parent, node, nodePtr)
applyCursor(v, c)
if node != c.node {
res.Set(reflect.ValueOf(c.node))
if ast.Node(node) != c.node {
*nodePtr = c.node.(N)
}
}

Expand Down Expand Up @@ -349,10 +347,10 @@ func applyCursor(v applyVisitor, c Cursor) {
// nothing to do

case *ast.Interpolation:
applyExprList(v, c, &n, n.Elts)
applyExprList(v, c, n.Elts)

case *ast.ListLit:
applyExprList(v, c, &n, n.Elts)
applyExprList(v, c, n.Elts)

case *ast.Ellipsis:
if n.Type != nil {
Expand Down Expand Up @@ -381,7 +379,7 @@ func applyCursor(v applyVisitor, c Cursor) {

case *ast.CallExpr:
apply(v, c, &n.Fun)
applyExprList(v, c, &n, n.Args)
applyExprList(v, c, n.Args)

case *ast.UnaryExpr:
apply(v, c, &n.X)
Expand Down

0 comments on commit b8cb9a4

Please sign in to comment.