normalize most of the message accessors:

- Rename the accessors from upb_MiniTable_Foo() to upb_Message_Foo()
- delete _upb_Message_Clearext() which is now redundant
- Allow the getters and setters to accept both extension and non-extension fields
- Add a (upb_Arena*) param to setters (only needed for extensions)
- Change setters from void to bool (since extensions may require allocations)

PiperOrigin-RevId: 493760399
pull/13171/head
Eric Salo 2 years ago committed by Copybara-Service
parent 44e6f6699a
commit 3bac8780cb
  1. 1
      protos/BUILD
  2. 4
      protos/protos.h
  3. 282
      upb/message/accessors.h
  4. 108
      upb/message/accessors_test.cc
  5. 2
      upb/message/extension_internal.h
  6. 14
      upb/message/message.c
  7. 8
      upb/reflection/message.c
  8. 16
      upbc/protoc-gen-upb.cc

@ -41,6 +41,7 @@ cc_library(
copts = UPB_DEFAULT_CPPOPTS, copts = UPB_DEFAULT_CPPOPTS,
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
deps = [ deps = [
"//:message_accessors",
"//:mini_table", "//:mini_table",
"//:upb", "//:upb",
"@com_google_absl//absl/status", "@com_google_absl//absl/status",

@ -270,7 +270,7 @@ template <typename T, typename Extendee, typename Extension,
void ClearExtension( void ClearExtension(
const Ptr<T>& message, const Ptr<T>& message,
const ::protos::internal::ExtensionIdentifier<Extendee, Extension>& id) { const ::protos::internal::ExtensionIdentifier<Extendee, Extension>& id) {
_upb_Message_Clearext(message->msg(), id.mini_table_ext()); _upb_Message_ClearExtensionField(message->msg(), id.mini_table_ext());
} }
template <typename T, typename Extendee, typename Extension, template <typename T, typename Extendee, typename Extension,
@ -278,7 +278,7 @@ template <typename T, typename Extendee, typename Extension,
void ClearExtension( void ClearExtension(
const T& message, const T& message,
const ::protos::internal::ExtensionIdentifier<Extendee, Extension>& id) { const ::protos::internal::ExtensionIdentifier<Extendee, Extension>& id) {
_upb_Message_Clearext(message.msg(), id.mini_table_ext()); _upb_Message_ClearExtensionField(message.msg(), id.mini_table_ext());
} }
template <typename T, typename Extendee, typename Extension, template <typename T, typename Extendee, typename Extension,

@ -56,8 +56,8 @@ UPB_INLINE const void* _upb_MiniTableField_GetConstPtr(
return (char*)msg + field->offset; return (char*)msg + field->offset;
} }
UPB_INLINE void _upb_MiniTable_SetPresence(upb_Message* msg, UPB_INLINE void _upb_Message_SetPresence(upb_Message* msg,
const upb_MiniTableField* field) { const upb_MiniTableField* field) {
if (field->presence > 0) { if (field->presence > 0) {
_upb_sethas_field(msg, field); _upb_sethas_field(msg, field);
} else if (_upb_MiniTableField_InOneOf(field)) { } else if (_upb_MiniTableField_InOneOf(field)) {
@ -65,9 +65,6 @@ UPB_INLINE void _upb_MiniTable_SetPresence(upb_Message* msg,
} }
} }
UPB_API_INLINE bool upb_MiniTable_HasField(const upb_Message* msg,
const upb_MiniTableField* field);
UPB_INLINE bool _upb_MiniTable_ValueIsNonZero(const void* default_val, UPB_INLINE bool _upb_MiniTable_ValueIsNonZero(const void* default_val,
const upb_MiniTableField* field) { const upb_MiniTableField* field) {
char zero[16] = {0}; char zero[16] = {0};
@ -116,31 +113,52 @@ UPB_INLINE void _upb_MiniTable_CopyFieldData(void* to, const void* from,
// 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.
// _upb_MiniTable_SetNonExtensionField(msg, &field, &value); // _upb_Message_SetNonExtensionField(msg, &field, &value);
// } // }
// //
// // Via UPB_ASSUME(). // // Via UPB_ASSUME().
// UPB_INLINE void upb_MiniTable_SetBool(upb_Message* msg, // UPB_INLINE bool upb_Message_SetBool(upb_Message* msg,
// const upb_MiniTableField* field, // const upb_MiniTableField* field,
// bool value) { // bool value, upb_Arena* a) {
// UPB_ASSUME(field->descriptortype == kUpb_FieldType_Bool); // UPB_ASSUME(field->descriptortype == kUpb_FieldType_Bool);
// UPB_ASSUME(!upb_IsRepeatedOrMap(field)); // UPB_ASSUME(!upb_IsRepeatedOrMap(field));
// UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_1Byte); // UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_1Byte);
// _upb_MiniTable_SetNonExtensionField(msg, field, &value); // _upb_Message_SetField(msg, field, &value, a);
// } // }
// //
// 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 the 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
// returned bool value may be ignored since it will always succeed.
UPB_INLINE bool _upb_Message_HasExtensionField(
const upb_Message* msg, const upb_MiniTableExtension* ext) {
UPB_ASSERT(upb_MiniTableField_HasPresence(&ext->field));
return _upb_Message_Getext(msg, ext) != NULL;
}
UPB_INLINE bool _upb_Message_HasNonExtensionField(
const upb_Message* msg, const upb_MiniTableField* field) {
UPB_ASSERT(upb_MiniTableField_HasPresence(field));
UPB_ASSUME(!upb_MiniTableField_IsExtension(field));
if (_upb_MiniTableField_InOneOf(field)) {
return _upb_getoneofcase_field(msg, field) == field->number;
} else {
return _upb_hasbit_field(msg, field);
}
}
static UPB_FORCEINLINE void _upb_MiniTable_GetNonExtensionField( static UPB_FORCEINLINE void _upb_Message_GetNonExtensionField(
const upb_Message* msg, const upb_MiniTableField* field, const upb_Message* msg, const upb_MiniTableField* field,
const void* default_val, void* val) { const void* default_val, void* val) {
UPB_ASSUME(!upb_MiniTableField_IsExtension(field)); UPB_ASSUME(!upb_MiniTableField_IsExtension(field));
if ((_upb_MiniTableField_InOneOf(field) || if ((_upb_MiniTableField_InOneOf(field) ||
_upb_MiniTable_ValueIsNonZero(default_val, field)) && _upb_MiniTable_ValueIsNonZero(default_val, field)) &&
!upb_MiniTable_HasField(msg, field)) { !_upb_Message_HasNonExtensionField(msg, field)) {
_upb_MiniTable_CopyFieldData(val, default_val, field); _upb_MiniTable_CopyFieldData(val, default_val, field);
return; return;
} }
@ -148,7 +166,7 @@ static UPB_FORCEINLINE void _upb_MiniTable_GetNonExtensionField(
field); field);
} }
UPB_INLINE void _upb_MiniTable_GetExtensionField( UPB_INLINE void _upb_Message_GetExtensionField(
const upb_Message* msg, const upb_MiniTableExtension* mt_ext, const upb_Message* msg, const upb_MiniTableExtension* mt_ext,
const void* default_val, void* val) { const void* default_val, void* val) {
UPB_ASSUME(upb_MiniTableField_IsExtension(&mt_ext->field)); UPB_ASSUME(upb_MiniTableField_IsExtension(&mt_ext->field));
@ -160,28 +178,29 @@ UPB_INLINE void _upb_MiniTable_GetExtensionField(
} }
} }
UPB_INLINE void _upb_MiniTable_GetField(const upb_Message* msg, UPB_INLINE void _upb_Message_GetField(const upb_Message* msg,
const upb_MiniTableField* field, const upb_MiniTableField* field,
const void* default_val, void* val) { const void* default_val, void* val) {
if (upb_MiniTableField_IsExtension(field)) { if (upb_MiniTableField_IsExtension(field)) {
_upb_MiniTable_GetExtensionField(msg, (upb_MiniTableExtension*)field, _upb_Message_GetExtensionField(msg, (upb_MiniTableExtension*)field,
default_val, val); default_val, val);
} else { } else {
_upb_MiniTable_GetNonExtensionField(msg, field, default_val, val); _upb_Message_GetNonExtensionField(msg, field, default_val, val);
} }
} }
UPB_INLINE void _upb_MiniTable_SetNonExtensionField( UPB_INLINE void _upb_Message_SetNonExtensionField(
upb_Message* msg, const upb_MiniTableField* field, const void* val) { upb_Message* msg, const upb_MiniTableField* field, const void* val) {
UPB_ASSUME(!upb_MiniTableField_IsExtension(field)); UPB_ASSUME(!upb_MiniTableField_IsExtension(field));
_upb_MiniTable_SetPresence(msg, field); _upb_Message_SetPresence(msg, field);
_upb_MiniTable_CopyFieldData(_upb_MiniTableField_GetPtr(msg, field), val, _upb_MiniTable_CopyFieldData(_upb_MiniTableField_GetPtr(msg, field), val,
field); field);
} }
UPB_INLINE bool _upb_MiniTable_SetExtensionField( UPB_INLINE bool _upb_Message_SetExtensionField(
upb_Message* msg, const upb_MiniTableExtension* mt_ext, const void* val, upb_Message* msg, const upb_MiniTableExtension* mt_ext, const void* val,
upb_Arena* a) { upb_Arena* a) {
UPB_ASSERT(a);
upb_Message_Extension* ext = upb_Message_Extension* ext =
_upb_Message_GetOrCreateExtension(msg, mt_ext, a); _upb_Message_GetOrCreateExtension(msg, mt_ext, a);
if (!ext) return false; if (!ext) return false;
@ -189,51 +208,33 @@ UPB_INLINE bool _upb_MiniTable_SetExtensionField(
return true; return true;
} }
UPB_INLINE bool _upb_MiniTable_SetField(upb_Message* msg, UPB_INLINE bool _upb_Message_SetField(upb_Message* msg,
const upb_MiniTableField* field, const upb_MiniTableField* field,
const void* val, upb_Arena* a) { const void* val, upb_Arena* a) {
if (upb_MiniTableField_IsExtension(field)) { if (upb_MiniTableField_IsExtension(field)) {
return _upb_MiniTable_SetExtensionField( const upb_MiniTableExtension* ext = (const upb_MiniTableExtension*)field;
msg, (const upb_MiniTableExtension*)field, val, a); return _upb_Message_SetExtensionField(msg, ext, val, a);
} else { } else {
_upb_MiniTable_SetNonExtensionField(msg, field, val); _upb_Message_SetNonExtensionField(msg, field, val);
return true; return true;
} }
} }
UPB_INLINE bool _upb_MiniTable_HasExtensionField( UPB_INLINE void _upb_Message_ClearExtensionField(
const upb_Message* msg, const upb_MiniTableExtension* ext) { upb_Message* msg, const upb_MiniTableExtension* ext_l) {
UPB_ASSERT(upb_MiniTableField_HasPresence(&ext->field)); upb_Message_Internal* in = upb_Message_Getinternal(msg);
return _upb_Message_Getext(msg, ext) != NULL; if (!in->internal) return;
} const upb_Message_Extension* base =
UPB_PTR_AT(in->internal, in->internal->ext_begin, upb_Message_Extension);
UPB_INLINE bool _upb_MiniTable_HasNonExtensionField( upb_Message_Extension* ext =
const upb_Message* msg, const upb_MiniTableField* field) { (upb_Message_Extension*)_upb_Message_Getext(msg, ext_l);
UPB_ASSERT(upb_MiniTableField_HasPresence(field)); if (ext) {
UPB_ASSUME(!upb_MiniTableField_IsExtension(field)); *ext = *base;
if (_upb_MiniTableField_InOneOf(field)) { in->internal->ext_begin += sizeof(upb_Message_Extension);
return _upb_getoneofcase_field(msg, field) == field->number;
} else {
return _upb_hasbit_field(msg, field);
}
}
UPB_INLINE bool _upb_MiniTable_HasField(const upb_Message* msg,
const upb_MiniTableField* field) {
if (upb_MiniTableField_IsExtension(field)) {
return _upb_MiniTable_HasExtensionField(
msg, (const upb_MiniTableExtension*)field);
} else {
return _upb_MiniTable_HasNonExtensionField(msg, field);
} }
} }
UPB_INLINE void _upb_MiniTable_ClearExtensionField( UPB_INLINE void _upb_Message_ClearNonExtensionField(
upb_Message* msg, const upb_MiniTableExtension* ext) {
_upb_Message_Clearext(msg, ext);
}
UPB_INLINE void _upb_MiniTable_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_field(msg, field);
@ -247,50 +248,51 @@ UPB_INLINE void _upb_MiniTable_ClearNonExtensionField(
field); field);
} }
UPB_INLINE void _upb_MiniTable_ClearField(upb_Message* msg, // EVERYTHING ABOVE THIS LINE IS INTERNAL - DO NOT USE /////////////////////////
const upb_MiniTableField* field) {
UPB_API_INLINE void upb_Message_ClearField(upb_Message* msg,
const upb_MiniTableField* field) {
if (upb_MiniTableField_IsExtension(field)) { if (upb_MiniTableField_IsExtension(field)) {
_upb_Message_Clearext(msg, (const upb_MiniTableExtension*)field); const upb_MiniTableExtension* ext = (const upb_MiniTableExtension*)field;
_upb_Message_ClearExtensionField(msg, ext);
} else { } else {
_upb_MiniTable_ClearNonExtensionField(msg, field); _upb_Message_ClearNonExtensionField(msg, field);
} }
} }
// EVERYTHING ABOVE THIS LINE IS INTERNAL - DO NOT USE ///////////////////////// UPB_API_INLINE bool upb_Message_HasField(const upb_Message* msg,
const upb_MiniTableField* field) {
UPB_API_INLINE void upb_MiniTable_ClearField(upb_Message* msg, if (upb_MiniTableField_IsExtension(field)) {
const upb_MiniTableField* field) { const upb_MiniTableExtension* ext = (const upb_MiniTableExtension*)field;
_upb_MiniTable_ClearNonExtensionField(msg, field); return _upb_Message_HasExtensionField(msg, ext);
} } else {
return _upb_Message_HasNonExtensionField(msg, field);
UPB_API_INLINE bool upb_MiniTable_HasField(const upb_Message* msg, }
const upb_MiniTableField* field) {
return _upb_MiniTable_HasNonExtensionField(msg, field);
} }
UPB_API_INLINE bool upb_MiniTable_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) {
UPB_ASSUME(field->descriptortype == kUpb_FieldType_Bool); UPB_ASSUME(field->descriptortype == kUpb_FieldType_Bool);
UPB_ASSUME(!upb_IsRepeatedOrMap(field)); UPB_ASSUME(!upb_IsRepeatedOrMap(field));
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_1Byte); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_1Byte);
bool ret; bool ret;
_upb_MiniTable_GetNonExtensionField(msg, field, &default_val, &ret); _upb_Message_GetField(msg, field, &default_val, &ret);
return ret; return ret;
} }
UPB_API_INLINE void upb_MiniTable_SetBool(upb_Message* msg, UPB_API_INLINE bool upb_Message_SetBool(upb_Message* msg,
const upb_MiniTableField* field, const upb_MiniTableField* field,
bool value) { bool value, upb_Arena* a) {
UPB_ASSUME(field->descriptortype == kUpb_FieldType_Bool); UPB_ASSUME(field->descriptortype == kUpb_FieldType_Bool);
UPB_ASSUME(!upb_IsRepeatedOrMap(field)); UPB_ASSUME(!upb_IsRepeatedOrMap(field));
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_1Byte); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_1Byte);
_upb_MiniTable_SetNonExtensionField(msg, field, &value); return _upb_Message_SetField(msg, field, &value, a);
} }
UPB_API_INLINE int32_t upb_MiniTable_GetInt32(const upb_Message* msg, UPB_API_INLINE int32_t upb_Message_GetInt32(const upb_Message* msg,
const upb_MiniTableField* field, const upb_MiniTableField* field,
int32_t default_val) { int32_t default_val) {
UPB_ASSUME(field->descriptortype == kUpb_FieldType_Int32 || UPB_ASSUME(field->descriptortype == kUpb_FieldType_Int32 ||
field->descriptortype == kUpb_FieldType_SInt32 || field->descriptortype == kUpb_FieldType_SInt32 ||
field->descriptortype == kUpb_FieldType_SFixed32 || field->descriptortype == kUpb_FieldType_SFixed32 ||
@ -298,41 +300,41 @@ UPB_API_INLINE int32_t upb_MiniTable_GetInt32(const upb_Message* msg,
UPB_ASSUME(!upb_IsRepeatedOrMap(field)); UPB_ASSUME(!upb_IsRepeatedOrMap(field));
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte);
int32_t ret; int32_t ret;
_upb_MiniTable_GetNonExtensionField(msg, field, &default_val, &ret); _upb_Message_GetField(msg, field, &default_val, &ret);
return ret; return ret;
} }
UPB_API_INLINE void upb_MiniTable_SetInt32(upb_Message* msg, UPB_API_INLINE bool upb_Message_SetInt32(upb_Message* msg,
const upb_MiniTableField* field, const upb_MiniTableField* field,
int32_t value) { int32_t value, upb_Arena* a) {
UPB_ASSUME(field->descriptortype == kUpb_FieldType_Int32 || UPB_ASSUME(field->descriptortype == kUpb_FieldType_Int32 ||
field->descriptortype == kUpb_FieldType_SInt32 || field->descriptortype == kUpb_FieldType_SInt32 ||
field->descriptortype == kUpb_FieldType_SFixed32); field->descriptortype == kUpb_FieldType_SFixed32);
UPB_ASSUME(!upb_IsRepeatedOrMap(field)); UPB_ASSUME(!upb_IsRepeatedOrMap(field));
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte);
_upb_MiniTable_SetNonExtensionField(msg, field, &value); return _upb_Message_SetField(msg, field, &value, a);
} }
UPB_API_INLINE uint32_t upb_MiniTable_GetUInt32(const upb_Message* msg, UPB_API_INLINE uint32_t upb_Message_GetUInt32(const upb_Message* msg,
const upb_MiniTableField* field, const upb_MiniTableField* field,
uint32_t default_val) { uint32_t default_val) {
UPB_ASSUME(field->descriptortype == kUpb_FieldType_UInt32 || UPB_ASSUME(field->descriptortype == kUpb_FieldType_UInt32 ||
field->descriptortype == kUpb_FieldType_Fixed32); field->descriptortype == kUpb_FieldType_Fixed32);
UPB_ASSUME(!upb_IsRepeatedOrMap(field)); UPB_ASSUME(!upb_IsRepeatedOrMap(field));
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte);
uint32_t ret; uint32_t ret;
_upb_MiniTable_GetNonExtensionField(msg, field, &default_val, &ret); _upb_Message_GetField(msg, field, &default_val, &ret);
return ret; return ret;
} }
UPB_API_INLINE void upb_MiniTable_SetUInt32(upb_Message* msg, UPB_API_INLINE bool upb_Message_SetUInt32(upb_Message* msg,
const upb_MiniTableField* field, const upb_MiniTableField* field,
uint32_t value) { uint32_t value, upb_Arena* a) {
UPB_ASSUME(field->descriptortype == kUpb_FieldType_UInt32 || UPB_ASSUME(field->descriptortype == kUpb_FieldType_UInt32 ||
field->descriptortype == kUpb_FieldType_Fixed32); field->descriptortype == kUpb_FieldType_Fixed32);
UPB_ASSUME(!upb_IsRepeatedOrMap(field)); UPB_ASSUME(!upb_IsRepeatedOrMap(field));
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte);
_upb_MiniTable_SetNonExtensionField(msg, field, &value); return _upb_Message_SetField(msg, field, &value, a);
} }
UPB_API_INLINE void upb_MiniTable_SetEnumProto2( UPB_API_INLINE void upb_MiniTable_SetEnumProto2(
@ -343,115 +345,115 @@ UPB_API_INLINE void upb_MiniTable_SetEnumProto2(
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte);
UPB_ASSERT(upb_MiniTableEnum_CheckValue( UPB_ASSERT(upb_MiniTableEnum_CheckValue(
upb_MiniTable_GetSubEnumTable(msg_mini_table, field), value)); upb_MiniTable_GetSubEnumTable(msg_mini_table, field), value));
_upb_MiniTable_SetNonExtensionField(msg, field, &value); _upb_Message_SetNonExtensionField(msg, field, &value);
} }
UPB_API_INLINE int64_t upb_MiniTable_GetInt64(const upb_Message* msg, UPB_API_INLINE int64_t upb_Message_GetInt64(const upb_Message* msg,
const upb_MiniTableField* field, const upb_MiniTableField* field,
uint64_t default_val) { uint64_t default_val) {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_Int64 || UPB_ASSERT(field->descriptortype == kUpb_FieldType_Int64 ||
field->descriptortype == kUpb_FieldType_SInt64 || field->descriptortype == kUpb_FieldType_SInt64 ||
field->descriptortype == kUpb_FieldType_SFixed64); field->descriptortype == kUpb_FieldType_SFixed64);
UPB_ASSUME(!upb_IsRepeatedOrMap(field)); UPB_ASSUME(!upb_IsRepeatedOrMap(field));
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte);
int64_t ret; int64_t ret;
_upb_MiniTable_GetNonExtensionField(msg, field, &default_val, &ret); _upb_Message_GetField(msg, field, &default_val, &ret);
return ret; return ret;
} }
UPB_API_INLINE void upb_MiniTable_SetInt64(upb_Message* msg, UPB_API_INLINE bool upb_Message_SetInt64(upb_Message* msg,
const upb_MiniTableField* field, const upb_MiniTableField* field,
int64_t value) { int64_t value, upb_Arena* a) {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_Int64 || UPB_ASSERT(field->descriptortype == kUpb_FieldType_Int64 ||
field->descriptortype == kUpb_FieldType_SInt64 || field->descriptortype == kUpb_FieldType_SInt64 ||
field->descriptortype == kUpb_FieldType_SFixed64); field->descriptortype == kUpb_FieldType_SFixed64);
UPB_ASSUME(!upb_IsRepeatedOrMap(field)); UPB_ASSUME(!upb_IsRepeatedOrMap(field));
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte);
_upb_MiniTable_SetNonExtensionField(msg, field, &value); return _upb_Message_SetField(msg, field, &value, a);
} }
UPB_API_INLINE uint64_t upb_MiniTable_GetUInt64(const upb_Message* msg, UPB_API_INLINE uint64_t upb_Message_GetUInt64(const upb_Message* msg,
const upb_MiniTableField* field, const upb_MiniTableField* field,
uint64_t default_val) { uint64_t default_val) {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_UInt64 || UPB_ASSERT(field->descriptortype == kUpb_FieldType_UInt64 ||
field->descriptortype == kUpb_FieldType_Fixed64); field->descriptortype == kUpb_FieldType_Fixed64);
UPB_ASSUME(!upb_IsRepeatedOrMap(field)); UPB_ASSUME(!upb_IsRepeatedOrMap(field));
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte);
uint64_t ret; uint64_t ret;
_upb_MiniTable_GetNonExtensionField(msg, field, &default_val, &ret); _upb_Message_GetField(msg, field, &default_val, &ret);
return ret; return ret;
} }
UPB_API_INLINE void upb_MiniTable_SetUInt64(upb_Message* msg, UPB_API_INLINE bool upb_Message_SetUInt64(upb_Message* msg,
const upb_MiniTableField* field, const upb_MiniTableField* field,
uint64_t value) { uint64_t value, upb_Arena* a) {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_UInt64 || UPB_ASSERT(field->descriptortype == kUpb_FieldType_UInt64 ||
field->descriptortype == kUpb_FieldType_Fixed64); field->descriptortype == kUpb_FieldType_Fixed64);
UPB_ASSUME(!upb_IsRepeatedOrMap(field)); UPB_ASSUME(!upb_IsRepeatedOrMap(field));
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte);
_upb_MiniTable_SetNonExtensionField(msg, field, &value); return _upb_Message_SetField(msg, field, &value, a);
} }
UPB_API_INLINE float upb_MiniTable_GetFloat(const upb_Message* msg, UPB_API_INLINE float upb_Message_GetFloat(const upb_Message* msg,
const upb_MiniTableField* field, const upb_MiniTableField* field,
float default_val) { float default_val) {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_Float); UPB_ASSERT(field->descriptortype == kUpb_FieldType_Float);
UPB_ASSUME(!upb_IsRepeatedOrMap(field)); UPB_ASSUME(!upb_IsRepeatedOrMap(field));
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte);
float ret; float ret;
_upb_MiniTable_GetNonExtensionField(msg, field, &default_val, &ret); _upb_Message_GetField(msg, field, &default_val, &ret);
return ret; return ret;
} }
UPB_API_INLINE void upb_MiniTable_SetFloat(upb_Message* msg, UPB_API_INLINE bool upb_Message_SetFloat(upb_Message* msg,
const upb_MiniTableField* field, const upb_MiniTableField* field,
float value) { float value, upb_Arena* a) {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_Float); UPB_ASSERT(field->descriptortype == kUpb_FieldType_Float);
UPB_ASSUME(!upb_IsRepeatedOrMap(field)); UPB_ASSUME(!upb_IsRepeatedOrMap(field));
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte);
_upb_MiniTable_SetNonExtensionField(msg, field, &value); return _upb_Message_SetField(msg, field, &value, a);
} }
UPB_API_INLINE double upb_MiniTable_GetDouble(const upb_Message* msg, UPB_API_INLINE double upb_Message_GetDouble(const upb_Message* msg,
const upb_MiniTableField* field, const upb_MiniTableField* field,
double default_val) { double default_val) {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_Double); UPB_ASSERT(field->descriptortype == kUpb_FieldType_Double);
UPB_ASSUME(!upb_IsRepeatedOrMap(field)); UPB_ASSUME(!upb_IsRepeatedOrMap(field));
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte);
double ret; double ret;
_upb_MiniTable_GetNonExtensionField(msg, field, &default_val, &ret); _upb_Message_GetField(msg, field, &default_val, &ret);
return ret; return ret;
} }
UPB_API_INLINE void upb_MiniTable_SetDouble(upb_Message* msg, UPB_API_INLINE bool upb_Message_SetDouble(upb_Message* msg,
const upb_MiniTableField* field, const upb_MiniTableField* field,
double value) { double value, upb_Arena* a) {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_Double); UPB_ASSERT(field->descriptortype == kUpb_FieldType_Double);
UPB_ASSUME(!upb_IsRepeatedOrMap(field)); UPB_ASSUME(!upb_IsRepeatedOrMap(field));
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte);
_upb_MiniTable_SetNonExtensionField(msg, field, &value); return _upb_Message_SetField(msg, field, &value, a);
} }
UPB_API_INLINE upb_StringView UPB_API_INLINE upb_StringView
upb_MiniTable_GetString(const upb_Message* msg, const upb_MiniTableField* field, upb_Message_GetString(const upb_Message* msg, const upb_MiniTableField* field,
upb_StringView def_val) { upb_StringView def_val) {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_Bytes || UPB_ASSERT(field->descriptortype == kUpb_FieldType_Bytes ||
field->descriptortype == kUpb_FieldType_String); field->descriptortype == kUpb_FieldType_String);
UPB_ASSUME(!upb_IsRepeatedOrMap(field)); UPB_ASSUME(!upb_IsRepeatedOrMap(field));
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_StringView); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_StringView);
upb_StringView ret; upb_StringView ret;
_upb_MiniTable_GetNonExtensionField(msg, field, &def_val, &ret); _upb_Message_GetField(msg, field, &def_val, &ret);
return ret; return ret;
} }
UPB_API_INLINE void upb_MiniTable_SetString(upb_Message* msg, UPB_API_INLINE bool upb_Message_SetString(upb_Message* msg,
const upb_MiniTableField* field, const upb_MiniTableField* field,
upb_StringView value) { upb_StringView value, upb_Arena* a) {
UPB_ASSERT(field->descriptortype == kUpb_FieldType_Bytes || UPB_ASSERT(field->descriptortype == kUpb_FieldType_Bytes ||
field->descriptortype == kUpb_FieldType_String); field->descriptortype == kUpb_FieldType_String);
UPB_ASSUME(!upb_IsRepeatedOrMap(field)); UPB_ASSUME(!upb_IsRepeatedOrMap(field));
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_StringView); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_StringView);
_upb_MiniTable_SetNonExtensionField(msg, field, &value); return _upb_Message_SetField(msg, field, &value, a);
} }
UPB_API_INLINE const upb_Message* upb_MiniTable_GetMessage( UPB_API_INLINE const upb_Message* upb_MiniTable_GetMessage(
@ -463,7 +465,7 @@ UPB_API_INLINE const upb_Message* upb_MiniTable_GetMessage(
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == UPB_ASSUME(_upb_MiniTableField_GetRep(field) ==
UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte)); UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte));
upb_Message* ret; upb_Message* ret;
_upb_MiniTable_GetNonExtensionField(msg, field, &default_val, &ret); _upb_Message_GetNonExtensionField(msg, field, &default_val, &ret);
return ret; return ret;
} }
@ -477,7 +479,7 @@ UPB_API_INLINE void upb_MiniTable_SetMessage(upb_Message* msg,
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == UPB_ASSUME(_upb_MiniTableField_GetRep(field) ==
UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte)); UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte));
UPB_ASSERT(mini_table->subs[field->submsg_index].submsg); UPB_ASSERT(mini_table->subs[field->submsg_index].submsg);
_upb_MiniTable_SetNonExtensionField(msg, field, &sub_message); _upb_Message_SetNonExtensionField(msg, field, &sub_message);
} }
UPB_API_INLINE upb_Message* upb_MiniTable_GetMutableMessage( UPB_API_INLINE upb_Message* upb_MiniTable_GetMutableMessage(
@ -492,7 +494,7 @@ UPB_API_INLINE upb_Message* upb_MiniTable_GetMutableMessage(
UPB_ASSERT(sub_mini_table); UPB_ASSERT(sub_mini_table);
sub_message = _upb_Message_New(sub_mini_table, arena); sub_message = _upb_Message_New(sub_mini_table, arena);
*UPB_PTR_AT(msg, field->offset, upb_Message*) = sub_message; *UPB_PTR_AT(msg, field->offset, upb_Message*) = sub_message;
_upb_MiniTable_SetPresence(msg, field); _upb_Message_SetPresence(msg, field);
} }
return sub_message; return sub_message;
} }
@ -501,7 +503,7 @@ UPB_API_INLINE const upb_Array* upb_MiniTable_GetArray(
const upb_Message* msg, const upb_MiniTableField* field) { const upb_Message* msg, const upb_MiniTableField* field) {
const upb_Array* ret; const upb_Array* ret;
const upb_Array* default_val = NULL; const upb_Array* default_val = NULL;
_upb_MiniTable_GetNonExtensionField(msg, field, &default_val, &ret); _upb_Message_GetNonExtensionField(msg, field, &default_val, &ret);
return ret; return ret;
} }

