Clean up more wuffs-c output (before clang-format)

This commit is contained in:
Nigel Tao committed 2020-06-04 23:26:15 +10:00
1 parent 58a48d5a54
commit b2dc84105a
7 files changed
+116 -101

No files matched your search

+35 -35
View File
@@ -59,7 +59,7 @@ func (g *gen) writeBuiltinCall(b *buffer, n *a.Expr, depth uint32) error {
} }
for i, o := range n.Args() { for i, o := range n.Args() {
b.writeb(',') b.writes(", ")
if i == u64ToFlicksIndex { if i == u64ToFlicksIndex {
if o.AsArg().Name().Str(g.tm) != "duration" { if o.AsArg().Name().Str(g.tm) != "duration" {
return errors.New("cgen: internal error: inconsistent frame_config.set argument") return errors.New("cgen: internal error: inconsistent frame_config.set argument")
@@ -106,7 +106,7 @@ func (g *gen) writeBuiltinCall(b *buffer, n *a.Expr, depth uint32) error {
} }
args := n.Args() args := n.Args()
for _, o := range args[:len(args)-1] { for _, o := range args[:len(args)-1] {
b.writeb(',') b.writes(", ")
if err := g.writeExpr(b, o.AsArg().Value(), depth); err != nil { if err := g.writeExpr(b, o.AsArg().Value(), depth); err != nil {
return err return err
} }
@@ -274,7 +274,7 @@ func (g *gen) writeBuiltinIOWriter(b *buffer, recv *a.Expr, method t.ID, args []
b.printf("wuffs_base__io_writer__limited_copy_u32_from_history%s(&%s%s, %s%s, %s%s", b.printf("wuffs_base__io_writer__limited_copy_u32_from_history%s(&%s%s, %s%s, %s%s",
suffix, iopPrefix, name, io0Prefix, name, io2Prefix, name) suffix, iopPrefix, name, io0Prefix, name, io2Prefix, name)
for _, o := range args { for _, o := range args {
b.writeb(',') b.writes(", ")
if err := g.writeExpr(b, o.AsArg().Value(), depth); err != nil { if err := g.writeExpr(b, o.AsArg().Value(), depth); err != nil {
return err return err
} }
@@ -490,7 +490,7 @@ func (g *gen) writeBuiltinSlice(b *buffer, recv *a.Expr, method t.ID, args []*a.
if err := g.writeExpr(b, recv, depth); err != nil { if err := g.writeExpr(b, recv, depth); err != nil {
return err return err
} }
b.writeb(',') b.writes(", ")
return g.writeArgs(b, args, depth) return g.writeArgs(b, args, depth)
case t.IDLength: case t.IDLength:
@@ -507,7 +507,7 @@ func (g *gen) writeBuiltinSlice(b *buffer, recv *a.Expr, method t.ID, args []*a.
if err := g.writeExpr(b, recv, depth); err != nil { if err := g.writeExpr(b, recv, depth); err != nil {
return err return err
} }
b.writeb(',') b.writes(", ")
return g.writeArgs(b, args, depth) return g.writeArgs(b, args, depth)
} }
return errNoSuchBuiltin return errNoSuchBuiltin
@@ -600,7 +600,7 @@ func (g *gen) writeBuiltinTable(b *buffer, recv *a.Expr, method t.ID, args []*a.
if err := g.writeExpr(b, recv, depth); err != nil { if err := g.writeExpr(b, recv, depth); err != nil {
return err return err
} }
b.writeb(',') b.writes(", ")
return g.writeArgs(b, args, depth) return g.writeArgs(b, args, depth)
} }
@@ -619,7 +619,7 @@ func (g *gen) writeBuiltinTable(b *buffer, recv *a.Expr, method t.ID, args []*a.
func (g *gen) writeArgs(b *buffer, args []*a.Node, depth uint32) error { func (g *gen) writeArgs(b *buffer, args []*a.Node, depth uint32) error {
for i, o := range args { for i, o := range args {
if i > 0 { if i > 0 {
b.writeb(',') b.writes(", ")
} }
if err := g.writeExpr(b, o.AsArg().Value(), depth); err != nil { if err := g.writeExpr(b, o.AsArg().Value(), depth); err != nil {
return err return err
@@ -654,8 +654,8 @@ func (g *gen) writeBuiltinQuestionCall(b *buffer, n *a.Expr, depth uint32) error
temp := g.currFunk.tempW temp := g.currFunk.tempW
g.currFunk.tempW++ g.currFunk.tempW++
b.printf("if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {" + b.printf("if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {\n" +
"status = wuffs_base__make_status(wuffs_base__suspension__short_read); goto suspend; }") "status = wuffs_base__make_status(wuffs_base__suspension__short_read);\ngoto suspend;\n}\n")
// TODO: watch for passing an array type to writeCTypeName? In C, an // TODO: watch for passing an array type to writeCTypeName? In C, an
// array type can decay into a pointer. // array type can decay into a pointer.
@@ -671,8 +671,8 @@ func (g *gen) writeBuiltinQuestionCall(b *buffer, n *a.Expr, depth uint32) error
if err := g.writeCoroSuspPoint(b, false); err != nil { if err := g.writeCoroSuspPoint(b, false); err != nil {
return err return err
} }
b.printf("if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {" + b.printf("if (WUFFS_BASE__UNLIKELY(iop_a_src == io2_a_src)) {\n" +
"status = wuffs_base__make_status(wuffs_base__suspension__short_read); goto suspend; }") "status = wuffs_base__make_status(wuffs_base__suspension__short_read);\ngoto suspend;\n}\n")
b.printf("iop_a_src++;\n") b.printf("iop_a_src++;\n")
return nil return nil
} }
@@ -696,7 +696,7 @@ func (g *gen) writeBuiltinQuestionCall(b *buffer, n *a.Expr, depth uint32) error
b.printf("%s -= ((uint64_t)(io2_a_src - iop_a_src));\n", scratchName) b.printf("%s -= ((uint64_t)(io2_a_src - iop_a_src));\n", scratchName)
b.printf("iop_a_src = io2_a_src;\n") b.printf("iop_a_src = io2_a_src;\n")
b.writes("status = wuffs_base__make_status(wuffs_base__suspension__short_read); goto suspend; }\n") b.writes("status = wuffs_base__make_status(wuffs_base__suspension__short_read);\ngoto suspend;\n}\n")
b.printf("iop_a_src += %s;\n", scratchName) b.printf("iop_a_src += %s;\n", scratchName)
return nil return nil
} }
@@ -731,7 +731,7 @@ func (g *gen) writeBuiltinQuestionCall(b *buffer, n *a.Expr, depth uint32) error
return err return err
} }
b.printf("if (iop_a_dst == io2_a_dst) {\n"+ b.printf("if (iop_a_dst == io2_a_dst) {\n"+
"status = wuffs_base__make_status(wuffs_base__suspension__short_write); goto suspend; }\n"+ "status = wuffs_base__make_status(wuffs_base__suspension__short_write);\ngoto suspend;\n}\n"+
"*iop_a_dst++ = ((uint8_t)(%s));\n", scratchName) "*iop_a_dst++ = ((uint8_t)(%s));\n", scratchName)
return nil return nil
} }
@@ -757,15 +757,15 @@ func (g *gen) writeReadUxxAsUyy(b *buffer, n *a.Expr, preName string, xx uint8,
if err := g.writeCTypeName(b, n.MType(), tPrefix, fmt.Sprint(temp)); err != nil { if err := g.writeCTypeName(b, n.MType(), tPrefix, fmt.Sprint(temp)); err != nil {
return err return err
} }
b.writes(";") b.writes(";\n")
g.currFunk.usesScratch = true g.currFunk.usesScratch = true
// TODO: don't hard-code [0], and allow recursive coroutines. // TODO: don't hard-code [0], and allow recursive coroutines.
scratchName := fmt.Sprintf("self->private_data.%s%s[0].scratch", scratchName := fmt.Sprintf("self->private_data.%s%s[0].scratch",
sPrefix, g.currFunk.astFunc.FuncName().Str(g.tm)) sPrefix, g.currFunk.astFunc.FuncName().Str(g.tm))
b.printf("if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= %d)) {", xx/8) b.printf("if (WUFFS_BASE__LIKELY(io2_a_src - iop_a_src >= %d)) {\n", xx/8)
b.printf("%s%d =", tPrefix, temp) b.printf("%s%d = ", tPrefix, temp)
if xx != yy { if xx != yy {
b.printf("((uint%d_t)(", yy) b.printf("((uint%d_t)(", yy)
} }
@@ -773,51 +773,51 @@ func (g *gen) writeReadUxxAsUyy(b *buffer, n *a.Expr, preName string, xx uint8,
if xx != yy { if xx != yy {
b.writes("))") b.writes("))")
} }
b.printf("; iop_a_src += %d;\n", xx/8) b.printf(";\niop_a_src += %d;\n", xx/8)
b.printf("} else {") b.printf("} else {\n")
b.printf("%s = 0;\n", scratchName) b.printf("%s = 0;\n", scratchName)
if err := g.writeCoroSuspPoint(b, false); err != nil { if err := g.writeCoroSuspPoint(b, false); err != nil {
return err return err
} }
b.printf("while (true) {") b.printf("while (true) {\n")
b.printf("if (WUFFS_BASE__UNLIKELY(iop_%s == io2_%s)) {"+ b.printf("if (WUFFS_BASE__UNLIKELY(iop_%s == io2_%s)) {\n"+
"status = wuffs_base__make_status(wuffs_base__suspension__short_read); goto suspend; }", "status = wuffs_base__make_status(wuffs_base__suspension__short_read);\ngoto suspend;\n}\n",
preName, preName) preName, preName)
b.printf("uint64_t *scratch = &%s;", scratchName) b.printf("uint64_t* scratch = &%s;\n", scratchName)
b.printf("uint32_t num_bits_%d = ((uint32_t)(*scratch", temp) b.printf("uint32_t num_bits_%d = ((uint32_t)(*scratch", temp)
switch endianness { switch endianness {
case 'b': case 'b':
b.writes("& 0xFF)); *scratch >>= 8; *scratch <<= 8;") b.writes(" & 0xFF));\n*scratch >>= 8;\n*scratch <<= 8;\n")
b.printf("*scratch |= ((uint64_t)(*%s%s++)) << (56 - num_bits_%d);", b.printf("*scratch |= ((uint64_t)(*%s%s++)) << (56 - num_bits_%d);\n",
iopPrefix, preName, temp) iopPrefix, preName, temp)
case 'l': case 'l':
b.writes(">> 56)); *scratch <<= 8; *scratch >>= 8;") b.writes(" >> 56));\n*scratch <<= 8;\n*scratch >>= 8;\n")
b.printf("*scratch |= ((uint64_t)(*%s%s++)) << num_bits_%d;", b.printf("*scratch |= ((uint64_t)(*%s%s++)) << num_bits_%d;\n",
iopPrefix, preName, temp) iopPrefix, preName, temp)
} }
b.printf("if (num_bits_%d == %d) { %s%d = ((uint%d_t)(", temp, xx-8, tPrefix, temp, yy) b.printf("if (num_bits_%d == %d) {\n%s%d = ((uint%d_t)(", temp, xx-8, tPrefix, temp, yy)
switch endianness { switch endianness {
case 'b': case 'b':
b.printf("*scratch >> %d));", 64-xx) b.printf("*scratch >> %d));\n", 64-xx)
case 'l': case 'l':
b.printf("*scratch));") b.printf("*scratch));\n")
} }
b.printf("break;") b.printf("break;\n")
b.printf("}") b.printf("}\n")
b.printf("num_bits_%d += 8;", temp) b.printf("num_bits_%d += 8;\n", temp)
switch endianness { switch endianness {
case 'b': case 'b':
b.printf("*scratch |= ((uint64_t)(num_bits_%d));", temp) b.printf("*scratch |= ((uint64_t)(num_bits_%d));\n", temp)
case 'l': case 'l':
b.printf("*scratch |= ((uint64_t)(num_bits_%d)) << 56;", temp) b.printf("*scratch |= ((uint64_t)(num_bits_%d)) << 56;\n", temp)
} }
b.writes("}}\n") b.writes("}\n}\n")
return nil return nil
} }
+35 -26
View File
@@ -497,7 +497,7 @@ func insertInterfaceDefinitions(buf *buffer) error {
if err := g.writeFuncSignature(buf, f, wfsCDecl); err != nil { if err := g.writeFuncSignature(buf, f, wfsCDecl); err != nil {
return err return err
} }
buf.writes(" {\n ") buf.writes(" {\n")
if err := writeFuncImplSelfMagicCheck(buf, g.tm, f); err != nil { if err := writeFuncImplSelfMagicCheck(buf, g.tm, f); err != nil {
return err return err
} }
@@ -766,7 +766,7 @@ func (g *gen) genHeader(b *buffer) error {
iQID := impl.AsTypeExpr().QID() iQID := impl.AsTypeExpr().QID()
iName := fmt.Sprintf("wuffs_%s__%s", iQID[0].Str(g.tm), iQID[1].Str(g.tm)) iName := fmt.Sprintf("wuffs_%s__%s", iQID[0].Str(g.tm), iQID[1].Str(g.tm))
b.printf("static inline %s* //\n", iName) b.printf("static inline %s* //\n", iName)
b.printf("%s%s__alloc_as__%s(){\n", g.pkgPrefix, structName, iName) b.printf("%s%s__alloc_as__%s() {\n", g.pkgPrefix, structName, iName)
b.printf("return (%s*)(%s%s__alloc());\n", iName, g.pkgPrefix, structName) b.printf("return (%s*)(%s%s__alloc());\n", iName, g.pkgPrefix, structName)
b.printf("}\n\n") b.printf("}\n\n")
} }
@@ -779,7 +779,7 @@ func (g *gen) genHeader(b *buffer) error {
iQID := impl.AsTypeExpr().QID() iQID := impl.AsTypeExpr().QID()
iName := fmt.Sprintf("wuffs_%s__%s", iQID[0].Str(g.tm), iQID[1].Str(g.tm)) iName := fmt.Sprintf("wuffs_%s__%s", iQID[0].Str(g.tm), iQID[1].Str(g.tm))
b.printf("static inline %s* //\n", iName) b.printf("static inline %s* //\n", iName)
b.printf("%s%s__upcast_as__%s(%s%s* p){\n", b.printf("%s%s__upcast_as__%s(\n %s%s* p) {\n",
g.pkgPrefix, structName, iName, g.pkgPrefix, structName) g.pkgPrefix, structName, iName, g.pkgPrefix, structName)
b.printf("return (%s*)p;\n", iName) b.printf("return (%s*)p;\n", iName)
b.printf("}\n\n") b.printf("}\n\n")
@@ -1017,13 +1017,16 @@ func (g *gen) writeConst(b *buffer, n *a.Const) error {
func (g *gen) writeConstList(b *buffer, n *a.Expr) error { func (g *gen) writeConstList(b *buffer, n *a.Expr) error {
if n.Operator() == t.IDComma { if n.Operator() == t.IDComma {
b.writeb('{') b.writeb('{')
for _, o := range n.Args() { for i, o := range n.Args() {
if i&7 == 0 {
b.writeb('\n')
}
if err := g.writeConstList(b, o.AsExpr()); err != nil { if err := g.writeConstList(b, o.AsExpr()); err != nil {
return err return err
} }
b.writeb(',') b.writes(", ")
} }
b.writeb('}') b.writes("\n}")
} else if cv := n.ConstValue(); cv != nil { } else if cv := n.ConstValue(); cv != nil {
b.writes(cv.String()) b.writes(cv.String())
} else { } else {
@@ -1187,8 +1190,8 @@ func (g *gen) writeCppMethods(b *buffer, n *a.Struct) error {
iQID := impl.AsTypeExpr().QID() iQID := impl.AsTypeExpr().QID()
iName := fmt.Sprintf("wuffs_%s__%s", iQID[0].Str(g.tm), iQID[1].Str(g.tm)) iName := fmt.Sprintf("wuffs_%s__%s", iQID[0].Str(g.tm), iQID[1].Str(g.tm))
b.printf("\nstatic inline %s::unique_ptr //\n", iName) b.printf("\nstatic inline %s::unique_ptr //\n", iName)
b.printf("alloc_as__%s(){\n", iName) b.printf("alloc_as__%s() {\n", iName)
b.printf("return %s::unique_ptr(%s%s__alloc_as__%s(), &free);\n", b.printf("return %s::unique_ptr(\n%s%s__alloc_as__%s(), &free);\n",
iName, g.pkgPrefix, structName, iName) iName, g.pkgPrefix, structName, iName)
b.printf("}\n") b.printf("}\n")
} }
@@ -1208,7 +1211,7 @@ func (g *gen) writeCppMethods(b *buffer, n *a.Struct) error {
b.writes("// can write \"bar->baz(etc)\" instead of \"wuffs_foo__bar__baz(bar, etc)\".\n") b.writes("// can write \"bar->baz(etc)\" instead of \"wuffs_foo__bar__baz(bar, etc)\".\n")
b.printf("%s() = delete;\n", fullStructName) b.printf("%s() = delete;\n", fullStructName)
b.printf("%s(const %s&) = delete;\n", fullStructName, fullStructName) b.printf("%s(const %s&) = delete;\n", fullStructName, fullStructName)
b.printf("%s& operator=(const %s&) = delete;\n", fullStructName, fullStructName) b.printf("%s& operator=(\nconst %s&) = delete;\n", fullStructName, fullStructName)
b.writes("\n") b.writes("\n")
b.writes("// As above, the size of the struct is not part of the public API, and unless\n") b.writes("// As above, the size of the struct is not part of the public API, and unless\n")
b.writes("// WUFFS_IMPLEMENTATION is #define'd, this struct type T should be heap\n") b.writes("// WUFFS_IMPLEMENTATION is #define'd, this struct type T should be heap\n")
@@ -1224,15 +1227,15 @@ func (g *gen) writeCppMethods(b *buffer, n *a.Struct) error {
// The empty // comment makes clang-format place the function name // The empty // comment makes clang-format place the function name
// at the start of a line. // at the start of a line.
b.writes("inline wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT //\n" + b.writes("inline wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT //\n" +
"initialize(size_t sizeof_star_self, uint64_t wuffs_version, uint32_t initialize_flags) {\n") "initialize(\nsize_t sizeof_star_self,\nuint64_t wuffs_version,\nuint32_t initialize_flags) {\n")
b.printf("return %s%s__initialize(this, sizeof_star_self, wuffs_version, initialize_flags);\n}\n\n", b.printf("return %s%s__initialize(\nthis, sizeof_star_self, wuffs_version, initialize_flags);\n}\n\n",
g.pkgPrefix, structName) g.pkgPrefix, structName)
for _, impl := range n.Implements() { for _, impl := range n.Implements() {
iQID := impl.AsTypeExpr().QID() iQID := impl.AsTypeExpr().QID()
iName := fmt.Sprintf("wuffs_%s__%s", iQID[0].Str(g.tm), iQID[1].Str(g.tm)) iName := fmt.Sprintf("wuffs_%s__%s", iQID[0].Str(g.tm), iQID[1].Str(g.tm))
b.printf("inline %s* //\n", iName) b.printf("inline %s* //\n", iName)
b.printf("upcast_as__%s(){\n", iName) b.printf("upcast_as__%s() {\n", iName)
b.printf("return (%s*)this;\n", iName) b.printf("return (%s*)this;\n", iName)
b.printf("}\n\n") b.printf("}\n\n")
} }
@@ -1252,11 +1255,11 @@ func (g *gen) writeCppMethods(b *buffer, n *a.Struct) error {
if err := g.writeFuncSignature(b, f, wfsCppDecl); err != nil { if err := g.writeFuncSignature(b, f, wfsCppDecl); err != nil {
return err return err
} }
b.writes("{\n return ") b.writes(" {\n return ")
b.writes(g.funcCName(f)) b.writes(g.funcCName(f))
b.writes("(this") b.writes("(this")
for _, o := range f.In().Fields() { for _, o := range f.In().Fields() {
b.writeb(',') b.writes(", ")
b.writes(aPrefix) b.writes(aPrefix)
b.writes(o.AsField().Name().Str(g.tm)) b.writes(o.AsField().Name().Str(g.tm))
} }
@@ -1281,7 +1284,7 @@ func (g *gen) writeVTableImpl(b *buffer, n *a.Struct) error {
nQID := n.QID() nQID := n.QID()
for _, impl := range impls { for _, impl := range impls {
iQID := impl.AsTypeExpr().QID() iQID := impl.AsTypeExpr().QID()
b.printf("const wuffs_%s__%s__func_ptrs %s%s__func_ptrs_for__wuffs_%s__%s = {", b.printf("const wuffs_%s__%s__func_ptrs\n%s%s__func_ptrs_for__wuffs_%s__%s = {\n",
iQID[0].Str(g.tm), iQID[1].Str(g.tm), iQID[0].Str(g.tm), iQID[1].Str(g.tm),
g.pkgPrefix, nQID[1].Str(g.tm), g.pkgPrefix, nQID[1].Str(g.tm),
iQID[0].Str(g.tm), iQID[1].Str(g.tm), iQID[0].Str(g.tm), iQID[1].Str(g.tm),
@@ -1310,7 +1313,11 @@ func (g *gen) writeVTableImpl(b *buffer, n *a.Struct) error {
func (g *gen) writeInitializerSignature(b *buffer, n *a.Struct, public bool) error { func (g *gen) writeInitializerSignature(b *buffer, n *a.Struct, public bool) error {
structName := n.QID().Str(g.tm) structName := n.QID().Str(g.tm)
b.printf("wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT //\n"+ b.printf("wuffs_base__status WUFFS_BASE__WARN_UNUSED_RESULT //\n"+
"%s%s__initialize(%s%s *self, size_t sizeof_star_self, uint64_t wuffs_version, uint32_t initialize_flags)", "%s%s__initialize(\n"+
" %s%s* self,\n"+
" size_t sizeof_star_self,\n"+
" uint64_t wuffs_version,\n"+
" uint32_t initialize_flags)",
g.pkgPrefix, structName, g.pkgPrefix, structName) g.pkgPrefix, structName, g.pkgPrefix, structName)
return nil return nil
} }
@@ -1353,12 +1360,14 @@ func (g *gen) writeInitializerImpl(b *buffer, n *a.Struct) error {
return err return err
} }
b.writes("{\n") b.writes("{\n")
b.writes("if (!self) { return wuffs_base__make_status(wuffs_base__error__bad_receiver); }\n") b.writes("if (!self) {\n")
b.writes(" return wuffs_base__make_status(wuffs_base__error__bad_receiver);\n")
b.writes("}\n")
b.writes("if (sizeof(*self) != sizeof_star_self) {\n") b.writes("if (sizeof(*self) != sizeof_star_self) {\n")
b.writes(" return wuffs_base__make_status(wuffs_base__error__bad_sizeof_receiver);\n") b.writes(" return wuffs_base__make_status(wuffs_base__error__bad_sizeof_receiver);\n")
b.writes("}\n") b.writes("}\n")
b.writes("if (((wuffs_version >> 32) != WUFFS_VERSION_MAJOR) || " + b.writes("if (((wuffs_version >> 32) != WUFFS_VERSION_MAJOR) ||\n" +
"(((wuffs_version >> 16) & 0xFFFF) > WUFFS_VERSION_MINOR)) {\n") "(((wuffs_version >> 16) & 0xFFFF) > WUFFS_VERSION_MINOR)) {\n")
b.writes(" return wuffs_base__make_status(wuffs_base__error__bad_wuffs_version);\n") b.writes(" return wuffs_base__make_status(wuffs_base__error__bad_wuffs_version);\n")
b.writes("}\n\n") b.writes("}\n\n")
@@ -1419,9 +1428,9 @@ func (g *gen) writeInitializerImpl(b *buffer, n *a.Struct) error {
for _, impl := range n.Implements() { for _, impl := range n.Implements() {
qid := impl.AsTypeExpr().QID() qid := impl.AsTypeExpr().QID()
iName := fmt.Sprintf("wuffs_%s__%s", qid[0].Str(g.tm), qid[1].Str(g.tm)) iName := fmt.Sprintf("wuffs_%s__%s", qid[0].Str(g.tm), qid[1].Str(g.tm))
b.printf("self->private_impl.vtable_for__%s.vtable_name = "+ b.printf("self->private_impl.vtable_for__%s.vtable_name =\n"+
"%s__vtable_name;\n", iName, iName) "%s__vtable_name;\n", iName, iName)
b.printf("self->private_impl.vtable_for__%s.function_pointers = "+ b.printf("self->private_impl.vtable_for__%s.function_pointers =\n"+
"(const void*)(&%s%s__func_ptrs_for__%s);\n", "(const void*)(&%s%s__func_ptrs_for__%s);\n",
iName, g.pkgPrefix, n.QID().Str(g.tm), iName) iName, g.pkgPrefix, n.QID().Str(g.tm), iName)
} }
@@ -1433,21 +1442,21 @@ func (g *gen) writeInitializerImpl(b *buffer, n *a.Struct) error {
if err := g.writeAllocSignature(b, n); err != nil { if err := g.writeAllocSignature(b, n); err != nil {
return err return err
} }
b.writes("{\n") b.writes(" {\n")
b.printf("%s%s* x = (%s%s*)(calloc(sizeof(%s%s), 1));\n", b.printf("%s%s* x =\n(%s%s*)(calloc(sizeof(%s%s), 1));\n",
g.pkgPrefix, structName, g.pkgPrefix, structName, g.pkgPrefix, structName) g.pkgPrefix, structName, g.pkgPrefix, structName, g.pkgPrefix, structName)
b.writes("if (!x) { return NULL; }\n") b.writes("if (!x) {\nreturn NULL;\n}\n")
b.printf("if (%s%s__initialize(x, sizeof(%s%s), "+ b.printf("if (%s%s__initialize(\nx, sizeof(%s%s), "+
"WUFFS_VERSION, WUFFS_INITIALIZE__ALREADY_ZEROED).repr) {\n", "WUFFS_VERSION, WUFFS_INITIALIZE__ALREADY_ZEROED).repr) {\n",
g.pkgPrefix, structName, g.pkgPrefix, structName) g.pkgPrefix, structName, g.pkgPrefix, structName)
b.writes("free(x); return NULL; }\n") b.writes("free(x);\nreturn NULL;\n}\n")
b.writes("return x;\n") b.writes("return x;\n")
b.writes("}\n\n") b.writes("}\n\n")
if err := g.writeSizeofSignature(b, n); err != nil { if err := g.writeSizeofSignature(b, n); err != nil {
return err return err
} }
b.printf("{ return sizeof(%s%s); }\n\n", g.pkgPrefix, structName) b.printf(" {\nreturn sizeof(%s%s);\n}\n\n", g.pkgPrefix, structName)
} }
return nil return nil
} }
+4 -4
View File
@@ -182,13 +182,13 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, depth uint32) error {
} }
if mhs != nil { if mhs != nil {
b.writeb(',') b.writes(", ")
if err := g.writeExpr(b, mhs, depth); err != nil { if err := g.writeExpr(b, mhs, depth); err != nil {
return err return err
} }
} }
if rhs != nil { if rhs != nil {
b.writeb(',') b.writes(", ")
if err := g.writeExpr(b, rhs, depth); err != nil { if err := g.writeExpr(b, rhs, depth); err != nil {
return err return err
} }
@@ -263,7 +263,7 @@ func (g *gen) writeExprBinaryOp(b *buffer, n *a.Expr, depth uint32) error {
uOp = "sub" uOp = "sub"
} }
b.printf("wuffs_base__u%d__sat_%s", uBits, uOp) b.printf("wuffs_base__u%d__sat_%s", uBits, uOp)
opName = "," opName = ", "
case t.IDXBinaryAs: case t.IDXBinaryAs:
return g.writeExprAs(b, n.LHS().AsExpr(), n.RHS().AsTypeExpr(), depth) return g.writeExprAs(b, n.LHS().AsExpr(), n.RHS().AsTypeExpr(), depth)
@@ -358,7 +358,7 @@ func (g *gen) writeExprUserDefinedCall(b *buffer, n *a.Expr, depth uint32) error
return err return err
} }
if len(n.Args()) > 0 { if len(n.Args()) > 0 {
b.writeb(',') b.writes(", ")
} }
} }
return g.writeArgs(b, n.Args(), depth) return g.writeArgs(b, n.Args(), depth)
+13 -13
View File
@@ -201,7 +201,7 @@ func (g *gen) writeFuncImpl(b *buffer, n *a.Func) error {
if err := g.writeFuncSignature(b, n, wfsCDecl); err != nil { if err := g.writeFuncSignature(b, n, wfsCDecl); err != nil {
return err return err
} }
b.writes("{\n") b.writes(" {\n")
if (len(n.Body()) != 0) || n.Effect().Coroutine() || (n.Out() != nil) { if (len(n.Body()) != 0) || n.Effect().Coroutine() || (n.Out() != nil) {
b.writex(k.bPrologue) b.writex(k.bPrologue)
@@ -212,7 +212,7 @@ func (g *gen) writeFuncImpl(b *buffer, n *a.Func) error {
if n.Effect().Coroutine() { if n.Effect().Coroutine() {
b.writex(k.bBodySuspend) b.writex(k.bBodySuspend)
} else if k.hasGotoOK { } else if k.hasGotoOK {
b.writes("\ngoto ok;ok:\n") // The goto avoids the "unused label" warning. b.writes("\ngoto ok;\nok:\n") // The goto avoids the "unused label" warning.
} }
} }
@@ -307,7 +307,7 @@ func writeFuncImplSelfMagicCheck(b *buffer, tm *t.Map, f *a.Func) error {
returnsStatus := f.Effect().Coroutine() || returnsStatus := f.Effect().Coroutine() ||
((f.Out() != nil) && f.Out().IsStatus()) ((f.Out() != nil) && f.Out().IsStatus())
b.writes("if (!self) {\n return ") b.writes(" if (!self) {\n return ")
if returnsStatus { if returnsStatus {
b.writes("wuffs_base__make_status(wuffs_base__error__bad_receiver)") b.writes("wuffs_base__make_status(wuffs_base__error__bad_receiver)")
} else if err := writeOutParamZeroValue(b, tm, f.Out()); err != nil { } else if err := writeOutParamZeroValue(b, tm, f.Out()); err != nil {
@@ -353,7 +353,7 @@ func (g *gen) writeFuncImplPrologue(b *buffer) error {
// For public coroutines, check that we are not suspended in an active // For public coroutines, check that we are not suspended in an active
// coroutine. // coroutine.
if g.currFunk.astFunc.Effect().Coroutine() { if g.currFunk.astFunc.Effect().Coroutine() {
b.printf("if ((self->private_impl.active_coroutine != 0) && "+ b.printf("if ((self->private_impl.active_coroutine != 0) &&\n"+
"(self->private_impl.active_coroutine != %d)) {\n", g.currFunk.coroID) "(self->private_impl.active_coroutine != %d)) {\n", g.currFunk.coroID)
b.writes("self->private_impl.magic = WUFFS_BASE__DISABLED;\n") b.writes("self->private_impl.magic = WUFFS_BASE__DISABLED;\n")
b.writes("return wuffs_base__make_status(wuffs_base__error__interleaved_coroutine_calls);\n") b.writes("return wuffs_base__make_status(wuffs_base__error__interleaved_coroutine_calls);\n")
@@ -426,12 +426,12 @@ func (g *gen) writeFuncImplBodySuspend(b *buffer) error {
// We've reached the end of the function body. Reset the coroutine // We've reached the end of the function body. Reset the coroutine
// suspension point so that the next call to this function starts at // suspension point so that the next call to this function starts at
// the top. // the top.
b.writes("\ngoto ok;ok:") // The goto avoids the "unused label" warning. b.writes("\ngoto ok;\nok:\n") // The goto avoids the "unused label" warning.
b.printf("self->private_impl.%s%s[0] = 0;\n", b.printf("self->private_impl.%s%s[0] = 0;\n",
pPrefix, g.currFunk.astFunc.FuncName().Str(g.tm)) pPrefix, g.currFunk.astFunc.FuncName().Str(g.tm))
b.writes("goto exit; }\n\n") // Close the coroutine switch. b.writes("goto exit;\n}\n\n") // Close the coroutine switch.
b.writes("goto suspend;suspend:") // The goto avoids the "unused label" warning. b.writes("goto suspend;\nsuspend:\n") // The goto avoids the "unused label" warning.
b.printf("self->private_impl.%s%s[0] = "+ b.printf("self->private_impl.%s%s[0] = "+
"wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;\n", "wuffs_base__status__is_suspension(&status) ? coro_susp_point : 0;\n",
@@ -446,7 +446,7 @@ func (g *gen) writeFuncImplBodySuspend(b *buffer) error {
b.writes("\n") b.writes("\n")
} else if g.currFunk.astFunc.Effect().Coroutine() { } else if g.currFunk.astFunc.Effect().Coroutine() {
b.writes("\ngoto ok;ok:") // The goto avoids the "unused label" warning. b.writes("\ngoto ok;\nok:\n") // The goto avoids the "unused label" warning.
} }
return nil return nil
} }
@@ -456,11 +456,11 @@ func (g *gen) writeFuncImplEpilogue(b *buffer) error {
if g.currFunk.astFunc.Effect().Coroutine() || if g.currFunk.astFunc.Effect().Coroutine() ||
(g.currFunk.returnsStatus && (len(g.currFunk.derivedVars) > 0)) { (g.currFunk.returnsStatus && (len(g.currFunk.derivedVars) > 0)) {
b.writes("goto exit;exit:") // The goto avoids the "unused label" warning. b.writes("goto exit;\nexit:\n") // The goto avoids the "unused label" warning.
if g.currFunk.astFunc.Public() { if g.currFunk.astFunc.Public() {
epilogue = "if (wuffs_base__status__is_error(&status)) { " + epilogue = "if (wuffs_base__status__is_error(&status)) {\n" +
"self->private_impl.magic = WUFFS_BASE__DISABLED; }\n" + "self->private_impl.magic = WUFFS_BASE__DISABLED;\n}\n" +
"return status;\n" "return status;\n"
} else { } else {
epilogue = "return status;\n" epilogue = "return status;\n"
@@ -540,10 +540,10 @@ func (g *gen) writeFuncImplArgChecks(b *buffer, n *a.Func) error {
} }
b.writes(c) b.writes(c)
} }
b.writes(") {") b.writes(") {\n")
b.writes("self->private_impl.magic = WUFFS_BASE__DISABLED;\n") b.writes("self->private_impl.magic = WUFFS_BASE__DISABLED;\n")
if g.currFunk.astFunc.Effect().Coroutine() { if g.currFunk.astFunc.Effect().Coroutine() {
b.writes("return wuffs_base__make_status(wuffs_base__error__bad_argument);\n\n") b.writes("return wuffs_base__make_status(wuffs_base__error__bad_argument);\n")
} else { } else {
// TODO: don't assume that the return type is empty. // TODO: don't assume that the return type is empty.
b.printf("return wuffs_base__make_empty_struct();\n") b.printf("return wuffs_base__make_empty_struct();\n")
+10 -10
View File
@@ -162,7 +162,7 @@ func (g *gen) writeStatementAssign0(b *buffer, op t.ID, lhs *a.Expr, rhs *a.Expr
} }
if op != t.IDEqQuestion && rhs.Effect().Coroutine() { if op != t.IDEqQuestion && rhs.Effect().Coroutine() {
b.writes("if (status.repr) { goto suspend; }\n") b.writes("if (status.repr) {\ngoto suspend;\n}\n")
} }
} }
@@ -285,7 +285,7 @@ func (g *gen) writeStatementIOBind(b *buffer, n *a.IOBind, depth uint32) error {
} }
if n.Keyword() == t.IDIOBind { if n.Keyword() == t.IDIOBind {
b.printf("%s%s = wuffs_base__io_%s__set(&%s%s, &%s%s%s, &%s%s%s, &%s%s%s, &%s%s%s,", b.printf("%s%s = wuffs_base__io_%s__set(&%s%s, &%s%s%s, &%s%s%s, &%s%s%s, &%s%s%s, ",
prefix, name, cTyp, uPrefix, name, iopPrefix, prefix, name, prefix, name, cTyp, uPrefix, name, iopPrefix, prefix, name,
io0Prefix, prefix, name, io1Prefix, prefix, name, io2Prefix, prefix, name) io0Prefix, prefix, name, io1Prefix, prefix, name, io2Prefix, prefix, name)
if err := g.writeExpr(b, n.Arg1(), 0); err != nil { if err := g.writeExpr(b, n.Arg1(), 0); err != nil {
@@ -387,7 +387,7 @@ func (g *gen) writeStatementIterate(b *buffer, n *a.Iterate, depth uint32) error
// TODO: don't assume that the slice is a slice of base.u8. In // TODO: don't assume that the slice is a slice of base.u8. In
// particular, the code gen can be subtle if the slice element type has // particular, the code gen can be subtle if the slice element type has
// zero size, such as the empty struct. // zero size, such as the empty struct.
b.printf("wuffs_base__slice_u8 %sslice_%s =", iPrefix, name) b.printf("wuffs_base__slice_u8 %sslice_%s = ", iPrefix, name)
if err := g.writeExpr(b, o.RHS(), 0); err != nil { if err := g.writeExpr(b, o.RHS(), 0); err != nil {
return err return err
} }
@@ -450,23 +450,23 @@ func (g *gen) writeStatementRet(b *buffer, n *a.Ret, depth uint32) error {
return err return err
} }
} }
b.writes(";") b.writes(";\n")
if n.Keyword() == t.IDYield { if n.Keyword() == t.IDYield {
return g.writeCoroSuspPoint(b, true) return g.writeCoroSuspPoint(b, true)
} }
if n.RetsError() { if n.RetsError() {
b.writes("goto exit;") b.writes("goto exit;\n")
} else if isComplete { } else if isComplete {
g.currFunk.hasGotoOK = true g.currFunk.hasGotoOK = true
b.writes("goto ok;") b.writes("goto ok;\n")
} else { } else {
g.currFunk.hasGotoOK = true g.currFunk.hasGotoOK = true
// TODO: the "goto exit"s can be "goto ok". // TODO: the "goto exit"s can be "goto ok".
b.writes("if (wuffs_base__status__is_error(&status)) { goto exit; }" + b.writes("if (wuffs_base__status__is_error(&status)) {\ngoto exit;\n} " +
"else if (wuffs_base__status__is_suspension(&status)) { " + "else if (wuffs_base__status__is_suspension(&status)) {\n" +
"status = wuffs_base__make_status(wuffs_base__error__cannot_return_a_suspension); goto exit; } goto ok;") "status = wuffs_base__make_status(wuffs_base__error__cannot_return_a_suspension);\ngoto exit;\n}\ngoto ok;\n")
} }
return nil return nil
} }
@@ -507,7 +507,7 @@ func (g *gen) writeStatementRet(b *buffer, n *a.Ret, depth uint32) error {
} }
} }
b.writeb(';') b.writes(";\n")
return nil return nil
} }
+13 -13
View File
@@ -115,27 +115,27 @@ func (g *gen) writeLoadDerivedVar(b *buffer, hack string, prefix string, name t.
} }
if header { if header {
b.printf("%s* %s%s = NULL;", elem, iopPrefix, preName) b.printf("%s* %s%s = NULL;\n", elem, iopPrefix, preName)
b.printf("%s* %s%s WUFFS_BASE__POTENTIALLY_UNUSED = NULL;", elem, io0Prefix, preName) b.printf("%s* %s%s WUFFS_BASE__POTENTIALLY_UNUSED = NULL;\n", elem, io0Prefix, preName)
b.printf("%s* %s%s WUFFS_BASE__POTENTIALLY_UNUSED = NULL;", elem, io1Prefix, preName) b.printf("%s* %s%s WUFFS_BASE__POTENTIALLY_UNUSED = NULL;\n", elem, io1Prefix, preName)
b.printf("%s* %s%s WUFFS_BASE__POTENTIALLY_UNUSED = NULL;", elem, io2Prefix, preName) b.printf("%s* %s%s WUFFS_BASE__POTENTIALLY_UNUSED = NULL;\n", elem, io2Prefix, preName)
} }
b.printf("if (%s) {", preName) b.printf("if (%s) {\n", preName)
if header { if header {
b.printf("%s%s = %s->data.ptr;", io0Prefix, preName, preName) b.printf("%s%s = %s->data.ptr;\n", io0Prefix, preName, preName)
b.printf("%s%s = %s%s + %s->%s;", io1Prefix, preName, io0Prefix, preName, preName, i1) b.printf("%s%s = %s%s + %s->%s;\n", io1Prefix, preName, io0Prefix, preName, preName, i1)
b.printf("%s%s = %s%s;", iopPrefix, preName, io1Prefix, preName) b.printf("%s%s = %s%s;\n", iopPrefix, preName, io1Prefix, preName)
b.printf("%s%s = %s%s + %s->%s;", io2Prefix, preName, io0Prefix, preName, preName, i2) b.printf("%s%s = %s%s + %s->%s;\n", io2Prefix, preName, io0Prefix, preName, preName, i2)
if q := typ.QID()[1]; (q == t.IDIOWriter) || (q == t.IDTokenWriter) { if q := typ.QID()[1]; (q == t.IDIOWriter) || (q == t.IDTokenWriter) {
b.printf("if (%s->meta.closed) {", preName) b.printf("if (%s->meta.closed) {\n", preName)
b.printf("%s%s = %s%s;", io2Prefix, preName, iopPrefix, preName) b.printf("%s%s = %s%s;\n", io2Prefix, preName, iopPrefix, preName)
b.printf("}\n") b.printf("}\n")
} }
} else { } else {
b.printf("%s%s = %s->data.ptr + %s->%s;", iopPrefix, preName, preName, preName, i1) b.printf("%s%s = %s->data.ptr + %s->%s;\n", iopPrefix, preName, preName, preName, i1)
} }
b.printf("}\n") b.printf("}\n")
@@ -173,7 +173,7 @@ func (g *gen) writeSaveDerivedVar(b *buffer, hack string, prefix string, name t.
index = "wi" index = "wi"
} }
b.printf("if (%s) { %s->meta.%s = ((size_t)(%s%s - %s->data.ptr)); }\n", b.printf("if (%s) {\n%s->meta.%s = ((size_t)(%s%s - %s->data.ptr));\n}\n",
preName, preName, index, iopPrefix, preName, preName) preName, preName, index, iopPrefix, preName, preName)
return nil return nil
} }
+6
View File
@@ -13216,6 +13216,7 @@ label__outer__continue:;
label__outer__break:; label__outer__break:;
status = wuffs_base__make_status(NULL); status = wuffs_base__make_status(NULL);
goto ok; goto ok;
goto ok; goto ok;
ok: ok:
goto exit; goto exit;
@@ -17734,6 +17735,7 @@ wuffs_gif__config_decoder__tell_me_more(wuffs_gif__config_decoder* self,
self->private_impl.f_metadata_io_position = 0; self->private_impl.f_metadata_io_position = 0;
status = wuffs_base__make_status(NULL); status = wuffs_base__make_status(NULL);
goto ok; goto ok;
goto ok; goto ok;
ok: ok:
self->private_impl.p_tell_me_more[0] = 0; self->private_impl.p_tell_me_more[0] = 0;
@@ -18166,6 +18168,7 @@ wuffs_gif__config_decoder__decode_frame(
status = wuffs_base__make_status(wuffs_base__error__unsupported_method); status = wuffs_base__make_status(wuffs_base__error__unsupported_method);
goto exit; goto exit;
goto ok; goto ok;
ok: ok:
goto exit; goto exit;
@@ -19559,6 +19562,7 @@ wuffs_gif__decoder__tell_me_more(wuffs_gif__decoder* self,
self->private_impl.f_metadata_io_position = 0; self->private_impl.f_metadata_io_position = 0;
status = wuffs_base__make_status(NULL); status = wuffs_base__make_status(NULL);
goto ok; goto ok;
goto ok; goto ok;
ok: ok:
self->private_impl.p_tell_me_more[0] = 0; self->private_impl.p_tell_me_more[0] = 0;
@@ -24293,6 +24297,7 @@ wuffs_json__decoder__decode_comment(wuffs_json__decoder* self,
} }
status = wuffs_base__make_status(wuffs_json__error__bad_input); status = wuffs_base__make_status(wuffs_json__error__bad_input);
goto exit; goto exit;
goto ok; goto ok;
ok: ok:
self->private_impl.p_decode_comment[0] = 0; self->private_impl.p_decode_comment[0] = 0;
@@ -25380,6 +25385,7 @@ wuffs_wbmp__decoder__tell_me_more(wuffs_wbmp__decoder* self,
status = wuffs_base__make_status(wuffs_base__error__no_more_information); status = wuffs_base__make_status(wuffs_base__error__no_more_information);
goto exit; goto exit;
goto ok; goto ok;
ok: ok:
goto exit; goto exit;