From 7d3e2bd2c4cfd1296d1d6f996d7548de26540d41 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Fri, 15 Feb 2013 16:27:18 -0800 Subject: Sync with 8 months of Google-internal development. Many things have changed and been simplified. The memory-management story for upb_def and upb_handlers is much more robust; upb_def and upb_handlers should be fairly stable interfaces now. There is still much work to do for the runtime component (upb_sink). --- upb/def.c | 991 ++++++++++++++++++++++++++------------------------------------ 1 file changed, 409 insertions(+), 582 deletions(-) (limited to 'upb/def.c') diff --git a/upb/def.c b/upb/def.c index 4bcc0c6..d858c39 100644 --- a/upb/def.c +++ b/upb/def.c @@ -5,11 +5,12 @@ * Author: Josh Haberman */ +#include "upb/def.h" + #include -#include #include #include "upb/bytestream.h" -#include "upb/def.h" +#include "upb/handlers.h" // isalpha() etc. from are locale-dependent, which we don't want. static bool upb_isbetween(char c, char low, char high) { @@ -44,204 +45,175 @@ static bool upb_isident(const char *str, size_t len, bool full) { /* upb_def ********************************************************************/ -static void upb_msgdef_free(upb_msgdef *m); -static void upb_fielddef_free(upb_fielddef *f); -static void upb_enumdef_free(upb_enumdef *e); +upb_deftype_t upb_def_type(const upb_def *d) { return d->type; } -bool upb_def_ismutable(const upb_def *def) { return !def->is_finalized; } -bool upb_def_isfinalized(const upb_def *def) { return def->is_finalized; } +const char *upb_def_fullname(const upb_def *d) { return d->fullname; } bool upb_def_setfullname(upb_def *def, const char *fullname) { - assert(upb_def_ismutable(def)); + assert(!upb_def_isfrozen(def)); if (!upb_isident(fullname, strlen(fullname), true)) return false; - free(def->fullname); - def->fullname = strdup(fullname); + free((void*)def->fullname); + def->fullname = upb_strdup(fullname); return true; } -void upb_def_ref(const upb_def *_def, const void *owner) { - upb_def *def = (upb_def*)_def; - upb_refcount_ref(&def->refcount, owner); -} - -void upb_def_unref(const upb_def *_def, const void *owner) { - upb_def *def = (upb_def*)_def; - if (!def) return; - if (!upb_refcount_unref(&def->refcount, owner)) return; - upb_def *base = def; - // Free all defs in the SCC. - do { - upb_def *next = (upb_def*)def->refcount.next; - switch (def->type) { - case UPB_DEF_MSG: upb_msgdef_free(upb_downcast_msgdef(def)); break; - case UPB_DEF_FIELD: upb_fielddef_free(upb_downcast_fielddef(def)); break; - case UPB_DEF_ENUM: upb_enumdef_free(upb_downcast_enumdef(def)); break; - default: - assert(false); - } - def = next; - } while(def != base); -} - -void upb_def_donateref(const upb_def *_def, const void *from, const void *to) { - upb_def *def = (upb_def*)_def; - upb_refcount_donateref(&def->refcount, from, to); -} - upb_def *upb_def_dup(const upb_def *def, const void *o) { switch (def->type) { case UPB_DEF_MSG: - return UPB_UPCAST(upb_msgdef_dup(upb_downcast_msgdef_const(def), o)); + return upb_upcast(upb_msgdef_dup(upb_downcast_msgdef(def), o)); case UPB_DEF_FIELD: - return UPB_UPCAST(upb_fielddef_dup(upb_downcast_fielddef_const(def), o)); + return upb_upcast(upb_fielddef_dup(upb_downcast_fielddef(def), o)); case UPB_DEF_ENUM: - return UPB_UPCAST(upb_enumdef_dup(upb_downcast_enumdef_const(def), o)); + return upb_upcast(upb_enumdef_dup(upb_downcast_enumdef(def), o)); default: assert(false); return NULL; } } -static bool upb_def_init(upb_def *def, upb_deftype_t type, const void *owner) { +bool upb_def_isfrozen(const upb_def *def) { + return upb_refcounted_isfrozen(upb_upcast(def)); +} + +void upb_def_ref(const upb_def *def, const void *owner) { + upb_refcounted_ref(upb_upcast(def), owner); +} + +void upb_def_unref(const upb_def *def, const void *owner) { + upb_refcounted_unref(upb_upcast(def), owner); +} + +void upb_def_donateref(const upb_def *def, const void *from, const void *to) { + upb_refcounted_donateref(upb_upcast(def), from, to); +} + +void upb_def_checkref(const upb_def *def, const void *owner) { + upb_refcounted_checkref(upb_upcast(def), owner); +} + +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_upcast(def), vtbl, owner)) return false; def->type = type; - def->is_finalized = false; def->fullname = NULL; - return upb_refcount_init(&def->refcount, owner); + def->came_from_user = false; + return true; } static void upb_def_uninit(upb_def *def) { - upb_refcount_uninit(&def->refcount); - free(def->fullname); + free((void*)def->fullname); } -static void upb_def_getsuccessors(upb_refcount *refcount, void *closure) { - upb_def *def = (upb_def*)refcount; - switch (def->type) { - case UPB_DEF_MSG: { - upb_msgdef *m = upb_downcast_msgdef(def); - upb_msg_iter i; - for(upb_msg_begin(&i, m); !upb_msg_done(&i); upb_msg_next(&i)) { - upb_fielddef *f = upb_msg_iter_field(&i); - upb_refcount_visit(refcount, &f->base.refcount, closure); - } - break; - } - case UPB_DEF_FIELD: { - upb_fielddef *f = upb_downcast_fielddef(def); - assert(f->msgdef); - upb_refcount_visit(refcount, &f->msgdef->base.refcount, closure); - upb_def *subdef = f->sub.def; - if (subdef) - upb_refcount_visit(refcount, &subdef->refcount, closure); - break; - } - case UPB_DEF_ENUM: - case UPB_DEF_SERVICE: - case UPB_DEF_ANY: - break; - } +static const char *msgdef_name(const upb_msgdef *m) { + const char *name = upb_def_fullname(upb_upcast(m)); + return name ? name : "(anonymous)"; } -static bool upb_validate_field(const upb_fielddef *f, upb_status *s) { - if (upb_fielddef_name(f) == NULL || upb_fielddef_number(f) == -1) { +static bool upb_validate_field(upb_fielddef *f, upb_status *s) { + if (upb_fielddef_name(f) == NULL || upb_fielddef_number(f) == 0) { upb_status_seterrliteral(s, "fielddef must have name and number set"); return false; } - if (upb_hassubdef(f)) { + if (upb_fielddef_hassubdef(f)) { if (f->subdef_is_symbolic) { upb_status_seterrf(s, - "field %s has not been resolved", upb_fielddef_name(f)); + "field '%s' has not been resolved", upb_fielddef_name(f)); return false; - } else if (upb_fielddef_subdef(f) == NULL) { + } + + const upb_def *subdef = upb_fielddef_subdef(f); + if (subdef == NULL) { + upb_status_seterrf(s, + "field %s.%s is missing required subdef", + msgdef_name(f->msgdef), upb_fielddef_name(f)); + return false; + } else if (!upb_def_isfrozen(subdef) && !subdef->came_from_user) { upb_status_seterrf(s, - "field is %s missing required subdef", upb_fielddef_name(f)); + "subdef of field %s.%s is not frozen or being frozen", + msgdef_name(f->msgdef), upb_fielddef_name(f)); return false; - } else if (!upb_def_isfinalized(upb_fielddef_subdef(f))) { + } else if (upb_fielddef_default_is_symbolic(f)) { upb_status_seterrf(s, - "field %s subtype is not being finalized", upb_fielddef_name(f)); + "enum field %s.%s has not been resolved", + msgdef_name(f->msgdef), upb_fielddef_name(f)); return false; } } return true; } -bool upb_finalize(upb_def *const*defs, int n, upb_status *s) { - if (n >= UINT16_MAX - 1) { - upb_status_seterrliteral(s, "too many defs (max is 64k at a time)"); - return false; - } - +bool upb_def_freeze(upb_def *const* defs, int n, upb_status *s) { // First perform validation, in two passes so we can check that we have a // transitive closure without needing to search. for (int i = 0; i < n; i++) { upb_def *def = defs[i]; - if (upb_def_isfinalized(def)) { + if (upb_def_isfrozen(def)) { // Could relax this requirement if it's annoying. - upb_status_seterrliteral(s, "def is already finalized"); + upb_status_seterrliteral(s, "def is already frozen"); goto err; } else if (def->type == UPB_DEF_FIELD) { - upb_status_seterrliteral(s, "standalone fielddefs can not be finalized"); + upb_status_seterrliteral(s, "standalone fielddefs can not be frozen"); goto err; } else { // Set now to detect transitive closure in the second pass. - def->is_finalized = true; + def->came_from_user = true; } } for (int i = 0; i < n; i++) { - upb_msgdef *m = upb_dyncast_msgdef(defs[i]); - if (!m) continue; - upb_inttable_compact(&m->itof); - upb_msg_iter j; - for(upb_msg_begin(&j, m); !upb_msg_done(&j); upb_msg_next(&j)) { - upb_fielddef *f = upb_msg_iter_field(&j); - assert(f->msgdef == m); - if (!upb_validate_field(f, s)) goto err; - } - } - - // Validation all passed, now find strongly-connected components so that - // our refcounting works with cycles. - upb_refcount_findscc((upb_refcount**)defs, n, &upb_def_getsuccessors); - - // Now that ref cycles have been removed it is safe to have each fielddef - // take a ref on its subdef (if any), but only if it's a member of another - // SCC. - for (int i = 0; i < n; i++) { - upb_msgdef *m = upb_dyncast_msgdef(defs[i]); - if (!m) continue; - upb_msg_iter j; - for(upb_msg_begin(&j, m); !upb_msg_done(&j); upb_msg_next(&j)) { - upb_fielddef *f = upb_msg_iter_field(&j); - f->base.is_finalized = true; - // Release the ref taken in upb_msgdef_addfields(). - upb_fielddef_unref(f, m); - if (!upb_hassubdef(f)) continue; - assert(upb_fielddef_subdef(f)); - if (!upb_refcount_merged(&f->base.refcount, &f->sub.def->refcount)) { - // Subdef is part of a different strongly-connected component. - upb_def_ref(f->sub.def, &f->sub.def); - f->subdef_is_owned = true; + upb_msgdef *m = upb_dyncast_msgdef_mutable(defs[i]); + upb_enumdef *e = upb_dyncast_enumdef_mutable(defs[i]); + if (m) { + upb_inttable_compact(&m->itof); + upb_msg_iter j; + uint32_t selector = 0; + for(upb_msg_begin(&j, m); !upb_msg_done(&j); upb_msg_next(&j)) { + upb_fielddef *f = upb_msg_iter_field(&j); + assert(f->msgdef == m); + if (!upb_validate_field(f, s)) goto err; + f->selector_base = selector + upb_handlers_selectorbaseoffset(f); + selector += upb_handlers_selectorcount(f); } + m->selector_count = selector; + } else if (e) { + upb_inttable_compact(&e->iton); } } - return true; + // Validation all passed; freeze the defs. + return upb_refcounted_freeze((upb_refcounted*const*)defs, n, s); err: for (int i = 0; i < n; i++) { - defs[i]->is_finalized = false; + defs[i]->came_from_user = false; } + assert(!upb_ok(s)); return false; } /* upb_enumdef ****************************************************************/ +static void upb_enumdef_free(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_strdup() from upb_enumdef_addval(). + free(upb_value_getcstr(upb_inttable_iter_value(&i))); + } + upb_strtable_uninit(&e->ntoi); + upb_inttable_uninit(&e->iton); + upb_def_uninit(upb_upcast(e)); + free(e); +} + upb_enumdef *upb_enumdef_new(const void *owner) { + static const struct upb_refcounted_vtbl vtbl = {NULL, &upb_enumdef_free}; upb_enumdef *e = malloc(sizeof(*e)); if (!e) return NULL; - if (!upb_def_init(&e->base, UPB_DEF_ENUM, owner)) goto err2; - if (!upb_strtable_init(&e->ntoi)) goto err2; - if (!upb_inttable_init(&e->iton)) goto err1; + if (!upb_def_init(upb_upcast(e), UPB_DEF_ENUM, &vtbl, owner)) goto err2; + 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: @@ -251,26 +223,13 @@ err2: return NULL; } -static void upb_enumdef_free(upb_enumdef *e) { - upb_inttable_iter i; - upb_inttable_begin(&i, &e->iton); - for( ; !upb_inttable_done(&i); upb_inttable_next(&i)) { - // To clean up the strdup() from upb_enumdef_addval(). - free(upb_value_getptr(upb_inttable_iter_value(&i))); - } - upb_strtable_uninit(&e->ntoi); - upb_inttable_uninit(&e->iton); - upb_def_uninit(&e->base); - free(e); -} - upb_enumdef *upb_enumdef_dup(const upb_enumdef *e, const void *owner) { upb_enumdef *new_e = upb_enumdef_new(owner); if (!new_e) return NULL; upb_enum_iter i; for(upb_enum_begin(&i, e); !upb_enum_done(&i); upb_enum_next(&i)) { bool success = upb_enumdef_addval( - new_e, upb_enum_iter_name(&i),upb_enum_iter_number(&i)); + new_e, upb_enum_iter_name(&i),upb_enum_iter_number(&i), NULL); if (!success) { upb_enumdef_unref(new_e, owner); return NULL; @@ -279,23 +238,69 @@ upb_enumdef *upb_enumdef_dup(const upb_enumdef *e, const void *owner) { return new_e; } -bool upb_enumdef_addval(upb_enumdef *e, const char *name, int32_t num) { - if (!upb_isident(name, strlen(name), false)) return false; - if (upb_enumdef_ntoi(e, name, NULL)) +bool upb_enumdef_isfrozen(const upb_enumdef *e) { + return upb_def_isfrozen(upb_upcast(e)); +} + +void upb_enumdef_ref(const upb_enumdef *e, const void *owner) { + upb_def_ref(upb_upcast(e), owner); +} + +void upb_enumdef_unref(const upb_enumdef *e, const void *owner) { + upb_def_unref(upb_upcast(e), owner); +} + +void upb_enumdef_donateref( + const upb_enumdef *e, const void *from, const void *to) { + upb_def_donateref(upb_upcast(e), from, to); +} + +void upb_enumdef_checkref(const upb_enumdef *e, const void *owner) { + upb_def_checkref(upb_upcast(e), owner); +} + +const char *upb_enumdef_fullname(const upb_enumdef *e) { + return upb_def_fullname(upb_upcast(e)); +} + +bool upb_enumdef_setfullname(upb_enumdef *e, const char *fullname) { + return upb_def_setfullname(upb_upcast(e), fullname); +} + +bool upb_enumdef_addval(upb_enumdef *e, const char *name, int32_t num, + upb_status *status) { + if (!upb_isident(name, strlen(name), false)) { + upb_status_seterrf(status, "name '%s' is not a valid identifier", name); return false; - if (!upb_strtable_insert(&e->ntoi, name, upb_value_int32(num))) + } + if (upb_enumdef_ntoi(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_seterrliteral(status, "out of memory"); + return false; + } if (!upb_inttable_lookup(&e->iton, num) && - !upb_inttable_insert(&e->iton, num, upb_value_ptr(strdup(name)))) + !upb_inttable_insert(&e->iton, num, upb_value_cstr(upb_strdup(name)))) { + upb_status_seterrliteral(status, "out of memory"); + upb_strtable_remove(&e->ntoi, name, NULL); return false; + } return true; } +int32_t upb_enumdef_default(const upb_enumdef *e) { return e->defaultval; } + void upb_enumdef_setdefault(upb_enumdef *e, int32_t val) { - assert(upb_def_ismutable(UPB_UPCAST(e))); + assert(!upb_enumdef_isfrozen(e)); e->defaultval = val; } +int upb_enumdef_numvals(const upb_enumdef *e) { + return upb_strtable_count(&e->ntoi); +} + void upb_enum_begin(upb_enum_iter *i, const upb_enumdef *e) { // We iterate over the ntoi table, to account for duplicate numbers. upb_strtable_begin(i, &e->ntoi); @@ -313,7 +318,15 @@ bool upb_enumdef_ntoi(const upb_enumdef *def, const char *name, int32_t *num) { const char *upb_enumdef_iton(const upb_enumdef *def, int32_t num) { const upb_value *v = upb_inttable_lookup32(&def->iton, num); - return v ? upb_value_getptr(*v) : NULL; + return v ? upb_value_getcstr(*v) : NULL; +} + +const char *upb_enum_iter_name(upb_enum_iter *iter) { + return upb_strtable_iter_key(iter); +} + +int32_t upb_enum_iter_number(upb_enum_iter *iter) { + return upb_value_getint32(upb_strtable_iter_value(iter)); } @@ -324,9 +337,7 @@ const char *upb_enumdef_iton(const upb_enumdef *def, int32_t num) { {alignof(ctype), sizeof(ctype), UPB_CTYPE_ ## inmemory_type} const upb_typeinfo upb_types[UPB_NUM_TYPES] = { - // END_GROUP is not real, but used to signify the pseudo-field that - // ends a group from within the group. - TYPE_INFO(void*, PTR), // ENDGROUP + TYPE_INFO(void*, PTR), // (unused) TYPE_INFO(double, DOUBLE), // DOUBLE TYPE_INFO(float, FLOAT), // FLOAT TYPE_INFO(int64_t, INT64), // INT64 @@ -340,7 +351,7 @@ const upb_typeinfo upb_types[UPB_NUM_TYPES] = { TYPE_INFO(void*, PTR), // MESSAGE TYPE_INFO(void*, BYTEREGION), // BYTES TYPE_INFO(uint32_t, UINT32), // UINT32 - TYPE_INFO(uint32_t, INT32), // ENUM + TYPE_INFO(int32_t, INT32), // ENUM TYPE_INFO(int32_t, INT32), // SFIXED32 TYPE_INFO(int64_t, INT64), // SFIXED64 TYPE_INFO(int32_t, INT32), // SINT32 @@ -349,10 +360,36 @@ const upb_typeinfo upb_types[UPB_NUM_TYPES] = { static void upb_fielddef_init_default(upb_fielddef *f); +static void upb_fielddef_uninit_default(upb_fielddef *f) { + if (f->default_is_string) + upb_byteregion_free(upb_value_getbyteregion(f->defaultval)); +} + +static void visitfield(const upb_refcounted *r, upb_refcounted_visit *visit, + void *closure) { + const upb_fielddef *f = (const upb_fielddef*)r; + if (f->msgdef) { + visit(r, upb_upcast2(f->msgdef), closure); + } + if (!f->subdef_is_symbolic && f->sub.def) { + visit(r, upb_upcast(f->sub.def), closure); + } +} + +static void freefield(upb_refcounted *r) { + upb_fielddef *f = (upb_fielddef*)r; + upb_fielddef_uninit_default(f); + if (f->subdef_is_symbolic) + free(f->sub.name); + upb_def_uninit(upb_upcast(f)); + free(f); +} + upb_fielddef *upb_fielddef_new(const void *owner) { + static const struct upb_refcounted_vtbl vtbl = {visitfield, freefield}; upb_fielddef *f = malloc(sizeof(*f)); if (!f) return NULL; - if (!upb_def_init(UPB_UPCAST(f), UPB_DEF_FIELD, owner)) { + if (!upb_def_init(upb_upcast(f), UPB_DEF_FIELD, &vtbl, owner)) { free(f); return NULL; } @@ -360,35 +397,18 @@ upb_fielddef *upb_fielddef_new(const void *owner) { f->sub.def = NULL; f->subdef_is_symbolic = false; f->subdef_is_owned = false; - f->label = UPB_LABEL(OPTIONAL); - f->hasbit = -1; - f->offset = 0; - f->accessor = NULL; - upb_value_setfielddef(&f->fval, f); + f->label_ = UPB_LABEL(OPTIONAL); // These are initialized to be invalid; the user must set them explicitly. // Could relax this later if it's convenient and non-confusing to have a // defaults for them. - f->type = UPB_TYPE_NONE; - f->number = 0; + f->type_ = UPB_TYPE_NONE; + f->number_ = 0; upb_fielddef_init_default(f); return f; } -static void upb_fielddef_uninit_default(upb_fielddef *f) { - if (f->default_is_string) - upb_byteregion_free(upb_value_getbyteregion(f->defaultval)); -} - -static void upb_fielddef_free(upb_fielddef *f) { - if (f->subdef_is_owned) - upb_def_unref(f->sub.def, &f->sub.def); - upb_fielddef_uninit_default(f); - upb_def_uninit(UPB_UPCAST(f)); - free(f); -} - upb_fielddef *upb_fielddef_dup(const upb_fielddef *f, const void *owner) { upb_fielddef *newf = upb_fielddef_new(owner); if (!newf) return NULL; @@ -396,10 +416,6 @@ upb_fielddef *upb_fielddef_dup(const upb_fielddef *f, const void *owner) { upb_fielddef_setlabel(newf, upb_fielddef_label(f)); upb_fielddef_setnumber(newf, upb_fielddef_number(f)); upb_fielddef_setname(newf, upb_fielddef_name(f)); - upb_fielddef_sethasbit(newf, upb_fielddef_hasbit(f)); - upb_fielddef_setoffset(newf, upb_fielddef_offset(f)); - upb_fielddef_setaccessor(newf, upb_fielddef_accessor(f)); - upb_fielddef_setfval(newf, upb_fielddef_fval(f)); if (f->default_is_string) { upb_byteregion *r = upb_value_getbyteregion(upb_fielddef_default(f)); size_t len; @@ -424,13 +440,64 @@ upb_fielddef *upb_fielddef_dup(const upb_fielddef *f, const void *owner) { } strcpy(newname, "."); strcat(newname, f->sub.def->fullname); - upb_fielddef_setsubtypename(newf, newname); + upb_fielddef_setsubdefname(newf, newname); free(newname); } return newf; } +bool upb_fielddef_isfrozen(const upb_fielddef *f) { + return upb_def_isfrozen(upb_upcast(f)); +} + +void upb_fielddef_ref(const upb_fielddef *f, const void *owner) { + upb_def_ref(upb_upcast(f), owner); +} + +void upb_fielddef_unref(const upb_fielddef *f, const void *owner) { + upb_def_unref(upb_upcast(f), owner); +} + +void upb_fielddef_donateref( + const upb_fielddef *f, const void *from, const void *to) { + upb_def_donateref(upb_upcast(f), from, to); +} + +void upb_fielddef_checkref(const upb_fielddef *f, const void *owner) { + upb_def_checkref(upb_upcast(f), owner); +} + +upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f) { + return f->type_; +} + +upb_label_t upb_fielddef_label(const upb_fielddef *f) { + return f->label_; +} + +uint32_t upb_fielddef_number(const upb_fielddef *f) { return f->number_; } + +const char *upb_fielddef_name(const upb_fielddef *f) { + return upb_def_fullname(upb_upcast(f)); +} + +const upb_msgdef *upb_fielddef_msgdef(const upb_fielddef *f) { + return f->msgdef; +} + +upb_msgdef *upb_fielddef_msgdef_mutable(upb_fielddef *f) { + return (upb_msgdef*)f->msgdef; +} + +bool upb_fielddef_setname(upb_fielddef *f, const char *name) { + return upb_def_setfullname(upb_upcast(f), name); +} + +upb_value upb_fielddef_default(const upb_fielddef *f) { + return f->defaultval; +} + static void upb_fielddef_init_default(upb_fielddef *f) { f->default_is_string = false; switch (upb_fielddef_type(f)) { @@ -455,13 +522,12 @@ static void upb_fielddef_init_default(upb_fielddef *f) { break; case UPB_TYPE(GROUP): case UPB_TYPE(MESSAGE): upb_value_setptr(&f->defaultval, NULL); break; - case UPB_TYPE_ENDGROUP: assert(false); case UPB_TYPE_NONE: break; } } const upb_def *upb_fielddef_subdef(const upb_fielddef *f) { - if (upb_hassubdef(f) && upb_fielddef_isfinalized(f)) { + if (upb_fielddef_hassubdef(f) && upb_fielddef_isfrozen(f)) { assert(f->sub.def); return f->sub.def; } else { @@ -473,65 +539,34 @@ upb_def *upb_fielddef_subdef_mutable(upb_fielddef *f) { return (upb_def*)upb_fielddef_subdef(f); } -const char *upb_fielddef_subtypename(upb_fielddef *f) { - assert(upb_fielddef_ismutable(f)); +const char *upb_fielddef_subdefname(const upb_fielddef *f) { + assert(!upb_fielddef_isfrozen(f)); return f->subdef_is_symbolic ? f->sub.name : NULL; } -// Could expose this to clients if a client wants to call it independently -// of upb_resolve() for whatever reason. -static bool upb_fielddef_resolvedefault(upb_fielddef *f, upb_status *s) { - if (!f->default_is_string) return true; - // Resolve the enum's default from a string to an integer. - upb_byteregion *bytes = upb_value_getbyteregion(f->defaultval); - assert(bytes); // Points to either a real default or the empty string. - upb_enumdef *e = upb_downcast_enumdef(upb_fielddef_subdef_mutable(f)); - int32_t val = 0; - if (upb_byteregion_len(bytes) == 0) { - upb_value_setint32(&f->defaultval, e->defaultval); - } else { - size_t len; - // ptr is guaranteed to be NULL-terminated because the byteregion was - // created with upb_byteregion_newl(). - const char *ptr = upb_byteregion_getptr( - bytes, upb_byteregion_startofs(bytes), &len); - assert(len == upb_byteregion_len(bytes)); // Should all be in one chunk. - bool success = upb_enumdef_ntoi(e, ptr, &val); - if (!success) { - upb_status_seterrf( - s, "Default enum value (%s) is not a member of the enum", ptr); - return false; - } - upb_value_setint32(&f->defaultval, val); - } - f->default_is_string = false; - upb_byteregion_free(bytes); - return true; -} - -bool upb_fielddef_setnumber(upb_fielddef *f, int32_t number) { +bool upb_fielddef_setnumber(upb_fielddef *f, uint32_t number) { assert(f->msgdef == NULL); - f->number = number; + f->number_ = number; return true; } bool upb_fielddef_settype(upb_fielddef *f, upb_fieldtype_t type) { - assert(upb_fielddef_ismutable(f)); + assert(!upb_fielddef_isfrozen(f)); upb_fielddef_uninit_default(f); - f->type = type; + f->type_ = type; upb_fielddef_init_default(f); return true; } bool upb_fielddef_setlabel(upb_fielddef *f, upb_label_t label) { - assert(upb_fielddef_ismutable(f)); - f->label = label; + assert(!upb_fielddef_isfrozen(f)); + f->label_ = label; return true; } void upb_fielddef_setdefault(upb_fielddef *f, upb_value value) { - assert(upb_fielddef_ismutable(f)); - assert(!upb_isstring(f) && !upb_issubmsg(f)); + assert(!upb_fielddef_isfrozen(f)); + assert(!upb_fielddef_isstring(f) && !upb_fielddef_issubmsg(f)); if (f->default_is_string) { upb_byteregion *bytes = upb_value_getbyteregion(f->defaultval); assert(bytes); @@ -542,20 +577,21 @@ void upb_fielddef_setdefault(upb_fielddef *f, upb_value value) { } bool upb_fielddef_setdefaultstr(upb_fielddef *f, const void *str, size_t len) { - assert(upb_isstring(f) || f->type == UPB_TYPE(ENUM)); + assert(upb_fielddef_isstring(f) || f->type_ == UPB_TYPE(ENUM)); + if (f->type_ == UPB_TYPE(ENUM) && !upb_isident(str, len, false)) return false; + if (f->default_is_string) { upb_byteregion *bytes = upb_value_getbyteregion(f->defaultval); assert(bytes); upb_byteregion_free(bytes); } else { - assert(f->type == UPB_TYPE(ENUM)); + assert(f->type_ == UPB_TYPE(ENUM)); } - if (f->type == UPB_TYPE(ENUM) && !upb_isident(str, len, false)) return false; + upb_byteregion *r = upb_byteregion_newl(str, len); upb_value_setbyteregion(&f->defaultval, r); upb_bytesuccess_t ret = upb_byteregion_fetch(r); - (void)ret; - assert(ret == (len == 0 ? UPB_BYTE_EOF : UPB_BYTE_OK)); + UPB_ASSERT_VAR(ret, ret == (len == 0 ? UPB_BYTE_EOF : UPB_BYTE_OK)); assert(upb_byteregion_available(r, 0) == upb_byteregion_len(r)); f->default_is_string = true; return true; @@ -565,77 +601,127 @@ void upb_fielddef_setdefaultcstr(upb_fielddef *f, const char *str) { upb_fielddef_setdefaultstr(f, str, str ? strlen(str) : 0); } -void upb_fielddef_setfval(upb_fielddef *f, upb_value fval) { - assert(upb_fielddef_ismutable(f)); - // TODO: we need an ownership/freeing mechanism for dynamically-allocated - // fvals. One possibility is to let the user supply a free() function - // and call it when the fval is no longer referenced. Would have to - // ensure that no common use cases need cycles. - // - // For now the fval has no ownership; the caller must simply guarantee - // somehow that it outlives any handlers/plan. - f->fval = fval; +bool upb_fielddef_default_is_symbolic(const upb_fielddef *f) { + return f->default_is_string && f->type_ == UPB_TYPE_ENUM; } -void upb_fielddef_sethasbit(upb_fielddef *f, int16_t hasbit) { - assert(upb_fielddef_ismutable(f)); - f->hasbit = hasbit; -} +bool upb_fielddef_resolvedefault(upb_fielddef *f) { + if (!upb_fielddef_default_is_symbolic(f)) return true; -void upb_fielddef_setoffset(upb_fielddef *f, uint16_t offset) { - assert(upb_fielddef_ismutable(f)); - f->offset = offset; -} - -void upb_fielddef_setaccessor(upb_fielddef *f, struct _upb_accessor_vtbl *tbl) { - assert(upb_fielddef_ismutable(f)); - f->accessor = tbl; + upb_byteregion *bytes = upb_value_getbyteregion(f->defaultval); + const upb_enumdef *e = upb_downcast_enumdef(upb_fielddef_subdef(f)); + assert(bytes); // Points to either a real default or the empty string. + assert(e); + if (upb_byteregion_len(bytes) == 0) { + // The "default default" for an enum is the first defined value. + upb_value_setint32(&f->defaultval, e->defaultval); + } else { + size_t len; + int32_t val = 0; + // ptr is guaranteed to be NULL-terminated because the byteregion was + // created with upb_byteregion_newl(). + const char *ptr = upb_byteregion_getptr( + bytes, upb_byteregion_startofs(bytes), &len); + assert(len == upb_byteregion_len(bytes)); // Should all be in one chunk + if (!upb_enumdef_ntoi(e, ptr, &val)) { + return false; + } + upb_value_setint32(&f->defaultval, val); + } + f->default_is_string = false; + upb_byteregion_free(bytes); + return true; } -static bool upb_subtype_typecheck(upb_fielddef *f, const upb_def *subdef) { - if (f->type == UPB_TYPE(MESSAGE) || f->type == UPB_TYPE(GROUP)) - return upb_dyncast_msgdef_const(subdef) != NULL; - else if (f->type == UPB_TYPE(ENUM)) - return upb_dyncast_enumdef_const(subdef) != NULL; +static bool upb_subdef_typecheck(upb_fielddef *f, const upb_def *subdef) { + if (f->type_ == UPB_TYPE(MESSAGE) || f->type_ == UPB_TYPE(GROUP)) + return upb_dyncast_msgdef(subdef) != NULL; + else if (f->type_ == UPB_TYPE(ENUM)) + return upb_dyncast_enumdef(subdef) != NULL; else { assert(false); return false; } } -bool upb_fielddef_setsubdef(upb_fielddef *f, upb_def *subdef) { - assert(upb_fielddef_ismutable(f)); - assert(upb_hassubdef(f)); - assert(subdef); - if (!upb_subtype_typecheck(f, subdef)) return false; - if (f->subdef_is_symbolic) free(f->sub.name); +static void release_subdef(upb_fielddef *f) { + if (f->subdef_is_symbolic) { + free(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) { + assert(!upb_fielddef_isfrozen(f)); + assert(upb_fielddef_hassubdef(f)); + if (subdef && !upb_subdef_typecheck(f, subdef)) 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_setsubtypename(upb_fielddef *f, const char *name) { - assert(upb_fielddef_ismutable(f)); - assert(upb_hassubdef(f)); - if (f->subdef_is_symbolic) free(f->sub.name); - f->sub.name = strdup(name); +bool upb_fielddef_setsubdefname(upb_fielddef *f, const char *name) { + assert(!upb_fielddef_isfrozen(f)); + assert(upb_fielddef_hassubdef(f)); + release_subdef(f); + f->sub.name = upb_strdup(name); f->subdef_is_symbolic = true; return true; } +bool upb_fielddef_issubmsg(const upb_fielddef *f) { + return upb_fielddef_type(f) == UPB_TYPE_GROUP || + upb_fielddef_type(f) == UPB_TYPE_MESSAGE; +} + +bool upb_fielddef_isstring(const upb_fielddef *f) { + return upb_fielddef_type(f) == UPB_TYPE_STRING || + upb_fielddef_type(f) == UPB_TYPE_BYTES; +} + +bool upb_fielddef_isseq(const upb_fielddef *f) { + return upb_fielddef_label(f) == UPB_LABEL_REPEATED; +} + +bool upb_fielddef_isprimitive(const upb_fielddef *f) { + return !upb_fielddef_isstring(f) && !upb_fielddef_issubmsg(f); +} + +bool upb_fielddef_hassubdef(const upb_fielddef *f) { + return upb_fielddef_issubmsg(f) || upb_fielddef_type(f) == UPB_TYPE(ENUM); +} + /* upb_msgdef *****************************************************************/ +static void visitmsg(const upb_refcounted *r, upb_refcounted_visit *visit, + void *closure) { + const upb_msgdef *m = (const upb_msgdef*)r; + upb_msg_iter i; + for(upb_msg_begin(&i, m); !upb_msg_done(&i); upb_msg_next(&i)) { + upb_fielddef *f = upb_msg_iter_field(&i); + visit(r, upb_upcast2(f), 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_upcast(m)); + free(m); +} + upb_msgdef *upb_msgdef_new(const void *owner) { + static const struct upb_refcounted_vtbl vtbl = {visitmsg, freemsg}; upb_msgdef *m = malloc(sizeof(*m)); if (!m) return NULL; - if (!upb_def_init(&m->base, UPB_DEF_MSG, owner)) goto err2; - if (!upb_inttable_init(&m->itof)) goto err2; - if (!upb_strtable_init(&m->ntof)) goto err1; - m->size = 0; - m->hasbit_bytes = 0; - m->extstart = 0; - m->extend = 0; + if (!upb_def_init(upb_upcast(m), UPB_DEF_MSG, &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; return m; err1: @@ -645,20 +731,10 @@ err2: return NULL; } -static void upb_msgdef_free(upb_msgdef *m) { - upb_strtable_uninit(&m->ntof); - upb_inttable_uninit(&m->itof); - upb_def_uninit(&m->base); - free(m); -} - upb_msgdef *upb_msgdef_dup(const upb_msgdef *m, const void *owner) { upb_msgdef *newm = upb_msgdef_new(owner); if (!newm) return NULL; - upb_msgdef_setsize(newm, upb_msgdef_size(m)); - upb_msgdef_sethasbit_bytes(newm, upb_msgdef_hasbit_bytes(m)); - upb_msgdef_setextrange(newm, upb_msgdef_extstart(m), upb_msgdef_extend(m)); - upb_def_setfullname(UPB_UPCAST(newm), upb_def_fullname(UPB_UPCAST(m))); + upb_def_setfullname(upb_upcast(newm), upb_def_fullname(upb_upcast(m))); upb_msg_iter i; for(upb_msg_begin(&i, m); !upb_msg_done(&i); upb_msg_next(&i)) { upb_fielddef *f = upb_fielddef_dup(upb_msg_iter_field(&i), &f); @@ -670,26 +746,33 @@ upb_msgdef *upb_msgdef_dup(const upb_msgdef *m, const void *owner) { return newm; } -void upb_msgdef_setsize(upb_msgdef *m, uint16_t size) { - assert(upb_def_ismutable(UPB_UPCAST(m))); - m->size = size; +bool upb_msgdef_isfrozen(const upb_msgdef *m) { + return upb_def_isfrozen(upb_upcast(m)); } -void upb_msgdef_sethasbit_bytes(upb_msgdef *m, uint16_t bytes) { - assert(upb_def_ismutable(UPB_UPCAST(m))); - m->hasbit_bytes = bytes; +void upb_msgdef_ref(const upb_msgdef *m, const void *owner) { + upb_def_ref(upb_upcast(m), owner); } -bool upb_msgdef_setextrange(upb_msgdef *m, uint32_t start, uint32_t end) { - assert(upb_def_ismutable(UPB_UPCAST(m))); - if (start == 0 && end == 0) { - // Clearing the extension range -- ok to fall through. - } else if (start >= end || start < 1 || end > UPB_MAX_FIELDNUMBER) { - return false; - } - m->extstart = start; - m->extend = start; - return true; +void upb_msgdef_unref(const upb_msgdef *m, const void *owner) { + upb_def_unref(upb_upcast(m), owner); +} + +void upb_msgdef_donateref( + const upb_msgdef *m, const void *from, const void *to) { + upb_def_donateref(upb_upcast(m), from, to); +} + +void upb_msgdef_checkref(const upb_msgdef *m, const void *owner) { + upb_def_checkref(upb_upcast(m), owner); +} + +const char *upb_msgdef_fullname(const upb_msgdef *m) { + return upb_def_fullname(upb_upcast(m)); +} + +bool upb_msgdef_setfullname(upb_msgdef *m, const char *fullname) { + return upb_def_setfullname(upb_upcast(m), fullname); } bool upb_msgdef_addfields(upb_msgdef *m, upb_fielddef *const *fields, int n, @@ -697,6 +780,8 @@ bool upb_msgdef_addfields(upb_msgdef *m, upb_fielddef *const *fields, int n, // Check constraints for all fields before performing any action. for (int i = 0; i < n; i++) { upb_fielddef *f = fields[i]; + // TODO(haberman): handle the case where two fields of the input duplicate + // name or number. if (f->msgdef != NULL || upb_fielddef_name(f) == NULL || upb_fielddef_number(f) == 0 || upb_msgdef_itof(m, upb_fielddef_number(f)) || @@ -710,306 +795,48 @@ bool upb_msgdef_addfields(upb_msgdef *m, upb_fielddef *const *fields, int n, f->msgdef = m; 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_fielddef_ref(f, m); + upb_ref2(f, m); + upb_ref2(m, f); if (ref_donor) upb_fielddef_unref(f, ref_donor); } return true; } -void upb_msg_begin(upb_msg_iter *iter, const upb_msgdef *m) { - upb_inttable_begin(iter, &m->itof); +bool upb_msgdef_addfield(upb_msgdef *m, upb_fielddef *f, + const void *ref_donor) { + return upb_msgdef_addfields(m, &f, 1, ref_donor); } -void upb_msg_next(upb_msg_iter *iter) { upb_inttable_next(iter); } - - -/* upb_symtab *****************************************************************/ - -upb_symtab *upb_symtab_new(const void *owner) { - upb_symtab *s = malloc(sizeof(*s)); - upb_refcount_init(&s->refcount, owner); - upb_strtable_init(&s->symtab); - return s; +const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i) { + const upb_value *val = upb_inttable_lookup32(&m->itof, i); + return val ? (const upb_fielddef*)upb_value_getptr(*val) : NULL; } -void upb_symtab_ref(const upb_symtab *s, const void *owner) { - upb_refcount_ref(&s->refcount, owner); +const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name) { + const upb_value *val = upb_strtable_lookup(&m->ntof, name); + return val ? (upb_fielddef*)upb_value_getptr(*val) : NULL; } -void upb_symtab_unref(const upb_symtab *s, const void *owner) { - if(s && upb_refcount_unref(&s->refcount, owner)) { - upb_symtab *destroying = (upb_symtab*)s; - upb_strtable_iter i; - upb_strtable_begin(&i, &destroying->symtab); - for (; !upb_strtable_done(&i); upb_strtable_next(&i)) - upb_def_unref(upb_value_getptr(upb_strtable_iter_value(&i)), s); - upb_strtable_uninit(&destroying->symtab); - upb_refcount_uninit(&destroying->refcount); - free(destroying); - } +upb_fielddef *upb_msgdef_itof_mutable(upb_msgdef *m, uint32_t i) { + return (upb_fielddef*)upb_msgdef_itof(m, i); } -void upb_symtab_donateref( - const upb_symtab *s, const void *from, const void *to) { - upb_refcount_donateref(&s->refcount, from, to); -} - -const upb_def **upb_symtab_getdefs(const upb_symtab *s, int *count, - upb_deftype_t type, const void *owner) { - int total = upb_strtable_count(&s->symtab); - // We may only use part of this, depending on how many symbols are of the - // correct type. - const upb_def **defs = malloc(sizeof(*defs) * total); - upb_strtable_iter iter; - upb_strtable_begin(&iter, &s->symtab); - int i = 0; - for(; !upb_strtable_done(&iter); upb_strtable_next(&iter)) { - upb_def *def = upb_value_getptr(upb_strtable_iter_value(&iter)); - assert(def); - if(type == UPB_DEF_ANY || def->type == type) - defs[i++] = def; - } - *count = i; - if (owner) - for(i = 0; i < *count; i++) upb_def_ref(defs[i], owner); - return defs; -} - -const upb_def *upb_symtab_lookup(const upb_symtab *s, const char *sym, - const void *owner) { - const upb_value *v = upb_strtable_lookup(&s->symtab, sym); - upb_def *ret = v ? upb_value_getptr(*v) : NULL; - if (ret) upb_def_ref(ret, owner); - return ret; -} - -const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym, - const void *owner) { - const upb_value *v = upb_strtable_lookup(&s->symtab, sym); - upb_def *def = v ? upb_value_getptr(*v) : NULL; - upb_msgdef *ret = NULL; - if(def && def->type == UPB_DEF_MSG) { - ret = upb_downcast_msgdef(def); - upb_def_ref(def, owner); - } - return ret; -} - -// 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] == UPB_SYMBOL_SEPARATOR) { - // Symbols starting with '.' are absolute, so we do a single lookup. - // Slice to omit the leading '.' - const upb_value *v = upb_strtable_lookup(t, sym + 1); - return 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; - assert(false); - return NULL; - } +upb_fielddef *upb_msgdef_ntof_mutable(upb_msgdef *m, const char *name) { + return (upb_fielddef*)upb_msgdef_ntof(m, name); } -const upb_def *upb_symtab_resolve(const upb_symtab *s, const char *base, - const char *sym, const void *owner) { - upb_def *ret = upb_resolvename(&s->symtab, base, sym); - if (ret) upb_def_ref(ret, owner); - return ret; -} - -// Adds dups of any existing def that can reach a def with the same name as one -// of "defs." This is to provide a consistent output graph as documented in -// the header file. We use a modified depth-first traversal that traverses -// each SCC (which we already computed) as if it were a single node. This -// allows us to traverse the possibly-cyclic graph as if it were a DAG and to -// easily dup the correct set of nodes with O(n) time. -// -// Returns true if defs that can reach "def" need to be duplicated into deftab. -static bool upb_resolve_dfs(const upb_def *def, upb_strtable *deftab, - const void *new_owner, upb_inttable *seen, - upb_status *s) { - // Memoize results of this function for efficiency (since we're traversing a - // DAG this is not needed to limit the depth of the search). - upb_value *v = upb_inttable_lookup(seen, (uintptr_t)def); - if (v) return upb_value_getbool(*v); - - // Visit submessages for all messages in the SCC. - bool need_dup = false; - const upb_def *base = def; - do { - assert(upb_def_isfinalized(def)); - if (def->type == UPB_DEF_FIELD) continue; - upb_value *v = upb_strtable_lookup(deftab, upb_def_fullname(def)); - if (v) { - upb_def *add_def = upb_value_getptr(*v); - if (add_def->refcount.next && add_def->refcount.next != &def->refcount) { - upb_status_seterrf(s, "conflicting existing defs for name: '%s'", - upb_def_fullname(def)); - return false; - } - need_dup = true; - } - const upb_msgdef *m = upb_dyncast_msgdef_const(def); - if (m) { - upb_msg_iter i; - for(upb_msg_begin(&i, m); !upb_msg_done(&i); upb_msg_next(&i)) { - upb_fielddef *f = upb_msg_iter_field(&i); - if (!upb_hassubdef(f)) continue; - // |= to avoid short-circuit; we need its side-effects. - need_dup |= upb_resolve_dfs( - upb_fielddef_subdef_mutable(f), deftab, new_owner, seen, s); - if (!upb_ok(s)) return false; - } - } - } while ((def = (upb_def*)def->refcount.next) != base); - - if (need_dup) { - // Dup any defs that don't already have entries in deftab. - def = base; - do { - if (def->type == UPB_DEF_FIELD) continue; - const char *name = upb_def_fullname(def); - if (upb_strtable_lookup(deftab, name) == NULL) { - upb_def *newdef = upb_def_dup(def, new_owner); - if (!newdef) goto oom; - // We temporarily use this field to track who we were dup'd from. - newdef->refcount.next = (upb_refcount*)def; - if (!upb_strtable_insert(deftab, name, upb_value_ptr(newdef))) - goto oom; - } - } while ((def = (upb_def*)def->refcount.next) != base); - } - - upb_inttable_insert(seen, (uintptr_t)def, upb_value_bool(need_dup)); - return need_dup; - -oom: - upb_status_seterrliteral(s, "out of memory"); - return false; +int upb_msgdef_numfields(const upb_msgdef *m) { + return upb_strtable_count(&m->ntof); } -bool upb_symtab_add(upb_symtab *s, upb_def *const*defs, int n, void *ref_donor, - upb_status *status) { - upb_def **add_defs = NULL; - upb_strtable addtab; - if (!upb_strtable_init(&addtab)) { - upb_status_seterrliteral(status, "out of memory"); - return false; - } - - // Add new defs to table. - for (int i = 0; i < n; i++) { - upb_def *def = defs[i]; - assert(upb_def_ismutable(def)); - const char *fullname = upb_def_fullname(def); - if (!fullname) { - upb_status_seterrliteral( - status, "Anonymous defs cannot be added to a symtab"); - goto err; - } - if (upb_strtable_lookup(&addtab, fullname) != NULL) { - upb_status_seterrf(status, "Conflicting defs named '%s'", fullname); - goto err; - } - if (!upb_strtable_insert(&addtab, fullname, upb_value_ptr(def))) - goto oom_err; - // We temporarily use this field to indicate that we came from the user's - // list rather than being dup'd. - def->refcount.next = NULL; - } - - // Add dups of any existing def that can reach a def with the same name as - // one of "defs." - upb_inttable seen; - if (!upb_inttable_init(&seen)) goto oom_err; - upb_strtable_iter i; - upb_strtable_begin(&i, &s->symtab); - for (; !upb_strtable_done(&i); upb_strtable_next(&i)) { - upb_def *def = upb_value_getptr(upb_strtable_iter_value(&i)); - upb_resolve_dfs(def, &addtab, ref_donor, &seen, status); - if (!upb_ok(status)) goto err; - } - upb_inttable_uninit(&seen); - - // Now using the table, resolve symbolic references. - upb_strtable_begin(&i, &addtab); - for (; !upb_strtable_done(&i); upb_strtable_next(&i)) { - upb_def *def = upb_value_getptr(upb_strtable_iter_value(&i)); - upb_msgdef *m = upb_dyncast_msgdef(def); - if (!m) continue; - // Type names are resolved relative to the message in which they appear. - const char *base = upb_def_fullname(UPB_UPCAST(m)); - - upb_msg_iter j; - for(upb_msg_begin(&j, m); !upb_msg_done(&j); upb_msg_next(&j)) { - upb_fielddef *f = upb_msg_iter_field(&j); - const char *name = upb_fielddef_subtypename(f); - if (name) { - upb_def *subdef = upb_resolvename(&addtab, 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)) { - upb_status_seterrf( - status, "def '%s' had the wrong type for field '%s'", - upb_def_fullname(subdef), upb_fielddef_name(f)); - goto err; - } - } - - if (upb_fielddef_type(f) == UPB_TYPE(ENUM) && upb_fielddef_subdef(f) && - !upb_fielddef_resolvedefault(f, status)) - goto err; - } - } - - // We need an array of the defs in addtab, for passing to upb_finalize. - add_defs = malloc(sizeof(void*) * upb_strtable_count(&addtab)); - if (add_defs == NULL) goto oom_err; - upb_strtable_begin(&i, &addtab); - for (n = 0; !upb_strtable_done(&i); upb_strtable_next(&i)) - add_defs[n++] = upb_value_getptr(upb_strtable_iter_value(&i)); - - // Restore the next pointer that we stole. - for (int i = 0; i < n; i++) - add_defs[i]->refcount.next = &add_defs[i]->refcount; +void upb_msg_begin(upb_msg_iter *iter, const upb_msgdef *m) { + upb_inttable_begin(iter, &m->itof); +} - if (!upb_finalize(add_defs, n, status)) goto err; - upb_strtable_uninit(&addtab); +void upb_msg_next(upb_msg_iter *iter) { upb_inttable_next(iter); } - for (int i = 0; i < n; i++) { - upb_def *def = add_defs[i]; - const char *name = upb_def_fullname(def); - upb_def_donateref(def, ref_donor, s); - upb_value *v = upb_strtable_lookup(&s->symtab, name); - if(v) { - upb_def_unref(upb_value_getptr(*v), s); - upb_value_setptr(v, def); - } else { - upb_strtable_insert(&s->symtab, name, upb_value_ptr(def)); - } - } - free(add_defs); - return true; +bool upb_msg_done(upb_msg_iter *iter) { return upb_inttable_done(iter); } -oom_err: - upb_status_seterrliteral(status, "out of memory"); -err: { - // Need to unref any defs we dup'd (we can distinguish them from defs that - // the user passed in by their def->refcount.next pointers). - upb_strtable_iter i; - upb_strtable_begin(&i, &addtab); - for (; !upb_strtable_done(&i); upb_strtable_next(&i)) { - upb_def *def = upb_value_getptr(upb_strtable_iter_value(&i)); - if (def->refcount.next) upb_def_unref(def, s); - } - } - upb_strtable_uninit(&addtab); - free(add_defs); - return false; +upb_fielddef *upb_msg_iter_field(upb_msg_iter *iter) { + return (upb_fielddef*)upb_value_getptr(upb_inttable_iter_value(iter)); } -- cgit v1.2.3