From 203b45e869c27f3826bed2ee143f1b5e1130033e Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 12 Aug 2018 19:36:29 +1000 Subject: [PATCH] Remove status values --- cmd/wuffs-c/internal/cgen/cgen.go | 52 +++++++++----------------- cmd/wuffs-c/internal/cgen/expr.go | 6 --- cmd/wuffs/gen.go | 3 +- lang/ast/ast.go | 7 +--- lang/check/check.go | 29 -------------- lang/check/type.go | 15 ++------ lang/parse/parse.go | 20 +--------- release/c/wuffs-unsupported-snapshot.h | 1 - std/deflate/decode_deflate.wuffs | 36 +++++++++--------- std/gif/decode_gif.wuffs | 16 ++++---- std/gzip/decode_gzip.wuffs | 8 ++-- std/lzw/decode_lzw.wuffs | 4 +- std/zlib/decode_zlib.wuffs | 10 ++--- 13 files changed, 61 insertions(+), 146 deletions(-) diff --git a/cmd/wuffs-c/internal/cgen/cgen.go b/cmd/wuffs-c/internal/cgen/cgen.go index 7ef8bbef..9676512d 100644 --- a/cmd/wuffs-c/internal/cgen/cgen.go +++ b/cmd/wuffs-c/internal/cgen/cgen.go @@ -181,19 +181,9 @@ const ( ) type status struct { - cName string - name string - value int8 - msg string - keyword t.ID - public bool -} - -func (z status) cNamePrefix() string { - if z.keyword == t.IDSuspension { - return "suspension" - } - return "error" + cName string + msg string + public bool } type buffer []byte @@ -361,11 +351,11 @@ func (g *gen) genHeader(b *buffer) error { b.writes("// ---------------- Status Codes\n\n") - for _, s := range g.statusList { - if !s.public { + for _, z := range g.statusList { + if !z.public { continue } - b.printf("extern const char* %s%s__%s;\n", g.pkgPrefix, s.cNamePrefix(), s.cName) + b.printf("extern const char* %s;\n", z.cName) } b.writes("\n") @@ -418,8 +408,7 @@ func (g *gen) genImpl(b *buffer) error { if z.msg == "" { continue } - b.printf("const char* %s%s__%s = \"%s%s: %s\";\n", - g.pkgPrefix, z.cNamePrefix(), z.cName, z.msg[:1], g.pkgName, z.msg[1:]) + b.printf("const char* %s = \"%s%s: %s\";\n", z.cName, z.msg[:1], g.pkgName, z.msg[1:]) } b.writes("\n") @@ -573,27 +562,20 @@ func (g *gen) sizeof(typ *a.TypeExpr) (uint32, error) { func (g *gen) gatherStatuses(b *buffer, n *a.Status) error { raw := n.QID()[1].Str(g.tm) msg, ok := t.Unescape(raw) - if !ok { + if !ok || msg == "" { return fmt.Errorf("bad status message %q", raw) } - prefix := "suspension_" - if n.Keyword() == t.IDError { - prefix = "error_" + errSus := "error__" + if msg[0] == '$' { + errSus = "suspension__" } - value := int8(n.Value().ConstValue().Int64()) - if n.Keyword() == t.IDError { - value = -value + z := status{ + cName: g.pkgPrefix + errSus + cName(msg, ""), + msg: msg, + public: n.Public(), } - s := status{ - cName: cName(msg, ""), - name: strings.ToUpper(g.cName(prefix + msg)), - value: value, - msg: msg, - keyword: n.Keyword(), - public: n.Public(), - } - g.statusList = append(g.statusList, s) - g.statusMap[n.QID()] = s + g.statusList = append(g.statusList, z) + g.statusMap[n.QID()] = z return nil } diff --git a/cmd/wuffs-c/internal/cgen/expr.go b/cmd/wuffs-c/internal/cgen/expr.go index 587e621b..ded90683 100644 --- a/cmd/wuffs-c/internal/cgen/expr.go +++ b/cmd/wuffs-c/internal/cgen/expr.go @@ -210,12 +210,6 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, depth u case t.IDError, t.IDStatus, t.IDSuspension: if z := g.statusMap[n.StatusQID()]; z.cName != "" { - b.writes(g.pkgPrefix) - if z.keyword == t.IDSuspension { - b.writes("suspension__") - } else { - b.writes("error__") - } b.writes(z.cName) } else { msg, _ := t.Unescape(n.Ident().Str(g.tm)) diff --git a/cmd/wuffs/gen.go b/cmd/wuffs/gen.go index cf9dbf6e..8d869317 100644 --- a/cmd/wuffs/gen.go +++ b/cmd/wuffs/gen.go @@ -310,8 +310,7 @@ func (h *genHelper) genWuffs(dirname string, qualifiedFilenames []string) error if !n.Public() { continue } - fmt.Fprintf(out, "pub %s (%s) %s\n", - n.Keyword().Str(&h.tm), n.Value().Str(&h.tm), n.QID().Str(&h.tm)) + fmt.Fprintf(out, "pub status %s\n", n.QID().Str(&h.tm)) case a.KStruct: n := n.AsStruct() diff --git a/lang/ast/ast.go b/lang/ast/ast.go index 9286c868..f6cba99a 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -846,7 +846,6 @@ func NewFunc(flags Flags, filename string, line uint32, receiverName t.ID, funcN // Status is "error (RHS) ID2" or "suspension (RHS) ID2": // - FlagsPublic is "pub" vs "pri" -// - ID0: // - ID1: <0|pkg> (set by calling SetPackage) // - ID2: message type Status Node @@ -855,19 +854,15 @@ func (n *Status) AsNode() *Node { return (*Node)(n) } 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) QID() t.QID { return t.QID{n.id1, n.id2} } -func (n *Status) Value() *Expr { return n.rhs.AsExpr() } -func NewStatus(flags Flags, filename string, line uint32, keyword t.ID, value *Expr, message t.ID) *Status { +func NewStatus(flags Flags, filename string, line uint32, message t.ID) *Status { return &Status{ kind: KStatus, flags: flags, filename: filename, line: line, - id0: keyword, id2: message, - rhs: value.AsNode(), } } diff --git a/lang/check/check.go b/lang/check/check.go index a2bb1544..e8ebb23a 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -177,8 +177,6 @@ type Checker struct { builtInSliceFuncs map[t.QQID]*a.Func builtInTableFuncs map[t.QQID]*a.Func unsortedStructs []*a.Struct - - statusByValue [256]t.ID } func (c *Checker) PackageID() uint32 { return c.packageID } @@ -298,33 +296,6 @@ func (c *Checker) checkStatus(node *a.Node) error { } c.statuses[qid] = n - q := &checker{ - c: c, - tm: c.tm, - } - if err := q.tcheckExpr(n.Value(), 0); err != nil { - return fmt.Errorf("%v in status %s", err, qid.Str(c.tm)) - } - if _, err := q.bcheckExpr(n.Value(), 0); err != nil { - return fmt.Errorf("%v in status %s", err, qid.Str(c.tm)) - } - - if cv := n.Value().ConstValue(); cv == nil { - return fmt.Errorf("status %s value is not a constant expression", qid.Str(c.tm)) - } else if cv.Cmp(zero) <= 0 || oneTwentyEight.Cmp(cv) <= 0 { - return fmt.Errorf("status %s value %v is out of range [0x01..0x7F]", qid.Str(c.tm), cv) - } else if n.QID()[0] == 0 { - x := uint8(cv.Int64()) - if n.Keyword() == t.IDError { - x = -x - } - if other := c.statusByValue[x]; other != 0 { - return fmt.Errorf("duplicate %s value 0x%02X: %s and %s", - n.Keyword().Str(c.tm), cv, other.Str(c.tm), n.QID()[1].Str(c.tm)) - } - c.statusByValue[x] = n.QID()[1] - } - setPlaceholderMBoundsMType(n.AsNode()) return nil } diff --git a/lang/check/type.go b/lang/check/type.go index d4ac1783..1d033478 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -489,22 +489,13 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error { case t.IDDot: return q.tcheckDot(n, depth) + // TODO: remove all but t.IDStatus. case t.IDError, t.IDStatus, t.IDSuspension: - nominalKeyword := n.Operator() - declaredKeyword := t.ID(0) - if s, ok := q.c.statuses[n.StatusQID()]; ok { - declaredKeyword = s.Keyword() - } else { + if _, ok := q.c.statuses[n.StatusQID()]; !ok { msg, _ := t.Unescape(n.Ident().Str(q.tm)) - z, ok := builtin.StatusMap[msg] - if !ok { + if _, ok := builtin.StatusMap[msg]; !ok { return fmt.Errorf("check: no error or status with message %q", msg) } - declaredKeyword = z.Keyword - } - if nominalKeyword != declaredKeyword { - return fmt.Errorf("check: status literal says %q but declaration says %q", - nominalKeyword.Str(q.tm), declaredKeyword.Str(q.tm)) } n.SetMType(typeExprStatus) return nil diff --git a/lang/parse/parse.go b/lang/parse/parse.go index e4fed23d..0ead8d56 100644 --- a/lang/parse/parse.go +++ b/lang/parse/parse.go @@ -221,23 +221,7 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) { in := a.NewStruct(0, p.filename, line, t.IDArgs, argFields) return a.NewFunc(flags, p.filename, line, id0, id1, in, out, asserts, body).AsNode(), nil - case t.IDError, t.IDSuspension: - keyword := p.src[0].ID - p.src = p.src[1:] - - if x := p.peek1(); x != t.IDOpenParen { - got := p.tm.ByID(x) - return nil, fmt.Errorf(`parse: expected "(", got %q at %s:%d`, got, p.filename, p.line()) - } - p.src = p.src[1:] - value, err := p.parseExpr() - if err != nil { - return nil, err - } - if x := p.peek1(); x != t.IDCloseParen { - got := p.tm.ByID(x) - return nil, fmt.Errorf(`parse: expected ")", got %q at %s:%d`, got, p.filename, p.line()) - } + case t.IDStatus: p.src = p.src[1:] message := p.peek1() @@ -251,7 +235,7 @@ func (p *parser) parseTopLevelDecl() (*a.Node, error) { return nil, fmt.Errorf(`parse: expected (implicit) ";", got %q at %s:%d`, got, p.filename, p.line()) } p.src = p.src[1:] - return a.NewStatus(flags, p.filename, line, keyword, value, message).AsNode(), nil + return a.NewStatus(flags, p.filename, line, message).AsNode(), nil case t.IDStruct: p.src = p.src[1:] diff --git a/release/c/wuffs-unsupported-snapshot.h b/release/c/wuffs-unsupported-snapshot.h index 691db406..63b1c430 100644 --- a/release/c/wuffs-unsupported-snapshot.h +++ b/release/c/wuffs-unsupported-snapshot.h @@ -2199,7 +2199,6 @@ extern const char* wuffs_deflate__error__bad_literal_length_code_count; extern const char* wuffs_deflate__error__inconsistent_stored_block_length; extern const char* wuffs_deflate__error__missing_end_of_block_code; extern const char* wuffs_deflate__error__no_huffman_codes; -extern const char* wuffs_deflate__error__internal_error_inconsistent_i_o; // ---------------- Public Consts diff --git a/std/deflate/decode_deflate.wuffs b/std/deflate/decode_deflate.wuffs index 65fe9584..d81a7a6d 100644 --- a/std/deflate/decode_deflate.wuffs +++ b/std/deflate/decode_deflate.wuffs @@ -14,25 +14,25 @@ packageid "dflt" -pub error (0x01) "?bad Huffman code (over-subscribed)" -pub error (0x02) "?bad Huffman code (under-subscribed)" -pub error (0x03) "?bad Huffman code length count" -pub error (0x04) "?bad Huffman code length repetition" -pub error (0x05) "?bad Huffman code" -pub error (0x06) "?bad Huffman minimum code length" -pub error (0x07) "?bad block" -pub error (0x08) "?bad distance" -pub error (0x09) "?bad distance code count" -pub error (0x0A) "?bad literal/length code count" -pub error (0x0B) "?inconsistent stored block length" -pub error (0x0C) "?missing end-of-block code" -pub error (0x0D) "?no Huffman codes" +pub status "?bad Huffman code (over-subscribed)" +pub status "?bad Huffman code (under-subscribed)" +pub status "?bad Huffman code length count" +pub status "?bad Huffman code length repetition" +pub status "?bad Huffman code" +pub status "?bad Huffman minimum code length" +pub status "?bad block" +pub status "?bad distance" +pub status "?bad distance code count" +pub status "?bad literal/length code count" +pub status "?inconsistent stored block length" +pub status "?missing end-of-block code" +pub status "?no Huffman codes" -pri error (0x40) "?internal error: inconsistent Huffman decoder state" -pri error (0x41) "?internal error: inconsistent Huffman end_of_block" -pub error (0x42) "?internal error: inconsistent I/O" -pri error (0x43) "?internal error: inconsistent distance" -pri error (0x44) "?internal error: inconsistent n_bits" +pri status "?internal error: inconsistent Huffman decoder state" +pri status "?internal error: inconsistent Huffman end_of_block" +pri status "?internal error: inconsistent I/O" +pri status "?internal error: inconsistent distance" +pri status "?internal error: inconsistent n_bits" // The next two tables were created by script/print-deflate-magic-numbers.go. // diff --git a/std/gif/decode_gif.wuffs b/std/gif/decode_gif.wuffs index bda66276..24e4331f 100644 --- a/std/gif/decode_gif.wuffs +++ b/std/gif/decode_gif.wuffs @@ -16,15 +16,15 @@ packageid "gif " use "std/lzw" -pub error (0x01) "?bad block" -pub error (0x02) "?bad extension label" -pub error (0x03) "?bad graphic control" -pub error (0x04) "?bad header" -pub error (0x05) "?bad literal width" -pub error (0x06) "?not enough pixel data" -pub error (0x07) "?too much pixel data" +pub status "?bad block" +pub status "?bad extension label" +pub status "?bad graphic control" +pub status "?bad header" +pub status "?bad literal width" +pub status "?not enough pixel data" +pub status "?too much pixel data" -pri error (0x40) "?internal error: inconsistent ri/wi" +pri status "?internal error: inconsistent ri/wi" // See the spec appendix E "Interlaced Images" on page 29. The first element // represents either that the frame was non-interlaced, or that all interlace diff --git a/std/gzip/decode_gzip.wuffs b/std/gzip/decode_gzip.wuffs index 4e93686f..68afa04a 100644 --- a/std/gzip/decode_gzip.wuffs +++ b/std/gzip/decode_gzip.wuffs @@ -17,10 +17,10 @@ packageid "gzip" use "std/crc32" use "std/deflate" -pub error (0x01) "?bad checksum" -pub error (0x02) "?bad compression method" -pub error (0x03) "?bad encoding flags" -pub error (0x04) "?bad header" +pub status "?bad checksum" +pub status "?bad compression method" +pub status "?bad encoding flags" +pub status "?bad header" pub struct decoder?( flate deflate.decoder, diff --git a/std/lzw/decode_lzw.wuffs b/std/lzw/decode_lzw.wuffs index 8f4672ae..9b9e799e 100644 --- a/std/lzw/decode_lzw.wuffs +++ b/std/lzw/decode_lzw.wuffs @@ -14,8 +14,8 @@ packageid "lzw " -pub error (0x01) "?bad code" -pub error (0x02) "?cyclical prefix chain" +pub status "?bad code" +pub status "?cyclical prefix chain" pub struct decoder?( literal_width base.u32[..8], diff --git a/std/zlib/decode_zlib.wuffs b/std/zlib/decode_zlib.wuffs index e9300748..f7593ae6 100644 --- a/std/zlib/decode_zlib.wuffs +++ b/std/zlib/decode_zlib.wuffs @@ -17,12 +17,12 @@ packageid "zlib" use "std/adler32" use "std/deflate" -pub error (0x01) "?bad checksum" -pub error (0x02) "?bad compression method" -pub error (0x03) "?bad compression window size" -pub error (0x04) "?bad parity check" +pub status "?bad checksum" +pub status "?bad compression method" +pub status "?bad compression window size" +pub status "?bad parity check" -pri error (0x40) "?TODO: unsupported preset dictionary" +pri status "?TODO: unsupported preset dictionary" pub struct decoder?( flate deflate.decoder,