1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 #ifndef UPB_BASE_DESCRIPTOR_CONSTANTS_H_
9 #define UPB_BASE_DESCRIPTOR_CONSTANTS_H_
10
11 // Must be last.
12 #include "upb/port/def.inc"
13
14 // The types a field can have. Note that this list is not identical to the
15 // types defined in descriptor.proto, which gives INT32 and SINT32 separate
16 // types (we distinguish the two with the "integer encoding" enum below).
17 // This enum is an internal convenience only and has no meaning outside of upb.
18 typedef enum {
19 kUpb_CType_Bool = 1,
20 kUpb_CType_Float = 2,
21 kUpb_CType_Int32 = 3,
22 kUpb_CType_UInt32 = 4,
23 kUpb_CType_Enum = 5, // Enum values are int32. TODO: rename
24 kUpb_CType_Message = 6,
25 kUpb_CType_Double = 7,
26 kUpb_CType_Int64 = 8,
27 kUpb_CType_UInt64 = 9,
28 kUpb_CType_String = 10,
29 kUpb_CType_Bytes = 11
30 } upb_CType;
31
32 // The repeated-ness of each field; this matches descriptor.proto.
33 typedef enum {
34 kUpb_Label_Optional = 1,
35 kUpb_Label_Required = 2,
36 kUpb_Label_Repeated = 3
37 } upb_Label;
38
39 // Descriptor types, as defined in descriptor.proto.
40 typedef enum {
41 kUpb_FieldType_Double = 1,
42 kUpb_FieldType_Float = 2,
43 kUpb_FieldType_Int64 = 3,
44 kUpb_FieldType_UInt64 = 4,
45 kUpb_FieldType_Int32 = 5,
46 kUpb_FieldType_Fixed64 = 6,
47 kUpb_FieldType_Fixed32 = 7,
48 kUpb_FieldType_Bool = 8,
49 kUpb_FieldType_String = 9,
50 kUpb_FieldType_Group = 10,
51 kUpb_FieldType_Message = 11,
52 kUpb_FieldType_Bytes = 12,
53 kUpb_FieldType_UInt32 = 13,
54 kUpb_FieldType_Enum = 14,
55 kUpb_FieldType_SFixed32 = 15,
56 kUpb_FieldType_SFixed64 = 16,
57 kUpb_FieldType_SInt32 = 17,
58 kUpb_FieldType_SInt64 = 18,
59 } upb_FieldType;
60
61 #define kUpb_FieldType_SizeOf 19
62
63 #ifdef __cplusplus
64 extern "C" {
65 #endif
66
67 // Convert from upb_FieldType to upb_CType
upb_FieldType_CType(upb_FieldType field_type)68 UPB_INLINE upb_CType upb_FieldType_CType(upb_FieldType field_type) {
69 static const upb_CType c_type[] = {
70 kUpb_CType_Double, // kUpb_FieldType_Double
71 kUpb_CType_Float, // kUpb_FieldType_Float
72 kUpb_CType_Int64, // kUpb_FieldType_Int64
73 kUpb_CType_UInt64, // kUpb_FieldType_UInt64
74 kUpb_CType_Int32, // kUpb_FieldType_Int32
75 kUpb_CType_UInt64, // kUpb_FieldType_Fixed64
76 kUpb_CType_UInt32, // kUpb_FieldType_Fixed32
77 kUpb_CType_Bool, // kUpb_FieldType_Bool
78 kUpb_CType_String, // kUpb_FieldType_String
79 kUpb_CType_Message, // kUpb_FieldType_Group
80 kUpb_CType_Message, // kUpb_FieldType_Message
81 kUpb_CType_Bytes, // kUpb_FieldType_Bytes
82 kUpb_CType_UInt32, // kUpb_FieldType_UInt32
83 kUpb_CType_Enum, // kUpb_FieldType_Enum
84 kUpb_CType_Int32, // kUpb_FieldType_SFixed32
85 kUpb_CType_Int64, // kUpb_FieldType_SFixed64
86 kUpb_CType_Int32, // kUpb_FieldType_SInt32
87 kUpb_CType_Int64, // kUpb_FieldType_SInt64
88 };
89
90 // -1 here because the enum is one-based but the table is zero-based.
91 return c_type[field_type - 1];
92 }
93
upb_FieldType_IsPackable(upb_FieldType field_type)94 UPB_INLINE bool upb_FieldType_IsPackable(upb_FieldType field_type) {
95 // clang-format off
96 const unsigned kUnpackableTypes =
97 (1 << kUpb_FieldType_String) |
98 (1 << kUpb_FieldType_Bytes) |
99 (1 << kUpb_FieldType_Message) |
100 (1 << kUpb_FieldType_Group);
101 // clang-format on
102 return (1 << field_type) & ~kUnpackableTypes;
103 }
104
105 #ifdef __cplusplus
106 } /* extern "C" */
107 #endif
108
109 #include "upb/port/undef.inc"
110
111 #endif /* UPB_BASE_DESCRIPTOR_CONSTANTS_H_ */
112