1 /*
2 ** Defs are upb's internal representation of the constructs that can appear
3 ** in a .proto file:
4 **
5 ** - upb_msgdef: describes a "message" construct.
6 ** - upb_fielddef: describes a message field.
7 ** - upb_filedef: describes a .proto file and its defs.
8 ** - upb_enumdef: describes an enum.
9 ** - upb_oneofdef: describes a oneof.
10 **
11 ** TODO: definitions of services.
12 */
13 
14 #ifndef UPB_DEF_H_
15 #define UPB_DEF_H_
16 
17 #include "upb/upb.h"
18 #include "upb/table.int.h"
19 #include "google/protobuf/descriptor.upb.h"
20 
21 #include "upb/port_def.inc"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif  /* __cplusplus */
26 
27 struct upb_enumdef;
28 typedef struct upb_enumdef upb_enumdef;
29 struct upb_fielddef;
30 typedef struct upb_fielddef upb_fielddef;
31 struct upb_filedef;
32 typedef struct upb_filedef upb_filedef;
33 struct upb_msgdef;
34 typedef struct upb_msgdef upb_msgdef;
35 struct upb_oneofdef;
36 typedef struct upb_oneofdef upb_oneofdef;
37 struct upb_symtab;
38 typedef struct upb_symtab upb_symtab;
39 
40 typedef enum {
41   UPB_SYNTAX_PROTO2 = 2,
42   UPB_SYNTAX_PROTO3 = 3
43 } upb_syntax_t;
44 
45 /* All the different kind of well known type messages. For simplicity of check,
46  * number wrappers and string wrappers are grouped together. Make sure the
47  * order and merber of these groups are not changed.
48  */
49 typedef enum {
50   UPB_WELLKNOWN_UNSPECIFIED,
51   UPB_WELLKNOWN_ANY,
52   UPB_WELLKNOWN_FIELDMASK,
53   UPB_WELLKNOWN_DURATION,
54   UPB_WELLKNOWN_TIMESTAMP,
55   /* number wrappers */
56   UPB_WELLKNOWN_DOUBLEVALUE,
57   UPB_WELLKNOWN_FLOATVALUE,
58   UPB_WELLKNOWN_INT64VALUE,
59   UPB_WELLKNOWN_UINT64VALUE,
60   UPB_WELLKNOWN_INT32VALUE,
61   UPB_WELLKNOWN_UINT32VALUE,
62   /* string wrappers */
63   UPB_WELLKNOWN_STRINGVALUE,
64   UPB_WELLKNOWN_BYTESVALUE,
65   UPB_WELLKNOWN_BOOLVALUE,
66   UPB_WELLKNOWN_VALUE,
67   UPB_WELLKNOWN_LISTVALUE,
68   UPB_WELLKNOWN_STRUCT
69 } upb_wellknowntype_t;
70 
71 /* upb_fielddef ***************************************************************/
72 
73 /* Maximum field number allowed for FieldDefs.  This is an inherent limit of the
74  * protobuf wire format. */
75 #define UPB_MAX_FIELDNUMBER ((1 << 29) - 1)
76 
77 const char *upb_fielddef_fullname(const upb_fielddef *f);
78 upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f);
79 upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f);
80 upb_label_t upb_fielddef_label(const upb_fielddef *f);
81 uint32_t upb_fielddef_number(const upb_fielddef *f);
82 const char *upb_fielddef_name(const upb_fielddef *f);
83 const char *upb_fielddef_jsonname(const upb_fielddef *f);
84 bool upb_fielddef_isextension(const upb_fielddef *f);
85 bool upb_fielddef_lazy(const upb_fielddef *f);
86 bool upb_fielddef_packed(const upb_fielddef *f);
87 const upb_filedef *upb_fielddef_file(const upb_fielddef *f);
88 const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f);
89 const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f);
90 const upb_oneofdef *upb_fielddef_realcontainingoneof(const upb_fielddef *f);
91 uint32_t upb_fielddef_index(const upb_fielddef *f);
92 bool upb_fielddef_issubmsg(const upb_fielddef *f);
93 bool upb_fielddef_isstring(const upb_fielddef *f);
94 bool upb_fielddef_isseq(const upb_fielddef *f);
95 bool upb_fielddef_isprimitive(const upb_fielddef *f);
96 bool upb_fielddef_ismap(const upb_fielddef *f);
97 int64_t upb_fielddef_defaultint64(const upb_fielddef *f);
98 int32_t upb_fielddef_defaultint32(const upb_fielddef *f);
99 uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f);
100 uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f);
101 bool upb_fielddef_defaultbool(const upb_fielddef *f);
102 float upb_fielddef_defaultfloat(const upb_fielddef *f);
103 double upb_fielddef_defaultdouble(const upb_fielddef *f);
104 const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len);
105 bool upb_fielddef_hassubdef(const upb_fielddef *f);
106 bool upb_fielddef_haspresence(const upb_fielddef *f);
107 const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f);
108 const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f);
109 const upb_msglayout_field *upb_fielddef_layout(const upb_fielddef *f);
110 
111 /* Internal only. */
112 uint32_t upb_fielddef_selectorbase(const upb_fielddef *f);
113 
114 /* upb_oneofdef ***************************************************************/
115 
116 typedef upb_inttable_iter upb_oneof_iter;
117 
118 const char *upb_oneofdef_name(const upb_oneofdef *o);
119 const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o);
120 uint32_t upb_oneofdef_index(const upb_oneofdef *o);
121 bool upb_oneofdef_issynthetic(const upb_oneofdef *o);
122 int upb_oneofdef_fieldcount(const upb_oneofdef *o);
123 const upb_fielddef *upb_oneofdef_field(const upb_oneofdef *o, int i);
124 
125 /* Oneof lookups:
126  * - ntof:  look up a field by name.
127  * - ntofz: look up a field by name (as a null-terminated string).
128  * - itof:  look up a field by number. */
129 const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o,
130                                       const char *name, size_t length);
upb_oneofdef_ntofz(const upb_oneofdef * o,const char * name)131 UPB_INLINE const upb_fielddef *upb_oneofdef_ntofz(const upb_oneofdef *o,
132                                                   const char *name) {
133   return upb_oneofdef_ntof(o, name, strlen(name));
134 }
135 const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num);
136 
137 /* DEPRECATED, slated for removal. */
138 int upb_oneofdef_numfields(const upb_oneofdef *o);
139 void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o);
140 void upb_oneof_next(upb_oneof_iter *iter);
141 bool upb_oneof_done(upb_oneof_iter *iter);
142 upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter);
143 void upb_oneof_iter_setdone(upb_oneof_iter *iter);
144 bool upb_oneof_iter_isequal(const upb_oneof_iter *iter1,
145                             const upb_oneof_iter *iter2);
146 /* END DEPRECATED */
147 
148 /* upb_msgdef *****************************************************************/
149 
150 typedef upb_inttable_iter upb_msg_field_iter;
151 typedef upb_strtable_iter upb_msg_oneof_iter;
152 
153 /* Well-known field tag numbers for map-entry messages. */
154 #define UPB_MAPENTRY_KEY   1
155 #define UPB_MAPENTRY_VALUE 2
156 
157 /* Well-known field tag numbers for Any messages. */
158 #define UPB_ANY_TYPE 1
159 #define UPB_ANY_VALUE 2
160 
161 /* Well-known field tag numbers for timestamp messages. */
162 #define UPB_DURATION_SECONDS 1
163 #define UPB_DURATION_NANOS 2
164 
165 /* Well-known field tag numbers for duration messages. */
166 #define UPB_TIMESTAMP_SECONDS 1
167 #define UPB_TIMESTAMP_NANOS 2
168 
169 const char *upb_msgdef_fullname(const upb_msgdef *m);
170 const upb_filedef *upb_msgdef_file(const upb_msgdef *m);
171 const char *upb_msgdef_name(const upb_msgdef *m);
172 upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m);
173 bool upb_msgdef_mapentry(const upb_msgdef *m);
174 upb_wellknowntype_t upb_msgdef_wellknowntype(const upb_msgdef *m);
175 bool upb_msgdef_iswrapper(const upb_msgdef *m);
176 bool upb_msgdef_isnumberwrapper(const upb_msgdef *m);
177 int upb_msgdef_fieldcount(const upb_msgdef *m);
178 int upb_msgdef_oneofcount(const upb_msgdef *m);
179 const upb_fielddef *upb_msgdef_field(const upb_msgdef *m, int i);
180 const upb_oneofdef *upb_msgdef_oneof(const upb_msgdef *m, int i);
181 const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i);
182 const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name,
183                                     size_t len);
184 const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name,
185                                     size_t len);
186 const upb_msglayout *upb_msgdef_layout(const upb_msgdef *m);
187 
upb_msgdef_ntooz(const upb_msgdef * m,const char * name)188 UPB_INLINE const upb_oneofdef *upb_msgdef_ntooz(const upb_msgdef *m,
189                                                const char *name) {
190   return upb_msgdef_ntoo(m, name, strlen(name));
191 }
192 
upb_msgdef_ntofz(const upb_msgdef * m,const char * name)193 UPB_INLINE const upb_fielddef *upb_msgdef_ntofz(const upb_msgdef *m,
194                                                 const char *name) {
195   return upb_msgdef_ntof(m, name, strlen(name));
196 }
197 
198 /* Internal-only. */
199 size_t upb_msgdef_selectorcount(const upb_msgdef *m);
200 uint32_t upb_msgdef_submsgfieldcount(const upb_msgdef *m);
201 
202 /* Lookup of either field or oneof by name.  Returns whether either was found.
203  * If the return is true, then the found def will be set, and the non-found
204  * one set to NULL. */
205 bool upb_msgdef_lookupname(const upb_msgdef *m, const char *name, size_t len,
206                            const upb_fielddef **f, const upb_oneofdef **o);
207 
upb_msgdef_lookupnamez(const upb_msgdef * m,const char * name,const upb_fielddef ** f,const upb_oneofdef ** o)208 UPB_INLINE bool upb_msgdef_lookupnamez(const upb_msgdef *m, const char *name,
209                                        const upb_fielddef **f,
210                                        const upb_oneofdef **o) {
211   return upb_msgdef_lookupname(m, name, strlen(name), f, o);
212 }
213 
214 /* Returns a field by either JSON name or regular proto name. */
215 const upb_fielddef *upb_msgdef_lookupjsonname(const upb_msgdef *m,
216                                               const char *name, size_t len);
217 
218 /* DEPRECATED, slated for removal */
219 int upb_msgdef_numfields(const upb_msgdef *m);
220 int upb_msgdef_numoneofs(const upb_msgdef *m);
221 int upb_msgdef_numrealoneofs(const upb_msgdef *m);
222 void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m);
223 void upb_msg_field_next(upb_msg_field_iter *iter);
224 bool upb_msg_field_done(const upb_msg_field_iter *iter);
225 upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter);
226 void upb_msg_field_iter_setdone(upb_msg_field_iter *iter);
227 bool upb_msg_field_iter_isequal(const upb_msg_field_iter * iter1,
228                                 const upb_msg_field_iter * iter2);
229 void upb_msg_oneof_begin(upb_msg_oneof_iter * iter, const upb_msgdef *m);
230 void upb_msg_oneof_next(upb_msg_oneof_iter * iter);
231 bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter);
232 const upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter);
233 void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter * iter);
234 bool upb_msg_oneof_iter_isequal(const upb_msg_oneof_iter *iter1,
235                                 const upb_msg_oneof_iter *iter2);
236 /* END DEPRECATED */
237 
238 /* upb_enumdef ****************************************************************/
239 
240 typedef upb_strtable_iter upb_enum_iter;
241 
242 const char *upb_enumdef_fullname(const upb_enumdef *e);
243 const char *upb_enumdef_name(const upb_enumdef *e);
244 const upb_filedef *upb_enumdef_file(const upb_enumdef *e);
245 int32_t upb_enumdef_default(const upb_enumdef *e);
246 int upb_enumdef_numvals(const upb_enumdef *e);
247 
248 /* Enum lookups:
249  * - ntoi:  look up a name with specified length.
250  * - ntoiz: look up a name provided as a null-terminated string.
251  * - iton:  look up an integer, returning the name as a null-terminated
252  *          string. */
253 bool upb_enumdef_ntoi(const upb_enumdef *e, const char *name, size_t len,
254                       int32_t *num);
upb_enumdef_ntoiz(const upb_enumdef * e,const char * name,int32_t * num)255 UPB_INLINE bool upb_enumdef_ntoiz(const upb_enumdef *e,
256                                   const char *name, int32_t *num) {
257   return upb_enumdef_ntoi(e, name, strlen(name), num);
258 }
259 const char *upb_enumdef_iton(const upb_enumdef *e, int32_t num);
260 
261 void upb_enum_begin(upb_enum_iter *iter, const upb_enumdef *e);
262 void upb_enum_next(upb_enum_iter *iter);
263 bool upb_enum_done(upb_enum_iter *iter);
264 const char *upb_enum_iter_name(upb_enum_iter *iter);
265 int32_t upb_enum_iter_number(upb_enum_iter *iter);
266 
267 /* upb_filedef ****************************************************************/
268 
269 const char *upb_filedef_name(const upb_filedef *f);
270 const char *upb_filedef_package(const upb_filedef *f);
271 const char *upb_filedef_phpprefix(const upb_filedef *f);
272 const char *upb_filedef_phpnamespace(const upb_filedef *f);
273 upb_syntax_t upb_filedef_syntax(const upb_filedef *f);
274 int upb_filedef_depcount(const upb_filedef *f);
275 int upb_filedef_msgcount(const upb_filedef *f);
276 int upb_filedef_enumcount(const upb_filedef *f);
277 const upb_filedef *upb_filedef_dep(const upb_filedef *f, int i);
278 const upb_msgdef *upb_filedef_msg(const upb_filedef *f, int i);
279 const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i);
280 const upb_symtab *upb_filedef_symtab(const upb_filedef *f);
281 
282 /* upb_symtab *****************************************************************/
283 
284 upb_symtab *upb_symtab_new(void);
285 void upb_symtab_free(upb_symtab* s);
286 const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym);
287 const upb_msgdef *upb_symtab_lookupmsg2(
288     const upb_symtab *s, const char *sym, size_t len);
289 const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym);
290 const upb_filedef *upb_symtab_lookupfile(const upb_symtab *s, const char *name);
291 const upb_filedef *upb_symtab_lookupfile2(
292     const upb_symtab *s, const char *name, size_t len);
293 int upb_symtab_filecount(const upb_symtab *s);
294 const upb_filedef *upb_symtab_addfile(
295     upb_symtab *s, const google_protobuf_FileDescriptorProto *file,
296     upb_status *status);
297 size_t _upb_symtab_bytesloaded(const upb_symtab *s);
298 
299 /* For generated code only: loads a generated descriptor. */
300 typedef struct upb_def_init {
301   struct upb_def_init **deps;     /* Dependencies of this file. */
302   const upb_msglayout **layouts;  /* Pre-order layouts of all messages. */
303   const char *filename;
304   upb_strview descriptor;         /* Serialized descriptor. */
305 } upb_def_init;
306 
307 bool _upb_symtab_loaddefinit(upb_symtab *s, const upb_def_init *init);
308 
309 #include "upb/port_undef.inc"
310 
311 #ifdef __cplusplus
312 }  /* extern "C" */
313 #endif  /* __cplusplus */
314 
315 #endif /* UPB_DEF_H_ */
316