Commit 2e4dc86b authored by Josh Bleecher Snyder's avatar Josh Bleecher Snyder

cmd/compile: add Node.IsMethod helper

Changes generated with eg:

func before(n *gc.Node) bool { return n.Type.Recv() != nil }
func after(n *gc.Node) bool  { return n.IsMethod() }

func before(n *gc.Node) bool { return n.Type.Recv() == nil }
func after(n *gc.Node) bool  { return !n.IsMethod() }

Change-Id: I28e544490d17bbdc06ab11ed32464af5802ab206
Reviewed-on: https://go-review.googlesource.com/28968
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 3bf14195
...@@ -116,7 +116,7 @@ func reexportdep(n *Node) { ...@@ -116,7 +116,7 @@ func reexportdep(n *Node) {
} }
// nodes for method calls. // nodes for method calls.
if n.Type == nil || n.Type.Recv() != nil { if n.Type == nil || n.IsMethod() {
break break
} }
fallthrough fallthrough
......
...@@ -32,7 +32,7 @@ import "fmt" ...@@ -32,7 +32,7 @@ import "fmt"
// Get the function's package. For ordinary functions it's on the ->sym, but for imported methods // Get the function's package. For ordinary functions it's on the ->sym, but for imported methods
// the ->sym can be re-used in the local package, so peel it off the receiver's type. // the ->sym can be re-used in the local package, so peel it off the receiver's type.
func fnpkg(fn *Node) *Pkg { func fnpkg(fn *Node) *Pkg {
if fn.Type.Recv() != nil { if fn.IsMethod() {
// method // method
rcvr := fn.Type.Recv().Type rcvr := fn.Type.Recv().Type
...@@ -615,7 +615,7 @@ func mkinlcall1(n *Node, fn *Node, isddd bool) *Node { ...@@ -615,7 +615,7 @@ func mkinlcall1(n *Node, fn *Node, isddd bool) *Node {
} }
// assign receiver. // assign receiver.
if fn.Type.Recv() != nil && n.Left.Op == ODOTMETH { if fn.IsMethod() && n.Left.Op == ODOTMETH {
// method call with a receiver. // method call with a receiver.
t := fn.Type.Recv() t := fn.Type.Recv()
...@@ -679,7 +679,7 @@ func mkinlcall1(n *Node, fn *Node, isddd bool) *Node { ...@@ -679,7 +679,7 @@ func mkinlcall1(n *Node, fn *Node, isddd bool) *Node {
li := 0 li := 0
// TODO: if len(nlist) == 1 but multiple args, check that n->list->n is a call? // TODO: if len(nlist) == 1 but multiple args, check that n->list->n is a call?
if fn.Type.Recv() != nil && n.Left.Op != ODOTMETH { if fn.IsMethod() && n.Left.Op != ODOTMETH {
// non-method call to method // non-method call to method
if n.List.Len() == 0 { if n.List.Len() == 0 {
Fatalf("non-method call to method without first arg: %+v", n) Fatalf("non-method call to method without first arg: %+v", n)
......
...@@ -144,7 +144,7 @@ func emitptrargsmap() { ...@@ -144,7 +144,7 @@ func emitptrargsmap() {
off := duint32(sym, 0, uint32(nbitmap)) off := duint32(sym, 0, uint32(nbitmap))
off = duint32(sym, off, uint32(bv.n)) off = duint32(sym, off, uint32(bv.n))
var xoffset int64 var xoffset int64
if Curfn.Type.Recv() != nil { if Curfn.IsMethod() {
xoffset = 0 xoffset = 0
onebitwalktype1(Curfn.Type.Recvs(), &xoffset, bv) onebitwalktype1(Curfn.Type.Recvs(), &xoffset, bv)
} }
......
...@@ -1038,6 +1038,12 @@ func Is64(t *Type) bool { ...@@ -1038,6 +1038,12 @@ func Is64(t *Type) bool {
return false return false
} }
// IsMethod reports whether n is a method.
// n must be a function or a method.
func (n *Node) IsMethod() bool {
return n.Type.Recv() != nil
}
// SliceBounds returns n's slice bounds: low, high, and max in expr[low:high:max]. // SliceBounds returns n's slice bounds: low, high, and max in expr[low:high:max].
// n must be a slice expression. max is nil if n is a simple slice expression. // n must be a slice expression. max is nil if n is a simple slice expression.
func (n *Node) SliceBounds() (low, high, max *Node) { func (n *Node) SliceBounds() (low, high, max *Node) {
......
...@@ -860,7 +860,7 @@ OpSwitch: ...@@ -860,7 +860,7 @@ OpSwitch:
return n return n
} }
if n.Type.Etype != TFUNC || n.Type.Recv() == nil { if n.Type.Etype != TFUNC || !n.IsMethod() {
Yyerror("type %v has no method %1v", n.Left.Type, n.Right.Sym) Yyerror("type %v has no method %1v", n.Left.Type, n.Right.Sym)
n.Type = nil n.Type = nil
return n return n
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment