File indexing completed on 2025-01-18 10:13:17
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef UPB_MESSAGE_INTERNAL_ACCESSORS_H_
0009 #define UPB_MESSAGE_INTERNAL_ACCESSORS_H_
0010
0011 #include <stddef.h>
0012 #include <stdint.h>
0013 #include <string.h>
0014
0015 #include "upb/base/descriptor_constants.h"
0016 #include "upb/base/internal/endian.h"
0017 #include "upb/base/string_view.h"
0018 #include "upb/mem/arena.h"
0019 #include "upb/message/internal/array.h"
0020 #include "upb/message/internal/extension.h"
0021 #include "upb/message/internal/map.h"
0022 #include "upb/message/internal/message.h"
0023 #include "upb/message/internal/tagged_ptr.h"
0024 #include "upb/message/internal/types.h"
0025 #include "upb/message/value.h"
0026 #include "upb/mini_table/enum.h"
0027 #include "upb/mini_table/extension.h"
0028 #include "upb/mini_table/field.h"
0029 #include "upb/mini_table/internal/field.h"
0030 #include "upb/mini_table/message.h"
0031
0032
0033 #include "upb/port/def.inc"
0034
0035 #if defined(__GNUC__) && !defined(__clang__)
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 #pragma GCC diagnostic push
0046 #pragma GCC diagnostic ignored "-Warray-bounds"
0047 #pragma GCC diagnostic ignored "-Wstringop-overflow"
0048 #if __GNUC__ >= 11
0049 #pragma GCC diagnostic ignored "-Wstringop-overread"
0050 #endif
0051 #endif
0052
0053 #ifdef __cplusplus
0054 extern "C" {
0055 #endif
0056
0057
0058
0059
0060
0061 UPB_INLINE bool UPB_PRIVATE(_upb_Message_GetHasbit)(
0062 const struct upb_Message* msg, const upb_MiniTableField* f) {
0063 const size_t offset = UPB_PRIVATE(_upb_MiniTableField_HasbitOffset)(f);
0064 const char mask = UPB_PRIVATE(_upb_MiniTableField_HasbitMask)(f);
0065
0066 return (*UPB_PTR_AT(msg, offset, const char) & mask) != 0;
0067 }
0068
0069 UPB_INLINE void UPB_PRIVATE(_upb_Message_SetHasbit)(
0070 const struct upb_Message* msg, const upb_MiniTableField* f) {
0071 const size_t offset = UPB_PRIVATE(_upb_MiniTableField_HasbitOffset)(f);
0072 const char mask = UPB_PRIVATE(_upb_MiniTableField_HasbitMask)(f);
0073
0074 (*UPB_PTR_AT(msg, offset, char)) |= mask;
0075 }
0076
0077 UPB_INLINE void UPB_PRIVATE(_upb_Message_ClearHasbit)(
0078 const struct upb_Message* msg, const upb_MiniTableField* f) {
0079 const size_t offset = UPB_PRIVATE(_upb_MiniTableField_HasbitOffset)(f);
0080 const char mask = UPB_PRIVATE(_upb_MiniTableField_HasbitMask)(f);
0081
0082 (*UPB_PTR_AT(msg, offset, char)) &= ~mask;
0083 }
0084
0085
0086
0087 UPB_INLINE uint32_t* UPB_PRIVATE(_upb_Message_OneofCasePtr)(
0088 struct upb_Message* msg, const upb_MiniTableField* f) {
0089 return UPB_PTR_AT(msg, UPB_PRIVATE(_upb_MiniTableField_OneofOffset)(f),
0090 uint32_t);
0091 }
0092
0093 UPB_INLINE uint32_t UPB_PRIVATE(_upb_Message_GetOneofCase)(
0094 const struct upb_Message* msg, const upb_MiniTableField* f) {
0095 const uint32_t* ptr =
0096 UPB_PRIVATE(_upb_Message_OneofCasePtr)((struct upb_Message*)msg, f);
0097
0098 return *ptr;
0099 }
0100
0101 UPB_INLINE void UPB_PRIVATE(_upb_Message_SetOneofCase)(
0102 struct upb_Message* msg, const upb_MiniTableField* f) {
0103 uint32_t* ptr = UPB_PRIVATE(_upb_Message_OneofCasePtr)(msg, f);
0104
0105 *ptr = upb_MiniTableField_Number(f);
0106 }
0107
0108
0109
0110 UPB_INLINE bool UPB_PRIVATE(_upb_Message_ClearOneofCase)(
0111 struct upb_Message* msg, const upb_MiniTableField* f) {
0112 uint32_t* ptr = UPB_PRIVATE(_upb_Message_OneofCasePtr)(msg, f);
0113
0114 if (*ptr != upb_MiniTableField_Number(f)) return false;
0115 *ptr = 0;
0116 return true;
0117 }
0118
0119 UPB_API_INLINE uint32_t upb_Message_WhichOneofFieldNumber(
0120 const struct upb_Message* message, const upb_MiniTableField* oneof_field) {
0121 UPB_ASSUME(upb_MiniTableField_IsInOneof(oneof_field));
0122 return UPB_PRIVATE(_upb_Message_GetOneofCase)(message, oneof_field);
0123 }
0124
0125 UPB_API_INLINE const upb_MiniTableField* upb_Message_WhichOneof(
0126 const struct upb_Message* msg, const upb_MiniTable* m,
0127 const upb_MiniTableField* f) {
0128 uint32_t field_number = upb_Message_WhichOneofFieldNumber(msg, f);
0129 if (field_number == 0) {
0130
0131 return NULL;
0132 }
0133 return upb_MiniTable_FindFieldByNumber(m, field_number);
0134 }
0135
0136
0137
0138
0139 UPB_INLINE bool UPB_PRIVATE(_upb_Message_IsInitializedShallow)(
0140 const struct upb_Message* msg, const upb_MiniTable* m) {
0141 uint64_t bits;
0142 memcpy(&bits, msg + 1, sizeof(bits));
0143 bits = upb_BigEndian64(bits);
0144 return (UPB_PRIVATE(_upb_MiniTable_RequiredMask)(m) & ~bits) == 0;
0145 }
0146
0147 UPB_INLINE void* UPB_PRIVATE(_upb_Message_MutableDataPtr)(
0148 struct upb_Message* msg, const upb_MiniTableField* f) {
0149 return (char*)msg + f->UPB_ONLYBITS(offset);
0150 }
0151
0152 UPB_INLINE const void* UPB_PRIVATE(_upb_Message_DataPtr)(
0153 const struct upb_Message* msg, const upb_MiniTableField* f) {
0154 return (const char*)msg + f->UPB_ONLYBITS(offset);
0155 }
0156
0157 UPB_INLINE void UPB_PRIVATE(_upb_Message_SetPresence)(
0158 struct upb_Message* msg, const upb_MiniTableField* f) {
0159 if (UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(f)) {
0160 UPB_PRIVATE(_upb_Message_SetHasbit)(msg, f);
0161 } else if (upb_MiniTableField_IsInOneof(f)) {
0162 UPB_PRIVATE(_upb_Message_SetOneofCase)(msg, f);
0163 }
0164 }
0165
0166 UPB_INLINE void UPB_PRIVATE(_upb_MiniTableField_DataCopy)(
0167 const upb_MiniTableField* f, void* to, const void* from) {
0168 switch (UPB_PRIVATE(_upb_MiniTableField_GetRep)(f)) {
0169 case kUpb_FieldRep_1Byte:
0170 memcpy(to, from, 1);
0171 return;
0172 case kUpb_FieldRep_4Byte:
0173 memcpy(to, from, 4);
0174 return;
0175 case kUpb_FieldRep_8Byte:
0176 memcpy(to, from, 8);
0177 return;
0178 case kUpb_FieldRep_StringView: {
0179 memcpy(to, from, sizeof(upb_StringView));
0180 return;
0181 }
0182 }
0183 UPB_UNREACHABLE();
0184 }
0185
0186 UPB_INLINE bool UPB_PRIVATE(_upb_MiniTableField_DataEquals)(
0187 const upb_MiniTableField* f, const void* a, const void* b) {
0188 switch (UPB_PRIVATE(_upb_MiniTableField_GetRep)(f)) {
0189 case kUpb_FieldRep_1Byte:
0190 return memcmp(a, b, 1) == 0;
0191 case kUpb_FieldRep_4Byte:
0192 return memcmp(a, b, 4) == 0;
0193 case kUpb_FieldRep_8Byte:
0194 return memcmp(a, b, 8) == 0;
0195 case kUpb_FieldRep_StringView: {
0196 const upb_StringView sa = *(const upb_StringView*)a;
0197 const upb_StringView sb = *(const upb_StringView*)b;
0198 return upb_StringView_IsEqual(sa, sb);
0199 }
0200 }
0201 UPB_UNREACHABLE();
0202 }
0203
0204 UPB_INLINE void UPB_PRIVATE(_upb_MiniTableField_DataClear)(
0205 const upb_MiniTableField* f, void* val) {
0206 const char zero[16] = {0};
0207 UPB_PRIVATE(_upb_MiniTableField_DataCopy)(f, val, zero);
0208 }
0209
0210 UPB_INLINE bool UPB_PRIVATE(_upb_MiniTableField_DataIsZero)(
0211 const upb_MiniTableField* f, const void* val) {
0212 const char zero[16] = {0};
0213 return UPB_PRIVATE(_upb_MiniTableField_DataEquals)(f, val, zero);
0214 }
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248 UPB_API_INLINE bool upb_Message_HasBaseField(const struct upb_Message* msg,
0249 const upb_MiniTableField* field) {
0250 UPB_ASSERT(upb_MiniTableField_HasPresence(field));
0251 UPB_ASSUME(!upb_MiniTableField_IsExtension(field));
0252 if (upb_MiniTableField_IsInOneof(field)) {
0253 return UPB_PRIVATE(_upb_Message_GetOneofCase)(msg, field) ==
0254 upb_MiniTableField_Number(field);
0255 } else {
0256 return UPB_PRIVATE(_upb_Message_GetHasbit)(msg, field);
0257 }
0258 }
0259
0260 UPB_API_INLINE bool upb_Message_HasExtension(const struct upb_Message* msg,
0261 const upb_MiniTableExtension* e) {
0262 UPB_ASSERT(upb_MiniTableField_HasPresence(&e->UPB_PRIVATE(field)));
0263 return UPB_PRIVATE(_upb_Message_Getext)(msg, e) != NULL;
0264 }
0265
0266 UPB_FORCEINLINE void _upb_Message_GetNonExtensionField(
0267 const struct upb_Message* msg, const upb_MiniTableField* field,
0268 const void* default_val, void* val) {
0269 UPB_ASSUME(!upb_MiniTableField_IsExtension(field));
0270 if ((upb_MiniTableField_IsInOneof(field) ||
0271 !UPB_PRIVATE(_upb_MiniTableField_DataIsZero)(field, default_val)) &&
0272 !upb_Message_HasBaseField(msg, field)) {
0273 UPB_PRIVATE(_upb_MiniTableField_DataCopy)(field, val, default_val);
0274 return;
0275 }
0276 UPB_PRIVATE(_upb_MiniTableField_DataCopy)
0277 (field, val, UPB_PRIVATE(_upb_Message_DataPtr)(msg, field));
0278 }
0279
0280 UPB_INLINE void _upb_Message_GetExtensionField(
0281 const struct upb_Message* msg, const upb_MiniTableExtension* mt_ext,
0282 const void* default_val, void* val) {
0283 const upb_Extension* ext = UPB_PRIVATE(_upb_Message_Getext)(msg, mt_ext);
0284 const upb_MiniTableField* f = &mt_ext->UPB_PRIVATE(field);
0285 UPB_ASSUME(upb_MiniTableField_IsExtension(f));
0286
0287 if (ext) {
0288 UPB_PRIVATE(_upb_MiniTableField_DataCopy)(f, val, &ext->data);
0289 } else {
0290 UPB_PRIVATE(_upb_MiniTableField_DataCopy)(f, val, default_val);
0291 }
0292 }
0293
0294
0295
0296
0297
0298
0299 UPB_API_INLINE upb_MessageValue upb_Message_GetField(
0300 const struct upb_Message* msg, const upb_MiniTableField* field,
0301 upb_MessageValue default_val) {
0302 upb_MessageValue ret;
0303 if (upb_MiniTableField_IsExtension(field)) {
0304 _upb_Message_GetExtensionField(msg, (upb_MiniTableExtension*)field,
0305 &default_val, &ret);
0306 } else {
0307 _upb_Message_GetNonExtensionField(msg, field, &default_val, &ret);
0308 }
0309 return ret;
0310 }
0311
0312 UPB_API_INLINE void upb_Message_SetBaseField(struct upb_Message* msg,
0313 const upb_MiniTableField* f,
0314 const void* val) {
0315 UPB_ASSERT(!upb_Message_IsFrozen(msg));
0316 UPB_ASSUME(!upb_MiniTableField_IsExtension(f));
0317 UPB_PRIVATE(_upb_Message_SetPresence)(msg, f);
0318 UPB_PRIVATE(_upb_MiniTableField_DataCopy)
0319 (f, UPB_PRIVATE(_upb_Message_MutableDataPtr)(msg, f), val);
0320 }
0321
0322 UPB_API_INLINE bool upb_Message_SetExtension(struct upb_Message* msg,
0323 const upb_MiniTableExtension* e,
0324 const void* val, upb_Arena* a) {
0325 UPB_ASSERT(!upb_Message_IsFrozen(msg));
0326 UPB_ASSERT(a);
0327 upb_Extension* ext =
0328 UPB_PRIVATE(_upb_Message_GetOrCreateExtension)(msg, e, a);
0329 if (!ext) return false;
0330 UPB_PRIVATE(_upb_MiniTableField_DataCopy)
0331 (&e->UPB_PRIVATE(field), &ext->data, val);
0332 return true;
0333 }
0334
0335
0336
0337
0338 UPB_INLINE bool UPB_PRIVATE(_upb_Message_SetField)(struct upb_Message* msg,
0339 const upb_MiniTableField* f,
0340 upb_MessageValue val,
0341 upb_Arena* a) {
0342 if (upb_MiniTableField_IsExtension(f)) {
0343 const upb_MiniTableExtension* ext = (const upb_MiniTableExtension*)f;
0344 return upb_Message_SetExtension(msg, ext, &val, a);
0345 } else {
0346 upb_Message_SetBaseField(msg, f, &val);
0347 return true;
0348 }
0349 }
0350
0351 UPB_API_INLINE const upb_Array* upb_Message_GetArray(
0352 const struct upb_Message* msg, const upb_MiniTableField* f) {
0353 UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f);
0354 upb_Array* ret;
0355 const upb_Array* default_val = NULL;
0356 _upb_Message_GetNonExtensionField(msg, f, &default_val, &ret);
0357 return ret;
0358 }
0359
0360 UPB_API_INLINE bool upb_Message_GetBool(const struct upb_Message* msg,
0361 const upb_MiniTableField* f,
0362 bool default_val) {
0363 UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Bool);
0364 UPB_ASSUME(upb_MiniTableField_IsScalar(f));
0365 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_1Byte);
0366 upb_MessageValue def;
0367 def.bool_val = default_val;
0368 return upb_Message_GetField(msg, f, def).bool_val;
0369 }
0370
0371 UPB_API_INLINE double upb_Message_GetDouble(const struct upb_Message* msg,
0372 const upb_MiniTableField* f,
0373 double default_val) {
0374 UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Double);
0375 UPB_ASSUME(upb_MiniTableField_IsScalar(f));
0376 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
0377
0378 upb_MessageValue def;
0379 def.double_val = default_val;
0380 return upb_Message_GetField(msg, f, def).double_val;
0381 }
0382
0383 UPB_API_INLINE float upb_Message_GetFloat(const struct upb_Message* msg,
0384 const upb_MiniTableField* f,
0385 float default_val) {
0386 UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Float);
0387 UPB_ASSUME(upb_MiniTableField_IsScalar(f));
0388 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
0389
0390 upb_MessageValue def;
0391 def.float_val = default_val;
0392 return upb_Message_GetField(msg, f, def).float_val;
0393 }
0394
0395 UPB_API_INLINE int32_t upb_Message_GetInt32(const struct upb_Message* msg,
0396 const upb_MiniTableField* f,
0397 int32_t default_val) {
0398 UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int32 ||
0399 upb_MiniTableField_CType(f) == kUpb_CType_Enum);
0400 UPB_ASSUME(upb_MiniTableField_IsScalar(f));
0401 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
0402
0403 upb_MessageValue def;
0404 def.int32_val = default_val;
0405 return upb_Message_GetField(msg, f, def).int32_val;
0406 }
0407
0408 UPB_API_INLINE int64_t upb_Message_GetInt64(const struct upb_Message* msg,
0409 const upb_MiniTableField* f,
0410 int64_t default_val) {
0411 UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int64);
0412 UPB_ASSUME(upb_MiniTableField_IsScalar(f));
0413 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
0414
0415 upb_MessageValue def;
0416 def.int64_val = default_val;
0417 return upb_Message_GetField(msg, f, def).int64_val;
0418 }
0419
0420 UPB_INLINE void UPB_PRIVATE(_upb_Message_AssertMapIsUntagged)(
0421 const struct upb_Message* msg, const upb_MiniTableField* field) {
0422 UPB_UNUSED(msg);
0423 UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(field);
0424 #ifndef NDEBUG
0425 uintptr_t default_val = 0;
0426 uintptr_t tagged;
0427 _upb_Message_GetNonExtensionField(msg, field, &default_val, &tagged);
0428 UPB_ASSERT(!upb_TaggedMessagePtr_IsEmpty(tagged));
0429 #endif
0430 }
0431
0432 UPB_API_INLINE const struct upb_Map* upb_Message_GetMap(
0433 const struct upb_Message* msg, const upb_MiniTableField* f) {
0434 UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(f);
0435 UPB_PRIVATE(_upb_Message_AssertMapIsUntagged)(msg, f);
0436 struct upb_Map* ret;
0437 const struct upb_Map* default_val = NULL;
0438 _upb_Message_GetNonExtensionField(msg, f, &default_val, &ret);
0439 return ret;
0440 }
0441
0442 UPB_API_INLINE uintptr_t upb_Message_GetTaggedMessagePtr(
0443 const struct upb_Message* msg, const upb_MiniTableField* f,
0444 struct upb_Message* default_val) {
0445 UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message);
0446 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
0447 UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte));
0448 UPB_ASSUME(upb_MiniTableField_IsScalar(f));
0449 uintptr_t tagged;
0450 _upb_Message_GetNonExtensionField(msg, f, &default_val, &tagged);
0451 return tagged;
0452 }
0453
0454
0455
0456
0457 UPB_INLINE void UPB_PRIVATE(_upb_Message_SetTaggedMessagePtr)(
0458 struct upb_Message* msg, const upb_MiniTableField* f,
0459 uintptr_t sub_message) {
0460 UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message);
0461 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
0462 UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte));
0463 UPB_ASSUME(upb_MiniTableField_IsScalar(f));
0464 upb_Message_SetBaseField(msg, f, &sub_message);
0465 }
0466
0467 UPB_API_INLINE const struct upb_Message* upb_Message_GetMessage(
0468 const struct upb_Message* msg, const upb_MiniTableField* f) {
0469 uintptr_t tagged = upb_Message_GetTaggedMessagePtr(msg, f, NULL);
0470 return upb_TaggedMessagePtr_GetNonEmptyMessage(tagged);
0471 }
0472
0473 UPB_API_INLINE upb_Array* upb_Message_GetMutableArray(
0474 struct upb_Message* msg, const upb_MiniTableField* f) {
0475 UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f);
0476 return (upb_Array*)upb_Message_GetArray(msg, f);
0477 }
0478
0479 UPB_API_INLINE struct upb_Map* upb_Message_GetMutableMap(
0480 struct upb_Message* msg, const upb_MiniTableField* f) {
0481 return (struct upb_Map*)upb_Message_GetMap(msg, f);
0482 }
0483
0484 UPB_API_INLINE struct upb_Message* upb_Message_GetMutableMessage(
0485 struct upb_Message* msg, const upb_MiniTableField* f) {
0486 return (struct upb_Message*)upb_Message_GetMessage(msg, f);
0487 }
0488
0489 UPB_API_INLINE upb_Array* upb_Message_GetOrCreateMutableArray(
0490 struct upb_Message* msg, const upb_MiniTableField* f, upb_Arena* arena) {
0491 UPB_ASSERT(arena);
0492 UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f);
0493 upb_Array* array = upb_Message_GetMutableArray(msg, f);
0494 if (!array) {
0495 array = UPB_PRIVATE(_upb_Array_New)(
0496 arena, 4, UPB_PRIVATE(_upb_MiniTableField_ElemSizeLg2)(f));
0497
0498 UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f);
0499 upb_MessageValue val;
0500 val.array_val = array;
0501 UPB_PRIVATE(_upb_Message_SetField)(msg, f, val, arena);
0502 }
0503 return array;
0504 }
0505
0506 UPB_INLINE struct upb_Map* _upb_Message_GetOrCreateMutableMap(
0507 struct upb_Message* msg, const upb_MiniTableField* field, size_t key_size,
0508 size_t val_size, upb_Arena* arena) {
0509 UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(field);
0510 UPB_PRIVATE(_upb_Message_AssertMapIsUntagged)(msg, field);
0511 struct upb_Map* map = NULL;
0512 struct upb_Map* default_map_value = NULL;
0513 _upb_Message_GetNonExtensionField(msg, field, &default_map_value, &map);
0514 if (!map) {
0515 map = _upb_Map_New(arena, key_size, val_size);
0516
0517 UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(field);
0518 upb_Message_SetBaseField(msg, field, &map);
0519 }
0520 return map;
0521 }
0522
0523 UPB_API_INLINE struct upb_Map* upb_Message_GetOrCreateMutableMap(
0524 struct upb_Message* msg, const upb_MiniTable* map_entry_mini_table,
0525 const upb_MiniTableField* f, upb_Arena* arena) {
0526 UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message);
0527 const upb_MiniTableField* map_entry_key_field =
0528 &map_entry_mini_table->UPB_ONLYBITS(fields)[0];
0529 const upb_MiniTableField* map_entry_value_field =
0530 &map_entry_mini_table->UPB_ONLYBITS(fields)[1];
0531 return _upb_Message_GetOrCreateMutableMap(
0532 msg, f, _upb_Map_CTypeSize(upb_MiniTableField_CType(map_entry_key_field)),
0533 _upb_Map_CTypeSize(upb_MiniTableField_CType(map_entry_value_field)),
0534 arena);
0535 }
0536
0537 UPB_API_INLINE struct upb_Message* upb_Message_GetOrCreateMutableMessage(
0538 struct upb_Message* msg, const upb_MiniTable* mini_table,
0539 const upb_MiniTableField* f, upb_Arena* arena) {
0540 UPB_ASSERT(arena);
0541 UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message);
0542 UPB_ASSUME(!upb_MiniTableField_IsExtension(f));
0543 struct upb_Message* sub_message =
0544 *UPB_PTR_AT(msg, f->UPB_ONLYBITS(offset), struct upb_Message*);
0545 if (!sub_message) {
0546 const upb_MiniTable* sub_mini_table =
0547 upb_MiniTable_SubMessage(mini_table, f);
0548 UPB_ASSERT(sub_mini_table);
0549 sub_message = _upb_Message_New(sub_mini_table, arena);
0550 *UPB_PTR_AT(msg, f->UPB_ONLYBITS(offset), struct upb_Message*) =
0551 sub_message;
0552 UPB_PRIVATE(_upb_Message_SetPresence)(msg, f);
0553 }
0554 return sub_message;
0555 }
0556
0557 UPB_API_INLINE upb_StringView
0558 upb_Message_GetString(const struct upb_Message* msg,
0559 const upb_MiniTableField* f, upb_StringView default_val) {
0560 UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_String ||
0561 upb_MiniTableField_CType(f) == kUpb_CType_Bytes);
0562 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
0563 kUpb_FieldRep_StringView);
0564
0565 upb_MessageValue def;
0566 def.str_val = default_val;
0567 return upb_Message_GetField(msg, f, def).str_val;
0568 }
0569
0570 UPB_API_INLINE uint32_t upb_Message_GetUInt32(const struct upb_Message* msg,
0571 const upb_MiniTableField* f,
0572 uint32_t default_val) {
0573 UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt32);
0574 UPB_ASSUME(upb_MiniTableField_IsScalar(f));
0575 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
0576
0577 upb_MessageValue def;
0578 def.uint32_val = default_val;
0579 return upb_Message_GetField(msg, f, def).uint32_val;
0580 }
0581
0582 UPB_API_INLINE uint64_t upb_Message_GetUInt64(const struct upb_Message* msg,
0583 const upb_MiniTableField* f,
0584 uint64_t default_val) {
0585 UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt64);
0586 UPB_ASSUME(upb_MiniTableField_IsScalar(f));
0587 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
0588
0589 upb_MessageValue def;
0590 def.uint64_val = default_val;
0591 return upb_Message_GetField(msg, f, def).uint64_val;
0592 }
0593
0594
0595
0596 UPB_API_INLINE void upb_Message_SetBaseFieldBool(struct upb_Message* msg,
0597 const upb_MiniTableField* f,
0598 bool value) {
0599 UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Bool);
0600 UPB_ASSUME(upb_MiniTableField_IsScalar(f));
0601 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_1Byte);
0602 upb_Message_SetBaseField(msg, f, &value);
0603 }
0604
0605 UPB_API_INLINE void upb_Message_SetBaseFieldDouble(struct upb_Message* msg,
0606 const upb_MiniTableField* f,
0607 double value) {
0608 UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Double);
0609 UPB_ASSUME(upb_MiniTableField_IsScalar(f));
0610 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
0611 upb_Message_SetBaseField(msg, f, &value);
0612 }
0613
0614 UPB_API_INLINE void upb_Message_SetBaseFieldFloat(struct upb_Message* msg,
0615 const upb_MiniTableField* f,
0616 float value) {
0617 UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Float);
0618 UPB_ASSUME(upb_MiniTableField_IsScalar(f));
0619 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
0620 upb_Message_SetBaseField(msg, f, &value);
0621 }
0622
0623 UPB_API_INLINE void upb_Message_SetBaseFieldInt32(struct upb_Message* msg,
0624 const upb_MiniTableField* f,
0625 int32_t value) {
0626 UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int32 ||
0627 upb_MiniTableField_CType(f) == kUpb_CType_Enum);
0628 UPB_ASSUME(upb_MiniTableField_IsScalar(f));
0629 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
0630 upb_Message_SetBaseField(msg, f, &value);
0631 }
0632
0633 UPB_API_INLINE void upb_Message_SetBaseFieldInt64(struct upb_Message* msg,
0634 const upb_MiniTableField* f,
0635 int64_t value) {
0636 UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int64);
0637 UPB_ASSUME(upb_MiniTableField_IsScalar(f));
0638 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
0639 upb_Message_SetBaseField(msg, f, &value);
0640 }
0641
0642 UPB_API_INLINE void upb_Message_SetBaseFieldString(struct upb_Message* msg,
0643 const upb_MiniTableField* f,
0644 upb_StringView value) {
0645 UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_String ||
0646 upb_MiniTableField_CType(f) == kUpb_CType_Bytes);
0647 UPB_ASSUME(upb_MiniTableField_IsScalar(f));
0648 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
0649 kUpb_FieldRep_StringView);
0650 upb_Message_SetBaseField(msg, f, &value);
0651 }
0652
0653 UPB_API_INLINE void upb_Message_SetBaseFieldUInt32(struct upb_Message* msg,
0654 const upb_MiniTableField* f,
0655 uint32_t value) {
0656 UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt32);
0657 UPB_ASSUME(upb_MiniTableField_IsScalar(f));
0658 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
0659 upb_Message_SetBaseField(msg, f, &value);
0660 }
0661
0662 UPB_API_INLINE void upb_Message_SetBaseFieldUInt64(struct upb_Message* msg,
0663 const upb_MiniTableField* f,
0664 uint64_t value) {
0665 UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt64);
0666 UPB_ASSUME(upb_MiniTableField_IsScalar(f));
0667 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
0668 upb_Message_SetBaseField(msg, f, &value);
0669 }
0670
0671 UPB_API_INLINE void upb_Message_SetClosedEnum(struct upb_Message* msg,
0672 const upb_MiniTable* m,
0673 const upb_MiniTableField* f,
0674 int32_t value) {
0675 UPB_ASSERT(upb_MiniTableField_IsClosedEnum(f));
0676 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
0677 UPB_ASSERT(
0678 upb_MiniTableEnum_CheckValue(upb_MiniTable_GetSubEnumTable(m, f), value));
0679 upb_Message_SetBaseField(msg, f, &value);
0680 }
0681
0682
0683
0684 UPB_API_INLINE bool upb_Message_SetExtensionBool(
0685 struct upb_Message* msg, const upb_MiniTableExtension* e, bool value,
0686 upb_Arena* a) {
0687 UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Bool);
0688 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
0689 kUpb_FieldRep_1Byte);
0690 return upb_Message_SetExtension(msg, e, &value, a);
0691 }
0692
0693 UPB_API_INLINE bool upb_Message_SetExtensionDouble(
0694 struct upb_Message* msg, const upb_MiniTableExtension* e, double value,
0695 upb_Arena* a) {
0696 UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Double);
0697 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
0698 kUpb_FieldRep_8Byte);
0699 return upb_Message_SetExtension(msg, e, &value, a);
0700 }
0701
0702 UPB_API_INLINE bool upb_Message_SetExtensionFloat(
0703 struct upb_Message* msg, const upb_MiniTableExtension* e, float value,
0704 upb_Arena* a) {
0705 UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Float);
0706 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
0707 kUpb_FieldRep_4Byte);
0708 return upb_Message_SetExtension(msg, e, &value, a);
0709 }
0710
0711 UPB_API_INLINE bool upb_Message_SetExtensionInt32(
0712 struct upb_Message* msg, const upb_MiniTableExtension* e, int32_t value,
0713 upb_Arena* a) {
0714 UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Int32 ||
0715 upb_MiniTableExtension_CType(e) == kUpb_CType_Enum);
0716 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
0717 kUpb_FieldRep_4Byte);
0718 return upb_Message_SetExtension(msg, e, &value, a);
0719 }
0720
0721 UPB_API_INLINE bool upb_Message_SetExtensionInt64(
0722 struct upb_Message* msg, const upb_MiniTableExtension* e, int64_t value,
0723 upb_Arena* a) {
0724 UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Int64);
0725 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
0726 kUpb_FieldRep_8Byte);
0727 return upb_Message_SetExtension(msg, e, &value, a);
0728 }
0729
0730 UPB_API_INLINE bool upb_Message_SetExtensionString(
0731 struct upb_Message* msg, const upb_MiniTableExtension* e,
0732 upb_StringView value, upb_Arena* a) {
0733 UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_String ||
0734 upb_MiniTableExtension_CType(e) == kUpb_CType_Bytes);
0735 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
0736 kUpb_FieldRep_StringView);
0737 return upb_Message_SetExtension(msg, e, &value, a);
0738 }
0739
0740 UPB_API_INLINE bool upb_Message_SetExtensionUInt32(
0741 struct upb_Message* msg, const upb_MiniTableExtension* e, uint32_t value,
0742 upb_Arena* a) {
0743 UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_UInt32);
0744 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
0745 kUpb_FieldRep_4Byte);
0746 return upb_Message_SetExtension(msg, e, &value, a);
0747 }
0748
0749 UPB_API_INLINE bool upb_Message_SetExtensionUInt64(
0750 struct upb_Message* msg, const upb_MiniTableExtension* e, uint64_t value,
0751 upb_Arena* a) {
0752 UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_UInt64);
0753 UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
0754 kUpb_FieldRep_8Byte);
0755 return upb_Message_SetExtension(msg, e, &value, a);
0756 }
0757
0758
0759
0760 UPB_API_INLINE bool upb_Message_SetBool(struct upb_Message* msg,
0761 const upb_MiniTableField* f, bool value,
0762 upb_Arena* a) {
0763 return upb_MiniTableField_IsExtension(f)
0764 ? upb_Message_SetExtensionBool(
0765 msg, (const upb_MiniTableExtension*)f, value, a)
0766 : (upb_Message_SetBaseFieldBool(msg, f, value), true);
0767 }
0768
0769 UPB_API_INLINE bool upb_Message_SetDouble(struct upb_Message* msg,
0770 const upb_MiniTableField* f,
0771 double value, upb_Arena* a) {
0772 return upb_MiniTableField_IsExtension(f)
0773 ? upb_Message_SetExtensionDouble(
0774 msg, (const upb_MiniTableExtension*)f, value, a)
0775 : (upb_Message_SetBaseFieldDouble(msg, f, value), true);
0776 }
0777
0778 UPB_API_INLINE bool upb_Message_SetFloat(struct upb_Message* msg,
0779 const upb_MiniTableField* f,
0780 float value, upb_Arena* a) {
0781 return upb_MiniTableField_IsExtension(f)
0782 ? upb_Message_SetExtensionFloat(
0783 msg, (const upb_MiniTableExtension*)f, value, a)
0784 : (upb_Message_SetBaseFieldFloat(msg, f, value), true);
0785 }
0786
0787 UPB_API_INLINE bool upb_Message_SetInt32(struct upb_Message* msg,
0788 const upb_MiniTableField* f,
0789 int32_t value, upb_Arena* a) {
0790 return upb_MiniTableField_IsExtension(f)
0791 ? upb_Message_SetExtensionInt32(
0792 msg, (const upb_MiniTableExtension*)f, value, a)
0793 : (upb_Message_SetBaseFieldInt32(msg, f, value), true);
0794 }
0795
0796 UPB_API_INLINE bool upb_Message_SetInt64(struct upb_Message* msg,
0797 const upb_MiniTableField* f,
0798 int64_t value, upb_Arena* a) {
0799 return upb_MiniTableField_IsExtension(f)
0800 ? upb_Message_SetExtensionInt64(
0801 msg, (const upb_MiniTableExtension*)f, value, a)
0802 : (upb_Message_SetBaseFieldInt64(msg, f, value), true);
0803 }
0804
0805
0806
0807 UPB_API_INLINE void upb_Message_SetMessage(struct upb_Message* msg,
0808 const upb_MiniTableField* f,
0809 struct upb_Message* value) {
0810 UPB_PRIVATE(_upb_Message_SetTaggedMessagePtr)
0811 (msg, f, UPB_PRIVATE(_upb_TaggedMessagePtr_Pack)(value, false));
0812 }
0813
0814
0815
0816
0817
0818 UPB_API_INLINE bool upb_Message_SetString(struct upb_Message* msg,
0819 const upb_MiniTableField* f,
0820 upb_StringView value, upb_Arena* a) {
0821 return upb_MiniTableField_IsExtension(f)
0822 ? upb_Message_SetExtensionString(
0823 msg, (const upb_MiniTableExtension*)f, value, a)
0824 : (upb_Message_SetBaseFieldString(msg, f, value), true);
0825 }
0826
0827 UPB_API_INLINE bool upb_Message_SetUInt32(struct upb_Message* msg,
0828 const upb_MiniTableField* f,
0829 uint32_t value, upb_Arena* a) {
0830 return upb_MiniTableField_IsExtension(f)
0831 ? upb_Message_SetExtensionUInt32(
0832 msg, (const upb_MiniTableExtension*)f, value, a)
0833 : (upb_Message_SetBaseFieldUInt32(msg, f, value), true);
0834 }
0835
0836 UPB_API_INLINE bool upb_Message_SetUInt64(struct upb_Message* msg,
0837 const upb_MiniTableField* f,
0838 uint64_t value, upb_Arena* a) {
0839 return upb_MiniTableField_IsExtension(f)
0840 ? upb_Message_SetExtensionUInt64(
0841 msg, (const upb_MiniTableExtension*)f, value, a)
0842 : (upb_Message_SetBaseFieldUInt64(msg, f, value), true);
0843 }
0844
0845 UPB_API_INLINE void upb_Message_Clear(struct upb_Message* msg,
0846 const upb_MiniTable* m) {
0847 UPB_ASSERT(!upb_Message_IsFrozen(msg));
0848 upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
0849 memset(msg, 0, m->UPB_PRIVATE(size));
0850 if (in) {
0851
0852 in->unknown_end = sizeof(upb_Message_Internal);
0853 in->ext_begin = in->size;
0854 UPB_PRIVATE(_upb_Message_SetInternal)(msg, in);
0855 }
0856 }
0857
0858 UPB_API_INLINE void upb_Message_ClearBaseField(struct upb_Message* msg,
0859 const upb_MiniTableField* f) {
0860 UPB_ASSERT(!upb_Message_IsFrozen(msg));
0861 if (UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(f)) {
0862 UPB_PRIVATE(_upb_Message_ClearHasbit)(msg, f);
0863 } else if (upb_MiniTableField_IsInOneof(f)) {
0864 uint32_t* ptr = UPB_PRIVATE(_upb_Message_OneofCasePtr)(msg, f);
0865 if (*ptr != upb_MiniTableField_Number(f)) return;
0866 *ptr = 0;
0867 }
0868 const char zeros[16] = {0};
0869 UPB_PRIVATE(_upb_MiniTableField_DataCopy)
0870 (f, UPB_PRIVATE(_upb_Message_MutableDataPtr)(msg, f), zeros);
0871 }
0872
0873 UPB_API_INLINE void upb_Message_ClearExtension(
0874 struct upb_Message* msg, const upb_MiniTableExtension* e) {
0875 UPB_ASSERT(!upb_Message_IsFrozen(msg));
0876 upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
0877 if (!in) return;
0878 const upb_Extension* base = UPB_PTR_AT(in, in->ext_begin, upb_Extension);
0879 upb_Extension* ext = (upb_Extension*)UPB_PRIVATE(_upb_Message_Getext)(msg, e);
0880 if (ext) {
0881 *ext = *base;
0882 in->ext_begin += sizeof(upb_Extension);
0883 }
0884 }
0885
0886 UPB_API_INLINE void upb_Message_ClearOneof(struct upb_Message* msg,
0887 const upb_MiniTable* m,
0888 const upb_MiniTableField* f) {
0889 UPB_ASSERT(!upb_Message_IsFrozen(msg));
0890 uint32_t field_number = upb_Message_WhichOneofFieldNumber(msg, f);
0891 if (field_number == 0) {
0892
0893 return;
0894 }
0895
0896 const upb_MiniTableField* field =
0897 upb_MiniTable_FindFieldByNumber(m, field_number);
0898 upb_Message_ClearBaseField(msg, field);
0899 }
0900
0901 UPB_API_INLINE void* upb_Message_ResizeArrayUninitialized(
0902 struct upb_Message* msg, const upb_MiniTableField* f, size_t size,
0903 upb_Arena* arena) {
0904 UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f);
0905 upb_Array* arr = upb_Message_GetOrCreateMutableArray(msg, f, arena);
0906 if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)(arr, size, arena)) {
0907 return NULL;
0908 }
0909 return upb_Array_MutableDataPtr(arr);
0910 }
0911
0912 #ifdef __cplusplus
0913 }
0914 #endif
0915
0916 #if defined(__GNUC__) && !defined(__clang__)
0917 #pragma GCC diagnostic pop
0918 #endif
0919
0920 #include "upb/port/undef.inc"
0921
0922 #endif