1
0
mirror of https://github.com/golang/go synced 2024-11-18 08:54:45 -07:00

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 (
"go/ast"
"go/token"
"golang.org/x/tools/astutil"
)
func init() {
@ -162,7 +164,7 @@ func hasSideEffects(e ast.Expr) bool {
// split returns []{d, c, b, a}.
func (op boolOp) split(e ast.Expr) (exprs []ast.Expr) {
for {
e = unparen(e)
e = astutil.Unparen(e)
if b, ok := e.(*ast.BinaryExpr); ok && b.Op == op.tok {
exprs = append(exprs, op.split(b.Y)...)
e = b.X
@ -173,13 +175,3 @@ func (op boolOp) split(e ast.Expr) (exprs []ast.Expr) {
}
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"
"os"
"golang.org/x/tools/astutil"
"golang.org/x/tools/go/types"
)
@ -22,17 +23,7 @@ func unreachable() {
//// AST utilities
// unparen returns e with any enclosing parentheses stripped.
func unparen(e ast.Expr) ast.Expr {
for {
p, ok := e.(*ast.ParenExpr)
if !ok {
break
}
e = p.X
}
return e
}
func unparen(e ast.Expr) ast.Expr { return astutil.Unparen(e) }
// isBlankIdent returns true iff e is an Ident with name "_".
// They have no associated types.Object, and thus no type.

View File

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

View File

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

View File

@ -8,6 +8,7 @@ import (
"os"
"reflect"
"golang.org/x/tools/astutil"
"golang.org/x/tools/go/exact"
"golang.org/x/tools/go/loader"
"golang.org/x/tools/go/types"
@ -216,18 +217,7 @@ func (tr *Transformer) matchWildcard(xobj *types.Var, y ast.Expr) bool {
// -- utilities --------------------------------------------------------
// unparen returns e with any enclosing parentheses stripped.
// 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
}
func unparen(e ast.Expr) ast.Expr { return astutil.Unparen(e) }
// isRef returns the object referred to by this (possibly qualified)
// identifier, or nil if the node is not a referring identifier.

View File

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

View File

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