split apart constructors for extensions vs non-extensions

PiperOrigin-RevId: 506921160
pull/13171/head
Eric Salo 2 years ago committed by Copybara-Service
parent 662497f1d3
commit 0cc83d1adf
  1. 47
      upb/reflection/field_def.c
  2. 6
      upb/reflection/field_def_internal.h
  3. 3
      upb/reflection/file_def.c
  4. 6
      upb/reflection/message_def.c

@ -713,6 +713,24 @@ static void _upb_FieldDef_CreateNotExt(upb_DefBuilder* ctx, const char* prefix,
UPB_ASSERT(false); // It should be impossible to reach this point.
}
upb_FieldDef* _upb_Extensions_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(FieldDescriptorProto) * const* protos, const char* prefix,
upb_MessageDef* m) {
_upb_DefType_CheckPadding(sizeof(upb_FieldDef));
upb_FieldDef* defs =
(upb_FieldDef*)_upb_DefBuilder_Alloc(ctx, sizeof(upb_FieldDef) * n);
for (int i = 0; i < n; i++) {
upb_FieldDef* f = &defs[i];
_upb_FieldDef_CreateExt(ctx, prefix, protos[i], m, f);
f->index_ = i;
}
return defs;
}
upb_FieldDef* _upb_FieldDefs_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(FieldDescriptorProto) * const* protos, const char* prefix,
@ -721,28 +739,17 @@ upb_FieldDef* _upb_FieldDefs_New(
upb_FieldDef* defs =
(upb_FieldDef*)_upb_DefBuilder_Alloc(ctx, sizeof(upb_FieldDef) * n);
// If we are creating extensions then is_sorted will be NULL.
// If we are not creating extensions then is_sorted will be non-NULL.
if (is_sorted) {
uint32_t previous = 0;
for (int i = 0; i < n; i++) {
upb_FieldDef* f = &defs[i];
_upb_FieldDef_CreateNotExt(ctx, prefix, protos[i], m, f);
f->index_ = i;
if (!ctx->layout) f->layout_index = i;
uint32_t previous = 0;
for (int i = 0; i < n; i++) {
upb_FieldDef* f = &defs[i];
const uint32_t current = f->number_;
if (previous > current) *is_sorted = false;
previous = current;
}
} else {
for (int i = 0; i < n; i++) {
upb_FieldDef* f = &defs[i];
_upb_FieldDef_CreateNotExt(ctx, prefix, protos[i], m, f);
f->index_ = i;
if (!ctx->layout) f->layout_index = i;
_upb_FieldDef_CreateExt(ctx, prefix, protos[i], m, f);
f->index_ = i;
}
const uint32_t current = f->number_;
if (previous > current) *is_sorted = false;
previous = current;
}
return defs;

@ -48,6 +48,12 @@ uint64_t _upb_FieldDef_Modifiers(const upb_FieldDef* f);
void _upb_FieldDef_Resolve(upb_DefBuilder* ctx, const char* prefix,
upb_FieldDef* f);
// Allocate and initialize an array of |n| extensions (field defs).
upb_FieldDef* _upb_Extensions_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(FieldDescriptorProto) * const* protos, const char* prefix,
upb_MessageDef* m);
// Allocate and initialize an array of |n| field defs.
upb_FieldDef* _upb_FieldDefs_New(
upb_DefBuilder* ctx, int n,

@ -305,8 +305,7 @@ void _upb_FileDef_Create(upb_DefBuilder* ctx,
// Create extensions.
exts = UPB_DESC(FileDescriptorProto_extension)(file_proto, &n);
file->top_lvl_ext_count = n;
file->top_lvl_exts =
_upb_FieldDefs_New(ctx, n, exts, file->package, NULL, NULL);
file->top_lvl_exts = _upb_Extensions_New(ctx, n, exts, file->package, NULL);
// Create messages.
msgs = UPB_DESC(FileDescriptorProto_message_type)(file_proto, &n);

@ -370,7 +370,6 @@ void _upb_MessageDef_Resolve(upb_DefBuilder* ctx, upb_MessageDef* m) {
if (!ctx->layout) {
m->layout = _upb_MessageDef_MakeMiniTable(ctx, m);
if (!m->layout) _upb_DefBuilder_OomErr(ctx);
}
#ifndef NDEBUG
@ -637,8 +636,7 @@ static void create_msgdef(upb_DefBuilder* ctx, const char* prefix,
UPB_ASSERT(n_field == m->layout->field_count);
} else {
/* Allocate now (to allow cross-linking), populate later. */
m->layout = _upb_DefBuilder_Alloc(
ctx, sizeof(*m->layout) + sizeof(_upb_FastTable_Entry));
m->layout = _upb_DefBuilder_Alloc(ctx, sizeof(*m->layout));
}
UPB_DEF_SET_OPTIONS(m->opts, DescriptorProto, MessageOptions, msg_proto);
@ -681,7 +679,7 @@ static void create_msgdef(upb_DefBuilder* ctx, const char* prefix,
const UPB_DESC(FieldDescriptorProto)* const* exts =
UPB_DESC(DescriptorProto_extension)(msg_proto, &n_ext);
m->nested_ext_count = n_ext;
m->nested_exts = _upb_FieldDefs_New(ctx, n_ext, exts, m->full_name, m, NULL);
m->nested_exts = _upb_Extensions_New(ctx, n_ext, exts, m->full_name, m);
const UPB_DESC(DescriptorProto)* const* msgs =
UPB_DESC(DescriptorProto_nested_type)(msg_proto, &n_msg);

Loading…
Cancel
Save