Tighten an ast.TypeExpr's types.

This commit is contained in:
Nigel Tao committed 2017-05-11 16:05:34 +10:00
1 parent 9f226d00e0
commit 4a4c6375c0
3 files changed
+23 -25

No files matched your search

+11 -13
View File
@@ -336,35 +336,33 @@ func NewContinue() *Continue {
// - ID1: <0|type name>
// - LHS: <nil|Expr>
// - MHS: <nil|Expr>
// - RHS: <nil|Expr|TypeExpr>
// - RHS: <nil|TypeExpr>
//
// An IDPtr ID0 means "ptr RHS". RHS is a TypeExpr.
// An IDPtr ID0 means "ptr RHS". RHS is the inner type.
//
// An IDOpenBracket ID0 means "[LHS] RHS". RHS is a TypeExpr.
// An IDOpenBracket ID0 means "[LHS] RHS". RHS is the inner type.
//
// 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 "pkg.foo[MHS:RHS]". MHS and RHS are Expr's, possibly nil. For
// refined as "pkg.foo[LHS:MHS]". LHS and MHS are Expr's, possibly nil. For
// example, the MHS for "u32[:4096]" is nil.
type TypeExpr Node
func (n *TypeExpr) Node() *Node { return (*Node)(n) }
func (n *TypeExpr) PackageOrDecorator() t.ID { return n.id0 }
func (n *TypeExpr) Name() t.ID { return n.id1 }
func (n *TypeExpr) LHS() *Node { return n.lhs }
func (n *TypeExpr) MHS() *Node { return n.mhs }
func (n *TypeExpr) RHS() *Node { return n.rhs }
func (n *TypeExpr) LHS() *Expr { return n.lhs.Expr() }
func (n *TypeExpr) MHS() *Expr { return n.mhs.Expr() }
func (n *TypeExpr) Inner() *TypeExpr { return n.rhs.TypeExpr() }
// TODO: tighten the types here.
func NewTypeExpr(pkgOrDec t.ID, name t.ID, lhs *Node, mhs *Node, rhs *Node) *TypeExpr {
func NewTypeExpr(pkgOrDec t.ID, name t.ID, lhs *Expr, mhs *Expr, inner *TypeExpr) *TypeExpr {
return &TypeExpr{
kind: KTypeExpr,
id0: pkgOrDec,
id1: name,
lhs: lhs,
mhs: mhs,
rhs: rhs,
lhs: lhs.Node(),
mhs: mhs.Node(),
rhs: inner.Node(),
}
}
+2 -2
View File
@@ -23,7 +23,7 @@ func TypeString(m *t.IDMap, n *a.TypeExpr) string {
case 0:
return m.ByID(n.Name())
case t.IDPtr:
return "ptr " + TypeString(m, n.RHS().TypeExpr())
return "ptr " + TypeString(m, n.Inner())
case t.IDOpenBracket:
// TODO.
default:
@@ -347,7 +347,7 @@ func (c *Checker) checkTypeExpr(n *a.TypeExpr) error {
return fmt.Errorf("check: %q is not a type", c.idMap.ByID(name))
case t.IDPtr:
n = n.RHS().TypeExpr()
n = n.Inner()
case t.IDOpenBracket:
// TODO.
+10 -10
View File
@@ -229,7 +229,7 @@ func (p *parser) parseTypeExpr() (*a.TypeExpr, error) {
if err != nil {
return nil, err
}
return a.NewTypeExpr(t.IDPtr, 0, nil, nil, rhs.Node()), nil
return a.NewTypeExpr(t.IDPtr, 0, nil, nil, rhs), nil
}
if x := p.peekID(); x == t.IDOpenBracket {
@@ -247,7 +247,7 @@ func (p *parser) parseTypeExpr() (*a.TypeExpr, error) {
if err != nil {
return nil, err
}
return a.NewTypeExpr(t.IDOpenBracket, 0, lhs.Node(), nil, rhs.Node()), nil
return a.NewTypeExpr(t.IDOpenBracket, 0, lhs, nil, rhs), nil
}
pkg, name, err := p.parseQualifiedIdent()
@@ -255,21 +255,21 @@ func (p *parser) parseTypeExpr() (*a.TypeExpr, error) {
return nil, err
}
mhs, rhs := (*a.Expr)(nil), (*a.Expr)(nil)
lhs, mhs := (*a.Expr)(nil), (*a.Expr)(nil)
if x := p.peekID(); x == t.IDOpenBracket {
_, mhs, rhs, err = p.parseRangeOrIndex(false)
_, lhs, mhs, err = p.parseRangeOrIndex(false)
if err != nil {
return nil, err
}
}
return a.NewTypeExpr(pkg, name, nil, mhs.Node(), rhs.Node()), nil
return a.NewTypeExpr(pkg, name, lhs, mhs, nil), nil
}
// parseRangeOrIndex parses "[i:j]", "[i:]", "[:j]" and "[:]". If allowIndex,
// it also parses "[x]". The returned op is t.IDColon for a range and
// t.IDOpenBracket for an index.
func (p *parser) parseRangeOrIndex(allowIndex bool) (op t.ID, mhs *a.Expr, rhs *a.Expr, err error) {
func (p *parser) parseRangeOrIndex(allowIndex bool) (op t.ID, ei *a.Expr, ej *a.Expr, err error) {
if x := p.peekID(); x != t.IDOpenBracket {
got := p.m.ByID(x)
return 0, nil, nil, fmt.Errorf("parse: expected \"[\", got %q at %s:%d", got, p.filename, p.line())
@@ -277,7 +277,7 @@ func (p *parser) parseRangeOrIndex(allowIndex bool) (op t.ID, mhs *a.Expr, rhs *
p.src = p.src[1:]
if p.peekID() != t.IDColon {
mhs, err = p.parseExpr()
ei, err = p.parseExpr()
if err != nil {
return 0, nil, nil, err
}
@@ -289,7 +289,7 @@ func (p *parser) parseRangeOrIndex(allowIndex bool) (op t.ID, mhs *a.Expr, rhs *
case x == t.IDCloseBracket && allowIndex:
p.src = p.src[1:]
return t.IDOpenBracket, nil, mhs, nil
return t.IDOpenBracket, nil, ei, nil
default:
expected := `":"`
@@ -301,7 +301,7 @@ func (p *parser) parseRangeOrIndex(allowIndex bool) (op t.ID, mhs *a.Expr, rhs *
}
if p.peekID() != t.IDCloseBracket {
rhs, err = p.parseExpr()
ej, err = p.parseExpr()
if err != nil {
return 0, nil, nil, err
}
@@ -313,7 +313,7 @@ func (p *parser) parseRangeOrIndex(allowIndex bool) (op t.ID, mhs *a.Expr, rhs *
}
p.src = p.src[1:]
return t.IDColon, mhs, rhs, nil
return t.IDColon, ei, ej, nil
}
func (p *parser) parseBlock() ([]*a.Node, error) {