1
0
mirror of https://github.com/golang/go synced 2024-11-19 03:44:40 -07:00

cmd/compile: add Type.ChanDir

Generated with eg.

Passes toolstash -cmp.

Change-Id: I3af35191e73a558080f777a4eed93bcec7dfe1f5
Reviewed-on: https://go-review.googlesource.com/21469
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Josh Bleecher Snyder 2016-04-02 16:26:30 -07:00
parent 4ae4e81c2c
commit 29267c2131
7 changed files with 20 additions and 13 deletions

View File

@ -547,7 +547,7 @@ func (p *exporter) typ(t *Type) {
case TCHAN:
p.tag(chanTag)
p.int(int(t.Chan))
p.int(int(t.ChanDir()))
p.typ(t.Elem())
default:

View File

@ -596,7 +596,7 @@ func typefmt(t *Type, flag FmtFlag) string {
return "[]" + t.Elem().String()
case TCHAN:
switch t.Chan {
switch t.ChanDir() {
case Crecv:
return "<-chan " + t.Elem().String()
@ -604,7 +604,7 @@ func typefmt(t *Type, flag FmtFlag) string {
return "chan<- " + t.Elem().String()
}
if t.Elem() != nil && t.Elem().IsChan() && t.Elem().Sym == nil && t.Elem().Chan == Crecv {
if t.Elem() != nil && t.Elem().IsChan() && t.Elem().Sym == nil && t.Elem().ChanDir() == Crecv {
return "chan (" + t.Elem().String() + ")"
}
return "chan " + t.Elem().String()
@ -1102,7 +1102,7 @@ func exprfmt(n *Node, prec int) string {
if n.Type != nil && n.Type.Etype != TIDEAL && n.Type.Etype != TNIL && n.Type != idealbool && n.Type != idealstring {
// Need parens when type begins with what might
// be misinterpreted as a unary operator: * or <-.
if n.Type.IsPtr() || (n.Type.IsChan() && n.Type.Chan == Crecv) {
if n.Type.IsPtr() || (n.Type.IsChan() && n.Type.ChanDir() == Crecv) {
return fmt.Sprintf("(%v)(%v)", n.Type, Vconv(n.Val(), 0))
} else {
return fmt.Sprintf("%v(%v)", n.Type, Vconv(n.Val(), 0))

View File

@ -58,7 +58,7 @@ func typecheckrange(n *Node) {
t2 = t.Val()
case TCHAN:
if t.Chan&Crecv == 0 {
if t.ChanDir()&Crecv == 0 {
Yyerror("invalid operation: range %v (receive from send-only type %v)", n.Right, n.Right.Type)
goto out
}

View File

@ -1143,7 +1143,7 @@ ok:
ot = dcommontype(s, ot, t)
ot = dsymptr(s, ot, s1, 0)
ot = duintptr(s, ot, uint64(t.Chan))
ot = duintptr(s, ot, uint64(t.ChanDir()))
ot = dextratype(s, ot, t, 0)
case TFUNC:

View File

@ -721,7 +721,7 @@ func eqtype1(t1, t2 *Type, assumedEqual map[typePair]struct{}) bool {
}
case TCHAN:
if t1.Chan != t2.Chan {
if t1.ChanDir() != t2.ChanDir() {
return false
}
@ -844,7 +844,7 @@ func assignop(src *Type, dst *Type, why *string) Op {
// 4. src is a bidirectional channel value, dst is a channel type,
// src and dst have identical element types, and
// either src or dst is not a named type.
if src.IsChan() && src.Chan == Cboth && dst.IsChan() {
if src.IsChan() && src.ChanDir() == Cboth && dst.IsChan() {
if Eqtype(src.Elem(), dst.Elem()) && (src.Sym == nil || dst.Sym == nil) {
return OCONVNOP
}

View File

@ -817,8 +817,8 @@ func (t *Type) cmp(x *Type) ssa.Cmp {
}
case TCHAN:
if t.Chan != x.Chan {
return cmpForNe(t.Chan < x.Chan)
if t.ChanDir() != x.ChanDir() {
return cmpForNe(t.ChanDir() < x.ChanDir())
}
default:
@ -955,6 +955,13 @@ func (t *Type) SetNumElem(n int64) {
t.Bound = n
}
// ChanDir returns the direction of a channel type t.
// The direction will be one of Crecv, Csend, or Cboth.
func (t *Type) ChanDir() uint8 {
t.wantEtype(TCHAN)
return t.Chan
}
func (t *Type) IsMemory() bool { return false }
func (t *Type) IsFlags() bool { return false }
func (t *Type) IsVoid() bool { return false }

View File

@ -1048,7 +1048,7 @@ OpSwitch:
return n
}
if t.Chan&Crecv == 0 {
if t.ChanDir()&Crecv == 0 {
Yyerror("invalid operation: %v (receive from send-only type %v)", n, t)
n.Type = nil
return n
@ -1075,7 +1075,7 @@ OpSwitch:
return n
}
if t.Chan&Csend == 0 {
if t.ChanDir()&Csend == 0 {
Yyerror("invalid operation: %v (send to receive-only type %v)", n, t)
n.Type = nil
return n
@ -1528,7 +1528,7 @@ OpSwitch:
return n
}
if t.Chan&Csend == 0 {
if t.ChanDir()&Csend == 0 {
Yyerror("invalid operation: %v (cannot close receive-only channel)", n)
n.Type = nil
return n