Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:25:23

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_MESSAGE_H_
0009 #define UPB_MINI_TABLE_INTERNAL_MESSAGE_H_
0010 
0011 #include <stddef.h>
0012 #include <stdint.h>
0013 
0014 #include "upb/base/descriptor_constants.h"
0015 #include "upb/mini_table/internal/field.h"
0016 #include "upb/mini_table/internal/sub.h"
0017 
0018 // Must be last.
0019 #include "upb/port/def.inc"
0020 
0021 struct upb_Decoder;
0022 struct upb_Message;
0023 typedef const char* _upb_FieldParser(struct upb_Decoder* d, const char* ptr,
0024                                      struct upb_Message* msg, intptr_t table,
0025                                      uint64_t hasbits, uint64_t data);
0026 typedef struct {
0027   uint64_t field_data;
0028   _upb_FieldParser* field_parser;
0029 } _upb_FastTable_Entry;
0030 
0031 typedef enum {
0032   kUpb_ExtMode_NonExtendable = 0,  // Non-extendable message.
0033   kUpb_ExtMode_Extendable = 1,     // Normal extendable message.
0034   kUpb_ExtMode_IsMessageSet = 2,   // MessageSet message.
0035   kUpb_ExtMode_IsMessageSet_ITEM =
0036       3,  // MessageSet item (temporary only, see decode.c)
0037 
0038   // During table building we steal a bit to indicate that the message is a map
0039   // entry.  *Only* used during table building!
0040   kUpb_ExtMode_IsMapEntry = 4,
0041 } upb_ExtMode;
0042 
0043 // upb_MiniTable represents the memory layout of a given upb_MessageDef.
0044 // The members are public so generated code can initialize them,
0045 // but users MUST NOT directly read or write any of its members.
0046 
0047 // LINT.IfChange(minitable_struct_definition)
0048 struct upb_MiniTable {
0049   const upb_MiniTableSubInternal* UPB_PRIVATE(subs);
0050   const struct upb_MiniTableField* UPB_ONLYBITS(fields);
0051 
0052   // Must be aligned to sizeof(void*). Doesn't include internal members like
0053   // unknown fields, extension dict, pointer to msglayout, etc.
0054   uint16_t UPB_PRIVATE(size);
0055 
0056   uint16_t UPB_ONLYBITS(field_count);
0057 
0058   uint8_t UPB_PRIVATE(ext);  // upb_ExtMode, uint8_t here so sizeof(ext) == 1
0059   uint8_t UPB_PRIVATE(dense_below);
0060   uint8_t UPB_PRIVATE(table_mask);
0061   uint8_t UPB_PRIVATE(required_count);  // Required fields have the low hasbits.
0062 
0063 #ifdef UPB_TRACING_ENABLED
0064   const char* UPB_PRIVATE(full_name);
0065 #endif
0066 
0067 #ifdef UPB_FASTTABLE_ENABLED
0068   // To statically initialize the tables of variable length, we need a flexible
0069   // array member, and we need to compile in gnu99 mode (constant initialization
0070   // of flexible array members is a GNU extension, not in C99 unfortunately.
0071   _upb_FastTable_Entry UPB_PRIVATE(fasttable)[];
0072 #endif
0073 };
0074 // LINT.ThenChange(//depot/google3/third_party/upb/bits/typescript/mini_table.ts)
0075 
0076 #ifdef __cplusplus
0077 extern "C" {
0078 #endif
0079 
0080 UPB_INLINE const struct upb_MiniTable* UPB_PRIVATE(
0081     _upb_MiniTable_StrongReference)(const struct upb_MiniTable* mt) {
0082 #if defined(__GNUC__)
0083   __asm__("" : : "r"(mt));
0084 #else
0085   const struct upb_MiniTable* volatile unused = mt;
0086   (void)&unused;  // Use address to avoid an extra load of "unused".
0087 #endif
0088   return mt;
0089 }
0090 
0091 UPB_INLINE const struct upb_MiniTable* UPB_PRIVATE(_upb_MiniTable_Empty)(void) {
0092   extern const struct upb_MiniTable UPB_PRIVATE(_kUpb_MiniTable_Empty);
0093 
0094   return &UPB_PRIVATE(_kUpb_MiniTable_Empty);
0095 }
0096 
0097 UPB_API_INLINE int upb_MiniTable_FieldCount(const struct upb_MiniTable* m) {
0098   return m->UPB_ONLYBITS(field_count);
0099 }
0100 
0101 UPB_INLINE bool UPB_PRIVATE(_upb_MiniTable_IsEmpty)(
0102     const struct upb_MiniTable* m) {
0103   extern const struct upb_MiniTable UPB_PRIVATE(_kUpb_MiniTable_Empty);
0104 
0105   return m == &UPB_PRIVATE(_kUpb_MiniTable_Empty);
0106 }
0107 
0108 UPB_API_INLINE const struct upb_MiniTableField* upb_MiniTable_GetFieldByIndex(
0109     const struct upb_MiniTable* m, uint32_t i) {
0110   return &m->UPB_ONLYBITS(fields)[i];
0111 }
0112 
0113 UPB_INLINE const struct upb_MiniTable* UPB_PRIVATE(
0114     _upb_MiniTable_GetSubTableByIndex)(const struct upb_MiniTable* m,
0115                                        uint32_t i) {
0116   return *m->UPB_PRIVATE(subs)[i].UPB_PRIVATE(submsg);
0117 }
0118 
0119 UPB_API_INLINE const struct upb_MiniTable* upb_MiniTable_SubMessage(
0120     const struct upb_MiniTable* m, const struct upb_MiniTableField* f) {
0121   if (upb_MiniTableField_CType(f) != kUpb_CType_Message) {
0122     return NULL;
0123   }
0124   return UPB_PRIVATE(_upb_MiniTable_GetSubTableByIndex)(
0125       m, f->UPB_PRIVATE(submsg_index));
0126 }
0127 
0128 UPB_API_INLINE const struct upb_MiniTable* upb_MiniTable_GetSubMessageTable(
0129     const struct upb_MiniTable* m, const struct upb_MiniTableField* f) {
0130   UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message);
0131   const struct upb_MiniTable* ret = upb_MiniTable_SubMessage(m, f);
0132   UPB_ASSUME(ret);
0133   return UPB_PRIVATE(_upb_MiniTable_IsEmpty)(ret) ? NULL : ret;
0134 }
0135 
0136 UPB_API_INLINE bool upb_MiniTable_FieldIsLinked(
0137     const struct upb_MiniTable* m, const struct upb_MiniTableField* f) {
0138   return upb_MiniTable_GetSubMessageTable(m, f) != NULL;
0139 }
0140 
0141 UPB_API_INLINE const struct upb_MiniTable* upb_MiniTable_MapEntrySubMessage(
0142     const struct upb_MiniTable* m, const struct upb_MiniTableField* f) {
0143   UPB_ASSERT(upb_MiniTable_FieldIsLinked(m, f));  // Map entries must be linked.
0144   UPB_ASSERT(upb_MiniTableField_IsMap(f));        // Function precondition.
0145   return upb_MiniTable_SubMessage(m, f);
0146 }
0147 
0148 UPB_API_INLINE const struct upb_MiniTableEnum* upb_MiniTable_GetSubEnumTable(
0149     const struct upb_MiniTable* m, const struct upb_MiniTableField* f) {
0150   UPB_ASSERT(upb_MiniTableField_CType(f) == kUpb_CType_Enum);
0151   return m->UPB_PRIVATE(subs)[f->UPB_PRIVATE(submsg_index)].UPB_PRIVATE(
0152       subenum);
0153 }
0154 
0155 UPB_API_INLINE const struct upb_MiniTableField* upb_MiniTable_MapKey(
0156     const struct upb_MiniTable* m) {
0157   UPB_ASSERT(upb_MiniTable_FieldCount(m) == 2);
0158   const struct upb_MiniTableField* f = upb_MiniTable_GetFieldByIndex(m, 0);
0159   UPB_ASSERT(upb_MiniTableField_Number(f) == 1);
0160   return f;
0161 }
0162 
0163 UPB_API_INLINE const struct upb_MiniTableField* upb_MiniTable_MapValue(
0164     const struct upb_MiniTable* m) {
0165   UPB_ASSERT(upb_MiniTable_FieldCount(m) == 2);
0166   const struct upb_MiniTableField* f = upb_MiniTable_GetFieldByIndex(m, 1);
0167   UPB_ASSERT(upb_MiniTableField_Number(f) == 2);
0168   return f;
0169 }
0170 
0171 // Computes a bitmask in which the |m->required_count| lowest bits are set.
0172 //
0173 // Sample output:
0174 //    RequiredMask(1) => 0b1 (0x1)
0175 //    RequiredMask(5) => 0b11111 (0x1f)
0176 UPB_INLINE uint64_t
0177 UPB_PRIVATE(_upb_MiniTable_RequiredMask)(const struct upb_MiniTable* m) {
0178   int n = m->UPB_PRIVATE(required_count);
0179   UPB_ASSERT(0 < n && n <= 64);
0180   return (1ULL << n) - 1;
0181 }
0182 
0183 #ifdef UPB_TRACING_ENABLED
0184 UPB_INLINE const char* upb_MiniTable_FullName(
0185     const struct upb_MiniTable* mini_table) {
0186   return mini_table->UPB_PRIVATE(full_name);
0187 }
0188 // Initializes tracing proto name from language runtimes that construct
0189 // mini tables dynamically at runtime. The runtime is responsible for passing
0190 // controlling lifetime of name such as storing in same arena as mini_table.
0191 UPB_INLINE void upb_MiniTable_SetFullName(struct upb_MiniTable* mini_table,
0192                                           const char* full_name) {
0193   mini_table->UPB_PRIVATE(full_name) = full_name;
0194 }
0195 #endif
0196 
0197 #ifdef __cplusplus
0198 } /* extern "C" */
0199 #endif
0200 
0201 #include "upb/port/undef.inc"
0202 
0203 #endif /* UPB_MINI_TABLE_INTERNAL_MESSAGE_H_ */