Add pkg to ast Func, Struct and TypeExpr nodes

This commit is contained in:
Nigel Tao committed 2017-12-29 12:08:17 +11:00
1 parent 10e4936f08
commit ed03c5943a
16 files changed
+311 -245

No files matched your search

+42 -47
View File
@@ -34,25 +34,25 @@ var (
// typeExprFoo is an *ast.Node MType (implicit type).
var (
typeExprGeneric = a.NewTypeExpr(0, t.IDDiamond, nil, nil, nil)
typeExprIdeal = a.NewTypeExpr(0, t.IDDoubleZ, nil, nil, nil)
typeExprList = a.NewTypeExpr(0, t.IDDollar, nil, nil, nil)
typeExprGeneric = a.NewTypeExpr(0, 0, t.IDDiamond, nil, nil, nil)
typeExprIdeal = a.NewTypeExpr(0, 0, t.IDDoubleZ, nil, nil, nil)
typeExprList = a.NewTypeExpr(0, 0, t.IDDollar, nil, nil, nil)
typeExprU8 = a.NewTypeExpr(0, t.IDU8, nil, nil, nil)
typeExprU16 = a.NewTypeExpr(0, t.IDU16, nil, nil, nil)
typeExprU32 = a.NewTypeExpr(0, t.IDU32, nil, nil, nil)
typeExprU64 = a.NewTypeExpr(0, t.IDU64, nil, nil, nil)
typeExprBool = a.NewTypeExpr(0, t.IDBool, nil, nil, nil)
typeExprStatus = a.NewTypeExpr(0, t.IDStatus, nil, nil, nil)
typeExprReader1 = a.NewTypeExpr(0, t.IDReader1, nil, nil, nil)
typeExprWriter1 = a.NewTypeExpr(0, t.IDWriter1, nil, nil, nil)
typeExprU8 = a.NewTypeExpr(0, 0, t.IDU8, nil, nil, nil)
typeExprU16 = a.NewTypeExpr(0, 0, t.IDU16, nil, nil, nil)
typeExprU32 = a.NewTypeExpr(0, 0, t.IDU32, nil, nil, nil)
typeExprU64 = a.NewTypeExpr(0, 0, t.IDU64, nil, nil, nil)
typeExprBool = a.NewTypeExpr(0, 0, t.IDBool, nil, nil, nil)
typeExprStatus = a.NewTypeExpr(0, 0, t.IDStatus, nil, nil, nil)
typeExprReader1 = a.NewTypeExpr(0, 0, t.IDReader1, nil, nil, nil)
typeExprWriter1 = a.NewTypeExpr(0, 0, t.IDWriter1, nil, nil, nil)
typeExprSliceU8 = a.NewTypeExpr(t.IDColon, 0, nil, nil, typeExprU8)
typeExprSliceU8 = a.NewTypeExpr(t.IDColon, 0, 0, nil, nil, typeExprU8)
// TODO: delete this.
typeExprPlaceholder = a.NewTypeExpr(0, t.IDU8, nil, nil, nil)
typeExprPlaceholder16 = a.NewTypeExpr(0, t.IDU16, nil, nil, nil)
typeExprPlaceholder32 = a.NewTypeExpr(0, t.IDU32, nil, nil, nil)
typeExprPlaceholder = a.NewTypeExpr(0, 0, t.IDU8, nil, nil, nil)
typeExprPlaceholder16 = a.NewTypeExpr(0, 0, t.IDU16, nil, nil, nil)
typeExprPlaceholder32 = a.NewTypeExpr(0, 0, t.IDU32, nil, nil, nil)
)
// TypeMap maps from variable names (as token IDs) to types.
@@ -69,7 +69,7 @@ var builtInTypeMap = TypeMap{
t.IDWriter1: typeExprWriter1,
}
func (c *Checker) builtInFunc(qid t.QID) (*a.Func, error) {
func (c *Checker) builtInFunc(qqid t.QQID) (*a.Func, error) {
if c.builtInFuncs == nil {
err := error(nil)
c.builtInFuncs, err = parseBuiltInFuncs(c.tm, builtin.Funcs, false)
@@ -77,10 +77,10 @@ func (c *Checker) builtInFunc(qid t.QID) (*a.Func, error) {
return nil, err
}
}
return c.builtInFuncs[qid], nil
return c.builtInFuncs[qqid], nil
}
func (c *Checker) builtInSliceFunc(qid t.QID) (*a.Func, error) {
func (c *Checker) builtInSliceFunc(qqid t.QQID) (*a.Func, error) {
if c.builtInSliceFuncs == nil {
err := error(nil)
c.builtInSliceFuncs, err = parseBuiltInFuncs(c.tm, builtin.SliceFuncs, true)
@@ -88,11 +88,11 @@ func (c *Checker) builtInSliceFunc(qid t.QID) (*a.Func, error) {
return nil, err
}
}
return c.builtInSliceFuncs[qid], nil
return c.builtInSliceFuncs[qqid], nil
}
func parseBuiltInFuncs(tm *t.Map, ss []string, generic bool) (map[t.QID]*a.Func, error) {
m := map[t.QID]*a.Func{}
func parseBuiltInFuncs(tm *t.Map, ss []string, generic bool) (map[t.QQID]*a.Func, error) {
m := map[t.QQID]*a.Func{}
buf := []byte(nil)
opts := parse.Options{
AllowBuiltIns: true,
@@ -125,7 +125,7 @@ func parseBuiltInFuncs(tm *t.Map, ss []string, generic bool) (map[t.QID]*a.Func,
return nil, fmt.Errorf("check: parsing %q: got %d top level decls, want %d", s, len(tlds), 1)
}
f := tlds[0].Func()
m[f.QID()] = f
m[f.QQID()] = f
}
return m, nil
}
@@ -135,37 +135,32 @@ func (c *Checker) resolveFunc(typ *a.TypeExpr) (*a.Func, error) {
return nil, fmt.Errorf("check: resolveFunc cannot look up non-func TypeExpr %q", typ.Str(c.tm))
}
lTyp := typ.Receiver()
qid := t.QID{lTyp.Name(), typ.Name()}
switch 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:
lQID := lTyp.QID()
qqid := t.QQID{lQID[0], lQID[1], typ.FuncName()}
if lTyp.Decorator().Key() == t.KeyColon {
// lTyp is a slice.
qid[0] = t.IDDiamond
if f, err := c.builtInSliceFunc(qid); err != nil {
qqid[0] = 0
qqid[1] = t.IDDiamond
if f, err := c.builtInSliceFunc(qqid); err != nil {
return nil, err
} else if f != nil {
return f, nil
}
default:
// lTyp is from a used package: `use "foo"` followed by `foo.bar`.
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))
} else {
if f, err := c.builtInFunc(qqid); err != nil {
return nil, err
} else if f != nil {
return f, nil
}
if f := u.funcs[qid]; f != nil {
// TODO: f.Receiver().Str(c.tm) should be "deflate.decoder", not
// just "decoder".
if f := c.funcs[qqid].Func; f != nil {
return f, nil
}
// lTyp is from a used package: `use "foo"` followed by `foo.bar`.
u := c.usees[qqid[0]]
if u == nil {
return nil, fmt.Errorf("check: cannot resolve %q in type %q", qqid[0].Str(c.tm), lTyp.Str(c.tm))
}
if f := u.funcs[qqid]; f != nil {
return f, nil
}
}