Resolve used packages during type checking

This commit is contained in:
Nigel Tao committed 2017-12-28 11:24:59 +11:00
1 parent 366a9388b3
commit 1255b162ea
5 files changed
+138 -35

No files matched your search

+81 -11
View File
@@ -20,8 +20,10 @@ import (
"errors"
"fmt"
"math/big"
"path"
"github.com/google/wuffs/lang/base38"
"github.com/google/wuffs/lang/parse"
a "github.com/google/wuffs/lang/ast"
t "github.com/google/wuffs/lang/token"
@@ -79,7 +81,14 @@ type Struct struct {
Struct *a.Struct
}
func Check(tm *t.Map, files ...*a.File) (*Checker, error) {
type usee struct {
consts map[t.ID]*a.Const
funcs map[t.QID]*a.Func
statuses map[t.ID]*a.Status
structs map[t.ID]*a.Struct
}
func Check(tm *t.Map, files []*a.File, resolveUse func(usePath string) ([]byte, error)) (*Checker, error) {
for _, f := range files {
if f == nil {
return nil, errors.New("check: Check given a nil *ast.File")
@@ -103,13 +112,15 @@ func Check(tm *t.Map, files ...*a.File) (*Checker, error) {
}
}
c := &Checker{
tm: tm,
reasonMap: rMap,
packageID: base38.Max + 1,
consts: map[t.ID]Const{},
funcs: map[t.QID]Func{},
statuses: map[t.ID]Status{},
structs: map[t.ID]Struct{},
tm: tm,
resolveUse: resolveUse,
reasonMap: rMap,
packageID: base38.Max + 1,
consts: map[t.ID]Const{},
funcs: map[t.QID]Func{},
statuses: map[t.ID]Status{},
structs: map[t.ID]Struct{},
usees: map[t.ID]*usee{},
}
for _, phase := range phases {
@@ -158,8 +169,9 @@ type reason func(q *checker, n *a.Assert) error
type reasonMap map[t.Key]reason
type Checker struct {
tm *t.Map
reasonMap reasonMap
tm *t.Map
resolveUse func(usePath string) ([]byte, error)
reasonMap reasonMap
packageID uint32
otherPackageID *a.PackageID
@@ -169,6 +181,10 @@ type Checker struct {
statuses map[t.ID]Status
structs map[t.ID]Struct
// usees are the packages referred to in the `use "foo/bar"` lines. The
// keys are the use path base: `bar`, not `"foo/bar"`.
usees map[t.ID]*usee
builtInFuncs map[t.QID]*a.Func
builtInSliceFuncs map[t.QID]*a.Func
unsortedStructs []*a.Struct
@@ -221,7 +237,61 @@ func (c *Checker) checkPackageIDExists(node *a.Node) error {
}
func (c *Checker) checkUse(node *a.Node) error {
// TODO.
usePath := node.Use().Path()
filename, ok := t.Unescape(usePath.Str(c.tm))
if !ok {
return fmt.Errorf("check: cannot resolve `use %s`", usePath.Str(c.tm))
}
base, err := c.tm.Insert(path.Base(filename))
if err != nil {
return fmt.Errorf("check: cannot resolve `use %s`: %v", usePath.Str(c.tm), err)
}
filename += ".wuffs"
if _, ok := c.usees[base]; ok {
return fmt.Errorf("check: duplicate use-name %q", base.Str(c.tm))
}
if c.resolveUse == nil {
return fmt.Errorf("check: cannot resolve a use declaration")
}
src, err := c.resolveUse(filename)
if err != nil {
return err
}
tokens, _, err := t.Tokenize(c.tm, filename, src)
if err != nil {
return err
}
f, err := parse.Parse(c.tm, filename, tokens, &parse.Options{
AllowDoubleUnderscoreNames: true,
})
if err != nil {
return err
}
u := &usee{
consts: map[t.ID]*a.Const{},
funcs: map[t.QID]*a.Func{},
statuses: map[t.ID]*a.Status{},
structs: map[t.ID]*a.Struct{},
}
for _, n := range f.TopLevelDecls() {
switch n.Kind() {
case a.KConst:
n := n.Const()
u.consts[n.Name()] = n
case a.KFunc:
n := n.Func()
u.funcs[n.QID()] = n
case a.KStatus:
n := n.Status()
u.statuses[n.Message()] = n
case a.KStruct:
n := n.Struct()
u.structs[n.Name()] = n
}
}
c.usees[base] = u
return nil
}
+2 -2
View File
@@ -111,7 +111,7 @@ func TestCheck(t *testing.T) {
t.Fatalf("compareToWuffsfmt: %v", err)
}
c, err := Check(tm, file)
c, err := Check(tm, []*ast.File{file}, nil)
if err != nil {
t.Fatalf("Check: %v", err)
}
@@ -208,7 +208,7 @@ func TestConstValues(t *testing.T) {
continue
}
c, err := Check(tm, file)
c, err := Check(tm, []*ast.File{file}, nil)
if err != nil {
t.Errorf("%q: Check: %v", s, err)
continue
+26 -11
View File
@@ -131,25 +131,40 @@ func parseBuiltInFuncs(tm *t.Map, ss []string, generic bool) (map[t.QID]*a.Func,
}
func (c *Checker) resolveFunc(typ *a.TypeExpr) (*a.Func, error) {
// TODO: look up funcs in used packages.
if typ.Decorator().Key() != t.KeyOpenParen {
return nil, fmt.Errorf("check: resolveFunc cannot look up non-func TypeExpr %q", typ.Str(c.tm))
}
qid := t.QID{typ.Receiver().Name(), typ.Name()}
if f, err := c.builtInFunc(qid); err != nil {
return nil, err
} else if f != nil {
return f, nil
}
if f := c.funcs[qid].Func; f != nil {
return f, nil
}
if typ.Receiver().IsSliceType() {
if f, err := c.builtInSliceFunc(t.QID{t.IDDiamond, qid[1]}); err != nil {
switch lTyp := typ.Receiver(); lTyp.Decorator().Key() {
case 0:
if f, err := c.builtInFunc(qid); err != nil {
return nil, err
} else if f != nil {
return f, nil
}
if f := c.funcs[qid].Func; f != nil {
return f, nil
}
case t.KeyColon:
qid[0] = t.IDDiamond
if f, err := c.builtInSliceFunc(qid); err != nil {
return nil, err
} else if f != nil {
return f, nil
}
default:
u := c.usees[lTyp.Decorator()]
if u == nil {
return nil, fmt.Errorf("check: cannot resolve %q in type %q",
lTyp.Decorator().Str(c.tm), lTyp.Str(c.tm))
}
if f := u.funcs[qid]; f != nil {
// TODO: f.Receiver().Str(c.tm) should be "deflate.decoder", not
// just "decoder".
return f, nil
}
}
return nil, fmt.Errorf("check: resolveFunc cannot look up %q", typ.Str(c.tm))
}
+20 -10
View File
@@ -430,8 +430,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
// require a TypeExpr being able to express function and method types.
// TODO: delete this hack that only matches "foo.decode(etc)".
if isThatMethod(q.tm, n, q.tm.ByName("decode").Key(), 2) ||
isThatMethod(q.tm, n, q.tm.ByName("decode").Key(), 3) {
if isThatMethod(q.tm, n, q.tm.ByName("decode").Key(), 3) {
foo := n.LHS().Expr().LHS().Expr()
if err := q.tcheckExpr(foo, depth); err != nil {
return err
@@ -695,8 +694,19 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
n.SetMType(a.NewTypeExpr(t.IDOpenParen, n.ID1(), lTyp.Node(), nil, nil))
return nil
}
// TODO.
return fmt.Errorf("check: unsupported decorator for tcheckDot")
// lTyp is from a used package: `use "foo"` followed by `foo.bar`.
u := q.c.usees[lTyp.Decorator()]
if u == nil {
return fmt.Errorf("check: cannot resolve %q in type %q for expression %q",
lTyp.Decorator().Str(q.tm), lTyp.Str(q.tm), lhs.Str(q.tm))
}
if f := u.funcs[t.QID{lTyp.Name(), n.ID1()}]; f != nil {
n.SetMType(a.NewTypeExpr(t.IDOpenParen, n.ID1(), lTyp.Node(), nil, nil))
return nil
}
return fmt.Errorf("check: no method named %q found in type %q for expression %q",
n.ID1().Str(q.tm), lTyp.Str(q.tm), n.Str(q.tm))
}
qid := t.QID{lTyp.Name(), n.ID1()}
@@ -724,7 +734,7 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
name := lTyp.Name()
s = q.c.structs[name].Struct
if s == nil && builtInTypeMap[name] == nil {
return fmt.Errorf("check: no struct type %q found for expression %q", name.Str(q.tm), lhs.Str(q.tm))
return fmt.Errorf("check: no struct type %q found for expression %q", lTyp.Str(q.tm), lhs.Str(q.tm))
}
}
@@ -739,7 +749,7 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
}
return fmt.Errorf("check: no field or method named %q found in type %q for expression %q",
n.ID1().Str(q.tm), lTyp.Name().Str(q.tm), n.Str(q.tm))
n.ID1().Str(q.tm), lTyp.Str(q.tm), n.Str(q.tm))
}
func (q *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error {
@@ -1068,11 +1078,11 @@ swtch:
}
default:
// TODO: don't hard-code deflate.decoder as an acceptable type.
if q.tm.ByID(typ.Decorator()) == "deflate" && q.tm.ByID(typ.Name()) == "decoder" {
break
if u := q.c.usees[typ.Decorator()]; u == nil {
return fmt.Errorf("check: cannot resolve %q in type %q", typ.Decorator().Str(q.tm), typ.Str(q.tm))
} else if u.structs[typ.Name()] == nil {
return fmt.Errorf("check: cannot resolve type %q", typ.Str(q.tm))
}
return fmt.Errorf("check: unrecognized node for tcheckTypeExpr")
}
typ.Node().SetTypeChecked()
return nil
+9 -1
View File
@@ -50,7 +50,7 @@ func Do(args []string, g Generator) error {
return err
}
c, err := check.Check(tm, files...)
c, err := check.Check(tm, files, resolveUse)
if err != nil {
return err
}
@@ -125,6 +125,14 @@ func ParseFiles(tm *token.Map, filenames []string, opts *parse.Options) (files [
return files, nil
}
func resolveUse(usePath string) ([]byte, error) {
wuffsRoot, err := WuffsRoot()
if err != nil {
return nil, err
}
return ioutil.ReadFile(filepath.Join(wuffsRoot, "gen", "wuffs", filepath.FromSlash(usePath)))
}
var cachedWuffsRoot struct {
mu sync.Mutex
value string