upb: clean up and lock down the internal hasbit accessors

PiperOrigin-RevId: 588147628
pull/14932/head
Eric Salo 12 months ago committed by Copybara-Service
parent 67571d0375
commit 347486dd85
  1. 54
      upb/message/internal/accessors.h
  2. 14
      upb/mini_table/internal/field.h
  3. 2
      upb/wire/decode.c
  4. 2
      upb/wire/encode.c

@ -55,51 +55,27 @@ extern "C" {
// Hasbit access ///////////////////////////////////////////////////////////////
UPB_INLINE size_t _upb_Hasbit_Offset(size_t index) { return index / 8; }
UPB_INLINE char _upb_Hasbit_Mask(size_t index) { return 1 << (index % 8); }
UPB_INLINE size_t _upb_Hasbit_Index(const upb_MiniTableField* f) {
UPB_ASSERT(f->presence > 0);
return f->presence;
}
UPB_INLINE bool _upb_Message_GetHasbitByIndex(const upb_Message* msg,
size_t index) {
const size_t offset = _upb_Hasbit_Offset(index);
const char mask = _upb_Hasbit_Mask(index);
UPB_INLINE bool UPB_PRIVATE(_upb_Message_GetHasbit)(
const upb_Message* msg, const upb_MiniTableField* f) {
const size_t offset = _upb_MiniTableField_HasbitOffset(f);
const char mask = _upb_MiniTableField_HasbitMask(f);
return (*UPB_PTR_AT(msg, offset, const char) & mask) != 0;
}
UPB_INLINE void _upb_Message_SetHasbitByIndex(const upb_Message* msg,
size_t index) {
const size_t offset = _upb_Hasbit_Offset(index);
const char mask = _upb_Hasbit_Mask(index);
UPB_INLINE void UPB_PRIVATE(_upb_Message_SetHasbit)(
const upb_Message* msg, const upb_MiniTableField* f) {
const size_t offset = _upb_MiniTableField_HasbitOffset(f);
const char mask = _upb_MiniTableField_HasbitMask(f);
(*UPB_PTR_AT(msg, offset, char)) |= mask;
}
UPB_INLINE void _upb_Message_ClearHasbitByIndex(const upb_Message* msg,
size_t index) {
const size_t offset = _upb_Hasbit_Offset(index);
const char mask = _upb_Hasbit_Mask(index);
UPB_INLINE void UPB_PRIVATE(_upb_Message_ClearHasbit)(
const upb_Message* msg, const upb_MiniTableField* f) {
const size_t offset = _upb_MiniTableField_HasbitOffset(f);
const char mask = _upb_MiniTableField_HasbitMask(f);
(*UPB_PTR_AT(msg, offset, char)) &= ~mask;
}
UPB_INLINE bool _upb_Message_GetHasbitByField(const upb_Message* msg,
const upb_MiniTableField* f) {
return _upb_Message_GetHasbitByIndex(msg, _upb_Hasbit_Index(f));
}
UPB_INLINE void _upb_Message_SetHasbitByField(const upb_Message* msg,
const upb_MiniTableField* f) {
_upb_Message_SetHasbitByIndex(msg, _upb_Hasbit_Index(f));
}
UPB_INLINE void _upb_Message_ClearHasbitByField(const upb_Message* msg,
const upb_MiniTableField* f) {
_upb_Message_ClearHasbitByIndex(msg, _upb_Hasbit_Index(f));
}
// Oneof case access ///////////////////////////////////////////////////////////
UPB_INLINE size_t _upb_MiniTableField_OneofOffset(const upb_MiniTableField* f) {
@ -139,7 +115,7 @@ UPB_INLINE const void* _upb_MiniTableField_GetConstPtr(
UPB_INLINE void _upb_Message_SetPresence(upb_Message* msg,
const upb_MiniTableField* field) {
if (field->presence > 0) {
_upb_Message_SetHasbitByField(msg, field);
UPB_PRIVATE(_upb_Message_SetHasbit)(msg, field);
} else if (upb_MiniTableField_IsInOneof(field)) {
_upb_Message_SetOneofCase(msg, field);
}
@ -230,7 +206,7 @@ UPB_INLINE bool _upb_Message_HasNonExtensionField(
return _upb_Message_GetOneofCase(msg, field) ==
upb_MiniTableField_Number(field);
} else {
return _upb_Message_GetHasbitByField(msg, field);
return UPB_PRIVATE(_upb_Message_GetHasbit)(msg, field);
}
}
@ -323,7 +299,7 @@ UPB_INLINE void _upb_Message_ClearExtensionField(
UPB_INLINE void _upb_Message_ClearNonExtensionField(
upb_Message* msg, const upb_MiniTableField* field) {
if (field->presence > 0) {
_upb_Message_ClearHasbitByField(msg, field);
UPB_PRIVATE(_upb_Message_ClearHasbit)(msg, field);
} else if (upb_MiniTableField_IsInOneof(field)) {
uint32_t* ptr = _upb_Message_OneofCasePtr(msg, field);
if (*ptr != upb_MiniTableField_Number(field)) return;

@ -130,6 +130,20 @@ UPB_PRIVATE(_upb_MiniTableField_CType)(const struct upb_MiniTableField* f) {
return upb_FieldType_CType(UPB_PRIVATE(_upb_MiniTableField_Type)(f));
}
UPB_INLINE char _upb_MiniTableField_HasbitMask(
const struct upb_MiniTableField* f) {
UPB_ASSERT(f->presence > 0);
const size_t index = f->presence;
return 1 << (index % 8);
}
UPB_INLINE size_t
_upb_MiniTableField_HasbitOffset(const struct upb_MiniTableField* f) {
UPB_ASSERT(f->presence > 0);
const size_t index = f->presence;
return index / 8;
}
UPB_INLINE bool UPB_PRIVATE(_upb_MiniTableField_IsClosedEnum)(
const struct upb_MiniTableField* f) {
return f->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Enum;

@ -689,7 +689,7 @@ static const char* _upb_Decoder_DecodeToSubMessage(
/* Set presence if necessary. */
if (field->presence > 0) {
_upb_Message_SetHasbitByField(msg, field);
UPB_PRIVATE(_upb_Message_SetHasbit)(msg, field);
} else if (field->presence < 0) {
/* Oneof case */
uint32_t* oneof_case = _upb_Message_OneofCasePtr(msg, field);

@ -493,7 +493,7 @@ static bool encode_shouldencode(upb_encstate* e, const upb_Message* msg,
}
} else if (f->presence > 0) {
// Proto2 presence: hasbit.
return _upb_Message_GetHasbitByField(msg, f);
return UPB_PRIVATE(_upb_Message_GetHasbit)(msg, f);
} else {
// Field is in a oneof.
return _upb_Message_GetOneofCase(msg, f) == f->UPB_PRIVATE(number);

Loading…
Cancel
Save