diff --git a/upb/base/descriptor_constants.h b/upb/base/descriptor_constants.h index 638649a9ca..6aa21409e7 100644 --- a/upb/base/descriptor_constants.h +++ b/upb/base/descriptor_constants.h @@ -64,7 +64,34 @@ typedef enum { extern "C" { #endif -UPB_INLINE bool upb_FieldType_IsPackable(upb_FieldType type) { +// Convert from upb_FieldType to upb_CType +UPB_INLINE upb_CType upb_FieldType_CType(upb_FieldType field_type) { + static const upb_CType c_type[] = { + kUpb_CType_Double, // kUpb_FieldType_Double + kUpb_CType_Float, // kUpb_FieldType_Float + kUpb_CType_Int64, // kUpb_FieldType_Int64 + kUpb_CType_UInt64, // kUpb_FieldType_UInt64 + kUpb_CType_Int32, // kUpb_FieldType_Int32 + kUpb_CType_UInt64, // kUpb_FieldType_Fixed64 + kUpb_CType_UInt32, // kUpb_FieldType_Fixed32 + kUpb_CType_Bool, // kUpb_FieldType_Bool + kUpb_CType_String, // kUpb_FieldType_String + kUpb_CType_Message, // kUpb_FieldType_Group + kUpb_CType_Message, // kUpb_FieldType_Message + kUpb_CType_Bytes, // kUpb_FieldType_Bytes + kUpb_CType_UInt32, // kUpb_FieldType_UInt32 + kUpb_CType_Enum, // kUpb_FieldType_Enum + kUpb_CType_Int32, // kUpb_FieldType_SFixed32 + kUpb_CType_Int64, // kUpb_FieldType_SFixed64 + kUpb_CType_Int32, // kUpb_FieldType_SInt32 + kUpb_CType_Int64, // kUpb_FieldType_SInt64 + }; + + // -1 here because the enum is one-based but the table is zero-based. + return c_type[field_type - 1]; +} + +UPB_INLINE bool upb_FieldType_IsPackable(upb_FieldType field_type) { // clang-format off const unsigned kUnpackableTypes = (1 << kUpb_FieldType_String) | @@ -72,7 +99,7 @@ UPB_INLINE bool upb_FieldType_IsPackable(upb_FieldType type) { (1 << kUpb_FieldType_Message) | (1 << kUpb_FieldType_Group); // clang-format on - return (1 << type) & ~kUnpackableTypes; + return (1 << field_type) & ~kUnpackableTypes; } #ifdef __cplusplus diff --git a/upb/message/BUILD b/upb/message/BUILD index 22d4ea9319..2640868573 100644 --- a/upb/message/BUILD +++ b/upb/message/BUILD @@ -29,9 +29,12 @@ cc_library( visibility = ["//visibility:public"], deps = [ ":internal", + ":internal_types", ":message", + ":tagged_ptr", "//upb:base", "//upb:eps_copy_input_stream", + "//upb:mem", "//upb:mini_table", "//upb:mini_table_internal", "//upb:port", @@ -49,6 +52,12 @@ cc_library( visibility = ["//visibility:public"], deps = [ ":internal", + ":internal_types", + ":message", + ":tagged_ptr", + "//upb:base", + "//upb:mem", + "//upb:mini_table", "//upb:mini_table_internal", "//upb:port", ], @@ -66,8 +75,10 @@ cc_library( visibility = ["//visibility:public"], deps = [ ":accessors", + ":accessors_internal", ":internal", ":message", + ":tagged_ptr", ":types", "//upb:base", "//upb:mem", @@ -95,6 +106,7 @@ cc_library( "internal/map_entry.h", "internal/map_sorter.h", "internal/message.h", + "internal/size_log2.h", "map_gencode_util.h", ], copts = UPB_DEFAULT_COPTS, diff --git a/upb/message/array.c b/upb/message/array.c index 0585b315cf..b4e907a350 100644 --- a/upb/message/array.c +++ b/upb/message/array.c @@ -28,29 +28,20 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#include "upb/message/internal/array.h" +#include "upb/message/array.h" #include +#include "upb/base/descriptor_constants.h" +#include "upb/mem/arena.h" +#include "upb/message/internal/array.h" +#include "upb/message/internal/size_log2.h" + // Must be last. #include "upb/port/def.inc" -const char _upb_Array_CTypeSizeLg2Table[] = { - [kUpb_CType_Bool] = 0, - [kUpb_CType_Float] = 2, - [kUpb_CType_Int32] = 2, - [kUpb_CType_UInt32] = 2, - [kUpb_CType_Enum] = 2, - [kUpb_CType_Message] = UPB_SIZE(2, 3), - [kUpb_CType_Double] = 3, - [kUpb_CType_Int64] = 3, - [kUpb_CType_UInt64] = 3, - [kUpb_CType_String] = UPB_SIZE(3, 4), - [kUpb_CType_Bytes] = UPB_SIZE(3, 4), -}; - upb_Array* upb_Array_New(upb_Arena* a, upb_CType type) { - return _upb_Array_New(a, 4, _upb_Array_CTypeSizeLg2(type)); + return _upb_Array_New(a, 4, upb_SizeLog2_CType(type)); } const void* upb_Array_DataPtr(const upb_Array* arr) { diff --git a/upb/message/copy.c b/upb/message/copy.c index 2747a0ce13..67e3fee9b6 100644 --- a/upb/message/copy.c +++ b/upb/message/copy.c @@ -14,10 +14,21 @@ #include "upb/base/string_view.h" #include "upb/mem/arena.h" #include "upb/message/accessors.h" +#include "upb/message/array.h" +#include "upb/message/internal/accessors.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/size_log2.h" +#include "upb/message/map.h" #include "upb/message/message.h" +#include "upb/message/tagged_ptr.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" // Must be last. #include "upb/port/def.inc" @@ -132,7 +143,7 @@ upb_Array* upb_Array_DeepClone(const upb_Array* array, upb_CType value_type, const upb_MiniTable* sub, upb_Arena* arena) { size_t size = array->size; upb_Array* cloned_array = - _upb_Array_New(arena, size, _upb_Array_CTypeSizeLg2(value_type)); + _upb_Array_New(arena, size, upb_SizeLog2_CType(value_type)); if (!cloned_array) { return NULL; } diff --git a/upb/message/internal/accessors.h b/upb/message/internal/accessors.h index 2ec295a22c..bc7dd59070 100644 --- a/upb/message/internal/accessors.h +++ b/upb/message/internal/accessors.h @@ -8,9 +8,23 @@ #ifndef UPB_MESSAGE_INTERNAL_ACCESSORS_H_ #define UPB_MESSAGE_INTERNAL_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/internal/extension.h" #include "upb/message/internal/map.h" #include "upb/message/internal/message.h" +#include "upb/message/internal/size_log2.h" +#include "upb/message/internal/types.h" +#include "upb/message/map.h" +#include "upb/message/message.h" +#include "upb/message/tagged_ptr.h" +#include "upb/mini_table/extension.h" +#include "upb/mini_table/field.h" #include "upb/mini_table/internal/field.h" // Must be last. @@ -156,28 +170,8 @@ UPB_INLINE void _upb_MiniTable_CopyFieldData(void* to, const void* from, UPB_INLINE size_t _upb_MiniTable_ElementSizeLg2(const upb_MiniTableField* field) { - const unsigned char table[] = { - 0, - 3, // kUpb_FieldType_Double = 1, - 2, // kUpb_FieldType_Float = 2, - 3, // kUpb_FieldType_Int64 = 3, - 3, // kUpb_FieldType_UInt64 = 4, - 2, // kUpb_FieldType_Int32 = 5, - 3, // kUpb_FieldType_Fixed64 = 6, - 2, // kUpb_FieldType_Fixed32 = 7, - 0, // kUpb_FieldType_Bool = 8, - UPB_SIZE(3, 4), // kUpb_FieldType_String = 9, - UPB_SIZE(2, 3), // kUpb_FieldType_Group = 10, - UPB_SIZE(2, 3), // kUpb_FieldType_Message = 11, - UPB_SIZE(3, 4), // kUpb_FieldType_Bytes = 12, - 2, // kUpb_FieldType_UInt32 = 13, - 2, // kUpb_FieldType_Enum = 14, - 2, // kUpb_FieldType_SFixed32 = 15, - 3, // kUpb_FieldType_SFixed64 = 16, - 2, // kUpb_FieldType_SInt32 = 17, - 3, // kUpb_FieldType_SInt64 = 18, - }; - return table[field->UPB_PRIVATE(descriptortype)]; + return upb_SizeLog2_FieldType( + (upb_FieldType)field->UPB_PRIVATE(descriptortype)); } // Here we define universal getter/setter functions for message fields. diff --git a/upb/message/internal/array.h b/upb/message/internal/array.h index e8e8429cfd..c0fbc75f79 100644 --- a/upb/message/internal/array.h +++ b/upb/message/internal/array.h @@ -77,12 +77,6 @@ UPB_INLINE uintptr_t _upb_tag_arrptr(void* ptr, int elem_size_lg2) { return (uintptr_t)ptr | (unsigned)elem_size_lg2; } -extern const char _upb_Array_CTypeSizeLg2Table[]; - -UPB_INLINE size_t _upb_Array_CTypeSizeLg2(upb_CType ctype) { - return _upb_Array_CTypeSizeLg2Table[ctype]; -} - UPB_INLINE upb_Array* _upb_Array_New(upb_Arena* a, size_t init_capacity, int elem_size_lg2) { UPB_ASSERT(elem_size_lg2 <= 4); diff --git a/upb/message/internal/size_log2.h b/upb/message/internal/size_log2.h new file mode 100644 index 0000000000..c263012460 --- /dev/null +++ b/upb/message/internal/size_log2.h @@ -0,0 +1,77 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2023 Google LLC. All rights reserved. +// https://developers.google.com/protocol-buffers/ +/// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +#ifndef UPB_MESSAGE_INTERNAL_SIZE_LOG2_H_ +#define UPB_MESSAGE_INTERNAL_SIZE_LOG2_H_ + +#include +#include + +#include "upb/base/descriptor_constants.h" + +// Must be last. +#include "upb/port/def.inc" + +#ifdef __cplusplus +extern "C" { +#endif + +// Return the log2 of the storage size in bytes for a upb_CType +UPB_INLINE int upb_SizeLog2_CType(upb_CType c_type) { + static const int8_t size[] = { + 0, // kUpb_CType_Bool + 2, // kUpb_CType_Float + 2, // kUpb_CType_Int32 + 2, // kUpb_CType_UInt32 + 2, // kUpb_CType_Enum + UPB_SIZE(2, 3), // kUpb_CType_Message + 3, // kUpb_CType_Double + 3, // kUpb_CType_Int64 + 3, // kUpb_CType_UInt64 + UPB_SIZE(3, 4), // kUpb_CType_String + UPB_SIZE(3, 4), // kUpb_CType_Bytes + }; + + // -1 here because the enum is one-based but the table is zero-based. + return size[c_type - 1]; +} + +// Return the log2 of the storage size in bytes for a upb_FieldType +UPB_INLINE int upb_SizeLog2_FieldType(upb_FieldType field_type) { + static const int8_t size[] = { + 3, // kUpb_FieldType_Double + 2, // kUpb_FieldType_Float + 3, // kUpb_FieldType_Int64 + 3, // kUpb_FieldType_UInt64 + 2, // kUpb_FieldType_Int32 + 3, // kUpb_FieldType_Fixed64 + 2, // kUpb_FieldType_Fixed32 + 0, // kUpb_FieldType_Bool + UPB_SIZE(3, 4), // kUpb_FieldType_String + UPB_SIZE(2, 3), // kUpb_FieldType_Group + UPB_SIZE(2, 3), // kUpb_FieldType_Message + UPB_SIZE(3, 4), // kUpb_FieldType_Bytes + 2, // kUpb_FieldType_UInt32 + 2, // kUpb_FieldType_Enum + 2, // kUpb_FieldType_SFixed32 + 3, // kUpb_FieldType_SFixed64 + 2, // kUpb_FieldType_SInt32 + 3, // kUpb_FieldType_SInt64 + }; + + // -1 here because the enum is one-based but the table is zero-based. + return size[field_type - 1]; +} + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#include "upb/port/undef.inc" + +#endif /* UPB_MESSAGE_INTERNAL_SIZE_LOG2_H_ */ diff --git a/upb/mini_table/field.h b/upb/mini_table/field.h index 7f382c4e59..c744dd9d66 100644 --- a/upb/mini_table/field.h +++ b/upb/mini_table/field.h @@ -8,6 +8,7 @@ #ifndef UPB_MINI_TABLE_FIELD_H_ #define UPB_MINI_TABLE_FIELD_H_ +#include "upb/base/descriptor_constants.h" #include "upb/mini_table/internal/field.h" #include "upb/mini_table/internal/message.h" #include "upb/mini_table/internal/sub.h" @@ -36,38 +37,7 @@ upb_MiniTableField_Type(const upb_MiniTableField* field) { } UPB_API_INLINE upb_CType upb_MiniTableField_CType(const upb_MiniTableField* f) { - switch (upb_MiniTableField_Type(f)) { - case kUpb_FieldType_Double: - return kUpb_CType_Double; - case kUpb_FieldType_Float: - return kUpb_CType_Float; - case kUpb_FieldType_Int64: - case kUpb_FieldType_SInt64: - case kUpb_FieldType_SFixed64: - return kUpb_CType_Int64; - case kUpb_FieldType_Int32: - case kUpb_FieldType_SFixed32: - case kUpb_FieldType_SInt32: - return kUpb_CType_Int32; - case kUpb_FieldType_UInt64: - case kUpb_FieldType_Fixed64: - return kUpb_CType_UInt64; - case kUpb_FieldType_UInt32: - case kUpb_FieldType_Fixed32: - return kUpb_CType_UInt32; - case kUpb_FieldType_Enum: - return kUpb_CType_Enum; - case kUpb_FieldType_Bool: - return kUpb_CType_Bool; - case kUpb_FieldType_String: - return kUpb_CType_String; - case kUpb_FieldType_Bytes: - return kUpb_CType_Bytes; - case kUpb_FieldType_Group: - case kUpb_FieldType_Message: - return kUpb_CType_Message; - } - UPB_UNREACHABLE(); + return upb_FieldType_CType(upb_MiniTableField_Type(f)); } UPB_API_INLINE bool upb_MiniTableField_IsExtension( diff --git a/upb/reflection/field_def.c b/upb/reflection/field_def.c index e568bf798d..87d989e96a 100644 --- a/upb/reflection/field_def.c +++ b/upb/reflection/field_def.c @@ -10,14 +10,26 @@ #include #include #include +#include +#include +#include #include "upb/base/descriptor_constants.h" +#include "upb/base/string_view.h" +#include "upb/mem/arena.h" #include "upb/message/accessors.h" +#include "upb/message/value.h" #include "upb/mini_descriptor/decode.h" +#include "upb/mini_descriptor/internal/encode.h" #include "upb/mini_descriptor/internal/modifiers.h" +#include "upb/mini_table/extension.h" +#include "upb/mini_table/field.h" +#include "upb/mini_table/message.h" +#include "upb/mini_table/sub.h" #include "upb/reflection/def.h" #include "upb/reflection/def_type.h" #include "upb/reflection/internal/def_builder.h" +#include "upb/reflection/internal/def_pool.h" #include "upb/reflection/internal/desc_state.h" #include "upb/reflection/internal/enum_def.h" #include "upb/reflection/internal/file_def.h" @@ -94,38 +106,7 @@ const char* upb_FieldDef_FullName(const upb_FieldDef* f) { } upb_CType upb_FieldDef_CType(const upb_FieldDef* f) { - switch (f->type_) { - case kUpb_FieldType_Double: - return kUpb_CType_Double; - case kUpb_FieldType_Float: - return kUpb_CType_Float; - case kUpb_FieldType_Int64: - case kUpb_FieldType_SInt64: - case kUpb_FieldType_SFixed64: - return kUpb_CType_Int64; - case kUpb_FieldType_Int32: - case kUpb_FieldType_SFixed32: - case kUpb_FieldType_SInt32: - return kUpb_CType_Int32; - case kUpb_FieldType_UInt64: - case kUpb_FieldType_Fixed64: - return kUpb_CType_UInt64; - case kUpb_FieldType_UInt32: - case kUpb_FieldType_Fixed32: - return kUpb_CType_UInt32; - case kUpb_FieldType_Enum: - return kUpb_CType_Enum; - case kUpb_FieldType_Bool: - return kUpb_CType_Bool; - case kUpb_FieldType_String: - return kUpb_CType_String; - case kUpb_FieldType_Bytes: - return kUpb_CType_Bytes; - case kUpb_FieldType_Group: - case kUpb_FieldType_Message: - return kUpb_CType_Message; - } - UPB_UNREACHABLE(); + return upb_FieldType_CType(f->type_); } upb_FieldType upb_FieldDef_Type(const upb_FieldDef* f) { diff --git a/upb/wire/decode.c b/upb/wire/decode.c index c528c912f2..b51bcb71d4 100644 --- a/upb/wire/decode.c +++ b/upb/wire/decode.c @@ -25,6 +25,7 @@ #include "upb/message/internal/map.h" #include "upb/message/internal/map_entry.h" #include "upb/message/internal/message.h" +#include "upb/message/internal/size_log2.h" #include "upb/message/map.h" #include "upb/message/message.h" #include "upb/message/tagged_ptr.h" @@ -486,30 +487,8 @@ static const char* _upb_Decoder_DecodeEnumPacked( upb_Array* _upb_Decoder_CreateArray(upb_Decoder* d, const upb_MiniTableField* field) { - /* Maps descriptor type -> elem_size_lg2. */ - static const uint8_t kElemSizeLg2[] = { - [0] = -1, // invalid descriptor type - [kUpb_FieldType_Double] = 3, - [kUpb_FieldType_Float] = 2, - [kUpb_FieldType_Int64] = 3, - [kUpb_FieldType_UInt64] = 3, - [kUpb_FieldType_Int32] = 2, - [kUpb_FieldType_Fixed64] = 3, - [kUpb_FieldType_Fixed32] = 2, - [kUpb_FieldType_Bool] = 0, - [kUpb_FieldType_String] = UPB_SIZE(3, 4), - [kUpb_FieldType_Group] = UPB_SIZE(2, 3), - [kUpb_FieldType_Message] = UPB_SIZE(2, 3), - [kUpb_FieldType_Bytes] = UPB_SIZE(3, 4), - [kUpb_FieldType_UInt32] = 2, - [kUpb_FieldType_Enum] = 2, - [kUpb_FieldType_SFixed32] = 2, - [kUpb_FieldType_SFixed64] = 3, - [kUpb_FieldType_SInt32] = 2, - [kUpb_FieldType_SInt64] = 3, - }; - - size_t lg2 = kElemSizeLg2[field->UPB_PRIVATE(descriptortype)]; + const upb_FieldType field_type = field->UPB_PRIVATE(descriptortype); + const size_t lg2 = upb_SizeLog2_FieldType(field_type); upb_Array* ret = _upb_Array_New(&d->arena, 4, lg2); if (!ret) _upb_Decoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory); return ret;