upb: implement some upb_FieldType and upb_CType functions

PiperOrigin-RevId: 582150596
pull/14700/head
Eric Salo 1 year ago committed by Copybara-Service
parent b01bd1ba17
commit 8849deab91
  1. 31
      upb/base/descriptor_constants.h
  2. 12
      upb/message/BUILD
  3. 23
      upb/message/array.c
  4. 13
      upb/message/copy.c
  5. 38
      upb/message/internal/accessors.h
  6. 6
      upb/message/internal/array.h
  7. 77
      upb/message/internal/size_log2.h
  8. 34
      upb/mini_table/field.h
  9. 45
      upb/reflection/field_def.c
  10. 27
      upb/wire/decode.c

@ -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

@ -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,

@ -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 <string.h>
#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) {

@ -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;
}

@ -8,9 +8,23 @@
#ifndef UPB_MESSAGE_INTERNAL_ACCESSORS_H_
#define UPB_MESSAGE_INTERNAL_ACCESSORS_H_
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#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.

@ -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);

@ -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 <stddef.h>
#include <stdint.h>
#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_ */

@ -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(

@ -10,14 +10,26 @@
#include <ctype.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#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) {

@ -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;

Loading…
Cancel
Save