upb: attempt to reduce the direct use of UPB_PRIVATE() fields

PiperOrigin-RevId: 602270576
pull/15616/head
Eric Salo 1 year ago committed by Copybara-Service
parent 1d6ac5979b
commit 98843d15ac
  1. 9
      upb/message/accessors.c
  2. 6
      upb/message/array.c
  3. 17
      upb/message/copy.c
  4. 5
      upb/message/promote.c
  5. 26
      upb/mini_descriptor/link.c
  6. 4
      upb/reflection/message_def.c
  7. 40
      upb/wire/encode.c

@ -20,20 +20,19 @@
// Must be last.
#include "upb/port/def.inc"
bool upb_Message_SetMapEntry(upb_Map* map, const upb_MiniTable* mini_table,
bool upb_Message_SetMapEntry(upb_Map* map, const upb_MiniTable* m,
const upb_MiniTableField* f,
upb_Message* map_entry_message, upb_Arena* arena) {
UPB_ASSERT(!upb_Message_IsFrozen(map_entry_message));
// TODO: use a variant of upb_MiniTable_GetSubMessageTable() here.
const upb_MiniTable* map_entry_mini_table = upb_MiniTableSub_Message(
mini_table->UPB_PRIVATE(subs)[f->UPB_PRIVATE(submsg_index)]);
m->UPB_PRIVATE(subs)[f->UPB_PRIVATE(submsg_index)]);
UPB_ASSERT(map_entry_mini_table);
UPB_ASSERT(map_entry_mini_table->UPB_PRIVATE(field_count) == 2);
const upb_MiniTableField* map_entry_key_field =
&map_entry_mini_table->UPB_PRIVATE(fields)[0];
upb_MiniTable_MapKey(map_entry_mini_table);
const upb_MiniTableField* map_entry_value_field =
&map_entry_mini_table->UPB_PRIVATE(fields)[1];
upb_MiniTable_MapValue(map_entry_mini_table);
// Map key/value cannot have explicit defaults,
// hence assuming a zero default is valid.
upb_MessageValue default_val;

@ -27,28 +27,28 @@ upb_Array* upb_Array_New(upb_Arena* a, upb_CType type) {
}
upb_MessageValue upb_Array_Get(const upb_Array* arr, size_t i) {
UPB_ASSERT(i < upb_Array_Size(arr));
upb_MessageValue ret;
const char* data = upb_Array_DataPtr(arr);
const int lg2 = UPB_PRIVATE(_upb_Array_ElemSizeLg2)(arr);
UPB_ASSERT(i < arr->UPB_PRIVATE(size));
memcpy(&ret, data + (i << lg2), 1 << lg2);
return ret;
}
upb_MutableMessageValue upb_Array_GetMutable(upb_Array* arr, size_t i) {
UPB_ASSERT(i < upb_Array_Size(arr));
upb_MutableMessageValue ret;
char* data = upb_Array_MutableDataPtr(arr);
const int lg2 = UPB_PRIVATE(_upb_Array_ElemSizeLg2)(arr);
UPB_ASSERT(i < arr->UPB_PRIVATE(size));
memcpy(&ret, data + (i << lg2), 1 << lg2);
return ret;
}
void upb_Array_Set(upb_Array* arr, size_t i, upb_MessageValue val) {
UPB_ASSERT(!upb_Array_IsFrozen(arr));
UPB_ASSERT(i < upb_Array_Size(arr));
char* data = upb_Array_MutableDataPtr(arr);
const int lg2 = UPB_PRIVATE(_upb_Array_ElemSizeLg2)(arr);
UPB_ASSERT(i < arr->UPB_PRIVATE(size));
memcpy(data + (i << lg2), &val, 1 << lg2);
}

@ -96,7 +96,7 @@ upb_Map* upb_Map_DeepClone(const upb_Map* map, upb_CType key_type,
size_t iter = kUpb_Map_Begin;
while (upb_Map_Next(map, &key, &val, &iter)) {
const upb_MiniTableField* value_field =
&map_entry_table->UPB_PRIVATE(fields)[1];
upb_MiniTable_MapValue(map_entry_table);
const upb_MiniTable* value_sub =
upb_MiniTableField_CType(value_field) == kUpb_CType_Message
? upb_MiniTable_GetSubMessageTable(map_entry_table, value_field)
@ -122,10 +122,9 @@ static upb_Map* upb_Message_Map_DeepClone(const upb_Map* map,
mini_table->UPB_PRIVATE(subs)[f->UPB_PRIVATE(submsg_index)]);
UPB_ASSERT(map_entry_table);
const upb_MiniTableField* key_field =
&map_entry_table->UPB_PRIVATE(fields)[0];
const upb_MiniTableField* key_field = upb_MiniTable_MapKey(map_entry_table);
const upb_MiniTableField* value_field =
&map_entry_table->UPB_PRIVATE(fields)[1];
upb_MiniTable_MapValue(map_entry_table);
upb_Map* cloned_map = upb_Map_DeepClone(
map, upb_MiniTableField_CType(key_field),
@ -139,7 +138,7 @@ static upb_Map* upb_Message_Map_DeepClone(const upb_Map* map,
upb_Array* upb_Array_DeepClone(const upb_Array* array, upb_CType value_type,
const upb_MiniTable* sub, upb_Arena* arena) {
const size_t size = array->UPB_PRIVATE(size);
const size_t size = upb_Array_Size(array);
const int lg2 = UPB_PRIVATE(_upb_CType_SizeLg2)(value_type);
upb_Array* cloned_array = UPB_PRIVATE(_upb_Array_New)(arena, size, lg2);
if (!cloned_array) {
@ -180,8 +179,7 @@ static bool upb_Clone_ExtensionValue(
upb_Extension* dest, upb_Arena* arena) {
dest->data = source->data;
return upb_Clone_MessageValue(
&dest->data,
upb_MiniTableField_CType(&mini_table_ext->UPB_PRIVATE(field)),
&dest->data, upb_MiniTableExtension_CType(mini_table_ext),
upb_MiniTableExtension_GetSubMessage(mini_table_ext), arena);
}
@ -191,8 +189,9 @@ upb_Message* _upb_Message_Copy(upb_Message* dst, const upb_Message* src,
upb_StringView empty_string = upb_StringView_FromDataAndSize(NULL, 0);
// Only copy message area skipping upb_Message_Internal.
memcpy(dst + 1, src + 1, mini_table->UPB_PRIVATE(size) - sizeof(upb_Message));
for (size_t i = 0; i < mini_table->UPB_PRIVATE(field_count); ++i) {
const upb_MiniTableField* field = &mini_table->UPB_PRIVATE(fields)[i];
for (int i = 0; i < upb_MiniTable_FieldCount(mini_table); ++i) {
const upb_MiniTableField* field =
upb_MiniTable_GetFieldByIndex(mini_table, i);
if (upb_MiniTableField_IsScalar(field)) {
switch (upb_MiniTableField_CType(field)) {
case kUpb_CType_Message: {

@ -196,7 +196,7 @@ upb_DecodeStatus upb_Array_PromoteMessages(upb_Array* arr,
int decode_options,
upb_Arena* arena) {
void** data = upb_Array_MutableDataPtr(arr);
size_t size = arr->UPB_PRIVATE(size);
size_t size = upb_Array_Size(arr);
for (size_t i = 0; i < size; i++) {
upb_TaggedMessagePtr tagged;
memcpy(&tagged, &data[i], sizeof(tagged));
@ -331,8 +331,7 @@ upb_UnknownToMessage_Status upb_MiniTable_PromoteUnknownToMap(
const upb_MiniTable* map_entry_mini_table = upb_MiniTableSub_Message(
mini_table->UPB_PRIVATE(subs)[field->UPB_PRIVATE(submsg_index)]);
UPB_ASSERT(map_entry_mini_table);
UPB_ASSERT(map_entry_mini_table);
UPB_ASSERT(map_entry_mini_table->UPB_PRIVATE(field_count) == 2);
UPB_ASSERT(upb_MiniTable_FieldCount(map_entry_mini_table) == 2);
UPB_ASSERT(upb_MiniTableField_IsMap(field));
// Find all unknowns with given field number and parse.
upb_FindUnknownRet unknown;

@ -72,13 +72,13 @@ bool upb_MiniTable_SetSubEnum(upb_MiniTable* table, upb_MiniTableField* field,
return true;
}
uint32_t upb_MiniTable_GetSubList(const upb_MiniTable* mt,
uint32_t upb_MiniTable_GetSubList(const upb_MiniTable* m,
const upb_MiniTableField** subs) {
uint32_t msg_count = 0;
uint32_t enum_count = 0;
for (int i = 0; i < mt->UPB_PRIVATE(field_count); i++) {
const upb_MiniTableField* f = &mt->UPB_PRIVATE(fields)[i];
for (int i = 0; i < upb_MiniTable_FieldCount(m); i++) {
const upb_MiniTableField* f = upb_MiniTable_GetFieldByIndex(m, i);
if (upb_MiniTableField_CType(f) == kUpb_CType_Message) {
*subs = f;
++subs;
@ -86,8 +86,8 @@ uint32_t upb_MiniTable_GetSubList(const upb_MiniTable* mt,
}
}
for (int i = 0; i < mt->UPB_PRIVATE(field_count); i++) {
const upb_MiniTableField* f = &mt->UPB_PRIVATE(fields)[i];
for (int i = 0; i < upb_MiniTable_FieldCount(m); i++) {
const upb_MiniTableField* f = upb_MiniTable_GetFieldByIndex(m, i);
if (upb_MiniTableField_CType(f) == kUpb_CType_Enum) {
*subs = f;
++subs;
@ -101,31 +101,33 @@ uint32_t upb_MiniTable_GetSubList(const upb_MiniTable* mt,
// The list of sub_tables and sub_enums must exactly match the number and order
// of sub-message fields and sub-enum fields given by upb_MiniTable_GetSubList()
// above.
bool upb_MiniTable_Link(upb_MiniTable* mt, const upb_MiniTable** sub_tables,
bool upb_MiniTable_Link(upb_MiniTable* m, const upb_MiniTable** sub_tables,
size_t sub_table_count,
const upb_MiniTableEnum** sub_enums,
size_t sub_enum_count) {
uint32_t msg_count = 0;
uint32_t enum_count = 0;
for (int i = 0; i < mt->UPB_PRIVATE(field_count); i++) {
upb_MiniTableField* f = (upb_MiniTableField*)&mt->UPB_PRIVATE(fields)[i];
for (int i = 0; i < upb_MiniTable_FieldCount(m); i++) {
upb_MiniTableField* f =
(upb_MiniTableField*)upb_MiniTable_GetFieldByIndex(m, i);
if (upb_MiniTableField_CType(f) == kUpb_CType_Message) {
const upb_MiniTable* sub = sub_tables[msg_count++];
if (msg_count > sub_table_count) return false;
if (sub != NULL) {
if (!upb_MiniTable_SetSubMessage(mt, f, sub)) return false;
if (!upb_MiniTable_SetSubMessage(m, f, sub)) return false;
}
}
}
for (int i = 0; i < mt->UPB_PRIVATE(field_count); i++) {
upb_MiniTableField* f = (upb_MiniTableField*)&mt->UPB_PRIVATE(fields)[i];
for (int i = 0; i < upb_MiniTable_FieldCount(m); i++) {
upb_MiniTableField* f =
(upb_MiniTableField*)upb_MiniTable_GetFieldByIndex(m, i);
if (upb_MiniTableField_IsClosedEnum(f)) {
const upb_MiniTableEnum* sub = sub_enums[enum_count++];
if (enum_count > sub_enum_count) return false;
if (sub != NULL) {
if (!upb_MiniTable_SetSubEnum(mt, f, sub)) return false;
if (!upb_MiniTable_SetSubEnum(m, f, sub)) return false;
}
}
}

@ -446,7 +446,7 @@ void _upb_MessageDef_CreateMiniTable(upb_DefBuilder* ctx, upb_MessageDef* m) {
m->layout = _upb_MessageDef_MakeMiniTable(ctx, m);
} else {
m->layout = upb_MiniTableFile_Message(ctx->layout, ctx->msg_count++);
UPB_ASSERT(m->field_count == m->layout->UPB_PRIVATE(field_count));
UPB_ASSERT(m->field_count == upb_MiniTable_FieldCount(m->layout));
// We don't need the result of this call, but it will assign layout_index
// for all the fields in O(n lg n) time.
@ -504,7 +504,7 @@ void _upb_MessageDef_LinkMiniTable(upb_DefBuilder* ctx,
for (int i = 0; i < m->field_count; i++) {
const upb_FieldDef* f = upb_MessageDef_Field(m, i);
const int layout_index = _upb_FieldDef_LayoutIndex(f);
UPB_ASSERT(layout_index < m->layout->UPB_PRIVATE(field_count));
UPB_ASSERT(layout_index < upb_MiniTable_FieldCount(m->layout));
const upb_MiniTableField* mt_f =
&m->layout->UPB_PRIVATE(fields)[layout_index];
UPB_ASSERT(upb_FieldDef_Type(f) == upb_MiniTableField_Type(mt_f));

@ -182,7 +182,7 @@ static void encode_tag(upb_encstate* e, uint32_t field_number,
static void encode_fixedarray(upb_encstate* e, const upb_Array* arr,
size_t elem_size, uint32_t tag) {
size_t bytes = arr->UPB_PRIVATE(size) * elem_size;
size_t bytes = upb_Array_Size(arr) * elem_size;
const char* data = upb_Array_DataPtr(arr);
const char* ptr = data + bytes - elem_size;
@ -279,7 +279,7 @@ static void encode_scalar(upb_encstate* e, const void* _field_mem,
return;
}
if (--e->depth == 0) encode_err(e, kUpb_EncodeStatus_MaxDepthExceeded);
encode_tag(e, f->UPB_PRIVATE(number), kUpb_WireType_EndGroup);
encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_EndGroup);
encode_TaggedMessagePtr(e, submsg, subm, &size);
wire_type = kUpb_WireType_StartGroup;
e->depth++;
@ -305,7 +305,7 @@ static void encode_scalar(upb_encstate* e, const void* _field_mem,
}
#undef CASE
encode_tag(e, f->UPB_PRIVATE(number), wire_type);
encode_tag(e, upb_MiniTableField_Number(f), wire_type);
}
static void encode_array(upb_encstate* e, const upb_Message* msg,
@ -315,14 +315,14 @@ static void encode_array(upb_encstate* e, const upb_Message* msg,
bool packed = upb_MiniTableField_IsPacked(f);
size_t pre_len = e->limit - e->ptr;
if (arr == NULL || arr->UPB_PRIVATE(size) == 0) {
if (arr == NULL || upb_Array_Size(arr) == 0) {
return;
}
#define VARINT_CASE(ctype, encode) \
{ \
const ctype* start = upb_Array_DataPtr(arr); \
const ctype* ptr = start + arr->UPB_PRIVATE(size); \
const ctype* ptr = start + upb_Array_Size(arr); \
uint32_t tag = \
packed ? 0 : (f->UPB_PRIVATE(number) << 3) | kUpb_WireType_Varint; \
do { \
@ -367,34 +367,34 @@ static void encode_array(upb_encstate* e, const upb_Message* msg,
case kUpb_FieldType_String:
case kUpb_FieldType_Bytes: {
const upb_StringView* start = upb_Array_DataPtr(arr);
const upb_StringView* ptr = start + arr->UPB_PRIVATE(size);
const upb_StringView* ptr = start + upb_Array_Size(arr);
do {
ptr--;
encode_bytes(e, ptr->data, ptr->size);
encode_varint(e, ptr->size);
encode_tag(e, f->UPB_PRIVATE(number), kUpb_WireType_Delimited);
encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_Delimited);
} while (ptr != start);
return;
}
case kUpb_FieldType_Group: {
const upb_TaggedMessagePtr* start = upb_Array_DataPtr(arr);
const upb_TaggedMessagePtr* ptr = start + arr->UPB_PRIVATE(size);
const upb_TaggedMessagePtr* ptr = start + upb_Array_Size(arr);
const upb_MiniTable* subm =
upb_MiniTableSub_Message(subs[f->UPB_PRIVATE(submsg_index)]);
if (--e->depth == 0) encode_err(e, kUpb_EncodeStatus_MaxDepthExceeded);
do {
size_t size;
ptr--;
encode_tag(e, f->UPB_PRIVATE(number), kUpb_WireType_EndGroup);
encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_EndGroup);
encode_TaggedMessagePtr(e, *ptr, subm, &size);
encode_tag(e, f->UPB_PRIVATE(number), kUpb_WireType_StartGroup);
encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_StartGroup);
} while (ptr != start);
e->depth++;
return;
}
case kUpb_FieldType_Message: {
const upb_TaggedMessagePtr* start = upb_Array_DataPtr(arr);
const upb_TaggedMessagePtr* ptr = start + arr->UPB_PRIVATE(size);
const upb_TaggedMessagePtr* ptr = start + upb_Array_Size(arr);
const upb_MiniTable* subm =
upb_MiniTableSub_Message(subs[f->UPB_PRIVATE(submsg_index)]);
if (--e->depth == 0) encode_err(e, kUpb_EncodeStatus_MaxDepthExceeded);
@ -403,7 +403,7 @@ static void encode_array(upb_encstate* e, const upb_Message* msg,
ptr--;
encode_TaggedMessagePtr(e, *ptr, subm, &size);
encode_varint(e, size);
encode_tag(e, f->UPB_PRIVATE(number), kUpb_WireType_Delimited);
encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_Delimited);
} while (ptr != start);
e->depth++;
return;
@ -413,15 +413,15 @@ static void encode_array(upb_encstate* e, const upb_Message* msg,
if (packed) {
encode_varint(e, e->limit - e->ptr - pre_len);
encode_tag(e, f->UPB_PRIVATE(number), kUpb_WireType_Delimited);
encode_tag(e, upb_MiniTableField_Number(f), kUpb_WireType_Delimited);
}
}
static void encode_mapentry(upb_encstate* e, uint32_t number,
const upb_MiniTable* layout,
const upb_MapEntry* ent) {
const upb_MiniTableField* key_field = &layout->UPB_PRIVATE(fields)[0];
const upb_MiniTableField* val_field = &layout->UPB_PRIVATE(fields)[1];
const upb_MiniTableField* key_field = upb_MiniTable_MapKey(layout);
const upb_MiniTableField* val_field = upb_MiniTable_MapValue(layout);
size_t pre_len = e->limit - e->ptr;
size_t size;
encode_scalar(e, &ent->v, layout->UPB_PRIVATE(subs), val_field);
@ -437,7 +437,7 @@ static void encode_map(upb_encstate* e, const upb_Message* msg,
const upb_Map* map = *UPB_PTR_AT(msg, f->UPB_PRIVATE(offset), const upb_Map*);
const upb_MiniTable* layout =
upb_MiniTableSub_Message(subs[f->UPB_PRIVATE(submsg_index)]);
UPB_ASSERT(layout->UPB_PRIVATE(field_count) == 2);
UPB_ASSERT(upb_MiniTable_FieldCount(layout) == 2);
if (!map || !upb_Map_Size(map)) return;
@ -448,7 +448,7 @@ static void encode_map(upb_encstate* e, const upb_Message* msg,
map, &sorted);
upb_MapEntry ent;
while (_upb_sortedmap_next(&e->sorter, map, &sorted, &ent)) {
encode_mapentry(e, f->UPB_PRIVATE(number), layout, &ent);
encode_mapentry(e, upb_MiniTableField_Number(f), layout, &ent);
}
_upb_mapsorter_popmap(&e->sorter, &sorted);
} else {
@ -459,7 +459,7 @@ static void encode_map(upb_encstate* e, const upb_Message* msg,
upb_MapEntry ent;
_upb_map_fromkey(key, &ent.k, map->key_size);
_upb_map_fromvalue(val, &ent.v, map->val_size);
encode_mapentry(e, f->UPB_PRIVATE(number), layout, &ent);
encode_mapentry(e, upb_MiniTableField_Number(f), layout, &ent);
}
}
}
@ -499,7 +499,7 @@ static bool encode_shouldencode(upb_encstate* e, const upb_Message* msg,
} else {
// Field is in a oneof.
return UPB_PRIVATE(_upb_Message_GetOneofCase)(msg, f) ==
f->UPB_PRIVATE(number);
upb_MiniTableField_Number(f);
}
}
@ -589,7 +589,7 @@ static void encode_message(upb_encstate* e, const upb_Message* msg,
}
}
if (m->UPB_PRIVATE(field_count)) {
if (upb_MiniTable_FieldCount(m)) {
const upb_MiniTableField* f =
&m->UPB_PRIVATE(fields)[m->UPB_PRIVATE(field_count)];
const upb_MiniTableField* first = &m->UPB_PRIVATE(fields)[0];

Loading…
Cancel
Save