Make the status type a struct, not an int32_t

This commit is contained in:
Nigel Tao committed 2018-08-11 13:27:29 +10:00
1 parent 9c0b2d2f5c
commit 2a32a86a6c
26 files changed
+791 -668

No files matched your search

+2 -2
View File
@@ -25,8 +25,8 @@
// !! INSERT wuffs_base__status__string data.
const char* //
wuffs_base__status__string(wuffs_base__status s) {
uint16_t o = wuffs_base__status__string_offsets[(uint8_t)(s >> 24)];
wuffs_base__status__string(int32_t status_code) {
uint16_t o = wuffs_base__status__string_offsets[(uint8_t)(status_code >> 24)];
return o ? wuffs_base__status__string_data + o : "unknown status";
}
@@ -56,9 +56,9 @@ static inline void wuffs_base__ignore_check_wuffs_version_status(
case n:;
#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \
if (status < 0) { \
if (status.code < 0) { \
goto exit; \
} else if (status == 0) { \
} else if (status.code == 0) { \
goto ok; \
} \
coro_susp_point = n; \
+16 -5
View File
@@ -108,27 +108,38 @@ typedef struct {
//
// Do not manipulate these bits directly; they are private implementation
// details. Use methods such as wuffs_base__status__is_error instead.
typedef int32_t wuffs_base__status;
typedef struct {
int32_t code;
} wuffs_base__status;
// !! INSERT wuffs_base__status names.
// TODO: turn WUFFS_BASE__MAKE_STATUS into a macro, and add a msg field.
static inline wuffs_base__status //
WUFFS_BASE__MAKE_STATUS(int32_t code) {
return ((wuffs_base__status){
.code = code,
});
}
static inline bool //
wuffs_base__status__is_error(wuffs_base__status s) {
return s < 0;
return s.code < 0;
}
static inline bool //
wuffs_base__status__is_ok(wuffs_base__status s) {
return s == 0;
return s.code == 0;
}
static inline bool //
wuffs_base__status__is_suspension(wuffs_base__status s) {
return s > 0;
return s.code > 0;
}
const char* //
wuffs_base__status__string(wuffs_base__status s);
wuffs_base__status__string(int32_t status_code);
// --------
@@ -809,17 +809,18 @@ wuffs_base__pixel_buffer__set_from_slice(wuffs_base__pixel_buffer* b,
wuffs_base__pixel_config* pixcfg,
wuffs_base__slice_u8 pixbuf_memory) {
if (!b) {
return WUFFS_BASE__ERROR_BAD_RECEIVER;
return WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__ERROR_BAD_RECEIVER);
}
*b = ((wuffs_base__pixel_buffer){});
if (!pixcfg) {
return WUFFS_BASE__ERROR_BAD_ARGUMENT;
return WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__ERROR_BAD_ARGUMENT);
}
// TODO: don't assume 1 byte per pixel. Don't assume packed.
uint64_t wh = ((uint64_t)pixcfg->private_impl.width) *
((uint64_t)pixcfg->private_impl.height);
if (wh > pixbuf_memory.len) {
return WUFFS_BASE__ERROR_BAD_ARGUMENT_LENGTH_TOO_SHORT;
return WUFFS_BASE__MAKE_STATUS(
WUFFS_BASE__ERROR_BAD_ARGUMENT_LENGTH_TOO_SHORT);
}
b->private_impl.pixcfg = *pixcfg;
wuffs_base__table_u8* tab = &b->private_impl.planes[0];
@@ -827,7 +828,7 @@ wuffs_base__pixel_buffer__set_from_slice(wuffs_base__pixel_buffer* b,
tab->width = pixcfg->private_impl.width;
tab->height = pixcfg->private_impl.height;
tab->stride = pixcfg->private_impl.width;
return WUFFS_BASE__STATUS_OK;
return WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__STATUS_OK);
}
// The palette argument is ignored unless its length is exactly 1024.
+6 -6
View File
@@ -350,11 +350,11 @@ func (g *gen) writeBuiltinStatus(b *buffer, recv *a.Expr, method t.ID, args []*a
}
switch method {
case t.IDIsError:
b.writes(" < 0")
b.writes(".code < 0")
case t.IDIsOK:
b.writes(" == 0")
b.writes(".code == 0")
case t.IDIsSuspension:
b.writes(" > 0")
b.writes(".code > 0")
default:
return fmt.Errorf("cgen: internal error for writeBuiltinStatus")
}
@@ -430,7 +430,7 @@ func (g *gen) writeBuiltinCallSuspendibles(b *buffer, n *a.Expr, depth uint32) e
g.currFunk.tempW++
b.printf("if (WUFFS_BASE__UNLIKELY(iop_a_src == io1_a_src)) {" +
"status = WUFFS_BASE__SUSPENSION_SHORT_READ; goto suspend; }")
"status = WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__SUSPENSION_SHORT_READ); goto suspend; }")
// TODO: watch for passing an array type to writeCTypeName? In C, an
// array type can decay into a pointer.
@@ -491,7 +491,7 @@ func (g *gen) writeBuiltinCallSuspendibles(b *buffer, n *a.Expr, depth uint32) e
b.printf("%s -= io1_a_src - iop_a_src;\n", scratchName)
b.printf("iop_a_src = io1_a_src;\n")
b.writes("status = WUFFS_BASE__SUSPENSION_SHORT_READ; goto suspend; }\n")
b.writes("status = WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__SUSPENSION_SHORT_READ); goto suspend; }\n")
b.printf("iop_a_src += %s;\n", scratchName)
return nil
}
@@ -500,7 +500,7 @@ func (g *gen) writeBuiltinCallSuspendibles(b *buffer, n *a.Expr, depth uint32) e
switch method.Ident() {
case t.IDWriteU8:
b.writes("if (iop_a_dst == io1_a_dst) {\n" +
"status = WUFFS_BASE__SUSPENSION_SHORT_WRITE; goto suspend; }\n" +
"status = WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__SUSPENSION_SHORT_WRITE); goto suspend; }\n" +
"*iop_a_dst++ = ")
x := n.Args()[0].AsArg().Value()
if err := g.writeExpr(b, x, replaceCallSuspendibles, depth); err != nil {
+12 -12
View File
@@ -378,7 +378,7 @@ func (g *gen) genHeader(b *buffer) error {
}
b.writes("\n")
b.printf("const char* %sstatus__string(wuffs_base__status s);\n\n", g.pkgPrefix)
b.printf("const char* %sstatus__string(int32_t status_code);\n\n", g.pkgPrefix)
b.writes("// ---------------- Public Consts\n\n")
if err := g.forEachConst(b, pubOnly, (*gen).writeConst); err != nil {
@@ -435,18 +435,18 @@ func (g *gen) genImpl(b *buffer) error {
}
}
b.printf("const char* %sstatus__string(wuffs_base__status s) {\n", g.pkgPrefix)
b.printf("const char* %sstatus__string(int32_t status_code) {\n", g.pkgPrefix)
b.printf("uint16_t o;")
b.printf("switch (s & 0x%X) {\n", (1<<base38.MaxBits)-1)
b.printf("case 0: return wuffs_base__status__string(s);\n")
b.printf("switch (status_code & 0x%X) {\n", (1<<base38.MaxBits)-1)
b.printf("case 0: return wuffs_base__status__string(status_code);\n")
b.printf("case %spackageid:\n", g.pkgPrefix)
b.printf("o = %sstatus__string_offsets[(uint8_t)(s >> 24)];\n", g.pkgPrefix)
b.printf("o = %sstatus__string_offsets[(uint8_t)(status_code >> 24)];\n", g.pkgPrefix)
b.printf("if (o) { return %sstatus__string_data + o; } break;\n", g.pkgPrefix)
for _, u := range g.usesList {
// TODO: is path.Base always correct? Should we check
// validName(packageName)?
useePkgName := path.Base(u)
b.printf("case wuffs_%s__packageid: return wuffs_%s__status__string(s);\n", useePkgName, useePkgName)
b.printf("case wuffs_%s__packageid: return wuffs_%s__status__string(status_code);\n", useePkgName, useePkgName)
}
b.printf("}\n")
b.printf("return \"unknown status\";\n")
@@ -923,17 +923,17 @@ func (g *gen) writeInitializerImpl(b *buffer, n *a.Struct) error {
return err
}
b.writes("{\n")
b.writes("if (!self) { return WUFFS_BASE__ERROR_BAD_RECEIVER; }\n")
b.writes("if (!self) { return WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__ERROR_BAD_RECEIVER); }\n")
b.writes("if (sizeof(*self) != sizeof_star_self) {\n")
b.writes("return 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("if (((wuffs_version >> 32) != WUFFS_VERSION_MAJOR) || " +
"(((wuffs_version >> 16) & 0xFFFF) > WUFFS_VERSION_MINOR)) {\n")
b.writes("return WUFFS_BASE__ERROR_BAD_WUFFS_VERSION;\n")
b.writes("return WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__ERROR_BAD_WUFFS_VERSION);\n")
b.writes("}\n")
b.writes("if (self->private_impl.magic != 0) {\n")
b.writes("return WUFFS_BASE__ERROR_CHECK_WUFFS_VERSION_CALLED_TWICE;\n")
b.writes("return WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__ERROR_CHECK_WUFFS_VERSION_CALLED_TWICE);\n")
b.writes("}\n")
// Call any ctors on sub-structs.
@@ -962,12 +962,12 @@ func (g *gen) writeInitializerImpl(b *buffer, n *a.Struct) error {
b.printf("wuffs_base__status z = %s%s__check_wuffs_version("+
"&self->private_impl.%s%s, sizeof(self->private_impl.%s%s), WUFFS_VERSION);\n",
prefix, qid[1].Str(g.tm), fPrefix, f.Name().Str(g.tm), fPrefix, f.Name().Str(g.tm))
b.printf("if (z) { return z; }\n")
b.printf("if (z.code) { return z; }\n")
b.printf("}\n")
}
b.writes("self->private_impl.magic = WUFFS_BASE__MAGIC;\n")
b.writes("return WUFFS_BASE__STATUS_OK;\n")
b.writes("return WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__STATUS_OK);\n")
b.writes("}\n\n")
return nil
}
+9 -8
View File
@@ -22,7 +22,8 @@ const baseBasePublicH = "" +
"nt.com/nothings/stb/master/docs/stb_howto.txt\n#ifdef WUFFS_CONFIG__STATIC_FUNCTIONS\n#define WUFFS_BASE__MAYBE_STATIC static\n#else\n#define WUFFS_BASE__MAYBE_STATIC\n#endif\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define WUFFS_BASE__WARN_UNUSED_RESULT __attribute__((warn_unused_result))\n#else\n#define WUFFS_BASE__WARN_UNUSED_RESULT\n#endif\n\n// wuffs_base__empty_struct is used when a Wuffs function returns an empty\n// struct. In C, if a function f returns void, you can't say \"x = f()\", but in\n// Wuffs, if a function g returns empty, you can say \"y = g()\".\ntypedef struct {\n // private_impl is a placeholder field. It isn't explicitly used, except that\n // without it, the sizeof a struct with no fields can differ across C/C++\n // compilers, and it is undefined behavior in C99. For example, gcc says that\n // the sizeof an empty struct is 0, and g++ says that it is 1. This leads to\n // ABI incompatibility if a Wuffs .c file is processed by one compiler and\n // its .h file with another compiler.\n " +
"//\n // Instead, we explicitly insert an otherwise unused field, so that the\n // sizeof this struct is always 1.\n uint8_t private_impl;\n} wuffs_base__empty_struct;\n\n// wuffs_base__utility is a placeholder receiver type. It enables what Java\n// calls static methods, as opposed to regular methods.\ntypedef struct {\n // private_impl is a placeholder field. It isn't explicitly used, except that\n // without it, the sizeof a struct with no fields can differ across C/C++\n // compilers, and it is undefined behavior in C99. For example, gcc says that\n // the sizeof an empty struct is 0, and g++ says that it is 1. This leads to\n // ABI incompatibility if a Wuffs .c file is processed by one compiler and\n // its .h file with another compiler.\n //\n // Instead, we explicitly insert an otherwise unused field, so that the\n // sizeof this struct is always 1.\n uint8_t private_impl;\n} wuffs_base__utility;\n\n" +
"" +
"// --------\n\n// A status code is either zero (OK), positive (a recoverable suspension or\n// pause in processing) or negative (a non-recoverable error). Its bits:\n// - bit 31 (the sign bit) indicates unrecoverable-ness: an error.\n// - bits 30 .. 24 are a package-namespaced numeric code\n// - bits 23 .. 21 are reserved.\n// - bits 20 .. 0 are the packageid (a namespace) as a base38 value.\n//\n// Do not manipulate these bits directly; they are private implementation\n// details. Use methods such as wuffs_base__status__is_error instead.\ntypedef int32_t wuffs_base__status;\n\n// !! INSERT wuffs_base__status names.\n\nstatic inline bool //\nwuffs_base__status__is_error(wuffs_base__status s) {\n return s < 0;\n}\n\nstatic inline bool //\nwuffs_base__status__is_ok(wuffs_base__status s) {\n return s == 0;\n}\n\nstatic inline bool //\nwuffs_base__status__is_suspension(wuffs_base__status s) {\n return s > 0;\n}\n\nconst char* //\nwuffs_base__status__string(wuffs_base__status s);\n\n" +
"// --------\n\n// A status code is either zero (OK), positive (a recoverable suspension or\n// pause in processing) or negative (a non-recoverable error). Its bits:\n// - bit 31 (the sign bit) indicates unrecoverable-ness: an error.\n// - bits 30 .. 24 are a package-namespaced numeric code\n// - bits 23 .. 21 are reserved.\n// - bits 20 .. 0 are the packageid (a namespace) as a base38 value.\n//\n// Do not manipulate these bits directly; they are private implementation\n// details. Use methods such as wuffs_base__status__is_error instead.\ntypedef struct {\n int32_t code;\n} wuffs_base__status;\n\n// !! INSERT wuffs_base__status names.\n\n// TODO: turn WUFFS_BASE__MAKE_STATUS into a macro, and add a msg field.\n\nstatic inline wuffs_base__status //\nWUFFS_BASE__MAKE_STATUS(int32_t code) {\n return ((wuffs_base__status){\n .code = code,\n });\n}\n\nstatic inline bool //\nwuffs_base__status__is_error(wuffs_base__status s) {\n return s.code < 0;\n}\n\nstatic inline bool //\nwuffs_base__status__is_ok(wuffs_base__status s" +
") {\n return s.code == 0;\n}\n\nstatic inline bool //\nwuffs_base__status__is_suspension(wuffs_base__status s) {\n return s.code > 0;\n}\n\nconst char* //\nwuffs_base__status__string(int32_t status_code);\n\n" +
"" +
"// --------\n\n// Flicks are a unit of time. One flick (frame-tick) is 1 / 705_600_000 of a\n// second. See https://github.com/OculusVR/Flicks\ntypedef int64_t wuffs_base__flicks;\n\n#define WUFFS_BASE__FLICKS_PER_SECOND ((uint64_t)705600000)\n#define WUFFS_BASE__FLICKS_PER_MILLISECOND ((uint64_t)705600)\n\n" +
"" +
@@ -73,7 +74,7 @@ const baseBasePublicH = "" +
const baseBasePrivateH = "" +
"#ifndef WUFFS_INCLUDE_GUARD__BASE_PRIVATE\n#define WUFFS_INCLUDE_GUARD__BASE_PRIVATE\n\n// Copyright 2017 The Wuffs Authors.\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// https://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n#define WUFFS_BASE__IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)\n\nstatic inline void wuffs_base__ignore_check_wuffs_version_status(\n wuffs_base__status z) {}\n\n// WUFFS_BASE__MAGIC is a magic number to check that initializers are called.\n// It's not foolproof, given C doesn't automatically zero me" +
"mory before use,\n// but it should catch 99.99% of cases.\n//\n// Its (non-zero) value is arbitrary, based on md5sum(\"wuffs\").\n#define WUFFS_BASE__MAGIC ((uint32_t)0x3CCB6C71)\n\n// Denote intentional fallthroughs for -Wimplicit-fallthrough.\n//\n// The order matters here. Clang also defines \"__GNUC__\".\n#if defined(__clang__) && __cplusplus >= 201103L\n#define WUFFS_BASE__FALLTHROUGH [[clang::fallthrough]]\n#elif !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 7)\n#define WUFFS_BASE__FALLTHROUGH __attribute__((fallthrough))\n#else\n#define WUFFS_BASE__FALLTHROUGH\n#endif\n\n// Use switch cases for coroutine suspension points, similar to the technique\n// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use trivial macros instead of an explicit assignment and case statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT_0 case 0:;\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; " +
" \\\n WUFFS_BASE__FALLTHROUGH; \\\n case n:;\n\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \\\n if (status < 0) { \\\n goto exit; \\\n } else if (status == 0) { \\\n goto ok; \\\n } \\\n coro_susp_point = n; \\\n goto suspend; \\\n case n:;\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define WUFFS_BASE__LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define WUFFS_BASE__UNLIKELY(expr) (__builtin_expect(!!(expr), 0))\n#else\n#define WUFFS_BASE__LIKELY(expr) (expr)\n#define WUFFS_BASE__UNLIKELY(expr) (expr)\n#endif\n\n// The helpers below are functions, instead of macros, because their arguments\n// can be an expression that we shouldn't evaluat" +
" \\\n WUFFS_BASE__FALLTHROUGH; \\\n case n:;\n\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \\\n if (status.code < 0) { \\\n goto exit; \\\n } else if (status.code == 0) { \\\n goto ok; \\\n } \\\n coro_susp_point = n; \\\n goto suspend; \\\n case n:;\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define WUFFS_BASE__LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define WUFFS_BASE__UNLIKELY(expr) (__builtin_expect(!!(expr), 0))\n#else\n#define WUFFS_BASE__LIKELY(expr) (expr)\n#define WUFFS_BASE__UNLIKELY(expr) (expr)\n#endif\n\n// The helpers below are functions, instead of macros, because their arguments\n// can be an expression that we shouldn't evaluat" +
"e more than once.\n//\n// They are static, so that linking multiple wuffs .o files won't complain about\n// duplicate function definitions.\n//\n// They are explicitly marked inline, even if modern compilers don't use the\n// inline attribute to guide optimizations such as inlining, to avoid the\n// -Wunused-function warning, and we like to compile with -Wall -Werror.\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__return_empty_struct() {\n return ((wuffs_base__empty_struct){});\n}\n\n" +
"" +
"// ---------------- Numeric Types\n\nstatic inline uint8_t //\nwuffs_base__load_u8be(uint8_t* p) {\n return p[0];\n}\n\nstatic inline uint16_t //\nwuffs_base__load_u16be(uint8_t* p) {\n return ((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0);\n}\n\nstatic inline uint16_t //\nwuffs_base__load_u16le(uint8_t* p) {\n return ((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8);\n}\n\nstatic inline uint32_t //\nwuffs_base__load_u24be(uint8_t* p) {\n return ((uint32_t)(p[0]) << 16) | ((uint32_t)(p[1]) << 8) |\n ((uint32_t)(p[2]) << 0);\n}\n\nstatic inline uint32_t //\nwuffs_base__load_u24le(uint8_t* p) {\n return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) |\n ((uint32_t)(p[2]) << 16);\n}\n\nstatic inline uint32_t //\nwuffs_base__load_u32be(uint8_t* p) {\n return ((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) |\n ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0);\n}\n\nstatic inline uint32_t //\nwuffs_base__load_u32le(uint8_t* p) {\n return ((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) |\n ((uin" +
@@ -108,8 +109,8 @@ const baseBasePrivateH = "" +
""
const baseBaseImplC = "" +
"// Copyright 2018 The Wuffs Authors.\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// https://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n// !! INSERT base-public.h.\n\n#ifdef WUFFS_IMPLEMENTATION\n\n// !! INSERT base-private.h.\n\n#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BASE)\n\n// !! INSERT wuffs_base__status__string data.\n\nconst char* //\nwuffs_base__status__string(wuffs_base__status s) {\n uint16_t o = wuffs_base__status__string_offsets[(uint8_t)(s >> 24)];\n return o ? wuffs_base__status__string_data + o : \"unknown status\";\n}\n\n#endif " +
" // !defined(WUFFS_CONFIG__MODULES) ||\n // defined(WUFFS_CONFIG__MODULE__BASE)\n\n#endif // WUFFS_IMPLEMENTATION\n" +
"// Copyright 2018 The Wuffs Authors.\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// https://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n// !! INSERT base-public.h.\n\n#ifdef WUFFS_IMPLEMENTATION\n\n// !! INSERT base-private.h.\n\n#if !defined(WUFFS_CONFIG__MODULES) || defined(WUFFS_CONFIG__MODULE__BASE)\n\n// !! INSERT wuffs_base__status__string data.\n\nconst char* //\nwuffs_base__status__string(int32_t status_code) {\n uint16_t o = wuffs_base__status__string_offsets[(uint8_t)(status_code >> 24)];\n return o ? wuffs_base__status__string_data + o : \"unknown status\";\n}" +
"\n\n#endif // !defined(WUFFS_CONFIG__MODULES) ||\n // defined(WUFFS_CONFIG__MODULE__BASE)\n\n#endif // WUFFS_IMPLEMENTATION\n" +
""
const baseImagePublicH = "" +
@@ -154,8 +155,8 @@ const baseImagePublicH = "" +
"wuffs_base__frame_config__height(this);\n}\n\ninline wuffs_base__flicks //\nwuffs_base__frame_config::duration() {\n return wuffs_base__frame_config__duration(this);\n}\n\ninline bool //\nwuffs_base__frame_config::blend() {\n return wuffs_base__frame_config__blend(this);\n}\n\ninline wuffs_base__animation_disposal //\nwuffs_base__frame_config::disposal() {\n return wuffs_base__frame_config__disposal(this);\n}\n\ninline bool //\nwuffs_base__frame_config::palette_changed() {\n return wuffs_base__frame_config__palette_changed(this);\n}\n\n#endif // __cplusplus\n\n" +
"" +
"// --------\n\ntypedef struct {\n // Do not access the private_impl's fields directly. There is no API/ABI\n // compatibility or safety guarantee if you do so.\n struct {\n wuffs_base__pixel_config pixcfg;\n wuffs_base__table_u8 planes[WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX];\n uint8_t palette[1024];\n // TODO: color spaces.\n } private_impl;\n\n#ifdef __cplusplus\n inline wuffs_base__status set_from_slice(wuffs_base__pixel_config* pixcfg,\n wuffs_base__slice_u8 pixbuf_memory);\n inline void set_palette(wuffs_base__slice_u8 palette);\n inline wuffs_base__pixel_config* pixel_config();\n inline wuffs_base__rect_ie_u32 bounds();\n inline uint32_t width();\n inline uint32_t height();\n inline wuffs_base__table_u8 plane(uint32_t p);\n inline wuffs_base__slice_u8 palette();\n#endif // __cplusplus\n\n} wuffs_base__pixel_buffer;\n\nstatic inline wuffs_base__status //\nwuffs_base__pixel_buffer__set_from_slice(wuffs_base__pixel_buffer* b,\n " +
" wuffs_base__pixel_config* pixcfg,\n wuffs_base__slice_u8 pixbuf_memory) {\n if (!b) {\n return WUFFS_BASE__ERROR_BAD_RECEIVER;\n }\n *b = ((wuffs_base__pixel_buffer){});\n if (!pixcfg) {\n return WUFFS_BASE__ERROR_BAD_ARGUMENT;\n }\n // TODO: don't assume 1 byte per pixel. Don't assume packed.\n uint64_t wh = ((uint64_t)pixcfg->private_impl.width) *\n ((uint64_t)pixcfg->private_impl.height);\n if (wh > pixbuf_memory.len) {\n return WUFFS_BASE__ERROR_BAD_ARGUMENT_LENGTH_TOO_SHORT;\n }\n b->private_impl.pixcfg = *pixcfg;\n wuffs_base__table_u8* tab = &b->private_impl.planes[0];\n tab->ptr = pixbuf_memory.ptr;\n tab->width = pixcfg->private_impl.width;\n tab->height = pixcfg->private_impl.height;\n tab->stride = pixcfg->private_impl.width;\n return WUFFS_BASE__STATUS_OK;\n}\n\n// The palette argument is ignored unless its length is exactly 1024.\nstatic inline void //\nwuffs_base__pixel_buffer__set_palette(wuffs_base__pixel_buffer* b,\n " +
" wuffs_base__slice_u8 palette) {\n if (b && palette.ptr && (palette.len == 1024)) {\n memmove(b->private_impl.palette, palette.ptr, 1024);\n }\n}\n\n// wuffs_base__pixel_config returns the overall configuration for this frame.\nstatic inline wuffs_base__pixel_config* //\nwuffs_base__pixel_buffer__pixel_config(wuffs_base__pixel_buffer* b) {\n return b ? &b->private_impl.pixcfg : NULL;\n}\n\nstatic inline wuffs_base__rect_ie_u32 //\nwuffs_base__pixel_buffer__bounds(wuffs_base__pixel_buffer* b) {\n return b ? wuffs_base__pixel_config__bounds(&b->private_impl.pixcfg)\n : ((wuffs_base__rect_ie_u32){});\n}\n\nstatic inline uint32_t //\nwuffs_base__pixel_buffer__width(wuffs_base__pixel_buffer* b) {\n return b ? wuffs_base__pixel_config__width(&b->private_impl.pixcfg) : 0;\n}\n\nstatic inline uint32_t //\nwuffs_base__pixel_buffer__height(wuffs_base__pixel_buffer* b) {\n return b ? wuffs_base__pixel_config__height(&b->private_impl.pixcfg) : 0;\n}\n\nstatic inline wuffs_base__table_u8 //\nwuffs_base__pixel_" +
"buffer__plane(wuffs_base__pixel_buffer* b, uint32_t p) {\n return (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX))\n ? b->private_impl.planes[p]\n : ((wuffs_base__table_u8){});\n}\n\n// wuffs_base__pixel_buffer__palette returns the palette that the pixel data\n// can index. The backing array is inside b and has length 1024.\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__pixel_buffer__palette(wuffs_base__pixel_buffer* b) {\n return b ? ((wuffs_base__slice_u8){.ptr = b->private_impl.palette,\n .len = 1024})\n : ((wuffs_base__slice_u8){});\n}\n\n#ifdef __cplusplus\n\ninline wuffs_base__status //\nwuffs_base__pixel_buffer::set_from_slice(wuffs_base__pixel_config* pixcfg,\n wuffs_base__slice_u8 pixbuf_memory) {\n return wuffs_base__pixel_buffer__set_from_slice(this, pixcfg, pixbuf_memory);\n}\n\ninline void //\nwuffs_base__pixel_buffer::set_palette(wuffs_base__slice_u8 palette) {\n wuffs_base__pixel_buffer__set_pa" +
"lette(this, palette);\n}\n\ninline wuffs_base__pixel_config* //\nwuffs_base__pixel_buffer::pixel_config() {\n return wuffs_base__pixel_buffer__pixel_config(this);\n}\n\ninline wuffs_base__rect_ie_u32 //\nwuffs_base__pixel_buffer::bounds() {\n return wuffs_base__pixel_buffer__bounds(this);\n}\n\ninline uint32_t //\nwuffs_base__pixel_buffer::width() {\n return wuffs_base__pixel_buffer__width(this);\n}\n\ninline uint32_t //\nwuffs_base__pixel_buffer::height() {\n return wuffs_base__pixel_buffer__height(this);\n}\n\ninline wuffs_base__table_u8 //\nwuffs_base__pixel_buffer::plane(uint32_t p) {\n return wuffs_base__pixel_buffer__plane(this, p);\n}\n\ninline wuffs_base__slice_u8 //\nwuffs_base__pixel_buffer::palette() {\n return wuffs_base__pixel_buffer__palette(this);\n}\n\n#endif // __cplusplus\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n" +
" wuffs_base__pixel_config* pixcfg,\n wuffs_base__slice_u8 pixbuf_memory) {\n if (!b) {\n return WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__ERROR_BAD_RECEIVER);\n }\n *b = ((wuffs_base__pixel_buffer){});\n if (!pixcfg) {\n return WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__ERROR_BAD_ARGUMENT);\n }\n // TODO: don't assume 1 byte per pixel. Don't assume packed.\n uint64_t wh = ((uint64_t)pixcfg->private_impl.width) *\n ((uint64_t)pixcfg->private_impl.height);\n if (wh > pixbuf_memory.len) {\n return WUFFS_BASE__MAKE_STATUS(\n WUFFS_BASE__ERROR_BAD_ARGUMENT_LENGTH_TOO_SHORT);\n }\n b->private_impl.pixcfg = *pixcfg;\n wuffs_base__table_u8* tab = &b->private_impl.planes[0];\n tab->ptr = pixbuf_memory.ptr;\n tab->width = pixcfg->private_impl.width;\n tab->height = pixcfg->private_impl.height;\n tab->stride = pixcfg->private_impl.width;\n return WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__STATUS_OK);\n}\n\n// The palette argument is ignored unless its length is exactly 1024.\ns" +
"tatic inline void //\nwuffs_base__pixel_buffer__set_palette(wuffs_base__pixel_buffer* b,\n wuffs_base__slice_u8 palette) {\n if (b && palette.ptr && (palette.len == 1024)) {\n memmove(b->private_impl.palette, palette.ptr, 1024);\n }\n}\n\n// wuffs_base__pixel_config returns the overall configuration for this frame.\nstatic inline wuffs_base__pixel_config* //\nwuffs_base__pixel_buffer__pixel_config(wuffs_base__pixel_buffer* b) {\n return b ? &b->private_impl.pixcfg : NULL;\n}\n\nstatic inline wuffs_base__rect_ie_u32 //\nwuffs_base__pixel_buffer__bounds(wuffs_base__pixel_buffer* b) {\n return b ? wuffs_base__pixel_config__bounds(&b->private_impl.pixcfg)\n : ((wuffs_base__rect_ie_u32){});\n}\n\nstatic inline uint32_t //\nwuffs_base__pixel_buffer__width(wuffs_base__pixel_buffer* b) {\n return b ? wuffs_base__pixel_config__width(&b->private_impl.pixcfg) : 0;\n}\n\nstatic inline uint32_t //\nwuffs_base__pixel_buffer__height(wuffs_base__pixel_buffer* b) {\n return b ? wuffs_base__pi" +
"xel_config__height(&b->private_impl.pixcfg) : 0;\n}\n\nstatic inline wuffs_base__table_u8 //\nwuffs_base__pixel_buffer__plane(wuffs_base__pixel_buffer* b, uint32_t p) {\n return (b && (p < WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX))\n ? b->private_impl.planes[p]\n : ((wuffs_base__table_u8){});\n}\n\n// wuffs_base__pixel_buffer__palette returns the palette that the pixel data\n// can index. The backing array is inside b and has length 1024.\nstatic inline wuffs_base__slice_u8 //\nwuffs_base__pixel_buffer__palette(wuffs_base__pixel_buffer* b) {\n return b ? ((wuffs_base__slice_u8){.ptr = b->private_impl.palette,\n .len = 1024})\n : ((wuffs_base__slice_u8){});\n}\n\n#ifdef __cplusplus\n\ninline wuffs_base__status //\nwuffs_base__pixel_buffer::set_from_slice(wuffs_base__pixel_config* pixcfg,\n wuffs_base__slice_u8 pixbuf_memory) {\n return wuffs_base__pixel_buffer__set_from_slice(this, pixcfg, pixbuf_memory);\n}\n\ninline void" +
" //\nwuffs_base__pixel_buffer::set_palette(wuffs_base__slice_u8 palette) {\n wuffs_base__pixel_buffer__set_palette(this, palette);\n}\n\ninline wuffs_base__pixel_config* //\nwuffs_base__pixel_buffer::pixel_config() {\n return wuffs_base__pixel_buffer__pixel_config(this);\n}\n\ninline wuffs_base__rect_ie_u32 //\nwuffs_base__pixel_buffer::bounds() {\n return wuffs_base__pixel_buffer__bounds(this);\n}\n\ninline uint32_t //\nwuffs_base__pixel_buffer::width() {\n return wuffs_base__pixel_buffer__width(this);\n}\n\ninline uint32_t //\nwuffs_base__pixel_buffer::height() {\n return wuffs_base__pixel_buffer__height(this);\n}\n\ninline wuffs_base__table_u8 //\nwuffs_base__pixel_buffer::plane(uint32_t p) {\n return wuffs_base__pixel_buffer__plane(this, p);\n}\n\ninline wuffs_base__slice_u8 //\nwuffs_base__pixel_buffer::palette() {\n return wuffs_base__pixel_buffer__palette(this);\n}\n\n#endif // __cplusplus\n\n#ifdef __cplusplus\n} // extern \"C\"\n#endif\n" +
""
+12 -3
View File
@@ -219,7 +219,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, depth u
}
status.name = strings.ToUpper(cName(z.String(), "WUFFS_BASE__"))
}
b.writes(status.name)
b.printf("WUFFS_BASE__MAKE_STATUS(%s)", status.name)
return nil
}
return fmt.Errorf("unrecognized token (0x%X) for writeExprOther", n.Operator())
@@ -237,10 +237,16 @@ func (g *gen) writeExprUnaryOp(b *buffer, n *a.Expr, rp replacementPolicy, depth
}
func (g *gen) writeExprBinaryOp(b *buffer, n *a.Expr, rp replacementPolicy, depth uint32) error {
opName := ""
opName, lhsExtra, rhsExtra := "", "", ""
op := n.Operator()
switch op {
case t.IDXBinaryEqEq, t.IDXBinaryNotEq:
if lhs := n.LHS().AsExpr(); lhs.MType().IsStatus() {
lhsExtra = ".code"
rhsExtra = ".code"
}
case t.IDXBinaryTildeSatPlus, t.IDXBinaryTildeSatMinus:
uBits := uintBits(n.MType().QID())
if uBits == 0 {
@@ -255,8 +261,9 @@ func (g *gen) writeExprBinaryOp(b *buffer, n *a.Expr, rp replacementPolicy, dept
case t.IDXBinaryAs:
return g.writeExprAs(b, n.LHS().AsExpr(), n.RHS().AsTypeExpr(), rp, depth)
}
default:
if opName == "" {
opName = cOpName(op)
if opName == "" {
return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm))
@@ -267,10 +274,12 @@ func (g *gen) writeExprBinaryOp(b *buffer, n *a.Expr, rp replacementPolicy, dept
if err := g.writeExpr(b, n.LHS().AsExpr(), rp, depth); err != nil {
return err
}
b.writes(lhsExtra)
b.writes(opName)
if err := g.writeExpr(b, n.RHS().AsExpr(), rp, depth); err != nil {
return err
}
b.writes(rhsExtra)
b.writeb(')')
return nil
}
+7 -7
View File
@@ -223,16 +223,16 @@ func (g *gen) writeFuncImplHeader(b *buffer) error {
b.writes("if (!self) { return ")
if g.currFunk.suspendible {
b.writes("WUFFS_BASE__ERROR_BAD_RECEIVER")
b.writes("WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__ERROR_BAD_RECEIVER)")
} else if err := g.writeOutParamZeroValue(b, out); err != nil {
return err
}
b.writes(";}")
b.writes("if (self->private_impl.magic != WUFFS_BASE__MAGIC) {" +
"self->private_impl.status = WUFFS_BASE__ERROR_CHECK_WUFFS_VERSION_MISSING; }")
"self->private_impl.status = WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__ERROR_CHECK_WUFFS_VERSION_MISSING); }")
b.writes("if (self->private_impl.status < 0) { return ")
b.writes("if (self->private_impl.status.code < 0) { return ")
if g.currFunk.suspendible {
b.writes("self->private_impl.status")
} else if err := g.writeOutParamZeroValue(b, out); err != nil {
@@ -250,7 +250,7 @@ func (g *gen) writeFuncImplHeader(b *buffer) error {
}
if g.currFunk.suspendible {
b.printf("wuffs_base__status status = WUFFS_BASE__STATUS_OK;\n")
b.printf("wuffs_base__status status = WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__STATUS_OK);\n")
}
b.writes("\n")
@@ -408,12 +408,12 @@ func (g *gen) writeFuncImplArgChecks(b *buffer, n *a.Func) error {
b.writes(") {")
if g.currFunk.suspendible {
if g.currFunk.public {
b.writes("self->private_impl.status = WUFFS_BASE__ERROR_BAD_ARGUMENT;\n")
b.writes("self->private_impl.status = WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__ERROR_BAD_ARGUMENT);\n")
}
b.writes("return WUFFS_BASE__ERROR_BAD_ARGUMENT;\n\n")
b.writes("return WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__ERROR_BAD_ARGUMENT);\n\n")
} else if !n.Receiver().IsZero() {
// TODO: unused code path??
b.writes("self->private_impl.status = WUFFS_BASE__ERROR_BAD_ARGUMENT; return;")
b.writes("self->private_impl.status = WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__ERROR_BAD_ARGUMENT); return;")
} else {
b.printf("return;")
}
+5 -5
View File
@@ -309,7 +309,7 @@ func (g *gen) writeStatementRet(b *buffer, n *a.Ret, depth uint32) error {
b.writes("status = ")
retKeyword := t.IDStatus
if retExpr == nil {
b.writes("WUFFS_BASE__STATUS_OK")
b.writes("WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__STATUS_OK)")
} else {
retKeyword = retExpr.Operator()
// TODO: check that retExpr has no call-suspendibles.
@@ -332,8 +332,8 @@ func (g *gen) writeStatementRet(b *buffer, n *a.Ret, depth uint32) error {
b.writes("goto ok;")
default:
g.currFunk.hasGotoOK = true
b.writes("if (status == 0) { goto ok; } else if (status > 0) { " +
"status = WUFFS_BASE__ERROR_CANNOT_RETURN_A_SUSPENSION; } goto exit;")
b.writes("if (status.code == 0) { goto ok; } else if (status.code > 0) { " +
"status = WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__ERROR_CANNOT_RETURN_A_SUSPENSION); } goto exit;")
}
return nil
}
@@ -528,7 +528,7 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
}
if n.Operator() != t.IDTry {
b.writes("if (status) { goto suspend; }\n")
b.writes("if (status.code) { goto suspend; }\n")
}
return nil
}
@@ -571,7 +571,7 @@ func (g *gen) writeReadUXX(b *buffer, n *a.Expr, preName string, size uint32, en
b.printf("while (true) {")
b.printf("if (WUFFS_BASE__UNLIKELY(iop_%s == io1_%s)) {"+
"status = WUFFS_BASE__SUSPENSION_SHORT_READ; goto suspend; }",
"status = WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__SUSPENSION_SHORT_READ); goto suspend; }",
preName, preName)
b.printf("uint64_t *scratch = &%s;", scratchName)