From 86c6b79e1ac75ac01695f6f0f9a010fa035d8392 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 15 Jun 2017 15:19:41 +1000 Subject: [PATCH] Handle structs that contain structs. --- cmd/puffs-gen-c/main.go | 148 +++++++++++++++++++-------------------- gen/c/gif.c | 52 +++++++++++++- gen/h/gif.h | 28 +++++++- lang/ast/ast.go | 6 ++ lang/ast/sort.go | 57 +++++++++++++++ lang/check/bounds.go | 5 +- lang/check/check.go | 48 +++++++++---- lang/check/type.go | 9 ++- std/gif/decode_gif.puffs | 6 ++ 9 files changed, 263 insertions(+), 96 deletions(-) create mode 100644 lang/ast/sort.go create mode 100644 std/gif/decode_gif.puffs diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index d950d8c6..19630cfb 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -167,6 +167,20 @@ func (g *gen) generate() error { return err } + // Make a topologically sorted list of structs. + unsortedStructs := []*a.Struct(nil) + for _, file := range g.files { + for _, n := range file.TopLevelDecls() { + if n.Kind() == a.KStruct { + unsortedStructs = append(unsortedStructs, n.Struct()) + } + } + } + structs, ok := a.TopologicalSortStructs(unsortedStructs) + if !ok { + return fmt.Errorf("cyclical struct definitions") + } + includeGuard := "PUFFS_" + strings.ToUpper(g.pkgName) + "_H" g.printf("#ifndef %s\n#define %s\n\n", includeGuard, includeGuard) @@ -197,14 +211,20 @@ func (g *gen) generate() error { g.printf("bool puffs_%s_status_is_error(puffs_%s_status s);\n\n", g.pkgName, g.pkgName) g.printf("const char* puffs_%s_status_string(puffs_%s_status s);\n\n", g.pkgName, g.pkgName) - g.writes("// ---------------- Public Structs\n\n") - if err := g.forEachStruct(pubOnly, (*gen).writeStruct); err != nil { - return err + g.writes("// ---------------- Structs\n\n") + for _, n := range structs { + if err := g.writeStruct(n); err != nil { + return err + } } g.writes("// ---------------- Public Constructor and Destructor Prototypes\n\n") - if err := g.forEachStruct(pubOnly, (*gen).writeCtorPrototypesPub); err != nil { - return err + for _, n := range structs { + if n.Public() { + if err := g.writeCtorPrototypesPub(n); err != nil { + return err + } + } } g.writes("// ---------------- Public Function Prototypes\n\n") @@ -247,14 +267,13 @@ func (g *gen) generate() error { g.printf("}\nreturn \"%s: unknown status\";\n", g.pkgName) g.writes("}\n\n") - g.writes("// ---------------- Private Structs\n\n") - if err := g.forEachStruct(priOnly, (*gen).writeStruct); err != nil { - return err - } - g.writes("// ---------------- Private Constructor and Destructor Prototypes\n\n") - if err := g.forEachStruct(priOnly, (*gen).writeCtorPrototypesPri); err != nil { - return err + for _, n := range structs { + if !n.Public() { + if err := g.writeCtorPrototypesPri(n); err != nil { + return err + } + } } g.writes("// ---------------- Private Function Prototypes\n\n") @@ -275,8 +294,10 @@ func (g *gen) generate() error { g.writes("//\n") g.writes("// Its (non-zero) value is arbitrary, based on md5sum(\"zeroed\").\n") g.writes("#define PUFFS_ALREADY_ZEROED (0x68602EF1U)\n\n") - if err := g.forEachStruct(bothPubPri, (*gen).writeCtorImpls); err != nil { - return err + for _, n := range structs { + if err := g.writeCtorImpls(n); err != nil { + return err + } } g.writes("// ---------------- Function Implementations\n\n") @@ -319,22 +340,6 @@ func (g *gen) forEachStatus(v visibility, f func(*gen, *a.Status) error) error { return nil } -func (g *gen) forEachStruct(v visibility, f func(*gen, *a.Struct) error) error { - for _, file := range g.files { - for _, n := range file.TopLevelDecls() { - if n.Kind() != a.KStruct || - (v == pubOnly && n.Raw().Flags()&a.FlagsPublic == 0) || - (v == priOnly && n.Raw().Flags()&a.FlagsPublic != 0) { - continue - } - if err := f(g, n.Struct()); err != nil { - return err - } - } - } - return nil -} - func (g *gen) cName(name string) string { b := []byte(nil) b = append(b, "puffs_"...) @@ -662,50 +667,22 @@ func (g *gen) writeFuncImplArgChecks(n *a.Func) error { } func (g *gen) writeField(n *a.Field, namePrefix string) error { - // TODO: use g.writeCTypeName. - const maxNPtr = 16 - - convertible, nPtr := true, 0 - for x := n.XType(); x != nil; x = x.Inner() { - if p := x.Decorator().Key(); p == t.KeyPtr { - if nPtr == maxNPtr { - return fmt.Errorf("cannot convert Puffs type %q to C: too many ptr's", n.XType().String(g.tm)) - } - nPtr++ - continue - } else if p == t.KeyOpenBracket { - continue - } else if p != 0 { - convertible = false - break - } - if k := x.Name().Key(); k < t.Key(len(cTypeNames)) { - if s := cTypeNames[k]; s != "" { - g.writes(s) - g.writeb(' ') - continue - } - } - convertible = false - break - } - if !convertible { - // TODO: fix this. - return fmt.Errorf("cannot convert Puffs type %q to C", n.XType().String(g.tm)) + x := n.XType() + for ; x != nil && x.Decorator().Key() == t.KeyOpenBracket; x = x.Inner() { } - for i := 0; i < nPtr; i++ { - g.writeb('*') + if err := g.writeCTypeName(x); err != nil { + return err } + g.writeb(' ') g.writes(namePrefix) g.writes(n.Name().String(g.tm)) - for x := n.XType(); x != nil; x = x.Inner() { - if x.Decorator() == t.IDOpenBracket { - g.writeb('[') - g.writes(x.ArrayLength().ConstValue().String()) - g.writeb(']') - } + x = n.XType() + for ; x != nil && x.Decorator().Key() == t.KeyOpenBracket; x = x.Inner() { + g.writeb('[') + g.writes(x.ArrayLength().ConstValue().String()) + g.writeb(']') } return nil @@ -1222,16 +1199,37 @@ func (g *gen) writeExprAssociativeOp(n *a.Expr, rp replacementPolicy, depth uint } func (g *gen) writeCTypeName(n *a.TypeExpr) error { - if n.Decorator() == 0 { - if k := n.Name().Key(); k < t.Key(len(cTypeNames)) { - if s := cTypeNames[k]; s != "" { - g.writes(s) - return nil + const maxNPtr = 16 + + numPointers, innermost := 0, n + for ; innermost != nil && innermost.Inner() != nil; innermost = innermost.Inner() { + // TODO: "nptr T", not just "ptr T". + if p := innermost.Decorator().Key(); p == t.KeyPtr { + if numPointers == maxNPtr { + return fmt.Errorf("cannot convert Puffs type %q to C: too many ptr's", n.String(g.tm)) } + numPointers++ + continue + } + // TODO: fix this. + return fmt.Errorf("cannot convert Puffs type %q to C", n.String(g.tm)) + } + + fallback := true + if k := innermost.Name().Key(); k < t.Key(len(cTypeNames)) { + if s := cTypeNames[k]; s != "" { + g.writes(s) + fallback = false } } - // TODO: fix this. - return fmt.Errorf("cannot convert Puffs type %q to C", n.String(g.tm)) + if fallback { + g.printf("puffs_%s_%s", g.pkgName, n.Name().String(g.tm)) + } + + for i := 0; i < numPointers; i++ { + g.writeb('*') + } + return nil } var numTypeBounds = [256][2]*big.Int{ diff --git a/gen/c/gif.c b/gen/c/gif.c index 918c785e..7acffb33 100644 --- a/gen/c/gif.c +++ b/gen/c/gif.c @@ -73,7 +73,7 @@ bool puffs_gif_status_is_error(puffs_gif_status s); const char* puffs_gif_status_string(puffs_gif_status s); -// ---------------- Public Structs +// ---------------- Structs typedef struct { // Do not access the private_impl's fields directly. There is no API/ABI @@ -93,6 +93,21 @@ typedef struct { } private_impl; } puffs_gif_lzw_decoder; +typedef struct { + // Do not access the private_impl's fields directly. There is no API/ABI + // compatibility or safety guarantee if you do so. Instead, use the + // puffs_gif_decoder_etc functions. + // + // In C++, these fields would be "private", but C does not support that. + // + // It is a struct, not a struct*, so that it can be stack allocated. + struct { + puffs_gif_status status; + uint32_t magic; + puffs_gif_lzw_decoder f_lzw; + } private_impl; +} puffs_gif_decoder; + // ---------------- Public Constructor and Destructor Prototypes // puffs_gif_lzw_decoder_constructor is a constructor function. @@ -106,6 +121,17 @@ void puffs_gif_lzw_decoder_constructor(puffs_gif_lzw_decoder* self, void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self); +// puffs_gif_decoder_constructor is a constructor function. +// +// It should be called before any other puffs_gif_decoder_* function. +// +// Pass PUFFS_VERSION and 0 for puffs_version and for_internal_use_only. +void puffs_gif_decoder_constructor(puffs_gif_decoder* self, + uint32_t puffs_version, + uint32_t for_internal_use_only); + +void puffs_gif_decoder_destructor(puffs_gif_decoder* self); + // ---------------- Public Function Prototypes void puffs_gif_lzw_decoder_set_literal_width(puffs_gif_lzw_decoder* self, @@ -164,8 +190,6 @@ const char* puffs_gif_status_string(puffs_gif_status s) { return "gif: unknown status"; } -// ---------------- Private Structs - // ---------------- Private Constructor and Destructor Prototypes // ---------------- Private Function Prototypes @@ -209,6 +233,28 @@ void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self) { } } +void puffs_gif_decoder_constructor(puffs_gif_decoder* self, + uint32_t puffs_version, + uint32_t for_internal_use_only) { + if (!self) { + return; + } + if (puffs_version != PUFFS_VERSION) { + self->private_impl.status = puffs_gif_error_bad_version; + return; + } + if (for_internal_use_only != PUFFS_ALREADY_ZEROED) { + memset(self, 0, sizeof(*self)); + } + self->private_impl.magic = PUFFS_MAGIC; +} + +void puffs_gif_decoder_destructor(puffs_gif_decoder* self) { + if (!self) { + return; + } +} + // ---------------- Function Implementations void puffs_gif_lzw_decoder_set_literal_width(puffs_gif_lzw_decoder* self, diff --git a/gen/h/gif.h b/gen/h/gif.h index aaedf324..cd757621 100644 --- a/gen/h/gif.h +++ b/gen/h/gif.h @@ -73,7 +73,7 @@ bool puffs_gif_status_is_error(puffs_gif_status s); const char* puffs_gif_status_string(puffs_gif_status s); -// ---------------- Public Structs +// ---------------- Structs typedef struct { // Do not access the private_impl's fields directly. There is no API/ABI @@ -93,6 +93,21 @@ typedef struct { } private_impl; } puffs_gif_lzw_decoder; +typedef struct { + // Do not access the private_impl's fields directly. There is no API/ABI + // compatibility or safety guarantee if you do so. Instead, use the + // puffs_gif_decoder_etc functions. + // + // In C++, these fields would be "private", but C does not support that. + // + // It is a struct, not a struct*, so that it can be stack allocated. + struct { + puffs_gif_status status; + uint32_t magic; + puffs_gif_lzw_decoder f_lzw; + } private_impl; +} puffs_gif_decoder; + // ---------------- Public Constructor and Destructor Prototypes // puffs_gif_lzw_decoder_constructor is a constructor function. @@ -106,6 +121,17 @@ void puffs_gif_lzw_decoder_constructor(puffs_gif_lzw_decoder* self, void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self); +// puffs_gif_decoder_constructor is a constructor function. +// +// It should be called before any other puffs_gif_decoder_* function. +// +// Pass PUFFS_VERSION and 0 for puffs_version and for_internal_use_only. +void puffs_gif_decoder_constructor(puffs_gif_decoder* self, + uint32_t puffs_version, + uint32_t for_internal_use_only); + +void puffs_gif_decoder_destructor(puffs_gif_decoder* self); + // ---------------- Public Function Prototypes void puffs_gif_lzw_decoder_set_literal_width(puffs_gif_lzw_decoder* self, diff --git a/lang/ast/ast.go b/lang/ast/ast.go index fb702c76..8f077b68 100644 --- a/lang/ast/ast.go +++ b/lang/ast/ast.go @@ -469,6 +469,12 @@ func (n *TypeExpr) Min() *Expr { return n.lhs.Expr() } func (n *TypeExpr) Max() *Expr { return n.mhs.Expr() } func (n *TypeExpr) Inner() *TypeExpr { return n.rhs.TypeExpr() } +func (n *TypeExpr) Innermost() *TypeExpr { + for ; n != nil && n.Inner() != nil; n = n.Inner() { + } + return n +} + func (n *TypeExpr) IsBool() bool { return n.id0 == 0 && n.id1.Key() == t.KeyBool } diff --git a/lang/ast/sort.go b/lang/ast/sort.go new file mode 100644 index 00000000..dcc49d73 --- /dev/null +++ b/lang/ast/sort.go @@ -0,0 +1,57 @@ +// Use of this source code is governed by a BSD-style license that can be found +// in the LICENSE file. + +package ast + +import ( + t "github.com/google/puffs/lang/token" +) + +func TopologicalSortStructs(ns []*Struct) (sorted []*Struct, ok bool) { + // Algorithm is a depth-first search as per + // https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search + sorted = make([]*Struct, 0, len(ns)) + byName := map[t.ID]*Struct{} + for _, n := range ns { + byName[n.Name()] = n + } + marks := map[*Struct]uint8{} + for _, n := range ns { + if _, ok := marks[n]; !ok { + sorted, ok = tssVisit(sorted, n, byName, marks) + if !ok { + return nil, false + } + } + } + return sorted, true +} + +func tssVisit(dst []*Struct, n *Struct, byName map[t.ID]*Struct, marks map[*Struct]uint8) ([]*Struct, bool) { + const ( + unmarked = 0 + temporary = 1 + permanent = 2 + ) + if marks[n] == temporary { + return nil, false + } + marks[n] = temporary + + for _, f := range n.Fields() { + x := f.Field().XType().Innermost() + if x.Decorator() != 0 { + continue + } + if o := byName[x.Name()]; o != nil { + var ok bool + dst, ok = tssVisit(dst, o, byName, marks) + if !ok { + return nil, false + } + } + } + + marks[n] = permanent + return append(dst, n), true +} diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 58a9d476..4a0dfc71 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -127,9 +127,8 @@ func invert(tm *t.Map, n *a.Expr) (*a.Expr, error) { } func bcheckField(tm *t.Map, n *a.Field) error { - x := n.XType() - for ; x.Inner() != nil; x = x.Inner() { - } + // TODO: check if x is a numeric type? What if n.XType() is "ptr T"? + x := n.XType().Innermost() dv, nMin, nMax := zero, zero, zero if o := n.DefaultValue(); o != nil { dv = o.ConstValue() diff --git a/lang/check/check.go b/lang/check/check.go index 2c85bb52..45ec4769 100644 --- a/lang/check/check.go +++ b/lang/check/check.go @@ -117,6 +117,12 @@ func Check(tm *t.Map, flags Flags, files ...*a.File) (*Checker, error) { for _, phase := range phases { for _, f := range files { + if phase.kind == a.KInvalid { + if err := phase.check(c, nil); err != nil { + return nil, err + } + continue + } for _, n := range f.TopLevelDecls() { if n.Kind() != phase.kind { continue @@ -137,7 +143,9 @@ var phases = [...]struct { }{ {a.KUse, (*Checker).checkUse}, {a.KStatus, (*Checker).checkStatus}, - {a.KStruct, (*Checker).checkStruct}, + {a.KStruct, (*Checker).checkStructDecl}, + {a.KInvalid, (*Checker).checkStructCycles}, + {a.KStruct, (*Checker).checkStructFields}, {a.KFunc, (*Checker).checkFuncSignature}, {a.KFunc, (*Checker).checkFuncContract}, {a.KFunc, (*Checker).checkFuncBody}, @@ -147,9 +155,12 @@ type Checker struct { flags Flags tm *t.Map reasonMap reasonMap - funcs map[t.QID]Func - statuses map[t.ID]Status - structs map[t.ID]Struct + + funcs map[t.QID]Func + statuses map[t.ID]Status + structs map[t.ID]Struct + + unsortedStructs []*a.Struct } func (c *Checker) Funcs() map[t.QID]Func { return c.funcs } @@ -229,15 +240,8 @@ func (c *Checker) checkFields(fields []*a.Node, banPtrTypes bool) error { return nil } -func (c *Checker) checkStruct(node *a.Node) error { +func (c *Checker) checkStructDecl(node *a.Node) error { n := node.Struct() - if err := c.checkFields(n.Fields(), true); err != nil { - return &Error{ - Err: fmt.Errorf("%v in struct %q", err, n.Name().String(c.tm)), - Filename: n.Filename(), - Line: n.Line(), - } - } id := n.Name() if other, ok := c.structs[id]; ok { return &Error{ @@ -252,6 +256,26 @@ func (c *Checker) checkStruct(node *a.Node) error { ID: id, Struct: n, } + c.unsortedStructs = append(c.unsortedStructs, n) + return nil +} + +func (c *Checker) checkStructCycles(_ *a.Node) error { + if _, ok := a.TopologicalSortStructs(c.unsortedStructs); !ok { + return fmt.Errorf("check: cyclical struct definitions") + } + return nil +} + +func (c *Checker) checkStructFields(node *a.Node) error { + n := node.Struct() + if err := c.checkFields(n.Fields(), true); err != nil { + return &Error{ + Err: fmt.Errorf("%v in struct %q", err, n.Name().String(c.tm)), + Filename: n.Filename(), + Line: n.Line(), + } + } n.Node().SetTypeChecked() return nil } diff --git a/lang/check/type.go b/lang/check/type.go index aa1b7eec..092fc8e3 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -667,6 +667,7 @@ func (q *checker) tcheckTypeExpr(n *a.TypeExpr, depth uint32) error { } depth++ +swtch: switch n.Decorator().Key() { case 0: if n.Name().IsNumType() { @@ -684,12 +685,16 @@ func (q *checker) tcheckTypeExpr(n *a.TypeExpr, depth uint32) error { break } if n.Min() != nil || n.Max() != nil { - // TODO: reject. + // TODO: reject. You can only refine numeric types. } if name := n.Name().Key(); name == t.KeyBool || name == t.KeyBuf1 { break } - // TODO: see if name refers to a struct type. + for _, s := range q.c.structs { + if s.ID == n.Name() { + break swtch + } + } return fmt.Errorf("check: %q is not a type", n.Name().String(q.tm)) case t.KeyPtr: diff --git a/std/gif/decode_gif.puffs b/std/gif/decode_gif.puffs new file mode 100644 index 00000000..315dd217 --- /dev/null +++ b/std/gif/decode_gif.puffs @@ -0,0 +1,6 @@ +// Use of this source code is governed by a BSD-style license that can be found +// in the LICENSE file. + +pub struct decoder?( + lzw lzw_decoder, +)