upb: upb_Message_Extension -> upb_Extension

PiperOrigin-RevId: 592075816
pull/15104/head
Eric Salo 1 year ago committed by Copybara-Service
parent 7315f6d398
commit 5b7bafc60e
  1. 11
      protos/protos.cc
  2. 7
      protos/protos.h
  3. 6
      upb/message/compat.c
  4. 4
      upb/message/compat.h
  5. 11
      upb/message/copy.c
  6. 14
      upb/message/internal/accessors.h
  7. 26
      upb/message/internal/extension.c
  8. 12
      upb/message/internal/extension.h
  9. 9
      upb/message/internal/map_sorter.h
  10. 2
      upb/message/internal/message.h
  11. 10
      upb/message/map_sorter.c
  12. 2
      upb/message/message.h
  13. 6
      upb/message/promote.c
  14. 3
      upb/message/promote.h
  15. 2
      upb/message/promote_test.cc
  16. 2
      upb/reflection/message.c
  17. 4
      upb/wire/decode.c
  18. 9
      upb/wire/encode.c

@ -119,10 +119,11 @@ bool HasExtensionOrUnknown(const upb_Message* msg,
.status == kUpb_FindUnknown_Ok;
}
const upb_Message_Extension* GetOrPromoteExtension(
upb_Message* msg, const upb_MiniTableExtension* eid, upb_Arena* arena) {
const upb_Extension* GetOrPromoteExtension(upb_Message* msg,
const upb_MiniTableExtension* eid,
upb_Arena* arena) {
MessageLock msg_lock(msg);
const upb_Message_Extension* ext = _upb_Message_Getext(msg, eid);
const upb_Extension* ext = _upb_Message_Getext(msg, eid);
if (ext == nullptr) {
upb_GetExtension_Status ext_status = upb_MiniTable_GetOrPromoteExtension(
(upb_Message*)msg, eid, 0, arena, &ext);
@ -162,7 +163,7 @@ upb_Message* DeepClone(const upb_Message* source,
absl::Status MoveExtension(upb_Message* message, upb_Arena* message_arena,
const upb_MiniTableExtension* ext,
upb_Message* extension, upb_Arena* extension_arena) {
upb_Message_Extension* msg_ext =
upb_Extension* msg_ext =
_upb_Message_GetOrCreateExtension(message, ext, message_arena);
if (!msg_ext) {
return MessageAllocationError();
@ -183,7 +184,7 @@ absl::Status MoveExtension(upb_Message* message, upb_Arena* message_arena,
absl::Status SetExtension(upb_Message* message, upb_Arena* message_arena,
const upb_MiniTableExtension* ext,
const upb_Message* extension) {
upb_Message_Extension* msg_ext =
upb_Extension* msg_ext =
_upb_Message_GetOrCreateExtension(message, ext, message_arena);
if (!msg_ext) {
return MessageAllocationError();

@ -223,8 +223,9 @@ absl::StatusOr<absl::string_view> Serialize(const upb_Message* message,
bool HasExtensionOrUnknown(const upb_Message* msg,
const upb_MiniTableExtension* eid);
const upb_Message_Extension* GetOrPromoteExtension(
upb_Message* msg, const upb_MiniTableExtension* eid, upb_Arena* arena);
const upb_Extension* GetOrPromoteExtension(upb_Message* msg,
const upb_MiniTableExtension* eid,
upb_Arena* arena);
void DeepCopy(upb_Message* target, const upb_Message* source,
const upb_MiniTable* mini_table, upb_Arena* arena);
@ -409,7 +410,7 @@ absl::StatusOr<Ptr<const Extension>> GetExtension(
Ptr<T> message,
const ::protos::internal::ExtensionIdentifier<Extendee, Extension>& id) {
// TODO: Fix const correctness issues.
const upb_Message_Extension* ext = ::protos::internal::GetOrPromoteExtension(
const upb_Extension* ext = ::protos::internal::GetOrPromoteExtension(
const_cast<upb_Message*>(internal::GetInternalMsg(message)),
id.mini_table_ext(), ::protos::internal::GetArena(message));
if (!ext) {

@ -17,10 +17,10 @@
// Must be last.
#include "upb/port/def.inc"
const upb_Message_Extension* upb_Message_FindExtensionByNumber(
const upb_Message* msg, uint32_t field_number) {
const upb_Extension* upb_Message_FindExtensionByNumber(const upb_Message* msg,
uint32_t field_number) {
size_t count = 0;
const upb_Message_Extension* ext = _upb_Message_Getexts(msg, &count);
const upb_Extension* ext = _upb_Message_Getexts(msg, &count);
while (count--) {
if (upb_MiniTableExtension_Number(ext->ext) == field_number) return ext;

@ -25,8 +25,8 @@ extern "C" {
#endif
// Returns the extension with the given field number, or NULL on failure.
const upb_Message_Extension* upb_Message_FindExtensionByNumber(
const upb_Message* msg, uint32_t field_number);
const upb_Extension* upb_Message_FindExtensionByNumber(const upb_Message* msg,
uint32_t field_number);
#ifdef __cplusplus
} /* extern "C" */

@ -180,9 +180,8 @@ static bool upb_Message_Array_DeepClone(const upb_Array* array,
}
static bool upb_Clone_ExtensionValue(
const upb_MiniTableExtension* mini_table_ext,
const upb_Message_Extension* source, upb_Message_Extension* dest,
upb_Arena* arena) {
const upb_MiniTableExtension* mini_table_ext, const upb_Extension* source,
upb_Extension* dest, upb_Arena* arena) {
dest->data = source->data;
return upb_Clone_MessageValue(
&dest->data,
@ -258,11 +257,11 @@ upb_Message* _upb_Message_Copy(upb_Message* dst, const upb_Message* src,
}
// Clone extensions.
size_t ext_count;
const upb_Message_Extension* ext = _upb_Message_Getexts(src, &ext_count);
const upb_Extension* ext = _upb_Message_Getexts(src, &ext_count);
for (size_t i = 0; i < ext_count; ++i) {
const upb_Message_Extension* msg_ext = &ext[i];
const upb_Extension* msg_ext = &ext[i];
const upb_MiniTableField* field = &msg_ext->ext->UPB_PRIVATE(field);
upb_Message_Extension* dst_ext =
upb_Extension* dst_ext =
_upb_Message_GetOrCreateExtension(dst, msg_ext->ext, arena);
if (!dst_ext) return NULL;
if (upb_MiniTableField_IsScalar(field)) {

@ -229,7 +229,7 @@ static UPB_FORCEINLINE void _upb_Message_GetNonExtensionField(
UPB_INLINE void _upb_Message_GetExtensionField(
const upb_Message* msg, const upb_MiniTableExtension* mt_ext,
const void* default_val, void* val) {
const upb_Message_Extension* ext = _upb_Message_Getext(msg, mt_ext);
const upb_Extension* ext = _upb_Message_Getext(msg, mt_ext);
const upb_MiniTableField* f = &mt_ext->UPB_PRIVATE(field);
UPB_ASSUME(upb_MiniTableField_IsExtension(f));
@ -279,8 +279,7 @@ UPB_INLINE bool _upb_Message_SetExtensionField(
upb_Message* msg, const upb_MiniTableExtension* mt_ext, const void* val,
upb_Arena* a) {
UPB_ASSERT(a);
upb_Message_Extension* ext =
_upb_Message_GetOrCreateExtension(msg, mt_ext, a);
upb_Extension* ext = _upb_Message_GetOrCreateExtension(msg, mt_ext, a);
if (!ext) return false;
UPB_PRIVATE(_upb_MiniTableField_DataCopy)
(&mt_ext->UPB_PRIVATE(field), &ext->data, val);
@ -291,13 +290,12 @@ UPB_INLINE void _upb_Message_ClearExtensionField(
upb_Message* msg, const upb_MiniTableExtension* ext_l) {
upb_Message_Internal* in = upb_Message_Getinternal(msg);
if (!in->internal) return;
const upb_Message_Extension* base =
UPB_PTR_AT(in->internal, in->internal->ext_begin, upb_Message_Extension);
upb_Message_Extension* ext =
(upb_Message_Extension*)_upb_Message_Getext(msg, ext_l);
const upb_Extension* base =
UPB_PTR_AT(in->internal, in->internal->ext_begin, upb_Extension);
upb_Extension* ext = (upb_Extension*)_upb_Message_Getext(msg, ext_l);
if (ext) {
*ext = *base;
in->internal->ext_begin += sizeof(upb_Message_Extension);
in->internal->ext_begin += sizeof(upb_Extension);
}
}

@ -18,10 +18,10 @@
// Must be last.
#include "upb/port/def.inc"
const upb_Message_Extension* _upb_Message_Getext(
const upb_Message* msg, const upb_MiniTableExtension* e) {
const upb_Extension* _upb_Message_Getext(const upb_Message* msg,
const upb_MiniTableExtension* e) {
size_t n;
const upb_Message_Extension* ext = _upb_Message_Getexts(msg, &n);
const upb_Extension* ext = _upb_Message_Getexts(msg, &n);
// For now we use linear search exclusively to find extensions.
// If this becomes an issue due to messages with lots of extensions,
@ -35,12 +35,12 @@ const upb_Message_Extension* _upb_Message_Getext(
return NULL;
}
const upb_Message_Extension* _upb_Message_Getexts(const upb_Message* msg,
size_t* count) {
const upb_Extension* _upb_Message_Getexts(const upb_Message* msg,
size_t* count) {
const upb_Message_Internal* in = upb_Message_Getinternal(msg);
if (in->internal) {
*count = (in->internal->size - in->internal->ext_begin) /
sizeof(upb_Message_Extension);
*count =
(in->internal->size - in->internal->ext_begin) / sizeof(upb_Extension);
return UPB_PTR_AT(in->internal, in->internal->ext_begin, void);
} else {
*count = 0;
@ -48,16 +48,16 @@ const upb_Message_Extension* _upb_Message_Getexts(const upb_Message* msg,
}
}
upb_Message_Extension* _upb_Message_GetOrCreateExtension(
upb_Extension* _upb_Message_GetOrCreateExtension(
upb_Message* msg, const upb_MiniTableExtension* e, upb_Arena* arena) {
upb_Message_Extension* ext =
(upb_Message_Extension*)_upb_Message_Getext(msg, e);
upb_Extension* ext = (upb_Extension*)_upb_Message_Getext(msg, e);
if (ext) return ext;
if (!UPB_PRIVATE(_upb_Message_Realloc)(msg, sizeof(upb_Message_Extension), arena)) return NULL;
if (!UPB_PRIVATE(_upb_Message_Realloc)(msg, sizeof(upb_Extension), arena))
return NULL;
upb_Message_Internal* in = upb_Message_Getinternal(msg);
in->internal->ext_begin -= sizeof(upb_Message_Extension);
in->internal->ext_begin -= sizeof(upb_Extension);
ext = UPB_PTR_AT(in->internal, in->internal->ext_begin, void);
memset(ext, 0, sizeof(upb_Message_Extension));
memset(ext, 0, sizeof(upb_Extension));
ext->ext = e;
return ext;
}

@ -24,7 +24,7 @@
// This is rather wasteful for scalars (in the extreme case of bool,
// it wastes 15 bytes). We accept this because we expect messages to be
// the most common extension type.
struct upb_Message_Extension {
struct upb_Extension {
const upb_MiniTableExtension* ext;
union {
upb_StringView str;
@ -40,18 +40,18 @@ extern "C" {
// Adds the given extension data to the given message.
// |ext| is copied into the message instance.
// This logically replaces any previously-added extension with this number.
upb_Message_Extension* _upb_Message_GetOrCreateExtension(
upb_Extension* _upb_Message_GetOrCreateExtension(
upb_Message* msg, const upb_MiniTableExtension* ext, upb_Arena* arena);
// Returns an array of extensions for this message.
// Note: the array is ordered in reverse relative to the order of creation.
const upb_Message_Extension* _upb_Message_Getexts(const upb_Message* msg,
size_t* count);
const upb_Extension* _upb_Message_Getexts(const upb_Message* msg,
size_t* count);
// Returns an extension for the given field number, or NULL if no extension
// exists for this field number.
const upb_Message_Extension* _upb_Message_Getext(
const upb_Message* msg, const upb_MiniTableExtension* ext);
const upb_Extension* _upb_Message_Getext(const upb_Message* msg,
const upb_MiniTableExtension* ext);
#ifdef __cplusplus
} /* extern "C" */

@ -63,9 +63,9 @@ UPB_INLINE bool _upb_sortedmap_next(_upb_mapsorter* s, const upb_Map* map,
UPB_INLINE bool _upb_sortedmap_nextext(_upb_mapsorter* s,
_upb_sortedmap* sorted,
const upb_Message_Extension** ext) {
const upb_Extension** ext) {
if (sorted->pos == sorted->end) return false;
*ext = (const upb_Message_Extension*)s->entries[sorted->pos++];
*ext = (const upb_Extension*)s->entries[sorted->pos++];
return true;
}
@ -77,9 +77,8 @@ UPB_INLINE void _upb_mapsorter_popmap(_upb_mapsorter* s,
bool _upb_mapsorter_pushmap(_upb_mapsorter* s, upb_FieldType key_type,
const upb_Map* map, _upb_sortedmap* sorted);
bool _upb_mapsorter_pushexts(_upb_mapsorter* s,
const upb_Message_Extension* exts, size_t count,
_upb_sortedmap* sorted);
bool _upb_mapsorter_pushexts(_upb_mapsorter* s, const upb_Extension* exts,
size_t count, _upb_sortedmap* sorted);
#ifdef __cplusplus
} /* extern "C" */

@ -41,7 +41,7 @@ extern const double kUpb_NaN;
struct upb_Message_InternalData {
/* Total size of this structure, including the data that follows.
* Must be aligned to 8, which is alignof(upb_Message_Extension) */
* Must be aligned to 8, which is alignof(upb_Extension) */
uint32_t size;
/* Offsets relative to the beginning of this structure.

@ -15,6 +15,7 @@
#include "upb/base/string_view.h"
#include "upb/mem/alloc.h"
#include "upb/message/map.h"
#include "upb/message/message.h"
#include "upb/mini_table/extension.h"
// Must be last.
@ -134,17 +135,16 @@ bool _upb_mapsorter_pushmap(_upb_mapsorter* s, upb_FieldType key_type,
}
static int _upb_mapsorter_cmpext(const void* _a, const void* _b) {
const upb_Message_Extension* const* a = _a;
const upb_Message_Extension* const* b = _b;
const upb_Extension* const* a = _a;
const upb_Extension* const* b = _b;
uint32_t a_num = upb_MiniTableExtension_Number((*a)->ext);
uint32_t b_num = upb_MiniTableExtension_Number((*b)->ext);
assert(a_num != b_num);
return a_num < b_num ? -1 : 1;
}
bool _upb_mapsorter_pushexts(_upb_mapsorter* s,
const upb_Message_Extension* exts, size_t count,
_upb_sortedmap* sorted) {
bool _upb_mapsorter_pushexts(_upb_mapsorter* s, const upb_Extension* exts,
size_t count, _upb_sortedmap* sorted) {
if (!_upb_mapsorter_resize(s, sorted, count)) return false;
for (size_t i = 0; i < count; i++) {

@ -21,7 +21,7 @@
// Must be last.
#include "upb/port/def.inc"
typedef struct upb_Message_Extension upb_Message_Extension;
typedef struct upb_Extension upb_Extension;
#ifdef __cplusplus
extern "C" {

@ -67,8 +67,7 @@ static upb_UnknownToMessageRet upb_MiniTable_ParseUnknownMessage(
upb_GetExtension_Status upb_MiniTable_GetOrPromoteExtension(
upb_Message* msg, const upb_MiniTableExtension* ext_table,
int decode_options, upb_Arena* arena,
const upb_Message_Extension** extension) {
int decode_options, upb_Arena* arena, const upb_Extension** extension) {
UPB_ASSERT(upb_MiniTableField_CType(upb_MiniTableExtension_AsField(
ext_table)) == kUpb_CType_Message);
*extension = _upb_Message_Getext(msg, ext_table);
@ -102,8 +101,7 @@ upb_GetExtension_Status upb_MiniTable_GetOrPromoteExtension(
}
upb_Message* extension_msg = parse_result.message;
// Add to extensions.
upb_Message_Extension* ext =
_upb_Message_GetOrCreateExtension(msg, ext_table, arena);
upb_Extension* ext = _upb_Message_GetOrCreateExtension(msg, ext_table, arena);
if (!ext) {
return kUpb_GetExtension_OutOfMemory;
}

@ -40,8 +40,7 @@ typedef enum {
// expand support to include non-message types.
upb_GetExtension_Status upb_MiniTable_GetOrPromoteExtension(
upb_Message* msg, const upb_MiniTableExtension* ext_table,
int decode_options, upb_Arena* arena,
const upb_Message_Extension** extension);
int decode_options, upb_Arena* arena, const upb_Extension** extension);
typedef enum {
kUpb_FindUnknown_Ok,

@ -122,7 +122,7 @@ TEST(GeneratedCode, Extensions) {
char* serialized =
upb_test_ModelWithExtensions_serialize(msg, arena, &serialized_size);
const upb_Message_Extension* upb_ext2;
const upb_Extension* upb_ext2;
upb_test_ModelExtension1* ext1;
upb_test_ModelExtension2* ext2;
upb_GetExtension_Status promote_status;

@ -146,7 +146,7 @@ bool upb_Message_Next(const upb_Message* msg, const upb_MessageDef* m,
if (ext_pool) {
// Return any extensions that are set.
size_t count;
const upb_Message_Extension* ext = _upb_Message_Getexts(msg, &count);
const upb_Extension* ext = _upb_Message_Getexts(msg, &count);
if (i - n < count) {
ext += count - 1 - (i - n);
memcpy(out_val, &ext->data, sizeof(*out_val));

@ -813,7 +813,7 @@ enum {
static void upb_Decoder_AddKnownMessageSetItem(
upb_Decoder* d, upb_Message* msg, const upb_MiniTableExtension* item_mt,
const char* data, uint32_t size) {
upb_Message_Extension* ext =
upb_Extension* ext =
_upb_Message_GetOrCreateExtension(msg, item_mt, &d->arena);
if (UPB_UNLIKELY(!ext)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
@ -1157,7 +1157,7 @@ static const char* _upb_Decoder_DecodeKnownField(
if (UPB_UNLIKELY(mode & kUpb_LabelFlags_IsExtension)) {
const upb_MiniTableExtension* ext_layout =
(const upb_MiniTableExtension*)field;
upb_Message_Extension* ext =
upb_Extension* ext =
_upb_Message_GetOrCreateExtension(msg, ext_layout, &d->arena);
if (UPB_UNLIKELY(!ext)) {
_upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);

@ -519,8 +519,7 @@ static void encode_field(upb_encstate* e, const upb_Message* msg,
}
}
static void encode_msgset_item(upb_encstate* e,
const upb_Message_Extension* ext) {
static void encode_msgset_item(upb_encstate* e, const upb_Extension* ext) {
size_t size;
encode_tag(e, kUpb_MsgSet_Item, kUpb_WireType_EndGroup);
encode_message(e, ext->data.ptr,
@ -532,7 +531,7 @@ static void encode_msgset_item(upb_encstate* e,
encode_tag(e, kUpb_MsgSet_Item, kUpb_WireType_StartGroup);
}
static void encode_ext(upb_encstate* e, const upb_Message_Extension* ext,
static void encode_ext(upb_encstate* e, const upb_Extension* ext,
bool is_message_set) {
if (UPB_UNLIKELY(is_message_set)) {
encode_msgset_item(e, ext);
@ -570,7 +569,7 @@ static void encode_message(upb_encstate* e, const upb_Message* msg,
* these in field number order relative to normal fields or even to each
* other. */
size_t ext_count;
const upb_Message_Extension* ext = _upb_Message_Getexts(msg, &ext_count);
const upb_Extension* ext = _upb_Message_Getexts(msg, &ext_count);
if (ext_count) {
if (e->options & kUpb_EncodeOption_Deterministic) {
_upb_sortedmap sorted;
@ -580,7 +579,7 @@ static void encode_message(upb_encstate* e, const upb_Message* msg,
}
_upb_mapsorter_popmap(&e->sorter, &sorted);
} else {
const upb_Message_Extension* end = ext + ext_count;
const upb_Extension* end = ext + ext_count;
for (; ext != end; ext++) {
encode_ext(e, ext, m->UPB_PRIVATE(ext) == kUpb_ExtMode_IsMessageSet);
}

Loading…
Cancel
Save