upb: clean up and lock down the internal oneof accessors

PiperOrigin-RevId: 588166536
pull/14940/head
Eric Salo 12 months ago committed by Copybara-Service
parent 7713df55ab
commit 6efd909a9b
  1. 4
      upb/message/accessors.h
  2. 34
      upb/message/internal/accessors.h
  3. 4
      upb/message/promote.c
  4. 6
      upb/mini_table/internal/field.h
  5. 2
      upb/wire/decode.c
  6. 3
      upb/wire/encode.c

@ -68,7 +68,7 @@ UPB_API_INLINE bool upb_Message_HasField(const upb_Message* msg,
UPB_API_INLINE uint32_t upb_Message_WhichOneofFieldNumber(
const upb_Message* message, const upb_MiniTableField* oneof_field) {
UPB_ASSUME(upb_MiniTableField_IsInOneof(oneof_field));
return _upb_Message_GetOneofCase(message, oneof_field);
return UPB_PRIVATE(_upb_Message_GetOneofCase)(message, oneof_field);
}
// NOTE: The default_val is only used for fields that support presence.
@ -371,7 +371,7 @@ UPB_API_INLINE upb_Message* upb_Message_GetOrCreateMutableMessage(
UPB_ASSERT(sub_mini_table);
sub_message = _upb_Message_New(sub_mini_table, arena);
*UPB_PTR_AT(msg, field->offset, upb_Message*) = sub_message;
_upb_Message_SetPresence(msg, field);
UPB_PRIVATE(_upb_Message_SetPresence)(msg, field);
}
return sub_message;
}

