1 /*
2 * Copyright (c) 2009-2021, Google LLC
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Google LLC nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "upb/hash/int_table.h"
29 #include "upb/hash/str_table.h"
30 #include "upb/mini_table/decode.h"
31 #include "upb/reflection/def_builder_internal.h"
32 #include "upb/reflection/def_type.h"
33 #include "upb/reflection/desc_state_internal.h"
34 #include "upb/reflection/enum_def_internal.h"
35 #include "upb/reflection/enum_reserved_range_internal.h"
36 #include "upb/reflection/enum_value_def_internal.h"
37 #include "upb/reflection/file_def_internal.h"
38 #include "upb/reflection/message_def_internal.h"
39
40 // Must be last.
41 #include "upb/port/def.inc"
42
43 struct upb_EnumDef {
44 const UPB_DESC(EnumOptions) * opts;
45 const upb_MiniTableEnum* layout; // Only for proto2.
46 const upb_FileDef* file;
47 const upb_MessageDef* containing_type; // Could be merged with "file".
48 const char* full_name;
49 upb_strtable ntoi;
50 upb_inttable iton;
51 const upb_EnumValueDef* values;
52 const upb_EnumReservedRange* res_ranges;
53 const upb_StringView* res_names;
54 int value_count;
55 int res_range_count;
56 int res_name_count;
57 int32_t defaultval;
58 bool is_closed;
59 bool is_sorted; // Whether all of the values are defined in ascending order.
60 };
61
_upb_EnumDef_At(const upb_EnumDef * e,int i)62 upb_EnumDef* _upb_EnumDef_At(const upb_EnumDef* e, int i) {
63 return (upb_EnumDef*)&e[i];
64 }
65
_upb_EnumDef_MiniTable(const upb_EnumDef * e)66 const upb_MiniTableEnum* _upb_EnumDef_MiniTable(const upb_EnumDef* e) {
67 return e->layout;
68 }
69
_upb_EnumDef_Insert(upb_EnumDef * e,upb_EnumValueDef * v,upb_Arena * a)70 bool _upb_EnumDef_Insert(upb_EnumDef* e, upb_EnumValueDef* v, upb_Arena* a) {
71 const char* name = upb_EnumValueDef_Name(v);
72 const upb_value val = upb_value_constptr(v);
73 bool ok = upb_strtable_insert(&e->ntoi, name, strlen(name), val, a);
74 if (!ok) return false;
75
76 // Multiple enumerators can have the same number, first one wins.
77 const int number = upb_EnumValueDef_Number(v);
78 if (!upb_inttable_lookup(&e->iton, number, NULL)) {
79 return upb_inttable_insert(&e->iton, number, val, a);
80 }
81 return true;
82 }
83
UPB_DESC(EnumOptions)84 const UPB_DESC(EnumOptions) * upb_EnumDef_Options(const upb_EnumDef* e) {
85 return e->opts;
86 }
87
upb_EnumDef_HasOptions(const upb_EnumDef * e)88 bool upb_EnumDef_HasOptions(const upb_EnumDef* e) {
89 return e->opts != (void*)kUpbDefOptDefault;
90 }
91
upb_EnumDef_FullName(const upb_EnumDef * e)92 const char* upb_EnumDef_FullName(const upb_EnumDef* e) { return e->full_name; }
93
upb_EnumDef_Name(const upb_EnumDef * e)94 const char* upb_EnumDef_Name(const upb_EnumDef* e) {
95 return _upb_DefBuilder_FullToShort(e->full_name);
96 }
97
upb_EnumDef_File(const upb_EnumDef * e)98 const upb_FileDef* upb_EnumDef_File(const upb_EnumDef* e) { return e->file; }
99
upb_EnumDef_ContainingType(const upb_EnumDef * e)100 const upb_MessageDef* upb_EnumDef_ContainingType(const upb_EnumDef* e) {
101 return e->containing_type;
102 }
103
upb_EnumDef_Default(const upb_EnumDef * e)104 int32_t upb_EnumDef_Default(const upb_EnumDef* e) {
105 UPB_ASSERT(upb_EnumDef_FindValueByNumber(e, e->defaultval));
106 return e->defaultval;
107 }
108
upb_EnumDef_ReservedRangeCount(const upb_EnumDef * e)109 int upb_EnumDef_ReservedRangeCount(const upb_EnumDef* e) {
110 return e->res_range_count;
111 }
112
upb_EnumDef_ReservedRange(const upb_EnumDef * e,int i)113 const upb_EnumReservedRange* upb_EnumDef_ReservedRange(const upb_EnumDef* e,
114 int i) {
115 UPB_ASSERT(0 <= i && i < e->res_range_count);
116 return _upb_EnumReservedRange_At(e->res_ranges, i);
117 }
118
upb_EnumDef_ReservedNameCount(const upb_EnumDef * e)119 int upb_EnumDef_ReservedNameCount(const upb_EnumDef* e) {
120 return e->res_name_count;
121 }
122
upb_EnumDef_ReservedName(const upb_EnumDef * e,int i)123 upb_StringView upb_EnumDef_ReservedName(const upb_EnumDef* e, int i) {
124 UPB_ASSERT(0 <= i && i < e->res_name_count);
125 return e->res_names[i];
126 }
127
upb_EnumDef_ValueCount(const upb_EnumDef * e)128 int upb_EnumDef_ValueCount(const upb_EnumDef* e) { return e->value_count; }
129
upb_EnumDef_FindValueByName(const upb_EnumDef * e,const char * name)130 const upb_EnumValueDef* upb_EnumDef_FindValueByName(const upb_EnumDef* e,
131 const char* name) {
132 return upb_EnumDef_FindValueByNameWithSize(e, name, strlen(name));
133 }
134
upb_EnumDef_FindValueByNameWithSize(const upb_EnumDef * e,const char * name,size_t size)135 const upb_EnumValueDef* upb_EnumDef_FindValueByNameWithSize(
136 const upb_EnumDef* e, const char* name, size_t size) {
137 upb_value v;
138 return upb_strtable_lookup2(&e->ntoi, name, size, &v)
139 ? upb_value_getconstptr(v)
140 : NULL;
141 }
142
upb_EnumDef_FindValueByNumber(const upb_EnumDef * e,int32_t num)143 const upb_EnumValueDef* upb_EnumDef_FindValueByNumber(const upb_EnumDef* e,
144 int32_t num) {
145 upb_value v;
146 return upb_inttable_lookup(&e->iton, num, &v) ? upb_value_getconstptr(v)
147 : NULL;
148 }
149
upb_EnumDef_CheckNumber(const upb_EnumDef * e,int32_t num)150 bool upb_EnumDef_CheckNumber(const upb_EnumDef* e, int32_t num) {
151 // We could use upb_EnumDef_FindValueByNumber(e, num) != NULL, but we expect
152 // this to be faster (especially for small numbers).
153 return upb_MiniTableEnum_CheckValue(e->layout, num);
154 }
155
upb_EnumDef_Value(const upb_EnumDef * e,int i)156 const upb_EnumValueDef* upb_EnumDef_Value(const upb_EnumDef* e, int i) {
157 UPB_ASSERT(0 <= i && i < e->value_count);
158 return _upb_EnumValueDef_At(e->values, i);
159 }
160
upb_EnumDef_IsClosed(const upb_EnumDef * e)161 bool upb_EnumDef_IsClosed(const upb_EnumDef* e) { return e->is_closed; }
162
upb_EnumDef_MiniDescriptorEncode(const upb_EnumDef * e,upb_Arena * a,upb_StringView * out)163 bool upb_EnumDef_MiniDescriptorEncode(const upb_EnumDef* e, upb_Arena* a,
164 upb_StringView* out) {
165 upb_DescState s;
166 _upb_DescState_Init(&s);
167
168 const upb_EnumValueDef** sorted = NULL;
169 if (!e->is_sorted) {
170 sorted = _upb_EnumValueDefs_Sorted(e->values, e->value_count, a);
171 if (!sorted) return false;
172 }
173
174 if (!_upb_DescState_Grow(&s, a)) return false;
175 s.ptr = upb_MtDataEncoder_StartEnum(&s.e, s.ptr);
176
177 // Duplicate values are allowed but we only encode each value once.
178 uint32_t previous = 0;
179
180 for (size_t i = 0; i < e->value_count; i++) {
181 const uint32_t current =
182 upb_EnumValueDef_Number(sorted ? sorted[i] : upb_EnumDef_Value(e, i));
183 if (i != 0 && previous == current) continue;
184
185 if (!_upb_DescState_Grow(&s, a)) return false;
186 s.ptr = upb_MtDataEncoder_PutEnumValue(&s.e, s.ptr, current);
187 previous = current;
188 }
189
190 if (!_upb_DescState_Grow(&s, a)) return false;
191 s.ptr = upb_MtDataEncoder_EndEnum(&s.e, s.ptr);
192
193 // There will always be room for this '\0' in the encoder buffer because
194 // kUpb_MtDataEncoder_MinSize is overkill for upb_MtDataEncoder_EndEnum().
195 UPB_ASSERT(s.ptr < s.buf + s.bufsize);
196 *s.ptr = '\0';
197
198 out->data = s.buf;
199 out->size = s.ptr - s.buf;
200 return true;
201 }
202
create_enumlayout(upb_DefBuilder * ctx,const upb_EnumDef * e)203 static upb_MiniTableEnum* create_enumlayout(upb_DefBuilder* ctx,
204 const upb_EnumDef* e) {
205 upb_StringView sv;
206 bool ok = upb_EnumDef_MiniDescriptorEncode(e, ctx->tmp_arena, &sv);
207 if (!ok) _upb_DefBuilder_Errf(ctx, "OOM while building enum MiniDescriptor");
208
209 upb_Status status;
210 upb_MiniTableEnum* layout =
211 upb_MiniTableEnum_Build(sv.data, sv.size, ctx->arena, &status);
212 if (!layout)
213 _upb_DefBuilder_Errf(ctx, "Error building enum MiniTable: %s", status.msg);
214 return layout;
215 }
216
_upb_EnumReservedNames_New(upb_DefBuilder * ctx,int n,const upb_StringView * protos)217 static upb_StringView* _upb_EnumReservedNames_New(
218 upb_DefBuilder* ctx, int n, const upb_StringView* protos) {
219 upb_StringView* sv = _upb_DefBuilder_Alloc(ctx, sizeof(upb_StringView) * n);
220 for (size_t i = 0; i < n; i++) {
221 sv[i].data =
222 upb_strdup2(protos[i].data, protos[i].size, _upb_DefBuilder_Arena(ctx));
223 sv[i].size = protos[i].size;
224 }
225 return sv;
226 }
227
create_enumdef(upb_DefBuilder * ctx,const char * prefix,const UPB_DESC (EnumDescriptorProto)* enum_proto,upb_EnumDef * e)228 static void create_enumdef(upb_DefBuilder* ctx, const char* prefix,
229 const UPB_DESC(EnumDescriptorProto) * enum_proto,
230 upb_EnumDef* e) {
231 const UPB_DESC(EnumValueDescriptorProto)* const* values;
232 const UPB_DESC(EnumDescriptorProto_EnumReservedRange)* const* res_ranges;
233 const upb_StringView* res_names;
234 upb_StringView name;
235 size_t n_value, n_res_range, n_res_name;
236
237 // Must happen before _upb_DefBuilder_Add()
238 e->file = _upb_DefBuilder_File(ctx);
239
240 name = UPB_DESC(EnumDescriptorProto_name)(enum_proto);
241
242 e->full_name = _upb_DefBuilder_MakeFullName(ctx, prefix, name);
243 _upb_DefBuilder_Add(ctx, e->full_name,
244 _upb_DefType_Pack(e, UPB_DEFTYPE_ENUM));
245
246 e->is_closed = (!UPB_TREAT_PROTO2_ENUMS_LIKE_PROTO3) &&
247 (upb_FileDef_Syntax(e->file) == kUpb_Syntax_Proto2);
248
249 values = UPB_DESC(EnumDescriptorProto_value)(enum_proto, &n_value);
250
251 bool ok = upb_strtable_init(&e->ntoi, n_value, ctx->arena);
252 if (!ok) _upb_DefBuilder_OomErr(ctx);
253
254 ok = upb_inttable_init(&e->iton, ctx->arena);
255 if (!ok) _upb_DefBuilder_OomErr(ctx);
256
257 e->defaultval = 0;
258 e->value_count = n_value;
259 e->values =
260 _upb_EnumValueDefs_New(ctx, prefix, n_value, values, e, &e->is_sorted);
261
262 if (n_value == 0) {
263 _upb_DefBuilder_Errf(ctx, "enums must contain at least one value (%s)",
264 e->full_name);
265 }
266
267 res_ranges =
268 UPB_DESC(EnumDescriptorProto_reserved_range)(enum_proto, &n_res_range);
269 e->res_range_count = n_res_range;
270 e->res_ranges = _upb_EnumReservedRanges_New(ctx, n_res_range, res_ranges, e);
271
272 res_names =
273 UPB_DESC(EnumDescriptorProto_reserved_name)(enum_proto, &n_res_name);
274 e->res_name_count = n_res_name;
275 e->res_names = _upb_EnumReservedNames_New(ctx, n_res_name, res_names);
276
277 UPB_DEF_SET_OPTIONS(e->opts, EnumDescriptorProto, EnumOptions, enum_proto);
278
279 upb_inttable_compact(&e->iton, ctx->arena);
280
281 if (e->is_closed) {
282 if (ctx->layout) {
283 UPB_ASSERT(ctx->enum_count < ctx->layout->enum_count);
284 e->layout = ctx->layout->enums[ctx->enum_count++];
285 } else {
286 e->layout = create_enumlayout(ctx, e);
287 }
288 } else {
289 e->layout = NULL;
290 }
291 }
292
_upb_EnumDefs_New(upb_DefBuilder * ctx,int n,const UPB_DESC (EnumDescriptorProto)* const * protos,const upb_MessageDef * containing_type)293 upb_EnumDef* _upb_EnumDefs_New(
294 upb_DefBuilder* ctx, int n,
295 const UPB_DESC(EnumDescriptorProto) * const* protos,
296 const upb_MessageDef* containing_type) {
297 _upb_DefType_CheckPadding(sizeof(upb_EnumDef));
298
299 // If a containing type is defined then get the full name from that.
300 // Otherwise use the package name from the file def.
301 const char* name = containing_type ? upb_MessageDef_FullName(containing_type)
302 : _upb_FileDef_RawPackage(ctx->file);
303
304 upb_EnumDef* e = _upb_DefBuilder_Alloc(ctx, sizeof(upb_EnumDef) * n);
305 for (size_t i = 0; i < n; i++) {
306 create_enumdef(ctx, name, protos[i], &e[i]);
307 e[i].containing_type = containing_type;
308 }
309 return e;
310 }
311