From 65821a67d60c62ea8ec69a5d76d68da3d775b2da Mon Sep 17 00:00:00 2001 From: Eric Salo Date: Tue, 7 May 2024 18:22:51 -0700 Subject: [PATCH] upb: finish cleaning up message/accessors.h and message/internal/accessors.h PiperOrigin-RevId: 631612923 --- upb/message/accessors.h | 77 +++++++------------------------- upb/message/copy.c | 2 +- upb/message/internal/accessors.h | 46 +++++++++++++++++++ 3 files changed, 62 insertions(+), 63 deletions(-) diff --git a/upb/message/accessors.h b/upb/message/accessors.h index c50467c73c..ebdfa52bef 100644 --- a/upb/message/accessors.h +++ b/upb/message/accessors.h @@ -8,30 +8,17 @@ #ifndef UPB_MESSAGE_ACCESSORS_H_ #define UPB_MESSAGE_ACCESSORS_H_ -#include -#include -#include - -#include "upb/base/descriptor_constants.h" #include "upb/base/string_view.h" #include "upb/mem/arena.h" #include "upb/message/array.h" #include "upb/message/internal/accessors.h" -#include "upb/message/internal/array.h" -#include "upb/message/internal/map.h" -#include "upb/message/internal/message.h" -#include "upb/message/internal/tagged_ptr.h" #include "upb/message/map.h" #include "upb/message/message.h" #include "upb/message/tagged_ptr.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" -#include "upb/mini_table/internal/message.h" #include "upb/mini_table/message.h" -#include "upb/mini_table/sub.h" // Must be last. #include "upb/port/def.inc" @@ -76,6 +63,10 @@ UPB_API_INLINE upb_MessageValue upb_Message_GetField(const upb_Message* msg, const upb_MiniTableField* f, upb_MessageValue default_val); +UPB_API_INLINE upb_TaggedMessagePtr upb_Message_GetTaggedMessagePtr( + const upb_Message* msg, const upb_MiniTableField* field, + upb_Message* default_val); + UPB_API_INLINE const upb_Array* upb_Message_GetArray( const upb_Message* msg, const upb_MiniTableField* f); @@ -102,12 +93,18 @@ UPB_API_INLINE int64_t upb_Message_GetInt64(const upb_Message* msg, UPB_API_INLINE const upb_Map* upb_Message_GetMap(const upb_Message* msg, const upb_MiniTableField* f); +UPB_API_INLINE const upb_Message* upb_Message_GetMessage( + const upb_Message* msg, const upb_MiniTableField* f); + UPB_API_INLINE upb_Array* upb_Message_GetMutableArray( upb_Message* msg, const upb_MiniTableField* f); UPB_API_INLINE upb_Map* upb_Message_GetMutableMap(upb_Message* msg, const upb_MiniTableField* f); +UPB_API_INLINE upb_Message* upb_Message_GetMutableMessage( + upb_Message* msg, const upb_MiniTableField* f); + UPB_API_INLINE upb_Array* upb_Message_GetOrCreateMutableArray( upb_Message* msg, const upb_MiniTableField* f, upb_Arena* arena); @@ -155,6 +152,11 @@ UPB_API_INLINE bool upb_Message_SetInt64(upb_Message* msg, const upb_MiniTableField* f, int64_t value, upb_Arena* a); +UPB_API_INLINE void upb_Message_SetMessage(upb_Message* msg, + const upb_MiniTable* m, + const upb_MiniTableField* f, + upb_Message* sub_message); + UPB_API_INLINE bool upb_Message_SetString(upb_Message* msg, const upb_MiniTableField* f, upb_StringView value, upb_Arena* a); @@ -167,55 +169,6 @@ 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, - upb_Message* default_val) { - UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Message); - UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(field) == - UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte)); - UPB_ASSUME(upb_MiniTableField_IsScalar(field)); - upb_TaggedMessagePtr tagged; - _upb_Message_GetNonExtensionField(msg, field, &default_val, &tagged); - return tagged; -} - -UPB_API_INLINE const upb_Message* upb_Message_GetMessage( - const upb_Message* msg, const upb_MiniTableField* field) { - upb_TaggedMessagePtr tagged = - upb_Message_GetTaggedMessagePtr(msg, field, NULL); - return upb_TaggedMessagePtr_GetNonEmptyMessage(tagged); -} - -UPB_API_INLINE upb_Message* upb_Message_GetMutableMessage( - upb_Message* msg, const upb_MiniTableField* field) { - return (upb_Message*)upb_Message_GetMessage(msg, field); -} - -// 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_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(f)); - upb_Message_SetBaseField(msg, f, &sub_message); -} - -// Sets the value of a message-typed field. The `mini_table` and `field` -// parameters belong to `msg`, not `sub_message`. The mini_tables of `msg` and -// `sub_message` must have been linked for this to work correctly. -UPB_API_INLINE void upb_Message_SetMessage(upb_Message* msg, - const upb_MiniTable* mini_table, - const upb_MiniTableField* field, - upb_Message* sub_message) { - UPB_PRIVATE(_upb_Message_SetTaggedMessagePtr) - (msg, mini_table, field, - UPB_PRIVATE(_upb_TaggedMessagePtr_Pack)(sub_message, false)); -} - UPB_API_INLINE void* upb_Message_ResizeArrayUninitialized( upb_Message* msg, const upb_MiniTableField* f, size_t size, upb_Arena* arena); diff --git a/upb/message/copy.c b/upb/message/copy.c index dca2107779..67e1b5b503 100644 --- a/upb/message/copy.c +++ b/upb/message/copy.c @@ -215,7 +215,7 @@ upb_Message* _upb_Message_Copy(upb_Message* dst, const upb_Message* src, return NULL; } UPB_PRIVATE(_upb_Message_SetTaggedMessagePtr) - (dst, mini_table, field, + (dst, field, UPB_PRIVATE(_upb_TaggedMessagePtr_Pack)(dst_sub_message, is_empty)); } diff --git a/upb/message/internal/accessors.h b/upb/message/internal/accessors.h index c7b447ecc8..f1c5473939 100644 --- a/upb/message/internal/accessors.h +++ b/upb/message/internal/accessors.h @@ -429,6 +429,37 @@ UPB_API_INLINE const struct upb_Map* upb_Message_GetMap( return ret; } +UPB_API_INLINE uintptr_t upb_Message_GetTaggedMessagePtr( + const struct upb_Message* msg, const upb_MiniTableField* f, + struct upb_Message* default_val) { + 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(f)); + uintptr_t tagged; + _upb_Message_GetNonExtensionField(msg, f, &default_val, &tagged); + return tagged; +} + +// 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_INLINE void UPB_PRIVATE(_upb_Message_SetTaggedMessagePtr)( + struct upb_Message* msg, const upb_MiniTableField* f, + uintptr_t 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(f)); + upb_Message_SetBaseField(msg, f, &sub_message); +} + +UPB_API_INLINE const struct upb_Message* upb_Message_GetMessage( + const struct upb_Message* msg, const upb_MiniTableField* f) { + uintptr_t tagged = upb_Message_GetTaggedMessagePtr(msg, f, NULL); + return upb_TaggedMessagePtr_GetNonEmptyMessage(tagged); +} + UPB_API_INLINE upb_Array* upb_Message_GetMutableArray( struct upb_Message* msg, const upb_MiniTableField* f) { UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f); @@ -440,6 +471,11 @@ UPB_API_INLINE struct upb_Map* upb_Message_GetMutableMap( return (struct upb_Map*)upb_Message_GetMap(msg, f); } +UPB_API_INLINE struct upb_Message* upb_Message_GetMutableMessage( + struct upb_Message* msg, const upb_MiniTableField* f) { + return (struct upb_Message*)upb_Message_GetMessage(msg, f); +} + UPB_API_INLINE upb_Array* upb_Message_GetOrCreateMutableArray( struct upb_Message* msg, const upb_MiniTableField* f, upb_Arena* arena) { UPB_ASSERT(arena); @@ -613,6 +649,16 @@ UPB_API_INLINE bool upb_Message_SetInt64(struct upb_Message* msg, return UPB_PRIVATE(_upb_Message_SetField)(msg, f, val, a); } +// Sets the value of a message-typed field. The mini_tables of `msg` and +// `sub_message` must have been linked for this to work correctly. +UPB_API_INLINE void upb_Message_SetMessage(struct upb_Message* msg, + const upb_MiniTable* m, + const upb_MiniTableField* f, + struct upb_Message* sub_message) { + UPB_PRIVATE(_upb_Message_SetTaggedMessagePtr) + (msg, f, UPB_PRIVATE(_upb_TaggedMessagePtr_Pack)(sub_message, false)); +} + // 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