mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 16:52:18 +01:00
Generate C ctor/dtor calls for sub-structs.
This commit is contained in:
1 parent
b79036ff32
commit
64aeba8fcc
3 files changed
+58
-10
No files matched your search
+37
-10
@@ -139,6 +139,8 @@ type gen struct {
|
|||||||
files []*a.File
|
files []*a.File
|
||||||
statusList []status
|
statusList []status
|
||||||
statusMap map[t.ID]status
|
statusMap map[t.ID]status
|
||||||
|
structList []*a.Struct
|
||||||
|
structMap map[t.ID]*a.Struct
|
||||||
perFunc perFunc
|
perFunc perFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -176,19 +178,24 @@ func (g *gen) generate() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
structs, ok := a.TopologicalSortStructs(unsortedStructs)
|
var ok bool
|
||||||
|
g.structList, ok = a.TopologicalSortStructs(unsortedStructs)
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("cyclical struct definitions")
|
return fmt.Errorf("cyclical struct definitions")
|
||||||
}
|
}
|
||||||
|
g.structMap = map[t.ID]*a.Struct{}
|
||||||
|
for _, n := range g.structList {
|
||||||
|
g.structMap[n.Name()] = n
|
||||||
|
}
|
||||||
|
|
||||||
if err := g.genHeader(structs); err != nil {
|
if err := g.genHeader(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
g.writes("// C HEADER ENDS HERE.\n\n")
|
g.writes("// C HEADER ENDS HERE.\n\n")
|
||||||
return g.genImpl(structs)
|
return g.genImpl()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *gen) genHeader(structs []*a.Struct) error {
|
func (g *gen) genHeader() error {
|
||||||
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)
|
||||||
|
|
||||||
@@ -220,14 +227,14 @@ func (g *gen) genHeader(structs []*a.Struct) error {
|
|||||||
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("// ---------------- Structs\n\n")
|
g.writes("// ---------------- Structs\n\n")
|
||||||
for _, n := range structs {
|
for _, n := range g.structList {
|
||||||
if err := g.writeStruct(n); err != nil {
|
if err := g.writeStruct(n); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
g.writes("// ---------------- Public Constructor and Destructor Prototypes\n\n")
|
g.writes("// ---------------- Public Constructor and Destructor Prototypes\n\n")
|
||||||
for _, n := range structs {
|
for _, n := range g.structList {
|
||||||
if n.Public() {
|
if n.Public() {
|
||||||
if err := g.writeCtorPrototype(n); err != nil {
|
if err := g.writeCtorPrototype(n); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -245,7 +252,7 @@ func (g *gen) genHeader(structs []*a.Struct) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *gen) genImpl(structs []*a.Struct) error {
|
func (g *gen) genImpl() error {
|
||||||
g.writes(baseImpl)
|
g.writes(baseImpl)
|
||||||
g.writes("\n")
|
g.writes("\n")
|
||||||
|
|
||||||
@@ -278,7 +285,7 @@ func (g *gen) genImpl(structs []*a.Struct) error {
|
|||||||
g.writes("}\n\n")
|
g.writes("}\n\n")
|
||||||
|
|
||||||
g.writes("// ---------------- Private Constructor and Destructor Prototypes\n\n")
|
g.writes("// ---------------- Private Constructor and Destructor Prototypes\n\n")
|
||||||
for _, n := range structs {
|
for _, n := range g.structList {
|
||||||
if !n.Public() {
|
if !n.Public() {
|
||||||
if err := g.writeCtorPrototype(n); err != nil {
|
if err := g.writeCtorPrototype(n); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -304,7 +311,7 @@ func (g *gen) genImpl(structs []*a.Struct) 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")
|
||||||
for _, n := range structs {
|
for _, n := range g.structList {
|
||||||
if err := g.writeCtorImpl(n); err != nil {
|
if err := g.writeCtorImpl(n); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -478,7 +485,27 @@ func (g *gen) writeCtorImpl(n *a.Struct) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: call any ctor/dtors on sub-structures.
|
// Call any ctor/dtors on sub-structs.
|
||||||
|
for _, f := range n.Fields() {
|
||||||
|
f := f.Field()
|
||||||
|
x := f.XType()
|
||||||
|
if x != x.Innermost() {
|
||||||
|
// TODO: arrays of sub-structs.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if g.structMap[x.Name()] == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if ctor {
|
||||||
|
g.printf("puffs_%s_%s_constructor(&self->private_impl.%s%s,"+
|
||||||
|
"PUFFS_VERSION, PUFFS_ALREADY_ZEROED);\n",
|
||||||
|
g.pkgName, x.Name().String(g.tm), fPrefix, f.Name().String(g.tm))
|
||||||
|
} else {
|
||||||
|
g.printf("puffs_%s_%s_destructor(&self->private_impl.%s%s);\n",
|
||||||
|
g.pkgName, x.Name().String(g.tm), fPrefix, f.Name().String(g.tm))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
g.writes("}\n\n")
|
g.writes("}\n\n")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -247,12 +247,15 @@ void puffs_gif_decoder_constructor(puffs_gif_decoder* self,
|
|||||||
memset(self, 0, sizeof(*self));
|
memset(self, 0, sizeof(*self));
|
||||||
}
|
}
|
||||||
self->private_impl.magic = PUFFS_MAGIC;
|
self->private_impl.magic = PUFFS_MAGIC;
|
||||||
|
puffs_gif_lzw_decoder_constructor(&self->private_impl.f_lzw, PUFFS_VERSION,
|
||||||
|
PUFFS_ALREADY_ZEROED);
|
||||||
}
|
}
|
||||||
|
|
||||||
void puffs_gif_decoder_destructor(puffs_gif_decoder* self) {
|
void puffs_gif_decoder_destructor(puffs_gif_decoder* self) {
|
||||||
if (!self) {
|
if (!self) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
puffs_gif_lzw_decoder_destructor(&self->private_impl.f_lzw);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------------- Function Implementations
|
// ---------------- Function Implementations
|
||||||
|
|||||||
@@ -161,6 +161,23 @@ void test_status_strings() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_sub_struct_constructor() {
|
||||||
|
test_funcname = __func__;
|
||||||
|
puffs_gif_decoder dec;
|
||||||
|
puffs_gif_decoder_constructor(&dec, PUFFS_VERSION, 0);
|
||||||
|
if (dec.private_impl.magic != PUFFS_MAGIC) {
|
||||||
|
FAIL("outer magic: got %u, want %u", dec.private_impl.magic, PUFFS_MAGIC);
|
||||||
|
goto cleanup0;
|
||||||
|
}
|
||||||
|
if (dec.private_impl.f_lzw.private_impl.magic != PUFFS_MAGIC) {
|
||||||
|
FAIL("inner magic: got %u, want %u",
|
||||||
|
dec.private_impl.f_lzw.private_impl.magic, PUFFS_MAGIC);
|
||||||
|
goto cleanup0;
|
||||||
|
}
|
||||||
|
cleanup0:
|
||||||
|
puffs_gif_decoder_destructor(&dec);
|
||||||
|
}
|
||||||
|
|
||||||
// The empty comments forces clang-format to place one element per line.
|
// The empty comments forces clang-format to place one element per line.
|
||||||
test tests[] = {
|
test tests[] = {
|
||||||
test_constructor_not_called, //
|
test_constructor_not_called, //
|
||||||
@@ -171,5 +188,6 @@ test tests[] = {
|
|||||||
test_puffs_version_good, //
|
test_puffs_version_good, //
|
||||||
test_status_is_error, //
|
test_status_is_error, //
|
||||||
test_status_strings, //
|
test_status_strings, //
|
||||||
|
test_sub_struct_constructor, //
|
||||||
NULL, //
|
NULL, //
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user