From b79fd65a83e6563393807efbc44be3e2bdb16537 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Tue, 11 Sep 2018 10:59:08 -0700 Subject: WIP. --- upb/bindings/lua/def.c | 64 +- upb/bindings/lua/upb/table.c | 211 ----- upb/bindings/lua/upb/table.lua | 3 - upb/decode.c | 1 + upb/def.c | 1469 +++++++---------------------------- upb/def.h | 1183 +--------------------------- upb/descriptor/descriptor.pb | Bin 6420 -> 0 bytes upb/descriptor/descriptor.proto | 788 ------------------- upb/descriptor/descriptor.upbdefs.c | 927 ---------------------- upb/descriptor/descriptor.upbdefs.h | 608 --------------- upb/descriptor/reader.c | 900 --------------------- upb/descriptor/reader.h | 83 -- upb/encode.c | 1 + upb/handlers.c | 34 +- upb/handlers.h | 1 + upb/msg.c | 2 + upb/msg.h | 5 +- upb/msgfactory.c | 1 + upb/pb/decoder.int.h | 1 - upb/pb/glue.c | 54 -- upb/pb/glue.h | 72 -- upb/structdefs.int.h | 191 ----- upb/table.int.h | 81 +- upb/upb.h | 71 ++ 24 files changed, 431 insertions(+), 6320 deletions(-) delete mode 100644 upb/bindings/lua/upb/table.c delete mode 100644 upb/bindings/lua/upb/table.lua delete mode 100644 upb/descriptor/descriptor.pb delete mode 100644 upb/descriptor/descriptor.proto delete mode 100644 upb/descriptor/descriptor.upbdefs.c delete mode 100644 upb/descriptor/descriptor.upbdefs.h delete mode 100644 upb/descriptor/reader.c delete mode 100644 upb/descriptor/reader.h delete mode 100644 upb/pb/glue.c delete mode 100644 upb/pb/glue.h delete mode 100644 upb/structdefs.int.h (limited to 'upb') diff --git a/upb/bindings/lua/def.c b/upb/bindings/lua/def.c index 194acc3..7b3f612 100644 --- a/upb/bindings/lua/def.c +++ b/upb/bindings/lua/def.c @@ -6,7 +6,6 @@ #include "lauxlib.h" #include "upb/bindings/lua/upb.h" #include "upb/def.h" -#include "upb/pb/glue.h" #define LUPB_ENUMDEF "lupb.enumdef" #define LUPB_FIELDDEF "lupb.fielddef" @@ -1196,37 +1195,27 @@ static int lupb_symtab_gc(lua_State *L) { } static int lupb_symtab_add(lua_State *L) { + size_t len; upb_symtab *s = lupb_symtab_check(L, 1); - int n; - upb_def **defs; - - luaL_checktype(L, 2, LUA_TTABLE); - /* Iterate over table twice. First iteration to count entries and - * check constraints. */ - n = 0; - for (lua_pushnil(L); lua_next(L, 2); lua_pop(L, 1)) { - lupb_def_checkmutable(L, -1); - ++n; - } + const char *str = luaL_checklstring(L, 2, &len); + google_protobuf_FileDescriptorSet *set = + google_protobuf_FileDescriptorSet_parsenew(upb_stringview_make(str, len), + upb_symtab_arena(s)); + size_t i; + upb_array *file_arr; - /* Second iteration to build deflist. - * Allocate list with lua_newuserdata() so it is anchored as a GC root in - * case any Lua functions longjmp(). */ - defs = lua_newuserdata(L, n * sizeof(*defs)); - n = 0; - for (lua_pushnil(L); lua_next(L, 2); lua_pop(L, 1)) { - upb_def *def = lupb_def_checkmutable(L, -1); - defs[n++] = def; + if (!set) { + luaL_argerror(L, 2, "failed to parse descriptor"); } - CHK(upb_symtab_add(s, defs, n, NULL, &status)); - return 0; -} + file_arr = (upb_array*)google_protobuf_FileDescriptorSet_file(set); + for (i = 0; i < upb_array_size(file_arr); i++) { + upb_msgval entry = upb_array_get(file_arr, i); + google_protobuf_FileDescriptorProto *file = + (void *)upb_msgval_getmsg(entry); + CHK(upb_symtab_addfile(s, file, &status)); + } -static int lupb_symtab_addfile(lua_State *L) { - upb_symtab *s = lupb_symtab_check(L, 1); - upb_filedef *f = lupb_filedef_checkmutable(L, 2); - CHK(upb_symtab_addfile(s, f, &status)); return 0; } @@ -1262,7 +1251,6 @@ static int lupb_symtab_defs(lua_State *L) { static const struct luaL_Reg lupb_symtab_m[] = { {"add", lupb_symtab_add}, - {"add_file", lupb_symtab_addfile}, {"defs", lupb_symtab_defs}, {"lookup", lupb_symtab_lookup}, {NULL, NULL} @@ -1290,25 +1278,6 @@ static int lupb_freeze(lua_State *L) { return 0; } -/* This is a *temporary* API that will be removed once pending refactorings are - * complete (it does not belong here in core because it depends on both - * the descriptor.proto schema and the protobuf binary format. */ -static int lupb_loaddescriptor(lua_State *L) { - size_t len; - const char *str = luaL_checklstring(L, 1, &len); - size_t i; - upb_filedef **files = NULL; - CHK(files = upb_loaddescriptor(str, len, &files, &status)); - - lua_newtable(L); - for (i = 1; *files; i++, files++) { - lupb_filedef_pushnewrapper(L, *files, &files); - lua_rawseti(L, -2, i); - } - - return 1; -} - static void lupb_setfieldi(lua_State *L, const char *field, int i) { lua_pushinteger(L, i); lua_setfield(L, -2, field); @@ -1322,7 +1291,6 @@ static const struct luaL_Reg lupbdef_toplevel_m[] = { {"OneofDef", lupb_oneofdef_new}, {"SymbolTable", lupb_symtab_new}, {"freeze", lupb_freeze}, - {"load_descriptor", lupb_loaddescriptor}, {NULL, NULL} }; diff --git a/upb/bindings/lua/upb/table.c b/upb/bindings/lua/upb/table.c deleted file mode 100644 index 79120c7..0000000 --- a/upb/bindings/lua/upb/table.c +++ /dev/null @@ -1,211 +0,0 @@ -/* -** require("upb.table") -- a Lua extension for accessing upb_table -** -** This is an internal-only interface and exists for the sole purpose of -** writing a C code generator in Lua that can dump a upb_table as static C -** initializers. This lets us use Lua for convenient string manipulation while -** saving us from re-implementing the upb_table hash function and hash table -** layout / collision strategy in Lua. -** -** Since this is used only as part of the toolchain (and not part of the -** runtime) we do not hold this module to the same stringent requirements as -** the main Lua modules (for example that misbehaving Lua programs cannot -** crash the interpreter). -*/ - -#include -#include -#include -#include - -#include "lauxlib.h" -#include "upb/bindings/lua/upb.h" -#include "upb/def.h" -#include "upb/structdefs.int.h" -#include "upb/table.int.h" - -static void lupbtable_setnum(lua_State *L, int tab, const char *key, - lua_Number val) { - lua_pushnumber(L, val); - lua_setfield(L, tab - 1, key); -} - -static void lupbtable_pushval(lua_State *L, upb_tabval val, upb_ctype_t ctype) { - switch (ctype) { - case UPB_CTYPE_INT32: - lua_pushnumber(L, val.val); - break; - case UPB_CTYPE_PTR: - lupb_def_pushwrapper(L, (void*)val.val, NULL); - break; - case UPB_CTYPE_CSTR: - lua_pushstring(L, (const char*)val.val); - break; - default: - luaL_error(L, "Unexpected type: %d", ctype); - } -} - -/* Sets a few fields common to both hash table entries and arrays. */ -static void lupbtable_setmetafields(lua_State *L, int ctype, const void *ptr) { - /* We tack this onto every entry so we know it even if the entries - * don't stay with the table. */ - lua_pushnumber(L, ctype); - lua_setfield(L, -2, "valtype"); - - /* Set this to facilitate linking. */ - lua_pushlightuserdata(L, (void*)ptr); - lua_setfield(L, -2, "ptr"); -} - -static void lupbtable_pushent(lua_State *L, const upb_tabent *e, - bool inttab, int ctype) { - lua_newtable(L); - if (!upb_tabent_isempty(e)) { - if (inttab) { - lua_pushnumber(L, e->key); - } else { - uint32_t len; - const char *str = upb_tabstr(e->key, &len); - lua_pushlstring(L, str, len); - } - lua_setfield(L, -2, "key"); - lupbtable_pushval(L, e->val, ctype); - lua_setfield(L, -2, "value"); - } - lua_pushlightuserdata(L, (void*)e->next); - lua_setfield(L, -2, "next"); - lupbtable_setmetafields(L, ctype, e); -} - -/* Dumps the shared part of upb_table into a Lua table. */ -static void lupbtable_pushtable(lua_State *L, const upb_table *t, bool inttab) { - size_t i; - - lua_newtable(L); - lupbtable_setnum(L, -1, "count", t->count); - lupbtable_setnum(L, -1, "mask", t->mask); - lupbtable_setnum(L, -1, "ctype", t->ctype); - lupbtable_setnum(L, -1, "size_lg2", t->size_lg2); - - lua_newtable(L); - for (i = 0; i < upb_table_size(t); i++) { - lupbtable_pushent(L, &t->entries[i], inttab, t->ctype); - lua_rawseti(L, -2, i + 1); - } - lua_setfield(L, -2, "entries"); -} - -/* Dumps a upb_inttable to a Lua table. */ -static void lupbtable_pushinttable(lua_State *L, const upb_inttable *t) { - size_t i; - - lupbtable_pushtable(L, &t->t, true); - lupbtable_setnum(L, -1, "array_size", t->array_size); - lupbtable_setnum(L, -1, "array_count", t->array_count); - - lua_newtable(L); - for (i = 0; i < t->array_size; i++) { - lua_newtable(L); - if (upb_arrhas(t->array[i])) { - lupbtable_pushval(L, t->array[i], t->t.ctype); - lua_setfield(L, -2, "val"); - } - lupbtable_setmetafields(L, t->t.ctype, &t->array[i]); - lua_rawseti(L, -2, i + 1); - } - lua_setfield(L, -2, "array"); -} - -static void lupbtable_pushstrtable(lua_State *L, const upb_strtable *t) { - lupbtable_pushtable(L, &t->t, false); -} - -static int lupbtable_msgdef_itof(lua_State *L) { - const upb_msgdef *m = lupb_msgdef_check(L, 1); - lupbtable_pushinttable(L, &m->itof); - return 1; -} - -static int lupbtable_msgdef_ntof(lua_State *L) { - const upb_msgdef *m = lupb_msgdef_check(L, 1); - lupbtable_pushstrtable(L, &m->ntof); - return 1; -} - -static int lupbtable_enumdef_iton(lua_State *L) { - const upb_enumdef *e = lupb_enumdef_check(L, 1); - lupbtable_pushinttable(L, &e->iton); - return 1; -} - -static int lupbtable_enumdef_ntoi(lua_State *L) { - const upb_enumdef *e = lupb_enumdef_check(L, 1); - lupbtable_pushstrtable(L, &e->ntoi); - return 1; -} - -static int lupbtable_symtab_symtab(lua_State *L) { - const upb_symtab *s = lupb_symtab_check(L, 1); - lupbtable_pushstrtable(L, &s->symtab); - return 1; -} - -static void lupbtable_setfieldi(lua_State *L, const char *field, int i) { - lua_pushnumber(L, i); - lua_setfield(L, -2, field); -} - -/* These aren't from the table, but they access other internal-only - * definitions. */ -static int lupb_fielddef_selectorbase(lua_State *L) { - const upb_fielddef *f = lupb_fielddef_check(L, 1); - if (!upb_fielddef_isfrozen(f)) - luaL_error(L, "_selectorbase is only defined for frozen fielddefs"); - lua_pushinteger(L, f->selector_base); - return 1; -} - -static int lupb_msgdef_selectorcount(lua_State *L) { - const upb_msgdef *m = lupb_msgdef_check(L, 1); - lua_pushinteger(L, m->selector_count); - return 1; -} - -static int lupb_msgdef_submsgfieldcount(lua_State *L) { - const upb_msgdef *m = lupb_msgdef_check(L, 1); - lua_pushinteger(L, m->submsg_field_count); - return 1; -} - -static const struct luaL_Reg lupbtable_toplevel_m[] = { - {"msgdef_itof", lupbtable_msgdef_itof}, - {"msgdef_ntof", lupbtable_msgdef_ntof}, - {"enumdef_iton", lupbtable_enumdef_iton}, - {"enumdef_ntoi", lupbtable_enumdef_ntoi}, - {"symtab_symtab", lupbtable_symtab_symtab}, - - {"msgdef_selector_count", lupb_msgdef_selectorcount}, - {"msgdef_submsg_field_count", lupb_msgdef_submsgfieldcount}, - - {"fielddef_selector_base", lupb_fielddef_selectorbase}, - - {NULL, NULL} -}; - -int luaopen_upb_table_c(lua_State *L) { - static char module_key; - if (lupb_openlib(L, &module_key, "upb.table", lupbtable_toplevel_m)) { - return 1; - } - - /* We define these here because they are not public. */ - lupbtable_setfieldi(L, "CTYPE_PTR", UPB_CTYPE_PTR); - lupbtable_setfieldi(L, "CTYPE_CSTR", UPB_CTYPE_CSTR); - lupbtable_setfieldi(L, "CTYPE_INT32", UPB_CTYPE_INT32); - - lua_pushlightuserdata(L, NULL); - lua_setfield(L, -2, "NULL"); - - return 1; /* Return a single Lua value, the package table created above. */ -} diff --git a/upb/bindings/lua/upb/table.lua b/upb/bindings/lua/upb/table.lua deleted file mode 100644 index ad8b066..0000000 --- a/upb/bindings/lua/upb/table.lua +++ /dev/null @@ -1,3 +0,0 @@ - -require "upb" -return require "upb.table_c" diff --git a/upb/decode.c b/upb/decode.c index fc0e644..0f62326 100644 --- a/upb/decode.c +++ b/upb/decode.c @@ -1,4 +1,5 @@ +#include #include "upb/upb.h" #include "upb/decode.h" #include "upb/structs.int.h" diff --git a/upb/def.c b/upb/def.c index 0c154e6..2e53cf7 100644 --- a/upb/def.c +++ b/upb/def.c @@ -4,9 +4,82 @@ #include #include #include -#include "upb/structdefs.int.h" #include "upb/handlers.h" +struct upb_fielddef { + union { + int64_t sint; + uint64_t uint; + double dbl; + float flt; + void *bytes; + } defaultval; + const upb_msgdef *msgdef; + union { + const upb_msgdef *msgdef; + const upb_msgdef *enumdef; + char *unresolved_name; + } sub; + union { + const upb_oneofdef *oneof; + } + uint32_t number_; + uint32_t selector_base; /* Used to index into a upb::Handlers table. */ + bool is_extension_; + bool lazy_; + bool packed_; + upb_descriptortype_t type_; + upb_label_t label_; +}; + +struct upb_msgdef { + upb_filedef *file; + const char *full_name; + uint32_t selector_count; + uint32_t submsg_field_count; + + upb_fielddef **fields; + upb_oneofdef **oneofs; + + /* Tables for looking up fields by number and name. Built lazily. */ + upb_inttable *itof; /* int to field */ + upb_strtable *ntof; /* name to field/oneof */ + + /* Is this a map-entry message? */ + bool map_entry; + + /* TODO(haberman): proper extension ranges (there can be multiple). */ +}; + +struct upb_enumdef { + upb_strtable ntoi; + upb_inttable iton; + int32_t defaultval; +}; + +struct upb_oneofdef { + const upb_msgdef *parent; + const char *name; + upb_strtable *ntof; + upb_inttable *itof; +}; + +struct upb_symtab { + upb_arena arena; + upb_strtable symtab; +}; + +struct upb_filedef { + const char *name; + const char *package; + const char *phpprefix; + const char *phpnamespace; + upb_syntax_t syntax; + + upb_inttable defs; + upb_inttable deps; +}; + typedef struct { size_t len; char str[1]; /* Null-terminated string data follows. */ @@ -66,186 +139,24 @@ static bool upb_isident(const char *str, size_t len, bool full, upb_status *s) { return !start; } -static bool upb_isoneof(const upb_refcounted *def) { - return def->vtbl == &upb_oneofdef_vtbl; -} - -static bool upb_isfield(const upb_refcounted *def) { - return def->vtbl == &upb_fielddef_vtbl; -} - -static const upb_oneofdef *upb_trygetoneof(const upb_refcounted *def) { - return upb_isoneof(def) ? (const upb_oneofdef*)def : NULL; -} - -static const upb_fielddef *upb_trygetfield(const upb_refcounted *def) { - return upb_isfield(def) ? (const upb_fielddef*)def : NULL; -} - - -/* upb_def ********************************************************************/ - -upb_deftype_t upb_def_type(const upb_def *d) { return d->type; } - -const char *upb_def_fullname(const upb_def *d) { return d->fullname; } - -const char *upb_def_name(const upb_def *d) { - const char *p; - - if (d->fullname == NULL) { - return NULL; - } else if ((p = strrchr(d->fullname, '.')) == NULL) { - /* No '.' in the name, return the full string. */ - return d->fullname; - } else { - /* Return one past the last '.'. */ - return p + 1; - } -} - -bool upb_def_setfullname(upb_def *def, const char *fullname, upb_status *s) { - UPB_ASSERT(!upb_def_isfrozen(def)); - if (!upb_isident(fullname, strlen(fullname), true, s)) { - return false; - } - - fullname = upb_gstrdup(fullname); - if (!fullname) { - upb_upberr_setoom(s); - return false; - } - - upb_gfree((void*)def->fullname); - def->fullname = fullname; +static bool upb_isoneof(const void *def) { + UPB_UNUSED(def); return true; } -const upb_filedef *upb_def_file(const upb_def *d) { return d->file; } - -static bool upb_def_init(upb_def *def, upb_deftype_t type, - const struct upb_refcounted_vtbl *vtbl, - const void *owner) { - if (!upb_refcounted_init(upb_def_upcast_mutable(def), vtbl, owner)) return false; - def->type = type; - def->fullname = NULL; - def->came_from_user = false; - def->file = NULL; +static bool upb_isfield(const void *def) { + UPB_UNUSED(def); return true; } -static void upb_def_uninit(upb_def *def) { - upb_gfree((void*)def->fullname); -} - -static const char *msgdef_name(const upb_msgdef *m) { - const char *name = upb_def_fullname(upb_msgdef_upcast(m)); - return name ? name : "(anonymous)"; +static const upb_oneofdef *upb_trygetoneof(const void *def) { + return upb_isoneof(def) ? (const upb_oneofdef*)def : NULL; } -static bool upb_validate_field(upb_fielddef *f, upb_status *s) { - if (upb_fielddef_name(f) == NULL || upb_fielddef_number(f) == 0) { - upb_status_seterrmsg(s, "fielddef must have name and number set"); - return false; - } - - if (!f->type_is_set_) { - upb_status_seterrmsg(s, "fielddef type was not initialized"); - return false; - } - - if (upb_fielddef_lazy(f) && - upb_fielddef_descriptortype(f) != UPB_DESCRIPTOR_TYPE_MESSAGE) { - upb_status_seterrmsg(s, - "only length-delimited submessage fields may be lazy"); - return false; - } - - if (upb_fielddef_hassubdef(f)) { - const upb_def *subdef; - - if (f->subdef_is_symbolic) { - upb_status_seterrf(s, "field '%s.%s' has not been resolved", - msgdef_name(f->msg.def), upb_fielddef_name(f)); - return false; - } - - subdef = upb_fielddef_subdef(f); - if (subdef == NULL) { - upb_status_seterrf(s, "field %s.%s is missing required subdef", - msgdef_name(f->msg.def), upb_fielddef_name(f)); - return false; - } - - if (!upb_def_isfrozen(subdef) && !subdef->came_from_user) { - upb_status_seterrf(s, - "subdef of field %s.%s is not frozen or being frozen", - msgdef_name(f->msg.def), upb_fielddef_name(f)); - return false; - } - } - - if (upb_fielddef_type(f) == UPB_TYPE_ENUM) { - bool has_default_name = upb_fielddef_enumhasdefaultstr(f); - bool has_default_number = upb_fielddef_enumhasdefaultint32(f); - - /* Previously verified by upb_validate_enumdef(). */ - UPB_ASSERT(upb_enumdef_numvals(upb_fielddef_enumsubdef(f)) > 0); - - /* We've already validated that we have an associated enumdef and that it - * has at least one member, so at least one of these should be true. - * Because if the user didn't set anything, we'll pick up the enum's - * default, but if the user *did* set something we should at least pick up - * the one they set (int32 or string). */ - UPB_ASSERT(has_default_name || has_default_number); - - if (!has_default_name) { - upb_status_seterrf(s, - "enum default for field %s.%s (%d) is not in the enum", - msgdef_name(f->msg.def), upb_fielddef_name(f), - upb_fielddef_defaultint32(f)); - return false; - } - - if (!has_default_number) { - upb_status_seterrf(s, - "enum default for field %s.%s (%s) is not in the enum", - msgdef_name(f->msg.def), upb_fielddef_name(f), - upb_fielddef_defaultstr(f, NULL)); - return false; - } - - /* Lift the effective numeric default into the field's default slot, in case - * we were only getting it "by reference" from the enumdef. */ - upb_fielddef_setdefaultint32(f, upb_fielddef_defaultint32(f)); - } - - /* Ensure that MapEntry submessages only appear as repeated fields, not - * optional/required (singular) fields. */ - if (upb_fielddef_type(f) == UPB_TYPE_MESSAGE && - upb_fielddef_msgsubdef(f) != NULL) { - const upb_msgdef *subdef = upb_fielddef_msgsubdef(f); - if (upb_msgdef_mapentry(subdef) && !upb_fielddef_isseq(f)) { - upb_status_seterrf(s, - "Field %s refers to mapentry message but is not " - "a repeated field", - upb_fielddef_name(f) ? upb_fielddef_name(f) : - "(unnamed)"); - return false; - } - } - - return true; +static const upb_fielddef *upb_trygetfield(const void *def) { + return upb_isfield(def) ? (const upb_fielddef*)def : NULL; } -static bool upb_validate_enumdef(const upb_enumdef *e, upb_status *s) { - if (upb_enumdef_numvals(e) == 0) { - upb_status_seterrf(s, "enum %s has no members (must have at least one)", - upb_enumdef_fullname(e)); - return false; - } - - return true; -} /* All submessage fields are lower than all other fields. * Secondly, fields are increasing in order. */ @@ -291,11 +202,7 @@ static bool assign_msg_indices(upb_msgdef *m, upb_status *s) { !upb_msg_field_done(&j); upb_msg_field_next(&j), i++) { upb_fielddef *f = upb_msg_iter_field(&j); - UPB_ASSERT(f->msg.def == m); - if (!upb_validate_field(f, s)) { - upb_gfree(fields); - return false; - } + UPB_ASSERT(f->msgdef == m); if (upb_fielddef_issubmsg(f)) { m->submsg_field_count++; } @@ -365,6 +272,7 @@ static bool assign_msg_indices(upb_msgdef *m, upb_status *s) { return true; } +#if 0 bool _upb_def_validate(upb_def *const*defs, size_t n, upb_status *s) { size_t i; @@ -416,73 +324,20 @@ err: UPB_ASSERT(!(s && upb_ok(s))); return false; } - -bool upb_def_freeze(upb_def *const* defs, size_t n, upb_status *s) { - /* Def graph contains FieldDefs between each MessageDef, so double the - * limit. */ - const size_t maxdepth = UPB_MAX_MESSAGE_DEPTH * 2; - - if (!_upb_def_validate(defs, n, s)) { - return false; - } - - - /* Validation all passed; freeze the objects. */ - return upb_refcounted_freeze((upb_refcounted *const*)defs, n, s, maxdepth); -} +#endif /* upb_enumdef ****************************************************************/ -static void visitenum(const upb_refcounted *r, upb_refcounted_visit *visit, - void *closure) { - const upb_enumdef *e = (const upb_enumdef*)r; - const upb_def *def = upb_enumdef_upcast(e); - if (upb_def_file(def)) { - visit(r, upb_filedef_upcast(upb_def_file(def)), closure); - } -} - -static void freeenum(upb_refcounted *r) { - upb_enumdef *e = (upb_enumdef*)r; - upb_inttable_iter i; - upb_inttable_begin(&i, &e->iton); - for( ; !upb_inttable_done(&i); upb_inttable_next(&i)) { - /* To clean up the upb_gstrdup() from upb_enumdef_addval(). */ - upb_gfree(upb_value_getcstr(upb_inttable_iter_value(&i))); - } - upb_strtable_uninit(&e->ntoi); - upb_inttable_uninit(&e->iton); - upb_def_uninit(upb_enumdef_upcast_mutable(e)); - upb_gfree(e); -} - -const struct upb_refcounted_vtbl upb_enumdef_vtbl = {&visitenum, &freeenum}; - -upb_enumdef *upb_enumdef_new(const void *owner) { - upb_enumdef *e = upb_gmalloc(sizeof(*e)); - if (!e) return NULL; - - if (!upb_def_init(upb_enumdef_upcast_mutable(e), UPB_DEF_ENUM, - &upb_enumdef_vtbl, owner)) { - goto err2; - } +#if 0 +bool upb_enumdef_init(upb_enumdef *e) { + upb_def_init(&e->base, UPB_DEF_ENUM); if (!upb_strtable_init(&e->ntoi, UPB_CTYPE_INT32)) goto err2; if (!upb_inttable_init(&e->iton, UPB_CTYPE_CSTR)) goto err1; - return e; - -err1: - upb_strtable_uninit(&e->ntoi); -err2: - upb_gfree(e); - return NULL; -} - -bool upb_enumdef_freeze(upb_enumdef *e, upb_status *status) { - upb_def *d = upb_enumdef_upcast_mutable(e); - return upb_def_freeze(&d, 1, status); + return true; } +#endif const char *upb_enumdef_fullname(const upb_enumdef *e) { return upb_def_fullname(upb_enumdef_upcast(e)); @@ -492,11 +347,7 @@ const char *upb_enumdef_name(const upb_enumdef *e) { return upb_def_name(upb_enumdef_upcast(e)); } -bool upb_enumdef_setfullname(upb_enumdef *e, const char *fullname, - upb_status *s) { - return upb_def_setfullname(upb_enumdef_upcast_mutable(e), fullname, s); -} - +#if 0 bool upb_enumdef_addval(upb_enumdef *e, const char *name, int32_t num, upb_status *status) { char *name2; @@ -531,12 +382,14 @@ bool upb_enumdef_addval(upb_enumdef *e, const char *name, int32_t num, return true; } +#endif int32_t upb_enumdef_default(const upb_enumdef *e) { UPB_ASSERT(upb_enumdef_iton(e, e->defaultval)); return e->defaultval; } +#if 0 bool upb_enumdef_setdefault(upb_enumdef *e, int32_t val, upb_status *s) { UPB_ASSERT(!upb_enumdef_isfrozen(e)); if (!upb_enumdef_iton(e, val)) { @@ -546,6 +399,7 @@ bool upb_enumdef_setdefault(upb_enumdef *e, int32_t val, upb_status *s) { e->defaultval = val; return true; } +#endif int upb_enumdef_numvals(const upb_enumdef *e) { return upb_strtable_count(&e->ntoi); @@ -588,110 +442,19 @@ int32_t upb_enum_iter_number(upb_enum_iter *iter) { static void upb_fielddef_init_default(upb_fielddef *f); -static void upb_fielddef_uninit_default(upb_fielddef *f) { - if (f->type_is_set_ && f->default_is_string && f->defaultval.bytes) - freestr(f->defaultval.bytes); -} - const char *upb_fielddef_fullname(const upb_fielddef *e) { return upb_def_fullname(upb_fielddef_upcast(e)); } -static void visitfield(const upb_refcounted *r, upb_refcounted_visit *visit, - void *closure) { - const upb_fielddef *f = (const upb_fielddef*)r; - const upb_def *def = upb_fielddef_upcast(f); - if (upb_fielddef_containingtype(f)) { - visit(r, upb_msgdef_upcast2(upb_fielddef_containingtype(f)), closure); - } - if (upb_fielddef_containingoneof(f)) { - visit(r, upb_oneofdef_upcast(upb_fielddef_containingoneof(f)), closure); - } - if (upb_fielddef_subdef(f)) { - visit(r, upb_def_upcast(upb_fielddef_subdef(f)), closure); - } - if (upb_def_file(def)) { - visit(r, upb_filedef_upcast(upb_def_file(def)), closure); - } -} - -static void freefield(upb_refcounted *r) { - upb_fielddef *f = (upb_fielddef*)r; - upb_fielddef_uninit_default(f); - if (f->subdef_is_symbolic) - upb_gfree(f->sub.name); - upb_def_uninit(upb_fielddef_upcast_mutable(f)); - upb_gfree(f); -} - -static const char *enumdefaultstr(const upb_fielddef *f) { - const upb_enumdef *e; - UPB_ASSERT(f->type_is_set_ && f->type_ == UPB_TYPE_ENUM); - e = upb_fielddef_enumsubdef(f); - if (f->default_is_string && f->defaultval.bytes) { - /* Default was explicitly set as a string. */ - str_t *s = f->defaultval.bytes; - return s->str; - } else if (e) { - if (!f->default_is_string) { - /* Default was explicitly set as an integer; look it up in enumdef. */ - const char *name = upb_enumdef_iton(e, f->defaultval.sint); - if (name) { - return name; - } - } else { - /* Default is completely unset; pull enumdef default. */ - if (upb_enumdef_numvals(e) > 0) { - const char *name = upb_enumdef_iton(e, upb_enumdef_default(e)); - UPB_ASSERT(name); - return name; - } - } - } - return NULL; -} - -static bool enumdefaultint32(const upb_fielddef *f, int32_t *val) { - const upb_enumdef *e; - UPB_ASSERT(f->type_is_set_ && f->type_ == UPB_TYPE_ENUM); - e = upb_fielddef_enumsubdef(f); - if (!f->default_is_string) { - /* Default was explicitly set as an integer. */ - *val = f->defaultval.sint; - return true; - } else if (e) { - if (f->defaultval.bytes) { - /* Default was explicitly set as a str; try to lookup corresponding int. */ - str_t *s = f->defaultval.bytes; - if (upb_enumdef_ntoiz(e, s->str, val)) { - return true; - } - } else { - /* Default is unset; try to pull in enumdef default. */ - if (upb_enumdef_numvals(e) > 0) { - *val = upb_enumdef_default(e); - return true; - } - } - } - return false; -} - -const struct upb_refcounted_vtbl upb_fielddef_vtbl = {visitfield, freefield}; - +#if 0 upb_fielddef *upb_fielddef_new(const void *o) { upb_fielddef *f = upb_gmalloc(sizeof(*f)); if (!f) return NULL; - if (!upb_def_init(upb_fielddef_upcast_mutable(f), UPB_DEF_FIELD, - &upb_fielddef_vtbl, o)) { - upb_gfree(f); - return NULL; - } + upb_def_init(&f->base, UPB_DEF_FIELD); f->msg.def = NULL; f->sub.def = NULL; f->oneof = NULL; f->subdef_is_symbolic = false; - f->msg_is_symbolic = false; f->label_ = UPB_LABEL_OPTIONAL; f->type_ = UPB_TYPE_INT32; f->number_ = 0; @@ -712,13 +475,9 @@ upb_fielddef *upb_fielddef_new(const void *o) { f->intfmt = UPB_INTFMT_VARIABLE; return f; } - -bool upb_fielddef_typeisset(const upb_fielddef *f) { - return f->type_is_set_; -} +#endif upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f) { - UPB_ASSERT(f->type_is_set_); return f->type_; } @@ -798,60 +557,16 @@ size_t upb_fielddef_getjsonname(const upb_fielddef *f, char *buf, size_t len) { } const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f) { - return f->msg_is_symbolic ? NULL : f->msg.def; + return f->msgdef; } const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f) { return f->oneof; } -upb_msgdef *upb_fielddef_containingtype_mutable(upb_fielddef *f) { - return (upb_msgdef*)upb_fielddef_containingtype(f); -} - -const char *upb_fielddef_containingtypename(upb_fielddef *f) { - return f->msg_is_symbolic ? f->msg.name : NULL; -} - -static void release_containingtype(upb_fielddef *f) { - if (f->msg_is_symbolic) upb_gfree(f->msg.name); -} - -bool upb_fielddef_setcontainingtypename(upb_fielddef *f, const char *name, - upb_status *s) { - char *name_copy; - UPB_ASSERT(!upb_fielddef_isfrozen(f)); - if (upb_fielddef_containingtype(f)) { - upb_status_seterrmsg(s, "field has already been added to a message."); - return false; - } - /* TODO: validate name (upb_isident() doesn't quite work atm because this name - * may have a leading "."). */ - - name_copy = upb_gstrdup(name); - if (!name_copy) { - upb_upberr_setoom(s); - return false; - } - - release_containingtype(f); - f->msg.name = name_copy; - f->msg_is_symbolic = true; - return true; -} - -bool upb_fielddef_setname(upb_fielddef *f, const char *name, upb_status *s) { - if (upb_fielddef_containingtype(f) || upb_fielddef_containingoneof(f)) { - upb_status_seterrmsg(s, "Already added to message or oneof"); - return false; - } - return upb_def_setfullname(upb_fielddef_upcast_mutable(f), name, s); -} - -static void chkdefaulttype(const upb_fielddef *f, upb_fieldtype_t type) { +static void chkdefaulttype(const upb_fielddef *f, int ctype) { UPB_UNUSED(f); - UPB_UNUSED(type); - UPB_ASSERT(f->type_is_set_ && upb_fielddef_type(f) == type); + UPB_UNUSED(ctype); } int64_t upb_fielddef_defaultint64(const upb_fielddef *f) { @@ -860,15 +575,8 @@ int64_t upb_fielddef_defaultint64(const upb_fielddef *f) { } int32_t upb_fielddef_defaultint32(const upb_fielddef *f) { - if (f->type_is_set_ && upb_fielddef_type(f) == UPB_TYPE_ENUM) { - int32_t val; - bool ok = enumdefaultint32(f, &val); - UPB_ASSERT(ok); - return val; - } else { - chkdefaulttype(f, UPB_TYPE_INT32); - return f->defaultval.sint; - } + chkdefaulttype(f, UPB_TYPE_INT32); + return f->defaultval.sint; } uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f) { @@ -943,10 +651,6 @@ static void upb_fielddef_init_default(upb_fielddef *f) { } } -const upb_def *upb_fielddef_subdef(const upb_fielddef *f) { - return f->subdef_is_symbolic ? NULL : f->sub.def; -} - const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f) { const upb_def *def = upb_fielddef_subdef(f); return def ? upb_dyncast_msgdef(def) : NULL; @@ -957,10 +661,6 @@ const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f) { return def ? upb_dyncast_enumdef(def) : NULL; } -upb_def *upb_fielddef_subdef_mutable(upb_fielddef *f) { - return (upb_def*)upb_fielddef_subdef(f); -} - const char *upb_fielddef_subdefname(const upb_fielddef *f) { if (f->subdef_is_symbolic) { return f->sub.name; @@ -985,306 +685,24 @@ bool upb_fielddef_setnumber(upb_fielddef *f, uint32_t number, upb_status *s) { return true; } -void upb_fielddef_settype(upb_fielddef *f, upb_fieldtype_t type) { - UPB_ASSERT(!upb_fielddef_isfrozen(f)); - UPB_ASSERT(upb_fielddef_checktype(type)); - upb_fielddef_uninit_default(f); - f->type_ = type; - f->type_is_set_ = true; - upb_fielddef_init_default(f); -} - -void upb_fielddef_setdescriptortype(upb_fielddef *f, int type) { - UPB_ASSERT(!upb_fielddef_isfrozen(f)); - switch (type) { - case UPB_DESCRIPTOR_TYPE_DOUBLE: - upb_fielddef_settype(f, UPB_TYPE_DOUBLE); - break; - case UPB_DESCRIPTOR_TYPE_FLOAT: - upb_fielddef_settype(f, UPB_TYPE_FLOAT); - break; - case UPB_DESCRIPTOR_TYPE_INT64: - case UPB_DESCRIPTOR_TYPE_SFIXED64: - case UPB_DESCRIPTOR_TYPE_SINT64: - upb_fielddef_settype(f, UPB_TYPE_INT64); - break; - case UPB_DESCRIPTOR_TYPE_UINT64: - case UPB_DESCRIPTOR_TYPE_FIXED64: - upb_fielddef_settype(f, UPB_TYPE_UINT64); - break; - case UPB_DESCRIPTOR_TYPE_INT32: - case UPB_DESCRIPTOR_TYPE_SFIXED32: - case UPB_DESCRIPTOR_TYPE_SINT32: - upb_fielddef_settype(f, UPB_TYPE_INT32); - break; - case UPB_DESCRIPTOR_TYPE_UINT32: - case UPB_DESCRIPTOR_TYPE_FIXED32: - upb_fielddef_settype(f, UPB_TYPE_UINT32); - break; - case UPB_DESCRIPTOR_TYPE_BOOL: - upb_fielddef_settype(f, UPB_TYPE_BOOL); - break; - case UPB_DESCRIPTOR_TYPE_STRING: - upb_fielddef_settype(f, UPB_TYPE_STRING); - break; - case UPB_DESCRIPTOR_TYPE_BYTES: - upb_fielddef_settype(f, UPB_TYPE_BYTES); - break; - case UPB_DESCRIPTOR_TYPE_GROUP: - case UPB_DESCRIPTOR_TYPE_MESSAGE: - upb_fielddef_settype(f, UPB_TYPE_MESSAGE); - break; - case UPB_DESCRIPTOR_TYPE_ENUM: - upb_fielddef_settype(f, UPB_TYPE_ENUM); - break; - default: UPB_ASSERT(false); - } - - if (type == UPB_DESCRIPTOR_TYPE_FIXED64 || - type == UPB_DESCRIPTOR_TYPE_FIXED32 || - type == UPB_DESCRIPTOR_TYPE_SFIXED64 || - type == UPB_DESCRIPTOR_TYPE_SFIXED32) { - upb_fielddef_setintfmt(f, UPB_INTFMT_FIXED); - } else if (type == UPB_DESCRIPTOR_TYPE_SINT64 || - type == UPB_DESCRIPTOR_TYPE_SINT32) { - upb_fielddef_setintfmt(f, UPB_INTFMT_ZIGZAG); - } else { - upb_fielddef_setintfmt(f, UPB_INTFMT_VARIABLE); - } - - upb_fielddef_settagdelim(f, type == UPB_DESCRIPTOR_TYPE_GROUP); -} - upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f) { - switch (upb_fielddef_type(f)) { - case UPB_TYPE_FLOAT: return UPB_DESCRIPTOR_TYPE_FLOAT; - case UPB_TYPE_DOUBLE: return UPB_DESCRIPTOR_TYPE_DOUBLE; - case UPB_TYPE_BOOL: return UPB_DESCRIPTOR_TYPE_BOOL; - case UPB_TYPE_STRING: return UPB_DESCRIPTOR_TYPE_STRING; - case UPB_TYPE_BYTES: return UPB_DESCRIPTOR_TYPE_BYTES; - case UPB_TYPE_ENUM: return UPB_DESCRIPTOR_TYPE_ENUM; - case UPB_TYPE_INT32: - switch (upb_fielddef_intfmt(f)) { - case UPB_INTFMT_VARIABLE: return UPB_DESCRIPTOR_TYPE_INT32; - case UPB_INTFMT_FIXED: return UPB_DESCRIPTOR_TYPE_SFIXED32; - case UPB_INTFMT_ZIGZAG: return UPB_DESCRIPTOR_TYPE_SINT32; - } - case UPB_TYPE_INT64: - switch (upb_fielddef_intfmt(f)) { - case UPB_INTFMT_VARIABLE: return UPB_DESCRIPTOR_TYPE_INT64; - case UPB_INTFMT_FIXED: return UPB_DESCRIPTOR_TYPE_SFIXED64; - case UPB_INTFMT_ZIGZAG: return UPB_DESCRIPTOR_TYPE_SINT64; - } - case UPB_TYPE_UINT32: - switch (upb_fielddef_intfmt(f)) { - case UPB_INTFMT_VARIABLE: return UPB_DESCRIPTOR_TYPE_UINT32; - case UPB_INTFMT_FIXED: return UPB_DESCRIPTOR_TYPE_FIXED32; - case UPB_INTFMT_ZIGZAG: return -1; - } - case UPB_TYPE_UINT64: - switch (upb_fielddef_intfmt(f)) { - case UPB_INTFMT_VARIABLE: return UPB_DESCRIPTOR_TYPE_UINT64; - case UPB_INTFMT_FIXED: return UPB_DESCRIPTOR_TYPE_FIXED64; - case UPB_INTFMT_ZIGZAG: return -1; - } - case UPB_TYPE_MESSAGE: - return upb_fielddef_istagdelim(f) ? - UPB_DESCRIPTOR_TYPE_GROUP : UPB_DESCRIPTOR_TYPE_MESSAGE; - } - return 0; -} - -void upb_fielddef_setisextension(upb_fielddef *f, bool is_extension) { - UPB_ASSERT(!upb_fielddef_isfrozen(f)); - f->is_extension_ = is_extension; -} - -void upb_fielddef_setlazy(upb_fielddef *f, bool lazy) { - UPB_ASSERT(!upb_fielddef_isfrozen(f)); - f->lazy_ = lazy; -} - -void upb_fielddef_setpacked(upb_fielddef *f, bool packed) { - UPB_ASSERT(!upb_fielddef_isfrozen(f)); - f->packed_ = packed; + return f->type; } -void upb_fielddef_setlabel(upb_fielddef *f, upb_label_t label) { - UPB_ASSERT(!upb_fielddef_isfrozen(f)); - UPB_ASSERT(upb_fielddef_checklabel(label)); - f->label_ = label; -} - -void upb_fielddef_setintfmt(upb_fielddef *f, upb_intfmt_t fmt) { - UPB_ASSERT(!upb_fielddef_isfrozen(f)); - UPB_ASSERT(upb_fielddef_checkintfmt(fmt)); - f->intfmt = fmt; -} - -void upb_fielddef_settagdelim(upb_fielddef *f, bool tag_delim) { - UPB_ASSERT(!upb_fielddef_isfrozen(f)); - f->tagdelim = tag_delim; - f->tagdelim = tag_delim; -} - -static bool checksetdefault(upb_fielddef *f, upb_fieldtype_t type) { - if (!f->type_is_set_ || upb_fielddef_isfrozen(f) || - upb_fielddef_type(f) != type) { - UPB_ASSERT(false); - return false; - } - if (f->default_is_string) { - str_t *s = f->defaultval.bytes; - UPB_ASSERT(s || type == UPB_TYPE_ENUM); - if (s) freestr(s); - } - f->default_is_string = false; - return true; -} - -void upb_fielddef_setdefaultint64(upb_fielddef *f, int64_t value) { - if (checksetdefault(f, UPB_TYPE_INT64)) - f->defaultval.sint = value; -} - -void upb_fielddef_setdefaultint32(upb_fielddef *f, int32_t value) { - if ((upb_fielddef_type(f) == UPB_TYPE_ENUM && - checksetdefault(f, UPB_TYPE_ENUM)) || - checksetdefault(f, UPB_TYPE_INT32)) { - f->defaultval.sint = value; - } -} - -void upb_fielddef_setdefaultuint64(upb_fielddef *f, uint64_t value) { - if (checksetdefault(f, UPB_TYPE_UINT64)) - f->defaultval.uint = value; -} - -void upb_fielddef_setdefaultuint32(upb_fielddef *f, uint32_t value) { - if (checksetdefault(f, UPB_TYPE_UINT32)) - f->defaultval.uint = value; -} - -void upb_fielddef_setdefaultbool(upb_fielddef *f, bool value) { - if (checksetdefault(f, UPB_TYPE_BOOL)) - f->defaultval.uint = value; -} - -void upb_fielddef_setdefaultfloat(upb_fielddef *f, float value) { - if (checksetdefault(f, UPB_TYPE_FLOAT)) - f->defaultval.flt = value; -} - -void upb_fielddef_setdefaultdouble(upb_fielddef *f, double value) { - if (checksetdefault(f, UPB_TYPE_DOUBLE)) - f->defaultval.dbl = value; -} - -bool upb_fielddef_setdefaultstr(upb_fielddef *f, const void *str, size_t len, - upb_status *s) { - str_t *str2; - UPB_ASSERT(upb_fielddef_isstring(f) || f->type_ == UPB_TYPE_ENUM); - if (f->type_ == UPB_TYPE_ENUM && !upb_isident(str, len, false, s)) - return false; - - if (f->default_is_string) { - str_t *s = f->defaultval.bytes; - UPB_ASSERT(s || f->type_ == UPB_TYPE_ENUM); - if (s) freestr(s); - } else { - UPB_ASSERT(f->type_ == UPB_TYPE_ENUM); - } - - str2 = newstr(str, len); - f->defaultval.bytes = str2; - f->default_is_string = true; - return true; -} - -void upb_fielddef_setdefaultcstr(upb_fielddef *f, const char *str, - upb_status *s) { - UPB_ASSERT(f->type_is_set_); - upb_fielddef_setdefaultstr(f, str, str ? strlen(str) : 0, s); -} - -bool upb_fielddef_enumhasdefaultint32(const upb_fielddef *f) { - int32_t val; - UPB_ASSERT(f->type_is_set_ && f->type_ == UPB_TYPE_ENUM); - return enumdefaultint32(f, &val); -} - -bool upb_fielddef_enumhasdefaultstr(const upb_fielddef *f) { - UPB_ASSERT(f->type_is_set_ && f->type_ == UPB_TYPE_ENUM); - return enumdefaultstr(f) != NULL; -} - -static bool upb_subdef_typecheck(upb_fielddef *f, const upb_def *subdef, - upb_status *s) { - if (f->type_ == UPB_TYPE_MESSAGE) { - if (upb_dyncast_msgdef(subdef)) return true; - upb_status_seterrmsg(s, "invalid subdef type for this submessage field"); - return false; - } else if (f->type_ == UPB_TYPE_ENUM) { - if (upb_dyncast_enumdef(subdef)) return true; - upb_status_seterrmsg(s, "invalid subdef type for this enum field"); - return false; - } else { - upb_status_seterrmsg(s, "only message and enum fields can have a subdef"); - return false; - } -} - -static void release_subdef(upb_fielddef *f) { - if (f->subdef_is_symbolic) { - upb_gfree(f->sub.name); - } else if (f->sub.def) { - upb_unref2(f->sub.def, f); - } -} - -bool upb_fielddef_setsubdef(upb_fielddef *f, const upb_def *subdef, - upb_status *s) { - UPB_ASSERT(!upb_fielddef_isfrozen(f)); - UPB_ASSERT(upb_fielddef_hassubdef(f)); - if (subdef && !upb_subdef_typecheck(f, subdef, s)) return false; - release_subdef(f); - f->sub.def = subdef; - f->subdef_is_symbolic = false; - if (f->sub.def) upb_ref2(f->sub.def, f); - return true; -} - -bool upb_fielddef_setmsgsubdef(upb_fielddef *f, const upb_msgdef *subdef, - upb_status *s) { - return upb_fielddef_setsubdef(f, upb_msgdef_upcast(subdef), s); -} - -bool upb_fielddef_setenumsubdef(upb_fielddef *f, const upb_enumdef *subdef, - upb_status *s) { - return upb_fielddef_setsubdef(f, upb_enumdef_upcast(subdef), s); -} - -bool upb_fielddef_setsubdefname(upb_fielddef *f, const char *name, - upb_status *s) { - char *name_copy; - UPB_ASSERT(!upb_fielddef_isfrozen(f)); - if (!upb_fielddef_hassubdef(f)) { - upb_status_seterrmsg(s, "field type does not accept a subdef"); - return false; - } - - name_copy = upb_gstrdup(name); - if (!name_copy) { - upb_upberr_setoom(s); +static bool upb_subdef_typecheck(upb_fielddef *f, const upb_def *subdef, + upb_status *s) { + if (f->type_ == UPB_TYPE_MESSAGE) { + if (upb_dyncast_msgdef(subdef)) return true; + upb_status_seterrmsg(s, "invalid subdef type for this submessage field"); + return false; + } else if (f->type_ == UPB_TYPE_ENUM) { + if (upb_dyncast_enumdef(subdef)) return true; + upb_status_seterrmsg(s, "invalid subdef type for this enum field"); + return false; + } else { + upb_status_seterrmsg(s, "only message and enum fields can have a subdef"); return false; } - - /* TODO: validate name (upb_isident() doesn't quite work atm because this name - * may have a leading "."). */ - release_subdef(f); - f->sub.name = name_copy; - f->subdef_is_symbolic = true; - return true; } bool upb_fielddef_issubmsg(const upb_fielddef *f) { @@ -1343,66 +761,6 @@ static const char *kValueFullMessageName = "google.protobuf.Value"; static const char *kListValueFullMessageName = "google.protobuf.ListValue"; static const char *kStructFullMessageName = "google.protobuf.Struct"; -static void visitmsg(const upb_refcounted *r, upb_refcounted_visit *visit, - void *closure) { - upb_msg_oneof_iter o; - const upb_msgdef *m = (const upb_msgdef*)r; - const upb_def *def = upb_msgdef_upcast(m); - upb_msg_field_iter i; - for(upb_msg_field_begin(&i, m); - !upb_msg_field_done(&i); - upb_msg_field_next(&i)) { - upb_fielddef *f = upb_msg_iter_field(&i); - visit(r, upb_fielddef_upcast2(f), closure); - } - for(upb_msg_oneof_begin(&o, m); - !upb_msg_oneof_done(&o); - upb_msg_oneof_next(&o)) { - upb_oneofdef *f = upb_msg_iter_oneof(&o); - visit(r, upb_oneofdef_upcast(f), closure); - } - if (upb_def_file(def)) { - visit(r, upb_filedef_upcast(upb_def_file(def)), closure); - } -} - -static void freemsg(upb_refcounted *r) { - upb_msgdef *m = (upb_msgdef*)r; - upb_strtable_uninit(&m->ntof); - upb_inttable_uninit(&m->itof); - upb_def_uninit(upb_msgdef_upcast_mutable(m)); - upb_gfree(m); -} - -const struct upb_refcounted_vtbl upb_msgdef_vtbl = {visitmsg, freemsg}; - -upb_msgdef *upb_msgdef_new(const void *owner) { - upb_msgdef *m = upb_gmalloc(sizeof(*m)); - if (!m) return NULL; - - if (!upb_def_init(upb_msgdef_upcast_mutable(m), UPB_DEF_MSG, &upb_msgdef_vtbl, - owner)) { - goto err2; - } - - if (!upb_inttable_init(&m->itof, UPB_CTYPE_PTR)) goto err2; - if (!upb_strtable_init(&m->ntof, UPB_CTYPE_PTR)) goto err1; - m->map_entry = false; - m->syntax = UPB_SYNTAX_PROTO2; - return m; - -err1: - upb_inttable_uninit(&m->itof); -err2: - upb_gfree(m); - return NULL; -} - -bool upb_msgdef_freeze(upb_msgdef *m, upb_status *status) { - upb_def *d = upb_msgdef_upcast_mutable(m); - return upb_def_freeze(&d, 1, status); -} - const char *upb_msgdef_fullname(const upb_msgdef *m) { return upb_def_fullname(upb_msgdef_upcast(m)); } @@ -1411,20 +769,6 @@ const char *upb_msgdef_name(const upb_msgdef *m) { return upb_def_name(upb_msgdef_upcast(m)); } -bool upb_msgdef_setfullname(upb_msgdef *m, const char *fullname, - upb_status *s) { - return upb_def_setfullname(upb_msgdef_upcast_mutable(m), fullname, s); -} - -bool upb_msgdef_setsyntax(upb_msgdef *m, upb_syntax_t syntax) { - if (syntax != UPB_SYNTAX_PROTO2 && syntax != UPB_SYNTAX_PROTO3) { - return false; - } - - m->syntax = syntax; - return true; -} - upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m) { return m->syntax; } @@ -1449,17 +793,6 @@ static bool check_field_add(const upb_msgdef *m, const upb_fielddef *f, return true; } -static void add_field(upb_msgdef *m, upb_fielddef *f, const void *ref_donor) { - release_containingtype(f); - f->msg.def = m; - f->msg_is_symbolic = false; - upb_inttable_insert(&m->itof, upb_fielddef_number(f), upb_value_ptr(f)); - upb_strtable_insert(&m->ntof, upb_fielddef_name(f), upb_value_ptr(f)); - upb_ref2(f, m); - upb_ref2(m, f); - if (ref_donor) upb_fielddef_unref(f, ref_donor); -} - bool upb_msgdef_addfield(upb_msgdef *m, upb_fielddef *f, const void *ref_donor, upb_status *s) { /* TODO: extensions need to have a separate namespace, because proto2 allows a @@ -1590,11 +923,6 @@ int upb_msgdef_numoneofs(const upb_msgdef *m) { return upb_strtable_count(&m->ntof) - upb_inttable_count(&m->itof); } -void upb_msgdef_setmapentry(upb_msgdef *m, bool map_entry) { - UPB_ASSERT(!upb_msgdef_isfrozen(m)); - m->map_entry = map_entry; -} - bool upb_msgdef_mapentry(const upb_msgdef *m) { return m->map_entry; } @@ -1668,80 +996,8 @@ void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter) { /* upb_oneofdef ***************************************************************/ -static void visitoneof(const upb_refcounted *r, upb_refcounted_visit *visit, - void *closure) { - const upb_oneofdef *o = (const upb_oneofdef*)r; - upb_oneof_iter i; - for (upb_oneof_begin(&i, o); !upb_oneof_done(&i); upb_oneof_next(&i)) { - const upb_fielddef *f = upb_oneof_iter_field(&i); - visit(r, upb_fielddef_upcast2(f), closure); - } - if (o->parent) { - visit(r, upb_msgdef_upcast2(o->parent), closure); - } -} - -static void freeoneof(upb_refcounted *r) { - upb_oneofdef *o = (upb_oneofdef*)r; - upb_strtable_uninit(&o->ntof); - upb_inttable_uninit(&o->itof); - upb_gfree((void*)o->name); - upb_gfree(o); -} - -const struct upb_refcounted_vtbl upb_oneofdef_vtbl = {visitoneof, freeoneof}; - -upb_oneofdef *upb_oneofdef_new(const void *owner) { - upb_oneofdef *o = upb_gmalloc(sizeof(*o)); - - if (!o) { - return NULL; - } - - o->parent = NULL; - o->name = NULL; - - if (!upb_refcounted_init(upb_oneofdef_upcast_mutable(o), &upb_oneofdef_vtbl, - owner)) { - goto err2; - } - - if (!upb_inttable_init(&o->itof, UPB_CTYPE_PTR)) goto err2; - if (!upb_strtable_init(&o->ntof, UPB_CTYPE_PTR)) goto err1; - - return o; - -err1: - upb_inttable_uninit(&o->itof); -err2: - upb_gfree(o); - return NULL; -} - const char *upb_oneofdef_name(const upb_oneofdef *o) { return o->name; } -bool upb_oneofdef_setname(upb_oneofdef *o, const char *name, upb_status *s) { - UPB_ASSERT(!upb_oneofdef_isfrozen(o)); - if (upb_oneofdef_containingtype(o)) { - upb_status_seterrmsg(s, "oneof already added to a message"); - return false; - } - - if (!upb_isident(name, strlen(name), true, s)) { - return false; - } - - name = upb_gstrdup(name); - if (!name) { - upb_status_seterrmsg(s, "One of memory"); - return false; - } - - upb_gfree((void*)o->name); - o->name = name; - return true; -} - const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o) { return o->parent; } @@ -1818,7 +1074,6 @@ bool upb_oneofdef_addfield(upb_oneofdef *o, upb_fielddef *f, } } - release_containingtype(f); f->oneof = o; upb_inttable_insert(&o->itof, upb_fielddef_number(f), upb_value_ptr(f)); upb_strtable_insert(&o->ntof, upb_fielddef_name(f), upb_value_ptr(f)); @@ -1864,35 +1119,6 @@ void upb_oneof_iter_setdone(upb_oneof_iter *iter) { /* upb_filedef ****************************************************************/ -static void visitfiledef(const upb_refcounted *r, upb_refcounted_visit *visit, - void *closure) { - const upb_filedef *f = (const upb_filedef*)r; - size_t i; - - for(i = 0; i < upb_filedef_defcount(f); i++) { - visit(r, upb_def_upcast(upb_filedef_def(f, i)), closure); - } -} - -static void freefiledef(upb_refcounted *r) { - upb_filedef *f = (upb_filedef*)r; - size_t i; - - for(i = 0; i < upb_filedef_depcount(f); i++) { - upb_filedef_unref(upb_filedef_dep(f, i), f); - } - - upb_inttable_uninit(&f->defs); - upb_inttable_uninit(&f->deps); - upb_gfree((void*)f->name); - upb_gfree((void*)f->package); - upb_gfree((void*)f->phpprefix); - upb_gfree((void*)f->phpnamespace); - upb_gfree(f); -} - -const struct upb_refcounted_vtbl upb_filedef_vtbl = {visitfiledef, freefiledef}; - upb_filedef *upb_filedef_new(const void *owner) { upb_filedef *f = upb_gmalloc(sizeof(*f)); @@ -1906,11 +1132,6 @@ upb_filedef *upb_filedef_new(const void *owner) { f->phpnamespace = NULL; f->syntax = UPB_SYNTAX_PROTO2; - if (!upb_refcounted_init(upb_filedef_upcast_mutable(f), &upb_filedef_vtbl, - owner)) { - goto err; - } - if (!upb_inttable_init(&f->defs, UPB_CTYPE_CONSTPTR)) { goto err; } @@ -1978,122 +1199,8 @@ const upb_filedef *upb_filedef_dep(const upb_filedef *f, size_t i) { } } -bool upb_filedef_setname(upb_filedef *f, const char *name, upb_status *s) { - name = upb_gstrdup(name); - if (!name) { - upb_upberr_setoom(s); - return false; - } - upb_gfree((void*)f->name); - f->name = name; - return true; -} - -bool upb_filedef_setpackage(upb_filedef *f, const char *package, - upb_status *s) { - if (!upb_isident(package, strlen(package), true, s)) return false; - package = upb_gstrdup(package); - if (!package) { - upb_upberr_setoom(s); - return false; - } - upb_gfree((void*)f->package); - f->package = package; - return true; -} - -bool upb_filedef_setphpprefix(upb_filedef *f, const char *phpprefix, - upb_status *s) { - phpprefix = upb_gstrdup(phpprefix); - if (!phpprefix) { - upb_upberr_setoom(s); - return false; - } - upb_gfree((void*)f->phpprefix); - f->phpprefix = phpprefix; - return true; -} - -bool upb_filedef_setphpnamespace(upb_filedef *f, const char *phpnamespace, - upb_status *s) { - phpnamespace = upb_gstrdup(phpnamespace); - if (!phpnamespace) { - upb_upberr_setoom(s); - return false; - } - upb_gfree((void*)f->phpnamespace); - f->phpnamespace = phpnamespace; - return true; -} - -bool upb_filedef_setsyntax(upb_filedef *f, upb_syntax_t syntax, - upb_status *s) { - UPB_UNUSED(s); - if (syntax != UPB_SYNTAX_PROTO2 && - syntax != UPB_SYNTAX_PROTO3) { - upb_status_seterrmsg(s, "Unknown syntax value."); - return false; - } - f->syntax = syntax; - - { - /* Set all messages in this file to match. */ - size_t i; - for (i = 0; i < upb_filedef_defcount(f); i++) { - /* Casting const away is safe since all defs in mutable filedef must - * also be mutable. */ - upb_def *def = (upb_def*)upb_filedef_def(f, i); - - upb_msgdef *m = upb_dyncast_msgdef_mutable(def); - if (m) { - m->syntax = syntax; - } - } - } - - return true; -} - -bool upb_filedef_adddef(upb_filedef *f, upb_def *def, const void *ref_donor, - upb_status *s) { - if (def->file) { - upb_status_seterrmsg(s, "Def is already part of another filedef."); - return false; - } - - if (upb_inttable_push(&f->defs, upb_value_constptr(def))) { - def->file = f; - upb_ref2(def, f); - upb_ref2(f, def); - if (ref_donor) upb_def_unref(def, ref_donor); - if (def->type == UPB_DEF_MSG) { - upb_downcast_msgdef_mutable(def)->syntax = f->syntax; - } - return true; - } else { - upb_upberr_setoom(s); - return false; - } -} - -bool upb_filedef_adddep(upb_filedef *f, const upb_filedef *dep) { - if (upb_inttable_push(&f->deps, upb_value_constptr(dep))) { - /* Regular ref instead of ref2 because files can't form cycles. */ - upb_filedef_ref(dep, f); - return true; - } else { - return false; - } -} - void upb_symtab_free(upb_symtab *s) { - upb_strtable_iter i; - upb_strtable_begin(&i, &s->symtab); - for (; !upb_strtable_done(&i); upb_strtable_next(&i)) { - const upb_def *def = upb_value_getptr(upb_strtable_iter_value(&i)); - upb_def_unref(def, s); - } - upb_strtable_uninit(&s->symtab); + upb_arena_uninit(&s->arena); upb_gfree(s); } @@ -2103,7 +1210,17 @@ upb_symtab *upb_symtab_new() { return NULL; } - upb_strtable_init(&s->symtab, UPB_CTYPE_PTR); + if (!upb_arena_init(&s->arena)) goto err2; + if (!upb_strtable_init2(&s->symtab, UPB_CTYPE_PTR, + upb_arena_alloc(&s->arena))) { + goto err1; + } + return s; + +err1: + upb_arena_uninit(&s->arena); +err2: + upb_gfree(s); return s; } @@ -2153,77 +1270,7 @@ const upb_def *upb_symtab_resolve(const upb_symtab *s, const char *base, return ret; } -/* TODO(haberman): we need a lot more testing of error conditions. */ -static bool symtab_add(upb_symtab *s, upb_def *const*defs, size_t n, - void *ref_donor, upb_refcounted *freeze_also, - upb_status *status) { - size_t i; - size_t add_n; - size_t freeze_n; - upb_strtable_iter iter; - upb_refcounted **add_objs = NULL; - upb_def **add_defs = NULL; - size_t add_objs_size; - upb_strtable addtab; - - if (n == 0 && !freeze_also) { - return true; - } - - if (!upb_strtable_init(&addtab, UPB_CTYPE_PTR)) { - upb_status_seterrmsg(status, "out of memory"); - return false; - } - - /* Add new defs to our "add" set. */ - for (i = 0; i < n; i++) { - upb_def *def = defs[i]; - const char *fullname; - upb_fielddef *f; - - if (upb_def_isfrozen(def)) { - upb_status_seterrmsg(status, "added defs must be mutable"); - goto err; - } - UPB_ASSERT(!upb_def_isfrozen(def)); - fullname = upb_def_fullname(def); - if (!fullname) { - upb_status_seterrmsg( - status, "Anonymous defs cannot be added to a symtab"); - goto err; - } - - f = upb_dyncast_fielddef_mutable(def); - - if (f) { - if (!upb_fielddef_containingtypename(f)) { - upb_status_seterrmsg(status, - "Standalone fielddefs must have a containing type " - "(extendee) name set"); - goto err; - } - } else { - if (upb_strtable_lookup(&addtab, fullname, NULL)) { - upb_status_seterrf(status, "Conflicting defs named '%s'", fullname); - goto err; - } - if (upb_strtable_lookup(&s->symtab, fullname, NULL)) { - upb_status_seterrf(status, "Symtab already has a def named '%s'", - fullname); - goto err; - } - if (!upb_strtable_insert(&addtab, fullname, upb_value_ptr(def))) - goto oom_err; - upb_def_donateref(def, ref_donor, s); - } - - if (upb_dyncast_fielddef_mutable(def)) { - /* TODO(haberman): allow adding extensions attached to files. */ - upb_status_seterrf(status, "Can't add extensions to symtab.\n"); - goto err; - } - } - +#if 0 /* Now using the table, resolve symbolic references for subdefs. */ upb_strtable_begin(&iter, &addtab); for (; !upb_strtable_done(&iter); upb_strtable_next(&iter)) { @@ -2258,132 +1305,202 @@ static bool symtab_add(upb_symtab *s, upb_def *const*defs, size_t n, } } } +#endif - /* We need an array of the defs in addtab, for passing to - * upb_refcounted_freeze(). */ - add_objs_size = upb_strtable_count(&addtab); - if (freeze_also) { - add_objs_size++; - } +static bool create_oneofdef( + upb_symtab *s, const google_protobuf_OneofDescriptorProto *oneof_proto, + upb_msgdef *m, upb_status *s) { + upb_alloc *alloc = upb_arena_alloc(&s->arena); + upb_oneofdef *o = upb_malloc(alloc, sizeof(upb_oneofdef)); + upb_stringview name = google_protobuf_OneofDescriptorProto_name(oneof_proto); + upb_value o_ptr = upb_value_ptr(o); - add_defs = upb_gmalloc(sizeof(void*) * add_objs_size); - if (add_defs == NULL) goto oom_err; - upb_strtable_begin(&iter, &addtab); - for (add_n = 0; !upb_strtable_done(&iter); upb_strtable_next(&iter)) { - add_defs[add_n++] = upb_value_getptr(upb_strtable_iter_value(&iter)); - } + CHK(o); + CHK(upb_inttable_init2(&o->itof, UPB_CTYPE_PTR, alloc)); + CHK(upb_strtable_init2(&o->ntof, UPB_CTYPE_PTR, alloc)); - /* Validate defs. */ - if (!_upb_def_validate(add_defs, add_n, status)) { - goto err; + o->index = upb_strtable_count(&m->ntof); + o->name = upb_strdup2(name.data, name.size, alloc); + o->parent = m; + + CHK(upb_strtable_insert3(&m->ntof, name.data, name.size, o_ptr, alloc)); + + return true; +} + +static bool create_field( + upb_symtab *s, const google_protobuf_FieldDescriptorProto *field_proto, + upb_msgdef *m, upb_status *s) { + upb_alloc *alloc = upb_arena_alloc(&s->arena); + upb_fielddef *f = upb_malloc(alloc, sizeof(upb_fielddef)); + const google_protobuf_MessageOptions *options; + const upb_array *arr; + + CHK(f); + + f->msgdef = m; + + union { + int64_t sint; + uint64_t uint; + double dbl; + float flt; + void *bytes; + } defaultval; + const upb_msgdef *msgdef; + union { + const upb_msgdef *msgdef; + const upb_msgdef *enumdef; + } sub; + const upb_oneofdef *oneof; + bool is_extension_; + bool lazy_; + bool packed_; + upb_intfmt_t intfmt; + bool tagdelim; + upb_fieldtype_t type_; + upb_label_t label_; + uint32_t number_; + uint32_t selector_base; /* Used to index into a upb::Handlers table. */ + uint32_t index_; +} + +static bool create_msgdef(upb_symtab *s, + const google_protobuf_DescriptorProto *msg_proto, + upb_symtab *addtab, upb_status *status) { + upb_alloc *alloc = upb_arena_alloc(&s->arena); + upb_msgdef *m = upb_malloc(alloc, sizeof(upb_msgdef)); + const google_protobuf_MessageOptions *options; + const upb_array *arr; + + CHK(m); + CHK(upb_inttable_init2(&m->itof, UPB_CTYPE_PTR, alloc)); + CHK(upb_strtable_init2(&m->ntof, UPB_CTYPE_PTR, alloc)); + + m->map_entry = false; + + options = google_protobuf_DescriptorProto_options(msg_proto); + + if (options) { + m->map_entry = google_protobuf_MessageOptions_map_entry(options); } - /* Cheat a little and give the array a new type. - * This is probably undefined behavior, but this code will be deleted soon. */ - add_objs = (upb_refcounted**)add_defs; + arr = google_protobuf_DescriptorProto_oneof_decl(msg_proto); - freeze_n = add_n; - if (freeze_also) { - add_objs[freeze_n++] = freeze_also; + for (i = 0; i < upb_array_size(arr); i++) { + const google_protobuf_OneofDescriptorProto *oneof_proto = + (const void *)upb_array_get(arr, i); + CHK(create_oneofdef(s, oneof_proto, m, status)); } - if (!upb_refcounted_freeze(add_objs, freeze_n, status, - UPB_MAX_MESSAGE_DEPTH * 2)) { - goto err; + arr = google_protobuf_DescriptorProto_field(msg_proto); + + for (i = 0; i < upb_array_size(arr); i++) { + const google_protobuf_FieldDescriptorProto *field_proto = + (const void *)upb_array_get(arr, i); + CHK(create_fielddef(s, field_proto, m, status)); } - /* This must be delayed until all errors have been detected, since error - * recovery code uses this table to cleanup defs. */ - upb_strtable_uninit(&addtab); - - /* TODO(haberman) we don't properly handle errors after this point (like - * OOM in upb_strtable_insert() below). */ - for (i = 0; i < add_n; i++) { - upb_def *def = (upb_def*)add_objs[i]; - const char *name = upb_def_fullname(def); - bool success; - success = upb_strtable_insert(&s->symtab, name, upb_value_ptr(def)); - UPB_ASSERT(success); + arr = google_protobuf_DescriptorProto_enum_type(msg_proto); + + for (i = 0; i < upb_array_size(arr); i++) { + const google_protobuf_EnumDescriptorProto *enum_proto = + (const void *)upb_array_get(arr, i); + CHK(create_enumdef(s, enum_proto, addtab, status)); } - upb_gfree(add_defs); - return true; -oom_err: - upb_status_seterrmsg(status, "out of memory"); -err: { - /* We need to donate the refs back. */ - upb_strtable_begin(&iter, &addtab); - for (; !upb_strtable_done(&iter); upb_strtable_next(&iter)) { - upb_def *def = upb_value_getptr(upb_strtable_iter_value(&iter)); - upb_def_donateref(def, s, ref_donor); - } + arr = google_protobuf_DescriptorProto_nested_type(msg_proto); + + for (i = 0; i < upb_array_size(arr); i++) { + const google_protobuf_DescriptorProto *msg_proto2 = + (const void *)upb_array_get(arr, i); + CHK(create_msgdef(s, msg_proto2, addtab, status)); } - upb_strtable_uninit(&addtab); - upb_gfree(add_defs); - UPB_ASSERT(!upb_ok(status)); - return false; + + return true; } -bool upb_symtab_add(upb_symtab *s, upb_def *const*defs, size_t n, - void *ref_donor, upb_status *status) { - return symtab_add(s, defs, n, ref_donor, NULL, status); +static char* strviewdup(upb_symtab *symtab, upb_stringview view) { + if (view.size == 0) { + return NULL; + } + return upb_strdup2(view.data, view.size, upb_arena_alloc(&symtab->arena)); } -bool upb_symtab_addfile(upb_symtab *s, upb_filedef *file, upb_status *status) { - size_t n; +bool upb_symtab_addfile(upb_symtab *s, const char *buf, size_t len, + upb_status *status) { + upb_arena tmparena; + upb_strtable addtab; + upb_alloc *alloc = upb_arena_alloc(&s->arena); + upb_stringview serialized = upb_stringview_make(buf, len); + const google_protobuf_FileDescriptorProto *file_proto; + const google_protobuf_FileOptions *file_options_proto; + upb_filedef *file = upb_malloc(alloc, sizeof(*file)); + const upb_array *arr; + upb_strtable_iter iter; size_t i; - upb_def **defs; - bool ret; - n = upb_filedef_defcount(file); - if (n == 0) { - return true; - } - defs = upb_gmalloc(sizeof(*defs) * n); + upb_arena_init(&tmparena); + upb_strtable_init2(&addtab, UPB_CTYPE_PTR, upb_arena_alloc(&tmparena)); + file_proto = + google_protobuf_FileDescriptorProto_parsenew(serialized, &tmparena); - if (defs == NULL) { - upb_status_seterrmsg(status, "Out of memory"); - return false; - } - for (i = 0; i < n; i++) { - defs[i] = upb_filedef_mutabledef(file, i); + if (!file_proto || !file) goto err; + + file->name = + strviewdup(s, google_protobuf_FileDescriptorProto_name(file_proto)); + file->package = + strviewdup(s, google_protobuf_FileDescriptorProto_package(file_proto)); + file->phpprefix = NULL; + file->phpnamespace = NULL; + + file_options_proto = google_protobuf_FileDescriptorProto_options(file_proto); + if (file_options_proto) { + file->phpprefix = + strviewdup(s, google_protobuf_FileOptions_php_class_prefix(file_proto)); + file->phpnamespace = + strviewdup(s, google_protobuf_FileOptions_php_namespace(file_proto)); } - ret = symtab_add(s, defs, n, NULL, upb_filedef_upcast_mutable(file), status); + arr = google_protobuf_FileDescriptorProto_message_type(file_proto); - upb_gfree(defs); - return ret; -} + for (i = 0; i < upb_array_size(arr); i++) { + const google_protobuf_DescriptorProto *msg_proto = + (const void *)upb_array_get(arr, i); + if (!create_msgdef(s, msg_proto, &addtab, status)) goto err; + } -/* Iteration. */ + arr = google_protobuf_FileDescriptorProto_enum_type(file_proto); -static void advance_to_matching(upb_symtab_iter *iter) { - if (iter->type == UPB_DEF_ANY) - return; + for (i = 0; i < upb_array_size(arr); i++) { + const google_protobuf_EnumDescriptorProto *enum_proto = + (const void *)upb_array_get(arr, i); + if (!create_enumdef(s, enum_proto, &addtab, status)) goto err; + } - while (!upb_strtable_done(&iter->iter) && - iter->type != upb_symtab_iter_def(iter)->type) { - upb_strtable_next(&iter->iter); + /* Now that all names are in the table, resolve references. */ + upb_strtable_begin(&iter, &addtab); + for (; !upb_strtable_done(&iter); upb_strtable_next(&iter)) { } -} -void upb_symtab_begin(upb_symtab_iter *iter, const upb_symtab *s, - upb_deftype_t type) { - upb_strtable_begin(&iter->iter, &s->symtab); - iter->type = type; - advance_to_matching(iter); -} + /* Success; add addtab to symtab. */ + upb_strtable_begin(&iter, &addtab); + for (; !upb_strtable_done(&iter); upb_strtable_next(&iter)) { + const char *key = upb_strtable_iter_key(&iter); + size_t keylen = upb_strtable_iter_keylen(&iter); + upb_value value = upb_strtable_iter_value(&iter); + upb_strtable_insert3(&s->symtab, key, keylen, value, alloc); + } -void upb_symtab_next(upb_symtab_iter *iter) { - upb_strtable_next(&iter->iter); - advance_to_matching(iter); -} + return true; -bool upb_symtab_done(const upb_symtab_iter *iter) { - return upb_strtable_done(&iter->iter); +err: + upb_arena_uninit(&tmparena); + return false; } -const upb_def *upb_symtab_iter_def(const upb_symtab_iter *iter) { - return upb_value_getptr(upb_strtable_iter_value(&iter->iter)); +bool upb_symtab_addset(upb_symtab *s, const char *buf, size_t len, + upb_status *status) { + return true; } diff --git a/upb/def.h b/upb/def.h index 3886417..c229ca4 100644 --- a/upb/def.h +++ b/upb/def.h @@ -7,13 +7,9 @@ ** - upb::FileDef (upb_filedef): describes a .proto file and its defs. ** - upb::EnumDef (upb_enumdef): describes an enum. ** - upb::OneofDef (upb_oneofdef): describes a oneof. -** - upb::Def (upb_def): base class of all the others. ** ** TODO: definitions of services. ** -** Like upb_refcounted objects, defs are mutable only until frozen, and are -** only thread-safe once frozen. -** ** This is a mixed C/C++ interface that offers a full API to both languages. ** See the top-level README for more information. */ @@ -21,7 +17,8 @@ #ifndef UPB_DEF_H_ #define UPB_DEF_H_ -#include "upb/refcounted.h" +#include "upb/upb.h" +#include "upb/table.int.h" #ifdef __cplusplus #include @@ -29,7 +26,6 @@ #include namespace upb { -class Def; class EnumDef; class FieldDef; class FileDef; @@ -39,249 +35,16 @@ class SymbolTable; } #endif -UPB_DECLARE_DERIVED_TYPE(upb::Def, upb::RefCounted, upb_def, upb_refcounted) -UPB_DECLARE_DERIVED_TYPE(upb::OneofDef, upb::RefCounted, upb_oneofdef, - upb_refcounted) -UPB_DECLARE_DERIVED_TYPE(upb::FileDef, upb::RefCounted, upb_filedef, - upb_refcounted) +UPB_DECLARE_TYPE(upb::EnumDef, upb_enumdef) +UPB_DECLARE_TYPE(upb::FieldDef, upb_fielddef) +UPB_DECLARE_TYPE(upb::FileDef, upb_filedef) +UPB_DECLARE_TYPE(upb::MessageDef, upb_msgdef) +UPB_DECLARE_TYPE(upb::OneofDef, upb_oneofdef) UPB_DECLARE_TYPE(upb::SymbolTable, upb_symtab) -/* The maximum message depth that the type graph can have. This is a resource - * limit for the C stack since we sometimes need to recursively traverse the - * graph. Cycles are ok; the traversal will stop when it detects a cycle, but - * we must hit the cycle before the maximum depth is reached. - * - * If having a single static limit is too inflexible, we can add another variant - * of Def::Freeze that allows specifying this as a parameter. */ -#define UPB_MAX_MESSAGE_DEPTH 64 - - -/* upb::Def: base class for top-level defs ***********************************/ - -/* All the different kind of defs that can be defined at the top-level and put - * in a SymbolTable or appear in a FileDef::defs() list. This excludes some - * defs (like oneofs and files). It only includes fields because they can be - * defined as extensions. */ -typedef enum { - UPB_DEF_MSG, - UPB_DEF_FIELD, - UPB_DEF_ENUM, - UPB_DEF_SERVICE, /* Not yet implemented. */ - UPB_DEF_ANY = -1 /* Wildcard for upb_symtab_get*() */ -} upb_deftype_t; - -#ifdef __cplusplus - -/* The base class of all defs. Its base is upb::RefCounted (use upb::upcast() - * to convert). */ -class upb::Def { - public: - typedef upb_deftype_t Type; - - /* upb::RefCounted methods like Ref()/Unref(). */ - UPB_REFCOUNTED_CPPMETHODS - - Type def_type() const; - - /* "fullname" is the def's fully-qualified name (eg. foo.bar.Message). */ - const char *full_name() const; - - /* The final part of a def's name (eg. Message). */ - const char *name() const; - - /* The def must be mutable. Caller retains ownership of fullname. Defs are - * not required to have a name; if a def has no name when it is frozen, it - * will remain an anonymous def. On failure, returns false and details in "s" - * if non-NULL. */ - bool set_full_name(const char* fullname, upb::Status* s); - bool set_full_name(const std::string &fullname, upb::Status* s); - - /* The file in which this def appears. It is not necessary to add a def to a - * file (and consequently the accessor may return NULL). Set this by calling - * file->Add(def). */ - FileDef* file() const; - - /* Freezes the given defs; this validates all constraints and marks the defs - * as frozen (read-only). "defs" may not contain any fielddefs, but fields - * of any msgdefs will be frozen. - * - * Symbolic references to sub-types and enum defaults must have already been - * resolved. Any mutable defs reachable from any of "defs" must also be in - * the list; more formally, "defs" must be a transitive closure of mutable - * defs. - * - * After this operation succeeds, the finalized defs must only be accessed - * through a const pointer! */ - static bool Freeze(Def* const* defs, size_t n, Status* status); - static bool Freeze(const std::vector& defs, Status* status); - - private: - UPB_DISALLOW_POD_OPS(Def, upb::Def) -}; - -#endif /* __cplusplus */ - -UPB_BEGIN_EXTERN_C - -/* Include upb_refcounted methods like upb_def_ref()/upb_def_unref(). */ -UPB_REFCOUNTED_CMETHODS(upb_def, upb_def_upcast) - -upb_deftype_t upb_def_type(const upb_def *d); -const char *upb_def_fullname(const upb_def *d); -const char *upb_def_name(const upb_def *d); -const upb_filedef *upb_def_file(const upb_def *d); -bool upb_def_setfullname(upb_def *def, const char *fullname, upb_status *s); -bool upb_def_freeze(upb_def *const *defs, size_t n, upb_status *s); - -/* Temporary API: for internal use only. */ -bool _upb_def_validate(upb_def *const*defs, size_t n, upb_status *s); - -UPB_END_EXTERN_C - - -/* upb::Def casts *************************************************************/ - -#ifdef __cplusplus -#define UPB_CPP_CASTS(cname, cpptype) \ - namespace upb { \ - template <> \ - inline cpptype *down_cast(Def * def) { \ - return upb_downcast_##cname##_mutable(def); \ - } \ - template <> \ - inline cpptype *dyn_cast(Def * def) { \ - return upb_dyncast_##cname##_mutable(def); \ - } \ - template <> \ - inline const cpptype *down_cast( \ - const Def *def) { \ - return upb_downcast_##cname(def); \ - } \ - template <> \ - inline const cpptype *dyn_cast(const Def *def) { \ - return upb_dyncast_##cname(def); \ - } \ - template <> \ - inline const cpptype *down_cast(Def * def) { \ - return upb_downcast_##cname(def); \ - } \ - template <> \ - inline const cpptype *dyn_cast(Def * def) { \ - return upb_dyncast_##cname(def); \ - } \ - } /* namespace upb */ -#else -#define UPB_CPP_CASTS(cname, cpptype) -#endif /* __cplusplus */ - -/* Dynamic casts, for determining if a def is of a particular type at runtime. - * Downcasts, for when some wants to assert that a def is of a particular type. - * These are only checked if we are building debug. */ -#define UPB_DEF_CASTS(lower, upper, cpptype) \ - UPB_INLINE const upb_##lower *upb_dyncast_##lower(const upb_def *def) { \ - if (upb_def_type(def) != UPB_DEF_##upper) return NULL; \ - return (upb_##lower *)def; \ - } \ - UPB_INLINE const upb_##lower *upb_downcast_##lower(const upb_def *def) { \ - UPB_ASSERT(upb_def_type(def) == UPB_DEF_##upper); \ - return (const upb_##lower *)def; \ - } \ - UPB_INLINE upb_##lower *upb_dyncast_##lower##_mutable(upb_def *def) { \ - return (upb_##lower *)upb_dyncast_##lower(def); \ - } \ - UPB_INLINE upb_##lower *upb_downcast_##lower##_mutable(upb_def *def) { \ - return (upb_##lower *)upb_downcast_##lower(def); \ - } \ - UPB_CPP_CASTS(lower, cpptype) - -#define UPB_DEFINE_DEF(cppname, lower, upper, cppmethods, members) \ - UPB_DEFINE_CLASS2(cppname, upb::Def, upb::RefCounted, cppmethods, \ - members) \ - UPB_DEF_CASTS(lower, upper, cppname) - -#define UPB_DECLARE_DEF_TYPE(cppname, lower, upper) \ - UPB_DECLARE_DERIVED_TYPE2(cppname, upb::Def, upb::RefCounted, \ - upb_ ## lower, upb_def, upb_refcounted) \ - UPB_DEF_CASTS(lower, upper, cppname) - -UPB_DECLARE_DEF_TYPE(upb::FieldDef, fielddef, FIELD) -UPB_DECLARE_DEF_TYPE(upb::MessageDef, msgdef, MSG) -UPB_DECLARE_DEF_TYPE(upb::EnumDef, enumdef, ENUM) - -#undef UPB_DECLARE_DEF_TYPE -#undef UPB_DEF_CASTS -#undef UPB_CPP_CASTS - - /* upb::FieldDef **************************************************************/ -/* The types a field can have. Note that this list is not identical to the - * types defined in descriptor.proto, which gives INT32 and SINT32 separate - * types (we distinguish the two with the "integer encoding" enum below). */ -typedef enum { - /* Types stored in 1 byte. */ - UPB_TYPE_BOOL = 1, - /* Types stored in 4 bytes. */ - UPB_TYPE_FLOAT = 2, - UPB_TYPE_INT32 = 3, - UPB_TYPE_UINT32 = 4, - UPB_TYPE_ENUM = 5, /* Enum values are int32. */ - /* Types stored as pointers (probably 4 or 8 bytes). */ - UPB_TYPE_STRING = 6, - UPB_TYPE_BYTES = 7, - UPB_TYPE_MESSAGE = 8, - /* Types stored as 8 bytes. */ - UPB_TYPE_DOUBLE = 9, - UPB_TYPE_INT64 = 10, - UPB_TYPE_UINT64 = 11 -} upb_fieldtype_t; - -/* The repeated-ness of each field; this matches descriptor.proto. */ -typedef enum { - UPB_LABEL_OPTIONAL = 1, - UPB_LABEL_REQUIRED = 2, - UPB_LABEL_REPEATED = 3 -} upb_label_t; - -/* How integers should be encoded in serializations that offer multiple - * integer encoding methods. */ -typedef enum { - UPB_INTFMT_VARIABLE = 1, - UPB_INTFMT_FIXED = 2, - UPB_INTFMT_ZIGZAG = 3 /* Only for signed types (INT32/INT64). */ -} upb_intfmt_t; - -/* Descriptor types, as defined in descriptor.proto. */ -typedef enum { - UPB_DESCRIPTOR_TYPE_DOUBLE = 1, - UPB_DESCRIPTOR_TYPE_FLOAT = 2, - UPB_DESCRIPTOR_TYPE_INT64 = 3, - UPB_DESCRIPTOR_TYPE_UINT64 = 4, - UPB_DESCRIPTOR_TYPE_INT32 = 5, - UPB_DESCRIPTOR_TYPE_FIXED64 = 6, - UPB_DESCRIPTOR_TYPE_FIXED32 = 7, - UPB_DESCRIPTOR_TYPE_BOOL = 8, - UPB_DESCRIPTOR_TYPE_STRING = 9, - UPB_DESCRIPTOR_TYPE_GROUP = 10, - UPB_DESCRIPTOR_TYPE_MESSAGE = 11, - UPB_DESCRIPTOR_TYPE_BYTES = 12, - UPB_DESCRIPTOR_TYPE_UINT32 = 13, - UPB_DESCRIPTOR_TYPE_ENUM = 14, - UPB_DESCRIPTOR_TYPE_SFIXED32 = 15, - UPB_DESCRIPTOR_TYPE_SFIXED64 = 16, - UPB_DESCRIPTOR_TYPE_SINT32 = 17, - UPB_DESCRIPTOR_TYPE_SINT64 = 18 -} upb_descriptortype_t; - -typedef enum { - UPB_SYNTAX_PROTO2 = 2, - UPB_SYNTAX_PROTO3 = 3 -} upb_syntax_t; - -/* Maps descriptor type -> upb field type. */ -extern const uint8_t upb_desctype_to_fieldtype[]; - /* Maximum field number allowed for FieldDefs. This is an inherent limit of the * protobuf wire format. */ #define UPB_MAX_FIELDNUMBER ((1 << 29) - 1) @@ -300,33 +63,12 @@ class upb::FieldDef { typedef upb_intfmt_t IntegerFormat; typedef upb_descriptortype_t DescriptorType; - /* These return true if the given value is a valid member of the enumeration. */ - static bool CheckType(int32_t val); - static bool CheckLabel(int32_t val); - static bool CheckDescriptorType(int32_t val); - static bool CheckIntegerFormat(int32_t val); - - /* These convert to the given enumeration; they require that the value is - * valid. */ - static Type ConvertType(int32_t val); - static Label ConvertLabel(int32_t val); - static DescriptorType ConvertDescriptorType(int32_t val); - static IntegerFormat ConvertIntegerFormat(int32_t val); - - /* Returns NULL if memory allocation failed. */ - static reffed_ptr New(); - - /* upb::RefCounted methods like Ref()/Unref(). */ - UPB_REFCOUNTED_CPPMETHODS - - /* Functionality from upb::Def. */ const char* full_name() const; - bool type_is_set() const; /* set_[descriptor_]type() has been called? */ - Type type() const; /* Requires that type_is_set() == true. */ - Label label() const; /* Defaults to UPB_LABEL_OPTIONAL. */ - const char* name() const; /* NULL if uninitialized. */ - uint32_t number() const; /* Returns 0 if uninitialized. */ + Type type() const; + Label label() const; + const char* name() const; + uint32_t number() const; bool is_extension() const; /* Copies the JSON name for this field into the given buffer. Returns the @@ -381,7 +123,6 @@ class upb::FieldDef { * of the containing type symbolically instead. This is mostly useful for * extensions, where the extension is declared separately from the message. */ const MessageDef* containing_type() const; - const char* containing_type_name(); /* The OneofDef to which this field belongs, or NULL if this field is not part * of a oneof. */ @@ -391,8 +132,7 @@ class upb::FieldDef { * the same as UPB_TYPE_*, because it distinguishes between (for example) * INT32 and SINT32, whereas our "type" enum does not. This return of * descriptor_type() is a function of type(), integer_format(), and - * is_tag_delimited(). Likewise set_descriptor_type() sets all three - * appropriately. */ + * is_tag_delimited(). */ DescriptorType descriptor_type() const; /* Convenient field type tests. */ @@ -471,89 +211,6 @@ class upb::FieldDef { * been resolved to a specific subdef, returns the name from that subdef. */ const char* subdef_name() const; - /* Setters (non-const methods), only valid for mutable FieldDefs! ***********/ - - bool set_full_name(const char* fullname, upb::Status* s); - bool set_full_name(const std::string& fullname, upb::Status* s); - - /* This may only be called if containing_type() == NULL (ie. the field has not - * been added to a message yet). */ - bool set_containing_type_name(const char *name, Status* status); - bool set_containing_type_name(const std::string& name, Status* status); - - /* Defaults to false. When we freeze, we ensure that this can only be true - * for length-delimited message fields. Prior to freezing this can be true or - * false with no restrictions. */ - void set_lazy(bool lazy); - - /* Defaults to true. Sets whether this field is encoded in packed format. */ - void set_packed(bool packed); - - /* "type" or "descriptor_type" MUST be set explicitly before the fielddef is - * finalized. These setters require that the enum value is valid; if the - * value did not come directly from an enum constant, the caller should - * validate it first with the functions above (CheckFieldType(), etc). */ - void set_type(Type type); - void set_label(Label label); - void set_descriptor_type(DescriptorType type); - void set_is_extension(bool is_extension); - - /* "number" and "name" must be set before the FieldDef is added to a - * MessageDef, and may not be set after that. - * - * "name" is the same as full_name()/set_full_name(), but since fielddefs - * most often use simple, non-qualified names, we provide this accessor - * also. Generally only extensions will want to think of this name as - * fully-qualified. */ - bool set_number(uint32_t number, upb::Status* s); - bool set_name(const char* name, upb::Status* s); - bool set_name(const std::string& name, upb::Status* s); - - /* Sets the JSON name to the given string. */ - /* TODO(haberman): implement. Right now only default json_name (camelCase) - * is supported. */ - bool set_json_name(const char* json_name, upb::Status* s); - bool set_json_name(const std::string& name, upb::Status* s); - - /* Clears the JSON name. This will make it revert to its default, which is - * a camelCased version of the regular field name. */ - void clear_json_name(); - - void set_integer_format(IntegerFormat format); - bool set_tag_delimited(bool tag_delimited, upb::Status* s); - - /* Sets default value for the field. The call must exactly match the type - * of the field. Enum fields may use either setint32 or setstring to set - * the default numerically or symbolically, respectively, but symbolic - * defaults must be resolved before finalizing (see ResolveEnumDefault()). - * - * Changing the type of a field will reset its default. */ - void set_default_int64(int64_t val); - void set_default_int32(int32_t val); - void set_default_uint64(uint64_t val); - void set_default_uint32(uint32_t val); - void set_default_bool(bool val); - void set_default_float(float val); - void set_default_double(double val); - bool set_default_string(const void *str, size_t len, Status *s); - bool set_default_string(const std::string &str, Status *s); - void set_default_cstr(const char *str, Status *s); - - /* Before a fielddef is frozen, its subdef may be set either directly (with a - * upb::Def*) or symbolically. Symbolic refs must be resolved before the - * containing msgdef can be frozen (see upb_resolve() above). upb always - * guarantees that any def reachable from a live def will also be kept alive. - * - * Both methods require that upb_hassubdef(f) (so the type must be set prior - * to calling these methods). Returns false if this is not the case, or if - * the given subdef is not of the correct type. The subdef is reset if the - * field's type is changed. The subdef can be set to NULL to clear it. */ - bool set_subdef(const Def* subdef, Status* s); - bool set_enum_subdef(const EnumDef* subdef, Status* s); - bool set_message_subdef(const MessageDef* subdef, Status* s); - bool set_subdef_name(const char* name, Status* s); - bool set_subdef_name(const std::string &name, Status* s); - private: UPB_DISALLOW_POD_OPS(FieldDef, upb::FieldDef) }; @@ -563,16 +220,7 @@ class upb::FieldDef { UPB_BEGIN_EXTERN_C /* Native C API. */ -upb_fielddef *upb_fielddef_new(const void *owner); - -/* Include upb_refcounted methods like upb_fielddef_ref(). */ -UPB_REFCOUNTED_CMETHODS(upb_fielddef, upb_fielddef_upcast2) - -/* Methods from upb_def. */ const char *upb_fielddef_fullname(const upb_fielddef *f); -bool upb_fielddef_setfullname(upb_fielddef *f, const char *fullname, - upb_status *s); - bool upb_fielddef_typeisset(const upb_fielddef *f); upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f); upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f); @@ -586,7 +234,6 @@ size_t upb_fielddef_getjsonname(const upb_fielddef *f, char *buf, size_t len); const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f); const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f); upb_msgdef *upb_fielddef_containingtype_mutable(upb_fielddef *f); -const char *upb_fielddef_containingtypename(upb_fielddef *f); upb_intfmt_t upb_fielddef_intfmt(const upb_fielddef *f); uint32_t upb_fielddef_index(const upb_fielddef *f); bool upb_fielddef_istagdelim(const upb_fielddef *f); @@ -604,52 +251,13 @@ bool upb_fielddef_defaultbool(const upb_fielddef *f); float upb_fielddef_defaultfloat(const upb_fielddef *f); double upb_fielddef_defaultdouble(const upb_fielddef *f); const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len); -bool upb_fielddef_enumhasdefaultint32(const upb_fielddef *f); -bool upb_fielddef_enumhasdefaultstr(const upb_fielddef *f); bool upb_fielddef_hassubdef(const upb_fielddef *f); -const upb_def *upb_fielddef_subdef(const upb_fielddef *f); const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f); const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f); const char *upb_fielddef_subdefname(const upb_fielddef *f); -void upb_fielddef_settype(upb_fielddef *f, upb_fieldtype_t type); -void upb_fielddef_setdescriptortype(upb_fielddef *f, int type); -void upb_fielddef_setlabel(upb_fielddef *f, upb_label_t label); -bool upb_fielddef_setnumber(upb_fielddef *f, uint32_t number, upb_status *s); -bool upb_fielddef_setname(upb_fielddef *f, const char *name, upb_status *s); -bool upb_fielddef_setjsonname(upb_fielddef *f, const char *name, upb_status *s); -bool upb_fielddef_clearjsonname(upb_fielddef *f); -bool upb_fielddef_setcontainingtypename(upb_fielddef *f, const char *name, - upb_status *s); -void upb_fielddef_setisextension(upb_fielddef *f, bool is_extension); -void upb_fielddef_setlazy(upb_fielddef *f, bool lazy); -void upb_fielddef_setpacked(upb_fielddef *f, bool packed); -void upb_fielddef_setintfmt(upb_fielddef *f, upb_intfmt_t fmt); -void upb_fielddef_settagdelim(upb_fielddef *f, bool tag_delim); -void upb_fielddef_setdefaultint64(upb_fielddef *f, int64_t val); -void upb_fielddef_setdefaultint32(upb_fielddef *f, int32_t val); -void upb_fielddef_setdefaultuint64(upb_fielddef *f, uint64_t val); -void upb_fielddef_setdefaultuint32(upb_fielddef *f, uint32_t val); -void upb_fielddef_setdefaultbool(upb_fielddef *f, bool val); -void upb_fielddef_setdefaultfloat(upb_fielddef *f, float val); -void upb_fielddef_setdefaultdouble(upb_fielddef *f, double val); -bool upb_fielddef_setdefaultstr(upb_fielddef *f, const void *str, size_t len, - upb_status *s); -void upb_fielddef_setdefaultcstr(upb_fielddef *f, const char *str, - upb_status *s); -bool upb_fielddef_setsubdef(upb_fielddef *f, const upb_def *subdef, - upb_status *s); -bool upb_fielddef_setmsgsubdef(upb_fielddef *f, const upb_msgdef *subdef, - upb_status *s); -bool upb_fielddef_setenumsubdef(upb_fielddef *f, const upb_enumdef *subdef, - upb_status *s); -bool upb_fielddef_setsubdefname(upb_fielddef *f, const char *name, - upb_status *s); - -bool upb_fielddef_checklabel(int32_t label); -bool upb_fielddef_checktype(int32_t type); -bool upb_fielddef_checkdescriptortype(int32_t type); -bool upb_fielddef_checkintfmt(int32_t fmt); +/* Internal only. */ +uint32_t upb_fielddef_selectorbase(const upb_fielddef *f); UPB_END_EXTERN_C @@ -678,22 +286,8 @@ typedef upb_strtable_iter upb_msg_oneof_iter; * Its base class is upb::Def (use upb::upcast() to convert). */ class upb::MessageDef { public: - /* Returns NULL if memory allocation failed. */ - static reffed_ptr New(); - - /* upb::RefCounted methods like Ref()/Unref(). */ - UPB_REFCOUNTED_CPPMETHODS - - /* Functionality from upb::Def. */ const char* full_name() const; const char* name() const; - bool set_full_name(const char* fullname, Status* s); - bool set_full_name(const std::string& fullname, Status* s); - - /* Call to freeze this MessageDef. - * WARNING: this will fail if this message has any unfrozen submessages! - * Messages with cycles must be frozen as a batch using upb::Def::Freeze(). */ - bool Freeze(Status* s); /* The number of fields that belong to the MessageDef. */ int field_count() const; @@ -701,62 +295,17 @@ class upb::MessageDef { /* The number of oneofs that belong to the MessageDef. */ int oneof_count() const; - /* Adds a field (upb_fielddef object) to a msgdef. Requires that the msgdef - * and the fielddefs are mutable. The fielddef's name and number must be - * set, and the message may not already contain any field with this name or - * number, and this fielddef may not be part of another message. In error - * cases false is returned and the msgdef is unchanged. - * - * If the given field is part of a oneof, this call succeeds if and only if - * that oneof is already part of this msgdef. (Note that adding a oneof to a - * msgdef automatically adds all of its fields to the msgdef at the time that - * the oneof is added, so it is usually more idiomatic to add the oneof's - * fields first then add the oneof to the msgdef. This case is supported for - * convenience.) - * - * If |f| is already part of this MessageDef, this method performs no action - * and returns true (success). Thus, this method is idempotent. */ - bool AddField(FieldDef* f, Status* s); - bool AddField(const reffed_ptr& f, Status* s); - - /* Adds a oneof (upb_oneofdef object) to a msgdef. Requires that the msgdef, - * oneof, and any fielddefs are mutable, that the fielddefs contained in the - * oneof do not have any name or number conflicts with existing fields in the - * msgdef, and that the oneof's name is unique among all oneofs in the msgdef. - * If the oneof is added successfully, all of its fields will be added - * directly to the msgdef as well. In error cases, false is returned and the - * msgdef is unchanged. */ - bool AddOneof(OneofDef* o, Status* s); - bool AddOneof(const reffed_ptr& o, Status* s); - upb_syntax_t syntax() const; - /* Returns false if we don't support this syntax value. */ - bool set_syntax(upb_syntax_t syntax); - - /* Set this to false to indicate that primitive fields should not have - * explicit presence information associated with them. This will affect all - * fields added to this message. Defaults to true. */ - void SetPrimitivesHavePresence(bool have_presence); - /* These return NULL if the field is not found. */ - FieldDef* FindFieldByNumber(uint32_t number); - FieldDef* FindFieldByName(const char *name, size_t len); const FieldDef* FindFieldByNumber(uint32_t number) const; const FieldDef* FindFieldByName(const char* name, size_t len) const; - FieldDef* FindFieldByName(const char *name) { - return FindFieldByName(name, strlen(name)); - } const FieldDef* FindFieldByName(const char *name) const { return FindFieldByName(name, strlen(name)); } - template - FieldDef* FindFieldByName(const T& str) { - return FindFieldByName(str.c_str(), str.size()); - } template const FieldDef* FindFieldByName(const T& str) const { return FindFieldByName(str.c_str(), str.size()); @@ -765,17 +314,10 @@ class upb::MessageDef { OneofDef* FindOneofByName(const char* name, size_t len); const OneofDef* FindOneofByName(const char* name, size_t len) const; - OneofDef* FindOneofByName(const char* name) { - return FindOneofByName(name, strlen(name)); - } const OneofDef* FindOneofByName(const char* name) const { return FindOneofByName(name, strlen(name)); } - template - OneofDef* FindOneofByName(const T& str) { - return FindOneofByName(str.c_str(), str.size()); - } template const OneofDef* FindOneofByName(const T& str) const { return FindOneofByName(str.c_str(), str.size()); @@ -801,21 +343,6 @@ class upb::MessageDef { bool structvalue() const; /* Iteration over fields. The order is undefined. */ - class field_iterator - : public std::iterator { - public: - explicit field_iterator(MessageDef* md); - static field_iterator end(MessageDef* md); - - void operator++(); - FieldDef* operator*() const; - bool operator!=(const field_iterator& other) const; - bool operator==(const field_iterator& other) const; - - private: - upb_msg_field_iter iter_; - }; - class const_field_iterator : public std::iterator { public: @@ -832,21 +359,6 @@ class upb::MessageDef { }; /* Iteration over oneofs. The order is undefined. */ - class oneof_iterator - : public std::iterator { - public: - explicit oneof_iterator(MessageDef* md); - static oneof_iterator end(MessageDef* md); - - void operator++(); - OneofDef* operator*() const; - bool operator!=(const oneof_iterator& other) const; - bool operator==(const oneof_iterator& other) const; - - private: - upb_msg_oneof_iter iter_; - }; - class const_oneof_iterator : public std::iterator { public: @@ -862,15 +374,6 @@ class upb::MessageDef { upb_msg_oneof_iter iter_; }; - class FieldAccessor { - public: - explicit FieldAccessor(MessageDef* msg) : msg_(msg) {} - field_iterator begin() { return msg_->field_begin(); } - field_iterator end() { return msg_->field_end(); } - private: - MessageDef* msg_; - }; - class ConstFieldAccessor { public: explicit ConstFieldAccessor(const MessageDef* msg) : msg_(msg) {} @@ -880,15 +383,6 @@ class upb::MessageDef { const MessageDef* msg_; }; - class OneofAccessor { - public: - explicit OneofAccessor(MessageDef* msg) : msg_(msg) {} - oneof_iterator begin() { return msg_->oneof_begin(); } - oneof_iterator end() { return msg_->oneof_end(); } - private: - MessageDef* msg_; - }; - class ConstOneofAccessor { public: explicit ConstOneofAccessor(const MessageDef* msg) : msg_(msg) {} @@ -898,19 +392,13 @@ class upb::MessageDef { const MessageDef* msg_; }; - field_iterator field_begin(); - field_iterator field_end(); const_field_iterator field_begin() const; const_field_iterator field_end() const; - oneof_iterator oneof_begin(); - oneof_iterator oneof_end(); const_oneof_iterator oneof_begin() const; const_oneof_iterator oneof_end() const; - FieldAccessor fields() { return FieldAccessor(this); } ConstFieldAccessor fields() const { return ConstFieldAccessor(this); } - OneofAccessor oneofs() { return OneofAccessor(this); } ConstOneofAccessor oneofs() const { return ConstOneofAccessor(this); } private: @@ -921,32 +409,20 @@ class upb::MessageDef { UPB_BEGIN_EXTERN_C -/* Returns NULL if memory allocation failed. */ -upb_msgdef *upb_msgdef_new(const void *owner); - -/* Include upb_refcounted methods like upb_msgdef_ref(). */ -UPB_REFCOUNTED_CMETHODS(upb_msgdef, upb_msgdef_upcast2) - -bool upb_msgdef_freeze(upb_msgdef *m, upb_status *status); - const char *upb_msgdef_fullname(const upb_msgdef *m); const char *upb_msgdef_name(const upb_msgdef *m); int upb_msgdef_numoneofs(const upb_msgdef *m); upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m); - -bool upb_msgdef_addfield(upb_msgdef *m, upb_fielddef *f, const void *ref_donor, - upb_status *s); -bool upb_msgdef_addoneof(upb_msgdef *m, upb_oneofdef *o, const void *ref_donor, - upb_status *s); -bool upb_msgdef_setfullname(upb_msgdef *m, const char *fullname, upb_status *s); -void upb_msgdef_setmapentry(upb_msgdef *m, bool map_entry); bool upb_msgdef_mapentry(const upb_msgdef *m); bool upb_msgdef_duration(const upb_msgdef *m); bool upb_msgdef_timestamp(const upb_msgdef *m); bool upb_msgdef_value(const upb_msgdef *m); bool upb_msgdef_listvalue(const upb_msgdef *m); bool upb_msgdef_structvalue(const upb_msgdef *m); -bool upb_msgdef_setsyntax(upb_msgdef *m, upb_syntax_t syntax); + +/* Internal-only. */ +size_t upb_msgdef_selectorcount(const upb_msgdef *m); +uint32_t upb_msgdef_submsgfieldcount(const upb_msgdef *m); /* Field lookup in a couple of different variations: * - itof = int to field @@ -962,15 +438,6 @@ UPB_INLINE const upb_fielddef *upb_msgdef_ntofz(const upb_msgdef *m, return upb_msgdef_ntof(m, name, strlen(name)); } -UPB_INLINE upb_fielddef *upb_msgdef_itof_mutable(upb_msgdef *m, uint32_t i) { - return (upb_fielddef*)upb_msgdef_itof(m, i); -} - -UPB_INLINE upb_fielddef *upb_msgdef_ntof_mutable(upb_msgdef *m, - const char *name, size_t len) { - return (upb_fielddef *)upb_msgdef_ntof(m, name, len); -} - /* Oneof lookup: * - ntoo = name to oneof * - ntooz = name to oneof, null-terminated string. */ @@ -983,11 +450,6 @@ UPB_INLINE const upb_oneofdef *upb_msgdef_ntooz(const upb_msgdef *m, return upb_msgdef_ntoo(m, name, strlen(name)); } -UPB_INLINE upb_oneofdef *upb_msgdef_ntoo_mutable(upb_msgdef *m, - const char *name, size_t len) { - return (upb_oneofdef *)upb_msgdef_ntoo(m, name, len); -} - /* Lookup of either field or oneof by name. Returns whether either was found. * If the return is true, then the found def will be set, and the non-found * one set to NULL. */ @@ -1040,41 +502,19 @@ typedef upb_strtable_iter upb_enum_iter; * upb::upcast()). */ class upb::EnumDef { public: - /* Returns NULL if memory allocation failed. */ - static reffed_ptr New(); - - /* upb::RefCounted methods like Ref()/Unref(). */ - UPB_REFCOUNTED_CPPMETHODS - - /* Functionality from upb::Def. */ const char* full_name() const; const char* name() const; - bool set_full_name(const char* fullname, Status* s); - bool set_full_name(const std::string& fullname, Status* s); - - /* Call to freeze this EnumDef. */ - bool Freeze(Status* s); - /* The value that is used as the default when no field default is specified. * If not set explicitly, the first value that was added will be used. * The default value must be a member of the enum. * Requires that value_count() > 0. */ int32_t default_value() const; - /* Sets the default value. If this value is not valid, returns false and an - * error message in status. */ - bool set_default_value(int32_t val, Status* status); - /* Returns the number of values currently defined in the enum. Note that * multiple names can refer to the same number, so this may be greater than * the total number of unique numbers. */ int value_count() const; - /* Adds a single name/number pair to the enum. Fails if this name has - * already been used by another value. */ - bool AddValue(const char* name, int32_t num, Status* status); - bool AddValue(const std::string& name, int32_t num, Status* status); - /* Lookups from name to integer, returning true if found. */ bool FindValueByName(const char* name, int32_t* num) const; @@ -1108,25 +548,10 @@ class upb::EnumDef { UPB_BEGIN_EXTERN_C -/* Native C API. */ -upb_enumdef *upb_enumdef_new(const void *owner); - -/* Include upb_refcounted methods like upb_enumdef_ref(). */ -UPB_REFCOUNTED_CMETHODS(upb_enumdef, upb_enumdef_upcast2) - -bool upb_enumdef_freeze(upb_enumdef *e, upb_status *status); - -/* From upb_def. */ const char *upb_enumdef_fullname(const upb_enumdef *e); const char *upb_enumdef_name(const upb_enumdef *e); -bool upb_enumdef_setfullname(upb_enumdef *e, const char *fullname, - upb_status *s); - int32_t upb_enumdef_default(const upb_enumdef *e); -bool upb_enumdef_setdefault(upb_enumdef *e, int32_t val, upb_status *s); int upb_enumdef_numvals(const upb_enumdef *e); -bool upb_enumdef_addval(upb_enumdef *e, const char *name, int32_t num, - upb_status *status); /* Enum lookups: * - ntoi: look up a name with specified length. @@ -1164,53 +589,23 @@ typedef upb_inttable_iter upb_oneof_iter; /* Class that represents a oneof. */ class upb::OneofDef { public: - /* Returns NULL if memory allocation failed. */ - static reffed_ptr New(); - - /* upb::RefCounted methods like Ref()/Unref(). */ - UPB_REFCOUNTED_CPPMETHODS - /* Returns the MessageDef that owns this OneofDef. */ const MessageDef* containing_type() const; /* Returns the name of this oneof. This is the name used to look up the oneof * by name once added to a message def. */ const char* name() const; - bool set_name(const char* name, Status* s); - bool set_name(const std::string& name, Status* s); /* Returns the number of fields currently defined in the oneof. */ int field_count() const; - /* Adds a field to the oneof. The field must not have been added to any other - * oneof or msgdef. If the oneof is not yet part of a msgdef, then when the - * oneof is eventually added to a msgdef, all fields added to the oneof will - * also be added to the msgdef at that time. If the oneof is already part of a - * msgdef, the field must either be a part of that msgdef already, or must not - * be a part of any msgdef; in the latter case, the field is added to the - * msgdef as a part of this operation. - * - * The field may only have an OPTIONAL label, never REQUIRED or REPEATED. - * - * If |f| is already part of this MessageDef, this method performs no action - * and returns true (success). Thus, this method is idempotent. */ - bool AddField(FieldDef* field, Status* s); - bool AddField(const reffed_ptr& field, Status* s); - /* Looks up by name. */ const FieldDef* FindFieldByName(const char* name, size_t len) const; FieldDef* FindFieldByName(const char* name, size_t len); const FieldDef* FindFieldByName(const char* name) const { return FindFieldByName(name, strlen(name)); } - FieldDef* FindFieldByName(const char* name) { - return FindFieldByName(name, strlen(name)); - } - template - FieldDef* FindFieldByName(const T& str) { - return FindFieldByName(str.c_str(), str.size()); - } template const FieldDef* FindFieldByName(const T& str) const { return FindFieldByName(str.c_str(), str.size()); @@ -1219,21 +614,6 @@ class upb::OneofDef { /* Looks up by tag number. */ const FieldDef* FindFieldByNumber(uint32_t num) const; - /* Iteration over fields. The order is undefined. */ - class iterator : public std::iterator { - public: - explicit iterator(OneofDef* md); - static iterator end(OneofDef* md); - - void operator++(); - FieldDef* operator*() const; - bool operator!=(const iterator& other) const; - bool operator==(const iterator& other) const; - - private: - upb_oneof_iter iter_; - }; - class const_iterator : public std::iterator { public: @@ -1262,22 +642,11 @@ class upb::OneofDef { UPB_BEGIN_EXTERN_C -/* Native C API. */ -upb_oneofdef *upb_oneofdef_new(const void *owner); - -/* Include upb_refcounted methods like upb_oneofdef_ref(). */ -UPB_REFCOUNTED_CMETHODS(upb_oneofdef, upb_oneofdef_upcast) - const char *upb_oneofdef_name(const upb_oneofdef *o); const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o); int upb_oneofdef_numfields(const upb_oneofdef *o); uint32_t upb_oneofdef_index(const upb_oneofdef *o); -bool upb_oneofdef_setname(upb_oneofdef *o, const char *name, upb_status *s); -bool upb_oneofdef_addfield(upb_oneofdef *o, upb_fielddef *f, - const void *ref_donor, - upb_status *s); - /* Oneof lookups: * - ntof: look up a field by name. * - ntofz: look up a field by name (as a null-terminated string). @@ -1314,35 +683,23 @@ UPB_END_EXTERN_C * read the values of file-level options. */ class upb::FileDef { public: - /* Returns NULL if memory allocation failed. */ - static reffed_ptr New(); - - /* upb::RefCounted methods like Ref()/Unref(). */ - UPB_REFCOUNTED_CPPMETHODS - /* Get/set name of the file (eg. "foo/bar.proto"). */ const char* name() const; - bool set_name(const char* name, Status* s); - bool set_name(const std::string& name, Status* s); /* Package name for definitions inside the file (eg. "foo.bar"). */ const char* package() const; - bool set_package(const char* package, Status* s); /* Sets the php class prefix which is prepended to all php generated classes * from this .proto. Default is empty. */ const char* phpprefix() const; - bool set_phpprefix(const char* phpprefix, Status* s); /* Use this option to change the namespace of php generated classes. Default * is empty. When this option is empty, the package name will be used for * determining the namespace. */ const char* phpnamespace() const; - bool set_phpnamespace(const char* phpnamespace, Status* s); /* Syntax for the file. Defaults to proto2. */ upb_syntax_t syntax() const; - void set_syntax(upb_syntax_t syntax); /* Get the list of defs from the file. These are returned in the order that * they were added to the FileDef. */ @@ -1355,29 +712,6 @@ class upb::FileDef { int dependency_count() const; const FileDef* dependency(int index) const; - /* Adds defs to this file. The def must not already belong to another - * file. - * - * Note: this does *not* ensure that this def's name is unique in this file! - * Use a SymbolTable if you want to check this property. Especially since - * properly checking uniqueness would require a check across *all* files - * (including dependencies). */ - bool AddDef(Def* def, Status* s); - bool AddMessage(MessageDef* m, Status* s); - bool AddEnum(EnumDef* e, Status* s); - bool AddExtension(FieldDef* f, Status* s); - - /* Adds a dependency of this file. */ - bool AddDependency(const FileDef* file); - - /* Freezes this FileDef and all messages/enums under it. All subdefs must be - * resolved and all messages/enums must validate. Returns true if this - * succeeded. - * - * TODO(haberman): should we care whether the file's dependencies are frozen - * already? */ - bool Freeze(Status* s); - private: UPB_DISALLOW_POD_OPS(FileDef, upb::FileDef) }; @@ -1386,60 +720,14 @@ class upb::FileDef { UPB_BEGIN_EXTERN_C -upb_filedef *upb_filedef_new(const void *owner); - -/* Include upb_refcounted methods like upb_msgdef_ref(). */ -UPB_REFCOUNTED_CMETHODS(upb_filedef, upb_filedef_upcast) - const char *upb_filedef_name(const upb_filedef *f); const char *upb_filedef_package(const upb_filedef *f); const char *upb_filedef_phpprefix(const upb_filedef *f); const char *upb_filedef_phpnamespace(const upb_filedef *f); upb_syntax_t upb_filedef_syntax(const upb_filedef *f); -size_t upb_filedef_defcount(const upb_filedef *f); -size_t upb_filedef_depcount(const upb_filedef *f); -const upb_def *upb_filedef_def(const upb_filedef *f, size_t i); -const upb_filedef *upb_filedef_dep(const upb_filedef *f, size_t i); - -bool upb_filedef_freeze(upb_filedef *f, upb_status *s); -bool upb_filedef_setname(upb_filedef *f, const char *name, upb_status *s); -bool upb_filedef_setpackage(upb_filedef *f, const char *package, upb_status *s); -bool upb_filedef_setphpprefix(upb_filedef *f, const char *phpprefix, - upb_status *s); -bool upb_filedef_setphpnamespace(upb_filedef *f, const char *phpnamespace, - upb_status *s); -bool upb_filedef_setsyntax(upb_filedef *f, upb_syntax_t syntax, upb_status *s); - -bool upb_filedef_adddef(upb_filedef *f, upb_def *def, const void *ref_donor, - upb_status *s); -bool upb_filedef_adddep(upb_filedef *f, const upb_filedef *dep); - -UPB_INLINE bool upb_filedef_addmsg(upb_filedef *f, upb_msgdef *m, - const void *ref_donor, upb_status *s) { - return upb_filedef_adddef(f, upb_msgdef_upcast_mutable(m), ref_donor, s); -} - -UPB_INLINE bool upb_filedef_addenum(upb_filedef *f, upb_enumdef *e, - const void *ref_donor, upb_status *s) { - return upb_filedef_adddef(f, upb_enumdef_upcast_mutable(e), ref_donor, s); -} - -UPB_INLINE bool upb_filedef_addext(upb_filedef *file, upb_fielddef *f, - const void *ref_donor, upb_status *s) { - return upb_filedef_adddef(file, upb_fielddef_upcast_mutable(f), ref_donor, s); -} -UPB_INLINE upb_def *upb_filedef_mutabledef(upb_filedef *f, int i) { - return (upb_def*)upb_filedef_def(f, i); -} UPB_END_EXTERN_C -typedef struct { - UPB_PRIVATE_FOR_CPP - upb_strtable_iter iter; - upb_deftype_t type; -} upb_symtab_iter; - #ifdef __cplusplus /* Non-const methods in upb::SymbolTable are NOT thread-safe. */ @@ -1450,68 +738,19 @@ class upb::SymbolTable { static SymbolTable* New(); static void Free(upb::SymbolTable* table); - /* For all lookup functions, the returned pointer is not owned by the - * caller; it may be invalidated by any non-const call or unref of the - * SymbolTable! To protect against this, take a ref if desired. */ - - /* Freezes the symbol table: prevents further modification of it. - * After the Freeze() operation is successful, the SymbolTable must only be - * accessed via a const pointer. - * - * Unlike with upb::MessageDef/upb::EnumDef/etc, freezing a SymbolTable is not - * a necessary step in using a SymbolTable. If you have no need for it to be - * immutable, there is no need to freeze it ever. However sometimes it is - * useful, and SymbolTables that are statically compiled into the binary are - * always frozen by nature. */ - void Freeze(); - - /* Resolves the given symbol using the rules described in descriptor.proto, - * namely: - * - * If the name starts with a '.', it is fully-qualified. Otherwise, - * C++-like scoping rules are used to find the type (i.e. first the nested - * types within this message are searched, then within the parent, on up - * to the root namespace). - * - * If not found, returns NULL. */ - const Def* Resolve(const char* base, const char* sym) const; - /* Finds an entry in the symbol table with this exact name. If not found, * returns NULL. */ const Def* Lookup(const char *sym) const; const MessageDef* LookupMessage(const char *sym) const; const EnumDef* LookupEnum(const char *sym) const; - /* TODO: introduce a C++ iterator, but make it nice and templated so that if - * you ask for an iterator of MessageDef the iterated elements are strongly - * typed as MessageDef*. */ + /* TODO: iteration? */ - /* Adds the given mutable defs to the symtab, resolving all symbols (including - * enum default values) and finalizing the defs. Only one def per name may be - * in the list, and the defs may not duplicate any name already in the symtab. - * All defs must have a name -- anonymous defs are not allowed. Anonymous - * defs can still be frozen by calling upb_def_freeze() directly. - * - * The entire operation either succeeds or fails. If the operation fails, - * the symtab is unchanged, false is returned, and status indicates the - * error. The caller passes a ref on all defs to the symtab (even if the - * operation fails). - * - * TODO(haberman): currently failure will leave the symtab unchanged, but may - * leave the defs themselves partially resolved. Does this matter? If so we - * could do a prepass that ensures that all symbols are resolvable and bail - * if not, so we don't mutate anything until we know the operation will - * succeed. */ - bool Add(Def*const* defs, size_t n, void* ref_donor, Status* status); - - bool Add(const std::vector& defs, void *owner, Status* status) { - return Add((Def*const*)&defs[0], defs.size(), owner, status); - } + /* Adds the given serialized FileDescriptorProto to the pool. */ + bool AddFile(const char *file, size_t len, Status *status); - /* Resolves all subdefs for messages in this file and attempts to freeze the - * file. If this succeeds, adds all the symbols to this SymbolTable - * (replacing any existing ones with the same names). */ - bool AddFile(FileDef* file, Status* s); + /* Adds the given serialized FileDescriptorSet to the pool. */ + bool AddSet(const char *set, size_t len, Status *status); private: UPB_DISALLOW_POD_OPS(SymbolTable, upb::SymbolTable) @@ -1525,30 +764,12 @@ UPB_BEGIN_EXTERN_C upb_symtab *upb_symtab_new(); void upb_symtab_free(upb_symtab* s); -const upb_def *upb_symtab_resolve(const upb_symtab *s, const char *base, - const char *sym); -const upb_def *upb_symtab_lookup(const upb_symtab *s, const char *sym); const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym); const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym); -bool upb_symtab_add(upb_symtab *s, upb_def *const*defs, size_t n, - void *ref_donor, upb_status *status); -bool upb_symtab_addfile(upb_symtab *s, upb_filedef *file, upb_status* status); - -/* upb_symtab_iter i; - * for(upb_symtab_begin(&i, s, type); !upb_symtab_done(&i); - * upb_symtab_next(&i)) { - * const upb_def *def = upb_symtab_iter_def(&i); - * // ... - * } - * - * For C we don't have separate iterators for const and non-const. - * It is the caller's responsibility to cast the upb_fielddef* to - * const if the upb_msgdef* is const. */ -void upb_symtab_begin(upb_symtab_iter *iter, const upb_symtab *s, - upb_deftype_t type); -void upb_symtab_next(upb_symtab_iter *iter); -bool upb_symtab_done(const upb_symtab_iter *iter); -const upb_def *upb_symtab_iter_def(const upb_symtab_iter *iter); +bool upb_symtab_addfile(upb_symtab *s, const char *buf, size_t len, + upb_status *status); +bool upb_symtab_addset(upb_symtab *s, const char *buf, size_t len, + upb_status *status); UPB_END_EXTERN_C @@ -1561,23 +782,12 @@ inline SymbolTable* SymbolTable::New() { inline void SymbolTable::Free(SymbolTable* s) { upb_symtab_free(s); } -inline const Def *SymbolTable::Resolve(const char *base, - const char *sym) const { - return upb_symtab_resolve(this, base, sym); -} inline const Def* SymbolTable::Lookup(const char *sym) const { return upb_symtab_lookup(this, sym); } inline const MessageDef *SymbolTable::LookupMessage(const char *sym) const { return upb_symtab_lookupmsg(this, sym); } -inline bool SymbolTable::Add( - Def*const* defs, size_t n, void* ref_donor, Status* status) { - return upb_symtab_add(this, (upb_def*const*)defs, n, ref_donor, status); -} -inline bool SymbolTable::AddFile(FileDef* file, Status* s) { - return upb_symtab_addfile(this, file, s); -} } /* namespace upb */ #endif @@ -1594,64 +804,10 @@ namespace upb { inline Def::Type Def::def_type() const { return upb_def_type(this); } inline const char* Def::full_name() const { return upb_def_fullname(this); } inline const char* Def::name() const { return upb_def_name(this); } -inline bool Def::set_full_name(const char* fullname, Status* s) { - return upb_def_setfullname(this, fullname, s); -} -inline bool Def::set_full_name(const std::string& fullname, Status* s) { - return upb_def_setfullname(this, upb_safecstr(fullname), s); -} -inline bool Def::Freeze(Def* const* defs, size_t n, Status* status) { - return upb_def_freeze(defs, n, status); -} -inline bool Def::Freeze(const std::vector& defs, Status* status) { - return upb_def_freeze((Def* const*)&defs[0], defs.size(), status); -} -inline bool FieldDef::CheckType(int32_t val) { - return upb_fielddef_checktype(val); -} -inline bool FieldDef::CheckLabel(int32_t val) { - return upb_fielddef_checklabel(val); -} -inline bool FieldDef::CheckDescriptorType(int32_t val) { - return upb_fielddef_checkdescriptortype(val); -} -inline bool FieldDef::CheckIntegerFormat(int32_t val) { - return upb_fielddef_checkintfmt(val); -} -inline FieldDef::Type FieldDef::ConvertType(int32_t val) { - UPB_ASSERT(CheckType(val)); - return static_cast(val); -} -inline FieldDef::Label FieldDef::ConvertLabel(int32_t val) { - UPB_ASSERT(CheckLabel(val)); - return static_cast(val); -} -inline FieldDef::DescriptorType FieldDef::ConvertDescriptorType(int32_t val) { - UPB_ASSERT(CheckDescriptorType(val)); - return static_cast(val); -} -inline FieldDef::IntegerFormat FieldDef::ConvertIntegerFormat(int32_t val) { - UPB_ASSERT(CheckIntegerFormat(val)); - return static_cast(val); -} - -inline reffed_ptr FieldDef::New() { - upb_fielddef *f = upb_fielddef_new(&f); - return reffed_ptr(f, &f); -} inline const char* FieldDef::full_name() const { return upb_fielddef_fullname(this); } -inline bool FieldDef::set_full_name(const char* fullname, Status* s) { - return upb_fielddef_setfullname(this, fullname, s); -} -inline bool FieldDef::set_full_name(const std::string& fullname, Status* s) { - return upb_fielddef_setfullname(this, upb_safecstr(fullname), s); -} -inline bool FieldDef::type_is_set() const { - return upb_fielddef_typeisset(this); -} inline FieldDef::Type FieldDef::type() const { return upb_fielddef_type(this); } inline FieldDef::DescriptorType FieldDef::descriptor_type() const { return upb_fielddef_descriptortype(this); @@ -1670,64 +826,21 @@ inline size_t FieldDef::GetJsonName(char* buf, size_t len) const { inline bool FieldDef::lazy() const { return upb_fielddef_lazy(this); } -inline void FieldDef::set_lazy(bool lazy) { - upb_fielddef_setlazy(this, lazy); -} inline bool FieldDef::packed() const { return upb_fielddef_packed(this); } inline uint32_t FieldDef::index() const { return upb_fielddef_index(this); } -inline void FieldDef::set_packed(bool packed) { - upb_fielddef_setpacked(this, packed); -} inline const MessageDef* FieldDef::containing_type() const { return upb_fielddef_containingtype(this); } inline const OneofDef* FieldDef::containing_oneof() const { return upb_fielddef_containingoneof(this); } -inline const char* FieldDef::containing_type_name() { - return upb_fielddef_containingtypename(this); -} -inline bool FieldDef::set_number(uint32_t number, Status* s) { - return upb_fielddef_setnumber(this, number, s); -} -inline bool FieldDef::set_name(const char *name, Status* s) { - return upb_fielddef_setname(this, name, s); -} -inline bool FieldDef::set_name(const std::string& name, Status* s) { - return upb_fielddef_setname(this, upb_safecstr(name), s); -} -inline bool FieldDef::set_json_name(const char *name, Status* s) { - return upb_fielddef_setjsonname(this, name, s); -} -inline bool FieldDef::set_json_name(const std::string& name, Status* s) { - return upb_fielddef_setjsonname(this, upb_safecstr(name), s); -} inline void FieldDef::clear_json_name() { upb_fielddef_clearjsonname(this); } -inline bool FieldDef::set_containing_type_name(const char *name, Status* s) { - return upb_fielddef_setcontainingtypename(this, name, s); -} -inline bool FieldDef::set_containing_type_name(const std::string &name, - Status *s) { - return upb_fielddef_setcontainingtypename(this, upb_safecstr(name), s); -} -inline void FieldDef::set_type(upb_fieldtype_t type) { - upb_fielddef_settype(this, type); -} -inline void FieldDef::set_is_extension(bool is_extension) { - upb_fielddef_setisextension(this, is_extension); -} -inline void FieldDef::set_descriptor_type(FieldDef::DescriptorType type) { - upb_fielddef_setdescriptortype(this, type); -} -inline void FieldDef::set_label(upb_label_t label) { - upb_fielddef_setlabel(this, label); -} inline bool FieldDef::IsSubMessage() const { return upb_fielddef_issubmsg(this); } @@ -1758,37 +871,6 @@ inline double FieldDef::default_double() const { inline const char* FieldDef::default_string(size_t* len) const { return upb_fielddef_defaultstr(this, len); } -inline void FieldDef::set_default_int64(int64_t value) { - upb_fielddef_setdefaultint64(this, value); -} -inline void FieldDef::set_default_int32(int32_t value) { - upb_fielddef_setdefaultint32(this, value); -} -inline void FieldDef::set_default_uint64(uint64_t value) { - upb_fielddef_setdefaultuint64(this, value); -} -inline void FieldDef::set_default_uint32(uint32_t value) { - upb_fielddef_setdefaultuint32(this, value); -} -inline void FieldDef::set_default_bool(bool value) { - upb_fielddef_setdefaultbool(this, value); -} -inline void FieldDef::set_default_float(float value) { - upb_fielddef_setdefaultfloat(this, value); -} -inline void FieldDef::set_default_double(double value) { - upb_fielddef_setdefaultdouble(this, value); -} -inline bool FieldDef::set_default_string(const void *str, size_t len, - Status *s) { - return upb_fielddef_setdefaultstr(this, str, len, s); -} -inline bool FieldDef::set_default_string(const std::string& str, Status* s) { - return upb_fielddef_setdefaultstr(this, str.c_str(), str.size(), s); -} -inline void FieldDef::set_default_cstr(const char* str, Status* s) { - return upb_fielddef_setdefaultcstr(this, str, s); -} inline bool FieldDef::HasSubDef() const { return upb_fielddef_hassubdef(this); } inline const Def* FieldDef::subdef() const { return upb_fielddef_subdef(this); } inline const MessageDef *FieldDef::message_subdef() const { @@ -1800,26 +882,7 @@ inline const EnumDef *FieldDef::enum_subdef() const { inline const char* FieldDef::subdef_name() const { return upb_fielddef_subdefname(this); } -inline bool FieldDef::set_subdef(const Def* subdef, Status* s) { - return upb_fielddef_setsubdef(this, subdef, s); -} -inline bool FieldDef::set_enum_subdef(const EnumDef* subdef, Status* s) { - return upb_fielddef_setenumsubdef(this, subdef, s); -} -inline bool FieldDef::set_message_subdef(const MessageDef* subdef, Status* s) { - return upb_fielddef_setmsgsubdef(this, subdef, s); -} -inline bool FieldDef::set_subdef_name(const char* name, Status* s) { - return upb_fielddef_setsubdefname(this, name, s); -} -inline bool FieldDef::set_subdef_name(const std::string& name, Status* s) { - return upb_fielddef_setsubdefname(this, upb_safecstr(name), s); -} -inline reffed_ptr MessageDef::New() { - upb_msgdef *m = upb_msgdef_new(&m); - return reffed_ptr(m, &m); -} inline const char *MessageDef::full_name() const { return upb_msgdef_fullname(this); } @@ -1829,42 +892,12 @@ inline const char *MessageDef::name() const { inline upb_syntax_t MessageDef::syntax() const { return upb_msgdef_syntax(this); } -inline bool MessageDef::set_full_name(const char* fullname, Status* s) { - return upb_msgdef_setfullname(this, fullname, s); -} -inline bool MessageDef::set_full_name(const std::string& fullname, Status* s) { - return upb_msgdef_setfullname(this, upb_safecstr(fullname), s); -} -inline bool MessageDef::set_syntax(upb_syntax_t syntax) { - return upb_msgdef_setsyntax(this, syntax); -} -inline bool MessageDef::Freeze(Status* status) { - return upb_msgdef_freeze(this, status); -} inline int MessageDef::field_count() const { return upb_msgdef_numfields(this); } inline int MessageDef::oneof_count() const { return upb_msgdef_numoneofs(this); } -inline bool MessageDef::AddField(upb_fielddef* f, Status* s) { - return upb_msgdef_addfield(this, f, NULL, s); -} -inline bool MessageDef::AddField(const reffed_ptr& f, Status* s) { - return upb_msgdef_addfield(this, f.get(), NULL, s); -} -inline bool MessageDef::AddOneof(upb_oneofdef* o, Status* s) { - return upb_msgdef_addoneof(this, o, NULL, s); -} -inline bool MessageDef::AddOneof(const reffed_ptr& o, Status* s) { - return upb_msgdef_addoneof(this, o.get(), NULL, s); -} -inline FieldDef* MessageDef::FindFieldByNumber(uint32_t number) { - return upb_msgdef_itof_mutable(this, number); -} -inline FieldDef* MessageDef::FindFieldByName(const char* name, size_t len) { - return upb_msgdef_ntof_mutable(this, name, len); -} inline const FieldDef* MessageDef::FindFieldByNumber(uint32_t number) const { return upb_msgdef_itof(this, number); } @@ -1872,16 +905,10 @@ inline const FieldDef *MessageDef::FindFieldByName(const char *name, size_t len) const { return upb_msgdef_ntof(this, name, len); } -inline OneofDef* MessageDef::FindOneofByName(const char* name, size_t len) { - return upb_msgdef_ntoo_mutable(this, name, len); -} inline const OneofDef* MessageDef::FindOneofByName(const char* name, size_t len) const { return upb_msgdef_ntoo(this, name, len); } -inline void MessageDef::setmapentry(bool map_entry) { - upb_msgdef_setmapentry(this, map_entry); -} inline bool MessageDef::mapentry() const { return upb_msgdef_mapentry(this); } @@ -1900,12 +927,6 @@ inline bool MessageDef::listvalue() const { inline bool MessageDef::structvalue() const { return upb_msgdef_structvalue(this); } -inline MessageDef::field_iterator MessageDef::field_begin() { - return field_iterator(this); -} -inline MessageDef::field_iterator MessageDef::field_end() { - return field_iterator::end(this); -} inline MessageDef::const_field_iterator MessageDef::field_begin() const { return const_field_iterator(this); } @@ -1913,12 +934,6 @@ inline MessageDef::const_field_iterator MessageDef::field_end() const { return const_field_iterator::end(this); } -inline MessageDef::oneof_iterator MessageDef::oneof_begin() { - return oneof_iterator(this); -} -inline MessageDef::oneof_iterator MessageDef::oneof_end() { - return oneof_iterator::end(this); -} inline MessageDef::const_oneof_iterator MessageDef::oneof_begin() const { return const_oneof_iterator(this); } @@ -1926,30 +941,6 @@ inline MessageDef::const_oneof_iterator MessageDef::oneof_end() const { return const_oneof_iterator::end(this); } -inline MessageDef::field_iterator::field_iterator(MessageDef* md) { - upb_msg_field_begin(&iter_, md); -} -inline MessageDef::field_iterator MessageDef::field_iterator::end( - MessageDef* md) { - MessageDef::field_iterator iter(md); - upb_msg_field_iter_setdone(&iter.iter_); - return iter; -} -inline FieldDef* MessageDef::field_iterator::operator*() const { - return upb_msg_iter_field(&iter_); -} -inline void MessageDef::field_iterator::operator++() { - return upb_msg_field_next(&iter_); -} -inline bool MessageDef::field_iterator::operator==( - const field_iterator &other) const { - return upb_inttable_iter_isequal(&iter_, &other.iter_); -} -inline bool MessageDef::field_iterator::operator!=( - const field_iterator &other) const { - return !(*this == other); -} - inline MessageDef::const_field_iterator::const_field_iterator( const MessageDef* md) { upb_msg_field_begin(&iter_, md); @@ -1975,30 +966,6 @@ inline bool MessageDef::const_field_iterator::operator!=( return !(*this == other); } -inline MessageDef::oneof_iterator::oneof_iterator(MessageDef* md) { - upb_msg_oneof_begin(&iter_, md); -} -inline MessageDef::oneof_iterator MessageDef::oneof_iterator::end( - MessageDef* md) { - MessageDef::oneof_iterator iter(md); - upb_msg_oneof_iter_setdone(&iter.iter_); - return iter; -} -inline OneofDef* MessageDef::oneof_iterator::operator*() const { - return upb_msg_iter_oneof(&iter_); -} -inline void MessageDef::oneof_iterator::operator++() { - return upb_msg_oneof_next(&iter_); -} -inline bool MessageDef::oneof_iterator::operator==( - const oneof_iterator &other) const { - return upb_strtable_iter_isequal(&iter_, &other.iter_); -} -inline bool MessageDef::oneof_iterator::operator!=( - const oneof_iterator &other) const { - return !(*this == other); -} - inline MessageDef::const_oneof_iterator::const_oneof_iterator( const MessageDef* md) { upb_msg_oneof_begin(&iter_, md); @@ -2024,39 +991,16 @@ inline bool MessageDef::const_oneof_iterator::operator!=( return !(*this == other); } -inline reffed_ptr EnumDef::New() { - upb_enumdef *e = upb_enumdef_new(&e); - return reffed_ptr(e, &e); -} inline const char* EnumDef::full_name() const { return upb_enumdef_fullname(this); } inline const char* EnumDef::name() const { return upb_enumdef_name(this); } -inline bool EnumDef::set_full_name(const char* fullname, Status* s) { - return upb_enumdef_setfullname(this, fullname, s); -} -inline bool EnumDef::set_full_name(const std::string& fullname, Status* s) { - return upb_enumdef_setfullname(this, upb_safecstr(fullname), s); -} -inline bool EnumDef::Freeze(Status* status) { - return upb_enumdef_freeze(this, status); -} inline int32_t EnumDef::default_value() const { return upb_enumdef_default(this); } -inline bool EnumDef::set_default_value(int32_t val, Status* status) { - return upb_enumdef_setdefault(this, val, status); -} inline int EnumDef::value_count() const { return upb_enumdef_numvals(this); } -inline bool EnumDef::AddValue(const char* name, int32_t num, Status* status) { - return upb_enumdef_addval(this, name, num, status); -} -inline bool EnumDef::AddValue(const std::string& name, int32_t num, - Status* status) { - return upb_enumdef_addval(this, upb_safecstr(name), num, status); -} inline bool EnumDef::FindValueByName(const char* name, int32_t *num) const { return upb_enumdef_ntoiz(this, name, num); } @@ -2076,32 +1020,15 @@ inline const char* EnumDef::Iterator::name() { inline bool EnumDef::Iterator::Done() { return upb_enum_done(&iter_); } inline void EnumDef::Iterator::Next() { return upb_enum_next(&iter_); } -inline reffed_ptr OneofDef::New() { - upb_oneofdef *o = upb_oneofdef_new(&o); - return reffed_ptr(o, &o); -} - inline const MessageDef* OneofDef::containing_type() const { return upb_oneofdef_containingtype(this); } inline const char* OneofDef::name() const { return upb_oneofdef_name(this); } -inline bool OneofDef::set_name(const char* name, Status* s) { - return upb_oneofdef_setname(this, name, s); -} -inline bool OneofDef::set_name(const std::string& name, Status* s) { - return upb_oneofdef_setname(this, upb_safecstr(name), s); -} inline int OneofDef::field_count() const { return upb_oneofdef_numfields(this); } -inline bool OneofDef::AddField(FieldDef* field, Status* s) { - return upb_oneofdef_addfield(this, field, NULL, s); -} -inline bool OneofDef::AddField(const reffed_ptr& field, Status* s) { - return upb_oneofdef_addfield(this, field.get(), NULL, s); -} inline const FieldDef* OneofDef::FindFieldByName(const char* name, size_t len) const { return upb_oneofdef_ntof(this, name, len); @@ -2109,8 +1036,6 @@ inline const FieldDef* OneofDef::FindFieldByName(const char* name, inline const FieldDef* OneofDef::FindFieldByNumber(uint32_t num) const { return upb_oneofdef_itof(this, num); } -inline OneofDef::iterator OneofDef::begin() { return iterator(this); } -inline OneofDef::iterator OneofDef::end() { return iterator::end(this); } inline OneofDef::const_iterator OneofDef::begin() const { return const_iterator(this); } @@ -2118,25 +1043,6 @@ inline OneofDef::const_iterator OneofDef::end() const { return const_iterator::end(this); } -inline OneofDef::iterator::iterator(OneofDef* o) { - upb_oneof_begin(&iter_, o); -} -inline OneofDef::iterator OneofDef::iterator::end(OneofDef* o) { - OneofDef::iterator iter(o); - upb_oneof_iter_setdone(&iter.iter_); - return iter; -} -inline FieldDef* OneofDef::iterator::operator*() const { - return upb_oneof_iter_field(&iter_); -} -inline void OneofDef::iterator::operator++() { return upb_oneof_next(&iter_); } -inline bool OneofDef::iterator::operator==(const iterator &other) const { - return upb_inttable_iter_isequal(&iter_, &other.iter_); -} -inline bool OneofDef::iterator::operator!=(const iterator &other) const { - return !(*this == other); -} - inline OneofDef::const_iterator::const_iterator(const OneofDef* md) { upb_oneof_begin(&iter_, md); } @@ -2161,38 +1067,18 @@ inline bool OneofDef::const_iterator::operator!=( return !(*this == other); } -inline reffed_ptr FileDef::New() { - upb_filedef *f = upb_filedef_new(&f); - return reffed_ptr(f, &f); -} - inline const char* FileDef::name() const { return upb_filedef_name(this); } -inline bool FileDef::set_name(const char* name, Status* s) { - return upb_filedef_setname(this, name, s); -} -inline bool FileDef::set_name(const std::string& name, Status* s) { - return upb_filedef_setname(this, upb_safecstr(name), s); -} inline const char* FileDef::package() const { return upb_filedef_package(this); } -inline bool FileDef::set_package(const char* package, Status* s) { - return upb_filedef_setpackage(this, package, s); -} inline const char* FileDef::phpprefix() const { return upb_filedef_phpprefix(this); } -inline bool FileDef::set_phpprefix(const char* phpprefix, Status* s) { - return upb_filedef_setphpprefix(this, phpprefix, s); -} inline const char* FileDef::phpnamespace() const { return upb_filedef_phpnamespace(this); } -inline bool FileDef::set_phpnamespace(const char* phpnamespace, Status* s) { - return upb_filedef_setphpnamespace(this, phpnamespace, s); -} inline int FileDef::def_count() const { return upb_filedef_defcount(this); } @@ -2208,21 +1094,6 @@ inline int FileDef::dependency_count() const { inline const FileDef* FileDef::dependency(int index) const { return upb_filedef_dep(this, index); } -inline bool FileDef::AddDef(Def* def, Status* s) { - return upb_filedef_adddef(this, def, NULL, s); -} -inline bool FileDef::AddMessage(MessageDef* m, Status* s) { - return upb_filedef_addmsg(this, m, NULL, s); -} -inline bool FileDef::AddEnum(EnumDef* e, Status* s) { - return upb_filedef_addenum(this, e, NULL, s); -} -inline bool FileDef::AddExtension(FieldDef* f, Status* s) { - return upb_filedef_addext(this, f, NULL, s); -} -inline bool FileDef::AddDependency(const FileDef* file) { - return upb_filedef_adddep(this, file); -} } /* namespace upb */ #endif diff --git a/upb/descriptor/descriptor.pb b/upb/descriptor/descriptor.pb deleted file mode 100644 index 74bc3bc..0000000 Binary files a/upb/descriptor/descriptor.pb and /dev/null differ diff --git a/upb/descriptor/descriptor.proto b/upb/descriptor/descriptor.proto deleted file mode 100644 index b6e303e..0000000 --- a/upb/descriptor/descriptor.proto +++ /dev/null @@ -1,788 +0,0 @@ -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -// Author: kenton@google.com (Kenton Varda) -// Based on original Protocol Buffers design by -// Sanjay Ghemawat, Jeff Dean, and others. -// -// The messages in this file describe the definitions found in .proto files. -// A valid .proto file can be translated directly to a FileDescriptorProto -// without any other information (e.g. without reading its imports). - - -syntax = "proto2"; - -package google.protobuf; -option go_package = "descriptor"; -option java_package = "com.google.protobuf"; -option java_outer_classname = "DescriptorProtos"; -option csharp_namespace = "Google.Protobuf.Reflection"; -option objc_class_prefix = "GPB"; - -// descriptor.proto must be optimized for speed because reflection-based -// algorithms don't work during bootstrapping. -option optimize_for = SPEED; - -// The protocol compiler can output a FileDescriptorSet containing the .proto -// files it parses. -message FileDescriptorSet { - repeated FileDescriptorProto file = 1; -} - -// Describes a complete .proto file. -message FileDescriptorProto { - optional string name = 1; // file name, relative to root of source tree - optional string package = 2; // e.g. "foo", "foo.bar", etc. - - // Names of files imported by this file. - repeated string dependency = 3; - // Indexes of the public imported files in the dependency list above. - repeated int32 public_dependency = 10; - // Indexes of the weak imported files in the dependency list. - // For Google-internal migration only. Do not use. - repeated int32 weak_dependency = 11; - - // All top-level definitions in this file. - repeated DescriptorProto message_type = 4; - repeated EnumDescriptorProto enum_type = 5; - repeated ServiceDescriptorProto service = 6; - repeated FieldDescriptorProto extension = 7; - - optional FileOptions options = 8; - - // This field contains optional information about the original source code. - // You may safely remove this entire field without harming runtime - // functionality of the descriptors -- the information is needed only by - // development tools. - optional SourceCodeInfo source_code_info = 9; - - // The syntax of the proto file. - // The supported values are "proto2" and "proto3". - optional string syntax = 12; -} - -// Describes a message type. -message DescriptorProto { - optional string name = 1; - - repeated FieldDescriptorProto field = 2; - repeated FieldDescriptorProto extension = 6; - - repeated DescriptorProto nested_type = 3; - repeated EnumDescriptorProto enum_type = 4; - - message ExtensionRange { - optional int32 start = 1; - optional int32 end = 2; - } - repeated ExtensionRange extension_range = 5; - - repeated OneofDescriptorProto oneof_decl = 8; - - optional MessageOptions options = 7; - - // Range of reserved tag numbers. Reserved tag numbers may not be used by - // fields or extension ranges in the same message. Reserved ranges may - // not overlap. - message ReservedRange { - optional int32 start = 1; // Inclusive. - optional int32 end = 2; // Exclusive. - } - repeated ReservedRange reserved_range = 9; - // Reserved field names, which may not be used by fields in the same message. - // A given name may only be reserved once. - repeated string reserved_name = 10; -} - -// Describes a field within a message. -message FieldDescriptorProto { - enum Type { - // 0 is reserved for errors. - // Order is weird for historical reasons. - TYPE_DOUBLE = 1; - TYPE_FLOAT = 2; - // Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT64 if - // negative values are likely. - TYPE_INT64 = 3; - TYPE_UINT64 = 4; - // Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT32 if - // negative values are likely. - TYPE_INT32 = 5; - TYPE_FIXED64 = 6; - TYPE_FIXED32 = 7; - TYPE_BOOL = 8; - TYPE_STRING = 9; - TYPE_GROUP = 10; // Tag-delimited aggregate. - TYPE_MESSAGE = 11; // Length-delimited aggregate. - - // New in version 2. - TYPE_BYTES = 12; - TYPE_UINT32 = 13; - TYPE_ENUM = 14; - TYPE_SFIXED32 = 15; - TYPE_SFIXED64 = 16; - TYPE_SINT32 = 17; // Uses ZigZag encoding. - TYPE_SINT64 = 18; // Uses ZigZag encoding. - }; - - enum Label { - // 0 is reserved for errors - LABEL_OPTIONAL = 1; - LABEL_REQUIRED = 2; - LABEL_REPEATED = 3; - // TODO(sanjay): Should we add LABEL_MAP? - }; - - optional string name = 1; - optional int32 number = 3; - optional Label label = 4; - - // If type_name is set, this need not be set. If both this and type_name - // are set, this must be one of TYPE_ENUM, TYPE_MESSAGE or TYPE_GROUP. - optional Type type = 5; - - // For message and enum types, this is the name of the type. If the name - // starts with a '.', it is fully-qualified. Otherwise, C++-like scoping - // rules are used to find the type (i.e. first the nested types within this - // message are searched, then within the parent, on up to the root - // namespace). - optional string type_name = 6; - - // For extensions, this is the name of the type being extended. It is - // resolved in the same manner as type_name. - optional string extendee = 2; - - // For numeric types, contains the original text representation of the value. - // For booleans, "true" or "false". - // For strings, contains the default text contents (not escaped in any way). - // For bytes, contains the C escaped value. All bytes >= 128 are escaped. - // TODO(kenton): Base-64 encode? - optional string default_value = 7; - - // If set, gives the index of a oneof in the containing type's oneof_decl - // list. This field is a member of that oneof. - optional int32 oneof_index = 9; - - // JSON name of this field. The value is set by protocol compiler. If the - // user has set a "json_name" option on this field, that option's value - // will be used. Otherwise, it's deduced from the field's name by converting - // it to camelCase. - optional string json_name = 10; - - optional FieldOptions options = 8; -} - -// Describes a oneof. -message OneofDescriptorProto { - optional string name = 1; -} - -// Describes an enum type. -message EnumDescriptorProto { - optional string name = 1; - - repeated EnumValueDescriptorProto value = 2; - - optional EnumOptions options = 3; -} - -// Describes a value within an enum. -message EnumValueDescriptorProto { - optional string name = 1; - optional int32 number = 2; - - optional EnumValueOptions options = 3; -} - -// Describes a service. -message ServiceDescriptorProto { - optional string name = 1; - repeated MethodDescriptorProto method = 2; - - optional ServiceOptions options = 3; -} - -// Describes a method of a service. -message MethodDescriptorProto { - optional string name = 1; - - // Input and output type names. These are resolved in the same way as - // FieldDescriptorProto.type_name, but must refer to a message type. - optional string input_type = 2; - optional string output_type = 3; - - optional MethodOptions options = 4; - - // Identifies if client streams multiple client messages - optional bool client_streaming = 5 [default=false]; - // Identifies if server streams multiple server messages - optional bool server_streaming = 6 [default=false]; -} - - -// =================================================================== -// Options - -// Each of the definitions above may have "options" attached. These are -// just annotations which may cause code to be generated slightly differently -// or may contain hints for code that manipulates protocol messages. -// -// Clients may define custom options as extensions of the *Options messages. -// These extensions may not yet be known at parsing time, so the parser cannot -// store the values in them. Instead it stores them in a field in the *Options -// message called uninterpreted_option. This field must have the same name -// across all *Options messages. We then use this field to populate the -// extensions when we build a descriptor, at which point all protos have been -// parsed and so all extensions are known. -// -// Extension numbers for custom options may be chosen as follows: -// * For options which will only be used within a single application or -// organization, or for experimental options, use field numbers 50000 -// through 99999. It is up to you to ensure that you do not use the -// same number for multiple options. -// * For options which will be published and used publicly by multiple -// independent entities, e-mail protobuf-global-extension-registry@google.com -// to reserve extension numbers. Simply provide your project name (e.g. -// Objective-C plugin) and your project website (if available) -- there's no -// need to explain how you intend to use them. Usually you only need one -// extension number. You can declare multiple options with only one extension -// number by putting them in a sub-message. See the Custom Options section of -// the docs for examples: -// https://developers.google.com/protocol-buffers/docs/proto#options -// If this turns out to be popular, a web service will be set up -// to automatically assign option numbers. - - -message FileOptions { - - // Sets the Java package where classes generated from this .proto will be - // placed. By default, the proto package is used, but this is often - // inappropriate because proto packages do not normally start with backwards - // domain names. - optional string java_package = 1; - - - // If set, all the classes from the .proto file are wrapped in a single - // outer class with the given name. This applies to both Proto1 - // (equivalent to the old "--one_java_file" option) and Proto2 (where - // a .proto always translates to a single class, but you may want to - // explicitly choose the class name). - optional string java_outer_classname = 8; - - // If set true, then the Java code generator will generate a separate .java - // file for each top-level message, enum, and service defined in the .proto - // file. Thus, these types will *not* be nested inside the outer class - // named by java_outer_classname. However, the outer class will still be - // generated to contain the file's getDescriptor() method as well as any - // top-level extensions defined in the file. - optional bool java_multiple_files = 10 [default=false]; - - // If set true, then the Java code generator will generate equals() and - // hashCode() methods for all messages defined in the .proto file. - // This increases generated code size, potentially substantially for large - // protos, which may harm a memory-constrained application. - // - In the full runtime this is a speed optimization, as the - // AbstractMessage base class includes reflection-based implementations of - // these methods. - // - In the lite runtime, setting this option changes the semantics of - // equals() and hashCode() to more closely match those of the full runtime; - // the generated methods compute their results based on field values rather - // than object identity. (Implementations should not assume that hashcodes - // will be consistent across runtimes or versions of the protocol compiler.) - optional bool java_generate_equals_and_hash = 20 [default=false]; - - // If set true, then the Java2 code generator will generate code that - // throws an exception whenever an attempt is made to assign a non-UTF-8 - // byte sequence to a string field. - // Message reflection will do the same. - // However, an extension field still accepts non-UTF-8 byte sequences. - // This option has no effect on when used with the lite runtime. - optional bool java_string_check_utf8 = 27 [default=false]; - - - // Generated classes can be optimized for speed or code size. - enum OptimizeMode { - SPEED = 1; // Generate complete code for parsing, serialization, - // etc. - CODE_SIZE = 2; // Use ReflectionOps to implement these methods. - LITE_RUNTIME = 3; // Generate code using MessageLite and the lite runtime. - } - optional OptimizeMode optimize_for = 9 [default=SPEED]; - - // Sets the Go package where structs generated from this .proto will be - // placed. If omitted, the Go package will be derived from the following: - // - The basename of the package import path, if provided. - // - Otherwise, the package statement in the .proto file, if present. - // - Otherwise, the basename of the .proto file, without extension. - optional string go_package = 11; - - - - // Should generic services be generated in each language? "Generic" services - // are not specific to any particular RPC system. They are generated by the - // main code generators in each language (without additional plugins). - // Generic services were the only kind of service generation supported by - // early versions of google.protobuf. - // - // Generic services are now considered deprecated in favor of using plugins - // that generate code specific to your particular RPC system. Therefore, - // these default to false. Old code which depends on generic services should - // explicitly set them to true. - optional bool cc_generic_services = 16 [default=false]; - optional bool java_generic_services = 17 [default=false]; - optional bool py_generic_services = 18 [default=false]; - - // Is this file deprecated? - // Depending on the target platform, this can emit Deprecated annotations - // for everything in the file, or it will be completely ignored; in the very - // least, this is a formalization for deprecating files. - optional bool deprecated = 23 [default=false]; - - // Enables the use of arenas for the proto messages in this file. This applies - // only to generated classes for C++. - optional bool cc_enable_arenas = 31 [default=false]; - - - // Sets the objective c class prefix which is prepended to all objective c - // generated classes from this .proto. There is no default. - optional string objc_class_prefix = 36; - - // Namespace for generated classes; defaults to the package. - optional string csharp_namespace = 37; - - // Whether the nano proto compiler should generate in the deprecated non-nano - // suffixed package. - optional bool javanano_use_deprecated_package = 38; - - // Sets the php class prefix which is prepended to all php generated classes - // from this .proto. Default is empty. - optional string php_class_prefix = 40; - - // Use this option to change the namespace of php generated classes. Default - // is empty. When this option is empty, the package name will be used for - // determining the namespace. - optional string php_namespace = 41; - - // The parser stores options it doesn't recognize here. See above. - repeated UninterpretedOption uninterpreted_option = 999; - - // Clients can define custom options in extensions of this message. See above. - extensions 1000 to max; -} - -message MessageOptions { - // Set true to use the old proto1 MessageSet wire format for extensions. - // This is provided for backwards-compatibility with the MessageSet wire - // format. You should not use this for any other reason: It's less - // efficient, has fewer features, and is more complicated. - // - // The message must be defined exactly as follows: - // message Foo { - // option message_set_wire_format = true; - // extensions 4 to max; - // } - // Note that the message cannot have any defined fields; MessageSets only - // have extensions. - // - // All extensions of your type must be singular messages; e.g. they cannot - // be int32s, enums, or repeated messages. - // - // Because this is an option, the above two restrictions are not enforced by - // the protocol compiler. - optional bool message_set_wire_format = 1 [default=false]; - - // Disables the generation of the standard "descriptor()" accessor, which can - // conflict with a field of the same name. This is meant to make migration - // from proto1 easier; new code should avoid fields named "descriptor". - optional bool no_standard_descriptor_accessor = 2 [default=false]; - - // Is this message deprecated? - // Depending on the target platform, this can emit Deprecated annotations - // for the message, or it will be completely ignored; in the very least, - // this is a formalization for deprecating messages. - optional bool deprecated = 3 [default=false]; - - // Whether the message is an automatically generated map entry type for the - // maps field. - // - // For maps fields: - // map map_field = 1; - // The parsed descriptor looks like: - // message MapFieldEntry { - // option map_entry = true; - // optional KeyType key = 1; - // optional ValueType value = 2; - // } - // repeated MapFieldEntry map_field = 1; - // - // Implementations may choose not to generate the map_entry=true message, but - // use a native map in the target language to hold the keys and values. - // The reflection APIs in such implementions still need to work as - // if the field is a repeated message field. - // - // NOTE: Do not set the option in .proto files. Always use the maps syntax - // instead. The option should only be implicitly set by the proto compiler - // parser. - optional bool map_entry = 7; - - // The parser stores options it doesn't recognize here. See above. - repeated UninterpretedOption uninterpreted_option = 999; - - // Clients can define custom options in extensions of this message. See above. - extensions 1000 to max; -} - -message FieldOptions { - // The ctype option instructs the C++ code generator to use a different - // representation of the field than it normally would. See the specific - // options below. This option is not yet implemented in the open source - // release -- sorry, we'll try to include it in a future version! - optional CType ctype = 1 [default = STRING]; - enum CType { - // Default mode. - STRING = 0; - - CORD = 1; - - STRING_PIECE = 2; - } - // The packed option can be enabled for repeated primitive fields to enable - // a more efficient representation on the wire. Rather than repeatedly - // writing the tag and type for each element, the entire array is encoded as - // a single length-delimited blob. In proto3, only explicit setting it to - // false will avoid using packed encoding. - optional bool packed = 2; - - - // The jstype option determines the JavaScript type used for values of the - // field. The option is permitted only for 64 bit integral and fixed types - // (int64, uint64, sint64, fixed64, sfixed64). By default these types are - // represented as JavaScript strings. This avoids loss of precision that can - // happen when a large value is converted to a floating point JavaScript - // numbers. Specifying JS_NUMBER for the jstype causes the generated - // JavaScript code to use the JavaScript "number" type instead of strings. - // This option is an enum to permit additional types to be added, - // e.g. goog.math.Integer. - optional JSType jstype = 6 [default = JS_NORMAL]; - enum JSType { - // Use the default type. - JS_NORMAL = 0; - - // Use JavaScript strings. - JS_STRING = 1; - - // Use JavaScript numbers. - JS_NUMBER = 2; - } - - // Should this field be parsed lazily? Lazy applies only to message-type - // fields. It means that when the outer message is initially parsed, the - // inner message's contents will not be parsed but instead stored in encoded - // form. The inner message will actually be parsed when it is first accessed. - // - // This is only a hint. Implementations are free to choose whether to use - // eager or lazy parsing regardless of the value of this option. However, - // setting this option true suggests that the protocol author believes that - // using lazy parsing on this field is worth the additional bookkeeping - // overhead typically needed to implement it. - // - // This option does not affect the public interface of any generated code; - // all method signatures remain the same. Furthermore, thread-safety of the - // interface is not affected by this option; const methods remain safe to - // call from multiple threads concurrently, while non-const methods continue - // to require exclusive access. - // - // - // Note that implementations may choose not to check required fields within - // a lazy sub-message. That is, calling IsInitialized() on the outher message - // may return true even if the inner message has missing required fields. - // This is necessary because otherwise the inner message would have to be - // parsed in order to perform the check, defeating the purpose of lazy - // parsing. An implementation which chooses not to check required fields - // must be consistent about it. That is, for any particular sub-message, the - // implementation must either *always* check its required fields, or *never* - // check its required fields, regardless of whether or not the message has - // been parsed. - optional bool lazy = 5 [default=false]; - - // Is this field deprecated? - // Depending on the target platform, this can emit Deprecated annotations - // for accessors, or it will be completely ignored; in the very least, this - // is a formalization for deprecating fields. - optional bool deprecated = 3 [default=false]; - - // For Google-internal migration only. Do not use. - optional bool weak = 10 [default=false]; - - - // The parser stores options it doesn't recognize here. See above. - repeated UninterpretedOption uninterpreted_option = 999; - - // Clients can define custom options in extensions of this message. See above. - extensions 1000 to max; -} - -message EnumOptions { - - // Set this option to true to allow mapping different tag names to the same - // value. - optional bool allow_alias = 2; - - // Is this enum deprecated? - // Depending on the target platform, this can emit Deprecated annotations - // for the enum, or it will be completely ignored; in the very least, this - // is a formalization for deprecating enums. - optional bool deprecated = 3 [default=false]; - - // The parser stores options it doesn't recognize here. See above. - repeated UninterpretedOption uninterpreted_option = 999; - - // Clients can define custom options in extensions of this message. See above. - extensions 1000 to max; -} - -message EnumValueOptions { - // Is this enum value deprecated? - // Depending on the target platform, this can emit Deprecated annotations - // for the enum value, or it will be completely ignored; in the very least, - // this is a formalization for deprecating enum values. - optional bool deprecated = 1 [default=false]; - - // The parser stores options it doesn't recognize here. See above. - repeated UninterpretedOption uninterpreted_option = 999; - - // Clients can define custom options in extensions of this message. See above. - extensions 1000 to max; -} - -message ServiceOptions { - - // Note: Field numbers 1 through 32 are reserved for Google's internal RPC - // framework. We apologize for hoarding these numbers to ourselves, but - // we were already using them long before we decided to release Protocol - // Buffers. - - // Is this service deprecated? - // Depending on the target platform, this can emit Deprecated annotations - // for the service, or it will be completely ignored; in the very least, - // this is a formalization for deprecating services. - optional bool deprecated = 33 [default=false]; - - // The parser stores options it doesn't recognize here. See above. - repeated UninterpretedOption uninterpreted_option = 999; - - // Clients can define custom options in extensions of this message. See above. - extensions 1000 to max; -} - -message MethodOptions { - - // Note: Field numbers 1 through 32 are reserved for Google's internal RPC - // framework. We apologize for hoarding these numbers to ourselves, but - // we were already using them long before we decided to release Protocol - // Buffers. - - // Is this method deprecated? - // Depending on the target platform, this can emit Deprecated annotations - // for the method, or it will be completely ignored; in the very least, - // this is a formalization for deprecating methods. - optional bool deprecated = 33 [default=false]; - - // The parser stores options it doesn't recognize here. See above. - repeated UninterpretedOption uninterpreted_option = 999; - - // Clients can define custom options in extensions of this message. See above. - extensions 1000 to max; -} - - -// A message representing a option the parser does not recognize. This only -// appears in options protos created by the compiler::Parser class. -// DescriptorPool resolves these when building Descriptor objects. Therefore, -// options protos in descriptor objects (e.g. returned by Descriptor::options(), -// or produced by Descriptor::CopyTo()) will never have UninterpretedOptions -// in them. -message UninterpretedOption { - // The name of the uninterpreted option. Each string represents a segment in - // a dot-separated name. is_extension is true iff a segment represents an - // extension (denoted with parentheses in options specs in .proto files). - // E.g.,{ ["foo", false], ["bar.baz", true], ["qux", false] } represents - // "foo.(bar.baz).qux". - message NamePart { - required string name_part = 1; - required bool is_extension = 2; - } - repeated NamePart name = 2; - - // The value of the uninterpreted option, in whatever type the tokenizer - // identified it as during parsing. Exactly one of these should be set. - optional string identifier_value = 3; - optional uint64 positive_int_value = 4; - optional int64 negative_int_value = 5; - optional double double_value = 6; - optional bytes string_value = 7; - optional string aggregate_value = 8; -} - -// =================================================================== -// Optional source code info - -// Encapsulates information about the original source file from which a -// FileDescriptorProto was generated. -message SourceCodeInfo { - // A Location identifies a piece of source code in a .proto file which - // corresponds to a particular definition. This information is intended - // to be useful to IDEs, code indexers, documentation generators, and similar - // tools. - // - // For example, say we have a file like: - // message Foo { - // optional string foo = 1; - // } - // Let's look at just the field definition: - // optional string foo = 1; - // ^ ^^ ^^ ^ ^^^ - // a bc de f ghi - // We have the following locations: - // span path represents - // [a,i) [ 4, 0, 2, 0 ] The whole field definition. - // [a,b) [ 4, 0, 2, 0, 4 ] The label (optional). - // [c,d) [ 4, 0, 2, 0, 5 ] The type (string). - // [e,f) [ 4, 0, 2, 0, 1 ] The name (foo). - // [g,h) [ 4, 0, 2, 0, 3 ] The number (1). - // - // Notes: - // - A location may refer to a repeated field itself (i.e. not to any - // particular index within it). This is used whenever a set of elements are - // logically enclosed in a single code segment. For example, an entire - // extend block (possibly containing multiple extension definitions) will - // have an outer location whose path refers to the "extensions" repeated - // field without an index. - // - Multiple locations may have the same path. This happens when a single - // logical declaration is spread out across multiple places. The most - // obvious example is the "extend" block again -- there may be multiple - // extend blocks in the same scope, each of which will have the same path. - // - A location's span is not always a subset of its parent's span. For - // example, the "extendee" of an extension declaration appears at the - // beginning of the "extend" block and is shared by all extensions within - // the block. - // - Just because a location's span is a subset of some other location's span - // does not mean that it is a descendent. For example, a "group" defines - // both a type and a field in a single declaration. Thus, the locations - // corresponding to the type and field and their components will overlap. - // - Code which tries to interpret locations should probably be designed to - // ignore those that it doesn't understand, as more types of locations could - // be recorded in the future. - repeated Location location = 1; - message Location { - // Identifies which part of the FileDescriptorProto was defined at this - // location. - // - // Each element is a field number or an index. They form a path from - // the root FileDescriptorProto to the place where the definition. For - // example, this path: - // [ 4, 3, 2, 7, 1 ] - // refers to: - // file.message_type(3) // 4, 3 - // .field(7) // 2, 7 - // .name() // 1 - // This is because FileDescriptorProto.message_type has field number 4: - // repeated DescriptorProto message_type = 4; - // and DescriptorProto.field has field number 2: - // repeated FieldDescriptorProto field = 2; - // and FieldDescriptorProto.name has field number 1: - // optional string name = 1; - // - // Thus, the above path gives the location of a field name. If we removed - // the last element: - // [ 4, 3, 2, 7 ] - // this path refers to the whole field declaration (from the beginning - // of the label to the terminating semicolon). - repeated int32 path = 1 [packed=true]; - - // Always has exactly three or four elements: start line, start column, - // end line (optional, otherwise assumed same as start line), end column. - // These are packed into a single field for efficiency. Note that line - // and column numbers are zero-based -- typically you will want to add - // 1 to each before displaying to a user. - repeated int32 span = 2 [packed=true]; - - // If this SourceCodeInfo represents a complete declaration, these are any - // comments appearing before and after the declaration which appear to be - // attached to the declaration. - // - // A series of line comments appearing on consecutive lines, with no other - // tokens appearing on those lines, will be treated as a single comment. - // - // leading_detached_comments will keep paragraphs of comments that appear - // before (but not connected to) the current element. Each paragraph, - // separated by empty lines, will be one comment element in the repeated - // field. - // - // Only the comment content is provided; comment markers (e.g. //) are - // stripped out. For block comments, leading whitespace and an asterisk - // will be stripped from the beginning of each line other than the first. - // Newlines are included in the output. - // - // Examples: - // - // optional int32 foo = 1; // Comment attached to foo. - // // Comment attached to bar. - // optional int32 bar = 2; - // - // optional string baz = 3; - // // Comment attached to baz. - // // Another line attached to baz. - // - // // Comment attached to qux. - // // - // // Another line attached to qux. - // optional double qux = 4; - // - // // Detached comment for corge. This is not leading or trailing comments - // // to qux or corge because there are blank lines separating it from - // // both. - // - // // Detached comment for corge paragraph 2. - // - // optional string corge = 5; - // /* Block comment attached - // * to corge. Leading asterisks - // * will be removed. */ - // /* Block comment attached to - // * grault. */ - // optional int32 grault = 6; - // - // // ignored detached comments. - optional string leading_comments = 3; - optional string trailing_comments = 4; - repeated string leading_detached_comments = 6; - } -} diff --git a/upb/descriptor/descriptor.upbdefs.c b/upb/descriptor/descriptor.upbdefs.c deleted file mode 100644 index ceba74e..0000000 --- a/upb/descriptor/descriptor.upbdefs.c +++ /dev/null @@ -1,927 +0,0 @@ -/* This file was generated by upbc (the upb compiler) from the input - * file: - * - * upb/descriptor/descriptor.proto - * - * Do not edit -- your changes will be discarded when the file is - * regenerated. */ - -#include "upb/def.h" -#include "upb/structdefs.int.h" - -static const upb_msgdef msgs[22]; -static const upb_fielddef fields[107]; -static const upb_enumdef enums[5]; -static const upb_tabent strentries[236]; -static const upb_tabent intentries[18]; -static const upb_tabval arrays[187]; - -#ifdef UPB_DEBUG_REFS -static upb_inttable reftables[268]; -#endif - -static const upb_msgdef msgs[22] = { - UPB_MSGDEF_INIT("google.protobuf.DescriptorProto", 41, 8, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[0], 11, 10), UPB_STRTABLE_INIT(10, 15, UPB_CTYPE_PTR, 4, &strentries[0]), false, UPB_SYNTAX_PROTO2, &reftables[0], &reftables[1]), - UPB_MSGDEF_INIT("google.protobuf.DescriptorProto.ExtensionRange", 5, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[11], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[16]), false, UPB_SYNTAX_PROTO2, &reftables[2], &reftables[3]), - UPB_MSGDEF_INIT("google.protobuf.DescriptorProto.ReservedRange", 5, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[14], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[20]), false, UPB_SYNTAX_PROTO2, &reftables[4], &reftables[5]), - UPB_MSGDEF_INIT("google.protobuf.EnumDescriptorProto", 12, 2, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[17], 4, 3), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[24]), false, UPB_SYNTAX_PROTO2, &reftables[6], &reftables[7]), - UPB_MSGDEF_INIT("google.protobuf.EnumOptions", 9, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[0], &arrays[21], 4, 2), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[28]), false, UPB_SYNTAX_PROTO2, &reftables[8], &reftables[9]), - UPB_MSGDEF_INIT("google.protobuf.EnumValueDescriptorProto", 9, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[25], 4, 3), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[32]), false, UPB_SYNTAX_PROTO2, &reftables[10], &reftables[11]), - UPB_MSGDEF_INIT("google.protobuf.EnumValueOptions", 8, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[2], &arrays[29], 2, 1), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[36]), false, UPB_SYNTAX_PROTO2, &reftables[12], &reftables[13]), - UPB_MSGDEF_INIT("google.protobuf.FieldDescriptorProto", 24, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[31], 11, 10), UPB_STRTABLE_INIT(10, 15, UPB_CTYPE_PTR, 4, &strentries[40]), false, UPB_SYNTAX_PROTO2, &reftables[14], &reftables[15]), - UPB_MSGDEF_INIT("google.protobuf.FieldOptions", 13, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[4], &arrays[42], 11, 6), UPB_STRTABLE_INIT(7, 15, UPB_CTYPE_PTR, 4, &strentries[56]), false, UPB_SYNTAX_PROTO2, &reftables[16], &reftables[17]), - UPB_MSGDEF_INIT("google.protobuf.FileDescriptorProto", 43, 6, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[53], 13, 12), UPB_STRTABLE_INIT(12, 15, UPB_CTYPE_PTR, 4, &strentries[72]), false, UPB_SYNTAX_PROTO2, &reftables[18], &reftables[19]), - UPB_MSGDEF_INIT("google.protobuf.FileDescriptorSet", 7, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[66], 2, 1), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[88]), false, UPB_SYNTAX_PROTO2, &reftables[20], &reftables[21]), - UPB_MSGDEF_INIT("google.protobuf.FileOptions", 38, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[6], &arrays[68], 42, 17), UPB_STRTABLE_INIT(18, 31, UPB_CTYPE_PTR, 5, &strentries[92]), false, UPB_SYNTAX_PROTO2, &reftables[22], &reftables[23]), - UPB_MSGDEF_INIT("google.protobuf.MessageOptions", 11, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[8], &arrays[110], 8, 4), UPB_STRTABLE_INIT(5, 7, UPB_CTYPE_PTR, 3, &strentries[124]), false, UPB_SYNTAX_PROTO2, &reftables[24], &reftables[25]), - UPB_MSGDEF_INIT("google.protobuf.MethodDescriptorProto", 16, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[118], 7, 6), UPB_STRTABLE_INIT(6, 7, UPB_CTYPE_PTR, 3, &strentries[132]), false, UPB_SYNTAX_PROTO2, &reftables[26], &reftables[27]), - UPB_MSGDEF_INIT("google.protobuf.MethodOptions", 8, 1, UPB_INTTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &intentries[10], &arrays[125], 1, 0), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[140]), false, UPB_SYNTAX_PROTO2, &reftables[28], &reftables[29]), - UPB_MSGDEF_INIT("google.protobuf.OneofDescriptorProto", 6, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[126], 2, 1), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[144]), false, UPB_SYNTAX_PROTO2, &reftables[30], &reftables[31]), - UPB_MSGDEF_INIT("google.protobuf.ServiceDescriptorProto", 12, 2, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[128], 4, 3), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[148]), false, UPB_SYNTAX_PROTO2, &reftables[32], &reftables[33]), - UPB_MSGDEF_INIT("google.protobuf.ServiceOptions", 8, 1, UPB_INTTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &intentries[14], &arrays[132], 1, 0), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[152]), false, UPB_SYNTAX_PROTO2, &reftables[34], &reftables[35]), - UPB_MSGDEF_INIT("google.protobuf.SourceCodeInfo", 7, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[133], 2, 1), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[156]), false, UPB_SYNTAX_PROTO2, &reftables[36], &reftables[37]), - UPB_MSGDEF_INIT("google.protobuf.SourceCodeInfo.Location", 20, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[135], 7, 5), UPB_STRTABLE_INIT(5, 7, UPB_CTYPE_PTR, 3, &strentries[160]), false, UPB_SYNTAX_PROTO2, &reftables[38], &reftables[39]), - UPB_MSGDEF_INIT("google.protobuf.UninterpretedOption", 19, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[142], 9, 7), UPB_STRTABLE_INIT(7, 15, UPB_CTYPE_PTR, 4, &strentries[168]), false, UPB_SYNTAX_PROTO2, &reftables[40], &reftables[41]), - UPB_MSGDEF_INIT("google.protobuf.UninterpretedOption.NamePart", 7, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[151], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[184]), false, UPB_SYNTAX_PROTO2, &reftables[42], &reftables[43]), -}; - -static const upb_fielddef fields[107] = { - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "aggregate_value", 8, &msgs[20], NULL, 16, 6, {0},&reftables[44], &reftables[45]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "allow_alias", 2, &msgs[4], NULL, 7, 1, {0},&reftables[46], &reftables[47]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "cc_enable_arenas", 31, &msgs[11], NULL, 24, 12, {0},&reftables[48], &reftables[49]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "cc_generic_services", 16, &msgs[11], NULL, 18, 6, {0},&reftables[50], &reftables[51]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "client_streaming", 5, &msgs[13], NULL, 14, 4, {0},&reftables[52], &reftables[53]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "csharp_namespace", 37, &msgs[11], NULL, 28, 14, {0},&reftables[54], &reftables[55]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "ctype", 1, &msgs[8], (const upb_def*)(&enums[2]), 7, 1, {0},&reftables[56], &reftables[57]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "default_value", 7, &msgs[7], NULL, 17, 7, {0},&reftables[58], &reftables[59]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_STRING, 0, false, false, false, false, "dependency", 3, &msgs[9], NULL, 31, 8, {0},&reftables[60], &reftables[61]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 3, &msgs[8], NULL, 9, 3, {0},&reftables[62], &reftables[63]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 33, &msgs[14], NULL, 7, 1, {0},&reftables[64], &reftables[65]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 3, &msgs[12], NULL, 9, 3, {0},&reftables[66], &reftables[67]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 23, &msgs[11], NULL, 22, 10, {0},&reftables[68], &reftables[69]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 1, &msgs[6], NULL, 7, 1, {0},&reftables[70], &reftables[71]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 3, &msgs[4], NULL, 8, 2, {0},&reftables[72], &reftables[73]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 33, &msgs[17], NULL, 7, 1, {0},&reftables[74], &reftables[75]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_DOUBLE, 0, false, false, false, false, "double_value", 6, &msgs[20], NULL, 12, 4, {0},&reftables[76], &reftables[77]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "end", 2, &msgs[2], NULL, 4, 1, {0},&reftables[78], &reftables[79]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "end", 2, &msgs[1], NULL, 4, 1, {0},&reftables[80], &reftables[81]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "enum_type", 5, &msgs[9], (const upb_def*)(&msgs[3]), 14, 1, {0},&reftables[82], &reftables[83]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "enum_type", 4, &msgs[0], (const upb_def*)(&msgs[3]), 19, 2, {0},&reftables[84], &reftables[85]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "extendee", 2, &msgs[7], NULL, 8, 2, {0},&reftables[86], &reftables[87]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "extension", 6, &msgs[0], (const upb_def*)(&msgs[7]), 25, 4, {0},&reftables[88], &reftables[89]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "extension", 7, &msgs[9], (const upb_def*)(&msgs[7]), 20, 3, {0},&reftables[90], &reftables[91]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "extension_range", 5, &msgs[0], (const upb_def*)(&msgs[1]), 22, 3, {0},&reftables[92], &reftables[93]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "field", 2, &msgs[0], (const upb_def*)(&msgs[7]), 13, 0, {0},&reftables[94], &reftables[95]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "file", 1, &msgs[10], (const upb_def*)(&msgs[9]), 6, 0, {0},&reftables[96], &reftables[97]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "go_package", 11, &msgs[11], NULL, 15, 5, {0},&reftables[98], &reftables[99]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "identifier_value", 3, &msgs[20], NULL, 7, 1, {0},&reftables[100], &reftables[101]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "input_type", 2, &msgs[13], NULL, 8, 2, {0},&reftables[102], &reftables[103]), - UPB_FIELDDEF_INIT(UPB_LABEL_REQUIRED, UPB_TYPE_BOOL, 0, false, false, false, false, "is_extension", 2, &msgs[21], NULL, 6, 1, {0},&reftables[104], &reftables[105]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "java_generate_equals_and_hash", 20, &msgs[11], NULL, 21, 9, {0},&reftables[106], &reftables[107]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "java_generic_services", 17, &msgs[11], NULL, 19, 7, {0},&reftables[108], &reftables[109]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "java_multiple_files", 10, &msgs[11], NULL, 14, 4, {0},&reftables[110], &reftables[111]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "java_outer_classname", 8, &msgs[11], NULL, 10, 2, {0},&reftables[112], &reftables[113]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "java_package", 1, &msgs[11], NULL, 7, 1, {0},&reftables[114], &reftables[115]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "java_string_check_utf8", 27, &msgs[11], NULL, 23, 11, {0},&reftables[116], &reftables[117]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "javanano_use_deprecated_package", 38, &msgs[11], NULL, 31, 15, {0},&reftables[118], &reftables[119]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "json_name", 10, &msgs[7], NULL, 21, 9, {0},&reftables[120], &reftables[121]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "jstype", 6, &msgs[8], (const upb_def*)(&enums[3]), 11, 5, {0},&reftables[122], &reftables[123]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "label", 4, &msgs[7], (const upb_def*)(&enums[0]), 12, 4, {0},&reftables[124], &reftables[125]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "lazy", 5, &msgs[8], NULL, 10, 4, {0},&reftables[126], &reftables[127]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "leading_comments", 3, &msgs[19], NULL, 9, 2, {0},&reftables[128], &reftables[129]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_STRING, 0, false, false, false, false, "leading_detached_comments", 6, &msgs[19], NULL, 17, 4, {0},&reftables[130], &reftables[131]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "location", 1, &msgs[18], (const upb_def*)(&msgs[19]), 6, 0, {0},&reftables[132], &reftables[133]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "map_entry", 7, &msgs[12], NULL, 10, 4, {0},&reftables[134], &reftables[135]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "message_set_wire_format", 1, &msgs[12], NULL, 7, 1, {0},&reftables[136], &reftables[137]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "message_type", 4, &msgs[9], (const upb_def*)(&msgs[0]), 11, 0, {0},&reftables[138], &reftables[139]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "method", 2, &msgs[16], (const upb_def*)(&msgs[13]), 7, 0, {0},&reftables[140], &reftables[141]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "name", 2, &msgs[20], (const upb_def*)(&msgs[21]), 6, 0, {0},&reftables[142], &reftables[143]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[5], NULL, 5, 1, {0},&reftables[144], &reftables[145]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[9], NULL, 23, 6, {0},&reftables[146], &reftables[147]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[3], NULL, 9, 2, {0},&reftables[148], &reftables[149]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[16], NULL, 9, 2, {0},&reftables[150], &reftables[151]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[15], NULL, 3, 0, {0},&reftables[152], &reftables[153]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[13], NULL, 5, 1, {0},&reftables[154], &reftables[155]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[7], NULL, 5, 1, {0},&reftables[156], &reftables[157]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[0], NULL, 33, 8, {0},&reftables[158], &reftables[159]), - UPB_FIELDDEF_INIT(UPB_LABEL_REQUIRED, UPB_TYPE_STRING, 0, false, false, false, false, "name_part", 1, &msgs[21], NULL, 3, 0, {0},&reftables[160], &reftables[161]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT64, UPB_INTFMT_VARIABLE, false, false, false, false, "negative_int_value", 5, &msgs[20], NULL, 11, 3, {0},&reftables[162], &reftables[163]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "nested_type", 3, &msgs[0], (const upb_def*)(&msgs[0]), 16, 1, {0},&reftables[164], &reftables[165]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "no_standard_descriptor_accessor", 2, &msgs[12], NULL, 8, 2, {0},&reftables[166], &reftables[167]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "number", 3, &msgs[7], NULL, 11, 3, {0},&reftables[168], &reftables[169]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "number", 2, &msgs[5], NULL, 8, 2, {0},&reftables[170], &reftables[171]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "objc_class_prefix", 36, &msgs[11], NULL, 25, 13, {0},&reftables[172], &reftables[173]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "oneof_decl", 8, &msgs[0], (const upb_def*)(&msgs[15]), 29, 6, {0},&reftables[174], &reftables[175]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "oneof_index", 9, &msgs[7], NULL, 20, 8, {0},&reftables[176], &reftables[177]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "optimize_for", 9, &msgs[11], (const upb_def*)(&enums[4]), 13, 3, {0},&reftables[178], &reftables[179]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 7, &msgs[0], (const upb_def*)(&msgs[12]), 26, 5, {0},&reftables[180], &reftables[181]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 8, &msgs[9], (const upb_def*)(&msgs[11]), 21, 4, {0},&reftables[182], &reftables[183]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 8, &msgs[7], (const upb_def*)(&msgs[8]), 4, 0, {0},&reftables[184], &reftables[185]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 4, &msgs[13], (const upb_def*)(&msgs[14]), 4, 0, {0},&reftables[186], &reftables[187]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 3, &msgs[16], (const upb_def*)(&msgs[17]), 8, 1, {0},&reftables[188], &reftables[189]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 3, &msgs[3], (const upb_def*)(&msgs[4]), 8, 1, {0},&reftables[190], &reftables[191]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 3, &msgs[5], (const upb_def*)(&msgs[6]), 4, 0, {0},&reftables[192], &reftables[193]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "output_type", 3, &msgs[13], NULL, 11, 3, {0},&reftables[194], &reftables[195]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "package", 2, &msgs[9], NULL, 26, 7, {0},&reftables[196], &reftables[197]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "packed", 2, &msgs[8], NULL, 8, 2, {0},&reftables[198], &reftables[199]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, true, "path", 1, &msgs[19], NULL, 5, 0, {0},&reftables[200], &reftables[201]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "php_class_prefix", 40, &msgs[11], NULL, 32, 16, {0},&reftables[202], &reftables[203]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "php_namespace", 41, &msgs[11], NULL, 35, 17, {0},&reftables[204], &reftables[205]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_UINT64, UPB_INTFMT_VARIABLE, false, false, false, false, "positive_int_value", 4, &msgs[20], NULL, 10, 2, {0},&reftables[206], &reftables[207]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "public_dependency", 10, &msgs[9], NULL, 36, 9, {0},&reftables[208], &reftables[209]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "py_generic_services", 18, &msgs[11], NULL, 20, 8, {0},&reftables[210], &reftables[211]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_STRING, 0, false, false, false, false, "reserved_name", 10, &msgs[0], NULL, 38, 9, {0},&reftables[212], &reftables[213]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "reserved_range", 9, &msgs[0], (const upb_def*)(&msgs[2]), 32, 7, {0},&reftables[214], &reftables[215]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "server_streaming", 6, &msgs[13], NULL, 15, 5, {0},&reftables[216], &reftables[217]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "service", 6, &msgs[9], (const upb_def*)(&msgs[16]), 17, 2, {0},&reftables[218], &reftables[219]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "source_code_info", 9, &msgs[9], (const upb_def*)(&msgs[18]), 22, 5, {0},&reftables[220], &reftables[221]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, true, "span", 2, &msgs[19], NULL, 8, 1, {0},&reftables[222], &reftables[223]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "start", 1, &msgs[2], NULL, 3, 0, {0},&reftables[224], &reftables[225]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "start", 1, &msgs[1], NULL, 3, 0, {0},&reftables[226], &reftables[227]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BYTES, 0, false, false, false, false, "string_value", 7, &msgs[20], NULL, 13, 5, {0},&reftables[228], &reftables[229]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "syntax", 12, &msgs[9], NULL, 40, 11, {0},&reftables[230], &reftables[231]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "trailing_comments", 4, &msgs[19], NULL, 12, 3, {0},&reftables[232], &reftables[233]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "type", 5, &msgs[7], (const upb_def*)(&enums[1]), 13, 5, {0},&reftables[234], &reftables[235]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "type_name", 6, &msgs[7], NULL, 14, 6, {0},&reftables[236], &reftables[237]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[12], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[238], &reftables[239]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[17], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[240], &reftables[241]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[11], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[242], &reftables[243]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[14], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[244], &reftables[245]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[8], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[246], &reftables[247]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[6], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[248], &reftables[249]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[4], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[250], &reftables[251]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "value", 2, &msgs[3], (const upb_def*)(&msgs[5]), 7, 0, {0},&reftables[252], &reftables[253]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "weak", 10, &msgs[8], NULL, 12, 6, {0},&reftables[254], &reftables[255]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "weak_dependency", 11, &msgs[9], NULL, 39, 10, {0},&reftables[256], &reftables[257]), -}; - -static const upb_enumdef enums[5] = { - UPB_ENUMDEF_INIT("google.protobuf.FieldDescriptorProto.Label", UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_INT32, 2, &strentries[188]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[154], 4, 3), 0, &reftables[258], &reftables[259]), - UPB_ENUMDEF_INIT("google.protobuf.FieldDescriptorProto.Type", UPB_STRTABLE_INIT(18, 31, UPB_CTYPE_INT32, 5, &strentries[192]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[158], 19, 18), 0, &reftables[260], &reftables[261]), - UPB_ENUMDEF_INIT("google.protobuf.FieldOptions.CType", UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_INT32, 2, &strentries[224]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[177], 3, 3), 0, &reftables[262], &reftables[263]), - UPB_ENUMDEF_INIT("google.protobuf.FieldOptions.JSType", UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_INT32, 2, &strentries[228]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[180], 3, 3), 0, &reftables[264], &reftables[265]), - UPB_ENUMDEF_INIT("google.protobuf.FileOptions.OptimizeMode", UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_INT32, 2, &strentries[232]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[183], 4, 3), 0, &reftables[266], &reftables[267]), -}; - -static const upb_tabent strentries[236] = { - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "extension"), UPB_TABVALUE_PTR_INIT(&fields[22]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "reserved_name"), UPB_TABVALUE_PTR_INIT(&fields[84]), NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[57]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "field"), UPB_TABVALUE_PTR_INIT(&fields[25]), &strentries[12]}, - {UPB_TABKEY_STR("\017", "\000", "\000", "\000", "extension_range"), UPB_TABVALUE_PTR_INIT(&fields[24]), &strentries[14]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "nested_type"), UPB_TABVALUE_PTR_INIT(&fields[60]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\016", "\000", "\000", "\000", "reserved_range"), UPB_TABVALUE_PTR_INIT(&fields[85]), NULL}, - {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[68]), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "oneof_decl"), UPB_TABVALUE_PTR_INIT(&fields[65]), NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "enum_type"), UPB_TABVALUE_PTR_INIT(&fields[20]), &strentries[13]}, - {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "start"), UPB_TABVALUE_PTR_INIT(&fields[91]), NULL}, - {UPB_TABKEY_STR("\003", "\000", "\000", "\000", "end"), UPB_TABVALUE_PTR_INIT(&fields[18]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "start"), UPB_TABVALUE_PTR_INIT(&fields[90]), NULL}, - {UPB_TABKEY_STR("\003", "\000", "\000", "\000", "end"), UPB_TABVALUE_PTR_INIT(&fields[17]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "value"), UPB_TABVALUE_PTR_INIT(&fields[104]), NULL}, - {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[73]), NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[52]), &strentries[26]}, - {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[103]), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[14]), NULL}, - {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "allow_alias"), UPB_TABVALUE_PTR_INIT(&fields[1]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "number"), UPB_TABVALUE_PTR_INIT(&fields[63]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[74]), NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[50]), &strentries[34]}, - {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[102]), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[13]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "oneof_index"), UPB_TABVALUE_PTR_INIT(&fields[66]), NULL}, - {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "label"), UPB_TABVALUE_PTR_INIT(&fields[40]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[56]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "number"), UPB_TABVALUE_PTR_INIT(&fields[62]), &strentries[53]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\010", "\000", "\000", "\000", "extendee"), UPB_TABVALUE_PTR_INIT(&fields[21]), NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "type_name"), UPB_TABVALUE_PTR_INIT(&fields[96]), NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "json_name"), UPB_TABVALUE_PTR_INIT(&fields[38]), NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "type"), UPB_TABVALUE_PTR_INIT(&fields[95]), &strentries[50]}, - {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "default_value"), UPB_TABVALUE_PTR_INIT(&fields[7]), NULL}, - {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[70]), NULL}, - {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[101]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "weak"), UPB_TABVALUE_PTR_INIT(&fields[105]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "packed"), UPB_TABVALUE_PTR_INIT(&fields[77]), NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "lazy"), UPB_TABVALUE_PTR_INIT(&fields[41]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "ctype"), UPB_TABVALUE_PTR_INIT(&fields[6]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "jstype"), UPB_TABVALUE_PTR_INIT(&fields[39]), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[9]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "extension"), UPB_TABVALUE_PTR_INIT(&fields[23]), NULL}, - {UPB_TABKEY_STR("\017", "\000", "\000", "\000", "weak_dependency"), UPB_TABVALUE_PTR_INIT(&fields[106]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[51]), NULL}, - {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "service"), UPB_TABVALUE_PTR_INIT(&fields[87]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "source_code_info"), UPB_TABVALUE_PTR_INIT(&fields[88]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "syntax"), UPB_TABVALUE_PTR_INIT(&fields[93]), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "dependency"), UPB_TABVALUE_PTR_INIT(&fields[8]), NULL}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "message_type"), UPB_TABVALUE_PTR_INIT(&fields[47]), NULL}, - {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "package"), UPB_TABVALUE_PTR_INIT(&fields[76]), NULL}, - {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[69]), &strentries[86]}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "enum_type"), UPB_TABVALUE_PTR_INIT(&fields[19]), NULL}, - {UPB_TABKEY_STR("\021", "\000", "\000", "\000", "public_dependency"), UPB_TABVALUE_PTR_INIT(&fields[82]), &strentries[85]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "file"), UPB_TABVALUE_PTR_INIT(&fields[26]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\023", "\000", "\000", "\000", "cc_generic_services"), UPB_TABVALUE_PTR_INIT(&fields[3]), NULL}, - {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "csharp_namespace"), UPB_TABVALUE_PTR_INIT(&fields[5]), &strentries[116]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "go_package"), UPB_TABVALUE_PTR_INIT(&fields[27]), NULL}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "java_package"), UPB_TABVALUE_PTR_INIT(&fields[35]), &strentries[120]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "java_outer_classname"), UPB_TABVALUE_PTR_INIT(&fields[34]), NULL}, - {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "php_namespace"), UPB_TABVALUE_PTR_INIT(&fields[80]), &strentries[113]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\023", "\000", "\000", "\000", "java_multiple_files"), UPB_TABVALUE_PTR_INIT(&fields[33]), &strentries[117]}, - {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[99]), NULL}, - {UPB_TABKEY_STR("\025", "\000", "\000", "\000", "java_generic_services"), UPB_TABVALUE_PTR_INIT(&fields[32]), &strentries[118]}, - {UPB_TABKEY_STR("\035", "\000", "\000", "\000", "java_generate_equals_and_hash"), UPB_TABVALUE_PTR_INIT(&fields[31]), NULL}, - {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "php_class_prefix"), UPB_TABVALUE_PTR_INIT(&fields[79]), NULL}, - {UPB_TABKEY_STR("\037", "\000", "\000", "\000", "javanano_use_deprecated_package"), UPB_TABVALUE_PTR_INIT(&fields[37]), &strentries[123]}, - {UPB_TABKEY_STR("\023", "\000", "\000", "\000", "py_generic_services"), UPB_TABVALUE_PTR_INIT(&fields[83]), NULL}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "optimize_for"), UPB_TABVALUE_PTR_INIT(&fields[67]), NULL}, - {UPB_TABKEY_STR("\026", "\000", "\000", "\000", "java_string_check_utf8"), UPB_TABVALUE_PTR_INIT(&fields[36]), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[12]), &strentries[119]}, - {UPB_TABKEY_STR("\021", "\000", "\000", "\000", "objc_class_prefix"), UPB_TABVALUE_PTR_INIT(&fields[64]), NULL}, - {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "cc_enable_arenas"), UPB_TABVALUE_PTR_INIT(&fields[2]), NULL}, - {UPB_TABKEY_STR("\027", "\000", "\000", "\000", "message_set_wire_format"), UPB_TABVALUE_PTR_INIT(&fields[46]), &strentries[128]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[97]), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[11]), NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "map_entry"), UPB_TABVALUE_PTR_INIT(&fields[45]), NULL}, - {UPB_TABKEY_STR("\037", "\000", "\000", "\000", "no_standard_descriptor_accessor"), UPB_TABVALUE_PTR_INIT(&fields[61]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "client_streaming"), UPB_TABVALUE_PTR_INIT(&fields[4]), NULL}, - {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "server_streaming"), UPB_TABVALUE_PTR_INIT(&fields[86]), NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[55]), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "input_type"), UPB_TABVALUE_PTR_INIT(&fields[29]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "output_type"), UPB_TABVALUE_PTR_INIT(&fields[75]), NULL}, - {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[71]), NULL}, - {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[100]), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[10]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[54]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[72]), &strentries[150]}, - {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "method"), UPB_TABVALUE_PTR_INIT(&fields[48]), NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[53]), &strentries[149]}, - {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[98]), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[15]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\010", "\000", "\000", "\000", "location"), UPB_TABVALUE_PTR_INIT(&fields[44]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "span"), UPB_TABVALUE_PTR_INIT(&fields[89]), &strentries[167]}, - {UPB_TABKEY_STR("\031", "\000", "\000", "\000", "leading_detached_comments"), UPB_TABVALUE_PTR_INIT(&fields[43]), &strentries[165]}, - {UPB_TABKEY_STR("\021", "\000", "\000", "\000", "trailing_comments"), UPB_TABVALUE_PTR_INIT(&fields[94]), NULL}, - {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "leading_comments"), UPB_TABVALUE_PTR_INIT(&fields[42]), &strentries[164]}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "path"), UPB_TABVALUE_PTR_INIT(&fields[78]), NULL}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "double_value"), UPB_TABVALUE_PTR_INIT(&fields[16]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[49]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\022", "\000", "\000", "\000", "negative_int_value"), UPB_TABVALUE_PTR_INIT(&fields[59]), NULL}, - {UPB_TABKEY_STR("\017", "\000", "\000", "\000", "aggregate_value"), UPB_TABVALUE_PTR_INIT(&fields[0]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\022", "\000", "\000", "\000", "positive_int_value"), UPB_TABVALUE_PTR_INIT(&fields[81]), NULL}, - {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "identifier_value"), UPB_TABVALUE_PTR_INIT(&fields[28]), NULL}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "string_value"), UPB_TABVALUE_PTR_INIT(&fields[92]), &strentries[182]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "is_extension"), UPB_TABVALUE_PTR_INIT(&fields[30]), NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "name_part"), UPB_TABVALUE_PTR_INIT(&fields[58]), NULL}, - {UPB_TABKEY_STR("\016", "\000", "\000", "\000", "LABEL_REQUIRED"), UPB_TABVALUE_INT_INIT(2), &strentries[190]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\016", "\000", "\000", "\000", "LABEL_REPEATED"), UPB_TABVALUE_INT_INIT(3), NULL}, - {UPB_TABKEY_STR("\016", "\000", "\000", "\000", "LABEL_OPTIONAL"), UPB_TABVALUE_INT_INIT(1), NULL}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "TYPE_FIXED64"), UPB_TABVALUE_INT_INIT(6), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_STRING"), UPB_TABVALUE_INT_INIT(9), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "TYPE_FLOAT"), UPB_TABVALUE_INT_INIT(2), &strentries[221]}, - {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_DOUBLE"), UPB_TABVALUE_INT_INIT(1), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "TYPE_INT32"), UPB_TABVALUE_INT_INIT(5), NULL}, - {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "TYPE_SFIXED32"), UPB_TABVALUE_INT_INIT(15), NULL}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "TYPE_FIXED32"), UPB_TABVALUE_INT_INIT(7), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "TYPE_MESSAGE"), UPB_TABVALUE_INT_INIT(11), &strentries[222]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "TYPE_INT64"), UPB_TABVALUE_INT_INIT(3), &strentries[219]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "TYPE_ENUM"), UPB_TABVALUE_INT_INIT(14), NULL}, - {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_UINT32"), UPB_TABVALUE_INT_INIT(13), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_UINT64"), UPB_TABVALUE_INT_INIT(4), &strentries[218]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "TYPE_SFIXED64"), UPB_TABVALUE_INT_INIT(16), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "TYPE_BYTES"), UPB_TABVALUE_INT_INIT(12), NULL}, - {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_SINT64"), UPB_TABVALUE_INT_INIT(18), NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "TYPE_BOOL"), UPB_TABVALUE_INT_INIT(8), NULL}, - {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "TYPE_GROUP"), UPB_TABVALUE_INT_INIT(10), NULL}, - {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_SINT32"), UPB_TABVALUE_INT_INIT(17), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "CORD"), UPB_TABVALUE_INT_INIT(1), NULL}, - {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "STRING"), UPB_TABVALUE_INT_INIT(0), &strentries[225]}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "STRING_PIECE"), UPB_TABVALUE_INT_INIT(2), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "JS_NORMAL"), UPB_TABVALUE_INT_INIT(0), NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "JS_NUMBER"), UPB_TABVALUE_INT_INIT(2), NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "JS_STRING"), UPB_TABVALUE_INT_INIT(1), NULL}, - {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "CODE_SIZE"), UPB_TABVALUE_INT_INIT(2), NULL}, - {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "SPEED"), UPB_TABVALUE_INT_INIT(1), &strentries[235]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "LITE_RUNTIME"), UPB_TABVALUE_INT_INIT(3), NULL}, -}; - -static const upb_tabent intentries[18] = { - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[103]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[102]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[101]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[99]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[97]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NUM(33), UPB_TABVALUE_PTR_INIT(&fields[10]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[100]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NUM(33), UPB_TABVALUE_PTR_INIT(&fields[15]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[98]), NULL}, -}; - -static const upb_tabval arrays[187] = { - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[57]), - UPB_TABVALUE_PTR_INIT(&fields[25]), - UPB_TABVALUE_PTR_INIT(&fields[60]), - UPB_TABVALUE_PTR_INIT(&fields[20]), - UPB_TABVALUE_PTR_INIT(&fields[24]), - UPB_TABVALUE_PTR_INIT(&fields[22]), - UPB_TABVALUE_PTR_INIT(&fields[68]), - UPB_TABVALUE_PTR_INIT(&fields[65]), - UPB_TABVALUE_PTR_INIT(&fields[85]), - UPB_TABVALUE_PTR_INIT(&fields[84]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[91]), - UPB_TABVALUE_PTR_INIT(&fields[18]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[90]), - UPB_TABVALUE_PTR_INIT(&fields[17]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[52]), - UPB_TABVALUE_PTR_INIT(&fields[104]), - UPB_TABVALUE_PTR_INIT(&fields[73]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[1]), - UPB_TABVALUE_PTR_INIT(&fields[14]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[50]), - UPB_TABVALUE_PTR_INIT(&fields[63]), - UPB_TABVALUE_PTR_INIT(&fields[74]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[13]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[56]), - UPB_TABVALUE_PTR_INIT(&fields[21]), - UPB_TABVALUE_PTR_INIT(&fields[62]), - UPB_TABVALUE_PTR_INIT(&fields[40]), - UPB_TABVALUE_PTR_INIT(&fields[95]), - UPB_TABVALUE_PTR_INIT(&fields[96]), - UPB_TABVALUE_PTR_INIT(&fields[7]), - UPB_TABVALUE_PTR_INIT(&fields[70]), - UPB_TABVALUE_PTR_INIT(&fields[66]), - UPB_TABVALUE_PTR_INIT(&fields[38]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[6]), - UPB_TABVALUE_PTR_INIT(&fields[77]), - UPB_TABVALUE_PTR_INIT(&fields[9]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[41]), - UPB_TABVALUE_PTR_INIT(&fields[39]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[105]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[51]), - UPB_TABVALUE_PTR_INIT(&fields[76]), - UPB_TABVALUE_PTR_INIT(&fields[8]), - UPB_TABVALUE_PTR_INIT(&fields[47]), - UPB_TABVALUE_PTR_INIT(&fields[19]), - UPB_TABVALUE_PTR_INIT(&fields[87]), - UPB_TABVALUE_PTR_INIT(&fields[23]), - UPB_TABVALUE_PTR_INIT(&fields[69]), - UPB_TABVALUE_PTR_INIT(&fields[88]), - UPB_TABVALUE_PTR_INIT(&fields[82]), - UPB_TABVALUE_PTR_INIT(&fields[106]), - UPB_TABVALUE_PTR_INIT(&fields[93]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[26]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[35]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[34]), - UPB_TABVALUE_PTR_INIT(&fields[67]), - UPB_TABVALUE_PTR_INIT(&fields[33]), - UPB_TABVALUE_PTR_INIT(&fields[27]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[3]), - UPB_TABVALUE_PTR_INIT(&fields[32]), - UPB_TABVALUE_PTR_INIT(&fields[83]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[31]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[12]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[36]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[2]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[64]), - UPB_TABVALUE_PTR_INIT(&fields[5]), - UPB_TABVALUE_PTR_INIT(&fields[37]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[79]), - UPB_TABVALUE_PTR_INIT(&fields[80]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[46]), - UPB_TABVALUE_PTR_INIT(&fields[61]), - UPB_TABVALUE_PTR_INIT(&fields[11]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[45]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[55]), - UPB_TABVALUE_PTR_INIT(&fields[29]), - UPB_TABVALUE_PTR_INIT(&fields[75]), - UPB_TABVALUE_PTR_INIT(&fields[71]), - UPB_TABVALUE_PTR_INIT(&fields[4]), - UPB_TABVALUE_PTR_INIT(&fields[86]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[54]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[53]), - UPB_TABVALUE_PTR_INIT(&fields[48]), - UPB_TABVALUE_PTR_INIT(&fields[72]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[44]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[78]), - UPB_TABVALUE_PTR_INIT(&fields[89]), - UPB_TABVALUE_PTR_INIT(&fields[42]), - UPB_TABVALUE_PTR_INIT(&fields[94]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[43]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[49]), - UPB_TABVALUE_PTR_INIT(&fields[28]), - UPB_TABVALUE_PTR_INIT(&fields[81]), - UPB_TABVALUE_PTR_INIT(&fields[59]), - UPB_TABVALUE_PTR_INIT(&fields[16]), - UPB_TABVALUE_PTR_INIT(&fields[92]), - UPB_TABVALUE_PTR_INIT(&fields[0]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[58]), - UPB_TABVALUE_PTR_INIT(&fields[30]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT("LABEL_OPTIONAL"), - UPB_TABVALUE_PTR_INIT("LABEL_REQUIRED"), - UPB_TABVALUE_PTR_INIT("LABEL_REPEATED"), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT("TYPE_DOUBLE"), - UPB_TABVALUE_PTR_INIT("TYPE_FLOAT"), - UPB_TABVALUE_PTR_INIT("TYPE_INT64"), - UPB_TABVALUE_PTR_INIT("TYPE_UINT64"), - UPB_TABVALUE_PTR_INIT("TYPE_INT32"), - UPB_TABVALUE_PTR_INIT("TYPE_FIXED64"), - UPB_TABVALUE_PTR_INIT("TYPE_FIXED32"), - UPB_TABVALUE_PTR_INIT("TYPE_BOOL"), - UPB_TABVALUE_PTR_INIT("TYPE_STRING"), - UPB_TABVALUE_PTR_INIT("TYPE_GROUP"), - UPB_TABVALUE_PTR_INIT("TYPE_MESSAGE"), - UPB_TABVALUE_PTR_INIT("TYPE_BYTES"), - UPB_TABVALUE_PTR_INIT("TYPE_UINT32"), - UPB_TABVALUE_PTR_INIT("TYPE_ENUM"), - UPB_TABVALUE_PTR_INIT("TYPE_SFIXED32"), - UPB_TABVALUE_PTR_INIT("TYPE_SFIXED64"), - UPB_TABVALUE_PTR_INIT("TYPE_SINT32"), - UPB_TABVALUE_PTR_INIT("TYPE_SINT64"), - UPB_TABVALUE_PTR_INIT("STRING"), - UPB_TABVALUE_PTR_INIT("CORD"), - UPB_TABVALUE_PTR_INIT("STRING_PIECE"), - UPB_TABVALUE_PTR_INIT("JS_NORMAL"), - UPB_TABVALUE_PTR_INIT("JS_STRING"), - UPB_TABVALUE_PTR_INIT("JS_NUMBER"), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT("SPEED"), - UPB_TABVALUE_PTR_INIT("CODE_SIZE"), - UPB_TABVALUE_PTR_INIT("LITE_RUNTIME"), -}; - -#ifdef UPB_DEBUG_REFS -static upb_inttable reftables[268] = { - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), -}; -#endif - -static const upb_msgdef *refm(const upb_msgdef *m, const void *owner) { - upb_msgdef_ref(m, owner); - return m; -} - -static const upb_enumdef *refe(const upb_enumdef *e, const void *owner) { - upb_enumdef_ref(e, owner); - return e; -} - -/* Public API. */ -const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_get(const void *owner) { return refm(&msgs[0], owner); } -const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_get(const void *owner) { return refm(&msgs[1], owner); } -const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ReservedRange_get(const void *owner) { return refm(&msgs[2], owner); } -const upb_msgdef *upbdefs_google_protobuf_EnumDescriptorProto_get(const void *owner) { return refm(&msgs[3], owner); } -const upb_msgdef *upbdefs_google_protobuf_EnumOptions_get(const void *owner) { return refm(&msgs[4], owner); } -const upb_msgdef *upbdefs_google_protobuf_EnumValueDescriptorProto_get(const void *owner) { return refm(&msgs[5], owner); } -const upb_msgdef *upbdefs_google_protobuf_EnumValueOptions_get(const void *owner) { return refm(&msgs[6], owner); } -const upb_msgdef *upbdefs_google_protobuf_FieldDescriptorProto_get(const void *owner) { return refm(&msgs[7], owner); } -const upb_msgdef *upbdefs_google_protobuf_FieldOptions_get(const void *owner) { return refm(&msgs[8], owner); } -const upb_msgdef *upbdefs_google_protobuf_FileDescriptorProto_get(const void *owner) { return refm(&msgs[9], owner); } -const upb_msgdef *upbdefs_google_protobuf_FileDescriptorSet_get(const void *owner) { return refm(&msgs[10], owner); } -const upb_msgdef *upbdefs_google_protobuf_FileOptions_get(const void *owner) { return refm(&msgs[11], owner); } -const upb_msgdef *upbdefs_google_protobuf_MessageOptions_get(const void *owner) { return refm(&msgs[12], owner); } -const upb_msgdef *upbdefs_google_protobuf_MethodDescriptorProto_get(const void *owner) { return refm(&msgs[13], owner); } -const upb_msgdef *upbdefs_google_protobuf_MethodOptions_get(const void *owner) { return refm(&msgs[14], owner); } -const upb_msgdef *upbdefs_google_protobuf_OneofDescriptorProto_get(const void *owner) { return refm(&msgs[15], owner); } -const upb_msgdef *upbdefs_google_protobuf_ServiceDescriptorProto_get(const void *owner) { return refm(&msgs[16], owner); } -const upb_msgdef *upbdefs_google_protobuf_ServiceOptions_get(const void *owner) { return refm(&msgs[17], owner); } -const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo_get(const void *owner) { return refm(&msgs[18], owner); } -const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo_Location_get(const void *owner) { return refm(&msgs[19], owner); } -const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption_get(const void *owner) { return refm(&msgs[20], owner); } -const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption_NamePart_get(const void *owner) { return refm(&msgs[21], owner); } - -const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Label_get(const void *owner) { return refe(&enums[0], owner); } -const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Type_get(const void *owner) { return refe(&enums[1], owner); } -const upb_enumdef *upbdefs_google_protobuf_FieldOptions_CType_get(const void *owner) { return refe(&enums[2], owner); } -const upb_enumdef *upbdefs_google_protobuf_FieldOptions_JSType_get(const void *owner) { return refe(&enums[3], owner); } -const upb_enumdef *upbdefs_google_protobuf_FileOptions_OptimizeMode_get(const void *owner) { return refe(&enums[4], owner); } diff --git a/upb/descriptor/descriptor.upbdefs.h b/upb/descriptor/descriptor.upbdefs.h deleted file mode 100644 index dd118a0..0000000 --- a/upb/descriptor/descriptor.upbdefs.h +++ /dev/null @@ -1,608 +0,0 @@ -/* This file contains accessors for a set of compiled-in defs. - * Note that unlike Google's protobuf, it does *not* define - * generated classes or any other kind of data structure for - * actually storing protobufs. It only contains *defs* which - * let you reflect over a protobuf *schema*. - */ -/* This file was generated by upbc (the upb compiler) from the input - * file: - * - * upb/descriptor/descriptor.proto - * - * Do not edit -- your changes will be discarded when the file is - * regenerated. */ - -#ifndef UPB_DESCRIPTOR_DESCRIPTOR_PROTO_UPB_H_ -#define UPB_DESCRIPTOR_DESCRIPTOR_PROTO_UPB_H_ - -#include "upb/def.h" - -UPB_BEGIN_EXTERN_C - -/* MessageDefs: call these functions to get a ref to a msgdef. */ -const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ReservedRange_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_EnumDescriptorProto_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_EnumOptions_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_EnumValueDescriptorProto_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_EnumValueOptions_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_FieldDescriptorProto_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_FieldOptions_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_FileDescriptorProto_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_FileDescriptorSet_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_FileOptions_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_MessageOptions_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_MethodDescriptorProto_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_MethodOptions_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_OneofDescriptorProto_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_ServiceDescriptorProto_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_ServiceOptions_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo_Location_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption_get(const void *owner); -const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption_NamePart_get(const void *owner); - -/* EnumDefs: call these functions to get a ref to an enumdef. */ -const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Label_get(const void *owner); -const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Type_get(const void *owner); -const upb_enumdef *upbdefs_google_protobuf_FieldOptions_CType_get(const void *owner); -const upb_enumdef *upbdefs_google_protobuf_FieldOptions_JSType_get(const void *owner); -const upb_enumdef *upbdefs_google_protobuf_FileOptions_OptimizeMode_get(const void *owner); - -/* Functions to test whether this message is of a certain type. */ -UPB_INLINE bool upbdefs_google_protobuf_DescriptorProto_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.DescriptorProto") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_DescriptorProto_ExtensionRange_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.DescriptorProto.ExtensionRange") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_DescriptorProto_ReservedRange_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.DescriptorProto.ReservedRange") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_EnumDescriptorProto_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.EnumDescriptorProto") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_EnumOptions_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.EnumOptions") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_EnumValueDescriptorProto_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.EnumValueDescriptorProto") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_EnumValueOptions_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.EnumValueOptions") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_FieldDescriptorProto_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.FieldDescriptorProto") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_FieldOptions_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.FieldOptions") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_FileDescriptorProto_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.FileDescriptorProto") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_FileDescriptorSet_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.FileDescriptorSet") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_FileOptions_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.FileOptions") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_MessageOptions_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.MessageOptions") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_MethodDescriptorProto_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.MethodDescriptorProto") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_MethodOptions_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.MethodOptions") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_OneofDescriptorProto_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.OneofDescriptorProto") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_ServiceDescriptorProto_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.ServiceDescriptorProto") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_ServiceOptions_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.ServiceOptions") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_SourceCodeInfo_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.SourceCodeInfo") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_SourceCodeInfo_Location_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.SourceCodeInfo.Location") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_UninterpretedOption_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.UninterpretedOption") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_UninterpretedOption_NamePart_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "google.protobuf.UninterpretedOption.NamePart") == 0; -} - -/* Functions to test whether this enum is of a certain type. */ -UPB_INLINE bool upbdefs_google_protobuf_FieldDescriptorProto_Label_is(const upb_enumdef *e) { - return strcmp(upb_enumdef_fullname(e), "google.protobuf.FieldDescriptorProto.Label") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_FieldDescriptorProto_Type_is(const upb_enumdef *e) { - return strcmp(upb_enumdef_fullname(e), "google.protobuf.FieldDescriptorProto.Type") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_FieldOptions_CType_is(const upb_enumdef *e) { - return strcmp(upb_enumdef_fullname(e), "google.protobuf.FieldOptions.CType") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_FieldOptions_JSType_is(const upb_enumdef *e) { - return strcmp(upb_enumdef_fullname(e), "google.protobuf.FieldOptions.JSType") == 0; -} -UPB_INLINE bool upbdefs_google_protobuf_FileOptions_OptimizeMode_is(const upb_enumdef *e) { - return strcmp(upb_enumdef_fullname(e), "google.protobuf.FileOptions.OptimizeMode") == 0; -} - - -/* Functions to get a fielddef from a msgdef reference. */ -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_f_end(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ExtensionRange_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_f_start(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ExtensionRange_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ReservedRange_f_end(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ReservedRange_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ReservedRange_f_start(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ReservedRange_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_enum_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 4); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_extension(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 6); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_extension_range(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 5); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_field(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_nested_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_oneof_decl(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 8); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 7); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_reserved_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 10); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_reserved_range(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 9); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_f_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_f_allow_alias(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumOptions_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumOptions_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumOptions_is(m)); return upb_msgdef_itof(m, 999); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_f_number(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumValueOptions_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumValueOptions_is(m)); return upb_msgdef_itof(m, 999); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_default_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 7); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_extendee(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_json_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 10); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_label(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 4); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_number(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_oneof_index(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 9); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 8); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 5); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_type_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 6); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_ctype(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_jstype(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 6); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_lazy(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 5); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_packed(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 999); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_weak(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 10); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_dependency(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_enum_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 5); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_extension(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 7); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_message_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 4); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 8); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_package(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_public_dependency(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 10); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_service(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 6); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_source_code_info(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 9); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_syntax(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 12); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_weak_dependency(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 11); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorSet_f_file(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorSet_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_cc_enable_arenas(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 31); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_cc_generic_services(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 16); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_csharp_namespace(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 37); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 23); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_go_package(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 11); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_generate_equals_and_hash(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 20); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_generic_services(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 17); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_multiple_files(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 10); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_outer_classname(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 8); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_package(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_string_check_utf8(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 27); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_javanano_use_deprecated_package(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 38); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_objc_class_prefix(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 36); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_optimize_for(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 9); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_php_class_prefix(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 40); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_php_namespace(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 41); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_py_generic_services(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 18); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 999); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_map_entry(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 7); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_message_set_wire_format(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_no_standard_descriptor_accessor(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 999); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_client_streaming(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 5); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_input_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 4); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_output_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_server_streaming(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 6); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodOptions_is(m)); return upb_msgdef_itof(m, 33); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodOptions_is(m)); return upb_msgdef_itof(m, 999); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_OneofDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_OneofDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_f_method(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_ServiceDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_ServiceDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_ServiceDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_ServiceOptions_is(m)); return upb_msgdef_itof(m, 33); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_ServiceOptions_is(m)); return upb_msgdef_itof(m, 999); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_leading_comments(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_leading_detached_comments(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 6); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_path(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_span(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_trailing_comments(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 4); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_f_location(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_NamePart_f_is_extension(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_NamePart_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_NamePart_f_name_part(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_NamePart_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_aggregate_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 8); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_double_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 6); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_identifier_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_negative_int_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 5); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_positive_int_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 4); } -UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_string_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 7); } - -UPB_END_EXTERN_C - -#ifdef __cplusplus - -namespace upbdefs { -namespace google { -namespace protobuf { - -class DescriptorProto : public ::upb::reffed_ptr { - public: - DescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); - } - - static DescriptorProto get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_DescriptorProto_get(&m); - return DescriptorProto(m, &m); - } - - class ExtensionRange : public ::upb::reffed_ptr { - public: - ExtensionRange(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ExtensionRange_is(m)); - } - - static ExtensionRange get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_DescriptorProto_ExtensionRange_get(&m); - return ExtensionRange(m, &m); - } - }; - - class ReservedRange : public ::upb::reffed_ptr { - public: - ReservedRange(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ReservedRange_is(m)); - } - - static ReservedRange get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_DescriptorProto_ReservedRange_get(&m); - return ReservedRange(m, &m); - } - }; -}; - -class EnumDescriptorProto : public ::upb::reffed_ptr { - public: - EnumDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_EnumDescriptorProto_is(m)); - } - - static EnumDescriptorProto get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_EnumDescriptorProto_get(&m); - return EnumDescriptorProto(m, &m); - } -}; - -class EnumOptions : public ::upb::reffed_ptr { - public: - EnumOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_EnumOptions_is(m)); - } - - static EnumOptions get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_EnumOptions_get(&m); - return EnumOptions(m, &m); - } -}; - -class EnumValueDescriptorProto : public ::upb::reffed_ptr { - public: - EnumValueDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)); - } - - static EnumValueDescriptorProto get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_EnumValueDescriptorProto_get(&m); - return EnumValueDescriptorProto(m, &m); - } -}; - -class EnumValueOptions : public ::upb::reffed_ptr { - public: - EnumValueOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_EnumValueOptions_is(m)); - } - - static EnumValueOptions get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_EnumValueOptions_get(&m); - return EnumValueOptions(m, &m); - } -}; - -class FieldDescriptorProto : public ::upb::reffed_ptr { - public: - FieldDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); - } - - static FieldDescriptorProto get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_FieldDescriptorProto_get(&m); - return FieldDescriptorProto(m, &m); - } - - class Label : public ::upb::reffed_ptr { - public: - Label(const ::upb::EnumDef* e, const void *ref_donor = NULL) - : reffed_ptr(e, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_Label_is(e)); - } - static Label get() { - const ::upb::EnumDef* e = upbdefs_google_protobuf_FieldDescriptorProto_Label_get(&e); - return Label(e, &e); - } - }; - - class Type : public ::upb::reffed_ptr { - public: - Type(const ::upb::EnumDef* e, const void *ref_donor = NULL) - : reffed_ptr(e, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_Type_is(e)); - } - static Type get() { - const ::upb::EnumDef* e = upbdefs_google_protobuf_FieldDescriptorProto_Type_get(&e); - return Type(e, &e); - } - }; -}; - -class FieldOptions : public ::upb::reffed_ptr { - public: - FieldOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); - } - - static FieldOptions get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_FieldOptions_get(&m); - return FieldOptions(m, &m); - } - - class CType : public ::upb::reffed_ptr { - public: - CType(const ::upb::EnumDef* e, const void *ref_donor = NULL) - : reffed_ptr(e, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_CType_is(e)); - } - static CType get() { - const ::upb::EnumDef* e = upbdefs_google_protobuf_FieldOptions_CType_get(&e); - return CType(e, &e); - } - }; - - class JSType : public ::upb::reffed_ptr { - public: - JSType(const ::upb::EnumDef* e, const void *ref_donor = NULL) - : reffed_ptr(e, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_JSType_is(e)); - } - static JSType get() { - const ::upb::EnumDef* e = upbdefs_google_protobuf_FieldOptions_JSType_get(&e); - return JSType(e, &e); - } - }; -}; - -class FileDescriptorProto : public ::upb::reffed_ptr { - public: - FileDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); - } - - static FileDescriptorProto get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_FileDescriptorProto_get(&m); - return FileDescriptorProto(m, &m); - } -}; - -class FileDescriptorSet : public ::upb::reffed_ptr { - public: - FileDescriptorSet(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorSet_is(m)); - } - - static FileDescriptorSet get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_FileDescriptorSet_get(&m); - return FileDescriptorSet(m, &m); - } -}; - -class FileOptions : public ::upb::reffed_ptr { - public: - FileOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); - } - - static FileOptions get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_FileOptions_get(&m); - return FileOptions(m, &m); - } - - class OptimizeMode : public ::upb::reffed_ptr { - public: - OptimizeMode(const ::upb::EnumDef* e, const void *ref_donor = NULL) - : reffed_ptr(e, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_FileOptions_OptimizeMode_is(e)); - } - static OptimizeMode get() { - const ::upb::EnumDef* e = upbdefs_google_protobuf_FileOptions_OptimizeMode_get(&e); - return OptimizeMode(e, &e); - } - }; -}; - -class MessageOptions : public ::upb::reffed_ptr { - public: - MessageOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m)); - } - - static MessageOptions get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_MessageOptions_get(&m); - return MessageOptions(m, &m); - } -}; - -class MethodDescriptorProto : public ::upb::reffed_ptr { - public: - MethodDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); - } - - static MethodDescriptorProto get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_MethodDescriptorProto_get(&m); - return MethodDescriptorProto(m, &m); - } -}; - -class MethodOptions : public ::upb::reffed_ptr { - public: - MethodOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_MethodOptions_is(m)); - } - - static MethodOptions get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_MethodOptions_get(&m); - return MethodOptions(m, &m); - } -}; - -class OneofDescriptorProto : public ::upb::reffed_ptr { - public: - OneofDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_OneofDescriptorProto_is(m)); - } - - static OneofDescriptorProto get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_OneofDescriptorProto_get(&m); - return OneofDescriptorProto(m, &m); - } -}; - -class ServiceDescriptorProto : public ::upb::reffed_ptr { - public: - ServiceDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_ServiceDescriptorProto_is(m)); - } - - static ServiceDescriptorProto get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_ServiceDescriptorProto_get(&m); - return ServiceDescriptorProto(m, &m); - } -}; - -class ServiceOptions : public ::upb::reffed_ptr { - public: - ServiceOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_ServiceOptions_is(m)); - } - - static ServiceOptions get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_ServiceOptions_get(&m); - return ServiceOptions(m, &m); - } -}; - -class SourceCodeInfo : public ::upb::reffed_ptr { - public: - SourceCodeInfo(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_is(m)); - } - - static SourceCodeInfo get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_SourceCodeInfo_get(&m); - return SourceCodeInfo(m, &m); - } - - class Location : public ::upb::reffed_ptr { - public: - Location(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); - } - - static Location get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_SourceCodeInfo_Location_get(&m); - return Location(m, &m); - } - }; -}; - -class UninterpretedOption : public ::upb::reffed_ptr { - public: - UninterpretedOption(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); - } - - static UninterpretedOption get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_UninterpretedOption_get(&m); - return UninterpretedOption(m, &m); - } - - class NamePart : public ::upb::reffed_ptr { - public: - NamePart(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_NamePart_is(m)); - } - - static NamePart get() { - const ::upb::MessageDef* m = upbdefs_google_protobuf_UninterpretedOption_NamePart_get(&m); - return NamePart(m, &m); - } - }; -}; - -} /* namespace protobuf */ -} /* namespace google */ -} /* namespace upbdefs */ - -#endif /* __cplusplus */ - -#endif /* UPB_DESCRIPTOR_DESCRIPTOR_PROTO_UPB_H_ */ diff --git a/upb/descriptor/reader.c b/upb/descriptor/reader.c deleted file mode 100644 index da89622..0000000 --- a/upb/descriptor/reader.c +++ /dev/null @@ -1,900 +0,0 @@ -/* -** XXX: The routines in this file that consume a string do not currently -** support having the string span buffers. In the future, as upb_sink and -** its buffering/sharing functionality evolve there should be an easy and -** idiomatic way of correctly handling this case. For now, we accept this -** limitation since we currently only parse descriptors from single strings. -*/ - -#include "upb/descriptor/reader.h" - -#include -#include -#include -#include "upb/def.h" -#include "upb/sink.h" -#include "upb/descriptor/descriptor.upbdefs.h" - -/* Compares a NULL-terminated string with a non-NULL-terminated string. */ -static bool upb_streq(const char *str, const char *buf, size_t n) { - return strlen(str) == n && memcmp(str, buf, n) == 0; -} - -/* We keep a stack of all the messages scopes we are currently in, as well as - * the top-level file scope. This is necessary to correctly qualify the - * definitions that are contained inside. "name" tracks the name of the - * message or package (a bare name -- not qualified by any enclosing scopes). */ -typedef struct { - char *name; - /* Index of the first def that is under this scope. For msgdefs, the - * msgdef itself is at start-1. */ - int start; - uint32_t oneof_start; - uint32_t oneof_index; -} upb_descreader_frame; - -/* The maximum number of nested declarations that are allowed, ie. - * message Foo { - * message Bar { - * message Baz { - * } - * } - * } - * - * This is a resource limit that affects how big our runtime stack can grow. - * TODO: make this a runtime-settable property of the Reader instance. */ -#define UPB_MAX_MESSAGE_NESTING 64 - -struct upb_descreader { - upb_sink sink; - upb_inttable files; - upb_strtable files_by_name; - upb_filedef *file; /* The last file in files. */ - upb_descreader_frame stack[UPB_MAX_MESSAGE_NESTING]; - int stack_len; - upb_inttable oneofs; - - uint32_t number; - char *name; - bool saw_number; - bool saw_name; - - char *default_string; - - upb_fielddef *f; -}; - -static char *upb_gstrndup(const char *buf, size_t n) { - char *ret = upb_gmalloc(n + 1); - if (!ret) return NULL; - memcpy(ret, buf, n); - ret[n] = '\0'; - return ret; -} - -/* Returns a newly allocated string that joins input strings together, for - * example: - * join("Foo.Bar", "Baz") -> "Foo.Bar.Baz" - * join("", "Baz") -> "Baz" - * Caller owns a ref on the returned string. */ -static char *upb_join(const char *base, const char *name) { - if (!base || strlen(base) == 0) { - return upb_gstrdup(name); - } else { - char *ret = upb_gmalloc(strlen(base) + strlen(name) + 2); - if (!ret) { - return NULL; - } - ret[0] = '\0'; - strcat(ret, base); - strcat(ret, "."); - strcat(ret, name); - return ret; - } -} - -/* Qualify the defname for all defs starting with offset "start" with "str". */ -static bool upb_descreader_qualify(upb_filedef *f, char *str, int32_t start) { - size_t i; - for (i = start; i < upb_filedef_defcount(f); i++) { - upb_def *def = upb_filedef_mutabledef(f, i); - char *name = upb_join(str, upb_def_fullname(def)); - if (!name) { - /* Need better logic here; at this point we've qualified some names but - * not others. */ - return false; - } - upb_def_setfullname(def, name, NULL); - upb_gfree(name); - } - return true; -} - - -/* upb_descreader ************************************************************/ - -static upb_msgdef *upb_descreader_top(upb_descreader *r) { - int index; - UPB_ASSERT(r->stack_len > 1); - index = r->stack[r->stack_len-1].start - 1; - UPB_ASSERT(index >= 0); - return upb_downcast_msgdef_mutable(upb_filedef_mutabledef(r->file, index)); -} - -static upb_def *upb_descreader_last(upb_descreader *r) { - return upb_filedef_mutabledef(r->file, upb_filedef_defcount(r->file) - 1); -} - -/* Start/end handlers for FileDescriptorProto and DescriptorProto (the two - * entities that have names and can contain sub-definitions. */ -void upb_descreader_startcontainer(upb_descreader *r) { - upb_descreader_frame *f = &r->stack[r->stack_len++]; - f->start = upb_filedef_defcount(r->file); - f->oneof_start = upb_inttable_count(&r->oneofs); - f->oneof_index = 0; - f->name = NULL; -} - -bool upb_descreader_endcontainer(upb_descreader *r) { - upb_descreader_frame *f = &r->stack[r->stack_len - 1]; - - while (upb_inttable_count(&r->oneofs) > f->oneof_start) { - upb_oneofdef *o = upb_value_getptr(upb_inttable_pop(&r->oneofs)); - bool ok = upb_msgdef_addoneof(upb_descreader_top(r), o, &r->oneofs, NULL); - UPB_ASSERT(ok); - } - - if (!upb_descreader_qualify(r->file, f->name, f->start)) { - return false; - } - upb_gfree(f->name); - f->name = NULL; - - r->stack_len--; - return true; -} - -void upb_descreader_setscopename(upb_descreader *r, char *str) { - upb_descreader_frame *f = &r->stack[r->stack_len-1]; - upb_gfree(f->name); - f->name = str; -} - -static upb_oneofdef *upb_descreader_getoneof(upb_descreader *r, - uint32_t index) { - bool found; - upb_value val; - upb_descreader_frame *f = &r->stack[r->stack_len-1]; - - /* DescriptorProto messages can be nested, so we will see the nested messages - * between when we see the FieldDescriptorProto and the OneofDescriptorProto. - * We need to preserve the oneofs in between these two things. */ - index += f->oneof_start; - - while (upb_inttable_count(&r->oneofs) <= index) { - upb_inttable_push(&r->oneofs, upb_value_ptr(upb_oneofdef_new(&r->oneofs))); - } - - found = upb_inttable_lookup(&r->oneofs, index, &val); - UPB_ASSERT(found); - return upb_value_getptr(val); -} - -/** Handlers for google.protobuf.FileDescriptorSet. ***************************/ - -static void *fileset_startfile(void *closure, const void *hd) { - upb_descreader *r = closure; - UPB_UNUSED(hd); - r->file = upb_filedef_new(&r->files); - upb_inttable_push(&r->files, upb_value_ptr(r->file)); - return r; -} - -/** Handlers for google.protobuf.FileDescriptorProto. *************************/ - -static bool file_start(void *closure, const void *hd) { - upb_descreader *r = closure; - UPB_UNUSED(hd); - upb_descreader_startcontainer(r); - return true; -} - -static bool file_end(void *closure, const void *hd, upb_status *status) { - upb_descreader *r = closure; - UPB_UNUSED(hd); - UPB_UNUSED(status); - return upb_descreader_endcontainer(r); -} - -static size_t file_onname(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - char *name; - bool ok; - UPB_UNUSED(hd); - UPB_UNUSED(handle); - - name = upb_gstrndup(buf, n); - upb_strtable_insert(&r->files_by_name, name, upb_value_ptr(r->file)); - /* XXX: see comment at the top of the file. */ - ok = upb_filedef_setname(r->file, name, NULL); - upb_gfree(name); - UPB_ASSERT(ok); - return n; -} - -static size_t file_onpackage(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - char *package; - bool ok; - UPB_UNUSED(hd); - UPB_UNUSED(handle); - - package = upb_gstrndup(buf, n); - /* XXX: see comment at the top of the file. */ - upb_descreader_setscopename(r, package); - ok = upb_filedef_setpackage(r->file, package, NULL); - UPB_ASSERT(ok); - return n; -} - -static void *file_startphpnamespace(void *closure, const void *hd, - size_t size_hint) { - upb_descreader *r = closure; - bool ok; - UPB_UNUSED(hd); - UPB_UNUSED(size_hint); - - ok = upb_filedef_setphpnamespace(r->file, "", NULL); - UPB_ASSERT(ok); - return closure; -} - -static size_t file_onphpnamespace(void *closure, const void *hd, - const char *buf, size_t n, - const upb_bufhandle *handle) { - upb_descreader *r = closure; - char *php_namespace; - bool ok; - UPB_UNUSED(hd); - UPB_UNUSED(handle); - - php_namespace = upb_gstrndup(buf, n); - ok = upb_filedef_setphpnamespace(r->file, php_namespace, NULL); - upb_gfree(php_namespace); - UPB_ASSERT(ok); - return n; -} - -static size_t file_onphpprefix(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - char *prefix; - bool ok; - UPB_UNUSED(hd); - UPB_UNUSED(handle); - - prefix = upb_gstrndup(buf, n); - ok = upb_filedef_setphpprefix(r->file, prefix, NULL); - upb_gfree(prefix); - UPB_ASSERT(ok); - return n; -} - -static size_t file_onsyntax(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - bool ok; - UPB_UNUSED(hd); - UPB_UNUSED(handle); - /* XXX: see comment at the top of the file. */ - if (upb_streq("proto2", buf, n)) { - ok = upb_filedef_setsyntax(r->file, UPB_SYNTAX_PROTO2, NULL); - } else if (upb_streq("proto3", buf, n)) { - ok = upb_filedef_setsyntax(r->file, UPB_SYNTAX_PROTO3, NULL); - } else { - ok = false; - } - - UPB_ASSERT(ok); - return n; -} - -static void *file_startmsg(void *closure, const void *hd) { - upb_descreader *r = closure; - upb_msgdef *m = upb_msgdef_new(&m); - bool ok = upb_filedef_addmsg(r->file, m, &m, NULL); - UPB_UNUSED(hd); - UPB_ASSERT(ok); - return r; -} - -static void *file_startenum(void *closure, const void *hd) { - upb_descreader *r = closure; - upb_enumdef *e = upb_enumdef_new(&e); - bool ok = upb_filedef_addenum(r->file, e, &e, NULL); - UPB_UNUSED(hd); - UPB_ASSERT(ok); - return r; -} - -static void *file_startext(void *closure, const void *hd) { - upb_descreader *r = closure; - bool ok; - r->f = upb_fielddef_new(r); - ok = upb_filedef_addext(r->file, r->f, r, NULL); - UPB_UNUSED(hd); - UPB_ASSERT(ok); - return r; -} - -static size_t file_ondep(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - upb_value val; - if (upb_strtable_lookup2(&r->files_by_name, buf, n, &val)) { - upb_filedef_adddep(r->file, upb_value_getptr(val)); - } - UPB_UNUSED(hd); - UPB_UNUSED(handle); - return n; -} - -/** Handlers for google.protobuf.EnumValueDescriptorProto. *********************/ - -static bool enumval_startmsg(void *closure, const void *hd) { - upb_descreader *r = closure; - UPB_UNUSED(hd); - r->saw_number = false; - r->saw_name = false; - return true; -} - -static size_t enumval_onname(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - UPB_UNUSED(hd); - UPB_UNUSED(handle); - /* XXX: see comment at the top of the file. */ - upb_gfree(r->name); - r->name = upb_gstrndup(buf, n); - r->saw_name = true; - return n; -} - -static bool enumval_onnumber(void *closure, const void *hd, int32_t val) { - upb_descreader *r = closure; - UPB_UNUSED(hd); - r->number = val; - r->saw_number = true; - return true; -} - -static bool enumval_endmsg(void *closure, const void *hd, upb_status *status) { - upb_descreader *r = closure; - upb_enumdef *e; - UPB_UNUSED(hd); - - if(!r->saw_number || !r->saw_name) { - upb_status_seterrmsg(status, "Enum value missing name or number."); - return false; - } - e = upb_downcast_enumdef_mutable(upb_descreader_last(r)); - upb_enumdef_addval(e, r->name, r->number, status); - upb_gfree(r->name); - r->name = NULL; - return true; -} - -/** Handlers for google.protobuf.EnumDescriptorProto. *************************/ - -static bool enum_endmsg(void *closure, const void *hd, upb_status *status) { - upb_descreader *r = closure; - upb_enumdef *e; - UPB_UNUSED(hd); - - e = upb_downcast_enumdef_mutable(upb_descreader_last(r)); - if (upb_def_fullname(upb_descreader_last(r)) == NULL) { - upb_status_seterrmsg(status, "Enum had no name."); - return false; - } - if (upb_enumdef_numvals(e) == 0) { - upb_status_seterrmsg(status, "Enum had no values."); - return false; - } - return true; -} - -static size_t enum_onname(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - char *fullname = upb_gstrndup(buf, n); - UPB_UNUSED(hd); - UPB_UNUSED(handle); - /* XXX: see comment at the top of the file. */ - upb_def_setfullname(upb_descreader_last(r), fullname, NULL); - upb_gfree(fullname); - return n; -} - -/** Handlers for google.protobuf.FieldDescriptorProto *************************/ - -static bool field_startmsg(void *closure, const void *hd) { - upb_descreader *r = closure; - UPB_UNUSED(hd); - UPB_ASSERT(r->f); - upb_gfree(r->default_string); - r->default_string = NULL; - - /* fielddefs default to packed, but descriptors default to non-packed. */ - upb_fielddef_setpacked(r->f, false); - return true; -} - -/* Converts the default value in string "str" into "d". Passes a ref on str. - * Returns true on success. */ -static bool parse_default(char *str, upb_fielddef *f) { - bool success = true; - char *end; - switch (upb_fielddef_type(f)) { - case UPB_TYPE_INT32: { - long val = strtol(str, &end, 0); - if (val > INT32_MAX || val < INT32_MIN || errno == ERANGE || *end) - success = false; - else - upb_fielddef_setdefaultint32(f, val); - break; - } - case UPB_TYPE_INT64: { - /* XXX: Need to write our own strtoll, since it's not available in c89. */ - long long val = strtol(str, &end, 0); - if (val > INT64_MAX || val < INT64_MIN || errno == ERANGE || *end) - success = false; - else - upb_fielddef_setdefaultint64(f, val); - break; - } - case UPB_TYPE_UINT32: { - unsigned long val = strtoul(str, &end, 0); - if (val > UINT32_MAX || errno == ERANGE || *end) - success = false; - else - upb_fielddef_setdefaultuint32(f, val); - break; - } - case UPB_TYPE_UINT64: { - /* XXX: Need to write our own strtoull, since it's not available in c89. */ - unsigned long long val = strtoul(str, &end, 0); - if (val > UINT64_MAX || errno == ERANGE || *end) - success = false; - else - upb_fielddef_setdefaultuint64(f, val); - break; - } - case UPB_TYPE_DOUBLE: { - double val = strtod(str, &end); - if (errno == ERANGE || *end) - success = false; - else - upb_fielddef_setdefaultdouble(f, val); - break; - } - case UPB_TYPE_FLOAT: { - /* XXX: Need to write our own strtof, since it's not available in c89. */ - float val = strtod(str, &end); - if (errno == ERANGE || *end) - success = false; - else - upb_fielddef_setdefaultfloat(f, val); - break; - } - case UPB_TYPE_BOOL: { - if (strcmp(str, "false") == 0) - upb_fielddef_setdefaultbool(f, false); - else if (strcmp(str, "true") == 0) - upb_fielddef_setdefaultbool(f, true); - else - success = false; - break; - } - default: abort(); - } - return success; -} - -static bool field_endmsg(void *closure, const void *hd, upb_status *status) { - upb_descreader *r = closure; - upb_fielddef *f = r->f; - UPB_UNUSED(hd); - - /* TODO: verify that all required fields were present. */ - UPB_ASSERT(upb_fielddef_number(f) != 0); - UPB_ASSERT(upb_fielddef_name(f) != NULL); - UPB_ASSERT((upb_fielddef_subdefname(f) != NULL) == upb_fielddef_hassubdef(f)); - - if (r->default_string) { - if (upb_fielddef_issubmsg(f)) { - upb_status_seterrmsg(status, "Submessages cannot have defaults."); - return false; - } - if (upb_fielddef_isstring(f) || upb_fielddef_type(f) == UPB_TYPE_ENUM) { - upb_fielddef_setdefaultcstr(f, r->default_string, NULL); - } else { - if (r->default_string && !parse_default(r->default_string, f)) { - /* We don't worry too much about giving a great error message since the - * compiler should have ensured this was correct. */ - upb_status_seterrmsg(status, "Error converting default value."); - return false; - } - } - } - return true; -} - -static bool field_onlazy(void *closure, const void *hd, bool val) { - upb_descreader *r = closure; - UPB_UNUSED(hd); - - upb_fielddef_setlazy(r->f, val); - return true; -} - -static bool field_onpacked(void *closure, const void *hd, bool val) { - upb_descreader *r = closure; - UPB_UNUSED(hd); - - upb_fielddef_setpacked(r->f, val); - return true; -} - -static bool field_ontype(void *closure, const void *hd, int32_t val) { - upb_descreader *r = closure; - UPB_UNUSED(hd); - - upb_fielddef_setdescriptortype(r->f, val); - return true; -} - -static bool field_onlabel(void *closure, const void *hd, int32_t val) { - upb_descreader *r = closure; - UPB_UNUSED(hd); - - upb_fielddef_setlabel(r->f, val); - return true; -} - -static bool field_onnumber(void *closure, const void *hd, int32_t val) { - upb_descreader *r = closure; - bool ok; - UPB_UNUSED(hd); - - ok = upb_fielddef_setnumber(r->f, val, NULL); - UPB_ASSERT(ok); - return true; -} - -static size_t field_onname(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - char *name = upb_gstrndup(buf, n); - UPB_UNUSED(hd); - UPB_UNUSED(handle); - - /* XXX: see comment at the top of the file. */ - upb_fielddef_setname(r->f, name, NULL); - upb_gfree(name); - return n; -} - -static size_t field_ontypename(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - char *name = upb_gstrndup(buf, n); - UPB_UNUSED(hd); - UPB_UNUSED(handle); - - /* XXX: see comment at the top of the file. */ - upb_fielddef_setsubdefname(r->f, name, NULL); - upb_gfree(name); - return n; -} - -static size_t field_onextendee(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - char *name = upb_gstrndup(buf, n); - UPB_UNUSED(hd); - UPB_UNUSED(handle); - - /* XXX: see comment at the top of the file. */ - upb_fielddef_setcontainingtypename(r->f, name, NULL); - upb_gfree(name); - return n; -} - -static size_t field_ondefaultval(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - UPB_UNUSED(hd); - UPB_UNUSED(handle); - - /* Have to convert from string to the correct type, but we might not know the - * type yet, so we save it as a string until the end of the field. - * XXX: see comment at the top of the file. */ - upb_gfree(r->default_string); - r->default_string = upb_gstrndup(buf, n); - return n; -} - -static bool field_ononeofindex(void *closure, const void *hd, int32_t index) { - upb_descreader *r = closure; - upb_oneofdef *o = upb_descreader_getoneof(r, index); - bool ok = upb_oneofdef_addfield(o, r->f, &r->f, NULL); - UPB_UNUSED(hd); - - UPB_ASSERT(ok); - return true; -} - -/** Handlers for google.protobuf.OneofDescriptorProto. ************************/ - -static size_t oneof_name(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - upb_descreader_frame *f = &r->stack[r->stack_len-1]; - upb_oneofdef *o = upb_descreader_getoneof(r, f->oneof_index++); - char *name_null_terminated = upb_gstrndup(buf, n); - bool ok = upb_oneofdef_setname(o, name_null_terminated, NULL); - UPB_UNUSED(hd); - UPB_UNUSED(handle); - - UPB_ASSERT(ok); - free(name_null_terminated); - return n; -} - -/** Handlers for google.protobuf.DescriptorProto ******************************/ - -static bool msg_start(void *closure, const void *hd) { - upb_descreader *r = closure; - UPB_UNUSED(hd); - - upb_descreader_startcontainer(r); - return true; -} - -static bool msg_end(void *closure, const void *hd, upb_status *status) { - upb_descreader *r = closure; - upb_msgdef *m = upb_descreader_top(r); - UPB_UNUSED(hd); - - if(!upb_def_fullname(upb_msgdef_upcast_mutable(m))) { - upb_status_seterrmsg(status, "Encountered message with no name."); - return false; - } - return upb_descreader_endcontainer(r); -} - -static size_t msg_name(void *closure, const void *hd, const char *buf, - size_t n, const upb_bufhandle *handle) { - upb_descreader *r = closure; - upb_msgdef *m = upb_descreader_top(r); - /* XXX: see comment at the top of the file. */ - char *name = upb_gstrndup(buf, n); - UPB_UNUSED(hd); - UPB_UNUSED(handle); - - upb_def_setfullname(upb_msgdef_upcast_mutable(m), name, NULL); - upb_descreader_setscopename(r, name); /* Passes ownership of name. */ - return n; -} - -static void *msg_startmsg(void *closure, const void *hd) { - upb_descreader *r = closure; - upb_msgdef *m = upb_msgdef_new(&m); - bool ok = upb_filedef_addmsg(r->file, m, &m, NULL); - UPB_UNUSED(hd); - UPB_ASSERT(ok); - return r; -} - -static void *msg_startext(void *closure, const void *hd) { - upb_descreader *r = closure; - upb_fielddef *f = upb_fielddef_new(&f); - bool ok = upb_filedef_addext(r->file, f, &f, NULL); - UPB_UNUSED(hd); - UPB_ASSERT(ok); - return r; -} - -static void *msg_startfield(void *closure, const void *hd) { - upb_descreader *r = closure; - r->f = upb_fielddef_new(&r->f); - /* We can't add the new field to the message until its name/number are - * filled in. */ - UPB_UNUSED(hd); - return r; -} - -static bool msg_endfield(void *closure, const void *hd) { - upb_descreader *r = closure; - upb_msgdef *m = upb_descreader_top(r); - bool ok; - UPB_UNUSED(hd); - - /* Oneof fields are added to the msgdef through their oneof, so don't need to - * be added here. */ - if (upb_fielddef_containingoneof(r->f) == NULL) { - ok = upb_msgdef_addfield(m, r->f, &r->f, NULL); - UPB_ASSERT(ok); - } - r->f = NULL; - return true; -} - -static bool msg_onmapentry(void *closure, const void *hd, bool mapentry) { - upb_descreader *r = closure; - upb_msgdef *m = upb_descreader_top(r); - UPB_UNUSED(hd); - - upb_msgdef_setmapentry(m, mapentry); - r->f = NULL; - return true; -} - - - -/** Code to register handlers *************************************************/ - -#define F(msg, field) upbdefs_google_protobuf_ ## msg ## _f_ ## field(m) - -static void reghandlers(const void *closure, upb_handlers *h) { - const upb_msgdef *m = upb_handlers_msgdef(h); - UPB_UNUSED(closure); - - if (upbdefs_google_protobuf_FileDescriptorSet_is(m)) { - upb_handlers_setstartsubmsg(h, F(FileDescriptorSet, file), - &fileset_startfile, NULL); - } else if (upbdefs_google_protobuf_DescriptorProto_is(m)) { - upb_handlers_setstartmsg(h, &msg_start, NULL); - upb_handlers_setendmsg(h, &msg_end, NULL); - upb_handlers_setstring(h, F(DescriptorProto, name), &msg_name, NULL); - upb_handlers_setstartsubmsg(h, F(DescriptorProto, extension), &msg_startext, - NULL); - upb_handlers_setstartsubmsg(h, F(DescriptorProto, nested_type), - &msg_startmsg, NULL); - upb_handlers_setstartsubmsg(h, F(DescriptorProto, field), - &msg_startfield, NULL); - upb_handlers_setendsubmsg(h, F(DescriptorProto, field), - &msg_endfield, NULL); - upb_handlers_setstartsubmsg(h, F(DescriptorProto, enum_type), - &file_startenum, NULL); - } else if (upbdefs_google_protobuf_FileDescriptorProto_is(m)) { - upb_handlers_setstartmsg(h, &file_start, NULL); - upb_handlers_setendmsg(h, &file_end, NULL); - upb_handlers_setstring(h, F(FileDescriptorProto, name), &file_onname, - NULL); - upb_handlers_setstring(h, F(FileDescriptorProto, package), &file_onpackage, - NULL); - upb_handlers_setstring(h, F(FileDescriptorProto, syntax), &file_onsyntax, - NULL); - upb_handlers_setstartsubmsg(h, F(FileDescriptorProto, message_type), - &file_startmsg, NULL); - upb_handlers_setstartsubmsg(h, F(FileDescriptorProto, enum_type), - &file_startenum, NULL); - upb_handlers_setstartsubmsg(h, F(FileDescriptorProto, extension), - &file_startext, NULL); - upb_handlers_setstring(h, F(FileDescriptorProto, dependency), - &file_ondep, NULL); - } else if (upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)) { - upb_handlers_setstartmsg(h, &enumval_startmsg, NULL); - upb_handlers_setendmsg(h, &enumval_endmsg, NULL); - upb_handlers_setstring(h, F(EnumValueDescriptorProto, name), &enumval_onname, NULL); - upb_handlers_setint32(h, F(EnumValueDescriptorProto, number), &enumval_onnumber, - NULL); - } else if (upbdefs_google_protobuf_EnumDescriptorProto_is(m)) { - upb_handlers_setendmsg(h, &enum_endmsg, NULL); - upb_handlers_setstring(h, F(EnumDescriptorProto, name), &enum_onname, NULL); - } else if (upbdefs_google_protobuf_FieldDescriptorProto_is(m)) { - upb_handlers_setstartmsg(h, &field_startmsg, NULL); - upb_handlers_setendmsg(h, &field_endmsg, NULL); - upb_handlers_setint32(h, F(FieldDescriptorProto, type), &field_ontype, - NULL); - upb_handlers_setint32(h, F(FieldDescriptorProto, label), &field_onlabel, - NULL); - upb_handlers_setint32(h, F(FieldDescriptorProto, number), &field_onnumber, - NULL); - upb_handlers_setstring(h, F(FieldDescriptorProto, name), &field_onname, - NULL); - upb_handlers_setstring(h, F(FieldDescriptorProto, type_name), - &field_ontypename, NULL); - upb_handlers_setstring(h, F(FieldDescriptorProto, extendee), - &field_onextendee, NULL); - upb_handlers_setstring(h, F(FieldDescriptorProto, default_value), - &field_ondefaultval, NULL); - upb_handlers_setint32(h, F(FieldDescriptorProto, oneof_index), - &field_ononeofindex, NULL); - } else if (upbdefs_google_protobuf_OneofDescriptorProto_is(m)) { - upb_handlers_setstring(h, F(OneofDescriptorProto, name), &oneof_name, NULL); - } else if (upbdefs_google_protobuf_FieldOptions_is(m)) { - upb_handlers_setbool(h, F(FieldOptions, lazy), &field_onlazy, NULL); - upb_handlers_setbool(h, F(FieldOptions, packed), &field_onpacked, NULL); - } else if (upbdefs_google_protobuf_MessageOptions_is(m)) { - upb_handlers_setbool(h, F(MessageOptions, map_entry), &msg_onmapentry, NULL); - } else if (upbdefs_google_protobuf_FileOptions_is(m)) { - upb_handlers_setstring(h, F(FileOptions, php_class_prefix), - &file_onphpprefix, NULL); - upb_handlers_setstartstr(h, F(FileOptions, php_namespace), - &file_startphpnamespace, NULL); - upb_handlers_setstring(h, F(FileOptions, php_namespace), - &file_onphpnamespace, NULL); - } - - UPB_ASSERT(upb_ok(upb_handlers_status(h))); -} - -#undef F - -void descreader_cleanup(void *_r) { - upb_descreader *r = _r; - size_t i; - - for (i = 0; i < upb_descreader_filecount(r); i++) { - upb_filedef_unref(upb_descreader_file(r, i), &r->files); - } - - upb_gfree(r->name); - upb_inttable_uninit(&r->files); - upb_strtable_uninit(&r->files_by_name); - upb_inttable_uninit(&r->oneofs); - upb_gfree(r->default_string); - while (r->stack_len > 0) { - upb_descreader_frame *f = &r->stack[--r->stack_len]; - upb_gfree(f->name); - } -} - - -/* Public API ****************************************************************/ - -upb_descreader *upb_descreader_create(upb_env *e, const upb_handlers *h) { - upb_descreader *r = upb_env_malloc(e, sizeof(upb_descreader)); - if (!r || !upb_env_addcleanup(e, descreader_cleanup, r)) { - return NULL; - } - - upb_inttable_init(&r->files, UPB_CTYPE_PTR); - upb_strtable_init(&r->files_by_name, UPB_CTYPE_PTR); - upb_inttable_init(&r->oneofs, UPB_CTYPE_PTR); - upb_sink_reset(upb_descreader_input(r), h, r); - r->stack_len = 0; - r->name = NULL; - r->default_string = NULL; - - return r; -} - -size_t upb_descreader_filecount(const upb_descreader *r) { - return upb_inttable_count(&r->files); -} - -upb_filedef *upb_descreader_file(const upb_descreader *r, size_t i) { - upb_value v; - if (upb_inttable_lookup(&r->files, i, &v)) { - return upb_value_getptr(v); - } else { - return NULL; - } -} - -upb_sink *upb_descreader_input(upb_descreader *r) { - return &r->sink; -} - -const upb_handlers *upb_descreader_newhandlers(const void *owner) { - const upb_msgdef *m = upbdefs_google_protobuf_FileDescriptorSet_get(&m); - const upb_handlers *h = upb_handlers_newfrozen(m, owner, reghandlers, NULL); - upb_msgdef_unref(m, &m); - return h; -} diff --git a/upb/descriptor/reader.h b/upb/descriptor/reader.h deleted file mode 100644 index e8ede0d..0000000 --- a/upb/descriptor/reader.h +++ /dev/null @@ -1,83 +0,0 @@ -/* -** upb::descriptor::Reader (upb_descreader) -** -** Provides a way of building upb::Defs from data in descriptor.proto format. -*/ - -#ifndef UPB_DESCRIPTOR_H -#define UPB_DESCRIPTOR_H - -#include "upb/sink.h" - -#ifdef __cplusplus -namespace upb { -namespace descriptor { -class Reader; -} /* namespace descriptor */ -} /* namespace upb */ -#endif - -UPB_DECLARE_TYPE(upb::descriptor::Reader, upb_descreader) - -#ifdef __cplusplus - -/* Class that receives descriptor data according to the descriptor.proto schema - * and use it to build upb::Defs corresponding to that schema. */ -class upb::descriptor::Reader { - public: - /* These handlers must have come from NewHandlers() and must outlive the - * Reader. - * - * TODO: generate the handlers statically (like we do with the - * descriptor.proto defs) so that there is no need to pass this parameter (or - * to build/memory-manage the handlers at runtime at all). Unfortunately this - * is a bit tricky to implement for Handlers, but necessary to simplify this - * interface. */ - static Reader* Create(Environment* env, const Handlers* handlers); - - /* The reader's input; this is where descriptor.proto data should be sent. */ - Sink* input(); - - /* Use to get the FileDefs that have been parsed. */ - size_t file_count() const; - FileDef* file(size_t i) const; - - /* Builds and returns handlers for the reader, owned by "owner." */ - static Handlers* NewHandlers(const void* owner); - - private: - UPB_DISALLOW_POD_OPS(Reader, upb::descriptor::Reader) -}; - -#endif - -UPB_BEGIN_EXTERN_C - -/* C API. */ -upb_descreader *upb_descreader_create(upb_env *e, const upb_handlers *h); -upb_sink *upb_descreader_input(upb_descreader *r); -size_t upb_descreader_filecount(const upb_descreader *r); -upb_filedef *upb_descreader_file(const upb_descreader *r, size_t i); -const upb_handlers *upb_descreader_newhandlers(const void *owner); - -UPB_END_EXTERN_C - -#ifdef __cplusplus -/* C++ implementation details. ************************************************/ -namespace upb { -namespace descriptor { -inline Reader* Reader::Create(Environment* e, const Handlers *h) { - return upb_descreader_create(e, h); -} -inline Sink* Reader::input() { return upb_descreader_input(this); } -inline size_t Reader::file_count() const { - return upb_descreader_filecount(this); -} -inline FileDef* Reader::file(size_t i) const { - return upb_descreader_file(this, i); -} -} /* namespace descriptor */ -} /* namespace upb */ -#endif - -#endif /* UPB_DESCRIPTOR_H */ diff --git a/upb/encode.c b/upb/encode.c index bff8262..c874281 100644 --- a/upb/encode.c +++ b/upb/encode.c @@ -1,5 +1,6 @@ /* We encode backwards, to avoid pre-computing lengths (one-pass encode). */ +#include #include "upb/upb.h" #include "upb/encode.h" #include "upb/structs.int.h" diff --git a/upb/handlers.c b/upb/handlers.c index 0022c32..7382e0c 100644 --- a/upb/handlers.c +++ b/upb/handlers.c @@ -4,7 +4,6 @@ */ #include "upb/handlers.h" -#include "upb/structdefs.int.h" #include @@ -35,7 +34,6 @@ static void freehandlers(upb_refcounted *r) { } upb_inttable_uninit(&h->cleanup_); - upb_msgdef_unref(h->msg, h); upb_gfree(h->sub); upb_gfree(h); } @@ -108,7 +106,7 @@ oom: #define SUBH(h, selector) (h->sub[selector]) /* The selector for a submessage field is the field index. */ -#define SUBH_F(h, f) SUBH(h, f->index_) +#define SUBH_F(h, f) SUBH(h, upb_fielddef_index(f)) static int32_t trygetsel(upb_handlers *h, const upb_fielddef *f, upb_handlertype_t type) { @@ -284,18 +282,15 @@ upb_handlers *upb_handlers_new(const upb_msgdef *md, const void *owner) { int extra; upb_handlers *h; - UPB_ASSERT(upb_msgdef_isfrozen(md)); - - extra = sizeof(upb_handlers_tabent) * (md->selector_count - 1); + extra = sizeof(upb_handlers_tabent) * (upb_msgdef_selectorcount(md) - 1); h = upb_calloc(sizeof(*h) + extra); if (!h) return NULL; h->msg = md; - upb_msgdef_ref(h->msg, h); upb_status_clear(&h->status_); - if (md->submsg_field_count > 0) { - h->sub = upb_calloc(md->submsg_field_count * sizeof(*h->sub)); + if (upb_msgdef_submsgfieldcount(md) > 0) { + h->sub = upb_calloc(upb_msgdef_submsgfieldcount(md) * sizeof(*h->sub)); if (!h->sub) goto oom; } else { h->sub = 0; @@ -540,6 +535,7 @@ upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f) { bool upb_handlers_getselector(const upb_fielddef *f, upb_handlertype_t type, upb_selector_t *s) { + uint32_t selector_base = upb_fielddef_selectorbase(f); switch (type) { case UPB_HANDLER_INT32: case UPB_HANDLER_INT64: @@ -551,38 +547,38 @@ bool upb_handlers_getselector(const upb_fielddef *f, upb_handlertype_t type, if (!upb_fielddef_isprimitive(f) || upb_handlers_getprimitivehandlertype(f) != type) return false; - *s = f->selector_base; + *s = selector_base; break; case UPB_HANDLER_STRING: if (upb_fielddef_isstring(f)) { - *s = f->selector_base; + *s = selector_base; } else if (upb_fielddef_lazy(f)) { - *s = f->selector_base + 3; + *s = selector_base + 3; } else { return false; } break; case UPB_HANDLER_STARTSTR: if (upb_fielddef_isstring(f) || upb_fielddef_lazy(f)) { - *s = f->selector_base + 1; + *s = selector_base + 1; } else { return false; } break; case UPB_HANDLER_ENDSTR: if (upb_fielddef_isstring(f) || upb_fielddef_lazy(f)) { - *s = f->selector_base + 2; + *s = selector_base + 2; } else { return false; } break; case UPB_HANDLER_STARTSEQ: if (!upb_fielddef_isseq(f)) return false; - *s = f->selector_base - 2; + *s = selector_base - 2; break; case UPB_HANDLER_ENDSEQ: if (!upb_fielddef_isseq(f)) return false; - *s = f->selector_base - 1; + *s = selector_base - 1; break; case UPB_HANDLER_STARTSUBMSG: if (!upb_fielddef_issubmsg(f)) return false; @@ -590,14 +586,14 @@ bool upb_handlers_getselector(const upb_fielddef *f, upb_handlertype_t type, * selector can also be used as an index into the "sub" array of * subhandlers. The indexes for the two into these two tables are the * same, except that in the handler table the static selectors come first. */ - *s = f->index_ + UPB_STATIC_SELECTOR_COUNT; + *s = upb_fielddef_index(f) + UPB_STATIC_SELECTOR_COUNT; break; case UPB_HANDLER_ENDSUBMSG: if (!upb_fielddef_issubmsg(f)) return false; - *s = f->selector_base; + *s = selector_base; break; } - UPB_ASSERT((size_t)*s < upb_fielddef_containingtype(f)->selector_count); + UPB_ASSERT((size_t)*s < upb_msgdef_selectorcount(upb_fielddef_containingtype(f))); return true; } diff --git a/upb/handlers.h b/upb/handlers.h index a4e2a04..15d06a0 100644 --- a/upb/handlers.h +++ b/upb/handlers.h @@ -21,6 +21,7 @@ #include "upb/def.h" #include "upb/table.int.h" +#include "upb/refcounted.h" #ifdef __cplusplus namespace upb { diff --git a/upb/msg.c b/upb/msg.c index 29ba2e1..41260d6 100644 --- a/upb/msg.c +++ b/upb/msg.c @@ -1,4 +1,6 @@ +#include +#include "upb/table.int.h" #include "upb/msg.h" #include "upb/structs.int.h" diff --git a/upb/msg.h b/upb/msg.h index c72e9f0..3f0aa0b 100644 --- a/upb/msg.h +++ b/upb/msg.h @@ -21,9 +21,8 @@ #ifndef UPB_MSG_H_ #define UPB_MSG_H_ -#include "upb/def.h" -#include "upb/handlers.h" -#include "upb/sink.h" +#include +#include "upb/upb.h" #ifdef __cplusplus diff --git a/upb/msgfactory.c b/upb/msgfactory.c index 593c9dc..63df49e 100644 --- a/upb/msgfactory.c +++ b/upb/msgfactory.c @@ -1,4 +1,5 @@ +#include "upb/handlers.h" #include "upb/msgfactory.h" static bool is_power_of_two(size_t val) { diff --git a/upb/pb/decoder.int.h b/upb/pb/decoder.int.h index 4032570..f02bdd5 100644 --- a/upb/pb/decoder.int.h +++ b/upb/pb/decoder.int.h @@ -9,7 +9,6 @@ #include "upb/handlers.h" #include "upb/pb/decoder.h" #include "upb/sink.h" -#include "upb/structdefs.int.h" #include "upb/table.int.h" /* C++ names are not actually used since this type isn't exposed to users. */ diff --git a/upb/pb/glue.c b/upb/pb/glue.c deleted file mode 100644 index fb2b769..0000000 --- a/upb/pb/glue.c +++ /dev/null @@ -1,54 +0,0 @@ - -#include "upb/pb/glue.h" - -#include "upb/descriptor/reader.h" -#include "upb/pb/decoder.h" - -upb_filedef **upb_loaddescriptor(const char *buf, size_t n, const void *owner, - upb_status *status) { - /* Create handlers. */ - const upb_pbdecodermethod *decoder_m; - const upb_handlers *reader_h = upb_descreader_newhandlers(&reader_h); - upb_env env; - upb_pbdecodermethodopts opts; - upb_pbdecoder *decoder; - upb_descreader *reader; - bool ok; - size_t i; - upb_filedef **ret = NULL; - - upb_pbdecodermethodopts_init(&opts, reader_h); - decoder_m = upb_pbdecodermethod_new(&opts, &decoder_m); - - upb_env_init(&env); - upb_env_reporterrorsto(&env, status); - - reader = upb_descreader_create(&env, reader_h); - decoder = upb_pbdecoder_create(&env, decoder_m, upb_descreader_input(reader)); - - /* Push input data. */ - ok = upb_bufsrc_putbuf(buf, n, upb_pbdecoder_input(decoder)); - - if (!ok) { - goto cleanup; - } - - ret = upb_gmalloc(sizeof (*ret) * (upb_descreader_filecount(reader) + 1)); - - if (!ret) { - goto cleanup; - } - - for (i = 0; i < upb_descreader_filecount(reader); i++) { - ret[i] = upb_descreader_file(reader, i); - upb_filedef_ref(ret[i], owner); - } - - ret[i] = NULL; - -cleanup: - upb_env_uninit(&env); - upb_handlers_unref(reader_h, &reader_h); - upb_pbdecodermethod_unref(decoder_m, &decoder_m); - return ret; -} diff --git a/upb/pb/glue.h b/upb/pb/glue.h deleted file mode 100644 index 716fc0e..0000000 --- a/upb/pb/glue.h +++ /dev/null @@ -1,72 +0,0 @@ -/* -** upb's core components like upb_decoder and upb_msg are carefully designed to -** avoid depending on each other for maximum orthogonality. In other words, -** you can use a upb_decoder to decode into *any* kind of structure; upb_msg is -** just one such structure. A upb_msg can be serialized/deserialized into any -** format, protobuf binary format is just one such format. -** -** However, for convenience we provide functions here for doing common -** operations like deserializing protobuf binary format into a upb_msg. The -** compromise is that this file drags in almost all of upb as a dependency, -** which could be undesirable if you're trying to use a trimmed-down build of -** upb. -** -** While these routines are convenient, they do not reuse any encoding/decoding -** state. For example, if a decoder is JIT-based, it will be re-JITted every -** time these functions are called. For this reason, if you are parsing lots -** of data and efficiency is an issue, these may not be the best functions to -** use (though they are useful for prototyping, before optimizing). -*/ - -#ifndef UPB_GLUE_H -#define UPB_GLUE_H - -#include -#include "upb/def.h" - -#ifdef __cplusplus -#include - -extern "C" { -#endif - -/* Loads a binary descriptor and returns a NULL-terminated array of unfrozen - * filedefs. The caller owns the returned array, which must be freed with - * upb_gfree(). */ -upb_filedef **upb_loaddescriptor(const char *buf, size_t n, const void *owner, - upb_status *status); - -#ifdef __cplusplus -} /* extern "C" */ - -namespace upb { - -inline bool LoadDescriptor(const char* buf, size_t n, Status* status, - std::vector >* files) { - FileDef** parsed_files = upb_loaddescriptor(buf, n, &parsed_files, status); - - if (parsed_files) { - FileDef** p = parsed_files; - while (*p) { - files->push_back(reffed_ptr(*p, &parsed_files)); - ++p; - } - free(parsed_files); - return true; - } else { - return false; - } -} - -/* Templated so it can accept both string and std::string. */ -template -bool LoadDescriptor(const T& desc, Status* status, - std::vector >* files) { - return LoadDescriptor(desc.c_str(), desc.size(), status, files); -} - -} /* namespace upb */ - -#endif - -#endif /* UPB_GLUE_H */ diff --git a/upb/structdefs.int.h b/upb/structdefs.int.h deleted file mode 100644 index ff44e2d..0000000 --- a/upb/structdefs.int.h +++ /dev/null @@ -1,191 +0,0 @@ -/* -** This file contains definitions of structs that should be considered private -** and NOT stable across versions of upb. -** -** The only reason they are declared here and not in .c files is to allow upb -** and the application (if desired) to embed statically-initialized instances -** of structures like defs. -** -** If you include this file, all guarantees of ABI compatibility go out the -** window! Any code that includes this file needs to recompile against the -** exact same version of upb that they are linking against. -** -** You also need to recompile if you change the value of the UPB_DEBUG_REFS -** flag. -*/ - -#include "upb/def.h" - -#ifndef UPB_STATICINIT_H_ -#define UPB_STATICINIT_H_ - -#ifdef __cplusplus -/* Because of how we do our typedefs, this header can't be included from C++. */ -#error This file cannot be included from C++ -#endif - -/* upb_refcounted *************************************************************/ - - -/* upb_def ********************************************************************/ - -struct upb_def { - upb_refcounted base; - - const char *fullname; - const upb_filedef* file; - char type; /* A upb_deftype_t (char to save space) */ - - /* Used as a flag during the def's mutable stage. Must be false unless - * it is currently being used by a function on the stack. This allows - * us to easily determine which defs were passed into the function's - * current invocation. */ - bool came_from_user; -}; - -#define UPB_DEF_INIT(name, type, vtbl, refs, ref2s) \ - { UPB_REFCOUNT_INIT(vtbl, refs, ref2s), name, NULL, type, false } - - -/* upb_fielddef ***************************************************************/ - -struct upb_fielddef { - upb_def base; - - union { - int64_t sint; - uint64_t uint; - double dbl; - float flt; - void *bytes; - } defaultval; - union { - const upb_msgdef *def; /* If !msg_is_symbolic. */ - char *name; /* If msg_is_symbolic. */ - } msg; - union { - const upb_def *def; /* If !subdef_is_symbolic. */ - char *name; /* If subdef_is_symbolic. */ - } sub; /* The msgdef or enumdef for this field, if upb_hassubdef(f). */ - bool subdef_is_symbolic; - bool msg_is_symbolic; - const upb_oneofdef *oneof; - bool default_is_string; - bool type_is_set_; /* False until type is explicitly set. */ - bool is_extension_; - bool lazy_; - bool packed_; - upb_intfmt_t intfmt; - bool tagdelim; - upb_fieldtype_t type_; - upb_label_t label_; - uint32_t number_; - uint32_t selector_base; /* Used to index into a upb::Handlers table. */ - uint32_t index_; -}; - -extern const struct upb_refcounted_vtbl upb_fielddef_vtbl; - -#define UPB_FIELDDEF_INIT(label, type, intfmt, tagdelim, is_extension, lazy, \ - packed, name, num, msgdef, subdef, selector_base, \ - index, defaultval, refs, ref2s) \ - { \ - UPB_DEF_INIT(name, UPB_DEF_FIELD, &upb_fielddef_vtbl, refs, ref2s), \ - defaultval, {msgdef}, {subdef}, NULL, false, false, \ - type == UPB_TYPE_STRING || type == UPB_TYPE_BYTES, true, is_extension, \ - lazy, packed, intfmt, tagdelim, type, label, num, selector_base, index \ - } - - -/* upb_msgdef *****************************************************************/ - -struct upb_msgdef { - upb_def base; - - size_t selector_count; - uint32_t submsg_field_count; - - /* Tables for looking up fields by number and name. */ - upb_inttable itof; /* int to field */ - upb_strtable ntof; /* name to field/oneof */ - - /* Is this a map-entry message? */ - bool map_entry; - - /* Whether this message has proto2 or proto3 semantics. */ - upb_syntax_t syntax; - - /* TODO(haberman): proper extension ranges (there can be multiple). */ -}; - -extern const struct upb_refcounted_vtbl upb_msgdef_vtbl; - -/* TODO: also support static initialization of the oneofs table. This will be - * needed if we compile in descriptors that contain oneofs. */ -#define UPB_MSGDEF_INIT(name, selector_count, submsg_field_count, itof, ntof, \ - map_entry, syntax, refs, ref2s) \ - { \ - UPB_DEF_INIT(name, UPB_DEF_MSG, &upb_fielddef_vtbl, refs, ref2s), \ - selector_count, submsg_field_count, itof, ntof, map_entry, syntax \ - } - - -/* upb_enumdef ****************************************************************/ - -struct upb_enumdef { - upb_def base; - - upb_strtable ntoi; - upb_inttable iton; - int32_t defaultval; -}; - -extern const struct upb_refcounted_vtbl upb_enumdef_vtbl; - -#define UPB_ENUMDEF_INIT(name, ntoi, iton, defaultval, refs, ref2s) \ - { UPB_DEF_INIT(name, UPB_DEF_ENUM, &upb_enumdef_vtbl, refs, ref2s), ntoi, \ - iton, defaultval } - - -/* upb_oneofdef ***************************************************************/ - -struct upb_oneofdef { - upb_refcounted base; - - uint32_t index; /* Index within oneofs. */ - const char *name; - upb_strtable ntof; - upb_inttable itof; - const upb_msgdef *parent; -}; - -extern const struct upb_refcounted_vtbl upb_oneofdef_vtbl; - -#define UPB_ONEOFDEF_INIT(name, ntof, itof, refs, ref2s) \ - { UPB_REFCOUNT_INIT(&upb_oneofdef_vtbl, refs, ref2s), 0, name, ntof, itof } - - -/* upb_symtab *****************************************************************/ - -struct upb_symtab { - upb_refcounted base; - - upb_strtable symtab; -}; - -struct upb_filedef { - upb_refcounted base; - - const char *name; - const char *package; - const char *phpprefix; - const char *phpnamespace; - upb_syntax_t syntax; - - upb_inttable defs; - upb_inttable deps; -}; - -extern const struct upb_refcounted_vtbl upb_filedef_vtbl; - -#endif /* UPB_STATICINIT_H_ */ diff --git a/upb/table.int.h b/upb/table.int.h index 225235e..63dce59 100644 --- a/upb/table.int.h +++ b/upb/table.int.h @@ -180,69 +180,11 @@ UPB_INLINE char *upb_tabstr(upb_tabkey key, uint32_t *len) { /* upb_tabval *****************************************************************/ -#ifdef __cplusplus - -/* Status initialization not supported. - * - * This separate definition is necessary because in C++, UINTPTR_MAX isn't - * reliably available. */ typedef struct { uint64_t val; } upb_tabval; -#else - -/* C -- supports static initialization, but to support static initialization of - * both integers and points for both 32 and 64 bit targets, it takes a little - * bit of doing. */ - -#if UINTPTR_MAX == 0xffffffffffffffffULL -#define UPB_PTR_IS_64BITS -#elif UINTPTR_MAX != 0xffffffff -#error Could not determine how many bits pointers are. -#endif - -typedef union { - /* For static initialization. - * - * Unfortunately this ugliness is necessary -- it is the only way that we can, - * with -std=c89 -pedantic, statically initialize this to either a pointer or - * an integer on 32-bit platforms. */ - struct { -#ifdef UPB_PTR_IS_64BITS - uintptr_t val; -#else - uintptr_t val1; - uintptr_t val2; -#endif - } staticinit; - - /* The normal accessor that we use for everything at runtime. */ - uint64_t val; -} upb_tabval; - -#ifdef UPB_PTR_IS_64BITS -#define UPB_TABVALUE_INT_INIT(v) {{v}} -#define UPB_TABVALUE_EMPTY_INIT {{-1}} -#else - -/* 32-bit pointers */ - -#ifdef UPB_BIG_ENDIAN -#define UPB_TABVALUE_INT_INIT(v) {{0, v}} -#define UPB_TABVALUE_EMPTY_INIT {{-1, -1}} -#else -#define UPB_TABVALUE_INT_INIT(v) {{v, 0}} -#define UPB_TABVALUE_EMPTY_INIT {{-1, -1}} -#endif - -#endif - -#define UPB_TABVALUE_PTR_INIT(v) UPB_TABVALUE_INT_INIT((uintptr_t)v) - -#undef UPB_PTR_IS_64BITS - -#endif /* __cplusplus */ +#define UPB_TABVALUE_EMPTY_INIT {-1} /* upb_table ******************************************************************/ @@ -284,31 +226,10 @@ typedef struct { #endif } upb_table; -#ifdef NDEBUG -# define UPB_TABLE_INIT(count, mask, ctype, size_lg2, entries) \ - {count, mask, ctype, size_lg2, entries} -#else -# ifdef UPB_DEBUG_REFS -/* At the moment the only mutable tables we statically initialize are debug - * ref tables. */ -# define UPB_TABLE_INIT(count, mask, ctype, size_lg2, entries) \ - {count, mask, ctype, size_lg2, entries, &upb_alloc_debugrefs} -# else -# define UPB_TABLE_INIT(count, mask, ctype, size_lg2, entries) \ - {count, mask, ctype, size_lg2, entries, NULL} -# endif -#endif - typedef struct { upb_table t; } upb_strtable; -#define UPB_STRTABLE_INIT(count, mask, ctype, size_lg2, entries) \ - {UPB_TABLE_INIT(count, mask, ctype, size_lg2, entries)} - -#define UPB_EMPTY_STRTABLE_INIT(ctype) \ - UPB_STRTABLE_INIT(0, 0, ctype, 0, NULL) - typedef struct { upb_table t; /* For entries that don't fit in the array part. */ const upb_tabval *array; /* Array part of the table. See const note above. */ diff --git a/upb/upb.h b/upb/upb.h index 2d6db8e..1c28488 100644 --- a/upb/upb.h +++ b/upb/upb.h @@ -12,6 +12,7 @@ #include #include #include +#include #ifdef __cplusplus namespace upb { @@ -526,6 +527,13 @@ size_t upb_arena_bytesallocated(const upb_arena *a); void upb_arena_setnextblocksize(upb_arena *a, size_t size); void upb_arena_setmaxblocksize(upb_arena *a, size_t size); UPB_INLINE upb_alloc *upb_arena_alloc(upb_arena *a) { return (upb_alloc*)a; } +UPB_INLINE void *upb_arena_malloc(upb_arena *a, size_t size) { + return upb_malloc(upb_arena_alloc(a), size); +} +UPB_INLINE void *upb_arena_realloc(upb_arena *a, void *ptr, size_t oldsize, + size_t size) { + return upb_malloc(upb_arena_realloc(a), ptr, oldsize, size); +} UPB_END_EXTERN_C @@ -733,6 +741,69 @@ template class upb::InlinedEnvironment : public upb::Environment { #endif /* __cplusplus */ +/* The types a field can have. Note that this list is not identical to the + * types defined in descriptor.proto, which gives INT32 and SINT32 separate + * types (we distinguish the two with the "integer encoding" enum below). */ +typedef enum { + /* Types stored in 1 byte. */ + UPB_TYPE_BOOL = 1, + /* Types stored in 4 bytes. */ + UPB_TYPE_FLOAT = 2, + UPB_TYPE_INT32 = 3, + UPB_TYPE_UINT32 = 4, + UPB_TYPE_ENUM = 5, /* Enum values are int32. */ + /* Types stored as pointers (probably 4 or 8 bytes). */ + UPB_TYPE_STRING = 6, + UPB_TYPE_BYTES = 7, + UPB_TYPE_MESSAGE = 8, + /* Types stored as 8 bytes. */ + UPB_TYPE_DOUBLE = 9, + UPB_TYPE_INT64 = 10, + UPB_TYPE_UINT64 = 11 +} upb_fieldtype_t; + +/* The repeated-ness of each field; this matches descriptor.proto. */ +typedef enum { + UPB_LABEL_OPTIONAL = 1, + UPB_LABEL_REQUIRED = 2, + UPB_LABEL_REPEATED = 3 +} upb_label_t; + +/* How integers should be encoded in serializations that offer multiple + * integer encoding methods. */ +typedef enum { + UPB_INTFMT_VARIABLE = 1, + UPB_INTFMT_FIXED = 2, + UPB_INTFMT_ZIGZAG = 3 /* Only for signed types (INT32/INT64). */ +} upb_intfmt_t; + +/* Descriptor types, as defined in descriptor.proto. */ +typedef enum { + UPB_DESCRIPTOR_TYPE_DOUBLE = 1, + UPB_DESCRIPTOR_TYPE_FLOAT = 2, + UPB_DESCRIPTOR_TYPE_INT64 = 3, + UPB_DESCRIPTOR_TYPE_UINT64 = 4, + UPB_DESCRIPTOR_TYPE_INT32 = 5, + UPB_DESCRIPTOR_TYPE_FIXED64 = 6, + UPB_DESCRIPTOR_TYPE_FIXED32 = 7, + UPB_DESCRIPTOR_TYPE_BOOL = 8, + UPB_DESCRIPTOR_TYPE_STRING = 9, + UPB_DESCRIPTOR_TYPE_GROUP = 10, + UPB_DESCRIPTOR_TYPE_MESSAGE = 11, + UPB_DESCRIPTOR_TYPE_BYTES = 12, + UPB_DESCRIPTOR_TYPE_UINT32 = 13, + UPB_DESCRIPTOR_TYPE_ENUM = 14, + UPB_DESCRIPTOR_TYPE_SFIXED32 = 15, + UPB_DESCRIPTOR_TYPE_SFIXED64 = 16, + UPB_DESCRIPTOR_TYPE_SINT32 = 17, + UPB_DESCRIPTOR_TYPE_SINT64 = 18 +} upb_descriptortype_t; + +typedef enum { + UPB_SYNTAX_PROTO2 = 2, + UPB_SYNTAX_PROTO3 = 3 +} upb_syntax_t; +extern const uint8_t upb_desctype_to_fieldtype[]; #endif /* UPB_H_ */ -- cgit v1.2.3 From 336402b4d7251d37239d2f7ff8259174525baa38 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Mon, 10 Dec 2018 10:18:48 -0800 Subject: WIP, core library compiles now. --- BUILD | 33 +-- CMakeLists.txt | 216 +------------- upb/bindings/lua/def.c | 780 ++++++------------------------------------------- upb/def.c | 722 ++++++++++++++++++++++++++------------------- upb/def.h | 13 +- upb/handlers.c | 4 +- upb/json/parser.c | 126 ++++---- upb/json/parser.h | 3 +- upb/json/parser.rl | 20 +- upb/json/printer.c | 2 +- upb/msg.h | 9 + upb/pb/textprinter.c | 4 +- upb/structdefs.int.h | 196 ------------- 13 files changed, 602 insertions(+), 1526 deletions(-) delete mode 100644 upb/structdefs.int.h (limited to 'upb') diff --git a/BUILD b/BUILD index 96418f1..df05e06 100644 --- a/BUILD +++ b/BUILD @@ -27,7 +27,6 @@ cc_library( "upb/port_undef.inc", "upb/refcounted.c", "upb/sink.c", - "upb/structdefs.int.h", "upb/structs.int.h", "upb/table.c", "upb/table.int.h", @@ -429,13 +428,6 @@ proto_library( ], ) -genrule( - name = "copy_google_descriptor_pb", - srcs = [":google_descriptor_proto"], - outs = ["generated/google/protobuf/descriptor.pb"], - cmd = "cp $< $@", -) - genrule( name = "generate_descriptor_c", srcs = ["google/protobuf/descriptor.proto"], @@ -462,16 +454,16 @@ genrule( cmd = "cp $< $@", ) -genrule( - name = "generated_json_test_proto_upbdefs", - srcs = ["generated/tests/json/test.proto.pb"], - outs = [ - "generated/tests/json/test.upbdefs.h", - "generated/tests/json/test.upbdefs.c", - ], - cmd = "UPBC=$$PWD/$(location :lua_upbc); INFILE=$$PWD/$<; cd $(GENDIR)/generated && $$UPBC --generate-upbdefs $$INFILE", - tools = [":lua_upbc"], -) +#genrule( +# name = "generated_json_test_proto_upbdefs", +# srcs = ["generated/tests/json/test.proto.pb"], +# outs = [ +# "generated/tests/json/test.upbdefs.h", +# "generated/tests/json/test.upbdefs.c", +# ], +# cmd = "UPBC=$$PWD/$(location :lua_upbc); INFILE=$$PWD/$<; cd $(GENDIR)/generated && $$UPBC --generate-upbdefs $$INFILE", +# tools = [":lua_upbc"], +#) genrule( name = "generate_json_ragel", @@ -488,11 +480,6 @@ generated_file_staleness_test( "google/protobuf/descriptor.upb.c", "google/protobuf/descriptor.upb.h", "tests/json/test.proto.pb", - "tests/json/test.upbdefs.c", - "tests/json/test.upbdefs.h", - "upb/descriptor/descriptor.pb", - "upb/descriptor/descriptor.upbdefs.c", - "upb/descriptor/descriptor.upbdefs.h", "upb/json/parser.c", "upb/pb/compile_decoder_x64.h", ], diff --git a/CMakeLists.txt b/CMakeLists.txt index 4fb3b53..26ac066 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,209 +57,6 @@ elseif(UNIX) set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--build-id") endif() -<<<<<<< HEAD -FIND_PACKAGE(Lua) -if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/third_party/protobuf/Makefile.am") - set(PROTOBUF_FOUND TRUE) -endif() -find_program(RAGEL NAMES ragel) - -if(LUA_FOUND) - include_directories(${LUA_INCLUDE_DIR}) - - add_library(upb_c SHARED - upb/bindings/lua/upb.c - upb/bindings/lua/def.c - upb/bindings/lua/msg.c - ) - target_link_libraries(upb_c LINK_PRIVATE - upbpb_pic - upbdef_pic - upbhandlers_pic upb_pic ) - - add_library(pb_c SHARED - upb/bindings/lua/upb/pb.c - ) - target_link_libraries(pb_c LINK_PRIVATE upb_c upbpb_pic) - - set_target_properties(upb_c - PROPERTIES - LIBRARY_OUTPUT_DIRECTORY "upb/bindings/lua" - PREFIX "" - SUFFIX ".so") - set_target_properties(pb_c - PROPERTIES - LIBRARY_OUTPUT_DIRECTORY "upb/bindings/lua/upb" - PREFIX "" - SUFFIX ".so") - - add_custom_command( - OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/tools/upbc - DEPENDS ${CMAKE_SOURCE_DIR}/tools/upbc - ${CMAKE_SOURCE_DIR}/tools/upbc.lua - ${CMAKE_SOURCE_DIR}/tools/make_c_api.lua - ${CMAKE_SOURCE_DIR}/upb/bindings/lua/upb.lua - ${CMAKE_SOURCE_DIR}/upb/bindings/lua/upb/pb.lua - upb_c - pb_c - COMMAND ${CMAKE_COMMAND} -E copy - ${CMAKE_SOURCE_DIR}/tools/upbc - ${CMAKE_SOURCE_DIR}/tools/upbc.lua - ${CMAKE_SOURCE_DIR}/tools/make_c_api.lua - ${CMAKE_CURRENT_BINARY_DIR}/tools) - add_custom_command( - OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/lua/upb.lua - DEPENDS ${CMAKE_SOURCE_DIR}/upb/bindings/lua/upb.lua - COMMAND ${CMAKE_COMMAND} -E copy - ${CMAKE_SOURCE_DIR}/upb/bindings/lua/upb.lua - ${CMAKE_CURRENT_BINARY_DIR}/upb/bindings/lua) - add_custom_command( - OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/lua/upb/pb.lua - DEPENDS ${CMAKE_SOURCE_DIR}/upb/bindings/lua/upb/pb.lua - COMMAND ${CMAKE_COMMAND} -E copy - ${CMAKE_SOURCE_DIR}/upb/bindings/lua/upb/pb.lua - ${CMAKE_CURRENT_BINARY_DIR}/upb/bindings/lua/upb) - - add_custom_command( - OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/upb/pb/compile_decoder_x64.h - DEPENDS upb/pb/compile_decoder_x64.dasc - COMMAND - cd ${CMAKE_CURRENT_SOURCE_DIR} && - lua third_party/dynasm/dynasm.lua - -c upb/pb/compile_decoder_x64.dasc - > ${CMAKE_CURRENT_BINARY_DIR}/upb/pb/compile_decoder_x64.h - ) - - add_custom_target( - genfiles2 ALL - DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/upb/pb/compile_decoder_x64.h - ) - - add_test( - NAME testlua - COMMAND lua ${CMAKE_SOURCE_DIR}/tests/bindings/lua/test_upb.lua - ${CMAKE_SOURCE_DIR}/tests/bindings/lua/test_upb.pb.lua - ) - set_property(TEST testlua PROPERTY ENVIRONMENT - LUA_PATH=${CMAKE_SOURCE_DIR}/third_party/lunit/?.lua$${CMAKE_SOURCE_DIR}/upb/bindings/lua/?.lua - LUA_CPATH=${CMAKE_BINARY_DIR}/upb/bindings/lua/?.so - ) - set_tests_properties(testlua - PROPERTIES - DEPENDS "upb_c;pb_c" - ) - enable_testing() -endif() - -if(LUA_FOUND AND PROTOBUF_FOUND) - set(protobuf_BUILD_TESTS OFF CACHE BOOL "enable tests for proto2" FORCE) - set(protobuf_BUILD_SHARED_LIBS OFF CACHE BOOL "enable shared libs for proto2" FORCE) - set(protobuf_BUILD_CONFORMANCE ON CACHE BOOL "Build conformance tests" FORCE) - add_subdirectory(third_party/protobuf/cmake) - - add_custom_target( - upbc ALL - DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/tools/make_c_api.lua - ${CMAKE_CURRENT_SOURCE_DIR}/tools/upbc.lua - ${CMAKE_CURRENT_BINARY_DIR}/tools/upbc - ${CMAKE_CURRENT_BINARY_DIR}/lua/upb.lua - ${CMAKE_CURRENT_BINARY_DIR}/lua/upb/pb.lua - ) - - add_custom_command( - OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/conformance.upb.h - ${CMAKE_CURRENT_BINARY_DIR}/google/protobuf/test_messages_proto3.upb.h - ${CMAKE_CURRENT_BINARY_DIR}/google/protobuf/test_messages_proto3.upb.c - ${CMAKE_CURRENT_BINARY_DIR}/google/protobuf/any.upb.c - ${CMAKE_CURRENT_BINARY_DIR}/google/protobuf/duration.upb.c - ${CMAKE_CURRENT_BINARY_DIR}/google/protobuf/field_mask.upb.c - ${CMAKE_CURRENT_BINARY_DIR}/google/protobuf/struct.upb.c - ${CMAKE_CURRENT_BINARY_DIR}/google/protobuf/timestamp.upb.c - ${CMAKE_CURRENT_BINARY_DIR}/google/protobuf/wrappers.upb.c - ${CMAKE_CURRENT_BINARY_DIR}/conformance.upb.c - DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/tools/upbc - ${CMAKE_CURRENT_SOURCE_DIR}/third_party/protobuf/conformance/conformance.proto - COMMAND protoc --include_imports - ${CMAKE_CURRENT_SOURCE_DIR}/third_party/protobuf/conformance/conformance.proto - ${CMAKE_CURRENT_SOURCE_DIR}/third_party/protobuf/src/google/protobuf/test_messages_proto3.proto - -I${CMAKE_CURRENT_SOURCE_DIR}/third_party/protobuf/conformance - -I${CMAKE_CURRENT_SOURCE_DIR}/third_party/protobuf/src - -o${CMAKE_CURRENT_BINARY_DIR}/conformance.pb && - tools/upbc ${CMAKE_CURRENT_BINARY_DIR}/conformance.pb - ) - - add_custom_command( - OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/google/protobuf/descriptor.upb.h - ${CMAKE_CURRENT_BINARY_DIR}/google/protobuf/descriptor.upb.c - DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/tools/upbc - ${CMAKE_CURRENT_SOURCE_DIR}/google/protobuf/descriptor.proto - COMMAND protoc - ${CMAKE_CURRENT_SOURCE_DIR}/google/protobuf/descriptor.proto - -I${CMAKE_CURRENT_SOURCE_DIR} - -o${CMAKE_CURRENT_BINARY_DIR}/descriptor.pb && - tools/upbc ${CMAKE_CURRENT_BINARY_DIR}/descriptor.pb - ) - - add_custom_target( - genfiles ALL - DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/conformance.upb.h - ${CMAKE_CURRENT_BINARY_DIR}/google/protobuf/descriptor.upb.h - ) - - add_executable(conformance_upb - tests/conformance_upb.c - ${CMAKE_CURRENT_BINARY_DIR}/conformance.upb.c - ${CMAKE_CURRENT_BINARY_DIR}/google/protobuf/test_messages_proto3.upb.c - ${CMAKE_CURRENT_BINARY_DIR}/google/protobuf/any.upb.c - ${CMAKE_CURRENT_BINARY_DIR}/google/protobuf/duration.upb.c - ${CMAKE_CURRENT_BINARY_DIR}/google/protobuf/field_mask.upb.c - ${CMAKE_CURRENT_BINARY_DIR}/google/protobuf/struct.upb.c - ${CMAKE_CURRENT_BINARY_DIR}/google/protobuf/timestamp.upb.c - ${CMAKE_CURRENT_BINARY_DIR}/google/protobuf/wrappers.upb.c - ) - target_link_libraries(conformance_upb LINK_PRIVATE - upb - ) - - add_custom_target( - conformance - DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/conformance_upb - ${CMAKE_CURRENT_BINARY_DIR}/third_party/protobuf/cmake/conformance_test_runner - COMMAND ${CMAKE_CURRENT_BINARY_DIR}/third_party/protobuf/cmake/conformance_test_runner ${CMAKE_CURRENT_BINARY_DIR}/conformance_upb - ) - -endif() - -if (RAGEL) - add_custom_command( - OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/upb/json/parser.c - DEPENDS upb/json/parser.rl - COMMAND cd ${CMAKE_CURRENT_SOURCE_DIR} && RAGEL -C -o ${CMAKE_CURRENT_BINARY_DIR}/upb/json/parser.c upb/json/parser.rl - ) -endif() - -configure_file(tools/copy_genfiles.sh.in tools/copy_genfiles.sh) - -set(UPB_SRCS - upb/decode.c - upb/encode.c - upb/msg.c - upb/table.c - upb/upb.c -) - -set (UPBDEF_SRCS - upb/def.c - upb/msgfactory.c -) - -set(UPBHANDLERS_SRCS - upb/sink.c - upb/handlers.c -) - -set(UPBPB_SRCS -======= enable_testing() add_library(upb @@ -290,15 +87,7 @@ add_library(upb upb/refcounted.h upb/sink.h upb/upb.h) -add_library(upb_descriptor - upb/descriptor/descriptor.upbdefs.c - upb/descriptor/reader.c - upb/descriptor/descriptor.upbdefs.h - upb/descriptor/reader.h) -target_link_libraries(upb_descriptor - upb) add_library(upb_pb ->>>>>>> cmake-mac-fixes upb/pb/compile_decoder.c upb/pb/decoder.c upb/pb/decoder.int.h @@ -308,11 +97,9 @@ add_library(upb_pb upb/pb/varint.int.h upb/pb/decoder.h upb/pb/encoder.h - upb/pb/glue.h upb/pb/textprinter.h) target_link_libraries(upb_pb - upb - upb_descriptor) + upb) add_library(upb_json upb/json/parser.c upb/json/printer.c @@ -369,7 +156,6 @@ add_executable(test_cpp add_test(NAME test_cpp COMMAND test_cpp) target_link_libraries(test_cpp upb - upb_descriptor upb_pb upb_test) add_executable(test_table diff --git a/upb/bindings/lua/def.c b/upb/bindings/lua/def.c index 7b3f612..1fde212 100644 --- a/upb/bindings/lua/def.c +++ b/upb/bindings/lua/def.c @@ -22,22 +22,6 @@ } while (0) -const char *lupb_checkname(lua_State *L, int narg) { - size_t len; - const char *name = luaL_checklstring(L, narg, &len); - if (strlen(name) != len) - luaL_error(L, "names cannot have embedded NULLs"); - return name; -} - -upb_fieldtype_t lupb_checkfieldtype(lua_State *L, int narg) { - int type = luaL_checkint(L, narg); - if (!upb_fielddef_checktype(type)) - luaL_argerror(L, narg, "invalid field type"); - return type; -} - - /* lupb_refcounted ************************************************************/ /* All upb objects that use upb_refcounted have a userdata that begins with a @@ -49,12 +33,12 @@ upb_fieldtype_t lupb_checkfieldtype(lua_State *L, int narg) { * long as it begins with a pointer. */ /* Checks type; if it matches, pulls the pointer out of the wrapper. */ -void *lupb_refcounted_check(lua_State *L, int narg, const char *type) { +void *lupb_checkwrapper(lua_State *L, int narg, const char *type) { void *ud = lua_touserdata(L, narg); void *ret; if (!ud) { - luaL_typerror(L, narg, "refcounted"); + luaL_typerror(L, narg, "upb wrapper"); } memcpy(&ret, ud, sizeof(ret)); @@ -66,15 +50,12 @@ void *lupb_refcounted_check(lua_State *L, int narg, const char *type) { return ret; } -bool lupb_refcounted_pushwrapper(lua_State *L, const upb_refcounted *obj, - const char *type, const void *ref_donor, - size_t size) { - bool create; +void lupb_pushwrapper(lua_State *L, const void *obj, const char *type) { void *ud; if (obj == NULL) { lua_pushnil(L); - return false; + return; } /* Lookup our cache in the registry (we don't put our objects in the registry @@ -85,34 +66,11 @@ bool lupb_refcounted_pushwrapper(lua_State *L, const upb_refcounted *obj, lua_rawget(L, -2); /* Stack is now: objcache, cached value. */ - create = false; - if (lua_isnil(L, -1)) { - create = true; - } else { - void *ud = lua_touserdata(L, -1); - void *ud_obj; - lupb_assert(L, ud); - memcpy(&ud_obj, ud, sizeof(void*)); - - /* A corner case: it is possible for the value to be GC'd - * already, in which case we should evict this entry and create - * a new one. */ - if (ud_obj == NULL) { - create = true; - } - } - - ud = NULL; - - if (create) { /* Remove bad cached value and push new value. */ lua_pop(L, 1); - - /* All of our userdata begin with a pointer to the obj. */ - ud = lua_newuserdata(L, size); - memcpy(ud, &obj, sizeof(void*)); - upb_refcounted_donateref(obj, ref_donor, ud); + ud = lua_newuserdata(L, sizeof(*ud)); + memcpy(ud, &obj, sizeof(*ud)); luaL_getmetatable(L, type); /* Should have been created by luaopen_upb. */ @@ -123,207 +81,36 @@ bool lupb_refcounted_pushwrapper(lua_State *L, const upb_refcounted *obj, lua_pushlightuserdata(L, (void*)obj); lua_pushvalue(L, -2); lua_rawset(L, -4); - } else { - /* Existing wrapper obj already has a ref. */ - ud = lua_touserdata(L, -1); - upb_refcounted_checkref(obj, ud); - if (ref_donor) - upb_refcounted_unref(obj, ref_donor); } lua_insert(L, -2); lua_pop(L, 1); - return create; -} - -void lupb_refcounted_pushnewrapper(lua_State *L, const upb_refcounted *obj, - const char *type, const void *ref_donor) { - bool created = - lupb_refcounted_pushwrapper(L, obj, type, ref_donor, sizeof(void *)); - UPB_ASSERT(created == true); -} - -int lupb_refcounted_gc(lua_State *L) { - void *ud = lua_touserdata(L, 1); - void *nullp; - upb_refcounted *obj; - memcpy(&obj, ud, sizeof(obj)); - upb_refcounted_unref(obj, ud); - - /* Zero out pointer so we can detect a call into a GC'd object. */ - nullp = NULL; - memcpy(ud, &nullp, sizeof(nullp)); - - return 0; -} - - -/* lupb_def *******************************************************************/ - -static const upb_def *lupb_def_check(lua_State *L, int narg) { - upb_def *ret; - void *ud, *ud2; - - ud = lua_touserdata(L, narg); - if (!ud) { - luaL_typerror(L, narg, "upb def"); - } - - memcpy(&ret, ud, sizeof(ret)); - if (!ret) { - luaL_error(L, "called into dead object"); - } - - ud2 = luaL_testudata(L, narg, LUPB_MSGDEF); - if (!ud2) ud2 = luaL_testudata(L, narg, LUPB_ENUMDEF); - if (!ud2) ud2 = luaL_testudata(L, narg, LUPB_FIELDDEF); - if (!ud2) luaL_typerror(L, narg, "upb def"); - - return ret; -} - -static upb_def *lupb_def_checkmutable(lua_State *L, int narg) { - const upb_def *def = lupb_def_check(L, narg); - if (upb_def_isfrozen(def)) - luaL_error(L, "not allowed on frozen value"); - return (upb_def*)def; -} - -bool lupb_def_pushwrapper(lua_State *L, const upb_def *def, - const void *ref_donor) { - const char *type = NULL; - bool created; - - if (def == NULL) { - lua_pushnil(L); - return false; - } - - switch (upb_def_type(def)) { - case UPB_DEF_MSG: - type = LUPB_MSGDEF; - break; - case UPB_DEF_FIELD: - type = LUPB_FIELDDEF; - break; - case UPB_DEF_ENUM: - type = LUPB_ENUMDEF; - break; - default: - printf("Def type: %d\n", (int)upb_def_type(def)); - UPB_UNREACHABLE(); - } - - created = lupb_refcounted_pushwrapper(L, upb_def_upcast(def), type, ref_donor, - sizeof(void *)); - return created; -} - -void lupb_def_pushnewrapper(lua_State *L, const upb_def *def, - const void *ref_donor) { - bool created = lupb_def_pushwrapper(L, def, ref_donor); - UPB_ASSERT(created == true); -} - -static int lupb_def_type(lua_State *L) { - const upb_def *def = lupb_def_check(L, 1); - lua_pushinteger(L, upb_def_type(def)); - return 1; -} - -void lupb_filedef_pushwrapper(lua_State *L, const upb_filedef *f, - const void *ref_donor); - -static int lupb_def_file(lua_State *L) { - const upb_def *def = lupb_def_check(L, 1); - lupb_filedef_pushwrapper(L, upb_def_file(def), NULL); - return 1; -} - -static int lupb_def_freeze(lua_State *L) { - upb_def *def = lupb_def_checkmutable(L, 1); - CHK(upb_def_freeze(&def, 1, &status)); - return 0; -} - -static int lupb_def_isfrozen(lua_State *L) { - const upb_def *def = lupb_def_check(L, 1); - lua_pushboolean(L, upb_def_isfrozen(def)); - return 1; -} - -static int lupb_def_fullname(lua_State *L) { - const upb_def *def = lupb_def_check(L, 1); - lua_pushstring(L, upb_def_fullname(def)); - return 1; } -static int lupb_def_name(lua_State *L) { - const upb_def *def = lupb_def_check(L, 1); - lua_pushstring(L, upb_def_name(def)); - return 1; -} - -static int lupb_def_setfullname(lua_State *L) { - const char *name = lupb_checkname(L, 2); - CHK(upb_def_setfullname(lupb_def_checkmutable(L, 1), name, &status)); - return 0; -} - -#define LUPB_COMMON_DEF_METHODS \ - {"def_type", lupb_def_type}, \ - {"file", lupb_def_file}, \ - {"full_name", lupb_def_fullname}, \ - {"freeze", lupb_def_freeze}, \ - {"is_frozen", lupb_def_isfrozen}, \ - {"name", lupb_def_name}, \ - {"set_full_name", lupb_def_setfullname}, \ +void lupb_msgdef_pushwrapper(lua_State *L, const upb_msgdef *m); +void lupb_oneofdef_pushwrapper(lua_State *L, const upb_oneofdef *o); +static void lupb_enumdef_pushwrapper(lua_State *L, const upb_enumdef *e); /* lupb_fielddef **************************************************************/ -void lupb_fielddef_pushwrapper(lua_State *L, const upb_fielddef *f, - const void *ref_donor) { - lupb_def_pushwrapper(L, upb_fielddef_upcast(f), ref_donor); +void lupb_fielddef_pushwrapper(lua_State *L, const upb_fielddef *f) { + lupb_pushwrapper(L, f, LUPB_FIELDDEF); } const upb_fielddef *lupb_fielddef_check(lua_State *L, int narg) { - return lupb_refcounted_check(L, narg, LUPB_FIELDDEF); + return lupb_checkwrapper(L, narg, LUPB_FIELDDEF); } -static upb_fielddef *lupb_fielddef_checkmutable(lua_State *L, int narg) { - const upb_fielddef *f = lupb_fielddef_check(L, narg); - if (upb_fielddef_isfrozen(f)) - luaL_error(L, "not allowed on frozen value"); - return (upb_fielddef*)f; -} - -static int lupb_fielddef_new(lua_State *L) { - upb_fielddef *f = upb_fielddef_new(&f); - lupb_def_pushnewrapper(L, upb_fielddef_upcast(f), &f); - return 1; -} - -/* Getters */ - -void lupb_oneofdef_pushwrapper(lua_State *L, const upb_oneofdef *o, - const void *ref_donor); - static int lupb_fielddef_containingoneof(lua_State *L) { const upb_fielddef *f = lupb_fielddef_check(L, 1); - lupb_oneofdef_pushwrapper(L, upb_fielddef_containingoneof(f), NULL); + lupb_oneofdef_pushwrapper(L, upb_fielddef_containingoneof(f)); return 1; } static int lupb_fielddef_containingtype(lua_State *L) { const upb_fielddef *f = lupb_fielddef_check(L, 1); - lupb_msgdef_pushwrapper(L, upb_fielddef_containingtype(f), NULL); - return 1; -} - -static int lupb_fielddef_containingtypename(lua_State *L) { - upb_fielddef *f = lupb_fielddef_checkmutable(L, 1); - lua_pushstring(L, upb_fielddef_containingtypename(f)); + lupb_msgdef_pushwrapper(L, upb_fielddef_containingtype(f)); return 1; } @@ -331,7 +118,7 @@ static int lupb_fielddef_default(lua_State *L) { const upb_fielddef *f = lupb_fielddef_check(L, 1); switch (upb_fielddef_type(f)) { case UPB_TYPE_INT32: - int32: + case UPB_TYPE_ENUM: lupb_pushint32(L, upb_fielddef_defaultint32(f)); break; case UPB_TYPE_INT64: lupb_pushint64(L, upb_fielddef_defaultint64(f)); break; @@ -345,18 +132,8 @@ static int lupb_fielddef_default(lua_State *L) { lua_pushnumber(L, upb_fielddef_defaultfloat(f)); break; case UPB_TYPE_BOOL: lua_pushboolean(L, upb_fielddef_defaultbool(f)); break; - case UPB_TYPE_ENUM: - if (upb_fielddef_enumhasdefaultstr(f)) { - goto str; - } else if (upb_fielddef_enumhasdefaultint32(f)) { - goto int32; - } else { - lua_pushnil(L); - } - break; case UPB_TYPE_STRING: - case UPB_TYPE_BYTES: - str: { + case UPB_TYPE_BYTES: { size_t len; const char *data = upb_fielddef_defaultstr(f, &len); lua_pushlstring(L, data, len); @@ -397,24 +174,12 @@ static int lupb_fielddef_index(lua_State *L) { return 1; } -static int lupb_fielddef_intfmt(lua_State *L) { - const upb_fielddef *f = lupb_fielddef_check(L, 1); - lua_pushinteger(L, upb_fielddef_intfmt(f)); - return 1; -} - static int lupb_fielddef_isextension(lua_State *L) { const upb_fielddef *f = lupb_fielddef_check(L, 1); lua_pushboolean(L, upb_fielddef_isextension(f)); return 1; } -static int lupb_fielddef_istagdelim(lua_State *L) { - const upb_fielddef *f = lupb_fielddef_check(L, 1); - lua_pushboolean(L, upb_fielddef_istagdelim(f)); - return 1; -} - static int lupb_fielddef_label(lua_State *L) { const upb_fielddef *f = lupb_fielddef_check(L, 1); lua_pushinteger(L, upb_fielddef_label(f)); @@ -449,22 +214,15 @@ static int lupb_fielddef_packed(lua_State *L) { return 1; } -static int lupb_fielddef_subdef(lua_State *L) { +static int lupb_fielddef_msgsubdef(lua_State *L) { const upb_fielddef *f = lupb_fielddef_check(L, 1); - const upb_def *def; - - if (!upb_fielddef_hassubdef(f)) - luaL_error(L, "Tried to get subdef of non-message field"); - def = upb_fielddef_subdef(f); - lupb_def_pushwrapper(L, def, NULL); + lupb_msgdef_pushwrapper(L, upb_fielddef_msgsubdef(f)); return 1; } -static int lupb_fielddef_subdefname(lua_State *L) { - upb_fielddef *f = lupb_fielddef_checkmutable(L, 1); - if (!upb_fielddef_hassubdef(f)) - luaL_error(L, "Tried to get subdef name of non-message field"); - lua_pushstring(L, upb_fielddef_subdefname(f)); +static int lupb_fielddef_enumsubdef(lua_State *L) { + const upb_fielddef *f = lupb_fielddef_check(L, 1); + lupb_enumdef_pushwrapper(L, upb_fielddef_enumsubdef(f)); return 1; } @@ -477,216 +235,40 @@ static int lupb_fielddef_type(lua_State *L) { return 1; } -/* Setters */ - -static int lupb_fielddef_setcontainingtypename(lua_State *L) { - upb_fielddef *f = lupb_fielddef_checkmutable(L, 1); - const char *name = NULL; - if (!lua_isnil(L, 2)) - name = lupb_checkname(L, 2); - CHK(upb_fielddef_setcontainingtypename(f, name, &status)); - return 0; -} - -static int lupb_fielddef_setdefault(lua_State *L) { - upb_fielddef *f = lupb_fielddef_checkmutable(L, 1); - - switch (upb_fielddef_type(f)) { - case UPB_TYPE_INT32: - upb_fielddef_setdefaultint32(f, lupb_checkint32(L, 2)); - break; - case UPB_TYPE_INT64: - upb_fielddef_setdefaultint64(f, lupb_checkint64(L, 2)); - break; - case UPB_TYPE_UINT32: - upb_fielddef_setdefaultuint32(f, lupb_checkuint32(L, 2)); - break; - case UPB_TYPE_UINT64: - upb_fielddef_setdefaultuint64(f, lupb_checkuint64(L, 2)); - break; - case UPB_TYPE_DOUBLE: - upb_fielddef_setdefaultdouble(f, lupb_checkdouble(L, 2)); - break; - case UPB_TYPE_FLOAT: - upb_fielddef_setdefaultfloat(f, lupb_checkfloat(L, 2)); - break; - case UPB_TYPE_BOOL: - upb_fielddef_setdefaultbool(f, lupb_checkbool(L, 2)); - break; - case UPB_TYPE_MESSAGE: - return luaL_error(L, "Message types cannot have defaults."); - case UPB_TYPE_ENUM: - if (lua_type(L, 2) != LUA_TSTRING) { - upb_fielddef_setdefaultint32(f, lupb_checkint32(L, 2)); - break; - } - /* Else fall through and set string default. */ - case UPB_TYPE_BYTES: - case UPB_TYPE_STRING: { - size_t len; - const char *str = lua_tolstring(L, 2, &len); - CHK(upb_fielddef_setdefaultstr(f, str, len, &status)); - } - } - return 0; -} - -static int lupb_fielddef_setisextension(lua_State *L) { - upb_fielddef *f = lupb_fielddef_checkmutable(L, 1); - CHK(upb_fielddef_setisextension(f, lupb_checkbool(L, 2))); - return 0; -} - -static int lupb_fielddef_setlabel(lua_State *L) { - upb_fielddef *f = lupb_fielddef_checkmutable(L, 1); - int label = luaL_checkint(L, 2); - if (!upb_fielddef_checklabel(label)) - luaL_argerror(L, 2, "invalid field label"); - upb_fielddef_setlabel(f, label); - return 0; -} - -static int lupb_fielddef_setlazy(lua_State *L) { - upb_fielddef *f = lupb_fielddef_checkmutable(L, 1); - upb_fielddef_setlazy(f, lupb_checkbool(L, 2)); - return 0; -} - -static int lupb_fielddef_setname(lua_State *L) { - upb_fielddef *f = lupb_fielddef_checkmutable(L, 1); - CHK(upb_fielddef_setname(f, lupb_checkname(L, 2), &status)); - return 0; -} - -static int lupb_fielddef_setnumber(lua_State *L) { - upb_fielddef *f = lupb_fielddef_checkmutable(L, 1); - CHK(upb_fielddef_setnumber(f, luaL_checkint(L, 2), &status)); - return 0; -} - -static int lupb_fielddef_setpacked(lua_State *L) { - upb_fielddef *f = lupb_fielddef_checkmutable(L, 1); - upb_fielddef_setpacked(f, lupb_checkbool(L, 2)); - return 0; -} - -static int lupb_fielddef_setsubdef(lua_State *L) { - upb_fielddef *f = lupb_fielddef_checkmutable(L, 1); - const upb_def *def = NULL; - if (!lua_isnil(L, 2)) - def = lupb_def_check(L, 2); - CHK(upb_fielddef_setsubdef(f, def, &status)); - return 0; -} - -static int lupb_fielddef_setsubdefname(lua_State *L) { - upb_fielddef *f = lupb_fielddef_checkmutable(L, 1); - const char *name = NULL; - if (!lua_isnil(L, 2)) - name = lupb_checkname(L, 2); - CHK(upb_fielddef_setsubdefname(f, name, &status)); - return 0; -} - -static int lupb_fielddef_settype(lua_State *L) { - upb_fielddef *f = lupb_fielddef_checkmutable(L, 1); - upb_fielddef_settype(f, lupb_checkfieldtype(L, 2)); - return 0; -} - -static int lupb_fielddef_setintfmt(lua_State *L) { - upb_fielddef *f = lupb_fielddef_checkmutable(L, 1); - int32_t intfmt = luaL_checknumber(L, 2); - if (!upb_fielddef_checkintfmt(intfmt)) - luaL_argerror(L, 2, "invalid intfmt"); - upb_fielddef_setintfmt(f, intfmt); - return 0; -} - -static int lupb_fielddef_settagdelim(lua_State *L) { - upb_fielddef *f = lupb_fielddef_checkmutable(L, 1); - bool is_tag_delim = lupb_checkbool(L, 2); - CHK(upb_fielddef_settagdelim(f, is_tag_delim)); - return 0; -} - -static const struct luaL_Reg lupb_fielddef_mm[] = { - {"__gc", lupb_refcounted_gc}, - {NULL, NULL} -}; - static const struct luaL_Reg lupb_fielddef_m[] = { - LUPB_COMMON_DEF_METHODS - {"containing_oneof", lupb_fielddef_containingoneof}, {"containing_type", lupb_fielddef_containingtype}, - {"containing_type_name", lupb_fielddef_containingtypename}, {"default", lupb_fielddef_default}, {"descriptor_type", lupb_fielddef_descriptortype}, {"getsel", lupb_fielddef_getsel}, {"has_subdef", lupb_fielddef_hassubdef}, {"index", lupb_fielddef_index}, - {"intfmt", lupb_fielddef_intfmt}, {"is_extension", lupb_fielddef_isextension}, - {"istagdelim", lupb_fielddef_istagdelim}, {"label", lupb_fielddef_label}, {"lazy", lupb_fielddef_lazy}, {"name", lupb_fielddef_name}, {"number", lupb_fielddef_number}, {"packed", lupb_fielddef_packed}, - {"subdef", lupb_fielddef_subdef}, - {"subdef_name", lupb_fielddef_subdefname}, + {"msgsubdef", lupb_fielddef_msgsubdef}, + {"enumsubdef", lupb_fielddef_enumsubdef}, {"type", lupb_fielddef_type}, - - {"set_containing_type_name", lupb_fielddef_setcontainingtypename}, - {"set_default", lupb_fielddef_setdefault}, - {"set_is_extension", lupb_fielddef_setisextension}, - {"set_label", lupb_fielddef_setlabel}, - {"set_lazy", lupb_fielddef_setlazy}, - {"set_name", lupb_fielddef_setname}, - {"set_number", lupb_fielddef_setnumber}, - {"set_packed", lupb_fielddef_setpacked}, - {"set_subdef", lupb_fielddef_setsubdef}, - {"set_subdef_name", lupb_fielddef_setsubdefname}, - {"set_type", lupb_fielddef_settype}, - {"set_intfmt", lupb_fielddef_setintfmt}, - {"set_tagdelim", lupb_fielddef_settagdelim}, - {NULL, NULL} }; /* lupb_oneofdef **************************************************************/ -void lupb_oneofdef_pushwrapper(lua_State *L, const upb_oneofdef *o, - const void *ref_donor) { - lupb_refcounted_pushwrapper(L, upb_oneofdef_upcast(o), LUPB_ONEOFDEF, - ref_donor, sizeof(void *)); +void lupb_oneofdef_pushwrapper(lua_State *L, const upb_oneofdef *o) { + lupb_pushwrapper(L, o, LUPB_ONEOFDEF); } const upb_oneofdef *lupb_oneofdef_check(lua_State *L, int narg) { return lupb_refcounted_check(L, narg, LUPB_ONEOFDEF); } -static upb_oneofdef *lupb_oneofdef_checkmutable(lua_State *L, int narg) { - const upb_oneofdef *o = lupb_oneofdef_check(L, narg); - if (upb_oneofdef_isfrozen(o)) - luaL_error(L, "not allowed on frozen value"); - return (upb_oneofdef*)o; -} - -static int lupb_oneofdef_new(lua_State *L) { - upb_oneofdef *o = upb_oneofdef_new(&o); - lupb_refcounted_pushnewrapper(L, upb_oneofdef_upcast(o), LUPB_ONEOFDEF, &o); - return 1; -} - -/* Getters */ - static int lupb_oneofdef_containingtype(lua_State *L) { const upb_oneofdef *o = lupb_oneofdef_check(L, 1); - lupb_def_pushwrapper(L, upb_msgdef_upcast(upb_oneofdef_containingtype(o)), - NULL); + lupb_msgdef_pushwrapper(L, upb_oneofdef_containingtype(o)); return 1; } @@ -704,14 +286,14 @@ static int lupb_oneofdef_field(lua_State *L) { return luaL_argerror(L, 2, msg); } - lupb_def_pushwrapper(L, upb_fielddef_upcast(f), NULL); + lupb_fielddef_pushwrapper(L, f); return 1; } static int lupb_oneofiter_next(lua_State *L) { upb_oneof_iter *i = lua_touserdata(L, lua_upvalueindex(1)); if (upb_oneof_done(i)) return 0; - lupb_fielddef_pushwrapper(L, upb_oneof_iter_field(i), NULL); + lupb_fielddef_pushwrapper(L, upb_oneof_iter_field(i)); upb_oneof_next(i); return 1; } @@ -738,35 +320,15 @@ static int lupb_oneofdef_name(lua_State *L) { return 1; } -/* Setters */ - -static int lupb_oneofdef_add(lua_State *L) { - upb_oneofdef *o = lupb_oneofdef_checkmutable(L, 1); - upb_fielddef *f = lupb_fielddef_checkmutable(L, 2); - CHK(upb_oneofdef_addfield(o, f, NULL, &status)); - return 0; -} - -static int lupb_oneofdef_setname(lua_State *L) { - upb_oneofdef *o = lupb_oneofdef_checkmutable(L, 1); - CHK(upb_oneofdef_setname(o, lupb_checkname(L, 2), &status)); - return 0; -} - static const struct luaL_Reg lupb_oneofdef_m[] = { {"containing_type", lupb_oneofdef_containingtype}, {"field", lupb_oneofdef_field}, {"fields", lupb_oneofdef_fields}, {"name", lupb_oneofdef_name}, - - {"add", lupb_oneofdef_add}, - {"set_name", lupb_oneofdef_setname}, - {NULL, NULL} }; static const struct luaL_Reg lupb_oneofdef_mm[] = { - {"__gc", lupb_refcounted_gc}, {"__len", lupb_oneofdef_len}, {NULL, NULL} }; @@ -778,42 +340,14 @@ typedef struct { const upb_msgdef *md; } lupb_msgdef; -void lupb_msgdef_pushwrapper(lua_State *L, const upb_msgdef *m, - const void *ref_donor) { - lupb_def_pushwrapper(L, upb_msgdef_upcast(m), ref_donor); +void lupb_msgdef_pushwrapper(lua_State *L, const upb_msgdef *m) { + lupb_pushwrapper(L, m, LUPB_MSGDEF); } const upb_msgdef *lupb_msgdef_check(lua_State *L, int narg) { return lupb_refcounted_check(L, narg, LUPB_MSGDEF); } -static upb_msgdef *lupb_msgdef_checkmutable(lua_State *L, int narg) { - const upb_msgdef *m = lupb_msgdef_check(L, narg); - if (upb_msgdef_isfrozen(m)) - luaL_error(L, "not allowed on frozen value"); - return (upb_msgdef*)m; -} - -static int lupb_msgdef_new(lua_State *L) { - upb_msgdef *md = upb_msgdef_new(&md); - lupb_def_pushnewrapper(L, upb_msgdef_upcast(md), &md); - return 1; -} - -static int lupb_msgdef_add(lua_State *L) { - upb_msgdef *m = lupb_msgdef_checkmutable(L, 1); - - /* Both oneofs and fields can be added. */ - if (luaL_testudata(L, 2, LUPB_FIELDDEF)) { - upb_fielddef *f = lupb_fielddef_checkmutable(L, 2); - CHK(upb_msgdef_addfield(m, f, NULL, &status)); - } else if (luaL_testudata(L, 2, LUPB_ONEOFDEF)) { - upb_oneofdef *o = lupb_oneofdef_checkmutable(L, 2); - CHK(upb_msgdef_addoneof(m, o, NULL, &status)); - } - return 0; -} - static int lupb_msgdef_len(lua_State *L) { const upb_msgdef *m = lupb_msgdef_check(L, 1); lua_pushinteger(L, upb_msgdef_numfields(m)); @@ -834,7 +368,7 @@ static int lupb_msgdef_field(lua_State *L) { return luaL_argerror(L, 2, msg); } - lupb_def_pushwrapper(L, upb_fielddef_upcast(f), NULL); + lupb_fielddef_pushwrapper(L, f); return 1; } @@ -845,9 +379,9 @@ static int lupb_msgdef_lookupname(lua_State *L) { if (!upb_msgdef_lookupnamez(m, lua_tostring(L, 2), &f, &o)) { lua_pushnil(L); } else if (o) { - lupb_oneofdef_pushwrapper(L, o, NULL); + lupb_oneofdef_pushwrapper(L, o); } else { - lupb_fielddef_pushwrapper(L, f, NULL); + lupb_fielddef_pushwrapper(L, f); } return 1; } @@ -855,7 +389,7 @@ static int lupb_msgdef_lookupname(lua_State *L) { static int lupb_msgfielditer_next(lua_State *L) { upb_msg_field_iter *i = lua_touserdata(L, lua_upvalueindex(1)); if (upb_msg_field_done(i)) return 0; - lupb_def_pushwrapper(L, upb_fielddef_upcast(upb_msg_iter_field(i)), NULL); + lupb_fielddef_pushwrapper(L, upb_msg_iter_field(i)); upb_msg_field_next(i); return 1; } @@ -873,7 +407,7 @@ static int lupb_msgdef_fields(lua_State *L) { static int lupb_msgoneofiter_next(lua_State *L) { upb_msg_oneof_iter *i = lua_touserdata(L, lua_upvalueindex(1)); if (upb_msg_oneof_done(i)) return 0; - lupb_oneofdef_pushwrapper(L, upb_msg_iter_oneof(i), NULL); + lupb_oneofdef_pushwrapper(L, upb_msg_iter_oneof(i)); upb_msg_oneof_next(i); return 1; } @@ -902,20 +436,16 @@ static int lupb_msgdef_syntax(lua_State *L) { static const struct luaL_Reg lupb_msgdef_mm[] = { {"__len", lupb_msgdef_len}, - {"__gc", lupb_refcounted_gc}, {NULL, NULL} }; static const struct luaL_Reg lupb_msgdef_m[] = { - LUPB_COMMON_DEF_METHODS - {"add", lupb_msgdef_add}, {"field", lupb_msgdef_field}, {"fields", lupb_msgdef_fields}, {"lookup_name", lupb_msgdef_lookupname}, {"oneofs", lupb_msgdef_oneofs}, {"syntax", lupb_msgdef_syntax}, {"_map_entry", lupb_msgdef_mapentry}, - {NULL, NULL} }; @@ -926,25 +456,8 @@ const upb_enumdef *lupb_enumdef_check(lua_State *L, int narg) { return lupb_refcounted_check(L, narg, LUPB_ENUMDEF); } -static upb_enumdef *lupb_enumdef_checkmutable(lua_State *L, int narg) { - const upb_enumdef *f = lupb_enumdef_check(L, narg); - if (upb_enumdef_isfrozen(f)) - luaL_error(L, "not allowed on frozen value"); - return (upb_enumdef*)f; -} - -static int lupb_enumdef_new(lua_State *L) { - upb_enumdef *e = upb_enumdef_new(&e); - lupb_def_pushnewrapper(L, upb_enumdef_upcast(e), &e); - return 1; -} - -static int lupb_enumdef_add(lua_State *L) { - upb_enumdef *e = lupb_enumdef_checkmutable(L, 1); - const char *name = lupb_checkname(L, 2); - int32_t val = lupb_checkint32(L, 3); - CHK(upb_enumdef_addval(e, name, val, &status)); - return 0; +static void lupb_enumdef_pushwrapper(lua_State *L, const upb_enumdef *e) { + lupb_pushwrapper(L, e, LUPB_ENUMDEF); } static int lupb_enumdef_len(lua_State *L) { @@ -996,14 +509,11 @@ static int lupb_enumdef_values(lua_State *L) { } static const struct luaL_Reg lupb_enumdef_mm[] = { - {"__gc", lupb_refcounted_gc}, {"__len", lupb_enumdef_len}, {NULL, NULL} }; static const struct luaL_Reg lupb_enumdef_m[] = { - LUPB_COMMON_DEF_METHODS - {"add", lupb_enumdef_add}, {"value", lupb_enumdef_value}, {"values", lupb_enumdef_values}, {NULL, NULL} @@ -1012,68 +522,50 @@ static const struct luaL_Reg lupb_enumdef_m[] = { /* lupb_filedef ***************************************************************/ -void lupb_filedef_pushwrapper(lua_State *L, const upb_filedef *f, - const void *ref_donor) { - lupb_refcounted_pushwrapper(L, upb_filedef_upcast(f), LUPB_FILEDEF, ref_donor, - sizeof(void *)); -} - -void lupb_filedef_pushnewrapper(lua_State *L, const upb_filedef *f, - const void *ref_donor) { - lupb_refcounted_pushnewrapper(L, upb_filedef_upcast(f), LUPB_FILEDEF, - ref_donor); +void lupb_filedef_pushwrapper(lua_State *L, const upb_filedef *f) { + lupb_pushwrapper(L, f, LUPB_FILEDEF); } const upb_filedef *lupb_filedef_check(lua_State *L, int narg) { return lupb_refcounted_check(L, narg, LUPB_FILEDEF); } -static upb_filedef *lupb_filedef_checkmutable(lua_State *L, int narg) { - const upb_filedef *f = lupb_filedef_check(L, narg); - if (upb_filedef_isfrozen(f)) - luaL_error(L, "not allowed on frozen value"); - return (upb_filedef*)f; +static int lupb_filedef_dep(lua_State *L) { + const upb_filedef *f = lupb_filedef_check(L, 1); + int index = luaL_checkint(L, 2); + lupb_filedef_pushwrapper(L, upb_filedef_dep(f, index)); + return 1; } -static int lupb_filedef_new(lua_State *L) { - upb_filedef *f = upb_filedef_new(&f); - lupb_filedef_pushnewrapper(L, f, &f); +static int lupb_filedef_depcount(lua_State *L) { + const upb_filedef *f = lupb_filedef_check(L, 1); + lua_pushnumber(L, upb_filedef_depcount(f)); return 1; } -static int lupb_filedef_def(lua_State *L) { +static int lupb_filedef_enum(lua_State *L) { const upb_filedef *f = lupb_filedef_check(L, 1); int index = luaL_checkint(L, 2); - const upb_def *def = upb_filedef_def(f, index); - - if (!def) { - return luaL_error(L, "index out of range"); - } - - lupb_def_pushwrapper(L, def, NULL); + lupb_enumdef_pushwrapper(L, upb_filedef_enum(f, index)); return 1; } -static int lupb_filedefdepiter_next(lua_State *L) { - const upb_filedef *f = lupb_filedef_check(L, lua_upvalueindex(1)); - size_t i = lua_tointeger(L, lua_upvalueindex(2)); - - if (i >= upb_filedef_depcount(f)) { - return 0; - } - - lupb_filedef_pushwrapper(L, upb_filedef_dep(f, i), NULL); - lua_pushinteger(L, i + 1); - lua_replace(L, lua_upvalueindex(2)); +static int lupb_filedef_enumcount(lua_State *L) { + const upb_filedef *f = lupb_filedef_check(L, 1); + lua_pushnumber(L, upb_filedef_enumcount(f)); return 1; } +static int lupb_filedef_msg(lua_State *L) { + const upb_filedef *f = lupb_filedef_check(L, 1); + int index = luaL_checkint(L, 2); + lupb_msgdef_pushwrapper(L, upb_filedef_msg(f, index)); + return 1; +} -static int lupb_filedef_dependencies(lua_State *L) { - lupb_filedef_check(L, 1); - lua_pushvalue(L, 1); - lua_pushnumber(L, 0); /* Index, starts at zero. */ - lua_pushcclosure(L, &lupb_filedefdepiter_next, 2); +static int lupb_filedef_msgcount(lua_State *L) { + const upb_filedef *f = lupb_filedef_check(L, 1); + lua_pushnumber(L, upb_filedef_msgcount(f)); return 1; } @@ -1095,72 +587,16 @@ static int lupb_filedef_syntax(lua_State *L) { return 1; } -static int lupb_filedef_len(lua_State *L) { - const upb_filedef *f = lupb_filedef_check(L, 1); - lua_pushinteger(L, upb_filedef_defcount(f)); - return 1; -} - -static int lupb_filedef_setname(lua_State *L) { - upb_filedef *f = lupb_filedef_checkmutable(L, 1); - CHK(upb_filedef_setname(f, lupb_checkname(L, 2), &status)); - return 0; -} - -static int lupb_filedef_setpackage(lua_State *L) { - upb_filedef *f = lupb_filedef_checkmutable(L, 1); - CHK(upb_filedef_setpackage(f, lupb_checkname(L, 2), &status)); - return 0; -} - -static int lupb_filedefiter_next(lua_State *L) { - const upb_filedef *f = lupb_filedef_check(L, lua_upvalueindex(1)); - int type = lua_tointeger(L, lua_upvalueindex(2)); - size_t i = lua_tointeger(L, lua_upvalueindex(3)); - size_t n = upb_filedef_defcount(f); - - for (; i < n; i++) { - const upb_def *def; - - def = upb_filedef_def(f, i); - UPB_ASSERT(def); - - if (type == UPB_DEF_ANY || upb_def_type(def) == type) { - lua_pushinteger(L, i + 1); - lua_replace(L, lua_upvalueindex(3)); - lupb_def_pushwrapper(L, def, NULL); - return 1; - } - } - - return 0; -} - -static int lupb_filedef_defs(lua_State *L) { - lupb_filedef_check(L, 1); - luaL_checkinteger(L, 2); /* Def type. Could make this optional. */ - lua_pushnumber(L, 0); /* Index, starts at zero. */ - lua_pushcclosure(L, &lupb_filedefiter_next, 3); - return 1; -} - -static const struct luaL_Reg lupb_filedef_mm[] = { - {"__gc", lupb_refcounted_gc}, - {"__len", lupb_filedef_len}, - {NULL, NULL} -}; - static const struct luaL_Reg lupb_filedef_m[] = { - {"def", lupb_filedef_def}, - {"defs", lupb_filedef_defs}, - {"dependencies", lupb_filedef_dependencies}, + {"dep", lupb_filedef_dep}, + {"depcount", lupb_filedef_depcount}, + {"enum", lupb_filedef_enum}, + {"enumcount", lupb_filedef_enumcount}, + {"msg", lupb_filedef_msg}, + {"msgcount", lupb_filedef_msgcount}, {"name", lupb_filedef_name}, {"package", lupb_filedef_package}, {"syntax", lupb_filedef_syntax}, - - {"set_name", lupb_filedef_setname}, - {"set_package", lupb_filedef_setpackage}, - {NULL, NULL} }; @@ -1194,15 +630,23 @@ static int lupb_symtab_gc(lua_State *L) { return 0; } +/* TODO(haberman): perhaps this should take a message object instead of a + * serialized string once we have a good story for vending compiled-in + * messages. */ static int lupb_symtab_add(lua_State *L) { size_t len; - upb_symtab *s = lupb_symtab_check(L, 1); - const char *str = luaL_checklstring(L, 2, &len); - google_protobuf_FileDescriptorSet *set = - google_protobuf_FileDescriptorSet_parsenew(upb_stringview_make(str, len), - upb_symtab_arena(s)); + upb_arena *arena; size_t i; upb_array *file_arr; + google_protobuf_FileDescriptorSet *set; + upb_symtab *s = lupb_symtab_check(L, 1); + const char *str = luaL_checklstring(L, 2, &len); + + lupb_arena_new(L); + arena = lupb_arena_check(L, -1); + + set = google_protobuf_FileDescriptorSet_parsenew( + upb_stringview_make(str, len), arena); if (!set) { luaL_argerror(L, 2, "failed to parse descriptor"); @@ -1219,40 +663,24 @@ static int lupb_symtab_add(lua_State *L) { return 0; } -static int lupb_symtab_lookup(lua_State *L) { +static int lupb_symtab_lookupmsg(lua_State *L) { const upb_symtab *s = lupb_symtab_check(L, 1); - int i; - for (i = 2; i <= lua_gettop(L); i++) { - const upb_def *def = upb_symtab_lookup(s, luaL_checkstring(L, i)); - lupb_def_pushwrapper(L, def, NULL); - lua_replace(L, i); - } - return lua_gettop(L) - 1; -} - -static int lupb_symtabiter_next(lua_State *L) { - upb_symtab_iter *i = lua_touserdata(L, lua_upvalueindex(1)); - if (upb_symtab_done(i)) return 0; - lupb_def_pushwrapper(L, upb_symtab_iter_def(i), NULL); - upb_symtab_next(i); + const upb_msgdef *m = upb_symtab_lookupmsg(s, luaL_checkstring(L, 2)); + lupb_msgdef_pushwrapper(L, m); return 1; } -static int lupb_symtab_defs(lua_State *L) { +static int lupb_symtab_lookupenum(lua_State *L) { const upb_symtab *s = lupb_symtab_check(L, 1); - upb_deftype_t type = lua_gettop(L) > 1 ? luaL_checkint(L, 2) : UPB_DEF_ANY; - upb_symtab_iter *i = lua_newuserdata(L, sizeof(upb_symtab_iter)); - upb_symtab_begin(i, s, type); - /* Need to guarantee that the symtab outlives the iter. */ - lua_pushvalue(L, 1); - lua_pushcclosure(L, &lupb_symtabiter_next, 2); + const upb_enumdef *e = upb_symtab_lookupenum(s, luaL_checkstring(L, 2)); + lupb_enumdef_pushwrapper(L, e); return 1; } static const struct luaL_Reg lupb_symtab_m[] = { {"add", lupb_symtab_add}, - {"defs", lupb_symtab_defs}, - {"lookup", lupb_symtab_lookup}, + {"lookup_msg", lupb_symtab_lookupmsg}, + {"lookup_enum", lupb_symtab_lookupenum}, {NULL, NULL} }; @@ -1263,35 +691,13 @@ static const struct luaL_Reg lupb_symtab_mm[] = { /* lupb toplevel **************************************************************/ -static int lupb_freeze(lua_State *L) { - int n = lua_gettop(L); - int i; - /* Scratch memory; lua_newuserdata() anchors it as a GC root in case any Lua - * functions fail. */ - upb_def **defs = lua_newuserdata(L, n * sizeof(upb_def*)); - - for (i = 0; i < n; i++) { - /* Could allow an array of defs here also. */ - defs[i] = lupb_def_checkmutable(L, i + 1); - } - CHK(upb_def_freeze(defs, n, &status)); - return 0; -} - static void lupb_setfieldi(lua_State *L, const char *field, int i) { lua_pushinteger(L, i); lua_setfield(L, -2, field); } static const struct luaL_Reg lupbdef_toplevel_m[] = { - {"EnumDef", lupb_enumdef_new}, - {"FieldDef", lupb_fielddef_new}, - {"FileDef", lupb_filedef_new}, - {"MessageDef", lupb_msgdef_new}, - {"OneofDef", lupb_oneofdef_new}, {"SymbolTable", lupb_symtab_new}, - {"freeze", lupb_freeze}, - {NULL, NULL} }; @@ -1300,8 +706,8 @@ void lupb_def_registertypes(lua_State *L) { /* Refcounted types. */ lupb_register_type(L, LUPB_ENUMDEF, lupb_enumdef_m, lupb_enumdef_mm); - lupb_register_type(L, LUPB_FIELDDEF, lupb_fielddef_m, lupb_fielddef_mm); - lupb_register_type(L, LUPB_FILEDEF, lupb_filedef_m, lupb_filedef_mm); + lupb_register_type(L, LUPB_FIELDDEF, lupb_fielddef_m, NULL); + lupb_register_type(L, LUPB_FILEDEF, lupb_filedef_m, NULL); lupb_register_type(L, LUPB_MSGDEF, lupb_msgdef_m, lupb_msgdef_mm); lupb_register_type(L, LUPB_ONEOFDEF, lupb_oneofdef_m, lupb_oneofdef_mm); lupb_register_type(L, LUPB_SYMTAB, lupb_symtab_m, lupb_symtab_mm); @@ -1354,12 +760,6 @@ void lupb_def_registertypes(lua_State *L) { lupb_setfieldi(L, "DESCRIPTOR_TYPE_SINT32", UPB_DESCRIPTOR_TYPE_SINT32); lupb_setfieldi(L, "DESCRIPTOR_TYPE_SINT64", UPB_DESCRIPTOR_TYPE_SINT64); - lupb_setfieldi(L, "DEF_MSG", UPB_DEF_MSG); - lupb_setfieldi(L, "DEF_FIELD", UPB_DEF_FIELD); - lupb_setfieldi(L, "DEF_ENUM", UPB_DEF_ENUM); - lupb_setfieldi(L, "DEF_SERVICE", UPB_DEF_SERVICE); - lupb_setfieldi(L, "DEF_ANY", UPB_DEF_ANY); - lupb_setfieldi(L, "HANDLER_INT32", UPB_HANDLER_INT32); lupb_setfieldi(L, "HANDLER_INT64", UPB_HANDLER_INT64); lupb_setfieldi(L, "HANDLER_UINT32", UPB_HANDLER_UINT32); diff --git a/upb/def.c b/upb/def.c index c800319..3167222 100644 --- a/upb/def.c +++ b/upb/def.c @@ -2,8 +2,10 @@ #include "upb/def.h" #include +#include #include #include +#include "google/protobuf/descriptor.upb.h" #include "upb/handlers.h" typedef struct { @@ -29,6 +31,7 @@ struct upb_fielddef { uint64_t uint; double dbl; float flt; + bool boolean; str_t *str; } defaultval; const upb_msgdef *msgdef; @@ -48,14 +51,11 @@ struct upb_fielddef { }; struct upb_msgdef { - upb_filedef *file; + const upb_filedef *file; const char *full_name; uint32_t selector_count; uint32_t submsg_field_count; - upb_fielddef **fields; - upb_oneofdef **oneofs; - /* Tables for looking up fields by number and name. */ upb_inttable itof; upb_strtable ntof; @@ -82,11 +82,6 @@ struct upb_oneofdef { upb_inttable itof; }; -struct upb_symtab { - upb_arena arena; - upb_strtable symtab; -}; - struct upb_filedef { const char *name; const char *package; @@ -94,8 +89,37 @@ struct upb_filedef { const char *phpnamespace; upb_syntax_t syntax; - upb_inttable defs; - upb_inttable deps; + const upb_msgdef **msgs; + const upb_enumdef **enums; + const upb_filedef **deps; + + int msgcount; + int enumcount; + int depcount; +}; + +/* Inside a symtab we store tagged pointers to specific def types. */ +typedef enum { + UPB_DEFTYPE_MSG = 0, + UPB_DEFTYPE_ENUM = 1, + UPB_DEFTYPE_FIELD = 2, + UPB_DEFTYPE_ONEOF = 3 +} upb_deftype_t; + +static const void *unpack_def(upb_value v, upb_deftype_t type) { + uintptr_t num = (uintptr_t)upb_value_getconstptr(v); + return (num & 3) == type ? (const void*)(num & ~3) : NULL; +} + +static upb_value pack_def(const void *ptr, upb_deftype_t type) { + uintptr_t num = (uintptr_t)ptr | type; + return upb_value_constptr((const void*)num); +} + +struct upb_symtab { + upb_arena arena; + upb_strtable syms; /* full_name -> packed def ptr */ + upb_strtable files; /* file_name -> upb_filedef* */ }; /* isalpha() etc. from are locale-dependent, which we don't want. */ @@ -111,7 +135,9 @@ static bool upb_isalphanum(char c) { return upb_isletter(c) || upb_isbetween(c, '0', '9'); } -static bool upb_isident(const char *str, size_t len, bool full, upb_status *s) { +static bool upb_isident(upb_stringview name, bool full, upb_status *s) { + const char *str = name.data; + size_t len = name.size; bool start = true; size_t i; for (i = 0; i < len; i++) { @@ -141,7 +167,7 @@ static bool upb_isident(const char *str, size_t len, bool full, upb_status *s) { return !start; } -static const char *shortname(const char *fullname) { +static const char *shortdefname(const char *fullname) { const char *p; if (fullname == NULL) { @@ -388,22 +414,12 @@ err: /* upb_enumdef ****************************************************************/ -#if 0 -bool upb_enumdef_init(upb_enumdef *e) { - upb_def_init(&e->base, UPB_DEF_ENUM); - - if (!upb_strtable_init(&e->ntoi, UPB_CTYPE_INT32)) goto err2; - if (!upb_inttable_init(&e->iton, UPB_CTYPE_CSTR)) goto err1; - return true; -} -#endif - const char *upb_enumdef_fullname(const upb_enumdef *e) { return e->full_name; } const char *upb_enumdef_name(const upb_enumdef *e) { - return shortname(e->full_name); + return shortdefname(e->full_name); } #if 0 @@ -499,43 +515,10 @@ int32_t upb_enum_iter_number(upb_enum_iter *iter) { /* upb_fielddef ***************************************************************/ -static void upb_fielddef_init_default(upb_fielddef *f); - const char *upb_fielddef_fullname(const upb_fielddef *f) { return f->full_name; } -#if 0 -upb_fielddef *upb_fielddef_new(const void *o) { - upb_fielddef *f = upb_gmalloc(sizeof(*f)); - if (!f) return NULL; - upb_def_init(&f->base, UPB_DEF_FIELD); - f->msg.def = NULL; - f->sub.def = NULL; - f->oneof = NULL; - f->subdef_is_symbolic = false; - f->label_ = UPB_LABEL_OPTIONAL; - f->type_ = UPB_TYPE_INT32; - f->number_ = 0; - f->type_is_set_ = false; - f->tagdelim = false; - f->is_extension_ = false; - f->lazy_ = false; - f->packed_ = true; - - /* For the moment we default this to UPB_INTFMT_VARIABLE, since it will work - * with all integer types and is in some since more "default" since the most - * normal-looking proto2 types int32/int64/uint32/uint64 use variable. - * - * Other options to consider: - * - there is no default; users must set this manually (like type). - * - default signed integers to UPB_INTFMT_ZIGZAG, since it's more likely to - * be an optimal default for signed integers. */ - f->intfmt = UPB_INTFMT_VARIABLE; - return f; -} -#endif - upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f) { switch (f->type_) { case UPB_DESCRIPTOR_TYPE_DOUBLE: @@ -699,32 +682,6 @@ const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len) { return str->str; } -#if 0 -static void upb_fielddef_init_default(upb_fielddef *f) { - f->default_is_string = false; - switch (upb_fielddef_type(f)) { - case UPB_TYPE_DOUBLE: f->defaultval.dbl = 0; break; - case UPB_TYPE_FLOAT: f->defaultval.flt = 0; break; - case UPB_TYPE_INT32: - case UPB_TYPE_INT64: f->defaultval.sint = 0; break; - case UPB_TYPE_UINT64: - case UPB_TYPE_UINT32: - case UPB_TYPE_BOOL: f->defaultval.uint = 0; break; - case UPB_TYPE_STRING: - case UPB_TYPE_BYTES: - f->defaultval.bytes = newstr("", 0); - f->default_is_string = true; - break; - case UPB_TYPE_MESSAGE: break; - case UPB_TYPE_ENUM: - /* This is our special sentinel that indicates "not set" for an enum. */ - f->default_is_string = true; - f->defaultval.bytes = NULL; - break; - } -} -#endif - const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f) { UPB_ASSERT(upb_fielddef_type(f) == UPB_TYPE_MESSAGE); return f->sub.msgdef; @@ -735,38 +692,6 @@ const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f) { return f->sub.enumdef; } -#if 0 -bool upb_fielddef_setnumber(upb_fielddef *f, uint32_t number, upb_status *s) { - if (upb_fielddef_containingtype(f)) { - upb_status_seterrmsg( - s, "cannot change field number after adding to a message"); - return false; - } - if (number == 0 || number > UPB_MAX_FIELDNUMBER) { - upb_status_seterrf(s, "invalid field number (%u)", number); - return false; - } - f->number_ = number; - return true; -} - -static bool upb_subdef_typecheck(upb_fielddef *f, const upb_def *subdef, - upb_status *s) { - if (f->type_ == UPB_TYPE_MESSAGE) { - if (upb_dyncast_msgdef(subdef)) return true; - upb_status_seterrmsg(s, "invalid subdef type for this submessage field"); - return false; - } else if (f->type_ == UPB_TYPE_ENUM) { - if (upb_dyncast_enumdef(subdef)) return true; - upb_status_seterrmsg(s, "invalid subdef type for this enum field"); - return false; - } else { - upb_status_seterrmsg(s, "only message and enum fields can have a subdef"); - return false; - } -} -#endif - bool upb_fielddef_issubmsg(const upb_fielddef *f) { return upb_fielddef_type(f) == UPB_TYPE_MESSAGE; } @@ -812,7 +737,7 @@ const char *upb_msgdef_fullname(const upb_msgdef *m) { } const char *upb_msgdef_name(const upb_msgdef *m) { - return shortname(m->full_name); + return shortdefname(m->full_name); } upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m) { @@ -827,16 +752,6 @@ static bool check_field_add(const upb_msgdef *m, const upb_fielddef *f, if (upb_fielddef_containingtype(f) != NULL) { upb_status_seterrmsg(s, "fielddef already belongs to a message"); return false; - } else if (upb_fielddef_name(f) == NULL || upb_fielddef_number(f) == 0) { - upb_status_seterrmsg(s, "field name or number were not set"); - return false; - } else if (upb_msgdef_itof(m, upb_fielddef_number(f))) { - upb_status_seterrmsg(s, "duplicate field number"); - return false; - } else if (upb_strtable_lookup(&m->ntof, upb_fielddef_name(f), NULL)) { - upb_status_seterrmsg(s, "name conflicts with existing field or oneof"); - return false; - } return true; } @@ -1156,38 +1071,6 @@ void upb_oneof_iter_setdone(upb_oneof_iter *iter) { /* upb_filedef ****************************************************************/ -upb_filedef *upb_filedef_new(const void *owner) { - upb_filedef *f = upb_gmalloc(sizeof(*f)); - - if (!f) { - return NULL; - } - - f->package = NULL; - f->name = NULL; - f->phpprefix = NULL; - f->phpnamespace = NULL; - f->syntax = UPB_SYNTAX_PROTO2; - - if (!upb_inttable_init(&f->defs, UPB_CTYPE_CONSTPTR)) { - goto err; - } - - if (!upb_inttable_init(&f->deps, UPB_CTYPE_CONSTPTR)) { - goto err2; - } - - return f; - - -err2: - upb_inttable_uninit(&f->defs); - -err: - upb_gfree(f); - return NULL; -} - const char *upb_filedef_name(const upb_filedef *f) { return f->name; } @@ -1208,32 +1091,28 @@ upb_syntax_t upb_filedef_syntax(const upb_filedef *f) { return f->syntax; } -size_t upb_filedef_defcount(const upb_filedef *f) { - return upb_inttable_count(&f->defs); +int upb_filedef_msgcount(const upb_filedef *f) { + return f->msgcount; } -size_t upb_filedef_depcount(const upb_filedef *f) { - return upb_inttable_count(&f->deps); +int upb_filedef_depcount(const upb_filedef *f) { + return f->depcount; } -const upb_def *upb_filedef_def(const upb_filedef *f, size_t i) { - upb_value v; +int upb_filedef_enumcount(const upb_filedef *f) { + return f->enumcount; +} - if (upb_inttable_lookup32(&f->defs, i, &v)) { - return upb_value_getconstptr(v); - } else { - return NULL; - } +const upb_filedef *upb_filedef_dep(const upb_filedef *f, int i) { + return i < 0 || i >= f->depcount ? NULL : f->deps[i]; } -const upb_filedef *upb_filedef_dep(const upb_filedef *f, size_t i) { - upb_value v; +const upb_msgdef *upb_filedef_msg(const upb_filedef *f, int i) { + return i < 0 || i >= f->msgcount ? NULL : f->msgs[i]; +} - if (upb_inttable_lookup32(&f->deps, i, &v)) { - return upb_value_getconstptr(v); - } else { - return NULL; - } +const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i) { + return i < 0 || i >= f->enumcount ? NULL : f->enums[i]; } void upb_symtab_free(upb_symtab *s) { @@ -1243,53 +1122,44 @@ void upb_symtab_free(upb_symtab *s) { upb_symtab *upb_symtab_new() { upb_symtab *s = upb_gmalloc(sizeof(*s)); + upb_alloc *alloc; + if (!s) { return NULL; } - if (!upb_arena_init(&s->arena)) goto err2; - if (!upb_strtable_init2(&s->symtab, UPB_CTYPE_PTR, - upb_arena_alloc(&s->arena))) { - goto err1; - } - return s; + upb_arena_init(&s->arena); + alloc = upb_arena_alloc(&s->arena); -err1: - upb_arena_uninit(&s->arena); -err2: - upb_gfree(s); + if (!upb_strtable_init2(&s->syms, UPB_CTYPE_CONSTPTR, alloc) || + !upb_strtable_init2(&s->files, UPB_CTYPE_CONSTPTR, alloc)) { + upb_arena_uninit(&s->arena); + upb_gfree(s); + s = NULL; + } return s; } -const upb_def *upb_symtab_lookup(const upb_symtab *s, const char *sym) { - upb_value v; - upb_def *ret = upb_strtable_lookup(&s->symtab, sym, &v) ? - upb_value_getptr(v) : NULL; - return ret; -} - const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym) { upb_value v; - upb_def *def = upb_strtable_lookup(&s->symtab, sym, &v) ? - upb_value_getptr(v) : NULL; - return def ? upb_dyncast_msgdef(def) : NULL; + return upb_strtable_lookup(&s->syms, sym, &v) ? + unpack_def(v, UPB_DEFTYPE_MSG) : NULL; } const upb_msgdef *upb_symtab_lookupmsg2(const upb_symtab *s, const char *sym, size_t len) { upb_value v; - upb_def *def = upb_strtable_lookup2(&s->symtab, sym, len, &v) ? - upb_value_getptr(v) : NULL; - return def ? upb_dyncast_msgdef(def) : NULL; + return upb_strtable_lookup2(&s->syms, sym, len, &v) ? + unpack_def(v, UPB_DEFTYPE_MSG) : NULL; } const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym) { upb_value v; - upb_def *def = upb_strtable_lookup(&s->symtab, sym, &v) ? - upb_value_getptr(v) : NULL; - return def ? upb_dyncast_enumdef(def) : NULL; + return upb_strtable_lookup(&s->syms, sym, &v) ? + unpack_def(v, UPB_DEFTYPE_ENUM) : NULL; } +#if 0 /* Given a symbol and the base symbol inside which it is defined, find the * symbol's definition in t. */ static upb_def *upb_resolvename(const upb_strtable *t, @@ -1311,11 +1181,10 @@ static upb_def *upb_resolvename(const upb_strtable *t, const upb_def *upb_symtab_resolve(const upb_symtab *s, const char *base, const char *sym) { - upb_def *ret = upb_resolvename(&s->symtab, base, sym); + upb_def *ret = upb_resolvename(&s->syms, base, sym); return ret; } -#if 0 /* Now using the table, resolve symbolic references for subdefs. */ upb_strtable_begin(&iter, &addtab); for (; !upb_strtable_done(&iter); upb_strtable_next(&iter)) { @@ -1338,7 +1207,7 @@ const upb_def *upb_symtab_resolve(const upb_symtab *s, const char *base, * there, try existing defs. */ upb_def *subdef = upb_resolvename(&addtab, base, name); if (subdef == NULL) { - subdef = upb_resolvename(&s->symtab, base, name); + subdef = upb_resolvename(&s->syms, base, name); } if (subdef == NULL) { upb_status_seterrf( @@ -1352,76 +1221,258 @@ const upb_def *upb_symtab_resolve(const upb_symtab *s, const char *base, } #endif + +/* Code to build defs from descriptor protos. *********************************/ + +/* There is a question of how much validation to do here. It will be difficult + * to perfectly match the amount of validation performed by proto2. But since + * this code is used to directly build defs from Ruby (for example) we do need + * to validate important constraints like uniqueness of names and numbers. */ + +#define CHK(x) if (!(x)) return false +#define CHK_OOM(x) if (!(x)) { upb_upberr_setoom(ctx->status); return false; } + +typedef struct { + const upb_symtab *symtab; + const upb_filedef *file; /* File we are building. */ + upb_alloc *alloc; /* Allocate defs here. */ + upb_alloc *tmp; /* Alloc for addtab and any other tmp data. */ + upb_strtable *addtab; /* full_name -> packed def ptr for new defs. */ + upb_status *status; /* Record errors here. */ +} symtab_addctx; + +static const char *makefullname(const char *prefix, upb_stringview name) { + return NULL; +} + +static bool symtab_add(const symtab_addctx *ctx, const char *name, + upb_value v) { + upb_value tmp; + if (upb_strtable_lookup(ctx->addtab, name, &tmp) || + upb_strtable_lookup(&ctx->symtab->syms, name, &tmp)) { + upb_status_seterrf(ctx->status, "Duplicate symbol '%s'", name); + return false; + } + + CHK_OOM(upb_strtable_insert3(ctx->addtab, name, strlen(name), v, ctx->tmp)); + return true; +} + static bool create_oneofdef( - upb_symtab *s, const google_protobuf_OneofDescriptorProto *oneof_proto, - upb_msgdef *m, upb_status *s) { - upb_alloc *alloc = upb_arena_alloc(&s->arena); - upb_oneofdef *o = upb_malloc(alloc, sizeof(upb_oneofdef)); + const symtab_addctx *ctx, upb_msgdef *m, + const google_protobuf_OneofDescriptorProto *oneof_proto) { + upb_oneofdef *o = upb_malloc(ctx->alloc, sizeof(*o)); upb_stringview name = google_protobuf_OneofDescriptorProto_name(oneof_proto); upb_value o_ptr = upb_value_ptr(o); - CHK(o); - CHK(upb_inttable_init2(&o->itof, UPB_CTYPE_PTR, alloc)); - CHK(upb_strtable_init2(&o->ntof, UPB_CTYPE_PTR, alloc)); + CHK_OOM(o); + CHK_OOM(upb_inttable_init2(&o->itof, UPB_CTYPE_PTR, ctx->alloc)); + CHK_OOM(upb_strtable_init2(&o->ntof, UPB_CTYPE_PTR, ctx->alloc)); o->index = upb_strtable_count(&m->ntof); - o->name = upb_strdup2(name.data, name.size, alloc); + o->name = upb_strdup2(name.data, name.size, ctx->alloc); o->parent = m; - CHK(upb_strtable_insert3(&m->ntof, name.data, name.size, o_ptr, alloc)); + CHK_OOM(upb_strtable_insert3(&m->ntof, name.data, name.size, o_ptr, + ctx->alloc)); return true; } -static bool create_field( - upb_symtab *s, const google_protobuf_FieldDescriptorProto *field_proto, - upb_msgdef *m, upb_status *s) { - upb_alloc *alloc = upb_arena_alloc(&s->arena); - upb_fielddef *f = upb_malloc(alloc, sizeof(upb_fielddef)); - const google_protobuf_MessageOptions *options; +static bool parse_default(const char *str, size_t len, upb_fielddef *f) { + char *end; + switch (upb_fielddef_type(f)) { + case UPB_TYPE_INT32: { + long val = strtol(str, &end, 0); + CHK(val <= INT32_MAX && val >= INT32_MIN && errno != ERANGE && !*end); + f->defaultval.sint = val; + break; + } + case UPB_TYPE_ENUM: + case UPB_TYPE_INT64: { + /* XXX: Need to write our own strtoll, since it's not available in c89. */ + long long val = strtol(str, &end, 0); + CHK(val <= INT64_MAX && val >= INT64_MIN && errno != ERANGE && !*end); + f->defaultval.sint = val; + break; + } + case UPB_TYPE_UINT32: { + unsigned long val = strtoul(str, &end, 0); + CHK(val <= UINT32_MAX && errno != ERANGE && !*end); + f->defaultval.uint = val; + break; + } + case UPB_TYPE_UINT64: { + /* XXX: Need to write our own strtoull, since it's not available in c89. */ + unsigned long long val = strtoul(str, &end, 0); + CHK(val <= UINT64_MAX && errno != ERANGE && !*end); + f->defaultval.uint = val; + break; + } + case UPB_TYPE_DOUBLE: { + double val = strtod(str, &end); + CHK(errno != ERANGE && !*end); + f->defaultval.dbl = val; + break; + } + case UPB_TYPE_FLOAT: { + /* XXX: Need to write our own strtof, since it's not available in c89. */ + float val = strtod(str, &end); + CHK(errno != ERANGE && !*end); + f->defaultval.dbl = val; + break; + } + case UPB_TYPE_BOOL: { + if (strcmp(str, "false") == 0) { + f->defaultval.boolean = false; + } else if (strcmp(str, "true") == 0) { + f->defaultval.boolean = true; + } else { + return false; + } + } + case UPB_TYPE_STRING: + f->defaultval.str = newstr(str, len); + break; + case UPB_TYPE_BYTES: + /* XXX: need to interpret the C-escaped value. */ + f->defaultval.str = newstr(str, len); + case UPB_TYPE_MESSAGE: + /* Should not have a default value. */ + return false; + } + return true; +} + +static bool create_fielddef( + const symtab_addctx *ctx, const char *prefix, upb_msgdef *m, + const google_protobuf_FieldDescriptorProto *field_proto) { + upb_alloc *alloc = ctx->alloc; + upb_fielddef *f = upb_malloc(ctx->alloc, sizeof(*f)); + const google_protobuf_FieldOptions *options; + upb_stringview defaultval, name; + upb_value packed_v = pack_def(f, UPB_DEFTYPE_FIELD); + upb_value v = upb_value_constptr(f); + const char *shortname; + + CHK_OOM(f); + memset(f, 0, sizeof(*f)); + + name = google_protobuf_FieldDescriptorProto_name(field_proto); + + /* TODO(haberman): use hazzers. */ + if (name.size == 0 || f->number_ == 0) { + upb_status_seterrmsg(ctx->status, "field name or number were not set"); + return false; + } + + CHK(upb_isident(name, false, ctx->status)); + + f->full_name = makefullname(prefix, name); + f->type_ = (int)google_protobuf_FieldDescriptorProto_type(field_proto); + f->label_ = (int)google_protobuf_FieldDescriptorProto_label(field_proto); + f->number_ = google_protobuf_FieldDescriptorProto_number(field_proto); + shortname = shortdefname(f->full_name); + + if (f->number_ == 0 || f->number_ > UPB_MAX_FIELDNUMBER) { + upb_status_seterrf(ctx->status, "invalid field number (%u)", f->number_); + return false; + } +#if 0 + //f->oneof + //f->sub +#endif + + defaultval = google_protobuf_FieldDescriptorProto_default_value(field_proto); + + if (defaultval.data) { + CHK(parse_default(defaultval.data, defaultval.size, f)); + } + + options = google_protobuf_FieldDescriptorProto_options(field_proto); + + if (options) { + f->lazy_ = google_protobuf_FieldOptions_lazy(options); + f->packed_ = google_protobuf_FieldOptions_packed(options); + } + + if (m) { + /* direct message field. */ + if (!upb_strtable_insert3(&m->ntof, name.data, name.size, packed_v, alloc)) { + upb_status_seterrf(ctx->status, "duplicate name (%s)", shortname); + return false; + } + + if (!upb_inttable_insert2(&m->itof, f->number_, v, alloc)) { + upb_status_seterrf(ctx->status, "duplicate field number (%u)", f->number_); + return false; + } + + f->msgdef = m; + f->is_extension_ = false; + } else { + /* extension field. */ + f->is_extension_ = true; + } + + return true; +} + +static bool create_enumdef( + const symtab_addctx *ctx, const char *prefix, + const google_protobuf_EnumDescriptorProto *enum_proto) { + upb_enumdef *e = upb_malloc(ctx->alloc, sizeof(*e)); const upb_array *arr; + upb_stringview name; + size_t i; - CHK(f); + name = google_protobuf_EnumDescriptorProto_name(enum_proto); + CHK(upb_isident(name, false, ctx->status)); - f->msgdef = m; + e->full_name = makefullname(prefix, name); + e->defaultval = 0; - union { - int64_t sint; - uint64_t uint; - double dbl; - float flt; - void *bytes; - } defaultval; - const upb_msgdef *msgdef; - union { - const upb_msgdef *msgdef; - const upb_msgdef *enumdef; - } sub; - const upb_oneofdef *oneof; - bool is_extension_; - bool lazy_; - bool packed_; - upb_intfmt_t intfmt; - bool tagdelim; - upb_fieldtype_t type_; - upb_label_t label_; - uint32_t number_; - uint32_t selector_base; /* Used to index into a upb::Handlers table. */ - uint32_t index_; + CHK_OOM(e); + CHK_OOM(upb_strtable_init2(&e->ntoi, UPB_CTYPE_INT32, ctx->alloc)); + CHK_OOM(upb_inttable_init2(&e->iton, UPB_CTYPE_CSTR, ctx->alloc)); + + arr = google_protobuf_EnumDescriptorProto_value(enum_proto); + + for (i = 0; i < upb_array_size(arr); i++) { + const google_protobuf_EnumValueDescriptorProto *value = + upb_msgval_getptr(upb_array_get(arr, i)); + upb_stringview name = google_protobuf_EnumValueDescriptorProto_name(value); + char *name2 = upb_strdup2(name.data, name.size, ctx->alloc); + int32_t num = google_protobuf_EnumValueDescriptorProto_number(value); + upb_value v = upb_value_int32(num); + + CHK_OOM(name2 && upb_strtable_insert(&e->ntoi, name2, v)); + + if (!upb_inttable_lookup(&e->iton, num, NULL)) { + CHK_OOM(upb_inttable_insert(&e->iton, num, upb_value_cstr(name2))); + } + } + + return true; } -static bool create_msgdef(upb_symtab *s, - const google_protobuf_DescriptorProto *msg_proto, - upb_symtab *addtab, upb_status *status) { - upb_alloc *alloc = upb_arena_alloc(&s->arena); - upb_msgdef *m = upb_malloc(alloc, sizeof(upb_msgdef)); +static bool create_msgdef(const symtab_addctx *ctx, const char *prefix, + const google_protobuf_DescriptorProto *msg_proto) { + upb_msgdef *m = upb_malloc(ctx->alloc, sizeof(upb_msgdef)); const google_protobuf_MessageOptions *options; const upb_array *arr; + size_t i; + upb_stringview name; - CHK(m); - CHK(upb_inttable_init2(&m->itof, UPB_CTYPE_PTR, alloc)); - CHK(upb_strtable_init2(&m->ntof, UPB_CTYPE_PTR, alloc)); + CHK_OOM(m); + CHK_OOM(upb_inttable_init2(&m->itof, UPB_CTYPE_PTR, ctx->alloc)); + CHK_OOM(upb_strtable_init2(&m->ntof, UPB_CTYPE_PTR, ctx->alloc)); + name = google_protobuf_DescriptorProto_name(msg_proto); + CHK(upb_isident(name, false, ctx->status)); + + m->file = ctx->file; + m->full_name = makefullname(prefix, name); m->map_entry = false; options = google_protobuf_DescriptorProto_options(msg_proto); @@ -1434,118 +1485,181 @@ static bool create_msgdef(upb_symtab *s, for (i = 0; i < upb_array_size(arr); i++) { const google_protobuf_OneofDescriptorProto *oneof_proto = - (const void *)upb_array_get(arr, i); - CHK(create_oneofdef(s, oneof_proto, m, status)); + upb_msgval_getptr(upb_array_get(arr, i)); + CHK(create_oneofdef(ctx, m, oneof_proto)); } arr = google_protobuf_DescriptorProto_field(msg_proto); for (i = 0; i < upb_array_size(arr); i++) { const google_protobuf_FieldDescriptorProto *field_proto = - (const void *)upb_array_get(arr, i); - CHK(create_fielddef(s, field_proto, m, status)); + upb_msgval_getptr(upb_array_get(arr, i)); + CHK(create_fielddef(ctx, m->full_name, m, field_proto)); } + CHK(assign_msg_indices(m, ctx->status)); + assign_msg_wellknowntype(m); + symtab_add(ctx, m->full_name, pack_def(m, UPB_DEFTYPE_MSG)); + + /* This message is built. Now build nested messages and enums. */ + arr = google_protobuf_DescriptorProto_enum_type(msg_proto); for (i = 0; i < upb_array_size(arr); i++) { const google_protobuf_EnumDescriptorProto *enum_proto = - (const void *)upb_array_get(arr, i); - CHK(create_enumdef(s, enum_proto, addtab, status)); + upb_msgval_getptr(upb_array_get(arr, i)); + CHK(create_enumdef(ctx, m->full_name, enum_proto)); } arr = google_protobuf_DescriptorProto_nested_type(msg_proto); for (i = 0; i < upb_array_size(arr); i++) { const google_protobuf_DescriptorProto *msg_proto2 = - (const void *)upb_array_get(arr, i); - CHK(create_msgdef(s, msg_proto2, addtab, status)); + upb_msgval_getptr(upb_array_get(arr, i)); + CHK(create_msgdef(ctx, m->full_name, msg_proto2)); } return true; } -static char* strviewdup(upb_symtab *symtab, upb_stringview view) { +static char* strviewdup(const symtab_addctx *ctx, upb_stringview view) { if (view.size == 0) { return NULL; } - return upb_strdup2(view.data, view.size, upb_arena_alloc(&symtab->arena)); + return upb_strdup2(view.data, view.size, ctx->alloc); } -bool upb_symtab_addfile(upb_symtab *s, const char *buf, size_t len, - upb_status *status) { - upb_arena tmparena; - upb_strtable addtab; - upb_alloc *alloc = upb_arena_alloc(&s->arena); - upb_stringview serialized = upb_stringview_make(buf, len); - const google_protobuf_FileDescriptorProto *file_proto; +static bool build_filedef( + const symtab_addctx *ctx, upb_filedef *file, + const google_protobuf_FileDescriptorProto *file_proto) { const google_protobuf_FileOptions *file_options_proto; - upb_filedef *file = upb_malloc(alloc, sizeof(*file)); - const upb_array *arr; upb_strtable_iter iter; - size_t i; - - upb_arena_init(&tmparena); - upb_strtable_init2(&addtab, UPB_CTYPE_PTR, upb_arena_alloc(&tmparena)); - file_proto = - google_protobuf_FileDescriptorProto_parsenew(serialized, &tmparena); - + const upb_array *arr; + size_t i, n; + upb_stringview syntax, package; - if (!file_proto || !file) goto err; + package = google_protobuf_FileDescriptorProto_package(file_proto); + CHK(upb_isident(package, true, ctx->status)); file->name = - strviewdup(s, google_protobuf_FileDescriptorProto_name(file_proto)); - file->package = - strviewdup(s, google_protobuf_FileDescriptorProto_package(file_proto)); + strviewdup(ctx, google_protobuf_FileDescriptorProto_name(file_proto)); + file->package = strviewdup(ctx, package); file->phpprefix = NULL; file->phpnamespace = NULL; + syntax = google_protobuf_FileDescriptorProto_syntax(file_proto); + + if (upb_stringview_eql(syntax, upb_stringview_makez("proto2"))) { + file->syntax = UPB_SYNTAX_PROTO2; + } else if (upb_stringview_eql(syntax, upb_stringview_makez("proto3"))) { + file->syntax = UPB_SYNTAX_PROTO3; + } else { + upb_status_seterrf(ctx->status, "Invalid syntax '%s'", syntax); + return false; + } + + /* Read options. */ + file_options_proto = google_protobuf_FileDescriptorProto_options(file_proto); + if (file_options_proto) { - file->phpprefix = - strviewdup(s, google_protobuf_FileOptions_php_class_prefix(file_proto)); - file->phpnamespace = - strviewdup(s, google_protobuf_FileOptions_php_namespace(file_proto)); + file->phpprefix = strviewdup( + ctx, google_protobuf_FileOptions_php_class_prefix(file_options_proto)); + file->phpnamespace = strviewdup( + ctx, google_protobuf_FileOptions_php_namespace(file_options_proto)); } - arr = google_protobuf_FileDescriptorProto_message_type(file_proto); + /* Resolve dependencies. */ + + arr = google_protobuf_FileDescriptorProto_dependency(file_proto); + n = upb_array_size(arr); + file->deps = upb_malloc(ctx->alloc, sizeof(*file->deps) * n) ; + CHK_OOM(file->deps); + for (i = 0; i < n; i++) { + upb_stringview dep_name = upb_msgval_getstr(upb_array_get(arr, i)); + upb_value v; + if (!upb_strtable_lookup2(&ctx->symtab->files, dep_name.data, + dep_name.size, &v)) { + upb_status_seterrf(ctx->status, + "Depends on file '%s', but it has not been loaded", + dep_name.data); + return false; + } + file->deps[i] = upb_value_getconstptr(v); + } + /* Create messages. */ + + arr = google_protobuf_FileDescriptorProto_message_type(file_proto); for (i = 0; i < upb_array_size(arr); i++) { const google_protobuf_DescriptorProto *msg_proto = - (const void *)upb_array_get(arr, i); - if (!create_msgdef(s, msg_proto, &addtab, status)) goto err; + upb_msgval_getptr(upb_array_get(arr, i)); + CHK(create_msgdef(ctx, file->package, msg_proto)); } - arr = google_protobuf_FileDescriptorProto_enum_type(file_proto); + /* Create enums. */ + arr = google_protobuf_FileDescriptorProto_enum_type(file_proto); for (i = 0; i < upb_array_size(arr); i++) { const google_protobuf_EnumDescriptorProto *enum_proto = - (const void *)upb_array_get(arr, i); - if (!create_enumdef(s, enum_proto, &addtab, status)) goto err; + upb_msgval_getptr(upb_array_get(arr, i)); + CHK(create_enumdef(ctx, file->package, enum_proto)); } /* Now that all names are in the table, resolve references. */ - upb_strtable_begin(&iter, &addtab); + upb_strtable_begin(&iter, ctx->addtab); for (; !upb_strtable_done(&iter); upb_strtable_next(&iter)) { } - /* Success; add addtab to symtab. */ - upb_strtable_begin(&iter, &addtab); + return true; +} + +static bool upb_symtab_addtotabs(upb_symtab *s, symtab_addctx *ctx, + upb_status *status) { + const upb_filedef *file = ctx->file; + upb_alloc *alloc = upb_arena_alloc(&s->arena); + upb_strtable_iter iter; + + CHK_OOM(upb_strtable_insert3(&s->files, file->name, strlen(file->name), + upb_value_constptr(file), alloc)); + + upb_strtable_begin(&iter, ctx->addtab); for (; !upb_strtable_done(&iter); upb_strtable_next(&iter)) { const char *key = upb_strtable_iter_key(&iter); - size_t keylen = upb_strtable_iter_keylen(&iter); + size_t keylen = upb_strtable_iter_keylength(&iter); upb_value value = upb_strtable_iter_value(&iter); - upb_strtable_insert3(&s->symtab, key, keylen, value, alloc); + CHK_OOM(upb_strtable_insert3(&s->syms, key, keylen, value, alloc)); } return true; +} + +bool upb_symtab_addfile(upb_symtab *s, + const google_protobuf_FileDescriptorProto *file_proto, + upb_status *status) { + upb_arena tmparena; + upb_strtable addtab; + symtab_addctx ctx; + upb_alloc *alloc = upb_arena_alloc(&s->arena); + upb_filedef *file = upb_malloc(alloc, sizeof(*file)); + bool ok; + + ctx.file = file; + ctx.alloc = alloc; + ctx.tmp = upb_arena_alloc(&tmparena); + ctx.addtab = &addtab; + ctx.status = status; + + upb_arena_init(&tmparena); + + ok = file && + upb_strtable_init2(&addtab, UPB_CTYPE_PTR, ctx.tmp) && + build_filedef(&ctx, file, file_proto) && + upb_symtab_addtotabs(s, &ctx, status); -err: upb_arena_uninit(&tmparena); - return false; + return ok; } -bool upb_symtab_addset(upb_symtab *s, const char *buf, size_t len, - upb_status *status) { - return true; -} +#undef CHK +#undef CHK_OOM diff --git a/upb/def.h b/upb/def.h index 2551c86..4a7342e 100644 --- a/upb/def.h +++ b/upb/def.h @@ -19,6 +19,7 @@ #include "upb/upb.h" #include "upb/table.int.h" +#include "google/protobuf/descriptor.upb.h" #ifdef __cplusplus #include @@ -665,6 +666,13 @@ const char *upb_filedef_package(const upb_filedef *f); const char *upb_filedef_phpprefix(const upb_filedef *f); const char *upb_filedef_phpnamespace(const upb_filedef *f); upb_syntax_t upb_filedef_syntax(const upb_filedef *f); +int upb_filedef_depcount(const upb_filedef *f); +int upb_filedef_msgcount(const upb_filedef *f); +int upb_filedef_enumcount(const upb_filedef *f); +const upb_filedef *upb_filedef_dep(const upb_filedef *f, int i); +const upb_msgdef *upb_filedef_msg(const upb_filedef *f, int i); +const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i); + UPB_END_EXTERN_C @@ -708,10 +716,9 @@ const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym); const upb_msgdef *upb_symtab_lookupmsg2( const upb_symtab *s, const char *sym, size_t len); const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym); -bool upb_symtab_addfile(upb_symtab *s, const char *buf, size_t len, +bool upb_symtab_addfile(upb_symtab *s, + const google_protobuf_FileDescriptorProto* file, upb_status *status); -bool upb_symtab_addset(upb_symtab *s, const char *buf, size_t len, - upb_status *status); UPB_END_EXTERN_C diff --git a/upb/handlers.c b/upb/handlers.c index 7382e0c..fa75a48 100644 --- a/upb/handlers.c +++ b/upb/handlers.c @@ -84,7 +84,7 @@ static upb_handlers *newformsg(const upb_msgdef *m, const void *owner, if (!upb_fielddef_issubmsg(f)) continue; - subdef = upb_downcast_msgdef(upb_fielddef_subdef(f)); + subdef = upb_fielddef_msgsubdef(f); if (upb_inttable_lookupptr(&s->tab, subdef, &subm_ent)) { upb_handlers_setsubhandlers(h, f, upb_value_getptr(subm_ent)); } else { @@ -392,7 +392,7 @@ bool upb_handlers_setsubhandlers(upb_handlers *h, const upb_fielddef *f, UPB_ASSERT(!upb_handlers_isfrozen(h)); UPB_ASSERT(upb_fielddef_issubmsg(f)); if (SUBH_F(h, f)) return false; /* Can't reset. */ - if (upb_msgdef_upcast(upb_handlers_msgdef(sub)) != upb_fielddef_subdef(f)) { + if (upb_handlers_msgdef(sub) != upb_fielddef_msgsubdef(f)) { return false; } SUBH_F(h, f) = sub; diff --git a/upb/json/parser.c b/upb/json/parser.c index 83590a1..85919e7 100644 --- a/upb/json/parser.c +++ b/upb/json/parser.c @@ -264,10 +264,6 @@ struct upb_json_parsermethod { upb_byteshandler input_handler_; - /* Mainly for the purposes of refcounting, so all the fielddefs we point - * to stay alive. */ - const upb_msgdef *msg; - /* Keys are upb_msgdef*, values are upb_strtable (json_name -> fielddef) */ upb_inttable name_tables; }; @@ -1327,8 +1323,7 @@ static bool end_stringval_nontop(upb_json_parser *p) { case UPB_TYPE_ENUM: { /* Resolve enum symbolic name to integer value. */ - const upb_enumdef *enumdef = - (const upb_enumdef*)upb_fielddef_subdef(p->top->f); + const upb_enumdef *enumdef = upb_fielddef_enumsubdef(p->top->f); size_t len; const char *buf = accumulate_getptr(p, &len); @@ -2414,11 +2409,11 @@ static bool is_string_wrapper_object(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 2576 "upb/json/parser.rl" +#line 2571 "upb/json/parser.rl" -#line 2422 "upb/json/parser.c" +#line 2417 "upb/json/parser.c" static const char _json_actions[] = { 0, 1, 0, 1, 1, 1, 3, 1, 4, 1, 6, 1, 7, 1, 8, 1, @@ -2665,7 +2660,7 @@ static const int json_en_value_machine = 75; static const int json_en_main = 1; -#line 2579 "upb/json/parser.rl" +#line 2574 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -2688,7 +2683,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 2692 "upb/json/parser.c" +#line 2687 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -2763,83 +2758,83 @@ _match: switch ( *_acts++ ) { case 1: -#line 2427 "upb/json/parser.rl" +#line 2422 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 2429 "upb/json/parser.rl" +#line 2424 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 23;goto _again;} } break; case 3: -#line 2433 "upb/json/parser.rl" +#line 2428 "upb/json/parser.rl" { start_text(parser, p); } break; case 4: -#line 2434 "upb/json/parser.rl" +#line 2429 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -#line 2440 "upb/json/parser.rl" +#line 2435 "upb/json/parser.rl" { start_hex(parser); } break; case 6: -#line 2441 "upb/json/parser.rl" +#line 2436 "upb/json/parser.rl" { hexdigit(parser, p); } break; case 7: -#line 2442 "upb/json/parser.rl" +#line 2437 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -#line 2448 "upb/json/parser.rl" +#line 2443 "upb/json/parser.rl" { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -#line 2454 "upb/json/parser.rl" +#line 2449 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 10: -#line 2466 "upb/json/parser.rl" +#line 2461 "upb/json/parser.rl" { start_duration_base(parser, p); } break; case 11: -#line 2467 "upb/json/parser.rl" +#line 2462 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_duration_base(parser, p)); } break; case 12: -#line 2469 "upb/json/parser.rl" +#line 2464 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 13: -#line 2474 "upb/json/parser.rl" +#line 2469 "upb/json/parser.rl" { start_timestamp_base(parser, p); } break; case 14: -#line 2475 "upb/json/parser.rl" +#line 2470 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_base(parser, p)); } break; case 15: -#line 2477 "upb/json/parser.rl" +#line 2472 "upb/json/parser.rl" { start_timestamp_fraction(parser, p); } break; case 16: -#line 2478 "upb/json/parser.rl" +#line 2473 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } break; case 17: -#line 2480 "upb/json/parser.rl" +#line 2475 "upb/json/parser.rl" { start_timestamp_zone(parser, p); } break; case 18: -#line 2481 "upb/json/parser.rl" +#line 2476 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } break; case 19: -#line 2483 "upb/json/parser.rl" +#line 2478 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 20: -#line 2488 "upb/json/parser.rl" +#line 2483 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) { {stack[top++] = cs; cs = 47;goto _again;} @@ -2851,11 +2846,11 @@ _match: } break; case 21: -#line 2499 "upb/json/parser.rl" +#line 2494 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 75;goto _again;} } break; case 22: -#line 2504 "upb/json/parser.rl" +#line 2499 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_member(parser, p); @@ -2865,11 +2860,11 @@ _match: } break; case 23: -#line 2511 "upb/json/parser.rl" +#line 2506 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_membername(parser)); } break; case 24: -#line 2514 "upb/json/parser.rl" +#line 2509 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { end_any_member(parser, p); @@ -2879,7 +2874,7 @@ _match: } break; case 25: -#line 2525 "upb/json/parser.rl" +#line 2520 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_object(parser, p); @@ -2889,7 +2884,7 @@ _match: } break; case 26: -#line 2534 "upb/json/parser.rl" +#line 2529 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { CHECK_RETURN_TOP(end_any_object(parser, p)); @@ -2899,54 +2894,54 @@ _match: } break; case 27: -#line 2546 "upb/json/parser.rl" +#line 2541 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_array(parser)); } break; case 28: -#line 2550 "upb/json/parser.rl" +#line 2545 "upb/json/parser.rl" { end_array(parser); } break; case 29: -#line 2555 "upb/json/parser.rl" +#line 2550 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_number(parser, p)); } break; case 30: -#line 2556 "upb/json/parser.rl" +#line 2551 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 31: -#line 2558 "upb/json/parser.rl" +#line 2553 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_stringval(parser)); } break; case 32: -#line 2559 "upb/json/parser.rl" +#line 2554 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_stringval(parser)); } break; case 33: -#line 2561 "upb/json/parser.rl" +#line 2556 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 34: -#line 2563 "upb/json/parser.rl" +#line 2558 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 35: -#line 2565 "upb/json/parser.rl" +#line 2560 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 36: -#line 2567 "upb/json/parser.rl" +#line 2562 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_subobject_full(parser)); } break; case 37: -#line 2568 "upb/json/parser.rl" +#line 2563 "upb/json/parser.rl" { end_subobject_full(parser); } break; case 38: -#line 2573 "upb/json/parser.rl" +#line 2568 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 2950 "upb/json/parser.c" +#line 2945 "upb/json/parser.c" } } @@ -2963,32 +2958,32 @@ _again: while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 0: -#line 2425 "upb/json/parser.rl" +#line 2420 "upb/json/parser.rl" { p--; {cs = stack[--top]; if ( p == pe ) goto _test_eof; goto _again;} } break; case 30: -#line 2556 "upb/json/parser.rl" +#line 2551 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 33: -#line 2561 "upb/json/parser.rl" +#line 2556 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 34: -#line 2563 "upb/json/parser.rl" +#line 2558 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 35: -#line 2565 "upb/json/parser.rl" +#line 2560 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 37: -#line 2568 "upb/json/parser.rl" +#line 2563 "upb/json/parser.rl" { end_subobject_full(parser); } break; -#line 2992 "upb/json/parser.c" +#line 2987 "upb/json/parser.c" } } } @@ -2996,7 +2991,7 @@ goto _again;} } _out: {} } -#line 2601 "upb/json/parser.rl" +#line 2596 "upb/json/parser.rl" if (p != pe) { upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p); @@ -3044,13 +3039,13 @@ static void json_parser_reset(upb_json_parser *p) { /* Emit Ragel initialization of the parser. */ -#line 3048 "upb/json/parser.c" +#line 3043 "upb/json/parser.c" { cs = json_start; top = 0; } -#line 2648 "upb/json/parser.rl" +#line 2643 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); @@ -3060,13 +3055,6 @@ static void json_parser_reset(upb_json_parser *p) { upb_status_clear(&p->status); } -static void visit_json_parsermethod(const upb_refcounted *r, - upb_refcounted_visit *visit, - void *closure) { - const upb_json_parsermethod *method = (upb_json_parsermethod*)r; - visit(r, upb_msgdef_upcast2(method->msg), closure); -} - static void free_json_parsermethod(upb_refcounted *r) { upb_json_parsermethod *method = (upb_json_parsermethod*)r; @@ -3183,14 +3171,10 @@ upb_bytessink *upb_json_parser_input(upb_json_parser *p) { upb_json_parsermethod *upb_json_parsermethod_new(const upb_msgdef* md, const void* owner) { - static const struct upb_refcounted_vtbl vtbl = {visit_json_parsermethod, - free_json_parsermethod}; + static const struct upb_refcounted_vtbl vtbl = {NULL, free_json_parsermethod}; upb_json_parsermethod *ret = upb_gmalloc(sizeof(*ret)); upb_refcounted_init(upb_json_parsermethod_upcast_mutable(ret), &vtbl, owner); - ret->msg = md; - upb_ref2(md, ret); - upb_byteshandler_init(&ret->input_handler_); upb_byteshandler_setstring(&ret->input_handler_, parse, ret); upb_byteshandler_setendstr(&ret->input_handler_, end, ret); diff --git a/upb/json/parser.h b/upb/json/parser.h index fb5fd2f..91b08d8 100644 --- a/upb/json/parser.h +++ b/upb/json/parser.h @@ -52,7 +52,8 @@ class upb::json::ParserMethod { /* Include base methods from upb::ReferenceCounted. */ UPB_REFCOUNTED_CPPMETHODS - /* Returns handlers for parsing according to the specified schema. */ + /* Returns handlers for parsing according to the specified schema. + * The MessageDef must outlive the ParserMethod. */ static reffed_ptr New(const upb::MessageDef* md); /* The destination handlers that are statically bound to this method. diff --git a/upb/json/parser.rl b/upb/json/parser.rl index a7bdb3f..9ceb816 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -262,10 +262,6 @@ struct upb_json_parsermethod { upb_byteshandler input_handler_; - /* Mainly for the purposes of refcounting, so all the fielddefs we point - * to stay alive. */ - const upb_msgdef *msg; - /* Keys are upb_msgdef*, values are upb_strtable (json_name -> fielddef) */ upb_inttable name_tables; }; @@ -1325,8 +1321,7 @@ static bool end_stringval_nontop(upb_json_parser *p) { case UPB_TYPE_ENUM: { /* Resolve enum symbolic name to integer value. */ - const upb_enumdef *enumdef = - (const upb_enumdef*)upb_fielddef_subdef(p->top->f); + const upb_enumdef *enumdef = upb_fielddef_enumsubdef(p->top->f); size_t len; const char *buf = accumulate_getptr(p, &len); @@ -2654,13 +2649,6 @@ static void json_parser_reset(upb_json_parser *p) { upb_status_clear(&p->status); } -static void visit_json_parsermethod(const upb_refcounted *r, - upb_refcounted_visit *visit, - void *closure) { - const upb_json_parsermethod *method = (upb_json_parsermethod*)r; - visit(r, upb_msgdef_upcast2(method->msg), closure); -} - static void free_json_parsermethod(upb_refcounted *r) { upb_json_parsermethod *method = (upb_json_parsermethod*)r; @@ -2777,14 +2765,10 @@ upb_bytessink *upb_json_parser_input(upb_json_parser *p) { upb_json_parsermethod *upb_json_parsermethod_new(const upb_msgdef* md, const void* owner) { - static const struct upb_refcounted_vtbl vtbl = {visit_json_parsermethod, - free_json_parsermethod}; + static const struct upb_refcounted_vtbl vtbl = {NULL, free_json_parsermethod}; upb_json_parsermethod *ret = upb_gmalloc(sizeof(*ret)); upb_refcounted_init(upb_json_parsermethod_upcast_mutable(ret), &vtbl, owner); - ret->msg = md; - upb_ref2(md, ret); - upb_byteshandler_init(&ret->input_handler_); upb_byteshandler_setstring(&ret->input_handler_, parse, ret); upb_byteshandler_setendstr(&ret->input_handler_, end, ret); diff --git a/upb/json/printer.c b/upb/json/printer.c index fe306d4..00438aa 100644 --- a/upb/json/printer.c +++ b/upb/json/printer.c @@ -585,7 +585,7 @@ static void set_enum_hd(upb_handlers *h, bool preserve_fieldnames, upb_handlerattr *attr) { EnumHandlerData *hd = upb_gmalloc(sizeof(EnumHandlerData)); - hd->enumdef = (const upb_enumdef *)upb_fielddef_subdef(f); + hd->enumdef = upb_fielddef_enumsubdef(f); hd->keyname = newstrpc(h, f, preserve_fieldnames); upb_handlers_addcleanup(h, hd, upb_gfree); upb_handlerattr_sethandlerdata(attr, hd); diff --git a/upb/msg.h b/upb/msg.h index 3f0aa0b..f707795 100644 --- a/upb/msg.h +++ b/upb/msg.h @@ -22,6 +22,7 @@ #define UPB_MSG_H_ #include +#include #include "upb/upb.h" #ifdef __cplusplus @@ -86,6 +87,14 @@ UPB_INLINE upb_stringview upb_stringview_make(const char *data, size_t size) { return ret; } +UPB_INLINE upb_stringview upb_stringview_makez(const char *data) { + return upb_stringview_make(data, strlen(data)); +} + +UPB_INLINE bool upb_stringview_eql(upb_stringview a, upb_stringview b) { + return a.size == b.size && memcmp(a.data, b.data, a.size) == 0; +} + #define UPB_STRINGVIEW_INIT(ptr, len) {ptr, len} diff --git a/upb/pb/textprinter.c b/upb/pb/textprinter.c index abfc2eb..e8033f8 100644 --- a/upb/pb/textprinter.c +++ b/upb/pb/textprinter.c @@ -183,7 +183,7 @@ static bool textprinter_putenum(void *closure, const void *handler_data, int32_t val) { upb_textprinter *p = closure; const upb_fielddef *f = handler_data; - const upb_enumdef *enum_def = upb_downcast_enumdef(upb_fielddef_subdef(f)); + const upb_enumdef *enum_def = upb_fielddef_enumsubdef(f); const char *label = upb_enumdef_iton(enum_def, val); if (label) { indent(p); @@ -292,7 +292,7 @@ static void onmreg(const void *c, upb_handlers *h) { break; case UPB_TYPE_MESSAGE: { const char *name = - upb_fielddef_istagdelim(f) + upb_fielddef_descriptortype(f) == UPB_DESCRIPTOR_TYPE_GROUP ? shortname(upb_msgdef_fullname(upb_fielddef_msgsubdef(f))) : upb_fielddef_name(f); upb_handlerattr_sethandlerdata(&attr, name); diff --git a/upb/structdefs.int.h b/upb/structdefs.int.h deleted file mode 100644 index cf8bd1d..0000000 --- a/upb/structdefs.int.h +++ /dev/null @@ -1,196 +0,0 @@ -/* -** This file contains definitions of structs that should be considered private -** and NOT stable across versions of upb. -** -** The only reason they are declared here and not in .c files is to allow upb -** and the application (if desired) to embed statically-initialized instances -** of structures like defs. -** -** If you include this file, all guarantees of ABI compatibility go out the -** window! Any code that includes this file needs to recompile against the -** exact same version of upb that they are linking against. -** -** You also need to recompile if you change the value of the UPB_DEBUG_REFS -** flag. -*/ - -#include "upb/def.h" - -#ifndef UPB_STATICINIT_H_ -#define UPB_STATICINIT_H_ - -#ifdef __cplusplus -/* Because of how we do our typedefs, this header can't be included from C++. */ -#error This file cannot be included from C++ -#endif - -/* upb_refcounted *************************************************************/ - - -/* upb_def ********************************************************************/ - -struct upb_def { - upb_refcounted base; - - const char *fullname; - const upb_filedef* file; - char type; /* A upb_deftype_t (char to save space) */ - - /* Used as a flag during the def's mutable stage. Must be false unless - * it is currently being used by a function on the stack. This allows - * us to easily determine which defs were passed into the function's - * current invocation. */ - bool came_from_user; -}; - -#define UPB_DEF_INIT(name, type, vtbl, refs, ref2s) \ - { UPB_REFCOUNT_INIT(vtbl, refs, ref2s), name, NULL, type, false } - - -/* upb_fielddef ***************************************************************/ - -struct upb_fielddef { - upb_def base; - - union { - int64_t sint; - uint64_t uint; - double dbl; - float flt; - void *bytes; - } defaultval; - union { - const upb_msgdef *def; /* If !msg_is_symbolic. */ - char *name; /* If msg_is_symbolic. */ - } msg; - union { - const upb_def *def; /* If !subdef_is_symbolic. */ - char *name; /* If subdef_is_symbolic. */ - } sub; /* The msgdef or enumdef for this field, if upb_hassubdef(f). */ - bool subdef_is_symbolic; - bool msg_is_symbolic; - const upb_oneofdef *oneof; - bool default_is_string; - bool type_is_set_; /* False until type is explicitly set. */ - bool is_extension_; - bool lazy_; - bool packed_; - upb_intfmt_t intfmt; - bool tagdelim; - upb_fieldtype_t type_; - upb_label_t label_; - uint32_t number_; - uint32_t selector_base; /* Used to index into a upb::Handlers table. */ - uint32_t index_; -}; - -extern const struct upb_refcounted_vtbl upb_fielddef_vtbl; - -#define UPB_FIELDDEF_INIT(label, type, intfmt, tagdelim, is_extension, lazy, \ - packed, name, num, msgdef, subdef, selector_base, \ - index, defaultval, refs, ref2s) \ - { \ - UPB_DEF_INIT(name, UPB_DEF_FIELD, &upb_fielddef_vtbl, refs, ref2s), \ - defaultval, {msgdef}, {subdef}, NULL, false, false, \ - type == UPB_TYPE_STRING || type == UPB_TYPE_BYTES, true, is_extension, \ - lazy, packed, intfmt, tagdelim, type, label, num, selector_base, index \ - } - - -/* upb_msgdef *****************************************************************/ - -struct upb_msgdef { - upb_def base; - - size_t selector_count; - uint32_t submsg_field_count; - - /* Tables for looking up fields by number and name. */ - upb_inttable itof; /* int to field */ - upb_strtable ntof; /* name to field/oneof */ - - /* Is this a map-entry message? */ - bool map_entry; - - /* Whether this message has proto2 or proto3 semantics. */ - upb_syntax_t syntax; - - /* Type of well known type message. UPB_WELLKNOWN_UNSPECIFIED for - * non-well-known message. */ - upb_wellknowntype_t well_known_type; - - /* TODO(haberman): proper extension ranges (there can be multiple). */ -}; - -extern const struct upb_refcounted_vtbl upb_msgdef_vtbl; - -/* TODO: also support static initialization of the oneofs table. This will be - * needed if we compile in descriptors that contain oneofs. */ -#define UPB_MSGDEF_INIT(name, selector_count, submsg_field_count, itof, ntof, \ - map_entry, syntax, well_known_type, refs, ref2s) \ - { \ - UPB_DEF_INIT(name, UPB_DEF_MSG, &upb_fielddef_vtbl, refs, ref2s), \ - selector_count, submsg_field_count, itof, ntof, map_entry, syntax, \ - well_known_type \ - } - - -/* upb_enumdef ****************************************************************/ - -struct upb_enumdef { - upb_def base; - - upb_strtable ntoi; - upb_inttable iton; - int32_t defaultval; -}; - -extern const struct upb_refcounted_vtbl upb_enumdef_vtbl; - -#define UPB_ENUMDEF_INIT(name, ntoi, iton, defaultval, refs, ref2s) \ - { UPB_DEF_INIT(name, UPB_DEF_ENUM, &upb_enumdef_vtbl, refs, ref2s), ntoi, \ - iton, defaultval } - - -/* upb_oneofdef ***************************************************************/ - -struct upb_oneofdef { - upb_refcounted base; - - uint32_t index; /* Index within oneofs. */ - const char *name; - upb_strtable ntof; - upb_inttable itof; - const upb_msgdef *parent; -}; - -extern const struct upb_refcounted_vtbl upb_oneofdef_vtbl; - -#define UPB_ONEOFDEF_INIT(name, ntof, itof, refs, ref2s) \ - { UPB_REFCOUNT_INIT(&upb_oneofdef_vtbl, refs, ref2s), 0, name, ntof, itof } - - -/* upb_symtab *****************************************************************/ - -struct upb_symtab { - upb_refcounted base; - - upb_strtable symtab; -}; - -struct upb_filedef { - upb_refcounted base; - - const char *name; - const char *package; - const char *phpprefix; - const char *phpnamespace; - upb_syntax_t syntax; - - upb_inttable defs; - upb_inttable deps; -}; - -extern const struct upb_refcounted_vtbl upb_filedef_vtbl; - -#endif /* UPB_STATICINIT_H_ */ -- cgit v1.2.3 From 0fed529ea1795d728a7ec75c3f06d95e4ba3bd8d Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 12 Dec 2018 06:35:03 -0800 Subject: WIP, more progress on refactoring. --- CMakeLists.txt | 1 - upb/def.c | 647 +++++++++++++++++++++++++-------------------------------- upb/def.h | 30 +-- 3 files changed, 281 insertions(+), 397 deletions(-) (limited to 'upb') diff --git a/CMakeLists.txt b/CMakeLists.txt index 26ac066..e3d30c2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,7 +72,6 @@ add_library(upb upb/port_undef.inc upb/refcounted.c upb/sink.c - upb/structdefs.int.h upb/structs.int.h upb/table.c upb/table.int.h diff --git a/upb/def.c b/upb/def.c index 3167222..33ad70e 100644 --- a/upb/def.c +++ b/upb/def.c @@ -13,8 +13,8 @@ typedef struct { char str[1]; /* Null-terminated string data follows. */ } str_t; -static str_t *newstr(const char *data, size_t len) { - str_t *ret = upb_gmalloc(sizeof(*ret) + len); +static str_t *newstr(upb_alloc *alloc, const char *data, size_t len) { + str_t *ret = upb_malloc(alloc, sizeof(*ret) + len); if (!ret) return NULL; ret->len = len; memcpy(ret->str, data, len); @@ -22,9 +22,9 @@ static str_t *newstr(const char *data, size_t len) { return ret; } -static void freestr(str_t *s) { upb_gfree(s); } - struct upb_fielddef { + const upb_filedef *file; + const upb_msgdef *msgdef; const char *full_name; union { int64_t sint; @@ -34,11 +34,11 @@ struct upb_fielddef { bool boolean; str_t *str; } defaultval; - const upb_msgdef *msgdef; const upb_oneofdef *oneof; union { const upb_msgdef *msgdef; const upb_enumdef *enumdef; + const google_protobuf_FieldDescriptorProto *unresolved; } sub; uint32_t number_; uint32_t index_; @@ -60,6 +60,11 @@ struct upb_msgdef { upb_inttable itof; upb_strtable ntof; + const upb_fielddef *fields; + const upb_oneofdef *oneofs; + int field_count; + int oneof_count; + /* Is this a map-entry message? */ bool map_entry; upb_wellknowntype_t well_known_type; @@ -68,6 +73,7 @@ struct upb_msgdef { }; struct upb_enumdef { + const upb_filedef *file; const char *full_name; upb_strtable ntoi; upb_inttable iton; @@ -76,7 +82,7 @@ struct upb_enumdef { struct upb_oneofdef { const upb_msgdef *parent; - const char *name; + const char *full_name; uint32_t index; upb_strtable ntof; upb_inttable itof; @@ -89,13 +95,15 @@ struct upb_filedef { const char *phpnamespace; upb_syntax_t syntax; - const upb_msgdef **msgs; - const upb_enumdef **enums; const upb_filedef **deps; - - int msgcount; - int enumcount; - int depcount; + const upb_msgdef *msgs; + const upb_enumdef *enums; + const upb_fielddef *exts; + + int dep_count; + int msg_count; + int enum_count; + int ext_count; }; /* Inside a symtab we store tagged pointers to specific def types. */ @@ -422,60 +430,11 @@ const char *upb_enumdef_name(const upb_enumdef *e) { return shortdefname(e->full_name); } -#if 0 -bool upb_enumdef_addval(upb_enumdef *e, const char *name, int32_t num, - upb_status *status) { - char *name2; - - if (!upb_isident(name, strlen(name), false, status)) { - return false; - } - - if (upb_enumdef_ntoiz(e, name, NULL)) { - upb_status_seterrf(status, "name '%s' is already defined", name); - return false; - } - - if (!upb_strtable_insert(&e->ntoi, name, upb_value_int32(num))) { - upb_status_seterrmsg(status, "out of memory"); - return false; - } - - if (!upb_inttable_lookup(&e->iton, num, NULL)) { - name2 = upb_gstrdup(name); - if (!name2 || !upb_inttable_insert(&e->iton, num, upb_value_cstr(name2))) { - upb_status_seterrmsg(status, "out of memory"); - upb_strtable_remove(&e->ntoi, name, NULL); - return false; - } - } - - if (upb_enumdef_numvals(e) == 1) { - bool ok = upb_enumdef_setdefault(e, num, NULL); - UPB_ASSERT(ok); - } - - return true; -} -#endif - int32_t upb_enumdef_default(const upb_enumdef *e) { UPB_ASSERT(upb_enumdef_iton(e, e->defaultval)); return e->defaultval; } -#if 0 -bool upb_enumdef_setdefault(upb_enumdef *e, int32_t val, upb_status *s) { - UPB_ASSERT(!upb_enumdef_isfrozen(e)); - if (!upb_enumdef_iton(e, val)) { - upb_status_seterrf(s, "number '%d' is not in the enum.", val); - return false; - } - e->defaultval = val; - return true; -} -#endif - int upb_enumdef_numvals(const upb_enumdef *e) { return upb_strtable_count(&e->ntoi); } @@ -744,96 +703,6 @@ upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m) { return m->file->syntax; } -#if 0 -/* Helper: check that the field |f| is safe to add to msgdef |m|. Set an error - * on status |s| and return false if not. */ -static bool check_field_add(const upb_msgdef *m, const upb_fielddef *f, - upb_status *s) { - if (upb_fielddef_containingtype(f) != NULL) { - upb_status_seterrmsg(s, "fielddef already belongs to a message"); - return false; - return true; -} - -bool upb_msgdef_addfield(upb_msgdef *m, upb_fielddef *f, const void *ref_donor, - upb_status *s) { - /* TODO: extensions need to have a separate namespace, because proto2 allows a - * top-level extension (ie. one not in any package) to have the same name as a - * field from the message. - * - * This also implies that there needs to be a separate lookup-by-name method - * for extensions. It seems desirable for iteration to return both extensions - * and non-extensions though. - * - * We also need to validate that the field number is in an extension range iff - * it is an extension. - * - * This method is idempotent. Check if |f| is already part of this msgdef and - * return immediately if so. */ - if (upb_fielddef_containingtype(f) == m) { - if (ref_donor) upb_fielddef_unref(f, ref_donor); - return true; - } - - /* Check constraints for all fields before performing any action. */ - if (!check_field_add(m, f, s)) { - return false; - } else if (upb_fielddef_containingoneof(f) != NULL) { - /* Fields in a oneof can only be added by adding the oneof to the msgdef. */ - upb_status_seterrmsg(s, "fielddef is part of a oneof"); - return false; - } - - /* Constraint checks ok, perform the action. */ - add_field(m, f, ref_donor); - return true; -} - -bool upb_msgdef_addoneof(upb_msgdef *m, upb_oneofdef *o, const void *ref_donor, - upb_status *s) { - upb_oneof_iter it; - - /* Check various conditions that would prevent this oneof from being added. */ - if (upb_oneofdef_containingtype(o)) { - upb_status_seterrmsg(s, "oneofdef already belongs to a message"); - return false; - } else if (upb_oneofdef_name(o) == NULL) { - upb_status_seterrmsg(s, "oneofdef name was not set"); - return false; - } else if (upb_strtable_lookup(&m->ntof, upb_oneofdef_name(o), NULL)) { - upb_status_seterrmsg(s, "name conflicts with existing field or oneof"); - return false; - } - - /* Check that all of the oneof's fields do not conflict with names or numbers - * of fields already in the message. */ - for (upb_oneof_begin(&it, o); !upb_oneof_done(&it); upb_oneof_next(&it)) { - const upb_fielddef *f = upb_oneof_iter_field(&it); - if (!check_field_add(m, f, s)) { - return false; - } - } - - /* Everything checks out -- commit now. */ - - /* Add oneof itself first. */ - o->parent = m; - upb_strtable_insert(&m->ntof, upb_oneofdef_name(o), upb_value_ptr(o)); - upb_ref2(o, m); - upb_ref2(m, o); - - /* Add each field of the oneof directly to the msgdef. */ - for (upb_oneof_begin(&it, o); !upb_oneof_done(&it); upb_oneof_next(&it)) { - upb_fielddef *f = upb_oneof_iter_field(&it); - add_field(m, f, NULL); - } - - if (ref_donor) upb_oneofdef_unref(o, ref_donor); - - return true; -} -#endif - const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i) { upb_value val; return upb_inttable_lookup32(&m->itof, i, &val) ? @@ -949,7 +818,9 @@ void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter) { /* upb_oneofdef ***************************************************************/ -const char *upb_oneofdef_name(const upb_oneofdef *o) { return o->name; } +const char *upb_oneofdef_name(const upb_oneofdef *o) { + return shortdefname(o->full_name); +} const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o) { return o->parent; @@ -963,79 +834,6 @@ uint32_t upb_oneofdef_index(const upb_oneofdef *o) { return o->index; } -#if 0 -bool upb_oneofdef_addfield(upb_oneofdef *o, upb_fielddef *f, - const void *ref_donor, - upb_status *s) { - /* This method is idempotent. Check if |f| is already part of this oneofdef - * and return immediately if so. */ - if (upb_fielddef_containingoneof(f) == o) { - return true; - } - - /* The field must have an OPTIONAL label. */ - if (upb_fielddef_label(f) != UPB_LABEL_OPTIONAL) { - upb_status_seterrmsg(s, "fields in oneof must have OPTIONAL label"); - return false; - } - - /* Check that no field with this name or number exists already in the oneof. - * Also check that the field is not already part of a oneof. */ - if (upb_fielddef_name(f) == NULL || upb_fielddef_number(f) == 0) { - upb_status_seterrmsg(s, "field name or number were not set"); - return false; - } else if (upb_oneofdef_itof(o, upb_fielddef_number(f)) || - upb_oneofdef_ntofz(o, upb_fielddef_name(f))) { - upb_status_seterrmsg(s, "duplicate field name or number"); - return false; - } else if (upb_fielddef_containingoneof(f) != NULL) { - upb_status_seterrmsg(s, "fielddef already belongs to a oneof"); - return false; - } - - /* We allow adding a field to the oneof either if the field is not part of a - * msgdef, or if it is and we are also part of the same msgdef. */ - if (o->parent == NULL) { - /* If we're not in a msgdef, the field cannot be either. Otherwise we would - * need to magically add this oneof to a msgdef to remain consistent, which - * is surprising behavior. */ - if (upb_fielddef_containingtype(f) != NULL) { - upb_status_seterrmsg(s, "fielddef already belongs to a message, but " - "oneof does not"); - return false; - } - } else { - /* If we're in a msgdef, the user can add fields that either aren't in any - * msgdef (in which case they're added to our msgdef) or already a part of - * our msgdef. */ - if (upb_fielddef_containingtype(f) != NULL && - upb_fielddef_containingtype(f) != o->parent) { - upb_status_seterrmsg(s, "fielddef belongs to a different message " - "than oneof"); - return false; - } - } - - /* Commit phase. First add the field to our parent msgdef, if any, because - * that may fail; then add the field to our own tables. */ - - if (o->parent != NULL && upb_fielddef_containingtype(f) == NULL) { - if (!upb_msgdef_addfield((upb_msgdef*)o->parent, f, NULL, s)) { - return false; - } - } - - f->oneof = o; - upb_inttable_insert(&o->itof, upb_fielddef_number(f), upb_value_ptr(f)); - upb_strtable_insert(&o->ntof, upb_fielddef_name(f), upb_value_ptr(f)); - upb_ref2(f, o); - upb_ref2(o, f); - if (ref_donor) upb_fielddef_unref(f, ref_donor); - - return true; -} -#endif - const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o, const char *name, size_t length) { upb_value val; @@ -1092,27 +890,27 @@ upb_syntax_t upb_filedef_syntax(const upb_filedef *f) { } int upb_filedef_msgcount(const upb_filedef *f) { - return f->msgcount; + return f->msg_count; } int upb_filedef_depcount(const upb_filedef *f) { - return f->depcount; + return f->dep_count; } int upb_filedef_enumcount(const upb_filedef *f) { - return f->enumcount; + return f->enum_count; } const upb_filedef *upb_filedef_dep(const upb_filedef *f, int i) { - return i < 0 || i >= f->depcount ? NULL : f->deps[i]; + return i < 0 || i >= f->dep_count ? NULL : f->deps[i]; } const upb_msgdef *upb_filedef_msg(const upb_filedef *f, int i) { - return i < 0 || i >= f->msgcount ? NULL : f->msgs[i]; + return i < 0 || i >= f->msg_count ? NULL : &f->msgs[i]; } const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i) { - return i < 0 || i >= f->enumcount ? NULL : f->enums[i]; + return i < 0 || i >= f->enum_count ? NULL : &f->enums[i]; } void upb_symtab_free(upb_symtab *s) { @@ -1159,68 +957,6 @@ const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym) { unpack_def(v, UPB_DEFTYPE_ENUM) : NULL; } -#if 0 -/* Given a symbol and the base symbol inside which it is defined, find the - * symbol's definition in t. */ -static upb_def *upb_resolvename(const upb_strtable *t, - const char *base, const char *sym) { - if(strlen(sym) == 0) return NULL; - if(sym[0] == '.') { - /* Symbols starting with '.' are absolute, so we do a single lookup. - * Slice to omit the leading '.' */ - upb_value v; - return upb_strtable_lookup(t, sym + 1, &v) ? upb_value_getptr(v) : NULL; - } else { - /* Remove components from base until we find an entry or run out. - * TODO: This branch is totally broken, but currently not used. */ - (void)base; - UPB_ASSERT(false); - return NULL; - } -} - -const upb_def *upb_symtab_resolve(const upb_symtab *s, const char *base, - const char *sym) { - upb_def *ret = upb_resolvename(&s->syms, base, sym); - return ret; -} - - /* Now using the table, resolve symbolic references for subdefs. */ - upb_strtable_begin(&iter, &addtab); - for (; !upb_strtable_done(&iter); upb_strtable_next(&iter)) { - const char *base; - upb_def *def = upb_value_getptr(upb_strtable_iter_value(&iter)); - upb_msgdef *m = upb_dyncast_msgdef_mutable(def); - upb_msg_field_iter j; - - if (!m) continue; - /* Type names are resolved relative to the message in which they appear. */ - base = upb_msgdef_fullname(m); - - for(upb_msg_field_begin(&j, m); - !upb_msg_field_done(&j); - upb_msg_field_next(&j)) { - upb_fielddef *f = upb_msg_iter_field(&j); - const char *name = upb_fielddef_subdefname(f); - if (name && !upb_fielddef_subdef(f)) { - /* Try the lookup in the current set of to-be-added defs first. If not - * there, try existing defs. */ - upb_def *subdef = upb_resolvename(&addtab, base, name); - if (subdef == NULL) { - subdef = upb_resolvename(&s->syms, base, name); - } - if (subdef == NULL) { - upb_status_seterrf( - status, "couldn't resolve name '%s' in message '%s'", name, base); - goto err; - } else if (!upb_fielddef_setsubdef(f, subdef, status)) { - goto err; - } - } - } - } -#endif - /* Code to build defs from descriptor protos. *********************************/ @@ -1234,7 +970,7 @@ const upb_def *upb_symtab_resolve(const upb_symtab *s, const char *base, typedef struct { const upb_symtab *symtab; - const upb_filedef *file; /* File we are building. */ + upb_filedef *file; /* File we are building. */ upb_alloc *alloc; /* Allocate defs here. */ upb_alloc *tmp; /* Alloc for addtab and any other tmp data. */ upb_strtable *addtab; /* full_name -> packed def ptr for new defs. */ @@ -1250,7 +986,7 @@ static bool symtab_add(const symtab_addctx *ctx, const char *name, upb_value tmp; if (upb_strtable_lookup(ctx->addtab, name, &tmp) || upb_strtable_lookup(&ctx->symtab->syms, name, &tmp)) { - upb_status_seterrf(ctx->status, "Duplicate symbol '%s'", name); + upb_status_seterrf(ctx->status, "duplicate symbol '%s'", name); return false; } @@ -1258,28 +994,75 @@ static bool symtab_add(const symtab_addctx *ctx, const char *name, return true; } +/* Given a symbol and the base symbol inside which it is defined, find the + * symbol's definition in t. */ +static bool resolvename(const upb_strtable *t, const upb_fielddef *f, + const char *base, upb_stringview sym, + upb_deftype_t type, upb_status *status, + const void **def) { + if(sym.size == 0) return NULL; + if(sym.data[0] == '.') { + /* Symbols starting with '.' are absolute, so we do a single lookup. + * Slice to omit the leading '.' */ + upb_value v; + if (!upb_strtable_lookup2(t, sym.data + 1, sym.size - 1, &v)) { + return false; + } + + *def = unpack_def(v, type); + + if (!*def) { + upb_status_seterrf(status, + "type mismatch when resolving field %s, name %s", + f->full_name, sym.data); + return false; + } + + return true; + } else { + /* Remove components from base until we find an entry or run out. + * TODO: This branch is totally broken, but currently not used. */ + (void)base; + UPB_ASSERT(false); + return false; + } +} + +const void *symtab_resolve(const symtab_addctx *ctx, const upb_fielddef *f, + const char *base, upb_stringview sym, + upb_deftype_t type) { + const void *ret; + if (!resolvename(ctx->addtab, f, base, sym, type, ctx->status, &ret) && + !resolvename(&ctx->symtab->syms, f, base, sym, type, ctx->status, &ret)) { + if (upb_ok(ctx->status)) { + upb_status_seterrf(ctx->status, "couldn't resolve name '%s'", sym.data); + } + return false; + } + return ret; +} + static bool create_oneofdef( const symtab_addctx *ctx, upb_msgdef *m, const google_protobuf_OneofDescriptorProto *oneof_proto) { - upb_oneofdef *o = upb_malloc(ctx->alloc, sizeof(*o)); + upb_oneofdef *o; upb_stringview name = google_protobuf_OneofDescriptorProto_name(oneof_proto); - upb_value o_ptr = upb_value_ptr(o); - CHK_OOM(o); - CHK_OOM(upb_inttable_init2(&o->itof, UPB_CTYPE_PTR, ctx->alloc)); - CHK_OOM(upb_strtable_init2(&o->ntof, UPB_CTYPE_PTR, ctx->alloc)); - - o->index = upb_strtable_count(&m->ntof); - o->name = upb_strdup2(name.data, name.size, ctx->alloc); + o = (upb_oneofdef*)&m->oneofs[m->oneof_count++]; o->parent = m; - - CHK_OOM(upb_strtable_insert3(&m->ntof, name.data, name.size, o_ptr, + o->full_name = makefullname(m->full_name, name); + CHK_OOM(symtab_add(ctx, o->full_name, pack_def(o, UPB_DEFTYPE_ONEOF))); + CHK_OOM(upb_strtable_insert3(&m->ntof, name.data, name.size, upb_value_ptr(o), ctx->alloc)); + CHK_OOM(upb_inttable_init2(&o->itof, UPB_CTYPE_PTR, ctx->alloc)); + CHK_OOM(upb_strtable_init2(&o->ntof, UPB_CTYPE_PTR, ctx->alloc)); + return true; } -static bool parse_default(const char *str, size_t len, upb_fielddef *f) { +static bool parse_default(const symtab_addctx *ctx, const char *str, size_t len, + upb_fielddef *f) { char *end; switch (upb_fielddef_type(f)) { case UPB_TYPE_INT32: { @@ -1332,11 +1115,11 @@ static bool parse_default(const char *str, size_t len, upb_fielddef *f) { } } case UPB_TYPE_STRING: - f->defaultval.str = newstr(str, len); + f->defaultval.str = newstr(ctx->alloc, str, len); break; case UPB_TYPE_BYTES: /* XXX: need to interpret the C-escaped value. */ - f->defaultval.str = newstr(str, len); + f->defaultval.str = newstr(ctx->alloc, str, len); case UPB_TYPE_MESSAGE: /* Should not have a default value. */ return false; @@ -1348,17 +1131,48 @@ static bool create_fielddef( const symtab_addctx *ctx, const char *prefix, upb_msgdef *m, const google_protobuf_FieldDescriptorProto *field_proto) { upb_alloc *alloc = ctx->alloc; - upb_fielddef *f = upb_malloc(ctx->alloc, sizeof(*f)); + upb_fielddef *f; const google_protobuf_FieldOptions *options; upb_stringview defaultval, name; upb_value packed_v = pack_def(f, UPB_DEFTYPE_FIELD); upb_value v = upb_value_constptr(f); + const char *full_name; const char *shortname; - - CHK_OOM(f); - memset(f, 0, sizeof(*f)); + int oneof_index; name = google_protobuf_FieldDescriptorProto_name(field_proto); + CHK(upb_isident(name, false, ctx->status)); + full_name = makefullname(prefix, name); + shortname = shortdefname(full_name); + + if (m) { + /* direct message field. */ + f = (upb_fielddef*)&m->fields[m->field_count++]; + f->msgdef = m; + f->is_extension_ = false; + + if (!upb_strtable_insert3(&m->ntof, name.data, name.size, packed_v, alloc)) { + upb_status_seterrf(ctx->status, "duplicate field name (%s)", shortname); + return false; + } + + if (!upb_inttable_insert2(&m->itof, f->number_, v, alloc)) { + upb_status_seterrf(ctx->status, "duplicate field number (%u)", f->number_); + return false; + } + } else { + /* extension field. */ + f = (upb_fielddef*)&ctx->file->exts[ctx->file->ext_count]; + f->is_extension_ = true; + CHK_OOM(symtab_add(ctx, full_name, pack_def(f, UPB_DEFTYPE_FIELD))); + } + + f->full_name = full_name; + f->file = ctx->file; + f->type_ = (int)google_protobuf_FieldDescriptorProto_type(field_proto); + f->label_ = (int)google_protobuf_FieldDescriptorProto_label(field_proto); + f->number_ = google_protobuf_FieldDescriptorProto_number(field_proto); + f->oneof = NULL; /* TODO(haberman): use hazzers. */ if (name.size == 0 || f->number_ == 0) { @@ -1366,27 +1180,42 @@ static bool create_fielddef( return false; } - CHK(upb_isident(name, false, ctx->status)); + /* We can't resolve the subdef or (in the case of extensions) the containing + * message yet, because it may not have been defined yet. We stash a pointer + * to the field_proto until later when we can properly resolve it. */ + f->sub.unresolved = field_proto; - f->full_name = makefullname(prefix, name); - f->type_ = (int)google_protobuf_FieldDescriptorProto_type(field_proto); - f->label_ = (int)google_protobuf_FieldDescriptorProto_label(field_proto); - f->number_ = google_protobuf_FieldDescriptorProto_number(field_proto); - shortname = shortdefname(f->full_name); + if (m) { + /* XXX: need hazzers. */ + oneof_index = google_protobuf_FieldDescriptorProto_oneof_index(field_proto); + if (oneof_index) { + if (upb_fielddef_label(f) != UPB_LABEL_OPTIONAL) { + upb_status_seterrf(ctx->status, + "fields in oneof must have OPTIONAL label (%s)", + f->full_name); + return false; + } + + if (oneof_index >= m->oneof_count) { + upb_status_seterrf(ctx->status, "oneof_index out of range (%s)", + f->full_name); + return false; + } + f->oneof = &m->oneofs[oneof_index]; + } else { + f->oneof = NULL; + } + } if (f->number_ == 0 || f->number_ > UPB_MAX_FIELDNUMBER) { upb_status_seterrf(ctx->status, "invalid field number (%u)", f->number_); return false; } -#if 0 - //f->oneof - //f->sub -#endif defaultval = google_protobuf_FieldDescriptorProto_default_value(field_proto); if (defaultval.data) { - CHK(parse_default(defaultval.data, defaultval.size, f)); + CHK(parse_default(ctx, defaultval.data, defaultval.size, f)); } options = google_protobuf_FieldDescriptorProto_options(field_proto); @@ -1396,32 +1225,13 @@ static bool create_fielddef( f->packed_ = google_protobuf_FieldOptions_packed(options); } - if (m) { - /* direct message field. */ - if (!upb_strtable_insert3(&m->ntof, name.data, name.size, packed_v, alloc)) { - upb_status_seterrf(ctx->status, "duplicate name (%s)", shortname); - return false; - } - - if (!upb_inttable_insert2(&m->itof, f->number_, v, alloc)) { - upb_status_seterrf(ctx->status, "duplicate field number (%u)", f->number_); - return false; - } - - f->msgdef = m; - f->is_extension_ = false; - } else { - /* extension field. */ - f->is_extension_ = true; - } - return true; } static bool create_enumdef( const symtab_addctx *ctx, const char *prefix, const google_protobuf_EnumDescriptorProto *enum_proto) { - upb_enumdef *e = upb_malloc(ctx->alloc, sizeof(*e)); + upb_enumdef *e; const upb_array *arr; upb_stringview name; size_t i; @@ -1429,13 +1239,15 @@ static bool create_enumdef( name = google_protobuf_EnumDescriptorProto_name(enum_proto); CHK(upb_isident(name, false, ctx->status)); + e = (upb_enumdef*)&ctx->file->enums[ctx->file->enum_count++]; e->full_name = makefullname(prefix, name); - e->defaultval = 0; + CHK_OOM(symtab_add(ctx, e->full_name, pack_def(e, UPB_DEFTYPE_ENUM))); - CHK_OOM(e); CHK_OOM(upb_strtable_init2(&e->ntoi, UPB_CTYPE_INT32, ctx->alloc)); CHK_OOM(upb_inttable_init2(&e->iton, UPB_CTYPE_CSTR, ctx->alloc)); + e->defaultval = 0; + arr = google_protobuf_EnumDescriptorProto_value(enum_proto); for (i = 0; i < upb_array_size(arr); i++) { @@ -1446,6 +1258,11 @@ static bool create_enumdef( int32_t num = google_protobuf_EnumValueDescriptorProto_number(value); upb_value v = upb_value_int32(num); + if (upb_strtable_lookup(&e->ntoi, name2, NULL)) { + upb_status_seterrf(ctx->status, "duplicate enum label '%s'", name2); + return false; + } + CHK_OOM(name2 && upb_strtable_insert(&e->ntoi, name2, v)); if (!upb_inttable_lookup(&e->iton, num, NULL)) { @@ -1458,21 +1275,23 @@ static bool create_enumdef( static bool create_msgdef(const symtab_addctx *ctx, const char *prefix, const google_protobuf_DescriptorProto *msg_proto) { - upb_msgdef *m = upb_malloc(ctx->alloc, sizeof(upb_msgdef)); + upb_msgdef *m; const google_protobuf_MessageOptions *options; const upb_array *arr; - size_t i; + size_t i, n; upb_stringview name; - CHK_OOM(m); - CHK_OOM(upb_inttable_init2(&m->itof, UPB_CTYPE_PTR, ctx->alloc)); - CHK_OOM(upb_strtable_init2(&m->ntof, UPB_CTYPE_PTR, ctx->alloc)); - name = google_protobuf_DescriptorProto_name(msg_proto); CHK(upb_isident(name, false, ctx->status)); - m->file = ctx->file; + m = (upb_msgdef*)&ctx->file->msgs[ctx->file->msg_count++]; m->full_name = makefullname(prefix, name); + CHK_OOM(symtab_add(ctx, m->full_name, pack_def(m, UPB_DEFTYPE_MSG))); + + CHK_OOM(upb_inttable_init2(&m->itof, UPB_CTYPE_PTR, ctx->alloc)); + CHK_OOM(upb_strtable_init2(&m->ntof, UPB_CTYPE_PTR, ctx->alloc)); + + m->file = ctx->file; m->map_entry = false; options = google_protobuf_DescriptorProto_options(msg_proto); @@ -1482,15 +1301,16 @@ static bool create_msgdef(const symtab_addctx *ctx, const char *prefix, } arr = google_protobuf_DescriptorProto_oneof_decl(msg_proto); - - for (i = 0; i < upb_array_size(arr); i++) { + m->oneof_count = 0; + n = upb_array_size(arr); + m->oneofs = upb_malloc(ctx->alloc, sizeof(*m->oneofs) * n); + for (i = 0; i < n; i++) { const google_protobuf_OneofDescriptorProto *oneof_proto = upb_msgval_getptr(upb_array_get(arr, i)); CHK(create_oneofdef(ctx, m, oneof_proto)); } arr = google_protobuf_DescriptorProto_field(msg_proto); - for (i = 0; i < upb_array_size(arr); i++) { const google_protobuf_FieldDescriptorProto *field_proto = upb_msgval_getptr(upb_array_get(arr, i)); @@ -1499,12 +1319,10 @@ static bool create_msgdef(const symtab_addctx *ctx, const char *prefix, CHK(assign_msg_indices(m, ctx->status)); assign_msg_wellknowntype(m); - symtab_add(ctx, m->full_name, pack_def(m, UPB_DEFTYPE_MSG)); /* This message is built. Now build nested messages and enums. */ arr = google_protobuf_DescriptorProto_enum_type(msg_proto); - for (i = 0; i < upb_array_size(arr); i++) { const google_protobuf_EnumDescriptorProto *enum_proto = upb_msgval_getptr(upb_array_get(arr, i)); @@ -1512,7 +1330,6 @@ static bool create_msgdef(const symtab_addctx *ctx, const char *prefix, } arr = google_protobuf_DescriptorProto_nested_type(msg_proto); - for (i = 0; i < upb_array_size(arr); i++) { const google_protobuf_DescriptorProto *msg_proto2 = upb_msgval_getptr(upb_array_get(arr, i)); @@ -1529,15 +1346,96 @@ static char* strviewdup(const symtab_addctx *ctx, upb_stringview view) { return upb_strdup2(view.data, view.size, ctx->alloc); } +typedef struct { + int msg_count; + int enum_count; + int ext_count; +} decl_counts; + +static void count_types_in_msg( + const google_protobuf_DescriptorProto *msg_proto, + decl_counts *counts) { + const upb_array *arr; + size_t i; + + arr = google_protobuf_DescriptorProto_nested_type(msg_proto); + for (i = 0; i < upb_array_size(arr); i++) { + count_types_in_msg(upb_msgval_getptr(upb_array_get(arr, i)), counts); + } + + arr = google_protobuf_DescriptorProto_enum_type(msg_proto); + counts->enum_count += upb_array_size(arr); + arr = google_protobuf_DescriptorProto_extension(msg_proto); + counts->ext_count += upb_array_size(arr); +} + +static void count_types_in_file( + const google_protobuf_FileDescriptorProto *file_proto, + decl_counts *counts) { + const upb_array *arr; + size_t i; + + arr = google_protobuf_FileDescriptorProto_message_type(file_proto); + for (i = 0; i < upb_array_size(arr); i++) { + count_types_in_msg(upb_msgval_getptr(upb_array_get(arr, i)), counts); + } + + arr = google_protobuf_FileDescriptorProto_enum_type(file_proto); + counts->enum_count += upb_array_size(arr); + arr = google_protobuf_FileDescriptorProto_extension(file_proto); + counts->ext_count += upb_array_size(arr); +} + +static bool resolve_fielddef(const symtab_addctx *ctx, const char *prefix, + upb_fielddef *f) { + upb_stringview name; + if (f->is_extension_) { + name = google_protobuf_FieldDescriptorProto_extendee(f->sub.unresolved); + f->msgdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_MSG); + CHK(f->msgdef); + } + + name = google_protobuf_FieldDescriptorProto_type_name(f->sub.unresolved); + if (upb_fielddef_issubmsg(f)) { + f->sub.msgdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_MSG); + CHK(f->sub.msgdef); + } else if (f->type_ == UPB_DESCRIPTOR_TYPE_ENUM) { + f->sub.enumdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_ENUM); + CHK(f->sub.enumdef); + + if (!upb_enumdef_iton(f->sub.enumdef, f->defaultval.sint)) { + upb_status_seterrf(ctx->status, + "enum field %s has default (%d) not in the enum", + f->full_name, f->defaultval.sint); + return false; + } + } + + return true; +} + static bool build_filedef( const symtab_addctx *ctx, upb_filedef *file, const google_protobuf_FileDescriptorProto *file_proto) { const google_protobuf_FileOptions *file_options_proto; - upb_strtable_iter iter; + upb_alloc *alloc = ctx->alloc; const upb_array *arr; size_t i, n; + int j; + decl_counts counts; upb_stringview syntax, package; + count_types_in_file(file_proto, &counts); + + file->msgs = upb_malloc(alloc, sizeof(*file->msgs) * counts.msg_count); + file->enums = upb_malloc(alloc, sizeof(*file->enums) * counts.enum_count); + file->exts = upb_malloc(alloc, sizeof(*file->exts) * counts.ext_count); + + /* We increment these as defs are added. */ + file->msg_count = 0; + file->enum_count = 0; + file->ext_count = 0; + package = google_protobuf_FileDescriptorProto_package(file_proto); CHK(upb_isident(package, true, ctx->status)); @@ -1559,9 +1457,7 @@ static bool build_filedef( } /* Read options. */ - file_options_proto = google_protobuf_FileDescriptorProto_options(file_proto); - if (file_options_proto) { file->phpprefix = strviewdup( ctx, google_protobuf_FileOptions_php_class_prefix(file_options_proto)); @@ -1569,11 +1465,10 @@ static bool build_filedef( ctx, google_protobuf_FileOptions_php_namespace(file_options_proto)); } - /* Resolve dependencies. */ - + /* Verify dependencies. */ arr = google_protobuf_FileDescriptorProto_dependency(file_proto); n = upb_array_size(arr); - file->deps = upb_malloc(ctx->alloc, sizeof(*file->deps) * n) ; + file->deps = upb_malloc(alloc, sizeof(*file->deps) * n) ; CHK_OOM(file->deps); for (i = 0; i < n; i++) { upb_stringview dep_name = upb_msgval_getstr(upb_array_get(arr, i)); @@ -1589,7 +1484,6 @@ static bool build_filedef( } /* Create messages. */ - arr = google_protobuf_FileDescriptorProto_message_type(file_proto); for (i = 0; i < upb_array_size(arr); i++) { const google_protobuf_DescriptorProto *msg_proto = @@ -1598,7 +1492,6 @@ static bool build_filedef( } /* Create enums. */ - arr = google_protobuf_FileDescriptorProto_enum_type(file_proto); for (i = 0; i < upb_array_size(arr); i++) { const google_protobuf_EnumDescriptorProto *enum_proto = @@ -1606,9 +1499,27 @@ static bool build_filedef( CHK(create_enumdef(ctx, file->package, enum_proto)); } + /* Create extensions. */ + arr = google_protobuf_FileDescriptorProto_extension(file_proto); + n = upb_array_size(arr); + file->exts = upb_malloc(alloc, sizeof(*file->exts) * n); + CHK_OOM(file->exts); + for (i = 0; i < n; i++) { + const google_protobuf_FieldDescriptorProto *field_proto = + upb_msgval_getptr(upb_array_get(arr, i)); + CHK(create_fielddef(ctx, file->package, NULL, field_proto)); + } + /* Now that all names are in the table, resolve references. */ - upb_strtable_begin(&iter, ctx->addtab); - for (; !upb_strtable_done(&iter); upb_strtable_next(&iter)) { + for (i = 0; i < file->ext_count; i++) { + CHK(resolve_fielddef(ctx, file->package, (upb_fielddef*)&file->exts[i])); + } + + for (i = 0; i < file->msg_count; i++) { + const upb_msgdef *m = &file->msgs[i]; + for (j = 0; i < m->field_count; i++) { + CHK(resolve_fielddef(ctx, m->full_name, (upb_fielddef*)&m->fields[j])); + } } return true; diff --git a/upb/def.h b/upb/def.h index 4a7342e..78ae9b3 100644 --- a/upb/def.h +++ b/upb/def.h @@ -54,9 +54,7 @@ UPB_DECLARE_TYPE(upb::SymbolTable, upb_symtab) /* A upb_fielddef describes a single field in a message. It is most often * found as a part of a upb_msgdef, but can also stand alone to represent - * an extension. - * - * Its base class is upb::Def (use upb::upcast() to convert). */ + * an extension. */ class upb::FieldDef { public: typedef upb_fieldtype_t Type; @@ -232,9 +230,7 @@ typedef upb_strtable_iter upb_msg_oneof_iter; #ifdef __cplusplus -/* Structure that describes a single .proto message type. - * - * Its base class is upb::Def (use upb::upcast() to convert). */ +/* Structure that describes a single .proto message type. */ class upb::MessageDef { public: const char* full_name() const; @@ -439,8 +435,6 @@ typedef upb_strtable_iter upb_enum_iter; #ifdef __cplusplus -/* Class that represents an enum. Its base class is upb::Def (convert with - * upb::upcast()). */ class upb::EnumDef { public: const char* full_name() const; @@ -642,12 +636,6 @@ class upb::FileDef { /* Syntax for the file. Defaults to proto2. */ upb_syntax_t syntax() const; - /* Get the list of defs from the file. These are returned in the order that - * they were added to the FileDef. */ - int def_count() const; - const Def* def(int index) const; - Def* def(int index); - /* Get the list of dependencies from the file. These are returned in the * order that they were added to the FileDef. */ int dependency_count() const; @@ -688,7 +676,6 @@ class upb::SymbolTable { /* Finds an entry in the symbol table with this exact name. If not found, * returns NULL. */ - const Def* Lookup(const char *sym) const; const MessageDef* LookupMessage(const char *sym) const; const EnumDef* LookupEnum(const char *sym) const; @@ -731,9 +718,6 @@ inline SymbolTable* SymbolTable::New() { inline void SymbolTable::Free(SymbolTable* s) { upb_symtab_free(s); } -inline const Def* SymbolTable::Lookup(const char *sym) const { - return upb_symtab_lookup(this, sym); -} inline const MessageDef *SymbolTable::LookupMessage(const char *sym) const { return upb_symtab_lookupmsg(this, sym); } @@ -750,10 +734,6 @@ UPB_INLINE const char* upb_safecstr(const std::string& str) { /* Inline C++ wrappers. */ namespace upb { -inline Def::Type Def::def_type() const { return upb_def_type(this); } -inline const char* Def::full_name() const { return upb_def_fullname(this); } -inline const char* Def::name() const { return upb_def_name(this); } - inline const char* FieldDef::full_name() const { return upb_fielddef_fullname(this); } @@ -1017,12 +997,6 @@ inline const char* FileDef::phpnamespace() const { inline int FileDef::def_count() const { return upb_filedef_defcount(this); } -inline const Def* FileDef::def(int index) const { - return upb_filedef_def(this, index); -} -inline Def* FileDef::def(int index) { - return const_cast(upb_filedef_def(this, index)); -} inline int FileDef::dependency_count() const { return upb_filedef_depcount(this); } -- cgit v1.2.3 From 7ff8413a0317566d67b5b4abcb6ee2b72e9d465b Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Thu, 13 Dec 2018 05:40:33 -0800 Subject: Ported some tests. --- BUILD | 26 ++- CMakeLists.txt | 10 +- google/protobuf/descriptor.pb | Bin 0 -> 7493 bytes tests/pb/test_decoder.cc | 102 +--------- tests/pb/test_decoder.proto | 86 ++++++++ tests/pb/test_encoder.cc | 28 ++- tests/test_def.c | 453 ------------------------------------------ upb/def.c | 15 +- upb/def.h | 16 +- 9 files changed, 145 insertions(+), 591 deletions(-) create mode 100644 google/protobuf/descriptor.pb create mode 100644 tests/pb/test_decoder.proto delete mode 100644 tests/test_def.c (limited to 'upb') diff --git a/BUILD b/BUILD index df05e06..4d4168f 100644 --- a/BUILD +++ b/BUILD @@ -157,15 +157,6 @@ cc_test( ], ) -cc_test( - name = "test_def", - srcs = ["tests/test_def.c"], - deps = [ - ":upb_pb", - ":upb_test", - ], -) - cc_test( name = "test_handlers", srcs = ["tests/test_handlers.c"], @@ -187,7 +178,7 @@ cc_test( cc_test( name = "test_encoder", srcs = ["tests/pb/test_encoder.cc"], - data = ["upb/descriptor/descriptor.pb"], + data = ["google/protobuf/descriptor.pb"], deps = [ ":upb_cc_bindings", ":upb_pb", @@ -413,6 +404,20 @@ py_binary( srcs = ["tools/make_cmakelists.py"], ) +proto_library( + name = "descriptor_proto", + srcs = [ + "google/protobuf/descriptor.proto", + ], +) + +genrule( + name = "copy_upb_descriptor_pb", + srcs = [":descriptor_proto"], + outs = ["generated/google/protobuf/descriptor.pb"], + cmd = "cp $< $@", +) + genrule( name = "gen_cmakelists", outs = ["generated/CMakeLists.txt"], @@ -477,6 +482,7 @@ generated_file_staleness_test( name = "test_generated_files", outs = [ "CMakeLists.txt", + "google/protobuf/descriptor.pb", "google/protobuf/descriptor.upb.c", "google/protobuf/descriptor.upb.h", "tests/json/test.proto.pb", diff --git a/CMakeLists.txt b/CMakeLists.txt index e3d30c2..54082a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -120,12 +120,6 @@ add_test(NAME test_varint COMMAND test_varint) target_link_libraries(test_varint upb_pb upb_test) -add_executable(test_def - tests/test_def.c) -add_test(NAME test_def COMMAND test_def) -target_link_libraries(test_def - upb_pb - upb_test) add_executable(test_handlers tests/test_handlers.c) add_test(NAME test_handlers COMMAND test_handlers) @@ -144,8 +138,8 @@ add_test(NAME test_encoder COMMAND test_encoder) add_custom_command( TARGET test_encoder POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy - ${CMAKE_SOURCE_DIR}/upb/descriptor/descriptor.pb - ${CMAKE_CURRENT_BINARY_DIR}/upb/descriptor/descriptor.pb) + ${CMAKE_SOURCE_DIR}/google/protobuf/descriptor.pb + ${CMAKE_CURRENT_BINARY_DIR}/google/protobuf/descriptor.pb) target_link_libraries(test_encoder upb_cc_bindings upb_pb diff --git a/google/protobuf/descriptor.pb b/google/protobuf/descriptor.pb new file mode 100644 index 0000000..0a74c99 Binary files /dev/null and b/google/protobuf/descriptor.pb differ diff --git a/tests/pb/test_decoder.cc b/tests/pb/test_decoder.cc index 49544b2..a931779 100644 --- a/tests/pb/test_decoder.cc +++ b/tests/pb/test_decoder.cc @@ -399,107 +399,8 @@ void reg_str(upb_handlers *h, uint32_t num) { ASSERT(h->SetStringHandler(f, UpbBind(value_string, new uint32_t(num)))); } -void AddField(upb_descriptortype_t descriptor_type, const std::string& name, - uint32_t fn, bool repeated, upb::MessageDef* md) { - // TODO: Fluent interface? ie. - // ASSERT(md->AddField(upb::BuildFieldDef() - // .SetName("f_message") - // .SetNumber(UPB_DESCRIPTOR_TYPE_MESSAGE) - // .SetDescriptorType(UPB_DESCRIPTOR_TYPE_MESSAGE) - // .SetMessageSubdef(md.get()))); - upb::reffed_ptr f = upb::FieldDef::New(); - ASSERT(f->set_name(name, NULL)); - ASSERT(f->set_number(fn, NULL)); - f->set_label(repeated ? UPB_LABEL_REPEATED : UPB_LABEL_OPTIONAL); - f->set_descriptor_type(descriptor_type); - ASSERT(md->AddField(f.get(), NULL)); -} - -void AddFieldsForType(upb_descriptortype_t descriptor_type, - const char* basename, upb::MessageDef* md) { - const upb_descriptortype_t t = descriptor_type; - AddField(t, std::string("f_") + basename, t, false, md); - AddField(t, std::string("r_") + basename, rep_fn(t), true, md); -} - -upb::reffed_ptr NewMessageDef() { - upb::reffed_ptr md = upb::MessageDef::New(); - - md->set_full_name("DecoderTest", NULL); - - AddFieldsForType(UPB_DESCRIPTOR_TYPE_DOUBLE, "double", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_FLOAT, "float", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_INT64, "int64", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_UINT64, "uint64", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_INT32, "int32", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_FIXED64, "fixed64", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_FIXED32, "fixed32", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_BOOL, "bool", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_STRING, "string", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_BYTES, "bytes", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_UINT32, "uint32", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_SFIXED32, "sfixed32", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_SFIXED64, "sfixed64", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_SINT32, "sint32", md.get()); - AddFieldsForType(UPB_DESCRIPTOR_TYPE_SINT64, "sint64", md.get()); - - AddField(UPB_DESCRIPTOR_TYPE_STRING, "nop_field", 40, false, md.get()); - - upb::reffed_ptr f = upb::FieldDef::New(); - ASSERT(f->set_name("f_message", NULL)); - ASSERT(f->set_number(UPB_DESCRIPTOR_TYPE_MESSAGE, NULL)); - f->set_descriptor_type(UPB_DESCRIPTOR_TYPE_MESSAGE); - ASSERT(f->set_message_subdef(md.get(), NULL)); - ASSERT(md->AddField(f.get(), NULL)); - - f = upb::FieldDef::New(); - ASSERT(f->set_name("r_message", NULL)); - ASSERT(f->set_number(rep_fn(UPB_DESCRIPTOR_TYPE_MESSAGE), NULL)); - f->set_label(UPB_LABEL_REPEATED); - f->set_descriptor_type(UPB_DESCRIPTOR_TYPE_MESSAGE); - ASSERT(f->set_message_subdef(md.get(), NULL)); - ASSERT(md->AddField(f.get(), NULL)); - - f = upb::FieldDef::New(); - ASSERT(f->set_name("f_group", NULL)); - ASSERT(f->set_number(UPB_DESCRIPTOR_TYPE_GROUP, NULL)); - f->set_descriptor_type(UPB_DESCRIPTOR_TYPE_GROUP); - ASSERT(f->set_message_subdef(md.get(), NULL)); - ASSERT(md->AddField(f.get(), NULL)); - - f = upb::FieldDef::New(); - ASSERT(f->set_name("r_group", NULL)); - ASSERT(f->set_number(rep_fn(UPB_DESCRIPTOR_TYPE_GROUP), NULL)); - f->set_label(UPB_LABEL_REPEATED); - f->set_descriptor_type(UPB_DESCRIPTOR_TYPE_GROUP); - ASSERT(f->set_message_subdef(md.get(), NULL)); - ASSERT(md->AddField(f.get(), NULL)); - - upb::reffed_ptr e = upb::EnumDef::New(); - ASSERT(e->AddValue("FOO", 1, NULL)); - ASSERT(e->Freeze(NULL)); - - f = upb::FieldDef::New(); - ASSERT(f->set_name("f_enum", NULL)); - ASSERT(f->set_number(UPB_DESCRIPTOR_TYPE_ENUM, NULL)); - f->set_descriptor_type(UPB_DESCRIPTOR_TYPE_ENUM); - ASSERT(f->set_enum_subdef(e.get(), NULL)); - ASSERT(md->AddField(f.get(), NULL)); - - f = upb::FieldDef::New(); - ASSERT(f->set_name("r_enum", NULL)); - ASSERT(f->set_number(rep_fn(UPB_DESCRIPTOR_TYPE_ENUM), NULL)); - f->set_label(UPB_LABEL_REPEATED); - f->set_descriptor_type(UPB_DESCRIPTOR_TYPE_ENUM); - ASSERT(f->set_enum_subdef(e.get(), NULL)); - ASSERT(md->AddField(f.get(), NULL)); - - ASSERT(md->Freeze(NULL)); - - return md; -} - upb::reffed_ptr NewHandlers(TestMode mode) { + upb::reffed_ptr h(upb::Handlers::New(NewMessageDef().get())); if (mode == ALL_HANDLERS) { @@ -1277,6 +1178,7 @@ upb::reffed_ptr method = void run_tests(bool use_jit) { upb::reffed_ptr method; upb::reffed_ptr handlers; + upb::SymbolTable symtab; handlers = NewHandlers(test_mode); global_handlers = handlers.get(); diff --git a/tests/pb/test_decoder.proto b/tests/pb/test_decoder.proto new file mode 100644 index 0000000..8197dea --- /dev/null +++ b/tests/pb/test_decoder.proto @@ -0,0 +1,86 @@ + +syntax = "proto2"; + +enum TestEnum { + FOO = 1; +} + +message DecoderTest { + optional double f_double = 1; + optional float f_float = 2; + optional int64 f_int64 = 3; + optional uint64 f_uint64 = 4; + optional int32 f_int32 = 5; + optional fixed64 f_fixed64 = 6; + optional fixed32 f_fixed32 = 7; + optional bool f_bool = 8; + optional string f_string = 9; + optional DecoderTest f_message = 11; + optional bytes f_bytes = 12; + optional uint32 f_uint32 = 13; + optional TestEnum f_enum = 14; + optional sfixed32 f_sfixed32 = 15; + optional sfixed64 f_sfixed64 = 16; + optional sint32 f_sint32 = 17; + optional sint64 f_sint64 = 18; + + optional string nop_field = 40; + + repeated double r_double = 536869912; + repeated float r_float = 536869913; + repeated int64 r_int64 = 536869914; + repeated uint64 r_uint64 = 536869915; + repeated int32 r_int32 = 536869916; + repeated fixed64 r_fixed64 = 536869917; + repeated fixed32 r_fixed32 = 536869918; + repeated bool r_bool = 536869919; + repeated string r_string = 536869920; + repeated DecoderTest r_message = 536869922; + repeated bytes r_bytes = 536869923; + repeated uint32 r_uint32 = 536869924; + repeated TestEnum r_enum = 536869925; + repeated sfixed32 r_sfixed32 = 536869926; + repeated sfixed64 r_sfixed64 = 536869927; + repeated sint32 r_sint32 = 536869928; + repeated sint64 r_sint64 = 536869929; + + optional group F_group = 10 { + optional double f_double = 1; + optional float f_float = 2; + optional int64 f_int64 = 3; + optional uint64 f_uint64 = 4; + optional int32 f_int32 = 5; + optional fixed64 f_fixed64 = 6; + optional fixed32 f_fixed32 = 7; + optional bool f_bool = 8; + optional string f_string = 9; + optional DecoderTest f_message = 11; + optional bytes f_bytes = 12; + optional uint32 f_uint32 = 13; + optional TestEnum f_enum = 14; + optional sfixed32 f_sfixed32 = 15; + optional sfixed64 f_sfixed64 = 16; + optional sint32 f_sint32 = 17; + optional sint64 f_sint64 = 18; + } + + optional group R_group = 536869921 { + optional double f_double = 1; + optional float f_float = 2; + optional int64 f_int64 = 3; + optional uint64 f_uint64 = 4; + optional int32 f_int32 = 5; + optional fixed64 f_fixed64 = 6; + optional fixed32 f_fixed32 = 7; + optional bool f_bool = 8; + optional string f_string = 9; + optional DecoderTest f_message = 11; + optional bytes f_bytes = 12; + optional uint32 f_uint32 = 13; + optional TestEnum f_enum = 14; + optional sfixed32 f_sfixed32 = 15; + optional sfixed64 f_sfixed64 = 16; + optional sint32 f_sint32 = 17; + optional sint64 f_sint64 = 18; + } +} diff --git a/tests/pb/test_encoder.cc b/tests/pb/test_encoder.cc index 6c20e27..78a779f 100644 --- a/tests/pb/test_encoder.cc +++ b/tests/pb/test_encoder.cc @@ -2,10 +2,9 @@ #include "tests/test_util.h" #include "tests/upb_test.h" #include "upb/bindings/stdc++/string.h" -#include "upb/descriptor/descriptor.upbdefs.h" +#include "google/protobuf/descriptor.upb.h" #include "upb/pb/decoder.h" #include "upb/pb/encoder.h" -#include "upb/pb/glue.h" std::string read_string(const char *filename) { size_t len; @@ -18,16 +17,30 @@ std::string read_string(const char *filename) { } void test_pb_roundtrip() { - upb::reffed_ptr md( - upbdefs::google::protobuf::FileDescriptorSet::get()); + std::string input = read_string("google/protobuf/descriptor.pb"); + upb::SymbolTable* symtab = upb::SymbolTable::New(); + upb::Arena arena; + google_protobuf_FileDescriptorSet *set = + google_protobuf_FileDescriptorSet_parsenew( + upb_stringview_make(input.c_str(), input.size()), &arena); + ASSERT(set); + const upb_array *arr = google_protobuf_FileDescriptorSet_file(set); + const google_protobuf_FileDescriptorProto *file_proto = + static_cast( + upb_msgval_getptr(upb_array_get(arr, 0))); + upb::Status status; + bool ok = symtab->AddFile(file_proto, &status); + ASSERT(ok); + const upb::MessageDef *md = + symtab->LookupMessage("google.protobuf.FileDescriptorSet"); + ASSERT(md); upb::reffed_ptr encoder_handlers( - upb::pb::Encoder::NewHandlers(md.get())); + upb::pb::Encoder::NewHandlers(md)); upb::reffed_ptr method( upb::pb::DecoderMethod::New( upb::pb::DecoderMethodOptions(encoder_handlers.get()))); upb::InlinedEnvironment<512> env; - std::string input = read_string("upb/descriptor/descriptor.pb"); std::string output; upb::StringSink string_sink(&output); upb::pb::Encoder* encoder = @@ -35,9 +48,10 @@ void test_pb_roundtrip() { string_sink.input()); upb::pb::Decoder* decoder = upb::pb::Decoder::Create(&env, method.get(), encoder->input()); - bool ok = upb::BufferSource::PutBuffer(input, decoder->input()); + ok = upb::BufferSource::PutBuffer(input, decoder->input()); ASSERT(ok); ASSERT(input == output); + upb::SymbolTable::Free(symtab); } extern "C" { diff --git a/tests/test_def.c b/tests/test_def.c deleted file mode 100644 index d82fddb..0000000 --- a/tests/test_def.c +++ /dev/null @@ -1,453 +0,0 @@ -/* -** Test of defs and symtab. There should be far more tests of edge conditions -** (like attempts to link defs that don't have required properties set). -*/ - -#include "tests/test_util.h" -#include "upb/def.h" -#include "upb/pb/glue.h" -#include "upb_test.h" -#include -#include - -const char *descriptor_file; - -static void test_empty_symtab() { - upb_symtab *s = upb_symtab_new(); - upb_symtab_iter i; - for (upb_symtab_begin(&i, s, UPB_DEF_ANY); !upb_symtab_done(&i); - upb_symtab_next(&i)) { - ASSERT(false); /* Should not get here. */ - } - upb_symtab_free(s); -} - -static void test_noreftracking() { - /* Reftracking is not required; clients can pass UPB_UNTRACKED_REF for owner. */ - upb_msgdef *md = upb_msgdef_new(UPB_UNTRACKED_REF); - upb_msgdef_ref(md, UPB_UNTRACKED_REF); - - /* Clients can mix tracked and untracked refs. */ - upb_msgdef_ref(md, &md); - - upb_msgdef_unref(md, UPB_UNTRACKED_REF); - upb_msgdef_unref(md, UPB_UNTRACKED_REF); - - /* Call some random function on the messagedef to test that it is alive. */ - ASSERT(!upb_msgdef_isfrozen(md)); - - upb_msgdef_unref(md, &md); -} - -static upb_symtab *load_test_proto() { - upb_symtab *s = upb_symtab_new(); - upb_status status = UPB_STATUS_INIT; - size_t len; - char *data = upb_readfile(descriptor_file, &len); - upb_filedef **files, **files_ptr; - ASSERT(s); - ASSERT(data); - files = upb_loaddescriptor(data, len, &files, &status); - ASSERT(files); - free(data); - - files_ptr = files; - while (*files_ptr) { - bool ok; - ASSERT(!upb_filedef_isfrozen(*files_ptr)); - ok = upb_symtab_addfile(s, *files_ptr, &status); - ASSERT(ok); - ASSERT(upb_filedef_isfrozen(*files_ptr)); - upb_filedef_unref(*files_ptr, &files); - files_ptr++; - } - - upb_gfree(files); - - return s; -} - -static void test_cycles() { - bool ok; - upb_symtab *s = load_test_proto(); - const upb_msgdef *m; - const upb_fielddef *f; - const upb_def *def; - const upb_def *def2; - - /* Test cycle detection by making a cyclic def's main refcount go to zero - * and then be incremented to one again. */ - def = upb_symtab_lookup(s, "A"); - upb_def_ref(def, &def); - ASSERT(def); - ASSERT(upb_def_isfrozen(def)); - upb_symtab_free(s); - - /* Message A has only one subfield: "optional B b = 1". */ - m = upb_downcast_msgdef(def); - f = upb_msgdef_itof(m, 1); - ASSERT(f); - ASSERT(upb_fielddef_hassubdef(f)); - ASSERT(upb_msgdef_ntofz(m, "b") == f); - ASSERT(upb_msgdef_ntof(m, "b", 1) == f); - def2 = upb_fielddef_subdef(f); - ASSERT(upb_downcast_msgdef(def2)); - ok = strcmp(upb_def_fullname(def2), "B") == 0; - ASSERT(ok); - - upb_def_ref(def2, &def2); - upb_def_unref(def, &def); - - /* We know "def" is still alive because it's reachable from def2. */ - ok = strcmp(upb_def_fullname(def), "A") == 0; - ASSERT(ok); - upb_def_unref(def2, &def2); -} - -static void test_symbol_resolution() { - upb_status s = UPB_STATUS_INIT; - upb_def *defs[2]; - upb_msgdef *m1; - upb_msgdef *m2; - upb_msgdef *m3; - upb_fielddef *m3_field1; - upb_fielddef *m3_field2; - - upb_symtab *symtab = upb_symtab_new(&symtab); - ASSERT(symtab); - - /* m1 has name "A.B.C" and no fields. We'll add it to the symtab now. */ - m1 = upb_msgdef_new(&m1); - ASSERT(m1); - ASSERT_STATUS(upb_msgdef_setfullname(m1, "A.B.C", &s), &s); - ASSERT_STATUS(upb_symtab_add(symtab, (upb_def**)&m1, 1, - NULL, &s), &s); - - /* m2 has name "D.E" and no fields. We'll add it in the same batch as m3 - * below. */ - m2 = upb_msgdef_new(&m2); - ASSERT(m2); - ASSERT_STATUS(upb_msgdef_setfullname(m2, "D.E", &s), &s); - - /* m3 has name "F.G" and two fields, of type A.B.C and D.E respectively. We'll - * add it in the same batch as m2 above. */ - m3 = upb_msgdef_new(&m3); - ASSERT(m3); - ASSERT_STATUS(upb_msgdef_setfullname(m3, "F.G", &s), &s); - m3_field1 = upb_fielddef_new(&m3_field1); - ASSERT_STATUS(upb_fielddef_setname(m3_field1, "field1", &s), &s); - ASSERT_STATUS(upb_fielddef_setnumber(m3_field1, 1, &s), &s); - upb_fielddef_setlabel(m3_field1, UPB_LABEL_OPTIONAL); - upb_fielddef_settype(m3_field1, UPB_TYPE_MESSAGE); - ASSERT_STATUS(upb_fielddef_setsubdefname(m3_field1, ".A.B.C", &s), &s); - ASSERT_STATUS(upb_msgdef_addfield(m3, m3_field1, NULL, &s), &s); - - m3_field2 = upb_fielddef_new(&m3_field2); - ASSERT_STATUS(upb_fielddef_setname(m3_field2, "field2", &s), &s); - ASSERT_STATUS(upb_fielddef_setnumber(m3_field2, 2, &s), &s); - upb_fielddef_setlabel(m3_field2, UPB_LABEL_OPTIONAL); - upb_fielddef_settype(m3_field2, UPB_TYPE_MESSAGE); - ASSERT_STATUS(upb_fielddef_setsubdefname(m3_field2, ".D.E", &s), &s); - ASSERT_STATUS(upb_msgdef_addfield(m3, m3_field2, NULL, &s), &s); - - defs[0] = upb_msgdef_upcast_mutable(m2); - defs[1] = upb_msgdef_upcast_mutable(m3); - ASSERT_STATUS(upb_symtab_add(symtab, defs, 2, NULL, &s), &s); - - upb_fielddef_unref(m3_field2, &m3_field2); - upb_fielddef_unref(m3_field1, &m3_field1); - upb_msgdef_unref(m3, &m3); - upb_msgdef_unref(m2, &m2); - upb_msgdef_unref(m1, &m1); - upb_symtab_free(symtab); -} - -static void test_fielddef_unref() { - bool ok; - upb_symtab *s = load_test_proto(); - const upb_msgdef *md = upb_symtab_lookupmsg(s, "A"); - const upb_fielddef *f = upb_msgdef_itof(md, 1); - upb_fielddef_ref(f, &f); - - /* Unref symtab; now fielddef is the only thing keeping the msgdef alive. */ - upb_symtab_free(s); - /* Check that md is still alive. */ - ok = strcmp(upb_msgdef_fullname(md), "A") == 0; - ASSERT(ok); - - /* Check that unref of fielddef frees the whole remaining graph. */ - upb_fielddef_unref(f, &f); -} - -static void test_fielddef() { - /* Test that we don't leak an unresolved subdef name. */ - upb_fielddef *f1 = upb_fielddef_new(&f1); - upb_fielddef_settype(f1, UPB_TYPE_MESSAGE); - ASSERT(upb_fielddef_setsubdefname(f1, "YO", NULL)); - upb_fielddef_unref(f1, &f1); -} - -static upb_fielddef *newfield( - const char *name, int32_t num, uint8_t type, uint8_t label, - const char *type_name, void *owner) { - upb_fielddef *f = upb_fielddef_new(owner); - ASSERT(upb_fielddef_setname(f, name, NULL)); - ASSERT(upb_fielddef_setnumber(f, num, NULL)); - upb_fielddef_settype(f, type); - upb_fielddef_setlabel(f, label); - if (type_name) { - ASSERT(upb_fielddef_setsubdefname(f, type_name, NULL)); - } - return f; -} - -static upb_msgdef *upb_msgdef_newnamed(const char *name, void *owner) { - upb_msgdef *m = upb_msgdef_new(owner); - upb_msgdef_setfullname(m, name, NULL); - return m; -} - -static void test_replacement_fails() { - bool ok; - upb_symtab *s = upb_symtab_new(&s); - upb_status status = UPB_STATUS_INIT; - upb_def *newdefs[2]; - - upb_msgdef *m = upb_msgdef_newnamed("MyMessage", &s); - upb_msgdef *m2 = upb_msgdef_newnamed("MyMessage", &s); - - newdefs[0] = upb_msgdef_upcast_mutable(m); - newdefs[1] = upb_msgdef_upcast_mutable(m2); - ok = upb_symtab_add(s, newdefs, 2, &s, &status); - ASSERT(ok == false); - upb_status_clear(&status); - - /* Adding just one is ok. */ - ASSERT_STATUS(upb_symtab_add(s, newdefs, 1, &s, &status), &status); - - /* Adding a conflicting one is not ok. */ - newdefs[0] = upb_msgdef_upcast_mutable(m2); - ok = upb_symtab_add(s, newdefs, 1, &s, &status); - ASSERT(ok == false); - - upb_symtab_free(s); -} - -static void test_freeze_free() { - bool ok; - - /* Test that freeze frees defs that were only being kept alive by virtue of - * sharing a group with other defs that are being frozen. */ - upb_msgdef *m1 = upb_msgdef_newnamed("M1", &m1); - upb_msgdef *m2 = upb_msgdef_newnamed("M2", &m2); - upb_msgdef *m3 = upb_msgdef_newnamed("M3", &m3); - upb_msgdef *m4 = upb_msgdef_newnamed("M4", &m4); - upb_fielddef *f = upb_fielddef_new(&f); - - /* Freeze M4 and make M1 point to it. */ - upb_def_freeze((upb_def*const*)&m4, 1, NULL); - - upb_fielddef_settype(f, UPB_TYPE_MESSAGE); - ASSERT(upb_fielddef_setnumber(f, 1, NULL)); - ASSERT(upb_fielddef_setname(f, "foo", NULL)); - ASSERT(upb_fielddef_setmsgsubdef(f, m4, NULL)); - - ASSERT(upb_msgdef_addfield(m1, f, &f, NULL)); - - /* After this unref, M1 is the only thing keeping M4 alive. */ - upb_msgdef_unref(m4, &m4); - - /* Force M1/M2/M3 into a single mutable refcounting group. */ - f = upb_fielddef_new(&f); - upb_fielddef_settype(f, UPB_TYPE_MESSAGE); - ASSERT(upb_fielddef_setnumber(f, 1, NULL)); - ASSERT(upb_fielddef_setname(f, "foo", NULL)); - - ASSERT(upb_fielddef_setmsgsubdef(f, m1, NULL)); - ASSERT(upb_fielddef_setmsgsubdef(f, m2, NULL)); - ASSERT(upb_fielddef_setmsgsubdef(f, m3, NULL)); - - /* Make M3 cyclic with itself. */ - ASSERT(upb_msgdef_addfield(m3, f, &f, NULL)); - - /* These will be kept alive since they are in the same refcounting group as - * M3, which still has a ref. Note: this behavior is not guaranteed by the - * API, but true in practice with its current implementation. */ - upb_msgdef_unref(m1, &m1); - upb_msgdef_unref(m2, &m2); - - /* Test that they are still alive (NOT allowed by the API). */ - ok = strcmp("M1", upb_msgdef_fullname(m1)) == 0; - ASSERT(ok); - ok = strcmp("M2", upb_msgdef_fullname(m2)) == 0; - ASSERT(ok); - - /* Freeze M3. If the test leaked no memory, then freeing m1 and m2 was - * successful. */ - ASSERT(upb_def_freeze((upb_def*const*)&m3, 1, NULL)); - - upb_msgdef_unref(m3, &m3); -} - -static void test_partial_freeze() { - /* Test that freeze of only part of the graph correctly adjusts objects that - * point to the newly-frozen objects. */ - upb_msgdef *m1 = upb_msgdef_newnamed("M1", &m1); - upb_msgdef *m2 = upb_msgdef_newnamed("M2", &m2); - upb_msgdef *m3 = upb_msgdef_newnamed("M3", &m3); - upb_fielddef *f1 = upb_fielddef_new(&f1); - upb_fielddef *f2 = upb_fielddef_new(&f2); - upb_def *defs[2]; - defs[0] = upb_msgdef_upcast_mutable(m1); - defs[1] = upb_msgdef_upcast_mutable(m2); - - upb_fielddef_settype(f1, UPB_TYPE_MESSAGE); - ASSERT(upb_fielddef_setnumber(f1, 1, NULL)); - ASSERT(upb_fielddef_setname(f1, "f1", NULL)); - ASSERT(upb_fielddef_setmsgsubdef(f1, m1, NULL)); - - upb_fielddef_settype(f2, UPB_TYPE_MESSAGE); - ASSERT(upb_fielddef_setnumber(f2, 2, NULL)); - ASSERT(upb_fielddef_setname(f2, "f2", NULL)); - ASSERT(upb_fielddef_setmsgsubdef(f2, m2, NULL)); - - ASSERT(upb_msgdef_addfield(m3, f1, &f1, NULL)); - ASSERT(upb_msgdef_addfield(m3, f2, &f2, NULL)); - - /* Freeze M1 and M2, which should cause the group to be split - * and m3 (left mutable) to take references on m1 and m2. */ - ASSERT(upb_def_freeze(defs, 2, NULL)); - - ASSERT(upb_msgdef_isfrozen(m1)); - ASSERT(upb_msgdef_isfrozen(m2)); - ASSERT(!upb_msgdef_isfrozen(m3)); - - upb_msgdef_unref(m1, &m1); - upb_msgdef_unref(m2, &m2); - upb_msgdef_unref(m3, &m3); -} - -static void test_descriptor_flags() { - upb_msgdef *m = upb_msgdef_new(&m); - upb_status s = UPB_STATUS_INIT; - - ASSERT(upb_msgdef_mapentry(m) == false); - upb_msgdef_setfullname(m, "TestMessage", &s); - ASSERT(upb_ok(&s)); - upb_msgdef_setmapentry(m, true); - ASSERT(upb_msgdef_mapentry(m) == true); - upb_msgdef_unref(m, &m); -} - -static void test_mapentry_check() { - upb_status s = UPB_STATUS_INIT; - upb_msgdef *m = upb_msgdef_new(&m); - upb_fielddef *f = upb_fielddef_new(&f); - upb_symtab *symtab = upb_symtab_new(&symtab); - upb_msgdef *subm = upb_msgdef_new(&subm); - upb_def *defs[2]; - - upb_msgdef_setfullname(m, "TestMessage", &s); - upb_fielddef_setname(f, "field1", &s); - upb_fielddef_setnumber(f, 1, &s); - upb_fielddef_setlabel(f, UPB_LABEL_OPTIONAL); - upb_fielddef_settype(f, UPB_TYPE_MESSAGE); - upb_fielddef_setsubdefname(f, ".MapEntry", &s); - upb_msgdef_addfield(m, f, &f, &s); - ASSERT(upb_ok(&s)); - - upb_msgdef_setfullname(subm, "MapEntry", &s); - upb_msgdef_setmapentry(subm, true); - - defs[0] = upb_msgdef_upcast_mutable(m); - defs[1] = upb_msgdef_upcast_mutable(subm); - upb_symtab_add(symtab, defs, 2, NULL, &s); - /* Should not have succeeded: non-repeated field pointing to a MapEntry. */ - ASSERT(!upb_ok(&s)); - - upb_status_clear(&s); - upb_fielddef_setlabel(f, UPB_LABEL_REPEATED); - upb_symtab_add(symtab, defs, 2, NULL, &s); - ASSERT(upb_ok(&s)); - - upb_symtab_free(symtab); - upb_msgdef_unref(subm, &subm); - upb_msgdef_unref(m, &m); -} - -static void test_oneofs() { - upb_status s = UPB_STATUS_INIT; - bool ok = true; - upb_def *subm_defs[1]; - upb_symtab *symtab = upb_symtab_new(&symtab); - upb_msgdef *subm = upb_msgdef_newnamed("SubMessage", &symtab); - upb_msgdef *m = upb_msgdef_newnamed("TestMessage", &symtab); - upb_oneofdef *o = upb_oneofdef_new(&o); - const upb_oneofdef *lookup_o; - const upb_fielddef *lookup_field; - upb_def *defs[1]; - - ASSERT(symtab != NULL); - - /* Create a test message for fields to refer to. */ - upb_msgdef_addfield(subm, newfield("field1", 1, UPB_TYPE_INT32, - UPB_LABEL_OPTIONAL, NULL, &symtab), - &symtab, NULL); - subm_defs[0] = upb_msgdef_upcast_mutable(subm); - ASSERT_STATUS(upb_symtab_add(symtab, subm_defs, 1, &symtab, &s), &s); - - ASSERT(upb_msgdef_numoneofs(m) == 0); - - ASSERT(upb_oneofdef_numfields(o) == 0); - ASSERT(upb_oneofdef_name(o) == NULL); - - ok = upb_oneofdef_setname(o, "test_oneof", &s); - ASSERT_STATUS(ok, &s); - - ok = upb_oneofdef_addfield(o, newfield("field1", 1, UPB_TYPE_INT32, - UPB_LABEL_OPTIONAL, NULL, &symtab), - &symtab, NULL); - ASSERT_STATUS(ok, &s); - ok = upb_oneofdef_addfield(o, newfield("field2", 2, UPB_TYPE_MESSAGE, - UPB_LABEL_OPTIONAL, ".SubMessage", - &symtab), - &symtab, NULL); - ASSERT_STATUS(ok, &s); - - ok = upb_msgdef_addoneof(m, o, NULL, &s); - ASSERT_STATUS(ok, &s); - - defs[0] = upb_msgdef_upcast_mutable(m); - ASSERT_STATUS(upb_symtab_add(symtab, defs, 1, &symtab, &s), &s); - - ASSERT(upb_msgdef_numoneofs(m) == 1); - lookup_o = upb_msgdef_ntooz(m, "test_oneof"); - ASSERT(lookup_o == o); - - lookup_field = upb_oneofdef_ntofz(o, "field1"); - ASSERT(lookup_field != NULL && upb_fielddef_number(lookup_field) == 1); - - upb_symtab_free(symtab); - upb_oneofdef_unref(o, &o); -} - -int run_tests(int argc, char *argv[]) { - if (argc < 2) { - fprintf(stderr, "Usage: test_def \n"); - return 1; - } - descriptor_file = argv[1]; - test_empty_symtab(); - test_cycles(); - test_symbol_resolution(); - test_fielddef(); - test_fielddef_unref(); - test_replacement_fails(); - test_freeze_free(); - test_partial_freeze(); - test_noreftracking(); - test_descriptor_flags(); - test_mapentry_check(); - test_oneofs(); - return 0; -} diff --git a/upb/def.c b/upb/def.c index 33ad70e..9a4d766 100644 --- a/upb/def.c +++ b/upb/def.c @@ -1359,14 +1359,21 @@ static void count_types_in_msg( size_t i; arr = google_protobuf_DescriptorProto_nested_type(msg_proto); - for (i = 0; i < upb_array_size(arr); i++) { - count_types_in_msg(upb_msgval_getptr(upb_array_get(arr, i)), counts); + if (arr) { + for (i = 0; i < upb_array_size(arr); i++) { + count_types_in_msg(upb_msgval_getptr(upb_array_get(arr, i)), counts); + } } arr = google_protobuf_DescriptorProto_enum_type(msg_proto); - counts->enum_count += upb_array_size(arr); + if (arr) { + counts->enum_count += upb_array_size(arr); + } + arr = google_protobuf_DescriptorProto_extension(msg_proto); - counts->ext_count += upb_array_size(arr); + if (arr) { + counts->ext_count += upb_array_size(arr); + } } static void count_types_in_file( diff --git a/upb/def.h b/upb/def.h index 78ae9b3..4fe6d04 100644 --- a/upb/def.h +++ b/upb/def.h @@ -564,8 +564,6 @@ class upb::OneofDef { upb_oneof_iter iter_; }; - iterator begin(); - iterator end(); const_iterator begin() const; const_iterator end() const; @@ -682,7 +680,8 @@ class upb::SymbolTable { /* TODO: iteration? */ /* Adds the given serialized FileDescriptorProto to the pool. */ - bool AddFile(const char *file, size_t len, Status *status); + bool AddFile(const google_protobuf_FileDescriptorProto *file_proto, + Status *status); /* Adds the given serialized FileDescriptorSet to the pool. */ bool AddSet(const char *set, size_t len, Status *status); @@ -703,6 +702,7 @@ const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym); const upb_msgdef *upb_symtab_lookupmsg2( const upb_symtab *s, const char *sym, size_t len); const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym); +int upb_symtab_filecount(const upb_symtab *s); bool upb_symtab_addfile(upb_symtab *s, const google_protobuf_FileDescriptorProto* file, upb_status *status); @@ -721,6 +721,10 @@ inline void SymbolTable::Free(SymbolTable* s) { inline const MessageDef *SymbolTable::LookupMessage(const char *sym) const { return upb_symtab_lookupmsg(this, sym); } +inline bool SymbolTable::AddFile( + const google_protobuf_FileDescriptorProto *file_proto, Status *status) { + return upb_symtab_addfile(this, file_proto, status); +} } /* namespace upb */ #endif @@ -767,9 +771,6 @@ inline const MessageDef* FieldDef::containing_type() const { inline const OneofDef* FieldDef::containing_oneof() const { return upb_fielddef_containingoneof(this); } -inline void FieldDef::clear_json_name() { - upb_fielddef_clearjsonname(this); -} inline bool FieldDef::IsSubMessage() const { return upb_fielddef_issubmsg(this); } @@ -994,9 +995,6 @@ inline const char* FileDef::phpprefix() const { inline const char* FileDef::phpnamespace() const { return upb_filedef_phpnamespace(this); } -inline int FileDef::def_count() const { - return upb_filedef_defcount(this); -} inline int FileDef::dependency_count() const { return upb_filedef_depcount(this); } -- cgit v1.2.3 From cf0a89ed69561d73210056566272b27844a95782 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Thu, 13 Dec 2018 07:41:56 -0800 Subject: Updated to use the new array accessors. --- upb/bindings/lua/def.c | 14 ++---- upb/def.c | 133 +++++++++++++++++++++---------------------------- 2 files changed, 63 insertions(+), 84 deletions(-) (limited to 'upb') diff --git a/upb/bindings/lua/def.c b/upb/bindings/lua/def.c index 1fde212..00b75c9 100644 --- a/upb/bindings/lua/def.c +++ b/upb/bindings/lua/def.c @@ -634,10 +634,9 @@ static int lupb_symtab_gc(lua_State *L) { * serialized string once we have a good story for vending compiled-in * messages. */ static int lupb_symtab_add(lua_State *L) { - size_t len; upb_arena *arena; - size_t i; - upb_array *file_arr; + size_t i, n, len; + const google_protobuf_FileDescriptorProto *const *files; google_protobuf_FileDescriptorSet *set; upb_symtab *s = lupb_symtab_check(L, 1); const char *str = luaL_checklstring(L, 2, &len); @@ -652,12 +651,9 @@ static int lupb_symtab_add(lua_State *L) { luaL_argerror(L, 2, "failed to parse descriptor"); } - file_arr = (upb_array*)google_protobuf_FileDescriptorSet_file(set); - for (i = 0; i < upb_array_size(file_arr); i++) { - upb_msgval entry = upb_array_get(file_arr, i); - google_protobuf_FileDescriptorProto *file = - (void *)upb_msgval_getmsg(entry); - CHK(upb_symtab_addfile(s, file, &status)); + files = google_protobuf_FileDescriptorSet_file(set, &n); + for (i = 0; i < n; i++) { + CHK(upb_symtab_addfile(s, files[i], &status)); } return 0; diff --git a/upb/def.c b/upb/def.c index 9a4d766..8029c92 100644 --- a/upb/def.c +++ b/upb/def.c @@ -1232,9 +1232,9 @@ static bool create_enumdef( const symtab_addctx *ctx, const char *prefix, const google_protobuf_EnumDescriptorProto *enum_proto) { upb_enumdef *e; - const upb_array *arr; + const google_protobuf_EnumValueDescriptorProto *const *values; upb_stringview name; - size_t i; + size_t i, n; name = google_protobuf_EnumDescriptorProto_name(enum_proto); CHK(upb_isident(name, false, ctx->status)); @@ -1248,11 +1248,10 @@ static bool create_enumdef( e->defaultval = 0; - arr = google_protobuf_EnumDescriptorProto_value(enum_proto); + values = google_protobuf_EnumDescriptorProto_value(enum_proto, &n); - for (i = 0; i < upb_array_size(arr); i++) { - const google_protobuf_EnumValueDescriptorProto *value = - upb_msgval_getptr(upb_array_get(arr, i)); + for (i = 0; i < n; i++) { + const google_protobuf_EnumValueDescriptorProto *value = values[i]; upb_stringview name = google_protobuf_EnumValueDescriptorProto_name(value); char *name2 = upb_strdup2(name.data, name.size, ctx->alloc); int32_t num = google_protobuf_EnumValueDescriptorProto_number(value); @@ -1277,7 +1276,10 @@ static bool create_msgdef(const symtab_addctx *ctx, const char *prefix, const google_protobuf_DescriptorProto *msg_proto) { upb_msgdef *m; const google_protobuf_MessageOptions *options; - const upb_array *arr; + const google_protobuf_OneofDescriptorProto *const *oneofs; + const google_protobuf_FieldDescriptorProto *const *fields; + const google_protobuf_EnumDescriptorProto *const *enums; + const google_protobuf_DescriptorProto *const *msgs; size_t i, n; upb_stringview name; @@ -1300,21 +1302,16 @@ static bool create_msgdef(const symtab_addctx *ctx, const char *prefix, m->map_entry = google_protobuf_MessageOptions_map_entry(options); } - arr = google_protobuf_DescriptorProto_oneof_decl(msg_proto); + oneofs = google_protobuf_DescriptorProto_oneof_decl(msg_proto, &n); m->oneof_count = 0; - n = upb_array_size(arr); m->oneofs = upb_malloc(ctx->alloc, sizeof(*m->oneofs) * n); for (i = 0; i < n; i++) { - const google_protobuf_OneofDescriptorProto *oneof_proto = - upb_msgval_getptr(upb_array_get(arr, i)); - CHK(create_oneofdef(ctx, m, oneof_proto)); + CHK(create_oneofdef(ctx, m, oneofs[i])); } - arr = google_protobuf_DescriptorProto_field(msg_proto); - for (i = 0; i < upb_array_size(arr); i++) { - const google_protobuf_FieldDescriptorProto *field_proto = - upb_msgval_getptr(upb_array_get(arr, i)); - CHK(create_fielddef(ctx, m->full_name, m, field_proto)); + fields = google_protobuf_DescriptorProto_field(msg_proto, &n); + for (i = 0; i < n; i++) { + CHK(create_fielddef(ctx, m->full_name, m, fields[i])); } CHK(assign_msg_indices(m, ctx->status)); @@ -1322,18 +1319,14 @@ static bool create_msgdef(const symtab_addctx *ctx, const char *prefix, /* This message is built. Now build nested messages and enums. */ - arr = google_protobuf_DescriptorProto_enum_type(msg_proto); - for (i = 0; i < upb_array_size(arr); i++) { - const google_protobuf_EnumDescriptorProto *enum_proto = - upb_msgval_getptr(upb_array_get(arr, i)); - CHK(create_enumdef(ctx, m->full_name, enum_proto)); + enums = google_protobuf_DescriptorProto_enum_type(msg_proto, &n); + for (i = 0; i < n; i++) { + CHK(create_enumdef(ctx, m->full_name, enums[i])); } - arr = google_protobuf_DescriptorProto_nested_type(msg_proto); - for (i = 0; i < upb_array_size(arr); i++) { - const google_protobuf_DescriptorProto *msg_proto2 = - upb_msgval_getptr(upb_array_get(arr, i)); - CHK(create_msgdef(ctx, m->full_name, msg_proto2)); + msgs = google_protobuf_DescriptorProto_nested_type(msg_proto, &n); + for (i = 0; i < n; i++) { + CHK(create_msgdef(ctx, m->full_name, msgs[i])); } return true; @@ -1352,45 +1345,39 @@ typedef struct { int ext_count; } decl_counts; -static void count_types_in_msg( - const google_protobuf_DescriptorProto *msg_proto, - decl_counts *counts) { - const upb_array *arr; - size_t i; +static void count_types_in_msg(const google_protobuf_DescriptorProto *msg_proto, + decl_counts *counts) { + const google_protobuf_DescriptorProto *const *msgs; + size_t i, n; - arr = google_protobuf_DescriptorProto_nested_type(msg_proto); - if (arr) { - for (i = 0; i < upb_array_size(arr); i++) { - count_types_in_msg(upb_msgval_getptr(upb_array_get(arr, i)), counts); - } + msgs = google_protobuf_DescriptorProto_nested_type(msg_proto, &n); + for (i = 0; i < n; i++) { + count_types_in_msg(msgs[i], counts); } - arr = google_protobuf_DescriptorProto_enum_type(msg_proto); - if (arr) { - counts->enum_count += upb_array_size(arr); - } + google_protobuf_DescriptorProto_enum_type(msg_proto, &n); + counts->enum_count += n; - arr = google_protobuf_DescriptorProto_extension(msg_proto); - if (arr) { - counts->ext_count += upb_array_size(arr); - } + google_protobuf_DescriptorProto_extension(msg_proto, &n); + counts->ext_count += n; } static void count_types_in_file( const google_protobuf_FileDescriptorProto *file_proto, decl_counts *counts) { - const upb_array *arr; - size_t i; + const google_protobuf_DescriptorProto *const *msgs; + size_t i, n; - arr = google_protobuf_FileDescriptorProto_message_type(file_proto); - for (i = 0; i < upb_array_size(arr); i++) { - count_types_in_msg(upb_msgval_getptr(upb_array_get(arr, i)), counts); + msgs = google_protobuf_FileDescriptorProto_message_type(file_proto, &n); + for (i = 0; i < n; i++) { + count_types_in_msg(msgs[i], counts); } - arr = google_protobuf_FileDescriptorProto_enum_type(file_proto); - counts->enum_count += upb_array_size(arr); - arr = google_protobuf_FileDescriptorProto_extension(file_proto); - counts->ext_count += upb_array_size(arr); + google_protobuf_FileDescriptorProto_enum_type(file_proto, &n); + counts->enum_count += n; + + google_protobuf_FileDescriptorProto_extension(file_proto, &n); + counts->ext_count += n; } static bool resolve_fielddef(const symtab_addctx *ctx, const char *prefix, @@ -1424,11 +1411,13 @@ static bool resolve_fielddef(const symtab_addctx *ctx, const char *prefix, static bool build_filedef( const symtab_addctx *ctx, upb_filedef *file, const google_protobuf_FileDescriptorProto *file_proto) { - const google_protobuf_FileOptions *file_options_proto; upb_alloc *alloc = ctx->alloc; - const upb_array *arr; + const google_protobuf_FileOptions *file_options_proto; + const google_protobuf_DescriptorProto *const *msgs; + const google_protobuf_EnumDescriptorProto *const *enums; + const google_protobuf_FieldDescriptorProto *const *exts; + const upb_stringview* strs; size_t i, n; - int j; decl_counts counts; upb_stringview syntax, package; @@ -1473,12 +1462,12 @@ static bool build_filedef( } /* Verify dependencies. */ - arr = google_protobuf_FileDescriptorProto_dependency(file_proto); - n = upb_array_size(arr); + strs = google_protobuf_FileDescriptorProto_dependency(file_proto, &n); file->deps = upb_malloc(alloc, sizeof(*file->deps) * n) ; CHK_OOM(file->deps); + for (i = 0; i < n; i++) { - upb_stringview dep_name = upb_msgval_getstr(upb_array_get(arr, i)); + upb_stringview dep_name = strs[i]; upb_value v; if (!upb_strtable_lookup2(&ctx->symtab->files, dep_name.data, dep_name.size, &v)) { @@ -1491,30 +1480,23 @@ static bool build_filedef( } /* Create messages. */ - arr = google_protobuf_FileDescriptorProto_message_type(file_proto); - for (i = 0; i < upb_array_size(arr); i++) { - const google_protobuf_DescriptorProto *msg_proto = - upb_msgval_getptr(upb_array_get(arr, i)); - CHK(create_msgdef(ctx, file->package, msg_proto)); + msgs = google_protobuf_FileDescriptorProto_message_type(file_proto, &n); + for (i = 0; i < n; i++) { + CHK(create_msgdef(ctx, file->package, msgs[i])); } /* Create enums. */ - arr = google_protobuf_FileDescriptorProto_enum_type(file_proto); - for (i = 0; i < upb_array_size(arr); i++) { - const google_protobuf_EnumDescriptorProto *enum_proto = - upb_msgval_getptr(upb_array_get(arr, i)); - CHK(create_enumdef(ctx, file->package, enum_proto)); + enums = google_protobuf_FileDescriptorProto_enum_type(file_proto, &n); + for (i = 0; i < n; i++) { + CHK(create_enumdef(ctx, file->package, enums[i])); } /* Create extensions. */ - arr = google_protobuf_FileDescriptorProto_extension(file_proto); - n = upb_array_size(arr); + exts = google_protobuf_FileDescriptorProto_extension(file_proto, &n); file->exts = upb_malloc(alloc, sizeof(*file->exts) * n); CHK_OOM(file->exts); for (i = 0; i < n; i++) { - const google_protobuf_FieldDescriptorProto *field_proto = - upb_msgval_getptr(upb_array_get(arr, i)); - CHK(create_fielddef(ctx, file->package, NULL, field_proto)); + CHK(create_fielddef(ctx, file->package, NULL, exts[i])); } /* Now that all names are in the table, resolve references. */ @@ -1524,6 +1506,7 @@ static bool build_filedef( for (i = 0; i < file->msg_count; i++) { const upb_msgdef *m = &file->msgs[i]; + int j; for (j = 0; i < m->field_count; i++) { CHK(resolve_fielddef(ctx, m->full_name, (upb_fielddef*)&m->fields[j])); } -- cgit v1.2.3 From e2c2121f20bb8e90830f11d562e19d7244a14a5b Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Thu, 13 Dec 2018 10:55:26 -0800 Subject: Use some hazzers in def.c. --- upb/def.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'upb') diff --git a/upb/def.c b/upb/def.c index ab8a8b7..e9acd43 100644 --- a/upb/def.c +++ b/upb/def.c @@ -1133,7 +1133,7 @@ static bool create_fielddef( upb_alloc *alloc = ctx->alloc; upb_fielddef *f; const google_protobuf_FieldOptions *options; - upb_stringview defaultval, name; + upb_stringview name; upb_value packed_v = pack_def(f, UPB_DEFTYPE_FIELD); upb_value v = upb_value_constptr(f); const char *full_name; @@ -1220,17 +1220,20 @@ static bool create_fielddef( f->oneof = NULL; } - defaultval = google_protobuf_FieldDescriptorProto_default_value(field_proto); - - if (defaultval.data) { + if (google_protobuf_FieldDescriptorProto_has_default_value(field_proto)) { + upb_stringview defaultval = + google_protobuf_FieldDescriptorProto_default_value(field_proto); CHK(parse_default(ctx, defaultval.data, defaultval.size, f)); } - options = google_protobuf_FieldDescriptorProto_options(field_proto); - if (options) { + if (google_protobuf_FieldDescriptorProto_has_options(field_proto)) { + options = google_protobuf_FieldDescriptorProto_options(field_proto); f->lazy_ = google_protobuf_FieldOptions_lazy(options); f->packed_ = google_protobuf_FieldOptions_packed(options); + } else { + f->lazy_ = false; + f->packed_ = false; } return true; -- cgit v1.2.3 From 380558922b661499f4eb43b0463b5a73e5fe87b7 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 15 Dec 2018 16:17:48 -0800 Subject: test_encoder passes! Other tests still need to be fixed. --- google/protobuf/descriptor.upb.h | 172 +++++++++++------------ tests/pb/test_encoder.cc | 7 +- upb/def.c | 289 ++++++++++++++++++++------------------- upb/generated_util.h | 6 +- upb/msg.h | 3 + upb/upb.c | 1 + upb/upb.h | 1 + upbc/generator.cc | 2 +- upbc/message_layout.cc | 4 +- 9 files changed, 251 insertions(+), 234 deletions(-) (limited to 'upb') diff --git a/google/protobuf/descriptor.upb.h b/google/protobuf/descriptor.upb.h index 8ed3fbe..421e41e 100644 --- a/google/protobuf/descriptor.upb.h +++ b/google/protobuf/descriptor.upb.h @@ -160,22 +160,22 @@ UPB_INLINE char *google_protobuf_FileDescriptorProto_serialize(const google_prot return upb_encode(msg, &google_protobuf_FileDescriptorProto_msginit, arena, len); } -UPB_INLINE bool google_protobuf_FileDescriptorProto_has_name(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_FileDescriptorProto_has_name(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 1); } UPB_INLINE upb_stringview google_protobuf_FileDescriptorProto_name(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(4, 8)); } -UPB_INLINE bool google_protobuf_FileDescriptorProto_has_package(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_FileDescriptorProto_has_package(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 2); } UPB_INLINE upb_stringview google_protobuf_FileDescriptorProto_package(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(12, 24)); } UPB_INLINE upb_stringview const* google_protobuf_FileDescriptorProto_dependency(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (upb_stringview const*)_upb_array_accessor(msg, UPB_SIZE(36, 72), len); } UPB_INLINE const google_protobuf_DescriptorProto* const* google_protobuf_FileDescriptorProto_message_type(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(40, 80), len); } UPB_INLINE const google_protobuf_EnumDescriptorProto* const* google_protobuf_FileDescriptorProto_enum_type(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(44, 88), len); } UPB_INLINE const google_protobuf_ServiceDescriptorProto* const* google_protobuf_FileDescriptorProto_service(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_ServiceDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(48, 96), len); } UPB_INLINE const google_protobuf_FieldDescriptorProto* const* google_protobuf_FileDescriptorProto_extension(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_FieldDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(52, 104), len); } -UPB_INLINE bool google_protobuf_FileDescriptorProto_has_options(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE bool google_protobuf_FileDescriptorProto_has_options(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 4); } UPB_INLINE const google_protobuf_FileOptions* google_protobuf_FileDescriptorProto_options(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_FileOptions*, UPB_SIZE(28, 56)); } -UPB_INLINE bool google_protobuf_FileDescriptorProto_has_source_code_info(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 4); } +UPB_INLINE bool google_protobuf_FileDescriptorProto_has_source_code_info(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 5); } UPB_INLINE const google_protobuf_SourceCodeInfo* google_protobuf_FileDescriptorProto_source_code_info(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_SourceCodeInfo*, UPB_SIZE(32, 64)); } UPB_INLINE int32_t const* google_protobuf_FileDescriptorProto_public_dependency(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(56, 112), len); } UPB_INLINE int32_t const* google_protobuf_FileDescriptorProto_weak_dependency(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(60, 120), len); } -UPB_INLINE bool google_protobuf_FileDescriptorProto_has_syntax(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE bool google_protobuf_FileDescriptorProto_has_syntax(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 3); } UPB_INLINE upb_stringview google_protobuf_FileDescriptorProto_syntax(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(20, 40)); } UPB_INLINE void google_protobuf_FileDescriptorProto_set_name(google_protobuf_FileDescriptorProto *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(4, 8)) = value; } @@ -213,14 +213,14 @@ UPB_INLINE char *google_protobuf_DescriptorProto_serialize(const google_protobuf return upb_encode(msg, &google_protobuf_DescriptorProto_msginit, arena, len); } -UPB_INLINE bool google_protobuf_DescriptorProto_has_name(const google_protobuf_DescriptorProto *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_DescriptorProto_has_name(const google_protobuf_DescriptorProto *msg) { return _upb_has_field(msg, 1); } UPB_INLINE upb_stringview google_protobuf_DescriptorProto_name(const google_protobuf_DescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(4, 8)); } UPB_INLINE const google_protobuf_FieldDescriptorProto* const* google_protobuf_DescriptorProto_field(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_FieldDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(16, 32), len); } UPB_INLINE const google_protobuf_DescriptorProto* const* google_protobuf_DescriptorProto_nested_type(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(20, 40), len); } UPB_INLINE const google_protobuf_EnumDescriptorProto* const* google_protobuf_DescriptorProto_enum_type(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(24, 48), len); } UPB_INLINE const google_protobuf_DescriptorProto_ExtensionRange* const* google_protobuf_DescriptorProto_extension_range(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto_ExtensionRange* const*)_upb_array_accessor(msg, UPB_SIZE(28, 56), len); } UPB_INLINE const google_protobuf_FieldDescriptorProto* const* google_protobuf_DescriptorProto_extension(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_FieldDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(32, 64), len); } -UPB_INLINE bool google_protobuf_DescriptorProto_has_options(const google_protobuf_DescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_DescriptorProto_has_options(const google_protobuf_DescriptorProto *msg) { return _upb_has_field(msg, 2); } UPB_INLINE const google_protobuf_MessageOptions* google_protobuf_DescriptorProto_options(const google_protobuf_DescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_MessageOptions*, UPB_SIZE(12, 24)); } UPB_INLINE const google_protobuf_OneofDescriptorProto* const* google_protobuf_DescriptorProto_oneof_decl(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_OneofDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(36, 72), len); } UPB_INLINE const google_protobuf_DescriptorProto_ReservedRange* const* google_protobuf_DescriptorProto_reserved_range(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto_ReservedRange* const*)_upb_array_accessor(msg, UPB_SIZE(40, 80), len); } @@ -260,11 +260,11 @@ UPB_INLINE char *google_protobuf_DescriptorProto_ExtensionRange_serialize(const return upb_encode(msg, &google_protobuf_DescriptorProto_ExtensionRange_msginit, arena, len); } -UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_start(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_start(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return _upb_has_field(msg, 1); } UPB_INLINE int32_t google_protobuf_DescriptorProto_ExtensionRange_start(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); } -UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_end(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_end(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return _upb_has_field(msg, 2); } UPB_INLINE int32_t google_protobuf_DescriptorProto_ExtensionRange_end(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); } -UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_options(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_options(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return _upb_has_field(msg, 3); } UPB_INLINE const google_protobuf_ExtensionRangeOptions* google_protobuf_DescriptorProto_ExtensionRange_options(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return UPB_FIELD_AT(msg, const google_protobuf_ExtensionRangeOptions*, UPB_SIZE(12, 16)); } UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_set_start(google_protobuf_DescriptorProto_ExtensionRange *msg, int32_t value) { UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value; } @@ -286,9 +286,9 @@ UPB_INLINE char *google_protobuf_DescriptorProto_ReservedRange_serialize(const g return upb_encode(msg, &google_protobuf_DescriptorProto_ReservedRange_msginit, arena, len); } -UPB_INLINE bool google_protobuf_DescriptorProto_ReservedRange_has_start(const google_protobuf_DescriptorProto_ReservedRange *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_DescriptorProto_ReservedRange_has_start(const google_protobuf_DescriptorProto_ReservedRange *msg) { return _upb_has_field(msg, 1); } UPB_INLINE int32_t google_protobuf_DescriptorProto_ReservedRange_start(const google_protobuf_DescriptorProto_ReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); } -UPB_INLINE bool google_protobuf_DescriptorProto_ReservedRange_has_end(const google_protobuf_DescriptorProto_ReservedRange *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_DescriptorProto_ReservedRange_has_end(const google_protobuf_DescriptorProto_ReservedRange *msg) { return _upb_has_field(msg, 2); } UPB_INLINE int32_t google_protobuf_DescriptorProto_ReservedRange_end(const google_protobuf_DescriptorProto_ReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); } UPB_INLINE void google_protobuf_DescriptorProto_ReservedRange_set_start(google_protobuf_DescriptorProto_ReservedRange *msg, int32_t value) { UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value; } @@ -329,25 +329,25 @@ UPB_INLINE char *google_protobuf_FieldDescriptorProto_serialize(const google_pro return upb_encode(msg, &google_protobuf_FieldDescriptorProto_msginit, arena, len); } -UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_name(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 4); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_name(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 5); } UPB_INLINE upb_stringview google_protobuf_FieldDescriptorProto_name(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(32, 32)); } -UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_extendee(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 5); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_extendee(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 6); } UPB_INLINE upb_stringview google_protobuf_FieldDescriptorProto_extendee(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(40, 48)); } -UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_number(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_number(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 3); } UPB_INLINE int32_t google_protobuf_FieldDescriptorProto_number(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(24, 24)); } -UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_label(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_label(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 1); } UPB_INLINE google_protobuf_FieldDescriptorProto_Label google_protobuf_FieldDescriptorProto_label(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, google_protobuf_FieldDescriptorProto_Label, UPB_SIZE(8, 8)); } -UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_type(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_type(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 2); } UPB_INLINE google_protobuf_FieldDescriptorProto_Type google_protobuf_FieldDescriptorProto_type(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, google_protobuf_FieldDescriptorProto_Type, UPB_SIZE(16, 16)); } -UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_type_name(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 6); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_type_name(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 7); } UPB_INLINE upb_stringview google_protobuf_FieldDescriptorProto_type_name(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(48, 64)); } -UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_default_value(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 7); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_default_value(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 8); } UPB_INLINE upb_stringview google_protobuf_FieldDescriptorProto_default_value(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(56, 80)); } -UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_options(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 9); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_options(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 10); } UPB_INLINE const google_protobuf_FieldOptions* google_protobuf_FieldDescriptorProto_options(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_FieldOptions*, UPB_SIZE(72, 112)); } -UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_oneof_index(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_oneof_index(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 4); } UPB_INLINE int32_t google_protobuf_FieldDescriptorProto_oneof_index(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(28, 28)); } -UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_json_name(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 8); } +UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_json_name(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 9); } UPB_INLINE upb_stringview google_protobuf_FieldDescriptorProto_json_name(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(64, 96)); } UPB_INLINE void google_protobuf_FieldDescriptorProto_set_name(google_protobuf_FieldDescriptorProto *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(32, 32)) = value; } @@ -376,9 +376,9 @@ UPB_INLINE char *google_protobuf_OneofDescriptorProto_serialize(const google_pro return upb_encode(msg, &google_protobuf_OneofDescriptorProto_msginit, arena, len); } -UPB_INLINE bool google_protobuf_OneofDescriptorProto_has_name(const google_protobuf_OneofDescriptorProto *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_OneofDescriptorProto_has_name(const google_protobuf_OneofDescriptorProto *msg) { return _upb_has_field(msg, 1); } UPB_INLINE upb_stringview google_protobuf_OneofDescriptorProto_name(const google_protobuf_OneofDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(4, 8)); } -UPB_INLINE bool google_protobuf_OneofDescriptorProto_has_options(const google_protobuf_OneofDescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_OneofDescriptorProto_has_options(const google_protobuf_OneofDescriptorProto *msg) { return _upb_has_field(msg, 2); } UPB_INLINE const google_protobuf_OneofOptions* google_protobuf_OneofDescriptorProto_options(const google_protobuf_OneofDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_OneofOptions*, UPB_SIZE(12, 24)); } UPB_INLINE void google_protobuf_OneofDescriptorProto_set_name(google_protobuf_OneofDescriptorProto *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(4, 8)) = value; } @@ -399,10 +399,10 @@ UPB_INLINE char *google_protobuf_EnumDescriptorProto_serialize(const google_prot return upb_encode(msg, &google_protobuf_EnumDescriptorProto_msginit, arena, len); } -UPB_INLINE bool google_protobuf_EnumDescriptorProto_has_name(const google_protobuf_EnumDescriptorProto *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_EnumDescriptorProto_has_name(const google_protobuf_EnumDescriptorProto *msg) { return _upb_has_field(msg, 1); } UPB_INLINE upb_stringview google_protobuf_EnumDescriptorProto_name(const google_protobuf_EnumDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(4, 8)); } UPB_INLINE const google_protobuf_EnumValueDescriptorProto* const* google_protobuf_EnumDescriptorProto_value(const google_protobuf_EnumDescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumValueDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(16, 32), len); } -UPB_INLINE bool google_protobuf_EnumDescriptorProto_has_options(const google_protobuf_EnumDescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_EnumDescriptorProto_has_options(const google_protobuf_EnumDescriptorProto *msg) { return _upb_has_field(msg, 2); } UPB_INLINE const google_protobuf_EnumOptions* google_protobuf_EnumDescriptorProto_options(const google_protobuf_EnumDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_EnumOptions*, UPB_SIZE(12, 24)); } UPB_INLINE const google_protobuf_EnumDescriptorProto_EnumReservedRange* const* google_protobuf_EnumDescriptorProto_reserved_range(const google_protobuf_EnumDescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumDescriptorProto_EnumReservedRange* const*)_upb_array_accessor(msg, UPB_SIZE(20, 40), len); } UPB_INLINE upb_stringview const* google_protobuf_EnumDescriptorProto_reserved_name(const google_protobuf_EnumDescriptorProto *msg, size_t *len) { return (upb_stringview const*)_upb_array_accessor(msg, UPB_SIZE(24, 48), len); } @@ -431,9 +431,9 @@ UPB_INLINE char *google_protobuf_EnumDescriptorProto_EnumReservedRange_serialize return upb_encode(msg, &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, arena, len); } -UPB_INLINE bool google_protobuf_EnumDescriptorProto_EnumReservedRange_has_start(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_EnumDescriptorProto_EnumReservedRange_has_start(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return _upb_has_field(msg, 1); } UPB_INLINE int32_t google_protobuf_EnumDescriptorProto_EnumReservedRange_start(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); } -UPB_INLINE bool google_protobuf_EnumDescriptorProto_EnumReservedRange_has_end(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_EnumDescriptorProto_EnumReservedRange_has_end(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return _upb_has_field(msg, 2); } UPB_INLINE int32_t google_protobuf_EnumDescriptorProto_EnumReservedRange_end(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); } UPB_INLINE void google_protobuf_EnumDescriptorProto_EnumReservedRange_set_start(google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, int32_t value) { UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value; } @@ -454,11 +454,11 @@ UPB_INLINE char *google_protobuf_EnumValueDescriptorProto_serialize(const google return upb_encode(msg, &google_protobuf_EnumValueDescriptorProto_msginit, arena, len); } -UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_name(const google_protobuf_EnumValueDescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_name(const google_protobuf_EnumValueDescriptorProto *msg) { return _upb_has_field(msg, 2); } UPB_INLINE upb_stringview google_protobuf_EnumValueDescriptorProto_name(const google_protobuf_EnumValueDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(8, 8)); } -UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_number(const google_protobuf_EnumValueDescriptorProto *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_number(const google_protobuf_EnumValueDescriptorProto *msg) { return _upb_has_field(msg, 1); } UPB_INLINE int32_t google_protobuf_EnumValueDescriptorProto_number(const google_protobuf_EnumValueDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); } -UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_options(const google_protobuf_EnumValueDescriptorProto *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_options(const google_protobuf_EnumValueDescriptorProto *msg) { return _upb_has_field(msg, 3); } UPB_INLINE const google_protobuf_EnumValueOptions* google_protobuf_EnumValueDescriptorProto_options(const google_protobuf_EnumValueDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_EnumValueOptions*, UPB_SIZE(16, 24)); } UPB_INLINE void google_protobuf_EnumValueDescriptorProto_set_name(google_protobuf_EnumValueDescriptorProto *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(8, 8)) = value; } @@ -480,10 +480,10 @@ UPB_INLINE char *google_protobuf_ServiceDescriptorProto_serialize(const google_p return upb_encode(msg, &google_protobuf_ServiceDescriptorProto_msginit, arena, len); } -UPB_INLINE bool google_protobuf_ServiceDescriptorProto_has_name(const google_protobuf_ServiceDescriptorProto *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_ServiceDescriptorProto_has_name(const google_protobuf_ServiceDescriptorProto *msg) { return _upb_has_field(msg, 1); } UPB_INLINE upb_stringview google_protobuf_ServiceDescriptorProto_name(const google_protobuf_ServiceDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(4, 8)); } UPB_INLINE const google_protobuf_MethodDescriptorProto* const* google_protobuf_ServiceDescriptorProto_method(const google_protobuf_ServiceDescriptorProto *msg, size_t *len) { return (const google_protobuf_MethodDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(16, 32), len); } -UPB_INLINE bool google_protobuf_ServiceDescriptorProto_has_options(const google_protobuf_ServiceDescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_ServiceDescriptorProto_has_options(const google_protobuf_ServiceDescriptorProto *msg) { return _upb_has_field(msg, 2); } UPB_INLINE const google_protobuf_ServiceOptions* google_protobuf_ServiceDescriptorProto_options(const google_protobuf_ServiceDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_ServiceOptions*, UPB_SIZE(12, 24)); } UPB_INLINE void google_protobuf_ServiceDescriptorProto_set_name(google_protobuf_ServiceDescriptorProto *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(4, 8)) = value; } @@ -506,17 +506,17 @@ UPB_INLINE char *google_protobuf_MethodDescriptorProto_serialize(const google_pr return upb_encode(msg, &google_protobuf_MethodDescriptorProto_msginit, arena, len); } -UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_name(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_name(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 3); } UPB_INLINE upb_stringview google_protobuf_MethodDescriptorProto_name(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(4, 8)); } -UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_input_type(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_input_type(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 4); } UPB_INLINE upb_stringview google_protobuf_MethodDescriptorProto_input_type(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(12, 24)); } -UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_output_type(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 4); } +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_output_type(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 5); } UPB_INLINE upb_stringview google_protobuf_MethodDescriptorProto_output_type(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(20, 40)); } -UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_options(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 5); } +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_options(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 6); } UPB_INLINE const google_protobuf_MethodOptions* google_protobuf_MethodDescriptorProto_options(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_MethodOptions*, UPB_SIZE(28, 56)); } -UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_client_streaming(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_client_streaming(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 1); } UPB_INLINE bool google_protobuf_MethodDescriptorProto_client_streaming(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); } -UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_server_streaming(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_server_streaming(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 2); } UPB_INLINE bool google_protobuf_MethodDescriptorProto_server_streaming(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)); } UPB_INLINE void google_protobuf_MethodDescriptorProto_set_name(google_protobuf_MethodDescriptorProto *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(4, 8)) = value; } @@ -541,41 +541,41 @@ UPB_INLINE char *google_protobuf_FileOptions_serialize(const google_protobuf_Fil return upb_encode(msg, &google_protobuf_FileOptions_msginit, arena, len); } -UPB_INLINE bool google_protobuf_FileOptions_has_java_package(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 10); } +UPB_INLINE bool google_protobuf_FileOptions_has_java_package(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 11); } UPB_INLINE upb_stringview google_protobuf_FileOptions_java_package(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(28, 32)); } -UPB_INLINE bool google_protobuf_FileOptions_has_java_outer_classname(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 11); } +UPB_INLINE bool google_protobuf_FileOptions_has_java_outer_classname(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 12); } UPB_INLINE upb_stringview google_protobuf_FileOptions_java_outer_classname(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(36, 48)); } -UPB_INLINE bool google_protobuf_FileOptions_has_optimize_for(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_FileOptions_has_optimize_for(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 1); } UPB_INLINE google_protobuf_FileOptions_OptimizeMode google_protobuf_FileOptions_optimize_for(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, google_protobuf_FileOptions_OptimizeMode, UPB_SIZE(8, 8)); } -UPB_INLINE bool google_protobuf_FileOptions_has_java_multiple_files(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_FileOptions_has_java_multiple_files(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 2); } UPB_INLINE bool google_protobuf_FileOptions_java_multiple_files(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)); } -UPB_INLINE bool google_protobuf_FileOptions_has_go_package(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 12); } +UPB_INLINE bool google_protobuf_FileOptions_has_go_package(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 13); } UPB_INLINE upb_stringview google_protobuf_FileOptions_go_package(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(44, 64)); } -UPB_INLINE bool google_protobuf_FileOptions_has_cc_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE bool google_protobuf_FileOptions_has_cc_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 3); } UPB_INLINE bool google_protobuf_FileOptions_cc_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(17, 17)); } -UPB_INLINE bool google_protobuf_FileOptions_has_java_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE bool google_protobuf_FileOptions_has_java_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 4); } UPB_INLINE bool google_protobuf_FileOptions_java_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(18, 18)); } -UPB_INLINE bool google_protobuf_FileOptions_has_py_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 4); } +UPB_INLINE bool google_protobuf_FileOptions_has_py_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 5); } UPB_INLINE bool google_protobuf_FileOptions_py_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(19, 19)); } -UPB_INLINE bool google_protobuf_FileOptions_has_java_generate_equals_and_hash(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 5); } +UPB_INLINE bool google_protobuf_FileOptions_has_java_generate_equals_and_hash(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 6); } UPB_INLINE bool google_protobuf_FileOptions_java_generate_equals_and_hash(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(20, 20)); } -UPB_INLINE bool google_protobuf_FileOptions_has_deprecated(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 6); } +UPB_INLINE bool google_protobuf_FileOptions_has_deprecated(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 7); } UPB_INLINE bool google_protobuf_FileOptions_deprecated(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(21, 21)); } -UPB_INLINE bool google_protobuf_FileOptions_has_java_string_check_utf8(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 7); } +UPB_INLINE bool google_protobuf_FileOptions_has_java_string_check_utf8(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 8); } UPB_INLINE bool google_protobuf_FileOptions_java_string_check_utf8(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(22, 22)); } -UPB_INLINE bool google_protobuf_FileOptions_has_cc_enable_arenas(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 8); } +UPB_INLINE bool google_protobuf_FileOptions_has_cc_enable_arenas(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 9); } UPB_INLINE bool google_protobuf_FileOptions_cc_enable_arenas(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(23, 23)); } -UPB_INLINE bool google_protobuf_FileOptions_has_objc_class_prefix(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 13); } +UPB_INLINE bool google_protobuf_FileOptions_has_objc_class_prefix(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 14); } UPB_INLINE upb_stringview google_protobuf_FileOptions_objc_class_prefix(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(52, 80)); } -UPB_INLINE bool google_protobuf_FileOptions_has_csharp_namespace(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 14); } +UPB_INLINE bool google_protobuf_FileOptions_has_csharp_namespace(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 15); } UPB_INLINE upb_stringview google_protobuf_FileOptions_csharp_namespace(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(60, 96)); } -UPB_INLINE bool google_protobuf_FileOptions_has_swift_prefix(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 15); } +UPB_INLINE bool google_protobuf_FileOptions_has_swift_prefix(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 16); } UPB_INLINE upb_stringview google_protobuf_FileOptions_swift_prefix(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(68, 112)); } -UPB_INLINE bool google_protobuf_FileOptions_has_php_class_prefix(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 16); } +UPB_INLINE bool google_protobuf_FileOptions_has_php_class_prefix(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 17); } UPB_INLINE upb_stringview google_protobuf_FileOptions_php_class_prefix(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(76, 128)); } -UPB_INLINE bool google_protobuf_FileOptions_has_php_namespace(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 17); } +UPB_INLINE bool google_protobuf_FileOptions_has_php_namespace(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 18); } UPB_INLINE upb_stringview google_protobuf_FileOptions_php_namespace(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(84, 144)); } -UPB_INLINE bool google_protobuf_FileOptions_has_php_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 9); } +UPB_INLINE bool google_protobuf_FileOptions_has_php_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 10); } UPB_INLINE bool google_protobuf_FileOptions_php_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)); } UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_FileOptions_uninterpreted_option(const google_protobuf_FileOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(92, 160), len); } @@ -615,13 +615,13 @@ UPB_INLINE char *google_protobuf_MessageOptions_serialize(const google_protobuf_ return upb_encode(msg, &google_protobuf_MessageOptions_msginit, arena, len); } -UPB_INLINE bool google_protobuf_MessageOptions_has_message_set_wire_format(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_MessageOptions_has_message_set_wire_format(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 1); } UPB_INLINE bool google_protobuf_MessageOptions_message_set_wire_format(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); } -UPB_INLINE bool google_protobuf_MessageOptions_has_no_standard_descriptor_accessor(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_MessageOptions_has_no_standard_descriptor_accessor(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 2); } UPB_INLINE bool google_protobuf_MessageOptions_no_standard_descriptor_accessor(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)); } -UPB_INLINE bool google_protobuf_MessageOptions_has_deprecated(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE bool google_protobuf_MessageOptions_has_deprecated(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 3); } UPB_INLINE bool google_protobuf_MessageOptions_deprecated(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(3, 3)); } -UPB_INLINE bool google_protobuf_MessageOptions_has_map_entry(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE bool google_protobuf_MessageOptions_has_map_entry(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 4); } UPB_INLINE bool google_protobuf_MessageOptions_map_entry(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(4, 4)); } UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_MessageOptions_uninterpreted_option(const google_protobuf_MessageOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(8, 8), len); } @@ -647,17 +647,17 @@ UPB_INLINE char *google_protobuf_FieldOptions_serialize(const google_protobuf_Fi return upb_encode(msg, &google_protobuf_FieldOptions_msginit, arena, len); } -UPB_INLINE bool google_protobuf_FieldOptions_has_ctype(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_FieldOptions_has_ctype(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 1); } UPB_INLINE google_protobuf_FieldOptions_CType google_protobuf_FieldOptions_ctype(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, google_protobuf_FieldOptions_CType, UPB_SIZE(8, 8)); } -UPB_INLINE bool google_protobuf_FieldOptions_has_packed(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE bool google_protobuf_FieldOptions_has_packed(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 3); } UPB_INLINE bool google_protobuf_FieldOptions_packed(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)); } -UPB_INLINE bool google_protobuf_FieldOptions_has_deprecated(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE bool google_protobuf_FieldOptions_has_deprecated(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 4); } UPB_INLINE bool google_protobuf_FieldOptions_deprecated(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(25, 25)); } -UPB_INLINE bool google_protobuf_FieldOptions_has_lazy(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 4); } +UPB_INLINE bool google_protobuf_FieldOptions_has_lazy(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 5); } UPB_INLINE bool google_protobuf_FieldOptions_lazy(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(26, 26)); } -UPB_INLINE bool google_protobuf_FieldOptions_has_jstype(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_FieldOptions_has_jstype(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 2); } UPB_INLINE google_protobuf_FieldOptions_JSType google_protobuf_FieldOptions_jstype(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, google_protobuf_FieldOptions_JSType, UPB_SIZE(16, 16)); } -UPB_INLINE bool google_protobuf_FieldOptions_has_weak(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 5); } +UPB_INLINE bool google_protobuf_FieldOptions_has_weak(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 6); } UPB_INLINE bool google_protobuf_FieldOptions_weak(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(27, 27)); } UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_FieldOptions_uninterpreted_option(const google_protobuf_FieldOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(28, 32), len); } @@ -705,9 +705,9 @@ UPB_INLINE char *google_protobuf_EnumOptions_serialize(const google_protobuf_Enu return upb_encode(msg, &google_protobuf_EnumOptions_msginit, arena, len); } -UPB_INLINE bool google_protobuf_EnumOptions_has_allow_alias(const google_protobuf_EnumOptions *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_EnumOptions_has_allow_alias(const google_protobuf_EnumOptions *msg) { return _upb_has_field(msg, 1); } UPB_INLINE bool google_protobuf_EnumOptions_allow_alias(const google_protobuf_EnumOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); } -UPB_INLINE bool google_protobuf_EnumOptions_has_deprecated(const google_protobuf_EnumOptions *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_EnumOptions_has_deprecated(const google_protobuf_EnumOptions *msg) { return _upb_has_field(msg, 2); } UPB_INLINE bool google_protobuf_EnumOptions_deprecated(const google_protobuf_EnumOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)); } UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_EnumOptions_uninterpreted_option(const google_protobuf_EnumOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(4, 8), len); } @@ -731,7 +731,7 @@ UPB_INLINE char *google_protobuf_EnumValueOptions_serialize(const google_protobu return upb_encode(msg, &google_protobuf_EnumValueOptions_msginit, arena, len); } -UPB_INLINE bool google_protobuf_EnumValueOptions_has_deprecated(const google_protobuf_EnumValueOptions *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_EnumValueOptions_has_deprecated(const google_protobuf_EnumValueOptions *msg) { return _upb_has_field(msg, 1); } UPB_INLINE bool google_protobuf_EnumValueOptions_deprecated(const google_protobuf_EnumValueOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); } UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_EnumValueOptions_uninterpreted_option(const google_protobuf_EnumValueOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(4, 8), len); } @@ -754,7 +754,7 @@ UPB_INLINE char *google_protobuf_ServiceOptions_serialize(const google_protobuf_ return upb_encode(msg, &google_protobuf_ServiceOptions_msginit, arena, len); } -UPB_INLINE bool google_protobuf_ServiceOptions_has_deprecated(const google_protobuf_ServiceOptions *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_ServiceOptions_has_deprecated(const google_protobuf_ServiceOptions *msg) { return _upb_has_field(msg, 1); } UPB_INLINE bool google_protobuf_ServiceOptions_deprecated(const google_protobuf_ServiceOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); } UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_ServiceOptions_uninterpreted_option(const google_protobuf_ServiceOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(4, 8), len); } @@ -777,9 +777,9 @@ UPB_INLINE char *google_protobuf_MethodOptions_serialize(const google_protobuf_M return upb_encode(msg, &google_protobuf_MethodOptions_msginit, arena, len); } -UPB_INLINE bool google_protobuf_MethodOptions_has_deprecated(const google_protobuf_MethodOptions *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_MethodOptions_has_deprecated(const google_protobuf_MethodOptions *msg) { return _upb_has_field(msg, 2); } UPB_INLINE bool google_protobuf_MethodOptions_deprecated(const google_protobuf_MethodOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)); } -UPB_INLINE bool google_protobuf_MethodOptions_has_idempotency_level(const google_protobuf_MethodOptions *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_MethodOptions_has_idempotency_level(const google_protobuf_MethodOptions *msg) { return _upb_has_field(msg, 1); } UPB_INLINE google_protobuf_MethodOptions_IdempotencyLevel google_protobuf_MethodOptions_idempotency_level(const google_protobuf_MethodOptions *msg) { return UPB_FIELD_AT(msg, google_protobuf_MethodOptions_IdempotencyLevel, UPB_SIZE(8, 8)); } UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_MethodOptions_uninterpreted_option(const google_protobuf_MethodOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(20, 24), len); } @@ -804,17 +804,17 @@ UPB_INLINE char *google_protobuf_UninterpretedOption_serialize(const google_prot } UPB_INLINE const google_protobuf_UninterpretedOption_NamePart* const* google_protobuf_UninterpretedOption_name(const google_protobuf_UninterpretedOption *msg, size_t *len) { return (const google_protobuf_UninterpretedOption_NamePart* const*)_upb_array_accessor(msg, UPB_SIZE(56, 80), len); } -UPB_INLINE bool google_protobuf_UninterpretedOption_has_identifier_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 3); } +UPB_INLINE bool google_protobuf_UninterpretedOption_has_identifier_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 4); } UPB_INLINE upb_stringview google_protobuf_UninterpretedOption_identifier_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(32, 32)); } -UPB_INLINE bool google_protobuf_UninterpretedOption_has_positive_int_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_UninterpretedOption_has_positive_int_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 1); } UPB_INLINE uint64_t google_protobuf_UninterpretedOption_positive_int_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, uint64_t, UPB_SIZE(8, 8)); } -UPB_INLINE bool google_protobuf_UninterpretedOption_has_negative_int_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_UninterpretedOption_has_negative_int_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 2); } UPB_INLINE int64_t google_protobuf_UninterpretedOption_negative_int_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, int64_t, UPB_SIZE(16, 16)); } -UPB_INLINE bool google_protobuf_UninterpretedOption_has_double_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE bool google_protobuf_UninterpretedOption_has_double_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 3); } UPB_INLINE double google_protobuf_UninterpretedOption_double_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, double, UPB_SIZE(24, 24)); } -UPB_INLINE bool google_protobuf_UninterpretedOption_has_string_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 4); } +UPB_INLINE bool google_protobuf_UninterpretedOption_has_string_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 5); } UPB_INLINE upb_stringview google_protobuf_UninterpretedOption_string_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(40, 48)); } -UPB_INLINE bool google_protobuf_UninterpretedOption_has_aggregate_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 5); } +UPB_INLINE bool google_protobuf_UninterpretedOption_has_aggregate_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 6); } UPB_INLINE upb_stringview google_protobuf_UninterpretedOption_aggregate_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(48, 64)); } UPB_INLINE google_protobuf_UninterpretedOption_NamePart** google_protobuf_UninterpretedOption_name_mutable(google_protobuf_UninterpretedOption *msg, size_t *len) { return (google_protobuf_UninterpretedOption_NamePart**)_upb_array_mutable_accessor(msg, UPB_SIZE(56, 80), len); } @@ -841,9 +841,9 @@ UPB_INLINE char *google_protobuf_UninterpretedOption_NamePart_serialize(const go return upb_encode(msg, &google_protobuf_UninterpretedOption_NamePart_msginit, arena, len); } -UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_has_name_part(const google_protobuf_UninterpretedOption_NamePart *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_has_name_part(const google_protobuf_UninterpretedOption_NamePart *msg) { return _upb_has_field(msg, 2); } UPB_INLINE upb_stringview google_protobuf_UninterpretedOption_NamePart_name_part(const google_protobuf_UninterpretedOption_NamePart *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(4, 8)); } -UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_has_is_extension(const google_protobuf_UninterpretedOption_NamePart *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_has_is_extension(const google_protobuf_UninterpretedOption_NamePart *msg) { return _upb_has_field(msg, 1); } UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_is_extension(const google_protobuf_UninterpretedOption_NamePart *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); } UPB_INLINE void google_protobuf_UninterpretedOption_NamePart_set_name_part(google_protobuf_UninterpretedOption_NamePart *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(4, 8)) = value; } @@ -886,9 +886,9 @@ UPB_INLINE char *google_protobuf_SourceCodeInfo_Location_serialize(const google_ UPB_INLINE int32_t const* google_protobuf_SourceCodeInfo_Location_path(const google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(20, 40), len); } UPB_INLINE int32_t const* google_protobuf_SourceCodeInfo_Location_span(const google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(24, 48), len); } -UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_has_leading_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_has_leading_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return _upb_has_field(msg, 1); } UPB_INLINE upb_stringview google_protobuf_SourceCodeInfo_Location_leading_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(4, 8)); } -UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_has_trailing_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_has_trailing_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return _upb_has_field(msg, 2); } UPB_INLINE upb_stringview google_protobuf_SourceCodeInfo_Location_trailing_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(12, 24)); } UPB_INLINE upb_stringview const* google_protobuf_SourceCodeInfo_Location_leading_detached_comments(const google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { return (upb_stringview const*)_upb_array_accessor(msg, UPB_SIZE(28, 56), len); } @@ -937,11 +937,11 @@ UPB_INLINE char *google_protobuf_GeneratedCodeInfo_Annotation_serialize(const go } UPB_INLINE int32_t const* google_protobuf_GeneratedCodeInfo_Annotation_path(const google_protobuf_GeneratedCodeInfo_Annotation *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(20, 32), len); } -UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_source_file(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return _upb_has_field(msg, 2); } +UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_source_file(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return _upb_has_field(msg, 3); } UPB_INLINE upb_stringview google_protobuf_GeneratedCodeInfo_Annotation_source_file(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(12, 16)); } -UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_begin(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return _upb_has_field(msg, 0); } +UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_begin(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return _upb_has_field(msg, 1); } UPB_INLINE int32_t google_protobuf_GeneratedCodeInfo_Annotation_begin(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); } -UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_end(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return _upb_has_field(msg, 1); } +UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_end(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return _upb_has_field(msg, 2); } UPB_INLINE int32_t google_protobuf_GeneratedCodeInfo_Annotation_end(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); } UPB_INLINE int32_t* google_protobuf_GeneratedCodeInfo_Annotation_path_mutable(google_protobuf_GeneratedCodeInfo_Annotation *msg, size_t *len) { return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 32), len); } diff --git a/tests/pb/test_encoder.cc b/tests/pb/test_encoder.cc index 6cc9a2a..a0f8453 100644 --- a/tests/pb/test_encoder.cc +++ b/tests/pb/test_encoder.cc @@ -27,14 +27,17 @@ void test_pb_roundtrip() { size_t n; const google_protobuf_FileDescriptorProto *const *files = google_protobuf_FileDescriptorSet_file(set, &n); - fprintf(stderr, "n: %d\n", (int)n); ASSERT(n == 1); upb::Status status; bool ok = symtab->AddFile(files[0], &status); - ASSERT(ok); + if (!ok) { + fprintf(stderr, "Error building def: %s\n", upb_status_errmsg(&status)); + ASSERT(false); + } const upb::MessageDef *md = symtab->LookupMessage("google.protobuf.FileDescriptorSet"); ASSERT(md); + printf("name: %s\n", md->full_name()); upb::reffed_ptr encoder_handlers( upb::pb::Encoder::NewHandlers(md)); upb::reffed_ptr method( diff --git a/upb/def.c b/upb/def.c index e9acd43..ba6de50 100644 --- a/upb/def.c +++ b/upb/def.c @@ -189,25 +189,6 @@ static const char *shortdefname(const char *fullname) { } } -static bool upb_isoneof(const void *def) { - UPB_UNUSED(def); - return true; -} - -static bool upb_isfield(const void *def) { - UPB_UNUSED(def); - return true; -} - -static const upb_oneofdef *upb_trygetoneof(const void *def) { - return upb_isoneof(def) ? (const upb_oneofdef*)def : NULL; -} - -static const upb_fielddef *upb_trygetfield(const void *def) { - return upb_isfield(def) ? (const upb_fielddef*)def : NULL; -} - - /* All submessage fields are lower than all other fields. * Secondly, fields are increasing in order. */ uint32_t field_rank(const upb_fielddef *f) { @@ -274,7 +255,7 @@ static bool assign_msg_indices(upb_msgdef *m, upb_status *s) { { /* Verify that all selectors for the message are distinct. */ #define TRY(type) \ - if (upb_handlers_getselector(f, type, &sel)) upb_inttable_insert(&t, sel, v); + if (upb_handlers_getselector(f, type, &sel)) { upb_inttable_insert(&t, sel, v); } upb_inttable t; upb_value v; @@ -363,62 +344,6 @@ static void assign_msg_wellknowntype(upb_msgdef *m) { } } -#if 0 -bool _upb_def_validate(upb_def *const*defs, size_t n, upb_status *s) { - size_t i; - - /* First perform validation, in two passes so we can check that we have a - * transitive closure without needing to search. */ - for (i = 0; i < n; i++) { - upb_def *def = defs[i]; - if (upb_def_isfrozen(def)) { - /* Could relax this requirement if it's annoying. */ - upb_status_seterrmsg(s, "def is already frozen"); - goto err; - } else if (def->type == UPB_DEF_FIELD) { - upb_status_seterrmsg(s, "standalone fielddefs can not be frozen"); - goto err; - } else { - /* Set now to detect transitive closure in the second pass. */ - def->came_from_user = true; - - if (def->type == UPB_DEF_ENUM && - !upb_validate_enumdef(upb_dyncast_enumdef(def), s)) { - goto err; - } - } - } - - /* Second pass of validation. Also assign selector bases and indexes, and - * compact tables. */ - for (i = 0; i < n; i++) { - upb_def *def = defs[i]; - upb_msgdef *m = upb_dyncast_msgdef_mutable(def); - upb_enumdef *e = upb_dyncast_enumdef_mutable(def); - if (m) { - upb_inttable_compact(&m->itof); - if (!assign_msg_indices(m, s)) { - goto err; - } - assign_msg_wellknowntype(m); - /* m->well_known_type = UPB_WELLKNOWN_UNSPECIFIED; */ - } else if (e) { - upb_inttable_compact(&e->iton); - } - } - - return true; - -err: - for (i = 0; i < n; i++) { - upb_def *def = defs[i]; - def->came_from_user = false; - } - UPB_ASSERT(!(s && upb_ok(s))); - return false; -} -#endif - /* upb_enumdef ****************************************************************/ @@ -545,6 +470,10 @@ const char *upb_fielddef_name(const upb_fielddef *f) { return f->full_name; } +uint32_t upb_fielddef_selectorbase(const upb_fielddef *f) { + return f->selector_base; +} + size_t upb_fielddef_getjsonname(const upb_fielddef *f, char *buf, size_t len) { const char *name = upb_fielddef_name(f); size_t src, dst = 0; @@ -703,6 +632,14 @@ upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m) { return m->file->syntax; } +size_t upb_msgdef_selectorcount(const upb_msgdef *m) { + return m->selector_count; +} + +uint32_t upb_msgdef_submsgfieldcount(const upb_msgdef *m) { + return m->submsg_field_count; +} + const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i) { upb_value val; return upb_inttable_lookup32(&m->itof, i, &val) ? @@ -717,7 +654,7 @@ const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name, return NULL; } - return upb_trygetfield(upb_value_getptr(val)); + return unpack_def(val, UPB_DEFTYPE_FIELD); } const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name, @@ -728,7 +665,7 @@ const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name, return NULL; } - return upb_trygetoneof(upb_value_getptr(val)); + return unpack_def(val, UPB_DEFTYPE_ONEOF); } bool upb_msgdef_lookupname(const upb_msgdef *m, const char *name, size_t len, @@ -739,8 +676,8 @@ bool upb_msgdef_lookupname(const upb_msgdef *m, const char *name, size_t len, return false; } - *o = upb_trygetoneof(upb_value_getptr(val)); - *f = upb_trygetfield(upb_value_getptr(val)); + *o = unpack_def(val, UPB_DEFTYPE_ONEOF); + *f = unpack_def(val, UPB_DEFTYPE_FIELD); UPB_ASSERT((*o != NULL) ^ (*f != NULL)); /* Exactly one of the two should be set. */ return true; } @@ -780,7 +717,7 @@ bool upb_msg_field_done(const upb_msg_field_iter *iter) { } upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter) { - return (upb_fielddef*)upb_value_getptr(upb_inttable_iter_value(iter)); + return (upb_fielddef *)upb_value_getconstptr(upb_inttable_iter_value(iter)); } void upb_msg_field_iter_setdone(upb_msg_field_iter *iter) { @@ -791,7 +728,7 @@ void upb_msg_oneof_begin(upb_msg_oneof_iter *iter, const upb_msgdef *m) { upb_strtable_begin(iter, &m->ntof); /* We need to skip past any initial fields. */ while (!upb_strtable_done(iter) && - !upb_isoneof(upb_value_getptr(upb_strtable_iter_value(iter)))) { + !unpack_def(upb_strtable_iter_value(iter), UPB_DEFTYPE_ONEOF)) { upb_strtable_next(iter); } } @@ -801,7 +738,7 @@ void upb_msg_oneof_next(upb_msg_oneof_iter *iter) { do { upb_strtable_next(iter); } while (!upb_strtable_done(iter) && - !upb_isoneof(upb_value_getptr(upb_strtable_iter_value(iter)))); + !unpack_def(upb_strtable_iter_value(iter), UPB_DEFTYPE_ONEOF)); } bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter) { @@ -809,7 +746,7 @@ bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter) { } upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter) { - return (upb_oneofdef*)upb_value_getptr(upb_strtable_iter_value(iter)); + return (upb_oneofdef *)upb_value_getconstptr(upb_strtable_iter_value(iter)); } void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter) { @@ -965,7 +902,7 @@ const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym) { * this code is used to directly build defs from Ruby (for example) we do need * to validate important constraints like uniqueness of names and numbers. */ -#define CHK(x) if (!(x)) return false +#define CHK(x) if (!(x)) { __builtin_trap(); return false; } #define CHK_OOM(x) if (!(x)) { upb_upberr_setoom(ctx->status); return false; } typedef struct { @@ -977,8 +914,36 @@ typedef struct { upb_status *status; /* Record errors here. */ } symtab_addctx; -static const char *makefullname(const char *prefix, upb_stringview name) { - return NULL; +static char* strviewdup(const symtab_addctx *ctx, upb_stringview view) { + if (view.size == 0) { + return NULL; + } + return upb_strdup2(view.data, view.size, ctx->alloc); +} + +static bool streql(const char *a, size_t n, const char *b) { + return n == strlen(b) && memcmp(a, b, n) == 0; +} + +static bool streql_view(upb_stringview view, const char *b) { + return streql(view.data, view.size, b); +} + +static const char *makefullname(const symtab_addctx *ctx, const char *prefix, + upb_stringview name) { + if (prefix) { + /* ret = prefix + '.' + name; */ + size_t n = strlen(prefix); + char *ret = upb_malloc(ctx->alloc, n + name.size + 2); + CHK_OOM(ret); + strcpy(ret, prefix); + ret[n] = '.'; + memcpy(&ret[n + 1], name.data, name.size); + ret[n + 1 + name.size] = '\0'; + return ret; + } else { + return strviewdup(ctx, name); + } } static bool symtab_add(const symtab_addctx *ctx, const char *name, @@ -1050,13 +1015,13 @@ static bool create_oneofdef( o = (upb_oneofdef*)&m->oneofs[m->oneof_count++]; o->parent = m; - o->full_name = makefullname(m->full_name, name); + o->full_name = makefullname(ctx, m->full_name, name); CHK_OOM(symtab_add(ctx, o->full_name, pack_def(o, UPB_DEFTYPE_ONEOF))); CHK_OOM(upb_strtable_insert3(&m->ntof, name.data, name.size, upb_value_ptr(o), ctx->alloc)); - CHK_OOM(upb_inttable_init2(&o->itof, UPB_CTYPE_PTR, ctx->alloc)); - CHK_OOM(upb_strtable_init2(&o->ntof, UPB_CTYPE_PTR, ctx->alloc)); + CHK_OOM(upb_inttable_init2(&o->itof, UPB_CTYPE_CONSTPTR, ctx->alloc)); + CHK_OOM(upb_strtable_init2(&o->ntof, UPB_CTYPE_CONSTPTR, ctx->alloc)); return true; } @@ -1071,7 +1036,13 @@ static bool parse_default(const symtab_addctx *ctx, const char *str, size_t len, f->defaultval.sint = val; break; } - case UPB_TYPE_ENUM: + case UPB_TYPE_ENUM: { + const upb_enumdef *e = f->sub.enumdef; + int32_t val; + CHK(upb_enumdef_ntoi(e, str, len, &val)); + f->defaultval.sint = val; + break; + } case UPB_TYPE_INT64: { /* XXX: Need to write our own strtoll, since it's not available in c89. */ long long val = strtol(str, &end, 0); @@ -1106,9 +1077,9 @@ static bool parse_default(const symtab_addctx *ctx, const char *str, size_t len, break; } case UPB_TYPE_BOOL: { - if (strcmp(str, "false") == 0) { + if (streql(str, len, "false")) { f->defaultval.boolean = false; - } else if (strcmp(str, "true") == 0) { + } else if (streql(str, len, "true")) { f->defaultval.boolean = true; } else { return false; @@ -1134,8 +1105,6 @@ static bool create_fielddef( upb_fielddef *f; const google_protobuf_FieldOptions *options; upb_stringview name; - upb_value packed_v = pack_def(f, UPB_DEFTYPE_FIELD); - upb_value v = upb_value_constptr(f); const char *full_name; const char *shortname; uint32_t field_number; @@ -1147,7 +1116,7 @@ static bool create_fielddef( name = google_protobuf_FieldDescriptorProto_name(field_proto); CHK(upb_isident(name, false, ctx->status)); - full_name = makefullname(prefix, name); + full_name = makefullname(ctx, prefix, name); shortname = shortdefname(full_name); field_number = google_protobuf_FieldDescriptorProto_number(field_proto); @@ -1163,13 +1132,17 @@ static bool create_fielddef( f->msgdef = m; f->is_extension_ = false; + upb_value packed_v = pack_def(f, UPB_DEFTYPE_FIELD); + upb_value v = upb_value_constptr(f); + if (!upb_strtable_insert3(&m->ntof, name.data, name.size, packed_v, alloc)) { upb_status_seterrf(ctx->status, "duplicate field name (%s)", shortname); return false; } if (!upb_inttable_insert2(&m->itof, field_number, v, alloc)) { - upb_status_seterrf(ctx->status, "duplicate field number (%u)", f->number_); + upb_status_seterrf(ctx->status, "duplicate field number (%u)", + field_number); return false; } } else { @@ -1220,13 +1193,6 @@ static bool create_fielddef( f->oneof = NULL; } - if (google_protobuf_FieldDescriptorProto_has_default_value(field_proto)) { - upb_stringview defaultval = - google_protobuf_FieldDescriptorProto_default_value(field_proto); - CHK(parse_default(ctx, defaultval.data, defaultval.size, f)); - } - - if (google_protobuf_FieldDescriptorProto_has_options(field_proto)) { options = google_protobuf_FieldDescriptorProto_options(field_proto); f->lazy_ = google_protobuf_FieldOptions_lazy(options); @@ -1251,7 +1217,7 @@ static bool create_enumdef( CHK(upb_isident(name, false, ctx->status)); e = (upb_enumdef*)&ctx->file->enums[ctx->file->enum_count++]; - e->full_name = makefullname(prefix, name); + e->full_name = makefullname(ctx, prefix, name); CHK_OOM(symtab_add(ctx, e->full_name, pack_def(e, UPB_DEFTYPE_ENUM))); CHK_OOM(upb_strtable_init2(&e->ntoi, UPB_CTYPE_INT32, ctx->alloc)); @@ -1264,7 +1230,7 @@ static bool create_enumdef( for (i = 0; i < n; i++) { const google_protobuf_EnumValueDescriptorProto *value = values[i]; upb_stringview name = google_protobuf_EnumValueDescriptorProto_name(value); - char *name2 = upb_strdup2(name.data, name.size, ctx->alloc); + char *name2 = strviewdup(ctx, name); int32_t num = google_protobuf_EnumValueDescriptorProto_number(value); upb_value v = upb_value_int32(num); @@ -1273,10 +1239,13 @@ static bool create_enumdef( return false; } - CHK_OOM(name2 && upb_strtable_insert(&e->ntoi, name2, v)); + CHK_OOM(name2) + CHK_OOM( + upb_strtable_insert3(&e->ntoi, name2, strlen(name2), v, ctx->alloc)); if (!upb_inttable_lookup(&e->iton, num, NULL)) { - CHK_OOM(upb_inttable_insert(&e->iton, num, upb_value_cstr(name2))); + upb_value v = upb_value_cstr(name2); + CHK_OOM(upb_inttable_insert2(&e->iton, num, v, ctx->alloc)); } } @@ -1298,11 +1267,11 @@ static bool create_msgdef(const symtab_addctx *ctx, const char *prefix, CHK(upb_isident(name, false, ctx->status)); m = (upb_msgdef*)&ctx->file->msgs[ctx->file->msg_count++]; - m->full_name = makefullname(prefix, name); + m->full_name = makefullname(ctx, prefix, name); CHK_OOM(symtab_add(ctx, m->full_name, pack_def(m, UPB_DEFTYPE_MSG))); - CHK_OOM(upb_inttable_init2(&m->itof, UPB_CTYPE_PTR, ctx->alloc)); - CHK_OOM(upb_strtable_init2(&m->ntof, UPB_CTYPE_PTR, ctx->alloc)); + CHK_OOM(upb_inttable_init2(&m->itof, UPB_CTYPE_CONSTPTR, ctx->alloc)); + CHK_OOM(upb_strtable_init2(&m->ntof, UPB_CTYPE_CONSTPTR, ctx->alloc)); m->file = ctx->file; m->map_entry = false; @@ -1321,6 +1290,8 @@ static bool create_msgdef(const symtab_addctx *ctx, const char *prefix, } fields = google_protobuf_DescriptorProto_field(msg_proto, &n); + m->field_count = 0; + m->fields = upb_malloc(ctx->alloc, sizeof(*m->fields) * n); for (i = 0; i < n; i++) { CHK(create_fielddef(ctx, m->full_name, m, fields[i])); } @@ -1343,13 +1314,6 @@ static bool create_msgdef(const symtab_addctx *ctx, const char *prefix, return true; } -static char* strviewdup(const symtab_addctx *ctx, upb_stringview view) { - if (view.size == 0) { - return NULL; - } - return upb_strdup2(view.data, view.size, ctx->alloc); -} - typedef struct { int msg_count; int enum_count; @@ -1361,6 +1325,8 @@ static void count_types_in_msg(const google_protobuf_DescriptorProto *msg_proto, const google_protobuf_DescriptorProto *const *msgs; size_t i, n; + counts->msg_count++; + msgs = google_protobuf_DescriptorProto_nested_type(msg_proto, &n); for (i = 0; i < n; i++) { count_types_in_msg(msgs[i], counts); @@ -1394,24 +1360,46 @@ static void count_types_in_file( static bool resolve_fielddef(const symtab_addctx *ctx, const char *prefix, upb_fielddef *f) { upb_stringview name; + const google_protobuf_FieldDescriptorProto *field_proto = f->sub.unresolved; + if (f->is_extension_) { - name = google_protobuf_FieldDescriptorProto_extendee(f->sub.unresolved); + if (!google_protobuf_FieldDescriptorProto_has_extendee(field_proto)) { + upb_status_seterrf(ctx->status, + "extension for field '%s' had no extendee", + f->full_name); + return false; + } + + name = google_protobuf_FieldDescriptorProto_extendee(field_proto); f->msgdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_MSG); CHK(f->msgdef); } - name = google_protobuf_FieldDescriptorProto_type_name(f->sub.unresolved); + if ((upb_fielddef_issubmsg(f) || f->type_ == UPB_DESCRIPTOR_TYPE_ENUM) && + !google_protobuf_FieldDescriptorProto_has_type_name(field_proto)) { + upb_status_seterrf(ctx->status, "field '%s' is missing type name", + f->full_name); + return false; + } + + name = google_protobuf_FieldDescriptorProto_type_name(field_proto); + if (upb_fielddef_issubmsg(f)) { f->sub.msgdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_MSG); CHK(f->sub.msgdef); } else if (f->type_ == UPB_DESCRIPTOR_TYPE_ENUM) { f->sub.enumdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_ENUM); CHK(f->sub.enumdef); + } - if (!upb_enumdef_iton(f->sub.enumdef, f->defaultval.sint)) { - upb_status_seterrf(ctx->status, - "enum field %s has default (%d) not in the enum", - f->full_name, f->defaultval.sint); + /* Have to delay resolving of the default value until now because of the enum + * case, since enum defaults are specified with a label. */ + if (google_protobuf_FieldDescriptorProto_has_default_value(field_proto)) { + upb_stringview defaultval = + google_protobuf_FieldDescriptorProto_default_value(field_proto); + if (!parse_default(ctx, defaultval.data, defaultval.size, f)) { + upb_status_seterrf(ctx->status, "bad default '" UPB_STRINGVIEW_FORMAT "'", + UPB_STRINGVIEW_ARGS(defaultval)); return false; } } @@ -1429,8 +1417,7 @@ static bool build_filedef( const google_protobuf_FieldDescriptorProto *const *exts; const upb_stringview* strs; size_t i, n; - decl_counts counts; - upb_stringview syntax, package; + decl_counts counts = {0}; count_types_in_file(file_proto, &counts); @@ -1438,29 +1425,48 @@ static bool build_filedef( file->enums = upb_malloc(alloc, sizeof(*file->enums) * counts.enum_count); file->exts = upb_malloc(alloc, sizeof(*file->exts) * counts.ext_count); + CHK_OOM(counts.msg_count == 0 || file->msgs); + CHK_OOM(counts.enum_count == 0 || file->enums); + CHK_OOM(counts.ext_count == 0 || file->exts); + /* We increment these as defs are added. */ file->msg_count = 0; file->enum_count = 0; file->ext_count = 0; - package = google_protobuf_FileDescriptorProto_package(file_proto); - CHK(upb_isident(package, true, ctx->status)); + if (!google_protobuf_FileDescriptorProto_has_name(file_proto)) { + upb_status_seterrmsg(ctx->status, "File has no name"); + return false; + } file->name = strviewdup(ctx, google_protobuf_FileDescriptorProto_name(file_proto)); - file->package = strviewdup(ctx, package); file->phpprefix = NULL; file->phpnamespace = NULL; - syntax = google_protobuf_FileDescriptorProto_syntax(file_proto); + if (google_protobuf_FileDescriptorProto_has_package(file_proto)) { + upb_stringview package = + google_protobuf_FileDescriptorProto_package(file_proto); + CHK(upb_isident(package, true, ctx->status)); + file->package = strviewdup(ctx, package); + } else { + file->package = NULL; + } + + if (google_protobuf_FileDescriptorProto_has_syntax(file_proto)) { + upb_stringview syntax = + google_protobuf_FileDescriptorProto_syntax(file_proto); - if (upb_stringview_eql(syntax, upb_stringview_makez("proto2"))) { - file->syntax = UPB_SYNTAX_PROTO2; - } else if (upb_stringview_eql(syntax, upb_stringview_makez("proto3"))) { - file->syntax = UPB_SYNTAX_PROTO3; + if (streql_view(syntax, "proto2")) { + file->syntax = UPB_SYNTAX_PROTO2; + } else if (streql_view(syntax, "proto3")) { + file->syntax = UPB_SYNTAX_PROTO3; + } else { + upb_status_seterrf(ctx->status, "Invalid syntax '%s'", syntax); + return false; + } } else { - upb_status_seterrf(ctx->status, "Invalid syntax '%s'", syntax); - return false; + file->syntax = UPB_SYNTAX_PROTO2; } /* Read options. */ @@ -1475,7 +1481,7 @@ static bool build_filedef( /* Verify dependencies. */ strs = google_protobuf_FileDescriptorProto_dependency(file_proto, &n); file->deps = upb_malloc(alloc, sizeof(*file->deps) * n) ; - CHK_OOM(file->deps); + CHK_OOM(n == 0 || file->deps); for (i = 0; i < n; i++) { upb_stringview dep_name = strs[i]; @@ -1505,7 +1511,7 @@ static bool build_filedef( /* Create extensions. */ exts = google_protobuf_FileDescriptorProto_extension(file_proto, &n); file->exts = upb_malloc(alloc, sizeof(*file->exts) * n); - CHK_OOM(file->exts); + CHK_OOM(n == 0 || file->exts); for (i = 0; i < n; i++) { CHK(create_fielddef(ctx, file->package, NULL, exts[i])); } @@ -1518,7 +1524,7 @@ static bool build_filedef( for (i = 0; i < file->msg_count; i++) { const upb_msgdef *m = &file->msgs[i]; int j; - for (j = 0; i < m->field_count; i++) { + for (j = 0; j < m->field_count; j++) { CHK(resolve_fielddef(ctx, m->full_name, (upb_fielddef*)&m->fields[j])); } } @@ -1551,12 +1557,13 @@ bool upb_symtab_addfile(upb_symtab *s, upb_status *status) { upb_arena tmparena; upb_strtable addtab; - symtab_addctx ctx; upb_alloc *alloc = upb_arena_alloc(&s->arena); upb_filedef *file = upb_malloc(alloc, sizeof(*file)); bool ok; + symtab_addctx ctx; ctx.file = file; + ctx.symtab = s; ctx.alloc = alloc; ctx.tmp = upb_arena_alloc(&tmparena); ctx.addtab = &addtab; @@ -1565,7 +1572,7 @@ bool upb_symtab_addfile(upb_symtab *s, upb_arena_init(&tmparena); ok = file && - upb_strtable_init2(&addtab, UPB_CTYPE_PTR, ctx.tmp) && + upb_strtable_init2(&addtab, UPB_CTYPE_CONSTPTR, ctx.tmp) && build_filedef(&ctx, file, file_proto) && upb_symtab_addtotabs(s, &ctx, status); diff --git a/upb/generated_util.h b/upb/generated_util.h index 0a10099..88a983d 100644 --- a/upb/generated_util.h +++ b/upb/generated_util.h @@ -16,7 +16,7 @@ UPB_INLINE const void *_upb_array_accessor(const void *msg, size_t ofs, size_t *size) { const upb_array *arr = *PTR_AT(msg, ofs, const upb_array*); if (arr) { - if (size) *size = arr->size; + if (size) *size = arr->len; return arr->data; } else { if (size) *size = 0; @@ -28,7 +28,7 @@ UPB_INLINE void *_upb_array_mutable_accessor(void *msg, size_t ofs, size_t *size) { upb_array *arr = *PTR_AT(msg, ofs, upb_array*); if (arr) { - if (size) *size = arr->size; + if (size) *size = arr->len; return arr->data; } else { if (size) size = 0; @@ -66,7 +66,7 @@ UPB_INLINE void *_upb_array_resize_accessor(void *msg, size_t ofs, size_t size, } UPB_INLINE bool _upb_has_field(const void *msg, size_t idx) { - return (*PTR_AT(msg, idx / 8, const char) & (idx % 8)) != 0; + return (*PTR_AT(msg, idx / 8, const char) & (1 << (idx % 8))) != 0; } UPB_INLINE bool _upb_has_oneof_field(const void *msg, size_t case_ofs, int32_t num) { diff --git a/upb/msg.h b/upb/msg.h index 6002b41..6061470 100644 --- a/upb/msg.h +++ b/upb/msg.h @@ -98,6 +98,9 @@ UPB_INLINE bool upb_stringview_eql(upb_stringview a, upb_stringview b) { return a.size == b.size && memcmp(a.data, b.data, a.size) == 0; } +#define UPB_STRINGVIEW_FORMAT "%.*s" +#define UPB_STRINGVIEW_ARGS(view) view.size, view.data + #define UPB_STRINGVIEW_INIT(ptr, len) {ptr, len} diff --git a/upb/upb.c b/upb/upb.c index 97f6c61..cbf5afb 100644 --- a/upb/upb.c +++ b/upb/upb.c @@ -31,6 +31,7 @@ static void nullz(upb_status *status) { upb_errorspace upb_upberr = {"upb error"}; void upb_upberr_setoom(upb_status *status) { + __builtin_trap(); status->error_space_ = &upb_upberr; upb_status_seterrmsg(status, "Out of memory"); } diff --git a/upb/upb.h b/upb/upb.h index 2fb7a88..020022b 100644 --- a/upb/upb.h +++ b/upb/upb.h @@ -463,6 +463,7 @@ struct upb_alloc { UPB_INLINE void *upb_malloc(upb_alloc *alloc, size_t size) { UPB_ASSERT(alloc); + UPB_ASSERT(size < 65535); return alloc->func(alloc, NULL, 0, size); } diff --git a/upbc/generator.cc b/upbc/generator.cc index bb29041..68996a9 100644 --- a/upbc/generator.cc +++ b/upbc/generator.cc @@ -521,7 +521,7 @@ void WriteSource(const protobuf::FileDescriptor* file, Output& output) { } if (MessageLayout::HasHasbit(field)) { - presence = absl::StrCat(layout.GetHasbitIndex(field) + 1); + presence = absl::StrCat(layout.GetHasbitIndex(field)); } else if (field->containing_oneof()) { MessageLayout::Size case_offset = layout.GetOneofCaseOffset(field->containing_oneof()); diff --git a/upbc/message_layout.cc b/upbc/message_layout.cc index 5956424..f0a6872 100644 --- a/upbc/message_layout.cc +++ b/upbc/message_layout.cc @@ -130,7 +130,9 @@ void MessageLayout::PlaceNonOneofFields( int hasbit_count = 0; for (auto field : field_order) { if (HasHasbit(field)) { - hasbit_indexes_[field] = hasbit_count++; + // We don't use hasbit 0, so that 0 can indicate "no presence" in the + // table. This wastes one hasbit, but we don't worry about it for now. + hasbit_indexes_[field] = ++hasbit_count; } } -- cgit v1.2.3 From 377871f10403c7b4e1cc6f769b9443b5197aecc8 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sun, 16 Dec 2018 14:32:14 -0800 Subject: Got test_decoder working! --- BUILD | 15 + CMakeLists.txt | 1 + build_defs.bzl | 51 ++- tests/pb/test_decoder.cc | 91 +++--- tests/pb/test_decoder.proto | 42 +++ tools/dump_cinit.lua | 749 -------------------------------------------- tools/make_cmakelists.py | 3 + tools/upbc.lua | 91 ------ upb/def.c | 51 ++- upb/def.h | 10 +- upb/upb.h | 1 - upbc/generator.cc | 104 ++++++ 12 files changed, 317 insertions(+), 892 deletions(-) delete mode 100644 tools/dump_cinit.lua delete mode 100644 tools/upbc.lua (limited to 'upb') diff --git a/BUILD b/BUILD index 9b8513d..b3f397a 100644 --- a/BUILD +++ b/BUILD @@ -8,6 +8,7 @@ load( "make_shell_script", "upb_amalgamation", "upb_proto_library", + "upb_proto_reflection_library", ) # C/C++ rules ################################################################## @@ -167,10 +168,24 @@ cc_test( ], ) +proto_library( + name = "test_decoder_proto", + srcs = [ + "tests/pb/test_decoder.proto" + ] +) + +upb_proto_reflection_library( + name = "test_decoder_upbproto", + deps = ["test_decoder_proto"], + upbc = ":protoc-gen-upb", +) + cc_test( name = "test_decoder", srcs = ["tests/pb/test_decoder.cc"], deps = [ + ":test_decoder_upbproto", ":upb_pb", ":upb_test", ], diff --git a/CMakeLists.txt b/CMakeLists.txt index 6133c16..3439aac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -131,6 +131,7 @@ add_executable(test_decoder tests/pb/test_decoder.cc) add_test(NAME test_decoder COMMAND test_decoder) target_link_libraries(test_decoder + test_decoder_upbproto upb_pb upb_test) add_executable(test_encoder diff --git a/build_defs.bzl b/build_defs.bzl index 8251014..3867976 100644 --- a/build_defs.bzl +++ b/build_defs.bzl @@ -217,7 +217,7 @@ def _remove_up(string): return _remove_suffix(string, ".proto") -def _upb_proto_library_srcs_impl(ctx): +def _upb_proto_srcs_impl(ctx, suffix): sources = [] outs = [] include_dirs = {} @@ -225,14 +225,19 @@ def _upb_proto_library_srcs_impl(ctx): if hasattr(dep, 'proto'): for src in dep.proto.transitive_sources: sources.append(src) - include_dirs[_remove_suffix(src.path, _remove_up(src.short_path) + "." + src.extension)] = True - outs.append(ctx.actions.declare_file(_remove_up(src.short_path) + ".upb.h")) - outs.append(ctx.actions.declare_file(_remove_up(src.short_path) + ".upb.c")) - outdir = _remove_suffix(outs[-1].path, _remove_up(src.short_path) + ".upb.c") + include_dir = _remove_suffix(src.path, _remove_up(src.short_path) + "." + src.extension) + if include_dir: + include_dirs[include_dir] = True + outs.append(ctx.actions.declare_file(_remove_up(src.short_path) + suffix + ".h")) + outs.append(ctx.actions.declare_file(_remove_up(src.short_path) + suffix + ".c")) + outdir = _remove_suffix(outs[-1].path, _remove_up(src.short_path) + suffix + ".c") source_paths = [d.path for d in sources] include_args = ["-I" + root for root in include_dirs.keys()] + print(source_paths) + print(include_args) + ctx.actions.run( inputs = [ctx.executable.upbc] + sources, outputs = outs, @@ -243,6 +248,12 @@ def _upb_proto_library_srcs_impl(ctx): return [DefaultInfo(files = depset(outs))] +def _upb_proto_library_srcs_impl(ctx): + return _upb_proto_srcs_impl(ctx, ".upb") + +def _upb_proto_reflection_library_srcs_impl(ctx): + return _upb_proto_srcs_impl(ctx, ".upbdefs") + _upb_proto_library_srcs = rule( implementation = _upb_proto_library_srcs_impl, attrs = { @@ -272,3 +283,33 @@ def upb_proto_library(name, deps, upbc): deps = [":upb"], copts = ["-Ibazel-out/k8-fastbuild/bin"], ) + +_upb_proto_reflection_library_srcs = rule( + implementation = _upb_proto_reflection_library_srcs_impl, + attrs = { + "upbc": attr.label( + executable = True, + cfg = "host", + ), + "protoc": attr.label( + executable = True, + cfg = "host", + default = "@com_google_protobuf//:protoc", + ), + "deps": attr.label_list(), + } +) + +def upb_proto_reflection_library(name, deps, upbc): + srcs_rule = name + "_defsrcs.cc" + _upb_proto_reflection_library_srcs( + name = srcs_rule, + upbc = upbc, + deps = deps, + ) + native.cc_library( + name = name, + srcs = [":" + srcs_rule], + deps = [":upb"], + copts = ["-Ibazel-out/k8-fastbuild/bin"], + ) diff --git a/tests/pb/test_decoder.cc b/tests/pb/test_decoder.cc index a931779..d0e3fa3 100644 --- a/tests/pb/test_decoder.cc +++ b/tests/pb/test_decoder.cc @@ -36,6 +36,7 @@ #include "tests/test_util.h" #include "tests/upb_test.h" +#include "tests/pb/test_decoder.upbdefs.h" #ifdef AMALGAMATED #include "upb.h" @@ -387,7 +388,6 @@ void reg_subm(upb_handlers *h, uint32_t num) { ASSERT( h->SetStartSubMessageHandler(f, UpbBind(startsubmsg, new uint32_t(num)))); ASSERT(h->SetEndSubMessageHandler(f, UpbBind(endsubmsg, new uint32_t(num)))); - ASSERT(upb_handlers_setsubhandlers(h, f, h)); } void reg_str(upb_handlers *h, uint32_t num) { @@ -399,52 +399,60 @@ void reg_str(upb_handlers *h, uint32_t num) { ASSERT(h->SetStringHandler(f, UpbBind(value_string, new uint32_t(num)))); } -upb::reffed_ptr NewHandlers(TestMode mode) { - - upb::reffed_ptr h(upb::Handlers::New(NewMessageDef().get())); +struct HandlerRegisterData { + TestMode mode; +}; - if (mode == ALL_HANDLERS) { +void callback(const void *closure, upb_handlers *h) { + const HandlerRegisterData* data = + static_cast(closure); + if (data->mode == ALL_HANDLERS) { h->SetStartMessageHandler(UpbMakeHandler(startmsg)); h->SetEndMessageHandler(UpbMakeHandler(endmsg)); // Register handlers for each type. - reg(h.get(), UPB_DESCRIPTOR_TYPE_DOUBLE); - reg (h.get(), UPB_DESCRIPTOR_TYPE_FLOAT); - reg (h.get(), UPB_DESCRIPTOR_TYPE_INT64); - reg(h.get(), UPB_DESCRIPTOR_TYPE_UINT64); - reg (h.get(), UPB_DESCRIPTOR_TYPE_INT32); - reg(h.get(), UPB_DESCRIPTOR_TYPE_FIXED64); - reg(h.get(), UPB_DESCRIPTOR_TYPE_FIXED32); - reg (h.get(), UPB_DESCRIPTOR_TYPE_BOOL); - reg(h.get(), UPB_DESCRIPTOR_TYPE_UINT32); - reg (h.get(), UPB_DESCRIPTOR_TYPE_ENUM); - reg (h.get(), UPB_DESCRIPTOR_TYPE_SFIXED32); - reg (h.get(), UPB_DESCRIPTOR_TYPE_SFIXED64); - reg (h.get(), UPB_DESCRIPTOR_TYPE_SINT32); - reg (h.get(), UPB_DESCRIPTOR_TYPE_SINT64); - - reg_str(h.get(), UPB_DESCRIPTOR_TYPE_STRING); - reg_str(h.get(), UPB_DESCRIPTOR_TYPE_BYTES); - reg_str(h.get(), rep_fn(UPB_DESCRIPTOR_TYPE_STRING)); - reg_str(h.get(), rep_fn(UPB_DESCRIPTOR_TYPE_BYTES)); + reg(h, UPB_DESCRIPTOR_TYPE_DOUBLE); + reg (h, UPB_DESCRIPTOR_TYPE_FLOAT); + reg (h, UPB_DESCRIPTOR_TYPE_INT64); + reg(h, UPB_DESCRIPTOR_TYPE_UINT64); + reg (h, UPB_DESCRIPTOR_TYPE_INT32); + reg(h, UPB_DESCRIPTOR_TYPE_FIXED64); + reg(h, UPB_DESCRIPTOR_TYPE_FIXED32); + reg (h, UPB_DESCRIPTOR_TYPE_BOOL); + reg(h, UPB_DESCRIPTOR_TYPE_UINT32); + reg (h, UPB_DESCRIPTOR_TYPE_ENUM); + reg (h, UPB_DESCRIPTOR_TYPE_SFIXED32); + reg (h, UPB_DESCRIPTOR_TYPE_SFIXED64); + reg (h, UPB_DESCRIPTOR_TYPE_SINT32); + reg (h, UPB_DESCRIPTOR_TYPE_SINT64); + + reg_str(h, UPB_DESCRIPTOR_TYPE_STRING); + reg_str(h, UPB_DESCRIPTOR_TYPE_BYTES); + reg_str(h, rep_fn(UPB_DESCRIPTOR_TYPE_STRING)); + reg_str(h, rep_fn(UPB_DESCRIPTOR_TYPE_BYTES)); // Register submessage/group handlers that are self-recursive // to this type, eg: message M { optional M m = 1; } - reg_subm(h.get(), UPB_DESCRIPTOR_TYPE_MESSAGE); - reg_subm(h.get(), rep_fn(UPB_DESCRIPTOR_TYPE_MESSAGE)); - reg_subm(h.get(), UPB_DESCRIPTOR_TYPE_GROUP); - reg_subm(h.get(), rep_fn(UPB_DESCRIPTOR_TYPE_GROUP)); + reg_subm(h, UPB_DESCRIPTOR_TYPE_MESSAGE); + reg_subm(h, rep_fn(UPB_DESCRIPTOR_TYPE_MESSAGE)); + + if (h->message_def()->full_name() == std::string("DecoderTest")) { + reg_subm(h, UPB_DESCRIPTOR_TYPE_GROUP); + reg_subm(h, rep_fn(UPB_DESCRIPTOR_TYPE_GROUP)); + } // For NOP_FIELD we register no handlers, so we can pad a proto freely without // changing the output. } - - bool ok = h->Freeze(NULL); - ASSERT(ok); - - return h; } +upb::reffed_ptr NewHandlers(upb::SymbolTable* symtab, + TestMode mode) { + HandlerRegisterData handlerdata; + handlerdata.mode = mode; + return upb::Handlers::NewFrozen(DecoderTest_getmsgdef(symtab), callback, + &handlerdata); +} /* Running of test cases ******************************************************/ @@ -1132,14 +1140,11 @@ upb::reffed_ptr NewMethod( return cache.GetDecoderMethod(upb::pb::DecoderMethodOptions(dest_handlers)); } -void test_emptyhandlers(bool allowjit) { +void test_emptyhandlers(upb::SymbolTable* symtab, bool allowjit) { // Create an empty handlers to make sure that the decoder can handle empty // messages. - upb::reffed_ptr md = upb::MessageDef::New(); - ASSERT(md->set_full_name("Empty", NULL)); - ASSERT(md->Freeze(NULL)); - - upb::reffed_ptr h(upb::Handlers::New(md.get())); + const upb::MessageDef* md = Empty_getmsgdef(symtab); + upb::reffed_ptr h(upb::Handlers::New(md)); bool ok = h->Freeze(NULL); ASSERT(ok); upb::reffed_ptr method = @@ -1178,9 +1183,9 @@ upb::reffed_ptr method = void run_tests(bool use_jit) { upb::reffed_ptr method; upb::reffed_ptr handlers; - upb::SymbolTable symtab; + upb::SymbolTable* symtab = upb::SymbolTable::New(); - handlers = NewHandlers(test_mode); + handlers = NewHandlers(symtab, test_mode); global_handlers = handlers.get(); method = NewMethod(handlers.get(), use_jit); @@ -1191,7 +1196,9 @@ void run_tests(bool use_jit) { test_invalid(); test_valid(); - test_emptyhandlers(use_jit); + test_emptyhandlers(symtab, use_jit); + + upb::SymbolTable::Free(symtab); } void run_test_suite() { diff --git a/tests/pb/test_decoder.proto b/tests/pb/test_decoder.proto index 8197dea..e9fa6ad 100644 --- a/tests/pb/test_decoder.proto +++ b/tests/pb/test_decoder.proto @@ -5,6 +5,8 @@ enum TestEnum { FOO = 1; } +message Empty {} + message DecoderTest { optional double f_double = 1; optional float f_float = 2; @@ -62,6 +64,26 @@ message DecoderTest { optional sfixed64 f_sfixed64 = 16; optional sint32 f_sint32 = 17; optional sint64 f_sint64 = 18; + + optional string nop_field = 40; + + repeated double r_double = 536869912; + repeated float r_float = 536869913; + repeated int64 r_int64 = 536869914; + repeated uint64 r_uint64 = 536869915; + repeated int32 r_int32 = 536869916; + repeated fixed64 r_fixed64 = 536869917; + repeated fixed32 r_fixed32 = 536869918; + repeated bool r_bool = 536869919; + repeated string r_string = 536869920; + repeated DecoderTest r_message = 536869922; + repeated bytes r_bytes = 536869923; + repeated uint32 r_uint32 = 536869924; + repeated TestEnum r_enum = 536869925; + repeated sfixed32 r_sfixed32 = 536869926; + repeated sfixed64 r_sfixed64 = 536869927; + repeated sint32 r_sint32 = 536869928; + repeated sint64 r_sint64 = 536869929; } optional group R_group = 536869921 { @@ -82,5 +104,25 @@ message DecoderTest { optional sfixed64 f_sfixed64 = 16; optional sint32 f_sint32 = 17; optional sint64 f_sint64 = 18; + + optional string nop_field = 40; + + repeated double r_double = 536869912; + repeated float r_float = 536869913; + repeated int64 r_int64 = 536869914; + repeated uint64 r_uint64 = 536869915; + repeated int32 r_int32 = 536869916; + repeated fixed64 r_fixed64 = 536869917; + repeated fixed32 r_fixed32 = 536869918; + repeated bool r_bool = 536869919; + repeated string r_string = 536869920; + repeated DecoderTest r_message = 536869922; + repeated bytes r_bytes = 536869923; + repeated uint32 r_uint32 = 536869924; + repeated TestEnum r_enum = 536869925; + repeated sfixed32 r_sfixed32 = 536869926; + repeated sfixed64 r_sfixed64 = 536869927; + repeated sint32 r_sint32 = 536869928; + repeated sint64 r_sint64 = 536869929; } } diff --git a/tools/dump_cinit.lua b/tools/dump_cinit.lua deleted file mode 100644 index 93ee12e..0000000 --- a/tools/dump_cinit.lua +++ /dev/null @@ -1,749 +0,0 @@ ---[[ - - Routines for dumping internal data structures into C initializers - that can be compiled into a .o file. - ---]] - -local upbtable = require "upb.table" -local upb = require "upb" -local export = {} - --- A tiny little abstraction that decouples the dump_* functions from --- what they're writing to (appending to a string, writing to file I/O, etc). --- This could possibly matter since naive string building is O(n^2) in the --- number of appends. -function export.str_appender() - local str = "" - local function append(fmt, ...) - str = str .. string.format(fmt, ...) - end - local function get() - return str - end - return append, get -end - -function export.file_appender(file) - local f = file - local function append(fmt, ...) - f:write(string.format(fmt, ...)) - end - return append -end - -function handler_types(base) - local ret = {} - for k, _ in pairs(base) do - if string.find(k, "^" .. "HANDLER_") then - ret[#ret + 1] = k - end - end - return ret -end - -function octchar(num) - assert(num < 8) - local idx = num + 1 -- 1-based index - return string.sub("01234567", idx, idx) -end - -function c_escape(num) - assert(num < 256) - return string.format("\\%s%s%s", - octchar(math.floor(num / 64)), - octchar(math.floor(num / 8) % 8), - octchar(num % 8)); -end - --- const(f, label) -> UPB_LABEL_REPEATED, where f:label() == upb.LABEL_REPEATED -function const(obj, name, base) - local val = obj[name] - base = base or upb - - -- Support both f:label() and f.label. - if type(val) == "function" then - val = val(obj) - end - - for k, v in pairs(base) do - if v == val and string.find(k, "^" .. string.upper(name)) then - return "UPB_" .. k - end - end - assert(false, "Couldn't find UPB_" .. string.upper(name) .. - " constant for value: " .. val) -end - -function sortedkeys(tab) - arr = {} - for key in pairs(tab) do - arr[#arr + 1] = key - end - table.sort(arr) - return arr -end - -function sorted_defs(defs) - local sorted = {} - - for def in defs do - if def.type == deftype then - sorted[#sorted + 1] = def - end - end - - table.sort(sorted, - function(a, b) return a:full_name() < b:full_name() end) - - return sorted -end - -function constlist(pattern) - local ret = {} - for k, v in pairs(upb) do - if string.find(k, "^" .. pattern) then - ret[k] = v - end - end - return ret -end - -function boolstr(val) - if val == true then - return "true" - elseif val == false then - return "false" - else - assert(false, "Bad bool value: " .. tostring(val)) - end -end - ---[[ - - LinkTable: an object that tracks all linkable objects and their offsets to - facilitate linking. - ---]] - -local LinkTable = {} -function LinkTable:new(types) - local linktab = { - types = types, - table = {}, -- ptr -> {type, 0-based offset} - obj_arrays = {} -- Establishes the ordering for each object type - } - for type, _ in pairs(types) do - linktab.obj_arrays[type] = {} - end - setmetatable(linktab, {__index = LinkTable}) -- Inheritance - return linktab -end - --- Adds a new object to the sequence of objects of this type. -function LinkTable:add(objtype, ptr, obj) - obj = obj or ptr - assert(self.table[obj] == nil) - assert(self.types[objtype]) - local arr = self.obj_arrays[objtype] - self.table[ptr] = {objtype, #arr} - arr[#arr + 1] = obj -end - --- Returns a C symbol name for the given objtype and offset. -function LinkTable:csym(objtype, offset) - local typestr = assert(self.types[objtype]) - return string.format("%s[%d]", typestr, offset) -end - --- Returns the address of the given C object. -function LinkTable:addr(obj) - if obj == upbtable.NULL then - return "NULL" - else - local tabent = assert(self.table[obj], "unknown object: " .. tostring(obj)) - return "&" .. self:csym(tabent[1], tabent[2]) - end -end - --- Returns an array declarator indicating how many objects have been added. -function LinkTable:cdecl(objtype) - return self:csym(objtype, #self.obj_arrays[objtype]) -end - -function LinkTable:objs(objtype) - -- Return iterator function, allowing use as: - -- for obj in linktable:objs(type) do - -- -- ... - -- done - local array = self.obj_arrays[objtype] - local i = 0 - return function() - i = i + 1 - if array[i] then return array[i] end - end -end - -function LinkTable:empty(objtype) - return #self.obj_arrays[objtype] == 0 -end - ---[[ - - Dumper: an object that can dump C initializers for several constructs. - Uses a LinkTable to resolve references when necessary. - ---]] - -local Dumper = {} -function Dumper:new(linktab) - local obj = {linktab = linktab} - setmetatable(obj, {__index = Dumper}) -- Inheritance - return obj -end - --- Dumps a upb_tabval, eg: --- UPB_TABVALUE_INIT(5) -function Dumper:_value(val, upbtype) - if type(val) == "nil" then - return "UPB_TABVALUE_EMPTY_INIT" - elseif type(val) == "number" then - -- Use upbtype to disambiguate what kind of number it is. - if upbtype == upbtable.CTYPE_INT32 then - return string.format("UPB_TABVALUE_INT_INIT(%d)", val) - else - -- TODO(haberman): add support for these so we can properly support - -- default values. - error("Unsupported number type " .. upbtype) - end - elseif type(val) == "string" then - return string.format('UPB_TABVALUE_PTR_INIT("%s")', val) - else - -- We take this as an object reference that has an entry in the link table. - return string.format("UPB_TABVALUE_PTR_INIT(%s)", self.linktab:addr(val)) - end -end - --- Dumps a table key. -function Dumper:tabkey(key) - if type(key) == "nil" then - return "UPB_TABKEY_NONE" - elseif type(key) == "string" then - local len = #key - local len1 = c_escape(len % 256) - local len2 = c_escape(math.floor(len / 256) % 256) - local len3 = c_escape(math.floor(len / (256 * 256)) % 256) - local len4 = c_escape(math.floor(len / (256 * 256 * 256)) % 256) - return string.format('UPB_TABKEY_STR("%s", "%s", "%s", "%s", "%s")', - len1, len2, len3, len4, key) - else - return string.format("UPB_TABKEY_NUM(%d)", key) - end -end - --- Dumps a table entry. -function Dumper:tabent(ent) - local key = self:tabkey(ent.key) - local val = self:_value(ent.value, ent.valtype) - local next = self.linktab:addr(ent.next) - return string.format(' {%s, %s, %s},\n', key, val, next) -end - --- Dumps an inttable array entry. This is almost the same as value() above, --- except that nil values have a special value to indicate "empty". -function Dumper:arrayval(val) - if val.val then - return string.format(" %s,\n", self:_value(val.val, val.valtype)) - else - return " UPB_TABVALUE_EMPTY_INIT,\n" - end -end - --- Dumps an initializer for the given strtable/inttable (respectively). Its --- entries must have previously been added to the linktable. -function Dumper:strtable(t) - -- UPB_STRTABLE_INIT(count, mask, type, size_lg2, entries) - return string.format( - "UPB_STRTABLE_INIT(%d, %d, %s, %d, %s)", - t.count, t.mask, const(t, "ctype", upbtable) , t.size_lg2, - self.linktab:addr(t.entries[1].ptr)) -end - -function Dumper:inttable(t) - local lt = assert(self.linktab) - -- UPB_INTTABLE_INIT(count, mask, type, size_lg2, ent, a, asize, acount) - local entries = "NULL" - if #t.entries > 0 then - entries = lt:addr(t.entries[1].ptr) - end - return string.format( - "UPB_INTTABLE_INIT(%d, %d, %s, %d, %s, %s, %d, %d)", - t.count, t.mask, const(t, "ctype", upbtable), t.size_lg2, entries, - lt:addr(t.array[1].ptr), t.array_size, t.array_count) -end - --- A visitor for visiting all tables of a def. Used first to count entries --- and later to dump them. -local function gettables(def) - if def:def_type() == upb.DEF_MSG then - return {int = upbtable.msgdef_itof(def), str = upbtable.msgdef_ntof(def)} - elseif def:def_type() == upb.DEF_ENUM then - return {int = upbtable.enumdef_iton(def), str = upbtable.enumdef_ntoi(def)} - end -end - -local function emit_file_warning(filedef, append) - append('/* This file was generated by upbc (the upb compiler) from the input\n') - append(' * file:\n') - append(' *\n') - append(' * %s\n', filedef:name()) - append(' *\n') - append(' * Do not edit -- your changes will be discarded when the file is\n') - append(' * regenerated. */\n\n') -end - -local function join(...) - return table.concat({...}, ".") -end - -local function split(str) - local ret = {} - for word in string.gmatch(str, "%w+") do - table.insert(ret, word) - end - return ret -end - -local function to_cident(...) - return string.gsub(join(...), "[%./]", "_") -end - -local function to_preproc(...) - return string.upper(to_cident(...)) -end - --- Strips away last path element, ie: --- foo.Bar.Baz -> foo.Bar -local function remove_name(name) - local package_end = 0 - for i=1,string.len(name) do - if string.byte(name, i) == string.byte(".", 1) then - package_end = i - 1 - end - end - return string.sub(name, 1, package_end) -end - -local function start_namespace(package, append) - local package_components = split(package) - for _, component in ipairs(package_components) do - append("namespace %s {\n", component) - end -end - -local function end_namespace(package, append) - local package_components = split(package) - for i=#package_components,1,-1 do - append("} /* namespace %s */\n", package_components[i]) - end -end - -local function well_known_type(m) - local type_map = {} - type_map["google.protobuf.Duration"] = "UPB_WELLKNOWN_DURATION" - type_map["google.protobuf.Timestamp"] = "UPB_WELLKNOWN_TIMESTAMP" - type_map["google.protobuf.Value"] = "UPB_WELLKNOWN_VALUE" - type_map["google.protobuf.ListValue"] = "UPB_WELLKNOWN_LISTVALUE" - type_map["google.protobuf.Struct"] = "UPB_WELLKNOWN_STRUCT" - type_map["google.protobuf.DoubleValue"] = "UPB_WELLKNOWN_DOUBLEVALUE" - type_map["google.protobuf.FloatValue"] = "UPB_WELLKNOWN_FLOATVALUE" - type_map["google.protobuf.Int64Value"] = "UPB_WELLKNOWN_INT64VALUE" - type_map["google.protobuf.UInt64Value"] = "UPB_WELLKNOWN_UINT64VALUE" - type_map["google.protobuf.Int32Value"] = "UPB_WELLKNOWN_INT32VALUE" - type_map["google.protobuf.UInt32Value"] = "UPB_WELLKNOWN_UINT32VALUE" - type_map["google.protobuf.BoolValue"] = "UPB_WELLKNOWN_BOOLVALUE" - type_map["google.protobuf.StringValue"] = "UPB_WELLKNOWN_STRINGVALUE" - type_map["google.protobuf.BytesValue"] = "UPB_WELLKNOWN_BYTESVALUE" - local t = type_map[m:full_name()] - if (t == nil) then - t = "UPB_WELLKNOWN_UNSPECIFIED" - end - return t -end - ---[[ - - Top-level, exported dumper functions - ---]] - -local function dump_defs_c(filedef, append) - local defs = {} - for def in filedef:defs(upb.DEF_ANY) do - defs[#defs + 1] = def - if (def:def_type() == upb.DEF_MSG) then - for field in def:fields() do - defs[#defs + 1] = field - end - end - end - - -- Sort all defs by (type, name). - -- This gives us a linear ordering that we can use to create offsets into - -- shared arrays like REFTABLES, hash table entries, and arrays. - table.sort(defs, function(a, b) - if a:def_type() ~= b:def_type() then - return a:def_type() < b:def_type() - else - return a:full_name() < b:full_name() end - end - ) - - -- Perform pre-pass to build the link table. - local linktab = LinkTable:new{ - [upb.DEF_MSG] = "msgs", - [upb.DEF_FIELD] = "fields", - [upb.DEF_ENUM] = "enums", - intentries = "intentries", - strentries = "strentries", - arrays = "arrays", - } - local reftable_count = 0 - - for _, def in ipairs(defs) do - assert(def:is_frozen(), "can only dump frozen defs.") - linktab:add(def:def_type(), def) - reftable_count = reftable_count + 2 - local tables = gettables(def) - if tables then - for _, e in ipairs(tables.str.entries) do - linktab:add("strentries", e.ptr, e) - end - for _, e in ipairs(tables.int.entries) do - linktab:add("intentries", e.ptr, e) - end - for _, e in ipairs(tables.int.array) do - linktab:add("arrays", e.ptr, e) - end - end - end - - -- Emit forward declarations. - emit_file_warning(filedef, append) - append('#include "upb/def.h"\n') - append('#include "upb/structdefs.int.h"\n\n') - append("static const upb_msgdef %s;\n", linktab:cdecl(upb.DEF_MSG)) - append("static const upb_fielddef %s;\n", linktab:cdecl(upb.DEF_FIELD)) - if not linktab:empty(upb.DEF_ENUM) then - append("static const upb_enumdef %s;\n", linktab:cdecl(upb.DEF_ENUM)) - end - append("static const upb_tabent %s;\n", linktab:cdecl("strentries")) - if not linktab:empty("intentries") then - append("static const upb_tabent %s;\n", linktab:cdecl("intentries")) - end - append("static const upb_tabval %s;\n", linktab:cdecl("arrays")) - append("\n") - append("#ifdef UPB_DEBUG_REFS\n") - append("static upb_inttable reftables[%d];\n", reftable_count) - append("#endif\n") - append("\n") - - -- Emit defs. - local dumper = Dumper:new(linktab) - - local reftable = 0 - - append("static const upb_msgdef %s = {\n", linktab:cdecl(upb.DEF_MSG)) - for m in linktab:objs(upb.DEF_MSG) do - local tables = gettables(m) - -- UPB_MSGDEF_INIT(name, selector_count, submsg_field_count, itof, ntof, - -- refs, ref2s) - append(' UPB_MSGDEF_INIT("%s", %d, %d, %s, %s, %s, %s, %s,' .. - ' &reftables[%d], &reftables[%d]),\n', - m:full_name(), - upbtable.msgdef_selector_count(m), - upbtable.msgdef_submsg_field_count(m), - dumper:inttable(tables.int), - dumper:strtable(tables.str), - boolstr(m:_map_entry()), - const(m, "syntax"), - well_known_type(m), - reftable, reftable + 1) - reftable = reftable + 2 - end - append("};\n\n") - - append("static const upb_fielddef %s = {\n", linktab:cdecl(upb.DEF_FIELD)) - for f in linktab:objs(upb.DEF_FIELD) do - local subdef = "NULL" - if f:has_subdef() then - subdef = string.format("(const upb_def*)(%s)", linktab:addr(f:subdef())) - end - local intfmt - if f:type() == upb.TYPE_UINT32 or - f:type() == upb.TYPE_INT32 or - f:type() == upb.TYPE_UINT64 or - f:type() == upb.TYPE_INT64 then - intfmt = const(f, "intfmt") - else - intfmt = "0" - end - -- UPB_FIELDDEF_INIT(label, type, intfmt, tagdelim, is_extension, lazy, - -- packed, name, num, msgdef, subdef, selector_base, - -- index, -- default_value) - append(' UPB_FIELDDEF_INIT(%s, %s, %s, %s, %s, %s, %s, "%s", %d, %s, ' .. - '%s, %d, %d, {0},' .. -- TODO: support default value - '&reftables[%d], &reftables[%d]),\n', - const(f, "label"), const(f, "type"), intfmt, - boolstr(f:istagdelim()), boolstr(f:is_extension()), - boolstr(f:lazy()), boolstr(f:packed()), f:name(), f:number(), - linktab:addr(f:containing_type()), subdef, - upbtable.fielddef_selector_base(f), f:index(), - reftable, reftable + 1 - ) - reftable = reftable + 2 - end - append("};\n\n") - - if not linktab:empty(upb.DEF_ENUM) then - append("static const upb_enumdef %s = {\n", linktab:cdecl(upb.DEF_ENUM)) - for e in linktab:objs(upb.DEF_ENUM) do - local tables = gettables(e) - -- UPB_ENUMDEF_INIT(name, ntoi, iton, defaultval) - append(' UPB_ENUMDEF_INIT("%s", %s, %s, %d, ' .. - '&reftables[%d], &reftables[%d]),\n', - e:full_name(), - dumper:strtable(tables.str), - dumper:inttable(tables.int), - --e:default()) - 0, - reftable, reftable + 1) - reftable = reftable + 2 - end - append("};\n\n") - end - - append("static const upb_tabent %s = {\n", linktab:cdecl("strentries")) - for ent in linktab:objs("strentries") do - append(dumper:tabent(ent)) - end - append("};\n\n"); - - if not linktab:empty("intentries") then - append("static const upb_tabent %s = {\n", linktab:cdecl("intentries")) - for ent in linktab:objs("intentries") do - append(dumper:tabent(ent)) - end - append("};\n\n"); - end - - append("static const upb_tabval %s = {\n", linktab:cdecl("arrays")) - for ent in linktab:objs("arrays") do - append(dumper:arrayval(ent)) - end - append("};\n\n"); - - append("#ifdef UPB_DEBUG_REFS\n") - append("static upb_inttable reftables[%d] = {\n", reftable_count) - for i = 1,reftable_count do - append(" UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),\n") - end - append("};\n") - append("#endif\n\n") - - append("static const upb_msgdef *refm(const upb_msgdef *m, const void *owner) {\n") - append(" upb_msgdef_ref(m, owner);\n") - append(" return m;\n") - append("}\n\n") - append("static const upb_enumdef *refe(const upb_enumdef *e, const void *owner) {\n") - append(" upb_enumdef_ref(e, owner);\n") - append(" return e;\n") - append("}\n\n") - - append("/* Public API. */\n") - - for m in linktab:objs(upb.DEF_MSG) do - append("const upb_msgdef *upbdefs_%s_get(const void *owner)" .. - " { return refm(%s, owner); }\n", - to_cident(m:full_name()), linktab:addr(m)) - end - - append("\n") - - for e in linktab:objs(upb.DEF_ENUM) do - append("const upb_enumdef *upbdefs_%s_get(const void *owner)" .. - " { return refe(%s, owner); }\n", - to_cident(e:full_name()), linktab:addr(e)) - end - - return linktab -end - -local function dump_defs_for_type(format, defs, append) - local sorted = sorted_defs(defs) - for _, def in ipairs(sorted) do - append(format, to_cident(def:full_name()), def:full_name()) - end - - append("\n") -end - -local function make_children_map(file) - -- Maps file:package() or msg:full_name() -> children. - local map = {} - for def in file:defs(upb.DEF_ANY) do - local container = remove_name(def:full_name()) - if not map[container] then - map[container] = {} - end - table.insert(map[container], def) - end - - -- Sort all the lists for a consistent ordering. - for name, children in pairs(map) do - table.sort(children, function(a, b) return a:name() < b:name() end) - end - - return map -end - -local print_classes - -local function print_message(def, map, indent, append) - append("\n") - append("%sclass %s : public ::upb::reffed_ptr {\n", - indent, def:name()) - append("%s public:\n", indent) - append("%s %s(const ::upb::MessageDef* m, const void *ref_donor = NULL)\n", - indent, def:name()) - append("%s : reffed_ptr(m, ref_donor) {\n", indent) - append("%s UPB_ASSERT(upbdefs_%s_is(m));\n", indent, to_cident(def:full_name())) - append("%s }\n", indent) - append("\n") - append("%s static %s get() {\n", indent, def:name()) - append("%s const ::upb::MessageDef* m = upbdefs_%s_get(&m);\n", indent, to_cident(def:full_name())) - append("%s return %s(m, &m);\n", indent, def:name()) - append("%s }\n", indent) - -- TODO(haberman): add fields - print_classes(def:full_name(), map, indent .. " ", append) - append("%s};\n", indent) -end - -local function print_enum(def, indent, append) - append("\n") - append("%sclass %s : public ::upb::reffed_ptr {\n", - indent, def:name()) - append("%s public:\n", indent) - append("%s %s(const ::upb::EnumDef* e, const void *ref_donor = NULL)\n", - indent, def:name()) - append("%s : reffed_ptr(e, ref_donor) {\n", indent) - append("%s UPB_ASSERT(upbdefs_%s_is(e));\n", indent, to_cident(def:full_name())) - append("%s }\n", indent) - append("%s static %s get() {\n", indent, def:name()) - append("%s const ::upb::EnumDef* e = upbdefs_%s_get(&e);\n", indent, to_cident(def:full_name())) - append("%s return %s(e, &e);\n", indent, def:name()) - append("%s }\n", indent) - append("%s};\n", indent) -end - -function print_classes(name, map, indent, append) - if not map[name] then - return - end - - for _, def in ipairs(map[name]) do - if def:def_type() == upb.DEF_MSG then - print_message(def, map, indent, append) - elseif def:def_type() == upb.DEF_ENUM then - print_enum(def, indent, append) - else - error("Unknown def type for " .. def:full_name()) - end - end -end - -local function dump_defs_h(file, append, linktab) - local basename_preproc = to_preproc(file:name()) - append("/* This file contains accessors for a set of compiled-in defs.\n") - append(" * Note that unlike Google's protobuf, it does *not* define\n") - append(" * generated classes or any other kind of data structure for\n") - append(" * actually storing protobufs. It only contains *defs* which\n") - append(" * let you reflect over a protobuf *schema*.\n") - append(" */\n") - emit_file_warning(file, append) - append('#ifndef %s_UPB_H_\n', basename_preproc) - append('#define %s_UPB_H_\n\n', basename_preproc) - append('#include "upb/def.h"\n\n') - append('UPB_BEGIN_EXTERN_C\n\n') - - -- Dump C enums for proto enums. - - append("/* MessageDefs: call these functions to get a ref to a msgdef. */\n") - dump_defs_for_type( - "const upb_msgdef *upbdefs_%s_get(const void *owner);\n", - file:defs(upb.DEF_MSG), append) - - append("/* EnumDefs: call these functions to get a ref to an enumdef. */\n") - dump_defs_for_type( - "const upb_enumdef *upbdefs_%s_get(const void *owner);\n", - file:defs(upb.DEF_ENUM), append) - - append("/* Functions to test whether this message is of a certain type. */\n") - dump_defs_for_type( - "UPB_INLINE bool upbdefs_%s_is(const upb_msgdef *m) {\n" .. - " return strcmp(upb_msgdef_fullname(m), \"%s\") == 0;\n}\n", - file:defs(upb.DEF_MSG), append) - - append("/* Functions to test whether this enum is of a certain type. */\n") - dump_defs_for_type( - "UPB_INLINE bool upbdefs_%s_is(const upb_enumdef *e) {\n" .. - " return strcmp(upb_enumdef_fullname(e), \"%s\") == 0;\n}\n", - file:defs(upb.DEF_ENUM), append) - - append("\n") - - -- fields - local fields = {} - - for f in linktab:objs(upb.DEF_FIELD) do - local symname = f:containing_type():full_name() .. "." .. f:name() - fields[#fields + 1] = {to_cident(symname), f} - end - - table.sort(fields, function(a, b) return a[1] < b[1] end) - - append("/* Functions to get a fielddef from a msgdef reference. */\n") - for _, field in ipairs(fields) do - local f = field[2] - local msg_cident = to_cident(f:containing_type():full_name()) - local field_cident = to_cident(f:name()) - append("UPB_INLINE const upb_fielddef *upbdefs_%s_f_%s(const upb_msgdef *m) {" .. - " UPB_ASSERT(upbdefs_%s_is(m));" .. - " return upb_msgdef_itof(m, %d); }\n", - msg_cident, field_cident, msg_cident, f:number()) - end - - append('\nUPB_END_EXTERN_C\n\n') - - -- C++ wrappers. - local children_map = make_children_map(file) - - append("#ifdef __cplusplus\n\n") - append("namespace upbdefs {\n") - start_namespace(file:package(), append) - print_classes(file:package(), children_map, "", append) - append("\n") - end_namespace(file:package(), append) - append("} /* namespace upbdefs */\n\n") - append("#endif /* __cplusplus */\n") - - append("\n") - append('#endif /* %s_UPB_H_ */\n', basename_preproc) -end - -function export.dump_defs(filedef, append_h, append_c) - local linktab = dump_defs_c(filedef, append_c) - dump_defs_h(filedef, append_h, linktab) -end - -return export diff --git a/tools/make_cmakelists.py b/tools/make_cmakelists.py index b8f46b9..2b426b5 100755 --- a/tools/make_cmakelists.py +++ b/tools/make_cmakelists.py @@ -120,6 +120,9 @@ class BuildFileFunctions(object): def upb_proto_library(self, **kwargs): pass + def upb_proto_reflection_library(self, **kwargs): + pass + def genrule(self, **kwargs): pass diff --git a/tools/upbc.lua b/tools/upbc.lua deleted file mode 100644 index 80d2886..0000000 --- a/tools/upbc.lua +++ /dev/null @@ -1,91 +0,0 @@ ---[[ - - The upb compiler. It can write two different kinds of output - files: - - - generated code for a C API (foo.upb.h, foo.upb.c) - - (obsolete): definitions of upb defs. (foo.upbdefs.h, foo.upbdefs.c) - ---]] - -local dump_cinit = require "dump_cinit" -local upb = require "upb" - -local generate_upbdefs = false -local outdir = "." - -i = 1 -while i <= #arg do - argument = arg[i] - if argument.sub(argument, 1, 2) == "--" then - if argument == "--generate-upbdefs" then - generate_upbdefs = true - elseif argument == "--outdir" then - i = i + 1 - outdir = arg[i] - else - print("Unknown flag: " .. argument) - return 1 - end - else - if src then - print("upbc can only handle one input file at a time.") - return 1 - end - src = argument - end - i = i + 1 -end - -if not src then - print("Usage: upbc [--generate-upbdefs] ") - return 1 -end - -function strip_proto(filename) - return string.gsub(filename, '%.proto$','') -end - -local function open(filename) - local full_name = outdir .. "/" .. filename - return assert(io.open(full_name, "w"), "couldn't open " .. full_name) -end - --- Open input/output files. -local f = assert(io.open(src, "r"), "couldn't open input file " .. src) -local descriptor = f:read("*all") -local files = upb.load_descriptor(descriptor) -local symtab = upb.SymbolTable() - -for _, file in ipairs(files) do - symtab:add_file(file) - local outbase = strip_proto(file:name()) - - -- Write upbdefs. - - local hfilename = outbase .. ".upbdefs.h" - local cfilename = outbase .. ".upbdefs.c" - - if os.getenv("UPBC_VERBOSE") then - print("upbc:") - print(string.format(" source file=%s", src)) - print(string.format(" output file base=%s", outbase)) - print(string.format(" hfilename=%s", hfilename)) - print(string.format(" cfilename=%s", cfilename)) - end - - os.execute(string.format("mkdir -p `dirname %s`", outbase)) - - assert(generate_upbdefs) - -- Legacy generated defs. - local hfile = open(hfilename) - local cfile = open(cfilename) - - local happend = dump_cinit.file_appender(hfile) - local cappend = dump_cinit.file_appender(cfile) - - dump_cinit.dump_defs(file, happend, cappend) - - hfile:close() - cfile:close() -end diff --git a/upb/def.c b/upb/def.c index ba6de50..047684e 100644 --- a/upb/def.c +++ b/upb/def.c @@ -643,7 +643,7 @@ uint32_t upb_msgdef_submsgfieldcount(const upb_msgdef *m) { const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i) { upb_value val; return upb_inttable_lookup32(&m->itof, i, &val) ? - upb_value_getptr(val) : NULL; + upb_value_getconstptr(val) : NULL; } const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name, @@ -1128,12 +1128,14 @@ static bool create_fielddef( if (m) { /* direct message field. */ + upb_value v, packed_v; + f = (upb_fielddef*)&m->fields[m->field_count++]; f->msgdef = m; f->is_extension_ = false; - upb_value packed_v = pack_def(f, UPB_DEFTYPE_FIELD); - upb_value v = upb_value_constptr(f); + packed_v = pack_def(f, UPB_DEFTYPE_FIELD); + v = upb_value_constptr(f); if (!upb_strtable_insert3(&m->ntof, name.data, name.size, packed_v, alloc)) { upb_status_seterrf(ctx->status, "duplicate field name (%s)", shortname); @@ -1580,5 +1582,48 @@ bool upb_symtab_addfile(upb_symtab *s, return ok; } +/* Include here since we want most of this file to be stdio-free. */ +#include + +bool _upb_symtab_loaddefinit(upb_symtab *s, const upb_def_init *init) { + /* Since this function should never fail (it would indicate a bug in upb) we + * print errors to stderr instead of returning error status to the user. */ + upb_def_init **deps = init->deps; + google_protobuf_FileDescriptorProto *file; + upb_arena arena; + upb_status status = UPB_STATUS_INIT; + + if (upb_strtable_lookup(&s->files, init->filename, NULL)) { + return true; + } + + for (; *deps; deps++) { + if (!_upb_symtab_loaddefinit(s, *deps)) goto err; + } + + upb_arena_init(&arena); + file = google_protobuf_FileDescriptorProto_parsenew(init->descriptor, &arena); + + if (!file) { + upb_status_seterrf( + &status, + "Failed to parse compiled-in descriptor for file '%s'. This should " + "never happen.", + init->filename); + goto err; + } + + if (!upb_symtab_addfile(s, file, &status)) goto err; + + upb_arena_uninit(&arena); + return true; + +err: + fprintf(stderr, "Error loading compiled-in descriptor: %s\n", + upb_status_errmsg(&status)); + upb_arena_uninit(&arena); + return false; +} + #undef CHK #undef CHK_OOM diff --git a/upb/def.h b/upb/def.h index 4fe6d04..e6fdf21 100644 --- a/upb/def.h +++ b/upb/def.h @@ -659,7 +659,6 @@ const upb_filedef *upb_filedef_dep(const upb_filedef *f, int i); const upb_msgdef *upb_filedef_msg(const upb_filedef *f, int i); const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i); - UPB_END_EXTERN_C #ifdef __cplusplus @@ -707,6 +706,15 @@ bool upb_symtab_addfile(upb_symtab *s, const google_protobuf_FileDescriptorProto* file, upb_status *status); +/* For generated code only: loads a generated descriptor. */ +typedef struct upb_def_init { + struct upb_def_init **deps; + const char *filename; + upb_stringview descriptor; +} upb_def_init; + +bool _upb_symtab_loaddefinit(upb_symtab *s, const upb_def_init *init); + UPB_END_EXTERN_C #ifdef __cplusplus diff --git a/upb/upb.h b/upb/upb.h index 020022b..2fb7a88 100644 --- a/upb/upb.h +++ b/upb/upb.h @@ -463,7 +463,6 @@ struct upb_alloc { UPB_INLINE void *upb_malloc(upb_alloc *alloc, size_t size) { UPB_ASSERT(alloc); - UPB_ASSERT(size < 65535); return alloc->func(alloc, NULL, 0, size); } diff --git a/upbc/generator.cc b/upbc/generator.cc index 68996a9..3b4f6ac 100644 --- a/upbc/generator.cc +++ b/upbc/generator.cc @@ -8,6 +8,7 @@ #include "absl/strings/substitute.h" #include "google/protobuf/compiler/code_generator.h" #include "google/protobuf/descriptor.h" +#include "google/protobuf/descriptor.pb.h" #include "google/protobuf/io/zero_copy_stream.h" #include "upbc/generator.h" @@ -32,6 +33,14 @@ static std::string SourceFilename(std::string proto_filename) { return StripExtension(proto_filename) + ".upb.c"; } +static std::string DefHeaderFilename(std::string proto_filename) { + return StripExtension(proto_filename) + ".upbdefs.h"; +} + +static std::string DefSourceFilename(std::string proto_filename) { + return StripExtension(proto_filename) + ".upbdefs.c"; +} + class Output { public: Output(protobuf::io::ZeroCopyOutputStream* stream) : stream_(stream) {} @@ -165,6 +174,10 @@ std::string ToCIdent(absl::string_view str) { return absl::StrReplaceAll(str, {{".", "_"}, {"/", "_"}}); } +std::string DefInitSymbol(const protobuf::FileDescriptor *file) { + return ToCIdent(file->name()) + "_upbdefinit"; +} + std::string ToPreproc(absl::string_view str) { return absl::AsciiStrToUpper(ToCIdent(str)); } @@ -558,6 +571,91 @@ void WriteSource(const protobuf::FileDescriptor* file, Output& output) { output("\n"); } +void GenerateMessageDefAccessor(const protobuf::Descriptor* d, Output& output) { + output("UPB_INLINE const upb_msgdef *$0_getmsgdef(upb_symtab *s) {\n", + ToCIdent(d->full_name())); + output(" _upb_symtab_loaddefinit(s, &$0);\n", DefInitSymbol(d->file())); + output(" return upb_symtab_lookupmsg(s, \"$0\");\n", d->full_name()); + output("}\n"); + output("\n"); + + for (int i = 0; i < d->nested_type_count(); i++) { + GenerateMessageDefAccessor(d->nested_type(i), output); + } +} + +void WriteDefHeader(const protobuf::FileDescriptor* file, Output& output) { + EmitFileWarning(file, output); + + output("extern upb_def_init $0;\n", DefInitSymbol(file)); + + for (int i = 0; i < file->message_type_count(); i++) { + GenerateMessageDefAccessor(file->message_type(i), output); + } +} + +// Escape C++ trigraphs by escaping question marks to \? +std::string EscapeTrigraphs(absl::string_view to_escape) { + return absl::StrReplaceAll(to_escape, {{"?", "\\?"}}); +} + +void WriteDefSource(const protobuf::FileDescriptor* file, Output& output) { + EmitFileWarning(file, output); + + output("#include \"upb/def.h\"\n"); + output("\n"); + + for (int i = 0; i < file->dependency_count(); i++) { + output("extern upb_def_init $0;\n", DefInitSymbol(file->dependency(i))); + } + + protobuf::FileDescriptorProto file_proto; + file->CopyTo(&file_proto); + std::string file_data; + file_proto.SerializeToString(&file_data); + + output("static const char descriptor[$0] =\n", file_data.size()); + + { + if (file_data.size() > 65535) { + // Workaround for MSVC: "Error C1091: compiler limit: string exceeds + // 65535 bytes in length". Declare a static array of chars rather than + // use a string literal. Only write 25 bytes per line. + static const int kBytesPerLine = 25; + output("{ "); + for (int i = 0; i < file_data.size();) { + for (int j = 0; j < kBytesPerLine && i < file_data.size(); ++i, ++j) { + output("'$0', ", absl::CEscape(file_data.substr(i, 1))); + } + output("\n"); + } + output("'\\0' }"); // null-terminate + } else { + // Only write 40 bytes per line. + static const int kBytesPerLine = 40; + for (int i = 0; i < file_data.size(); i += kBytesPerLine) { + output( + "\"$0\"\n", + EscapeTrigraphs(absl::CEscape(file_data.substr(i, kBytesPerLine)))); + } + } + output(";\n"); + } + + output("static upb_def_init *deps[$0] = {\n", file->dependency_count() + 1); + for (int i = 0; i < file->dependency_count(); i++) { + output(" $0,\n", DefInitSymbol(file->dependency(i))); + } + output(" NULL\n"); + output("};\n"); + + output("upb_def_init $0 = {\n", DefInitSymbol(file)); + output(" deps,\n"); + output(" \"$0\",\n", file->name()); + output(" UPB_STRINGVIEW_INIT(descriptor, $0)\n", file_data.size()); + output("};\n"); +} + bool Generator::Generate(const protobuf::FileDescriptor* file, const std::string& parameter, protoc::GeneratorContext* context, @@ -568,6 +666,12 @@ bool Generator::Generate(const protobuf::FileDescriptor* file, Output c_output(context->Open(SourceFilename(file->name()))); WriteSource(file, c_output); + Output h_def_output(context->Open(DefHeaderFilename(file->name()))); + WriteDefHeader(file, h_def_output); + + Output c_def_output(context->Open(DefSourceFilename(file->name()))); + WriteDefSource(file, c_def_output); + return true; } -- cgit v1.2.3 From 5e958a8c055872ecd5b7f7d00f48212de5711ae5 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Mon, 17 Dec 2018 09:16:46 -0800 Subject: test_json is working! --- BUILD | 21 +++- CMakeLists.txt | 1 + tests/json/test.upbdefs.c | 313 ---------------------------------------------- tests/json/test.upbdefs.h | 238 ----------------------------------- tests/json/test_json.cc | 13 +- tests/test_handlers.c | 11 +- upb/def.c | 2 +- upbc/generator.cc | 14 ++- 8 files changed, 44 insertions(+), 569 deletions(-) delete mode 100644 tests/json/test.upbdefs.c delete mode 100644 tests/json/test.upbdefs.h (limited to 'upb') diff --git a/BUILD b/BUILD index b3f397a..0f4231a 100644 --- a/BUILD +++ b/BUILD @@ -159,10 +159,17 @@ cc_test( ], ) +upb_proto_reflection_library( + name = "descriptor_upbproto", + deps = ["descriptor_proto"], + upbc = ":protoc-gen-upb", +) + cc_test( name = "test_handlers", srcs = ["tests/test_handlers.c"], deps = [ + ":descriptor_upbproto", ":upb_pb", ":upb_test", ], @@ -221,14 +228,24 @@ cc_test( ], ) +proto_library( + name = "test_json_proto", + srcs = ["tests/json/test.proto"], +) + +upb_proto_reflection_library( + name = "test_json_upbproto", + deps = ["test_json_proto"], + upbc = ":protoc-gen-upb", +) + cc_test( name = "test_json", srcs = [ - "tests/json/test.upbdefs.c", - "tests/json/test.upbdefs.h", "tests/json/test_json.cc", ], deps = [ + ":test_json_upbproto", ":upb_json", ":upb_test", ], diff --git a/CMakeLists.txt b/CMakeLists.txt index 3439aac..54dcf24 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -125,6 +125,7 @@ add_executable(test_handlers tests/test_handlers.c) add_test(NAME test_handlers COMMAND test_handlers) target_link_libraries(test_handlers + descriptor_upbproto upb_pb upb_test) add_executable(test_decoder diff --git a/tests/json/test.upbdefs.c b/tests/json/test.upbdefs.c deleted file mode 100644 index 933fe5f..0000000 --- a/tests/json/test.upbdefs.c +++ /dev/null @@ -1,313 +0,0 @@ -/* This file was generated by upbc (the upb compiler) from the input - * file: - * - * tests/json/test.proto - * - * Do not edit -- your changes will be discarded when the file is - * regenerated. */ - -#include "upb/def.h" -#include "upb/structdefs.int.h" - -static const upb_msgdef msgs[8]; -static const upb_fielddef fields[37]; -static const upb_enumdef enums[1]; -static const upb_tabent strentries[64]; -static const upb_tabval arrays[49]; - -#ifdef UPB_DEBUG_REFS -static upb_inttable reftables[92]; -#endif - -static const upb_msgdef msgs[8] = { - UPB_MSGDEF_INIT("upb.test.json.SubMessage", 4, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[0], 2, 1), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[0]), false, UPB_SYNTAX_PROTO3, UPB_WELLKNOWN_UNSPECIFIED, &reftables[0], &reftables[1]), - UPB_MSGDEF_INIT("upb.test.json.TestMessage", 73, 8, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[2], 26, 24), UPB_STRTABLE_INIT(24, 31, UPB_CTYPE_PTR, 5, &strentries[4]), false, UPB_SYNTAX_PROTO3, UPB_WELLKNOWN_UNSPECIFIED, &reftables[2], &reftables[3]), - UPB_MSGDEF_INIT("upb.test.json.TestMessage.MapBoolStringEntry", 7, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[28], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[36]), true, UPB_SYNTAX_PROTO3, UPB_WELLKNOWN_UNSPECIFIED, &reftables[4], &reftables[5]), - UPB_MSGDEF_INIT("upb.test.json.TestMessage.MapInt32StringEntry", 7, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[31], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[40]), true, UPB_SYNTAX_PROTO3, UPB_WELLKNOWN_UNSPECIFIED, &reftables[6], &reftables[7]), - UPB_MSGDEF_INIT("upb.test.json.TestMessage.MapStringBoolEntry", 7, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[34], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[44]), true, UPB_SYNTAX_PROTO3, UPB_WELLKNOWN_UNSPECIFIED, &reftables[8], &reftables[9]), - UPB_MSGDEF_INIT("upb.test.json.TestMessage.MapStringInt32Entry", 7, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[37], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[48]), true, UPB_SYNTAX_PROTO3, UPB_WELLKNOWN_UNSPECIFIED, &reftables[10], &reftables[11]), - UPB_MSGDEF_INIT("upb.test.json.TestMessage.MapStringMsgEntry", 8, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[40], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[52]), true, UPB_SYNTAX_PROTO3, UPB_WELLKNOWN_UNSPECIFIED, &reftables[12], &reftables[13]), - UPB_MSGDEF_INIT("upb.test.json.TestMessage.MapStringStringEntry", 9, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[43], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[56]), true, UPB_SYNTAX_PROTO3, UPB_WELLKNOWN_UNSPECIFIED, &reftables[14], &reftables[15]), -}; - -static const upb_fielddef fields[37] = { - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "foo", 1, &msgs[0], NULL, 3, 0, {0},&reftables[16], &reftables[17]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "key", 1, &msgs[7], NULL, 3, 0, {0},&reftables[18], &reftables[19]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "key", 1, &msgs[3], NULL, 3, 0, {0},&reftables[20], &reftables[21]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "key", 1, &msgs[2], NULL, 3, 0, {0},&reftables[22], &reftables[23]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "key", 1, &msgs[5], NULL, 3, 0, {0},&reftables[24], &reftables[25]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "key", 1, &msgs[4], NULL, 3, 0, {0},&reftables[26], &reftables[27]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "key", 1, &msgs[6], NULL, 5, 1, {0},&reftables[28], &reftables[29]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "map_bool_string", 22, &msgs[1], (const upb_def*)(&msgs[2]), 23, 4, {0},&reftables[30], &reftables[31]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "map_int32_string", 21, &msgs[1], (const upb_def*)(&msgs[3]), 20, 3, {0},&reftables[32], &reftables[33]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "map_string_bool", 24, &msgs[1], (const upb_def*)(&msgs[4]), 29, 6, {0},&reftables[34], &reftables[35]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "map_string_int32", 23, &msgs[1], (const upb_def*)(&msgs[5]), 26, 5, {0},&reftables[36], &reftables[37]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "map_string_msg", 25, &msgs[1], (const upb_def*)(&msgs[6]), 32, 7, {0},&reftables[38], &reftables[39]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "map_string_string", 20, &msgs[1], (const upb_def*)(&msgs[7]), 17, 2, {0},&reftables[40], &reftables[41]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "optional_bool", 7, &msgs[1], NULL, 43, 14, {0},&reftables[42], &reftables[43]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BYTES, 0, false, false, false, false, "optional_bytes", 6, &msgs[1], NULL, 40, 13, {0},&reftables[44], &reftables[45]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "optional_enum", 9, &msgs[1], (const upb_def*)(&enums[0]), 44, 15, {0},&reftables[46], &reftables[47]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "optional_int32", 1, &msgs[1], NULL, 33, 8, {0},&reftables[48], &reftables[49]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT64, UPB_INTFMT_VARIABLE, false, false, false, false, "optional_int64", 2, &msgs[1], NULL, 34, 9, {0},&reftables[50], &reftables[51]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "optional_msg", 8, &msgs[1], (const upb_def*)(&msgs[0]), 11, 0, {0},&reftables[52], &reftables[53]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "optional_string", 5, &msgs[1], NULL, 37, 12, {0},&reftables[54], &reftables[55]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "optional_uint32", 3, &msgs[1], NULL, 35, 10, {0},&reftables[56], &reftables[57]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT64, UPB_INTFMT_VARIABLE, false, false, false, false, "optional_uint64", 4, &msgs[1], NULL, 36, 11, {0},&reftables[58], &reftables[59]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_BOOL, 0, false, false, false, false, "repeated_bool", 17, &msgs[1], NULL, 69, 22, {0},&reftables[60], &reftables[61]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_BYTES, 0, false, false, false, false, "repeated_bytes", 16, &msgs[1], NULL, 64, 21, {0},&reftables[62], &reftables[63]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_ENUM, 0, false, false, false, false, "repeated_enum", 19, &msgs[1], (const upb_def*)(&enums[0]), 72, 23, {0},&reftables[64], &reftables[65]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "repeated_int32", 11, &msgs[1], NULL, 47, 16, {0},&reftables[66], &reftables[67]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_INT64, UPB_INTFMT_VARIABLE, false, false, false, false, "repeated_int64", 12, &msgs[1], NULL, 50, 17, {0},&reftables[68], &reftables[69]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "repeated_msg", 18, &msgs[1], (const upb_def*)(&msgs[0]), 14, 1, {0},&reftables[70], &reftables[71]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_STRING, 0, false, false, false, false, "repeated_string", 15, &msgs[1], NULL, 59, 20, {0},&reftables[72], &reftables[73]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_UINT32, UPB_INTFMT_VARIABLE, false, false, false, false, "repeated_uint32", 13, &msgs[1], NULL, 53, 18, {0},&reftables[74], &reftables[75]), - UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_UINT64, UPB_INTFMT_VARIABLE, false, false, false, false, "repeated_uint64", 14, &msgs[1], NULL, 56, 19, {0},&reftables[76], &reftables[77]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "value", 2, &msgs[6], (const upb_def*)(&msgs[0]), 4, 0, {0},&reftables[78], &reftables[79]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "value", 2, &msgs[7], NULL, 6, 1, {0},&reftables[80], &reftables[81]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "value", 2, &msgs[5], NULL, 6, 1, {0},&reftables[82], &reftables[83]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "value", 2, &msgs[2], NULL, 4, 1, {0},&reftables[84], &reftables[85]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "value", 2, &msgs[4], NULL, 6, 1, {0},&reftables[86], &reftables[87]), - UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "value", 2, &msgs[3], NULL, 4, 1, {0},&reftables[88], &reftables[89]), -}; - -static const upb_enumdef enums[1] = { - UPB_ENUMDEF_INIT("upb.test.json.MyEnum", UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_INT32, 2, &strentries[60]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[46], 3, 3), 0, &reftables[90], &reftables[91]), -}; - -static const upb_tabent strentries[64] = { - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\003", "\000", "\000", "\000", "foo"), UPB_TABVALUE_PTR_INIT(&fields[0]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "repeated_enum"), UPB_TABVALUE_PTR_INIT(&fields[24]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "optional_enum"), UPB_TABVALUE_PTR_INIT(&fields[15]), &strentries[31]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\016", "\000", "\000", "\000", "optional_int32"), UPB_TABVALUE_PTR_INIT(&fields[16]), NULL}, - {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "repeated_bool"), UPB_TABVALUE_PTR_INIT(&fields[22]), NULL}, - {UPB_TABKEY_STR("\016", "\000", "\000", "\000", "repeated_bytes"), UPB_TABVALUE_PTR_INIT(&fields[23]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\016", "\000", "\000", "\000", "optional_int64"), UPB_TABVALUE_PTR_INIT(&fields[17]), NULL}, - {UPB_TABKEY_STR("\017", "\000", "\000", "\000", "optional_string"), UPB_TABVALUE_PTR_INIT(&fields[19]), NULL}, - {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "optional_bool"), UPB_TABVALUE_PTR_INIT(&fields[13]), &strentries[30]}, - {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "map_int32_string"), UPB_TABVALUE_PTR_INIT(&fields[8]), NULL}, - {UPB_TABKEY_STR("\016", "\000", "\000", "\000", "optional_bytes"), UPB_TABVALUE_PTR_INIT(&fields[14]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "optional_msg"), UPB_TABVALUE_PTR_INIT(&fields[18]), NULL}, - {UPB_TABKEY_STR("\016", "\000", "\000", "\000", "repeated_int32"), UPB_TABVALUE_PTR_INIT(&fields[25]), &strentries[35]}, - {UPB_TABKEY_STR("\017", "\000", "\000", "\000", "repeated_uint32"), UPB_TABVALUE_PTR_INIT(&fields[29]), NULL}, - {UPB_TABKEY_STR("\017", "\000", "\000", "\000", "map_bool_string"), UPB_TABVALUE_PTR_INIT(&fields[7]), NULL}, - {UPB_TABKEY_STR("\017", "\000", "\000", "\000", "optional_uint64"), UPB_TABVALUE_PTR_INIT(&fields[21]), NULL}, - {UPB_TABKEY_STR("\017", "\000", "\000", "\000", "optional_uint32"), UPB_TABVALUE_PTR_INIT(&fields[20]), &strentries[32]}, - {UPB_TABKEY_STR("\017", "\000", "\000", "\000", "map_string_bool"), UPB_TABVALUE_PTR_INIT(&fields[9]), NULL}, - {UPB_TABKEY_STR("\016", "\000", "\000", "\000", "repeated_int64"), UPB_TABVALUE_PTR_INIT(&fields[26]), &strentries[34]}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\016", "\000", "\000", "\000", "map_string_msg"), UPB_TABVALUE_PTR_INIT(&fields[11]), NULL}, - {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "map_string_int32"), UPB_TABVALUE_PTR_INIT(&fields[10]), NULL}, - {UPB_TABKEY_STR("\021", "\000", "\000", "\000", "map_string_string"), UPB_TABVALUE_PTR_INIT(&fields[12]), NULL}, - {UPB_TABKEY_STR("\017", "\000", "\000", "\000", "repeated_uint64"), UPB_TABVALUE_PTR_INIT(&fields[30]), NULL}, - {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "repeated_msg"), UPB_TABVALUE_PTR_INIT(&fields[27]), NULL}, - {UPB_TABKEY_STR("\017", "\000", "\000", "\000", "repeated_string"), UPB_TABVALUE_PTR_INIT(&fields[28]), NULL}, - {UPB_TABKEY_STR("\003", "\000", "\000", "\000", "key"), UPB_TABVALUE_PTR_INIT(&fields[3]), NULL}, - {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "value"), UPB_TABVALUE_PTR_INIT(&fields[34]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\003", "\000", "\000", "\000", "key"), UPB_TABVALUE_PTR_INIT(&fields[2]), NULL}, - {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "value"), UPB_TABVALUE_PTR_INIT(&fields[36]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\003", "\000", "\000", "\000", "key"), UPB_TABVALUE_PTR_INIT(&fields[5]), NULL}, - {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "value"), UPB_TABVALUE_PTR_INIT(&fields[35]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\003", "\000", "\000", "\000", "key"), UPB_TABVALUE_PTR_INIT(&fields[4]), NULL}, - {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "value"), UPB_TABVALUE_PTR_INIT(&fields[33]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\003", "\000", "\000", "\000", "key"), UPB_TABVALUE_PTR_INIT(&fields[6]), NULL}, - {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "value"), UPB_TABVALUE_PTR_INIT(&fields[31]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\003", "\000", "\000", "\000", "key"), UPB_TABVALUE_PTR_INIT(&fields[1]), NULL}, - {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "value"), UPB_TABVALUE_PTR_INIT(&fields[32]), NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL}, - {UPB_TABKEY_STR("\001", "\000", "\000", "\000", "A"), UPB_TABVALUE_INT_INIT(0), NULL}, - {UPB_TABKEY_STR("\001", "\000", "\000", "\000", "B"), UPB_TABVALUE_INT_INIT(1), &strentries[63]}, - {UPB_TABKEY_STR("\001", "\000", "\000", "\000", "C"), UPB_TABVALUE_INT_INIT(2), NULL}, -}; - -static const upb_tabval arrays[49] = { - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[0]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[16]), - UPB_TABVALUE_PTR_INIT(&fields[17]), - UPB_TABVALUE_PTR_INIT(&fields[20]), - UPB_TABVALUE_PTR_INIT(&fields[21]), - UPB_TABVALUE_PTR_INIT(&fields[19]), - UPB_TABVALUE_PTR_INIT(&fields[14]), - UPB_TABVALUE_PTR_INIT(&fields[13]), - UPB_TABVALUE_PTR_INIT(&fields[18]), - UPB_TABVALUE_PTR_INIT(&fields[15]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[25]), - UPB_TABVALUE_PTR_INIT(&fields[26]), - UPB_TABVALUE_PTR_INIT(&fields[29]), - UPB_TABVALUE_PTR_INIT(&fields[30]), - UPB_TABVALUE_PTR_INIT(&fields[28]), - UPB_TABVALUE_PTR_INIT(&fields[23]), - UPB_TABVALUE_PTR_INIT(&fields[22]), - UPB_TABVALUE_PTR_INIT(&fields[27]), - UPB_TABVALUE_PTR_INIT(&fields[24]), - UPB_TABVALUE_PTR_INIT(&fields[12]), - UPB_TABVALUE_PTR_INIT(&fields[8]), - UPB_TABVALUE_PTR_INIT(&fields[7]), - UPB_TABVALUE_PTR_INIT(&fields[10]), - UPB_TABVALUE_PTR_INIT(&fields[9]), - UPB_TABVALUE_PTR_INIT(&fields[11]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[3]), - UPB_TABVALUE_PTR_INIT(&fields[34]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[2]), - UPB_TABVALUE_PTR_INIT(&fields[36]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[5]), - UPB_TABVALUE_PTR_INIT(&fields[35]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[4]), - UPB_TABVALUE_PTR_INIT(&fields[33]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[6]), - UPB_TABVALUE_PTR_INIT(&fields[31]), - UPB_TABVALUE_EMPTY_INIT, - UPB_TABVALUE_PTR_INIT(&fields[1]), - UPB_TABVALUE_PTR_INIT(&fields[32]), - UPB_TABVALUE_PTR_INIT("A"), - UPB_TABVALUE_PTR_INIT("B"), - UPB_TABVALUE_PTR_INIT("C"), -}; - -#ifdef UPB_DEBUG_REFS -static upb_inttable reftables[92] = { - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), - UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR), -}; -#endif - -static const upb_msgdef *refm(const upb_msgdef *m, const void *owner) { - upb_msgdef_ref(m, owner); - return m; -} - -static const upb_enumdef *refe(const upb_enumdef *e, const void *owner) { - upb_enumdef_ref(e, owner); - return e; -} - -/* Public API. */ -const upb_msgdef *upbdefs_upb_test_json_SubMessage_get(const void *owner) { return refm(&msgs[0], owner); } -const upb_msgdef *upbdefs_upb_test_json_TestMessage_get(const void *owner) { return refm(&msgs[1], owner); } -const upb_msgdef *upbdefs_upb_test_json_TestMessage_MapBoolStringEntry_get(const void *owner) { return refm(&msgs[2], owner); } -const upb_msgdef *upbdefs_upb_test_json_TestMessage_MapInt32StringEntry_get(const void *owner) { return refm(&msgs[3], owner); } -const upb_msgdef *upbdefs_upb_test_json_TestMessage_MapStringBoolEntry_get(const void *owner) { return refm(&msgs[4], owner); } -const upb_msgdef *upbdefs_upb_test_json_TestMessage_MapStringInt32Entry_get(const void *owner) { return refm(&msgs[5], owner); } -const upb_msgdef *upbdefs_upb_test_json_TestMessage_MapStringMsgEntry_get(const void *owner) { return refm(&msgs[6], owner); } -const upb_msgdef *upbdefs_upb_test_json_TestMessage_MapStringStringEntry_get(const void *owner) { return refm(&msgs[7], owner); } - -const upb_enumdef *upbdefs_upb_test_json_MyEnum_get(const void *owner) { return refe(&enums[0], owner); } diff --git a/tests/json/test.upbdefs.h b/tests/json/test.upbdefs.h deleted file mode 100644 index 065c001..0000000 --- a/tests/json/test.upbdefs.h +++ /dev/null @@ -1,238 +0,0 @@ -/* This file contains accessors for a set of compiled-in defs. - * Note that unlike Google's protobuf, it does *not* define - * generated classes or any other kind of data structure for - * actually storing protobufs. It only contains *defs* which - * let you reflect over a protobuf *schema*. - */ -/* This file was generated by upbc (the upb compiler) from the input - * file: - * - * tests/json/test.proto - * - * Do not edit -- your changes will be discarded when the file is - * regenerated. */ - -#ifndef TESTS_JSON_TEST_PROTO_UPB_H_ -#define TESTS_JSON_TEST_PROTO_UPB_H_ - -#include "upb/def.h" - -UPB_BEGIN_EXTERN_C - -/* MessageDefs: call these functions to get a ref to a msgdef. */ -const upb_msgdef *upbdefs_upb_test_json_SubMessage_get(const void *owner); -const upb_msgdef *upbdefs_upb_test_json_TestMessage_get(const void *owner); -const upb_msgdef *upbdefs_upb_test_json_TestMessage_MapBoolStringEntry_get(const void *owner); -const upb_msgdef *upbdefs_upb_test_json_TestMessage_MapInt32StringEntry_get(const void *owner); -const upb_msgdef *upbdefs_upb_test_json_TestMessage_MapStringBoolEntry_get(const void *owner); -const upb_msgdef *upbdefs_upb_test_json_TestMessage_MapStringInt32Entry_get(const void *owner); -const upb_msgdef *upbdefs_upb_test_json_TestMessage_MapStringMsgEntry_get(const void *owner); -const upb_msgdef *upbdefs_upb_test_json_TestMessage_MapStringStringEntry_get(const void *owner); - -/* EnumDefs: call these functions to get a ref to an enumdef. */ -const upb_enumdef *upbdefs_upb_test_json_MyEnum_get(const void *owner); - -/* Functions to test whether this message is of a certain type. */ -UPB_INLINE bool upbdefs_upb_test_json_SubMessage_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "upb.test.json.SubMessage") == 0; -} -UPB_INLINE bool upbdefs_upb_test_json_TestMessage_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "upb.test.json.TestMessage") == 0; -} -UPB_INLINE bool upbdefs_upb_test_json_TestMessage_MapBoolStringEntry_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "upb.test.json.TestMessage.MapBoolStringEntry") == 0; -} -UPB_INLINE bool upbdefs_upb_test_json_TestMessage_MapInt32StringEntry_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "upb.test.json.TestMessage.MapInt32StringEntry") == 0; -} -UPB_INLINE bool upbdefs_upb_test_json_TestMessage_MapStringBoolEntry_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "upb.test.json.TestMessage.MapStringBoolEntry") == 0; -} -UPB_INLINE bool upbdefs_upb_test_json_TestMessage_MapStringInt32Entry_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "upb.test.json.TestMessage.MapStringInt32Entry") == 0; -} -UPB_INLINE bool upbdefs_upb_test_json_TestMessage_MapStringMsgEntry_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "upb.test.json.TestMessage.MapStringMsgEntry") == 0; -} -UPB_INLINE bool upbdefs_upb_test_json_TestMessage_MapStringStringEntry_is(const upb_msgdef *m) { - return strcmp(upb_msgdef_fullname(m), "upb.test.json.TestMessage.MapStringStringEntry") == 0; -} - -/* Functions to test whether this enum is of a certain type. */ -UPB_INLINE bool upbdefs_upb_test_json_MyEnum_is(const upb_enumdef *e) { - return strcmp(upb_enumdef_fullname(e), "upb.test.json.MyEnum") == 0; -} - - -/* Functions to get a fielddef from a msgdef reference. */ -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_SubMessage_f_foo(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_SubMessage_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_MapBoolStringEntry_f_key(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_MapBoolStringEntry_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_MapBoolStringEntry_f_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_MapBoolStringEntry_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_MapInt32StringEntry_f_key(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_MapInt32StringEntry_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_MapInt32StringEntry_f_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_MapInt32StringEntry_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_MapStringBoolEntry_f_key(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_MapStringBoolEntry_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_MapStringBoolEntry_f_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_MapStringBoolEntry_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_MapStringInt32Entry_f_key(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_MapStringInt32Entry_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_MapStringInt32Entry_f_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_MapStringInt32Entry_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_MapStringMsgEntry_f_key(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_MapStringMsgEntry_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_MapStringMsgEntry_f_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_MapStringMsgEntry_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_MapStringStringEntry_f_key(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_MapStringStringEntry_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_MapStringStringEntry_f_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_MapStringStringEntry_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_map_bool_string(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 22); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_map_int32_string(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 21); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_map_string_bool(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 24); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_map_string_int32(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 23); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_map_string_msg(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 25); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_map_string_string(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 20); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_optional_bool(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 7); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_optional_bytes(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 6); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_optional_enum(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 9); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_optional_int32(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 1); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_optional_int64(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 2); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_optional_msg(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 8); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_optional_string(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 5); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_optional_uint32(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 3); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_optional_uint64(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 4); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_repeated_bool(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 17); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_repeated_bytes(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 16); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_repeated_enum(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 19); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_repeated_int32(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 11); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_repeated_int64(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 12); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_repeated_msg(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 18); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_repeated_string(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 15); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_repeated_uint32(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 13); } -UPB_INLINE const upb_fielddef *upbdefs_upb_test_json_TestMessage_f_repeated_uint64(const upb_msgdef *m) { UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); return upb_msgdef_itof(m, 14); } - -UPB_END_EXTERN_C - -#ifdef __cplusplus - -namespace upbdefs { -namespace upb { -namespace test { -namespace json { - -class MyEnum : public ::upb::reffed_ptr { - public: - MyEnum(const ::upb::EnumDef* e, const void *ref_donor = NULL) - : reffed_ptr(e, ref_donor) { - UPB_ASSERT(upbdefs_upb_test_json_MyEnum_is(e)); - } - static MyEnum get() { - const ::upb::EnumDef* e = upbdefs_upb_test_json_MyEnum_get(&e); - return MyEnum(e, &e); - } -}; - -class SubMessage : public ::upb::reffed_ptr { - public: - SubMessage(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_upb_test_json_SubMessage_is(m)); - } - - static SubMessage get() { - const ::upb::MessageDef* m = upbdefs_upb_test_json_SubMessage_get(&m); - return SubMessage(m, &m); - } -}; - -class TestMessage : public ::upb::reffed_ptr { - public: - TestMessage(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_upb_test_json_TestMessage_is(m)); - } - - static TestMessage get() { - const ::upb::MessageDef* m = upbdefs_upb_test_json_TestMessage_get(&m); - return TestMessage(m, &m); - } - - class MapBoolStringEntry : public ::upb::reffed_ptr { - public: - MapBoolStringEntry(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_upb_test_json_TestMessage_MapBoolStringEntry_is(m)); - } - - static MapBoolStringEntry get() { - const ::upb::MessageDef* m = upbdefs_upb_test_json_TestMessage_MapBoolStringEntry_get(&m); - return MapBoolStringEntry(m, &m); - } - }; - - class MapInt32StringEntry : public ::upb::reffed_ptr { - public: - MapInt32StringEntry(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_upb_test_json_TestMessage_MapInt32StringEntry_is(m)); - } - - static MapInt32StringEntry get() { - const ::upb::MessageDef* m = upbdefs_upb_test_json_TestMessage_MapInt32StringEntry_get(&m); - return MapInt32StringEntry(m, &m); - } - }; - - class MapStringBoolEntry : public ::upb::reffed_ptr { - public: - MapStringBoolEntry(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_upb_test_json_TestMessage_MapStringBoolEntry_is(m)); - } - - static MapStringBoolEntry get() { - const ::upb::MessageDef* m = upbdefs_upb_test_json_TestMessage_MapStringBoolEntry_get(&m); - return MapStringBoolEntry(m, &m); - } - }; - - class MapStringInt32Entry : public ::upb::reffed_ptr { - public: - MapStringInt32Entry(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_upb_test_json_TestMessage_MapStringInt32Entry_is(m)); - } - - static MapStringInt32Entry get() { - const ::upb::MessageDef* m = upbdefs_upb_test_json_TestMessage_MapStringInt32Entry_get(&m); - return MapStringInt32Entry(m, &m); - } - }; - - class MapStringMsgEntry : public ::upb::reffed_ptr { - public: - MapStringMsgEntry(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_upb_test_json_TestMessage_MapStringMsgEntry_is(m)); - } - - static MapStringMsgEntry get() { - const ::upb::MessageDef* m = upbdefs_upb_test_json_TestMessage_MapStringMsgEntry_get(&m); - return MapStringMsgEntry(m, &m); - } - }; - - class MapStringStringEntry : public ::upb::reffed_ptr { - public: - MapStringStringEntry(const ::upb::MessageDef* m, const void *ref_donor = NULL) - : reffed_ptr(m, ref_donor) { - UPB_ASSERT(upbdefs_upb_test_json_TestMessage_MapStringStringEntry_is(m)); - } - - static MapStringStringEntry get() { - const ::upb::MessageDef* m = upbdefs_upb_test_json_TestMessage_MapStringStringEntry_get(&m); - return MapStringStringEntry(m, &m); - } - }; -}; - -} /* namespace json */ -} /* namespace test */ -} /* namespace upb */ -} /* namespace upbdefs */ - -#endif /* __cplusplus */ - -#endif /* TESTS_JSON_TEST_PROTO_UPB_H_ */ diff --git a/tests/json/test_json.cc b/tests/json/test_json.cc index 815d292..98bf59e 100644 --- a/tests/json/test_json.cc +++ b/tests/json/test_json.cc @@ -203,12 +203,13 @@ void test_json_roundtrip_message(const char* json_src, // Starts with a message in JSON format, parses and directly serializes again, // and compares the result. void test_json_roundtrip() { - upb::reffed_ptr md( - upbdefs::upb::test::json::TestMessage::get()); + upb::SymbolTable* symtab = upb::SymbolTable::New(); + const upb::MessageDef* md = upb_test_json_TestMessage_getmsgdef(symtab); + ASSERT(md); upb::reffed_ptr serialize_handlers( - upb::json::Printer::NewHandlers(md.get(), false)); + upb::json::Printer::NewHandlers(md, false)); upb::reffed_ptr parser_method( - upb::json::ParserMethod::New(md.get())); + upb::json::ParserMethod::New(md)); for (const TestCase* test_case = kTestRoundtripMessages; test_case->input != NULL; test_case++) { @@ -224,7 +225,7 @@ void test_json_roundtrip() { } } - serialize_handlers = upb::json::Printer::NewHandlers(md.get(), true); + serialize_handlers = upb::json::Printer::NewHandlers(md, true); for (const TestCase* test_case = kTestRoundtripMessagesPreserve; test_case->input != NULL; test_case++) { @@ -239,6 +240,8 @@ void test_json_roundtrip() { i); } } + + upb::SymbolTable::Free(symtab); } extern "C" { diff --git a/tests/test_handlers.c b/tests/test_handlers.c index fe6fb82..2b19cab 100644 --- a/tests/test_handlers.c +++ b/tests/test_handlers.c @@ -1,9 +1,9 @@ -#include "upb/handlers.h" -#include "upb/descriptor/descriptor.upbdefs.h" -#include "upb_test.h" #include #include +#include "google/protobuf/descriptor.upbdefs.h" +#include "upb/handlers.h" +#include "upb_test.h" static bool startmsg(void *c, const void *hd) { UPB_UNUSED(c); @@ -13,9 +13,9 @@ static bool startmsg(void *c, const void *hd) { static void test_error() { /* Test creating handlers of a static msgdef. */ - const upb_msgdef *m = upbdefs_google_protobuf_DescriptorProto_get(&m); + upb_symtab *s = upb_symtab_new(); + const upb_msgdef *m = google_protobuf_DescriptorProto_getmsgdef(s); upb_handlers *h = upb_handlers_new(m, &h); - upb_msgdef_unref(m, &m); /* Attempt to set the same handler twice causes error. */ ASSERT(upb_ok(upb_handlers_status(h))); @@ -31,6 +31,7 @@ static void test_error() { ASSERT(upb_handlers_isfrozen(h)); upb_handlers_unref(h, &h); + upb_symtab_free(s); } int run_tests(int argc, char *argv[]) { diff --git a/upb/def.c b/upb/def.c index 047684e..c10394e 100644 --- a/upb/def.c +++ b/upb/def.c @@ -467,7 +467,7 @@ bool upb_fielddef_packed(const upb_fielddef *f) { } const char *upb_fielddef_name(const upb_fielddef *f) { - return f->full_name; + return shortdefname(f->full_name); } uint32_t upb_fielddef_selectorbase(const upb_fielddef *f) { diff --git a/upbc/generator.cc b/upbc/generator.cc index 3b4f6ac..d8ec831 100644 --- a/upbc/generator.cc +++ b/upbc/generator.cc @@ -587,7 +587,11 @@ void GenerateMessageDefAccessor(const protobuf::Descriptor* d, Output& output) { void WriteDefHeader(const protobuf::FileDescriptor* file, Output& output) { EmitFileWarning(file, output); + output("#include \"upb/def.h\"\n"); + output("\n"); + output("extern upb_def_init $0;\n", DefInitSymbol(file)); + output("\n"); for (int i = 0; i < file->message_type_count(); i++) { GenerateMessageDefAccessor(file->message_type(i), output); @@ -621,10 +625,10 @@ void WriteDefSource(const protobuf::FileDescriptor* file, Output& output) { // Workaround for MSVC: "Error C1091: compiler limit: string exceeds // 65535 bytes in length". Declare a static array of chars rather than // use a string literal. Only write 25 bytes per line. - static const int kBytesPerLine = 25; + static const size_t kBytesPerLine = 25; output("{ "); - for (int i = 0; i < file_data.size();) { - for (int j = 0; j < kBytesPerLine && i < file_data.size(); ++i, ++j) { + for (size_t i = 0; i < file_data.size();) { + for (size_t j = 0; j < kBytesPerLine && i < file_data.size(); ++i, ++j) { output("'$0', ", absl::CEscape(file_data.substr(i, 1))); } output("\n"); @@ -632,8 +636,8 @@ void WriteDefSource(const protobuf::FileDescriptor* file, Output& output) { output("'\\0' }"); // null-terminate } else { // Only write 40 bytes per line. - static const int kBytesPerLine = 40; - for (int i = 0; i < file_data.size(); i += kBytesPerLine) { + static const size_t kBytesPerLine = 40; + for (size_t i = 0; i < file_data.size(); i += kBytesPerLine) { output( "\"$0\"\n", EscapeTrigraphs(absl::CEscape(file_data.substr(i, kBytesPerLine)))); -- cgit v1.2.3 From 9dd2446531827a9848983a2dbe3bfb612ed7047e Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Mon, 17 Dec 2018 10:07:00 -0800 Subject: test_cpp is working! --- BUILD | 44 ++---- build_defs.bzl | 3 - tests/test_cpp.cc | 414 +++++------------------------------------------------- upb/def.c | 8 +- 4 files changed, 57 insertions(+), 412 deletions(-) (limited to 'upb') diff --git a/BUILD b/BUILD index 0f4231a..bf988f9 100644 --- a/BUILD +++ b/BUILD @@ -209,10 +209,24 @@ cc_test( ], ) +proto_library( + name = "test_cpp_proto", + srcs = [ + "tests/test_cpp.proto" + ] +) + +upb_proto_reflection_library( + name = "test_cpp_upbproto", + deps = ["test_cpp_proto"], + upbc = ":protoc-gen-upb", +) + cc_test( name = "test_cpp", srcs = ["tests/test_cpp.cc"], deps = [ + ":test_cpp_upbproto", ":upb", ":upb_pb", ":upb_test", @@ -331,17 +345,6 @@ lua_library( strip_prefix = "upb/bindings/lua", ) -lua_library( - name = "lua/upbc_lib", - srcs = [ - "tools/dump_cinit.lua", - ], - luadeps = [ - "lua/upb", - ], - strip_prefix = "tools", -) - # Lua tests. ################################################################### lua_test( @@ -358,14 +361,6 @@ lua_test( # upb compiler ################################################################# -lua_binary( - name = "lua_upbc", - luadeps = [ - "lua/upbc_lib", - ], - luamain = "tools/upbc.lua", -) - cc_library( name = "upbc_generator", hdrs = ["upbc/generator.h"], @@ -491,17 +486,6 @@ genrule( cmd = "cp $< $@", ) -#genrule( -# name = "generated_json_test_proto_upbdefs", -# srcs = ["generated/tests/json/test.proto.pb"], -# outs = [ -# "generated/tests/json/test.upbdefs.h", -# "generated/tests/json/test.upbdefs.c", -# ], -# cmd = "UPBC=$$PWD/$(location :lua_upbc); INFILE=$$PWD/$<; cd $(GENDIR)/generated && $$UPBC --generate-upbdefs $$INFILE", -# tools = [":lua_upbc"], -#) - genrule( name = "generate_json_ragel", srcs = ["upb/json/parser.rl"], diff --git a/build_defs.bzl b/build_defs.bzl index 3867976..717a0e7 100644 --- a/build_defs.bzl +++ b/build_defs.bzl @@ -235,9 +235,6 @@ def _upb_proto_srcs_impl(ctx, suffix): source_paths = [d.path for d in sources] include_args = ["-I" + root for root in include_dirs.keys()] - print(source_paths) - print(include_args) - ctx.actions.run( inputs = [ctx.executable.upbc] + sources, outputs = outs, diff --git a/tests/test_cpp.cc b/tests/test_cpp.cc index 2cd9802..b6d8212 100644 --- a/tests/test_cpp.cc +++ b/tests/test_cpp.cc @@ -11,13 +11,12 @@ #include #include +#include "tests/test_cpp.upbdefs.h" #include "upb/def.h" -#include "upb/descriptor/reader.h" #include "upb/handlers.h" #include "upb/pb/decoder.h" -#include "upb/pb/glue.h" -#include "upb_test.h" #include "upb/upb.h" +#include "upb_test.h" template void AssertInsert(T* const container, const typename T::value_type& val) { @@ -25,182 +24,6 @@ void AssertInsert(T* const container, const typename T::value_type& val) { ASSERT(inserted); } -static void TestCastsUpDown() { - upb::reffed_ptr reffed_md(upb::MessageDef::New()); - const upb::MessageDef* md = reffed_md.get(); - - // Upcast to reffed_ptr implicitly. - upb::reffed_ptr reffed_def = reffed_md; - ASSERT(reffed_def.get() == upb::upcast(reffed_md.get())); - - // Upcast to raw pointer must be explicit. - const upb::Def* def = upb::upcast(md); - ASSERT(def == reffed_def.get()); - const upb::Def* def2 = upb::upcast(reffed_md.get()); - ASSERT(def2 == reffed_def.get()); - - // Downcast/dyncast of raw pointer uses upb::down_cast/upb::dyn_cast. - const upb::MessageDef* md2 = upb::down_cast(def); - const upb::MessageDef* md3 = upb::dyn_cast(def); - ASSERT(md == md2); - ASSERT(md == md3); - - // Downcast/dyncast of reffed_ptr uses down_cast/dyn_cast members. - upb::reffed_ptr md4( - reffed_def.down_cast()); - upb::reffed_ptr md5( - reffed_def.dyn_cast()); - ASSERT(md == md4.get()); - ASSERT(md == md5.get()); - - // Failed dyncast returns NULL. - ASSERT(upb::dyn_cast(def) == NULL); - ASSERT(reffed_def.dyn_cast().get() == NULL); -} - -static void TestCastsConst0() { - // Should clean up properly even if it is not assigned to anything. - upb::MessageDef::New(); -} - -static void TestCastsConst1() { - // Test reffed mutable -> reffed mutable construction/assignment. - upb::reffed_ptr md(upb::MessageDef::New()); - upb::MessageDef *md2 = md.get(); - md = upb::MessageDef::New(); - ASSERT(md.get()); - ASSERT(md.get() != md2); -} - -static void TestCastsConst2() { - // Test reffed mutable -> reffed mutable upcast construction/assignment. - upb::reffed_ptr md(upb::MessageDef::New()); - upb::reffed_ptr def = md; - ASSERT(upb::upcast(md.get()) == def.get()); - def = md; - ASSERT(upb::upcast(md.get()) == def.get()); -} - -static void TestCastsConst3() { - // Test reffed mutable -> reffed mutable downcast. - upb::reffed_ptr def(upb::MessageDef::New()); - upb::reffed_ptr md = def.down_cast(); - ASSERT(upb::upcast(md.get()) == def.get()); -} - -static void TestCastsConst4() { - // Test reffed mutable -> reffed mutable dyncast. - upb::reffed_ptr def(upb::MessageDef::New()); - upb::reffed_ptr md = def.dyn_cast(); - ASSERT(upb::upcast(md.get()) == def.get()); -} - -static void TestCastsConst5() { - // Test reffed mutable -> reffed const construction/assignment. - upb::reffed_ptr md(upb::MessageDef::New()); - const upb::MessageDef *md2 = md.get(); - md = upb::MessageDef::New(); - ASSERT(md.get()); - ASSERT(md.get() != md2); -} - -static void TestCastsConst6() { - // Test reffed mutable -> reffed const upcast construction/assignment. - upb::reffed_ptr md(upb::MessageDef::New()); - upb::reffed_ptr def = md; - ASSERT(upb::upcast(md.get()) == def.get()); - def = md; - ASSERT(upb::upcast(md.get()) == def.get()); -} - -static void TestCastsConst7() { - // Test reffed mutable -> reffed const downcast. - upb::reffed_ptr def(upb::MessageDef::New()); - upb::reffed_ptr md = - def.down_cast(); - ASSERT(upb::upcast(md.get()) == def.get()); -} - -static void TestCastsConst8() { - // Test reffed mutable -> reffed const dyncast. - upb::reffed_ptr def(upb::MessageDef::New()); - upb::reffed_ptr md = - def.dyn_cast(); - ASSERT(upb::upcast(md.get()) == def.get()); -} - -static void TestCastsConst9() { - // Test plain mutable -> plain mutable upcast - upb::reffed_ptr md(upb::MessageDef::New()); - upb::Def* def = upb::upcast(md.get()); - ASSERT(upb::down_cast(def) == md.get()); -} - -static void TestCastsConst10() { - // Test plain const -> plain const upcast - upb::reffed_ptr md(upb::MessageDef::New()); - const upb::Def* def = upb::upcast(md.get()); - ASSERT(upb::down_cast(def) == md.get()); -} - -static void TestSymbolTable(const char *descriptor_file) { - upb::Status status; - std::ifstream file_in(descriptor_file, std::ios::binary); - std::string descriptor((std::istreambuf_iterator(file_in)), - (std::istreambuf_iterator())); - std::vector > files; - if (!upb::LoadDescriptor(descriptor, &status, &files)) { - std::cerr << "Couldn't load descriptor: " << status.error_message(); - exit(1); - } - - upb::SymbolTable* s = upb::SymbolTable::New(); - - for (size_t i = 0; i < files.size(); i++) { - ASSERT(s->AddFile(files[i].get(), &status)); - } - - upb::reffed_ptr md(s->LookupMessage("C")); - ASSERT(md.get()); - - // We want a def that satisfies this to test iteration. - ASSERT(md->field_count() > 1); - -#ifdef UPB_CXX11 - // Test range-based for. - std::set fielddefs; - for (const upb::FieldDef* f : md.get()->fields()) { - AssertInsert(&fielddefs, f); - ASSERT(f->containing_type() == md.get()); - } - ASSERT(fielddefs.size() == md->field_count()); -#endif - - ASSERT(md.get()); - upb::SymbolTable::Free(s); -} - -static void TestCasts1() { - upb::reffed_ptr md(upb::MessageDef::New()); - const upb::Def* def = upb::upcast(md.get()); - const upb::MessageDef* md2 = upb::down_cast(def); - const upb::MessageDef* md3 = upb::dyn_cast(def); - - ASSERT(md.get() == md2); - ASSERT(md.get() == md3); - - const upb::EnumDef* ed = upb::dyn_cast(def); - ASSERT(!ed); -} - -static void TestCasts2() { - // Test mutable -> const cast. - upb::reffed_ptr md(upb::MessageDef::New()); - upb::Def* def = upb::upcast(md.get()); - const upb::MessageDef* const_md = upb::down_cast(def); - ASSERT(const_md == md.get()); -} - // // Tests for registering and calling handlers in all their variants. // This test code is very repetitive because we have to declare each @@ -225,7 +48,7 @@ static const int kExpectedHandlerData = 1232323; class StringBufTesterBase { public: - static const upb::FieldDef::Type kFieldType = UPB_TYPE_STRING; + static const int kFieldNumber = 3; StringBufTesterBase() : seen_(false), handler_data_val_(0) {} @@ -461,7 +284,7 @@ class StartMsgTesterBase { public: // We don't need the FieldDef it will create, but the test harness still // requires that we provide one. - static const upb::FieldDef::Type kFieldType = UPB_TYPE_STRING; + static const int kFieldNumber = 3; StartMsgTesterBase() : seen_(false), handler_data_val_(0) {} @@ -612,7 +435,7 @@ class StartMsgTesterBoolMethodWithHandlerData : public StartMsgTesterBase { class Int32ValueTesterBase { public: - static const upb::FieldDef::Type kFieldType = UPB_TYPE_INT32; + static const int kFieldNumber = 1; Int32ValueTesterBase() : seen_(false), val_(0), handler_data_val_(0) {} @@ -770,21 +593,20 @@ class ValueTesterInt32BoolMethodWithHandlerData : public Int32ValueTesterBase { template void TestHandler() { - upb::reffed_ptr md(upb::MessageDef::New()); - upb::reffed_ptr f(upb::FieldDef::New()); - f->set_type(T::kFieldType); - ASSERT(f->set_name("test", NULL)); - ASSERT(f->set_number(1, NULL)); - ASSERT(md->AddField(f, NULL)); - ASSERT(md->Freeze(NULL)); - - upb::reffed_ptr h(upb::Handlers::New(md.get())); + upb::SymbolTable* symtab = upb::SymbolTable::New(); + const upb::MessageDef* md = upb_test_TestMessage_getmsgdef(symtab); + ASSERT(md); + const upb::FieldDef* f = md->FindFieldByNumber(T::kFieldNumber); + ASSERT(f); + + upb::reffed_ptr h(upb::Handlers::New(md)); T tester; - tester.Register(h.get(), f.get()); + tester.Register(h.get(), f); ASSERT(h->Freeze(NULL)); upb::Sink sink(h.get(), &tester); - tester.CallAndVerify(&sink, f.get()); + tester.CallAndVerify(&sink, f); + upb::SymbolTable::Free(symtab); } class T1 {}; @@ -850,59 +672,24 @@ void DoNothingEndMessageHandler(C* closure, upb::Status *status) { void TestMismatchedTypes() { // First create a schema for our test. - upb::reffed_ptr md(upb::MessageDef::New()); - - upb::reffed_ptr f(upb::FieldDef::New()); - f->set_type(UPB_TYPE_INT32); - ASSERT(f->set_name("i32", NULL)); - ASSERT(f->set_number(1, NULL)); - ASSERT(md->AddField(f, NULL)); - const upb::FieldDef* i32 = f.get(); - - f = upb::FieldDef::New(); - f->set_type(UPB_TYPE_INT32); - ASSERT(f->set_name("r_i32", NULL)); - ASSERT(f->set_number(2, NULL)); - f->set_label(UPB_LABEL_REPEATED); - ASSERT(md->AddField(f, NULL)); - const upb::FieldDef* r_i32 = f.get(); - - f = upb::FieldDef::New(); - f->set_type(UPB_TYPE_STRING); - ASSERT(f->set_name("str", NULL)); - ASSERT(f->set_number(3, NULL)); - ASSERT(md->AddField(f, NULL)); - const upb::FieldDef* str = f.get(); - - f = upb::FieldDef::New(); - f->set_type(UPB_TYPE_STRING); - ASSERT(f->set_name("r_str", NULL)); - ASSERT(f->set_number(4, NULL)); - f->set_label(UPB_LABEL_REPEATED); - ASSERT(md->AddField(f, NULL)); - const upb::FieldDef* r_str = f.get(); - - f = upb::FieldDef::New(); - f->set_type(UPB_TYPE_MESSAGE); - ASSERT(f->set_name("msg", NULL)); - ASSERT(f->set_number(5, NULL)); - ASSERT(f->set_message_subdef(md.get(), NULL)); - ASSERT(md->AddField(f, NULL)); - const upb::FieldDef* msg = f.get(); - - f = upb::FieldDef::New(); - f->set_type(UPB_TYPE_MESSAGE); - ASSERT(f->set_name("r_msg", NULL)); - ASSERT(f->set_number(6, NULL)); - ASSERT(f->set_message_subdef(md.get(), NULL)); - f->set_label(UPB_LABEL_REPEATED); - ASSERT(md->AddField(f, NULL)); - const upb::FieldDef* r_msg = f.get(); - - ASSERT(md->Freeze(NULL)); + upb::SymbolTable* symtab = upb::SymbolTable::New(); + const upb::MessageDef* md = upb_test_TestMessage_getmsgdef(symtab); + ASSERT(md); + const upb::FieldDef* i32 = md->FindFieldByName("i32"); + const upb::FieldDef* r_i32 = md->FindFieldByName("r_i32"); + const upb::FieldDef* str = md->FindFieldByName("str"); + const upb::FieldDef* r_str = md->FindFieldByName("r_str"); + const upb::FieldDef* msg = md->FindFieldByName("msg"); + const upb::FieldDef* r_msg = md->FindFieldByName("r_msg"); + ASSERT(i32); + ASSERT(r_i32); + ASSERT(str); + ASSERT(r_str); + ASSERT(msg); + ASSERT(r_msg); // Now test the type-checking in handler registration. - upb::reffed_ptr h(upb::Handlers::New(md.get())); + upb::reffed_ptr h(upb::Handlers::New(md)); // Establish T1 as the top-level closure type. ASSERT(h->SetInt32Handler(i32, UpbMakeHandler(DoNothingInt32Handler))); @@ -1007,7 +794,7 @@ void TestMismatchedTypes() { // For our second test we do the same in reverse. We directly set the type of // the frame and then observe failures at registering a Start* handler that // returns a different type. - h = upb::Handlers::New(md.get()); + h = upb::Handlers::New(md); // First establish the type of a sequence frame directly. ASSERT(h->SetInt32Handler(r_i32, UpbMakeHandler(DoNothingInt32Handler))); @@ -1040,7 +827,7 @@ void TestMismatchedTypes() { // should exist to return the closure type of the inner frame but no // StartSequence/StartString handler is registered. - h = upb::Handlers::New(md.get()); + h = upb::Handlers::New(md); // Establish T1 as top-level closure type. ASSERT(h->SetInt32Handler(i32, UpbMakeHandler(DoNothingInt32Handler))); @@ -1061,7 +848,7 @@ void TestMismatchedTypes() { ASSERT(h->Freeze(NULL)); // Test for a broken chain that is two deep. - h = upb::Handlers::New(md.get()); + h = upb::Handlers::New(md); // Establish T1 as top-level closure type. ASSERT(h->SetInt32Handler(i32, UpbMakeHandler(DoNothingInt32Handler))); @@ -1104,145 +891,24 @@ class IntIncrementer { void TestHandlerDataDestruction() { - upb::reffed_ptr md(upb::MessageDef::New()); - upb::reffed_ptr f(upb::FieldDef::New()); - f->set_type(UPB_TYPE_INT32); - ASSERT(f->set_name("test", NULL)); - ASSERT(f->set_number(1, NULL)); - ASSERT(md->AddField(f, NULL)); - ASSERT(md->Freeze(NULL)); + upb::SymbolTable* symtab = upb::SymbolTable::New(); + const upb::MessageDef* md = upb_test_TestMessage_getmsgdef(symtab); + const upb::FieldDef* f = md->FindFieldByName("i32"); int x = 0; { - upb::reffed_ptr h(upb::Handlers::New(md.get())); + upb::reffed_ptr h(upb::Handlers::New(md)); h->SetInt32Handler( - f.get(), UpbBind(&IntIncrementer::Handler, new IntIncrementer(&x))); + f, UpbBind(&IntIncrementer::Handler, new IntIncrementer(&x))); ASSERT(x == 1); } ASSERT(x == 0); } -void TestOneofs() { - upb::Status status; - upb::reffed_ptr md(upb::MessageDef::New()); - upb::reffed_ptr o(upb::OneofDef::New()); - - o->set_name("test_oneof", &status); - ASSERT(status.ok()); - - for (int i = 0; i < 5; i++) { - std::ostringstream fieldname; - fieldname << "field_" << i; - upb::reffed_ptr f(upb::FieldDef::New()); - f->set_name(fieldname.str(), &status); - ASSERT(status.ok()); - f->set_type(UPB_TYPE_INT32); - f->set_number(i + 1, &status); - ASSERT(status.ok()); - f->set_label(UPB_LABEL_OPTIONAL); - - o->AddField(f.get(), &status); - ASSERT(status.ok()); - } - - md->AddOneof(o.get(), &status); - ASSERT(status.ok()); - - int field_count = 0; - for (upb::OneofDef::iterator it = o->begin(); it != o->end(); ++it) { - upb::FieldDef* f = *it; - ASSERT(f->type() == UPB_TYPE_INT32); - field_count++; - } - ASSERT(field_count == 5); - - upb::MessageDef::oneof_iterator msg_it = md->oneof_begin(); - ASSERT(msg_it != md->oneof_end()); - ASSERT((*msg_it) == o.get()); - -#ifdef UPB_CXX11 - // Test range-based for on both fields and oneofs (with the iterator adaptor). - field_count = 0; - for (auto* field : md->fields()) { - UPB_UNUSED(field); - field_count++; - } - ASSERT(field_count == 5); - - int oneof_count = 0; - for (auto* oneof : md->oneofs()) { - UPB_UNUSED(oneof); - oneof_count++; - } - ASSERT(oneof_count == 1); -#endif // UPB_CXX11 - - // Test that we can add a new field to the oneof and that it becomes a member - // of the msgdef as well. - upb::reffed_ptr newf(upb::FieldDef::New()); - newf->set_name("new_field_10", &status); - ASSERT(status.ok()); - newf->set_number(10, &status); - ASSERT(status.ok()); - newf->set_label(UPB_LABEL_OPTIONAL); - newf->set_type(UPB_TYPE_INT32); - o->AddField(newf.get(), &status); - ASSERT(status.ok()); - ASSERT(newf->containing_type() == md.get()); - - // Test that we can add a new field to the msgdef first and then to the oneof. - upb::reffed_ptr newf2(upb::FieldDef::New()); - newf2->set_name("new_field_11", &status); - ASSERT(status.ok()); - newf2->set_number(11, &status); - ASSERT(status.ok()); - newf2->set_label(UPB_LABEL_OPTIONAL); - newf2->set_type(UPB_TYPE_INT32); - md->AddField(newf2.get(), &status); - ASSERT(status.ok()); - o->AddField(newf2.get(), &status); - ASSERT(status.ok()); - ASSERT(newf2->containing_oneof() == o.get()); - - // Test that we cannot add REQUIRED or REPEATED fields to the oneof. - upb::reffed_ptr newf3(upb::FieldDef::New()); - newf3->set_name("new_field_12", &status); - ASSERT(status.ok()); - newf3->set_number(12, &status); - ASSERT(status.ok()); - newf3->set_label(UPB_LABEL_REQUIRED); - newf3->set_type(UPB_TYPE_INT32); - o->AddField(newf3.get(), &status); - ASSERT(!status.ok()); - newf->set_label(UPB_LABEL_REPEATED); - o->AddField(newf3.get(), &status); - ASSERT(!status.ok()); -} - extern "C" { int run_tests(int argc, char *argv[]) { - if (argc < 2) { - fprintf(stderr, "Usage: test_cpp \n"); - return 1; - } - TestSymbolTable(argv[1]); - TestCastsUpDown(); - TestCasts1(); - TestCasts2(); - TestCastsConst0(); - TestCastsConst1(); - TestCastsConst2(); - TestCastsConst3(); - TestCastsConst4(); - TestCastsConst5(); - TestCastsConst6(); - TestCastsConst7(); - TestCastsConst8(); - TestCastsConst9(); - TestCastsConst10(); - TestHandler(); TestHandler(); TestHandler(); @@ -1276,8 +942,6 @@ int run_tests(int argc, char *argv[]) { TestHandlerDataDestruction(); - TestOneofs(); - return 0; } diff --git a/upb/def.c b/upb/def.c index c10394e..ccbf407 100644 --- a/upb/def.c +++ b/upb/def.c @@ -921,12 +921,12 @@ static char* strviewdup(const symtab_addctx *ctx, upb_stringview view) { return upb_strdup2(view.data, view.size, ctx->alloc); } -static bool streql(const char *a, size_t n, const char *b) { +static bool streql2(const char *a, size_t n, const char *b) { return n == strlen(b) && memcmp(a, b, n) == 0; } static bool streql_view(upb_stringview view, const char *b) { - return streql(view.data, view.size, b); + return streql2(view.data, view.size, b); } static const char *makefullname(const symtab_addctx *ctx, const char *prefix, @@ -1077,9 +1077,9 @@ static bool parse_default(const symtab_addctx *ctx, const char *str, size_t len, break; } case UPB_TYPE_BOOL: { - if (streql(str, len, "false")) { + if (streql2(str, len, "false")) { f->defaultval.boolean = false; - } else if (streql(str, len, "true")) { + } else if (streql2(str, len, "true")) { f->defaultval.boolean = true; } else { return false; -- cgit v1.2.3 From 8afe0b03a349cc259fb731ff2d2e0a13e47c166a Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Thu, 20 Dec 2018 09:53:13 -0800 Subject: Some fixes for Ruby. --- upb/def.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- upb/def.h | 3 +++ upb/msgfactory.c | 9 --------- 3 files changed, 55 insertions(+), 12 deletions(-) (limited to 'upb') diff --git a/upb/def.c b/upb/def.c index ccbf407..83e5a99 100644 --- a/upb/def.c +++ b/upb/def.c @@ -355,6 +355,10 @@ const char *upb_enumdef_name(const upb_enumdef *e) { return shortdefname(e->full_name); } +const upb_filedef *upb_enumdef_file(const upb_enumdef *e) { + return e->file; +} + int32_t upb_enumdef_default(const upb_enumdef *e) { UPB_ASSERT(upb_enumdef_iton(e, e->defaultval)); return e->defaultval; @@ -606,6 +610,12 @@ bool upb_fielddef_hassubdef(const upb_fielddef *f) { return upb_fielddef_issubmsg(f) || upb_fielddef_type(f) == UPB_TYPE_ENUM; } +bool upb_fielddef_haspresence(const upb_fielddef *f) { + if (upb_fielddef_isseq(f)) return false; + if (upb_fielddef_issubmsg(f)) return true; + return f->file->syntax == UPB_SYNTAX_PROTO2; +} + static bool between(int32_t x, int32_t low, int32_t high) { return x >= low && x <= high; } @@ -624,6 +634,10 @@ const char *upb_msgdef_fullname(const upb_msgdef *m) { return m->full_name; } +const upb_filedef *upb_msgdef_file(const upb_msgdef *m) { + return m->file; +} + const char *upb_msgdef_name(const upb_msgdef *m) { return shortdefname(m->full_name); } @@ -1166,6 +1180,12 @@ static bool create_fielddef( * to the field_proto until later when we can properly resolve it. */ f->sub.unresolved = field_proto; + if (f->label_ == UPB_LABEL_REQUIRED && f->file->syntax == UPB_SYNTAX_PROTO3) { + upb_status_seterrf(ctx->status, "proto3 fields cannot be required (%s)", + f->full_name); + return false; + } + if (google_protobuf_FieldDescriptorProto_has_oneof_index(field_proto)) { int oneof_index = google_protobuf_FieldDescriptorProto_oneof_index(field_proto); @@ -1229,6 +1249,13 @@ static bool create_enumdef( values = google_protobuf_EnumDescriptorProto_value(enum_proto, &n); + if (n == 0) { + upb_status_seterrf(ctx->status, + "enums must contain at least one value (%s)", + e->full_name); + return false; + } + for (i = 0; i < n; i++) { const google_protobuf_EnumValueDescriptorProto *value = values[i]; upb_stringview name = google_protobuf_EnumValueDescriptorProto_name(value); @@ -1236,6 +1263,13 @@ static bool create_enumdef( int32_t num = google_protobuf_EnumValueDescriptorProto_number(value); upb_value v = upb_value_int32(num); + if (n == 0 && e->file->syntax == UPB_SYNTAX_PROTO3 && num != 0) { + upb_status_seterrf(ctx->status, + "for proto3, the first enum value must be zero (%s)", + e->full_name); + return false; + } + if (upb_strtable_lookup(&e->ntoi, name2, NULL)) { upb_status_seterrf(ctx->status, "duplicate enum label '%s'", name2); return false; @@ -1399,9 +1433,24 @@ static bool resolve_fielddef(const symtab_addctx *ctx, const char *prefix, if (google_protobuf_FieldDescriptorProto_has_default_value(field_proto)) { upb_stringview defaultval = google_protobuf_FieldDescriptorProto_default_value(field_proto); + + if (f->file->syntax == UPB_SYNTAX_PROTO3) { + upb_status_seterrf(ctx->status, + "proto3 fields cannot have explicit defaults (%s)", + f->full_name); + return false; + } + + if (upb_fielddef_issubmsg(f)) { + upb_status_seterrf(ctx->status, + "message fields cannot have explicit defaults (%s)", + f->full_name); + return false; + } + if (!parse_default(ctx, defaultval.data, defaultval.size, f)) { - upb_status_seterrf(ctx->status, "bad default '" UPB_STRINGVIEW_FORMAT "'", - UPB_STRINGVIEW_ARGS(defaultval)); + upb_status_seterrf(ctx->status, "couldn't parse default for field (%s)", + f->full_name); return false; } } @@ -1532,7 +1581,7 @@ static bool build_filedef( } return true; -} + } static bool upb_symtab_addtotabs(upb_symtab *s, symtab_addctx *ctx, upb_status *status) { diff --git a/upb/def.h b/upb/def.h index e6fdf21..8664fb5 100644 --- a/upb/def.h +++ b/upb/def.h @@ -202,6 +202,7 @@ float upb_fielddef_defaultfloat(const upb_fielddef *f); double upb_fielddef_defaultdouble(const upb_fielddef *f); const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len); bool upb_fielddef_hassubdef(const upb_fielddef *f); +bool upb_fielddef_haspresence(const upb_fielddef *f); const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f); const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f); @@ -349,6 +350,7 @@ class upb::MessageDef { UPB_BEGIN_EXTERN_C const char *upb_msgdef_fullname(const upb_msgdef *m); +const upb_filedef *upb_msgdef_file(const upb_msgdef *m); const char *upb_msgdef_name(const upb_msgdef *m); int upb_msgdef_numoneofs(const upb_msgdef *m); upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m); @@ -485,6 +487,7 @@ UPB_BEGIN_EXTERN_C const char *upb_enumdef_fullname(const upb_enumdef *e); const char *upb_enumdef_name(const upb_enumdef *e); +const upb_filedef *upb_enumdef_file(const upb_enumdef *e); int32_t upb_enumdef_default(const upb_enumdef *e); int upb_enumdef_numvals(const upb_enumdef *e); diff --git a/upb/msgfactory.c b/upb/msgfactory.c index 73347b8..63df49e 100644 --- a/upb/msgfactory.c +++ b/upb/msgfactory.c @@ -46,15 +46,6 @@ static uint8_t upb_msg_fielddefsize(const upb_fielddef *f) { } } -static bool upb_fielddef_haspresence(const upb_fielddef *f) { - if (upb_fielddef_isseq(f)) return false; - if (upb_fielddef_issubmsg(f)) return true; - - /* Primitive field: return true unless there is a message that specifies - * presence should not exist. */ - return upb_msgdef_syntax(upb_fielddef_containingtype(f)) == UPB_SYNTAX_PROTO2; -} - /** upb_msglayout *************************************************************/ -- cgit v1.2.3 From d0c8bb84f449e7fc94a85ac1594cb8090f3c9bb8 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Mon, 31 Dec 2018 20:00:22 -0800 Subject: WIP. --- upb/def.c | 9 ++++++--- upb/msg.h | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) (limited to 'upb') diff --git a/upb/def.c b/upb/def.c index 83e5a99..aff8401 100644 --- a/upb/def.c +++ b/upb/def.c @@ -916,7 +916,7 @@ const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym) { * this code is used to directly build defs from Ruby (for example) we do need * to validate important constraints like uniqueness of names and numbers. */ -#define CHK(x) if (!(x)) { __builtin_trap(); return false; } +#define CHK(x) if (!(x)) { return false; } #define CHK_OOM(x) if (!(x)) { upb_upberr_setoom(ctx->status); return false; } typedef struct { @@ -1105,6 +1105,7 @@ static bool parse_default(const symtab_addctx *ctx, const char *str, size_t len, case UPB_TYPE_BYTES: /* XXX: need to interpret the C-escaped value. */ f->defaultval.str = newstr(ctx->alloc, str, len); + break; case UPB_TYPE_MESSAGE: /* Should not have a default value. */ return false; @@ -1449,8 +1450,10 @@ static bool resolve_fielddef(const symtab_addctx *ctx, const char *prefix, } if (!parse_default(ctx, defaultval.data, defaultval.size, f)) { - upb_status_seterrf(ctx->status, "couldn't parse default for field (%s)", - f->full_name); + upb_status_seterrf(ctx->status, + "couldn't parse default '" UPB_STRINGVIEW_FORMAT + "' for field (%s)", + UPB_STRINGVIEW_ARGS(defaultval), f->full_name); return false; } } diff --git a/upb/msg.h b/upb/msg.h index 6061470..4529478 100644 --- a/upb/msg.h +++ b/upb/msg.h @@ -99,7 +99,7 @@ UPB_INLINE bool upb_stringview_eql(upb_stringview a, upb_stringview b) { } #define UPB_STRINGVIEW_FORMAT "%.*s" -#define UPB_STRINGVIEW_ARGS(view) view.size, view.data +#define UPB_STRINGVIEW_ARGS(view) (int)(view).size, (view).data #define UPB_STRINGVIEW_INIT(ptr, len) {ptr, len} -- cgit v1.2.3 From 31e0997c1abaa531505d28e36473f1c972ca0849 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 2 Jan 2019 13:00:53 -0800 Subject: Some bugfixes. --- upb/def.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++--------- upb/def.h | 2 +- 2 files changed, 53 insertions(+), 10 deletions(-) (limited to 'upb') diff --git a/upb/def.c b/upb/def.c index aff8401..27de875 100644 --- a/upb/def.c +++ b/upb/def.c @@ -295,7 +295,7 @@ static bool assign_msg_indices(upb_msgdef *m, upb_status *s) { for(upb_msg_oneof_begin(&k, m), i = 0; !upb_msg_oneof_done(&k); upb_msg_oneof_next(&k), i++) { - upb_oneofdef *o = upb_msg_iter_oneof(&k); + upb_oneofdef *o = (upb_oneofdef*)upb_msg_iter_oneof(&k); o->index = i; } @@ -570,8 +570,13 @@ const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len) { UPB_ASSERT(upb_fielddef_type(f) == UPB_TYPE_STRING || upb_fielddef_type(f) == UPB_TYPE_BYTES || upb_fielddef_type(f) == UPB_TYPE_ENUM); - if (len) *len = str->len; - return str->str; + if (str) { + if (len) *len = str->len; + return str->str; + } else { + if (len) *len = 0; + return NULL; + } } const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f) { @@ -759,8 +764,8 @@ bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter) { return upb_strtable_done(iter); } -upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter) { - return (upb_oneofdef *)upb_value_getconstptr(upb_strtable_iter_value(iter)); +const upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter) { + return unpack_def(upb_strtable_iter_value(iter), UPB_DEFTYPE_ONEOF); } void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter) { @@ -1026,13 +1031,15 @@ static bool create_oneofdef( const google_protobuf_OneofDescriptorProto *oneof_proto) { upb_oneofdef *o; upb_stringview name = google_protobuf_OneofDescriptorProto_name(oneof_proto); + upb_value v; o = (upb_oneofdef*)&m->oneofs[m->oneof_count++]; o->parent = m; o->full_name = makefullname(ctx, m->full_name, name); - CHK_OOM(symtab_add(ctx, o->full_name, pack_def(o, UPB_DEFTYPE_ONEOF))); - CHK_OOM(upb_strtable_insert3(&m->ntof, name.data, name.size, upb_value_ptr(o), - ctx->alloc)); + + v = pack_def(o, UPB_DEFTYPE_ONEOF); + CHK_OOM(symtab_add(ctx, o->full_name, v)); + CHK_OOM(upb_strtable_insert3(&m->ntof, name.data, name.size, v, ctx->alloc)); CHK_OOM(upb_inttable_init2(&o->itof, UPB_CTYPE_CONSTPTR, ctx->alloc)); CHK_OOM(upb_strtable_init2(&o->ntof, UPB_CTYPE_CONSTPTR, ctx->alloc)); @@ -1113,6 +1120,33 @@ static bool parse_default(const symtab_addctx *ctx, const char *str, size_t len, return true; } +static void set_default_default(const symtab_addctx *ctx, upb_fielddef *f) { + switch (upb_fielddef_type(f)) { + case UPB_TYPE_INT32: + case UPB_TYPE_INT64: + case UPB_TYPE_ENUM: + f->defaultval.sint = 0; + break; + case UPB_TYPE_UINT64: + case UPB_TYPE_UINT32: + f->defaultval.uint = 0; + break; + case UPB_TYPE_DOUBLE: + case UPB_TYPE_FLOAT: + f->defaultval.dbl = 0; + break; + case UPB_TYPE_STRING: + case UPB_TYPE_BYTES: + f->defaultval.str = newstr(ctx->alloc, NULL, 0); + break; + case UPB_TYPE_BOOL: + f->defaultval.boolean = false; + break; + case UPB_TYPE_MESSAGE: + break; + } +} + static bool create_fielddef( const symtab_addctx *ctx, const char *prefix, upb_msgdef *m, const google_protobuf_FieldDescriptorProto *field_proto) { @@ -1190,6 +1224,8 @@ static bool create_fielddef( if (google_protobuf_FieldDescriptorProto_has_oneof_index(field_proto)) { int oneof_index = google_protobuf_FieldDescriptorProto_oneof_index(field_proto); + upb_oneofdef *oneof; + upb_value v = upb_value_constptr(f); if (upb_fielddef_label(f) != UPB_LABEL_OPTIONAL) { upb_status_seterrf(ctx->status, @@ -1211,7 +1247,11 @@ static bool create_fielddef( return false; } - f->oneof = &m->oneofs[oneof_index]; + oneof = (upb_oneofdef*)&m->oneofs[oneof_index]; + f->oneof = oneof; + + CHK(upb_inttable_insert2(&oneof->itof, f->number_, v, alloc)); + CHK(upb_strtable_insert3(&oneof->ntof, name.data, name.size, v, alloc)); } else { f->oneof = NULL; } @@ -1246,6 +1286,7 @@ static bool create_enumdef( CHK_OOM(upb_strtable_init2(&e->ntoi, UPB_CTYPE_INT32, ctx->alloc)); CHK_OOM(upb_inttable_init2(&e->iton, UPB_CTYPE_CSTR, ctx->alloc)); + e->file = ctx->file; e->defaultval = 0; values = google_protobuf_EnumDescriptorProto_value(enum_proto, &n); @@ -1456,6 +1497,8 @@ static bool resolve_fielddef(const symtab_addctx *ctx, const char *prefix, UPB_STRINGVIEW_ARGS(defaultval), f->full_name); return false; } + } else { + set_default_default(ctx, f); } return true; diff --git a/upb/def.h b/upb/def.h index bd36d92..81b5659 100644 --- a/upb/def.h +++ b/upb/def.h @@ -429,7 +429,7 @@ void upb_msg_field_iter_setdone(upb_msg_field_iter *iter); void upb_msg_oneof_begin(upb_msg_oneof_iter *iter, const upb_msgdef *m); void upb_msg_oneof_next(upb_msg_oneof_iter *iter); bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter); -upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter); +const upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter); void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter); UPB_END_EXTERN_C -- cgit v1.2.3 From 0553eff64a87eceff0de3b6260b4f2d45b61703a Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 9 Jan 2019 22:40:50 -0800 Subject: upb_refcounted is gone! Some tests still to fix. --- BUILD | 49 +-- tests/json/test_json.cc | 23 +- tests/pb/test_encoder.cc | 22 +- upb/bindings/lua/def.c | 18 +- upb/bindings/lua/upb.h | 7 +- upb/handlers-inl.h | 34 +- upb/handlers.c | 366 ++++++-------------- upb/handlers.h | 84 ++--- upb/json/parser.c | 320 +++++++++--------- upb/json/parser.h | 50 ++- upb/json/parser.rl | 214 ++++++------ upb/json/printer.c | 17 +- upb/json/printer.h | 17 +- upb/msgfactory.c | 9 - upb/pb/compile_decoder.c | 176 ++++------ upb/pb/decoder.h | 128 ++----- upb/pb/decoder.int.h | 63 ++-- upb/pb/encoder.c | 5 +- upb/pb/encoder.h | 12 +- upb/pb/textprinter.c | 5 +- upb/pb/textprinter.h | 11 +- upb/refcounted.c | 851 ----------------------------------------------- upb/refcounted.h | 348 ------------------- 23 files changed, 666 insertions(+), 2163 deletions(-) delete mode 100644 upb/refcounted.c delete mode 100644 upb/refcounted.h (limited to 'upb') diff --git a/BUILD b/BUILD index bf988f9..ffb5bbd 100644 --- a/BUILD +++ b/BUILD @@ -1,16 +1,19 @@ load( ":build_defs.bzl", + "generated_file_staleness_test", + "lua_binary", "lua_cclibrary", "lua_library", - "lua_binary", "lua_test", - "generated_file_staleness_test", "make_shell_script", "upb_amalgamation", "upb_proto_library", "upb_proto_reflection_library", ) +# Remove once our C++ wrappers are more sane. +CXX_COPTS = ["-Wno-unused-private-field"] + # C/C++ rules ################################################################## cc_library( @@ -26,7 +29,6 @@ cc_library( "upb/msgfactory.c", "upb/port_def.inc", "upb/port_undef.inc", - "upb/refcounted.c", "upb/sink.c", "upb/structs.int.h", "upb/table.c", @@ -42,7 +44,6 @@ cc_library( "upb/handlers.h", "upb/msg.h", "upb/msgfactory.h", - "upb/refcounted.h", "upb/sink.h", "upb/upb.h", ], @@ -161,8 +162,8 @@ cc_test( upb_proto_reflection_library( name = "descriptor_upbproto", - deps = ["descriptor_proto"], upbc = ":protoc-gen-upb", + deps = ["descriptor_proto"], ) cc_test( @@ -178,14 +179,14 @@ cc_test( proto_library( name = "test_decoder_proto", srcs = [ - "tests/pb/test_decoder.proto" - ] + "tests/pb/test_decoder.proto", + ], ) upb_proto_reflection_library( name = "test_decoder_upbproto", - deps = ["test_decoder_proto"], upbc = ":protoc-gen-upb", + deps = ["test_decoder_proto"], ) cc_test( @@ -201,6 +202,7 @@ cc_test( cc_test( name = "test_encoder", srcs = ["tests/pb/test_encoder.cc"], + copts = CXX_COPTS, data = ["google/protobuf/descriptor.pb"], deps = [ ":upb_cc_bindings", @@ -212,19 +214,20 @@ cc_test( proto_library( name = "test_cpp_proto", srcs = [ - "tests/test_cpp.proto" - ] + "tests/test_cpp.proto", + ], ) upb_proto_reflection_library( name = "test_cpp_upbproto", - deps = ["test_cpp_proto"], upbc = ":protoc-gen-upb", + deps = ["test_cpp_proto"], ) cc_test( name = "test_cpp", srcs = ["tests/test_cpp.cc"], + copts = CXX_COPTS, deps = [ ":test_cpp_upbproto", ":upb", @@ -249,8 +252,8 @@ proto_library( upb_proto_reflection_library( name = "test_json_upbproto", - deps = ["test_json_proto"], upbc = ":protoc-gen-upb", + deps = ["test_json_proto"], ) cc_test( @@ -258,6 +261,7 @@ cc_test( srcs = [ "tests/json/test_json.cc", ], + copts = CXX_COPTS, deps = [ ":test_json_upbproto", ":upb_json", @@ -279,11 +283,11 @@ cc_binary( srcs = [ "tests/conformance_upb.c", ], + copts = ["-Ibazel-out/k8-fastbuild/bin"], deps = [ ":conformance_proto_upb", ":upb", ], - copts = ["-Ibazel-out/k8-fastbuild/bin"], ) make_shell_script( @@ -363,12 +367,16 @@ lua_test( cc_library( name = "upbc_generator", + srcs = [ + "upbc/generator.cc", + "upbc/message_layout.cc", + "upbc/message_layout.h", + ], hdrs = ["upbc/generator.h"], - srcs = ["upbc/generator.cc", "upbc/message_layout.h", "upbc/message_layout.cc"], deps = [ + "@absl//absl/strings", "@com_google_protobuf//:protobuf", "@com_google_protobuf//:protoc_lib", - "@absl//absl/strings", ], ) @@ -386,7 +394,7 @@ cc_binary( make_shell_script( name = "gen_run_cmake_build", out = "run_cmake_build.sh", - contents = "mkdir build && cd build && cmake .. && make -j8 && make test" + contents = "mkdir build && cd build && cmake .. && make -j8 && make test", ) sh_test( @@ -447,10 +455,13 @@ genrule( genrule( name = "gen_cmakelists", + srcs = [ + "BUILD", + "WORKSPACE", + ], outs = ["generated/CMakeLists.txt"], - srcs = ["BUILD", "WORKSPACE"], + cmd = "$(location :make_cmakelists) $@", tools = [":make_cmakelists"], - cmd = "$(location :make_cmakelists) $@" ) proto_library( @@ -469,8 +480,8 @@ genrule( ], cmd = "$(location @com_google_protobuf//:protoc) $< --upb_out=$(GENDIR)/generated --plugin=protoc-gen-upb=$(location :protoc-gen-upb)", tools = [ + ":protoc-gen-upb", "@com_google_protobuf//:protoc", - ":protoc-gen-upb" ], ) diff --git a/tests/json/test_json.cc b/tests/json/test_json.cc index 98bf59e..b9b50cd 100644 --- a/tests/json/test_json.cc +++ b/tests/json/test_json.cc @@ -204,12 +204,15 @@ void test_json_roundtrip_message(const char* json_src, // and compares the result. void test_json_roundtrip() { upb::SymbolTable* symtab = upb::SymbolTable::New(); + upb::HandlerCache* serialize_handlercache = upb::json::Printer::NewCache(false); + upb::json::CodeCache* parse_codecache = upb::json::CodeCache::New(); + const upb::MessageDef* md = upb_test_json_TestMessage_getmsgdef(symtab); ASSERT(md); - upb::reffed_ptr serialize_handlers( - upb::json::Printer::NewHandlers(md, false)); - upb::reffed_ptr parser_method( - upb::json::ParserMethod::New(md)); + const upb::Handlers* serialize_handlers = serialize_handlercache->Get(md); + const upb::json::ParserMethod* parser_method = parse_codecache->Get(md); + ASSERT(serialize_handlers); + ASSERT(parser_method); for (const TestCase* test_case = kTestRoundtripMessages; test_case->input != NULL; test_case++) { @@ -220,12 +223,13 @@ void test_json_roundtrip() { for (size_t i = 0; i < strlen(test_case->input); i++) { test_json_roundtrip_message(test_case->input, expected, - serialize_handlers.get(), parser_method.get(), - i); + serialize_handlers, parser_method, i); } } - serialize_handlers = upb::json::Printer::NewHandlers(md, true); + upb::HandlerCache::Free(serialize_handlercache); + serialize_handlercache = upb::json::Printer::NewCache(true); + serialize_handlers = serialize_handlercache->Get(md); for (const TestCase* test_case = kTestRoundtripMessagesPreserve; test_case->input != NULL; test_case++) { @@ -236,11 +240,12 @@ void test_json_roundtrip() { for (size_t i = 0; i < strlen(test_case->input); i++) { test_json_roundtrip_message(test_case->input, expected, - serialize_handlers.get(), parser_method.get(), - i); + serialize_handlers, parser_method, i); } } + upb::HandlerCache::Free(serialize_handlercache); + upb::json::CodeCache::Free(parse_codecache); upb::SymbolTable::Free(symtab); } diff --git a/tests/pb/test_encoder.cc b/tests/pb/test_encoder.cc index a0f8453..fac0dae 100644 --- a/tests/pb/test_encoder.cc +++ b/tests/pb/test_encoder.cc @@ -19,6 +19,11 @@ std::string read_string(const char *filename) { void test_pb_roundtrip() { std::string input = read_string("google/protobuf/descriptor.pb"); upb::SymbolTable* symtab = upb::SymbolTable::New(); + upb::HandlerCache* encoder_cache = upb::pb::Encoder::NewCache(); + upb::pb::CodeCache* decoder_cache = upb::pb::CodeCache::New(encoder_cache); + ASSERT(symtab); + ASSERT(encoder_cache); + ASSERT(decoder_cache); upb::Arena arena; google_protobuf_FileDescriptorSet *set = google_protobuf_FileDescriptorSet_parsenew( @@ -37,24 +42,23 @@ void test_pb_roundtrip() { const upb::MessageDef *md = symtab->LookupMessage("google.protobuf.FileDescriptorSet"); ASSERT(md); - printf("name: %s\n", md->full_name()); - upb::reffed_ptr encoder_handlers( - upb::pb::Encoder::NewHandlers(md)); - upb::reffed_ptr method( - upb::pb::DecoderMethod::New( - upb::pb::DecoderMethodOptions(encoder_handlers.get()))); + const upb::Handlers* encoder_handlers = encoder_cache->Get(md); + ASSERT(encoder_handlers); + const upb::pb::DecoderMethod* method = decoder_cache->Get(md); + ASSERT(method); upb::InlinedEnvironment<512> env; std::string output; upb::StringSink string_sink(&output); upb::pb::Encoder* encoder = - upb::pb::Encoder::Create(&env, encoder_handlers.get(), - string_sink.input()); + upb::pb::Encoder::Create(&env, encoder_handlers, string_sink.input()); upb::pb::Decoder* decoder = - upb::pb::Decoder::Create(&env, method.get(), encoder->input()); + upb::pb::Decoder::Create(&env, method, encoder->input()); ok = upb::BufferSource::PutBuffer(input, decoder->input()); ASSERT(ok); ASSERT(input == output); + upb::pb::CodeCache::Free(decoder_cache); + upb::HandlerCache::Free(encoder_cache); upb::SymbolTable::Free(symtab); } diff --git a/upb/bindings/lua/def.c b/upb/bindings/lua/def.c index 00b75c9..76510be 100644 --- a/upb/bindings/lua/def.c +++ b/upb/bindings/lua/def.c @@ -22,15 +22,9 @@ } while (0) -/* lupb_refcounted ************************************************************/ +/* lupb_wrapper ***************************************************************/ -/* All upb objects that use upb_refcounted have a userdata that begins with a - * pointer to that object. Each type has its own metatable. Objects are cached - * in a weak table indexed by the C pointer of the object they are caching. - * - * Note that we consistently use memcpy() to read to/from the object. This - * allows the userdata to use its own struct without violating aliasing, as - * long as it begins with a pointer. */ +/* Wrappers around upb objects. */ /* Checks type; if it matches, pulls the pointer out of the wrapper. */ void *lupb_checkwrapper(lua_State *L, int narg, const char *type) { @@ -263,7 +257,7 @@ void lupb_oneofdef_pushwrapper(lua_State *L, const upb_oneofdef *o) { } const upb_oneofdef *lupb_oneofdef_check(lua_State *L, int narg) { - return lupb_refcounted_check(L, narg, LUPB_ONEOFDEF); + return lupb_checkwrapper(L, narg, LUPB_ONEOFDEF); } static int lupb_oneofdef_containingtype(lua_State *L) { @@ -345,7 +339,7 @@ void lupb_msgdef_pushwrapper(lua_State *L, const upb_msgdef *m) { } const upb_msgdef *lupb_msgdef_check(lua_State *L, int narg) { - return lupb_refcounted_check(L, narg, LUPB_MSGDEF); + return lupb_checkwrapper(L, narg, LUPB_MSGDEF); } static int lupb_msgdef_len(lua_State *L) { @@ -453,7 +447,7 @@ static const struct luaL_Reg lupb_msgdef_m[] = { /* lupb_enumdef ***************************************************************/ const upb_enumdef *lupb_enumdef_check(lua_State *L, int narg) { - return lupb_refcounted_check(L, narg, LUPB_ENUMDEF); + return lupb_checkwrapper(L, narg, LUPB_ENUMDEF); } static void lupb_enumdef_pushwrapper(lua_State *L, const upb_enumdef *e) { @@ -527,7 +521,7 @@ void lupb_filedef_pushwrapper(lua_State *L, const upb_filedef *f) { } const upb_filedef *lupb_filedef_check(lua_State *L, int narg) { - return lupb_refcounted_check(L, narg, LUPB_FILEDEF); + return lupb_checkwrapper(L, narg, LUPB_FILEDEF); } static int lupb_filedef_dep(lua_State *L) { diff --git a/upb/bindings/lua/upb.h b/upb/bindings/lua/upb.h index 9e58f03..6861286 100644 --- a/upb/bindings/lua/upb.h +++ b/upb/bindings/lua/upb.h @@ -84,9 +84,7 @@ void lupb_pushuint32(lua_State *L, uint32_t val); void lupb_pushdouble(lua_State *L, double val); void lupb_pushfloat(lua_State *L, float val); -/* Registers a type with the given name, methods, and metamethods. - * If "refcount_gc" is true, adds a __gc metamethod that does an unref. - * Refcounted types must be allocated with lupb_refcounted_push[new]wrapper. */ +/* Registers a type with the given name, methods, and metamethods. */ void lupb_register_type(lua_State *L, const char *name, const luaL_Reg *m, const luaL_Reg *mm); @@ -98,7 +96,6 @@ void lupb_checkstatus(lua_State *L, upb_status *s); upb_fieldtype_t lupb_checkfieldtype(lua_State *L, int narg); -void *lupb_refcounted_check(lua_State *L, int narg, const char *type); const upb_msgdef *lupb_msgdef_check(lua_State *L, int narg); const upb_enumdef *lupb_enumdef_check(lua_State *L, int narg); const upb_fielddef *lupb_fielddef_check(lua_State *L, int narg); @@ -106,8 +103,6 @@ upb_symtab *lupb_symtab_check(lua_State *L, int narg); void lupb_def_registertypes(lua_State *L); -int lupb_refcounted_gc(lua_State *L); - /** From msg.c. ***************************************************************/ diff --git a/upb/handlers-inl.h b/upb/handlers-inl.h index afc1382..eb9a0fa 100644 --- a/upb/handlers-inl.h +++ b/upb/handlers-inl.h @@ -997,32 +997,12 @@ const T* BufferHandle::GetAttachedObject() const { : NULL; } -inline reffed_ptr Handlers::New(const MessageDef *m) { - upb_handlers *h = upb_handlers_new(m, &h); - return reffed_ptr(h, &h); -} -inline reffed_ptr Handlers::NewFrozen( - const MessageDef *m, upb_handlers_callback *callback, - const void *closure) { - const upb_handlers *h = upb_handlers_newfrozen(m, &h, callback, closure); - return reffed_ptr(h, &h); -} inline const Status* Handlers::status() { return upb_handlers_status(this); } inline void Handlers::ClearError() { return upb_handlers_clearerr(this); } -inline bool Handlers::Freeze(Status *s) { - upb::Handlers* h = this; - return upb_handlers_freeze(&h, 1, s); -} -inline bool Handlers::Freeze(Handlers *const *handlers, int n, Status *s) { - return upb_handlers_freeze(handlers, n, s); -} -inline bool Handlers::Freeze(const std::vector& h, Status* status) { - return upb_handlers_freeze((Handlers* const*)&h[0], h.size(), status); -} inline const MessageDef *Handlers::message_def() const { return upb_handlers_msgdef(this); } @@ -1092,9 +1072,6 @@ inline bool Handlers::SetEndSequenceHandler(const FieldDef *f, handler.AddCleanup(this); return upb_handlers_setendseq(this, f, handler.handler_, &handler.attr_); } -inline bool Handlers::SetSubHandlers(const FieldDef *f, const Handlers *sub) { - return upb_handlers_setsubhandlers(this, f, sub); -} inline const Handlers *Handlers::GetSubHandlers(const FieldDef *f) const { return upb_handlers_getsubhandlers(this, f); } @@ -1116,6 +1093,17 @@ inline const void *Handlers::GetHandlerData(Handlers::Selector selector) { return upb_handlers_gethandlerdata(this, selector); } +inline HandlerCache *HandlerCache::New(upb_handlers_callback *callback, + const void *closure) { + return upb_handlercache_new(callback, closure); +} +inline void HandlerCache::Free(HandlerCache* cache) { + return upb_handlercache_free(cache); +} +const Handlers* HandlerCache::Get(const MessageDef* md) { + return upb_handlercache_get(this, md); +} + inline BytesHandler::BytesHandler() { upb_byteshandler_init(this); } diff --git a/upb/handlers.c b/upb/handlers.c index fa75a48..90fb7b8 100644 --- a/upb/handlers.c +++ b/upb/handlers.c @@ -9,8 +9,8 @@ #include "upb/sink.h" -static void *upb_calloc(size_t size) { - void *mem = upb_gmalloc(size); +static void *upb_calloc(upb_arena *arena, size_t size) { + void *mem = upb_malloc(upb_arena_alloc(arena), size); if (mem) { memset(mem, 0, size); } @@ -21,86 +21,6 @@ static void *upb_calloc(size_t size) { * UPB_NO_CLOSURE. */ char _upb_noclosure; -static void freehandlers(upb_refcounted *r) { - upb_handlers *h = (upb_handlers*)r; - - upb_inttable_iter i; - upb_inttable_begin(&i, &h->cleanup_); - for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { - void *val = (void*)upb_inttable_iter_key(&i); - upb_value func_val = upb_inttable_iter_value(&i); - upb_handlerfree *func = upb_value_getfptr(func_val); - func(val); - } - - upb_inttable_uninit(&h->cleanup_); - upb_gfree(h->sub); - upb_gfree(h); -} - -static void visithandlers(const upb_refcounted *r, upb_refcounted_visit *visit, - void *closure) { - const upb_handlers *h = (const upb_handlers*)r; - upb_msg_field_iter i; - for(upb_msg_field_begin(&i, h->msg); - !upb_msg_field_done(&i); - upb_msg_field_next(&i)) { - upb_fielddef *f = upb_msg_iter_field(&i); - const upb_handlers *sub; - if (!upb_fielddef_issubmsg(f)) continue; - sub = upb_handlers_getsubhandlers(h, f); - if (sub) visit(r, upb_handlers_upcast(sub), closure); - } -} - -static const struct upb_refcounted_vtbl vtbl = {visithandlers, freehandlers}; - -typedef struct { - upb_inttable tab; /* maps upb_msgdef* -> upb_handlers*. */ - upb_handlers_callback *callback; - const void *closure; -} dfs_state; - -/* TODO(haberman): discard upb_handlers* objects that do not actually have any - * handlers set and cannot reach any upb_handlers* object that does. This is - * slightly tricky to do correctly. */ -static upb_handlers *newformsg(const upb_msgdef *m, const void *owner, - dfs_state *s) { - upb_msg_field_iter i; - upb_handlers *h = upb_handlers_new(m, owner); - if (!h) return NULL; - if (!upb_inttable_insertptr(&s->tab, m, upb_value_ptr(h))) goto oom; - - s->callback(s->closure, h); - - /* For each submessage field, get or create a handlers object and set it as - * the subhandlers. */ - for(upb_msg_field_begin(&i, m); - !upb_msg_field_done(&i); - upb_msg_field_next(&i)) { - upb_fielddef *f = upb_msg_iter_field(&i); - const upb_msgdef *subdef; - upb_value subm_ent; - - if (!upb_fielddef_issubmsg(f)) continue; - - subdef = upb_fielddef_msgsubdef(f); - if (upb_inttable_lookupptr(&s->tab, subdef, &subm_ent)) { - upb_handlers_setsubhandlers(h, f, upb_value_getptr(subm_ent)); - } else { - upb_handlers *sub_mh = newformsg(subdef, &sub_mh, s); - if (!sub_mh) goto oom; - upb_handlers_setsubhandlers(h, f, sub_mh); - upb_handlers_unref(sub_mh, &sub_mh); - } - } - return h; - -oom: - upb_handlers_unref(h, owner); - return NULL; -} - /* Given a selector for a STARTSUBMSG handler, resolves to a pointer to the * subhandlers for this submessage field. */ #define SUBH(h, selector) (h->sub[selector]) @@ -111,20 +31,13 @@ oom: static int32_t trygetsel(upb_handlers *h, const upb_fielddef *f, upb_handlertype_t type) { upb_selector_t sel; - UPB_ASSERT(!upb_handlers_isfrozen(h)); - if (upb_handlers_msgdef(h) != upb_fielddef_containingtype(f)) { - upb_status_seterrf( - &h->status_, "type mismatch: field %s does not belong to message %s", - upb_fielddef_name(f), upb_msgdef_fullname(upb_handlers_msgdef(h))); - return -1; - } - if (!upb_handlers_getselector(f, type, &sel)) { - upb_status_seterrf( - &h->status_, - "type mismatch: cannot register handler type %d for field %s", - type, upb_fielddef_name(f)); - return -1; - } + bool ok; + + ok = upb_handlers_getselector(f, type, &sel); + + UPB_ASSERT(upb_handlers_msgdef(h) == upb_fielddef_containingtype(f)); + UPB_ASSERT(ok); + return sel; } @@ -147,19 +60,7 @@ static bool doset(upb_handlers *h, int32_t sel, const upb_fielddef *f, const void *closure_type; const void **context_closure_type; - UPB_ASSERT(!upb_handlers_isfrozen(h)); - - if (sel < 0) { - upb_status_seterrmsg(&h->status_, - "incorrect handler type for this field."); - return false; - } - - if (h->table[sel].func) { - upb_status_seterrmsg(&h->status_, - "cannot change handler once it has been set."); - return false; - } + UPB_ASSERT(!h->table[sel].func); if (attr) { set_attr = *attr; @@ -181,16 +82,7 @@ static bool doset(upb_handlers *h, int32_t sel, const upb_fielddef *f, if (closure_type && *context_closure_type && closure_type != *context_closure_type) { - /* TODO(haberman): better message for debugging. */ - if (f) { - upb_status_seterrf(&h->status_, - "closure type does not match for field %s", - upb_fielddef_name(f)); - } else { - upb_status_seterrmsg( - &h->status_, "closure type does not match for message-level handler"); - } - return false; + UPB_ASSERT(false); } if (closure_type) @@ -203,8 +95,7 @@ static bool doset(upb_handlers *h, int32_t sel, const upb_fielddef *f, const void *table_return_type = upb_handlerattr_returnclosuretype(&h->table[sel].attr); if (return_type && table_return_type && return_type != table_return_type) { - upb_status_seterrmsg(&h->status_, "closure return type does not match"); - return false; + UPB_ASSERT(false); } if (table_return_type && !return_type) @@ -268,80 +159,36 @@ bool checkstart(upb_handlers *h, const upb_fielddef *f, upb_handlertype_t type, return_closure_type = upb_handlerattr_returnclosuretype(attr); if (closure_type && return_closure_type && closure_type != return_closure_type) { - upb_status_seterrf(status, - "expected start handler to return sub type for field %f", - upb_fielddef_name(f)); - return false; + UPB_ASSERT(false); } return true; } -/* Public interface ***********************************************************/ - -upb_handlers *upb_handlers_new(const upb_msgdef *md, const void *owner) { +static upb_handlers *upb_handlers_new(const upb_msgdef *md, upb_handlercache *cache) { int extra; upb_handlers *h; extra = sizeof(upb_handlers_tabent) * (upb_msgdef_selectorcount(md) - 1); - h = upb_calloc(sizeof(*h) + extra); + h = upb_calloc(&cache->arena, sizeof(*h) + extra); if (!h) return NULL; + h->cache = cache; h->msg = md; - upb_status_clear(&h->status_); if (upb_msgdef_submsgfieldcount(md) > 0) { - h->sub = upb_calloc(upb_msgdef_submsgfieldcount(md) * sizeof(*h->sub)); - if (!h->sub) goto oom; + size_t bytes = upb_msgdef_submsgfieldcount(md) * sizeof(*h->sub); + h->sub = upb_calloc(&cache->arena, bytes); + if (!h->sub) return NULL; } else { h->sub = 0; } - if (!upb_refcounted_init(upb_handlers_upcast_mutable(h), &vtbl, owner)) - goto oom; - if (!upb_inttable_init(&h->cleanup_, UPB_CTYPE_FPTR)) goto oom; - /* calloc() above initialized all handlers to NULL. */ return h; - -oom: - freehandlers(upb_handlers_upcast_mutable(h)); - return NULL; } -const upb_handlers *upb_handlers_newfrozen(const upb_msgdef *m, - const void *owner, - upb_handlers_callback *callback, - const void *closure) { - dfs_state state; - upb_handlers *ret; - bool ok; - upb_refcounted *r; - - state.callback = callback; - state.closure = closure; - if (!upb_inttable_init(&state.tab, UPB_CTYPE_PTR)) return NULL; - - ret = newformsg(m, owner, &state); - - upb_inttable_uninit(&state.tab); - if (!ret) return NULL; - - r = upb_handlers_upcast_mutable(ret); - ok = upb_refcounted_freeze(&r, 1, NULL, UPB_MAX_HANDLER_DEPTH); - UPB_ASSERT(ok); - return ret; -} - -const upb_status *upb_handlers_status(upb_handlers *h) { - UPB_ASSERT(!upb_handlers_isfrozen(h)); - return &h->status_; -} - -void upb_handlers_clearerr(upb_handlers *h) { - UPB_ASSERT(!upb_handlers_isfrozen(h)); - upb_status_clear(&h->status_); -} +/* Public interface ***********************************************************/ #define SETTER(name, handlerctype, handlertype) \ bool upb_handlers_set ## name(upb_handlers *h, const upb_fielddef *f, \ @@ -381,7 +228,6 @@ bool upb_handlers_setstartmsg(upb_handlers *h, upb_startmsg_handlerfunc *func, bool upb_handlers_setendmsg(upb_handlers *h, upb_endmsg_handlerfunc *func, upb_handlerattr *attr) { - UPB_ASSERT(!upb_handlers_isfrozen(h)); return doset(h, UPB_ENDMSG_SELECTOR, NULL, UPB_HANDLER_INT32, (upb_func *)func, attr); } @@ -389,14 +235,12 @@ bool upb_handlers_setendmsg(upb_handlers *h, upb_endmsg_handlerfunc *func, bool upb_handlers_setsubhandlers(upb_handlers *h, const upb_fielddef *f, const upb_handlers *sub) { UPB_ASSERT(sub); - UPB_ASSERT(!upb_handlers_isfrozen(h)); UPB_ASSERT(upb_fielddef_issubmsg(f)); if (SUBH_F(h, f)) return false; /* Can't reset. */ if (upb_handlers_msgdef(sub) != upb_fielddef_msgsubdef(f)) { return false; } SUBH_F(h, f) = sub; - upb_ref2(sub, h); return true; } @@ -424,101 +268,14 @@ const upb_msgdef *upb_handlers_msgdef(const upb_handlers *h) { return h->msg; } bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *func) { bool ok; - if (upb_inttable_lookupptr(&h->cleanup_, p, NULL)) { + if (upb_inttable_lookupptr(&h->cache->cleanup_, p, NULL)) { return false; } - ok = upb_inttable_insertptr(&h->cleanup_, p, upb_value_fptr(func)); + ok = upb_inttable_insertptr(&h->cache->cleanup_, p, upb_value_fptr(func)); UPB_ASSERT(ok); return true; } - -/* "Static" methods ***********************************************************/ - -bool upb_handlers_freeze(upb_handlers *const*handlers, int n, upb_status *s) { - /* TODO: verify we have a transitive closure. */ - int i; - for (i = 0; i < n; i++) { - upb_msg_field_iter j; - upb_handlers *h = handlers[i]; - - if (!upb_ok(&h->status_)) { - upb_status_seterrf(s, "handlers for message %s had error status: %s", - upb_msgdef_fullname(upb_handlers_msgdef(h)), - upb_status_errmsg(&h->status_)); - return false; - } - - /* Check that there are no closure mismatches due to missing Start* handlers - * or subhandlers with different type-level types. */ - for(upb_msg_field_begin(&j, h->msg); - !upb_msg_field_done(&j); - upb_msg_field_next(&j)) { - - const upb_fielddef *f = upb_msg_iter_field(&j); - if (upb_fielddef_isseq(f)) { - if (!checkstart(h, f, UPB_HANDLER_STARTSEQ, s)) - return false; - } - - if (upb_fielddef_isstring(f)) { - if (!checkstart(h, f, UPB_HANDLER_STARTSTR, s)) - return false; - } - - if (upb_fielddef_issubmsg(f)) { - bool hashandler = false; - if (upb_handlers_gethandler( - h, handlers_getsel(h, f, UPB_HANDLER_STARTSUBMSG)) || - upb_handlers_gethandler( - h, handlers_getsel(h, f, UPB_HANDLER_ENDSUBMSG))) { - hashandler = true; - } - - if (upb_fielddef_isseq(f) && - (upb_handlers_gethandler( - h, handlers_getsel(h, f, UPB_HANDLER_STARTSEQ)) || - upb_handlers_gethandler( - h, handlers_getsel(h, f, UPB_HANDLER_ENDSEQ)))) { - hashandler = true; - } - - if (hashandler && !upb_handlers_getsubhandlers(h, f)) { - /* For now we add an empty subhandlers in this case. It makes the - * decoder code generator simpler, because it only has to handle two - * cases (submessage has handlers or not) as opposed to three - * (submessage has handlers in enclosing message but no subhandlers). - * - * This makes parsing less efficient in the case that we want to - * notice a submessage but skip its contents (like if we're testing - * for submessage presence or counting the number of repeated - * submessages). In this case we will end up parsing the submessage - * field by field and throwing away the results for each, instead of - * skipping the whole delimited thing at once. If this is an issue we - * can revisit it, but do remember that this only arises when you have - * handlers (startseq/startsubmsg/endsubmsg/endseq) set for the - * submessage but no subhandlers. The uses cases for this are - * limited. */ - upb_handlers *sub = upb_handlers_new(upb_fielddef_msgsubdef(f), &sub); - upb_handlers_setsubhandlers(h, f, sub); - upb_handlers_unref(sub, &sub); - } - - /* TODO(haberman): check type of submessage. - * This is slightly tricky; also consider whether we should check that - * they match at setsubhandlers time. */ - } - } - } - - if (!upb_refcounted_freeze((upb_refcounted*const*)handlers, n, s, - UPB_MAX_HANDLER_DEPTH)) { - return false; - } - - return true; -} - upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f) { switch (upb_fielddef_type(f)) { case UPB_TYPE_INT32: @@ -616,6 +373,85 @@ uint32_t upb_handlers_selectorcount(const upb_fielddef *f) { return ret; } +/* upb_handlercache ***********************************************************/ + +const upb_handlers *upb_handlercache_get(upb_handlercache *c, + const upb_msgdef *md) { + upb_msg_field_iter i; + upb_value v; + upb_handlers *h; + + if (upb_inttable_lookupptr(&c->tab, md, &v)) { + return upb_value_getptr(v); + } + + h = upb_handlers_new(md, c); + v = upb_value_ptr(h); + + if (!h) return NULL; + if (!upb_inttable_insertptr(&c->tab, md, v)) return NULL; + + c->callback(c->closure, h); + + /* For each submessage field, get or create a handlers object and set it as + * the subhandlers. */ + for(upb_msg_field_begin(&i, md); + !upb_msg_field_done(&i); + upb_msg_field_next(&i)) { + upb_fielddef *f = upb_msg_iter_field(&i); + + if (upb_fielddef_issubmsg(f)) { + const upb_msgdef *subdef = upb_fielddef_msgsubdef(f); + const upb_handlers *sub_mh = upb_handlercache_get(c, subdef); + + if (!sub_mh) return NULL; + + upb_handlers_setsubhandlers(h, f, sub_mh); + } + } + + return h; +} + + +upb_handlercache *upb_handlercache_new(upb_handlers_callback *callback, + const void *closure) { + upb_handlercache *cache = upb_gmalloc(sizeof(*cache)); + + if (!cache) return NULL; + + upb_arena_init(&cache->arena); + + cache->callback = callback; + cache->closure = closure; + + if (!upb_inttable_init(&cache->tab, UPB_CTYPE_PTR)) goto oom; + if (!upb_inttable_init(&cache->cleanup_, UPB_CTYPE_FPTR)) goto oom; + + return cache; + +oom: + upb_gfree(cache); + return NULL; +} + +void upb_handlercache_free(upb_handlercache *cache) { + upb_inttable_iter i; + + upb_inttable_begin(&i, &cache->cleanup_); + for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { + void *val = (void*)upb_inttable_iter_key(&i); + upb_value func_val = upb_inttable_iter_value(&i); + upb_handlerfree *func = upb_value_getfptr(func_val); + func(val); + } + + upb_inttable_uninit(&cache->tab); + upb_inttable_uninit(&cache->cleanup_); + upb_arena_uninit(&cache->arena); + upb_gfree(cache); +} + /* upb_handlerattr ************************************************************/ diff --git a/upb/handlers.h b/upb/handlers.h index 15d06a0..741bd48 100644 --- a/upb/handlers.h +++ b/upb/handlers.h @@ -21,7 +21,6 @@ #include "upb/def.h" #include "upb/table.int.h" -#include "upb/refcounted.h" #ifdef __cplusplus namespace upb { @@ -29,6 +28,7 @@ class BufferHandle; class BytesHandler; class HandlerAttributes; class Handlers; +class HandlerCache; template class Handler; template struct CanonicalType; } /* namespace upb */ @@ -37,8 +37,8 @@ template struct CanonicalType; UPB_DECLARE_TYPE(upb::BufferHandle, upb_bufhandle) UPB_DECLARE_TYPE(upb::BytesHandler, upb_byteshandler) UPB_DECLARE_TYPE(upb::HandlerAttributes, upb_handlerattr) -UPB_DECLARE_DERIVED_TYPE(upb::Handlers, upb::RefCounted, - upb_handlers, upb_refcounted) +UPB_DECLARE_TYPE(upb::Handlers, upb_handlers) +UPB_DECLARE_TYPE(upb::HandlerCache, upb_handlercache) /* The maximum depth that the handler graph can have. This is a resource limit * for the C stack since we sometimes need to recursively traverse the graph. @@ -275,22 +275,6 @@ class upb::Handlers { typedef void HandlersCallback(const void *closure, upb_handlers *h); - /* Returns a new handlers object for the given frozen msgdef. - * Returns NULL if memory allocation failed. */ - static reffed_ptr New(const MessageDef *m); - - /* Convenience function for registering a graph of handlers that mirrors the - * graph of msgdefs for some message. For "m" and all its children a new set - * of handlers will be created and the given callback will be invoked, - * allowing the client to register handlers for this message. Note that any - * subhandlers set by the callback will be overwritten. */ - static reffed_ptr NewFrozen(const MessageDef *m, - HandlersCallback *callback, - const void *closure); - - /* Functionality from upb::RefCounted. */ - UPB_REFCOUNTED_CPPMETHODS - /* All handler registration functions return bool to indicate success or * failure; details about failures are stored in this status object. If a * failure does occur, it must be cleared before the Handlers are frozen, @@ -299,16 +283,6 @@ class upb::Handlers { const Status* status(); void ClearError(); - /* Call to freeze these Handlers. Requires that any SubHandlers are already - * frozen. For cycles, you must use the static version below and freeze the - * whole graph at once. */ - bool Freeze(Status* s); - - /* Freezes the given set of handlers. You may not freeze a handler without - * also freezing any handlers they point to. */ - static bool Freeze(Handlers*const* handlers, int n, Status* s); - static bool Freeze(const std::vector& handlers, Status* s); - /* Returns the msgdef associated with this handlers object. */ const MessageDef* message_def() const; @@ -473,9 +447,8 @@ class upb::Handlers { */ bool SetEndSequenceHandler(const FieldDef* f, const EndFieldHandler& h); - /* Sets or gets the object that specifies handlers for the given field, which + /* Gets the object that specifies handlers for the given field, which * must be a submessage or group. Returns NULL if no handlers are set. */ - bool SetSubHandlers(const FieldDef* f, const Handlers* sub); const Handlers* GetSubHandlers(const FieldDef* f) const; /* Equivalent to GetSubHandlers, but takes the STARTSUBMSG selector for the @@ -519,13 +492,10 @@ class upb::Handlers { #else struct upb_handlers { #endif - upb_refcounted base; - + upb_handlercache *cache; const upb_msgdef *msg; const upb_handlers **sub; const void *top_closure_type; - upb_inttable cleanup_; - upb_status status_; /* Used only when mutable. */ upb_handlers_tabent table[1]; /* Dynamically-sized field handler array. */ }; @@ -675,17 +645,6 @@ UPB_INLINE const void *upb_handlerattr_handlerdata( } /* upb_handlers */ -typedef void upb_handlers_callback(const void *closure, upb_handlers *h); -upb_handlers *upb_handlers_new(const upb_msgdef *m, - const void *owner); -const upb_handlers *upb_handlers_newfrozen(const upb_msgdef *m, - const void *owner, - upb_handlers_callback *callback, - const void *closure); - -/* Include refcounted methods like upb_handlers_ref(). */ -UPB_REFCOUNTED_CMETHODS(upb_handlers, upb_handlers_upcast) - const upb_status *upb_handlers_status(upb_handlers *h); void upb_handlers_clearerr(upb_handlers *h); const upb_msgdef *upb_handlers_msgdef(const upb_handlers *h); @@ -737,8 +696,6 @@ bool upb_handlers_setendseq(upb_handlers *h, const upb_fielddef *f, upb_endfield_handlerfunc *func, upb_handlerattr *attr); -bool upb_handlers_setsubhandlers(upb_handlers *h, const upb_fielddef *f, - const upb_handlers *sub); const upb_handlers *upb_handlers_getsubhandlers(const upb_handlers *h, const upb_fielddef *f); const upb_handlers *upb_handlers_getsubhandlers_sel(const upb_handlers *h, @@ -757,6 +714,36 @@ UPB_INLINE const void *upb_handlers_gethandlerdata(const upb_handlers *h, return upb_handlerattr_handlerdata(&h->table[s].attr); } +typedef void upb_handlers_callback(const void *closure, upb_handlers *h); + +#ifdef __cplusplus + +class upb::HandlerCache { + public: + static HandlerCache *New(upb_handlers_callback *callback, + const void *closure); + static void Free(HandlerCache* cache); + + const Handlers* Get(const MessageDef* md); + + private: + UPB_DISALLOW_POD_OPS(HandlerCache, upb::pb::HandlerCache) +#else +struct upb_handlercache { +#endif + upb_arena arena; + upb_inttable tab; /* maps upb_msgdef* -> upb_handlers*. */ + upb_inttable cleanup_; + upb_handlers_callback *callback; + const void *closure; +}; + +upb_handlercache *upb_handlercache_new(upb_handlers_callback *callback, + const void *closure); +void upb_handlercache_free(upb_handlercache *cache); +const upb_handlers *upb_handlercache_get(upb_handlercache *cache, + const upb_msgdef *md); + #ifdef __cplusplus /* Handler types for single fields. @@ -788,7 +775,6 @@ bool upb_byteshandler_setendstr(upb_byteshandler *h, upb_endfield_handlerfunc *func, void *d); /* "Static" methods */ -bool upb_handlers_freeze(upb_handlers *const *handlers, int n, upb_status *s); upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f); bool upb_handlers_getselector(const upb_fielddef *f, upb_handlertype_t type, upb_selector_t *s); diff --git a/upb/json/parser.c b/upb/json/parser.c index 90a401d..4bc9163 100644 --- a/upb/json/parser.c +++ b/upb/json/parser.c @@ -154,14 +154,13 @@ void upb_stringsink_uninit(upb_stringsink *sink) { free(sink->ptr); } typedef struct { /* For encoding Any value field in binary format. */ - const upb_handlers *encoder_handlers; - upb_pb_encoder *encoder; + upb_handlercache *encoder_handlercache; upb_stringsink stringsink; /* For decoding Any value field in json format. */ - upb_json_parsermethod *parser_method; - upb_json_parser* parser; + upb_json_codecache *parser_codecache; upb_sink sink; + upb_json_parser *parser; /* Mark the range of uninterpreted values in json input before type url. */ const char *before_type_url_start; @@ -180,7 +179,7 @@ typedef struct { const upb_fielddef *f; /* The table mapping json name to fielddef for this message. */ - upb_strtable *name_table; + const upb_strtable *name_table; /* We are in a repeated-field context, ready to emit mapentries as * submessages. This flag alters the start-of-object (open-brace) behavior to @@ -259,60 +258,67 @@ struct upb_json_parser { struct tm tm; }; -struct upb_json_parsermethod { - upb_refcounted base; +struct upb_json_codecache { + upb_arena arena; + upb_inttable methods; /* upb_msgdef* -> upb_json_parsermethod* */ +}; +struct upb_json_parsermethod { + const upb_json_codecache *cache; upb_byteshandler input_handler_; - /* Keys are upb_msgdef*, values are upb_strtable (json_name -> fielddef) */ - upb_inttable name_tables; + /* Maps json_name -> fielddef */ + upb_strtable name_table; }; #define PARSER_CHECK_RETURN(x) if (!(x)) return false -static void json_parser_any_frame_reset(upb_jsonparser_any_frame *frame) { - frame->encoder_handlers = NULL; - frame->encoder = NULL; - frame->parser_method = NULL; +static upb_jsonparser_any_frame *json_parser_any_frame_new( + upb_json_parser *p) { + upb_jsonparser_any_frame *frame; + + frame = upb_env_malloc(p->env, sizeof(upb_jsonparser_any_frame)); + + frame->encoder_handlercache = upb_pb_encoder_newcache(); + frame->parser_codecache = upb_json_codecache_new(); frame->parser = NULL; frame->before_type_url_start = NULL; frame->before_type_url_end = NULL; frame->after_type_url_start = NULL; + + upb_stringsink_init(&frame->stringsink); + + return frame; } static void json_parser_any_frame_set_payload_type( upb_json_parser *p, upb_jsonparser_any_frame *frame, const upb_msgdef *payload_type) { + const upb_handlers *h; + const upb_json_parsermethod *parser_method; + upb_pb_encoder *encoder; + /* Initialize encoder. */ - frame->encoder_handlers = - upb_pb_encoder_newhandlers(payload_type, &frame->encoder_handlers); - upb_stringsink_init(&frame->stringsink); - frame->encoder = - upb_pb_encoder_create( - p->env, frame->encoder_handlers, - &frame->stringsink.sink); + h = upb_handlercache_get(frame->encoder_handlercache, payload_type); + encoder = upb_pb_encoder_create(p->env, h, &frame->stringsink.sink); /* Initialize parser. */ - frame->parser_method = - upb_json_parsermethod_new(payload_type, &frame->parser_method); - upb_sink_reset(&frame->sink, frame->encoder_handlers, frame->encoder); - frame->parser = - upb_json_parser_create(p->env, frame->parser_method, p->symtab, - &frame->sink, p->ignore_json_unknown); + parser_method = upb_json_codecache_get(frame->parser_codecache, payload_type); + upb_sink_reset(&frame->sink, h, encoder); + frame->parser = upb_json_parser_create(p->env, parser_method, p->symtab, + &frame->sink, p->ignore_json_unknown); } static void json_parser_any_frame_free(upb_jsonparser_any_frame *frame) { - upb_handlers_unref(frame->encoder_handlers, - &frame->encoder_handlers); - upb_json_parsermethod_unref(frame->parser_method, - &frame->parser_method); + upb_handlercache_free(frame->encoder_handlercache); + upb_json_codecache_free(frame->parser_codecache); upb_stringsink_uninit(&frame->stringsink); } static bool json_parser_any_frame_has_type_url( upb_jsonparser_any_frame *frame) { - return frame->encoder != NULL; + return frame->parser != NULL; } static bool json_parser_any_frame_has_value_before_type_url( @@ -334,7 +340,7 @@ static bool json_parser_any_frame_has_value( static void json_parser_any_frame_set_before_type_url_end( upb_jsonparser_any_frame *frame, const char *ptr) { - if (frame->encoder == NULL) { + if (frame->parser == NULL) { frame->before_type_url_end = ptr; } } @@ -376,9 +382,12 @@ static bool check_stack(upb_json_parser *p) { static void set_name_table(upb_json_parser *p, upb_jsonparser_frame *frame) { upb_value v; - bool ok = upb_inttable_lookupptr(&p->method->name_tables, frame->m, &v); + const upb_json_codecache *cache = p->method->cache; + bool ok = upb_inttable_lookupptr(&cache->methods, frame->m, &v); + const upb_json_parsermethod *method = upb_value_getptr(v); UPB_ASSERT(ok); - frame->name_table = upb_value_getptr(v); + + frame->name_table = &method->name_table; } /* There are GCC/Clang built-ins for overflow checking which we could start @@ -1929,9 +1938,7 @@ static bool start_subobject(upb_json_parser *p) { if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) { p->top->is_any = true; - p->top->any_frame = - upb_env_malloc(p->env, sizeof(upb_jsonparser_any_frame)); - json_parser_any_frame_reset(p->top->any_frame); + p->top->any_frame = json_parser_any_frame_new(p); } else { p->top->is_any = false; p->top->any_frame = NULL; @@ -2409,11 +2416,11 @@ static bool is_string_wrapper_object(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 2571 "upb/json/parser.rl" +#line 2578 "upb/json/parser.rl" -#line 2417 "upb/json/parser.c" +#line 2424 "upb/json/parser.c" static const char _json_actions[] = { 0, 1, 0, 1, 1, 1, 3, 1, 4, 1, 6, 1, 7, 1, 8, 1, @@ -2660,7 +2667,7 @@ static const int json_en_value_machine = 75; static const int json_en_main = 1; -#line 2574 "upb/json/parser.rl" +#line 2581 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -2683,7 +2690,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 2687 "upb/json/parser.c" +#line 2694 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -2758,83 +2765,83 @@ _match: switch ( *_acts++ ) { case 1: -#line 2422 "upb/json/parser.rl" +#line 2429 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 2424 "upb/json/parser.rl" +#line 2431 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 23;goto _again;} } break; case 3: -#line 2428 "upb/json/parser.rl" +#line 2435 "upb/json/parser.rl" { start_text(parser, p); } break; case 4: -#line 2429 "upb/json/parser.rl" +#line 2436 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -#line 2435 "upb/json/parser.rl" +#line 2442 "upb/json/parser.rl" { start_hex(parser); } break; case 6: -#line 2436 "upb/json/parser.rl" +#line 2443 "upb/json/parser.rl" { hexdigit(parser, p); } break; case 7: -#line 2437 "upb/json/parser.rl" +#line 2444 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -#line 2443 "upb/json/parser.rl" +#line 2450 "upb/json/parser.rl" { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -#line 2449 "upb/json/parser.rl" +#line 2456 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 10: -#line 2461 "upb/json/parser.rl" +#line 2468 "upb/json/parser.rl" { start_duration_base(parser, p); } break; case 11: -#line 2462 "upb/json/parser.rl" +#line 2469 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_duration_base(parser, p)); } break; case 12: -#line 2464 "upb/json/parser.rl" +#line 2471 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 13: -#line 2469 "upb/json/parser.rl" +#line 2476 "upb/json/parser.rl" { start_timestamp_base(parser, p); } break; case 14: -#line 2470 "upb/json/parser.rl" +#line 2477 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_base(parser, p)); } break; case 15: -#line 2472 "upb/json/parser.rl" +#line 2479 "upb/json/parser.rl" { start_timestamp_fraction(parser, p); } break; case 16: -#line 2473 "upb/json/parser.rl" +#line 2480 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } break; case 17: -#line 2475 "upb/json/parser.rl" +#line 2482 "upb/json/parser.rl" { start_timestamp_zone(parser, p); } break; case 18: -#line 2476 "upb/json/parser.rl" +#line 2483 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } break; case 19: -#line 2478 "upb/json/parser.rl" +#line 2485 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 20: -#line 2483 "upb/json/parser.rl" +#line 2490 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) { {stack[top++] = cs; cs = 47;goto _again;} @@ -2846,11 +2853,11 @@ _match: } break; case 21: -#line 2494 "upb/json/parser.rl" +#line 2501 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 75;goto _again;} } break; case 22: -#line 2499 "upb/json/parser.rl" +#line 2506 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_member(parser, p); @@ -2860,11 +2867,11 @@ _match: } break; case 23: -#line 2506 "upb/json/parser.rl" +#line 2513 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_membername(parser)); } break; case 24: -#line 2509 "upb/json/parser.rl" +#line 2516 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { end_any_member(parser, p); @@ -2874,7 +2881,7 @@ _match: } break; case 25: -#line 2520 "upb/json/parser.rl" +#line 2527 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_object(parser, p); @@ -2884,7 +2891,7 @@ _match: } break; case 26: -#line 2529 "upb/json/parser.rl" +#line 2536 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { CHECK_RETURN_TOP(end_any_object(parser, p)); @@ -2894,54 +2901,54 @@ _match: } break; case 27: -#line 2541 "upb/json/parser.rl" +#line 2548 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_array(parser)); } break; case 28: -#line 2545 "upb/json/parser.rl" +#line 2552 "upb/json/parser.rl" { end_array(parser); } break; case 29: -#line 2550 "upb/json/parser.rl" +#line 2557 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_number(parser, p)); } break; case 30: -#line 2551 "upb/json/parser.rl" +#line 2558 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 31: -#line 2553 "upb/json/parser.rl" +#line 2560 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_stringval(parser)); } break; case 32: -#line 2554 "upb/json/parser.rl" +#line 2561 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_stringval(parser)); } break; case 33: -#line 2556 "upb/json/parser.rl" +#line 2563 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 34: -#line 2558 "upb/json/parser.rl" +#line 2565 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 35: -#line 2560 "upb/json/parser.rl" +#line 2567 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 36: -#line 2562 "upb/json/parser.rl" +#line 2569 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_subobject_full(parser)); } break; case 37: -#line 2563 "upb/json/parser.rl" +#line 2570 "upb/json/parser.rl" { end_subobject_full(parser); } break; case 38: -#line 2568 "upb/json/parser.rl" +#line 2575 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 2945 "upb/json/parser.c" +#line 2952 "upb/json/parser.c" } } @@ -2958,32 +2965,32 @@ _again: while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 0: -#line 2420 "upb/json/parser.rl" +#line 2427 "upb/json/parser.rl" { p--; {cs = stack[--top]; if ( p == pe ) goto _test_eof; goto _again;} } break; case 30: -#line 2551 "upb/json/parser.rl" +#line 2558 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 33: -#line 2556 "upb/json/parser.rl" +#line 2563 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 34: -#line 2558 "upb/json/parser.rl" +#line 2565 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 35: -#line 2560 "upb/json/parser.rl" +#line 2567 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 37: -#line 2563 "upb/json/parser.rl" +#line 2570 "upb/json/parser.rl" { end_subobject_full(parser); } break; -#line 2987 "upb/json/parser.c" +#line 2994 "upb/json/parser.c" } } } @@ -2991,7 +2998,7 @@ goto _again;} } _out: {} } -#line 2596 "upb/json/parser.rl" +#line 2603 "upb/json/parser.rl" if (p != pe) { upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p); @@ -3039,13 +3046,13 @@ static void json_parser_reset(upb_json_parser *p) { /* Emit Ragel initialization of the parser. */ -#line 3043 "upb/json/parser.c" +#line 3050 "upb/json/parser.c" { cs = json_start; top = 0; } -#line 2643 "upb/json/parser.rl" +#line 2650 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); @@ -3055,70 +3062,46 @@ static void json_parser_reset(upb_json_parser *p) { upb_status_clear(&p->status); } -static void free_json_parsermethod(upb_refcounted *r) { - upb_json_parsermethod *method = (upb_json_parsermethod*)r; - - upb_inttable_iter i; - upb_inttable_begin(&i, &method->name_tables); - for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { - upb_value val = upb_inttable_iter_value(&i); - upb_strtable *t = upb_value_getptr(val); - upb_strtable_uninit(t); - upb_gfree(t); - } - - upb_inttable_uninit(&method->name_tables); +static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c, + const upb_msgdef *md) { + upb_msg_field_iter i; + upb_alloc *alloc = upb_arena_alloc(&c->arena); - upb_gfree(r); -} + upb_json_parsermethod *m = upb_gmalloc(sizeof(*m)); -static void add_jsonname_table(upb_json_parsermethod *m, const upb_msgdef* md) { - upb_msg_field_iter i; - upb_strtable *t; + m->cache = c; - /* It would be nice to stack-allocate this, but protobufs do not limit the - * length of fields to any reasonable limit. */ - char *buf = NULL; - size_t len = 0; + upb_byteshandler_init(&m->input_handler_); + upb_byteshandler_setstring(&m->input_handler_, parse, m); + upb_byteshandler_setendstr(&m->input_handler_, end, m); - if (upb_inttable_lookupptr(&m->name_tables, md, NULL)) { - return; - } + upb_strtable_init2(&m->name_table, UPB_CTYPE_CONSTPTR, alloc); - /* TODO(haberman): handle malloc failure. */ - t = upb_gmalloc(sizeof(*t)); - upb_strtable_init(t, UPB_CTYPE_CONSTPTR); - upb_inttable_insertptr(&m->name_tables, md, upb_value_ptr(t)); + /* Build name_table */ for(upb_msg_field_begin(&i, md); !upb_msg_field_done(&i); upb_msg_field_next(&i)) { const upb_fielddef *f = upb_msg_iter_field(&i); + upb_value v = upb_value_constptr(f); + char *buf; /* Add an entry for the JSON name. */ - size_t field_len = upb_fielddef_getjsonname(f, buf, len); - if (field_len > len) { - size_t len2; - buf = upb_grealloc(buf, 0, field_len); - len = field_len; - len2 = upb_fielddef_getjsonname(f, buf, len); - UPB_ASSERT(len == len2); - } - upb_strtable_insert(t, buf, upb_value_constptr(f)); + size_t len = upb_fielddef_getjsonname(f, NULL, 0); + buf = upb_malloc(alloc, len); + upb_fielddef_getjsonname(f, buf, len); + upb_strtable_insert3(&m->name_table, buf, len, v, alloc); if (strcmp(buf, upb_fielddef_name(f)) != 0) { /* Since the JSON name is different from the regular field name, add an * entry for the raw name (compliant proto3 JSON parsers must accept * both). */ - upb_strtable_insert(t, upb_fielddef_name(f), upb_value_constptr(f)); - } - - if (upb_fielddef_issubmsg(f)) { - add_jsonname_table(m, upb_fielddef_msgsubdef(f)); + const char *name = upb_fielddef_name(f); + upb_strtable_insert3(&m->name_table, name, strlen(name), v, alloc); } } - upb_gfree(buf); + return m; } /* Public API *****************************************************************/ @@ -3146,9 +3129,7 @@ upb_json_parser *upb_json_parser_create(upb_env *env, p->top->m = upb_handlers_msgdef(output->handlers); if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) { p->top->is_any = true; - p->top->any_frame = - upb_env_malloc(p->env, sizeof(upb_jsonparser_any_frame)); - json_parser_any_frame_reset(p->top->any_frame); + p->top->any_frame = json_parser_any_frame_new(p); } else { p->top->is_any = false; p->top->any_frame = NULL; @@ -3169,24 +3150,61 @@ upb_bytessink *upb_json_parser_input(upb_json_parser *p) { return &p->input_; } -upb_json_parsermethod *upb_json_parsermethod_new(const upb_msgdef* md, - const void* owner) { - static const struct upb_refcounted_vtbl vtbl = {NULL, free_json_parsermethod}; - upb_json_parsermethod *ret = upb_gmalloc(sizeof(*ret)); - upb_refcounted_init(upb_json_parsermethod_upcast_mutable(ret), &vtbl, owner); +const upb_byteshandler *upb_json_parsermethod_inputhandler( + const upb_json_parsermethod *m) { + return &m->input_handler_; +} + +upb_json_codecache *upb_json_codecache_new() { + upb_alloc *alloc; + upb_json_codecache *c; - upb_byteshandler_init(&ret->input_handler_); - upb_byteshandler_setstring(&ret->input_handler_, parse, ret); - upb_byteshandler_setendstr(&ret->input_handler_, end, ret); + c = upb_gmalloc(sizeof(*c)); - upb_inttable_init(&ret->name_tables, UPB_CTYPE_PTR); + upb_arena_init(&c->arena); + alloc = upb_arena_alloc(&c->arena); - add_jsonname_table(ret, md); + upb_inttable_init2(&c->methods, UPB_CTYPE_CONSTPTR, alloc); - return ret; + return c; } -const upb_byteshandler *upb_json_parsermethod_inputhandler( - const upb_json_parsermethod *m) { - return &m->input_handler_; +void upb_json_codecache_free(upb_json_codecache *c) { + upb_arena_uninit(&c->arena); + upb_gfree(c); +} + +upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c, + const upb_msgdef *md) { + upb_json_parsermethod *m; + upb_value v; + upb_msg_field_iter i; + + if (upb_inttable_lookupptr(&c->methods, md, &v)) { + return upb_value_getptr(v); + } + + m = parsermethod_new(c, md); + v = upb_value_ptr(m); + + if (!m) return NULL; + if (!upb_inttable_insertptr(&c->methods, m, v)) return NULL; + + /* Populate parser methods for all submessages, so the name tables will + * be available during parsing. */ + for(upb_msg_field_begin(&i, md); + !upb_msg_field_done(&i); + upb_msg_field_next(&i)) { + upb_fielddef *f = upb_msg_iter_field(&i); + + if (upb_fielddef_issubmsg(f)) { + const upb_msgdef *subdef = upb_fielddef_msgsubdef(f); + const upb_json_parsermethod *sub_method = + upb_json_codecache_get(c, subdef); + + if (!sub_method) return NULL; + } + } + + return m; } diff --git a/upb/json/parser.h b/upb/json/parser.h index 91b08d8..d5ec396 100644 --- a/upb/json/parser.h +++ b/upb/json/parser.h @@ -13,6 +13,7 @@ #ifdef __cplusplus namespace upb { namespace json { +class CodeCache; class Parser; class ParserMethod; } /* namespace json */ @@ -20,8 +21,8 @@ class ParserMethod; #endif UPB_DECLARE_TYPE(upb::json::Parser, upb_json_parser) -UPB_DECLARE_DERIVED_TYPE(upb::json::ParserMethod, upb::RefCounted, - upb_json_parsermethod, upb_refcounted) +UPB_DECLARE_TYPE(upb::json::ParserMethod, upb_json_parsermethod) +UPB_DECLARE_TYPE(upb::json::CodeCache, upb_json_codecache) /* upb::json::Parser **********************************************************/ @@ -49,18 +50,6 @@ class upb::json::Parser { class upb::json::ParserMethod { public: - /* Include base methods from upb::ReferenceCounted. */ - UPB_REFCOUNTED_CPPMETHODS - - /* Returns handlers for parsing according to the specified schema. - * The MessageDef must outlive the ParserMethod. */ - static reffed_ptr New(const upb::MessageDef* md); - - /* The destination handlers that are statically bound to this method. - * This method is only capable of outputting to a sink that uses these - * handlers. */ - const Handlers* dest_handlers() const; - /* The input handlers for this decoder method. */ const BytesHandler* input_handler() const; @@ -68,6 +57,19 @@ class upb::json::ParserMethod { UPB_DISALLOW_POD_OPS(ParserMethod, upb::json::ParserMethod) }; +class upb::json::CodeCache { + public: + static CodeCache* New(); + static void Free(CodeCache* cache); + + /* Returns a DecoderMethod that can push data to the given handlers. + * If a suitable method already exists, it will be returned from the cache. */ + const ParserMethod *Get(const MessageDef* md); + + private: + UPB_DISALLOW_POD_OPS(CodeCache, upb::json::CodeCache) +}; + #endif UPB_BEGIN_EXTERN_C @@ -79,15 +81,13 @@ upb_json_parser* upb_json_parser_create(upb_env* e, bool ignore_json_unknown); upb_bytessink *upb_json_parser_input(upb_json_parser *p); -upb_json_parsermethod* upb_json_parsermethod_new(const upb_msgdef* md, - const void* owner); -const upb_handlers *upb_json_parsermethod_desthandlers( - const upb_json_parsermethod *m); const upb_byteshandler *upb_json_parsermethod_inputhandler( const upb_json_parsermethod *m); -/* Include refcounted methods like upb_json_parsermethod_ref(). */ -UPB_REFCOUNTED_CMETHODS(upb_json_parsermethod, upb_json_parsermethod_upcast) +upb_json_codecache *upb_json_codecache_new(); +void upb_json_codecache_free(upb_json_codecache *cache); +upb_json_parsermethod* upb_json_codecache_get(upb_json_codecache* cache, + const upb_msgdef* md); UPB_END_EXTERN_C @@ -105,17 +105,12 @@ inline BytesSink* Parser::input() { return upb_json_parser_input(this); } -inline const Handlers* ParserMethod::dest_handlers() const { - return upb_json_parsermethod_desthandlers(this); -} inline const BytesHandler* ParserMethod::input_handler() const { return upb_json_parsermethod_inputhandler(this); } /* static */ -inline reffed_ptr ParserMethod::New( - const MessageDef* md) { - const upb_json_parsermethod *m = upb_json_parsermethod_new(md, &m); - return reffed_ptr(m, &m); +inline const ParserMethod* CodeCache::Get(const MessageDef* md) { + return upb_json_codecache_get(this, md); } } /* namespace json */ @@ -123,5 +118,4 @@ inline reffed_ptr ParserMethod::New( #endif - #endif /* UPB_JSON_PARSER_H_ */ diff --git a/upb/json/parser.rl b/upb/json/parser.rl index 3a32fd9..c2866c9 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -152,14 +152,13 @@ void upb_stringsink_uninit(upb_stringsink *sink) { free(sink->ptr); } typedef struct { /* For encoding Any value field in binary format. */ - const upb_handlers *encoder_handlers; - upb_pb_encoder *encoder; + upb_handlercache *encoder_handlercache; upb_stringsink stringsink; /* For decoding Any value field in json format. */ - upb_json_parsermethod *parser_method; - upb_json_parser* parser; + upb_json_codecache *parser_codecache; upb_sink sink; + upb_json_parser *parser; /* Mark the range of uninterpreted values in json input before type url. */ const char *before_type_url_start; @@ -178,7 +177,7 @@ typedef struct { const upb_fielddef *f; /* The table mapping json name to fielddef for this message. */ - upb_strtable *name_table; + const upb_strtable *name_table; /* We are in a repeated-field context, ready to emit mapentries as * submessages. This flag alters the start-of-object (open-brace) behavior to @@ -257,60 +256,67 @@ struct upb_json_parser { struct tm tm; }; -struct upb_json_parsermethod { - upb_refcounted base; +struct upb_json_codecache { + upb_arena arena; + upb_inttable methods; /* upb_msgdef* -> upb_json_parsermethod* */ +}; +struct upb_json_parsermethod { + const upb_json_codecache *cache; upb_byteshandler input_handler_; - /* Keys are upb_msgdef*, values are upb_strtable (json_name -> fielddef) */ - upb_inttable name_tables; + /* Maps json_name -> fielddef */ + upb_strtable name_table; }; #define PARSER_CHECK_RETURN(x) if (!(x)) return false -static void json_parser_any_frame_reset(upb_jsonparser_any_frame *frame) { - frame->encoder_handlers = NULL; - frame->encoder = NULL; - frame->parser_method = NULL; +static upb_jsonparser_any_frame *json_parser_any_frame_new( + upb_json_parser *p) { + upb_jsonparser_any_frame *frame; + + frame = upb_env_malloc(p->env, sizeof(upb_jsonparser_any_frame)); + + frame->encoder_handlercache = upb_pb_encoder_newcache(); + frame->parser_codecache = upb_json_codecache_new(); frame->parser = NULL; frame->before_type_url_start = NULL; frame->before_type_url_end = NULL; frame->after_type_url_start = NULL; + + upb_stringsink_init(&frame->stringsink); + + return frame; } static void json_parser_any_frame_set_payload_type( upb_json_parser *p, upb_jsonparser_any_frame *frame, const upb_msgdef *payload_type) { + const upb_handlers *h; + const upb_json_parsermethod *parser_method; + upb_pb_encoder *encoder; + /* Initialize encoder. */ - frame->encoder_handlers = - upb_pb_encoder_newhandlers(payload_type, &frame->encoder_handlers); - upb_stringsink_init(&frame->stringsink); - frame->encoder = - upb_pb_encoder_create( - p->env, frame->encoder_handlers, - &frame->stringsink.sink); + h = upb_handlercache_get(frame->encoder_handlercache, payload_type); + encoder = upb_pb_encoder_create(p->env, h, &frame->stringsink.sink); /* Initialize parser. */ - frame->parser_method = - upb_json_parsermethod_new(payload_type, &frame->parser_method); - upb_sink_reset(&frame->sink, frame->encoder_handlers, frame->encoder); - frame->parser = - upb_json_parser_create(p->env, frame->parser_method, p->symtab, - &frame->sink, p->ignore_json_unknown); + parser_method = upb_json_codecache_get(frame->parser_codecache, payload_type); + upb_sink_reset(&frame->sink, h, encoder); + frame->parser = upb_json_parser_create(p->env, parser_method, p->symtab, + &frame->sink, p->ignore_json_unknown); } static void json_parser_any_frame_free(upb_jsonparser_any_frame *frame) { - upb_handlers_unref(frame->encoder_handlers, - &frame->encoder_handlers); - upb_json_parsermethod_unref(frame->parser_method, - &frame->parser_method); + upb_handlercache_free(frame->encoder_handlercache); + upb_json_codecache_free(frame->parser_codecache); upb_stringsink_uninit(&frame->stringsink); } static bool json_parser_any_frame_has_type_url( upb_jsonparser_any_frame *frame) { - return frame->encoder != NULL; + return frame->parser != NULL; } static bool json_parser_any_frame_has_value_before_type_url( @@ -332,7 +338,7 @@ static bool json_parser_any_frame_has_value( static void json_parser_any_frame_set_before_type_url_end( upb_jsonparser_any_frame *frame, const char *ptr) { - if (frame->encoder == NULL) { + if (frame->parser == NULL) { frame->before_type_url_end = ptr; } } @@ -374,9 +380,12 @@ static bool check_stack(upb_json_parser *p) { static void set_name_table(upb_json_parser *p, upb_jsonparser_frame *frame) { upb_value v; - bool ok = upb_inttable_lookupptr(&p->method->name_tables, frame->m, &v); + const upb_json_codecache *cache = p->method->cache; + bool ok = upb_inttable_lookupptr(&cache->methods, frame->m, &v); + const upb_json_parsermethod *method = upb_value_getptr(v); UPB_ASSERT(ok); - frame->name_table = upb_value_getptr(v); + + frame->name_table = &method->name_table; } /* There are GCC/Clang built-ins for overflow checking which we could start @@ -1927,9 +1936,7 @@ static bool start_subobject(upb_json_parser *p) { if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) { p->top->is_any = true; - p->top->any_frame = - upb_env_malloc(p->env, sizeof(upb_jsonparser_any_frame)); - json_parser_any_frame_reset(p->top->any_frame); + p->top->any_frame = json_parser_any_frame_new(p); } else { p->top->is_any = false; p->top->any_frame = NULL; @@ -2649,70 +2656,46 @@ static void json_parser_reset(upb_json_parser *p) { upb_status_clear(&p->status); } -static void free_json_parsermethod(upb_refcounted *r) { - upb_json_parsermethod *method = (upb_json_parsermethod*)r; +static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c, + const upb_msgdef *md) { + upb_msg_field_iter i; + upb_alloc *alloc = upb_arena_alloc(&c->arena); - upb_inttable_iter i; - upb_inttable_begin(&i, &method->name_tables); - for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { - upb_value val = upb_inttable_iter_value(&i); - upb_strtable *t = upb_value_getptr(val); - upb_strtable_uninit(t); - upb_gfree(t); - } + upb_json_parsermethod *m = upb_gmalloc(sizeof(*m)); - upb_inttable_uninit(&method->name_tables); + m->cache = c; - upb_gfree(r); -} + upb_byteshandler_init(&m->input_handler_); + upb_byteshandler_setstring(&m->input_handler_, parse, m); + upb_byteshandler_setendstr(&m->input_handler_, end, m); -static void add_jsonname_table(upb_json_parsermethod *m, const upb_msgdef* md) { - upb_msg_field_iter i; - upb_strtable *t; + upb_strtable_init2(&m->name_table, UPB_CTYPE_CONSTPTR, alloc); - /* It would be nice to stack-allocate this, but protobufs do not limit the - * length of fields to any reasonable limit. */ - char *buf = NULL; - size_t len = 0; - - if (upb_inttable_lookupptr(&m->name_tables, md, NULL)) { - return; - } - - /* TODO(haberman): handle malloc failure. */ - t = upb_gmalloc(sizeof(*t)); - upb_strtable_init(t, UPB_CTYPE_CONSTPTR); - upb_inttable_insertptr(&m->name_tables, md, upb_value_ptr(t)); + /* Build name_table */ for(upb_msg_field_begin(&i, md); !upb_msg_field_done(&i); upb_msg_field_next(&i)) { const upb_fielddef *f = upb_msg_iter_field(&i); + upb_value v = upb_value_constptr(f); + char *buf; /* Add an entry for the JSON name. */ - size_t field_len = upb_fielddef_getjsonname(f, buf, len); - if (field_len > len) { - size_t len2; - buf = upb_grealloc(buf, 0, field_len); - len = field_len; - len2 = upb_fielddef_getjsonname(f, buf, len); - UPB_ASSERT(len == len2); - } - upb_strtable_insert(t, buf, upb_value_constptr(f)); + size_t len = upb_fielddef_getjsonname(f, NULL, 0); + buf = upb_malloc(alloc, len); + upb_fielddef_getjsonname(f, buf, len); + upb_strtable_insert3(&m->name_table, buf, len, v, alloc); if (strcmp(buf, upb_fielddef_name(f)) != 0) { /* Since the JSON name is different from the regular field name, add an * entry for the raw name (compliant proto3 JSON parsers must accept * both). */ - upb_strtable_insert(t, upb_fielddef_name(f), upb_value_constptr(f)); - } - - if (upb_fielddef_issubmsg(f)) { - add_jsonname_table(m, upb_fielddef_msgsubdef(f)); + const char *name = upb_fielddef_name(f); + upb_strtable_insert3(&m->name_table, name, strlen(name), v, alloc); } } - upb_gfree(buf); + return m; } /* Public API *****************************************************************/ @@ -2740,9 +2723,7 @@ upb_json_parser *upb_json_parser_create(upb_env *env, p->top->m = upb_handlers_msgdef(output->handlers); if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) { p->top->is_any = true; - p->top->any_frame = - upb_env_malloc(p->env, sizeof(upb_jsonparser_any_frame)); - json_parser_any_frame_reset(p->top->any_frame); + p->top->any_frame = json_parser_any_frame_new(p); } else { p->top->is_any = false; p->top->any_frame = NULL; @@ -2763,24 +2744,61 @@ upb_bytessink *upb_json_parser_input(upb_json_parser *p) { return &p->input_; } -upb_json_parsermethod *upb_json_parsermethod_new(const upb_msgdef* md, - const void* owner) { - static const struct upb_refcounted_vtbl vtbl = {NULL, free_json_parsermethod}; - upb_json_parsermethod *ret = upb_gmalloc(sizeof(*ret)); - upb_refcounted_init(upb_json_parsermethod_upcast_mutable(ret), &vtbl, owner); +const upb_byteshandler *upb_json_parsermethod_inputhandler( + const upb_json_parsermethod *m) { + return &m->input_handler_; +} + +upb_json_codecache *upb_json_codecache_new() { + upb_alloc *alloc; + upb_json_codecache *c; - upb_byteshandler_init(&ret->input_handler_); - upb_byteshandler_setstring(&ret->input_handler_, parse, ret); - upb_byteshandler_setendstr(&ret->input_handler_, end, ret); + c = upb_gmalloc(sizeof(*c)); - upb_inttable_init(&ret->name_tables, UPB_CTYPE_PTR); + upb_arena_init(&c->arena); + alloc = upb_arena_alloc(&c->arena); - add_jsonname_table(ret, md); + upb_inttable_init2(&c->methods, UPB_CTYPE_CONSTPTR, alloc); - return ret; + return c; } -const upb_byteshandler *upb_json_parsermethod_inputhandler( - const upb_json_parsermethod *m) { - return &m->input_handler_; +void upb_json_codecache_free(upb_json_codecache *c) { + upb_arena_uninit(&c->arena); + upb_gfree(c); +} + +upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c, + const upb_msgdef *md) { + upb_json_parsermethod *m; + upb_value v; + upb_msg_field_iter i; + + if (upb_inttable_lookupptr(&c->methods, md, &v)) { + return upb_value_getptr(v); + } + + m = parsermethod_new(c, md); + v = upb_value_ptr(m); + + if (!m) return NULL; + if (!upb_inttable_insertptr(&c->methods, m, v)) return NULL; + + /* Populate parser methods for all submessages, so the name tables will + * be available during parsing. */ + for(upb_msg_field_begin(&i, md); + !upb_msg_field_done(&i); + upb_msg_field_next(&i)) { + upb_fielddef *f = upb_msg_iter_field(&i); + + if (upb_fielddef_issubmsg(f)) { + const upb_msgdef *subdef = upb_fielddef_msgsubdef(f); + const upb_json_parsermethod *sub_method = + upb_json_codecache_get(c, subdef); + + if (!sub_method) return NULL; + } + } + + return m; } diff --git a/upb/json/printer.c b/upb/json/printer.c index 95a4ad5..444916f 100644 --- a/upb/json/printer.c +++ b/upb/json/printer.c @@ -48,6 +48,10 @@ void freestrpc(void *ptr) { upb_gfree(pc); } +typedef struct { + bool preserve_fieldnames; +} upb_json_printercache; + /* Convert fielddef name to JSON name and return as a string piece. */ strpc *newstrpc(upb_handlers *h, const upb_fielddef *f, bool preserve_fieldnames) { @@ -1112,8 +1116,8 @@ void printer_sethandlers(const void *closure, upb_handlers *h) { bool is_mapentry = upb_msgdef_mapentry(md); upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; upb_msg_field_iter i; - const bool *preserve_fieldnames_ptr = closure; - const bool preserve_fieldnames = *preserve_fieldnames_ptr; + const upb_json_printercache *cache = closure; + const bool preserve_fieldnames = cache->preserve_fieldnames; if (is_mapentry) { /* mapentry messages are sufficiently different that we handle them @@ -1281,9 +1285,8 @@ upb_sink *upb_json_printer_input(upb_json_printer *p) { return &p->input_; } -const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md, - bool preserve_fieldnames, - const void *owner) { - return upb_handlers_newfrozen( - md, owner, printer_sethandlers, &preserve_fieldnames); +upb_handlercache *upb_json_printer_newcache(bool preserve_proto_fieldnames) { + upb_json_printercache *cache = upb_gmalloc(sizeof(*cache)); + cache->preserve_fieldnames = preserve_proto_fieldnames; + return upb_handlercache_new(printer_sethandlers, cache); } diff --git a/upb/json/printer.h b/upb/json/printer.h index 80644f1..fe9c8f1 100644 --- a/upb/json/printer.h +++ b/upb/json/printer.h @@ -35,14 +35,8 @@ class upb::json::Printer { /* The input to the printer. */ Sink* input(); - /* Returns handlers for printing according to the specified schema. - * If preserve_proto_fieldnames is true, the output JSON will use the - * original .proto field names (ie. {"my_field":3}) instead of using - * camelCased names, which is the default: (eg. {"myField":3}). */ - static reffed_ptr NewHandlers(const upb::MessageDef* md, - bool preserve_proto_fieldnames); - static const size_t kSize = UPB_JSON_PRINTER_SIZE; + static upb_handlercache* NewCache(bool preserve_proto_fieldnames); private: UPB_DISALLOW_POD_OPS(Printer, upb::json::Printer) @@ -60,6 +54,8 @@ const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md, bool preserve_fieldnames, const void *owner); +upb_handlercache *upb_json_printer_newcache(bool preserve_proto_fieldnames); + UPB_END_EXTERN_C #ifdef __cplusplus @@ -71,11 +67,8 @@ inline Printer* Printer::Create(Environment* env, const upb::Handlers* handlers, return upb_json_printer_create(env, handlers, output); } inline Sink* Printer::input() { return upb_json_printer_input(this); } -inline reffed_ptr Printer::NewHandlers( - const upb::MessageDef *md, bool preserve_proto_fieldnames) { - const Handlers* h = upb_json_printer_newhandlers( - md, preserve_proto_fieldnames, &h); - return reffed_ptr(h, &h); +inline upb_handlercache* Printer::NewCache(bool preserve_proto_fieldnames) { + return upb_json_printer_newcache(preserve_proto_fieldnames); } } /* namespace json */ } /* namespace upb */ diff --git a/upb/msgfactory.c b/upb/msgfactory.c index 63df49e..23dd79a 100644 --- a/upb/msgfactory.c +++ b/upb/msgfactory.c @@ -197,7 +197,6 @@ static bool upb_msglayout_init(const upb_msgdef *m, struct upb_msgfactory { const upb_symtab *symtab; /* We own a ref. */ upb_inttable layouts; - upb_inttable mergehandlers; }; upb_msgfactory *upb_msgfactory_new(const upb_symtab *symtab) { @@ -205,7 +204,6 @@ upb_msgfactory *upb_msgfactory_new(const upb_symtab *symtab) { ret->symtab = symtab; upb_inttable_init(&ret->layouts, UPB_CTYPE_PTR); - upb_inttable_init(&ret->mergehandlers, UPB_CTYPE_CONSTPTR); return ret; } @@ -218,14 +216,7 @@ void upb_msgfactory_free(upb_msgfactory *f) { upb_msglayout_free(l); } - upb_inttable_begin(&i, &f->mergehandlers); - for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { - const upb_handlers *h = upb_value_getconstptr(upb_inttable_iter_value(&i)); - upb_handlers_unref(h, f); - } - upb_inttable_uninit(&f->layouts); - upb_inttable_uninit(&f->mergehandlers); upb_gfree(f); } diff --git a/upb/pb/compile_decoder.c b/upb/pb/compile_decoder.c index d147edf..02f5179 100644 --- a/upb/pb/compile_decoder.c +++ b/upb/pb/compile_decoder.c @@ -23,80 +23,23 @@ #define MAXLABEL 5 #define EMPTYLABEL -1 -/* mgroup *********************************************************************/ - -static void freegroup(upb_refcounted *r) { - mgroup *g = (mgroup*)r; - upb_inttable_uninit(&g->methods); -#ifdef UPB_USE_JIT_X64 - upb_pbdecoder_freejit(g); -#endif - upb_gfree(g->bytecode); - upb_gfree(g); -} - -static void visitgroup(const upb_refcounted *r, upb_refcounted_visit *visit, - void *closure) { - const mgroup *g = (const mgroup*)r; - upb_inttable_iter i; - upb_inttable_begin(&i, &g->methods); - for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { - upb_pbdecodermethod *method = upb_value_getptr(upb_inttable_iter_value(&i)); - visit(r, upb_pbdecodermethod_upcast(method), closure); - } -} - -mgroup *newgroup(const void *owner) { - mgroup *g = upb_gmalloc(sizeof(*g)); - static const struct upb_refcounted_vtbl vtbl = {visitgroup, freegroup}; - upb_refcounted_init(mgroup_upcast_mutable(g), &vtbl, owner); - upb_inttable_init(&g->methods, UPB_CTYPE_PTR); - g->bytecode = NULL; - g->bytecode_end = NULL; - return g; -} - - /* upb_pbdecodermethod ********************************************************/ -static void freemethod(upb_refcounted *r) { - upb_pbdecodermethod *method = (upb_pbdecodermethod*)r; - - if (method->dest_handlers_) { - upb_handlers_unref(method->dest_handlers_, method); - } - +static void freemethod(upb_pbdecodermethod *method) { upb_inttable_uninit(&method->dispatch); upb_gfree(method); } -static void visitmethod(const upb_refcounted *r, upb_refcounted_visit *visit, - void *closure) { - const upb_pbdecodermethod *m = (const upb_pbdecodermethod*)r; - visit(r, m->group, closure); -} - static upb_pbdecodermethod *newmethod(const upb_handlers *dest_handlers, mgroup *group) { - static const struct upb_refcounted_vtbl vtbl = {visitmethod, freemethod}; upb_pbdecodermethod *ret = upb_gmalloc(sizeof(*ret)); - upb_refcounted_init(upb_pbdecodermethod_upcast_mutable(ret), &vtbl, &ret); upb_byteshandler_init(&ret->input_handler_); - /* The method references the group and vice-versa, in a circular reference. */ - upb_ref2(ret, group); - upb_ref2(group, ret); - upb_inttable_insertptr(&group->methods, dest_handlers, upb_value_ptr(ret)); - upb_pbdecodermethod_unref(ret, &ret); - - ret->group = mgroup_upcast_mutable(group); + ret->group = group; ret->dest_handlers_ = dest_handlers; ret->is_native_ = false; /* If we JIT, it will update this later. */ upb_inttable_init(&ret->dispatch, UPB_CTYPE_UINT64); - if (ret->dest_handlers_) { - upb_handlers_ref(ret->dest_handlers_, ret); - } return ret; } @@ -114,16 +57,31 @@ bool upb_pbdecodermethod_isnative(const upb_pbdecodermethod *m) { return m->is_native_; } -const upb_pbdecodermethod *upb_pbdecodermethod_new( - const upb_pbdecodermethodopts *opts, const void *owner) { - const upb_pbdecodermethod *ret; - upb_pbcodecache cache; - upb_pbcodecache_init(&cache); - ret = upb_pbcodecache_getdecodermethod(&cache, opts); - upb_pbdecodermethod_ref(ret, owner); - upb_pbcodecache_uninit(&cache); - return ret; +/* mgroup *********************************************************************/ + +static void freegroup(mgroup *g) { + upb_inttable_iter i; + + upb_inttable_begin(&i, &g->methods); + for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { + freemethod(upb_value_getptr(upb_inttable_iter_value(&i))); + } + + upb_inttable_uninit(&g->methods); +#ifdef UPB_USE_JIT_X64 + upb_pbdecoder_freejit(g); +#endif + upb_gfree(g->bytecode); + upb_gfree(g); +} + +mgroup *newgroup() { + mgroup *g = upb_gmalloc(sizeof(*g)); + upb_inttable_init(&g->methods, UPB_CTYPE_PTR); + g->bytecode = NULL; + g->bytecode_end = NULL; + return g; } @@ -814,10 +772,13 @@ static void find_methods(compiler *c, const upb_handlers *h) { upb_value v; upb_msg_field_iter i; const upb_msgdef *md; + upb_pbdecodermethod *method; if (upb_inttable_lookupptr(&c->group->methods, h, &v)) return; - newmethod(h, c->group); + + method = newmethod(h, c->group); + upb_inttable_insertptr(&c->group->methods, h, upb_value_ptr(method)); /* Find submethods. */ md = upb_handlers_msgdef(h); @@ -893,15 +854,13 @@ static void sethandlers(mgroup *g, bool allowjit) { /* TODO(haberman): allow this to be constructed for an arbitrary set of dest * handlers and other mgroups (but verify we have a transitive closure). */ -const mgroup *mgroup_new(const upb_handlers *dest, bool allowjit, bool lazy, - const void *owner) { +const mgroup *mgroup_new(const upb_handlers *dest, bool allowjit, bool lazy) { mgroup *g; compiler *c; UPB_UNUSED(allowjit); - UPB_ASSERT(upb_handlers_isfrozen(dest)); - g = newgroup(owner); + g = newgroup(); c = newcompiler(g, lazy); find_methods(c, dest); @@ -939,56 +898,63 @@ const mgroup *mgroup_new(const upb_handlers *dest, bool allowjit, bool lazy, /* upb_pbcodecache ************************************************************/ -void upb_pbcodecache_init(upb_pbcodecache *c) { - upb_inttable_init(&c->groups, UPB_CTYPE_CONSTPTR); - c->allow_jit_ = true; +upb_pbcodecache *upb_pbcodecache_new(upb_handlercache *dest) { + upb_pbcodecache *c = upb_gmalloc(sizeof(*c)); + + if (!c) return NULL; + + c->dest = dest; + c->allow_jit = true; + c->lazy = false; + + upb_arena_init(&c->arena); + if (!upb_inttable_init(&c->groups, UPB_CTYPE_CONSTPTR)) return NULL; + + return c; } -void upb_pbcodecache_uninit(upb_pbcodecache *c) { - upb_inttable_iter i; - upb_inttable_begin(&i, &c->groups); - for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { - const mgroup *group = upb_value_getconstptr(upb_inttable_iter_value(&i)); - mgroup_unref(group, c); +void upb_pbcodecache_free(upb_pbcodecache *c) { + size_t i; + + for (i = 0; i < upb_inttable_count(&c->groups); i++) { + upb_value v; + bool ok = upb_inttable_lookup(&c->groups, i, &v); + UPB_ASSERT(ok); + freegroup((void*)upb_value_getconstptr(v)); } + upb_inttable_uninit(&c->groups); + upb_gfree(c); } bool upb_pbcodecache_allowjit(const upb_pbcodecache *c) { - return c->allow_jit_; + return c->allow_jit; } -bool upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow) { - if (upb_inttable_count(&c->groups) > 0) - return false; - c->allow_jit_ = allow; - return true; +void upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow) { + UPB_ASSERT(upb_inttable_count(&c->groups) == 0); + c->allow_jit = allow; } -const upb_pbdecodermethod *upb_pbcodecache_getdecodermethod( - upb_pbcodecache *c, const upb_pbdecodermethodopts *opts) { +void upb_pbdecodermethodopts_setlazy(upb_pbcodecache *c, bool lazy) { + UPB_ASSERT(upb_inttable_count(&c->groups) == 0); + c->lazy = lazy; +} + +const upb_pbdecodermethod *upb_pbcodecache_get(upb_pbcodecache *c, + const upb_msgdef *md) { upb_value v; bool ok; + const upb_handlers *h; + const mgroup *g; /* Right now we build a new DecoderMethod every time. * TODO(haberman): properly cache methods by their true key. */ - const mgroup *g = mgroup_new(opts->handlers, c->allow_jit_, opts->lazy, c); + h = upb_handlercache_get(c->dest, md); + g = mgroup_new(h, c->allow_jit, c->lazy); upb_inttable_push(&c->groups, upb_value_constptr(g)); - ok = upb_inttable_lookupptr(&g->methods, opts->handlers, &v); + ok = upb_inttable_lookupptr(&g->methods, h, &v); UPB_ASSERT(ok); return upb_value_getptr(v); } - - -/* upb_pbdecodermethodopts ****************************************************/ - -void upb_pbdecodermethodopts_init(upb_pbdecodermethodopts *opts, - const upb_handlers *h) { - opts->handlers = h; - opts->lazy = false; -} - -void upb_pbdecodermethodopts_setlazy(upb_pbdecodermethodopts *opts, bool lazy) { - opts->lazy = lazy; -} diff --git a/upb/pb/decoder.h b/upb/pb/decoder.h index 7c1877a..1a00801 100644 --- a/upb/pb/decoder.h +++ b/upb/pb/decoder.h @@ -30,10 +30,7 @@ class DecoderMethodOptions; UPB_DECLARE_TYPE(upb::pb::CodeCache, upb_pbcodecache) UPB_DECLARE_TYPE(upb::pb::Decoder, upb_pbdecoder) -UPB_DECLARE_TYPE(upb::pb::DecoderMethodOptions, upb_pbdecodermethodopts) - -UPB_DECLARE_DERIVED_TYPE(upb::pb::DecoderMethod, upb::RefCounted, - upb_pbdecodermethod, upb_refcounted) +UPB_DECLARE_TYPE(upb::pb::DecoderMethod, upb_pbdecodermethod) /* The maximum number of bytes we are required to buffer internally between * calls to the decoder. The value is 14: a 5 byte unknown tag plus ten-byte @@ -44,35 +41,10 @@ UPB_DECLARE_DERIVED_TYPE(upb::pb::DecoderMethod, upb::RefCounted, #ifdef __cplusplus -/* The parameters one uses to construct a DecoderMethod. - * TODO(haberman): move allowjit here? Seems more convenient for users. - * TODO(haberman): move this to be heap allocated for ABI stability. */ -class upb::pb::DecoderMethodOptions { - public: - /* Parameter represents the destination handlers that this method will push - * to. */ - explicit DecoderMethodOptions(const Handlers* dest_handlers); - - /* Should the decoder push submessages to lazy handlers for fields that have - * them? The caller should set this iff the lazy handlers expect data that is - * in protobuf binary format and the caller wishes to lazy parse it. */ - void set_lazy(bool lazy); -#else -struct upb_pbdecodermethodopts { -#endif - const upb_handlers *handlers; - bool lazy; -}; - -#ifdef __cplusplus - /* Represents the code to parse a protobuf according to a destination * Handlers. */ class upb::pb::DecoderMethod { public: - /* Include base methods from upb::ReferenceCounted. */ - UPB_REFCOUNTED_CPPMETHODS - /* The destination handlers that are statically bound to this method. * This method is only capable of outputting to a sink that uses these * handlers. */ @@ -84,10 +56,6 @@ class upb::pb::DecoderMethod { /* Whether this method is native. */ bool is_native() const; - /* Convenience method for generating a DecoderMethod without explicitly - * creating a CodeCache. */ - static reffed_ptr New(const DecoderMethodOptions& opts); - private: UPB_DISALLOW_POD_OPS(DecoderMethod, upb::pb::DecoderMethod) }; @@ -147,20 +115,14 @@ class upb::pb::Decoder { UPB_DISALLOW_POD_OPS(Decoder, upb::pb::Decoder) }; -#endif /* __cplusplus */ - -#ifdef __cplusplus - /* A class for caching protobuf processing code, whether bytecode for the * interpreted decoder or machine code for the JIT. * - * This class is not thread-safe. - * - * TODO(haberman): move this to be heap allocated for ABI stability. */ + * This class is not thread-safe. */ class upb::pb::CodeCache { public: - CodeCache(); - ~CodeCache(); + static CodeCache* New(HandlerCache* dest); + static void Free(CodeCache* cache); /* Whether the cache is allowed to generate machine code. Defaults to true. * There is no real reason to turn it off except for testing or if you are @@ -172,33 +134,24 @@ class upb::pb::CodeCache { bool allow_jit() const; /* This may only be called when the object is first constructed, and prior to - * any code generation, otherwise returns false and does nothing. */ - bool set_allow_jit(bool allow); + * any code generation. */ + void set_allow_jit(bool allow); - /* Returns a DecoderMethod that can push data to the given handlers. - * If a suitable method already exists, it will be returned from the cache. - * - * Specifying the destination handlers here allows the DecoderMethod to be - * statically bound to the destination handlers if possible, which can allow - * more efficient decoding. However the returned method may or may not - * actually be statically bound. But in all cases, the returned method can - * push data to the given handlers. */ - const DecoderMethod *GetDecoderMethod(const DecoderMethodOptions& opts); + /* Should the decoder push submessages to lazy handlers for fields that have + * them? The caller should set this iff the lazy handlers expect data that is + * in protobuf binary format and the caller wishes to lazy parse it. */ + void set_lazy(bool lazy); - /* If/when someone needs to explicitly create a dynamically-bound - * DecoderMethod*, we can add a method to get it here. */ + /* Returns a DecoderMethod that can push data to the given handlers. + * If a suitable method already exists, it will be returned from the cache. */ + const DecoderMethod *Get(const MessageDef* md); private: - UPB_DISALLOW_COPY_AND_ASSIGN(CodeCache) -#else -struct upb_pbcodecache { -#endif - bool allow_jit_; - - /* Array of mgroups. */ - upb_inttable groups; + UPB_DISALLOW_POD_OPS(CodeCache, upb::pb::CodeCache) }; +#endif + UPB_BEGIN_EXTERN_C upb_pbdecoder *upb_pbdecoder_create(upb_env *e, @@ -211,28 +164,21 @@ size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d); bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max); void upb_pbdecoder_reset(upb_pbdecoder *d); -void upb_pbdecodermethodopts_init(upb_pbdecodermethodopts *opts, - const upb_handlers *h); -void upb_pbdecodermethodopts_setlazy(upb_pbdecodermethodopts *opts, bool lazy); -/* Include refcounted methods like upb_pbdecodermethod_ref(). */ -UPB_REFCOUNTED_CMETHODS(upb_pbdecodermethod, upb_pbdecodermethod_upcast) - const upb_handlers *upb_pbdecodermethod_desthandlers( const upb_pbdecodermethod *m); const upb_byteshandler *upb_pbdecodermethod_inputhandler( const upb_pbdecodermethod *m); bool upb_pbdecodermethod_isnative(const upb_pbdecodermethod *m); -const upb_pbdecodermethod *upb_pbdecodermethod_new( - const upb_pbdecodermethodopts *opts, const void *owner); -void upb_pbcodecache_init(upb_pbcodecache *c); -void upb_pbcodecache_uninit(upb_pbcodecache *c); +upb_pbcodecache *upb_pbcodecache_new(upb_handlercache *dest); +void upb_pbcodecache_free(upb_pbcodecache *c); bool upb_pbcodecache_allowjit(const upb_pbcodecache *c); -bool upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow); -const upb_pbdecodermethod *upb_pbcodecache_getdecodermethod( - upb_pbcodecache *c, const upb_pbdecodermethodopts *opts); +void upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow); +void upb_pbcodecache_setlazy(upb_pbcodecache *c, bool lazy); +const upb_pbdecodermethod *upb_pbcodecache_get(upb_pbcodecache *c, + const upb_msgdef *md); UPB_END_EXTERN_C @@ -264,13 +210,6 @@ inline bool Decoder::set_max_nesting(size_t max) { } inline void Decoder::Reset() { upb_pbdecoder_reset(this); } -inline DecoderMethodOptions::DecoderMethodOptions(const Handlers* h) { - upb_pbdecodermethodopts_init(this, h); -} -inline void DecoderMethodOptions::set_lazy(bool lazy) { - upb_pbdecodermethodopts_setlazy(this, lazy); -} - inline const Handlers* DecoderMethod::dest_handlers() const { return upb_pbdecodermethod_desthandlers(this); } @@ -280,28 +219,21 @@ inline const BytesHandler* DecoderMethod::input_handler() const { inline bool DecoderMethod::is_native() const { return upb_pbdecodermethod_isnative(this); } -/* static */ -inline reffed_ptr DecoderMethod::New( - const DecoderMethodOptions &opts) { - const upb_pbdecodermethod *m = upb_pbdecodermethod_new(&opts, &m); - return reffed_ptr(m, &m); -} -inline CodeCache::CodeCache() { - upb_pbcodecache_init(this); +inline CodeCache* CodeCache::New(HandlerCache* dest) { + return upb_pbcodecache_new(dest); } -inline CodeCache::~CodeCache() { - upb_pbcodecache_uninit(this); +inline void CodeCache::Free(CodeCache* cache) { + upb_pbcodecache_free(cache); } inline bool CodeCache::allow_jit() const { return upb_pbcodecache_allowjit(this); } -inline bool CodeCache::set_allow_jit(bool allow) { - return upb_pbcodecache_setallowjit(this, allow); +inline void CodeCache::set_allow_jit(bool allow) { + upb_pbcodecache_setallowjit(this, allow); } -inline const DecoderMethod *CodeCache::GetDecoderMethod( - const DecoderMethodOptions& opts) { - return upb_pbcodecache_getdecodermethod(this, &opts); +inline const DecoderMethod *CodeCache::Get(const MessageDef *md) { + return upb_pbcodecache_get(this, md); } } /* namespace pb */ diff --git a/upb/pb/decoder.int.h b/upb/pb/decoder.int.h index f02bdd5..8d464fa 100644 --- a/upb/pb/decoder.int.h +++ b/upb/pb/decoder.int.h @@ -11,17 +11,6 @@ #include "upb/sink.h" #include "upb/table.int.h" -/* C++ names are not actually used since this type isn't exposed to users. */ -#ifdef __cplusplus -namespace upb { -namespace pb { -class MessageGroup; -} /* namespace pb */ -} /* namespace upb */ -#endif -UPB_DECLARE_DERIVED_TYPE(upb::pb::MessageGroup, upb::RefCounted, - mgroup, upb_refcounted) - /* Opcode definitions. The canonical meaning of each opcode is its * implementation in the interpreter (the JIT is written to match this). * @@ -83,30 +72,25 @@ typedef enum { UPB_INLINE opcode getop(uint32_t instr) { return instr & 0xff; } +struct upb_pbcodecache { + upb_arena arena; + upb_handlercache *dest; + bool allow_jit; + bool lazy; + + /* Array of mgroups. */ + upb_inttable groups; +}; + /* Method group; represents a set of decoder methods that had their code - * emitted together, and must therefore be freed together. Immutable once - * created. It is possible we may want to expose this to users at some point. - * - * Overall ownership of Decoder objects looks like this: - * - * +----------+ - * | | <---> DecoderMethod - * | method | - * CodeCache ---> | group | <---> DecoderMethod - * | | - * | (mgroup) | <---> DecoderMethod - * +----------+ - */ -struct mgroup { - upb_refcounted base; - - /* Maps upb_msgdef/upb_handlers -> upb_pbdecodermethod. We own refs on the - * methods. */ + * emitted together. Immutable once created. */ +typedef struct { + /* Maps upb_msgdef/upb_handlers -> upb_pbdecodermethod. Owned by us. + * + * Ideally this would be on pbcodecache (if we were actually caching code). + * Right now we don't actually cache anything, which is wasteful. */ upb_inttable methods; - /* When we add the ability to link to previously existing mgroups, we'll - * need an array of mgroups we reference here, and own refs on them. */ - /* The bytecode for our methods, if any exists. Owned by us. */ uint32_t *bytecode; uint32_t *bytecode_end; @@ -119,7 +103,7 @@ struct mgroup { char *debug_info; void *dl; #endif -}; +} mgroup; /* The maximum that any submessages can be nested. Matches proto2's limit. * This specifies the size of the decoder's statically-sized array and therefore @@ -159,8 +143,6 @@ typedef struct { } upb_pbdecoder_frame; struct upb_pbdecodermethod { - upb_refcounted base; - /* While compiling, the base is relative in "ofs", after compiling it is * absolute in "ptr". */ union { @@ -168,14 +150,8 @@ struct upb_pbdecodermethod { void *ptr; /* Pointer to bytecode or machine code for this method. */ } code_base; - /* The decoder method group to which this method belongs. We own a ref. - * Owning a ref on the entire group is more coarse-grained than is strictly - * necessary; all we truly require is that methods we directly reference - * outlive us, while the group could contain many other messages we don't - * require. But the group represents the messages that were - * allocated+compiled together, so it makes the most sense to free them - * together also. */ - const upb_refcounted *group; + /* The decoder method group to which this method belongs. */ + const mgroup *group; /* Whether this method is native code or bytecode. */ bool is_native_; @@ -276,7 +252,6 @@ const char *upb_pbdecoder_getopname(unsigned int op); /* JIT codegen entry point. */ void upb_pbdecoder_jit(mgroup *group); void upb_pbdecoder_freejit(mgroup *group); -UPB_REFCOUNTED_CMETHODS(mgroup, mgroup_upcast) /* A special label that means "do field dispatch for this message and branch to * wherever that takes you." */ diff --git a/upb/pb/encoder.c b/upb/pb/encoder.c index 839ede0..ca3ca5c 100644 --- a/upb/pb/encoder.c +++ b/upb/pb/encoder.c @@ -526,9 +526,8 @@ void upb_pb_encoder_reset(upb_pb_encoder *e) { /* public API *****************************************************************/ -const upb_handlers *upb_pb_encoder_newhandlers(const upb_msgdef *m, - const void *owner) { - return upb_handlers_newfrozen(m, owner, newhandlers_callback, NULL); +upb_handlercache *upb_pb_encoder_newcache() { + return upb_handlercache_new(newhandlers_callback, NULL); } upb_pb_encoder *upb_pb_encoder_create(upb_env *env, const upb_handlers *h, diff --git a/upb/pb/encoder.h b/upb/pb/encoder.h index 41b7e7b..eefa385 100644 --- a/upb/pb/encoder.h +++ b/upb/pb/encoder.h @@ -47,7 +47,7 @@ class upb::pb::Encoder { Sink* input(); /* Creates a new set of handlers for this MessageDef. */ - static reffed_ptr NewHandlers(const MessageDef* msg); + static upb_handlercache* NewCache(); static const size_t kSize = UPB_PB_ENCODER_SIZE; @@ -59,12 +59,12 @@ class upb::pb::Encoder { UPB_BEGIN_EXTERN_C -const upb_handlers *upb_pb_encoder_newhandlers(const upb_msgdef *m, - const void *owner); upb_sink *upb_pb_encoder_input(upb_pb_encoder *p); upb_pb_encoder* upb_pb_encoder_create(upb_env* e, const upb_handlers* h, upb_bytessink* output); +upb_handlercache *upb_pb_encoder_newcache(); + UPB_END_EXTERN_C #ifdef __cplusplus @@ -78,10 +78,8 @@ inline Encoder* Encoder::Create(Environment* env, const Handlers* handlers, inline Sink* Encoder::input() { return upb_pb_encoder_input(this); } -inline reffed_ptr Encoder::NewHandlers( - const upb::MessageDef *md) { - const Handlers* h = upb_pb_encoder_newhandlers(md, &h); - return reffed_ptr(h, &h); +inline upb_handlercache* Encoder::NewCache() { + return upb_pb_encoder_newcache(); } } /* namespace pb */ } /* namespace upb */ diff --git a/upb/pb/textprinter.c b/upb/pb/textprinter.c index e8033f8..b6f8024 100644 --- a/upb/pb/textprinter.c +++ b/upb/pb/textprinter.c @@ -327,9 +327,8 @@ upb_textprinter *upb_textprinter_create(upb_env *env, const upb_handlers *h, return p; } -const upb_handlers *upb_textprinter_newhandlers(const upb_msgdef *m, - const void *owner) { - return upb_handlers_newfrozen(m, owner, &onmreg, NULL); +upb_handlercache *upb_textprinter_newcache() { + return upb_handlercache_new(&onmreg, NULL); } upb_sink *upb_textprinter_input(upb_textprinter *p) { return &p->input_; } diff --git a/upb/pb/textprinter.h b/upb/pb/textprinter.h index 2f40ed8..06ff7d5 100644 --- a/upb/pb/textprinter.h +++ b/upb/pb/textprinter.h @@ -34,7 +34,7 @@ class upb::pb::TextPrinter { /* If handler caching becomes a requirement we can add a code cache as in * decoder.h */ - static reffed_ptr NewHandlers(const MessageDef* md); + static HandlerCache* NewCache(); }; #endif @@ -47,8 +47,7 @@ upb_textprinter *upb_textprinter_create(upb_env *env, const upb_handlers *h, void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line); upb_sink *upb_textprinter_input(upb_textprinter *p); -const upb_handlers *upb_textprinter_newhandlers(const upb_msgdef *m, - const void *owner); +upb_handlercache *upb_textprinter_newcache(); UPB_END_EXTERN_C @@ -67,10 +66,8 @@ inline void TextPrinter::SetSingleLineMode(bool single_line) { inline Sink* TextPrinter::input() { return upb_textprinter_input(this); } -inline reffed_ptr TextPrinter::NewHandlers( - const MessageDef *md) { - const Handlers* h = upb_textprinter_newhandlers(md, &h); - return reffed_ptr(h, &h); +inline HandlerCache* TextPrinter::NewCache() { + return upb_textprinter_newcache(); } } /* namespace pb */ } /* namespace upb */ diff --git a/upb/refcounted.c b/upb/refcounted.c deleted file mode 100644 index f00dbb7..0000000 --- a/upb/refcounted.c +++ /dev/null @@ -1,851 +0,0 @@ -/* -** upb::RefCounted Implementation -** -** Our key invariants are: -** 1. reference cycles never span groups -** 2. for ref2(to, from), we increment to's count iff group(from) != group(to) -** -** The previous two are how we avoid leaking cycles. Other important -** invariants are: -** 3. for mutable objects "from" and "to", if there exists a ref2(to, from) -** this implies group(from) == group(to). (In practice, what we implement -** is even stronger; "from" and "to" will share a group if there has *ever* -** been a ref2(to, from), but all that is necessary for correctness is the -** weaker one). -** 4. mutable and immutable objects are never in the same group. -*/ - -#include "upb/refcounted.h" - -#include - -static void freeobj(upb_refcounted *o); - -const char untracked_val; -const void *UPB_UNTRACKED_REF = &untracked_val; - -/* arch-specific atomic primitives *******************************************/ - -#ifdef UPB_THREAD_UNSAFE /*---------------------------------------------------*/ - -static void atomic_inc(uint32_t *a) { (*a)++; } -static bool atomic_dec(uint32_t *a) { return --(*a) == 0; } - -#elif defined(__GNUC__) || defined(__clang__) /*------------------------------*/ - -static void atomic_inc(uint32_t *a) { __sync_fetch_and_add(a, 1); } -static bool atomic_dec(uint32_t *a) { return __sync_sub_and_fetch(a, 1) == 0; } - -#elif defined(WIN32) /*-------------------------------------------------------*/ - -#include - -static void atomic_inc(upb_atomic_t *a) { InterlockedIncrement(&a->val); } -static bool atomic_dec(upb_atomic_t *a) { - return InterlockedDecrement(&a->val) == 0; -} - -#else -#error Atomic primitives not defined for your platform/CPU. \ - Implement them or compile with UPB_THREAD_UNSAFE. -#endif - -/* All static objects point to this refcount. - * It is special-cased in ref/unref below. */ -uint32_t static_refcount = -1; - -/* We can avoid atomic ops for statically-declared objects. - * This is a minor optimization but nice since we can avoid degrading under - * contention in this case. */ - -static void refgroup(uint32_t *group) { - if (group != &static_refcount) - atomic_inc(group); -} - -static bool unrefgroup(uint32_t *group) { - if (group == &static_refcount) { - return false; - } else { - return atomic_dec(group); - } -} - - -/* Reference tracking (debug only) ********************************************/ - -#ifdef UPB_DEBUG_REFS - -#ifdef UPB_THREAD_UNSAFE - -static void upb_lock() {} -static void upb_unlock() {} - -#else - -/* User must define functions that lock/unlock a global mutex and link this - * file against them. */ -void upb_lock(); -void upb_unlock(); - -#endif - -/* UPB_DEBUG_REFS mode counts on being able to malloc() memory in some - * code-paths that can normally never fail, like upb_refcounted_ref(). Since - * we have no way to propagage out-of-memory errors back to the user, and since - * these errors can only occur in UPB_DEBUG_REFS mode, we use an allocator that - * immediately aborts on failure (avoiding the global allocator, which might - * inject failures). */ - -#include - -static void *upb_debugrefs_allocfunc(upb_alloc *alloc, void *ptr, - size_t oldsize, size_t size) { - UPB_UNUSED(alloc); - UPB_UNUSED(oldsize); - if (size == 0) { - free(ptr); - return NULL; - } else { - void *ret = realloc(ptr, size); - - if (!ret) { - abort(); - } - - return ret; - } -} - -upb_alloc upb_alloc_debugrefs = {&upb_debugrefs_allocfunc}; - -typedef struct { - int count; /* How many refs there are (duplicates only allowed for ref2). */ - bool is_ref2; -} trackedref; - -static trackedref *trackedref_new(bool is_ref2) { - trackedref *ret = upb_malloc(&upb_alloc_debugrefs, sizeof(*ret)); - ret->count = 1; - ret->is_ref2 = is_ref2; - return ret; -} - -static void track(const upb_refcounted *r, const void *owner, bool ref2) { - upb_value v; - - UPB_ASSERT(owner); - if (owner == UPB_UNTRACKED_REF) return; - - upb_lock(); - if (upb_inttable_lookupptr(r->refs, owner, &v)) { - trackedref *ref = upb_value_getptr(v); - /* Since we allow multiple ref2's for the same to/from pair without - * allocating separate memory for each one, we lose the fine-grained - * tracking behavior we get with regular refs. Since ref2s only happen - * inside upb, we'll accept this limitation until/unless there is a really - * difficult upb-internal bug that can't be figured out without it. */ - UPB_ASSERT(ref2); - UPB_ASSERT(ref->is_ref2); - ref->count++; - } else { - trackedref *ref = trackedref_new(ref2); - upb_inttable_insertptr2(r->refs, owner, upb_value_ptr(ref), - &upb_alloc_debugrefs); - if (ref2) { - /* We know this cast is safe when it is a ref2, because it's coming from - * another refcounted object. */ - const upb_refcounted *from = owner; - UPB_ASSERT(!upb_inttable_lookupptr(from->ref2s, r, NULL)); - upb_inttable_insertptr2(from->ref2s, r, upb_value_ptr(NULL), - &upb_alloc_debugrefs); - } - } - upb_unlock(); -} - -static void untrack(const upb_refcounted *r, const void *owner, bool ref2) { - upb_value v; - bool found; - trackedref *ref; - - UPB_ASSERT(owner); - if (owner == UPB_UNTRACKED_REF) return; - - upb_lock(); - found = upb_inttable_lookupptr(r->refs, owner, &v); - /* This assert will fail if an owner attempts to release a ref it didn't have. */ - UPB_ASSERT(found); - ref = upb_value_getptr(v); - UPB_ASSERT(ref->is_ref2 == ref2); - if (--ref->count == 0) { - free(ref); - upb_inttable_removeptr(r->refs, owner, NULL); - if (ref2) { - /* We know this cast is safe when it is a ref2, because it's coming from - * another refcounted object. */ - const upb_refcounted *from = owner; - bool removed = upb_inttable_removeptr(from->ref2s, r, NULL); - UPB_ASSERT(removed); - } - } - upb_unlock(); -} - -static void checkref(const upb_refcounted *r, const void *owner, bool ref2) { - upb_value v; - bool found; - trackedref *ref; - - upb_lock(); - found = upb_inttable_lookupptr(r->refs, owner, &v); - UPB_ASSERT(found); - ref = upb_value_getptr(v); - UPB_ASSERT(ref->is_ref2 == ref2); - upb_unlock(); -} - -/* Populates the given UPB_CTYPE_INT32 inttable with counts of ref2's that - * originate from the given owner. */ -static void getref2s(const upb_refcounted *owner, upb_inttable *tab) { - upb_inttable_iter i; - - upb_lock(); - upb_inttable_begin(&i, owner->ref2s); - for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { - upb_value v; - upb_value count; - trackedref *ref; - bool found; - - upb_refcounted *to = (upb_refcounted*)upb_inttable_iter_key(&i); - - /* To get the count we need to look in the target's table. */ - found = upb_inttable_lookupptr(to->refs, owner, &v); - UPB_ASSERT(found); - ref = upb_value_getptr(v); - count = upb_value_int32(ref->count); - - upb_inttable_insertptr2(tab, to, count, &upb_alloc_debugrefs); - } - upb_unlock(); -} - -typedef struct { - upb_inttable ref2; - const upb_refcounted *obj; -} check_state; - -static void visit_check(const upb_refcounted *obj, const upb_refcounted *subobj, - void *closure) { - check_state *s = closure; - upb_inttable *ref2 = &s->ref2; - upb_value v; - bool removed; - int32_t newcount; - - UPB_ASSERT(obj == s->obj); - UPB_ASSERT(subobj); - removed = upb_inttable_removeptr(ref2, subobj, &v); - /* The following assertion will fail if the visit() function visits a subobj - * that it did not have a ref2 on, or visits the same subobj too many times. */ - UPB_ASSERT(removed); - newcount = upb_value_getint32(v) - 1; - if (newcount > 0) { - upb_inttable_insert2(ref2, (uintptr_t)subobj, upb_value_int32(newcount), - &upb_alloc_debugrefs); - } -} - -static void visit(const upb_refcounted *r, upb_refcounted_visit *v, - void *closure) { - /* In DEBUG_REFS mode we know what existing ref2 refs there are, so we know - * exactly the set of nodes that visit() should visit. So we verify visit()'s - * correctness here. */ - check_state state; - state.obj = r; - upb_inttable_init2(&state.ref2, UPB_CTYPE_INT32, &upb_alloc_debugrefs); - getref2s(r, &state.ref2); - - /* This should visit any children in the ref2 table. */ - if (r->vtbl->visit) r->vtbl->visit(r, visit_check, &state); - - /* This assertion will fail if the visit() function missed any children. */ - UPB_ASSERT(upb_inttable_count(&state.ref2) == 0); - upb_inttable_uninit2(&state.ref2, &upb_alloc_debugrefs); - if (r->vtbl->visit) r->vtbl->visit(r, v, closure); -} - -static void trackinit(upb_refcounted *r) { - r->refs = upb_malloc(&upb_alloc_debugrefs, sizeof(*r->refs)); - r->ref2s = upb_malloc(&upb_alloc_debugrefs, sizeof(*r->ref2s)); - upb_inttable_init2(r->refs, UPB_CTYPE_PTR, &upb_alloc_debugrefs); - upb_inttable_init2(r->ref2s, UPB_CTYPE_PTR, &upb_alloc_debugrefs); -} - -static void trackfree(const upb_refcounted *r) { - upb_inttable_uninit2(r->refs, &upb_alloc_debugrefs); - upb_inttable_uninit2(r->ref2s, &upb_alloc_debugrefs); - upb_free(&upb_alloc_debugrefs, r->refs); - upb_free(&upb_alloc_debugrefs, r->ref2s); -} - -#else - -static void track(const upb_refcounted *r, const void *owner, bool ref2) { - UPB_UNUSED(r); - UPB_UNUSED(owner); - UPB_UNUSED(ref2); -} - -static void untrack(const upb_refcounted *r, const void *owner, bool ref2) { - UPB_UNUSED(r); - UPB_UNUSED(owner); - UPB_UNUSED(ref2); -} - -static void checkref(const upb_refcounted *r, const void *owner, bool ref2) { - UPB_UNUSED(r); - UPB_UNUSED(owner); - UPB_UNUSED(ref2); -} - -static void trackinit(upb_refcounted *r) { - UPB_UNUSED(r); -} - -static void trackfree(const upb_refcounted *r) { - UPB_UNUSED(r); -} - -static void visit(const upb_refcounted *r, upb_refcounted_visit *v, - void *closure) { - if (r->vtbl->visit) r->vtbl->visit(r, v, closure); -} - -#endif /* UPB_DEBUG_REFS */ - - -/* freeze() *******************************************************************/ - -/* The freeze() operation is by far the most complicated part of this scheme. - * We compute strongly-connected components and then mutate the graph such that - * we preserve the invariants documented at the top of this file. And we must - * handle out-of-memory errors gracefully (without leaving the graph - * inconsistent), which adds to the fun. */ - -/* The state used by the freeze operation (shared across many functions). */ -typedef struct { - int depth; - int maxdepth; - uint64_t index; - /* Maps upb_refcounted* -> attributes (color, etc). attr layout varies by - * color. */ - upb_inttable objattr; - upb_inttable stack; /* stack of upb_refcounted* for Tarjan's algorithm. */ - upb_inttable groups; /* array of uint32_t*, malloc'd refcounts for new groups */ - upb_status *status; - jmp_buf err; -} tarjan; - -static void release_ref2(const upb_refcounted *obj, - const upb_refcounted *subobj, - void *closure); - -/* Node attributes -----------------------------------------------------------*/ - -/* After our analysis phase all nodes will be either GRAY or WHITE. */ - -typedef enum { - BLACK = 0, /* Object has not been seen. */ - GRAY, /* Object has been found via a refgroup but may not be reachable. */ - GREEN, /* Object is reachable and is currently on the Tarjan stack. */ - WHITE /* Object is reachable and has been assigned a group (SCC). */ -} color_t; - -UPB_NORETURN static void err(tarjan *t) { longjmp(t->err, 1); } -UPB_NORETURN static void oom(tarjan *t) { - upb_status_seterrmsg(t->status, "out of memory"); - err(t); -} - -static uint64_t trygetattr(const tarjan *t, const upb_refcounted *r) { - upb_value v; - return upb_inttable_lookupptr(&t->objattr, r, &v) ? - upb_value_getuint64(v) : 0; -} - -static uint64_t getattr(const tarjan *t, const upb_refcounted *r) { - upb_value v; - bool found = upb_inttable_lookupptr(&t->objattr, r, &v); - UPB_ASSERT(found); - return upb_value_getuint64(v); -} - -static void setattr(tarjan *t, const upb_refcounted *r, uint64_t attr) { - upb_inttable_removeptr(&t->objattr, r, NULL); - upb_inttable_insertptr(&t->objattr, r, upb_value_uint64(attr)); -} - -static color_t color(tarjan *t, const upb_refcounted *r) { - return trygetattr(t, r) & 0x3; /* Color is always stored in the low 2 bits. */ -} - -static void set_gray(tarjan *t, const upb_refcounted *r) { - UPB_ASSERT(color(t, r) == BLACK); - setattr(t, r, GRAY); -} - -/* Pushes an obj onto the Tarjan stack and sets it to GREEN. */ -static void push(tarjan *t, const upb_refcounted *r) { - UPB_ASSERT(color(t, r) == BLACK || color(t, r) == GRAY); - /* This defines the attr layout for the GREEN state. "index" and "lowlink" - * get 31 bits, which is plenty (limit of 2B objects frozen at a time). */ - setattr(t, r, GREEN | (t->index << 2) | (t->index << 33)); - if (++t->index == 0x80000000) { - upb_status_seterrmsg(t->status, "too many objects to freeze"); - err(t); - } - upb_inttable_push(&t->stack, upb_value_ptr((void*)r)); -} - -/* Pops an obj from the Tarjan stack and sets it to WHITE, with a ptr to its - * SCC group. */ -static upb_refcounted *pop(tarjan *t) { - upb_refcounted *r = upb_value_getptr(upb_inttable_pop(&t->stack)); - UPB_ASSERT(color(t, r) == GREEN); - /* This defines the attr layout for nodes in the WHITE state. - * Top of group stack is [group, NULL]; we point at group. */ - setattr(t, r, WHITE | (upb_inttable_count(&t->groups) - 2) << 8); - return r; -} - -static void tarjan_newgroup(tarjan *t) { - uint32_t *group = upb_gmalloc(sizeof(*group)); - if (!group) oom(t); - /* Push group and empty group leader (we'll fill in leader later). */ - if (!upb_inttable_push(&t->groups, upb_value_ptr(group)) || - !upb_inttable_push(&t->groups, upb_value_ptr(NULL))) { - upb_gfree(group); - oom(t); - } - *group = 0; -} - -static uint32_t idx(tarjan *t, const upb_refcounted *r) { - UPB_ASSERT(color(t, r) == GREEN); - return (getattr(t, r) >> 2) & 0x7FFFFFFF; -} - -static uint32_t lowlink(tarjan *t, const upb_refcounted *r) { - if (color(t, r) == GREEN) { - return getattr(t, r) >> 33; - } else { - return UINT32_MAX; - } -} - -static void set_lowlink(tarjan *t, const upb_refcounted *r, uint32_t lowlink) { - UPB_ASSERT(color(t, r) == GREEN); - setattr(t, r, ((uint64_t)lowlink << 33) | (getattr(t, r) & 0x1FFFFFFFF)); -} - -static uint32_t *group(tarjan *t, upb_refcounted *r) { - uint64_t groupnum; - upb_value v; - bool found; - - UPB_ASSERT(color(t, r) == WHITE); - groupnum = getattr(t, r) >> 8; - found = upb_inttable_lookup(&t->groups, groupnum, &v); - UPB_ASSERT(found); - return upb_value_getptr(v); -} - -/* If the group leader for this object's group has not previously been set, - * the given object is assigned to be its leader. */ -static upb_refcounted *groupleader(tarjan *t, upb_refcounted *r) { - uint64_t leader_slot; - upb_value v; - bool found; - - UPB_ASSERT(color(t, r) == WHITE); - leader_slot = (getattr(t, r) >> 8) + 1; - found = upb_inttable_lookup(&t->groups, leader_slot, &v); - UPB_ASSERT(found); - if (upb_value_getptr(v)) { - return upb_value_getptr(v); - } else { - upb_inttable_remove(&t->groups, leader_slot, NULL); - upb_inttable_insert(&t->groups, leader_slot, upb_value_ptr(r)); - return r; - } -} - - -/* Tarjan's algorithm --------------------------------------------------------*/ - -/* See: - * http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm */ -static void do_tarjan(const upb_refcounted *obj, tarjan *t); - -static void tarjan_visit(const upb_refcounted *obj, - const upb_refcounted *subobj, - void *closure) { - tarjan *t = closure; - if (++t->depth > t->maxdepth) { - upb_status_seterrf(t->status, "graph too deep to freeze (%d)", t->maxdepth); - err(t); - } else if (subobj->is_frozen || color(t, subobj) == WHITE) { - /* Do nothing: we don't want to visit or color already-frozen nodes, - * and WHITE nodes have already been assigned a SCC. */ - } else if (color(t, subobj) < GREEN) { - /* Subdef has not yet been visited; recurse on it. */ - do_tarjan(subobj, t); - set_lowlink(t, obj, UPB_MIN(lowlink(t, obj), lowlink(t, subobj))); - } else if (color(t, subobj) == GREEN) { - /* Subdef is in the stack and hence in the current SCC. */ - set_lowlink(t, obj, UPB_MIN(lowlink(t, obj), idx(t, subobj))); - } - --t->depth; -} - -static void do_tarjan(const upb_refcounted *obj, tarjan *t) { - if (color(t, obj) == BLACK) { - /* We haven't seen this object's group; mark the whole group GRAY. */ - const upb_refcounted *o = obj; - do { set_gray(t, o); } while ((o = o->next) != obj); - } - - push(t, obj); - visit(obj, tarjan_visit, t); - if (lowlink(t, obj) == idx(t, obj)) { - tarjan_newgroup(t); - while (pop(t) != obj) - ; - } -} - - -/* freeze() ------------------------------------------------------------------*/ - -static void crossref(const upb_refcounted *r, const upb_refcounted *subobj, - void *_t) { - tarjan *t = _t; - UPB_ASSERT(color(t, r) > BLACK); - if (color(t, subobj) > BLACK && r->group != subobj->group) { - /* Previously this ref was not reflected in subobj->group because they - * were in the same group; now that they are split a ref must be taken. */ - refgroup(subobj->group); - } -} - -static bool freeze(upb_refcounted *const*roots, int n, upb_status *s, - int maxdepth) { - volatile bool ret = false; - int i; - upb_inttable_iter iter; - - /* We run in two passes so that we can allocate all memory before performing - * any mutation of the input -- this allows us to leave the input unchanged - * in the case of memory allocation failure. */ - tarjan t; - t.index = 0; - t.depth = 0; - t.maxdepth = maxdepth; - t.status = s; - if (!upb_inttable_init(&t.objattr, UPB_CTYPE_UINT64)) goto err1; - if (!upb_inttable_init(&t.stack, UPB_CTYPE_PTR)) goto err2; - if (!upb_inttable_init(&t.groups, UPB_CTYPE_PTR)) goto err3; - if (setjmp(t.err) != 0) goto err4; - - - for (i = 0; i < n; i++) { - if (color(&t, roots[i]) < GREEN) { - do_tarjan(roots[i], &t); - } - } - - /* If we've made it this far, no further errors are possible so it's safe to - * mutate the objects without risk of leaving them in an inconsistent state. */ - ret = true; - - /* The transformation that follows requires care. The preconditions are: - * - all objects in attr map are WHITE or GRAY, and are in mutable groups - * (groups of all mutable objs) - * - no ref2(to, from) refs have incremented count(to) if both "to" and - * "from" are in our attr map (this follows from invariants (2) and (3)) */ - - /* Pass 1: we remove WHITE objects from their mutable groups, and add them to - * new groups according to the SCC's we computed. These new groups will - * consist of only frozen objects. None will be immediately collectible, - * because WHITE objects are by definition reachable from one of "roots", - * which the caller must own refs on. */ - upb_inttable_begin(&iter, &t.objattr); - for(; !upb_inttable_done(&iter); upb_inttable_next(&iter)) { - upb_refcounted *obj = (upb_refcounted*)upb_inttable_iter_key(&iter); - /* Since removal from a singly-linked list requires access to the object's - * predecessor, we consider obj->next instead of obj for moving. With the - * while() loop we guarantee that we will visit every node's predecessor. - * Proof: - * 1. every node's predecessor is in our attr map. - * 2. though the loop body may change a node's predecessor, it will only - * change it to be the node we are currently operating on, so with a - * while() loop we guarantee ourselves the chance to remove each node. */ - while (color(&t, obj->next) == WHITE && - group(&t, obj->next) != obj->next->group) { - upb_refcounted *leader; - - /* Remove from old group. */ - upb_refcounted *move = obj->next; - if (obj == move) { - /* Removing the last object from a group. */ - UPB_ASSERT(*obj->group == obj->individual_count); - upb_gfree(obj->group); - } else { - obj->next = move->next; - /* This may decrease to zero; we'll collect GRAY objects (if any) that - * remain in the group in the third pass. */ - UPB_ASSERT(*move->group >= move->individual_count); - *move->group -= move->individual_count; - } - - /* Add to new group. */ - leader = groupleader(&t, move); - if (move == leader) { - /* First object added to new group is its leader. */ - move->group = group(&t, move); - move->next = move; - *move->group = move->individual_count; - } else { - /* Group already has at least one object in it. */ - UPB_ASSERT(leader->group == group(&t, move)); - move->group = group(&t, move); - move->next = leader->next; - leader->next = move; - *move->group += move->individual_count; - } - - move->is_frozen = true; - } - } - - /* Pass 2: GRAY and WHITE objects "obj" with ref2(to, obj) references must - * increment count(to) if group(obj) != group(to) (which could now be the - * case if "to" was just frozen). */ - upb_inttable_begin(&iter, &t.objattr); - for(; !upb_inttable_done(&iter); upb_inttable_next(&iter)) { - upb_refcounted *obj = (upb_refcounted*)upb_inttable_iter_key(&iter); - visit(obj, crossref, &t); - } - - /* Pass 3: GRAY objects are collected if their group's refcount dropped to - * zero when we removed its white nodes. This can happen if they had only - * been kept alive by virtue of sharing a group with an object that was just - * frozen. - * - * It is important that we do this last, since the GRAY object's free() - * function could call unref2() on just-frozen objects, which will decrement - * refs that were added in pass 2. */ - upb_inttable_begin(&iter, &t.objattr); - for(; !upb_inttable_done(&iter); upb_inttable_next(&iter)) { - upb_refcounted *obj = (upb_refcounted*)upb_inttable_iter_key(&iter); - if (obj->group == NULL || *obj->group == 0) { - if (obj->group) { - upb_refcounted *o; - - /* We eagerly free() the group's count (since we can't easily determine - * the group's remaining size it's the easiest way to ensure it gets - * done). */ - upb_gfree(obj->group); - - /* Visit to release ref2's (done in a separate pass since release_ref2 - * depends on o->group being unmodified so it can test merged()). */ - o = obj; - do { visit(o, release_ref2, NULL); } while ((o = o->next) != obj); - - /* Mark "group" fields as NULL so we know to free the objects later in - * this loop, but also don't try to delete the group twice. */ - o = obj; - do { o->group = NULL; } while ((o = o->next) != obj); - } - freeobj(obj); - } - } - -err4: - if (!ret) { - upb_inttable_begin(&iter, &t.groups); - for(; !upb_inttable_done(&iter); upb_inttable_next(&iter)) - upb_gfree(upb_value_getptr(upb_inttable_iter_value(&iter))); - } - upb_inttable_uninit(&t.groups); -err3: - upb_inttable_uninit(&t.stack); -err2: - upb_inttable_uninit(&t.objattr); -err1: - return ret; -} - - -/* Misc internal functions ***************************************************/ - -static bool merged(const upb_refcounted *r, const upb_refcounted *r2) { - return r->group == r2->group; -} - -static void merge(upb_refcounted *r, upb_refcounted *from) { - upb_refcounted *base; - upb_refcounted *tmp; - - if (merged(r, from)) return; - *r->group += *from->group; - upb_gfree(from->group); - base = from; - - /* Set all refcount pointers in the "from" chain to the merged refcount. - * - * TODO(haberman): this linear algorithm can result in an overall O(n^2) bound - * if the user continuously extends a group by one object. Prevent this by - * using one of the techniques in this paper: - * http://bioinfo.ict.ac.cn/~dbu/AlgorithmCourses/Lectures/Union-Find-Tarjan.pdf */ - do { from->group = r->group; } while ((from = from->next) != base); - - /* Merge the two circularly linked lists by swapping their next pointers. */ - tmp = r->next; - r->next = base->next; - base->next = tmp; -} - -static void unref(const upb_refcounted *r); - -static void release_ref2(const upb_refcounted *obj, - const upb_refcounted *subobj, - void *closure) { - UPB_UNUSED(closure); - untrack(subobj, obj, true); - if (!merged(obj, subobj)) { - UPB_ASSERT(subobj->is_frozen); - unref(subobj); - } -} - -static void unref(const upb_refcounted *r) { - if (unrefgroup(r->group)) { - const upb_refcounted *o; - - upb_gfree(r->group); - - /* In two passes, since release_ref2 needs a guarantee that any subobjs - * are alive. */ - o = r; - do { visit(o, release_ref2, NULL); } while((o = o->next) != r); - - o = r; - do { - const upb_refcounted *next = o->next; - UPB_ASSERT(o->is_frozen || o->individual_count == 0); - freeobj((upb_refcounted*)o); - o = next; - } while(o != r); - } -} - -static void freeobj(upb_refcounted *o) { - trackfree(o); - o->vtbl->free((upb_refcounted*)o); -} - - -/* Public interface ***********************************************************/ - -bool upb_refcounted_init(upb_refcounted *r, - const struct upb_refcounted_vtbl *vtbl, - const void *owner) { -#ifndef NDEBUG - /* Endianness check. This is unrelated to upb_refcounted, it's just a - * convenient place to put the check that we can be assured will run for - * basically every program using upb. */ - const int x = 1; -#ifdef UPB_BIG_ENDIAN - UPB_ASSERT(*(char*)&x != 1); -#else - UPB_ASSERT(*(char*)&x == 1); -#endif -#endif - - r->next = r; - r->vtbl = vtbl; - r->individual_count = 0; - r->is_frozen = false; - r->group = upb_gmalloc(sizeof(*r->group)); - if (!r->group) return false; - *r->group = 0; - trackinit(r); - upb_refcounted_ref(r, owner); - return true; -} - -bool upb_refcounted_isfrozen(const upb_refcounted *r) { - return r->is_frozen; -} - -void upb_refcounted_ref(const upb_refcounted *r, const void *owner) { - track(r, owner, false); - if (!r->is_frozen) - ((upb_refcounted*)r)->individual_count++; - refgroup(r->group); -} - -void upb_refcounted_unref(const upb_refcounted *r, const void *owner) { - untrack(r, owner, false); - if (!r->is_frozen) - ((upb_refcounted*)r)->individual_count--; - unref(r); -} - -void upb_refcounted_ref2(const upb_refcounted *r, upb_refcounted *from) { - UPB_ASSERT(!from->is_frozen); /* Non-const pointer implies this. */ - track(r, from, true); - if (r->is_frozen) { - refgroup(r->group); - } else { - merge((upb_refcounted*)r, from); - } -} - -void upb_refcounted_unref2(const upb_refcounted *r, upb_refcounted *from) { - UPB_ASSERT(!from->is_frozen); /* Non-const pointer implies this. */ - untrack(r, from, true); - if (r->is_frozen) { - unref(r); - } else { - UPB_ASSERT(merged(r, from)); - } -} - -void upb_refcounted_donateref( - const upb_refcounted *r, const void *from, const void *to) { - UPB_ASSERT(from != to); - if (to != NULL) - upb_refcounted_ref(r, to); - if (from != NULL) - upb_refcounted_unref(r, from); -} - -void upb_refcounted_checkref(const upb_refcounted *r, const void *owner) { - checkref(r, owner, false); -} - -bool upb_refcounted_freeze(upb_refcounted *const*roots, int n, upb_status *s, - int maxdepth) { - int i; - bool ret; - for (i = 0; i < n; i++) { - UPB_ASSERT(!roots[i]->is_frozen); - } - ret = freeze(roots, n, s, maxdepth); - UPB_ASSERT(!s || ret == upb_ok(s)); - return ret; -} diff --git a/upb/refcounted.h b/upb/refcounted.h deleted file mode 100644 index 6698d38..0000000 --- a/upb/refcounted.h +++ /dev/null @@ -1,348 +0,0 @@ -/* -** upb::RefCounted (upb_refcounted) -** -** A refcounting scheme that supports circular refs. It accomplishes this by -** partitioning the set of objects into groups such that no cycle spans groups; -** we can then reference-count the group as a whole and ignore refs within the -** group. When objects are mutable, these groups are computed very -** conservatively; we group any objects that have ever had a link between them. -** When objects are frozen, we compute strongly-connected components which -** allows us to be precise and only group objects that are actually cyclic. -** -** This is a mixed C/C++ interface that offers a full API to both languages. -** See the top-level README for more information. -*/ - -#ifndef UPB_REFCOUNTED_H_ -#define UPB_REFCOUNTED_H_ - -#include "upb/table.int.h" - -/* Reference tracking will check ref()/unref() operations to make sure the - * ref ownership is correct. Where possible it will also make tools like - * Valgrind attribute ref leaks to the code that took the leaked ref, not - * the code that originally created the object. - * - * Enabling this requires the application to define upb_lock()/upb_unlock() - * functions that acquire/release a global mutex (or #define UPB_THREAD_UNSAFE). - * For this reason we don't enable it by default, even in debug builds. - */ - -/* #define UPB_DEBUG_REFS */ - -#ifdef __cplusplus -namespace upb { -class RefCounted; -template class reffed_ptr; -} -#endif - -UPB_DECLARE_TYPE(upb::RefCounted, upb_refcounted) - -struct upb_refcounted_vtbl; - -#ifdef __cplusplus - -class upb::RefCounted { - public: - /* Returns true if the given object is frozen. */ - bool IsFrozen() const; - - /* Increases the ref count, the new ref is owned by "owner" which must not - * already own a ref (and should not itself be a refcounted object if the ref - * could possibly be circular; see below). - * Thread-safe iff "this" is frozen. */ - void Ref(const void *owner) const; - - /* Release a ref that was acquired from upb_refcounted_ref() and collects any - * objects it can. */ - void Unref(const void *owner) const; - - /* Moves an existing ref from "from" to "to", without changing the overall - * ref count. DonateRef(foo, NULL, owner) is the same as Ref(foo, owner), - * but "to" may not be NULL. */ - void DonateRef(const void *from, const void *to) const; - - /* Verifies that a ref to the given object is currently held by the given - * owner. Only effective in UPB_DEBUG_REFS builds. */ - void CheckRef(const void *owner) const; - - private: - UPB_DISALLOW_POD_OPS(RefCounted, upb::RefCounted) -#else -struct upb_refcounted { -#endif - /* TODO(haberman): move the actual structure definition to structdefs.int.h. - * The only reason they are here is because inline functions need to see the - * definition of upb_handlers, which needs to see this definition. But we - * can change the upb_handlers inline functions to deal in raw offsets - * instead. - */ - - /* A single reference count shared by all objects in the group. */ - uint32_t *group; - - /* A singly-linked list of all objects in the group. */ - upb_refcounted *next; - - /* Table of function pointers for this type. */ - const struct upb_refcounted_vtbl *vtbl; - - /* Maintained only when mutable, this tracks the number of refs (but not - * ref2's) to this object. *group should be the sum of all individual_count - * in the group. */ - uint32_t individual_count; - - bool is_frozen; - -#ifdef UPB_DEBUG_REFS - upb_inttable *refs; /* Maps owner -> trackedref for incoming refs. */ - upb_inttable *ref2s; /* Set of targets for outgoing ref2s. */ -#endif -}; - -#ifdef UPB_DEBUG_REFS -extern upb_alloc upb_alloc_debugrefs; -#define UPB_REFCOUNT_INIT(vtbl, refs, ref2s) \ - {&static_refcount, NULL, vtbl, 0, true, refs, ref2s} -#else -#define UPB_REFCOUNT_INIT(vtbl, refs, ref2s) \ - {&static_refcount, NULL, vtbl, 0, true} -#endif - -UPB_BEGIN_EXTERN_C - -/* It is better to use tracked refs when possible, for the extra debugging - * capability. But if this is not possible (because you don't have easy access - * to a stable pointer value that is associated with the ref), you can pass - * UPB_UNTRACKED_REF instead. */ -extern const void *UPB_UNTRACKED_REF; - -/* Native C API. */ -bool upb_refcounted_isfrozen(const upb_refcounted *r); -void upb_refcounted_ref(const upb_refcounted *r, const void *owner); -void upb_refcounted_unref(const upb_refcounted *r, const void *owner); -void upb_refcounted_donateref( - const upb_refcounted *r, const void *from, const void *to); -void upb_refcounted_checkref(const upb_refcounted *r, const void *owner); - -#define UPB_REFCOUNTED_CMETHODS(type, upcastfunc) \ - UPB_INLINE bool type ## _isfrozen(const type *v) { \ - return upb_refcounted_isfrozen(upcastfunc(v)); \ - } \ - UPB_INLINE void type ## _ref(const type *v, const void *owner) { \ - upb_refcounted_ref(upcastfunc(v), owner); \ - } \ - UPB_INLINE void type ## _unref(const type *v, const void *owner) { \ - upb_refcounted_unref(upcastfunc(v), owner); \ - } \ - UPB_INLINE void type ## _donateref(const type *v, const void *from, const void *to) { \ - upb_refcounted_donateref(upcastfunc(v), from, to); \ - } \ - UPB_INLINE void type ## _checkref(const type *v, const void *owner) { \ - upb_refcounted_checkref(upcastfunc(v), owner); \ - } - -#define UPB_REFCOUNTED_CPPMETHODS \ - bool IsFrozen() const { \ - return upb::upcast_to(this)->IsFrozen(); \ - } \ - void Ref(const void *owner) const { \ - return upb::upcast_to(this)->Ref(owner); \ - } \ - void Unref(const void *owner) const { \ - return upb::upcast_to(this)->Unref(owner); \ - } \ - void DonateRef(const void *from, const void *to) const { \ - return upb::upcast_to(this)->DonateRef(from, to); \ - } \ - void CheckRef(const void *owner) const { \ - return upb::upcast_to(this)->CheckRef(owner); \ - } - -/* Internal-to-upb Interface **************************************************/ - -typedef void upb_refcounted_visit(const upb_refcounted *r, - const upb_refcounted *subobj, - void *closure); - -struct upb_refcounted_vtbl { - /* Must visit all subobjects that are currently ref'd via upb_refcounted_ref2. - * Must be longjmp()-safe. */ - void (*visit)(const upb_refcounted *r, upb_refcounted_visit *visit, void *c); - - /* Must free the object and release all references to other objects. */ - void (*free)(upb_refcounted *r); -}; - -/* Initializes the refcounted with a single ref for the given owner. Returns - * false if memory could not be allocated. */ -bool upb_refcounted_init(upb_refcounted *r, - const struct upb_refcounted_vtbl *vtbl, - const void *owner); - -/* Adds a ref from one refcounted object to another ("from" must not already - * own a ref). These refs may be circular; cycles will be collected correctly - * (if conservatively). These refs do not need to be freed in from's free() - * function. */ -void upb_refcounted_ref2(const upb_refcounted *r, upb_refcounted *from); - -/* Removes a ref that was acquired from upb_refcounted_ref2(), and collects any - * object it can. This is only necessary when "from" no longer points to "r", - * and not from from's "free" function. */ -void upb_refcounted_unref2(const upb_refcounted *r, upb_refcounted *from); - -#define upb_ref2(r, from) \ - upb_refcounted_ref2((const upb_refcounted*)r, (upb_refcounted*)from) -#define upb_unref2(r, from) \ - upb_refcounted_unref2((const upb_refcounted*)r, (upb_refcounted*)from) - -/* Freezes all mutable object reachable by ref2() refs from the given roots. - * This will split refcounting groups into precise SCC groups, so that - * refcounting of frozen objects can be more aggressive. If memory allocation - * fails, or if more than 2**31 mutable objects are reachable from "roots", or - * if the maximum depth of the graph exceeds "maxdepth", false is returned and - * the objects are unchanged. - * - * After this operation succeeds, the objects are frozen/const, and may not be - * used through non-const pointers. In particular, they may not be passed as - * the second parameter of upb_refcounted_{ref,unref}2(). On the upside, all - * operations on frozen refcounteds are threadsafe, and objects will be freed - * at the precise moment that they become unreachable. - * - * Caller must own refs on each object in the "roots" list. */ -bool upb_refcounted_freeze(upb_refcounted *const*roots, int n, upb_status *s, - int maxdepth); - -/* Shared by all compiled-in refcounted objects. */ -extern uint32_t static_refcount; - -UPB_END_EXTERN_C - -#ifdef __cplusplus -/* C++ Wrappers. */ -namespace upb { -inline bool RefCounted::IsFrozen() const { - return upb_refcounted_isfrozen(this); -} -inline void RefCounted::Ref(const void *owner) const { - upb_refcounted_ref(this, owner); -} -inline void RefCounted::Unref(const void *owner) const { - upb_refcounted_unref(this, owner); -} -inline void RefCounted::DonateRef(const void *from, const void *to) const { - upb_refcounted_donateref(this, from, to); -} -inline void RefCounted::CheckRef(const void *owner) const { - upb_refcounted_checkref(this, owner); -} -} /* namespace upb */ -#endif - - -/* upb::reffed_ptr ************************************************************/ - -#ifdef __cplusplus - -#include /* For std::swap(). */ - -/* Provides RAII semantics for upb refcounted objects. Each reffed_ptr owns a - * ref on whatever object it points to (if any). */ -template class upb::reffed_ptr { - public: - reffed_ptr() : ptr_(NULL) {} - - /* If ref_donor is NULL, takes a new ref, otherwise adopts from ref_donor. */ - template - reffed_ptr(U* val, const void* ref_donor = NULL) - : ptr_(upb::upcast(val)) { - if (ref_donor) { - UPB_ASSERT(ptr_); - ptr_->DonateRef(ref_donor, this); - } else if (ptr_) { - ptr_->Ref(this); - } - } - - template - reffed_ptr(const reffed_ptr& other) - : ptr_(upb::upcast(other.get())) { - if (ptr_) ptr_->Ref(this); - } - - reffed_ptr(const reffed_ptr& other) - : ptr_(upb::upcast(other.get())) { - if (ptr_) ptr_->Ref(this); - } - - ~reffed_ptr() { if (ptr_) ptr_->Unref(this); } - - template - reffed_ptr& operator=(const reffed_ptr& other) { - reset(other.get()); - return *this; - } - - reffed_ptr& operator=(const reffed_ptr& other) { - reset(other.get()); - return *this; - } - - /* TODO(haberman): add C++11 move construction/assignment for greater - * efficiency. */ - - void swap(reffed_ptr& other) { - if (ptr_ == other.ptr_) { - return; - } - - if (ptr_) ptr_->DonateRef(this, &other); - if (other.ptr_) other.ptr_->DonateRef(&other, this); - std::swap(ptr_, other.ptr_); - } - - T& operator*() const { - UPB_ASSERT(ptr_); - return *ptr_; - } - - T* operator->() const { - UPB_ASSERT(ptr_); - return ptr_; - } - - T* get() const { return ptr_; } - - /* If ref_donor is NULL, takes a new ref, otherwise adopts from ref_donor. */ - template - void reset(U* ptr = NULL, const void* ref_donor = NULL) { - reffed_ptr(ptr, ref_donor).swap(*this); - } - - template - reffed_ptr down_cast() { - return reffed_ptr(upb::down_cast(get())); - } - - template - reffed_ptr dyn_cast() { - return reffed_ptr(upb::dyn_cast(get())); - } - - /* Plain release() is unsafe; if we were the only owner, it would leak the - * object. Instead we provide this: */ - T* ReleaseTo(const void* new_owner) { - T* ret = NULL; - ptr_->DonateRef(this, new_owner); - std::swap(ret, ptr_); - return ret; - } - - private: - T* ptr_; -}; - -#endif /* __cplusplus */ - -#endif /* UPB_REFCOUNT_H_ */ -- cgit v1.2.3 From d2f9bec5c6f3c34362cf13e35e11d3dbc7888a32 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 12 Jan 2019 16:15:46 -0800 Subject: Removed old-style C++ handlers that relied on UB in favor of more normal ones. --- CMakeLists.txt | 2 - tests/pb/test_encoder.cc | 33 +- tests/test_util.h | 2 +- upb/bindings/stdc++/string.h | 6 +- upb/def.h | 1159 ++++++++++++++++++------------------------ upb/handlers-inl.h | 292 ++--------- upb/handlers.c | 175 +++---- upb/handlers.h | 816 +++++++++++++---------------- upb/json/printer.c | 44 +- upb/pb/compile_decoder.c | 8 +- upb/pb/decoder.h | 221 ++++---- upb/pb/encoder.c | 10 +- upb/pb/encoder.h | 70 ++- upb/pb/textprinter.c | 6 +- upb/sink.c | 6 +- upb/sink.h | 603 ++++++++++------------ upb/upb.h | 69 --- 17 files changed, 1408 insertions(+), 2114 deletions(-) (limited to 'upb') diff --git a/CMakeLists.txt b/CMakeLists.txt index 699653f..58d6571 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,7 +70,6 @@ add_library(upb upb/msgfactory.c upb/port_def.inc upb/port_undef.inc - upb/refcounted.c upb/sink.c upb/structs.int.h upb/table.c @@ -84,7 +83,6 @@ add_library(upb upb/handlers.h upb/msg.h upb/msgfactory.h - upb/refcounted.h upb/sink.h upb/upb.h) add_library(upb_pb diff --git a/tests/pb/test_encoder.cc b/tests/pb/test_encoder.cc index fac0dae..35c0e1e 100644 --- a/tests/pb/test_encoder.cc +++ b/tests/pb/test_encoder.cc @@ -18,12 +18,9 @@ std::string read_string(const char *filename) { void test_pb_roundtrip() { std::string input = read_string("google/protobuf/descriptor.pb"); - upb::SymbolTable* symtab = upb::SymbolTable::New(); - upb::HandlerCache* encoder_cache = upb::pb::Encoder::NewCache(); - upb::pb::CodeCache* decoder_cache = upb::pb::CodeCache::New(encoder_cache); - ASSERT(symtab); - ASSERT(encoder_cache); - ASSERT(decoder_cache); + upb::SymbolTable symtab; + upb::HandlerCache encoder_cache(upb::pb::EncoderPtr::NewCache()); + upb::pb::CodeCache decoder_cache(&encoder_cache); upb::Arena arena; google_protobuf_FileDescriptorSet *set = google_protobuf_FileDescriptorSet_parsenew( @@ -34,32 +31,28 @@ void test_pb_roundtrip() { google_protobuf_FileDescriptorSet_file(set, &n); ASSERT(n == 1); upb::Status status; - bool ok = symtab->AddFile(files[0], &status); + bool ok = symtab.AddFile(files[0], &status); if (!ok) { fprintf(stderr, "Error building def: %s\n", upb_status_errmsg(&status)); ASSERT(false); } - const upb::MessageDef *md = - symtab->LookupMessage("google.protobuf.FileDescriptorSet"); + upb::MessageDefPtr md = + symtab.LookupMessage("google.protobuf.FileDescriptorSet"); ASSERT(md); - const upb::Handlers* encoder_handlers = encoder_cache->Get(md); + const upb::Handlers *encoder_handlers = encoder_cache.Get(md); ASSERT(encoder_handlers); - const upb::pb::DecoderMethod* method = decoder_cache->Get(md); - ASSERT(method); + const upb::pb::DecoderMethodPtr method = decoder_cache.Get(md); upb::InlinedEnvironment<512> env; std::string output; upb::StringSink string_sink(&output); - upb::pb::Encoder* encoder = - upb::pb::Encoder::Create(&env, encoder_handlers, string_sink.input()); - upb::pb::Decoder* decoder = - upb::pb::Decoder::Create(&env, method, encoder->input()); - ok = upb::BufferSource::PutBuffer(input, decoder->input()); + upb::pb::EncoderPtr encoder = + upb::pb::EncoderPtr::Create(&env, encoder_handlers, string_sink.input()); + upb::pb::DecoderPtr decoder = + upb::pb::DecoderPtr::Create(&env, method, encoder.input()); + ok = upb::PutBuffer(input, decoder.input()); ASSERT(ok); ASSERT(input == output); - upb::pb::CodeCache::Free(decoder_cache); - upb::HandlerCache::Free(encoder_cache); - upb::SymbolTable::Free(symtab); } extern "C" { diff --git a/tests/test_util.h b/tests/test_util.h index f616c36..1b1ff01 100644 --- a/tests/test_util.h +++ b/tests/test_util.h @@ -12,7 +12,7 @@ #ifdef __cplusplus -upb::BufferHandle global_handle; +upb_bufhandle global_handle; /* A convenience class for parser tests. Provides some useful features: * diff --git a/upb/bindings/stdc++/string.h b/upb/bindings/stdc++/string.h index 99efd4f..4d7a719 100644 --- a/upb/bindings/stdc++/string.h +++ b/upb/bindings/stdc++/string.h @@ -9,7 +9,7 @@ namespace upb { template class FillStringHandler { public: - static void SetHandler(BytesHandler* handler) { + static void SetHandler(upb_byteshandler* handler) { upb_byteshandler_setstartstr(handler, &FillStringHandler::StartString, NULL); upb_byteshandler_setstring(handler, &FillStringHandler::StringBuf, NULL); @@ -28,7 +28,7 @@ class FillStringHandler { } static size_t StringBuf(void* c, const void* hd, const char* buf, size_t n, - const BufferHandle* h) { + const upb_bufhandle* h) { UPB_UNUSED(hd); UPB_UNUSED(h); @@ -55,7 +55,7 @@ class StringSink { BytesSink* input() { return &input_; } private: - BytesHandler handler_; + upb_byteshandler handler_; BytesSink input_; }; diff --git a/upb/def.h b/upb/def.h index 81b5659..fb8a71d 100644 --- a/upb/def.h +++ b/upb/def.h @@ -2,11 +2,11 @@ ** Defs are upb's internal representation of the constructs that can appear ** in a .proto file: ** -** - upb::MessageDef (upb_msgdef): describes a "message" construct. -** - upb::FieldDef (upb_fielddef): describes a message field. -** - upb::FileDef (upb_filedef): describes a .proto file and its defs. -** - upb::EnumDef (upb_enumdef): describes an enum. -** - upb::OneofDef (upb_oneofdef): describes a oneof. +** - upb::MessageDefPtr (upb_msgdef): describes a "message" construct. +** - upb::FieldDefPtr (upb_fielddef): describes a message field. +** - upb::FileDefPtr (upb_filedef): describes a .proto file and its defs. +** - upb::EnumDefPtr (upb_enumdef): describes an enum. +** - upb::OneofDefPtr (upb_oneofdef): describes a oneof. ** ** TODO: definitions of services. ** @@ -23,51 +23,101 @@ #ifdef __cplusplus #include +#include #include #include namespace upb { -class EnumDef; -class FieldDef; -class FileDef; -class MessageDef; -class OneofDef; +class EnumDefPtr; +class FieldDefPtr; +class FileDefPtr; +class MessageDefPtr; +class OneofDefPtr; class SymbolTable; } #endif -UPB_DECLARE_TYPE(upb::EnumDef, upb_enumdef) -UPB_DECLARE_TYPE(upb::FieldDef, upb_fielddef) -UPB_DECLARE_TYPE(upb::FileDef, upb_filedef) -UPB_DECLARE_TYPE(upb::MessageDef, upb_msgdef) -UPB_DECLARE_TYPE(upb::OneofDef, upb_oneofdef) -UPB_DECLARE_TYPE(upb::SymbolTable, upb_symtab) - - -/* upb::FieldDef **************************************************************/ +struct upb_enumdef; +typedef struct upb_enumdef upb_enumdef; +struct upb_fielddef; +typedef struct upb_fielddef upb_fielddef; +struct upb_filedef; +typedef struct upb_filedef upb_filedef; +struct upb_msgdef; +typedef struct upb_msgdef upb_msgdef; +struct upb_oneofdef; +typedef struct upb_oneofdef upb_oneofdef; +struct upb_symtab; +typedef struct upb_symtab upb_symtab; + +/* upb_fielddef ***************************************************************/ /* Maximum field number allowed for FieldDefs. This is an inherent limit of the * protobuf wire format. */ #define UPB_MAX_FIELDNUMBER ((1 << 29) - 1) +UPB_BEGIN_EXTERN_C + +const char *upb_fielddef_fullname(const upb_fielddef *f); +upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f); +upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f); +upb_label_t upb_fielddef_label(const upb_fielddef *f); +uint32_t upb_fielddef_number(const upb_fielddef *f); +const char *upb_fielddef_name(const upb_fielddef *f); +bool upb_fielddef_isextension(const upb_fielddef *f); +bool upb_fielddef_lazy(const upb_fielddef *f); +bool upb_fielddef_packed(const upb_fielddef *f); +size_t upb_fielddef_getjsonname(const upb_fielddef *f, char *buf, size_t len); +const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f); +const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f); +upb_msgdef *upb_fielddef_containingtype_mutable(upb_fielddef *f); +uint32_t upb_fielddef_index(const upb_fielddef *f); +bool upb_fielddef_issubmsg(const upb_fielddef *f); +bool upb_fielddef_isstring(const upb_fielddef *f); +bool upb_fielddef_isseq(const upb_fielddef *f); +bool upb_fielddef_isprimitive(const upb_fielddef *f); +bool upb_fielddef_ismap(const upb_fielddef *f); +int64_t upb_fielddef_defaultint64(const upb_fielddef *f); +int32_t upb_fielddef_defaultint32(const upb_fielddef *f); +uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f); +uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f); +bool upb_fielddef_defaultbool(const upb_fielddef *f); +float upb_fielddef_defaultfloat(const upb_fielddef *f); +double upb_fielddef_defaultdouble(const upb_fielddef *f); +const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len); +bool upb_fielddef_hassubdef(const upb_fielddef *f); +bool upb_fielddef_haspresence(const upb_fielddef *f); +const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f); +const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f); + +/* Internal only. */ +uint32_t upb_fielddef_selectorbase(const upb_fielddef *f); + +UPB_END_EXTERN_C + #ifdef __cplusplus /* A upb_fielddef describes a single field in a message. It is most often * found as a part of a upb_msgdef, but can also stand alone to represent * an extension. */ -class upb::FieldDef { +class upb::FieldDefPtr { public: + explicit FieldDefPtr(const upb_fielddef *ptr) : ptr_(ptr) {} + + const upb_fielddef* ptr() const { return ptr_; } + explicit operator bool() const { return ptr_ != nullptr; } + typedef upb_fieldtype_t Type; typedef upb_label_t Label; typedef upb_descriptortype_t DescriptorType; - const char* full_name() const; + const char* full_name() const { return upb_fielddef_fullname(ptr_); } - Type type() const; - Label label() const; - const char* name() const; - uint32_t number() const; - bool is_extension() const; + Type type() const { return upb_fielddef_type(ptr_); } + Label label() const { return upb_fielddef_label(ptr_); } + const char* name() const { return upb_fielddef_name(ptr_); } + uint32_t number() const { return upb_fielddef_number(ptr_); } + bool is_extension() const { return upb_fielddef_isextension(ptr_); } /* Copies the JSON name for this field into the given buffer. Returns the * actual size of the JSON name, including the NULL terminator. If the @@ -79,7 +129,9 @@ class upb::FieldDef { * name. However if the regular name is unset, the JSON name will be unset * also. */ - size_t GetJsonName(char* buf, size_t len) const; + size_t GetJsonName(char *buf, size_t len) const { + return upb_fielddef_getjsonname(ptr_, buf, len); + } /* Convenience version of the above function which copies the JSON name * into the given string, returning false if the name is not set. */ @@ -97,20 +149,20 @@ class upb::FieldDef { * TODO(haberman): I think we want to move this into a FieldOptions container * when we add support for custom options (the FieldOptions struct will * contain both regular FieldOptions like "lazy" *and* custom options). */ - bool lazy() const; + bool lazy() const { return upb_fielddef_lazy(ptr_); } /* For non-string, non-submessage fields, this indicates whether binary * protobufs are encoded in packed or non-packed format. * * TODO(haberman): see note above about putting options like this into a * FieldOptions container. */ - bool packed() const; + bool packed() const { return upb_fielddef_packed(ptr_); } /* An integer that can be used as an index into an array of fields for * whatever message this field belongs to. Guaranteed to be less than * f->containing_type()->field_count(). May only be accessed once the def has * been finalized. */ - uint32_t index() const; + uint32_t index() const { return upb_fielddef_index(ptr_); } /* The MessageDef to which this field belongs. * @@ -120,25 +172,27 @@ class upb::FieldDef { * If the field has not yet been added to a MessageDef, you can set the name * of the containing type symbolically instead. This is mostly useful for * extensions, where the extension is declared separately from the message. */ - const MessageDef* containing_type() const; + MessageDefPtr containing_type() const; /* The OneofDef to which this field belongs, or NULL if this field is not part * of a oneof. */ - const OneofDef* containing_oneof() const; + OneofDefPtr containing_oneof() const; /* The field's type according to the enum in descriptor.proto. This is not * the same as UPB_TYPE_*, because it distinguishes between (for example) * INT32 and SINT32, whereas our "type" enum does not. This return of * descriptor_type() is a function of type(), integer_format(), and * is_tag_delimited(). */ - DescriptorType descriptor_type() const; + DescriptorType descriptor_type() const { + return upb_fielddef_descriptortype(ptr_); + } /* Convenient field type tests. */ - bool IsSubMessage() const; - bool IsString() const; - bool IsSequence() const; - bool IsPrimitive() const; - bool IsMap() const; + bool IsSubMessage() const { return upb_fielddef_issubmsg(ptr_); } + bool IsString() const { return upb_fielddef_isstring(ptr_); } + bool IsSequence() const { return upb_fielddef_isseq(ptr_); } + bool IsPrimitive() const { return upb_fielddef_isprimitive(ptr_); } + bool IsMap() const { return upb_fielddef_ismap(ptr_); } /* Returns the non-string default value for this fielddef, which may either * be something the client set explicitly or the "default default" (0 for @@ -146,210 +200,170 @@ class upb::FieldDef { * returned value, except for enum fields that are still mutable. * * Requires that the given function matches the field's current type. */ - int64_t default_int64() const; - int32_t default_int32() const; - uint64_t default_uint64() const; - uint32_t default_uint32() const; - bool default_bool() const; - float default_float() const; - double default_double() const; + int64_t default_int64() const { return upb_fielddef_defaultint64(ptr_); } + int32_t default_int32() const { return upb_fielddef_defaultint32(ptr_); } + uint64_t default_uint64() const { return upb_fielddef_defaultuint64(ptr_); } + uint32_t default_uint32() const { return upb_fielddef_defaultuint32(ptr_); } + bool default_bool() const { return upb_fielddef_defaultbool(ptr_); } + float default_float() const { return upb_fielddef_defaultfloat(ptr_); } + double default_double() const { return upb_fielddef_defaultdouble(ptr_); } /* The resulting string is always NULL-terminated. If non-NULL, the length * will be stored in *len. */ - const char *default_string(size_t* len) const; + const char *default_string(size_t * len) const { + return upb_fielddef_defaultstr(ptr_, len); + } /* Returns the enum or submessage def for this field, if any. The field's * type must match (ie. you may only call enum_subdef() for fields where * type() == UPB_TYPE_ENUM). */ - const EnumDef* enum_subdef() const; - const MessageDef* message_subdef() const; + EnumDefPtr enum_subdef() const; + MessageDefPtr message_subdef() const; private: - UPB_DISALLOW_POD_OPS(FieldDef, upb::FieldDef) + const upb_fielddef *ptr_; }; -# endif /* defined(__cplusplus) */ - -UPB_BEGIN_EXTERN_C - -/* Native C API. */ -const char *upb_fielddef_fullname(const upb_fielddef *f); -bool upb_fielddef_typeisset(const upb_fielddef *f); -upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f); -upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f); -upb_label_t upb_fielddef_label(const upb_fielddef *f); -uint32_t upb_fielddef_number(const upb_fielddef *f); -const char *upb_fielddef_name(const upb_fielddef *f); -bool upb_fielddef_isextension(const upb_fielddef *f); -bool upb_fielddef_lazy(const upb_fielddef *f); -bool upb_fielddef_packed(const upb_fielddef *f); -size_t upb_fielddef_getjsonname(const upb_fielddef *f, char *buf, size_t len); -const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f); -const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f); -upb_msgdef *upb_fielddef_containingtype_mutable(upb_fielddef *f); -uint32_t upb_fielddef_index(const upb_fielddef *f); -bool upb_fielddef_issubmsg(const upb_fielddef *f); -bool upb_fielddef_isstring(const upb_fielddef *f); -bool upb_fielddef_isseq(const upb_fielddef *f); -bool upb_fielddef_isprimitive(const upb_fielddef *f); -bool upb_fielddef_ismap(const upb_fielddef *f); -int64_t upb_fielddef_defaultint64(const upb_fielddef *f); -int32_t upb_fielddef_defaultint32(const upb_fielddef *f); -uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f); -uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f); -bool upb_fielddef_defaultbool(const upb_fielddef *f); -float upb_fielddef_defaultfloat(const upb_fielddef *f); -double upb_fielddef_defaultdouble(const upb_fielddef *f); -const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len); -bool upb_fielddef_hassubdef(const upb_fielddef *f); -bool upb_fielddef_haspresence(const upb_fielddef *f); -const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f); -const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f); - -/* Internal only. */ -uint32_t upb_fielddef_selectorbase(const upb_fielddef *f); - -UPB_END_EXTERN_C +#endif /* __cplusplus */ +/* upb_oneofdef ***************************************************************/ -/* upb::MessageDef ************************************************************/ +UPB_BEGIN_EXTERN_C -typedef upb_inttable_iter upb_msg_field_iter; -typedef upb_strtable_iter upb_msg_oneof_iter; +typedef upb_inttable_iter upb_oneof_iter; -/* Well-known field tag numbers for map-entry messages. */ -#define UPB_MAPENTRY_KEY 1 -#define UPB_MAPENTRY_VALUE 2 +const char *upb_oneofdef_name(const upb_oneofdef *o); +const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o); +int upb_oneofdef_numfields(const upb_oneofdef *o); +uint32_t upb_oneofdef_index(const upb_oneofdef *o); -/* Well-known field tag numbers for Any messages. */ -#define UPB_ANY_TYPE 1 -#define UPB_ANY_VALUE 2 +/* Oneof lookups: + * - ntof: look up a field by name. + * - ntofz: look up a field by name (as a null-terminated string). + * - itof: look up a field by number. */ +const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o, + const char *name, size_t length); +UPB_INLINE const upb_fielddef *upb_oneofdef_ntofz(const upb_oneofdef *o, + const char *name) { + return upb_oneofdef_ntof(o, name, strlen(name)); +} +const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num); -/* Well-known field tag numbers for timestamp messages. */ -#define UPB_DURATION_SECONDS 1 -#define UPB_DURATION_NANOS 2 +/* upb_oneof_iter i; + * for(upb_oneof_begin(&i, e); !upb_oneof_done(&i); upb_oneof_next(&i)) { + * // ... + * } + */ +void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o); +void upb_oneof_next(upb_oneof_iter *iter); +bool upb_oneof_done(upb_oneof_iter *iter); +upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter); +void upb_oneof_iter_setdone(upb_oneof_iter *iter); +bool upb_oneof_iter_isequal(const upb_oneof_iter *iter1, + const upb_oneof_iter *iter2); -/* Well-known field tag numbers for duration messages. */ -#define UPB_TIMESTAMP_SECONDS 1 -#define UPB_TIMESTAMP_NANOS 2 +UPB_END_EXTERN_C #ifdef __cplusplus -/* Structure that describes a single .proto message type. */ -class upb::MessageDef { +/* Class that represents a oneof. */ +class upb::OneofDefPtr { public: - const char* full_name() const; - const char* name() const; - - /* The number of fields that belong to the MessageDef. */ - int field_count() const; + explicit OneofDefPtr(const upb_oneofdef *ptr) : ptr_(ptr) {} - /* The number of oneofs that belong to the MessageDef. */ - int oneof_count() const; + const upb_oneofdef* ptr() const { return ptr_; } + explicit operator bool() { return ptr_ != nullptr; } - upb_syntax_t syntax() const; + /* Returns the MessageDef that owns this OneofDef. */ + MessageDefPtr containing_type() const; - /* These return NULL if the field is not found. */ - const FieldDef* FindFieldByNumber(uint32_t number) const; - const FieldDef* FindFieldByName(const char* name, size_t len) const; + /* Returns the name of this oneof. This is the name used to look up the oneof + * by name once added to a message def. */ + const char* name() const { return upb_oneofdef_name(ptr_); } + /* Returns the number of fields currently defined in the oneof. */ + int field_count() const { return upb_oneofdef_numfields(ptr_); } - const FieldDef* FindFieldByName(const char *name) const { - return FindFieldByName(name, strlen(name)); + /* Looks up by name. */ + FieldDefPtr FindFieldByName(const char *name, size_t len) const { + return FieldDefPtr(upb_oneofdef_ntof(ptr_, name, len)); + } + FieldDefPtr FindFieldByName(const char* name) const { + return FieldDefPtr(upb_oneofdef_ntofz(ptr_, name)); } template - const FieldDef* FindFieldByName(const T& str) const { + FieldDefPtr FindFieldByName(const T& str) const { return FindFieldByName(str.c_str(), str.size()); } - OneofDef* FindOneofByName(const char* name, size_t len); - const OneofDef* FindOneofByName(const char* name, size_t len) const; - - const OneofDef* FindOneofByName(const char* name) const { - return FindOneofByName(name, strlen(name)); - } - - template - const OneofDef* FindOneofByName(const T& str) const { - return FindOneofByName(str.c_str(), str.size()); + /* Looks up by tag number. */ + FieldDefPtr FindFieldByNumber(uint32_t num) const { + return FieldDefPtr(upb_oneofdef_itof(ptr_, num)); } - /* Is this message a map entry? */ - void setmapentry(bool map_entry); - bool mapentry() const; - - /* Return the type of well known type message. UPB_WELLKNOWN_UNSPECIFIED for - * non-well-known message. */ - upb_wellknowntype_t wellknowntype() const; + class const_iterator + : public std::iterator { + public: + void operator++() { upb_oneof_next(&iter_); } - /* Whether is a number wrapper. */ - bool isnumberwrapper() const; + FieldDefPtr operator*() const { + return FieldDefPtr(upb_oneof_iter_field(&iter_)); + } - /* Iteration over fields. The order is undefined. */ - class const_field_iterator - : public std::iterator { - public: - explicit const_field_iterator(const MessageDef* md); - static const_field_iterator end(const MessageDef* md); + bool operator!=(const const_iterator& other) const { + return !upb_oneof_iter_isequal(&iter_, &other.iter_); + } - void operator++(); - const FieldDef* operator*() const; - bool operator!=(const const_field_iterator& other) const; - bool operator==(const const_field_iterator& other) const; + bool operator==(const const_iterator& other) const { + return upb_oneof_iter_isequal(&iter_, &other.iter_); + } private: - upb_msg_field_iter iter_; - }; + friend class OneofDefPtr; + + const_iterator() {} + explicit const_iterator(OneofDefPtr o) { + upb_oneof_begin(&iter_, o.ptr()); + } + static const_iterator end() { + const_iterator iter; + upb_oneof_iter_setdone(&iter.iter_); + return iter; + } - /* Iteration over oneofs. The order is undefined. */ - class const_oneof_iterator - : public std::iterator { - public: - explicit const_oneof_iterator(const MessageDef* md); - static const_oneof_iterator end(const MessageDef* md); + upb_oneof_iter iter_; + }; - void operator++(); - const OneofDef* operator*() const; - bool operator!=(const const_oneof_iterator& other) const; - bool operator==(const const_oneof_iterator& other) const; + const_iterator begin() const { return const_iterator(*this); } + const_iterator end() const { return const_iterator::end(); } - private: - upb_msg_oneof_iter iter_; - }; + private: + const upb_oneofdef *ptr_; +}; - class ConstFieldAccessor { - public: - explicit ConstFieldAccessor(const MessageDef* msg) : msg_(msg) {} - const_field_iterator begin() { return msg_->field_begin(); } - const_field_iterator end() { return msg_->field_end(); } - private: - const MessageDef* msg_; - }; +#endif /* __cplusplus */ - class ConstOneofAccessor { - public: - explicit ConstOneofAccessor(const MessageDef* msg) : msg_(msg) {} - const_oneof_iterator begin() { return msg_->oneof_begin(); } - const_oneof_iterator end() { return msg_->oneof_end(); } - private: - const MessageDef* msg_; - }; +/* upb_msgdef *****************************************************************/ - const_field_iterator field_begin() const; - const_field_iterator field_end() const; +typedef upb_inttable_iter upb_msg_field_iter; +typedef upb_strtable_iter upb_msg_oneof_iter; - const_oneof_iterator oneof_begin() const; - const_oneof_iterator oneof_end() const; +/* Well-known field tag numbers for map-entry messages. */ +#define UPB_MAPENTRY_KEY 1 +#define UPB_MAPENTRY_VALUE 2 - ConstFieldAccessor fields() const { return ConstFieldAccessor(this); } - ConstOneofAccessor oneofs() const { return ConstOneofAccessor(this); } +/* Well-known field tag numbers for Any messages. */ +#define UPB_ANY_TYPE 1 +#define UPB_ANY_VALUE 2 - private: - UPB_DISALLOW_POD_OPS(MessageDef, upb::MessageDef) -}; +/* Well-known field tag numbers for timestamp messages. */ +#define UPB_DURATION_SECONDS 1 +#define UPB_DURATION_NANOS 2 -#endif /* __cplusplus */ +/* Well-known field tag numbers for duration messages. */ +#define UPB_TIMESTAMP_SECONDS 1 +#define UPB_TIMESTAMP_NANOS 2 UPB_BEGIN_EXTERN_C @@ -362,30 +376,12 @@ bool upb_msgdef_mapentry(const upb_msgdef *m); upb_wellknowntype_t upb_msgdef_wellknowntype(const upb_msgdef *m); bool upb_msgdef_isnumberwrapper(const upb_msgdef *m); bool upb_msgdef_setsyntax(upb_msgdef *m, upb_syntax_t syntax); - -/* Internal-only. */ -size_t upb_msgdef_selectorcount(const upb_msgdef *m); -uint32_t upb_msgdef_submsgfieldcount(const upb_msgdef *m); - -/* Field lookup in a couple of different variations: - * - itof = int to field - * - ntof = name to field - * - ntofz = name to field, null-terminated string. */ const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i); const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name, size_t len); -int upb_msgdef_numfields(const upb_msgdef *m); - -UPB_INLINE const upb_fielddef *upb_msgdef_ntofz(const upb_msgdef *m, - const char *name) { - return upb_msgdef_ntof(m, name, strlen(name)); -} - -/* Oneof lookup: - * - ntoo = name to oneof - * - ntooz = name to oneof, null-terminated string. */ const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name, size_t len); +int upb_msgdef_numfields(const upb_msgdef *m); int upb_msgdef_numoneofs(const upb_msgdef *m); UPB_INLINE const upb_oneofdef *upb_msgdef_ntooz(const upb_msgdef *m, @@ -393,6 +389,15 @@ UPB_INLINE const upb_oneofdef *upb_msgdef_ntooz(const upb_msgdef *m, return upb_msgdef_ntoo(m, name, strlen(name)); } +UPB_INLINE const upb_fielddef *upb_msgdef_ntofz(const upb_msgdef *m, + const char *name) { + return upb_msgdef_ntof(m, name, strlen(name)); +} + +/* Internal-only. */ +size_t upb_msgdef_selectorcount(const upb_msgdef *m); +uint32_t upb_msgdef_submsgfieldcount(const upb_msgdef *m); + /* Lookup of either field or oneof by name. Returns whether either was found. * If the return is true, then the found def will be set, and the non-found * one set to NULL. */ @@ -423,75 +428,202 @@ void upb_msg_field_next(upb_msg_field_iter *iter); bool upb_msg_field_done(const upb_msg_field_iter *iter); upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter); void upb_msg_field_iter_setdone(upb_msg_field_iter *iter); +bool upb_msg_field_iter_isequal(const upb_msg_field_iter * iter1, + const upb_msg_field_iter * iter2); /* Similar to above, we also support iterating through the oneofs in a * msgdef. */ -void upb_msg_oneof_begin(upb_msg_oneof_iter *iter, const upb_msgdef *m); -void upb_msg_oneof_next(upb_msg_oneof_iter *iter); +void upb_msg_oneof_begin(upb_msg_oneof_iter * iter, const upb_msgdef *m); +void upb_msg_oneof_next(upb_msg_oneof_iter * iter); bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter); const upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter); -void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter); +void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter * iter); +bool upb_msg_oneof_iter_isequal(const upb_msg_oneof_iter *iter1, + const upb_msg_oneof_iter *iter2); UPB_END_EXTERN_C - -/* upb::EnumDef ***************************************************************/ - -typedef upb_strtable_iter upb_enum_iter; - #ifdef __cplusplus -class upb::EnumDef { +/* Structure that describes a single .proto message type. */ +class upb::MessageDefPtr { public: - const char* full_name() const; - const char* name() const; - /* The value that is used as the default when no field default is specified. - * If not set explicitly, the first value that was added will be used. - * The default value must be a member of the enum. - * Requires that value_count() > 0. */ - int32_t default_value() const; + MessageDefPtr(const upb_msgdef *ptr) : ptr_(ptr) {} - /* Returns the number of values currently defined in the enum. Note that - * multiple names can refer to the same number, so this may be greater than - * the total number of unique numbers. */ - int value_count() const; + const upb_msgdef *ptr() const { return ptr_; } + explicit operator bool() const { return ptr_ != nullptr; } - /* Lookups from name to integer, returning true if found. */ - bool FindValueByName(const char* name, int32_t* num) const; + const char* full_name() const { return upb_msgdef_fullname(ptr_); } + const char* name() const { return upb_msgdef_name(ptr_); } - /* Finds the name corresponding to the given number, or NULL if none was - * found. If more than one name corresponds to this number, returns the - * first one that was added. */ - const char* FindValueByNumber(int32_t num) const; + /* The number of fields that belong to the MessageDef. */ + int field_count() const { return upb_msgdef_numfields(ptr_); } - /* Iteration over name/value pairs. The order is undefined. - * Adding an enum val invalidates any iterators. - * - * TODO: make compatible with range-for, with elements as pairs? */ - class Iterator { - public: - explicit Iterator(const EnumDef*); + /* The number of oneofs that belong to the MessageDef. */ + int oneof_count() const { return upb_msgdef_numoneofs(ptr_); } - int32_t number(); - const char *name(); - bool Done(); - void Next(); + upb_syntax_t syntax() const { return upb_msgdef_syntax(ptr_); } - private: - upb_enum_iter iter_; - }; + /* These return null pointers if the field is not found. */ + FieldDefPtr FindFieldByNumber(uint32_t number) const { + return FieldDefPtr(upb_msgdef_itof(ptr_, number)); + } + FieldDefPtr FindFieldByName(const char* name, size_t len) const { + return FieldDefPtr(upb_msgdef_ntof(ptr_, name, len)); + } + FieldDefPtr FindFieldByName(const char *name) const { + return FieldDefPtr(upb_msgdef_ntofz(ptr_, name)); + } - private: - UPB_DISALLOW_POD_OPS(EnumDef, upb::EnumDef) -}; + template + FieldDefPtr FindFieldByName(const T& str) const { + return FindFieldByName(str.c_str(), str.size()); + } -#endif /* __cplusplus */ + OneofDefPtr FindOneofByName(const char* name, size_t len) const { + return OneofDefPtr(upb_msgdef_ntoo(ptr_, name, len)); + } -UPB_BEGIN_EXTERN_C + OneofDefPtr FindOneofByName(const char *name) const { + return OneofDefPtr(upb_msgdef_ntooz(ptr_, name)); + } -const char *upb_enumdef_fullname(const upb_enumdef *e); -const char *upb_enumdef_name(const upb_enumdef *e); -const upb_filedef *upb_enumdef_file(const upb_enumdef *e); + template + OneofDefPtr FindOneofByName(const T &str) const { + return FindOneofByName(str.c_str(), str.size()); + } + + /* Is this message a map entry? */ + bool mapentry() const { return upb_msgdef_mapentry(ptr_); } + + /* Return the type of well known type message. UPB_WELLKNOWN_UNSPECIFIED for + * non-well-known message. */ + upb_wellknowntype_t wellknowntype() const { + return upb_msgdef_wellknowntype(ptr_); + } + + /* Whether is a number wrapper. */ + bool isnumberwrapper() const { return upb_msgdef_isnumberwrapper(ptr_); } + + /* Iteration over fields. The order is undefined. */ + class const_field_iterator + : public std::iterator { + public: + void operator++() { upb_msg_field_next(&iter_); } + + FieldDefPtr operator*() const { + return FieldDefPtr(upb_msg_iter_field(&iter_)); + } + + bool operator!=(const const_field_iterator &other) const { + return !upb_msg_field_iter_isequal(&iter_, &other.iter_); + } + + bool operator==(const const_field_iterator &other) const { + return upb_msg_field_iter_isequal(&iter_, &other.iter_); + } + + private: + friend class MessageDefPtr; + + explicit const_field_iterator() {} + + explicit const_field_iterator(MessageDefPtr msg) { + upb_msg_field_begin(&iter_, msg.ptr()); + } + + static const_field_iterator end() { + const_field_iterator iter; + upb_msg_field_iter_setdone(&iter.iter_); + return iter; + } + + upb_msg_field_iter iter_; + }; + + /* Iteration over oneofs. The order is undefined. */ + class const_oneof_iterator + : public std::iterator { + public: + + void operator++() { upb_msg_oneof_next(&iter_); } + + OneofDefPtr operator*() const { + return OneofDefPtr(upb_msg_iter_oneof(&iter_)); + } + + bool operator!=(const const_oneof_iterator& other) const { + return !upb_msg_oneof_iter_isequal(&iter_, &other.iter_); + } + + bool operator==(const const_oneof_iterator &other) const { + return upb_msg_oneof_iter_isequal(&iter_, &other.iter_); + } + + private: + friend class MessageDefPtr; + + const_oneof_iterator() {} + + explicit const_oneof_iterator(MessageDefPtr msg) { + upb_msg_oneof_begin(&iter_, msg.ptr()); + } + + static const_oneof_iterator end() { + const_oneof_iterator iter; + upb_msg_oneof_iter_setdone(&iter.iter_); + return iter; + } + + upb_msg_oneof_iter iter_; + }; + + class ConstFieldAccessor { + public: + explicit ConstFieldAccessor(const upb_msgdef* md) : md_(md) {} + const_field_iterator begin() { return MessageDefPtr(md_).field_begin(); } + const_field_iterator end() { return MessageDefPtr(md_).field_end(); } + private: + const upb_msgdef* md_; + }; + + class ConstOneofAccessor { + public: + explicit ConstOneofAccessor(const upb_msgdef* md) : md_(md) {} + const_oneof_iterator begin() { return MessageDefPtr(md_).oneof_begin(); } + const_oneof_iterator end() { return MessageDefPtr(md_).oneof_end(); } + private: + const upb_msgdef* md_; + }; + + const_field_iterator field_begin() const { + return const_field_iterator(*this); + } + + const_field_iterator field_end() const { return const_field_iterator::end(); } + + const_oneof_iterator oneof_begin() const { + return const_oneof_iterator(*this); + } + + const_oneof_iterator oneof_end() const { return const_oneof_iterator::end(); } + + ConstFieldAccessor fields() const { return ConstFieldAccessor(ptr()); } + ConstOneofAccessor oneofs() const { return ConstOneofAccessor(ptr()); } + + private: + const upb_msgdef* ptr_; +}; + +#endif /* __cplusplus */ + +/* upb_enumdef ****************************************************************/ + +typedef upb_strtable_iter upb_enum_iter; + +const char *upb_enumdef_fullname(const upb_enumdef *e); +const char *upb_enumdef_name(const upb_enumdef *e); +const upb_filedef *upb_enumdef_file(const upb_enumdef *e); int32_t upb_enumdef_default(const upb_enumdef *e); int upb_enumdef_numvals(const upb_enumdef *e); @@ -519,188 +651,129 @@ bool upb_enum_done(upb_enum_iter *iter); const char *upb_enum_iter_name(upb_enum_iter *iter); int32_t upb_enum_iter_number(upb_enum_iter *iter); -UPB_END_EXTERN_C - - -/* upb::OneofDef **************************************************************/ - -typedef upb_inttable_iter upb_oneof_iter; - #ifdef __cplusplus -/* Class that represents a oneof. */ -class upb::OneofDef { +class upb::EnumDefPtr { public: - /* Returns the MessageDef that owns this OneofDef. */ - const MessageDef* containing_type() const; + explicit EnumDefPtr(const upb_enumdef* ptr) : ptr_(ptr) {} - /* Returns the name of this oneof. This is the name used to look up the oneof - * by name once added to a message def. */ - const char* name() const; + const upb_enumdef* ptr() const { return ptr_; } + explicit operator bool() const { return ptr_ != nullptr; } - /* Returns the number of fields currently defined in the oneof. */ - int field_count() const; + const char* full_name() const { return upb_enumdef_fullname(ptr_); } + const char* name() const { return upb_enumdef_name(ptr_); } - /* Looks up by name. */ - const FieldDef* FindFieldByName(const char* name, size_t len) const; - FieldDef* FindFieldByName(const char* name, size_t len); - const FieldDef* FindFieldByName(const char* name) const { - return FindFieldByName(name, strlen(name)); - } + /* The value that is used as the default when no field default is specified. + * If not set explicitly, the first value that was added will be used. + * The default value must be a member of the enum. + * Requires that value_count() > 0. */ + int32_t default_value() const { return upb_enumdef_default(ptr_); } - template - const FieldDef* FindFieldByName(const T& str) const { - return FindFieldByName(str.c_str(), str.size()); + /* Returns the number of values currently defined in the enum. Note that + * multiple names can refer to the same number, so this may be greater than + * the total number of unique numbers. */ + int value_count() const { return upb_enumdef_numvals(ptr_); } + + /* Lookups from name to integer, returning true if found. */ + bool FindValueByName(const char *name, int32_t *num) const { + return upb_enumdef_ntoiz(ptr_, name, num); } - /* Looks up by tag number. */ - const FieldDef* FindFieldByNumber(uint32_t num) const; + /* Finds the name corresponding to the given number, or NULL if none was + * found. If more than one name corresponds to this number, returns the + * first one that was added. */ + const char *FindValueByNumber(int32_t num) const { + return upb_enumdef_iton(ptr_, num); + } - class const_iterator - : public std::iterator { + /* Iteration over name/value pairs. The order is undefined. + * Adding an enum val invalidates any iterators. + * + * TODO: make compatible with range-for, with elements as pairs? */ + class Iterator { public: - explicit const_iterator(const OneofDef* md); - static const_iterator end(const OneofDef* md); + explicit Iterator(EnumDefPtr e) { upb_enum_begin(&iter_, e.ptr()); } - void operator++(); - const FieldDef* operator*() const; - bool operator!=(const const_iterator& other) const; - bool operator==(const const_iterator& other) const; + int32_t number() { return upb_enum_iter_number(&iter_); } + const char *name() { return upb_enum_iter_name(&iter_); } + bool Done() { return upb_enum_done(&iter_); } + void Next() { return upb_enum_next(&iter_); } private: - upb_oneof_iter iter_; + upb_enum_iter iter_; }; - const_iterator begin() const; - const_iterator end() const; - private: - UPB_DISALLOW_POD_OPS(OneofDef, upb::OneofDef) + const upb_enumdef *ptr_; }; #endif /* __cplusplus */ -UPB_BEGIN_EXTERN_C +/* upb_filedef ****************************************************************/ -const char *upb_oneofdef_name(const upb_oneofdef *o); -const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o); -int upb_oneofdef_numfields(const upb_oneofdef *o); -uint32_t upb_oneofdef_index(const upb_oneofdef *o); - -/* Oneof lookups: - * - ntof: look up a field by name. - * - ntofz: look up a field by name (as a null-terminated string). - * - itof: look up a field by number. */ -const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o, - const char *name, size_t length); -UPB_INLINE const upb_fielddef *upb_oneofdef_ntofz(const upb_oneofdef *o, - const char *name) { - return upb_oneofdef_ntof(o, name, strlen(name)); -} -const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num); +UPB_BEGIN_EXTERN_C -/* upb_oneof_iter i; - * for(upb_oneof_begin(&i, e); !upb_oneof_done(&i); upb_oneof_next(&i)) { - * // ... - * } - */ -void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o); -void upb_oneof_next(upb_oneof_iter *iter); -bool upb_oneof_done(upb_oneof_iter *iter); -upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter); -void upb_oneof_iter_setdone(upb_oneof_iter *iter); +const char *upb_filedef_name(const upb_filedef *f); +const char *upb_filedef_package(const upb_filedef *f); +const char *upb_filedef_phpprefix(const upb_filedef *f); +const char *upb_filedef_phpnamespace(const upb_filedef *f); +upb_syntax_t upb_filedef_syntax(const upb_filedef *f); +int upb_filedef_depcount(const upb_filedef *f); +int upb_filedef_msgcount(const upb_filedef *f); +int upb_filedef_enumcount(const upb_filedef *f); +const upb_filedef *upb_filedef_dep(const upb_filedef *f, int i); +const upb_msgdef *upb_filedef_msg(const upb_filedef *f, int i); +const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i); UPB_END_EXTERN_C - -/* upb::FileDef ***************************************************************/ - #ifdef __cplusplus /* Class that represents a .proto file with some things defined in it. * * Many users won't care about FileDefs, but they are necessary if you want to * read the values of file-level options. */ -class upb::FileDef { +class upb::FileDefPtr { public: + explicit FileDefPtr(const upb_filedef *ptr) : ptr_(ptr) {} + + const upb_filedef* ptr() const { return ptr_; } + explicit operator bool() const { return ptr_ != nullptr; } + /* Get/set name of the file (eg. "foo/bar.proto"). */ - const char* name() const; + const char* name() const { return upb_filedef_name(ptr_); } /* Package name for definitions inside the file (eg. "foo.bar"). */ - const char* package() const; + const char* package() const { return upb_filedef_package(ptr_); } /* Sets the php class prefix which is prepended to all php generated classes * from this .proto. Default is empty. */ - const char* phpprefix() const; + const char* phpprefix() const { return upb_filedef_phpprefix(ptr_); } /* Use this option to change the namespace of php generated classes. Default * is empty. When this option is empty, the package name will be used for * determining the namespace. */ - const char* phpnamespace() const; + const char* phpnamespace() const { return upb_filedef_phpnamespace(ptr_); } /* Syntax for the file. Defaults to proto2. */ - upb_syntax_t syntax() const; + upb_syntax_t syntax() const { return upb_filedef_syntax(ptr_); } /* Get the list of dependencies from the file. These are returned in the - * order that they were added to the FileDef. */ - int dependency_count() const; - const FileDef* dependency(int index) const; - - private: - UPB_DISALLOW_POD_OPS(FileDef, upb::FileDef) -}; - -#endif - -UPB_BEGIN_EXTERN_C - -const char *upb_filedef_name(const upb_filedef *f); -const char *upb_filedef_package(const upb_filedef *f); -const char *upb_filedef_phpprefix(const upb_filedef *f); -const char *upb_filedef_phpnamespace(const upb_filedef *f); -upb_syntax_t upb_filedef_syntax(const upb_filedef *f); -int upb_filedef_depcount(const upb_filedef *f); -int upb_filedef_msgcount(const upb_filedef *f); -int upb_filedef_enumcount(const upb_filedef *f); -const upb_filedef *upb_filedef_dep(const upb_filedef *f, int i); -const upb_msgdef *upb_filedef_msg(const upb_filedef *f, int i); -const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i); - -UPB_END_EXTERN_C - -#ifdef __cplusplus - -/* Non-const methods in upb::SymbolTable are NOT thread-safe. */ -class upb::SymbolTable { - public: - /* Returns a new symbol table with a single ref owned by "owner." - * Returns NULL if memory allocation failed. */ - static SymbolTable* New(); - static void Free(upb::SymbolTable* table); - - /* Finds an entry in the symbol table with this exact name. If not found, - * returns NULL. */ - const MessageDef* LookupMessage(const char *sym) const; - const EnumDef* LookupEnum(const char *sym) const; - - /* TODO: iteration? */ - - /* Adds the given serialized FileDescriptorProto to the pool. */ - bool AddFile(const google_protobuf_FileDescriptorProto *file_proto, - Status *status); - - /* Adds the given serialized FileDescriptorSet to the pool. */ - bool AddSet(const char *set, size_t len, Status *status); + * order that they were added to the FileDefPtr. */ + int dependency_count() const { return upb_filedef_depcount(ptr_); } + const FileDefPtr dependency(int index) const { + return FileDefPtr(upb_filedef_dep(ptr_, index)); + } private: - UPB_DISALLOW_POD_OPS(SymbolTable, upb::SymbolTable) + const upb_filedef* ptr_; }; #endif /* __cplusplus */ -UPB_BEGIN_EXTERN_C +/* upb_symtab *****************************************************************/ -/* Native C API. */ +UPB_BEGIN_EXTERN_C upb_symtab *upb_symtab_new(); void upb_symtab_free(upb_symtab* s); @@ -725,299 +798,43 @@ bool _upb_symtab_loaddefinit(upb_symtab *s, const upb_def_init *init); UPB_END_EXTERN_C #ifdef __cplusplus -/* C++ inline wrappers. */ -namespace upb { -inline SymbolTable* SymbolTable::New() { - return upb_symtab_new(); -} -inline void SymbolTable::Free(SymbolTable* s) { - upb_symtab_free(s); -} -inline const MessageDef *SymbolTable::LookupMessage(const char *sym) const { - return upb_symtab_lookupmsg(this, sym); -} -inline bool SymbolTable::AddFile( - const google_protobuf_FileDescriptorProto *file_proto, Status *status) { - return upb_symtab_addfile(this, file_proto, status); -} -} /* namespace upb */ -#endif - -#ifdef __cplusplus - -UPB_INLINE const char* upb_safecstr(const std::string& str) { - UPB_ASSERT(str.size() == std::strlen(str.c_str())); - return str.c_str(); -} - -/* Inline C++ wrappers. */ -namespace upb { - -inline const char* FieldDef::full_name() const { - return upb_fielddef_fullname(this); -} -inline FieldDef::Type FieldDef::type() const { return upb_fielddef_type(this); } -inline FieldDef::DescriptorType FieldDef::descriptor_type() const { - return upb_fielddef_descriptortype(this); -} -inline FieldDef::Label FieldDef::label() const { - return upb_fielddef_label(this); -} -inline uint32_t FieldDef::number() const { return upb_fielddef_number(this); } -inline const char* FieldDef::name() const { return upb_fielddef_name(this); } -inline bool FieldDef::is_extension() const { - return upb_fielddef_isextension(this); -} -inline size_t FieldDef::GetJsonName(char* buf, size_t len) const { - return upb_fielddef_getjsonname(this, buf, len); -} -inline bool FieldDef::lazy() const { - return upb_fielddef_lazy(this); -} -inline bool FieldDef::packed() const { - return upb_fielddef_packed(this); -} -inline uint32_t FieldDef::index() const { - return upb_fielddef_index(this); -} -inline const MessageDef* FieldDef::containing_type() const { - return upb_fielddef_containingtype(this); -} -inline const OneofDef* FieldDef::containing_oneof() const { - return upb_fielddef_containingoneof(this); -} -inline bool FieldDef::IsSubMessage() const { - return upb_fielddef_issubmsg(this); -} -inline bool FieldDef::IsString() const { return upb_fielddef_isstring(this); } -inline bool FieldDef::IsSequence() const { return upb_fielddef_isseq(this); } -inline bool FieldDef::IsMap() const { return upb_fielddef_ismap(this); } -inline int64_t FieldDef::default_int64() const { - return upb_fielddef_defaultint64(this); -} -inline int32_t FieldDef::default_int32() const { - return upb_fielddef_defaultint32(this); -} -inline uint64_t FieldDef::default_uint64() const { - return upb_fielddef_defaultuint64(this); -} -inline uint32_t FieldDef::default_uint32() const { - return upb_fielddef_defaultuint32(this); -} -inline bool FieldDef::default_bool() const { - return upb_fielddef_defaultbool(this); -} -inline float FieldDef::default_float() const { - return upb_fielddef_defaultfloat(this); -} -inline double FieldDef::default_double() const { - return upb_fielddef_defaultdouble(this); -} -inline const char* FieldDef::default_string(size_t* len) const { - return upb_fielddef_defaultstr(this, len); -} -inline const MessageDef *FieldDef::message_subdef() const { - return upb_fielddef_msgsubdef(this); -} -inline const EnumDef *FieldDef::enum_subdef() const { - return upb_fielddef_enumsubdef(this); -} -inline const char *MessageDef::full_name() const { - return upb_msgdef_fullname(this); -} -inline const char *MessageDef::name() const { - return upb_msgdef_name(this); -} -inline upb_syntax_t MessageDef::syntax() const { - return upb_msgdef_syntax(this); -} -inline int MessageDef::field_count() const { - return upb_msgdef_numfields(this); -} -inline int MessageDef::oneof_count() const { - return upb_msgdef_numoneofs(this); -} -inline const FieldDef* MessageDef::FindFieldByNumber(uint32_t number) const { - return upb_msgdef_itof(this, number); -} -inline const FieldDef *MessageDef::FindFieldByName(const char *name, - size_t len) const { - return upb_msgdef_ntof(this, name, len); -} -inline const OneofDef* MessageDef::FindOneofByName(const char* name, - size_t len) const { - return upb_msgdef_ntoo(this, name, len); -} -inline bool MessageDef::mapentry() const { - return upb_msgdef_mapentry(this); -} -inline upb_wellknowntype_t MessageDef::wellknowntype() const { - return upb_msgdef_wellknowntype(this); -} -inline bool MessageDef::isnumberwrapper() const { - return upb_msgdef_isnumberwrapper(this); -} -inline MessageDef::const_field_iterator MessageDef::field_begin() const { - return const_field_iterator(this); -} -inline MessageDef::const_field_iterator MessageDef::field_end() const { - return const_field_iterator::end(this); -} - -inline MessageDef::const_oneof_iterator MessageDef::oneof_begin() const { - return const_oneof_iterator(this); -} -inline MessageDef::const_oneof_iterator MessageDef::oneof_end() const { - return const_oneof_iterator::end(this); -} +/* Non-const methods in upb::SymbolTable are NOT thread-safe. */ +class upb::SymbolTable { + public: + SymbolTable() : ptr_(upb_symtab_new(), upb_symtab_free) {} + explicit SymbolTable(upb_symtab* s) : ptr_(s, upb_symtab_free) {} -inline MessageDef::const_field_iterator::const_field_iterator( - const MessageDef* md) { - upb_msg_field_begin(&iter_, md); -} -inline MessageDef::const_field_iterator MessageDef::const_field_iterator::end( - const MessageDef *md) { - MessageDef::const_field_iterator iter(md); - upb_msg_field_iter_setdone(&iter.iter_); - return iter; -} -inline const FieldDef* MessageDef::const_field_iterator::operator*() const { - return upb_msg_iter_field(&iter_); -} -inline void MessageDef::const_field_iterator::operator++() { - return upb_msg_field_next(&iter_); -} -inline bool MessageDef::const_field_iterator::operator==( - const const_field_iterator &other) const { - return upb_inttable_iter_isequal(&iter_, &other.iter_); -} -inline bool MessageDef::const_field_iterator::operator!=( - const const_field_iterator &other) const { - return !(*this == other); -} + const upb_symtab* ptr() const { return ptr_.get(); } + upb_symtab* ptr() { return ptr_.get(); } -inline MessageDef::const_oneof_iterator::const_oneof_iterator( - const MessageDef* md) { - upb_msg_oneof_begin(&iter_, md); -} -inline MessageDef::const_oneof_iterator MessageDef::const_oneof_iterator::end( - const MessageDef *md) { - MessageDef::const_oneof_iterator iter(md); - upb_msg_oneof_iter_setdone(&iter.iter_); - return iter; -} -inline const OneofDef* MessageDef::const_oneof_iterator::operator*() const { - return upb_msg_iter_oneof(&iter_); -} -inline void MessageDef::const_oneof_iterator::operator++() { - return upb_msg_oneof_next(&iter_); -} -inline bool MessageDef::const_oneof_iterator::operator==( - const const_oneof_iterator &other) const { - return upb_strtable_iter_isequal(&iter_, &other.iter_); -} -inline bool MessageDef::const_oneof_iterator::operator!=( - const const_oneof_iterator &other) const { - return !(*this == other); -} + /* Finds an entry in the symbol table with this exact name. If not found, + * returns NULL. */ + MessageDefPtr LookupMessage(const char *sym) const { + return MessageDefPtr(upb_symtab_lookupmsg(ptr_.get(), sym)); + } -inline const char* EnumDef::full_name() const { - return upb_enumdef_fullname(this); -} -inline const char* EnumDef::name() const { - return upb_enumdef_name(this); -} -inline int32_t EnumDef::default_value() const { - return upb_enumdef_default(this); -} -inline int EnumDef::value_count() const { return upb_enumdef_numvals(this); } -inline bool EnumDef::FindValueByName(const char* name, int32_t *num) const { - return upb_enumdef_ntoiz(this, name, num); -} -inline const char* EnumDef::FindValueByNumber(int32_t num) const { - return upb_enumdef_iton(this, num); -} + const EnumDefPtr LookupEnum(const char *sym) const { + return EnumDefPtr(upb_symtab_lookupenum(ptr_.get(), sym)); + } -inline EnumDef::Iterator::Iterator(const EnumDef* e) { - upb_enum_begin(&iter_, e); -} -inline int32_t EnumDef::Iterator::number() { - return upb_enum_iter_number(&iter_); -} -inline const char* EnumDef::Iterator::name() { - return upb_enum_iter_name(&iter_); -} -inline bool EnumDef::Iterator::Done() { return upb_enum_done(&iter_); } -inline void EnumDef::Iterator::Next() { return upb_enum_next(&iter_); } + /* TODO: iteration? */ -inline const MessageDef* OneofDef::containing_type() const { - return upb_oneofdef_containingtype(this); -} -inline const char* OneofDef::name() const { - return upb_oneofdef_name(this); -} -inline int OneofDef::field_count() const { - return upb_oneofdef_numfields(this); -} -inline const FieldDef* OneofDef::FindFieldByName(const char* name, - size_t len) const { - return upb_oneofdef_ntof(this, name, len); -} -inline const FieldDef* OneofDef::FindFieldByNumber(uint32_t num) const { - return upb_oneofdef_itof(this, num); -} -inline OneofDef::const_iterator OneofDef::begin() const { - return const_iterator(this); -} -inline OneofDef::const_iterator OneofDef::end() const { - return const_iterator::end(this); -} + /* Adds the given serialized FileDescriptorProto to the pool. */ + bool AddFile(const google_protobuf_FileDescriptorProto *file_proto, + Status *status) { + return upb_symtab_addfile(ptr_.get(), file_proto, status); + } -inline OneofDef::const_iterator::const_iterator(const OneofDef* md) { - upb_oneof_begin(&iter_, md); -} -inline OneofDef::const_iterator OneofDef::const_iterator::end( - const OneofDef *md) { - OneofDef::const_iterator iter(md); - upb_oneof_iter_setdone(&iter.iter_); - return iter; -} -inline const FieldDef* OneofDef::const_iterator::operator*() const { - return upb_msg_iter_field(&iter_); -} -inline void OneofDef::const_iterator::operator++() { - return upb_oneof_next(&iter_); -} -inline bool OneofDef::const_iterator::operator==( - const const_iterator &other) const { - return upb_inttable_iter_isequal(&iter_, &other.iter_); -} -inline bool OneofDef::const_iterator::operator!=( - const const_iterator &other) const { - return !(*this == other); -} + private: + std::unique_ptr ptr_; +}; -inline const char* FileDef::name() const { - return upb_filedef_name(this); -} -inline const char* FileDef::package() const { - return upb_filedef_package(this); -} -inline const char* FileDef::phpprefix() const { - return upb_filedef_phpprefix(this); -} -inline const char* FileDef::phpnamespace() const { - return upb_filedef_phpnamespace(this); -} -inline int FileDef::dependency_count() const { - return upb_filedef_depcount(this); -} -inline const FileDef* FileDef::dependency(int index) const { - return upb_filedef_dep(this, index); +UPB_INLINE const char* upb_safecstr(const std::string& str) { + UPB_ASSERT(str.size() == std::strlen(str.c_str())); + return str.c_str(); } -} /* namespace upb */ -#endif +#endif /* __cplusplus */ #endif /* UPB_DEF_H_ */ diff --git a/upb/handlers-inl.h b/upb/handlers-inl.h index eb9a0fa..b038e30 100644 --- a/upb/handlers-inl.h +++ b/upb/handlers-inl.h @@ -8,39 +8,6 @@ #include -/* C inline methods. */ - -/* upb_bufhandle */ -UPB_INLINE void upb_bufhandle_init(upb_bufhandle *h) { - h->obj_ = NULL; - h->objtype_ = NULL; - h->buf_ = NULL; - h->objofs_ = 0; -} -UPB_INLINE void upb_bufhandle_uninit(upb_bufhandle *h) { - UPB_UNUSED(h); -} -UPB_INLINE void upb_bufhandle_setobj(upb_bufhandle *h, const void *obj, - const void *type) { - h->obj_ = obj; - h->objtype_ = type; -} -UPB_INLINE void upb_bufhandle_setbuf(upb_bufhandle *h, const char *buf, - size_t ofs) { - h->buf_ = buf; - h->objofs_ = ofs; -} -UPB_INLINE const void *upb_bufhandle_obj(const upb_bufhandle *h) { - return h->obj_; -} -UPB_INLINE const void *upb_bufhandle_objtype(const upb_bufhandle *h) { - return h->objtype_; -} -UPB_INLINE const char *upb_bufhandle_buf(const upb_bufhandle *h) { - return h->buf_; -} - - #ifdef __cplusplus /* Type detection and typedefs for integer types. @@ -604,9 +571,9 @@ void *ReturnClosureOrBreak3(P1 p1, P2 p2, P3 p3) { /* For the string callback, which takes five params, returns the size param. */ template + void F(P1, P2, const char *, size_t, const upb_bufhandle *)> size_t ReturnStringLen(P1 p1, P2 p2, const char *p3, size_t p4, - const BufferHandle *p5) { + const upb_bufhandle *p5) { F(p1, p2, p3, p4, p5); return p4; } @@ -614,9 +581,9 @@ size_t ReturnStringLen(P1 p1, P2 p2, const char *p3, size_t p4, /* For the string callback, which takes five params, returns the size param or * zero. */ template + bool F(P1, P2, const char *, size_t, const upb_bufhandle *)> size_t ReturnNOr0(P1 p1, P2 p2, const char *p3, size_t p4, - const BufferHandle *p5) { + const upb_bufhandle *p5) { return F(p1, p2, p3, p4, p5) ? p4 : 0; } @@ -675,22 +642,22 @@ struct MaybeWrapReturn, void *> { /* If our function returns void but we want one returning size_t, wrap it in a * function that returns the size argument. */ template + void F(P1, P2, const char *, size_t, const upb_bufhandle *), class I> struct MaybeWrapReturn< - Func5, + Func5, size_t> { - typedef Func5, I> Func; }; /* If our function returns bool but we want one returning size_t, wrap it in a * function that returns either 0 or the buf size. */ template + bool F(P1, P2, const char *, size_t, const upb_bufhandle *), class I> struct MaybeWrapReturn< - Func5, + Func5, size_t> { - typedef Func5, I> Func; }; @@ -731,7 +698,7 @@ R IgnoreHandlerData5(void *p1, const void *hd, P2 p2, P3 p3, P4 p4) { template R IgnoreHandlerDataIgnoreHandle(void *p1, const void *hd, const char *p2, - size_t p3, const BufferHandle *handle) { + size_t p3, const upb_bufhandle *handle) { UPB_UNUSED(hd); UPB_UNUSED(handle); return F(static_cast(p1), p2, p3); @@ -757,7 +724,7 @@ R CastHandlerData5(void *c, const void *hd, P3 p3, P4 p4, P5 p5) { template R CastHandlerDataIgnoreHandle(void *c, const void *hd, const char *p3, - size_t p4, const BufferHandle *handle) { + size_t p4, const upb_bufhandle *handle) { UPB_UNUSED(handle); return F(static_cast(c), static_cast(hd), p3, p4); } @@ -777,11 +744,11 @@ struct ConvertParams, }; /* For StringBuffer only; this ignores both the handler data and the - * BufferHandle. */ + * upb_bufhandle. */ template struct ConvertParams, T> { typedef Func5, + const upb_bufhandle *, IgnoreHandlerDataIgnoreHandle, I> Func; }; @@ -807,13 +774,14 @@ struct ConvertParams, CastHandlerData3, I> Func; }; -/* For StringBuffer only; this ignores the BufferHandle. */ +/* For StringBuffer only; this ignores the upb_bufhandle. */ template struct ConvertParams, T> { typedef Func5, - I> Func; + const upb_bufhandle *, + CastHandlerDataIgnoreHandle, I> + Func; }; template , T> { /* utype/ltype are upper/lower-case, ctype is canonical C type, vtype is * variant C type. */ -#define TYPE_METHODS(utype, ltype, ctype, vtype) \ - template <> struct CanonicalType { \ - typedef ctype Type; \ - }; \ - template <> \ - inline bool Handlers::SetValueHandler( \ - const FieldDef *f, \ - const Handlers::utype ## Handler& handler) { \ - UPB_ASSERT(!handler.registered_); \ - handler.AddCleanup(this); \ - handler.registered_ = true; \ - return upb_handlers_set##ltype(this, f, handler.handler_, &handler.attr_); \ - } \ +#define TYPE_METHODS(utype, ltype, ctype, vtype) \ + template <> \ + struct CanonicalType { \ + typedef ctype Type; \ + }; \ + template <> \ + inline bool HandlersPtr::SetValueHandler( \ + FieldDefPtr f, const HandlersPtr::utype##Handler &handler) { \ + handler.AddCleanup(ptr()); \ + return upb_handlers_set##ltype(ptr(), f.ptr(), handler.handler(), \ + &handler.attr()); \ + } TYPE_METHODS(Double, double, double, double) TYPE_METHODS(Float, float, float, float) @@ -862,24 +829,6 @@ template <> struct CanonicalType { typedef Status* Type; }; -/* Type methods that are only one-per-canonical-type and not - * one-per-cvariant. */ - -#define TYPE_METHODS(utype, ctype) \ - inline bool Handlers::Set##utype##Handler(const FieldDef *f, \ - const utype##Handler &h) { \ - return SetValueHandler(f, h); \ - } \ - -TYPE_METHODS(Double, double) -TYPE_METHODS(Float, float) -TYPE_METHODS(UInt64, uint64_t) -TYPE_METHODS(UInt32, uint32_t) -TYPE_METHODS(Int64, int64_t) -TYPE_METHODS(Int32, int32_t) -TYPE_METHODS(Bool, bool) -#undef TYPE_METHODS - template struct ReturnOf; template @@ -902,10 +851,6 @@ struct ReturnOf { typedef R Return; }; -template const void *UniquePtrForType() { - static const char ch = 0; - return &ch; -} template template @@ -926,10 +871,10 @@ inline Handler::Handler(F func) /* If the original function returns void, then we know that we wrapped it to * always return ok. */ bool always_ok = is_same::value; - attr_.SetAlwaysOk(always_ok); + attr_.alwaysok = always_ok; /* Closure parameter and return type. */ - attr_.SetClosureType(UniquePtrForType()); + attr_.closure_type = UniquePtrForType(); /* We use the closure type (from the first parameter) if the return type is * void or bool, since these are the two cases we wrap to return the closure's @@ -940,176 +885,19 @@ inline Handler::Handler(F func) typedef typename FirstUnlessVoidOrBool::value EffectiveReturn; - attr_.SetReturnClosureType(UniquePtrForType()); + attr_.return_closure_type = UniquePtrForType(); } template -inline Handler::~Handler() { - UPB_ASSERT(registered_); -} - -inline HandlerAttributes::HandlerAttributes() { upb_handlerattr_init(this); } -inline HandlerAttributes::~HandlerAttributes() { upb_handlerattr_uninit(this); } -inline bool HandlerAttributes::SetHandlerData(const void *hd) { - return upb_handlerattr_sethandlerdata(this, hd); -} -inline const void* HandlerAttributes::handler_data() const { - return upb_handlerattr_handlerdata(this); -} -inline bool HandlerAttributes::SetClosureType(const void *type) { - return upb_handlerattr_setclosuretype(this, type); -} -inline const void* HandlerAttributes::closure_type() const { - return upb_handlerattr_closuretype(this); -} -inline bool HandlerAttributes::SetReturnClosureType(const void *type) { - return upb_handlerattr_setreturnclosuretype(this, type); -} -inline const void* HandlerAttributes::return_closure_type() const { - return upb_handlerattr_returnclosuretype(this); -} -inline bool HandlerAttributes::SetAlwaysOk(bool always_ok) { - return upb_handlerattr_setalwaysok(this, always_ok); -} -inline bool HandlerAttributes::always_ok() const { - return upb_handlerattr_alwaysok(this); -} - -inline BufferHandle::BufferHandle() { upb_bufhandle_init(this); } -inline BufferHandle::~BufferHandle() { upb_bufhandle_uninit(this); } -inline const char* BufferHandle::buffer() const { - return upb_bufhandle_buf(this); -} -inline size_t BufferHandle::object_offset() const { - return upb_bufhandle_objofs(this); -} -inline void BufferHandle::SetBuffer(const char* buf, size_t ofs) { - upb_bufhandle_setbuf(this, buf, ofs); -} -template -void BufferHandle::SetAttachedObject(const T* obj) { - upb_bufhandle_setobj(this, obj, UniquePtrForType()); -} -template -const T* BufferHandle::GetAttachedObject() const { - return upb_bufhandle_objtype(this) == UniquePtrForType() - ? static_cast(upb_bufhandle_obj(this)) - : NULL; -} - -inline const Status* Handlers::status() { - return upb_handlers_status(this); -} -inline void Handlers::ClearError() { - return upb_handlers_clearerr(this); -} -inline const MessageDef *Handlers::message_def() const { - return upb_handlers_msgdef(this); -} -inline bool Handlers::AddCleanup(void *p, upb_handlerfree *func) { - return upb_handlers_addcleanup(this, p, func); -} -inline bool Handlers::SetStartMessageHandler( - const Handlers::StartMessageHandler &handler) { - UPB_ASSERT(!handler.registered_); - handler.registered_ = true; - handler.AddCleanup(this); - return upb_handlers_setstartmsg(this, handler.handler_, &handler.attr_); -} -inline bool Handlers::SetEndMessageHandler( - const Handlers::EndMessageHandler &handler) { - UPB_ASSERT(!handler.registered_); - handler.registered_ = true; - handler.AddCleanup(this); - return upb_handlers_setendmsg(this, handler.handler_, &handler.attr_); -} -inline bool Handlers::SetStartStringHandler(const FieldDef *f, - const StartStringHandler &handler) { - UPB_ASSERT(!handler.registered_); - handler.registered_ = true; - handler.AddCleanup(this); - return upb_handlers_setstartstr(this, f, handler.handler_, &handler.attr_); -} -inline bool Handlers::SetEndStringHandler(const FieldDef *f, - const EndFieldHandler &handler) { - UPB_ASSERT(!handler.registered_); - handler.registered_ = true; - handler.AddCleanup(this); - return upb_handlers_setendstr(this, f, handler.handler_, &handler.attr_); -} -inline bool Handlers::SetStringHandler(const FieldDef *f, - const StringHandler& handler) { - UPB_ASSERT(!handler.registered_); - handler.registered_ = true; - handler.AddCleanup(this); - return upb_handlers_setstring(this, f, handler.handler_, &handler.attr_); -} -inline bool Handlers::SetStartSequenceHandler( - const FieldDef *f, const StartFieldHandler &handler) { - UPB_ASSERT(!handler.registered_); - handler.registered_ = true; - handler.AddCleanup(this); - return upb_handlers_setstartseq(this, f, handler.handler_, &handler.attr_); -} -inline bool Handlers::SetStartSubMessageHandler( - const FieldDef *f, const StartFieldHandler &handler) { - UPB_ASSERT(!handler.registered_); - handler.registered_ = true; - handler.AddCleanup(this); - return upb_handlers_setstartsubmsg(this, f, handler.handler_, &handler.attr_); -} -inline bool Handlers::SetEndSubMessageHandler(const FieldDef *f, - const EndFieldHandler &handler) { - UPB_ASSERT(!handler.registered_); - handler.registered_ = true; - handler.AddCleanup(this); - return upb_handlers_setendsubmsg(this, f, handler.handler_, &handler.attr_); -} -inline bool Handlers::SetEndSequenceHandler(const FieldDef *f, - const EndFieldHandler &handler) { - UPB_ASSERT(!handler.registered_); - handler.registered_ = true; - handler.AddCleanup(this); - return upb_handlers_setendseq(this, f, handler.handler_, &handler.attr_); -} -inline const Handlers *Handlers::GetSubHandlers(const FieldDef *f) const { - return upb_handlers_getsubhandlers(this, f); -} -inline const Handlers *Handlers::GetSubHandlers(Handlers::Selector sel) const { - return upb_handlers_getsubhandlers_sel(this, sel); -} -inline bool Handlers::GetSelector(const FieldDef *f, Handlers::Type type, - Handlers::Selector *s) { - return upb_handlers_getselector(f, type, s); -} -inline Handlers::Selector Handlers::GetEndSelector(Handlers::Selector start) { - return upb_handlers_getendselector(start); -} -inline Handlers::GenericFunction *Handlers::GetHandler( - Handlers::Selector selector) { - return upb_handlers_gethandler(this, selector); -} -inline const void *Handlers::GetHandlerData(Handlers::Selector selector) { - return upb_handlers_gethandlerdata(this, selector); -} - -inline HandlerCache *HandlerCache::New(upb_handlers_callback *callback, - const void *closure) { - return upb_handlercache_new(callback, closure); -} -inline void HandlerCache::Free(HandlerCache* cache) { - return upb_handlercache_free(cache); -} -const Handlers* HandlerCache::Get(const MessageDef* md) { - return upb_handlercache_get(this, md); -} - -inline BytesHandler::BytesHandler() { - upb_byteshandler_init(this); +inline void Handler::AddCleanup(upb_handlers* h) const { + UPB_ASSERT(!registered_); + registered_ = true; + if (cleanup_func_) { + bool ok = upb_handlers_addcleanup(h, cleanup_data_, cleanup_func_); + UPB_ASSERT(ok); + } } -inline BytesHandler::~BytesHandler() {} - } /* namespace upb */ #endif /* __cplusplus */ diff --git a/upb/handlers.c b/upb/handlers.c index 90fb7b8..fd81b03 100644 --- a/upb/handlers.c +++ b/upb/handlers.c @@ -9,6 +9,15 @@ #include "upb/sink.h" + +struct upb_handlers { + upb_handlercache *cache; + const upb_msgdef *msg; + const upb_handlers **sub; + const void *top_closure_type; + upb_handlers_tabent table[1]; /* Dynamically-sized field handler array. */ +}; + static void *upb_calloc(upb_arena *arena, size_t size) { void *mem = upb_malloc(upb_arena_alloc(arena), size); if (mem) { @@ -50,13 +59,13 @@ static upb_selector_t handlers_getsel(upb_handlers *h, const upb_fielddef *f, static const void **returntype(upb_handlers *h, const upb_fielddef *f, upb_handlertype_t type) { - return &h->table[handlers_getsel(h, f, type)].attr.return_closure_type_; + return &h->table[handlers_getsel(h, f, type)].attr.return_closure_type; } static bool doset(upb_handlers *h, int32_t sel, const upb_fielddef *f, upb_handlertype_t type, upb_func *func, - upb_handlerattr *attr) { - upb_handlerattr set_attr = UPB_HANDLERATTR_INITIALIZER; + const upb_handlerattr *attr) { + upb_handlerattr set_attr = UPB_HANDLERATTR_INIT; const void *closure_type; const void **context_closure_type; @@ -68,7 +77,7 @@ static bool doset(upb_handlers *h, int32_t sel, const upb_fielddef *f, /* Check that the given closure type matches the closure type that has been * established for this context (if any). */ - closure_type = upb_handlerattr_closuretype(&set_attr); + closure_type = set_attr.closure_type; if (type == UPB_HANDLER_STRING) { context_closure_type = returntype(h, f, UPB_HANDLER_STARTSTR); @@ -91,15 +100,15 @@ static bool doset(upb_handlers *h, int32_t sel, const upb_fielddef *f, /* If this is a STARTSEQ or STARTSTR handler, check that the returned pointer * matches any pre-existing expectations about what type is expected. */ if (type == UPB_HANDLER_STARTSEQ || type == UPB_HANDLER_STARTSTR) { - const void *return_type = upb_handlerattr_returnclosuretype(&set_attr); - const void *table_return_type = - upb_handlerattr_returnclosuretype(&h->table[sel].attr); + const void *return_type = set_attr.return_closure_type; + const void *table_return_type = h->table[sel].attr.return_closure_type; if (return_type && table_return_type && return_type != table_return_type) { UPB_ASSERT(false); } - if (table_return_type && !return_type) - upb_handlerattr_setreturnclosuretype(&set_attr, table_return_type); + if (table_return_type && !return_type) { + set_attr.return_closure_type = table_return_type; + } } h->table[sel].func = (upb_func*)func; @@ -125,18 +134,18 @@ const void *effective_closure_type(upb_handlers *h, const upb_fielddef *f, type != UPB_HANDLER_STARTSEQ && type != UPB_HANDLER_ENDSEQ && h->table[sel = handlers_getsel(h, f, UPB_HANDLER_STARTSEQ)].func) { - ret = upb_handlerattr_returnclosuretype(&h->table[sel].attr); + ret = h->table[sel].attr.return_closure_type; } if (type == UPB_HANDLER_STRING && h->table[sel = handlers_getsel(h, f, UPB_HANDLER_STARTSTR)].func) { - ret = upb_handlerattr_returnclosuretype(&h->table[sel].attr); + ret = h->table[sel].attr.return_closure_type; } /* The effective type of the submessage; not used yet. * if (type == SUBMESSAGE && * h->table[sel = handlers_getsel(h, f, UPB_HANDLER_STARTSUBMSG)].func) { - * ret = upb_handlerattr_returnclosuretype(&h->table[sel].attr); + * ret = h->table[sel].attr.return_closure_type; * } */ return ret; @@ -156,7 +165,7 @@ bool checkstart(upb_handlers *h, const upb_fielddef *f, upb_handlertype_t type, if (h->table[sel].func) return true; closure_type = effective_closure_type(h, f, type); attr = &h->table[sel].attr; - return_closure_type = upb_handlerattr_returnclosuretype(attr); + return_closure_type = attr->return_closure_type; if (closure_type && return_closure_type && closure_type != return_closure_type) { UPB_ASSERT(false); @@ -164,12 +173,14 @@ bool checkstart(upb_handlers *h, const upb_fielddef *f, upb_handlertype_t type, return true; } -static upb_handlers *upb_handlers_new(const upb_msgdef *md, upb_handlercache *cache) { +static upb_handlers *upb_handlers_new(const upb_msgdef *md, + upb_handlercache *cache, + upb_arena *arena) { int extra; upb_handlers *h; extra = sizeof(upb_handlers_tabent) * (upb_msgdef_selectorcount(md) - 1); - h = upb_calloc(&cache->arena, sizeof(*h) + extra); + h = upb_calloc(arena, sizeof(*h) + extra); if (!h) return NULL; h->cache = cache; @@ -177,7 +188,7 @@ static upb_handlers *upb_handlers_new(const upb_msgdef *md, upb_handlercache *ca if (upb_msgdef_submsgfieldcount(md) > 0) { size_t bytes = upb_msgdef_submsgfieldcount(md) * sizeof(*h->sub); - h->sub = upb_calloc(&cache->arena, bytes); + h->sub = upb_calloc(arena, bytes); if (!h->sub) return NULL; } else { h->sub = 0; @@ -187,14 +198,14 @@ static upb_handlers *upb_handlers_new(const upb_msgdef *md, upb_handlercache *ca return h; } - /* Public interface ***********************************************************/ -#define SETTER(name, handlerctype, handlertype) \ - bool upb_handlers_set ## name(upb_handlers *h, const upb_fielddef *f, \ - handlerctype func, upb_handlerattr *attr) { \ - int32_t sel = trygetsel(h, f, handlertype); \ - return doset(h, sel, f, handlertype, (upb_func*)func, attr); \ +#define SETTER(name, handlerctype, handlertype) \ + bool upb_handlers_set##name(upb_handlers *h, const upb_fielddef *f, \ + handlerctype func, \ + const upb_handlerattr *attr) { \ + int32_t sel = trygetsel(h, f, handlertype); \ + return doset(h, sel, f, handlertype, (upb_func *)func, attr); \ } SETTER(int32, upb_int32_handlerfunc*, UPB_HANDLER_INT32) @@ -215,19 +226,19 @@ SETTER(endseq, upb_endfield_handlerfunc*, UPB_HANDLER_ENDSEQ) #undef SETTER bool upb_handlers_setunknown(upb_handlers *h, upb_unknown_handlerfunc *func, - upb_handlerattr *attr) { + const upb_handlerattr *attr) { return doset(h, UPB_UNKNOWN_SELECTOR, NULL, UPB_HANDLER_INT32, (upb_func *)func, attr); } bool upb_handlers_setstartmsg(upb_handlers *h, upb_startmsg_handlerfunc *func, - upb_handlerattr *attr) { + const upb_handlerattr *attr) { return doset(h, UPB_STARTMSG_SELECTOR, NULL, UPB_HANDLER_INT32, (upb_func *)func, attr); } bool upb_handlers_setendmsg(upb_handlers *h, upb_endmsg_handlerfunc *func, - upb_handlerattr *attr) { + const upb_handlerattr *attr) { return doset(h, UPB_ENDMSG_SELECTOR, NULL, UPB_HANDLER_INT32, (upb_func *)func, attr); } @@ -250,9 +261,18 @@ const upb_handlers *upb_handlers_getsubhandlers(const upb_handlers *h, return SUBH_F(h, f); } +upb_func *upb_handlers_gethandler(const upb_handlers *h, upb_selector_t s, + const void **handler_data) { + upb_func *ret = (upb_func *)h->table[s].func; + if (ret && handler_data) { + *handler_data = h->table[s].attr.handler_data; + } + return ret; +} + bool upb_handlers_getattr(const upb_handlers *h, upb_selector_t sel, upb_handlerattr *attr) { - if (!upb_handlers_gethandler(h, sel)) + if (!upb_handlers_gethandler(h, sel, NULL)) return false; *attr = h->table[sel].attr; return true; @@ -266,16 +286,6 @@ const upb_handlers *upb_handlers_getsubhandlers_sel(const upb_handlers *h, const upb_msgdef *upb_handlers_msgdef(const upb_handlers *h) { return h->msg; } -bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *func) { - bool ok; - if (upb_inttable_lookupptr(&h->cache->cleanup_, p, NULL)) { - return false; - } - ok = upb_inttable_insertptr(&h->cache->cleanup_, p, upb_value_fptr(func)); - UPB_ASSERT(ok); - return true; -} - upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f) { switch (upb_fielddef_type(f)) { case UPB_TYPE_INT32: @@ -375,6 +385,14 @@ uint32_t upb_handlers_selectorcount(const upb_fielddef *f) { /* upb_handlercache ***********************************************************/ +struct upb_handlercache { + upb_arena arena; + upb_inttable tab; /* maps upb_msgdef* -> upb_handlers*. */ + upb_inttable cleanup_; + upb_handlers_callback *callback; + const void *closure; +}; + const upb_handlers *upb_handlercache_get(upb_handlercache *c, const upb_msgdef *md) { upb_msg_field_iter i; @@ -385,7 +403,7 @@ const upb_handlers *upb_handlercache_get(upb_handlercache *c, return upb_value_getptr(v); } - h = upb_handlers_new(md, c); + h = upb_handlers_new(md, c, &c->arena); v = upb_value_ptr(h); if (!h) return NULL; @@ -452,90 +470,39 @@ void upb_handlercache_free(upb_handlercache *cache) { upb_gfree(cache); } - -/* upb_handlerattr ************************************************************/ - -void upb_handlerattr_init(upb_handlerattr *attr) { - upb_handlerattr from = UPB_HANDLERATTR_INITIALIZER; - memcpy(attr, &from, sizeof(*attr)); -} - -void upb_handlerattr_uninit(upb_handlerattr *attr) { - UPB_UNUSED(attr); -} - -bool upb_handlerattr_sethandlerdata(upb_handlerattr *attr, const void *hd) { - attr->handler_data_ = hd; - return true; -} - -bool upb_handlerattr_setclosuretype(upb_handlerattr *attr, const void *type) { - attr->closure_type_ = type; - return true; -} - -const void *upb_handlerattr_closuretype(const upb_handlerattr *attr) { - return attr->closure_type_; -} - -bool upb_handlerattr_setreturnclosuretype(upb_handlerattr *attr, - const void *type) { - attr->return_closure_type_ = type; - return true; -} - -const void *upb_handlerattr_returnclosuretype(const upb_handlerattr *attr) { - return attr->return_closure_type_; -} - -bool upb_handlerattr_setalwaysok(upb_handlerattr *attr, bool alwaysok) { - attr->alwaysok_ = alwaysok; +bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *func) { + bool ok; + if (upb_inttable_lookupptr(&h->cache->cleanup_, p, NULL)) { + return false; + } + ok = upb_inttable_insertptr(&h->cache->cleanup_, p, upb_value_fptr(func)); + UPB_ASSERT(ok); return true; } -bool upb_handlerattr_alwaysok(const upb_handlerattr *attr) { - return attr->alwaysok_; -} - -/* upb_bufhandle **************************************************************/ - -size_t upb_bufhandle_objofs(const upb_bufhandle *h) { - return h->objofs_; -} - /* upb_byteshandler ***********************************************************/ -void upb_byteshandler_init(upb_byteshandler* h) { - memset(h, 0, sizeof(*h)); -} - -/* For when we support handlerfree callbacks. */ -void upb_byteshandler_uninit(upb_byteshandler* h) { - UPB_UNUSED(h); -} - bool upb_byteshandler_setstartstr(upb_byteshandler *h, upb_startstr_handlerfunc *func, void *d) { h->table[UPB_STARTSTR_SELECTOR].func = (upb_func*)func; - h->table[UPB_STARTSTR_SELECTOR].attr.handler_data_ = d; + h->table[UPB_STARTSTR_SELECTOR].attr.handler_data = d; return true; } bool upb_byteshandler_setstring(upb_byteshandler *h, upb_string_handlerfunc *func, void *d) { h->table[UPB_STRING_SELECTOR].func = (upb_func*)func; - h->table[UPB_STRING_SELECTOR].attr.handler_data_ = d; + h->table[UPB_STRING_SELECTOR].attr.handler_data = d; return true; } bool upb_byteshandler_setendstr(upb_byteshandler *h, upb_endfield_handlerfunc *func, void *d) { h->table[UPB_ENDSTR_SELECTOR].func = (upb_func*)func; - h->table[UPB_ENDSTR_SELECTOR].attr.handler_data_ = d; + h->table[UPB_ENDSTR_SELECTOR].attr.handler_data = d; return true; } - /** Handlers for upb_msg ******************************************************/ typedef struct { @@ -564,7 +531,7 @@ MSG_WRITER(bool, bool) bool upb_msg_setscalarhandler(upb_handlers *h, const upb_fielddef *f, size_t offset, int32_t hasbit) { - upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER; + upb_handlerattr attr = UPB_HANDLERATTR_INIT; bool ok; upb_msg_handlerdata *d = upb_gmalloc(sizeof(*d)); @@ -572,8 +539,8 @@ bool upb_msg_setscalarhandler(upb_handlers *h, const upb_fielddef *f, d->offset = offset; d->hasbit = hasbit; - upb_handlerattr_sethandlerdata(&attr, d); - upb_handlerattr_setalwaysok(&attr, true); + attr.handler_data = d; + attr.alwaysok = true; upb_handlers_addcleanup(h, d, upb_gfree); #define TYPE(u, l) \ @@ -595,7 +562,6 @@ bool upb_msg_setscalarhandler(upb_handlers *h, const upb_fielddef *f, } #undef TYPE - upb_handlerattr_uninit(&attr); return ok; } @@ -605,7 +571,8 @@ bool upb_msg_getscalarhandlerdata(const upb_handlers *h, size_t *offset, int32_t *hasbit) { const upb_msg_handlerdata *d; - upb_func *f = upb_handlers_gethandler(h, s); + const void *p; + upb_func *f = upb_handlers_gethandler(h, s, &p); if ((upb_int64_handlerfunc*)f == upb_msg_setint64) { *type = UPB_TYPE_INT64; @@ -625,7 +592,7 @@ bool upb_msg_getscalarhandlerdata(const upb_handlers *h, return false; } - d = upb_handlers_gethandlerdata(h, s); + d = p; *offset = d->offset; *hasbit = d->hasbit; return true; diff --git a/upb/handlers.h b/upb/handlers.h index 741bd48..4558786 100644 --- a/upb/handlers.h +++ b/upb/handlers.h @@ -24,21 +24,13 @@ #ifdef __cplusplus namespace upb { -class BufferHandle; -class BytesHandler; -class HandlerAttributes; -class Handlers; +class HandlersPtr; class HandlerCache; template class Handler; template struct CanonicalType; } /* namespace upb */ #endif -UPB_DECLARE_TYPE(upb::BufferHandle, upb_bufhandle) -UPB_DECLARE_TYPE(upb::BytesHandler, upb_byteshandler) -UPB_DECLARE_TYPE(upb::HandlerAttributes, upb_handlerattr) -UPB_DECLARE_TYPE(upb::Handlers, upb_handlers) -UPB_DECLARE_TYPE(upb::HandlerCache, upb_handlercache) /* The maximum depth that the handler graph can have. This is a resource limit * for the C stack since we sometimes need to recursively traverse the graph. @@ -80,28 +72,6 @@ extern char _upb_noclosure; * (for example: the STARTSUBMSG handler for field "field15"). */ typedef int32_t upb_selector_t; -UPB_BEGIN_EXTERN_C - -/* Forward-declares for C inline accessors. We need to declare these here - * so we can "friend" them in the class declarations in C++. */ -UPB_INLINE upb_func *upb_handlers_gethandler(const upb_handlers *h, - upb_selector_t s); -UPB_INLINE const void *upb_handlerattr_handlerdata(const upb_handlerattr *attr); -UPB_INLINE const void *upb_handlers_gethandlerdata(const upb_handlers *h, - upb_selector_t s); - -UPB_INLINE void upb_bufhandle_init(upb_bufhandle *h); -UPB_INLINE void upb_bufhandle_setobj(upb_bufhandle *h, const void *obj, - const void *type); -UPB_INLINE void upb_bufhandle_setbuf(upb_bufhandle *h, const char *buf, - size_t ofs); -UPB_INLINE const void *upb_bufhandle_obj(const upb_bufhandle *h); -UPB_INLINE const void *upb_bufhandle_objtype(const upb_bufhandle *h); -UPB_INLINE const char *upb_bufhandle_buf(const upb_bufhandle *h); - -UPB_END_EXTERN_C - - /* Static selectors for upb::Handlers. */ #define UPB_STARTMSG_SELECTOR 0 #define UPB_ENDMSG_SELECTOR 1 @@ -113,126 +83,234 @@ UPB_END_EXTERN_C #define UPB_STRING_SELECTOR 1 #define UPB_ENDSTR_SELECTOR 2 -typedef void upb_handlerfree(void *d); - #ifdef __cplusplus - -/* A set of attributes that accompanies a handler's function pointer. */ -class upb::HandlerAttributes { - public: - HandlerAttributes(); - ~HandlerAttributes(); - - /* Sets the handler data that will be passed as the second parameter of the - * handler. To free this pointer when the handlers are freed, call - * Handlers::AddCleanup(). */ - bool SetHandlerData(const void *handler_data); - const void* handler_data() const; - - /* Use this to specify the type of the closure. This will be checked against - * all other closure types for handler that use the same closure. - * Registration will fail if this does not match all other non-NULL closure - * types. */ - bool SetClosureType(const void *closure_type); - const void* closure_type() const; - - /* Use this to specify the type of the returned closure. Only used for - * Start*{String,SubMessage,Sequence} handlers. This must match the closure - * type of any handlers that use it (for example, the StringBuf handler must - * match the closure returned from StartString). */ - bool SetReturnClosureType(const void *return_closure_type); - const void* return_closure_type() const; - - /* Set to indicate that the handler always returns "ok" (either "true" or a - * non-NULL closure). This is a hint that can allow code generators to - * generate more efficient code. */ - bool SetAlwaysOk(bool always_ok); - bool always_ok() const; - - private: - friend UPB_INLINE const void * ::upb_handlerattr_handlerdata( - const upb_handlerattr *attr); -#else -struct upb_handlerattr { +template const void *UniquePtrForType() { + static const char ch = 0; + return &ch; +} #endif - const void *handler_data_; - const void *closure_type_; - const void *return_closure_type_; - bool alwaysok_; -}; -#define UPB_HANDLERATTR_INITIALIZER {NULL, NULL, NULL, false} +/* upb_handlers ************************************************************/ +/* Handler attributes, to be registered with the handler itself. */ typedef struct { - upb_func *func; + const void *handler_data; + const void *closure_type; + const void *return_closure_type; + bool alwaysok; +} upb_handlerattr; - /* It is wasteful to include the entire attributes here: - * - * * Some of the information is redundant (like storing the closure type - * separately for each handler that must match). - * * Some of the info is only needed prior to freeze() (like closure types). - * * alignment padding wastes a lot of space for alwaysok_. - * - * If/when the size and locality of handlers is an issue, we can optimize this - * not to store the entire attr like this. We do not expose the table's - * layout to allow this optimization in the future. */ - upb_handlerattr attr; -} upb_handlers_tabent; - -#ifdef __cplusplus - -/* Extra information about a buffer that is passed to a StringBuf handler. - * TODO(haberman): allow the handle to be pinned so that it will outlive - * the handler invocation. */ -class upb::BufferHandle { - public: - BufferHandle(); - ~BufferHandle(); +#define UPB_HANDLERATTR_INIT {NULL, NULL, NULL, false} +/* Bufhandle, data passed along with a buffer to indicate its provenance. */ +typedef struct { /* The beginning of the buffer. This may be different than the pointer * passed to a StringBuf handler because the handler may receive data * that is from the middle or end of a larger buffer. */ - const char* buffer() const; + const char *buf; /* The offset within the attached object where this buffer begins. Only * meaningful if there is an attached object. */ - size_t object_offset() const; + size_t objofs; - /* Note that object_offset is the offset of "buf" within the attached - * object. */ - void SetBuffer(const char* buf, size_t object_offset); + /* The attached object (if any) and a pointer representing its type. */ + const void *obj; + const void *objtype; - /* The BufferHandle can have an "attached object", which can be used to - * tunnel through a pointer to the buffer's underlying representation. */ +#ifdef __cplusplus template - void SetAttachedObject(const T* obj); + void SetAttachedObject(const T* _obj) { + obj = _obj; + objtype = UniquePtrForType(); + } - /* Returns NULL if the attached object is not of this type. */ template - const T* GetAttachedObject() const; - - private: - friend UPB_INLINE void ::upb_bufhandle_init(upb_bufhandle *h); - friend UPB_INLINE void ::upb_bufhandle_setobj(upb_bufhandle *h, - const void *obj, - const void *type); - friend UPB_INLINE void ::upb_bufhandle_setbuf(upb_bufhandle *h, - const char *buf, size_t ofs); - friend UPB_INLINE const void* ::upb_bufhandle_obj(const upb_bufhandle *h); - friend UPB_INLINE const void* ::upb_bufhandle_objtype( - const upb_bufhandle *h); - friend UPB_INLINE const char* ::upb_bufhandle_buf(const upb_bufhandle *h); -#else -struct upb_bufhandle { + const T *GetAttachedObject() const { + return objtype == UniquePtrForType() ? static_cast(obj) + : NULL; + } #endif - const char *buf_; - const void *obj_; - const void *objtype_; - size_t objofs_; -}; +} upb_bufhandle; + +#define UPB_BUFHANDLE_INIT {NULL, 0, NULL, NULL} + +/* Handler function typedefs. */ +typedef void upb_handlerfree(void *d); +typedef bool upb_unknown_handlerfunc(void *c, const void *hd, const char *buf, + size_t n); +typedef bool upb_startmsg_handlerfunc(void *c, const void*); +typedef bool upb_endmsg_handlerfunc(void *c, const void *, upb_status *status); +typedef void* upb_startfield_handlerfunc(void *c, const void *hd); +typedef bool upb_endfield_handlerfunc(void *c, const void *hd); +typedef bool upb_int32_handlerfunc(void *c, const void *hd, int32_t val); +typedef bool upb_int64_handlerfunc(void *c, const void *hd, int64_t val); +typedef bool upb_uint32_handlerfunc(void *c, const void *hd, uint32_t val); +typedef bool upb_uint64_handlerfunc(void *c, const void *hd, uint64_t val); +typedef bool upb_float_handlerfunc(void *c, const void *hd, float val); +typedef bool upb_double_handlerfunc(void *c, const void *hd, double val); +typedef bool upb_bool_handlerfunc(void *c, const void *hd, bool val); +typedef void *upb_startstr_handlerfunc(void *c, const void *hd, + size_t size_hint); +typedef size_t upb_string_handlerfunc(void *c, const void *hd, const char *buf, + size_t n, const upb_bufhandle* handle); + +struct upb_handlers; +typedef struct upb_handlers upb_handlers; + +UPB_BEGIN_EXTERN_C + +/* Mutating accessors. */ +const upb_status *upb_handlers_status(upb_handlers *h); +void upb_handlers_clearerr(upb_handlers *h); +const upb_msgdef *upb_handlers_msgdef(const upb_handlers *h); +bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *hfree); +bool upb_handlers_setunknown(upb_handlers *h, upb_unknown_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setstartmsg(upb_handlers *h, upb_startmsg_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setendmsg(upb_handlers *h, upb_endmsg_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setint32(upb_handlers *h, const upb_fielddef *f, + upb_int32_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setint64(upb_handlers *h, const upb_fielddef *f, + upb_int64_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setuint32(upb_handlers *h, const upb_fielddef *f, + upb_uint32_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setuint64(upb_handlers *h, const upb_fielddef *f, + upb_uint64_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setfloat(upb_handlers *h, const upb_fielddef *f, + upb_float_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setdouble(upb_handlers *h, const upb_fielddef *f, + upb_double_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setbool(upb_handlers *h, const upb_fielddef *f, + upb_bool_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setstartstr(upb_handlers *h, const upb_fielddef *f, + upb_startstr_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setstring(upb_handlers *h, const upb_fielddef *f, + upb_string_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setendstr(upb_handlers *h, const upb_fielddef *f, + upb_endfield_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setstartseq(upb_handlers *h, const upb_fielddef *f, + upb_startfield_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setstartsubmsg(upb_handlers *h, const upb_fielddef *f, + upb_startfield_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setendsubmsg(upb_handlers *h, const upb_fielddef *f, + upb_endfield_handlerfunc *func, + const upb_handlerattr *attr); +bool upb_handlers_setendseq(upb_handlers *h, const upb_fielddef *f, + upb_endfield_handlerfunc *func, + const upb_handlerattr *attr); + +/* Read-only accessors. */ +const upb_handlers *upb_handlers_getsubhandlers(const upb_handlers *h, + const upb_fielddef *f); +const upb_handlers *upb_handlers_getsubhandlers_sel(const upb_handlers *h, + upb_selector_t sel); +upb_func *upb_handlers_gethandler(const upb_handlers *h, upb_selector_t s, + const void **handler_data); +bool upb_handlers_getattr(const upb_handlers *h, upb_selector_t s, + upb_handlerattr *attr); + +/* "Static" methods */ +upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f); +bool upb_handlers_getselector(const upb_fielddef *f, upb_handlertype_t type, + upb_selector_t *s); +UPB_INLINE upb_selector_t upb_handlers_getendselector(upb_selector_t start) { + return start + 1; +} + +/* Internal-only. */ +uint32_t upb_handlers_selectorbaseoffset(const upb_fielddef *f); +uint32_t upb_handlers_selectorcount(const upb_fielddef *f); + +UPB_END_EXTERN_C #ifdef __cplusplus +namespace upb { +typedef upb_handlers Handlers; +} + +/* Convenience macros for creating a Handler object that is wrapped with a + * type-safe wrapper function that converts the "void*" parameters/returns + * of the underlying C API into nice C++ function. + * + * Sample usage: + * void OnValue1(MyClosure* c, const MyHandlerData* d, int32_t val) { + * // do stuff ... + * } + * + * // Handler that doesn't need any data bound to it. + * void OnValue2(MyClosure* c, int32_t val) { + * // do stuff ... + * } + * + * // Handler that returns bool so it can return failure if necessary. + * bool OnValue3(MyClosure* c, int32_t val) { + * // do stuff ... + * return ok; + * } + * + * // Member function handler. + * class MyClosure { + * public: + * void OnValue(int32_t val) { + * // do stuff ... + * } + * }; + * + * // Takes ownership of the MyHandlerData. + * handlers->SetInt32Handler(f1, UpbBind(OnValue1, new MyHandlerData(...))); + * handlers->SetInt32Handler(f2, UpbMakeHandler(OnValue2)); + * handlers->SetInt32Handler(f1, UpbMakeHandler(OnValue3)); + * handlers->SetInt32Handler(f2, UpbMakeHandler(&MyClosure::OnValue)); + */ + +/* In C++11, the "template" disambiguator can appear even outside templates, + * so all calls can safely use this pair of macros. */ + +#define UpbMakeHandler(f) upb::MatchFunc(f).template GetFunc() + +/* We have to be careful to only evaluate "d" once. */ +#define UpbBind(f, d) upb::MatchFunc(f).template GetFunc((d)) + +/* Handler: a struct that contains the (handler, data, deleter) tuple that is + * used to register all handlers. Users can Make() these directly but it's + * more convenient to use the UpbMakeHandler/UpbBind macros above. */ +template class upb::Handler { + public: + /* The underlying, handler function signature that upb uses internally. */ + typedef T FuncPtr; + + /* Intentionally implicit. */ + template Handler(F func); + ~Handler() { UPB_ASSERT(registered_); } + + void AddCleanup(upb_handlers* h) const; + FuncPtr handler() const { return handler_; } + const upb_handlerattr& attr() const { return attr_; } + + private: + UPB_DISALLOW_COPY_AND_ASSIGN(Handler) + FuncPtr handler_; + mutable upb_handlerattr attr_; + mutable bool registered_; + void *cleanup_data_; + upb_handlerfree *cleanup_func_; +}; + /* A upb::Handlers object represents the set of handlers associated with a * message in the graph of messages. You can think of it as a big virtual * table with functions corresponding to all the events that can fire while @@ -244,18 +322,23 @@ struct upb_bufhandle { * * The easiest way to create the *Handler objects needed by the Set* methods is * with the UpbBind() and UpbMakeHandler() macros; see below. */ -class upb::Handlers { +class upb::HandlersPtr { public: + HandlersPtr(upb_handlers* ptr) : ptr_(ptr) {} + + upb_handlers* ptr() const { return ptr_; } + typedef upb_selector_t Selector; typedef upb_handlertype_t Type; typedef Handler StartFieldHandler; typedef Handler EndFieldHandler; typedef Handler StartMessageHandler; - typedef Handler EndMessageHandler; + typedef Handler EndMessageHandler; typedef Handler StartStringHandler; typedef Handler StringHandler; + const upb_bufhandle *)> + StringHandler; template struct ValueHandler { typedef Handler H; @@ -275,21 +358,17 @@ class upb::Handlers { typedef void HandlersCallback(const void *closure, upb_handlers *h); - /* All handler registration functions return bool to indicate success or - * failure; details about failures are stored in this status object. If a - * failure does occur, it must be cleared before the Handlers are frozen, - * otherwise the freeze() operation will fail. The functions may *only* be - * used while the Handlers are mutable. */ - const Status* status(); - void ClearError(); - /* Returns the msgdef associated with this handlers object. */ - const MessageDef* message_def() const; + MessageDefPtr message_def() const { + return MessageDefPtr(upb_handlers_msgdef(ptr())); + } /* Adds the given pointer and function to the list of cleanup functions that * will be run when these handlers are freed. If this pointer has previously * been registered, the function returns false and does nothing. */ - bool AddCleanup(void *ptr, upb_handlerfree *cleanup); + bool AddCleanup(void *ptr, upb_handlerfree *cleanup) { + return upb_handlers_addcleanup(ptr_, ptr, cleanup); + } /* Sets the startmsg handler for the message, which is defined as follows: * @@ -299,7 +378,10 @@ class upb::Handlers { * return true; * } */ - bool SetStartMessageHandler(const StartMessageHandler& handler); + bool SetStartMessageHandler(const StartMessageHandler &h) { + h.AddCleanup(ptr()); + return upb_handlers_setstartmsg(ptr(), h.handler(), &h.attr()); + } /* Sets the endmsg handler for the message, which is defined as follows: * @@ -309,7 +391,10 @@ class upb::Handlers { * // can also be modified in-place to update the final status. * } */ - bool SetEndMessageHandler(const EndMessageHandler& handler); + bool SetEndMessageHandler(const EndMessageHandler& h) { + h.AddCleanup(ptr()); + return upb_handlers_setendmsg(ptr(), h.handler(), &h.attr()); + } /* Sets the value handler for the given field, which is defined as follows * (this is for an int32 field; other field types will pass their native @@ -331,13 +416,40 @@ class upb::Handlers { * Returns false if the handler failed to register; in this case the cleanup * handler (if any) will be called immediately. */ - bool SetInt32Handler (const FieldDef* f, const Int32Handler& h); - bool SetInt64Handler (const FieldDef* f, const Int64Handler& h); - bool SetUInt32Handler(const FieldDef* f, const UInt32Handler& h); - bool SetUInt64Handler(const FieldDef* f, const UInt64Handler& h); - bool SetFloatHandler (const FieldDef* f, const FloatHandler& h); - bool SetDoubleHandler(const FieldDef* f, const DoubleHandler& h); - bool SetBoolHandler (const FieldDef* f, const BoolHandler& h); + bool SetInt32Handler(FieldDefPtr f, const Int32Handler &h) { + h.AddCleanup(ptr()); + return upb_handlers_setint32(ptr(), f.ptr(), h.handler(), &h.attr()); + } + + bool SetInt64Handler (FieldDefPtr f, const Int64Handler& h) { + h.AddCleanup(ptr()); + return upb_handlers_setint64(ptr(), f.ptr(), h.handler(), &h.attr()); + } + + bool SetUInt32Handler(FieldDefPtr f, const UInt32Handler& h) { + h.AddCleanup(ptr()); + return upb_handlers_setuint32(ptr(), f.ptr(), h.handler(), &h.attr()); + } + + bool SetUInt64Handler(FieldDefPtr f, const UInt64Handler& h) { + h.AddCleanup(ptr()); + return upb_handlers_setuint64(ptr(), f.ptr(), h.handler(), &h.attr()); + } + + bool SetFloatHandler (FieldDefPtr f, const FloatHandler& h) { + h.AddCleanup(ptr()); + return upb_handlers_setfloat(ptr(), f.ptr(), h.handler(), &h.attr()); + } + + bool SetDoubleHandler(FieldDefPtr f, const DoubleHandler& h) { + h.AddCleanup(ptr()); + return upb_handlers_setdouble(ptr(), f.ptr(), h.handler(), &h.attr()); + } + + bool SetBoolHandler(FieldDefPtr f, const BoolHandler &h) { + h.AddCleanup(ptr()); + return upb_handlers_setbool(ptr(), f.ptr(), h.handler(), &h.attr()); + } /* Like the previous, but templated on the type on the value (ie. int32). * This is mostly useful to call from other templates. To call this you must @@ -345,8 +457,8 @@ class upb::Handlers { * h->SetValueHandler(f, UpbBind(MyHandler, MyData)); */ template bool SetValueHandler( - const FieldDef *f, - const typename ValueHandler::Type>::H& handler); + FieldDefPtr f, + const typename ValueHandler::Type>::H &handler); /* Sets handlers for a string field, which are defined as follows: * @@ -384,9 +496,20 @@ class upb::Handlers { * return true; * } */ - bool SetStartStringHandler(const FieldDef* f, const StartStringHandler& h); - bool SetStringHandler(const FieldDef* f, const StringHandler& h); - bool SetEndStringHandler(const FieldDef* f, const EndFieldHandler& h); + bool SetStartStringHandler(FieldDefPtr f, const StartStringHandler &h) { + h.AddCleanup(ptr()); + return upb_handlers_setstartstr(ptr(), f.ptr(), h.handler(), &h.attr()); + } + + bool SetStringHandler(FieldDefPtr f, const StringHandler& h) { + h.AddCleanup(ptr()); + return upb_handlers_setstring(ptr(), f.ptr(), h.handler(), &h.attr()); + } + + bool SetEndStringHandler(FieldDefPtr f, const EndFieldHandler& h) { + h.AddCleanup(ptr()); + return upb_handlers_setendstr(ptr(), f.ptr(), h.handler(), &h.attr()); + } /* Sets the startseq handler, which is defined as follows: * @@ -402,7 +525,10 @@ class upb::Handlers { * Returns "false" if "f" does not belong to this message or is not a * repeated field. */ - bool SetStartSequenceHandler(const FieldDef* f, const StartFieldHandler& h); + bool SetStartSequenceHandler(FieldDefPtr f, const StartFieldHandler &h) { + h.AddCleanup(ptr()); + return upb_handlers_setstartseq(ptr(), f.ptr(), h.handler(), &h.attr()); + } /* Sets the startsubmsg handler for the given field, which is defined as * follows: @@ -419,7 +545,10 @@ class upb::Handlers { * Returns "false" if "f" does not belong to this message or is not a * submessage/group field. */ - bool SetStartSubMessageHandler(const FieldDef* f, const StartFieldHandler& h); + bool SetStartSubMessageHandler(FieldDefPtr f, const StartFieldHandler& h) { + h.AddCleanup(ptr()); + return upb_handlers_setstartsubmsg(ptr(), f.ptr(), h.handler(), &h.attr()); + } /* Sets the endsubmsg handler for the given field, which is defined as * follows: @@ -432,7 +561,10 @@ class upb::Handlers { * Returns "false" if "f" does not belong to this message or is not a * submessage/group field. */ - bool SetEndSubMessageHandler(const FieldDef *f, const EndFieldHandler &h); + bool SetEndSubMessageHandler(FieldDefPtr f, const EndFieldHandler &h) { + h.AddCleanup(ptr()); + return upb_handlers_setendsubmsg(ptr(), f.ptr(), h.handler(), &h.attr()); + } /* Starts the endsubseq handler for the given field, which is defined as * follows: @@ -445,328 +577,93 @@ class upb::Handlers { * Returns "false" if "f" does not belong to this message or is not a * repeated field. */ - bool SetEndSequenceHandler(const FieldDef* f, const EndFieldHandler& h); - - /* Gets the object that specifies handlers for the given field, which - * must be a submessage or group. Returns NULL if no handlers are set. */ - const Handlers* GetSubHandlers(const FieldDef* f) const; - - /* Equivalent to GetSubHandlers, but takes the STARTSUBMSG selector for the - * field. */ - const Handlers* GetSubHandlers(Selector startsubmsg) const; - - /* A selector refers to a specific field handler in the Handlers object - * (for example: the STARTSUBMSG handler for field "field15"). - * On success, returns true and stores the selector in "s". - * If the FieldDef or Type are invalid, returns false. - * The returned selector is ONLY valid for Handlers whose MessageDef - * contains this FieldDef. */ - static bool GetSelector(const FieldDef* f, Type type, Selector* s); - - /* Given a START selector of any kind, returns the corresponding END selector. */ - static Selector GetEndSelector(Selector start_selector); - - /* Returns the function pointer for this handler. It is the client's - * responsibility to cast to the correct function type before calling it. */ - GenericFunction* GetHandler(Selector selector); - - /* Sets the given attributes to the attributes for this selector. */ - bool GetAttributes(Selector selector, HandlerAttributes* attr); - - /* Returns the handler data that was registered with this handler. */ - const void* GetHandlerData(Selector selector); - - /* Could add any of the following functions as-needed, with some minor - * implementation changes: - * - * const FieldDef* GetFieldDef(Selector selector); - * static bool IsSequence(Selector selector); */ + bool SetEndSequenceHandler(FieldDefPtr f, const EndFieldHandler &h) { + h.AddCleanup(ptr()); + return upb_handlers_setendseq(ptr(), f.ptr(), h.handler(), &h.attr()); + } private: - UPB_DISALLOW_POD_OPS(Handlers, upb::Handlers) - - friend UPB_INLINE GenericFunction *::upb_handlers_gethandler( - const upb_handlers *h, upb_selector_t s); - friend UPB_INLINE const void *::upb_handlers_gethandlerdata( - const upb_handlers *h, upb_selector_t s); -#else -struct upb_handlers { -#endif - upb_handlercache *cache; - const upb_msgdef *msg; - const upb_handlers **sub; - const void *top_closure_type; - upb_handlers_tabent table[1]; /* Dynamically-sized field handler array. */ + upb_handlers* ptr_; }; -#ifdef __cplusplus - -namespace upb { - -/* Convenience macros for creating a Handler object that is wrapped with a - * type-safe wrapper function that converts the "void*" parameters/returns - * of the underlying C API into nice C++ function. - * - * Sample usage: - * void OnValue1(MyClosure* c, const MyHandlerData* d, int32_t val) { - * // do stuff ... - * } - * - * // Handler that doesn't need any data bound to it. - * void OnValue2(MyClosure* c, int32_t val) { - * // do stuff ... - * } - * - * // Handler that returns bool so it can return failure if necessary. - * bool OnValue3(MyClosure* c, int32_t val) { - * // do stuff ... - * return ok; - * } - * - * // Member function handler. - * class MyClosure { - * public: - * void OnValue(int32_t val) { - * // do stuff ... - * } - * }; - * - * // Takes ownership of the MyHandlerData. - * handlers->SetInt32Handler(f1, UpbBind(OnValue1, new MyHandlerData(...))); - * handlers->SetInt32Handler(f2, UpbMakeHandler(OnValue2)); - * handlers->SetInt32Handler(f1, UpbMakeHandler(OnValue3)); - * handlers->SetInt32Handler(f2, UpbMakeHandler(&MyClosure::OnValue)); - */ - -#ifdef UPB_CXX11 - -/* In C++11, the "template" disambiguator can appear even outside templates, - * so all calls can safely use this pair of macros. */ - -#define UpbMakeHandler(f) upb::MatchFunc(f).template GetFunc() - -/* We have to be careful to only evaluate "d" once. */ -#define UpbBind(f, d) upb::MatchFunc(f).template GetFunc((d)) +#endif /* __cplusplus */ -#else +/* upb_handlercache ***********************************************************/ -/* Prior to C++11, the "template" disambiguator may only appear inside a - * template, so the regular macro must not use "template" */ +UPB_BEGIN_EXTERN_C -#define UpbMakeHandler(f) upb::MatchFunc(f).GetFunc() +struct upb_handlercache; +typedef struct upb_handlercache upb_handlercache; -#define UpbBind(f, d) upb::MatchFunc(f).GetFunc((d)) +typedef void upb_handlers_callback(const void *closure, upb_handlers *h); -#endif /* UPB_CXX11 */ +upb_handlercache *upb_handlercache_new(upb_handlers_callback *callback, + const void *closure); +void upb_handlercache_free(upb_handlercache *cache); +const upb_handlers *upb_handlercache_get(upb_handlercache *cache, + const upb_msgdef *md); -/* This macro must be used in C++98 for calls from inside a template. But we - * define this variant in all cases; code that wants to be compatible with both - * C++98 and C++11 should always use this macro when calling from a template. */ -#define UpbMakeHandlerT(f) upb::MatchFunc(f).template GetFunc() +UPB_END_EXTERN_C -/* We have to be careful to only evaluate "d" once. */ -#define UpbBindT(f, d) upb::MatchFunc(f).template GetFunc((d)) +#ifdef __cplusplus -/* Handler: a struct that contains the (handler, data, deleter) tuple that is - * used to register all handlers. Users can Make() these directly but it's - * more convenient to use the UpbMakeHandler/UpbBind macros above. */ -template class Handler { +class upb::HandlerCache { public: - /* The underlying, handler function signature that upb uses internally. */ - typedef T FuncPtr; + HandlerCache(upb_handlers_callback *callback, const void *closure) + : ptr_(upb_handlercache_new(callback, closure), upb_handlercache_free) {} + HandlerCache(HandlerCache&&) = default; + HandlerCache& operator=(HandlerCache&&) = default; + HandlerCache(upb_handlercache* c) : ptr_(c, upb_handlercache_free) {} - /* Intentionally implicit. */ - template Handler(F func); - ~Handler(); + upb_handlercache* ptr() { return ptr_.get(); } - private: - void AddCleanup(Handlers* h) const { - if (cleanup_func_) { - bool ok = h->AddCleanup(cleanup_data_, cleanup_func_); - UPB_ASSERT(ok); - } + const upb_handlers *Get(MessageDefPtr md) { + return upb_handlercache_get(ptr_.get(), md.ptr()); } - UPB_DISALLOW_COPY_AND_ASSIGN(Handler) - friend class Handlers; - FuncPtr handler_; - mutable HandlerAttributes attr_; - mutable bool registered_; - void *cleanup_data_; - upb_handlerfree *cleanup_func_; + private: + std::unique_ptr ptr_; }; -} /* namespace upb */ - #endif /* __cplusplus */ -UPB_BEGIN_EXTERN_C - -/* Native C API. */ +/* upb_byteshandler ***********************************************************/ -/* Handler function typedefs. */ -typedef bool upb_unknown_handlerfunc(void *c, const void *hd, const char *buf, - size_t n); -typedef bool upb_startmsg_handlerfunc(void *c, const void*); -typedef bool upb_endmsg_handlerfunc(void *c, const void *, upb_status *status); -typedef void* upb_startfield_handlerfunc(void *c, const void *hd); -typedef bool upb_endfield_handlerfunc(void *c, const void *hd); -typedef bool upb_int32_handlerfunc(void *c, const void *hd, int32_t val); -typedef bool upb_int64_handlerfunc(void *c, const void *hd, int64_t val); -typedef bool upb_uint32_handlerfunc(void *c, const void *hd, uint32_t val); -typedef bool upb_uint64_handlerfunc(void *c, const void *hd, uint64_t val); -typedef bool upb_float_handlerfunc(void *c, const void *hd, float val); -typedef bool upb_double_handlerfunc(void *c, const void *hd, double val); -typedef bool upb_bool_handlerfunc(void *c, const void *hd, bool val); -typedef void *upb_startstr_handlerfunc(void *c, const void *hd, - size_t size_hint); -typedef size_t upb_string_handlerfunc(void *c, const void *hd, const char *buf, - size_t n, const upb_bufhandle* handle); - -/* upb_bufhandle */ -size_t upb_bufhandle_objofs(const upb_bufhandle *h); - -/* upb_handlerattr */ -void upb_handlerattr_init(upb_handlerattr *attr); -void upb_handlerattr_uninit(upb_handlerattr *attr); - -bool upb_handlerattr_sethandlerdata(upb_handlerattr *attr, const void *hd); -bool upb_handlerattr_setclosuretype(upb_handlerattr *attr, const void *type); -const void *upb_handlerattr_closuretype(const upb_handlerattr *attr); -bool upb_handlerattr_setreturnclosuretype(upb_handlerattr *attr, - const void *type); -const void *upb_handlerattr_returnclosuretype(const upb_handlerattr *attr); -bool upb_handlerattr_setalwaysok(upb_handlerattr *attr, bool alwaysok); -bool upb_handlerattr_alwaysok(const upb_handlerattr *attr); - -UPB_INLINE const void *upb_handlerattr_handlerdata( - const upb_handlerattr *attr) { - return attr->handler_data_; -} +UPB_BEGIN_EXTERN_C -/* upb_handlers */ -const upb_status *upb_handlers_status(upb_handlers *h); -void upb_handlers_clearerr(upb_handlers *h); -const upb_msgdef *upb_handlers_msgdef(const upb_handlers *h); -bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *hfree); -bool upb_handlers_setunknown(upb_handlers *h, upb_unknown_handlerfunc *func, - upb_handlerattr *attr); +typedef struct { + upb_func *func; -bool upb_handlers_setstartmsg(upb_handlers *h, upb_startmsg_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setendmsg(upb_handlers *h, upb_endmsg_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setint32(upb_handlers *h, const upb_fielddef *f, - upb_int32_handlerfunc *func, upb_handlerattr *attr); -bool upb_handlers_setint64(upb_handlers *h, const upb_fielddef *f, - upb_int64_handlerfunc *func, upb_handlerattr *attr); -bool upb_handlers_setuint32(upb_handlers *h, const upb_fielddef *f, - upb_uint32_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setuint64(upb_handlers *h, const upb_fielddef *f, - upb_uint64_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setfloat(upb_handlers *h, const upb_fielddef *f, - upb_float_handlerfunc *func, upb_handlerattr *attr); -bool upb_handlers_setdouble(upb_handlers *h, const upb_fielddef *f, - upb_double_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setbool(upb_handlers *h, const upb_fielddef *f, - upb_bool_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setstartstr(upb_handlers *h, const upb_fielddef *f, - upb_startstr_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setstring(upb_handlers *h, const upb_fielddef *f, - upb_string_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setendstr(upb_handlers *h, const upb_fielddef *f, - upb_endfield_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setstartseq(upb_handlers *h, const upb_fielddef *f, - upb_startfield_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setstartsubmsg(upb_handlers *h, const upb_fielddef *f, - upb_startfield_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setendsubmsg(upb_handlers *h, const upb_fielddef *f, - upb_endfield_handlerfunc *func, - upb_handlerattr *attr); -bool upb_handlers_setendseq(upb_handlers *h, const upb_fielddef *f, - upb_endfield_handlerfunc *func, - upb_handlerattr *attr); + /* It is wasteful to include the entire attributes here: + * + * * Some of the information is redundant (like storing the closure type + * separately for each handler that must match). + * * Some of the info is only needed prior to freeze() (like closure types). + * * alignment padding wastes a lot of space for alwaysok_. + * + * If/when the size and locality of handlers is an issue, we can optimize this + * not to store the entire attr like this. We do not expose the table's + * layout to allow this optimization in the future. */ + upb_handlerattr attr; +} upb_handlers_tabent; -const upb_handlers *upb_handlers_getsubhandlers(const upb_handlers *h, - const upb_fielddef *f); -const upb_handlers *upb_handlers_getsubhandlers_sel(const upb_handlers *h, - upb_selector_t sel); +#define UPB_TABENT_INIT {NULL, UPB_HANDLERATTR_INIT} -UPB_INLINE upb_func *upb_handlers_gethandler(const upb_handlers *h, - upb_selector_t s) { - return (upb_func *)h->table[s].func; -} +typedef struct { + upb_handlers_tabent table[3]; +} upb_byteshandler; -bool upb_handlers_getattr(const upb_handlers *h, upb_selector_t s, - upb_handlerattr *attr); +#define UPB_BYTESHANDLER_INIT \ + { \ + { UPB_TABENT_INIT, UPB_TABENT_INIT, UPB_TABENT_INIT } \ + } -UPB_INLINE const void *upb_handlers_gethandlerdata(const upb_handlers *h, - upb_selector_t s) { - return upb_handlerattr_handlerdata(&h->table[s].attr); +UPB_INLINE void upb_byteshandler_init(upb_byteshandler *handler) { + upb_byteshandler init = UPB_BYTESHANDLER_INIT; + *handler = init; } -typedef void upb_handlers_callback(const void *closure, upb_handlers *h); - -#ifdef __cplusplus - -class upb::HandlerCache { - public: - static HandlerCache *New(upb_handlers_callback *callback, - const void *closure); - static void Free(HandlerCache* cache); - - const Handlers* Get(const MessageDef* md); - - private: - UPB_DISALLOW_POD_OPS(HandlerCache, upb::pb::HandlerCache) -#else -struct upb_handlercache { -#endif - upb_arena arena; - upb_inttable tab; /* maps upb_msgdef* -> upb_handlers*. */ - upb_inttable cleanup_; - upb_handlers_callback *callback; - const void *closure; -}; - -upb_handlercache *upb_handlercache_new(upb_handlers_callback *callback, - const void *closure); -void upb_handlercache_free(upb_handlercache *cache); -const upb_handlers *upb_handlercache_get(upb_handlercache *cache, - const upb_msgdef *md); - -#ifdef __cplusplus - -/* Handler types for single fields. - * Right now we only have one for TYPE_BYTES but ones for other types - * should follow. - * - * These follow the same handlers protocol for fields of a message. */ -class upb::BytesHandler { - public: - BytesHandler(); - ~BytesHandler(); -#else -struct upb_byteshandler { -#endif - upb_handlers_tabent table[3]; -}; - -void upb_byteshandler_init(upb_byteshandler *h); - -/* Caller must ensure that "d" outlives the handlers. - * TODO(haberman): should this have a "freeze" operation? It's not necessary - * for memory management, but could be useful to force immutability and provide - * a convenient moment to verify that all registration succeeded. */ +/* Caller must ensure that "d" outlives the handlers. */ bool upb_byteshandler_setstartstr(upb_byteshandler *h, upb_startstr_handlerfunc *func, void *d); bool upb_byteshandler_setstring(upb_byteshandler *h, @@ -774,21 +671,18 @@ bool upb_byteshandler_setstring(upb_byteshandler *h, bool upb_byteshandler_setendstr(upb_byteshandler *h, upb_endfield_handlerfunc *func, void *d); -/* "Static" methods */ -upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f); -bool upb_handlers_getselector(const upb_fielddef *f, upb_handlertype_t type, - upb_selector_t *s); -UPB_INLINE upb_selector_t upb_handlers_getendselector(upb_selector_t start) { - return start + 1; +#ifdef __cplusplus +namespace upb { +typedef upb_byteshandler BytesHandler; } +#endif -/* Internal-only. */ -uint32_t upb_handlers_selectorbaseoffset(const upb_fielddef *f); -uint32_t upb_handlers_selectorcount(const upb_fielddef *f); - +UPB_END_EXTERN_C /** Message handlers ******************************************************************/ +UPB_BEGIN_EXTERN_C + /* These are the handlers used internally by upb_msgfactory_getmergehandlers(). * They write scalar data to a known offset from the message pointer. * diff --git a/upb/json/printer.c b/upb/json/printer.c index 444916f..b2c9ebd 100644 --- a/upb/json/printer.c +++ b/upb/json/printer.c @@ -601,7 +601,7 @@ static void set_enum_hd(upb_handlers *h, hd->enumdef = upb_fielddef_enumsubdef(f); hd->keyname = newstrpc(h, f, preserve_fieldnames); upb_handlers_addcleanup(h, hd, upb_gfree); - upb_handlerattr_sethandlerdata(attr, hd); + attr->handler_data = hd; } /* Set up handlers for a mapentry submessage (i.e., an individual key/value pair @@ -626,7 +626,7 @@ void printer_sethandlers_mapentry(const void *closure, bool preserve_fieldnames, const upb_fielddef* key_field = upb_msgdef_itof(md, UPB_MAPENTRY_KEY); const upb_fielddef* value_field = upb_msgdef_itof(md, UPB_MAPENTRY_VALUE); - upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; + upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT; UPB_UNUSED(closure); @@ -690,10 +690,9 @@ void printer_sethandlers_mapentry(const void *closure, bool preserve_fieldnames, upb_handlers_setstring(h, value_field, putbytes, &empty_attr); break; case UPB_TYPE_ENUM: { - upb_handlerattr enum_attr = UPB_HANDLERATTR_INITIALIZER; + upb_handlerattr enum_attr = UPB_HANDLERATTR_INIT; set_enum_hd(h, value_field, preserve_fieldnames, &enum_attr); upb_handlers_setint32(h, value_field, mapvalue_enum, &enum_attr); - upb_handlerattr_uninit(&enum_attr); break; } case UPB_TYPE_MESSAGE: @@ -701,8 +700,6 @@ void printer_sethandlers_mapentry(const void *closure, bool preserve_fieldnames, * as appropriate. */ break; } - - upb_handlerattr_uninit(&empty_attr); } static bool putseconds(void *closure, const void *handler_data, @@ -948,16 +945,16 @@ void printer_sethandlers_any(const void *closure, upb_handlers *h) { const upb_fielddef* type_field = upb_msgdef_itof(md, UPB_ANY_TYPE); const upb_fielddef* value_field = upb_msgdef_itof(md, UPB_ANY_VALUE); - upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; + upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT; /* type_url's json name is "@type" */ - upb_handlerattr type_name_attr = UPB_HANDLERATTR_INITIALIZER; - upb_handlerattr value_name_attr = UPB_HANDLERATTR_INITIALIZER; + upb_handlerattr type_name_attr = UPB_HANDLERATTR_INIT; + upb_handlerattr value_name_attr = UPB_HANDLERATTR_INIT; strpc *type_url_json_name = newstrpc_str(h, "@type"); strpc *value_json_name = newstrpc_str(h, "value"); - upb_handlerattr_sethandlerdata(&type_name_attr, type_url_json_name); - upb_handlerattr_sethandlerdata(&value_name_attr, value_json_name); + type_name_attr.handler_data = type_url_json_name; + value_name_attr.handler_data = value_json_name; /* Set up handlers. */ upb_handlers_setstartmsg(h, printer_startmsg, &empty_attr); @@ -985,7 +982,7 @@ void printer_sethandlers_duration(const void *closure, upb_handlers *h) { const upb_fielddef* nanos_field = upb_msgdef_itof(md, UPB_DURATION_NANOS); - upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; + upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT; upb_handlers_setstartmsg(h, printer_startdurationmsg, &empty_attr); upb_handlers_setint64(h, seconds_field, putseconds, &empty_attr); @@ -1005,7 +1002,7 @@ void printer_sethandlers_timestamp(const void *closure, upb_handlers *h) { const upb_fielddef* nanos_field = upb_msgdef_itof(md, UPB_TIMESTAMP_NANOS); - upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; + upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT; upb_handlers_setstartmsg(h, printer_starttimestampmsg, &empty_attr); upb_handlers_setint64(h, seconds_field, putseconds, &empty_attr); @@ -1019,7 +1016,7 @@ void printer_sethandlers_value(const void *closure, upb_handlers *h) { const upb_msgdef *md = upb_handlers_msgdef(h); upb_msg_field_iter i; - upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; + upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT; upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr); upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr); @@ -1058,7 +1055,7 @@ void printer_sethandlers_value(const void *closure, upb_handlers *h) { void printer_sethandlers_##wrapper(const void *closure, upb_handlers *h) { \ const upb_msgdef *md = upb_handlers_msgdef(h); \ const upb_fielddef* f = upb_msgdef_itof(md, 1); \ - upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; \ + upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT; \ upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr); \ upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr); \ upb_handlers_set##type(h, f, putmethod, &empty_attr); \ @@ -1081,7 +1078,7 @@ void printer_sethandlers_listvalue(const void *closure, upb_handlers *h) { const upb_msgdef *md = upb_handlers_msgdef(h); const upb_fielddef* f = upb_msgdef_itof(md, 1); - upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; + upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT; upb_handlers_setstartseq(h, f, startseq_nokey, &empty_attr); upb_handlers_setendseq(h, f, endseq, &empty_attr); @@ -1098,7 +1095,7 @@ void printer_sethandlers_structvalue(const void *closure, upb_handlers *h) { const upb_msgdef *md = upb_handlers_msgdef(h); const upb_fielddef* f = upb_msgdef_itof(md, 1); - upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; + upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT; upb_handlers_setstartseq(h, f, startmap_nokey, &empty_attr); upb_handlers_setendseq(h, f, endmap, &empty_attr); @@ -1114,7 +1111,7 @@ void printer_sethandlers_structvalue(const void *closure, upb_handlers *h) { void printer_sethandlers(const void *closure, upb_handlers *h) { const upb_msgdef *md = upb_handlers_msgdef(h); bool is_mapentry = upb_msgdef_mapentry(md); - upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; + upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT; upb_msg_field_iter i; const upb_json_printercache *cache = closure; const bool preserve_fieldnames = cache->preserve_fieldnames; @@ -1181,9 +1178,8 @@ void printer_sethandlers(const void *closure, upb_handlers *h) { for(; !upb_msg_field_done(&i); upb_msg_field_next(&i)) { const upb_fielddef *f = upb_msg_iter_field(&i); - upb_handlerattr name_attr = UPB_HANDLERATTR_INITIALIZER; - upb_handlerattr_sethandlerdata(&name_attr, - newstrpc(h, f, preserve_fieldnames)); + upb_handlerattr name_attr = UPB_HANDLERATTR_INIT; + name_attr.handler_data = newstrpc(h, f, preserve_fieldnames); if (upb_fielddef_ismap(f)) { upb_handlers_setstartseq(h, f, startmap, &name_attr); @@ -1205,7 +1201,7 @@ void printer_sethandlers(const void *closure, upb_handlers *h) { /* For now, we always emit symbolic names for enums. We may want an * option later to control this behavior, but we will wait for a real * need first. */ - upb_handlerattr enum_attr = UPB_HANDLERATTR_INITIALIZER; + upb_handlerattr enum_attr = UPB_HANDLERATTR_INIT; set_enum_hd(h, f, preserve_fieldnames, &enum_attr); if (upb_fielddef_isseq(f)) { @@ -1214,7 +1210,6 @@ void printer_sethandlers(const void *closure, upb_handlers *h) { upb_handlers_setint32(h, f, scalar_enum, &enum_attr); } - upb_handlerattr_uninit(&enum_attr); break; } case UPB_TYPE_STRING: @@ -1245,11 +1240,8 @@ void printer_sethandlers(const void *closure, upb_handlers *h) { } break; } - - upb_handlerattr_uninit(&name_attr); } - upb_handlerattr_uninit(&empty_attr); #undef TYPE } diff --git a/upb/pb/compile_decoder.c b/upb/pb/compile_decoder.c index 02f5179..e17ca03 100644 --- a/upb/pb/compile_decoder.c +++ b/upb/pb/compile_decoder.c @@ -516,7 +516,7 @@ static upb_pbdecodermethod *find_submethod(const compiler *c, static void putsel(compiler *c, opcode op, upb_selector_t sel, const upb_handlers *h) { - if (upb_handlers_gethandler(h, sel)) { + if (upb_handlers_gethandler(h, sel, NULL)) { putop(c, op, sel); } } @@ -532,9 +532,9 @@ static bool haslazyhandlers(const upb_handlers *h, const upb_fielddef *f) { if (!upb_fielddef_lazy(f)) return false; - return upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_STARTSTR)) || - upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_STRING)) || - upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_ENDSTR)); + return upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_STARTSTR), NULL) || + upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_STRING), NULL) || + upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_ENDSTR), NULL); } diff --git a/upb/pb/decoder.h b/upb/pb/decoder.h index 1a00801..1ffcb7d 100644 --- a/upb/pb/decoder.h +++ b/upb/pb/decoder.h @@ -21,17 +21,13 @@ namespace upb { namespace pb { class CodeCache; -class Decoder; -class DecoderMethod; +class DecoderPtr; +class DecoderMethodPtr; class DecoderMethodOptions; } /* namespace pb */ } /* namespace upb */ #endif -UPB_DECLARE_TYPE(upb::pb::CodeCache, upb_pbcodecache) -UPB_DECLARE_TYPE(upb::pb::Decoder, upb_pbdecoder) -UPB_DECLARE_TYPE(upb::pb::DecoderMethod, upb_pbdecodermethod) - /* The maximum number of bytes we are required to buffer internally between * calls to the decoder. The value is 14: a 5 byte unknown tag plus ten-byte * varint, less one because we are buffering an incomplete value. @@ -39,54 +35,106 @@ UPB_DECLARE_TYPE(upb::pb::DecoderMethod, upb_pbdecodermethod) * Should only be used by unit tests. */ #define UPB_DECODER_MAX_RESIDUAL_BYTES 14 +/* upb_pbdecodermethod ********************************************************/ + +struct upb_pbdecodermethod; +typedef struct upb_pbdecodermethod upb_pbdecodermethod; + +UPB_BEGIN_EXTERN_C + +const upb_handlers *upb_pbdecodermethod_desthandlers( + const upb_pbdecodermethod *m); +const upb_byteshandler *upb_pbdecodermethod_inputhandler( + const upb_pbdecodermethod *m); +bool upb_pbdecodermethod_isnative(const upb_pbdecodermethod *m); + +UPB_END_EXTERN_C + #ifdef __cplusplus /* Represents the code to parse a protobuf according to a destination * Handlers. */ -class upb::pb::DecoderMethod { +class upb::pb::DecoderMethodPtr { public: + DecoderMethodPtr(const upb_pbdecodermethod* ptr) : ptr_(ptr) {} + + const upb_pbdecodermethod* ptr() { return ptr_; } + /* The destination handlers that are statically bound to this method. * This method is only capable of outputting to a sink that uses these * handlers. */ - const Handlers* dest_handlers() const; + const Handlers *dest_handlers() const { + return upb_pbdecodermethod_desthandlers(ptr_); + } /* The input handlers for this decoder method. */ - const BytesHandler* input_handler() const; + const BytesHandler* input_handler() const { + return upb_pbdecodermethod_inputhandler(ptr_); + } /* Whether this method is native. */ - bool is_native() const; + bool is_native() const { + return upb_pbdecodermethod_isnative(ptr_); + } private: - UPB_DISALLOW_POD_OPS(DecoderMethod, upb::pb::DecoderMethod) + const upb_pbdecodermethod* ptr_; }; #endif +/* upb_pbdecoder **************************************************************/ + /* Preallocation hint: decoder won't allocate more bytes than this when first * constructed. This hint may be an overestimate for some build configurations. * But if the decoder library is upgraded without recompiling the application, * it may be an underestimate. */ #define UPB_PB_DECODER_SIZE 4416 +struct upb_pbdecoder; +typedef struct upb_pbdecoder upb_pbdecoder; + +UPB_BEGIN_EXTERN_C + +upb_pbdecoder *upb_pbdecoder_create(upb_env *e, + const upb_pbdecodermethod *method, + upb_sink *output); +const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d); +upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d); +uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d); +size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d); +bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max); +void upb_pbdecoder_reset(upb_pbdecoder *d); + +UPB_END_EXTERN_C + #ifdef __cplusplus /* A Decoder receives binary protobuf data on its input sink and pushes the * decoded data to its output sink. */ -class upb::pb::Decoder { +class upb::pb::DecoderPtr { public: + DecoderPtr(upb_pbdecoder* ptr) : ptr_(ptr) {} + + upb_pbdecoder* ptr() { return ptr_; } + /* Constructs a decoder instance for the given method, which must outlive this * decoder. Any errors during parsing will be set on the given status, which * must also outlive this decoder. * * The sink must match the given method. */ - static Decoder* Create(Environment* env, const DecoderMethod* method, - Sink* output); + static DecoderPtr Create(Environment *env, DecoderMethodPtr method, + upb_sink *output) { + return DecoderPtr(upb_pbdecoder_create(env, method.ptr(), output)); + } /* Returns the DecoderMethod this decoder is parsing from. */ - const DecoderMethod* method() const; + const DecoderMethodPtr method() const { + return DecoderMethodPtr(upb_pbdecoder_method(ptr_)); + } /* The sink on which this decoder receives input. */ - BytesSink* input(); + upb_bytessink* input() { return upb_pbdecoder_input(ptr()); } /* Returns number of bytes successfully parsed. * @@ -95,7 +143,7 @@ class upb::pb::Decoder { * * This value may not be up-to-date when called from inside a parsing * callback. */ - uint64_t BytesParsed() const; + uint64_t BytesParsed() { return upb_pbdecoder_bytesparsed(ptr()); } /* Gets/sets the parsing nexting limit. If the total number of nested * submessages and repeated fields hits this limit, parsing will fail. This @@ -104,25 +152,51 @@ class upb::pb::Decoder { * * Setting the limit will fail if the parser is currently suspended at a depth * greater than this, or if memory allocation of the stack fails. */ - size_t max_nesting() const; - bool set_max_nesting(size_t max); + size_t max_nesting() { return upb_pbdecoder_maxnesting(ptr()); } + bool set_max_nesting(size_t max) { return upb_pbdecoder_maxnesting(ptr()); } - void Reset(); + void Reset() { upb_pbdecoder_reset(ptr()); } static const size_t kSize = UPB_PB_DECODER_SIZE; private: - UPB_DISALLOW_POD_OPS(Decoder, upb::pb::Decoder) + upb_pbdecoder *ptr_; }; +#endif /* __cplusplus */ + +/* upb_pbcodecache ************************************************************/ + +struct upb_pbcodecache; +typedef struct upb_pbcodecache upb_pbcodecache; + +UPB_BEGIN_EXTERN_C + +upb_pbcodecache *upb_pbcodecache_new(upb_handlercache *dest); +void upb_pbcodecache_free(upb_pbcodecache *c); +bool upb_pbcodecache_allowjit(const upb_pbcodecache *c); +void upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow); +void upb_pbcodecache_setlazy(upb_pbcodecache *c, bool lazy); +const upb_pbdecodermethod *upb_pbcodecache_get(upb_pbcodecache *c, + const upb_msgdef *md); + +UPB_END_EXTERN_C + +#ifdef __cplusplus + /* A class for caching protobuf processing code, whether bytecode for the * interpreted decoder or machine code for the JIT. * * This class is not thread-safe. */ class upb::pb::CodeCache { public: - static CodeCache* New(HandlerCache* dest); - static void Free(CodeCache* cache); + CodeCache(upb::HandlerCache *dest) + : ptr_(upb_pbcodecache_new(dest->ptr()), upb_pbcodecache_free) {} + CodeCache(CodeCache&&) = default; + CodeCache& operator=(CodeCache&&) = default; + + upb_pbcodecache* ptr() { return ptr_.get(); } + const upb_pbcodecache* ptr() const { return ptr_.get(); } /* Whether the cache is allowed to generate machine code. Defaults to true. * There is no real reason to turn it off except for testing or if you are @@ -131,114 +205,27 @@ class upb::pb::CodeCache { * Note that allow_jit = true does not *guarantee* that the code will be JIT * compiled. If this platform is not supported or the JIT was not compiled * in, the code may still be interpreted. */ - bool allow_jit() const; + bool allow_jit() const { return upb_pbcodecache_allowjit(ptr()); } /* This may only be called when the object is first constructed, and prior to * any code generation. */ - void set_allow_jit(bool allow); + void set_allow_jit(bool allow) { upb_pbcodecache_setallowjit(ptr(), allow); } /* Should the decoder push submessages to lazy handlers for fields that have * them? The caller should set this iff the lazy handlers expect data that is * in protobuf binary format and the caller wishes to lazy parse it. */ - void set_lazy(bool lazy); + void set_lazy(bool lazy) { upb_pbcodecache_setlazy(ptr(), lazy); } /* Returns a DecoderMethod that can push data to the given handlers. * If a suitable method already exists, it will be returned from the cache. */ - const DecoderMethod *Get(const MessageDef* md); + const DecoderMethodPtr Get(MessageDefPtr md) { + return DecoderMethodPtr(upb_pbcodecache_get(ptr(), md.ptr())); + } private: - UPB_DISALLOW_POD_OPS(CodeCache, upb::pb::CodeCache) + std::unique_ptr ptr_; }; -#endif - -UPB_BEGIN_EXTERN_C - -upb_pbdecoder *upb_pbdecoder_create(upb_env *e, - const upb_pbdecodermethod *method, - upb_sink *output); -const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d); -upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d); -uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d); -size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d); -bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max); -void upb_pbdecoder_reset(upb_pbdecoder *d); - - - -const upb_handlers *upb_pbdecodermethod_desthandlers( - const upb_pbdecodermethod *m); -const upb_byteshandler *upb_pbdecodermethod_inputhandler( - const upb_pbdecodermethod *m); -bool upb_pbdecodermethod_isnative(const upb_pbdecodermethod *m); - -upb_pbcodecache *upb_pbcodecache_new(upb_handlercache *dest); -void upb_pbcodecache_free(upb_pbcodecache *c); -bool upb_pbcodecache_allowjit(const upb_pbcodecache *c); -void upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow); -void upb_pbcodecache_setlazy(upb_pbcodecache *c, bool lazy); -const upb_pbdecodermethod *upb_pbcodecache_get(upb_pbcodecache *c, - const upb_msgdef *md); - -UPB_END_EXTERN_C - -#ifdef __cplusplus - -namespace upb { - -namespace pb { - -/* static */ -inline Decoder* Decoder::Create(Environment* env, const DecoderMethod* m, - Sink* sink) { - return upb_pbdecoder_create(env, m, sink); -} -inline const DecoderMethod* Decoder::method() const { - return upb_pbdecoder_method(this); -} -inline BytesSink* Decoder::input() { - return upb_pbdecoder_input(this); -} -inline uint64_t Decoder::BytesParsed() const { - return upb_pbdecoder_bytesparsed(this); -} -inline size_t Decoder::max_nesting() const { - return upb_pbdecoder_maxnesting(this); -} -inline bool Decoder::set_max_nesting(size_t max) { - return upb_pbdecoder_setmaxnesting(this, max); -} -inline void Decoder::Reset() { upb_pbdecoder_reset(this); } - -inline const Handlers* DecoderMethod::dest_handlers() const { - return upb_pbdecodermethod_desthandlers(this); -} -inline const BytesHandler* DecoderMethod::input_handler() const { - return upb_pbdecodermethod_inputhandler(this); -} -inline bool DecoderMethod::is_native() const { - return upb_pbdecodermethod_isnative(this); -} - -inline CodeCache* CodeCache::New(HandlerCache* dest) { - return upb_pbcodecache_new(dest); -} -inline void CodeCache::Free(CodeCache* cache) { - upb_pbcodecache_free(cache); -} -inline bool CodeCache::allow_jit() const { - return upb_pbcodecache_allowjit(this); -} -inline void CodeCache::set_allow_jit(bool allow) { - upb_pbcodecache_setallowjit(this, allow); -} -inline const DecoderMethod *CodeCache::Get(const MessageDef *md) { - return upb_pbcodecache_get(this, md); -} - -} /* namespace pb */ -} /* namespace upb */ - #endif /* __cplusplus */ #endif /* UPB_DECODER_H_ */ diff --git a/upb/pb/encoder.c b/upb/pb/encoder.c index ca3ca5c..3497007 100644 --- a/upb/pb/encoder.c +++ b/upb/pb/encoder.c @@ -304,8 +304,7 @@ static void new_tag(upb_handlers *h, const upb_fielddef *f, upb_wiretype_t wt, tag_t *tag = upb_gmalloc(sizeof(tag_t)); tag->bytes = upb_vencode64((n << 3) | wt, tag->tag); - upb_handlerattr_init(attr); - upb_handlerattr_sethandlerdata(attr, tag); + attr->handler_data = tag; upb_handlers_addcleanup(h, tag, upb_gfree); } @@ -451,7 +450,7 @@ static void newhandlers_callback(const void *closure, upb_handlers *h) { const upb_fielddef *f = upb_msg_iter_field(&i); bool packed = upb_fielddef_isseq(f) && upb_fielddef_isprimitive(f) && upb_fielddef_packed(f); - upb_handlerattr attr; + upb_handlerattr attr = UPB_HANDLERATTR_INIT; upb_wiretype_t wt = packed ? UPB_WIRE_TYPE_DELIMITED : upb_pb_native_wire_types[upb_fielddef_descriptortype(f)]; @@ -500,20 +499,17 @@ static void newhandlers_callback(const void *closure, upb_handlers *h) { break; case UPB_DESCRIPTOR_TYPE_GROUP: { /* Endgroup takes a different tag (wire_type = END_GROUP). */ - upb_handlerattr attr2; + upb_handlerattr attr2 = UPB_HANDLERATTR_INIT; new_tag(h, f, UPB_WIRE_TYPE_END_GROUP, &attr2); upb_handlers_setstartsubmsg(h, f, encode_startgroup, &attr); upb_handlers_setendsubmsg(h, f, encode_endgroup, &attr2); - upb_handlerattr_uninit(&attr2); break; } } #undef T - - upb_handlerattr_uninit(&attr); } } diff --git a/upb/pb/encoder.h b/upb/pb/encoder.h index eefa385..20ce606 100644 --- a/upb/pb/encoder.h +++ b/upb/pb/encoder.h @@ -17,16 +17,14 @@ #ifdef __cplusplus namespace upb { namespace pb { -class Encoder; +class EncoderPtr; } /* namespace pb */ } /* namespace upb */ #endif -UPB_DECLARE_TYPE(upb::pb::Encoder, upb_pb_encoder) - #define UPB_PBENCODER_MAX_NESTING 100 -/* upb::pb::Encoder ***********************************************************/ +/* upb_pb_encoder *************************************************************/ /* Preallocation hint: decoder won't allocate more bytes than this when first * constructed. This hint may be an overestimate for some build configurations. @@ -34,56 +32,48 @@ UPB_DECLARE_TYPE(upb::pb::Encoder, upb_pb_encoder) * it may be an underestimate. */ #define UPB_PB_ENCODER_SIZE 768 +struct upb_pb_encoder; +typedef struct upb_pb_encoder upb_pb_encoder; + +UPB_BEGIN_EXTERN_C + +upb_sink *upb_pb_encoder_input(upb_pb_encoder *p); +upb_pb_encoder* upb_pb_encoder_create(upb_env* e, const upb_handlers* h, + upb_bytessink* output); + +upb_handlercache *upb_pb_encoder_newcache(); + +UPB_END_EXTERN_C + #ifdef __cplusplus -class upb::pb::Encoder { +class upb::pb::EncoderPtr { public: + EncoderPtr(upb_pb_encoder* ptr) : ptr_(ptr) {} + + upb_pb_encoder* ptr() { return ptr_; } + /* Creates a new encoder in the given environment. The Handlers must have * come from NewHandlers() below. */ - static Encoder* Create(Environment* env, const Handlers* handlers, - BytesSink* output); + static EncoderPtr Create(Environment* env, const Handlers* handlers, + BytesSink* output) { + return EncoderPtr(upb_pb_encoder_create(env, handlers, output->ptr())); + } /* The input to the encoder. */ - Sink* input(); + upb_sink* input() { return upb_pb_encoder_input(ptr()); } /* Creates a new set of handlers for this MessageDef. */ - static upb_handlercache* NewCache(); + static HandlerCache NewCache() { + return HandlerCache(upb_pb_encoder_newcache()); + } static const size_t kSize = UPB_PB_ENCODER_SIZE; private: - UPB_DISALLOW_POD_OPS(Encoder, upb::pb::Encoder) + upb_pb_encoder* ptr_; }; -#endif - -UPB_BEGIN_EXTERN_C - -upb_sink *upb_pb_encoder_input(upb_pb_encoder *p); -upb_pb_encoder* upb_pb_encoder_create(upb_env* e, const upb_handlers* h, - upb_bytessink* output); - -upb_handlercache *upb_pb_encoder_newcache(); - -UPB_END_EXTERN_C - -#ifdef __cplusplus - -namespace upb { -namespace pb { -inline Encoder* Encoder::Create(Environment* env, const Handlers* handlers, - BytesSink* output) { - return upb_pb_encoder_create(env, handlers, output); -} -inline Sink* Encoder::input() { - return upb_pb_encoder_input(this); -} -inline upb_handlercache* Encoder::NewCache() { - return upb_pb_encoder_newcache(); -} -} /* namespace pb */ -} /* namespace upb */ - -#endif +#endif /* __cplusplus */ #endif /* UPB_ENCODER_H_ */ diff --git a/upb/pb/textprinter.c b/upb/pb/textprinter.c index b6f8024..d1d539d 100644 --- a/upb/pb/textprinter.c +++ b/upb/pb/textprinter.c @@ -260,8 +260,8 @@ static void onmreg(const void *c, upb_handlers *h) { !upb_msg_field_done(&i); upb_msg_field_next(&i)) { upb_fielddef *f = upb_msg_iter_field(&i); - upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER; - upb_handlerattr_sethandlerdata(&attr, f); + upb_handlerattr attr = UPB_HANDLERATTR_INIT; + attr.handler_data = f; switch (upb_fielddef_type(f)) { case UPB_TYPE_INT32: upb_handlers_setint32(h, f, textprinter_putint32, &attr); @@ -295,7 +295,7 @@ static void onmreg(const void *c, upb_handlers *h) { upb_fielddef_descriptortype(f) == UPB_DESCRIPTOR_TYPE_GROUP ? shortname(upb_msgdef_fullname(upb_fielddef_msgsubdef(f))) : upb_fielddef_name(f); - upb_handlerattr_sethandlerdata(&attr, name); + attr.handler_data = name; upb_handlers_setstartsubmsg(h, f, textprinter_startsubmsg, &attr); upb_handlers_setendsubmsg(h, f, textprinter_endsubmsg, &attr); break; diff --git a/upb/sink.c b/upb/sink.c index e6ede49..d0197a6 100644 --- a/upb/sink.c +++ b/upb/sink.c @@ -4,9 +4,8 @@ bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink *sink) { void *subc; bool ret; - upb_bufhandle handle; - upb_bufhandle_init(&handle); - upb_bufhandle_setbuf(&handle, buf, 0); + upb_bufhandle handle = UPB_BUFHANDLE_INIT; + handle.buf = buf; ret = upb_bytessink_start(sink, len, &subc); if (ret && len != 0) { ret = (upb_bytessink_putbuf(sink, subc, buf, len, &handle) >= len); @@ -14,7 +13,6 @@ bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink *sink) { if (ret) { ret = upb_bytessink_end(sink); } - upb_bufhandle_uninit(&handle); return ret; } diff --git a/upb/sink.h b/upb/sink.h index 0b98f07..8cab45d 100644 --- a/upb/sink.h +++ b/upb/sink.h @@ -22,17 +22,177 @@ #ifdef __cplusplus namespace upb { -class BufferSink; -class BufferSource; class BytesSink; class Sink; } #endif -UPB_DECLARE_TYPE(upb::BufferSink, upb_bufsink) -UPB_DECLARE_TYPE(upb::BufferSource, upb_bufsrc) -UPB_DECLARE_TYPE(upb::BytesSink, upb_bytessink) -UPB_DECLARE_TYPE(upb::Sink, upb_sink) +/* upb_sink *******************************************************************/ + +UPB_BEGIN_EXTERN_C + +typedef struct { + const upb_handlers *handlers; + void *closure; +} upb_sink; + +#define PUTVAL(type, ctype) \ + UPB_INLINE bool upb_sink_put##type(upb_sink *s, upb_selector_t sel, \ + ctype val) { \ + typedef upb_##type##_handlerfunc functype; \ + functype *func; \ + const void *hd; \ + if (!s->handlers) return true; \ + func = (functype *)upb_handlers_gethandler(s->handlers, sel, &hd); \ + if (!func) return true; \ + return func(s->closure, hd, val); \ + } + +PUTVAL(int32, int32_t) +PUTVAL(int64, int64_t) +PUTVAL(uint32, uint32_t) +PUTVAL(uint64, uint64_t) +PUTVAL(float, float) +PUTVAL(double, double) +PUTVAL(bool, bool) +#undef PUTVAL + +UPB_INLINE void upb_sink_reset(upb_sink *s, const upb_handlers *h, void *c) { + s->handlers = h; + s->closure = c; +} + +UPB_INLINE size_t upb_sink_putstring(upb_sink *s, upb_selector_t sel, + const char *buf, size_t n, + const upb_bufhandle *handle) { + typedef upb_string_handlerfunc func; + func *handler; + const void *hd; + if (!s->handlers) return n; + handler = (func *)upb_handlers_gethandler(s->handlers, sel, &hd); + + if (!handler) return n; + return handler(s->closure, hd, buf, n, handle); +} + +UPB_INLINE bool upb_sink_putunknown(upb_sink *s, const char *buf, size_t n) { + typedef upb_unknown_handlerfunc func; + func *handler; + const void *hd; + if (!s->handlers) return true; + handler = + (func *)upb_handlers_gethandler(s->handlers, UPB_UNKNOWN_SELECTOR, &hd); + + if (!handler) return n; + return handler(s->closure, hd, buf, n); +} + +UPB_INLINE bool upb_sink_startmsg(upb_sink *s) { + typedef upb_startmsg_handlerfunc func; + func *startmsg; + const void *hd; + if (!s->handlers) return true; + startmsg = + (func *)upb_handlers_gethandler(s->handlers, UPB_STARTMSG_SELECTOR, &hd); + + if (!startmsg) return true; + return startmsg(s->closure, hd); +} + +UPB_INLINE bool upb_sink_endmsg(upb_sink *s, upb_status *status) { + typedef upb_endmsg_handlerfunc func; + func *endmsg; + const void *hd; + if (!s->handlers) return true; + endmsg = + (func *)upb_handlers_gethandler(s->handlers, UPB_ENDMSG_SELECTOR, &hd); + + if (!endmsg) return true; + return endmsg(s->closure, hd, status); +} + +UPB_INLINE bool upb_sink_startseq(upb_sink *s, upb_selector_t sel, + upb_sink *sub) { + typedef upb_startfield_handlerfunc func; + func *startseq; + const void *hd; + sub->closure = s->closure; + sub->handlers = s->handlers; + if (!s->handlers) return true; + startseq = (func*)upb_handlers_gethandler(s->handlers, sel, &hd); + + if (!startseq) return true; + sub->closure = startseq(s->closure, hd); + return sub->closure ? true : false; +} + +UPB_INLINE bool upb_sink_endseq(upb_sink *s, upb_selector_t sel) { + typedef upb_endfield_handlerfunc func; + func *endseq; + const void *hd; + if (!s->handlers) return true; + endseq = (func*)upb_handlers_gethandler(s->handlers, sel, &hd); + + if (!endseq) return true; + return endseq(s->closure, hd); +} + +UPB_INLINE bool upb_sink_startstr(upb_sink *s, upb_selector_t sel, + size_t size_hint, upb_sink *sub) { + typedef upb_startstr_handlerfunc func; + func *startstr; + const void *hd; + sub->closure = s->closure; + sub->handlers = s->handlers; + if (!s->handlers) return true; + startstr = (func*)upb_handlers_gethandler(s->handlers, sel, &hd); + + if (!startstr) return true; + sub->closure = startstr(s->closure, hd, size_hint); + return sub->closure ? true : false; +} + +UPB_INLINE bool upb_sink_endstr(upb_sink *s, upb_selector_t sel) { + typedef upb_endfield_handlerfunc func; + func *endstr; + const void *hd; + if (!s->handlers) return true; + endstr = (func*)upb_handlers_gethandler(s->handlers, sel, &hd); + + if (!endstr) return true; + return endstr(s->closure, hd); +} + +UPB_INLINE bool upb_sink_startsubmsg(upb_sink *s, upb_selector_t sel, + upb_sink *sub) { + typedef upb_startfield_handlerfunc func; + func *startsubmsg; + const void *hd; + sub->closure = s->closure; + if (!s->handlers) { + sub->handlers = NULL; + return true; + } + sub->handlers = upb_handlers_getsubhandlers_sel(s->handlers, sel); + startsubmsg = (func*)upb_handlers_gethandler(s->handlers, sel, &hd); + + if (!startsubmsg) return true; + sub->closure = startsubmsg(s->closure, hd); + return sub->closure ? true : false; +} + +UPB_INLINE bool upb_sink_endsubmsg(upb_sink *s, upb_selector_t sel) { + typedef upb_endfield_handlerfunc func; + func *endsubmsg; + const void *hd; + if (!s->handlers) return true; + endsubmsg = (func*)upb_handlers_gethandler(s->handlers, sel, &hd); + + if (!endsubmsg) return s->closure; + return endsubmsg(s->closure, hd); +} + +UPB_END_EXTERN_C #ifdef __cplusplus @@ -81,16 +241,24 @@ class upb::Sink { * * TODO: once the Handlers know the expected closure type, verify that T * matches it. */ - template Sink(const Handlers* handlers, T* closure); + template Sink(const upb_handlers* handlers, T* closure) { + Reset(handlers, closure); + } + + upb_sink* ptr() { return &sink_; } /* Resets the value of the sink. */ - template void Reset(const Handlers* handlers, T* closure); + template void Reset(const upb_handlers* handlers, T* closure) { + upb_sink_reset(&sink_, handlers, closure); + } /* Returns the top-level object that is bound to this sink. * * TODO: once the Handlers know the expected closure type, verify that T * matches it. */ - template T* GetObject() const; + template T* GetObject() const { + return static_cast(sink_.closure); + } /* Functions for pushing data into the sink. * @@ -108,37 +276,57 @@ class upb::Sink { * // ... * sink->EndMessage(&status); * sink->EndSubMessage(endsubmsg_selector); */ - bool StartMessage(); - bool EndMessage(Status* status); + bool StartMessage() { return upb_sink_startmsg(&sink_); } + bool EndMessage(Status* status) { return upb_sink_endmsg(&sink_, status); } /* Putting of individual values. These work for both repeated and * non-repeated fields, but for repeated fields you must wrap them in * calls to StartSequence()/EndSequence(). */ - bool PutInt32(Handlers::Selector s, int32_t val); - bool PutInt64(Handlers::Selector s, int64_t val); - bool PutUInt32(Handlers::Selector s, uint32_t val); - bool PutUInt64(Handlers::Selector s, uint64_t val); - bool PutFloat(Handlers::Selector s, float val); - bool PutDouble(Handlers::Selector s, double val); - bool PutBool(Handlers::Selector s, bool val); + bool PutInt32(HandlersPtr::Selector s, int32_t val) { + return upb_sink_putint32(&sink_, s, val); + } + + bool PutInt64(HandlersPtr::Selector s, int64_t val) { + return upb_sink_putint64(&sink_, s, val); + } + + bool PutUInt32(HandlersPtr::Selector s, uint32_t val) { + return upb_sink_putuint32(&sink_, s, val); + } + + bool PutUInt64(HandlersPtr::Selector s, uint64_t val) { + return upb_sink_putuint64(&sink_, s, val); + } + + bool PutFloat(HandlersPtr::Selector s, float val) { + return upb_sink_putfloat(&sink_, s, val); + } + + bool PutDouble(HandlersPtr::Selector s, double val) { + return upb_sink_putdouble(&sink_, s, val); + } + + bool PutBool(HandlersPtr::Selector s, bool val) { + return upb_sink_putbool(&sink_, s, val); + } /* Putting of string/bytes values. Each string can consist of zero or more * non-contiguous buffers of data. * * For StartString(), the function will write a sink for the string to "sub." * The sub-sink must be used for any/all PutStringBuffer() calls. */ - bool StartString(Handlers::Selector s, size_t size_hint, Sink* sub); - size_t PutStringBuffer(Handlers::Selector s, const char *buf, size_t len, - const BufferHandle *handle); - bool EndString(Handlers::Selector s); + bool StartString(HandlersPtr::Selector s, size_t size_hint, Sink* sub); + size_t PutStringBuffer(HandlersPtr::Selector s, const char *buf, size_t len, + const upb_bufhandle *handle); + bool EndString(HandlersPtr::Selector s); /* For submessage fields. * * For StartSubMessage(), the function will write a sink for the string to * "sub." The sub-sink must be used for any/all handlers called within the * submessage. */ - bool StartSubMessage(Handlers::Selector s, Sink* sub); - bool EndSubMessage(Handlers::Selector s); + bool StartSubMessage(HandlersPtr::Selector s, Sink* sub); + bool EndSubMessage(HandlersPtr::Selector s); /* For repeated fields of any type, the sequence of values must be wrapped in * these calls. @@ -146,84 +334,26 @@ class upb::Sink { * For StartSequence(), the function will write a sink for the string to * "sub." The sub-sink must be used for any/all handlers called within the * sequence. */ - bool StartSequence(Handlers::Selector s, Sink* sub); - bool EndSequence(Handlers::Selector s); + bool StartSequence(HandlersPtr::Selector s, Sink* sub); + bool EndSequence(HandlersPtr::Selector s); /* Copy and assign specifically allowed. * We don't even bother making these members private because so many * functions need them and this is mainly just a dumb data container anyway. */ -#else -struct upb_sink { -#endif - const upb_handlers *handlers; - void *closure; -}; -#ifdef __cplusplus -class upb::BytesSink { - public: - BytesSink() {} + private: + upb_sink sink_; +}; - /* Constructs a new sink for the given frozen handlers and closure. - * - * TODO(haberman): once the Handlers know the expected closure type, verify - * that T matches it. */ - template BytesSink(const BytesHandler* handler, T* closure); +#endif /* __cplusplus */ - /* Resets the value of the sink. */ - template void Reset(const BytesHandler* handler, T* closure); +/* upb_bytessink **************************************************************/ - bool Start(size_t size_hint, void **subc); - size_t PutBuffer(void *subc, const char *buf, size_t len, - const BufferHandle *handle); - bool End(); -#else -struct upb_bytessink { -#endif +typedef struct { const upb_byteshandler *handler; void *closure; -}; - -#ifdef __cplusplus - -/* A class for pushing a flat buffer of data to a BytesSink. - * You can construct an instance of this to get a resumable source, - * or just call the static PutBuffer() to do a non-resumable push all in one - * go. */ -class upb::BufferSource { - public: - BufferSource(); - BufferSource(const char* buf, size_t len, BytesSink* sink); - - /* Returns true if the entire buffer was pushed successfully. Otherwise the - * next call to PutNext() will resume where the previous one left off. - * TODO(haberman): implement this. */ - bool PutNext(); - - /* A static version; with this version is it not possible to resume in the - * case of failure or a partially-consumed buffer. */ - static bool PutBuffer(const char* buf, size_t len, BytesSink* sink); - - template static bool PutBuffer(const T& str, BytesSink* sink) { - return PutBuffer(str.c_str(), str.size(), sink); - } -#else -struct upb_bufsrc { - char dummy; -#endif -}; - -UPB_BEGIN_EXTERN_C - -/* A class for accumulating output string data in a flat buffer. */ - -upb_bufsink *upb_bufsink_new(upb_env *env); -void upb_bufsink_free(upb_bufsink *sink); -upb_bytessink *upb_bufsink_sink(upb_bufsink *sink); -const char *upb_bufsink_getdata(const upb_bufsink *sink, size_t *len); - -/* Inline definitions. */ +} upb_bytessink ; UPB_INLINE void upb_bytessink_reset(upb_bytessink *s, const upb_byteshandler *h, void *closure) { @@ -240,8 +370,8 @@ UPB_INLINE bool upb_bytessink_start(upb_bytessink *s, size_t size_hint, start = (func *)s->handler->table[UPB_STARTSTR_SELECTOR].func; if (!start) return true; - *subc = start(s->closure, upb_handlerattr_handlerdata( - &s->handler->table[UPB_STARTSTR_SELECTOR].attr), + *subc = start(s->closure, + s->handler->table[UPB_STARTSTR_SELECTOR].attr.handler_data, size_hint); return *subc != NULL; } @@ -255,8 +385,7 @@ UPB_INLINE size_t upb_bytessink_putbuf(upb_bytessink *s, void *subc, putbuf = (func *)s->handler->table[UPB_STRING_SELECTOR].func; if (!putbuf) return true; - return putbuf(subc, upb_handlerattr_handlerdata( - &s->handler->table[UPB_STRING_SELECTOR].attr), + return putbuf(subc, s->handler->table[UPB_STRING_SELECTOR].attr.handler_data, buf, size, handle); } @@ -268,266 +397,80 @@ UPB_INLINE bool upb_bytessink_end(upb_bytessink *s) { if (!end) return true; return end(s->closure, - upb_handlerattr_handlerdata( - &s->handler->table[UPB_ENDSTR_SELECTOR].attr)); -} - -bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink *sink); - -#define PUTVAL(type, ctype) \ - UPB_INLINE bool upb_sink_put##type(upb_sink *s, upb_selector_t sel, \ - ctype val) { \ - typedef upb_##type##_handlerfunc functype; \ - functype *func; \ - const void *hd; \ - if (!s->handlers) return true; \ - func = (functype *)upb_handlers_gethandler(s->handlers, sel); \ - if (!func) return true; \ - hd = upb_handlers_gethandlerdata(s->handlers, sel); \ - return func(s->closure, hd, val); \ - } - -PUTVAL(int32, int32_t) -PUTVAL(int64, int64_t) -PUTVAL(uint32, uint32_t) -PUTVAL(uint64, uint64_t) -PUTVAL(float, float) -PUTVAL(double, double) -PUTVAL(bool, bool) -#undef PUTVAL - -UPB_INLINE void upb_sink_reset(upb_sink *s, const upb_handlers *h, void *c) { - s->handlers = h; - s->closure = c; + s->handler->table[UPB_ENDSTR_SELECTOR].attr.handler_data); } -UPB_INLINE size_t upb_sink_putstring(upb_sink *s, upb_selector_t sel, - const char *buf, size_t n, - const upb_bufhandle *handle) { - typedef upb_string_handlerfunc func; - func *handler; - const void *hd; - if (!s->handlers) return n; - handler = (func *)upb_handlers_gethandler(s->handlers, sel); - - if (!handler) return n; - hd = upb_handlers_gethandlerdata(s->handlers, sel); - return handler(s->closure, hd, buf, n, handle); -} - -UPB_INLINE bool upb_sink_putunknown(upb_sink *s, const char *buf, size_t n) { - typedef upb_unknown_handlerfunc func; - func *handler; - const void *hd; - if (!s->handlers) return true; - handler = (func *)upb_handlers_gethandler(s->handlers, UPB_UNKNOWN_SELECTOR); - - if (!handler) return n; - hd = upb_handlers_gethandlerdata(s->handlers, UPB_UNKNOWN_SELECTOR); - return handler(s->closure, hd, buf, n); -} - -UPB_INLINE bool upb_sink_startmsg(upb_sink *s) { - typedef upb_startmsg_handlerfunc func; - func *startmsg; - const void *hd; - if (!s->handlers) return true; - startmsg = (func*)upb_handlers_gethandler(s->handlers, UPB_STARTMSG_SELECTOR); - - if (!startmsg) return true; - hd = upb_handlers_gethandlerdata(s->handlers, UPB_STARTMSG_SELECTOR); - return startmsg(s->closure, hd); -} - -UPB_INLINE bool upb_sink_endmsg(upb_sink *s, upb_status *status) { - typedef upb_endmsg_handlerfunc func; - func *endmsg; - const void *hd; - if (!s->handlers) return true; - endmsg = (func *)upb_handlers_gethandler(s->handlers, UPB_ENDMSG_SELECTOR); - - if (!endmsg) return true; - hd = upb_handlers_gethandlerdata(s->handlers, UPB_ENDMSG_SELECTOR); - return endmsg(s->closure, hd, status); -} +#ifdef __cplusplus -UPB_INLINE bool upb_sink_startseq(upb_sink *s, upb_selector_t sel, - upb_sink *sub) { - typedef upb_startfield_handlerfunc func; - func *startseq; - const void *hd; - sub->closure = s->closure; - sub->handlers = s->handlers; - if (!s->handlers) return true; - startseq = (func*)upb_handlers_gethandler(s->handlers, sel); +class upb::BytesSink { + public: + BytesSink() {} - if (!startseq) return true; - hd = upb_handlers_gethandlerdata(s->handlers, sel); - sub->closure = startseq(s->closure, hd); - return sub->closure ? true : false; -} + upb_bytessink* ptr() { return &sink_; } -UPB_INLINE bool upb_sink_endseq(upb_sink *s, upb_selector_t sel) { - typedef upb_endfield_handlerfunc func; - func *endseq; - const void *hd; - if (!s->handlers) return true; - endseq = (func*)upb_handlers_gethandler(s->handlers, sel); + /* Constructs a new sink for the given frozen handlers and closure. + * + * TODO(haberman): once the Handlers know the expected closure type, verify + * that T matches it. */ + template BytesSink(const upb_byteshandler* handler, T* closure) { + upb_bytessink_reset(&sink_, handler, closure); + } - if (!endseq) return true; - hd = upb_handlers_gethandlerdata(s->handlers, sel); - return endseq(s->closure, hd); -} + /* Resets the value of the sink. */ + template void Reset(const upb_byteshandler* handler, T* closure) { + upb_bytessink_reset(&sink_, handler, closure); + } -UPB_INLINE bool upb_sink_startstr(upb_sink *s, upb_selector_t sel, - size_t size_hint, upb_sink *sub) { - typedef upb_startstr_handlerfunc func; - func *startstr; - const void *hd; - sub->closure = s->closure; - sub->handlers = s->handlers; - if (!s->handlers) return true; - startstr = (func*)upb_handlers_gethandler(s->handlers, sel); + bool Start(size_t size_hint, void **subc) { + return upb_bytessink_start(&sink_, size_hint, subc); + } - if (!startstr) return true; - hd = upb_handlers_gethandlerdata(s->handlers, sel); - sub->closure = startstr(s->closure, hd, size_hint); - return sub->closure ? true : false; -} + size_t PutBuffer(void *subc, const char *buf, size_t len, + const upb_bufhandle *handle) { + return upb_bytessink_putbuf(&sink_, subc, buf, len, handle); + } -UPB_INLINE bool upb_sink_endstr(upb_sink *s, upb_selector_t sel) { - typedef upb_endfield_handlerfunc func; - func *endstr; - const void *hd; - if (!s->handlers) return true; - endstr = (func*)upb_handlers_gethandler(s->handlers, sel); + bool End() { + return upb_bytessink_end(&sink_); + } - if (!endstr) return true; - hd = upb_handlers_gethandlerdata(s->handlers, sel); - return endstr(s->closure, hd); -} + private: + upb_bytessink sink_; +}; -UPB_INLINE bool upb_sink_startsubmsg(upb_sink *s, upb_selector_t sel, - upb_sink *sub) { - typedef upb_startfield_handlerfunc func; - func *startsubmsg; - const void *hd; - sub->closure = s->closure; - if (!s->handlers) { - sub->handlers = NULL; - return true; - } - sub->handlers = upb_handlers_getsubhandlers_sel(s->handlers, sel); - startsubmsg = (func*)upb_handlers_gethandler(s->handlers, sel); +#endif /* __cplusplus */ - if (!startsubmsg) return true; - hd = upb_handlers_gethandlerdata(s->handlers, sel); - sub->closure = startsubmsg(s->closure, hd); - return sub->closure ? true : false; -} +/* upb_bufsrc *****************************************************************/ -UPB_INLINE bool upb_sink_endsubmsg(upb_sink *s, upb_selector_t sel) { - typedef upb_endfield_handlerfunc func; - func *endsubmsg; - const void *hd; - if (!s->handlers) return true; - endsubmsg = (func*)upb_handlers_gethandler(s->handlers, sel); +UPB_BEGIN_EXTERN_C - if (!endsubmsg) return s->closure; - hd = upb_handlers_gethandlerdata(s->handlers, sel); - return endsubmsg(s->closure, hd); -} +bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink *sink); UPB_END_EXTERN_C #ifdef __cplusplus namespace upb { - -template Sink::Sink(const Handlers* handlers, T* closure) { - upb_sink_reset(this, handlers, closure); -} -template -inline void Sink::Reset(const Handlers* handlers, T* closure) { - upb_sink_reset(this, handlers, closure); -} -inline bool Sink::StartMessage() { - return upb_sink_startmsg(this); -} -inline bool Sink::EndMessage(Status* status) { - return upb_sink_endmsg(this, status); -} -inline bool Sink::PutInt32(Handlers::Selector sel, int32_t val) { - return upb_sink_putint32(this, sel, val); -} -inline bool Sink::PutInt64(Handlers::Selector sel, int64_t val) { - return upb_sink_putint64(this, sel, val); -} -inline bool Sink::PutUInt32(Handlers::Selector sel, uint32_t val) { - return upb_sink_putuint32(this, sel, val); -} -inline bool Sink::PutUInt64(Handlers::Selector sel, uint64_t val) { - return upb_sink_putuint64(this, sel, val); -} -inline bool Sink::PutFloat(Handlers::Selector sel, float val) { - return upb_sink_putfloat(this, sel, val); -} -inline bool Sink::PutDouble(Handlers::Selector sel, double val) { - return upb_sink_putdouble(this, sel, val); +template bool PutBuffer(const T& str, upb_bytessink* sink) { + return upb_bufsrc_putbuf(str.c_str(), str.size(), sink); } -inline bool Sink::PutBool(Handlers::Selector sel, bool val) { - return upb_sink_putbool(this, sel, val); -} -inline bool Sink::StartString(Handlers::Selector sel, size_t size_hint, - Sink *sub) { - return upb_sink_startstr(this, sel, size_hint, sub); -} -inline size_t Sink::PutStringBuffer(Handlers::Selector sel, const char *buf, - size_t len, const BufferHandle* handle) { - return upb_sink_putstring(this, sel, buf, len, handle); -} -inline bool Sink::EndString(Handlers::Selector sel) { - return upb_sink_endstr(this, sel); -} -inline bool Sink::StartSubMessage(Handlers::Selector sel, Sink* sub) { - return upb_sink_startsubmsg(this, sel, sub); -} -inline bool Sink::EndSubMessage(Handlers::Selector sel) { - return upb_sink_endsubmsg(this, sel); -} -inline bool Sink::StartSequence(Handlers::Selector sel, Sink* sub) { - return upb_sink_startseq(this, sel, sub); -} -inline bool Sink::EndSequence(Handlers::Selector sel) { - return upb_sink_endseq(this, sel); } -template -BytesSink::BytesSink(const BytesHandler* handler, T* closure) { - Reset(handler, closure); -} +#endif /* __cplusplus */ -template -void BytesSink::Reset(const BytesHandler *handler, T *closure) { - upb_bytessink_reset(this, handler, closure); -} -inline bool BytesSink::Start(size_t size_hint, void **subc) { - return upb_bytessink_start(this, size_hint, subc); -} -inline size_t BytesSink::PutBuffer(void *subc, const char *buf, size_t len, - const BufferHandle *handle) { - return upb_bytessink_putbuf(this, subc, buf, len, handle); -} -inline bool BytesSink::End() { - return upb_bytessink_end(this); -} +/* upb_bufsink ****************************************************************/ -inline bool BufferSource::PutBuffer(const char *buf, size_t len, - BytesSink *sink) { - return upb_bufsrc_putbuf(buf, len, sink); -} +/* A class for accumulating output string data in a flat buffer. */ +struct upb_bufsink; +typedef struct upb_bufsink upb_bufsink; -} /* namespace upb */ -#endif +UPB_BEGIN_EXTERN_C + +upb_bufsink *upb_bufsink_init(upb_env *env); +void upb_bufsink_free(upb_bufsink *sink); +upb_bytessink *upb_bufsink_sink(upb_bufsink *sink); +const char *upb_bufsink_getdata(const upb_bufsink *sink, size_t *len); + +UPB_END_EXTERN_C #endif diff --git a/upb/upb.h b/upb/upb.h index 2fb7a88..a75e311 100644 --- a/upb/upb.h +++ b/upb/upb.h @@ -74,7 +74,6 @@ template class InlinedEnvironment; #error Need implementations of [v]snprintf and va_copy #endif - #if ((defined(__cplusplus) && __cplusplus >= 201103L) || \ defined(__GXX_EXPERIMENTAL_CXX0X__)) && !defined(UPB_NO_CXX11) #define UPB_CXX11 @@ -110,28 +109,6 @@ template class InlinedEnvironment; #define UPB_FINAL #endif -/* UPB_DECLARE_TYPE() - * UPB_DECLARE_DERIVED_TYPE() - * UPB_DECLARE_DERIVED_TYPE2() - * - * Macros for declaring C and C++ types both, including inheritance. - * The inheritance doesn't use real C++ inheritance, to stay compatible with C. - * - * These macros also provide upcasts: - * - in C: types-specific functions (ie. upb_foo_upcast(foo)) - * - in C++: upb::upcast(foo) along with implicit conversions - * - * Downcasts are not provided, but upb/def.h defines downcasts for upb::Def. */ - -#define UPB_C_UPCASTS(ty, base) \ - UPB_INLINE base *ty ## _upcast_mutable(ty *p) { return (base*)p; } \ - UPB_INLINE const base *ty ## _upcast(const ty *p) { return (const base*)p; } - -#define UPB_C_UPCASTS2(ty, base, base2) \ - UPB_C_UPCASTS(ty, base) \ - UPB_INLINE base2 *ty ## _upcast2_mutable(ty *p) { return (base2*)p; } \ - UPB_INLINE const base2 *ty ## _upcast2(const ty *p) { return (const base2*)p; } - #ifdef __cplusplus #define UPB_BEGIN_EXTERN_C extern "C" { @@ -139,45 +116,6 @@ template class InlinedEnvironment; #define UPB_PRIVATE_FOR_CPP private: #define UPB_DECLARE_TYPE(cppname, cname) typedef cppname cname; -#define UPB_DECLARE_DERIVED_TYPE(cppname, cppbase, cname, cbase) \ - UPB_DECLARE_TYPE(cppname, cname) \ - UPB_C_UPCASTS(cname, cbase) \ - namespace upb { \ - template <> \ - class Pointer : public PointerBase { \ - public: \ - explicit Pointer(cppname* ptr) \ - : PointerBase(ptr) {} \ - }; \ - template <> \ - class Pointer \ - : public PointerBase { \ - public: \ - explicit Pointer(const cppname* ptr) \ - : PointerBase(ptr) {} \ - }; \ - } - -#define UPB_DECLARE_DERIVED_TYPE2(cppname, cppbase, cppbase2, cname, cbase, \ - cbase2) \ - UPB_DECLARE_TYPE(cppname, cname) \ - UPB_C_UPCASTS2(cname, cbase, cbase2) \ - namespace upb { \ - template <> \ - class Pointer : public PointerBase2 { \ - public: \ - explicit Pointer(cppname* ptr) \ - : PointerBase2(ptr) {} \ - }; \ - template <> \ - class Pointer \ - : public PointerBase2 { \ - public: \ - explicit Pointer(const cppname* ptr) \ - : PointerBase2(ptr) {} \ - }; \ - } - #else /* !defined(__cplusplus) */ #define UPB_BEGIN_EXTERN_C @@ -186,13 +124,6 @@ template class InlinedEnvironment; #define UPB_DECLARE_TYPE(cppname, cname) \ struct cname; \ typedef struct cname cname; -#define UPB_DECLARE_DERIVED_TYPE(cppname, cppbase, cname, cbase) \ - UPB_DECLARE_TYPE(cppname, cname) \ - UPB_C_UPCASTS(cname, cbase) -#define UPB_DECLARE_DERIVED_TYPE2(cppname, cppbase, cppbase2, \ - cname, cbase, cbase2) \ - UPB_DECLARE_TYPE(cppname, cname) \ - UPB_C_UPCASTS2(cname, cbase, cbase2) #endif /* defined(__cplusplus) */ -- cgit v1.2.3 From 48863ea0be94ea3d3d61206ad7ce9ead206770fa Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 12 Jan 2019 19:12:57 -0800 Subject: A lot more tests are working now. --- BUILD | 10 --- tests/json/test_json.cc | 36 +++++----- tests/pb/test_decoder.cc | 157 ++++++++++++++++++++----------------------- tests/test_handlers.c | 42 ------------ tests/test_util.h | 10 +-- upb/bindings/stdc++/string.h | 3 +- upb/handlers-inl.h | 2 +- upb/handlers.c | 11 ++- upb/handlers.h | 2 + upb/json/parser.c | 144 ++++++++++++++++++++------------------- upb/json/parser.h | 140 ++++++++++++++++++++------------------ upb/json/parser.rl | 38 ++++++----- upb/json/printer.c | 32 +++++---- upb/json/printer.h | 68 ++++++++----------- upb/pb/decoder.c | 11 ++- upb/pb/decoder.h | 11 +-- upb/pb/encoder.c | 14 ++-- upb/pb/encoder.h | 12 ++-- upb/sink.c | 8 +-- upb/sink.h | 28 ++++++-- 20 files changed, 375 insertions(+), 404 deletions(-) delete mode 100644 tests/test_handlers.c (limited to 'upb') diff --git a/BUILD b/BUILD index ffb5bbd..c6c401c 100644 --- a/BUILD +++ b/BUILD @@ -166,16 +166,6 @@ upb_proto_reflection_library( deps = ["descriptor_proto"], ) -cc_test( - name = "test_handlers", - srcs = ["tests/test_handlers.c"], - deps = [ - ":descriptor_upbproto", - ":upb_pb", - ":upb_test", - ], -) - proto_library( name = "test_decoder_proto", srcs = [ diff --git a/tests/json/test_json.cc b/tests/json/test_json.cc index b9b50cd..b0fd3e3 100644 --- a/tests/json/test_json.cc +++ b/tests/json/test_json.cc @@ -144,7 +144,7 @@ class StringSink { } ~StringSink() { } - upb_bytessink* Sink() { return &bytessink_; } + upb_bytessink Sink() { return bytessink_; } const std::string& Data() { return s_; } @@ -169,16 +169,15 @@ class StringSink { void test_json_roundtrip_message(const char* json_src, const char* json_expected, const upb::Handlers* serialize_handlers, - const upb::json::ParserMethod* parser_method, + const upb::json::ParserMethodPtr parser_method, int seam) { VerboseParserEnvironment env(verbose); StringSink data_sink; - upb::json::Printer* printer = upb::json::Printer::Create( + upb::json::PrinterPtr printer = upb::json::PrinterPtr::Create( env.env(), serialize_handlers, data_sink.Sink()); - upb::json::Parser* parser = - upb::json::Parser::Create( - env.env(), parser_method, NULL, printer->input(), false); - env.ResetBytesSink(parser->input()); + upb::json::ParserPtr parser = upb::json::ParserPtr::Create( + env.env(), parser_method, NULL, printer.input(), false); + env.ResetBytesSink(parser.input()); env.Reset(json_src, strlen(json_src), false, false); bool ok = env.Start() && @@ -203,16 +202,16 @@ void test_json_roundtrip_message(const char* json_src, // Starts with a message in JSON format, parses and directly serializes again, // and compares the result. void test_json_roundtrip() { - upb::SymbolTable* symtab = upb::SymbolTable::New(); - upb::HandlerCache* serialize_handlercache = upb::json::Printer::NewCache(false); - upb::json::CodeCache* parse_codecache = upb::json::CodeCache::New(); + upb::SymbolTable symtab; + upb::HandlerCache serialize_handlercache( + upb::json::PrinterPtr::NewCache(false)); + upb::json::CodeCache parse_codecache; - const upb::MessageDef* md = upb_test_json_TestMessage_getmsgdef(symtab); + upb::MessageDefPtr md(upb_test_json_TestMessage_getmsgdef(symtab.ptr())); ASSERT(md); - const upb::Handlers* serialize_handlers = serialize_handlercache->Get(md); - const upb::json::ParserMethod* parser_method = parse_codecache->Get(md); + const upb::Handlers* serialize_handlers = serialize_handlercache.Get(md); + const upb::json::ParserMethodPtr parser_method = parse_codecache.Get(md); ASSERT(serialize_handlers); - ASSERT(parser_method); for (const TestCase* test_case = kTestRoundtripMessages; test_case->input != NULL; test_case++) { @@ -227,9 +226,8 @@ void test_json_roundtrip() { } } - upb::HandlerCache::Free(serialize_handlercache); - serialize_handlercache = upb::json::Printer::NewCache(true); - serialize_handlers = serialize_handlercache->Get(md); + serialize_handlercache = upb::json::PrinterPtr::NewCache(true); + serialize_handlers = serialize_handlercache.Get(md); for (const TestCase* test_case = kTestRoundtripMessagesPreserve; test_case->input != NULL; test_case++) { @@ -243,10 +241,6 @@ void test_json_roundtrip() { serialize_handlers, parser_method, i); } } - - upb::HandlerCache::Free(serialize_handlercache); - upb::json::CodeCache::Free(parse_codecache); - upb::SymbolTable::Free(symtab); } extern "C" { diff --git a/tests/pb/test_decoder.cc b/tests/pb/test_decoder.cc index d0e3fa3..ec7a788 100644 --- a/tests/pb/test_decoder.cc +++ b/tests/pb/test_decoder.cc @@ -279,7 +279,7 @@ int* startstr(int* depth, const uint32_t* num, size_t size_hint) { } size_t value_string(int* depth, const uint32_t* num, const char* buf, - size_t n, const upb::BufferHandle* handle) { + size_t n, const upb_bufhandle* handle) { UPB_UNUSED(num); UPB_UNUSED(depth); check_stack_alignment(); @@ -348,13 +348,13 @@ void free_uint32(void *val) { } template -void doreg(upb_handlers *h, uint32_t num) { - const upb_fielddef *f = upb_msgdef_itof(upb_handlers_msgdef(h), num); +void doreg(upb::HandlersPtr h, uint32_t num) { + upb::FieldDefPtr f = h.message_def().FindFieldByNumber(num); ASSERT(f); - ASSERT(h->SetValueHandler(f, UpbBindT(F, new uint32_t(num)))); - if (f->IsSequence()) { - ASSERT(h->SetStartSequenceHandler(f, UpbBind(startseq, new uint32_t(num)))); - ASSERT(h->SetEndSequenceHandler(f, UpbBind(endseq, new uint32_t(num)))); + ASSERT(h.SetValueHandler(f, UpbBind(F, new uint32_t(num)))); + if (f.IsSequence()) { + ASSERT(h.SetStartSequenceHandler(f, UpbBind(startseq, new uint32_t(num)))); + ASSERT(h.SetEndSequenceHandler(f, UpbBind(endseq, new uint32_t(num)))); } } @@ -368,7 +368,7 @@ uint32_t rep_fn(uint32_t fn) { #define UNKNOWN_FIELD 666 template -void reg(upb_handlers *h, upb_descriptortype_t type) { +void reg(upb::HandlersPtr h, upb_descriptortype_t type) { // We register both a repeated and a non-repeated field for every type. // For the non-repeated field we make the field number the same as the // type. For the repeated field we make it a function of the type. @@ -376,39 +376,40 @@ void reg(upb_handlers *h, upb_descriptortype_t type) { doreg(h, rep_fn(type)); } -void regseq(upb::Handlers* h, const upb::FieldDef* f, uint32_t num) { - ASSERT(h->SetStartSequenceHandler(f, UpbBind(startseq, new uint32_t(num)))); - ASSERT(h->SetEndSequenceHandler(f, UpbBind(endseq, new uint32_t(num)))); +void regseq(upb::HandlersPtr h, upb::FieldDefPtr f, uint32_t num) { + ASSERT(h.SetStartSequenceHandler(f, UpbBind(startseq, new uint32_t(num)))); + ASSERT(h.SetEndSequenceHandler(f, UpbBind(endseq, new uint32_t(num)))); } -void reg_subm(upb_handlers *h, uint32_t num) { - const upb_fielddef *f = upb_msgdef_itof(upb_handlers_msgdef(h), num); +void reg_subm(upb::HandlersPtr h, uint32_t num) { + upb::FieldDefPtr f = h.message_def().FindFieldByNumber(num); ASSERT(f); - if (f->IsSequence()) regseq(h, f, num); + if (f.IsSequence()) regseq(h, f, num); ASSERT( - h->SetStartSubMessageHandler(f, UpbBind(startsubmsg, new uint32_t(num)))); - ASSERT(h->SetEndSubMessageHandler(f, UpbBind(endsubmsg, new uint32_t(num)))); + h.SetStartSubMessageHandler(f, UpbBind(startsubmsg, new uint32_t(num)))); + ASSERT(h.SetEndSubMessageHandler(f, UpbBind(endsubmsg, new uint32_t(num)))); } -void reg_str(upb_handlers *h, uint32_t num) { - const upb_fielddef *f = upb_msgdef_itof(upb_handlers_msgdef(h), num); +void reg_str(upb::HandlersPtr h, uint32_t num) { + upb::FieldDefPtr f = h.message_def().FindFieldByNumber(num); ASSERT(f); - if (f->IsSequence()) regseq(h, f, num); - ASSERT(h->SetStartStringHandler(f, UpbBind(startstr, new uint32_t(num)))); - ASSERT(h->SetEndStringHandler(f, UpbBind(endstr, new uint32_t(num)))); - ASSERT(h->SetStringHandler(f, UpbBind(value_string, new uint32_t(num)))); + if (f.IsSequence()) regseq(h, f, num); + ASSERT(h.SetStartStringHandler(f, UpbBind(startstr, new uint32_t(num)))); + ASSERT(h.SetEndStringHandler(f, UpbBind(endstr, new uint32_t(num)))); + ASSERT(h.SetStringHandler(f, UpbBind(value_string, new uint32_t(num)))); } struct HandlerRegisterData { TestMode mode; }; -void callback(const void *closure, upb_handlers *h) { +void callback(const void *closure, upb::Handlers* h_ptr) { + upb::HandlersPtr h(h_ptr); const HandlerRegisterData* data = static_cast(closure); if (data->mode == ALL_HANDLERS) { - h->SetStartMessageHandler(UpbMakeHandler(startmsg)); - h->SetEndMessageHandler(UpbMakeHandler(endmsg)); + h.SetStartMessageHandler(UpbMakeHandler(startmsg)); + h.SetEndMessageHandler(UpbMakeHandler(endmsg)); // Register handlers for each type. reg(h, UPB_DESCRIPTOR_TYPE_DOUBLE); @@ -436,7 +437,7 @@ void callback(const void *closure, upb_handlers *h) { reg_subm(h, UPB_DESCRIPTOR_TYPE_MESSAGE); reg_subm(h, rep_fn(UPB_DESCRIPTOR_TYPE_MESSAGE)); - if (h->message_def()->full_name() == std::string("DecoderTest")) { + if (h.message_def().full_name() == std::string("DecoderTest")) { reg_subm(h, UPB_DESCRIPTOR_TYPE_GROUP); reg_subm(h, rep_fn(UPB_DESCRIPTOR_TYPE_GROUP)); } @@ -446,25 +447,16 @@ void callback(const void *closure, upb_handlers *h) { } } -upb::reffed_ptr NewHandlers(upb::SymbolTable* symtab, - TestMode mode) { - HandlerRegisterData handlerdata; - handlerdata.mode = mode; - return upb::Handlers::NewFrozen(DecoderTest_getmsgdef(symtab), callback, - &handlerdata); -} - /* Running of test cases ******************************************************/ const upb::Handlers *global_handlers; -const upb::pb::DecoderMethod *global_method; - -upb::pb::Decoder* CreateDecoder(upb::Environment* env, - const upb::pb::DecoderMethod* method, - upb::Sink* sink) { - upb::pb::Decoder *ret = upb::pb::Decoder::Create(env, method, sink); - ASSERT(ret != NULL); - ret->set_max_nesting(MAX_NESTING); +upb::pb::DecoderMethodPtr global_method; + +upb::pb::DecoderPtr CreateDecoder(upb::Environment* env, + upb::pb::DecoderMethodPtr method, + upb::Sink sink) { + upb::pb::DecoderPtr ret = upb::pb::DecoderPtr::Create(env, method, sink); + ret.set_max_nesting(MAX_NESTING); return ret; } @@ -479,7 +471,7 @@ uint32_t Hash(const string& proto, const string* expected_output, size_t seam1, return hash; } -void CheckBytesParsed(const upb::pb::Decoder& decoder, size_t ofs) { +void CheckBytesParsed(upb::pb::DecoderPtr decoder, size_t ofs) { // We can't have parsed more data than the decoder callback is telling us it // parsed. ASSERT(decoder.BytesParsed() <= ofs); @@ -491,7 +483,7 @@ void CheckBytesParsed(const upb::pb::Decoder& decoder, size_t ofs) { } static bool parse(VerboseParserEnvironment* env, - const upb::pb::Decoder& decoder, int bytes) { + upb::pb::DecoderPtr decoder, int bytes) { CheckBytesParsed(decoder, env->ofs()); bool ret = env->ParseBuffer(bytes); if (ret) { @@ -501,11 +493,11 @@ static bool parse(VerboseParserEnvironment* env, return ret; } -void do_run_decoder(VerboseParserEnvironment* env, upb::pb::Decoder* decoder, +void do_run_decoder(VerboseParserEnvironment* env, upb::pb::DecoderPtr decoder, const string& proto, const string* expected_output, size_t i, size_t j, bool may_skip) { env->Reset(proto.c_str(), proto.size(), may_skip, expected_output == NULL); - decoder->Reset(); + decoder.Reset(); testhash = Hash(proto, expected_output, i, j, may_skip); if (filter_hash && testhash != filter_hash) return; @@ -515,7 +507,7 @@ void do_run_decoder(VerboseParserEnvironment* env, upb::pb::Decoder* decoder, if (filter_hash) { fprintf(stderr, "RUNNING TEST CASE, hash=%x\n", testhash); fprintf(stderr, "JIT on: %s\n", - global_method->is_native() ? "true" : "false"); + global_method.is_native() ? "true" : "false"); fprintf(stderr, "Input (len=%u): ", (unsigned)proto.size()); PrintBinary(proto); fprintf(stderr, "\n"); @@ -534,9 +526,9 @@ void do_run_decoder(VerboseParserEnvironment* env, upb::pb::Decoder* decoder, } bool ok = env->Start() && - parse(env, *decoder, i) && - parse(env, *decoder, j - i) && - parse(env, *decoder, -1) && + parse(env, decoder, i) && + parse(env, decoder, j - i) && + parse(env, decoder, -1) && env->End(); ASSERT(env->CheckConsistency()); @@ -564,8 +556,8 @@ void do_run_decoder(VerboseParserEnvironment* env, upb::pb::Decoder* decoder, void run_decoder(const string& proto, const string* expected_output) { VerboseParserEnvironment env(filter_hash != 0); upb::Sink sink(global_handlers, &closures[0]); - upb::pb::Decoder *decoder = CreateDecoder(env.env(), global_method, &sink); - env.ResetBytesSink(decoder->input()); + upb::pb::DecoderPtr decoder = CreateDecoder(env.env(), global_method, sink); + env.ResetBytesSink(decoder.input()); for (size_t i = 0; i < proto.size(); i++) { for (size_t j = i; j < UPB_MIN(proto.size(), i + 5); j++) { do_run_decoder(&env, decoder, proto, expected_output, i, j, true); @@ -883,9 +875,9 @@ void test_valid() { upb::Environment env; env.ReportErrorsTo(&status); upb::Sink sink(global_handlers, &closures[0]); - upb::pb::Decoder* decoder = CreateDecoder(&env, global_method, &sink); + upb::pb::DecoderPtr decoder = CreateDecoder(&env, global_method, sink); output.clear(); - bool ok = upb::BufferSource::PutBuffer("", 0, decoder->input()); + bool ok = upb::PutBuffer(std::string(), decoder.input()); ASSERT(ok); ASSERT(status.ok()); if (test_mode == ALL_HANDLERS) { @@ -1133,23 +1125,22 @@ void test_valid() { run_decoder(buf, &textbuf); } -upb::reffed_ptr NewMethod( - const upb::Handlers* dest_handlers, bool allow_jit) { - upb::pb::CodeCache cache; - cache.set_allow_jit(allow_jit); - return cache.GetDecoderMethod(upb::pb::DecoderMethodOptions(dest_handlers)); -} +void empty_callback(const void *closure, upb::Handlers* h_ptr) {} void test_emptyhandlers(upb::SymbolTable* symtab, bool allowjit) { // Create an empty handlers to make sure that the decoder can handle empty // messages. - const upb::MessageDef* md = Empty_getmsgdef(symtab); - upb::reffed_ptr h(upb::Handlers::New(md)); - bool ok = h->Freeze(NULL); - ASSERT(ok); -upb::reffed_ptr method = - NewMethod(h.get(), allowjit); - ASSERT(method.get()); + HandlerRegisterData handlerdata; + handlerdata.mode = test_mode; + + upb::HandlerCache handler_cache(empty_callback, &handlerdata); + upb::pb::CodeCache pb_code_cache(&handler_cache); + + pb_code_cache.set_allow_jit(allowjit); + + upb::MessageDefPtr md = upb::MessageDefPtr(Empty_getmsgdef(symtab->ptr())); + global_handlers = handler_cache.Get(md); + global_method = pb_code_cache.Get(md); // TODO: also test the case where a message has fields, but the fields are // submessage fields and have no handlers. This also results in a decoder @@ -1169,9 +1160,9 @@ upb::reffed_ptr method = }; for (int i = 0; testdata[i].data; i++) { VerboseParserEnvironment env(filter_hash != 0); - upb::Sink sink(method->dest_handlers(), &closures[0]); - upb::pb::Decoder* decoder = CreateDecoder(env.env(), method.get(), &sink); - env.ResetBytesSink(decoder->input()); + upb::Sink sink(global_method.dest_handlers(), &closures[0]); + upb::pb::DecoderPtr decoder = CreateDecoder(env.env(), global_method, sink); + env.ResetBytesSink(decoder.input()); env.Reset(testdata[i].data, testdata[i].length, true, false); ASSERT(env.Start()); ASSERT(env.ParseBuffer(-1)); @@ -1181,24 +1172,25 @@ upb::reffed_ptr method = } void run_tests(bool use_jit) { - upb::reffed_ptr method; - upb::reffed_ptr handlers; - upb::SymbolTable* symtab = upb::SymbolTable::New(); + HandlerRegisterData handlerdata; + handlerdata.mode = test_mode; - handlers = NewHandlers(symtab, test_mode); - global_handlers = handlers.get(); + upb::SymbolTable symtab; + upb::HandlerCache handler_cache(callback, &handlerdata); + upb::pb::CodeCache pb_code_cache(&handler_cache); - method = NewMethod(handlers.get(), use_jit); - global_method = method.get(); - ASSERT(use_jit == global_method->is_native()); + pb_code_cache.set_allow_jit(use_jit); + + upb::MessageDefPtr md(DecoderTest_getmsgdef(symtab.ptr())); + global_handlers = handler_cache.Get(md); + global_method = pb_code_cache.Get(md); + ASSERT(use_jit == global_method.is_native()); completed = 0; test_invalid(); test_valid(); - test_emptyhandlers(symtab, use_jit); - - upb::SymbolTable::Free(symtab); + test_emptyhandlers(&symtab, use_jit); } void run_test_suite() { @@ -1218,9 +1210,6 @@ int run_tests(int argc, char *argv[]) { closures[i] = i; } - upb::reffed_ptr method; - upb::reffed_ptr handlers; - // Count tests. count = &total; total = 0; diff --git a/tests/test_handlers.c b/tests/test_handlers.c deleted file mode 100644 index 2b19cab..0000000 --- a/tests/test_handlers.c +++ /dev/null @@ -1,42 +0,0 @@ - -#include -#include -#include "google/protobuf/descriptor.upbdefs.h" -#include "upb/handlers.h" -#include "upb_test.h" - -static bool startmsg(void *c, const void *hd) { - UPB_UNUSED(c); - UPB_UNUSED(hd); - return true; -} - -static void test_error() { - /* Test creating handlers of a static msgdef. */ - upb_symtab *s = upb_symtab_new(); - const upb_msgdef *m = google_protobuf_DescriptorProto_getmsgdef(s); - upb_handlers *h = upb_handlers_new(m, &h); - - /* Attempt to set the same handler twice causes error. */ - ASSERT(upb_ok(upb_handlers_status(h))); - upb_handlers_setstartmsg(h, &startmsg, NULL); - ASSERT(upb_ok(upb_handlers_status(h))); - upb_handlers_setstartmsg(h, &startmsg, NULL); - ASSERT(!upb_ok(upb_handlers_status(h))); - ASSERT(!upb_handlers_freeze(&h, 1, NULL)); - - /* Clearing the error will let us proceed. */ - upb_handlers_clearerr(h); - ASSERT(upb_handlers_freeze(&h, 1, NULL)); - ASSERT(upb_handlers_isfrozen(h)); - - upb_handlers_unref(h, &h); - upb_symtab_free(s); -} - -int run_tests(int argc, char *argv[]) { - UPB_UNUSED(argc); - UPB_UNUSED(argv); - test_error(); - return 0; -} diff --git a/tests/test_util.h b/tests/test_util.h index 1b1ff01..0b5ddd4 100644 --- a/tests/test_util.h +++ b/tests/test_util.h @@ -78,14 +78,14 @@ class VerboseParserEnvironment { if (verbose_) { fprintf(stderr, "Calling start()\n"); } - return sink_->Start(len_, &subc_); + return sink_.Start(len_, &subc_); } bool End() { if (verbose_) { fprintf(stderr, "Calling end()\n"); } - end_ok_ = sink_->End(); + end_ok_ = sink_.End(); end_ok_set_ = true; return end_ok_; @@ -137,7 +137,7 @@ class VerboseParserEnvironment { (unsigned)bytes, (unsigned)ofs_, (unsigned)(ofs_ + bytes)); } - int parsed = sink_->PutBuffer(subc_, buf2, bytes, &global_handle); + int parsed = sink_.PutBuffer(subc_, buf2, bytes, &global_handle); free(buf2); if (verbose_) { @@ -170,7 +170,7 @@ class VerboseParserEnvironment { return true; } - void ResetBytesSink(upb::BytesSink* sink) { + void ResetBytesSink(upb::BytesSink sink) { sink_ = sink; } @@ -181,7 +181,7 @@ class VerboseParserEnvironment { private: upb::Environment env_; - upb::BytesSink* sink_; + upb::BytesSink sink_; const char* buf_; size_t len_; bool verbose_; diff --git a/upb/bindings/stdc++/string.h b/upb/bindings/stdc++/string.h index 4d7a719..55b44cf 100644 --- a/upb/bindings/stdc++/string.h +++ b/upb/bindings/stdc++/string.h @@ -48,11 +48,12 @@ class StringSink { explicit StringSink(T* target) { // TODO(haberman): we need to avoid rebuilding a new handler every time, // but with class globals disallowed for google3 C++ this is tricky. + upb_byteshandler_init(&handler_); FillStringHandler::SetHandler(&handler_); input_.Reset(&handler_, target); } - BytesSink* input() { return &input_; } + BytesSink input() { return input_; } private: upb_byteshandler handler_; diff --git a/upb/handlers-inl.h b/upb/handlers-inl.h index b038e30..5677a4a 100644 --- a/upb/handlers-inl.h +++ b/upb/handlers-inl.h @@ -858,7 +858,7 @@ inline Handler::Handler(F func) : registered_(false), cleanup_data_(func.GetData()), cleanup_func_(func.GetCleanup()) { - upb_handlerattr_sethandlerdata(&attr_, func.GetData()); + attr_.handler_data = func.GetData(); typedef typename ReturnOf::Return Return; typedef typename ConvertParams::Func ConvertedParamsFunc; typedef typename MaybeWrapReturn::Func diff --git a/upb/handlers.c b/upb/handlers.c index fd81b03..ba27b98 100644 --- a/upb/handlers.c +++ b/upb/handlers.c @@ -286,6 +286,10 @@ const upb_handlers *upb_handlers_getsubhandlers_sel(const upb_handlers *h, const upb_msgdef *upb_handlers_msgdef(const upb_handlers *h) { return h->msg; } +bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *func) { + return upb_handlercache_addcleanup(h->cache, p, func); +} + upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f) { switch (upb_fielddef_type(f)) { case UPB_TYPE_INT32: @@ -470,12 +474,13 @@ void upb_handlercache_free(upb_handlercache *cache) { upb_gfree(cache); } -bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *func) { +bool upb_handlercache_addcleanup(upb_handlercache *c, void *p, + upb_handlerfree *func) { bool ok; - if (upb_inttable_lookupptr(&h->cache->cleanup_, p, NULL)) { + if (upb_inttable_lookupptr(&c->cleanup_, p, NULL)) { return false; } - ok = upb_inttable_insertptr(&h->cache->cleanup_, p, upb_value_fptr(func)); + ok = upb_inttable_insertptr(&c->cleanup_, p, upb_value_fptr(func)); UPB_ASSERT(ok); return true; } diff --git a/upb/handlers.h b/upb/handlers.h index 4558786..44cad18 100644 --- a/upb/handlers.h +++ b/upb/handlers.h @@ -602,6 +602,8 @@ upb_handlercache *upb_handlercache_new(upb_handlers_callback *callback, void upb_handlercache_free(upb_handlercache *cache); const upb_handlers *upb_handlercache_get(upb_handlercache *cache, const upb_msgdef *md); +bool upb_handlercache_addcleanup(upb_handlercache *h, void *p, + upb_handlerfree *hfree); UPB_END_EXTERN_C diff --git a/upb/json/parser.c b/upb/json/parser.c index 4bc9163..1dac800 100644 --- a/upb/json/parser.c +++ b/upb/json/parser.c @@ -301,13 +301,13 @@ static void json_parser_any_frame_set_payload_type( /* Initialize encoder. */ h = upb_handlercache_get(frame->encoder_handlercache, payload_type); - encoder = upb_pb_encoder_create(p->env, h, &frame->stringsink.sink); + encoder = upb_pb_encoder_create(p->env, h, frame->stringsink.sink); /* Initialize parser. */ parser_method = upb_json_codecache_get(frame->parser_codecache, payload_type); upb_sink_reset(&frame->sink, h, encoder); frame->parser = upb_json_parser_create(p->env, parser_method, p->symtab, - &frame->sink, p->ignore_json_unknown); + frame->sink, p->ignore_json_unknown); } static void json_parser_any_frame_free(upb_jsonparser_any_frame *frame) { @@ -383,9 +383,12 @@ static bool check_stack(upb_json_parser *p) { static void set_name_table(upb_json_parser *p, upb_jsonparser_frame *frame) { upb_value v; const upb_json_codecache *cache = p->method->cache; - bool ok = upb_inttable_lookupptr(&cache->methods, frame->m, &v); - const upb_json_parsermethod *method = upb_value_getptr(v); + bool ok; + const upb_json_parsermethod *method; + + ok = upb_inttable_lookupptr(&cache->methods, frame->m, &v); UPB_ASSERT(ok); + method = upb_value_getconstptr(v); frame->name_table = &method->name_table; } @@ -1287,7 +1290,7 @@ static bool end_any_stringval(upb_json_parser *p) { } json_parser_any_frame_set_payload_type(p, p->top->any_frame, payload_type); - + return true; } else { upb_status_seterrf( @@ -2416,11 +2419,11 @@ static bool is_string_wrapper_object(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 2578 "upb/json/parser.rl" +#line 2581 "upb/json/parser.rl" -#line 2424 "upb/json/parser.c" +#line 2427 "upb/json/parser.c" static const char _json_actions[] = { 0, 1, 0, 1, 1, 1, 3, 1, 4, 1, 6, 1, 7, 1, 8, 1, @@ -2667,7 +2670,7 @@ static const int json_en_value_machine = 75; static const int json_en_main = 1; -#line 2581 "upb/json/parser.rl" +#line 2584 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -2690,7 +2693,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 2694 "upb/json/parser.c" +#line 2697 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -2765,83 +2768,83 @@ _match: switch ( *_acts++ ) { case 1: -#line 2429 "upb/json/parser.rl" +#line 2432 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 2431 "upb/json/parser.rl" +#line 2434 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 23;goto _again;} } break; case 3: -#line 2435 "upb/json/parser.rl" +#line 2438 "upb/json/parser.rl" { start_text(parser, p); } break; case 4: -#line 2436 "upb/json/parser.rl" +#line 2439 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -#line 2442 "upb/json/parser.rl" +#line 2445 "upb/json/parser.rl" { start_hex(parser); } break; case 6: -#line 2443 "upb/json/parser.rl" +#line 2446 "upb/json/parser.rl" { hexdigit(parser, p); } break; case 7: -#line 2444 "upb/json/parser.rl" +#line 2447 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -#line 2450 "upb/json/parser.rl" +#line 2453 "upb/json/parser.rl" { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -#line 2456 "upb/json/parser.rl" +#line 2459 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 10: -#line 2468 "upb/json/parser.rl" +#line 2471 "upb/json/parser.rl" { start_duration_base(parser, p); } break; case 11: -#line 2469 "upb/json/parser.rl" +#line 2472 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_duration_base(parser, p)); } break; case 12: -#line 2471 "upb/json/parser.rl" +#line 2474 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 13: -#line 2476 "upb/json/parser.rl" +#line 2479 "upb/json/parser.rl" { start_timestamp_base(parser, p); } break; case 14: -#line 2477 "upb/json/parser.rl" +#line 2480 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_base(parser, p)); } break; case 15: -#line 2479 "upb/json/parser.rl" +#line 2482 "upb/json/parser.rl" { start_timestamp_fraction(parser, p); } break; case 16: -#line 2480 "upb/json/parser.rl" +#line 2483 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } break; case 17: -#line 2482 "upb/json/parser.rl" +#line 2485 "upb/json/parser.rl" { start_timestamp_zone(parser, p); } break; case 18: -#line 2483 "upb/json/parser.rl" +#line 2486 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } break; case 19: -#line 2485 "upb/json/parser.rl" +#line 2488 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 20: -#line 2490 "upb/json/parser.rl" +#line 2493 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) { {stack[top++] = cs; cs = 47;goto _again;} @@ -2853,11 +2856,11 @@ _match: } break; case 21: -#line 2501 "upb/json/parser.rl" +#line 2504 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 75;goto _again;} } break; case 22: -#line 2506 "upb/json/parser.rl" +#line 2509 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_member(parser, p); @@ -2867,11 +2870,11 @@ _match: } break; case 23: -#line 2513 "upb/json/parser.rl" +#line 2516 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_membername(parser)); } break; case 24: -#line 2516 "upb/json/parser.rl" +#line 2519 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { end_any_member(parser, p); @@ -2881,7 +2884,7 @@ _match: } break; case 25: -#line 2527 "upb/json/parser.rl" +#line 2530 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_object(parser, p); @@ -2891,7 +2894,7 @@ _match: } break; case 26: -#line 2536 "upb/json/parser.rl" +#line 2539 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { CHECK_RETURN_TOP(end_any_object(parser, p)); @@ -2901,54 +2904,54 @@ _match: } break; case 27: -#line 2548 "upb/json/parser.rl" +#line 2551 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_array(parser)); } break; case 28: -#line 2552 "upb/json/parser.rl" +#line 2555 "upb/json/parser.rl" { end_array(parser); } break; case 29: -#line 2557 "upb/json/parser.rl" +#line 2560 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_number(parser, p)); } break; case 30: -#line 2558 "upb/json/parser.rl" +#line 2561 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 31: -#line 2560 "upb/json/parser.rl" +#line 2563 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_stringval(parser)); } break; case 32: -#line 2561 "upb/json/parser.rl" +#line 2564 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_stringval(parser)); } break; case 33: -#line 2563 "upb/json/parser.rl" +#line 2566 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 34: -#line 2565 "upb/json/parser.rl" +#line 2568 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 35: -#line 2567 "upb/json/parser.rl" +#line 2570 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 36: -#line 2569 "upb/json/parser.rl" +#line 2572 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_subobject_full(parser)); } break; case 37: -#line 2570 "upb/json/parser.rl" +#line 2573 "upb/json/parser.rl" { end_subobject_full(parser); } break; case 38: -#line 2575 "upb/json/parser.rl" +#line 2578 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 2952 "upb/json/parser.c" +#line 2955 "upb/json/parser.c" } } @@ -2965,32 +2968,32 @@ _again: while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 0: -#line 2427 "upb/json/parser.rl" +#line 2430 "upb/json/parser.rl" { p--; {cs = stack[--top]; if ( p == pe ) goto _test_eof; goto _again;} } break; case 30: -#line 2558 "upb/json/parser.rl" +#line 2561 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 33: -#line 2563 "upb/json/parser.rl" +#line 2566 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 34: -#line 2565 "upb/json/parser.rl" +#line 2568 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 35: -#line 2567 "upb/json/parser.rl" +#line 2570 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 37: -#line 2570 "upb/json/parser.rl" +#line 2573 "upb/json/parser.rl" { end_subobject_full(parser); } break; -#line 2994 "upb/json/parser.c" +#line 2997 "upb/json/parser.c" } } } @@ -2998,7 +3001,7 @@ goto _again;} } _out: {} } -#line 2603 "upb/json/parser.rl" +#line 2606 "upb/json/parser.rl" if (p != pe) { upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p); @@ -3046,13 +3049,13 @@ static void json_parser_reset(upb_json_parser *p) { /* Emit Ragel initialization of the parser. */ -#line 3050 "upb/json/parser.c" +#line 3053 "upb/json/parser.c" { cs = json_start; top = 0; } -#line 2650 "upb/json/parser.rl" +#line 2653 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); @@ -3067,7 +3070,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c, upb_msg_field_iter i; upb_alloc *alloc = upb_arena_alloc(&c->arena); - upb_json_parsermethod *m = upb_gmalloc(sizeof(*m)); + upb_json_parsermethod *m = upb_malloc(alloc, sizeof(*m)); m->cache = c; @@ -3090,7 +3093,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c, size_t len = upb_fielddef_getjsonname(f, NULL, 0); buf = upb_malloc(alloc, len); upb_fielddef_getjsonname(f, buf, len); - upb_strtable_insert3(&m->name_table, buf, len, v, alloc); + upb_strtable_insert3(&m->name_table, buf, strlen(buf), v, alloc); if (strcmp(buf, upb_fielddef_name(f)) != 0) { /* Since the JSON name is different from the regular field name, add an @@ -3109,7 +3112,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c, upb_json_parser *upb_json_parser_create(upb_env *env, const upb_json_parsermethod *method, const upb_symtab* symtab, - upb_sink *output, + upb_sink output, bool ignore_json_unknown) { #ifndef NDEBUG const size_t size_before = upb_env_bytesallocated(env); @@ -3125,8 +3128,8 @@ upb_json_parser *upb_json_parser_create(upb_env *env, upb_bytessink_reset(&p->input_, &method->input_handler_, p); json_parser_reset(p); - upb_sink_reset(&p->top->sink, output->handlers, output->closure); - p->top->m = upb_handlers_msgdef(output->handlers); + p->top->sink = output; + p->top->m = upb_handlers_msgdef(output.handlers); if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) { p->top->is_any = true; p->top->any_frame = json_parser_any_frame_new(p); @@ -3146,8 +3149,8 @@ upb_json_parser *upb_json_parser_create(upb_env *env, return p; } -upb_bytessink *upb_json_parser_input(upb_json_parser *p) { - return &p->input_; +upb_bytessink upb_json_parser_input(upb_json_parser *p) { + return p->input_; } const upb_byteshandler *upb_json_parsermethod_inputhandler( @@ -3174,21 +3177,22 @@ void upb_json_codecache_free(upb_json_codecache *c) { upb_gfree(c); } -upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c, - const upb_msgdef *md) { +const upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c, + const upb_msgdef *md) { upb_json_parsermethod *m; upb_value v; upb_msg_field_iter i; + upb_alloc *alloc = upb_arena_alloc(&c->arena); if (upb_inttable_lookupptr(&c->methods, md, &v)) { - return upb_value_getptr(v); + return upb_value_getconstptr(v); } m = parsermethod_new(c, md); - v = upb_value_ptr(m); + v = upb_value_constptr(m); if (!m) return NULL; - if (!upb_inttable_insertptr(&c->methods, m, v)) return NULL; + if (!upb_inttable_insertptr2(&c->methods, md, v, alloc)) return NULL; /* Populate parser methods for all submessages, so the name tables will * be available during parsing. */ diff --git a/upb/json/parser.h b/upb/json/parser.h index d5ec396..d1a1471 100644 --- a/upb/json/parser.h +++ b/upb/json/parser.h @@ -14,17 +14,44 @@ namespace upb { namespace json { class CodeCache; -class Parser; -class ParserMethod; +class ParserPtr; +class ParserMethodPtr; } /* namespace json */ } /* namespace upb */ #endif -UPB_DECLARE_TYPE(upb::json::Parser, upb_json_parser) -UPB_DECLARE_TYPE(upb::json::ParserMethod, upb_json_parsermethod) -UPB_DECLARE_TYPE(upb::json::CodeCache, upb_json_codecache) +/* upb_json_parsermethod ******************************************************/ -/* upb::json::Parser **********************************************************/ +struct upb_json_parsermethod; +typedef struct upb_json_parsermethod upb_json_parsermethod; + +UPB_BEGIN_EXTERN_C + +const upb_byteshandler* upb_json_parsermethod_inputhandler( + const upb_json_parsermethod* m); + +UPB_END_EXTERN_C + +#ifdef __cplusplus + +class upb::json::ParserMethodPtr { + public: + ParserMethodPtr() : ptr_(nullptr) {} + ParserMethodPtr(const upb_json_parsermethod* ptr) : ptr_(ptr) {} + + const upb_json_parsermethod* ptr() const { return ptr_; } + + const BytesHandler* input_handler() const { + return upb_json_parsermethod_inputhandler(ptr()); + } + + private: + const upb_json_parsermethod* ptr_; +}; + +#endif /* __cplusplus */ + +/* upb_json_parser ************************************************************/ /* Preallocation hint: parser won't allocate more bytes than this when first * constructed. This hint may be an overestimate for some build configurations. @@ -32,89 +59,72 @@ UPB_DECLARE_TYPE(upb::json::CodeCache, upb_json_codecache) * it may be an underestimate. */ #define UPB_JSON_PARSER_SIZE 5712 +struct upb_json_parser; +typedef struct upb_json_parser upb_json_parser; + +UPB_BEGIN_EXTERN_C + +upb_json_parser* +upb_json_parser_create(upb_env* e, const upb_json_parsermethod* m, + const upb_symtab* symtab, upb_sink output, + bool ignore_json_unknown); +upb_bytessink upb_json_parser_input(upb_json_parser* p); + +UPB_END_EXTERN_C + #ifdef __cplusplus /* Parses an incoming BytesStream, pushing the results to the destination * sink. */ -class upb::json::Parser { +class upb::json::ParserPtr { public: - static Parser* Create(Environment* env, const ParserMethod* method, - const SymbolTable* symtab, - Sink* output, bool ignore_json_unknown); + ParserPtr(upb_json_parser* ptr) : ptr_(ptr) {} - BytesSink* input(); + static ParserPtr Create(Environment* env, ParserMethodPtr method, + SymbolTable* symtab, Sink output, + bool ignore_json_unknown) { + upb_symtab* symtab_ptr = symtab ? symtab->ptr() : nullptr; + return ParserPtr(upb_json_parser_create( + env, method.ptr(), symtab_ptr, output.sink(), ignore_json_unknown)); + } - private: - UPB_DISALLOW_POD_OPS(Parser, upb::json::Parser) -}; - -class upb::json::ParserMethod { - public: - /* The input handlers for this decoder method. */ - const BytesHandler* input_handler() const; + BytesSink input() { return upb_json_parser_input(ptr_); } private: - UPB_DISALLOW_POD_OPS(ParserMethod, upb::json::ParserMethod) + upb_json_parser* ptr_; }; -class upb::json::CodeCache { - public: - static CodeCache* New(); - static void Free(CodeCache* cache); - - /* Returns a DecoderMethod that can push data to the given handlers. - * If a suitable method already exists, it will be returned from the cache. */ - const ParserMethod *Get(const MessageDef* md); +#endif /* __cplusplus */ - private: - UPB_DISALLOW_POD_OPS(CodeCache, upb::json::CodeCache) -}; +/* upb_json_codecache *********************************************************/ -#endif +struct upb_json_codecache; +typedef struct upb_json_codecache upb_json_codecache; UPB_BEGIN_EXTERN_C -upb_json_parser* upb_json_parser_create(upb_env* e, - const upb_json_parsermethod* m, - const upb_symtab* symtab, - upb_sink* output, - bool ignore_json_unknown); -upb_bytessink *upb_json_parser_input(upb_json_parser *p); - -const upb_byteshandler *upb_json_parsermethod_inputhandler( - const upb_json_parsermethod *m); - upb_json_codecache *upb_json_codecache_new(); void upb_json_codecache_free(upb_json_codecache *cache); -upb_json_parsermethod* upb_json_codecache_get(upb_json_codecache* cache, - const upb_msgdef* md); +const upb_json_parsermethod* upb_json_codecache_get(upb_json_codecache* cache, + const upb_msgdef* md); UPB_END_EXTERN_C #ifdef __cplusplus -namespace upb { -namespace json { -inline Parser* Parser::Create(Environment* env, const ParserMethod* method, - const SymbolTable* symtab, - Sink* output, bool ignore_json_unknown) { - return upb_json_parser_create( - env, method, symtab, output, ignore_json_unknown); -} -inline BytesSink* Parser::input() { - return upb_json_parser_input(this); -} - -inline const BytesHandler* ParserMethod::input_handler() const { - return upb_json_parsermethod_inputhandler(this); -} -/* static */ -inline const ParserMethod* CodeCache::Get(const MessageDef* md) { - return upb_json_codecache_get(this, md); -} +class upb::json::CodeCache { + public: + CodeCache() : ptr_(upb_json_codecache_new(), upb_json_codecache_free) {} -} /* namespace json */ -} /* namespace upb */ + /* Returns a DecoderMethod that can push data to the given handlers. + * If a suitable method already exists, it will be returned from the cache. */ + ParserMethodPtr Get(MessageDefPtr md) { + return upb_json_codecache_get(ptr_.get(), md.ptr()); + } + + private: + std::unique_ptr ptr_; +}; #endif diff --git a/upb/json/parser.rl b/upb/json/parser.rl index c2866c9..05a9505 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -299,13 +299,13 @@ static void json_parser_any_frame_set_payload_type( /* Initialize encoder. */ h = upb_handlercache_get(frame->encoder_handlercache, payload_type); - encoder = upb_pb_encoder_create(p->env, h, &frame->stringsink.sink); + encoder = upb_pb_encoder_create(p->env, h, frame->stringsink.sink); /* Initialize parser. */ parser_method = upb_json_codecache_get(frame->parser_codecache, payload_type); upb_sink_reset(&frame->sink, h, encoder); frame->parser = upb_json_parser_create(p->env, parser_method, p->symtab, - &frame->sink, p->ignore_json_unknown); + frame->sink, p->ignore_json_unknown); } static void json_parser_any_frame_free(upb_jsonparser_any_frame *frame) { @@ -381,9 +381,12 @@ static bool check_stack(upb_json_parser *p) { static void set_name_table(upb_json_parser *p, upb_jsonparser_frame *frame) { upb_value v; const upb_json_codecache *cache = p->method->cache; - bool ok = upb_inttable_lookupptr(&cache->methods, frame->m, &v); - const upb_json_parsermethod *method = upb_value_getptr(v); + bool ok; + const upb_json_parsermethod *method; + + ok = upb_inttable_lookupptr(&cache->methods, frame->m, &v); UPB_ASSERT(ok); + method = upb_value_getconstptr(v); frame->name_table = &method->name_table; } @@ -1285,7 +1288,7 @@ static bool end_any_stringval(upb_json_parser *p) { } json_parser_any_frame_set_payload_type(p, p->top->any_frame, payload_type); - + return true; } else { upb_status_seterrf( @@ -2661,7 +2664,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c, upb_msg_field_iter i; upb_alloc *alloc = upb_arena_alloc(&c->arena); - upb_json_parsermethod *m = upb_gmalloc(sizeof(*m)); + upb_json_parsermethod *m = upb_malloc(alloc, sizeof(*m)); m->cache = c; @@ -2684,7 +2687,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c, size_t len = upb_fielddef_getjsonname(f, NULL, 0); buf = upb_malloc(alloc, len); upb_fielddef_getjsonname(f, buf, len); - upb_strtable_insert3(&m->name_table, buf, len, v, alloc); + upb_strtable_insert3(&m->name_table, buf, strlen(buf), v, alloc); if (strcmp(buf, upb_fielddef_name(f)) != 0) { /* Since the JSON name is different from the regular field name, add an @@ -2703,7 +2706,7 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c, upb_json_parser *upb_json_parser_create(upb_env *env, const upb_json_parsermethod *method, const upb_symtab* symtab, - upb_sink *output, + upb_sink output, bool ignore_json_unknown) { #ifndef NDEBUG const size_t size_before = upb_env_bytesallocated(env); @@ -2719,8 +2722,8 @@ upb_json_parser *upb_json_parser_create(upb_env *env, upb_bytessink_reset(&p->input_, &method->input_handler_, p); json_parser_reset(p); - upb_sink_reset(&p->top->sink, output->handlers, output->closure); - p->top->m = upb_handlers_msgdef(output->handlers); + p->top->sink = output; + p->top->m = upb_handlers_msgdef(output.handlers); if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) { p->top->is_any = true; p->top->any_frame = json_parser_any_frame_new(p); @@ -2740,8 +2743,8 @@ upb_json_parser *upb_json_parser_create(upb_env *env, return p; } -upb_bytessink *upb_json_parser_input(upb_json_parser *p) { - return &p->input_; +upb_bytessink upb_json_parser_input(upb_json_parser *p) { + return p->input_; } const upb_byteshandler *upb_json_parsermethod_inputhandler( @@ -2768,21 +2771,22 @@ void upb_json_codecache_free(upb_json_codecache *c) { upb_gfree(c); } -upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c, - const upb_msgdef *md) { +const upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c, + const upb_msgdef *md) { upb_json_parsermethod *m; upb_value v; upb_msg_field_iter i; + upb_alloc *alloc = upb_arena_alloc(&c->arena); if (upb_inttable_lookupptr(&c->methods, md, &v)) { - return upb_value_getptr(v); + return upb_value_getconstptr(v); } m = parsermethod_new(c, md); - v = upb_value_ptr(m); + v = upb_value_constptr(m); if (!m) return NULL; - if (!upb_inttable_insertptr(&c->methods, m, v)) return NULL; + if (!upb_inttable_insertptr2(&c->methods, md, v, alloc)) return NULL; /* Populate parser methods for all submessages, so the name tables will * be available during parsing. */ diff --git a/upb/json/printer.c b/upb/json/printer.c index b2c9ebd..83f1a58 100644 --- a/upb/json/printer.c +++ b/upb/json/printer.c @@ -13,7 +13,7 @@ struct upb_json_printer { upb_sink input_; /* BytesSink closure. */ void *subc_; - upb_bytessink *output_; + upb_bytessink output_; /* We track the depth so that we know when to emit startstr/endstr on the * output. */ @@ -87,7 +87,7 @@ strpc *newstrpc_str(upb_handlers *h, const char * str) { static void print_data( upb_json_printer *p, const char *buf, unsigned int len) { /* TODO: Will need to change if we support pushback from the sink. */ - size_t n = upb_bytessink_putbuf(p->output_, p->subc_, buf, len, NULL); + size_t n = upb_bytessink_putbuf(&p->output_, p->subc_, buf, len, NULL); UPB_ASSERT(n == len); } @@ -369,7 +369,7 @@ static bool printer_startmsg(void *closure, const void *handler_data) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); if (p->depth_ == 0) { - upb_bytessink_start(p->output_, 0, &p->subc_); + upb_bytessink_start(&p->output_, 0, &p->subc_); } start_frame(p); return true; @@ -381,7 +381,7 @@ static bool printer_endmsg(void *closure, const void *handler_data, upb_status * UPB_UNUSED(s); end_frame(p); if (p->depth_ == 0) { - upb_bytessink_end(p->output_); + upb_bytessink_end(&p->output_); } return true; } @@ -770,7 +770,7 @@ static bool printer_startdurationmsg(void *closure, const void *handler_data) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); if (p->depth_ == 0) { - upb_bytessink_start(p->output_, 0, &p->subc_); + upb_bytessink_start(&p->output_, 0, &p->subc_); } return true; } @@ -828,7 +828,7 @@ static bool printer_enddurationmsg(void *closure, const void *handler_data, print_data(p, "\"", 1); if (p->depth_ == 0) { - upb_bytessink_end(p->output_); + upb_bytessink_end(&p->output_); } UPB_UNUSED(handler_data); @@ -839,7 +839,7 @@ static bool printer_starttimestampmsg(void *closure, const void *handler_data) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); if (p->depth_ == 0) { - upb_bytessink_start(p->output_, 0, &p->subc_); + upb_bytessink_start(&p->output_, 0, &p->subc_); } return true; } @@ -902,7 +902,7 @@ static bool printer_endtimestampmsg(void *closure, const void *handler_data, print_data(p, "\"", 1); if (p->depth_ == 0) { - upb_bytessink_end(p->output_); + upb_bytessink_end(&p->output_); } UPB_UNUSED(handler_data); @@ -914,7 +914,7 @@ static bool printer_startmsg_noframe(void *closure, const void *handler_data) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); if (p->depth_ == 0) { - upb_bytessink_start(p->output_, 0, &p->subc_); + upb_bytessink_start(&p->output_, 0, &p->subc_); } return true; } @@ -925,7 +925,7 @@ static bool printer_endmsg_noframe( UPB_UNUSED(handler_data); UPB_UNUSED(s); if (p->depth_ == 0) { - upb_bytessink_end(p->output_); + upb_bytessink_end(&p->output_); } return true; } @@ -1253,7 +1253,7 @@ static void json_printer_reset(upb_json_printer *p) { /* Public API *****************************************************************/ upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h, - upb_bytessink *output) { + upb_bytessink output) { #ifndef NDEBUG size_t size_before = upb_env_bytesallocated(e); #endif @@ -1273,12 +1273,16 @@ upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h, return p; } -upb_sink *upb_json_printer_input(upb_json_printer *p) { - return &p->input_; +upb_sink upb_json_printer_input(upb_json_printer *p) { + return p->input_; } upb_handlercache *upb_json_printer_newcache(bool preserve_proto_fieldnames) { upb_json_printercache *cache = upb_gmalloc(sizeof(*cache)); + upb_handlercache *ret = upb_handlercache_new(printer_sethandlers, cache); + cache->preserve_fieldnames = preserve_proto_fieldnames; - return upb_handlercache_new(printer_sethandlers, cache); + upb_handlercache_addcleanup(ret, cache, upb_gfree); + + return ret; } diff --git a/upb/json/printer.h b/upb/json/printer.h index fe9c8f1..a7a37bb 100644 --- a/upb/json/printer.h +++ b/upb/json/printer.h @@ -12,44 +12,24 @@ #ifdef __cplusplus namespace upb { namespace json { -class Printer; +class PrinterPtr; } /* namespace json */ } /* namespace upb */ #endif -UPB_DECLARE_TYPE(upb::json::Printer, upb_json_printer) - - -/* upb::json::Printer *********************************************************/ +/* upb_json_printer ***********************************************************/ #define UPB_JSON_PRINTER_SIZE 192 -#ifdef __cplusplus - -/* Prints an incoming stream of data to a BytesSink in JSON format. */ -class upb::json::Printer { - public: - static Printer* Create(Environment* env, const upb::Handlers* handlers, - BytesSink* output); - - /* The input to the printer. */ - Sink* input(); - - static const size_t kSize = UPB_JSON_PRINTER_SIZE; - static upb_handlercache* NewCache(bool preserve_proto_fieldnames); - - private: - UPB_DISALLOW_POD_OPS(Printer, upb::json::Printer) -}; - -#endif +struct upb_json_printer; +typedef struct upb_json_printer upb_json_printer; UPB_BEGIN_EXTERN_C /* Native C API. */ upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h, - upb_bytessink *output); -upb_sink *upb_json_printer_input(upb_json_printer *p); + upb_bytessink output); +upb_sink upb_json_printer_input(upb_json_printer *p); const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md, bool preserve_fieldnames, const void *owner); @@ -60,19 +40,29 @@ UPB_END_EXTERN_C #ifdef __cplusplus -namespace upb { -namespace json { -inline Printer* Printer::Create(Environment* env, const upb::Handlers* handlers, - BytesSink* output) { - return upb_json_printer_create(env, handlers, output); -} -inline Sink* Printer::input() { return upb_json_printer_input(this); } -inline upb_handlercache* Printer::NewCache(bool preserve_proto_fieldnames) { - return upb_json_printer_newcache(preserve_proto_fieldnames); -} -} /* namespace json */ -} /* namespace upb */ +/* Prints an incoming stream of data to a BytesSink in JSON format. */ +class upb::json::PrinterPtr { + public: + PrinterPtr(upb_json_printer* ptr) : ptr_(ptr) {} -#endif + static PrinterPtr Create(Environment *env, const upb::Handlers *handlers, + BytesSink output) { + return PrinterPtr(upb_json_printer_create(env, handlers, output.sink())); + } + + /* The input to the printer. */ + Sink input() { return upb_json_printer_input(ptr_); } + + static const size_t kSize = UPB_JSON_PRINTER_SIZE; + + static HandlerCache NewCache(bool preserve_proto_fieldnames) { + return upb_json_printer_newcache(preserve_proto_fieldnames); + } + + private: + upb_json_printer* ptr_; +}; + +#endif /* __cplusplus */ #endif /* UPB_JSON_TYPED_PRINTER_H_ */ diff --git a/upb/pb/decoder.c b/upb/pb/decoder.c index 0cae05b..cd64f72 100644 --- a/upb/pb/decoder.c +++ b/upb/pb/decoder.c @@ -993,7 +993,7 @@ void upb_pbdecoder_reset(upb_pbdecoder *d) { } upb_pbdecoder *upb_pbdecoder_create(upb_env *e, const upb_pbdecodermethod *m, - upb_sink *sink) { + upb_sink sink) { const size_t default_max_nesting = 64; #ifndef NDEBUG size_t size_before = upb_env_bytesallocated(e); @@ -1017,12 +1017,11 @@ upb_pbdecoder *upb_pbdecoder_create(upb_env *e, const upb_pbdecodermethod *m, upb_pbdecoder_reset(d); upb_bytessink_reset(&d->input_, &m->input_handler_, d); - UPB_ASSERT(sink); if (d->method_->dest_handlers_) { - if (sink->handlers != d->method_->dest_handlers_) + if (sink.handlers != d->method_->dest_handlers_) return NULL; } - upb_sink_reset(&d->top->sink, sink->handlers, sink->closure); + d->top->sink = sink; /* If this fails, increase the value in decoder.h. */ UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(e) - size_before <= @@ -1038,8 +1037,8 @@ const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d) { return d->method_; } -upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d) { - return &d->input_; +upb_bytessink upb_pbdecoder_input(upb_pbdecoder *d) { + return d->input_; } size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d) { diff --git a/upb/pb/decoder.h b/upb/pb/decoder.h index 1ffcb7d..ba381f3 100644 --- a/upb/pb/decoder.h +++ b/upb/pb/decoder.h @@ -56,6 +56,7 @@ UPB_END_EXTERN_C * Handlers. */ class upb::pb::DecoderMethodPtr { public: + DecoderMethodPtr() : ptr_(nullptr) {} DecoderMethodPtr(const upb_pbdecodermethod* ptr) : ptr_(ptr) {} const upb_pbdecodermethod* ptr() { return ptr_; } @@ -98,9 +99,9 @@ UPB_BEGIN_EXTERN_C upb_pbdecoder *upb_pbdecoder_create(upb_env *e, const upb_pbdecodermethod *method, - upb_sink *output); + upb_sink output); const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d); -upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d); +upb_bytessink upb_pbdecoder_input(upb_pbdecoder *d); uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d); size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d); bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max); @@ -124,8 +125,8 @@ class upb::pb::DecoderPtr { * * The sink must match the given method. */ static DecoderPtr Create(Environment *env, DecoderMethodPtr method, - upb_sink *output) { - return DecoderPtr(upb_pbdecoder_create(env, method.ptr(), output)); + upb::Sink output) { + return DecoderPtr(upb_pbdecoder_create(env, method.ptr(), output.sink())); } /* Returns the DecoderMethod this decoder is parsing from. */ @@ -134,7 +135,7 @@ class upb::pb::DecoderPtr { } /* The sink on which this decoder receives input. */ - upb_bytessink* input() { return upb_pbdecoder_input(ptr()); } + BytesSink input() { return BytesSink(upb_pbdecoder_input(ptr())); } /* Returns number of bytes successfully parsed. * diff --git a/upb/pb/encoder.c b/upb/pb/encoder.c index 3497007..1496eba 100644 --- a/upb/pb/encoder.c +++ b/upb/pb/encoder.c @@ -95,7 +95,7 @@ struct upb_pb_encoder { /* Our input and output. */ upb_sink input_; - upb_bytessink *output_; + upb_bytessink output_; /* The "subclosure" -- used as the inner closure as part of the bytessink * protocol. */ @@ -127,7 +127,7 @@ struct upb_pb_encoder { /* TODO(haberman): handle pushback */ static void putbuf(upb_pb_encoder *e, const char *buf, size_t len) { - size_t n = upb_bytessink_putbuf(e->output_, e->subc, buf, len, NULL); + size_t n = upb_bytessink_putbuf(&e->output_, e->subc, buf, len, NULL); UPB_ASSERT(n == len); } @@ -353,7 +353,7 @@ static bool startmsg(void *c, const void *hd) { upb_pb_encoder *e = c; UPB_UNUSED(hd); if (e->depth++ == 0) { - upb_bytessink_start(e->output_, 0, &e->subc); + upb_bytessink_start(&e->output_, 0, &e->subc); } return true; } @@ -363,7 +363,7 @@ static bool endmsg(void *c, const void *hd, upb_status *status) { UPB_UNUSED(hd); UPB_UNUSED(status); if (--e->depth == 0) { - upb_bytessink_end(e->output_); + upb_bytessink_end(&e->output_); } return true; } @@ -527,7 +527,7 @@ upb_handlercache *upb_pb_encoder_newcache() { } upb_pb_encoder *upb_pb_encoder_create(upb_env *env, const upb_handlers *h, - upb_bytessink *output) { + upb_bytessink output) { const size_t initial_bufsize = 256; const size_t initial_segbufsize = 16; /* TODO(haberman): make this configurable. */ @@ -556,7 +556,7 @@ upb_pb_encoder *upb_pb_encoder_create(upb_env *env, const upb_handlers *h, e->env = env; e->output_ = output; - e->subc = output->closure; + e->subc = output.closure; e->ptr = e->buf; /* If this fails, increase the value in encoder.h. */ @@ -565,4 +565,4 @@ upb_pb_encoder *upb_pb_encoder_create(upb_env *env, const upb_handlers *h, return e; } -upb_sink *upb_pb_encoder_input(upb_pb_encoder *e) { return &e->input_; } +upb_sink upb_pb_encoder_input(upb_pb_encoder *e) { return e->input_; } diff --git a/upb/pb/encoder.h b/upb/pb/encoder.h index 20ce606..7aa2870 100644 --- a/upb/pb/encoder.h +++ b/upb/pb/encoder.h @@ -30,16 +30,16 @@ class EncoderPtr; * constructed. This hint may be an overestimate for some build configurations. * But if the decoder library is upgraded without recompiling the application, * it may be an underestimate. */ -#define UPB_PB_ENCODER_SIZE 768 +#define UPB_PB_ENCODER_SIZE 784 struct upb_pb_encoder; typedef struct upb_pb_encoder upb_pb_encoder; UPB_BEGIN_EXTERN_C -upb_sink *upb_pb_encoder_input(upb_pb_encoder *p); +upb_sink upb_pb_encoder_input(upb_pb_encoder *p); upb_pb_encoder* upb_pb_encoder_create(upb_env* e, const upb_handlers* h, - upb_bytessink* output); + upb_bytessink output); upb_handlercache *upb_pb_encoder_newcache(); @@ -56,12 +56,12 @@ class upb::pb::EncoderPtr { /* Creates a new encoder in the given environment. The Handlers must have * come from NewHandlers() below. */ static EncoderPtr Create(Environment* env, const Handlers* handlers, - BytesSink* output) { - return EncoderPtr(upb_pb_encoder_create(env, handlers, output->ptr())); + BytesSink output) { + return EncoderPtr(upb_pb_encoder_create(env, handlers, output.sink())); } /* The input to the encoder. */ - upb_sink* input() { return upb_pb_encoder_input(ptr()); } + upb::Sink input() { return upb_pb_encoder_input(ptr()); } /* Creates a new set of handlers for this MessageDef. */ static HandlerCache NewCache() { diff --git a/upb/sink.c b/upb/sink.c index d0197a6..6ef5718 100644 --- a/upb/sink.c +++ b/upb/sink.c @@ -1,17 +1,17 @@ #include "upb/sink.h" -bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink *sink) { +bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink sink) { void *subc; bool ret; upb_bufhandle handle = UPB_BUFHANDLE_INIT; handle.buf = buf; - ret = upb_bytessink_start(sink, len, &subc); + ret = upb_bytessink_start(&sink, len, &subc); if (ret && len != 0) { - ret = (upb_bytessink_putbuf(sink, subc, buf, len, &handle) >= len); + ret = (upb_bytessink_putbuf(&sink, subc, buf, len, &handle) >= len); } if (ret) { - ret = upb_bytessink_end(sink); + ret = upb_bytessink_end(&sink); } return ret; } diff --git a/upb/sink.h b/upb/sink.h index 8cab45d..1359c5e 100644 --- a/upb/sink.h +++ b/upb/sink.h @@ -237,6 +237,17 @@ class upb::Sink { /* Constructor with no initialization; must be Reset() before use. */ Sink() {} + Sink(const Sink&) = default; + Sink& operator=(const Sink&) = default; + + Sink(const upb_sink& sink) : sink_(sink) {} + Sink &operator=(const upb_sink &sink) { + sink_ = sink; + return *this; + } + + upb_sink sink() { return sink_; } + /* Constructs a new sink for the given frozen handlers and closure. * * TODO: once the Handlers know the expected closure type, verify that T @@ -406,7 +417,16 @@ class upb::BytesSink { public: BytesSink() {} - upb_bytessink* ptr() { return &sink_; } + BytesSink(const BytesSink&) = default; + BytesSink& operator=(const BytesSink&) = default; + + BytesSink(const upb_bytessink& sink) : sink_(sink) {} + BytesSink &operator=(const upb_bytessink &sink) { + sink_ = sink; + return *this; + } + + upb_bytessink sink() { return sink_; } /* Constructs a new sink for the given frozen handlers and closure. * @@ -444,15 +464,15 @@ class upb::BytesSink { UPB_BEGIN_EXTERN_C -bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink *sink); +bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink sink); UPB_END_EXTERN_C #ifdef __cplusplus namespace upb { -template bool PutBuffer(const T& str, upb_bytessink* sink) { - return upb_bufsrc_putbuf(str.c_str(), str.size(), sink); +template bool PutBuffer(const T& str, BytesSink sink) { + return upb_bufsrc_putbuf(str.c_str(), str.size(), sink.sink()); } } -- cgit v1.2.3 From ef7c50223a9bd1ab451e82a31a93cab3272b497f Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 12 Jan 2019 20:02:52 -0800 Subject: All tests pass again! --- tests/test_cpp.cc | 382 ++++++++++++++++++++++++++---------------------------- upb/handlers.c | 6 +- upb/sink.h | 41 +++++- 3 files changed, 221 insertions(+), 208 deletions(-) (limited to 'upb') diff --git a/tests/test_cpp.cc b/tests/test_cpp.cc index b6d8212..a5bfbc7 100644 --- a/tests/test_cpp.cc +++ b/tests/test_cpp.cc @@ -52,16 +52,16 @@ class StringBufTesterBase { StringBufTesterBase() : seen_(false), handler_data_val_(0) {} - void CallAndVerify(upb::Sink* sink, const upb::FieldDef* f) { - upb::Handlers::Selector start; - ASSERT(upb::Handlers::GetSelector(f, UPB_HANDLER_STARTSTR, &start)); - upb::Handlers::Selector str; - ASSERT(upb::Handlers::GetSelector(f, UPB_HANDLER_STRING, &str)); + void CallAndVerify(upb::Sink sink, upb::FieldDefPtr f) { + upb_selector_t start; + ASSERT(upb_handlers_getselector(f.ptr(), UPB_HANDLER_STARTSTR, &start)); + upb_selector_t str; + ASSERT(upb_handlers_getselector(f.ptr(), UPB_HANDLER_STRING, &str)); ASSERT(!seen_); upb::Sink sub; - sink->StartMessage(); - sink->StartString(start, 0, &sub); + sink.StartMessage(); + sink.StartString(start, 0, &sub); size_t ret = sub.PutStringBuffer(str, &buf_, 5, &handle_); ASSERT(seen_); ASSERT(len_ == 5); @@ -74,7 +74,7 @@ class StringBufTesterBase { int handler_data_val_; size_t len_; char buf_; - upb::BufferHandle handle_; + upb_bufhandle handle_; }; // Test 8 combinations of: @@ -91,9 +91,9 @@ class StringBufTesterVoidMethodNoHandlerDataNoHandle : public StringBufTesterBase { public: typedef StringBufTesterVoidMethodNoHandlerDataNoHandle ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { UPB_UNUSED(f); - ASSERT(h->SetStringHandler(f, UpbMakeHandler(&ME::Handler))); + ASSERT(h.SetStringHandler(f, UpbMakeHandler(&ME::Handler))); handler_data_val_ = kExpectedHandlerData; } @@ -109,14 +109,14 @@ class StringBufTesterVoidMethodNoHandlerDataWithHandle : public StringBufTesterBase { public: typedef StringBufTesterVoidMethodNoHandlerDataWithHandle ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { UPB_UNUSED(f); - ASSERT(h->SetStringHandler(f, UpbMakeHandler(&ME::Handler))); + ASSERT(h.SetStringHandler(f, UpbMakeHandler(&ME::Handler))); handler_data_val_ = kExpectedHandlerData; } private: - void Handler(const char *buf, size_t len, const upb::BufferHandle* handle) { + void Handler(const char *buf, size_t len, const upb_bufhandle* handle) { ASSERT(buf == &buf_); ASSERT(handle == &handle_); seen_ = true; @@ -128,9 +128,9 @@ class StringBufTesterVoidMethodWithHandlerDataNoHandle : public StringBufTesterBase { public: typedef StringBufTesterVoidMethodWithHandlerDataNoHandle ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { UPB_UNUSED(f); - ASSERT(h->SetStringHandler( + ASSERT(h.SetStringHandler( f, UpbBind(&ME::Handler, new int(kExpectedHandlerData)))); } @@ -147,15 +147,15 @@ class StringBufTesterVoidMethodWithHandlerDataWithHandle : public StringBufTesterBase { public: typedef StringBufTesterVoidMethodWithHandlerDataWithHandle ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { UPB_UNUSED(f); - ASSERT(h->SetStringHandler( + ASSERT(h.SetStringHandler( f, UpbBind(&ME::Handler, new int(kExpectedHandlerData)))); } private: void Handler(const int* hd, const char* buf, size_t len, - const upb::BufferHandle* handle) { + const upb_bufhandle* handle) { ASSERT(buf == &buf_); ASSERT(handle == &handle_); handler_data_val_ = *hd; @@ -168,9 +168,9 @@ class StringBufTesterVoidFunctionNoHandlerDataNoHandle : public StringBufTesterBase { public: typedef StringBufTesterVoidFunctionNoHandlerDataNoHandle ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { UPB_UNUSED(f); - ASSERT(h->SetStringHandler(f, UpbMakeHandler(&ME::Handler))); + ASSERT(h.SetStringHandler(f, UpbMakeHandler(&ME::Handler))); handler_data_val_ = kExpectedHandlerData; } @@ -186,15 +186,15 @@ class StringBufTesterVoidFunctionNoHandlerDataWithHandle : public StringBufTesterBase { public: typedef StringBufTesterVoidFunctionNoHandlerDataWithHandle ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { UPB_UNUSED(f); - ASSERT(h->SetStringHandler(f, UpbMakeHandler(&ME::Handler))); + ASSERT(h.SetStringHandler(f, UpbMakeHandler(&ME::Handler))); handler_data_val_ = kExpectedHandlerData; } private: static void Handler(ME* t, const char* buf, size_t len, - const upb::BufferHandle* handle) { + const upb_bufhandle* handle) { ASSERT(buf == &t->buf_); ASSERT(handle == &t->handle_); t->seen_ = true; @@ -206,9 +206,9 @@ class StringBufTesterVoidFunctionWithHandlerDataNoHandle : public StringBufTesterBase { public: typedef StringBufTesterVoidFunctionWithHandlerDataNoHandle ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { UPB_UNUSED(f); - ASSERT(h->SetStringHandler( + ASSERT(h.SetStringHandler( f, UpbBind(&ME::Handler, new int(kExpectedHandlerData)))); } @@ -225,15 +225,15 @@ class StringBufTesterVoidFunctionWithHandlerDataWithHandle : public StringBufTesterBase { public: typedef StringBufTesterVoidFunctionWithHandlerDataWithHandle ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { UPB_UNUSED(f); - ASSERT(h->SetStringHandler( + ASSERT(h.SetStringHandler( f, UpbBind(&ME::Handler, new int(kExpectedHandlerData)))); } private: static void Handler(ME* t, const int* hd, const char* buf, size_t len, - const upb::BufferHandle* handle) { + const upb_bufhandle* handle) { ASSERT(buf == &t->buf_); ASSERT(handle == &t->handle_); t->handler_data_val_ = *hd; @@ -246,9 +246,9 @@ class StringBufTesterSizeTMethodNoHandlerDataNoHandle : public StringBufTesterBase { public: typedef StringBufTesterSizeTMethodNoHandlerDataNoHandle ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { UPB_UNUSED(f); - ASSERT(h->SetStringHandler(f, UpbMakeHandler(&ME::Handler))); + ASSERT(h.SetStringHandler(f, UpbMakeHandler(&ME::Handler))); handler_data_val_ = kExpectedHandlerData; } @@ -265,9 +265,9 @@ class StringBufTesterBoolMethodNoHandlerDataNoHandle : public StringBufTesterBase { public: typedef StringBufTesterBoolMethodNoHandlerDataNoHandle ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { UPB_UNUSED(f); - ASSERT(h->SetStringHandler(f, UpbMakeHandler(&ME::Handler))); + ASSERT(h.SetStringHandler(f, UpbMakeHandler(&ME::Handler))); handler_data_val_ = kExpectedHandlerData; } @@ -288,10 +288,10 @@ class StartMsgTesterBase { StartMsgTesterBase() : seen_(false), handler_data_val_(0) {} - void CallAndVerify(upb::Sink* sink, const upb::FieldDef* f) { + void CallAndVerify(upb::Sink sink, upb::FieldDefPtr f) { UPB_UNUSED(f); ASSERT(!seen_); - sink->StartMessage(); + sink.StartMessage(); ASSERT(seen_); ASSERT(handler_data_val_ == kExpectedHandlerData); } @@ -307,9 +307,9 @@ class StartMsgTesterBase { class StartMsgTesterVoidFunctionNoHandlerData : public StartMsgTesterBase { public: typedef StartMsgTesterVoidFunctionNoHandlerData ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { UPB_UNUSED(f); - ASSERT(h->SetStartMessageHandler(UpbMakeHandler(&Handler))); + ASSERT(h.SetStartMessageHandler(UpbMakeHandler(&Handler))); handler_data_val_ = kExpectedHandlerData; } @@ -323,9 +323,9 @@ class StartMsgTesterVoidFunctionNoHandlerData : public StartMsgTesterBase { class StartMsgTesterBoolFunctionNoHandlerData : public StartMsgTesterBase { public: typedef StartMsgTesterBoolFunctionNoHandlerData ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { UPB_UNUSED(f); - ASSERT(h->SetStartMessageHandler(UpbMakeHandler(&Handler))); + ASSERT(h.SetStartMessageHandler(UpbMakeHandler(&Handler))); handler_data_val_ = kExpectedHandlerData; } @@ -339,9 +339,9 @@ class StartMsgTesterBoolFunctionNoHandlerData : public StartMsgTesterBase { class StartMsgTesterVoidMethodNoHandlerData : public StartMsgTesterBase { public: typedef StartMsgTesterVoidMethodNoHandlerData ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { UPB_UNUSED(f); - ASSERT(h->SetStartMessageHandler(UpbMakeHandler(&ME::Handler))); + ASSERT(h.SetStartMessageHandler(UpbMakeHandler(&ME::Handler))); handler_data_val_ = kExpectedHandlerData; } @@ -354,9 +354,9 @@ class StartMsgTesterVoidMethodNoHandlerData : public StartMsgTesterBase { class StartMsgTesterBoolMethodNoHandlerData : public StartMsgTesterBase { public: typedef StartMsgTesterBoolMethodNoHandlerData ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { UPB_UNUSED(f); - ASSERT(h->SetStartMessageHandler(UpbMakeHandler(&ME::Handler))); + ASSERT(h.SetStartMessageHandler(UpbMakeHandler(&ME::Handler))); handler_data_val_ = kExpectedHandlerData; } @@ -370,9 +370,9 @@ class StartMsgTesterBoolMethodNoHandlerData : public StartMsgTesterBase { class StartMsgTesterVoidFunctionWithHandlerData : public StartMsgTesterBase { public: typedef StartMsgTesterVoidFunctionWithHandlerData ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { UPB_UNUSED(f); - ASSERT(h->SetStartMessageHandler( + ASSERT(h.SetStartMessageHandler( UpbBind(&Handler, new int(kExpectedHandlerData)))); } @@ -386,9 +386,9 @@ class StartMsgTesterVoidFunctionWithHandlerData : public StartMsgTesterBase { class StartMsgTesterBoolFunctionWithHandlerData : public StartMsgTesterBase { public: typedef StartMsgTesterBoolFunctionWithHandlerData ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { UPB_UNUSED(f); - ASSERT(h->SetStartMessageHandler( + ASSERT(h.SetStartMessageHandler( UpbBind(&Handler, new int(kExpectedHandlerData)))); } @@ -403,9 +403,9 @@ class StartMsgTesterBoolFunctionWithHandlerData : public StartMsgTesterBase { class StartMsgTesterVoidMethodWithHandlerData : public StartMsgTesterBase { public: typedef StartMsgTesterVoidMethodWithHandlerData ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { UPB_UNUSED(f); - ASSERT(h->SetStartMessageHandler( + ASSERT(h.SetStartMessageHandler( UpbBind(&ME::Handler, new int(kExpectedHandlerData)))); } @@ -419,9 +419,9 @@ class StartMsgTesterVoidMethodWithHandlerData : public StartMsgTesterBase { class StartMsgTesterBoolMethodWithHandlerData : public StartMsgTesterBase { public: typedef StartMsgTesterBoolMethodWithHandlerData ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { UPB_UNUSED(f); - ASSERT(h->SetStartMessageHandler( + ASSERT(h.SetStartMessageHandler( UpbBind(&ME::Handler, new int(kExpectedHandlerData)))); } @@ -439,12 +439,12 @@ class Int32ValueTesterBase { Int32ValueTesterBase() : seen_(false), val_(0), handler_data_val_(0) {} - void CallAndVerify(upb::Sink* sink, const upb::FieldDef* f) { - upb::Handlers::Selector s; - ASSERT(upb::Handlers::GetSelector(f, UPB_HANDLER_INT32, &s)); + void CallAndVerify(upb::Sink sink, upb::FieldDefPtr f) { + upb_selector_t s; + ASSERT(upb_handlers_getselector(f.ptr(), UPB_HANDLER_INT32, &s)); ASSERT(!seen_); - sink->PutInt32(s, 5); + sink.PutInt32(s, 5); ASSERT(seen_); ASSERT(handler_data_val_ == kExpectedHandlerData); ASSERT(val_ == 5); @@ -463,8 +463,8 @@ class ValueTesterInt32VoidFunctionNoHandlerData : public Int32ValueTesterBase { public: typedef ValueTesterInt32VoidFunctionNoHandlerData ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { - ASSERT(h->SetInt32Handler(f, UpbMakeHandler(&Handler))); + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { + ASSERT(h.SetInt32Handler(f, UpbMakeHandler(&Handler))); handler_data_val_ = kExpectedHandlerData; } @@ -479,8 +479,8 @@ class ValueTesterInt32BoolFunctionNoHandlerData : public Int32ValueTesterBase { public: typedef ValueTesterInt32BoolFunctionNoHandlerData ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { - ASSERT(h->SetInt32Handler(f, UpbMakeHandler(&Handler))); + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { + ASSERT(h.SetInt32Handler(f, UpbMakeHandler(&Handler))); handler_data_val_ = kExpectedHandlerData; } @@ -495,8 +495,8 @@ class ValueTesterInt32BoolFunctionNoHandlerData class ValueTesterInt32VoidMethodNoHandlerData : public Int32ValueTesterBase { public: typedef ValueTesterInt32VoidMethodNoHandlerData ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { - ASSERT(h->SetInt32Handler(f, UpbMakeHandler(&ME::Handler))); + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { + ASSERT(h.SetInt32Handler(f, UpbMakeHandler(&ME::Handler))); handler_data_val_ = kExpectedHandlerData; } @@ -510,8 +510,8 @@ class ValueTesterInt32VoidMethodNoHandlerData : public Int32ValueTesterBase { class ValueTesterInt32BoolMethodNoHandlerData : public Int32ValueTesterBase { public: typedef ValueTesterInt32BoolMethodNoHandlerData ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { - ASSERT(h->SetInt32Handler(f, UpbMakeHandler(&ME::Handler))); + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { + ASSERT(h.SetInt32Handler(f, UpbMakeHandler(&ME::Handler))); handler_data_val_ = kExpectedHandlerData; } @@ -527,8 +527,8 @@ class ValueTesterInt32VoidFunctionWithHandlerData : public Int32ValueTesterBase { public: typedef ValueTesterInt32VoidFunctionWithHandlerData ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { - ASSERT(h->SetInt32Handler( + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { + ASSERT(h.SetInt32Handler( f, UpbBind(&Handler, new int(kExpectedHandlerData)))); } @@ -544,8 +544,8 @@ class ValueTesterInt32BoolFunctionWithHandlerData : public Int32ValueTesterBase { public: typedef ValueTesterInt32BoolFunctionWithHandlerData ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { - ASSERT(h->SetInt32Handler( + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { + ASSERT(h.SetInt32Handler( f, UpbBind(&Handler, new int(kExpectedHandlerData)))); } @@ -561,8 +561,8 @@ class ValueTesterInt32BoolFunctionWithHandlerData class ValueTesterInt32VoidMethodWithHandlerData : public Int32ValueTesterBase { public: typedef ValueTesterInt32VoidMethodWithHandlerData ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { - ASSERT(h->SetInt32Handler( + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { + ASSERT(h.SetInt32Handler( f, UpbBind(&ME::Handler, new int(kExpectedHandlerData)))); } @@ -577,8 +577,8 @@ class ValueTesterInt32VoidMethodWithHandlerData : public Int32ValueTesterBase { class ValueTesterInt32BoolMethodWithHandlerData : public Int32ValueTesterBase { public: typedef ValueTesterInt32BoolMethodWithHandlerData ME; - void Register(upb::Handlers* h, const upb::FieldDef* f) { - ASSERT(h->SetInt32Handler( + void Register(upb::HandlersPtr h, upb::FieldDefPtr f) { + ASSERT(h.SetInt32Handler( f, UpbBind(&ME::Handler, new int(kExpectedHandlerData)))); } @@ -591,22 +591,29 @@ class ValueTesterInt32BoolMethodWithHandlerData : public Int32ValueTesterBase { } }; +template +void RegisterHandlers(const void* closure, upb::Handlers* h_ptr) { + T* tester = const_cast(static_cast(closure)); + upb::HandlersPtr h(h_ptr); + upb::FieldDefPtr f = h.message_def().FindFieldByNumber(T::kFieldNumber); + ASSERT(f); + tester->Register(h, f); +} + template void TestHandler() { - upb::SymbolTable* symtab = upb::SymbolTable::New(); - const upb::MessageDef* md = upb_test_TestMessage_getmsgdef(symtab); + T tester; + upb::SymbolTable symtab; + upb::HandlerCache cache(&RegisterHandlers, &tester); + upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr())); ASSERT(md); - const upb::FieldDef* f = md->FindFieldByNumber(T::kFieldNumber); + upb::FieldDefPtr f = md.FindFieldByNumber(T::kFieldNumber); ASSERT(f); - upb::reffed_ptr h(upb::Handlers::New(md)); - T tester; - tester.Register(h.get(), f); - ASSERT(h->Freeze(NULL)); + const upb::Handlers* h = cache.Get(md); - upb::Sink sink(h.get(), &tester); - tester.CallAndVerify(&sink, f); - upb::SymbolTable::Free(symtab); + upb::Sink sink(h, &tester); + tester.CallAndVerify(sink, f); } class T1 {}; @@ -670,17 +677,17 @@ void DoNothingEndMessageHandler(C* closure, upb::Status *status) { UPB_UNUSED(status); } -void TestMismatchedTypes() { - // First create a schema for our test. - upb::SymbolTable* symtab = upb::SymbolTable::New(); - const upb::MessageDef* md = upb_test_TestMessage_getmsgdef(symtab); +void RegisterMismatchedTypes(const void* closure, upb::Handlers* h_ptr) { + upb::HandlersPtr h(h_ptr); + + upb::MessageDefPtr md(h.message_def()); ASSERT(md); - const upb::FieldDef* i32 = md->FindFieldByName("i32"); - const upb::FieldDef* r_i32 = md->FindFieldByName("r_i32"); - const upb::FieldDef* str = md->FindFieldByName("str"); - const upb::FieldDef* r_str = md->FindFieldByName("r_str"); - const upb::FieldDef* msg = md->FindFieldByName("msg"); - const upb::FieldDef* r_msg = md->FindFieldByName("r_msg"); + upb::FieldDefPtr i32 = md.FindFieldByName("i32"); + upb::FieldDefPtr r_i32 = md.FindFieldByName("r_i32"); + upb::FieldDefPtr str = md.FindFieldByName("str"); + upb::FieldDefPtr r_str = md.FindFieldByName("r_str"); + upb::FieldDefPtr msg = md.FindFieldByName("msg"); + upb::FieldDefPtr r_msg = md.FindFieldByName("r_msg"); ASSERT(i32); ASSERT(r_i32); ASSERT(str); @@ -688,189 +695,163 @@ void TestMismatchedTypes() { ASSERT(msg); ASSERT(r_msg); - // Now test the type-checking in handler registration. - upb::reffed_ptr h(upb::Handlers::New(md)); - // Establish T1 as the top-level closure type. - ASSERT(h->SetInt32Handler(i32, UpbMakeHandler(DoNothingInt32Handler))); + ASSERT(h.SetInt32Handler(i32, UpbMakeHandler(DoNothingInt32Handler))); // Now any other attempt to set another handler with T2 as the top-level // closure should fail. But setting these same handlers with T1 as the // top-level closure will succeed. - ASSERT(!h->SetStartMessageHandler(UpbMakeHandler(DoNothingHandler))); - ASSERT(h->SetStartMessageHandler(UpbMakeHandler(DoNothingHandler))); + ASSERT(!h.SetStartMessageHandler(UpbMakeHandler(DoNothingHandler))); + ASSERT(h.SetStartMessageHandler(UpbMakeHandler(DoNothingHandler))); ASSERT( - !h->SetEndMessageHandler(UpbMakeHandler(DoNothingEndMessageHandler))); + !h.SetEndMessageHandler(UpbMakeHandler(DoNothingEndMessageHandler))); ASSERT( - h->SetEndMessageHandler(UpbMakeHandler(DoNothingEndMessageHandler))); + h.SetEndMessageHandler(UpbMakeHandler(DoNothingEndMessageHandler))); - ASSERT(!h->SetStartStringHandler( + ASSERT(!h.SetStartStringHandler( str, UpbMakeHandler(DoNothingStartHandler::String))); - ASSERT(h->SetStartStringHandler( + ASSERT(h.SetStartStringHandler( str, UpbMakeHandler(DoNothingStartHandler::String))); - ASSERT(!h->SetEndStringHandler(str, UpbMakeHandler(DoNothingHandler))); - ASSERT(h->SetEndStringHandler(str, UpbMakeHandler(DoNothingHandler))); + ASSERT(!h.SetEndStringHandler(str, UpbMakeHandler(DoNothingHandler))); + ASSERT(h.SetEndStringHandler(str, UpbMakeHandler(DoNothingHandler))); - ASSERT(!h->SetStartSubMessageHandler( + ASSERT(!h.SetStartSubMessageHandler( msg, UpbMakeHandler(DoNothingStartHandler::Handler))); - ASSERT(h->SetStartSubMessageHandler( + ASSERT(h.SetStartSubMessageHandler( msg, UpbMakeHandler(DoNothingStartHandler::Handler))); ASSERT( - !h->SetEndSubMessageHandler(msg, UpbMakeHandler(DoNothingHandler))); + !h.SetEndSubMessageHandler(msg, UpbMakeHandler(DoNothingHandler))); ASSERT( - h->SetEndSubMessageHandler(msg, UpbMakeHandler(DoNothingHandler))); + h.SetEndSubMessageHandler(msg, UpbMakeHandler(DoNothingHandler))); - ASSERT(!h->SetStartSequenceHandler( + ASSERT(!h.SetStartSequenceHandler( r_i32, UpbMakeHandler(DoNothingStartHandler::Handler))); - ASSERT(h->SetStartSequenceHandler( + ASSERT(h.SetStartSequenceHandler( r_i32, UpbMakeHandler(DoNothingStartHandler::Handler))); - ASSERT(!h->SetEndSequenceHandler( + ASSERT(!h.SetEndSequenceHandler( r_i32, UpbMakeHandler(DoNothingHandler))); - ASSERT(h->SetEndSequenceHandler( + ASSERT(h.SetEndSequenceHandler( r_i32, UpbMakeHandler(DoNothingHandler))); - ASSERT(!h->SetStartSequenceHandler( + ASSERT(!h.SetStartSequenceHandler( r_msg, UpbMakeHandler(DoNothingStartHandler::Handler))); - ASSERT(h->SetStartSequenceHandler( + ASSERT(h.SetStartSequenceHandler( r_msg, UpbMakeHandler(DoNothingStartHandler::Handler))); - ASSERT(!h->SetEndSequenceHandler( + ASSERT(!h.SetEndSequenceHandler( r_msg, UpbMakeHandler(DoNothingHandler))); - ASSERT(h->SetEndSequenceHandler( + ASSERT(h.SetEndSequenceHandler( r_msg, UpbMakeHandler(DoNothingHandler))); - ASSERT(!h->SetStartSequenceHandler( + ASSERT(!h.SetStartSequenceHandler( r_str, UpbMakeHandler(DoNothingStartHandler::Handler))); - ASSERT(h->SetStartSequenceHandler( + ASSERT(h.SetStartSequenceHandler( r_str, UpbMakeHandler(DoNothingStartHandler::Handler))); - ASSERT(!h->SetEndSequenceHandler( + ASSERT(!h.SetEndSequenceHandler( r_str, UpbMakeHandler(DoNothingHandler))); - ASSERT(h->SetEndSequenceHandler( + ASSERT(h.SetEndSequenceHandler( r_str, UpbMakeHandler(DoNothingHandler))); // By setting T1 as the return type for the Start* handlers we have // established T1 as the type of the sequence and string frames. // Setting callbacks that use T2 should fail, but T1 should succeed. ASSERT( - !h->SetStringHandler(str, UpbMakeHandler(DoNothingStringBufHandler))); + !h.SetStringHandler(str, UpbMakeHandler(DoNothingStringBufHandler))); ASSERT( - h->SetStringHandler(str, UpbMakeHandler(DoNothingStringBufHandler))); + h.SetStringHandler(str, UpbMakeHandler(DoNothingStringBufHandler))); - ASSERT(!h->SetInt32Handler(r_i32, UpbMakeHandler(DoNothingInt32Handler))); - ASSERT(h->SetInt32Handler(r_i32, UpbMakeHandler(DoNothingInt32Handler))); + ASSERT(!h.SetInt32Handler(r_i32, UpbMakeHandler(DoNothingInt32Handler))); + ASSERT(h.SetInt32Handler(r_i32, UpbMakeHandler(DoNothingInt32Handler))); - ASSERT(!h->SetStartSubMessageHandler( + ASSERT(!h.SetStartSubMessageHandler( r_msg, UpbMakeHandler(DoNothingStartHandler::Handler))); - ASSERT(h->SetStartSubMessageHandler( + ASSERT(h.SetStartSubMessageHandler( r_msg, UpbMakeHandler(DoNothingStartHandler::Handler))); - ASSERT(!h->SetEndSubMessageHandler(r_msg, + ASSERT(!h.SetEndSubMessageHandler(r_msg, UpbMakeHandler(DoNothingHandler))); - ASSERT(h->SetEndSubMessageHandler(r_msg, + ASSERT(h.SetEndSubMessageHandler(r_msg, UpbMakeHandler(DoNothingHandler))); - ASSERT(!h->SetStartStringHandler( + ASSERT(!h.SetStartStringHandler( r_str, UpbMakeHandler(DoNothingStartHandler::String))); - ASSERT(h->SetStartStringHandler( + ASSERT(h.SetStartStringHandler( r_str, UpbMakeHandler(DoNothingStartHandler::String))); ASSERT( - !h->SetEndStringHandler(r_str, UpbMakeHandler(DoNothingHandler))); - ASSERT(h->SetEndStringHandler(r_str, UpbMakeHandler(DoNothingHandler))); + !h.SetEndStringHandler(r_str, UpbMakeHandler(DoNothingHandler))); + ASSERT(h.SetEndStringHandler(r_str, UpbMakeHandler(DoNothingHandler))); - ASSERT(!h->SetStringHandler(r_str, + ASSERT(!h.SetStringHandler(r_str, UpbMakeHandler(DoNothingStringBufHandler))); - ASSERT(h->SetStringHandler(r_str, + ASSERT(h.SetStringHandler(r_str, UpbMakeHandler(DoNothingStringBufHandler))); +} - h->ClearError(); - ASSERT(h->Freeze(NULL)); +void RegisterMismatchedTypes2(const void* closure, upb::Handlers* h_ptr) { + upb::HandlersPtr h(h_ptr); + + upb::MessageDefPtr md(h.message_def()); + ASSERT(md); + upb::FieldDefPtr i32 = md.FindFieldByName("i32"); + upb::FieldDefPtr r_i32 = md.FindFieldByName("r_i32"); + upb::FieldDefPtr str = md.FindFieldByName("str"); + upb::FieldDefPtr r_str = md.FindFieldByName("r_str"); + upb::FieldDefPtr msg = md.FindFieldByName("msg"); + upb::FieldDefPtr r_msg = md.FindFieldByName("r_msg"); + ASSERT(i32); + ASSERT(r_i32); + ASSERT(str); + ASSERT(r_str); + ASSERT(msg); + ASSERT(r_msg); // For our second test we do the same in reverse. We directly set the type of // the frame and then observe failures at registering a Start* handler that // returns a different type. - h = upb::Handlers::New(md); // First establish the type of a sequence frame directly. - ASSERT(h->SetInt32Handler(r_i32, UpbMakeHandler(DoNothingInt32Handler))); + ASSERT(h.SetInt32Handler(r_i32, UpbMakeHandler(DoNothingInt32Handler))); // Now setting a StartSequence callback that returns a different type should // fail. - ASSERT(!h->SetStartSequenceHandler( + ASSERT(!h.SetStartSequenceHandler( r_i32, UpbMakeHandler(DoNothingStartHandler::Handler))); - ASSERT(h->SetStartSequenceHandler( + ASSERT(h.SetStartSequenceHandler( r_i32, UpbMakeHandler(DoNothingStartHandler::Handler))); // Establish a string frame directly. - ASSERT(h->SetStringHandler(r_str, + ASSERT(h.SetStringHandler(r_str, UpbMakeHandler(DoNothingStringBufHandler))); // Fail setting a StartString callback that returns a different type. - ASSERT(!h->SetStartStringHandler( + ASSERT(!h.SetStartStringHandler( r_str, UpbMakeHandler(DoNothingStartHandler::String))); - ASSERT(h->SetStartStringHandler( + ASSERT(h.SetStartStringHandler( r_str, UpbMakeHandler(DoNothingStartHandler::String))); // The previous established T1 as the frame for the r_str sequence. - ASSERT(!h->SetStartSequenceHandler( + ASSERT(!h.SetStartSequenceHandler( r_str, UpbMakeHandler(DoNothingStartHandler::Handler))); - ASSERT(h->SetStartSequenceHandler( + ASSERT(h.SetStartSequenceHandler( r_str, UpbMakeHandler(DoNothingStartHandler::Handler))); +} - // Now test for this error that is not caught until freeze time: - // Change-of-closure-type implies that a StartSequence or StartString handler - // should exist to return the closure type of the inner frame but no - // StartSequence/StartString handler is registered. - - h = upb::Handlers::New(md); - - // Establish T1 as top-level closure type. - ASSERT(h->SetInt32Handler(i32, UpbMakeHandler(DoNothingInt32Handler))); +void TestMismatchedTypes() { + // First create a schema for our test. + upb::SymbolTable symtab; + upb::HandlerCache handler_cache(&RegisterMismatchedTypes, nullptr); + upb::HandlerCache handler_cache2(&RegisterMismatchedTypes2, nullptr); + const upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr())); - // Establish T2 as closure type of sequence frame. - ASSERT( - h->SetInt32Handler(r_i32, UpbMakeHandler(DoNothingInt32Handler))); - - // Now attempt to freeze; this should fail because a StartSequence handler - // needs to be registered that takes a T1 and returns a T2. - ASSERT(!h->Freeze(NULL)); - - // Now if we register the necessary StartSequence handler, the freezing should - // work. - ASSERT(h->SetStartSequenceHandler( - r_i32, UpbMakeHandler(DoNothingStartHandler::Handler))); - h->ClearError(); - ASSERT(h->Freeze(NULL)); - - // Test for a broken chain that is two deep. - h = upb::Handlers::New(md); - - // Establish T1 as top-level closure type. - ASSERT(h->SetInt32Handler(i32, UpbMakeHandler(DoNothingInt32Handler))); - - // Establish T2 as the closure type of the string frame inside a sequence - // frame. - ASSERT(h->SetStringHandler(r_str, - UpbMakeHandler(DoNothingStringBufHandler))); - - // Now attempt to freeze; this should fail because a StartSequence or - // StartString handler needs to be registered that takes a T1 and returns a - // T2. - ASSERT(!h->Freeze(NULL)); - - // Now if we register a StartSequence handler it succeeds. - ASSERT(h->SetStartSequenceHandler( - r_str, UpbMakeHandler(DoNothingStartHandler::Handler))); - h->ClearError(); - ASSERT(h->Freeze(NULL)); - - // TODO(haberman): test that closure returned by StartSubMessage does not - // match top-level closure of sub-handlers. + // Now test the type-checking in handler registration. + handler_cache.Get(md); + handler_cache2.Get(md); } class IntIncrementer { @@ -889,17 +870,22 @@ class IntIncrementer { int* x_; }; +void RegisterIncrementor(const void* closure, upb::Handlers* h_ptr) { + const int* x = static_cast(closure); + upb::HandlersPtr h(h_ptr); + upb::FieldDefPtr f = h.message_def().FindFieldByName("i32"); + h.SetInt32Handler(f, UpbBind(&IntIncrementer::Handler, + new IntIncrementer(const_cast(x)))); +} void TestHandlerDataDestruction() { - upb::SymbolTable* symtab = upb::SymbolTable::New(); - const upb::MessageDef* md = upb_test_TestMessage_getmsgdef(symtab); - const upb::FieldDef* f = md->FindFieldByName("i32"); - int x = 0; + { - upb::reffed_ptr h(upb::Handlers::New(md)); - h->SetInt32Handler( - f, UpbBind(&IntIncrementer::Handler, new IntIncrementer(&x))); + upb::SymbolTable symtab; + upb::HandlerCache cache(&RegisterIncrementor, &x); + upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr())); + cache.Get(md); ASSERT(x == 1); } diff --git a/upb/handlers.c b/upb/handlers.c index ba27b98..ca978bf 100644 --- a/upb/handlers.c +++ b/upb/handlers.c @@ -91,7 +91,7 @@ static bool doset(upb_handlers *h, int32_t sel, const upb_fielddef *f, if (closure_type && *context_closure_type && closure_type != *context_closure_type) { - UPB_ASSERT(false); + return false; } if (closure_type) @@ -103,7 +103,7 @@ static bool doset(upb_handlers *h, int32_t sel, const upb_fielddef *f, const void *return_type = set_attr.return_closure_type; const void *table_return_type = h->table[sel].attr.return_closure_type; if (return_type && table_return_type && return_type != table_return_type) { - UPB_ASSERT(false); + return false; } if (table_return_type && !return_type) { @@ -168,7 +168,7 @@ bool checkstart(upb_handlers *h, const upb_fielddef *f, upb_handlertype_t type, return_closure_type = attr->return_closure_type; if (closure_type && return_closure_type && closure_type != return_closure_type) { - UPB_ASSERT(false); + return false; } return true; } diff --git a/upb/sink.h b/upb/sink.h index 1359c5e..1855542 100644 --- a/upb/sink.h +++ b/upb/sink.h @@ -326,18 +326,37 @@ class upb::Sink { * * For StartString(), the function will write a sink for the string to "sub." * The sub-sink must be used for any/all PutStringBuffer() calls. */ - bool StartString(HandlersPtr::Selector s, size_t size_hint, Sink* sub); + bool StartString(HandlersPtr::Selector s, size_t size_hint, Sink* sub) { + upb_sink sub_c; + bool ret = upb_sink_startstr(&sink_, s, size_hint, &sub_c); + *sub = sub_c; + return ret; + } + size_t PutStringBuffer(HandlersPtr::Selector s, const char *buf, size_t len, - const upb_bufhandle *handle); - bool EndString(HandlersPtr::Selector s); + const upb_bufhandle *handle) { + return upb_sink_putstring(&sink_, s, buf, len, handle); + } + + bool EndString(HandlersPtr::Selector s) { + return upb_sink_endstr(&sink_, s); + } /* For submessage fields. * * For StartSubMessage(), the function will write a sink for the string to * "sub." The sub-sink must be used for any/all handlers called within the * submessage. */ - bool StartSubMessage(HandlersPtr::Selector s, Sink* sub); - bool EndSubMessage(HandlersPtr::Selector s); + bool StartSubMessage(HandlersPtr::Selector s, Sink* sub) { + upb_sink sub_c; + bool ret = upb_sink_startsubmsg(&sink_, s, &sub_c); + *sub = sub_c; + return ret; + } + + bool EndSubMessage(HandlersPtr::Selector s) { + return upb_sink_endsubmsg(&sink_, s); + } /* For repeated fields of any type, the sequence of values must be wrapped in * these calls. @@ -345,8 +364,16 @@ class upb::Sink { * For StartSequence(), the function will write a sink for the string to * "sub." The sub-sink must be used for any/all handlers called within the * sequence. */ - bool StartSequence(HandlersPtr::Selector s, Sink* sub); - bool EndSequence(HandlersPtr::Selector s); + bool StartSequence(HandlersPtr::Selector s, Sink* sub) { + upb_sink sub_c; + bool ret = upb_sink_startseq(&sink_, s, &sub_c); + *sub = sub_c; + return ret; + } + + bool EndSequence(HandlersPtr::Selector s) { + return upb_sink_endseq(&sink_, s); + } /* Copy and assign specifically allowed. * We don't even bother making these members private because so many -- cgit v1.2.3 From a9c375f8ea81e52d832653c963da72033c9a98be Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 12 Jan 2019 21:23:13 -0800 Subject: Partway through refactoring of Arena. --- BUILD | 6 - upb/upb.c | 109 ++++-------------- upb/upb.h | 379 +++++++++----------------------------------------------------- 3 files changed, 77 insertions(+), 417 deletions(-) (limited to 'upb') diff --git a/BUILD b/BUILD index c6c401c..9e79d95 100644 --- a/BUILD +++ b/BUILD @@ -11,9 +11,6 @@ load( "upb_proto_reflection_library", ) -# Remove once our C++ wrappers are more sane. -CXX_COPTS = ["-Wno-unused-private-field"] - # C/C++ rules ################################################################## cc_library( @@ -192,7 +189,6 @@ cc_test( cc_test( name = "test_encoder", srcs = ["tests/pb/test_encoder.cc"], - copts = CXX_COPTS, data = ["google/protobuf/descriptor.pb"], deps = [ ":upb_cc_bindings", @@ -217,7 +213,6 @@ upb_proto_reflection_library( cc_test( name = "test_cpp", srcs = ["tests/test_cpp.cc"], - copts = CXX_COPTS, deps = [ ":test_cpp_upbproto", ":upb", @@ -251,7 +246,6 @@ cc_test( srcs = [ "tests/json/test_json.cc", ], - copts = CXX_COPTS, deps = [ ":test_json_upbproto", ":upb_json", diff --git a/upb/upb.c b/upb/upb.c index cbf5afb..68e26b0 100644 --- a/upb/upb.c +++ b/upb/upb.c @@ -25,18 +25,6 @@ static void nullz(upb_status *status) { memcpy(status->msg + sizeof(status->msg) - len, ellipsis, len); } - -/* upb_upberr *****************************************************************/ - -upb_errorspace upb_upberr = {"upb error"}; - -void upb_upberr_setoom(upb_status *status) { - __builtin_trap(); - status->error_space_ = &upb_upberr; - upb_status_seterrmsg(status, "Out of memory"); -} - - /* upb_status *****************************************************************/ void upb_status_clear(upb_status *status) { @@ -109,6 +97,30 @@ static size_t align_up_max(size_t size) { return ((size + maxalign - 1) / maxalign) * maxalign; } +struct upb_arena { + /* We implement the allocator interface. + * This must be the first member of upb_arena! */ + upb_alloc alloc; + + /* Allocator to allocate arena blocks. We are responsible for freeing these + * when we are destroyed. */ + upb_alloc *block_alloc; + + size_t bytes_allocated; + size_t next_block_size; + size_t max_block_size; + + /* Linked list of blocks. Points to an arena_block, defined in env.c */ + void *block_head; + + /* Cleanup entries. Pointer to a cleanup_ent, defined in env.c */ + void *cleanup_head; + + /* For future expansion, since the size of this struct is exposed to users. */ + void *future1; + void *future2; +} upb_arena; + typedef struct mem_block { struct mem_block *next; size_t size; @@ -255,76 +267,3 @@ bool upb_arena_addcleanup(upb_arena *a, upb_cleanup_func *func, void *ud) { size_t upb_arena_bytesallocated(const upb_arena *a) { return a->bytes_allocated; } - - -/* Standard error functions ***************************************************/ - -static bool default_err(void *ud, const upb_status *status) { - UPB_UNUSED(ud); - UPB_UNUSED(status); - return false; -} - -static bool write_err_to(void *ud, const upb_status *status) { - upb_status *copy_to = ud; - upb_status_copy(copy_to, status); - return false; -} - - -/* upb_env ********************************************************************/ - -void upb_env_initonly(upb_env *e) { - e->ok_ = true; - e->error_func_ = &default_err; - e->error_ud_ = NULL; -} - -void upb_env_init(upb_env *e) { - upb_arena_init(&e->arena_); - upb_env_initonly(e); -} - -void upb_env_init2(upb_env *e, void *mem, size_t n, upb_alloc *alloc) { - upb_arena_init2(&e->arena_, mem, n, alloc); - upb_env_initonly(e); -} - -void upb_env_uninit(upb_env *e) { - upb_arena_uninit(&e->arena_); -} - -void upb_env_seterrorfunc(upb_env *e, upb_error_func *func, void *ud) { - e->error_func_ = func; - e->error_ud_ = ud; -} - -void upb_env_reporterrorsto(upb_env *e, upb_status *s) { - e->error_func_ = &write_err_to; - e->error_ud_ = s; -} - -bool upb_env_reporterror(upb_env *e, const upb_status *status) { - e->ok_ = false; - return e->error_func_(e->error_ud_, status); -} - -void *upb_env_malloc(upb_env *e, size_t size) { - return upb_malloc(&e->arena_.alloc, size); -} - -void *upb_env_realloc(upb_env *e, void *ptr, size_t oldsize, size_t size) { - return upb_realloc(&e->arena_.alloc, ptr, oldsize, size); -} - -void upb_env_free(upb_env *e, void *ptr) { - upb_free(&e->arena_.alloc, ptr); -} - -bool upb_env_addcleanup(upb_env *e, upb_cleanup_func *func, void *ud) { - return upb_arena_addcleanup(&e->arena_, func, ud); -} - -size_t upb_env_bytesallocated(const upb_env *e) { - return upb_arena_bytesallocated(&e->arena_); -} diff --git a/upb/upb.h b/upb/upb.h index a75e311..4384812 100644 --- a/upb/upb.h +++ b/upb/upb.h @@ -74,9 +74,12 @@ template class InlinedEnvironment; #error Need implementations of [v]snprintf and va_copy #endif -#if ((defined(__cplusplus) && __cplusplus >= 201103L) || \ - defined(__GXX_EXPERIMENTAL_CXX0X__)) && !defined(UPB_NO_CXX11) -#define UPB_CXX11 +#if (defined(__cplusplus) && __cplusplus >= 201103L) || \ + defined(__GXX_EXPERIMENTAL_CXX0X__) || \ + (defined(_MSC_VER) && _MSC_VER >= 1900) +// C++11 is present +#else +#error upb requires C++11 for C++ support #endif /* UPB_DISALLOW_COPY_AND_ASSIGN() @@ -84,7 +87,6 @@ template class InlinedEnvironment; * * Declare these in the "private" section of a C++ class to forbid copy/assign * or all POD ops (construct, destruct, copy, assign) on that class. */ -#ifdef UPB_CXX11 #include #define UPB_DISALLOW_COPY_AND_ASSIGN(class_name) \ class_name(const class_name&) = delete; \ @@ -96,31 +98,17 @@ template class InlinedEnvironment; #define UPB_ASSERT_STDLAYOUT(type) \ static_assert(std::is_standard_layout::value, \ #type " must be standard layout"); -#define UPB_FINAL final -#else /* !defined(UPB_CXX11) */ -#define UPB_DISALLOW_COPY_AND_ASSIGN(class_name) \ - class_name(const class_name&); \ - void operator=(const class_name&); -#define UPB_DISALLOW_POD_OPS(class_name, full_class_name) \ - class_name(); \ - ~class_name(); \ - UPB_DISALLOW_COPY_AND_ASSIGN(class_name) -#define UPB_ASSERT_STDLAYOUT(type) -#define UPB_FINAL -#endif #ifdef __cplusplus #define UPB_BEGIN_EXTERN_C extern "C" { #define UPB_END_EXTERN_C } -#define UPB_PRIVATE_FOR_CPP private: #define UPB_DECLARE_TYPE(cppname, cname) typedef cppname cname; #else /* !defined(__cplusplus) */ #define UPB_BEGIN_EXTERN_C #define UPB_END_EXTERN_C -#define UPB_PRIVATE_FOR_CPP #define UPB_DECLARE_TYPE(cppname, cname) \ struct cname; \ typedef struct cname cname; @@ -150,119 +138,6 @@ template class InlinedEnvironment; #define UPB_UNREACHABLE() do { assert(0); } while(0) #endif -/* Generic function type. */ -typedef void upb_func(); - - -/* C++ Casts ******************************************************************/ - -#ifdef __cplusplus - -namespace upb { - -template class Pointer; - -/* Casts to a subclass. The caller must know that cast is correct; an - * incorrect cast will throw an assertion failure in debug mode. - * - * Example: - * upb::Def* def = GetDef(); - * // Assert-fails if this was not actually a MessageDef. - * upb::MessgeDef* md = upb::down_cast(def); - * - * Note that downcasts are only defined for some types (at the moment you can - * only downcast from a upb::Def to a specific Def type). */ -template To down_cast(From* f); - -/* Casts to a subclass. If the class does not actually match the given To type, - * returns NULL. - * - * Example: - * upb::Def* def = GetDef(); - * // md will be NULL if this was not actually a MessageDef. - * upb::MessgeDef* md = upb::down_cast(def); - * - * Note that dynamic casts are only defined for some types (at the moment you - * can only downcast from a upb::Def to a specific Def type).. */ -template To dyn_cast(From* f); - -/* Casts to any base class, or the type itself (ie. can be a no-op). - * - * Example: - * upb::MessageDef* md = GetDef(); - * // This will fail to compile if this wasn't actually a base class. - * upb::Def* def = upb::upcast(md); - */ -template inline Pointer upcast(T *f) { return Pointer(f); } - -/* Attempt upcast to specific base class. - * - * Example: - * upb::MessageDef* md = GetDef(); - * upb::upcast_to(md)->MethodOnDef(); - */ -template inline T* upcast_to(F *f) { - return static_cast(upcast(f)); -} - -/* PointerBase: implementation detail of upb::upcast(). - * It is implicitly convertable to pointers to the Base class(es). - */ -template -class PointerBase { - public: - explicit PointerBase(T* ptr) : ptr_(ptr) {} - operator T*() { return ptr_; } - operator Base*() { return (Base*)ptr_; } - - private: - T* ptr_; -}; - -template -class PointerBase2 : public PointerBase { - public: - explicit PointerBase2(T* ptr) : PointerBase(ptr) {} - operator Base2*() { return Pointer(*this); } -}; - -} - -#endif - -/* A list of types as they are encoded on-the-wire. */ -typedef enum { - UPB_WIRE_TYPE_VARINT = 0, - UPB_WIRE_TYPE_64BIT = 1, - UPB_WIRE_TYPE_DELIMITED = 2, - UPB_WIRE_TYPE_START_GROUP = 3, - UPB_WIRE_TYPE_END_GROUP = 4, - UPB_WIRE_TYPE_32BIT = 5 -} upb_wiretype_t; - - -/* upb::ErrorSpace ************************************************************/ - -/* A upb::ErrorSpace represents some domain of possible error values. This lets - * upb::Status attach specific error codes to operations, like POSIX/C errno, - * Win32 error codes, etc. Clients who want to know the very specific error - * code can check the error space and then know the type of the integer code. - * - * NOTE: upb::ErrorSpace is currently not used and should be considered - * experimental. It is important primarily in cases where upb is performing - * I/O, but upb doesn't currently have any components that do this. */ - -UPB_DECLARE_TYPE(upb::ErrorSpace, upb_errorspace) - -#ifdef __cplusplus -class upb::ErrorSpace { -#else -struct upb_errorspace { -#endif - const char *name; -}; - - /* upb::Status ****************************************************************/ /* upb::Status represents a success or failure status and error message. @@ -277,7 +152,6 @@ UPB_BEGIN_EXTERN_C const char *upb_status_errmsg(const upb_status *status); bool upb_ok(const upb_status *status); -upb_errorspace *upb_status_errspace(const upb_status *status); int upb_status_errcode(const upb_status *status); /* Any of the functions that write to a status object allow status to be NULL, @@ -342,33 +216,16 @@ struct upb_status { #define UPB_STATUS_INIT {true, 0, NULL, {0}} +/** upb_alloc *****************************************************************/ -/** Built-in error spaces. ****************************************************/ - -/* Errors raised by upb that we want to be able to detect programmatically. */ -typedef enum { - UPB_NOMEM /* Can't reuse ENOMEM because it is POSIX, not ISO C. */ -} upb_errcode_t; - -extern upb_errorspace upb_upberr; - -void upb_upberr_setoom(upb_status *s); - -/* Since errno is defined by standard C, we define an error space for it in - * core upb. Other error spaces should be defined in other, platform-specific - * modules. */ - -extern upb_errorspace upb_errnoerr; - - -/** upb::Allocator ************************************************************/ - -/* A upb::Allocator is a possibly-stateful allocator object. +/* A upb_alloc is a possibly-stateful allocator object. * * It could either be an arena allocator (which doesn't require individual * free() calls) or a regular malloc() (which does). The client must therefore * free memory unless it knows that the allocator is an arena allocator. */ -UPB_DECLARE_TYPE(upb::Allocator, upb_alloc) + +struct upb_alloc; +typedef struct upb_alloc upb_alloc; /* A malloc()/free() function. * If "size" is 0 then the function acts like free(), otherwise it acts like @@ -376,19 +233,7 @@ UPB_DECLARE_TYPE(upb::Allocator, upb_alloc) typedef void *upb_alloc_func(upb_alloc *alloc, void *ptr, size_t oldsize, size_t size); -#ifdef __cplusplus - -class upb::Allocator UPB_FINAL { - public: - Allocator() {} - - private: - UPB_DISALLOW_COPY_AND_ASSIGN(Allocator) - - public: -#else struct upb_alloc { -#endif /* __cplusplus */ upb_alloc_func *func; }; @@ -429,43 +274,47 @@ UPB_INLINE void upb_gfree(void *ptr) { upb_free(&upb_alloc_global, ptr); } -/* upb::Arena *****************************************************************/ +/* upb_arena ******************************************************************/ -/* upb::Arena is a specific allocator implementation that uses arena allocation. +/* upb_arena is a specific allocator implementation that uses arena allocation. * The user provides an allocator that will be used to allocate the underlying * arena blocks. Arenas by nature do not require the individual allocations * to be freed. However the Arena does allow users to register cleanup * functions that will run when the arena is destroyed. * - * A upb::Arena is *not* thread-safe. + * A upb_arena is *not* thread-safe. * * You could write a thread-safe arena allocator that satisfies the - * upb::Allocator interface, but it would not be as efficient for the + * upb_alloc interface, but it would not be as efficient for the * single-threaded case. */ -UPB_DECLARE_TYPE(upb::Arena, upb_arena) typedef void upb_cleanup_func(void *ud); -#define UPB_ARENA_BLOCK_OVERHEAD (sizeof(size_t)*4) +struct upb_arena; +typedef upb_arena upb_arena; UPB_BEGIN_EXTERN_C -void upb_arena_init(upb_arena *a); -void upb_arena_init2(upb_arena *a, void *mem, size_t n, upb_alloc *alloc); -void upb_arena_uninit(upb_arena *a); +upb_arena *upb_arena_new2(void *mem, size_t n, upb_alloc *alloc); +void upb_arena_free(upb_arena *a); bool upb_arena_addcleanup(upb_arena *a, upb_cleanup_func *func, void *ud); size_t upb_arena_bytesallocated(const upb_arena *a); -void upb_arena_setnextblocksize(upb_arena *a, size_t size); -void upb_arena_setmaxblocksize(upb_arena *a, size_t size); + UPB_INLINE upb_alloc *upb_arena_alloc(upb_arena *a) { return (upb_alloc*)a; } + UPB_INLINE void *upb_arena_malloc(upb_arena *a, size_t size) { return upb_malloc(upb_arena_alloc(a), size); } + UPB_INLINE void *upb_arena_realloc(upb_arena *a, void *ptr, size_t oldsize, size_t size) { return upb_realloc(upb_arena_alloc(a), ptr, oldsize, size); } +UPB_INLINE upb_arena *upb_arena_new() { + return upb_arena_new2(NULL, 0, &upb_alloc_global); +} + UPB_END_EXTERN_C #ifdef __cplusplus @@ -473,7 +322,9 @@ UPB_END_EXTERN_C class upb::Arena { public: /* A simple arena with no initial memory block and the default allocator. */ - Arena() { upb_arena_init(this); } + Arena() { upb_arena_init(&arena_); } + + upb_arena* ptr() { return &arena_; } /* Constructs an arena with the given initial block which allocates blocks * with the given allocator. The given allocator must outlive the Arena. @@ -481,167 +332,40 @@ class upb::Arena { * If you pass NULL for the allocator it will default to the global allocator * upb_alloc_global, and NULL/0 for the initial block will cause there to be * no initial block. */ - Arena(void *mem, size_t len, Allocator* a) { - upb_arena_init2(this, mem, len, a); + Arena(void *mem, size_t len, upb_alloc *a) { + upb_arena_init2(&arena_, mem, len, a); } - ~Arena() { upb_arena_uninit(this); } - - /* Sets the size of the next block the Arena will request (unless the - * requested allocation is larger). Each block will double in size until the - * max limit is reached. */ - void SetNextBlockSize(size_t size) { upb_arena_setnextblocksize(this, size); } - - /* Sets the maximum block size. No blocks larger than this will be requested - * from the underlying allocator unless individual arena allocations are - * larger. */ - void SetMaxBlockSize(size_t size) { upb_arena_setmaxblocksize(this, size); } + ~Arena() { upb_arena_uninit(&arena_); } /* Allows this arena to be used as a generic allocator. * * The arena does not need free() calls so when using Arena as an allocator * it is safe to skip them. However they are no-ops so there is no harm in * calling free() either. */ - Allocator* allocator() { return upb_arena_alloc(this); } + upb_alloc *allocator() { return upb_arena_alloc(&arena_); } /* Add a cleanup function to run when the arena is destroyed. * Returns false on out-of-memory. */ - bool AddCleanup(upb_cleanup_func* func, void* ud) { - return upb_arena_addcleanup(this, func, ud); + bool AddCleanup(upb_cleanup_func *func, void *ud) { + return upb_arena_addcleanup(&arena_, func, ud); } /* Total number of bytes that have been allocated. It is undefined what - * Realloc() does to this counter. */ - size_t BytesAllocated() const { - return upb_arena_bytesallocated(this); - } + * Realloc() does to &arena_ counter. */ + size_t BytesAllocated() const { return upb_arena_bytesallocated(&arena_); } private: UPB_DISALLOW_COPY_AND_ASSIGN(Arena) - -#else -struct upb_arena { -#endif /* __cplusplus */ - /* We implement the allocator interface. - * This must be the first member of upb_arena! */ - upb_alloc alloc; - - /* Allocator to allocate arena blocks. We are responsible for freeing these - * when we are destroyed. */ - upb_alloc *block_alloc; - - size_t bytes_allocated; - size_t next_block_size; - size_t max_block_size; - - /* Linked list of blocks. Points to an arena_block, defined in env.c */ - void *block_head; - - /* Cleanup entries. Pointer to a cleanup_ent, defined in env.c */ - void *cleanup_head; - - /* For future expansion, since the size of this struct is exposed to users. */ - void *future1; - void *future2; -}; - - -/* upb::Environment ***********************************************************/ - -/* A upb::Environment provides a means for injecting malloc and an - * error-reporting callback into encoders/decoders. This allows them to be - * independent of nearly all assumptions about their actual environment. - * - * It is also a container for allocating the encoders/decoders themselves that - * insulates clients from knowing their actual size. This provides ABI - * compatibility even if the size of the objects change. And this allows the - * structure definitions to be in the .c files instead of the .h files, making - * the .h files smaller and more readable. - * - * We might want to consider renaming this to "Pipeline" if/when the concept of - * a pipeline element becomes more formalized. */ -UPB_DECLARE_TYPE(upb::Environment, upb_env) - -/* A function that receives an error report from an encoder or decoder. The - * callback can return true to request that the error should be recovered, but - * if the error is not recoverable this has no effect. */ -typedef bool upb_error_func(void *ud, const upb_status *status); - -UPB_BEGIN_EXTERN_C - -void upb_env_init(upb_env *e); -void upb_env_init2(upb_env *e, void *mem, size_t n, upb_alloc *alloc); -void upb_env_uninit(upb_env *e); - -void upb_env_initonly(upb_env *e); - -UPB_INLINE upb_arena *upb_env_arena(upb_env *e) { return (upb_arena*)e; } -bool upb_env_ok(const upb_env *e); -void upb_env_seterrorfunc(upb_env *e, upb_error_func *func, void *ud); - -/* Convenience wrappers around the methods of the contained arena. */ -void upb_env_reporterrorsto(upb_env *e, upb_status *s); -bool upb_env_reporterror(upb_env *e, const upb_status *s); -void *upb_env_malloc(upb_env *e, size_t size); -void *upb_env_realloc(upb_env *e, void *ptr, size_t oldsize, size_t size); -void upb_env_free(upb_env *e, void *ptr); -bool upb_env_addcleanup(upb_env *e, upb_cleanup_func *func, void *ud); -size_t upb_env_bytesallocated(const upb_env *e); - -UPB_END_EXTERN_C - -#ifdef __cplusplus - -class upb::Environment { - public: - /* The given Arena must outlive this environment. */ - Environment() { upb_env_initonly(this); } - - Environment(void *mem, size_t len, Allocator *a) : arena_(mem, len, a) { - upb_env_initonly(this); - } - - Arena* arena() { return upb_env_arena(this); } - - /* Set a custom error reporting function. */ - void SetErrorFunction(upb_error_func* func, void* ud) { - upb_env_seterrorfunc(this, func, ud); - } - - /* Set the error reporting function to simply copy the status to the given - * status and abort. */ - void ReportErrorsTo(Status* status) { upb_env_reporterrorsto(this, status); } - - /* Returns true if all allocations and AddCleanup() calls have succeeded, - * and no errors were reported with ReportError() (except ones that recovered - * successfully). */ - bool ok() const { return upb_env_ok(this); } - - /* Reports an error to this environment's callback, returning true if - * the caller should try to recover. */ - bool ReportError(const Status* status) { - return upb_env_reporterror(this, status); - } - - private: - UPB_DISALLOW_COPY_AND_ASSIGN(Environment) - -#else -struct upb_env { -#endif /* __cplusplus */ upb_arena arena_; - upb_error_func *error_func_; - void *error_ud_; - bool ok_; }; +#endif /* upb::InlinedArena **********************************************************/ -/* upb::InlinedEnvironment ****************************************************/ -/* upb::InlinedArena and upb::InlinedEnvironment seed their arenas with a - * predefined amount of memory. No heap memory will be allocated until the - * initial block is exceeded. +/* upb::InlinedArena seeds the arenas with a predefined amount of memory. No + * heap memory will be allocated until the initial block is exceeded. * * These types only exist in C++ */ @@ -658,19 +382,22 @@ template class upb::InlinedArena : public upb::Arena { char initial_block_[N + UPB_ARENA_BLOCK_OVERHEAD]; }; -template class upb::InlinedEnvironment : public upb::Environment { - public: - InlinedEnvironment() : Environment(initial_block_, N, NULL) {} - explicit InlinedEnvironment(Allocator *a) - : Environment(initial_block_, N, a) {} +#endif /* __cplusplus */ - private: - UPB_DISALLOW_COPY_AND_ASSIGN(InlinedEnvironment) +/* Constants ******************************************************************/ - char initial_block_[N + UPB_ARENA_BLOCK_OVERHEAD]; -}; +/* Generic function type. */ +typedef void upb_func(); -#endif /* __cplusplus */ +/* A list of types as they are encoded on-the-wire. */ +typedef enum { + UPB_WIRE_TYPE_VARINT = 0, + UPB_WIRE_TYPE_64BIT = 1, + UPB_WIRE_TYPE_DELIMITED = 2, + UPB_WIRE_TYPE_START_GROUP = 3, + UPB_WIRE_TYPE_END_GROUP = 4, + UPB_WIRE_TYPE_32BIT = 5 +} upb_wiretype_t; /* The types a field can have. Note that this list is not identical to the * types defined in descriptor.proto, which gives INT32 and SINT32 separate -- cgit v1.2.3 From cb26d883d1290ed258e5594454c2ffe0526b13f9 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Mon, 14 Jan 2019 10:56:58 -0800 Subject: WIP. --- google/protobuf/descriptor.upb.h | 8 +- tests/conformance_upb.c | 16 +-- tests/json/test_json.cc | 4 +- tests/pb/test_decoder.cc | 14 +-- tests/pb/test_encoder.cc | 9 +- tests/test_cpp.cc | 2 +- tests/test_util.h | 21 ++-- upb/bindings/lua/def.c | 9 +- upb/bindings/lua/msg.c | 13 +- upb/bindings/lua/upb/pb.c | 8 +- upb/decode.h | 8 +- upb/def.c | 41 ++++--- upb/def.h | 37 +++--- upb/encode.h | 8 +- upb/generated_util.h | 3 +- upb/handlers.c | 8 +- upb/handlers.h | 39 +++--- upb/json/parser.c | 259 ++++++++++++++++----------------------- upb/json/parser.h | 37 +++--- upb/json/parser.rl | 153 +++++++++-------------- upb/json/printer.c | 8 +- upb/json/printer.h | 14 ++- upb/msg.h | 28 ++--- upb/msgfactory.h | 12 +- upb/pb/compile_decoder.c | 2 +- upb/pb/decoder.c | 22 ++-- upb/pb/decoder.h | 28 +++-- upb/pb/decoder.int.h | 4 +- upb/pb/encoder.c | 22 ++-- upb/pb/encoder.h | 14 ++- upb/pb/textprinter.c | 26 ++-- upb/pb/textprinter.h | 75 +++++------- upb/sink.c | 70 ----------- upb/sink.h | 33 ++--- upb/table.int.h | 15 --- upb/upb.c | 75 +++++------- upb/upb.h | 161 +++++++++--------------- upbc/generator.cc | 8 +- 38 files changed, 555 insertions(+), 759 deletions(-) (limited to 'upb') diff --git a/google/protobuf/descriptor.upb.h b/google/protobuf/descriptor.upb.h index 32bccc7..7e62be5 100644 --- a/google/protobuf/descriptor.upb.h +++ b/google/protobuf/descriptor.upb.h @@ -16,7 +16,9 @@ #include "upb/decode.h" #include "upb/encode.h" #include "upb/port_def.inc" -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif struct google_protobuf_FileDescriptorSet; struct google_protobuf_FileDescriptorProto; @@ -1668,7 +1670,9 @@ UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_end(google_prot } -UPB_END_EXTERN_C +#ifdef __cplusplus +} /* extern "C" */ +#endif #include "upb/port_undef.inc" diff --git a/tests/conformance_upb.c b/tests/conformance_upb.c index e0a7d8c..cefac4c 100644 --- a/tests/conformance_upb.c +++ b/tests/conformance_upb.c @@ -130,7 +130,7 @@ void DoTest( } bool DoTestIo() { - upb_arena arena; + upb_arena *arena; upb_alloc *alloc; upb_status status; char *serialized_input; @@ -145,8 +145,8 @@ bool DoTestIo() { return false; } - upb_arena_init(&arena); - alloc = upb_arena_alloc(&arena); + arena = upb_arena_new(); + alloc = upb_arena_alloc(arena); serialized_input = upb_malloc(alloc, input_size); if (!CheckedRead(STDIN_FILENO, serialized_input, input_size)) { @@ -155,24 +155,26 @@ bool DoTestIo() { } request = conformance_ConformanceRequest_parsenew( - upb_stringview_make(serialized_input, input_size), &arena); - response = conformance_ConformanceResponse_new(&arena); + upb_stringview_make(serialized_input, input_size), arena); + response = conformance_ConformanceResponse_new(arena); if (request) { - DoTest(request, response, &arena); + DoTest(request, response, arena); } else { fprintf(stderr, "conformance_upb: parse of ConformanceRequest failed: %s\n", upb_status_errmsg(&status)); } serialized_output = conformance_ConformanceResponse_serialize( - response, &arena, &output_size); + response, arena, &output_size); CheckedWrite(STDOUT_FILENO, &output_size, sizeof(uint32_t)); CheckedWrite(STDOUT_FILENO, serialized_output, output_size); test_count++; + upb_arena_free(arena); + return true; } diff --git a/tests/json/test_json.cc b/tests/json/test_json.cc index b0fd3e3..1f7d364 100644 --- a/tests/json/test_json.cc +++ b/tests/json/test_json.cc @@ -174,9 +174,9 @@ void test_json_roundtrip_message(const char* json_src, VerboseParserEnvironment env(verbose); StringSink data_sink; upb::json::PrinterPtr printer = upb::json::PrinterPtr::Create( - env.env(), serialize_handlers, data_sink.Sink()); + env.arena(), serialize_handlers, data_sink.Sink()); upb::json::ParserPtr parser = upb::json::ParserPtr::Create( - env.env(), parser_method, NULL, printer.input(), false); + env.arena(), parser_method, NULL, printer.input(), false); env.ResetBytesSink(parser.input()); env.Reset(json_src, strlen(json_src), false, false); diff --git a/tests/pb/test_decoder.cc b/tests/pb/test_decoder.cc index ec7a788..b2dd812 100644 --- a/tests/pb/test_decoder.cc +++ b/tests/pb/test_decoder.cc @@ -452,10 +452,10 @@ void callback(const void *closure, upb::Handlers* h_ptr) { const upb::Handlers *global_handlers; upb::pb::DecoderMethodPtr global_method; -upb::pb::DecoderPtr CreateDecoder(upb::Environment* env, +upb::pb::DecoderPtr CreateDecoder(upb::Arena* arena, upb::pb::DecoderMethodPtr method, upb::Sink sink) { - upb::pb::DecoderPtr ret = upb::pb::DecoderPtr::Create(env, method, sink); + upb::pb::DecoderPtr ret = upb::pb::DecoderPtr::Create(arena, method, sink); ret.set_max_nesting(MAX_NESTING); return ret; } @@ -556,7 +556,7 @@ void do_run_decoder(VerboseParserEnvironment* env, upb::pb::DecoderPtr decoder, void run_decoder(const string& proto, const string* expected_output) { VerboseParserEnvironment env(filter_hash != 0); upb::Sink sink(global_handlers, &closures[0]); - upb::pb::DecoderPtr decoder = CreateDecoder(env.env(), global_method, sink); + upb::pb::DecoderPtr decoder = CreateDecoder(env.arena(), global_method, sink); env.ResetBytesSink(decoder.input()); for (size_t i = 0; i < proto.size(); i++) { for (size_t j = i; j < UPB_MIN(proto.size(), i + 5); j++) { @@ -872,10 +872,9 @@ void test_valid() { if (!filter_hash || filter_hash == testhash) { testhash = emptyhash; upb::Status status; - upb::Environment env; - env.ReportErrorsTo(&status); + upb::Arena arena; upb::Sink sink(global_handlers, &closures[0]); - upb::pb::DecoderPtr decoder = CreateDecoder(&env, global_method, sink); + upb::pb::DecoderPtr decoder = CreateDecoder(&arena, global_method, sink); output.clear(); bool ok = upb::PutBuffer(std::string(), decoder.input()); ASSERT(ok); @@ -1161,7 +1160,8 @@ void test_emptyhandlers(upb::SymbolTable* symtab, bool allowjit) { for (int i = 0; testdata[i].data; i++) { VerboseParserEnvironment env(filter_hash != 0); upb::Sink sink(global_method.dest_handlers(), &closures[0]); - upb::pb::DecoderPtr decoder = CreateDecoder(env.env(), global_method, sink); + upb::pb::DecoderPtr decoder = + CreateDecoder(env.arena(), global_method, sink); env.ResetBytesSink(decoder.input()); env.Reset(testdata[i].data, testdata[i].length, true, false); ASSERT(env.Start()); diff --git a/tests/pb/test_encoder.cc b/tests/pb/test_encoder.cc index 35c0e1e..7145097 100644 --- a/tests/pb/test_encoder.cc +++ b/tests/pb/test_encoder.cc @@ -24,7 +24,7 @@ void test_pb_roundtrip() { upb::Arena arena; google_protobuf_FileDescriptorSet *set = google_protobuf_FileDescriptorSet_parsenew( - upb_stringview_make(input.c_str(), input.size()), &arena); + upb_stringview_make(input.c_str(), input.size()), arena.ptr()); ASSERT(set); size_t n; const google_protobuf_FileDescriptorProto *const *files = @@ -33,7 +33,7 @@ void test_pb_roundtrip() { upb::Status status; bool ok = symtab.AddFile(files[0], &status); if (!ok) { - fprintf(stderr, "Error building def: %s\n", upb_status_errmsg(&status)); + fprintf(stderr, "Error building def: %s\n", status.error_message()); ASSERT(false); } upb::MessageDefPtr md = @@ -43,13 +43,12 @@ void test_pb_roundtrip() { ASSERT(encoder_handlers); const upb::pb::DecoderMethodPtr method = decoder_cache.Get(md); - upb::InlinedEnvironment<512> env; std::string output; upb::StringSink string_sink(&output); upb::pb::EncoderPtr encoder = - upb::pb::EncoderPtr::Create(&env, encoder_handlers, string_sink.input()); + upb::pb::EncoderPtr::Create(&arena, encoder_handlers, string_sink.input()); upb::pb::DecoderPtr decoder = - upb::pb::DecoderPtr::Create(&env, method, encoder.input()); + upb::pb::DecoderPtr::Create(&arena, method, encoder.input()); ok = upb::PutBuffer(input, decoder.input()); ASSERT(ok); ASSERT(input == output); diff --git a/tests/test_cpp.cc b/tests/test_cpp.cc index a5bfbc7..5e9a8dd 100644 --- a/tests/test_cpp.cc +++ b/tests/test_cpp.cc @@ -672,7 +672,7 @@ void DoNothingStringBufHandler(C* closure, const char *buf, size_t len) { } template -void DoNothingEndMessageHandler(C* closure, upb::Status *status) { +void DoNothingEndMessageHandler(C* closure, upb_status *status) { UPB_UNUSED(closure); UPB_UNUSED(status); } diff --git a/tests/test_util.h b/tests/test_util.h index 0b5ddd4..04ca3fb 100644 --- a/tests/test_util.h +++ b/tests/test_util.h @@ -30,15 +30,11 @@ upb_bufhandle global_handle; class VerboseParserEnvironment { public: /* Pass verbose=true to print detailed diagnostics to stderr. */ - VerboseParserEnvironment(bool verbose) : verbose_(verbose) { - env_.SetErrorFunction(&VerboseParserEnvironment::OnError, this); - } + VerboseParserEnvironment(bool verbose) : verbose_(verbose) {} static bool OnError(void *ud, const upb::Status* status) { VerboseParserEnvironment* env = static_cast(ud); - env->saw_error_ = true; - if (env->expect_error_ && env->verbose_) { fprintf(stderr, "Encountered error, as expected: "); } else if (!env->expect_error_) { @@ -56,7 +52,6 @@ class VerboseParserEnvironment { len_ = len; ofs_ = 0; expect_error_ = expect_error; - saw_error_ = false; end_ok_set_ = false; skip_until_ = may_skip ? 0 : -1; skipped_with_null_ = false; @@ -94,12 +89,12 @@ class VerboseParserEnvironment { bool CheckConsistency() { /* If we called end (which we should only do when previous bytes are fully * accepted), then end() should return true iff there were no errors. */ - if (end_ok_set_ && end_ok_ != !saw_error_) { + if (end_ok_set_ && end_ok_ != status_.ok()) { fprintf(stderr, "End() status and saw_error didn't match.\n"); return false; } - if (expect_error_ && !saw_error_) { + if (expect_error_ && status_.ok()) { fprintf(stderr, "Expected error but saw none.\n"); return false; } @@ -158,8 +153,9 @@ class VerboseParserEnvironment { } } - if (saw_error_) + if (!status_.ok()) { return false; + } if (parsed > bytes && skip_until_ >= 0) { skip_until_ = ofs_ + parsed; @@ -175,12 +171,14 @@ class VerboseParserEnvironment { } size_t ofs() { return ofs_; } - upb::Environment* env() { return &env_; } bool SkippedWithNull() { return skipped_with_null_; } + upb::Arena* arena() { return &arena_; } + private: - upb::Environment env_; + upb::Arena arena_; + upb::Status status_; upb::BytesSink sink_; const char* buf_; size_t len_; @@ -188,7 +186,6 @@ class VerboseParserEnvironment { size_t ofs_; void *subc_; bool expect_error_; - bool saw_error_; bool end_ok_; bool end_ok_set_; diff --git a/upb/bindings/lua/def.c b/upb/bindings/lua/def.c index 76510be..c38b6d1 100644 --- a/upb/bindings/lua/def.c +++ b/upb/bindings/lua/def.c @@ -15,13 +15,14 @@ #define LUPB_SYMTAB "lupb.symtab" #define LUPB_OBJCACHE "lupb.objcache" -#define CHK(pred) do { \ - upb_status status = UPB_STATUS_INIT; \ - pred; \ +#define CHK(pred) \ + do { \ + upb_status status; \ + upb_status_clear(&status); \ + pred; \ lupb_checkstatus(L, &status); \ } while (0) - /* lupb_wrapper ***************************************************************/ /* Wrappers around upb objects. */ diff --git a/upb/bindings/lua/msg.c b/upb/bindings/lua/msg.c index e983f46..df5a143 100644 --- a/upb/bindings/lua/msg.c +++ b/upb/bindings/lua/msg.c @@ -100,16 +100,21 @@ static void *lupb_newuserdata(lua_State *L, size_t size, const char *type) { * it is an internal memory management detail. Other objects refer to this * object from their userdata to keep the arena-owned data alive. */ +typedef struct { + upb_arena *arena; +} lupb_arena; + upb_arena *lupb_arena_check(lua_State *L, int narg) { - return luaL_checkudata(L, narg, LUPB_ARENA); + lupb_arena *a = luaL_checkudata(L, narg, LUPB_ARENA); + return a ? a->arena : NULL; } int lupb_arena_new(lua_State *L) { - upb_arena *a = lupb_newuserdata(L, sizeof(upb_arena), LUPB_ARENA); + lupb_arena *a = lupb_newuserdata(L, sizeof(lupb_arena), LUPB_ARENA); /* TODO(haberman): use Lua alloc func as block allocator? Would need to * verify that all cases of upb_malloc in msg/table are longjmp-safe. */ - upb_arena_init(a); + a->arena = upb_arena_new(); return 1; } @@ -140,7 +145,7 @@ static void lupb_arena_initsingleton(lua_State *L) { static int lupb_arena_gc(lua_State *L) { upb_arena *a = lupb_arena_check(L, 1); - upb_arena_uninit(a); + upb_arena_free(a); return 0; } diff --git a/upb/bindings/lua/upb/pb.c b/upb/bindings/lua/upb/pb.c index bca2ee8..2edefe0 100644 --- a/upb/bindings/lua/upb/pb.c +++ b/upb/bindings/lua/upb/pb.c @@ -27,17 +27,15 @@ static int lupb_pb_decode(lua_State *L) { static int lupb_pb_encode(lua_State *L) { const upb_msglayout *layout; const upb_msg *msg = lupb_msg_checkmsg2(L, 1, &layout); - upb_arena arena; + upb_arena *arena = upb_arena_new(); size_t size; char *result; - upb_arena_init(&arena); - - result = upb_encode(msg, (const void*)layout, &arena, &size); + result = upb_encode(msg, (const void*)layout, arena, &size); /* Free resources before we potentially bail on error. */ lua_pushlstring(L, result, size); - upb_arena_uninit(&arena); + upb_arena_free(arena); /* TODO(haberman): check for error. */ return 1; diff --git a/upb/decode.h b/upb/decode.h index 79774ed..790d7ef 100644 --- a/upb/decode.h +++ b/upb/decode.h @@ -7,10 +7,14 @@ #include "upb/msg.h" -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif bool upb_decode(upb_stringview buf, upb_msg *msg, const upb_msglayout *l); -UPB_END_EXTERN_C +#ifdef __cplusplus +} /* extern "C" */ +#endif #endif /* UPB_DECODE_H_ */ diff --git a/upb/def.c b/upb/def.c index 27de875..c744138 100644 --- a/upb/def.c +++ b/upb/def.c @@ -125,7 +125,7 @@ static upb_value pack_def(const void *ptr, upb_deftype_t type) { } struct upb_symtab { - upb_arena arena; + upb_arena *arena; upb_strtable syms; /* full_name -> packed def ptr */ upb_strtable files; /* file_name -> upb_filedef* */ }; @@ -224,7 +224,7 @@ static bool assign_msg_indices(upb_msgdef *m, upb_status *s) { fields = upb_gmalloc(n * sizeof(*fields)); if (!fields) { - upb_upberr_setoom(s); + upb_status_setoom(s); return false; } @@ -870,7 +870,7 @@ const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i) { } void upb_symtab_free(upb_symtab *s) { - upb_arena_uninit(&s->arena); + upb_arena_free(s->arena); upb_gfree(s); } @@ -882,12 +882,12 @@ upb_symtab *upb_symtab_new() { return NULL; } - upb_arena_init(&s->arena); - alloc = upb_arena_alloc(&s->arena); + s->arena = upb_arena_new(); + alloc = upb_arena_alloc(s->arena); if (!upb_strtable_init2(&s->syms, UPB_CTYPE_CONSTPTR, alloc) || !upb_strtable_init2(&s->files, UPB_CTYPE_CONSTPTR, alloc)) { - upb_arena_uninit(&s->arena); + upb_arena_free(s->arena); upb_gfree(s); s = NULL; } @@ -922,7 +922,7 @@ const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym) { * to validate important constraints like uniqueness of names and numbers. */ #define CHK(x) if (!(x)) { return false; } -#define CHK_OOM(x) if (!(x)) { upb_upberr_setoom(ctx->status); return false; } +#define CHK_OOM(x) if (!(x)) { upb_status_setoom(ctx->status); return false; } typedef struct { const upb_symtab *symtab; @@ -1632,7 +1632,7 @@ static bool build_filedef( static bool upb_symtab_addtotabs(upb_symtab *s, symtab_addctx *ctx, upb_status *status) { const upb_filedef *file = ctx->file; - upb_alloc *alloc = upb_arena_alloc(&s->arena); + upb_alloc *alloc = upb_arena_alloc(s->arena); upb_strtable_iter iter; CHK_OOM(upb_strtable_insert3(&s->files, file->name, strlen(file->name), @@ -1652,9 +1652,9 @@ static bool upb_symtab_addtotabs(upb_symtab *s, symtab_addctx *ctx, bool upb_symtab_addfile(upb_symtab *s, const google_protobuf_FileDescriptorProto *file_proto, upb_status *status) { - upb_arena tmparena; + upb_arena *tmparena = upb_arena_new(); upb_strtable addtab; - upb_alloc *alloc = upb_arena_alloc(&s->arena); + upb_alloc *alloc = upb_arena_alloc(s->arena); upb_filedef *file = upb_malloc(alloc, sizeof(*file)); bool ok; symtab_addctx ctx; @@ -1662,18 +1662,16 @@ bool upb_symtab_addfile(upb_symtab *s, ctx.file = file; ctx.symtab = s; ctx.alloc = alloc; - ctx.tmp = upb_arena_alloc(&tmparena); + ctx.tmp = upb_arena_alloc(tmparena); ctx.addtab = &addtab; ctx.status = status; - upb_arena_init(&tmparena); - ok = file && upb_strtable_init2(&addtab, UPB_CTYPE_CONSTPTR, ctx.tmp) && build_filedef(&ctx, file, file_proto) && upb_symtab_addtotabs(s, &ctx, status); - upb_arena_uninit(&tmparena); + upb_arena_free(tmparena); return ok; } @@ -1685,19 +1683,22 @@ bool _upb_symtab_loaddefinit(upb_symtab *s, const upb_def_init *init) { * print errors to stderr instead of returning error status to the user. */ upb_def_init **deps = init->deps; google_protobuf_FileDescriptorProto *file; - upb_arena arena; - upb_status status = UPB_STATUS_INIT; + upb_arena *arena; + upb_status status; + + upb_status_clear(&status); if (upb_strtable_lookup(&s->files, init->filename, NULL)) { return true; } + arena = upb_arena_new(); + for (; *deps; deps++) { if (!_upb_symtab_loaddefinit(s, *deps)) goto err; } - upb_arena_init(&arena); - file = google_protobuf_FileDescriptorProto_parsenew(init->descriptor, &arena); + file = google_protobuf_FileDescriptorProto_parsenew(init->descriptor, arena); if (!file) { upb_status_seterrf( @@ -1710,13 +1711,13 @@ bool _upb_symtab_loaddefinit(upb_symtab *s, const upb_def_init *init) { if (!upb_symtab_addfile(s, file, &status)) goto err; - upb_arena_uninit(&arena); + upb_arena_free(arena); return true; err: fprintf(stderr, "Error loading compiled-in descriptor: %s\n", upb_status_errmsg(&status)); - upb_arena_uninit(&arena); + upb_arena_free(arena); return false; } diff --git a/upb/def.h b/upb/def.h index fb8a71d..b1cf275 100644 --- a/upb/def.h +++ b/upb/def.h @@ -56,7 +56,9 @@ typedef struct upb_symtab upb_symtab; * protobuf wire format. */ #define UPB_MAX_FIELDNUMBER ((1 << 29) - 1) -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif const char *upb_fielddef_fullname(const upb_fielddef *f); upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f); @@ -93,9 +95,8 @@ const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f); /* Internal only. */ uint32_t upb_fielddef_selectorbase(const upb_fielddef *f); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ /* A upb_fielddef describes a single field in a message. It is most often * found as a part of a upb_msgdef, but can also stand alone to represent @@ -228,7 +229,9 @@ class upb::FieldDefPtr { /* upb_oneofdef ***************************************************************/ -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif typedef upb_inttable_iter upb_oneof_iter; @@ -262,9 +265,8 @@ void upb_oneof_iter_setdone(upb_oneof_iter *iter); bool upb_oneof_iter_isequal(const upb_oneof_iter *iter1, const upb_oneof_iter *iter2); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ /* Class that represents a oneof. */ class upb::OneofDefPtr { @@ -365,7 +367,9 @@ typedef upb_strtable_iter upb_msg_oneof_iter; #define UPB_TIMESTAMP_SECONDS 1 #define UPB_TIMESTAMP_NANOS 2 -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif const char *upb_msgdef_fullname(const upb_msgdef *m); const upb_filedef *upb_msgdef_file(const upb_msgdef *m); @@ -441,9 +445,8 @@ void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter * iter); bool upb_msg_oneof_iter_isequal(const upb_msg_oneof_iter *iter1, const upb_msg_oneof_iter *iter2); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ /* Structure that describes a single .proto message type. */ class upb::MessageDefPtr { @@ -711,7 +714,9 @@ class upb::EnumDefPtr { /* upb_filedef ****************************************************************/ -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif const char *upb_filedef_name(const upb_filedef *f); const char *upb_filedef_package(const upb_filedef *f); @@ -725,9 +730,8 @@ const upb_filedef *upb_filedef_dep(const upb_filedef *f, int i); const upb_msgdef *upb_filedef_msg(const upb_filedef *f, int i); const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ /* Class that represents a .proto file with some things defined in it. * @@ -773,7 +777,9 @@ class upb::FileDefPtr { /* upb_symtab *****************************************************************/ -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif upb_symtab *upb_symtab_new(); void upb_symtab_free(upb_symtab* s); @@ -795,9 +801,8 @@ typedef struct upb_def_init { bool _upb_symtab_loaddefinit(upb_symtab *s, const upb_def_init *init); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ /* Non-const methods in upb::SymbolTable are NOT thread-safe. */ class upb::SymbolTable { @@ -823,7 +828,7 @@ class upb::SymbolTable { /* Adds the given serialized FileDescriptorProto to the pool. */ bool AddFile(const google_protobuf_FileDescriptorProto *file_proto, Status *status) { - return upb_symtab_addfile(ptr_.get(), file_proto, status); + return upb_symtab_addfile(ptr_.get(), file_proto, status->ptr()); } private: diff --git a/upb/encode.h b/upb/encode.h index 1a451b0..6842777 100644 --- a/upb/encode.h +++ b/upb/encode.h @@ -7,11 +7,15 @@ #include "upb/msg.h" -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif char *upb_encode(const void *msg, const upb_msglayout *l, upb_arena *arena, size_t *size); -UPB_END_EXTERN_C +#ifdef __cplusplus +} /* extern "C" */ +#endif #endif /* UPB_ENCODE_H_ */ diff --git a/upb/generated_util.h b/upb/generated_util.h index 3989f1e..657280f 100644 --- a/upb/generated_util.h +++ b/upb/generated_util.h @@ -54,10 +54,9 @@ UPB_INLINE void *_upb_array_resize_accessor(void *msg, size_t ofs, size_t size, size_t new_size = UPB_MAX(arr->size, 4); size_t old_bytes = arr->size * elem_size; size_t new_bytes; - upb_alloc *alloc = upb_arena_alloc(arr->arena); while (new_size < size) new_size *= 2; new_bytes = new_size * elem_size; - arr->data = upb_realloc(alloc, arr->data, old_bytes, new_bytes); + arr->data = upb_arena_realloc(arena, arr->data, old_bytes, new_bytes); if (!arr->data) { return NULL; } diff --git a/upb/handlers.c b/upb/handlers.c index ca978bf..aa23b46 100644 --- a/upb/handlers.c +++ b/upb/handlers.c @@ -390,7 +390,7 @@ uint32_t upb_handlers_selectorcount(const upb_fielddef *f) { /* upb_handlercache ***********************************************************/ struct upb_handlercache { - upb_arena arena; + upb_arena *arena; upb_inttable tab; /* maps upb_msgdef* -> upb_handlers*. */ upb_inttable cleanup_; upb_handlers_callback *callback; @@ -407,7 +407,7 @@ const upb_handlers *upb_handlercache_get(upb_handlercache *c, return upb_value_getptr(v); } - h = upb_handlers_new(md, c, &c->arena); + h = upb_handlers_new(md, c, c->arena); v = upb_value_ptr(h); if (!h) return NULL; @@ -442,7 +442,7 @@ upb_handlercache *upb_handlercache_new(upb_handlers_callback *callback, if (!cache) return NULL; - upb_arena_init(&cache->arena); + cache->arena = upb_arena_new(); cache->callback = callback; cache->closure = closure; @@ -470,7 +470,7 @@ void upb_handlercache_free(upb_handlercache *cache) { upb_inttable_uninit(&cache->tab); upb_inttable_uninit(&cache->cleanup_); - upb_arena_uninit(&cache->arena); + upb_arena_free(cache->arena); upb_gfree(cache); } diff --git a/upb/handlers.h b/upb/handlers.h index 44cad18..764e83e 100644 --- a/upb/handlers.h +++ b/upb/handlers.h @@ -157,7 +157,9 @@ typedef size_t upb_string_handlerfunc(void *c, const void *hd, const char *buf, struct upb_handlers; typedef struct upb_handlers upb_handlers; -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif /* Mutating accessors. */ const upb_status *upb_handlers_status(upb_handlers *h); @@ -235,9 +237,8 @@ UPB_INLINE upb_selector_t upb_handlers_getendselector(upb_selector_t start) { uint32_t upb_handlers_selectorbaseoffset(const upb_fielddef *f); uint32_t upb_handlers_selectorcount(const upb_fielddef *f); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ namespace upb { typedef upb_handlers Handlers; @@ -303,7 +304,9 @@ template class upb::Handler { const upb_handlerattr& attr() const { return attr_; } private: - UPB_DISALLOW_COPY_AND_ASSIGN(Handler) + Handler(const Handler&) = delete; + Handler& operator=(const Handler&) = delete; + FuncPtr handler_; mutable upb_handlerattr attr_; mutable bool registered_; @@ -334,7 +337,8 @@ class upb::HandlersPtr { typedef Handler StartFieldHandler; typedef Handler EndFieldHandler; typedef Handler StartMessageHandler; - typedef Handler EndMessageHandler; + typedef Handler + EndMessageHandler; typedef Handler StartStringHandler; typedef Handler @@ -590,7 +594,9 @@ class upb::HandlersPtr { /* upb_handlercache ***********************************************************/ -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif struct upb_handlercache; typedef struct upb_handlercache upb_handlercache; @@ -605,9 +611,8 @@ const upb_handlers *upb_handlercache_get(upb_handlercache *cache, bool upb_handlercache_addcleanup(upb_handlercache *h, void *p, upb_handlerfree *hfree); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ class upb::HandlerCache { public: @@ -631,8 +636,6 @@ class upb::HandlerCache { /* upb_byteshandler ***********************************************************/ -UPB_BEGIN_EXTERN_C - typedef struct { upb_func *func; @@ -665,6 +668,10 @@ UPB_INLINE void upb_byteshandler_init(upb_byteshandler *handler) { *handler = init; } +#ifdef __cplusplus +extern "C" { +#endif + /* Caller must ensure that "d" outlives the handlers. */ bool upb_byteshandler_setstartstr(upb_byteshandler *h, upb_startstr_handlerfunc *func, void *d); @@ -674,16 +681,18 @@ bool upb_byteshandler_setendstr(upb_byteshandler *h, upb_endfield_handlerfunc *func, void *d); #ifdef __cplusplus +} /* extern "C" */ + namespace upb { typedef upb_byteshandler BytesHandler; } #endif -UPB_END_EXTERN_C - /** Message handlers ******************************************************************/ -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif /* These are the handlers used internally by upb_msgfactory_getmergehandlers(). * They write scalar data to a known offset from the message pointer. @@ -710,7 +719,9 @@ bool upb_msg_getscalarhandlerdata(const upb_handlers *h, -UPB_END_EXTERN_C +#ifdef __cplusplus +} /* extern "C" */ +#endif #include "upb/handlers-inl.h" diff --git a/upb/json/parser.c b/upb/json/parser.c index 1dac800..a594bfd 100644 --- a/upb/json/parser.c +++ b/upb/json/parser.c @@ -212,7 +212,7 @@ typedef struct { } upb_jsonparser_frame; struct upb_json_parser { - upb_env *env; + upb_arena *arena; const upb_json_parsermethod *method; upb_bytessink input_; @@ -221,7 +221,7 @@ struct upb_json_parser { upb_jsonparser_frame *top; upb_jsonparser_frame *limit; - upb_status status; + upb_status *status; /* Ragel's internal parsing stack for the parsing state machine. */ int current_state; @@ -259,7 +259,7 @@ struct upb_json_parser { }; struct upb_json_codecache { - upb_arena arena; + upb_arena *arena; upb_inttable methods; /* upb_msgdef* -> upb_json_parsermethod* */ }; @@ -277,7 +277,7 @@ static upb_jsonparser_any_frame *json_parser_any_frame_new( upb_json_parser *p) { upb_jsonparser_any_frame *frame; - frame = upb_env_malloc(p->env, sizeof(upb_jsonparser_any_frame)); + frame = upb_arena_malloc(p->arena, sizeof(upb_jsonparser_any_frame)); frame->encoder_handlercache = upb_pb_encoder_newcache(); frame->parser_codecache = upb_json_codecache_new(); @@ -301,12 +301,12 @@ static void json_parser_any_frame_set_payload_type( /* Initialize encoder. */ h = upb_handlercache_get(frame->encoder_handlercache, payload_type); - encoder = upb_pb_encoder_create(p->env, h, frame->stringsink.sink); + encoder = upb_pb_encoder_create(p->arena, h, frame->stringsink.sink); /* Initialize parser. */ parser_method = upb_json_codecache_get(frame->parser_codecache, payload_type); upb_sink_reset(&frame->sink, h, encoder); - frame->parser = upb_json_parser_create(p->env, parser_method, p->symtab, + frame->parser = upb_json_parser_create(p->arena, parser_method, p->symtab, frame->sink, p->ignore_json_unknown); } @@ -372,8 +372,7 @@ static upb_selector_t parser_getsel(upb_json_parser *p) { static bool check_stack(upb_json_parser *p) { if ((p->top + 1) == p->limit) { - upb_status_seterrmsg(&p->status, "Nesting too deep"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Nesting too deep"); return false; } @@ -468,10 +467,9 @@ static bool base64_push(upb_json_parser *p, upb_selector_t sel, const char *ptr, char output[3]; if (limit - ptr < 4) { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Base64 input for bytes field not a multiple of 4: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } @@ -495,10 +493,9 @@ static bool base64_push(upb_json_parser *p, upb_selector_t sel, const char *ptr, otherchar: if (nonbase64(ptr[0]) || nonbase64(ptr[1]) || nonbase64(ptr[2]) || nonbase64(ptr[3]) ) { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Non-base64 characters in bytes field: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } if (ptr[2] == '=') { uint32_t val; @@ -536,11 +533,10 @@ otherchar: } badpadding: - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Incorrect base64 padding for field: %s (%.*s)", upb_fielddef_name(p->top->f), 4, ptr); - upb_env_reporterror(p->env, &p->status); return false; } @@ -584,10 +580,9 @@ static bool accumulate_realloc(upb_json_parser *p, size_t need) { new_size = saturating_multiply(new_size, 2); } - mem = upb_env_realloc(p->env, p->accumulate_buf, old_size, new_size); + mem = upb_arena_realloc(p->arena, p->accumulate_buf, old_size, new_size); if (!mem) { - upb_status_seterrmsg(&p->status, "Out of memory allocating buffer."); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Out of memory allocating buffer."); return false; } @@ -610,8 +605,7 @@ static bool accumulate_append(upb_json_parser *p, const char *buf, size_t len, } if (!checked_add(p->accumulated_len, len, &need)) { - upb_status_seterrmsg(&p->status, "Integer overflow."); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Integer overflow."); return false; } @@ -689,8 +683,7 @@ static bool multipart_text(upb_json_parser *p, const char *buf, size_t len, switch (p->multipart_state) { case MULTIPART_INACTIVE: upb_status_seterrmsg( - &p->status, "Internal error: unexpected state MULTIPART_INACTIVE"); - upb_env_reporterror(p->env, &p->status); + p->status, "Internal error: unexpected state MULTIPART_INACTIVE"); return false; case MULTIPART_ACCUMULATE: @@ -1055,8 +1048,7 @@ static bool parse_number(upb_json_parser *p, bool is_quoted) { multipart_end(p); return true; } else { - upb_status_seterrf(&p->status, "error parsing number: %s", buf); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrf(p->status, "error parsing number: %s", buf); multipart_end(p); return false; } @@ -1070,10 +1062,9 @@ static bool parser_putbool(upb_json_parser *p, bool val) { } if (upb_fielddef_type(p->top->f) != UPB_TYPE_BOOL) { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Boolean value specified for non-bool field: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1248,10 +1239,9 @@ static bool start_stringval(upb_json_parser *p) { multipart_startaccum(p); return true; } else { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "String specified for bool or submessage field: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } } @@ -1284,8 +1274,7 @@ static bool end_any_stringval(upb_json_parser *p) { payload_type = upb_symtab_lookupmsg2(p->symtab, buf, len); if (payload_type == NULL) { upb_status_seterrf( - &p->status, "Cannot find packed type: %.*s\n", (int)len, buf); - upb_env_reporterror(p->env, &p->status); + p->status, "Cannot find packed type: %.*s\n", (int)len, buf); return false; } @@ -1294,8 +1283,7 @@ static bool end_any_stringval(upb_json_parser *p) { return true; } else { upb_status_seterrf( - &p->status, "Invalid type url: %.*s\n", (int)len, buf); - upb_env_reporterror(p->env, &p->status); + p->status, "Invalid type url: %.*s\n", (int)len, buf); return false; } } @@ -1347,8 +1335,7 @@ static bool end_stringval_nontop(upb_json_parser *p) { upb_selector_t sel = parser_getsel(p); upb_sink_putint32(&p->top->sink, sel, int_val); } else { - upb_status_seterrf(&p->status, "Enum value unknown: '%.*s'", len, buf); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrf(p->status, "Enum value unknown: '%.*s'", len, buf); } break; @@ -1365,8 +1352,7 @@ static bool end_stringval_nontop(upb_json_parser *p) { default: UPB_ASSERT(false); - upb_status_seterrmsg(&p->status, "Internal error in JSON decoder"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Internal error in JSON decoder"); ok = false; break; } @@ -1445,25 +1431,22 @@ static bool end_duration_base(upb_json_parser *p, const char *ptr) { memcpy(seconds_buf, buf, fraction_start); seconds = strtol(seconds_buf, &end, 10); if (errno == ERANGE || end != seconds_buf + fraction_start) { - upb_status_seterrf(&p->status, "error parsing duration: %s", + upb_status_seterrf(p->status, "error parsing duration: %s", seconds_buf); - upb_env_reporterror(p->env, &p->status); return false; } if (seconds > 315576000000) { - upb_status_seterrf(&p->status, "error parsing duration: " + upb_status_seterrf(p->status, "error parsing duration: " "maximum acceptable value is " "315576000000"); - upb_env_reporterror(p->env, &p->status); return false; } if (seconds < -315576000000) { - upb_status_seterrf(&p->status, "error parsing duration: " + upb_status_seterrf(p->status, "error parsing duration: " "minimum acceptable value is " "-315576000000"); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1472,9 +1455,8 @@ static bool end_duration_base(upb_json_parser *p, const char *ptr) { memcpy(nanos_buf + 1, buf + fraction_start, len - fraction_start); val = strtod(nanos_buf, &end); if (errno == ERANGE || end != nanos_buf + len - fraction_start + 1) { - upb_status_seterrf(&p->status, "error parsing duration: %s", + upb_status_seterrf(p->status, "error parsing duration: %s", nanos_buf); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1500,7 +1482,7 @@ static bool end_duration_base(upb_json_parser *p, const char *ptr) { upb_sink_putint32(&p->top->sink, parser_getsel(p), nanos); end_member(p); - /* Continue previous environment */ + /* Continue previous arena */ multipart_startaccum(p); return true; @@ -1530,8 +1512,7 @@ static bool end_timestamp_base(upb_json_parser *p, const char *ptr) { /* Parse seconds */ if (strptime(timestamp_buf, "%FT%H:%M:%S%Z", &p->tm) == NULL) { - upb_status_seterrf(&p->status, "error parsing timestamp: %s", buf); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrf(p->status, "error parsing timestamp: %s", buf); return false; } @@ -1564,9 +1545,8 @@ static bool end_timestamp_fraction(upb_json_parser *p, const char *ptr) { buf = accumulate_getptr(p, &len); if (len > 10) { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "error parsing timestamp: at most 9-digit fraction."); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1576,9 +1556,8 @@ static bool end_timestamp_fraction(upb_json_parser *p, const char *ptr) { val = strtod(nanos_buf, &end); if (errno == ERANGE || end != nanos_buf + len + 1) { - upb_status_seterrf(&p->status, "error parsing timestamp nanos: %s", + upb_status_seterrf(p->status, "error parsing timestamp nanos: %s", nanos_buf); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1620,8 +1599,7 @@ static bool end_timestamp_zone(upb_json_parser *p, const char *ptr) { if (buf[0] != 'Z') { if (sscanf(buf + 1, "%2d:00", &hours) != 1) { - upb_status_seterrf(&p->status, "error parsing timestamp offset"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrf(p->status, "error parsing timestamp offset"); return false; } @@ -1637,10 +1615,9 @@ static bool end_timestamp_zone(upb_json_parser *p, const char *ptr) { /* Check timestamp boundary */ if (seconds < -62135596800) { - upb_status_seterrf(&p->status, "error parsing timestamp: " + upb_status_seterrf(p->status, "error parsing timestamp: " "minimum acceptable value is " "0001-01-01T00:00:00Z"); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1681,8 +1658,7 @@ static bool parse_mapentry_key(upb_json_parser *p) { p->top->f = upb_msgdef_itof(p->top->m, UPB_MAPENTRY_KEY); if (p->top->f == NULL) { - upb_status_seterrmsg(&p->status, "mapentry message has no key"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "mapentry message has no key"); return false; } switch (upb_fielddef_type(p->top->f)) { @@ -1705,9 +1681,8 @@ static bool parse_mapentry_key(upb_json_parser *p) { return false; } } else { - upb_status_seterrmsg(&p->status, + upb_status_seterrmsg(p->status, "Map bool key not 'true' or 'false'"); - upb_env_reporterror(p->env, &p->status); return false; } multipart_end(p); @@ -1725,8 +1700,7 @@ static bool parse_mapentry_key(upb_json_parser *p) { break; } default: - upb_status_seterrmsg(&p->status, "Invalid field type for map key"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Invalid field type for map key"); return false; } @@ -1785,8 +1759,7 @@ static bool handle_mapentry(upb_json_parser *p) { p->top->is_mapentry = true; /* set up to pop frame after value is parsed. */ p->top->mapfield = mapfield; if (p->top->f == NULL) { - upb_status_seterrmsg(&p->status, "mapentry message has no value"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "mapentry message has no value"); return false; } @@ -1821,8 +1794,7 @@ static bool end_membername(upb_json_parser *p) { multipart_end(p); return true; } else { - upb_status_seterrf(&p->status, "No such field: %.*s\n", (int)len, buf); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrf(p->status, "No such field: %.*s\n", (int)len, buf); return false; } } @@ -1848,14 +1820,13 @@ static bool end_any_membername(upb_json_parser *p) { static void end_member(upb_json_parser *p) { /* If we just parsed a map-entry value, end that frame too. */ if (p->top->is_mapentry) { - upb_status s = UPB_STATUS_INIT; upb_selector_t sel; bool ok; const upb_fielddef *mapfield; UPB_ASSERT(p->top > p->stack); /* send ENDMSG on submsg. */ - upb_sink_endmsg(&p->top->sink, &s); + upb_sink_endmsg(&p->top->sink, p->status); mapfield = p->top->mapfield; /* send ENDSUBMSG in repeated-field-of-mapentries frame. */ @@ -1949,10 +1920,9 @@ static bool start_subobject(upb_json_parser *p) { return true; } else { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Object specified for non-message/group field: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } } @@ -2060,10 +2030,9 @@ static bool start_array(upb_json_parser *p) { } if (!upb_fielddef_isseq(p->top->f)) { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Array specified for non-repeated field: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } @@ -2122,12 +2091,7 @@ static void start_object(upb_json_parser *p) { static void end_object(upb_json_parser *p) { if (!p->top->is_map && p->top->m != NULL) { - upb_status status; - upb_status_clear(&status); - upb_sink_endmsg(&p->top->sink, &status); - if (!upb_ok(&status)) { - upb_env_reporterror(p->env, &status); - } + upb_sink_endmsg(&p->top->sink, p->status); } } @@ -2146,8 +2110,7 @@ static bool end_any_object(upb_json_parser *p, const char *ptr) { if (json_parser_any_frame_has_value(p->top->any_frame) && !json_parser_any_frame_has_type_url(p->top->any_frame)) { - upb_status_seterrmsg(&p->status, "No valid type url"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "No valid type url"); return false; } @@ -2162,8 +2125,7 @@ static bool end_any_object(upb_json_parser *p, const char *ptr) { p->top->any_frame->before_type_url_end - p->top->any_frame->before_type_url_start); if (p->top->any_frame->before_type_url_start == NULL) { - upb_status_seterrmsg(&p->status, "invalid data for well known type."); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "invalid data for well known type."); return false; } p->top->any_frame->before_type_url_start++; @@ -2175,8 +2137,7 @@ static bool end_any_object(upb_json_parser *p, const char *ptr) { (ptr + 1) - p->top->any_frame->after_type_url_start); if (p->top->any_frame->after_type_url_start == NULL) { - upb_status_seterrmsg(&p->status, "Invalid data for well known type."); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Invalid data for well known type."); return false; } p->top->any_frame->after_type_url_start++; @@ -2249,7 +2210,6 @@ static bool end_any_object(upb_json_parser *p, const char *ptr) { /* Deallocate any parse frame. */ json_parser_any_frame_free(p->top->any_frame); - upb_env_free(p->env, p->top->any_frame); return true; } @@ -2419,11 +2379,11 @@ static bool is_string_wrapper_object(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 2581 "upb/json/parser.rl" +#line 2541 "upb/json/parser.rl" -#line 2427 "upb/json/parser.c" +#line 2387 "upb/json/parser.c" static const char _json_actions[] = { 0, 1, 0, 1, 1, 1, 3, 1, 4, 1, 6, 1, 7, 1, 8, 1, @@ -2670,7 +2630,7 @@ static const int json_en_value_machine = 75; static const int json_en_main = 1; -#line 2584 "upb/json/parser.rl" +#line 2544 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -2693,7 +2653,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 2697 "upb/json/parser.c" +#line 2657 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -2768,83 +2728,83 @@ _match: switch ( *_acts++ ) { case 1: -#line 2432 "upb/json/parser.rl" +#line 2392 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 2434 "upb/json/parser.rl" +#line 2394 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 23;goto _again;} } break; case 3: -#line 2438 "upb/json/parser.rl" +#line 2398 "upb/json/parser.rl" { start_text(parser, p); } break; case 4: -#line 2439 "upb/json/parser.rl" +#line 2399 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -#line 2445 "upb/json/parser.rl" +#line 2405 "upb/json/parser.rl" { start_hex(parser); } break; case 6: -#line 2446 "upb/json/parser.rl" +#line 2406 "upb/json/parser.rl" { hexdigit(parser, p); } break; case 7: -#line 2447 "upb/json/parser.rl" +#line 2407 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -#line 2453 "upb/json/parser.rl" +#line 2413 "upb/json/parser.rl" { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -#line 2459 "upb/json/parser.rl" +#line 2419 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 10: -#line 2471 "upb/json/parser.rl" +#line 2431 "upb/json/parser.rl" { start_duration_base(parser, p); } break; case 11: -#line 2472 "upb/json/parser.rl" +#line 2432 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_duration_base(parser, p)); } break; case 12: -#line 2474 "upb/json/parser.rl" +#line 2434 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 13: -#line 2479 "upb/json/parser.rl" +#line 2439 "upb/json/parser.rl" { start_timestamp_base(parser, p); } break; case 14: -#line 2480 "upb/json/parser.rl" +#line 2440 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_base(parser, p)); } break; case 15: -#line 2482 "upb/json/parser.rl" +#line 2442 "upb/json/parser.rl" { start_timestamp_fraction(parser, p); } break; case 16: -#line 2483 "upb/json/parser.rl" +#line 2443 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } break; case 17: -#line 2485 "upb/json/parser.rl" +#line 2445 "upb/json/parser.rl" { start_timestamp_zone(parser, p); } break; case 18: -#line 2486 "upb/json/parser.rl" +#line 2446 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } break; case 19: -#line 2488 "upb/json/parser.rl" +#line 2448 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 20: -#line 2493 "upb/json/parser.rl" +#line 2453 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) { {stack[top++] = cs; cs = 47;goto _again;} @@ -2856,11 +2816,11 @@ _match: } break; case 21: -#line 2504 "upb/json/parser.rl" +#line 2464 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 75;goto _again;} } break; case 22: -#line 2509 "upb/json/parser.rl" +#line 2469 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_member(parser, p); @@ -2870,11 +2830,11 @@ _match: } break; case 23: -#line 2516 "upb/json/parser.rl" +#line 2476 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_membername(parser)); } break; case 24: -#line 2519 "upb/json/parser.rl" +#line 2479 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { end_any_member(parser, p); @@ -2884,7 +2844,7 @@ _match: } break; case 25: -#line 2530 "upb/json/parser.rl" +#line 2490 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_object(parser, p); @@ -2894,7 +2854,7 @@ _match: } break; case 26: -#line 2539 "upb/json/parser.rl" +#line 2499 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { CHECK_RETURN_TOP(end_any_object(parser, p)); @@ -2904,54 +2864,54 @@ _match: } break; case 27: -#line 2551 "upb/json/parser.rl" +#line 2511 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_array(parser)); } break; case 28: -#line 2555 "upb/json/parser.rl" +#line 2515 "upb/json/parser.rl" { end_array(parser); } break; case 29: -#line 2560 "upb/json/parser.rl" +#line 2520 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_number(parser, p)); } break; case 30: -#line 2561 "upb/json/parser.rl" +#line 2521 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 31: -#line 2563 "upb/json/parser.rl" +#line 2523 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_stringval(parser)); } break; case 32: -#line 2564 "upb/json/parser.rl" +#line 2524 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_stringval(parser)); } break; case 33: -#line 2566 "upb/json/parser.rl" +#line 2526 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 34: -#line 2568 "upb/json/parser.rl" +#line 2528 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 35: -#line 2570 "upb/json/parser.rl" +#line 2530 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 36: -#line 2572 "upb/json/parser.rl" +#line 2532 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_subobject_full(parser)); } break; case 37: -#line 2573 "upb/json/parser.rl" +#line 2533 "upb/json/parser.rl" { end_subobject_full(parser); } break; case 38: -#line 2578 "upb/json/parser.rl" +#line 2538 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 2955 "upb/json/parser.c" +#line 2915 "upb/json/parser.c" } } @@ -2968,32 +2928,32 @@ _again: while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 0: -#line 2430 "upb/json/parser.rl" +#line 2390 "upb/json/parser.rl" { p--; {cs = stack[--top]; if ( p == pe ) goto _test_eof; goto _again;} } break; case 30: -#line 2561 "upb/json/parser.rl" +#line 2521 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 33: -#line 2566 "upb/json/parser.rl" +#line 2526 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 34: -#line 2568 "upb/json/parser.rl" +#line 2528 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 35: -#line 2570 "upb/json/parser.rl" +#line 2530 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 37: -#line 2573 "upb/json/parser.rl" +#line 2533 "upb/json/parser.rl" { end_subobject_full(parser); } break; -#line 2997 "upb/json/parser.c" +#line 2957 "upb/json/parser.c" } } } @@ -3001,11 +2961,10 @@ goto _again;} } _out: {} } -#line 2606 "upb/json/parser.rl" +#line 2566 "upb/json/parser.rl" if (p != pe) { - upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p); - upb_env_reporterror(parser->env, &parser->status); + upb_status_seterrf(parser->status, "Parse error at '%.*s'\n", pe - p, p); } else { capture_suspend(parser, &p); } @@ -3049,26 +3008,25 @@ static void json_parser_reset(upb_json_parser *p) { /* Emit Ragel initialization of the parser. */ -#line 3053 "upb/json/parser.c" +#line 3012 "upb/json/parser.c" { cs = json_start; top = 0; } -#line 2653 "upb/json/parser.rl" +#line 2612 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); p->multipart_state = MULTIPART_INACTIVE; p->capture = NULL; p->accumulated = NULL; - upb_status_clear(&p->status); } static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c, const upb_msgdef *md) { upb_msg_field_iter i; - upb_alloc *alloc = upb_arena_alloc(&c->arena); + upb_alloc *alloc = upb_arena_alloc(c->arena); upb_json_parsermethod *m = upb_malloc(alloc, sizeof(*m)); @@ -3109,19 +3067,20 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c, /* Public API *****************************************************************/ -upb_json_parser *upb_json_parser_create(upb_env *env, +upb_json_parser *upb_json_parser_create(upb_arena *arena, const upb_json_parsermethod *method, const upb_symtab* symtab, upb_sink output, bool ignore_json_unknown) { #ifndef NDEBUG - const size_t size_before = upb_env_bytesallocated(env); + const size_t size_before = upb_arena_bytesallocated(arena); #endif - upb_json_parser *p = upb_env_malloc(env, sizeof(upb_json_parser)); + upb_json_parser *p = upb_arena_malloc(arena, sizeof(upb_json_parser)); if (!p) return false; - p->env = env; + p->arena = arena; p->method = method; + p->status = NULL; p->limit = p->stack + UPB_JSON_MAX_DEPTH; p->accumulate_buf = NULL; p->accumulate_buf_size = 0; @@ -3143,8 +3102,8 @@ upb_json_parser *upb_json_parser_create(upb_env *env, p->ignore_json_unknown = ignore_json_unknown; /* If this fails, uncomment and increase the value in parser.h. */ - /* fprintf(stderr, "%zd\n", upb_env_bytesallocated(env) - size_before); */ - UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(env) - size_before <= + /* fprintf(stderr, "%zd\n", upb_arena_bytesallocated(arena) - size_before); */ + UPB_ASSERT_DEBUGVAR(upb_arena_bytesallocated(arena) - size_before <= UPB_JSON_PARSER_SIZE); return p; } @@ -3164,8 +3123,8 @@ upb_json_codecache *upb_json_codecache_new() { c = upb_gmalloc(sizeof(*c)); - upb_arena_init(&c->arena); - alloc = upb_arena_alloc(&c->arena); + c->arena = upb_arena_new(); + alloc = upb_arena_alloc(c->arena); upb_inttable_init2(&c->methods, UPB_CTYPE_CONSTPTR, alloc); @@ -3173,7 +3132,7 @@ upb_json_codecache *upb_json_codecache_new() { } void upb_json_codecache_free(upb_json_codecache *c) { - upb_arena_uninit(&c->arena); + upb_arena_free(c->arena); upb_gfree(c); } @@ -3182,7 +3141,7 @@ const upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c, upb_json_parsermethod *m; upb_value v; upb_msg_field_iter i; - upb_alloc *alloc = upb_arena_alloc(&c->arena); + upb_alloc *alloc = upb_arena_alloc(c->arena); if (upb_inttable_lookupptr(&c->methods, md, &v)) { return upb_value_getconstptr(v); diff --git a/upb/json/parser.h b/upb/json/parser.h index d1a1471..2a06fcf 100644 --- a/upb/json/parser.h +++ b/upb/json/parser.h @@ -25,14 +25,15 @@ class ParserMethodPtr; struct upb_json_parsermethod; typedef struct upb_json_parsermethod upb_json_parsermethod; -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif const upb_byteshandler* upb_json_parsermethod_inputhandler( const upb_json_parsermethod* m); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ class upb::json::ParserMethodPtr { public: @@ -62,17 +63,19 @@ class upb::json::ParserMethodPtr { struct upb_json_parser; typedef struct upb_json_parser upb_json_parser; -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif -upb_json_parser* -upb_json_parser_create(upb_env* e, const upb_json_parsermethod* m, - const upb_symtab* symtab, upb_sink output, - bool ignore_json_unknown); +upb_json_parser* upb_json_parser_create(upb_arena* a, + const upb_json_parsermethod* m, + const upb_symtab* symtab, + upb_sink output, + bool ignore_json_unknown); upb_bytessink upb_json_parser_input(upb_json_parser* p); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ /* Parses an incoming BytesStream, pushing the results to the destination * sink. */ @@ -80,12 +83,13 @@ class upb::json::ParserPtr { public: ParserPtr(upb_json_parser* ptr) : ptr_(ptr) {} - static ParserPtr Create(Environment* env, ParserMethodPtr method, + static ParserPtr Create(Arena* arena, ParserMethodPtr method, SymbolTable* symtab, Sink output, bool ignore_json_unknown) { upb_symtab* symtab_ptr = symtab ? symtab->ptr() : nullptr; - return ParserPtr(upb_json_parser_create( - env, method.ptr(), symtab_ptr, output.sink(), ignore_json_unknown)); + return ParserPtr(upb_json_parser_create(arena->ptr(), method.ptr(), + symtab_ptr, output.sink(), + ignore_json_unknown)); } BytesSink input() { return upb_json_parser_input(ptr_); } @@ -101,16 +105,17 @@ class upb::json::ParserPtr { struct upb_json_codecache; typedef struct upb_json_codecache upb_json_codecache; -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif upb_json_codecache *upb_json_codecache_new(); void upb_json_codecache_free(upb_json_codecache *cache); const upb_json_parsermethod* upb_json_codecache_get(upb_json_codecache* cache, const upb_msgdef* md); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ class upb::json::CodeCache { public: diff --git a/upb/json/parser.rl b/upb/json/parser.rl index 05a9505..a117d0c 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -210,7 +210,7 @@ typedef struct { } upb_jsonparser_frame; struct upb_json_parser { - upb_env *env; + upb_arena *arena; const upb_json_parsermethod *method; upb_bytessink input_; @@ -219,7 +219,7 @@ struct upb_json_parser { upb_jsonparser_frame *top; upb_jsonparser_frame *limit; - upb_status status; + upb_status *status; /* Ragel's internal parsing stack for the parsing state machine. */ int current_state; @@ -257,7 +257,7 @@ struct upb_json_parser { }; struct upb_json_codecache { - upb_arena arena; + upb_arena *arena; upb_inttable methods; /* upb_msgdef* -> upb_json_parsermethod* */ }; @@ -275,7 +275,7 @@ static upb_jsonparser_any_frame *json_parser_any_frame_new( upb_json_parser *p) { upb_jsonparser_any_frame *frame; - frame = upb_env_malloc(p->env, sizeof(upb_jsonparser_any_frame)); + frame = upb_arena_malloc(p->arena, sizeof(upb_jsonparser_any_frame)); frame->encoder_handlercache = upb_pb_encoder_newcache(); frame->parser_codecache = upb_json_codecache_new(); @@ -299,12 +299,12 @@ static void json_parser_any_frame_set_payload_type( /* Initialize encoder. */ h = upb_handlercache_get(frame->encoder_handlercache, payload_type); - encoder = upb_pb_encoder_create(p->env, h, frame->stringsink.sink); + encoder = upb_pb_encoder_create(p->arena, h, frame->stringsink.sink); /* Initialize parser. */ parser_method = upb_json_codecache_get(frame->parser_codecache, payload_type); upb_sink_reset(&frame->sink, h, encoder); - frame->parser = upb_json_parser_create(p->env, parser_method, p->symtab, + frame->parser = upb_json_parser_create(p->arena, parser_method, p->symtab, frame->sink, p->ignore_json_unknown); } @@ -370,8 +370,7 @@ static upb_selector_t parser_getsel(upb_json_parser *p) { static bool check_stack(upb_json_parser *p) { if ((p->top + 1) == p->limit) { - upb_status_seterrmsg(&p->status, "Nesting too deep"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Nesting too deep"); return false; } @@ -466,10 +465,9 @@ static bool base64_push(upb_json_parser *p, upb_selector_t sel, const char *ptr, char output[3]; if (limit - ptr < 4) { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Base64 input for bytes field not a multiple of 4: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } @@ -493,10 +491,9 @@ static bool base64_push(upb_json_parser *p, upb_selector_t sel, const char *ptr, otherchar: if (nonbase64(ptr[0]) || nonbase64(ptr[1]) || nonbase64(ptr[2]) || nonbase64(ptr[3]) ) { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Non-base64 characters in bytes field: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } if (ptr[2] == '=') { uint32_t val; @@ -534,11 +531,10 @@ otherchar: } badpadding: - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Incorrect base64 padding for field: %s (%.*s)", upb_fielddef_name(p->top->f), 4, ptr); - upb_env_reporterror(p->env, &p->status); return false; } @@ -582,10 +578,9 @@ static bool accumulate_realloc(upb_json_parser *p, size_t need) { new_size = saturating_multiply(new_size, 2); } - mem = upb_env_realloc(p->env, p->accumulate_buf, old_size, new_size); + mem = upb_arena_realloc(p->arena, p->accumulate_buf, old_size, new_size); if (!mem) { - upb_status_seterrmsg(&p->status, "Out of memory allocating buffer."); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Out of memory allocating buffer."); return false; } @@ -608,8 +603,7 @@ static bool accumulate_append(upb_json_parser *p, const char *buf, size_t len, } if (!checked_add(p->accumulated_len, len, &need)) { - upb_status_seterrmsg(&p->status, "Integer overflow."); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Integer overflow."); return false; } @@ -687,8 +681,7 @@ static bool multipart_text(upb_json_parser *p, const char *buf, size_t len, switch (p->multipart_state) { case MULTIPART_INACTIVE: upb_status_seterrmsg( - &p->status, "Internal error: unexpected state MULTIPART_INACTIVE"); - upb_env_reporterror(p->env, &p->status); + p->status, "Internal error: unexpected state MULTIPART_INACTIVE"); return false; case MULTIPART_ACCUMULATE: @@ -1053,8 +1046,7 @@ static bool parse_number(upb_json_parser *p, bool is_quoted) { multipart_end(p); return true; } else { - upb_status_seterrf(&p->status, "error parsing number: %s", buf); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrf(p->status, "error parsing number: %s", buf); multipart_end(p); return false; } @@ -1068,10 +1060,9 @@ static bool parser_putbool(upb_json_parser *p, bool val) { } if (upb_fielddef_type(p->top->f) != UPB_TYPE_BOOL) { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Boolean value specified for non-bool field: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1246,10 +1237,9 @@ static bool start_stringval(upb_json_parser *p) { multipart_startaccum(p); return true; } else { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "String specified for bool or submessage field: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } } @@ -1282,8 +1272,7 @@ static bool end_any_stringval(upb_json_parser *p) { payload_type = upb_symtab_lookupmsg2(p->symtab, buf, len); if (payload_type == NULL) { upb_status_seterrf( - &p->status, "Cannot find packed type: %.*s\n", (int)len, buf); - upb_env_reporterror(p->env, &p->status); + p->status, "Cannot find packed type: %.*s\n", (int)len, buf); return false; } @@ -1292,8 +1281,7 @@ static bool end_any_stringval(upb_json_parser *p) { return true; } else { upb_status_seterrf( - &p->status, "Invalid type url: %.*s\n", (int)len, buf); - upb_env_reporterror(p->env, &p->status); + p->status, "Invalid type url: %.*s\n", (int)len, buf); return false; } } @@ -1345,8 +1333,7 @@ static bool end_stringval_nontop(upb_json_parser *p) { upb_selector_t sel = parser_getsel(p); upb_sink_putint32(&p->top->sink, sel, int_val); } else { - upb_status_seterrf(&p->status, "Enum value unknown: '%.*s'", len, buf); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrf(p->status, "Enum value unknown: '%.*s'", len, buf); } break; @@ -1363,8 +1350,7 @@ static bool end_stringval_nontop(upb_json_parser *p) { default: UPB_ASSERT(false); - upb_status_seterrmsg(&p->status, "Internal error in JSON decoder"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Internal error in JSON decoder"); ok = false; break; } @@ -1443,25 +1429,22 @@ static bool end_duration_base(upb_json_parser *p, const char *ptr) { memcpy(seconds_buf, buf, fraction_start); seconds = strtol(seconds_buf, &end, 10); if (errno == ERANGE || end != seconds_buf + fraction_start) { - upb_status_seterrf(&p->status, "error parsing duration: %s", + upb_status_seterrf(p->status, "error parsing duration: %s", seconds_buf); - upb_env_reporterror(p->env, &p->status); return false; } if (seconds > 315576000000) { - upb_status_seterrf(&p->status, "error parsing duration: " + upb_status_seterrf(p->status, "error parsing duration: " "maximum acceptable value is " "315576000000"); - upb_env_reporterror(p->env, &p->status); return false; } if (seconds < -315576000000) { - upb_status_seterrf(&p->status, "error parsing duration: " + upb_status_seterrf(p->status, "error parsing duration: " "minimum acceptable value is " "-315576000000"); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1470,9 +1453,8 @@ static bool end_duration_base(upb_json_parser *p, const char *ptr) { memcpy(nanos_buf + 1, buf + fraction_start, len - fraction_start); val = strtod(nanos_buf, &end); if (errno == ERANGE || end != nanos_buf + len - fraction_start + 1) { - upb_status_seterrf(&p->status, "error parsing duration: %s", + upb_status_seterrf(p->status, "error parsing duration: %s", nanos_buf); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1498,7 +1480,7 @@ static bool end_duration_base(upb_json_parser *p, const char *ptr) { upb_sink_putint32(&p->top->sink, parser_getsel(p), nanos); end_member(p); - /* Continue previous environment */ + /* Continue previous arena */ multipart_startaccum(p); return true; @@ -1528,8 +1510,7 @@ static bool end_timestamp_base(upb_json_parser *p, const char *ptr) { /* Parse seconds */ if (strptime(timestamp_buf, "%FT%H:%M:%S%Z", &p->tm) == NULL) { - upb_status_seterrf(&p->status, "error parsing timestamp: %s", buf); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrf(p->status, "error parsing timestamp: %s", buf); return false; } @@ -1562,9 +1543,8 @@ static bool end_timestamp_fraction(upb_json_parser *p, const char *ptr) { buf = accumulate_getptr(p, &len); if (len > 10) { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "error parsing timestamp: at most 9-digit fraction."); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1574,9 +1554,8 @@ static bool end_timestamp_fraction(upb_json_parser *p, const char *ptr) { val = strtod(nanos_buf, &end); if (errno == ERANGE || end != nanos_buf + len + 1) { - upb_status_seterrf(&p->status, "error parsing timestamp nanos: %s", + upb_status_seterrf(p->status, "error parsing timestamp nanos: %s", nanos_buf); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1618,8 +1597,7 @@ static bool end_timestamp_zone(upb_json_parser *p, const char *ptr) { if (buf[0] != 'Z') { if (sscanf(buf + 1, "%2d:00", &hours) != 1) { - upb_status_seterrf(&p->status, "error parsing timestamp offset"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrf(p->status, "error parsing timestamp offset"); return false; } @@ -1635,10 +1613,9 @@ static bool end_timestamp_zone(upb_json_parser *p, const char *ptr) { /* Check timestamp boundary */ if (seconds < -62135596800) { - upb_status_seterrf(&p->status, "error parsing timestamp: " + upb_status_seterrf(p->status, "error parsing timestamp: " "minimum acceptable value is " "0001-01-01T00:00:00Z"); - upb_env_reporterror(p->env, &p->status); return false; } @@ -1679,8 +1656,7 @@ static bool parse_mapentry_key(upb_json_parser *p) { p->top->f = upb_msgdef_itof(p->top->m, UPB_MAPENTRY_KEY); if (p->top->f == NULL) { - upb_status_seterrmsg(&p->status, "mapentry message has no key"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "mapentry message has no key"); return false; } switch (upb_fielddef_type(p->top->f)) { @@ -1703,9 +1679,8 @@ static bool parse_mapentry_key(upb_json_parser *p) { return false; } } else { - upb_status_seterrmsg(&p->status, + upb_status_seterrmsg(p->status, "Map bool key not 'true' or 'false'"); - upb_env_reporterror(p->env, &p->status); return false; } multipart_end(p); @@ -1723,8 +1698,7 @@ static bool parse_mapentry_key(upb_json_parser *p) { break; } default: - upb_status_seterrmsg(&p->status, "Invalid field type for map key"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Invalid field type for map key"); return false; } @@ -1783,8 +1757,7 @@ static bool handle_mapentry(upb_json_parser *p) { p->top->is_mapentry = true; /* set up to pop frame after value is parsed. */ p->top->mapfield = mapfield; if (p->top->f == NULL) { - upb_status_seterrmsg(&p->status, "mapentry message has no value"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "mapentry message has no value"); return false; } @@ -1819,8 +1792,7 @@ static bool end_membername(upb_json_parser *p) { multipart_end(p); return true; } else { - upb_status_seterrf(&p->status, "No such field: %.*s\n", (int)len, buf); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrf(p->status, "No such field: %.*s\n", (int)len, buf); return false; } } @@ -1846,14 +1818,13 @@ static bool end_any_membername(upb_json_parser *p) { static void end_member(upb_json_parser *p) { /* If we just parsed a map-entry value, end that frame too. */ if (p->top->is_mapentry) { - upb_status s = UPB_STATUS_INIT; upb_selector_t sel; bool ok; const upb_fielddef *mapfield; UPB_ASSERT(p->top > p->stack); /* send ENDMSG on submsg. */ - upb_sink_endmsg(&p->top->sink, &s); + upb_sink_endmsg(&p->top->sink, p->status); mapfield = p->top->mapfield; /* send ENDSUBMSG in repeated-field-of-mapentries frame. */ @@ -1947,10 +1918,9 @@ static bool start_subobject(upb_json_parser *p) { return true; } else { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Object specified for non-message/group field: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } } @@ -2058,10 +2028,9 @@ static bool start_array(upb_json_parser *p) { } if (!upb_fielddef_isseq(p->top->f)) { - upb_status_seterrf(&p->status, + upb_status_seterrf(p->status, "Array specified for non-repeated field: %s", upb_fielddef_name(p->top->f)); - upb_env_reporterror(p->env, &p->status); return false; } @@ -2120,12 +2089,7 @@ static void start_object(upb_json_parser *p) { static void end_object(upb_json_parser *p) { if (!p->top->is_map && p->top->m != NULL) { - upb_status status; - upb_status_clear(&status); - upb_sink_endmsg(&p->top->sink, &status); - if (!upb_ok(&status)) { - upb_env_reporterror(p->env, &status); - } + upb_sink_endmsg(&p->top->sink, p->status); } } @@ -2144,8 +2108,7 @@ static bool end_any_object(upb_json_parser *p, const char *ptr) { if (json_parser_any_frame_has_value(p->top->any_frame) && !json_parser_any_frame_has_type_url(p->top->any_frame)) { - upb_status_seterrmsg(&p->status, "No valid type url"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "No valid type url"); return false; } @@ -2160,8 +2123,7 @@ static bool end_any_object(upb_json_parser *p, const char *ptr) { p->top->any_frame->before_type_url_end - p->top->any_frame->before_type_url_start); if (p->top->any_frame->before_type_url_start == NULL) { - upb_status_seterrmsg(&p->status, "invalid data for well known type."); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "invalid data for well known type."); return false; } p->top->any_frame->before_type_url_start++; @@ -2173,8 +2135,7 @@ static bool end_any_object(upb_json_parser *p, const char *ptr) { (ptr + 1) - p->top->any_frame->after_type_url_start); if (p->top->any_frame->after_type_url_start == NULL) { - upb_status_seterrmsg(&p->status, "Invalid data for well known type."); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrmsg(p->status, "Invalid data for well known type."); return false; } p->top->any_frame->after_type_url_start++; @@ -2247,7 +2208,6 @@ static bool end_any_object(upb_json_parser *p, const char *ptr) { /* Deallocate any parse frame. */ json_parser_any_frame_free(p->top->any_frame); - upb_env_free(p->env, p->top->any_frame); return true; } @@ -2605,8 +2565,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, %% write exec; if (p != pe) { - upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p); - upb_env_reporterror(parser->env, &parser->status); + upb_status_seterrf(parser->status, "Parse error at '%.*s'\n", pe - p, p); } else { capture_suspend(parser, &p); } @@ -2656,13 +2615,12 @@ static void json_parser_reset(upb_json_parser *p) { p->multipart_state = MULTIPART_INACTIVE; p->capture = NULL; p->accumulated = NULL; - upb_status_clear(&p->status); } static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c, const upb_msgdef *md) { upb_msg_field_iter i; - upb_alloc *alloc = upb_arena_alloc(&c->arena); + upb_alloc *alloc = upb_arena_alloc(c->arena); upb_json_parsermethod *m = upb_malloc(alloc, sizeof(*m)); @@ -2703,19 +2661,20 @@ static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c, /* Public API *****************************************************************/ -upb_json_parser *upb_json_parser_create(upb_env *env, +upb_json_parser *upb_json_parser_create(upb_arena *arena, const upb_json_parsermethod *method, const upb_symtab* symtab, upb_sink output, bool ignore_json_unknown) { #ifndef NDEBUG - const size_t size_before = upb_env_bytesallocated(env); + const size_t size_before = upb_arena_bytesallocated(arena); #endif - upb_json_parser *p = upb_env_malloc(env, sizeof(upb_json_parser)); + upb_json_parser *p = upb_arena_malloc(arena, sizeof(upb_json_parser)); if (!p) return false; - p->env = env; + p->arena = arena; p->method = method; + p->status = NULL; p->limit = p->stack + UPB_JSON_MAX_DEPTH; p->accumulate_buf = NULL; p->accumulate_buf_size = 0; @@ -2737,8 +2696,8 @@ upb_json_parser *upb_json_parser_create(upb_env *env, p->ignore_json_unknown = ignore_json_unknown; /* If this fails, uncomment and increase the value in parser.h. */ - /* fprintf(stderr, "%zd\n", upb_env_bytesallocated(env) - size_before); */ - UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(env) - size_before <= + /* fprintf(stderr, "%zd\n", upb_arena_bytesallocated(arena) - size_before); */ + UPB_ASSERT_DEBUGVAR(upb_arena_bytesallocated(arena) - size_before <= UPB_JSON_PARSER_SIZE); return p; } @@ -2758,8 +2717,8 @@ upb_json_codecache *upb_json_codecache_new() { c = upb_gmalloc(sizeof(*c)); - upb_arena_init(&c->arena); - alloc = upb_arena_alloc(&c->arena); + c->arena = upb_arena_new(); + alloc = upb_arena_alloc(c->arena); upb_inttable_init2(&c->methods, UPB_CTYPE_CONSTPTR, alloc); @@ -2767,7 +2726,7 @@ upb_json_codecache *upb_json_codecache_new() { } void upb_json_codecache_free(upb_json_codecache *c) { - upb_arena_uninit(&c->arena); + upb_arena_free(c->arena); upb_gfree(c); } @@ -2776,7 +2735,7 @@ const upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c, upb_json_parsermethod *m; upb_value v; upb_msg_field_iter i; - upb_alloc *alloc = upb_arena_alloc(&c->arena); + upb_alloc *alloc = upb_arena_alloc(c->arena); if (upb_inttable_lookupptr(&c->methods, md, &v)) { return upb_value_getconstptr(v); diff --git a/upb/json/printer.c b/upb/json/printer.c index 83f1a58..bc18055 100644 --- a/upb/json/printer.c +++ b/upb/json/printer.c @@ -1252,13 +1252,13 @@ static void json_printer_reset(upb_json_printer *p) { /* Public API *****************************************************************/ -upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h, +upb_json_printer *upb_json_printer_create(upb_arena *a, const upb_handlers *h, upb_bytessink output) { #ifndef NDEBUG - size_t size_before = upb_env_bytesallocated(e); + size_t size_before = upb_arena_bytesallocated(a); #endif - upb_json_printer *p = upb_env_malloc(e, sizeof(upb_json_printer)); + upb_json_printer *p = upb_arena_malloc(a, sizeof(upb_json_printer)); if (!p) return NULL; p->output_ = output; @@ -1268,7 +1268,7 @@ upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h, p->nanos = 0; /* If this fails, increase the value in printer.h. */ - UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(e) - size_before <= + UPB_ASSERT_DEBUGVAR(upb_arena_bytesallocated(a) - size_before <= UPB_JSON_PRINTER_SIZE); return p; } diff --git a/upb/json/printer.h b/upb/json/printer.h index a7a37bb..857ae47 100644 --- a/upb/json/printer.h +++ b/upb/json/printer.h @@ -24,10 +24,12 @@ class PrinterPtr; struct upb_json_printer; typedef struct upb_json_printer upb_json_printer; -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif /* Native C API. */ -upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h, +upb_json_printer *upb_json_printer_create(upb_arena *a, const upb_handlers *h, upb_bytessink output); upb_sink upb_json_printer_input(upb_json_printer *p); const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md, @@ -36,18 +38,18 @@ const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md, upb_handlercache *upb_json_printer_newcache(bool preserve_proto_fieldnames); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ /* Prints an incoming stream of data to a BytesSink in JSON format. */ class upb::json::PrinterPtr { public: PrinterPtr(upb_json_printer* ptr) : ptr_(ptr) {} - static PrinterPtr Create(Environment *env, const upb::Handlers *handlers, + static PrinterPtr Create(Arena *arena, const upb::Handlers *handlers, BytesSink output) { - return PrinterPtr(upb_json_printer_create(env, handlers, output.sink())); + return PrinterPtr( + upb_json_printer_create(arena->ptr(), handlers, output.sink())); } /* The input to the printer. */ diff --git a/upb/msg.h b/upb/msg.h index 4529478..149b7ab 100644 --- a/upb/msg.h +++ b/upb/msg.h @@ -37,18 +37,22 @@ class MessageLayout; #endif -UPB_DECLARE_TYPE(upb::Map, upb_map) -UPB_DECLARE_TYPE(upb::MapIterator, upb_mapiter) - -struct upb_array; -typedef struct upb_array upb_array; - /* TODO(haberman): C++ accessors */ -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif typedef void upb_msg; +struct upb_array; +typedef struct upb_array upb_array; + +struct upb_map; +typedef struct upb_map upb_map; + +struct upb_mapiter; +typedef struct upb_mapiter upb_mapiter; /** upb_msglayout *************************************************************/ @@ -75,7 +79,6 @@ typedef struct upb_msglayout { bool extendable; } upb_msglayout; - /** upb_stringview ************************************************************/ typedef struct { @@ -103,7 +106,6 @@ UPB_INLINE bool upb_stringview_eql(upb_stringview a, upb_stringview b) { #define UPB_STRINGVIEW_INIT(ptr, len) {ptr, len} - /** upb_msgval ****************************************************************/ /* A union representing all possible protobuf values. Used for generic get/set @@ -156,7 +158,6 @@ UPB_INLINE upb_msgval upb_msgval_makestr(const char *data, size_t size) { return upb_msgval_str(upb_stringview_make(data, size)); } - /** upb_msg *******************************************************************/ /* A upb_msg represents a protobuf message. It always corresponds to a specific @@ -216,7 +217,6 @@ bool upb_msg_clearfield(upb_msg *msg, /* TODO(haberman): copyfrom()/mergefrom()? */ - /** upb_array *****************************************************************/ /* A upb_array stores data for a repeated field. The memory management @@ -236,7 +236,6 @@ upb_msgval upb_array_get(const upb_array *arr, size_t i); bool upb_array_set(upb_array *arr, size_t i, upb_msgval val); - /** upb_map *******************************************************************/ /* A upb_map stores data for a map field. The memory management semantics are @@ -268,7 +267,6 @@ bool upb_map_set(upb_map *map, /* Deletes an entry in the map. Returns true if the key was present. */ bool upb_map_del(upb_map *map, upb_msgval key); - /** upb_mapiter ***************************************************************/ /* For iterating over a map. Map iterators are invalidated by mutations to the @@ -290,6 +288,8 @@ upb_msgval upb_mapiter_value(const upb_mapiter *i); void upb_mapiter_setdone(upb_mapiter *i); bool upb_mapiter_isequal(const upb_mapiter *i1, const upb_mapiter *i2); -UPB_END_EXTERN_C +#ifdef __cplusplus +} /* extern "C" */ +#endif #endif /* UPB_MSG_H_ */ diff --git a/upb/msgfactory.h b/upb/msgfactory.h index 73a26ba..9b3b599 100644 --- a/upb/msgfactory.h +++ b/upb/msgfactory.h @@ -5,10 +5,15 @@ #ifndef UPB_MSGFACTORY_H_ #define UPB_MSGFACTORY_H_ -UPB_DECLARE_TYPE(upb::MessageFactory, upb_msgfactory) - /** upb_msgfactory ************************************************************/ +struct upb_msgfactory; +typedef struct upb_msgfactory upb_msgfactory; + +#ifdef __cplusplus +extern "C" { +#endif + /* A upb_msgfactory contains a cache of upb_msglayout, upb_handlers, and * upb_visitorplan objects. These are the objects necessary to represent, * populate, and and visit upb_msg objects. @@ -36,5 +41,8 @@ const upb_symtab *upb_msgfactory_symtab(const upb_msgfactory *f); const upb_msglayout *upb_msgfactory_getlayout(upb_msgfactory *f, const upb_msgdef *m); +#ifdef __cplusplus +} /* extern "C" */ +#endif #endif /* UPB_MSGFACTORY_H_ */ diff --git a/upb/pb/compile_decoder.c b/upb/pb/compile_decoder.c index e17ca03..ca497ed 100644 --- a/upb/pb/compile_decoder.c +++ b/upb/pb/compile_decoder.c @@ -907,7 +907,7 @@ upb_pbcodecache *upb_pbcodecache_new(upb_handlercache *dest) { c->allow_jit = true; c->lazy = false; - upb_arena_init(&c->arena); + c->arena = upb_arena_new(); if (!upb_inttable_init(&c->groups, UPB_CTYPE_CONSTPTR)) return NULL; return c; diff --git a/upb/pb/decoder.c b/upb/pb/decoder.c index cd64f72..5068225 100644 --- a/upb/pb/decoder.c +++ b/upb/pb/decoder.c @@ -99,9 +99,7 @@ static bool in_residual_buf(const upb_pbdecoder *d, const char *p); * benchmarks. */ static void seterr(upb_pbdecoder *d, const char *msg) { - upb_status status = UPB_STATUS_INIT; - upb_status_seterrmsg(&status, msg); - upb_env_reporterror(d->env, &status); + upb_status_seterrmsg(d->status, msg); } void upb_pbdecoder_seterr(upb_pbdecoder *d, const char *msg) { @@ -992,24 +990,24 @@ void upb_pbdecoder_reset(upb_pbdecoder *d) { d->residual_end = d->residual; } -upb_pbdecoder *upb_pbdecoder_create(upb_env *e, const upb_pbdecodermethod *m, +upb_pbdecoder *upb_pbdecoder_create(upb_arena *a, const upb_pbdecodermethod *m, upb_sink sink) { const size_t default_max_nesting = 64; #ifndef NDEBUG - size_t size_before = upb_env_bytesallocated(e); + size_t size_before = upb_arena_bytesallocated(a); #endif - upb_pbdecoder *d = upb_env_malloc(e, sizeof(upb_pbdecoder)); + upb_pbdecoder *d = upb_arena_malloc(a, sizeof(upb_pbdecoder)); if (!d) return NULL; d->method_ = m; - d->callstack = upb_env_malloc(e, callstacksize(d, default_max_nesting)); - d->stack = upb_env_malloc(e, stacksize(d, default_max_nesting)); + d->callstack = upb_arena_malloc(a, callstacksize(d, default_max_nesting)); + d->stack = upb_arena_malloc(a, stacksize(d, default_max_nesting)); if (!d->stack || !d->callstack) { return NULL; } - d->env = e; + d->arena = a; d->limit = d->stack + default_max_nesting - 1; d->stack_size = default_max_nesting; d->status = NULL; @@ -1024,7 +1022,7 @@ upb_pbdecoder *upb_pbdecoder_create(upb_env *e, const upb_pbdecodermethod *m, d->top->sink = sink; /* If this fails, increase the value in decoder.h. */ - UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(e) - size_before <= + UPB_ASSERT_DEBUGVAR(upb_arena_bytesallocated(a) - size_before <= UPB_PB_DECODER_SIZE); return d; } @@ -1057,7 +1055,7 @@ bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max) { /* Need to reallocate stack and callstack to accommodate. */ size_t old_size = stacksize(d, d->stack_size); size_t new_size = stacksize(d, max); - void *p = upb_env_realloc(d->env, d->stack, old_size, new_size); + void *p = upb_arena_realloc(d->arena, d->stack, old_size, new_size); if (!p) { return false; } @@ -1065,7 +1063,7 @@ bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max) { old_size = callstacksize(d, d->stack_size); new_size = callstacksize(d, max); - p = upb_env_realloc(d->env, d->callstack, old_size, new_size); + p = upb_arena_realloc(d->arena, d->callstack, old_size, new_size); if (!p) { return false; } diff --git a/upb/pb/decoder.h b/upb/pb/decoder.h index ba381f3..6fcef03 100644 --- a/upb/pb/decoder.h +++ b/upb/pb/decoder.h @@ -40,7 +40,9 @@ class DecoderMethodOptions; struct upb_pbdecodermethod; typedef struct upb_pbdecodermethod upb_pbdecodermethod; -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif const upb_handlers *upb_pbdecodermethod_desthandlers( const upb_pbdecodermethod *m); @@ -48,9 +50,8 @@ const upb_byteshandler *upb_pbdecodermethod_inputhandler( const upb_pbdecodermethod *m); bool upb_pbdecodermethod_isnative(const upb_pbdecodermethod *m); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ /* Represents the code to parse a protobuf according to a destination * Handlers. */ @@ -95,9 +96,11 @@ class upb::pb::DecoderMethodPtr { struct upb_pbdecoder; typedef struct upb_pbdecoder upb_pbdecoder; -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif -upb_pbdecoder *upb_pbdecoder_create(upb_env *e, +upb_pbdecoder *upb_pbdecoder_create(upb_arena *arena, const upb_pbdecodermethod *method, upb_sink output); const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d); @@ -107,9 +110,8 @@ size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d); bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max); void upb_pbdecoder_reset(upb_pbdecoder *d); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ /* A Decoder receives binary protobuf data on its input sink and pushes the * decoded data to its output sink. */ @@ -124,9 +126,10 @@ class upb::pb::DecoderPtr { * must also outlive this decoder. * * The sink must match the given method. */ - static DecoderPtr Create(Environment *env, DecoderMethodPtr method, + static DecoderPtr Create(Arena *arena, DecoderMethodPtr method, upb::Sink output) { - return DecoderPtr(upb_pbdecoder_create(env, method.ptr(), output.sink())); + return DecoderPtr( + upb_pbdecoder_create(arena->ptr(), method.ptr(), output.sink())); } /* Returns the DecoderMethod this decoder is parsing from. */ @@ -171,7 +174,9 @@ class upb::pb::DecoderPtr { struct upb_pbcodecache; typedef struct upb_pbcodecache upb_pbcodecache; -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif upb_pbcodecache *upb_pbcodecache_new(upb_handlercache *dest); void upb_pbcodecache_free(upb_pbcodecache *c); @@ -181,9 +186,8 @@ void upb_pbcodecache_setlazy(upb_pbcodecache *c, bool lazy); const upb_pbdecodermethod *upb_pbcodecache_get(upb_pbcodecache *c, const upb_msgdef *md); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ /* A class for caching protobuf processing code, whether bytecode for the * interpreted decoder or machine code for the JIT. diff --git a/upb/pb/decoder.int.h b/upb/pb/decoder.int.h index 8d464fa..47eb3ed 100644 --- a/upb/pb/decoder.int.h +++ b/upb/pb/decoder.int.h @@ -73,7 +73,7 @@ typedef enum { UPB_INLINE opcode getop(uint32_t instr) { return instr & 0xff; } struct upb_pbcodecache { - upb_arena arena; + upb_arena *arena; upb_handlercache *dest; bool allow_jit; bool lazy; @@ -169,7 +169,7 @@ struct upb_pbdecodermethod { }; struct upb_pbdecoder { - upb_env *env; + upb_arena *arena; /* Our input sink. */ upb_bytessink input_; diff --git a/upb/pb/encoder.c b/upb/pb/encoder.c index 1496eba..722cc5b 100644 --- a/upb/pb/encoder.c +++ b/upb/pb/encoder.c @@ -91,7 +91,7 @@ typedef struct { } upb_pb_encoder_segment; struct upb_pb_encoder { - upb_env *env; + upb_arena *arena; /* Our input and output. */ upb_sink input_; @@ -150,7 +150,7 @@ static bool reserve(upb_pb_encoder *e, size_t bytes) { new_size *= 2; } - new_buf = upb_env_realloc(e->env, e->buf, old_size, new_size); + new_buf = upb_arena_realloc(e->arena, e->buf, old_size, new_size); if (new_buf == NULL) { return false; @@ -230,7 +230,7 @@ static bool start_delim(upb_pb_encoder *e) { (e->seglimit - e->segbuf) * sizeof(upb_pb_encoder_segment); size_t new_size = old_size * 2; upb_pb_encoder_segment *new_buf = - upb_env_realloc(e->env, e->segbuf, old_size, new_size); + upb_arena_realloc(e->arena, e->segbuf, old_size, new_size); if (new_buf == NULL) { return false; @@ -526,22 +526,22 @@ upb_handlercache *upb_pb_encoder_newcache() { return upb_handlercache_new(newhandlers_callback, NULL); } -upb_pb_encoder *upb_pb_encoder_create(upb_env *env, const upb_handlers *h, +upb_pb_encoder *upb_pb_encoder_create(upb_arena *arena, const upb_handlers *h, upb_bytessink output) { const size_t initial_bufsize = 256; const size_t initial_segbufsize = 16; /* TODO(haberman): make this configurable. */ const size_t stack_size = 64; #ifndef NDEBUG - const size_t size_before = upb_env_bytesallocated(env); + const size_t size_before = upb_arena_bytesallocated(arena); #endif - upb_pb_encoder *e = upb_env_malloc(env, sizeof(upb_pb_encoder)); + upb_pb_encoder *e = upb_arena_malloc(arena, sizeof(upb_pb_encoder)); if (!e) return NULL; - e->buf = upb_env_malloc(env, initial_bufsize); - e->segbuf = upb_env_malloc(env, initial_segbufsize * sizeof(*e->segbuf)); - e->stack = upb_env_malloc(env, stack_size * sizeof(*e->stack)); + e->buf = upb_arena_malloc(arena, initial_bufsize); + e->segbuf = upb_arena_malloc(arena, initial_segbufsize * sizeof(*e->segbuf)); + e->stack = upb_arena_malloc(arena, stack_size * sizeof(*e->stack)); if (!e->buf || !e->segbuf || !e->stack) { return NULL; @@ -554,13 +554,13 @@ upb_pb_encoder *upb_pb_encoder_create(upb_env *env, const upb_handlers *h, upb_pb_encoder_reset(e); upb_sink_reset(&e->input_, h, e); - e->env = env; + e->arena = arena; e->output_ = output; e->subc = output.closure; e->ptr = e->buf; /* If this fails, increase the value in encoder.h. */ - UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(env) - size_before <= + UPB_ASSERT_DEBUGVAR(upb_arena_bytesallocated(arena) - size_before <= UPB_PB_ENCODER_SIZE); return e; } diff --git a/upb/pb/encoder.h b/upb/pb/encoder.h index 7aa2870..780f60f 100644 --- a/upb/pb/encoder.h +++ b/upb/pb/encoder.h @@ -35,17 +35,18 @@ class EncoderPtr; struct upb_pb_encoder; typedef struct upb_pb_encoder upb_pb_encoder; -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif upb_sink upb_pb_encoder_input(upb_pb_encoder *p); -upb_pb_encoder* upb_pb_encoder_create(upb_env* e, const upb_handlers* h, +upb_pb_encoder* upb_pb_encoder_create(upb_arena* a, const upb_handlers* h, upb_bytessink output); upb_handlercache *upb_pb_encoder_newcache(); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" { */ class upb::pb::EncoderPtr { public: @@ -55,9 +56,10 @@ class upb::pb::EncoderPtr { /* Creates a new encoder in the given environment. The Handlers must have * come from NewHandlers() below. */ - static EncoderPtr Create(Environment* env, const Handlers* handlers, + static EncoderPtr Create(Arena* arena, const Handlers* handlers, BytesSink output) { - return EncoderPtr(upb_pb_encoder_create(env, handlers, output.sink())); + return EncoderPtr( + upb_pb_encoder_create(arena->ptr(), handlers, output.sink())); } /* The input to the encoder. */ diff --git a/upb/pb/textprinter.c b/upb/pb/textprinter.c index d1d539d..91d0d55 100644 --- a/upb/pb/textprinter.c +++ b/upb/pb/textprinter.c @@ -18,7 +18,7 @@ struct upb_textprinter { upb_sink input_; - upb_bytessink *output_; + upb_bytessink output_; int indent_depth_; bool single_line_; void *subc; @@ -35,13 +35,13 @@ static int indent(upb_textprinter *p) { int i; if (!p->single_line_) for (i = 0; i < p->indent_depth_; i++) - upb_bytessink_putbuf(p->output_, p->subc, " ", 2, NULL); + upb_bytessink_putbuf(&p->output_, p->subc, " ", 2, NULL); return 0; } static int endfield(upb_textprinter *p) { const char ch = (p->single_line_ ? ' ' : '\n'); - upb_bytessink_putbuf(p->output_, p->subc, &ch, 1, NULL); + upb_bytessink_putbuf(&p->output_, p->subc, &ch, 1, NULL); return 0; } @@ -60,7 +60,7 @@ static int putescaped(upb_textprinter *p, const char *buf, size_t len, bool is_hex_escape; if (dstend - dst < 4) { - upb_bytessink_putbuf(p->output_, p->subc, dstbuf, dst - dstbuf, NULL); + upb_bytessink_putbuf(&p->output_, p->subc, dstbuf, dst - dstbuf, NULL); dst = dstbuf; } @@ -88,7 +88,7 @@ static int putescaped(upb_textprinter *p, const char *buf, size_t len, last_hex_escape = is_hex_escape; } /* Flush remaining data. */ - upb_bytessink_putbuf(p->output_, p->subc, dstbuf, dst - dstbuf, NULL); + upb_bytessink_putbuf(&p->output_, p->subc, dstbuf, dst - dstbuf, NULL); return 0; } @@ -114,7 +114,7 @@ bool putf(upb_textprinter *p, const char *fmt, ...) { va_end(args); UPB_ASSERT(written == len); - ok = upb_bytessink_putbuf(p->output_, p->subc, str, len, NULL); + ok = upb_bytessink_putbuf(&p->output_, p->subc, str, len, NULL); upb_gfree(str); return ok; } @@ -126,7 +126,7 @@ static bool textprinter_startmsg(void *c, const void *hd) { upb_textprinter *p = c; UPB_UNUSED(hd); if (p->indent_depth_ == 0) { - upb_bytessink_start(p->output_, 0, &p->subc); + upb_bytessink_start(&p->output_, 0, &p->subc); } return true; } @@ -136,7 +136,7 @@ static bool textprinter_endmsg(void *c, const void *hd, upb_status *s) { UPB_UNUSED(hd); UPB_UNUSED(s); if (p->indent_depth_ == 0) { - upb_bytessink_end(p->output_); + upb_bytessink_end(&p->output_); } return true; } @@ -241,7 +241,7 @@ static bool textprinter_endsubmsg(void *closure, const void *handler_data) { UPB_UNUSED(handler_data); p->indent_depth_--; CHECK(indent(p)); - upb_bytessink_putbuf(p->output_, p->subc, "}", 1, NULL); + upb_bytessink_putbuf(&p->output_, p->subc, "}", 1, NULL); CHECK(endfield(p)); return true; err: @@ -315,9 +315,9 @@ static void textprinter_reset(upb_textprinter *p, bool single_line) { /* Public API *****************************************************************/ -upb_textprinter *upb_textprinter_create(upb_env *env, const upb_handlers *h, - upb_bytessink *output) { - upb_textprinter *p = upb_env_malloc(env, sizeof(upb_textprinter)); +upb_textprinter *upb_textprinter_create(upb_arena *arena, const upb_handlers *h, + upb_bytessink output) { + upb_textprinter *p = upb_arena_malloc(arena, sizeof(upb_textprinter)); if (!p) return NULL; p->output_ = output; @@ -331,7 +331,7 @@ upb_handlercache *upb_textprinter_newcache() { return upb_handlercache_new(&onmreg, NULL); } -upb_sink *upb_textprinter_input(upb_textprinter *p) { return &p->input_; } +upb_sink upb_textprinter_input(upb_textprinter *p) { return p->input_; } void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line) { p->single_line_ = single_line; diff --git a/upb/pb/textprinter.h b/upb/pb/textprinter.h index 06ff7d5..e59e11d 100644 --- a/upb/pb/textprinter.h +++ b/upb/pb/textprinter.h @@ -17,60 +17,51 @@ class TextPrinter; } /* namespace upb */ #endif -UPB_DECLARE_TYPE(upb::pb::TextPrinter, upb_textprinter) +/* upb_textprinter ************************************************************/ + +struct upb_textprinter; +typedef struct upb_textprinter upb_textprinter; #ifdef __cplusplus +extern "C" { +#endif -class upb::pb::TextPrinter { +/* C API. */ +upb_textprinter *upb_textprinter_create(upb_arena *arena, const upb_handlers *h, + upb_bytessink output); +void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line); +upb_sink upb_textprinter_input(upb_textprinter *p); +upb_handlercache *upb_textprinter_newcache(); + +#ifdef __cplusplus +} /* extern "C" */ + +class upb::pb::TextPrinterPtr { public: + TextPrinterPtr(upb_textprinter* ptr) : ptr_(ptr) {} + /* The given handlers must have come from NewHandlers(). It must outlive the * TextPrinter. */ - static TextPrinter *Create(Environment *env, const upb::Handlers *handlers, - BytesSink *output); + static TextPrinterPtr *Create(Arena *arena, const upb::Handlers *handlers, + BytesSink output) { + return TextPrinterPtr(upb_textprinter_create(arena, handlers, output)); + } - void SetSingleLineMode(bool single_line); + void SetSingleLineMode(bool single_line) { + upb_textprinter_setsingleline(ptr_, single_line); + } - Sink* input(); + Sink input() { return upb_textprinter_input(ptr_); } /* If handler caching becomes a requirement we can add a code cache as in * decoder.h */ - static HandlerCache* NewCache(); -}; - -#endif - -UPB_BEGIN_EXTERN_C + static HandlerCache NewCache() { + return HandlerCache(upb_textprinter_newcache()); + } -/* C API. */ -upb_textprinter *upb_textprinter_create(upb_env *env, const upb_handlers *h, - upb_bytessink *output); -void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line); -upb_sink *upb_textprinter_input(upb_textprinter *p); - -upb_handlercache *upb_textprinter_newcache(); - -UPB_END_EXTERN_C - -#ifdef __cplusplus - -namespace upb { -namespace pb { -inline TextPrinter *TextPrinter::Create(Environment *env, - const upb::Handlers *handlers, - BytesSink *output) { - return upb_textprinter_create(env, handlers, output); -} -inline void TextPrinter::SetSingleLineMode(bool single_line) { - upb_textprinter_setsingleline(this, single_line); -} -inline Sink* TextPrinter::input() { - return upb_textprinter_input(this); -} -inline HandlerCache* TextPrinter::NewCache() { - return upb_textprinter_newcache(); -} -} /* namespace pb */ -} /* namespace upb */ + private: + upb_textprinter* ptr_; +}; #endif diff --git a/upb/sink.c b/upb/sink.c index 6ef5718..7a7eeb4 100644 --- a/upb/sink.c +++ b/upb/sink.c @@ -15,73 +15,3 @@ bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink sink) { } return ret; } - -struct upb_bufsink { - upb_byteshandler handler; - upb_bytessink sink; - upb_env *env; - char *ptr; - size_t len, size; -}; - -static void *upb_bufsink_start(void *_sink, const void *hd, size_t size_hint) { - upb_bufsink *sink = _sink; - UPB_UNUSED(hd); - UPB_UNUSED(size_hint); - sink->len = 0; - return sink; -} - -static size_t upb_bufsink_string(void *_sink, const void *hd, const char *ptr, - size_t len, const upb_bufhandle *handle) { - upb_bufsink *sink = _sink; - size_t new_size = sink->size; - - UPB_ASSERT(new_size > 0); - UPB_UNUSED(hd); - UPB_UNUSED(handle); - - while (sink->len + len > new_size) { - new_size *= 2; - } - - if (new_size != sink->size) { - sink->ptr = upb_env_realloc(sink->env, sink->ptr, sink->size, new_size); - sink->size = new_size; - } - - memcpy(sink->ptr + sink->len, ptr, len); - sink->len += len; - - return len; -} - -upb_bufsink *upb_bufsink_new(upb_env *env) { - upb_bufsink *sink = upb_env_malloc(env, sizeof(upb_bufsink)); - upb_byteshandler_init(&sink->handler); - upb_byteshandler_setstartstr(&sink->handler, upb_bufsink_start, NULL); - upb_byteshandler_setstring(&sink->handler, upb_bufsink_string, NULL); - - upb_bytessink_reset(&sink->sink, &sink->handler, sink); - - sink->env = env; - sink->size = 32; - sink->ptr = upb_env_malloc(env, sink->size); - sink->len = 0; - - return sink; -} - -void upb_bufsink_free(upb_bufsink *sink) { - upb_env_free(sink->env, sink->ptr); - upb_env_free(sink->env, sink); -} - -upb_bytessink *upb_bufsink_sink(upb_bufsink *sink) { - return &sink->sink; -} - -const char *upb_bufsink_getdata(const upb_bufsink *sink, size_t *len) { - *len = sink->len; - return sink->ptr; -} diff --git a/upb/sink.h b/upb/sink.h index 1855542..d93d966 100644 --- a/upb/sink.h +++ b/upb/sink.h @@ -29,7 +29,9 @@ class Sink; /* upb_sink *******************************************************************/ -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif typedef struct { const upb_handlers *handlers; @@ -192,9 +194,8 @@ UPB_INLINE bool upb_sink_endsubmsg(upb_sink *s, upb_selector_t sel) { return endsubmsg(s->closure, hd); } -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ /* A upb::Sink is an object that binds a upb::Handlers object to some runtime * state. It represents an endpoint to which data can be sent. @@ -288,7 +289,9 @@ class upb::Sink { * sink->EndMessage(&status); * sink->EndSubMessage(endsubmsg_selector); */ bool StartMessage() { return upb_sink_startmsg(&sink_); } - bool EndMessage(Status* status) { return upb_sink_endmsg(&sink_, status); } + bool EndMessage(upb_status *status) { + return upb_sink_endmsg(&sink_, status); + } /* Putting of individual values. These work for both repeated and * non-repeated fields, but for repeated fields you must wrap them in @@ -489,13 +492,14 @@ class upb::BytesSink { /* upb_bufsrc *****************************************************************/ -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink sink); -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ namespace upb { template bool PutBuffer(const T& str, BytesSink sink) { @@ -505,19 +509,4 @@ template bool PutBuffer(const T& str, BytesSink sink) { #endif /* __cplusplus */ -/* upb_bufsink ****************************************************************/ - -/* A class for accumulating output string data in a flat buffer. */ -struct upb_bufsink; -typedef struct upb_bufsink upb_bufsink; - -UPB_BEGIN_EXTERN_C - -upb_bufsink *upb_bufsink_init(upb_env *env); -void upb_bufsink_free(upb_bufsink *sink); -upb_bytessink *upb_bufsink_sink(upb_bufsink *sink); -const char *upb_bufsink_getdata(const upb_bufsink *sink, size_t *len); - -UPB_END_EXTERN_C - #endif diff --git a/upb/table.int.h b/upb/table.int.h index 63dce59..2b86390 100644 --- a/upb/table.int.h +++ b/upb/table.int.h @@ -156,21 +156,6 @@ UPB_INLINE upb_value upb_value_double(double cval) { * initializing a non-first union member. */ typedef uintptr_t upb_tabkey; -#define UPB_TABKEY_NUM(n) n -#define UPB_TABKEY_NONE 0 -/* The preprocessor isn't quite powerful enough to turn the compile-time string - * length into a byte-wise string representation, so code generation needs to - * help it along. - * - * "len1" is the low byte and len4 is the high byte. */ -#ifdef UPB_BIG_ENDIAN -#define UPB_TABKEY_STR(len1, len2, len3, len4, strval) \ - (uintptr_t)(len4 len3 len2 len1 strval) -#else -#define UPB_TABKEY_STR(len1, len2, len3, len4, strval) \ - (uintptr_t)(len1 len2 len3 len4 strval) -#endif - UPB_INLINE char *upb_tabstr(upb_tabkey key, uint32_t *len) { char* mem = (char*)key; if (len) memcpy(len, mem, sizeof(*len)); diff --git a/upb/upb.c b/upb/upb.c index 68e26b0..f56f6c4 100644 --- a/upb/upb.c +++ b/upb/upb.c @@ -8,12 +8,6 @@ #include #include "upb/upb.h" -bool upb_dumptostderr(void *closure, const upb_status* status) { - UPB_UNUSED(closure); - fprintf(stderr, "%s\n", upb_status_errmsg(status)); - return false; -} - /* Guarantee null-termination and provide ellipsis truncation. * It may be tempting to "optimize" this by initializing these final * four bytes up-front and then being careful never to overwrite them, @@ -29,24 +23,17 @@ static void nullz(upb_status *status) { void upb_status_clear(upb_status *status) { if (!status) return; - status->ok_ = true; - status->code_ = 0; + status->ok = true; status->msg[0] = '\0'; } -bool upb_ok(const upb_status *status) { return status->ok_; } - -upb_errorspace *upb_status_errspace(const upb_status *status) { - return status->error_space_; -} - -int upb_status_errcode(const upb_status *status) { return status->code_; } +bool upb_ok(const upb_status *status) { return status->ok; } const char *upb_status_errmsg(const upb_status *status) { return status->msg; } void upb_status_seterrmsg(upb_status *status, const char *msg) { if (!status) return; - status->ok_ = false; + status->ok = false; strncpy(status->msg, msg, sizeof(status->msg)); nullz(status); } @@ -60,17 +47,11 @@ void upb_status_seterrf(upb_status *status, const char *fmt, ...) { void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args) { if (!status) return; - status->ok_ = false; + status->ok = false; _upb_vsnprintf(status->msg, sizeof(status->msg), fmt, args); nullz(status); } -void upb_status_copy(upb_status *to, const upb_status *from) { - if (!to) return; - *to = *from; -} - - /* upb_alloc ******************************************************************/ static void *upb_global_allocfunc(upb_alloc *alloc, void *ptr, size_t oldsize, @@ -87,7 +68,6 @@ static void *upb_global_allocfunc(upb_alloc *alloc, void *ptr, size_t oldsize, upb_alloc upb_alloc_global = {&upb_global_allocfunc}; - /* upb_arena ******************************************************************/ /* Be conservative and choose 16 in case anyone is using SSE. */ @@ -115,11 +95,7 @@ struct upb_arena { /* Cleanup entries. Pointer to a cleanup_ent, defined in env.c */ void *cleanup_head; - - /* For future expansion, since the size of this struct is exposed to users. */ - void *future1; - void *future2; -} upb_arena; +}; typedef struct mem_block { struct mem_block *next; @@ -149,7 +125,6 @@ static void upb_arena_addblock(upb_arena *a, void *ptr, size_t size, /* TODO(haberman): ASAN poison. */ } - static mem_block *upb_arena_allocblock(upb_arena *a, size_t size) { size_t block_size = UPB_MAX(size, a->next_block_size) + sizeof(mem_block); mem_block *block = upb_malloc(a->block_alloc, block_size); @@ -202,7 +177,25 @@ static void *upb_arena_doalloc(upb_alloc *alloc, void *ptr, size_t oldsize, /* Public Arena API ***********************************************************/ -void upb_arena_init(upb_arena *a) { +upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc) { + const size_t first_block_overhead = sizeof(upb_arena) + sizeof(mem_block); + upb_arena *a; + bool owned = false; + + if (n < first_block_overhead) { + /* We need to malloc the initial block. */ + n = first_block_overhead + 256; + owned = true; + if (!alloc || !(mem = upb_malloc(alloc, n))) { + return NULL; + } + } + + a = mem; + mem = (char*)mem + sizeof(*a); + n -= sizeof(*a); + upb_arena_addblock(a, mem, n, owned); + a->alloc.func = &upb_arena_doalloc; a->block_alloc = &upb_alloc_global; a->bytes_allocated = 0; @@ -210,21 +203,12 @@ void upb_arena_init(upb_arena *a) { a->max_block_size = 16384; a->cleanup_head = NULL; a->block_head = NULL; -} - -void upb_arena_init2(upb_arena *a, void *mem, size_t size, upb_alloc *alloc) { - upb_arena_init(a); + a->block_alloc = alloc; - if (size > sizeof(mem_block)) { - upb_arena_addblock(a, mem, size, false); - } - - if (alloc) { - a->block_alloc = alloc; - } + return a; } -void upb_arena_uninit(upb_arena *a) { +void upb_arena_free(upb_arena *a) { cleanup_ent *ent = a->cleanup_head; mem_block *block = a->block_head; @@ -236,6 +220,7 @@ void upb_arena_uninit(upb_arena *a) { /* Must do this after running cleanup functions, because this will delete * the memory we store our cleanup entries in! */ while (block) { + /* Load first since we are deleting block. */ mem_block *next = block->next; if (block->owned) { @@ -244,10 +229,6 @@ void upb_arena_uninit(upb_arena *a) { block = next; } - - /* Protect against multiple-uninit. */ - a->cleanup_head = NULL; - a->block_head = NULL; } bool upb_arena_addcleanup(upb_arena *a, upb_cleanup_func *func, void *ud) { diff --git a/upb/upb.h b/upb/upb.h index 4384812..07b0455 100644 --- a/upb/upb.h +++ b/upb/upb.h @@ -15,14 +15,11 @@ #include #ifdef __cplusplus +#include namespace upb { -class Allocator; class Arena; -class Environment; -class ErrorSpace; class Status; template class InlinedArena; -template class InlinedEnvironment; } #endif @@ -74,46 +71,14 @@ template class InlinedEnvironment; #error Need implementations of [v]snprintf and va_copy #endif -#if (defined(__cplusplus) && __cplusplus >= 201103L) || \ - defined(__GXX_EXPERIMENTAL_CXX0X__) || \ +#ifdef __cplusplus +#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) || \ (defined(_MSC_VER) && _MSC_VER >= 1900) // C++11 is present #else #error upb requires C++11 for C++ support #endif - -/* UPB_DISALLOW_COPY_AND_ASSIGN() - * UPB_DISALLOW_POD_OPS() - * - * Declare these in the "private" section of a C++ class to forbid copy/assign - * or all POD ops (construct, destruct, copy, assign) on that class. */ -#include -#define UPB_DISALLOW_COPY_AND_ASSIGN(class_name) \ - class_name(const class_name&) = delete; \ - void operator=(const class_name&) = delete; -#define UPB_DISALLOW_POD_OPS(class_name, full_class_name) \ - class_name() = delete; \ - ~class_name() = delete; \ - UPB_DISALLOW_COPY_AND_ASSIGN(class_name) -#define UPB_ASSERT_STDLAYOUT(type) \ - static_assert(std::is_standard_layout::value, \ - #type " must be standard layout"); - -#ifdef __cplusplus - -#define UPB_BEGIN_EXTERN_C extern "C" { -#define UPB_END_EXTERN_C } -#define UPB_DECLARE_TYPE(cppname, cname) typedef cppname cname; - -#else /* !defined(__cplusplus) */ - -#define UPB_BEGIN_EXTERN_C -#define UPB_END_EXTERN_C -#define UPB_DECLARE_TYPE(cppname, cname) \ - struct cname; \ - typedef struct cname cname; - -#endif /* defined(__cplusplus) */ +#endif #define UPB_MAX(x, y) ((x) > (y) ? (x) : (y)) #define UPB_MIN(x, y) ((x) < (y) ? (x) : (y)) @@ -138,21 +103,26 @@ template class InlinedEnvironment; #define UPB_UNREACHABLE() do { assert(0); } while(0) #endif -/* upb::Status ****************************************************************/ +/* upb_status *****************************************************************/ -/* upb::Status represents a success or failure status and error message. +/* upb_status represents a success or failure status and error message. * It owns no resources and allocates no memory, so it should work * even in OOM situations. */ -UPB_DECLARE_TYPE(upb::Status, upb_status) /* The maximum length of an error message before it will get truncated. */ -#define UPB_STATUS_MAX_MESSAGE 128 +#define UPB_STATUS_MAX_MESSAGE 127 -UPB_BEGIN_EXTERN_C +typedef struct { + bool ok; + char msg[UPB_STATUS_MAX_MESSAGE]; /* Error message; NULL-terminated. */ +} upb_status; + +#ifdef __cplusplus +extern "C" { +#endif const char *upb_status_errmsg(const upb_status *status); bool upb_ok(const upb_status *status); -int upb_status_errcode(const upb_status *status); /* Any of the functions that write to a status object allow status to be NULL, * to support use cases where the function's caller does not care about the @@ -161,60 +131,44 @@ void upb_status_clear(upb_status *status); void upb_status_seterrmsg(upb_status *status, const char *msg); void upb_status_seterrf(upb_status *status, const char *fmt, ...); void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args); -void upb_status_copy(upb_status *to, const upb_status *from); -UPB_END_EXTERN_C +UPB_INLINE void upb_status_setoom(upb_status *status) { + upb_status_seterrmsg(status, "out of memory"); +} #ifdef __cplusplus +} /* extern "C" */ class upb::Status { public: - Status() { upb_status_clear(this); } + Status() { upb_status_clear(&status_); } - /* Returns true if there is no error. */ - bool ok() const { return upb_ok(this); } + upb_status* ptr() { return &status_; } - /* Optional error space and code, useful if the caller wants to - * programmatically check the specific kind of error. */ - ErrorSpace* error_space() { return upb_status_errspace(this); } - int error_code() const { return upb_status_errcode(this); } + /* Returns true if there is no error. */ + bool ok() const { return upb_ok(&status_); } - /* The returned string is invalidated by any other call into the status. */ - const char *error_message() const { return upb_status_errmsg(this); } + /* Guaranteed to be NULL-terminated. */ + const char *error_message() const { return upb_status_errmsg(&status_); } /* The error message will be truncated if it is longer than * UPB_STATUS_MAX_MESSAGE-4. */ - void SetErrorMessage(const char* msg) { upb_status_seterrmsg(this, msg); } - void SetFormattedErrorMessage(const char* fmt, ...) { + void SetErrorMessage(const char *msg) { upb_status_seterrmsg(&status_, msg); } + void SetFormattedErrorMessage(const char *fmt, ...) { va_list args; va_start(args, fmt); - upb_status_vseterrf(this, fmt, args); + upb_status_vseterrf(&status_, fmt, args); va_end(args); } /* Resets the status to a successful state with no message. */ - void Clear() { upb_status_clear(this); } - - void CopyFrom(const Status& other) { upb_status_copy(this, &other); } + void Clear() { upb_status_clear(&status_); } private: - UPB_DISALLOW_COPY_AND_ASSIGN(Status) -#else -struct upb_status { -#endif - bool ok_; - - /* Specific status code defined by some error space (optional). */ - int code_; - upb_errorspace *error_space_; - - /* TODO(haberman): add file/line of error? */ - - /* Error message; NULL-terminated. */ - char msg[UPB_STATUS_MAX_MESSAGE]; + upb_status status_; }; -#define UPB_STATUS_INIT {true, 0, NULL, {0}} +#endif /* __cplusplus */ /** upb_alloc *****************************************************************/ @@ -291,17 +245,23 @@ UPB_INLINE void upb_gfree(void *ptr) { typedef void upb_cleanup_func(void *ud); struct upb_arena; -typedef upb_arena upb_arena; +typedef struct upb_arena upb_arena; -UPB_BEGIN_EXTERN_C +#ifdef __cplusplus +extern "C" { +#endif -upb_arena *upb_arena_new2(void *mem, size_t n, upb_alloc *alloc); +/* Creates an arena from the given initial block (if any -- n may be 0). + * Additional blocks will be allocated from |alloc|. If |alloc| is NULL, this + * is a fixed-size arena and cannot grow. */ +upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc); void upb_arena_free(upb_arena *a); bool upb_arena_addcleanup(upb_arena *a, upb_cleanup_func *func, void *ud); size_t upb_arena_bytesallocated(const upb_arena *a); UPB_INLINE upb_alloc *upb_arena_alloc(upb_arena *a) { return (upb_alloc*)a; } +/* Convenience wrappers around upb_alloc functions. */ UPB_INLINE void *upb_arena_malloc(upb_arena *a, size_t size) { return upb_malloc(upb_arena_alloc(a), size); } @@ -312,52 +272,38 @@ UPB_INLINE void *upb_arena_realloc(upb_arena *a, void *ptr, size_t oldsize, } UPB_INLINE upb_arena *upb_arena_new() { - return upb_arena_new2(NULL, 0, &upb_alloc_global); + return upb_arena_init(NULL, 0, &upb_alloc_global); } -UPB_END_EXTERN_C - #ifdef __cplusplus +} /* extern "C" */ class upb::Arena { public: /* A simple arena with no initial memory block and the default allocator. */ - Arena() { upb_arena_init(&arena_); } + Arena() : ptr_(upb_arena_new(), upb_arena_free) {} - upb_arena* ptr() { return &arena_; } - - /* Constructs an arena with the given initial block which allocates blocks - * with the given allocator. The given allocator must outlive the Arena. - * - * If you pass NULL for the allocator it will default to the global allocator - * upb_alloc_global, and NULL/0 for the initial block will cause there to be - * no initial block. */ - Arena(void *mem, size_t len, upb_alloc *a) { - upb_arena_init2(&arena_, mem, len, a); - } - - ~Arena() { upb_arena_uninit(&arena_); } + upb_arena* ptr() { return ptr_.get(); } /* Allows this arena to be used as a generic allocator. * * The arena does not need free() calls so when using Arena as an allocator * it is safe to skip them. However they are no-ops so there is no harm in * calling free() either. */ - upb_alloc *allocator() { return upb_arena_alloc(&arena_); } + upb_alloc *allocator() { return upb_arena_alloc(ptr_.get()); } /* Add a cleanup function to run when the arena is destroyed. * Returns false on out-of-memory. */ bool AddCleanup(upb_cleanup_func *func, void *ud) { - return upb_arena_addcleanup(&arena_, func, ud); + return upb_arena_addcleanup(ptr_.get(), func, ud); } /* Total number of bytes that have been allocated. It is undefined what * Realloc() does to &arena_ counter. */ - size_t BytesAllocated() const { return upb_arena_bytesallocated(&arena_); } + size_t BytesAllocated() const { return upb_arena_bytesallocated(ptr_.get()); } private: - UPB_DISALLOW_COPY_AND_ASSIGN(Arena) - upb_arena arena_; + std::unique_ptr ptr_; }; #endif @@ -373,13 +319,16 @@ class upb::Arena { template class upb::InlinedArena : public upb::Arena { public: - InlinedArena() : Arena(initial_block_, N, NULL) {} - explicit InlinedArena(Allocator* a) : Arena(initial_block_, N, a) {} + InlinedArena() : ptr_(upb_arena_new(&initial_block_, N, &upb_alloc_global)) {} + + upb_arena* ptr() { return ptr_.get(); } private: - UPB_DISALLOW_COPY_AND_ASSIGN(InlinedArena) + InlinedArena(const InlinedArena*) = delete; + InlinedArena& operator=(const InlinedArena*) = delete; - char initial_block_[N + UPB_ARENA_BLOCK_OVERHEAD]; + std::unique_ptr ptr_; + char initial_block_[N]; }; #endif /* __cplusplus */ diff --git a/upbc/generator.cc b/upbc/generator.cc index ddfaa7b..de40eee 100644 --- a/upbc/generator.cc +++ b/upbc/generator.cc @@ -502,7 +502,9 @@ void WriteHeader(const protobuf::FileDescriptor* file, Output& output) { "#include \"upb/decode.h\"\n" "#include \"upb/encode.h\"\n" "#include \"upb/port_def.inc\"\n" - "UPB_BEGIN_EXTERN_C\n\n", + "#ifdef __cplusplus\n" + "extern \"C\" {\n" + "#endif\n\n", ToPreproc(file->name())); // Forward-declare types defined in this file. @@ -548,7 +550,9 @@ void WriteHeader(const protobuf::FileDescriptor* file, Output& output) { } output( - "UPB_END_EXTERN_C\n" + "#ifdef __cplusplus\n" + "} /* extern \"C\" */\n" + "#endif\n" "\n" "#include \"upb/port_undef.inc\"\n" "\n" -- cgit v1.2.3 From 754b9f1cfdf59104fa62cfaf99f21e5a817d281b Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 15 Jan 2019 03:37:34 -0800 Subject: All tests pass again! --- tests/json/test_json.cc | 2 +- tests/pb/test_decoder.cc | 12 ++--- tests/pb/test_encoder.cc | 2 +- tests/test_util.h | 27 +++++------ upb/json/parser.c | 114 ++++++++++++++++++++++++----------------------- upb/json/parser.h | 9 ++-- upb/json/parser.rl | 8 ++-- upb/pb/decoder.c | 4 +- upb/pb/decoder.h | 8 ++-- 9 files changed, 95 insertions(+), 91 deletions(-) (limited to 'upb') diff --git a/tests/json/test_json.cc b/tests/json/test_json.cc index 1f7d364..3dc4a6c 100644 --- a/tests/json/test_json.cc +++ b/tests/json/test_json.cc @@ -176,7 +176,7 @@ void test_json_roundtrip_message(const char* json_src, upb::json::PrinterPtr printer = upb::json::PrinterPtr::Create( env.arena(), serialize_handlers, data_sink.Sink()); upb::json::ParserPtr parser = upb::json::ParserPtr::Create( - env.arena(), parser_method, NULL, printer.input(), false); + env.arena(), parser_method, NULL, printer.input(), env.status(), false); env.ResetBytesSink(parser.input()); env.Reset(json_src, strlen(json_src), false, false); diff --git a/tests/pb/test_decoder.cc b/tests/pb/test_decoder.cc index b2dd812..3c41801 100644 --- a/tests/pb/test_decoder.cc +++ b/tests/pb/test_decoder.cc @@ -454,8 +454,9 @@ upb::pb::DecoderMethodPtr global_method; upb::pb::DecoderPtr CreateDecoder(upb::Arena* arena, upb::pb::DecoderMethodPtr method, - upb::Sink sink) { - upb::pb::DecoderPtr ret = upb::pb::DecoderPtr::Create(arena, method, sink); + upb::Sink sink, upb::Status* status) { + upb::pb::DecoderPtr ret = + upb::pb::DecoderPtr::Create(arena, method, sink, status); ret.set_max_nesting(MAX_NESTING); return ret; } @@ -556,7 +557,7 @@ void do_run_decoder(VerboseParserEnvironment* env, upb::pb::DecoderPtr decoder, void run_decoder(const string& proto, const string* expected_output) { VerboseParserEnvironment env(filter_hash != 0); upb::Sink sink(global_handlers, &closures[0]); - upb::pb::DecoderPtr decoder = CreateDecoder(env.arena(), global_method, sink); + upb::pb::DecoderPtr decoder = CreateDecoder(env.arena(), global_method, sink, env.status()); env.ResetBytesSink(decoder.input()); for (size_t i = 0; i < proto.size(); i++) { for (size_t j = i; j < UPB_MIN(proto.size(), i + 5); j++) { @@ -874,7 +875,8 @@ void test_valid() { upb::Status status; upb::Arena arena; upb::Sink sink(global_handlers, &closures[0]); - upb::pb::DecoderPtr decoder = CreateDecoder(&arena, global_method, sink); + upb::pb::DecoderPtr decoder = + CreateDecoder(&arena, global_method, sink, &status); output.clear(); bool ok = upb::PutBuffer(std::string(), decoder.input()); ASSERT(ok); @@ -1161,7 +1163,7 @@ void test_emptyhandlers(upb::SymbolTable* symtab, bool allowjit) { VerboseParserEnvironment env(filter_hash != 0); upb::Sink sink(global_method.dest_handlers(), &closures[0]); upb::pb::DecoderPtr decoder = - CreateDecoder(env.arena(), global_method, sink); + CreateDecoder(env.arena(), global_method, sink, env.status()); env.ResetBytesSink(decoder.input()); env.Reset(testdata[i].data, testdata[i].length, true, false); ASSERT(env.Start()); diff --git a/tests/pb/test_encoder.cc b/tests/pb/test_encoder.cc index 7145097..a042d3d 100644 --- a/tests/pb/test_encoder.cc +++ b/tests/pb/test_encoder.cc @@ -48,7 +48,7 @@ void test_pb_roundtrip() { upb::pb::EncoderPtr encoder = upb::pb::EncoderPtr::Create(&arena, encoder_handlers, string_sink.input()); upb::pb::DecoderPtr decoder = - upb::pb::DecoderPtr::Create(&arena, method, encoder.input()); + upb::pb::DecoderPtr::Create(&arena, method, encoder.input(), &status); ok = upb::PutBuffer(input, decoder.input()); ASSERT(ok); ASSERT(input == output); diff --git a/tests/test_util.h b/tests/test_util.h index 04ca3fb..680673d 100644 --- a/tests/test_util.h +++ b/tests/test_util.h @@ -32,21 +32,6 @@ class VerboseParserEnvironment { /* Pass verbose=true to print detailed diagnostics to stderr. */ VerboseParserEnvironment(bool verbose) : verbose_(verbose) {} - static bool OnError(void *ud, const upb::Status* status) { - VerboseParserEnvironment* env = static_cast(ud); - - if (env->expect_error_ && env->verbose_) { - fprintf(stderr, "Encountered error, as expected: "); - } else if (!env->expect_error_) { - fprintf(stderr, "Encountered unexpected error: "); - } else { - return false; - } - - fprintf(stderr, "%s\n", status->error_message()); - return false; - } - void Reset(const char *buf, size_t len, bool may_skip, bool expect_error) { buf_ = buf; len_ = len; @@ -99,6 +84,17 @@ class VerboseParserEnvironment { return false; } + if (!status_.ok()) { + if (expect_error_ && verbose_) { + fprintf(stderr, "Encountered error, as expected: %s", + status_.error_message()); + } else if (!expect_error_) { + fprintf(stderr, "Encountered unexpected error: %s", + status_.error_message()); + return false; + } + } + return true; } @@ -175,6 +171,7 @@ class VerboseParserEnvironment { bool SkippedWithNull() { return skipped_with_null_; } upb::Arena* arena() { return &arena_; } + upb::Status* status() { return &status_; } private: upb::Arena arena_; diff --git a/upb/json/parser.c b/upb/json/parser.c index a594bfd..5fe20ef 100644 --- a/upb/json/parser.c +++ b/upb/json/parser.c @@ -306,8 +306,9 @@ static void json_parser_any_frame_set_payload_type( /* Initialize parser. */ parser_method = upb_json_codecache_get(frame->parser_codecache, payload_type); upb_sink_reset(&frame->sink, h, encoder); - frame->parser = upb_json_parser_create(p->arena, parser_method, p->symtab, - frame->sink, p->ignore_json_unknown); + frame->parser = + upb_json_parser_create(p->arena, parser_method, p->symtab, frame->sink, + p->status, p->ignore_json_unknown); } static void json_parser_any_frame_free(upb_jsonparser_any_frame *frame) { @@ -2379,11 +2380,11 @@ static bool is_string_wrapper_object(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 2541 "upb/json/parser.rl" +#line 2542 "upb/json/parser.rl" -#line 2387 "upb/json/parser.c" +#line 2388 "upb/json/parser.c" static const char _json_actions[] = { 0, 1, 0, 1, 1, 1, 3, 1, 4, 1, 6, 1, 7, 1, 8, 1, @@ -2630,7 +2631,7 @@ static const int json_en_value_machine = 75; static const int json_en_main = 1; -#line 2544 "upb/json/parser.rl" +#line 2545 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -2653,7 +2654,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 2657 "upb/json/parser.c" +#line 2658 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -2728,83 +2729,83 @@ _match: switch ( *_acts++ ) { case 1: -#line 2392 "upb/json/parser.rl" +#line 2393 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 2394 "upb/json/parser.rl" +#line 2395 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 23;goto _again;} } break; case 3: -#line 2398 "upb/json/parser.rl" +#line 2399 "upb/json/parser.rl" { start_text(parser, p); } break; case 4: -#line 2399 "upb/json/parser.rl" +#line 2400 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -#line 2405 "upb/json/parser.rl" +#line 2406 "upb/json/parser.rl" { start_hex(parser); } break; case 6: -#line 2406 "upb/json/parser.rl" +#line 2407 "upb/json/parser.rl" { hexdigit(parser, p); } break; case 7: -#line 2407 "upb/json/parser.rl" +#line 2408 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -#line 2413 "upb/json/parser.rl" +#line 2414 "upb/json/parser.rl" { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -#line 2419 "upb/json/parser.rl" +#line 2420 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 10: -#line 2431 "upb/json/parser.rl" +#line 2432 "upb/json/parser.rl" { start_duration_base(parser, p); } break; case 11: -#line 2432 "upb/json/parser.rl" +#line 2433 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_duration_base(parser, p)); } break; case 12: -#line 2434 "upb/json/parser.rl" +#line 2435 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 13: -#line 2439 "upb/json/parser.rl" +#line 2440 "upb/json/parser.rl" { start_timestamp_base(parser, p); } break; case 14: -#line 2440 "upb/json/parser.rl" +#line 2441 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_base(parser, p)); } break; case 15: -#line 2442 "upb/json/parser.rl" +#line 2443 "upb/json/parser.rl" { start_timestamp_fraction(parser, p); } break; case 16: -#line 2443 "upb/json/parser.rl" +#line 2444 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } break; case 17: -#line 2445 "upb/json/parser.rl" +#line 2446 "upb/json/parser.rl" { start_timestamp_zone(parser, p); } break; case 18: -#line 2446 "upb/json/parser.rl" +#line 2447 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } break; case 19: -#line 2448 "upb/json/parser.rl" +#line 2449 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 20: -#line 2453 "upb/json/parser.rl" +#line 2454 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) { {stack[top++] = cs; cs = 47;goto _again;} @@ -2816,11 +2817,11 @@ _match: } break; case 21: -#line 2464 "upb/json/parser.rl" +#line 2465 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 75;goto _again;} } break; case 22: -#line 2469 "upb/json/parser.rl" +#line 2470 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_member(parser, p); @@ -2830,11 +2831,11 @@ _match: } break; case 23: -#line 2476 "upb/json/parser.rl" +#line 2477 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_membername(parser)); } break; case 24: -#line 2479 "upb/json/parser.rl" +#line 2480 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { end_any_member(parser, p); @@ -2844,7 +2845,7 @@ _match: } break; case 25: -#line 2490 "upb/json/parser.rl" +#line 2491 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_object(parser, p); @@ -2854,7 +2855,7 @@ _match: } break; case 26: -#line 2499 "upb/json/parser.rl" +#line 2500 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { CHECK_RETURN_TOP(end_any_object(parser, p)); @@ -2864,54 +2865,54 @@ _match: } break; case 27: -#line 2511 "upb/json/parser.rl" +#line 2512 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_array(parser)); } break; case 28: -#line 2515 "upb/json/parser.rl" +#line 2516 "upb/json/parser.rl" { end_array(parser); } break; case 29: -#line 2520 "upb/json/parser.rl" +#line 2521 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_number(parser, p)); } break; case 30: -#line 2521 "upb/json/parser.rl" +#line 2522 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 31: -#line 2523 "upb/json/parser.rl" +#line 2524 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_stringval(parser)); } break; case 32: -#line 2524 "upb/json/parser.rl" +#line 2525 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_stringval(parser)); } break; case 33: -#line 2526 "upb/json/parser.rl" +#line 2527 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 34: -#line 2528 "upb/json/parser.rl" +#line 2529 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 35: -#line 2530 "upb/json/parser.rl" +#line 2531 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 36: -#line 2532 "upb/json/parser.rl" +#line 2533 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_subobject_full(parser)); } break; case 37: -#line 2533 "upb/json/parser.rl" +#line 2534 "upb/json/parser.rl" { end_subobject_full(parser); } break; case 38: -#line 2538 "upb/json/parser.rl" +#line 2539 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 2915 "upb/json/parser.c" +#line 2916 "upb/json/parser.c" } } @@ -2928,32 +2929,32 @@ _again: while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 0: -#line 2390 "upb/json/parser.rl" +#line 2391 "upb/json/parser.rl" { p--; {cs = stack[--top]; if ( p == pe ) goto _test_eof; goto _again;} } break; case 30: -#line 2521 "upb/json/parser.rl" +#line 2522 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 33: -#line 2526 "upb/json/parser.rl" +#line 2527 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 34: -#line 2528 "upb/json/parser.rl" +#line 2529 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 35: -#line 2530 "upb/json/parser.rl" +#line 2531 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 37: -#line 2533 "upb/json/parser.rl" +#line 2534 "upb/json/parser.rl" { end_subobject_full(parser); } break; -#line 2957 "upb/json/parser.c" +#line 2958 "upb/json/parser.c" } } } @@ -2961,7 +2962,7 @@ goto _again;} } _out: {} } -#line 2566 "upb/json/parser.rl" +#line 2567 "upb/json/parser.rl" if (p != pe) { upb_status_seterrf(parser->status, "Parse error at '%.*s'\n", pe - p, p); @@ -3008,13 +3009,13 @@ static void json_parser_reset(upb_json_parser *p) { /* Emit Ragel initialization of the parser. */ -#line 3012 "upb/json/parser.c" +#line 3013 "upb/json/parser.c" { cs = json_start; top = 0; } -#line 2612 "upb/json/parser.rl" +#line 2613 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); @@ -3071,6 +3072,7 @@ upb_json_parser *upb_json_parser_create(upb_arena *arena, const upb_json_parsermethod *method, const upb_symtab* symtab, upb_sink output, + upb_status *status, bool ignore_json_unknown) { #ifndef NDEBUG const size_t size_before = upb_arena_bytesallocated(arena); @@ -3080,7 +3082,7 @@ upb_json_parser *upb_json_parser_create(upb_arena *arena, p->arena = arena; p->method = method; - p->status = NULL; + p->status = status; p->limit = p->stack + UPB_JSON_MAX_DEPTH; p->accumulate_buf = NULL; p->accumulate_buf_size = 0; diff --git a/upb/json/parser.h b/upb/json/parser.h index 2a06fcf..6f3eaa7 100644 --- a/upb/json/parser.h +++ b/upb/json/parser.h @@ -71,6 +71,7 @@ upb_json_parser* upb_json_parser_create(upb_arena* a, const upb_json_parsermethod* m, const upb_symtab* symtab, upb_sink output, + upb_status *status, bool ignore_json_unknown); upb_bytessink upb_json_parser_input(upb_json_parser* p); @@ -84,12 +85,12 @@ class upb::json::ParserPtr { ParserPtr(upb_json_parser* ptr) : ptr_(ptr) {} static ParserPtr Create(Arena* arena, ParserMethodPtr method, - SymbolTable* symtab, Sink output, + SymbolTable* symtab, Sink output, Status* status, bool ignore_json_unknown) { upb_symtab* symtab_ptr = symtab ? symtab->ptr() : nullptr; - return ParserPtr(upb_json_parser_create(arena->ptr(), method.ptr(), - symtab_ptr, output.sink(), - ignore_json_unknown)); + return ParserPtr(upb_json_parser_create( + arena->ptr(), method.ptr(), symtab_ptr, output.sink(), status->ptr(), + ignore_json_unknown)); } BytesSink input() { return upb_json_parser_input(ptr_); } diff --git a/upb/json/parser.rl b/upb/json/parser.rl index a117d0c..6f27630 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -304,8 +304,9 @@ static void json_parser_any_frame_set_payload_type( /* Initialize parser. */ parser_method = upb_json_codecache_get(frame->parser_codecache, payload_type); upb_sink_reset(&frame->sink, h, encoder); - frame->parser = upb_json_parser_create(p->arena, parser_method, p->symtab, - frame->sink, p->ignore_json_unknown); + frame->parser = + upb_json_parser_create(p->arena, parser_method, p->symtab, frame->sink, + p->status, p->ignore_json_unknown); } static void json_parser_any_frame_free(upb_jsonparser_any_frame *frame) { @@ -2665,6 +2666,7 @@ upb_json_parser *upb_json_parser_create(upb_arena *arena, const upb_json_parsermethod *method, const upb_symtab* symtab, upb_sink output, + upb_status *status, bool ignore_json_unknown) { #ifndef NDEBUG const size_t size_before = upb_arena_bytesallocated(arena); @@ -2674,7 +2676,7 @@ upb_json_parser *upb_json_parser_create(upb_arena *arena, p->arena = arena; p->method = method; - p->status = NULL; + p->status = status; p->limit = p->stack + UPB_JSON_MAX_DEPTH; p->accumulate_buf = NULL; p->accumulate_buf_size = 0; diff --git a/upb/pb/decoder.c b/upb/pb/decoder.c index 5068225..f1617db 100644 --- a/upb/pb/decoder.c +++ b/upb/pb/decoder.c @@ -991,7 +991,7 @@ void upb_pbdecoder_reset(upb_pbdecoder *d) { } upb_pbdecoder *upb_pbdecoder_create(upb_arena *a, const upb_pbdecodermethod *m, - upb_sink sink) { + upb_sink sink, upb_status *status) { const size_t default_max_nesting = 64; #ifndef NDEBUG size_t size_before = upb_arena_bytesallocated(a); @@ -1010,7 +1010,7 @@ upb_pbdecoder *upb_pbdecoder_create(upb_arena *a, const upb_pbdecodermethod *m, d->arena = a; d->limit = d->stack + default_max_nesting - 1; d->stack_size = default_max_nesting; - d->status = NULL; + d->status = status; upb_pbdecoder_reset(d); upb_bytessink_reset(&d->input_, &m->input_handler_, d); diff --git a/upb/pb/decoder.h b/upb/pb/decoder.h index 6fcef03..5adfba8 100644 --- a/upb/pb/decoder.h +++ b/upb/pb/decoder.h @@ -102,7 +102,7 @@ extern "C" { upb_pbdecoder *upb_pbdecoder_create(upb_arena *arena, const upb_pbdecodermethod *method, - upb_sink output); + upb_sink output, upb_status *status); const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d); upb_bytessink upb_pbdecoder_input(upb_pbdecoder *d); uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d); @@ -127,9 +127,9 @@ class upb::pb::DecoderPtr { * * The sink must match the given method. */ static DecoderPtr Create(Arena *arena, DecoderMethodPtr method, - upb::Sink output) { - return DecoderPtr( - upb_pbdecoder_create(arena->ptr(), method.ptr(), output.sink())); + upb::Sink output, Status *status) { + return DecoderPtr(upb_pbdecoder_create(arena->ptr(), method.ptr(), + output.sink(), status->ptr())); } /* Returns the DecoderMethod this decoder is parsing from. */ -- cgit v1.2.3 From 1508648f30cbaf5a3590572b2313fb5b595a7946 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 15 Jan 2019 04:21:56 -0800 Subject: Build & fix the JIT. --- BUILD | 20 ++- tools/amalgamate.py | 4 + tools/make_cmakelists.py | 6 + upb/pb/compile_decoder_x64.c | 3 +- upb/pb/compile_decoder_x64.dasc | 19 ++- upb/pb/compile_decoder_x64.h | 287 ++++++++++++++++++++-------------------- upb/upb.h | 1 + 7 files changed, 189 insertions(+), 151 deletions(-) (limited to 'upb') diff --git a/BUILD b/BUILD index 9e79d95..5804d36 100644 --- a/BUILD +++ b/BUILD @@ -11,6 +11,11 @@ load( "upb_proto_reflection_library", ) +config_setting( + name = "k8", + values = {"cpu": "k8"}, +) + # C/C++ rules ################################################################## cc_library( @@ -62,7 +67,15 @@ cc_library( "upb/pb/textprinter.c", "upb/pb/varint.c", "upb/pb/varint.int.h", - ], + ] + select({ + ":k8": [ + "upb/pb/compile_decoder_x64.c", + "upb/pb/compile_decoder_x64.h", + "third_party/dynasm/dasm_proto.h", + "third_party/dynasm/dasm_x86.h", + ], + "//conditions:default": [], + }), hdrs = [ "upb/pb/decoder.h", "upb/pb/encoder.h", @@ -72,7 +85,10 @@ cc_library( "-std=c89", "-pedantic", "-Wno-long-long", - ], + ] + select({ + ":k8": ["-DUPB_USE_JIT_X64"], + "//conditions:default": [], + }), deps = [ ":upb", ], diff --git a/tools/amalgamate.py b/tools/amalgamate.py index 4739a94..9ad0286 100755 --- a/tools/amalgamate.py +++ b/tools/amalgamate.py @@ -51,6 +51,10 @@ output_path = sys.argv[2] amalgamator = Amalgamator(include_path, output_path) for filename in sys.argv[3:]: + # Leave JIT out of the amalgamation. + if "x64" in filename or "dynasm" in filename: + continue + amalgamator.add_src(filename.strip()) amalgamator.finish() diff --git a/tools/make_cmakelists.py b/tools/make_cmakelists.py index 0d5640d..c2700b6 100755 --- a/tools/make_cmakelists.py +++ b/tools/make_cmakelists.py @@ -128,6 +128,12 @@ class BuildFileFunctions(object): def genrule(self, **kwargs): pass + def config_setting(self, **kwargs): + pass + + def select(self, arg_dict): + return [] + def glob(*args): return [] diff --git a/upb/pb/compile_decoder_x64.c b/upb/pb/compile_decoder_x64.c index fd541a4..7c716e8 100644 --- a/upb/pb/compile_decoder_x64.c +++ b/upb/pb/compile_decoder_x64.c @@ -233,7 +233,8 @@ static int getjmptarget(jitcompiler *jc, const void *key) { * * Creates/allocates a pclabel for this target if one does not exist already. */ static int jmptarget(jitcompiler *jc, const void *key) { - // Optimizer sometimes can't figure out that initializing this is unnecessary. + /* Optimizer sometimes can't figure out that initializing this is unnecessary. + */ int pclabel = 0; if (!try_getjmptarget(jc, key, &pclabel)) { pclabel = alloc_pclabel(jc); diff --git a/upb/pb/compile_decoder_x64.dasc b/upb/pb/compile_decoder_x64.dasc index 7fcd006..7dc1987 100644 --- a/upb/pb/compile_decoder_x64.dasc +++ b/upb/pb/compile_decoder_x64.dasc @@ -92,7 +92,7 @@ |.endmacro | |.macro load_handler_data, h, arg -| ld64 upb_handlers_gethandlerdata(h, arg) +| ld64 gethandlerdata(h, arg) |.endmacro | |.macro chkeob, bytes, target @@ -140,7 +140,7 @@ #define DECODE_EOF -3 static upb_func *gethandler(const upb_handlers *h, upb_selector_t sel) { - return h ? upb_handlers_gethandler(h, sel) : NULL; + return h ? upb_handlers_gethandler(h, sel, NULL) : NULL; } /* Defines an "assembly label" for the current code generation offset. @@ -179,14 +179,19 @@ static void asmlabel(jitcompiler *jc, const char *fmt, ...) { /* Should only be called when the associated handler is known to exist. */ static bool alwaysok(const upb_handlers *h, upb_selector_t sel) { - upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER; + upb_handlerattr attr = UPB_HANDLERATTR_INIT; bool ok = upb_handlers_getattr(h, sel, &attr); - bool ret; UPB_ASSERT(ok); - ret = upb_handlerattr_alwaysok(&attr); - upb_handlerattr_uninit(&attr); - return ret; + return attr.alwaysok; +} + +static const void *gethandlerdata(const upb_handlers *h, upb_selector_t sel) { + upb_handlerattr attr = UPB_HANDLERATTR_INIT; + bool ok = upb_handlers_getattr(h, sel, &attr); + + UPB_ASSERT(ok); + return attr.handler_data; } /* Emit static assembly routines; code that does not vary based on the message diff --git a/upb/pb/compile_decoder_x64.h b/upb/pb/compile_decoder_x64.h index 2c07063..4a4dffc 100644 --- a/upb/pb/compile_decoder_x64.h +++ b/upb/pb/compile_decoder_x64.h @@ -278,7 +278,7 @@ static const char *const upb_jit_globalnames[] = { /*|.endmacro */ /*| */ /*|.macro load_handler_data, h, arg */ -/*| ld64 upb_handlers_gethandlerdata(h, arg) */ +/*| ld64 gethandlerdata(h, arg) */ /*|.endmacro */ /*| */ /*|.macro chkeob, bytes, target */ @@ -326,7 +326,7 @@ static const char *const upb_jit_globalnames[] = { #define DECODE_EOF -3 static upb_func *gethandler(const upb_handlers *h, upb_selector_t sel) { - return h ? upb_handlers_gethandler(h, sel) : NULL; + return h ? upb_handlers_gethandler(h, sel, NULL) : NULL; } /* Defines an "assembly label" for the current code generation offset. @@ -367,14 +367,19 @@ static void asmlabel(jitcompiler *jc, const char *fmt, ...) { /* Should only be called when the associated handler is known to exist. */ static bool alwaysok(const upb_handlers *h, upb_selector_t sel) { - upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER; + upb_handlerattr attr = UPB_HANDLERATTR_INIT; bool ok = upb_handlers_getattr(h, sel, &attr); - bool ret; UPB_ASSERT(ok); - ret = upb_handlerattr_alwaysok(&attr); - upb_handlerattr_uninit(&attr); - return ret; + return attr.alwaysok; +} + +static const void *gethandlerdata(const upb_handlers *h, upb_selector_t sel) { + upb_handlerattr attr = UPB_HANDLERATTR_INIT; + bool ok = upb_handlers_getattr(h, sel, &attr); + + UPB_ASSERT(ok); + return attr.handler_data; } /* Emit static assembly routines; code that does not vary based on the message @@ -424,7 +429,7 @@ static void emit_static_asm(jitcompiler *jc) { /*|1: */ /*| pop rbx */ dasm_put(Dst, 2, (unsigned int)((uintptr_t)upb_pbdecoder_resume), (unsigned int)(((uintptr_t)upb_pbdecoder_resume)>>32), 0xfffffffffffffff0UL, Dt2(->saved_rsp), Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt1(->sink.closure), Dt1(->end_ofs), Dt2(->bufstart_ofs), Dt2(->buf), Dt2(->call_len), Dt2(->size_param), Dt2(->call_len)); -# 238 "upb/pb/compile_decoder_x64.dasc" +# 243 "upb/pb/compile_decoder_x64.dasc" /*| pop r12 */ /*| pop r13 */ /*| pop r14 */ @@ -445,7 +450,7 @@ static void emit_static_asm(jitcompiler *jc) { /*| // the JIT resumes, and more buffer space will be available. */ /*| // Args: eax=the value that decode() should return. */ dasm_put(Dst, 115, Dt2(->callstack), (unsigned int)((uintptr_t)memcpy), (unsigned int)(((uintptr_t)memcpy)>>32), 0xfffffffffffffff0UL); -# 257 "upb/pb/compile_decoder_x64.dasc" +# 262 "upb/pb/compile_decoder_x64.dasc" asmlabel(jc, "exitjit"); /*|->exitjit: */ /*| // Save the stack into DECODER->callstack. */ @@ -473,7 +478,7 @@ static void emit_static_asm(jitcompiler *jc) { /*| // (from the caller's perspective) not to return until the decoder is */ /*| // resumed. */ dasm_put(Dst, 161, Dt2(->callstack), Dt2(->saved_rsp), Dt2(->call_len), (unsigned int)((uintptr_t)memcpy), (unsigned int)(((uintptr_t)memcpy)>>32), 0xfffffffffffffff0UL, Dt2(->saved_rsp)); -# 283 "upb/pb/compile_decoder_x64.dasc" +# 288 "upb/pb/compile_decoder_x64.dasc" asmlabel(jc, "suspend"); /*|->suspend: */ /*| cmp DECODER->ptr, PTR */ @@ -486,7 +491,7 @@ static void emit_static_asm(jitcompiler *jc) { /*| jmp ->exitjit */ /*| */ dasm_put(Dst, 222, Dt2(->ptr), Dt2(->checkpoint), Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt2(->delim_end), Dt2(->buf), Dt2(->bufstart_ofs), Dt1(->end_ofs), Dt1(->sink.closure), (unsigned int)((uintptr_t)upb_pbdecoder_suspend), (unsigned int)(((uintptr_t)upb_pbdecoder_suspend)>>32), 0xfffffffffffffff0UL); -# 294 "upb/pb/compile_decoder_x64.dasc" +# 299 "upb/pb/compile_decoder_x64.dasc" asmlabel(jc, "pushlendelim"); /*|->pushlendelim: */ /*|1: */ @@ -499,7 +504,7 @@ static void emit_static_asm(jitcompiler *jc) { } else { dasm_put(Dst, 321); } -# 300 "upb/pb/compile_decoder_x64.dasc" +# 305 "upb/pb/compile_decoder_x64.dasc" /*| mov rcx, DELIMEND */ /*| sub rcx, PTR */ /*| sub rcx, rdx */ @@ -520,7 +525,7 @@ static void emit_static_asm(jitcompiler *jc) { /*| ja >2 */ /*| mov DATAEND, DELIMEND // If DELIMEND >= PTR && DELIMEND < DATAEND */ dasm_put(Dst, 337, Dt1(->end_ofs), Dt2(->limit), sizeof(upb_pbdecoder_frame), Dt1(->groupnum), Dt2(->end)); -# 319 "upb/pb/compile_decoder_x64.dasc" +# 324 "upb/pb/compile_decoder_x64.dasc" /*|2: */ /*| ret */ /*|3: */ @@ -540,7 +545,7 @@ static void emit_static_asm(jitcompiler *jc) { dasm_put(Dst, 454); } } -# 327 "upb/pb/compile_decoder_x64.dasc" +# 332 "upb/pb/compile_decoder_x64.dasc" /*| callp upb_pbdecoder_seterr */ /*| call ->suspend */ /*| jmp <1 */ @@ -561,7 +566,7 @@ static void emit_static_asm(jitcompiler *jc) { dasm_put(Dst, 454); } } -# 336 "upb/pb/compile_decoder_x64.dasc" +# 341 "upb/pb/compile_decoder_x64.dasc" /*| callp upb_pbdecoder_seterr */ /*| call ->suspend */ /*| jmp <1 */ @@ -592,7 +597,7 @@ static void emit_static_asm(jitcompiler *jc) { /*|.endmacro */ /*| */ dasm_put(Dst, 497, (unsigned int)((uintptr_t)upb_pbdecoder_seterr), (unsigned int)(((uintptr_t)upb_pbdecoder_seterr)>>32), 0xfffffffffffffff0UL); -# 365 "upb/pb/compile_decoder_x64.dasc" +# 370 "upb/pb/compile_decoder_x64.dasc" asmlabel(jc, "parse_unknown"); /*| // Args: edx=fieldnum, cl=wire type */ /*|->parse_unknown: */ @@ -607,7 +612,7 @@ static void emit_static_asm(jitcompiler *jc) { /*| cmp eax, DECODE_ENDGROUP */ /*| jne >1 */ dasm_put(Dst, 526, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt2(->delim_end), Dt2(->buf), Dt2(->bufstart_ofs), Dt1(->end_ofs), Dt1(->sink.closure), (unsigned int)((uintptr_t)upb_pbdecoder_skipunknown), (unsigned int)(((uintptr_t)upb_pbdecoder_skipunknown)>>32), 0xfffffffffffffff0UL, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt1(->sink.closure), Dt1(->end_ofs), Dt2(->bufstart_ofs), Dt2(->buf), DECODE_ENDGROUP); -# 378 "upb/pb/compile_decoder_x64.dasc" +# 383 "upb/pb/compile_decoder_x64.dasc" /*| ret // Return eax=DECODE_ENDGROUP, not zero */ /*|1: */ /*| cmp eax, DECODE_OK */ @@ -627,26 +632,26 @@ static void emit_static_asm(jitcompiler *jc) { /*| // completes. We also set DECODER->ptr to this value which is a signal to */ /*| // ->suspend that DECODER->checkpoint is up to date. */ dasm_put(Dst, 623, DECODE_OK); -# 396 "upb/pb/compile_decoder_x64.dasc" +# 401 "upb/pb/compile_decoder_x64.dasc" asmlabel(jc, "skip_decode_f32_fallback"); /*|->skipf32_fallback: */ /*|->decodef32_fallback: */ /*| getvalue_slow upb_pbdecoder_decode_f32, 4 */ dasm_put(Dst, 647, Dt2(->checkpoint), Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt2(->delim_end), Dt2(->buf), Dt2(->bufstart_ofs), Dt1(->end_ofs), Dt1(->sink.closure), (unsigned int)((uintptr_t)upb_pbdecoder_decode_f32), (unsigned int)(((uintptr_t)upb_pbdecoder_decode_f32)>>32), 0xfffffffffffffff0UL, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt1(->sink.closure), Dt1(->end_ofs)); -# 400 "upb/pb/compile_decoder_x64.dasc" +# 405 "upb/pb/compile_decoder_x64.dasc" /*| */ dasm_put(Dst, 751, Dt2(->bufstart_ofs), Dt2(->buf), Dt2(->ptr)); -# 401 "upb/pb/compile_decoder_x64.dasc" +# 406 "upb/pb/compile_decoder_x64.dasc" asmlabel(jc, "skip_decode_f64_fallback"); /*|->skipf64_fallback: */ /*|->decodef64_fallback: */ /*| getvalue_slow upb_pbdecoder_decode_f64, 8 */ dasm_put(Dst, 799, Dt2(->checkpoint), Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt2(->delim_end), Dt2(->buf), Dt2(->bufstart_ofs), Dt1(->end_ofs), Dt1(->sink.closure), (unsigned int)((uintptr_t)upb_pbdecoder_decode_f64), (unsigned int)(((uintptr_t)upb_pbdecoder_decode_f64)>>32), 0xfffffffffffffff0UL, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt1(->sink.closure), Dt1(->end_ofs)); -# 405 "upb/pb/compile_decoder_x64.dasc" +# 410 "upb/pb/compile_decoder_x64.dasc" /*| */ /*| // Called for varint >= 1 byte. */ dasm_put(Dst, 903, Dt2(->bufstart_ofs), Dt2(->buf), Dt2(->ptr)); -# 407 "upb/pb/compile_decoder_x64.dasc" +# 412 "upb/pb/compile_decoder_x64.dasc" asmlabel(jc, "skip_decode_v32_fallback"); /*|->skipv32_fallback: */ /*|->skipv64_fallback: */ @@ -657,7 +662,7 @@ static void emit_static_asm(jitcompiler *jc) { } else { dasm_put(Dst, 964); } -# 411 "upb/pb/compile_decoder_x64.dasc" +# 416 "upb/pb/compile_decoder_x64.dasc" /*| // With at least 16 bytes left, we can do a branch-less SSE version. */ /*| movdqu xmm0, [PTR] */ /*| pmovmskb eax, xmm0 // bits 0-15 are continuation bits, 16-31 are 0. */ @@ -687,7 +692,7 @@ static void emit_static_asm(jitcompiler *jc) { /*| */ /*| // Returns tag in edx */ dasm_put(Dst, 980, 10); -# 439 "upb/pb/compile_decoder_x64.dasc" +# 444 "upb/pb/compile_decoder_x64.dasc" asmlabel(jc, "decode_unknown_tag_fallback"); /*|->decode_unknown_tag_fallback: */ /*| sub rsp, 16 */ @@ -705,7 +710,7 @@ static void emit_static_asm(jitcompiler *jc) { /*| callp upb_pbdecoder_decode_varint_slow */ /*| load_regs */ dasm_put(Dst, 1053, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt2(->delim_end), Dt2(->buf), Dt2(->bufstart_ofs), Dt1(->end_ofs), Dt1(->sink.closure), (unsigned int)((uintptr_t)upb_pbdecoder_decode_varint_slow), (unsigned int)(((uintptr_t)upb_pbdecoder_decode_varint_slow)>>32), 0xfffffffffffffff0UL, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt1(->sink.closure)); -# 455 "upb/pb/compile_decoder_x64.dasc" +# 460 "upb/pb/compile_decoder_x64.dasc" /*| cmp eax, 0 */ /*| jge >3 */ /*| mov edx, [rsp] // Success; return parsed data. */ @@ -717,7 +722,7 @@ static void emit_static_asm(jitcompiler *jc) { /*| */ /*| // Called for varint >= 1 byte. */ dasm_put(Dst, 1156, Dt1(->end_ofs), Dt2(->bufstart_ofs), Dt2(->buf)); -# 465 "upb/pb/compile_decoder_x64.dasc" +# 470 "upb/pb/compile_decoder_x64.dasc" asmlabel(jc, "decode_v32_v64_fallback"); /*|->decodev32_fallback: */ /*|->decodev64_fallback: */ @@ -728,7 +733,7 @@ static void emit_static_asm(jitcompiler *jc) { } else { dasm_put(Dst, 1207); } -# 469 "upb/pb/compile_decoder_x64.dasc" +# 474 "upb/pb/compile_decoder_x64.dasc" /*| // OPT: do something faster than just calling the C version. */ /*| mov rdi, PTR */ /*| callp upb_vdecode_fast */ @@ -740,17 +745,17 @@ static void emit_static_asm(jitcompiler *jc) { /*| ret */ /*| */ dasm_put(Dst, 1223, (unsigned int)((uintptr_t)upb_vdecode_fast), (unsigned int)(((uintptr_t)upb_vdecode_fast)>>32), 0xfffffffffffffff0UL, Dt2(->ptr)); -# 479 "upb/pb/compile_decoder_x64.dasc" +# 484 "upb/pb/compile_decoder_x64.dasc" asmlabel(jc, "decode_varint_slow"); /*|->decode_varint_slow: */ /*| // Slow path: end of buffer or error (varint length >= 10). */ /*| getvalue_slow upb_pbdecoder_decode_varint_slow, 1 */ dasm_put(Dst, 1268, Dt2(->checkpoint), Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt2(->delim_end), Dt2(->buf), Dt2(->bufstart_ofs), Dt1(->end_ofs), Dt1(->sink.closure), (unsigned int)((uintptr_t)upb_pbdecoder_decode_varint_slow), (unsigned int)(((uintptr_t)upb_pbdecoder_decode_varint_slow)>>32), 0xfffffffffffffff0UL, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt1(->sink.closure), Dt1(->end_ofs), Dt2(->bufstart_ofs)); -# 483 "upb/pb/compile_decoder_x64.dasc" +# 488 "upb/pb/compile_decoder_x64.dasc" /*| */ /*| // Args: rsi=expected tag, return=rax (DECODE_{OK,MISMATCH}) */ dasm_put(Dst, 1374, Dt2(->buf), Dt2(->ptr)); -# 485 "upb/pb/compile_decoder_x64.dasc" +# 490 "upb/pb/compile_decoder_x64.dasc" asmlabel(jc, "checktag_fallback"); /*|->checktag_fallback: */ /*| sub rsp, 8 */ @@ -762,7 +767,7 @@ static void emit_static_asm(jitcompiler *jc) { /*| callp upb_pbdecoder_checktag_slow */ /*| load_regs */ dasm_put(Dst, 1418, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt2(->delim_end), Dt2(->buf), Dt2(->bufstart_ofs), Dt1(->end_ofs), Dt1(->sink.closure), Dt2(->checkpoint), (unsigned int)((uintptr_t)upb_pbdecoder_checktag_slow), (unsigned int)(((uintptr_t)upb_pbdecoder_checktag_slow)>>32), 0xfffffffffffffff0UL, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt1(->sink.closure), Dt1(->end_ofs), Dt2(->bufstart_ofs)); -# 495 "upb/pb/compile_decoder_x64.dasc" +# 500 "upb/pb/compile_decoder_x64.dasc" /*| cmp eax, 0 */ /*| jge >2 */ /*| add rsp, 8 */ @@ -780,7 +785,7 @@ static void emit_static_asm(jitcompiler *jc) { /*| // Preserves: rcx, rdx */ /*| // OPT: Could write this in assembly if it's a hotspot. */ dasm_put(Dst, 1517, Dt2(->buf), DECODE_EOF); -# 511 "upb/pb/compile_decoder_x64.dasc" +# 516 "upb/pb/compile_decoder_x64.dasc" asmlabel(jc, "hashlookup"); /*|->hashlookup: */ /*| push rcx */ @@ -802,7 +807,7 @@ static void emit_static_asm(jitcompiler *jc) { /*| not rax */ /*| ret */ dasm_put(Dst, 1559, (unsigned int)((uintptr_t)upb_inttable_lookup), (unsigned int)(((uintptr_t)upb_inttable_lookup)>>32), 0xfffffffffffffff0UL); -# 531 "upb/pb/compile_decoder_x64.dasc" +# 536 "upb/pb/compile_decoder_x64.dasc" } static void jitprimitive(jitcompiler *jc, opcode op, @@ -828,63 +833,63 @@ static void jitprimitive(jitcompiler *jc, opcode op, } else { dasm_put(Dst, 1636, fastbytes); } -# 550 "upb/pb/compile_decoder_x64.dasc" +# 555 "upb/pb/compile_decoder_x64.dasc" /*|2: */ dasm_put(Dst, 1652); -# 551 "upb/pb/compile_decoder_x64.dasc" +# 556 "upb/pb/compile_decoder_x64.dasc" switch (vtype) { case V32: /*| call ->decodev32_fallback */ dasm_put(Dst, 1655); -# 554 "upb/pb/compile_decoder_x64.dasc" +# 559 "upb/pb/compile_decoder_x64.dasc" break; case V64: /*| call ->decodev64_fallback */ dasm_put(Dst, 1659); -# 557 "upb/pb/compile_decoder_x64.dasc" +# 562 "upb/pb/compile_decoder_x64.dasc" break; case F32: /*| call ->decodef32_fallback */ dasm_put(Dst, 1663); -# 560 "upb/pb/compile_decoder_x64.dasc" +# 565 "upb/pb/compile_decoder_x64.dasc" break; case F64: /*| call ->decodef64_fallback */ dasm_put(Dst, 1667); -# 563 "upb/pb/compile_decoder_x64.dasc" +# 568 "upb/pb/compile_decoder_x64.dasc" break; case X: break; } /*| jmp >4 */ dasm_put(Dst, 1671); -# 567 "upb/pb/compile_decoder_x64.dasc" +# 572 "upb/pb/compile_decoder_x64.dasc" /* Fast path decode; for when check_bytes bytes are available. */ /*|3: */ dasm_put(Dst, 1676); -# 570 "upb/pb/compile_decoder_x64.dasc" +# 575 "upb/pb/compile_decoder_x64.dasc" switch (op) { case OP_PARSE_SFIXED32: case OP_PARSE_FIXED32: /*| mov edx, dword [PTR] */ dasm_put(Dst, 1679); -# 574 "upb/pb/compile_decoder_x64.dasc" +# 579 "upb/pb/compile_decoder_x64.dasc" break; case OP_PARSE_SFIXED64: case OP_PARSE_FIXED64: /*| mov rdx, qword [PTR] */ dasm_put(Dst, 1682); -# 578 "upb/pb/compile_decoder_x64.dasc" +# 583 "upb/pb/compile_decoder_x64.dasc" break; case OP_PARSE_FLOAT: /*| movss xmm0, dword [PTR] */ dasm_put(Dst, 1686); -# 581 "upb/pb/compile_decoder_x64.dasc" +# 586 "upb/pb/compile_decoder_x64.dasc" break; case OP_PARSE_DOUBLE: /*| movsd xmm0, qword [PTR] */ dasm_put(Dst, 1692); -# 584 "upb/pb/compile_decoder_x64.dasc" +# 589 "upb/pb/compile_decoder_x64.dasc" break; default: /* Inline one byte of varint decoding. */ @@ -892,7 +897,7 @@ static void jitprimitive(jitcompiler *jc, opcode op, /*| test dl, dl */ /*| js <2 // Fallback to slow path for >1 byte varint. */ dasm_put(Dst, 1698); -# 590 "upb/pb/compile_decoder_x64.dasc" +# 595 "upb/pb/compile_decoder_x64.dasc" break; } @@ -900,7 +905,7 @@ static void jitprimitive(jitcompiler *jc, opcode op, /* (only needed for a few types). */ /*|4: */ dasm_put(Dst, 1708); -# 596 "upb/pb/compile_decoder_x64.dasc" +# 601 "upb/pb/compile_decoder_x64.dasc" switch (op) { case OP_PARSE_SINT32: /* 32-bit zig-zag decode. */ @@ -910,7 +915,7 @@ static void jitprimitive(jitcompiler *jc, opcode op, /*| neg eax */ /*| xor edx, eax */ dasm_put(Dst, 1711); -# 604 "upb/pb/compile_decoder_x64.dasc" +# 609 "upb/pb/compile_decoder_x64.dasc" break; case OP_PARSE_SINT64: /* 64-bit zig-zag decode. */ @@ -920,13 +925,13 @@ static void jitprimitive(jitcompiler *jc, opcode op, /*| neg rax */ /*| xor rdx, rax */ dasm_put(Dst, 1725); -# 612 "upb/pb/compile_decoder_x64.dasc" +# 617 "upb/pb/compile_decoder_x64.dasc" break; case OP_PARSE_BOOL: /*| test rdx, rdx */ /*| setne dl */ dasm_put(Dst, 1744); -# 616 "upb/pb/compile_decoder_x64.dasc" +# 621 "upb/pb/compile_decoder_x64.dasc" break; default: break; } @@ -938,29 +943,29 @@ static void jitprimitive(jitcompiler *jc, opcode op, case UPB_TYPE_UINT64: /*| mov [CLOSURE + offset], rdx */ dasm_put(Dst, 1751, offset); -# 626 "upb/pb/compile_decoder_x64.dasc" +# 631 "upb/pb/compile_decoder_x64.dasc" break; case UPB_TYPE_INT32: case UPB_TYPE_UINT32: case UPB_TYPE_ENUM: /*| mov [CLOSURE + offset], edx */ dasm_put(Dst, 1756, offset); -# 631 "upb/pb/compile_decoder_x64.dasc" +# 636 "upb/pb/compile_decoder_x64.dasc" break; case UPB_TYPE_DOUBLE: /*| movsd qword [CLOSURE + offset], XMMARG1 */ dasm_put(Dst, 1761, offset); -# 634 "upb/pb/compile_decoder_x64.dasc" +# 639 "upb/pb/compile_decoder_x64.dasc" break; case UPB_TYPE_FLOAT: /*| movss dword [CLOSURE + offset], XMMARG1 */ dasm_put(Dst, 1769, offset); -# 637 "upb/pb/compile_decoder_x64.dasc" +# 642 "upb/pb/compile_decoder_x64.dasc" break; case UPB_TYPE_BOOL: /*| mov [CLOSURE + offset], dl */ dasm_put(Dst, 1777, offset); -# 640 "upb/pb/compile_decoder_x64.dasc" +# 645 "upb/pb/compile_decoder_x64.dasc" break; case UPB_TYPE_STRING: case UPB_TYPE_BYTES: @@ -971,13 +976,13 @@ static void jitprimitive(jitcompiler *jc, opcode op, if (hasbit >= 0) { dasm_put(Dst, 1782, ((uint32_t)hasbit / 8), (1 << ((uint32_t)hasbit % 8))); } -# 647 "upb/pb/compile_decoder_x64.dasc" +# 652 "upb/pb/compile_decoder_x64.dasc" } else if (handler) { /*| mov ARG1_64, CLOSURE */ /*| load_handler_data h, sel */ dasm_put(Dst, 1788); { - uintptr_t v = (uintptr_t)upb_handlers_gethandlerdata(h, sel); + uintptr_t v = (uintptr_t)gethandlerdata(h, sel); if (v > 0xffffffff) { dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); } else if (v) { @@ -986,10 +991,10 @@ static void jitprimitive(jitcompiler *jc, opcode op, dasm_put(Dst, 454); } } -# 650 "upb/pb/compile_decoder_x64.dasc" +# 655 "upb/pb/compile_decoder_x64.dasc" /*| callp handler */ dasm_put(Dst, 1793, (unsigned int)((uintptr_t)handler), (unsigned int)(((uintptr_t)handler)>>32), 0xfffffffffffffff0UL); -# 651 "upb/pb/compile_decoder_x64.dasc" +# 656 "upb/pb/compile_decoder_x64.dasc" if (!alwaysok(h, sel)) { /*| test al, al */ /*| jnz >5 */ @@ -997,7 +1002,7 @@ static void jitprimitive(jitcompiler *jc, opcode op, /*| jmp <1 */ /*|5: */ dasm_put(Dst, 1815); -# 657 "upb/pb/compile_decoder_x64.dasc" +# 662 "upb/pb/compile_decoder_x64.dasc" } } @@ -1005,7 +1010,7 @@ static void jitprimitive(jitcompiler *jc, opcode op, * data until the callback has returned success. */ /*| add PTR, fastbytes */ dasm_put(Dst, 1831, fastbytes); -# 663 "upb/pb/compile_decoder_x64.dasc" +# 668 "upb/pb/compile_decoder_x64.dasc" } else { /* No handler registered for this value, just skip it. */ /*| chkneob fastbytes, >3 */ @@ -1014,30 +1019,30 @@ static void jitprimitive(jitcompiler *jc, opcode op, } else { dasm_put(Dst, 1636, fastbytes); } -# 666 "upb/pb/compile_decoder_x64.dasc" +# 671 "upb/pb/compile_decoder_x64.dasc" /*|2: */ dasm_put(Dst, 1652); -# 667 "upb/pb/compile_decoder_x64.dasc" +# 672 "upb/pb/compile_decoder_x64.dasc" switch (vtype) { case V32: /*| call ->skipv32_fallback */ dasm_put(Dst, 1836); -# 670 "upb/pb/compile_decoder_x64.dasc" +# 675 "upb/pb/compile_decoder_x64.dasc" break; case V64: /*| call ->skipv64_fallback */ dasm_put(Dst, 1840); -# 673 "upb/pb/compile_decoder_x64.dasc" +# 678 "upb/pb/compile_decoder_x64.dasc" break; case F32: /*| call ->skipf32_fallback */ dasm_put(Dst, 1844); -# 676 "upb/pb/compile_decoder_x64.dasc" +# 681 "upb/pb/compile_decoder_x64.dasc" break; case F64: /*| call ->skipf64_fallback */ dasm_put(Dst, 1848); -# 679 "upb/pb/compile_decoder_x64.dasc" +# 684 "upb/pb/compile_decoder_x64.dasc" break; case X: break; } @@ -1045,16 +1050,16 @@ static void jitprimitive(jitcompiler *jc, opcode op, /* Fast-path skip. */ /*|3: */ dasm_put(Dst, 1676); -# 685 "upb/pb/compile_decoder_x64.dasc" +# 690 "upb/pb/compile_decoder_x64.dasc" if (vtype == V32 || vtype == V64) { /*| test byte [PTR], 0x80 */ /*| jnz <2 */ dasm_put(Dst, 1852); -# 688 "upb/pb/compile_decoder_x64.dasc" +# 693 "upb/pb/compile_decoder_x64.dasc" } /*| add PTR, fastbytes */ dasm_put(Dst, 1831, fastbytes); -# 690 "upb/pb/compile_decoder_x64.dasc" +# 695 "upb/pb/compile_decoder_x64.dasc" } } @@ -1075,7 +1080,7 @@ static void jitdispatch(jitcompiler *jc, /*|=>define_jmptarget(jc, &method->dispatch): */ /*|1: */ dasm_put(Dst, 1861, define_jmptarget(jc, &method->dispatch)); -# 709 "upb/pb/compile_decoder_x64.dasc" +# 714 "upb/pb/compile_decoder_x64.dasc" /* Decode the field tag. */ /*| mov aword DECODER->checkpoint, PTR */ /*| chkeob 2, >6 */ @@ -1085,7 +1090,7 @@ static void jitdispatch(jitcompiler *jc, } else { dasm_put(Dst, 1873); } -# 712 "upb/pb/compile_decoder_x64.dasc" +# 717 "upb/pb/compile_decoder_x64.dasc" /*| movzx edx, byte [PTR] */ /*| test dl, dl */ /*| jns >7 // Jump if first byte has no continuation bit. */ @@ -1110,48 +1115,48 @@ static void jitdispatch(jitcompiler *jc, /*| shr edx, 3 */ /*| and cl, 7 */ dasm_put(Dst, 1889, 1); -# 735 "upb/pb/compile_decoder_x64.dasc" +# 740 "upb/pb/compile_decoder_x64.dasc" /* See comment attached to upb_pbdecodermethod.dispatch for layout of the * dispatch table. */ /*|2: */ /*| cmp edx, dispatch->array_size */ dasm_put(Dst, 1954, dispatch->array_size); -# 740 "upb/pb/compile_decoder_x64.dasc" +# 745 "upb/pb/compile_decoder_x64.dasc" if (has_hash_entries) { /*| jae >7 */ dasm_put(Dst, 1961); -# 742 "upb/pb/compile_decoder_x64.dasc" +# 747 "upb/pb/compile_decoder_x64.dasc" } else { /*| jae >5 */ dasm_put(Dst, 1966); -# 744 "upb/pb/compile_decoder_x64.dasc" +# 749 "upb/pb/compile_decoder_x64.dasc" } /*| // OPT: Compact the lookup arr into 32-bit entries. */ if ((uintptr_t)dispatch->array > 0x7fffffff) { /*| mov64 rax, (uintptr_t)dispatch->array */ /*| mov rax, qword [rax + rdx * 8] */ dasm_put(Dst, 1971, (unsigned int)((uintptr_t)dispatch->array), (unsigned int)(((uintptr_t)dispatch->array)>>32)); -# 749 "upb/pb/compile_decoder_x64.dasc" +# 754 "upb/pb/compile_decoder_x64.dasc" } else { /*| mov rax, qword [rdx * 8 + dispatch->array] */ dasm_put(Dst, 1980, dispatch->array); -# 751 "upb/pb/compile_decoder_x64.dasc" +# 756 "upb/pb/compile_decoder_x64.dasc" } /*|3: */ /*| // We take advantage of the fact that non-present entries are stored */ /*| // as -1, which will result in wire types that will never match. */ /*| cmp al, cl */ dasm_put(Dst, 1986); -# 756 "upb/pb/compile_decoder_x64.dasc" +# 761 "upb/pb/compile_decoder_x64.dasc" if (has_multi_wiretype) { /*| jne >6 */ dasm_put(Dst, 1991); -# 758 "upb/pb/compile_decoder_x64.dasc" +# 763 "upb/pb/compile_decoder_x64.dasc" } else { /*| jne >5 */ dasm_put(Dst, 1996); -# 760 "upb/pb/compile_decoder_x64.dasc" +# 765 "upb/pb/compile_decoder_x64.dasc" } /*| shr rax, 16 */ /*| */ @@ -1182,7 +1187,7 @@ static void jitdispatch(jitcompiler *jc, /*| lea rax, [>9] // ENDGROUP; Load address of OP_ENDMSG. */ /*| ret */ dasm_put(Dst, 2001, define_jmptarget(jc, dispatch->array), (unsigned int)((uintptr_t)method->dest_handlers_), (unsigned int)(((uintptr_t)method->dest_handlers_)>>32), Dt1(->sink.handlers)); -# 789 "upb/pb/compile_decoder_x64.dasc" +# 794 "upb/pb/compile_decoder_x64.dasc" if (has_multi_wiretype) { /*|6: */ @@ -1193,7 +1198,7 @@ static void jitdispatch(jitcompiler *jc, /*| add rdx, UPB_MAX_FIELDNUMBER */ /*| // This key will never be in the array part, so do a hash lookup. */ dasm_put(Dst, 2043, UPB_MAX_FIELDNUMBER); -# 798 "upb/pb/compile_decoder_x64.dasc" +# 803 "upb/pb/compile_decoder_x64.dasc" UPB_ASSERT(has_hash_entries); /*| ld64 dispatch */ { @@ -1206,10 +1211,10 @@ static void jitdispatch(jitcompiler *jc, dasm_put(Dst, 454); } } -# 800 "upb/pb/compile_decoder_x64.dasc" +# 805 "upb/pb/compile_decoder_x64.dasc" /*| jmp ->hashlookup // Tail call. */ dasm_put(Dst, 2056); -# 801 "upb/pb/compile_decoder_x64.dasc" +# 806 "upb/pb/compile_decoder_x64.dasc" } if (has_hash_entries) { @@ -1227,11 +1232,11 @@ static void jitdispatch(jitcompiler *jc, dasm_put(Dst, 454); } } -# 807 "upb/pb/compile_decoder_x64.dasc" +# 812 "upb/pb/compile_decoder_x64.dasc" /*| call ->hashlookup */ /*| jmp <3 */ dasm_put(Dst, 2064); -# 809 "upb/pb/compile_decoder_x64.dasc" +# 814 "upb/pb/compile_decoder_x64.dasc" } } @@ -1258,7 +1263,7 @@ static void jittag(jitcompiler *jc, uint64_t tag, int n, int ofs, } else { dasm_put(Dst, 2080, n); } -# 830 "upb/pb/compile_decoder_x64.dasc" +# 835 "upb/pb/compile_decoder_x64.dasc" /*| // OPT: this is way too much fallback code to put here. */ /*| // Reduce and/or move to a separate section to make better icache usage. */ @@ -1273,7 +1278,7 @@ static void jittag(jitcompiler *jc, uint64_t tag, int n, int ofs, dasm_put(Dst, 454); } } -# 834 "upb/pb/compile_decoder_x64.dasc" +# 839 "upb/pb/compile_decoder_x64.dasc" /*| call ->checktag_fallback */ /*| cmp eax, DECODE_MISMATCH */ /*| je >3 */ @@ -1281,21 +1286,21 @@ static void jittag(jitcompiler *jc, uint64_t tag, int n, int ofs, /*| je =>jmptarget(jc, delimend) */ /*| jmp >5 */ dasm_put(Dst, 2096, DECODE_MISMATCH, DECODE_EOF, jmptarget(jc, delimend)); -# 840 "upb/pb/compile_decoder_x64.dasc" +# 845 "upb/pb/compile_decoder_x64.dasc" /*|1: */ dasm_put(Dst, 112); -# 842 "upb/pb/compile_decoder_x64.dasc" +# 847 "upb/pb/compile_decoder_x64.dasc" switch (n) { case 1: /*| cmp byte [PTR], tag */ dasm_put(Dst, 2119, tag); -# 845 "upb/pb/compile_decoder_x64.dasc" +# 850 "upb/pb/compile_decoder_x64.dasc" break; case 2: /*| cmp word [PTR], tag */ dasm_put(Dst, 2123, tag); -# 848 "upb/pb/compile_decoder_x64.dasc" +# 853 "upb/pb/compile_decoder_x64.dasc" break; case 3: /*| // OPT: Slightly more efficient code, but depends on an extra byte. */ @@ -1307,41 +1312,41 @@ static void jittag(jitcompiler *jc, uint64_t tag, int n, int ofs, /*| cmp byte [PTR + 2], (tag >> 16) */ /*|2: */ dasm_put(Dst, 2128, (tag & 0xffff), 2, (tag >> 16)); -# 858 "upb/pb/compile_decoder_x64.dasc" +# 863 "upb/pb/compile_decoder_x64.dasc" break; case 4: /*| cmp dword [PTR], tag */ dasm_put(Dst, 2143, tag); -# 861 "upb/pb/compile_decoder_x64.dasc" +# 866 "upb/pb/compile_decoder_x64.dasc" break; case 5: /*| cmp dword [PTR], (tag & 0xffffffff) */ /*| jne >3 */ /*| cmp byte [PTR + 4], (tag >> 32) */ dasm_put(Dst, 2147, (tag & 0xffffffff), 4, (tag >> 32)); -# 866 "upb/pb/compile_decoder_x64.dasc" +# 871 "upb/pb/compile_decoder_x64.dasc" } /*| je >4 */ /*|3: */ dasm_put(Dst, 2159); -# 869 "upb/pb/compile_decoder_x64.dasc" +# 874 "upb/pb/compile_decoder_x64.dasc" if (ofs == 0) { /*| call =>jmptarget(jc, &method->dispatch) */ /*| test rax, rax */ /*| jz =>jmptarget(jc, delimend) */ /*| jmp rax */ dasm_put(Dst, 2166, jmptarget(jc, &method->dispatch), jmptarget(jc, delimend)); -# 874 "upb/pb/compile_decoder_x64.dasc" +# 879 "upb/pb/compile_decoder_x64.dasc" } else { /*| jmp =>jmptarget(jc, jc->pc + ofs) */ dasm_put(Dst, 2178, jmptarget(jc, jc->pc + ofs)); -# 876 "upb/pb/compile_decoder_x64.dasc" +# 881 "upb/pb/compile_decoder_x64.dasc" } /*|4: */ /*| add PTR, n */ /*|5: */ dasm_put(Dst, 2182, n); -# 880 "upb/pb/compile_decoder_x64.dasc" +# 885 "upb/pb/compile_decoder_x64.dasc" } /* Compile the bytecode to x64. */ @@ -1364,7 +1369,7 @@ static void jitbytecode(jitcompiler *jc) { * TODO: optimize this to only define pclabels that are actually used. */ /*|=>define_jmptarget(jc, jc->pc): */ dasm_put(Dst, 0, define_jmptarget(jc, jc->pc)); -# 901 "upb/pb/compile_decoder_x64.dasc" +# 906 "upb/pb/compile_decoder_x64.dasc" } jc->pc++; @@ -1379,7 +1384,7 @@ static void jitbytecode(jitcompiler *jc) { /*| load_handler_data h, UPB_STARTMSG_SELECTOR */ dasm_put(Dst, 2191); { - uintptr_t v = (uintptr_t)upb_handlers_gethandlerdata(h, UPB_STARTMSG_SELECTOR); + uintptr_t v = (uintptr_t)gethandlerdata(h, UPB_STARTMSG_SELECTOR); if (v > 0xffffffff) { dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); } else if (v) { @@ -1388,10 +1393,10 @@ static void jitbytecode(jitcompiler *jc) { dasm_put(Dst, 454); } } -# 913 "upb/pb/compile_decoder_x64.dasc" +# 918 "upb/pb/compile_decoder_x64.dasc" /*| callp startmsg */ dasm_put(Dst, 1793, (unsigned int)((uintptr_t)startmsg), (unsigned int)(((uintptr_t)startmsg)>>32), 0xfffffffffffffff0UL); -# 914 "upb/pb/compile_decoder_x64.dasc" +# 919 "upb/pb/compile_decoder_x64.dasc" if (!alwaysok(h, UPB_STARTMSG_SELECTOR)) { /*| test al, al */ /*| jnz >2 */ @@ -1399,12 +1404,12 @@ static void jitbytecode(jitcompiler *jc) { /*| jmp <1 */ /*|2: */ dasm_put(Dst, 2198); -# 920 "upb/pb/compile_decoder_x64.dasc" +# 925 "upb/pb/compile_decoder_x64.dasc" } } else { /*| nop */ dasm_put(Dst, 2214); -# 923 "upb/pb/compile_decoder_x64.dasc" +# 928 "upb/pb/compile_decoder_x64.dasc" } break; } @@ -1412,14 +1417,14 @@ static void jitbytecode(jitcompiler *jc) { upb_func *endmsg = gethandler(h, UPB_ENDMSG_SELECTOR); /*|9: */ dasm_put(Dst, 2216); -# 929 "upb/pb/compile_decoder_x64.dasc" +# 934 "upb/pb/compile_decoder_x64.dasc" if (endmsg) { /* bool endmsg(void *closure, const void *hd, upb_status *status) */ /*| mov ARG1_64, CLOSURE */ /*| load_handler_data h, UPB_ENDMSG_SELECTOR */ dasm_put(Dst, 1788); { - uintptr_t v = (uintptr_t)upb_handlers_gethandlerdata(h, UPB_ENDMSG_SELECTOR); + uintptr_t v = (uintptr_t)gethandlerdata(h, UPB_ENDMSG_SELECTOR); if (v > 0xffffffff) { dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); } else if (v) { @@ -1428,11 +1433,11 @@ static void jitbytecode(jitcompiler *jc) { dasm_put(Dst, 454); } } -# 933 "upb/pb/compile_decoder_x64.dasc" +# 938 "upb/pb/compile_decoder_x64.dasc" /*| mov ARG3_64, DECODER->status */ /*| callp endmsg */ dasm_put(Dst, 2219, Dt2(->status), (unsigned int)((uintptr_t)endmsg), (unsigned int)(((uintptr_t)endmsg)>>32), 0xfffffffffffffff0UL); -# 935 "upb/pb/compile_decoder_x64.dasc" +# 940 "upb/pb/compile_decoder_x64.dasc" } break; } @@ -1465,7 +1470,7 @@ static void jitbytecode(jitcompiler *jc) { /*|=>define_jmptarget(jc, method): */ /*| sub rsp, 8 */ dasm_put(Dst, 2245, define_jmptarget(jc, op_pc), define_jmptarget(jc, method)); -# 966 "upb/pb/compile_decoder_x64.dasc" +# 971 "upb/pb/compile_decoder_x64.dasc" break; } @@ -1497,7 +1502,7 @@ static void jitbytecode(jitcompiler *jc) { /*| load_handler_data h, arg */ dasm_put(Dst, 2191); { - uintptr_t v = (uintptr_t)upb_handlers_gethandlerdata(h, arg); + uintptr_t v = (uintptr_t)gethandlerdata(h, arg); if (v > 0xffffffff) { dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); } else if (v) { @@ -1506,16 +1511,16 @@ static void jitbytecode(jitcompiler *jc) { dasm_put(Dst, 454); } } -# 995 "upb/pb/compile_decoder_x64.dasc" +# 1000 "upb/pb/compile_decoder_x64.dasc" if (op == OP_STARTSTR) { /*| mov ARG3_64, DELIMEND */ /*| sub ARG3_64, PTR */ dasm_put(Dst, 2253); -# 998 "upb/pb/compile_decoder_x64.dasc" +# 1003 "upb/pb/compile_decoder_x64.dasc" } /*| callp start */ dasm_put(Dst, 1793, (unsigned int)((uintptr_t)start), (unsigned int)(((uintptr_t)start)>>32), 0xfffffffffffffff0UL); -# 1000 "upb/pb/compile_decoder_x64.dasc" +# 1005 "upb/pb/compile_decoder_x64.dasc" if (!alwaysok(h, arg)) { /*| test rax, rax */ /*| jnz >2 */ @@ -1523,16 +1528,16 @@ static void jitbytecode(jitcompiler *jc) { /*| jmp <1 */ /*|2: */ dasm_put(Dst, 2261); -# 1006 "upb/pb/compile_decoder_x64.dasc" +# 1011 "upb/pb/compile_decoder_x64.dasc" } /*| mov CLOSURE, rax */ dasm_put(Dst, 2278); -# 1008 "upb/pb/compile_decoder_x64.dasc" +# 1013 "upb/pb/compile_decoder_x64.dasc" } else { /* TODO: nop is only required because of asmlabel(). */ /*| nop */ dasm_put(Dst, 2214); -# 1011 "upb/pb/compile_decoder_x64.dasc" +# 1016 "upb/pb/compile_decoder_x64.dasc" } break; } @@ -1549,7 +1554,7 @@ static void jitbytecode(jitcompiler *jc) { /*| load_handler_data h, arg */ dasm_put(Dst, 2191); { - uintptr_t v = (uintptr_t)upb_handlers_gethandlerdata(h, arg); + uintptr_t v = (uintptr_t)gethandlerdata(h, arg); if (v > 0xffffffff) { dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); } else if (v) { @@ -1558,10 +1563,10 @@ static void jitbytecode(jitcompiler *jc) { dasm_put(Dst, 454); } } -# 1025 "upb/pb/compile_decoder_x64.dasc" +# 1030 "upb/pb/compile_decoder_x64.dasc" /*| callp end */ dasm_put(Dst, 1793, (unsigned int)((uintptr_t)end), (unsigned int)(((uintptr_t)end)>>32), 0xfffffffffffffff0UL); -# 1026 "upb/pb/compile_decoder_x64.dasc" +# 1031 "upb/pb/compile_decoder_x64.dasc" if (!alwaysok(h, arg)) { /*| test al, al */ /*| jnz >2 */ @@ -1569,13 +1574,13 @@ static void jitbytecode(jitcompiler *jc) { /*| jmp <1 */ /*|2: */ dasm_put(Dst, 2198); -# 1032 "upb/pb/compile_decoder_x64.dasc" +# 1037 "upb/pb/compile_decoder_x64.dasc" } } else { /* TODO: nop is only required because of asmlabel(). */ /*| nop */ dasm_put(Dst, 2214); -# 1036 "upb/pb/compile_decoder_x64.dasc" +# 1041 "upb/pb/compile_decoder_x64.dasc" } break; } @@ -1590,7 +1595,7 @@ static void jitbytecode(jitcompiler *jc) { /*| jmp <1 */ /*|2: */ dasm_put(Dst, 2282); -# 1049 "upb/pb/compile_decoder_x64.dasc" +# 1054 "upb/pb/compile_decoder_x64.dasc" if (str) { /* size_t str(void *closure, const void *hd, const char *str, * size_t n) */ @@ -1598,7 +1603,7 @@ static void jitbytecode(jitcompiler *jc) { /*| load_handler_data h, arg */ dasm_put(Dst, 1788); { - uintptr_t v = (uintptr_t)upb_handlers_gethandlerdata(h, arg); + uintptr_t v = (uintptr_t)gethandlerdata(h, arg); if (v > 0xffffffff) { dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); } else if (v) { @@ -1607,7 +1612,7 @@ static void jitbytecode(jitcompiler *jc) { dasm_put(Dst, 454); } } -# 1054 "upb/pb/compile_decoder_x64.dasc" +# 1059 "upb/pb/compile_decoder_x64.dasc" /*| mov ARG3_64, PTR */ /*| mov ARG4_64, DATAEND */ /*| sub ARG4_64, PTR */ @@ -1615,25 +1620,25 @@ static void jitbytecode(jitcompiler *jc) { /*| callp str */ /*| add PTR, rax */ dasm_put(Dst, 2309, Dt2(->handle), (unsigned int)((uintptr_t)str), (unsigned int)(((uintptr_t)str)>>32), 0xfffffffffffffff0UL); -# 1060 "upb/pb/compile_decoder_x64.dasc" +# 1065 "upb/pb/compile_decoder_x64.dasc" if (!alwaysok(h, arg)) { /*| cmp PTR, DATAEND */ /*| je >3 */ /*| call ->strret_fallback */ /*|3: */ dasm_put(Dst, 2347); -# 1065 "upb/pb/compile_decoder_x64.dasc" +# 1070 "upb/pb/compile_decoder_x64.dasc" } } else { /*| mov PTR, DATAEND */ dasm_put(Dst, 2360); -# 1068 "upb/pb/compile_decoder_x64.dasc" +# 1073 "upb/pb/compile_decoder_x64.dasc" } /*| cmp PTR, DELIMEND */ /*| jne <1 */ /*|4: */ dasm_put(Dst, 2364); -# 1072 "upb/pb/compile_decoder_x64.dasc" +# 1077 "upb/pb/compile_decoder_x64.dasc" break; } case OP_PUSHTAGDELIM: @@ -1649,18 +1654,18 @@ static void jitbytecode(jitcompiler *jc) { /*| add FRAME, sizeof(upb_pbdecoder_frame) */ /*| mov dword FRAME->groupnum, arg */ dasm_put(Dst, 2375, Dt1(->sink.closure), Dt1(->end_ofs), Dt2(->limit), sizeof(upb_pbdecoder_frame), Dt1(->groupnum), arg); -# 1086 "upb/pb/compile_decoder_x64.dasc" +# 1091 "upb/pb/compile_decoder_x64.dasc" break; case OP_PUSHLENDELIM: /*| call ->pushlendelim */ dasm_put(Dst, 2405); -# 1089 "upb/pb/compile_decoder_x64.dasc" +# 1094 "upb/pb/compile_decoder_x64.dasc" break; case OP_POP: /*| sub FRAME, sizeof(upb_pbdecoder_frame) */ /*| mov CLOSURE, FRAME->sink.closure */ dasm_put(Dst, 2409, sizeof(upb_pbdecoder_frame), Dt1(->sink.closure)); -# 1093 "upb/pb/compile_decoder_x64.dasc" +# 1098 "upb/pb/compile_decoder_x64.dasc" break; case OP_SETDELIM: /* OPT: experiment with testing vs old offset to optimize away. */ @@ -1673,35 +1678,35 @@ static void jitbytecode(jitcompiler *jc) { /*| mov DATAEND, DELIMEND */ /*|1: */ dasm_put(Dst, 2419, Dt2(->end), Dt1(->end_ofs), Dt2(->buf)); -# 1104 "upb/pb/compile_decoder_x64.dasc" +# 1109 "upb/pb/compile_decoder_x64.dasc" break; case OP_SETBIGGROUPNUM: /*| mov dword FRAME->groupnum, *jc->pc++ */ dasm_put(Dst, 2399, Dt1(->groupnum), *jc->pc++); -# 1107 "upb/pb/compile_decoder_x64.dasc" +# 1112 "upb/pb/compile_decoder_x64.dasc" break; case OP_CHECKDELIM: /*| cmp DELIMEND, PTR */ /*| je =>jmptarget(jc, jc->pc + longofs) */ dasm_put(Dst, 2449, jmptarget(jc, jc->pc + longofs)); -# 1111 "upb/pb/compile_decoder_x64.dasc" +# 1116 "upb/pb/compile_decoder_x64.dasc" break; case OP_CALL: /*| call =>jmptarget(jc, jc->pc + longofs) */ dasm_put(Dst, 2456, jmptarget(jc, jc->pc + longofs)); -# 1114 "upb/pb/compile_decoder_x64.dasc" +# 1119 "upb/pb/compile_decoder_x64.dasc" break; case OP_BRANCH: /*| jmp =>jmptarget(jc, jc->pc + longofs); */ dasm_put(Dst, 2178, jmptarget(jc, jc->pc + longofs)); -# 1117 "upb/pb/compile_decoder_x64.dasc" +# 1122 "upb/pb/compile_decoder_x64.dasc" break; case OP_RET: /*|9: */ /*| add rsp, 8 */ /*| ret */ dasm_put(Dst, 2459); -# 1122 "upb/pb/compile_decoder_x64.dasc" +# 1127 "upb/pb/compile_decoder_x64.dasc" break; case OP_TAG1: jittag(jc, (arg >> 8) & 0xff, 1, (int8_t)arg, method); @@ -1718,7 +1723,7 @@ static void jitbytecode(jitcompiler *jc) { case OP_DISPATCH: /*| call =>jmptarget(jc, &method->dispatch) */ dasm_put(Dst, 2456, jmptarget(jc, &method->dispatch)); -# 1137 "upb/pb/compile_decoder_x64.dasc" +# 1142 "upb/pb/compile_decoder_x64.dasc" break; case OP_HALT: UPB_ASSERT(false); @@ -1728,5 +1733,5 @@ static void jitbytecode(jitcompiler *jc) { asmlabel(jc, "eof"); /*| nop */ dasm_put(Dst, 2214); -# 1145 "upb/pb/compile_decoder_x64.dasc" +# 1150 "upb/pb/compile_decoder_x64.dasc" } diff --git a/upb/upb.h b/upb/upb.h index 07b0455..1c13dd9 100644 --- a/upb/upb.h +++ b/upb/upb.h @@ -262,6 +262,7 @@ size_t upb_arena_bytesallocated(const upb_arena *a); UPB_INLINE upb_alloc *upb_arena_alloc(upb_arena *a) { return (upb_alloc*)a; } /* Convenience wrappers around upb_alloc functions. */ + UPB_INLINE void *upb_arena_malloc(upb_arena *a, size_t size) { return upb_malloc(upb_arena_alloc(a), size); } -- cgit v1.2.3 From f30c7f4acbe0457aa41597f034765d94eb39e739 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 15 Jan 2019 07:14:48 -0800 Subject: Take sinks by value. --- upb/json/parser.c | 80 +++++++++++----------- upb/json/parser.rl | 80 +++++++++++----------- upb/json/printer.c | 18 ++--- upb/pb/decoder.c | 22 +++--- upb/pb/encoder.c | 6 +- upb/pb/textprinter.c | 16 ++--- upb/sink.c | 6 +- upb/sink.h | 186 +++++++++++++++++++++++++-------------------------- 8 files changed, 207 insertions(+), 207 deletions(-) (limited to 'upb') diff --git a/upb/json/parser.c b/upb/json/parser.c index 5fe20ef..70ebfcf 100644 --- a/upb/json/parser.c +++ b/upb/json/parser.c @@ -487,7 +487,7 @@ static bool base64_push(upb_json_parser *p, upb_selector_t sel, const char *ptr, output[0] = val >> 16; output[1] = (val >> 8) & 0xff; output[2] = val & 0xff; - upb_sink_putstring(&p->top->sink, sel, output, 3, NULL); + upb_sink_putstring(p->top->sink, sel, output, 3, NULL); } return true; @@ -512,7 +512,7 @@ otherchar: UPB_ASSERT(!(val & 0x80000000)); output = val >> 16; - upb_sink_putstring(&p->top->sink, sel, &output, 1, NULL); + upb_sink_putstring(p->top->sink, sel, &output, 1, NULL); return true; } else { uint32_t val; @@ -529,7 +529,7 @@ otherchar: output[0] = val >> 16; output[1] = (val >> 8) & 0xff; - upb_sink_putstring(&p->top->sink, sel, output, 2, NULL); + upb_sink_putstring(p->top->sink, sel, output, 2, NULL); return true; } @@ -695,7 +695,7 @@ static bool multipart_text(upb_json_parser *p, const char *buf, size_t len, case MULTIPART_PUSHEAGERLY: { const upb_bufhandle *handle = can_alias ? p->handle : NULL; - upb_sink_putstring(&p->top->sink, p->string_selector, buf, len, handle); + upb_sink_putstring(p->top->sink, p->string_selector, buf, len, handle); break; } } @@ -941,7 +941,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, } else if (val > INT32_MAX || val < INT32_MIN) { return false; } else { - upb_sink_putint32(&p->top->sink, parser_getsel(p), val); + upb_sink_putint32(p->top->sink, parser_getsel(p), val); return true; } } @@ -952,7 +952,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, } else if (val > UINT32_MAX || errno == ERANGE) { return false; } else { - upb_sink_putuint32(&p->top->sink, parser_getsel(p), val); + upb_sink_putuint32(p->top->sink, parser_getsel(p), val); return true; } } @@ -963,7 +963,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, if (errno == ERANGE || end != bufend) { break; } else { - upb_sink_putint64(&p->top->sink, parser_getsel(p), val); + upb_sink_putint64(p->top->sink, parser_getsel(p), val); return true; } } @@ -974,7 +974,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, } else if (errno == ERANGE) { return false; } else { - upb_sink_putuint64(&p->top->sink, parser_getsel(p), val); + upb_sink_putuint64(p->top->sink, parser_getsel(p), val); return true; } } @@ -1005,7 +1005,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, if (modf(val, &dummy) != 0 || val > max || val < min) { \ return false; \ } else { \ - upb_sink_put ## smalltype(&p->top->sink, parser_getsel(p), \ + upb_sink_put ## smalltype(p->top->sink, parser_getsel(p), \ (ctype)val); \ return true; \ } \ @@ -1019,13 +1019,13 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, #undef CASE case UPB_TYPE_DOUBLE: - upb_sink_putdouble(&p->top->sink, parser_getsel(p), val); + upb_sink_putdouble(p->top->sink, parser_getsel(p), val); return true; case UPB_TYPE_FLOAT: if ((val > FLT_MAX || val < -FLT_MAX) && val != inf && val != -inf) { return false; } else { - upb_sink_putfloat(&p->top->sink, parser_getsel(p), val); + upb_sink_putfloat(p->top->sink, parser_getsel(p), val); return true; } default: @@ -1069,7 +1069,7 @@ static bool parser_putbool(upb_json_parser *p, bool val) { return false; } - ok = upb_sink_putbool(&p->top->sink, parser_getsel(p), val); + ok = upb_sink_putbool(p->top->sink, parser_getsel(p), val); UPB_ASSERT(ok); return true; @@ -1209,7 +1209,7 @@ static bool start_stringval(upb_json_parser *p) { * handler frames, and string events occur in a sub-frame. */ inner = p->top + 1; sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); - upb_sink_startstr(&p->top->sink, sel, 0, &inner->sink); + upb_sink_startstr(p->top->sink, sel, 0, &inner->sink); inner->m = p->top->m; inner->f = p->top->f; inner->name_table = NULL; @@ -1258,11 +1258,11 @@ static bool end_any_stringval(upb_json_parser *p) { inner = p->top + 1; sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); - upb_sink_startstr(&p->top->sink, sel, 0, &inner->sink); + upb_sink_startstr(p->top->sink, sel, 0, &inner->sink); sel = getsel_for_handlertype(p, UPB_HANDLER_STRING); - upb_sink_putstring(&inner->sink, sel, buf, len, NULL); + upb_sink_putstring(inner->sink, sel, buf, len, NULL); sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); - upb_sink_endstr(&inner->sink, sel); + upb_sink_endstr(inner->sink, sel); multipart_end(p); @@ -1318,7 +1318,7 @@ static bool end_stringval_nontop(upb_json_parser *p) { case UPB_TYPE_STRING: { upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); p->top--; - upb_sink_endstr(&p->top->sink, sel); + upb_sink_endstr(p->top->sink, sel); break; } @@ -1334,7 +1334,7 @@ static bool end_stringval_nontop(upb_json_parser *p) { if (ok) { upb_selector_t sel = parser_getsel(p); - upb_sink_putint32(&p->top->sink, sel, int_val); + upb_sink_putint32(p->top->sink, sel, int_val); } else { upb_status_seterrf(p->status, "Enum value unknown: '%.*s'", len, buf); } @@ -1472,7 +1472,7 @@ static bool end_duration_base(upb_json_parser *p, const char *ptr) { capture_begin(p, seconds_membername); capture_end(p, seconds_membername + 7); end_membername(p); - upb_sink_putint64(&p->top->sink, parser_getsel(p), seconds); + upb_sink_putint64(p->top->sink, parser_getsel(p), seconds); end_member(p); /* Set nanos */ @@ -1480,7 +1480,7 @@ static bool end_duration_base(upb_json_parser *p, const char *ptr) { capture_begin(p, nanos_membername); capture_end(p, nanos_membername + 5); end_membername(p); - upb_sink_putint32(&p->top->sink, parser_getsel(p), nanos); + upb_sink_putint32(p->top->sink, parser_getsel(p), nanos); end_member(p); /* Continue previous arena */ @@ -1572,7 +1572,7 @@ static bool end_timestamp_fraction(upb_json_parser *p, const char *ptr) { capture_begin(p, nanos_membername); capture_end(p, nanos_membername + 5); end_membername(p); - upb_sink_putint32(&p->top->sink, parser_getsel(p), nanos); + upb_sink_putint32(p->top->sink, parser_getsel(p), nanos); end_member(p); /* Continue previous environment */ @@ -1630,7 +1630,7 @@ static bool end_timestamp_zone(upb_json_parser *p, const char *ptr) { capture_begin(p, seconds_membername); capture_end(p, seconds_membername + 7); end_membername(p); - upb_sink_putint64(&p->top->sink, parser_getsel(p), seconds); + upb_sink_putint64(p->top->sink, parser_getsel(p), seconds); end_member(p); /* Continue previous environment */ @@ -1692,11 +1692,11 @@ static bool parse_mapentry_key(upb_json_parser *p) { case UPB_TYPE_BYTES: { upb_sink subsink; upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); - upb_sink_startstr(&p->top->sink, sel, len, &subsink); + upb_sink_startstr(p->top->sink, sel, len, &subsink); sel = getsel_for_handlertype(p, UPB_HANDLER_STRING); - upb_sink_putstring(&subsink, sel, buf, len, NULL); + upb_sink_putstring(subsink, sel, buf, len, NULL); sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); - upb_sink_endstr(&p->top->sink, sel); + upb_sink_endstr(p->top->sink, sel); multipart_end(p); break; } @@ -1734,7 +1734,7 @@ static bool handle_mapentry(upb_json_parser *p) { inner = p->top + 1; p->top->f = mapfield; sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSUBMSG); - upb_sink_startsubmsg(&p->top->sink, sel, &inner->sink); + upb_sink_startsubmsg(p->top->sink, sel, &inner->sink); inner->m = mapentrymsg; inner->name_table = NULL; inner->mapfield = mapfield; @@ -1751,7 +1751,7 @@ static bool handle_mapentry(upb_json_parser *p) { p->top = inner; /* send STARTMSG in submsg frame. */ - upb_sink_startmsg(&p->top->sink); + upb_sink_startmsg(p->top->sink); parse_mapentry_key(p); @@ -1827,14 +1827,14 @@ static void end_member(upb_json_parser *p) { UPB_ASSERT(p->top > p->stack); /* send ENDMSG on submsg. */ - upb_sink_endmsg(&p->top->sink, p->status); + upb_sink_endmsg(p->top->sink, p->status); mapfield = p->top->mapfield; /* send ENDSUBMSG in repeated-field-of-mapentries frame. */ p->top--; ok = upb_handlers_getselector(mapfield, UPB_HANDLER_ENDSUBMSG, &sel); UPB_ASSERT(ok); - upb_sink_endsubmsg(&p->top->sink, sel); + upb_sink_endsubmsg(p->top->sink, sel); } p->top->f = NULL; @@ -1878,7 +1878,7 @@ static bool start_subobject(upb_json_parser *p) { inner = p->top + 1; sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSEQ); - upb_sink_startseq(&p->top->sink, sel, &inner->sink); + upb_sink_startseq(p->top->sink, sel, &inner->sink); inner->m = upb_fielddef_msgsubdef(p->top->f); inner->name_table = NULL; inner->mapfield = p->top->f; @@ -1902,7 +1902,7 @@ static bool start_subobject(upb_json_parser *p) { inner = p->top + 1; sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSUBMSG); - upb_sink_startsubmsg(&p->top->sink, sel, &inner->sink); + upb_sink_startsubmsg(p->top->sink, sel, &inner->sink); inner->m = upb_fielddef_msgsubdef(p->top->f); set_name_table(p, inner); inner->f = NULL; @@ -1961,14 +1961,14 @@ static void end_subobject(upb_json_parser *p) { upb_selector_t sel; p->top--; sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSEQ); - upb_sink_endseq(&p->top->sink, sel); + upb_sink_endseq(p->top->sink, sel); } else { upb_selector_t sel; bool is_unknown = p->top->m == NULL; p->top--; if (!is_unknown) { sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSUBMSG); - upb_sink_endsubmsg(&p->top->sink, sel); + upb_sink_endsubmsg(p->top->sink, sel); } } } @@ -2041,7 +2041,7 @@ static bool start_array(upb_json_parser *p) { inner = p->top + 1; sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSEQ); - upb_sink_startseq(&p->top->sink, sel, &inner->sink); + upb_sink_startseq(p->top->sink, sel, &inner->sink); inner->m = p->top->m; inner->name_table = NULL; inner->f = p->top->f; @@ -2067,7 +2067,7 @@ static void end_array(upb_json_parser *p) { } sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSEQ); - upb_sink_endseq(&p->top->sink, sel); + upb_sink_endseq(p->top->sink, sel); if (is_wellknown_msg(p, UPB_WELLKNOWN_LISTVALUE)) { end_listvalue_object(p); @@ -2086,13 +2086,13 @@ static void end_array(upb_json_parser *p) { static void start_object(upb_json_parser *p) { if (!p->top->is_map && p->top->m != NULL) { - upb_sink_startmsg(&p->top->sink); + upb_sink_startmsg(p->top->sink); } } static void end_object(upb_json_parser *p) { if (!p->top->is_map && p->top->m != NULL) { - upb_sink_endmsg(&p->top->sink, p->status); + upb_sink_endmsg(p->top->sink, p->status); } } @@ -2198,12 +2198,12 @@ static bool end_any_object(upb_json_parser *p, const char *ptr) { inner = p->top + 1; sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); - upb_sink_startstr(&p->top->sink, sel, 0, &inner->sink); + upb_sink_startstr(p->top->sink, sel, 0, &inner->sink); sel = getsel_for_handlertype(p, UPB_HANDLER_STRING); - upb_sink_putstring(&inner->sink, sel, p->top->any_frame->stringsink.ptr, + upb_sink_putstring(inner->sink, sel, p->top->any_frame->stringsink.ptr, p->top->any_frame->stringsink.len, NULL); sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); - upb_sink_endstr(&inner->sink, sel); + upb_sink_endstr(inner->sink, sel); end_member(p); diff --git a/upb/json/parser.rl b/upb/json/parser.rl index 6f27630..11a6955 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -485,7 +485,7 @@ static bool base64_push(upb_json_parser *p, upb_selector_t sel, const char *ptr, output[0] = val >> 16; output[1] = (val >> 8) & 0xff; output[2] = val & 0xff; - upb_sink_putstring(&p->top->sink, sel, output, 3, NULL); + upb_sink_putstring(p->top->sink, sel, output, 3, NULL); } return true; @@ -510,7 +510,7 @@ otherchar: UPB_ASSERT(!(val & 0x80000000)); output = val >> 16; - upb_sink_putstring(&p->top->sink, sel, &output, 1, NULL); + upb_sink_putstring(p->top->sink, sel, &output, 1, NULL); return true; } else { uint32_t val; @@ -527,7 +527,7 @@ otherchar: output[0] = val >> 16; output[1] = (val >> 8) & 0xff; - upb_sink_putstring(&p->top->sink, sel, output, 2, NULL); + upb_sink_putstring(p->top->sink, sel, output, 2, NULL); return true; } @@ -693,7 +693,7 @@ static bool multipart_text(upb_json_parser *p, const char *buf, size_t len, case MULTIPART_PUSHEAGERLY: { const upb_bufhandle *handle = can_alias ? p->handle : NULL; - upb_sink_putstring(&p->top->sink, p->string_selector, buf, len, handle); + upb_sink_putstring(p->top->sink, p->string_selector, buf, len, handle); break; } } @@ -939,7 +939,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, } else if (val > INT32_MAX || val < INT32_MIN) { return false; } else { - upb_sink_putint32(&p->top->sink, parser_getsel(p), val); + upb_sink_putint32(p->top->sink, parser_getsel(p), val); return true; } } @@ -950,7 +950,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, } else if (val > UINT32_MAX || errno == ERANGE) { return false; } else { - upb_sink_putuint32(&p->top->sink, parser_getsel(p), val); + upb_sink_putuint32(p->top->sink, parser_getsel(p), val); return true; } } @@ -961,7 +961,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, if (errno == ERANGE || end != bufend) { break; } else { - upb_sink_putint64(&p->top->sink, parser_getsel(p), val); + upb_sink_putint64(p->top->sink, parser_getsel(p), val); return true; } } @@ -972,7 +972,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, } else if (errno == ERANGE) { return false; } else { - upb_sink_putuint64(&p->top->sink, parser_getsel(p), val); + upb_sink_putuint64(p->top->sink, parser_getsel(p), val); return true; } } @@ -1003,7 +1003,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, if (modf(val, &dummy) != 0 || val > max || val < min) { \ return false; \ } else { \ - upb_sink_put ## smalltype(&p->top->sink, parser_getsel(p), \ + upb_sink_put ## smalltype(p->top->sink, parser_getsel(p), \ (ctype)val); \ return true; \ } \ @@ -1017,13 +1017,13 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, #undef CASE case UPB_TYPE_DOUBLE: - upb_sink_putdouble(&p->top->sink, parser_getsel(p), val); + upb_sink_putdouble(p->top->sink, parser_getsel(p), val); return true; case UPB_TYPE_FLOAT: if ((val > FLT_MAX || val < -FLT_MAX) && val != inf && val != -inf) { return false; } else { - upb_sink_putfloat(&p->top->sink, parser_getsel(p), val); + upb_sink_putfloat(p->top->sink, parser_getsel(p), val); return true; } default: @@ -1067,7 +1067,7 @@ static bool parser_putbool(upb_json_parser *p, bool val) { return false; } - ok = upb_sink_putbool(&p->top->sink, parser_getsel(p), val); + ok = upb_sink_putbool(p->top->sink, parser_getsel(p), val); UPB_ASSERT(ok); return true; @@ -1207,7 +1207,7 @@ static bool start_stringval(upb_json_parser *p) { * handler frames, and string events occur in a sub-frame. */ inner = p->top + 1; sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); - upb_sink_startstr(&p->top->sink, sel, 0, &inner->sink); + upb_sink_startstr(p->top->sink, sel, 0, &inner->sink); inner->m = p->top->m; inner->f = p->top->f; inner->name_table = NULL; @@ -1256,11 +1256,11 @@ static bool end_any_stringval(upb_json_parser *p) { inner = p->top + 1; sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); - upb_sink_startstr(&p->top->sink, sel, 0, &inner->sink); + upb_sink_startstr(p->top->sink, sel, 0, &inner->sink); sel = getsel_for_handlertype(p, UPB_HANDLER_STRING); - upb_sink_putstring(&inner->sink, sel, buf, len, NULL); + upb_sink_putstring(inner->sink, sel, buf, len, NULL); sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); - upb_sink_endstr(&inner->sink, sel); + upb_sink_endstr(inner->sink, sel); multipart_end(p); @@ -1316,7 +1316,7 @@ static bool end_stringval_nontop(upb_json_parser *p) { case UPB_TYPE_STRING: { upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); p->top--; - upb_sink_endstr(&p->top->sink, sel); + upb_sink_endstr(p->top->sink, sel); break; } @@ -1332,7 +1332,7 @@ static bool end_stringval_nontop(upb_json_parser *p) { if (ok) { upb_selector_t sel = parser_getsel(p); - upb_sink_putint32(&p->top->sink, sel, int_val); + upb_sink_putint32(p->top->sink, sel, int_val); } else { upb_status_seterrf(p->status, "Enum value unknown: '%.*s'", len, buf); } @@ -1470,7 +1470,7 @@ static bool end_duration_base(upb_json_parser *p, const char *ptr) { capture_begin(p, seconds_membername); capture_end(p, seconds_membername + 7); end_membername(p); - upb_sink_putint64(&p->top->sink, parser_getsel(p), seconds); + upb_sink_putint64(p->top->sink, parser_getsel(p), seconds); end_member(p); /* Set nanos */ @@ -1478,7 +1478,7 @@ static bool end_duration_base(upb_json_parser *p, const char *ptr) { capture_begin(p, nanos_membername); capture_end(p, nanos_membername + 5); end_membername(p); - upb_sink_putint32(&p->top->sink, parser_getsel(p), nanos); + upb_sink_putint32(p->top->sink, parser_getsel(p), nanos); end_member(p); /* Continue previous arena */ @@ -1570,7 +1570,7 @@ static bool end_timestamp_fraction(upb_json_parser *p, const char *ptr) { capture_begin(p, nanos_membername); capture_end(p, nanos_membername + 5); end_membername(p); - upb_sink_putint32(&p->top->sink, parser_getsel(p), nanos); + upb_sink_putint32(p->top->sink, parser_getsel(p), nanos); end_member(p); /* Continue previous environment */ @@ -1628,7 +1628,7 @@ static bool end_timestamp_zone(upb_json_parser *p, const char *ptr) { capture_begin(p, seconds_membername); capture_end(p, seconds_membername + 7); end_membername(p); - upb_sink_putint64(&p->top->sink, parser_getsel(p), seconds); + upb_sink_putint64(p->top->sink, parser_getsel(p), seconds); end_member(p); /* Continue previous environment */ @@ -1690,11 +1690,11 @@ static bool parse_mapentry_key(upb_json_parser *p) { case UPB_TYPE_BYTES: { upb_sink subsink; upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); - upb_sink_startstr(&p->top->sink, sel, len, &subsink); + upb_sink_startstr(p->top->sink, sel, len, &subsink); sel = getsel_for_handlertype(p, UPB_HANDLER_STRING); - upb_sink_putstring(&subsink, sel, buf, len, NULL); + upb_sink_putstring(subsink, sel, buf, len, NULL); sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); - upb_sink_endstr(&p->top->sink, sel); + upb_sink_endstr(p->top->sink, sel); multipart_end(p); break; } @@ -1732,7 +1732,7 @@ static bool handle_mapentry(upb_json_parser *p) { inner = p->top + 1; p->top->f = mapfield; sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSUBMSG); - upb_sink_startsubmsg(&p->top->sink, sel, &inner->sink); + upb_sink_startsubmsg(p->top->sink, sel, &inner->sink); inner->m = mapentrymsg; inner->name_table = NULL; inner->mapfield = mapfield; @@ -1749,7 +1749,7 @@ static bool handle_mapentry(upb_json_parser *p) { p->top = inner; /* send STARTMSG in submsg frame. */ - upb_sink_startmsg(&p->top->sink); + upb_sink_startmsg(p->top->sink); parse_mapentry_key(p); @@ -1825,14 +1825,14 @@ static void end_member(upb_json_parser *p) { UPB_ASSERT(p->top > p->stack); /* send ENDMSG on submsg. */ - upb_sink_endmsg(&p->top->sink, p->status); + upb_sink_endmsg(p->top->sink, p->status); mapfield = p->top->mapfield; /* send ENDSUBMSG in repeated-field-of-mapentries frame. */ p->top--; ok = upb_handlers_getselector(mapfield, UPB_HANDLER_ENDSUBMSG, &sel); UPB_ASSERT(ok); - upb_sink_endsubmsg(&p->top->sink, sel); + upb_sink_endsubmsg(p->top->sink, sel); } p->top->f = NULL; @@ -1876,7 +1876,7 @@ static bool start_subobject(upb_json_parser *p) { inner = p->top + 1; sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSEQ); - upb_sink_startseq(&p->top->sink, sel, &inner->sink); + upb_sink_startseq(p->top->sink, sel, &inner->sink); inner->m = upb_fielddef_msgsubdef(p->top->f); inner->name_table = NULL; inner->mapfield = p->top->f; @@ -1900,7 +1900,7 @@ static bool start_subobject(upb_json_parser *p) { inner = p->top + 1; sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSUBMSG); - upb_sink_startsubmsg(&p->top->sink, sel, &inner->sink); + upb_sink_startsubmsg(p->top->sink, sel, &inner->sink); inner->m = upb_fielddef_msgsubdef(p->top->f); set_name_table(p, inner); inner->f = NULL; @@ -1959,14 +1959,14 @@ static void end_subobject(upb_json_parser *p) { upb_selector_t sel; p->top--; sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSEQ); - upb_sink_endseq(&p->top->sink, sel); + upb_sink_endseq(p->top->sink, sel); } else { upb_selector_t sel; bool is_unknown = p->top->m == NULL; p->top--; if (!is_unknown) { sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSUBMSG); - upb_sink_endsubmsg(&p->top->sink, sel); + upb_sink_endsubmsg(p->top->sink, sel); } } } @@ -2039,7 +2039,7 @@ static bool start_array(upb_json_parser *p) { inner = p->top + 1; sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSEQ); - upb_sink_startseq(&p->top->sink, sel, &inner->sink); + upb_sink_startseq(p->top->sink, sel, &inner->sink); inner->m = p->top->m; inner->name_table = NULL; inner->f = p->top->f; @@ -2065,7 +2065,7 @@ static void end_array(upb_json_parser *p) { } sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSEQ); - upb_sink_endseq(&p->top->sink, sel); + upb_sink_endseq(p->top->sink, sel); if (is_wellknown_msg(p, UPB_WELLKNOWN_LISTVALUE)) { end_listvalue_object(p); @@ -2084,13 +2084,13 @@ static void end_array(upb_json_parser *p) { static void start_object(upb_json_parser *p) { if (!p->top->is_map && p->top->m != NULL) { - upb_sink_startmsg(&p->top->sink); + upb_sink_startmsg(p->top->sink); } } static void end_object(upb_json_parser *p) { if (!p->top->is_map && p->top->m != NULL) { - upb_sink_endmsg(&p->top->sink, p->status); + upb_sink_endmsg(p->top->sink, p->status); } } @@ -2196,12 +2196,12 @@ static bool end_any_object(upb_json_parser *p, const char *ptr) { inner = p->top + 1; sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); - upb_sink_startstr(&p->top->sink, sel, 0, &inner->sink); + upb_sink_startstr(p->top->sink, sel, 0, &inner->sink); sel = getsel_for_handlertype(p, UPB_HANDLER_STRING); - upb_sink_putstring(&inner->sink, sel, p->top->any_frame->stringsink.ptr, + upb_sink_putstring(inner->sink, sel, p->top->any_frame->stringsink.ptr, p->top->any_frame->stringsink.len, NULL); sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); - upb_sink_endstr(&inner->sink, sel); + upb_sink_endstr(inner->sink, sel); end_member(p); diff --git a/upb/json/printer.c b/upb/json/printer.c index bc18055..2bc978a 100644 --- a/upb/json/printer.c +++ b/upb/json/printer.c @@ -87,7 +87,7 @@ strpc *newstrpc_str(upb_handlers *h, const char * str) { static void print_data( upb_json_printer *p, const char *buf, unsigned int len) { /* TODO: Will need to change if we support pushback from the sink. */ - size_t n = upb_bytessink_putbuf(&p->output_, p->subc_, buf, len, NULL); + size_t n = upb_bytessink_putbuf(p->output_, p->subc_, buf, len, NULL); UPB_ASSERT(n == len); } @@ -369,7 +369,7 @@ static bool printer_startmsg(void *closure, const void *handler_data) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); if (p->depth_ == 0) { - upb_bytessink_start(&p->output_, 0, &p->subc_); + upb_bytessink_start(p->output_, 0, &p->subc_); } start_frame(p); return true; @@ -381,7 +381,7 @@ static bool printer_endmsg(void *closure, const void *handler_data, upb_status * UPB_UNUSED(s); end_frame(p); if (p->depth_ == 0) { - upb_bytessink_end(&p->output_); + upb_bytessink_end(p->output_); } return true; } @@ -770,7 +770,7 @@ static bool printer_startdurationmsg(void *closure, const void *handler_data) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); if (p->depth_ == 0) { - upb_bytessink_start(&p->output_, 0, &p->subc_); + upb_bytessink_start(p->output_, 0, &p->subc_); } return true; } @@ -828,7 +828,7 @@ static bool printer_enddurationmsg(void *closure, const void *handler_data, print_data(p, "\"", 1); if (p->depth_ == 0) { - upb_bytessink_end(&p->output_); + upb_bytessink_end(p->output_); } UPB_UNUSED(handler_data); @@ -839,7 +839,7 @@ static bool printer_starttimestampmsg(void *closure, const void *handler_data) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); if (p->depth_ == 0) { - upb_bytessink_start(&p->output_, 0, &p->subc_); + upb_bytessink_start(p->output_, 0, &p->subc_); } return true; } @@ -902,7 +902,7 @@ static bool printer_endtimestampmsg(void *closure, const void *handler_data, print_data(p, "\"", 1); if (p->depth_ == 0) { - upb_bytessink_end(&p->output_); + upb_bytessink_end(p->output_); } UPB_UNUSED(handler_data); @@ -914,7 +914,7 @@ static bool printer_startmsg_noframe(void *closure, const void *handler_data) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); if (p->depth_ == 0) { - upb_bytessink_start(&p->output_, 0, &p->subc_); + upb_bytessink_start(p->output_, 0, &p->subc_); } return true; } @@ -925,7 +925,7 @@ static bool printer_endmsg_noframe( UPB_UNUSED(handler_data); UPB_UNUSED(s); if (p->depth_ == 0) { - upb_bytessink_end(&p->output_); + upb_bytessink_end(p->output_); } return true; } diff --git a/upb/pb/decoder.c b/upb/pb/decoder.c index f1617db..2384312 100644 --- a/upb/pb/decoder.c +++ b/upb/pb/decoder.c @@ -594,7 +594,7 @@ have_tag: if (d->top->groupnum >= 0) { /* TODO: More code needed for handling unknown groups. */ - upb_sink_putunknown(&d->top->sink, d->checkpoint, d->ptr - d->checkpoint); + upb_sink_putunknown(d->top->sink, d->checkpoint, d->ptr - d->checkpoint); return DECODE_OK; } @@ -688,7 +688,7 @@ size_t run_decoder_vm(upb_pbdecoder *d, const mgroup *group, VMCASE(OP_PARSE_ ## type, { \ ctype val; \ CHECK_RETURN(decode_ ## wt(d, &val)); \ - upb_sink_put ## name(&d->top->sink, arg, (convfunc)(val)); \ + upb_sink_put ## name(d->top->sink, arg, (convfunc)(val)); \ }) while(1) { @@ -740,36 +740,36 @@ size_t run_decoder_vm(upb_pbdecoder *d, const mgroup *group, d->pc += sizeof(void*) / sizeof(uint32_t); ) VMCASE(OP_STARTMSG, - CHECK_SUSPEND(upb_sink_startmsg(&d->top->sink)); + CHECK_SUSPEND(upb_sink_startmsg(d->top->sink)); ) VMCASE(OP_ENDMSG, - CHECK_SUSPEND(upb_sink_endmsg(&d->top->sink, d->status)); + CHECK_SUSPEND(upb_sink_endmsg(d->top->sink, d->status)); ) VMCASE(OP_STARTSEQ, upb_pbdecoder_frame *outer = outer_frame(d); - CHECK_SUSPEND(upb_sink_startseq(&outer->sink, arg, &d->top->sink)); + CHECK_SUSPEND(upb_sink_startseq(outer->sink, arg, &d->top->sink)); ) VMCASE(OP_ENDSEQ, - CHECK_SUSPEND(upb_sink_endseq(&d->top->sink, arg)); + CHECK_SUSPEND(upb_sink_endseq(d->top->sink, arg)); ) VMCASE(OP_STARTSUBMSG, upb_pbdecoder_frame *outer = outer_frame(d); - CHECK_SUSPEND(upb_sink_startsubmsg(&outer->sink, arg, &d->top->sink)); + CHECK_SUSPEND(upb_sink_startsubmsg(outer->sink, arg, &d->top->sink)); ) VMCASE(OP_ENDSUBMSG, - CHECK_SUSPEND(upb_sink_endsubmsg(&d->top->sink, arg)); + CHECK_SUSPEND(upb_sink_endsubmsg(d->top->sink, arg)); ) VMCASE(OP_STARTSTR, uint32_t len = delim_remaining(d); upb_pbdecoder_frame *outer = outer_frame(d); - CHECK_SUSPEND(upb_sink_startstr(&outer->sink, arg, len, &d->top->sink)); + CHECK_SUSPEND(upb_sink_startstr(outer->sink, arg, len, &d->top->sink)); if (len == 0) { d->pc++; /* Skip OP_STRING. */ } ) VMCASE(OP_STRING, uint32_t len = curbufleft(d); - size_t n = upb_sink_putstring(&d->top->sink, arg, d->ptr, len, handle); + size_t n = upb_sink_putstring(d->top->sink, arg, d->ptr, len, handle); if (n > len) { if (n > delim_remaining(d)) { seterr(d, "Tried to skip past end of string."); @@ -790,7 +790,7 @@ size_t run_decoder_vm(upb_pbdecoder *d, const mgroup *group, } ) VMCASE(OP_ENDSTR, - CHECK_SUSPEND(upb_sink_endstr(&d->top->sink, arg)); + CHECK_SUSPEND(upb_sink_endstr(d->top->sink, arg)); ) VMCASE(OP_PUSHTAGDELIM, CHECK_SUSPEND(pushtagdelim(d, arg)); diff --git a/upb/pb/encoder.c b/upb/pb/encoder.c index 722cc5b..2da34db 100644 --- a/upb/pb/encoder.c +++ b/upb/pb/encoder.c @@ -127,7 +127,7 @@ struct upb_pb_encoder { /* TODO(haberman): handle pushback */ static void putbuf(upb_pb_encoder *e, const char *buf, size_t len) { - size_t n = upb_bytessink_putbuf(&e->output_, e->subc, buf, len, NULL); + size_t n = upb_bytessink_putbuf(e->output_, e->subc, buf, len, NULL); UPB_ASSERT(n == len); } @@ -353,7 +353,7 @@ static bool startmsg(void *c, const void *hd) { upb_pb_encoder *e = c; UPB_UNUSED(hd); if (e->depth++ == 0) { - upb_bytessink_start(&e->output_, 0, &e->subc); + upb_bytessink_start(e->output_, 0, &e->subc); } return true; } @@ -363,7 +363,7 @@ static bool endmsg(void *c, const void *hd, upb_status *status) { UPB_UNUSED(hd); UPB_UNUSED(status); if (--e->depth == 0) { - upb_bytessink_end(&e->output_); + upb_bytessink_end(e->output_); } return true; } diff --git a/upb/pb/textprinter.c b/upb/pb/textprinter.c index 91d0d55..934130e 100644 --- a/upb/pb/textprinter.c +++ b/upb/pb/textprinter.c @@ -35,13 +35,13 @@ static int indent(upb_textprinter *p) { int i; if (!p->single_line_) for (i = 0; i < p->indent_depth_; i++) - upb_bytessink_putbuf(&p->output_, p->subc, " ", 2, NULL); + upb_bytessink_putbuf(p->output_, p->subc, " ", 2, NULL); return 0; } static int endfield(upb_textprinter *p) { const char ch = (p->single_line_ ? ' ' : '\n'); - upb_bytessink_putbuf(&p->output_, p->subc, &ch, 1, NULL); + upb_bytessink_putbuf(p->output_, p->subc, &ch, 1, NULL); return 0; } @@ -60,7 +60,7 @@ static int putescaped(upb_textprinter *p, const char *buf, size_t len, bool is_hex_escape; if (dstend - dst < 4) { - upb_bytessink_putbuf(&p->output_, p->subc, dstbuf, dst - dstbuf, NULL); + upb_bytessink_putbuf(p->output_, p->subc, dstbuf, dst - dstbuf, NULL); dst = dstbuf; } @@ -88,7 +88,7 @@ static int putescaped(upb_textprinter *p, const char *buf, size_t len, last_hex_escape = is_hex_escape; } /* Flush remaining data. */ - upb_bytessink_putbuf(&p->output_, p->subc, dstbuf, dst - dstbuf, NULL); + upb_bytessink_putbuf(p->output_, p->subc, dstbuf, dst - dstbuf, NULL); return 0; } @@ -114,7 +114,7 @@ bool putf(upb_textprinter *p, const char *fmt, ...) { va_end(args); UPB_ASSERT(written == len); - ok = upb_bytessink_putbuf(&p->output_, p->subc, str, len, NULL); + ok = upb_bytessink_putbuf(p->output_, p->subc, str, len, NULL); upb_gfree(str); return ok; } @@ -126,7 +126,7 @@ static bool textprinter_startmsg(void *c, const void *hd) { upb_textprinter *p = c; UPB_UNUSED(hd); if (p->indent_depth_ == 0) { - upb_bytessink_start(&p->output_, 0, &p->subc); + upb_bytessink_start(p->output_, 0, &p->subc); } return true; } @@ -136,7 +136,7 @@ static bool textprinter_endmsg(void *c, const void *hd, upb_status *s) { UPB_UNUSED(hd); UPB_UNUSED(s); if (p->indent_depth_ == 0) { - upb_bytessink_end(&p->output_); + upb_bytessink_end(p->output_); } return true; } @@ -241,7 +241,7 @@ static bool textprinter_endsubmsg(void *closure, const void *handler_data) { UPB_UNUSED(handler_data); p->indent_depth_--; CHECK(indent(p)); - upb_bytessink_putbuf(&p->output_, p->subc, "}", 1, NULL); + upb_bytessink_putbuf(p->output_, p->subc, "}", 1, NULL); CHECK(endfield(p)); return true; err: diff --git a/upb/sink.c b/upb/sink.c index 7a7eeb4..d55d258 100644 --- a/upb/sink.c +++ b/upb/sink.c @@ -6,12 +6,12 @@ bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink sink) { bool ret; upb_bufhandle handle = UPB_BUFHANDLE_INIT; handle.buf = buf; - ret = upb_bytessink_start(&sink, len, &subc); + ret = upb_bytessink_start(sink, len, &subc); if (ret && len != 0) { - ret = (upb_bytessink_putbuf(&sink, subc, buf, len, &handle) >= len); + ret = (upb_bytessink_putbuf(sink, subc, buf, len, &handle) >= len); } if (ret) { - ret = upb_bytessink_end(&sink); + ret = upb_bytessink_end(sink); } return ret; } diff --git a/upb/sink.h b/upb/sink.h index d93d966..e41feae 100644 --- a/upb/sink.h +++ b/upb/sink.h @@ -38,16 +38,16 @@ typedef struct { void *closure; } upb_sink; -#define PUTVAL(type, ctype) \ - UPB_INLINE bool upb_sink_put##type(upb_sink *s, upb_selector_t sel, \ - ctype val) { \ - typedef upb_##type##_handlerfunc functype; \ - functype *func; \ - const void *hd; \ - if (!s->handlers) return true; \ - func = (functype *)upb_handlers_gethandler(s->handlers, sel, &hd); \ - if (!func) return true; \ - return func(s->closure, hd, val); \ +#define PUTVAL(type, ctype) \ + UPB_INLINE bool upb_sink_put##type(upb_sink s, upb_selector_t sel, \ + ctype val) { \ + typedef upb_##type##_handlerfunc functype; \ + functype *func; \ + const void *hd; \ + if (!s.handlers) return true; \ + func = (functype *)upb_handlers_gethandler(s.handlers, sel, &hd); \ + if (!func) return true; \ + return func(s.closure, hd, val); \ } PUTVAL(int32, int32_t) @@ -64,134 +64,134 @@ UPB_INLINE void upb_sink_reset(upb_sink *s, const upb_handlers *h, void *c) { s->closure = c; } -UPB_INLINE size_t upb_sink_putstring(upb_sink *s, upb_selector_t sel, +UPB_INLINE size_t upb_sink_putstring(upb_sink s, upb_selector_t sel, const char *buf, size_t n, const upb_bufhandle *handle) { typedef upb_string_handlerfunc func; func *handler; const void *hd; - if (!s->handlers) return n; - handler = (func *)upb_handlers_gethandler(s->handlers, sel, &hd); + if (!s.handlers) return n; + handler = (func *)upb_handlers_gethandler(s.handlers, sel, &hd); if (!handler) return n; - return handler(s->closure, hd, buf, n, handle); + return handler(s.closure, hd, buf, n, handle); } -UPB_INLINE bool upb_sink_putunknown(upb_sink *s, const char *buf, size_t n) { +UPB_INLINE bool upb_sink_putunknown(upb_sink s, const char *buf, size_t n) { typedef upb_unknown_handlerfunc func; func *handler; const void *hd; - if (!s->handlers) return true; + if (!s.handlers) return true; handler = - (func *)upb_handlers_gethandler(s->handlers, UPB_UNKNOWN_SELECTOR, &hd); + (func *)upb_handlers_gethandler(s.handlers, UPB_UNKNOWN_SELECTOR, &hd); if (!handler) return n; - return handler(s->closure, hd, buf, n); + return handler(s.closure, hd, buf, n); } -UPB_INLINE bool upb_sink_startmsg(upb_sink *s) { +UPB_INLINE bool upb_sink_startmsg(upb_sink s) { typedef upb_startmsg_handlerfunc func; func *startmsg; const void *hd; - if (!s->handlers) return true; + if (!s.handlers) return true; startmsg = - (func *)upb_handlers_gethandler(s->handlers, UPB_STARTMSG_SELECTOR, &hd); + (func *)upb_handlers_gethandler(s.handlers, UPB_STARTMSG_SELECTOR, &hd); if (!startmsg) return true; - return startmsg(s->closure, hd); + return startmsg(s.closure, hd); } -UPB_INLINE bool upb_sink_endmsg(upb_sink *s, upb_status *status) { +UPB_INLINE bool upb_sink_endmsg(upb_sink s, upb_status *status) { typedef upb_endmsg_handlerfunc func; func *endmsg; const void *hd; - if (!s->handlers) return true; + if (!s.handlers) return true; endmsg = - (func *)upb_handlers_gethandler(s->handlers, UPB_ENDMSG_SELECTOR, &hd); + (func *)upb_handlers_gethandler(s.handlers, UPB_ENDMSG_SELECTOR, &hd); if (!endmsg) return true; - return endmsg(s->closure, hd, status); + return endmsg(s.closure, hd, status); } -UPB_INLINE bool upb_sink_startseq(upb_sink *s, upb_selector_t sel, +UPB_INLINE bool upb_sink_startseq(upb_sink s, upb_selector_t sel, upb_sink *sub) { typedef upb_startfield_handlerfunc func; func *startseq; const void *hd; - sub->closure = s->closure; - sub->handlers = s->handlers; - if (!s->handlers) return true; - startseq = (func*)upb_handlers_gethandler(s->handlers, sel, &hd); + sub->closure = s.closure; + sub->handlers = s.handlers; + if (!s.handlers) return true; + startseq = (func*)upb_handlers_gethandler(s.handlers, sel, &hd); if (!startseq) return true; - sub->closure = startseq(s->closure, hd); + sub->closure = startseq(s.closure, hd); return sub->closure ? true : false; } -UPB_INLINE bool upb_sink_endseq(upb_sink *s, upb_selector_t sel) { +UPB_INLINE bool upb_sink_endseq(upb_sink s, upb_selector_t sel) { typedef upb_endfield_handlerfunc func; func *endseq; const void *hd; - if (!s->handlers) return true; - endseq = (func*)upb_handlers_gethandler(s->handlers, sel, &hd); + if (!s.handlers) return true; + endseq = (func*)upb_handlers_gethandler(s.handlers, sel, &hd); if (!endseq) return true; - return endseq(s->closure, hd); + return endseq(s.closure, hd); } -UPB_INLINE bool upb_sink_startstr(upb_sink *s, upb_selector_t sel, +UPB_INLINE bool upb_sink_startstr(upb_sink s, upb_selector_t sel, size_t size_hint, upb_sink *sub) { typedef upb_startstr_handlerfunc func; func *startstr; const void *hd; - sub->closure = s->closure; - sub->handlers = s->handlers; - if (!s->handlers) return true; - startstr = (func*)upb_handlers_gethandler(s->handlers, sel, &hd); + sub->closure = s.closure; + sub->handlers = s.handlers; + if (!s.handlers) return true; + startstr = (func*)upb_handlers_gethandler(s.handlers, sel, &hd); if (!startstr) return true; - sub->closure = startstr(s->closure, hd, size_hint); + sub->closure = startstr(s.closure, hd, size_hint); return sub->closure ? true : false; } -UPB_INLINE bool upb_sink_endstr(upb_sink *s, upb_selector_t sel) { +UPB_INLINE bool upb_sink_endstr(upb_sink s, upb_selector_t sel) { typedef upb_endfield_handlerfunc func; func *endstr; const void *hd; - if (!s->handlers) return true; - endstr = (func*)upb_handlers_gethandler(s->handlers, sel, &hd); + if (!s.handlers) return true; + endstr = (func*)upb_handlers_gethandler(s.handlers, sel, &hd); if (!endstr) return true; - return endstr(s->closure, hd); + return endstr(s.closure, hd); } -UPB_INLINE bool upb_sink_startsubmsg(upb_sink *s, upb_selector_t sel, +UPB_INLINE bool upb_sink_startsubmsg(upb_sink s, upb_selector_t sel, upb_sink *sub) { typedef upb_startfield_handlerfunc func; func *startsubmsg; const void *hd; - sub->closure = s->closure; - if (!s->handlers) { + sub->closure = s.closure; + if (!s.handlers) { sub->handlers = NULL; return true; } - sub->handlers = upb_handlers_getsubhandlers_sel(s->handlers, sel); - startsubmsg = (func*)upb_handlers_gethandler(s->handlers, sel, &hd); + sub->handlers = upb_handlers_getsubhandlers_sel(s.handlers, sel); + startsubmsg = (func*)upb_handlers_gethandler(s.handlers, sel, &hd); if (!startsubmsg) return true; - sub->closure = startsubmsg(s->closure, hd); + sub->closure = startsubmsg(s.closure, hd); return sub->closure ? true : false; } -UPB_INLINE bool upb_sink_endsubmsg(upb_sink *s, upb_selector_t sel) { +UPB_INLINE bool upb_sink_endsubmsg(upb_sink s, upb_selector_t sel) { typedef upb_endfield_handlerfunc func; func *endsubmsg; const void *hd; - if (!s->handlers) return true; - endsubmsg = (func*)upb_handlers_gethandler(s->handlers, sel, &hd); + if (!s.handlers) return true; + endsubmsg = (func*)upb_handlers_gethandler(s.handlers, sel, &hd); - if (!endsubmsg) return s->closure; - return endsubmsg(s->closure, hd); + if (!endsubmsg) return s.closure; + return endsubmsg(s.closure, hd); } #ifdef __cplusplus @@ -288,40 +288,40 @@ class upb::Sink { * // ... * sink->EndMessage(&status); * sink->EndSubMessage(endsubmsg_selector); */ - bool StartMessage() { return upb_sink_startmsg(&sink_); } + bool StartMessage() { return upb_sink_startmsg(sink_); } bool EndMessage(upb_status *status) { - return upb_sink_endmsg(&sink_, status); + return upb_sink_endmsg(sink_, status); } /* Putting of individual values. These work for both repeated and * non-repeated fields, but for repeated fields you must wrap them in * calls to StartSequence()/EndSequence(). */ bool PutInt32(HandlersPtr::Selector s, int32_t val) { - return upb_sink_putint32(&sink_, s, val); + return upb_sink_putint32(sink_, s, val); } bool PutInt64(HandlersPtr::Selector s, int64_t val) { - return upb_sink_putint64(&sink_, s, val); + return upb_sink_putint64(sink_, s, val); } bool PutUInt32(HandlersPtr::Selector s, uint32_t val) { - return upb_sink_putuint32(&sink_, s, val); + return upb_sink_putuint32(sink_, s, val); } bool PutUInt64(HandlersPtr::Selector s, uint64_t val) { - return upb_sink_putuint64(&sink_, s, val); + return upb_sink_putuint64(sink_, s, val); } bool PutFloat(HandlersPtr::Selector s, float val) { - return upb_sink_putfloat(&sink_, s, val); + return upb_sink_putfloat(sink_, s, val); } bool PutDouble(HandlersPtr::Selector s, double val) { - return upb_sink_putdouble(&sink_, s, val); + return upb_sink_putdouble(sink_, s, val); } bool PutBool(HandlersPtr::Selector s, bool val) { - return upb_sink_putbool(&sink_, s, val); + return upb_sink_putbool(sink_, s, val); } /* Putting of string/bytes values. Each string can consist of zero or more @@ -331,18 +331,18 @@ class upb::Sink { * The sub-sink must be used for any/all PutStringBuffer() calls. */ bool StartString(HandlersPtr::Selector s, size_t size_hint, Sink* sub) { upb_sink sub_c; - bool ret = upb_sink_startstr(&sink_, s, size_hint, &sub_c); + bool ret = upb_sink_startstr(sink_, s, size_hint, &sub_c); *sub = sub_c; return ret; } size_t PutStringBuffer(HandlersPtr::Selector s, const char *buf, size_t len, const upb_bufhandle *handle) { - return upb_sink_putstring(&sink_, s, buf, len, handle); + return upb_sink_putstring(sink_, s, buf, len, handle); } bool EndString(HandlersPtr::Selector s) { - return upb_sink_endstr(&sink_, s); + return upb_sink_endstr(sink_, s); } /* For submessage fields. @@ -352,13 +352,13 @@ class upb::Sink { * submessage. */ bool StartSubMessage(HandlersPtr::Selector s, Sink* sub) { upb_sink sub_c; - bool ret = upb_sink_startsubmsg(&sink_, s, &sub_c); + bool ret = upb_sink_startsubmsg(sink_, s, &sub_c); *sub = sub_c; return ret; } bool EndSubMessage(HandlersPtr::Selector s) { - return upb_sink_endsubmsg(&sink_, s); + return upb_sink_endsubmsg(sink_, s); } /* For repeated fields of any type, the sequence of values must be wrapped in @@ -369,13 +369,13 @@ class upb::Sink { * sequence. */ bool StartSequence(HandlersPtr::Selector s, Sink* sub) { upb_sink sub_c; - bool ret = upb_sink_startseq(&sink_, s, &sub_c); + bool ret = upb_sink_startseq(sink_, s, &sub_c); *sub = sub_c; return ret; } bool EndSequence(HandlersPtr::Selector s) { - return upb_sink_endseq(&sink_, s); + return upb_sink_endseq(sink_, s); } /* Copy and assign specifically allowed. @@ -396,49 +396,49 @@ typedef struct { void *closure; } upb_bytessink ; -UPB_INLINE void upb_bytessink_reset(upb_bytessink *s, const upb_byteshandler *h, +UPB_INLINE void upb_bytessink_reset(upb_bytessink* s, const upb_byteshandler *h, void *closure) { s->handler = h; s->closure = closure; } -UPB_INLINE bool upb_bytessink_start(upb_bytessink *s, size_t size_hint, +UPB_INLINE bool upb_bytessink_start(upb_bytessink s, size_t size_hint, void **subc) { typedef upb_startstr_handlerfunc func; func *start; - *subc = s->closure; - if (!s->handler) return true; - start = (func *)s->handler->table[UPB_STARTSTR_SELECTOR].func; + *subc = s.closure; + if (!s.handler) return true; + start = (func *)s.handler->table[UPB_STARTSTR_SELECTOR].func; if (!start) return true; - *subc = start(s->closure, - s->handler->table[UPB_STARTSTR_SELECTOR].attr.handler_data, + *subc = start(s.closure, + s.handler->table[UPB_STARTSTR_SELECTOR].attr.handler_data, size_hint); return *subc != NULL; } -UPB_INLINE size_t upb_bytessink_putbuf(upb_bytessink *s, void *subc, +UPB_INLINE size_t upb_bytessink_putbuf(upb_bytessink s, void *subc, const char *buf, size_t size, const upb_bufhandle* handle) { typedef upb_string_handlerfunc func; func *putbuf; - if (!s->handler) return true; - putbuf = (func *)s->handler->table[UPB_STRING_SELECTOR].func; + if (!s.handler) return true; + putbuf = (func *)s.handler->table[UPB_STRING_SELECTOR].func; if (!putbuf) return true; - return putbuf(subc, s->handler->table[UPB_STRING_SELECTOR].attr.handler_data, + return putbuf(subc, s.handler->table[UPB_STRING_SELECTOR].attr.handler_data, buf, size, handle); } -UPB_INLINE bool upb_bytessink_end(upb_bytessink *s) { +UPB_INLINE bool upb_bytessink_end(upb_bytessink s) { typedef upb_endfield_handlerfunc func; func *end; - if (!s->handler) return true; - end = (func *)s->handler->table[UPB_ENDSTR_SELECTOR].func; + if (!s.handler) return true; + end = (func *)s.handler->table[UPB_ENDSTR_SELECTOR].func; if (!end) return true; - return end(s->closure, - s->handler->table[UPB_ENDSTR_SELECTOR].attr.handler_data); + return end(s.closure, + s.handler->table[UPB_ENDSTR_SELECTOR].attr.handler_data); } #ifdef __cplusplus @@ -463,7 +463,7 @@ class upb::BytesSink { * TODO(haberman): once the Handlers know the expected closure type, verify * that T matches it. */ template BytesSink(const upb_byteshandler* handler, T* closure) { - upb_bytessink_reset(&sink_, handler, closure); + upb_bytessink_reset(sink_, handler, closure); } /* Resets the value of the sink. */ @@ -472,16 +472,16 @@ class upb::BytesSink { } bool Start(size_t size_hint, void **subc) { - return upb_bytessink_start(&sink_, size_hint, subc); + return upb_bytessink_start(sink_, size_hint, subc); } size_t PutBuffer(void *subc, const char *buf, size_t len, const upb_bufhandle *handle) { - return upb_bytessink_putbuf(&sink_, subc, buf, len, handle); + return upb_bytessink_putbuf(sink_, subc, buf, len, handle); } bool End() { - return upb_bytessink_end(&sink_); + return upb_bytessink_end(sink_); } private: -- cgit v1.2.3 From 9349b703a33c76b0d50a15c6d372e8948e045749 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 15 Jan 2019 13:35:39 -0800 Subject: Fixed a few bugs surfaced by Ruby. --- upb/def.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'upb') diff --git a/upb/def.c b/upb/def.c index ae2306f..aedd88d 100644 --- a/upb/def.c +++ b/upb/def.c @@ -552,7 +552,7 @@ uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f) { bool upb_fielddef_defaultbool(const upb_fielddef *f) { chkdefaulttype(f, UPB_TYPE_BOOL); - return f->defaultval.uint; + return f->defaultval.boolean; } float upb_fielddef_defaultfloat(const upb_fielddef *f) { @@ -1094,7 +1094,7 @@ static bool parse_default(const symtab_addctx *ctx, const char *str, size_t len, /* XXX: Need to write our own strtof, since it's not available in c89. */ float val = strtod(str, &end); CHK(errno != ERANGE && !*end); - f->defaultval.dbl = val; + f->defaultval.flt = val; break; } case UPB_TYPE_BOOL: { @@ -1305,7 +1305,7 @@ static bool create_enumdef( int32_t num = google_protobuf_EnumValueDescriptorProto_number(value); upb_value v = upb_value_int32(num); - if (n == 0 && e->file->syntax == UPB_SYNTAX_PROTO3 && num != 0) { + if (i == 0 && e->file->syntax == UPB_SYNTAX_PROTO3 && num != 0) { upb_status_seterrf(ctx->status, "for proto3, the first enum value must be zero (%s)", e->full_name); -- cgit v1.2.3 From 2c26f60dbbc49bca6233cc20a15ff4b32454c6e8 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Tue, 15 Jan 2019 14:48:09 -0800 Subject: Added some comments and reversed upb_arena_cleanup() args. --- upb/handlers.c | 21 +-------------------- upb/handlers.h | 4 ++++ upb/json/parser.h | 3 +++ upb/json/printer.h | 2 ++ upb/pb/decoder.h | 3 +++ upb/pb/encoder.h | 2 ++ upb/upb.c | 2 +- upb/upb.h | 6 +++--- 8 files changed, 19 insertions(+), 24 deletions(-) (limited to 'upb') diff --git a/upb/handlers.c b/upb/handlers.c index aa23b46..7abf948 100644 --- a/upb/handlers.c +++ b/upb/handlers.c @@ -392,7 +392,6 @@ uint32_t upb_handlers_selectorcount(const upb_fielddef *f) { struct upb_handlercache { upb_arena *arena; upb_inttable tab; /* maps upb_msgdef* -> upb_handlers*. */ - upb_inttable cleanup_; upb_handlers_callback *callback; const void *closure; }; @@ -448,7 +447,6 @@ upb_handlercache *upb_handlercache_new(upb_handlers_callback *callback, cache->closure = closure; if (!upb_inttable_init(&cache->tab, UPB_CTYPE_PTR)) goto oom; - if (!upb_inttable_init(&cache->cleanup_, UPB_CTYPE_FPTR)) goto oom; return cache; @@ -458,31 +456,14 @@ oom: } void upb_handlercache_free(upb_handlercache *cache) { - upb_inttable_iter i; - - upb_inttable_begin(&i, &cache->cleanup_); - for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { - void *val = (void*)upb_inttable_iter_key(&i); - upb_value func_val = upb_inttable_iter_value(&i); - upb_handlerfree *func = upb_value_getfptr(func_val); - func(val); - } - upb_inttable_uninit(&cache->tab); - upb_inttable_uninit(&cache->cleanup_); upb_arena_free(cache->arena); upb_gfree(cache); } bool upb_handlercache_addcleanup(upb_handlercache *c, void *p, upb_handlerfree *func) { - bool ok; - if (upb_inttable_lookupptr(&c->cleanup_, p, NULL)) { - return false; - } - ok = upb_inttable_insertptr(&c->cleanup_, p, upb_value_fptr(func)); - UPB_ASSERT(ok); - return true; + return upb_arena_addcleanup(c->arena, p, func); } /* upb_byteshandler ***********************************************************/ diff --git a/upb/handlers.h b/upb/handlers.h index 764e83e..e267f12 100644 --- a/upb/handlers.h +++ b/upb/handlers.h @@ -594,6 +594,10 @@ class upb::HandlersPtr { /* upb_handlercache ***********************************************************/ +/* A upb_handlercache lazily builds and caches upb_handlers. You pass it a + * function (with optional closure) that can build handlers for a given + * message on-demand, and the cache maintains a map of msgdef->handlers. */ + #ifdef __cplusplus extern "C" { #endif diff --git a/upb/json/parser.h b/upb/json/parser.h index 6f3eaa7..c063a77 100644 --- a/upb/json/parser.h +++ b/upb/json/parser.h @@ -103,6 +103,9 @@ class upb::json::ParserPtr { /* upb_json_codecache *********************************************************/ +/* Lazily builds and caches decoder methods that will push data to the given + * handlers. The upb_symtab object(s) must outlive this object. */ + struct upb_json_codecache; typedef struct upb_json_codecache upb_json_codecache; diff --git a/upb/json/printer.h b/upb/json/printer.h index 857ae47..85b9b12 100644 --- a/upb/json/printer.h +++ b/upb/json/printer.h @@ -36,6 +36,8 @@ const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md, bool preserve_fieldnames, const void *owner); +/* Lazily builds and caches handlers that will push encoded data to a bytessink. + * Any msgdef objects used with this object must outlive it. */ upb_handlercache *upb_json_printer_newcache(bool preserve_proto_fieldnames); #ifdef __cplusplus diff --git a/upb/pb/decoder.h b/upb/pb/decoder.h index 5adfba8..86e51df 100644 --- a/upb/pb/decoder.h +++ b/upb/pb/decoder.h @@ -171,6 +171,9 @@ class upb::pb::DecoderPtr { /* upb_pbcodecache ************************************************************/ +/* Lazily builds and caches decoder methods that will push data to the given + * handlers. The destination handlercache must outlive this object. */ + struct upb_pbcodecache; typedef struct upb_pbcodecache upb_pbcodecache; diff --git a/upb/pb/encoder.h b/upb/pb/encoder.h index 780f60f..1113c3a 100644 --- a/upb/pb/encoder.h +++ b/upb/pb/encoder.h @@ -43,6 +43,8 @@ upb_sink upb_pb_encoder_input(upb_pb_encoder *p); upb_pb_encoder* upb_pb_encoder_create(upb_arena* a, const upb_handlers* h, upb_bytessink output); +/* Lazily builds and caches handlers that will push encoded data to a bytessink. + * Any msgdef objects used with this object must outlive it. */ upb_handlercache *upb_pb_encoder_newcache(); #ifdef __cplusplus diff --git a/upb/upb.c b/upb/upb.c index f56f6c4..d8d2723 100644 --- a/upb/upb.c +++ b/upb/upb.c @@ -231,7 +231,7 @@ void upb_arena_free(upb_arena *a) { } } -bool upb_arena_addcleanup(upb_arena *a, upb_cleanup_func *func, void *ud) { +bool upb_arena_addcleanup(upb_arena *a, void *ud, upb_cleanup_func *func) { cleanup_ent *ent = upb_malloc(&a->alloc, sizeof(cleanup_ent)); if (!ent) { return false; /* Out of memory. */ diff --git a/upb/upb.h b/upb/upb.h index 1c13dd9..fc83902 100644 --- a/upb/upb.h +++ b/upb/upb.h @@ -256,7 +256,7 @@ extern "C" { * is a fixed-size arena and cannot grow. */ upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc); void upb_arena_free(upb_arena *a); -bool upb_arena_addcleanup(upb_arena *a, upb_cleanup_func *func, void *ud); +bool upb_arena_addcleanup(upb_arena *a, void *ud, upb_cleanup_func *func); size_t upb_arena_bytesallocated(const upb_arena *a); UPB_INLINE upb_alloc *upb_arena_alloc(upb_arena *a) { return (upb_alloc*)a; } @@ -295,8 +295,8 @@ class upb::Arena { /* Add a cleanup function to run when the arena is destroyed. * Returns false on out-of-memory. */ - bool AddCleanup(upb_cleanup_func *func, void *ud) { - return upb_arena_addcleanup(ptr_.get(), func, ud); + bool AddCleanup(void *ud, upb_cleanup_func* func) { + return upb_arena_addcleanup(ptr_.get(), ud, func); } /* Total number of bytes that have been allocated. It is undefined what -- cgit v1.2.3 From 84fb01ad0f7301b416e03d97fbffef1a7512e7ea Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 16 Jan 2019 15:53:13 -0800 Subject: Removed the JIT. Nobody was actually using it, and table-driven should achieve 80-90% of the perf. --- BUILD | 29 +- tests/pb/test_decoder.cc | 45 +- third_party/dynasm/LICENSE | 56 -- third_party/dynasm/README.google | 10 - third_party/dynasm/dasm_arm.h | 448 --------- third_party/dynasm/dasm_arm.lua | 949 ------------------- third_party/dynasm/dasm_mips.h | 415 -------- third_party/dynasm/dasm_mips.lua | 959 ------------------- third_party/dynasm/dasm_ppc.h | 411 -------- third_party/dynasm/dasm_ppc.lua | 1230 ------------------------ third_party/dynasm/dasm_proto.h | 83 -- third_party/dynasm/dasm_x64.lua | 12 - third_party/dynasm/dasm_x86.h | 470 ---------- third_party/dynasm/dasm_x86.lua | 1931 -------------------------------------- third_party/dynasm/dynasm.lua | 1084 --------------------- third_party/protobuf | 1 - upb/pb/compile_decoder.c | 38 +- upb/pb/compile_decoder_x64.c | 511 ---------- upb/pb/compile_decoder_x64.dasc | 1150 ----------------------- upb/pb/compile_decoder_x64.h | 1737 ---------------------------------- 20 files changed, 10 insertions(+), 11559 deletions(-) delete mode 100644 third_party/dynasm/LICENSE delete mode 100644 third_party/dynasm/README.google delete mode 100644 third_party/dynasm/dasm_arm.h delete mode 100644 third_party/dynasm/dasm_arm.lua delete mode 100644 third_party/dynasm/dasm_mips.h delete mode 100644 third_party/dynasm/dasm_mips.lua delete mode 100644 third_party/dynasm/dasm_ppc.h delete mode 100644 third_party/dynasm/dasm_ppc.lua delete mode 100644 third_party/dynasm/dasm_proto.h delete mode 100644 third_party/dynasm/dasm_x64.lua delete mode 100644 third_party/dynasm/dasm_x86.h delete mode 100644 third_party/dynasm/dasm_x86.lua delete mode 100644 third_party/dynasm/dynasm.lua delete mode 160000 third_party/protobuf delete mode 100644 upb/pb/compile_decoder_x64.c delete mode 100644 upb/pb/compile_decoder_x64.dasc delete mode 100644 upb/pb/compile_decoder_x64.h (limited to 'upb') diff --git a/BUILD b/BUILD index 5804d36..80daf4f 100644 --- a/BUILD +++ b/BUILD @@ -67,15 +67,7 @@ cc_library( "upb/pb/textprinter.c", "upb/pb/varint.c", "upb/pb/varint.int.h", - ] + select({ - ":k8": [ - "upb/pb/compile_decoder_x64.c", - "upb/pb/compile_decoder_x64.h", - "third_party/dynasm/dasm_proto.h", - "third_party/dynasm/dasm_x86.h", - ], - "//conditions:default": [], - }), + ], hdrs = [ "upb/pb/decoder.h", "upb/pb/encoder.h", @@ -85,10 +77,7 @@ cc_library( "-std=c89", "-pedantic", "-Wno-long-long", - ] + select({ - ":k8": ["-DUPB_USE_JIT_X64"], - "//conditions:default": [], - }), + ], deps = [ ":upb", ], @@ -421,19 +410,6 @@ py_library( srcs = ["tools/staleness_test_lib.py"], ) -genrule( - name = "make_dynasm_decoder", - srcs = [ - "third_party/dynasm/dynasm.lua", - "third_party/dynasm/dasm_x64.lua", - "third_party/dynasm/dasm_x86.lua", - "upb/pb/compile_decoder_x64.dasc", - ], - outs = ["generated/upb/pb/compile_decoder_x64.h"], - cmd = "LUA_PATH=third_party/dynasm/?.lua $(location @lua//:lua) third_party/dynasm/dynasm.lua -c upb/pb/compile_decoder_x64.dasc > $@", - tools = ["@lua"], -) - py_binary( name = "make_cmakelists", srcs = ["tools/make_cmakelists.py"], @@ -514,7 +490,6 @@ generated_file_staleness_test( "google/protobuf/descriptor.upb.h", "tests/json/test.proto.pb", "upb/json/parser.c", - "upb/pb/compile_decoder_x64.h", ], generated_pattern = "generated/%s", ) diff --git a/tests/pb/test_decoder.cc b/tests/pb/test_decoder.cc index 3c41801..8012405 100644 --- a/tests/pb/test_decoder.cc +++ b/tests/pb/test_decoder.cc @@ -242,16 +242,8 @@ void indentbuf(string *buf, int depth) { buf->append(2 * depth, ' '); } -void check_stack_alignment() { -#ifdef UPB_USE_JIT_X64 - void *rsp = __builtin_frame_address(0); - ASSERT(((uintptr_t)rsp % 16) == 0); -#endif -} - #define NUMERIC_VALUE_HANDLER(member, ctype, fmt) \ bool value_##member(int* depth, const uint32_t* num, ctype val) { \ - check_stack_alignment(); \ indentbuf(&output, *depth); \ appendf(&output, "%" PRIu32 ":%" fmt "\n", *num, val); \ return true; \ @@ -265,14 +257,12 @@ NUMERIC_VALUE_HANDLER(float, float, "g") NUMERIC_VALUE_HANDLER(double, double, "g") bool value_bool(int* depth, const uint32_t* num, bool val) { - check_stack_alignment(); indentbuf(&output, *depth); appendf(&output, "%" PRIu32 ":%s\n", *num, val ? "true" : "false"); return true; } int* startstr(int* depth, const uint32_t* num, size_t size_hint) { - check_stack_alignment(); indentbuf(&output, *depth); appendf(&output, "%" PRIu32 ":(%zu)\"", *num, size_hint); return depth + 1; @@ -282,7 +272,6 @@ size_t value_string(int* depth, const uint32_t* num, const char* buf, size_t n, const upb_bufhandle* handle) { UPB_UNUSED(num); UPB_UNUSED(depth); - check_stack_alignment(); output.append(buf, n); ASSERT(handle == &global_handle); return n; @@ -290,7 +279,6 @@ size_t value_string(int* depth, const uint32_t* num, const char* buf, bool endstr(int* depth, const uint32_t* num) { UPB_UNUSED(num); - check_stack_alignment(); output.append("\n"); indentbuf(&output, *depth); appendf(&output, "%" PRIu32 ":\"\n", *num); @@ -298,7 +286,6 @@ bool endstr(int* depth, const uint32_t* num) { } int* startsubmsg(int* depth, const uint32_t* num) { - check_stack_alignment(); indentbuf(&output, *depth); appendf(&output, "%" PRIu32 ":{\n", *num); return depth + 1; @@ -306,14 +293,12 @@ int* startsubmsg(int* depth, const uint32_t* num) { bool endsubmsg(int* depth, const uint32_t* num) { UPB_UNUSED(num); - check_stack_alignment(); indentbuf(&output, *depth); output.append("}\n"); return true; } int* startseq(int* depth, const uint32_t* num) { - check_stack_alignment(); indentbuf(&output, *depth); appendf(&output, "%" PRIu32 ":[\n", *num); return depth + 1; @@ -321,14 +306,12 @@ int* startseq(int* depth, const uint32_t* num) { bool endseq(int* depth, const uint32_t* num) { UPB_UNUSED(num); - check_stack_alignment(); indentbuf(&output, *depth); output.append("]\n"); return true; } bool startmsg(int* depth) { - check_stack_alignment(); indentbuf(&output, *depth); output.append("<\n"); return true; @@ -336,7 +319,6 @@ bool startmsg(int* depth) { bool endmsg(int* depth, upb_status* status) { UPB_UNUSED(status); - check_stack_alignment(); indentbuf(&output, *depth); output.append(">\n"); return true; @@ -507,8 +489,6 @@ void do_run_decoder(VerboseParserEnvironment* env, upb::pb::DecoderPtr decoder, if (filter_hash) { fprintf(stderr, "RUNNING TEST CASE, hash=%x\n", testhash); - fprintf(stderr, "JIT on: %s\n", - global_method.is_native() ? "true" : "false"); fprintf(stderr, "Input (len=%u): ", (unsigned)proto.size()); PrintBinary(proto); fprintf(stderr, "\n"); @@ -1128,7 +1108,7 @@ void test_valid() { void empty_callback(const void *closure, upb::Handlers* h_ptr) {} -void test_emptyhandlers(upb::SymbolTable* symtab, bool allowjit) { +void test_emptyhandlers(upb::SymbolTable* symtab) { // Create an empty handlers to make sure that the decoder can handle empty // messages. HandlerRegisterData handlerdata; @@ -1137,8 +1117,6 @@ void test_emptyhandlers(upb::SymbolTable* symtab, bool allowjit) { upb::HandlerCache handler_cache(empty_callback, &handlerdata); upb::pb::CodeCache pb_code_cache(&handler_cache); - pb_code_cache.set_allow_jit(allowjit); - upb::MessageDefPtr md = upb::MessageDefPtr(Empty_getmsgdef(symtab->ptr())); global_handlers = handler_cache.Get(md); global_method = pb_code_cache.Get(md); @@ -1173,7 +1151,7 @@ void test_emptyhandlers(upb::SymbolTable* symtab, bool allowjit) { } } -void run_tests(bool use_jit) { +void run_tests() { HandlerRegisterData handlerdata; handlerdata.mode = test_mode; @@ -1181,26 +1159,15 @@ void run_tests(bool use_jit) { upb::HandlerCache handler_cache(callback, &handlerdata); upb::pb::CodeCache pb_code_cache(&handler_cache); - pb_code_cache.set_allow_jit(use_jit); - upb::MessageDefPtr md(DecoderTest_getmsgdef(symtab.ptr())); global_handlers = handler_cache.Get(md); global_method = pb_code_cache.Get(md); - ASSERT(use_jit == global_method.is_native()); completed = 0; test_invalid(); test_valid(); - test_emptyhandlers(&symtab, use_jit); -} - -void run_test_suite() { - // Test without/with JIT. - run_tests(false); -#ifdef UPB_USE_JIT_X64 - run_tests(true); -#endif + test_emptyhandlers(&symtab); } extern "C" { @@ -1216,16 +1183,16 @@ int run_tests(int argc, char *argv[]) { count = &total; total = 0; test_mode = COUNT_ONLY; - run_test_suite(); + run_tests(); count = &completed; total *= 2; // NO_HANDLERS, ALL_HANDLERS. test_mode = NO_HANDLERS; - run_test_suite(); + run_tests(); test_mode = ALL_HANDLERS; - run_test_suite(); + run_tests(); printf("All tests passed, %d assertions.\n", num_assertions); return 0; diff --git a/third_party/dynasm/LICENSE b/third_party/dynasm/LICENSE deleted file mode 100644 index 6f2a45b..0000000 --- a/third_party/dynasm/LICENSE +++ /dev/null @@ -1,56 +0,0 @@ -=============================================================================== -LuaJIT -- a Just-In-Time Compiler for Lua. http://luajit.org/ - -Copyright (C) 2005-2011 Mike Pall. All rights reserved. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - -[ MIT license: http://www.opensource.org/licenses/mit-license.php ] - -=============================================================================== -[ LuaJIT includes code from Lua 5.1/5.2, which has this license statement: ] - -Copyright (C) 1994-2011 Lua.org, PUC-Rio. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. - -=============================================================================== -[ LuaJIT includes code from dlmalloc, which has this license statement: ] - -This is a version (aka dlmalloc) of malloc/free/realloc written by -Doug Lea and released to the public domain, as explained at -http://creativecommons.org/licenses/publicdomain - -=============================================================================== diff --git a/third_party/dynasm/README.google b/third_party/dynasm/README.google deleted file mode 100644 index dd829a0..0000000 --- a/third_party/dynasm/README.google +++ /dev/null @@ -1,10 +0,0 @@ -URL: http://repo.or.cz/w/luajit-2.0.git/tree/6c05739684527919293e25668589f17c35a7c129:/dynasm -Version: 6c05739684527919293e25668589f17c35a7c129 -License: MIT -License File: LICENSE -Description: -Taken from the larger LuaJIT project, DynASM is a tiny preprocessor and -runtime for generating machine code at runtime. - -Local Modifications: -No modifications. diff --git a/third_party/dynasm/dasm_arm.h b/third_party/dynasm/dasm_arm.h deleted file mode 100644 index b770c2d..0000000 --- a/third_party/dynasm/dasm_arm.h +++ /dev/null @@ -1,448 +0,0 @@ -/* -** DynASM ARM encoding engine. -** Copyright (C) 2005-2012 Mike Pall. All rights reserved. -** Released under the MIT license. See dynasm.lua for full copyright notice. -*/ - -#include -#include -#include -#include - -#define DASM_ARCH "arm" - -#ifndef DASM_EXTERN -#define DASM_EXTERN(a,b,c,d) 0 -#endif - -/* Action definitions. */ -enum { - DASM_STOP, DASM_SECTION, DASM_ESC, DASM_REL_EXT, - /* The following actions need a buffer position. */ - DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG, - /* The following actions also have an argument. */ - DASM_REL_PC, DASM_LABEL_PC, - DASM_IMM, DASM_IMM12, DASM_IMM16, DASM_IMML8, DASM_IMML12, - DASM__MAX -}; - -/* Maximum number of section buffer positions for a single dasm_put() call. */ -#define DASM_MAXSECPOS 25 - -/* DynASM encoder status codes. Action list offset or number are or'ed in. */ -#define DASM_S_OK 0x00000000 -#define DASM_S_NOMEM 0x01000000 -#define DASM_S_PHASE 0x02000000 -#define DASM_S_MATCH_SEC 0x03000000 -#define DASM_S_RANGE_I 0x11000000 -#define DASM_S_RANGE_SEC 0x12000000 -#define DASM_S_RANGE_LG 0x13000000 -#define DASM_S_RANGE_PC 0x14000000 -#define DASM_S_RANGE_REL 0x15000000 -#define DASM_S_UNDEF_LG 0x21000000 -#define DASM_S_UNDEF_PC 0x22000000 - -/* Macros to convert positions (8 bit section + 24 bit index). */ -#define DASM_POS2IDX(pos) ((pos)&0x00ffffff) -#define DASM_POS2BIAS(pos) ((pos)&0xff000000) -#define DASM_SEC2POS(sec) ((sec)<<24) -#define DASM_POS2SEC(pos) ((pos)>>24) -#define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos)) - -/* Action list type. */ -typedef const unsigned int *dasm_ActList; - -/* Per-section structure. */ -typedef struct dasm_Section { - int *rbuf; /* Biased buffer pointer (negative section bias). */ - int *buf; /* True buffer pointer. */ - size_t bsize; /* Buffer size in bytes. */ - int pos; /* Biased buffer position. */ - int epos; /* End of biased buffer position - max single put. */ - int ofs; /* Byte offset into section. */ -} dasm_Section; - -/* Core structure holding the DynASM encoding state. */ -struct dasm_State { - size_t psize; /* Allocated size of this structure. */ - dasm_ActList actionlist; /* Current actionlist pointer. */ - int *lglabels; /* Local/global chain/pos ptrs. */ - size_t lgsize; - int *pclabels; /* PC label chains/pos ptrs. */ - size_t pcsize; - void **globals; /* Array of globals (bias -10). */ - dasm_Section *section; /* Pointer to active section. */ - size_t codesize; /* Total size of all code sections. */ - int maxsection; /* 0 <= sectionidx < maxsection. */ - int status; /* Status code. */ - dasm_Section sections[1]; /* All sections. Alloc-extended. */ -}; - -/* The size of the core structure depends on the max. number of sections. */ -#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section)) - - -/* Initialize DynASM state. */ -void dasm_init(Dst_DECL, int maxsection) -{ - dasm_State *D; - size_t psz = 0; - int i; - Dst_REF = NULL; - DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection)); - D = Dst_REF; - D->psize = psz; - D->lglabels = NULL; - D->lgsize = 0; - D->pclabels = NULL; - D->pcsize = 0; - D->globals = NULL; - D->maxsection = maxsection; - for (i = 0; i < maxsection; i++) { - D->sections[i].buf = NULL; /* Need this for pass3. */ - D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); - D->sections[i].bsize = 0; - D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ - } -} - -/* Free DynASM state. */ -void dasm_free(Dst_DECL) -{ - dasm_State *D = Dst_REF; - int i; - for (i = 0; i < D->maxsection; i++) - if (D->sections[i].buf) - DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize); - if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize); - if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize); - DASM_M_FREE(Dst, D, D->psize); -} - -/* Setup global label array. Must be called before dasm_setup(). */ -void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl) -{ - dasm_State *D = Dst_REF; - D->globals = gl - 10; /* Negative bias to compensate for locals. */ - DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int)); -} - -/* Grow PC label array. Can be called after dasm_setup(), too. */ -void dasm_growpc(Dst_DECL, unsigned int maxpc) -{ - dasm_State *D = Dst_REF; - size_t osz = D->pcsize; - DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int)); - memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz); -} - -/* Setup encoder. */ -void dasm_setup(Dst_DECL, const void *actionlist) -{ - dasm_State *D = Dst_REF; - int i; - D->actionlist = (dasm_ActList)actionlist; - D->status = DASM_S_OK; - D->section = &D->sections[0]; - memset((void *)D->lglabels, 0, D->lgsize); - if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize); - for (i = 0; i < D->maxsection; i++) { - D->sections[i].pos = DASM_SEC2POS(i); - D->sections[i].ofs = 0; - } -} - - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) { \ - D->status = DASM_S_##st|(p-D->actionlist-1); return; } } while (0) -#define CKPL(kind, st) \ - do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \ - D->status = DASM_S_RANGE_##st|(p-D->actionlist-1); return; } } while (0) -#else -#define CK(x, st) ((void)0) -#define CKPL(kind, st) ((void)0) -#endif - -static int dasm_imm12(unsigned int n) -{ - int i; - for (i = 0; i < 16; i++, n = (n << 2) | (n >> 30)) - if (n <= 255) return (int)(n + (i << 8)); - return -1; -} - -/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */ -void dasm_put(Dst_DECL, int start, ...) -{ - va_list ap; - dasm_State *D = Dst_REF; - dasm_ActList p = D->actionlist + start; - dasm_Section *sec = D->section; - int pos = sec->pos, ofs = sec->ofs; - int *b; - - if (pos >= sec->epos) { - DASM_M_GROW(Dst, int, sec->buf, sec->bsize, - sec->bsize + 2*DASM_MAXSECPOS*sizeof(int)); - sec->rbuf = sec->buf - DASM_POS2BIAS(pos); - sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos); - } - - b = sec->rbuf; - b[pos++] = start; - - va_start(ap, start); - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - if (action >= DASM__MAX) { - ofs += 4; - } else { - int *pl, n = action >= DASM_REL_PC ? va_arg(ap, int) : 0; - switch (action) { - case DASM_STOP: goto stop; - case DASM_SECTION: - n = (ins & 255); CK(n < D->maxsection, RANGE_SEC); - D->section = &D->sections[n]; goto stop; - case DASM_ESC: p++; ofs += 4; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs += (ins & 255); b[pos++] = ofs; break; - case DASM_REL_LG: - n = (ins & 2047) - 10; pl = D->lglabels + n; - if (n >= 0) { CKPL(lg, LG); goto putrel; } /* Bkwd rel or global. */ - pl += 10; n = *pl; - if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */ - goto linkrel; - case DASM_REL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putrel: - n = *pl; - if (n < 0) { /* Label exists. Get label pos and store it. */ - b[pos] = -n; - } else { - linkrel: - b[pos] = n; /* Else link to rel chain, anchored at label. */ - *pl = pos; - } - pos++; - break; - case DASM_LABEL_LG: - pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel; - case DASM_LABEL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putlabel: - n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; - } - *pl = -pos; /* Label exists now. */ - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_IMM: - case DASM_IMM16: -#ifdef DASM_CHECKS - CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I); - if ((ins & 0x8000)) - CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I); - else - CK((n>>((ins>>5)&31)) == 0, RANGE_I); -#endif - b[pos++] = n; - break; - case DASM_IMML8: - case DASM_IMML12: - CK(n >= 0 ? ((n>>((ins>>5)&31)) == 0) : - (((-n)>>((ins>>5)&31)) == 0), RANGE_I); - b[pos++] = n; - break; - case DASM_IMM12: - CK(dasm_imm12((unsigned int)n) != -1, RANGE_I); - b[pos++] = n; - break; - } - } - } -stop: - va_end(ap); - sec->pos = pos; - sec->ofs = ofs; -} -#undef CK - -/* Pass 2: Link sections, shrink aligns, fix label offsets. */ -int dasm_link(Dst_DECL, size_t *szp) -{ - dasm_State *D = Dst_REF; - int secnum; - int ofs = 0; - -#ifdef DASM_CHECKS - *szp = 0; - if (D->status != DASM_S_OK) return D->status; - { - int pc; - for (pc = 0; pc*sizeof(int) < D->pcsize; pc++) - if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc; - } -#endif - - { /* Handle globals not defined in this translation unit. */ - int idx; - for (idx = 20; idx*sizeof(int) < D->lgsize; idx++) { - int n = D->lglabels[idx]; - /* Undefined label: Collapse rel chain and replace with marker (< 0). */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; } - } - } - - /* Combine all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->rbuf; - int pos = DASM_SEC2POS(secnum); - int lastpos = sec->pos; - - while (pos != lastpos) { - dasm_ActList p = D->actionlist + b[pos++]; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: p++; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break; - case DASM_REL_LG: case DASM_REL_PC: pos++; break; - case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break; - case DASM_IMM: case DASM_IMM12: case DASM_IMM16: - case DASM_IMML8: case DASM_IMML12: pos++; break; - } - } - stop: (void)0; - } - ofs += sec->ofs; /* Next section starts right after current section. */ - } - - D->codesize = ofs; /* Total size of all code sections */ - *szp = ofs; - return DASM_S_OK; -} - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) return DASM_S_##st|(p-D->actionlist-1); } while (0) -#else -#define CK(x, st) ((void)0) -#endif - -/* Pass 3: Encode sections. */ -int dasm_encode(Dst_DECL, void *buffer) -{ - dasm_State *D = Dst_REF; - char *base = (char *)buffer; - unsigned int *cp = (unsigned int *)buffer; - int secnum; - - /* Encode all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->buf; - int *endb = sec->rbuf + sec->pos; - - while (b != endb) { - dasm_ActList p = D->actionlist + *b++; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0; - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: *cp++ = *p++; break; - case DASM_REL_EXT: - n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins&2047), !(ins&2048)); - goto patchrel; - case DASM_ALIGN: - ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0xe1a00000; - break; - case DASM_REL_LG: - CK(n >= 0, UNDEF_LG); - case DASM_REL_PC: - CK(n >= 0, UNDEF_PC); - n = *DASM_POS2PTR(D, n) - (int)((char *)cp - base) - 4; - patchrel: - if ((ins & 0x800) == 0) { - CK((n & 3) == 0 && ((n+0x02000000) >> 26) == 0, RANGE_REL); - cp[-1] |= ((n >> 2) & 0x00ffffff); - } else if ((ins & 0x1000)) { - CK((n & 3) == 0 && -256 <= n && n <= 256, RANGE_REL); - goto patchimml8; - } else { - CK((n & 3) == 0 && -4096 <= n && n <= 4096, RANGE_REL); - goto patchimml12; - } - break; - case DASM_LABEL_LG: - ins &= 2047; if (ins >= 20) D->globals[ins-10] = (void *)(base + n); - break; - case DASM_LABEL_PC: break; - case DASM_IMM: - cp[-1] |= ((n>>((ins>>10)&31)) & ((1<<((ins>>5)&31))-1)) << (ins&31); - break; - case DASM_IMM12: - cp[-1] |= dasm_imm12((unsigned int)n); - break; - case DASM_IMM16: - cp[-1] |= ((n & 0xf000) << 4) | (n & 0x0fff); - break; - case DASM_IMML8: patchimml8: - cp[-1] |= n >= 0 ? (0x00800000 | (n & 0x0f) | ((n & 0xf0) << 4)) : - ((-n & 0x0f) | ((-n & 0xf0) << 4)); - break; - case DASM_IMML12: patchimml12: - cp[-1] |= n >= 0 ? (0x00800000 | n) : (-n); - break; - default: *cp++ = ins; break; - } - } - stop: (void)0; - } - } - - if (base + D->codesize != (char *)cp) /* Check for phase errors. */ - return DASM_S_PHASE; - return DASM_S_OK; -} -#undef CK - -/* Get PC label offset. */ -int dasm_getpclabel(Dst_DECL, unsigned int pc) -{ - dasm_State *D = Dst_REF; - if (pc*sizeof(int) < D->pcsize) { - int pos = D->pclabels[pc]; - if (pos < 0) return *DASM_POS2PTR(D, -pos); - if (pos > 0) return -1; /* Undefined. */ - } - return -2; /* Unused or out of range. */ -} - -#ifdef DASM_CHECKS -/* Optional sanity checker to call between isolated encoding steps. */ -int dasm_checkstep(Dst_DECL, int secmatch) -{ - dasm_State *D = Dst_REF; - if (D->status == DASM_S_OK) { - int i; - for (i = 1; i <= 9; i++) { - if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_LG|i; break; } - D->lglabels[i] = 0; - } - } - if (D->status == DASM_S_OK && secmatch >= 0 && - D->section != &D->sections[secmatch]) - D->status = DASM_S_MATCH_SEC|(D->section-D->sections); - return D->status; -} -#endif - diff --git a/third_party/dynasm/dasm_arm.lua b/third_party/dynasm/dasm_arm.lua deleted file mode 100644 index cc4fa17..0000000 --- a/third_party/dynasm/dasm_arm.lua +++ /dev/null @@ -1,949 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM ARM module. --- --- Copyright (C) 2005-2012 Mike Pall. All rights reserved. --- See dynasm.lua for full copyright notice. ------------------------------------------------------------------------------- - --- Module information: -local _info = { - arch = "arm", - description = "DynASM ARM module", - version = "1.3.0", - vernum = 10300, - release = "2011-05-05", - author = "Mike Pall", - license = "MIT", -} - --- Exported glue functions for the arch-specific module. -local _M = { _info = _info } - --- Cache library functions. -local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs -local assert, setmetatable, rawget = assert, setmetatable, rawget -local _s = string -local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char -local match, gmatch, gsub = _s.match, _s.gmatch, _s.gsub -local concat, sort, insert = table.concat, table.sort, table.insert - --- Inherited tables and callbacks. -local g_opt, g_arch -local wline, werror, wfatal, wwarn - --- Action name list. --- CHECK: Keep this in sync with the C code! -local action_names = { - "STOP", "SECTION", "ESC", "REL_EXT", - "ALIGN", "REL_LG", "LABEL_LG", - "REL_PC", "LABEL_PC", "IMM", "IMM12", "IMM16", "IMML8", "IMML12", -} - --- Maximum number of section buffer positions for dasm_put(). --- CHECK: Keep this in sync with the C code! -local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines. - --- Action name -> action number. -local map_action = {} -for n,name in ipairs(action_names) do - map_action[name] = n-1 -end - --- Action list buffer. -local actlist = {} - --- Argument list for next dasm_put(). Start with offset 0 into action list. -local actargs = { 0 } - --- Current number of section buffer positions for dasm_put(). -local secpos = 1 - ------------------------------------------------------------------------------- - --- Return 8 digit hex number. -local function tohex(x) - return sub(format("%08x", x), -8) -- Avoid 64 bit portability problem in Lua. -end - --- Dump action names and numbers. -local function dumpactions(out) - out:write("DynASM encoding engine action codes:\n") - for n,name in ipairs(action_names) do - local num = map_action[name] - out:write(format(" %-10s %02X %d\n", name, num, num)) - end - out:write("\n") -end - --- Write action list buffer as a huge static C array. -local function writeactions(out, name) - local nn = #actlist - if nn == 0 then nn = 1; actlist[0] = map_action.STOP end - out:write("static const unsigned int ", name, "[", nn, "] = {\n") - for i = 1,nn-1 do - assert(out:write("0x", tohex(actlist[i]), ",\n")) - end - assert(out:write("0x", tohex(actlist[nn]), "\n};\n\n")) -end - ------------------------------------------------------------------------------- - --- Add word to action list. -local function wputxw(n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - actlist[#actlist+1] = n -end - --- Add action to list with optional arg. Advance buffer pos, too. -local function waction(action, val, a, num) - local w = assert(map_action[action], "bad action name `"..action.."'") - wputxw(w * 0x10000 + (val or 0)) - if a then actargs[#actargs+1] = a end - if a or num then secpos = secpos + (num or 1) end -end - --- Flush action list (intervening C code or buffer pos overflow). -local function wflush(term) - if #actlist == actargs[1] then return end -- Nothing to flush. - if not term then waction("STOP") end -- Terminate action list. - wline(format("dasm_put(Dst, %s);", concat(actargs, ", ")), true) - actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put(). - secpos = 1 -- The actionlist offset occupies a buffer position, too. -end - --- Put escaped word. -local function wputw(n) - if n <= 0x000fffff then waction("ESC") end - wputxw(n) -end - --- Reserve position for word. -local function wpos() - local pos = #actlist+1 - actlist[pos] = "" - return pos -end - --- Store word to reserved position. -local function wputpos(pos, n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - if n <= 0x000fffff then - insert(actlist, pos+1, n) - n = map_action.ESC * 0x10000 - end - actlist[pos] = n -end - ------------------------------------------------------------------------------- - --- Global label name -> global label number. With auto assignment on 1st use. -local next_global = 20 -local map_global = setmetatable({}, { __index = function(t, name) - if not match(name, "^[%a_][%w_]*$") then werror("bad global label") end - local n = next_global - if n > 2047 then werror("too many global labels") end - next_global = n + 1 - t[name] = n - return n -end}) - --- Dump global labels. -local function dumpglobals(out, lvl) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("Global labels:\n") - for i=20,next_global-1 do - out:write(format(" %s\n", t[i])) - end - out:write("\n") -end - --- Write global label enum. -local function writeglobals(out, prefix) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("enum {\n") - for i=20,next_global-1 do - out:write(" ", prefix, t[i], ",\n") - end - out:write(" ", prefix, "_MAX\n};\n") -end - --- Write global label names. -local function writeglobalnames(out, name) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("static const char *const ", name, "[] = {\n") - for i=20,next_global-1 do - out:write(" \"", t[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Extern label name -> extern label number. With auto assignment on 1st use. -local next_extern = 0 -local map_extern_ = {} -local map_extern = setmetatable({}, { __index = function(t, name) - -- No restrictions on the name for now. - local n = next_extern - if n > 2047 then werror("too many extern labels") end - next_extern = n + 1 - t[name] = n - map_extern_[n] = name - return n -end}) - --- Dump extern labels. -local function dumpexterns(out, lvl) - out:write("Extern labels:\n") - for i=0,next_extern-1 do - out:write(format(" %s\n", map_extern_[i])) - end - out:write("\n") -end - --- Write extern label names. -local function writeexternnames(out, name) - out:write("static const char *const ", name, "[] = {\n") - for i=0,next_extern-1 do - out:write(" \"", map_extern_[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Arch-specific maps. - --- Ext. register name -> int. name. -local map_archdef = { sp = "r13", lr = "r14", pc = "r15", } - --- Int. register name -> ext. name. -local map_reg_rev = { r13 = "sp", r14 = "lr", r15 = "pc", } - -local map_type = {} -- Type name -> { ctype, reg } -local ctypenum = 0 -- Type number (for Dt... macros). - --- Reverse defines for registers. -function _M.revdef(s) - return map_reg_rev[s] or s -end - -local map_shift = { lsl = 0, lsr = 1, asr = 2, ror = 3, } - -local map_cond = { - eq = 0, ne = 1, cs = 2, cc = 3, mi = 4, pl = 5, vs = 6, vc = 7, - hi = 8, ls = 9, ge = 10, lt = 11, gt = 12, le = 13, al = 14, - hs = 2, lo = 3, -} - ------------------------------------------------------------------------------- - --- Template strings for ARM instructions. -local map_op = { - -- Basic data processing instructions. - and_3 = "e0000000DNPs", - eor_3 = "e0200000DNPs", - sub_3 = "e0400000DNPs", - rsb_3 = "e0600000DNPs", - add_3 = "e0800000DNPs", - adc_3 = "e0a00000DNPs", - sbc_3 = "e0c00000DNPs", - rsc_3 = "e0e00000DNPs", - tst_2 = "e1100000NP", - teq_2 = "e1300000NP", - cmp_2 = "e1500000NP", - cmn_2 = "e1700000NP", - orr_3 = "e1800000DNPs", - mov_2 = "e1a00000DPs", - bic_3 = "e1c00000DNPs", - mvn_2 = "e1e00000DPs", - - and_4 = "e0000000DNMps", - eor_4 = "e0200000DNMps", - sub_4 = "e0400000DNMps", - rsb_4 = "e0600000DNMps", - add_4 = "e0800000DNMps", - adc_4 = "e0a00000DNMps", - sbc_4 = "e0c00000DNMps", - rsc_4 = "e0e00000DNMps", - tst_3 = "e1100000NMp", - teq_3 = "e1300000NMp", - cmp_3 = "e1500000NMp", - cmn_3 = "e1700000NMp", - orr_4 = "e1800000DNMps", - mov_3 = "e1a00000DMps", - bic_4 = "e1c00000DNMps", - mvn_3 = "e1e00000DMps", - - lsl_3 = "e1a00000DMws", - lsr_3 = "e1a00020DMws", - asr_3 = "e1a00040DMws", - ror_3 = "e1a00060DMws", - rrx_2 = "e1a00060DMs", - - -- Multiply and multiply-accumulate. - mul_3 = "e0000090NMSs", - mla_4 = "e0200090NMSDs", - umaal_4 = "e0400090DNMSs", -- v6 - mls_4 = "e0600090DNMSs", -- v6T2 - umull_4 = "e0800090DNMSs", - umlal_4 = "e0a00090DNMSs", - smull_4 = "e0c00090DNMSs", - smlal_4 = "e0e00090DNMSs", - - -- Halfword multiply and multiply-accumulate. - smlabb_4 = "e1000080NMSD", -- v5TE - smlatb_4 = "e10000a0NMSD", -- v5TE - smlabt_4 = "e10000c0NMSD", -- v5TE - smlatt_4 = "e10000e0NMSD", -- v5TE - smlawb_4 = "e1200080NMSD", -- v5TE - smulwb_3 = "e12000a0NMS", -- v5TE - smlawt_4 = "e12000c0NMSD", -- v5TE - smulwt_3 = "e12000e0NMS", -- v5TE - smlalbb_4 = "e1400080NMSD", -- v5TE - smlaltb_4 = "e14000a0NMSD", -- v5TE - smlalbt_4 = "e14000c0NMSD", -- v5TE - smlaltt_4 = "e14000e0NMSD", -- v5TE - smulbb_3 = "e1600080NMS", -- v5TE - smultb_3 = "e16000a0NMS", -- v5TE - smulbt_3 = "e16000c0NMS", -- v5TE - smultt_3 = "e16000e0NMS", -- v5TE - - -- Miscellaneous data processing instructions. - clz_2 = "e16f0f10DM", -- v5T - rev_2 = "e6bf0f30DM", -- v6 - rev16_2 = "e6bf0fb0DM", -- v6 - revsh_2 = "e6ff0fb0DM", -- v6 - sel_3 = "e6800fb0DNM", -- v6 - usad8_3 = "e780f010NMS", -- v6 - usada8_4 = "e7800010NMSD", -- v6 - rbit_2 = "e6ff0f30DM", -- v6T2 - movw_2 = "e3000000DW", -- v6T2 - movt_2 = "e3400000DW", -- v6T2 - -- Note: the X encodes width-1, not width. - sbfx_4 = "e7a00050DMvX", -- v6T2 - ubfx_4 = "e7e00050DMvX", -- v6T2 - -- Note: the X encodes the msb field, not the width. - bfc_3 = "e7c0001fDvX", -- v6T2 - bfi_4 = "e7c00010DMvX", -- v6T2 - - -- Packing and unpacking instructions. - pkhbt_3 = "e6800010DNM", pkhbt_4 = "e6800010DNMv", -- v6 - pkhtb_3 = "e6800050DNM", pkhtb_4 = "e6800050DNMv", -- v6 - sxtab_3 = "e6a00070DNM", sxtab_4 = "e6a00070DNMv", -- v6 - sxtab16_3 = "e6800070DNM", sxtab16_4 = "e6800070DNMv", -- v6 - sxtah_3 = "e6b00070DNM", sxtah_4 = "e6b00070DNMv", -- v6 - sxtb_2 = "e6af0070DM", sxtb_3 = "e6af0070DMv", -- v6 - sxtb16_2 = "e68f0070DM", sxtb16_3 = "e68f0070DMv", -- v6 - sxth_2 = "e6bf0070DM", sxth_3 = "e6bf0070DMv", -- v6 - uxtab_3 = "e6e00070DNM", uxtab_4 = "e6e00070DNMv", -- v6 - uxtab16_3 = "e6c00070DNM", uxtab16_4 = "e6c00070DNMv", -- v6 - uxtah_3 = "e6f00070DNM", uxtah_4 = "e6f00070DNMv", -- v6 - uxtb_2 = "e6ef0070DM", uxtb_3 = "e6ef0070DMv", -- v6 - uxtb16_2 = "e6cf0070DM", uxtb16_3 = "e6cf0070DMv", -- v6 - uxth_2 = "e6ff0070DM", uxth_3 = "e6ff0070DMv", -- v6 - - -- Saturating instructions. - qadd_3 = "e1000050DMN", -- v5TE - qsub_3 = "e1200050DMN", -- v5TE - qdadd_3 = "e1400050DMN", -- v5TE - qdsub_3 = "e1600050DMN", -- v5TE - -- Note: the X for ssat* encodes sat_imm-1, not sat_imm. - ssat_3 = "e6a00010DXM", ssat_4 = "e6a00010DXMp", -- v6 - usat_3 = "e6e00010DXM", usat_4 = "e6e00010DXMp", -- v6 - ssat16_3 = "e6a00f30DXM", -- v6 - usat16_3 = "e6e00f30DXM", -- v6 - - -- Parallel addition and subtraction. - sadd16_3 = "e6100f10DNM", -- v6 - sasx_3 = "e6100f30DNM", -- v6 - ssax_3 = "e6100f50DNM", -- v6 - ssub16_3 = "e6100f70DNM", -- v6 - sadd8_3 = "e6100f90DNM", -- v6 - ssub8_3 = "e6100ff0DNM", -- v6 - qadd16_3 = "e6200f10DNM", -- v6 - qasx_3 = "e6200f30DNM", -- v6 - qsax_3 = "e6200f50DNM", -- v6 - qsub16_3 = "e6200f70DNM", -- v6 - qadd8_3 = "e6200f90DNM", -- v6 - qsub8_3 = "e6200ff0DNM", -- v6 - shadd16_3 = "e6300f10DNM", -- v6 - shasx_3 = "e6300f30DNM", -- v6 - shsax_3 = "e6300f50DNM", -- v6 - shsub16_3 = "e6300f70DNM", -- v6 - shadd8_3 = "e6300f90DNM", -- v6 - shsub8_3 = "e6300ff0DNM", -- v6 - uadd16_3 = "e6500f10DNM", -- v6 - uasx_3 = "e6500f30DNM", -- v6 - usax_3 = "e6500f50DNM", -- v6 - usub16_3 = "e6500f70DNM", -- v6 - uadd8_3 = "e6500f90DNM", -- v6 - usub8_3 = "e6500ff0DNM", -- v6 - uqadd16_3 = "e6600f10DNM", -- v6 - uqasx_3 = "e6600f30DNM", -- v6 - uqsax_3 = "e6600f50DNM", -- v6 - uqsub16_3 = "e6600f70DNM", -- v6 - uqadd8_3 = "e6600f90DNM", -- v6 - uqsub8_3 = "e6600ff0DNM", -- v6 - uhadd16_3 = "e6700f10DNM", -- v6 - uhasx_3 = "e6700f30DNM", -- v6 - uhsax_3 = "e6700f50DNM", -- v6 - uhsub16_3 = "e6700f70DNM", -- v6 - uhadd8_3 = "e6700f90DNM", -- v6 - uhsub8_3 = "e6700ff0DNM", -- v6 - - -- Load/store instructions. - str_2 = "e4000000DL", str_3 = "e4000000DL", str_4 = "e4000000DL", - strb_2 = "e4400000DL", strb_3 = "e4400000DL", strb_4 = "e4400000DL", - ldr_2 = "e4100000DL", ldr_3 = "e4100000DL", ldr_4 = "e4100000DL", - ldrb_2 = "e4500000DL", ldrb_3 = "e4500000DL", ldrb_4 = "e4500000DL", - strh_2 = "e00000b0DL", strh_3 = "e00000b0DL", - ldrh_2 = "e01000b0DL", ldrh_3 = "e01000b0DL", - ldrd_2 = "e00000d0DL", ldrd_3 = "e00000d0DL", -- v5TE - ldrsb_2 = "e01000d0DL", ldrsb_3 = "e01000d0DL", - strd_2 = "e00000f0DL", strd_3 = "e00000f0DL", -- v5TE - ldrsh_2 = "e01000f0DL", ldrsh_3 = "e01000f0DL", - - ldm_2 = "e8900000nR", ldmia_2 = "e8900000nR", ldmfd_2 = "e8900000nR", - ldmda_2 = "e8100000nR", ldmfa_2 = "e8100000nR", - ldmdb_2 = "e9100000nR", ldmea_2 = "e9100000nR", - ldmib_2 = "e9900000nR", ldmed_2 = "e9900000nR", - stm_2 = "e8800000nR", stmia_2 = "e8800000nR", stmfd_2 = "e8800000nR", - stmda_2 = "e8000000nR", stmfa_2 = "e8000000nR", - stmdb_2 = "e9000000nR", stmea_2 = "e9000000nR", - stmib_2 = "e9800000nR", stmed_2 = "e9800000nR", - pop_1 = "e8bd0000R", push_1 = "e92d0000R", - - -- Branch instructions. - b_1 = "ea000000B", - bl_1 = "eb000000B", - blx_1 = "e12fff30C", - bx_1 = "e12fff10M", - - -- Miscellaneous instructions. - nop_0 = "e1a00000", - mrs_1 = "e10f0000D", - bkpt_1 = "e1200070K", -- v5T - svc_1 = "ef000000T", swi_1 = "ef000000T", - ud_0 = "e7f001f0", - - -- NYI: Advanced SIMD and VFP instructions. - - -- NYI instructions, since I have no need for them right now: - -- swp, swpb, strex, ldrex, strexd, ldrexd, strexb, ldrexb, strexh, ldrexh - -- msr, nopv6, yield, wfe, wfi, sev, dbg, bxj, smc, srs, rfe - -- cps, setend, pli, pld, pldw, clrex, dsb, dmb, isb - -- stc, ldc, mcr, mcr2, mrc, mrc2, mcrr, mcrr2, mrrc, mrrc2, cdp, cdp2 -} - --- Add mnemonics for "s" variants. -do - local t = {} - for k,v in pairs(map_op) do - if sub(v, -1) == "s" then - local v2 = sub(v, 1, 2)..char(byte(v, 3)+1)..sub(v, 4, -2) - t[sub(k, 1, -3).."s"..sub(k, -2)] = v2 - end - end - for k,v in pairs(t) do - map_op[k] = v - end -end - ------------------------------------------------------------------------------- - -local function parse_gpr(expr) - local tname, ovreg = match(expr, "^([%w_]+):(r1?[0-9])$") - local tp = map_type[tname or expr] - if tp then - local reg = ovreg or tp.reg - if not reg then - werror("type `"..(tname or expr).."' needs a register override") - end - expr = reg - end - local r = match(expr, "^r(1?[0-9])$") - if r then - r = tonumber(r) - if r <= 15 then return r, tp end - end - werror("bad register name `"..expr.."'") -end - -local function parse_gpr_pm(expr) - local pm, expr2 = match(expr, "^([+-]?)(.*)$") - return parse_gpr(expr2), (pm == "-") -end - -local function parse_reglist(reglist) - reglist = match(reglist, "^{%s*([^}]*)}$") - if not reglist then werror("register list expected") end - local rr = 0 - for p in gmatch(reglist..",", "%s*([^,]*),") do - local rbit = 2^parse_gpr(gsub(p, "%s+$", "")) - if ((rr - (rr % rbit)) / rbit) % 2 ~= 0 then - werror("duplicate register `"..p.."'") - end - rr = rr + rbit - end - return rr -end - -local function parse_imm(imm, bits, shift, scale, signed) - imm = match(imm, "^#(.*)$") - if not imm then werror("expected immediate operand") end - local n = tonumber(imm) - if n then - if n % 2^scale == 0 then - n = n / 2^scale - if signed then - if n >= 0 then - if n < 2^(bits-1) then return n*2^shift end - else - if n >= -(2^(bits-1))-1 then return (n+2^bits)*2^shift end - end - else - if n >= 0 and n <= 2^bits-1 then return n*2^shift end - end - end - werror("out of range immediate `"..imm.."'") - else - waction("IMM", (signed and 32768 or 0)+scale*1024+bits*32+shift, imm) - return 0 - end -end - -local function parse_imm12(imm) - local n = tonumber(imm) - if n then - local m = n - for i=0,-15,-1 do - if m >= 0 and m <= 255 and n % 1 == 0 then return m + (i%16) * 256 end - local t = m % 4 - m = (m - t) / 4 + t * 2^30 - end - werror("out of range immediate `"..imm.."'") - else - waction("IMM12", 0, imm) - return 0 - end -end - -local function parse_imm16(imm) - imm = match(imm, "^#(.*)$") - if not imm then werror("expected immediate operand") end - local n = tonumber(imm) - if n then - if n >= 0 and n <= 65535 and n % 1 == 0 then - local t = n % 4096 - return (n - t) * 16 + t - end - werror("out of range immediate `"..imm.."'") - else - waction("IMM16", 32*16, imm) - return 0 - end -end - -local function parse_imm_load(imm, ext) - local n = tonumber(imm) - if n then - if ext then - if n >= -255 and n <= 255 then - local up = 0x00800000 - if n < 0 then n = -n; up = 0 end - return (n-(n%16))*16+(n%16) + up - end - else - if n >= -4095 and n <= 4095 then - if n >= 0 then return n+0x00800000 end - return -n - end - end - werror("out of range immediate `"..imm.."'") - else - waction(ext and "IMML8" or "IMML12", 32768 + 32*(ext and 8 or 12), imm) - return 0 - end -end - -local function parse_shift(shift, gprok) - if shift == "rrx" then - return 3 * 32 - else - local s, s2 = match(shift, "^(%S+)%s*(.*)$") - s = map_shift[s] - if not s then werror("expected shift operand") end - if sub(s2, 1, 1) == "#" then - return parse_imm(s2, 5, 7, 0, false) + s * 32 - else - if not gprok then werror("expected immediate shift operand") end - return parse_gpr(s2) * 256 + s * 32 + 16 - end - end -end - -local function parse_label(label, def) - local prefix = sub(label, 1, 2) - -- =>label (pc label reference) - if prefix == "=>" then - return "PC", 0, sub(label, 3) - end - -- ->name (global label reference) - if prefix == "->" then - return "LG", map_global[sub(label, 3)] - end - if def then - -- [1-9] (local label definition) - if match(label, "^[1-9]$") then - return "LG", 10+tonumber(label) - end - else - -- [<>][1-9] (local label reference) - local dir, lnum = match(label, "^([<>])([1-9])$") - if dir then -- Fwd: 1-9, Bkwd: 11-19. - return "LG", lnum + (dir == ">" and 0 or 10) - end - -- extern label (extern label reference) - local extname = match(label, "^extern%s+(%S+)$") - if extname then - return "EXT", map_extern[extname] - end - end - werror("bad label `"..label.."'") -end - -local function parse_load(params, nparams, n, op) - local oplo = op % 256 - local ext, ldrd = (oplo ~= 0), (oplo == 208) - local d - if (ldrd or oplo == 240) then - d = ((op - (op % 4096)) / 4096) % 16 - if d % 2 ~= 0 then werror("odd destination register") end - end - local pn = params[n] - local p1, wb = match(pn, "^%[%s*(.-)%s*%](!?)$") - local p2 = params[n+1] - if not p1 then - if not p2 then - if match(pn, "^[<>=%-]") or match(pn, "^extern%s+") then - local mode, n, s = parse_label(pn, false) - waction("REL_"..mode, n + (ext and 0x1800 or 0x0800), s, 1) - return op + 15 * 65536 + 0x01000000 + (ext and 0x00400000 or 0) - end - local reg, tailr = match(pn, "^([%w_:]+)%s*(.*)$") - if reg and tailr ~= "" then - local d, tp = parse_gpr(reg) - if tp then - waction(ext and "IMML8" or "IMML12", 32768 + 32*(ext and 8 or 12), - format(tp.ctypefmt, tailr)) - return op + d * 65536 + 0x01000000 + (ext and 0x00400000 or 0) - end - end - end - werror("expected address operand") - end - if wb == "!" then op = op + 0x00200000 end - if p2 then - if wb == "!" then werror("bad use of '!'") end - local p3 = params[n+2] - op = op + parse_gpr(p1) * 65536 - local imm = match(p2, "^#(.*)$") - if imm then - local m = parse_imm_load(imm, ext) - if p3 then werror("too many parameters") end - op = op + m + (ext and 0x00400000 or 0) - else - local m, neg = parse_gpr_pm(p2) - if ldrd and (m == d or m-1 == d) then werror("register conflict") end - op = op + m + (neg and 0 or 0x00800000) + (ext and 0 or 0x02000000) - if p3 then op = op + parse_shift(p3) end - end - else - local p1a, p2 = match(p1, "^([^,%s]*)%s*(.*)$") - op = op + parse_gpr(p1a) * 65536 + 0x01000000 - if p2 ~= "" then - local imm = match(p2, "^,%s*#(.*)$") - if imm then - local m = parse_imm_load(imm, ext) - op = op + m + (ext and 0x00400000 or 0) - else - local p2a, p3 = match(p2, "^,%s*([^,%s]*)%s*,?%s*(.*)$") - local m, neg = parse_gpr_pm(p2a) - if ldrd and (m == d or m-1 == d) then werror("register conflict") end - op = op + m + (neg and 0 or 0x00800000) + (ext and 0 or 0x02000000) - if p3 ~= "" then - if ext then werror("too many parameters") end - op = op + parse_shift(p3) - end - end - else - if wb == "!" then werror("bad use of '!'") end - op = op + (ext and 0x00c00000 or 0x00800000) - end - end - return op -end - ------------------------------------------------------------------------------- - --- Handle opcodes defined with template strings. -map_op[".template__"] = function(params, template, nparams) - if not params then return sub(template, 9) end - local op = tonumber(sub(template, 1, 8), 16) - local n = 1 - - -- Limit number of section buffer positions used by a single dasm_put(). - -- A single opcode needs a maximum of 3 positions. - if secpos+3 > maxsecpos then wflush() end - local pos = wpos() - - -- Process each character. - for p in gmatch(sub(template, 9), ".") do - if p == "D" then - op = op + parse_gpr(params[n]) * 4096; n = n + 1 - elseif p == "N" then - op = op + parse_gpr(params[n]) * 65536; n = n + 1 - elseif p == "S" then - op = op + parse_gpr(params[n]) * 256; n = n + 1 - elseif p == "M" then - op = op + parse_gpr(params[n]); n = n + 1 - elseif p == "P" then - local imm = match(params[n], "^#(.*)$") - if imm then - op = op + parse_imm12(imm) + 0x02000000 - else - op = op + parse_gpr(params[n]) - end - n = n + 1 - elseif p == "p" then - op = op + parse_shift(params[n], true); n = n + 1 - elseif p == "L" then - op = parse_load(params, nparams, n, op) - elseif p == "B" then - local mode, n, s = parse_label(params[n], false) - waction("REL_"..mode, n, s, 1) - elseif p == "C" then -- blx gpr vs. blx label. - local p = params[n] - if match(p, "^([%w_]+):(r1?[0-9])$") or match(p, "^r(1?[0-9])$") then - op = op + parse_gpr(p) - else - if op < 0xe0000000 then werror("unconditional instruction") end - local mode, n, s = parse_label(p, false) - waction("REL_"..mode, n, s, 1) - op = 0xfa000000 - end - elseif p == "n" then - local r, wb = match(params[n], "^([^!]*)(!?)$") - op = op + parse_gpr(r) * 65536 + (wb == "!" and 0x00200000 or 0) - n = n + 1 - elseif p == "R" then - op = op + parse_reglist(params[n]); n = n + 1 - elseif p == "W" then - op = op + parse_imm16(params[n]); n = n + 1 - elseif p == "v" then - op = op + parse_imm(params[n], 5, 7, 0, false); n = n + 1 - elseif p == "w" then - local imm = match(params[n], "^#(.*)$") - if imm then - op = op + parse_imm(params[n], 5, 7, 0, false); n = n + 1 - else - op = op + parse_gpr(params[n]) * 256 + 16 - end - elseif p == "X" then - op = op + parse_imm(params[n], 5, 16, 0, false); n = n + 1 - elseif p == "K" then - local imm = tonumber(match(params[n], "^#(.*)$")); n = n + 1 - if not imm or imm % 1 ~= 0 or imm < 0 or imm > 0xffff then - werror("bad immediate operand") - end - local t = imm % 16 - op = op + (imm - t) * 16 + t - elseif p == "T" then - op = op + parse_imm(params[n], 24, 0, 0, false); n = n + 1 - elseif p == "s" then - -- Ignored. - else - assert(false) - end - end - wputpos(pos, op) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode to mark the position where the action list is to be emitted. -map_op[".actionlist_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeactions(out, name) end) -end - --- Pseudo-opcode to mark the position where the global enum is to be emitted. -map_op[".globals_1"] = function(params) - if not params then return "prefix" end - local prefix = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobals(out, prefix) end) -end - --- Pseudo-opcode to mark the position where the global names are to be emitted. -map_op[".globalnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobalnames(out, name) end) -end - --- Pseudo-opcode to mark the position where the extern names are to be emitted. -map_op[".externnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeexternnames(out, name) end) -end - ------------------------------------------------------------------------------- - --- Label pseudo-opcode (converted from trailing colon form). -map_op[".label_1"] = function(params) - if not params then return "[1-9] | ->global | =>pcexpr" end - if secpos+1 > maxsecpos then wflush() end - local mode, n, s = parse_label(params[1], true) - if mode == "EXT" then werror("bad label definition") end - waction("LABEL_"..mode, n, s, 1) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcodes for data storage. -map_op[".long_*"] = function(params) - if not params then return "imm..." end - for _,p in ipairs(params) do - local n = tonumber(p) - if not n then werror("bad immediate `"..p.."'") end - if n < 0 then n = n + 2^32 end - wputw(n) - if secpos+2 > maxsecpos then wflush() end - end -end - --- Alignment pseudo-opcode. -map_op[".align_1"] = function(params) - if not params then return "numpow2" end - if secpos+1 > maxsecpos then wflush() end - local align = tonumber(params[1]) - if align then - local x = align - -- Must be a power of 2 in the range (2 ... 256). - for i=1,8 do - x = x / 2 - if x == 1 then - waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1. - return - end - end - end - werror("bad alignment") -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode for (primitive) type definitions (map to C types). -map_op[".type_3"] = function(params, nparams) - if not params then - return nparams == 2 and "name, ctype" or "name, ctype, reg" - end - local name, ctype, reg = params[1], params[2], params[3] - if not match(name, "^[%a_][%w_]*$") then - werror("bad type name `"..name.."'") - end - local tp = map_type[name] - if tp then - werror("duplicate type `"..name.."'") - end - -- Add #type to defines. A bit unclean to put it in map_archdef. - map_archdef["#"..name] = "sizeof("..ctype..")" - -- Add new type and emit shortcut define. - local num = ctypenum + 1 - map_type[name] = { - ctype = ctype, - ctypefmt = format("Dt%X(%%s)", num), - reg = reg, - } - wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype)) - ctypenum = num -end -map_op[".type_2"] = map_op[".type_3"] - --- Dump type definitions. -local function dumptypes(out, lvl) - local t = {} - for name in pairs(map_type) do t[#t+1] = name end - sort(t) - out:write("Type definitions:\n") - for _,name in ipairs(t) do - local tp = map_type[name] - local reg = tp.reg or "" - out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Set the current section. -function _M.section(num) - waction("SECTION", num) - wflush(true) -- SECTION is a terminal action. -end - ------------------------------------------------------------------------------- - --- Dump architecture description. -function _M.dumparch(out) - out:write(format("DynASM %s version %s, released %s\n\n", - _info.arch, _info.version, _info.release)) - dumpactions(out) -end - --- Dump all user defined elements. -function _M.dumpdef(out, lvl) - dumptypes(out, lvl) - dumpglobals(out, lvl) - dumpexterns(out, lvl) -end - ------------------------------------------------------------------------------- - --- Pass callbacks from/to the DynASM core. -function _M.passcb(wl, we, wf, ww) - wline, werror, wfatal, wwarn = wl, we, wf, ww - return wflush -end - --- Setup the arch-specific module. -function _M.setup(arch, opt) - g_arch, g_opt = arch, opt -end - --- Merge the core maps and the arch-specific maps. -function _M.mergemaps(map_coreop, map_def) - setmetatable(map_op, { __index = function(t, k) - local v = map_coreop[k] - if v then return v end - local cc = sub(k, -4, -3) - local cv = map_cond[cc] - if cv then - local v = rawget(t, sub(k, 1, -5)..sub(k, -2)) - if type(v) == "string" then return format("%x%s", cv, sub(v, 2)) end - end - end }) - setmetatable(map_def, { __index = map_archdef }) - return map_op, map_def -end - -return _M - ------------------------------------------------------------------------------- - diff --git a/third_party/dynasm/dasm_mips.h b/third_party/dynasm/dasm_mips.h deleted file mode 100644 index af87d99..0000000 --- a/third_party/dynasm/dasm_mips.h +++ /dev/null @@ -1,415 +0,0 @@ -/* -** DynASM MIPS encoding engine. -** Copyright (C) 2005-2012 Mike Pall. All rights reserved. -** Released under the MIT license. See dynasm.lua for full copyright notice. -*/ - -#include -#include -#include -#include - -#define DASM_ARCH "mips" - -#ifndef DASM_EXTERN -#define DASM_EXTERN(a,b,c,d) 0 -#endif - -/* Action definitions. */ -enum { - DASM_STOP, DASM_SECTION, DASM_ESC, DASM_REL_EXT, - /* The following actions need a buffer position. */ - DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG, - /* The following actions also have an argument. */ - DASM_REL_PC, DASM_LABEL_PC, DASM_IMM, - DASM__MAX -}; - -/* Maximum number of section buffer positions for a single dasm_put() call. */ -#define DASM_MAXSECPOS 25 - -/* DynASM encoder status codes. Action list offset or number are or'ed in. */ -#define DASM_S_OK 0x00000000 -#define DASM_S_NOMEM 0x01000000 -#define DASM_S_PHASE 0x02000000 -#define DASM_S_MATCH_SEC 0x03000000 -#define DASM_S_RANGE_I 0x11000000 -#define DASM_S_RANGE_SEC 0x12000000 -#define DASM_S_RANGE_LG 0x13000000 -#define DASM_S_RANGE_PC 0x14000000 -#define DASM_S_RANGE_REL 0x15000000 -#define DASM_S_UNDEF_LG 0x21000000 -#define DASM_S_UNDEF_PC 0x22000000 - -/* Macros to convert positions (8 bit section + 24 bit index). */ -#define DASM_POS2IDX(pos) ((pos)&0x00ffffff) -#define DASM_POS2BIAS(pos) ((pos)&0xff000000) -#define DASM_SEC2POS(sec) ((sec)<<24) -#define DASM_POS2SEC(pos) ((pos)>>24) -#define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos)) - -/* Action list type. */ -typedef const unsigned int *dasm_ActList; - -/* Per-section structure. */ -typedef struct dasm_Section { - int *rbuf; /* Biased buffer pointer (negative section bias). */ - int *buf; /* True buffer pointer. */ - size_t bsize; /* Buffer size in bytes. */ - int pos; /* Biased buffer position. */ - int epos; /* End of biased buffer position - max single put. */ - int ofs; /* Byte offset into section. */ -} dasm_Section; - -/* Core structure holding the DynASM encoding state. */ -struct dasm_State { - size_t psize; /* Allocated size of this structure. */ - dasm_ActList actionlist; /* Current actionlist pointer. */ - int *lglabels; /* Local/global chain/pos ptrs. */ - size_t lgsize; - int *pclabels; /* PC label chains/pos ptrs. */ - size_t pcsize; - void **globals; /* Array of globals (bias -10). */ - dasm_Section *section; /* Pointer to active section. */ - size_t codesize; /* Total size of all code sections. */ - int maxsection; /* 0 <= sectionidx < maxsection. */ - int status; /* Status code. */ - dasm_Section sections[1]; /* All sections. Alloc-extended. */ -}; - -/* The size of the core structure depends on the max. number of sections. */ -#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section)) - - -/* Initialize DynASM state. */ -void dasm_init(Dst_DECL, int maxsection) -{ - dasm_State *D; - size_t psz = 0; - int i; - Dst_REF = NULL; - DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection)); - D = Dst_REF; - D->psize = psz; - D->lglabels = NULL; - D->lgsize = 0; - D->pclabels = NULL; - D->pcsize = 0; - D->globals = NULL; - D->maxsection = maxsection; - for (i = 0; i < maxsection; i++) { - D->sections[i].buf = NULL; /* Need this for pass3. */ - D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); - D->sections[i].bsize = 0; - D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ - } -} - -/* Free DynASM state. */ -void dasm_free(Dst_DECL) -{ - dasm_State *D = Dst_REF; - int i; - for (i = 0; i < D->maxsection; i++) - if (D->sections[i].buf) - DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize); - if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize); - if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize); - DASM_M_FREE(Dst, D, D->psize); -} - -/* Setup global label array. Must be called before dasm_setup(). */ -void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl) -{ - dasm_State *D = Dst_REF; - D->globals = gl - 10; /* Negative bias to compensate for locals. */ - DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int)); -} - -/* Grow PC label array. Can be called after dasm_setup(), too. */ -void dasm_growpc(Dst_DECL, unsigned int maxpc) -{ - dasm_State *D = Dst_REF; - size_t osz = D->pcsize; - DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int)); - memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz); -} - -/* Setup encoder. */ -void dasm_setup(Dst_DECL, const void *actionlist) -{ - dasm_State *D = Dst_REF; - int i; - D->actionlist = (dasm_ActList)actionlist; - D->status = DASM_S_OK; - D->section = &D->sections[0]; - memset((void *)D->lglabels, 0, D->lgsize); - if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize); - for (i = 0; i < D->maxsection; i++) { - D->sections[i].pos = DASM_SEC2POS(i); - D->sections[i].ofs = 0; - } -} - - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) { \ - D->status = DASM_S_##st|(p-D->actionlist-1); return; } } while (0) -#define CKPL(kind, st) \ - do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \ - D->status = DASM_S_RANGE_##st|(p-D->actionlist-1); return; } } while (0) -#else -#define CK(x, st) ((void)0) -#define CKPL(kind, st) ((void)0) -#endif - -/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */ -void dasm_put(Dst_DECL, int start, ...) -{ - va_list ap; - dasm_State *D = Dst_REF; - dasm_ActList p = D->actionlist + start; - dasm_Section *sec = D->section; - int pos = sec->pos, ofs = sec->ofs; - int *b; - - if (pos >= sec->epos) { - DASM_M_GROW(Dst, int, sec->buf, sec->bsize, - sec->bsize + 2*DASM_MAXSECPOS*sizeof(int)); - sec->rbuf = sec->buf - DASM_POS2BIAS(pos); - sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos); - } - - b = sec->rbuf; - b[pos++] = start; - - va_start(ap, start); - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16) - 0xff00; - if (action >= DASM__MAX) { - ofs += 4; - } else { - int *pl, n = action >= DASM_REL_PC ? va_arg(ap, int) : 0; - switch (action) { - case DASM_STOP: goto stop; - case DASM_SECTION: - n = (ins & 255); CK(n < D->maxsection, RANGE_SEC); - D->section = &D->sections[n]; goto stop; - case DASM_ESC: p++; ofs += 4; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs += (ins & 255); b[pos++] = ofs; break; - case DASM_REL_LG: - n = (ins & 2047) - 10; pl = D->lglabels + n; - if (n >= 0) { CKPL(lg, LG); goto putrel; } /* Bkwd rel or global. */ - pl += 10; n = *pl; - if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */ - goto linkrel; - case DASM_REL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putrel: - n = *pl; - if (n < 0) { /* Label exists. Get label pos and store it. */ - b[pos] = -n; - } else { - linkrel: - b[pos] = n; /* Else link to rel chain, anchored at label. */ - *pl = pos; - } - pos++; - break; - case DASM_LABEL_LG: - pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel; - case DASM_LABEL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putlabel: - n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; - } - *pl = -pos; /* Label exists now. */ - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_IMM: -#ifdef DASM_CHECKS - CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I); -#endif - n >>= ((ins>>10)&31); -#ifdef DASM_CHECKS - if (ins & 0x8000) - CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I); - else - CK((n>>((ins>>5)&31)) == 0, RANGE_I); -#endif - b[pos++] = n; - break; - } - } - } -stop: - va_end(ap); - sec->pos = pos; - sec->ofs = ofs; -} -#undef CK - -/* Pass 2: Link sections, shrink aligns, fix label offsets. */ -int dasm_link(Dst_DECL, size_t *szp) -{ - dasm_State *D = Dst_REF; - int secnum; - int ofs = 0; - -#ifdef DASM_CHECKS - *szp = 0; - if (D->status != DASM_S_OK) return D->status; - { - int pc; - for (pc = 0; pc*sizeof(int) < D->pcsize; pc++) - if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc; - } -#endif - - { /* Handle globals not defined in this translation unit. */ - int idx; - for (idx = 20; idx*sizeof(int) < D->lgsize; idx++) { - int n = D->lglabels[idx]; - /* Undefined label: Collapse rel chain and replace with marker (< 0). */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; } - } - } - - /* Combine all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->rbuf; - int pos = DASM_SEC2POS(secnum); - int lastpos = sec->pos; - - while (pos != lastpos) { - dasm_ActList p = D->actionlist + b[pos++]; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16) - 0xff00; - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: p++; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break; - case DASM_REL_LG: case DASM_REL_PC: pos++; break; - case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break; - case DASM_IMM: pos++; break; - } - } - stop: (void)0; - } - ofs += sec->ofs; /* Next section starts right after current section. */ - } - - D->codesize = ofs; /* Total size of all code sections */ - *szp = ofs; - return DASM_S_OK; -} - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) return DASM_S_##st|(p-D->actionlist-1); } while (0) -#else -#define CK(x, st) ((void)0) -#endif - -/* Pass 3: Encode sections. */ -int dasm_encode(Dst_DECL, void *buffer) -{ - dasm_State *D = Dst_REF; - char *base = (char *)buffer; - unsigned int *cp = (unsigned int *)buffer; - int secnum; - - /* Encode all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->buf; - int *endb = sec->rbuf + sec->pos; - - while (b != endb) { - dasm_ActList p = D->actionlist + *b++; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16) - 0xff00; - int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0; - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: *cp++ = *p++; break; - case DASM_REL_EXT: - n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins & 2047), 1); - goto patchrel; - case DASM_ALIGN: - ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0x60000000; - break; - case DASM_REL_LG: - CK(n >= 0, UNDEF_LG); - case DASM_REL_PC: - CK(n >= 0, UNDEF_PC); - n = *DASM_POS2PTR(D, n); - if (ins & 2048) - n = n - (int)((char *)cp - base); - else - n = (n + (int)base) & 0x0fffffff; - patchrel: - CK((n & 3) == 0 && - ((n + ((ins & 2048) ? 0x00020000 : 0)) >> - ((ins & 2048) ? 18 : 28)) == 0, RANGE_REL); - cp[-1] |= ((n>>2) & ((ins & 2048) ? 0x0000ffff: 0x03ffffff)); - break; - case DASM_LABEL_LG: - ins &= 2047; if (ins >= 20) D->globals[ins-10] = (void *)(base + n); - break; - case DASM_LABEL_PC: break; - case DASM_IMM: - cp[-1] |= (n & ((1<<((ins>>5)&31))-1)) << (ins&31); - break; - default: *cp++ = ins; break; - } - } - stop: (void)0; - } - } - - if (base + D->codesize != (char *)cp) /* Check for phase errors. */ - return DASM_S_PHASE; - return DASM_S_OK; -} -#undef CK - -/* Get PC label offset. */ -int dasm_getpclabel(Dst_DECL, unsigned int pc) -{ - dasm_State *D = Dst_REF; - if (pc*sizeof(int) < D->pcsize) { - int pos = D->pclabels[pc]; - if (pos < 0) return *DASM_POS2PTR(D, -pos); - if (pos > 0) return -1; /* Undefined. */ - } - return -2; /* Unused or out of range. */ -} - -#ifdef DASM_CHECKS -/* Optional sanity checker to call between isolated encoding steps. */ -int dasm_checkstep(Dst_DECL, int secmatch) -{ - dasm_State *D = Dst_REF; - if (D->status == DASM_S_OK) { - int i; - for (i = 1; i <= 9; i++) { - if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_LG|i; break; } - D->lglabels[i] = 0; - } - } - if (D->status == DASM_S_OK && secmatch >= 0 && - D->section != &D->sections[secmatch]) - D->status = DASM_S_MATCH_SEC|(D->section-D->sections); - return D->status; -} -#endif - diff --git a/third_party/dynasm/dasm_mips.lua b/third_party/dynasm/dasm_mips.lua deleted file mode 100644 index aa33f0c..0000000 --- a/third_party/dynasm/dasm_mips.lua +++ /dev/null @@ -1,959 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM MIPS module. --- --- Copyright (C) 2005-2012 Mike Pall. All rights reserved. --- See dynasm.lua for full copyright notice. ------------------------------------------------------------------------------- - --- Module information: -local _info = { - arch = "mips", - description = "DynASM MIPS module", - version = "1.3.0", - vernum = 10300, - release = "2012-01-23", - author = "Mike Pall", - license = "MIT", -} - --- Exported glue functions for the arch-specific module. -local _M = { _info = _info } - --- Cache library functions. -local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs -local assert, setmetatable = assert, setmetatable -local _s = string -local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char -local match, gmatch = _s.match, _s.gmatch -local concat, sort = table.concat, table.sort - --- Inherited tables and callbacks. -local g_opt, g_arch -local wline, werror, wfatal, wwarn - --- Action name list. --- CHECK: Keep this in sync with the C code! -local action_names = { - "STOP", "SECTION", "ESC", "REL_EXT", - "ALIGN", "REL_LG", "LABEL_LG", - "REL_PC", "LABEL_PC", "IMM", -} - --- Maximum number of section buffer positions for dasm_put(). --- CHECK: Keep this in sync with the C code! -local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines. - --- Action name -> action number. -local map_action = {} -for n,name in ipairs(action_names) do - map_action[name] = n-1 -end - --- Action list buffer. -local actlist = {} - --- Argument list for next dasm_put(). Start with offset 0 into action list. -local actargs = { 0 } - --- Current number of section buffer positions for dasm_put(). -local secpos = 1 - ------------------------------------------------------------------------------- - --- Return 8 digit hex number. -local function tohex(x) - return sub(format("%08x", x), -8) -- Avoid 64 bit portability problem in Lua. -end - --- Dump action names and numbers. -local function dumpactions(out) - out:write("DynASM encoding engine action codes:\n") - for n,name in ipairs(action_names) do - local num = map_action[name] - out:write(format(" %-10s %02X %d\n", name, num, num)) - end - out:write("\n") -end - --- Write action list buffer as a huge static C array. -local function writeactions(out, name) - local nn = #actlist - if nn == 0 then nn = 1; actlist[0] = map_action.STOP end - out:write("static const unsigned int ", name, "[", nn, "] = {\n") - for i = 1,nn-1 do - assert(out:write("0x", tohex(actlist[i]), ",\n")) - end - assert(out:write("0x", tohex(actlist[nn]), "\n};\n\n")) -end - ------------------------------------------------------------------------------- - --- Add word to action list. -local function wputxw(n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - actlist[#actlist+1] = n -end - --- Add action to list with optional arg. Advance buffer pos, too. -local function waction(action, val, a, num) - local w = assert(map_action[action], "bad action name `"..action.."'") - wputxw(0xff000000 + w * 0x10000 + (val or 0)) - if a then actargs[#actargs+1] = a end - if a or num then secpos = secpos + (num or 1) end -end - --- Flush action list (intervening C code or buffer pos overflow). -local function wflush(term) - if #actlist == actargs[1] then return end -- Nothing to flush. - if not term then waction("STOP") end -- Terminate action list. - wline(format("dasm_put(Dst, %s);", concat(actargs, ", ")), true) - actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put(). - secpos = 1 -- The actionlist offset occupies a buffer position, too. -end - --- Put escaped word. -local function wputw(n) - if n >= 0xff000000 then waction("ESC") end - wputxw(n) -end - --- Reserve position for word. -local function wpos() - local pos = #actlist+1 - actlist[pos] = "" - return pos -end - --- Store word to reserved position. -local function wputpos(pos, n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - actlist[pos] = n -end - ------------------------------------------------------------------------------- - --- Global label name -> global label number. With auto assignment on 1st use. -local next_global = 20 -local map_global = setmetatable({}, { __index = function(t, name) - if not match(name, "^[%a_][%w_]*$") then werror("bad global label") end - local n = next_global - if n > 2047 then werror("too many global labels") end - next_global = n + 1 - t[name] = n - return n -end}) - --- Dump global labels. -local function dumpglobals(out, lvl) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("Global labels:\n") - for i=20,next_global-1 do - out:write(format(" %s\n", t[i])) - end - out:write("\n") -end - --- Write global label enum. -local function writeglobals(out, prefix) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("enum {\n") - for i=20,next_global-1 do - out:write(" ", prefix, t[i], ",\n") - end - out:write(" ", prefix, "_MAX\n};\n") -end - --- Write global label names. -local function writeglobalnames(out, name) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("static const char *const ", name, "[] = {\n") - for i=20,next_global-1 do - out:write(" \"", t[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Extern label name -> extern label number. With auto assignment on 1st use. -local next_extern = 0 -local map_extern_ = {} -local map_extern = setmetatable({}, { __index = function(t, name) - -- No restrictions on the name for now. - local n = next_extern - if n > 2047 then werror("too many extern labels") end - next_extern = n + 1 - t[name] = n - map_extern_[n] = name - return n -end}) - --- Dump extern labels. -local function dumpexterns(out, lvl) - out:write("Extern labels:\n") - for i=0,next_extern-1 do - out:write(format(" %s\n", map_extern_[i])) - end - out:write("\n") -end - --- Write extern label names. -local function writeexternnames(out, name) - out:write("static const char *const ", name, "[] = {\n") - for i=0,next_extern-1 do - out:write(" \"", map_extern_[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Arch-specific maps. -local map_archdef = { sp="r29", ra="r31" } -- Ext. register name -> int. name. - -local map_type = {} -- Type name -> { ctype, reg } -local ctypenum = 0 -- Type number (for Dt... macros). - --- Reverse defines for registers. -function _M.revdef(s) - if s == "r29" then return "sp" - elseif s == "r31" then return "ra" end - return s -end - ------------------------------------------------------------------------------- - --- Template strings for MIPS instructions. -local map_op = { - -- First-level opcodes. - j_1 = "08000000J", - jal_1 = "0c000000J", - b_1 = "10000000B", - beqz_2 = "10000000SB", - beq_3 = "10000000STB", - bnez_2 = "14000000SB", - bne_3 = "14000000STB", - blez_2 = "18000000SB", - bgtz_2 = "1c000000SB", - addi_3 = "20000000TSI", - li_2 = "24000000TI", - addiu_3 = "24000000TSI", - slti_3 = "28000000TSI", - sltiu_3 = "2c000000TSI", - andi_3 = "30000000TSU", - lu_2 = "34000000TU", - ori_3 = "34000000TSU", - xori_3 = "38000000TSU", - lui_2 = "3c000000TU", - beqzl_2 = "50000000SB", - beql_3 = "50000000STB", - bnezl_2 = "54000000SB", - bnel_3 = "54000000STB", - blezl_2 = "58000000SB", - bgtzl_2 = "5c000000SB", - lb_2 = "80000000TO", - lh_2 = "84000000TO", - lwl_2 = "88000000TO", - lw_2 = "8c000000TO", - lbu_2 = "90000000TO", - lhu_2 = "94000000TO", - lwr_2 = "98000000TO", - sb_2 = "a0000000TO", - sh_2 = "a4000000TO", - swl_2 = "a8000000TO", - sw_2 = "ac000000TO", - swr_2 = "b8000000TO", - cache_2 = "bc000000NO", - ll_2 = "c0000000TO", - lwc1_2 = "c4000000HO", - pref_2 = "cc000000NO", - ldc1_2 = "d4000000HO", - sc_2 = "e0000000TO", - swc1_2 = "e4000000HO", - sdc1_2 = "f4000000HO", - - -- Opcode SPECIAL. - nop_0 = "00000000", - sll_3 = "00000000DTA", - movf_2 = "00000001DS", - movf_3 = "00000001DSC", - movt_2 = "00010001DS", - movt_3 = "00010001DSC", - srl_3 = "00000002DTA", - rotr_3 = "00200002DTA", - sra_3 = "00000003DTA", - sllv_3 = "00000004DTS", - srlv_3 = "00000006DTS", - rotrv_3 = "00000046DTS", - srav_3 = "00000007DTS", - jr_1 = "00000008S", - jalr_1 = "0000f809S", - jalr_2 = "00000009DS", - movz_3 = "0000000aDST", - movn_3 = "0000000bDST", - syscall_0 = "0000000c", - syscall_1 = "0000000cY", - break_0 = "0000000d", - break_1 = "0000000dY", - sync_0 = "0000000f", - mfhi_1 = "00000010D", - mthi_1 = "00000011S", - mflo_1 = "00000012D", - mtlo_1 = "00000013S", - mult_2 = "00000018ST", - multu_2 = "00000019ST", - div_2 = "0000001aST", - divu_2 = "0000001bST", - add_3 = "00000020DST", - move_2 = "00000021DS", - addu_3 = "00000021DST", - sub_3 = "00000022DST", - negu_2 = "00000023DT", - subu_3 = "00000023DST", - and_3 = "00000024DST", - or_3 = "00000025DST", - xor_3 = "00000026DST", - not_2 = "00000027DS", - nor_3 = "00000027DST", - slt_3 = "0000002aDST", - sltu_3 = "0000002bDST", - tge_2 = "00000030ST", - tge_3 = "00000030STZ", - tgeu_2 = "00000031ST", - tgeu_3 = "00000031STZ", - tlt_2 = "00000032ST", - tlt_3 = "00000032STZ", - tltu_2 = "00000033ST", - tltu_3 = "00000033STZ", - teq_2 = "00000034ST", - teq_3 = "00000034STZ", - tne_2 = "00000036ST", - tne_3 = "00000036STZ", - - -- Opcode REGIMM. - bltz_2 = "04000000SB", - bgez_2 = "04010000SB", - bltzl_2 = "04020000SB", - bgezl_2 = "04030000SB", - tgei_2 = "04080000SI", - tgeiu_2 = "04090000SI", - tlti_2 = "040a0000SI", - tltiu_2 = "040b0000SI", - teqi_2 = "040c0000SI", - tnei_2 = "040e0000SI", - bltzal_2 = "04100000SB", - bal_1 = "04110000B", - bgezal_2 = "04110000SB", - bltzall_2 = "04120000SB", - bgezall_2 = "04130000SB", - synci_1 = "041f0000O", - - -- Opcode SPECIAL2. - madd_2 = "70000000ST", - maddu_2 = "70000001ST", - mul_3 = "70000002DST", - msub_2 = "70000004ST", - msubu_2 = "70000005ST", - clz_2 = "70000020DS=", - clo_2 = "70000021DS=", - sdbbp_0 = "7000003f", - sdbbp_1 = "7000003fY", - - -- Opcode SPECIAL3. - ext_4 = "7c000000TSAM", -- Note: last arg is msbd = size-1 - ins_4 = "7c000004TSAM", -- Note: last arg is msb = pos+size-1 - wsbh_2 = "7c0000a0DT", - seb_2 = "7c000420DT", - seh_2 = "7c000620DT", - rdhwr_2 = "7c00003bTD", - - -- Opcode COP0. - mfc0_2 = "40000000TD", - mfc0_3 = "40000000TDW", - mtc0_2 = "40800000TD", - mtc0_3 = "40800000TDW", - rdpgpr_2 = "41400000DT", - di_0 = "41606000", - di_1 = "41606000T", - ei_0 = "41606020", - ei_1 = "41606020T", - wrpgpr_2 = "41c00000DT", - tlbr_0 = "42000001", - tlbwi_0 = "42000002", - tlbwr_0 = "42000006", - tlbp_0 = "42000008", - eret_0 = "42000018", - deret_0 = "4200001f", - wait_0 = "42000020", - - -- Opcode COP1. - mfc1_2 = "44000000TG", - cfc1_2 = "44400000TG", - mfhc1_2 = "44600000TG", - mtc1_2 = "44800000TG", - ctc1_2 = "44c00000TG", - mthc1_2 = "44e00000TG", - - bc1f_1 = "45000000B", - bc1f_2 = "45000000CB", - bc1t_1 = "45010000B", - bc1t_2 = "45010000CB", - bc1fl_1 = "45020000B", - bc1fl_2 = "45020000CB", - bc1tl_1 = "45030000B", - bc1tl_2 = "45030000CB", - - ["add.s_3"] = "46000000FGH", - ["sub.s_3"] = "46000001FGH", - ["mul.s_3"] = "46000002FGH", - ["div.s_3"] = "46000003FGH", - ["sqrt.s_2"] = "46000004FG", - ["abs.s_2"] = "46000005FG", - ["mov.s_2"] = "46000006FG", - ["neg.s_2"] = "46000007FG", - ["round.l.s_2"] = "46000008FG", - ["trunc.l.s_2"] = "46000009FG", - ["ceil.l.s_2"] = "4600000aFG", - ["floor.l.s_2"] = "4600000bFG", - ["round.w.s_2"] = "4600000cFG", - ["trunc.w.s_2"] = "4600000dFG", - ["ceil.w.s_2"] = "4600000eFG", - ["floor.w.s_2"] = "4600000fFG", - ["movf.s_2"] = "46000011FG", - ["movf.s_3"] = "46000011FGC", - ["movt.s_2"] = "46010011FG", - ["movt.s_3"] = "46010011FGC", - ["movz.s_3"] = "46000012FGT", - ["movn.s_3"] = "46000013FGT", - ["recip.s_2"] = "46000015FG", - ["rsqrt.s_2"] = "46000016FG", - ["cvt.d.s_2"] = "46000021FG", - ["cvt.w.s_2"] = "46000024FG", - ["cvt.l.s_2"] = "46000025FG", - ["cvt.ps.s_3"] = "46000026FGH", - ["c.f.s_2"] = "46000030GH", - ["c.f.s_3"] = "46000030VGH", - ["c.un.s_2"] = "46000031GH", - ["c.un.s_3"] = "46000031VGH", - ["c.eq.s_2"] = "46000032GH", - ["c.eq.s_3"] = "46000032VGH", - ["c.ueq.s_2"] = "46000033GH", - ["c.ueq.s_3"] = "46000033VGH", - ["c.olt.s_2"] = "46000034GH", - ["c.olt.s_3"] = "46000034VGH", - ["c.ult.s_2"] = "46000035GH", - ["c.ult.s_3"] = "46000035VGH", - ["c.ole.s_2"] = "46000036GH", - ["c.ole.s_3"] = "46000036VGH", - ["c.ule.s_2"] = "46000037GH", - ["c.ule.s_3"] = "46000037VGH", - ["c.sf.s_2"] = "46000038GH", - ["c.sf.s_3"] = "46000038VGH", - ["c.ngle.s_2"] = "46000039GH", - ["c.ngle.s_3"] = "46000039VGH", - ["c.seq.s_2"] = "4600003aGH", - ["c.seq.s_3"] = "4600003aVGH", - ["c.ngl.s_2"] = "4600003bGH", - ["c.ngl.s_3"] = "4600003bVGH", - ["c.lt.s_2"] = "4600003cGH", - ["c.lt.s_3"] = "4600003cVGH", - ["c.nge.s_2"] = "4600003dGH", - ["c.nge.s_3"] = "4600003dVGH", - ["c.le.s_2"] = "4600003eGH", - ["c.le.s_3"] = "4600003eVGH", - ["c.ngt.s_2"] = "4600003fGH", - ["c.ngt.s_3"] = "4600003fVGH", - - ["add.d_3"] = "46200000FGH", - ["sub.d_3"] = "46200001FGH", - ["mul.d_3"] = "46200002FGH", - ["div.d_3"] = "46200003FGH", - ["sqrt.d_2"] = "46200004FG", - ["abs.d_2"] = "46200005FG", - ["mov.d_2"] = "46200006FG", - ["neg.d_2"] = "46200007FG", - ["round.l.d_2"] = "46200008FG", - ["trunc.l.d_2"] = "46200009FG", - ["ceil.l.d_2"] = "4620000aFG", - ["floor.l.d_2"] = "4620000bFG", - ["round.w.d_2"] = "4620000cFG", - ["trunc.w.d_2"] = "4620000dFG", - ["ceil.w.d_2"] = "4620000eFG", - ["floor.w.d_2"] = "4620000fFG", - ["movf.d_2"] = "46200011FG", - ["movf.d_3"] = "46200011FGC", - ["movt.d_2"] = "46210011FG", - ["movt.d_3"] = "46210011FGC", - ["movz.d_3"] = "46200012FGT", - ["movn.d_3"] = "46200013FGT", - ["recip.d_2"] = "46200015FG", - ["rsqrt.d_2"] = "46200016FG", - ["cvt.s.d_2"] = "46200020FG", - ["cvt.w.d_2"] = "46200024FG", - ["cvt.l.d_2"] = "46200025FG", - ["c.f.d_2"] = "46200030GH", - ["c.f.d_3"] = "46200030VGH", - ["c.un.d_2"] = "46200031GH", - ["c.un.d_3"] = "46200031VGH", - ["c.eq.d_2"] = "46200032GH", - ["c.eq.d_3"] = "46200032VGH", - ["c.ueq.d_2"] = "46200033GH", - ["c.ueq.d_3"] = "46200033VGH", - ["c.olt.d_2"] = "46200034GH", - ["c.olt.d_3"] = "46200034VGH", - ["c.ult.d_2"] = "46200035GH", - ["c.ult.d_3"] = "46200035VGH", - ["c.ole.d_2"] = "46200036GH", - ["c.ole.d_3"] = "46200036VGH", - ["c.ule.d_2"] = "46200037GH", - ["c.ule.d_3"] = "46200037VGH", - ["c.sf.d_2"] = "46200038GH", - ["c.sf.d_3"] = "46200038VGH", - ["c.ngle.d_2"] = "46200039GH", - ["c.ngle.d_3"] = "46200039VGH", - ["c.seq.d_2"] = "4620003aGH", - ["c.seq.d_3"] = "4620003aVGH", - ["c.ngl.d_2"] = "4620003bGH", - ["c.ngl.d_3"] = "4620003bVGH", - ["c.lt.d_2"] = "4620003cGH", - ["c.lt.d_3"] = "4620003cVGH", - ["c.nge.d_2"] = "4620003dGH", - ["c.nge.d_3"] = "4620003dVGH", - ["c.le.d_2"] = "4620003eGH", - ["c.le.d_3"] = "4620003eVGH", - ["c.ngt.d_2"] = "4620003fGH", - ["c.ngt.d_3"] = "4620003fVGH", - - ["add.ps_3"] = "46c00000FGH", - ["sub.ps_3"] = "46c00001FGH", - ["mul.ps_3"] = "46c00002FGH", - ["abs.ps_2"] = "46c00005FG", - ["mov.ps_2"] = "46c00006FG", - ["neg.ps_2"] = "46c00007FG", - ["movf.ps_2"] = "46c00011FG", - ["movf.ps_3"] = "46c00011FGC", - ["movt.ps_2"] = "46c10011FG", - ["movt.ps_3"] = "46c10011FGC", - ["movz.ps_3"] = "46c00012FGT", - ["movn.ps_3"] = "46c00013FGT", - ["cvt.s.pu_2"] = "46c00020FG", - ["cvt.s.pl_2"] = "46c00028FG", - ["pll.ps_3"] = "46c0002cFGH", - ["plu.ps_3"] = "46c0002dFGH", - ["pul.ps_3"] = "46c0002eFGH", - ["puu.ps_3"] = "46c0002fFGH", - ["c.f.ps_2"] = "46c00030GH", - ["c.f.ps_3"] = "46c00030VGH", - ["c.un.ps_2"] = "46c00031GH", - ["c.un.ps_3"] = "46c00031VGH", - ["c.eq.ps_2"] = "46c00032GH", - ["c.eq.ps_3"] = "46c00032VGH", - ["c.ueq.ps_2"] = "46c00033GH", - ["c.ueq.ps_3"] = "46c00033VGH", - ["c.olt.ps_2"] = "46c00034GH", - ["c.olt.ps_3"] = "46c00034VGH", - ["c.ult.ps_2"] = "46c00035GH", - ["c.ult.ps_3"] = "46c00035VGH", - ["c.ole.ps_2"] = "46c00036GH", - ["c.ole.ps_3"] = "46c00036VGH", - ["c.ule.ps_2"] = "46c00037GH", - ["c.ule.ps_3"] = "46c00037VGH", - ["c.sf.ps_2"] = "46c00038GH", - ["c.sf.ps_3"] = "46c00038VGH", - ["c.ngle.ps_2"] = "46c00039GH", - ["c.ngle.ps_3"] = "46c00039VGH", - ["c.seq.ps_2"] = "46c0003aGH", - ["c.seq.ps_3"] = "46c0003aVGH", - ["c.ngl.ps_2"] = "46c0003bGH", - ["c.ngl.ps_3"] = "46c0003bVGH", - ["c.lt.ps_2"] = "46c0003cGH", - ["c.lt.ps_3"] = "46c0003cVGH", - ["c.nge.ps_2"] = "46c0003dGH", - ["c.nge.ps_3"] = "46c0003dVGH", - ["c.le.ps_2"] = "46c0003eGH", - ["c.le.ps_3"] = "46c0003eVGH", - ["c.ngt.ps_2"] = "46c0003fGH", - ["c.ngt.ps_3"] = "46c0003fVGH", - - ["cvt.s.w_2"] = "46800020FG", - ["cvt.d.w_2"] = "46800021FG", - - ["cvt.s.l_2"] = "46a00020FG", - ["cvt.d.l_2"] = "46a00021FG", - - -- Opcode COP1X. - lwxc1_2 = "4c000000FX", - ldxc1_2 = "4c000001FX", - luxc1_2 = "4c000005FX", - swxc1_2 = "4c000008FX", - sdxc1_2 = "4c000009FX", - suxc1_2 = "4c00000dFX", - prefx_2 = "4c00000fMX", - ["alnv.ps_4"] = "4c00001eFGHS", - ["madd.s_4"] = "4c000020FRGH", - ["madd.d_4"] = "4c000021FRGH", - ["madd.ps_4"] = "4c000026FRGH", - ["msub.s_4"] = "4c000028FRGH", - ["msub.d_4"] = "4c000029FRGH", - ["msub.ps_4"] = "4c00002eFRGH", - ["nmadd.s_4"] = "4c000030FRGH", - ["nmadd.d_4"] = "4c000031FRGH", - ["nmadd.ps_4"] = "4c000036FRGH", - ["nmsub.s_4"] = "4c000038FRGH", - ["nmsub.d_4"] = "4c000039FRGH", - ["nmsub.ps_4"] = "4c00003eFRGH", -} - ------------------------------------------------------------------------------- - -local function parse_gpr(expr) - local tname, ovreg = match(expr, "^([%w_]+):(r[1-3]?[0-9])$") - local tp = map_type[tname or expr] - if tp then - local reg = ovreg or tp.reg - if not reg then - werror("type `"..(tname or expr).."' needs a register override") - end - expr = reg - end - local r = match(expr, "^r([1-3]?[0-9])$") - if r then - r = tonumber(r) - if r <= 31 then return r, tp end - end - werror("bad register name `"..expr.."'") -end - -local function parse_fpr(expr) - local r = match(expr, "^f([1-3]?[0-9])$") - if r then - r = tonumber(r) - if r <= 31 then return r end - end - werror("bad register name `"..expr.."'") -end - -local function parse_imm(imm, bits, shift, scale, signed) - local n = tonumber(imm) - if n then - if n % 2^scale == 0 then - n = n / 2^scale - if signed then - if n >= 0 then - if n < 2^(bits-1) then return n*2^shift end - else - if n >= -(2^(bits-1))-1 then return (n+2^bits)*2^shift end - end - else - if n >= 0 and n <= 2^bits-1 then return n*2^shift end - end - end - werror("out of range immediate `"..imm.."'") - elseif match(imm, "^[rf]([1-3]?[0-9])$") or - match(imm, "^([%w_]+):([rf][1-3]?[0-9])$") then - werror("expected immediate operand, got register") - else - waction("IMM", (signed and 32768 or 0)+scale*1024+bits*32+shift, imm) - return 0 - end -end - -local function parse_disp(disp) - local imm, reg = match(disp, "^(.*)%(([%w_:]+)%)$") - if imm then - local r = parse_gpr(reg)*2^21 - local extname = match(imm, "^extern%s+(%S+)$") - if extname then - waction("REL_EXT", map_extern[extname], nil, 1) - return r - else - return r + parse_imm(imm, 16, 0, 0, true) - end - end - local reg, tailr = match(disp, "^([%w_:]+)%s*(.*)$") - if reg and tailr ~= "" then - local r, tp = parse_gpr(reg) - if tp then - waction("IMM", 32768+16*32, format(tp.ctypefmt, tailr)) - return r*2^21 - end - end - werror("bad displacement `"..disp.."'") -end - -local function parse_index(idx) - local rt, rs = match(idx, "^(.*)%(([%w_:]+)%)$") - if rt then - rt = parse_gpr(rt) - rs = parse_gpr(rs) - return rt*2^16 + rs*2^21 - end - werror("bad index `"..idx.."'") -end - -local function parse_label(label, def) - local prefix = sub(label, 1, 2) - -- =>label (pc label reference) - if prefix == "=>" then - return "PC", 0, sub(label, 3) - end - -- ->name (global label reference) - if prefix == "->" then - return "LG", map_global[sub(label, 3)] - end - if def then - -- [1-9] (local label definition) - if match(label, "^[1-9]$") then - return "LG", 10+tonumber(label) - end - else - -- [<>][1-9] (local label reference) - local dir, lnum = match(label, "^([<>])([1-9])$") - if dir then -- Fwd: 1-9, Bkwd: 11-19. - return "LG", lnum + (dir == ">" and 0 or 10) - end - -- extern label (extern label reference) - local extname = match(label, "^extern%s+(%S+)$") - if extname then - return "EXT", map_extern[extname] - end - end - werror("bad label `"..label.."'") -end - ------------------------------------------------------------------------------- - --- Handle opcodes defined with template strings. -map_op[".template__"] = function(params, template, nparams) - if not params then return sub(template, 9) end - local op = tonumber(sub(template, 1, 8), 16) - local n = 1 - - -- Limit number of section buffer positions used by a single dasm_put(). - -- A single opcode needs a maximum of 2 positions (ins/ext). - if secpos+2 > maxsecpos then wflush() end - local pos = wpos() - - -- Process each character. - for p in gmatch(sub(template, 9), ".") do - if p == "D" then - op = op + parse_gpr(params[n]) * 2^11; n = n + 1 - elseif p == "T" then - op = op + parse_gpr(params[n]) * 2^16; n = n + 1 - elseif p == "S" then - op = op + parse_gpr(params[n]) * 2^21; n = n + 1 - elseif p == "F" then - op = op + parse_fpr(params[n]) * 2^6; n = n + 1 - elseif p == "G" then - op = op + parse_fpr(params[n]) * 2^11; n = n + 1 - elseif p == "H" then - op = op + parse_fpr(params[n]) * 2^16; n = n + 1 - elseif p == "R" then - op = op + parse_fpr(params[n]) * 2^21; n = n + 1 - elseif p == "I" then - op = op + parse_imm(params[n], 16, 0, 0, true); n = n + 1 - elseif p == "U" then - op = op + parse_imm(params[n], 16, 0, 0, false); n = n + 1 - elseif p == "O" then - op = op + parse_disp(params[n]); n = n + 1 - elseif p == "X" then - op = op + parse_index(params[n]); n = n + 1 - elseif p == "B" or p == "J" then - local mode, n, s = parse_label(params[n], false) - if p == "B" then n = n + 2048 end - waction("REL_"..mode, n, s, 1) - n = n + 1 - elseif p == "A" then - op = op + parse_imm(params[n], 5, 6, 0, false); n = n + 1 - elseif p == "M" then - op = op + parse_imm(params[n], 5, 11, 0, false); n = n + 1 - elseif p == "N" then - op = op + parse_imm(params[n], 5, 16, 0, false); n = n + 1 - elseif p == "C" then - op = op + parse_imm(params[n], 3, 18, 0, false); n = n + 1 - elseif p == "V" then - op = op + parse_imm(params[n], 3, 8, 0, false); n = n + 1 - elseif p == "W" then - op = op + parse_imm(params[n], 3, 0, 0, false); n = n + 1 - elseif p == "Y" then - op = op + parse_imm(params[n], 20, 6, 0, false); n = n + 1 - elseif p == "Z" then - op = op + parse_imm(params[n], 10, 6, 0, false); n = n + 1 - elseif p == "=" then - local d = ((op - op % 2^11) / 2^11) % 32 - op = op + d * 2^16 -- Copy D to T for clz, clo. - else - assert(false) - end - end - wputpos(pos, op) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode to mark the position where the action list is to be emitted. -map_op[".actionlist_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeactions(out, name) end) -end - --- Pseudo-opcode to mark the position where the global enum is to be emitted. -map_op[".globals_1"] = function(params) - if not params then return "prefix" end - local prefix = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobals(out, prefix) end) -end - --- Pseudo-opcode to mark the position where the global names are to be emitted. -map_op[".globalnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobalnames(out, name) end) -end - --- Pseudo-opcode to mark the position where the extern names are to be emitted. -map_op[".externnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeexternnames(out, name) end) -end - ------------------------------------------------------------------------------- - --- Label pseudo-opcode (converted from trailing colon form). -map_op[".label_1"] = function(params) - if not params then return "[1-9] | ->global | =>pcexpr" end - if secpos+1 > maxsecpos then wflush() end - local mode, n, s = parse_label(params[1], true) - if mode == "EXT" then werror("bad label definition") end - waction("LABEL_"..mode, n, s, 1) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcodes for data storage. -map_op[".long_*"] = function(params) - if not params then return "imm..." end - for _,p in ipairs(params) do - local n = tonumber(p) - if not n then werror("bad immediate `"..p.."'") end - if n < 0 then n = n + 2^32 end - wputw(n) - if secpos+2 > maxsecpos then wflush() end - end -end - --- Alignment pseudo-opcode. -map_op[".align_1"] = function(params) - if not params then return "numpow2" end - if secpos+1 > maxsecpos then wflush() end - local align = tonumber(params[1]) - if align then - local x = align - -- Must be a power of 2 in the range (2 ... 256). - for i=1,8 do - x = x / 2 - if x == 1 then - waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1. - return - end - end - end - werror("bad alignment") -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode for (primitive) type definitions (map to C types). -map_op[".type_3"] = function(params, nparams) - if not params then - return nparams == 2 and "name, ctype" or "name, ctype, reg" - end - local name, ctype, reg = params[1], params[2], params[3] - if not match(name, "^[%a_][%w_]*$") then - werror("bad type name `"..name.."'") - end - local tp = map_type[name] - if tp then - werror("duplicate type `"..name.."'") - end - -- Add #type to defines. A bit unclean to put it in map_archdef. - map_archdef["#"..name] = "sizeof("..ctype..")" - -- Add new type and emit shortcut define. - local num = ctypenum + 1 - map_type[name] = { - ctype = ctype, - ctypefmt = format("Dt%X(%%s)", num), - reg = reg, - } - wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype)) - ctypenum = num -end -map_op[".type_2"] = map_op[".type_3"] - --- Dump type definitions. -local function dumptypes(out, lvl) - local t = {} - for name in pairs(map_type) do t[#t+1] = name end - sort(t) - out:write("Type definitions:\n") - for _,name in ipairs(t) do - local tp = map_type[name] - local reg = tp.reg or "" - out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Set the current section. -function _M.section(num) - waction("SECTION", num) - wflush(true) -- SECTION is a terminal action. -end - ------------------------------------------------------------------------------- - --- Dump architecture description. -function _M.dumparch(out) - out:write(format("DynASM %s version %s, released %s\n\n", - _info.arch, _info.version, _info.release)) - dumpactions(out) -end - --- Dump all user defined elements. -function _M.dumpdef(out, lvl) - dumptypes(out, lvl) - dumpglobals(out, lvl) - dumpexterns(out, lvl) -end - ------------------------------------------------------------------------------- - --- Pass callbacks from/to the DynASM core. -function _M.passcb(wl, we, wf, ww) - wline, werror, wfatal, wwarn = wl, we, wf, ww - return wflush -end - --- Setup the arch-specific module. -function _M.setup(arch, opt) - g_arch, g_opt = arch, opt -end - --- Merge the core maps and the arch-specific maps. -function _M.mergemaps(map_coreop, map_def) - setmetatable(map_op, { __index = map_coreop }) - setmetatable(map_def, { __index = map_archdef }) - return map_op, map_def -end - -return _M - ------------------------------------------------------------------------------- - diff --git a/third_party/dynasm/dasm_ppc.h b/third_party/dynasm/dasm_ppc.h deleted file mode 100644 index bf5957e..0000000 --- a/third_party/dynasm/dasm_ppc.h +++ /dev/null @@ -1,411 +0,0 @@ -/* -** DynASM PPC encoding engine. -** Copyright (C) 2005-2012 Mike Pall. All rights reserved. -** Released under the MIT license. See dynasm.lua for full copyright notice. -*/ - -#include -#include -#include -#include - -#define DASM_ARCH "ppc" - -#ifndef DASM_EXTERN -#define DASM_EXTERN(a,b,c,d) 0 -#endif - -/* Action definitions. */ -enum { - DASM_STOP, DASM_SECTION, DASM_ESC, DASM_REL_EXT, - /* The following actions need a buffer position. */ - DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG, - /* The following actions also have an argument. */ - DASM_REL_PC, DASM_LABEL_PC, DASM_IMM, - DASM__MAX -}; - -/* Maximum number of section buffer positions for a single dasm_put() call. */ -#define DASM_MAXSECPOS 25 - -/* DynASM encoder status codes. Action list offset or number are or'ed in. */ -#define DASM_S_OK 0x00000000 -#define DASM_S_NOMEM 0x01000000 -#define DASM_S_PHASE 0x02000000 -#define DASM_S_MATCH_SEC 0x03000000 -#define DASM_S_RANGE_I 0x11000000 -#define DASM_S_RANGE_SEC 0x12000000 -#define DASM_S_RANGE_LG 0x13000000 -#define DASM_S_RANGE_PC 0x14000000 -#define DASM_S_RANGE_REL 0x15000000 -#define DASM_S_UNDEF_LG 0x21000000 -#define DASM_S_UNDEF_PC 0x22000000 - -/* Macros to convert positions (8 bit section + 24 bit index). */ -#define DASM_POS2IDX(pos) ((pos)&0x00ffffff) -#define DASM_POS2BIAS(pos) ((pos)&0xff000000) -#define DASM_SEC2POS(sec) ((sec)<<24) -#define DASM_POS2SEC(pos) ((pos)>>24) -#define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos)) - -/* Action list type. */ -typedef const unsigned int *dasm_ActList; - -/* Per-section structure. */ -typedef struct dasm_Section { - int *rbuf; /* Biased buffer pointer (negative section bias). */ - int *buf; /* True buffer pointer. */ - size_t bsize; /* Buffer size in bytes. */ - int pos; /* Biased buffer position. */ - int epos; /* End of biased buffer position - max single put. */ - int ofs; /* Byte offset into section. */ -} dasm_Section; - -/* Core structure holding the DynASM encoding state. */ -struct dasm_State { - size_t psize; /* Allocated size of this structure. */ - dasm_ActList actionlist; /* Current actionlist pointer. */ - int *lglabels; /* Local/global chain/pos ptrs. */ - size_t lgsize; - int *pclabels; /* PC label chains/pos ptrs. */ - size_t pcsize; - void **globals; /* Array of globals (bias -10). */ - dasm_Section *section; /* Pointer to active section. */ - size_t codesize; /* Total size of all code sections. */ - int maxsection; /* 0 <= sectionidx < maxsection. */ - int status; /* Status code. */ - dasm_Section sections[1]; /* All sections. Alloc-extended. */ -}; - -/* The size of the core structure depends on the max. number of sections. */ -#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section)) - - -/* Initialize DynASM state. */ -void dasm_init(Dst_DECL, int maxsection) -{ - dasm_State *D; - size_t psz = 0; - int i; - Dst_REF = NULL; - DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection)); - D = Dst_REF; - D->psize = psz; - D->lglabels = NULL; - D->lgsize = 0; - D->pclabels = NULL; - D->pcsize = 0; - D->globals = NULL; - D->maxsection = maxsection; - for (i = 0; i < maxsection; i++) { - D->sections[i].buf = NULL; /* Need this for pass3. */ - D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); - D->sections[i].bsize = 0; - D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ - } -} - -/* Free DynASM state. */ -void dasm_free(Dst_DECL) -{ - dasm_State *D = Dst_REF; - int i; - for (i = 0; i < D->maxsection; i++) - if (D->sections[i].buf) - DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize); - if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize); - if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize); - DASM_M_FREE(Dst, D, D->psize); -} - -/* Setup global label array. Must be called before dasm_setup(). */ -void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl) -{ - dasm_State *D = Dst_REF; - D->globals = gl - 10; /* Negative bias to compensate for locals. */ - DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int)); -} - -/* Grow PC label array. Can be called after dasm_setup(), too. */ -void dasm_growpc(Dst_DECL, unsigned int maxpc) -{ - dasm_State *D = Dst_REF; - size_t osz = D->pcsize; - DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int)); - memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz); -} - -/* Setup encoder. */ -void dasm_setup(Dst_DECL, const void *actionlist) -{ - dasm_State *D = Dst_REF; - int i; - D->actionlist = (dasm_ActList)actionlist; - D->status = DASM_S_OK; - D->section = &D->sections[0]; - memset((void *)D->lglabels, 0, D->lgsize); - if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize); - for (i = 0; i < D->maxsection; i++) { - D->sections[i].pos = DASM_SEC2POS(i); - D->sections[i].ofs = 0; - } -} - - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) { \ - D->status = DASM_S_##st|(p-D->actionlist-1); return; } } while (0) -#define CKPL(kind, st) \ - do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \ - D->status = DASM_S_RANGE_##st|(p-D->actionlist-1); return; } } while (0) -#else -#define CK(x, st) ((void)0) -#define CKPL(kind, st) ((void)0) -#endif - -/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */ -void dasm_put(Dst_DECL, int start, ...) -{ - va_list ap; - dasm_State *D = Dst_REF; - dasm_ActList p = D->actionlist + start; - dasm_Section *sec = D->section; - int pos = sec->pos, ofs = sec->ofs; - int *b; - - if (pos >= sec->epos) { - DASM_M_GROW(Dst, int, sec->buf, sec->bsize, - sec->bsize + 2*DASM_MAXSECPOS*sizeof(int)); - sec->rbuf = sec->buf - DASM_POS2BIAS(pos); - sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos); - } - - b = sec->rbuf; - b[pos++] = start; - - va_start(ap, start); - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - if (action >= DASM__MAX) { - ofs += 4; - } else { - int *pl, n = action >= DASM_REL_PC ? va_arg(ap, int) : 0; - switch (action) { - case DASM_STOP: goto stop; - case DASM_SECTION: - n = (ins & 255); CK(n < D->maxsection, RANGE_SEC); - D->section = &D->sections[n]; goto stop; - case DASM_ESC: p++; ofs += 4; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs += (ins & 255); b[pos++] = ofs; break; - case DASM_REL_LG: - n = (ins & 2047) - 10; pl = D->lglabels + n; - if (n >= 0) { CKPL(lg, LG); goto putrel; } /* Bkwd rel or global. */ - pl += 10; n = *pl; - if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */ - goto linkrel; - case DASM_REL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putrel: - n = *pl; - if (n < 0) { /* Label exists. Get label pos and store it. */ - b[pos] = -n; - } else { - linkrel: - b[pos] = n; /* Else link to rel chain, anchored at label. */ - *pl = pos; - } - pos++; - break; - case DASM_LABEL_LG: - pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel; - case DASM_LABEL_PC: - pl = D->pclabels + n; CKPL(pc, PC); - putlabel: - n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; - } - *pl = -pos; /* Label exists now. */ - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_IMM: -#ifdef DASM_CHECKS - CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I); -#endif - n >>= ((ins>>10)&31); -#ifdef DASM_CHECKS - if (ins & 0x8000) - CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I); - else - CK((n>>((ins>>5)&31)) == 0, RANGE_I); -#endif - b[pos++] = n; - break; - } - } - } -stop: - va_end(ap); - sec->pos = pos; - sec->ofs = ofs; -} -#undef CK - -/* Pass 2: Link sections, shrink aligns, fix label offsets. */ -int dasm_link(Dst_DECL, size_t *szp) -{ - dasm_State *D = Dst_REF; - int secnum; - int ofs = 0; - -#ifdef DASM_CHECKS - *szp = 0; - if (D->status != DASM_S_OK) return D->status; - { - int pc; - for (pc = 0; pc*sizeof(int) < D->pcsize; pc++) - if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc; - } -#endif - - { /* Handle globals not defined in this translation unit. */ - int idx; - for (idx = 20; idx*sizeof(int) < D->lgsize; idx++) { - int n = D->lglabels[idx]; - /* Undefined label: Collapse rel chain and replace with marker (< 0). */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; } - } - } - - /* Combine all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->rbuf; - int pos = DASM_SEC2POS(secnum); - int lastpos = sec->pos; - - while (pos != lastpos) { - dasm_ActList p = D->actionlist + b[pos++]; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: p++; break; - case DASM_REL_EXT: break; - case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break; - case DASM_REL_LG: case DASM_REL_PC: pos++; break; - case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break; - case DASM_IMM: pos++; break; - } - } - stop: (void)0; - } - ofs += sec->ofs; /* Next section starts right after current section. */ - } - - D->codesize = ofs; /* Total size of all code sections */ - *szp = ofs; - return DASM_S_OK; -} - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) return DASM_S_##st|(p-D->actionlist-1); } while (0) -#else -#define CK(x, st) ((void)0) -#endif - -/* Pass 3: Encode sections. */ -int dasm_encode(Dst_DECL, void *buffer) -{ - dasm_State *D = Dst_REF; - char *base = (char *)buffer; - unsigned int *cp = (unsigned int *)buffer; - int secnum; - - /* Encode all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->buf; - int *endb = sec->rbuf + sec->pos; - - while (b != endb) { - dasm_ActList p = D->actionlist + *b++; - while (1) { - unsigned int ins = *p++; - unsigned int action = (ins >> 16); - int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0; - switch (action) { - case DASM_STOP: case DASM_SECTION: goto stop; - case DASM_ESC: *cp++ = *p++; break; - case DASM_REL_EXT: - n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins & 2047), 1) - 4; - goto patchrel; - case DASM_ALIGN: - ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0x60000000; - break; - case DASM_REL_LG: - CK(n >= 0, UNDEF_LG); - case DASM_REL_PC: - CK(n >= 0, UNDEF_PC); - n = *DASM_POS2PTR(D, n) - (int)((char *)cp - base); - patchrel: - CK((n & 3) == 0 && - (((n+4) + ((ins & 2048) ? 0x00008000 : 0x02000000)) >> - ((ins & 2048) ? 16 : 26)) == 0, RANGE_REL); - cp[-1] |= ((n+4) & ((ins & 2048) ? 0x0000fffc: 0x03fffffc)); - break; - case DASM_LABEL_LG: - ins &= 2047; if (ins >= 20) D->globals[ins-10] = (void *)(base + n); - break; - case DASM_LABEL_PC: break; - case DASM_IMM: - cp[-1] |= (n & ((1<<((ins>>5)&31))-1)) << (ins&31); - break; - default: *cp++ = ins; break; - } - } - stop: (void)0; - } - } - - if (base + D->codesize != (char *)cp) /* Check for phase errors. */ - return DASM_S_PHASE; - return DASM_S_OK; -} -#undef CK - -/* Get PC label offset. */ -int dasm_getpclabel(Dst_DECL, unsigned int pc) -{ - dasm_State *D = Dst_REF; - if (pc*sizeof(int) < D->pcsize) { - int pos = D->pclabels[pc]; - if (pos < 0) return *DASM_POS2PTR(D, -pos); - if (pos > 0) return -1; /* Undefined. */ - } - return -2; /* Unused or out of range. */ -} - -#ifdef DASM_CHECKS -/* Optional sanity checker to call between isolated encoding steps. */ -int dasm_checkstep(Dst_DECL, int secmatch) -{ - dasm_State *D = Dst_REF; - if (D->status == DASM_S_OK) { - int i; - for (i = 1; i <= 9; i++) { - if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_LG|i; break; } - D->lglabels[i] = 0; - } - } - if (D->status == DASM_S_OK && secmatch >= 0 && - D->section != &D->sections[secmatch]) - D->status = DASM_S_MATCH_SEC|(D->section-D->sections); - return D->status; -} -#endif - diff --git a/third_party/dynasm/dasm_ppc.lua b/third_party/dynasm/dasm_ppc.lua deleted file mode 100644 index dc2af69..0000000 --- a/third_party/dynasm/dasm_ppc.lua +++ /dev/null @@ -1,1230 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM PPC module. --- --- Copyright (C) 2005-2012 Mike Pall. All rights reserved. --- See dynasm.lua for full copyright notice. ------------------------------------------------------------------------------- - --- Module information: -local _info = { - arch = "ppc", - description = "DynASM PPC module", - version = "1.3.0", - vernum = 10300, - release = "2011-05-05", - author = "Mike Pall", - license = "MIT", -} - --- Exported glue functions for the arch-specific module. -local _M = { _info = _info } - --- Cache library functions. -local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs -local assert, setmetatable = assert, setmetatable -local _s = string -local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char -local match, gmatch = _s.match, _s.gmatch -local concat, sort = table.concat, table.sort - --- Inherited tables and callbacks. -local g_opt, g_arch -local wline, werror, wfatal, wwarn - --- Action name list. --- CHECK: Keep this in sync with the C code! -local action_names = { - "STOP", "SECTION", "ESC", "REL_EXT", - "ALIGN", "REL_LG", "LABEL_LG", - "REL_PC", "LABEL_PC", "IMM", -} - --- Maximum number of section buffer positions for dasm_put(). --- CHECK: Keep this in sync with the C code! -local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines. - --- Action name -> action number. -local map_action = {} -for n,name in ipairs(action_names) do - map_action[name] = n-1 -end - --- Action list buffer. -local actlist = {} - --- Argument list for next dasm_put(). Start with offset 0 into action list. -local actargs = { 0 } - --- Current number of section buffer positions for dasm_put(). -local secpos = 1 - ------------------------------------------------------------------------------- - --- Return 8 digit hex number. -local function tohex(x) - return sub(format("%08x", x), -8) -- Avoid 64 bit portability problem in Lua. -end - --- Dump action names and numbers. -local function dumpactions(out) - out:write("DynASM encoding engine action codes:\n") - for n,name in ipairs(action_names) do - local num = map_action[name] - out:write(format(" %-10s %02X %d\n", name, num, num)) - end - out:write("\n") -end - --- Write action list buffer as a huge static C array. -local function writeactions(out, name) - local nn = #actlist - if nn == 0 then nn = 1; actlist[0] = map_action.STOP end - out:write("static const unsigned int ", name, "[", nn, "] = {\n") - for i = 1,nn-1 do - assert(out:write("0x", tohex(actlist[i]), ",\n")) - end - assert(out:write("0x", tohex(actlist[nn]), "\n};\n\n")) -end - ------------------------------------------------------------------------------- - --- Add word to action list. -local function wputxw(n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - actlist[#actlist+1] = n -end - --- Add action to list with optional arg. Advance buffer pos, too. -local function waction(action, val, a, num) - local w = assert(map_action[action], "bad action name `"..action.."'") - wputxw(w * 0x10000 + (val or 0)) - if a then actargs[#actargs+1] = a end - if a or num then secpos = secpos + (num or 1) end -end - --- Flush action list (intervening C code or buffer pos overflow). -local function wflush(term) - if #actlist == actargs[1] then return end -- Nothing to flush. - if not term then waction("STOP") end -- Terminate action list. - wline(format("dasm_put(Dst, %s);", concat(actargs, ", ")), true) - actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put(). - secpos = 1 -- The actionlist offset occupies a buffer position, too. -end - --- Put escaped word. -local function wputw(n) - if n <= 0xffffff then waction("ESC") end - wputxw(n) -end - --- Reserve position for word. -local function wpos() - local pos = #actlist+1 - actlist[pos] = "" - return pos -end - --- Store word to reserved position. -local function wputpos(pos, n) - assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range") - actlist[pos] = n -end - ------------------------------------------------------------------------------- - --- Global label name -> global label number. With auto assignment on 1st use. -local next_global = 20 -local map_global = setmetatable({}, { __index = function(t, name) - if not match(name, "^[%a_][%w_]*$") then werror("bad global label") end - local n = next_global - if n > 2047 then werror("too many global labels") end - next_global = n + 1 - t[name] = n - return n -end}) - --- Dump global labels. -local function dumpglobals(out, lvl) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("Global labels:\n") - for i=20,next_global-1 do - out:write(format(" %s\n", t[i])) - end - out:write("\n") -end - --- Write global label enum. -local function writeglobals(out, prefix) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("enum {\n") - for i=20,next_global-1 do - out:write(" ", prefix, t[i], ",\n") - end - out:write(" ", prefix, "_MAX\n};\n") -end - --- Write global label names. -local function writeglobalnames(out, name) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("static const char *const ", name, "[] = {\n") - for i=20,next_global-1 do - out:write(" \"", t[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Extern label name -> extern label number. With auto assignment on 1st use. -local next_extern = 0 -local map_extern_ = {} -local map_extern = setmetatable({}, { __index = function(t, name) - -- No restrictions on the name for now. - local n = next_extern - if n > 2047 then werror("too many extern labels") end - next_extern = n + 1 - t[name] = n - map_extern_[n] = name - return n -end}) - --- Dump extern labels. -local function dumpexterns(out, lvl) - out:write("Extern labels:\n") - for i=0,next_extern-1 do - out:write(format(" %s\n", map_extern_[i])) - end - out:write("\n") -end - --- Write extern label names. -local function writeexternnames(out, name) - out:write("static const char *const ", name, "[] = {\n") - for i=0,next_extern-1 do - out:write(" \"", map_extern_[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Arch-specific maps. -local map_archdef = { sp = "r1" } -- Ext. register name -> int. name. - -local map_type = {} -- Type name -> { ctype, reg } -local ctypenum = 0 -- Type number (for Dt... macros). - --- Reverse defines for registers. -function _M.revdef(s) - if s == "r1" then return "sp" end - return s -end - -local map_cond = { - lt = 0, gt = 1, eq = 2, so = 3, - ge = 4, le = 5, ne = 6, ns = 7, -} - ------------------------------------------------------------------------------- - --- Template strings for PPC instructions. -local map_op = { - tdi_3 = "08000000ARI", - twi_3 = "0c000000ARI", - mulli_3 = "1c000000RRI", - subfic_3 = "20000000RRI", - cmplwi_3 = "28000000XRU", - cmplwi_2 = "28000000-RU", - cmpldi_3 = "28200000XRU", - cmpldi_2 = "28200000-RU", - cmpwi_3 = "2c000000XRI", - cmpwi_2 = "2c000000-RI", - cmpdi_3 = "2c200000XRI", - cmpdi_2 = "2c200000-RI", - addic_3 = "30000000RRI", - ["addic._3"] = "34000000RRI", - addi_3 = "38000000RR0I", - li_2 = "38000000RI", - la_2 = "38000000RD", - addis_3 = "3c000000RR0I", - lis_2 = "3c000000RI", - lus_2 = "3c000000RU", - bc_3 = "40000000AAK", - bcl_3 = "40000001AAK", - bdnz_1 = "42000000K", - bdz_1 = "42400000K", - sc_0 = "44000000", - b_1 = "48000000J", - bl_1 = "48000001J", - rlwimi_5 = "50000000RR~AAA.", - rlwinm_5 = "54000000RR~AAA.", - rlwnm_5 = "5c000000RR~RAA.", - ori_3 = "60000000RR~U", - nop_0 = "60000000", - oris_3 = "64000000RR~U", - xori_3 = "68000000RR~U", - xoris_3 = "6c000000RR~U", - ["andi._3"] = "70000000RR~U", - ["andis._3"] = "74000000RR~U", - lwz_2 = "80000000RD", - lwzu_2 = "84000000RD", - lbz_2 = "88000000RD", - lbzu_2 = "8c000000RD", - stw_2 = "90000000RD", - stwu_2 = "94000000RD", - stb_2 = "98000000RD", - stbu_2 = "9c000000RD", - lhz_2 = "a0000000RD", - lhzu_2 = "a4000000RD", - lha_2 = "a8000000RD", - lhau_2 = "ac000000RD", - sth_2 = "b0000000RD", - sthu_2 = "b4000000RD", - lmw_2 = "b8000000RD", - stmw_2 = "bc000000RD", - lfs_2 = "c0000000FD", - lfsu_2 = "c4000000FD", - lfd_2 = "c8000000FD", - lfdu_2 = "cc000000FD", - stfs_2 = "d0000000FD", - stfsu_2 = "d4000000FD", - stfd_2 = "d8000000FD", - stfdu_2 = "dc000000FD", - ld_2 = "e8000000RD", -- NYI: displacement must be divisible by 4. - ldu_2 = "e8000001RD", - lwa_2 = "e8000002RD", - std_2 = "f8000000RD", - stdu_2 = "f8000001RD", - - -- Primary opcode 19: - mcrf_2 = "4c000000XX", - isync_0 = "4c00012c", - crnor_3 = "4c000042CCC", - crnot_2 = "4c000042CC=", - crandc_3 = "4c000102CCC", - crxor_3 = "4c000182CCC", - crclr_1 = "4c000182C==", - crnand_3 = "4c0001c2CCC", - crand_3 = "4c000202CCC", - creqv_3 = "4c000242CCC", - crset_1 = "4c000242C==", - crorc_3 = "4c000342CCC", - cror_3 = "4c000382CCC", - crmove_2 = "4c000382CC=", - bclr_2 = "4c000020AA", - bclrl_2 = "4c000021AA", - bcctr_2 = "4c000420AA", - bcctrl_2 = "4c000421AA", - blr_0 = "4e800020", - blrl_0 = "4e800021", - bctr_0 = "4e800420", - bctrl_0 = "4e800421", - - -- Primary opcode 31: - cmpw_3 = "7c000000XRR", - cmpw_2 = "7c000000-RR", - cmpd_3 = "7c200000XRR", - cmpd_2 = "7c200000-RR", - tw_3 = "7c000008ARR", - subfc_3 = "7c000010RRR.", - subc_3 = "7c000010RRR~.", - mulhdu_3 = "7c000012RRR.", - addc_3 = "7c000014RRR.", - mulhwu_3 = "7c000016RRR.", - isel_4 = "7c00001eRRRC", - isellt_3 = "7c00001eRRR", - iselgt_3 = "7c00005eRRR", - iseleq_3 = "7c00009eRRR", - mfcr_1 = "7c000026R", - mtcrf_2 = "7c000120GR", - -- NYI: mtocrf, mfocrf - lwarx_3 = "7c000028RR0R", - ldx_3 = "7c00002aRR0R", - lwzx_3 = "7c00002eRR0R", - slw_3 = "7c000030RR~R.", - cntlzw_2 = "7c000034RR~", - sld_3 = "7c000036RR~R.", - and_3 = "7c000038RR~R.", - cmplw_3 = "7c000040XRR", - cmplw_2 = "7c000040-RR", - cmpld_3 = "7c200040XRR", - cmpld_2 = "7c200040-RR", - subf_3 = "7c000050RRR.", - sub_3 = "7c000050RRR~.", - ldux_3 = "7c00006aRR0R", - dcbst_2 = "7c00006c-RR", - lwzux_3 = "7c00006eRR0R", - cntlzd_2 = "7c000074RR~", - andc_3 = "7c000078RR~R.", - td_3 = "7c000088ARR", - mulhd_3 = "7c000092RRR.", - mulhw_3 = "7c000096RRR.", - ldarx_3 = "7c0000a8RR0R", - dcbf_2 = "7c0000ac-RR", - lbzx_3 = "7c0000aeRR0R", - neg_2 = "7c0000d0RR.", - lbzux_3 = "7c0000eeRR0R", - popcntb_2 = "7c0000f4RR~", - not_2 = "7c0000f8RR~%.", - nor_3 = "7c0000f8RR~R.", - subfe_3 = "7c000110RRR.", - sube_3 = "7c000110RRR~.", - adde_3 = "7c000114RRR.", - stdx_3 = "7c00012aRR0R", - stwcx_3 = "7c00012cRR0R.", - stwx_3 = "7c00012eRR0R", - prtyw_2 = "7c000134RR~", - stdux_3 = "7c00016aRR0R", - stwux_3 = "7c00016eRR0R", - prtyd_2 = "7c000174RR~", - subfze_2 = "7c000190RR.", - addze_2 = "7c000194RR.", - stdcx_3 = "7c0001acRR0R.", - stbx_3 = "7c0001aeRR0R", - subfme_2 = "7c0001d0RR.", - mulld_3 = "7c0001d2RRR.", - addme_2 = "7c0001d4RR.", - mullw_3 = "7c0001d6RRR.", - dcbtst_2 = "7c0001ec-RR", - stbux_3 = "7c0001eeRR0R", - add_3 = "7c000214RRR.", - dcbt_2 = "7c00022c-RR", - lhzx_3 = "7c00022eRR0R", - eqv_3 = "7c000238RR~R.", - eciwx_3 = "7c00026cRR0R", - lhzux_3 = "7c00026eRR0R", - xor_3 = "7c000278RR~R.", - mfspefscr_1 = "7c0082a6R", - mfxer_1 = "7c0102a6R", - mflr_1 = "7c0802a6R", - mfctr_1 = "7c0902a6R", - lwax_3 = "7c0002aaRR0R", - lhax_3 = "7c0002aeRR0R", - mftb_1 = "7c0c42e6R", - mftbu_1 = "7c0d42e6R", - lwaux_3 = "7c0002eaRR0R", - lhaux_3 = "7c0002eeRR0R", - sthx_3 = "7c00032eRR0R", - orc_3 = "7c000338RR~R.", - ecowx_3 = "7c00036cRR0R", - sthux_3 = "7c00036eRR0R", - or_3 = "7c000378RR~R.", - mr_2 = "7c000378RR~%.", - divdu_3 = "7c000392RRR.", - divwu_3 = "7c000396RRR.", - mtspefscr_1 = "7c0083a6R", - mtxer_1 = "7c0103a6R", - mtlr_1 = "7c0803a6R", - mtctr_1 = "7c0903a6R", - dcbi_2 = "7c0003ac-RR", - nand_3 = "7c0003b8RR~R.", - divd_3 = "7c0003d2RRR.", - divw_3 = "7c0003d6RRR.", - cmpb_3 = "7c0003f8RR~R.", - mcrxr_1 = "7c000400X", - subfco_3 = "7c000410RRR.", - subco_3 = "7c000410RRR~.", - addco_3 = "7c000414RRR.", - ldbrx_3 = "7c000428RR0R", - lswx_3 = "7c00042aRR0R", - lwbrx_3 = "7c00042cRR0R", - lfsx_3 = "7c00042eFR0R", - srw_3 = "7c000430RR~R.", - srd_3 = "7c000436RR~R.", - subfo_3 = "7c000450RRR.", - subo_3 = "7c000450RRR~.", - lfsux_3 = "7c00046eFR0R", - lswi_3 = "7c0004aaRR0A", - sync_0 = "7c0004ac", - lwsync_0 = "7c2004ac", - ptesync_0 = "7c4004ac", - lfdx_3 = "7c0004aeFR0R", - nego_2 = "7c0004d0RR.", - lfdux_3 = "7c0004eeFR0R", - subfeo_3 = "7c000510RRR.", - subeo_3 = "7c000510RRR~.", - addeo_3 = "7c000514RRR.", - stdbrx_3 = "7c000528RR0R", - stswx_3 = "7c00052aRR0R", - stwbrx_3 = "7c00052cRR0R", - stfsx_3 = "7c00052eFR0R", - stfsux_3 = "7c00056eFR0R", - subfzeo_2 = "7c000590RR.", - addzeo_2 = "7c000594RR.", - stswi_3 = "7c0005aaRR0A", - stfdx_3 = "7c0005aeFR0R", - subfmeo_2 = "7c0005d0RR.", - mulldo_3 = "7c0005d2RRR.", - addmeo_2 = "7c0005d4RR.", - mullwo_3 = "7c0005d6RRR.", - dcba_2 = "7c0005ec-RR", - stfdux_3 = "7c0005eeFR0R", - addo_3 = "7c000614RRR.", - lhbrx_3 = "7c00062cRR0R", - sraw_3 = "7c000630RR~R.", - srad_3 = "7c000634RR~R.", - srawi_3 = "7c000670RR~A.", - eieio_0 = "7c0006ac", - lfiwax_3 = "7c0006aeFR0R", - sthbrx_3 = "7c00072cRR0R", - extsh_2 = "7c000734RR~.", - extsb_2 = "7c000774RR~.", - divduo_3 = "7c000792RRR.", - divwou_3 = "7c000796RRR.", - icbi_2 = "7c0007ac-RR", - stfiwx_3 = "7c0007aeFR0R", - extsw_2 = "7c0007b4RR~.", - divdo_3 = "7c0007d2RRR.", - divwo_3 = "7c0007d6RRR.", - dcbz_2 = "7c0007ec-RR", - - -- Primary opcode 59: - fdivs_3 = "ec000024FFF.", - fsubs_3 = "ec000028FFF.", - fadds_3 = "ec00002aFFF.", - fsqrts_2 = "ec00002cF-F.", - fres_2 = "ec000030F-F.", - fmuls_3 = "ec000032FF-F.", - frsqrtes_2 = "ec000034F-F.", - fmsubs_4 = "ec000038FFFF~.", - fmadds_4 = "ec00003aFFFF~.", - fnmsubs_4 = "ec00003cFFFF~.", - fnmadds_4 = "ec00003eFFFF~.", - - -- Primary opcode 63: - fdiv_3 = "fc000024FFF.", - fsub_3 = "fc000028FFF.", - fadd_3 = "fc00002aFFF.", - fsqrt_2 = "fc00002cF-F.", - fsel_4 = "fc00002eFFFF~.", - fre_2 = "fc000030F-F.", - fmul_3 = "fc000032FF-F.", - frsqrte_2 = "fc000034F-F.", - fmsub_4 = "fc000038FFFF~.", - fmadd_4 = "fc00003aFFFF~.", - fnmsub_4 = "fc00003cFFFF~.", - fnmadd_4 = "fc00003eFFFF~.", - fcmpu_3 = "fc000000XFF", - fcpsgn_3 = "fc000010FFF.", - fcmpo_3 = "fc000040XFF", - mtfsb1_1 = "fc00004cA", - fneg_2 = "fc000050F-F.", - mcrfs_2 = "fc000080XX", - mtfsb0_1 = "fc00008cA", - fmr_2 = "fc000090F-F.", - frsp_2 = "fc000018F-F.", - fctiw_2 = "fc00001cF-F.", - fctiwz_2 = "fc00001eF-F.", - mtfsfi_2 = "fc00010cAA", -- NYI: upshift. - fnabs_2 = "fc000110F-F.", - fabs_2 = "fc000210F-F.", - frin_2 = "fc000310F-F.", - friz_2 = "fc000350F-F.", - frip_2 = "fc000390F-F.", - frim_2 = "fc0003d0F-F.", - mffs_1 = "fc00048eF.", - -- NYI: mtfsf, mtfsb0, mtfsb1. - fctid_2 = "fc00065cF-F.", - fctidz_2 = "fc00065eF-F.", - fcfid_2 = "fc00069cF-F.", - - -- Primary opcode 4, SPE APU extension: - evaddw_3 = "10000200RRR", - evaddiw_3 = "10000202RAR~", - evsubw_3 = "10000204RRR~", - evsubiw_3 = "10000206RAR~", - evabs_2 = "10000208RR", - evneg_2 = "10000209RR", - evextsb_2 = "1000020aRR", - evextsh_2 = "1000020bRR", - evrndw_2 = "1000020cRR", - evcntlzw_2 = "1000020dRR", - evcntlsw_2 = "1000020eRR", - brinc_3 = "1000020fRRR", - evand_3 = "10000211RRR", - evandc_3 = "10000212RRR", - evxor_3 = "10000216RRR", - evor_3 = "10000217RRR", - evmr_2 = "10000217RR=", - evnor_3 = "10000218RRR", - evnot_2 = "10000218RR=", - eveqv_3 = "10000219RRR", - evorc_3 = "1000021bRRR", - evnand_3 = "1000021eRRR", - evsrwu_3 = "10000220RRR", - evsrws_3 = "10000221RRR", - evsrwiu_3 = "10000222RRA", - evsrwis_3 = "10000223RRA", - evslw_3 = "10000224RRR", - evslwi_3 = "10000226RRA", - evrlw_3 = "10000228RRR", - evsplati_2 = "10000229RS", - evrlwi_3 = "1000022aRRA", - evsplatfi_2 = "1000022bRS", - evmergehi_3 = "1000022cRRR", - evmergelo_3 = "1000022dRRR", - evcmpgtu_3 = "10000230XRR", - evcmpgtu_2 = "10000230-RR", - evcmpgts_3 = "10000231XRR", - evcmpgts_2 = "10000231-RR", - evcmpltu_3 = "10000232XRR", - evcmpltu_2 = "10000232-RR", - evcmplts_3 = "10000233XRR", - evcmplts_2 = "10000233-RR", - evcmpeq_3 = "10000234XRR", - evcmpeq_2 = "10000234-RR", - evsel_4 = "10000278RRRW", - evsel_3 = "10000278RRR", - evfsadd_3 = "10000280RRR", - evfssub_3 = "10000281RRR", - evfsabs_2 = "10000284RR", - evfsnabs_2 = "10000285RR", - evfsneg_2 = "10000286RR", - evfsmul_3 = "10000288RRR", - evfsdiv_3 = "10000289RRR", - evfscmpgt_3 = "1000028cXRR", - evfscmpgt_2 = "1000028c-RR", - evfscmplt_3 = "1000028dXRR", - evfscmplt_2 = "1000028d-RR", - evfscmpeq_3 = "1000028eXRR", - evfscmpeq_2 = "1000028e-RR", - evfscfui_2 = "10000290R-R", - evfscfsi_2 = "10000291R-R", - evfscfuf_2 = "10000292R-R", - evfscfsf_2 = "10000293R-R", - evfsctui_2 = "10000294R-R", - evfsctsi_2 = "10000295R-R", - evfsctuf_2 = "10000296R-R", - evfsctsf_2 = "10000297R-R", - evfsctuiz_2 = "10000298R-R", - evfsctsiz_2 = "1000029aR-R", - evfststgt_3 = "1000029cXRR", - evfststgt_2 = "1000029c-RR", - evfststlt_3 = "1000029dXRR", - evfststlt_2 = "1000029d-RR", - evfststeq_3 = "1000029eXRR", - evfststeq_2 = "1000029e-RR", - efsadd_3 = "100002c0RRR", - efssub_3 = "100002c1RRR", - efsabs_2 = "100002c4RR", - efsnabs_2 = "100002c5RR", - efsneg_2 = "100002c6RR", - efsmul_3 = "100002c8RRR", - efsdiv_3 = "100002c9RRR", - efscmpgt_3 = "100002ccXRR", - efscmpgt_2 = "100002cc-RR", - efscmplt_3 = "100002cdXRR", - efscmplt_2 = "100002cd-RR", - efscmpeq_3 = "100002ceXRR", - efscmpeq_2 = "100002ce-RR", - efscfd_2 = "100002cfR-R", - efscfui_2 = "100002d0R-R", - efscfsi_2 = "100002d1R-R", - efscfuf_2 = "100002d2R-R", - efscfsf_2 = "100002d3R-R", - efsctui_2 = "100002d4R-R", - efsctsi_2 = "100002d5R-R", - efsctuf_2 = "100002d6R-R", - efsctsf_2 = "100002d7R-R", - efsctuiz_2 = "100002d8R-R", - efsctsiz_2 = "100002daR-R", - efststgt_3 = "100002dcXRR", - efststgt_2 = "100002dc-RR", - efststlt_3 = "100002ddXRR", - efststlt_2 = "100002dd-RR", - efststeq_3 = "100002deXRR", - efststeq_2 = "100002de-RR", - efdadd_3 = "100002e0RRR", - efdsub_3 = "100002e1RRR", - efdcfuid_2 = "100002e2R-R", - efdcfsid_2 = "100002e3R-R", - efdabs_2 = "100002e4RR", - efdnabs_2 = "100002e5RR", - efdneg_2 = "100002e6RR", - efdmul_3 = "100002e8RRR", - efddiv_3 = "100002e9RRR", - efdctuidz_2 = "100002eaR-R", - efdctsidz_2 = "100002ebR-R", - efdcmpgt_3 = "100002ecXRR", - efdcmpgt_2 = "100002ec-RR", - efdcmplt_3 = "100002edXRR", - efdcmplt_2 = "100002ed-RR", - efdcmpeq_3 = "100002eeXRR", - efdcmpeq_2 = "100002ee-RR", - efdcfs_2 = "100002efR-R", - efdcfui_2 = "100002f0R-R", - efdcfsi_2 = "100002f1R-R", - efdcfuf_2 = "100002f2R-R", - efdcfsf_2 = "100002f3R-R", - efdctui_2 = "100002f4R-R", - efdctsi_2 = "100002f5R-R", - efdctuf_2 = "100002f6R-R", - efdctsf_2 = "100002f7R-R", - efdctuiz_2 = "100002f8R-R", - efdctsiz_2 = "100002faR-R", - efdtstgt_3 = "100002fcXRR", - efdtstgt_2 = "100002fc-RR", - efdtstlt_3 = "100002fdXRR", - efdtstlt_2 = "100002fd-RR", - efdtsteq_3 = "100002feXRR", - efdtsteq_2 = "100002fe-RR", - evlddx_3 = "10000300RR0R", - evldd_2 = "10000301R8", - evldwx_3 = "10000302RR0R", - evldw_2 = "10000303R8", - evldhx_3 = "10000304RR0R", - evldh_2 = "10000305R8", - evlwhex_3 = "10000310RR0R", - evlwhe_2 = "10000311R4", - evlwhoux_3 = "10000314RR0R", - evlwhou_2 = "10000315R4", - evlwhosx_3 = "10000316RR0R", - evlwhos_2 = "10000317R4", - evstddx_3 = "10000320RR0R", - evstdd_2 = "10000321R8", - evstdwx_3 = "10000322RR0R", - evstdw_2 = "10000323R8", - evstdhx_3 = "10000324RR0R", - evstdh_2 = "10000325R8", - evstwhex_3 = "10000330RR0R", - evstwhe_2 = "10000331R4", - evstwhox_3 = "10000334RR0R", - evstwho_2 = "10000335R4", - evstwwex_3 = "10000338RR0R", - evstwwe_2 = "10000339R4", - evstwwox_3 = "1000033cRR0R", - evstwwo_2 = "1000033dR4", - evmhessf_3 = "10000403RRR", - evmhossf_3 = "10000407RRR", - evmheumi_3 = "10000408RRR", - evmhesmi_3 = "10000409RRR", - evmhesmf_3 = "1000040bRRR", - evmhoumi_3 = "1000040cRRR", - evmhosmi_3 = "1000040dRRR", - evmhosmf_3 = "1000040fRRR", - evmhessfa_3 = "10000423RRR", - evmhossfa_3 = "10000427RRR", - evmheumia_3 = "10000428RRR", - evmhesmia_3 = "10000429RRR", - evmhesmfa_3 = "1000042bRRR", - evmhoumia_3 = "1000042cRRR", - evmhosmia_3 = "1000042dRRR", - evmhosmfa_3 = "1000042fRRR", - evmwhssf_3 = "10000447RRR", - evmwlumi_3 = "10000448RRR", - evmwhumi_3 = "1000044cRRR", - evmwhsmi_3 = "1000044dRRR", - evmwhsmf_3 = "1000044fRRR", - evmwssf_3 = "10000453RRR", - evmwumi_3 = "10000458RRR", - evmwsmi_3 = "10000459RRR", - evmwsmf_3 = "1000045bRRR", - evmwhssfa_3 = "10000467RRR", - evmwlumia_3 = "10000468RRR", - evmwhumia_3 = "1000046cRRR", - evmwhsmia_3 = "1000046dRRR", - evmwhsmfa_3 = "1000046fRRR", - evmwssfa_3 = "10000473RRR", - evmwumia_3 = "10000478RRR", - evmwsmia_3 = "10000479RRR", - evmwsmfa_3 = "1000047bRRR", - evmra_2 = "100004c4RR", - evdivws_3 = "100004c6RRR", - evdivwu_3 = "100004c7RRR", - evmwssfaa_3 = "10000553RRR", - evmwumiaa_3 = "10000558RRR", - evmwsmiaa_3 = "10000559RRR", - evmwsmfaa_3 = "1000055bRRR", - evmwssfan_3 = "100005d3RRR", - evmwumian_3 = "100005d8RRR", - evmwsmian_3 = "100005d9RRR", - evmwsmfan_3 = "100005dbRRR", - evmergehilo_3 = "1000022eRRR", - evmergelohi_3 = "1000022fRRR", - evlhhesplatx_3 = "10000308RR0R", - evlhhesplat_2 = "10000309R2", - evlhhousplatx_3 = "1000030cRR0R", - evlhhousplat_2 = "1000030dR2", - evlhhossplatx_3 = "1000030eRR0R", - evlhhossplat_2 = "1000030fR2", - evlwwsplatx_3 = "10000318RR0R", - evlwwsplat_2 = "10000319R4", - evlwhsplatx_3 = "1000031cRR0R", - evlwhsplat_2 = "1000031dR4", - evaddusiaaw_2 = "100004c0RR", - evaddssiaaw_2 = "100004c1RR", - evsubfusiaaw_2 = "100004c2RR", - evsubfssiaaw_2 = "100004c3RR", - evaddumiaaw_2 = "100004c8RR", - evaddsmiaaw_2 = "100004c9RR", - evsubfumiaaw_2 = "100004caRR", - evsubfsmiaaw_2 = "100004cbRR", - evmheusiaaw_3 = "10000500RRR", - evmhessiaaw_3 = "10000501RRR", - evmhessfaaw_3 = "10000503RRR", - evmhousiaaw_3 = "10000504RRR", - evmhossiaaw_3 = "10000505RRR", - evmhossfaaw_3 = "10000507RRR", - evmheumiaaw_3 = "10000508RRR", - evmhesmiaaw_3 = "10000509RRR", - evmhesmfaaw_3 = "1000050bRRR", - evmhoumiaaw_3 = "1000050cRRR", - evmhosmiaaw_3 = "1000050dRRR", - evmhosmfaaw_3 = "1000050fRRR", - evmhegumiaa_3 = "10000528RRR", - evmhegsmiaa_3 = "10000529RRR", - evmhegsmfaa_3 = "1000052bRRR", - evmhogumiaa_3 = "1000052cRRR", - evmhogsmiaa_3 = "1000052dRRR", - evmhogsmfaa_3 = "1000052fRRR", - evmwlusiaaw_3 = "10000540RRR", - evmwlssiaaw_3 = "10000541RRR", - evmwlumiaaw_3 = "10000548RRR", - evmwlsmiaaw_3 = "10000549RRR", - evmheusianw_3 = "10000580RRR", - evmhessianw_3 = "10000581RRR", - evmhessfanw_3 = "10000583RRR", - evmhousianw_3 = "10000584RRR", - evmhossianw_3 = "10000585RRR", - evmhossfanw_3 = "10000587RRR", - evmheumianw_3 = "10000588RRR", - evmhesmianw_3 = "10000589RRR", - evmhesmfanw_3 = "1000058bRRR", - evmhoumianw_3 = "1000058cRRR", - evmhosmianw_3 = "1000058dRRR", - evmhosmfanw_3 = "1000058fRRR", - evmhegumian_3 = "100005a8RRR", - evmhegsmian_3 = "100005a9RRR", - evmhegsmfan_3 = "100005abRRR", - evmhogumian_3 = "100005acRRR", - evmhogsmian_3 = "100005adRRR", - evmhogsmfan_3 = "100005afRRR", - evmwlusianw_3 = "100005c0RRR", - evmwlssianw_3 = "100005c1RRR", - evmwlumianw_3 = "100005c8RRR", - evmwlsmianw_3 = "100005c9RRR", - - -- NYI: some 64 bit PowerPC and Book E instructions: - -- rldicl, rldicr, rldic, rldimi, rldcl, rldcr, sradi, 64 bit ext. add/sub, - -- extended addressing branches, cache management, loads and stores -} - --- Add mnemonics for "." variants. -do - local t = {} - for k,v in pairs(map_op) do - if sub(v, -1) == "." then - local v2 = sub(v, 1, 7)..char(byte(v, 8)+1)..sub(v, 9, -2) - t[sub(k, 1, -3).."."..sub(k, -2)] = v2 - end - end - for k,v in pairs(t) do - map_op[k] = v - end -end - --- Add more branch mnemonics. -for cond,c in pairs(map_cond) do - local b1 = "b"..cond - local c1 = (c%4)*0x00010000 + (c < 4 and 0x01000000 or 0) - -- bX[l] - map_op[b1.."_1"] = tohex(0x40800000 + c1).."K" - map_op[b1.."y_1"] = tohex(0x40a00000 + c1).."K" - map_op[b1.."l_1"] = tohex(0x40800001 + c1).."K" - map_op[b1.."_2"] = tohex(0x40800000 + c1).."-XK" - map_op[b1.."y_2"] = tohex(0x40a00000 + c1).."-XK" - map_op[b1.."l_2"] = tohex(0x40800001 + c1).."-XK" - -- bXlr[l] - map_op[b1.."lr_0"] = tohex(0x4c800020 + c1) - map_op[b1.."lrl_0"] = tohex(0x4c800021 + c1) - map_op[b1.."ctr_0"] = tohex(0x4c800420 + c1) - map_op[b1.."ctrl_0"] = tohex(0x4c800421 + c1) - -- bXctr[l] - map_op[b1.."lr_1"] = tohex(0x4c800020 + c1).."-X" - map_op[b1.."lrl_1"] = tohex(0x4c800021 + c1).."-X" - map_op[b1.."ctr_1"] = tohex(0x4c800420 + c1).."-X" - map_op[b1.."ctrl_1"] = tohex(0x4c800421 + c1).."-X" -end - ------------------------------------------------------------------------------- - -local function parse_gpr(expr) - local tname, ovreg = match(expr, "^([%w_]+):(r[1-3]?[0-9])$") - local tp = map_type[tname or expr] - if tp then - local reg = ovreg or tp.reg - if not reg then - werror("type `"..(tname or expr).."' needs a register override") - end - expr = reg - end - local r = match(expr, "^r([1-3]?[0-9])$") - if r then - r = tonumber(r) - if r <= 31 then return r, tp end - end - werror("bad register name `"..expr.."'") -end - -local function parse_fpr(expr) - local r = match(expr, "^f([1-3]?[0-9])$") - if r then - r = tonumber(r) - if r <= 31 then return r end - end - werror("bad register name `"..expr.."'") -end - -local function parse_cr(expr) - local r = match(expr, "^cr([0-7])$") - if r then return tonumber(r) end - werror("bad condition register name `"..expr.."'") -end - -local function parse_cond(expr) - local r, cond = match(expr, "^4%*cr([0-7])%+(%w%w)$") - if r then - r = tonumber(r) - local c = map_cond[cond] - if c and c < 4 then return r*4+c end - end - werror("bad condition bit name `"..expr.."'") -end - -local function parse_imm(imm, bits, shift, scale, signed) - local n = tonumber(imm) - if n then - if n % 2^scale == 0 then - n = n / 2^scale - if signed then - if n >= 0 then - if n < 2^(bits-1) then return n*2^shift end - else - if n >= -(2^(bits-1))-1 then return (n+2^bits)*2^shift end - end - else - if n >= 0 and n <= 2^bits-1 then return n*2^shift end - end - end - werror("out of range immediate `"..imm.."'") - elseif match(imm, "^r([1-3]?[0-9])$") or - match(imm, "^([%w_]+):(r[1-3]?[0-9])$") then - werror("expected immediate operand, got register") - else - waction("IMM", (signed and 32768 or 0)+scale*1024+bits*32+shift, imm) - return 0 - end -end - -local function parse_disp(disp) - local imm, reg = match(disp, "^(.*)%(([%w_:]+)%)$") - if imm then - local r = parse_gpr(reg) - if r == 0 then werror("cannot use r0 in displacement") end - return r*65536 + parse_imm(imm, 16, 0, 0, true) - end - local reg, tailr = match(disp, "^([%w_:]+)%s*(.*)$") - if reg and tailr ~= "" then - local r, tp = parse_gpr(reg) - if r == 0 then werror("cannot use r0 in displacement") end - if tp then - waction("IMM", 32768+16*32, format(tp.ctypefmt, tailr)) - return r*65536 - end - end - werror("bad displacement `"..disp.."'") -end - -local function parse_u5disp(disp, scale) - local imm, reg = match(disp, "^(.*)%(([%w_:]+)%)$") - if imm then - local r = parse_gpr(reg) - if r == 0 then werror("cannot use r0 in displacement") end - return r*65536 + parse_imm(imm, 5, 11, scale, false) - end - local reg, tailr = match(disp, "^([%w_:]+)%s*(.*)$") - if reg and tailr ~= "" then - local r, tp = parse_gpr(reg) - if r == 0 then werror("cannot use r0 in displacement") end - if tp then - waction("IMM", scale*1024+5*32+11, format(tp.ctypefmt, tailr)) - return r*65536 - end - end - werror("bad displacement `"..disp.."'") -end - -local function parse_label(label, def) - local prefix = sub(label, 1, 2) - -- =>label (pc label reference) - if prefix == "=>" then - return "PC", 0, sub(label, 3) - end - -- ->name (global label reference) - if prefix == "->" then - return "LG", map_global[sub(label, 3)] - end - if def then - -- [1-9] (local label definition) - if match(label, "^[1-9]$") then - return "LG", 10+tonumber(label) - end - else - -- [<>][1-9] (local label reference) - local dir, lnum = match(label, "^([<>])([1-9])$") - if dir then -- Fwd: 1-9, Bkwd: 11-19. - return "LG", lnum + (dir == ">" and 0 or 10) - end - -- extern label (extern label reference) - local extname = match(label, "^extern%s+(%S+)$") - if extname then - return "EXT", map_extern[extname] - end - end - werror("bad label `"..label.."'") -end - ------------------------------------------------------------------------------- - --- Handle opcodes defined with template strings. -map_op[".template__"] = function(params, template, nparams) - if not params then return sub(template, 9) end - local op = tonumber(sub(template, 1, 8), 16) - local n, rs = 1, 26 - - -- Limit number of section buffer positions used by a single dasm_put(). - -- A single opcode needs a maximum of 3 positions (rlwinm). - if secpos+3 > maxsecpos then wflush() end - local pos = wpos() - - -- Process each character. - for p in gmatch(sub(template, 9), ".") do - if p == "R" then - rs = rs - 5; op = op + parse_gpr(params[n]) * 2^rs; n = n + 1 - elseif p == "F" then - rs = rs - 5; op = op + parse_fpr(params[n]) * 2^rs; n = n + 1 - elseif p == "A" then - rs = rs - 5; op = op + parse_imm(params[n], 5, rs, 0, false); n = n + 1 - elseif p == "S" then - rs = rs - 5; op = op + parse_imm(params[n], 5, rs, 0, true); n = n + 1 - elseif p == "I" then - op = op + parse_imm(params[n], 16, 0, 0, true); n = n + 1 - elseif p == "U" then - op = op + parse_imm(params[n], 16, 0, 0, false); n = n + 1 - elseif p == "D" then - op = op + parse_disp(params[n]); n = n + 1 - elseif p == "2" then - op = op + parse_u5disp(params[n], 1); n = n + 1 - elseif p == "4" then - op = op + parse_u5disp(params[n], 2); n = n + 1 - elseif p == "8" then - op = op + parse_u5disp(params[n], 3); n = n + 1 - elseif p == "C" then - rs = rs - 5; op = op + parse_cond(params[n]) * 2^rs; n = n + 1 - elseif p == "X" then - rs = rs - 5; op = op + parse_cr(params[n]) * 2^(rs+2); n = n + 1 - elseif p == "W" then - op = op + parse_cr(params[n]); n = n + 1 - elseif p == "G" then - op = op + parse_imm(params[n], 8, 12, 0, false); n = n + 1 - elseif p == "J" or p == "K" then - local mode, n, s = parse_label(params[n], false) - if p == "K" then n = n + 2048 end - waction("REL_"..mode, n, s, 1) - n = n + 1 - elseif p == "0" then - local mm = 2^rs - local t = op % mm - if ((op - t) / mm) % 32 == 0 then werror("cannot use r0") end - elseif p == "=" or p == "%" then - local mm = 2^(rs + (p == "%" and 5 or 0)) - local t = ((op - op % mm) / mm) % 32 - rs = rs - 5 - op = op + t * 2^rs - elseif p == "~" then - local mm = 2^rs - local t1l = op % mm - local t1h = (op - t1l) / mm - local t2l = t1h % 32 - local t2h = (t1h - t2l) / 32 - local t3l = t2h % 32 - op = ((t2h - t3l + t2l)*32 + t3l)*mm + t1l - elseif p == "-" then - rs = rs - 5 - elseif p == "." then - -- Ignored. - else - assert(false) - end - end - wputpos(pos, op) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode to mark the position where the action list is to be emitted. -map_op[".actionlist_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeactions(out, name) end) -end - --- Pseudo-opcode to mark the position where the global enum is to be emitted. -map_op[".globals_1"] = function(params) - if not params then return "prefix" end - local prefix = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobals(out, prefix) end) -end - --- Pseudo-opcode to mark the position where the global names are to be emitted. -map_op[".globalnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobalnames(out, name) end) -end - --- Pseudo-opcode to mark the position where the extern names are to be emitted. -map_op[".externnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeexternnames(out, name) end) -end - ------------------------------------------------------------------------------- - --- Label pseudo-opcode (converted from trailing colon form). -map_op[".label_1"] = function(params) - if not params then return "[1-9] | ->global | =>pcexpr" end - if secpos+1 > maxsecpos then wflush() end - local mode, n, s = parse_label(params[1], true) - if mode == "EXT" then werror("bad label definition") end - waction("LABEL_"..mode, n, s, 1) -end - ------------------------------------------------------------------------------- - --- Pseudo-opcodes for data storage. -map_op[".long_*"] = function(params) - if not params then return "imm..." end - for _,p in ipairs(params) do - local n = tonumber(p) - if not n then werror("bad immediate `"..p.."'") end - if n < 0 then n = n + 2^32 end - wputw(n) - if secpos+2 > maxsecpos then wflush() end - end -end - --- Alignment pseudo-opcode. -map_op[".align_1"] = function(params) - if not params then return "numpow2" end - if secpos+1 > maxsecpos then wflush() end - local align = tonumber(params[1]) - if align then - local x = align - -- Must be a power of 2 in the range (2 ... 256). - for i=1,8 do - x = x / 2 - if x == 1 then - waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1. - return - end - end - end - werror("bad alignment") -end - ------------------------------------------------------------------------------- - --- Pseudo-opcode for (primitive) type definitions (map to C types). -map_op[".type_3"] = function(params, nparams) - if not params then - return nparams == 2 and "name, ctype" or "name, ctype, reg" - end - local name, ctype, reg = params[1], params[2], params[3] - if not match(name, "^[%a_][%w_]*$") then - werror("bad type name `"..name.."'") - end - local tp = map_type[name] - if tp then - werror("duplicate type `"..name.."'") - end - -- Add #type to defines. A bit unclean to put it in map_archdef. - map_archdef["#"..name] = "sizeof("..ctype..")" - -- Add new type and emit shortcut define. - local num = ctypenum + 1 - map_type[name] = { - ctype = ctype, - ctypefmt = format("Dt%X(%%s)", num), - reg = reg, - } - wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype)) - ctypenum = num -end -map_op[".type_2"] = map_op[".type_3"] - --- Dump type definitions. -local function dumptypes(out, lvl) - local t = {} - for name in pairs(map_type) do t[#t+1] = name end - sort(t) - out:write("Type definitions:\n") - for _,name in ipairs(t) do - local tp = map_type[name] - local reg = tp.reg or "" - out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Set the current section. -function _M.section(num) - waction("SECTION", num) - wflush(true) -- SECTION is a terminal action. -end - ------------------------------------------------------------------------------- - --- Dump architecture description. -function _M.dumparch(out) - out:write(format("DynASM %s version %s, released %s\n\n", - _info.arch, _info.version, _info.release)) - dumpactions(out) -end - --- Dump all user defined elements. -function _M.dumpdef(out, lvl) - dumptypes(out, lvl) - dumpglobals(out, lvl) - dumpexterns(out, lvl) -end - ------------------------------------------------------------------------------- - --- Pass callbacks from/to the DynASM core. -function _M.passcb(wl, we, wf, ww) - wline, werror, wfatal, wwarn = wl, we, wf, ww - return wflush -end - --- Setup the arch-specific module. -function _M.setup(arch, opt) - g_arch, g_opt = arch, opt -end - --- Merge the core maps and the arch-specific maps. -function _M.mergemaps(map_coreop, map_def) - setmetatable(map_op, { __index = map_coreop }) - setmetatable(map_def, { __index = map_archdef }) - return map_op, map_def -end - -return _M - ------------------------------------------------------------------------------- - diff --git a/third_party/dynasm/dasm_proto.h b/third_party/dynasm/dasm_proto.h deleted file mode 100644 index 3002811..0000000 --- a/third_party/dynasm/dasm_proto.h +++ /dev/null @@ -1,83 +0,0 @@ -/* -** DynASM encoding engine prototypes. -** Copyright (C) 2005-2012 Mike Pall. All rights reserved. -** Released under the MIT license. See dynasm.lua for full copyright notice. -*/ - -#ifndef _DASM_PROTO_H -#define _DASM_PROTO_H - -#include -#include - -#define DASM_IDENT "DynASM 1.3.0" -#define DASM_VERSION 10300 /* 1.3.0 */ - -#ifndef Dst_DECL -#define Dst_DECL dasm_State **Dst -#endif - -#ifndef Dst_REF -#define Dst_REF (*Dst) -#endif - -#ifndef DASM_FDEF -#define DASM_FDEF extern -#endif - -#ifndef DASM_M_GROW -#define DASM_M_GROW(ctx, t, p, sz, need) \ - do { \ - size_t _sz = (sz), _need = (need); \ - if (_sz < _need) { \ - if (_sz < 16) _sz = 16; \ - while (_sz < _need) _sz += _sz; \ - (p) = (t *)realloc((p), _sz); \ - if ((p) == NULL) exit(1); \ - (sz) = _sz; \ - } \ - } while(0) -#endif - -#ifndef DASM_M_FREE -#define DASM_M_FREE(ctx, p, sz) free(p) -#endif - -/* Internal DynASM encoder state. */ -typedef struct dasm_State dasm_State; - - -/* Initialize and free DynASM state. */ -DASM_FDEF void dasm_init(Dst_DECL, int maxsection); -DASM_FDEF void dasm_free(Dst_DECL); - -/* Setup global array. Must be called before dasm_setup(). */ -DASM_FDEF void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl); - -/* Grow PC label array. Can be called after dasm_setup(), too. */ -DASM_FDEF void dasm_growpc(Dst_DECL, unsigned int maxpc); - -/* Setup encoder. */ -DASM_FDEF void dasm_setup(Dst_DECL, const void *actionlist); - -/* Feed encoder with actions. Calls are generated by pre-processor. */ -DASM_FDEF void dasm_put(Dst_DECL, int start, ...); - -/* Link sections and return the resulting size. */ -DASM_FDEF int dasm_link(Dst_DECL, size_t *szp); - -/* Encode sections into buffer. */ -DASM_FDEF int dasm_encode(Dst_DECL, void *buffer); - -/* Get PC label offset. */ -DASM_FDEF int dasm_getpclabel(Dst_DECL, unsigned int pc); - -#ifdef DASM_CHECKS -/* Optional sanity checker to call between isolated encoding steps. */ -DASM_FDEF int dasm_checkstep(Dst_DECL, int secmatch); -#else -#define dasm_checkstep(a, b) 0 -#endif - - -#endif /* _DASM_PROTO_H */ diff --git a/third_party/dynasm/dasm_x64.lua b/third_party/dynasm/dasm_x64.lua deleted file mode 100644 index bae72ac..0000000 --- a/third_party/dynasm/dasm_x64.lua +++ /dev/null @@ -1,12 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM x64 module. --- --- Copyright (C) 2005-2012 Mike Pall. All rights reserved. --- See dynasm.lua for full copyright notice. ------------------------------------------------------------------------------- --- This module just sets 64 bit mode for the combined x86/x64 module. --- All the interesting stuff is there. ------------------------------------------------------------------------------- - -x64 = true -- Using a global is an ugly, but effective solution. -return require("dasm_x86") diff --git a/third_party/dynasm/dasm_x86.h b/third_party/dynasm/dasm_x86.h deleted file mode 100644 index 7c6dcd3..0000000 --- a/third_party/dynasm/dasm_x86.h +++ /dev/null @@ -1,470 +0,0 @@ -/* -** DynASM x86 encoding engine. -** Copyright (C) 2005-2012 Mike Pall. All rights reserved. -** Released under the MIT license. See dynasm.lua for full copyright notice. -*/ - -#include -#include -#include -#include - -#define DASM_ARCH "x86" - -#ifndef DASM_EXTERN -#define DASM_EXTERN(a,b,c,d) 0 -#endif - -/* Action definitions. DASM_STOP must be 255. */ -enum { - DASM_DISP = 233, - DASM_IMM_S, DASM_IMM_B, DASM_IMM_W, DASM_IMM_D, DASM_IMM_WB, DASM_IMM_DB, - DASM_VREG, DASM_SPACE, DASM_SETLABEL, DASM_REL_A, DASM_REL_LG, DASM_REL_PC, - DASM_IMM_LG, DASM_IMM_PC, DASM_LABEL_LG, DASM_LABEL_PC, DASM_ALIGN, - DASM_EXTERN, DASM_ESC, DASM_MARK, DASM_SECTION, DASM_STOP -}; - -/* Maximum number of section buffer positions for a single dasm_put() call. */ -#define DASM_MAXSECPOS 25 - -/* DynASM encoder status codes. Action list offset or number are or'ed in. */ -#define DASM_S_OK 0x00000000 -#define DASM_S_NOMEM 0x01000000 -#define DASM_S_PHASE 0x02000000 -#define DASM_S_MATCH_SEC 0x03000000 -#define DASM_S_RANGE_I 0x11000000 -#define DASM_S_RANGE_SEC 0x12000000 -#define DASM_S_RANGE_LG 0x13000000 -#define DASM_S_RANGE_PC 0x14000000 -#define DASM_S_RANGE_VREG 0x15000000 -#define DASM_S_UNDEF_L 0x21000000 -#define DASM_S_UNDEF_PC 0x22000000 - -/* Macros to convert positions (8 bit section + 24 bit index). */ -#define DASM_POS2IDX(pos) ((pos)&0x00ffffff) -#define DASM_POS2BIAS(pos) ((pos)&0xff000000) -#define DASM_SEC2POS(sec) ((sec)<<24) -#define DASM_POS2SEC(pos) ((pos)>>24) -#define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos)) - -/* Action list type. */ -typedef const unsigned char *dasm_ActList; - -/* Per-section structure. */ -typedef struct dasm_Section { - int *rbuf; /* Biased buffer pointer (negative section bias). */ - int *buf; /* True buffer pointer. */ - size_t bsize; /* Buffer size in bytes. */ - int pos; /* Biased buffer position. */ - int epos; /* End of biased buffer position - max single put. */ - int ofs; /* Byte offset into section. */ -} dasm_Section; - -/* Core structure holding the DynASM encoding state. */ -struct dasm_State { - size_t psize; /* Allocated size of this structure. */ - dasm_ActList actionlist; /* Current actionlist pointer. */ - int *lglabels; /* Local/global chain/pos ptrs. */ - size_t lgsize; - int *pclabels; /* PC label chains/pos ptrs. */ - size_t pcsize; - void **globals; /* Array of globals (bias -10). */ - dasm_Section *section; /* Pointer to active section. */ - size_t codesize; /* Total size of all code sections. */ - int maxsection; /* 0 <= sectionidx < maxsection. */ - int status; /* Status code. */ - dasm_Section sections[1]; /* All sections. Alloc-extended. */ -}; - -/* The size of the core structure depends on the max. number of sections. */ -#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section)) - - -/* Initialize DynASM state. */ -void dasm_init(Dst_DECL, int maxsection) -{ - dasm_State *D; - size_t psz = 0; - int i; - Dst_REF = NULL; - DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection)); - D = Dst_REF; - D->psize = psz; - D->lglabels = NULL; - D->lgsize = 0; - D->pclabels = NULL; - D->pcsize = 0; - D->globals = NULL; - D->maxsection = maxsection; - for (i = 0; i < maxsection; i++) { - D->sections[i].buf = NULL; /* Need this for pass3. */ - D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i); - D->sections[i].bsize = 0; - D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */ - } -} - -/* Free DynASM state. */ -void dasm_free(Dst_DECL) -{ - dasm_State *D = Dst_REF; - int i; - for (i = 0; i < D->maxsection; i++) - if (D->sections[i].buf) - DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize); - if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize); - if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize); - DASM_M_FREE(Dst, D, D->psize); -} - -/* Setup global label array. Must be called before dasm_setup(). */ -void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl) -{ - dasm_State *D = Dst_REF; - D->globals = gl - 10; /* Negative bias to compensate for locals. */ - DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int)); -} - -/* Grow PC label array. Can be called after dasm_setup(), too. */ -void dasm_growpc(Dst_DECL, unsigned int maxpc) -{ - dasm_State *D = Dst_REF; - size_t osz = D->pcsize; - DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int)); - memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz); -} - -/* Setup encoder. */ -void dasm_setup(Dst_DECL, const void *actionlist) -{ - dasm_State *D = Dst_REF; - int i; - D->actionlist = (dasm_ActList)actionlist; - D->status = DASM_S_OK; - D->section = &D->sections[0]; - memset((void *)D->lglabels, 0, D->lgsize); - if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize); - for (i = 0; i < D->maxsection; i++) { - D->sections[i].pos = DASM_SEC2POS(i); - D->sections[i].ofs = 0; - } -} - - -#ifdef DASM_CHECKS -#define CK(x, st) \ - do { if (!(x)) { \ - D->status = DASM_S_##st|(int)(p-D->actionlist-1); return; } } while (0) -#define CKPL(kind, st) \ - do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \ - D->status=DASM_S_RANGE_##st|(int)(p-D->actionlist-1); return; } } while (0) -#else -#define CK(x, st) ((void)0) -#define CKPL(kind, st) ((void)0) -#endif - -/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */ -void dasm_put(Dst_DECL, int start, ...) -{ - va_list ap; - dasm_State *D = Dst_REF; - dasm_ActList p = D->actionlist + start; - dasm_Section *sec = D->section; - int pos = sec->pos, ofs = sec->ofs, mrm = 4; - int *b; - - if (pos >= sec->epos) { - DASM_M_GROW(Dst, int, sec->buf, sec->bsize, - sec->bsize + 2*DASM_MAXSECPOS*sizeof(int)); - sec->rbuf = sec->buf - DASM_POS2BIAS(pos); - sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos); - } - - b = sec->rbuf; - b[pos++] = start; - - va_start(ap, start); - while (1) { - int action = *p++; - if (action < DASM_DISP) { - ofs++; - } else if (action <= DASM_REL_A) { - int n = va_arg(ap, int); - b[pos++] = n; - switch (action) { - case DASM_DISP: - if (n == 0) { if ((mrm&7) == 4) mrm = p[-2]; if ((mrm&7) != 5) break; } - case DASM_IMM_DB: if (((n+128)&-256) == 0) goto ob; - case DASM_REL_A: /* Assumes ptrdiff_t is int. !x64 */ - case DASM_IMM_D: ofs += 4; break; - case DASM_IMM_S: CK(((n+128)&-256) == 0, RANGE_I); goto ob; - case DASM_IMM_B: CK((n&-256) == 0, RANGE_I); ob: ofs++; break; - case DASM_IMM_WB: if (((n+128)&-256) == 0) goto ob; - case DASM_IMM_W: CK((n&-65536) == 0, RANGE_I); ofs += 2; break; - case DASM_SPACE: p++; ofs += n; break; - case DASM_SETLABEL: b[pos-2] = -0x40000000; break; /* Neg. label ofs. */ - case DASM_VREG: CK((n&-8) == 0 && (n != 4 || (*p&1) == 0), RANGE_VREG); - if (*p++ == 1 && *p == DASM_DISP) mrm = n; continue; - } - mrm = 4; - } else { - int *pl, n; - switch (action) { - case DASM_REL_LG: - case DASM_IMM_LG: - n = *p++; pl = D->lglabels + n; - if (n <= 246) { CKPL(lg, LG); goto putrel; } /* Bkwd rel or global. */ - pl -= 246; n = *pl; - if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */ - goto linkrel; - case DASM_REL_PC: - case DASM_IMM_PC: pl = D->pclabels + va_arg(ap, int); CKPL(pc, PC); - putrel: - n = *pl; - if (n < 0) { /* Label exists. Get label pos and store it. */ - b[pos] = -n; - } else { - linkrel: - b[pos] = n; /* Else link to rel chain, anchored at label. */ - *pl = pos; - } - pos++; - ofs += 4; /* Maximum offset needed. */ - if (action == DASM_REL_LG || action == DASM_REL_PC) - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_LABEL_LG: pl = D->lglabels + *p++; CKPL(lg, LG); goto putlabel; - case DASM_LABEL_PC: pl = D->pclabels + va_arg(ap, int); CKPL(pc, PC); - putlabel: - n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; } - *pl = -pos; /* Label exists now. */ - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_ALIGN: - ofs += *p++; /* Maximum alignment needed (arg is 2**n-1). */ - b[pos++] = ofs; /* Store pass1 offset estimate. */ - break; - case DASM_EXTERN: p += 2; ofs += 4; break; - case DASM_ESC: p++; ofs++; break; - case DASM_MARK: mrm = p[-2]; break; - case DASM_SECTION: - n = *p; CK(n < D->maxsection, RANGE_SEC); D->section = &D->sections[n]; - case DASM_STOP: goto stop; - } - } - } -stop: - va_end(ap); - sec->pos = pos; - sec->ofs = ofs; -} -#undef CK - -/* Pass 2: Link sections, shrink branches/aligns, fix label offsets. */ -int dasm_link(Dst_DECL, size_t *szp) -{ - dasm_State *D = Dst_REF; - int secnum; - int ofs = 0; - -#ifdef DASM_CHECKS - *szp = 0; - if (D->status != DASM_S_OK) return D->status; - { - int pc; - for (pc = 0; pc*sizeof(int) < D->pcsize; pc++) - if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc; - } -#endif - - { /* Handle globals not defined in this translation unit. */ - int idx; - for (idx = 10; idx*sizeof(int) < D->lgsize; idx++) { - int n = D->lglabels[idx]; - /* Undefined label: Collapse rel chain and replace with marker (< 0). */ - while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; } - } - } - - /* Combine all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->rbuf; - int pos = DASM_SEC2POS(secnum); - int lastpos = sec->pos; - - while (pos != lastpos) { - dasm_ActList p = D->actionlist + b[pos++]; - while (1) { - int op, action = *p++; - switch (action) { - case DASM_REL_LG: p++; op = p[-3]; goto rel_pc; - case DASM_REL_PC: op = p[-2]; rel_pc: { - int shrink = op == 0xe9 ? 3 : ((op&0xf0) == 0x80 ? 4 : 0); - if (shrink) { /* Shrinkable branch opcode? */ - int lofs, lpos = b[pos]; - if (lpos < 0) goto noshrink; /* Ext global? */ - lofs = *DASM_POS2PTR(D, lpos); - if (lpos > pos) { /* Fwd label: add cumulative section offsets. */ - int i; - for (i = secnum; i < DASM_POS2SEC(lpos); i++) - lofs += D->sections[i].ofs; - } else { - lofs -= ofs; /* Bkwd label: unfix offset. */ - } - lofs -= b[pos+1]; /* Short branch ok? */ - if (lofs >= -128-shrink && lofs <= 127) ofs -= shrink; /* Yes. */ - else { noshrink: shrink = 0; } /* No, cannot shrink op. */ - } - b[pos+1] = shrink; - pos += 2; - break; - } - case DASM_SPACE: case DASM_IMM_LG: case DASM_VREG: p++; - case DASM_DISP: case DASM_IMM_S: case DASM_IMM_B: case DASM_IMM_W: - case DASM_IMM_D: case DASM_IMM_WB: case DASM_IMM_DB: - case DASM_SETLABEL: case DASM_REL_A: case DASM_IMM_PC: pos++; break; - case DASM_LABEL_LG: p++; - case DASM_LABEL_PC: b[pos++] += ofs; break; /* Fix label offset. */ - case DASM_ALIGN: ofs -= (b[pos++]+ofs)&*p++; break; /* Adjust ofs. */ - case DASM_EXTERN: p += 2; break; - case DASM_ESC: p++; break; - case DASM_MARK: break; - case DASM_SECTION: case DASM_STOP: goto stop; - } - } - stop: (void)0; - } - ofs += sec->ofs; /* Next section starts right after current section. */ - } - - D->codesize = ofs; /* Total size of all code sections */ - *szp = ofs; - return DASM_S_OK; -} - -#define dasmb(x) *cp++ = (unsigned char)(x) -#ifndef DASM_ALIGNED_WRITES -#define dasmw(x) \ - do { *((unsigned short *)cp) = (unsigned short)(x); cp+=2; } while (0) -#define dasmd(x) \ - do { *((unsigned int *)cp) = (unsigned int)(x); cp+=4; } while (0) -#else -#define dasmw(x) do { dasmb(x); dasmb((x)>>8); } while (0) -#define dasmd(x) do { dasmw(x); dasmw((x)>>16); } while (0) -#endif - -/* Pass 3: Encode sections. */ -int dasm_encode(Dst_DECL, void *buffer) -{ - dasm_State *D = Dst_REF; - unsigned char *base = (unsigned char *)buffer; - unsigned char *cp = base; - int secnum; - - /* Encode all code sections. No support for data sections (yet). */ - for (secnum = 0; secnum < D->maxsection; secnum++) { - dasm_Section *sec = D->sections + secnum; - int *b = sec->buf; - int *endb = sec->rbuf + sec->pos; - - while (b != endb) { - dasm_ActList p = D->actionlist + *b++; - unsigned char *mark = NULL; - while (1) { - int action = *p++; - int n = (action >= DASM_DISP && action <= DASM_ALIGN) ? *b++ : 0; - switch (action) { - case DASM_DISP: if (!mark) mark = cp; { - unsigned char *mm = mark; - if (*p != DASM_IMM_DB && *p != DASM_IMM_WB) mark = NULL; - if (n == 0) { int mrm = mm[-1]&7; if (mrm == 4) mrm = mm[0]&7; - if (mrm != 5) { mm[-1] -= 0x80; break; } } - if (((n+128) & -256) != 0) goto wd; else mm[-1] -= 0x40; - } - case DASM_IMM_S: case DASM_IMM_B: wb: dasmb(n); break; - case DASM_IMM_DB: if (((n+128)&-256) == 0) { - db: if (!mark) mark = cp; mark[-2] += 2; mark = NULL; goto wb; - } else mark = NULL; - case DASM_IMM_D: wd: dasmd(n); break; - case DASM_IMM_WB: if (((n+128)&-256) == 0) goto db; else mark = NULL; - case DASM_IMM_W: dasmw(n); break; - case DASM_VREG: { int t = *p++; if (t >= 2) n<<=3; cp[-1] |= n; break; } - case DASM_REL_LG: p++; if (n >= 0) goto rel_pc; - b++; n = (int)(ptrdiff_t)D->globals[-n]; - case DASM_REL_A: rel_a: n -= (int)(ptrdiff_t)(cp+4); goto wd; /* !x64 */ - case DASM_REL_PC: rel_pc: { - int shrink = *b++; - int *pb = DASM_POS2PTR(D, n); if (*pb < 0) { n = pb[1]; goto rel_a; } - n = *pb - ((int)(cp-base) + 4-shrink); - if (shrink == 0) goto wd; - if (shrink == 4) { cp--; cp[-1] = *cp-0x10; } else cp[-1] = 0xeb; - goto wb; - } - case DASM_IMM_LG: - p++; if (n < 0) { n = (int)(ptrdiff_t)D->globals[-n]; goto wd; } - case DASM_IMM_PC: { - int *pb = DASM_POS2PTR(D, n); - n = *pb < 0 ? pb[1] : (*pb + (int)(ptrdiff_t)base); - goto wd; - } - case DASM_LABEL_LG: { - int idx = *p++; - if (idx >= 10) - D->globals[idx] = (void *)(base + (*p == DASM_SETLABEL ? *b : n)); - break; - } - case DASM_LABEL_PC: case DASM_SETLABEL: break; - case DASM_SPACE: { int fill = *p++; while (n--) *cp++ = fill; break; } - case DASM_ALIGN: - n = *p++; - while (((cp-base) & n)) *cp++ = 0x90; /* nop */ - break; - case DASM_EXTERN: n = DASM_EXTERN(Dst, cp, p[1], *p); p += 2; goto wd; - case DASM_MARK: mark = cp; break; - case DASM_ESC: action = *p++; - default: *cp++ = action; break; - case DASM_SECTION: case DASM_STOP: goto stop; - } - } - stop: (void)0; - } - } - - if (base + D->codesize != cp) /* Check for phase errors. */ - return DASM_S_PHASE; - return DASM_S_OK; -} - -/* Get PC label offset. */ -int dasm_getpclabel(Dst_DECL, unsigned int pc) -{ - dasm_State *D = Dst_REF; - if (pc*sizeof(int) < D->pcsize) { - int pos = D->pclabels[pc]; - if (pos < 0) return *DASM_POS2PTR(D, -pos); - if (pos > 0) return -1; /* Undefined. */ - } - return -2; /* Unused or out of range. */ -} - -#ifdef DASM_CHECKS -/* Optional sanity checker to call between isolated encoding steps. */ -int dasm_checkstep(Dst_DECL, int secmatch) -{ - dasm_State *D = Dst_REF; - if (D->status == DASM_S_OK) { - int i; - for (i = 1; i <= 9; i++) { - if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_L|i; break; } - D->lglabels[i] = 0; - } - } - if (D->status == DASM_S_OK && secmatch >= 0 && - D->section != &D->sections[secmatch]) - D->status = DASM_S_MATCH_SEC|(int)(D->section-D->sections); - return D->status; -} -#endif - diff --git a/third_party/dynasm/dasm_x86.lua b/third_party/dynasm/dasm_x86.lua deleted file mode 100644 index 3bebb83..0000000 --- a/third_party/dynasm/dasm_x86.lua +++ /dev/null @@ -1,1931 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM x86/x64 module. --- --- Copyright (C) 2005-2012 Mike Pall. All rights reserved. --- See dynasm.lua for full copyright notice. ------------------------------------------------------------------------------- - -local x64 = x64 - --- Module information: -local _info = { - arch = x64 and "x64" or "x86", - description = "DynASM x86/x64 module", - version = "1.3.0", - vernum = 10300, - release = "2011-05-05", - author = "Mike Pall", - license = "MIT", -} - --- Exported glue functions for the arch-specific module. -local _M = { _info = _info } - --- Cache library functions. -local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs -local assert, unpack, setmetatable = assert, unpack or table.unpack, setmetatable -local _s = string -local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char -local find, match, gmatch, gsub = _s.find, _s.match, _s.gmatch, _s.gsub -local concat, sort = table.concat, table.sort - --- Inherited tables and callbacks. -local g_opt, g_arch -local wline, werror, wfatal, wwarn - --- Action name list. --- CHECK: Keep this in sync with the C code! -local action_names = { - -- int arg, 1 buffer pos: - "DISP", "IMM_S", "IMM_B", "IMM_W", "IMM_D", "IMM_WB", "IMM_DB", - -- action arg (1 byte), int arg, 1 buffer pos (reg/num): - "VREG", "SPACE", -- !x64: VREG support NYI. - -- ptrdiff_t arg, 1 buffer pos (address): !x64 - "SETLABEL", "REL_A", - -- action arg (1 byte) or int arg, 2 buffer pos (link, offset): - "REL_LG", "REL_PC", - -- action arg (1 byte) or int arg, 1 buffer pos (link): - "IMM_LG", "IMM_PC", - -- action arg (1 byte) or int arg, 1 buffer pos (offset): - "LABEL_LG", "LABEL_PC", - -- action arg (1 byte), 1 buffer pos (offset): - "ALIGN", - -- action args (2 bytes), no buffer pos. - "EXTERN", - -- action arg (1 byte), no buffer pos. - "ESC", - -- no action arg, no buffer pos. - "MARK", - -- action arg (1 byte), no buffer pos, terminal action: - "SECTION", - -- no args, no buffer pos, terminal action: - "STOP" -} - --- Maximum number of section buffer positions for dasm_put(). --- CHECK: Keep this in sync with the C code! -local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines. - --- Action name -> action number (dynamically generated below). -local map_action = {} --- First action number. Everything below does not need to be escaped. -local actfirst = 256-#action_names - --- Action list buffer and string (only used to remove dupes). -local actlist = {} -local actstr = "" - --- Argument list for next dasm_put(). Start with offset 0 into action list. -local actargs = { 0 } - --- Current number of section buffer positions for dasm_put(). -local secpos = 1 - ------------------------------------------------------------------------------- - --- Compute action numbers for action names. -for n,name in ipairs(action_names) do - local num = actfirst + n - 1 - map_action[name] = num -end - --- Dump action names and numbers. -local function dumpactions(out) - out:write("DynASM encoding engine action codes:\n") - for n,name in ipairs(action_names) do - local num = map_action[name] - out:write(format(" %-10s %02X %d\n", name, num, num)) - end - out:write("\n") -end - --- Write action list buffer as a huge static C array. -local function writeactions(out, name) - local nn = #actlist - local last = actlist[nn] or 255 - actlist[nn] = nil -- Remove last byte. - if nn == 0 then nn = 1 end - out:write("static const unsigned char ", name, "[", nn, "] = {\n") - local s = " " - for n,b in ipairs(actlist) do - s = s..b.."," - if #s >= 75 then - assert(out:write(s, "\n")) - s = " " - end - end - out:write(s, last, "\n};\n\n") -- Add last byte back. -end - ------------------------------------------------------------------------------- - --- Add byte to action list. -local function wputxb(n) - assert(n >= 0 and n <= 255 and n % 1 == 0, "byte out of range") - actlist[#actlist+1] = n -end - --- Add action to list with optional arg. Advance buffer pos, too. -local function waction(action, a, num) - wputxb(assert(map_action[action], "bad action name `"..action.."'")) - if a then actargs[#actargs+1] = a end - if a or num then secpos = secpos + (num or 1) end -end - --- Add call to embedded DynASM C code. -local function wcall(func, args) - wline(format("dasm_%s(Dst, %s);", func, concat(args, ", ")), true) -end - --- Delete duplicate action list chunks. A tad slow, but so what. -local function dedupechunk(offset) - local al, as = actlist, actstr - local chunk = char(unpack(al, offset+1, #al)) - local orig = find(as, chunk, 1, true) - if orig then - actargs[1] = orig-1 -- Replace with original offset. - for i=offset+1,#al do al[i] = nil end -- Kill dupe. - else - actstr = as..chunk - end -end - --- Flush action list (intervening C code or buffer pos overflow). -local function wflush(term) - local offset = actargs[1] - if #actlist == offset then return end -- Nothing to flush. - if not term then waction("STOP") end -- Terminate action list. - dedupechunk(offset) - wcall("put", actargs) -- Add call to dasm_put(). - actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put(). - secpos = 1 -- The actionlist offset occupies a buffer position, too. -end - --- Put escaped byte. -local function wputb(n) - if n >= actfirst then waction("ESC") end -- Need to escape byte. - wputxb(n) -end - ------------------------------------------------------------------------------- - --- Global label name -> global label number. With auto assignment on 1st use. -local next_global = 10 -local map_global = setmetatable({}, { __index = function(t, name) - if not match(name, "^[%a_][%w_@]*$") then werror("bad global label") end - local n = next_global - if n > 246 then werror("too many global labels") end - next_global = n + 1 - t[name] = n - return n -end}) - --- Dump global labels. -local function dumpglobals(out, lvl) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("Global labels:\n") - for i=10,next_global-1 do - out:write(format(" %s\n", t[i])) - end - out:write("\n") -end - --- Write global label enum. -local function writeglobals(out, prefix) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("enum {\n") - for i=10,next_global-1 do - out:write(" ", prefix, gsub(t[i], "@.*", ""), ",\n") - end - out:write(" ", prefix, "_MAX\n};\n") -end - --- Write global label names. -local function writeglobalnames(out, name) - local t = {} - for name, n in pairs(map_global) do t[n] = name end - out:write("static const char *const ", name, "[] = {\n") - for i=10,next_global-1 do - out:write(" \"", t[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Extern label name -> extern label number. With auto assignment on 1st use. -local next_extern = -1 -local map_extern = setmetatable({}, { __index = function(t, name) - -- No restrictions on the name for now. - local n = next_extern - if n < -256 then werror("too many extern labels") end - next_extern = n - 1 - t[name] = n - return n -end}) - --- Dump extern labels. -local function dumpexterns(out, lvl) - local t = {} - for name, n in pairs(map_extern) do t[-n] = name end - out:write("Extern labels:\n") - for i=1,-next_extern-1 do - out:write(format(" %s\n", t[i])) - end - out:write("\n") -end - --- Write extern label names. -local function writeexternnames(out, name) - local t = {} - for name, n in pairs(map_extern) do t[-n] = name end - out:write("static const char *const ", name, "[] = {\n") - for i=1,-next_extern-1 do - out:write(" \"", t[i], "\",\n") - end - out:write(" (const char *)0\n};\n") -end - ------------------------------------------------------------------------------- - --- Arch-specific maps. -local map_archdef = {} -- Ext. register name -> int. name. -local map_reg_rev = {} -- Int. register name -> ext. name. -local map_reg_num = {} -- Int. register name -> register number. -local map_reg_opsize = {} -- Int. register name -> operand size. -local map_reg_valid_base = {} -- Int. register name -> valid base register? -local map_reg_valid_index = {} -- Int. register name -> valid index register? -local map_reg_needrex = {} -- Int. register name -> need rex vs. no rex. -local reg_list = {} -- Canonical list of int. register names. - -local map_type = {} -- Type name -> { ctype, reg } -local ctypenum = 0 -- Type number (for _PTx macros). - -local addrsize = x64 and "q" or "d" -- Size for address operands. - --- Helper functions to fill register maps. -local function mkrmap(sz, cl, names) - local cname = format("@%s", sz) - reg_list[#reg_list+1] = cname - map_archdef[cl] = cname - map_reg_rev[cname] = cl - map_reg_num[cname] = -1 - map_reg_opsize[cname] = sz - if sz == addrsize or sz == "d" then - map_reg_valid_base[cname] = true - map_reg_valid_index[cname] = true - end - if names then - for n,name in ipairs(names) do - local iname = format("@%s%x", sz, n-1) - reg_list[#reg_list+1] = iname - map_archdef[name] = iname - map_reg_rev[iname] = name - map_reg_num[iname] = n-1 - map_reg_opsize[iname] = sz - if sz == "b" and n > 4 then map_reg_needrex[iname] = false end - if sz == addrsize or sz == "d" then - map_reg_valid_base[iname] = true - map_reg_valid_index[iname] = true - end - end - end - for i=0,(x64 and sz ~= "f") and 15 or 7 do - local needrex = sz == "b" and i > 3 - local iname = format("@%s%x%s", sz, i, needrex and "R" or "") - if needrex then map_reg_needrex[iname] = true end - local name - if sz == "o" then name = format("xmm%d", i) - elseif sz == "f" then name = format("st%d", i) - else name = format("r%d%s", i, sz == addrsize and "" or sz) end - map_archdef[name] = iname - if not map_reg_rev[iname] then - reg_list[#reg_list+1] = iname - map_reg_rev[iname] = name - map_reg_num[iname] = i - map_reg_opsize[iname] = sz - if sz == addrsize or sz == "d" then - map_reg_valid_base[iname] = true - map_reg_valid_index[iname] = true - end - end - end - reg_list[#reg_list+1] = "" -end - --- Integer registers (qword, dword, word and byte sized). -if x64 then - mkrmap("q", "Rq", {"rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi"}) -end -mkrmap("d", "Rd", {"eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi"}) -mkrmap("w", "Rw", {"ax", "cx", "dx", "bx", "sp", "bp", "si", "di"}) -mkrmap("b", "Rb", {"al", "cl", "dl", "bl", "ah", "ch", "dh", "bh"}) -map_reg_valid_index[map_archdef.esp] = false -if x64 then map_reg_valid_index[map_archdef.rsp] = false end -map_archdef["Ra"] = "@"..addrsize - --- FP registers (internally tword sized, but use "f" as operand size). -mkrmap("f", "Rf") - --- SSE registers (oword sized, but qword and dword accessible). -mkrmap("o", "xmm") - --- Operand size prefixes to codes. -local map_opsize = { - byte = "b", word = "w", dword = "d", qword = "q", oword = "o", tword = "t", - aword = addrsize, -} - --- Operand size code to number. -local map_opsizenum = { - b = 1, w = 2, d = 4, q = 8, o = 16, t = 10, -} - --- Operand size code to name. -local map_opsizename = { - b = "byte", w = "word", d = "dword", q = "qword", o = "oword", t = "tword", - f = "fpword", -} - --- Valid index register scale factors. -local map_xsc = { - ["1"] = 0, ["2"] = 1, ["4"] = 2, ["8"] = 3, -} - --- Condition codes. -local map_cc = { - o = 0, no = 1, b = 2, nb = 3, e = 4, ne = 5, be = 6, nbe = 7, - s = 8, ns = 9, p = 10, np = 11, l = 12, nl = 13, le = 14, nle = 15, - c = 2, nae = 2, nc = 3, ae = 3, z = 4, nz = 5, na = 6, a = 7, - pe = 10, po = 11, nge = 12, ge = 13, ng = 14, g = 15, -} - - --- Reverse defines for registers. -function _M.revdef(s) - return gsub(s, "@%w+", map_reg_rev) -end - --- Dump register names and numbers -local function dumpregs(out) - out:write("Register names, sizes and internal numbers:\n") - for _,reg in ipairs(reg_list) do - if reg == "" then - out:write("\n") - else - local name = map_reg_rev[reg] - local num = map_reg_num[reg] - local opsize = map_opsizename[map_reg_opsize[reg]] - out:write(format(" %-5s %-8s %s\n", name, opsize, - num < 0 and "(variable)" or num)) - end - end -end - ------------------------------------------------------------------------------- - --- Put action for label arg (IMM_LG, IMM_PC, REL_LG, REL_PC). -local function wputlabel(aprefix, imm, num) - if type(imm) == "number" then - if imm < 0 then - waction("EXTERN") - wputxb(aprefix == "IMM_" and 0 or 1) - imm = -imm-1 - else - waction(aprefix.."LG", nil, num); - end - wputxb(imm) - else - waction(aprefix.."PC", imm, num) - end -end - --- Put signed byte or arg. -local function wputsbarg(n) - if type(n) == "number" then - if n < -128 or n > 127 then - werror("signed immediate byte out of range") - end - if n < 0 then n = n + 256 end - wputb(n) - else waction("IMM_S", n) end -end - --- Put unsigned byte or arg. -local function wputbarg(n) - if type(n) == "number" then - if n < 0 or n > 255 then - werror("unsigned immediate byte out of range") - end - wputb(n) - else waction("IMM_B", n) end -end - --- Put unsigned word or arg. -local function wputwarg(n) - if type(n) == "number" then - if n < 0 or n > 65535 then - werror("unsigned immediate word out of range") - end - local r = n%256; n = (n-r)/256; wputb(r); wputb(n); - else waction("IMM_W", n) end -end - --- Put signed or unsigned dword or arg. -local function wputdarg(n) - local tn = type(n) - if tn == "number" then - if n < 0 then n = n + 4294967296 end - local r = n%256; n = (n-r)/256; wputb(r); - r = n%256; n = (n-r)/256; wputb(r); - r = n%256; n = (n-r)/256; wputb(r); wputb(n); - elseif tn == "table" then - wputlabel("IMM_", n[1], 1) - else - waction("IMM_D", n) - end -end - --- Put operand-size dependent number or arg (defaults to dword). -local function wputszarg(sz, n) - if not sz or sz == "d" or sz == "q" then wputdarg(n) - elseif sz == "w" then wputwarg(n) - elseif sz == "b" then wputbarg(n) - elseif sz == "s" then wputsbarg(n) - else werror("bad operand size") end -end - --- Put multi-byte opcode with operand-size dependent modifications. -local function wputop(sz, op, rex) - local r - if rex ~= 0 and not x64 then werror("bad operand size") end - if sz == "w" then wputb(102) end - -- Needs >32 bit numbers, but only for crc32 eax, word [ebx] - if op >= 4294967296 then r = op%4294967296 wputb((op-r)/4294967296) op = r end - if op >= 16777216 then r = op % 16777216 wputb((op-r) / 16777216) op = r end - if op >= 65536 then - if rex ~= 0 then - local opc3 = op - op % 256 - if opc3 == 0x0f3a00 or opc3 == 0x0f3800 then - wputb(64 + rex % 16); rex = 0 - end - end - r = op % 65536 wputb((op-r) / 65536) op = r - end - if op >= 256 then - r = op % 256 - local b = (op-r) / 256 - if b == 15 and rex ~= 0 then wputb(64 + rex % 16); rex = 0 end - wputb(b) - op = r - end - if rex ~= 0 then wputb(64 + rex % 16) end - if sz == "b" then op = op - 1 end - wputb(op) -end - --- Put ModRM or SIB formatted byte. -local function wputmodrm(m, s, rm, vs, vrm) - assert(m < 4 and s < 16 and rm < 16, "bad modrm operands") - wputb(64*m + 8*(s%8) + (rm%8)) -end - --- Put ModRM/SIB plus optional displacement. -local function wputmrmsib(t, imark, s, vsreg) - local vreg, vxreg - local reg, xreg = t.reg, t.xreg - if reg and reg < 0 then reg = 0; vreg = t.vreg end - if xreg and xreg < 0 then xreg = 0; vxreg = t.vxreg end - if s < 0 then s = 0 end - - -- Register mode. - if sub(t.mode, 1, 1) == "r" then - wputmodrm(3, s, reg) - if vsreg then waction("VREG", vsreg); wputxb(2) end - if vreg then waction("VREG", vreg); wputxb(0) end - return - end - - local disp = t.disp - local tdisp = type(disp) - -- No base register? - if not reg then - local riprel = false - if xreg then - -- Indexed mode with index register only. - -- [xreg*xsc+disp] -> (0, s, esp) (xsc, xreg, ebp) - wputmodrm(0, s, 4) - if imark == "I" then waction("MARK") end - if vsreg then waction("VREG", vsreg); wputxb(2) end - wputmodrm(t.xsc, xreg, 5) - if vxreg then waction("VREG", vxreg); wputxb(3) end - else - -- Pure 32 bit displacement. - if x64 and tdisp ~= "table" then - wputmodrm(0, s, 4) -- [disp] -> (0, s, esp) (0, esp, ebp) - if imark == "I" then waction("MARK") end - wputmodrm(0, 4, 5) - else - riprel = x64 - wputmodrm(0, s, 5) -- [disp|rip-label] -> (0, s, ebp) - if imark == "I" then waction("MARK") end - end - if vsreg then waction("VREG", vsreg); wputxb(2) end - end - if riprel then -- Emit rip-relative displacement. - if match("UWSiI", imark) then - werror("NYI: rip-relative displacement followed by immediate") - end - -- The previous byte in the action buffer cannot be 0xe9 or 0x80-0x8f. - wputlabel("REL_", disp[1], 2) - else - wputdarg(disp) - end - return - end - - local m - if tdisp == "number" then -- Check displacement size at assembly time. - if disp == 0 and (reg%8) ~= 5 then -- [ebp] -> [ebp+0] (in SIB, too) - if not vreg then m = 0 end -- Force DISP to allow [Rd(5)] -> [ebp+0] - elseif disp >= -128 and disp <= 127 then m = 1 - else m = 2 end - elseif tdisp == "table" then - m = 2 - end - - -- Index register present or esp as base register: need SIB encoding. - if xreg or (reg%8) == 4 then - wputmodrm(m or 2, s, 4) -- ModRM. - if m == nil or imark == "I" then waction("MARK") end - if vsreg then waction("VREG", vsreg); wputxb(2) end - wputmodrm(t.xsc or 0, xreg or 4, reg) -- SIB. - if vxreg then waction("VREG", vxreg); wputxb(3) end - if vreg then waction("VREG", vreg); wputxb(1) end - else - wputmodrm(m or 2, s, reg) -- ModRM. - if (imark == "I" and (m == 1 or m == 2)) or - (m == nil and (vsreg or vreg)) then waction("MARK") end - if vsreg then waction("VREG", vsreg); wputxb(2) end - if vreg then waction("VREG", vreg); wputxb(1) end - end - - -- Put displacement. - if m == 1 then wputsbarg(disp) - elseif m == 2 then wputdarg(disp) - elseif m == nil then waction("DISP", disp) end -end - ------------------------------------------------------------------------------- - --- Return human-readable operand mode string. -local function opmodestr(op, args) - local m = {} - for i=1,#args do - local a = args[i] - m[#m+1] = sub(a.mode, 1, 1)..(a.opsize or "?") - end - return op.." "..concat(m, ",") -end - --- Convert number to valid integer or nil. -local function toint(expr) - local n = tonumber(expr) - if n then - if n % 1 ~= 0 or n < -2147483648 or n > 4294967295 then - werror("bad integer number `"..expr.."'") - end - return n - end -end - --- Parse immediate expression. -local function immexpr(expr) - -- &expr (pointer) - if sub(expr, 1, 1) == "&" then - return "iPJ", format("(ptrdiff_t)(%s)", sub(expr,2)) - end - - local prefix = sub(expr, 1, 2) - -- =>expr (pc label reference) - if prefix == "=>" then - return "iJ", sub(expr, 3) - end - -- ->name (global label reference) - if prefix == "->" then - return "iJ", map_global[sub(expr, 3)] - end - - -- [<>][1-9] (local label reference) - local dir, lnum = match(expr, "^([<>])([1-9])$") - if dir then -- Fwd: 247-255, Bkwd: 1-9. - return "iJ", lnum + (dir == ">" and 246 or 0) - end - - local extname = match(expr, "^extern%s+(%S+)$") - if extname then - return "iJ", map_extern[extname] - end - - -- expr (interpreted as immediate) - return "iI", expr -end - --- Parse displacement expression: +-num, +-expr, +-opsize*num -local function dispexpr(expr) - local disp = expr == "" and 0 or toint(expr) - if disp then return disp end - local c, dispt = match(expr, "^([+-])%s*(.+)$") - if c == "+" then - expr = dispt - elseif not c then - werror("bad displacement expression `"..expr.."'") - end - local opsize, tailops = match(dispt, "^(%w+)%s*%*%s*(.+)$") - local ops, imm = map_opsize[opsize], toint(tailops) - if ops and imm then - if c == "-" then imm = -imm end - return imm*map_opsizenum[ops] - end - local mode, iexpr = immexpr(dispt) - if mode == "iJ" then - if c == "-" then werror("cannot invert label reference") end - return { iexpr } - end - return expr -- Need to return original signed expression. -end - --- Parse register or type expression. -local function rtexpr(expr) - if not expr then return end - local tname, ovreg = match(expr, "^([%w_]+):(@[%w_]+)$") - local tp = map_type[tname or expr] - if tp then - local reg = ovreg or tp.reg - local rnum = map_reg_num[reg] - if not rnum then - werror("type `"..(tname or expr).."' needs a register override") - end - if not map_reg_valid_base[reg] then - werror("bad base register override `"..(map_reg_rev[reg] or reg).."'") - end - return reg, rnum, tp - end - return expr, map_reg_num[expr] -end - --- Parse operand and return { mode, opsize, reg, xreg, xsc, disp, imm }. -local function parseoperand(param) - local t = {} - - local expr = param - local opsize, tailops = match(param, "^(%w+)%s*(.+)$") - if opsize then - t.opsize = map_opsize[opsize] - if t.opsize then expr = tailops end - end - - local br = match(expr, "^%[%s*(.-)%s*%]$") - repeat - if br then - t.mode = "xm" - - -- [disp] - t.disp = toint(br) - if t.disp then - t.mode = x64 and "xm" or "xmO" - break - end - - -- [reg...] - local tp - local reg, tailr = match(br, "^([@%w_:]+)%s*(.*)$") - reg, t.reg, tp = rtexpr(reg) - if not t.reg then - -- [expr] - t.mode = x64 and "xm" or "xmO" - t.disp = dispexpr("+"..br) - break - end - - if t.reg == -1 then - t.vreg, tailr = match(tailr, "^(%b())(.*)$") - if not t.vreg then werror("bad variable register expression") end - end - - -- [xreg*xsc] or [xreg*xsc+-disp] or [xreg*xsc+-expr] - local xsc, tailsc = match(tailr, "^%*%s*([1248])%s*(.*)$") - if xsc then - if not map_reg_valid_index[reg] then - werror("bad index register `"..map_reg_rev[reg].."'") - end - t.xsc = map_xsc[xsc] - t.xreg = t.reg - t.vxreg = t.vreg - t.reg = nil - t.vreg = nil - t.disp = dispexpr(tailsc) - break - end - if not map_reg_valid_base[reg] then - werror("bad base register `"..map_reg_rev[reg].."'") - end - - -- [reg] or [reg+-disp] - t.disp = toint(tailr) or (tailr == "" and 0) - if t.disp then break end - - -- [reg+xreg...] - local xreg, tailx = match(tailr, "^+%s*([@%w_:]+)%s*(.*)$") - xreg, t.xreg, tp = rtexpr(xreg) - if not t.xreg then - -- [reg+-expr] - t.disp = dispexpr(tailr) - break - end - if not map_reg_valid_index[xreg] then - werror("bad index register `"..map_reg_rev[xreg].."'") - end - - if t.xreg == -1 then - t.vxreg, tailx = match(tailx, "^(%b())(.*)$") - if not t.vxreg then werror("bad variable register expression") end - end - - -- [reg+xreg*xsc...] - local xsc, tailsc = match(tailx, "^%*%s*([1248])%s*(.*)$") - if xsc then - t.xsc = map_xsc[xsc] - tailx = tailsc - end - - -- [...] or [...+-disp] or [...+-expr] - t.disp = dispexpr(tailx) - else - -- imm or opsize*imm - local imm = toint(expr) - if not imm and sub(expr, 1, 1) == "*" and t.opsize then - imm = toint(sub(expr, 2)) - if imm then - imm = imm * map_opsizenum[t.opsize] - t.opsize = nil - end - end - if imm then - if t.opsize then werror("bad operand size override") end - local m = "i" - if imm == 1 then m = m.."1" end - if imm >= 4294967168 and imm <= 4294967295 then imm = imm-4294967296 end - if imm >= -128 and imm <= 127 then m = m.."S" end - t.imm = imm - t.mode = m - break - end - - local tp - local reg, tailr = match(expr, "^([@%w_:]+)%s*(.*)$") - reg, t.reg, tp = rtexpr(reg) - if t.reg then - if t.reg == -1 then - t.vreg, tailr = match(tailr, "^(%b())(.*)$") - if not t.vreg then werror("bad variable register expression") end - end - -- reg - if tailr == "" then - if t.opsize then werror("bad operand size override") end - t.opsize = map_reg_opsize[reg] - if t.opsize == "f" then - t.mode = t.reg == 0 and "fF" or "f" - else - if reg == "@w4" or (x64 and reg == "@d4") then - wwarn("bad idea, try again with `"..(x64 and "rsp'" or "esp'")) - end - t.mode = t.reg == 0 and "rmR" or (reg == "@b1" and "rmC" or "rm") - end - t.needrex = map_reg_needrex[reg] - break - end - - -- type[idx], type[idx].field, type->field -> [reg+offset_expr] - if not tp then werror("bad operand `"..param.."'") end - t.mode = "xm" - t.disp = format(tp.ctypefmt, tailr) - else - t.mode, t.imm = immexpr(expr) - if sub(t.mode, -1) == "J" then - if t.opsize and t.opsize ~= addrsize then - werror("bad operand size override") - end - t.opsize = addrsize - end - end - end - until true - return t -end - ------------------------------------------------------------------------------- --- x86 Template String Description --- =============================== --- --- Each template string is a list of [match:]pattern pairs, --- separated by "|". The first match wins. No match means a --- bad or unsupported combination of operand modes or sizes. --- --- The match part and the ":" is omitted if the operation has --- no operands. Otherwise the first N characters are matched --- against the mode strings of each of the N operands. --- --- The mode string for each operand type is (see parseoperand()): --- Integer register: "rm", +"R" for eax, ax, al, +"C" for cl --- FP register: "f", +"F" for st0 --- Index operand: "xm", +"O" for [disp] (pure offset) --- Immediate: "i", +"S" for signed 8 bit, +"1" for 1, --- +"I" for arg, +"P" for pointer --- Any: +"J" for valid jump targets --- --- So a match character "m" (mixed) matches both an integer register --- and an index operand (to be encoded with the ModRM/SIB scheme). --- But "r" matches only a register and "x" only an index operand --- (e.g. for FP memory access operations). --- --- The operand size match string starts right after the mode match --- characters and ends before the ":". "dwb" or "qdwb" is assumed, if empty. --- The effective data size of the operation is matched against this list. --- --- If only the regular "b", "w", "d", "q", "t" operand sizes are --- present, then all operands must be the same size. Unspecified sizes --- are ignored, but at least one operand must have a size or the pattern --- won't match (use the "byte", "word", "dword", "qword", "tword" --- operand size overrides. E.g.: mov dword [eax], 1). --- --- If the list has a "1" or "2" prefix, the operand size is taken --- from the respective operand and any other operand sizes are ignored. --- If the list contains only ".", all operand sizes are ignored. --- If the list has a "/" prefix, the concatenated (mixed) operand sizes --- are compared to the match. --- --- E.g. "rrdw" matches for either two dword registers or two word --- registers. "Fx2dq" matches an st0 operand plus an index operand --- pointing to a dword (float) or qword (double). --- --- Every character after the ":" is part of the pattern string: --- Hex chars are accumulated to form the opcode (left to right). --- "n" disables the standard opcode mods --- (otherwise: -1 for "b", o16 prefix for "w", rex.w for "q") --- "X" Force REX.W. --- "r"/"R" adds the reg. number from the 1st/2nd operand to the opcode. --- "m"/"M" generates ModRM/SIB from the 1st/2nd operand. --- The spare 3 bits are either filled with the last hex digit or --- the result from a previous "r"/"R". The opcode is restored. --- --- All of the following characters force a flush of the opcode: --- "o"/"O" stores a pure 32 bit disp (offset) from the 1st/2nd operand. --- "S" stores a signed 8 bit immediate from the last operand. --- "U" stores an unsigned 8 bit immediate from the last operand. --- "W" stores an unsigned 16 bit immediate from the last operand. --- "i" stores an operand sized immediate from the last operand. --- "I" dito, but generates an action code to optionally modify --- the opcode (+2) for a signed 8 bit immediate. --- "J" generates one of the REL action codes from the last operand. --- ------------------------------------------------------------------------------- - --- Template strings for x86 instructions. Ordered by first opcode byte. --- Unimplemented opcodes (deliberate omissions) are marked with *. -local map_op = { - -- 00-05: add... - -- 06: *push es - -- 07: *pop es - -- 08-0D: or... - -- 0E: *push cs - -- 0F: two byte opcode prefix - -- 10-15: adc... - -- 16: *push ss - -- 17: *pop ss - -- 18-1D: sbb... - -- 1E: *push ds - -- 1F: *pop ds - -- 20-25: and... - es_0 = "26", - -- 27: *daa - -- 28-2D: sub... - cs_0 = "2E", - -- 2F: *das - -- 30-35: xor... - ss_0 = "36", - -- 37: *aaa - -- 38-3D: cmp... - ds_0 = "3E", - -- 3F: *aas - inc_1 = x64 and "m:FF0m" or "rdw:40r|m:FF0m", - dec_1 = x64 and "m:FF1m" or "rdw:48r|m:FF1m", - push_1 = (x64 and "rq:n50r|rw:50r|mq:nFF6m|mw:FF6m" or - "rdw:50r|mdw:FF6m").."|S.:6AS|ib:n6Ai|i.:68i", - pop_1 = x64 and "rq:n58r|rw:58r|mq:n8F0m|mw:8F0m" or "rdw:58r|mdw:8F0m", - -- 60: *pusha, *pushad, *pushaw - -- 61: *popa, *popad, *popaw - -- 62: *bound rdw,x - -- 63: x86: *arpl mw,rw - movsxd_2 = x64 and "rm/qd:63rM", - fs_0 = "64", - gs_0 = "65", - o16_0 = "66", - a16_0 = not x64 and "67" or nil, - a32_0 = x64 and "67", - -- 68: push idw - -- 69: imul rdw,mdw,idw - -- 6A: push ib - -- 6B: imul rdw,mdw,S - -- 6C: *insb - -- 6D: *insd, *insw - -- 6E: *outsb - -- 6F: *outsd, *outsw - -- 70-7F: jcc lb - -- 80: add... mb,i - -- 81: add... mdw,i - -- 82: *undefined - -- 83: add... mdw,S - test_2 = "mr:85Rm|rm:85rM|Ri:A9ri|mi:F70mi", - -- 86: xchg rb,mb - -- 87: xchg rdw,mdw - -- 88: mov mb,r - -- 89: mov mdw,r - -- 8A: mov r,mb - -- 8B: mov r,mdw - -- 8C: *mov mdw,seg - lea_2 = "rx1dq:8DrM", - -- 8E: *mov seg,mdw - -- 8F: pop mdw - nop_0 = "90", - xchg_2 = "Rrqdw:90R|rRqdw:90r|rm:87rM|mr:87Rm", - cbw_0 = "6698", - cwde_0 = "98", - cdqe_0 = "4898", - cwd_0 = "6699", - cdq_0 = "99", - cqo_0 = "4899", - -- 9A: *call iw:idw - wait_0 = "9B", - fwait_0 = "9B", - pushf_0 = "9C", - pushfd_0 = not x64 and "9C", - pushfq_0 = x64 and "9C", - popf_0 = "9D", - popfd_0 = not x64 and "9D", - popfq_0 = x64 and "9D", - sahf_0 = "9E", - lahf_0 = "9F", - mov_2 = "OR:A3o|RO:A1O|mr:89Rm|rm:8BrM|rib:nB0ri|ridw:B8ri|mi:C70mi", - movsb_0 = "A4", - movsw_0 = "66A5", - movsd_0 = "A5", - cmpsb_0 = "A6", - cmpsw_0 = "66A7", - cmpsd_0 = "A7", - -- A8: test Rb,i - -- A9: test Rdw,i - stosb_0 = "AA", - stosw_0 = "66AB", - stosd_0 = "AB", - lodsb_0 = "AC", - lodsw_0 = "66AD", - lodsd_0 = "AD", - scasb_0 = "AE", - scasw_0 = "66AF", - scasd_0 = "AF", - -- B0-B7: mov rb,i - -- B8-BF: mov rdw,i - -- C0: rol... mb,i - -- C1: rol... mdw,i - ret_1 = "i.:nC2W", - ret_0 = "C3", - -- C4: *les rdw,mq - -- C5: *lds rdw,mq - -- C6: mov mb,i - -- C7: mov mdw,i - -- C8: *enter iw,ib - leave_0 = "C9", - -- CA: *retf iw - -- CB: *retf - int3_0 = "CC", - int_1 = "i.:nCDU", - into_0 = "CE", - -- CF: *iret - -- D0: rol... mb,1 - -- D1: rol... mdw,1 - -- D2: rol... mb,cl - -- D3: rol... mb,cl - -- D4: *aam ib - -- D5: *aad ib - -- D6: *salc - -- D7: *xlat - -- D8-DF: floating point ops - -- E0: *loopne - -- E1: *loope - -- E2: *loop - -- E3: *jcxz, *jecxz - -- E4: *in Rb,ib - -- E5: *in Rdw,ib - -- E6: *out ib,Rb - -- E7: *out ib,Rdw - call_1 = x64 and "mq:nFF2m|J.:E8nJ" or "md:FF2m|J.:E8J", - jmp_1 = x64 and "mq:nFF4m|J.:E9nJ" or "md:FF4m|J.:E9J", -- short: EB - -- EA: *jmp iw:idw - -- EB: jmp ib - -- EC: *in Rb,dx - -- ED: *in Rdw,dx - -- EE: *out dx,Rb - -- EF: *out dx,Rdw - -- F0: *lock - int1_0 = "F1", - repne_0 = "F2", - repnz_0 = "F2", - rep_0 = "F3", - repe_0 = "F3", - repz_0 = "F3", - -- F4: *hlt - cmc_0 = "F5", - -- F6: test... mb,i; div... mb - -- F7: test... mdw,i; div... mdw - clc_0 = "F8", - stc_0 = "F9", - -- FA: *cli - cld_0 = "FC", - std_0 = "FD", - -- FE: inc... mb - -- FF: inc... mdw - - -- misc ops - not_1 = "m:F72m", - neg_1 = "m:F73m", - mul_1 = "m:F74m", - imul_1 = "m:F75m", - div_1 = "m:F76m", - idiv_1 = "m:F77m", - - imul_2 = "rmqdw:0FAFrM|rIqdw:69rmI|rSqdw:6BrmS|riqdw:69rmi", - imul_3 = "rmIqdw:69rMI|rmSqdw:6BrMS|rmiqdw:69rMi", - - movzx_2 = "rm/db:0FB6rM|rm/qb:|rm/wb:0FB6rM|rm/dw:0FB7rM|rm/qw:", - movsx_2 = "rm/db:0FBErM|rm/qb:|rm/wb:0FBErM|rm/dw:0FBFrM|rm/qw:", - - bswap_1 = "rqd:0FC8r", - bsf_2 = "rmqdw:0FBCrM", - bsr_2 = "rmqdw:0FBDrM", - bt_2 = "mrqdw:0FA3Rm|miqdw:0FBA4mU", - btc_2 = "mrqdw:0FBBRm|miqdw:0FBA7mU", - btr_2 = "mrqdw:0FB3Rm|miqdw:0FBA6mU", - bts_2 = "mrqdw:0FABRm|miqdw:0FBA5mU", - - rdtsc_0 = "0F31", -- P1+ - cpuid_0 = "0FA2", -- P1+ - - -- floating point ops - fst_1 = "ff:DDD0r|xd:D92m|xq:nDD2m", - fstp_1 = "ff:DDD8r|xd:D93m|xq:nDD3m|xt:DB7m", - fld_1 = "ff:D9C0r|xd:D90m|xq:nDD0m|xt:DB5m", - - fpop_0 = "DDD8", -- Alias for fstp st0. - - fist_1 = "xw:nDF2m|xd:DB2m", - fistp_1 = "xw:nDF3m|xd:DB3m|xq:nDF7m", - fild_1 = "xw:nDF0m|xd:DB0m|xq:nDF5m", - - fxch_0 = "D9C9", - fxch_1 = "ff:D9C8r", - fxch_2 = "fFf:D9C8r|Fff:D9C8R", - - fucom_1 = "ff:DDE0r", - fucom_2 = "Fff:DDE0R", - fucomp_1 = "ff:DDE8r", - fucomp_2 = "Fff:DDE8R", - fucomi_1 = "ff:DBE8r", -- P6+ - fucomi_2 = "Fff:DBE8R", -- P6+ - fucomip_1 = "ff:DFE8r", -- P6+ - fucomip_2 = "Fff:DFE8R", -- P6+ - fcomi_1 = "ff:DBF0r", -- P6+ - fcomi_2 = "Fff:DBF0R", -- P6+ - fcomip_1 = "ff:DFF0r", -- P6+ - fcomip_2 = "Fff:DFF0R", -- P6+ - fucompp_0 = "DAE9", - fcompp_0 = "DED9", - - fldcw_1 = "xw:nD95m", - fstcw_1 = "xw:n9BD97m", - fnstcw_1 = "xw:nD97m", - fstsw_1 = "Rw:n9BDFE0|xw:n9BDD7m", - fnstsw_1 = "Rw:nDFE0|xw:nDD7m", - fclex_0 = "9BDBE2", - fnclex_0 = "DBE2", - - fnop_0 = "D9D0", - -- D9D1-D9DF: unassigned - - fchs_0 = "D9E0", - fabs_0 = "D9E1", - -- D9E2: unassigned - -- D9E3: unassigned - ftst_0 = "D9E4", - fxam_0 = "D9E5", - -- D9E6: unassigned - -- D9E7: unassigned - fld1_0 = "D9E8", - fldl2t_0 = "D9E9", - fldl2e_0 = "D9EA", - fldpi_0 = "D9EB", - fldlg2_0 = "D9EC", - fldln2_0 = "D9ED", - fldz_0 = "D9EE", - -- D9EF: unassigned - - f2xm1_0 = "D9F0", - fyl2x_0 = "D9F1", - fptan_0 = "D9F2", - fpatan_0 = "D9F3", - fxtract_0 = "D9F4", - fprem1_0 = "D9F5", - fdecstp_0 = "D9F6", - fincstp_0 = "D9F7", - fprem_0 = "D9F8", - fyl2xp1_0 = "D9F9", - fsqrt_0 = "D9FA", - fsincos_0 = "D9FB", - frndint_0 = "D9FC", - fscale_0 = "D9FD", - fsin_0 = "D9FE", - fcos_0 = "D9FF", - - -- SSE, SSE2 - andnpd_2 = "rmo:660F55rM", - andnps_2 = "rmo:0F55rM", - andpd_2 = "rmo:660F54rM", - andps_2 = "rmo:0F54rM", - clflush_1 = "x.:0FAE7m", - cmppd_3 = "rmio:660FC2rMU", - cmpps_3 = "rmio:0FC2rMU", - cmpsd_3 = "rrio:F20FC2rMU|rxi/oq:", - cmpss_3 = "rrio:F30FC2rMU|rxi/od:", - comisd_2 = "rro:660F2FrM|rx/oq:", - comiss_2 = "rro:0F2FrM|rx/od:", - cvtdq2pd_2 = "rro:F30FE6rM|rx/oq:", - cvtdq2ps_2 = "rmo:0F5BrM", - cvtpd2dq_2 = "rmo:F20FE6rM", - cvtpd2ps_2 = "rmo:660F5ArM", - cvtpi2pd_2 = "rx/oq:660F2ArM", - cvtpi2ps_2 = "rx/oq:0F2ArM", - cvtps2dq_2 = "rmo:660F5BrM", - cvtps2pd_2 = "rro:0F5ArM|rx/oq:", - cvtsd2si_2 = "rr/do:F20F2DrM|rr/qo:|rx/dq:|rxq:", - cvtsd2ss_2 = "rro:F20F5ArM|rx/oq:", - cvtsi2sd_2 = "rm/od:F20F2ArM|rm/oq:F20F2ArXM", - cvtsi2ss_2 = "rm/od:F30F2ArM|rm/oq:F30F2ArXM", - cvtss2sd_2 = "rro:F30F5ArM|rx/od:", - cvtss2si_2 = "rr/do:F20F2CrM|rr/qo:|rxd:|rx/qd:", - cvttpd2dq_2 = "rmo:660FE6rM", - cvttps2dq_2 = "rmo:F30F5BrM", - cvttsd2si_2 = "rr/do:F20F2CrM|rr/qo:|rx/dq:|rxq:", - cvttss2si_2 = "rr/do:F30F2CrM|rr/qo:|rxd:|rx/qd:", - ldmxcsr_1 = "xd:0FAE2m", - lfence_0 = "0FAEE8", - maskmovdqu_2 = "rro:660FF7rM", - mfence_0 = "0FAEF0", - movapd_2 = "rmo:660F28rM|mro:660F29Rm", - movaps_2 = "rmo:0F28rM|mro:0F29Rm", - movd_2 = "rm/od:660F6ErM|rm/oq:660F6ErXM|mr/do:660F7ERm|mr/qo:", - movdqa_2 = "rmo:660F6FrM|mro:660F7FRm", - movdqu_2 = "rmo:F30F6FrM|mro:F30F7FRm", - movhlps_2 = "rro:0F12rM", - movhpd_2 = "rx/oq:660F16rM|xr/qo:n660F17Rm", - movhps_2 = "rx/oq:0F16rM|xr/qo:n0F17Rm", - movlhps_2 = "rro:0F16rM", - movlpd_2 = "rx/oq:660F12rM|xr/qo:n660F13Rm", - movlps_2 = "rx/oq:0F12rM|xr/qo:n0F13Rm", - movmskpd_2 = "rr/do:660F50rM", - movmskps_2 = "rr/do:0F50rM", - movntdq_2 = "xro:660FE7Rm", - movnti_2 = "xrqd:0FC3Rm", - movntpd_2 = "xro:660F2BRm", - movntps_2 = "xro:0F2BRm", - movq_2 = "rro:F30F7ErM|rx/oq:|xr/qo:n660FD6Rm", - movsd_2 = "rro:F20F10rM|rx/oq:|xr/qo:nF20F11Rm", - movss_2 = "rro:F30F10rM|rx/od:|xr/do:F30F11Rm", - movupd_2 = "rmo:660F10rM|mro:660F11Rm", - movups_2 = "rmo:0F10rM|mro:0F11Rm", - orpd_2 = "rmo:660F56rM", - orps_2 = "rmo:0F56rM", - packssdw_2 = "rmo:660F6BrM", - packsswb_2 = "rmo:660F63rM", - packuswb_2 = "rmo:660F67rM", - paddb_2 = "rmo:660FFCrM", - paddd_2 = "rmo:660FFErM", - paddq_2 = "rmo:660FD4rM", - paddsb_2 = "rmo:660FECrM", - paddsw_2 = "rmo:660FEDrM", - paddusb_2 = "rmo:660FDCrM", - paddusw_2 = "rmo:660FDDrM", - paddw_2 = "rmo:660FFDrM", - pand_2 = "rmo:660FDBrM", - pandn_2 = "rmo:660FDFrM", - pause_0 = "F390", - pavgb_2 = "rmo:660FE0rM", - pavgw_2 = "rmo:660FE3rM", - pcmpeqb_2 = "rmo:660F74rM", - pcmpeqd_2 = "rmo:660F76rM", - pcmpeqw_2 = "rmo:660F75rM", - pcmpgtb_2 = "rmo:660F64rM", - pcmpgtd_2 = "rmo:660F66rM", - pcmpgtw_2 = "rmo:660F65rM", - pextrw_3 = "rri/do:660FC5rMU|xri/wo:660F3A15nrMU", -- Mem op: SSE4.1 only. - pinsrw_3 = "rri/od:660FC4rMU|rxi/ow:", - pmaddwd_2 = "rmo:660FF5rM", - pmaxsw_2 = "rmo:660FEErM", - pmaxub_2 = "rmo:660FDErM", - pminsw_2 = "rmo:660FEArM", - pminub_2 = "rmo:660FDArM", - pmovmskb_2 = "rr/do:660FD7rM", - pmulhuw_2 = "rmo:660FE4rM", - pmulhw_2 = "rmo:660FE5rM", - pmullw_2 = "rmo:660FD5rM", - pmuludq_2 = "rmo:660FF4rM", - por_2 = "rmo:660FEBrM", - prefetchnta_1 = "xb:n0F180m", - prefetcht0_1 = "xb:n0F181m", - prefetcht1_1 = "xb:n0F182m", - prefetcht2_1 = "xb:n0F183m", - psadbw_2 = "rmo:660FF6rM", - pshufd_3 = "rmio:660F70rMU", - pshufhw_3 = "rmio:F30F70rMU", - pshuflw_3 = "rmio:F20F70rMU", - pslld_2 = "rmo:660FF2rM|rio:660F726mU", - pslldq_2 = "rio:660F737mU", - psllq_2 = "rmo:660FF3rM|rio:660F736mU", - psllw_2 = "rmo:660FF1rM|rio:660F716mU", - psrad_2 = "rmo:660FE2rM|rio:660F724mU", - psraw_2 = "rmo:660FE1rM|rio:660F714mU", - psrld_2 = "rmo:660FD2rM|rio:660F722mU", - psrldq_2 = "rio:660F733mU", - psrlq_2 = "rmo:660FD3rM|rio:660F732mU", - psrlw_2 = "rmo:660FD1rM|rio:660F712mU", - psubb_2 = "rmo:660FF8rM", - psubd_2 = "rmo:660FFArM", - psubq_2 = "rmo:660FFBrM", - psubsb_2 = "rmo:660FE8rM", - psubsw_2 = "rmo:660FE9rM", - psubusb_2 = "rmo:660FD8rM", - psubusw_2 = "rmo:660FD9rM", - psubw_2 = "rmo:660FF9rM", - punpckhbw_2 = "rmo:660F68rM", - punpckhdq_2 = "rmo:660F6ArM", - punpckhqdq_2 = "rmo:660F6DrM", - punpckhwd_2 = "rmo:660F69rM", - punpcklbw_2 = "rmo:660F60rM", - punpckldq_2 = "rmo:660F62rM", - punpcklqdq_2 = "rmo:660F6CrM", - punpcklwd_2 = "rmo:660F61rM", - pxor_2 = "rmo:660FEFrM", - rcpps_2 = "rmo:0F53rM", - rcpss_2 = "rro:F30F53rM|rx/od:", - rsqrtps_2 = "rmo:0F52rM", - rsqrtss_2 = "rmo:F30F52rM", - sfence_0 = "0FAEF8", - shufpd_3 = "rmio:660FC6rMU", - shufps_3 = "rmio:0FC6rMU", - stmxcsr_1 = "xd:0FAE3m", - ucomisd_2 = "rro:660F2ErM|rx/oq:", - ucomiss_2 = "rro:0F2ErM|rx/od:", - unpckhpd_2 = "rmo:660F15rM", - unpckhps_2 = "rmo:0F15rM", - unpcklpd_2 = "rmo:660F14rM", - unpcklps_2 = "rmo:0F14rM", - xorpd_2 = "rmo:660F57rM", - xorps_2 = "rmo:0F57rM", - - -- SSE3 ops - fisttp_1 = "xw:nDF1m|xd:DB1m|xq:nDD1m", - addsubpd_2 = "rmo:660FD0rM", - addsubps_2 = "rmo:F20FD0rM", - haddpd_2 = "rmo:660F7CrM", - haddps_2 = "rmo:F20F7CrM", - hsubpd_2 = "rmo:660F7DrM", - hsubps_2 = "rmo:F20F7DrM", - lddqu_2 = "rxo:F20FF0rM", - movddup_2 = "rmo:F20F12rM", - movshdup_2 = "rmo:F30F16rM", - movsldup_2 = "rmo:F30F12rM", - - -- SSSE3 ops - pabsb_2 = "rmo:660F381CrM", - pabsd_2 = "rmo:660F381ErM", - pabsw_2 = "rmo:660F381DrM", - palignr_3 = "rmio:660F3A0FrMU", - phaddd_2 = "rmo:660F3802rM", - phaddsw_2 = "rmo:660F3803rM", - phaddw_2 = "rmo:660F3801rM", - phsubd_2 = "rmo:660F3806rM", - phsubsw_2 = "rmo:660F3807rM", - phsubw_2 = "rmo:660F3805rM", - pmaddubsw_2 = "rmo:660F3804rM", - pmulhrsw_2 = "rmo:660F380BrM", - pshufb_2 = "rmo:660F3800rM", - psignb_2 = "rmo:660F3808rM", - psignd_2 = "rmo:660F380ArM", - psignw_2 = "rmo:660F3809rM", - - -- SSE4.1 ops - blendpd_3 = "rmio:660F3A0DrMU", - blendps_3 = "rmio:660F3A0CrMU", - blendvpd_3 = "rmRo:660F3815rM", - blendvps_3 = "rmRo:660F3814rM", - dppd_3 = "rmio:660F3A41rMU", - dpps_3 = "rmio:660F3A40rMU", - extractps_3 = "mri/do:660F3A17RmU|rri/qo:660F3A17RXmU", - insertps_3 = "rrio:660F3A41rMU|rxi/od:", - movntdqa_2 = "rmo:660F382ArM", - mpsadbw_3 = "rmio:660F3A42rMU", - packusdw_2 = "rmo:660F382BrM", - pblendvb_3 = "rmRo:660F3810rM", - pblendw_3 = "rmio:660F3A0ErMU", - pcmpeqq_2 = "rmo:660F3829rM", - pextrb_3 = "rri/do:660F3A14nRmU|rri/qo:|xri/bo:", - pextrd_3 = "mri/do:660F3A16RmU", - pextrq_3 = "mri/qo:660F3A16RmU", - -- pextrw is SSE2, mem operand is SSE4.1 only - phminposuw_2 = "rmo:660F3841rM", - pinsrb_3 = "rri/od:660F3A20nrMU|rxi/ob:", - pinsrd_3 = "rmi/od:660F3A22rMU", - pinsrq_3 = "rmi/oq:660F3A22rXMU", - pmaxsb_2 = "rmo:660F383CrM", - pmaxsd_2 = "rmo:660F383DrM", - pmaxud_2 = "rmo:660F383FrM", - pmaxuw_2 = "rmo:660F383ErM", - pminsb_2 = "rmo:660F3838rM", - pminsd_2 = "rmo:660F3839rM", - pminud_2 = "rmo:660F383BrM", - pminuw_2 = "rmo:660F383ArM", - pmovsxbd_2 = "rro:660F3821rM|rx/od:", - pmovsxbq_2 = "rro:660F3822rM|rx/ow:", - pmovsxbw_2 = "rro:660F3820rM|rx/oq:", - pmovsxdq_2 = "rro:660F3825rM|rx/oq:", - pmovsxwd_2 = "rro:660F3823rM|rx/oq:", - pmovsxwq_2 = "rro:660F3824rM|rx/od:", - pmovzxbd_2 = "rro:660F3831rM|rx/od:", - pmovzxbq_2 = "rro:660F3832rM|rx/ow:", - pmovzxbw_2 = "rro:660F3830rM|rx/oq:", - pmovzxdq_2 = "rro:660F3835rM|rx/oq:", - pmovzxwd_2 = "rro:660F3833rM|rx/oq:", - pmovzxwq_2 = "rro:660F3834rM|rx/od:", - pmuldq_2 = "rmo:660F3828rM", - pmulld_2 = "rmo:660F3840rM", - ptest_2 = "rmo:660F3817rM", - roundpd_3 = "rmio:660F3A09rMU", - roundps_3 = "rmio:660F3A08rMU", - roundsd_3 = "rrio:660F3A0BrMU|rxi/oq:", - roundss_3 = "rrio:660F3A0ArMU|rxi/od:", - - -- SSE4.2 ops - crc32_2 = "rmqd:F20F38F1rM|rm/dw:66F20F38F1rM|rm/db:F20F38F0rM|rm/qb:", - pcmpestri_3 = "rmio:660F3A61rMU", - pcmpestrm_3 = "rmio:660F3A60rMU", - pcmpgtq_2 = "rmo:660F3837rM", - pcmpistri_3 = "rmio:660F3A63rMU", - pcmpistrm_3 = "rmio:660F3A62rMU", - popcnt_2 = "rmqdw:F30FB8rM", - - -- SSE4a - extrq_2 = "rro:660F79rM", - extrq_3 = "riio:660F780mUU", - insertq_2 = "rro:F20F79rM", - insertq_4 = "rriio:F20F78rMUU", - lzcnt_2 = "rmqdw:F30FBDrM", - movntsd_2 = "xr/qo:nF20F2BRm", - movntss_2 = "xr/do:F30F2BRm", - -- popcnt is also in SSE4.2 -} - ------------------------------------------------------------------------------- - --- Arithmetic ops. -for name,n in pairs{ add = 0, ["or"] = 1, adc = 2, sbb = 3, - ["and"] = 4, sub = 5, xor = 6, cmp = 7 } do - local n8 = n * 8 - map_op[name.."_2"] = format( - "mr:%02XRm|rm:%02XrM|mI1qdw:81%XmI|mS1qdw:83%XmS|Ri1qdwb:%02Xri|mi1qdwb:81%Xmi", - 1+n8, 3+n8, n, n, 5+n8, n) -end - --- Shift ops. -for name,n in pairs{ rol = 0, ror = 1, rcl = 2, rcr = 3, - shl = 4, shr = 5, sar = 7, sal = 4 } do - map_op[name.."_2"] = format("m1:D1%Xm|mC1qdwb:D3%Xm|mi:C1%XmU", n, n, n) -end - --- Conditional ops. -for cc,n in pairs(map_cc) do - map_op["j"..cc.."_1"] = format("J.:n0F8%XJ", n) -- short: 7%X - map_op["set"..cc.."_1"] = format("mb:n0F9%X2m", n) - map_op["cmov"..cc.."_2"] = format("rmqdw:0F4%XrM", n) -- P6+ -end - --- FP arithmetic ops. -for name,n in pairs{ add = 0, mul = 1, com = 2, comp = 3, - sub = 4, subr = 5, div = 6, divr = 7 } do - local nc = 192 + n * 8 - local nr = nc + (n < 4 and 0 or (n % 2 == 0 and 8 or -8)) - local fn = "f"..name - map_op[fn.."_1"] = format("ff:D8%02Xr|xd:D8%Xm|xq:nDC%Xm", nc, n, n) - if n == 2 or n == 3 then - map_op[fn.."_2"] = format("Fff:D8%02XR|Fx2d:D8%XM|Fx2q:nDC%XM", nc, n, n) - else - map_op[fn.."_2"] = format("Fff:D8%02XR|fFf:DC%02Xr|Fx2d:D8%XM|Fx2q:nDC%XM", nc, nr, n, n) - map_op[fn.."p_1"] = format("ff:DE%02Xr", nr) - map_op[fn.."p_2"] = format("fFf:DE%02Xr", nr) - end - map_op["fi"..name.."_1"] = format("xd:DA%Xm|xw:nDE%Xm", n, n) -end - --- FP conditional moves. -for cc,n in pairs{ b=0, e=1, be=2, u=3, nb=4, ne=5, nbe=6, nu=7 } do - local n4 = n % 4 - local nc = 56000 + n4 * 8 + (n-n4) * 64 - map_op["fcmov"..cc.."_1"] = format("ff:%04Xr", nc) -- P6+ - map_op["fcmov"..cc.."_2"] = format("Fff:%04XR", nc) -- P6+ -end - --- SSE FP arithmetic ops. -for name,n in pairs{ sqrt = 1, add = 8, mul = 9, - sub = 12, min = 13, div = 14, max = 15 } do - map_op[name.."ps_2"] = format("rmo:0F5%XrM", n) - map_op[name.."ss_2"] = format("rro:F30F5%XrM|rx/od:", n) - map_op[name.."pd_2"] = format("rmo:660F5%XrM", n) - map_op[name.."sd_2"] = format("rro:F20F5%XrM|rx/oq:", n) -end - ------------------------------------------------------------------------------- - --- Process pattern string. -local function dopattern(pat, args, sz, op, needrex) - local digit, addin - local opcode = 0 - local szov = sz - local narg = 1 - local rex = 0 - - -- Limit number of section buffer positions used by a single dasm_put(). - -- A single opcode needs a maximum of 5 positions. - if secpos+5 > maxsecpos then wflush() end - - -- Process each character. - for c in gmatch(pat.."|", ".") do - if match(c, "%x") then -- Hex digit. - digit = byte(c) - 48 - if digit > 48 then digit = digit - 39 - elseif digit > 16 then digit = digit - 7 end - opcode = opcode*16 + digit - addin = nil - elseif c == "n" then -- Disable operand size mods for opcode. - szov = nil - elseif c == "X" then -- Force REX.W. - rex = 8 - elseif c == "r" then -- Merge 1st operand regno. into opcode. - addin = args[1]; opcode = opcode + (addin.reg % 8) - if narg < 2 then narg = 2 end - elseif c == "R" then -- Merge 2nd operand regno. into opcode. - addin = args[2]; opcode = opcode + (addin.reg % 8) - narg = 3 - elseif c == "m" or c == "M" then -- Encode ModRM/SIB. - local s - if addin then - s = addin.reg - opcode = opcode - (s%8) -- Undo regno opcode merge. - else - s = opcode % 16 -- Undo last digit. - opcode = (opcode - s) / 16 - end - local nn = c == "m" and 1 or 2 - local t = args[nn] - if narg <= nn then narg = nn + 1 end - if szov == "q" and rex == 0 then rex = rex + 8 end - if t.reg and t.reg > 7 then rex = rex + 1 end - if t.xreg and t.xreg > 7 then rex = rex + 2 end - if s > 7 then rex = rex + 4 end - if needrex then rex = rex + 16 end - wputop(szov, opcode, rex); opcode = nil - local imark = sub(pat, -1) -- Force a mark (ugly). - -- Put ModRM/SIB with regno/last digit as spare. - wputmrmsib(t, imark, s, addin and addin.vreg) - addin = nil - else - if opcode then -- Flush opcode. - if szov == "q" and rex == 0 then rex = rex + 8 end - if needrex then rex = rex + 16 end - if addin and addin.reg == -1 then - wputop(szov, opcode - 7, rex) - waction("VREG", addin.vreg); wputxb(0) - else - if addin and addin.reg > 7 then rex = rex + 1 end - wputop(szov, opcode, rex) - end - opcode = nil - end - if c == "|" then break end - if c == "o" then -- Offset (pure 32 bit displacement). - wputdarg(args[1].disp); if narg < 2 then narg = 2 end - elseif c == "O" then - wputdarg(args[2].disp); narg = 3 - else - -- Anything else is an immediate operand. - local a = args[narg] - narg = narg + 1 - local mode, imm = a.mode, a.imm - if mode == "iJ" and not match("iIJ", c) then - werror("bad operand size for label") - end - if c == "S" then - wputsbarg(imm) - elseif c == "U" then - wputbarg(imm) - elseif c == "W" then - wputwarg(imm) - elseif c == "i" or c == "I" then - if mode == "iJ" then - wputlabel("IMM_", imm, 1) - elseif mode == "iI" and c == "I" then - waction(sz == "w" and "IMM_WB" or "IMM_DB", imm) - else - wputszarg(sz, imm) - end - elseif c == "J" then - if mode == "iPJ" then - waction("REL_A", imm) -- !x64 (secpos) - else - wputlabel("REL_", imm, 2) - end - else - werror("bad char `"..c.."' in pattern `"..pat.."' for `"..op.."'") - end - end - end - end -end - ------------------------------------------------------------------------------- - --- Mapping of operand modes to short names. Suppress output with '#'. -local map_modename = { - r = "reg", R = "eax", C = "cl", x = "mem", m = "mrm", i = "imm", - f = "stx", F = "st0", J = "lbl", ["1"] = "1", - I = "#", S = "#", O = "#", -} - --- Return a table/string showing all possible operand modes. -local function templatehelp(template, nparams) - if nparams == 0 then return "" end - local t = {} - for tm in gmatch(template, "[^%|]+") do - local s = map_modename[sub(tm, 1, 1)] - s = s..gsub(sub(tm, 2, nparams), ".", function(c) - return ", "..map_modename[c] - end) - if not match(s, "#") then t[#t+1] = s end - end - return t -end - --- Match operand modes against mode match part of template. -local function matchtm(tm, args) - for i=1,#args do - if not match(args[i].mode, sub(tm, i, i)) then return end - end - return true -end - --- Handle opcodes defined with template strings. -map_op[".template__"] = function(params, template, nparams) - if not params then return templatehelp(template, nparams) end - local args = {} - - -- Zero-operand opcodes have no match part. - if #params == 0 then - dopattern(template, args, "d", params.op, nil) - return - end - - -- Determine common operand size (coerce undefined size) or flag as mixed. - local sz, szmix, needrex - for i,p in ipairs(params) do - args[i] = parseoperand(p) - local nsz = args[i].opsize - if nsz then - if sz and sz ~= nsz then szmix = true else sz = nsz end - end - local nrex = args[i].needrex - if nrex ~= nil then - if needrex == nil then - needrex = nrex - elseif needrex ~= nrex then - werror("bad mix of byte-addressable registers") - end - end - end - - -- Try all match:pattern pairs (separated by '|'). - local gotmatch, lastpat - for tm in gmatch(template, "[^%|]+") do - -- Split off size match (starts after mode match) and pattern string. - local szm, pat = match(tm, "^(.-):(.*)$", #args+1) - if pat == "" then pat = lastpat else lastpat = pat end - if matchtm(tm, args) then - local prefix = sub(szm, 1, 1) - if prefix == "/" then -- Match both operand sizes. - if args[1].opsize == sub(szm, 2, 2) and - args[2].opsize == sub(szm, 3, 3) then - dopattern(pat, args, sz, params.op, needrex) -- Process pattern. - return - end - else -- Match common operand size. - local szp = sz - if szm == "" then szm = x64 and "qdwb" or "dwb" end -- Default sizes. - if prefix == "1" then szp = args[1].opsize; szmix = nil - elseif prefix == "2" then szp = args[2].opsize; szmix = nil end - if not szmix and (prefix == "." or match(szm, szp or "#")) then - dopattern(pat, args, szp, params.op, needrex) -- Process pattern. - return - end - end - gotmatch = true - end - end - - local msg = "bad operand mode" - if gotmatch then - if szmix then - msg = "mixed operand size" - else - msg = sz and "bad operand size" or "missing operand size" - end - end - - werror(msg.." in `"..opmodestr(params.op, args).."'") -end - ------------------------------------------------------------------------------- - --- x64-specific opcode for 64 bit immediates and displacements. -if x64 then - function map_op.mov64_2(params) - if not params then return { "reg, imm", "reg, [disp]", "[disp], reg" } end - if secpos+2 > maxsecpos then wflush() end - local opcode, op64, sz, rex - local op64 = match(params[1], "^%[%s*(.-)%s*%]$") - if op64 then - local a = parseoperand(params[2]) - if a.mode ~= "rmR" then werror("bad operand mode") end - sz = a.opsize - rex = sz == "q" and 8 or 0 - opcode = 0xa3 - else - op64 = match(params[2], "^%[%s*(.-)%s*%]$") - local a = parseoperand(params[1]) - if op64 then - if a.mode ~= "rmR" then werror("bad operand mode") end - sz = a.opsize - rex = sz == "q" and 8 or 0 - opcode = 0xa1 - else - if sub(a.mode, 1, 1) ~= "r" or a.opsize ~= "q" then - werror("bad operand mode") - end - op64 = params[2] - opcode = 0xb8 + (a.reg%8) -- !x64: no VREG support. - rex = a.reg > 7 and 9 or 8 - end - end - wputop(sz, opcode, rex) - waction("IMM_D", format("(unsigned int)(%s)", op64)) - waction("IMM_D", format("(unsigned int)((%s)>>32)", op64)) - end -end - ------------------------------------------------------------------------------- - --- Pseudo-opcodes for data storage. -local function op_data(params) - if not params then return "imm..." end - local sz = sub(params.op, 2, 2) - if sz == "a" then sz = addrsize end - for _,p in ipairs(params) do - local a = parseoperand(p) - if sub(a.mode, 1, 1) ~= "i" or (a.opsize and a.opsize ~= sz) then - werror("bad mode or size in `"..p.."'") - end - if a.mode == "iJ" then - wputlabel("IMM_", a.imm, 1) - else - wputszarg(sz, a.imm) - end - if secpos+2 > maxsecpos then wflush() end - end -end - -map_op[".byte_*"] = op_data -map_op[".sbyte_*"] = op_data -map_op[".word_*"] = op_data -map_op[".dword_*"] = op_data -map_op[".aword_*"] = op_data - ------------------------------------------------------------------------------- - --- Pseudo-opcode to mark the position where the action list is to be emitted. -map_op[".actionlist_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeactions(out, name) end) -end - --- Pseudo-opcode to mark the position where the global enum is to be emitted. -map_op[".globals_1"] = function(params) - if not params then return "prefix" end - local prefix = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobals(out, prefix) end) -end - --- Pseudo-opcode to mark the position where the global names are to be emitted. -map_op[".globalnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeglobalnames(out, name) end) -end - --- Pseudo-opcode to mark the position where the extern names are to be emitted. -map_op[".externnames_1"] = function(params) - if not params then return "cvar" end - local name = params[1] -- No syntax check. You get to keep the pieces. - wline(function(out) writeexternnames(out, name) end) -end - ------------------------------------------------------------------------------- - --- Label pseudo-opcode (converted from trailing colon form). -map_op[".label_2"] = function(params) - if not params then return "[1-9] | ->global | =>pcexpr [, addr]" end - if secpos+2 > maxsecpos then wflush() end - local a = parseoperand(params[1]) - local mode, imm = a.mode, a.imm - if type(imm) == "number" and (mode == "iJ" or (imm >= 1 and imm <= 9)) then - -- Local label (1: ... 9:) or global label (->global:). - waction("LABEL_LG", nil, 1) - wputxb(imm) - elseif mode == "iJ" then - -- PC label (=>pcexpr:). - waction("LABEL_PC", imm) - else - werror("bad label definition") - end - -- SETLABEL must immediately follow LABEL_LG/LABEL_PC. - local addr = params[2] - if addr then - local a = parseoperand(addr) - if a.mode == "iPJ" then - waction("SETLABEL", a.imm) - else - werror("bad label assignment") - end - end -end -map_op[".label_1"] = map_op[".label_2"] - ------------------------------------------------------------------------------- - --- Alignment pseudo-opcode. -map_op[".align_1"] = function(params) - if not params then return "numpow2" end - if secpos+1 > maxsecpos then wflush() end - local align = tonumber(params[1]) or map_opsizenum[map_opsize[params[1]]] - if align then - local x = align - -- Must be a power of 2 in the range (2 ... 256). - for i=1,8 do - x = x / 2 - if x == 1 then - waction("ALIGN", nil, 1) - wputxb(align-1) -- Action byte is 2**n-1. - return - end - end - end - werror("bad alignment") -end - --- Spacing pseudo-opcode. -map_op[".space_2"] = function(params) - if not params then return "num [, filler]" end - if secpos+1 > maxsecpos then wflush() end - waction("SPACE", params[1]) - local fill = params[2] - if fill then - fill = tonumber(fill) - if not fill or fill < 0 or fill > 255 then werror("bad filler") end - end - wputxb(fill or 0) -end -map_op[".space_1"] = map_op[".space_2"] - ------------------------------------------------------------------------------- - --- Pseudo-opcode for (primitive) type definitions (map to C types). -map_op[".type_3"] = function(params, nparams) - if not params then - return nparams == 2 and "name, ctype" or "name, ctype, reg" - end - local name, ctype, reg = params[1], params[2], params[3] - if not match(name, "^[%a_][%w_]*$") then - werror("bad type name `"..name.."'") - end - local tp = map_type[name] - if tp then - werror("duplicate type `"..name.."'") - end - if reg and not map_reg_valid_base[reg] then - werror("bad base register `"..(map_reg_rev[reg] or reg).."'") - end - -- Add #type to defines. A bit unclean to put it in map_archdef. - map_archdef["#"..name] = "sizeof("..ctype..")" - -- Add new type and emit shortcut define. - local num = ctypenum + 1 - map_type[name] = { - ctype = ctype, - ctypefmt = format("Dt%X(%%s)", num), - reg = reg, - } - wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype)) - ctypenum = num -end -map_op[".type_2"] = map_op[".type_3"] - --- Dump type definitions. -local function dumptypes(out, lvl) - local t = {} - for name in pairs(map_type) do t[#t+1] = name end - sort(t) - out:write("Type definitions:\n") - for _,name in ipairs(t) do - local tp = map_type[name] - local reg = tp.reg and map_reg_rev[tp.reg] or "" - out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Set the current section. -function _M.section(num) - waction("SECTION") - wputxb(num) - wflush(true) -- SECTION is a terminal action. -end - ------------------------------------------------------------------------------- - --- Dump architecture description. -function _M.dumparch(out) - out:write(format("DynASM %s version %s, released %s\n\n", - _info.arch, _info.version, _info.release)) - dumpregs(out) - dumpactions(out) -end - --- Dump all user defined elements. -function _M.dumpdef(out, lvl) - dumptypes(out, lvl) - dumpglobals(out, lvl) - dumpexterns(out, lvl) -end - ------------------------------------------------------------------------------- - --- Pass callbacks from/to the DynASM core. -function _M.passcb(wl, we, wf, ww) - wline, werror, wfatal, wwarn = wl, we, wf, ww - return wflush -end - --- Setup the arch-specific module. -function _M.setup(arch, opt) - g_arch, g_opt = arch, opt -end - --- Merge the core maps and the arch-specific maps. -function _M.mergemaps(map_coreop, map_def) - setmetatable(map_op, { __index = map_coreop }) - setmetatable(map_def, { __index = map_archdef }) - return map_op, map_def -end - -return _M - ------------------------------------------------------------------------------- - diff --git a/third_party/dynasm/dynasm.lua b/third_party/dynasm/dynasm.lua deleted file mode 100644 index 2ef8164..0000000 --- a/third_party/dynasm/dynasm.lua +++ /dev/null @@ -1,1084 +0,0 @@ ------------------------------------------------------------------------------- --- DynASM. A dynamic assembler for code generation engines. --- Originally designed and implemented for LuaJIT. --- --- Copyright (C) 2005-2012 Mike Pall. All rights reserved. --- See below for full copyright notice. ------------------------------------------------------------------------------- - --- Application information. -local _info = { - name = "DynASM", - description = "A dynamic assembler for code generation engines", - version = "1.3.0", - vernum = 10300, - release = "2011-05-05", - author = "Mike Pall", - url = "http://luajit.org/dynasm.html", - license = "MIT", - copyright = [[ -Copyright (C) 2005-2012 Mike Pall. All rights reserved. - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -[ MIT license: http://www.opensource.org/licenses/mit-license.php ] -]], -} - --- Cache library functions. -local type, pairs, ipairs = type, pairs, ipairs -local pcall, error, assert = pcall, error, assert -local _s = string -local sub, match, gmatch, gsub = _s.sub, _s.match, _s.gmatch, _s.gsub -local format, rep, upper = _s.format, _s.rep, _s.upper -local _t = table -local insert, remove, concat, sort = _t.insert, _t.remove, _t.concat, _t.sort -local exit = os.exit -local io = io -local stdin, stdout, stderr = io.stdin, io.stdout, io.stderr - ------------------------------------------------------------------------------- - --- Program options. -local g_opt = {} - --- Global state for current file. -local g_fname, g_curline, g_indent, g_lineno, g_synclineno, g_arch -local g_errcount = 0 - --- Write buffer for output file. -local g_wbuffer, g_capbuffer - ------------------------------------------------------------------------------- - --- Write an output line (or callback function) to the buffer. -local function wline(line, needindent) - local buf = g_capbuffer or g_wbuffer - buf[#buf+1] = needindent and g_indent..line or line - g_synclineno = g_synclineno + 1 -end - --- Write assembler line as a comment, if requestd. -local function wcomment(aline) - if g_opt.comment then - wline(g_opt.comment..aline..g_opt.endcomment, true) - end -end - --- Resync CPP line numbers. -local function wsync() - if g_synclineno ~= g_lineno and g_opt.cpp then - wline("# "..g_lineno..' "'..g_fname..'"') - g_synclineno = g_lineno - end -end - --- Dummy action flush function. Replaced with arch-specific function later. -local function wflush(term) -end - --- Dump all buffered output lines. -local function wdumplines(out, buf) - for _,line in ipairs(buf) do - if type(line) == "string" then - assert(out:write(line, "\n")) - else - -- Special callback to dynamically insert lines after end of processing. - line(out) - end - end -end - ------------------------------------------------------------------------------- - --- Emit an error. Processing continues with next statement. -local function werror(msg) - error(format("%s:%s: error: %s:\n%s", g_fname, g_lineno, msg, g_curline), 0) -end - --- Emit a fatal error. Processing stops. -local function wfatal(msg) - g_errcount = "fatal" - werror(msg) -end - --- Print a warning. Processing continues. -local function wwarn(msg) - stderr:write(format("%s:%s: warning: %s:\n%s\n", - g_fname, g_lineno, msg, g_curline)) -end - --- Print caught error message. But suppress excessive errors. -local function wprinterr(...) - if type(g_errcount) == "number" then - -- Regular error. - g_errcount = g_errcount + 1 - if g_errcount < 21 then -- Seems to be a reasonable limit. - stderr:write(...) - elseif g_errcount == 21 then - stderr:write(g_fname, - ":*: warning: too many errors (suppressed further messages).\n") - end - else - -- Fatal error. - stderr:write(...) - return true -- Stop processing. - end -end - ------------------------------------------------------------------------------- - --- Map holding all option handlers. -local opt_map = {} -local opt_current - --- Print error and exit with error status. -local function opterror(...) - stderr:write("dynasm.lua: ERROR: ", ...) - stderr:write("\n") - exit(1) -end - --- Get option parameter. -local function optparam(args) - local argn = args.argn - local p = args[argn] - if not p then - opterror("missing parameter for option `", opt_current, "'.") - end - args.argn = argn + 1 - return p -end - ------------------------------------------------------------------------------- - --- Core pseudo-opcodes. -local map_coreop = {} --- Dummy opcode map. Replaced by arch-specific map. -local map_op = {} - --- Forward declarations. -local dostmt -local readfile - ------------------------------------------------------------------------------- - --- Map for defines (initially empty, chains to arch-specific map). -local map_def = {} - --- Pseudo-opcode to define a substitution. -map_coreop[".define_2"] = function(params, nparams) - if not params then return nparams == 1 and "name" or "name, subst" end - local name, def = params[1], params[2] or "1" - if not match(name, "^[%a_][%w_]*$") then werror("bad or duplicate define") end - map_def[name] = def -end -map_coreop[".define_1"] = map_coreop[".define_2"] - --- Define a substitution on the command line. -function opt_map.D(args) - local namesubst = optparam(args) - local name, subst = match(namesubst, "^([%a_][%w_]*)=(.*)$") - if name then - map_def[name] = subst - elseif match(namesubst, "^[%a_][%w_]*$") then - map_def[namesubst] = "1" - else - opterror("bad define") - end -end - --- Undefine a substitution on the command line. -function opt_map.U(args) - local name = optparam(args) - if match(name, "^[%a_][%w_]*$") then - map_def[name] = nil - else - opterror("bad define") - end -end - --- Helper for definesubst. -local gotsubst - -local function definesubst_one(word) - local subst = map_def[word] - if subst then gotsubst = word; return subst else return word end -end - --- Iteratively substitute defines. -local function definesubst(stmt) - -- Limit number of iterations. - for i=1,100 do - gotsubst = false - stmt = gsub(stmt, "#?[%w_]+", definesubst_one) - if not gotsubst then break end - end - if gotsubst then wfatal("recursive define involving `"..gotsubst.."'") end - return stmt -end - --- Dump all defines. -local function dumpdefines(out, lvl) - local t = {} - for name in pairs(map_def) do - t[#t+1] = name - end - sort(t) - out:write("Defines:\n") - for _,name in ipairs(t) do - local subst = map_def[name] - if g_arch then subst = g_arch.revdef(subst) end - out:write(format(" %-20s %s\n", name, subst)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Support variables for conditional assembly. -local condlevel = 0 -local condstack = {} - --- Evaluate condition with a Lua expression. Substitutions already performed. -local function cond_eval(cond) - local func, err - if setfenv then - func, err = loadstring("return "..cond, "=expr") - else - -- No globals. All unknown identifiers evaluate to nil. - func, err = load("return "..cond, "=expr", "t", {}) - end - if func then - if setfenv then - setfenv(func, {}) -- No globals. All unknown identifiers evaluate to nil. - end - local ok, res = pcall(func) - if ok then - if res == 0 then return false end -- Oh well. - return not not res - end - err = res - end - wfatal("bad condition: "..err) -end - --- Skip statements until next conditional pseudo-opcode at the same level. -local function stmtskip() - local dostmt_save = dostmt - local lvl = 0 - dostmt = function(stmt) - local op = match(stmt, "^%s*(%S+)") - if op == ".if" then - lvl = lvl + 1 - elseif lvl ~= 0 then - if op == ".endif" then lvl = lvl - 1 end - elseif op == ".elif" or op == ".else" or op == ".endif" then - dostmt = dostmt_save - dostmt(stmt) - end - end -end - --- Pseudo-opcodes for conditional assembly. -map_coreop[".if_1"] = function(params) - if not params then return "condition" end - local lvl = condlevel + 1 - local res = cond_eval(params[1]) - condlevel = lvl - condstack[lvl] = res - if not res then stmtskip() end -end - -map_coreop[".elif_1"] = function(params) - if not params then return "condition" end - if condlevel == 0 then wfatal(".elif without .if") end - local lvl = condlevel - local res = condstack[lvl] - if res then - if res == "else" then wfatal(".elif after .else") end - else - res = cond_eval(params[1]) - if res then - condstack[lvl] = res - return - end - end - stmtskip() -end - -map_coreop[".else_0"] = function(params) - if condlevel == 0 then wfatal(".else without .if") end - local lvl = condlevel - local res = condstack[lvl] - condstack[lvl] = "else" - if res then - if res == "else" then wfatal(".else after .else") end - stmtskip() - end -end - -map_coreop[".endif_0"] = function(params) - local lvl = condlevel - if lvl == 0 then wfatal(".endif without .if") end - condlevel = lvl - 1 -end - --- Check for unfinished conditionals. -local function checkconds() - if g_errcount ~= "fatal" and condlevel ~= 0 then - wprinterr(g_fname, ":*: error: unbalanced conditional\n") - end -end - ------------------------------------------------------------------------------- - --- Search for a file in the given path and open it for reading. -local function pathopen(path, name) - local dirsep = match(package.path, "\\") and "\\" or "/" - for _,p in ipairs(path) do - local fullname = p == "" and name or p..dirsep..name - local fin = io.open(fullname, "r") - if fin then - g_fname = fullname - return fin - end - end -end - --- Include a file. -map_coreop[".include_1"] = function(params) - if not params then return "filename" end - local name = params[1] - -- Save state. Ugly, I know. but upvalues are fast. - local gf, gl, gcl, gi = g_fname, g_lineno, g_curline, g_indent - -- Read the included file. - local fatal = readfile(pathopen(g_opt.include, name) or - wfatal("include file `"..name.."' not found")) - -- Restore state. - g_synclineno = -1 - g_fname, g_lineno, g_curline, g_indent = gf, gl, gcl, gi - if fatal then wfatal("in include file") end -end - --- Make .include and conditionals initially available, too. -map_op[".include_1"] = map_coreop[".include_1"] -map_op[".if_1"] = map_coreop[".if_1"] -map_op[".elif_1"] = map_coreop[".elif_1"] -map_op[".else_0"] = map_coreop[".else_0"] -map_op[".endif_0"] = map_coreop[".endif_0"] - ------------------------------------------------------------------------------- - --- Support variables for macros. -local mac_capture, mac_lineno, mac_name -local mac_active = {} -local mac_list = {} - --- Pseudo-opcode to define a macro. -map_coreop[".macro_*"] = function(mparams) - if not mparams then return "name [, params...]" end - -- Split off and validate macro name. - local name = remove(mparams, 1) - if not name then werror("missing macro name") end - if not (match(name, "^[%a_][%w_%.]*$") or match(name, "^%.[%w_%.]*$")) then - wfatal("bad macro name `"..name.."'") - end - -- Validate macro parameter names. - local mdup = {} - for _,mp in ipairs(mparams) do - if not match(mp, "^[%a_][%w_]*$") then - wfatal("bad macro parameter name `"..mp.."'") - end - if mdup[mp] then wfatal("duplicate macro parameter name `"..mp.."'") end - mdup[mp] = true - end - -- Check for duplicate or recursive macro definitions. - local opname = name.."_"..#mparams - if map_op[opname] or map_op[name.."_*"] then - wfatal("duplicate macro `"..name.."' ("..#mparams.." parameters)") - end - if mac_capture then wfatal("recursive macro definition") end - - -- Enable statement capture. - local lines = {} - mac_lineno = g_lineno - mac_name = name - mac_capture = function(stmt) -- Statement capture function. - -- Stop macro definition with .endmacro pseudo-opcode. - if not match(stmt, "^%s*.endmacro%s*$") then - lines[#lines+1] = stmt - return - end - mac_capture = nil - mac_lineno = nil - mac_name = nil - mac_list[#mac_list+1] = opname - -- Add macro-op definition. - map_op[opname] = function(params) - if not params then return mparams, lines end - -- Protect against recursive macro invocation. - if mac_active[opname] then wfatal("recursive macro invocation") end - mac_active[opname] = true - -- Setup substitution map. - local subst = {} - for i,mp in ipairs(mparams) do subst[mp] = params[i] end - local mcom - if g_opt.maccomment and g_opt.comment then - mcom = " MACRO "..name.." ("..#mparams..")" - wcomment("{"..mcom) - end - -- Loop through all captured statements - for _,stmt in ipairs(lines) do - -- Substitute macro parameters. - local st = gsub(stmt, "[%w_]+", subst) - st = definesubst(st) - st = gsub(st, "%s*%.%.%s*", "") -- Token paste a..b. - if mcom and sub(st, 1, 1) ~= "|" then wcomment(st) end - -- Emit statement. Use a protected call for better diagnostics. - local ok, err = pcall(dostmt, st) - if not ok then - -- Add the captured statement to the error. - wprinterr(err, "\n", g_indent, "| ", stmt, - "\t[MACRO ", name, " (", #mparams, ")]\n") - end - end - if mcom then wcomment("}"..mcom) end - mac_active[opname] = nil - end - end -end - --- An .endmacro pseudo-opcode outside of a macro definition is an error. -map_coreop[".endmacro_0"] = function(params) - wfatal(".endmacro without .macro") -end - --- Dump all macros and their contents (with -PP only). -local function dumpmacros(out, lvl) - sort(mac_list) - out:write("Macros:\n") - for _,opname in ipairs(mac_list) do - local name = sub(opname, 1, -3) - local params, lines = map_op[opname]() - out:write(format(" %-20s %s\n", name, concat(params, ", "))) - if lvl > 1 then - for _,line in ipairs(lines) do - out:write(" |", line, "\n") - end - out:write("\n") - end - end - out:write("\n") -end - --- Check for unfinished macro definitions. -local function checkmacros() - if mac_capture then - wprinterr(g_fname, ":", mac_lineno, - ": error: unfinished .macro `", mac_name ,"'\n") - end -end - ------------------------------------------------------------------------------- - --- Support variables for captures. -local cap_lineno, cap_name -local cap_buffers = {} -local cap_used = {} - --- Start a capture. -map_coreop[".capture_1"] = function(params) - if not params then return "name" end - wflush() - local name = params[1] - if not match(name, "^[%a_][%w_]*$") then - wfatal("bad capture name `"..name.."'") - end - if cap_name then - wfatal("already capturing to `"..cap_name.."' since line "..cap_lineno) - end - cap_name = name - cap_lineno = g_lineno - -- Create or continue a capture buffer and start the output line capture. - local buf = cap_buffers[name] - if not buf then buf = {}; cap_buffers[name] = buf end - g_capbuffer = buf - g_synclineno = 0 -end - --- Stop a capture. -map_coreop[".endcapture_0"] = function(params) - wflush() - if not cap_name then wfatal(".endcapture without a valid .capture") end - cap_name = nil - cap_lineno = nil - g_capbuffer = nil - g_synclineno = 0 -end - --- Dump a capture buffer. -map_coreop[".dumpcapture_1"] = function(params) - if not params then return "name" end - wflush() - local name = params[1] - if not match(name, "^[%a_][%w_]*$") then - wfatal("bad capture name `"..name.."'") - end - cap_used[name] = true - wline(function(out) - local buf = cap_buffers[name] - if buf then wdumplines(out, buf) end - end) - g_synclineno = 0 -end - --- Dump all captures and their buffers (with -PP only). -local function dumpcaptures(out, lvl) - out:write("Captures:\n") - for name,buf in pairs(cap_buffers) do - out:write(format(" %-20s %4s)\n", name, "("..#buf)) - if lvl > 1 then - local bar = rep("=", 76) - out:write(" ", bar, "\n") - for _,line in ipairs(buf) do - out:write(" ", line, "\n") - end - out:write(" ", bar, "\n\n") - end - end - out:write("\n") -end - --- Check for unfinished or unused captures. -local function checkcaptures() - if cap_name then - wprinterr(g_fname, ":", cap_lineno, - ": error: unfinished .capture `", cap_name,"'\n") - return - end - for name in pairs(cap_buffers) do - if not cap_used[name] then - wprinterr(g_fname, ":*: error: missing .dumpcapture ", name ,"\n") - end - end -end - ------------------------------------------------------------------------------- - --- Sections names. -local map_sections = {} - --- Pseudo-opcode to define code sections. --- TODO: Data sections, BSS sections. Needs extra C code and API. -map_coreop[".section_*"] = function(params) - if not params then return "name..." end - if #map_sections > 0 then werror("duplicate section definition") end - wflush() - for sn,name in ipairs(params) do - local opname = "."..name.."_0" - if not match(name, "^[%a][%w_]*$") or - map_op[opname] or map_op["."..name.."_*"] then - werror("bad section name `"..name.."'") - end - map_sections[#map_sections+1] = name - wline(format("#define DASM_SECTION_%s\t%d", upper(name), sn-1)) - map_op[opname] = function(params) g_arch.section(sn-1) end - end - wline(format("#define DASM_MAXSECTION\t\t%d", #map_sections)) -end - --- Dump all sections. -local function dumpsections(out, lvl) - out:write("Sections:\n") - for _,name in ipairs(map_sections) do - out:write(format(" %s\n", name)) - end - out:write("\n") -end - ------------------------------------------------------------------------------- - --- Load architecture-specific module. -local function loadarch(arch) - if not match(arch, "^[%w_]+$") then return "bad arch name" end - local ok, m_arch = pcall(require, "dasm_"..arch) - if not ok then return "cannot load module: "..m_arch end - g_arch = m_arch - wflush = m_arch.passcb(wline, werror, wfatal, wwarn) - m_arch.setup(arch, g_opt) - map_op, map_def = m_arch.mergemaps(map_coreop, map_def) -end - --- Dump architecture description. -function opt_map.dumparch(args) - local name = optparam(args) - if not g_arch then - local err = loadarch(name) - if err then opterror(err) end - end - - local t = {} - for name in pairs(map_coreop) do t[#t+1] = name end - for name in pairs(map_op) do t[#t+1] = name end - sort(t) - - local out = stdout - local _arch = g_arch._info - out:write(format("%s version %s, released %s, %s\n", - _info.name, _info.version, _info.release, _info.url)) - g_arch.dumparch(out) - - local pseudo = true - out:write("Pseudo-Opcodes:\n") - for _,sname in ipairs(t) do - local name, nparam = match(sname, "^(.+)_([0-9%*])$") - if name then - if pseudo and sub(name, 1, 1) ~= "." then - out:write("\nOpcodes:\n") - pseudo = false - end - local f = map_op[sname] - local s - if nparam ~= "*" then nparam = nparam + 0 end - if nparam == 0 then - s = "" - elseif type(f) == "string" then - s = map_op[".template__"](nil, f, nparam) - else - s = f(nil, nparam) - end - if type(s) == "table" then - for _,s2 in ipairs(s) do - out:write(format(" %-12s %s\n", name, s2)) - end - else - out:write(format(" %-12s %s\n", name, s)) - end - end - end - out:write("\n") - exit(0) -end - --- Pseudo-opcode to set the architecture. --- Only initially available (map_op is replaced when called). -map_op[".arch_1"] = function(params) - if not params then return "name" end - local err = loadarch(params[1]) - if err then wfatal(err) end -end - --- Dummy .arch pseudo-opcode to improve the error report. -map_coreop[".arch_1"] = function(params) - if not params then return "name" end - wfatal("duplicate .arch statement") -end - ------------------------------------------------------------------------------- - --- Dummy pseudo-opcode. Don't confuse '.nop' with 'nop'. -map_coreop[".nop_*"] = function(params) - if not params then return "[ignored...]" end -end - --- Pseudo-opcodes to raise errors. -map_coreop[".error_1"] = function(params) - if not params then return "message" end - werror(params[1]) -end - -map_coreop[".fatal_1"] = function(params) - if not params then return "message" end - wfatal(params[1]) -end - --- Dump all user defined elements. -local function dumpdef(out) - local lvl = g_opt.dumpdef - if lvl == 0 then return end - dumpsections(out, lvl) - dumpdefines(out, lvl) - if g_arch then g_arch.dumpdef(out, lvl) end - dumpmacros(out, lvl) - dumpcaptures(out, lvl) -end - ------------------------------------------------------------------------------- - --- Helper for splitstmt. -local splitlvl - -local function splitstmt_one(c) - if c == "(" then - splitlvl = ")"..splitlvl - elseif c == "[" then - splitlvl = "]"..splitlvl - elseif c == "{" then - splitlvl = "}"..splitlvl - elseif c == ")" or c == "]" or c == "}" then - if sub(splitlvl, 1, 1) ~= c then werror("unbalanced (), [] or {}") end - splitlvl = sub(splitlvl, 2) - elseif splitlvl == "" then - return " \0 " - end - return c -end - --- Split statement into (pseudo-)opcode and params. -local function splitstmt(stmt) - -- Convert label with trailing-colon into .label statement. - local label = match(stmt, "^%s*(.+):%s*$") - if label then return ".label", {label} end - - -- Split at commas and equal signs, but obey parentheses and brackets. - splitlvl = "" - stmt = gsub(stmt, "[,%(%)%[%]{}]", splitstmt_one) - if splitlvl ~= "" then werror("unbalanced () or []") end - - -- Split off opcode. - local op, other = match(stmt, "^%s*([^%s%z]+)%s*(.*)$") - if not op then werror("bad statement syntax") end - - -- Split parameters. - local params = {} - for p in gmatch(other, "%s*(%Z+)%z?") do - params[#params+1] = gsub(p, "%s+$", "") - end - if #params > 16 then werror("too many parameters") end - - params.op = op - return op, params -end - --- Process a single statement. -dostmt = function(stmt) - -- Ignore empty statements. - if match(stmt, "^%s*$") then return end - - -- Capture macro defs before substitution. - if mac_capture then return mac_capture(stmt) end - stmt = definesubst(stmt) - - -- Emit C code without parsing the line. - if sub(stmt, 1, 1) == "|" then - local tail = sub(stmt, 2) - wflush() - if sub(tail, 1, 2) == "//" then wcomment(tail) else wline(tail, true) end - return - end - - -- Split into (pseudo-)opcode and params. - local op, params = splitstmt(stmt) - - -- Get opcode handler (matching # of parameters or generic handler). - local f = map_op[op.."_"..#params] or map_op[op.."_*"] - if not f then - if not g_arch then wfatal("first statement must be .arch") end - -- Improve error report. - for i=0,9 do - if map_op[op.."_"..i] then - werror("wrong number of parameters for `"..op.."'") - end - end - werror("unknown statement `"..op.."'") - end - - -- Call opcode handler or special handler for template strings. - if type(f) == "string" then - map_op[".template__"](params, f) - else - f(params) - end -end - --- Process a single line. -local function doline(line) - if g_opt.flushline then wflush() end - - -- Assembler line? - local indent, aline = match(line, "^(%s*)%|(.*)$") - if not aline then - -- No, plain C code line, need to flush first. - wflush() - wsync() - wline(line, false) - return - end - - g_indent = indent -- Remember current line indentation. - - -- Emit C code (even from macros). Avoids echo and line parsing. - if sub(aline, 1, 1) == "|" then - if not mac_capture then - wsync() - elseif g_opt.comment then - wsync() - wcomment(aline) - end - dostmt(aline) - return - end - - -- Echo assembler line as a comment. - if g_opt.comment then - wsync() - wcomment(aline) - end - - -- Strip assembler comments. - aline = gsub(aline, "//.*$", "") - - -- Split line into statements at semicolons. - if match(aline, ";") then - for stmt in gmatch(aline, "[^;]+") do dostmt(stmt) end - else - dostmt(aline) - end -end - ------------------------------------------------------------------------------- - --- Write DynASM header. -local function dasmhead(out) - out:write(format([[ -/* -** This file has been pre-processed with DynASM. -** %s -** DynASM version %s, DynASM %s version %s -** DO NOT EDIT! The original file is in "%s". -*/ - -#if DASM_VERSION != %d -#error "Version mismatch between DynASM and included encoding engine" -#endif - -]], _info.url, - _info.version, g_arch._info.arch, g_arch._info.version, - g_fname, _info.vernum)) -end - --- Read input file. -readfile = function(fin) - g_indent = "" - g_lineno = 0 - g_synclineno = -1 - - -- Process all lines. - for line in fin:lines() do - g_lineno = g_lineno + 1 - g_curline = line - local ok, err = pcall(doline, line) - if not ok and wprinterr(err, "\n") then return true end - end - wflush() - - -- Close input file. - assert(fin == stdin or fin:close()) -end - --- Write output file. -local function writefile(outfile) - local fout - - -- Open output file. - if outfile == nil or outfile == "-" then - fout = stdout - else - fout = assert(io.open(outfile, "w")) - end - - -- Write all buffered lines - wdumplines(fout, g_wbuffer) - - -- Close output file. - assert(fout == stdout or fout:close()) - - -- Optionally dump definitions. - dumpdef(fout == stdout and stderr or stdout) -end - --- Translate an input file to an output file. -local function translate(infile, outfile) - g_wbuffer = {} - g_indent = "" - g_lineno = 0 - g_synclineno = -1 - - -- Put header. - wline(dasmhead) - - -- Read input file. - local fin - if infile == "-" then - g_fname = "(stdin)" - fin = stdin - else - g_fname = infile - fin = assert(io.open(infile, "r")) - end - readfile(fin) - - -- Check for errors. - if not g_arch then - wprinterr(g_fname, ":*: error: missing .arch directive\n") - end - checkconds() - checkmacros() - checkcaptures() - - if g_errcount ~= 0 then - stderr:write(g_fname, ":*: info: ", g_errcount, " error", - (type(g_errcount) == "number" and g_errcount > 1) and "s" or "", - " in input file -- no output file generated.\n") - dumpdef(stderr) - exit(1) - end - - -- Write output file. - writefile(outfile) -end - ------------------------------------------------------------------------------- - --- Print help text. -function opt_map.help() - stdout:write("DynASM -- ", _info.description, ".\n") - stdout:write("DynASM ", _info.version, " ", _info.release, " ", _info.url, "\n") - stdout:write[[ - -Usage: dynasm [OPTION]... INFILE.dasc|- - - -h, --help Display this help text. - -V, --version Display version and copyright information. - - -o, --outfile FILE Output file name (default is stdout). - -I, --include DIR Add directory to the include search path. - - -c, --ccomment Use /* */ comments for assembler lines. - -C, --cppcomment Use // comments for assembler lines (default). - -N, --nocomment Suppress assembler lines in output. - -M, --maccomment Show macro expansions as comments (default off). - - -L, --nolineno Suppress CPP line number information in output. - -F, --flushline Flush action list for every line. - - -D NAME[=SUBST] Define a substitution. - -U NAME Undefine a substitution. - - -P, --dumpdef Dump defines, macros, etc. Repeat for more output. - -A, --dumparch ARCH Load architecture ARCH and dump description. -]] - exit(0) -end - --- Print version information. -function opt_map.version() - stdout:write(format("%s version %s, released %s\n%s\n\n%s", - _info.name, _info.version, _info.release, _info.url, _info.copyright)) - exit(0) -end - --- Misc. options. -function opt_map.outfile(args) g_opt.outfile = optparam(args) end -function opt_map.include(args) insert(g_opt.include, 1, optparam(args)) end -function opt_map.ccomment() g_opt.comment = "/*|"; g_opt.endcomment = " */" end -function opt_map.cppcomment() g_opt.comment = "//|"; g_opt.endcomment = "" end -function opt_map.nocomment() g_opt.comment = false end -function opt_map.maccomment() g_opt.maccomment = true end -function opt_map.nolineno() g_opt.cpp = false end -function opt_map.flushline() g_opt.flushline = true end -function opt_map.dumpdef() g_opt.dumpdef = g_opt.dumpdef + 1 end - ------------------------------------------------------------------------------- - --- Short aliases for long options. -local opt_alias = { - h = "help", ["?"] = "help", V = "version", - o = "outfile", I = "include", - c = "ccomment", C = "cppcomment", N = "nocomment", M = "maccomment", - L = "nolineno", F = "flushline", - P = "dumpdef", A = "dumparch", -} - --- Parse single option. -local function parseopt(opt, args) - opt_current = #opt == 1 and "-"..opt or "--"..opt - local f = opt_map[opt] or opt_map[opt_alias[opt]] - if not f then - opterror("unrecognized option `", opt_current, "'. Try `--help'.\n") - end - f(args) -end - --- Parse arguments. -local function parseargs(args) - -- Default options. - g_opt.comment = "//|" - g_opt.endcomment = "" - g_opt.cpp = true - g_opt.dumpdef = 0 - g_opt.include = { "" } - - -- Process all option arguments. - args.argn = 1 - repeat - local a = args[args.argn] - if not a then break end - local lopt, opt = match(a, "^%-(%-?)(.+)") - if not opt then break end - args.argn = args.argn + 1 - if lopt == "" then - -- Loop through short options. - for o in gmatch(opt, ".") do parseopt(o, args) end - else - -- Long option. - parseopt(opt, args) - end - until false - - -- Check for proper number of arguments. - local nargs = #args - args.argn + 1 - if nargs ~= 1 then - if nargs == 0 then - if g_opt.dumpdef > 0 then return dumpdef(stdout) end - end - opt_map.help() - end - - -- Translate a single input file to a single output file - -- TODO: Handle multiple files? - translate(args[args.argn], g_opt.outfile) -end - ------------------------------------------------------------------------------- - --- Add the directory dynasm.lua resides in to the Lua module search path. -local arg = arg -if arg and arg[0] then - local prefix = match(arg[0], "^(.*[/\\])") - if prefix then package.path = prefix.."?.lua;"..package.path end -end - --- Start DynASM. -parseargs{...} - ------------------------------------------------------------------------------- - diff --git a/third_party/protobuf b/third_party/protobuf deleted file mode 160000 index 5aeee3d..0000000 --- a/third_party/protobuf +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 5aeee3dc910d37f37b75f5a6d1486fe75cb09284 diff --git a/upb/pb/compile_decoder.c b/upb/pb/compile_decoder.c index ca497ed..c5d8d9b 100644 --- a/upb/pb/compile_decoder.c +++ b/upb/pb/compile_decoder.c @@ -4,11 +4,6 @@ ** Code to compile a upb::Handlers into bytecode for decoding a protobuf ** according to that specific schema and destination handlers. ** -** Compiling to bytecode is always the first step. If we are using the -** interpreted decoder we leave it as bytecode and interpret that. If we are -** using a JIT decoder we use a code generator to turn the bytecode into native -** code, LLVM IR, etc. -** ** Bytecode definition is in decoder.int.h. */ @@ -37,7 +32,6 @@ static upb_pbdecodermethod *newmethod(const upb_handlers *dest_handlers, ret->group = group; ret->dest_handlers_ = dest_handlers; - ret->is_native_ = false; /* If we JIT, it will update this later. */ upb_inttable_init(&ret->dispatch, UPB_CTYPE_UINT64); return ret; @@ -69,9 +63,6 @@ static void freegroup(mgroup *g) { } upb_inttable_uninit(&g->methods); -#ifdef UPB_USE_JIT_X64 - upb_pbdecoder_freejit(g); -#endif upb_gfree(g->bytecode); upb_gfree(g); } @@ -313,7 +304,7 @@ static void putop(compiler *c, int op, ...) { va_end(ap); } -#if defined(UPB_USE_JIT_X64) || defined(UPB_DUMP_BYTECODE) +#if defined(UPB_DUMP_BYTECODE) const char *upb_pbdecoder_getopname(unsigned int op) { #define QUOTE(x) #x @@ -827,31 +818,6 @@ static void set_bytecode_handlers(mgroup *g) { } -/* JIT setup. *****************************************************************/ - -#ifdef UPB_USE_JIT_X64 - -static void sethandlers(mgroup *g, bool allowjit) { - g->jit_code = NULL; - if (allowjit) { - /* Compile byte-code into machine code, create handlers. */ - upb_pbdecoder_jit(g); - } else { - set_bytecode_handlers(g); - } -} - -#else /* UPB_USE_JIT_X64 */ - -static void sethandlers(mgroup *g, bool allowjit) { - /* No JIT compiled in; use bytecode handlers unconditionally. */ - UPB_UNUSED(allowjit); - set_bytecode_handlers(g); -} - -#endif /* UPB_USE_JIT_X64 */ - - /* TODO(haberman): allow this to be constructed for an arbitrary set of dest * handlers and other mgroups (but verify we have a transitive closure). */ const mgroup *mgroup_new(const upb_handlers *dest, bool allowjit, bool lazy) { @@ -891,7 +857,7 @@ const mgroup *mgroup_new(const upb_handlers *dest, bool allowjit, bool lazy) { } #endif - sethandlers(g, allowjit); + set_bytecode_handlers(g); return g; } diff --git a/upb/pb/compile_decoder_x64.c b/upb/pb/compile_decoder_x64.c deleted file mode 100644 index 7c716e8..0000000 --- a/upb/pb/compile_decoder_x64.c +++ /dev/null @@ -1,511 +0,0 @@ -/* -** Driver code for the x64 JIT compiler. -*/ - -/* Needed to ensure we get defines like MAP_ANON. */ -#define _GNU_SOURCE - -#include -#include -#include -#include -#include "upb/msg.h" -#include "upb/pb/decoder.h" -#include "upb/pb/decoder.int.h" -#include "upb/pb/varint.int.h" - -/* To debug the JIT: - * - * 1. Uncomment: - * #define UPB_JIT_LOAD_SO - * - * Note: this mode requires that we can shell out to gcc. - * - * 2. Run the test locally. This will load the JIT code by building a - * .so (/tmp/upb-jit-code.so) and using dlopen, so more of the tooling will - * work properly (like GDB). - * - * IF YOU ALSO WANT AUTOMATIC JIT DEBUG OUTPUT: - * - * 3. Run: upb/pb/make-gdb-script.rb > script.gdb. This reads - * /tmp/upb-jit-code.so as input and generates a GDB script that is specific - * to this jit code. - * - * 4. Run: gdb --command=script.gdb --args path/to/test - * This will drop you to a GDB prompt which you can now use normally. - * But when you run the test it will print a message to stdout every time - * the JIT executes assembly for a particular bytecode. Sample output: - * - * X.enterjit bytes=18 - * buf_ofs=1 data_rem=17 delim_rem=-2 X.0x6.OP_PARSE_DOUBLE - * buf_ofs=9 data_rem=9 delim_rem=-10 X.0x7.OP_CHECKDELIM - * buf_ofs=9 data_rem=9 delim_rem=-10 X.0x8.OP_TAG1 - * X.0x3.dispatch.DecoderTest - * X.parse_unknown - * X.0x3.dispatch.DecoderTest - * X.decode_unknown_tag_fallback - * X.exitjit - * - * This output should roughly correspond to the output that the bytecode - * interpreter emits when compiled with UPB_DUMP_BYTECODE (modulo some - * extra JIT-specific output). */ - -/* These defines are necessary for DynASM codegen. - * See dynasm/dasm_proto.h for more info. */ -#define Dst_DECL jitcompiler *jc -#define Dst_REF (jc->dynasm) -#define Dst (jc) - -/* In debug mode, make DynASM do internal checks (must be defined before any - * dasm header is included. */ -#ifndef NDEBUG -#define DASM_CHECKS -#endif - -#ifndef MAP_ANONYMOUS -#define MAP_ANONYMOUS MAP_ANON -#endif - -typedef struct { - mgroup *group; - uint32_t *pc; - - /* This pointer is allocated by dasm_init() and freed by dasm_free(). */ - struct dasm_State *dynasm; - - /* Maps some key (an arbitrary void*) to a pclabel. - * - * The pclabel represents a location in the generated code -- DynASM exposes - * a pclabel -> (machine code offset) lookup function. - * - * The key can be anything. There are two main kinds of keys: - * - bytecode location -- the void* points to the bytecode instruction - * itself. We can then use this to generate jumps to this instruction. - * - other object (like dispatch table). We use these to represent parts - * of the generated code that do not exactly correspond to a bytecode - * instruction. */ - upb_inttable jmptargets; - -#ifndef NDEBUG - /* Like jmptargets, but members are present in the table when they have had - * define_jmptarget() (as opposed to jmptarget) called. Used to verify that - * define_jmptarget() is called exactly once for every target. - * The value is ignored. */ - upb_inttable jmpdefined; - - /* For checking that two asmlabels aren't defined for the same byte. */ - int lastlabelofs; -#endif - -#ifdef UPB_JIT_LOAD_SO - /* For marking labels that should go into the generated code. - * Maps pclabel -> char* label (string is owned by the table). */ - upb_inttable asmlabels; -#endif - - /* The total number of pclabels currently defined. - * Note that this contains both jmptargets and asmlabels, which both use - * pclabels but for different purposes. */ - uint32_t pclabel_count; - - /* Used by DynASM to store globals. */ - void **globals; -} jitcompiler; - -/* Functions called by codegen. */ -static int jmptarget(jitcompiler *jc, const void *key); -static int define_jmptarget(jitcompiler *jc, const void *key); -static void asmlabel(jitcompiler *jc, const char *fmt, ...); -static int pcofs(jitcompiler* jc); -static int alloc_pclabel(jitcompiler *jc); - -#ifdef UPB_JIT_LOAD_SO -static char *upb_vasprintf(const char *fmt, va_list ap); -static char *upb_asprintf(const char *fmt, ...); -#endif - -#include "third_party/dynasm/dasm_proto.h" -#include "third_party/dynasm/dasm_x86.h" -#include "upb/pb/compile_decoder_x64.h" - -static jitcompiler *newjitcompiler(mgroup *group) { - jitcompiler *jc = malloc(sizeof(jitcompiler)); - jc->group = group; - jc->pclabel_count = 0; - upb_inttable_init(&jc->jmptargets, UPB_CTYPE_UINT32); -#ifndef NDEBUG - jc->lastlabelofs = -1; - upb_inttable_init(&jc->jmpdefined, UPB_CTYPE_BOOL); -#endif -#ifdef UPB_JIT_LOAD_SO - upb_inttable_init(&jc->asmlabels, UPB_CTYPE_PTR); -#endif - jc->globals = malloc(UPB_JIT_GLOBAL__MAX * sizeof(*jc->globals)); - - dasm_init(jc, 1); - dasm_setupglobal(jc, jc->globals, UPB_JIT_GLOBAL__MAX); - dasm_setup(jc, upb_jit_actionlist); - - return jc; -} - -static void freejitcompiler(jitcompiler *jc) { -#ifdef UPB_JIT_LOAD_SO - upb_inttable_iter i; - upb_inttable_begin(&i, &jc->asmlabels); - for (; !upb_inttable_done(&i); upb_inttable_next(&i)) { - free(upb_value_getptr(upb_inttable_iter_value(&i))); - } - upb_inttable_uninit(&jc->asmlabels); -#endif -#ifndef NDEBUG - upb_inttable_uninit(&jc->jmpdefined); -#endif - upb_inttable_uninit(&jc->jmptargets); - dasm_free(jc); - free(jc->globals); - free(jc); -} - -#ifdef UPB_JIT_LOAD_SO - -/* Like sprintf except allocates the string, which is returned and owned by the - * caller. - * - * Like the GNU extension asprintf(), except we abort on error (since this is - * only for debugging). */ -static char *upb_vasprintf(const char *fmt, va_list args) { - /* Run once to get the length of the string. */ - va_list args_copy; - va_copy(args_copy, args); - int len = _upb_vsnprintf(NULL, 0, fmt, args_copy); - va_end(args_copy); - - char *ret = malloc(len + 1); /* + 1 for NULL terminator. */ - if (!ret) abort(); - int written = _upb_vsnprintf(ret, len + 1, fmt, args); - UPB_ASSERT(written == len); - - return ret; -} - -static char *upb_asprintf(const char *fmt, ...) { - va_list args; - va_start(args, fmt); - char *ret = upb_vasprintf(fmt, args); - va_end(args); - return ret; -} - -#endif - -static int alloc_pclabel(jitcompiler *jc) { - int newpc = jc->pclabel_count++; - dasm_growpc(jc, jc->pclabel_count); - return newpc; -} - -static bool try_getjmptarget(jitcompiler *jc, const void *key, int *pclabel) { - upb_value v; - if (upb_inttable_lookupptr(&jc->jmptargets, key, &v)) { - *pclabel = upb_value_getuint32(v); - return true; - } else { - return false; - } -} - -/* Gets the pclabel for this bytecode location's jmptarget. Requires that the - * jmptarget() has been previously defined. */ -static int getjmptarget(jitcompiler *jc, const void *key) { - int pclabel = 0; - bool ok; - - UPB_ASSERT_DEBUGVAR(upb_inttable_lookupptr(&jc->jmpdefined, key, NULL)); - ok = try_getjmptarget(jc, key, &pclabel); - UPB_ASSERT(ok); - return pclabel; -} - -/* Returns a pclabel that serves as a jmp target for the given bytecode pointer. - * This should only be called for code that is jumping to the target; code - * defining the target should use define_jmptarget(). - * - * Creates/allocates a pclabel for this target if one does not exist already. */ -static int jmptarget(jitcompiler *jc, const void *key) { - /* Optimizer sometimes can't figure out that initializing this is unnecessary. - */ - int pclabel = 0; - if (!try_getjmptarget(jc, key, &pclabel)) { - pclabel = alloc_pclabel(jc); - upb_inttable_insertptr(&jc->jmptargets, key, upb_value_uint32(pclabel)); - } - return pclabel; -} - -/* Defines a pclabel associated with the given bytecode location. - * Must be called exactly once by the code that is generating the code for this - * bytecode. - * - * Must be called exactly once before bytecode generation is complete (this is a - * sanity check to make sure the label is defined exactly once). */ -static int define_jmptarget(jitcompiler *jc, const void *key) { -#ifndef NDEBUG - upb_inttable_insertptr(&jc->jmpdefined, key, upb_value_bool(true)); -#endif - return jmptarget(jc, key); -} - -/* Returns a bytecode pc offset relative to the beginning of the group's - * code. */ -static int pcofs(jitcompiler *jc) { - return jc->pc - jc->group->bytecode; -} - -/* Returns a machine code offset corresponding to the given key. - * Requires that this key was defined with define_jmptarget. */ -static int machine_code_ofs(jitcompiler *jc, const void *key) { - int pclabel = getjmptarget(jc, key); - /* Despite its name, this function takes a pclabel and returns the - * corresponding machine code offset. */ - return dasm_getpclabel(jc, pclabel); -} - -/* Returns a machine code offset corresponding to the given method-relative - * bytecode offset. Note that the bytecode offset is relative to the given - * method, but the returned machine code offset is relative to the beginning of - * *all* the machine code. */ -static int machine_code_ofs2(jitcompiler *jc, const upb_pbdecodermethod *method, - int pcofs) { - void *bc_target = jc->group->bytecode + method->code_base.ofs + pcofs; - return machine_code_ofs(jc, bc_target); -} - -/* Given a pcofs relative to this method's base, returns a machine code offset - * relative to jmptarget(dispatch->array) (which is used in jitdispatch as the - * machine code base for dispatch table lookups). */ -uint32_t dispatchofs(jitcompiler *jc, const upb_pbdecodermethod *method, - int pcofs) { - int mc_base = machine_code_ofs(jc, method->dispatch.array); - int mc_target = machine_code_ofs2(jc, method, pcofs); - int ret; - - UPB_ASSERT(mc_base > 0); - UPB_ASSERT(mc_target > 0); - ret = mc_target - mc_base; - UPB_ASSERT(ret > 0); - return ret; -} - -/* Rewrites the dispatch tables into machine code offsets. */ -static void patchdispatch(jitcompiler *jc) { - upb_inttable_iter i; - upb_inttable_begin(&i, &jc->group->methods); - for (; !upb_inttable_done(&i); upb_inttable_next(&i)) { - upb_pbdecodermethod *method = upb_value_getptr(upb_inttable_iter_value(&i)); - upb_inttable *dispatch = &method->dispatch; - upb_inttable_iter i2; - - method->is_native_ = true; - - /* Remove DISPATCH_ENDMSG -- only the bytecode interpreter needs it. - * And leaving it around will cause us to find field 0 improperly. */ - upb_inttable_remove(dispatch, DISPATCH_ENDMSG, NULL); - - upb_inttable_begin(&i2, dispatch); - for (; !upb_inttable_done(&i2); upb_inttable_next(&i2)) { - uintptr_t key = upb_inttable_iter_key(&i2); - uint64_t val = upb_value_getuint64(upb_inttable_iter_value(&i2)); - uint64_t newval; - bool ok; - if (key <= UPB_MAX_FIELDNUMBER) { - /* Primary slot. */ - uint64_t ofs; - uint8_t wt1; - uint8_t wt2; - upb_pbdecoder_unpackdispatch(val, &ofs, &wt1, &wt2); - - /* Update offset and repack. */ - ofs = dispatchofs(jc, method, ofs); - newval = upb_pbdecoder_packdispatch(ofs, wt1, wt2); - UPB_ASSERT((int64_t)newval > 0); - } else { - /* Secondary slot. Since we have 64 bits for the value, we use an - * absolute offset. */ - int mcofs = machine_code_ofs2(jc, method, val); - newval = (uint64_t)((char*)jc->group->jit_code + mcofs); - } - ok = upb_inttable_replace(dispatch, key, upb_value_uint64(newval)); - UPB_ASSERT(ok); - } - - /* Update entry point for this method to point at mc base instead of bc - * base. Set this only *after* we have patched the offsets - * (machine_code_ofs2() uses this). */ - method->code_base.ptr = (char*)jc->group->jit_code + machine_code_ofs(jc, method); - - { - upb_byteshandler *h = &method->input_handler_; - upb_byteshandler_setstartstr(h, upb_pbdecoder_startjit, NULL); - upb_byteshandler_setstring(h, jc->group->jit_code, method->code_base.ptr); - upb_byteshandler_setendstr(h, upb_pbdecoder_end, method); - } - } -} - -#ifdef UPB_JIT_LOAD_SO - -static void load_so(jitcompiler *jc) { - /* Dump to a .so file in /tmp and load that, so all the tooling works right - * (for example, debuggers and profilers will see symbol names for the JIT-ted - * code). This is the same goal of the GDB JIT code below, but the GDB JIT - * interface is only used/understood by GDB. Hopefully a standard will - * develop for registering JIT-ted code that all tools will recognize, - * rendering this obsolete. - * - * jc->asmlabels maps: - * pclabel -> char* label - * - * Use this to build mclabels, which maps: - * machine code offset -> char* label - * - * Then we can use mclabels to emit the labels as we iterate over the bytes we - * are outputting. */ - upb_inttable_iter i; - upb_inttable mclabels; - upb_inttable_init(&mclabels, UPB_CTYPE_PTR); - upb_inttable_begin(&i, &jc->asmlabels); - for (; !upb_inttable_done(&i); upb_inttable_next(&i)) { - upb_inttable_insert(&mclabels, - dasm_getpclabel(jc, upb_inttable_iter_key(&i)), - upb_inttable_iter_value(&i)); - } - - /* We write a .s file in text format, as input to the assembler. - * Then we run gcc to turn it into a .so file. - * - * The last "XXXXXX" will be replaced with something randomly generated by - * mkstmemp(). We don't add ".s" to this filename because it makes the string - * processing for mkstemp() and system() more complicated. */ - char s_filename[] = "/tmp/upb-jit-codeXXXXXX"; - int fd = mkstemp(s_filename); - FILE *f; - if (fd >= 0 && (f = fdopen(fd, "wb")) != NULL) { - uint8_t *jit_code = (uint8_t*)jc->group->jit_code; - size_t linelen = 0; - size_t i; - fputs(" .text\n\n", f); - for (i = 0; i < jc->group->jit_size; i++) { - upb_value v; - if (upb_inttable_lookup(&mclabels, i, &v)) { - const char *label = upb_value_getptr(v); - /* "X." makes our JIT syms recognizable as such, which we build into - * other tooling. */ - fprintf(f, "\n\nX.%s:\n", label); - fprintf(f, " .globl X.%s", label); - linelen = 1000; - } - if (linelen >= 77) { - linelen = fprintf(f, "\n .byte %u", jit_code[i]); - } else { - linelen += fprintf(f, ",%u", jit_code[i]); - } - } - fputs("\n", f); - fclose(f); - } else { - fprintf(stderr, "Error opening tmp file for JIT debug output.\n"); - abort(); - } - - /* This is exploitable if you have an adversary on your machine who can write - * to this tmp directory. But this is just for debugging so we don't worry - * too much about that. It shouldn't be prone to races against concurrent - * (non-adversarial) upb JIT's because we used mkstemp(). */ - char *cmd = upb_asprintf("gcc -shared -o %s.so -x assembler %s", s_filename, - s_filename); - if (system(cmd) != 0) { - fprintf(stderr, "Error compiling %s\n", s_filename); - abort(); - } - free(cmd); - - char *so_filename = upb_asprintf("%s.so", s_filename); - - /* Some convenience symlinks. - * This is racy, but just for convenience. */ - int ret; - unlink("/tmp/upb-jit-code.so"); - unlink("/tmp/upb-jit-code.s"); - ret = symlink(s_filename, "/tmp/upb-jit-code.s"); - ret = symlink(so_filename, "/tmp/upb-jit-code.so"); - UPB_UNUSED(ret); // We don't care if this fails. - - jc->group->dl = dlopen(so_filename, RTLD_LAZY); - free(so_filename); - if (!jc->group->dl) { - fprintf(stderr, "Couldn't dlopen(): %s\n", dlerror()); - abort(); - } - - munmap(jc->group->jit_code, jc->group->jit_size); - jc->group->jit_code = dlsym(jc->group->dl, "X.enterjit"); - if (!jc->group->jit_code) { - fprintf(stderr, "Couldn't find enterjit sym\n"); - abort(); - } - - upb_inttable_uninit(&mclabels); -} - -#endif - -void upb_pbdecoder_jit(mgroup *group) { - jitcompiler *jc; - char *jit_code; - int dasm_status; - - group->debug_info = NULL; - group->dl = NULL; - - UPB_ASSERT(group->bytecode); - jc = newjitcompiler(group); - emit_static_asm(jc); - jitbytecode(jc); - - dasm_status = dasm_link(jc, &jc->group->jit_size); - if (dasm_status != DASM_S_OK) { - fprintf(stderr, "DynASM error; returned status: 0x%08x\n", dasm_status); - abort(); - } - - jit_code = mmap(NULL, jc->group->jit_size, PROT_READ | PROT_WRITE, - MAP_ANONYMOUS | MAP_PRIVATE, 0, 0); - dasm_encode(jc, jit_code); - mprotect(jit_code, jc->group->jit_size, PROT_EXEC | PROT_READ); - jc->group->jit_code = (upb_string_handlerfunc *)jit_code; - -#ifdef UPB_JIT_LOAD_SO - load_so(jc); -#endif - - patchdispatch(jc); - - freejitcompiler(jc); - - /* Now the bytecode is no longer needed. */ - free(group->bytecode); - group->bytecode = NULL; -} - -void upb_pbdecoder_freejit(mgroup *group) { - if (!group->jit_code) return; - if (group->dl) { -#ifdef UPB_JIT_LOAD_SO - dlclose(group->dl); -#endif - } else { - munmap((void*)group->jit_code, group->jit_size); - } - free(group->debug_info); -} diff --git a/upb/pb/compile_decoder_x64.dasc b/upb/pb/compile_decoder_x64.dasc deleted file mode 100644 index 7dc1987..0000000 --- a/upb/pb/compile_decoder_x64.dasc +++ /dev/null @@ -1,1150 +0,0 @@ -|// -|// upb - a minimalist implementation of protocol buffers. -|// -|// Copyright (c) 2011-2013 Google Inc. See LICENSE for details. -|// Author: Josh Haberman -|// -|// JIT compiler for upb_pbdecoder on x86-64. Generates machine code from the -|// bytecode generated in compile_decoder.c. -| -|.arch x64 -|.actionlist upb_jit_actionlist -|.globals UPB_JIT_GLOBAL_ -|.globalnames upb_jit_globalnames -| -|// Calling conventions. Note -- this will need to be changed for -|// Windows, which uses a different calling convention! -|.define ARG1_64, rdi -|.define ARG2_8, r6b // DynASM's equivalent to "sil" -- low byte of esi. -|.define ARG2_32, esi -|.define ARG2_64, rsi -|.define ARG3_8, dl -|.define ARG3_32, edx -|.define ARG3_64, rdx -|.define ARG4_64, rcx -|.define ARG5_64, r8 -|.define XMMARG1, xmm0 -| -|// Register allocation / type map. -|// ALL of the code in this file uses these register allocations. -|// When we "call" within this file, we do not use regular calling -|// conventions, but of course when calling to user callbacks we must. -|.define PTR, rbx // DECODER->ptr (unsynced) -|.define DATAEND, r12 // DECODER->data_end (unsynced) -|.define CLOSURE, r13 // FRAME->closure (unsynced) -|.type FRAME, upb_pbdecoder_frame, r14 // DECODER->top (unsynced) -|.type DECODER, upb_pbdecoder, r15 // DECODER (immutable) -|.define DELIMEND, rbp -| -| // Spills unsynced registers back to memory. -|.macro commit_regs -| mov DECODER->top, FRAME -| mov DECODER->ptr, PTR -| mov DECODER->data_end, DATAEND -| // We don't guarantee that delim_end is NULL when out of range like the -| // interpreter does. -| mov DECODER->delim_end, DELIMEND -| sub DELIMEND, DECODER->buf -| add DELIMEND, DECODER->bufstart_ofs -| mov FRAME->end_ofs, DELIMEND -| mov FRAME->sink.closure, CLOSURE -|.endmacro -| -| // Loads unsynced registers from memory back into registers. -|.macro load_regs -| mov FRAME, DECODER->top -| mov PTR, DECODER->ptr -| mov DATAEND, DECODER->data_end -| mov CLOSURE, FRAME->sink.closure -| mov DELIMEND, FRAME->end_ofs -| sub DELIMEND, DECODER->bufstart_ofs -| add DELIMEND, DECODER->buf -|.endmacro -| -| // Calls an external C function at address "addr". -|.macro callp, addr -| mov64 rax, (uintptr_t)addr -| -| // Stack must be 16-byte aligned (x86-64 ABI requires this). -| // -| // OPT: possibly remove this by statically ensuring correct alignment. -| // -| // OPT: use "call rel32" where possible. -| push r12 -| mov r12, rsp -| and rsp, 0xfffffffffffffff0UL // Align stack. -| call rax -| mov rsp, r12 -| pop r12 -|.endmacro -| -|.macro ld64, val -|| { -|| uintptr_t v = (uintptr_t)val; -|| if (v > 0xffffffff) { -| mov64 ARG2_64, v -|| } else if (v) { -| mov ARG2_32, v -|| } else { -| xor ARG2_32, ARG2_32 -|| } -|| } -|.endmacro -| -|.macro load_handler_data, h, arg -| ld64 gethandlerdata(h, arg) -|.endmacro -| -|.macro chkeob, bytes, target -|| if (bytes == 1) { -| cmp PTR, DATAEND -| je target -|| } else { -| mov rcx, DATAEND -| sub rcx, PTR -| cmp rcx, bytes -| jb target -|| } -|.endmacro -| -|.macro chkneob, bytes, target -|| if (bytes == 1) { -| cmp PTR, DATAEND -| jne target -|| } else { -| mov rcx, DATAEND -| sub rcx, PTR -| cmp rcx, bytes -| jae target -|| } -|.endmacro - -|.macro sethas, reg, hasbit -|| if (hasbit >= 0) { -| or byte [reg + ((uint32_t)hasbit / 8)], (1 << ((uint32_t)hasbit % 8)) -|| } -|.endmacro -| -| // Decodes 32-bit varint into rdx, inlining 1 byte. -|.macro dv32 -| chkeob 1, >7 -| movzx edx, byte [PTR] -| test dl, dl -| jns >8 -|7: -| call ->decodev32_fallback -|8: -| add PTR, 1 -|.endmacro - -#define DECODE_EOF -3 - -static upb_func *gethandler(const upb_handlers *h, upb_selector_t sel) { - return h ? upb_handlers_gethandler(h, sel, NULL) : NULL; -} - -/* Defines an "assembly label" for the current code generation offset. - * This label exists *purely* for debugging purposes: it is emitted into - * the .so, and printed as part of JIT debugging output when UPB_JIT_LOAD_SO is - * defined. - * - * We would define this in the .c file except that it conditionally defines a - * pclabel. */ -static void asmlabel(jitcompiler *jc, const char *fmt, ...) { -#ifndef NDEBUG - int ofs = jc->dynasm->section->ofs; - UPB_ASSERT(ofs != jc->lastlabelofs); - jc->lastlabelofs = ofs; -#endif - -#ifndef UPB_JIT_LOAD_SO - UPB_UNUSED(jc); - UPB_UNUSED(fmt); -#else - va_list args; - va_start(args, fmt); - char *str = upb_vasprintf(fmt, args); - va_end(args); - - int pclabel = alloc_pclabel(jc); - /* Normally we would prefer to allocate this inline with the codegen, - * ie. - * |=>asmlabel(...) - * But since we do this conditionally, only when UPB_JIT_LOAD_SO is defined, - * we do it here instead. */ - |=>pclabel: - upb_inttable_insert(&jc->asmlabels, pclabel, upb_value_ptr(str)); -#endif -} - -/* Should only be called when the associated handler is known to exist. */ -static bool alwaysok(const upb_handlers *h, upb_selector_t sel) { - upb_handlerattr attr = UPB_HANDLERATTR_INIT; - bool ok = upb_handlers_getattr(h, sel, &attr); - - UPB_ASSERT(ok); - return attr.alwaysok; -} - -static const void *gethandlerdata(const upb_handlers *h, upb_selector_t sel) { - upb_handlerattr attr = UPB_HANDLERATTR_INIT; - bool ok = upb_handlers_getattr(h, sel, &attr); - - UPB_ASSERT(ok); - return attr.handler_data; -} - -/* Emit static assembly routines; code that does not vary based on the message - * schema. Since it's not input-dependent, we only need one single copy of it. - * For the moment we generate a single copy per generated handlers. Eventually - * we should generate this code at compile time and link it into the binary so - * we have one copy total. To do that we'll want to be sure that it is within - * 2GB of our JIT code, so that branches between the two are near (rel32). - * - * We'd put this assembly in a .s file directly, but DynASM's ability to - * calculate structure offsets automatically is too useful to pass up (it's way - * more convenient to write DECODER->sink than [rbx + 0x96], especially since - * the latter would have to be changed whenever the structure is updated). */ -static void emit_static_asm(jitcompiler *jc) { - | // Trampolines for entering/exiting the JIT. These are a bit tricky to - | // support full resuming; when we suspend we copy the JIT's portion of - | // the call stack into the upb_pbdecoder and restore it when we resume. - asmlabel(jc, "enterjit"); - |->enterjit: - |1: - | push rbp - | push r15 - | push r14 - | push r13 - | push r12 - | push rbx - | - | mov rbx, ARG2_64 // Preserve JIT method. - | - | mov DECODER, rdi - | callp upb_pbdecoder_resume // Same args as us; reuse regs. - | test eax, eax - | jns >1 - | mov DECODER->saved_rsp, rsp - | mov rax, rbx - | load_regs - | - | // Test whether we have a saved stack to resume. - | mov ARG3_64, DECODER->call_len - | test ARG3_64, ARG3_64 - | jnz >2 - | - | call rax - | - | mov rax, DECODER->size_param - | mov qword DECODER->call_len, 0 - |1: - | pop rbx - | pop r12 - | pop r13 - | pop r14 - | pop r15 - | pop rbp - | ret - | - |2: - | // Resume decoder. - | mov ARG2_64, DECODER->callstack - | sub rsp, ARG3_64 - | mov ARG1_64, rsp - | callp memcpy // Restore stack. - | ret // Return to resumed function (not ->enterjit caller). - | - | // Other code can call this to suspend the JIT. - | // To the calling code, it will appear that the function returns when - | // the JIT resumes, and more buffer space will be available. - | // Args: eax=the value that decode() should return. - asmlabel(jc, "exitjit"); - |->exitjit: - | // Save the stack into DECODER->callstack. - | mov ARG1_64, DECODER->callstack - | mov ARG2_64, rsp - | mov ARG3_64, DECODER->saved_rsp - | sub ARG3_64, rsp - | mov DECODER->call_len, ARG3_64 // Preserve len for next resume. - | mov ebx, eax // Preserve return value across memcpy. - | callp memcpy // Copy stack into decoder. - | mov eax, ebx // This will be our return value. - | - | // Must NOT do this before the memcpy(), otherwise memcpy() will - | // clobber the stack we are trying to save! - | mov rsp, DECODER->saved_rsp - | pop rbx - | pop r12 - | pop r13 - | pop r14 - | pop r15 - | pop rbp - | ret - | - | // Like suspend() in the C decoder, except that the function appears - | // (from the caller's perspective) not to return until the decoder is - | // resumed. - asmlabel(jc, "suspend"); - |->suspend: - | cmp DECODER->ptr, PTR - | je >1 - | mov DECODER->checkpoint, PTR - |1: - | commit_regs - | mov rdi, DECODER - | callp upb_pbdecoder_suspend - | jmp ->exitjit - | - asmlabel(jc, "pushlendelim"); - |->pushlendelim: - |1: - | mov FRAME->sink.closure, CLOSURE - | mov DECODER->checkpoint, PTR - | dv32 - | mov rcx, DELIMEND - | sub rcx, PTR - | sub rcx, rdx - | jb >4 // Len is greater than enclosing message. - | mov FRAME->end_ofs, rcx - | cmp FRAME, DECODER->limit - | je >3 // Stack overflow - | add FRAME, sizeof(upb_pbdecoder_frame) - | mov DELIMEND, PTR - | add DELIMEND, rdx - | mov dword FRAME->groupnum, 0 - | test rcx, rcx - | jz >2 - | mov DATAEND, DECODER->end - | cmp PTR, DELIMEND - | ja >2 - | cmp DELIMEND, DATAEND - | ja >2 - | mov DATAEND, DELIMEND // If DELIMEND >= PTR && DELIMEND < DATAEND - |2: - | ret - |3: - | // Stack overflow error. - | mov PTR, DECODER->checkpoint // Rollback to before the delim len. - | // Prepare seterr args. - | mov ARG1_64, DECODER - | ld64 kPbDecoderStackOverflow - | callp upb_pbdecoder_seterr - | call ->suspend - | jmp <1 - |4: - | // Overextended len. - | mov PTR, DECODER->checkpoint // Rollback to before the delim len. - | // Prepare seterr args. - | mov ARG1_64, DECODER - | ld64 kPbDecoderSubmessageTooLong - | callp upb_pbdecoder_seterr - | call ->suspend - | jmp <1 - | - | // For getting a value that spans a buffer seam. Falls back to C. - |.macro getvalue_slow, func, bytes - | sub rsp, 8 // Need stack space for func to write value to. - |1: - | mov qword [rsp], 0 // For parsing routines that only parse 32 bits. - | mov ARG1_64, DECODER - | mov ARG2_64, rsp - | mov DECODER->checkpoint, PTR - | commit_regs - | callp func - | load_regs - | test eax, eax - | jns >2 - | // Success; return parsed data (in rdx AND xmm0). - | mov rdx, [rsp] - | movsd xmm0, qword [rsp] - | add rsp, 8 - | sub PTR, bytes // Bias our buffer pointer to rejoin the fast-path. - | mov DECODER->ptr, PTR - | ret - |2: - | call ->exitjit // Return eax from decode function. - | jmp <1 - |.endmacro - | - asmlabel(jc, "parse_unknown"); - | // Args: edx=fieldnum, cl=wire type - |->parse_unknown: - | // OPT: handle directly instead of kicking to C. - | // Check for ENDGROUP. - | mov ARG1_64, DECODER - | mov ARG2_32, edx - | movzx ARG3_32, cl - | commit_regs - | callp upb_pbdecoder_skipunknown - | load_regs - | cmp eax, DECODE_ENDGROUP - | jne >1 - | ret // Return eax=DECODE_ENDGROUP, not zero - |1: - | cmp eax, DECODE_OK - | je >1 - | call ->exitjit // Return eax from decode function. - |1: - | xor eax, eax - | ret - | - | // Fallback functions for parsing single values. These are used when the - | // buffer doesn't contain enough remaining data for the fast path. Each - | // primitive type (v32, v64, f32, f64) has two functions: decode & skip. - | // Decode functions return their value in rsi/esi. - | // - | // These functions leave PTR = value_end - fast_path_bytes, so that we can - | // re-join the fast path which will add fast_path_bytes after the callback - | // completes. We also set DECODER->ptr to this value which is a signal to - | // ->suspend that DECODER->checkpoint is up to date. - asmlabel(jc, "skip_decode_f32_fallback"); - |->skipf32_fallback: - |->decodef32_fallback: - | getvalue_slow upb_pbdecoder_decode_f32, 4 - | - asmlabel(jc, "skip_decode_f64_fallback"); - |->skipf64_fallback: - |->decodef64_fallback: - | getvalue_slow upb_pbdecoder_decode_f64, 8 - | - | // Called for varint >= 1 byte. - asmlabel(jc, "skip_decode_v32_fallback"); - |->skipv32_fallback: - |->skipv64_fallback: - | chkeob 16, >1 - | // With at least 16 bytes left, we can do a branch-less SSE version. - | movdqu xmm0, [PTR] - | pmovmskb eax, xmm0 // bits 0-15 are continuation bits, 16-31 are 0. - | not eax - | bsf eax, eax - | cmp al, 10 - | jae ->decode_varint_slow // Error (>10 byte varint). - | add PTR, rax // bsf result is 0-based, so PTR=end-1, as desired. - | ret - | - |1: - | // With fewer than 16 bytes, we have to read byte by byte. - | lea rcx, [PTR + 10] - | mov rax, PTR // Preserve PTR in case of fallback to slow path. - | cmp rcx, DATAEND - | cmova rcx, DATAEND // rcx = MIN(DATAEND, PTR + 10) - |2: - | cmp rax, rcx - | je ->decode_varint_slow - | test byte [rax], 0x80 - | jz >3 - | add rax, 1 - | jmp <2 - |3: - | mov PTR, rax // PTR = varint_end - 1, as desired - | ret - | - | // Returns tag in edx - asmlabel(jc, "decode_unknown_tag_fallback"); - |->decode_unknown_tag_fallback: - | sub rsp, 16 - |1: - | cmp PTR, DELIMEND - | jne >2 - | add rsp, 16 - | xor eax, eax - | ret - |2: - | // OPT: Have a medium-fast path before falling back to _slow. - | mov ARG1_64, DECODER - | mov ARG2_64, rsp - | commit_regs - | callp upb_pbdecoder_decode_varint_slow - | load_regs - | cmp eax, 0 - | jge >3 - | mov edx, [rsp] // Success; return parsed data. - | add rsp, 16 - | ret - |3: - | call ->exitjit // Return eax from decode function. - | jmp <1 - | - | // Called for varint >= 1 byte. - asmlabel(jc, "decode_v32_v64_fallback"); - |->decodev32_fallback: - |->decodev64_fallback: - | chkeob 10, ->decode_varint_slow - | // OPT: do something faster than just calling the C version. - | mov rdi, PTR - | callp upb_vdecode_fast - | test rax, rax - | je ->decode_varint_slow // Unterminated varint. - | mov PTR, rax - | sub PTR, 1 - | mov DECODER->ptr, PTR - | ret - | - asmlabel(jc, "decode_varint_slow"); - |->decode_varint_slow: - | // Slow path: end of buffer or error (varint length >= 10). - | getvalue_slow upb_pbdecoder_decode_varint_slow, 1 - | - | // Args: rsi=expected tag, return=rax (DECODE_{OK,MISMATCH}) - asmlabel(jc, "checktag_fallback"); - |->checktag_fallback: - | sub rsp, 8 - | mov [rsp], rsi // Preserve expected tag. - |1: - | mov ARG1_64, DECODER - | commit_regs - | mov DECODER->checkpoint, PTR - | callp upb_pbdecoder_checktag_slow - | load_regs - | cmp eax, 0 - | jge >2 - | add rsp, 8 - | ret - |2: - | call ->exitjit - | mov rsi, [rsp] - | cmp PTR, DELIMEND - | jne <1 - | mov eax, DECODE_EOF - | add rsp, 8 - | ret - | - | // Args: rsi=upb_inttable, rdx=key, return=rax (-1 if not found). - | // Preserves: rcx, rdx - | // OPT: Could write this in assembly if it's a hotspot. - asmlabel(jc, "hashlookup"); - |->hashlookup: - | push rcx - | push rdx - | sub rsp, 16 - | mov rdi, rsi - | mov rsi, rdx - | mov rdx, rsp - | callp upb_inttable_lookup - | add rsp, 16 - | pop rdx - | pop rcx - | test al, al - | jz >2 // Unknown field. - | mov rax, [rsp-32] // Value from table. - | ret - |2: - | xor rax, rax - | not rax - | ret -} - -static void jitprimitive(jitcompiler *jc, opcode op, - const upb_handlers *h, upb_selector_t sel) { - typedef enum { V32, V64, F32, F64, X } valtype_t; - static valtype_t types[] = { - X, F64, F32, V64, V64, V32, F64, F32, V64, X, X, X, X, V32, V32, F32, F64, - V32, V64 }; - static char fastpath_bytes[] = { 1, 1, 4, 8 }; - const valtype_t vtype = types[op]; - const int fastbytes = fastpath_bytes[vtype]; - upb_func *handler = gethandler(h, sel); - upb_fieldtype_t ftype; - size_t offset; - int32_t hasbit; - - if (handler) { - |1: - | chkneob fastbytes, >3 - |2: - switch (vtype) { - case V32: - | call ->decodev32_fallback - break; - case V64: - | call ->decodev64_fallback - break; - case F32: - | call ->decodef32_fallback - break; - case F64: - | call ->decodef64_fallback - break; - case X: break; - } - | jmp >4 - - /* Fast path decode; for when check_bytes bytes are available. */ - |3: - switch (op) { - case OP_PARSE_SFIXED32: - case OP_PARSE_FIXED32: - | mov edx, dword [PTR] - break; - case OP_PARSE_SFIXED64: - case OP_PARSE_FIXED64: - | mov rdx, qword [PTR] - break; - case OP_PARSE_FLOAT: - | movss xmm0, dword [PTR] - break; - case OP_PARSE_DOUBLE: - | movsd xmm0, qword [PTR] - break; - default: - /* Inline one byte of varint decoding. */ - | movzx edx, byte [PTR] - | test dl, dl - | js <2 // Fallback to slow path for >1 byte varint. - break; - } - - /* Second-stage decode; used for both fast and slow paths */ - /* (only needed for a few types). */ - |4: - switch (op) { - case OP_PARSE_SINT32: - /* 32-bit zig-zag decode. */ - | mov eax, edx - | shr edx, 1 - | and eax, 1 - | neg eax - | xor edx, eax - break; - case OP_PARSE_SINT64: - /* 64-bit zig-zag decode. */ - | mov rax, rdx - | shr rdx, 1 - | and rax, 1 - | neg rax - | xor rdx, rax - break; - case OP_PARSE_BOOL: - | test rdx, rdx - | setne dl - break; - default: break; - } - - /* Call callback (or specialize if we can). */ - if (upb_msg_getscalarhandlerdata(h, sel, &ftype, &offset, &hasbit)) { - switch (ftype) { - case UPB_TYPE_INT64: - case UPB_TYPE_UINT64: - | mov [CLOSURE + offset], rdx - break; - case UPB_TYPE_INT32: - case UPB_TYPE_UINT32: - case UPB_TYPE_ENUM: - | mov [CLOSURE + offset], edx - break; - case UPB_TYPE_DOUBLE: - | movsd qword [CLOSURE + offset], XMMARG1 - break; - case UPB_TYPE_FLOAT: - | movss dword [CLOSURE + offset], XMMARG1 - break; - case UPB_TYPE_BOOL: - | mov [CLOSURE + offset], dl - break; - case UPB_TYPE_STRING: - case UPB_TYPE_BYTES: - case UPB_TYPE_MESSAGE: - UPB_ASSERT(false); break; - } - | sethas CLOSURE, hasbit - } else if (handler) { - | mov ARG1_64, CLOSURE - | load_handler_data h, sel - | callp handler - if (!alwaysok(h, sel)) { - | test al, al - | jnz >5 - | call ->suspend - | jmp <1 - |5: - } - } - - /* We do this last so that the checkpoint is not advanced past the user's - * data until the callback has returned success. */ - | add PTR, fastbytes - } else { - /* No handler registered for this value, just skip it. */ - | chkneob fastbytes, >3 - |2: - switch (vtype) { - case V32: - | call ->skipv32_fallback - break; - case V64: - | call ->skipv64_fallback - break; - case F32: - | call ->skipf32_fallback - break; - case F64: - | call ->skipf64_fallback - break; - case X: break; - } - - /* Fast-path skip. */ - |3: - if (vtype == V32 || vtype == V64) { - | test byte [PTR], 0x80 - | jnz <2 - } - | add PTR, fastbytes - } -} - -static void jitdispatch(jitcompiler *jc, - const upb_pbdecodermethod *method) { - /* Lots of room for tweaking/optimization here. */ - - const upb_inttable *dispatch = &method->dispatch; - bool has_hash_entries = (dispatch->t.count > 0); - - /* Whether any of the fields for this message can have two wire types which - * are both valid (packed & non-packed). - * - * OPT: populate this more precisely; not all messages with hash entries have - * this characteristic. */ - bool has_multi_wiretype = has_hash_entries; - - |=>define_jmptarget(jc, &method->dispatch): - |1: - /* Decode the field tag. */ - | mov aword DECODER->checkpoint, PTR - | chkeob 2, >6 - | movzx edx, byte [PTR] - | test dl, dl - | jns >7 // Jump if first byte has no continuation bit. - | movzx ecx, byte [PTR + 1] - | test cl, cl - | js >6 // Jump if second byte has continuation bit. - | // Confirmed two-byte varint. - | shl ecx, 7 - | and edx, 0x7f - | or edx, ecx - | add PTR, 2 - | jmp >8 - |6: - | call ->decode_unknown_tag_fallback - | test eax, eax // Hit DELIMEND? - | jnz >8 - | ret - |7: - | add PTR, 1 - |8: - | mov ecx, edx - | shr edx, 3 - | and cl, 7 - - /* See comment attached to upb_pbdecodermethod.dispatch for layout of the - * dispatch table. */ - |2: - | cmp edx, dispatch->array_size - if (has_hash_entries) { - | jae >7 - } else { - | jae >5 - } - | // OPT: Compact the lookup arr into 32-bit entries. - if ((uintptr_t)dispatch->array > 0x7fffffff) { - | mov64 rax, (uintptr_t)dispatch->array - | mov rax, qword [rax + rdx * 8] - } else { - | mov rax, qword [rdx * 8 + dispatch->array] - } - |3: - | // We take advantage of the fact that non-present entries are stored - | // as -1, which will result in wire types that will never match. - | cmp al, cl - if (has_multi_wiretype) { - | jne >6 - } else { - | jne >5 - } - | shr rax, 16 - | - | // Load the machine code address from the table entry. - | // The table entry is relative to the dispatch->array jmptarget - | // (patchdispatch() took care of this) which is the same as - | // local label "4". The "lea" is really just trying to do - | // lea rax, [>4 + rax] - | // - | // But we can't write that directly for some reason, so we use - | // rdx as a temporary. - | lea rdx, [>4] - |=>define_jmptarget(jc, dispatch->array): - |4: - | add rax, rdx - | ret - | - |5: - | // Field isn't in our table. - | - | // For pushing unknown fields to the unknown field handler. - | mov64 rax, (uintptr_t)method->dest_handlers_ - | mov FRAME->sink.handlers, rax - | - | call ->parse_unknown - | test eax, eax // ENDGROUP? - | jz <1 - | lea rax, [>9] // ENDGROUP; Load address of OP_ENDMSG. - | ret - - if (has_multi_wiretype) { - |6: - | // Primary wire type didn't match, check secondary wire type. - | cmp ah, cl - | jne <5 - | // Secondary wire type is a match, look up fn + UPB_MAX_FIELDNUMBER. - | add rdx, UPB_MAX_FIELDNUMBER - | // This key will never be in the array part, so do a hash lookup. - UPB_ASSERT(has_hash_entries); - | ld64 dispatch - | jmp ->hashlookup // Tail call. - } - - if (has_hash_entries) { - |7: - | // Hash table lookup. - | ld64 dispatch - | call ->hashlookup - | jmp <3 - } -} - -static void jittag(jitcompiler *jc, uint64_t tag, int n, int ofs, - const upb_pbdecodermethod *method) { - /* Internally we parse unknown fields; if this runs us into DELIMEND we jump - * to the corresponding DELIMEND target (either msg end or repeated field - * end), which we find from the OP_CHECKDELIM which must have necessarily - * preceded us. */ - uint32_t last_instruction = *(jc->pc - 2); - int last_arg = (int32_t)last_instruction >> 8; - uint32_t *delimend = (jc->pc - 1) + last_arg; - const size_t ptr_words = sizeof(void*) / sizeof(uint32_t); - - UPB_ASSERT((last_instruction & 0xff) == OP_CHECKDELIM); - - if (getop(*(jc->pc - 1)) == OP_TAGN) { - jc->pc += ptr_words; - } - - | chkneob n, >1 - - | // OPT: this is way too much fallback code to put here. - | // Reduce and/or move to a separate section to make better icache usage. - | ld64 tag - | call ->checktag_fallback - | cmp eax, DECODE_MISMATCH - | je >3 - | cmp eax, DECODE_EOF - | je =>jmptarget(jc, delimend) - | jmp >5 - - |1: - switch (n) { - case 1: - | cmp byte [PTR], tag - break; - case 2: - | cmp word [PTR], tag - break; - case 3: - | // OPT: Slightly more efficient code, but depends on an extra byte. - | // mov eax, dword [PTR] - | // shl eax, 8 - | // cmp eax, tag << 8 - | cmp word [PTR], (tag & 0xffff) - | jne >2 - | cmp byte [PTR + 2], (tag >> 16) - |2: - break; - case 4: - | cmp dword [PTR], tag - break; - case 5: - | cmp dword [PTR], (tag & 0xffffffff) - | jne >3 - | cmp byte [PTR + 4], (tag >> 32) - } - | je >4 - |3: - if (ofs == 0) { - | call =>jmptarget(jc, &method->dispatch) - | test rax, rax - | jz =>jmptarget(jc, delimend) - | jmp rax - } else { - | jmp =>jmptarget(jc, jc->pc + ofs) - } - |4: - | add PTR, n - |5: -} - -/* Compile the bytecode to x64. */ -static void jitbytecode(jitcompiler *jc) { - upb_pbdecodermethod *method = NULL; - const upb_handlers *h = NULL; - for (jc->pc = jc->group->bytecode; jc->pc < jc->group->bytecode_end; ) { - int32_t instr = *jc->pc; - opcode op = instr & 0xff; - uint32_t arg = instr >> 8; - int32_t longofs = arg; - - if (op != OP_SETDISPATCH) { - /* Skipped for SETDISPATCH because it defines its own asmlabel for the - * dispatch code it emits. */ - asmlabel(jc, "0x%lx.%s", pcofs(jc), upb_pbdecoder_getopname(op)); - - /* Skipped for SETDISPATCH because it should point at the function - * prologue, not the dispatch function that is emitted first. - * TODO: optimize this to only define pclabels that are actually used. */ - |=>define_jmptarget(jc, jc->pc): - } - - jc->pc++; - - switch (op) { - case OP_STARTMSG: { - upb_func *startmsg = gethandler(h, UPB_STARTMSG_SELECTOR); - if (startmsg) { - /* bool startmsg(void *closure, const void *hd) */ - |1: - | mov ARG1_64, CLOSURE - | load_handler_data h, UPB_STARTMSG_SELECTOR - | callp startmsg - if (!alwaysok(h, UPB_STARTMSG_SELECTOR)) { - | test al, al - | jnz >2 - | call ->suspend - | jmp <1 - |2: - } - } else { - | nop - } - break; - } - case OP_ENDMSG: { - upb_func *endmsg = gethandler(h, UPB_ENDMSG_SELECTOR); - |9: - if (endmsg) { - /* bool endmsg(void *closure, const void *hd, upb_status *status) */ - | mov ARG1_64, CLOSURE - | load_handler_data h, UPB_ENDMSG_SELECTOR - | mov ARG3_64, DECODER->status - | callp endmsg - } - break; - } - case OP_SETDISPATCH: { - uint32_t *op_pc = jc->pc - 1; - const char *msgname; - upb_inttable *dispatch; - - /* Load info for new method. */ - memcpy(&dispatch, jc->pc, sizeof(void*)); - jc->pc += sizeof(void*) / sizeof(uint32_t); - /* The OP_SETDISPATCH bytecode contains a pointer that is - * &method->dispatch; we want to go backwards and recover method. */ - method = - (void*)((char*)dispatch - offsetof(upb_pbdecodermethod, dispatch)); - /* May be NULL, in which case no handlers for this message will be found. - * OPT: we should do better by completely skipping the message in this - * case instead of parsing it field by field. We should also do the skip - * in the containing message's code. */ - h = method->dest_handlers_; - msgname = upb_msgdef_fullname(upb_handlers_msgdef(h)); - - /* Emit dispatch code for new method. */ - asmlabel(jc, "0x%lx.dispatch.%s", pcofs(jc), msgname); - jitdispatch(jc, method); - - /* Emit function prologue for new method. */ - asmlabel(jc, "0x%lx.parse.%s", pcofs(jc), msgname); - |=>define_jmptarget(jc, op_pc): - |=>define_jmptarget(jc, method): - | sub rsp, 8 - - break; - } - case OP_PARSE_DOUBLE: - case OP_PARSE_FLOAT: - case OP_PARSE_INT64: - case OP_PARSE_UINT64: - case OP_PARSE_INT32: - case OP_PARSE_FIXED64: - case OP_PARSE_FIXED32: - case OP_PARSE_BOOL: - case OP_PARSE_UINT32: - case OP_PARSE_SFIXED32: - case OP_PARSE_SFIXED64: - case OP_PARSE_SINT32: - case OP_PARSE_SINT64: - jitprimitive(jc, op, h, arg); - break; - case OP_STARTSEQ: - case OP_STARTSUBMSG: - case OP_STARTSTR: { - upb_func *start = gethandler(h, arg); - if (start) { - /* void *startseq(void *closure, const void *hd) - * void *startsubmsg(void *closure, const void *hd) - * void *startstr(void *closure, const void *hd, size_t size_hint) */ - |1: - | mov ARG1_64, CLOSURE - | load_handler_data h, arg - if (op == OP_STARTSTR) { - | mov ARG3_64, DELIMEND - | sub ARG3_64, PTR - } - | callp start - if (!alwaysok(h, arg)) { - | test rax, rax - | jnz >2 - | call ->suspend - | jmp <1 - |2: - } - | mov CLOSURE, rax - } else { - /* TODO: nop is only required because of asmlabel(). */ - | nop - } - break; - } - case OP_ENDSEQ: - case OP_ENDSUBMSG: - case OP_ENDSTR: { - upb_func *end = gethandler(h, arg); - if (end) { - /* bool endseq(void *closure, const void *hd) - * bool endsubmsg(void *closure, const void *hd) - * bool endstr(void *closure, const void *hd) */ - |1: - | mov ARG1_64, CLOSURE - | load_handler_data h, arg - | callp end - if (!alwaysok(h, arg)) { - | test al, al - | jnz >2 - | call ->suspend - | jmp <1 - |2: - } - } else { - /* TODO: nop is only required because of asmlabel(). */ - | nop - } - break; - } - case OP_STRING: { - upb_func *str = gethandler(h, arg); - | cmp PTR, DELIMEND - | je >4 - |1: - | cmp PTR, DATAEND - | jne >2 - | call ->suspend - | jmp <1 - |2: - if (str) { - /* size_t str(void *closure, const void *hd, const char *str, - * size_t n) */ - | mov ARG1_64, CLOSURE - | load_handler_data h, arg - | mov ARG3_64, PTR - | mov ARG4_64, DATAEND - | sub ARG4_64, PTR - | mov ARG5_64, qword DECODER->handle - | callp str - | add PTR, rax - if (!alwaysok(h, arg)) { - | cmp PTR, DATAEND - | je >3 - | call ->strret_fallback - |3: - } - } else { - | mov PTR, DATAEND - } - | cmp PTR, DELIMEND - | jne <1 - |4: - break; - } - case OP_PUSHTAGDELIM: - | mov FRAME->sink.closure, CLOSURE - | // This shouldn't need to be read, because tag-delimited fields - | // shouldn't have an OP_SETDELIM after them. But for the moment - | // non-packed repeated fields do OP_SETDELIM so they can share more - | // code with the packed code-path. If this is changed later, this - | // store can be removed. - | mov qword FRAME->end_ofs, 0 - | cmp FRAME, DECODER->limit - | je ->err - | add FRAME, sizeof(upb_pbdecoder_frame) - | mov dword FRAME->groupnum, arg - break; - case OP_PUSHLENDELIM: - | call ->pushlendelim - break; - case OP_POP: - | sub FRAME, sizeof(upb_pbdecoder_frame) - | mov CLOSURE, FRAME->sink.closure - break; - case OP_SETDELIM: - /* OPT: experiment with testing vs old offset to optimize away. */ - | mov DATAEND, DECODER->end - | add DELIMEND, FRAME->end_ofs - | cmp DELIMEND, DECODER->buf - | jb >1 - | cmp DELIMEND, DATAEND - | ja >1 // OPT: try cmov. - | mov DATAEND, DELIMEND - |1: - break; - case OP_SETBIGGROUPNUM: - | mov dword FRAME->groupnum, *jc->pc++ - break; - case OP_CHECKDELIM: - | cmp DELIMEND, PTR - | je =>jmptarget(jc, jc->pc + longofs) - break; - case OP_CALL: - | call =>jmptarget(jc, jc->pc + longofs) - break; - case OP_BRANCH: - | jmp =>jmptarget(jc, jc->pc + longofs); - break; - case OP_RET: - |9: - | add rsp, 8 - | ret - break; - case OP_TAG1: - jittag(jc, (arg >> 8) & 0xff, 1, (int8_t)arg, method); - break; - case OP_TAG2: - jittag(jc, (arg >> 8) & 0xffff, 2, (int8_t)arg, method); - break; - case OP_TAGN: { - uint64_t tag; - memcpy(&tag, jc->pc, 8); - jittag(jc, tag, arg >> 8, (int8_t)arg, method); - break; - } - case OP_DISPATCH: - | call =>jmptarget(jc, &method->dispatch) - break; - case OP_HALT: - UPB_ASSERT(false); - } - } - - asmlabel(jc, "eof"); - | nop -} diff --git a/upb/pb/compile_decoder_x64.h b/upb/pb/compile_decoder_x64.h deleted file mode 100644 index 4a4dffc..0000000 --- a/upb/pb/compile_decoder_x64.h +++ /dev/null @@ -1,1737 +0,0 @@ -/* -** This file has been pre-processed with DynASM. -** http://luajit.org/dynasm.html -** DynASM version 1.3.0, DynASM x64 version 1.3.0 -** DO NOT EDIT! The original file is in "upb/pb/compile_decoder_x64.dasc". -*/ - -#if DASM_VERSION != 10300 -#error "Version mismatch between DynASM and included encoding engine" -#endif - -# 1 "upb/pb/compile_decoder_x64.dasc" -/*|// */ -/*|// upb - a minimalist implementation of protocol buffers. */ -/*|// */ -/*|// Copyright (c) 2011-2013 Google Inc. See LICENSE for details. */ -/*|// Author: Josh Haberman */ -/*|// */ -/*|// JIT compiler for upb_pbdecoder on x86-64. Generates machine code from the */ -/*|// bytecode generated in compile_decoder.c. */ -/*| */ -/*|.arch x64 */ -/*|.actionlist upb_jit_actionlist */ -static const unsigned char upb_jit_actionlist[2467] = { - 249,255,248,10,248,1,85,65,87,65,86,65,85,65,84,83,72,137,252,243,73,137, - 252,255,72,184,237,237,65,84,73,137,228,72,129,228,239,252,255,208,76,137, - 228,65,92,133,192,15,137,244,247,73,137,167,233,72,137,216,77,139,183,233, - 73,139,159,233,77,139,167,233,77,139,174,233,73,139,174,233,73,43,175,233, - 73,3,175,233,73,139,151,233,72,133,210,15,133,244,248,252,255,208,73,139, - 135,233,73,199,135,233,0,0,0,0,248,1,255,91,65,92,65,93,65,94,65,95,93,195, - 248,2,73,139,183,233,72,41,212,72,137,231,72,184,237,237,65,84,73,137,228, - 72,129,228,239,252,255,208,76,137,228,65,92,195,255,248,11,73,139,191,233, - 72,137,230,73,139,151,233,72,41,226,73,137,151,233,137,195,72,184,237,237, - 65,84,73,137,228,72,129,228,239,252,255,208,76,137,228,65,92,137,216,73,139, - 167,233,91,65,92,65,93,65,94,65,95,93,195,255,248,12,73,57,159,233,15,132, - 244,247,73,137,159,233,248,1,77,137,183,233,73,137,159,233,77,137,167,233, - 73,137,175,233,73,43,175,233,73,3,175,233,73,137,174,233,77,137,174,233,76, - 137,252,255,72,184,237,237,65,84,73,137,228,72,129,228,239,252,255,208,76, - 137,228,65,92,252,233,244,11,255,248,13,248,1,77,137,174,233,73,137,159,233, - 255,76,57,227,15,132,244,253,255,76,137,225,72,41,217,72,131,252,249,1,15, - 130,244,253,255,15,182,19,132,210,15,137,244,254,248,7,232,244,14,248,8,72, - 131,195,1,72,137,252,233,72,41,217,72,41,209,15,130,244,250,73,137,142,233, - 77,59,183,233,15,132,244,249,73,129,198,239,72,137,221,72,1,213,65,199,134, - 233,0,0,0,0,72,133,201,15,132,244,248,77,139,167,233,72,57,252,235,15,135, - 244,248,76,57,229,15,135,244,248,255,73,137,252,236,248,2,195,248,3,73,139, - 159,233,76,137,252,255,255,72,190,237,237,255,190,237,255,49,252,246,255, - 72,184,237,237,65,84,73,137,228,72,129,228,239,252,255,208,76,137,228,65, - 92,232,244,12,252,233,244,1,248,4,73,139,159,233,76,137,252,255,255,72,184, - 237,237,65,84,73,137,228,72,129,228,239,252,255,208,76,137,228,65,92,232, - 244,12,252,233,244,1,255,248,15,76,137,252,255,137,214,15,182,209,77,137, - 183,233,73,137,159,233,77,137,167,233,73,137,175,233,73,43,175,233,73,3,175, - 233,73,137,174,233,77,137,174,233,72,184,237,237,65,84,73,137,228,72,129, - 228,239,252,255,208,76,137,228,65,92,77,139,183,233,73,139,159,233,77,139, - 167,233,77,139,174,233,73,139,174,233,73,43,175,233,73,3,175,233,129,252, - 248,239,255,15,133,244,247,195,248,1,129,252,248,239,15,132,244,247,232,244, - 11,248,1,49,192,195,255,248,16,248,17,72,131,252,236,8,248,1,72,199,4,36, - 0,0,0,0,76,137,252,255,72,137,230,73,137,159,233,77,137,183,233,73,137,159, - 233,77,137,167,233,73,137,175,233,73,43,175,233,73,3,175,233,73,137,174,233, - 77,137,174,233,72,184,237,237,65,84,73,137,228,72,129,228,239,252,255,208, - 76,137,228,65,92,77,139,183,233,73,139,159,233,77,139,167,233,77,139,174, - 233,73,139,174,233,255,73,43,175,233,73,3,175,233,133,192,15,137,244,248, - 72,139,20,36,252,242,15,16,4,36,72,131,196,8,72,131,252,235,4,73,137,159, - 233,195,248,2,232,244,11,252,233,244,1,255,248,18,248,19,72,131,252,236,8, - 248,1,72,199,4,36,0,0,0,0,76,137,252,255,72,137,230,73,137,159,233,77,137, - 183,233,73,137,159,233,77,137,167,233,73,137,175,233,73,43,175,233,73,3,175, - 233,73,137,174,233,77,137,174,233,72,184,237,237,65,84,73,137,228,72,129, - 228,239,252,255,208,76,137,228,65,92,77,139,183,233,73,139,159,233,77,139, - 167,233,77,139,174,233,73,139,174,233,255,73,43,175,233,73,3,175,233,133, - 192,15,137,244,248,72,139,20,36,252,242,15,16,4,36,72,131,196,8,72,131,252, - 235,8,73,137,159,233,195,248,2,232,244,11,252,233,244,1,255,248,20,248,21, - 255,76,57,227,15,132,244,247,255,76,137,225,72,41,217,72,131,252,249,16,15, - 130,244,247,255,252,243,15,111,3,102,15,215,192,252,247,208,15,188,192,60, - 10,15,131,244,22,72,1,195,195,248,1,72,141,139,233,72,137,216,76,57,225,73, - 15,71,204,248,2,72,57,200,15,132,244,22,252,246,0,128,15,132,244,249,72,131, - 192,1,252,233,244,2,248,3,72,137,195,195,255,248,23,72,131,252,236,16,248, - 1,72,57,252,235,15,133,244,248,72,131,196,16,49,192,195,248,2,76,137,252, - 255,72,137,230,77,137,183,233,73,137,159,233,77,137,167,233,73,137,175,233, - 73,43,175,233,73,3,175,233,73,137,174,233,77,137,174,233,72,184,237,237,65, - 84,73,137,228,72,129,228,239,252,255,208,76,137,228,65,92,77,139,183,233, - 73,139,159,233,77,139,167,233,77,139,174,233,255,73,139,174,233,73,43,175, - 233,73,3,175,233,131,252,248,0,15,141,244,249,139,20,36,72,131,196,16,195, - 248,3,232,244,11,252,233,244,1,255,248,14,248,24,255,76,57,227,15,132,244, - 22,255,76,137,225,72,41,217,72,131,252,249,10,15,130,244,22,255,72,137,223, - 72,184,237,237,65,84,73,137,228,72,129,228,239,252,255,208,76,137,228,65, - 92,72,133,192,15,132,244,22,72,137,195,72,131,252,235,1,73,137,159,233,195, - 255,248,22,72,131,252,236,8,248,1,72,199,4,36,0,0,0,0,76,137,252,255,72,137, - 230,73,137,159,233,77,137,183,233,73,137,159,233,77,137,167,233,73,137,175, - 233,73,43,175,233,73,3,175,233,73,137,174,233,77,137,174,233,72,184,237,237, - 65,84,73,137,228,72,129,228,239,252,255,208,76,137,228,65,92,77,139,183,233, - 73,139,159,233,77,139,167,233,77,139,174,233,73,139,174,233,73,43,175,233, - 255,73,3,175,233,133,192,15,137,244,248,72,139,20,36,252,242,15,16,4,36,72, - 131,196,8,72,131,252,235,1,73,137,159,233,195,248,2,232,244,11,252,233,244, - 1,255,248,25,72,131,252,236,8,72,137,52,36,248,1,76,137,252,255,77,137,183, - 233,73,137,159,233,77,137,167,233,73,137,175,233,73,43,175,233,73,3,175,233, - 73,137,174,233,77,137,174,233,73,137,159,233,72,184,237,237,65,84,73,137, - 228,72,129,228,239,252,255,208,76,137,228,65,92,77,139,183,233,73,139,159, - 233,77,139,167,233,77,139,174,233,73,139,174,233,73,43,175,233,255,73,3,175, - 233,131,252,248,0,15,141,244,248,72,131,196,8,195,248,2,232,244,11,72,139, - 52,36,72,57,252,235,15,133,244,1,184,237,72,131,196,8,195,255,248,26,81,82, - 72,131,252,236,16,72,137,252,247,72,137,214,72,137,226,72,184,237,237,65, - 84,73,137,228,72,129,228,239,252,255,208,76,137,228,65,92,72,131,196,16,90, - 89,132,192,15,132,244,248,72,139,68,36,224,195,248,2,72,49,192,72,252,247, - 208,195,255,76,57,227,15,133,244,249,255,76,137,225,72,41,217,72,129,252, - 249,239,15,131,244,249,255,248,2,255,232,244,14,255,232,244,24,255,232,244, - 17,255,232,244,19,255,252,233,244,250,255,248,3,255,139,19,255,72,139,19, - 255,252,243,15,16,3,255,252,242,15,16,3,255,15,182,19,132,210,15,136,244, - 2,255,248,4,255,137,208,209,252,234,131,224,1,252,247,216,49,194,255,72,137, - 208,72,209,252,234,72,131,224,1,72,252,247,216,72,49,194,255,72,133,210,15, - 149,210,255,73,137,149,233,255,65,137,149,233,255,252,242,65,15,17,133,233, - 255,252,243,65,15,17,133,233,255,65,136,149,233,255,65,128,141,233,235,255, - 76,137,252,239,255,72,184,237,237,65,84,73,137,228,72,129,228,239,252,255, - 208,76,137,228,65,92,255,132,192,15,133,244,251,232,244,12,252,233,244,1, - 248,5,255,72,129,195,239,255,232,244,20,255,232,244,21,255,232,244,16,255, - 232,244,18,255,252,246,3,128,15,133,244,2,255,249,248,1,255,76,57,227,15, - 132,244,252,255,76,137,225,72,41,217,72,131,252,249,2,15,130,244,252,255, - 15,182,19,132,210,15,137,244,253,15,182,139,233,132,201,15,136,244,252,193, - 225,7,131,226,127,9,202,72,131,195,2,252,233,244,254,248,6,232,244,23,133, - 192,15,133,244,254,195,248,7,72,131,195,1,248,8,137,209,193,252,234,3,128, - 225,7,255,248,2,129,252,250,239,255,15,131,244,253,255,15,131,244,251,255, - 72,184,237,237,72,139,4,208,255,72,139,4,213,237,255,248,3,56,200,255,15, - 133,244,252,255,15,133,244,251,255,72,193,232,16,72,141,21,244,250,249,248, - 4,72,1,208,195,248,5,72,184,237,237,73,137,134,233,232,244,15,133,192,15, - 132,244,1,72,141,5,244,255,195,255,248,6,56,204,15,133,244,5,72,129,194,239, - 255,252,233,244,26,255,248,7,255,232,244,26,252,233,244,3,255,76,57,227,15, - 133,244,247,255,76,137,225,72,41,217,72,129,252,249,239,15,131,244,247,255, - 232,244,25,129,252,248,239,15,132,244,249,129,252,248,239,15,132,245,252, - 233,244,251,255,128,59,235,255,102,129,59,238,255,102,129,59,238,15,133,244, - 248,128,187,233,235,248,2,255,129,59,239,255,129,59,239,15,133,244,249,128, - 187,233,235,255,15,132,244,250,248,3,255,232,245,72,133,192,15,132,245,252, - 255,224,255,252,233,245,255,248,4,72,129,195,239,248,5,255,248,1,76,137,252, - 239,255,132,192,15,133,244,248,232,244,12,252,233,244,1,248,2,255,144,255, - 248,9,255,73,139,151,233,72,184,237,237,65,84,73,137,228,72,129,228,239,252, - 255,208,76,137,228,65,92,255,249,249,72,131,252,236,8,255,72,137,252,234, - 72,41,218,255,72,133,192,15,133,244,248,232,244,12,252,233,244,1,248,2,255, - 73,137,197,255,72,57,252,235,15,132,244,250,248,1,76,57,227,15,133,244,248, - 232,244,12,252,233,244,1,248,2,255,72,137,218,76,137,225,72,41,217,77,139, - 135,233,72,184,237,237,65,84,73,137,228,72,129,228,239,252,255,208,76,137, - 228,65,92,72,1,195,255,76,57,227,15,132,244,249,232,244,27,248,3,255,76,137, - 227,255,72,57,252,235,15,133,244,1,248,4,255,77,137,174,233,73,199,134,233, - 0,0,0,0,77,59,183,233,15,132,244,28,73,129,198,239,65,199,134,233,237,255, - 232,244,13,255,73,129,252,238,239,77,139,174,233,255,77,139,167,233,73,3, - 174,233,73,59,175,233,15,130,244,247,76,57,229,15,135,244,247,73,137,252, - 236,248,1,255,72,57,221,15,132,245,255,232,245,255,248,9,72,131,196,8,195, - 255 -}; - -# 12 "upb/pb/compile_decoder_x64.dasc" -/*|.globals UPB_JIT_GLOBAL_ */ -enum { - UPB_JIT_GLOBAL_enterjit, - UPB_JIT_GLOBAL_exitjit, - UPB_JIT_GLOBAL_suspend, - UPB_JIT_GLOBAL_pushlendelim, - UPB_JIT_GLOBAL_decodev32_fallback, - UPB_JIT_GLOBAL_parse_unknown, - UPB_JIT_GLOBAL_skipf32_fallback, - UPB_JIT_GLOBAL_decodef32_fallback, - UPB_JIT_GLOBAL_skipf64_fallback, - UPB_JIT_GLOBAL_decodef64_fallback, - UPB_JIT_GLOBAL_skipv32_fallback, - UPB_JIT_GLOBAL_skipv64_fallback, - UPB_JIT_GLOBAL_decode_varint_slow, - UPB_JIT_GLOBAL_decode_unknown_tag_fallback, - UPB_JIT_GLOBAL_decodev64_fallback, - UPB_JIT_GLOBAL_checktag_fallback, - UPB_JIT_GLOBAL_hashlookup, - UPB_JIT_GLOBAL_strret_fallback, - UPB_JIT_GLOBAL_err, - UPB_JIT_GLOBAL__MAX -}; -# 13 "upb/pb/compile_decoder_x64.dasc" -/*|.globalnames upb_jit_globalnames */ -static const char *const upb_jit_globalnames[] = { - "enterjit", - "exitjit", - "suspend", - "pushlendelim", - "decodev32_fallback", - "parse_unknown", - "skipf32_fallback", - "decodef32_fallback", - "skipf64_fallback", - "decodef64_fallback", - "skipv32_fallback", - "skipv64_fallback", - "decode_varint_slow", - "decode_unknown_tag_fallback", - "decodev64_fallback", - "checktag_fallback", - "hashlookup", - "strret_fallback", - "err", - (const char *)0 -}; -# 14 "upb/pb/compile_decoder_x64.dasc" -/*| */ -/*|// Calling conventions. Note -- this will need to be changed for */ -/*|// Windows, which uses a different calling convention! */ -/*|.define ARG1_64, rdi */ -/*|.define ARG2_8, r6b // DynASM's equivalent to "sil" -- low byte of esi. */ -/*|.define ARG2_32, esi */ -/*|.define ARG2_64, rsi */ -/*|.define ARG3_8, dl */ -/*|.define ARG3_32, edx */ -/*|.define ARG3_64, rdx */ -/*|.define ARG4_64, rcx */ -/*|.define ARG5_64, r8 */ -/*|.define XMMARG1, xmm0 */ -/*| */ -/*|// Register allocation / type map. */ -/*|// ALL of the code in this file uses these register allocations. */ -/*|// When we "call" within this file, we do not use regular calling */ -/*|// conventions, but of course when calling to user callbacks we must. */ -/*|.define PTR, rbx // DECODER->ptr (unsynced) */ -/*|.define DATAEND, r12 // DECODER->data_end (unsynced) */ -/*|.define CLOSURE, r13 // FRAME->closure (unsynced) */ -/*|.type FRAME, upb_pbdecoder_frame, r14 // DECODER->top (unsynced) */ -#define Dt1(_V) (int)(ptrdiff_t)&(((upb_pbdecoder_frame *)0)_V) -# 36 "upb/pb/compile_decoder_x64.dasc" -/*|.type DECODER, upb_pbdecoder, r15 // DECODER (immutable) */ -#define Dt2(_V) (int)(ptrdiff_t)&(((upb_pbdecoder *)0)_V) -# 37 "upb/pb/compile_decoder_x64.dasc" -/*|.define DELIMEND, rbp */ -/*| */ -/*| // Spills unsynced registers back to memory. */ -/*|.macro commit_regs */ -/*| mov DECODER->top, FRAME */ -/*| mov DECODER->ptr, PTR */ -/*| mov DECODER->data_end, DATAEND */ -/*| // We don't guarantee that delim_end is NULL when out of range like the */ -/*| // interpreter does. */ -/*| mov DECODER->delim_end, DELIMEND */ -/*| sub DELIMEND, DECODER->buf */ -/*| add DELIMEND, DECODER->bufstart_ofs */ -/*| mov FRAME->end_ofs, DELIMEND */ -/*| mov FRAME->sink.closure, CLOSURE */ -/*|.endmacro */ -/*| */ -/*| // Loads unsynced registers from memory back into registers. */ -/*|.macro load_regs */ -/*| mov FRAME, DECODER->top */ -/*| mov PTR, DECODER->ptr */ -/*| mov DATAEND, DECODER->data_end */ -/*| mov CLOSURE, FRAME->sink.closure */ -/*| mov DELIMEND, FRAME->end_ofs */ -/*| sub DELIMEND, DECODER->bufstart_ofs */ -/*| add DELIMEND, DECODER->buf */ -/*|.endmacro */ -/*| */ -/*| // Calls an external C function at address "addr". */ -/*|.macro callp, addr */ -/*| mov64 rax, (uintptr_t)addr */ -/*| */ -/*| // Stack must be 16-byte aligned (x86-64 ABI requires this). */ -/*| // */ -/*| // OPT: possibly remove this by statically ensuring correct alignment. */ -/*| // */ -/*| // OPT: use "call rel32" where possible. */ -/*| push r12 */ -/*| mov r12, rsp */ -/*| and rsp, 0xfffffffffffffff0UL // Align stack. */ -/*| call rax */ -/*| mov rsp, r12 */ -/*| pop r12 */ -/*|.endmacro */ -/*| */ -/*|.macro ld64, val */ -/*|| { */ -/*|| uintptr_t v = (uintptr_t)val; */ -/*|| if (v > 0xffffffff) { */ -/*| mov64 ARG2_64, v */ -/*|| } else if (v) { */ -/*| mov ARG2_32, v */ -/*|| } else { */ -/*| xor ARG2_32, ARG2_32 */ -/*|| } */ -/*|| } */ -/*|.endmacro */ -/*| */ -/*|.macro load_handler_data, h, arg */ -/*| ld64 gethandlerdata(h, arg) */ -/*|.endmacro */ -/*| */ -/*|.macro chkeob, bytes, target */ -/*|| if (bytes == 1) { */ -/*| cmp PTR, DATAEND */ -/*| je target */ -/*|| } else { */ -/*| mov rcx, DATAEND */ -/*| sub rcx, PTR */ -/*| cmp rcx, bytes */ -/*| jb target */ -/*|| } */ -/*|.endmacro */ -/*| */ -/*|.macro chkneob, bytes, target */ -/*|| if (bytes == 1) { */ -/*| cmp PTR, DATAEND */ -/*| jne target */ -/*|| } else { */ -/*| mov rcx, DATAEND */ -/*| sub rcx, PTR */ -/*| cmp rcx, bytes */ -/*| jae target */ -/*|| } */ -/*|.endmacro */ - -/*|.macro sethas, reg, hasbit */ -/*|| if (hasbit >= 0) { */ -/*| or byte [reg + ((uint32_t)hasbit / 8)], (1 << ((uint32_t)hasbit % 8)) */ -/*|| } */ -/*|.endmacro */ -/*| */ -/*| // Decodes 32-bit varint into rdx, inlining 1 byte. */ -/*|.macro dv32 */ -/*| chkeob 1, >7 */ -/*| movzx edx, byte [PTR] */ -/*| test dl, dl */ -/*| jns >8 */ -/*|7: */ -/*| call ->decodev32_fallback */ -/*|8: */ -/*| add PTR, 1 */ -/*|.endmacro */ - -#define DECODE_EOF -3 - -static upb_func *gethandler(const upb_handlers *h, upb_selector_t sel) { - return h ? upb_handlers_gethandler(h, sel, NULL) : NULL; -} - -/* Defines an "assembly label" for the current code generation offset. - * This label exists *purely* for debugging purposes: it is emitted into - * the .so, and printed as part of JIT debugging output when UPB_JIT_LOAD_SO is - * defined. - * - * We would define this in the .c file except that it conditionally defines a - * pclabel. */ -static void asmlabel(jitcompiler *jc, const char *fmt, ...) { -#ifndef NDEBUG - int ofs = jc->dynasm->section->ofs; - UPB_ASSERT(ofs != jc->lastlabelofs); - jc->lastlabelofs = ofs; -#endif - -#ifndef UPB_JIT_LOAD_SO - UPB_UNUSED(jc); - UPB_UNUSED(fmt); -#else - va_list args; - va_start(args, fmt); - char *str = upb_vasprintf(fmt, args); - va_end(args); - - int pclabel = alloc_pclabel(jc); - /* Normally we would prefer to allocate this inline with the codegen, - * ie. - * |=>asmlabel(...) - * But since we do this conditionally, only when UPB_JIT_LOAD_SO is defined, - * we do it here instead. */ - /*|=>pclabel: */ - dasm_put(Dst, 0, pclabel); -# 176 "upb/pb/compile_decoder_x64.dasc" - upb_inttable_insert(&jc->asmlabels, pclabel, upb_value_ptr(str)); -#endif -} - -/* Should only be called when the associated handler is known to exist. */ -static bool alwaysok(const upb_handlers *h, upb_selector_t sel) { - upb_handlerattr attr = UPB_HANDLERATTR_INIT; - bool ok = upb_handlers_getattr(h, sel, &attr); - - UPB_ASSERT(ok); - return attr.alwaysok; -} - -static const void *gethandlerdata(const upb_handlers *h, upb_selector_t sel) { - upb_handlerattr attr = UPB_HANDLERATTR_INIT; - bool ok = upb_handlers_getattr(h, sel, &attr); - - UPB_ASSERT(ok); - return attr.handler_data; -} - -/* Emit static assembly routines; code that does not vary based on the message - * schema. Since it's not input-dependent, we only need one single copy of it. - * For the moment we generate a single copy per generated handlers. Eventually - * we should generate this code at compile time and link it into the binary so - * we have one copy total. To do that we'll want to be sure that it is within - * 2GB of our JIT code, so that branches between the two are near (rel32). - * - * We'd put this assembly in a .s file directly, but DynASM's ability to - * calculate structure offsets automatically is too useful to pass up (it's way - * more convenient to write DECODER->sink than [rbx + 0x96], especially since - * the latter would have to be changed whenever the structure is updated). */ -static void emit_static_asm(jitcompiler *jc) { - /*| // Trampolines for entering/exiting the JIT. These are a bit tricky to */ - /*| // support full resuming; when we suspend we copy the JIT's portion of */ - /*| // the call stack into the upb_pbdecoder and restore it when we resume. */ - asmlabel(jc, "enterjit"); - /*|->enterjit: */ - /*|1: */ - /*| push rbp */ - /*| push r15 */ - /*| push r14 */ - /*| push r13 */ - /*| push r12 */ - /*| push rbx */ - /*| */ - /*| mov rbx, ARG2_64 // Preserve JIT method. */ - /*| */ - /*| mov DECODER, rdi */ - /*| callp upb_pbdecoder_resume // Same args as us; reuse regs. */ - /*| test eax, eax */ - /*| jns >1 */ - /*| mov DECODER->saved_rsp, rsp */ - /*| mov rax, rbx */ - /*| load_regs */ - /*| */ - /*| // Test whether we have a saved stack to resume. */ - /*| mov ARG3_64, DECODER->call_len */ - /*| test ARG3_64, ARG3_64 */ - /*| jnz >2 */ - /*| */ - /*| call rax */ - /*| */ - /*| mov rax, DECODER->size_param */ - /*| mov qword DECODER->call_len, 0 */ - /*|1: */ - /*| pop rbx */ - dasm_put(Dst, 2, (unsigned int)((uintptr_t)upb_pbdecoder_resume), (unsigned int)(((uintptr_t)upb_pbdecoder_resume)>>32), 0xfffffffffffffff0UL, Dt2(->saved_rsp), Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt1(->sink.closure), Dt1(->end_ofs), Dt2(->bufstart_ofs), Dt2(->buf), Dt2(->call_len), Dt2(->size_param), Dt2(->call_len)); -# 243 "upb/pb/compile_decoder_x64.dasc" - /*| pop r12 */ - /*| pop r13 */ - /*| pop r14 */ - /*| pop r15 */ - /*| pop rbp */ - /*| ret */ - /*| */ - /*|2: */ - /*| // Resume decoder. */ - /*| mov ARG2_64, DECODER->callstack */ - /*| sub rsp, ARG3_64 */ - /*| mov ARG1_64, rsp */ - /*| callp memcpy // Restore stack. */ - /*| ret // Return to resumed function (not ->enterjit caller). */ - /*| */ - /*| // Other code can call this to suspend the JIT. */ - /*| // To the calling code, it will appear that the function returns when */ - /*| // the JIT resumes, and more buffer space will be available. */ - /*| // Args: eax=the value that decode() should return. */ - dasm_put(Dst, 115, Dt2(->callstack), (unsigned int)((uintptr_t)memcpy), (unsigned int)(((uintptr_t)memcpy)>>32), 0xfffffffffffffff0UL); -# 262 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "exitjit"); - /*|->exitjit: */ - /*| // Save the stack into DECODER->callstack. */ - /*| mov ARG1_64, DECODER->callstack */ - /*| mov ARG2_64, rsp */ - /*| mov ARG3_64, DECODER->saved_rsp */ - /*| sub ARG3_64, rsp */ - /*| mov DECODER->call_len, ARG3_64 // Preserve len for next resume. */ - /*| mov ebx, eax // Preserve return value across memcpy. */ - /*| callp memcpy // Copy stack into decoder. */ - /*| mov eax, ebx // This will be our return value. */ - /*| */ - /*| // Must NOT do this before the memcpy(), otherwise memcpy() will */ - /*| // clobber the stack we are trying to save! */ - /*| mov rsp, DECODER->saved_rsp */ - /*| pop rbx */ - /*| pop r12 */ - /*| pop r13 */ - /*| pop r14 */ - /*| pop r15 */ - /*| pop rbp */ - /*| ret */ - /*| */ - /*| // Like suspend() in the C decoder, except that the function appears */ - /*| // (from the caller's perspective) not to return until the decoder is */ - /*| // resumed. */ - dasm_put(Dst, 161, Dt2(->callstack), Dt2(->saved_rsp), Dt2(->call_len), (unsigned int)((uintptr_t)memcpy), (unsigned int)(((uintptr_t)memcpy)>>32), 0xfffffffffffffff0UL, Dt2(->saved_rsp)); -# 288 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "suspend"); - /*|->suspend: */ - /*| cmp DECODER->ptr, PTR */ - /*| je >1 */ - /*| mov DECODER->checkpoint, PTR */ - /*|1: */ - /*| commit_regs */ - /*| mov rdi, DECODER */ - /*| callp upb_pbdecoder_suspend */ - /*| jmp ->exitjit */ - /*| */ - dasm_put(Dst, 222, Dt2(->ptr), Dt2(->checkpoint), Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt2(->delim_end), Dt2(->buf), Dt2(->bufstart_ofs), Dt1(->end_ofs), Dt1(->sink.closure), (unsigned int)((uintptr_t)upb_pbdecoder_suspend), (unsigned int)(((uintptr_t)upb_pbdecoder_suspend)>>32), 0xfffffffffffffff0UL); -# 299 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "pushlendelim"); - /*|->pushlendelim: */ - /*|1: */ - /*| mov FRAME->sink.closure, CLOSURE */ - /*| mov DECODER->checkpoint, PTR */ - /*| dv32 */ - dasm_put(Dst, 300, Dt1(->sink.closure), Dt2(->checkpoint)); - if (1 == 1) { - dasm_put(Dst, 313); - } else { - dasm_put(Dst, 321); - } -# 305 "upb/pb/compile_decoder_x64.dasc" - /*| mov rcx, DELIMEND */ - /*| sub rcx, PTR */ - /*| sub rcx, rdx */ - /*| jb >4 // Len is greater than enclosing message. */ - /*| mov FRAME->end_ofs, rcx */ - /*| cmp FRAME, DECODER->limit */ - /*| je >3 // Stack overflow */ - /*| add FRAME, sizeof(upb_pbdecoder_frame) */ - /*| mov DELIMEND, PTR */ - /*| add DELIMEND, rdx */ - /*| mov dword FRAME->groupnum, 0 */ - /*| test rcx, rcx */ - /*| jz >2 */ - /*| mov DATAEND, DECODER->end */ - /*| cmp PTR, DELIMEND */ - /*| ja >2 */ - /*| cmp DELIMEND, DATAEND */ - /*| ja >2 */ - /*| mov DATAEND, DELIMEND // If DELIMEND >= PTR && DELIMEND < DATAEND */ - dasm_put(Dst, 337, Dt1(->end_ofs), Dt2(->limit), sizeof(upb_pbdecoder_frame), Dt1(->groupnum), Dt2(->end)); -# 324 "upb/pb/compile_decoder_x64.dasc" - /*|2: */ - /*| ret */ - /*|3: */ - /*| // Stack overflow error. */ - /*| mov PTR, DECODER->checkpoint // Rollback to before the delim len. */ - /*| // Prepare seterr args. */ - /*| mov ARG1_64, DECODER */ - /*| ld64 kPbDecoderStackOverflow */ - dasm_put(Dst, 428, Dt2(->checkpoint)); - { - uintptr_t v = (uintptr_t)kPbDecoderStackOverflow; - if (v > 0xffffffff) { - dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); - } else if (v) { - dasm_put(Dst, 451, v); - } else { - dasm_put(Dst, 454); - } - } -# 332 "upb/pb/compile_decoder_x64.dasc" - /*| callp upb_pbdecoder_seterr */ - /*| call ->suspend */ - /*| jmp <1 */ - /*|4: */ - /*| // Overextended len. */ - /*| mov PTR, DECODER->checkpoint // Rollback to before the delim len. */ - /*| // Prepare seterr args. */ - /*| mov ARG1_64, DECODER */ - /*| ld64 kPbDecoderSubmessageTooLong */ - dasm_put(Dst, 458, (unsigned int)((uintptr_t)upb_pbdecoder_seterr), (unsigned int)(((uintptr_t)upb_pbdecoder_seterr)>>32), 0xfffffffffffffff0UL, Dt2(->checkpoint)); - { - uintptr_t v = (uintptr_t)kPbDecoderSubmessageTooLong; - if (v > 0xffffffff) { - dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); - } else if (v) { - dasm_put(Dst, 451, v); - } else { - dasm_put(Dst, 454); - } - } -# 341 "upb/pb/compile_decoder_x64.dasc" - /*| callp upb_pbdecoder_seterr */ - /*| call ->suspend */ - /*| jmp <1 */ - /*| */ - /*| // For getting a value that spans a buffer seam. Falls back to C. */ - /*|.macro getvalue_slow, func, bytes */ - /*| sub rsp, 8 // Need stack space for func to write value to. */ - /*|1: */ - /*| mov qword [rsp], 0 // For parsing routines that only parse 32 bits. */ - /*| mov ARG1_64, DECODER */ - /*| mov ARG2_64, rsp */ - /*| mov DECODER->checkpoint, PTR */ - /*| commit_regs */ - /*| callp func */ - /*| load_regs */ - /*| test eax, eax */ - /*| jns >2 */ - /*| // Success; return parsed data (in rdx AND xmm0). */ - /*| mov rdx, [rsp] */ - /*| movsd xmm0, qword [rsp] */ - /*| add rsp, 8 */ - /*| sub PTR, bytes // Bias our buffer pointer to rejoin the fast-path. */ - /*| mov DECODER->ptr, PTR */ - /*| ret */ - /*|2: */ - /*| call ->exitjit // Return eax from decode function. */ - /*| jmp <1 */ - /*|.endmacro */ - /*| */ - dasm_put(Dst, 497, (unsigned int)((uintptr_t)upb_pbdecoder_seterr), (unsigned int)(((uintptr_t)upb_pbdecoder_seterr)>>32), 0xfffffffffffffff0UL); -# 370 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "parse_unknown"); - /*| // Args: edx=fieldnum, cl=wire type */ - /*|->parse_unknown: */ - /*| // OPT: handle directly instead of kicking to C. */ - /*| // Check for ENDGROUP. */ - /*| mov ARG1_64, DECODER */ - /*| mov ARG2_32, edx */ - /*| movzx ARG3_32, cl */ - /*| commit_regs */ - /*| callp upb_pbdecoder_skipunknown */ - /*| load_regs */ - /*| cmp eax, DECODE_ENDGROUP */ - /*| jne >1 */ - dasm_put(Dst, 526, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt2(->delim_end), Dt2(->buf), Dt2(->bufstart_ofs), Dt1(->end_ofs), Dt1(->sink.closure), (unsigned int)((uintptr_t)upb_pbdecoder_skipunknown), (unsigned int)(((uintptr_t)upb_pbdecoder_skipunknown)>>32), 0xfffffffffffffff0UL, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt1(->sink.closure), Dt1(->end_ofs), Dt2(->bufstart_ofs), Dt2(->buf), DECODE_ENDGROUP); -# 383 "upb/pb/compile_decoder_x64.dasc" - /*| ret // Return eax=DECODE_ENDGROUP, not zero */ - /*|1: */ - /*| cmp eax, DECODE_OK */ - /*| je >1 */ - /*| call ->exitjit // Return eax from decode function. */ - /*|1: */ - /*| xor eax, eax */ - /*| ret */ - /*| */ - /*| // Fallback functions for parsing single values. These are used when the */ - /*| // buffer doesn't contain enough remaining data for the fast path. Each */ - /*| // primitive type (v32, v64, f32, f64) has two functions: decode & skip. */ - /*| // Decode functions return their value in rsi/esi. */ - /*| // */ - /*| // These functions leave PTR = value_end - fast_path_bytes, so that we can */ - /*| // re-join the fast path which will add fast_path_bytes after the callback */ - /*| // completes. We also set DECODER->ptr to this value which is a signal to */ - /*| // ->suspend that DECODER->checkpoint is up to date. */ - dasm_put(Dst, 623, DECODE_OK); -# 401 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "skip_decode_f32_fallback"); - /*|->skipf32_fallback: */ - /*|->decodef32_fallback: */ - /*| getvalue_slow upb_pbdecoder_decode_f32, 4 */ - dasm_put(Dst, 647, Dt2(->checkpoint), Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt2(->delim_end), Dt2(->buf), Dt2(->bufstart_ofs), Dt1(->end_ofs), Dt1(->sink.closure), (unsigned int)((uintptr_t)upb_pbdecoder_decode_f32), (unsigned int)(((uintptr_t)upb_pbdecoder_decode_f32)>>32), 0xfffffffffffffff0UL, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt1(->sink.closure), Dt1(->end_ofs)); -# 405 "upb/pb/compile_decoder_x64.dasc" - /*| */ - dasm_put(Dst, 751, Dt2(->bufstart_ofs), Dt2(->buf), Dt2(->ptr)); -# 406 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "skip_decode_f64_fallback"); - /*|->skipf64_fallback: */ - /*|->decodef64_fallback: */ - /*| getvalue_slow upb_pbdecoder_decode_f64, 8 */ - dasm_put(Dst, 799, Dt2(->checkpoint), Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt2(->delim_end), Dt2(->buf), Dt2(->bufstart_ofs), Dt1(->end_ofs), Dt1(->sink.closure), (unsigned int)((uintptr_t)upb_pbdecoder_decode_f64), (unsigned int)(((uintptr_t)upb_pbdecoder_decode_f64)>>32), 0xfffffffffffffff0UL, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt1(->sink.closure), Dt1(->end_ofs)); -# 410 "upb/pb/compile_decoder_x64.dasc" - /*| */ - /*| // Called for varint >= 1 byte. */ - dasm_put(Dst, 903, Dt2(->bufstart_ofs), Dt2(->buf), Dt2(->ptr)); -# 412 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "skip_decode_v32_fallback"); - /*|->skipv32_fallback: */ - /*|->skipv64_fallback: */ - /*| chkeob 16, >1 */ - dasm_put(Dst, 951); - if (16 == 1) { - dasm_put(Dst, 956); - } else { - dasm_put(Dst, 964); - } -# 416 "upb/pb/compile_decoder_x64.dasc" - /*| // With at least 16 bytes left, we can do a branch-less SSE version. */ - /*| movdqu xmm0, [PTR] */ - /*| pmovmskb eax, xmm0 // bits 0-15 are continuation bits, 16-31 are 0. */ - /*| not eax */ - /*| bsf eax, eax */ - /*| cmp al, 10 */ - /*| jae ->decode_varint_slow // Error (>10 byte varint). */ - /*| add PTR, rax // bsf result is 0-based, so PTR=end-1, as desired. */ - /*| ret */ - /*| */ - /*|1: */ - /*| // With fewer than 16 bytes, we have to read byte by byte. */ - /*| lea rcx, [PTR + 10] */ - /*| mov rax, PTR // Preserve PTR in case of fallback to slow path. */ - /*| cmp rcx, DATAEND */ - /*| cmova rcx, DATAEND // rcx = MIN(DATAEND, PTR + 10) */ - /*|2: */ - /*| cmp rax, rcx */ - /*| je ->decode_varint_slow */ - /*| test byte [rax], 0x80 */ - /*| jz >3 */ - /*| add rax, 1 */ - /*| jmp <2 */ - /*|3: */ - /*| mov PTR, rax // PTR = varint_end - 1, as desired */ - /*| ret */ - /*| */ - /*| // Returns tag in edx */ - dasm_put(Dst, 980, 10); -# 444 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "decode_unknown_tag_fallback"); - /*|->decode_unknown_tag_fallback: */ - /*| sub rsp, 16 */ - /*|1: */ - /*| cmp PTR, DELIMEND */ - /*| jne >2 */ - /*| add rsp, 16 */ - /*| xor eax, eax */ - /*| ret */ - /*|2: */ - /*| // OPT: Have a medium-fast path before falling back to _slow. */ - /*| mov ARG1_64, DECODER */ - /*| mov ARG2_64, rsp */ - /*| commit_regs */ - /*| callp upb_pbdecoder_decode_varint_slow */ - /*| load_regs */ - dasm_put(Dst, 1053, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt2(->delim_end), Dt2(->buf), Dt2(->bufstart_ofs), Dt1(->end_ofs), Dt1(->sink.closure), (unsigned int)((uintptr_t)upb_pbdecoder_decode_varint_slow), (unsigned int)(((uintptr_t)upb_pbdecoder_decode_varint_slow)>>32), 0xfffffffffffffff0UL, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt1(->sink.closure)); -# 460 "upb/pb/compile_decoder_x64.dasc" - /*| cmp eax, 0 */ - /*| jge >3 */ - /*| mov edx, [rsp] // Success; return parsed data. */ - /*| add rsp, 16 */ - /*| ret */ - /*|3: */ - /*| call ->exitjit // Return eax from decode function. */ - /*| jmp <1 */ - /*| */ - /*| // Called for varint >= 1 byte. */ - dasm_put(Dst, 1156, Dt1(->end_ofs), Dt2(->bufstart_ofs), Dt2(->buf)); -# 470 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "decode_v32_v64_fallback"); - /*|->decodev32_fallback: */ - /*|->decodev64_fallback: */ - /*| chkeob 10, ->decode_varint_slow */ - dasm_put(Dst, 1194); - if (10 == 1) { - dasm_put(Dst, 1199); - } else { - dasm_put(Dst, 1207); - } -# 474 "upb/pb/compile_decoder_x64.dasc" - /*| // OPT: do something faster than just calling the C version. */ - /*| mov rdi, PTR */ - /*| callp upb_vdecode_fast */ - /*| test rax, rax */ - /*| je ->decode_varint_slow // Unterminated varint. */ - /*| mov PTR, rax */ - /*| sub PTR, 1 */ - /*| mov DECODER->ptr, PTR */ - /*| ret */ - /*| */ - dasm_put(Dst, 1223, (unsigned int)((uintptr_t)upb_vdecode_fast), (unsigned int)(((uintptr_t)upb_vdecode_fast)>>32), 0xfffffffffffffff0UL, Dt2(->ptr)); -# 484 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "decode_varint_slow"); - /*|->decode_varint_slow: */ - /*| // Slow path: end of buffer or error (varint length >= 10). */ - /*| getvalue_slow upb_pbdecoder_decode_varint_slow, 1 */ - dasm_put(Dst, 1268, Dt2(->checkpoint), Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt2(->delim_end), Dt2(->buf), Dt2(->bufstart_ofs), Dt1(->end_ofs), Dt1(->sink.closure), (unsigned int)((uintptr_t)upb_pbdecoder_decode_varint_slow), (unsigned int)(((uintptr_t)upb_pbdecoder_decode_varint_slow)>>32), 0xfffffffffffffff0UL, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt1(->sink.closure), Dt1(->end_ofs), Dt2(->bufstart_ofs)); -# 488 "upb/pb/compile_decoder_x64.dasc" - /*| */ - /*| // Args: rsi=expected tag, return=rax (DECODE_{OK,MISMATCH}) */ - dasm_put(Dst, 1374, Dt2(->buf), Dt2(->ptr)); -# 490 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "checktag_fallback"); - /*|->checktag_fallback: */ - /*| sub rsp, 8 */ - /*| mov [rsp], rsi // Preserve expected tag. */ - /*|1: */ - /*| mov ARG1_64, DECODER */ - /*| commit_regs */ - /*| mov DECODER->checkpoint, PTR */ - /*| callp upb_pbdecoder_checktag_slow */ - /*| load_regs */ - dasm_put(Dst, 1418, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt2(->delim_end), Dt2(->buf), Dt2(->bufstart_ofs), Dt1(->end_ofs), Dt1(->sink.closure), Dt2(->checkpoint), (unsigned int)((uintptr_t)upb_pbdecoder_checktag_slow), (unsigned int)(((uintptr_t)upb_pbdecoder_checktag_slow)>>32), 0xfffffffffffffff0UL, Dt2(->top), Dt2(->ptr), Dt2(->data_end), Dt1(->sink.closure), Dt1(->end_ofs), Dt2(->bufstart_ofs)); -# 500 "upb/pb/compile_decoder_x64.dasc" - /*| cmp eax, 0 */ - /*| jge >2 */ - /*| add rsp, 8 */ - /*| ret */ - /*|2: */ - /*| call ->exitjit */ - /*| mov rsi, [rsp] */ - /*| cmp PTR, DELIMEND */ - /*| jne <1 */ - /*| mov eax, DECODE_EOF */ - /*| add rsp, 8 */ - /*| ret */ - /*| */ - /*| // Args: rsi=upb_inttable, rdx=key, return=rax (-1 if not found). */ - /*| // Preserves: rcx, rdx */ - /*| // OPT: Could write this in assembly if it's a hotspot. */ - dasm_put(Dst, 1517, Dt2(->buf), DECODE_EOF); -# 516 "upb/pb/compile_decoder_x64.dasc" - asmlabel(jc, "hashlookup"); - /*|->hashlookup: */ - /*| push rcx */ - /*| push rdx */ - /*| sub rsp, 16 */ - /*| mov rdi, rsi */ - /*| mov rsi, rdx */ - /*| mov rdx, rsp */ - /*| callp upb_inttable_lookup */ - /*| add rsp, 16 */ - /*| pop rdx */ - /*| pop rcx */ - /*| test al, al */ - /*| jz >2 // Unknown field. */ - /*| mov rax, [rsp-32] // Value from table. */ - /*| ret */ - /*|2: */ - /*| xor rax, rax */ - /*| not rax */ - /*| ret */ - dasm_put(Dst, 1559, (unsigned int)((uintptr_t)upb_inttable_lookup), (unsigned int)(((uintptr_t)upb_inttable_lookup)>>32), 0xfffffffffffffff0UL); -# 536 "upb/pb/compile_decoder_x64.dasc" -} - -static void jitprimitive(jitcompiler *jc, opcode op, - const upb_handlers *h, upb_selector_t sel) { - typedef enum { V32, V64, F32, F64, X } valtype_t; - static valtype_t types[] = { - X, F64, F32, V64, V64, V32, F64, F32, V64, X, X, X, X, V32, V32, F32, F64, - V32, V64 }; - static char fastpath_bytes[] = { 1, 1, 4, 8 }; - const valtype_t vtype = types[op]; - const int fastbytes = fastpath_bytes[vtype]; - upb_func *handler = gethandler(h, sel); - upb_fieldtype_t ftype; - size_t offset; - int32_t hasbit; - - if (handler) { - /*|1: */ - /*| chkneob fastbytes, >3 */ - dasm_put(Dst, 112); - if (fastbytes == 1) { - dasm_put(Dst, 1628); - } else { - dasm_put(Dst, 1636, fastbytes); - } -# 555 "upb/pb/compile_decoder_x64.dasc" - /*|2: */ - dasm_put(Dst, 1652); -# 556 "upb/pb/compile_decoder_x64.dasc" - switch (vtype) { - case V32: - /*| call ->decodev32_fallback */ - dasm_put(Dst, 1655); -# 559 "upb/pb/compile_decoder_x64.dasc" - break; - case V64: - /*| call ->decodev64_fallback */ - dasm_put(Dst, 1659); -# 562 "upb/pb/compile_decoder_x64.dasc" - break; - case F32: - /*| call ->decodef32_fallback */ - dasm_put(Dst, 1663); -# 565 "upb/pb/compile_decoder_x64.dasc" - break; - case F64: - /*| call ->decodef64_fallback */ - dasm_put(Dst, 1667); -# 568 "upb/pb/compile_decoder_x64.dasc" - break; - case X: break; - } - /*| jmp >4 */ - dasm_put(Dst, 1671); -# 572 "upb/pb/compile_decoder_x64.dasc" - - /* Fast path decode; for when check_bytes bytes are available. */ - /*|3: */ - dasm_put(Dst, 1676); -# 575 "upb/pb/compile_decoder_x64.dasc" - switch (op) { - case OP_PARSE_SFIXED32: - case OP_PARSE_FIXED32: - /*| mov edx, dword [PTR] */ - dasm_put(Dst, 1679); -# 579 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_PARSE_SFIXED64: - case OP_PARSE_FIXED64: - /*| mov rdx, qword [PTR] */ - dasm_put(Dst, 1682); -# 583 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_PARSE_FLOAT: - /*| movss xmm0, dword [PTR] */ - dasm_put(Dst, 1686); -# 586 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_PARSE_DOUBLE: - /*| movsd xmm0, qword [PTR] */ - dasm_put(Dst, 1692); -# 589 "upb/pb/compile_decoder_x64.dasc" - break; - default: - /* Inline one byte of varint decoding. */ - /*| movzx edx, byte [PTR] */ - /*| test dl, dl */ - /*| js <2 // Fallback to slow path for >1 byte varint. */ - dasm_put(Dst, 1698); -# 595 "upb/pb/compile_decoder_x64.dasc" - break; - } - - /* Second-stage decode; used for both fast and slow paths */ - /* (only needed for a few types). */ - /*|4: */ - dasm_put(Dst, 1708); -# 601 "upb/pb/compile_decoder_x64.dasc" - switch (op) { - case OP_PARSE_SINT32: - /* 32-bit zig-zag decode. */ - /*| mov eax, edx */ - /*| shr edx, 1 */ - /*| and eax, 1 */ - /*| neg eax */ - /*| xor edx, eax */ - dasm_put(Dst, 1711); -# 609 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_PARSE_SINT64: - /* 64-bit zig-zag decode. */ - /*| mov rax, rdx */ - /*| shr rdx, 1 */ - /*| and rax, 1 */ - /*| neg rax */ - /*| xor rdx, rax */ - dasm_put(Dst, 1725); -# 617 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_PARSE_BOOL: - /*| test rdx, rdx */ - /*| setne dl */ - dasm_put(Dst, 1744); -# 621 "upb/pb/compile_decoder_x64.dasc" - break; - default: break; - } - - /* Call callback (or specialize if we can). */ - if (upb_msg_getscalarhandlerdata(h, sel, &ftype, &offset, &hasbit)) { - switch (ftype) { - case UPB_TYPE_INT64: - case UPB_TYPE_UINT64: - /*| mov [CLOSURE + offset], rdx */ - dasm_put(Dst, 1751, offset); -# 631 "upb/pb/compile_decoder_x64.dasc" - break; - case UPB_TYPE_INT32: - case UPB_TYPE_UINT32: - case UPB_TYPE_ENUM: - /*| mov [CLOSURE + offset], edx */ - dasm_put(Dst, 1756, offset); -# 636 "upb/pb/compile_decoder_x64.dasc" - break; - case UPB_TYPE_DOUBLE: - /*| movsd qword [CLOSURE + offset], XMMARG1 */ - dasm_put(Dst, 1761, offset); -# 639 "upb/pb/compile_decoder_x64.dasc" - break; - case UPB_TYPE_FLOAT: - /*| movss dword [CLOSURE + offset], XMMARG1 */ - dasm_put(Dst, 1769, offset); -# 642 "upb/pb/compile_decoder_x64.dasc" - break; - case UPB_TYPE_BOOL: - /*| mov [CLOSURE + offset], dl */ - dasm_put(Dst, 1777, offset); -# 645 "upb/pb/compile_decoder_x64.dasc" - break; - case UPB_TYPE_STRING: - case UPB_TYPE_BYTES: - case UPB_TYPE_MESSAGE: - UPB_ASSERT(false); break; - } - /*| sethas CLOSURE, hasbit */ - if (hasbit >= 0) { - dasm_put(Dst, 1782, ((uint32_t)hasbit / 8), (1 << ((uint32_t)hasbit % 8))); - } -# 652 "upb/pb/compile_decoder_x64.dasc" - } else if (handler) { - /*| mov ARG1_64, CLOSURE */ - /*| load_handler_data h, sel */ - dasm_put(Dst, 1788); - { - uintptr_t v = (uintptr_t)gethandlerdata(h, sel); - if (v > 0xffffffff) { - dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); - } else if (v) { - dasm_put(Dst, 451, v); - } else { - dasm_put(Dst, 454); - } - } -# 655 "upb/pb/compile_decoder_x64.dasc" - /*| callp handler */ - dasm_put(Dst, 1793, (unsigned int)((uintptr_t)handler), (unsigned int)(((uintptr_t)handler)>>32), 0xfffffffffffffff0UL); -# 656 "upb/pb/compile_decoder_x64.dasc" - if (!alwaysok(h, sel)) { - /*| test al, al */ - /*| jnz >5 */ - /*| call ->suspend */ - /*| jmp <1 */ - /*|5: */ - dasm_put(Dst, 1815); -# 662 "upb/pb/compile_decoder_x64.dasc" - } - } - - /* We do this last so that the checkpoint is not advanced past the user's - * data until the callback has returned success. */ - /*| add PTR, fastbytes */ - dasm_put(Dst, 1831, fastbytes); -# 668 "upb/pb/compile_decoder_x64.dasc" - } else { - /* No handler registered for this value, just skip it. */ - /*| chkneob fastbytes, >3 */ - if (fastbytes == 1) { - dasm_put(Dst, 1628); - } else { - dasm_put(Dst, 1636, fastbytes); - } -# 671 "upb/pb/compile_decoder_x64.dasc" - /*|2: */ - dasm_put(Dst, 1652); -# 672 "upb/pb/compile_decoder_x64.dasc" - switch (vtype) { - case V32: - /*| call ->skipv32_fallback */ - dasm_put(Dst, 1836); -# 675 "upb/pb/compile_decoder_x64.dasc" - break; - case V64: - /*| call ->skipv64_fallback */ - dasm_put(Dst, 1840); -# 678 "upb/pb/compile_decoder_x64.dasc" - break; - case F32: - /*| call ->skipf32_fallback */ - dasm_put(Dst, 1844); -# 681 "upb/pb/compile_decoder_x64.dasc" - break; - case F64: - /*| call ->skipf64_fallback */ - dasm_put(Dst, 1848); -# 684 "upb/pb/compile_decoder_x64.dasc" - break; - case X: break; - } - - /* Fast-path skip. */ - /*|3: */ - dasm_put(Dst, 1676); -# 690 "upb/pb/compile_decoder_x64.dasc" - if (vtype == V32 || vtype == V64) { - /*| test byte [PTR], 0x80 */ - /*| jnz <2 */ - dasm_put(Dst, 1852); -# 693 "upb/pb/compile_decoder_x64.dasc" - } - /*| add PTR, fastbytes */ - dasm_put(Dst, 1831, fastbytes); -# 695 "upb/pb/compile_decoder_x64.dasc" - } -} - -static void jitdispatch(jitcompiler *jc, - const upb_pbdecodermethod *method) { - /* Lots of room for tweaking/optimization here. */ - - const upb_inttable *dispatch = &method->dispatch; - bool has_hash_entries = (dispatch->t.count > 0); - - /* Whether any of the fields for this message can have two wire types which - * are both valid (packed & non-packed). - * - * OPT: populate this more precisely; not all messages with hash entries have - * this characteristic. */ - bool has_multi_wiretype = has_hash_entries; - - /*|=>define_jmptarget(jc, &method->dispatch): */ - /*|1: */ - dasm_put(Dst, 1861, define_jmptarget(jc, &method->dispatch)); -# 714 "upb/pb/compile_decoder_x64.dasc" - /* Decode the field tag. */ - /*| mov aword DECODER->checkpoint, PTR */ - /*| chkeob 2, >6 */ - dasm_put(Dst, 308, Dt2(->checkpoint)); - if (2 == 1) { - dasm_put(Dst, 1865); - } else { - dasm_put(Dst, 1873); - } -# 717 "upb/pb/compile_decoder_x64.dasc" - /*| movzx edx, byte [PTR] */ - /*| test dl, dl */ - /*| jns >7 // Jump if first byte has no continuation bit. */ - /*| movzx ecx, byte [PTR + 1] */ - /*| test cl, cl */ - /*| js >6 // Jump if second byte has continuation bit. */ - /*| // Confirmed two-byte varint. */ - /*| shl ecx, 7 */ - /*| and edx, 0x7f */ - /*| or edx, ecx */ - /*| add PTR, 2 */ - /*| jmp >8 */ - /*|6: */ - /*| call ->decode_unknown_tag_fallback */ - /*| test eax, eax // Hit DELIMEND? */ - /*| jnz >8 */ - /*| ret */ - /*|7: */ - /*| add PTR, 1 */ - /*|8: */ - /*| mov ecx, edx */ - /*| shr edx, 3 */ - /*| and cl, 7 */ - dasm_put(Dst, 1889, 1); -# 740 "upb/pb/compile_decoder_x64.dasc" - - /* See comment attached to upb_pbdecodermethod.dispatch for layout of the - * dispatch table. */ - /*|2: */ - /*| cmp edx, dispatch->array_size */ - dasm_put(Dst, 1954, dispatch->array_size); -# 745 "upb/pb/compile_decoder_x64.dasc" - if (has_hash_entries) { - /*| jae >7 */ - dasm_put(Dst, 1961); -# 747 "upb/pb/compile_decoder_x64.dasc" - } else { - /*| jae >5 */ - dasm_put(Dst, 1966); -# 749 "upb/pb/compile_decoder_x64.dasc" - } - /*| // OPT: Compact the lookup arr into 32-bit entries. */ - if ((uintptr_t)dispatch->array > 0x7fffffff) { - /*| mov64 rax, (uintptr_t)dispatch->array */ - /*| mov rax, qword [rax + rdx * 8] */ - dasm_put(Dst, 1971, (unsigned int)((uintptr_t)dispatch->array), (unsigned int)(((uintptr_t)dispatch->array)>>32)); -# 754 "upb/pb/compile_decoder_x64.dasc" - } else { - /*| mov rax, qword [rdx * 8 + dispatch->array] */ - dasm_put(Dst, 1980, dispatch->array); -# 756 "upb/pb/compile_decoder_x64.dasc" - } - /*|3: */ - /*| // We take advantage of the fact that non-present entries are stored */ - /*| // as -1, which will result in wire types that will never match. */ - /*| cmp al, cl */ - dasm_put(Dst, 1986); -# 761 "upb/pb/compile_decoder_x64.dasc" - if (has_multi_wiretype) { - /*| jne >6 */ - dasm_put(Dst, 1991); -# 763 "upb/pb/compile_decoder_x64.dasc" - } else { - /*| jne >5 */ - dasm_put(Dst, 1996); -# 765 "upb/pb/compile_decoder_x64.dasc" - } - /*| shr rax, 16 */ - /*| */ - /*| // Load the machine code address from the table entry. */ - /*| // The table entry is relative to the dispatch->array jmptarget */ - /*| // (patchdispatch() took care of this) which is the same as */ - /*| // local label "4". The "lea" is really just trying to do */ - /*| // lea rax, [>4 + rax] */ - /*| // */ - /*| // But we can't write that directly for some reason, so we use */ - /*| // rdx as a temporary. */ - /*| lea rdx, [>4] */ - /*|=>define_jmptarget(jc, dispatch->array): */ - /*|4: */ - /*| add rax, rdx */ - /*| ret */ - /*| */ - /*|5: */ - /*| // Field isn't in our table. */ - /*| */ - /*| // For pushing unknown fields to the unknown field handler. */ - /*| mov64 rax, (uintptr_t)method->dest_handlers_ */ - /*| mov FRAME->sink.handlers, rax */ - /*| */ - /*| call ->parse_unknown */ - /*| test eax, eax // ENDGROUP? */ - /*| jz <1 */ - /*| lea rax, [>9] // ENDGROUP; Load address of OP_ENDMSG. */ - /*| ret */ - dasm_put(Dst, 2001, define_jmptarget(jc, dispatch->array), (unsigned int)((uintptr_t)method->dest_handlers_), (unsigned int)(((uintptr_t)method->dest_handlers_)>>32), Dt1(->sink.handlers)); -# 794 "upb/pb/compile_decoder_x64.dasc" - - if (has_multi_wiretype) { - /*|6: */ - /*| // Primary wire type didn't match, check secondary wire type. */ - /*| cmp ah, cl */ - /*| jne <5 */ - /*| // Secondary wire type is a match, look up fn + UPB_MAX_FIELDNUMBER. */ - /*| add rdx, UPB_MAX_FIELDNUMBER */ - /*| // This key will never be in the array part, so do a hash lookup. */ - dasm_put(Dst, 2043, UPB_MAX_FIELDNUMBER); -# 803 "upb/pb/compile_decoder_x64.dasc" - UPB_ASSERT(has_hash_entries); - /*| ld64 dispatch */ - { - uintptr_t v = (uintptr_t)dispatch; - if (v > 0xffffffff) { - dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); - } else if (v) { - dasm_put(Dst, 451, v); - } else { - dasm_put(Dst, 454); - } - } -# 805 "upb/pb/compile_decoder_x64.dasc" - /*| jmp ->hashlookup // Tail call. */ - dasm_put(Dst, 2056); -# 806 "upb/pb/compile_decoder_x64.dasc" - } - - if (has_hash_entries) { - /*|7: */ - /*| // Hash table lookup. */ - /*| ld64 dispatch */ - dasm_put(Dst, 2061); - { - uintptr_t v = (uintptr_t)dispatch; - if (v > 0xffffffff) { - dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); - } else if (v) { - dasm_put(Dst, 451, v); - } else { - dasm_put(Dst, 454); - } - } -# 812 "upb/pb/compile_decoder_x64.dasc" - /*| call ->hashlookup */ - /*| jmp <3 */ - dasm_put(Dst, 2064); -# 814 "upb/pb/compile_decoder_x64.dasc" - } -} - -static void jittag(jitcompiler *jc, uint64_t tag, int n, int ofs, - const upb_pbdecodermethod *method) { - /* Internally we parse unknown fields; if this runs us into DELIMEND we jump - * to the corresponding DELIMEND target (either msg end or repeated field - * end), which we find from the OP_CHECKDELIM which must have necessarily - * preceded us. */ - uint32_t last_instruction = *(jc->pc - 2); - int last_arg = (int32_t)last_instruction >> 8; - uint32_t *delimend = (jc->pc - 1) + last_arg; - const size_t ptr_words = sizeof(void*) / sizeof(uint32_t); - - UPB_ASSERT((last_instruction & 0xff) == OP_CHECKDELIM); - - if (getop(*(jc->pc - 1)) == OP_TAGN) { - jc->pc += ptr_words; - } - - /*| chkneob n, >1 */ - if (n == 1) { - dasm_put(Dst, 2072); - } else { - dasm_put(Dst, 2080, n); - } -# 835 "upb/pb/compile_decoder_x64.dasc" - - /*| // OPT: this is way too much fallback code to put here. */ - /*| // Reduce and/or move to a separate section to make better icache usage. */ - /*| ld64 tag */ - { - uintptr_t v = (uintptr_t)tag; - if (v > 0xffffffff) { - dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); - } else if (v) { - dasm_put(Dst, 451, v); - } else { - dasm_put(Dst, 454); - } - } -# 839 "upb/pb/compile_decoder_x64.dasc" - /*| call ->checktag_fallback */ - /*| cmp eax, DECODE_MISMATCH */ - /*| je >3 */ - /*| cmp eax, DECODE_EOF */ - /*| je =>jmptarget(jc, delimend) */ - /*| jmp >5 */ - dasm_put(Dst, 2096, DECODE_MISMATCH, DECODE_EOF, jmptarget(jc, delimend)); -# 845 "upb/pb/compile_decoder_x64.dasc" - - /*|1: */ - dasm_put(Dst, 112); -# 847 "upb/pb/compile_decoder_x64.dasc" - switch (n) { - case 1: - /*| cmp byte [PTR], tag */ - dasm_put(Dst, 2119, tag); -# 850 "upb/pb/compile_decoder_x64.dasc" - break; - case 2: - /*| cmp word [PTR], tag */ - dasm_put(Dst, 2123, tag); -# 853 "upb/pb/compile_decoder_x64.dasc" - break; - case 3: - /*| // OPT: Slightly more efficient code, but depends on an extra byte. */ - /*| // mov eax, dword [PTR] */ - /*| // shl eax, 8 */ - /*| // cmp eax, tag << 8 */ - /*| cmp word [PTR], (tag & 0xffff) */ - /*| jne >2 */ - /*| cmp byte [PTR + 2], (tag >> 16) */ - /*|2: */ - dasm_put(Dst, 2128, (tag & 0xffff), 2, (tag >> 16)); -# 863 "upb/pb/compile_decoder_x64.dasc" - break; - case 4: - /*| cmp dword [PTR], tag */ - dasm_put(Dst, 2143, tag); -# 866 "upb/pb/compile_decoder_x64.dasc" - break; - case 5: - /*| cmp dword [PTR], (tag & 0xffffffff) */ - /*| jne >3 */ - /*| cmp byte [PTR + 4], (tag >> 32) */ - dasm_put(Dst, 2147, (tag & 0xffffffff), 4, (tag >> 32)); -# 871 "upb/pb/compile_decoder_x64.dasc" - } - /*| je >4 */ - /*|3: */ - dasm_put(Dst, 2159); -# 874 "upb/pb/compile_decoder_x64.dasc" - if (ofs == 0) { - /*| call =>jmptarget(jc, &method->dispatch) */ - /*| test rax, rax */ - /*| jz =>jmptarget(jc, delimend) */ - /*| jmp rax */ - dasm_put(Dst, 2166, jmptarget(jc, &method->dispatch), jmptarget(jc, delimend)); -# 879 "upb/pb/compile_decoder_x64.dasc" - } else { - /*| jmp =>jmptarget(jc, jc->pc + ofs) */ - dasm_put(Dst, 2178, jmptarget(jc, jc->pc + ofs)); -# 881 "upb/pb/compile_decoder_x64.dasc" - } - /*|4: */ - /*| add PTR, n */ - /*|5: */ - dasm_put(Dst, 2182, n); -# 885 "upb/pb/compile_decoder_x64.dasc" -} - -/* Compile the bytecode to x64. */ -static void jitbytecode(jitcompiler *jc) { - upb_pbdecodermethod *method = NULL; - const upb_handlers *h = NULL; - for (jc->pc = jc->group->bytecode; jc->pc < jc->group->bytecode_end; ) { - int32_t instr = *jc->pc; - opcode op = instr & 0xff; - uint32_t arg = instr >> 8; - int32_t longofs = arg; - - if (op != OP_SETDISPATCH) { - /* Skipped for SETDISPATCH because it defines its own asmlabel for the - * dispatch code it emits. */ - asmlabel(jc, "0x%lx.%s", pcofs(jc), upb_pbdecoder_getopname(op)); - - /* Skipped for SETDISPATCH because it should point at the function - * prologue, not the dispatch function that is emitted first. - * TODO: optimize this to only define pclabels that are actually used. */ - /*|=>define_jmptarget(jc, jc->pc): */ - dasm_put(Dst, 0, define_jmptarget(jc, jc->pc)); -# 906 "upb/pb/compile_decoder_x64.dasc" - } - - jc->pc++; - - switch (op) { - case OP_STARTMSG: { - upb_func *startmsg = gethandler(h, UPB_STARTMSG_SELECTOR); - if (startmsg) { - /* bool startmsg(void *closure, const void *hd) */ - /*|1: */ - /*| mov ARG1_64, CLOSURE */ - /*| load_handler_data h, UPB_STARTMSG_SELECTOR */ - dasm_put(Dst, 2191); - { - uintptr_t v = (uintptr_t)gethandlerdata(h, UPB_STARTMSG_SELECTOR); - if (v > 0xffffffff) { - dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); - } else if (v) { - dasm_put(Dst, 451, v); - } else { - dasm_put(Dst, 454); - } - } -# 918 "upb/pb/compile_decoder_x64.dasc" - /*| callp startmsg */ - dasm_put(Dst, 1793, (unsigned int)((uintptr_t)startmsg), (unsigned int)(((uintptr_t)startmsg)>>32), 0xfffffffffffffff0UL); -# 919 "upb/pb/compile_decoder_x64.dasc" - if (!alwaysok(h, UPB_STARTMSG_SELECTOR)) { - /*| test al, al */ - /*| jnz >2 */ - /*| call ->suspend */ - /*| jmp <1 */ - /*|2: */ - dasm_put(Dst, 2198); -# 925 "upb/pb/compile_decoder_x64.dasc" - } - } else { - /*| nop */ - dasm_put(Dst, 2214); -# 928 "upb/pb/compile_decoder_x64.dasc" - } - break; - } - case OP_ENDMSG: { - upb_func *endmsg = gethandler(h, UPB_ENDMSG_SELECTOR); - /*|9: */ - dasm_put(Dst, 2216); -# 934 "upb/pb/compile_decoder_x64.dasc" - if (endmsg) { - /* bool endmsg(void *closure, const void *hd, upb_status *status) */ - /*| mov ARG1_64, CLOSURE */ - /*| load_handler_data h, UPB_ENDMSG_SELECTOR */ - dasm_put(Dst, 1788); - { - uintptr_t v = (uintptr_t)gethandlerdata(h, UPB_ENDMSG_SELECTOR); - if (v > 0xffffffff) { - dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); - } else if (v) { - dasm_put(Dst, 451, v); - } else { - dasm_put(Dst, 454); - } - } -# 938 "upb/pb/compile_decoder_x64.dasc" - /*| mov ARG3_64, DECODER->status */ - /*| callp endmsg */ - dasm_put(Dst, 2219, Dt2(->status), (unsigned int)((uintptr_t)endmsg), (unsigned int)(((uintptr_t)endmsg)>>32), 0xfffffffffffffff0UL); -# 940 "upb/pb/compile_decoder_x64.dasc" - } - break; - } - case OP_SETDISPATCH: { - uint32_t *op_pc = jc->pc - 1; - const char *msgname; - upb_inttable *dispatch; - - /* Load info for new method. */ - memcpy(&dispatch, jc->pc, sizeof(void*)); - jc->pc += sizeof(void*) / sizeof(uint32_t); - /* The OP_SETDISPATCH bytecode contains a pointer that is - * &method->dispatch; we want to go backwards and recover method. */ - method = - (void*)((char*)dispatch - offsetof(upb_pbdecodermethod, dispatch)); - /* May be NULL, in which case no handlers for this message will be found. - * OPT: we should do better by completely skipping the message in this - * case instead of parsing it field by field. We should also do the skip - * in the containing message's code. */ - h = method->dest_handlers_; - msgname = upb_msgdef_fullname(upb_handlers_msgdef(h)); - - /* Emit dispatch code for new method. */ - asmlabel(jc, "0x%lx.dispatch.%s", pcofs(jc), msgname); - jitdispatch(jc, method); - - /* Emit function prologue for new method. */ - asmlabel(jc, "0x%lx.parse.%s", pcofs(jc), msgname); - /*|=>define_jmptarget(jc, op_pc): */ - /*|=>define_jmptarget(jc, method): */ - /*| sub rsp, 8 */ - dasm_put(Dst, 2245, define_jmptarget(jc, op_pc), define_jmptarget(jc, method)); -# 971 "upb/pb/compile_decoder_x64.dasc" - - break; - } - case OP_PARSE_DOUBLE: - case OP_PARSE_FLOAT: - case OP_PARSE_INT64: - case OP_PARSE_UINT64: - case OP_PARSE_INT32: - case OP_PARSE_FIXED64: - case OP_PARSE_FIXED32: - case OP_PARSE_BOOL: - case OP_PARSE_UINT32: - case OP_PARSE_SFIXED32: - case OP_PARSE_SFIXED64: - case OP_PARSE_SINT32: - case OP_PARSE_SINT64: - jitprimitive(jc, op, h, arg); - break; - case OP_STARTSEQ: - case OP_STARTSUBMSG: - case OP_STARTSTR: { - upb_func *start = gethandler(h, arg); - if (start) { - /* void *startseq(void *closure, const void *hd) - * void *startsubmsg(void *closure, const void *hd) - * void *startstr(void *closure, const void *hd, size_t size_hint) */ - /*|1: */ - /*| mov ARG1_64, CLOSURE */ - /*| load_handler_data h, arg */ - dasm_put(Dst, 2191); - { - uintptr_t v = (uintptr_t)gethandlerdata(h, arg); - if (v > 0xffffffff) { - dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); - } else if (v) { - dasm_put(Dst, 451, v); - } else { - dasm_put(Dst, 454); - } - } -# 1000 "upb/pb/compile_decoder_x64.dasc" - if (op == OP_STARTSTR) { - /*| mov ARG3_64, DELIMEND */ - /*| sub ARG3_64, PTR */ - dasm_put(Dst, 2253); -# 1003 "upb/pb/compile_decoder_x64.dasc" - } - /*| callp start */ - dasm_put(Dst, 1793, (unsigned int)((uintptr_t)start), (unsigned int)(((uintptr_t)start)>>32), 0xfffffffffffffff0UL); -# 1005 "upb/pb/compile_decoder_x64.dasc" - if (!alwaysok(h, arg)) { - /*| test rax, rax */ - /*| jnz >2 */ - /*| call ->suspend */ - /*| jmp <1 */ - /*|2: */ - dasm_put(Dst, 2261); -# 1011 "upb/pb/compile_decoder_x64.dasc" - } - /*| mov CLOSURE, rax */ - dasm_put(Dst, 2278); -# 1013 "upb/pb/compile_decoder_x64.dasc" - } else { - /* TODO: nop is only required because of asmlabel(). */ - /*| nop */ - dasm_put(Dst, 2214); -# 1016 "upb/pb/compile_decoder_x64.dasc" - } - break; - } - case OP_ENDSEQ: - case OP_ENDSUBMSG: - case OP_ENDSTR: { - upb_func *end = gethandler(h, arg); - if (end) { - /* bool endseq(void *closure, const void *hd) - * bool endsubmsg(void *closure, const void *hd) - * bool endstr(void *closure, const void *hd) */ - /*|1: */ - /*| mov ARG1_64, CLOSURE */ - /*| load_handler_data h, arg */ - dasm_put(Dst, 2191); - { - uintptr_t v = (uintptr_t)gethandlerdata(h, arg); - if (v > 0xffffffff) { - dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); - } else if (v) { - dasm_put(Dst, 451, v); - } else { - dasm_put(Dst, 454); - } - } -# 1030 "upb/pb/compile_decoder_x64.dasc" - /*| callp end */ - dasm_put(Dst, 1793, (unsigned int)((uintptr_t)end), (unsigned int)(((uintptr_t)end)>>32), 0xfffffffffffffff0UL); -# 1031 "upb/pb/compile_decoder_x64.dasc" - if (!alwaysok(h, arg)) { - /*| test al, al */ - /*| jnz >2 */ - /*| call ->suspend */ - /*| jmp <1 */ - /*|2: */ - dasm_put(Dst, 2198); -# 1037 "upb/pb/compile_decoder_x64.dasc" - } - } else { - /* TODO: nop is only required because of asmlabel(). */ - /*| nop */ - dasm_put(Dst, 2214); -# 1041 "upb/pb/compile_decoder_x64.dasc" - } - break; - } - case OP_STRING: { - upb_func *str = gethandler(h, arg); - /*| cmp PTR, DELIMEND */ - /*| je >4 */ - /*|1: */ - /*| cmp PTR, DATAEND */ - /*| jne >2 */ - /*| call ->suspend */ - /*| jmp <1 */ - /*|2: */ - dasm_put(Dst, 2282); -# 1054 "upb/pb/compile_decoder_x64.dasc" - if (str) { - /* size_t str(void *closure, const void *hd, const char *str, - * size_t n) */ - /*| mov ARG1_64, CLOSURE */ - /*| load_handler_data h, arg */ - dasm_put(Dst, 1788); - { - uintptr_t v = (uintptr_t)gethandlerdata(h, arg); - if (v > 0xffffffff) { - dasm_put(Dst, 446, (unsigned int)(v), (unsigned int)((v)>>32)); - } else if (v) { - dasm_put(Dst, 451, v); - } else { - dasm_put(Dst, 454); - } - } -# 1059 "upb/pb/compile_decoder_x64.dasc" - /*| mov ARG3_64, PTR */ - /*| mov ARG4_64, DATAEND */ - /*| sub ARG4_64, PTR */ - /*| mov ARG5_64, qword DECODER->handle */ - /*| callp str */ - /*| add PTR, rax */ - dasm_put(Dst, 2309, Dt2(->handle), (unsigned int)((uintptr_t)str), (unsigned int)(((uintptr_t)str)>>32), 0xfffffffffffffff0UL); -# 1065 "upb/pb/compile_decoder_x64.dasc" - if (!alwaysok(h, arg)) { - /*| cmp PTR, DATAEND */ - /*| je >3 */ - /*| call ->strret_fallback */ - /*|3: */ - dasm_put(Dst, 2347); -# 1070 "upb/pb/compile_decoder_x64.dasc" - } - } else { - /*| mov PTR, DATAEND */ - dasm_put(Dst, 2360); -# 1073 "upb/pb/compile_decoder_x64.dasc" - } - /*| cmp PTR, DELIMEND */ - /*| jne <1 */ - /*|4: */ - dasm_put(Dst, 2364); -# 1077 "upb/pb/compile_decoder_x64.dasc" - break; - } - case OP_PUSHTAGDELIM: - /*| mov FRAME->sink.closure, CLOSURE */ - /*| // This shouldn't need to be read, because tag-delimited fields */ - /*| // shouldn't have an OP_SETDELIM after them. But for the moment */ - /*| // non-packed repeated fields do OP_SETDELIM so they can share more */ - /*| // code with the packed code-path. If this is changed later, this */ - /*| // store can be removed. */ - /*| mov qword FRAME->end_ofs, 0 */ - /*| cmp FRAME, DECODER->limit */ - /*| je ->err */ - /*| add FRAME, sizeof(upb_pbdecoder_frame) */ - /*| mov dword FRAME->groupnum, arg */ - dasm_put(Dst, 2375, Dt1(->sink.closure), Dt1(->end_ofs), Dt2(->limit), sizeof(upb_pbdecoder_frame), Dt1(->groupnum), arg); -# 1091 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_PUSHLENDELIM: - /*| call ->pushlendelim */ - dasm_put(Dst, 2405); -# 1094 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_POP: - /*| sub FRAME, sizeof(upb_pbdecoder_frame) */ - /*| mov CLOSURE, FRAME->sink.closure */ - dasm_put(Dst, 2409, sizeof(upb_pbdecoder_frame), Dt1(->sink.closure)); -# 1098 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_SETDELIM: - /* OPT: experiment with testing vs old offset to optimize away. */ - /*| mov DATAEND, DECODER->end */ - /*| add DELIMEND, FRAME->end_ofs */ - /*| cmp DELIMEND, DECODER->buf */ - /*| jb >1 */ - /*| cmp DELIMEND, DATAEND */ - /*| ja >1 // OPT: try cmov. */ - /*| mov DATAEND, DELIMEND */ - /*|1: */ - dasm_put(Dst, 2419, Dt2(->end), Dt1(->end_ofs), Dt2(->buf)); -# 1109 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_SETBIGGROUPNUM: - /*| mov dword FRAME->groupnum, *jc->pc++ */ - dasm_put(Dst, 2399, Dt1(->groupnum), *jc->pc++); -# 1112 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_CHECKDELIM: - /*| cmp DELIMEND, PTR */ - /*| je =>jmptarget(jc, jc->pc + longofs) */ - dasm_put(Dst, 2449, jmptarget(jc, jc->pc + longofs)); -# 1116 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_CALL: - /*| call =>jmptarget(jc, jc->pc + longofs) */ - dasm_put(Dst, 2456, jmptarget(jc, jc->pc + longofs)); -# 1119 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_BRANCH: - /*| jmp =>jmptarget(jc, jc->pc + longofs); */ - dasm_put(Dst, 2178, jmptarget(jc, jc->pc + longofs)); -# 1122 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_RET: - /*|9: */ - /*| add rsp, 8 */ - /*| ret */ - dasm_put(Dst, 2459); -# 1127 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_TAG1: - jittag(jc, (arg >> 8) & 0xff, 1, (int8_t)arg, method); - break; - case OP_TAG2: - jittag(jc, (arg >> 8) & 0xffff, 2, (int8_t)arg, method); - break; - case OP_TAGN: { - uint64_t tag; - memcpy(&tag, jc->pc, 8); - jittag(jc, tag, arg >> 8, (int8_t)arg, method); - break; - } - case OP_DISPATCH: - /*| call =>jmptarget(jc, &method->dispatch) */ - dasm_put(Dst, 2456, jmptarget(jc, &method->dispatch)); -# 1142 "upb/pb/compile_decoder_x64.dasc" - break; - case OP_HALT: - UPB_ASSERT(false); - } - } - - asmlabel(jc, "eof"); - /*| nop */ - dasm_put(Dst, 2214); -# 1150 "upb/pb/compile_decoder_x64.dasc" -} -- cgit v1.2.3 From 01557462cc211cec9c7bddede77995b938067ea5 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 16 Jan 2019 18:50:46 -0800 Subject: upb_symtab_add() returns upb_filedef* instead of bool. --- tests/pb/test_encoder.cc | 6 +++--- upb/def.c | 8 ++++---- upb/def.h | 13 +++++++------ 3 files changed, 14 insertions(+), 13 deletions(-) (limited to 'upb') diff --git a/tests/pb/test_encoder.cc b/tests/pb/test_encoder.cc index 8b78ea8..f0b31c6 100644 --- a/tests/pb/test_encoder.cc +++ b/tests/pb/test_encoder.cc @@ -31,8 +31,8 @@ void test_pb_roundtrip() { google_protobuf_FileDescriptorSet_file(set, &n); ASSERT(n == 1); upb::Status status; - bool ok = symtab.AddFile(files[0], &status); - if (!ok) { + upb::FileDefPtr file_def = symtab.AddFile(files[0], &status); + if (!file_def) { fprintf(stderr, "Error building def: %s\n", status.error_message()); ASSERT(false); } @@ -49,7 +49,7 @@ void test_pb_roundtrip() { upb::pb::EncoderPtr::Create(&arena, encoder_handlers, string_sink.input()); upb::pb::DecoderPtr decoder = upb::pb::DecoderPtr::Create(&arena, method, encoder.input(), &status); - ok = upb::PutBuffer(input, decoder.input()); + bool ok = upb::PutBuffer(input, decoder.input()); ASSERT(ok); ASSERT(input == output); } diff --git a/upb/def.c b/upb/def.c index aedd88d..4a893d1 100644 --- a/upb/def.c +++ b/upb/def.c @@ -1649,9 +1649,9 @@ static bool upb_symtab_addtotabs(upb_symtab *s, symtab_addctx *ctx, return true; } -bool upb_symtab_addfile(upb_symtab *s, - const google_protobuf_FileDescriptorProto *file_proto, - upb_status *status) { +const upb_filedef *upb_symtab_addfile( + upb_symtab *s, const google_protobuf_FileDescriptorProto *file_proto, + upb_status *status) { upb_arena *tmparena = upb_arena_new(); upb_strtable addtab; upb_alloc *alloc = upb_arena_alloc(s->arena); @@ -1672,7 +1672,7 @@ bool upb_symtab_addfile(upb_symtab *s, upb_symtab_addtotabs(s, &ctx, status); upb_arena_free(tmparena); - return ok; + return ok ? file : NULL; } /* Include here since we want most of this file to be stdio-free. */ diff --git a/upb/def.h b/upb/def.h index 4fd8509..7675138 100644 --- a/upb/def.h +++ b/upb/def.h @@ -788,9 +788,9 @@ const upb_msgdef *upb_symtab_lookupmsg2( const upb_symtab *s, const char *sym, size_t len); const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym); int upb_symtab_filecount(const upb_symtab *s); -bool upb_symtab_addfile(upb_symtab *s, - const google_protobuf_FileDescriptorProto* file, - upb_status *status); +const upb_filedef *upb_symtab_addfile( + upb_symtab *s, const google_protobuf_FileDescriptorProto *file, + upb_status *status); /* For generated code only: loads a generated descriptor. */ typedef struct upb_def_init { @@ -826,9 +826,10 @@ class upb::SymbolTable { /* TODO: iteration? */ /* Adds the given serialized FileDescriptorProto to the pool. */ - bool AddFile(const google_protobuf_FileDescriptorProto *file_proto, - Status *status) { - return upb_symtab_addfile(ptr_.get(), file_proto, status->ptr()); + FileDefPtr AddFile(const google_protobuf_FileDescriptorProto *file_proto, + Status *status) { + return FileDefPtr( + upb_symtab_addfile(ptr_.get(), file_proto, status->ptr())); } private: -- cgit v1.2.3 From 8cd3b9dfa99764685727e065e43fb405ca5aa29f Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Thu, 17 Jan 2019 12:38:04 -0800 Subject: Moved some definitions to def.h --- upb/bindings/lua/def.c | 4 - upb/def.h | 31 +++++ upb/json/parser.c | 361 +++++++++---------------------------------------- upb/json/parser.rl | 20 +-- upb/json/printer.c | 5 +- upb/upb.h | 39 ------ 6 files changed, 106 insertions(+), 354 deletions(-) (limited to 'upb') diff --git a/upb/bindings/lua/def.c b/upb/bindings/lua/def.c index e16d426..3868179 100644 --- a/upb/bindings/lua/def.c +++ b/upb/bindings/lua/def.c @@ -728,10 +728,6 @@ void lupb_def_registertypes(lua_State *L) { lupb_setfieldi(L, "TYPE_UINT32", UPB_TYPE_UINT32); lupb_setfieldi(L, "TYPE_ENUM", UPB_TYPE_ENUM); - lupb_setfieldi(L, "INTFMT_VARIABLE", UPB_INTFMT_VARIABLE); - lupb_setfieldi(L, "INTFMT_FIXED", UPB_INTFMT_FIXED); - lupb_setfieldi(L, "INTFMT_ZIGZAG", UPB_INTFMT_ZIGZAG); - lupb_setfieldi(L, "DESCRIPTOR_TYPE_DOUBLE", UPB_DESCRIPTOR_TYPE_DOUBLE); lupb_setfieldi(L, "DESCRIPTOR_TYPE_FLOAT", UPB_DESCRIPTOR_TYPE_FLOAT); lupb_setfieldi(L, "DESCRIPTOR_TYPE_INT64", UPB_DESCRIPTOR_TYPE_INT64); diff --git a/upb/def.h b/upb/def.h index 7675138..2c336c5 100644 --- a/upb/def.h +++ b/upb/def.h @@ -50,6 +50,37 @@ typedef struct upb_oneofdef upb_oneofdef; struct upb_symtab; typedef struct upb_symtab upb_symtab; +typedef enum { + UPB_SYNTAX_PROTO2 = 2, + UPB_SYNTAX_PROTO3 = 3 +} upb_syntax_t; + +/* All the different kind of well known type messages. For simplicity of check, + * number wrappers and string wrappers are grouped together. Make sure the + * order and merber of these groups are not changed. + */ +typedef enum { + UPB_WELLKNOWN_UNSPECIFIED, + UPB_WELLKNOWN_ANY, + UPB_WELLKNOWN_FIELDMASK, + UPB_WELLKNOWN_DURATION, + UPB_WELLKNOWN_TIMESTAMP, + /* number wrappers */ + UPB_WELLKNOWN_DOUBLEVALUE, + UPB_WELLKNOWN_FLOATVALUE, + UPB_WELLKNOWN_INT64VALUE, + UPB_WELLKNOWN_UINT64VALUE, + UPB_WELLKNOWN_INT32VALUE, + UPB_WELLKNOWN_UINT32VALUE, + /* string wrappers */ + UPB_WELLKNOWN_STRINGVALUE, + UPB_WELLKNOWN_BYTESVALUE, + UPB_WELLKNOWN_BOOLVALUE, + UPB_WELLKNOWN_VALUE, + UPB_WELLKNOWN_LISTVALUE, + UPB_WELLKNOWN_STRUCT +} upb_wellknowntype_t; + /* upb_fielddef ***************************************************************/ /* Maximum field number allowed for FieldDefs. This is an inherent limit of the diff --git a/upb/json/parser.c b/upb/json/parser.c index 38a0535..0b9eb92 100644 --- a/upb/json/parser.c +++ b/upb/json/parser.c @@ -21,6 +21,7 @@ ** - handling of keys/escape-sequences/etc that span input buffers. */ +#include #include #include #include @@ -63,7 +64,6 @@ static bool is_string_wrapper_object(upb_json_parser *p); static bool does_string_wrapper_start(upb_json_parser *p); static bool does_string_wrapper_end(upb_json_parser *p); -static bool is_fieldmask_object(upb_json_parser *p); static bool does_fieldmask_start(upb_json_parser *p); static bool does_fieldmask_end(upb_json_parser *p); static void start_fieldmask_object(upb_json_parser *p); @@ -1332,13 +1332,8 @@ static bool end_stringval_nontop(upb_json_parser *p) { case UPB_TYPE_STRING: { upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); -<<<<<<< HEAD - p->top--; upb_sink_endstr(p->top->sink, sel); -======= - upb_sink_endstr(&p->top->sink, sel); p->top--; ->>>>>>> arrayapi break; } @@ -1675,9 +1670,7 @@ static void start_fieldmask_path_text(upb_json_parser *p, const char *ptr) { } static bool end_fieldmask_path_text(upb_json_parser *p, const char *ptr) { - if (!capture_end(p, ptr)) { - return false; - } + return capture_end(p, ptr); } static bool start_fieldmask_path(upb_json_parser *p) { @@ -1690,7 +1683,7 @@ static bool start_fieldmask_path(upb_json_parser *p) { * handler frames, and string events occur in a sub-frame. */ inner = p->top + 1; sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); - upb_sink_startstr(&p->top->sink, sel, 0, &inner->sink); + upb_sink_startstr(p->top->sink, sel, 0, &inner->sink); inner->m = p->top->m; inner->f = p->top->f; inner->name_table = NULL; @@ -1712,10 +1705,10 @@ static bool lower_camel_push( for (;ptr < limit; ptr++) { if (*ptr >= 'A' && *ptr <= 'Z' && !first) { char lower = tolower(*ptr); - upb_sink_putstring(&p->top->sink, sel, "_", 1, NULL); - upb_sink_putstring(&p->top->sink, sel, &lower, 1, NULL); + upb_sink_putstring(p->top->sink, sel, "_", 1, NULL); + upb_sink_putstring(p->top->sink, sel, &lower, 1, NULL); } else { - upb_sink_putstring(&p->top->sink, sel, ptr, 1, NULL); + upb_sink_putstring(p->top->sink, sel, ptr, 1, NULL); } first = false; } @@ -1732,7 +1725,7 @@ static bool end_fieldmask_path(upb_json_parser *p) { } sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); - upb_sink_endstr(&p->top->sink, sel); + upb_sink_endstr(p->top->sink, sel); p->top--; multipart_end(p); @@ -1796,11 +1789,7 @@ static bool parse_mapentry_key(upb_json_parser *p) { sel = getsel_for_handlertype(p, UPB_HANDLER_STRING); upb_sink_putstring(subsink, sel, buf, len, NULL); sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); -<<<<<<< HEAD - upb_sink_endstr(p->top->sink, sel); -======= - upb_sink_endstr(&subsink, sel); ->>>>>>> arrayapi + upb_sink_endstr(subsink, sel); multipart_end(p); break; } @@ -2498,10 +2487,6 @@ static bool does_fieldmask_end(upb_json_parser *p) { return p->top->m != NULL && is_fieldmask(p->top->m); } -static bool is_fieldmask_object(upb_json_parser *p) { - return p->top->m != NULL && is_fieldmask(p->top->m); -} - #define CHECK_RETURN_TOP(x) if (!(x)) goto error @@ -2523,19 +2508,11 @@ static bool is_fieldmask_object(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -<<<<<<< HEAD -#line 2542 "upb/json/parser.rl" +#line 2690 "upb/json/parser.rl" -#line 2388 "upb/json/parser.c" -======= -#line 2730 "upb/json/parser.rl" - - - -#line 2556 "upb/json/parser.c" ->>>>>>> arrayapi +#line 2516 "upb/json/parser.c" static const char _json_actions[] = { 0, 1, 0, 1, 1, 1, 3, 1, 4, 1, 6, 1, 7, 1, 8, 1, @@ -2787,11 +2764,7 @@ static const int json_en_value_machine = 78; static const int json_en_main = 1; -<<<<<<< HEAD -#line 2545 "upb/json/parser.rl" -======= -#line 2733 "upb/json/parser.rl" ->>>>>>> arrayapi +#line 2693 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -2814,11 +2787,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -<<<<<<< HEAD -#line 2658 "upb/json/parser.c" -======= -#line 2831 "upb/json/parser.c" ->>>>>>> arrayapi +#line 2791 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -2893,183 +2862,103 @@ _match: switch ( *_acts++ ) { case 1: -<<<<<<< HEAD -#line 2393 "upb/json/parser.rl" +#line 2521 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 2395 "upb/json/parser.rl" +#line 2523 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 23;goto _again;} } break; case 3: -#line 2399 "upb/json/parser.rl" +#line 2527 "upb/json/parser.rl" { start_text(parser, p); } break; case 4: -#line 2400 "upb/json/parser.rl" +#line 2528 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -#line 2406 "upb/json/parser.rl" +#line 2534 "upb/json/parser.rl" { start_hex(parser); } break; case 6: -#line 2407 "upb/json/parser.rl" +#line 2535 "upb/json/parser.rl" { hexdigit(parser, p); } break; case 7: -#line 2408 "upb/json/parser.rl" +#line 2536 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -#line 2414 "upb/json/parser.rl" +#line 2542 "upb/json/parser.rl" { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -#line 2420 "upb/json/parser.rl" +#line 2548 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 10: -#line 2432 "upb/json/parser.rl" +#line 2560 "upb/json/parser.rl" { start_duration_base(parser, p); } break; case 11: -#line 2433 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_duration_base(parser, p)); } - break; - case 12: -#line 2435 "upb/json/parser.rl" - { p--; {cs = stack[--top]; goto _again;} } - break; - case 13: -#line 2440 "upb/json/parser.rl" - { start_timestamp_base(parser, p); } - break; - case 14: -#line 2441 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_timestamp_base(parser, p)); } - break; - case 15: -#line 2443 "upb/json/parser.rl" - { start_timestamp_fraction(parser, p); } - break; - case 16: -#line 2444 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } - break; - case 17: -#line 2446 "upb/json/parser.rl" - { start_timestamp_zone(parser, p); } - break; - case 18: -#line 2447 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } - break; - case 19: -#line 2449 "upb/json/parser.rl" - { p--; {cs = stack[--top]; goto _again;} } - break; - case 20: -#line 2454 "upb/json/parser.rl" -======= #line 2561 "upb/json/parser.rl" - { p--; {cs = stack[--top]; goto _again;} } - break; - case 2: -#line 2563 "upb/json/parser.rl" - { p--; {stack[top++] = cs; cs = 23;goto _again;} } - break; - case 3: -#line 2567 "upb/json/parser.rl" - { start_text(parser, p); } - break; - case 4: -#line 2568 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_text(parser, p)); } - break; - case 5: -#line 2574 "upb/json/parser.rl" - { start_hex(parser); } - break; - case 6: -#line 2575 "upb/json/parser.rl" - { hexdigit(parser, p); } - break; - case 7: -#line 2576 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_hex(parser)); } - break; - case 8: -#line 2582 "upb/json/parser.rl" - { CHECK_RETURN_TOP(escape(parser, p)); } - break; - case 9: -#line 2588 "upb/json/parser.rl" - { p--; {cs = stack[--top]; goto _again;} } - break; - case 10: -#line 2600 "upb/json/parser.rl" - { start_duration_base(parser, p); } - break; - case 11: -#line 2601 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_duration_base(parser, p)); } break; case 12: -#line 2603 "upb/json/parser.rl" +#line 2563 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 13: -#line 2608 "upb/json/parser.rl" +#line 2568 "upb/json/parser.rl" { start_timestamp_base(parser, p); } break; case 14: -#line 2609 "upb/json/parser.rl" +#line 2569 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_base(parser, p)); } break; case 15: -#line 2611 "upb/json/parser.rl" +#line 2571 "upb/json/parser.rl" { start_timestamp_fraction(parser, p); } break; case 16: -#line 2612 "upb/json/parser.rl" +#line 2572 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } break; case 17: -#line 2614 "upb/json/parser.rl" +#line 2574 "upb/json/parser.rl" { start_timestamp_zone(parser, p); } break; case 18: -#line 2615 "upb/json/parser.rl" +#line 2575 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } break; case 19: -#line 2617 "upb/json/parser.rl" +#line 2577 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 20: -#line 2622 "upb/json/parser.rl" +#line 2582 "upb/json/parser.rl" { start_fieldmask_path_text(parser, p); } break; case 21: -#line 2623 "upb/json/parser.rl" +#line 2583 "upb/json/parser.rl" { end_fieldmask_path_text(parser, p); } break; case 22: -#line 2628 "upb/json/parser.rl" +#line 2588 "upb/json/parser.rl" { start_fieldmask_path(parser); } break; case 23: -#line 2629 "upb/json/parser.rl" +#line 2589 "upb/json/parser.rl" { end_fieldmask_path(parser); } break; case 24: -#line 2635 "upb/json/parser.rl" +#line 2595 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 25: -#line 2640 "upb/json/parser.rl" ->>>>>>> arrayapi +#line 2600 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) { {stack[top++] = cs; cs = 47;goto _again;} @@ -3082,21 +2971,12 @@ _match: } } break; -<<<<<<< HEAD - case 21: -#line 2465 "upb/json/parser.rl" - { p--; {stack[top++] = cs; cs = 75;goto _again;} } - break; - case 22: -#line 2470 "upb/json/parser.rl" -======= case 26: -#line 2653 "upb/json/parser.rl" +#line 2613 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 78;goto _again;} } break; case 27: -#line 2658 "upb/json/parser.rl" ->>>>>>> arrayapi +#line 2618 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_member(parser, p); @@ -3105,21 +2985,12 @@ _match: } } break; -<<<<<<< HEAD - case 23: -#line 2477 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_membername(parser)); } - break; - case 24: -#line 2480 "upb/json/parser.rl" -======= case 28: -#line 2665 "upb/json/parser.rl" +#line 2625 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_membername(parser)); } break; case 29: -#line 2668 "upb/json/parser.rl" ->>>>>>> arrayapi +#line 2628 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { end_any_member(parser, p); @@ -3128,13 +2999,8 @@ _match: } } break; -<<<<<<< HEAD - case 25: -#line 2491 "upb/json/parser.rl" -======= case 30: -#line 2679 "upb/json/parser.rl" ->>>>>>> arrayapi +#line 2639 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_object(parser, p); @@ -3143,13 +3009,8 @@ _match: } } break; -<<<<<<< HEAD - case 26: -#line 2500 "upb/json/parser.rl" -======= case 31: -#line 2688 "upb/json/parser.rl" ->>>>>>> arrayapi +#line 2648 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { CHECK_RETURN_TOP(end_any_object(parser, p)); @@ -3158,107 +3019,55 @@ _match: } } break; -<<<<<<< HEAD - case 27: -#line 2512 "upb/json/parser.rl" - { CHECK_RETURN_TOP(start_array(parser)); } - break; - case 28: -#line 2516 "upb/json/parser.rl" - { end_array(parser); } - break; - case 29: -#line 2521 "upb/json/parser.rl" - { CHECK_RETURN_TOP(start_number(parser, p)); } - break; - case 30: -#line 2522 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_number(parser, p)); } - break; - case 31: -#line 2524 "upb/json/parser.rl" - { CHECK_RETURN_TOP(start_stringval(parser)); } - break; - case 32: -#line 2525 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_stringval(parser)); } - break; - case 33: -#line 2527 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_bool(parser, true)); } - break; - case 34: -#line 2529 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_bool(parser, false)); } - break; - case 35: -#line 2531 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_null(parser)); } - break; - case 36: -#line 2533 "upb/json/parser.rl" - { CHECK_RETURN_TOP(start_subobject_full(parser)); } - break; - case 37: -#line 2534 "upb/json/parser.rl" - { end_subobject_full(parser); } - break; - case 38: -#line 2539 "upb/json/parser.rl" - { p--; {cs = stack[--top]; goto _again;} } - break; -#line 2916 "upb/json/parser.c" -======= case 32: -#line 2700 "upb/json/parser.rl" +#line 2660 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_array(parser)); } break; case 33: -#line 2704 "upb/json/parser.rl" +#line 2664 "upb/json/parser.rl" { end_array(parser); } break; case 34: -#line 2709 "upb/json/parser.rl" +#line 2669 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_number(parser, p)); } break; case 35: -#line 2710 "upb/json/parser.rl" +#line 2670 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 36: -#line 2712 "upb/json/parser.rl" +#line 2672 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_stringval(parser)); } break; case 37: -#line 2713 "upb/json/parser.rl" +#line 2673 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_stringval(parser)); } break; case 38: -#line 2715 "upb/json/parser.rl" +#line 2675 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 39: -#line 2717 "upb/json/parser.rl" +#line 2677 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 40: -#line 2719 "upb/json/parser.rl" +#line 2679 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 41: -#line 2721 "upb/json/parser.rl" +#line 2681 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_subobject_full(parser)); } break; case 42: -#line 2722 "upb/json/parser.rl" +#line 2682 "upb/json/parser.rl" { end_subobject_full(parser); } break; case 43: -#line 2727 "upb/json/parser.rl" +#line 2687 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 3111 "upb/json/parser.c" ->>>>>>> arrayapi +#line 3071 "upb/json/parser.c" } } @@ -3275,60 +3084,32 @@ _again: while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 0: -<<<<<<< HEAD -#line 2391 "upb/json/parser.rl" -======= -#line 2559 "upb/json/parser.rl" ->>>>>>> arrayapi +#line 2519 "upb/json/parser.rl" { p--; {cs = stack[--top]; if ( p == pe ) goto _test_eof; goto _again;} } break; -<<<<<<< HEAD - case 30: -#line 2522 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_number(parser, p)); } - break; - case 33: -#line 2527 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_bool(parser, true)); } - break; - case 34: -#line 2529 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_bool(parser, false)); } - break; - case 35: -#line 2531 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_null(parser)); } - break; - case 37: -#line 2534 "upb/json/parser.rl" - { end_subobject_full(parser); } - break; -#line 2958 "upb/json/parser.c" -======= case 35: -#line 2710 "upb/json/parser.rl" +#line 2670 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 38: -#line 2715 "upb/json/parser.rl" +#line 2675 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 39: -#line 2717 "upb/json/parser.rl" +#line 2677 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 40: -#line 2719 "upb/json/parser.rl" +#line 2679 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 42: -#line 2722 "upb/json/parser.rl" +#line 2682 "upb/json/parser.rl" { end_subobject_full(parser); } break; -#line 3153 "upb/json/parser.c" ->>>>>>> arrayapi +#line 3113 "upb/json/parser.c" } } } @@ -3336,11 +3117,7 @@ goto _again;} } _out: {} } -<<<<<<< HEAD -#line 2567 "upb/json/parser.rl" -======= -#line 2755 "upb/json/parser.rl" ->>>>>>> arrayapi +#line 2715 "upb/json/parser.rl" if (p != pe) { upb_status_seterrf(parser->status, "Parse error at '%.*s'\n", pe - p, p); @@ -3388,21 +3165,13 @@ static void json_parser_reset(upb_json_parser *p) { /* Emit Ragel initialization of the parser. */ -<<<<<<< HEAD -#line 3013 "upb/json/parser.c" -======= -#line 3210 "upb/json/parser.c" ->>>>>>> arrayapi +#line 3169 "upb/json/parser.c" { cs = json_start; top = 0; } -<<<<<<< HEAD -#line 2613 "upb/json/parser.rl" -======= -#line 2803 "upb/json/parser.rl" ->>>>>>> arrayapi +#line 2762 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); diff --git a/upb/json/parser.rl b/upb/json/parser.rl index f722d28..1ae896a 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -19,6 +19,7 @@ ** - handling of keys/escape-sequences/etc that span input buffers. */ +#include #include #include #include @@ -61,7 +62,6 @@ static bool is_string_wrapper_object(upb_json_parser *p); static bool does_string_wrapper_start(upb_json_parser *p); static bool does_string_wrapper_end(upb_json_parser *p); -static bool is_fieldmask_object(upb_json_parser *p); static bool does_fieldmask_start(upb_json_parser *p); static bool does_fieldmask_end(upb_json_parser *p); static void start_fieldmask_object(upb_json_parser *p); @@ -1668,9 +1668,7 @@ static void start_fieldmask_path_text(upb_json_parser *p, const char *ptr) { } static bool end_fieldmask_path_text(upb_json_parser *p, const char *ptr) { - if (!capture_end(p, ptr)) { - return false; - } + return capture_end(p, ptr); } static bool start_fieldmask_path(upb_json_parser *p) { @@ -1683,7 +1681,7 @@ static bool start_fieldmask_path(upb_json_parser *p) { * handler frames, and string events occur in a sub-frame. */ inner = p->top + 1; sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); - upb_sink_startstr(&p->top->sink, sel, 0, &inner->sink); + upb_sink_startstr(p->top->sink, sel, 0, &inner->sink); inner->m = p->top->m; inner->f = p->top->f; inner->name_table = NULL; @@ -1705,10 +1703,10 @@ static bool lower_camel_push( for (;ptr < limit; ptr++) { if (*ptr >= 'A' && *ptr <= 'Z' && !first) { char lower = tolower(*ptr); - upb_sink_putstring(&p->top->sink, sel, "_", 1, NULL); - upb_sink_putstring(&p->top->sink, sel, &lower, 1, NULL); + upb_sink_putstring(p->top->sink, sel, "_", 1, NULL); + upb_sink_putstring(p->top->sink, sel, &lower, 1, NULL); } else { - upb_sink_putstring(&p->top->sink, sel, ptr, 1, NULL); + upb_sink_putstring(p->top->sink, sel, ptr, 1, NULL); } first = false; } @@ -1725,7 +1723,7 @@ static bool end_fieldmask_path(upb_json_parser *p) { } sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR); - upb_sink_endstr(&p->top->sink, sel); + upb_sink_endstr(p->top->sink, sel); p->top--; multipart_end(p); @@ -2487,10 +2485,6 @@ static bool does_fieldmask_end(upb_json_parser *p) { return p->top->m != NULL && is_fieldmask(p->top->m); } -static bool is_fieldmask_object(upb_json_parser *p) { - return p->top->m != NULL && is_fieldmask(p->top->m); -} - #define CHECK_RETURN_TOP(x) if (!(x)) goto error diff --git a/upb/json/printer.c b/upb/json/printer.c index 5b16861..2d566cb 100644 --- a/upb/json/printer.c +++ b/upb/json/printer.c @@ -5,8 +5,9 @@ #include "upb/json/printer.h" -#include +#include #include +#include #include struct upb_json_printer { @@ -1029,7 +1030,7 @@ void printer_sethandlers_fieldmask(const void *closure, upb_handlers *h) { const upb_msgdef *md = upb_handlers_msgdef(h); const upb_fielddef* f = upb_msgdef_itof(md, 1); - upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER; + upb_handlerattr empty_attr = UPB_HANDLERATTR_INIT; upb_handlers_setstartseq(h, f, startseq_fieldmask, &empty_attr); upb_handlers_setendseq(h, f, endseq_fieldmask, &empty_attr); diff --git a/upb/upb.h b/upb/upb.h index 3848229..92db893 100644 --- a/upb/upb.h +++ b/upb/upb.h @@ -377,14 +377,6 @@ typedef enum { UPB_LABEL_REPEATED = 3 } upb_label_t; -/* How integers should be encoded in serializations that offer multiple - * integer encoding methods. */ -typedef enum { - UPB_INTFMT_VARIABLE = 1, - UPB_INTFMT_FIXED = 2, - UPB_INTFMT_ZIGZAG = 3 /* Only for signed types (INT32/INT64). */ -} upb_intfmt_t; - /* Descriptor types, as defined in descriptor.proto. */ typedef enum { UPB_DESCRIPTOR_TYPE_DOUBLE = 1, @@ -407,37 +399,6 @@ typedef enum { UPB_DESCRIPTOR_TYPE_SINT64 = 18 } upb_descriptortype_t; -typedef enum { - UPB_SYNTAX_PROTO2 = 2, - UPB_SYNTAX_PROTO3 = 3 -} upb_syntax_t; - -/* All the different kind of well known type messages. For simplicity of check, - * number wrappers and string wrappers are grouped together. Make sure the - * order and merber of these groups are not changed. - */ -typedef enum { - UPB_WELLKNOWN_UNSPECIFIED, - UPB_WELLKNOWN_ANY, - UPB_WELLKNOWN_FIELDMASK, - UPB_WELLKNOWN_DURATION, - UPB_WELLKNOWN_TIMESTAMP, - /* number wrappers */ - UPB_WELLKNOWN_DOUBLEVALUE, - UPB_WELLKNOWN_FLOATVALUE, - UPB_WELLKNOWN_INT64VALUE, - UPB_WELLKNOWN_UINT64VALUE, - UPB_WELLKNOWN_INT32VALUE, - UPB_WELLKNOWN_UINT32VALUE, - /* string wrappers */ - UPB_WELLKNOWN_STRINGVALUE, - UPB_WELLKNOWN_BYTESVALUE, - UPB_WELLKNOWN_BOOLVALUE, - UPB_WELLKNOWN_VALUE, - UPB_WELLKNOWN_LISTVALUE, - UPB_WELLKNOWN_STRUCT -} upb_wellknowntype_t; - extern const uint8_t upb_desctype_to_fieldtype[]; #endif /* UPB_H_ */ -- cgit v1.2.3 From 12a52977402146e236703e5465c6218b9f6b27b9 Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Sat, 19 Jan 2019 06:07:45 +0000 Subject: Fix empty FieldMask json encoding --- upb/json/printer.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) (limited to 'upb') diff --git a/upb/json/printer.c b/upb/json/printer.c index 2283d55..f0859a3 100644 --- a/upb/json/printer.c +++ b/upb/json/printer.c @@ -752,7 +752,6 @@ static void *startseq_fieldmask(void *closure, const void *handler_data) { UPB_UNUSED(handler_data); p->depth_++; p->first_elem_[p->depth_] = true; - print_data(p, "\"", 1); return closure; } @@ -760,7 +759,6 @@ static bool endseq_fieldmask(void *closure, const void *handler_data) { upb_json_printer *p = closure; UPB_UNUSED(handler_data); p->depth_--; - print_data(p, "\"", 1); return true; } @@ -980,6 +978,29 @@ static bool printer_endmsg_noframe( return true; } +static bool printer_startmsg_fieldmask( + void *closure, const void *handler_data) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + if (p->depth_ == 0) { + upb_bytessink_start(p->output_, 0, &p->subc_); + } + print_data(p, "\"", 1); + return true; +} + +static bool printer_endmsg_fieldmask( + void *closure, const void *handler_data, upb_status *s) { + upb_json_printer *p = closure; + UPB_UNUSED(handler_data); + UPB_UNUSED(s); + print_data(p, "\"", 1); + if (p->depth_ == 0) { + upb_bytessink_end(p->output_); + } + return true; +} + static void *scalar_startstr_onlykey( void *closure, const void *handler_data, size_t size_hint) { upb_json_printer *p = closure; @@ -1033,8 +1054,8 @@ void printer_sethandlers_fieldmask(const void *closure, upb_handlers *h) { upb_handlers_setstartseq(h, f, startseq_fieldmask, &empty_attr); upb_handlers_setendseq(h, f, endseq_fieldmask, &empty_attr); - upb_handlers_setstartmsg(h, printer_startmsg_noframe, &empty_attr); - upb_handlers_setendmsg(h, printer_endmsg_noframe, &empty_attr); + upb_handlers_setstartmsg(h, printer_startmsg_fieldmask, &empty_attr); + upb_handlers_setendmsg(h, printer_endmsg_fieldmask, &empty_attr); upb_handlers_setstartstr(h, f, repeated_startstr_fieldmask, &empty_attr); upb_handlers_setstring(h, f, repeated_str_fieldmask, &empty_attr); -- cgit v1.2.3 From 04923fc26e1ca9f935fe10fb0de0fa932f7aa8b4 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 19 Jan 2019 19:38:03 -0800 Subject: Some fixes for PHP. --- upb/def.c | 21 +++++++++++++-------- upb/pb/encoder.c | 1 + 2 files changed, 14 insertions(+), 8 deletions(-) (limited to 'upb') diff --git a/upb/def.c b/upb/def.c index 5b239dd..f816c4f 100644 --- a/upb/def.c +++ b/upb/def.c @@ -818,7 +818,7 @@ bool upb_oneof_done(upb_oneof_iter *iter) { } upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter) { - return (upb_fielddef*)upb_value_getptr(upb_inttable_iter_value(iter)); + return (upb_fielddef *)upb_value_getconstptr(upb_inttable_iter_value(iter)); } void upb_oneof_iter_setdone(upb_oneof_iter *iter) { @@ -936,9 +936,6 @@ typedef struct { } symtab_addctx; static char* strviewdup(const symtab_addctx *ctx, upb_strview view) { - if (view.size == 0) { - return NULL; - } return upb_strdup2(view.data, view.size, ctx->alloc); } @@ -1329,6 +1326,8 @@ static bool create_enumdef( } } + upb_inttable_compact2(&e->iton, ctx->alloc); + return true; } @@ -1378,6 +1377,7 @@ static bool create_msgdef(const symtab_addctx *ctx, const char *prefix, CHK(assign_msg_indices(m, ctx->status)); assign_msg_wellknowntype(m); + upb_inttable_compact2(&m->itof, ctx->alloc); /* This message is built. Now build nested messages and enums. */ @@ -1571,10 +1571,15 @@ static bool build_filedef( /* Read options. */ file_options_proto = google_protobuf_FileDescriptorProto_options(file_proto); if (file_options_proto) { - file->phpprefix = strviewdup( - ctx, google_protobuf_FileOptions_php_class_prefix(file_options_proto)); - file->phpnamespace = strviewdup( - ctx, google_protobuf_FileOptions_php_namespace(file_options_proto)); + if (google_protobuf_FileOptions_has_php_class_prefix(file_options_proto)) { + file->phpprefix = strviewdup( + ctx, + google_protobuf_FileOptions_php_class_prefix(file_options_proto)); + } + if (google_protobuf_FileOptions_has_php_namespace(file_options_proto)) { + file->phpnamespace = strviewdup( + ctx, google_protobuf_FileOptions_php_namespace(file_options_proto)); + } } /* Verify dependencies. */ diff --git a/upb/pb/encoder.c b/upb/pb/encoder.c index 2da34db..5f74010 100644 --- a/upb/pb/encoder.c +++ b/upb/pb/encoder.c @@ -433,6 +433,7 @@ T(sint64, int64_t, upb_zzenc_64, encode_varint) /* code to build the handlers *************************************************/ +#include static void newhandlers_callback(const void *closure, upb_handlers *h) { const upb_msgdef *m; upb_msg_field_iter i; -- cgit v1.2.3 From 315c167bedbe33386e0bffd537d1f28eb259c986 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 23 Jan 2019 12:58:20 -0800 Subject: Some more fixes for PHP. --- upb/def.c | 204 ++++++++++++++++++++++++----------------------- upb/def.h | 7 +- upb/pb/compile_decoder.c | 1 + upb/upb.c | 6 +- 4 files changed, 115 insertions(+), 103 deletions(-) (limited to 'upb') diff --git a/upb/def.c b/upb/def.c index f816c4f..38ef9fa 100644 --- a/upb/def.c +++ b/upb/def.c @@ -106,6 +106,12 @@ struct upb_filedef { int ext_count; }; +struct upb_symtab { + upb_arena *arena; + upb_strtable syms; /* full_name -> packed def ptr */ + upb_strtable files; /* file_name -> upb_filedef* */ +}; + /* Inside a symtab we store tagged pointers to specific def types. */ typedef enum { UPB_DEFTYPE_MSG = 0, @@ -124,12 +130,6 @@ static upb_value pack_def(const void *ptr, upb_deftype_t type) { return upb_value_constptr((const void*)num); } -struct upb_symtab { - upb_arena *arena; - upb_strtable syms; /* full_name -> packed def ptr */ - upb_strtable files; /* file_name -> upb_filedef* */ -}; - /* isalpha() etc. from are locale-dependent, which we don't want. */ static bool upb_isbetween(char c, char low, char high) { return c >= low && c <= high; @@ -825,97 +825,6 @@ void upb_oneof_iter_setdone(upb_oneof_iter *iter) { upb_inttable_iter_setdone(iter); } -/* upb_filedef ****************************************************************/ - -const char *upb_filedef_name(const upb_filedef *f) { - return f->name; -} - -const char *upb_filedef_package(const upb_filedef *f) { - return f->package; -} - -const char *upb_filedef_phpprefix(const upb_filedef *f) { - return f->phpprefix; -} - -const char *upb_filedef_phpnamespace(const upb_filedef *f) { - return f->phpnamespace; -} - -upb_syntax_t upb_filedef_syntax(const upb_filedef *f) { - return f->syntax; -} - -int upb_filedef_msgcount(const upb_filedef *f) { - return f->msg_count; -} - -int upb_filedef_depcount(const upb_filedef *f) { - return f->dep_count; -} - -int upb_filedef_enumcount(const upb_filedef *f) { - return f->enum_count; -} - -const upb_filedef *upb_filedef_dep(const upb_filedef *f, int i) { - return i < 0 || i >= f->dep_count ? NULL : f->deps[i]; -} - -const upb_msgdef *upb_filedef_msg(const upb_filedef *f, int i) { - return i < 0 || i >= f->msg_count ? NULL : &f->msgs[i]; -} - -const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i) { - return i < 0 || i >= f->enum_count ? NULL : &f->enums[i]; -} - -void upb_symtab_free(upb_symtab *s) { - upb_arena_free(s->arena); - upb_gfree(s); -} - -upb_symtab *upb_symtab_new() { - upb_symtab *s = upb_gmalloc(sizeof(*s)); - upb_alloc *alloc; - - if (!s) { - return NULL; - } - - s->arena = upb_arena_new(); - alloc = upb_arena_alloc(s->arena); - - if (!upb_strtable_init2(&s->syms, UPB_CTYPE_CONSTPTR, alloc) || - !upb_strtable_init2(&s->files, UPB_CTYPE_CONSTPTR, alloc)) { - upb_arena_free(s->arena); - upb_gfree(s); - s = NULL; - } - return s; -} - -const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym) { - upb_value v; - return upb_strtable_lookup(&s->syms, sym, &v) ? - unpack_def(v, UPB_DEFTYPE_MSG) : NULL; -} - -const upb_msgdef *upb_symtab_lookupmsg2(const upb_symtab *s, const char *sym, - size_t len) { - upb_value v; - return upb_strtable_lookup2(&s->syms, sym, len, &v) ? - unpack_def(v, UPB_DEFTYPE_MSG) : NULL; -} - -const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym) { - upb_value v; - return upb_strtable_lookup(&s->syms, sym, &v) ? - unpack_def(v, UPB_DEFTYPE_ENUM) : NULL; -} - - /* Code to build defs from descriptor protos. *********************************/ /* There is a question of how much validation to do here. It will be difficult @@ -1593,8 +1502,9 @@ static bool build_filedef( if (!upb_strtable_lookup2(&ctx->symtab->files, dep_name.data, dep_name.size, &v)) { upb_status_seterrf(ctx->status, - "Depends on file '%s', but it has not been loaded", - dep_name.data); + "Depends on file '" UPB_STRVIEW_FORMAT + "', but it has not been loaded", + UPB_STRVIEW_ARGS(dep_name)); return false; } file->deps[i] = upb_value_getconstptr(v); @@ -1656,6 +1566,102 @@ static bool upb_symtab_addtotabs(upb_symtab *s, symtab_addctx *ctx, return true; } +/* upb_filedef ****************************************************************/ + +const char *upb_filedef_name(const upb_filedef *f) { + return f->name; +} + +const char *upb_filedef_package(const upb_filedef *f) { + return f->package; +} + +const char *upb_filedef_phpprefix(const upb_filedef *f) { + return f->phpprefix; +} + +const char *upb_filedef_phpnamespace(const upb_filedef *f) { + return f->phpnamespace; +} + +upb_syntax_t upb_filedef_syntax(const upb_filedef *f) { + return f->syntax; +} + +int upb_filedef_msgcount(const upb_filedef *f) { + return f->msg_count; +} + +int upb_filedef_depcount(const upb_filedef *f) { + return f->dep_count; +} + +int upb_filedef_enumcount(const upb_filedef *f) { + return f->enum_count; +} + +const upb_filedef *upb_filedef_dep(const upb_filedef *f, int i) { + return i < 0 || i >= f->dep_count ? NULL : f->deps[i]; +} + +const upb_msgdef *upb_filedef_msg(const upb_filedef *f, int i) { + return i < 0 || i >= f->msg_count ? NULL : &f->msgs[i]; +} + +const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i) { + return i < 0 || i >= f->enum_count ? NULL : &f->enums[i]; +} + +void upb_symtab_free(upb_symtab *s) { + upb_arena_free(s->arena); + upb_gfree(s); +} + +upb_symtab *upb_symtab_new() { + upb_symtab *s = upb_gmalloc(sizeof(*s)); + upb_alloc *alloc; + + if (!s) { + return NULL; + } + + s->arena = upb_arena_new(); + alloc = upb_arena_alloc(s->arena); + + if (!upb_strtable_init2(&s->syms, UPB_CTYPE_CONSTPTR, alloc) || + !upb_strtable_init2(&s->files, UPB_CTYPE_CONSTPTR, alloc)) { + upb_arena_free(s->arena); + upb_gfree(s); + s = NULL; + } + return s; +} + +const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym) { + upb_value v; + return upb_strtable_lookup(&s->syms, sym, &v) ? + unpack_def(v, UPB_DEFTYPE_MSG) : NULL; +} + +const upb_msgdef *upb_symtab_lookupmsg2(const upb_symtab *s, const char *sym, + size_t len) { + upb_value v; + return upb_strtable_lookup2(&s->syms, sym, len, &v) ? + unpack_def(v, UPB_DEFTYPE_MSG) : NULL; +} + +const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym) { + upb_value v; + return upb_strtable_lookup(&s->syms, sym, &v) ? + unpack_def(v, UPB_DEFTYPE_ENUM) : NULL; +} + +const upb_filedef *upb_symtab_lookupfile(const upb_symtab *s, const char *name) { + upb_value v; + return upb_strtable_lookup(&s->files, name, &v) ? upb_value_getconstptr(v) + : NULL; +} + const upb_filedef *upb_symtab_addfile( upb_symtab *s, const google_protobuf_FileDescriptorProto *file_proto, upb_status *status) { diff --git a/upb/def.h b/upb/def.h index 2c336c5..9a722f3 100644 --- a/upb/def.h +++ b/upb/def.h @@ -818,6 +818,7 @@ const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym); const upb_msgdef *upb_symtab_lookupmsg2( const upb_symtab *s, const char *sym, size_t len); const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym); +const upb_filedef *upb_symtab_lookupfile(const upb_symtab *s, const char *name); int upb_symtab_filecount(const upb_symtab *s); const upb_filedef *upb_symtab_addfile( upb_symtab *s, const google_protobuf_FileDescriptorProto *file, @@ -850,10 +851,14 @@ class upb::SymbolTable { return MessageDefPtr(upb_symtab_lookupmsg(ptr_.get(), sym)); } - const EnumDefPtr LookupEnum(const char *sym) const { + EnumDefPtr LookupEnum(const char *sym) const { return EnumDefPtr(upb_symtab_lookupenum(ptr_.get(), sym)); } + FileDefPtr LookupFile(const char *name) const { + return FileDefPtr(upb_symtab_lookupfile(ptr_.get(), name)); + } + /* TODO: iteration? */ /* Adds the given serialized FileDescriptorProto to the pool. */ diff --git a/upb/pb/compile_decoder.c b/upb/pb/compile_decoder.c index 75cff48..89f000a 100644 --- a/upb/pb/compile_decoder.c +++ b/upb/pb/compile_decoder.c @@ -890,6 +890,7 @@ void upb_pbcodecache_free(upb_pbcodecache *c) { } upb_inttable_uninit(&c->groups); + upb_arena_free(c->arena); upb_gfree(c); } diff --git a/upb/upb.c b/upb/upb.c index d8d2723..9f80639 100644 --- a/upb/upb.c +++ b/upb/upb.c @@ -191,10 +191,8 @@ upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc) { } } - a = mem; - mem = (char*)mem + sizeof(*a); + a = (void*)((char*)mem + n - sizeof(*a)); n -= sizeof(*a); - upb_arena_addblock(a, mem, n, owned); a->alloc.func = &upb_arena_doalloc; a->block_alloc = &upb_alloc_global; @@ -205,6 +203,8 @@ upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc) { a->block_head = NULL; a->block_alloc = alloc; + upb_arena_addblock(a, mem, n, owned); + return a; } -- cgit v1.2.3 From f4532ab273173d96605ff22a7e47387023651625 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 23 Jan 2019 13:42:33 -0800 Subject: Properly align the arena. --- upb/msg.c | 2 -- upb/upb.c | 8 ++++++++ 2 files changed, 8 insertions(+), 2 deletions(-) (limited to 'upb') diff --git a/upb/msg.c b/upb/msg.c index 20401f9..b46d41c 100644 --- a/upb/msg.c +++ b/upb/msg.c @@ -17,8 +17,6 @@ bool upb_fieldtype_mapkeyok(upb_fieldtype_t type) { /** upb_msgval ****************************************************************/ -#define upb_alignof(t) offsetof(struct { char c; t x; }, x) - /* These functions will generate real memcpy() calls on ARM sadly, because * the compiler assumes they might not be aligned. */ diff --git a/upb/upb.c b/upb/upb.c index 9f80639..0cf5b50 100644 --- a/upb/upb.c +++ b/upb/upb.c @@ -177,11 +177,17 @@ static void *upb_arena_doalloc(upb_alloc *alloc, void *ptr, size_t oldsize, /* Public Arena API ***********************************************************/ +#define upb_alignof(type) offsetof (struct { char c; type member; }, member) + upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc) { const size_t first_block_overhead = sizeof(upb_arena) + sizeof(mem_block); upb_arena *a; bool owned = false; + /* Round block size down to alignof(*a) since we will allocate the arena + * itself at the end. */ + n &= ~(upb_alignof(upb_arena) - 1); + if (n < first_block_overhead) { /* We need to malloc the initial block. */ n = first_block_overhead + 256; @@ -208,6 +214,8 @@ upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc) { return a; } +#undef upb_alignof + void upb_arena_free(upb_arena *a) { cleanup_ent *ent = a->cleanup_head; mem_block *block = a->block_head; -- cgit v1.2.3 From 9bc7973e3893a72a62e75b8c7075d692c8794ec1 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Wed, 23 Jan 2019 17:16:31 -0800 Subject: Fixes for Google import. --- tools/amalgamate.py | 4 ++-- upb/handlers-inl.h | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'upb') diff --git a/tools/amalgamate.py b/tools/amalgamate.py index 9ad0286..9640201 100755 --- a/tools/amalgamate.py +++ b/tools/amalgamate.py @@ -16,11 +16,11 @@ class Amalgamator: self.output_h = open(output_path + "upb.h", "w") self.output_c = open(output_path + "upb.c", "w") - self.output_c.write("// Amalgamated source file\n") + self.output_c.write("/* Amalgamated source file */\n") self.output_c.write('#include "upb.h"\n') self.output_c.write(open("upb/port_def.inc").read()) - self.output_h.write("// Amalgamated source file\n") + self.output_h.write("/* Amalgamated source file */\n") self.output_h.write(open("upb/port_def.inc").read()) def finish(self): diff --git a/upb/handlers-inl.h b/upb/handlers-inl.h index 5677a4a..40a0047 100644 --- a/upb/handlers-inl.h +++ b/upb/handlers-inl.h @@ -7,6 +7,8 @@ #define UPB_HANDLERS_INL_H_ #include +#include +#include "upb/handlers.h" #ifdef __cplusplus @@ -162,8 +164,8 @@ struct FuncInfo { * These functions are not bound to a handler data so have no data or cleanup * handler. */ struct UnboundFunc { - CleanupFunc *GetCleanup() { return NULL; } - void *GetData() { return NULL; } + CleanupFunc *GetCleanup() { return nullptr; } + void *GetData() { return nullptr; } }; template -- cgit v1.2.3 From 2719bcff0d1972994e98433b830a921e2ded5fc4 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Thu, 24 Jan 2019 15:53:36 -0800 Subject: Fixes for Google-internal tests. --- upb/def.c | 22 ++++++++++++++++++++++ upb/def.h | 6 +++++- 2 files changed, 27 insertions(+), 1 deletion(-) (limited to 'upb') diff --git a/upb/def.c b/upb/def.c index 38ef9fa..4d4fea9 100644 --- a/upb/def.c +++ b/upb/def.c @@ -958,6 +958,28 @@ static bool create_oneofdef( static bool parse_default(const symtab_addctx *ctx, const char *str, size_t len, upb_fielddef *f) { char *end; + char nullz[64]; + errno = 0; + + switch (upb_fielddef_type(f)) { + case UPB_TYPE_INT32: + case UPB_TYPE_INT64: + case UPB_TYPE_UINT32: + case UPB_TYPE_UINT64: + case UPB_TYPE_DOUBLE: + case UPB_TYPE_FLOAT: + /* Standard C number parsing functions expect null-terminated strings. */ + if (len >= sizeof(nullz) - 1) { + return false; + } + memcpy(nullz, str, len); + nullz[len] = '\0'; + str = nullz; + break; + default: + break; + } + switch (upb_fielddef_type(f)) { case UPB_TYPE_INT32: { long val = strtol(str, &end, 0); diff --git a/upb/def.h b/upb/def.h index 9a722f3..f2b7032 100644 --- a/upb/def.h +++ b/upb/def.h @@ -134,6 +134,7 @@ uint32_t upb_fielddef_selectorbase(const upb_fielddef *f); * an extension. */ class upb::FieldDefPtr { public: + FieldDefPtr() : ptr_(nullptr) {} explicit FieldDefPtr(const upb_fielddef *ptr) : ptr_(ptr) {} const upb_fielddef* ptr() const { return ptr_; } @@ -302,6 +303,7 @@ bool upb_oneof_iter_isequal(const upb_oneof_iter *iter1, /* Class that represents a oneof. */ class upb::OneofDefPtr { public: + OneofDefPtr() : ptr_(nullptr) {} explicit OneofDefPtr(const upb_oneofdef *ptr) : ptr_(ptr) {} const upb_oneofdef* ptr() const { return ptr_; } @@ -482,7 +484,8 @@ bool upb_msg_oneof_iter_isequal(const upb_msg_oneof_iter *iter1, /* Structure that describes a single .proto message type. */ class upb::MessageDefPtr { public: - MessageDefPtr(const upb_msgdef *ptr) : ptr_(ptr) {} + MessageDefPtr() : ptr_(nullptr) {} + explicit MessageDefPtr(const upb_msgdef *ptr) : ptr_(ptr) {} const upb_msgdef *ptr() const { return ptr_; } explicit operator bool() const { return ptr_ != nullptr; } @@ -689,6 +692,7 @@ int32_t upb_enum_iter_number(upb_enum_iter *iter); class upb::EnumDefPtr { public: + EnumDefPtr() : ptr_(nullptr) {} explicit EnumDefPtr(const upb_enumdef* ptr) : ptr_(ptr) {} const upb_enumdef* ptr() const { return ptr_; } -- cgit v1.2.3 From 76033b3a655d1aa8ea243bd381fce2616a062edc Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Fri, 25 Jan 2019 12:12:44 -0800 Subject: Correct TextPrinterPtr. --- upb/pb/textprinter.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'upb') diff --git a/upb/pb/textprinter.h b/upb/pb/textprinter.h index e59e11d..80abc00 100644 --- a/upb/pb/textprinter.h +++ b/upb/pb/textprinter.h @@ -12,7 +12,7 @@ #ifdef __cplusplus namespace upb { namespace pb { -class TextPrinter; +class TextPrinterPtr; } /* namespace pb */ } /* namespace upb */ #endif -- cgit v1.2.3 From c358bb42fd9d4121bd642bcd41a317e5ed7625be Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Fri, 25 Jan 2019 12:15:23 -0800 Subject: A few more Google fixes. --- BUILD | 2 +- upb/pb/decoder.int.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'upb') diff --git a/BUILD b/BUILD index 1c6cdef..a0badc9 100644 --- a/BUILD +++ b/BUILD @@ -75,6 +75,7 @@ cc_library( "upb/pb/textprinter.c", "upb/pb/varint.c", "upb/pb/varint.int.h", + "upb/table.int.h", ], hdrs = [ "upb/pb/decoder.h", @@ -138,7 +139,6 @@ cc_binary( ], ) - # We strip the tests and remaining rules from google3 until the upb_proto_library() # and upb_proto_reflection_library() rules are fixed. diff --git a/upb/pb/decoder.int.h b/upb/pb/decoder.int.h index 47eb3ed..42fd7f8 100644 --- a/upb/pb/decoder.int.h +++ b/upb/pb/decoder.int.h @@ -70,7 +70,7 @@ typedef enum { #define OP_MAX OP_HALT -UPB_INLINE opcode getop(uint32_t instr) { return instr & 0xff; } +UPB_INLINE opcode getop(uint32_t instr) { return (opcode)(instr & 0xff); } struct upb_pbcodecache { upb_arena *arena; -- cgit v1.2.3 From 8980f6db5c4d28de9dd8773ae27c823767122970 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Fri, 25 Jan 2019 12:20:49 -0800 Subject: text_printer.h now successfully compiles. --- CMakeLists.txt | 1 + tests/test_cpp.cc | 1 + upb/pb/textprinter.h | 7 ++++--- 3 files changed, 6 insertions(+), 3 deletions(-) (limited to 'upb') diff --git a/CMakeLists.txt b/CMakeLists.txt index 58d6571..dfdeba5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -93,6 +93,7 @@ add_library(upb_pb upb/pb/textprinter.c upb/pb/varint.c upb/pb/varint.int.h + upb/table.int.h upb/pb/decoder.h upb/pb/encoder.h upb/pb/textprinter.h) diff --git a/tests/test_cpp.cc b/tests/test_cpp.cc index 5e9a8dd..86d7bc5 100644 --- a/tests/test_cpp.cc +++ b/tests/test_cpp.cc @@ -15,6 +15,7 @@ #include "upb/def.h" #include "upb/handlers.h" #include "upb/pb/decoder.h" +#include "upb/pb/textprinter.h" #include "upb/upb.h" #include "upb_test.h" diff --git a/upb/pb/textprinter.h b/upb/pb/textprinter.h index 80abc00..0af2b1a 100644 --- a/upb/pb/textprinter.h +++ b/upb/pb/textprinter.h @@ -42,9 +42,10 @@ class upb::pb::TextPrinterPtr { /* The given handlers must have come from NewHandlers(). It must outlive the * TextPrinter. */ - static TextPrinterPtr *Create(Arena *arena, const upb::Handlers *handlers, - BytesSink output) { - return TextPrinterPtr(upb_textprinter_create(arena, handlers, output)); + static TextPrinterPtr Create(Arena *arena, upb::HandlersPtr *handlers, + BytesSink output) { + return TextPrinterPtr( + upb_textprinter_create(arena->ptr(), handlers->ptr(), output.sink())); } void SetSingleLineMode(bool single_line) { -- cgit v1.2.3 From c4327e55ac186383f909d52ddd363971a2c60562 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Fri, 25 Jan 2019 15:03:22 -0800 Subject: A few more fixes. --- upb/pb/decoder.h | 1 + upb/sink.h | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'upb') diff --git a/upb/pb/decoder.h b/upb/pb/decoder.h index 86e51df..709db49 100644 --- a/upb/pb/decoder.h +++ b/upb/pb/decoder.h @@ -117,6 +117,7 @@ void upb_pbdecoder_reset(upb_pbdecoder *d); * decoded data to its output sink. */ class upb::pb::DecoderPtr { public: + DecoderPtr() : ptr_(nullptr) {} DecoderPtr(upb_pbdecoder* ptr) : ptr_(ptr) {} upb_pbdecoder* ptr() { return ptr_; } diff --git a/upb/sink.h b/upb/sink.h index e41feae..4d6d0a3 100644 --- a/upb/sink.h +++ b/upb/sink.h @@ -503,7 +503,7 @@ bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink sink); namespace upb { template bool PutBuffer(const T& str, BytesSink sink) { - return upb_bufsrc_putbuf(str.c_str(), str.size(), sink.sink()); + return upb_bufsrc_putbuf(str.data(), str.size(), sink.sink()); } } -- cgit v1.2.3 From 0dbbfd1f8041f863f8f4c6ada33c4102520d406f Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Sat, 26 Jan 2019 19:14:16 -0800 Subject: Added missing functions. --- tests/test_cpp.cc | 20 ++++++++++++++++++++ upb/def.c | 10 ++++++++++ 2 files changed, 30 insertions(+) (limited to 'upb') diff --git a/tests/test_cpp.cc b/tests/test_cpp.cc index 86d7bc5..f1badc4 100644 --- a/tests/test_cpp.cc +++ b/tests/test_cpp.cc @@ -893,6 +893,26 @@ void TestHandlerDataDestruction() { ASSERT(x == 0); } +void TestIteration() { + upb::SymbolTable symtab; + upb::MessageDefPtr md(upb_test_TestMessage_getmsgdef(symtab.ptr())); + + // Test range-based for on both fields and oneofs (with the iterator adaptor). + int field_count = 0; + for (auto field : md.fields()) { + UPB_UNUSED(field); + field_count++; + } + ASSERT(field_count == md.field_count()); + + int oneof_count = 0; + for (auto oneof : md.oneofs()) { + UPB_UNUSED(oneof); + oneof_count++; + } + ASSERT(oneof_count == md.oneof_count()); +} + extern "C" { int run_tests(int argc, char *argv[]) { diff --git a/upb/def.c b/upb/def.c index 4d4fea9..6d653ca 100644 --- a/upb/def.c +++ b/upb/def.c @@ -745,6 +745,11 @@ void upb_msg_field_iter_setdone(upb_msg_field_iter *iter) { upb_inttable_iter_setdone(iter); } +bool upb_msg_field_iter_isequal(const upb_msg_field_iter * iter1, + const upb_msg_field_iter * iter2) { + return upb_inttable_iter_isequal(iter1, iter2); +} + void upb_msg_oneof_begin(upb_msg_oneof_iter *iter, const upb_msgdef *m) { upb_strtable_begin(iter, &m->ntof); /* We need to skip past any initial fields. */ @@ -774,6 +779,11 @@ void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter) { upb_strtable_iter_setdone(iter); } +bool upb_msg_oneof_iter_isequal(const upb_msg_oneof_iter *iter1, + const upb_msg_oneof_iter *iter2) { + return upb_strtable_iter_isequal(iter1, iter2); +} + /* upb_oneofdef ***************************************************************/ const char *upb_oneofdef_name(const upb_oneofdef *o) { -- cgit v1.2.3 From 865876895d5a35bfa4f4135b1f545b73fb2faaac Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Sat, 26 Jan 2019 19:49:09 -0800 Subject: Fixed tests and code. --- tests/test_cpp.cc | 1 + upb/table.c | 4 ++++ 2 files changed, 5 insertions(+) (limited to 'upb') diff --git a/tests/test_cpp.cc b/tests/test_cpp.cc index f1badc4..ed8e9c4 100644 --- a/tests/test_cpp.cc +++ b/tests/test_cpp.cc @@ -948,6 +948,7 @@ int run_tests(int argc, char *argv[]) { TestMismatchedTypes(); TestHandlerDataDestruction(); + TestIteration(); return 0; } diff --git a/upb/table.c b/upb/table.c index 4239c6f..10d8322 100644 --- a/upb/table.c +++ b/upb/table.c @@ -381,6 +381,7 @@ void upb_strtable_next(upb_strtable_iter *i) { } bool upb_strtable_done(const upb_strtable_iter *i) { + if (!i->t) return true; return i->index >= upb_table_size(&i->t->t) || upb_tabent_isempty(str_tabent(i)); } @@ -403,6 +404,7 @@ upb_value upb_strtable_iter_value(const upb_strtable_iter *i) { } void upb_strtable_iter_setdone(upb_strtable_iter *i) { + i->t = NULL; i->index = SIZE_MAX; } @@ -692,6 +694,7 @@ void upb_inttable_next(upb_inttable_iter *iter) { } bool upb_inttable_done(const upb_inttable_iter *i) { + if (!i->t) return true; if (i->array_part) { return i->index >= i->t->array_size || !upb_arrhas(int_arrent(i)); @@ -714,6 +717,7 @@ upb_value upb_inttable_iter_value(const upb_inttable_iter *i) { } void upb_inttable_iter_setdone(upb_inttable_iter *i) { + i->t = NULL; i->index = SIZE_MAX; i->array_part = false; } -- cgit v1.2.3 From afdd4698bafa18057f0070d991f6202c6ea7a001 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Sat, 26 Jan 2019 20:13:12 -0800 Subject: Added missing fielddef function implementations for C++. --- upb/def.h | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'upb') diff --git a/upb/def.h b/upb/def.h index f2b7032..13e9611 100644 --- a/upb/def.h +++ b/upb/def.h @@ -652,6 +652,10 @@ class upb::MessageDefPtr { const upb_msgdef* ptr_; }; +inline upb::MessageDefPtr upb::FieldDefPtr::message_subdef() const { + return MessageDefPtr(upb_fielddef_msgsubdef(ptr_)); +} + #endif /* __cplusplus */ /* upb_enumdef ****************************************************************/ @@ -745,6 +749,10 @@ class upb::EnumDefPtr { const upb_enumdef *ptr_; }; +inline upb::EnumDefPtr upb::FieldDefPtr::enum_subdef() const { + return EnumDefPtr(upb_fielddef_enumsubdef(ptr_)); +} + #endif /* __cplusplus */ /* upb_filedef ****************************************************************/ -- cgit v1.2.3 From c1706e0a7cad3c1d24b0f0d19bd3626906fa9bfd Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Sat, 26 Jan 2019 20:20:55 -0800 Subject: Added a few more missing functions. --- upb/def.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'upb') diff --git a/upb/def.h b/upb/def.h index 13e9611..c585aa8 100644 --- a/upb/def.h +++ b/upb/def.h @@ -103,7 +103,6 @@ bool upb_fielddef_packed(const upb_fielddef *f); size_t upb_fielddef_getjsonname(const upb_fielddef *f, char *buf, size_t len); const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f); const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f); -upb_msgdef *upb_fielddef_containingtype_mutable(upb_fielddef *f); uint32_t upb_fielddef_index(const upb_fielddef *f); bool upb_fielddef_issubmsg(const upb_fielddef *f); bool upb_fielddef_isstring(const upb_fielddef *f); @@ -377,6 +376,10 @@ class upb::OneofDefPtr { const upb_oneofdef *ptr_; }; +inline upb::OneofDefPtr upb::FieldDefPtr::containing_oneof() const { + return OneofDefPtr(upb_fielddef_containingoneof(ptr_)); +} + #endif /* __cplusplus */ /* upb_msgdef *****************************************************************/ @@ -656,6 +659,14 @@ inline upb::MessageDefPtr upb::FieldDefPtr::message_subdef() const { return MessageDefPtr(upb_fielddef_msgsubdef(ptr_)); } +inline upb::MessageDefPtr upb::FieldDefPtr::containing_type() const { + return MessageDefPtr(upb_fielddef_containingtype(ptr_)); +} + +inline upb::MessageDefPtr upb::OneofDefPtr::containing_type() const { + return MessageDefPtr(upb_oneofdef_containingtype(ptr_)); +} + #endif /* __cplusplus */ /* upb_enumdef ****************************************************************/ -- cgit v1.2.3 From 33cd3cfa1d21d9f3557f01a137bec43322a3ec93 Mon Sep 17 00:00:00 2001 From: Shahid Date: Sat, 2 Feb 2019 00:46:46 +0530 Subject: Update varint.int.h --- upb/pb/varint.int.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'upb') diff --git a/upb/pb/varint.int.h b/upb/pb/varint.int.h index 9c54311..5de1296 100644 --- a/upb/pb/varint.int.h +++ b/upb/pb/varint.int.h @@ -9,6 +9,7 @@ #include #include #include +#include #include "upb/upb.h" #ifdef __cplusplus @@ -130,6 +131,9 @@ UPB_INLINE uint64_t upb_vencode32(uint32_t val) { uint64_t ret = 0; UPB_ASSERT(bytes <= 5); memcpy(&ret, buf, bytes); +#ifdef UPB_BIG_ENDIAN + ret = bswap_64(ret); +#endif UPB_ASSERT(ret <= 0xffffffffffU); return ret; } -- cgit v1.2.3 From e223001916b5ca8d914dcf9fe39fa1d9b4028275 Mon Sep 17 00:00:00 2001 From: Shahid Date: Sat, 2 Feb 2019 00:48:10 +0530 Subject: Update table.c --- upb/table.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'upb') diff --git a/upb/table.c b/upb/table.c index 4239c6f..96dccf6 100644 --- a/upb/table.c +++ b/upb/table.c @@ -726,7 +726,7 @@ bool upb_inttable_iter_isequal(const upb_inttable_iter *i1, i1->array_part == i2->array_part; } -#ifdef UPB_UNALIGNED_READS_OK +#if defined(UPB_UNALIGNED_READS_OK) || defined(__s390x__) /* ----------------------------------------------------------------------------- * MurmurHash2, by Austin Appleby (released as public domain). * Reformatted and C99-ified by Joshua Haberman. -- cgit v1.2.3 From ad905b08f5f93b497311290fb4df4059ccd083eb Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Sun, 17 Feb 2019 20:36:26 -0800 Subject: Fixed amalgamation to properly include stdint.h first for UPB_SIZE(). --- tools/amalgamate.py | 1 + upb/port_def.inc | 4 ++++ 2 files changed, 5 insertions(+) (limited to 'upb') diff --git a/tools/amalgamate.py b/tools/amalgamate.py index 9640201..374d126 100755 --- a/tools/amalgamate.py +++ b/tools/amalgamate.py @@ -21,6 +21,7 @@ class Amalgamator: self.output_c.write(open("upb/port_def.inc").read()) self.output_h.write("/* Amalgamated source file */\n") + self.output_h.write('#include ') self.output_h.write(open("upb/port_def.inc").read()) def finish(self): diff --git a/upb/port_def.inc b/upb/port_def.inc index 33ff78c..fe975a0 100644 --- a/upb/port_def.inc +++ b/upb/port_def.inc @@ -1,4 +1,8 @@ +#ifndef UINTPTR_MAX +#error must include stdint.h first +#endif + #if UINTPTR_MAX == 0xffffffff #define UPB_SIZE(size32, size64) size32 #else -- cgit v1.2.3 From 17db3722678f2ea11edbe2aeb34efdbce030c07f Mon Sep 17 00:00:00 2001 From: Shahid Date: Mon, 25 Feb 2019 13:40:12 +0530 Subject: Removing the 'byteswap.h' header which is not portable Incorporating code review changes: https://github.com/google/upb/pull/151#discussion_r257529497 --- upb/pb/varint.int.h | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'upb') diff --git a/upb/pb/varint.int.h b/upb/pb/varint.int.h index 5de1296..b216a94 100644 --- a/upb/pb/varint.int.h +++ b/upb/pb/varint.int.h @@ -9,7 +9,6 @@ #include #include #include -#include #include "upb/upb.h" #ifdef __cplusplus @@ -25,6 +24,18 @@ extern "C" { * descriptor type (upb_descriptortype_t). */ extern const uint8_t upb_pb_native_wire_types[]; +UPB_INLINE uint64_t byteswap64(uint64_t val) +{ + return ((((val) & 0xff00000000000000ull) >> 56) + | (((val) & 0x00ff000000000000ull) >> 40) + | (((val) & 0x0000ff0000000000ull) >> 24) + | (((val) & 0x000000ff00000000ull) >> 8) + | (((val) & 0x00000000ff000000ull) << 8) + | (((val) & 0x0000000000ff0000ull) << 24) + | (((val) & 0x000000000000ff00ull) << 40) + | (((val) & 0x00000000000000ffull) << 56)); +} + /* Zig-zag encoding/decoding **************************************************/ UPB_INLINE int32_t upb_zzdec_32(uint32_t n) { @@ -132,7 +143,7 @@ UPB_INLINE uint64_t upb_vencode32(uint32_t val) { UPB_ASSERT(bytes <= 5); memcpy(&ret, buf, bytes); #ifdef UPB_BIG_ENDIAN - ret = bswap_64(ret); + ret = byteswap64(ret); #endif UPB_ASSERT(ret <= 0xffffffffffU); return ret; -- cgit v1.2.3 From da8d1392c15b56a55f3d98108072c7fa9d73fe79 Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Tue, 5 Mar 2019 13:20:06 -0800 Subject: Fix the issue for strptime * Define _XOPEN_SOURCE * Exclude usage on windows --- upb/json/parser.c | 130 ++++++++++++++++++++++++++++------------------------- upb/json/parser.rl | 14 ++++-- 2 files changed, 78 insertions(+), 66 deletions(-) (limited to 'upb') diff --git a/upb/json/parser.c b/upb/json/parser.c index 42539ee..f15a95e 100644 --- a/upb/json/parser.c +++ b/upb/json/parser.c @@ -21,6 +21,9 @@ ** - handling of keys/escape-sequences/etc that span input buffers. */ +/* Need to define _XOPEN_SOURCE before any include to make strptime work. */ +#define _XOPEN_SOURCE 700 + #include #include #include @@ -29,10 +32,6 @@ #include #include -/* Need to define __USE_XOPEN before including time.h to make strptime work. */ -#ifndef __USE_XOPEN -#define __USE_XOPEN -#endif #include #include "upb/json/parser.h" @@ -1547,12 +1546,19 @@ static bool end_timestamp_base(upb_json_parser *p, const char *ptr) { memcpy(timestamp_buf + UPB_TIMESTAMP_BASE_SIZE, "GMT", 3); timestamp_buf[UPB_TIMESTAMP_BASE_SIZE + 3] = 0; +#if defined __MINGW32__ || defined __MINGW64__ + upb_status_seterrf( + &p->status, "error parsing timestamp: mingw doesn't support strptime"); + upb_env_reporterror(p->env, &p->status); + return false; +#else /* Parse seconds */ if (strptime(timestamp_buf, "%FT%H:%M:%S%Z", &p->tm) == NULL) { upb_status_seterrf(&p->status, "error parsing timestamp: %s", buf); upb_env_reporterror(p->env, &p->status); return false; } +#endif /* Clean up buffer */ multipart_end(p); @@ -2548,11 +2554,11 @@ static bool is_fieldmask_object(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 2730 "upb/json/parser.rl" +#line 2736 "upb/json/parser.rl" -#line 2556 "upb/json/parser.c" +#line 2562 "upb/json/parser.c" static const char _json_actions[] = { 0, 1, 0, 1, 1, 1, 3, 1, 4, 1, 6, 1, 7, 1, 8, 1, @@ -2804,7 +2810,7 @@ static const int json_en_value_machine = 78; static const int json_en_main = 1; -#line 2733 "upb/json/parser.rl" +#line 2739 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -2827,7 +2833,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 2831 "upb/json/parser.c" +#line 2837 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -2902,103 +2908,103 @@ _match: switch ( *_acts++ ) { case 1: -#line 2561 "upb/json/parser.rl" +#line 2567 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 2563 "upb/json/parser.rl" +#line 2569 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 23;goto _again;} } break; case 3: -#line 2567 "upb/json/parser.rl" +#line 2573 "upb/json/parser.rl" { start_text(parser, p); } break; case 4: -#line 2568 "upb/json/parser.rl" +#line 2574 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -#line 2574 "upb/json/parser.rl" +#line 2580 "upb/json/parser.rl" { start_hex(parser); } break; case 6: -#line 2575 "upb/json/parser.rl" +#line 2581 "upb/json/parser.rl" { hexdigit(parser, p); } break; case 7: -#line 2576 "upb/json/parser.rl" +#line 2582 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -#line 2582 "upb/json/parser.rl" +#line 2588 "upb/json/parser.rl" { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -#line 2588 "upb/json/parser.rl" +#line 2594 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 10: -#line 2600 "upb/json/parser.rl" +#line 2606 "upb/json/parser.rl" { start_duration_base(parser, p); } break; case 11: -#line 2601 "upb/json/parser.rl" +#line 2607 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_duration_base(parser, p)); } break; case 12: -#line 2603 "upb/json/parser.rl" +#line 2609 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 13: -#line 2608 "upb/json/parser.rl" +#line 2614 "upb/json/parser.rl" { start_timestamp_base(parser, p); } break; case 14: -#line 2609 "upb/json/parser.rl" +#line 2615 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_base(parser, p)); } break; case 15: -#line 2611 "upb/json/parser.rl" +#line 2617 "upb/json/parser.rl" { start_timestamp_fraction(parser, p); } break; case 16: -#line 2612 "upb/json/parser.rl" +#line 2618 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } break; case 17: -#line 2614 "upb/json/parser.rl" +#line 2620 "upb/json/parser.rl" { start_timestamp_zone(parser, p); } break; case 18: -#line 2615 "upb/json/parser.rl" +#line 2621 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } break; case 19: -#line 2617 "upb/json/parser.rl" +#line 2623 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 20: -#line 2622 "upb/json/parser.rl" +#line 2628 "upb/json/parser.rl" { start_fieldmask_path_text(parser, p); } break; case 21: -#line 2623 "upb/json/parser.rl" +#line 2629 "upb/json/parser.rl" { end_fieldmask_path_text(parser, p); } break; case 22: -#line 2628 "upb/json/parser.rl" +#line 2634 "upb/json/parser.rl" { start_fieldmask_path(parser); } break; case 23: -#line 2629 "upb/json/parser.rl" +#line 2635 "upb/json/parser.rl" { end_fieldmask_path(parser); } break; case 24: -#line 2635 "upb/json/parser.rl" +#line 2641 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 25: -#line 2640 "upb/json/parser.rl" +#line 2646 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) { {stack[top++] = cs; cs = 47;goto _again;} @@ -3012,11 +3018,11 @@ _match: } break; case 26: -#line 2653 "upb/json/parser.rl" +#line 2659 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 78;goto _again;} } break; case 27: -#line 2658 "upb/json/parser.rl" +#line 2664 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_member(parser, p); @@ -3026,11 +3032,11 @@ _match: } break; case 28: -#line 2665 "upb/json/parser.rl" +#line 2671 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_membername(parser)); } break; case 29: -#line 2668 "upb/json/parser.rl" +#line 2674 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { end_any_member(parser, p); @@ -3040,7 +3046,7 @@ _match: } break; case 30: -#line 2679 "upb/json/parser.rl" +#line 2685 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_object(parser, p); @@ -3050,7 +3056,7 @@ _match: } break; case 31: -#line 2688 "upb/json/parser.rl" +#line 2694 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { CHECK_RETURN_TOP(end_any_object(parser, p)); @@ -3060,54 +3066,54 @@ _match: } break; case 32: -#line 2700 "upb/json/parser.rl" +#line 2706 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_array(parser)); } break; case 33: -#line 2704 "upb/json/parser.rl" +#line 2710 "upb/json/parser.rl" { end_array(parser); } break; case 34: -#line 2709 "upb/json/parser.rl" +#line 2715 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_number(parser, p)); } break; case 35: -#line 2710 "upb/json/parser.rl" +#line 2716 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 36: -#line 2712 "upb/json/parser.rl" +#line 2718 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_stringval(parser)); } break; case 37: -#line 2713 "upb/json/parser.rl" +#line 2719 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_stringval(parser)); } break; case 38: -#line 2715 "upb/json/parser.rl" +#line 2721 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 39: -#line 2717 "upb/json/parser.rl" +#line 2723 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 40: -#line 2719 "upb/json/parser.rl" +#line 2725 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 41: -#line 2721 "upb/json/parser.rl" +#line 2727 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_subobject_full(parser)); } break; case 42: -#line 2722 "upb/json/parser.rl" +#line 2728 "upb/json/parser.rl" { end_subobject_full(parser); } break; case 43: -#line 2727 "upb/json/parser.rl" +#line 2733 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 3111 "upb/json/parser.c" +#line 3117 "upb/json/parser.c" } } @@ -3124,32 +3130,32 @@ _again: while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 0: -#line 2559 "upb/json/parser.rl" +#line 2565 "upb/json/parser.rl" { p--; {cs = stack[--top]; if ( p == pe ) goto _test_eof; goto _again;} } break; case 35: -#line 2710 "upb/json/parser.rl" +#line 2716 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 38: -#line 2715 "upb/json/parser.rl" +#line 2721 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 39: -#line 2717 "upb/json/parser.rl" +#line 2723 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 40: -#line 2719 "upb/json/parser.rl" +#line 2725 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 42: -#line 2722 "upb/json/parser.rl" +#line 2728 "upb/json/parser.rl" { end_subobject_full(parser); } break; -#line 3153 "upb/json/parser.c" +#line 3159 "upb/json/parser.c" } } } @@ -3157,7 +3163,7 @@ goto _again;} } _out: {} } -#line 2755 "upb/json/parser.rl" +#line 2761 "upb/json/parser.rl" if (p != pe) { upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p); @@ -3206,13 +3212,13 @@ static void json_parser_reset(upb_json_parser *p) { /* Emit Ragel initialization of the parser. */ -#line 3210 "upb/json/parser.c" +#line 3216 "upb/json/parser.c" { cs = json_start; top = 0; } -#line 2803 "upb/json/parser.rl" +#line 2809 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); diff --git a/upb/json/parser.rl b/upb/json/parser.rl index 70a45cb..2fc5027 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -19,6 +19,9 @@ ** - handling of keys/escape-sequences/etc that span input buffers. */ +/* Need to define _XOPEN_SOURCE before any include to make strptime work. */ +#define _XOPEN_SOURCE 700 + #include #include #include @@ -27,10 +30,6 @@ #include #include -/* Need to define __USE_XOPEN before including time.h to make strptime work. */ -#ifndef __USE_XOPEN -#define __USE_XOPEN -#endif #include #include "upb/json/parser.h" @@ -1545,12 +1544,19 @@ static bool end_timestamp_base(upb_json_parser *p, const char *ptr) { memcpy(timestamp_buf + UPB_TIMESTAMP_BASE_SIZE, "GMT", 3); timestamp_buf[UPB_TIMESTAMP_BASE_SIZE + 3] = 0; +#if defined __MINGW32__ || defined __MINGW64__ + upb_status_seterrf( + &p->status, "error parsing timestamp: mingw doesn't support strptime"); + upb_env_reporterror(p->env, &p->status); + return false; +#else /* Parse seconds */ if (strptime(timestamp_buf, "%FT%H:%M:%S%Z", &p->tm) == NULL) { upb_status_seterrf(&p->status, "error parsing timestamp: %s", buf); upb_env_reporterror(p->env, &p->status); return false; } +#endif /* Clean up buffer */ multipart_end(p); -- cgit v1.2.3 From 71ec01e34b03557cfed9df5f417791a189e7fca6 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Tue, 5 Mar 2019 19:34:20 -0800 Subject: Fix for Windows. --- upb/json/parser.c | 121 ++++++++++++++++++++++++++--------------------------- upb/json/parser.rl | 5 +-- 2 files changed, 62 insertions(+), 64 deletions(-) (limited to 'upb') diff --git a/upb/json/parser.c b/upb/json/parser.c index f73533a..d8db7fe 100644 --- a/upb/json/parser.c +++ b/upb/json/parser.c @@ -1537,9 +1537,8 @@ static bool end_timestamp_base(upb_json_parser *p, const char *ptr) { timestamp_buf[UPB_TIMESTAMP_BASE_SIZE + 3] = 0; #if defined __MINGW32__ || defined __MINGW64__ - upb_status_seterrf( - &p->status, "error parsing timestamp: mingw doesn't support strptime"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrf(p->status, + "error parsing timestamp: mingw doesn't support strptime"); return false; #else /* Parse seconds */ @@ -2514,11 +2513,11 @@ static bool does_fieldmask_end(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 2696 "upb/json/parser.rl" +#line 2695 "upb/json/parser.rl" -#line 2522 "upb/json/parser.c" +#line 2521 "upb/json/parser.c" static const char _json_actions[] = { 0, 1, 0, 1, 1, 1, 3, 1, 4, 1, 6, 1, 7, 1, 8, 1, @@ -2770,7 +2769,7 @@ static const int json_en_value_machine = 78; static const int json_en_main = 1; -#line 2699 "upb/json/parser.rl" +#line 2698 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -2793,7 +2792,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 2797 "upb/json/parser.c" +#line 2796 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -2868,103 +2867,103 @@ _match: switch ( *_acts++ ) { case 1: -#line 2527 "upb/json/parser.rl" +#line 2526 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 2529 "upb/json/parser.rl" +#line 2528 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 23;goto _again;} } break; case 3: -#line 2533 "upb/json/parser.rl" +#line 2532 "upb/json/parser.rl" { start_text(parser, p); } break; case 4: -#line 2534 "upb/json/parser.rl" +#line 2533 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -#line 2540 "upb/json/parser.rl" +#line 2539 "upb/json/parser.rl" { start_hex(parser); } break; case 6: -#line 2541 "upb/json/parser.rl" +#line 2540 "upb/json/parser.rl" { hexdigit(parser, p); } break; case 7: -#line 2542 "upb/json/parser.rl" +#line 2541 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -#line 2548 "upb/json/parser.rl" +#line 2547 "upb/json/parser.rl" { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -#line 2554 "upb/json/parser.rl" +#line 2553 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 10: -#line 2566 "upb/json/parser.rl" +#line 2565 "upb/json/parser.rl" { start_duration_base(parser, p); } break; case 11: -#line 2567 "upb/json/parser.rl" +#line 2566 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_duration_base(parser, p)); } break; case 12: -#line 2569 "upb/json/parser.rl" +#line 2568 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 13: -#line 2574 "upb/json/parser.rl" +#line 2573 "upb/json/parser.rl" { start_timestamp_base(parser, p); } break; case 14: -#line 2575 "upb/json/parser.rl" +#line 2574 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_base(parser, p)); } break; case 15: -#line 2577 "upb/json/parser.rl" +#line 2576 "upb/json/parser.rl" { start_timestamp_fraction(parser, p); } break; case 16: -#line 2578 "upb/json/parser.rl" +#line 2577 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } break; case 17: -#line 2580 "upb/json/parser.rl" +#line 2579 "upb/json/parser.rl" { start_timestamp_zone(parser, p); } break; case 18: -#line 2581 "upb/json/parser.rl" +#line 2580 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } break; case 19: -#line 2583 "upb/json/parser.rl" +#line 2582 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 20: -#line 2588 "upb/json/parser.rl" +#line 2587 "upb/json/parser.rl" { start_fieldmask_path_text(parser, p); } break; case 21: -#line 2589 "upb/json/parser.rl" +#line 2588 "upb/json/parser.rl" { end_fieldmask_path_text(parser, p); } break; case 22: -#line 2594 "upb/json/parser.rl" +#line 2593 "upb/json/parser.rl" { start_fieldmask_path(parser); } break; case 23: -#line 2595 "upb/json/parser.rl" +#line 2594 "upb/json/parser.rl" { end_fieldmask_path(parser); } break; case 24: -#line 2601 "upb/json/parser.rl" +#line 2600 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 25: -#line 2606 "upb/json/parser.rl" +#line 2605 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) { {stack[top++] = cs; cs = 47;goto _again;} @@ -2978,11 +2977,11 @@ _match: } break; case 26: -#line 2619 "upb/json/parser.rl" +#line 2618 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 78;goto _again;} } break; case 27: -#line 2624 "upb/json/parser.rl" +#line 2623 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_member(parser, p); @@ -2992,11 +2991,11 @@ _match: } break; case 28: -#line 2631 "upb/json/parser.rl" +#line 2630 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_membername(parser)); } break; case 29: -#line 2634 "upb/json/parser.rl" +#line 2633 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { end_any_member(parser, p); @@ -3006,7 +3005,7 @@ _match: } break; case 30: -#line 2645 "upb/json/parser.rl" +#line 2644 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_object(parser, p); @@ -3016,7 +3015,7 @@ _match: } break; case 31: -#line 2654 "upb/json/parser.rl" +#line 2653 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { CHECK_RETURN_TOP(end_any_object(parser, p)); @@ -3026,54 +3025,54 @@ _match: } break; case 32: -#line 2666 "upb/json/parser.rl" +#line 2665 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_array(parser)); } break; case 33: -#line 2670 "upb/json/parser.rl" +#line 2669 "upb/json/parser.rl" { end_array(parser); } break; case 34: -#line 2675 "upb/json/parser.rl" +#line 2674 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_number(parser, p)); } break; case 35: -#line 2676 "upb/json/parser.rl" +#line 2675 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 36: -#line 2678 "upb/json/parser.rl" +#line 2677 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_stringval(parser)); } break; case 37: -#line 2679 "upb/json/parser.rl" +#line 2678 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_stringval(parser)); } break; case 38: -#line 2681 "upb/json/parser.rl" +#line 2680 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 39: -#line 2683 "upb/json/parser.rl" +#line 2682 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 40: -#line 2685 "upb/json/parser.rl" +#line 2684 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 41: -#line 2687 "upb/json/parser.rl" +#line 2686 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_subobject_full(parser)); } break; case 42: -#line 2688 "upb/json/parser.rl" +#line 2687 "upb/json/parser.rl" { end_subobject_full(parser); } break; case 43: -#line 2693 "upb/json/parser.rl" +#line 2692 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 3077 "upb/json/parser.c" +#line 3076 "upb/json/parser.c" } } @@ -3090,32 +3089,32 @@ _again: while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 0: -#line 2525 "upb/json/parser.rl" +#line 2524 "upb/json/parser.rl" { p--; {cs = stack[--top]; if ( p == pe ) goto _test_eof; goto _again;} } break; case 35: -#line 2676 "upb/json/parser.rl" +#line 2675 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 38: -#line 2681 "upb/json/parser.rl" +#line 2680 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 39: -#line 2683 "upb/json/parser.rl" +#line 2682 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 40: -#line 2685 "upb/json/parser.rl" +#line 2684 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 42: -#line 2688 "upb/json/parser.rl" +#line 2687 "upb/json/parser.rl" { end_subobject_full(parser); } break; -#line 3119 "upb/json/parser.c" +#line 3118 "upb/json/parser.c" } } } @@ -3123,7 +3122,7 @@ goto _again;} } _out: {} } -#line 2721 "upb/json/parser.rl" +#line 2720 "upb/json/parser.rl" if (p != pe) { upb_status_seterrf(parser->status, "Parse error at '%.*s'\n", pe - p, p); @@ -3171,13 +3170,13 @@ static void json_parser_reset(upb_json_parser *p) { /* Emit Ragel initialization of the parser. */ -#line 3175 "upb/json/parser.c" +#line 3174 "upb/json/parser.c" { cs = json_start; top = 0; } -#line 2768 "upb/json/parser.rl" +#line 2767 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); diff --git a/upb/json/parser.rl b/upb/json/parser.rl index 75edb3b..3444257 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -1535,9 +1535,8 @@ static bool end_timestamp_base(upb_json_parser *p, const char *ptr) { timestamp_buf[UPB_TIMESTAMP_BASE_SIZE + 3] = 0; #if defined __MINGW32__ || defined __MINGW64__ - upb_status_seterrf( - &p->status, "error parsing timestamp: mingw doesn't support strptime"); - upb_env_reporterror(p->env, &p->status); + upb_status_seterrf(p->status, + "error parsing timestamp: mingw doesn't support strptime"); return false; #else /* Parse seconds */ -- cgit v1.2.3 From 56fefe2a5024f717abde1eb09942ba7e8dbf75e0 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Sun, 24 Mar 2019 11:54:57 -0700 Subject: Fix parsing repeated well known type value (#153) * Fix parsing repeated well known type value * Fix generated file * Fix comment --- upb/json/parser.c | 222 ++++++++++++++++++++++++----------------------------- upb/json/parser.rl | 106 ++++++++++--------------- 2 files changed, 140 insertions(+), 188 deletions(-) (limited to 'upb') diff --git a/upb/json/parser.c b/upb/json/parser.c index d8db7fe..b8b832e 100644 --- a/upb/json/parser.c +++ b/upb/json/parser.c @@ -186,6 +186,11 @@ typedef struct { /* The table mapping json name to fielddef for this message. */ const upb_strtable *name_table; + /* We are in a repeated-field context. We need this flag to decide whether to + * handle the array as a normal repeated field or a + * google.protobuf.ListValue/google.protobuf.Value. */ + bool is_repeated; + /* We are in a repeated-field context, ready to emit mapentries as * submessages. This flag alters the start-of-object (open-brace) behavior to * begin a sequence of mapentry messages rather than a single submessage. */ @@ -216,6 +221,19 @@ typedef struct { bool is_unknown_field; } upb_jsonparser_frame; +static void init_frame(upb_jsonparser_frame* frame) { + frame->m = NULL; + frame->f = NULL; + frame->name_table = NULL; + frame->is_repeated = false; + frame->is_map = false; + frame->is_mapentry = false; + frame->mapfield = NULL; + frame->is_any = false; + frame->any_frame = NULL; + frame->is_unknown_field = false; +} + struct upb_json_parser { upb_arena *arena; const upb_json_parsermethod *method; @@ -263,6 +281,13 @@ struct upb_json_parser { struct tm tm; }; +static upb_jsonparser_frame* start_jsonparser_frame(upb_json_parser *p) { + upb_jsonparser_frame *inner; + inner = p->top + 1; + init_frame(inner); + return inner; +} + struct upb_json_codecache { upb_arena *arena; upb_inttable methods; /* upb_msgdef* -> upb_json_parsermethod* */ @@ -1221,17 +1246,11 @@ static bool start_stringval(upb_json_parser *p) { /* Start a new parser frame: parser frames correspond one-to-one with * handler frames, and string events occur in a sub-frame. */ - inner = p->top + 1; + inner = start_jsonparser_frame(p); sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); upb_sink_startstr(p->top->sink, sel, 0, &inner->sink); inner->m = p->top->m; inner->f = p->top->f; - inner->name_table = NULL; - inner->is_map = false; - inner->is_mapentry = false; - inner->is_any = false; - inner->any_frame = NULL; - inner->is_unknown_field = false; p->top = inner; if (upb_fielddef_type(p->top->f) == UPB_TYPE_STRING) { @@ -1686,17 +1705,11 @@ static bool start_fieldmask_path(upb_json_parser *p) { /* Start a new parser frame: parser frames correspond one-to-one with * handler frames, and string events occur in a sub-frame. */ - inner = p->top + 1; + inner = start_jsonparser_frame(p); sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); upb_sink_startstr(p->top->sink, sel, 0, &inner->sink); inner->m = p->top->m; inner->f = p->top->f; - inner->name_table = NULL; - inner->is_map = false; - inner->is_mapentry = false; - inner->is_any = false; - inner->any_frame = NULL; - inner->is_unknown_field = false; p->top = inner; multipart_startaccum(p); @@ -1829,17 +1842,12 @@ static bool handle_mapentry(upb_json_parser *p) { mapfield = p->top->mapfield; mapentrymsg = upb_fielddef_msgsubdef(mapfield); - inner = p->top + 1; + inner = start_jsonparser_frame(p); p->top->f = mapfield; sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSUBMSG); upb_sink_startsubmsg(p->top->sink, sel, &inner->sink); inner->m = mapentrymsg; - inner->name_table = NULL; inner->mapfield = mapfield; - inner->is_map = false; - inner->is_any = false; - inner->any_frame = NULL; - inner->is_unknown_field = false; /* Don't set this to true *yet* -- we reuse parsing handlers below to push * the key field value to the sink, and these handlers will pop the frame @@ -1954,15 +1962,7 @@ static bool start_subobject(upb_json_parser *p) { upb_jsonparser_frame *inner; if (!check_stack(p)) return false; - inner = p->top + 1; - inner->m = NULL; - inner->f = NULL; - inner->is_map = false; - inner->is_mapentry = false; - inner->is_any = false; - inner->any_frame = NULL; - inner->is_unknown_field = false; - p->top = inner; + p->top = start_jsonparser_frame(p); return true; } @@ -1974,18 +1974,12 @@ static bool start_subobject(upb_json_parser *p) { * context. */ if (!check_stack(p)) return false; - inner = p->top + 1; + inner = start_jsonparser_frame(p); sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSEQ); upb_sink_startseq(p->top->sink, sel, &inner->sink); inner->m = upb_fielddef_msgsubdef(p->top->f); - inner->name_table = NULL; inner->mapfield = p->top->f; - inner->f = NULL; inner->is_map = true; - inner->is_mapentry = false; - inner->is_any = false; - inner->any_frame = NULL; - inner->is_unknown_field = false; p->top = inner; return true; @@ -1997,16 +1991,11 @@ static bool start_subobject(upb_json_parser *p) { * context. */ if (!check_stack(p)) return false; - inner = p->top + 1; - + inner = start_jsonparser_frame(p); sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSUBMSG); upb_sink_startsubmsg(p->top->sink, sel, &inner->sink); inner->m = upb_fielddef_msgsubdef(p->top->f); set_name_table(p, inner); - inner->f = NULL; - inner->is_map = false; - inner->is_mapentry = false; - inner->is_unknown_field = false; p->top = inner; if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) { @@ -2103,10 +2092,14 @@ static bool start_array(upb_json_parser *p) { } else { return false; } - } else if (is_wellknown_field(p, UPB_WELLKNOWN_LISTVALUE)) { + } else if (is_wellknown_field(p, UPB_WELLKNOWN_LISTVALUE) && + (!upb_fielddef_isseq(p->top->f) || + p->top->is_repeated)) { if (!start_subobject(p)) return false; start_listvalue_object(p); - } else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE)) { + } else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE) && + (!upb_fielddef_isseq(p->top->f) || + p->top->is_repeated)) { if (!start_subobject(p)) return false; start_value_object(p, VALUE_LISTVALUE); if (!start_subobject(p)) return false; @@ -2114,14 +2107,7 @@ static bool start_array(upb_json_parser *p) { } if (p->top->is_unknown_field) { - inner = p->top + 1; - inner->m = NULL; - inner->name_table = NULL; - inner->f = NULL; - inner->is_map = false; - inner->is_mapentry = false; - inner->is_any = false; - inner->any_frame = NULL; + inner = start_jsonparser_frame(p); inner->is_unknown_field = true; p->top = inner; @@ -2137,17 +2123,12 @@ static bool start_array(upb_json_parser *p) { if (!check_stack(p)) return false; - inner = p->top + 1; + inner = start_jsonparser_frame(p); sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSEQ); upb_sink_startseq(p->top->sink, sel, &inner->sink); inner->m = p->top->m; - inner->name_table = NULL; inner->f = p->top->f; - inner->is_map = false; - inner->is_mapentry = false; - inner->is_any = false; - inner->any_frame = NULL; - inner->is_unknown_field = false; + inner->is_repeated = true; p->top = inner; return true; @@ -2513,11 +2494,11 @@ static bool does_fieldmask_end(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 2695 "upb/json/parser.rl" +#line 2676 "upb/json/parser.rl" -#line 2521 "upb/json/parser.c" +#line 2502 "upb/json/parser.c" static const char _json_actions[] = { 0, 1, 0, 1, 1, 1, 3, 1, 4, 1, 6, 1, 7, 1, 8, 1, @@ -2769,7 +2750,7 @@ static const int json_en_value_machine = 78; static const int json_en_main = 1; -#line 2698 "upb/json/parser.rl" +#line 2679 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -2792,7 +2773,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 2796 "upb/json/parser.c" +#line 2777 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -2867,103 +2848,103 @@ _match: switch ( *_acts++ ) { case 1: -#line 2526 "upb/json/parser.rl" +#line 2507 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 2528 "upb/json/parser.rl" +#line 2509 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 23;goto _again;} } break; case 3: -#line 2532 "upb/json/parser.rl" +#line 2513 "upb/json/parser.rl" { start_text(parser, p); } break; case 4: -#line 2533 "upb/json/parser.rl" +#line 2514 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -#line 2539 "upb/json/parser.rl" +#line 2520 "upb/json/parser.rl" { start_hex(parser); } break; case 6: -#line 2540 "upb/json/parser.rl" +#line 2521 "upb/json/parser.rl" { hexdigit(parser, p); } break; case 7: -#line 2541 "upb/json/parser.rl" +#line 2522 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -#line 2547 "upb/json/parser.rl" +#line 2528 "upb/json/parser.rl" { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -#line 2553 "upb/json/parser.rl" +#line 2534 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 10: -#line 2565 "upb/json/parser.rl" +#line 2546 "upb/json/parser.rl" { start_duration_base(parser, p); } break; case 11: -#line 2566 "upb/json/parser.rl" +#line 2547 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_duration_base(parser, p)); } break; case 12: -#line 2568 "upb/json/parser.rl" +#line 2549 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 13: -#line 2573 "upb/json/parser.rl" +#line 2554 "upb/json/parser.rl" { start_timestamp_base(parser, p); } break; case 14: -#line 2574 "upb/json/parser.rl" +#line 2555 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_base(parser, p)); } break; case 15: -#line 2576 "upb/json/parser.rl" +#line 2557 "upb/json/parser.rl" { start_timestamp_fraction(parser, p); } break; case 16: -#line 2577 "upb/json/parser.rl" +#line 2558 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } break; case 17: -#line 2579 "upb/json/parser.rl" +#line 2560 "upb/json/parser.rl" { start_timestamp_zone(parser, p); } break; case 18: -#line 2580 "upb/json/parser.rl" +#line 2561 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } break; case 19: -#line 2582 "upb/json/parser.rl" +#line 2563 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 20: -#line 2587 "upb/json/parser.rl" +#line 2568 "upb/json/parser.rl" { start_fieldmask_path_text(parser, p); } break; case 21: -#line 2588 "upb/json/parser.rl" +#line 2569 "upb/json/parser.rl" { end_fieldmask_path_text(parser, p); } break; case 22: -#line 2593 "upb/json/parser.rl" +#line 2574 "upb/json/parser.rl" { start_fieldmask_path(parser); } break; case 23: -#line 2594 "upb/json/parser.rl" +#line 2575 "upb/json/parser.rl" { end_fieldmask_path(parser); } break; case 24: -#line 2600 "upb/json/parser.rl" +#line 2581 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 25: -#line 2605 "upb/json/parser.rl" +#line 2586 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) { {stack[top++] = cs; cs = 47;goto _again;} @@ -2977,11 +2958,11 @@ _match: } break; case 26: -#line 2618 "upb/json/parser.rl" +#line 2599 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 78;goto _again;} } break; case 27: -#line 2623 "upb/json/parser.rl" +#line 2604 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_member(parser, p); @@ -2991,11 +2972,11 @@ _match: } break; case 28: -#line 2630 "upb/json/parser.rl" +#line 2611 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_membername(parser)); } break; case 29: -#line 2633 "upb/json/parser.rl" +#line 2614 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { end_any_member(parser, p); @@ -3005,7 +2986,7 @@ _match: } break; case 30: -#line 2644 "upb/json/parser.rl" +#line 2625 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_object(parser, p); @@ -3015,7 +2996,7 @@ _match: } break; case 31: -#line 2653 "upb/json/parser.rl" +#line 2634 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { CHECK_RETURN_TOP(end_any_object(parser, p)); @@ -3025,54 +3006,54 @@ _match: } break; case 32: -#line 2665 "upb/json/parser.rl" +#line 2646 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_array(parser)); } break; case 33: -#line 2669 "upb/json/parser.rl" +#line 2650 "upb/json/parser.rl" { end_array(parser); } break; case 34: -#line 2674 "upb/json/parser.rl" +#line 2655 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_number(parser, p)); } break; case 35: -#line 2675 "upb/json/parser.rl" +#line 2656 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 36: -#line 2677 "upb/json/parser.rl" +#line 2658 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_stringval(parser)); } break; case 37: -#line 2678 "upb/json/parser.rl" +#line 2659 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_stringval(parser)); } break; case 38: -#line 2680 "upb/json/parser.rl" +#line 2661 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 39: -#line 2682 "upb/json/parser.rl" +#line 2663 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 40: -#line 2684 "upb/json/parser.rl" +#line 2665 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 41: -#line 2686 "upb/json/parser.rl" +#line 2667 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_subobject_full(parser)); } break; case 42: -#line 2687 "upb/json/parser.rl" +#line 2668 "upb/json/parser.rl" { end_subobject_full(parser); } break; case 43: -#line 2692 "upb/json/parser.rl" +#line 2673 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 3076 "upb/json/parser.c" +#line 3057 "upb/json/parser.c" } } @@ -3089,32 +3070,32 @@ _again: while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 0: -#line 2524 "upb/json/parser.rl" +#line 2505 "upb/json/parser.rl" { p--; {cs = stack[--top]; if ( p == pe ) goto _test_eof; goto _again;} } break; case 35: -#line 2675 "upb/json/parser.rl" +#line 2656 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 38: -#line 2680 "upb/json/parser.rl" +#line 2661 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 39: -#line 2682 "upb/json/parser.rl" +#line 2663 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 40: -#line 2684 "upb/json/parser.rl" +#line 2665 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 42: -#line 2687 "upb/json/parser.rl" +#line 2668 "upb/json/parser.rl" { end_subobject_full(parser); } break; -#line 3118 "upb/json/parser.c" +#line 3099 "upb/json/parser.c" } } } @@ -3122,7 +3103,7 @@ goto _again;} } _out: {} } -#line 2720 "upb/json/parser.rl" +#line 2701 "upb/json/parser.rl" if (p != pe) { upb_status_seterrf(parser->status, "Parse error at '%.*s'\n", pe - p, p); @@ -3161,22 +3142,17 @@ static void json_parser_reset(upb_json_parser *p) { int top; p->top = p->stack; - p->top->f = NULL; - p->top->is_map = false; - p->top->is_mapentry = false; - p->top->is_any = false; - p->top->any_frame = NULL; - p->top->is_unknown_field = false; + init_frame(p->top); /* Emit Ragel initialization of the parser. */ -#line 3174 "upb/json/parser.c" +#line 3150 "upb/json/parser.c" { cs = json_start; top = 0; } -#line 2767 "upb/json/parser.rl" +#line 2743 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); diff --git a/upb/json/parser.rl b/upb/json/parser.rl index 3444257..23e61b7 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -184,6 +184,11 @@ typedef struct { /* The table mapping json name to fielddef for this message. */ const upb_strtable *name_table; + /* We are in a repeated-field context. We need this flag to decide whether to + * handle the array as a normal repeated field or a + * google.protobuf.ListValue/google.protobuf.Value. */ + bool is_repeated; + /* We are in a repeated-field context, ready to emit mapentries as * submessages. This flag alters the start-of-object (open-brace) behavior to * begin a sequence of mapentry messages rather than a single submessage. */ @@ -214,6 +219,19 @@ typedef struct { bool is_unknown_field; } upb_jsonparser_frame; +static void init_frame(upb_jsonparser_frame* frame) { + frame->m = NULL; + frame->f = NULL; + frame->name_table = NULL; + frame->is_repeated = false; + frame->is_map = false; + frame->is_mapentry = false; + frame->mapfield = NULL; + frame->is_any = false; + frame->any_frame = NULL; + frame->is_unknown_field = false; +} + struct upb_json_parser { upb_arena *arena; const upb_json_parsermethod *method; @@ -261,6 +279,13 @@ struct upb_json_parser { struct tm tm; }; +static upb_jsonparser_frame* start_jsonparser_frame(upb_json_parser *p) { + upb_jsonparser_frame *inner; + inner = p->top + 1; + init_frame(inner); + return inner; +} + struct upb_json_codecache { upb_arena *arena; upb_inttable methods; /* upb_msgdef* -> upb_json_parsermethod* */ @@ -1219,17 +1244,11 @@ static bool start_stringval(upb_json_parser *p) { /* Start a new parser frame: parser frames correspond one-to-one with * handler frames, and string events occur in a sub-frame. */ - inner = p->top + 1; + inner = start_jsonparser_frame(p); sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); upb_sink_startstr(p->top->sink, sel, 0, &inner->sink); inner->m = p->top->m; inner->f = p->top->f; - inner->name_table = NULL; - inner->is_map = false; - inner->is_mapentry = false; - inner->is_any = false; - inner->any_frame = NULL; - inner->is_unknown_field = false; p->top = inner; if (upb_fielddef_type(p->top->f) == UPB_TYPE_STRING) { @@ -1684,17 +1703,11 @@ static bool start_fieldmask_path(upb_json_parser *p) { /* Start a new parser frame: parser frames correspond one-to-one with * handler frames, and string events occur in a sub-frame. */ - inner = p->top + 1; + inner = start_jsonparser_frame(p); sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR); upb_sink_startstr(p->top->sink, sel, 0, &inner->sink); inner->m = p->top->m; inner->f = p->top->f; - inner->name_table = NULL; - inner->is_map = false; - inner->is_mapentry = false; - inner->is_any = false; - inner->any_frame = NULL; - inner->is_unknown_field = false; p->top = inner; multipart_startaccum(p); @@ -1827,17 +1840,12 @@ static bool handle_mapentry(upb_json_parser *p) { mapfield = p->top->mapfield; mapentrymsg = upb_fielddef_msgsubdef(mapfield); - inner = p->top + 1; + inner = start_jsonparser_frame(p); p->top->f = mapfield; sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSUBMSG); upb_sink_startsubmsg(p->top->sink, sel, &inner->sink); inner->m = mapentrymsg; - inner->name_table = NULL; inner->mapfield = mapfield; - inner->is_map = false; - inner->is_any = false; - inner->any_frame = NULL; - inner->is_unknown_field = false; /* Don't set this to true *yet* -- we reuse parsing handlers below to push * the key field value to the sink, and these handlers will pop the frame @@ -1952,15 +1960,7 @@ static bool start_subobject(upb_json_parser *p) { upb_jsonparser_frame *inner; if (!check_stack(p)) return false; - inner = p->top + 1; - inner->m = NULL; - inner->f = NULL; - inner->is_map = false; - inner->is_mapentry = false; - inner->is_any = false; - inner->any_frame = NULL; - inner->is_unknown_field = false; - p->top = inner; + p->top = start_jsonparser_frame(p); return true; } @@ -1972,18 +1972,12 @@ static bool start_subobject(upb_json_parser *p) { * context. */ if (!check_stack(p)) return false; - inner = p->top + 1; + inner = start_jsonparser_frame(p); sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSEQ); upb_sink_startseq(p->top->sink, sel, &inner->sink); inner->m = upb_fielddef_msgsubdef(p->top->f); - inner->name_table = NULL; inner->mapfield = p->top->f; - inner->f = NULL; inner->is_map = true; - inner->is_mapentry = false; - inner->is_any = false; - inner->any_frame = NULL; - inner->is_unknown_field = false; p->top = inner; return true; @@ -1995,16 +1989,11 @@ static bool start_subobject(upb_json_parser *p) { * context. */ if (!check_stack(p)) return false; - inner = p->top + 1; - + inner = start_jsonparser_frame(p); sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSUBMSG); upb_sink_startsubmsg(p->top->sink, sel, &inner->sink); inner->m = upb_fielddef_msgsubdef(p->top->f); set_name_table(p, inner); - inner->f = NULL; - inner->is_map = false; - inner->is_mapentry = false; - inner->is_unknown_field = false; p->top = inner; if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) { @@ -2101,10 +2090,14 @@ static bool start_array(upb_json_parser *p) { } else { return false; } - } else if (is_wellknown_field(p, UPB_WELLKNOWN_LISTVALUE)) { + } else if (is_wellknown_field(p, UPB_WELLKNOWN_LISTVALUE) && + (!upb_fielddef_isseq(p->top->f) || + p->top->is_repeated)) { if (!start_subobject(p)) return false; start_listvalue_object(p); - } else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE)) { + } else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE) && + (!upb_fielddef_isseq(p->top->f) || + p->top->is_repeated)) { if (!start_subobject(p)) return false; start_value_object(p, VALUE_LISTVALUE); if (!start_subobject(p)) return false; @@ -2112,14 +2105,7 @@ static bool start_array(upb_json_parser *p) { } if (p->top->is_unknown_field) { - inner = p->top + 1; - inner->m = NULL; - inner->name_table = NULL; - inner->f = NULL; - inner->is_map = false; - inner->is_mapentry = false; - inner->is_any = false; - inner->any_frame = NULL; + inner = start_jsonparser_frame(p); inner->is_unknown_field = true; p->top = inner; @@ -2135,17 +2121,12 @@ static bool start_array(upb_json_parser *p) { if (!check_stack(p)) return false; - inner = p->top + 1; + inner = start_jsonparser_frame(p); sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSEQ); upb_sink_startseq(p->top->sink, sel, &inner->sink); inner->m = p->top->m; - inner->name_table = NULL; inner->f = p->top->f; - inner->is_map = false; - inner->is_mapentry = false; - inner->is_any = false; - inner->any_frame = NULL; - inner->is_unknown_field = false; + inner->is_repeated = true; p->top = inner; return true; @@ -2755,12 +2736,7 @@ static void json_parser_reset(upb_json_parser *p) { int top; p->top = p->stack; - p->top->f = NULL; - p->top->is_map = false; - p->top->is_mapentry = false; - p->top->is_any = false; - p->top->any_frame = NULL; - p->top->is_unknown_field = false; + init_frame(p->top); /* Emit Ragel initialization of the parser. */ %% write init; -- cgit v1.2.3 From 3c4f0347f91f0959202e7b7dbf49dd9ea266f8a0 Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Sun, 17 Mar 2019 05:32:22 +0000 Subject: Replace strptime with custom implementation --- upb/json/parser.c | 409 +++++++++++++++++++++++++++++++++-------------------- upb/json/parser.rl | 145 ++++++++++++++----- 2 files changed, 362 insertions(+), 192 deletions(-) (limited to 'upb') diff --git a/upb/json/parser.c b/upb/json/parser.c index b8b832e..db7f14f 100644 --- a/upb/json/parser.c +++ b/upb/json/parser.c @@ -21,9 +21,6 @@ ** - handling of keys/escape-sequences/etc that span input buffers. */ -/* Need to define _XOPEN_SOURCE before any include to make strptime work. */ -#define _XOPEN_SOURCE 700 - #include #include #include @@ -1533,47 +1530,100 @@ static bool end_duration_base(upb_json_parser *p, const char *ptr) { return true; } -static void start_timestamp_base(upb_json_parser *p, const char *ptr) { +static int parse_timestamp_number(upb_json_parser *p) { + size_t len; + const char *buf; + char *end; + int val; + + /* atoi() and friends unfortunately do not support specifying the length of + * the input string, so we need to force a copy into a NULL-terminated buffer. */ + multipart_text(p, "\0", 1, false); + + buf = accumulate_getptr(p, &len); + val = atoi(buf); + multipart_end(p); + multipart_startaccum(p); + + return val; +} + +static void start_year(upb_json_parser *p, const char *ptr) { capture_begin(p, ptr); } -#define UPB_TIMESTAMP_BASE_SIZE 19 +static bool end_year(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { + return false; + } + p->tm.tm_year = parse_timestamp_number(p) - 1900; + return true; +} -static bool end_timestamp_base(upb_json_parser *p, const char *ptr) { - size_t len; - const char *buf; - /* 3 for GMT and 1 for ending 0 */ - char timestamp_buf[UPB_TIMESTAMP_BASE_SIZE + 4]; +static void start_month(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} +static bool end_month(upb_json_parser *p, const char *ptr) { if (!capture_end(p, ptr)) { return false; } + p->tm.tm_mon = parse_timestamp_number(p) - 1; + return true; +} - buf = accumulate_getptr(p, &len); - UPB_ASSERT(len == UPB_TIMESTAMP_BASE_SIZE); - memcpy(timestamp_buf, buf, UPB_TIMESTAMP_BASE_SIZE); - memcpy(timestamp_buf + UPB_TIMESTAMP_BASE_SIZE, "GMT", 3); - timestamp_buf[UPB_TIMESTAMP_BASE_SIZE + 3] = 0; +static void start_day(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} -#if defined __MINGW32__ || defined __MINGW64__ - upb_status_seterrf(p->status, - "error parsing timestamp: mingw doesn't support strptime"); - return false; -#else - /* Parse seconds */ - if (strptime(timestamp_buf, "%FT%H:%M:%S%Z", &p->tm) == NULL) { - upb_status_seterrf(p->status, "error parsing timestamp: %s", buf); +static bool end_day(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { return false; } -#endif + p->tm.tm_mday = parse_timestamp_number(p); + return true; +} - /* Clean up buffer */ - multipart_end(p); - multipart_startaccum(p); +static void start_hour(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} + +static bool end_hour(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { + return false; + } + p->tm.tm_hour = parse_timestamp_number(p); + return true; +} + +static void start_minute(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} + +static bool end_minute(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { + return false; + } + p->tm.tm_min = parse_timestamp_number(p); + return true; +} + +static void start_second(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} +static bool end_second(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { + return false; + } + p->tm.tm_sec = parse_timestamp_number(p); return true; } +static void start_timestamp_base(upb_json_parser *p) { + memset(&p->tm, 0, sizeof(struct tm)); +} + static void start_timestamp_fraction(upb_json_parser *p, const char *ptr) { capture_begin(p, ptr); } @@ -2494,27 +2544,30 @@ static bool does_fieldmask_end(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 2676 "upb/json/parser.rl" +#line 2749 "upb/json/parser.rl" -#line 2502 "upb/json/parser.c" +#line 2552 "upb/json/parser.c" static const char _json_actions[] = { 0, 1, 0, 1, 1, 1, 3, 1, 4, 1, 6, 1, 7, 1, 8, 1, - 9, 1, 10, 1, 11, 1, 12, 1, - 13, 1, 24, 1, 26, 1, 28, 1, - 29, 1, 31, 1, 32, 1, 33, 1, - 35, 1, 37, 1, 38, 1, 39, 1, - 40, 1, 42, 1, 43, 2, 4, 9, - 2, 5, 6, 2, 7, 3, 2, 7, - 9, 2, 14, 15, 2, 16, 17, 2, - 18, 19, 2, 21, 23, 2, 22, 20, - 2, 27, 25, 2, 29, 31, 2, 34, - 2, 2, 35, 43, 2, 36, 25, 2, - 38, 43, 2, 39, 43, 2, 40, 43, - 2, 41, 30, 2, 42, 43, 3, 21, - 23, 24, 4, 14, 15, 16, 17 + 9, 1, 11, 1, 12, 1, 13, 1, + 14, 1, 15, 1, 16, 1, 17, 1, + 18, 1, 19, 1, 20, 1, 22, 1, + 23, 1, 24, 1, 35, 1, 37, 1, + 39, 1, 40, 1, 42, 1, 43, 1, + 44, 1, 46, 1, 48, 1, 49, 1, + 50, 1, 51, 1, 53, 1, 54, 2, + 4, 9, 2, 5, 6, 2, 7, 3, + 2, 7, 9, 2, 21, 26, 2, 25, + 10, 2, 27, 28, 2, 29, 30, 2, + 32, 34, 2, 33, 31, 2, 38, 36, + 2, 40, 42, 2, 45, 2, 2, 46, + 54, 2, 47, 36, 2, 49, 54, 2, + 50, 54, 2, 51, 54, 2, 52, 41, + 2, 53, 54, 3, 32, 34, 35, 4, + 21, 26, 27, 28 }; static const short _json_key_offsets[] = { @@ -2698,30 +2751,30 @@ static const char _json_trans_targs[] = { 106 }; -static const char _json_trans_actions[] = { - 0, 0, 92, 86, 35, 0, 0, 0, - 104, 41, 27, 0, 37, 0, 0, 0, +static const unsigned char _json_trans_actions[] = { + 0, 0, 113, 107, 53, 0, 0, 0, + 125, 59, 45, 0, 55, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 80, 33, 29, 0, 0, 27, - 31, 31, 83, 0, 0, 0, 0, 0, + 0, 0, 101, 51, 47, 0, 0, 45, + 49, 49, 104, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 5, 15, - 0, 0, 53, 7, 13, 0, 56, 9, - 9, 9, 59, 62, 11, 17, 17, 17, - 0, 0, 0, 19, 0, 21, 23, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 114, 65, 114, 0, 0, 0, 0, - 0, 71, 0, 68, 68, 77, 25, 0, - 110, 74, 92, 86, 35, 0, 0, 0, - 104, 41, 51, 89, 27, 0, 37, 0, - 0, 0, 0, 0, 0, 98, 0, 0, - 0, 101, 0, 0, 0, 95, 0, 80, - 33, 29, 0, 0, 27, 31, 31, 83, - 0, 0, 107, 0, 39, 45, 47, 43, - 49 + 0, 0, 71, 7, 13, 0, 74, 9, + 9, 9, 77, 80, 11, 37, 37, 37, + 0, 0, 0, 39, 0, 41, 86, 0, + 0, 0, 17, 19, 0, 21, 23, 0, + 25, 27, 0, 29, 31, 0, 33, 35, + 0, 135, 83, 135, 0, 0, 0, 0, + 0, 92, 0, 89, 89, 98, 43, 0, + 131, 95, 113, 107, 53, 0, 0, 0, + 125, 59, 69, 110, 45, 0, 55, 0, + 0, 0, 0, 0, 0, 119, 0, 0, + 0, 122, 0, 0, 0, 116, 0, 101, + 51, 47, 0, 0, 45, 49, 49, 104, + 0, 0, 128, 0, 57, 63, 65, 61, + 67 }; -static const char _json_eof_actions[] = { +static const unsigned char _json_eof_actions[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -2735,7 +2788,7 @@ static const char _json_eof_actions[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 39, 45, 47, 43, 49, + 0, 0, 0, 57, 63, 65, 61, 67, 0, 0, 0, 0, 0, 0 }; @@ -2750,7 +2803,7 @@ static const int json_en_value_machine = 78; static const int json_en_main = 1; -#line 2679 "upb/json/parser.rl" +#line 2752 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -2773,7 +2826,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 2777 "upb/json/parser.c" +#line 2830 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -2848,103 +2901,147 @@ _match: switch ( *_acts++ ) { case 1: -#line 2507 "upb/json/parser.rl" +#line 2557 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 2509 "upb/json/parser.rl" +#line 2559 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 23;goto _again;} } break; case 3: -#line 2513 "upb/json/parser.rl" +#line 2563 "upb/json/parser.rl" { start_text(parser, p); } break; case 4: -#line 2514 "upb/json/parser.rl" +#line 2564 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -#line 2520 "upb/json/parser.rl" +#line 2570 "upb/json/parser.rl" { start_hex(parser); } break; case 6: -#line 2521 "upb/json/parser.rl" +#line 2571 "upb/json/parser.rl" { hexdigit(parser, p); } break; case 7: -#line 2522 "upb/json/parser.rl" +#line 2572 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -#line 2528 "upb/json/parser.rl" +#line 2578 "upb/json/parser.rl" { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -#line 2534 "upb/json/parser.rl" +#line 2584 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 10: -#line 2546 "upb/json/parser.rl" - { start_duration_base(parser, p); } +#line 2589 "upb/json/parser.rl" + { start_year(parser, p); } break; case 11: -#line 2547 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_duration_base(parser, p)); } +#line 2590 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_year(parser, p)); } break; case 12: -#line 2549 "upb/json/parser.rl" - { p--; {cs = stack[--top]; goto _again;} } +#line 2594 "upb/json/parser.rl" + { start_month(parser, p); } break; case 13: -#line 2554 "upb/json/parser.rl" - { start_timestamp_base(parser, p); } +#line 2595 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_month(parser, p)); } break; case 14: -#line 2555 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_timestamp_base(parser, p)); } +#line 2599 "upb/json/parser.rl" + { start_day(parser, p); } break; case 15: -#line 2557 "upb/json/parser.rl" - { start_timestamp_fraction(parser, p); } +#line 2600 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_day(parser, p)); } break; case 16: -#line 2558 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } +#line 2604 "upb/json/parser.rl" + { start_hour(parser, p); } break; case 17: -#line 2560 "upb/json/parser.rl" - { start_timestamp_zone(parser, p); } +#line 2605 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_hour(parser, p)); } break; case 18: -#line 2561 "upb/json/parser.rl" - { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } +#line 2609 "upb/json/parser.rl" + { start_minute(parser, p); } break; case 19: -#line 2563 "upb/json/parser.rl" - { p--; {cs = stack[--top]; goto _again;} } +#line 2610 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_minute(parser, p)); } break; case 20: -#line 2568 "upb/json/parser.rl" - { start_fieldmask_path_text(parser, p); } +#line 2614 "upb/json/parser.rl" + { start_second(parser, p); } break; case 21: -#line 2569 "upb/json/parser.rl" - { end_fieldmask_path_text(parser, p); } +#line 2615 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_second(parser, p)); } break; case 22: -#line 2574 "upb/json/parser.rl" - { start_fieldmask_path(parser); } +#line 2620 "upb/json/parser.rl" + { start_duration_base(parser, p); } break; case 23: -#line 2575 "upb/json/parser.rl" - { end_fieldmask_path(parser); } +#line 2621 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_duration_base(parser, p)); } break; case 24: -#line 2581 "upb/json/parser.rl" +#line 2623 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 25: -#line 2586 "upb/json/parser.rl" +#line 2628 "upb/json/parser.rl" + { start_timestamp_base(parser); } + break; + case 26: +#line 2630 "upb/json/parser.rl" + { start_timestamp_fraction(parser, p); } + break; + case 27: +#line 2631 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } + break; + case 28: +#line 2633 "upb/json/parser.rl" + { start_timestamp_zone(parser, p); } + break; + case 29: +#line 2634 "upb/json/parser.rl" + { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } + break; + case 30: +#line 2636 "upb/json/parser.rl" + { p--; {cs = stack[--top]; goto _again;} } + break; + case 31: +#line 2641 "upb/json/parser.rl" + { start_fieldmask_path_text(parser, p); } + break; + case 32: +#line 2642 "upb/json/parser.rl" + { end_fieldmask_path_text(parser, p); } + break; + case 33: +#line 2647 "upb/json/parser.rl" + { start_fieldmask_path(parser); } + break; + case 34: +#line 2648 "upb/json/parser.rl" + { end_fieldmask_path(parser); } + break; + case 35: +#line 2654 "upb/json/parser.rl" + { p--; {cs = stack[--top]; goto _again;} } + break; + case 36: +#line 2659 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) { {stack[top++] = cs; cs = 47;goto _again;} @@ -2957,12 +3054,12 @@ _match: } } break; - case 26: -#line 2599 "upb/json/parser.rl" + case 37: +#line 2672 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 78;goto _again;} } break; - case 27: -#line 2604 "upb/json/parser.rl" + case 38: +#line 2677 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_member(parser, p); @@ -2971,12 +3068,12 @@ _match: } } break; - case 28: -#line 2611 "upb/json/parser.rl" + case 39: +#line 2684 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_membername(parser)); } break; - case 29: -#line 2614 "upb/json/parser.rl" + case 40: +#line 2687 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { end_any_member(parser, p); @@ -2985,8 +3082,8 @@ _match: } } break; - case 30: -#line 2625 "upb/json/parser.rl" + case 41: +#line 2698 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_object(parser, p); @@ -2995,8 +3092,8 @@ _match: } } break; - case 31: -#line 2634 "upb/json/parser.rl" + case 42: +#line 2707 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { CHECK_RETURN_TOP(end_any_object(parser, p)); @@ -3005,55 +3102,55 @@ _match: } } break; - case 32: -#line 2646 "upb/json/parser.rl" + case 43: +#line 2719 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_array(parser)); } break; - case 33: -#line 2650 "upb/json/parser.rl" + case 44: +#line 2723 "upb/json/parser.rl" { end_array(parser); } break; - case 34: -#line 2655 "upb/json/parser.rl" + case 45: +#line 2728 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_number(parser, p)); } break; - case 35: -#line 2656 "upb/json/parser.rl" + case 46: +#line 2729 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; - case 36: -#line 2658 "upb/json/parser.rl" + case 47: +#line 2731 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_stringval(parser)); } break; - case 37: -#line 2659 "upb/json/parser.rl" + case 48: +#line 2732 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_stringval(parser)); } break; - case 38: -#line 2661 "upb/json/parser.rl" + case 49: +#line 2734 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; - case 39: -#line 2663 "upb/json/parser.rl" + case 50: +#line 2736 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; - case 40: -#line 2665 "upb/json/parser.rl" + case 51: +#line 2738 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; - case 41: -#line 2667 "upb/json/parser.rl" + case 52: +#line 2740 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_subobject_full(parser)); } break; - case 42: -#line 2668 "upb/json/parser.rl" + case 53: +#line 2741 "upb/json/parser.rl" { end_subobject_full(parser); } break; - case 43: -#line 2673 "upb/json/parser.rl" + case 54: +#line 2746 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 3057 "upb/json/parser.c" +#line 3154 "upb/json/parser.c" } } @@ -3070,32 +3167,32 @@ _again: while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 0: -#line 2505 "upb/json/parser.rl" +#line 2555 "upb/json/parser.rl" { p--; {cs = stack[--top]; if ( p == pe ) goto _test_eof; goto _again;} } break; - case 35: -#line 2656 "upb/json/parser.rl" + case 46: +#line 2729 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; - case 38: -#line 2661 "upb/json/parser.rl" + case 49: +#line 2734 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; - case 39: -#line 2663 "upb/json/parser.rl" + case 50: +#line 2736 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; - case 40: -#line 2665 "upb/json/parser.rl" + case 51: +#line 2738 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; - case 42: -#line 2668 "upb/json/parser.rl" + case 53: +#line 2741 "upb/json/parser.rl" { end_subobject_full(parser); } break; -#line 3099 "upb/json/parser.c" +#line 3196 "upb/json/parser.c" } } } @@ -3103,7 +3200,7 @@ goto _again;} } _out: {} } -#line 2701 "upb/json/parser.rl" +#line 2774 "upb/json/parser.rl" if (p != pe) { upb_status_seterrf(parser->status, "Parse error at '%.*s'\n", pe - p, p); @@ -3146,13 +3243,13 @@ static void json_parser_reset(upb_json_parser *p) { /* Emit Ragel initialization of the parser. */ -#line 3150 "upb/json/parser.c" +#line 3247 "upb/json/parser.c" { cs = json_start; top = 0; } -#line 2743 "upb/json/parser.rl" +#line 2816 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); diff --git a/upb/json/parser.rl b/upb/json/parser.rl index 23e61b7..e7c456a 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -19,9 +19,6 @@ ** - handling of keys/escape-sequences/etc that span input buffers. */ -/* Need to define _XOPEN_SOURCE before any include to make strptime work. */ -#define _XOPEN_SOURCE 700 - #include #include #include @@ -1531,47 +1528,100 @@ static bool end_duration_base(upb_json_parser *p, const char *ptr) { return true; } -static void start_timestamp_base(upb_json_parser *p, const char *ptr) { +static int parse_timestamp_number(upb_json_parser *p) { + size_t len; + const char *buf; + char *end; + int val; + + /* atoi() and friends unfortunately do not support specifying the length of + * the input string, so we need to force a copy into a NULL-terminated buffer. */ + multipart_text(p, "\0", 1, false); + + buf = accumulate_getptr(p, &len); + val = atoi(buf); + multipart_end(p); + multipart_startaccum(p); + + return val; +} + +static void start_year(upb_json_parser *p, const char *ptr) { capture_begin(p, ptr); } -#define UPB_TIMESTAMP_BASE_SIZE 19 +static bool end_year(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { + return false; + } + p->tm.tm_year = parse_timestamp_number(p) - 1900; + return true; +} -static bool end_timestamp_base(upb_json_parser *p, const char *ptr) { - size_t len; - const char *buf; - /* 3 for GMT and 1 for ending 0 */ - char timestamp_buf[UPB_TIMESTAMP_BASE_SIZE + 4]; +static void start_month(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} +static bool end_month(upb_json_parser *p, const char *ptr) { if (!capture_end(p, ptr)) { return false; } + p->tm.tm_mon = parse_timestamp_number(p) - 1; + return true; +} - buf = accumulate_getptr(p, &len); - UPB_ASSERT(len == UPB_TIMESTAMP_BASE_SIZE); - memcpy(timestamp_buf, buf, UPB_TIMESTAMP_BASE_SIZE); - memcpy(timestamp_buf + UPB_TIMESTAMP_BASE_SIZE, "GMT", 3); - timestamp_buf[UPB_TIMESTAMP_BASE_SIZE + 3] = 0; +static void start_day(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} -#if defined __MINGW32__ || defined __MINGW64__ - upb_status_seterrf(p->status, - "error parsing timestamp: mingw doesn't support strptime"); - return false; -#else - /* Parse seconds */ - if (strptime(timestamp_buf, "%FT%H:%M:%S%Z", &p->tm) == NULL) { - upb_status_seterrf(p->status, "error parsing timestamp: %s", buf); +static bool end_day(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { return false; } -#endif + p->tm.tm_mday = parse_timestamp_number(p); + return true; +} - /* Clean up buffer */ - multipart_end(p); - multipart_startaccum(p); +static void start_hour(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} +static bool end_hour(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { + return false; + } + p->tm.tm_hour = parse_timestamp_number(p); return true; } +static void start_minute(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} + +static bool end_minute(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { + return false; + } + p->tm.tm_min = parse_timestamp_number(p); + return true; +} + +static void start_second(upb_json_parser *p, const char *ptr) { + capture_begin(p, ptr); +} + +static bool end_second(upb_json_parser *p, const char *ptr) { + if (!capture_end(p, ptr)) { + return false; + } + p->tm.tm_sec = parse_timestamp_number(p); + return true; +} + +static void start_timestamp_base(upb_json_parser *p) { + memset(&p->tm, 0, sizeof(struct tm)); +} + static void start_timestamp_fraction(upb_json_parser *p, const char *ptr) { capture_begin(p, ptr); } @@ -2534,12 +2584,36 @@ static bool does_fieldmask_end(upb_json_parser *p) { @{ fhold; fret; } ; - year = digit digit digit digit; - month = digit digit; - day = digit digit; - hour = digit digit; - minute = digit digit; - second = digit digit; + year = + (digit digit digit digit) + >{ start_year(parser, p); } + %{ CHECK_RETURN_TOP(end_year(parser, p)); } + ; + month = + (digit digit) + >{ start_month(parser, p); } + %{ CHECK_RETURN_TOP(end_month(parser, p)); } + ; + day = + (digit digit) + >{ start_day(parser, p); } + %{ CHECK_RETURN_TOP(end_day(parser, p)); } + ; + hour = + (digit digit) + >{ start_hour(parser, p); } + %{ CHECK_RETURN_TOP(end_hour(parser, p)); } + ; + minute = + (digit digit) + >{ start_minute(parser, p); } + %{ CHECK_RETURN_TOP(end_minute(parser, p)); } + ; + second = + (digit digit) + >{ start_second(parser, p); } + %{ CHECK_RETURN_TOP(end_second(parser, p)); } + ; duration_machine := ("-"? integer decimal?) @@ -2551,12 +2625,11 @@ static bool does_fieldmask_end(upb_json_parser *p) { timestamp_machine := (year "-" month "-" day "T" hour ":" minute ":" second) - >{ start_timestamp_base(parser, p); } - %{ CHECK_RETURN_TOP(end_timestamp_base(parser, p)); } + >{ start_timestamp_base(parser); } ("." digit+)? >{ start_timestamp_fraction(parser, p); } %{ CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } - ([+\-] hour ":00" | "Z") + ([+\-] digit digit ":00" | "Z") >{ start_timestamp_zone(parser, p); } %{ CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } '"' -- cgit v1.2.3 From a8a0bc73b981bd94509c180ad8cac36143c479ec Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Mon, 25 Mar 2019 10:49:07 -0700 Subject: Allow bytes field to be longer than 16000 bytes (#157) --- upb/json/printer.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'upb') diff --git a/upb/json/printer.c b/upb/json/printer.c index 26cc020..72f26b2 100644 --- a/upb/json/printer.c +++ b/upb/json/printer.c @@ -450,9 +450,14 @@ static size_t putbytes(void *closure, const void *handler_data, const char *str, UPB_UNUSED(handler_data); UPB_UNUSED(handle); + print_data(p, "\"", 1); + while (remaining > 2) { - /* TODO(haberman): handle encoded lengths > sizeof(data) */ - UPB_ASSERT((limit - to) >= 4); + if (limit - to < 4) { + bytes = to - data; + putstring(p, data, bytes); + to = data; + } to[0] = base64[from[0] >> 2]; to[1] = base64[((from[0] & 0x3) << 4) | (from[1] >> 4)]; @@ -484,7 +489,6 @@ static size_t putbytes(void *closure, const void *handler_data, const char *str, } bytes = to - data; - print_data(p, "\"", 1); putstring(p, data, bytes); print_data(p, "\"", 1); return len; -- cgit v1.2.3