mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-25 08:55:11 +01:00
Resolve used packages during type checking
This commit is contained in:
1 parent
366a9388b3
commit
1255b162ea
5 files changed
+138
-35
No files matched your search
+81
-11
@@ -20,8 +20,10 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"path"
|
||||||
|
|
||||||
"github.com/google/wuffs/lang/base38"
|
"github.com/google/wuffs/lang/base38"
|
||||||
|
"github.com/google/wuffs/lang/parse"
|
||||||
|
|
||||||
a "github.com/google/wuffs/lang/ast"
|
a "github.com/google/wuffs/lang/ast"
|
||||||
t "github.com/google/wuffs/lang/token"
|
t "github.com/google/wuffs/lang/token"
|
||||||
@@ -79,7 +81,14 @@ type Struct struct {
|
|||||||
Struct *a.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 {
|
for _, f := range files {
|
||||||
if f == nil {
|
if f == nil {
|
||||||
return nil, errors.New("check: Check given a nil *ast.File")
|
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{
|
c := &Checker{
|
||||||
tm: tm,
|
tm: tm,
|
||||||
reasonMap: rMap,
|
resolveUse: resolveUse,
|
||||||
packageID: base38.Max + 1,
|
reasonMap: rMap,
|
||||||
consts: map[t.ID]Const{},
|
packageID: base38.Max + 1,
|
||||||
funcs: map[t.QID]Func{},
|
consts: map[t.ID]Const{},
|
||||||
statuses: map[t.ID]Status{},
|
funcs: map[t.QID]Func{},
|
||||||
structs: map[t.ID]Struct{},
|
statuses: map[t.ID]Status{},
|
||||||
|
structs: map[t.ID]Struct{},
|
||||||
|
usees: map[t.ID]*usee{},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, phase := range phases {
|
for _, phase := range phases {
|
||||||
@@ -158,8 +169,9 @@ type reason func(q *checker, n *a.Assert) error
|
|||||||
type reasonMap map[t.Key]reason
|
type reasonMap map[t.Key]reason
|
||||||
|
|
||||||
type Checker struct {
|
type Checker struct {
|
||||||
tm *t.Map
|
tm *t.Map
|
||||||
reasonMap reasonMap
|
resolveUse func(usePath string) ([]byte, error)
|
||||||
|
reasonMap reasonMap
|
||||||
|
|
||||||
packageID uint32
|
packageID uint32
|
||||||
otherPackageID *a.PackageID
|
otherPackageID *a.PackageID
|
||||||
@@ -169,6 +181,10 @@ type Checker struct {
|
|||||||
statuses map[t.ID]Status
|
statuses map[t.ID]Status
|
||||||
structs map[t.ID]Struct
|
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
|
builtInFuncs map[t.QID]*a.Func
|
||||||
builtInSliceFuncs map[t.QID]*a.Func
|
builtInSliceFuncs map[t.QID]*a.Func
|
||||||
unsortedStructs []*a.Struct
|
unsortedStructs []*a.Struct
|
||||||
@@ -221,7 +237,61 @@ func (c *Checker) checkPackageIDExists(node *a.Node) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Checker) checkUse(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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -111,7 +111,7 @@ func TestCheck(t *testing.T) {
|
|||||||
t.Fatalf("compareToWuffsfmt: %v", err)
|
t.Fatalf("compareToWuffsfmt: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err := Check(tm, file)
|
c, err := Check(tm, []*ast.File{file}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Check: %v", err)
|
t.Fatalf("Check: %v", err)
|
||||||
}
|
}
|
||||||
@@ -208,7 +208,7 @@ func TestConstValues(t *testing.T) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err := Check(tm, file)
|
c, err := Check(tm, []*ast.File{file}, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("%q: Check: %v", s, err)
|
t.Errorf("%q: Check: %v", s, err)
|
||||||
continue
|
continue
|
||||||
|
|||||||
+26
-11
@@ -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) {
|
func (c *Checker) resolveFunc(typ *a.TypeExpr) (*a.Func, error) {
|
||||||
// TODO: look up funcs in used packages.
|
|
||||||
if typ.Decorator().Key() != t.KeyOpenParen {
|
if typ.Decorator().Key() != t.KeyOpenParen {
|
||||||
return nil, fmt.Errorf("check: resolveFunc cannot look up non-func TypeExpr %q", typ.Str(c.tm))
|
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()}
|
qid := t.QID{typ.Receiver().Name(), typ.Name()}
|
||||||
if f, err := c.builtInFunc(qid); err != nil {
|
switch lTyp := typ.Receiver(); lTyp.Decorator().Key() {
|
||||||
return nil, err
|
case 0:
|
||||||
} else if f != nil {
|
if f, err := c.builtInFunc(qid); err != 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 {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
} else if f != nil {
|
} else if f != nil {
|
||||||
return 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))
|
return nil, fmt.Errorf("check: resolveFunc cannot look up %q", typ.Str(c.tm))
|
||||||
}
|
}
|
||||||
+20
-10
@@ -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.
|
// require a TypeExpr being able to express function and method types.
|
||||||
|
|
||||||
// TODO: delete this hack that only matches "foo.decode(etc)".
|
// TODO: delete this hack that only matches "foo.decode(etc)".
|
||||||
if isThatMethod(q.tm, n, q.tm.ByName("decode").Key(), 2) ||
|
if isThatMethod(q.tm, n, q.tm.ByName("decode").Key(), 3) {
|
||||||
isThatMethod(q.tm, n, q.tm.ByName("decode").Key(), 3) {
|
|
||||||
foo := n.LHS().Expr().LHS().Expr()
|
foo := n.LHS().Expr().LHS().Expr()
|
||||||
if err := q.tcheckExpr(foo, depth); err != nil {
|
if err := q.tcheckExpr(foo, depth); err != nil {
|
||||||
return err
|
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))
|
n.SetMType(a.NewTypeExpr(t.IDOpenParen, n.ID1(), lTyp.Node(), nil, nil))
|
||||||
return 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()}
|
qid := t.QID{lTyp.Name(), n.ID1()}
|
||||||
@@ -724,7 +734,7 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
|
|||||||
name := lTyp.Name()
|
name := lTyp.Name()
|
||||||
s = q.c.structs[name].Struct
|
s = q.c.structs[name].Struct
|
||||||
if s == nil && builtInTypeMap[name] == nil {
|
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",
|
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 {
|
func (q *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error {
|
||||||
@@ -1068,11 +1078,11 @@ swtch:
|
|||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// TODO: don't hard-code deflate.decoder as an acceptable type.
|
if u := q.c.usees[typ.Decorator()]; u == nil {
|
||||||
if q.tm.ByID(typ.Decorator()) == "deflate" && q.tm.ByID(typ.Name()) == "decoder" {
|
return fmt.Errorf("check: cannot resolve %q in type %q", typ.Decorator().Str(q.tm), typ.Str(q.tm))
|
||||||
break
|
} 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()
|
typ.Node().SetTypeChecked()
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ func Do(args []string, g Generator) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err := check.Check(tm, files...)
|
c, err := check.Check(tm, files, resolveUse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -125,6 +125,14 @@ func ParseFiles(tm *token.Map, filenames []string, opts *parse.Options) (files [
|
|||||||
return files, nil
|
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 {
|
var cachedWuffsRoot struct {
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
value string
|
value string
|
||||||
|
|||||||
Reference in new issue
Block a user