mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 08:32:11 +01:00
Let ast.TypeExpr represent method types
This commit is contained in:
1 parent
2e12a41414
commit
bf00d114e4
4 files changed
+77
-24
No files matched your search
+25
-12
@@ -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(),
|
||||
}
|
||||
|
||||
@@ -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, '.')
|
||||
|
||||
+45
-10
@@ -416,12 +416,6 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
isInDst(q.tm, n, t.KeyCopyFromReader32, 2) || isInDst(q.tm, n, t.KeyCopyFromHistory32, 2) ||
|
||||
isInDst(q.tm, n, t.KeyWriteU8, 1) || isInDst(q.tm, n, t.KeySinceMark, 0) ||
|
||||
isInDst(q.tm, n, t.KeyMark, 0) || isInDst(q.tm, n, t.KeyIsMarked, 0) ||
|
||||
isThisMethod(q.tm, n, "decode_header", 1) || isThisMethod(q.tm, n, "decode_lsd", 1) ||
|
||||
isThisMethod(q.tm, n, "decode_extension", 1) || isThisMethod(q.tm, n, "decode_id", 2) ||
|
||||
isThisMethod(q.tm, n, "decode_uncompressed", 2) || isThisMethod(q.tm, n, "decode_blocks", 2) ||
|
||||
isThisMethod(q.tm, n, "decode_huffman_slow", 2) || isThisMethod(q.tm, n, "decode_huffman_fast", 2) ||
|
||||
isThisMethod(q.tm, n, "init_fixed_huffman", 0) || isThisMethod(q.tm, n, "init_dynamic_huffman", 1) ||
|
||||
isThisMethod(q.tm, n, "init_huff", 4) ||
|
||||
isThatMethod(q.tm, n, t.KeyMark, 0) || isThatMethod(q.tm, n, t.KeyLimit, 1) ||
|
||||
isThatMethod(q.tm, n, t.KeySinceMark, 0) {
|
||||
|
||||
@@ -561,6 +555,8 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
return q.tcheckExprCall(n, depth)
|
||||
|
||||
case t.KeyOpenBracket:
|
||||
// n is an index.
|
||||
lhs := n.LHS().Expr()
|
||||
@@ -657,10 +653,51 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
n.SetMType(typeExprList)
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("check: unrecognized token.Key (0x%X) in expression %q for tcheckExprOther",
|
||||
n.ID0().Key(), n.Str(q.tm))
|
||||
}
|
||||
|
||||
func (q *checker) tcheckExprCall(n *a.Expr, depth uint32) error {
|
||||
if err := q.tcheckExpr(n.LHS().Expr(), depth); err != nil {
|
||||
return err
|
||||
}
|
||||
lTyp := n.LHS().Expr().MType()
|
||||
// TODO: look up funcs in used packages. Factor out a resolveFunc method?
|
||||
f := q.c.funcs[t.QID{lTyp.Receiver().Name(), lTyp.Name()}]
|
||||
if f.Func == nil {
|
||||
return fmt.Errorf("check: cannot look up %q", lTyp.Str(q.tm))
|
||||
}
|
||||
|
||||
// Check that the func's in type matches the arguments.
|
||||
inFields := f.Func.In().Fields()
|
||||
if len(inFields) != len(n.Args()) {
|
||||
return fmt.Errorf("check: %q has %d arguments but %d were given",
|
||||
lTyp.Str(q.tm), len(inFields), len(n.Args()))
|
||||
}
|
||||
for _, o := range n.Args() {
|
||||
// TODO: tcheckArg should take inFields[i], checking both assignability
|
||||
// of types and that the "name" in the arg "name:val" matches the field
|
||||
// name.
|
||||
if err := q.tcheckArg(o.Arg(), depth); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: distinguish t.KeyOpenParen vs t.KeyTry in a more principled way?
|
||||
//
|
||||
// TODO: figure out calls with and without "?" should interact with the out
|
||||
// type.
|
||||
if n.ID0().Key() == t.KeyTry {
|
||||
n.SetMType(typeExprStatus)
|
||||
} else {
|
||||
// TODO: figure out how to translate f.Func.Out(), which is an
|
||||
// *ast.Struct, to an *ast.TypeExpr we can pass to n.SetMType.
|
||||
n.SetMType(typeExprPlaceholder) // HACK.
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func isInSrc(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
|
||||
callSuspendible := methodName != t.KeySinceMark &&
|
||||
methodName != t.KeyMark &&
|
||||
@@ -738,9 +775,7 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
|
||||
if err := q.tcheckExpr(lhs, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
lTyp := lhs.MType()
|
||||
for ; lTyp.Decorator().Key() == t.KeyPtr; lTyp = lTyp.Inner() {
|
||||
}
|
||||
lTyp := lhs.MType().Pointee()
|
||||
|
||||
if lTyp.Decorator() != 0 {
|
||||
// TODO.
|
||||
@@ -780,7 +815,7 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
|
||||
}
|
||||
|
||||
if f := q.c.funcs[t.QID{lTyp.Name(), n.ID1()}]; f.Func != nil {
|
||||
n.SetMType(typeExprPlaceholder) // HACK.
|
||||
n.SetMType(a.NewTypeExpr(t.IDOpenParen, n.ID1(), lTyp.Node(), nil, nil))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -375,7 +375,7 @@ func (p *parser) parseTypeExpr() (*a.TypeExpr, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return a.NewTypeExpr(decorator, 0, lhs, nil, rhs), nil
|
||||
return a.NewTypeExpr(decorator, 0, lhs.Node(), nil, rhs), nil
|
||||
}
|
||||
|
||||
pkg, name, err := p.parseQualifiedIdent()
|
||||
@@ -391,7 +391,7 @@ func (p *parser) parseTypeExpr() (*a.TypeExpr, error) {
|
||||
}
|
||||
}
|
||||
|
||||
return a.NewTypeExpr(pkg, name, lhs, mhs, nil), nil
|
||||
return a.NewTypeExpr(pkg, name, lhs.Node(), mhs, nil), nil
|
||||
}
|
||||
|
||||
// parseBracket parses "[i:j]", "[i:]", "[:j]" and "[:]". A double dot replaces
|
||||
|
||||
Reference in new issue
Block a user