poprewrite.go raw

   1  package loader
   2  
   3  import (
   4  	"go/ast"
   5  	"go/token"
   6  )
   7  
   8  // rewritePopStatements turns a statement-position pop into an explicit
   9  // resize assignment:
  10  //
  11  //	pop(s)        ->  s = resize(s, len(s)-1)
  12  //	pop(s, xs...) ->  s = resize(s, len(s)-len(xs))
  13  //
  14  // pop returns an element, so only the statement form means "shorten the
  15  // slice"; the second form is the symmetric counterpart of push(s, xs...) and
  16  // removes as many elements as xs holds. The rewrite runs before the first
  17  // type-check, so the checker and the SSA builder only ever see the resize
  18  // builtin, which already has the statement store-back. Stage4's front end
  19  // implements the same shrink directly (emitPopShrink).
  20  func rewritePopStatements(files []*ast.File) (n int, remaining []token.Pos) {
  21  	for _, f := range files {
  22  		ast.Inspect(f, func(node ast.Node) bool {
  23  			// Any pop call still in the tree after the statement rewrite is an
  24  			// expression pop, which no longer exists: pop is a statement.
  25  			if call, isCall := node.(*ast.CallExpr); isCall {
  26  				if fun, isIdent := call.Fun.(*ast.Ident); isIdent && fun.Name == "pop" {
  27  					remaining = append(remaining, call.Pos())
  28  				}
  29  			}
  30  			blk, ok := node.(*ast.BlockStmt)
  31  			if !ok {
  32  				return true
  33  			}
  34  			for i, st := range blk.List {
  35  				es, ok := st.(*ast.ExprStmt)
  36  				if !ok {
  37  					continue
  38  				}
  39  				call, ok := es.X.(*ast.CallExpr)
  40  				if !ok || len(call.Args) == 0 || len(call.Args) > 2 {
  41  					continue
  42  				}
  43  				fun, ok := call.Fun.(*ast.Ident)
  44  				if !ok || fun.Name != "pop" {
  45  					continue
  46  				}
  47  				if len(call.Args) == 2 && !call.Ellipsis.IsValid() {
  48  					continue
  49  				}
  50  				target := call.Args[0]
  51  				if !assignableExpr(target) {
  52  					continue
  53  				}
  54  				var count ast.Expr
  55  				lenPos := call.Lparen
  56  				if len(call.Args) == 2 {
  57  					count = &ast.CallExpr{Fun: ast.NewIdent("len"), Lparen: lenPos, Args: []ast.Expr{call.Args[1]}}
  58  				} else {
  59  					one := &ast.BasicLit{Kind: token.INT, Value: "1", ValuePos: lenPos}
  60  					count = one
  61  				}
  62  				lenSelf := &ast.CallExpr{Fun: ast.NewIdent("len"), Lparen: lenPos, Args: []ast.Expr{target}}
  63  				sub := &ast.BinaryExpr{X: lenSelf, Op: token.SUB, Y: count}
  64  				resize := &ast.CallExpr{Fun: ast.NewIdent("resize"), Lparen: lenPos, Args: []ast.Expr{target, sub}}
  65  				blk.List[i] = &ast.AssignStmt{Lhs: []ast.Expr{target}, Tok: token.ASSIGN, TokPos: call.Pos(), Rhs: []ast.Expr{resize}}
  66  				n++
  67  			}
  68  			return true
  69  		})
  70  	}
  71  	return n, remaining
  72  }
  73  
  74  // assignableExpr reports whether e can appear on the left of an assignment,
  75  // which is exactly what pop's slice argument must be for the statement form to
  76  // have anywhere to store the shortened header.
  77  func assignableExpr(e ast.Expr) bool {
  78  	switch x := e.(type) {
  79  	case *ast.Ident:
  80  		return x.Name != "_"
  81  	case *ast.SelectorExpr:
  82  		return true
  83  	case *ast.IndexExpr:
  84  		return true
  85  	case *ast.StarExpr:
  86  		return true
  87  	}
  88  	return false
  89  }
  90