@ -78,24 +78,20 @@ UPB_INLINE void UPB_PRIVATE(_upb_Message_ClearHasbit)(
// Oneof case access ///////////////////////////////////////////////////////////
UPB_INLINE size_t _upb_MiniTableField_OneofOffset(const upb_MiniTableField* f) {
UPB_ASSERT(f->presence < 0);
return ~(ptrdiff_t)f->presence;
}
UPB_INLINE uint32_t* _upb_Message_OneofCasePtr(upb_Message* msg,
const upb_MiniTableField* f) {
UPB_INLINE uint32_t* UPB_PRIVATE(_upb_Message_OneofCasePtr)(
upb_Message* msg, const upb_MiniTableField* f) {
return UPB_PTR_AT(msg, _upb_MiniTableField_OneofOffset(f), uint32_t);
}
UPB_INLINE uint32_t _upb_Message_GetOneofCase(const upb_Message* msg,
const upb_MiniTableField* f) {
return *_upb_Message_OneofCasePtr((upb_Message*)msg, f);
UPB_INLINE uint32_t UPB_PRIVATE(_upb_Message_GetOneofCase)(
const upb_Message* msg, const upb_MiniTableField* f) {
return *UPB_PRIVATE(_upb_Message_OneofCasePtr)((upb_Message*)msg, f);
}
UPB_INLINE void _upb_Message_SetOneofCase(upb_Message* msg,
const upb_MiniTableField* f) {
*_upb_Message_OneofCasePtr(msg, f) = upb_MiniTableField_Number(f);
UPB_INLINE void UPB_PRIVATE(_upb_Message_SetOneofCase)(
upb_Message* msg, const upb_MiniTableField* f) {
*UPB_PRIVATE(_upb_Message_OneofCasePtr)(msg, f) =
upb_MiniTableField_Number(f);
}
// TODO: implement _upb_Message_ClearOneofCase()
@ -112,12 +108,12 @@ UPB_INLINE const void* _upb_MiniTableField_GetConstPtr(
return (char*)msg + field->offset;
}
UPB_INLINE void _upb_Message_SetPresence(upb_Message* msg,
const upb_MiniTableField* field) {
UPB_INLINE void UPB_PRIVATE(_upb_Message_SetPresence)(
upb_Message* msg, const upb_MiniTableField* field) {
if (field->presence > 0) {
UPB_PRIVATE(_upb_Message_SetHasbit)(msg, field);
} else if (upb_MiniTableField_IsInOneof(field)) {
_upb_Message_SetOneofCase(msg, field);
UPB_PRIVATE(_upb_Message_SetOneofCase)(msg, field);
}
}
@ -203,7 +199,7 @@ UPB_INLINE bool _upb_Message_HasNonExtensionField(
UPB_ASSERT(upb_MiniTableField_HasPresence(field));
UPB_ASSUME(!upb_MiniTableField_IsExtension(field));
if (upb_MiniTableField_IsInOneof(field)) {
return _upb_Message_GetOneofCase(msg, field) ==
return UPB_PRIVATE(_upb_Message_GetOneofCase)(msg, field) ==
upb_MiniTableField_Number(field);
} else {
return UPB_PRIVATE(_upb_Message_GetHasbit)(msg, field);
@ -266,7 +262,7 @@ UPB_INLINE upb_MutableMessageValue _upb_Message_GetMutableField(
UPB_INLINE void _upb_Message_SetNonExtensionField(
upb_Message* msg, const upb_MiniTableField* field, const void* val) {
UPB_ASSUME(!upb_MiniTableField_IsExtension(field));
_upb_Message_SetPresence(msg, field);
UPB_PRIVATE(_upb_Message_SetPresence)(msg, field);
_upb_MiniTable_CopyFieldData(_upb_MiniTableField_GetPtr(msg, field), val,
field);
}
@ -301,7 +297,7 @@ UPB_INLINE void _upb_Message_ClearNonExtensionField(
if (field->presence > 0) {
UPB_PRIVATE(_upb_Message_ClearHasbit)(msg, field);
} else if (upb_MiniTableField_IsInOneof(field)) {
uint32_t* ptr = _upb_Message_OneofCasePtr(msg, field);
uint32_t* ptr = UPB_PRIVATE(_upb_Message_OneofCasePtr)(msg, field);
if (*ptr != upb_MiniTableField_Number(field)) return;
*ptr = 0;
}

@ -241,7 +241,7 @@ upb_UnknownToMessageRet upb_MiniTable_PromoteUnknownToMessage(
UPB_ASSERT(upb_MiniTable_GetSubMessageTable(mini_table, field) ==
sub_mini_table);
bool is_oneof = upb_MiniTableField_IsInOneof(field);
if (!is_oneof || _upb_Message_GetOneofCase(msg, field) ==
if (!is_oneof || UPB_PRIVATE(_upb_Message_GetOneofCase)(msg, field) ==
upb_MiniTableField_Number(field)) {
UPB_ASSERT(upb_Message_GetMessage(msg, field, NULL) == NULL);
}
@ -275,7 +275,7 @@ upb_UnknownToMessageRet upb_MiniTable_PromoteUnknownToMessage(
} while (unknown.status == kUpb_FindUnknown_Ok);
if (message) {
if (is_oneof) {
_upb_Message_SetOneofCase(msg, field);
UPB_PRIVATE(_upb_Message_SetOneofCase)(msg, field);
}
upb_Message_SetMessage(msg, mini_table, field, message);
ret.message = message;

@ -174,6 +174,12 @@ UPB_PRIVATE(_upb_MiniTableField_Number)(const struct upb_MiniTableField* f) {
return f->UPB_ONLYBITS(number);
}
UPB_INLINE size_t
_upb_MiniTableField_OneofOffset(const struct upb_MiniTableField* f) {
UPB_ASSERT(UPB_PRIVATE(_upb_MiniTableField_IsInOneof)(f));
return ~(ptrdiff_t)f->presence;
}
UPB_INLINE void UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(
const struct upb_MiniTableField* f) {
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==

@ -692,7 +692,7 @@ static const char* _upb_Decoder_DecodeToSubMessage(
UPB_PRIVATE(_upb_Message_SetHasbit)(msg, field);
} else if (field->presence < 0) {
/* Oneof case */
uint32_t* oneof_case = _upb_Message_OneofCasePtr(msg, field);
uint32_t* oneof_case = UPB_PRIVATE(_upb_Message_OneofCasePtr)(msg, field);
if (op == kUpb_DecodeOp_SubMessage &&
*oneof_case != field->UPB_PRIVATE(number)) {
memset(mem, 0, sizeof(void*));

@ -496,7 +496,8 @@ static bool encode_shouldencode(upb_encstate* e, const upb_Message* msg,
return UPB_PRIVATE(_upb_Message_GetHasbit)(msg, f);
} else {
// Field is in a oneof.
return _upb_Message_GetOneofCase(msg, f) == f->UPB_PRIVATE(number);
return UPB_PRIVATE(_upb_Message_GetOneofCase)(msg, f) ==
f->UPB_PRIVATE(number);
}
}

Loading…
Cancel
Save