From 7fa050d8c1ebe9062c49bde974498707f98b96bc Mon Sep 17 00:00:00 2001 From: Eric Salo Date: Mon, 20 Nov 2023 11:26:11 -0800 Subject: [PATCH] upb: move IsSubMessage() and IsRepeatedOrMap() from mini_table/internal/field.h PiperOrigin-RevId: 584082079 --- upb/message/accessors.h | 38 ++++++++++++++++---------------- upb/message/copy.c | 4 ++-- upb/message/internal/accessors.h | 2 +- upb/mini_descriptor/BUILD | 1 + upb/mini_descriptor/decode.c | 14 +++++++++--- upb/mini_table/field.h | 18 ++++++++++++--- upb/mini_table/internal/field.h | 10 --------- upb/wire/decode.c | 4 ++-- upb_generator/protoc-gen-upb.cc | 4 ++-- 9 files changed, 53 insertions(+), 42 deletions(-) diff --git a/upb/message/accessors.h b/upb/message/accessors.h index f7cc1b7ac1..3a23f2f75b 100644 --- a/upb/message/accessors.h +++ b/upb/message/accessors.h @@ -63,7 +63,7 @@ UPB_API_INLINE bool upb_Message_GetBool(const upb_Message* msg, bool default_val) { UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Bool); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_1Byte); - UPB_ASSUME(!upb_IsRepeatedOrMap(field)); + UPB_ASSUME(!upb_MiniTableField_IsRepeatedOrMap(field)); bool ret; _upb_Message_GetField(msg, field, &default_val, &ret); return ret; @@ -74,7 +74,7 @@ UPB_API_INLINE bool upb_Message_SetBool(upb_Message* msg, bool value, upb_Arena* a) { UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Bool); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_1Byte); - UPB_ASSUME(!upb_IsRepeatedOrMap(field)); + UPB_ASSUME(!upb_MiniTableField_IsRepeatedOrMap(field)); return _upb_Message_SetField(msg, field, &value, a); } @@ -84,7 +84,7 @@ UPB_API_INLINE int32_t upb_Message_GetInt32(const upb_Message* msg, UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Int32 || upb_MiniTableField_CType(field) == kUpb_CType_Enum); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte); - UPB_ASSUME(!upb_IsRepeatedOrMap(field)); + UPB_ASSUME(!upb_MiniTableField_IsRepeatedOrMap(field)); int32_t ret; _upb_Message_GetField(msg, field, &default_val, &ret); return ret; @@ -96,7 +96,7 @@ UPB_API_INLINE bool upb_Message_SetInt32(upb_Message* msg, UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Int32 || upb_MiniTableField_CType(field) == kUpb_CType_Enum); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte); - UPB_ASSUME(!upb_IsRepeatedOrMap(field)); + UPB_ASSUME(!upb_MiniTableField_IsRepeatedOrMap(field)); return _upb_Message_SetField(msg, field, &value, a); } @@ -105,7 +105,7 @@ UPB_API_INLINE uint32_t upb_Message_GetUInt32(const upb_Message* msg, uint32_t default_val) { UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_UInt32); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte); - UPB_ASSUME(!upb_IsRepeatedOrMap(field)); + UPB_ASSUME(!upb_MiniTableField_IsRepeatedOrMap(field)); uint32_t ret; _upb_Message_GetField(msg, field, &default_val, &ret); return ret; @@ -116,7 +116,7 @@ UPB_API_INLINE bool upb_Message_SetUInt32(upb_Message* msg, uint32_t value, upb_Arena* a) { UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_UInt32); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte); - UPB_ASSUME(!upb_IsRepeatedOrMap(field)); + UPB_ASSUME(!upb_MiniTableField_IsRepeatedOrMap(field)); return _upb_Message_SetField(msg, field, &value, a); } @@ -125,7 +125,7 @@ UPB_API_INLINE void upb_Message_SetClosedEnum( const upb_MiniTableField* field, int32_t value) { UPB_ASSERT(upb_MiniTableField_IsClosedEnum(field)); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte); - UPB_ASSUME(!upb_IsRepeatedOrMap(field)); + UPB_ASSUME(!upb_MiniTableField_IsRepeatedOrMap(field)); UPB_ASSERT(upb_MiniTableEnum_CheckValue( upb_MiniTable_GetSubEnumTable(msg_mini_table, field), value)); _upb_Message_SetNonExtensionField(msg, field, &value); @@ -136,7 +136,7 @@ UPB_API_INLINE int64_t upb_Message_GetInt64(const upb_Message* msg, uint64_t default_val) { UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Int64); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte); - UPB_ASSUME(!upb_IsRepeatedOrMap(field)); + UPB_ASSUME(!upb_MiniTableField_IsRepeatedOrMap(field)); int64_t ret; _upb_Message_GetField(msg, field, &default_val, &ret); return ret; @@ -147,7 +147,7 @@ UPB_API_INLINE bool upb_Message_SetInt64(upb_Message* msg, int64_t value, upb_Arena* a) { UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Int64); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte); - UPB_ASSUME(!upb_IsRepeatedOrMap(field)); + UPB_ASSUME(!upb_MiniTableField_IsRepeatedOrMap(field)); return _upb_Message_SetField(msg, field, &value, a); } @@ -156,7 +156,7 @@ UPB_API_INLINE uint64_t upb_Message_GetUInt64(const upb_Message* msg, uint64_t default_val) { UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_UInt64); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte); - UPB_ASSUME(!upb_IsRepeatedOrMap(field)); + UPB_ASSUME(!upb_MiniTableField_IsRepeatedOrMap(field)); uint64_t ret; _upb_Message_GetField(msg, field, &default_val, &ret); return ret; @@ -167,7 +167,7 @@ UPB_API_INLINE bool upb_Message_SetUInt64(upb_Message* msg, uint64_t value, upb_Arena* a) { UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_UInt64); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte); - UPB_ASSUME(!upb_IsRepeatedOrMap(field)); + UPB_ASSUME(!upb_MiniTableField_IsRepeatedOrMap(field)); return _upb_Message_SetField(msg, field, &value, a); } @@ -176,7 +176,7 @@ UPB_API_INLINE float upb_Message_GetFloat(const upb_Message* msg, float default_val) { UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Float); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte); - UPB_ASSUME(!upb_IsRepeatedOrMap(field)); + UPB_ASSUME(!upb_MiniTableField_IsRepeatedOrMap(field)); float ret; _upb_Message_GetField(msg, field, &default_val, &ret); return ret; @@ -187,7 +187,7 @@ UPB_API_INLINE bool upb_Message_SetFloat(upb_Message* msg, float value, upb_Arena* a) { UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Float); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte); - UPB_ASSUME(!upb_IsRepeatedOrMap(field)); + UPB_ASSUME(!upb_MiniTableField_IsRepeatedOrMap(field)); return _upb_Message_SetField(msg, field, &value, a); } @@ -196,7 +196,7 @@ UPB_API_INLINE double upb_Message_GetDouble(const upb_Message* msg, double default_val) { UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Double); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte); - UPB_ASSUME(!upb_IsRepeatedOrMap(field)); + UPB_ASSUME(!upb_MiniTableField_IsRepeatedOrMap(field)); double ret; _upb_Message_GetField(msg, field, &default_val, &ret); return ret; @@ -207,7 +207,7 @@ UPB_API_INLINE bool upb_Message_SetDouble(upb_Message* msg, double value, upb_Arena* a) { UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Double); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte); - UPB_ASSUME(!upb_IsRepeatedOrMap(field)); + UPB_ASSUME(!upb_MiniTableField_IsRepeatedOrMap(field)); return _upb_Message_SetField(msg, field, &value, a); } @@ -217,7 +217,7 @@ upb_Message_GetString(const upb_Message* msg, const upb_MiniTableField* field, UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_String || upb_MiniTableField_CType(field) == kUpb_CType_Bytes); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_StringView); - UPB_ASSUME(!upb_IsRepeatedOrMap(field)); + UPB_ASSUME(!upb_MiniTableField_IsRepeatedOrMap(field)); upb_StringView ret; _upb_Message_GetField(msg, field, &def_val, &ret); return ret; @@ -229,7 +229,7 @@ UPB_API_INLINE bool upb_Message_SetString(upb_Message* msg, UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_String || upb_MiniTableField_CType(field) == kUpb_CType_Bytes); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_StringView); - UPB_ASSUME(!upb_IsRepeatedOrMap(field)); + UPB_ASSUME(!upb_MiniTableField_IsRepeatedOrMap(field)); return _upb_Message_SetField(msg, field, &value, a); } @@ -239,7 +239,7 @@ UPB_API_INLINE upb_TaggedMessagePtr upb_Message_GetTaggedMessagePtr( UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Message); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte)); - UPB_ASSUME(!upb_IsRepeatedOrMap(field)); + UPB_ASSUME(!upb_MiniTableField_IsRepeatedOrMap(field)); upb_TaggedMessagePtr tagged; _upb_Message_GetNonExtensionField(msg, field, &default_val, &tagged); return tagged; @@ -262,7 +262,7 @@ UPB_API_INLINE void _upb_Message_SetTaggedMessagePtr( UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Message); UPB_ASSUME(_upb_MiniTableField_GetRep(field) == UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte)); - UPB_ASSUME(!upb_IsRepeatedOrMap(field)); + UPB_ASSUME(!upb_MiniTableField_IsRepeatedOrMap(field)); UPB_ASSERT(mini_table->subs[field->UPB_PRIVATE(submsg_index)].submsg); _upb_Message_SetNonExtensionField(msg, field, &sub_message); } diff --git a/upb/message/copy.c b/upb/message/copy.c index 67e3fee9b6..23f558f501 100644 --- a/upb/message/copy.c +++ b/upb/message/copy.c @@ -196,7 +196,7 @@ upb_Message* _upb_Message_Copy(upb_Message* dst, const upb_Message* src, memcpy(dst, src, mini_table->size); for (size_t i = 0; i < mini_table->field_count; ++i) { const upb_MiniTableField* field = &mini_table->fields[i]; - if (!upb_IsRepeatedOrMap(field)) { + if (!upb_MiniTableField_IsRepeatedOrMap(field)) { switch (upb_MiniTableField_CType(field)) { case kUpb_CType_Message: { upb_TaggedMessagePtr tagged = @@ -263,7 +263,7 @@ upb_Message* _upb_Message_Copy(upb_Message* dst, const upb_Message* src, upb_Message_Extension* dst_ext = _upb_Message_GetOrCreateExtension(dst, msg_ext->ext, arena); if (!dst_ext) return NULL; - if (!upb_IsRepeatedOrMap(field)) { + if (!upb_MiniTableField_IsRepeatedOrMap(field)) { if (!upb_Clone_ExtensionValue(msg_ext->ext, msg_ext, dst_ext, arena)) { return NULL; } diff --git a/upb/message/internal/accessors.h b/upb/message/internal/accessors.h index d80db81169..aa39009cd0 100644 --- a/upb/message/internal/accessors.h +++ b/upb/message/internal/accessors.h @@ -212,7 +212,7 @@ _upb_MiniTable_ElementSizeLg2(const upb_MiniTableField* field) { // const upb_MiniTableField* field, // bool value, upb_Arena* a) { // UPB_ASSUME(field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Bool); -// UPB_ASSUME(!upb_IsRepeatedOrMap(field)); +// UPB_ASSUME(!upb_MiniTableField_IsRepeatedOrMap(field)); // UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_1Byte); // _upb_Message_SetField(msg, field, &value, a); // } diff --git a/upb/mini_descriptor/BUILD b/upb/mini_descriptor/BUILD index 6c82039559..57aca1872a 100644 --- a/upb/mini_descriptor/BUILD +++ b/upb/mini_descriptor/BUILD @@ -21,6 +21,7 @@ cc_library( deps = [ ":internal", "//upb:base", + "//upb:base_internal", "//upb:mem", "//upb:mini_table", "//upb:mini_table_internal", diff --git a/upb/mini_descriptor/decode.c b/upb/mini_descriptor/decode.c index 6575e92e30..1470b3f56e 100644 --- a/upb/mini_descriptor/decode.c +++ b/upb/mini_descriptor/decode.c @@ -8,16 +8,24 @@ #include "upb/mini_descriptor/decode.h" #include +#include #include #include "upb/base/descriptor_constants.h" +#include "upb/base/internal/log2.h" +#include "upb/base/status.h" #include "upb/base/string_view.h" #include "upb/mem/arena.h" #include "upb/mini_descriptor/internal/base92.h" #include "upb/mini_descriptor/internal/decoder.h" #include "upb/mini_descriptor/internal/modifiers.h" #include "upb/mini_descriptor/internal/wire_constants.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" @@ -497,7 +505,7 @@ int upb_MtDecoder_CompareFields(const void* _a, const void* _b) { #define UPB_COMBINE(rep, ty, idx) (((rep << type_bits) | ty) << idx_bits) | idx uint32_t a_packed = UPB_COMBINE(a->rep, a->type, a->field_index); uint32_t b_packed = UPB_COMBINE(b->rep, b->type, b->field_index); - assert(a_packed != b_packed); + UPB_ASSERT(a_packed != b_packed); #undef UPB_COMBINE return a_packed < b_packed ? -1 : 1; } @@ -630,7 +638,7 @@ static void upb_MtDecoder_ValidateEntryField(upb_MtDecoder* d, name, expected_num, (int)f->number); } - if (upb_IsRepeatedOrMap(f)) { + if (upb_MiniTableField_IsRepeatedOrMap(f)) { upb_MdDecoder_ErrorJmp( &d->base, "map %s cannot be repeated or map, or be in oneof", name); } @@ -805,7 +813,7 @@ static const char* upb_MtDecoder_DoBuildMiniTableExtension( if (extendee->ext & kUpb_ExtMode_IsMessageSet) { // Extensions of MessageSet must be messages. - if (!upb_IsSubMessage(f)) return NULL; + if (!upb_MiniTableField_IsSubMessage(f)) return NULL; // Extensions of MessageSet must be non-repeating. if ((f->mode & kUpb_FieldMode_Mask) == kUpb_FieldMode_Array) return NULL; diff --git a/upb/mini_table/field.h b/upb/mini_table/field.h index c744dd9d66..6bfd33b685 100644 --- a/upb/mini_table/field.h +++ b/upb/mini_table/field.h @@ -40,20 +40,32 @@ UPB_API_INLINE upb_CType upb_MiniTableField_CType(const upb_MiniTableField* f) { return upb_FieldType_CType(upb_MiniTableField_Type(f)); } +UPB_API_INLINE bool upb_MiniTableField_IsClosedEnum( + const upb_MiniTableField* field) { + return field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Enum; +} + UPB_API_INLINE bool upb_MiniTableField_IsExtension( const upb_MiniTableField* field) { return field->mode & kUpb_LabelFlags_IsExtension; } -UPB_API_INLINE bool upb_MiniTableField_IsClosedEnum( +UPB_API_INLINE bool upb_MiniTableField_IsRepeatedOrMap( const upb_MiniTableField* field) { - return field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Enum; + // This works because upb_FieldMode has no value 3. + return !(field->mode & kUpb_FieldMode_Scalar); +} + +UPB_API_INLINE bool upb_MiniTableField_IsSubMessage( + const upb_MiniTableField* field) { + return field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Message || + field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Group; } UPB_API_INLINE bool upb_MiniTableField_HasPresence( const upb_MiniTableField* field) { if (upb_MiniTableField_IsExtension(field)) { - return !upb_IsRepeatedOrMap(field); + return !upb_MiniTableField_IsRepeatedOrMap(field); } else { return field->presence != 0; } diff --git a/upb/mini_table/internal/field.h b/upb/mini_table/internal/field.h index 3ed8afec76..7425d7102b 100644 --- a/upb/mini_table/internal/field.h +++ b/upb/mini_table/internal/field.h @@ -95,16 +95,6 @@ UPB_INLINE void _upb_MiniTableField_CheckIsMap( UPB_ASSUME(field->presence == 0); } -UPB_INLINE bool upb_IsRepeatedOrMap(const struct upb_MiniTableField* field) { - // This works because upb_FieldMode has no value 3. - return !(field->mode & kUpb_FieldMode_Scalar); -} - -UPB_INLINE bool upb_IsSubMessage(const struct upb_MiniTableField* field) { - return field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Message || - field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Group; -} - #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/upb/wire/decode.c b/upb/wire/decode.c index 93c42344f2..bd91d587fd 100644 --- a/upb/wire/decode.c +++ b/upb/wire/decode.c @@ -608,8 +608,8 @@ static const char* _upb_Decoder_DecodeToMap(upb_Decoder* d, const char* ptr, UPB_ASSERT(entry); UPB_ASSERT(entry->field_count == 2); - UPB_ASSERT(!upb_IsRepeatedOrMap(&entry->fields[0])); - UPB_ASSERT(!upb_IsRepeatedOrMap(&entry->fields[1])); + UPB_ASSERT(!upb_MiniTableField_IsRepeatedOrMap(&entry->fields[0])); + UPB_ASSERT(!upb_MiniTableField_IsRepeatedOrMap(&entry->fields[1])); if (!map) { map = _upb_Decoder_CreateMap(d, entry); diff --git a/upb_generator/protoc-gen-upb.cc b/upb_generator/protoc-gen-upb.cc index 0cd533a5a9..fa60e83603 100644 --- a/upb_generator/protoc-gen-upb.cc +++ b/upb_generator/protoc-gen-upb.cc @@ -282,7 +282,7 @@ void GenerateExtensionInHeader(const DefPoolPair& pools, upb::FieldDefPtr ext, R"cc( UPB_INLINE $0 $1_$2(const struct $3* msg) { const upb_MiniTableExtension* ext = &$4; - UPB_ASSUME(!upb_IsRepeatedOrMap(&ext->field)); + UPB_ASSUME(!upb_MiniTableField_IsRepeatedOrMap(&ext->field)); UPB_ASSUME(_upb_MiniTableField_GetRep(&ext->field) == $5); $0 default_val = $6; $0 ret; @@ -297,7 +297,7 @@ void GenerateExtensionInHeader(const DefPoolPair& pools, upb::FieldDefPtr ext, R"cc( UPB_INLINE void $1_set_$2(struct $3* msg, $0 val, upb_Arena* arena) { const upb_MiniTableExtension* ext = &$4; - UPB_ASSUME(!upb_IsRepeatedOrMap(&ext->field)); + UPB_ASSUME(!upb_MiniTableField_IsRepeatedOrMap(&ext->field)); UPB_ASSUME(_upb_MiniTableField_GetRep(&ext->field) == $5); bool ok = _upb_Message_SetExtensionField(msg, ext, &val, arena); UPB_ASSERT(ok);