mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-25 00:55:11 +01:00
Handle structs that contain structs.
This commit is contained in:
1 parent
b3c12e05cf
commit
86c6b79e1a
9 files changed
+263
-96
No files matched your search
+73
-75
@@ -167,6 +167,20 @@ func (g *gen) generate() error {
|
|||||||
return err
|
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"
|
includeGuard := "PUFFS_" + strings.ToUpper(g.pkgName) + "_H"
|
||||||
g.printf("#ifndef %s\n#define %s\n\n", includeGuard, includeGuard)
|
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("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.printf("const char* puffs_%s_status_string(puffs_%s_status s);\n\n", g.pkgName, g.pkgName)
|
||||||
|
|
||||||
g.writes("// ---------------- Public Structs\n\n")
|
g.writes("// ---------------- Structs\n\n")
|
||||||
if err := g.forEachStruct(pubOnly, (*gen).writeStruct); err != nil {
|
for _, n := range structs {
|
||||||
return err
|
if err := g.writeStruct(n); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
g.writes("// ---------------- Public Constructor and Destructor Prototypes\n\n")
|
g.writes("// ---------------- Public Constructor and Destructor Prototypes\n\n")
|
||||||
if err := g.forEachStruct(pubOnly, (*gen).writeCtorPrototypesPub); err != nil {
|
for _, n := range structs {
|
||||||
return err
|
if n.Public() {
|
||||||
|
if err := g.writeCtorPrototypesPub(n); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
g.writes("// ---------------- Public Function Prototypes\n\n")
|
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.printf("}\nreturn \"%s: unknown status\";\n", g.pkgName)
|
||||||
g.writes("}\n\n")
|
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")
|
g.writes("// ---------------- Private Constructor and Destructor Prototypes\n\n")
|
||||||
if err := g.forEachStruct(priOnly, (*gen).writeCtorPrototypesPri); err != nil {
|
for _, n := range structs {
|
||||||
return err
|
if !n.Public() {
|
||||||
|
if err := g.writeCtorPrototypesPri(n); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
g.writes("// ---------------- Private Function Prototypes\n\n")
|
g.writes("// ---------------- Private Function Prototypes\n\n")
|
||||||
@@ -275,8 +294,10 @@ func (g *gen) generate() error {
|
|||||||
g.writes("//\n")
|
g.writes("//\n")
|
||||||
g.writes("// Its (non-zero) value is arbitrary, based on md5sum(\"zeroed\").\n")
|
g.writes("// Its (non-zero) value is arbitrary, based on md5sum(\"zeroed\").\n")
|
||||||
g.writes("#define PUFFS_ALREADY_ZEROED (0x68602EF1U)\n\n")
|
g.writes("#define PUFFS_ALREADY_ZEROED (0x68602EF1U)\n\n")
|
||||||
if err := g.forEachStruct(bothPubPri, (*gen).writeCtorImpls); err != nil {
|
for _, n := range structs {
|
||||||
return err
|
if err := g.writeCtorImpls(n); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
g.writes("// ---------------- Function Implementations\n\n")
|
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
|
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 {
|
func (g *gen) cName(name string) string {
|
||||||
b := []byte(nil)
|
b := []byte(nil)
|
||||||
b = append(b, "puffs_"...)
|
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 {
|
func (g *gen) writeField(n *a.Field, namePrefix string) error {
|
||||||
// TODO: use g.writeCTypeName.
|
x := n.XType()
|
||||||
const maxNPtr = 16
|
for ; x != nil && x.Decorator().Key() == t.KeyOpenBracket; x = x.Inner() {
|
||||||
|
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < nPtr; i++ {
|
if err := g.writeCTypeName(x); err != nil {
|
||||||
g.writeb('*')
|
return err
|
||||||
}
|
}
|
||||||
|
g.writeb(' ')
|
||||||
g.writes(namePrefix)
|
g.writes(namePrefix)
|
||||||
g.writes(n.Name().String(g.tm))
|
g.writes(n.Name().String(g.tm))
|
||||||
|
|
||||||
for x := n.XType(); x != nil; x = x.Inner() {
|
x = n.XType()
|
||||||
if x.Decorator() == t.IDOpenBracket {
|
for ; x != nil && x.Decorator().Key() == t.KeyOpenBracket; x = x.Inner() {
|
||||||
g.writeb('[')
|
g.writeb('[')
|
||||||
g.writes(x.ArrayLength().ConstValue().String())
|
g.writes(x.ArrayLength().ConstValue().String())
|
||||||
g.writeb(']')
|
g.writeb(']')
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
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 {
|
func (g *gen) writeCTypeName(n *a.TypeExpr) error {
|
||||||
if n.Decorator() == 0 {
|
const maxNPtr = 16
|
||||||
if k := n.Name().Key(); k < t.Key(len(cTypeNames)) {
|
|
||||||
if s := cTypeNames[k]; s != "" {
|
numPointers, innermost := 0, n
|
||||||
g.writes(s)
|
for ; innermost != nil && innermost.Inner() != nil; innermost = innermost.Inner() {
|
||||||
return nil
|
// 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.
|
if fallback {
|
||||||
return fmt.Errorf("cannot convert Puffs type %q to C", n.String(g.tm))
|
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{
|
var numTypeBounds = [256][2]*big.Int{
|
||||||
|
|||||||
+49
-3
@@ -73,7 +73,7 @@ bool puffs_gif_status_is_error(puffs_gif_status s);
|
|||||||
|
|
||||||
const char* puffs_gif_status_string(puffs_gif_status s);
|
const char* puffs_gif_status_string(puffs_gif_status s);
|
||||||
|
|
||||||
// ---------------- Public Structs
|
// ---------------- Structs
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
// Do not access the private_impl's fields directly. There is no API/ABI
|
// Do not access the private_impl's fields directly. There is no API/ABI
|
||||||
@@ -93,6 +93,21 @@ typedef struct {
|
|||||||
} private_impl;
|
} private_impl;
|
||||||
} puffs_gif_lzw_decoder;
|
} 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
|
// ---------------- Public Constructor and Destructor Prototypes
|
||||||
|
|
||||||
// puffs_gif_lzw_decoder_constructor is a constructor function.
|
// 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);
|
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
|
// ---------------- Public Function Prototypes
|
||||||
|
|
||||||
void puffs_gif_lzw_decoder_set_literal_width(puffs_gif_lzw_decoder* self,
|
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";
|
return "gif: unknown status";
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------- Private Structs
|
|
||||||
|
|
||||||
// ---------------- Private Constructor and Destructor Prototypes
|
// ---------------- Private Constructor and Destructor Prototypes
|
||||||
|
|
||||||
// ---------------- Private Function 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
|
// ---------------- Function Implementations
|
||||||
|
|
||||||
void puffs_gif_lzw_decoder_set_literal_width(puffs_gif_lzw_decoder* self,
|
void puffs_gif_lzw_decoder_set_literal_width(puffs_gif_lzw_decoder* self,
|
||||||
|
|||||||
+27
-1
@@ -73,7 +73,7 @@ bool puffs_gif_status_is_error(puffs_gif_status s);
|
|||||||
|
|
||||||
const char* puffs_gif_status_string(puffs_gif_status s);
|
const char* puffs_gif_status_string(puffs_gif_status s);
|
||||||
|
|
||||||
// ---------------- Public Structs
|
// ---------------- Structs
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
// Do not access the private_impl's fields directly. There is no API/ABI
|
// Do not access the private_impl's fields directly. There is no API/ABI
|
||||||
@@ -93,6 +93,21 @@ typedef struct {
|
|||||||
} private_impl;
|
} private_impl;
|
||||||
} puffs_gif_lzw_decoder;
|
} 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
|
// ---------------- Public Constructor and Destructor Prototypes
|
||||||
|
|
||||||
// puffs_gif_lzw_decoder_constructor is a constructor function.
|
// 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);
|
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
|
// ---------------- Public Function Prototypes
|
||||||
|
|
||||||
void puffs_gif_lzw_decoder_set_literal_width(puffs_gif_lzw_decoder* self,
|
void puffs_gif_lzw_decoder_set_literal_width(puffs_gif_lzw_decoder* self,
|
||||||
|
|||||||
@@ -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) Max() *Expr { return n.mhs.Expr() }
|
||||||
func (n *TypeExpr) Inner() *TypeExpr { return n.rhs.TypeExpr() }
|
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 {
|
func (n *TypeExpr) IsBool() bool {
|
||||||
return n.id0 == 0 && n.id1.Key() == t.KeyBool
|
return n.id0 == 0 && n.id1.Key() == t.KeyBool
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
@@ -127,9 +127,8 @@ func invert(tm *t.Map, n *a.Expr) (*a.Expr, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func bcheckField(tm *t.Map, n *a.Field) error {
|
func bcheckField(tm *t.Map, n *a.Field) error {
|
||||||
x := n.XType()
|
// TODO: check if x is a numeric type? What if n.XType() is "ptr T"?
|
||||||
for ; x.Inner() != nil; x = x.Inner() {
|
x := n.XType().Innermost()
|
||||||
}
|
|
||||||
dv, nMin, nMax := zero, zero, zero
|
dv, nMin, nMax := zero, zero, zero
|
||||||
if o := n.DefaultValue(); o != nil {
|
if o := n.DefaultValue(); o != nil {
|
||||||
dv = o.ConstValue()
|
dv = o.ConstValue()
|
||||||
|
|||||||
+36
-12
@@ -117,6 +117,12 @@ func Check(tm *t.Map, flags Flags, files ...*a.File) (*Checker, error) {
|
|||||||
|
|
||||||
for _, phase := range phases {
|
for _, phase := range phases {
|
||||||
for _, f := range files {
|
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() {
|
for _, n := range f.TopLevelDecls() {
|
||||||
if n.Kind() != phase.kind {
|
if n.Kind() != phase.kind {
|
||||||
continue
|
continue
|
||||||
@@ -137,7 +143,9 @@ var phases = [...]struct {
|
|||||||
}{
|
}{
|
||||||
{a.KUse, (*Checker).checkUse},
|
{a.KUse, (*Checker).checkUse},
|
||||||
{a.KStatus, (*Checker).checkStatus},
|
{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).checkFuncSignature},
|
||||||
{a.KFunc, (*Checker).checkFuncContract},
|
{a.KFunc, (*Checker).checkFuncContract},
|
||||||
{a.KFunc, (*Checker).checkFuncBody},
|
{a.KFunc, (*Checker).checkFuncBody},
|
||||||
@@ -147,9 +155,12 @@ type Checker struct {
|
|||||||
flags Flags
|
flags Flags
|
||||||
tm *t.Map
|
tm *t.Map
|
||||||
reasonMap reasonMap
|
reasonMap reasonMap
|
||||||
funcs map[t.QID]Func
|
|
||||||
statuses map[t.ID]Status
|
funcs map[t.QID]Func
|
||||||
structs map[t.ID]Struct
|
statuses map[t.ID]Status
|
||||||
|
structs map[t.ID]Struct
|
||||||
|
|
||||||
|
unsortedStructs []*a.Struct
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Checker) Funcs() map[t.QID]Func { return c.funcs }
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Checker) checkStruct(node *a.Node) error {
|
func (c *Checker) checkStructDecl(node *a.Node) error {
|
||||||
n := node.Struct()
|
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()
|
id := n.Name()
|
||||||
if other, ok := c.structs[id]; ok {
|
if other, ok := c.structs[id]; ok {
|
||||||
return &Error{
|
return &Error{
|
||||||
@@ -252,6 +256,26 @@ func (c *Checker) checkStruct(node *a.Node) error {
|
|||||||
ID: id,
|
ID: id,
|
||||||
Struct: n,
|
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()
|
n.Node().SetTypeChecked()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-2
@@ -667,6 +667,7 @@ func (q *checker) tcheckTypeExpr(n *a.TypeExpr, depth uint32) error {
|
|||||||
}
|
}
|
||||||
depth++
|
depth++
|
||||||
|
|
||||||
|
swtch:
|
||||||
switch n.Decorator().Key() {
|
switch n.Decorator().Key() {
|
||||||
case 0:
|
case 0:
|
||||||
if n.Name().IsNumType() {
|
if n.Name().IsNumType() {
|
||||||
@@ -684,12 +685,16 @@ func (q *checker) tcheckTypeExpr(n *a.TypeExpr, depth uint32) error {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
if n.Min() != nil || n.Max() != nil {
|
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 {
|
if name := n.Name().Key(); name == t.KeyBool || name == t.KeyBuf1 {
|
||||||
break
|
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))
|
return fmt.Errorf("check: %q is not a type", n.Name().String(q.tm))
|
||||||
|
|
||||||
case t.KeyPtr:
|
case t.KeyPtr:
|
||||||
|
|||||||
@@ -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,
|
||||||
|
)
|
||||||
Reference in new issue
Block a user