@ -84,11 +84,11 @@ TEST(GeneratedCode, HazzersProto2) {
// Scalar/Boolean. // Scalar/Boolean.
const upb_MiniTableField* optional_bool_field = const upb_MiniTableField* optional_bool_field =
find_proto2_field(kFieldOptionalBool); find_proto2_field(kFieldOptionalBool);
EXPECT_EQ(false, upb_MiniTable_HasField(msg, optional_bool_field)); EXPECT_EQ(false, upb_Message_HasField(msg, optional_bool_field));
protobuf_test_messages_proto2_TestAllTypesProto2_set_optional_bool(msg, true); protobuf_test_messages_proto2_TestAllTypesProto2_set_optional_bool(msg, true);
EXPECT_EQ(true, upb_MiniTable_HasField(msg, optional_bool_field)); EXPECT_EQ(true, upb_Message_HasField(msg, optional_bool_field));
upb_MiniTable_ClearField(msg, optional_bool_field); upb_Message_ClearField(msg, optional_bool_field);
EXPECT_EQ(false, upb_MiniTable_HasField(msg, optional_bool_field)); EXPECT_EQ(false, upb_Message_HasField(msg, optional_bool_field));
EXPECT_EQ( EXPECT_EQ(
false, false,
protobuf_test_messages_proto2_TestAllTypesProto2_optional_bool(msg)); protobuf_test_messages_proto2_TestAllTypesProto2_optional_bool(msg));
@ -96,16 +96,16 @@ TEST(GeneratedCode, HazzersProto2) {
// String. // String.
const upb_MiniTableField* optional_string_field = const upb_MiniTableField* optional_string_field =
find_proto2_field(kFieldOptionalString); find_proto2_field(kFieldOptionalString);
EXPECT_EQ(false, upb_MiniTable_HasField(msg, optional_string_field)); EXPECT_EQ(false, upb_Message_HasField(msg, optional_string_field));
protobuf_test_messages_proto2_TestAllTypesProto2_set_optional_string( protobuf_test_messages_proto2_TestAllTypesProto2_set_optional_string(
msg, upb_StringView_FromString(kTestStr1)); msg, upb_StringView_FromString(kTestStr1));
EXPECT_EQ(true, upb_MiniTable_HasField(msg, optional_string_field)); EXPECT_EQ(true, upb_Message_HasField(msg, optional_string_field));
EXPECT_EQ( EXPECT_EQ(
strlen(kTestStr1), strlen(kTestStr1),
protobuf_test_messages_proto2_TestAllTypesProto2_optional_string(msg) protobuf_test_messages_proto2_TestAllTypesProto2_optional_string(msg)
.size); .size);
upb_MiniTable_ClearField(msg, optional_string_field); upb_Message_ClearField(msg, optional_string_field);
EXPECT_EQ(false, upb_MiniTable_HasField(msg, optional_string_field)); EXPECT_EQ(false, upb_Message_HasField(msg, optional_string_field));
EXPECT_EQ( EXPECT_EQ(
0, protobuf_test_messages_proto2_TestAllTypesProto2_optional_string(msg) 0, protobuf_test_messages_proto2_TestAllTypesProto2_optional_string(msg)
.size); .size);
@ -113,12 +113,12 @@ TEST(GeneratedCode, HazzersProto2) {
// Message. // Message.
const upb_MiniTableField* optional_message_field = const upb_MiniTableField* optional_message_field =
find_proto2_field(kFieldOptionalNestedMessage); find_proto2_field(kFieldOptionalNestedMessage);
EXPECT_EQ(false, upb_MiniTable_HasField(msg, optional_message_field)); EXPECT_EQ(false, upb_Message_HasField(msg, optional_message_field));
protobuf_test_messages_proto2_TestAllTypesProto2_mutable_optional_nested_message( protobuf_test_messages_proto2_TestAllTypesProto2_mutable_optional_nested_message(
msg, arena); msg, arena);
EXPECT_EQ(true, upb_MiniTable_HasField(msg, optional_message_field)); EXPECT_EQ(true, upb_Message_HasField(msg, optional_message_field));
upb_MiniTable_ClearField(msg, optional_message_field); upb_Message_ClearField(msg, optional_message_field);
EXPECT_EQ(false, upb_MiniTable_HasField(msg, optional_message_field)); EXPECT_EQ(false, upb_Message_HasField(msg, optional_message_field));
EXPECT_EQ( EXPECT_EQ(
true, true,
protobuf_test_messages_proto2_TestAllTypesProto2_optional_nested_message( protobuf_test_messages_proto2_TestAllTypesProto2_optional_nested_message(
@ -130,21 +130,21 @@ TEST(GeneratedCode, HazzersProto2) {
const upb_MiniTableField* optional_oneof_string_field = const upb_MiniTableField* optional_oneof_string_field =
find_proto2_field(kFieldOptionalOneOfString); find_proto2_field(kFieldOptionalOneOfString);
EXPECT_EQ(false, upb_MiniTable_HasField(msg, optional_oneof_uint32_field)); EXPECT_EQ(false, upb_Message_HasField(msg, optional_oneof_uint32_field));
EXPECT_EQ(false, upb_MiniTable_HasField(msg, optional_oneof_string_field)); EXPECT_EQ(false, upb_Message_HasField(msg, optional_oneof_string_field));
protobuf_test_messages_proto2_TestAllTypesProto2_set_oneof_uint32(msg, 123); protobuf_test_messages_proto2_TestAllTypesProto2_set_oneof_uint32(msg, 123);
EXPECT_EQ(true, upb_MiniTable_HasField(msg, optional_oneof_uint32_field)); EXPECT_EQ(true, upb_Message_HasField(msg, optional_oneof_uint32_field));
EXPECT_EQ(false, upb_MiniTable_HasField(msg, optional_oneof_string_field)); EXPECT_EQ(false, upb_Message_HasField(msg, optional_oneof_string_field));
protobuf_test_messages_proto2_TestAllTypesProto2_set_oneof_string( protobuf_test_messages_proto2_TestAllTypesProto2_set_oneof_string(
msg, upb_StringView_FromString(kTestStr1)); msg, upb_StringView_FromString(kTestStr1));
EXPECT_EQ(false, upb_MiniTable_HasField(msg, optional_oneof_uint32_field)); EXPECT_EQ(false, upb_Message_HasField(msg, optional_oneof_uint32_field));
EXPECT_EQ(true, upb_MiniTable_HasField(msg, optional_oneof_string_field)); EXPECT_EQ(true, upb_Message_HasField(msg, optional_oneof_string_field));
upb_MiniTable_ClearField(msg, optional_oneof_uint32_field); upb_Message_ClearField(msg, optional_oneof_uint32_field);
EXPECT_EQ(false, upb_MiniTable_HasField(msg, optional_oneof_uint32_field)); EXPECT_EQ(false, upb_Message_HasField(msg, optional_oneof_uint32_field));
EXPECT_EQ(true, upb_MiniTable_HasField(msg, optional_oneof_string_field)); EXPECT_EQ(true, upb_Message_HasField(msg, optional_oneof_string_field));
upb_MiniTable_ClearField(msg, optional_oneof_string_field); upb_Message_ClearField(msg, optional_oneof_string_field);
EXPECT_EQ(false, upb_MiniTable_HasField(msg, optional_oneof_uint32_field)); EXPECT_EQ(false, upb_Message_HasField(msg, optional_oneof_uint32_field));
EXPECT_EQ(false, upb_MiniTable_HasField(msg, optional_oneof_string_field)); EXPECT_EQ(false, upb_Message_HasField(msg, optional_oneof_string_field));
upb_Arena_Free(arena); upb_Arena_Free(arena);
} }
@ -160,10 +160,10 @@ TEST(GeneratedCode, ScalarsProto2) {
EXPECT_EQ( EXPECT_EQ(
0, protobuf_test_messages_proto2_TestAllTypesProto2_optional_int32(msg)); 0, protobuf_test_messages_proto2_TestAllTypesProto2_optional_int32(msg));
EXPECT_EQ(0, upb_MiniTable_GetInt32(msg, optional_int32_field, 0)); EXPECT_EQ(0, upb_Message_GetInt32(msg, optional_int32_field, 0));
upb_MiniTable_SetInt32(msg, optional_int32_field, kTestInt32); upb_Message_SetInt32(msg, optional_int32_field, kTestInt32, NULL);
EXPECT_EQ(true, upb_MiniTable_HasField(msg, optional_int32_field)); EXPECT_EQ(true, upb_Message_HasField(msg, optional_int32_field));
EXPECT_EQ(kTestInt32, upb_MiniTable_GetInt32(msg, optional_int32_field, 0)); EXPECT_EQ(kTestInt32, upb_Message_GetInt32(msg, optional_int32_field, 0));
EXPECT_EQ( EXPECT_EQ(
kTestInt32, kTestInt32,
protobuf_test_messages_proto2_TestAllTypesProto2_optional_int32(msg)); protobuf_test_messages_proto2_TestAllTypesProto2_optional_int32(msg));
@ -173,10 +173,9 @@ TEST(GeneratedCode, ScalarsProto2) {
EXPECT_EQ( EXPECT_EQ(
0, protobuf_test_messages_proto2_TestAllTypesProto2_optional_uint32(msg)); 0, protobuf_test_messages_proto2_TestAllTypesProto2_optional_uint32(msg));
EXPECT_EQ(0, upb_MiniTable_GetUInt32(msg, optional_uint32_field, 0)); EXPECT_EQ(0, upb_Message_GetUInt32(msg, optional_uint32_field, 0));
upb_MiniTable_SetUInt32(msg, optional_uint32_field, kTestUInt32); upb_Message_SetUInt32(msg, optional_uint32_field, kTestUInt32, NULL);
EXPECT_EQ(kTestUInt32, EXPECT_EQ(kTestUInt32, upb_Message_GetUInt32(msg, optional_uint32_field, 0));
upb_MiniTable_GetUInt32(msg, optional_uint32_field, 0));
EXPECT_EQ( EXPECT_EQ(
kTestUInt32, kTestUInt32,
protobuf_test_messages_proto2_TestAllTypesProto2_optional_uint32(msg)); protobuf_test_messages_proto2_TestAllTypesProto2_optional_uint32(msg));
@ -196,19 +195,18 @@ TEST(GeneratedCode, ScalarProto3) {
EXPECT_EQ( EXPECT_EQ(
0, protobuf_test_messages_proto3_TestAllTypesProto3_optional_int64(msg)); 0, protobuf_test_messages_proto3_TestAllTypesProto3_optional_int64(msg));
upb_MiniTable_SetInt64(msg, optional_int64_field, -1); upb_Message_SetInt64(msg, optional_int64_field, -1, NULL);
EXPECT_EQ( EXPECT_EQ(
-1, protobuf_test_messages_proto3_TestAllTypesProto3_optional_int64(msg)); -1, protobuf_test_messages_proto3_TestAllTypesProto3_optional_int64(msg));
EXPECT_EQ(-1, upb_MiniTable_GetInt64(msg, optional_int64_field, 0)); EXPECT_EQ(-1, upb_Message_GetInt64(msg, optional_int64_field, 0));
EXPECT_EQ( EXPECT_EQ(
0, protobuf_test_messages_proto3_TestAllTypesProto3_optional_uint64(msg)); 0, protobuf_test_messages_proto3_TestAllTypesProto3_optional_uint64(msg));
upb_MiniTable_SetUInt64(msg, optional_uint64_field, kTestUInt64); upb_Message_SetUInt64(msg, optional_uint64_field, kTestUInt64, NULL);
EXPECT_EQ( EXPECT_EQ(
kTestUInt64, kTestUInt64,
protobuf_test_messages_proto3_TestAllTypesProto3_optional_uint64(msg)); protobuf_test_messages_proto3_TestAllTypesProto3_optional_uint64(msg));
EXPECT_EQ(kTestUInt64, EXPECT_EQ(kTestUInt64, upb_Message_GetUInt64(msg, optional_uint64_field, 0));
upb_MiniTable_GetUInt64(msg, optional_uint64_field, 0));
upb_Arena_Free(arena); upb_Arena_Free(arena);
} }
@ -222,25 +220,25 @@ TEST(GeneratedCode, Strings) {
find_proto2_field(kFieldOptionalString); find_proto2_field(kFieldOptionalString);
// Test default. // Test default.
EXPECT_EQ(false, upb_MiniTable_HasField(msg, optional_string_field)); EXPECT_EQ(false, upb_Message_HasField(msg, optional_string_field));
// Test read after write using C. // Test read after write using C.
protobuf_test_messages_proto2_TestAllTypesProto2_set_optional_string( protobuf_test_messages_proto2_TestAllTypesProto2_set_optional_string(
msg, upb_StringView_FromString(kTestStr1)); msg, upb_StringView_FromString(kTestStr1));
EXPECT_EQ(true, upb_MiniTable_HasField(msg, optional_string_field)); EXPECT_EQ(true, upb_Message_HasField(msg, optional_string_field));
upb_StringView value = upb_MiniTable_GetString(msg, optional_string_field, upb_StringView value = upb_Message_GetString(msg, optional_string_field,
upb_StringView{NULL, 0}); upb_StringView{NULL, 0});
std::string read_value = std::string(value.data, value.size); std::string read_value = std::string(value.data, value.size);
EXPECT_EQ(kTestStr1, read_value); EXPECT_EQ(kTestStr1, read_value);
// Clear. // Clear.
upb_MiniTable_ClearField(msg, optional_string_field); upb_Message_ClearField(msg, optional_string_field);
EXPECT_EQ(false, upb_MiniTable_HasField(msg, optional_string_field)); EXPECT_EQ(false, upb_Message_HasField(msg, optional_string_field));
EXPECT_EQ( EXPECT_EQ(
false, false,
protobuf_test_messages_proto2_TestAllTypesProto2_has_optional_string( protobuf_test_messages_proto2_TestAllTypesProto2_has_optional_string(
msg)); msg));
upb_MiniTable_SetString(msg, optional_string_field, upb_Message_SetString(msg, optional_string_field,
upb_StringView_FromString(kTestStr2)); upb_StringView_FromString(kTestStr2), NULL);
EXPECT_EQ(true, upb_MiniTable_HasField(msg, optional_string_field)); EXPECT_EQ(true, upb_Message_HasField(msg, optional_string_field));
EXPECT_EQ( EXPECT_EQ(
true, true,
protobuf_test_messages_proto2_TestAllTypesProto2_has_optional_string( protobuf_test_messages_proto2_TestAllTypesProto2_has_optional_string(
@ -264,14 +262,14 @@ TEST(GeneratedCode, SubMessage) {
upb_MiniTable_GetMessage(msg, optional_message_field, NULL); upb_MiniTable_GetMessage(msg, optional_message_field, NULL);
EXPECT_EQ(NULL, test_message); EXPECT_EQ(NULL, test_message);
EXPECT_EQ(false, upb_MiniTable_HasField(msg, optional_message_field)); EXPECT_EQ(false, upb_Message_HasField(msg, optional_message_field));
// Get mutable using C API. // Get mutable using C API.
protobuf_test_messages_proto2_TestAllTypesProto2_NestedMessage* nested_message = protobuf_test_messages_proto2_TestAllTypesProto2_NestedMessage* nested_message =
protobuf_test_messages_proto2_TestAllTypesProto2_mutable_optional_nested_message( protobuf_test_messages_proto2_TestAllTypesProto2_mutable_optional_nested_message(
msg, arena); msg, arena);
EXPECT_EQ(true, nested_message != nullptr); EXPECT_EQ(true, nested_message != nullptr);
EXPECT_EQ(true, upb_MiniTable_HasField(msg, optional_message_field)); EXPECT_EQ(true, upb_Message_HasField(msg, optional_message_field));
protobuf_test_messages_proto2_TestAllTypesProto2_NestedMessage_set_a( protobuf_test_messages_proto2_TestAllTypesProto2_NestedMessage_set_a(
nested_message, 5); nested_message, 5);
@ -284,18 +282,18 @@ TEST(GeneratedCode, SubMessage) {
upb_MiniTable_FindFieldByNumber( upb_MiniTable_FindFieldByNumber(
&protobuf_test_messages_proto2_TestAllTypesProto2_NestedMessage_msg_init, &protobuf_test_messages_proto2_TestAllTypesProto2_NestedMessage_msg_init,
kFieldOptionalNestedMessageA); kFieldOptionalNestedMessageA);
EXPECT_EQ(5, upb_MiniTable_GetInt32(sub_message, nested_message_a_field, 0)); EXPECT_EQ(5, upb_Message_GetInt32(sub_message, nested_message_a_field, 0));
upb_MiniTable_ClearField(msg, optional_message_field); upb_Message_ClearField(msg, optional_message_field);
EXPECT_EQ( EXPECT_EQ(
NULL, NULL,
protobuf_test_messages_proto2_TestAllTypesProto2_optional_nested_message( protobuf_test_messages_proto2_TestAllTypesProto2_optional_nested_message(
msg)); msg));
EXPECT_EQ(false, upb_MiniTable_HasField(msg, optional_message_field)); EXPECT_EQ(false, upb_Message_HasField(msg, optional_message_field));
upb_Message* new_nested_message = upb_Message* new_nested_message =
protobuf_test_messages_proto2_TestAllTypesProto2_NestedMessage_new(arena); protobuf_test_messages_proto2_TestAllTypesProto2_NestedMessage_new(arena);
upb_MiniTable_SetInt32(new_nested_message, nested_message_a_field, 123); upb_Message_SetInt32(new_nested_message, nested_message_a_field, 123, NULL);
upb_MiniTable_SetMessage( upb_MiniTable_SetMessage(
msg, &protobuf_test_messages_proto2_TestAllTypesProto2_msg_init, msg, &protobuf_test_messages_proto2_TestAllTypesProto2_msg_init,
optional_message_field, new_nested_message); optional_message_field, new_nested_message);
@ -307,9 +305,9 @@ TEST(GeneratedCode, SubMessage) {
true, true,
protobuf_test_messages_proto2_TestAllTypesProto2_optional_nested_message( protobuf_test_messages_proto2_TestAllTypesProto2_optional_nested_message(
msg) != NULL); msg) != NULL);
EXPECT_EQ(true, upb_MiniTable_HasField(msg, optional_message_field)); EXPECT_EQ(true, upb_Message_HasField(msg, optional_message_field));
EXPECT_EQ(123, EXPECT_EQ(123,
upb_MiniTable_GetInt32(mutable_message, nested_message_a_field, 0)); upb_Message_GetInt32(mutable_message, nested_message_a_field, 0));
upb_Arena_Free(arena); upb_Arena_Free(arena);
} }
@ -600,7 +598,7 @@ TEST(GeneratedCode, PromoteUnknownMessage) {
upb_DecodeStatus decode_status = upb_Decode(serialized, serialized_size, msg, upb_DecodeStatus decode_status = upb_Decode(serialized, serialized_size, msg,
mini_table, nullptr, 0, arena); mini_table, nullptr, 0, arena);
EXPECT_EQ(decode_status, kUpb_DecodeStatus_Ok); EXPECT_EQ(decode_status, kUpb_DecodeStatus_Ok);
int32_t val = upb_MiniTable_GetInt32( int32_t val = upb_Message_GetInt32(
msg, upb_MiniTable_FindFieldByNumber(mini_table, 4), 0); msg, upb_MiniTable_FindFieldByNumber(mini_table, 4), 0);
EXPECT_EQ(val, 11); EXPECT_EQ(val, 11);
upb_FindUnknownRet unknown = upb_MiniTable_FindUnknown(msg, 5); upb_FindUnknownRet unknown = upb_MiniTable_FindUnknown(msg, 5);

@ -74,8 +74,6 @@ const upb_Message_Extension* _upb_Message_Getexts(const upb_Message* msg,
const upb_Message_Extension* _upb_Message_Getext( const upb_Message_Extension* _upb_Message_Getext(
const upb_Message* msg, const upb_MiniTableExtension* ext); const upb_Message* msg, const upb_MiniTableExtension* ext);
void _upb_Message_Clearext(upb_Message* msg, const upb_MiniTableExtension* ext);
#ifdef __cplusplus #ifdef __cplusplus
} /* extern "C" */ } /* extern "C" */
#endif #endif

@ -159,20 +159,6 @@ const upb_Message_Extension* _upb_Message_Getext(
return NULL; return NULL;
} }
void _upb_Message_Clearext(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, void);
upb_Message_Extension* ext =
(upb_Message_Extension*)_upb_Message_Getext(msg, ext_l);
if (ext) {
*ext = *base;
in->internal->ext_begin += sizeof(upb_Message_Extension);
}
}
upb_Message_Extension* _upb_Message_GetOrCreateExtension( upb_Message_Extension* _upb_Message_GetOrCreateExtension(
upb_Message* msg, const upb_MiniTableExtension* e, upb_Arena* arena) { upb_Message* msg, const upb_MiniTableExtension* e, upb_Arena* arena) {
upb_Message_Extension* ext = upb_Message_Extension* ext =

@ -46,7 +46,7 @@
bool upb_Message_HasFieldByDef(const upb_Message* msg, const upb_FieldDef* f) { bool upb_Message_HasFieldByDef(const upb_Message* msg, const upb_FieldDef* f) {
UPB_ASSERT(upb_FieldDef_HasPresence(f)); UPB_ASSERT(upb_FieldDef_HasPresence(f));
return _upb_MiniTable_HasField(msg, upb_FieldDef_MiniTable(f)); return upb_Message_HasField(msg, upb_FieldDef_MiniTable(f));
} }
const upb_FieldDef* upb_Message_WhichOneof(const upb_Message* msg, const upb_FieldDef* upb_Message_WhichOneof(const upb_Message* msg,
@ -68,7 +68,7 @@ upb_MessageValue upb_Message_GetFieldByDef(const upb_Message* msg,
const upb_FieldDef* f) { const upb_FieldDef* f) {
upb_MessageValue default_val = upb_FieldDef_Default(f); upb_MessageValue default_val = upb_FieldDef_Default(f);
upb_MessageValue ret; upb_MessageValue ret;
_upb_MiniTable_GetField(msg, upb_FieldDef_MiniTable(f), &default_val, &ret); _upb_Message_GetField(msg, upb_FieldDef_MiniTable(f), &default_val, &ret);
return ret; return ret;
} }
@ -113,11 +113,11 @@ make:
bool upb_Message_SetFieldByDef(upb_Message* msg, const upb_FieldDef* f, bool upb_Message_SetFieldByDef(upb_Message* msg, const upb_FieldDef* f,
upb_MessageValue val, upb_Arena* a) { upb_MessageValue val, upb_Arena* a) {
return _upb_MiniTable_SetField(msg, upb_FieldDef_MiniTable(f), &val, a); return _upb_Message_SetField(msg, upb_FieldDef_MiniTable(f), &val, a);
} }
void upb_Message_ClearFieldByDef(upb_Message* msg, const upb_FieldDef* f) { void upb_Message_ClearFieldByDef(upb_Message* msg, const upb_FieldDef* f) {
_upb_MiniTable_ClearField(msg, upb_FieldDef_MiniTable(f)); upb_Message_ClearField(msg, upb_FieldDef_MiniTable(f));
} }
void upb_Message_ClearByDef(upb_Message* msg, const upb_MessageDef* m) { void upb_Message_ClearByDef(upb_Message* msg, const upb_MessageDef* m) {

@ -259,7 +259,7 @@ void GenerateExtensionInHeader(const protobuf::FieldDescriptor* ext,
output( output(
R"cc( R"cc(
UPB_INLINE bool $0_has_$1(const struct $2* msg) { UPB_INLINE bool $0_has_$1(const struct $2* msg) {
return _upb_MiniTable_HasExtensionField(msg, &$3); return _upb_Message_HasExtensionField(msg, &$3);
} }
)cc", )cc",
ExtensionIdentBase(ext), ext->name(), MessageName(ext->containing_type()), ExtensionIdentBase(ext), ext->name(), MessageName(ext->containing_type()),
@ -268,7 +268,7 @@ void GenerateExtensionInHeader(const protobuf::FieldDescriptor* ext,
output( output(
R"cc( R"cc(
UPB_INLINE void $0_clear_$1(struct $2* msg) { UPB_INLINE void $0_clear_$1(struct $2* msg) {
_upb_MiniTable_ClearExtensionField(msg, &$3); _upb_Message_ClearExtensionField(msg, &$3);
} }
)cc", )cc",
ExtensionIdentBase(ext), ext->name(), MessageName(ext->containing_type()), ExtensionIdentBase(ext), ext->name(), MessageName(ext->containing_type()),
@ -285,7 +285,7 @@ void GenerateExtensionInHeader(const protobuf::FieldDescriptor* ext,
UPB_ASSUME(_upb_MiniTableField_GetRep(&ext->field) == $5); UPB_ASSUME(_upb_MiniTableField_GetRep(&ext->field) == $5);
$0 default_val = $6; $0 default_val = $6;
$0 ret; $0 ret;
_upb_MiniTable_GetExtensionField(msg, ext, &default_val, &ret); _upb_Message_GetExtensionField(msg, ext, &default_val, &ret);
return ret; return ret;
} }
)cc", )cc",
@ -298,7 +298,7 @@ void GenerateExtensionInHeader(const protobuf::FieldDescriptor* ext,
const upb_MiniTableExtension* ext = &$4; const upb_MiniTableExtension* ext = &$4;
UPB_ASSUME(!upb_IsRepeatedOrMap(&ext->field)); UPB_ASSUME(!upb_IsRepeatedOrMap(&ext->field));
UPB_ASSUME(_upb_MiniTableField_GetRep(&ext->field) == $5); UPB_ASSUME(_upb_MiniTableField_GetRep(&ext->field) == $5);
bool ok = _upb_MiniTable_SetExtensionField(msg, ext, &val, arena); bool ok = _upb_Message_SetExtensionField(msg, ext, &val, arena);
UPB_ASSERT(ok); UPB_ASSERT(ok);
} }
)cc", )cc",
@ -383,7 +383,7 @@ void GenerateHazzer(const protobuf::FieldDescriptor* field,
R"cc( R"cc(
UPB_INLINE bool $0_has_$1(const $0* msg) { UPB_INLINE bool $0_has_$1(const $0* msg) {
const upb_MiniTableField field = $2; const upb_MiniTableField field = $2;
return _upb_MiniTable_HasNonExtensionField(msg, &field); return _upb_Message_HasNonExtensionField(msg, &field);
} }
)cc", )cc",
msg_name, resolved_name, FieldInitializer(layout, field)); msg_name, resolved_name, FieldInitializer(layout, field));
@ -424,7 +424,7 @@ void GenerateClear(const protobuf::FieldDescriptor* field,
R"cc( R"cc(
UPB_INLINE void $0_clear_$1($0* msg) { UPB_INLINE void $0_clear_$1($0* msg) {
const upb_MiniTableField field = $2; const upb_MiniTableField field = $2;
_upb_MiniTable_ClearNonExtensionField(msg, &field); _upb_Message_ClearNonExtensionField(msg, &field);
} }
)cc", )cc",
msg_name, resolved_name, FieldInitializer(layout, field)); msg_name, resolved_name, FieldInitializer(layout, field));
@ -510,7 +510,7 @@ void GenerateScalarGetters(const protobuf::FieldDescriptor* field,
$0 default_val = $3; $0 default_val = $3;
$0 ret; $0 ret;
const upb_MiniTableField field = $4; const upb_MiniTableField field = $4;
_upb_MiniTable_GetNonExtensionField(msg, &field, &default_val, &ret); _upb_Message_GetNonExtensionField(msg, &field, &default_val, &ret);
return ret; return ret;
} }
)cc", )cc",
@ -650,7 +650,7 @@ void GenerateNonRepeatedSetters(const protobuf::FieldDescriptor* field,
output(R"cc( output(R"cc(
UPB_INLINE void $0_set_$1($0 *msg, $2 value) { UPB_INLINE void $0_set_$1($0 *msg, $2 value) {
const upb_MiniTableField field = $3; const upb_MiniTableField field = $3;
_upb_MiniTable_SetNonExtensionField(msg, &field, &value); _upb_Message_SetNonExtensionField(msg, &field, &value);
})cc", })cc",
msg_name, field_name, CType(field), FieldInitializer(layout, field)); msg_name, field_name, CType(field), FieldInitializer(layout, field));
} }

Loading…
Cancel
Save