upb: clean up message/accessors.h and message/internal/accessors.h

PiperOrigin-RevId: 630466865
pull/16719/head
Eric Salo 7 months ago committed by Copybara-Service
parent a91d76bb8a
commit aa70a3576b
  1. 406
      upb/message/accessors.h
  2. 4
      upb/message/copy.c
  3. 392
      upb/message/internal/accessors.h

@ -68,263 +68,100 @@ UPB_API_INLINE bool upb_Message_SetExtension(upb_Message* msg,
const upb_MiniTableExtension* e,
const void* val, upb_Arena* a);
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_PRIVATE(_upb_Message_GetOneofCase)(message, oneof_field);
}
// NOTE: The default_val is only used for fields that support presence.
// For repeated/map fields, the resulting upb_Array*/upb_Map* can be NULL if a
// upb_Array/upb_Map has not been allocated yet. Array/map fields do not have
// presence, so this is semantically identical to a pointer to an empty
// array/map, and must be treated the same for all semantic purposes.
UPB_INLINE upb_MessageValue
upb_Message_GetField(const upb_Message* msg, const upb_MiniTableField* field,
upb_MessageValue default_val) {
upb_MessageValue ret;
if (upb_MiniTableField_IsExtension(field)) {
_upb_Message_GetExtensionField(msg, (upb_MiniTableExtension*)field,
&default_val, &ret);
} else {
_upb_Message_GetNonExtensionField(msg, field, &default_val, &ret);
}
return ret;
}
UPB_API_INLINE upb_MessageValue
upb_Message_GetField(const upb_Message* msg, const upb_MiniTableField* f,
upb_MessageValue default_val);
// Sets the value of the given field in the given msg. The return value is true
// if the operation completed successfully, or false if memory allocation
// failed.
UPB_INLINE bool UPB_PRIVATE(_upb_Message_SetField)(
upb_Message* msg, const upb_MiniTableField* field, upb_MessageValue val,
upb_Arena* a) {
if (upb_MiniTableField_IsExtension(field)) {
const upb_MiniTableExtension* ext = (const upb_MiniTableExtension*)field;
return upb_Message_SetExtension(msg, ext, &val, a);
} else {
upb_Message_SetBaseField(msg, field, &val);
return true;
}
}
UPB_API_INLINE const upb_Array* upb_Message_GetArray(
const upb_Message* msg, const upb_MiniTableField* f);
UPB_API_INLINE bool upb_Message_GetBool(const upb_Message* msg,
const upb_MiniTableField* field,
bool default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Bool);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(field) ==
kUpb_FieldRep_1Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(field));
upb_MessageValue def;
def.bool_val = default_val;
return upb_Message_GetField(msg, field, def).bool_val;
}
const upb_MiniTableField* f,
bool default_val);
UPB_API_INLINE bool upb_Message_SetBool(upb_Message* msg,
UPB_API_INLINE double upb_Message_GetDouble(const upb_Message* msg,
const upb_MiniTableField* field,
bool value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Bool);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(field) ==
kUpb_FieldRep_1Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(field));
upb_MessageValue val;
val.bool_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, field, val, a);
}
double default_val);
UPB_API_INLINE float upb_Message_GetFloat(const upb_Message* msg,
const upb_MiniTableField* f,
float default_val);
UPB_API_INLINE int32_t upb_Message_GetInt32(const upb_Message* msg,
const upb_MiniTableField* field,
int32_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Int32 ||
upb_MiniTableField_CType(field) == kUpb_CType_Enum);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(field) ==
kUpb_FieldRep_4Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(field));
const upb_MiniTableField* f,
int32_t default_val);
upb_MessageValue def;
def.int32_val = default_val;
return upb_Message_GetField(msg, field, def).int32_val;
}
UPB_API_INLINE int64_t upb_Message_GetInt64(const upb_Message* msg,
const upb_MiniTableField* f,
int64_t default_val);
UPB_API_INLINE bool upb_Message_SetInt32(upb_Message* msg,
const upb_MiniTableField* field,
int32_t value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Int32 ||
upb_MiniTableField_CType(field) == kUpb_CType_Enum);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(field) ==
kUpb_FieldRep_4Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(field));
upb_MessageValue val;
val.int32_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, field, val, a);
}
UPB_API_INLINE const upb_Map* upb_Message_GetMap(const upb_Message* msg,
const upb_MiniTableField* f);
UPB_API_INLINE uint32_t upb_Message_GetUInt32(const upb_Message* msg,
const upb_MiniTableField* field,
uint32_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_UInt32);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(field) ==
kUpb_FieldRep_4Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(field));
UPB_API_INLINE upb_Array* upb_Message_GetMutableArray(
upb_Message* msg, const upb_MiniTableField* f);
upb_MessageValue def;
def.uint32_val = default_val;
return upb_Message_GetField(msg, field, def).uint32_val;
}
UPB_API_INLINE upb_Map* upb_Message_GetMutableMap(upb_Message* msg,
const upb_MiniTableField* f);
UPB_API_INLINE bool upb_Message_SetUInt32(upb_Message* msg,
const upb_MiniTableField* field,
uint32_t value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_UInt32);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(field) ==
kUpb_FieldRep_4Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(field));
upb_MessageValue val;
val.uint32_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, field, val, a);
}
UPB_API_INLINE upb_Array* upb_Message_GetOrCreateMutableArray(
upb_Message* msg, const upb_MiniTableField* f, upb_Arena* arena);
UPB_API_INLINE void upb_Message_SetClosedEnum(
upb_Message* msg, const upb_MiniTable* msg_mini_table,
const upb_MiniTableField* field, int32_t value) {
UPB_ASSERT(upb_MiniTableField_IsClosedEnum(field));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(field) ==
kUpb_FieldRep_4Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(field));
UPB_ASSERT(upb_MiniTableEnum_CheckValue(
upb_MiniTable_GetSubEnumTable(msg_mini_table, field), value));
upb_Message_SetBaseField(msg, field, &value);
}
UPB_API_INLINE upb_Map* upb_Message_GetOrCreateMutableMap(
upb_Message* msg, const upb_MiniTable* map_entry_mini_table,
const upb_MiniTableField* f, upb_Arena* arena);
UPB_API_INLINE int64_t upb_Message_GetInt64(const upb_Message* msg,
const upb_MiniTableField* field,
int64_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Int64);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(field) ==
kUpb_FieldRep_8Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(field));
UPB_API_INLINE upb_Message* upb_Message_GetOrCreateMutableMessage(
upb_Message* msg, const upb_MiniTable* mini_table,
const upb_MiniTableField* f, upb_Arena* arena);
upb_MessageValue def;
def.int64_val = default_val;
return upb_Message_GetField(msg, field, def).int64_val;
}
UPB_API_INLINE upb_StringView
upb_Message_GetString(const upb_Message* msg, const upb_MiniTableField* field,
upb_StringView default_val);
UPB_API_INLINE bool upb_Message_SetInt64(upb_Message* msg,
const upb_MiniTableField* field,
int64_t value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Int64);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(field) ==
kUpb_FieldRep_8Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(field));
upb_MessageValue val;
val.int64_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, field, val, a);
}
UPB_API_INLINE uint32_t upb_Message_GetUInt32(const upb_Message* msg,
const upb_MiniTableField* f,
uint32_t default_val);
UPB_API_INLINE uint64_t upb_Message_GetUInt64(const upb_Message* msg,
const upb_MiniTableField* field,
uint64_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_UInt64);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(field) ==
kUpb_FieldRep_8Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(field));
upb_MessageValue def;
def.uint64_val = default_val;
return upb_Message_GetField(msg, field, def).uint64_val;
}
const upb_MiniTableField* f,
uint64_t default_val);
UPB_API_INLINE bool upb_Message_SetUInt64(upb_Message* msg,
const upb_MiniTableField* field,
uint64_t value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_UInt64);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(field) ==
kUpb_FieldRep_8Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(field));
upb_MessageValue val;
val.uint64_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, field, val, a);
}
UPB_API_INLINE bool upb_Message_SetBool(upb_Message* msg,
const upb_MiniTableField* f, bool value,
upb_Arena* a);
UPB_API_INLINE float upb_Message_GetFloat(const upb_Message* msg,
const upb_MiniTableField* field,
float default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Float);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(field) ==
kUpb_FieldRep_4Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(field));
UPB_API_INLINE void upb_Message_SetClosedEnum(
upb_Message* msg, const upb_MiniTable* msg_mini_table,
const upb_MiniTableField* f, int32_t value);
upb_MessageValue def;
def.float_val = default_val;
return upb_Message_GetField(msg, field, def).float_val;
}
UPB_API_INLINE bool upb_Message_SetDouble(upb_Message* msg,
const upb_MiniTableField* f,
double value, upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetFloat(upb_Message* msg,
const upb_MiniTableField* field,
float value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Float);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(field) ==
kUpb_FieldRep_4Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(field));
upb_MessageValue val;
val.float_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, field, val, a);
}
UPB_API_INLINE double upb_Message_GetDouble(const upb_Message* msg,
const upb_MiniTableField* field,
double default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Double);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(field) ==
kUpb_FieldRep_8Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(field));
const upb_MiniTableField* f,
float value, upb_Arena* a);
upb_MessageValue def;
def.double_val = default_val;
return upb_Message_GetField(msg, field, def).double_val;
}
UPB_API_INLINE bool upb_Message_SetInt32(upb_Message* msg,
const upb_MiniTableField* f,
int32_t value, upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetDouble(upb_Message* msg,
const upb_MiniTableField* field,
double value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Double);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(field) ==
kUpb_FieldRep_8Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(field));
upb_MessageValue val;
val.double_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, field, val, a);
}
UPB_API_INLINE bool upb_Message_SetInt64(upb_Message* msg,
const upb_MiniTableField* f,
int64_t value, upb_Arena* a);
UPB_API_INLINE upb_StringView
upb_Message_GetString(const upb_Message* msg, const upb_MiniTableField* field,
upb_StringView default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_String ||
upb_MiniTableField_CType(field) == kUpb_CType_Bytes);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(field) ==
kUpb_FieldRep_StringView);
UPB_ASSUME(upb_MiniTableField_IsScalar(field));
UPB_API_INLINE bool upb_Message_SetString(upb_Message* msg,
const upb_MiniTableField* f,
upb_StringView value, upb_Arena* a);
upb_MessageValue def;
def.str_val = default_val;
return upb_Message_GetField(msg, field, def).str_val;
}
UPB_API_INLINE bool upb_Message_SetUInt32(upb_Message* msg,
const upb_MiniTableField* f,
uint32_t value, upb_Arena* a);
// Sets the value of a `string` or `bytes` field. The bytes of the value are not
// copied, so it is the caller's responsibility to ensure that they remain valid
// for the lifetime of `msg`. That might be done by copying them into the given
// arena, or by fusing that arena with the arena the bytes live in, for example.
UPB_API_INLINE bool upb_Message_SetString(upb_Message* msg,
const upb_MiniTableField* field,
upb_StringView value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_String ||
upb_MiniTableField_CType(field) == kUpb_CType_Bytes);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(field) ==
kUpb_FieldRep_StringView);
UPB_ASSUME(upb_MiniTableField_IsScalar(field));
upb_MessageValue val;
val.str_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, field, val, a);
}
UPB_API_INLINE bool upb_Message_SetUInt64(upb_Message* msg,
const upb_MiniTableField* f,
uint64_t value, upb_Arena* a);
UPB_API_INLINE upb_TaggedMessagePtr upb_Message_GetTaggedMessagePtr(
const upb_Message* msg, const upb_MiniTableField* field,
@ -353,14 +190,14 @@ UPB_API_INLINE upb_Message* upb_Message_GetMutableMessage(
// For internal use only; users cannot set tagged messages because only the
// parser and the message copier are allowed to directly create an empty
// message.
UPB_API_INLINE void _upb_Message_SetTaggedMessagePtr(
upb_Message* msg, const upb_MiniTable* mini_table,
const upb_MiniTableField* field, upb_TaggedMessagePtr sub_message) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Message);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(field) ==
UPB_INLINE void UPB_PRIVATE(_upb_Message_SetTaggedMessagePtr)(
struct upb_Message* msg, const upb_MiniTable* mini_table,
const upb_MiniTableField* f, upb_TaggedMessagePtr sub_message) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte));
UPB_ASSUME(upb_MiniTableField_IsScalar(field));
upb_Message_SetBaseField(msg, field, &sub_message);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_Message_SetBaseField(msg, f, &sub_message);
}
// Sets the value of a message-typed field. The `mini_table` and `field`
@ -370,102 +207,17 @@ UPB_API_INLINE void upb_Message_SetMessage(upb_Message* msg,
const upb_MiniTable* mini_table,
const upb_MiniTableField* field,
upb_Message* sub_message) {
_upb_Message_SetTaggedMessagePtr(
msg, mini_table, field,
UPB_PRIVATE(_upb_Message_SetTaggedMessagePtr)
(msg, mini_table, field,
UPB_PRIVATE(_upb_TaggedMessagePtr_Pack)(sub_message, false));
}
UPB_API_INLINE upb_Message* upb_Message_GetOrCreateMutableMessage(
upb_Message* msg, const upb_MiniTable* mini_table,
const upb_MiniTableField* field, upb_Arena* arena) {
UPB_ASSERT(arena);
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Message);
UPB_ASSUME(!upb_MiniTableField_IsExtension(field));
upb_Message* sub_message =
*UPB_PTR_AT(msg, field->UPB_ONLYBITS(offset), upb_Message*);
if (!sub_message) {
const upb_MiniTable* sub_mini_table =
upb_MiniTable_SubMessage(mini_table, field);
UPB_ASSERT(sub_mini_table);
sub_message = _upb_Message_New(sub_mini_table, arena);
*UPB_PTR_AT(msg, field->UPB_ONLYBITS(offset), upb_Message*) = sub_message;
UPB_PRIVATE(_upb_Message_SetPresence)(msg, field);
}
return sub_message;
}
UPB_API_INLINE const upb_Array* upb_Message_GetArray(
const upb_Message* msg, const upb_MiniTableField* field) {
UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(field);
upb_Array* ret;
const upb_Array* default_val = NULL;
_upb_Message_GetNonExtensionField(msg, field, &default_val, &ret);
return ret;
}
UPB_API_INLINE upb_Array* upb_Message_GetMutableArray(
upb_Message* msg, const upb_MiniTableField* field) {
UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(field);
return (upb_Array*)upb_Message_GetArray(msg, field);
}
UPB_API_INLINE upb_Array* upb_Message_GetOrCreateMutableArray(
upb_Message* msg, const upb_MiniTableField* field, upb_Arena* arena) {
UPB_ASSERT(arena);
UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(field);
upb_Array* array = upb_Message_GetMutableArray(msg, field);
if (!array) {
array = UPB_PRIVATE(_upb_Array_New)(
arena, 4, UPB_PRIVATE(_upb_MiniTableField_ElemSizeLg2)(field));
// Check again due to: https://godbolt.org/z/7WfaoKG1r
UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(field);
upb_MessageValue val;
val.array_val = array;
UPB_PRIVATE(_upb_Message_SetField)(msg, field, val, arena);
}
return array;
}
UPB_API_INLINE void* upb_Message_ResizeArrayUninitialized(
upb_Message* msg, const upb_MiniTableField* field, size_t size,
upb_Arena* arena) {
UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(field);
upb_Array* arr = upb_Message_GetOrCreateMutableArray(msg, field, arena);
if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)(arr, size, arena)) {
return NULL;
}
return upb_Array_MutableDataPtr(arr);
}
UPB_API_INLINE const upb_Map* upb_Message_GetMap(
const upb_Message* msg, const upb_MiniTableField* field) {
UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(field);
_upb_Message_AssertMapIsUntagged(msg, field);
upb_Map* ret;
const upb_Map* default_val = NULL;
_upb_Message_GetNonExtensionField(msg, field, &default_val, &ret);
return ret;
}
UPB_API_INLINE upb_Map* upb_Message_GetMutableMap(
upb_Message* msg, const upb_MiniTableField* field) {
return (upb_Map*)upb_Message_GetMap(msg, field);
}
upb_Message* msg, const upb_MiniTableField* f, size_t size,
upb_Arena* arena);
UPB_API_INLINE upb_Map* upb_Message_GetOrCreateMutableMap(
upb_Message* msg, const upb_MiniTable* map_entry_mini_table,
const upb_MiniTableField* field, upb_Arena* arena) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Message);
const upb_MiniTableField* map_entry_key_field =
&map_entry_mini_table->UPB_ONLYBITS(fields)[0];
const upb_MiniTableField* map_entry_value_field =
&map_entry_mini_table->UPB_ONLYBITS(fields)[1];
return _upb_Message_GetOrCreateMutableMap(
msg, field,
_upb_Map_CTypeSize(upb_MiniTableField_CType(map_entry_key_field)),
_upb_Map_CTypeSize(upb_MiniTableField_CType(map_entry_value_field)),
arena);
}
UPB_API_INLINE uint32_t upb_Message_WhichOneofFieldNumber(
const upb_Message* message, const upb_MiniTableField* oneof_field);
// Updates a map entry given an entry message.
bool upb_Message_SetMapEntry(upb_Map* map, const upb_MiniTable* mini_table,

@ -214,8 +214,8 @@ upb_Message* _upb_Message_Copy(upb_Message* dst, const upb_Message* src,
if (dst_sub_message == NULL) {
return NULL;
}
_upb_Message_SetTaggedMessagePtr(
dst, mini_table, field,
UPB_PRIVATE(_upb_Message_SetTaggedMessagePtr)
(dst, mini_table, field,
UPB_PRIVATE(_upb_TaggedMessagePtr_Pack)(dst_sub_message,
is_empty));
}

@ -12,14 +12,18 @@
#include <stdint.h>
#include <string.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/internal/endian.h"
#include "upb/base/string_view.h"
#include "upb/mem/arena.h"
#include "upb/message/internal/array.h"
#include "upb/message/internal/extension.h"
#include "upb/message/internal/map.h"
#include "upb/message/internal/message.h"
#include "upb/message/internal/tagged_ptr.h"
#include "upb/message/internal/types.h"
#include "upb/message/value.h"
#include "upb/mini_table/enum.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/internal/field.h"
@ -112,6 +116,12 @@ UPB_INLINE bool UPB_PRIVATE(_upb_Message_ClearOneofCase)(
return true;
}
UPB_API_INLINE uint32_t upb_Message_WhichOneofFieldNumber(
const struct upb_Message* message, const upb_MiniTableField* oneof_field) {
UPB_ASSUME(upb_MiniTableField_IsInOneof(oneof_field));
return UPB_PRIVATE(_upb_Message_GetOneofCase)(message, oneof_field);
}
// LINT.ThenChange(GoogleInternalName2)
// Returns false if the message is missing any of its required fields.
@ -271,6 +281,24 @@ UPB_INLINE void _upb_Message_GetExtensionField(
}
}
// NOTE: The default_val is only used for fields that support presence.
// For repeated/map fields, the resulting upb_Array*/upb_Map* can be NULL if a
// upb_Array/upb_Map has not been allocated yet. Array/map fields do not have
// presence, so this is semantically identical to a pointer to an empty
// array/map, and must be treated the same for all semantic purposes.
UPB_API_INLINE upb_MessageValue upb_Message_GetField(
const struct upb_Message* msg, const upb_MiniTableField* field,
upb_MessageValue default_val) {
upb_MessageValue ret;
if (upb_MiniTableField_IsExtension(field)) {
_upb_Message_GetExtensionField(msg, (upb_MiniTableExtension*)field,
&default_val, &ret);
} else {
_upb_Message_GetNonExtensionField(msg, field, &default_val, &ret);
}
return ret;
}
UPB_API_INLINE void upb_Message_SetBaseField(struct upb_Message* msg,
const upb_MiniTableField* f,
const void* val) {
@ -294,6 +322,336 @@ UPB_API_INLINE bool upb_Message_SetExtension(struct upb_Message* msg,
return true;
}
// Sets the value of the given field in the given msg. The return value is true
// if the operation completed successfully, or false if memory allocation
// failed.
UPB_INLINE bool UPB_PRIVATE(_upb_Message_SetField)(struct upb_Message* msg,
const upb_MiniTableField* f,
upb_MessageValue val,
upb_Arena* a) {
if (upb_MiniTableField_IsExtension(f)) {
const upb_MiniTableExtension* ext = (const upb_MiniTableExtension*)f;
return upb_Message_SetExtension(msg, ext, &val, a);
} else {
upb_Message_SetBaseField(msg, f, &val);
return true;
}
}
UPB_API_INLINE const upb_Array* upb_Message_GetArray(
const struct upb_Message* msg, const upb_MiniTableField* f) {
UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f);
upb_Array* ret;
const upb_Array* default_val = NULL;
_upb_Message_GetNonExtensionField(msg, f, &default_val, &ret);
return ret;
}
UPB_API_INLINE bool upb_Message_GetBool(const struct upb_Message* msg,
const upb_MiniTableField* f,
bool default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Bool);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_1Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue def;
def.bool_val = default_val;
return upb_Message_GetField(msg, f, def).bool_val;
}
UPB_API_INLINE double upb_Message_GetDouble(const struct upb_Message* msg,
const upb_MiniTableField* f,
double default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Double);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue def;
def.double_val = default_val;
return upb_Message_GetField(msg, f, def).double_val;
}
UPB_API_INLINE float upb_Message_GetFloat(const struct upb_Message* msg,
const upb_MiniTableField* f,
float default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Float);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue def;
def.float_val = default_val;
return upb_Message_GetField(msg, f, def).float_val;
}
UPB_API_INLINE int32_t upb_Message_GetInt32(const struct upb_Message* msg,
const upb_MiniTableField* f,
int32_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int32 ||
upb_MiniTableField_CType(f) == kUpb_CType_Enum);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue def;
def.int32_val = default_val;
return upb_Message_GetField(msg, f, def).int32_val;
}
UPB_API_INLINE int64_t upb_Message_GetInt64(const struct upb_Message* msg,
const upb_MiniTableField* f,
int64_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int64);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue def;
def.int64_val = default_val;
return upb_Message_GetField(msg, f, def).int64_val;
}
UPB_INLINE void UPB_PRIVATE(_upb_Message_AssertMapIsUntagged)(
const struct upb_Message* msg, const upb_MiniTableField* field) {
UPB_UNUSED(msg);
UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(field);
#ifndef NDEBUG
uintptr_t default_val = 0;
uintptr_t tagged;
_upb_Message_GetNonExtensionField(msg, field, &default_val, &tagged);
UPB_ASSERT(!upb_TaggedMessagePtr_IsEmpty(tagged));
#endif
}
UPB_API_INLINE const struct upb_Map* upb_Message_GetMap(
const struct upb_Message* msg, const upb_MiniTableField* f) {
UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(f);
UPB_PRIVATE(_upb_Message_AssertMapIsUntagged)(msg, f);
struct upb_Map* ret;
const struct upb_Map* default_val = NULL;
_upb_Message_GetNonExtensionField(msg, f, &default_val, &ret);
return ret;
}
UPB_API_INLINE upb_Array* upb_Message_GetMutableArray(
struct upb_Message* msg, const upb_MiniTableField* f) {
UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f);
return (upb_Array*)upb_Message_GetArray(msg, f);
}
UPB_API_INLINE struct upb_Map* upb_Message_GetMutableMap(
struct upb_Message* msg, const upb_MiniTableField* f) {
return (struct upb_Map*)upb_Message_GetMap(msg, f);
}
UPB_API_INLINE upb_Array* upb_Message_GetOrCreateMutableArray(
struct upb_Message* msg, const upb_MiniTableField* f, upb_Arena* arena) {
UPB_ASSERT(arena);
UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f);
upb_Array* array = upb_Message_GetMutableArray(msg, f);
if (!array) {
array = UPB_PRIVATE(_upb_Array_New)(
arena, 4, UPB_PRIVATE(_upb_MiniTableField_ElemSizeLg2)(f));
// Check again due to: https://godbolt.org/z/7WfaoKG1r
UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f);
upb_MessageValue val;
val.array_val = array;
UPB_PRIVATE(_upb_Message_SetField)(msg, f, val, arena);
}
return array;
}
UPB_INLINE struct upb_Map* _upb_Message_GetOrCreateMutableMap(
struct upb_Message* msg, const upb_MiniTableField* field, size_t key_size,
size_t val_size, upb_Arena* arena) {
UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(field);
UPB_PRIVATE(_upb_Message_AssertMapIsUntagged)(msg, field);
struct upb_Map* map = NULL;
struct upb_Map* default_map_value = NULL;
_upb_Message_GetNonExtensionField(msg, field, &default_map_value, &map);
if (!map) {
map = _upb_Map_New(arena, key_size, val_size);
// Check again due to: https://godbolt.org/z/7WfaoKG1r
UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(field);
upb_Message_SetBaseField(msg, field, &map);
}
return map;
}
UPB_API_INLINE struct upb_Map* upb_Message_GetOrCreateMutableMap(
struct upb_Message* msg, const upb_MiniTable* map_entry_mini_table,
const upb_MiniTableField* f, upb_Arena* arena) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message);
const upb_MiniTableField* map_entry_key_field =
&map_entry_mini_table->UPB_ONLYBITS(fields)[0];
const upb_MiniTableField* map_entry_value_field =
&map_entry_mini_table->UPB_ONLYBITS(fields)[1];
return _upb_Message_GetOrCreateMutableMap(
msg, f, _upb_Map_CTypeSize(upb_MiniTableField_CType(map_entry_key_field)),
_upb_Map_CTypeSize(upb_MiniTableField_CType(map_entry_value_field)),
arena);
}
UPB_API_INLINE struct upb_Message* upb_Message_GetOrCreateMutableMessage(
struct upb_Message* msg, const upb_MiniTable* mini_table,
const upb_MiniTableField* f, upb_Arena* arena) {
UPB_ASSERT(arena);
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message);
UPB_ASSUME(!upb_MiniTableField_IsExtension(f));
struct upb_Message* sub_message =
*UPB_PTR_AT(msg, f->UPB_ONLYBITS(offset), struct upb_Message*);
if (!sub_message) {
const upb_MiniTable* sub_mini_table =
upb_MiniTable_SubMessage(mini_table, f);
UPB_ASSERT(sub_mini_table);
sub_message = _upb_Message_New(sub_mini_table, arena);
*UPB_PTR_AT(msg, f->UPB_ONLYBITS(offset), struct upb_Message*) =
sub_message;
UPB_PRIVATE(_upb_Message_SetPresence)(msg, f);
}
return sub_message;
}
UPB_API_INLINE upb_StringView
upb_Message_GetString(const struct upb_Message* msg,
const upb_MiniTableField* f, upb_StringView default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_String ||
upb_MiniTableField_CType(f) == kUpb_CType_Bytes);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
kUpb_FieldRep_StringView);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue def;
def.str_val = default_val;
return upb_Message_GetField(msg, f, def).str_val;
}
UPB_API_INLINE uint32_t upb_Message_GetUInt32(const struct upb_Message* msg,
const upb_MiniTableField* f,
uint32_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt32);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue def;
def.uint32_val = default_val;
return upb_Message_GetField(msg, f, def).uint32_val;
}
UPB_API_INLINE uint64_t upb_Message_GetUInt64(const struct upb_Message* msg,
const upb_MiniTableField* f,
uint64_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt64);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue def;
def.uint64_val = default_val;
return upb_Message_GetField(msg, f, def).uint64_val;
}
UPB_API_INLINE bool upb_Message_SetBool(struct upb_Message* msg,
const upb_MiniTableField* f, bool value,
upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Bool);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_1Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue val;
val.bool_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, f, val, a);
}
UPB_API_INLINE void upb_Message_SetClosedEnum(
struct upb_Message* msg, const upb_MiniTable* msg_mini_table,
const upb_MiniTableField* f, int32_t value) {
UPB_ASSERT(upb_MiniTableField_IsClosedEnum(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSERT(upb_MiniTableEnum_CheckValue(
upb_MiniTable_GetSubEnumTable(msg_mini_table, f), value));
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE bool upb_Message_SetDouble(struct upb_Message* msg,
const upb_MiniTableField* f,
double value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Double);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue val;
val.double_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, f, val, a);
}
UPB_API_INLINE bool upb_Message_SetFloat(struct upb_Message* msg,
const upb_MiniTableField* f,
float value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Float);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue val;
val.float_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, f, val, a);
}
UPB_API_INLINE bool upb_Message_SetInt32(struct upb_Message* msg,
const upb_MiniTableField* f,
int32_t value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int32 ||
upb_MiniTableField_CType(f) == kUpb_CType_Enum);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue val;
val.int32_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, f, val, a);
}
UPB_API_INLINE bool upb_Message_SetInt64(struct upb_Message* msg,
const upb_MiniTableField* f,
int64_t value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int64);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue val;
val.int64_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, f, val, a);
}
// Sets the value of a `string` or `bytes` field. The bytes of the value are not
// copied, so it is the caller's responsibility to ensure that they remain valid
// for the lifetime of `msg`. That might be done by copying them into the given
// arena, or by fusing that arena with the arena the bytes live in, for example.
UPB_API_INLINE bool upb_Message_SetString(struct upb_Message* msg,
const upb_MiniTableField* f,
upb_StringView value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_String ||
upb_MiniTableField_CType(f) == kUpb_CType_Bytes);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
kUpb_FieldRep_StringView);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue val;
val.str_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, f, val, a);
}
UPB_API_INLINE bool upb_Message_SetUInt32(struct upb_Message* msg,
const upb_MiniTableField* f,
uint32_t value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt32);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue val;
val.uint32_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, f, val, a);
}
UPB_API_INLINE bool upb_Message_SetUInt64(struct upb_Message* msg,
const upb_MiniTableField* f,
uint64_t value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt64);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_MessageValue val;
val.uint64_val = value;
return UPB_PRIVATE(_upb_Message_SetField)(msg, f, val, a);
}
UPB_API_INLINE void upb_Message_Clear(struct upb_Message* msg,
const upb_MiniTable* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
@ -335,33 +693,15 @@ UPB_API_INLINE void upb_Message_ClearExtension(
}
}
UPB_INLINE void _upb_Message_AssertMapIsUntagged(
const struct upb_Message* msg, const upb_MiniTableField* field) {
UPB_UNUSED(msg);
UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(field);
#ifndef NDEBUG
uintptr_t default_val = 0;
uintptr_t tagged;
_upb_Message_GetNonExtensionField(msg, field, &default_val, &tagged);
UPB_ASSERT(!upb_TaggedMessagePtr_IsEmpty(tagged));
#endif
}
UPB_INLINE struct upb_Map* _upb_Message_GetOrCreateMutableMap(
struct upb_Message* msg, const upb_MiniTableField* field, size_t key_size,
size_t val_size, upb_Arena* arena) {
UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(field);
_upb_Message_AssertMapIsUntagged(msg, field);
struct upb_Map* map = NULL;
struct upb_Map* default_map_value = NULL;
_upb_Message_GetNonExtensionField(msg, field, &default_map_value, &map);
if (!map) {
map = _upb_Map_New(arena, key_size, val_size);
// Check again due to: https://godbolt.org/z/7WfaoKG1r
UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(field);
upb_Message_SetBaseField(msg, field, &map);
UPB_API_INLINE void* upb_Message_ResizeArrayUninitialized(
struct upb_Message* msg, const upb_MiniTableField* f, size_t size,
upb_Arena* arena) {
UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f);
upb_Array* arr = upb_Message_GetOrCreateMutableArray(msg, f, arena);
if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)(arr, size, arena)) {
return NULL;
}
return map;
return upb_Array_MutableDataPtr(arr);
}
#ifdef __cplusplus

Loading…
Cancel
Save