Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / third_party / upb / upb / def.h
1 /*
2 ** Defs are upb's internal representation of the constructs that can appear
3 ** in a .proto file:
4 **
5 ** - upb::MessageDefPtr (upb_msgdef): describes a "message" construct.
6 ** - upb::FieldDefPtr (upb_fielddef): describes a message field.
7 ** - upb::FileDefPtr (upb_filedef): describes a .proto file and its defs.
8 ** - upb::EnumDefPtr (upb_enumdef): describes an enum.
9 ** - upb::OneofDefPtr (upb_oneofdef): describes a oneof.
10 **
11 ** TODO: definitions of services.
12 **
13 ** This is a mixed C/C++ interface that offers a full API to both languages.
14 ** See the top-level README for more information.
15 */
16
17 #ifndef UPB_DEF_H_
18 #define UPB_DEF_H_
19
20 #include "upb/upb.h"
21 #include "upb/table.int.h"
22 #include "google/protobuf/descriptor.upb.h"
23
24 #ifdef __cplusplus
25 #include <cstring>
26 #include <memory>
27 #include <string>
28 #include <vector>
29
30 namespace upb {
31 class EnumDefPtr;
32 class FieldDefPtr;
33 class FileDefPtr;
34 class MessageDefPtr;
35 class OneofDefPtr;
36 class SymbolTable;
37 }
38 #endif
39
40 #include "upb/port_def.inc"
41
42 struct upb_enumdef;
43 typedef struct upb_enumdef upb_enumdef;
44 struct upb_fielddef;
45 typedef struct upb_fielddef upb_fielddef;
46 struct upb_filedef;
47 typedef struct upb_filedef upb_filedef;
48 struct upb_msgdef;
49 typedef struct upb_msgdef upb_msgdef;
50 struct upb_oneofdef;
51 typedef struct upb_oneofdef upb_oneofdef;
52 struct upb_symtab;
53 typedef struct upb_symtab upb_symtab;
54
55 typedef enum {
56   UPB_SYNTAX_PROTO2 = 2,
57   UPB_SYNTAX_PROTO3 = 3
58 } upb_syntax_t;
59
60 /* All the different kind of well known type messages. For simplicity of check,
61  * number wrappers and string wrappers are grouped together. Make sure the
62  * order and merber of these groups are not changed.
63  */
64 typedef enum {
65   UPB_WELLKNOWN_UNSPECIFIED,
66   UPB_WELLKNOWN_ANY,
67   UPB_WELLKNOWN_FIELDMASK,
68   UPB_WELLKNOWN_DURATION,
69   UPB_WELLKNOWN_TIMESTAMP,
70   /* number wrappers */
71   UPB_WELLKNOWN_DOUBLEVALUE,
72   UPB_WELLKNOWN_FLOATVALUE,
73   UPB_WELLKNOWN_INT64VALUE,
74   UPB_WELLKNOWN_UINT64VALUE,
75   UPB_WELLKNOWN_INT32VALUE,
76   UPB_WELLKNOWN_UINT32VALUE,
77   /* string wrappers */
78   UPB_WELLKNOWN_STRINGVALUE,
79   UPB_WELLKNOWN_BYTESVALUE,
80   UPB_WELLKNOWN_BOOLVALUE,
81   UPB_WELLKNOWN_VALUE,
82   UPB_WELLKNOWN_LISTVALUE,
83   UPB_WELLKNOWN_STRUCT
84 } upb_wellknowntype_t;
85
86 /* upb_fielddef ***************************************************************/
87
88 /* Maximum field number allowed for FieldDefs.  This is an inherent limit of the
89  * protobuf wire format. */
90 #define UPB_MAX_FIELDNUMBER ((1 << 29) - 1)
91
92 #ifdef __cplusplus
93 extern "C" {
94 #endif
95
96 const char *upb_fielddef_fullname(const upb_fielddef *f);
97 upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f);
98 upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f);
99 upb_label_t upb_fielddef_label(const upb_fielddef *f);
100 uint32_t upb_fielddef_number(const upb_fielddef *f);
101 const char *upb_fielddef_name(const upb_fielddef *f);
102 bool upb_fielddef_isextension(const upb_fielddef *f);
103 bool upb_fielddef_lazy(const upb_fielddef *f);
104 bool upb_fielddef_packed(const upb_fielddef *f);
105 size_t upb_fielddef_getjsonname(const upb_fielddef *f, char *buf, size_t len);
106 const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f);
107 const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f);
108 uint32_t upb_fielddef_index(const upb_fielddef *f);
109 bool upb_fielddef_issubmsg(const upb_fielddef *f);
110 bool upb_fielddef_isstring(const upb_fielddef *f);
111 bool upb_fielddef_isseq(const upb_fielddef *f);
112 bool upb_fielddef_isprimitive(const upb_fielddef *f);
113 bool upb_fielddef_ismap(const upb_fielddef *f);
114 int64_t upb_fielddef_defaultint64(const upb_fielddef *f);
115 int32_t upb_fielddef_defaultint32(const upb_fielddef *f);
116 uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f);
117 uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f);
118 bool upb_fielddef_defaultbool(const upb_fielddef *f);
119 float upb_fielddef_defaultfloat(const upb_fielddef *f);
120 double upb_fielddef_defaultdouble(const upb_fielddef *f);
121 const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len);
122 bool upb_fielddef_hassubdef(const upb_fielddef *f);
123 bool upb_fielddef_haspresence(const upb_fielddef *f);
124 const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f);
125 const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f);
126
127 /* Internal only. */
128 uint32_t upb_fielddef_selectorbase(const upb_fielddef *f);
129
130 #ifdef __cplusplus
131 }  /* extern "C" */
132
133 /* A upb_fielddef describes a single field in a message.  It is most often
134  * found as a part of a upb_msgdef, but can also stand alone to represent
135  * an extension. */
136 class upb::FieldDefPtr {
137  public:
138   FieldDefPtr() : ptr_(nullptr) {}
139   explicit FieldDefPtr(const upb_fielddef *ptr) : ptr_(ptr) {}
140
141   const upb_fielddef* ptr() const { return ptr_; }
142   explicit operator bool() const { return ptr_ != nullptr; }
143
144   typedef upb_fieldtype_t Type;
145   typedef upb_label_t Label;
146   typedef upb_descriptortype_t DescriptorType;
147
148   const char* full_name() const { return upb_fielddef_fullname(ptr_); }
149
150   Type type() const { return upb_fielddef_type(ptr_); }
151   Label label() const { return upb_fielddef_label(ptr_); }
152   const char* name() const { return upb_fielddef_name(ptr_); }
153   uint32_t number() const { return upb_fielddef_number(ptr_); }
154   bool is_extension() const { return upb_fielddef_isextension(ptr_); }
155
156   /* Copies the JSON name for this field into the given buffer.  Returns the
157    * actual size of the JSON name, including the NULL terminator.  If the
158    * return value is 0, the JSON name is unset.  If the return value is
159    * greater than len, the JSON name was truncated.  The buffer is always
160    * NULL-terminated if len > 0.
161    *
162    * The JSON name always defaults to a camelCased version of the regular
163    * name.  However if the regular name is unset, the JSON name will be unset
164    * also.
165    */
166   size_t GetJsonName(char *buf, size_t len) const {
167     return upb_fielddef_getjsonname(ptr_, buf, len);
168   }
169
170   /* Convenience version of the above function which copies the JSON name
171    * into the given string, returning false if the name is not set. */
172   template <class T>
173   bool GetJsonName(T* str) {
174     str->resize(GetJsonName(NULL, 0));
175     GetJsonName(&(*str)[0], str->size());
176     return str->size() > 0;
177   }
178
179   /* For UPB_TYPE_MESSAGE fields only where is_tag_delimited() == false,
180    * indicates whether this field should have lazy parsing handlers that yield
181    * the unparsed string for the submessage.
182    *
183    * TODO(haberman): I think we want to move this into a FieldOptions container
184    * when we add support for custom options (the FieldOptions struct will
185    * contain both regular FieldOptions like "lazy" *and* custom options). */
186   bool lazy() const { return upb_fielddef_lazy(ptr_); }
187
188   /* For non-string, non-submessage fields, this indicates whether binary
189    * protobufs are encoded in packed or non-packed format.
190    *
191    * TODO(haberman): see note above about putting options like this into a
192    * FieldOptions container. */
193   bool packed() const { return upb_fielddef_packed(ptr_); }
194
195   /* An integer that can be used as an index into an array of fields for
196    * whatever message this field belongs to.  Guaranteed to be less than
197    * f->containing_type()->field_count().  May only be accessed once the def has
198    * been finalized. */
199   uint32_t index() const { return upb_fielddef_index(ptr_); }
200
201   /* The MessageDef to which this field belongs.
202    *
203    * If this field has been added to a MessageDef, that message can be retrieved
204    * directly (this is always the case for frozen FieldDefs).
205    *
206    * If the field has not yet been added to a MessageDef, you can set the name
207    * of the containing type symbolically instead.  This is mostly useful for
208    * extensions, where the extension is declared separately from the message. */
209   MessageDefPtr containing_type() const;
210
211   /* The OneofDef to which this field belongs, or NULL if this field is not part
212    * of a oneof. */
213   OneofDefPtr containing_oneof() const;
214
215   /* The field's type according to the enum in descriptor.proto.  This is not
216    * the same as UPB_TYPE_*, because it distinguishes between (for example)
217    * INT32 and SINT32, whereas our "type" enum does not.  This return of
218    * descriptor_type() is a function of type(), integer_format(), and
219    * is_tag_delimited().  */
220   DescriptorType descriptor_type() const {
221     return upb_fielddef_descriptortype(ptr_);
222   }
223
224   /* Convenient field type tests. */
225   bool IsSubMessage() const { return upb_fielddef_issubmsg(ptr_); }
226   bool IsString() const { return upb_fielddef_isstring(ptr_); }
227   bool IsSequence() const { return upb_fielddef_isseq(ptr_); }
228   bool IsPrimitive() const { return upb_fielddef_isprimitive(ptr_); }
229   bool IsMap() const { return upb_fielddef_ismap(ptr_); }
230
231   /* Returns the non-string default value for this fielddef, which may either
232    * be something the client set explicitly or the "default default" (0 for
233    * numbers, empty for strings).  The field's type indicates the type of the
234    * returned value, except for enum fields that are still mutable.
235    *
236    * Requires that the given function matches the field's current type. */
237   int64_t default_int64() const { return upb_fielddef_defaultint64(ptr_); }
238   int32_t default_int32() const { return upb_fielddef_defaultint32(ptr_); }
239   uint64_t default_uint64() const { return upb_fielddef_defaultuint64(ptr_); }
240   uint32_t default_uint32() const { return upb_fielddef_defaultuint32(ptr_); }
241   bool default_bool() const { return upb_fielddef_defaultbool(ptr_); }
242   float default_float() const { return upb_fielddef_defaultfloat(ptr_); }
243   double default_double() const { return upb_fielddef_defaultdouble(ptr_); }
244
245   /* The resulting string is always NULL-terminated.  If non-NULL, the length
246    * will be stored in *len. */
247   const char *default_string(size_t * len) const {
248     return upb_fielddef_defaultstr(ptr_, len);
249   }
250
251   /* Returns the enum or submessage def for this field, if any.  The field's
252    * type must match (ie. you may only call enum_subdef() for fields where
253    * type() == UPB_TYPE_ENUM). */
254   EnumDefPtr enum_subdef() const;
255   MessageDefPtr message_subdef() const;
256
257  private:
258   const upb_fielddef *ptr_;
259 };
260
261 #endif  /* __cplusplus */
262
263 /* upb_oneofdef ***************************************************************/
264
265 #ifdef __cplusplus
266 extern "C" {
267 #endif
268
269 typedef upb_inttable_iter upb_oneof_iter;
270
271 const char *upb_oneofdef_name(const upb_oneofdef *o);
272 const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o);
273 int upb_oneofdef_numfields(const upb_oneofdef *o);
274 uint32_t upb_oneofdef_index(const upb_oneofdef *o);
275
276 /* Oneof lookups:
277  * - ntof:  look up a field by name.
278  * - ntofz: look up a field by name (as a null-terminated string).
279  * - itof:  look up a field by number. */
280 const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o,
281                                       const char *name, size_t length);
282 UPB_INLINE const upb_fielddef *upb_oneofdef_ntofz(const upb_oneofdef *o,
283                                                   const char *name) {
284   return upb_oneofdef_ntof(o, name, strlen(name));
285 }
286 const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num);
287
288 /*  upb_oneof_iter i;
289  *  for(upb_oneof_begin(&i, e); !upb_oneof_done(&i); upb_oneof_next(&i)) {
290  *    // ...
291  *  }
292  */
293 void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o);
294 void upb_oneof_next(upb_oneof_iter *iter);
295 bool upb_oneof_done(upb_oneof_iter *iter);
296 upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter);
297 void upb_oneof_iter_setdone(upb_oneof_iter *iter);
298 bool upb_oneof_iter_isequal(const upb_oneof_iter *iter1,
299                             const upb_oneof_iter *iter2);
300
301 #ifdef __cplusplus
302 }  /* extern "C" */
303
304 /* Class that represents a oneof. */
305 class upb::OneofDefPtr {
306  public:
307   OneofDefPtr() : ptr_(nullptr) {}
308   explicit OneofDefPtr(const upb_oneofdef *ptr) : ptr_(ptr) {}
309
310   const upb_oneofdef* ptr() const { return ptr_; }
311   explicit operator bool() { return ptr_ != nullptr; }
312
313   /* Returns the MessageDef that owns this OneofDef. */
314   MessageDefPtr containing_type() const;
315
316   /* Returns the name of this oneof. This is the name used to look up the oneof
317    * by name once added to a message def. */
318   const char* name() const { return upb_oneofdef_name(ptr_); }
319
320   /* Returns the number of fields currently defined in the oneof. */
321   int field_count() const { return upb_oneofdef_numfields(ptr_); }
322
323   /* Looks up by name. */
324   FieldDefPtr FindFieldByName(const char *name, size_t len) const {
325     return FieldDefPtr(upb_oneofdef_ntof(ptr_, name, len));
326   }
327   FieldDefPtr FindFieldByName(const char* name) const {
328     return FieldDefPtr(upb_oneofdef_ntofz(ptr_, name));
329   }
330
331   template <class T>
332   FieldDefPtr FindFieldByName(const T& str) const {
333     return FindFieldByName(str.c_str(), str.size());
334   }
335
336   /* Looks up by tag number. */
337   FieldDefPtr FindFieldByNumber(uint32_t num) const {
338     return FieldDefPtr(upb_oneofdef_itof(ptr_, num));
339   }
340
341   class const_iterator
342       : public std::iterator<std::forward_iterator_tag, FieldDefPtr> {
343    public:
344     void operator++() { upb_oneof_next(&iter_); }
345
346     FieldDefPtr operator*() const {
347       return FieldDefPtr(upb_oneof_iter_field(&iter_));
348     }
349
350     bool operator!=(const const_iterator& other) const {
351       return !upb_oneof_iter_isequal(&iter_, &other.iter_);
352     }
353
354     bool operator==(const const_iterator& other) const {
355       return upb_oneof_iter_isequal(&iter_, &other.iter_);
356     }
357
358    private:
359     friend class OneofDefPtr;
360
361     const_iterator() {}
362     explicit const_iterator(OneofDefPtr o) {
363       upb_oneof_begin(&iter_, o.ptr());
364     }
365     static const_iterator end() {
366       const_iterator iter;
367       upb_oneof_iter_setdone(&iter.iter_);
368       return iter;
369     }
370
371     upb_oneof_iter iter_;
372   };
373
374   const_iterator begin() const { return const_iterator(*this); }
375   const_iterator end() const { return const_iterator::end(); }
376
377  private:
378   const upb_oneofdef *ptr_;
379 };
380
381 inline upb::OneofDefPtr upb::FieldDefPtr::containing_oneof() const {
382   return OneofDefPtr(upb_fielddef_containingoneof(ptr_));
383 }
384
385 #endif  /* __cplusplus */
386
387 /* upb_msgdef *****************************************************************/
388
389 typedef upb_inttable_iter upb_msg_field_iter;
390 typedef upb_strtable_iter upb_msg_oneof_iter;
391
392 /* Well-known field tag numbers for map-entry messages. */
393 #define UPB_MAPENTRY_KEY   1
394 #define UPB_MAPENTRY_VALUE 2
395
396 /* Well-known field tag numbers for Any messages. */
397 #define UPB_ANY_TYPE 1
398 #define UPB_ANY_VALUE 2
399
400 /* Well-known field tag numbers for timestamp messages. */
401 #define UPB_DURATION_SECONDS 1
402 #define UPB_DURATION_NANOS 2
403
404 /* Well-known field tag numbers for duration messages. */
405 #define UPB_TIMESTAMP_SECONDS 1
406 #define UPB_TIMESTAMP_NANOS 2
407
408 #ifdef __cplusplus
409 extern "C" {
410 #endif
411
412 const char *upb_msgdef_fullname(const upb_msgdef *m);
413 const upb_filedef *upb_msgdef_file(const upb_msgdef *m);
414 const char *upb_msgdef_name(const upb_msgdef *m);
415 int upb_msgdef_numoneofs(const upb_msgdef *m);
416 upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m);
417 bool upb_msgdef_mapentry(const upb_msgdef *m);
418 upb_wellknowntype_t upb_msgdef_wellknowntype(const upb_msgdef *m);
419 bool upb_msgdef_isnumberwrapper(const upb_msgdef *m);
420 bool upb_msgdef_setsyntax(upb_msgdef *m, upb_syntax_t syntax);
421 const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i);
422 const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name,
423                                     size_t len);
424 const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name,
425                                     size_t len);
426 int upb_msgdef_numfields(const upb_msgdef *m);
427 int upb_msgdef_numoneofs(const upb_msgdef *m);
428
429 UPB_INLINE const upb_oneofdef *upb_msgdef_ntooz(const upb_msgdef *m,
430                                                const char *name) {
431   return upb_msgdef_ntoo(m, name, strlen(name));
432 }
433
434 UPB_INLINE const upb_fielddef *upb_msgdef_ntofz(const upb_msgdef *m,
435                                                 const char *name) {
436   return upb_msgdef_ntof(m, name, strlen(name));
437 }
438
439 /* Internal-only. */
440 size_t upb_msgdef_selectorcount(const upb_msgdef *m);
441 uint32_t upb_msgdef_submsgfieldcount(const upb_msgdef *m);
442
443 /* Lookup of either field or oneof by name.  Returns whether either was found.
444  * If the return is true, then the found def will be set, and the non-found
445  * one set to NULL. */
446 bool upb_msgdef_lookupname(const upb_msgdef *m, const char *name, size_t len,
447                            const upb_fielddef **f, const upb_oneofdef **o);
448
449 UPB_INLINE bool upb_msgdef_lookupnamez(const upb_msgdef *m, const char *name,
450                                        const upb_fielddef **f,
451                                        const upb_oneofdef **o) {
452   return upb_msgdef_lookupname(m, name, strlen(name), f, o);
453 }
454
455 /* Iteration over fields and oneofs.  For example:
456  *
457  * upb_msg_field_iter i;
458  * for(upb_msg_field_begin(&i, m);
459  *     !upb_msg_field_done(&i);
460  *     upb_msg_field_next(&i)) {
461  *   upb_fielddef *f = upb_msg_iter_field(&i);
462  *   // ...
463  * }
464  *
465  * For C we don't have separate iterators for const and non-const.
466  * It is the caller's responsibility to cast the upb_fielddef* to
467  * const if the upb_msgdef* is const. */
468 void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m);
469 void upb_msg_field_next(upb_msg_field_iter *iter);
470 bool upb_msg_field_done(const upb_msg_field_iter *iter);
471 upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter);
472 void upb_msg_field_iter_setdone(upb_msg_field_iter *iter);
473 bool upb_msg_field_iter_isequal(const upb_msg_field_iter * iter1,
474                                 const upb_msg_field_iter * iter2);
475
476 /* Similar to above, we also support iterating through the oneofs in a
477  * msgdef. */
478 void upb_msg_oneof_begin(upb_msg_oneof_iter * iter, const upb_msgdef *m);
479 void upb_msg_oneof_next(upb_msg_oneof_iter * iter);
480 bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter);
481 const upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter);
482 void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter * iter);
483 bool upb_msg_oneof_iter_isequal(const upb_msg_oneof_iter *iter1,
484                                 const upb_msg_oneof_iter *iter2);
485
486 #ifdef __cplusplus
487 }  /* extern "C" */
488
489 /* Structure that describes a single .proto message type. */
490 class upb::MessageDefPtr {
491  public:
492   MessageDefPtr() : ptr_(nullptr) {}
493   explicit MessageDefPtr(const upb_msgdef *ptr) : ptr_(ptr) {}
494
495   const upb_msgdef *ptr() const { return ptr_; }
496   explicit operator bool() const { return ptr_ != nullptr; }
497
498   const char* full_name() const { return upb_msgdef_fullname(ptr_); }
499   const char* name() const { return upb_msgdef_name(ptr_); }
500
501   /* The number of fields that belong to the MessageDef. */
502   int field_count() const { return upb_msgdef_numfields(ptr_); }
503
504   /* The number of oneofs that belong to the MessageDef. */
505   int oneof_count() const { return upb_msgdef_numoneofs(ptr_); }
506
507   upb_syntax_t syntax() const { return upb_msgdef_syntax(ptr_); }
508
509   /* These return null pointers if the field is not found. */
510   FieldDefPtr FindFieldByNumber(uint32_t number) const {
511     return FieldDefPtr(upb_msgdef_itof(ptr_, number));
512   }
513   FieldDefPtr FindFieldByName(const char* name, size_t len) const {
514     return FieldDefPtr(upb_msgdef_ntof(ptr_, name, len));
515   }
516   FieldDefPtr FindFieldByName(const char *name) const {
517     return FieldDefPtr(upb_msgdef_ntofz(ptr_, name));
518   }
519
520   template <class T>
521   FieldDefPtr FindFieldByName(const T& str) const {
522     return FindFieldByName(str.c_str(), str.size());
523   }
524
525   OneofDefPtr FindOneofByName(const char* name, size_t len) const {
526     return OneofDefPtr(upb_msgdef_ntoo(ptr_, name, len));
527   }
528
529   OneofDefPtr FindOneofByName(const char *name) const {
530     return OneofDefPtr(upb_msgdef_ntooz(ptr_, name));
531   }
532
533   template <class T>
534   OneofDefPtr FindOneofByName(const T &str) const {
535     return FindOneofByName(str.c_str(), str.size());
536   }
537
538   /* Is this message a map entry? */
539   bool mapentry() const { return upb_msgdef_mapentry(ptr_); }
540
541   /* Return the type of well known type message. UPB_WELLKNOWN_UNSPECIFIED for
542    * non-well-known message. */
543   upb_wellknowntype_t wellknowntype() const {
544     return upb_msgdef_wellknowntype(ptr_);
545   }
546
547   /* Whether is a number wrapper. */
548   bool isnumberwrapper() const { return upb_msgdef_isnumberwrapper(ptr_); }
549
550   /* Iteration over fields.  The order is undefined. */
551   class const_field_iterator
552       : public std::iterator<std::forward_iterator_tag, FieldDefPtr> {
553    public:
554     void operator++() { upb_msg_field_next(&iter_); }
555
556     FieldDefPtr operator*() const {
557       return FieldDefPtr(upb_msg_iter_field(&iter_));
558     }
559
560     bool operator!=(const const_field_iterator &other) const {
561       return !upb_msg_field_iter_isequal(&iter_, &other.iter_);
562     }
563
564     bool operator==(const const_field_iterator &other) const {
565       return upb_msg_field_iter_isequal(&iter_, &other.iter_);
566     }
567
568    private:
569     friend class MessageDefPtr;
570
571     explicit const_field_iterator() {}
572
573     explicit const_field_iterator(MessageDefPtr msg) {
574       upb_msg_field_begin(&iter_, msg.ptr());
575     }
576
577     static const_field_iterator end() {
578       const_field_iterator iter;
579       upb_msg_field_iter_setdone(&iter.iter_);
580       return iter;
581     }
582
583     upb_msg_field_iter iter_;
584   };
585
586   /* Iteration over oneofs. The order is undefined. */
587   class const_oneof_iterator
588       : public std::iterator<std::forward_iterator_tag, OneofDefPtr> {
589    public:
590
591     void operator++() { upb_msg_oneof_next(&iter_); }
592
593     OneofDefPtr operator*() const {
594       return OneofDefPtr(upb_msg_iter_oneof(&iter_));
595     }
596
597     bool operator!=(const const_oneof_iterator& other) const {
598       return !upb_msg_oneof_iter_isequal(&iter_, &other.iter_);
599     }
600
601     bool operator==(const const_oneof_iterator &other) const {
602       return upb_msg_oneof_iter_isequal(&iter_, &other.iter_);
603     }
604
605    private:
606     friend class MessageDefPtr;
607
608     const_oneof_iterator() {}
609
610     explicit const_oneof_iterator(MessageDefPtr msg) {
611       upb_msg_oneof_begin(&iter_, msg.ptr());
612     }
613
614     static const_oneof_iterator end() {
615       const_oneof_iterator iter;
616       upb_msg_oneof_iter_setdone(&iter.iter_);
617       return iter;
618     }
619
620     upb_msg_oneof_iter iter_;
621   };
622
623   class ConstFieldAccessor {
624    public:
625     explicit ConstFieldAccessor(const upb_msgdef* md) : md_(md) {}
626     const_field_iterator begin() { return MessageDefPtr(md_).field_begin(); }
627     const_field_iterator end() { return MessageDefPtr(md_).field_end(); }
628    private:
629     const upb_msgdef* md_;
630   };
631
632   class ConstOneofAccessor {
633    public:
634     explicit ConstOneofAccessor(const upb_msgdef* md) : md_(md) {}
635     const_oneof_iterator begin() { return MessageDefPtr(md_).oneof_begin(); }
636     const_oneof_iterator end() { return MessageDefPtr(md_).oneof_end(); }
637    private:
638     const upb_msgdef* md_;
639   };
640
641   const_field_iterator field_begin() const {
642     return const_field_iterator(*this);
643   }
644
645   const_field_iterator field_end() const { return const_field_iterator::end(); }
646
647   const_oneof_iterator oneof_begin() const {
648     return const_oneof_iterator(*this);
649   }
650
651   const_oneof_iterator oneof_end() const { return const_oneof_iterator::end(); }
652
653   ConstFieldAccessor fields() const { return ConstFieldAccessor(ptr()); }
654   ConstOneofAccessor oneofs() const { return ConstOneofAccessor(ptr()); }
655
656  private:
657   const upb_msgdef* ptr_;
658 };
659
660 inline upb::MessageDefPtr upb::FieldDefPtr::message_subdef() const {
661   return MessageDefPtr(upb_fielddef_msgsubdef(ptr_));
662 }
663
664 inline upb::MessageDefPtr upb::FieldDefPtr::containing_type() const {
665   return MessageDefPtr(upb_fielddef_containingtype(ptr_));
666 }
667
668 inline upb::MessageDefPtr upb::OneofDefPtr::containing_type() const {
669   return MessageDefPtr(upb_oneofdef_containingtype(ptr_));
670 }
671
672 #endif  /* __cplusplus */
673
674 /* upb_enumdef ****************************************************************/
675
676 typedef upb_strtable_iter upb_enum_iter;
677
678 const char *upb_enumdef_fullname(const upb_enumdef *e);
679 const char *upb_enumdef_name(const upb_enumdef *e);
680 const upb_filedef *upb_enumdef_file(const upb_enumdef *e);
681 int32_t upb_enumdef_default(const upb_enumdef *e);
682 int upb_enumdef_numvals(const upb_enumdef *e);
683
684 /* Enum lookups:
685  * - ntoi:  look up a name with specified length.
686  * - ntoiz: look up a name provided as a null-terminated string.
687  * - iton:  look up an integer, returning the name as a null-terminated
688  *          string. */
689 bool upb_enumdef_ntoi(const upb_enumdef *e, const char *name, size_t len,
690                       int32_t *num);
691 UPB_INLINE bool upb_enumdef_ntoiz(const upb_enumdef *e,
692                                   const char *name, int32_t *num) {
693   return upb_enumdef_ntoi(e, name, strlen(name), num);
694 }
695 const char *upb_enumdef_iton(const upb_enumdef *e, int32_t num);
696
697 /*  upb_enum_iter i;
698  *  for(upb_enum_begin(&i, e); !upb_enum_done(&i); upb_enum_next(&i)) {
699  *    // ...
700  *  }
701  */
702 void upb_enum_begin(upb_enum_iter *iter, const upb_enumdef *e);
703 void upb_enum_next(upb_enum_iter *iter);
704 bool upb_enum_done(upb_enum_iter *iter);
705 const char *upb_enum_iter_name(upb_enum_iter *iter);
706 int32_t upb_enum_iter_number(upb_enum_iter *iter);
707
708 #ifdef __cplusplus
709
710 class upb::EnumDefPtr {
711  public:
712   EnumDefPtr() : ptr_(nullptr) {}
713   explicit EnumDefPtr(const upb_enumdef* ptr) : ptr_(ptr) {}
714
715   const upb_enumdef* ptr() const { return ptr_; }
716   explicit operator bool() const { return ptr_ != nullptr; }
717
718   const char* full_name() const { return upb_enumdef_fullname(ptr_); }
719   const char* name() const { return upb_enumdef_name(ptr_); }
720
721   /* The value that is used as the default when no field default is specified.
722    * If not set explicitly, the first value that was added will be used.
723    * The default value must be a member of the enum.
724    * Requires that value_count() > 0. */
725   int32_t default_value() const { return upb_enumdef_default(ptr_); }
726
727   /* Returns the number of values currently defined in the enum.  Note that
728    * multiple names can refer to the same number, so this may be greater than
729    * the total number of unique numbers. */
730   int value_count() const { return upb_enumdef_numvals(ptr_); }
731
732   /* Lookups from name to integer, returning true if found. */
733   bool FindValueByName(const char *name, int32_t *num) const {
734     return upb_enumdef_ntoiz(ptr_, name, num);
735   }
736
737   /* Finds the name corresponding to the given number, or NULL if none was
738    * found.  If more than one name corresponds to this number, returns the
739    * first one that was added. */
740   const char *FindValueByNumber(int32_t num) const {
741     return upb_enumdef_iton(ptr_, num);
742   }
743
744   /* Iteration over name/value pairs.  The order is undefined.
745    * Adding an enum val invalidates any iterators.
746    *
747    * TODO: make compatible with range-for, with elements as pairs? */
748   class Iterator {
749    public:
750     explicit Iterator(EnumDefPtr e) { upb_enum_begin(&iter_, e.ptr()); }
751
752     int32_t number() { return upb_enum_iter_number(&iter_); }
753     const char *name() { return upb_enum_iter_name(&iter_); }
754     bool Done() { return upb_enum_done(&iter_); }
755     void Next() { return upb_enum_next(&iter_); }
756
757    private:
758     upb_enum_iter iter_;
759   };
760
761  private:
762   const upb_enumdef *ptr_;
763 };
764
765 inline upb::EnumDefPtr upb::FieldDefPtr::enum_subdef() const {
766   return EnumDefPtr(upb_fielddef_enumsubdef(ptr_));
767 }
768
769 #endif  /* __cplusplus */
770
771 /* upb_filedef ****************************************************************/
772
773 #ifdef __cplusplus
774 extern "C" {
775 #endif
776
777 const char *upb_filedef_name(const upb_filedef *f);
778 const char *upb_filedef_package(const upb_filedef *f);
779 const char *upb_filedef_phpprefix(const upb_filedef *f);
780 const char *upb_filedef_phpnamespace(const upb_filedef *f);
781 upb_syntax_t upb_filedef_syntax(const upb_filedef *f);
782 int upb_filedef_depcount(const upb_filedef *f);
783 int upb_filedef_msgcount(const upb_filedef *f);
784 int upb_filedef_enumcount(const upb_filedef *f);
785 const upb_filedef *upb_filedef_dep(const upb_filedef *f, int i);
786 const upb_msgdef *upb_filedef_msg(const upb_filedef *f, int i);
787 const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i);
788
789 #ifdef __cplusplus
790 }  /* extern "C" */
791
792 /* Class that represents a .proto file with some things defined in it.
793  *
794  * Many users won't care about FileDefs, but they are necessary if you want to
795  * read the values of file-level options. */
796 class upb::FileDefPtr {
797  public:
798   explicit FileDefPtr(const upb_filedef *ptr) : ptr_(ptr) {}
799
800   const upb_filedef* ptr() const { return ptr_; }
801   explicit operator bool() const { return ptr_ != nullptr; }
802
803   /* Get/set name of the file (eg. "foo/bar.proto"). */
804   const char* name() const { return upb_filedef_name(ptr_); }
805
806   /* Package name for definitions inside the file (eg. "foo.bar"). */
807   const char* package() const { return upb_filedef_package(ptr_); }
808
809   /* Sets the php class prefix which is prepended to all php generated classes
810    * from this .proto. Default is empty. */
811   const char* phpprefix() const { return upb_filedef_phpprefix(ptr_); }
812
813   /* Use this option to change the namespace of php generated classes. Default
814    * is empty. When this option is empty, the package name will be used for
815    * determining the namespace. */
816   const char* phpnamespace() const { return upb_filedef_phpnamespace(ptr_); }
817
818   /* Syntax for the file.  Defaults to proto2. */
819   upb_syntax_t syntax() const { return upb_filedef_syntax(ptr_); }
820
821   /* Get the list of dependencies from the file.  These are returned in the
822    * order that they were added to the FileDefPtr. */
823   int dependency_count() const { return upb_filedef_depcount(ptr_); }
824   const FileDefPtr dependency(int index) const {
825     return FileDefPtr(upb_filedef_dep(ptr_, index));
826   }
827
828  private:
829   const upb_filedef* ptr_;
830 };
831
832 #endif  /* __cplusplus */
833
834 /* upb_symtab *****************************************************************/
835
836 #ifdef __cplusplus
837 extern "C" {
838 #endif
839
840 upb_symtab *upb_symtab_new(void);
841 void upb_symtab_free(upb_symtab* s);
842 const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym);
843 const upb_msgdef *upb_symtab_lookupmsg2(
844     const upb_symtab *s, const char *sym, size_t len);
845 const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym);
846 const upb_filedef *upb_symtab_lookupfile(const upb_symtab *s, const char *name);
847 int upb_symtab_filecount(const upb_symtab *s);
848 const upb_filedef *upb_symtab_addfile(
849     upb_symtab *s, const google_protobuf_FileDescriptorProto *file,
850     upb_status *status);
851
852 /* For generated code only: loads a generated descriptor. */
853 typedef struct upb_def_init {
854   struct upb_def_init **deps;
855   const char *filename;
856   upb_strview descriptor;
857 } upb_def_init;
858
859 bool _upb_symtab_loaddefinit(upb_symtab *s, const upb_def_init *init);
860
861 #ifdef __cplusplus
862 }  /* extern "C" */
863
864 /* Non-const methods in upb::SymbolTable are NOT thread-safe. */
865 class upb::SymbolTable {
866  public:
867   SymbolTable() : ptr_(upb_symtab_new(), upb_symtab_free) {}
868   explicit SymbolTable(upb_symtab* s) : ptr_(s, upb_symtab_free) {}
869
870   const upb_symtab* ptr() const { return ptr_.get(); }
871   upb_symtab* ptr() { return ptr_.get(); }
872
873   /* Finds an entry in the symbol table with this exact name.  If not found,
874    * returns NULL. */
875   MessageDefPtr LookupMessage(const char *sym) const {
876     return MessageDefPtr(upb_symtab_lookupmsg(ptr_.get(), sym));
877   }
878
879   EnumDefPtr LookupEnum(const char *sym) const {
880     return EnumDefPtr(upb_symtab_lookupenum(ptr_.get(), sym));
881   }
882
883   FileDefPtr LookupFile(const char *name) const {
884     return FileDefPtr(upb_symtab_lookupfile(ptr_.get(), name));
885   }
886
887   /* TODO: iteration? */
888
889   /* Adds the given serialized FileDescriptorProto to the pool. */
890   FileDefPtr AddFile(const google_protobuf_FileDescriptorProto *file_proto,
891                      Status *status) {
892     return FileDefPtr(
893         upb_symtab_addfile(ptr_.get(), file_proto, status->ptr()));
894   }
895
896  private:
897   std::unique_ptr<upb_symtab, decltype(&upb_symtab_free)> ptr_;
898 };
899
900 UPB_INLINE const char* upb_safecstr(const std::string& str) {
901   UPB_ASSERT(str.size() == std::strlen(str.c_str()));
902   return str.c_str();
903 }
904
905 #endif  /* __cplusplus */
906
907 #include "upb/port_undef.inc"
908
909 #endif /* UPB_DEF_H_ */