use _upb_DefBuilder instead of _upb_AddDexCtx

(Because it is a better name and we can't properly bikeshed it without changing
it a few times first.)
Also removed it as an arg from a function that doesn't actually need it.

PiperOrigin-RevId: 470978322
pull/13171/head
Eric Salo 3 years ago committed by Copybara-Service
parent 02ef81247a
commit b987ef6249
  1. 326
      upb/def.c

@ -255,7 +255,7 @@ static const void* unpack_def(upb_value v, upb_deftype_t type) {
static upb_value pack_def(const void* ptr, upb_deftype_t type) {
// Our 3-bit pointer tagging requires all pointers to be multiples of 8.
// The arena will always yield 8-byte-aligned addresses, however we put
// the defs into arrays. For each element in the array to be 8-byte-aligned,
// the defs into arrays. For each element in the array to be 8-byte-aligned,
// the sizes of each def type must also be a multiple of 8.
//
// If any of these asserts fail, we need to add or remove padding on 32-bit
@ -380,7 +380,7 @@ typedef struct {
jmp_buf err; /* longjmp() on error. */
} upb_AddDefCtx;
UPB_NORETURN UPB_NOINLINE UPB_PRINTF(2, 3) static void _upb_AddDefCtx_Errf(
UPB_NORETURN UPB_NOINLINE UPB_PRINTF(2, 3) static void _upb_DefBuilder_Errf(
upb_AddDefCtx* ctx, const char* fmt, ...) {
va_list argp;
va_start(argp, fmt);
@ -389,16 +389,16 @@ UPB_NORETURN UPB_NOINLINE UPB_PRINTF(2, 3) static void _upb_AddDefCtx_Errf(
UPB_LONGJMP(ctx->err, 1);
}
UPB_NORETURN UPB_NOINLINE static void _upb_AddDefCtx_OomErr(
UPB_NORETURN UPB_NOINLINE static void _upb_DefBuilder_OomErr(
upb_AddDefCtx* ctx) {
upb_Status_SetErrorMessage(ctx->status, "out of memory");
UPB_LONGJMP(ctx->err, 1);
}
void* _upb_AddDefCtx_Alloc(upb_AddDefCtx* ctx, size_t bytes) {
void* _upb_DefBuilder_Alloc(upb_AddDefCtx* ctx, size_t bytes) {
if (bytes == 0) return NULL;
void* ret = upb_Arena_Malloc(ctx->arena, bytes);
if (!ret) _upb_AddDefCtx_OomErr(ctx);
if (!ret) _upb_DefBuilder_OomErr(ctx);
return ret;
}
@ -524,7 +524,7 @@ int32_t upb_ExtensionRange_End(const upb_ExtensionRange* e) { return e->end; }
// Allocate sufficient storage to contain an array of |n| extension ranges.
static upb_ExtensionRange* _upb_ExtensionRange_Alloc(upb_AddDefCtx* ctx,
int n) {
return _upb_AddDefCtx_Alloc(ctx, sizeof(upb_ExtensionRange) * n);
return _upb_DefBuilder_Alloc(ctx, sizeof(upb_ExtensionRange) * n);
}
/* upb_FieldDef ***************************************************************/
@ -744,7 +744,7 @@ bool upb_FieldDef_checkdescriptortype(int32_t type) {
// Allocate sufficient storage to contain an array of |n| field defs.
static upb_FieldDef* _upb_FieldDef_Alloc(upb_AddDefCtx* ctx, int n) {
return _upb_AddDefCtx_Alloc(ctx, sizeof(upb_FieldDef) * n);
return _upb_DefBuilder_Alloc(ctx, sizeof(upb_FieldDef) * n);
}
/* upb_MessageDef *************************************************************/
@ -1302,9 +1302,9 @@ const upb_FileDef* upb_DefPool_FindFileContainingSymbol(const upb_DefPool* s,
* 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_OOM(x) \
if (!(x)) { \
_upb_AddDefCtx_OomErr(ctx); \
#define CHK_OOM(x) \
if (!(x)) { \
_upb_DefBuilder_OomErr(ctx); \
}
// We want to copy the options verbatim into the destination options proto.
@ -1321,8 +1321,8 @@ const upb_FileDef* upb_DefPool_FindFileContainingSymbol(const upb_DefPool* s,
target = (const google_protobuf_##options_type*)opt_default; \
}
static void _upb_AddDefCtx_CheckIdent(upb_AddDefCtx* ctx, upb_StringView name,
bool full) {
static void _upb_DefBuilder_CheckIdent(upb_AddDefCtx* ctx, upb_StringView name,
bool full) {
const char* str = name.data;
size_t len = name.size;
bool start = true;
@ -1331,13 +1331,13 @@ static void _upb_AddDefCtx_CheckIdent(upb_AddDefCtx* ctx, upb_StringView name,
char c = str[i];
if (c == '.') {
if (start || !full) {
_upb_AddDefCtx_Errf(ctx, "invalid name: unexpected '.' (%.*s)",
(int)len, str);
_upb_DefBuilder_Errf(ctx, "invalid name: unexpected '.' (%.*s)",
(int)len, str);
}
start = true;
} else if (start) {
if (!upb_isletter(c)) {
_upb_AddDefCtx_Errf(
_upb_DefBuilder_Errf(
ctx,
"invalid name: path components must start with a letter (%.*s)",
(int)len, str);
@ -1345,14 +1345,14 @@ static void _upb_AddDefCtx_CheckIdent(upb_AddDefCtx* ctx, upb_StringView name,
start = false;
} else {
if (!upb_isalphanum(c)) {
_upb_AddDefCtx_Errf(ctx,
"invalid name: non-alphanumeric character (%.*s)",
(int)len, str);
_upb_DefBuilder_Errf(ctx,
"invalid name: non-alphanumeric character (%.*s)",
(int)len, str);
}
}
}
if (start) {
_upb_AddDefCtx_Errf(ctx, "invalid name: empty part (%.*s)", (int)len, str);
_upb_DefBuilder_Errf(ctx, "invalid name: empty part (%.*s)", (int)len, str);
}
}
@ -1398,9 +1398,9 @@ static uint32_t upb_MiniTable_place(upb_AddDefCtx* ctx, upb_MiniTable* l,
size_t next = ofs + size;
if (next > UINT16_MAX) {
_upb_AddDefCtx_Errf(ctx,
"size of message %s exceeded max size of %zu bytes",
upb_MessageDef_FullName(m), (size_t)UINT16_MAX);
_upb_DefBuilder_Errf(ctx,
"size of message %s exceeded max size of %zu bytes",
upb_MessageDef_FullName(m), (size_t)UINT16_MAX);
}
l->size = next;
@ -1525,8 +1525,8 @@ static void make_layout(upb_AddDefCtx* ctx, const upb_MessageDef* m) {
}
}
fields = _upb_AddDefCtx_Alloc(ctx, field_count * sizeof(*fields));
subs = _upb_AddDefCtx_Alloc(ctx, sublayout_count * sizeof(*subs));
fields = _upb_DefBuilder_Alloc(ctx, field_count * sizeof(*fields));
subs = _upb_DefBuilder_Alloc(ctx, sublayout_count * sizeof(*subs));
l->field_count = upb_MessageDef_numfields(m);
l->fields = fields;
@ -1601,8 +1601,8 @@ static void make_layout(upb_AddDefCtx* ctx, const upb_MessageDef* m) {
if (upb_FieldDef_Label(f) == kUpb_Label_Required) {
field->presence = ++hasbit;
if (hasbit >= 63) {
_upb_AddDefCtx_Errf(ctx, "Message with >=63 required fields: %s",
upb_MessageDef_FullName(m));
_upb_DefBuilder_Errf(ctx, "Message with >=63 required fields: %s",
upb_MessageDef_FullName(m));
}
l->required_count++;
}
@ -1667,8 +1667,8 @@ static void make_layout(upb_AddDefCtx* ctx, const upb_MessageDef* m) {
if (upb_OneofDef_IsSynthetic(o)) continue;
if (o->field_count == 0) {
_upb_AddDefCtx_Errf(ctx, "Oneof must have at least one field (%s)",
o->full_name);
_upb_DefBuilder_Errf(ctx, "Oneof must have at least one field (%s)",
o->full_name);
}
/* Calculate field size: the max of all field sizes. */
@ -1714,13 +1714,13 @@ static bool streql_view(upb_StringView view, const char* b) {
return streql2(view.data, view.size, b);
}
static const char* _upb_AddDefCtx_MakeFullName(upb_AddDefCtx* ctx,
const char* prefix,
upb_StringView name) {
static const char* _upb_DefBuilder_MakeFullName(upb_AddDefCtx* ctx,
const char* prefix,
upb_StringView name) {
if (prefix) {
/* ret = prefix + '.' + name; */
size_t n = strlen(prefix);
char* ret = _upb_AddDefCtx_Alloc(ctx, n + name.size + 2);
char* ret = _upb_DefBuilder_Alloc(ctx, n + name.size + 2);
strcpy(ret, prefix);
ret[n] = '.';
memcpy(&ret[n + 1], name.data, name.size);
@ -1740,21 +1740,21 @@ static void finalize_oneofs(upb_AddDefCtx* ctx, upb_MessageDef* m) {
upb_OneofDef* o = &mutable_oneofs[i];
if (o->synthetic && o->field_count != 1) {
_upb_AddDefCtx_Errf(ctx,
"Synthetic oneofs must have one field, not %d: %s",
o->field_count, upb_OneofDef_Name(o));
_upb_DefBuilder_Errf(ctx,
"Synthetic oneofs must have one field, not %d: %s",
o->field_count, upb_OneofDef_Name(o));
}
if (o->synthetic) {
synthetic_count++;
} else if (synthetic_count != 0) {
_upb_AddDefCtx_Errf(ctx,
"Synthetic oneofs must be after all other oneofs: %s",
upb_OneofDef_Name(o));
_upb_DefBuilder_Errf(
ctx, "Synthetic oneofs must be after all other oneofs: %s",
upb_OneofDef_Name(o));
}
o->fields =
_upb_AddDefCtx_Alloc(ctx, sizeof(upb_FieldDef*) * o->field_count);
_upb_DefBuilder_Alloc(ctx, sizeof(upb_FieldDef*) * o->field_count);
o->field_count = 0;
}
@ -1811,7 +1811,7 @@ size_t getjsonname(const char* name, char* buf, size_t len) {
static char* makejsonname(upb_AddDefCtx* ctx, const char* name) {
size_t size = getjsonname(name, NULL, 0);
char* json_name = _upb_AddDefCtx_Alloc(ctx, size);
char* json_name = _upb_DefBuilder_Alloc(ctx, size);
getjsonname(name, json_name, size);
return json_name;
}
@ -1819,12 +1819,12 @@ static char* makejsonname(upb_AddDefCtx* ctx, const char* name) {
/* Adds a symbol |v| to the symtab, which must be a def pointer previously
* packed with pack_def(). The def's pointer to upb_FileDef* must be set before
* adding, so we know which entries to remove if building this file fails. */
static void _upb_AddDefCtx_Add(upb_AddDefCtx* ctx, const char* name,
upb_value v) {
static void _upb_DefBuilder_Add(upb_AddDefCtx* ctx, const char* name,
upb_value v) {
// TODO: table should support an operation "tryinsert" to avoid the double
// lookup.
if (upb_strtable_lookup(&ctx->symtab->syms, name, NULL)) {
_upb_AddDefCtx_Errf(ctx, "duplicate symbol '%s'", name);
_upb_DefBuilder_Errf(ctx, "duplicate symbol '%s'", name);
}
size_t len = strlen(name);
CHK_OOM(upb_strtable_insert(&ctx->symtab->syms, name, len, v,
@ -1888,8 +1888,8 @@ static const void* symtab_resolveany(upb_AddDefCtx* ctx,
return unpack_def(v, *type);
notfound:
_upb_AddDefCtx_Errf(ctx, "couldn't resolve name '" UPB_STRINGVIEW_FORMAT "'",
UPB_STRINGVIEW_ARGS(sym));
_upb_DefBuilder_Errf(ctx, "couldn't resolve name '" UPB_STRINGVIEW_FORMAT "'",
UPB_STRINGVIEW_ARGS(sym));
}
static const void* symtab_resolve(upb_AddDefCtx* ctx, const char* from_name_dbg,
@ -1899,10 +1899,10 @@ static const void* symtab_resolve(upb_AddDefCtx* ctx, const char* from_name_dbg,
const void* ret =
symtab_resolveany(ctx, from_name_dbg, base, sym, &found_type);
if (ret && found_type != type) {
_upb_AddDefCtx_Errf(ctx,
"type mismatch when resolving %s: couldn't find "
"name " UPB_STRINGVIEW_FORMAT " with type=%d",
from_name_dbg, UPB_STRINGVIEW_ARGS(sym), (int)type);
_upb_DefBuilder_Errf(ctx,
"type mismatch when resolving %s: couldn't find "
"name " UPB_STRINGVIEW_FORMAT " with type=%d",
from_name_dbg, UPB_STRINGVIEW_ARGS(sym), (int)type);
}
return ret;
}
@ -1915,7 +1915,7 @@ static void create_oneofdef(upb_AddDefCtx* ctx, upb_MessageDef* m,
upb_value v;
o->parent = m;
o->full_name = _upb_AddDefCtx_MakeFullName(ctx, m->full_name, name);
o->full_name = _upb_DefBuilder_MakeFullName(ctx, m->full_name, name);
o->field_count = 0;
o->synthetic = false;
@ -1923,7 +1923,7 @@ static void create_oneofdef(upb_AddDefCtx* ctx, upb_MessageDef* m,
upb_value existing_v;
if (upb_strtable_lookup2(&m->ntof, name.data, name.size, &existing_v)) {
_upb_AddDefCtx_Errf(ctx, "duplicate oneof name (%s)", o->full_name);
_upb_DefBuilder_Errf(ctx, "duplicate oneof name (%s)", o->full_name);
}
v = pack_def(o, UPB_DEFTYPE_ONEOF);
@ -1937,7 +1937,7 @@ static void create_oneofdef(upb_AddDefCtx* ctx, upb_MessageDef* m,
static upb_OneofDef* _upb_OneofDefs_New(
upb_AddDefCtx* ctx, int n, const google_protobuf_OneofDescriptorProto* const* protos,
upb_MessageDef* m) {
upb_OneofDef* o = _upb_AddDefCtx_Alloc(ctx, sizeof(upb_OneofDef) * n);
upb_OneofDef* o = _upb_DefBuilder_Alloc(ctx, sizeof(upb_OneofDef) * n);
for (int i = 0; i < n; i++) {
create_oneofdef(ctx, m, protos[i], &o[i]);
}
@ -1945,7 +1945,7 @@ static upb_OneofDef* _upb_OneofDefs_New(
}
static str_t* newstr(upb_AddDefCtx* ctx, const char* data, size_t len) {
str_t* ret = _upb_AddDefCtx_Alloc(ctx, sizeof(*ret) + len);
str_t* ret = _upb_DefBuilder_Alloc(ctx, sizeof(*ret) + len);
CHK_OOM(ret);
ret->len = len;
if (len) memcpy(ret->str, data, len);
@ -1961,8 +1961,7 @@ static bool upb_DefPool_TryGetChar(const char** src, const char* end,
return true;
}
static char upb_DefPool_TryGetHexDigit(upb_AddDefCtx* ctx,
const upb_FieldDef* f, const char** src,
static char upb_DefPool_TryGetHexDigit(const upb_FieldDef* f, const char** src,
const char* end) {
char ch;
if (!upb_DefPool_TryGetChar(src, end, &ch)) return -1;
@ -1980,20 +1979,20 @@ static char upb_DefPool_TryGetHexDigit(upb_AddDefCtx* ctx,
static char upb_DefPool_ParseHexEscape(upb_AddDefCtx* ctx,
const upb_FieldDef* f, const char** src,
const char* end) {
char hex_digit = upb_DefPool_TryGetHexDigit(ctx, f, src, end);
char hex_digit = upb_DefPool_TryGetHexDigit(f, src, end);
if (hex_digit < 0) {
_upb_AddDefCtx_Errf(
_upb_DefBuilder_Errf(
ctx, "\\x cannot be followed by non-hex digit in field '%s' default",
upb_FieldDef_FullName(f));
return 0;
}
unsigned int ret = hex_digit;
while ((hex_digit = upb_DefPool_TryGetHexDigit(ctx, f, src, end)) >= 0) {
while ((hex_digit = upb_DefPool_TryGetHexDigit(f, src, end)) >= 0) {
ret = (ret << 4) | hex_digit;
}
if (ret > 0xff) {
_upb_AddDefCtx_Errf(ctx, "Value of hex escape in field %s exceeds 8 bits",
upb_FieldDef_FullName(f));
_upb_DefBuilder_Errf(ctx, "Value of hex escape in field %s exceeds 8 bits",
upb_FieldDef_FullName(f));
return 0;
}
return ret;
@ -2026,8 +2025,8 @@ static char upb_DefPool_ParseEscape(upb_AddDefCtx* ctx, const upb_FieldDef* f,
const char** src, const char* end) {
char ch;
if (!upb_DefPool_TryGetChar(src, end, &ch)) {
_upb_AddDefCtx_Errf(ctx, "unterminated escape sequence in field %s",
upb_FieldDef_FullName(f));
_upb_DefBuilder_Errf(ctx, "unterminated escape sequence in field %s",
upb_FieldDef_FullName(f));
return 0;
}
switch (ch) {
@ -2067,13 +2066,13 @@ static char upb_DefPool_ParseEscape(upb_AddDefCtx* ctx, const upb_FieldDef* f,
*src -= 1;
return upb_DefPool_ParseOctalEscape(ctx, f, src, end);
}
_upb_AddDefCtx_Errf(ctx, "Unknown escape sequence: \\%c", ch);
_upb_DefBuilder_Errf(ctx, "Unknown escape sequence: \\%c", ch);
}
static str_t* unescape(upb_AddDefCtx* ctx, const upb_FieldDef* f,
const char* data, size_t len) {
// Size here is an upper bound; escape sequences could ultimately shrink it.
str_t* ret = _upb_AddDefCtx_Alloc(ctx, sizeof(*ret) + len);
str_t* ret = _upb_DefBuilder_Alloc(ctx, sizeof(*ret) + len);
char* dst = &ret->str[0];
const char* src = data;
const char* end = data + len;
@ -2106,7 +2105,7 @@ static void parse_default(upb_AddDefCtx* ctx, const char* str, size_t len,
case kUpb_CType_Float:
/* Standard C number parsing functions expect null-terminated strings. */
if (len >= sizeof(nullz) - 1) {
_upb_AddDefCtx_Errf(ctx, "Default too long: %.*s", (int)len, str);
_upb_DefBuilder_Errf(ctx, "Default too long: %.*s", (int)len, str);
}
memcpy(nullz, str, len);
nullz[len] = '\0';
@ -2193,16 +2192,16 @@ static void parse_default(upb_AddDefCtx* ctx, const char* str, size_t len,
break;
case kUpb_CType_Message:
/* Should not have a default value. */
_upb_AddDefCtx_Errf(ctx, "Message should not have a default (%s)",
upb_FieldDef_FullName(f));
_upb_DefBuilder_Errf(ctx, "Message should not have a default (%s)",
upb_FieldDef_FullName(f));
}
return;
invalid:
_upb_AddDefCtx_Errf(ctx, "Invalid default '%.*s' for field %s of type %d",
(int)len, str, upb_FieldDef_FullName(f),
(int)upb_FieldDef_Type(f));
_upb_DefBuilder_Errf(ctx, "Invalid default '%.*s' for field %s of type %d",
(int)len, str, upb_FieldDef_FullName(f),
(int)upb_FieldDef_Type(f));
}
static void set_default_default(upb_AddDefCtx* ctx, upb_FieldDef* f) {
@ -2246,15 +2245,15 @@ static void create_fielddef(upb_AddDefCtx* ctx, const char* prefix,
const char* shortname;
int32_t field_number;
f->file = ctx->file; // Must happen prior to _upb_AddDefCtx_Add()
f->file = ctx->file; // Must happen prior to _upb_DefBuilder_Add()
if (!google_protobuf_FieldDescriptorProto_has_name(field_proto)) {
_upb_AddDefCtx_Errf(ctx, "field has no name");
_upb_DefBuilder_Errf(ctx, "field has no name");
}
name = google_protobuf_FieldDescriptorProto_name(field_proto);
_upb_AddDefCtx_CheckIdent(ctx, name, false);
full_name = _upb_AddDefCtx_MakeFullName(ctx, prefix, name);
_upb_DefBuilder_CheckIdent(ctx, name, false);
full_name = _upb_DefBuilder_MakeFullName(ctx, prefix, name);
shortname = shortdefname(full_name);
if (google_protobuf_FieldDescriptorProto_has_json_name(field_proto)) {
@ -2288,13 +2287,13 @@ static void create_fielddef(upb_AddDefCtx* ctx, const char* prefix,
case kUpb_FieldType_Group:
case kUpb_FieldType_Enum:
if (!has_type_name) {
_upb_AddDefCtx_Errf(ctx, "field of type %d requires type name (%s)",
(int)f->type_, full_name);
_upb_DefBuilder_Errf(ctx, "field of type %d requires type name (%s)",
(int)f->type_, full_name);
}
break;
default:
if (has_type_name) {
_upb_AddDefCtx_Errf(
_upb_DefBuilder_Errf(
ctx, "invalid type for field with type_name set (%s, %d)",
full_name, (int)f->type_);
}
@ -2310,7 +2309,7 @@ static void create_fielddef(upb_AddDefCtx* ctx, const char* prefix,
size_t json_size;
if (field_number <= 0 || field_number > kUpb_MaxFieldNumber) {
_upb_AddDefCtx_Errf(ctx, "invalid field number (%u)", field_number);
_upb_DefBuilder_Errf(ctx, "invalid field number (%u)", field_number);
}
f->index_ = f - m->fields;
@ -2323,7 +2322,7 @@ static void create_fielddef(upb_AddDefCtx* ctx, const char* prefix,
json_size = strlen(json_name);
if (upb_strtable_lookup(&m->ntof, shortname, &existing_v)) {
_upb_AddDefCtx_Errf(ctx, "duplicate field name (%s)", shortname);
_upb_DefBuilder_Errf(ctx, "duplicate field name (%s)", shortname);
}
CHK_OOM(upb_strtable_insert(&m->ntof, name.data, name.size, field_v,
@ -2331,7 +2330,7 @@ static void create_fielddef(upb_AddDefCtx* ctx, const char* prefix,
if (strcmp(shortname, json_name) != 0) {
if (upb_strtable_lookup(&m->ntof, json_name, &v)) {
_upb_AddDefCtx_Errf(ctx, "duplicate json_name (%s)", json_name);
_upb_DefBuilder_Errf(ctx, "duplicate json_name (%s)", json_name);
} else {
CHK_OOM(upb_strtable_insert(&m->ntof, json_name, json_size, json_v,
ctx->arena));
@ -2339,7 +2338,7 @@ static void create_fielddef(upb_AddDefCtx* ctx, const char* prefix,
}
if (upb_inttable_lookup(&m->itof, field_number, NULL)) {
_upb_AddDefCtx_Errf(ctx, "duplicate field number (%u)", field_number);
_upb_DefBuilder_Errf(ctx, "duplicate field number (%u)", field_number);
}
CHK_OOM(upb_inttable_insert(&m->itof, field_number, v, ctx->arena));
@ -2362,7 +2361,7 @@ static void create_fielddef(upb_AddDefCtx* ctx, const char* prefix,
/* extension field. */
f->is_extension_ = true;
f->scope.extension_scope = m;
_upb_AddDefCtx_Add(ctx, full_name, pack_def(f, UPB_DEFTYPE_EXT));
_upb_DefBuilder_Add(ctx, full_name, pack_def(f, UPB_DEFTYPE_EXT));
f->layout_index = ctx->ext_count++;
if (ctx->layout) {
UPB_ASSERT(_upb_FieldDef_ExtensionMiniTable(f)->field.number ==
@ -2371,13 +2370,13 @@ static void create_fielddef(upb_AddDefCtx* ctx, const char* prefix,
}
if (f->type_ < kUpb_FieldType_Double || f->type_ > kUpb_FieldType_SInt64) {
_upb_AddDefCtx_Errf(ctx, "invalid type for field %s (%d)", f->full_name,
f->type_);
_upb_DefBuilder_Errf(ctx, "invalid type for field %s (%d)", f->full_name,
f->type_);
}
if (f->label_ < kUpb_Label_Optional || f->label_ > kUpb_Label_Repeated) {
_upb_AddDefCtx_Errf(ctx, "invalid label for field %s (%d)", f->full_name,
f->label_);
_upb_DefBuilder_Errf(ctx, "invalid label for field %s (%d)", f->full_name,
f->label_);
}
/* We can't resolve the subdef or (in the case of extensions) the containing
@ -2387,8 +2386,8 @@ static void create_fielddef(upb_AddDefCtx* ctx, const char* prefix,
if (f->label_ == kUpb_Label_Required &&
f->file->syntax == kUpb_Syntax_Proto3) {
_upb_AddDefCtx_Errf(ctx, "proto3 fields cannot be required (%s)",
f->full_name);
_upb_DefBuilder_Errf(ctx, "proto3 fields cannot be required (%s)",
f->full_name);
}
if (google_protobuf_FieldDescriptorProto_has_oneof_index(field_proto)) {
@ -2397,17 +2396,17 @@ static void create_fielddef(upb_AddDefCtx* ctx, const char* prefix,
upb_value v = upb_value_constptr(f);
if (upb_FieldDef_Label(f) != kUpb_Label_Optional) {
_upb_AddDefCtx_Errf(ctx, "fields in oneof must have OPTIONAL label (%s)",
f->full_name);
_upb_DefBuilder_Errf(ctx, "fields in oneof must have OPTIONAL label (%s)",
f->full_name);
}
if (!m) {
_upb_AddDefCtx_Errf(ctx, "oneof_index provided for extension field (%s)",
f->full_name);
_upb_DefBuilder_Errf(ctx, "oneof_index provided for extension field (%s)",
f->full_name);
}
if (oneof_index >= m->oneof_count) {
_upb_AddDefCtx_Errf(ctx, "oneof_index out of range (%s)", f->full_name);
_upb_DefBuilder_Errf(ctx, "oneof_index out of range (%s)", f->full_name);
}
oneof = (upb_OneofDef*)&m->oneofs[oneof_index];
@ -2422,9 +2421,9 @@ static void create_fielddef(upb_AddDefCtx* ctx, const char* prefix,
upb_strtable_insert(&oneof->ntof, name.data, name.size, v, ctx->arena));
} else {
if (f->proto3_optional_) {
_upb_AddDefCtx_Errf(ctx,
"field with proto3_optional was not in a oneof (%s)",
f->full_name);
_upb_DefBuilder_Errf(ctx,
"field with proto3_optional was not in a oneof (%s)",
f->full_name);
}
}
@ -2446,7 +2445,7 @@ static void create_method(upb_AddDefCtx* ctx,
upb_StringView name = google_protobuf_MethodDescriptorProto_name(method_proto);
m->service = s;
m->full_name = _upb_AddDefCtx_MakeFullName(ctx, s->full_name, name);
m->full_name = _upb_DefBuilder_MakeFullName(ctx, s->full_name, name);
m->client_streaming =
google_protobuf_MethodDescriptorProto_client_streaming(method_proto);
m->server_streaming =
@ -2465,7 +2464,7 @@ static void create_method(upb_AddDefCtx* ctx,
static upb_MethodDef* _upb_MethodDefs_New(
upb_AddDefCtx* ctx, int n,
const google_protobuf_MethodDescriptorProto* const* protos, upb_ServiceDef* s) {
upb_MethodDef* m = _upb_AddDefCtx_Alloc(ctx, sizeof(upb_MethodDef) * n);
upb_MethodDef* m = _upb_DefBuilder_Alloc(ctx, sizeof(upb_MethodDef) * n);
for (int i = 0; i < n; i++) {
create_method(ctx, protos[i], s, &m[i]);
m[i].index = i;
@ -2479,12 +2478,12 @@ static void create_service(upb_AddDefCtx* ctx,
upb_StringView name;
size_t n;
s->file = ctx->file; // Must happen prior to _upb_AddDefCtx_Add()
s->file = ctx->file; // Must happen prior to _upb_DefBuilder_Add()
name = google_protobuf_ServiceDescriptorProto_name(svc_proto);
_upb_AddDefCtx_CheckIdent(ctx, name, false);
s->full_name = _upb_AddDefCtx_MakeFullName(ctx, ctx->file->package, name);
_upb_AddDefCtx_Add(ctx, s->full_name, pack_def(s, UPB_DEFTYPE_SERVICE));
_upb_DefBuilder_CheckIdent(ctx, name, false);
s->full_name = _upb_DefBuilder_MakeFullName(ctx, ctx->file->package, name);
_upb_DefBuilder_Add(ctx, s->full_name, pack_def(s, UPB_DEFTYPE_SERVICE));
const google_protobuf_MethodDescriptorProto* const* methods =
google_protobuf_ServiceDescriptorProto_method(svc_proto, &n);
@ -2498,7 +2497,7 @@ static void create_service(upb_AddDefCtx* ctx,
static upb_ServiceDef* _upb_ServiceDefs_New(
upb_AddDefCtx* ctx, int n,
const google_protobuf_ServiceDescriptorProto* const* protos) {
upb_ServiceDef* s = _upb_AddDefCtx_Alloc(ctx, sizeof(upb_ServiceDef) * n);
upb_ServiceDef* s = _upb_DefBuilder_Alloc(ctx, sizeof(upb_ServiceDef) * n);
for (int i = 0; i < n; i++) {
create_service(ctx, protos[i], &s[i]);
s[i].index = i;
@ -2525,13 +2524,14 @@ static int compare_int32(const void* a_ptr, const void* b_ptr) {
static upb_MiniTable_Enum* create_enumlayout(upb_AddDefCtx* ctx,
const upb_EnumDef* e) {
const char* desc = _upb_EnumDef_MiniDescriptor(e, ctx->tmp_arena);
if (!desc) _upb_AddDefCtx_Errf(ctx, "OOM while building enum MiniDescriptor");
if (!desc)
_upb_DefBuilder_Errf(ctx, "OOM while building enum MiniDescriptor");
upb_Status status;
upb_MiniTable_Enum* layout =
upb_MiniTable_BuildEnum(desc, strlen(desc), ctx->arena, &status);
if (!layout)
_upb_AddDefCtx_Errf(ctx, "Error building enum MiniTable: %s", status.msg);
_upb_DefBuilder_Errf(ctx, "Error building enum MiniTable: %s", status.msg);
return layout;
}
@ -2541,10 +2541,10 @@ static void create_enumvaldef(upb_AddDefCtx* ctx, const char* prefix,
upb_StringView name = google_protobuf_EnumValueDescriptorProto_name(val_proto);
upb_value val = upb_value_constptr(v);
v->parent = e; // Must happen prior to _upb_AddDefCtx_Add()
v->full_name = _upb_AddDefCtx_MakeFullName(ctx, prefix, name);
v->parent = e; // Must happen prior to _upb_DefBuilder_Add()
v->full_name = _upb_DefBuilder_MakeFullName(ctx, prefix, name);
v->number = google_protobuf_EnumValueDescriptorProto_number(val_proto);
_upb_AddDefCtx_Add(ctx, v->full_name, pack_def(v, UPB_DEFTYPE_ENUMVAL));
_upb_DefBuilder_Add(ctx, v->full_name, pack_def(v, UPB_DEFTYPE_ENUMVAL));
SET_OPTIONS(v->opts, EnumValueDescriptorProto, EnumValueOptions, val_proto);
@ -2561,7 +2561,8 @@ static void create_enumvaldef(upb_AddDefCtx* ctx, const char* prefix,
static upb_EnumValueDef* _upb_EnumValueDefs_New(
upb_AddDefCtx* ctx, const char* prefix, int n,
const google_protobuf_EnumValueDescriptorProto* const* protos, upb_EnumDef* e) {
upb_EnumValueDef* v = _upb_AddDefCtx_Alloc(ctx, sizeof(upb_EnumValueDef) * n);
upb_EnumValueDef* v =
_upb_DefBuilder_Alloc(ctx, sizeof(upb_EnumValueDef) * n);
bool is_sorted = true;
uint32_t previous = 0;
@ -2576,9 +2577,9 @@ static upb_EnumValueDef* _upb_EnumValueDefs_New(
if (upb_FileDef_Syntax(ctx->file) == kUpb_Syntax_Proto3 && n > 0 &&
v[0].number != 0) {
_upb_AddDefCtx_Errf(ctx,
"for proto3, the first enum value must be zero (%s)",
e->full_name);
_upb_DefBuilder_Errf(ctx,
"for proto3, the first enum value must be zero (%s)",
e->full_name);
}
return v;
@ -2591,13 +2592,13 @@ static void create_enumdef(upb_AddDefCtx* ctx, const char* prefix,
upb_StringView name;
size_t n;
e->file = ctx->file; // Must happen prior to _upb_AddDefCtx_Add()
e->file = ctx->file; // Must happen prior to _upb_DefBuilder_Add()
name = google_protobuf_EnumDescriptorProto_name(enum_proto);
_upb_AddDefCtx_CheckIdent(ctx, name, false);
_upb_DefBuilder_CheckIdent(ctx, name, false);
e->full_name = _upb_AddDefCtx_MakeFullName(ctx, prefix, name);
_upb_AddDefCtx_Add(ctx, e->full_name, pack_def(e, UPB_DEFTYPE_ENUM));
e->full_name = _upb_DefBuilder_MakeFullName(ctx, prefix, name);
_upb_DefBuilder_Add(ctx, e->full_name, pack_def(e, UPB_DEFTYPE_ENUM));
values = google_protobuf_EnumDescriptorProto_value(enum_proto, &n);
CHK_OOM(upb_strtable_init(&e->ntoi, n, ctx->arena));
@ -2608,8 +2609,8 @@ static void create_enumdef(upb_AddDefCtx* ctx, const char* prefix,
e->values = _upb_EnumValueDefs_New(ctx, prefix, n, values, e);
if (n == 0) {
_upb_AddDefCtx_Errf(ctx, "enums must contain at least one value (%s)",
e->full_name);
_upb_DefBuilder_Errf(ctx, "enums must contain at least one value (%s)",
e->full_name);
}
SET_OPTIONS(e->opts, EnumDescriptorProto, EnumOptions, enum_proto);
@ -2640,7 +2641,7 @@ static upb_EnumDef* _upb_EnumDefs_New(
const char* name =
containing_type ? containing_type->full_name : ctx->file->package;
upb_EnumDef* e = _upb_AddDefCtx_Alloc(ctx, sizeof(upb_EnumDef) * n);
upb_EnumDef* e = _upb_DefBuilder_Alloc(ctx, sizeof(upb_EnumDef) * n);
for (size_t i = 0; i < n; i++) {
create_enumdef(ctx, name, protos[i], &e[i]);
e[i].containing_type = containing_type;
@ -2663,14 +2664,14 @@ static void create_msgdef(upb_AddDefCtx* ctx, const char* prefix,
size_t i, n_oneof, n_field, n_ext_range;
upb_StringView name;
m->file = ctx->file; // Must happen prior to _upb_AddDefCtx_Add()
m->file = ctx->file; // Must happen prior to _upb_DefBuilder_Add()
m->containing_type = containing_type;
name = google_protobuf_DescriptorProto_name(msg_proto);
_upb_AddDefCtx_CheckIdent(ctx, name, false);
_upb_DefBuilder_CheckIdent(ctx, name, false);
m->full_name = _upb_AddDefCtx_MakeFullName(ctx, prefix, name);
_upb_AddDefCtx_Add(ctx, m->full_name, pack_def(m, UPB_DEFTYPE_MSG));
m->full_name = _upb_DefBuilder_MakeFullName(ctx, prefix, name);
_upb_DefBuilder_Add(ctx, m->full_name, pack_def(m, UPB_DEFTYPE_MSG));
oneofs = google_protobuf_DescriptorProto_oneof_decl(msg_proto, &n_oneof);
fields = google_protobuf_DescriptorProto_field(msg_proto, &n_field);
@ -2687,7 +2688,7 @@ static void create_msgdef(upb_AddDefCtx* ctx, const char* prefix,
UPB_ASSERT(n_field == m->layout->field_count);
} else {
/* Allocate now (to allow cross-linking), populate later. */
m->layout = _upb_AddDefCtx_Alloc(
m->layout = _upb_DefBuilder_Alloc(
ctx, sizeof(*m->layout) + sizeof(_upb_FastTable_Entry));
}
@ -2719,9 +2720,9 @@ static void create_msgdef(upb_AddDefCtx* ctx, const char* prefix,
// none of the fields overlap with the extension ranges, but we are just
// sanity checking here.
if (start < 1 || end <= start || end > max) {
_upb_AddDefCtx_Errf(ctx,
"Extension range (%d, %d) is invalid, message=%s\n",
(int)start, (int)end, m->full_name);
_upb_DefBuilder_Errf(ctx,
"Extension range (%d, %d) is invalid, message=%s\n",
(int)start, (int)end, m->full_name);
}
r_def->start = start;
@ -2742,7 +2743,7 @@ static upb_MessageDef* _upb_MessageDefs_New(
const upb_MessageDef* containing_type) {
const char* name =
containing_type ? containing_type->full_name : ctx->file->package;
upb_MessageDef* m = _upb_AddDefCtx_Alloc(ctx, sizeof(upb_MessageDef) * n);
upb_MessageDef* m = _upb_DefBuilder_Alloc(ctx, sizeof(upb_MessageDef) * n);
for (int i = 0; i < n; i++) {
create_msgdef(ctx, name, protos[i], containing_type, &m[i]);
}
@ -2800,8 +2801,8 @@ static void resolve_subdef(upb_AddDefCtx* ctx, const char* prefix,
// this being a group.
break;
default:
_upb_AddDefCtx_Errf(ctx, "Couldn't resolve type name for field %s",
f->full_name);
_upb_DefBuilder_Errf(ctx, "Couldn't resolve type name for field %s",
f->full_name);
}
}
case kUpb_FieldType_Message:
@ -2825,8 +2826,8 @@ static void resolve_extension(upb_AddDefCtx* ctx, const char* prefix,
upb_FieldDef* f,
const google_protobuf_FieldDescriptorProto* field_proto) {
if (!google_protobuf_FieldDescriptorProto_has_extendee(field_proto)) {
_upb_AddDefCtx_Errf(ctx, "extension for field '%s' had no extendee",
f->full_name);
_upb_DefBuilder_Errf(ctx, "extension for field '%s' had no extendee",
f->full_name);
}
upb_StringView name = google_protobuf_FieldDescriptorProto_extendee(field_proto);
@ -2845,7 +2846,7 @@ static void resolve_extension(upb_AddDefCtx* ctx, const char* prefix,
}
if (!found) {
_upb_AddDefCtx_Errf(
_upb_DefBuilder_Errf(
ctx,
"field number %u in extension %s has no extension range in "
"message %s",
@ -2878,15 +2879,15 @@ static void resolve_default(upb_AddDefCtx* ctx, upb_FieldDef* f,
google_protobuf_FieldDescriptorProto_default_value(field_proto);
if (upb_FileDef_Syntax(f->file) == kUpb_Syntax_Proto3) {
_upb_AddDefCtx_Errf(ctx,
"proto3 fields cannot have explicit defaults (%s)",
f->full_name);
_upb_DefBuilder_Errf(ctx,
"proto3 fields cannot have explicit defaults (%s)",
f->full_name);
}
if (upb_FieldDef_IsSubMessage(f)) {
_upb_AddDefCtx_Errf(ctx,
"message fields cannot have explicit defaults (%s)",
f->full_name);
_upb_DefBuilder_Errf(ctx,
"message fields cannot have explicit defaults (%s)",
f->full_name);
}
parse_default(ctx, defaultval.data, defaultval.size, f);
@ -2951,7 +2952,7 @@ static int count_exts_in_msg(const google_protobuf_DescriptorProto* msg_proto) {
// Allocate and initialize one file def, and add it to the context object.
static void _upb_FileDef_Create(upb_AddDefCtx* ctx,
const google_protobuf_FileDescriptorProto* file_proto) {
upb_FileDef* file = _upb_AddDefCtx_Alloc(ctx, sizeof(upb_FileDef));
upb_FileDef* file = _upb_DefBuilder_Alloc(ctx, sizeof(upb_FileDef));
ctx->file = file;
const google_protobuf_DescriptorProto* const* msgs;
@ -2978,30 +2979,30 @@ static void _upb_FileDef_Create(upb_AddDefCtx* ctx,
/* We are using the ext layouts that were passed in. */
file->ext_layouts = ctx->layout->exts;
if (ctx->layout->ext_count != file->ext_count) {
_upb_AddDefCtx_Errf(ctx,
"Extension count did not match layout (%d vs %d)",
ctx->layout->ext_count, file->ext_count);
_upb_DefBuilder_Errf(ctx,
"Extension count did not match layout (%d vs %d)",
ctx->layout->ext_count, file->ext_count);
}
} else {
/* We are building ext layouts from scratch. */
file->ext_layouts =
_upb_AddDefCtx_Alloc(ctx, sizeof(*file->ext_layouts) * file->ext_count);
file->ext_layouts = _upb_DefBuilder_Alloc(
ctx, sizeof(*file->ext_layouts) * file->ext_count);
upb_MiniTable_Extension* ext =
_upb_AddDefCtx_Alloc(ctx, sizeof(*ext) * file->ext_count);
_upb_DefBuilder_Alloc(ctx, sizeof(*ext) * file->ext_count);
for (int i = 0; i < file->ext_count; i++) {
file->ext_layouts[i] = &ext[i];
}
}
if (!google_protobuf_FileDescriptorProto_has_name(file_proto)) {
_upb_AddDefCtx_Errf(ctx, "File has no name");
_upb_DefBuilder_Errf(ctx, "File has no name");
}
file->name = strviewdup(ctx, google_protobuf_FileDescriptorProto_name(file_proto));
upb_StringView package = google_protobuf_FileDescriptorProto_package(file_proto);
if (package.size) {
_upb_AddDefCtx_CheckIdent(ctx, package, true);
_upb_DefBuilder_CheckIdent(ctx, package, true);
file->package = strviewdup(ctx, package);
} else {
file->package = NULL;
@ -3015,8 +3016,8 @@ static void _upb_FileDef_Create(upb_AddDefCtx* ctx,
} else if (streql_view(syntax, "proto3")) {
file->syntax = kUpb_Syntax_Proto3;
} else {
_upb_AddDefCtx_Errf(ctx, "Invalid syntax '" UPB_STRINGVIEW_FORMAT "'",
UPB_STRINGVIEW_ARGS(syntax));
_upb_DefBuilder_Errf(ctx, "Invalid syntax '" UPB_STRINGVIEW_FORMAT "'",
UPB_STRINGVIEW_ARGS(syntax));
}
} else {
file->syntax = kUpb_Syntax_Proto2;
@ -3028,40 +3029,41 @@ static void _upb_FileDef_Create(upb_AddDefCtx* ctx,
/* Verify dependencies. */
strs = google_protobuf_FileDescriptorProto_dependency(file_proto, &n);
file->dep_count = n;
file->deps = _upb_AddDefCtx_Alloc(ctx, sizeof(*file->deps) * n);
file->deps = _upb_DefBuilder_Alloc(ctx, sizeof(*file->deps) * n);
for (i = 0; i < n; i++) {
upb_StringView str = strs[i];
file->deps[i] =
upb_DefPool_FindFileByNameWithSize(ctx->symtab, str.data, str.size);
if (!file->deps[i]) {
_upb_AddDefCtx_Errf(ctx,
"Depends on file '" UPB_STRINGVIEW_FORMAT
"', but it has not been loaded",
UPB_STRINGVIEW_ARGS(str));
_upb_DefBuilder_Errf(ctx,
"Depends on file '" UPB_STRINGVIEW_FORMAT
"', but it has not been loaded",
UPB_STRINGVIEW_ARGS(str));
}
}
public_deps = google_protobuf_FileDescriptorProto_public_dependency(file_proto, &n);
file->public_dep_count = n;
file->public_deps = _upb_AddDefCtx_Alloc(ctx, sizeof(*file->public_deps) * n);
file->public_deps =
_upb_DefBuilder_Alloc(ctx, sizeof(*file->public_deps) * n);
int32_t* mutable_public_deps = (int32_t*)file->public_deps;
for (i = 0; i < n; i++) {
if (public_deps[i] >= file->dep_count) {
_upb_AddDefCtx_Errf(ctx, "public_dep %d is out of range",
(int)public_deps[i]);
_upb_DefBuilder_Errf(ctx, "public_dep %d is out of range",
(int)public_deps[i]);
}
mutable_public_deps[i] = public_deps[i];
}
weak_deps = google_protobuf_FileDescriptorProto_weak_dependency(file_proto, &n);
file->weak_dep_count = n;
file->weak_deps = _upb_AddDefCtx_Alloc(ctx, sizeof(*file->weak_deps) * n);
file->weak_deps = _upb_DefBuilder_Alloc(ctx, sizeof(*file->weak_deps) * n);
int32_t* mutable_weak_deps = (int32_t*)file->weak_deps;
for (i = 0; i < n; i++) {
if (weak_deps[i] >= file->dep_count) {
_upb_AddDefCtx_Errf(ctx, "weak_dep %d is out of range",
(int)weak_deps[i]);
_upb_DefBuilder_Errf(ctx, "weak_dep %d is out of range",
(int)weak_deps[i]);
}
mutable_weak_deps[i] = weak_deps[i];
}
@ -3171,7 +3173,7 @@ static const upb_FileDef* _upb_DefPool_AddFile(
ctx.file = NULL;
}
} else if (!ctx.arena || !ctx.tmp_arena) {
_upb_AddDefCtx_OomErr(&ctx);
_upb_DefBuilder_OomErr(&ctx);
} else {
_upb_FileDef_Create(&ctx, file_proto);
upb_strtable_insert(&s->files, name.data, name.size,

Loading…
Cancel
Save