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