From 457235a382a837adf59a25bfbc434f044cb9cf33 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 29 Dec 2017 14:23:58 +1100 Subject: [PATCH] Use qualified IDs instead of a usee map --- lang/check/check.go | 83 +++++++++++++++++++++++++------------------ lang/check/resolve.go | 8 ----- lang/check/type.go | 22 ------------ 3 files changed, 48 insertions(+), 65 deletions(-) diff --git a/lang/check/check.go b/lang/check/check.go index e92bd6d3..d7bf521b 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -60,13 +60,6 @@ func (e *Error) Error() string { return string(b) } -type usee struct { - consts map[t.QID]*a.Const - funcs map[t.QQID]*a.Func - statuses map[t.QID]*a.Status - structs map[t.QID]*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 { @@ -91,16 +84,16 @@ func Check(tm *t.Map, files []*a.File, resolveUse func(usePath string) ([]byte, } } c := &Checker{ - tm: tm, - resolveUse: resolveUse, - reasonMap: rMap, - packageID: base38.Max + 1, - consts: map[t.QID]*a.Const{}, - funcs: map[t.QQID]*a.Func{}, - localVars: map[t.QQID]typeMap{}, - statuses: map[t.QID]*a.Status{}, - structs: map[t.QID]*a.Struct{}, - usees: map[t.ID]*usee{}, + tm: tm, + resolveUse: resolveUse, + reasonMap: rMap, + packageID: base38.Max + 1, + consts: map[t.QID]*a.Const{}, + funcs: map[t.QQID]*a.Func{}, + localVars: map[t.QQID]typeMap{}, + statuses: map[t.QID]*a.Status{}, + structs: map[t.QID]*a.Struct{}, + useBaseNames: map[t.ID]struct{}{}, } for _, phase := range phases { @@ -162,9 +155,9 @@ type Checker struct { statuses map[t.QID]*a.Status structs map[t.QID]*a.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 + // useBaseNames are the base names of packages referred to by `use + // "foo/bar"` lines. The keys are `bar`, not `"foo/bar"`. + useBaseNames map[t.ID]struct{} builtInFuncs map[t.QQID]*a.Func builtInSliceFuncs map[t.QQID]*a.Func @@ -219,13 +212,13 @@ func (c *Checker) checkUse(node *a.Node) error { if !ok { return fmt.Errorf("check: cannot resolve `use %s`", usePath.Str(c.tm)) } - base, err := c.tm.Insert(path.Base(filename)) + baseName, 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 _, ok := c.useBaseNames[baseName]; ok { + return fmt.Errorf("check: duplicate `use \"etc\"` base name %q", baseName.Str(c.tm)) } if c.resolveUse == nil { @@ -246,32 +239,52 @@ func (c *Checker) checkUse(node *a.Node) error { return err } - u := &usee{ - consts: map[t.QID]*a.Const{}, - funcs: map[t.QQID]*a.Func{}, - statuses: map[t.QID]*a.Status{}, - structs: map[t.QID]*a.Struct{}, - } for _, n := range f.TopLevelDecls() { - if err := n.Raw().SetPackage(c.tm, base); err != nil { + if err := n.Raw().SetPackage(c.tm, baseName); err != nil { return err } + + duplicate := "" switch n.Kind() { case a.KConst: n := n.Const() - u.consts[n.QID()] = n + qid := n.QID() + if _, ok := c.consts[qid]; ok { + duplicate = qid.Str(c.tm) + } else { + c.consts[qid] = n + } case a.KFunc: n := n.Func() - u.funcs[n.QQID()] = n + qqid := n.QQID() + if _, ok := c.funcs[qqid]; ok { + duplicate = qqid.Str(c.tm) + } else { + c.funcs[qqid] = n + } case a.KStatus: n := n.Status() - u.statuses[n.QID()] = n + qid := n.QID() + if _, ok := c.statuses[qid]; ok { + duplicate = qid.Str(c.tm) + } else { + c.statuses[qid] = n + } case a.KStruct: n := n.Struct() - u.structs[n.QID()] = n + qid := n.QID() + if _, ok := c.structs[qid]; ok { + duplicate = qid.Str(c.tm) + } else { + c.structs[qid] = n + } + } + + if duplicate != "" { + return fmt.Errorf("check: duplicate name %s", duplicate) } } - c.usees[base] = u + c.useBaseNames[baseName] = struct{}{} return nil } diff --git a/lang/check/resolve.go b/lang/check/resolve.go index 908d720e..af21e097 100644 --- a/lang/check/resolve.go +++ b/lang/check/resolve.go @@ -155,14 +155,6 @@ func (c *Checker) resolveFunc(typ *a.TypeExpr) (*a.Func, error) { if f := c.funcs[qqid]; 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 - } } return nil, fmt.Errorf("check: resolveFunc cannot look up %q", typ.Str(c.tm)) } diff --git a/lang/check/type.go b/lang/check/type.go index e4bb27d3..efb5838b 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -700,19 +700,6 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error { return fmt.Errorf("check: invalid type %q for dot-expression LHS %q", lTyp.Str(q.tm), lhs.Str(q.tm)) } - if qqid[0] != 0 { - // lTyp is from a used package: `use "foo"` followed by `foo.bar`. - if u := q.c.usees[qqid[0]]; u == nil { - return fmt.Errorf("check: cannot resolve %q in type %q for expression %q", - qqid[0].Str(q.tm), lTyp.Str(q.tm), lhs.Str(q.tm)) - } else if u.funcs[qqid] == nil { - return fmt.Errorf("check: no method named %q found in type %q for expression %q", - n.Ident().Str(q.tm), lTyp.Str(q.tm), n.Str(q.tm)) - } - n.SetMType(a.NewTypeExpr(t.IDOpenParen, 0, n.Ident(), lTyp.Node(), nil, nil)) - return nil - } - f, err := q.c.builtInFunc(qqid) if err != nil { return err @@ -1038,15 +1025,6 @@ swtch: // TODO: also check t.KeyOpenParen. case 0: qid := typ.QID() - if qid[0] != 0 { - if u := q.c.usees[qid[0]]; u == nil { - return fmt.Errorf("check: cannot resolve %q in type %q", qid[0].Str(q.tm), typ.Str(q.tm)) - } else if u.structs[qid] == nil { - return fmt.Errorf("check: cannot resolve type %q", typ.Str(q.tm)) - } - break - } - if qid[1].IsNumType() { for _, b := range typ.Bounds() { if b == nil {