mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 08:52:18 +01:00
Generate C constructors and destructors.
This commit is contained in:
1 parent
a1dd73a58c
commit
ca9fba83c5
5 files changed
+98
-12
No files matched your search
+66
-11
@@ -20,7 +20,7 @@ type Generator struct {
|
|||||||
Extension byte
|
Extension byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Generator) Generate(packageName string, m *t.IDMap, files []*a.File) ([]byte, error) {
|
func (g *Generator) Generate(pkgName string, m *t.IDMap, files []*a.File) ([]byte, error) {
|
||||||
b := &bytes.Buffer{}
|
b := &bytes.Buffer{}
|
||||||
|
|
||||||
// Write preamble.
|
// Write preamble.
|
||||||
@@ -31,24 +31,48 @@ func (g *Generator) Generate(packageName string, m *t.IDMap, files []*a.File) ([
|
|||||||
b.WriteString("#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n")
|
b.WriteString("#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write status codes.
|
b.WriteString("// ---------------- Status Codes\n\n")
|
||||||
b.WriteString("typedef enum {\n")
|
b.WriteString("typedef enum {\n")
|
||||||
fmt.Fprintf(b, "puffs_%s_status_ok = 0,\n", packageName)
|
fmt.Fprintf(b, "puffs_%s_status_ok = 0,\n", pkgName)
|
||||||
fmt.Fprintf(b, "puffs_%s_status_short_dst = -1,\n", packageName)
|
fmt.Fprintf(b, "puffs_%s_status_short_dst = -1,\n", pkgName)
|
||||||
fmt.Fprintf(b, "puffs_%s_status_short_src = -2,\n", packageName)
|
fmt.Fprintf(b, "puffs_%s_status_short_src = -2,\n", pkgName)
|
||||||
fmt.Fprintf(b, "} puffs_%s_status;\n\n", packageName)
|
fmt.Fprintf(b, "} puffs_%s_status;\n\n", pkgName)
|
||||||
|
|
||||||
// Write structs.
|
b.WriteString("// ---------------- Structs\n\n")
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
for _, n := range f.TopLevelDecls() {
|
for _, n := range f.TopLevelDecls() {
|
||||||
if n.Kind() == a.KStruct {
|
if n.Kind() == a.KStruct {
|
||||||
if err := writeStruct(b, packageName, m, n.Struct()); err != nil {
|
if err := writeStruct(b, pkgName, m, n.Struct()); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
b.WriteString("// ---------------- Constructor and Destructor Prototypes\n\n")
|
||||||
|
for _, f := range files {
|
||||||
|
for _, n := range f.TopLevelDecls() {
|
||||||
|
if n.Kind() == a.KStruct {
|
||||||
|
if err := writeCtorPrototypes(b, pkgName, m, n.Struct()); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if g.Extension != 'h' {
|
||||||
|
b.WriteString("// ---------------- Constructor and Destructor Implementations\n\n")
|
||||||
|
for _, f := range files {
|
||||||
|
for _, n := range f.TopLevelDecls() {
|
||||||
|
if n.Kind() == a.KStruct {
|
||||||
|
if err := writeCtorImpls(b, pkgName, m, n.Struct()); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Write postamble.
|
// Write postamble.
|
||||||
if g.Extension == 'h' {
|
if g.Extension == 'h' {
|
||||||
b.WriteString("\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n")
|
b.WriteString("\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n")
|
||||||
@@ -57,17 +81,48 @@ func (g *Generator) Generate(packageName string, m *t.IDMap, files []*a.File) ([
|
|||||||
return format(b)
|
return format(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeStruct(b *bytes.Buffer, packageName string, m *t.IDMap, n *a.Struct) error {
|
func writeStruct(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Struct) error {
|
||||||
|
structName := n.Name().String(m)
|
||||||
fmt.Fprintf(b, "typedef struct {\n")
|
fmt.Fprintf(b, "typedef struct {\n")
|
||||||
if n.Suspendible() {
|
if n.Suspendible() {
|
||||||
fmt.Fprintf(b, "puffs_%s_status status;\n", packageName)
|
fmt.Fprintf(b, "puffs_%s_status status;\n", pkgName)
|
||||||
}
|
}
|
||||||
for _, f := range n.Fields() {
|
for _, f := range n.Fields() {
|
||||||
if err := writeField(b, m, f.Field()); err != nil {
|
if err := writeField(b, m, f.Field()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Fprintf(b, "} puffs_%s_%s;\n", packageName, n.Name().String(m))
|
fmt.Fprintf(b, "} puffs_%s_%s;\n\n", pkgName, structName)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeCtorPrototypes(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Struct) error {
|
||||||
|
if !n.Suspendible() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
structName := n.Name().String(m)
|
||||||
|
for _, ctor := range []string{"constructor", "destructor"} {
|
||||||
|
fmt.Fprintf(b, "void puffs_%s_%s_%s(puffs_%s_%s *self);\n\n",
|
||||||
|
pkgName, structName, ctor, pkgName, structName)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeCtorImpls(b *bytes.Buffer, pkgName string, m *t.IDMap, n *a.Struct) error {
|
||||||
|
if !n.Suspendible() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
structName := n.Name().String(m)
|
||||||
|
for _, ctor := range []string{"constructor", "destructor"} {
|
||||||
|
fmt.Fprintf(b, "void puffs_%s_%s_%s(puffs_%s_%s *self) {\n",
|
||||||
|
pkgName, structName, ctor, pkgName, structName)
|
||||||
|
if ctor == "constructor" {
|
||||||
|
b.WriteString("memset(self, 0, sizeof(*self));\n")
|
||||||
|
// TODO: set any non-zero default values.
|
||||||
|
}
|
||||||
|
// TODO: call any ctor/dtors on sub-structures.
|
||||||
|
b.WriteString("}\n\n")
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,5 +6,5 @@
|
|||||||
package cgen
|
package cgen
|
||||||
|
|
||||||
const preamble = "" +
|
const preamble = "" +
|
||||||
"// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n#include <stdbool.h>\n#include <stdint.h>\n\n// Puffs requires a word size of at least 32 bits because it assumes that\n// converting a u32 to usize will never overflow. For example, the size of a\n// decoded image is often represented, explicitly or implicitly in an image\n// file, as a u32, and it is convenient to compare that to a buffer size.\n#if __WORDSIZE < 32\n#error \"Puffs requires a word size of at least 32 bits\"\n#endif\n" +
|
"// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n#include <stdbool.h>\n#include <stdint.h>\n#include <string.h>\n\n// Puffs requires a word size of at least 32 bits because it assumes that\n// converting a u32 to usize will never overflow. For example, the size of a\n// decoded image is often represented, explicitly or implicitly in an image\n// file, as a u32, and it is convenient to compare that to a buffer size.\n#if __WORDSIZE < 32\n#error \"Puffs requires a word size of at least 32 bits\"\n#endif\n" +
|
||||||
""
|
""
|
||||||
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
// Puffs requires a word size of at least 32 bits because it assumes that
|
// Puffs requires a word size of at least 32 bits because it assumes that
|
||||||
// converting a u32 to usize will never overflow. For example, the size of a
|
// converting a u32 to usize will never overflow. For example, the size of a
|
||||||
|
|||||||
+19
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
// Puffs requires a word size of at least 32 bits because it assumes that
|
// Puffs requires a word size of at least 32 bits because it assumes that
|
||||||
// converting a u32 to usize will never overflow. For example, the size of a
|
// converting a u32 to usize will never overflow. For example, the size of a
|
||||||
@@ -14,12 +15,16 @@
|
|||||||
#error "Puffs requires a word size of at least 32 bits"
|
#error "Puffs requires a word size of at least 32 bits"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// ---------------- Status Codes
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
puffs_gif_status_ok = 0,
|
puffs_gif_status_ok = 0,
|
||||||
puffs_gif_status_short_dst = -1,
|
puffs_gif_status_short_dst = -1,
|
||||||
puffs_gif_status_short_src = -2,
|
puffs_gif_status_short_src = -2,
|
||||||
} puffs_gif_status;
|
} puffs_gif_status;
|
||||||
|
|
||||||
|
// ---------------- Structs
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
puffs_gif_status status;
|
puffs_gif_status status;
|
||||||
uint32_t f_literal_width;
|
uint32_t f_literal_width;
|
||||||
@@ -27,3 +32,17 @@ typedef struct {
|
|||||||
uint8_t f_suffixes[4096];
|
uint8_t f_suffixes[4096];
|
||||||
uint16_t f_prefixes[4096];
|
uint16_t f_prefixes[4096];
|
||||||
} puffs_gif_lzw_decoder;
|
} puffs_gif_lzw_decoder;
|
||||||
|
|
||||||
|
// ---------------- Constructor and Destructor Prototypes
|
||||||
|
|
||||||
|
void puffs_gif_lzw_decoder_constructor(puffs_gif_lzw_decoder* self);
|
||||||
|
|
||||||
|
void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self);
|
||||||
|
|
||||||
|
// ---------------- Constructor and Destructor Implementations
|
||||||
|
|
||||||
|
void puffs_gif_lzw_decoder_constructor(puffs_gif_lzw_decoder* self) {
|
||||||
|
memset(self, 0, sizeof(*self));
|
||||||
|
}
|
||||||
|
|
||||||
|
void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self) {}
|
||||||
+11
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
// Puffs requires a word size of at least 32 bits because it assumes that
|
// Puffs requires a word size of at least 32 bits because it assumes that
|
||||||
// converting a u32 to usize will never overflow. For example, the size of a
|
// converting a u32 to usize will never overflow. For example, the size of a
|
||||||
@@ -18,12 +19,16 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// ---------------- Status Codes
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
puffs_gif_status_ok = 0,
|
puffs_gif_status_ok = 0,
|
||||||
puffs_gif_status_short_dst = -1,
|
puffs_gif_status_short_dst = -1,
|
||||||
puffs_gif_status_short_src = -2,
|
puffs_gif_status_short_src = -2,
|
||||||
} puffs_gif_status;
|
} puffs_gif_status;
|
||||||
|
|
||||||
|
// ---------------- Structs
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
puffs_gif_status status;
|
puffs_gif_status status;
|
||||||
uint32_t f_literal_width;
|
uint32_t f_literal_width;
|
||||||
@@ -32,6 +37,12 @@ typedef struct {
|
|||||||
uint16_t f_prefixes[4096];
|
uint16_t f_prefixes[4096];
|
||||||
} puffs_gif_lzw_decoder;
|
} puffs_gif_lzw_decoder;
|
||||||
|
|
||||||
|
// ---------------- Constructor and Destructor Prototypes
|
||||||
|
|
||||||
|
void puffs_gif_lzw_decoder_constructor(puffs_gif_lzw_decoder* self);
|
||||||
|
|
||||||
|
void puffs_gif_lzw_decoder_destructor(puffs_gif_lzw_decoder* self);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
} // extern "C"
|
} // extern "C"
|
||||||
#endif
|
#endif
|
||||||
Reference in new issue
Block a user