xref: /aosp_15_r20/external/grpc-grpc/third_party/upb/upb/reflection/internal/def_builder.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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_REFLECTION_DEF_BUILDER_INTERNAL_H_
9 #define UPB_REFLECTION_DEF_BUILDER_INTERNAL_H_
10 
11 #include "upb/reflection/common.h"
12 #include "upb/reflection/def_type.h"
13 #include "upb/reflection/internal/def_pool.h"
14 
15 // Must be last.
16 #include "upb/port/def.inc"
17 
18 // We want to copy the options verbatim into the destination options proto.
19 // We use serialize+parse as our deep copy.
20 #define UPB_DEF_SET_OPTIONS(target, desc_type, options_type, proto)           \
21   if (UPB_DESC(desc_type##_has_options)(proto)) {                             \
22     size_t size;                                                              \
23     char* pb = UPB_DESC(options_type##_serialize)(                            \
24         UPB_DESC(desc_type##_options)(proto), ctx->tmp_arena, &size);         \
25     if (!pb) _upb_DefBuilder_OomErr(ctx);                                     \
26     target =                                                                  \
27         UPB_DESC(options_type##_parse)(pb, size, _upb_DefBuilder_Arena(ctx)); \
28     if (!target) _upb_DefBuilder_OomErr(ctx);                                 \
29   } else {                                                                    \
30     target = (const UPB_DESC(options_type)*)kUpbDefOptDefault;                \
31   }
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 struct upb_DefBuilder {
38   upb_DefPool* symtab;
39   upb_strtable feature_cache;             // Caches features by identity.
40   UPB_DESC(FeatureSet*) legacy_features;  // For computing legacy features.
41   char* tmp_buf;                          // Temporary buffer in tmp_arena.
42   size_t tmp_buf_size;                    // Size of temporary buffer.
43   upb_FileDef* file;                 // File we are building.
44   upb_Arena* arena;                  // Allocate defs here.
45   upb_Arena* tmp_arena;              // For temporary allocations.
46   upb_Status* status;                // Record errors here.
47   const upb_MiniTableFile* layout;   // NULL if we should build layouts.
48   upb_MiniTablePlatform platform;    // Platform we are targeting.
49   int enum_count;                    // Count of enums built so far.
50   int msg_count;                     // Count of messages built so far.
51   int ext_count;                     // Count of extensions built so far.
52   jmp_buf err;                       // longjmp() on error.
53 };
54 
55 extern const char* kUpbDefOptDefault;
56 
57 // ctx->status has already been set elsewhere so just fail/longjmp()
58 UPB_NORETURN void _upb_DefBuilder_FailJmp(upb_DefBuilder* ctx);
59 
60 UPB_NORETURN void _upb_DefBuilder_Errf(upb_DefBuilder* ctx, const char* fmt,
61                                        ...) UPB_PRINTF(2, 3);
62 UPB_NORETURN void _upb_DefBuilder_OomErr(upb_DefBuilder* ctx);
63 
64 const char* _upb_DefBuilder_MakeFullName(upb_DefBuilder* ctx,
65                                          const char* prefix,
66                                          upb_StringView name);
67 
68 // Given a symbol and the base symbol inside which it is defined,
69 // find the symbol's definition.
70 const void* _upb_DefBuilder_ResolveAny(upb_DefBuilder* ctx,
71                                        const char* from_name_dbg,
72                                        const char* base, upb_StringView sym,
73                                        upb_deftype_t* type);
74 
75 const void* _upb_DefBuilder_Resolve(upb_DefBuilder* ctx,
76                                     const char* from_name_dbg, const char* base,
77                                     upb_StringView sym, upb_deftype_t type);
78 
79 char _upb_DefBuilder_ParseEscape(upb_DefBuilder* ctx, const upb_FieldDef* f,
80                                  const char** src, const char* end);
81 
82 const char* _upb_DefBuilder_FullToShort(const char* fullname);
83 
_upb_DefBuilder_Alloc(upb_DefBuilder * ctx,size_t bytes)84 UPB_INLINE void* _upb_DefBuilder_Alloc(upb_DefBuilder* ctx, size_t bytes) {
85   if (bytes == 0) return NULL;
86   void* ret = upb_Arena_Malloc(ctx->arena, bytes);
87   if (!ret) _upb_DefBuilder_OomErr(ctx);
88   return ret;
89 }
90 
91 // Adds a symbol |v| to the symtab, which must be a def pointer previously
92 // packed with pack_def(). The def's pointer to upb_FileDef* must be set before
93 // adding, so we know which entries to remove if building this file fails.
_upb_DefBuilder_Add(upb_DefBuilder * ctx,const char * name,upb_value v)94 UPB_INLINE void _upb_DefBuilder_Add(upb_DefBuilder* ctx, const char* name,
95                                     upb_value v) {
96   upb_StringView sym = {.data = name, .size = strlen(name)};
97   bool ok = _upb_DefPool_InsertSym(ctx->symtab, sym, v, ctx->status);
98   if (!ok) _upb_DefBuilder_FailJmp(ctx);
99 }
100 
_upb_DefBuilder_Arena(const upb_DefBuilder * ctx)101 UPB_INLINE upb_Arena* _upb_DefBuilder_Arena(const upb_DefBuilder* ctx) {
102   return ctx->arena;
103 }
104 
_upb_DefBuilder_File(const upb_DefBuilder * ctx)105 UPB_INLINE upb_FileDef* _upb_DefBuilder_File(const upb_DefBuilder* ctx) {
106   return ctx->file;
107 }
108 
109 // This version of CheckIdent() is only called by other, faster versions after
110 // they detect a parsing error.
111 void _upb_DefBuilder_CheckIdentSlow(upb_DefBuilder* ctx, upb_StringView name,
112                                     bool full);
113 
114 // Verify a full identifier string. This is slightly more complicated than
115 // verifying a relative identifier string because we must track '.' chars.
_upb_DefBuilder_CheckIdentFull(upb_DefBuilder * ctx,upb_StringView name)116 UPB_INLINE void _upb_DefBuilder_CheckIdentFull(upb_DefBuilder* ctx,
117                                                upb_StringView name) {
118   bool good = name.size > 0;
119   bool start = true;
120 
121   for (size_t i = 0; i < name.size; i++) {
122     const char c = name.data[i];
123     const char d = c | 0x20;  // force lowercase
124     const bool is_alpha = (('a' <= d) & (d <= 'z')) | (c == '_');
125     const bool is_numer = ('0' <= c) & (c <= '9') & !start;
126     const bool is_dot = (c == '.') & !start;
127 
128     good &= is_alpha | is_numer | is_dot;
129     start = is_dot;
130   }
131 
132   if (!good) _upb_DefBuilder_CheckIdentSlow(ctx, name, true);
133 }
134 
135 // Returns true if the returned feature set is new and must be populated.
136 bool _upb_DefBuilder_GetOrCreateFeatureSet(upb_DefBuilder* ctx,
137                                            const UPB_DESC(FeatureSet*) parent,
138                                            upb_StringView key,
139                                            UPB_DESC(FeatureSet**) set);
140 
141 const UPB_DESC(FeatureSet*)
142     _upb_DefBuilder_DoResolveFeatures(upb_DefBuilder* ctx,
143                                       const UPB_DESC(FeatureSet*) parent,
144                                       const UPB_DESC(FeatureSet*) child,
145                                       bool is_implicit);
146 
UPB_DESC(FeatureSet *)147 UPB_INLINE const UPB_DESC(FeatureSet*)
148     _upb_DefBuilder_ResolveFeatures(upb_DefBuilder* ctx,
149                                     const UPB_DESC(FeatureSet*) parent,
150                                     const UPB_DESC(FeatureSet*) child) {
151   return _upb_DefBuilder_DoResolveFeatures(ctx, parent, child, false);
152 }
153 
154 #ifdef __cplusplus
155 } /* extern "C" */
156 #endif
157 
158 #include "upb/port/undef.inc"
159 
160 #endif /* UPB_REFLECTION_DEF_BUILDER_INTERNAL_H_ */
161