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 #include "upb/reflection/internal/oneof_def.h"
9
10 #include <ctype.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "upb/hash/int_table.h"
15 #include "upb/hash/str_table.h"
16 #include "upb/reflection/def_type.h"
17 #include "upb/reflection/internal/def_builder.h"
18 #include "upb/reflection/internal/field_def.h"
19 #include "upb/reflection/internal/message_def.h"
20
21 // Must be last.
22 #include "upb/port/def.inc"
23
24 struct upb_OneofDef {
25 const UPB_DESC(OneofOptions*) opts;
26 const UPB_DESC(FeatureSet*) resolved_features;
27 const upb_MessageDef* parent;
28 const char* full_name;
29 int field_count;
30 bool synthetic;
31 const upb_FieldDef** fields;
32 upb_strtable ntof; // lookup a field by name
33 upb_inttable itof; // lookup a field by number (index)
34 };
35
_upb_OneofDef_At(const upb_OneofDef * o,int i)36 upb_OneofDef* _upb_OneofDef_At(const upb_OneofDef* o, int i) {
37 return (upb_OneofDef*)&o[i];
38 }
39
UPB_DESC(OneofOptions)40 const UPB_DESC(OneofOptions) * upb_OneofDef_Options(const upb_OneofDef* o) {
41 return o->opts;
42 }
43
upb_OneofDef_HasOptions(const upb_OneofDef * o)44 bool upb_OneofDef_HasOptions(const upb_OneofDef* o) {
45 return o->opts != (void*)kUpbDefOptDefault;
46 }
47
UPB_DESC(FeatureSet)48 const UPB_DESC(FeatureSet) *
49 upb_OneofDef_ResolvedFeatures(const upb_OneofDef* o) {
50 return o->resolved_features;
51 }
52
upb_OneofDef_FullName(const upb_OneofDef * o)53 const char* upb_OneofDef_FullName(const upb_OneofDef* o) {
54 return o->full_name;
55 }
56
upb_OneofDef_Name(const upb_OneofDef * o)57 const char* upb_OneofDef_Name(const upb_OneofDef* o) {
58 return _upb_DefBuilder_FullToShort(o->full_name);
59 }
60
upb_OneofDef_ContainingType(const upb_OneofDef * o)61 const upb_MessageDef* upb_OneofDef_ContainingType(const upb_OneofDef* o) {
62 return o->parent;
63 }
64
upb_OneofDef_FieldCount(const upb_OneofDef * o)65 int upb_OneofDef_FieldCount(const upb_OneofDef* o) { return o->field_count; }
66
upb_OneofDef_Field(const upb_OneofDef * o,int i)67 const upb_FieldDef* upb_OneofDef_Field(const upb_OneofDef* o, int i) {
68 UPB_ASSERT(i < o->field_count);
69 return o->fields[i];
70 }
71
upb_OneofDef_numfields(const upb_OneofDef * o)72 int upb_OneofDef_numfields(const upb_OneofDef* o) { return o->field_count; }
73
upb_OneofDef_Index(const upb_OneofDef * o)74 uint32_t upb_OneofDef_Index(const upb_OneofDef* o) {
75 // Compute index in our parent's array.
76 return o - upb_MessageDef_Oneof(o->parent, 0);
77 }
78
upb_OneofDef_IsSynthetic(const upb_OneofDef * o)79 bool upb_OneofDef_IsSynthetic(const upb_OneofDef* o) { return o->synthetic; }
80
upb_OneofDef_LookupNameWithSize(const upb_OneofDef * o,const char * name,size_t size)81 const upb_FieldDef* upb_OneofDef_LookupNameWithSize(const upb_OneofDef* o,
82 const char* name,
83 size_t size) {
84 upb_value val;
85 return upb_strtable_lookup2(&o->ntof, name, size, &val)
86 ? upb_value_getptr(val)
87 : NULL;
88 }
89
upb_OneofDef_LookupName(const upb_OneofDef * o,const char * name)90 const upb_FieldDef* upb_OneofDef_LookupName(const upb_OneofDef* o,
91 const char* name) {
92 return upb_OneofDef_LookupNameWithSize(o, name, strlen(name));
93 }
94
upb_OneofDef_LookupNumber(const upb_OneofDef * o,uint32_t num)95 const upb_FieldDef* upb_OneofDef_LookupNumber(const upb_OneofDef* o,
96 uint32_t num) {
97 upb_value val;
98 return upb_inttable_lookup(&o->itof, num, &val) ? upb_value_getptr(val)
99 : NULL;
100 }
101
_upb_OneofDef_Insert(upb_DefBuilder * ctx,upb_OneofDef * o,const upb_FieldDef * f,const char * name,size_t size)102 void _upb_OneofDef_Insert(upb_DefBuilder* ctx, upb_OneofDef* o,
103 const upb_FieldDef* f, const char* name,
104 size_t size) {
105 o->field_count++;
106 if (_upb_FieldDef_IsProto3Optional(f)) o->synthetic = true;
107
108 const int number = upb_FieldDef_Number(f);
109 const upb_value v = upb_value_constptr(f);
110
111 // TODO: This lookup is unfortunate because we also perform it when
112 // inserting into the message's table. Unfortunately that step occurs after
113 // this one and moving things around could be tricky so let's leave it for
114 // a future refactoring.
115 const bool number_exists = upb_inttable_lookup(&o->itof, number, NULL);
116 if (UPB_UNLIKELY(number_exists)) {
117 _upb_DefBuilder_Errf(ctx, "oneof fields have the same number (%d)", number);
118 }
119
120 // TODO: More redundant work happening here.
121 const bool name_exists = upb_strtable_lookup2(&o->ntof, name, size, NULL);
122 if (UPB_UNLIKELY(name_exists)) {
123 _upb_DefBuilder_Errf(ctx, "oneof fields have the same name (%.*s)",
124 (int)size, name);
125 }
126
127 const bool ok = upb_inttable_insert(&o->itof, number, v, ctx->arena) &&
128 upb_strtable_insert(&o->ntof, name, size, v, ctx->arena);
129 if (UPB_UNLIKELY(!ok)) {
130 _upb_DefBuilder_OomErr(ctx);
131 }
132 }
133
134 // Returns the synthetic count.
_upb_OneofDefs_Finalize(upb_DefBuilder * ctx,upb_MessageDef * m)135 size_t _upb_OneofDefs_Finalize(upb_DefBuilder* ctx, upb_MessageDef* m) {
136 int synthetic_count = 0;
137
138 for (int i = 0; i < upb_MessageDef_OneofCount(m); i++) {
139 upb_OneofDef* o = (upb_OneofDef*)upb_MessageDef_Oneof(m, i);
140
141 if (o->synthetic && o->field_count != 1) {
142 _upb_DefBuilder_Errf(ctx,
143 "Synthetic oneofs must have one field, not %d: %s",
144 o->field_count, upb_OneofDef_Name(o));
145 }
146
147 if (o->synthetic) {
148 synthetic_count++;
149 } else if (synthetic_count != 0) {
150 _upb_DefBuilder_Errf(
151 ctx, "Synthetic oneofs must be after all other oneofs: %s",
152 upb_OneofDef_Name(o));
153 }
154
155 o->fields =
156 _upb_DefBuilder_Alloc(ctx, sizeof(upb_FieldDef*) * o->field_count);
157 o->field_count = 0;
158 }
159
160 for (int i = 0; i < upb_MessageDef_FieldCount(m); i++) {
161 const upb_FieldDef* f = upb_MessageDef_Field(m, i);
162 upb_OneofDef* o = (upb_OneofDef*)upb_FieldDef_ContainingOneof(f);
163 if (o) {
164 o->fields[o->field_count++] = f;
165 }
166 }
167
168 return synthetic_count;
169 }
170
create_oneofdef(upb_DefBuilder * ctx,upb_MessageDef * m,const UPB_DESC (OneofDescriptorProto *)oneof_proto,const UPB_DESC (FeatureSet *)parent_features,const upb_OneofDef * _o)171 static void create_oneofdef(upb_DefBuilder* ctx, upb_MessageDef* m,
172 const UPB_DESC(OneofDescriptorProto*) oneof_proto,
173 const UPB_DESC(FeatureSet*) parent_features,
174 const upb_OneofDef* _o) {
175 upb_OneofDef* o = (upb_OneofDef*)_o;
176
177 UPB_DEF_SET_OPTIONS(o->opts, OneofDescriptorProto, OneofOptions, oneof_proto);
178 o->resolved_features = _upb_DefBuilder_ResolveFeatures(
179 ctx, parent_features, UPB_DESC(OneofOptions_features)(o->opts));
180
181 upb_StringView name = UPB_DESC(OneofDescriptorProto_name)(oneof_proto);
182
183 o->parent = m;
184 o->full_name =
185 _upb_DefBuilder_MakeFullName(ctx, upb_MessageDef_FullName(m), name);
186 o->field_count = 0;
187 o->synthetic = false;
188
189 if (upb_MessageDef_FindByNameWithSize(m, name.data, name.size, NULL, NULL)) {
190 _upb_DefBuilder_Errf(ctx, "duplicate oneof name (%s)", o->full_name);
191 }
192
193 upb_value v = _upb_DefType_Pack(o, UPB_DEFTYPE_ONEOF);
194 bool ok = _upb_MessageDef_Insert(m, name.data, name.size, v, ctx->arena);
195 if (!ok) _upb_DefBuilder_OomErr(ctx);
196
197 ok = upb_inttable_init(&o->itof, ctx->arena);
198 if (!ok) _upb_DefBuilder_OomErr(ctx);
199
200 ok = upb_strtable_init(&o->ntof, 4, ctx->arena);
201 if (!ok) _upb_DefBuilder_OomErr(ctx);
202 }
203
204 // Allocate and initialize an array of |n| oneof defs.
_upb_OneofDefs_New(upb_DefBuilder * ctx,int n,const UPB_DESC (OneofDescriptorProto *)const * protos,const UPB_DESC (FeatureSet *)parent_features,upb_MessageDef * m)205 upb_OneofDef* _upb_OneofDefs_New(upb_DefBuilder* ctx, int n,
206 const UPB_DESC(OneofDescriptorProto*)
207 const* protos,
208 const UPB_DESC(FeatureSet*) parent_features,
209 upb_MessageDef* m) {
210 _upb_DefType_CheckPadding(sizeof(upb_OneofDef));
211
212 upb_OneofDef* o = _upb_DefBuilder_Alloc(ctx, sizeof(upb_OneofDef) * n);
213 for (int i = 0; i < n; i++) {
214 create_oneofdef(ctx, m, protos[i], parent_features, &o[i]);
215 }
216 return o;
217 }
218