Let ast.TypeExpr represent method types

This commit is contained in:
Nigel Tao committed 2017-12-09 13:55:59 +11:00
1 parent 2e12a41414
commit bf00d114e4
4 files changed
+77 -24

No files matched your search

+25 -12
View File
@@ -561,22 +561,29 @@ const MaxTypeExprDepth = 63
//
// An IDColon ID0 means "[] RHS". RHS is the inner type.
//
// An IDOpenParen ID0 means "LHS.ID1", a function or method type. LHS is the
// receiver type, which may be nil. If non-nil, it will be a pointee type: "T"
// instead of "ptr T", "ptr ptr T", etc.
//
// TODO: method effects: "foo" vs "foo!" vs "foo?".
//
// Other ID0 values mean a (possibly package-qualified) type like "pkg.foo" or
// "foo". ID0 is the "pkg" or zero, ID1 is the "foo". Such a type can be
// refined as "foo[LHS..MHS]". LHS and MHS are Expr's, possibly nil. For
// example, the LHS for "u32[..4095]" is nil.
//
// TODO: function / method types, struct types, list types.
// TODO: struct types, list types.
type TypeExpr Node
func (n *TypeExpr) Node() *Node { return (*Node)(n) }
func (n *TypeExpr) Decorator() t.ID { return n.id0 }
func (n *TypeExpr) Name() t.ID { return n.id1 }
func (n *TypeExpr) ArrayLength() *Expr { return n.lhs.Expr() }
func (n *TypeExpr) Bounds() [2]*Expr { return [2]*Expr{n.lhs.Expr(), n.mhs.Expr()} }
func (n *TypeExpr) Min() *Expr { return n.lhs.Expr() }
func (n *TypeExpr) Max() *Expr { return n.mhs.Expr() }
func (n *TypeExpr) Inner() *TypeExpr { return n.rhs.TypeExpr() }
func (n *TypeExpr) Node() *Node { return (*Node)(n) }
func (n *TypeExpr) Decorator() t.ID { return n.id0 }
func (n *TypeExpr) Name() t.ID { return n.id1 }
func (n *TypeExpr) ArrayLength() *Expr { return n.lhs.Expr() }
func (n *TypeExpr) Receiver() *TypeExpr { return n.lhs.TypeExpr() }
func (n *TypeExpr) Bounds() [2]*Expr { return [2]*Expr{n.lhs.Expr(), n.mhs.Expr()} }
func (n *TypeExpr) Min() *Expr { return n.lhs.Expr() }
func (n *TypeExpr) Max() *Expr { return n.mhs.Expr() }
func (n *TypeExpr) Inner() *TypeExpr { return n.rhs.TypeExpr() }
func (n *TypeExpr) Innermost() *TypeExpr {
for ; n != nil && n.Inner() != nil; n = n.Inner() {
@@ -584,6 +591,12 @@ func (n *TypeExpr) Innermost() *TypeExpr {
return n
}
func (n *TypeExpr) Pointee() *TypeExpr {
for ; n != nil && n.id0.Key() == t.KeyPtr; n = n.Inner() {
}
return n
}
func (n *TypeExpr) IsBool() bool {
return n.id0 == 0 && n.id1.Key() == t.KeyBool
}
@@ -601,7 +614,7 @@ func (n *TypeExpr) IsNumTypeOrIdeal() bool {
}
func (n *TypeExpr) IsRefined() bool {
return t.Key(n.id0>>t.KeyShift) != t.KeyOpenBracket && (n.lhs != nil || n.mhs != nil)
return n.id0.Key() != t.KeyOpenBracket && (n.lhs != nil || n.mhs != nil)
}
func (n *TypeExpr) IsUnsignedInteger() bool {
@@ -634,12 +647,12 @@ func (n *TypeExpr) Unrefined() *TypeExpr {
return &o
}
func NewTypeExpr(pkgOrDec t.ID, name t.ID, arrayLengthMin *Expr, max *Expr, inner *TypeExpr) *TypeExpr {
func NewTypeExpr(pkgOrDec t.ID, name t.ID, alenRecvMin *Node, max *Expr, inner *TypeExpr) *TypeExpr {
return &TypeExpr{
kind: KTypeExpr,
id0: pkgOrDec,
id1: name,
lhs: arrayLengthMin.Node(),
lhs: alenRecvMin,
mhs: max.Node(),
rhs: inner.Node(),
}
+5
View File
@@ -202,6 +202,11 @@ func (n *TypeExpr) appendStr(buf []byte, tm *t.Map, depth uint32) []byte {
case t.KeyColon:
buf = append(buf, "[] "...)
return n.Inner().appendStr(buf, tm, depth)
case t.KeyOpenParen:
buf = append(buf, "func "...)
buf = n.Receiver().appendStr(buf, tm, depth)
buf = append(buf, '.')
return append(buf, tm.ByID(n.Name())...)
default:
buf = append(buf, tm.ByID(n.Decorator())...)
buf = append(buf, '.')