From 10e4936f082c57d0901cc4076edad8d2aee0c203 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 28 Dec 2017 15:08:43 +1100 Subject: [PATCH] Add pkg to ast Const and Status nodes --- cmd/wuffs-c/internal/cgen/cgen.go | 6 +-- cmd/wuffs/gen.go | 9 +++- lang/ast/ast.go | 29 ++++++++++--- lang/check/check.go | 68 ++++++++++++++++--------------- lang/check/type.go | 7 ++-- 5 files changed, 73 insertions(+), 46 deletions(-) diff --git a/cmd/wuffs-c/internal/cgen/cgen.go b/cmd/wuffs-c/internal/cgen/cgen.go index 7ef05b19..4f07db70 100644 --- a/cmd/wuffs-c/internal/cgen/cgen.go +++ b/cmd/wuffs-c/internal/cgen/cgen.go @@ -470,7 +470,7 @@ func (g *gen) sizeof(typ *a.TypeExpr) (uint32, error) { } func (g *gen) gatherStatuses(b *buffer, n *a.Status) error { - raw := n.Message().Str(g.tm) + raw := n.QID()[1].Str(g.tm) msg, ok := t.Unescape(raw) if !ok { return fmt.Errorf("bad status message %q", raw) @@ -485,7 +485,7 @@ func (g *gen) gatherStatuses(b *buffer, n *a.Status) error { keyword: n.Keyword(), } g.statusList = append(g.statusList, s) - g.statusMap[n.Message()] = s + g.statusMap[n.QID()[1]] = s return nil } @@ -494,7 +494,7 @@ func (g *gen) writeConst(b *buffer, n *a.Const) error { b.writes("static ") } b.writes("const ") - if err := g.writeCTypeName(b, n.XType(), g.pkgPrefix, n.Name().Str(g.tm)); err != nil { + if err := g.writeCTypeName(b, n.XType(), g.pkgPrefix, n.QID()[1].Str(g.tm)); err != nil { return err } b.writes(" = ") diff --git a/cmd/wuffs/gen.go b/cmd/wuffs/gen.go index fb52ebb3..70f1ecbb 100644 --- a/cmd/wuffs/gen.go +++ b/cmd/wuffs/gen.go @@ -249,6 +249,13 @@ func (h *genHelper) genWuffs(dirname string, qualifiedFilenames []string) error for _, f := range files { for _, n := range f.TopLevelDecls() { switch n.Kind() { + case a.KConst: + n := n.Const() + if !n.Public() { + continue + } + return fmt.Errorf("TODO: genWuffs for consts") + case a.KFunc: n := n.Func() if !n.Public() { @@ -286,7 +293,7 @@ func (h *genHelper) genWuffs(dirname string, qualifiedFilenames []string) error if !n.Public() { continue } - fmt.Fprintf(out, "pub %s %s\n", n.Keyword().Str(&h.tm), n.Message().Str(&h.tm)) + fmt.Fprintf(out, "pub %s %s\n", n.Keyword().Str(&h.tm), n.QID().Str(&h.tm)) case a.KStruct: n := n.Struct() diff --git a/lang/ast/ast.go b/lang/ast/ast.go index ef906a99..ff29fa9e 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -15,6 +15,7 @@ package ast import ( + "fmt" "math/big" t "github.com/google/wuffs/lang/token" @@ -138,7 +139,7 @@ type Node struct { // Arg . . name Arg // Assert keyword reason . Assert // Assign operator . . Assign - // Const . . name Const + // Const . pkg name Const // Expr operator literal/ident . Expr // Field . . name Field // File . . . File @@ -148,7 +149,7 @@ type Node struct { // Jump keyword label . Jump // PackageID . literal . PackageID // Ret keyword . . Ret - // Status keyword literal . Status + // Status keyword pkg literal Status // Struct . . name Struct // TypeExpr decorator . name TypeExpr // Use . literal . Use @@ -234,6 +235,20 @@ func (n *Raw) SubLists() [3][]*Node { return [3][]*Node{n.list0, n.lis func (n *Raw) SetFilenameLine(f string, l uint32) { n.filename, n.line = f, l } +func (n *Raw) SetPackage(tm *t.Map, pkg t.ID) error { + return n.Node().Walk(func(o *Node) error { + switch o.Kind() { + case KConst, KStatus: + if o.id1 != 0 { + return fmt.Errorf(`invalid SetPackage(%q) call: old package: got %q, want ""`, + pkg.Str(tm), o.id1.Str(tm)) + } + o.id1 = pkg + } + return nil + }) +} + // MaxExprDepth is an advisory limit for an Expr's recursion depth. const MaxExprDepth = 255 @@ -731,7 +746,8 @@ func NewFunc(flags Flags, filename string, line uint32, receiver t.ID, name t.ID // Status is "error ID1" or "suspension ID1": // - FlagsPublic is "pub" vs "pri" // - ID0: -// - ID1: message +// - ID1: <0|pkg> (set by calling SetPackage) +// - ID2: message type Status Node func (n *Status) Node() *Node { return (*Node)(n) } @@ -739,7 +755,7 @@ func (n *Status) Public() bool { return n.flags&FlagsPublic != 0 } func (n *Status) Filename() string { return n.filename } func (n *Status) Line() uint32 { return n.line } func (n *Status) Keyword() t.ID { return n.id0 } -func (n *Status) Message() t.ID { return n.id1 } +func (n *Status) QID() t.QID { return t.QID{n.id1, n.id2} } func NewStatus(flags Flags, filename string, line uint32, keyword t.ID, message t.ID) *Status { return &Status{ @@ -748,12 +764,13 @@ func NewStatus(flags Flags, filename string, line uint32, keyword t.ID, message filename: filename, line: line, id0: keyword, - id1: message, + id2: message, } } // Const is "const ID1 LHS = RHS": // - FlagsPublic is "pub" vs "pri" +// - ID1: <0|pkg> (set by calling SetPackage) // - ID2: name // - LHS: // - RHS: @@ -763,7 +780,7 @@ func (n *Const) Node() *Node { return (*Node)(n) } func (n *Const) Public() bool { return n.flags&FlagsPublic != 0 } func (n *Const) Filename() string { return n.filename } func (n *Const) Line() uint32 { return n.line } -func (n *Const) Name() t.ID { return n.id2 } +func (n *Const) QID() t.QID { return t.QID{n.id1, n.id2} } func (n *Const) XType() *TypeExpr { return n.lhs.TypeExpr() } func (n *Const) Value() *Expr { return n.rhs.Expr() } diff --git a/lang/check/check.go b/lang/check/check.go index ccc8829b..52e88375 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -61,7 +61,7 @@ func (e *Error) Error() string { } type Const struct { - ID t.ID // ID of the const name. + QID t.QID // Qualified ID of the const name. Const *a.Const } @@ -72,7 +72,7 @@ type Func struct { } type Status struct { - ID t.ID // ID of the status message. + QID t.QID // Qualified ID of the status message. Status *a.Status } @@ -82,9 +82,9 @@ type Struct struct { } type usee struct { - consts map[t.ID]*a.Const + consts map[t.QID]*a.Const funcs map[t.QID]*a.Func - statuses map[t.ID]*a.Status + statuses map[t.QID]*a.Status structs map[t.ID]*a.Struct } @@ -116,9 +116,9 @@ func Check(tm *t.Map, files []*a.File, resolveUse func(usePath string) ([]byte, resolveUse: resolveUse, reasonMap: rMap, packageID: base38.Max + 1, - consts: map[t.ID]Const{}, + consts: map[t.QID]Const{}, funcs: map[t.QID]Func{}, - statuses: map[t.ID]Status{}, + statuses: map[t.QID]Status{}, structs: map[t.ID]Struct{}, usees: map[t.ID]*usee{}, } @@ -176,9 +176,9 @@ type Checker struct { packageID uint32 otherPackageID *a.PackageID - consts map[t.ID]Const + consts map[t.QID]Const funcs map[t.QID]Func - statuses map[t.ID]Status + statuses map[t.QID]Status structs map[t.ID]Struct // usees are the packages referred to in the `use "foo/bar"` lines. The @@ -190,11 +190,11 @@ type Checker struct { unsortedStructs []*a.Struct } -func (c *Checker) PackageID() uint32 { return c.packageID } -func (c *Checker) Consts() map[t.ID]Const { return c.consts } -func (c *Checker) Funcs() map[t.QID]Func { return c.funcs } -func (c *Checker) Statuses() map[t.ID]Status { return c.statuses } -func (c *Checker) Structs() map[t.ID]Struct { return c.structs } +func (c *Checker) PackageID() uint32 { return c.packageID } +func (c *Checker) Consts() map[t.QID]Const { return c.consts } +func (c *Checker) Funcs() map[t.QID]Func { return c.funcs } +func (c *Checker) Statuses() map[t.QID]Status { return c.statuses } +func (c *Checker) Structs() map[t.ID]Struct { return c.structs } func (c *Checker) checkPackageID(node *a.Node) error { n := node.PackageID() @@ -270,22 +270,25 @@ func (c *Checker) checkUse(node *a.Node) error { } u := &usee{ - consts: map[t.ID]*a.Const{}, + consts: map[t.QID]*a.Const{}, funcs: map[t.QID]*a.Func{}, - statuses: map[t.ID]*a.Status{}, + statuses: map[t.QID]*a.Status{}, structs: map[t.ID]*a.Struct{}, } for _, n := range f.TopLevelDecls() { + if err := n.Raw().SetPackage(c.tm, base); err != nil { + return err + } switch n.Kind() { case a.KConst: n := n.Const() - u.consts[n.Name()] = n + u.consts[n.QID()] = n case a.KFunc: n := n.Func() u.funcs[n.QID()] = n case a.KStatus: n := n.Status() - u.statuses[n.Message()] = n + u.statuses[n.QID()] = n case a.KStruct: n := n.Struct() u.structs[n.Name()] = n @@ -297,19 +300,18 @@ func (c *Checker) checkUse(node *a.Node) error { func (c *Checker) checkStatus(node *a.Node) error { n := node.Status() - id := n.Message() - if other, ok := c.statuses[id]; ok { - s, _ := t.Unescape(id.Str(c.tm)) + qid := n.QID() + if other, ok := c.statuses[qid]; ok { return &Error{ - Err: fmt.Errorf("check: duplicate status %q", s), + Err: fmt.Errorf("check: duplicate status %s", qid.Str(c.tm)), Filename: n.Filename(), Line: n.Line(), OtherFilename: other.Status.Filename(), OtherLine: other.Status.Line(), } } - c.statuses[id] = Status{ - ID: id, + c.statuses[qid] = Status{ + QID: qid, Status: n, } n.Node().SetTypeChecked() @@ -318,18 +320,18 @@ func (c *Checker) checkStatus(node *a.Node) error { func (c *Checker) checkConst(node *a.Node) error { n := node.Const() - id := n.Name() - if other, ok := c.consts[id]; ok { + qid := n.QID() + if other, ok := c.consts[qid]; ok { return &Error{ - Err: fmt.Errorf("check: duplicate const %q", id.Str(c.tm)), + Err: fmt.Errorf("check: duplicate const %s", qid.Str(c.tm)), Filename: n.Filename(), Line: n.Line(), OtherFilename: other.Const.Filename(), OtherLine: other.Const.Line(), } } - c.consts[id] = Const{ - ID: id, + c.consts[qid] = Const{ + QID: qid, Const: n, } @@ -338,10 +340,10 @@ func (c *Checker) checkConst(node *a.Node) error { tm: c.tm, } if err := q.tcheckTypeExpr(n.XType(), 0); err != nil { - return fmt.Errorf("%v in const %q", err, id.Str(c.tm)) + return fmt.Errorf("%v in const %s", err, qid.Str(c.tm)) } if err := q.tcheckExpr(n.Value(), 0); err != nil { - return fmt.Errorf("%v in const %q", err, id.Str(c.tm)) + return fmt.Errorf("%v in const %s", err, qid.Str(c.tm)) } nLists := 0 @@ -354,17 +356,17 @@ func (c *Checker) checkConst(node *a.Node) error { typ = typ.Inner() } if typ.Decorator() != 0 { - return fmt.Errorf("check: invalid const type %q for %q", n.XType().Str(c.tm), id.Str(c.tm)) + return fmt.Errorf("check: invalid const type %q for %s", n.XType().Str(c.tm), qid.Str(c.tm)) } nMin, nMax, err := q.bcheckTypeExpr(typ) if err != nil { return err } if nMin == nil || nMax == nil { - return fmt.Errorf("check: invalid const type %q for %q", n.XType().Str(c.tm), id.Str(c.tm)) + return fmt.Errorf("check: invalid const type %q for %s", n.XType().Str(c.tm), qid.Str(c.tm)) } if err := c.checkConstElement(n.Value(), nMin, nMax, nLists); err != nil { - return fmt.Errorf("check: %v for %q", err, id.Str(c.tm)) + return fmt.Errorf("check: %v for %s", err, qid.Str(c.tm)) } n.Node().SetTypeChecked() return nil diff --git a/lang/check/type.go b/lang/check/type.go index cea76a56..81983250 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -394,14 +394,15 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { return nil } } - if c, ok := q.c.consts[id1]; ok { + if c, ok := q.c.consts[t.QID{0, id1}]; ok { // TODO: check somewhere that a global ident (i.e. a const) is // not directly in the LHS of an assignment. n.SetGlobalIdent() n.SetMType(c.Const.XType()) return nil } - // TODO: look for other (global) names: funcs, structs. + // TODO: look for other (global) names: consts, funcs, statuses, + // structs from used packages. return fmt.Errorf("check: unrecognized identifier %q", id1.Str(q.tm)) } switch id1.Key() { @@ -522,7 +523,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { case t.KeyError, t.KeyStatus, t.KeySuspension: nominalKeyword := n.ID0() declaredKeyword := t.ID(0) - if s, ok := q.c.statuses[n.ID1()]; ok { + if s, ok := q.c.statuses[t.QID{0, n.ID1()}]; ok { declaredKeyword = s.Status.Keyword() } else { msg, _ := t.Unescape(n.ID1().Str(q.tm))