Make the status type a const char*, not a struct

This commit is contained in:
Nigel Tao committed 2018-08-12 16:05:14 +10:00
1 parent e0311e3bb2
commit b840c80040
24 files changed
+624 -815

No files matched your search

@@ -56,10 +56,10 @@ static inline void wuffs_base__ignore_check_wuffs_version_status(
case n:;
#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \
if (status.code < 0) { \
goto exit; \
} else if (status.code == 0) { \
if (!status) { \
goto ok; \
} else if (*status != '$') { \
goto exit; \
} \
coro_susp_point = n; \
goto suspend; \
+13 -29
View File
@@ -99,45 +99,29 @@ typedef struct {
// --------
// A status code is either zero (OK), positive (a recoverable suspension or
// pause in processing) or negative (a non-recoverable error). Its bits:
// - bit 31 (the sign bit) indicates unrecoverable-ness: an error.
// - bits 30 .. 24 are a package-namespaced numeric code
// - bits 23 .. 21 are reserved.
// - bits 20 .. 0 are the packageid (a namespace) as a base38 value.
// A status is either NULL (meaning OK or no error) or string message. That
// message is human-readable, for programmers, but it is not for end users. It
// is not localized, and does not contain additional contextual information
// such as a source filename.
//
// Do not manipulate these bits directly; they are private implementation
// details. Use methods such as wuffs_base__status__is_error instead.
typedef struct {
int32_t code;
const char* msg;
} wuffs_base__status;
// Status strings are statically allocated and should never be free'd.
typedef const char* 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,
.msg = NULL,
});
static inline bool //
wuffs_base__status__is_error(wuffs_base__status z) {
return z && (*z != '$');
}
static inline bool //
wuffs_base__status__is_error(wuffs_base__status s) {
return s.code < 0;
wuffs_base__status__is_ok(wuffs_base__status z) {
return z == NULL;
}
static inline bool //
wuffs_base__status__is_ok(wuffs_base__status s) {
return s.code == 0;
}
static inline bool //
wuffs_base__status__is_suspension(wuffs_base__status s) {
return s.code > 0;
wuffs_base__status__is_suspension(wuffs_base__status z) {
return z && (*z == '$');
}
const char* //
@@ -809,18 +809,17 @@ 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__MAKE_STATUS(WUFFS_BASE__ERROR_BAD_RECEIVER);
return wuffs_base__error__bad_receiver;
}
*b = ((wuffs_base__pixel_buffer){});
if (!pixcfg) {
return WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__ERROR_BAD_ARGUMENT);
return 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__MAKE_STATUS(
WUFFS_BASE__ERROR_BAD_ARGUMENT_LENGTH_TOO_SHORT);
return wuffs_base__error__bad_argument_length_too_short;
}
b->private_impl.pixcfg = *pixcfg;
wuffs_base__table_u8* tab = &b->private_impl.planes[0];
@@ -828,7 +827,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__MAKE_STATUS(WUFFS_BASE__STATUS_OK);
return NULL;
}
// The palette argument is ignored unless its length is exactly 1024.
+3 -3
View File
@@ -409,7 +409,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__MAKE_STATUS(WUFFS_BASE__SUSPENSION_SHORT_READ); goto suspend; }")
"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.
@@ -470,7 +470,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__MAKE_STATUS(WUFFS_BASE__SUSPENSION_SHORT_READ); goto suspend; }\n")
b.writes("status = wuffs_base__suspension__short_read; goto suspend; }\n")
b.printf("iop_a_src += %s;\n", scratchName)
return nil
}
@@ -479,7 +479,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__MAKE_STATUS(WUFFS_BASE__SUSPENSION_SHORT_WRITE); goto suspend; }\n" +
"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 {
+7 -7
View File
@@ -724,7 +724,7 @@ func (g *gen) writeConstList(b *buffer, n *a.Expr) error {
func (g *gen) writeStruct(b *buffer, n *a.Struct) error {
// For API/ABI compatibility, the very first field in the struct's
// private_impl must be the status code. This lets the initializer callee
// set "self->private_impl.status = WUFFS_BASE__ERROR_BAD_WUFFS_VERSION;"
// set "self->private_impl.status = wuffs_base__error__bad_wuffs_version;"
// regardless of the sizeof(*self) struct reserved by the caller and even
// if the caller and callee were built with different versions.
structName := n.QID().Str(g.tm)
@@ -983,17 +983,17 @@ func (g *gen) writeInitializerImpl(b *buffer, n *a.Struct) error {
return err
}
b.writes("{\n")
b.writes("if (!self) { return WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__ERROR_BAD_RECEIVER); }\n")
b.writes("if (!self) { return wuffs_base__error__bad_receiver; }\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__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__MAKE_STATUS(WUFFS_BASE__ERROR_BAD_WUFFS_VERSION);\n")
b.writes("return wuffs_base__error__bad_wuffs_version;\n")
b.writes("}\n")
b.writes("if (self->private_impl.magic != 0) {\n")
b.writes("return WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__ERROR_CHECK_WUFFS_VERSION_CALLED_TWICE);\n")
b.writes("return wuffs_base__error__check_wuffs_version_called_twice;\n")
b.writes("}\n")
// Call any ctors on sub-structs.
@@ -1022,12 +1022,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.code) { return z; }\n")
b.printf("if (z) { return z; }\n")
b.printf("}\n")
}
b.writes("self->private_impl.magic = WUFFS_BASE__MAGIC;\n")
b.writes("return WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__STATUS_OK);\n")
b.writes("return NULL;\n")
b.writes("}\n\n")
return nil
}
+6 -7
View File
@@ -22,8 +22,7 @@ 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 struct {\n int32_t code;\n const char* msg;\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 .msg = NULL,\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_ba" +
"se__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// A status is either NULL (meaning OK or no error) or string message. That\n// message is human-readable, for programmers, but it is not for end users. It\n// is not localized, and does not contain additional contextual information\n// such as a source filename.\n//\n// Status strings are statically allocated and should never be free'd.\ntypedef const char* wuffs_base__status;\n\n// !! INSERT wuffs_base__status names.\n\nstatic inline bool //\nwuffs_base__status__is_error(wuffs_base__status z) {\n return z && (*z != '$');\n}\n\nstatic inline bool //\nwuffs_base__status__is_ok(wuffs_base__status z) {\n return z == NULL;\n}\n\nstatic inline bool //\nwuffs_base__status__is_suspension(wuffs_base__status z) {\n return z && (*z == '$');\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" +
"" +
@@ -74,7 +73,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.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" +
" \\\n WUFFS_BASE__FALLTHROUGH; \\\n case n:;\n\n#define WUFFS_BASE__COROUTINE_SUSPENSION_POINT_MAYBE_SUSPEND(n) \\\n if (!status) { \\\n goto ok; \\\n } else if (*status != '$') { \\\n goto exit; \\\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" +
@@ -155,8 +154,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__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" +
" 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 NULL;\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_palette(this, pa" +
"lette);\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" +
""
+18 -15
View File
@@ -16,7 +16,6 @@ package cgen
import (
"fmt"
"strings"
"github.com/google/wuffs/lang/builtin"
@@ -210,16 +209,28 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, depth u
return nil
case t.IDError, t.IDStatus, t.IDSuspension:
status := g.statusMap[n.StatusQID()]
if status.name == "" {
if z := g.statusMap[n.StatusQID()]; z.cName != "" {
b.writes(g.pkgPrefix)
if z.keyword == t.IDSuspension {
b.writes("suspension__")
} else {
b.writes("error__")
}
b.writes(z.cName)
} else {
msg, _ := t.Unescape(n.Ident().Str(g.tm))
z := builtin.StatusMap[msg]
if z.Message == "" {
return fmt.Errorf("no status code for %q", msg)
}
status.name = strings.ToUpper(cName(z.String(), "WUFFS_BASE__"))
b.writes("wuffs_base__")
if z.Keyword == t.IDSuspension {
b.writes("suspension__")
} else {
b.writes("error__")
}
b.writes(cName(z.Message, ""))
}
b.printf("WUFFS_BASE__MAKE_STATUS(%s)", status.name)
return nil
}
return fmt.Errorf("unrecognized token (0x%X) for writeExprOther", n.Operator())
@@ -237,15 +248,10 @@ 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, extra := "", ""
opName := ""
op := n.Operator()
switch op {
case t.IDXBinaryEqEq, t.IDXBinaryNotEq:
if lhs := n.LHS().AsExpr(); lhs.MType().IsStatus() {
extra = ".code"
}
case t.IDXBinaryTildeSatPlus, t.IDXBinaryTildeSatMinus:
uBits := uintBits(n.MType().QID())
if uBits == 0 {
@@ -260,9 +266,8 @@ 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)
}
if opName == "" {
default:
opName = cOpName(op)
if opName == "" {
return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm))
@@ -273,12 +278,10 @@ 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(extra)
b.writes(opName)
if err := g.writeExpr(b, n.RHS().AsExpr(), rp, depth); err != nil {
return err
}
b.writes(extra)
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__MAKE_STATUS(WUFFS_BASE__ERROR_BAD_RECEIVER)")
b.writes("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__MAKE_STATUS(WUFFS_BASE__ERROR_CHECK_WUFFS_VERSION_MISSING); }")
"self->private_impl.status = wuffs_base__error__check_wuffs_version_missing; }")
b.writes("if (self->private_impl.status.code < 0) { return ")
b.writes("if (wuffs_base__status__is_error(self->private_impl.status)) { 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__MAKE_STATUS(WUFFS_BASE__STATUS_OK);\n")
b.printf("wuffs_base__status status = NULL;\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__MAKE_STATUS(WUFFS_BASE__ERROR_BAD_ARGUMENT);\n")
b.writes("self->private_impl.status = wuffs_base__error__bad_argument;\n")
}
b.writes("return WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__ERROR_BAD_ARGUMENT);\n\n")
b.writes("return wuffs_base__error__bad_argument;\n\n")
} else if !n.Receiver().IsZero() {
// TODO: unused code path??
b.writes("self->private_impl.status = WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__ERROR_BAD_ARGUMENT); return;")
b.writes("self->private_impl.status = wuffs_base__error__bad_argument; return;")
} else {
b.printf("return;")
}
+6 -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__MAKE_STATUS(WUFFS_BASE__STATUS_OK)")
b.writes("NULL")
} else {
retKeyword = retExpr.Operator()
// TODO: check that retExpr has no call-suspendibles.
@@ -332,8 +332,9 @@ 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.code == 0) { goto ok; } else if (status.code > 0) { " +
"status = WUFFS_BASE__MAKE_STATUS(WUFFS_BASE__ERROR_CANNOT_RETURN_A_SUSPENSION); } goto exit;")
b.writes("if (!status) { goto ok; }" +
"else if (wuffs_base__status__is_suspension(status)) { " +
"status = wuffs_base__error__cannot_return_a_suspension; } goto exit;")
}
return nil
}
@@ -528,7 +529,7 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
}
if n.Operator() != t.IDTry {
b.writes("if (status.code) { goto suspend; }\n")
b.writes("if (status) { goto suspend; }\n")
}
return nil
}
@@ -571,7 +572,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__MAKE_STATUS(WUFFS_BASE__SUSPENSION_SHORT_READ); goto suspend; }",
"status = wuffs_base__suspension__short_read; goto suspend; }",
preName, preName)
b.printf("uint64_t *scratch = &%s;", scratchName)