Unified accessor for WhichOneof().

PiperOrigin-RevId: 495220998
pull/13171/head
Joshua Haberman 2 years ago committed by Copybara-Service
parent 9e9d43b462
commit fa2481ddeb
  1. 12
      upb/message/accessors.h
  2. 21
      upb/mini_table/field_internal.h
  3. 4
      upb/port/def.inc
  4. 2
      upb/port/undef.inc
  5. 2
      upb/reflection/message.c
  6. 6
      upbc/protoc-gen-upb.cc

@ -112,7 +112,7 @@ UPB_INLINE void _upb_MiniTable_CopyFieldData(void* to, const void* from,
// we are left with ideal code. This can happen either through through // we are left with ideal code. This can happen either through through
// literals or UPB_ASSUME(): // literals or UPB_ASSUME():
// //
// // Via string literals. // // Via struct literals.
// bool FooMessage_set_bool_field(const upb_Message* msg, bool val) { // bool FooMessage_set_bool_field(const upb_Message* msg, bool val) {
// const upb_MiniTableField field = {1, 0, 0, /* etc... */}; // const upb_MiniTableField field = {1, 0, 0, /* etc... */};
// // All value in "field" are compile-time known. // // All value in "field" are compile-time known.
@ -132,7 +132,7 @@ UPB_INLINE void _upb_MiniTable_CopyFieldData(void* to, const void* from,
// As a result, we can use these universal getters/setters for *all* message // As a result, we can use these universal getters/setters for *all* message
// accessors: generated code, MiniTable accessors, and reflection. The only // accessors: generated code, MiniTable accessors, and reflection. The only
// exception is the binary encoder/decoder, which need to be a bit more clever // exception is the binary encoder/decoder, which need to be a bit more clever
// about how the read/write the message data, for efficiency. // about how they read/write the message data, for efficiency.
// //
// These functions work on both extensions and non-extensions. If the field // These functions work on both extensions and non-extensions. If the field
// of a setter is known to be a non-extension, the arena may be NULL and the // of a setter is known to be a non-extension, the arena may be NULL and the
@ -240,7 +240,7 @@ UPB_INLINE void _upb_Message_ClearExtensionField(
UPB_INLINE void _upb_Message_ClearNonExtensionField( UPB_INLINE void _upb_Message_ClearNonExtensionField(
upb_Message* msg, const upb_MiniTableField* field) { upb_Message* msg, const upb_MiniTableField* field) {
if (field->presence > 0) { if (field->presence > 0) {
_upb_clearhas_field(msg, field); _upb_clearhas(msg, _upb_Message_Hasidx(field));
} else if (_upb_MiniTableField_InOneOf(field)) { } else if (_upb_MiniTableField_InOneOf(field)) {
uint32_t* oneof_case = _upb_oneofcase_field(msg, field); uint32_t* oneof_case = _upb_oneofcase_field(msg, field);
if (*oneof_case != field->number) return; if (*oneof_case != field->number) return;
@ -273,6 +273,12 @@ 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_InOneOf(oneof_field));
return _upb_getoneofcase_field(message, oneof_field);
}
UPB_API_INLINE bool upb_Message_GetBool(const upb_Message* msg, UPB_API_INLINE bool upb_Message_GetBool(const upb_Message* msg,
const upb_MiniTableField* field, const upb_MiniTableField* field,
bool default_val) { bool default_val) {

@ -139,21 +139,8 @@ UPB_INLINE void _upb_sethas_field(const upb_Message* msg,
_upb_sethas(msg, _upb_Message_Hasidx(f)); _upb_sethas(msg, _upb_Message_Hasidx(f));
} }
UPB_INLINE void _upb_clearhas_field(const upb_Message* msg,
const upb_MiniTableField* f) {
_upb_clearhas(msg, _upb_Message_Hasidx(f));
}
// Oneof case access /////////////////////////////////////////////////////////// // Oneof case access ///////////////////////////////////////////////////////////
UPB_INLINE uint32_t* _upb_oneofcase(upb_Message* msg, size_t case_ofs) {
return UPB_PTR_AT(msg, case_ofs, uint32_t);
}
UPB_INLINE uint32_t _upb_getoneofcase(const void* msg, size_t case_ofs) {
return *UPB_PTR_AT(msg, case_ofs, uint32_t);
}
UPB_INLINE size_t _upb_oneofcase_ofs(const upb_MiniTableField* f) { UPB_INLINE size_t _upb_oneofcase_ofs(const upb_MiniTableField* f) {
UPB_ASSERT(f->presence < 0); UPB_ASSERT(f->presence < 0);
return ~(ptrdiff_t)f->presence; return ~(ptrdiff_t)f->presence;
@ -161,16 +148,12 @@ UPB_INLINE size_t _upb_oneofcase_ofs(const upb_MiniTableField* f) {
UPB_INLINE uint32_t* _upb_oneofcase_field(upb_Message* msg, UPB_INLINE uint32_t* _upb_oneofcase_field(upb_Message* msg,
const upb_MiniTableField* f) { const upb_MiniTableField* f) {
return _upb_oneofcase(msg, _upb_oneofcase_ofs(f)); return UPB_PTR_AT(msg, _upb_oneofcase_ofs(f), uint32_t);
} }
UPB_INLINE uint32_t _upb_getoneofcase_field(const upb_Message* msg, UPB_INLINE uint32_t _upb_getoneofcase_field(const upb_Message* msg,
const upb_MiniTableField* f) { const upb_MiniTableField* f) {
return _upb_getoneofcase(msg, _upb_oneofcase_ofs(f)); return *_upb_oneofcase_field((upb_Message*)msg, f);
}
UPB_INLINE bool _upb_has_submsg_nohasbit(const upb_Message* msg, size_t ofs) {
return *UPB_PTR_AT(msg, ofs, const upb_Message*) != NULL;
} }
// LINT.ThenChange(GoogleInternalName2) // LINT.ThenChange(GoogleInternalName2)

@ -80,10 +80,6 @@
*/ */
#define UPB_PTR_AT(msg, ofs, type) ((type*)((char*)(msg) + (ofs))) #define UPB_PTR_AT(msg, ofs, type) ((type*)((char*)(msg) + (ofs)))
#define UPB_WRITE_ONEOF(msg, fieldtype, offset, value, case_offset, case_val) \
*UPB_PTR_AT(msg, case_offset, int) = case_val; \
*UPB_PTR_AT(msg, offset, fieldtype) = value;
#define UPB_MAPTYPE_STRING 0 #define UPB_MAPTYPE_STRING 0
// UPB_EXPORT: always generate a public symbol. // UPB_EXPORT: always generate a public symbol.

@ -29,8 +29,6 @@
#undef UPB_SIZE #undef UPB_SIZE
#undef UPB_PTR_AT #undef UPB_PTR_AT
#undef UPB_READ_ONEOF
#undef UPB_WRITE_ONEOF
#undef UPB_MAPTYPE_STRING #undef UPB_MAPTYPE_STRING
#undef UPB_EXPORT #undef UPB_EXPORT
#undef UPB_INLINE #undef UPB_INLINE

@ -57,7 +57,7 @@ const upb_FieldDef* upb_Message_WhichOneof(const upb_Message* msg,
return upb_Message_HasFieldByDef(msg, f) ? f : NULL; return upb_Message_HasFieldByDef(msg, f) ? f : NULL;
} else { } else {
const upb_MiniTableField* field = upb_FieldDef_MiniTable(f); const upb_MiniTableField* field = upb_FieldDef_MiniTable(f);
uint32_t oneof_case = _upb_getoneofcase_field(msg, field); uint32_t oneof_case = upb_Message_WhichOneofFieldNumber(msg, field);
f = oneof_case ? upb_OneofDef_LookupNumber(o, oneof_case) : NULL; f = oneof_case ? upb_OneofDef_LookupNumber(o, oneof_case) : NULL;
UPB_ASSERT((f != NULL) == (oneof_case != 0)); UPB_ASSERT((f != NULL) == (oneof_case != 0));
return f; return f;

@ -367,10 +367,12 @@ void GenerateOneofInHeader(const protobuf::OneofDescriptor* oneof,
output( output(
R"cc( R"cc(
UPB_INLINE $0_oneofcases $1_$2_case(const $1* msg) { UPB_INLINE $0_oneofcases $1_$2_case(const $1* msg) {
return ($0_oneofcases)*UPB_PTR_AT(msg, $3, int32_t); const upb_MiniTableField field = $3;
return ($0_oneofcases)upb_Message_WhichOneofFieldNumber(msg, &field);
} }
)cc", )cc",
fullname, msg_name, oneof->name(), layout.GetOneofCaseOffset(oneof)); fullname, msg_name, oneof->name(),
FieldInitializer(layout, oneof->field(0)));
} }
void GenerateHazzer(const protobuf::FieldDescriptor* field, void GenerateHazzer(const protobuf::FieldDescriptor* field,

Loading…
Cancel
Save