astutil: add Unparen utility, eliminating 7 copies.

Change-Id: I824328c275bd6198a57edd64bf72cb2cf0490a68
Reviewed-on: https://go-review.googlesource.com/2172
Reviewed-by: Chris Manghane <cmang@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
This commit is contained in:
Alan Donovan 2014-12-29 15:35:00 -05:00
parent 43a6897047
commit 4f13714aca
8 changed files with 28 additions and 76 deletions

14
astutil/util.go Normal file
View File

@ -0,0 +1,14 @@
package astutil
import "go/ast"
// Unparen returns e with any enclosing parentheses stripped.
func Unparen(e ast.Expr) ast.Expr {
for {
p, ok := e.(*ast.ParenExpr)
if !ok {
return e
}
e = p.X
}
}

View File

@ -9,6 +9,8 @@ package main
import ( import (
"go/ast" "go/ast"
"go/token" "go/token"
"golang.org/x/tools/astutil"
) )
func init() { func init() {
@ -162,7 +164,7 @@ func hasSideEffects(e ast.Expr) bool {
// split returns []{d, c, b, a}. // split returns []{d, c, b, a}.
func (op boolOp) split(e ast.Expr) (exprs []ast.Expr) { func (op boolOp) split(e ast.Expr) (exprs []ast.Expr) {
for { for {
e = unparen(e) e = astutil.Unparen(e)
if b, ok := e.(*ast.BinaryExpr); ok && b.Op == op.tok { if b, ok := e.(*ast.BinaryExpr); ok && b.Op == op.tok {
exprs = append(exprs, op.split(b.Y)...) exprs = append(exprs, op.split(b.Y)...)
e = b.X e = b.X
@ -173,13 +175,3 @@ func (op boolOp) split(e ast.Expr) (exprs []ast.Expr) {
} }
return return
} }
func unparen(e ast.Expr) ast.Expr {
for {
p, ok := e.(*ast.ParenExpr)
if !ok {
return e
}
e = p.X
}
}

View File

@ -13,6 +13,7 @@ import (
"io" "io"
"os" "os"
"golang.org/x/tools/astutil"
"golang.org/x/tools/go/types" "golang.org/x/tools/go/types"
) )
@ -22,17 +23,7 @@ func unreachable() {
//// AST utilities //// AST utilities
// unparen returns e with any enclosing parentheses stripped. func unparen(e ast.Expr) ast.Expr { return astutil.Unparen(e) }
func unparen(e ast.Expr) ast.Expr {
for {
p, ok := e.(*ast.ParenExpr)
if !ok {
break
}
e = p.X
}
return e
}
// isBlankIdent returns true iff e is an Ident with name "_". // isBlankIdent returns true iff e is an Ident with name "_".
// They have no associated types.Object, and thus no type. // They have no associated types.Object, and thus no type.

View File

@ -10,6 +10,7 @@ import (
"go/ast" "go/ast"
"go/token" "go/token"
"golang.org/x/tools/astutil"
"golang.org/x/tools/go/exact" "golang.org/x/tools/go/exact"
) )
@ -608,15 +609,7 @@ func implicitArrayDeref(typ Type) Type {
return typ return typ
} }
// unparen removes any parentheses surrounding an expression and returns func unparen(x ast.Expr) ast.Expr { return astutil.Unparen(x) }
// the naked expression.
//
func unparen(x ast.Expr) ast.Expr {
if p, ok := x.(*ast.ParenExpr); ok {
return unparen(p.X)
}
return x
}
func (check *Checker) complexArg(x *operand) bool { func (check *Checker) complexArg(x *operand) bool {
t, _ := x.typ.Underlying().(*Basic) t, _ := x.typ.Underlying().(*Basic)

View File

@ -482,17 +482,7 @@ func ptrAnalysis(o *Oracle) *pointer.Result {
return result return result
} }
// unparen returns e with any enclosing parentheses stripped. func unparen(e ast.Expr) ast.Expr { return astutil.Unparen(e) }
func unparen(e ast.Expr) ast.Expr {
for {
p, ok := e.(*ast.ParenExpr)
if !ok {
break
}
e = p.X
}
return e
}
// deref returns a pointer's element type; otherwise it returns typ. // deref returns a pointer's element type; otherwise it returns typ.
func deref(typ types.Type) types.Type { func deref(typ types.Type) types.Type {

View File

@ -8,6 +8,7 @@ import (
"os" "os"
"reflect" "reflect"
"golang.org/x/tools/astutil"
"golang.org/x/tools/go/exact" "golang.org/x/tools/go/exact"
"golang.org/x/tools/go/loader" "golang.org/x/tools/go/loader"
"golang.org/x/tools/go/types" "golang.org/x/tools/go/types"
@ -216,18 +217,7 @@ func (tr *Transformer) matchWildcard(xobj *types.Var, y ast.Expr) bool {
// -- utilities -------------------------------------------------------- // -- utilities --------------------------------------------------------
// unparen returns e with any enclosing parentheses stripped. func unparen(e ast.Expr) ast.Expr { return astutil.Unparen(e) }
// TODO(adonovan): move to astutil package.
func unparen(e ast.Expr) ast.Expr {
for {
p, ok := e.(*ast.ParenExpr)
if !ok {
break
}
e = p.X
}
return e
}
// isRef returns the object referred to by this (possibly qualified) // isRef returns the object referred to by this (possibly qualified)
// identifier, or nil if the node is not a referring identifier. // identifier, or nil if the node is not a referring identifier.

View File

@ -13,6 +13,7 @@ import (
"strings" "strings"
"unicode" "unicode"
"golang.org/x/tools/astutil"
"golang.org/x/tools/go/types" "golang.org/x/tools/go/types"
) )
@ -100,14 +101,4 @@ func sameFile(x, y string) bool {
return false return false
} }
// unparen returns e with any enclosing parentheses stripped. func unparen(e ast.Expr) ast.Expr { return astutil.Unparen(e) }
func unparen(e ast.Expr) ast.Expr {
for {
p, ok := e.(*ast.ParenExpr)
if !ok {
break
}
e = p.X
}
return e
}

View File

@ -49,6 +49,7 @@ import (
"go/ast" "go/ast"
"go/token" "go/token"
"golang.org/x/tools/astutil"
"golang.org/x/tools/go/types" "golang.org/x/tools/go/types"
) )
@ -698,17 +699,7 @@ func deref(typ types.Type) types.Type {
return typ return typ
} }
// unparen returns e with any enclosing parentheses stripped. func unparen(e ast.Expr) ast.Expr { return astutil.Unparen(e) }
func unparen(e ast.Expr) ast.Expr {
for {
p, ok := e.(*ast.ParenExpr)
if !ok {
break
}
e = p.X
}
return e
}
func isInterface(T types.Type) bool { func isInterface(T types.Type) bool {
_, ok := T.Underlying().(*types.Interface) _, ok := T.Underlying().(*types.Interface)