Skip to content

Commit

Permalink
Unified accessor for WhichOneof().
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 495220998
  • Loading branch information
haberman authored and copybara-github committed Dec 14, 2022
1 parent 9e9d43b commit fa2481d
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 31 deletions.
12 changes: 9 additions & 3 deletions upb/message/accessors.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
// literals or UPB_ASSUME():
//
// // Via string literals.
// // Via struct literals.
// bool FooMessage_set_bool_field(const upb_Message* msg, bool val) {
// const upb_MiniTableField field = {1, 0, 0, /* etc... */};
// // All value in "field" are compile-time known.
Expand All @@ -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
// accessors: generated code, MiniTable accessors, and reflection. The only
// 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
// of a setter is known to be a non-extension, the arena may be NULL and the
Expand Down Expand Up @@ -240,7 +240,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_clearhas_field(msg, field);
_upb_clearhas(msg, _upb_Message_Hasidx(field));
} else if (_upb_MiniTableField_InOneOf(field)) {
uint32_t* oneof_case = _upb_oneofcase_field(msg, field);
if (*oneof_case != field->number) return;
Expand Down Expand Up @@ -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,
const upb_MiniTableField* field,
bool default_val) {
Expand Down
21 changes: 2 additions & 19 deletions upb/mini_table/field_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,38 +139,21 @@ UPB_INLINE void _upb_sethas_field(const upb_Message* msg,
_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 ///////////////////////////////////////////////////////////

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_ASSERT(f->presence < 0);
return ~(ptrdiff_t)f->presence;
}

UPB_INLINE uint32_t* _upb_oneofcase_field(upb_Message* msg,
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,
const upb_MiniTableField* f) {
return _upb_getoneofcase(msg, _upb_oneofcase_ofs(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;
return *_upb_oneofcase_field((upb_Message*)msg, f);
}

// LINT.ThenChange(GoogleInternalName2)
Expand Down
4 changes: 0 additions & 4 deletions upb/port/def.inc
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@
*/
#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

// UPB_EXPORT: always generate a public symbol.
Expand Down
2 changes: 0 additions & 2 deletions upb/port/undef.inc
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@

#undef UPB_SIZE
#undef UPB_PTR_AT
#undef UPB_READ_ONEOF
#undef UPB_WRITE_ONEOF
#undef UPB_MAPTYPE_STRING
#undef UPB_EXPORT
#undef UPB_INLINE
Expand Down
2 changes: 1 addition & 1 deletion upb/reflection/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const upb_FieldDef* upb_Message_WhichOneof(const upb_Message* msg,
return upb_Message_HasFieldByDef(msg, f) ? f : NULL;
} else {
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;
UPB_ASSERT((f != NULL) == (oneof_case != 0));
return f;
Expand Down
6 changes: 4 additions & 2 deletions upbc/protoc-gen-upb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -367,10 +367,12 @@ void GenerateOneofInHeader(const protobuf::OneofDescriptor* oneof,
output(
R"cc(
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",
fullname, msg_name, oneof->name(), layout.GetOneofCaseOffset(oneof));
fullname, msg_name, oneof->name(),
FieldInitializer(layout, oneof->field(0)));
}

void GenerateHazzer(const protobuf::FieldDescriptor* field,
Expand Down

0 comments on commit fa2481d

Please sign in to comment.