add some more accessor calls to def.c

PiperOrigin-RevId: 470404251
pull/13171/head
Eric Salo 2 years ago committed by Copybara-Service
parent 52c9b98692
commit 11aa037bfd
  1. 38
      upb/def.c

@ -678,7 +678,8 @@ const upb_EnumDef* upb_FieldDef_EnumSubDef(const upb_FieldDef* f) {
const upb_MiniTable_Field* upb_FieldDef_MiniTable(const upb_FieldDef* f) { const upb_MiniTable_Field* upb_FieldDef_MiniTable(const upb_FieldDef* f) {
UPB_ASSERT(!upb_FieldDef_IsExtension(f)); UPB_ASSERT(!upb_FieldDef_IsExtension(f));
return &f->msgdef->layout->fields[f->layout_index]; const upb_MiniTable* layout = upb_MessageDef_MiniTable(f->msgdef);
return &layout->fields[f->layout_index];
} }
const upb_MiniTable_Extension* _upb_FieldDef_ExtensionMiniTable( const upb_MiniTable_Extension* _upb_FieldDef_ExtensionMiniTable(
@ -1149,7 +1150,7 @@ int upb_ServiceDef_MethodCount(const upb_ServiceDef* s) {
} }
const upb_MethodDef* upb_ServiceDef_Method(const upb_ServiceDef* s, int i) { const upb_MethodDef* upb_ServiceDef_Method(const upb_ServiceDef* s, int i) {
return i < 0 || i >= s->method_count ? NULL : &s->methods[i]; return (i < 0 || i >= s->method_count) ? NULL : &s->methods[i];
} }
const upb_MethodDef* upb_ServiceDef_FindMethodByName(const upb_ServiceDef* s, const upb_MethodDef* upb_ServiceDef_FindMethodByName(const upb_ServiceDef* s,
@ -1176,22 +1177,18 @@ void upb_DefPool_Free(upb_DefPool* s) {
upb_DefPool* upb_DefPool_New(void) { upb_DefPool* upb_DefPool_New(void) {
upb_DefPool* s = upb_gmalloc(sizeof(*s)); upb_DefPool* s = upb_gmalloc(sizeof(*s));
if (!s) return NULL;
if (!s) {
return NULL;
}
s->arena = upb_Arena_New(); s->arena = upb_Arena_New();
s->bytes_loaded = 0; s->bytes_loaded = 0;
if (!upb_strtable_init(&s->syms, 32, s->arena) || if (!upb_strtable_init(&s->syms, 32, s->arena)) goto err;
!upb_strtable_init(&s->files, 4, s->arena) || if (!upb_strtable_init(&s->files, 4, s->arena)) goto err;
!upb_inttable_init(&s->exts, s->arena)) { if (!upb_inttable_init(&s->exts, s->arena)) goto err;
goto err;
}
s->extreg = upb_ExtensionRegistry_New(s->arena); s->extreg = upb_ExtensionRegistry_New(s->arena);
if (!s->extreg) goto err; if (!s->extreg) goto err;
return s; return s;
err: err:
@ -1537,7 +1534,7 @@ static void fill_fieldlayout(upb_MiniTable_Field* field,
/* This function is the dynamic equivalent of message_layout.{cc,h} in upbc. /* This function is the dynamic equivalent of message_layout.{cc,h} in upbc.
* It computes a dynamic layout for all of the fields in |m|. */ * It computes a dynamic layout for all of the fields in |m|. */
static void make_layout(symtab_addctx* ctx, const upb_MessageDef* m) { static void make_layout(symtab_addctx* ctx, const upb_MessageDef* m) {
upb_MiniTable* l = (upb_MiniTable*)m->layout; upb_MiniTable* l = (upb_MiniTable*)upb_MessageDef_MiniTable(m);
size_t field_count = upb_MessageDef_numfields(m); size_t field_count = upb_MessageDef_numfields(m);
size_t sublayout_count = 0; size_t sublayout_count = 0;
upb_MiniTable_Sub* subs; upb_MiniTable_Sub* subs;
@ -2261,7 +2258,7 @@ static void create_fielddef(
const char* shortname; const char* shortname;
int32_t field_number; int32_t field_number;
f->file = ctx->file; /* Must happen prior to symtab_add(). */ f->file = ctx->file; // Must happen prior to symtab_add()
if (!google_protobuf_FieldDescriptorProto_has_name(field_proto)) { if (!google_protobuf_FieldDescriptorProto_has_name(field_proto)) {
symtab_errf(ctx, "field has no name"); symtab_errf(ctx, "field has no name");
@ -2359,8 +2356,9 @@ static void create_fielddef(
CHK_OOM(upb_inttable_insert(&m->itof, field_number, v, ctx->arena)); CHK_OOM(upb_inttable_insert(&m->itof, field_number, v, ctx->arena));
if (ctx->layout) { if (ctx->layout) {
const upb_MiniTable_Field* fields = m->layout->fields; const upb_MiniTable* mt = upb_MessageDef_MiniTable(m);
int count = m->layout->field_count; const upb_MiniTable_Field* fields = mt->fields;
const int count = mt->field_count;
bool found = false; bool found = false;
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
if (fields[i].number == field_number) { if (fields[i].number == field_number) {
@ -2378,7 +2376,7 @@ static void create_fielddef(
symtab_add(ctx, full_name, pack_def(f, UPB_DEFTYPE_EXT)); symtab_add(ctx, full_name, pack_def(f, UPB_DEFTYPE_EXT));
f->layout_index = ctx->ext_count++; f->layout_index = ctx->ext_count++;
if (ctx->layout) { if (ctx->layout) {
UPB_ASSERT(ctx->file->ext_layouts[f->layout_index]->field.number == UPB_ASSERT(_upb_FieldDef_ExtensionMiniTable(f)->field.number ==
field_number); field_number);
} }
} }
@ -2446,7 +2444,7 @@ static void create_fielddef(
/* Repeated fields default to packed for proto3 only. */ /* Repeated fields default to packed for proto3 only. */
f->packed_ = upb_FieldDef_IsPrimitive(f) && f->packed_ = upb_FieldDef_IsPrimitive(f) &&
f->label_ == kUpb_Label_Repeated && f->label_ == kUpb_Label_Repeated &&
f->file->syntax == kUpb_Syntax_Proto3; upb_FileDef_Syntax(f->file) == kUpb_Syntax_Proto3;
} }
} }
@ -2458,7 +2456,7 @@ static void create_service(
const google_protobuf_MethodDescriptorProto* const* methods; const google_protobuf_MethodDescriptorProto* const* methods;
size_t i, n; size_t i, n;
s->file = ctx->file; /* Must happen prior to symtab_add. */ s->file = ctx->file; // Must happen prior to symtab_add()
name = google_protobuf_ServiceDescriptorProto_name(svc_proto); name = google_protobuf_ServiceDescriptorProto_name(svc_proto);
check_ident(ctx, name, false); check_ident(ctx, name, false);
@ -2654,7 +2652,7 @@ static void create_msgdef(symtab_addctx* ctx, const char* prefix,
size_t i, n_oneof, n_field, n_ext_range; size_t i, n_oneof, n_field, n_ext_range;
upb_StringView name; upb_StringView name;
m->file = ctx->file; /* Must happen prior to symtab_add(). */ m->file = ctx->file; // Must happen prior to symtab_add()
m->containing_type = containing_type; m->containing_type = containing_type;
name = google_protobuf_DescriptorProto_name(msg_proto); name = google_protobuf_DescriptorProto_name(msg_proto);
@ -2833,7 +2831,7 @@ static void resolve_extension(
(unsigned)f->number_, f->full_name, f->msgdef->full_name); (unsigned)f->number_, f->full_name, f->msgdef->full_name);
} }
const upb_MiniTable_Extension* ext = ctx->file->ext_layouts[f->layout_index]; const upb_MiniTable_Extension* ext = _upb_FieldDef_ExtensionMiniTable(f);
if (ctx->layout) { if (ctx->layout) {
UPB_ASSERT(upb_FieldDef_Number(f) == ext->field.number); UPB_ASSERT(upb_FieldDef_Number(f) == ext->field.number);
} else { } else {

Loading…
Cancel
Save