Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:13:18

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2023 Google LLC.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 #ifndef UPB_MINI_TABLE_INTERNAL_FIELD_H_
0009 #define UPB_MINI_TABLE_INTERNAL_FIELD_H_
0010 
0011 #include <stddef.h>
0012 #include <stdint.h>
0013 
0014 #include "upb/base/descriptor_constants.h"
0015 #include "upb/mini_table/internal/size_log2.h"
0016 
0017 // Must be last.
0018 #include "upb/port/def.inc"
0019 
0020 // LINT.IfChange(struct_definition)
0021 struct upb_MiniTableField {
0022   uint32_t UPB_ONLYBITS(number);
0023   uint16_t UPB_ONLYBITS(offset);
0024   int16_t presence;  // If >0, hasbit_index.  If <0, ~oneof_index
0025 
0026   // Indexes into `upb_MiniTable.subs`
0027   // Will be set to `kUpb_NoSub` if `descriptortype` != MESSAGE/GROUP/ENUM
0028   uint16_t UPB_PRIVATE(submsg_index);
0029 
0030   uint8_t UPB_PRIVATE(descriptortype);
0031 
0032   // upb_FieldMode | upb_LabelFlags | (upb_FieldRep << kUpb_FieldRep_Shift)
0033   uint8_t UPB_ONLYBITS(mode);
0034 };
0035 
0036 #define kUpb_NoSub ((uint16_t) - 1)
0037 
0038 typedef enum {
0039   kUpb_FieldMode_Map = 0,
0040   kUpb_FieldMode_Array = 1,
0041   kUpb_FieldMode_Scalar = 2,
0042 } upb_FieldMode;
0043 
0044 // Mask to isolate the upb_FieldMode from field.mode.
0045 #define kUpb_FieldMode_Mask 3
0046 
0047 // Extra flags on the mode field.
0048 typedef enum {
0049   kUpb_LabelFlags_IsPacked = 4,
0050   kUpb_LabelFlags_IsExtension = 8,
0051   // Indicates that this descriptor type is an "alternate type":
0052   //   - for Int32, this indicates that the actual type is Enum (but was
0053   //     rewritten to Int32 because it is an open enum that requires no check).
0054   //   - for Bytes, this indicates that the actual type is String (but does
0055   //     not require any UTF-8 check).
0056   kUpb_LabelFlags_IsAlternate = 16,
0057 } upb_LabelFlags;
0058 
0059 // Note: we sort by this number when calculating layout order.
0060 typedef enum {
0061   kUpb_FieldRep_1Byte = 0,
0062   kUpb_FieldRep_4Byte = 1,
0063   kUpb_FieldRep_StringView = 2,
0064   kUpb_FieldRep_8Byte = 3,
0065 
0066   kUpb_FieldRep_NativePointer =
0067       UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte),
0068   kUpb_FieldRep_Max = kUpb_FieldRep_8Byte,
0069 } upb_FieldRep;
0070 
0071 #define kUpb_FieldRep_Shift 6
0072 
0073 #ifdef __cplusplus
0074 extern "C" {
0075 #endif
0076 
0077 UPB_INLINE upb_FieldMode
0078 UPB_PRIVATE(_upb_MiniTableField_Mode)(const struct upb_MiniTableField* f) {
0079   return (upb_FieldMode)(f->UPB_ONLYBITS(mode) & kUpb_FieldMode_Mask);
0080 }
0081 
0082 UPB_INLINE upb_FieldRep
0083 UPB_PRIVATE(_upb_MiniTableField_GetRep)(const struct upb_MiniTableField* f) {
0084   return (upb_FieldRep)(f->UPB_ONLYBITS(mode) >> kUpb_FieldRep_Shift);
0085 }
0086 
0087 UPB_API_INLINE bool upb_MiniTableField_IsArray(
0088     const struct upb_MiniTableField* f) {
0089   return UPB_PRIVATE(_upb_MiniTableField_Mode)(f) == kUpb_FieldMode_Array;
0090 }
0091 
0092 UPB_API_INLINE bool upb_MiniTableField_IsMap(
0093     const struct upb_MiniTableField* f) {
0094   return UPB_PRIVATE(_upb_MiniTableField_Mode)(f) == kUpb_FieldMode_Map;
0095 }
0096 
0097 UPB_API_INLINE bool upb_MiniTableField_IsScalar(
0098     const struct upb_MiniTableField* f) {
0099   return UPB_PRIVATE(_upb_MiniTableField_Mode)(f) == kUpb_FieldMode_Scalar;
0100 }
0101 
0102 UPB_INLINE bool UPB_PRIVATE(_upb_MiniTableField_IsAlternate)(
0103     const struct upb_MiniTableField* f) {
0104   return (f->UPB_ONLYBITS(mode) & kUpb_LabelFlags_IsAlternate) != 0;
0105 }
0106 
0107 UPB_API_INLINE bool upb_MiniTableField_IsExtension(
0108     const struct upb_MiniTableField* f) {
0109   return (f->UPB_ONLYBITS(mode) & kUpb_LabelFlags_IsExtension) != 0;
0110 }
0111 
0112 UPB_API_INLINE bool upb_MiniTableField_IsPacked(
0113     const struct upb_MiniTableField* f) {
0114   return (f->UPB_ONLYBITS(mode) & kUpb_LabelFlags_IsPacked) != 0;
0115 }
0116 
0117 UPB_API_INLINE upb_FieldType
0118 upb_MiniTableField_Type(const struct upb_MiniTableField* f) {
0119   const upb_FieldType type = (upb_FieldType)f->UPB_PRIVATE(descriptortype);
0120   if (UPB_PRIVATE(_upb_MiniTableField_IsAlternate)(f)) {
0121     if (type == kUpb_FieldType_Int32) return kUpb_FieldType_Enum;
0122     if (type == kUpb_FieldType_Bytes) return kUpb_FieldType_String;
0123     UPB_ASSERT(false);
0124   }
0125   return type;
0126 }
0127 
0128 UPB_API_INLINE
0129 upb_CType upb_MiniTableField_CType(const struct upb_MiniTableField* f) {
0130   return upb_FieldType_CType(upb_MiniTableField_Type(f));
0131 }
0132 
0133 UPB_INLINE bool UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(
0134     const struct upb_MiniTableField* f) {
0135   return f->presence > 0;
0136 }
0137 
0138 UPB_INLINE char UPB_PRIVATE(_upb_MiniTableField_HasbitMask)(
0139     const struct upb_MiniTableField* f) {
0140   UPB_ASSERT(UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(f));
0141   const size_t index = f->presence;
0142   return 1 << (index % 8);
0143 }
0144 
0145 UPB_INLINE size_t UPB_PRIVATE(_upb_MiniTableField_HasbitOffset)(
0146     const struct upb_MiniTableField* f) {
0147   UPB_ASSERT(UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(f));
0148   const size_t index = f->presence;
0149   return index / 8;
0150 }
0151 
0152 UPB_API_INLINE bool upb_MiniTableField_IsClosedEnum(
0153     const struct upb_MiniTableField* f) {
0154   return f->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Enum;
0155 }
0156 
0157 UPB_API_INLINE bool upb_MiniTableField_IsInOneof(
0158     const struct upb_MiniTableField* f) {
0159   return f->presence < 0;
0160 }
0161 
0162 UPB_API_INLINE bool upb_MiniTableField_IsSubMessage(
0163     const struct upb_MiniTableField* f) {
0164   return f->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Message ||
0165          f->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Group;
0166 }
0167 
0168 UPB_API_INLINE bool upb_MiniTableField_HasPresence(
0169     const struct upb_MiniTableField* f) {
0170   if (upb_MiniTableField_IsExtension(f)) {
0171     return upb_MiniTableField_IsScalar(f);
0172   } else {
0173     return f->presence != 0;
0174   }
0175 }
0176 
0177 UPB_API_INLINE uint32_t
0178 upb_MiniTableField_Number(const struct upb_MiniTableField* f) {
0179   return f->UPB_ONLYBITS(number);
0180 }
0181 
0182 UPB_INLINE uint16_t
0183 UPB_PRIVATE(_upb_MiniTableField_Offset)(const struct upb_MiniTableField* f) {
0184   return f->UPB_ONLYBITS(offset);
0185 }
0186 
0187 UPB_INLINE size_t UPB_PRIVATE(_upb_MiniTableField_OneofOffset)(
0188     const struct upb_MiniTableField* f) {
0189   UPB_ASSERT(upb_MiniTableField_IsInOneof(f));
0190   return ~(ptrdiff_t)f->presence;
0191 }
0192 
0193 UPB_INLINE void UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(
0194     const struct upb_MiniTableField* f) {
0195   UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
0196              kUpb_FieldRep_NativePointer);
0197   UPB_ASSUME(upb_MiniTableField_IsArray(f));
0198   UPB_ASSUME(f->presence == 0);
0199 }
0200 
0201 UPB_INLINE void UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(
0202     const struct upb_MiniTableField* f) {
0203   UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
0204              kUpb_FieldRep_NativePointer);
0205   UPB_ASSUME(upb_MiniTableField_IsMap(f));
0206   UPB_ASSUME(f->presence == 0);
0207 }
0208 
0209 UPB_INLINE size_t UPB_PRIVATE(_upb_MiniTableField_ElemSizeLg2)(
0210     const struct upb_MiniTableField* f) {
0211   const upb_FieldType field_type = upb_MiniTableField_Type(f);
0212   return UPB_PRIVATE(_upb_FieldType_SizeLg2)(field_type);
0213 }
0214 
0215 // LINT.ThenChange(//depot/google3/third_party/upb/bits/typescript/mini_table_field.ts)
0216 
0217 #ifdef __cplusplus
0218 } /* extern "C" */
0219 #endif
0220 
0221 #include "upb/port/undef.inc"
0222 
0223 #endif /* UPB_MINI_TABLE_INTERNAL_FIELD_H_ */