1 /* Amalgamated source file */
2 
3 #if UINTPTR_MAX == 0xffffffff
4 #define UPB_SIZE(size32, size64) size32
5 #else
6 #define UPB_SIZE(size32, size64) size64
7 #endif
8 
9 #define UPB_FIELD_AT(msg, fieldtype, offset) \
10   *(fieldtype*)((const char*)(msg) + offset)
11 
12 #define UPB_READ_ONEOF(msg, fieldtype, offset, case_offset, case_val, default) \
13   UPB_FIELD_AT(msg, int, case_offset) == case_val                              \
14       ? UPB_FIELD_AT(msg, fieldtype, offset)                                   \
15       : default
16 
17 #define UPB_WRITE_ONEOF(msg, fieldtype, offset, value, case_offset, case_val) \
18   UPB_FIELD_AT(msg, int, case_offset) = case_val;                             \
19   UPB_FIELD_AT(msg, fieldtype, offset) = value;
20 /*
21 ** upb::Message is a representation for protobuf messages.
22 **
23 ** However it differs from other common representations like
24 ** google::protobuf::Message in one key way: it does not prescribe any
25 ** ownership between messages and submessages, and it relies on the
26 ** client to ensure that each submessage/array/map outlives its parent.
27 **
28 ** All messages, arrays, and maps live in an Arena.  If the entire message
29 ** tree is in the same arena, ensuring proper lifetimes is simple.  However
30 ** the client can mix arenas as long as they ensure that there are no
31 ** dangling pointers.
32 **
33 ** A client can access a upb::Message without knowing anything about
34 ** ownership semantics, but to create or mutate a message a user needs
35 ** to implement the memory management themselves.
36 **
37 ** TODO: UTF-8 checking?
38 **/
39 
40 #ifndef UPB_MSG_H_
41 #define UPB_MSG_H_
42 
43 /*
44 ** Defs are upb's internal representation of the constructs that can appear
45 ** in a .proto file:
46 **
47 ** - upb::MessageDef (upb_msgdef): describes a "message" construct.
48 ** - upb::FieldDef (upb_fielddef): describes a message field.
49 ** - upb::FileDef (upb_filedef): describes a .proto file and its defs.
50 ** - upb::EnumDef (upb_enumdef): describes an enum.
51 ** - upb::OneofDef (upb_oneofdef): describes a oneof.
52 ** - upb::Def (upb_def): base class of all the others.
53 **
54 ** TODO: definitions of services.
55 **
56 ** Like upb_refcounted objects, defs are mutable only until frozen, and are
57 ** only thread-safe once frozen.
58 **
59 ** This is a mixed C/C++ interface that offers a full API to both languages.
60 ** See the top-level README for more information.
61 */
62 
63 #ifndef UPB_DEF_H_
64 #define UPB_DEF_H_
65 
66 /*
67 ** upb::RefCounted (upb_refcounted)
68 **
69 ** A refcounting scheme that supports circular refs.  It accomplishes this by
70 ** partitioning the set of objects into groups such that no cycle spans groups;
71 ** we can then reference-count the group as a whole and ignore refs within the
72 ** group.  When objects are mutable, these groups are computed very
73 ** conservatively; we group any objects that have ever had a link between them.
74 ** When objects are frozen, we compute strongly-connected components which
75 ** allows us to be precise and only group objects that are actually cyclic.
76 **
77 ** This is a mixed C/C++ interface that offers a full API to both languages.
78 ** See the top-level README for more information.
79 */
80 
81 #ifndef UPB_REFCOUNTED_H_
82 #define UPB_REFCOUNTED_H_
83 
84 /*
85 ** upb_table
86 **
87 ** This header is INTERNAL-ONLY!  Its interfaces are not public or stable!
88 ** This file defines very fast int->upb_value (inttable) and string->upb_value
89 ** (strtable) hash tables.
90 **
91 ** The table uses chained scatter with Brent's variation (inspired by the Lua
92 ** implementation of hash tables).  The hash function for strings is Austin
93 ** Appleby's "MurmurHash."
94 **
95 ** The inttable uses uintptr_t as its key, which guarantees it can be used to
96 ** store pointers or integers of at least 32 bits (upb isn't really useful on
97 ** systems where sizeof(void*) < 4).
98 **
99 ** The table must be homogenous (all values of the same type).  In debug
100 ** mode, we check this on insert and lookup.
101 */
102 
103 #ifndef UPB_TABLE_H_
104 #define UPB_TABLE_H_
105 
106 #include <stdint.h>
107 #include <string.h>
108 /*
109 ** This file contains shared definitions that are widely used across upb.
110 **
111 ** This is a mixed C/C++ interface that offers a full API to both languages.
112 ** See the top-level README for more information.
113 */
114 
115 #ifndef UPB_H_
116 #define UPB_H_
117 
118 #include <assert.h>
119 #include <stdarg.h>
120 #include <stdbool.h>
121 #include <stddef.h>
122 
123 #ifdef __cplusplus
124 namespace upb {
125 class Allocator;
126 class Arena;
127 class Environment;
128 class ErrorSpace;
129 class Status;
130 template <int N> class InlinedArena;
131 template <int N> class InlinedEnvironment;
132 }
133 #endif
134 
135 /* UPB_INLINE: inline if possible, emit standalone code if required. */
136 #ifdef __cplusplus
137 #define UPB_INLINE inline
138 #elif defined (__GNUC__)
139 #define UPB_INLINE static __inline__
140 #else
141 #define UPB_INLINE static
142 #endif
143 
144 /* Hints to the compiler about likely/unlikely branches. */
145 #define UPB_LIKELY(x) __builtin_expect((x),1)
146 
147 /* Define UPB_BIG_ENDIAN manually if you're on big endian and your compiler
148  * doesn't provide these preprocessor symbols. */
149 #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
150 #define UPB_BIG_ENDIAN
151 #endif
152 
153 /* Macros for function attributes on compilers that support them. */
154 #ifdef __GNUC__
155 #define UPB_FORCEINLINE __inline__ __attribute__((always_inline))
156 #define UPB_NOINLINE __attribute__((noinline))
157 #define UPB_NORETURN __attribute__((__noreturn__))
158 #else  /* !defined(__GNUC__) */
159 #define UPB_FORCEINLINE
160 #define UPB_NOINLINE
161 #define UPB_NORETURN
162 #endif
163 
164 #if __STDC_VERSION__ >= 199901L || __cplusplus >= 201103L
165 /* C99/C++11 versions. */
166 #include <stdio.h>
167 #define _upb_snprintf snprintf
168 #define _upb_vsnprintf vsnprintf
169 #define _upb_va_copy(a, b) va_copy(a, b)
170 #elif defined __GNUC__
171 /* A few hacky workarounds for functions not in C89.
172  * For internal use only!
173  * TODO(haberman): fix these by including our own implementations, or finding
174  * another workaround.
175  */
176 #define _upb_snprintf __builtin_snprintf
177 #define _upb_vsnprintf __builtin_vsnprintf
178 #define _upb_va_copy(a, b) __va_copy(a, b)
179 #else
180 #error Need implementations of [v]snprintf and va_copy
181 #endif
182 
183 
184 #if ((defined(__cplusplus) && __cplusplus >= 201103L) || \
185       defined(__GXX_EXPERIMENTAL_CXX0X__)) && !defined(UPB_NO_CXX11)
186 #define UPB_CXX11
187 #endif
188 
189 /* UPB_DISALLOW_COPY_AND_ASSIGN()
190  * UPB_DISALLOW_POD_OPS()
191  *
192  * Declare these in the "private" section of a C++ class to forbid copy/assign
193  * or all POD ops (construct, destruct, copy, assign) on that class. */
194 #ifdef UPB_CXX11
195 #include <type_traits>
196 #define UPB_DISALLOW_COPY_AND_ASSIGN(class_name) \
197   class_name(const class_name&) = delete; \
198   void operator=(const class_name&) = delete;
199 #define UPB_DISALLOW_POD_OPS(class_name, full_class_name) \
200   class_name() = delete; \
201   ~class_name() = delete; \
202   UPB_DISALLOW_COPY_AND_ASSIGN(class_name)
203 #define UPB_ASSERT_STDLAYOUT(type) \
204   static_assert(std::is_standard_layout<type>::value, \
205                 #type " must be standard layout");
206 #define UPB_FINAL final
207 #else  /* !defined(UPB_CXX11) */
208 #define UPB_DISALLOW_COPY_AND_ASSIGN(class_name) \
209   class_name(const class_name&); \
210   void operator=(const class_name&);
211 #define UPB_DISALLOW_POD_OPS(class_name, full_class_name) \
212   class_name(); \
213   ~class_name(); \
214   UPB_DISALLOW_COPY_AND_ASSIGN(class_name)
215 #define UPB_ASSERT_STDLAYOUT(type)
216 #define UPB_FINAL
217 #endif
218 
219 /* UPB_DECLARE_TYPE()
220  * UPB_DECLARE_DERIVED_TYPE()
221  * UPB_DECLARE_DERIVED_TYPE2()
222  *
223  * Macros for declaring C and C++ types both, including inheritance.
224  * The inheritance doesn't use real C++ inheritance, to stay compatible with C.
225  *
226  * These macros also provide upcasts:
227  *  - in C: types-specific functions (ie. upb_foo_upcast(foo))
228  *  - in C++: upb::upcast(foo) along with implicit conversions
229  *
230  * Downcasts are not provided, but upb/def.h defines downcasts for upb::Def. */
231 
232 #define UPB_C_UPCASTS(ty, base)                                      \
233   UPB_INLINE base *ty ## _upcast_mutable(ty *p) { return (base*)p; } \
234   UPB_INLINE const base *ty ## _upcast(const ty *p) { return (const base*)p; }
235 
236 #define UPB_C_UPCASTS2(ty, base, base2)                                 \
237   UPB_C_UPCASTS(ty, base)                                               \
238   UPB_INLINE base2 *ty ## _upcast2_mutable(ty *p) { return (base2*)p; } \
239   UPB_INLINE const base2 *ty ## _upcast2(const ty *p) { return (const base2*)p; }
240 
241 #ifdef __cplusplus
242 
243 #define UPB_BEGIN_EXTERN_C extern "C" {
244 #define UPB_END_EXTERN_C }
245 #define UPB_PRIVATE_FOR_CPP private:
246 #define UPB_DECLARE_TYPE(cppname, cname) typedef cppname cname;
247 
248 #define UPB_DECLARE_DERIVED_TYPE(cppname, cppbase, cname, cbase)  \
249   UPB_DECLARE_TYPE(cppname, cname)                                \
250   UPB_C_UPCASTS(cname, cbase)                                     \
251   namespace upb {                                                 \
252   template <>                                                     \
253   class Pointer<cppname> : public PointerBase<cppname, cppbase> { \
254    public:                                                        \
255     explicit Pointer(cppname* ptr)                                \
256         : PointerBase<cppname, cppbase>(ptr) {}                   \
257   };                                                              \
258   template <>                                                     \
259   class Pointer<const cppname>                                    \
260       : public PointerBase<const cppname, const cppbase> {        \
261    public:                                                        \
262     explicit Pointer(const cppname* ptr)                          \
263         : PointerBase<const cppname, const cppbase>(ptr) {}       \
264   };                                                              \
265   }
266 
267 #define UPB_DECLARE_DERIVED_TYPE2(cppname, cppbase, cppbase2, cname, cbase,  \
268                                   cbase2)                                    \
269   UPB_DECLARE_TYPE(cppname, cname)                                           \
270   UPB_C_UPCASTS2(cname, cbase, cbase2)                                       \
271   namespace upb {                                                            \
272   template <>                                                                \
273   class Pointer<cppname> : public PointerBase2<cppname, cppbase, cppbase2> { \
274    public:                                                                   \
275     explicit Pointer(cppname* ptr)                                           \
276         : PointerBase2<cppname, cppbase, cppbase2>(ptr) {}                   \
277   };                                                                         \
278   template <>                                                                \
279   class Pointer<const cppname>                                               \
280       : public PointerBase2<const cppname, const cppbase, const cppbase2> {  \
281    public:                                                                   \
282     explicit Pointer(const cppname* ptr)                                     \
283         : PointerBase2<const cppname, const cppbase, const cppbase2>(ptr) {} \
284   };                                                                         \
285   }
286 
287 #else  /* !defined(__cplusplus) */
288 
289 #define UPB_BEGIN_EXTERN_C
290 #define UPB_END_EXTERN_C
291 #define UPB_PRIVATE_FOR_CPP
292 #define UPB_DECLARE_TYPE(cppname, cname) \
293   struct cname;                          \
294   typedef struct cname cname;
295 #define UPB_DECLARE_DERIVED_TYPE(cppname, cppbase, cname, cbase) \
296   UPB_DECLARE_TYPE(cppname, cname)                               \
297   UPB_C_UPCASTS(cname, cbase)
298 #define UPB_DECLARE_DERIVED_TYPE2(cppname, cppbase, cppbase2,    \
299                                   cname, cbase, cbase2)          \
300   UPB_DECLARE_TYPE(cppname, cname)                               \
301   UPB_C_UPCASTS2(cname, cbase, cbase2)
302 
303 #endif  /* defined(__cplusplus) */
304 
305 #define UPB_MAX(x, y) ((x) > (y) ? (x) : (y))
306 #define UPB_MIN(x, y) ((x) < (y) ? (x) : (y))
307 
308 #define UPB_UNUSED(var) (void)var
309 
310 /* UPB_ASSERT(): in release mode, we use the expression without letting it be
311  * evaluated.  This prevents "unused variable" warnings. */
312 #ifdef NDEBUG
313 #define UPB_ASSERT(expr) do {} while (false && (expr))
314 #else
315 #define UPB_ASSERT(expr) assert(expr)
316 #endif
317 
318 /* UPB_ASSERT_DEBUGVAR(): assert that uses functions or variables that only
319  * exist in debug mode.  This turns into regular assert. */
320 #define UPB_ASSERT_DEBUGVAR(expr) assert(expr)
321 
322 #ifdef __GNUC__
323 #define UPB_UNREACHABLE() do { assert(0); __builtin_unreachable(); } while(0)
324 #else
325 #define UPB_UNREACHABLE() do { assert(0); } while(0)
326 #endif
327 
328 /* Generic function type. */
329 typedef void upb_func();
330 
331 
332 /* C++ Casts ******************************************************************/
333 
334 #ifdef __cplusplus
335 
336 namespace upb {
337 
338 template <class T> class Pointer;
339 
340 /* Casts to a subclass.  The caller must know that cast is correct; an
341  * incorrect cast will throw an assertion failure in debug mode.
342  *
343  * Example:
344  *   upb::Def* def = GetDef();
345  *   // Assert-fails if this was not actually a MessageDef.
346  *   upb::MessgeDef* md = upb::down_cast<upb::MessageDef>(def);
347  *
348  * Note that downcasts are only defined for some types (at the moment you can
349  * only downcast from a upb::Def to a specific Def type). */
350 template<class To, class From> To down_cast(From* f);
351 
352 /* Casts to a subclass.  If the class does not actually match the given To type,
353  * returns NULL.
354  *
355  * Example:
356  *   upb::Def* def = GetDef();
357  *   // md will be NULL if this was not actually a MessageDef.
358  *   upb::MessgeDef* md = upb::down_cast<upb::MessageDef>(def);
359  *
360  * Note that dynamic casts are only defined for some types (at the moment you
361  * can only downcast from a upb::Def to a specific Def type).. */
362 template<class To, class From> To dyn_cast(From* f);
363 
364 /* Casts to any base class, or the type itself (ie. can be a no-op).
365  *
366  * Example:
367  *   upb::MessageDef* md = GetDef();
368  *   // This will fail to compile if this wasn't actually a base class.
369  *   upb::Def* def = upb::upcast(md);
370  */
upcast(T * f)371 template <class T> inline Pointer<T> upcast(T *f) { return Pointer<T>(f); }
372 
373 /* Attempt upcast to specific base class.
374  *
375  * Example:
376  *   upb::MessageDef* md = GetDef();
377  *   upb::upcast_to<upb::Def>(md)->MethodOnDef();
378  */
upcast_to(F * f)379 template <class T, class F> inline T* upcast_to(F *f) {
380   return static_cast<T*>(upcast(f));
381 }
382 
383 /* PointerBase<T>: implementation detail of upb::upcast().
384  * It is implicitly convertable to pointers to the Base class(es).
385  */
386 template <class T, class Base>
387 class PointerBase {
388  public:
PointerBase(T * ptr)389   explicit PointerBase(T* ptr) : ptr_(ptr) {}
390   operator T*() { return ptr_; }
391   operator Base*() { return (Base*)ptr_; }
392 
393  private:
394   T* ptr_;
395 };
396 
397 template <class T, class Base, class Base2>
398 class PointerBase2 : public PointerBase<T, Base> {
399  public:
PointerBase2(T * ptr)400   explicit PointerBase2(T* ptr) : PointerBase<T, Base>(ptr) {}
401   operator Base2*() { return Pointer<Base>(*this); }
402 };
403 
404 }
405 
406 #endif
407 
408 /* A list of types as they are encoded on-the-wire. */
409 typedef enum {
410   UPB_WIRE_TYPE_VARINT      = 0,
411   UPB_WIRE_TYPE_64BIT       = 1,
412   UPB_WIRE_TYPE_DELIMITED   = 2,
413   UPB_WIRE_TYPE_START_GROUP = 3,
414   UPB_WIRE_TYPE_END_GROUP   = 4,
415   UPB_WIRE_TYPE_32BIT       = 5
416 } upb_wiretype_t;
417 
418 
419 /* upb::ErrorSpace ************************************************************/
420 
421 /* A upb::ErrorSpace represents some domain of possible error values.  This lets
422  * upb::Status attach specific error codes to operations, like POSIX/C errno,
423  * Win32 error codes, etc.  Clients who want to know the very specific error
424  * code can check the error space and then know the type of the integer code.
425  *
426  * NOTE: upb::ErrorSpace is currently not used and should be considered
427  * experimental.  It is important primarily in cases where upb is performing
428  * I/O, but upb doesn't currently have any components that do this. */
429 
UPB_DECLARE_TYPE(upb::ErrorSpace,upb_errorspace)430 UPB_DECLARE_TYPE(upb::ErrorSpace, upb_errorspace)
431 
432 #ifdef __cplusplus
433 class upb::ErrorSpace {
434 #else
435 struct upb_errorspace {
436 #endif
437   const char *name;
438 };
439 
440 
441 /* upb::Status ****************************************************************/
442 
443 /* upb::Status represents a success or failure status and error message.
444  * It owns no resources and allocates no memory, so it should work
445  * even in OOM situations. */
446 UPB_DECLARE_TYPE(upb::Status, upb_status)
447 
448 /* The maximum length of an error message before it will get truncated. */
449 #define UPB_STATUS_MAX_MESSAGE 128
450 
451 UPB_BEGIN_EXTERN_C
452 
453 const char *upb_status_errmsg(const upb_status *status);
454 bool upb_ok(const upb_status *status);
455 upb_errorspace *upb_status_errspace(const upb_status *status);
456 int upb_status_errcode(const upb_status *status);
457 
458 /* Any of the functions that write to a status object allow status to be NULL,
459  * to support use cases where the function's caller does not care about the
460  * status message. */
461 void upb_status_clear(upb_status *status);
462 void upb_status_seterrmsg(upb_status *status, const char *msg);
463 void upb_status_seterrf(upb_status *status, const char *fmt, ...);
464 void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args);
465 void upb_status_copy(upb_status *to, const upb_status *from);
466 
467 UPB_END_EXTERN_C
468 
469 #ifdef __cplusplus
470 
471 class upb::Status {
472  public:
473   Status() { upb_status_clear(this); }
474 
475   /* Returns true if there is no error. */
476   bool ok() const { return upb_ok(this); }
477 
478   /* Optional error space and code, useful if the caller wants to
479    * programmatically check the specific kind of error. */
480   ErrorSpace* error_space() { return upb_status_errspace(this); }
481   int error_code() const { return upb_status_errcode(this); }
482 
483   /* The returned string is invalidated by any other call into the status. */
484   const char *error_message() const { return upb_status_errmsg(this); }
485 
486   /* The error message will be truncated if it is longer than
487    * UPB_STATUS_MAX_MESSAGE-4. */
488   void SetErrorMessage(const char* msg) { upb_status_seterrmsg(this, msg); }
489   void SetFormattedErrorMessage(const char* fmt, ...) {
490     va_list args;
491     va_start(args, fmt);
492     upb_status_vseterrf(this, fmt, args);
493     va_end(args);
494   }
495 
496   /* Resets the status to a successful state with no message. */
497   void Clear() { upb_status_clear(this); }
498 
499   void CopyFrom(const Status& other) { upb_status_copy(this, &other); }
500 
501  private:
502   UPB_DISALLOW_COPY_AND_ASSIGN(Status)
503 #else
504 struct upb_status {
505 #endif
506   bool ok_;
507 
508   /* Specific status code defined by some error space (optional). */
509   int code_;
510   upb_errorspace *error_space_;
511 
512   /* TODO(haberman): add file/line of error? */
513 
514   /* Error message; NULL-terminated. */
515   char msg[UPB_STATUS_MAX_MESSAGE];
516 };
517 
518 #define UPB_STATUS_INIT {true, 0, NULL, {0}}
519 
520 
521 /** Built-in error spaces. ****************************************************/
522 
523 /* Errors raised by upb that we want to be able to detect programmatically. */
524 typedef enum {
525   UPB_NOMEM   /* Can't reuse ENOMEM because it is POSIX, not ISO C. */
526 } upb_errcode_t;
527 
528 extern upb_errorspace upb_upberr;
529 
530 void upb_upberr_setoom(upb_status *s);
531 
532 /* Since errno is defined by standard C, we define an error space for it in
533  * core upb.  Other error spaces should be defined in other, platform-specific
534  * modules. */
535 
536 extern upb_errorspace upb_errnoerr;
537 
538 
539 /** upb::Allocator ************************************************************/
540 
541 /* A upb::Allocator is a possibly-stateful allocator object.
542  *
543  * It could either be an arena allocator (which doesn't require individual
544  * free() calls) or a regular malloc() (which does).  The client must therefore
545  * free memory unless it knows that the allocator is an arena allocator. */
546 UPB_DECLARE_TYPE(upb::Allocator, upb_alloc)
547 
548 /* A malloc()/free() function.
549  * If "size" is 0 then the function acts like free(), otherwise it acts like
550  * realloc().  Only "oldsize" bytes from a previous allocation are preserved. */
551 typedef void *upb_alloc_func(upb_alloc *alloc, void *ptr, size_t oldsize,
552                              size_t size);
553 
554 #ifdef __cplusplus
555 
556 class upb::Allocator UPB_FINAL {
557  public:
558   Allocator() {}
559 
560  private:
561   UPB_DISALLOW_COPY_AND_ASSIGN(Allocator)
562 
563  public:
564 #else
565 struct upb_alloc {
566 #endif  /* __cplusplus */
567   upb_alloc_func *func;
568 };
569 
570 UPB_INLINE void *upb_malloc(upb_alloc *alloc, size_t size) {
571   UPB_ASSERT(alloc);
572   return alloc->func(alloc, NULL, 0, size);
573 }
574 
575 UPB_INLINE void *upb_realloc(upb_alloc *alloc, void *ptr, size_t oldsize,
576                              size_t size) {
577   UPB_ASSERT(alloc);
578   return alloc->func(alloc, ptr, oldsize, size);
579 }
580 
581 UPB_INLINE void upb_free(upb_alloc *alloc, void *ptr) {
582   assert(alloc);
583   alloc->func(alloc, ptr, 0, 0);
584 }
585 
586 /* The global allocator used by upb.  Uses the standard malloc()/free(). */
587 
588 extern upb_alloc upb_alloc_global;
589 
590 /* Functions that hard-code the global malloc.
591  *
592  * We still get benefit because we can put custom logic into our global
593  * allocator, like injecting out-of-memory faults in debug/testing builds. */
594 
595 UPB_INLINE void *upb_gmalloc(size_t size) {
596   return upb_malloc(&upb_alloc_global, size);
597 }
598 
599 UPB_INLINE void *upb_grealloc(void *ptr, size_t oldsize, size_t size) {
600   return upb_realloc(&upb_alloc_global, ptr, oldsize, size);
601 }
602 
603 UPB_INLINE void upb_gfree(void *ptr) {
604   upb_free(&upb_alloc_global, ptr);
605 }
606 
607 /* upb::Arena *****************************************************************/
608 
609 /* upb::Arena is a specific allocator implementation that uses arena allocation.
610  * The user provides an allocator that will be used to allocate the underlying
611  * arena blocks.  Arenas by nature do not require the individual allocations
612  * to be freed.  However the Arena does allow users to register cleanup
613  * functions that will run when the arena is destroyed.
614  *
615  * A upb::Arena is *not* thread-safe.
616  *
617  * You could write a thread-safe arena allocator that satisfies the
618  * upb::Allocator interface, but it would not be as efficient for the
619  * single-threaded case. */
620 UPB_DECLARE_TYPE(upb::Arena, upb_arena)
621 
622 typedef void upb_cleanup_func(void *ud);
623 
624 #define UPB_ARENA_BLOCK_OVERHEAD (sizeof(size_t)*4)
625 
626 UPB_BEGIN_EXTERN_C
627 
628 void upb_arena_init(upb_arena *a);
629 void upb_arena_init2(upb_arena *a, void *mem, size_t n, upb_alloc *alloc);
630 void upb_arena_uninit(upb_arena *a);
631 bool upb_arena_addcleanup(upb_arena *a, upb_cleanup_func *func, void *ud);
632 size_t upb_arena_bytesallocated(const upb_arena *a);
633 void upb_arena_setnextblocksize(upb_arena *a, size_t size);
634 void upb_arena_setmaxblocksize(upb_arena *a, size_t size);
635 UPB_INLINE upb_alloc *upb_arena_alloc(upb_arena *a) { return (upb_alloc*)a; }
636 
637 UPB_END_EXTERN_C
638 
639 #ifdef __cplusplus
640 
641 class upb::Arena {
642  public:
643   /* A simple arena with no initial memory block and the default allocator. */
644   Arena() { upb_arena_init(this); }
645 
646   /* Constructs an arena with the given initial block which allocates blocks
647    * with the given allocator.  The given allocator must outlive the Arena.
648    *
649    * If you pass NULL for the allocator it will default to the global allocator
650    * upb_alloc_global, and NULL/0 for the initial block will cause there to be
651    * no initial block. */
652   Arena(void *mem, size_t len, Allocator* a) {
653     upb_arena_init2(this, mem, len, a);
654   }
655 
656   ~Arena() { upb_arena_uninit(this); }
657 
658   /* Sets the size of the next block the Arena will request (unless the
659    * requested allocation is larger).  Each block will double in size until the
660    * max limit is reached. */
661   void SetNextBlockSize(size_t size) { upb_arena_setnextblocksize(this, size); }
662 
663   /* Sets the maximum block size.  No blocks larger than this will be requested
664    * from the underlying allocator unless individual arena allocations are
665    * larger. */
666   void SetMaxBlockSize(size_t size) { upb_arena_setmaxblocksize(this, size); }
667 
668   /* Allows this arena to be used as a generic allocator.
669    *
670    * The arena does not need free() calls so when using Arena as an allocator
671    * it is safe to skip them.  However they are no-ops so there is no harm in
672    * calling free() either. */
673   Allocator* allocator() { return upb_arena_alloc(this); }
674 
675   /* Add a cleanup function to run when the arena is destroyed.
676    * Returns false on out-of-memory. */
677   bool AddCleanup(upb_cleanup_func* func, void* ud) {
678     return upb_arena_addcleanup(this, func, ud);
679   }
680 
681   /* Total number of bytes that have been allocated.  It is undefined what
682    * Realloc() does to this counter. */
683   size_t BytesAllocated() const {
684     return upb_arena_bytesallocated(this);
685   }
686 
687  private:
688   UPB_DISALLOW_COPY_AND_ASSIGN(Arena)
689 
690 #else
691 struct upb_arena {
692 #endif  /* __cplusplus */
693   /* We implement the allocator interface.
694    * This must be the first member of upb_arena! */
695   upb_alloc alloc;
696 
697   /* Allocator to allocate arena blocks.  We are responsible for freeing these
698    * when we are destroyed. */
699   upb_alloc *block_alloc;
700 
701   size_t bytes_allocated;
702   size_t next_block_size;
703   size_t max_block_size;
704 
705   /* Linked list of blocks.  Points to an arena_block, defined in env.c */
706   void *block_head;
707 
708   /* Cleanup entries.  Pointer to a cleanup_ent, defined in env.c */
709   void *cleanup_head;
710 
711   /* For future expansion, since the size of this struct is exposed to users. */
712   void *future1;
713   void *future2;
714 };
715 
716 
717 /* upb::Environment ***********************************************************/
718 
719 /* A upb::Environment provides a means for injecting malloc and an
720  * error-reporting callback into encoders/decoders.  This allows them to be
721  * independent of nearly all assumptions about their actual environment.
722  *
723  * It is also a container for allocating the encoders/decoders themselves that
724  * insulates clients from knowing their actual size.  This provides ABI
725  * compatibility even if the size of the objects change.  And this allows the
726  * structure definitions to be in the .c files instead of the .h files, making
727  * the .h files smaller and more readable.
728  *
729  * We might want to consider renaming this to "Pipeline" if/when the concept of
730  * a pipeline element becomes more formalized. */
731 UPB_DECLARE_TYPE(upb::Environment, upb_env)
732 
733 /* A function that receives an error report from an encoder or decoder.  The
734  * callback can return true to request that the error should be recovered, but
735  * if the error is not recoverable this has no effect. */
736 typedef bool upb_error_func(void *ud, const upb_status *status);
737 
738 UPB_BEGIN_EXTERN_C
739 
740 void upb_env_init(upb_env *e);
741 void upb_env_init2(upb_env *e, void *mem, size_t n, upb_alloc *alloc);
742 void upb_env_uninit(upb_env *e);
743 
744 void upb_env_initonly(upb_env *e);
745 
746 UPB_INLINE upb_arena *upb_env_arena(upb_env *e) { return (upb_arena*)e; }
747 bool upb_env_ok(const upb_env *e);
748 void upb_env_seterrorfunc(upb_env *e, upb_error_func *func, void *ud);
749 
750 /* Convenience wrappers around the methods of the contained arena. */
751 void upb_env_reporterrorsto(upb_env *e, upb_status *s);
752 bool upb_env_reporterror(upb_env *e, const upb_status *s);
753 void *upb_env_malloc(upb_env *e, size_t size);
754 void *upb_env_realloc(upb_env *e, void *ptr, size_t oldsize, size_t size);
755 void upb_env_free(upb_env *e, void *ptr);
756 bool upb_env_addcleanup(upb_env *e, upb_cleanup_func *func, void *ud);
757 size_t upb_env_bytesallocated(const upb_env *e);
758 
759 UPB_END_EXTERN_C
760 
761 #ifdef __cplusplus
762 
763 class upb::Environment {
764  public:
765   /* The given Arena must outlive this environment. */
766   Environment() { upb_env_initonly(this); }
767 
768   Environment(void *mem, size_t len, Allocator *a) : arena_(mem, len, a) {
769     upb_env_initonly(this);
770   }
771 
772   Arena* arena() { return upb_env_arena(this); }
773 
774   /* Set a custom error reporting function. */
775   void SetErrorFunction(upb_error_func* func, void* ud) {
776     upb_env_seterrorfunc(this, func, ud);
777   }
778 
779   /* Set the error reporting function to simply copy the status to the given
780    * status and abort. */
781   void ReportErrorsTo(Status* status) { upb_env_reporterrorsto(this, status); }
782 
783   /* Returns true if all allocations and AddCleanup() calls have succeeded,
784    * and no errors were reported with ReportError() (except ones that recovered
785    * successfully). */
786   bool ok() const { return upb_env_ok(this); }
787 
788   /* Reports an error to this environment's callback, returning true if
789    * the caller should try to recover. */
790   bool ReportError(const Status* status) {
791     return upb_env_reporterror(this, status);
792   }
793 
794  private:
795   UPB_DISALLOW_COPY_AND_ASSIGN(Environment)
796 
797 #else
798 struct upb_env {
799 #endif  /* __cplusplus */
800   upb_arena arena_;
801   upb_error_func *error_func_;
802   void *error_ud_;
803   bool ok_;
804 };
805 
806 
807 /* upb::InlinedArena **********************************************************/
808 /* upb::InlinedEnvironment ****************************************************/
809 
810 /* upb::InlinedArena and upb::InlinedEnvironment seed their arenas with a
811  * predefined amount of memory.  No heap memory will be allocated until the
812  * initial block is exceeded.
813  *
814  * These types only exist in C++ */
815 
816 #ifdef __cplusplus
817 
818 template <int N> class upb::InlinedArena : public upb::Arena {
819  public:
820   InlinedArena() : Arena(initial_block_, N, NULL) {}
821   explicit InlinedArena(Allocator* a) : Arena(initial_block_, N, a) {}
822 
823  private:
824   UPB_DISALLOW_COPY_AND_ASSIGN(InlinedArena)
825 
826   char initial_block_[N + UPB_ARENA_BLOCK_OVERHEAD];
827 };
828 
829 template <int N> class upb::InlinedEnvironment : public upb::Environment {
830  public:
831   InlinedEnvironment() : Environment(initial_block_, N, NULL) {}
832   explicit InlinedEnvironment(Allocator *a)
833       : Environment(initial_block_, N, a) {}
834 
835  private:
836   UPB_DISALLOW_COPY_AND_ASSIGN(InlinedEnvironment)
837 
838   char initial_block_[N + UPB_ARENA_BLOCK_OVERHEAD];
839 };
840 
841 #endif  /* __cplusplus */
842 
843 
844 
845 #endif  /* UPB_H_ */
846 
847 #ifdef __cplusplus
848 extern "C" {
849 #endif
850 
851 
852 /* upb_value ******************************************************************/
853 
854 /* A tagged union (stored untagged inside the table) so that we can check that
855  * clients calling table accessors are correctly typed without having to have
856  * an explosion of accessors. */
857 typedef enum {
858   UPB_CTYPE_INT32    = 1,
859   UPB_CTYPE_INT64    = 2,
860   UPB_CTYPE_UINT32   = 3,
861   UPB_CTYPE_UINT64   = 4,
862   UPB_CTYPE_BOOL     = 5,
863   UPB_CTYPE_CSTR     = 6,
864   UPB_CTYPE_PTR      = 7,
865   UPB_CTYPE_CONSTPTR = 8,
866   UPB_CTYPE_FPTR     = 9,
867   UPB_CTYPE_FLOAT    = 10,
868   UPB_CTYPE_DOUBLE   = 11
869 } upb_ctype_t;
870 
871 typedef struct {
872   uint64_t val;
873 #ifndef NDEBUG
874   /* In debug mode we carry the value type around also so we can check accesses
875    * to be sure the right member is being read. */
876   upb_ctype_t ctype;
877 #endif
878 } upb_value;
879 
880 #ifdef NDEBUG
881 #define SET_TYPE(dest, val)      UPB_UNUSED(val)
882 #else
883 #define SET_TYPE(dest, val) dest = val
884 #endif
885 
886 /* Like strdup(), which isn't always available since it's not ANSI C. */
887 char *upb_strdup(const char *s, upb_alloc *a);
888 /* Variant that works with a length-delimited rather than NULL-delimited string,
889  * as supported by strtable. */
890 char *upb_strdup2(const char *s, size_t len, upb_alloc *a);
891 
892 UPB_INLINE char *upb_gstrdup(const char *s) {
893   return upb_strdup(s, &upb_alloc_global);
894 }
895 
896 UPB_INLINE void _upb_value_setval(upb_value *v, uint64_t val,
897                                   upb_ctype_t ctype) {
898   v->val = val;
899   SET_TYPE(v->ctype, ctype);
900 }
901 
902 UPB_INLINE upb_value _upb_value_val(uint64_t val, upb_ctype_t ctype) {
903   upb_value ret;
904   _upb_value_setval(&ret, val, ctype);
905   return ret;
906 }
907 
908 /* For each value ctype, define the following set of functions:
909  *
910  * // Get/set an int32 from a upb_value.
911  * int32_t upb_value_getint32(upb_value val);
912  * void upb_value_setint32(upb_value *val, int32_t cval);
913  *
914  * // Construct a new upb_value from an int32.
915  * upb_value upb_value_int32(int32_t val); */
916 #define FUNCS(name, membername, type_t, converter, proto_type) \
917   UPB_INLINE void upb_value_set ## name(upb_value *val, type_t cval) { \
918     val->val = (converter)cval; \
919     SET_TYPE(val->ctype, proto_type); \
920   } \
921   UPB_INLINE upb_value upb_value_ ## name(type_t val) { \
922     upb_value ret; \
923     upb_value_set ## name(&ret, val); \
924     return ret; \
925   } \
926   UPB_INLINE type_t upb_value_get ## name(upb_value val) { \
927     UPB_ASSERT_DEBUGVAR(val.ctype == proto_type); \
928     return (type_t)(converter)val.val; \
929   }
930 
931 FUNCS(int32,    int32,        int32_t,      int32_t,    UPB_CTYPE_INT32)
932 FUNCS(int64,    int64,        int64_t,      int64_t,    UPB_CTYPE_INT64)
933 FUNCS(uint32,   uint32,       uint32_t,     uint32_t,   UPB_CTYPE_UINT32)
934 FUNCS(uint64,   uint64,       uint64_t,     uint64_t,   UPB_CTYPE_UINT64)
935 FUNCS(bool,     _bool,        bool,         bool,       UPB_CTYPE_BOOL)
936 FUNCS(cstr,     cstr,         char*,        uintptr_t,  UPB_CTYPE_CSTR)
937 FUNCS(ptr,      ptr,          void*,        uintptr_t,  UPB_CTYPE_PTR)
938 FUNCS(constptr, constptr,     const void*,  uintptr_t,  UPB_CTYPE_CONSTPTR)
939 FUNCS(fptr,     fptr,         upb_func*,    uintptr_t,  UPB_CTYPE_FPTR)
940 
941 #undef FUNCS
942 
943 UPB_INLINE void upb_value_setfloat(upb_value *val, float cval) {
944   memcpy(&val->val, &cval, sizeof(cval));
945   SET_TYPE(val->ctype, UPB_CTYPE_FLOAT);
946 }
947 
948 UPB_INLINE void upb_value_setdouble(upb_value *val, double cval) {
949   memcpy(&val->val, &cval, sizeof(cval));
950   SET_TYPE(val->ctype, UPB_CTYPE_DOUBLE);
951 }
952 
953 UPB_INLINE upb_value upb_value_float(float cval) {
954   upb_value ret;
955   upb_value_setfloat(&ret, cval);
956   return ret;
957 }
958 
959 UPB_INLINE upb_value upb_value_double(double cval) {
960   upb_value ret;
961   upb_value_setdouble(&ret, cval);
962   return ret;
963 }
964 
965 #undef SET_TYPE
966 
967 
968 /* upb_tabkey *****************************************************************/
969 
970 /* Either:
971  *   1. an actual integer key, or
972  *   2. a pointer to a string prefixed by its uint32_t length, owned by us.
973  *
974  * ...depending on whether this is a string table or an int table.  We would
975  * make this a union of those two types, but C89 doesn't support statically
976  * initializing a non-first union member. */
977 typedef uintptr_t upb_tabkey;
978 
979 #define UPB_TABKEY_NUM(n) n
980 #define UPB_TABKEY_NONE 0
981 /* The preprocessor isn't quite powerful enough to turn the compile-time string
982  * length into a byte-wise string representation, so code generation needs to
983  * help it along.
984  *
985  * "len1" is the low byte and len4 is the high byte. */
986 #ifdef UPB_BIG_ENDIAN
987 #define UPB_TABKEY_STR(len1, len2, len3, len4, strval) \
988     (uintptr_t)(len4 len3 len2 len1 strval)
989 #else
990 #define UPB_TABKEY_STR(len1, len2, len3, len4, strval) \
991     (uintptr_t)(len1 len2 len3 len4 strval)
992 #endif
993 
994 UPB_INLINE char *upb_tabstr(upb_tabkey key, uint32_t *len) {
995   char* mem = (char*)key;
996   if (len) memcpy(len, mem, sizeof(*len));
997   return mem + sizeof(*len);
998 }
999 
1000 
1001 /* upb_tabval *****************************************************************/
1002 
1003 #ifdef __cplusplus
1004 
1005 /* Status initialization not supported.
1006  *
1007  * This separate definition is necessary because in C++, UINTPTR_MAX isn't
1008  * reliably available. */
1009 typedef struct {
1010   uint64_t val;
1011 } upb_tabval;
1012 
1013 #else
1014 
1015 /* C -- supports static initialization, but to support static initialization of
1016  * both integers and points for both 32 and 64 bit targets, it takes a little
1017  * bit of doing. */
1018 
1019 #if UINTPTR_MAX == 0xffffffffffffffffULL
1020 #define UPB_PTR_IS_64BITS
1021 #elif UINTPTR_MAX != 0xffffffff
1022 #error Could not determine how many bits pointers are.
1023 #endif
1024 
1025 typedef union {
1026   /* For static initialization.
1027    *
1028    * Unfortunately this ugliness is necessary -- it is the only way that we can,
1029    * with -std=c89 -pedantic, statically initialize this to either a pointer or
1030    * an integer on 32-bit platforms. */
1031   struct {
1032 #ifdef UPB_PTR_IS_64BITS
1033     uintptr_t val;
1034 #else
1035     uintptr_t val1;
1036     uintptr_t val2;
1037 #endif
1038   } staticinit;
1039 
1040   /* The normal accessor that we use for everything at runtime. */
1041   uint64_t val;
1042 } upb_tabval;
1043 
1044 #ifdef UPB_PTR_IS_64BITS
1045 #define UPB_TABVALUE_INT_INIT(v) {{v}}
1046 #define UPB_TABVALUE_EMPTY_INIT  {{-1}}
1047 #else
1048 
1049 /* 32-bit pointers */
1050 
1051 #ifdef UPB_BIG_ENDIAN
1052 #define UPB_TABVALUE_INT_INIT(v) {{0, v}}
1053 #define UPB_TABVALUE_EMPTY_INIT  {{-1, -1}}
1054 #else
1055 #define UPB_TABVALUE_INT_INIT(v) {{v, 0}}
1056 #define UPB_TABVALUE_EMPTY_INIT  {{-1, -1}}
1057 #endif
1058 
1059 #endif
1060 
1061 #define UPB_TABVALUE_PTR_INIT(v) UPB_TABVALUE_INT_INIT((uintptr_t)v)
1062 
1063 #undef UPB_PTR_IS_64BITS
1064 
1065 #endif  /* __cplusplus */
1066 
1067 
1068 /* upb_table ******************************************************************/
1069 
1070 typedef struct _upb_tabent {
1071   upb_tabkey key;
1072   upb_tabval val;
1073 
1074   /* Internal chaining.  This is const so we can create static initializers for
1075    * tables.  We cast away const sometimes, but *only* when the containing
1076    * upb_table is known to be non-const.  This requires a bit of care, but
1077    * the subtlety is confined to table.c. */
1078   const struct _upb_tabent *next;
1079 } upb_tabent;
1080 
1081 typedef struct {
1082   size_t count;          /* Number of entries in the hash part. */
1083   size_t mask;           /* Mask to turn hash value -> bucket. */
1084   upb_ctype_t ctype;     /* Type of all values. */
1085   uint8_t size_lg2;      /* Size of the hashtable part is 2^size_lg2 entries. */
1086 
1087   /* Hash table entries.
1088    * Making this const isn't entirely accurate; what we really want is for it to
1089    * have the same const-ness as the table it's inside.  But there's no way to
1090    * declare that in C.  So we have to make it const so that we can statically
1091    * initialize const hash tables.  Then we cast away const when we have to.
1092    */
1093   const upb_tabent *entries;
1094 
1095 #ifndef NDEBUG
1096   /* This table's allocator.  We make the user pass it in to every relevant
1097    * function and only use this to check it in debug mode.  We do this solely
1098    * to keep upb_table as small as possible.  This might seem slightly paranoid
1099    * but the plan is to use upb_table for all map fields and extension sets in
1100    * a forthcoming message representation, so there could be a lot of these.
1101    * If this turns out to be too annoying later, we can change it (since this
1102    * is an internal-only header file). */
1103   upb_alloc *alloc;
1104 #endif
1105 } upb_table;
1106 
1107 #ifdef NDEBUG
1108 #  define UPB_TABLE_INIT(count, mask, ctype, size_lg2, entries) \
1109      {count, mask, ctype, size_lg2, entries}
1110 #else
1111 #  ifdef UPB_DEBUG_REFS
1112 /* At the moment the only mutable tables we statically initialize are debug
1113  * ref tables. */
1114 #    define UPB_TABLE_INIT(count, mask, ctype, size_lg2, entries) \
1115        {count, mask, ctype, size_lg2, entries, &upb_alloc_debugrefs}
1116 #  else
1117 #    define UPB_TABLE_INIT(count, mask, ctype, size_lg2, entries) \
1118        {count, mask, ctype, size_lg2, entries, NULL}
1119 #  endif
1120 #endif
1121 
1122 typedef struct {
1123   upb_table t;
1124 } upb_strtable;
1125 
1126 #define UPB_STRTABLE_INIT(count, mask, ctype, size_lg2, entries) \
1127   {UPB_TABLE_INIT(count, mask, ctype, size_lg2, entries)}
1128 
1129 #define UPB_EMPTY_STRTABLE_INIT(ctype)                           \
1130   UPB_STRTABLE_INIT(0, 0, ctype, 0, NULL)
1131 
1132 typedef struct {
1133   upb_table t;              /* For entries that don't fit in the array part. */
1134   const upb_tabval *array;  /* Array part of the table. See const note above. */
1135   size_t array_size;        /* Array part size. */
1136   size_t array_count;       /* Array part number of elements. */
1137 } upb_inttable;
1138 
1139 #define UPB_INTTABLE_INIT(count, mask, ctype, size_lg2, ent, a, asize, acount) \
1140   {UPB_TABLE_INIT(count, mask, ctype, size_lg2, ent), a, asize, acount}
1141 
1142 #define UPB_EMPTY_INTTABLE_INIT(ctype) \
1143   UPB_INTTABLE_INIT(0, 0, ctype, 0, NULL, NULL, 0, 0)
1144 
1145 #define UPB_ARRAY_EMPTYENT -1
1146 
1147 UPB_INLINE size_t upb_table_size(const upb_table *t) {
1148   if (t->size_lg2 == 0)
1149     return 0;
1150   else
1151     return 1 << t->size_lg2;
1152 }
1153 
1154 /* Internal-only functions, in .h file only out of necessity. */
1155 UPB_INLINE bool upb_tabent_isempty(const upb_tabent *e) {
1156   return e->key == 0;
1157 }
1158 
1159 /* Used by some of the unit tests for generic hashing functionality. */
1160 uint32_t MurmurHash2(const void * key, size_t len, uint32_t seed);
1161 
1162 UPB_INLINE uintptr_t upb_intkey(uintptr_t key) {
1163   return key;
1164 }
1165 
1166 UPB_INLINE uint32_t upb_inthash(uintptr_t key) {
1167   return (uint32_t)key;
1168 }
1169 
1170 static const upb_tabent *upb_getentry(const upb_table *t, uint32_t hash) {
1171   return t->entries + (hash & t->mask);
1172 }
1173 
1174 UPB_INLINE bool upb_arrhas(upb_tabval key) {
1175   return key.val != (uint64_t)-1;
1176 }
1177 
1178 /* Initialize and uninitialize a table, respectively.  If memory allocation
1179  * failed, false is returned that the table is uninitialized. */
1180 bool upb_inttable_init2(upb_inttable *table, upb_ctype_t ctype, upb_alloc *a);
1181 bool upb_strtable_init2(upb_strtable *table, upb_ctype_t ctype, upb_alloc *a);
1182 void upb_inttable_uninit2(upb_inttable *table, upb_alloc *a);
1183 void upb_strtable_uninit2(upb_strtable *table, upb_alloc *a);
1184 
1185 UPB_INLINE bool upb_inttable_init(upb_inttable *table, upb_ctype_t ctype) {
1186   return upb_inttable_init2(table, ctype, &upb_alloc_global);
1187 }
1188 
1189 UPB_INLINE bool upb_strtable_init(upb_strtable *table, upb_ctype_t ctype) {
1190   return upb_strtable_init2(table, ctype, &upb_alloc_global);
1191 }
1192 
1193 UPB_INLINE void upb_inttable_uninit(upb_inttable *table) {
1194   upb_inttable_uninit2(table, &upb_alloc_global);
1195 }
1196 
1197 UPB_INLINE void upb_strtable_uninit(upb_strtable *table) {
1198   upb_strtable_uninit2(table, &upb_alloc_global);
1199 }
1200 
1201 /* Returns the number of values in the table. */
1202 size_t upb_inttable_count(const upb_inttable *t);
1203 UPB_INLINE size_t upb_strtable_count(const upb_strtable *t) {
1204   return t->t.count;
1205 }
1206 
1207 void upb_inttable_packedsize(const upb_inttable *t, size_t *size);
1208 void upb_strtable_packedsize(const upb_strtable *t, size_t *size);
1209 upb_inttable *upb_inttable_pack(const upb_inttable *t, void *p, size_t *ofs,
1210                                 size_t size);
1211 upb_strtable *upb_strtable_pack(const upb_strtable *t, void *p, size_t *ofs,
1212                                 size_t size);
1213 
1214 /* Inserts the given key into the hashtable with the given value.  The key must
1215  * not already exist in the hash table.  For string tables, the key must be
1216  * NULL-terminated, and the table will make an internal copy of the key.
1217  * Inttables must not insert a value of UINTPTR_MAX.
1218  *
1219  * If a table resize was required but memory allocation failed, false is
1220  * returned and the table is unchanged. */
1221 bool upb_inttable_insert2(upb_inttable *t, uintptr_t key, upb_value val,
1222                           upb_alloc *a);
1223 bool upb_strtable_insert3(upb_strtable *t, const char *key, size_t len,
1224                           upb_value val, upb_alloc *a);
1225 
1226 UPB_INLINE bool upb_inttable_insert(upb_inttable *t, uintptr_t key,
1227                                     upb_value val) {
1228   return upb_inttable_insert2(t, key, val, &upb_alloc_global);
1229 }
1230 
1231 UPB_INLINE bool upb_strtable_insert2(upb_strtable *t, const char *key,
1232                                      size_t len, upb_value val) {
1233   return upb_strtable_insert3(t, key, len, val, &upb_alloc_global);
1234 }
1235 
1236 /* For NULL-terminated strings. */
1237 UPB_INLINE bool upb_strtable_insert(upb_strtable *t, const char *key,
1238                                     upb_value val) {
1239   return upb_strtable_insert2(t, key, strlen(key), val);
1240 }
1241 
1242 /* Looks up key in this table, returning "true" if the key was found.
1243  * If v is non-NULL, copies the value for this key into *v. */
1244 bool upb_inttable_lookup(const upb_inttable *t, uintptr_t key, upb_value *v);
1245 bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len,
1246                           upb_value *v);
1247 
1248 /* For NULL-terminated strings. */
1249 UPB_INLINE bool upb_strtable_lookup(const upb_strtable *t, const char *key,
1250                                     upb_value *v) {
1251   return upb_strtable_lookup2(t, key, strlen(key), v);
1252 }
1253 
1254 /* Removes an item from the table.  Returns true if the remove was successful,
1255  * and stores the removed item in *val if non-NULL. */
1256 bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val);
1257 bool upb_strtable_remove3(upb_strtable *t, const char *key, size_t len,
1258                           upb_value *val, upb_alloc *alloc);
1259 
1260 UPB_INLINE bool upb_strtable_remove2(upb_strtable *t, const char *key,
1261                                      size_t len, upb_value *val) {
1262   return upb_strtable_remove3(t, key, len, val, &upb_alloc_global);
1263 }
1264 
1265 /* For NULL-terminated strings. */
1266 UPB_INLINE bool upb_strtable_remove(upb_strtable *t, const char *key,
1267                                     upb_value *v) {
1268   return upb_strtable_remove2(t, key, strlen(key), v);
1269 }
1270 
1271 /* Updates an existing entry in an inttable.  If the entry does not exist,
1272  * returns false and does nothing.  Unlike insert/remove, this does not
1273  * invalidate iterators. */
1274 bool upb_inttable_replace(upb_inttable *t, uintptr_t key, upb_value val);
1275 
1276 /* Handy routines for treating an inttable like a stack.  May not be mixed with
1277  * other insert/remove calls. */
1278 bool upb_inttable_push2(upb_inttable *t, upb_value val, upb_alloc *a);
1279 upb_value upb_inttable_pop(upb_inttable *t);
1280 
1281 UPB_INLINE bool upb_inttable_push(upb_inttable *t, upb_value val) {
1282   return upb_inttable_push2(t, val, &upb_alloc_global);
1283 }
1284 
1285 /* Convenience routines for inttables with pointer keys. */
1286 bool upb_inttable_insertptr2(upb_inttable *t, const void *key, upb_value val,
1287                              upb_alloc *a);
1288 bool upb_inttable_removeptr(upb_inttable *t, const void *key, upb_value *val);
1289 bool upb_inttable_lookupptr(
1290     const upb_inttable *t, const void *key, upb_value *val);
1291 
1292 UPB_INLINE bool upb_inttable_insertptr(upb_inttable *t, const void *key,
1293                                        upb_value val) {
1294   return upb_inttable_insertptr2(t, key, val, &upb_alloc_global);
1295 }
1296 
1297 /* Optimizes the table for the current set of entries, for both memory use and
1298  * lookup time.  Client should call this after all entries have been inserted;
1299  * inserting more entries is legal, but will likely require a table resize. */
1300 void upb_inttable_compact2(upb_inttable *t, upb_alloc *a);
1301 
1302 UPB_INLINE void upb_inttable_compact(upb_inttable *t) {
1303   upb_inttable_compact2(t, &upb_alloc_global);
1304 }
1305 
1306 /* A special-case inlinable version of the lookup routine for 32-bit
1307  * integers. */
1308 UPB_INLINE bool upb_inttable_lookup32(const upb_inttable *t, uint32_t key,
1309                                       upb_value *v) {
1310   *v = upb_value_int32(0);  /* Silence compiler warnings. */
1311   if (key < t->array_size) {
1312     upb_tabval arrval = t->array[key];
1313     if (upb_arrhas(arrval)) {
1314       _upb_value_setval(v, arrval.val, t->t.ctype);
1315       return true;
1316     } else {
1317       return false;
1318     }
1319   } else {
1320     const upb_tabent *e;
1321     if (t->t.entries == NULL) return false;
1322     for (e = upb_getentry(&t->t, upb_inthash(key)); true; e = e->next) {
1323       if ((uint32_t)e->key == key) {
1324         _upb_value_setval(v, e->val.val, t->t.ctype);
1325         return true;
1326       }
1327       if (e->next == NULL) return false;
1328     }
1329   }
1330 }
1331 
1332 /* Exposed for testing only. */
1333 bool upb_strtable_resize(upb_strtable *t, size_t size_lg2, upb_alloc *a);
1334 
1335 /* Iterators ******************************************************************/
1336 
1337 /* Iterators for int and string tables.  We are subject to some kind of unusual
1338  * design constraints:
1339  *
1340  * For high-level languages:
1341  *  - we must be able to guarantee that we don't crash or corrupt memory even if
1342  *    the program accesses an invalidated iterator.
1343  *
1344  * For C++11 range-based for:
1345  *  - iterators must be copyable
1346  *  - iterators must be comparable
1347  *  - it must be possible to construct an "end" value.
1348  *
1349  * Iteration order is undefined.
1350  *
1351  * Modifying the table invalidates iterators.  upb_{str,int}table_done() is
1352  * guaranteed to work even on an invalidated iterator, as long as the table it
1353  * is iterating over has not been freed.  Calling next() or accessing data from
1354  * an invalidated iterator yields unspecified elements from the table, but it is
1355  * guaranteed not to crash and to return real table elements (except when done()
1356  * is true). */
1357 
1358 
1359 /* upb_strtable_iter **********************************************************/
1360 
1361 /*   upb_strtable_iter i;
1362  *   upb_strtable_begin(&i, t);
1363  *   for(; !upb_strtable_done(&i); upb_strtable_next(&i)) {
1364  *     const char *key = upb_strtable_iter_key(&i);
1365  *     const upb_value val = upb_strtable_iter_value(&i);
1366  *     // ...
1367  *   }
1368  */
1369 
1370 typedef struct {
1371   const upb_strtable *t;
1372   size_t index;
1373 } upb_strtable_iter;
1374 
1375 void upb_strtable_begin(upb_strtable_iter *i, const upb_strtable *t);
1376 void upb_strtable_next(upb_strtable_iter *i);
1377 bool upb_strtable_done(const upb_strtable_iter *i);
1378 const char *upb_strtable_iter_key(const upb_strtable_iter *i);
1379 size_t upb_strtable_iter_keylength(const upb_strtable_iter *i);
1380 upb_value upb_strtable_iter_value(const upb_strtable_iter *i);
1381 void upb_strtable_iter_setdone(upb_strtable_iter *i);
1382 bool upb_strtable_iter_isequal(const upb_strtable_iter *i1,
1383                                const upb_strtable_iter *i2);
1384 
1385 
1386 /* upb_inttable_iter **********************************************************/
1387 
1388 /*   upb_inttable_iter i;
1389  *   upb_inttable_begin(&i, t);
1390  *   for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
1391  *     uintptr_t key = upb_inttable_iter_key(&i);
1392  *     upb_value val = upb_inttable_iter_value(&i);
1393  *     // ...
1394  *   }
1395  */
1396 
1397 typedef struct {
1398   const upb_inttable *t;
1399   size_t index;
1400   bool array_part;
1401 } upb_inttable_iter;
1402 
1403 void upb_inttable_begin(upb_inttable_iter *i, const upb_inttable *t);
1404 void upb_inttable_next(upb_inttable_iter *i);
1405 bool upb_inttable_done(const upb_inttable_iter *i);
1406 uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i);
1407 upb_value upb_inttable_iter_value(const upb_inttable_iter *i);
1408 void upb_inttable_iter_setdone(upb_inttable_iter *i);
1409 bool upb_inttable_iter_isequal(const upb_inttable_iter *i1,
1410                                const upb_inttable_iter *i2);
1411 
1412 
1413 #ifdef __cplusplus
1414 }  /* extern "C" */
1415 #endif
1416 
1417 #endif  /* UPB_TABLE_H_ */
1418 
1419 /* Reference tracking will check ref()/unref() operations to make sure the
1420  * ref ownership is correct.  Where possible it will also make tools like
1421  * Valgrind attribute ref leaks to the code that took the leaked ref, not
1422  * the code that originally created the object.
1423  *
1424  * Enabling this requires the application to define upb_lock()/upb_unlock()
1425  * functions that acquire/release a global mutex (or #define UPB_THREAD_UNSAFE).
1426  * For this reason we don't enable it by default, even in debug builds.
1427  */
1428 
1429 /* #define UPB_DEBUG_REFS */
1430 
1431 #ifdef __cplusplus
1432 namespace upb {
1433 class RefCounted;
1434 template <class T> class reffed_ptr;
1435 }
1436 #endif
1437 
1438 UPB_DECLARE_TYPE(upb::RefCounted, upb_refcounted)
1439 
1440 struct upb_refcounted_vtbl;
1441 
1442 #ifdef __cplusplus
1443 
1444 class upb::RefCounted {
1445  public:
1446   /* Returns true if the given object is frozen. */
1447   bool IsFrozen() const;
1448 
1449   /* Increases the ref count, the new ref is owned by "owner" which must not
1450    * already own a ref (and should not itself be a refcounted object if the ref
1451    * could possibly be circular; see below).
1452    * Thread-safe iff "this" is frozen. */
1453   void Ref(const void *owner) const;
1454 
1455   /* Release a ref that was acquired from upb_refcounted_ref() and collects any
1456    * objects it can. */
1457   void Unref(const void *owner) const;
1458 
1459   /* Moves an existing ref from "from" to "to", without changing the overall
1460    * ref count.  DonateRef(foo, NULL, owner) is the same as Ref(foo, owner),
1461    * but "to" may not be NULL. */
1462   void DonateRef(const void *from, const void *to) const;
1463 
1464   /* Verifies that a ref to the given object is currently held by the given
1465    * owner.  Only effective in UPB_DEBUG_REFS builds. */
1466   void CheckRef(const void *owner) const;
1467 
1468  private:
1469   UPB_DISALLOW_POD_OPS(RefCounted, upb::RefCounted)
1470 #else
1471 struct upb_refcounted {
1472 #endif
1473   /* TODO(haberman): move the actual structure definition to structdefs.int.h.
1474    * The only reason they are here is because inline functions need to see the
1475    * definition of upb_handlers, which needs to see this definition.  But we
1476    * can change the upb_handlers inline functions to deal in raw offsets
1477    * instead.
1478    */
1479 
1480   /* A single reference count shared by all objects in the group. */
1481   uint32_t *group;
1482 
1483   /* A singly-linked list of all objects in the group. */
1484   upb_refcounted *next;
1485 
1486   /* Table of function pointers for this type. */
1487   const struct upb_refcounted_vtbl *vtbl;
1488 
1489   /* Maintained only when mutable, this tracks the number of refs (but not
1490    * ref2's) to this object.  *group should be the sum of all individual_count
1491    * in the group. */
1492   uint32_t individual_count;
1493 
1494   bool is_frozen;
1495 
1496 #ifdef UPB_DEBUG_REFS
1497   upb_inttable *refs;  /* Maps owner -> trackedref for incoming refs. */
1498   upb_inttable *ref2s; /* Set of targets for outgoing ref2s. */
1499 #endif
1500 };
1501 
1502 #ifdef UPB_DEBUG_REFS
1503 extern upb_alloc upb_alloc_debugrefs;
1504 #define UPB_REFCOUNT_INIT(vtbl, refs, ref2s) \
1505     {&static_refcount, NULL, vtbl, 0, true, refs, ref2s}
1506 #else
1507 #define UPB_REFCOUNT_INIT(vtbl, refs, ref2s) \
1508     {&static_refcount, NULL, vtbl, 0, true}
1509 #endif
1510 
1511 UPB_BEGIN_EXTERN_C
1512 
1513 /* It is better to use tracked refs when possible, for the extra debugging
1514  * capability.  But if this is not possible (because you don't have easy access
1515  * to a stable pointer value that is associated with the ref), you can pass
1516  * UPB_UNTRACKED_REF instead.  */
1517 extern const void *UPB_UNTRACKED_REF;
1518 
1519 /* Native C API. */
1520 bool upb_refcounted_isfrozen(const upb_refcounted *r);
1521 void upb_refcounted_ref(const upb_refcounted *r, const void *owner);
1522 void upb_refcounted_unref(const upb_refcounted *r, const void *owner);
1523 void upb_refcounted_donateref(
1524     const upb_refcounted *r, const void *from, const void *to);
1525 void upb_refcounted_checkref(const upb_refcounted *r, const void *owner);
1526 
1527 #define UPB_REFCOUNTED_CMETHODS(type, upcastfunc) \
1528   UPB_INLINE bool type ## _isfrozen(const type *v) { \
1529     return upb_refcounted_isfrozen(upcastfunc(v)); \
1530   } \
1531   UPB_INLINE void type ## _ref(const type *v, const void *owner) { \
1532     upb_refcounted_ref(upcastfunc(v), owner); \
1533   } \
1534   UPB_INLINE void type ## _unref(const type *v, const void *owner) { \
1535     upb_refcounted_unref(upcastfunc(v), owner); \
1536   } \
1537   UPB_INLINE void type ## _donateref(const type *v, const void *from, const void *to) { \
1538     upb_refcounted_donateref(upcastfunc(v), from, to); \
1539   } \
1540   UPB_INLINE void type ## _checkref(const type *v, const void *owner) { \
1541     upb_refcounted_checkref(upcastfunc(v), owner); \
1542   }
1543 
1544 #define UPB_REFCOUNTED_CPPMETHODS \
1545   bool IsFrozen() const { \
1546     return upb::upcast_to<const upb::RefCounted>(this)->IsFrozen(); \
1547   } \
1548   void Ref(const void *owner) const { \
1549     return upb::upcast_to<const upb::RefCounted>(this)->Ref(owner); \
1550   } \
1551   void Unref(const void *owner) const { \
1552     return upb::upcast_to<const upb::RefCounted>(this)->Unref(owner); \
1553   } \
1554   void DonateRef(const void *from, const void *to) const { \
1555     return upb::upcast_to<const upb::RefCounted>(this)->DonateRef(from, to); \
1556   } \
1557   void CheckRef(const void *owner) const { \
1558     return upb::upcast_to<const upb::RefCounted>(this)->CheckRef(owner); \
1559   }
1560 
1561 /* Internal-to-upb Interface **************************************************/
1562 
1563 typedef void upb_refcounted_visit(const upb_refcounted *r,
1564                                   const upb_refcounted *subobj,
1565                                   void *closure);
1566 
1567 struct upb_refcounted_vtbl {
1568   /* Must visit all subobjects that are currently ref'd via upb_refcounted_ref2.
1569    * Must be longjmp()-safe. */
1570   void (*visit)(const upb_refcounted *r, upb_refcounted_visit *visit, void *c);
1571 
1572   /* Must free the object and release all references to other objects. */
1573   void (*free)(upb_refcounted *r);
1574 };
1575 
1576 /* Initializes the refcounted with a single ref for the given owner.  Returns
1577  * false if memory could not be allocated. */
1578 bool upb_refcounted_init(upb_refcounted *r,
1579                          const struct upb_refcounted_vtbl *vtbl,
1580                          const void *owner);
1581 
1582 /* Adds a ref from one refcounted object to another ("from" must not already
1583  * own a ref).  These refs may be circular; cycles will be collected correctly
1584  * (if conservatively).  These refs do not need to be freed in from's free()
1585  * function. */
1586 void upb_refcounted_ref2(const upb_refcounted *r, upb_refcounted *from);
1587 
1588 /* Removes a ref that was acquired from upb_refcounted_ref2(), and collects any
1589  * object it can.  This is only necessary when "from" no longer points to "r",
1590  * and not from from's "free" function. */
1591 void upb_refcounted_unref2(const upb_refcounted *r, upb_refcounted *from);
1592 
1593 #define upb_ref2(r, from) \
1594     upb_refcounted_ref2((const upb_refcounted*)r, (upb_refcounted*)from)
1595 #define upb_unref2(r, from) \
1596     upb_refcounted_unref2((const upb_refcounted*)r, (upb_refcounted*)from)
1597 
1598 /* Freezes all mutable object reachable by ref2() refs from the given roots.
1599  * This will split refcounting groups into precise SCC groups, so that
1600  * refcounting of frozen objects can be more aggressive.  If memory allocation
1601  * fails, or if more than 2**31 mutable objects are reachable from "roots", or
1602  * if the maximum depth of the graph exceeds "maxdepth", false is returned and
1603  * the objects are unchanged.
1604  *
1605  * After this operation succeeds, the objects are frozen/const, and may not be
1606  * used through non-const pointers.  In particular, they may not be passed as
1607  * the second parameter of upb_refcounted_{ref,unref}2().  On the upside, all
1608  * operations on frozen refcounteds are threadsafe, and objects will be freed
1609  * at the precise moment that they become unreachable.
1610  *
1611  * Caller must own refs on each object in the "roots" list. */
1612 bool upb_refcounted_freeze(upb_refcounted *const*roots, int n, upb_status *s,
1613                            int maxdepth);
1614 
1615 /* Shared by all compiled-in refcounted objects. */
1616 extern uint32_t static_refcount;
1617 
1618 UPB_END_EXTERN_C
1619 
1620 #ifdef __cplusplus
1621 /* C++ Wrappers. */
1622 namespace upb {
1623 inline bool RefCounted::IsFrozen() const {
1624   return upb_refcounted_isfrozen(this);
1625 }
1626 inline void RefCounted::Ref(const void *owner) const {
1627   upb_refcounted_ref(this, owner);
1628 }
1629 inline void RefCounted::Unref(const void *owner) const {
1630   upb_refcounted_unref(this, owner);
1631 }
1632 inline void RefCounted::DonateRef(const void *from, const void *to) const {
1633   upb_refcounted_donateref(this, from, to);
1634 }
1635 inline void RefCounted::CheckRef(const void *owner) const {
1636   upb_refcounted_checkref(this, owner);
1637 }
1638 }  /* namespace upb */
1639 #endif
1640 
1641 
1642 /* upb::reffed_ptr ************************************************************/
1643 
1644 #ifdef __cplusplus
1645 
1646 #include <algorithm>  /* For std::swap(). */
1647 
1648 /* Provides RAII semantics for upb refcounted objects.  Each reffed_ptr owns a
1649  * ref on whatever object it points to (if any). */
1650 template <class T> class upb::reffed_ptr {
1651  public:
1652   reffed_ptr() : ptr_(NULL) {}
1653 
1654   /* If ref_donor is NULL, takes a new ref, otherwise adopts from ref_donor. */
1655   template <class U>
1656   reffed_ptr(U* val, const void* ref_donor = NULL)
1657       : ptr_(upb::upcast(val)) {
1658     if (ref_donor) {
1659       UPB_ASSERT(ptr_);
1660       ptr_->DonateRef(ref_donor, this);
1661     } else if (ptr_) {
1662       ptr_->Ref(this);
1663     }
1664   }
1665 
1666   template <class U>
1667   reffed_ptr(const reffed_ptr<U>& other)
1668       : ptr_(upb::upcast(other.get())) {
1669     if (ptr_) ptr_->Ref(this);
1670   }
1671 
1672   reffed_ptr(const reffed_ptr& other)
1673       : ptr_(upb::upcast(other.get())) {
1674     if (ptr_) ptr_->Ref(this);
1675   }
1676 
1677   ~reffed_ptr() { if (ptr_) ptr_->Unref(this); }
1678 
1679   template <class U>
1680   reffed_ptr& operator=(const reffed_ptr<U>& other) {
1681     reset(other.get());
1682     return *this;
1683   }
1684 
1685   reffed_ptr& operator=(const reffed_ptr& other) {
1686     reset(other.get());
1687     return *this;
1688   }
1689 
1690   /* TODO(haberman): add C++11 move construction/assignment for greater
1691    * efficiency. */
1692 
1693   void swap(reffed_ptr& other) {
1694     if (ptr_ == other.ptr_) {
1695       return;
1696     }
1697 
1698     if (ptr_) ptr_->DonateRef(this, &other);
1699     if (other.ptr_) other.ptr_->DonateRef(&other, this);
1700     std::swap(ptr_, other.ptr_);
1701   }
1702 
1703   T& operator*() const {
1704     UPB_ASSERT(ptr_);
1705     return *ptr_;
1706   }
1707 
1708   T* operator->() const {
1709     UPB_ASSERT(ptr_);
1710     return ptr_;
1711   }
1712 
1713   T* get() const { return ptr_; }
1714 
1715   /* If ref_donor is NULL, takes a new ref, otherwise adopts from ref_donor. */
1716   template <class U>
1717   void reset(U* ptr = NULL, const void* ref_donor = NULL) {
1718     reffed_ptr(ptr, ref_donor).swap(*this);
1719   }
1720 
1721   template <class U>
1722   reffed_ptr<U> down_cast() {
1723     return reffed_ptr<U>(upb::down_cast<U*>(get()));
1724   }
1725 
1726   template <class U>
1727   reffed_ptr<U> dyn_cast() {
1728     return reffed_ptr<U>(upb::dyn_cast<U*>(get()));
1729   }
1730 
1731   /* Plain release() is unsafe; if we were the only owner, it would leak the
1732    * object.  Instead we provide this: */
1733   T* ReleaseTo(const void* new_owner) {
1734     T* ret = NULL;
1735     ptr_->DonateRef(this, new_owner);
1736     std::swap(ret, ptr_);
1737     return ret;
1738   }
1739 
1740  private:
1741   T* ptr_;
1742 };
1743 
1744 #endif  /* __cplusplus */
1745 
1746 #endif  /* UPB_REFCOUNT_H_ */
1747 
1748 #ifdef __cplusplus
1749 #include <cstring>
1750 #include <string>
1751 #include <vector>
1752 
1753 namespace upb {
1754 class Def;
1755 class EnumDef;
1756 class FieldDef;
1757 class FileDef;
1758 class MessageDef;
1759 class OneofDef;
1760 class SymbolTable;
1761 }
1762 #endif
1763 
1764 UPB_DECLARE_DERIVED_TYPE(upb::Def, upb::RefCounted, upb_def, upb_refcounted)
1765 UPB_DECLARE_DERIVED_TYPE(upb::OneofDef, upb::RefCounted, upb_oneofdef,
1766                          upb_refcounted)
1767 UPB_DECLARE_DERIVED_TYPE(upb::FileDef, upb::RefCounted, upb_filedef,
1768                          upb_refcounted)
1769 UPB_DECLARE_TYPE(upb::SymbolTable, upb_symtab)
1770 
1771 
1772 /* The maximum message depth that the type graph can have.  This is a resource
1773  * limit for the C stack since we sometimes need to recursively traverse the
1774  * graph.  Cycles are ok; the traversal will stop when it detects a cycle, but
1775  * we must hit the cycle before the maximum depth is reached.
1776  *
1777  * If having a single static limit is too inflexible, we can add another variant
1778  * of Def::Freeze that allows specifying this as a parameter. */
1779 #define UPB_MAX_MESSAGE_DEPTH 64
1780 
1781 
1782 /* upb::Def: base class for top-level defs  ***********************************/
1783 
1784 /* All the different kind of defs that can be defined at the top-level and put
1785  * in a SymbolTable or appear in a FileDef::defs() list.  This excludes some
1786  * defs (like oneofs and files).  It only includes fields because they can be
1787  * defined as extensions. */
1788 typedef enum {
1789   UPB_DEF_MSG,
1790   UPB_DEF_FIELD,
1791   UPB_DEF_ENUM,
1792   UPB_DEF_SERVICE,   /* Not yet implemented. */
1793   UPB_DEF_ANY = -1   /* Wildcard for upb_symtab_get*() */
1794 } upb_deftype_t;
1795 
1796 #ifdef __cplusplus
1797 
1798 /* The base class of all defs.  Its base is upb::RefCounted (use upb::upcast()
1799  * to convert). */
1800 class upb::Def {
1801  public:
1802   typedef upb_deftype_t Type;
1803 
1804   /* upb::RefCounted methods like Ref()/Unref(). */
1805   UPB_REFCOUNTED_CPPMETHODS
1806 
1807   Type def_type() const;
1808 
1809   /* "fullname" is the def's fully-qualified name (eg. foo.bar.Message). */
1810   const char *full_name() const;
1811 
1812   /* The final part of a def's name (eg. Message). */
1813   const char *name() const;
1814 
1815   /* The def must be mutable.  Caller retains ownership of fullname.  Defs are
1816    * not required to have a name; if a def has no name when it is frozen, it
1817    * will remain an anonymous def.  On failure, returns false and details in "s"
1818    * if non-NULL. */
1819   bool set_full_name(const char* fullname, upb::Status* s);
1820   bool set_full_name(const std::string &fullname, upb::Status* s);
1821 
1822   /* The file in which this def appears.  It is not necessary to add a def to a
1823    * file (and consequently the accessor may return NULL).  Set this by calling
1824    * file->Add(def). */
1825   FileDef* file() const;
1826 
1827   /* Freezes the given defs; this validates all constraints and marks the defs
1828    * as frozen (read-only).  "defs" may not contain any fielddefs, but fields
1829    * of any msgdefs will be frozen.
1830    *
1831    * Symbolic references to sub-types and enum defaults must have already been
1832    * resolved.  Any mutable defs reachable from any of "defs" must also be in
1833    * the list; more formally, "defs" must be a transitive closure of mutable
1834    * defs.
1835    *
1836    * After this operation succeeds, the finalized defs must only be accessed
1837    * through a const pointer! */
1838   static bool Freeze(Def* const* defs, size_t n, Status* status);
1839   static bool Freeze(const std::vector<Def*>& defs, Status* status);
1840 
1841  private:
1842   UPB_DISALLOW_POD_OPS(Def, upb::Def)
1843 #else
1844 struct upb_def {
1845   upb_refcounted base;
1846 
1847   const char *fullname;
1848   const upb_filedef* file;
1849   char type;  /* A upb_deftype_t (char to save space) */
1850 
1851   /* Used as a flag during the def's mutable stage.  Must be false unless
1852    * it is currently being used by a function on the stack.  This allows
1853    * us to easily determine which defs were passed into the function's
1854    * current invocation. */
1855   bool came_from_user;
1856 #endif
1857 };
1858 
1859 #define UPB_DEF_INIT(name, type, vtbl, refs, ref2s) \
1860     { UPB_REFCOUNT_INIT(vtbl, refs, ref2s), name, NULL, type, false }
1861 
1862 UPB_BEGIN_EXTERN_C
1863 
1864 /* Include upb_refcounted methods like upb_def_ref()/upb_def_unref(). */
1865 UPB_REFCOUNTED_CMETHODS(upb_def, upb_def_upcast)
1866 
1867 upb_deftype_t upb_def_type(const upb_def *d);
1868 const char *upb_def_fullname(const upb_def *d);
1869 const char *upb_def_name(const upb_def *d);
1870 const upb_filedef *upb_def_file(const upb_def *d);
1871 bool upb_def_setfullname(upb_def *def, const char *fullname, upb_status *s);
1872 bool upb_def_freeze(upb_def *const *defs, size_t n, upb_status *s);
1873 
1874 /* Temporary API: for internal use only. */
1875 bool _upb_def_validate(upb_def *const*defs, size_t n, upb_status *s);
1876 
1877 UPB_END_EXTERN_C
1878 
1879 
1880 /* upb::Def casts *************************************************************/
1881 
1882 #ifdef __cplusplus
1883 #define UPB_CPP_CASTS(cname, cpptype)                                          \
1884   namespace upb {                                                              \
1885   template <>                                                                  \
1886   inline cpptype *down_cast<cpptype *, Def>(Def * def) {                       \
1887     return upb_downcast_##cname##_mutable(def);                                \
1888   }                                                                            \
1889   template <>                                                                  \
1890   inline cpptype *dyn_cast<cpptype *, Def>(Def * def) {                        \
1891     return upb_dyncast_##cname##_mutable(def);                                 \
1892   }                                                                            \
1893   template <>                                                                  \
1894   inline const cpptype *down_cast<const cpptype *, const Def>(                 \
1895       const Def *def) {                                                        \
1896     return upb_downcast_##cname(def);                                          \
1897   }                                                                            \
1898   template <>                                                                  \
1899   inline const cpptype *dyn_cast<const cpptype *, const Def>(const Def *def) { \
1900     return upb_dyncast_##cname(def);                                           \
1901   }                                                                            \
1902   template <>                                                                  \
1903   inline const cpptype *down_cast<const cpptype *, Def>(Def * def) {           \
1904     return upb_downcast_##cname(def);                                          \
1905   }                                                                            \
1906   template <>                                                                  \
1907   inline const cpptype *dyn_cast<const cpptype *, Def>(Def * def) {            \
1908     return upb_dyncast_##cname(def);                                           \
1909   }                                                                            \
1910   }  /* namespace upb */
1911 #else
1912 #define UPB_CPP_CASTS(cname, cpptype)
1913 #endif  /* __cplusplus */
1914 
1915 /* Dynamic casts, for determining if a def is of a particular type at runtime.
1916  * Downcasts, for when some wants to assert that a def is of a particular type.
1917  * These are only checked if we are building debug. */
1918 #define UPB_DEF_CASTS(lower, upper, cpptype)                               \
1919   UPB_INLINE const upb_##lower *upb_dyncast_##lower(const upb_def *def) {  \
1920     if (upb_def_type(def) != UPB_DEF_##upper) return NULL;                 \
1921     return (upb_##lower *)def;                                             \
1922   }                                                                        \
1923   UPB_INLINE const upb_##lower *upb_downcast_##lower(const upb_def *def) { \
1924     UPB_ASSERT(upb_def_type(def) == UPB_DEF_##upper);                          \
1925     return (const upb_##lower *)def;                                       \
1926   }                                                                        \
1927   UPB_INLINE upb_##lower *upb_dyncast_##lower##_mutable(upb_def *def) {    \
1928     return (upb_##lower *)upb_dyncast_##lower(def);                        \
1929   }                                                                        \
1930   UPB_INLINE upb_##lower *upb_downcast_##lower##_mutable(upb_def *def) {   \
1931     return (upb_##lower *)upb_downcast_##lower(def);                       \
1932   }                                                                        \
1933   UPB_CPP_CASTS(lower, cpptype)
1934 
1935 #define UPB_DEFINE_DEF(cppname, lower, upper, cppmethods, members)             \
1936   UPB_DEFINE_CLASS2(cppname, upb::Def, upb::RefCounted, cppmethods,            \
1937                    members)                                                    \
1938   UPB_DEF_CASTS(lower, upper, cppname)
1939 
1940 #define UPB_DECLARE_DEF_TYPE(cppname, lower, upper) \
1941   UPB_DECLARE_DERIVED_TYPE2(cppname, upb::Def, upb::RefCounted, \
1942                             upb_ ## lower, upb_def, upb_refcounted) \
1943   UPB_DEF_CASTS(lower, upper, cppname)
1944 
1945 UPB_DECLARE_DEF_TYPE(upb::FieldDef, fielddef, FIELD)
1946 UPB_DECLARE_DEF_TYPE(upb::MessageDef, msgdef, MSG)
1947 UPB_DECLARE_DEF_TYPE(upb::EnumDef, enumdef, ENUM)
1948 
1949 #undef UPB_DECLARE_DEF_TYPE
1950 #undef UPB_DEF_CASTS
1951 #undef UPB_CPP_CASTS
1952 
1953 
1954 /* upb::FieldDef **************************************************************/
1955 
1956 /* The types a field can have.  Note that this list is not identical to the
1957  * types defined in descriptor.proto, which gives INT32 and SINT32 separate
1958  * types (we distinguish the two with the "integer encoding" enum below). */
1959 typedef enum {
1960   /* Types stored in 1 byte. */
1961   UPB_TYPE_BOOL     = 1,
1962   /* Types stored in 4 bytes. */
1963   UPB_TYPE_FLOAT    = 2,
1964   UPB_TYPE_INT32    = 3,
1965   UPB_TYPE_UINT32   = 4,
1966   UPB_TYPE_ENUM     = 5,  /* Enum values are int32. */
1967   /* Types stored as pointers (probably 4 or 8 bytes). */
1968   UPB_TYPE_STRING   = 6,
1969   UPB_TYPE_BYTES    = 7,
1970   UPB_TYPE_MESSAGE  = 8,
1971   /* Types stored as 8 bytes. */
1972   UPB_TYPE_DOUBLE   = 9,
1973   UPB_TYPE_INT64    = 10,
1974   UPB_TYPE_UINT64   = 11
1975 } upb_fieldtype_t;
1976 
1977 /* The repeated-ness of each field; this matches descriptor.proto. */
1978 typedef enum {
1979   UPB_LABEL_OPTIONAL = 1,
1980   UPB_LABEL_REQUIRED = 2,
1981   UPB_LABEL_REPEATED = 3
1982 } upb_label_t;
1983 
1984 /* How integers should be encoded in serializations that offer multiple
1985  * integer encoding methods. */
1986 typedef enum {
1987   UPB_INTFMT_VARIABLE = 1,
1988   UPB_INTFMT_FIXED = 2,
1989   UPB_INTFMT_ZIGZAG = 3   /* Only for signed types (INT32/INT64). */
1990 } upb_intfmt_t;
1991 
1992 /* Descriptor types, as defined in descriptor.proto. */
1993 typedef enum {
1994   UPB_DESCRIPTOR_TYPE_DOUBLE   = 1,
1995   UPB_DESCRIPTOR_TYPE_FLOAT    = 2,
1996   UPB_DESCRIPTOR_TYPE_INT64    = 3,
1997   UPB_DESCRIPTOR_TYPE_UINT64   = 4,
1998   UPB_DESCRIPTOR_TYPE_INT32    = 5,
1999   UPB_DESCRIPTOR_TYPE_FIXED64  = 6,
2000   UPB_DESCRIPTOR_TYPE_FIXED32  = 7,
2001   UPB_DESCRIPTOR_TYPE_BOOL     = 8,
2002   UPB_DESCRIPTOR_TYPE_STRING   = 9,
2003   UPB_DESCRIPTOR_TYPE_GROUP    = 10,
2004   UPB_DESCRIPTOR_TYPE_MESSAGE  = 11,
2005   UPB_DESCRIPTOR_TYPE_BYTES    = 12,
2006   UPB_DESCRIPTOR_TYPE_UINT32   = 13,
2007   UPB_DESCRIPTOR_TYPE_ENUM     = 14,
2008   UPB_DESCRIPTOR_TYPE_SFIXED32 = 15,
2009   UPB_DESCRIPTOR_TYPE_SFIXED64 = 16,
2010   UPB_DESCRIPTOR_TYPE_SINT32   = 17,
2011   UPB_DESCRIPTOR_TYPE_SINT64   = 18
2012 } upb_descriptortype_t;
2013 
2014 typedef enum {
2015   UPB_SYNTAX_PROTO2 = 2,
2016   UPB_SYNTAX_PROTO3 = 3
2017 } upb_syntax_t;
2018 
2019 /* All the different kind of well known type messages. For simplicity of check,
2020  * number wrappers and string wrappers are grouped together. Make sure the
2021  * order and merber of these groups are not changed.
2022  */
2023 typedef enum {
2024   UPB_WELLKNOWN_UNSPECIFIED,
2025   UPB_WELLKNOWN_ANY,
2026   UPB_WELLKNOWN_FIELDMASK,
2027   UPB_WELLKNOWN_DURATION,
2028   UPB_WELLKNOWN_TIMESTAMP,
2029   /* number wrappers */
2030   UPB_WELLKNOWN_DOUBLEVALUE,
2031   UPB_WELLKNOWN_FLOATVALUE,
2032   UPB_WELLKNOWN_INT64VALUE,
2033   UPB_WELLKNOWN_UINT64VALUE,
2034   UPB_WELLKNOWN_INT32VALUE,
2035   UPB_WELLKNOWN_UINT32VALUE,
2036   /* string wrappers */
2037   UPB_WELLKNOWN_STRINGVALUE,
2038   UPB_WELLKNOWN_BYTESVALUE,
2039   UPB_WELLKNOWN_BOOLVALUE,
2040   UPB_WELLKNOWN_VALUE,
2041   UPB_WELLKNOWN_LISTVALUE,
2042   UPB_WELLKNOWN_STRUCT
2043 } upb_wellknowntype_t;
2044 
2045 
2046 /* Maps descriptor type -> upb field type.  */
2047 extern const uint8_t upb_desctype_to_fieldtype[];
2048 
2049 /* Maximum field number allowed for FieldDefs.  This is an inherent limit of the
2050  * protobuf wire format. */
2051 #define UPB_MAX_FIELDNUMBER ((1 << 29) - 1)
2052 
2053 #ifdef __cplusplus
2054 
2055 /* A upb_fielddef describes a single field in a message.  It is most often
2056  * found as a part of a upb_msgdef, but can also stand alone to represent
2057  * an extension.
2058  *
2059  * Its base class is upb::Def (use upb::upcast() to convert). */
2060 class upb::FieldDef {
2061  public:
2062   typedef upb_fieldtype_t Type;
2063   typedef upb_label_t Label;
2064   typedef upb_intfmt_t IntegerFormat;
2065   typedef upb_descriptortype_t DescriptorType;
2066 
2067   /* These return true if the given value is a valid member of the enumeration. */
2068   static bool CheckType(int32_t val);
2069   static bool CheckLabel(int32_t val);
2070   static bool CheckDescriptorType(int32_t val);
2071   static bool CheckIntegerFormat(int32_t val);
2072 
2073   /* These convert to the given enumeration; they require that the value is
2074    * valid. */
2075   static Type ConvertType(int32_t val);
2076   static Label ConvertLabel(int32_t val);
2077   static DescriptorType ConvertDescriptorType(int32_t val);
2078   static IntegerFormat ConvertIntegerFormat(int32_t val);
2079 
2080   /* Returns NULL if memory allocation failed. */
2081   static reffed_ptr<FieldDef> New();
2082 
2083   /* upb::RefCounted methods like Ref()/Unref(). */
2084   UPB_REFCOUNTED_CPPMETHODS
2085 
2086   /* Functionality from upb::Def. */
2087   const char* full_name() const;
2088 
2089   bool type_is_set() const;  /* set_[descriptor_]type() has been called? */
2090   Type type() const;         /* Requires that type_is_set() == true. */
2091   Label label() const;       /* Defaults to UPB_LABEL_OPTIONAL. */
2092   const char* name() const;  /* NULL if uninitialized. */
2093   uint32_t number() const;   /* Returns 0 if uninitialized. */
2094   bool is_extension() const;
2095 
2096   /* Copies the JSON name for this field into the given buffer.  Returns the
2097    * actual size of the JSON name, including the NULL terminator.  If the
2098    * return value is 0, the JSON name is unset.  If the return value is
2099    * greater than len, the JSON name was truncated.  The buffer is always
2100    * NULL-terminated if len > 0.
2101    *
2102    * The JSON name always defaults to a camelCased version of the regular
2103    * name.  However if the regular name is unset, the JSON name will be unset
2104    * also.
2105    */
2106   size_t GetJsonName(char* buf, size_t len) const;
2107 
2108   /* Convenience version of the above function which copies the JSON name
2109    * into the given string, returning false if the name is not set. */
2110   template <class T>
2111   bool GetJsonName(T* str) {
2112     str->resize(GetJsonName(NULL, 0));
2113     GetJsonName(&(*str)[0], str->size());
2114     return str->size() > 0;
2115   }
2116 
2117   /* For UPB_TYPE_MESSAGE fields only where is_tag_delimited() == false,
2118    * indicates whether this field should have lazy parsing handlers that yield
2119    * the unparsed string for the submessage.
2120    *
2121    * TODO(haberman): I think we want to move this into a FieldOptions container
2122    * when we add support for custom options (the FieldOptions struct will
2123    * contain both regular FieldOptions like "lazy" *and* custom options). */
2124   bool lazy() const;
2125 
2126   /* For non-string, non-submessage fields, this indicates whether binary
2127    * protobufs are encoded in packed or non-packed format.
2128    *
2129    * TODO(haberman): see note above about putting options like this into a
2130    * FieldOptions container. */
2131   bool packed() const;
2132 
2133   /* An integer that can be used as an index into an array of fields for
2134    * whatever message this field belongs to.  Guaranteed to be less than
2135    * f->containing_type()->field_count().  May only be accessed once the def has
2136    * been finalized. */
2137   uint32_t index() const;
2138 
2139   /* The MessageDef to which this field belongs.
2140    *
2141    * If this field has been added to a MessageDef, that message can be retrieved
2142    * directly (this is always the case for frozen FieldDefs).
2143    *
2144    * If the field has not yet been added to a MessageDef, you can set the name
2145    * of the containing type symbolically instead.  This is mostly useful for
2146    * extensions, where the extension is declared separately from the message. */
2147   const MessageDef* containing_type() const;
2148   const char* containing_type_name();
2149 
2150   /* The OneofDef to which this field belongs, or NULL if this field is not part
2151    * of a oneof. */
2152   const OneofDef* containing_oneof() const;
2153 
2154   /* The field's type according to the enum in descriptor.proto.  This is not
2155    * the same as UPB_TYPE_*, because it distinguishes between (for example)
2156    * INT32 and SINT32, whereas our "type" enum does not.  This return of
2157    * descriptor_type() is a function of type(), integer_format(), and
2158    * is_tag_delimited().  Likewise set_descriptor_type() sets all three
2159    * appropriately. */
2160   DescriptorType descriptor_type() const;
2161 
2162   /* Convenient field type tests. */
2163   bool IsSubMessage() const;
2164   bool IsString() const;
2165   bool IsSequence() const;
2166   bool IsPrimitive() const;
2167   bool IsMap() const;
2168 
2169   /* Returns whether this field explicitly represents presence.
2170    *
2171    * For proto2 messages: Returns true for any scalar (non-repeated) field.
2172    * For proto3 messages: Returns true for scalar submessage or oneof fields. */
2173   bool HasPresence() const;
2174 
2175   /* How integers are encoded.  Only meaningful for integer types.
2176    * Defaults to UPB_INTFMT_VARIABLE, and is reset when "type" changes. */
2177   IntegerFormat integer_format() const;
2178 
2179   /* Whether a submessage field is tag-delimited or not (if false, then
2180    * length-delimited).  May only be set when type() == UPB_TYPE_MESSAGE. */
2181   bool is_tag_delimited() const;
2182 
2183   /* Returns the non-string default value for this fielddef, which may either
2184    * be something the client set explicitly or the "default default" (0 for
2185    * numbers, empty for strings).  The field's type indicates the type of the
2186    * returned value, except for enum fields that are still mutable.
2187    *
2188    * Requires that the given function matches the field's current type. */
2189   int64_t default_int64() const;
2190   int32_t default_int32() const;
2191   uint64_t default_uint64() const;
2192   uint32_t default_uint32() const;
2193   bool default_bool() const;
2194   float default_float() const;
2195   double default_double() const;
2196 
2197   /* The resulting string is always NULL-terminated.  If non-NULL, the length
2198    * will be stored in *len. */
2199   const char *default_string(size_t* len) const;
2200 
2201   /* For frozen UPB_TYPE_ENUM fields, enum defaults can always be read as either
2202    * string or int32, and both of these methods will always return true.
2203    *
2204    * For mutable UPB_TYPE_ENUM fields, the story is a bit more complicated.
2205    * Enum defaults are unusual. They can be specified either as string or int32,
2206    * but to be valid the enum must have that value as a member.  And if no
2207    * default is specified, the "default default" comes from the EnumDef.
2208    *
2209    * We allow reading the default as either an int32 or a string, but only if
2210    * we have a meaningful value to report.  We have a meaningful value if it was
2211    * set explicitly, or if we could get the "default default" from the EnumDef.
2212    * Also if you explicitly set the name and we find the number in the EnumDef */
2213   bool EnumHasStringDefault() const;
2214   bool EnumHasInt32Default() const;
2215 
2216   /* Submessage and enum fields must reference a "subdef", which is the
2217    * upb::MessageDef or upb::EnumDef that defines their type.  Note that when
2218    * the FieldDef is mutable it may not have a subdef *yet*, but this function
2219    * still returns true to indicate that the field's type requires a subdef. */
2220   bool HasSubDef() const;
2221 
2222   /* Returns the enum or submessage def for this field, if any.  The field's
2223    * type must match (ie. you may only call enum_subdef() for fields where
2224    * type() == UPB_TYPE_ENUM).  Returns NULL if the subdef has not been set or
2225    * is currently set symbolically. */
2226   const EnumDef* enum_subdef() const;
2227   const MessageDef* message_subdef() const;
2228 
2229   /* Returns the generic subdef for this field.  Requires that HasSubDef() (ie.
2230    * only works for UPB_TYPE_ENUM and UPB_TYPE_MESSAGE fields). */
2231   const Def* subdef() const;
2232 
2233   /* Returns the symbolic name of the subdef.  If the subdef is currently set
2234    * unresolved (ie. set symbolically) returns the symbolic name.  If it has
2235    * been resolved to a specific subdef, returns the name from that subdef. */
2236   const char* subdef_name() const;
2237 
2238   /* Setters (non-const methods), only valid for mutable FieldDefs! ***********/
2239 
2240   bool set_full_name(const char* fullname, upb::Status* s);
2241   bool set_full_name(const std::string& fullname, upb::Status* s);
2242 
2243   /* This may only be called if containing_type() == NULL (ie. the field has not
2244    * been added to a message yet). */
2245   bool set_containing_type_name(const char *name, Status* status);
2246   bool set_containing_type_name(const std::string& name, Status* status);
2247 
2248   /* Defaults to false.  When we freeze, we ensure that this can only be true
2249    * for length-delimited message fields.  Prior to freezing this can be true or
2250    * false with no restrictions. */
2251   void set_lazy(bool lazy);
2252 
2253   /* Defaults to true.  Sets whether this field is encoded in packed format. */
2254   void set_packed(bool packed);
2255 
2256   /* "type" or "descriptor_type" MUST be set explicitly before the fielddef is
2257    * finalized.  These setters require that the enum value is valid; if the
2258    * value did not come directly from an enum constant, the caller should
2259    * validate it first with the functions above (CheckFieldType(), etc). */
2260   void set_type(Type type);
2261   void set_label(Label label);
2262   void set_descriptor_type(DescriptorType type);
2263   void set_is_extension(bool is_extension);
2264 
2265   /* "number" and "name" must be set before the FieldDef is added to a
2266    * MessageDef, and may not be set after that.
2267    *
2268    * "name" is the same as full_name()/set_full_name(), but since fielddefs
2269    * most often use simple, non-qualified names, we provide this accessor
2270    * also.  Generally only extensions will want to think of this name as
2271    * fully-qualified. */
2272   bool set_number(uint32_t number, upb::Status* s);
2273   bool set_name(const char* name, upb::Status* s);
2274   bool set_name(const std::string& name, upb::Status* s);
2275 
2276   /* Sets the JSON name to the given string. */
2277   /* TODO(haberman): implement.  Right now only default json_name (camelCase)
2278    * is supported. */
2279   bool set_json_name(const char* json_name, upb::Status* s);
2280   bool set_json_name(const std::string& name, upb::Status* s);
2281 
2282   /* Clears the JSON name. This will make it revert to its default, which is
2283    * a camelCased version of the regular field name. */
2284   void clear_json_name();
2285 
2286   void set_integer_format(IntegerFormat format);
2287   bool set_tag_delimited(bool tag_delimited, upb::Status* s);
2288 
2289   /* Sets default value for the field.  The call must exactly match the type
2290    * of the field.  Enum fields may use either setint32 or setstring to set
2291    * the default numerically or symbolically, respectively, but symbolic
2292    * defaults must be resolved before finalizing (see ResolveEnumDefault()).
2293    *
2294    * Changing the type of a field will reset its default. */
2295   void set_default_int64(int64_t val);
2296   void set_default_int32(int32_t val);
2297   void set_default_uint64(uint64_t val);
2298   void set_default_uint32(uint32_t val);
2299   void set_default_bool(bool val);
2300   void set_default_float(float val);
2301   void set_default_double(double val);
2302   bool set_default_string(const void *str, size_t len, Status *s);
2303   bool set_default_string(const std::string &str, Status *s);
2304   void set_default_cstr(const char *str, Status *s);
2305 
2306   /* Before a fielddef is frozen, its subdef may be set either directly (with a
2307    * upb::Def*) or symbolically.  Symbolic refs must be resolved before the
2308    * containing msgdef can be frozen (see upb_resolve() above).  upb always
2309    * guarantees that any def reachable from a live def will also be kept alive.
2310    *
2311    * Both methods require that upb_hassubdef(f) (so the type must be set prior
2312    * to calling these methods).  Returns false if this is not the case, or if
2313    * the given subdef is not of the correct type.  The subdef is reset if the
2314    * field's type is changed.  The subdef can be set to NULL to clear it. */
2315   bool set_subdef(const Def* subdef, Status* s);
2316   bool set_enum_subdef(const EnumDef* subdef, Status* s);
2317   bool set_message_subdef(const MessageDef* subdef, Status* s);
2318   bool set_subdef_name(const char* name, Status* s);
2319   bool set_subdef_name(const std::string &name, Status* s);
2320 
2321  private:
2322   UPB_DISALLOW_POD_OPS(FieldDef, upb::FieldDef)
2323 #else
2324 struct upb_fielddef {
2325   upb_def base;
2326 
2327   union {
2328     int64_t sint;
2329     uint64_t uint;
2330     double dbl;
2331     float flt;
2332     void *bytes;
2333   } defaultval;
2334   union {
2335     const upb_msgdef *def;  /* If !msg_is_symbolic. */
2336     char *name;             /* If msg_is_symbolic. */
2337   } msg;
2338   union {
2339     const upb_def *def;  /* If !subdef_is_symbolic. */
2340     char *name;          /* If subdef_is_symbolic. */
2341   } sub;  /* The msgdef or enumdef for this field, if upb_hassubdef(f). */
2342   bool subdef_is_symbolic;
2343   bool msg_is_symbolic;
2344   const upb_oneofdef *oneof;
2345   bool default_is_string;
2346   bool type_is_set_;     /* False until type is explicitly set. */
2347   bool is_extension_;
2348   bool lazy_;
2349   bool packed_;
2350   upb_intfmt_t intfmt;
2351   bool tagdelim;
2352   upb_fieldtype_t type_;
2353   upb_label_t label_;
2354   uint32_t number_;
2355   uint32_t selector_base;  /* Used to index into a upb::Handlers table. */
2356   uint32_t index_;
2357 # endif  /* defined(__cplusplus) */
2358 };
2359 
2360 UPB_BEGIN_EXTERN_C
2361 
2362 extern const struct upb_refcounted_vtbl upb_fielddef_vtbl;
2363 
2364 #define UPB_FIELDDEF_INIT(label, type, intfmt, tagdelim, is_extension, lazy,   \
2365                           packed, name, num, msgdef, subdef, selector_base,    \
2366                           index, defaultval, refs, ref2s)                      \
2367   {                                                                            \
2368     UPB_DEF_INIT(name, UPB_DEF_FIELD, &upb_fielddef_vtbl, refs, ref2s),        \
2369         defaultval, {msgdef}, {subdef}, NULL, false, false,                    \
2370         type == UPB_TYPE_STRING || type == UPB_TYPE_BYTES, true, is_extension, \
2371         lazy, packed, intfmt, tagdelim, type, label, num, selector_base, index \
2372   }
2373 
2374 /* Native C API. */
2375 upb_fielddef *upb_fielddef_new(const void *owner);
2376 
2377 /* Include upb_refcounted methods like upb_fielddef_ref(). */
2378 UPB_REFCOUNTED_CMETHODS(upb_fielddef, upb_fielddef_upcast2)
2379 
2380 /* Methods from upb_def. */
2381 const char *upb_fielddef_fullname(const upb_fielddef *f);
2382 bool upb_fielddef_setfullname(upb_fielddef *f, const char *fullname,
2383                               upb_status *s);
2384 
2385 bool upb_fielddef_typeisset(const upb_fielddef *f);
2386 upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f);
2387 upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f);
2388 upb_label_t upb_fielddef_label(const upb_fielddef *f);
2389 uint32_t upb_fielddef_number(const upb_fielddef *f);
2390 const char *upb_fielddef_name(const upb_fielddef *f);
2391 bool upb_fielddef_isextension(const upb_fielddef *f);
2392 bool upb_fielddef_lazy(const upb_fielddef *f);
2393 bool upb_fielddef_packed(const upb_fielddef *f);
2394 size_t upb_fielddef_getjsonname(const upb_fielddef *f, char *buf, size_t len);
2395 const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f);
2396 const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f);
2397 upb_msgdef *upb_fielddef_containingtype_mutable(upb_fielddef *f);
2398 const char *upb_fielddef_containingtypename(upb_fielddef *f);
2399 upb_intfmt_t upb_fielddef_intfmt(const upb_fielddef *f);
2400 uint32_t upb_fielddef_index(const upb_fielddef *f);
2401 bool upb_fielddef_istagdelim(const upb_fielddef *f);
2402 bool upb_fielddef_issubmsg(const upb_fielddef *f);
2403 bool upb_fielddef_isstring(const upb_fielddef *f);
2404 bool upb_fielddef_isseq(const upb_fielddef *f);
2405 bool upb_fielddef_isprimitive(const upb_fielddef *f);
2406 bool upb_fielddef_ismap(const upb_fielddef *f);
2407 bool upb_fielddef_haspresence(const upb_fielddef *f);
2408 int64_t upb_fielddef_defaultint64(const upb_fielddef *f);
2409 int32_t upb_fielddef_defaultint32(const upb_fielddef *f);
2410 uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f);
2411 uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f);
2412 bool upb_fielddef_defaultbool(const upb_fielddef *f);
2413 float upb_fielddef_defaultfloat(const upb_fielddef *f);
2414 double upb_fielddef_defaultdouble(const upb_fielddef *f);
2415 const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len);
2416 bool upb_fielddef_enumhasdefaultint32(const upb_fielddef *f);
2417 bool upb_fielddef_enumhasdefaultstr(const upb_fielddef *f);
2418 bool upb_fielddef_hassubdef(const upb_fielddef *f);
2419 const upb_def *upb_fielddef_subdef(const upb_fielddef *f);
2420 const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f);
2421 const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f);
2422 const char *upb_fielddef_subdefname(const upb_fielddef *f);
2423 
2424 void upb_fielddef_settype(upb_fielddef *f, upb_fieldtype_t type);
2425 void upb_fielddef_setdescriptortype(upb_fielddef *f, int type);
2426 void upb_fielddef_setlabel(upb_fielddef *f, upb_label_t label);
2427 bool upb_fielddef_setnumber(upb_fielddef *f, uint32_t number, upb_status *s);
2428 bool upb_fielddef_setname(upb_fielddef *f, const char *name, upb_status *s);
2429 bool upb_fielddef_setjsonname(upb_fielddef *f, const char *name, upb_status *s);
2430 bool upb_fielddef_clearjsonname(upb_fielddef *f);
2431 bool upb_fielddef_setcontainingtypename(upb_fielddef *f, const char *name,
2432                                         upb_status *s);
2433 void upb_fielddef_setisextension(upb_fielddef *f, bool is_extension);
2434 void upb_fielddef_setlazy(upb_fielddef *f, bool lazy);
2435 void upb_fielddef_setpacked(upb_fielddef *f, bool packed);
2436 void upb_fielddef_setintfmt(upb_fielddef *f, upb_intfmt_t fmt);
2437 void upb_fielddef_settagdelim(upb_fielddef *f, bool tag_delim);
2438 void upb_fielddef_setdefaultint64(upb_fielddef *f, int64_t val);
2439 void upb_fielddef_setdefaultint32(upb_fielddef *f, int32_t val);
2440 void upb_fielddef_setdefaultuint64(upb_fielddef *f, uint64_t val);
2441 void upb_fielddef_setdefaultuint32(upb_fielddef *f, uint32_t val);
2442 void upb_fielddef_setdefaultbool(upb_fielddef *f, bool val);
2443 void upb_fielddef_setdefaultfloat(upb_fielddef *f, float val);
2444 void upb_fielddef_setdefaultdouble(upb_fielddef *f, double val);
2445 bool upb_fielddef_setdefaultstr(upb_fielddef *f, const void *str, size_t len,
2446                                 upb_status *s);
2447 void upb_fielddef_setdefaultcstr(upb_fielddef *f, const char *str,
2448                                  upb_status *s);
2449 bool upb_fielddef_setsubdef(upb_fielddef *f, const upb_def *subdef,
2450                             upb_status *s);
2451 bool upb_fielddef_setmsgsubdef(upb_fielddef *f, const upb_msgdef *subdef,
2452                                upb_status *s);
2453 bool upb_fielddef_setenumsubdef(upb_fielddef *f, const upb_enumdef *subdef,
2454                                 upb_status *s);
2455 bool upb_fielddef_setsubdefname(upb_fielddef *f, const char *name,
2456                                 upb_status *s);
2457 
2458 bool upb_fielddef_checklabel(int32_t label);
2459 bool upb_fielddef_checktype(int32_t type);
2460 bool upb_fielddef_checkdescriptortype(int32_t type);
2461 bool upb_fielddef_checkintfmt(int32_t fmt);
2462 
2463 UPB_END_EXTERN_C
2464 
2465 
2466 /* upb::MessageDef ************************************************************/
2467 
2468 typedef upb_inttable_iter upb_msg_field_iter;
2469 typedef upb_strtable_iter upb_msg_oneof_iter;
2470 
2471 /* Well-known field tag numbers for map-entry messages. */
2472 #define UPB_MAPENTRY_KEY   1
2473 #define UPB_MAPENTRY_VALUE 2
2474 
2475 /* Well-known field tag numbers for Any messages. */
2476 #define UPB_ANY_TYPE 1
2477 #define UPB_ANY_VALUE 2
2478 
2479 /* Well-known field tag numbers for timestamp messages. */
2480 #define UPB_DURATION_SECONDS 1
2481 #define UPB_DURATION_NANOS 2
2482 
2483 /* Well-known field tag numbers for duration messages. */
2484 #define UPB_TIMESTAMP_SECONDS 1
2485 #define UPB_TIMESTAMP_NANOS 2
2486 
2487 #ifdef __cplusplus
2488 
2489 /* Structure that describes a single .proto message type.
2490  *
2491  * Its base class is upb::Def (use upb::upcast() to convert). */
2492 class upb::MessageDef {
2493  public:
2494   /* Returns NULL if memory allocation failed. */
2495   static reffed_ptr<MessageDef> New();
2496 
2497   /* upb::RefCounted methods like Ref()/Unref(). */
2498   UPB_REFCOUNTED_CPPMETHODS
2499 
2500   /* Functionality from upb::Def. */
2501   const char* full_name() const;
2502   const char* name() const;
2503   bool set_full_name(const char* fullname, Status* s);
2504   bool set_full_name(const std::string& fullname, Status* s);
2505 
2506   /* Call to freeze this MessageDef.
2507    * WARNING: this will fail if this message has any unfrozen submessages!
2508    * Messages with cycles must be frozen as a batch using upb::Def::Freeze(). */
2509   bool Freeze(Status* s);
2510 
2511   /* The number of fields that belong to the MessageDef. */
2512   int field_count() const;
2513 
2514   /* The number of oneofs that belong to the MessageDef. */
2515   int oneof_count() const;
2516 
2517   /* Adds a field (upb_fielddef object) to a msgdef.  Requires that the msgdef
2518    * and the fielddefs are mutable.  The fielddef's name and number must be
2519    * set, and the message may not already contain any field with this name or
2520    * number, and this fielddef may not be part of another message.  In error
2521    * cases false is returned and the msgdef is unchanged.
2522    *
2523    * If the given field is part of a oneof, this call succeeds if and only if
2524    * that oneof is already part of this msgdef. (Note that adding a oneof to a
2525    * msgdef automatically adds all of its fields to the msgdef at the time that
2526    * the oneof is added, so it is usually more idiomatic to add the oneof's
2527    * fields first then add the oneof to the msgdef. This case is supported for
2528    * convenience.)
2529    *
2530    * If |f| is already part of this MessageDef, this method performs no action
2531    * and returns true (success). Thus, this method is idempotent. */
2532   bool AddField(FieldDef* f, Status* s);
2533   bool AddField(const reffed_ptr<FieldDef>& f, Status* s);
2534 
2535   /* Adds a oneof (upb_oneofdef object) to a msgdef. Requires that the msgdef,
2536    * oneof, and any fielddefs are mutable, that the fielddefs contained in the
2537    * oneof do not have any name or number conflicts with existing fields in the
2538    * msgdef, and that the oneof's name is unique among all oneofs in the msgdef.
2539    * If the oneof is added successfully, all of its fields will be added
2540    * directly to the msgdef as well. In error cases, false is returned and the
2541    * msgdef is unchanged. */
2542   bool AddOneof(OneofDef* o, Status* s);
2543   bool AddOneof(const reffed_ptr<OneofDef>& o, Status* s);
2544 
2545   upb_syntax_t syntax() const;
2546 
2547   /* Returns false if we don't support this syntax value. */
2548   bool set_syntax(upb_syntax_t syntax);
2549 
2550   /* Set this to false to indicate that primitive fields should not have
2551    * explicit presence information associated with them.  This will affect all
2552    * fields added to this message.  Defaults to true. */
2553   void SetPrimitivesHavePresence(bool have_presence);
2554 
2555   /* These return NULL if the field is not found. */
2556   FieldDef* FindFieldByNumber(uint32_t number);
2557   FieldDef* FindFieldByName(const char *name, size_t len);
2558   const FieldDef* FindFieldByNumber(uint32_t number) const;
2559   const FieldDef* FindFieldByName(const char* name, size_t len) const;
2560 
2561 
2562   FieldDef* FindFieldByName(const char *name) {
2563     return FindFieldByName(name, strlen(name));
2564   }
2565   const FieldDef* FindFieldByName(const char *name) const {
2566     return FindFieldByName(name, strlen(name));
2567   }
2568 
2569   template <class T>
2570   FieldDef* FindFieldByName(const T& str) {
2571     return FindFieldByName(str.c_str(), str.size());
2572   }
2573   template <class T>
2574   const FieldDef* FindFieldByName(const T& str) const {
2575     return FindFieldByName(str.c_str(), str.size());
2576   }
2577 
2578   OneofDef* FindOneofByName(const char* name, size_t len);
2579   const OneofDef* FindOneofByName(const char* name, size_t len) const;
2580 
2581   OneofDef* FindOneofByName(const char* name) {
2582     return FindOneofByName(name, strlen(name));
2583   }
2584   const OneofDef* FindOneofByName(const char* name) const {
2585     return FindOneofByName(name, strlen(name));
2586   }
2587 
2588   template<class T>
2589   OneofDef* FindOneofByName(const T& str) {
2590     return FindOneofByName(str.c_str(), str.size());
2591   }
2592   template<class T>
2593   const OneofDef* FindOneofByName(const T& str) const {
2594     return FindOneofByName(str.c_str(), str.size());
2595   }
2596 
2597   /* Is this message a map entry? */
2598   void setmapentry(bool map_entry);
2599   bool mapentry() const;
2600 
2601   /* Return the type of well known type message. UPB_WELLKNOWN_UNSPECIFIED for
2602    * non-well-known message. */
2603   upb_wellknowntype_t wellknowntype() const;
2604 
2605   /* Whether is a number wrapper. */
2606   bool isnumberwrapper() const;
2607 
2608   /* Iteration over fields.  The order is undefined. */
2609   class field_iterator
2610       : public std::iterator<std::forward_iterator_tag, FieldDef*> {
2611    public:
2612     explicit field_iterator(MessageDef* md);
2613     static field_iterator end(MessageDef* md);
2614 
2615     void operator++();
2616     FieldDef* operator*() const;
2617     bool operator!=(const field_iterator& other) const;
2618     bool operator==(const field_iterator& other) const;
2619 
2620    private:
2621     upb_msg_field_iter iter_;
2622   };
2623 
2624   class const_field_iterator
2625       : public std::iterator<std::forward_iterator_tag, const FieldDef*> {
2626    public:
2627     explicit const_field_iterator(const MessageDef* md);
2628     static const_field_iterator end(const MessageDef* md);
2629 
2630     void operator++();
2631     const FieldDef* operator*() const;
2632     bool operator!=(const const_field_iterator& other) const;
2633     bool operator==(const const_field_iterator& other) const;
2634 
2635    private:
2636     upb_msg_field_iter iter_;
2637   };
2638 
2639   /* Iteration over oneofs. The order is undefined. */
2640   class oneof_iterator
2641       : public std::iterator<std::forward_iterator_tag, FieldDef*> {
2642    public:
2643     explicit oneof_iterator(MessageDef* md);
2644     static oneof_iterator end(MessageDef* md);
2645 
2646     void operator++();
2647     OneofDef* operator*() const;
2648     bool operator!=(const oneof_iterator& other) const;
2649     bool operator==(const oneof_iterator& other) const;
2650 
2651    private:
2652     upb_msg_oneof_iter iter_;
2653   };
2654 
2655   class const_oneof_iterator
2656       : public std::iterator<std::forward_iterator_tag, const FieldDef*> {
2657    public:
2658     explicit const_oneof_iterator(const MessageDef* md);
2659     static const_oneof_iterator end(const MessageDef* md);
2660 
2661     void operator++();
2662     const OneofDef* operator*() const;
2663     bool operator!=(const const_oneof_iterator& other) const;
2664     bool operator==(const const_oneof_iterator& other) const;
2665 
2666    private:
2667     upb_msg_oneof_iter iter_;
2668   };
2669 
2670   class FieldAccessor {
2671    public:
2672     explicit FieldAccessor(MessageDef* msg) : msg_(msg) {}
2673     field_iterator begin() { return msg_->field_begin(); }
2674     field_iterator end() { return msg_->field_end(); }
2675    private:
2676     MessageDef* msg_;
2677   };
2678 
2679   class ConstFieldAccessor {
2680    public:
2681     explicit ConstFieldAccessor(const MessageDef* msg) : msg_(msg) {}
2682     const_field_iterator begin() { return msg_->field_begin(); }
2683     const_field_iterator end() { return msg_->field_end(); }
2684    private:
2685     const MessageDef* msg_;
2686   };
2687 
2688   class OneofAccessor {
2689    public:
2690     explicit OneofAccessor(MessageDef* msg) : msg_(msg) {}
2691     oneof_iterator begin() { return msg_->oneof_begin(); }
2692     oneof_iterator end() { return msg_->oneof_end(); }
2693    private:
2694     MessageDef* msg_;
2695   };
2696 
2697   class ConstOneofAccessor {
2698    public:
2699     explicit ConstOneofAccessor(const MessageDef* msg) : msg_(msg) {}
2700     const_oneof_iterator begin() { return msg_->oneof_begin(); }
2701     const_oneof_iterator end() { return msg_->oneof_end(); }
2702    private:
2703     const MessageDef* msg_;
2704   };
2705 
2706   field_iterator field_begin();
2707   field_iterator field_end();
2708   const_field_iterator field_begin() const;
2709   const_field_iterator field_end() const;
2710 
2711   oneof_iterator oneof_begin();
2712   oneof_iterator oneof_end();
2713   const_oneof_iterator oneof_begin() const;
2714   const_oneof_iterator oneof_end() const;
2715 
2716   FieldAccessor fields() { return FieldAccessor(this); }
2717   ConstFieldAccessor fields() const { return ConstFieldAccessor(this); }
2718   OneofAccessor oneofs() { return OneofAccessor(this); }
2719   ConstOneofAccessor oneofs() const { return ConstOneofAccessor(this); }
2720 
2721  private:
2722   UPB_DISALLOW_POD_OPS(MessageDef, upb::MessageDef)
2723 #else
2724 struct upb_msgdef {
2725   upb_def base;
2726 
2727   size_t selector_count;
2728   uint32_t submsg_field_count;
2729 
2730   /* Tables for looking up fields by number and name. */
2731   upb_inttable itof;  /* int to field */
2732   upb_strtable ntof;  /* name to field/oneof */
2733 
2734   /* Is this a map-entry message? */
2735   bool map_entry;
2736 
2737   /* Whether this message has proto2 or proto3 semantics. */
2738   upb_syntax_t syntax;
2739 
2740   /* Type of well known type message. UPB_WELLKNOWN_UNSPECIFIED for
2741    * non-well-known message. */
2742   upb_wellknowntype_t well_known_type;
2743 
2744   /* TODO(haberman): proper extension ranges (there can be multiple). */
2745 #endif  /* __cplusplus */
2746 };
2747 
2748 UPB_BEGIN_EXTERN_C
2749 
2750 extern const struct upb_refcounted_vtbl upb_msgdef_vtbl;
2751 
2752 /* TODO: also support static initialization of the oneofs table. This will be
2753  * needed if we compile in descriptors that contain oneofs. */
2754 #define UPB_MSGDEF_INIT(name, selector_count, submsg_field_count, itof, ntof, \
2755                         map_entry, syntax, well_known_type, refs, ref2s)      \
2756   {                                                                           \
2757     UPB_DEF_INIT(name, UPB_DEF_MSG, &upb_fielddef_vtbl, refs, ref2s),         \
2758         selector_count, submsg_field_count, itof, ntof, map_entry, syntax,    \
2759         well_known_type                                                       \
2760   }
2761 
2762 /* Returns NULL if memory allocation failed. */
2763 upb_msgdef *upb_msgdef_new(const void *owner);
2764 
2765 /* Include upb_refcounted methods like upb_msgdef_ref(). */
2766 UPB_REFCOUNTED_CMETHODS(upb_msgdef, upb_msgdef_upcast2)
2767 
2768 bool upb_msgdef_freeze(upb_msgdef *m, upb_status *status);
2769 
2770 const char *upb_msgdef_fullname(const upb_msgdef *m);
2771 const char *upb_msgdef_name(const upb_msgdef *m);
2772 int upb_msgdef_numoneofs(const upb_msgdef *m);
2773 upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m);
2774 
2775 bool upb_msgdef_addfield(upb_msgdef *m, upb_fielddef *f, const void *ref_donor,
2776                          upb_status *s);
2777 bool upb_msgdef_addoneof(upb_msgdef *m, upb_oneofdef *o, const void *ref_donor,
2778                          upb_status *s);
2779 bool upb_msgdef_setfullname(upb_msgdef *m, const char *fullname, upb_status *s);
2780 void upb_msgdef_setmapentry(upb_msgdef *m, bool map_entry);
2781 bool upb_msgdef_mapentry(const upb_msgdef *m);
2782 upb_wellknowntype_t upb_msgdef_wellknowntype(const upb_msgdef *m);
2783 bool upb_msgdef_isnumberwrapper(const upb_msgdef *m);
2784 bool upb_msgdef_setsyntax(upb_msgdef *m, upb_syntax_t syntax);
2785 
2786 /* Field lookup in a couple of different variations:
2787  *   - itof = int to field
2788  *   - ntof = name to field
2789  *   - ntofz = name to field, null-terminated string. */
2790 const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i);
2791 const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name,
2792                                     size_t len);
2793 int upb_msgdef_numfields(const upb_msgdef *m);
2794 
2795 UPB_INLINE const upb_fielddef *upb_msgdef_ntofz(const upb_msgdef *m,
2796                                                 const char *name) {
2797   return upb_msgdef_ntof(m, name, strlen(name));
2798 }
2799 
2800 UPB_INLINE upb_fielddef *upb_msgdef_itof_mutable(upb_msgdef *m, uint32_t i) {
2801   return (upb_fielddef*)upb_msgdef_itof(m, i);
2802 }
2803 
2804 UPB_INLINE upb_fielddef *upb_msgdef_ntof_mutable(upb_msgdef *m,
2805                                                  const char *name, size_t len) {
2806   return (upb_fielddef *)upb_msgdef_ntof(m, name, len);
2807 }
2808 
2809 /* Oneof lookup:
2810  *   - ntoo = name to oneof
2811  *   - ntooz = name to oneof, null-terminated string. */
2812 const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name,
2813                                     size_t len);
2814 int upb_msgdef_numoneofs(const upb_msgdef *m);
2815 
2816 UPB_INLINE const upb_oneofdef *upb_msgdef_ntooz(const upb_msgdef *m,
2817                                                const char *name) {
2818   return upb_msgdef_ntoo(m, name, strlen(name));
2819 }
2820 
2821 UPB_INLINE upb_oneofdef *upb_msgdef_ntoo_mutable(upb_msgdef *m,
2822                                                  const char *name, size_t len) {
2823   return (upb_oneofdef *)upb_msgdef_ntoo(m, name, len);
2824 }
2825 
2826 /* Lookup of either field or oneof by name.  Returns whether either was found.
2827  * If the return is true, then the found def will be set, and the non-found
2828  * one set to NULL. */
2829 bool upb_msgdef_lookupname(const upb_msgdef *m, const char *name, size_t len,
2830                            const upb_fielddef **f, const upb_oneofdef **o);
2831 
2832 UPB_INLINE bool upb_msgdef_lookupnamez(const upb_msgdef *m, const char *name,
2833                                        const upb_fielddef **f,
2834                                        const upb_oneofdef **o) {
2835   return upb_msgdef_lookupname(m, name, strlen(name), f, o);
2836 }
2837 
2838 /* Iteration over fields and oneofs.  For example:
2839  *
2840  * upb_msg_field_iter i;
2841  * for(upb_msg_field_begin(&i, m);
2842  *     !upb_msg_field_done(&i);
2843  *     upb_msg_field_next(&i)) {
2844  *   upb_fielddef *f = upb_msg_iter_field(&i);
2845  *   // ...
2846  * }
2847  *
2848  * For C we don't have separate iterators for const and non-const.
2849  * It is the caller's responsibility to cast the upb_fielddef* to
2850  * const if the upb_msgdef* is const. */
2851 void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m);
2852 void upb_msg_field_next(upb_msg_field_iter *iter);
2853 bool upb_msg_field_done(const upb_msg_field_iter *iter);
2854 upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter);
2855 void upb_msg_field_iter_setdone(upb_msg_field_iter *iter);
2856 
2857 /* Similar to above, we also support iterating through the oneofs in a
2858  * msgdef. */
2859 void upb_msg_oneof_begin(upb_msg_oneof_iter *iter, const upb_msgdef *m);
2860 void upb_msg_oneof_next(upb_msg_oneof_iter *iter);
2861 bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter);
2862 upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter);
2863 void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter);
2864 
2865 UPB_END_EXTERN_C
2866 
2867 
2868 /* upb::EnumDef ***************************************************************/
2869 
2870 typedef upb_strtable_iter upb_enum_iter;
2871 
2872 #ifdef __cplusplus
2873 
2874 /* Class that represents an enum.  Its base class is upb::Def (convert with
2875  * upb::upcast()). */
2876 class upb::EnumDef {
2877  public:
2878   /* Returns NULL if memory allocation failed. */
2879   static reffed_ptr<EnumDef> New();
2880 
2881   /* upb::RefCounted methods like Ref()/Unref(). */
2882   UPB_REFCOUNTED_CPPMETHODS
2883 
2884   /* Functionality from upb::Def. */
2885   const char* full_name() const;
2886   const char* name() const;
2887   bool set_full_name(const char* fullname, Status* s);
2888   bool set_full_name(const std::string& fullname, Status* s);
2889 
2890   /* Call to freeze this EnumDef. */
2891   bool Freeze(Status* s);
2892 
2893   /* The value that is used as the default when no field default is specified.
2894    * If not set explicitly, the first value that was added will be used.
2895    * The default value must be a member of the enum.
2896    * Requires that value_count() > 0. */
2897   int32_t default_value() const;
2898 
2899   /* Sets the default value.  If this value is not valid, returns false and an
2900    * error message in status. */
2901   bool set_default_value(int32_t val, Status* status);
2902 
2903   /* Returns the number of values currently defined in the enum.  Note that
2904    * multiple names can refer to the same number, so this may be greater than
2905    * the total number of unique numbers. */
2906   int value_count() const;
2907 
2908   /* Adds a single name/number pair to the enum.  Fails if this name has
2909    * already been used by another value. */
2910   bool AddValue(const char* name, int32_t num, Status* status);
2911   bool AddValue(const std::string& name, int32_t num, Status* status);
2912 
2913   /* Lookups from name to integer, returning true if found. */
2914   bool FindValueByName(const char* name, int32_t* num) const;
2915 
2916   /* Finds the name corresponding to the given number, or NULL if none was
2917    * found.  If more than one name corresponds to this number, returns the
2918    * first one that was added. */
2919   const char* FindValueByNumber(int32_t num) const;
2920 
2921   /* Iteration over name/value pairs.  The order is undefined.
2922    * Adding an enum val invalidates any iterators.
2923    *
2924    * TODO: make compatible with range-for, with elements as pairs? */
2925   class Iterator {
2926    public:
2927     explicit Iterator(const EnumDef*);
2928 
2929     int32_t number();
2930     const char *name();
2931     bool Done();
2932     void Next();
2933 
2934    private:
2935     upb_enum_iter iter_;
2936   };
2937 
2938  private:
2939   UPB_DISALLOW_POD_OPS(EnumDef, upb::EnumDef)
2940 #else
2941 struct upb_enumdef {
2942   upb_def base;
2943 
2944   upb_strtable ntoi;
2945   upb_inttable iton;
2946   int32_t defaultval;
2947 #endif  /* __cplusplus */
2948 };
2949 
2950 UPB_BEGIN_EXTERN_C
2951 
2952 extern const struct upb_refcounted_vtbl upb_enumdef_vtbl;
2953 
2954 #define UPB_ENUMDEF_INIT(name, ntoi, iton, defaultval, refs, ref2s) \
2955   { UPB_DEF_INIT(name, UPB_DEF_ENUM, &upb_enumdef_vtbl, refs, ref2s), ntoi,    \
2956     iton, defaultval }
2957 
2958 /* Native C API. */
2959 upb_enumdef *upb_enumdef_new(const void *owner);
2960 
2961 /* Include upb_refcounted methods like upb_enumdef_ref(). */
2962 UPB_REFCOUNTED_CMETHODS(upb_enumdef, upb_enumdef_upcast2)
2963 
2964 bool upb_enumdef_freeze(upb_enumdef *e, upb_status *status);
2965 
2966 /* From upb_def. */
2967 const char *upb_enumdef_fullname(const upb_enumdef *e);
2968 const char *upb_enumdef_name(const upb_enumdef *e);
2969 bool upb_enumdef_setfullname(upb_enumdef *e, const char *fullname,
2970                              upb_status *s);
2971 
2972 int32_t upb_enumdef_default(const upb_enumdef *e);
2973 bool upb_enumdef_setdefault(upb_enumdef *e, int32_t val, upb_status *s);
2974 int upb_enumdef_numvals(const upb_enumdef *e);
2975 bool upb_enumdef_addval(upb_enumdef *e, const char *name, int32_t num,
2976                         upb_status *status);
2977 
2978 /* Enum lookups:
2979  * - ntoi:  look up a name with specified length.
2980  * - ntoiz: look up a name provided as a null-terminated string.
2981  * - iton:  look up an integer, returning the name as a null-terminated
2982  *          string. */
2983 bool upb_enumdef_ntoi(const upb_enumdef *e, const char *name, size_t len,
2984                       int32_t *num);
2985 UPB_INLINE bool upb_enumdef_ntoiz(const upb_enumdef *e,
2986                                   const char *name, int32_t *num) {
2987   return upb_enumdef_ntoi(e, name, strlen(name), num);
2988 }
2989 const char *upb_enumdef_iton(const upb_enumdef *e, int32_t num);
2990 
2991 /*  upb_enum_iter i;
2992  *  for(upb_enum_begin(&i, e); !upb_enum_done(&i); upb_enum_next(&i)) {
2993  *    // ...
2994  *  }
2995  */
2996 void upb_enum_begin(upb_enum_iter *iter, const upb_enumdef *e);
2997 void upb_enum_next(upb_enum_iter *iter);
2998 bool upb_enum_done(upb_enum_iter *iter);
2999 const char *upb_enum_iter_name(upb_enum_iter *iter);
3000 int32_t upb_enum_iter_number(upb_enum_iter *iter);
3001 
3002 UPB_END_EXTERN_C
3003 
3004 
3005 /* upb::OneofDef **************************************************************/
3006 
3007 typedef upb_inttable_iter upb_oneof_iter;
3008 
3009 #ifdef __cplusplus
3010 
3011 /* Class that represents a oneof. */
3012 class upb::OneofDef {
3013  public:
3014   /* Returns NULL if memory allocation failed. */
3015   static reffed_ptr<OneofDef> New();
3016 
3017   /* upb::RefCounted methods like Ref()/Unref(). */
3018   UPB_REFCOUNTED_CPPMETHODS
3019 
3020   /* Returns the MessageDef that owns this OneofDef. */
3021   const MessageDef* containing_type() const;
3022 
3023   /* Returns the name of this oneof. This is the name used to look up the oneof
3024    * by name once added to a message def. */
3025   const char* name() const;
3026   bool set_name(const char* name, Status* s);
3027   bool set_name(const std::string& name, Status* s);
3028 
3029   /* Returns the number of fields currently defined in the oneof. */
3030   int field_count() const;
3031 
3032   /* Adds a field to the oneof. The field must not have been added to any other
3033    * oneof or msgdef. If the oneof is not yet part of a msgdef, then when the
3034    * oneof is eventually added to a msgdef, all fields added to the oneof will
3035    * also be added to the msgdef at that time. If the oneof is already part of a
3036    * msgdef, the field must either be a part of that msgdef already, or must not
3037    * be a part of any msgdef; in the latter case, the field is added to the
3038    * msgdef as a part of this operation.
3039    *
3040    * The field may only have an OPTIONAL label, never REQUIRED or REPEATED.
3041    *
3042    * If |f| is already part of this MessageDef, this method performs no action
3043    * and returns true (success). Thus, this method is idempotent. */
3044   bool AddField(FieldDef* field, Status* s);
3045   bool AddField(const reffed_ptr<FieldDef>& field, Status* s);
3046 
3047   /* Looks up by name. */
3048   const FieldDef* FindFieldByName(const char* name, size_t len) const;
3049   FieldDef* FindFieldByName(const char* name, size_t len);
3050   const FieldDef* FindFieldByName(const char* name) const {
3051     return FindFieldByName(name, strlen(name));
3052   }
3053   FieldDef* FindFieldByName(const char* name) {
3054     return FindFieldByName(name, strlen(name));
3055   }
3056 
3057   template <class T>
3058   FieldDef* FindFieldByName(const T& str) {
3059     return FindFieldByName(str.c_str(), str.size());
3060   }
3061   template <class T>
3062   const FieldDef* FindFieldByName(const T& str) const {
3063     return FindFieldByName(str.c_str(), str.size());
3064   }
3065 
3066   /* Looks up by tag number. */
3067   const FieldDef* FindFieldByNumber(uint32_t num) const;
3068 
3069   /* Iteration over fields.  The order is undefined. */
3070   class iterator : public std::iterator<std::forward_iterator_tag, FieldDef*> {
3071    public:
3072     explicit iterator(OneofDef* md);
3073     static iterator end(OneofDef* md);
3074 
3075     void operator++();
3076     FieldDef* operator*() const;
3077     bool operator!=(const iterator& other) const;
3078     bool operator==(const iterator& other) const;
3079 
3080    private:
3081     upb_oneof_iter iter_;
3082   };
3083 
3084   class const_iterator
3085       : public std::iterator<std::forward_iterator_tag, const FieldDef*> {
3086    public:
3087     explicit const_iterator(const OneofDef* md);
3088     static const_iterator end(const OneofDef* md);
3089 
3090     void operator++();
3091     const FieldDef* operator*() const;
3092     bool operator!=(const const_iterator& other) const;
3093     bool operator==(const const_iterator& other) const;
3094 
3095    private:
3096     upb_oneof_iter iter_;
3097   };
3098 
3099   iterator begin();
3100   iterator end();
3101   const_iterator begin() const;
3102   const_iterator end() const;
3103 
3104  private:
3105   UPB_DISALLOW_POD_OPS(OneofDef, upb::OneofDef)
3106 #else
3107 struct upb_oneofdef {
3108   upb_refcounted base;
3109 
3110   uint32_t index;  /* Index within oneofs. */
3111   const char *name;
3112   upb_strtable ntof;
3113   upb_inttable itof;
3114   const upb_msgdef *parent;
3115 #endif  /* __cplusplus */
3116 };
3117 
3118 UPB_BEGIN_EXTERN_C
3119 
3120 extern const struct upb_refcounted_vtbl upb_oneofdef_vtbl;
3121 
3122 #define UPB_ONEOFDEF_INIT(name, ntof, itof, refs, ref2s) \
3123   { UPB_REFCOUNT_INIT(&upb_oneofdef_vtbl, refs, ref2s), 0, name, ntof, itof }
3124 
3125 /* Native C API. */
3126 upb_oneofdef *upb_oneofdef_new(const void *owner);
3127 
3128 /* Include upb_refcounted methods like upb_oneofdef_ref(). */
3129 UPB_REFCOUNTED_CMETHODS(upb_oneofdef, upb_oneofdef_upcast)
3130 
3131 const char *upb_oneofdef_name(const upb_oneofdef *o);
3132 const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o);
3133 int upb_oneofdef_numfields(const upb_oneofdef *o);
3134 uint32_t upb_oneofdef_index(const upb_oneofdef *o);
3135 
3136 bool upb_oneofdef_setname(upb_oneofdef *o, const char *name, upb_status *s);
3137 bool upb_oneofdef_addfield(upb_oneofdef *o, upb_fielddef *f,
3138                            const void *ref_donor,
3139                            upb_status *s);
3140 
3141 /* Oneof lookups:
3142  * - ntof:  look up a field by name.
3143  * - ntofz: look up a field by name (as a null-terminated string).
3144  * - itof:  look up a field by number. */
3145 const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o,
3146                                       const char *name, size_t length);
3147 UPB_INLINE const upb_fielddef *upb_oneofdef_ntofz(const upb_oneofdef *o,
3148                                                   const char *name) {
3149   return upb_oneofdef_ntof(o, name, strlen(name));
3150 }
3151 const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num);
3152 
3153 /*  upb_oneof_iter i;
3154  *  for(upb_oneof_begin(&i, e); !upb_oneof_done(&i); upb_oneof_next(&i)) {
3155  *    // ...
3156  *  }
3157  */
3158 void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o);
3159 void upb_oneof_next(upb_oneof_iter *iter);
3160 bool upb_oneof_done(upb_oneof_iter *iter);
3161 upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter);
3162 void upb_oneof_iter_setdone(upb_oneof_iter *iter);
3163 
3164 UPB_END_EXTERN_C
3165 
3166 
3167 /* upb::FileDef ***************************************************************/
3168 
3169 #ifdef __cplusplus
3170 
3171 /* Class that represents a .proto file with some things defined in it.
3172  *
3173  * Many users won't care about FileDefs, but they are necessary if you want to
3174  * read the values of file-level options. */
3175 class upb::FileDef {
3176  public:
3177   /* Returns NULL if memory allocation failed. */
3178   static reffed_ptr<FileDef> New();
3179 
3180   /* upb::RefCounted methods like Ref()/Unref(). */
3181   UPB_REFCOUNTED_CPPMETHODS
3182 
3183   /* Get/set name of the file (eg. "foo/bar.proto"). */
3184   const char* name() const;
3185   bool set_name(const char* name, Status* s);
3186   bool set_name(const std::string& name, Status* s);
3187 
3188   /* Package name for definitions inside the file (eg. "foo.bar"). */
3189   const char* package() const;
3190   bool set_package(const char* package, Status* s);
3191 
3192   /* Sets the php class prefix which is prepended to all php generated classes
3193    * from this .proto. Default is empty. */
3194   const char* phpprefix() const;
3195   bool set_phpprefix(const char* phpprefix, Status* s);
3196 
3197   /* Use this option to change the namespace of php generated classes. Default
3198    * is empty. When this option is empty, the package name will be used for
3199    * determining the namespace. */
3200   const char* phpnamespace() const;
3201   bool set_phpnamespace(const char* phpnamespace, Status* s);
3202 
3203   /* Syntax for the file.  Defaults to proto2. */
3204   upb_syntax_t syntax() const;
3205   void set_syntax(upb_syntax_t syntax);
3206 
3207   /* Get the list of defs from the file.  These are returned in the order that
3208    * they were added to the FileDef. */
3209   int def_count() const;
3210   const Def* def(int index) const;
3211   Def* def(int index);
3212 
3213   /* Get the list of dependencies from the file.  These are returned in the
3214    * order that they were added to the FileDef. */
3215   int dependency_count() const;
3216   const FileDef* dependency(int index) const;
3217 
3218   /* Adds defs to this file.  The def must not already belong to another
3219    * file.
3220    *
3221    * Note: this does *not* ensure that this def's name is unique in this file!
3222    * Use a SymbolTable if you want to check this property.  Especially since
3223    * properly checking uniqueness would require a check across *all* files
3224    * (including dependencies). */
3225   bool AddDef(Def* def, Status* s);
3226   bool AddMessage(MessageDef* m, Status* s);
3227   bool AddEnum(EnumDef* e, Status* s);
3228   bool AddExtension(FieldDef* f, Status* s);
3229 
3230   /* Adds a dependency of this file. */
3231   bool AddDependency(const FileDef* file);
3232 
3233   /* Freezes this FileDef and all messages/enums under it.  All subdefs must be
3234    * resolved and all messages/enums must validate.  Returns true if this
3235    * succeeded.
3236    *
3237    * TODO(haberman): should we care whether the file's dependencies are frozen
3238    * already? */
3239   bool Freeze(Status* s);
3240 
3241  private:
3242   UPB_DISALLOW_POD_OPS(FileDef, upb::FileDef)
3243 #else
3244 struct upb_filedef {
3245   upb_refcounted base;
3246 
3247   const char *name;
3248   const char *package;
3249   const char *phpprefix;
3250   const char *phpnamespace;
3251   upb_syntax_t syntax;
3252 
3253   upb_inttable defs;
3254   upb_inttable deps;
3255 #endif
3256 };
3257 
3258 UPB_BEGIN_EXTERN_C
3259 
3260 extern const struct upb_refcounted_vtbl upb_filedef_vtbl;
3261 
3262 upb_filedef *upb_filedef_new(const void *owner);
3263 
3264 /* Include upb_refcounted methods like upb_msgdef_ref(). */
3265 UPB_REFCOUNTED_CMETHODS(upb_filedef, upb_filedef_upcast)
3266 
3267 const char *upb_filedef_name(const upb_filedef *f);
3268 const char *upb_filedef_package(const upb_filedef *f);
3269 const char *upb_filedef_phpprefix(const upb_filedef *f);
3270 const char *upb_filedef_phpnamespace(const upb_filedef *f);
3271 upb_syntax_t upb_filedef_syntax(const upb_filedef *f);
3272 size_t upb_filedef_defcount(const upb_filedef *f);
3273 size_t upb_filedef_depcount(const upb_filedef *f);
3274 const upb_def *upb_filedef_def(const upb_filedef *f, size_t i);
3275 const upb_filedef *upb_filedef_dep(const upb_filedef *f, size_t i);
3276 
3277 bool upb_filedef_freeze(upb_filedef *f, upb_status *s);
3278 bool upb_filedef_setname(upb_filedef *f, const char *name, upb_status *s);
3279 bool upb_filedef_setpackage(upb_filedef *f, const char *package, upb_status *s);
3280 bool upb_filedef_setphpprefix(upb_filedef *f, const char *phpprefix,
3281                               upb_status *s);
3282 bool upb_filedef_setphpnamespace(upb_filedef *f, const char *phpnamespace,
3283                                  upb_status *s);
3284 bool upb_filedef_setsyntax(upb_filedef *f, upb_syntax_t syntax, upb_status *s);
3285 
3286 bool upb_filedef_adddef(upb_filedef *f, upb_def *def, const void *ref_donor,
3287                         upb_status *s);
3288 bool upb_filedef_adddep(upb_filedef *f, const upb_filedef *dep);
3289 
3290 UPB_INLINE bool upb_filedef_addmsg(upb_filedef *f, upb_msgdef *m,
3291                                    const void *ref_donor, upb_status *s) {
3292   return upb_filedef_adddef(f, upb_msgdef_upcast_mutable(m), ref_donor, s);
3293 }
3294 
3295 UPB_INLINE bool upb_filedef_addenum(upb_filedef *f, upb_enumdef *e,
3296                                     const void *ref_donor, upb_status *s) {
3297   return upb_filedef_adddef(f, upb_enumdef_upcast_mutable(e), ref_donor, s);
3298 }
3299 
3300 UPB_INLINE bool upb_filedef_addext(upb_filedef *file, upb_fielddef *f,
3301                                    const void *ref_donor, upb_status *s) {
3302   return upb_filedef_adddef(file, upb_fielddef_upcast_mutable(f), ref_donor, s);
3303 }
3304 UPB_INLINE upb_def *upb_filedef_mutabledef(upb_filedef *f, int i) {
3305   return (upb_def*)upb_filedef_def(f, i);
3306 }
3307 
3308 UPB_END_EXTERN_C
3309 
3310 typedef struct {
3311  UPB_PRIVATE_FOR_CPP
3312   upb_strtable_iter iter;
3313   upb_deftype_t type;
3314 } upb_symtab_iter;
3315 
3316 #ifdef __cplusplus
3317 
3318 /* Non-const methods in upb::SymbolTable are NOT thread-safe. */
3319 class upb::SymbolTable {
3320  public:
3321   /* Returns a new symbol table with a single ref owned by "owner."
3322    * Returns NULL if memory allocation failed. */
3323   static SymbolTable* New();
3324   static void Free(upb::SymbolTable* table);
3325 
3326   /* For all lookup functions, the returned pointer is not owned by the
3327    * caller; it may be invalidated by any non-const call or unref of the
3328    * SymbolTable!  To protect against this, take a ref if desired. */
3329 
3330   /* Freezes the symbol table: prevents further modification of it.
3331    * After the Freeze() operation is successful, the SymbolTable must only be
3332    * accessed via a const pointer.
3333    *
3334    * Unlike with upb::MessageDef/upb::EnumDef/etc, freezing a SymbolTable is not
3335    * a necessary step in using a SymbolTable.  If you have no need for it to be
3336    * immutable, there is no need to freeze it ever.  However sometimes it is
3337    * useful, and SymbolTables that are statically compiled into the binary are
3338    * always frozen by nature. */
3339   void Freeze();
3340 
3341   /* Resolves the given symbol using the rules described in descriptor.proto,
3342    * namely:
3343    *
3344    *    If the name starts with a '.', it is fully-qualified.  Otherwise,
3345    *    C++-like scoping rules are used to find the type (i.e. first the nested
3346    *    types within this message are searched, then within the parent, on up
3347    *    to the root namespace).
3348    *
3349    * If not found, returns NULL. */
3350   const Def* Resolve(const char* base, const char* sym) const;
3351 
3352   /* Finds an entry in the symbol table with this exact name.  If not found,
3353    * returns NULL. */
3354   const Def* Lookup(const char *sym) const;
3355   const MessageDef* LookupMessage(const char *sym) const;
3356   const EnumDef* LookupEnum(const char *sym) const;
3357 
3358   /* TODO: introduce a C++ iterator, but make it nice and templated so that if
3359    * you ask for an iterator of MessageDef the iterated elements are strongly
3360    * typed as MessageDef*. */
3361 
3362   /* Adds the given mutable defs to the symtab, resolving all symbols (including
3363    * enum default values) and finalizing the defs.  Only one def per name may be
3364    * in the list, and the defs may not duplicate any name already in the symtab.
3365    * All defs must have a name -- anonymous defs are not allowed.  Anonymous
3366    * defs can still be frozen by calling upb_def_freeze() directly.
3367    *
3368    * The entire operation either succeeds or fails.  If the operation fails,
3369    * the symtab is unchanged, false is returned, and status indicates the
3370    * error.  The caller passes a ref on all defs to the symtab (even if the
3371    * operation fails).
3372    *
3373    * TODO(haberman): currently failure will leave the symtab unchanged, but may
3374    * leave the defs themselves partially resolved.  Does this matter?  If so we
3375    * could do a prepass that ensures that all symbols are resolvable and bail
3376    * if not, so we don't mutate anything until we know the operation will
3377    * succeed. */
3378   bool Add(Def*const* defs, size_t n, void* ref_donor, Status* status);
3379 
3380   bool Add(const std::vector<Def*>& defs, void *owner, Status* status) {
3381     return Add((Def*const*)&defs[0], defs.size(), owner, status);
3382   }
3383 
3384   /* Resolves all subdefs for messages in this file and attempts to freeze the
3385    * file.  If this succeeds, adds all the symbols to this SymbolTable
3386    * (replacing any existing ones with the same names). */
3387   bool AddFile(FileDef* file, Status* s);
3388 
3389  private:
3390   UPB_DISALLOW_POD_OPS(SymbolTable, upb::SymbolTable)
3391 #else
3392 struct upb_symtab {
3393   upb_refcounted base;
3394 
3395   upb_strtable symtab;
3396 #endif  /* __cplusplus */
3397 };
3398 
3399 UPB_BEGIN_EXTERN_C
3400 
3401 /* Native C API. */
3402 
3403 upb_symtab *upb_symtab_new();
3404 void upb_symtab_free(upb_symtab* s);
3405 const upb_def *upb_symtab_resolve(const upb_symtab *s, const char *base,
3406                                   const char *sym);
3407 const upb_def *upb_symtab_lookup(const upb_symtab *s, const char *sym);
3408 const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym);
3409 const upb_msgdef *upb_symtab_lookupmsg2(
3410     const upb_symtab *s, const char *sym, size_t len);
3411 const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym);
3412 bool upb_symtab_add(upb_symtab *s, upb_def *const*defs, size_t n,
3413                     void *ref_donor, upb_status *status);
3414 bool upb_symtab_addfile(upb_symtab *s, upb_filedef *file, upb_status* status);
3415 
3416 /* upb_symtab_iter i;
3417  * for(upb_symtab_begin(&i, s, type); !upb_symtab_done(&i);
3418  *     upb_symtab_next(&i)) {
3419  *   const upb_def *def = upb_symtab_iter_def(&i);
3420  *    // ...
3421  * }
3422  *
3423  * For C we don't have separate iterators for const and non-const.
3424  * It is the caller's responsibility to cast the upb_fielddef* to
3425  * const if the upb_msgdef* is const. */
3426 void upb_symtab_begin(upb_symtab_iter *iter, const upb_symtab *s,
3427                       upb_deftype_t type);
3428 void upb_symtab_next(upb_symtab_iter *iter);
3429 bool upb_symtab_done(const upb_symtab_iter *iter);
3430 const upb_def *upb_symtab_iter_def(const upb_symtab_iter *iter);
3431 
3432 UPB_END_EXTERN_C
3433 
3434 #ifdef __cplusplus
3435 /* C++ inline wrappers. */
3436 namespace upb {
3437 inline SymbolTable* SymbolTable::New() {
3438   return upb_symtab_new();
3439 }
3440 inline void SymbolTable::Free(SymbolTable* s) {
3441   upb_symtab_free(s);
3442 }
3443 inline const Def *SymbolTable::Resolve(const char *base,
3444                                        const char *sym) const {
3445   return upb_symtab_resolve(this, base, sym);
3446 }
3447 inline const Def* SymbolTable::Lookup(const char *sym) const {
3448   return upb_symtab_lookup(this, sym);
3449 }
3450 inline const MessageDef *SymbolTable::LookupMessage(const char *sym) const {
3451   return upb_symtab_lookupmsg(this, sym);
3452 }
3453 inline bool SymbolTable::Add(
3454     Def*const* defs, size_t n, void* ref_donor, Status* status) {
3455   return upb_symtab_add(this, (upb_def*const*)defs, n, ref_donor, status);
3456 }
3457 inline bool SymbolTable::AddFile(FileDef* file, Status* s) {
3458   return upb_symtab_addfile(this, file, s);
3459 }
3460 }  /* namespace upb */
3461 #endif
3462 
3463 #ifdef __cplusplus
3464 
3465 UPB_INLINE const char* upb_safecstr(const std::string& str) {
3466   UPB_ASSERT(str.size() == std::strlen(str.c_str()));
3467   return str.c_str();
3468 }
3469 
3470 /* Inline C++ wrappers. */
3471 namespace upb {
3472 
3473 inline Def::Type Def::def_type() const { return upb_def_type(this); }
3474 inline const char* Def::full_name() const { return upb_def_fullname(this); }
3475 inline const char* Def::name() const { return upb_def_name(this); }
3476 inline bool Def::set_full_name(const char* fullname, Status* s) {
3477   return upb_def_setfullname(this, fullname, s);
3478 }
3479 inline bool Def::set_full_name(const std::string& fullname, Status* s) {
3480   return upb_def_setfullname(this, upb_safecstr(fullname), s);
3481 }
3482 inline bool Def::Freeze(Def* const* defs, size_t n, Status* status) {
3483   return upb_def_freeze(defs, n, status);
3484 }
3485 inline bool Def::Freeze(const std::vector<Def*>& defs, Status* status) {
3486   return upb_def_freeze((Def* const*)&defs[0], defs.size(), status);
3487 }
3488 
3489 inline bool FieldDef::CheckType(int32_t val) {
3490   return upb_fielddef_checktype(val);
3491 }
3492 inline bool FieldDef::CheckLabel(int32_t val) {
3493   return upb_fielddef_checklabel(val);
3494 }
3495 inline bool FieldDef::CheckDescriptorType(int32_t val) {
3496   return upb_fielddef_checkdescriptortype(val);
3497 }
3498 inline bool FieldDef::CheckIntegerFormat(int32_t val) {
3499   return upb_fielddef_checkintfmt(val);
3500 }
3501 inline FieldDef::Type FieldDef::ConvertType(int32_t val) {
3502   UPB_ASSERT(CheckType(val));
3503   return static_cast<FieldDef::Type>(val);
3504 }
3505 inline FieldDef::Label FieldDef::ConvertLabel(int32_t val) {
3506   UPB_ASSERT(CheckLabel(val));
3507   return static_cast<FieldDef::Label>(val);
3508 }
3509 inline FieldDef::DescriptorType FieldDef::ConvertDescriptorType(int32_t val) {
3510   UPB_ASSERT(CheckDescriptorType(val));
3511   return static_cast<FieldDef::DescriptorType>(val);
3512 }
3513 inline FieldDef::IntegerFormat FieldDef::ConvertIntegerFormat(int32_t val) {
3514   UPB_ASSERT(CheckIntegerFormat(val));
3515   return static_cast<FieldDef::IntegerFormat>(val);
3516 }
3517 
3518 inline reffed_ptr<FieldDef> FieldDef::New() {
3519   upb_fielddef *f = upb_fielddef_new(&f);
3520   return reffed_ptr<FieldDef>(f, &f);
3521 }
3522 inline const char* FieldDef::full_name() const {
3523   return upb_fielddef_fullname(this);
3524 }
3525 inline bool FieldDef::set_full_name(const char* fullname, Status* s) {
3526   return upb_fielddef_setfullname(this, fullname, s);
3527 }
3528 inline bool FieldDef::set_full_name(const std::string& fullname, Status* s) {
3529   return upb_fielddef_setfullname(this, upb_safecstr(fullname), s);
3530 }
3531 inline bool FieldDef::type_is_set() const {
3532   return upb_fielddef_typeisset(this);
3533 }
3534 inline FieldDef::Type FieldDef::type() const { return upb_fielddef_type(this); }
3535 inline FieldDef::DescriptorType FieldDef::descriptor_type() const {
3536   return upb_fielddef_descriptortype(this);
3537 }
3538 inline FieldDef::Label FieldDef::label() const {
3539   return upb_fielddef_label(this);
3540 }
3541 inline uint32_t FieldDef::number() const { return upb_fielddef_number(this); }
3542 inline const char* FieldDef::name() const { return upb_fielddef_name(this); }
3543 inline bool FieldDef::is_extension() const {
3544   return upb_fielddef_isextension(this);
3545 }
3546 inline size_t FieldDef::GetJsonName(char* buf, size_t len) const {
3547   return upb_fielddef_getjsonname(this, buf, len);
3548 }
3549 inline bool FieldDef::lazy() const {
3550   return upb_fielddef_lazy(this);
3551 }
3552 inline void FieldDef::set_lazy(bool lazy) {
3553   upb_fielddef_setlazy(this, lazy);
3554 }
3555 inline bool FieldDef::packed() const {
3556   return upb_fielddef_packed(this);
3557 }
3558 inline uint32_t FieldDef::index() const {
3559   return upb_fielddef_index(this);
3560 }
3561 inline void FieldDef::set_packed(bool packed) {
3562   upb_fielddef_setpacked(this, packed);
3563 }
3564 inline const MessageDef* FieldDef::containing_type() const {
3565   return upb_fielddef_containingtype(this);
3566 }
3567 inline const OneofDef* FieldDef::containing_oneof() const {
3568   return upb_fielddef_containingoneof(this);
3569 }
3570 inline const char* FieldDef::containing_type_name() {
3571   return upb_fielddef_containingtypename(this);
3572 }
3573 inline bool FieldDef::set_number(uint32_t number, Status* s) {
3574   return upb_fielddef_setnumber(this, number, s);
3575 }
3576 inline bool FieldDef::set_name(const char *name, Status* s) {
3577   return upb_fielddef_setname(this, name, s);
3578 }
3579 inline bool FieldDef::set_name(const std::string& name, Status* s) {
3580   return upb_fielddef_setname(this, upb_safecstr(name), s);
3581 }
3582 inline bool FieldDef::set_json_name(const char *name, Status* s) {
3583   return upb_fielddef_setjsonname(this, name, s);
3584 }
3585 inline bool FieldDef::set_json_name(const std::string& name, Status* s) {
3586   return upb_fielddef_setjsonname(this, upb_safecstr(name), s);
3587 }
3588 inline void FieldDef::clear_json_name() {
3589   upb_fielddef_clearjsonname(this);
3590 }
3591 inline bool FieldDef::set_containing_type_name(const char *name, Status* s) {
3592   return upb_fielddef_setcontainingtypename(this, name, s);
3593 }
3594 inline bool FieldDef::set_containing_type_name(const std::string &name,
3595                                                Status *s) {
3596   return upb_fielddef_setcontainingtypename(this, upb_safecstr(name), s);
3597 }
3598 inline void FieldDef::set_type(upb_fieldtype_t type) {
3599   upb_fielddef_settype(this, type);
3600 }
3601 inline void FieldDef::set_is_extension(bool is_extension) {
3602   upb_fielddef_setisextension(this, is_extension);
3603 }
3604 inline void FieldDef::set_descriptor_type(FieldDef::DescriptorType type) {
3605   upb_fielddef_setdescriptortype(this, type);
3606 }
3607 inline void FieldDef::set_label(upb_label_t label) {
3608   upb_fielddef_setlabel(this, label);
3609 }
3610 inline bool FieldDef::IsSubMessage() const {
3611   return upb_fielddef_issubmsg(this);
3612 }
3613 inline bool FieldDef::IsString() const { return upb_fielddef_isstring(this); }
3614 inline bool FieldDef::IsSequence() const { return upb_fielddef_isseq(this); }
3615 inline bool FieldDef::IsMap() const { return upb_fielddef_ismap(this); }
3616 inline int64_t FieldDef::default_int64() const {
3617   return upb_fielddef_defaultint64(this);
3618 }
3619 inline int32_t FieldDef::default_int32() const {
3620   return upb_fielddef_defaultint32(this);
3621 }
3622 inline uint64_t FieldDef::default_uint64() const {
3623   return upb_fielddef_defaultuint64(this);
3624 }
3625 inline uint32_t FieldDef::default_uint32() const {
3626   return upb_fielddef_defaultuint32(this);
3627 }
3628 inline bool FieldDef::default_bool() const {
3629   return upb_fielddef_defaultbool(this);
3630 }
3631 inline float FieldDef::default_float() const {
3632   return upb_fielddef_defaultfloat(this);
3633 }
3634 inline double FieldDef::default_double() const {
3635   return upb_fielddef_defaultdouble(this);
3636 }
3637 inline const char* FieldDef::default_string(size_t* len) const {
3638   return upb_fielddef_defaultstr(this, len);
3639 }
3640 inline void FieldDef::set_default_int64(int64_t value) {
3641   upb_fielddef_setdefaultint64(this, value);
3642 }
3643 inline void FieldDef::set_default_int32(int32_t value) {
3644   upb_fielddef_setdefaultint32(this, value);
3645 }
3646 inline void FieldDef::set_default_uint64(uint64_t value) {
3647   upb_fielddef_setdefaultuint64(this, value);
3648 }
3649 inline void FieldDef::set_default_uint32(uint32_t value) {
3650   upb_fielddef_setdefaultuint32(this, value);
3651 }
3652 inline void FieldDef::set_default_bool(bool value) {
3653   upb_fielddef_setdefaultbool(this, value);
3654 }
3655 inline void FieldDef::set_default_float(float value) {
3656   upb_fielddef_setdefaultfloat(this, value);
3657 }
3658 inline void FieldDef::set_default_double(double value) {
3659   upb_fielddef_setdefaultdouble(this, value);
3660 }
3661 inline bool FieldDef::set_default_string(const void *str, size_t len,
3662                                          Status *s) {
3663   return upb_fielddef_setdefaultstr(this, str, len, s);
3664 }
3665 inline bool FieldDef::set_default_string(const std::string& str, Status* s) {
3666   return upb_fielddef_setdefaultstr(this, str.c_str(), str.size(), s);
3667 }
3668 inline void FieldDef::set_default_cstr(const char* str, Status* s) {
3669   return upb_fielddef_setdefaultcstr(this, str, s);
3670 }
3671 inline bool FieldDef::HasSubDef() const { return upb_fielddef_hassubdef(this); }
3672 inline const Def* FieldDef::subdef() const { return upb_fielddef_subdef(this); }
3673 inline const MessageDef *FieldDef::message_subdef() const {
3674   return upb_fielddef_msgsubdef(this);
3675 }
3676 inline const EnumDef *FieldDef::enum_subdef() const {
3677   return upb_fielddef_enumsubdef(this);
3678 }
3679 inline const char* FieldDef::subdef_name() const {
3680   return upb_fielddef_subdefname(this);
3681 }
3682 inline bool FieldDef::set_subdef(const Def* subdef, Status* s) {
3683   return upb_fielddef_setsubdef(this, subdef, s);
3684 }
3685 inline bool FieldDef::set_enum_subdef(const EnumDef* subdef, Status* s) {
3686   return upb_fielddef_setenumsubdef(this, subdef, s);
3687 }
3688 inline bool FieldDef::set_message_subdef(const MessageDef* subdef, Status* s) {
3689   return upb_fielddef_setmsgsubdef(this, subdef, s);
3690 }
3691 inline bool FieldDef::set_subdef_name(const char* name, Status* s) {
3692   return upb_fielddef_setsubdefname(this, name, s);
3693 }
3694 inline bool FieldDef::set_subdef_name(const std::string& name, Status* s) {
3695   return upb_fielddef_setsubdefname(this, upb_safecstr(name), s);
3696 }
3697 
3698 inline reffed_ptr<MessageDef> MessageDef::New() {
3699   upb_msgdef *m = upb_msgdef_new(&m);
3700   return reffed_ptr<MessageDef>(m, &m);
3701 }
3702 inline const char *MessageDef::full_name() const {
3703   return upb_msgdef_fullname(this);
3704 }
3705 inline const char *MessageDef::name() const {
3706   return upb_msgdef_name(this);
3707 }
3708 inline upb_syntax_t MessageDef::syntax() const {
3709   return upb_msgdef_syntax(this);
3710 }
3711 inline bool MessageDef::set_full_name(const char* fullname, Status* s) {
3712   return upb_msgdef_setfullname(this, fullname, s);
3713 }
3714 inline bool MessageDef::set_full_name(const std::string& fullname, Status* s) {
3715   return upb_msgdef_setfullname(this, upb_safecstr(fullname), s);
3716 }
3717 inline bool MessageDef::set_syntax(upb_syntax_t syntax) {
3718   return upb_msgdef_setsyntax(this, syntax);
3719 }
3720 inline bool MessageDef::Freeze(Status* status) {
3721   return upb_msgdef_freeze(this, status);
3722 }
3723 inline int MessageDef::field_count() const {
3724   return upb_msgdef_numfields(this);
3725 }
3726 inline int MessageDef::oneof_count() const {
3727   return upb_msgdef_numoneofs(this);
3728 }
3729 inline bool MessageDef::AddField(upb_fielddef* f, Status* s) {
3730   return upb_msgdef_addfield(this, f, NULL, s);
3731 }
3732 inline bool MessageDef::AddField(const reffed_ptr<FieldDef>& f, Status* s) {
3733   return upb_msgdef_addfield(this, f.get(), NULL, s);
3734 }
3735 inline bool MessageDef::AddOneof(upb_oneofdef* o, Status* s) {
3736   return upb_msgdef_addoneof(this, o, NULL, s);
3737 }
3738 inline bool MessageDef::AddOneof(const reffed_ptr<OneofDef>& o, Status* s) {
3739   return upb_msgdef_addoneof(this, o.get(), NULL, s);
3740 }
3741 inline FieldDef* MessageDef::FindFieldByNumber(uint32_t number) {
3742   return upb_msgdef_itof_mutable(this, number);
3743 }
3744 inline FieldDef* MessageDef::FindFieldByName(const char* name, size_t len) {
3745   return upb_msgdef_ntof_mutable(this, name, len);
3746 }
3747 inline const FieldDef* MessageDef::FindFieldByNumber(uint32_t number) const {
3748   return upb_msgdef_itof(this, number);
3749 }
3750 inline const FieldDef *MessageDef::FindFieldByName(const char *name,
3751                                                    size_t len) const {
3752   return upb_msgdef_ntof(this, name, len);
3753 }
3754 inline OneofDef* MessageDef::FindOneofByName(const char* name, size_t len) {
3755   return upb_msgdef_ntoo_mutable(this, name, len);
3756 }
3757 inline const OneofDef* MessageDef::FindOneofByName(const char* name,
3758                                                    size_t len) const {
3759   return upb_msgdef_ntoo(this, name, len);
3760 }
3761 inline void MessageDef::setmapentry(bool map_entry) {
3762   upb_msgdef_setmapentry(this, map_entry);
3763 }
3764 inline bool MessageDef::mapentry() const {
3765   return upb_msgdef_mapentry(this);
3766 }
3767 inline upb_wellknowntype_t MessageDef::wellknowntype() const {
3768   return upb_msgdef_wellknowntype(this);
3769 }
3770 inline bool MessageDef::isnumberwrapper() const {
3771   return upb_msgdef_isnumberwrapper(this);
3772 }
3773 inline MessageDef::field_iterator MessageDef::field_begin() {
3774   return field_iterator(this);
3775 }
3776 inline MessageDef::field_iterator MessageDef::field_end() {
3777   return field_iterator::end(this);
3778 }
3779 inline MessageDef::const_field_iterator MessageDef::field_begin() const {
3780   return const_field_iterator(this);
3781 }
3782 inline MessageDef::const_field_iterator MessageDef::field_end() const {
3783   return const_field_iterator::end(this);
3784 }
3785 
3786 inline MessageDef::oneof_iterator MessageDef::oneof_begin() {
3787   return oneof_iterator(this);
3788 }
3789 inline MessageDef::oneof_iterator MessageDef::oneof_end() {
3790   return oneof_iterator::end(this);
3791 }
3792 inline MessageDef::const_oneof_iterator MessageDef::oneof_begin() const {
3793   return const_oneof_iterator(this);
3794 }
3795 inline MessageDef::const_oneof_iterator MessageDef::oneof_end() const {
3796   return const_oneof_iterator::end(this);
3797 }
3798 
3799 inline MessageDef::field_iterator::field_iterator(MessageDef* md) {
3800   upb_msg_field_begin(&iter_, md);
3801 }
3802 inline MessageDef::field_iterator MessageDef::field_iterator::end(
3803     MessageDef* md) {
3804   MessageDef::field_iterator iter(md);
3805   upb_msg_field_iter_setdone(&iter.iter_);
3806   return iter;
3807 }
3808 inline FieldDef* MessageDef::field_iterator::operator*() const {
3809   return upb_msg_iter_field(&iter_);
3810 }
3811 inline void MessageDef::field_iterator::operator++() {
3812   return upb_msg_field_next(&iter_);
3813 }
3814 inline bool MessageDef::field_iterator::operator==(
3815     const field_iterator &other) const {
3816   return upb_inttable_iter_isequal(&iter_, &other.iter_);
3817 }
3818 inline bool MessageDef::field_iterator::operator!=(
3819     const field_iterator &other) const {
3820   return !(*this == other);
3821 }
3822 
3823 inline MessageDef::const_field_iterator::const_field_iterator(
3824     const MessageDef* md) {
3825   upb_msg_field_begin(&iter_, md);
3826 }
3827 inline MessageDef::const_field_iterator MessageDef::const_field_iterator::end(
3828     const MessageDef *md) {
3829   MessageDef::const_field_iterator iter(md);
3830   upb_msg_field_iter_setdone(&iter.iter_);
3831   return iter;
3832 }
3833 inline const FieldDef* MessageDef::const_field_iterator::operator*() const {
3834   return upb_msg_iter_field(&iter_);
3835 }
3836 inline void MessageDef::const_field_iterator::operator++() {
3837   return upb_msg_field_next(&iter_);
3838 }
3839 inline bool MessageDef::const_field_iterator::operator==(
3840     const const_field_iterator &other) const {
3841   return upb_inttable_iter_isequal(&iter_, &other.iter_);
3842 }
3843 inline bool MessageDef::const_field_iterator::operator!=(
3844     const const_field_iterator &other) const {
3845   return !(*this == other);
3846 }
3847 
3848 inline MessageDef::oneof_iterator::oneof_iterator(MessageDef* md) {
3849   upb_msg_oneof_begin(&iter_, md);
3850 }
3851 inline MessageDef::oneof_iterator MessageDef::oneof_iterator::end(
3852     MessageDef* md) {
3853   MessageDef::oneof_iterator iter(md);
3854   upb_msg_oneof_iter_setdone(&iter.iter_);
3855   return iter;
3856 }
3857 inline OneofDef* MessageDef::oneof_iterator::operator*() const {
3858   return upb_msg_iter_oneof(&iter_);
3859 }
3860 inline void MessageDef::oneof_iterator::operator++() {
3861   return upb_msg_oneof_next(&iter_);
3862 }
3863 inline bool MessageDef::oneof_iterator::operator==(
3864     const oneof_iterator &other) const {
3865   return upb_strtable_iter_isequal(&iter_, &other.iter_);
3866 }
3867 inline bool MessageDef::oneof_iterator::operator!=(
3868     const oneof_iterator &other) const {
3869   return !(*this == other);
3870 }
3871 
3872 inline MessageDef::const_oneof_iterator::const_oneof_iterator(
3873     const MessageDef* md) {
3874   upb_msg_oneof_begin(&iter_, md);
3875 }
3876 inline MessageDef::const_oneof_iterator MessageDef::const_oneof_iterator::end(
3877     const MessageDef *md) {
3878   MessageDef::const_oneof_iterator iter(md);
3879   upb_msg_oneof_iter_setdone(&iter.iter_);
3880   return iter;
3881 }
3882 inline const OneofDef* MessageDef::const_oneof_iterator::operator*() const {
3883   return upb_msg_iter_oneof(&iter_);
3884 }
3885 inline void MessageDef::const_oneof_iterator::operator++() {
3886   return upb_msg_oneof_next(&iter_);
3887 }
3888 inline bool MessageDef::const_oneof_iterator::operator==(
3889     const const_oneof_iterator &other) const {
3890   return upb_strtable_iter_isequal(&iter_, &other.iter_);
3891 }
3892 inline bool MessageDef::const_oneof_iterator::operator!=(
3893     const const_oneof_iterator &other) const {
3894   return !(*this == other);
3895 }
3896 
3897 inline reffed_ptr<EnumDef> EnumDef::New() {
3898   upb_enumdef *e = upb_enumdef_new(&e);
3899   return reffed_ptr<EnumDef>(e, &e);
3900 }
3901 inline const char* EnumDef::full_name() const {
3902   return upb_enumdef_fullname(this);
3903 }
3904 inline const char* EnumDef::name() const {
3905   return upb_enumdef_name(this);
3906 }
3907 inline bool EnumDef::set_full_name(const char* fullname, Status* s) {
3908   return upb_enumdef_setfullname(this, fullname, s);
3909 }
3910 inline bool EnumDef::set_full_name(const std::string& fullname, Status* s) {
3911   return upb_enumdef_setfullname(this, upb_safecstr(fullname), s);
3912 }
3913 inline bool EnumDef::Freeze(Status* status) {
3914   return upb_enumdef_freeze(this, status);
3915 }
3916 inline int32_t EnumDef::default_value() const {
3917   return upb_enumdef_default(this);
3918 }
3919 inline bool EnumDef::set_default_value(int32_t val, Status* status) {
3920   return upb_enumdef_setdefault(this, val, status);
3921 }
3922 inline int EnumDef::value_count() const { return upb_enumdef_numvals(this); }
3923 inline bool EnumDef::AddValue(const char* name, int32_t num, Status* status) {
3924   return upb_enumdef_addval(this, name, num, status);
3925 }
3926 inline bool EnumDef::AddValue(const std::string& name, int32_t num,
3927                               Status* status) {
3928   return upb_enumdef_addval(this, upb_safecstr(name), num, status);
3929 }
3930 inline bool EnumDef::FindValueByName(const char* name, int32_t *num) const {
3931   return upb_enumdef_ntoiz(this, name, num);
3932 }
3933 inline const char* EnumDef::FindValueByNumber(int32_t num) const {
3934   return upb_enumdef_iton(this, num);
3935 }
3936 
3937 inline EnumDef::Iterator::Iterator(const EnumDef* e) {
3938   upb_enum_begin(&iter_, e);
3939 }
3940 inline int32_t EnumDef::Iterator::number() {
3941   return upb_enum_iter_number(&iter_);
3942 }
3943 inline const char* EnumDef::Iterator::name() {
3944   return upb_enum_iter_name(&iter_);
3945 }
3946 inline bool EnumDef::Iterator::Done() { return upb_enum_done(&iter_); }
3947 inline void EnumDef::Iterator::Next() { return upb_enum_next(&iter_); }
3948 
3949 inline reffed_ptr<OneofDef> OneofDef::New() {
3950   upb_oneofdef *o = upb_oneofdef_new(&o);
3951   return reffed_ptr<OneofDef>(o, &o);
3952 }
3953 
3954 inline const MessageDef* OneofDef::containing_type() const {
3955   return upb_oneofdef_containingtype(this);
3956 }
3957 inline const char* OneofDef::name() const {
3958   return upb_oneofdef_name(this);
3959 }
3960 inline bool OneofDef::set_name(const char* name, Status* s) {
3961   return upb_oneofdef_setname(this, name, s);
3962 }
3963 inline bool OneofDef::set_name(const std::string& name, Status* s) {
3964   return upb_oneofdef_setname(this, upb_safecstr(name), s);
3965 }
3966 inline int OneofDef::field_count() const {
3967   return upb_oneofdef_numfields(this);
3968 }
3969 inline bool OneofDef::AddField(FieldDef* field, Status* s) {
3970   return upb_oneofdef_addfield(this, field, NULL, s);
3971 }
3972 inline bool OneofDef::AddField(const reffed_ptr<FieldDef>& field, Status* s) {
3973   return upb_oneofdef_addfield(this, field.get(), NULL, s);
3974 }
3975 inline const FieldDef* OneofDef::FindFieldByName(const char* name,
3976                                                  size_t len) const {
3977   return upb_oneofdef_ntof(this, name, len);
3978 }
3979 inline const FieldDef* OneofDef::FindFieldByNumber(uint32_t num) const {
3980   return upb_oneofdef_itof(this, num);
3981 }
3982 inline OneofDef::iterator OneofDef::begin() { return iterator(this); }
3983 inline OneofDef::iterator OneofDef::end() { return iterator::end(this); }
3984 inline OneofDef::const_iterator OneofDef::begin() const {
3985   return const_iterator(this);
3986 }
3987 inline OneofDef::const_iterator OneofDef::end() const {
3988   return const_iterator::end(this);
3989 }
3990 
3991 inline OneofDef::iterator::iterator(OneofDef* o) {
3992   upb_oneof_begin(&iter_, o);
3993 }
3994 inline OneofDef::iterator OneofDef::iterator::end(OneofDef* o) {
3995   OneofDef::iterator iter(o);
3996   upb_oneof_iter_setdone(&iter.iter_);
3997   return iter;
3998 }
3999 inline FieldDef* OneofDef::iterator::operator*() const {
4000   return upb_oneof_iter_field(&iter_);
4001 }
4002 inline void OneofDef::iterator::operator++() { return upb_oneof_next(&iter_); }
4003 inline bool OneofDef::iterator::operator==(const iterator &other) const {
4004   return upb_inttable_iter_isequal(&iter_, &other.iter_);
4005 }
4006 inline bool OneofDef::iterator::operator!=(const iterator &other) const {
4007   return !(*this == other);
4008 }
4009 
4010 inline OneofDef::const_iterator::const_iterator(const OneofDef* md) {
4011   upb_oneof_begin(&iter_, md);
4012 }
4013 inline OneofDef::const_iterator OneofDef::const_iterator::end(
4014     const OneofDef *md) {
4015   OneofDef::const_iterator iter(md);
4016   upb_oneof_iter_setdone(&iter.iter_);
4017   return iter;
4018 }
4019 inline const FieldDef* OneofDef::const_iterator::operator*() const {
4020   return upb_msg_iter_field(&iter_);
4021 }
4022 inline void OneofDef::const_iterator::operator++() {
4023   return upb_oneof_next(&iter_);
4024 }
4025 inline bool OneofDef::const_iterator::operator==(
4026     const const_iterator &other) const {
4027   return upb_inttable_iter_isequal(&iter_, &other.iter_);
4028 }
4029 inline bool OneofDef::const_iterator::operator!=(
4030     const const_iterator &other) const {
4031   return !(*this == other);
4032 }
4033 
4034 inline reffed_ptr<FileDef> FileDef::New() {
4035   upb_filedef *f = upb_filedef_new(&f);
4036   return reffed_ptr<FileDef>(f, &f);
4037 }
4038 
4039 inline const char* FileDef::name() const {
4040   return upb_filedef_name(this);
4041 }
4042 inline bool FileDef::set_name(const char* name, Status* s) {
4043   return upb_filedef_setname(this, name, s);
4044 }
4045 inline bool FileDef::set_name(const std::string& name, Status* s) {
4046   return upb_filedef_setname(this, upb_safecstr(name), s);
4047 }
4048 inline const char* FileDef::package() const {
4049   return upb_filedef_package(this);
4050 }
4051 inline bool FileDef::set_package(const char* package, Status* s) {
4052   return upb_filedef_setpackage(this, package, s);
4053 }
4054 inline const char* FileDef::phpprefix() const {
4055   return upb_filedef_phpprefix(this);
4056 }
4057 inline bool FileDef::set_phpprefix(const char* phpprefix, Status* s) {
4058   return upb_filedef_setphpprefix(this, phpprefix, s);
4059 }
4060 inline const char* FileDef::phpnamespace() const {
4061   return upb_filedef_phpnamespace(this);
4062 }
4063 inline bool FileDef::set_phpnamespace(const char* phpnamespace, Status* s) {
4064   return upb_filedef_setphpnamespace(this, phpnamespace, s);
4065 }
4066 inline int FileDef::def_count() const {
4067   return upb_filedef_defcount(this);
4068 }
4069 inline const Def* FileDef::def(int index) const {
4070   return upb_filedef_def(this, index);
4071 }
4072 inline Def* FileDef::def(int index) {
4073   return const_cast<Def*>(upb_filedef_def(this, index));
4074 }
4075 inline int FileDef::dependency_count() const {
4076   return upb_filedef_depcount(this);
4077 }
4078 inline const FileDef* FileDef::dependency(int index) const {
4079   return upb_filedef_dep(this, index);
4080 }
4081 inline bool FileDef::AddDef(Def* def, Status* s) {
4082   return upb_filedef_adddef(this, def, NULL, s);
4083 }
4084 inline bool FileDef::AddMessage(MessageDef* m, Status* s) {
4085   return upb_filedef_addmsg(this, m, NULL, s);
4086 }
4087 inline bool FileDef::AddEnum(EnumDef* e, Status* s) {
4088   return upb_filedef_addenum(this, e, NULL, s);
4089 }
4090 inline bool FileDef::AddExtension(FieldDef* f, Status* s) {
4091   return upb_filedef_addext(this, f, NULL, s);
4092 }
4093 inline bool FileDef::AddDependency(const FileDef* file) {
4094   return upb_filedef_adddep(this, file);
4095 }
4096 
4097 }  /* namespace upb */
4098 #endif
4099 
4100 #endif /* UPB_DEF_H_ */
4101 /*
4102 ** upb::Handlers (upb_handlers)
4103 **
4104 ** A upb_handlers is like a virtual table for a upb_msgdef.  Each field of the
4105 ** message can have associated functions that will be called when we are
4106 ** parsing or visiting a stream of data.  This is similar to how handlers work
4107 ** in SAX (the Simple API for XML).
4108 **
4109 ** The handlers have no idea where the data is coming from, so a single set of
4110 ** handlers could be used with two completely different data sources (for
4111 ** example, a parser and a visitor over in-memory objects).  This decoupling is
4112 ** the most important feature of upb, because it allows parsers and serializers
4113 ** to be highly reusable.
4114 **
4115 ** This is a mixed C/C++ interface that offers a full API to both languages.
4116 ** See the top-level README for more information.
4117 */
4118 
4119 #ifndef UPB_HANDLERS_H
4120 #define UPB_HANDLERS_H
4121 
4122 
4123 #ifdef __cplusplus
4124 namespace upb {
4125 class BufferHandle;
4126 class BytesHandler;
4127 class HandlerAttributes;
4128 class Handlers;
4129 template <class T> class Handler;
4130 template <class T> struct CanonicalType;
4131 }  /* namespace upb */
4132 #endif
4133 
4134 UPB_DECLARE_TYPE(upb::BufferHandle, upb_bufhandle)
4135 UPB_DECLARE_TYPE(upb::BytesHandler, upb_byteshandler)
4136 UPB_DECLARE_TYPE(upb::HandlerAttributes, upb_handlerattr)
4137 UPB_DECLARE_DERIVED_TYPE(upb::Handlers, upb::RefCounted,
4138                          upb_handlers, upb_refcounted)
4139 
4140 /* The maximum depth that the handler graph can have.  This is a resource limit
4141  * for the C stack since we sometimes need to recursively traverse the graph.
4142  * Cycles are ok; the traversal will stop when it detects a cycle, but we must
4143  * hit the cycle before the maximum depth is reached.
4144  *
4145  * If having a single static limit is too inflexible, we can add another variant
4146  * of Handlers::Freeze that allows specifying this as a parameter. */
4147 #define UPB_MAX_HANDLER_DEPTH 64
4148 
4149 /* All the different types of handlers that can be registered.
4150  * Only needed for the advanced functions in upb::Handlers. */
4151 typedef enum {
4152   UPB_HANDLER_INT32,
4153   UPB_HANDLER_INT64,
4154   UPB_HANDLER_UINT32,
4155   UPB_HANDLER_UINT64,
4156   UPB_HANDLER_FLOAT,
4157   UPB_HANDLER_DOUBLE,
4158   UPB_HANDLER_BOOL,
4159   UPB_HANDLER_STARTSTR,
4160   UPB_HANDLER_STRING,
4161   UPB_HANDLER_ENDSTR,
4162   UPB_HANDLER_STARTSUBMSG,
4163   UPB_HANDLER_ENDSUBMSG,
4164   UPB_HANDLER_STARTSEQ,
4165   UPB_HANDLER_ENDSEQ
4166 } upb_handlertype_t;
4167 
4168 #define UPB_HANDLER_MAX (UPB_HANDLER_ENDSEQ+1)
4169 
4170 #define UPB_BREAK NULL
4171 
4172 /* A convenient definition for when no closure is needed. */
4173 extern char _upb_noclosure;
4174 #define UPB_NO_CLOSURE &_upb_noclosure
4175 
4176 /* A selector refers to a specific field handler in the Handlers object
4177  * (for example: the STARTSUBMSG handler for field "field15"). */
4178 typedef int32_t upb_selector_t;
4179 
4180 UPB_BEGIN_EXTERN_C
4181 
4182 /* Forward-declares for C inline accessors.  We need to declare these here
4183  * so we can "friend" them in the class declarations in C++. */
4184 UPB_INLINE upb_func *upb_handlers_gethandler(const upb_handlers *h,
4185                                              upb_selector_t s);
4186 UPB_INLINE const void *upb_handlerattr_handlerdata(const upb_handlerattr *attr);
4187 UPB_INLINE const void *upb_handlers_gethandlerdata(const upb_handlers *h,
4188                                                    upb_selector_t s);
4189 
4190 UPB_INLINE void upb_bufhandle_init(upb_bufhandle *h);
4191 UPB_INLINE void upb_bufhandle_setobj(upb_bufhandle *h, const void *obj,
4192                                      const void *type);
4193 UPB_INLINE void upb_bufhandle_setbuf(upb_bufhandle *h, const char *buf,
4194                                      size_t ofs);
4195 UPB_INLINE const void *upb_bufhandle_obj(const upb_bufhandle *h);
4196 UPB_INLINE const void *upb_bufhandle_objtype(const upb_bufhandle *h);
4197 UPB_INLINE const char *upb_bufhandle_buf(const upb_bufhandle *h);
4198 
4199 UPB_END_EXTERN_C
4200 
4201 
4202 /* Static selectors for upb::Handlers. */
4203 #define UPB_STARTMSG_SELECTOR 0
4204 #define UPB_ENDMSG_SELECTOR 1
4205 #define UPB_UNKNOWN_SELECTOR 2
4206 #define UPB_STATIC_SELECTOR_COUNT 3
4207 
4208 /* Static selectors for upb::BytesHandler. */
4209 #define UPB_STARTSTR_SELECTOR 0
4210 #define UPB_STRING_SELECTOR 1
4211 #define UPB_ENDSTR_SELECTOR 2
4212 
4213 typedef void upb_handlerfree(void *d);
4214 
4215 #ifdef __cplusplus
4216 
4217 /* A set of attributes that accompanies a handler's function pointer. */
4218 class upb::HandlerAttributes {
4219  public:
4220   HandlerAttributes();
4221   ~HandlerAttributes();
4222 
4223   /* Sets the handler data that will be passed as the second parameter of the
4224    * handler.  To free this pointer when the handlers are freed, call
4225    * Handlers::AddCleanup(). */
4226   bool SetHandlerData(const void *handler_data);
4227   const void* handler_data() const;
4228 
4229   /* Use this to specify the type of the closure.  This will be checked against
4230    * all other closure types for handler that use the same closure.
4231    * Registration will fail if this does not match all other non-NULL closure
4232    * types. */
4233   bool SetClosureType(const void *closure_type);
4234   const void* closure_type() const;
4235 
4236   /* Use this to specify the type of the returned closure.  Only used for
4237    * Start*{String,SubMessage,Sequence} handlers.  This must match the closure
4238    * type of any handlers that use it (for example, the StringBuf handler must
4239    * match the closure returned from StartString). */
4240   bool SetReturnClosureType(const void *return_closure_type);
4241   const void* return_closure_type() const;
4242 
4243   /* Set to indicate that the handler always returns "ok" (either "true" or a
4244    * non-NULL closure).  This is a hint that can allow code generators to
4245    * generate more efficient code. */
4246   bool SetAlwaysOk(bool always_ok);
4247   bool always_ok() const;
4248 
4249  private:
4250   friend UPB_INLINE const void * ::upb_handlerattr_handlerdata(
4251       const upb_handlerattr *attr);
4252 #else
4253 struct upb_handlerattr {
4254 #endif
4255   const void *handler_data_;
4256   const void *closure_type_;
4257   const void *return_closure_type_;
4258   bool alwaysok_;
4259 };
4260 
4261 #define UPB_HANDLERATTR_INITIALIZER {NULL, NULL, NULL, false}
4262 
4263 typedef struct {
4264   upb_func *func;
4265 
4266   /* It is wasteful to include the entire attributes here:
4267    *
4268    * * Some of the information is redundant (like storing the closure type
4269    *   separately for each handler that must match).
4270    * * Some of the info is only needed prior to freeze() (like closure types).
4271    * * alignment padding wastes a lot of space for alwaysok_.
4272    *
4273    * If/when the size and locality of handlers is an issue, we can optimize this
4274    * not to store the entire attr like this.  We do not expose the table's
4275    * layout to allow this optimization in the future. */
4276   upb_handlerattr attr;
4277 } upb_handlers_tabent;
4278 
4279 #ifdef __cplusplus
4280 
4281 /* Extra information about a buffer that is passed to a StringBuf handler.
4282  * TODO(haberman): allow the handle to be pinned so that it will outlive
4283  * the handler invocation. */
4284 class upb::BufferHandle {
4285  public:
4286   BufferHandle();
4287   ~BufferHandle();
4288 
4289   /* The beginning of the buffer.  This may be different than the pointer
4290    * passed to a StringBuf handler because the handler may receive data
4291    * that is from the middle or end of a larger buffer. */
4292   const char* buffer() const;
4293 
4294   /* The offset within the attached object where this buffer begins.  Only
4295    * meaningful if there is an attached object. */
4296   size_t object_offset() const;
4297 
4298   /* Note that object_offset is the offset of "buf" within the attached
4299    * object. */
4300   void SetBuffer(const char* buf, size_t object_offset);
4301 
4302   /* The BufferHandle can have an "attached object", which can be used to
4303    * tunnel through a pointer to the buffer's underlying representation. */
4304   template <class T>
4305   void SetAttachedObject(const T* obj);
4306 
4307   /* Returns NULL if the attached object is not of this type. */
4308   template <class T>
4309   const T* GetAttachedObject() const;
4310 
4311  private:
4312   friend UPB_INLINE void ::upb_bufhandle_init(upb_bufhandle *h);
4313   friend UPB_INLINE void ::upb_bufhandle_setobj(upb_bufhandle *h,
4314                                                 const void *obj,
4315                                                 const void *type);
4316   friend UPB_INLINE void ::upb_bufhandle_setbuf(upb_bufhandle *h,
4317                                                 const char *buf, size_t ofs);
4318   friend UPB_INLINE const void* ::upb_bufhandle_obj(const upb_bufhandle *h);
4319   friend UPB_INLINE const void* ::upb_bufhandle_objtype(
4320       const upb_bufhandle *h);
4321   friend UPB_INLINE const char* ::upb_bufhandle_buf(const upb_bufhandle *h);
4322 #else
4323 struct upb_bufhandle {
4324 #endif
4325   const char *buf_;
4326   const void *obj_;
4327   const void *objtype_;
4328   size_t objofs_;
4329 };
4330 
4331 #ifdef __cplusplus
4332 
4333 /* A upb::Handlers object represents the set of handlers associated with a
4334  * message in the graph of messages.  You can think of it as a big virtual
4335  * table with functions corresponding to all the events that can fire while
4336  * parsing or visiting a message of a specific type.
4337  *
4338  * Any handlers that are not set behave as if they had successfully consumed
4339  * the value.  Any unset Start* handlers will propagate their closure to the
4340  * inner frame.
4341  *
4342  * The easiest way to create the *Handler objects needed by the Set* methods is
4343  * with the UpbBind() and UpbMakeHandler() macros; see below. */
4344 class upb::Handlers {
4345  public:
4346   typedef upb_selector_t Selector;
4347   typedef upb_handlertype_t Type;
4348 
4349   typedef Handler<void *(*)(void *, const void *)> StartFieldHandler;
4350   typedef Handler<bool (*)(void *, const void *)> EndFieldHandler;
4351   typedef Handler<bool (*)(void *, const void *)> StartMessageHandler;
4352   typedef Handler<bool (*)(void *, const void *, Status*)> EndMessageHandler;
4353   typedef Handler<void *(*)(void *, const void *, size_t)> StartStringHandler;
4354   typedef Handler<size_t (*)(void *, const void *, const char *, size_t,
4355                              const BufferHandle *)> StringHandler;
4356 
4357   template <class T> struct ValueHandler {
4358     typedef Handler<bool(*)(void *, const void *, T)> H;
4359   };
4360 
4361   typedef ValueHandler<int32_t>::H     Int32Handler;
4362   typedef ValueHandler<int64_t>::H     Int64Handler;
4363   typedef ValueHandler<uint32_t>::H    UInt32Handler;
4364   typedef ValueHandler<uint64_t>::H    UInt64Handler;
4365   typedef ValueHandler<float>::H       FloatHandler;
4366   typedef ValueHandler<double>::H      DoubleHandler;
4367   typedef ValueHandler<bool>::H        BoolHandler;
4368 
4369   /* Any function pointer can be converted to this and converted back to its
4370    * correct type. */
4371   typedef void GenericFunction();
4372 
4373   typedef void HandlersCallback(const void *closure, upb_handlers *h);
4374 
4375   /* Returns a new handlers object for the given frozen msgdef.
4376    * Returns NULL if memory allocation failed. */
4377   static reffed_ptr<Handlers> New(const MessageDef *m);
4378 
4379   /* Convenience function for registering a graph of handlers that mirrors the
4380    * graph of msgdefs for some message.  For "m" and all its children a new set
4381    * of handlers will be created and the given callback will be invoked,
4382    * allowing the client to register handlers for this message.  Note that any
4383    * subhandlers set by the callback will be overwritten. */
4384   static reffed_ptr<const Handlers> NewFrozen(const MessageDef *m,
4385                                               HandlersCallback *callback,
4386                                               const void *closure);
4387 
4388   /* Functionality from upb::RefCounted. */
4389   UPB_REFCOUNTED_CPPMETHODS
4390 
4391   /* All handler registration functions return bool to indicate success or
4392    * failure; details about failures are stored in this status object.  If a
4393    * failure does occur, it must be cleared before the Handlers are frozen,
4394    * otherwise the freeze() operation will fail.  The functions may *only* be
4395    * used while the Handlers are mutable. */
4396   const Status* status();
4397   void ClearError();
4398 
4399   /* Call to freeze these Handlers.  Requires that any SubHandlers are already
4400    * frozen.  For cycles, you must use the static version below and freeze the
4401    * whole graph at once. */
4402   bool Freeze(Status* s);
4403 
4404   /* Freezes the given set of handlers.  You may not freeze a handler without
4405    * also freezing any handlers they point to. */
4406   static bool Freeze(Handlers*const* handlers, int n, Status* s);
4407   static bool Freeze(const std::vector<Handlers*>& handlers, Status* s);
4408 
4409   /* Returns the msgdef associated with this handlers object. */
4410   const MessageDef* message_def() const;
4411 
4412   /* Adds the given pointer and function to the list of cleanup functions that
4413    * will be run when these handlers are freed.  If this pointer has previously
4414    * been registered, the function returns false and does nothing. */
4415   bool AddCleanup(void *ptr, upb_handlerfree *cleanup);
4416 
4417   /* Sets the startmsg handler for the message, which is defined as follows:
4418    *
4419    *   bool startmsg(MyType* closure) {
4420    *     // Called when the message begins.  Returns true if processing should
4421    *     // continue.
4422    *     return true;
4423    *   }
4424    */
4425   bool SetStartMessageHandler(const StartMessageHandler& handler);
4426 
4427   /* Sets the endmsg handler for the message, which is defined as follows:
4428    *
4429    *   bool endmsg(MyType* closure, upb_status *status) {
4430    *     // Called when processing of this message ends, whether in success or
4431    *     // failure.  "status" indicates the final status of processing, and
4432    *     // can also be modified in-place to update the final status.
4433    *   }
4434    */
4435   bool SetEndMessageHandler(const EndMessageHandler& handler);
4436 
4437   /* Sets the value handler for the given field, which is defined as follows
4438    * (this is for an int32 field; other field types will pass their native
4439    * C/C++ type for "val"):
4440    *
4441    *   bool OnValue(MyClosure* c, const MyHandlerData* d, int32_t val) {
4442    *     // Called when the field's value is encountered.  "d" contains
4443    *     // whatever data was bound to this field when it was registered.
4444    *     // Returns true if processing should continue.
4445    *     return true;
4446    *   }
4447    *
4448    *   handers->SetInt32Handler(f, UpbBind(OnValue, new MyHandlerData(...)));
4449    *
4450    * The value type must exactly match f->type().
4451    * For example, a handler that takes an int32_t parameter may only be used for
4452    * fields of type UPB_TYPE_INT32 and UPB_TYPE_ENUM.
4453    *
4454    * Returns false if the handler failed to register; in this case the cleanup
4455    * handler (if any) will be called immediately.
4456    */
4457   bool SetInt32Handler (const FieldDef* f,  const Int32Handler& h);
4458   bool SetInt64Handler (const FieldDef* f,  const Int64Handler& h);
4459   bool SetUInt32Handler(const FieldDef* f, const UInt32Handler& h);
4460   bool SetUInt64Handler(const FieldDef* f, const UInt64Handler& h);
4461   bool SetFloatHandler (const FieldDef* f,  const FloatHandler& h);
4462   bool SetDoubleHandler(const FieldDef* f, const DoubleHandler& h);
4463   bool SetBoolHandler  (const FieldDef* f,   const BoolHandler& h);
4464 
4465   /* Like the previous, but templated on the type on the value (ie. int32).
4466    * This is mostly useful to call from other templates.  To call this you must
4467    * specify the template parameter explicitly, ie:
4468    *   h->SetValueHandler<T>(f, UpbBind(MyHandler<T>, MyData)); */
4469   template <class T>
4470   bool SetValueHandler(
4471       const FieldDef *f,
4472       const typename ValueHandler<typename CanonicalType<T>::Type>::H& handler);
4473 
4474   /* Sets handlers for a string field, which are defined as follows:
4475    *
4476    *   MySubClosure* startstr(MyClosure* c, const MyHandlerData* d,
4477    *                          size_t size_hint) {
4478    *     // Called when a string value begins.  The return value indicates the
4479    *     // closure for the string.  "size_hint" indicates the size of the
4480    *     // string if it is known, however if the string is length-delimited
4481    *     // and the end-of-string is not available size_hint will be zero.
4482    *     // This case is indistinguishable from the case where the size is
4483    *     // known to be zero.
4484    *     //
4485    *     // TODO(haberman): is it important to distinguish these cases?
4486    *     // If we had ssize_t as a type we could make -1 "unknown", but
4487    *     // ssize_t is POSIX (not ANSI) and therefore less portable.
4488    *     // In practice I suspect it won't be important to distinguish.
4489    *     return closure;
4490    *   }
4491    *
4492    *   size_t str(MyClosure* closure, const MyHandlerData* d,
4493    *              const char *str, size_t len) {
4494    *     // Called for each buffer of string data; the multiple physical buffers
4495    *     // are all part of the same logical string.  The return value indicates
4496    *     // how many bytes were consumed.  If this number is less than "len",
4497    *     // this will also indicate that processing should be halted for now,
4498    *     // like returning false or UPB_BREAK from any other callback.  If
4499    *     // number is greater than "len", the excess bytes will be skipped over
4500    *     // and not passed to the callback.
4501    *     return len;
4502    *   }
4503    *
4504    *   bool endstr(MyClosure* c, const MyHandlerData* d) {
4505    *     // Called when a string value ends.  Return value indicates whether
4506    *     // processing should continue.
4507    *     return true;
4508    *   }
4509    */
4510   bool SetStartStringHandler(const FieldDef* f, const StartStringHandler& h);
4511   bool SetStringHandler(const FieldDef* f, const StringHandler& h);
4512   bool SetEndStringHandler(const FieldDef* f, const EndFieldHandler& h);
4513 
4514   /* Sets the startseq handler, which is defined as follows:
4515    *
4516    *   MySubClosure *startseq(MyClosure* c, const MyHandlerData* d) {
4517    *     // Called when a sequence (repeated field) begins.  The returned
4518    *     // pointer indicates the closure for the sequence (or UPB_BREAK
4519    *     // to interrupt processing).
4520    *     return closure;
4521    *   }
4522    *
4523    *   h->SetStartSequenceHandler(f, UpbBind(startseq, new MyHandlerData(...)));
4524    *
4525    * Returns "false" if "f" does not belong to this message or is not a
4526    * repeated field.
4527    */
4528   bool SetStartSequenceHandler(const FieldDef* f, const StartFieldHandler& h);
4529 
4530   /* Sets the startsubmsg handler for the given field, which is defined as
4531    * follows:
4532    *
4533    *   MySubClosure* startsubmsg(MyClosure* c, const MyHandlerData* d) {
4534    *     // Called when a submessage begins.  The returned pointer indicates the
4535    *     // closure for the sequence (or UPB_BREAK to interrupt processing).
4536    *     return closure;
4537    *   }
4538    *
4539    *   h->SetStartSubMessageHandler(f, UpbBind(startsubmsg,
4540    *                                           new MyHandlerData(...)));
4541    *
4542    * Returns "false" if "f" does not belong to this message or is not a
4543    * submessage/group field.
4544    */
4545   bool SetStartSubMessageHandler(const FieldDef* f, const StartFieldHandler& h);
4546 
4547   /* Sets the endsubmsg handler for the given field, which is defined as
4548    * follows:
4549    *
4550    *   bool endsubmsg(MyClosure* c, const MyHandlerData* d) {
4551    *     // Called when a submessage ends.  Returns true to continue processing.
4552    *     return true;
4553    *   }
4554    *
4555    * Returns "false" if "f" does not belong to this message or is not a
4556    * submessage/group field.
4557    */
4558   bool SetEndSubMessageHandler(const FieldDef *f, const EndFieldHandler &h);
4559 
4560   /* Starts the endsubseq handler for the given field, which is defined as
4561    * follows:
4562    *
4563    *   bool endseq(MyClosure* c, const MyHandlerData* d) {
4564    *     // Called when a sequence ends.  Returns true continue processing.
4565    *     return true;
4566    *   }
4567    *
4568    * Returns "false" if "f" does not belong to this message or is not a
4569    * repeated field.
4570    */
4571   bool SetEndSequenceHandler(const FieldDef* f, const EndFieldHandler& h);
4572 
4573   /* Sets or gets the object that specifies handlers for the given field, which
4574    * must be a submessage or group.  Returns NULL if no handlers are set. */
4575   bool SetSubHandlers(const FieldDef* f, const Handlers* sub);
4576   const Handlers* GetSubHandlers(const FieldDef* f) const;
4577 
4578   /* Equivalent to GetSubHandlers, but takes the STARTSUBMSG selector for the
4579    * field. */
4580   const Handlers* GetSubHandlers(Selector startsubmsg) const;
4581 
4582   /* A selector refers to a specific field handler in the Handlers object
4583    * (for example: the STARTSUBMSG handler for field "field15").
4584    * On success, returns true and stores the selector in "s".
4585    * If the FieldDef or Type are invalid, returns false.
4586    * The returned selector is ONLY valid for Handlers whose MessageDef
4587    * contains this FieldDef. */
4588   static bool GetSelector(const FieldDef* f, Type type, Selector* s);
4589 
4590   /* Given a START selector of any kind, returns the corresponding END selector. */
4591   static Selector GetEndSelector(Selector start_selector);
4592 
4593   /* Returns the function pointer for this handler.  It is the client's
4594    * responsibility to cast to the correct function type before calling it. */
4595   GenericFunction* GetHandler(Selector selector);
4596 
4597   /* Sets the given attributes to the attributes for this selector. */
4598   bool GetAttributes(Selector selector, HandlerAttributes* attr);
4599 
4600   /* Returns the handler data that was registered with this handler. */
4601   const void* GetHandlerData(Selector selector);
4602 
4603   /* Could add any of the following functions as-needed, with some minor
4604    * implementation changes:
4605    *
4606    * const FieldDef* GetFieldDef(Selector selector);
4607    * static bool IsSequence(Selector selector); */
4608 
4609  private:
4610   UPB_DISALLOW_POD_OPS(Handlers, upb::Handlers)
4611 
4612   friend UPB_INLINE GenericFunction *::upb_handlers_gethandler(
4613       const upb_handlers *h, upb_selector_t s);
4614   friend UPB_INLINE const void *::upb_handlers_gethandlerdata(
4615       const upb_handlers *h, upb_selector_t s);
4616 #else
4617 struct upb_handlers {
4618 #endif
4619   upb_refcounted base;
4620 
4621   const upb_msgdef *msg;
4622   const upb_handlers **sub;
4623   const void *top_closure_type;
4624   upb_inttable cleanup_;
4625   upb_status status_;  /* Used only when mutable. */
4626   upb_handlers_tabent table[1];  /* Dynamically-sized field handler array. */
4627 };
4628 
4629 #ifdef __cplusplus
4630 
4631 namespace upb {
4632 
4633 /* Convenience macros for creating a Handler object that is wrapped with a
4634  * type-safe wrapper function that converts the "void*" parameters/returns
4635  * of the underlying C API into nice C++ function.
4636  *
4637  * Sample usage:
4638  *   void OnValue1(MyClosure* c, const MyHandlerData* d, int32_t val) {
4639  *     // do stuff ...
4640  *   }
4641  *
4642  *   // Handler that doesn't need any data bound to it.
4643  *   void OnValue2(MyClosure* c, int32_t val) {
4644  *     // do stuff ...
4645  *   }
4646  *
4647  *   // Handler that returns bool so it can return failure if necessary.
4648  *   bool OnValue3(MyClosure* c, int32_t val) {
4649  *     // do stuff ...
4650  *     return ok;
4651  *   }
4652  *
4653  *   // Member function handler.
4654  *   class MyClosure {
4655  *    public:
4656  *     void OnValue(int32_t val) {
4657  *       // do stuff ...
4658  *     }
4659  *   };
4660  *
4661  *   // Takes ownership of the MyHandlerData.
4662  *   handlers->SetInt32Handler(f1, UpbBind(OnValue1, new MyHandlerData(...)));
4663  *   handlers->SetInt32Handler(f2, UpbMakeHandler(OnValue2));
4664  *   handlers->SetInt32Handler(f1, UpbMakeHandler(OnValue3));
4665  *   handlers->SetInt32Handler(f2, UpbMakeHandler(&MyClosure::OnValue));
4666  */
4667 
4668 #ifdef UPB_CXX11
4669 
4670 /* In C++11, the "template" disambiguator can appear even outside templates,
4671  * so all calls can safely use this pair of macros. */
4672 
4673 #define UpbMakeHandler(f) upb::MatchFunc(f).template GetFunc<f>()
4674 
4675 /* We have to be careful to only evaluate "d" once. */
4676 #define UpbBind(f, d) upb::MatchFunc(f).template GetFunc<f>((d))
4677 
4678 #else
4679 
4680 /* Prior to C++11, the "template" disambiguator may only appear inside a
4681  * template, so the regular macro must not use "template" */
4682 
4683 #define UpbMakeHandler(f) upb::MatchFunc(f).GetFunc<f>()
4684 
4685 #define UpbBind(f, d) upb::MatchFunc(f).GetFunc<f>((d))
4686 
4687 #endif  /* UPB_CXX11 */
4688 
4689 /* This macro must be used in C++98 for calls from inside a template.  But we
4690  * define this variant in all cases; code that wants to be compatible with both
4691  * C++98 and C++11 should always use this macro when calling from a template. */
4692 #define UpbMakeHandlerT(f) upb::MatchFunc(f).template GetFunc<f>()
4693 
4694 /* We have to be careful to only evaluate "d" once. */
4695 #define UpbBindT(f, d) upb::MatchFunc(f).template GetFunc<f>((d))
4696 
4697 /* Handler: a struct that contains the (handler, data, deleter) tuple that is
4698  * used to register all handlers.  Users can Make() these directly but it's
4699  * more convenient to use the UpbMakeHandler/UpbBind macros above. */
4700 template <class T> class Handler {
4701  public:
4702   /* The underlying, handler function signature that upb uses internally. */
4703   typedef T FuncPtr;
4704 
4705   /* Intentionally implicit. */
4706   template <class F> Handler(F func);
4707   ~Handler();
4708 
4709  private:
4710   void AddCleanup(Handlers* h) const {
4711     if (cleanup_func_) {
4712       bool ok = h->AddCleanup(cleanup_data_, cleanup_func_);
4713       UPB_ASSERT(ok);
4714     }
4715   }
4716 
4717   UPB_DISALLOW_COPY_AND_ASSIGN(Handler)
4718   friend class Handlers;
4719   FuncPtr handler_;
4720   mutable HandlerAttributes attr_;
4721   mutable bool registered_;
4722   void *cleanup_data_;
4723   upb_handlerfree *cleanup_func_;
4724 };
4725 
4726 }  /* namespace upb */
4727 
4728 #endif  /* __cplusplus */
4729 
4730 UPB_BEGIN_EXTERN_C
4731 
4732 /* Native C API. */
4733 
4734 /* Handler function typedefs. */
4735 typedef bool upb_unknown_handlerfunc(void *c, const void *hd, const char *buf,
4736                                      size_t n);
4737 typedef bool upb_startmsg_handlerfunc(void *c, const void*);
4738 typedef bool upb_endmsg_handlerfunc(void *c, const void *, upb_status *status);
4739 typedef void* upb_startfield_handlerfunc(void *c, const void *hd);
4740 typedef bool upb_endfield_handlerfunc(void *c, const void *hd);
4741 typedef bool upb_int32_handlerfunc(void *c, const void *hd, int32_t val);
4742 typedef bool upb_int64_handlerfunc(void *c, const void *hd, int64_t val);
4743 typedef bool upb_uint32_handlerfunc(void *c, const void *hd, uint32_t val);
4744 typedef bool upb_uint64_handlerfunc(void *c, const void *hd, uint64_t val);
4745 typedef bool upb_float_handlerfunc(void *c, const void *hd, float val);
4746 typedef bool upb_double_handlerfunc(void *c, const void *hd, double val);
4747 typedef bool upb_bool_handlerfunc(void *c, const void *hd, bool val);
4748 typedef void *upb_startstr_handlerfunc(void *c, const void *hd,
4749                                        size_t size_hint);
4750 typedef size_t upb_string_handlerfunc(void *c, const void *hd, const char *buf,
4751                                       size_t n, const upb_bufhandle* handle);
4752 
4753 /* upb_bufhandle */
4754 size_t upb_bufhandle_objofs(const upb_bufhandle *h);
4755 
4756 /* upb_handlerattr */
4757 void upb_handlerattr_init(upb_handlerattr *attr);
4758 void upb_handlerattr_uninit(upb_handlerattr *attr);
4759 
4760 bool upb_handlerattr_sethandlerdata(upb_handlerattr *attr, const void *hd);
4761 bool upb_handlerattr_setclosuretype(upb_handlerattr *attr, const void *type);
4762 const void *upb_handlerattr_closuretype(const upb_handlerattr *attr);
4763 bool upb_handlerattr_setreturnclosuretype(upb_handlerattr *attr,
4764                                           const void *type);
4765 const void *upb_handlerattr_returnclosuretype(const upb_handlerattr *attr);
4766 bool upb_handlerattr_setalwaysok(upb_handlerattr *attr, bool alwaysok);
4767 bool upb_handlerattr_alwaysok(const upb_handlerattr *attr);
4768 
4769 UPB_INLINE const void *upb_handlerattr_handlerdata(
4770     const upb_handlerattr *attr) {
4771   return attr->handler_data_;
4772 }
4773 
4774 /* upb_handlers */
4775 typedef void upb_handlers_callback(const void *closure, upb_handlers *h);
4776 upb_handlers *upb_handlers_new(const upb_msgdef *m,
4777                                const void *owner);
4778 const upb_handlers *upb_handlers_newfrozen(const upb_msgdef *m,
4779                                            const void *owner,
4780                                            upb_handlers_callback *callback,
4781                                            const void *closure);
4782 
4783 /* Include refcounted methods like upb_handlers_ref(). */
4784 UPB_REFCOUNTED_CMETHODS(upb_handlers, upb_handlers_upcast)
4785 
4786 const upb_status *upb_handlers_status(upb_handlers *h);
4787 void upb_handlers_clearerr(upb_handlers *h);
4788 const upb_msgdef *upb_handlers_msgdef(const upb_handlers *h);
4789 bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *hfree);
4790 bool upb_handlers_setunknown(upb_handlers *h, upb_unknown_handlerfunc *func,
4791                              upb_handlerattr *attr);
4792 
4793 bool upb_handlers_setstartmsg(upb_handlers *h, upb_startmsg_handlerfunc *func,
4794                               upb_handlerattr *attr);
4795 bool upb_handlers_setendmsg(upb_handlers *h, upb_endmsg_handlerfunc *func,
4796                             upb_handlerattr *attr);
4797 bool upb_handlers_setint32(upb_handlers *h, const upb_fielddef *f,
4798                            upb_int32_handlerfunc *func, upb_handlerattr *attr);
4799 bool upb_handlers_setint64(upb_handlers *h, const upb_fielddef *f,
4800                            upb_int64_handlerfunc *func, upb_handlerattr *attr);
4801 bool upb_handlers_setuint32(upb_handlers *h, const upb_fielddef *f,
4802                             upb_uint32_handlerfunc *func,
4803                             upb_handlerattr *attr);
4804 bool upb_handlers_setuint64(upb_handlers *h, const upb_fielddef *f,
4805                             upb_uint64_handlerfunc *func,
4806                             upb_handlerattr *attr);
4807 bool upb_handlers_setfloat(upb_handlers *h, const upb_fielddef *f,
4808                            upb_float_handlerfunc *func, upb_handlerattr *attr);
4809 bool upb_handlers_setdouble(upb_handlers *h, const upb_fielddef *f,
4810                             upb_double_handlerfunc *func,
4811                             upb_handlerattr *attr);
4812 bool upb_handlers_setbool(upb_handlers *h, const upb_fielddef *f,
4813                           upb_bool_handlerfunc *func,
4814                           upb_handlerattr *attr);
4815 bool upb_handlers_setstartstr(upb_handlers *h, const upb_fielddef *f,
4816                               upb_startstr_handlerfunc *func,
4817                               upb_handlerattr *attr);
4818 bool upb_handlers_setstring(upb_handlers *h, const upb_fielddef *f,
4819                             upb_string_handlerfunc *func,
4820                             upb_handlerattr *attr);
4821 bool upb_handlers_setendstr(upb_handlers *h, const upb_fielddef *f,
4822                             upb_endfield_handlerfunc *func,
4823                             upb_handlerattr *attr);
4824 bool upb_handlers_setstartseq(upb_handlers *h, const upb_fielddef *f,
4825                               upb_startfield_handlerfunc *func,
4826                               upb_handlerattr *attr);
4827 bool upb_handlers_setstartsubmsg(upb_handlers *h, const upb_fielddef *f,
4828                                  upb_startfield_handlerfunc *func,
4829                                  upb_handlerattr *attr);
4830 bool upb_handlers_setendsubmsg(upb_handlers *h, const upb_fielddef *f,
4831                                upb_endfield_handlerfunc *func,
4832                                upb_handlerattr *attr);
4833 bool upb_handlers_setendseq(upb_handlers *h, const upb_fielddef *f,
4834                             upb_endfield_handlerfunc *func,
4835                             upb_handlerattr *attr);
4836 
4837 bool upb_handlers_setsubhandlers(upb_handlers *h, const upb_fielddef *f,
4838                                  const upb_handlers *sub);
4839 const upb_handlers *upb_handlers_getsubhandlers(const upb_handlers *h,
4840                                                 const upb_fielddef *f);
4841 const upb_handlers *upb_handlers_getsubhandlers_sel(const upb_handlers *h,
4842                                                     upb_selector_t sel);
4843 
4844 UPB_INLINE upb_func *upb_handlers_gethandler(const upb_handlers *h,
4845                                              upb_selector_t s) {
4846   return (upb_func *)h->table[s].func;
4847 }
4848 
4849 bool upb_handlers_getattr(const upb_handlers *h, upb_selector_t s,
4850                           upb_handlerattr *attr);
4851 
4852 UPB_INLINE const void *upb_handlers_gethandlerdata(const upb_handlers *h,
4853                                                    upb_selector_t s) {
4854   return upb_handlerattr_handlerdata(&h->table[s].attr);
4855 }
4856 
4857 #ifdef __cplusplus
4858 
4859 /* Handler types for single fields.
4860  * Right now we only have one for TYPE_BYTES but ones for other types
4861  * should follow.
4862  *
4863  * These follow the same handlers protocol for fields of a message. */
4864 class upb::BytesHandler {
4865  public:
4866   BytesHandler();
4867   ~BytesHandler();
4868 #else
4869 struct upb_byteshandler {
4870 #endif
4871   upb_handlers_tabent table[3];
4872 };
4873 
4874 void upb_byteshandler_init(upb_byteshandler *h);
4875 
4876 /* Caller must ensure that "d" outlives the handlers.
4877  * TODO(haberman): should this have a "freeze" operation?  It's not necessary
4878  * for memory management, but could be useful to force immutability and provide
4879  * a convenient moment to verify that all registration succeeded. */
4880 bool upb_byteshandler_setstartstr(upb_byteshandler *h,
4881                                   upb_startstr_handlerfunc *func, void *d);
4882 bool upb_byteshandler_setstring(upb_byteshandler *h,
4883                                 upb_string_handlerfunc *func, void *d);
4884 bool upb_byteshandler_setendstr(upb_byteshandler *h,
4885                                 upb_endfield_handlerfunc *func, void *d);
4886 
4887 /* "Static" methods */
4888 bool upb_handlers_freeze(upb_handlers *const *handlers, int n, upb_status *s);
4889 upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f);
4890 bool upb_handlers_getselector(const upb_fielddef *f, upb_handlertype_t type,
4891                               upb_selector_t *s);
4892 UPB_INLINE upb_selector_t upb_handlers_getendselector(upb_selector_t start) {
4893   return start + 1;
4894 }
4895 
4896 /* Internal-only. */
4897 uint32_t upb_handlers_selectorbaseoffset(const upb_fielddef *f);
4898 uint32_t upb_handlers_selectorcount(const upb_fielddef *f);
4899 
4900 
4901 /** Message handlers ******************************************************************/
4902 
4903 /* These are the handlers used internally by upb_msgfactory_getmergehandlers().
4904  * They write scalar data to a known offset from the message pointer.
4905  *
4906  * These would be trivial for anyone to implement themselves, but it's better
4907  * to use these because some JITs will recognize and specialize these instead
4908  * of actually calling the function. */
4909 
4910 /* Sets a handler for the given primitive field that will write the data at the
4911  * given offset.  If hasbit > 0, also sets a hasbit at the given bit offset
4912  * (addressing each byte low to high). */
4913 bool upb_msg_setscalarhandler(upb_handlers *h,
4914                               const upb_fielddef *f,
4915                               size_t offset,
4916                               int32_t hasbit);
4917 
4918 /* If the given handler is a msghandlers_primitive field, returns true and sets
4919  * *type, *offset and *hasbit.  Otherwise returns false. */
4920 bool upb_msg_getscalarhandlerdata(const upb_handlers *h,
4921                                   upb_selector_t s,
4922                                   upb_fieldtype_t *type,
4923                                   size_t *offset,
4924                                   int32_t *hasbit);
4925 
4926 
4927 
4928 UPB_END_EXTERN_C
4929 
4930 /*
4931 ** Inline definitions for handlers.h, which are particularly long and a bit
4932 ** tricky.
4933 */
4934 
4935 #ifndef UPB_HANDLERS_INL_H_
4936 #define UPB_HANDLERS_INL_H_
4937 
4938 #include <limits.h>
4939 
4940 /* C inline methods. */
4941 
4942 /* upb_bufhandle */
4943 UPB_INLINE void upb_bufhandle_init(upb_bufhandle *h) {
4944   h->obj_ = NULL;
4945   h->objtype_ = NULL;
4946   h->buf_ = NULL;
4947   h->objofs_ = 0;
4948 }
4949 UPB_INLINE void upb_bufhandle_uninit(upb_bufhandle *h) {
4950   UPB_UNUSED(h);
4951 }
4952 UPB_INLINE void upb_bufhandle_setobj(upb_bufhandle *h, const void *obj,
4953                                      const void *type) {
4954   h->obj_ = obj;
4955   h->objtype_ = type;
4956 }
4957 UPB_INLINE void upb_bufhandle_setbuf(upb_bufhandle *h, const char *buf,
4958                                      size_t ofs) {
4959   h->buf_ = buf;
4960   h->objofs_ = ofs;
4961 }
4962 UPB_INLINE const void *upb_bufhandle_obj(const upb_bufhandle *h) {
4963   return h->obj_;
4964 }
4965 UPB_INLINE const void *upb_bufhandle_objtype(const upb_bufhandle *h) {
4966   return h->objtype_;
4967 }
4968 UPB_INLINE const char *upb_bufhandle_buf(const upb_bufhandle *h) {
4969   return h->buf_;
4970 }
4971 
4972 
4973 #ifdef __cplusplus
4974 
4975 /* Type detection and typedefs for integer types.
4976  * For platforms where there are multiple 32-bit or 64-bit types, we need to be
4977  * able to enumerate them so we can properly create overloads for all variants.
4978  *
4979  * If any platform existed where there were three integer types with the same
4980  * size, this would have to become more complicated.  For example, short, int,
4981  * and long could all be 32-bits.  Even more diabolically, short, int, long,
4982  * and long long could all be 64 bits and still be standard-compliant.
4983  * However, few platforms are this strange, and it's unlikely that upb will be
4984  * used on the strangest ones. */
4985 
4986 /* Can't count on stdint.h limits like INT32_MAX, because in C++ these are
4987  * only defined when __STDC_LIMIT_MACROS are defined before the *first* include
4988  * of stdint.h.  We can't guarantee that someone else didn't include these first
4989  * without defining __STDC_LIMIT_MACROS. */
4990 #define UPB_INT32_MAX 0x7fffffffLL
4991 #define UPB_INT32_MIN (-UPB_INT32_MAX - 1)
4992 #define UPB_INT64_MAX 0x7fffffffffffffffLL
4993 #define UPB_INT64_MIN (-UPB_INT64_MAX - 1)
4994 
4995 #if INT_MAX == UPB_INT32_MAX && INT_MIN == UPB_INT32_MIN
4996 #define UPB_INT_IS_32BITS 1
4997 #endif
4998 
4999 #if LONG_MAX == UPB_INT32_MAX && LONG_MIN == UPB_INT32_MIN
5000 #define UPB_LONG_IS_32BITS 1
5001 #endif
5002 
5003 #if LONG_MAX == UPB_INT64_MAX && LONG_MIN == UPB_INT64_MIN
5004 #define UPB_LONG_IS_64BITS 1
5005 #endif
5006 
5007 #if LLONG_MAX == UPB_INT64_MAX && LLONG_MIN == UPB_INT64_MIN
5008 #define UPB_LLONG_IS_64BITS 1
5009 #endif
5010 
5011 /* We use macros instead of typedefs so we can undefine them later and avoid
5012  * leaking them outside this header file. */
5013 #if UPB_INT_IS_32BITS
5014 #define UPB_INT32_T int
5015 #define UPB_UINT32_T unsigned int
5016 
5017 #if UPB_LONG_IS_32BITS
5018 #define UPB_TWO_32BIT_TYPES 1
5019 #define UPB_INT32ALT_T long
5020 #define UPB_UINT32ALT_T unsigned long
5021 #endif  /* UPB_LONG_IS_32BITS */
5022 
5023 #elif UPB_LONG_IS_32BITS  /* && !UPB_INT_IS_32BITS */
5024 #define UPB_INT32_T long
5025 #define UPB_UINT32_T unsigned long
5026 #endif  /* UPB_INT_IS_32BITS */
5027 
5028 
5029 #if UPB_LONG_IS_64BITS
5030 #define UPB_INT64_T long
5031 #define UPB_UINT64_T unsigned long
5032 
5033 #if UPB_LLONG_IS_64BITS
5034 #define UPB_TWO_64BIT_TYPES 1
5035 #define UPB_INT64ALT_T long long
5036 #define UPB_UINT64ALT_T unsigned long long
5037 #endif  /* UPB_LLONG_IS_64BITS */
5038 
5039 #elif UPB_LLONG_IS_64BITS  /* && !UPB_LONG_IS_64BITS */
5040 #define UPB_INT64_T long long
5041 #define UPB_UINT64_T unsigned long long
5042 #endif  /* UPB_LONG_IS_64BITS */
5043 
5044 #undef UPB_INT32_MAX
5045 #undef UPB_INT32_MIN
5046 #undef UPB_INT64_MAX
5047 #undef UPB_INT64_MIN
5048 #undef UPB_INT_IS_32BITS
5049 #undef UPB_LONG_IS_32BITS
5050 #undef UPB_LONG_IS_64BITS
5051 #undef UPB_LLONG_IS_64BITS
5052 
5053 
5054 namespace upb {
5055 
5056 typedef void CleanupFunc(void *ptr);
5057 
5058 /* Template to remove "const" from "const T*" and just return "T*".
5059  *
5060  * We define a nonsense default because otherwise it will fail to instantiate as
5061  * a function parameter type even in cases where we don't expect any caller to
5062  * actually match the overload. */
5063 class CouldntRemoveConst {};
5064 template <class T> struct remove_constptr { typedef CouldntRemoveConst type; };
5065 template <class T> struct remove_constptr<const T *> { typedef T *type; };
5066 
5067 /* Template that we use below to remove a template specialization from
5068  * consideration if it matches a specific type. */
5069 template <class T, class U> struct disable_if_same { typedef void Type; };
5070 template <class T> struct disable_if_same<T, T> {};
5071 
5072 template <class T> void DeletePointer(void *p) { delete static_cast<T>(p); }
5073 
5074 template <class T1, class T2>
5075 struct FirstUnlessVoidOrBool {
5076   typedef T1 value;
5077 };
5078 
5079 template <class T2>
5080 struct FirstUnlessVoidOrBool<void, T2> {
5081   typedef T2 value;
5082 };
5083 
5084 template <class T2>
5085 struct FirstUnlessVoidOrBool<bool, T2> {
5086   typedef T2 value;
5087 };
5088 
5089 template<class T, class U>
5090 struct is_same {
5091   static bool value;
5092 };
5093 
5094 template<class T>
5095 struct is_same<T, T> {
5096   static bool value;
5097 };
5098 
5099 template<class T, class U>
5100 bool is_same<T, U>::value = false;
5101 
5102 template<class T>
5103 bool is_same<T, T>::value = true;
5104 
5105 /* FuncInfo *******************************************************************/
5106 
5107 /* Info about the user's original, pre-wrapped function. */
5108 template <class C, class R = void>
5109 struct FuncInfo {
5110   /* The type of the closure that the function takes (its first param). */
5111   typedef C Closure;
5112 
5113   /* The return type. */
5114   typedef R Return;
5115 };
5116 
5117 /* Func ***********************************************************************/
5118 
5119 /* Func1, Func2, Func3: Template classes representing a function and its
5120  * signature.
5121  *
5122  * Since the function is a template parameter, calling the function can be
5123  * inlined at compile-time and does not require a function pointer at runtime.
5124  * These functions are not bound to a handler data so have no data or cleanup
5125  * handler. */
5126 struct UnboundFunc {
5127   CleanupFunc *GetCleanup() { return NULL; }
5128   void *GetData() { return NULL; }
5129 };
5130 
5131 template <class R, class P1, R F(P1), class I>
5132 struct Func1 : public UnboundFunc {
5133   typedef R Return;
5134   typedef I FuncInfo;
5135   static R Call(P1 p1) { return F(p1); }
5136 };
5137 
5138 template <class R, class P1, class P2, R F(P1, P2), class I>
5139 struct Func2 : public UnboundFunc {
5140   typedef R Return;
5141   typedef I FuncInfo;
5142   static R Call(P1 p1, P2 p2) { return F(p1, p2); }
5143 };
5144 
5145 template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I>
5146 struct Func3 : public UnboundFunc {
5147   typedef R Return;
5148   typedef I FuncInfo;
5149   static R Call(P1 p1, P2 p2, P3 p3) { return F(p1, p2, p3); }
5150 };
5151 
5152 template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
5153           class I>
5154 struct Func4 : public UnboundFunc {
5155   typedef R Return;
5156   typedef I FuncInfo;
5157   static R Call(P1 p1, P2 p2, P3 p3, P4 p4) { return F(p1, p2, p3, p4); }
5158 };
5159 
5160 template <class R, class P1, class P2, class P3, class P4, class P5,
5161           R F(P1, P2, P3, P4, P5), class I>
5162 struct Func5 : public UnboundFunc {
5163   typedef R Return;
5164   typedef I FuncInfo;
5165   static R Call(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
5166     return F(p1, p2, p3, p4, p5);
5167   }
5168 };
5169 
5170 /* BoundFunc ******************************************************************/
5171 
5172 /* BoundFunc2, BoundFunc3: Like Func2/Func3 except also contains a value that
5173  * shall be bound to the function's second parameter.
5174  *
5175  * Note that the second parameter is a const pointer, but our stored bound value
5176  * is non-const so we can free it when the handlers are destroyed. */
5177 template <class T>
5178 struct BoundFunc {
5179   typedef typename remove_constptr<T>::type MutableP2;
5180   explicit BoundFunc(MutableP2 data_) : data(data_) {}
5181   CleanupFunc *GetCleanup() { return &DeletePointer<MutableP2>; }
5182   MutableP2 GetData() { return data; }
5183   MutableP2 data;
5184 };
5185 
5186 template <class R, class P1, class P2, R F(P1, P2), class I>
5187 struct BoundFunc2 : public BoundFunc<P2> {
5188   typedef BoundFunc<P2> Base;
5189   typedef I FuncInfo;
5190   explicit BoundFunc2(typename Base::MutableP2 arg) : Base(arg) {}
5191 };
5192 
5193 template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I>
5194 struct BoundFunc3 : public BoundFunc<P2> {
5195   typedef BoundFunc<P2> Base;
5196   typedef I FuncInfo;
5197   explicit BoundFunc3(typename Base::MutableP2 arg) : Base(arg) {}
5198 };
5199 
5200 template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
5201           class I>
5202 struct BoundFunc4 : public BoundFunc<P2> {
5203   typedef BoundFunc<P2> Base;
5204   typedef I FuncInfo;
5205   explicit BoundFunc4(typename Base::MutableP2 arg) : Base(arg) {}
5206 };
5207 
5208 template <class R, class P1, class P2, class P3, class P4, class P5,
5209           R F(P1, P2, P3, P4, P5), class I>
5210 struct BoundFunc5 : public BoundFunc<P2> {
5211   typedef BoundFunc<P2> Base;
5212   typedef I FuncInfo;
5213   explicit BoundFunc5(typename Base::MutableP2 arg) : Base(arg) {}
5214 };
5215 
5216 /* FuncSig ********************************************************************/
5217 
5218 /* FuncSig1, FuncSig2, FuncSig3: template classes reflecting a function
5219  * *signature*, but without a specific function attached.
5220  *
5221  * These classes contain member functions that can be invoked with a
5222  * specific function to return a Func/BoundFunc class. */
5223 template <class R, class P1>
5224 struct FuncSig1 {
5225   template <R F(P1)>
5226   Func1<R, P1, F, FuncInfo<P1, R> > GetFunc() {
5227     return Func1<R, P1, F, FuncInfo<P1, R> >();
5228   }
5229 };
5230 
5231 template <class R, class P1, class P2>
5232 struct FuncSig2 {
5233   template <R F(P1, P2)>
5234   Func2<R, P1, P2, F, FuncInfo<P1, R> > GetFunc() {
5235     return Func2<R, P1, P2, F, FuncInfo<P1, R> >();
5236   }
5237 
5238   template <R F(P1, P2)>
5239   BoundFunc2<R, P1, P2, F, FuncInfo<P1, R> > GetFunc(
5240       typename remove_constptr<P2>::type param2) {
5241     return BoundFunc2<R, P1, P2, F, FuncInfo<P1, R> >(param2);
5242   }
5243 };
5244 
5245 template <class R, class P1, class P2, class P3>
5246 struct FuncSig3 {
5247   template <R F(P1, P2, P3)>
5248   Func3<R, P1, P2, P3, F, FuncInfo<P1, R> > GetFunc() {
5249     return Func3<R, P1, P2, P3, F, FuncInfo<P1, R> >();
5250   }
5251 
5252   template <R F(P1, P2, P3)>
5253   BoundFunc3<R, P1, P2, P3, F, FuncInfo<P1, R> > GetFunc(
5254       typename remove_constptr<P2>::type param2) {
5255     return BoundFunc3<R, P1, P2, P3, F, FuncInfo<P1, R> >(param2);
5256   }
5257 };
5258 
5259 template <class R, class P1, class P2, class P3, class P4>
5260 struct FuncSig4 {
5261   template <R F(P1, P2, P3, P4)>
5262   Func4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> > GetFunc() {
5263     return Func4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> >();
5264   }
5265 
5266   template <R F(P1, P2, P3, P4)>
5267   BoundFunc4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> > GetFunc(
5268       typename remove_constptr<P2>::type param2) {
5269     return BoundFunc4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> >(param2);
5270   }
5271 };
5272 
5273 template <class R, class P1, class P2, class P3, class P4, class P5>
5274 struct FuncSig5 {
5275   template <R F(P1, P2, P3, P4, P5)>
5276   Func5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> > GetFunc() {
5277     return Func5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> >();
5278   }
5279 
5280   template <R F(P1, P2, P3, P4, P5)>
5281   BoundFunc5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> > GetFunc(
5282       typename remove_constptr<P2>::type param2) {
5283     return BoundFunc5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> >(param2);
5284   }
5285 };
5286 
5287 /* Overloaded template function that can construct the appropriate FuncSig*
5288  * class given a function pointer by deducing the template parameters. */
5289 template <class R, class P1>
5290 inline FuncSig1<R, P1> MatchFunc(R (*f)(P1)) {
5291   UPB_UNUSED(f);  /* Only used for template parameter deduction. */
5292   return FuncSig1<R, P1>();
5293 }
5294 
5295 template <class R, class P1, class P2>
5296 inline FuncSig2<R, P1, P2> MatchFunc(R (*f)(P1, P2)) {
5297   UPB_UNUSED(f);  /* Only used for template parameter deduction. */
5298   return FuncSig2<R, P1, P2>();
5299 }
5300 
5301 template <class R, class P1, class P2, class P3>
5302 inline FuncSig3<R, P1, P2, P3> MatchFunc(R (*f)(P1, P2, P3)) {
5303   UPB_UNUSED(f);  /* Only used for template parameter deduction. */
5304   return FuncSig3<R, P1, P2, P3>();
5305 }
5306 
5307 template <class R, class P1, class P2, class P3, class P4>
5308 inline FuncSig4<R, P1, P2, P3, P4> MatchFunc(R (*f)(P1, P2, P3, P4)) {
5309   UPB_UNUSED(f);  /* Only used for template parameter deduction. */
5310   return FuncSig4<R, P1, P2, P3, P4>();
5311 }
5312 
5313 template <class R, class P1, class P2, class P3, class P4, class P5>
5314 inline FuncSig5<R, P1, P2, P3, P4, P5> MatchFunc(R (*f)(P1, P2, P3, P4, P5)) {
5315   UPB_UNUSED(f);  /* Only used for template parameter deduction. */
5316   return FuncSig5<R, P1, P2, P3, P4, P5>();
5317 }
5318 
5319 /* MethodSig ******************************************************************/
5320 
5321 /* CallMethod*: a function template that calls a given method. */
5322 template <class R, class C, R (C::*F)()>
5323 R CallMethod0(C *obj) {
5324   return ((*obj).*F)();
5325 }
5326 
5327 template <class R, class C, class P1, R (C::*F)(P1)>
5328 R CallMethod1(C *obj, P1 arg1) {
5329   return ((*obj).*F)(arg1);
5330 }
5331 
5332 template <class R, class C, class P1, class P2, R (C::*F)(P1, P2)>
5333 R CallMethod2(C *obj, P1 arg1, P2 arg2) {
5334   return ((*obj).*F)(arg1, arg2);
5335 }
5336 
5337 template <class R, class C, class P1, class P2, class P3, R (C::*F)(P1, P2, P3)>
5338 R CallMethod3(C *obj, P1 arg1, P2 arg2, P3 arg3) {
5339   return ((*obj).*F)(arg1, arg2, arg3);
5340 }
5341 
5342 template <class R, class C, class P1, class P2, class P3, class P4,
5343           R (C::*F)(P1, P2, P3, P4)>
5344 R CallMethod4(C *obj, P1 arg1, P2 arg2, P3 arg3, P4 arg4) {
5345   return ((*obj).*F)(arg1, arg2, arg3, arg4);
5346 }
5347 
5348 /* MethodSig: like FuncSig, but for member functions.
5349  *
5350  * GetFunc() returns a normal FuncN object, so after calling GetFunc() no
5351  * more logic is required to special-case methods. */
5352 template <class R, class C>
5353 struct MethodSig0 {
5354   template <R (C::*F)()>
5355   Func1<R, C *, CallMethod0<R, C, F>, FuncInfo<C *, R> > GetFunc() {
5356     return Func1<R, C *, CallMethod0<R, C, F>, FuncInfo<C *, R> >();
5357   }
5358 };
5359 
5360 template <class R, class C, class P1>
5361 struct MethodSig1 {
5362   template <R (C::*F)(P1)>
5363   Func2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> > GetFunc() {
5364     return Func2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> >();
5365   }
5366 
5367   template <R (C::*F)(P1)>
5368   BoundFunc2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> > GetFunc(
5369       typename remove_constptr<P1>::type param1) {
5370     return BoundFunc2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> >(
5371         param1);
5372   }
5373 };
5374 
5375 template <class R, class C, class P1, class P2>
5376 struct MethodSig2 {
5377   template <R (C::*F)(P1, P2)>
5378   Func3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>, FuncInfo<C *, R> >
5379   GetFunc() {
5380     return Func3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>,
5381                  FuncInfo<C *, R> >();
5382   }
5383 
5384   template <R (C::*F)(P1, P2)>
5385   BoundFunc3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>, FuncInfo<C *, R> >
5386   GetFunc(typename remove_constptr<P1>::type param1) {
5387     return BoundFunc3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>,
5388                       FuncInfo<C *, R> >(param1);
5389   }
5390 };
5391 
5392 template <class R, class C, class P1, class P2, class P3>
5393 struct MethodSig3 {
5394   template <R (C::*F)(P1, P2, P3)>
5395   Func4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>, FuncInfo<C *, R> >
5396   GetFunc() {
5397     return Func4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
5398                  FuncInfo<C *, R> >();
5399   }
5400 
5401   template <R (C::*F)(P1, P2, P3)>
5402   BoundFunc4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
5403              FuncInfo<C *, R> >
5404   GetFunc(typename remove_constptr<P1>::type param1) {
5405     return BoundFunc4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
5406                       FuncInfo<C *, R> >(param1);
5407   }
5408 };
5409 
5410 template <class R, class C, class P1, class P2, class P3, class P4>
5411 struct MethodSig4 {
5412   template <R (C::*F)(P1, P2, P3, P4)>
5413   Func5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
5414         FuncInfo<C *, R> >
5415   GetFunc() {
5416     return Func5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
5417                  FuncInfo<C *, R> >();
5418   }
5419 
5420   template <R (C::*F)(P1, P2, P3, P4)>
5421   BoundFunc5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
5422              FuncInfo<C *, R> >
5423   GetFunc(typename remove_constptr<P1>::type param1) {
5424     return BoundFunc5<R, C *, P1, P2, P3, P4,
5425                       CallMethod4<R, C, P1, P2, P3, P4, F>, FuncInfo<C *, R> >(
5426         param1);
5427   }
5428 };
5429 
5430 template <class R, class C>
5431 inline MethodSig0<R, C> MatchFunc(R (C::*f)()) {
5432   UPB_UNUSED(f);  /* Only used for template parameter deduction. */
5433   return MethodSig0<R, C>();
5434 }
5435 
5436 template <class R, class C, class P1>
5437 inline MethodSig1<R, C, P1> MatchFunc(R (C::*f)(P1)) {
5438   UPB_UNUSED(f);  /* Only used for template parameter deduction. */
5439   return MethodSig1<R, C, P1>();
5440 }
5441 
5442 template <class R, class C, class P1, class P2>
5443 inline MethodSig2<R, C, P1, P2> MatchFunc(R (C::*f)(P1, P2)) {
5444   UPB_UNUSED(f);  /* Only used for template parameter deduction. */
5445   return MethodSig2<R, C, P1, P2>();
5446 }
5447 
5448 template <class R, class C, class P1, class P2, class P3>
5449 inline MethodSig3<R, C, P1, P2, P3> MatchFunc(R (C::*f)(P1, P2, P3)) {
5450   UPB_UNUSED(f);  /* Only used for template parameter deduction. */
5451   return MethodSig3<R, C, P1, P2, P3>();
5452 }
5453 
5454 template <class R, class C, class P1, class P2, class P3, class P4>
5455 inline MethodSig4<R, C, P1, P2, P3, P4> MatchFunc(R (C::*f)(P1, P2, P3, P4)) {
5456   UPB_UNUSED(f);  /* Only used for template parameter deduction. */
5457   return MethodSig4<R, C, P1, P2, P3, P4>();
5458 }
5459 
5460 /* MaybeWrapReturn ************************************************************/
5461 
5462 /* Template class that attempts to wrap the return value of the function so it
5463  * matches the expected type.  There are two main adjustments it may make:
5464  *
5465  *   1. If the function returns void, make it return the expected type and with
5466  *      a value that always indicates success.
5467  *   2. If the function returns bool, make it return the expected type with a
5468  *      value that indicates success or failure.
5469  *
5470  * The "expected type" for return is:
5471  *   1. void* for start handlers.  If the closure parameter has a different type
5472  *      we will cast it to void* for the return in the success case.
5473  *   2. size_t for string buffer handlers.
5474  *   3. bool for everything else. */
5475 
5476 /* Template parameters are FuncN type and desired return type. */
5477 template <class F, class R, class Enable = void>
5478 struct MaybeWrapReturn;
5479 
5480 /* If the return type matches, return the given function unwrapped. */
5481 template <class F>
5482 struct MaybeWrapReturn<F, typename F::Return> {
5483   typedef F Func;
5484 };
5485 
5486 /* Function wrapper that munges the return value from void to (bool)true. */
5487 template <class P1, class P2, void F(P1, P2)>
5488 bool ReturnTrue2(P1 p1, P2 p2) {
5489   F(p1, p2);
5490   return true;
5491 }
5492 
5493 template <class P1, class P2, class P3, void F(P1, P2, P3)>
5494 bool ReturnTrue3(P1 p1, P2 p2, P3 p3) {
5495   F(p1, p2, p3);
5496   return true;
5497 }
5498 
5499 /* Function wrapper that munges the return value from void to (void*)arg1  */
5500 template <class P1, class P2, void F(P1, P2)>
5501 void *ReturnClosure2(P1 p1, P2 p2) {
5502   F(p1, p2);
5503   return p1;
5504 }
5505 
5506 template <class P1, class P2, class P3, void F(P1, P2, P3)>
5507 void *ReturnClosure3(P1 p1, P2 p2, P3 p3) {
5508   F(p1, p2, p3);
5509   return p1;
5510 }
5511 
5512 /* Function wrapper that munges the return value from R to void*. */
5513 template <class R, class P1, class P2, R F(P1, P2)>
5514 void *CastReturnToVoidPtr2(P1 p1, P2 p2) {
5515   return F(p1, p2);
5516 }
5517 
5518 template <class R, class P1, class P2, class P3, R F(P1, P2, P3)>
5519 void *CastReturnToVoidPtr3(P1 p1, P2 p2, P3 p3) {
5520   return F(p1, p2, p3);
5521 }
5522 
5523 /* Function wrapper that munges the return value from bool to void*. */
5524 template <class P1, class P2, bool F(P1, P2)>
5525 void *ReturnClosureOrBreak2(P1 p1, P2 p2) {
5526   return F(p1, p2) ? p1 : UPB_BREAK;
5527 }
5528 
5529 template <class P1, class P2, class P3, bool F(P1, P2, P3)>
5530 void *ReturnClosureOrBreak3(P1 p1, P2 p2, P3 p3) {
5531   return F(p1, p2, p3) ? p1 : UPB_BREAK;
5532 }
5533 
5534 /* For the string callback, which takes five params, returns the size param. */
5535 template <class P1, class P2,
5536           void F(P1, P2, const char *, size_t, const BufferHandle *)>
5537 size_t ReturnStringLen(P1 p1, P2 p2, const char *p3, size_t p4,
5538                        const BufferHandle *p5) {
5539   F(p1, p2, p3, p4, p5);
5540   return p4;
5541 }
5542 
5543 /* For the string callback, which takes five params, returns the size param or
5544  * zero. */
5545 template <class P1, class P2,
5546           bool F(P1, P2, const char *, size_t, const BufferHandle *)>
5547 size_t ReturnNOr0(P1 p1, P2 p2, const char *p3, size_t p4,
5548                   const BufferHandle *p5) {
5549   return F(p1, p2, p3, p4, p5) ? p4 : 0;
5550 }
5551 
5552 /* If we have a function returning void but want a function returning bool, wrap
5553  * it in a function that returns true. */
5554 template <class P1, class P2, void F(P1, P2), class I>
5555 struct MaybeWrapReturn<Func2<void, P1, P2, F, I>, bool> {
5556   typedef Func2<bool, P1, P2, ReturnTrue2<P1, P2, F>, I> Func;
5557 };
5558 
5559 template <class P1, class P2, class P3, void F(P1, P2, P3), class I>
5560 struct MaybeWrapReturn<Func3<void, P1, P2, P3, F, I>, bool> {
5561   typedef Func3<bool, P1, P2, P3, ReturnTrue3<P1, P2, P3, F>, I> Func;
5562 };
5563 
5564 /* If our function returns void but we want one returning void*, wrap it in a
5565  * function that returns the first argument. */
5566 template <class P1, class P2, void F(P1, P2), class I>
5567 struct MaybeWrapReturn<Func2<void, P1, P2, F, I>, void *> {
5568   typedef Func2<void *, P1, P2, ReturnClosure2<P1, P2, F>, I> Func;
5569 };
5570 
5571 template <class P1, class P2, class P3, void F(P1, P2, P3), class I>
5572 struct MaybeWrapReturn<Func3<void, P1, P2, P3, F, I>, void *> {
5573   typedef Func3<void *, P1, P2, P3, ReturnClosure3<P1, P2, P3, F>, I> Func;
5574 };
5575 
5576 /* If our function returns R* but we want one returning void*, wrap it in a
5577  * function that casts to void*. */
5578 template <class R, class P1, class P2, R *F(P1, P2), class I>
5579 struct MaybeWrapReturn<Func2<R *, P1, P2, F, I>, void *,
5580                        typename disable_if_same<R *, void *>::Type> {
5581   typedef Func2<void *, P1, P2, CastReturnToVoidPtr2<R *, P1, P2, F>, I> Func;
5582 };
5583 
5584 template <class R, class P1, class P2, class P3, R *F(P1, P2, P3), class I>
5585 struct MaybeWrapReturn<Func3<R *, P1, P2, P3, F, I>, void *,
5586                        typename disable_if_same<R *, void *>::Type> {
5587   typedef Func3<void *, P1, P2, P3, CastReturnToVoidPtr3<R *, P1, P2, P3, F>, I>
5588       Func;
5589 };
5590 
5591 /* If our function returns bool but we want one returning void*, wrap it in a
5592  * function that returns either the first param or UPB_BREAK. */
5593 template <class P1, class P2, bool F(P1, P2), class I>
5594 struct MaybeWrapReturn<Func2<bool, P1, P2, F, I>, void *> {
5595   typedef Func2<void *, P1, P2, ReturnClosureOrBreak2<P1, P2, F>, I> Func;
5596 };
5597 
5598 template <class P1, class P2, class P3, bool F(P1, P2, P3), class I>
5599 struct MaybeWrapReturn<Func3<bool, P1, P2, P3, F, I>, void *> {
5600   typedef Func3<void *, P1, P2, P3, ReturnClosureOrBreak3<P1, P2, P3, F>, I>
5601       Func;
5602 };
5603 
5604 /* If our function returns void but we want one returning size_t, wrap it in a
5605  * function that returns the size argument. */
5606 template <class P1, class P2,
5607           void F(P1, P2, const char *, size_t, const BufferHandle *), class I>
5608 struct MaybeWrapReturn<
5609     Func5<void, P1, P2, const char *, size_t, const BufferHandle *, F, I>,
5610           size_t> {
5611   typedef Func5<size_t, P1, P2, const char *, size_t, const BufferHandle *,
5612                 ReturnStringLen<P1, P2, F>, I> Func;
5613 };
5614 
5615 /* If our function returns bool but we want one returning size_t, wrap it in a
5616  * function that returns either 0 or the buf size. */
5617 template <class P1, class P2,
5618           bool F(P1, P2, const char *, size_t, const BufferHandle *), class I>
5619 struct MaybeWrapReturn<
5620     Func5<bool, P1, P2, const char *, size_t, const BufferHandle *, F, I>,
5621     size_t> {
5622   typedef Func5<size_t, P1, P2, const char *, size_t, const BufferHandle *,
5623                 ReturnNOr0<P1, P2, F>, I> Func;
5624 };
5625 
5626 /* ConvertParams **************************************************************/
5627 
5628 /* Template class that converts the function parameters if necessary, and
5629  * ignores the HandlerData parameter if appropriate.
5630  *
5631  * Template parameter is the are FuncN function type. */
5632 template <class F, class T>
5633 struct ConvertParams;
5634 
5635 /* Function that discards the handler data parameter. */
5636 template <class R, class P1, R F(P1)>
5637 R IgnoreHandlerData2(void *p1, const void *hd) {
5638   UPB_UNUSED(hd);
5639   return F(static_cast<P1>(p1));
5640 }
5641 
5642 template <class R, class P1, class P2Wrapper, class P2Wrapped,
5643           R F(P1, P2Wrapped)>
5644 R IgnoreHandlerData3(void *p1, const void *hd, P2Wrapper p2) {
5645   UPB_UNUSED(hd);
5646   return F(static_cast<P1>(p1), p2);
5647 }
5648 
5649 template <class R, class P1, class P2, class P3, R F(P1, P2, P3)>
5650 R IgnoreHandlerData4(void *p1, const void *hd, P2 p2, P3 p3) {
5651   UPB_UNUSED(hd);
5652   return F(static_cast<P1>(p1), p2, p3);
5653 }
5654 
5655 template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4)>
5656 R IgnoreHandlerData5(void *p1, const void *hd, P2 p2, P3 p3, P4 p4) {
5657   UPB_UNUSED(hd);
5658   return F(static_cast<P1>(p1), p2, p3, p4);
5659 }
5660 
5661 template <class R, class P1, R F(P1, const char*, size_t)>
5662 R IgnoreHandlerDataIgnoreHandle(void *p1, const void *hd, const char *p2,
5663                                 size_t p3, const BufferHandle *handle) {
5664   UPB_UNUSED(hd);
5665   UPB_UNUSED(handle);
5666   return F(static_cast<P1>(p1), p2, p3);
5667 }
5668 
5669 /* Function that casts the handler data parameter. */
5670 template <class R, class P1, class P2, R F(P1, P2)>
5671 R CastHandlerData2(void *c, const void *hd) {
5672   return F(static_cast<P1>(c), static_cast<P2>(hd));
5673 }
5674 
5675 template <class R, class P1, class P2, class P3Wrapper, class P3Wrapped,
5676           R F(P1, P2, P3Wrapped)>
5677 R CastHandlerData3(void *c, const void *hd, P3Wrapper p3) {
5678   return F(static_cast<P1>(c), static_cast<P2>(hd), p3);
5679 }
5680 
5681 template <class R, class P1, class P2, class P3, class P4, class P5,
5682           R F(P1, P2, P3, P4, P5)>
5683 R CastHandlerData5(void *c, const void *hd, P3 p3, P4 p4, P5 p5) {
5684   return F(static_cast<P1>(c), static_cast<P2>(hd), p3, p4, p5);
5685 }
5686 
5687 template <class R, class P1, class P2, R F(P1, P2, const char *, size_t)>
5688 R CastHandlerDataIgnoreHandle(void *c, const void *hd, const char *p3,
5689                               size_t p4, const BufferHandle *handle) {
5690   UPB_UNUSED(handle);
5691   return F(static_cast<P1>(c), static_cast<P2>(hd), p3, p4);
5692 }
5693 
5694 /* For unbound functions, ignore the handler data. */
5695 template <class R, class P1, R F(P1), class I, class T>
5696 struct ConvertParams<Func1<R, P1, F, I>, T> {
5697   typedef Func2<R, void *, const void *, IgnoreHandlerData2<R, P1, F>, I> Func;
5698 };
5699 
5700 template <class R, class P1, class P2, R F(P1, P2), class I,
5701           class R2, class P1_2, class P2_2, class P3_2>
5702 struct ConvertParams<Func2<R, P1, P2, F, I>,
5703                      R2 (*)(P1_2, P2_2, P3_2)> {
5704   typedef Func3<R, void *, const void *, P3_2,
5705                 IgnoreHandlerData3<R, P1, P3_2, P2, F>, I> Func;
5706 };
5707 
5708 /* For StringBuffer only; this ignores both the handler data and the
5709  * BufferHandle. */
5710 template <class R, class P1, R F(P1, const char *, size_t), class I, class T>
5711 struct ConvertParams<Func3<R, P1, const char *, size_t, F, I>, T> {
5712   typedef Func5<R, void *, const void *, const char *, size_t,
5713                 const BufferHandle *, IgnoreHandlerDataIgnoreHandle<R, P1, F>,
5714                 I> Func;
5715 };
5716 
5717 template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
5718           class I, class T>
5719 struct ConvertParams<Func4<R, P1, P2, P3, P4, F, I>, T> {
5720   typedef Func5<R, void *, const void *, P2, P3, P4,
5721                 IgnoreHandlerData5<R, P1, P2, P3, P4, F>, I> Func;
5722 };
5723 
5724 /* For bound functions, cast the handler data. */
5725 template <class R, class P1, class P2, R F(P1, P2), class I, class T>
5726 struct ConvertParams<BoundFunc2<R, P1, P2, F, I>, T> {
5727   typedef Func2<R, void *, const void *, CastHandlerData2<R, P1, P2, F>, I>
5728       Func;
5729 };
5730 
5731 template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I,
5732           class R2, class P1_2, class P2_2, class P3_2>
5733 struct ConvertParams<BoundFunc3<R, P1, P2, P3, F, I>,
5734                      R2 (*)(P1_2, P2_2, P3_2)> {
5735   typedef Func3<R, void *, const void *, P3_2,
5736                 CastHandlerData3<R, P1, P2, P3_2, P3, F>, I> Func;
5737 };
5738 
5739 /* For StringBuffer only; this ignores the BufferHandle. */
5740 template <class R, class P1, class P2, R F(P1, P2, const char *, size_t),
5741           class I, class T>
5742 struct ConvertParams<BoundFunc4<R, P1, P2, const char *, size_t, F, I>, T> {
5743   typedef Func5<R, void *, const void *, const char *, size_t,
5744                 const BufferHandle *, CastHandlerDataIgnoreHandle<R, P1, P2, F>,
5745                 I> Func;
5746 };
5747 
5748 template <class R, class P1, class P2, class P3, class P4, class P5,
5749           R F(P1, P2, P3, P4, P5), class I, class T>
5750 struct ConvertParams<BoundFunc5<R, P1, P2, P3, P4, P5, F, I>, T> {
5751   typedef Func5<R, void *, const void *, P3, P4, P5,
5752                 CastHandlerData5<R, P1, P2, P3, P4, P5, F>, I> Func;
5753 };
5754 
5755 /* utype/ltype are upper/lower-case, ctype is canonical C type, vtype is
5756  * variant C type. */
5757 #define TYPE_METHODS(utype, ltype, ctype, vtype)                               \
5758   template <> struct CanonicalType<vtype> {                                    \
5759     typedef ctype Type;                                                        \
5760   };                                                                           \
5761   template <>                                                                  \
5762   inline bool Handlers::SetValueHandler<vtype>(                                \
5763       const FieldDef *f,                                                       \
5764       const Handlers::utype ## Handler& handler) {                             \
5765     UPB_ASSERT(!handler.registered_);                                              \
5766     handler.AddCleanup(this);                                                  \
5767     handler.registered_ = true;                                                \
5768     return upb_handlers_set##ltype(this, f, handler.handler_, &handler.attr_); \
5769   }                                                                            \
5770 
5771 TYPE_METHODS(Double, double, double,   double)
5772 TYPE_METHODS(Float,  float,  float,    float)
5773 TYPE_METHODS(UInt64, uint64, uint64_t, UPB_UINT64_T)
5774 TYPE_METHODS(UInt32, uint32, uint32_t, UPB_UINT32_T)
5775 TYPE_METHODS(Int64,  int64,  int64_t,  UPB_INT64_T)
5776 TYPE_METHODS(Int32,  int32,  int32_t,  UPB_INT32_T)
5777 TYPE_METHODS(Bool,   bool,   bool,     bool)
5778 
5779 #ifdef UPB_TWO_32BIT_TYPES
5780 TYPE_METHODS(Int32,  int32,  int32_t,  UPB_INT32ALT_T)
5781 TYPE_METHODS(UInt32, uint32, uint32_t, UPB_UINT32ALT_T)
5782 #endif
5783 
5784 #ifdef UPB_TWO_64BIT_TYPES
5785 TYPE_METHODS(Int64,  int64,  int64_t,  UPB_INT64ALT_T)
5786 TYPE_METHODS(UInt64, uint64, uint64_t, UPB_UINT64ALT_T)
5787 #endif
5788 #undef TYPE_METHODS
5789 
5790 template <> struct CanonicalType<Status*> {
5791   typedef Status* Type;
5792 };
5793 
5794 /* Type methods that are only one-per-canonical-type and not
5795  * one-per-cvariant. */
5796 
5797 #define TYPE_METHODS(utype, ctype) \
5798     inline bool Handlers::Set##utype##Handler(const FieldDef *f, \
5799                                               const utype##Handler &h) { \
5800       return SetValueHandler<ctype>(f, h); \
5801     } \
5802 
5803 TYPE_METHODS(Double, double)
5804 TYPE_METHODS(Float,  float)
5805 TYPE_METHODS(UInt64, uint64_t)
5806 TYPE_METHODS(UInt32, uint32_t)
5807 TYPE_METHODS(Int64,  int64_t)
5808 TYPE_METHODS(Int32,  int32_t)
5809 TYPE_METHODS(Bool,   bool)
5810 #undef TYPE_METHODS
5811 
5812 template <class F> struct ReturnOf;
5813 
5814 template <class R, class P1, class P2>
5815 struct ReturnOf<R (*)(P1, P2)> {
5816   typedef R Return;
5817 };
5818 
5819 template <class R, class P1, class P2, class P3>
5820 struct ReturnOf<R (*)(P1, P2, P3)> {
5821   typedef R Return;
5822 };
5823 
5824 template <class R, class P1, class P2, class P3, class P4>
5825 struct ReturnOf<R (*)(P1, P2, P3, P4)> {
5826   typedef R Return;
5827 };
5828 
5829 template <class R, class P1, class P2, class P3, class P4, class P5>
5830 struct ReturnOf<R (*)(P1, P2, P3, P4, P5)> {
5831   typedef R Return;
5832 };
5833 
5834 template<class T> const void *UniquePtrForType() {
5835   static const char ch = 0;
5836   return &ch;
5837 }
5838 
5839 template <class T>
5840 template <class F>
5841 inline Handler<T>::Handler(F func)
5842     : registered_(false),
5843       cleanup_data_(func.GetData()),
5844       cleanup_func_(func.GetCleanup()) {
5845   upb_handlerattr_sethandlerdata(&attr_, func.GetData());
5846   typedef typename ReturnOf<T>::Return Return;
5847   typedef typename ConvertParams<F, T>::Func ConvertedParamsFunc;
5848   typedef typename MaybeWrapReturn<ConvertedParamsFunc, Return>::Func
5849       ReturnWrappedFunc;
5850   handler_ = ReturnWrappedFunc().Call;
5851 
5852   /* Set attributes based on what templates can statically tell us about the
5853    * user's function. */
5854 
5855   /* If the original function returns void, then we know that we wrapped it to
5856    * always return ok. */
5857   bool always_ok = is_same<typename F::FuncInfo::Return, void>::value;
5858   attr_.SetAlwaysOk(always_ok);
5859 
5860   /* Closure parameter and return type. */
5861   attr_.SetClosureType(UniquePtrForType<typename F::FuncInfo::Closure>());
5862 
5863   /* We use the closure type (from the first parameter) if the return type is
5864    * void or bool, since these are the two cases we wrap to return the closure's
5865    * type anyway.
5866    *
5867    * This is all nonsense for non START* handlers, but it doesn't matter because
5868    * in that case the value will be ignored. */
5869   typedef typename FirstUnlessVoidOrBool<typename F::FuncInfo::Return,
5870                                          typename F::FuncInfo::Closure>::value
5871       EffectiveReturn;
5872   attr_.SetReturnClosureType(UniquePtrForType<EffectiveReturn>());
5873 }
5874 
5875 template <class T>
5876 inline Handler<T>::~Handler() {
5877   UPB_ASSERT(registered_);
5878 }
5879 
5880 inline HandlerAttributes::HandlerAttributes() { upb_handlerattr_init(this); }
5881 inline HandlerAttributes::~HandlerAttributes() { upb_handlerattr_uninit(this); }
5882 inline bool HandlerAttributes::SetHandlerData(const void *hd) {
5883   return upb_handlerattr_sethandlerdata(this, hd);
5884 }
5885 inline const void* HandlerAttributes::handler_data() const {
5886   return upb_handlerattr_handlerdata(this);
5887 }
5888 inline bool HandlerAttributes::SetClosureType(const void *type) {
5889   return upb_handlerattr_setclosuretype(this, type);
5890 }
5891 inline const void* HandlerAttributes::closure_type() const {
5892   return upb_handlerattr_closuretype(this);
5893 }
5894 inline bool HandlerAttributes::SetReturnClosureType(const void *type) {
5895   return upb_handlerattr_setreturnclosuretype(this, type);
5896 }
5897 inline const void* HandlerAttributes::return_closure_type() const {
5898   return upb_handlerattr_returnclosuretype(this);
5899 }
5900 inline bool HandlerAttributes::SetAlwaysOk(bool always_ok) {
5901   return upb_handlerattr_setalwaysok(this, always_ok);
5902 }
5903 inline bool HandlerAttributes::always_ok() const {
5904   return upb_handlerattr_alwaysok(this);
5905 }
5906 
5907 inline BufferHandle::BufferHandle() { upb_bufhandle_init(this); }
5908 inline BufferHandle::~BufferHandle() { upb_bufhandle_uninit(this); }
5909 inline const char* BufferHandle::buffer() const {
5910   return upb_bufhandle_buf(this);
5911 }
5912 inline size_t BufferHandle::object_offset() const {
5913   return upb_bufhandle_objofs(this);
5914 }
5915 inline void BufferHandle::SetBuffer(const char* buf, size_t ofs) {
5916   upb_bufhandle_setbuf(this, buf, ofs);
5917 }
5918 template <class T>
5919 void BufferHandle::SetAttachedObject(const T* obj) {
5920   upb_bufhandle_setobj(this, obj, UniquePtrForType<T>());
5921 }
5922 template <class T>
5923 const T* BufferHandle::GetAttachedObject() const {
5924   return upb_bufhandle_objtype(this) == UniquePtrForType<T>()
5925       ? static_cast<const T *>(upb_bufhandle_obj(this))
5926                                : NULL;
5927 }
5928 
5929 inline reffed_ptr<Handlers> Handlers::New(const MessageDef *m) {
5930   upb_handlers *h = upb_handlers_new(m, &h);
5931   return reffed_ptr<Handlers>(h, &h);
5932 }
5933 inline reffed_ptr<const Handlers> Handlers::NewFrozen(
5934     const MessageDef *m, upb_handlers_callback *callback,
5935     const void *closure) {
5936   const upb_handlers *h = upb_handlers_newfrozen(m, &h, callback, closure);
5937   return reffed_ptr<const Handlers>(h, &h);
5938 }
5939 inline const Status* Handlers::status() {
5940   return upb_handlers_status(this);
5941 }
5942 inline void Handlers::ClearError() {
5943   return upb_handlers_clearerr(this);
5944 }
5945 inline bool Handlers::Freeze(Status *s) {
5946   upb::Handlers* h = this;
5947   return upb_handlers_freeze(&h, 1, s);
5948 }
5949 inline bool Handlers::Freeze(Handlers *const *handlers, int n, Status *s) {
5950   return upb_handlers_freeze(handlers, n, s);
5951 }
5952 inline bool Handlers::Freeze(const std::vector<Handlers*>& h, Status* status) {
5953   return upb_handlers_freeze((Handlers* const*)&h[0], h.size(), status);
5954 }
5955 inline const MessageDef *Handlers::message_def() const {
5956   return upb_handlers_msgdef(this);
5957 }
5958 inline bool Handlers::AddCleanup(void *p, upb_handlerfree *func) {
5959   return upb_handlers_addcleanup(this, p, func);
5960 }
5961 inline bool Handlers::SetStartMessageHandler(
5962     const Handlers::StartMessageHandler &handler) {
5963   UPB_ASSERT(!handler.registered_);
5964   handler.registered_ = true;
5965   handler.AddCleanup(this);
5966   return upb_handlers_setstartmsg(this, handler.handler_, &handler.attr_);
5967 }
5968 inline bool Handlers::SetEndMessageHandler(
5969     const Handlers::EndMessageHandler &handler) {
5970   UPB_ASSERT(!handler.registered_);
5971   handler.registered_ = true;
5972   handler.AddCleanup(this);
5973   return upb_handlers_setendmsg(this, handler.handler_, &handler.attr_);
5974 }
5975 inline bool Handlers::SetStartStringHandler(const FieldDef *f,
5976                                             const StartStringHandler &handler) {
5977   UPB_ASSERT(!handler.registered_);
5978   handler.registered_ = true;
5979   handler.AddCleanup(this);
5980   return upb_handlers_setstartstr(this, f, handler.handler_, &handler.attr_);
5981 }
5982 inline bool Handlers::SetEndStringHandler(const FieldDef *f,
5983                                           const EndFieldHandler &handler) {
5984   UPB_ASSERT(!handler.registered_);
5985   handler.registered_ = true;
5986   handler.AddCleanup(this);
5987   return upb_handlers_setendstr(this, f, handler.handler_, &handler.attr_);
5988 }
5989 inline bool Handlers::SetStringHandler(const FieldDef *f,
5990                                        const StringHandler& handler) {
5991   UPB_ASSERT(!handler.registered_);
5992   handler.registered_ = true;
5993   handler.AddCleanup(this);
5994   return upb_handlers_setstring(this, f, handler.handler_, &handler.attr_);
5995 }
5996 inline bool Handlers::SetStartSequenceHandler(
5997     const FieldDef *f, const StartFieldHandler &handler) {
5998   UPB_ASSERT(!handler.registered_);
5999   handler.registered_ = true;
6000   handler.AddCleanup(this);
6001   return upb_handlers_setstartseq(this, f, handler.handler_, &handler.attr_);
6002 }
6003 inline bool Handlers::SetStartSubMessageHandler(
6004     const FieldDef *f, const StartFieldHandler &handler) {
6005   UPB_ASSERT(!handler.registered_);
6006   handler.registered_ = true;
6007   handler.AddCleanup(this);
6008   return upb_handlers_setstartsubmsg(this, f, handler.handler_, &handler.attr_);
6009 }
6010 inline bool Handlers::SetEndSubMessageHandler(const FieldDef *f,
6011                                               const EndFieldHandler &handler) {
6012   UPB_ASSERT(!handler.registered_);
6013   handler.registered_ = true;
6014   handler.AddCleanup(this);
6015   return upb_handlers_setendsubmsg(this, f, handler.handler_, &handler.attr_);
6016 }
6017 inline bool Handlers::SetEndSequenceHandler(const FieldDef *f,
6018                                             const EndFieldHandler &handler) {
6019   UPB_ASSERT(!handler.registered_);
6020   handler.registered_ = true;
6021   handler.AddCleanup(this);
6022   return upb_handlers_setendseq(this, f, handler.handler_, &handler.attr_);
6023 }
6024 inline bool Handlers::SetSubHandlers(const FieldDef *f, const Handlers *sub) {
6025   return upb_handlers_setsubhandlers(this, f, sub);
6026 }
6027 inline const Handlers *Handlers::GetSubHandlers(const FieldDef *f) const {
6028   return upb_handlers_getsubhandlers(this, f);
6029 }
6030 inline const Handlers *Handlers::GetSubHandlers(Handlers::Selector sel) const {
6031   return upb_handlers_getsubhandlers_sel(this, sel);
6032 }
6033 inline bool Handlers::GetSelector(const FieldDef *f, Handlers::Type type,
6034                                   Handlers::Selector *s) {
6035   return upb_handlers_getselector(f, type, s);
6036 }
6037 inline Handlers::Selector Handlers::GetEndSelector(Handlers::Selector start) {
6038   return upb_handlers_getendselector(start);
6039 }
6040 inline Handlers::GenericFunction *Handlers::GetHandler(
6041     Handlers::Selector selector) {
6042   return upb_handlers_gethandler(this, selector);
6043 }
6044 inline const void *Handlers::GetHandlerData(Handlers::Selector selector) {
6045   return upb_handlers_gethandlerdata(this, selector);
6046 }
6047 
6048 inline BytesHandler::BytesHandler() {
6049   upb_byteshandler_init(this);
6050 }
6051 
6052 inline BytesHandler::~BytesHandler() {}
6053 
6054 }  /* namespace upb */
6055 
6056 #endif  /* __cplusplus */
6057 
6058 
6059 #undef UPB_TWO_32BIT_TYPES
6060 #undef UPB_TWO_64BIT_TYPES
6061 #undef UPB_INT32_T
6062 #undef UPB_UINT32_T
6063 #undef UPB_INT32ALT_T
6064 #undef UPB_UINT32ALT_T
6065 #undef UPB_INT64_T
6066 #undef UPB_UINT64_T
6067 #undef UPB_INT64ALT_T
6068 #undef UPB_UINT64ALT_T
6069 
6070 #endif  /* UPB_HANDLERS_INL_H_ */
6071 
6072 #endif  /* UPB_HANDLERS_H */
6073 /*
6074 ** upb::Sink (upb_sink)
6075 ** upb::BytesSink (upb_bytessink)
6076 **
6077 ** A upb_sink is an object that binds a upb_handlers object to some runtime
6078 ** state.  It is the object that can actually receive data via the upb_handlers
6079 ** interface.
6080 **
6081 ** Unlike upb_def and upb_handlers, upb_sink is never frozen, immutable, or
6082 ** thread-safe.  You can create as many of them as you want, but each one may
6083 ** only be used in a single thread at a time.
6084 **
6085 ** If we compare with class-based OOP, a you can think of a upb_def as an
6086 ** abstract base class, a upb_handlers as a concrete derived class, and a
6087 ** upb_sink as an object (class instance).
6088 */
6089 
6090 #ifndef UPB_SINK_H
6091 #define UPB_SINK_H
6092 
6093 
6094 #ifdef __cplusplus
6095 namespace upb {
6096 class BufferSink;
6097 class BufferSource;
6098 class BytesSink;
6099 class Sink;
6100 }
6101 #endif
6102 
6103 UPB_DECLARE_TYPE(upb::BufferSink, upb_bufsink)
6104 UPB_DECLARE_TYPE(upb::BufferSource, upb_bufsrc)
6105 UPB_DECLARE_TYPE(upb::BytesSink, upb_bytessink)
6106 UPB_DECLARE_TYPE(upb::Sink, upb_sink)
6107 
6108 #ifdef __cplusplus
6109 
6110 /* A upb::Sink is an object that binds a upb::Handlers object to some runtime
6111  * state.  It represents an endpoint to which data can be sent.
6112  *
6113  * TODO(haberman): right now all of these functions take selectors.  Should they
6114  * take selectorbase instead?
6115  *
6116  * ie. instead of calling:
6117  *   sink->StartString(FOO_FIELD_START_STRING, ...)
6118  * a selector base would let you say:
6119  *   sink->StartString(FOO_FIELD, ...)
6120  *
6121  * This would make call sites a little nicer and require emitting fewer selector
6122  * definitions in .h files.
6123  *
6124  * But the current scheme has the benefit that you can retrieve a function
6125  * pointer for any handler with handlers->GetHandler(selector), without having
6126  * to have a separate GetHandler() function for each handler type.  The JIT
6127  * compiler uses this.  To accommodate we'd have to expose a separate
6128  * GetHandler() for every handler type.
6129  *
6130  * Also to ponder: selectors right now are independent of a specific Handlers
6131  * instance.  In other words, they allocate a number to every possible handler
6132  * that *could* be registered, without knowing anything about what handlers
6133  * *are* registered.  That means that using selectors as table offsets prohibits
6134  * us from compacting the handler table at Freeze() time.  If the table is very
6135  * sparse, this could be wasteful.
6136  *
6137  * Having another selector-like thing that is specific to a Handlers instance
6138  * would allow this compacting, but then it would be impossible to write code
6139  * ahead-of-time that can be bound to any Handlers instance at runtime.  For
6140  * example, a .proto file parser written as straight C will not know what
6141  * Handlers it will be bound to, so when it calls sink->StartString() what
6142  * selector will it pass?  It needs a selector like we have today, that is
6143  * independent of any particular upb::Handlers.
6144  *
6145  * Is there a way then to allow Handlers table compaction? */
6146 class upb::Sink {
6147  public:
6148   /* Constructor with no initialization; must be Reset() before use. */
6149   Sink() {}
6150 
6151   /* Constructs a new sink for the given frozen handlers and closure.
6152    *
6153    * TODO: once the Handlers know the expected closure type, verify that T
6154    * matches it. */
6155   template <class T> Sink(const Handlers* handlers, T* closure);
6156 
6157   /* Resets the value of the sink. */
6158   template <class T> void Reset(const Handlers* handlers, T* closure);
6159 
6160   /* Returns the top-level object that is bound to this sink.
6161    *
6162    * TODO: once the Handlers know the expected closure type, verify that T
6163    * matches it. */
6164   template <class T> T* GetObject() const;
6165 
6166   /* Functions for pushing data into the sink.
6167    *
6168    * These return false if processing should stop (either due to error or just
6169    * to suspend).
6170    *
6171    * These may not be called from within one of the same sink's handlers (in
6172    * other words, handlers are not re-entrant). */
6173 
6174   /* Should be called at the start and end of every message; both the top-level
6175    * message and submessages.  This means that submessages should use the
6176    * following sequence:
6177    *   sink->StartSubMessage(startsubmsg_selector);
6178    *   sink->StartMessage();
6179    *   // ...
6180    *   sink->EndMessage(&status);
6181    *   sink->EndSubMessage(endsubmsg_selector); */
6182   bool StartMessage();
6183   bool EndMessage(Status* status);
6184 
6185   /* Putting of individual values.  These work for both repeated and
6186    * non-repeated fields, but for repeated fields you must wrap them in
6187    * calls to StartSequence()/EndSequence(). */
6188   bool PutInt32(Handlers::Selector s, int32_t val);
6189   bool PutInt64(Handlers::Selector s, int64_t val);
6190   bool PutUInt32(Handlers::Selector s, uint32_t val);
6191   bool PutUInt64(Handlers::Selector s, uint64_t val);
6192   bool PutFloat(Handlers::Selector s, float val);
6193   bool PutDouble(Handlers::Selector s, double val);
6194   bool PutBool(Handlers::Selector s, bool val);
6195 
6196   /* Putting of string/bytes values.  Each string can consist of zero or more
6197    * non-contiguous buffers of data.
6198    *
6199    * For StartString(), the function will write a sink for the string to "sub."
6200    * The sub-sink must be used for any/all PutStringBuffer() calls. */
6201   bool StartString(Handlers::Selector s, size_t size_hint, Sink* sub);
6202   size_t PutStringBuffer(Handlers::Selector s, const char *buf, size_t len,
6203                          const BufferHandle *handle);
6204   bool EndString(Handlers::Selector s);
6205 
6206   /* For submessage fields.
6207    *
6208    * For StartSubMessage(), the function will write a sink for the string to
6209    * "sub." The sub-sink must be used for any/all handlers called within the
6210    * submessage. */
6211   bool StartSubMessage(Handlers::Selector s, Sink* sub);
6212   bool EndSubMessage(Handlers::Selector s);
6213 
6214   /* For repeated fields of any type, the sequence of values must be wrapped in
6215    * these calls.
6216    *
6217    * For StartSequence(), the function will write a sink for the string to
6218    * "sub." The sub-sink must be used for any/all handlers called within the
6219    * sequence. */
6220   bool StartSequence(Handlers::Selector s, Sink* sub);
6221   bool EndSequence(Handlers::Selector s);
6222 
6223   /* Copy and assign specifically allowed.
6224    * We don't even bother making these members private because so many
6225    * functions need them and this is mainly just a dumb data container anyway.
6226    */
6227 #else
6228 struct upb_sink {
6229 #endif
6230   const upb_handlers *handlers;
6231   void *closure;
6232 };
6233 
6234 #ifdef __cplusplus
6235 class upb::BytesSink {
6236  public:
6237   BytesSink() {}
6238 
6239   /* Constructs a new sink for the given frozen handlers and closure.
6240    *
6241    * TODO(haberman): once the Handlers know the expected closure type, verify
6242    * that T matches it. */
6243   template <class T> BytesSink(const BytesHandler* handler, T* closure);
6244 
6245   /* Resets the value of the sink. */
6246   template <class T> void Reset(const BytesHandler* handler, T* closure);
6247 
6248   bool Start(size_t size_hint, void **subc);
6249   size_t PutBuffer(void *subc, const char *buf, size_t len,
6250                    const BufferHandle *handle);
6251   bool End();
6252 #else
6253 struct upb_bytessink {
6254 #endif
6255   const upb_byteshandler *handler;
6256   void *closure;
6257 };
6258 
6259 #ifdef __cplusplus
6260 
6261 /* A class for pushing a flat buffer of data to a BytesSink.
6262  * You can construct an instance of this to get a resumable source,
6263  * or just call the static PutBuffer() to do a non-resumable push all in one
6264  * go. */
6265 class upb::BufferSource {
6266  public:
6267   BufferSource();
6268   BufferSource(const char* buf, size_t len, BytesSink* sink);
6269 
6270   /* Returns true if the entire buffer was pushed successfully.  Otherwise the
6271    * next call to PutNext() will resume where the previous one left off.
6272    * TODO(haberman): implement this. */
6273   bool PutNext();
6274 
6275   /* A static version; with this version is it not possible to resume in the
6276    * case of failure or a partially-consumed buffer. */
6277   static bool PutBuffer(const char* buf, size_t len, BytesSink* sink);
6278 
6279   template <class T> static bool PutBuffer(const T& str, BytesSink* sink) {
6280     return PutBuffer(str.c_str(), str.size(), sink);
6281   }
6282 #else
6283 struct upb_bufsrc {
6284   char dummy;
6285 #endif
6286 };
6287 
6288 UPB_BEGIN_EXTERN_C
6289 
6290 /* A class for accumulating output string data in a flat buffer. */
6291 
6292 upb_bufsink *upb_bufsink_new(upb_env *env);
6293 void upb_bufsink_free(upb_bufsink *sink);
6294 upb_bytessink *upb_bufsink_sink(upb_bufsink *sink);
6295 const char *upb_bufsink_getdata(const upb_bufsink *sink, size_t *len);
6296 
6297 /* Inline definitions. */
6298 
6299 UPB_INLINE void upb_bytessink_reset(upb_bytessink *s, const upb_byteshandler *h,
6300                                     void *closure) {
6301   s->handler = h;
6302   s->closure = closure;
6303 }
6304 
6305 UPB_INLINE bool upb_bytessink_start(upb_bytessink *s, size_t size_hint,
6306                                     void **subc) {
6307   typedef upb_startstr_handlerfunc func;
6308   func *start;
6309   *subc = s->closure;
6310   if (!s->handler) return true;
6311   start = (func *)s->handler->table[UPB_STARTSTR_SELECTOR].func;
6312 
6313   if (!start) return true;
6314   *subc = start(s->closure, upb_handlerattr_handlerdata(
6315                                 &s->handler->table[UPB_STARTSTR_SELECTOR].attr),
6316                 size_hint);
6317   return *subc != NULL;
6318 }
6319 
6320 UPB_INLINE size_t upb_bytessink_putbuf(upb_bytessink *s, void *subc,
6321                                        const char *buf, size_t size,
6322                                        const upb_bufhandle* handle) {
6323   typedef upb_string_handlerfunc func;
6324   func *putbuf;
6325   if (!s->handler) return true;
6326   putbuf = (func *)s->handler->table[UPB_STRING_SELECTOR].func;
6327 
6328   if (!putbuf) return true;
6329   return putbuf(subc, upb_handlerattr_handlerdata(
6330                           &s->handler->table[UPB_STRING_SELECTOR].attr),
6331                 buf, size, handle);
6332 }
6333 
6334 UPB_INLINE bool upb_bytessink_end(upb_bytessink *s) {
6335   typedef upb_endfield_handlerfunc func;
6336   func *end;
6337   if (!s->handler) return true;
6338   end = (func *)s->handler->table[UPB_ENDSTR_SELECTOR].func;
6339 
6340   if (!end) return true;
6341   return end(s->closure,
6342              upb_handlerattr_handlerdata(
6343                  &s->handler->table[UPB_ENDSTR_SELECTOR].attr));
6344 }
6345 
6346 bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink *sink);
6347 
6348 #define PUTVAL(type, ctype)                                                    \
6349   UPB_INLINE bool upb_sink_put##type(upb_sink *s, upb_selector_t sel,          \
6350                                      ctype val) {                              \
6351     typedef upb_##type##_handlerfunc functype;                                 \
6352     functype *func;                                                            \
6353     const void *hd;                                                            \
6354     if (!s->handlers) return true;                                             \
6355     func = (functype *)upb_handlers_gethandler(s->handlers, sel);              \
6356     if (!func) return true;                                                    \
6357     hd = upb_handlers_gethandlerdata(s->handlers, sel);                        \
6358     return func(s->closure, hd, val);                                          \
6359   }
6360 
6361 PUTVAL(int32,  int32_t)
6362 PUTVAL(int64,  int64_t)
6363 PUTVAL(uint32, uint32_t)
6364 PUTVAL(uint64, uint64_t)
6365 PUTVAL(float,  float)
6366 PUTVAL(double, double)
6367 PUTVAL(bool,   bool)
6368 #undef PUTVAL
6369 
6370 UPB_INLINE void upb_sink_reset(upb_sink *s, const upb_handlers *h, void *c) {
6371   s->handlers = h;
6372   s->closure = c;
6373 }
6374 
6375 UPB_INLINE size_t upb_sink_putstring(upb_sink *s, upb_selector_t sel,
6376                                      const char *buf, size_t n,
6377                                      const upb_bufhandle *handle) {
6378   typedef upb_string_handlerfunc func;
6379   func *handler;
6380   const void *hd;
6381   if (!s->handlers) return n;
6382   handler = (func *)upb_handlers_gethandler(s->handlers, sel);
6383 
6384   if (!handler) return n;
6385   hd = upb_handlers_gethandlerdata(s->handlers, sel);
6386   return handler(s->closure, hd, buf, n, handle);
6387 }
6388 
6389 UPB_INLINE bool upb_sink_putunknown(upb_sink *s, const char *buf, size_t n) {
6390   typedef upb_unknown_handlerfunc func;
6391   func *handler;
6392   const void *hd;
6393   if (!s->handlers) return true;
6394   handler = (func *)upb_handlers_gethandler(s->handlers, UPB_UNKNOWN_SELECTOR);
6395 
6396   if (!handler) return n;
6397   hd = upb_handlers_gethandlerdata(s->handlers, UPB_UNKNOWN_SELECTOR);
6398   return handler(s->closure, hd, buf, n);
6399 }
6400 
6401 UPB_INLINE bool upb_sink_startmsg(upb_sink *s) {
6402   typedef upb_startmsg_handlerfunc func;
6403   func *startmsg;
6404   const void *hd;
6405   if (!s->handlers) return true;
6406   startmsg = (func*)upb_handlers_gethandler(s->handlers, UPB_STARTMSG_SELECTOR);
6407 
6408   if (!startmsg) return true;
6409   hd = upb_handlers_gethandlerdata(s->handlers, UPB_STARTMSG_SELECTOR);
6410   return startmsg(s->closure, hd);
6411 }
6412 
6413 UPB_INLINE bool upb_sink_endmsg(upb_sink *s, upb_status *status) {
6414   typedef upb_endmsg_handlerfunc func;
6415   func *endmsg;
6416   const void *hd;
6417   if (!s->handlers) return true;
6418   endmsg = (func *)upb_handlers_gethandler(s->handlers, UPB_ENDMSG_SELECTOR);
6419 
6420   if (!endmsg) return true;
6421   hd = upb_handlers_gethandlerdata(s->handlers, UPB_ENDMSG_SELECTOR);
6422   return endmsg(s->closure, hd, status);
6423 }
6424 
6425 UPB_INLINE bool upb_sink_startseq(upb_sink *s, upb_selector_t sel,
6426                                   upb_sink *sub) {
6427   typedef upb_startfield_handlerfunc func;
6428   func *startseq;
6429   const void *hd;
6430   sub->closure = s->closure;
6431   sub->handlers = s->handlers;
6432   if (!s->handlers) return true;
6433   startseq = (func*)upb_handlers_gethandler(s->handlers, sel);
6434 
6435   if (!startseq) return true;
6436   hd = upb_handlers_gethandlerdata(s->handlers, sel);
6437   sub->closure = startseq(s->closure, hd);
6438   return sub->closure ? true : false;
6439 }
6440 
6441 UPB_INLINE bool upb_sink_endseq(upb_sink *s, upb_selector_t sel) {
6442   typedef upb_endfield_handlerfunc func;
6443   func *endseq;
6444   const void *hd;
6445   if (!s->handlers) return true;
6446   endseq = (func*)upb_handlers_gethandler(s->handlers, sel);
6447 
6448   if (!endseq) return true;
6449   hd = upb_handlers_gethandlerdata(s->handlers, sel);
6450   return endseq(s->closure, hd);
6451 }
6452 
6453 UPB_INLINE bool upb_sink_startstr(upb_sink *s, upb_selector_t sel,
6454                                   size_t size_hint, upb_sink *sub) {
6455   typedef upb_startstr_handlerfunc func;
6456   func *startstr;
6457   const void *hd;
6458   sub->closure = s->closure;
6459   sub->handlers = s->handlers;
6460   if (!s->handlers) return true;
6461   startstr = (func*)upb_handlers_gethandler(s->handlers, sel);
6462 
6463   if (!startstr) return true;
6464   hd = upb_handlers_gethandlerdata(s->handlers, sel);
6465   sub->closure = startstr(s->closure, hd, size_hint);
6466   return sub->closure ? true : false;
6467 }
6468 
6469 UPB_INLINE bool upb_sink_endstr(upb_sink *s, upb_selector_t sel) {
6470   typedef upb_endfield_handlerfunc func;
6471   func *endstr;
6472   const void *hd;
6473   if (!s->handlers) return true;
6474   endstr = (func*)upb_handlers_gethandler(s->handlers, sel);
6475 
6476   if (!endstr) return true;
6477   hd = upb_handlers_gethandlerdata(s->handlers, sel);
6478   return endstr(s->closure, hd);
6479 }
6480 
6481 UPB_INLINE bool upb_sink_startsubmsg(upb_sink *s, upb_selector_t sel,
6482                                      upb_sink *sub) {
6483   typedef upb_startfield_handlerfunc func;
6484   func *startsubmsg;
6485   const void *hd;
6486   sub->closure = s->closure;
6487   if (!s->handlers) {
6488     sub->handlers = NULL;
6489     return true;
6490   }
6491   sub->handlers = upb_handlers_getsubhandlers_sel(s->handlers, sel);
6492   startsubmsg = (func*)upb_handlers_gethandler(s->handlers, sel);
6493 
6494   if (!startsubmsg) return true;
6495   hd = upb_handlers_gethandlerdata(s->handlers, sel);
6496   sub->closure = startsubmsg(s->closure, hd);
6497   return sub->closure ? true : false;
6498 }
6499 
6500 UPB_INLINE bool upb_sink_endsubmsg(upb_sink *s, upb_selector_t sel) {
6501   typedef upb_endfield_handlerfunc func;
6502   func *endsubmsg;
6503   const void *hd;
6504   if (!s->handlers) return true;
6505   endsubmsg = (func*)upb_handlers_gethandler(s->handlers, sel);
6506 
6507   if (!endsubmsg) return s->closure;
6508   hd = upb_handlers_gethandlerdata(s->handlers, sel);
6509   return endsubmsg(s->closure, hd);
6510 }
6511 
6512 UPB_END_EXTERN_C
6513 
6514 #ifdef __cplusplus
6515 
6516 namespace upb {
6517 
6518 template <class T> Sink::Sink(const Handlers* handlers, T* closure) {
6519   upb_sink_reset(this, handlers, closure);
6520 }
6521 template <class T>
6522 inline void Sink::Reset(const Handlers* handlers, T* closure) {
6523   upb_sink_reset(this, handlers, closure);
6524 }
6525 inline bool Sink::StartMessage() {
6526   return upb_sink_startmsg(this);
6527 }
6528 inline bool Sink::EndMessage(Status* status) {
6529   return upb_sink_endmsg(this, status);
6530 }
6531 inline bool Sink::PutInt32(Handlers::Selector sel, int32_t val) {
6532   return upb_sink_putint32(this, sel, val);
6533 }
6534 inline bool Sink::PutInt64(Handlers::Selector sel, int64_t val) {
6535   return upb_sink_putint64(this, sel, val);
6536 }
6537 inline bool Sink::PutUInt32(Handlers::Selector sel, uint32_t val) {
6538   return upb_sink_putuint32(this, sel, val);
6539 }
6540 inline bool Sink::PutUInt64(Handlers::Selector sel, uint64_t val) {
6541   return upb_sink_putuint64(this, sel, val);
6542 }
6543 inline bool Sink::PutFloat(Handlers::Selector sel, float val) {
6544   return upb_sink_putfloat(this, sel, val);
6545 }
6546 inline bool Sink::PutDouble(Handlers::Selector sel, double val) {
6547   return upb_sink_putdouble(this, sel, val);
6548 }
6549 inline bool Sink::PutBool(Handlers::Selector sel, bool val) {
6550   return upb_sink_putbool(this, sel, val);
6551 }
6552 inline bool Sink::StartString(Handlers::Selector sel, size_t size_hint,
6553                               Sink *sub) {
6554   return upb_sink_startstr(this, sel, size_hint, sub);
6555 }
6556 inline size_t Sink::PutStringBuffer(Handlers::Selector sel, const char *buf,
6557                                     size_t len, const BufferHandle* handle) {
6558   return upb_sink_putstring(this, sel, buf, len, handle);
6559 }
6560 inline bool Sink::EndString(Handlers::Selector sel) {
6561   return upb_sink_endstr(this, sel);
6562 }
6563 inline bool Sink::StartSubMessage(Handlers::Selector sel, Sink* sub) {
6564   return upb_sink_startsubmsg(this, sel, sub);
6565 }
6566 inline bool Sink::EndSubMessage(Handlers::Selector sel) {
6567   return upb_sink_endsubmsg(this, sel);
6568 }
6569 inline bool Sink::StartSequence(Handlers::Selector sel, Sink* sub) {
6570   return upb_sink_startseq(this, sel, sub);
6571 }
6572 inline bool Sink::EndSequence(Handlers::Selector sel) {
6573   return upb_sink_endseq(this, sel);
6574 }
6575 
6576 template <class T>
6577 BytesSink::BytesSink(const BytesHandler* handler, T* closure) {
6578   Reset(handler, closure);
6579 }
6580 
6581 template <class T>
6582 void BytesSink::Reset(const BytesHandler *handler, T *closure) {
6583   upb_bytessink_reset(this, handler, closure);
6584 }
6585 inline bool BytesSink::Start(size_t size_hint, void **subc) {
6586   return upb_bytessink_start(this, size_hint, subc);
6587 }
6588 inline size_t BytesSink::PutBuffer(void *subc, const char *buf, size_t len,
6589                                    const BufferHandle *handle) {
6590   return upb_bytessink_putbuf(this, subc, buf, len, handle);
6591 }
6592 inline bool BytesSink::End() {
6593   return upb_bytessink_end(this);
6594 }
6595 
6596 inline bool BufferSource::PutBuffer(const char *buf, size_t len,
6597                                     BytesSink *sink) {
6598   return upb_bufsrc_putbuf(buf, len, sink);
6599 }
6600 
6601 }  /* namespace upb */
6602 #endif
6603 
6604 #endif
6605 
6606 #ifdef __cplusplus
6607 
6608 namespace upb {
6609 class Array;
6610 class Map;
6611 class MapIterator;
6612 class MessageLayout;
6613 }
6614 
6615 #endif
6616 
6617 UPB_DECLARE_TYPE(upb::Map, upb_map)
6618 UPB_DECLARE_TYPE(upb::MapIterator, upb_mapiter)
6619 
6620 struct upb_array;
6621 typedef struct upb_array upb_array;
6622 
6623 /* TODO(haberman): C++ accessors */
6624 
6625 UPB_BEGIN_EXTERN_C
6626 
6627 typedef void upb_msg;
6628 
6629 
6630 /** upb_msglayout *************************************************************/
6631 
6632 /* upb_msglayout represents the memory layout of a given upb_msgdef.  The
6633  * members are public so generated code can initialize them, but users MUST NOT
6634  * read or write any of its members. */
6635 
6636 typedef struct {
6637   uint32_t number;
6638   uint16_t offset;
6639   int16_t presence;      /* If >0, hasbit_index+1.  If <0, oneof_index+1. */
6640   uint16_t submsg_index;  /* undefined if descriptortype != MESSAGE or GROUP. */
6641   uint8_t descriptortype;
6642   uint8_t label;
6643 } upb_msglayout_field;
6644 
6645 typedef struct upb_msglayout {
6646   const struct upb_msglayout *const* submsgs;
6647   const upb_msglayout_field *fields;
6648   /* Must be aligned to sizeof(void*).  Doesn't include internal members like
6649    * unknown fields, extension dict, pointer to msglayout, etc. */
6650   uint16_t size;
6651   uint16_t field_count;
6652   bool extendable;
6653 } upb_msglayout;
6654 
6655 
6656 /** upb_strview ************************************************************/
6657 
6658 typedef struct {
6659   const char *data;
6660   size_t size;
6661 } upb_strview;
6662 
6663 UPB_INLINE upb_strview upb_strview_make(const char *data, size_t size) {
6664   upb_strview ret;
6665   ret.data = data;
6666   ret.size = size;
6667   return ret;
6668 }
6669 
6670 #define UPB_STRVIEW_INIT(ptr, len) {ptr, len}
6671 
6672 
6673 /** upb_msgval ****************************************************************/
6674 
6675 /* A union representing all possible protobuf values.  Used for generic get/set
6676  * operations. */
6677 
6678 typedef union {
6679   bool b;
6680   float flt;
6681   double dbl;
6682   int32_t i32;
6683   int64_t i64;
6684   uint32_t u32;
6685   uint64_t u64;
6686   const upb_map* map;
6687   const upb_msg* msg;
6688   const upb_array* arr;
6689   const void* ptr;
6690   upb_strview str;
6691 } upb_msgval;
6692 
6693 #define ACCESSORS(name, membername, ctype) \
6694   UPB_INLINE ctype upb_msgval_get ## name(upb_msgval v) { \
6695     return v.membername; \
6696   } \
6697   UPB_INLINE void upb_msgval_set ## name(upb_msgval *v, ctype cval) { \
6698     v->membername = cval; \
6699   } \
6700   UPB_INLINE upb_msgval upb_msgval_ ## name(ctype v) { \
6701     upb_msgval ret; \
6702     ret.membername = v; \
6703     return ret; \
6704   }
6705 
6706 ACCESSORS(bool,   b,   bool)
6707 ACCESSORS(float,  flt, float)
6708 ACCESSORS(double, dbl, double)
6709 ACCESSORS(int32,  i32, int32_t)
6710 ACCESSORS(int64,  i64, int64_t)
6711 ACCESSORS(uint32, u32, uint32_t)
6712 ACCESSORS(uint64, u64, uint64_t)
6713 ACCESSORS(map,    map, const upb_map*)
6714 ACCESSORS(msg,    msg, const upb_msg*)
6715 ACCESSORS(ptr,    ptr, const void*)
6716 ACCESSORS(arr,    arr, const upb_array*)
6717 ACCESSORS(str,    str, upb_strview)
6718 
6719 #undef ACCESSORS
6720 
6721 UPB_INLINE upb_msgval upb_msgval_makestr(const char *data, size_t size) {
6722   return upb_msgval_str(upb_strview_make(data, size));
6723 }
6724 
6725 
6726 /** upb_msg *******************************************************************/
6727 
6728 /* A upb_msg represents a protobuf message.  It always corresponds to a specific
6729  * upb_msglayout, which describes how it is laid out in memory.  */
6730 
6731 /* Creates a new message of the given type/layout in this arena. */
6732 upb_msg *upb_msg_new(const upb_msglayout *l, upb_arena *a);
6733 
6734 /* Returns the arena for the given message. */
6735 upb_arena *upb_msg_arena(const upb_msg *msg);
6736 
6737 void upb_msg_addunknown(upb_msg *msg, const char *data, size_t len);
6738 const char *upb_msg_getunknown(const upb_msg *msg, size_t *len);
6739 
6740 /* Read-only message API.  Can be safely called by anyone. */
6741 
6742 /* Returns the value associated with this field:
6743  *   - for scalar fields (including strings), the value directly.
6744  *   - return upb_msg*, or upb_map* for msg/map.
6745  *     If the field is unset for these field types, returns NULL.
6746  *
6747  * TODO(haberman): should we let users store cached array/map/msg
6748  * pointers here for fields that are unset?  Could be useful for the
6749  * strongly-owned submessage model (ie. generated C API that doesn't use
6750  * arenas).
6751  */
6752 upb_msgval upb_msg_get(const upb_msg *msg,
6753                        int field_index,
6754                        const upb_msglayout *l);
6755 
6756 /* May only be called for fields where upb_fielddef_haspresence(f) == true. */
6757 bool upb_msg_has(const upb_msg *msg,
6758                  int field_index,
6759                  const upb_msglayout *l);
6760 
6761 /* Mutable message API.  May only be called by the owner of the message who
6762  * knows its ownership scheme and how to keep it consistent. */
6763 
6764 /* Sets the given field to the given value.  Does not perform any memory
6765  * management: if you overwrite a pointer to a msg/array/map/string without
6766  * cleaning it up (or using an arena) it will leak.
6767  */
6768 void upb_msg_set(upb_msg *msg,
6769                  int field_index,
6770                  upb_msgval val,
6771                  const upb_msglayout *l);
6772 
6773 /* For a primitive field, set it back to its default. For repeated, string, and
6774  * submessage fields set it back to NULL.  This could involve releasing some
6775  * internal memory (for example, from an extension dictionary), but it is not
6776  * recursive in any way and will not recover any memory that may be used by
6777  * arrays/maps/strings/msgs that this field may have pointed to.
6778  */
6779 bool upb_msg_clearfield(upb_msg *msg,
6780                         int field_index,
6781                         const upb_msglayout *l);
6782 
6783 /* TODO(haberman): copyfrom()/mergefrom()? */
6784 
6785 
6786 /** upb_array *****************************************************************/
6787 
6788 /* A upb_array stores data for a repeated field.  The memory management
6789  * semantics are the same as upb_msg.  A upb_array allocates dynamic
6790  * memory internally for the array elements. */
6791 
6792 upb_array *upb_array_new(upb_fieldtype_t type, upb_arena *a);
6793 upb_fieldtype_t upb_array_type(const upb_array *arr);
6794 
6795 /* Read-only interface.  Safe for anyone to call. */
6796 
6797 size_t upb_array_size(const upb_array *arr);
6798 upb_msgval upb_array_get(const upb_array *arr, size_t i);
6799 
6800 /* Write interface.  May only be called by the message's owner who can enforce
6801  * its memory management invariants. */
6802 
6803 bool upb_array_set(upb_array *arr, size_t i, upb_msgval val);
6804 
6805 
6806 /** upb_map *******************************************************************/
6807 
6808 /* A upb_map stores data for a map field.  The memory management semantics are
6809  * the same as upb_msg, with one notable exception.  upb_map will internally
6810  * store a copy of all string keys, but *not* any string values or submessages.
6811  * So you must ensure that any string or message values outlive the map, and you
6812  * must delete them manually when they are no longer required. */
6813 
6814 upb_map *upb_map_new(upb_fieldtype_t ktype, upb_fieldtype_t vtype,
6815                      upb_arena *a);
6816 
6817 /* Read-only interface.  Safe for anyone to call. */
6818 
6819 size_t upb_map_size(const upb_map *map);
6820 upb_fieldtype_t upb_map_keytype(const upb_map *map);
6821 upb_fieldtype_t upb_map_valuetype(const upb_map *map);
6822 bool upb_map_get(const upb_map *map, upb_msgval key, upb_msgval *val);
6823 
6824 /* Write interface.  May only be called by the message's owner who can enforce
6825  * its memory management invariants. */
6826 
6827 /* Sets or overwrites an entry in the map.  Return value indicates whether
6828  * the operation succeeded or failed with OOM, and also whether an existing
6829  * key was replaced or not. */
6830 bool upb_map_set(upb_map *map,
6831                  upb_msgval key, upb_msgval val,
6832                  upb_msgval *valremoved);
6833 
6834 /* Deletes an entry in the map.  Returns true if the key was present. */
6835 bool upb_map_del(upb_map *map, upb_msgval key);
6836 
6837 
6838 /** upb_mapiter ***************************************************************/
6839 
6840 /* For iterating over a map.  Map iterators are invalidated by mutations to the
6841  * map, but an invalidated iterator will never return junk or crash the process.
6842  * An invalidated iterator may return entries that were already returned though,
6843  * and if you keep invalidating the iterator during iteration, the program may
6844  * enter an infinite loop. */
6845 
6846 size_t upb_mapiter_sizeof();
6847 
6848 void upb_mapiter_begin(upb_mapiter *i, const upb_map *t);
6849 upb_mapiter *upb_mapiter_new(const upb_map *t, upb_alloc *a);
6850 void upb_mapiter_free(upb_mapiter *i, upb_alloc *a);
6851 void upb_mapiter_next(upb_mapiter *i);
6852 bool upb_mapiter_done(const upb_mapiter *i);
6853 
6854 upb_msgval upb_mapiter_key(const upb_mapiter *i);
6855 upb_msgval upb_mapiter_value(const upb_mapiter *i);
6856 void upb_mapiter_setdone(upb_mapiter *i);
6857 bool upb_mapiter_isequal(const upb_mapiter *i1, const upb_mapiter *i2);
6858 
6859 UPB_END_EXTERN_C
6860 
6861 #endif /* UPB_MSG_H_ */
6862 /* This file was generated by upbc (the upb compiler) from the input
6863  * file:
6864  *
6865  *     google/protobuf/descriptor.proto
6866  *
6867  * Do not edit -- your changes will be discarded when the file is
6868  * regenerated. */
6869 
6870 #ifndef GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_
6871 #define GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_
6872 
6873 /*
6874 ** Functions for use by generated code.  These are not public and users must
6875 ** not call them directly.
6876 */
6877 
6878 #ifndef UPB_GENERATED_UTIL_H_
6879 #define UPB_GENERATED_UTIL_H_
6880 
6881 #include <stdint.h>
6882 /*
6883 ** structs.int.h: structures definitions that are internal to upb.
6884 */
6885 
6886 #ifndef UPB_STRUCTS_H_
6887 #define UPB_STRUCTS_H_
6888 
6889 
6890 struct upb_array {
6891   upb_fieldtype_t type;
6892   uint8_t element_size;
6893   void *data;   /* Each element is element_size. */
6894   size_t len;   /* Measured in elements. */
6895   size_t size;  /* Measured in elements. */
6896   upb_arena *arena;
6897 };
6898 
6899 #endif  /* UPB_STRUCTS_H_ */
6900 
6901 
6902 #define PTR_AT(msg, ofs, type) (type*)((const char*)msg + ofs)
6903 
6904 UPB_INLINE const void *_upb_array_accessor(const void *msg, size_t ofs,
6905                                            size_t *size) {
6906   const upb_array *arr = *PTR_AT(msg, ofs, const upb_array*);
6907   if (arr) {
6908     if (size) *size = arr->size;
6909     return arr->data;
6910   } else {
6911     if (size) *size = 0;
6912     return NULL;
6913   }
6914 }
6915 
6916 UPB_INLINE void *_upb_array_mutable_accessor(void *msg, size_t ofs,
6917                                              size_t *size) {
6918   upb_array *arr = *PTR_AT(msg, ofs, upb_array*);
6919   if (arr) {
6920     if (size) *size = arr->size;
6921     return arr->data;
6922   } else {
6923     if (size) *size = 0;
6924     return NULL;
6925   }
6926 }
6927 
6928 /* TODO(haberman): this is a mess.  It will improve when upb_array no longer
6929  * carries reflective state (type, elem_size). */
6930 UPB_INLINE void *_upb_array_resize_accessor(void *msg, size_t ofs, size_t size,
6931                                             size_t elem_size,
6932                                             upb_fieldtype_t type,
6933                                             upb_arena *arena) {
6934   upb_array *arr = *PTR_AT(msg, ofs, upb_array*);
6935 
6936   if (!arr) {
6937     arr = upb_array_new(type, arena);
6938     if (!arr) return NULL;
6939     *PTR_AT(msg, ofs, upb_array*) = arr;
6940   }
6941 
6942   if (size > arr->size) {
6943     size_t new_size = UPB_MAX(arr->size, 4);
6944     size_t old_bytes = arr->size * elem_size;
6945     size_t new_bytes;
6946     upb_alloc *alloc = upb_arena_alloc(arr->arena);
6947     while (new_size < size) new_size *= 2;
6948     new_bytes = new_size * elem_size;
6949     arr->data = upb_realloc(alloc, arr->data, old_bytes, new_bytes);
6950     if (!arr->data) {
6951       return NULL;
6952     }
6953     arr->size = new_size;
6954   }
6955 
6956   arr->len = size;
6957   return arr->data;
6958 }
6959 
6960 UPB_INLINE bool _upb_array_append_accessor(void *msg, size_t ofs,
6961                                            size_t elem_size,
6962                                            upb_fieldtype_t type,
6963                                            const void *value,
6964                                            upb_arena *arena) {
6965   upb_array *arr = *PTR_AT(msg, ofs, upb_array*);
6966   size_t i = arr ? arr->len : 0;
6967   void *data =
6968       _upb_array_resize_accessor(msg, ofs, i + 1, elem_size, type, arena);
6969   if (!data) return false;
6970   memcpy(PTR_AT(data, i * elem_size, char), value, elem_size);
6971   return true;
6972 }
6973 
6974 UPB_INLINE bool _upb_has_field(const void *msg, size_t idx) {
6975   return (*PTR_AT(msg, idx / 8, const char) & (idx % 8)) != 0;
6976 }
6977 
6978 UPB_INLINE bool _upb_sethas(const void *msg, size_t idx) {
6979   return (*PTR_AT(msg, idx / 8, char)) |= (1 << (idx % 8));
6980 }
6981 
6982 UPB_INLINE bool _upb_clearhas(const void *msg, size_t idx) {
6983   return (*PTR_AT(msg, idx / 8, char)) &= ~(1 << (idx % 8));
6984 }
6985 
6986 UPB_INLINE bool _upb_has_oneof_field(const void *msg, size_t case_ofs, int32_t num) {
6987   return *PTR_AT(msg, case_ofs, int32_t) == num;
6988 }
6989 
6990 #undef PTR_AT
6991 
6992 #endif  /* UPB_GENERATED_UTIL_H_ */
6993 
6994 
6995 /*
6996 ** upb_decode: parsing into a upb_msg using a upb_msglayout.
6997 */
6998 
6999 #ifndef UPB_DECODE_H_
7000 #define UPB_DECODE_H_
7001 
7002 
7003 UPB_BEGIN_EXTERN_C
7004 
7005 bool upb_decode(upb_strview buf, upb_msg *msg, const upb_msglayout *l);
7006 
7007 UPB_END_EXTERN_C
7008 
7009 #endif  /* UPB_DECODE_H_ */
7010 /*
7011 ** upb_encode: parsing into a upb_msg using a upb_msglayout.
7012 */
7013 
7014 #ifndef UPB_ENCODE_H_
7015 #define UPB_ENCODE_H_
7016 
7017 
7018 UPB_BEGIN_EXTERN_C
7019 
7020 char *upb_encode(const void *msg, const upb_msglayout *l, upb_arena *arena,
7021                  size_t *size);
7022 
7023 UPB_END_EXTERN_C
7024 
7025 #endif  /* UPB_ENCODE_H_ */
7026 UPB_BEGIN_EXTERN_C
7027 
7028 struct google_protobuf_FileDescriptorSet;
7029 struct google_protobuf_FileDescriptorProto;
7030 struct google_protobuf_DescriptorProto;
7031 struct google_protobuf_DescriptorProto_ExtensionRange;
7032 struct google_protobuf_DescriptorProto_ReservedRange;
7033 struct google_protobuf_ExtensionRangeOptions;
7034 struct google_protobuf_FieldDescriptorProto;
7035 struct google_protobuf_OneofDescriptorProto;
7036 struct google_protobuf_EnumDescriptorProto;
7037 struct google_protobuf_EnumDescriptorProto_EnumReservedRange;
7038 struct google_protobuf_EnumValueDescriptorProto;
7039 struct google_protobuf_ServiceDescriptorProto;
7040 struct google_protobuf_MethodDescriptorProto;
7041 struct google_protobuf_FileOptions;
7042 struct google_protobuf_MessageOptions;
7043 struct google_protobuf_FieldOptions;
7044 struct google_protobuf_OneofOptions;
7045 struct google_protobuf_EnumOptions;
7046 struct google_protobuf_EnumValueOptions;
7047 struct google_protobuf_ServiceOptions;
7048 struct google_protobuf_MethodOptions;
7049 struct google_protobuf_UninterpretedOption;
7050 struct google_protobuf_UninterpretedOption_NamePart;
7051 struct google_protobuf_SourceCodeInfo;
7052 struct google_protobuf_SourceCodeInfo_Location;
7053 struct google_protobuf_GeneratedCodeInfo;
7054 struct google_protobuf_GeneratedCodeInfo_Annotation;
7055 typedef struct google_protobuf_FileDescriptorSet google_protobuf_FileDescriptorSet;
7056 typedef struct google_protobuf_FileDescriptorProto google_protobuf_FileDescriptorProto;
7057 typedef struct google_protobuf_DescriptorProto google_protobuf_DescriptorProto;
7058 typedef struct google_protobuf_DescriptorProto_ExtensionRange google_protobuf_DescriptorProto_ExtensionRange;
7059 typedef struct google_protobuf_DescriptorProto_ReservedRange google_protobuf_DescriptorProto_ReservedRange;
7060 typedef struct google_protobuf_ExtensionRangeOptions google_protobuf_ExtensionRangeOptions;
7061 typedef struct google_protobuf_FieldDescriptorProto google_protobuf_FieldDescriptorProto;
7062 typedef struct google_protobuf_OneofDescriptorProto google_protobuf_OneofDescriptorProto;
7063 typedef struct google_protobuf_EnumDescriptorProto google_protobuf_EnumDescriptorProto;
7064 typedef struct google_protobuf_EnumDescriptorProto_EnumReservedRange google_protobuf_EnumDescriptorProto_EnumReservedRange;
7065 typedef struct google_protobuf_EnumValueDescriptorProto google_protobuf_EnumValueDescriptorProto;
7066 typedef struct google_protobuf_ServiceDescriptorProto google_protobuf_ServiceDescriptorProto;
7067 typedef struct google_protobuf_MethodDescriptorProto google_protobuf_MethodDescriptorProto;
7068 typedef struct google_protobuf_FileOptions google_protobuf_FileOptions;
7069 typedef struct google_protobuf_MessageOptions google_protobuf_MessageOptions;
7070 typedef struct google_protobuf_FieldOptions google_protobuf_FieldOptions;
7071 typedef struct google_protobuf_OneofOptions google_protobuf_OneofOptions;
7072 typedef struct google_protobuf_EnumOptions google_protobuf_EnumOptions;
7073 typedef struct google_protobuf_EnumValueOptions google_protobuf_EnumValueOptions;
7074 typedef struct google_protobuf_ServiceOptions google_protobuf_ServiceOptions;
7075 typedef struct google_protobuf_MethodOptions google_protobuf_MethodOptions;
7076 typedef struct google_protobuf_UninterpretedOption google_protobuf_UninterpretedOption;
7077 typedef struct google_protobuf_UninterpretedOption_NamePart google_protobuf_UninterpretedOption_NamePart;
7078 typedef struct google_protobuf_SourceCodeInfo google_protobuf_SourceCodeInfo;
7079 typedef struct google_protobuf_SourceCodeInfo_Location google_protobuf_SourceCodeInfo_Location;
7080 typedef struct google_protobuf_GeneratedCodeInfo google_protobuf_GeneratedCodeInfo;
7081 typedef struct google_protobuf_GeneratedCodeInfo_Annotation google_protobuf_GeneratedCodeInfo_Annotation;
7082 extern const upb_msglayout google_protobuf_FileDescriptorSet_msginit;
7083 extern const upb_msglayout google_protobuf_FileDescriptorProto_msginit;
7084 extern const upb_msglayout google_protobuf_DescriptorProto_msginit;
7085 extern const upb_msglayout google_protobuf_DescriptorProto_ExtensionRange_msginit;
7086 extern const upb_msglayout google_protobuf_DescriptorProto_ReservedRange_msginit;
7087 extern const upb_msglayout google_protobuf_ExtensionRangeOptions_msginit;
7088 extern const upb_msglayout google_protobuf_FieldDescriptorProto_msginit;
7089 extern const upb_msglayout google_protobuf_OneofDescriptorProto_msginit;
7090 extern const upb_msglayout google_protobuf_EnumDescriptorProto_msginit;
7091 extern const upb_msglayout google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit;
7092 extern const upb_msglayout google_protobuf_EnumValueDescriptorProto_msginit;
7093 extern const upb_msglayout google_protobuf_ServiceDescriptorProto_msginit;
7094 extern const upb_msglayout google_protobuf_MethodDescriptorProto_msginit;
7095 extern const upb_msglayout google_protobuf_FileOptions_msginit;
7096 extern const upb_msglayout google_protobuf_MessageOptions_msginit;
7097 extern const upb_msglayout google_protobuf_FieldOptions_msginit;
7098 extern const upb_msglayout google_protobuf_OneofOptions_msginit;
7099 extern const upb_msglayout google_protobuf_EnumOptions_msginit;
7100 extern const upb_msglayout google_protobuf_EnumValueOptions_msginit;
7101 extern const upb_msglayout google_protobuf_ServiceOptions_msginit;
7102 extern const upb_msglayout google_protobuf_MethodOptions_msginit;
7103 extern const upb_msglayout google_protobuf_UninterpretedOption_msginit;
7104 extern const upb_msglayout google_protobuf_UninterpretedOption_NamePart_msginit;
7105 extern const upb_msglayout google_protobuf_SourceCodeInfo_msginit;
7106 extern const upb_msglayout google_protobuf_SourceCodeInfo_Location_msginit;
7107 extern const upb_msglayout google_protobuf_GeneratedCodeInfo_msginit;
7108 extern const upb_msglayout google_protobuf_GeneratedCodeInfo_Annotation_msginit;
7109 
7110 /* Enums */
7111 
7112 typedef enum {
7113   google_protobuf_FieldDescriptorProto_LABEL_OPTIONAL = 1,
7114   google_protobuf_FieldDescriptorProto_LABEL_REQUIRED = 2,
7115   google_protobuf_FieldDescriptorProto_LABEL_REPEATED = 3
7116 } google_protobuf_FieldDescriptorProto_Label;
7117 
7118 typedef enum {
7119   google_protobuf_FieldDescriptorProto_TYPE_DOUBLE = 1,
7120   google_protobuf_FieldDescriptorProto_TYPE_FLOAT = 2,
7121   google_protobuf_FieldDescriptorProto_TYPE_INT64 = 3,
7122   google_protobuf_FieldDescriptorProto_TYPE_UINT64 = 4,
7123   google_protobuf_FieldDescriptorProto_TYPE_INT32 = 5,
7124   google_protobuf_FieldDescriptorProto_TYPE_FIXED64 = 6,
7125   google_protobuf_FieldDescriptorProto_TYPE_FIXED32 = 7,
7126   google_protobuf_FieldDescriptorProto_TYPE_BOOL = 8,
7127   google_protobuf_FieldDescriptorProto_TYPE_STRING = 9,
7128   google_protobuf_FieldDescriptorProto_TYPE_GROUP = 10,
7129   google_protobuf_FieldDescriptorProto_TYPE_MESSAGE = 11,
7130   google_protobuf_FieldDescriptorProto_TYPE_BYTES = 12,
7131   google_protobuf_FieldDescriptorProto_TYPE_UINT32 = 13,
7132   google_protobuf_FieldDescriptorProto_TYPE_ENUM = 14,
7133   google_protobuf_FieldDescriptorProto_TYPE_SFIXED32 = 15,
7134   google_protobuf_FieldDescriptorProto_TYPE_SFIXED64 = 16,
7135   google_protobuf_FieldDescriptorProto_TYPE_SINT32 = 17,
7136   google_protobuf_FieldDescriptorProto_TYPE_SINT64 = 18
7137 } google_protobuf_FieldDescriptorProto_Type;
7138 
7139 typedef enum {
7140   google_protobuf_FieldOptions_STRING = 0,
7141   google_protobuf_FieldOptions_CORD = 1,
7142   google_protobuf_FieldOptions_STRING_PIECE = 2
7143 } google_protobuf_FieldOptions_CType;
7144 
7145 typedef enum {
7146   google_protobuf_FieldOptions_JS_NORMAL = 0,
7147   google_protobuf_FieldOptions_JS_STRING = 1,
7148   google_protobuf_FieldOptions_JS_NUMBER = 2
7149 } google_protobuf_FieldOptions_JSType;
7150 
7151 typedef enum {
7152   google_protobuf_FileOptions_SPEED = 1,
7153   google_protobuf_FileOptions_CODE_SIZE = 2,
7154   google_protobuf_FileOptions_LITE_RUNTIME = 3
7155 } google_protobuf_FileOptions_OptimizeMode;
7156 
7157 typedef enum {
7158   google_protobuf_MethodOptions_IDEMPOTENCY_UNKNOWN = 0,
7159   google_protobuf_MethodOptions_NO_SIDE_EFFECTS = 1,
7160   google_protobuf_MethodOptions_IDEMPOTENT = 2
7161 } google_protobuf_MethodOptions_IdempotencyLevel;
7162 
7163 /* google.protobuf.FileDescriptorSet */
7164 
7165 UPB_INLINE google_protobuf_FileDescriptorSet *google_protobuf_FileDescriptorSet_new(upb_arena *arena) {
7166   return (google_protobuf_FileDescriptorSet *)upb_msg_new(&google_protobuf_FileDescriptorSet_msginit, arena);
7167 }
7168 UPB_INLINE google_protobuf_FileDescriptorSet *google_protobuf_FileDescriptorSet_parsenew(upb_strview buf, upb_arena *arena) {
7169   google_protobuf_FileDescriptorSet *ret = google_protobuf_FileDescriptorSet_new(arena);
7170   return (ret && upb_decode(buf, ret, &google_protobuf_FileDescriptorSet_msginit)) ? ret : NULL;
7171 }
7172 UPB_INLINE char *google_protobuf_FileDescriptorSet_serialize(const google_protobuf_FileDescriptorSet *msg, upb_arena *arena, size_t *len) {
7173   return upb_encode(msg, &google_protobuf_FileDescriptorSet_msginit, arena, len);
7174 }
7175 
7176 UPB_INLINE const google_protobuf_FileDescriptorProto* const* google_protobuf_FileDescriptorSet_file(const google_protobuf_FileDescriptorSet *msg, size_t *len) { return (const google_protobuf_FileDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); }
7177 
7178 UPB_INLINE google_protobuf_FileDescriptorProto** google_protobuf_FileDescriptorSet_mutable_file(google_protobuf_FileDescriptorSet *msg, size_t *len) {
7179   return (google_protobuf_FileDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len);
7180 }
7181 UPB_INLINE google_protobuf_FileDescriptorProto** google_protobuf_FileDescriptorSet_resize_file(google_protobuf_FileDescriptorSet *msg, size_t len, upb_arena *arena) {
7182   return (google_protobuf_FileDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
7183 }
7184 UPB_INLINE struct google_protobuf_FileDescriptorProto* google_protobuf_FileDescriptorSet_add_file(google_protobuf_FileDescriptorSet *msg, upb_arena *arena) {
7185   struct google_protobuf_FileDescriptorProto* sub = (struct google_protobuf_FileDescriptorProto*)upb_msg_new(&google_protobuf_FileDescriptorProto_msginit, arena);
7186   bool ok = _upb_array_append_accessor(
7187       msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
7188   if (!ok) return NULL;
7189   return sub;
7190 }
7191 
7192 
7193 /* google.protobuf.FileDescriptorProto */
7194 
7195 UPB_INLINE google_protobuf_FileDescriptorProto *google_protobuf_FileDescriptorProto_new(upb_arena *arena) {
7196   return (google_protobuf_FileDescriptorProto *)upb_msg_new(&google_protobuf_FileDescriptorProto_msginit, arena);
7197 }
7198 UPB_INLINE google_protobuf_FileDescriptorProto *google_protobuf_FileDescriptorProto_parsenew(upb_strview buf, upb_arena *arena) {
7199   google_protobuf_FileDescriptorProto *ret = google_protobuf_FileDescriptorProto_new(arena);
7200   return (ret && upb_decode(buf, ret, &google_protobuf_FileDescriptorProto_msginit)) ? ret : NULL;
7201 }
7202 UPB_INLINE char *google_protobuf_FileDescriptorProto_serialize(const google_protobuf_FileDescriptorProto *msg, upb_arena *arena, size_t *len) {
7203   return upb_encode(msg, &google_protobuf_FileDescriptorProto_msginit, arena, len);
7204 }
7205 
7206 UPB_INLINE bool google_protobuf_FileDescriptorProto_has_name(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 0); }
7207 UPB_INLINE upb_strview google_protobuf_FileDescriptorProto_name(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
7208 UPB_INLINE bool google_protobuf_FileDescriptorProto_has_package(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 1); }
7209 UPB_INLINE upb_strview google_protobuf_FileDescriptorProto_package(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)); }
7210 UPB_INLINE upb_strview const* google_protobuf_FileDescriptorProto_dependency(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (upb_strview const*)_upb_array_accessor(msg, UPB_SIZE(36, 72), len); }
7211 UPB_INLINE const google_protobuf_DescriptorProto* const* google_protobuf_FileDescriptorProto_message_type(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(40, 80), len); }
7212 UPB_INLINE const google_protobuf_EnumDescriptorProto* const* google_protobuf_FileDescriptorProto_enum_type(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(44, 88), len); }
7213 UPB_INLINE const google_protobuf_ServiceDescriptorProto* const* google_protobuf_FileDescriptorProto_service(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_ServiceDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(48, 96), len); }
7214 UPB_INLINE const google_protobuf_FieldDescriptorProto* const* google_protobuf_FileDescriptorProto_extension(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_FieldDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(52, 104), len); }
7215 UPB_INLINE bool google_protobuf_FileDescriptorProto_has_options(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 3); }
7216 UPB_INLINE const google_protobuf_FileOptions* google_protobuf_FileDescriptorProto_options(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_FileOptions*, UPB_SIZE(28, 56)); }
7217 UPB_INLINE bool google_protobuf_FileDescriptorProto_has_source_code_info(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 4); }
7218 UPB_INLINE const google_protobuf_SourceCodeInfo* google_protobuf_FileDescriptorProto_source_code_info(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_SourceCodeInfo*, UPB_SIZE(32, 64)); }
7219 UPB_INLINE int32_t const* google_protobuf_FileDescriptorProto_public_dependency(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(56, 112), len); }
7220 UPB_INLINE int32_t const* google_protobuf_FileDescriptorProto_weak_dependency(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(60, 120), len); }
7221 UPB_INLINE bool google_protobuf_FileDescriptorProto_has_syntax(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 2); }
7222 UPB_INLINE upb_strview google_protobuf_FileDescriptorProto_syntax(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(20, 40)); }
7223 
7224 UPB_INLINE void google_protobuf_FileDescriptorProto_set_name(google_protobuf_FileDescriptorProto *msg, upb_strview value) {
7225   _upb_sethas(msg, 0);
7226   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
7227 }
7228 UPB_INLINE void google_protobuf_FileDescriptorProto_set_package(google_protobuf_FileDescriptorProto *msg, upb_strview value) {
7229   _upb_sethas(msg, 1);
7230   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)) = value;
7231 }
7232 UPB_INLINE upb_strview* google_protobuf_FileDescriptorProto_mutable_dependency(google_protobuf_FileDescriptorProto *msg, size_t *len) {
7233   return (upb_strview*)_upb_array_mutable_accessor(msg, UPB_SIZE(36, 72), len);
7234 }
7235 UPB_INLINE upb_strview* google_protobuf_FileDescriptorProto_resize_dependency(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) {
7236   return (upb_strview*)_upb_array_resize_accessor(msg, UPB_SIZE(36, 72), len, UPB_SIZE(8, 16), UPB_TYPE_STRING, arena);
7237 }
7238 UPB_INLINE bool google_protobuf_FileDescriptorProto_add_dependency(google_protobuf_FileDescriptorProto *msg, upb_strview val, upb_arena *arena) {
7239   return _upb_array_append_accessor(
7240       msg, UPB_SIZE(36, 72), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, arena);
7241 }
7242 UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_FileDescriptorProto_mutable_message_type(google_protobuf_FileDescriptorProto *msg, size_t *len) {
7243   return (google_protobuf_DescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(40, 80), len);
7244 }
7245 UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_FileDescriptorProto_resize_message_type(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) {
7246   return (google_protobuf_DescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(40, 80), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
7247 }
7248 UPB_INLINE struct google_protobuf_DescriptorProto* google_protobuf_FileDescriptorProto_add_message_type(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) {
7249   struct google_protobuf_DescriptorProto* sub = (struct google_protobuf_DescriptorProto*)upb_msg_new(&google_protobuf_DescriptorProto_msginit, arena);
7250   bool ok = _upb_array_append_accessor(
7251       msg, UPB_SIZE(40, 80), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
7252   if (!ok) return NULL;
7253   return sub;
7254 }
7255 UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_FileDescriptorProto_mutable_enum_type(google_protobuf_FileDescriptorProto *msg, size_t *len) {
7256   return (google_protobuf_EnumDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(44, 88), len);
7257 }
7258 UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_FileDescriptorProto_resize_enum_type(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) {
7259   return (google_protobuf_EnumDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(44, 88), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
7260 }
7261 UPB_INLINE struct google_protobuf_EnumDescriptorProto* google_protobuf_FileDescriptorProto_add_enum_type(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) {
7262   struct google_protobuf_EnumDescriptorProto* sub = (struct google_protobuf_EnumDescriptorProto*)upb_msg_new(&google_protobuf_EnumDescriptorProto_msginit, arena);
7263   bool ok = _upb_array_append_accessor(
7264       msg, UPB_SIZE(44, 88), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
7265   if (!ok) return NULL;
7266   return sub;
7267 }
7268 UPB_INLINE google_protobuf_ServiceDescriptorProto** google_protobuf_FileDescriptorProto_mutable_service(google_protobuf_FileDescriptorProto *msg, size_t *len) {
7269   return (google_protobuf_ServiceDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(48, 96), len);
7270 }
7271 UPB_INLINE google_protobuf_ServiceDescriptorProto** google_protobuf_FileDescriptorProto_resize_service(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) {
7272   return (google_protobuf_ServiceDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(48, 96), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
7273 }
7274 UPB_INLINE struct google_protobuf_ServiceDescriptorProto* google_protobuf_FileDescriptorProto_add_service(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) {
7275   struct google_protobuf_ServiceDescriptorProto* sub = (struct google_protobuf_ServiceDescriptorProto*)upb_msg_new(&google_protobuf_ServiceDescriptorProto_msginit, arena);
7276   bool ok = _upb_array_append_accessor(
7277       msg, UPB_SIZE(48, 96), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
7278   if (!ok) return NULL;
7279   return sub;
7280 }
7281 UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_FileDescriptorProto_mutable_extension(google_protobuf_FileDescriptorProto *msg, size_t *len) {
7282   return (google_protobuf_FieldDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(52, 104), len);
7283 }
7284 UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_FileDescriptorProto_resize_extension(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) {
7285   return (google_protobuf_FieldDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(52, 104), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
7286 }
7287 UPB_INLINE struct google_protobuf_FieldDescriptorProto* google_protobuf_FileDescriptorProto_add_extension(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) {
7288   struct google_protobuf_FieldDescriptorProto* sub = (struct google_protobuf_FieldDescriptorProto*)upb_msg_new(&google_protobuf_FieldDescriptorProto_msginit, arena);
7289   bool ok = _upb_array_append_accessor(
7290       msg, UPB_SIZE(52, 104), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
7291   if (!ok) return NULL;
7292   return sub;
7293 }
7294 UPB_INLINE void google_protobuf_FileDescriptorProto_set_options(google_protobuf_FileDescriptorProto *msg, google_protobuf_FileOptions* value) {
7295   _upb_sethas(msg, 3);
7296   UPB_FIELD_AT(msg, google_protobuf_FileOptions*, UPB_SIZE(28, 56)) = value;
7297 }
7298 UPB_INLINE struct google_protobuf_FileOptions* google_protobuf_FileDescriptorProto_mutable_options(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) {
7299   struct google_protobuf_FileOptions* sub = (struct google_protobuf_FileOptions*)google_protobuf_FileDescriptorProto_options(msg);
7300   if (sub == NULL) {
7301     sub = (struct google_protobuf_FileOptions*)upb_msg_new(&google_protobuf_FileOptions_msginit, arena);
7302     if (!sub) return NULL;
7303     google_protobuf_FileDescriptorProto_set_options(msg, sub);
7304   }
7305   return sub;
7306 }
7307 UPB_INLINE void google_protobuf_FileDescriptorProto_set_source_code_info(google_protobuf_FileDescriptorProto *msg, google_protobuf_SourceCodeInfo* value) {
7308   _upb_sethas(msg, 4);
7309   UPB_FIELD_AT(msg, google_protobuf_SourceCodeInfo*, UPB_SIZE(32, 64)) = value;
7310 }
7311 UPB_INLINE struct google_protobuf_SourceCodeInfo* google_protobuf_FileDescriptorProto_mutable_source_code_info(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) {
7312   struct google_protobuf_SourceCodeInfo* sub = (struct google_protobuf_SourceCodeInfo*)google_protobuf_FileDescriptorProto_source_code_info(msg);
7313   if (sub == NULL) {
7314     sub = (struct google_protobuf_SourceCodeInfo*)upb_msg_new(&google_protobuf_SourceCodeInfo_msginit, arena);
7315     if (!sub) return NULL;
7316     google_protobuf_FileDescriptorProto_set_source_code_info(msg, sub);
7317   }
7318   return sub;
7319 }
7320 UPB_INLINE int32_t* google_protobuf_FileDescriptorProto_mutable_public_dependency(google_protobuf_FileDescriptorProto *msg, size_t *len) {
7321   return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(56, 112), len);
7322 }
7323 UPB_INLINE int32_t* google_protobuf_FileDescriptorProto_resize_public_dependency(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) {
7324   return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(56, 112), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena);
7325 }
7326 UPB_INLINE bool google_protobuf_FileDescriptorProto_add_public_dependency(google_protobuf_FileDescriptorProto *msg, int32_t val, upb_arena *arena) {
7327   return _upb_array_append_accessor(
7328       msg, UPB_SIZE(56, 112), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena);
7329 }
7330 UPB_INLINE int32_t* google_protobuf_FileDescriptorProto_mutable_weak_dependency(google_protobuf_FileDescriptorProto *msg, size_t *len) {
7331   return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(60, 120), len);
7332 }
7333 UPB_INLINE int32_t* google_protobuf_FileDescriptorProto_resize_weak_dependency(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) {
7334   return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(60, 120), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena);
7335 }
7336 UPB_INLINE bool google_protobuf_FileDescriptorProto_add_weak_dependency(google_protobuf_FileDescriptorProto *msg, int32_t val, upb_arena *arena) {
7337   return _upb_array_append_accessor(
7338       msg, UPB_SIZE(60, 120), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena);
7339 }
7340 UPB_INLINE void google_protobuf_FileDescriptorProto_set_syntax(google_protobuf_FileDescriptorProto *msg, upb_strview value) {
7341   _upb_sethas(msg, 2);
7342   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(20, 40)) = value;
7343 }
7344 
7345 
7346 /* google.protobuf.DescriptorProto */
7347 
7348 UPB_INLINE google_protobuf_DescriptorProto *google_protobuf_DescriptorProto_new(upb_arena *arena) {
7349   return (google_protobuf_DescriptorProto *)upb_msg_new(&google_protobuf_DescriptorProto_msginit, arena);
7350 }
7351 UPB_INLINE google_protobuf_DescriptorProto *google_protobuf_DescriptorProto_parsenew(upb_strview buf, upb_arena *arena) {
7352   google_protobuf_DescriptorProto *ret = google_protobuf_DescriptorProto_new(arena);
7353   return (ret && upb_decode(buf, ret, &google_protobuf_DescriptorProto_msginit)) ? ret : NULL;
7354 }
7355 UPB_INLINE char *google_protobuf_DescriptorProto_serialize(const google_protobuf_DescriptorProto *msg, upb_arena *arena, size_t *len) {
7356   return upb_encode(msg, &google_protobuf_DescriptorProto_msginit, arena, len);
7357 }
7358 
7359 UPB_INLINE bool google_protobuf_DescriptorProto_has_name(const google_protobuf_DescriptorProto *msg) { return _upb_has_field(msg, 0); }
7360 UPB_INLINE upb_strview google_protobuf_DescriptorProto_name(const google_protobuf_DescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
7361 UPB_INLINE const google_protobuf_FieldDescriptorProto* const* google_protobuf_DescriptorProto_field(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_FieldDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(16, 32), len); }
7362 UPB_INLINE const google_protobuf_DescriptorProto* const* google_protobuf_DescriptorProto_nested_type(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(20, 40), len); }
7363 UPB_INLINE const google_protobuf_EnumDescriptorProto* const* google_protobuf_DescriptorProto_enum_type(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(24, 48), len); }
7364 UPB_INLINE const google_protobuf_DescriptorProto_ExtensionRange* const* google_protobuf_DescriptorProto_extension_range(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto_ExtensionRange* const*)_upb_array_accessor(msg, UPB_SIZE(28, 56), len); }
7365 UPB_INLINE const google_protobuf_FieldDescriptorProto* const* google_protobuf_DescriptorProto_extension(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_FieldDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(32, 64), len); }
7366 UPB_INLINE bool google_protobuf_DescriptorProto_has_options(const google_protobuf_DescriptorProto *msg) { return _upb_has_field(msg, 1); }
7367 UPB_INLINE const google_protobuf_MessageOptions* google_protobuf_DescriptorProto_options(const google_protobuf_DescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_MessageOptions*, UPB_SIZE(12, 24)); }
7368 UPB_INLINE const google_protobuf_OneofDescriptorProto* const* google_protobuf_DescriptorProto_oneof_decl(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_OneofDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(36, 72), len); }
7369 UPB_INLINE const google_protobuf_DescriptorProto_ReservedRange* const* google_protobuf_DescriptorProto_reserved_range(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto_ReservedRange* const*)_upb_array_accessor(msg, UPB_SIZE(40, 80), len); }
7370 UPB_INLINE upb_strview const* google_protobuf_DescriptorProto_reserved_name(const google_protobuf_DescriptorProto *msg, size_t *len) { return (upb_strview const*)_upb_array_accessor(msg, UPB_SIZE(44, 88), len); }
7371 
7372 UPB_INLINE void google_protobuf_DescriptorProto_set_name(google_protobuf_DescriptorProto *msg, upb_strview value) {
7373   _upb_sethas(msg, 0);
7374   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
7375 }
7376 UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_mutable_field(google_protobuf_DescriptorProto *msg, size_t *len) {
7377   return (google_protobuf_FieldDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(16, 32), len);
7378 }
7379 UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_resize_field(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
7380   return (google_protobuf_FieldDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(16, 32), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
7381 }
7382 UPB_INLINE struct google_protobuf_FieldDescriptorProto* google_protobuf_DescriptorProto_add_field(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
7383   struct google_protobuf_FieldDescriptorProto* sub = (struct google_protobuf_FieldDescriptorProto*)upb_msg_new(&google_protobuf_FieldDescriptorProto_msginit, arena);
7384   bool ok = _upb_array_append_accessor(
7385       msg, UPB_SIZE(16, 32), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
7386   if (!ok) return NULL;
7387   return sub;
7388 }
7389 UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_DescriptorProto_mutable_nested_type(google_protobuf_DescriptorProto *msg, size_t *len) {
7390   return (google_protobuf_DescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 40), len);
7391 }
7392 UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_DescriptorProto_resize_nested_type(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
7393   return (google_protobuf_DescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(20, 40), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
7394 }
7395 UPB_INLINE struct google_protobuf_DescriptorProto* google_protobuf_DescriptorProto_add_nested_type(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
7396   struct google_protobuf_DescriptorProto* sub = (struct google_protobuf_DescriptorProto*)upb_msg_new(&google_protobuf_DescriptorProto_msginit, arena);
7397   bool ok = _upb_array_append_accessor(
7398       msg, UPB_SIZE(20, 40), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
7399   if (!ok) return NULL;
7400   return sub;
7401 }
7402 UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_DescriptorProto_mutable_enum_type(google_protobuf_DescriptorProto *msg, size_t *len) {
7403   return (google_protobuf_EnumDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(24, 48), len);
7404 }
7405 UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_DescriptorProto_resize_enum_type(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
7406   return (google_protobuf_EnumDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(24, 48), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
7407 }
7408 UPB_INLINE struct google_protobuf_EnumDescriptorProto* google_protobuf_DescriptorProto_add_enum_type(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
7409   struct google_protobuf_EnumDescriptorProto* sub = (struct google_protobuf_EnumDescriptorProto*)upb_msg_new(&google_protobuf_EnumDescriptorProto_msginit, arena);
7410   bool ok = _upb_array_append_accessor(
7411       msg, UPB_SIZE(24, 48), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
7412   if (!ok) return NULL;
7413   return sub;
7414 }
7415 UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange** google_protobuf_DescriptorProto_mutable_extension_range(google_protobuf_DescriptorProto *msg, size_t *len) {
7416   return (google_protobuf_DescriptorProto_ExtensionRange**)_upb_array_mutable_accessor(msg, UPB_SIZE(28, 56), len);
7417 }
7418 UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange** google_protobuf_DescriptorProto_resize_extension_range(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
7419   return (google_protobuf_DescriptorProto_ExtensionRange**)_upb_array_resize_accessor(msg, UPB_SIZE(28, 56), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
7420 }
7421 UPB_INLINE struct google_protobuf_DescriptorProto_ExtensionRange* google_protobuf_DescriptorProto_add_extension_range(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
7422   struct google_protobuf_DescriptorProto_ExtensionRange* sub = (struct google_protobuf_DescriptorProto_ExtensionRange*)upb_msg_new(&google_protobuf_DescriptorProto_ExtensionRange_msginit, arena);
7423   bool ok = _upb_array_append_accessor(
7424       msg, UPB_SIZE(28, 56), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
7425   if (!ok) return NULL;
7426   return sub;
7427 }
7428 UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_mutable_extension(google_protobuf_DescriptorProto *msg, size_t *len) {
7429   return (google_protobuf_FieldDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(32, 64), len);
7430 }
7431 UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_resize_extension(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
7432   return (google_protobuf_FieldDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(32, 64), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
7433 }
7434 UPB_INLINE struct google_protobuf_FieldDescriptorProto* google_protobuf_DescriptorProto_add_extension(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
7435   struct google_protobuf_FieldDescriptorProto* sub = (struct google_protobuf_FieldDescriptorProto*)upb_msg_new(&google_protobuf_FieldDescriptorProto_msginit, arena);
7436   bool ok = _upb_array_append_accessor(
7437       msg, UPB_SIZE(32, 64), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
7438   if (!ok) return NULL;
7439   return sub;
7440 }
7441 UPB_INLINE void google_protobuf_DescriptorProto_set_options(google_protobuf_DescriptorProto *msg, google_protobuf_MessageOptions* value) {
7442   _upb_sethas(msg, 1);
7443   UPB_FIELD_AT(msg, google_protobuf_MessageOptions*, UPB_SIZE(12, 24)) = value;
7444 }
7445 UPB_INLINE struct google_protobuf_MessageOptions* google_protobuf_DescriptorProto_mutable_options(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
7446   struct google_protobuf_MessageOptions* sub = (struct google_protobuf_MessageOptions*)google_protobuf_DescriptorProto_options(msg);
7447   if (sub == NULL) {
7448     sub = (struct google_protobuf_MessageOptions*)upb_msg_new(&google_protobuf_MessageOptions_msginit, arena);
7449     if (!sub) return NULL;
7450     google_protobuf_DescriptorProto_set_options(msg, sub);
7451   }
7452   return sub;
7453 }
7454 UPB_INLINE google_protobuf_OneofDescriptorProto** google_protobuf_DescriptorProto_mutable_oneof_decl(google_protobuf_DescriptorProto *msg, size_t *len) {
7455   return (google_protobuf_OneofDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(36, 72), len);
7456 }
7457 UPB_INLINE google_protobuf_OneofDescriptorProto** google_protobuf_DescriptorProto_resize_oneof_decl(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
7458   return (google_protobuf_OneofDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(36, 72), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
7459 }
7460 UPB_INLINE struct google_protobuf_OneofDescriptorProto* google_protobuf_DescriptorProto_add_oneof_decl(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
7461   struct google_protobuf_OneofDescriptorProto* sub = (struct google_protobuf_OneofDescriptorProto*)upb_msg_new(&google_protobuf_OneofDescriptorProto_msginit, arena);
7462   bool ok = _upb_array_append_accessor(
7463       msg, UPB_SIZE(36, 72), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
7464   if (!ok) return NULL;
7465   return sub;
7466 }
7467 UPB_INLINE google_protobuf_DescriptorProto_ReservedRange** google_protobuf_DescriptorProto_mutable_reserved_range(google_protobuf_DescriptorProto *msg, size_t *len) {
7468   return (google_protobuf_DescriptorProto_ReservedRange**)_upb_array_mutable_accessor(msg, UPB_SIZE(40, 80), len);
7469 }
7470 UPB_INLINE google_protobuf_DescriptorProto_ReservedRange** google_protobuf_DescriptorProto_resize_reserved_range(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
7471   return (google_protobuf_DescriptorProto_ReservedRange**)_upb_array_resize_accessor(msg, UPB_SIZE(40, 80), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
7472 }
7473 UPB_INLINE struct google_protobuf_DescriptorProto_ReservedRange* google_protobuf_DescriptorProto_add_reserved_range(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
7474   struct google_protobuf_DescriptorProto_ReservedRange* sub = (struct google_protobuf_DescriptorProto_ReservedRange*)upb_msg_new(&google_protobuf_DescriptorProto_ReservedRange_msginit, arena);
7475   bool ok = _upb_array_append_accessor(
7476       msg, UPB_SIZE(40, 80), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
7477   if (!ok) return NULL;
7478   return sub;
7479 }
7480 UPB_INLINE upb_strview* google_protobuf_DescriptorProto_mutable_reserved_name(google_protobuf_DescriptorProto *msg, size_t *len) {
7481   return (upb_strview*)_upb_array_mutable_accessor(msg, UPB_SIZE(44, 88), len);
7482 }
7483 UPB_INLINE upb_strview* google_protobuf_DescriptorProto_resize_reserved_name(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
7484   return (upb_strview*)_upb_array_resize_accessor(msg, UPB_SIZE(44, 88), len, UPB_SIZE(8, 16), UPB_TYPE_STRING, arena);
7485 }
7486 UPB_INLINE bool google_protobuf_DescriptorProto_add_reserved_name(google_protobuf_DescriptorProto *msg, upb_strview val, upb_arena *arena) {
7487   return _upb_array_append_accessor(
7488       msg, UPB_SIZE(44, 88), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, arena);
7489 }
7490 
7491 
7492 /* google.protobuf.DescriptorProto.ExtensionRange */
7493 
7494 UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange *google_protobuf_DescriptorProto_ExtensionRange_new(upb_arena *arena) {
7495   return (google_protobuf_DescriptorProto_ExtensionRange *)upb_msg_new(&google_protobuf_DescriptorProto_ExtensionRange_msginit, arena);
7496 }
7497 UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange *google_protobuf_DescriptorProto_ExtensionRange_parsenew(upb_strview buf, upb_arena *arena) {
7498   google_protobuf_DescriptorProto_ExtensionRange *ret = google_protobuf_DescriptorProto_ExtensionRange_new(arena);
7499   return (ret && upb_decode(buf, ret, &google_protobuf_DescriptorProto_ExtensionRange_msginit)) ? ret : NULL;
7500 }
7501 UPB_INLINE char *google_protobuf_DescriptorProto_ExtensionRange_serialize(const google_protobuf_DescriptorProto_ExtensionRange *msg, upb_arena *arena, size_t *len) {
7502   return upb_encode(msg, &google_protobuf_DescriptorProto_ExtensionRange_msginit, arena, len);
7503 }
7504 
7505 UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_start(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return _upb_has_field(msg, 0); }
7506 UPB_INLINE int32_t google_protobuf_DescriptorProto_ExtensionRange_start(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); }
7507 UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_end(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return _upb_has_field(msg, 1); }
7508 UPB_INLINE int32_t google_protobuf_DescriptorProto_ExtensionRange_end(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); }
7509 UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_options(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return _upb_has_field(msg, 2); }
7510 UPB_INLINE const google_protobuf_ExtensionRangeOptions* google_protobuf_DescriptorProto_ExtensionRange_options(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return UPB_FIELD_AT(msg, const google_protobuf_ExtensionRangeOptions*, UPB_SIZE(12, 16)); }
7511 
7512 UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_set_start(google_protobuf_DescriptorProto_ExtensionRange *msg, int32_t value) {
7513   _upb_sethas(msg, 0);
7514   UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value;
7515 }
7516 UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_set_end(google_protobuf_DescriptorProto_ExtensionRange *msg, int32_t value) {
7517   _upb_sethas(msg, 1);
7518   UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value;
7519 }
7520 UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_set_options(google_protobuf_DescriptorProto_ExtensionRange *msg, google_protobuf_ExtensionRangeOptions* value) {
7521   _upb_sethas(msg, 2);
7522   UPB_FIELD_AT(msg, google_protobuf_ExtensionRangeOptions*, UPB_SIZE(12, 16)) = value;
7523 }
7524 UPB_INLINE struct google_protobuf_ExtensionRangeOptions* google_protobuf_DescriptorProto_ExtensionRange_mutable_options(google_protobuf_DescriptorProto_ExtensionRange *msg, upb_arena *arena) {
7525   struct google_protobuf_ExtensionRangeOptions* sub = (struct google_protobuf_ExtensionRangeOptions*)google_protobuf_DescriptorProto_ExtensionRange_options(msg);
7526   if (sub == NULL) {
7527     sub = (struct google_protobuf_ExtensionRangeOptions*)upb_msg_new(&google_protobuf_ExtensionRangeOptions_msginit, arena);
7528     if (!sub) return NULL;
7529     google_protobuf_DescriptorProto_ExtensionRange_set_options(msg, sub);
7530   }
7531   return sub;
7532 }
7533 
7534 
7535 /* google.protobuf.DescriptorProto.ReservedRange */
7536 
7537 UPB_INLINE google_protobuf_DescriptorProto_ReservedRange *google_protobuf_DescriptorProto_ReservedRange_new(upb_arena *arena) {
7538   return (google_protobuf_DescriptorProto_ReservedRange *)upb_msg_new(&google_protobuf_DescriptorProto_ReservedRange_msginit, arena);
7539 }
7540 UPB_INLINE google_protobuf_DescriptorProto_ReservedRange *google_protobuf_DescriptorProto_ReservedRange_parsenew(upb_strview buf, upb_arena *arena) {
7541   google_protobuf_DescriptorProto_ReservedRange *ret = google_protobuf_DescriptorProto_ReservedRange_new(arena);
7542   return (ret && upb_decode(buf, ret, &google_protobuf_DescriptorProto_ReservedRange_msginit)) ? ret : NULL;
7543 }
7544 UPB_INLINE char *google_protobuf_DescriptorProto_ReservedRange_serialize(const google_protobuf_DescriptorProto_ReservedRange *msg, upb_arena *arena, size_t *len) {
7545   return upb_encode(msg, &google_protobuf_DescriptorProto_ReservedRange_msginit, arena, len);
7546 }
7547 
7548 UPB_INLINE bool google_protobuf_DescriptorProto_ReservedRange_has_start(const google_protobuf_DescriptorProto_ReservedRange *msg) { return _upb_has_field(msg, 0); }
7549 UPB_INLINE int32_t google_protobuf_DescriptorProto_ReservedRange_start(const google_protobuf_DescriptorProto_ReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); }
7550 UPB_INLINE bool google_protobuf_DescriptorProto_ReservedRange_has_end(const google_protobuf_DescriptorProto_ReservedRange *msg) { return _upb_has_field(msg, 1); }
7551 UPB_INLINE int32_t google_protobuf_DescriptorProto_ReservedRange_end(const google_protobuf_DescriptorProto_ReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); }
7552 
7553 UPB_INLINE void google_protobuf_DescriptorProto_ReservedRange_set_start(google_protobuf_DescriptorProto_ReservedRange *msg, int32_t value) {
7554   _upb_sethas(msg, 0);
7555   UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value;
7556 }
7557 UPB_INLINE void google_protobuf_DescriptorProto_ReservedRange_set_end(google_protobuf_DescriptorProto_ReservedRange *msg, int32_t value) {
7558   _upb_sethas(msg, 1);
7559   UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value;
7560 }
7561 
7562 
7563 /* google.protobuf.ExtensionRangeOptions */
7564 
7565 UPB_INLINE google_protobuf_ExtensionRangeOptions *google_protobuf_ExtensionRangeOptions_new(upb_arena *arena) {
7566   return (google_protobuf_ExtensionRangeOptions *)upb_msg_new(&google_protobuf_ExtensionRangeOptions_msginit, arena);
7567 }
7568 UPB_INLINE google_protobuf_ExtensionRangeOptions *google_protobuf_ExtensionRangeOptions_parsenew(upb_strview buf, upb_arena *arena) {
7569   google_protobuf_ExtensionRangeOptions *ret = google_protobuf_ExtensionRangeOptions_new(arena);
7570   return (ret && upb_decode(buf, ret, &google_protobuf_ExtensionRangeOptions_msginit)) ? ret : NULL;
7571 }
7572 UPB_INLINE char *google_protobuf_ExtensionRangeOptions_serialize(const google_protobuf_ExtensionRangeOptions *msg, upb_arena *arena, size_t *len) {
7573   return upb_encode(msg, &google_protobuf_ExtensionRangeOptions_msginit, arena, len);
7574 }
7575 
7576 UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_ExtensionRangeOptions_uninterpreted_option(const google_protobuf_ExtensionRangeOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); }
7577 
7578 UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ExtensionRangeOptions_mutable_uninterpreted_option(google_protobuf_ExtensionRangeOptions *msg, size_t *len) {
7579   return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len);
7580 }
7581 UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ExtensionRangeOptions_resize_uninterpreted_option(google_protobuf_ExtensionRangeOptions *msg, size_t len, upb_arena *arena) {
7582   return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
7583 }
7584 UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_ExtensionRangeOptions_add_uninterpreted_option(google_protobuf_ExtensionRangeOptions *msg, upb_arena *arena) {
7585   struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
7586   bool ok = _upb_array_append_accessor(
7587       msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
7588   if (!ok) return NULL;
7589   return sub;
7590 }
7591 
7592 
7593 /* google.protobuf.FieldDescriptorProto */
7594 
7595 UPB_INLINE google_protobuf_FieldDescriptorProto *google_protobuf_FieldDescriptorProto_new(upb_arena *arena) {
7596   return (google_protobuf_FieldDescriptorProto *)upb_msg_new(&google_protobuf_FieldDescriptorProto_msginit, arena);
7597 }
7598 UPB_INLINE google_protobuf_FieldDescriptorProto *google_protobuf_FieldDescriptorProto_parsenew(upb_strview buf, upb_arena *arena) {
7599   google_protobuf_FieldDescriptorProto *ret = google_protobuf_FieldDescriptorProto_new(arena);
7600   return (ret && upb_decode(buf, ret, &google_protobuf_FieldDescriptorProto_msginit)) ? ret : NULL;
7601 }
7602 UPB_INLINE char *google_protobuf_FieldDescriptorProto_serialize(const google_protobuf_FieldDescriptorProto *msg, upb_arena *arena, size_t *len) {
7603   return upb_encode(msg, &google_protobuf_FieldDescriptorProto_msginit, arena, len);
7604 }
7605 
7606 UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_name(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 4); }
7607 UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_name(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(32, 32)); }
7608 UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_extendee(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 5); }
7609 UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_extendee(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(40, 48)); }
7610 UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_number(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 2); }
7611 UPB_INLINE int32_t google_protobuf_FieldDescriptorProto_number(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(24, 24)); }
7612 UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_label(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 0); }
7613 UPB_INLINE google_protobuf_FieldDescriptorProto_Label google_protobuf_FieldDescriptorProto_label(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, google_protobuf_FieldDescriptorProto_Label, UPB_SIZE(8, 8)); }
7614 UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_type(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 1); }
7615 UPB_INLINE google_protobuf_FieldDescriptorProto_Type google_protobuf_FieldDescriptorProto_type(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, google_protobuf_FieldDescriptorProto_Type, UPB_SIZE(16, 16)); }
7616 UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_type_name(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 6); }
7617 UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_type_name(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(48, 64)); }
7618 UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_default_value(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 7); }
7619 UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_default_value(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(56, 80)); }
7620 UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_options(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 9); }
7621 UPB_INLINE const google_protobuf_FieldOptions* google_protobuf_FieldDescriptorProto_options(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_FieldOptions*, UPB_SIZE(72, 112)); }
7622 UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_oneof_index(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 3); }
7623 UPB_INLINE int32_t google_protobuf_FieldDescriptorProto_oneof_index(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(28, 28)); }
7624 UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_json_name(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 8); }
7625 UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_json_name(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(64, 96)); }
7626 
7627 UPB_INLINE void google_protobuf_FieldDescriptorProto_set_name(google_protobuf_FieldDescriptorProto *msg, upb_strview value) {
7628   _upb_sethas(msg, 4);
7629   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(32, 32)) = value;
7630 }
7631 UPB_INLINE void google_protobuf_FieldDescriptorProto_set_extendee(google_protobuf_FieldDescriptorProto *msg, upb_strview value) {
7632   _upb_sethas(msg, 5);
7633   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(40, 48)) = value;
7634 }
7635 UPB_INLINE void google_protobuf_FieldDescriptorProto_set_number(google_protobuf_FieldDescriptorProto *msg, int32_t value) {
7636   _upb_sethas(msg, 2);
7637   UPB_FIELD_AT(msg, int32_t, UPB_SIZE(24, 24)) = value;
7638 }
7639 UPB_INLINE void google_protobuf_FieldDescriptorProto_set_label(google_protobuf_FieldDescriptorProto *msg, google_protobuf_FieldDescriptorProto_Label value) {
7640   _upb_sethas(msg, 0);
7641   UPB_FIELD_AT(msg, google_protobuf_FieldDescriptorProto_Label, UPB_SIZE(8, 8)) = value;
7642 }
7643 UPB_INLINE void google_protobuf_FieldDescriptorProto_set_type(google_protobuf_FieldDescriptorProto *msg, google_protobuf_FieldDescriptorProto_Type value) {
7644   _upb_sethas(msg, 1);
7645   UPB_FIELD_AT(msg, google_protobuf_FieldDescriptorProto_Type, UPB_SIZE(16, 16)) = value;
7646 }
7647 UPB_INLINE void google_protobuf_FieldDescriptorProto_set_type_name(google_protobuf_FieldDescriptorProto *msg, upb_strview value) {
7648   _upb_sethas(msg, 6);
7649   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(48, 64)) = value;
7650 }
7651 UPB_INLINE void google_protobuf_FieldDescriptorProto_set_default_value(google_protobuf_FieldDescriptorProto *msg, upb_strview value) {
7652   _upb_sethas(msg, 7);
7653   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(56, 80)) = value;
7654 }
7655 UPB_INLINE void google_protobuf_FieldDescriptorProto_set_options(google_protobuf_FieldDescriptorProto *msg, google_protobuf_FieldOptions* value) {
7656   _upb_sethas(msg, 9);
7657   UPB_FIELD_AT(msg, google_protobuf_FieldOptions*, UPB_SIZE(72, 112)) = value;
7658 }
7659 UPB_INLINE struct google_protobuf_FieldOptions* google_protobuf_FieldDescriptorProto_mutable_options(google_protobuf_FieldDescriptorProto *msg, upb_arena *arena) {
7660   struct google_protobuf_FieldOptions* sub = (struct google_protobuf_FieldOptions*)google_protobuf_FieldDescriptorProto_options(msg);
7661   if (sub == NULL) {
7662     sub = (struct google_protobuf_FieldOptions*)upb_msg_new(&google_protobuf_FieldOptions_msginit, arena);
7663     if (!sub) return NULL;
7664     google_protobuf_FieldDescriptorProto_set_options(msg, sub);
7665   }
7666   return sub;
7667 }
7668 UPB_INLINE void google_protobuf_FieldDescriptorProto_set_oneof_index(google_protobuf_FieldDescriptorProto *msg, int32_t value) {
7669   _upb_sethas(msg, 3);
7670   UPB_FIELD_AT(msg, int32_t, UPB_SIZE(28, 28)) = value;
7671 }
7672 UPB_INLINE void google_protobuf_FieldDescriptorProto_set_json_name(google_protobuf_FieldDescriptorProto *msg, upb_strview value) {
7673   _upb_sethas(msg, 8);
7674   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(64, 96)) = value;
7675 }
7676 
7677 
7678 /* google.protobuf.OneofDescriptorProto */
7679 
7680 UPB_INLINE google_protobuf_OneofDescriptorProto *google_protobuf_OneofDescriptorProto_new(upb_arena *arena) {
7681   return (google_protobuf_OneofDescriptorProto *)upb_msg_new(&google_protobuf_OneofDescriptorProto_msginit, arena);
7682 }
7683 UPB_INLINE google_protobuf_OneofDescriptorProto *google_protobuf_OneofDescriptorProto_parsenew(upb_strview buf, upb_arena *arena) {
7684   google_protobuf_OneofDescriptorProto *ret = google_protobuf_OneofDescriptorProto_new(arena);
7685   return (ret && upb_decode(buf, ret, &google_protobuf_OneofDescriptorProto_msginit)) ? ret : NULL;
7686 }
7687 UPB_INLINE char *google_protobuf_OneofDescriptorProto_serialize(const google_protobuf_OneofDescriptorProto *msg, upb_arena *arena, size_t *len) {
7688   return upb_encode(msg, &google_protobuf_OneofDescriptorProto_msginit, arena, len);
7689 }
7690 
7691 UPB_INLINE bool google_protobuf_OneofDescriptorProto_has_name(const google_protobuf_OneofDescriptorProto *msg) { return _upb_has_field(msg, 0); }
7692 UPB_INLINE upb_strview google_protobuf_OneofDescriptorProto_name(const google_protobuf_OneofDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
7693 UPB_INLINE bool google_protobuf_OneofDescriptorProto_has_options(const google_protobuf_OneofDescriptorProto *msg) { return _upb_has_field(msg, 1); }
7694 UPB_INLINE const google_protobuf_OneofOptions* google_protobuf_OneofDescriptorProto_options(const google_protobuf_OneofDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_OneofOptions*, UPB_SIZE(12, 24)); }
7695 
7696 UPB_INLINE void google_protobuf_OneofDescriptorProto_set_name(google_protobuf_OneofDescriptorProto *msg, upb_strview value) {
7697   _upb_sethas(msg, 0);
7698   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
7699 }
7700 UPB_INLINE void google_protobuf_OneofDescriptorProto_set_options(google_protobuf_OneofDescriptorProto *msg, google_protobuf_OneofOptions* value) {
7701   _upb_sethas(msg, 1);
7702   UPB_FIELD_AT(msg, google_protobuf_OneofOptions*, UPB_SIZE(12, 24)) = value;
7703 }
7704 UPB_INLINE struct google_protobuf_OneofOptions* google_protobuf_OneofDescriptorProto_mutable_options(google_protobuf_OneofDescriptorProto *msg, upb_arena *arena) {
7705   struct google_protobuf_OneofOptions* sub = (struct google_protobuf_OneofOptions*)google_protobuf_OneofDescriptorProto_options(msg);
7706   if (sub == NULL) {
7707     sub = (struct google_protobuf_OneofOptions*)upb_msg_new(&google_protobuf_OneofOptions_msginit, arena);
7708     if (!sub) return NULL;
7709     google_protobuf_OneofDescriptorProto_set_options(msg, sub);
7710   }
7711   return sub;
7712 }
7713 
7714 
7715 /* google.protobuf.EnumDescriptorProto */
7716 
7717 UPB_INLINE google_protobuf_EnumDescriptorProto *google_protobuf_EnumDescriptorProto_new(upb_arena *arena) {
7718   return (google_protobuf_EnumDescriptorProto *)upb_msg_new(&google_protobuf_EnumDescriptorProto_msginit, arena);
7719 }
7720 UPB_INLINE google_protobuf_EnumDescriptorProto *google_protobuf_EnumDescriptorProto_parsenew(upb_strview buf, upb_arena *arena) {
7721   google_protobuf_EnumDescriptorProto *ret = google_protobuf_EnumDescriptorProto_new(arena);
7722   return (ret && upb_decode(buf, ret, &google_protobuf_EnumDescriptorProto_msginit)) ? ret : NULL;
7723 }
7724 UPB_INLINE char *google_protobuf_EnumDescriptorProto_serialize(const google_protobuf_EnumDescriptorProto *msg, upb_arena *arena, size_t *len) {
7725   return upb_encode(msg, &google_protobuf_EnumDescriptorProto_msginit, arena, len);
7726 }
7727 
7728 UPB_INLINE bool google_protobuf_EnumDescriptorProto_has_name(const google_protobuf_EnumDescriptorProto *msg) { return _upb_has_field(msg, 0); }
7729 UPB_INLINE upb_strview google_protobuf_EnumDescriptorProto_name(const google_protobuf_EnumDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
7730 UPB_INLINE const google_protobuf_EnumValueDescriptorProto* const* google_protobuf_EnumDescriptorProto_value(const google_protobuf_EnumDescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumValueDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(16, 32), len); }
7731 UPB_INLINE bool google_protobuf_EnumDescriptorProto_has_options(const google_protobuf_EnumDescriptorProto *msg) { return _upb_has_field(msg, 1); }
7732 UPB_INLINE const google_protobuf_EnumOptions* google_protobuf_EnumDescriptorProto_options(const google_protobuf_EnumDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_EnumOptions*, UPB_SIZE(12, 24)); }
7733 UPB_INLINE const google_protobuf_EnumDescriptorProto_EnumReservedRange* const* google_protobuf_EnumDescriptorProto_reserved_range(const google_protobuf_EnumDescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumDescriptorProto_EnumReservedRange* const*)_upb_array_accessor(msg, UPB_SIZE(20, 40), len); }
7734 UPB_INLINE upb_strview const* google_protobuf_EnumDescriptorProto_reserved_name(const google_protobuf_EnumDescriptorProto *msg, size_t *len) { return (upb_strview const*)_upb_array_accessor(msg, UPB_SIZE(24, 48), len); }
7735 
7736 UPB_INLINE void google_protobuf_EnumDescriptorProto_set_name(google_protobuf_EnumDescriptorProto *msg, upb_strview value) {
7737   _upb_sethas(msg, 0);
7738   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
7739 }
7740 UPB_INLINE google_protobuf_EnumValueDescriptorProto** google_protobuf_EnumDescriptorProto_mutable_value(google_protobuf_EnumDescriptorProto *msg, size_t *len) {
7741   return (google_protobuf_EnumValueDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(16, 32), len);
7742 }
7743 UPB_INLINE google_protobuf_EnumValueDescriptorProto** google_protobuf_EnumDescriptorProto_resize_value(google_protobuf_EnumDescriptorProto *msg, size_t len, upb_arena *arena) {
7744   return (google_protobuf_EnumValueDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(16, 32), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
7745 }
7746 UPB_INLINE struct google_protobuf_EnumValueDescriptorProto* google_protobuf_EnumDescriptorProto_add_value(google_protobuf_EnumDescriptorProto *msg, upb_arena *arena) {
7747   struct google_protobuf_EnumValueDescriptorProto* sub = (struct google_protobuf_EnumValueDescriptorProto*)upb_msg_new(&google_protobuf_EnumValueDescriptorProto_msginit, arena);
7748   bool ok = _upb_array_append_accessor(
7749       msg, UPB_SIZE(16, 32), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
7750   if (!ok) return NULL;
7751   return sub;
7752 }
7753 UPB_INLINE void google_protobuf_EnumDescriptorProto_set_options(google_protobuf_EnumDescriptorProto *msg, google_protobuf_EnumOptions* value) {
7754   _upb_sethas(msg, 1);
7755   UPB_FIELD_AT(msg, google_protobuf_EnumOptions*, UPB_SIZE(12, 24)) = value;
7756 }
7757 UPB_INLINE struct google_protobuf_EnumOptions* google_protobuf_EnumDescriptorProto_mutable_options(google_protobuf_EnumDescriptorProto *msg, upb_arena *arena) {
7758   struct google_protobuf_EnumOptions* sub = (struct google_protobuf_EnumOptions*)google_protobuf_EnumDescriptorProto_options(msg);
7759   if (sub == NULL) {
7760     sub = (struct google_protobuf_EnumOptions*)upb_msg_new(&google_protobuf_EnumOptions_msginit, arena);
7761     if (!sub) return NULL;
7762     google_protobuf_EnumDescriptorProto_set_options(msg, sub);
7763   }
7764   return sub;
7765 }
7766 UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange** google_protobuf_EnumDescriptorProto_mutable_reserved_range(google_protobuf_EnumDescriptorProto *msg, size_t *len) {
7767   return (google_protobuf_EnumDescriptorProto_EnumReservedRange**)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 40), len);
7768 }
7769 UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange** google_protobuf_EnumDescriptorProto_resize_reserved_range(google_protobuf_EnumDescriptorProto *msg, size_t len, upb_arena *arena) {
7770   return (google_protobuf_EnumDescriptorProto_EnumReservedRange**)_upb_array_resize_accessor(msg, UPB_SIZE(20, 40), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
7771 }
7772 UPB_INLINE struct google_protobuf_EnumDescriptorProto_EnumReservedRange* google_protobuf_EnumDescriptorProto_add_reserved_range(google_protobuf_EnumDescriptorProto *msg, upb_arena *arena) {
7773   struct google_protobuf_EnumDescriptorProto_EnumReservedRange* sub = (struct google_protobuf_EnumDescriptorProto_EnumReservedRange*)upb_msg_new(&google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, arena);
7774   bool ok = _upb_array_append_accessor(
7775       msg, UPB_SIZE(20, 40), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
7776   if (!ok) return NULL;
7777   return sub;
7778 }
7779 UPB_INLINE upb_strview* google_protobuf_EnumDescriptorProto_mutable_reserved_name(google_protobuf_EnumDescriptorProto *msg, size_t *len) {
7780   return (upb_strview*)_upb_array_mutable_accessor(msg, UPB_SIZE(24, 48), len);
7781 }
7782 UPB_INLINE upb_strview* google_protobuf_EnumDescriptorProto_resize_reserved_name(google_protobuf_EnumDescriptorProto *msg, size_t len, upb_arena *arena) {
7783   return (upb_strview*)_upb_array_resize_accessor(msg, UPB_SIZE(24, 48), len, UPB_SIZE(8, 16), UPB_TYPE_STRING, arena);
7784 }
7785 UPB_INLINE bool google_protobuf_EnumDescriptorProto_add_reserved_name(google_protobuf_EnumDescriptorProto *msg, upb_strview val, upb_arena *arena) {
7786   return _upb_array_append_accessor(
7787       msg, UPB_SIZE(24, 48), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, arena);
7788 }
7789 
7790 
7791 /* google.protobuf.EnumDescriptorProto.EnumReservedRange */
7792 
7793 UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange *google_protobuf_EnumDescriptorProto_EnumReservedRange_new(upb_arena *arena) {
7794   return (google_protobuf_EnumDescriptorProto_EnumReservedRange *)upb_msg_new(&google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, arena);
7795 }
7796 UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange *google_protobuf_EnumDescriptorProto_EnumReservedRange_parsenew(upb_strview buf, upb_arena *arena) {
7797   google_protobuf_EnumDescriptorProto_EnumReservedRange *ret = google_protobuf_EnumDescriptorProto_EnumReservedRange_new(arena);
7798   return (ret && upb_decode(buf, ret, &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit)) ? ret : NULL;
7799 }
7800 UPB_INLINE char *google_protobuf_EnumDescriptorProto_EnumReservedRange_serialize(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, upb_arena *arena, size_t *len) {
7801   return upb_encode(msg, &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, arena, len);
7802 }
7803 
7804 UPB_INLINE bool google_protobuf_EnumDescriptorProto_EnumReservedRange_has_start(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return _upb_has_field(msg, 0); }
7805 UPB_INLINE int32_t google_protobuf_EnumDescriptorProto_EnumReservedRange_start(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); }
7806 UPB_INLINE bool google_protobuf_EnumDescriptorProto_EnumReservedRange_has_end(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return _upb_has_field(msg, 1); }
7807 UPB_INLINE int32_t google_protobuf_EnumDescriptorProto_EnumReservedRange_end(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); }
7808 
7809 UPB_INLINE void google_protobuf_EnumDescriptorProto_EnumReservedRange_set_start(google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, int32_t value) {
7810   _upb_sethas(msg, 0);
7811   UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value;
7812 }
7813 UPB_INLINE void google_protobuf_EnumDescriptorProto_EnumReservedRange_set_end(google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, int32_t value) {
7814   _upb_sethas(msg, 1);
7815   UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value;
7816 }
7817 
7818 
7819 /* google.protobuf.EnumValueDescriptorProto */
7820 
7821 UPB_INLINE google_protobuf_EnumValueDescriptorProto *google_protobuf_EnumValueDescriptorProto_new(upb_arena *arena) {
7822   return (google_protobuf_EnumValueDescriptorProto *)upb_msg_new(&google_protobuf_EnumValueDescriptorProto_msginit, arena);
7823 }
7824 UPB_INLINE google_protobuf_EnumValueDescriptorProto *google_protobuf_EnumValueDescriptorProto_parsenew(upb_strview buf, upb_arena *arena) {
7825   google_protobuf_EnumValueDescriptorProto *ret = google_protobuf_EnumValueDescriptorProto_new(arena);
7826   return (ret && upb_decode(buf, ret, &google_protobuf_EnumValueDescriptorProto_msginit)) ? ret : NULL;
7827 }
7828 UPB_INLINE char *google_protobuf_EnumValueDescriptorProto_serialize(const google_protobuf_EnumValueDescriptorProto *msg, upb_arena *arena, size_t *len) {
7829   return upb_encode(msg, &google_protobuf_EnumValueDescriptorProto_msginit, arena, len);
7830 }
7831 
7832 UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_name(const google_protobuf_EnumValueDescriptorProto *msg) { return _upb_has_field(msg, 1); }
7833 UPB_INLINE upb_strview google_protobuf_EnumValueDescriptorProto_name(const google_protobuf_EnumValueDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(8, 8)); }
7834 UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_number(const google_protobuf_EnumValueDescriptorProto *msg) { return _upb_has_field(msg, 0); }
7835 UPB_INLINE int32_t google_protobuf_EnumValueDescriptorProto_number(const google_protobuf_EnumValueDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); }
7836 UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_options(const google_protobuf_EnumValueDescriptorProto *msg) { return _upb_has_field(msg, 2); }
7837 UPB_INLINE const google_protobuf_EnumValueOptions* google_protobuf_EnumValueDescriptorProto_options(const google_protobuf_EnumValueDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_EnumValueOptions*, UPB_SIZE(16, 24)); }
7838 
7839 UPB_INLINE void google_protobuf_EnumValueDescriptorProto_set_name(google_protobuf_EnumValueDescriptorProto *msg, upb_strview value) {
7840   _upb_sethas(msg, 1);
7841   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(8, 8)) = value;
7842 }
7843 UPB_INLINE void google_protobuf_EnumValueDescriptorProto_set_number(google_protobuf_EnumValueDescriptorProto *msg, int32_t value) {
7844   _upb_sethas(msg, 0);
7845   UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value;
7846 }
7847 UPB_INLINE void google_protobuf_EnumValueDescriptorProto_set_options(google_protobuf_EnumValueDescriptorProto *msg, google_protobuf_EnumValueOptions* value) {
7848   _upb_sethas(msg, 2);
7849   UPB_FIELD_AT(msg, google_protobuf_EnumValueOptions*, UPB_SIZE(16, 24)) = value;
7850 }
7851 UPB_INLINE struct google_protobuf_EnumValueOptions* google_protobuf_EnumValueDescriptorProto_mutable_options(google_protobuf_EnumValueDescriptorProto *msg, upb_arena *arena) {
7852   struct google_protobuf_EnumValueOptions* sub = (struct google_protobuf_EnumValueOptions*)google_protobuf_EnumValueDescriptorProto_options(msg);
7853   if (sub == NULL) {
7854     sub = (struct google_protobuf_EnumValueOptions*)upb_msg_new(&google_protobuf_EnumValueOptions_msginit, arena);
7855     if (!sub) return NULL;
7856     google_protobuf_EnumValueDescriptorProto_set_options(msg, sub);
7857   }
7858   return sub;
7859 }
7860 
7861 
7862 /* google.protobuf.ServiceDescriptorProto */
7863 
7864 UPB_INLINE google_protobuf_ServiceDescriptorProto *google_protobuf_ServiceDescriptorProto_new(upb_arena *arena) {
7865   return (google_protobuf_ServiceDescriptorProto *)upb_msg_new(&google_protobuf_ServiceDescriptorProto_msginit, arena);
7866 }
7867 UPB_INLINE google_protobuf_ServiceDescriptorProto *google_protobuf_ServiceDescriptorProto_parsenew(upb_strview buf, upb_arena *arena) {
7868   google_protobuf_ServiceDescriptorProto *ret = google_protobuf_ServiceDescriptorProto_new(arena);
7869   return (ret && upb_decode(buf, ret, &google_protobuf_ServiceDescriptorProto_msginit)) ? ret : NULL;
7870 }
7871 UPB_INLINE char *google_protobuf_ServiceDescriptorProto_serialize(const google_protobuf_ServiceDescriptorProto *msg, upb_arena *arena, size_t *len) {
7872   return upb_encode(msg, &google_protobuf_ServiceDescriptorProto_msginit, arena, len);
7873 }
7874 
7875 UPB_INLINE bool google_protobuf_ServiceDescriptorProto_has_name(const google_protobuf_ServiceDescriptorProto *msg) { return _upb_has_field(msg, 0); }
7876 UPB_INLINE upb_strview google_protobuf_ServiceDescriptorProto_name(const google_protobuf_ServiceDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
7877 UPB_INLINE const google_protobuf_MethodDescriptorProto* const* google_protobuf_ServiceDescriptorProto_method(const google_protobuf_ServiceDescriptorProto *msg, size_t *len) { return (const google_protobuf_MethodDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(16, 32), len); }
7878 UPB_INLINE bool google_protobuf_ServiceDescriptorProto_has_options(const google_protobuf_ServiceDescriptorProto *msg) { return _upb_has_field(msg, 1); }
7879 UPB_INLINE const google_protobuf_ServiceOptions* google_protobuf_ServiceDescriptorProto_options(const google_protobuf_ServiceDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_ServiceOptions*, UPB_SIZE(12, 24)); }
7880 
7881 UPB_INLINE void google_protobuf_ServiceDescriptorProto_set_name(google_protobuf_ServiceDescriptorProto *msg, upb_strview value) {
7882   _upb_sethas(msg, 0);
7883   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
7884 }
7885 UPB_INLINE google_protobuf_MethodDescriptorProto** google_protobuf_ServiceDescriptorProto_mutable_method(google_protobuf_ServiceDescriptorProto *msg, size_t *len) {
7886   return (google_protobuf_MethodDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(16, 32), len);
7887 }
7888 UPB_INLINE google_protobuf_MethodDescriptorProto** google_protobuf_ServiceDescriptorProto_resize_method(google_protobuf_ServiceDescriptorProto *msg, size_t len, upb_arena *arena) {
7889   return (google_protobuf_MethodDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(16, 32), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
7890 }
7891 UPB_INLINE struct google_protobuf_MethodDescriptorProto* google_protobuf_ServiceDescriptorProto_add_method(google_protobuf_ServiceDescriptorProto *msg, upb_arena *arena) {
7892   struct google_protobuf_MethodDescriptorProto* sub = (struct google_protobuf_MethodDescriptorProto*)upb_msg_new(&google_protobuf_MethodDescriptorProto_msginit, arena);
7893   bool ok = _upb_array_append_accessor(
7894       msg, UPB_SIZE(16, 32), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
7895   if (!ok) return NULL;
7896   return sub;
7897 }
7898 UPB_INLINE void google_protobuf_ServiceDescriptorProto_set_options(google_protobuf_ServiceDescriptorProto *msg, google_protobuf_ServiceOptions* value) {
7899   _upb_sethas(msg, 1);
7900   UPB_FIELD_AT(msg, google_protobuf_ServiceOptions*, UPB_SIZE(12, 24)) = value;
7901 }
7902 UPB_INLINE struct google_protobuf_ServiceOptions* google_protobuf_ServiceDescriptorProto_mutable_options(google_protobuf_ServiceDescriptorProto *msg, upb_arena *arena) {
7903   struct google_protobuf_ServiceOptions* sub = (struct google_protobuf_ServiceOptions*)google_protobuf_ServiceDescriptorProto_options(msg);
7904   if (sub == NULL) {
7905     sub = (struct google_protobuf_ServiceOptions*)upb_msg_new(&google_protobuf_ServiceOptions_msginit, arena);
7906     if (!sub) return NULL;
7907     google_protobuf_ServiceDescriptorProto_set_options(msg, sub);
7908   }
7909   return sub;
7910 }
7911 
7912 
7913 /* google.protobuf.MethodDescriptorProto */
7914 
7915 UPB_INLINE google_protobuf_MethodDescriptorProto *google_protobuf_MethodDescriptorProto_new(upb_arena *arena) {
7916   return (google_protobuf_MethodDescriptorProto *)upb_msg_new(&google_protobuf_MethodDescriptorProto_msginit, arena);
7917 }
7918 UPB_INLINE google_protobuf_MethodDescriptorProto *google_protobuf_MethodDescriptorProto_parsenew(upb_strview buf, upb_arena *arena) {
7919   google_protobuf_MethodDescriptorProto *ret = google_protobuf_MethodDescriptorProto_new(arena);
7920   return (ret && upb_decode(buf, ret, &google_protobuf_MethodDescriptorProto_msginit)) ? ret : NULL;
7921 }
7922 UPB_INLINE char *google_protobuf_MethodDescriptorProto_serialize(const google_protobuf_MethodDescriptorProto *msg, upb_arena *arena, size_t *len) {
7923   return upb_encode(msg, &google_protobuf_MethodDescriptorProto_msginit, arena, len);
7924 }
7925 
7926 UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_name(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 2); }
7927 UPB_INLINE upb_strview google_protobuf_MethodDescriptorProto_name(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
7928 UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_input_type(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 3); }
7929 UPB_INLINE upb_strview google_protobuf_MethodDescriptorProto_input_type(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)); }
7930 UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_output_type(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 4); }
7931 UPB_INLINE upb_strview google_protobuf_MethodDescriptorProto_output_type(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(20, 40)); }
7932 UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_options(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 5); }
7933 UPB_INLINE const google_protobuf_MethodOptions* google_protobuf_MethodDescriptorProto_options(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_MethodOptions*, UPB_SIZE(28, 56)); }
7934 UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_client_streaming(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 0); }
7935 UPB_INLINE bool google_protobuf_MethodDescriptorProto_client_streaming(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); }
7936 UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_server_streaming(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 1); }
7937 UPB_INLINE bool google_protobuf_MethodDescriptorProto_server_streaming(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)); }
7938 
7939 UPB_INLINE void google_protobuf_MethodDescriptorProto_set_name(google_protobuf_MethodDescriptorProto *msg, upb_strview value) {
7940   _upb_sethas(msg, 2);
7941   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
7942 }
7943 UPB_INLINE void google_protobuf_MethodDescriptorProto_set_input_type(google_protobuf_MethodDescriptorProto *msg, upb_strview value) {
7944   _upb_sethas(msg, 3);
7945   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)) = value;
7946 }
7947 UPB_INLINE void google_protobuf_MethodDescriptorProto_set_output_type(google_protobuf_MethodDescriptorProto *msg, upb_strview value) {
7948   _upb_sethas(msg, 4);
7949   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(20, 40)) = value;
7950 }
7951 UPB_INLINE void google_protobuf_MethodDescriptorProto_set_options(google_protobuf_MethodDescriptorProto *msg, google_protobuf_MethodOptions* value) {
7952   _upb_sethas(msg, 5);
7953   UPB_FIELD_AT(msg, google_protobuf_MethodOptions*, UPB_SIZE(28, 56)) = value;
7954 }
7955 UPB_INLINE struct google_protobuf_MethodOptions* google_protobuf_MethodDescriptorProto_mutable_options(google_protobuf_MethodDescriptorProto *msg, upb_arena *arena) {
7956   struct google_protobuf_MethodOptions* sub = (struct google_protobuf_MethodOptions*)google_protobuf_MethodDescriptorProto_options(msg);
7957   if (sub == NULL) {
7958     sub = (struct google_protobuf_MethodOptions*)upb_msg_new(&google_protobuf_MethodOptions_msginit, arena);
7959     if (!sub) return NULL;
7960     google_protobuf_MethodDescriptorProto_set_options(msg, sub);
7961   }
7962   return sub;
7963 }
7964 UPB_INLINE void google_protobuf_MethodDescriptorProto_set_client_streaming(google_protobuf_MethodDescriptorProto *msg, bool value) {
7965   _upb_sethas(msg, 0);
7966   UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value;
7967 }
7968 UPB_INLINE void google_protobuf_MethodDescriptorProto_set_server_streaming(google_protobuf_MethodDescriptorProto *msg, bool value) {
7969   _upb_sethas(msg, 1);
7970   UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)) = value;
7971 }
7972 
7973 
7974 /* google.protobuf.FileOptions */
7975 
7976 UPB_INLINE google_protobuf_FileOptions *google_protobuf_FileOptions_new(upb_arena *arena) {
7977   return (google_protobuf_FileOptions *)upb_msg_new(&google_protobuf_FileOptions_msginit, arena);
7978 }
7979 UPB_INLINE google_protobuf_FileOptions *google_protobuf_FileOptions_parsenew(upb_strview buf, upb_arena *arena) {
7980   google_protobuf_FileOptions *ret = google_protobuf_FileOptions_new(arena);
7981   return (ret && upb_decode(buf, ret, &google_protobuf_FileOptions_msginit)) ? ret : NULL;
7982 }
7983 UPB_INLINE char *google_protobuf_FileOptions_serialize(const google_protobuf_FileOptions *msg, upb_arena *arena, size_t *len) {
7984   return upb_encode(msg, &google_protobuf_FileOptions_msginit, arena, len);
7985 }
7986 
7987 UPB_INLINE bool google_protobuf_FileOptions_has_java_package(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 10); }
7988 UPB_INLINE upb_strview google_protobuf_FileOptions_java_package(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(28, 32)); }
7989 UPB_INLINE bool google_protobuf_FileOptions_has_java_outer_classname(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 11); }
7990 UPB_INLINE upb_strview google_protobuf_FileOptions_java_outer_classname(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(36, 48)); }
7991 UPB_INLINE bool google_protobuf_FileOptions_has_optimize_for(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 0); }
7992 UPB_INLINE google_protobuf_FileOptions_OptimizeMode google_protobuf_FileOptions_optimize_for(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, google_protobuf_FileOptions_OptimizeMode, UPB_SIZE(8, 8)); }
7993 UPB_INLINE bool google_protobuf_FileOptions_has_java_multiple_files(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 1); }
7994 UPB_INLINE bool google_protobuf_FileOptions_java_multiple_files(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)); }
7995 UPB_INLINE bool google_protobuf_FileOptions_has_go_package(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 12); }
7996 UPB_INLINE upb_strview google_protobuf_FileOptions_go_package(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(44, 64)); }
7997 UPB_INLINE bool google_protobuf_FileOptions_has_cc_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 2); }
7998 UPB_INLINE bool google_protobuf_FileOptions_cc_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(17, 17)); }
7999 UPB_INLINE bool google_protobuf_FileOptions_has_java_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 3); }
8000 UPB_INLINE bool google_protobuf_FileOptions_java_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(18, 18)); }
8001 UPB_INLINE bool google_protobuf_FileOptions_has_py_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 4); }
8002 UPB_INLINE bool google_protobuf_FileOptions_py_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(19, 19)); }
8003 UPB_INLINE bool google_protobuf_FileOptions_has_java_generate_equals_and_hash(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 5); }
8004 UPB_INLINE bool google_protobuf_FileOptions_java_generate_equals_and_hash(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(20, 20)); }
8005 UPB_INLINE bool google_protobuf_FileOptions_has_deprecated(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 6); }
8006 UPB_INLINE bool google_protobuf_FileOptions_deprecated(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(21, 21)); }
8007 UPB_INLINE bool google_protobuf_FileOptions_has_java_string_check_utf8(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 7); }
8008 UPB_INLINE bool google_protobuf_FileOptions_java_string_check_utf8(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(22, 22)); }
8009 UPB_INLINE bool google_protobuf_FileOptions_has_cc_enable_arenas(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 8); }
8010 UPB_INLINE bool google_protobuf_FileOptions_cc_enable_arenas(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(23, 23)); }
8011 UPB_INLINE bool google_protobuf_FileOptions_has_objc_class_prefix(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 13); }
8012 UPB_INLINE upb_strview google_protobuf_FileOptions_objc_class_prefix(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(52, 80)); }
8013 UPB_INLINE bool google_protobuf_FileOptions_has_csharp_namespace(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 14); }
8014 UPB_INLINE upb_strview google_protobuf_FileOptions_csharp_namespace(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(60, 96)); }
8015 UPB_INLINE bool google_protobuf_FileOptions_has_swift_prefix(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 15); }
8016 UPB_INLINE upb_strview google_protobuf_FileOptions_swift_prefix(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(68, 112)); }
8017 UPB_INLINE bool google_protobuf_FileOptions_has_php_class_prefix(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 16); }
8018 UPB_INLINE upb_strview google_protobuf_FileOptions_php_class_prefix(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(76, 128)); }
8019 UPB_INLINE bool google_protobuf_FileOptions_has_php_namespace(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 17); }
8020 UPB_INLINE upb_strview google_protobuf_FileOptions_php_namespace(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(84, 144)); }
8021 UPB_INLINE bool google_protobuf_FileOptions_has_php_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 9); }
8022 UPB_INLINE bool google_protobuf_FileOptions_php_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)); }
8023 UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_FileOptions_uninterpreted_option(const google_protobuf_FileOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(92, 160), len); }
8024 
8025 UPB_INLINE void google_protobuf_FileOptions_set_java_package(google_protobuf_FileOptions *msg, upb_strview value) {
8026   _upb_sethas(msg, 10);
8027   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(28, 32)) = value;
8028 }
8029 UPB_INLINE void google_protobuf_FileOptions_set_java_outer_classname(google_protobuf_FileOptions *msg, upb_strview value) {
8030   _upb_sethas(msg, 11);
8031   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(36, 48)) = value;
8032 }
8033 UPB_INLINE void google_protobuf_FileOptions_set_optimize_for(google_protobuf_FileOptions *msg, google_protobuf_FileOptions_OptimizeMode value) {
8034   _upb_sethas(msg, 0);
8035   UPB_FIELD_AT(msg, google_protobuf_FileOptions_OptimizeMode, UPB_SIZE(8, 8)) = value;
8036 }
8037 UPB_INLINE void google_protobuf_FileOptions_set_java_multiple_files(google_protobuf_FileOptions *msg, bool value) {
8038   _upb_sethas(msg, 1);
8039   UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)) = value;
8040 }
8041 UPB_INLINE void google_protobuf_FileOptions_set_go_package(google_protobuf_FileOptions *msg, upb_strview value) {
8042   _upb_sethas(msg, 12);
8043   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(44, 64)) = value;
8044 }
8045 UPB_INLINE void google_protobuf_FileOptions_set_cc_generic_services(google_protobuf_FileOptions *msg, bool value) {
8046   _upb_sethas(msg, 2);
8047   UPB_FIELD_AT(msg, bool, UPB_SIZE(17, 17)) = value;
8048 }
8049 UPB_INLINE void google_protobuf_FileOptions_set_java_generic_services(google_protobuf_FileOptions *msg, bool value) {
8050   _upb_sethas(msg, 3);
8051   UPB_FIELD_AT(msg, bool, UPB_SIZE(18, 18)) = value;
8052 }
8053 UPB_INLINE void google_protobuf_FileOptions_set_py_generic_services(google_protobuf_FileOptions *msg, bool value) {
8054   _upb_sethas(msg, 4);
8055   UPB_FIELD_AT(msg, bool, UPB_SIZE(19, 19)) = value;
8056 }
8057 UPB_INLINE void google_protobuf_FileOptions_set_java_generate_equals_and_hash(google_protobuf_FileOptions *msg, bool value) {
8058   _upb_sethas(msg, 5);
8059   UPB_FIELD_AT(msg, bool, UPB_SIZE(20, 20)) = value;
8060 }
8061 UPB_INLINE void google_protobuf_FileOptions_set_deprecated(google_protobuf_FileOptions *msg, bool value) {
8062   _upb_sethas(msg, 6);
8063   UPB_FIELD_AT(msg, bool, UPB_SIZE(21, 21)) = value;
8064 }
8065 UPB_INLINE void google_protobuf_FileOptions_set_java_string_check_utf8(google_protobuf_FileOptions *msg, bool value) {
8066   _upb_sethas(msg, 7);
8067   UPB_FIELD_AT(msg, bool, UPB_SIZE(22, 22)) = value;
8068 }
8069 UPB_INLINE void google_protobuf_FileOptions_set_cc_enable_arenas(google_protobuf_FileOptions *msg, bool value) {
8070   _upb_sethas(msg, 8);
8071   UPB_FIELD_AT(msg, bool, UPB_SIZE(23, 23)) = value;
8072 }
8073 UPB_INLINE void google_protobuf_FileOptions_set_objc_class_prefix(google_protobuf_FileOptions *msg, upb_strview value) {
8074   _upb_sethas(msg, 13);
8075   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(52, 80)) = value;
8076 }
8077 UPB_INLINE void google_protobuf_FileOptions_set_csharp_namespace(google_protobuf_FileOptions *msg, upb_strview value) {
8078   _upb_sethas(msg, 14);
8079   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(60, 96)) = value;
8080 }
8081 UPB_INLINE void google_protobuf_FileOptions_set_swift_prefix(google_protobuf_FileOptions *msg, upb_strview value) {
8082   _upb_sethas(msg, 15);
8083   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(68, 112)) = value;
8084 }
8085 UPB_INLINE void google_protobuf_FileOptions_set_php_class_prefix(google_protobuf_FileOptions *msg, upb_strview value) {
8086   _upb_sethas(msg, 16);
8087   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(76, 128)) = value;
8088 }
8089 UPB_INLINE void google_protobuf_FileOptions_set_php_namespace(google_protobuf_FileOptions *msg, upb_strview value) {
8090   _upb_sethas(msg, 17);
8091   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(84, 144)) = value;
8092 }
8093 UPB_INLINE void google_protobuf_FileOptions_set_php_generic_services(google_protobuf_FileOptions *msg, bool value) {
8094   _upb_sethas(msg, 9);
8095   UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)) = value;
8096 }
8097 UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FileOptions_mutable_uninterpreted_option(google_protobuf_FileOptions *msg, size_t *len) {
8098   return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(92, 160), len);
8099 }
8100 UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FileOptions_resize_uninterpreted_option(google_protobuf_FileOptions *msg, size_t len, upb_arena *arena) {
8101   return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(92, 160), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
8102 }
8103 UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_FileOptions_add_uninterpreted_option(google_protobuf_FileOptions *msg, upb_arena *arena) {
8104   struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
8105   bool ok = _upb_array_append_accessor(
8106       msg, UPB_SIZE(92, 160), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
8107   if (!ok) return NULL;
8108   return sub;
8109 }
8110 
8111 
8112 /* google.protobuf.MessageOptions */
8113 
8114 UPB_INLINE google_protobuf_MessageOptions *google_protobuf_MessageOptions_new(upb_arena *arena) {
8115   return (google_protobuf_MessageOptions *)upb_msg_new(&google_protobuf_MessageOptions_msginit, arena);
8116 }
8117 UPB_INLINE google_protobuf_MessageOptions *google_protobuf_MessageOptions_parsenew(upb_strview buf, upb_arena *arena) {
8118   google_protobuf_MessageOptions *ret = google_protobuf_MessageOptions_new(arena);
8119   return (ret && upb_decode(buf, ret, &google_protobuf_MessageOptions_msginit)) ? ret : NULL;
8120 }
8121 UPB_INLINE char *google_protobuf_MessageOptions_serialize(const google_protobuf_MessageOptions *msg, upb_arena *arena, size_t *len) {
8122   return upb_encode(msg, &google_protobuf_MessageOptions_msginit, arena, len);
8123 }
8124 
8125 UPB_INLINE bool google_protobuf_MessageOptions_has_message_set_wire_format(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 0); }
8126 UPB_INLINE bool google_protobuf_MessageOptions_message_set_wire_format(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); }
8127 UPB_INLINE bool google_protobuf_MessageOptions_has_no_standard_descriptor_accessor(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 1); }
8128 UPB_INLINE bool google_protobuf_MessageOptions_no_standard_descriptor_accessor(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)); }
8129 UPB_INLINE bool google_protobuf_MessageOptions_has_deprecated(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 2); }
8130 UPB_INLINE bool google_protobuf_MessageOptions_deprecated(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(3, 3)); }
8131 UPB_INLINE bool google_protobuf_MessageOptions_has_map_entry(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 3); }
8132 UPB_INLINE bool google_protobuf_MessageOptions_map_entry(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(4, 4)); }
8133 UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_MessageOptions_uninterpreted_option(const google_protobuf_MessageOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(8, 8), len); }
8134 
8135 UPB_INLINE void google_protobuf_MessageOptions_set_message_set_wire_format(google_protobuf_MessageOptions *msg, bool value) {
8136   _upb_sethas(msg, 0);
8137   UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value;
8138 }
8139 UPB_INLINE void google_protobuf_MessageOptions_set_no_standard_descriptor_accessor(google_protobuf_MessageOptions *msg, bool value) {
8140   _upb_sethas(msg, 1);
8141   UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)) = value;
8142 }
8143 UPB_INLINE void google_protobuf_MessageOptions_set_deprecated(google_protobuf_MessageOptions *msg, bool value) {
8144   _upb_sethas(msg, 2);
8145   UPB_FIELD_AT(msg, bool, UPB_SIZE(3, 3)) = value;
8146 }
8147 UPB_INLINE void google_protobuf_MessageOptions_set_map_entry(google_protobuf_MessageOptions *msg, bool value) {
8148   _upb_sethas(msg, 3);
8149   UPB_FIELD_AT(msg, bool, UPB_SIZE(4, 4)) = value;
8150 }
8151 UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MessageOptions_mutable_uninterpreted_option(google_protobuf_MessageOptions *msg, size_t *len) {
8152   return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(8, 8), len);
8153 }
8154 UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MessageOptions_resize_uninterpreted_option(google_protobuf_MessageOptions *msg, size_t len, upb_arena *arena) {
8155   return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(8, 8), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
8156 }
8157 UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_MessageOptions_add_uninterpreted_option(google_protobuf_MessageOptions *msg, upb_arena *arena) {
8158   struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
8159   bool ok = _upb_array_append_accessor(
8160       msg, UPB_SIZE(8, 8), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
8161   if (!ok) return NULL;
8162   return sub;
8163 }
8164 
8165 
8166 /* google.protobuf.FieldOptions */
8167 
8168 UPB_INLINE google_protobuf_FieldOptions *google_protobuf_FieldOptions_new(upb_arena *arena) {
8169   return (google_protobuf_FieldOptions *)upb_msg_new(&google_protobuf_FieldOptions_msginit, arena);
8170 }
8171 UPB_INLINE google_protobuf_FieldOptions *google_protobuf_FieldOptions_parsenew(upb_strview buf, upb_arena *arena) {
8172   google_protobuf_FieldOptions *ret = google_protobuf_FieldOptions_new(arena);
8173   return (ret && upb_decode(buf, ret, &google_protobuf_FieldOptions_msginit)) ? ret : NULL;
8174 }
8175 UPB_INLINE char *google_protobuf_FieldOptions_serialize(const google_protobuf_FieldOptions *msg, upb_arena *arena, size_t *len) {
8176   return upb_encode(msg, &google_protobuf_FieldOptions_msginit, arena, len);
8177 }
8178 
8179 UPB_INLINE bool google_protobuf_FieldOptions_has_ctype(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 0); }
8180 UPB_INLINE google_protobuf_FieldOptions_CType google_protobuf_FieldOptions_ctype(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, google_protobuf_FieldOptions_CType, UPB_SIZE(8, 8)); }
8181 UPB_INLINE bool google_protobuf_FieldOptions_has_packed(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 2); }
8182 UPB_INLINE bool google_protobuf_FieldOptions_packed(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)); }
8183 UPB_INLINE bool google_protobuf_FieldOptions_has_deprecated(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 3); }
8184 UPB_INLINE bool google_protobuf_FieldOptions_deprecated(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(25, 25)); }
8185 UPB_INLINE bool google_protobuf_FieldOptions_has_lazy(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 4); }
8186 UPB_INLINE bool google_protobuf_FieldOptions_lazy(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(26, 26)); }
8187 UPB_INLINE bool google_protobuf_FieldOptions_has_jstype(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 1); }
8188 UPB_INLINE google_protobuf_FieldOptions_JSType google_protobuf_FieldOptions_jstype(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, google_protobuf_FieldOptions_JSType, UPB_SIZE(16, 16)); }
8189 UPB_INLINE bool google_protobuf_FieldOptions_has_weak(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 5); }
8190 UPB_INLINE bool google_protobuf_FieldOptions_weak(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(27, 27)); }
8191 UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_FieldOptions_uninterpreted_option(const google_protobuf_FieldOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(28, 32), len); }
8192 
8193 UPB_INLINE void google_protobuf_FieldOptions_set_ctype(google_protobuf_FieldOptions *msg, google_protobuf_FieldOptions_CType value) {
8194   _upb_sethas(msg, 0);
8195   UPB_FIELD_AT(msg, google_protobuf_FieldOptions_CType, UPB_SIZE(8, 8)) = value;
8196 }
8197 UPB_INLINE void google_protobuf_FieldOptions_set_packed(google_protobuf_FieldOptions *msg, bool value) {
8198   _upb_sethas(msg, 2);
8199   UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)) = value;
8200 }
8201 UPB_INLINE void google_protobuf_FieldOptions_set_deprecated(google_protobuf_FieldOptions *msg, bool value) {
8202   _upb_sethas(msg, 3);
8203   UPB_FIELD_AT(msg, bool, UPB_SIZE(25, 25)) = value;
8204 }
8205 UPB_INLINE void google_protobuf_FieldOptions_set_lazy(google_protobuf_FieldOptions *msg, bool value) {
8206   _upb_sethas(msg, 4);
8207   UPB_FIELD_AT(msg, bool, UPB_SIZE(26, 26)) = value;
8208 }
8209 UPB_INLINE void google_protobuf_FieldOptions_set_jstype(google_protobuf_FieldOptions *msg, google_protobuf_FieldOptions_JSType value) {
8210   _upb_sethas(msg, 1);
8211   UPB_FIELD_AT(msg, google_protobuf_FieldOptions_JSType, UPB_SIZE(16, 16)) = value;
8212 }
8213 UPB_INLINE void google_protobuf_FieldOptions_set_weak(google_protobuf_FieldOptions *msg, bool value) {
8214   _upb_sethas(msg, 5);
8215   UPB_FIELD_AT(msg, bool, UPB_SIZE(27, 27)) = value;
8216 }
8217 UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FieldOptions_mutable_uninterpreted_option(google_protobuf_FieldOptions *msg, size_t *len) {
8218   return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(28, 32), len);
8219 }
8220 UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FieldOptions_resize_uninterpreted_option(google_protobuf_FieldOptions *msg, size_t len, upb_arena *arena) {
8221   return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(28, 32), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
8222 }
8223 UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_FieldOptions_add_uninterpreted_option(google_protobuf_FieldOptions *msg, upb_arena *arena) {
8224   struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
8225   bool ok = _upb_array_append_accessor(
8226       msg, UPB_SIZE(28, 32), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
8227   if (!ok) return NULL;
8228   return sub;
8229 }
8230 
8231 
8232 /* google.protobuf.OneofOptions */
8233 
8234 UPB_INLINE google_protobuf_OneofOptions *google_protobuf_OneofOptions_new(upb_arena *arena) {
8235   return (google_protobuf_OneofOptions *)upb_msg_new(&google_protobuf_OneofOptions_msginit, arena);
8236 }
8237 UPB_INLINE google_protobuf_OneofOptions *google_protobuf_OneofOptions_parsenew(upb_strview buf, upb_arena *arena) {
8238   google_protobuf_OneofOptions *ret = google_protobuf_OneofOptions_new(arena);
8239   return (ret && upb_decode(buf, ret, &google_protobuf_OneofOptions_msginit)) ? ret : NULL;
8240 }
8241 UPB_INLINE char *google_protobuf_OneofOptions_serialize(const google_protobuf_OneofOptions *msg, upb_arena *arena, size_t *len) {
8242   return upb_encode(msg, &google_protobuf_OneofOptions_msginit, arena, len);
8243 }
8244 
8245 UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_OneofOptions_uninterpreted_option(const google_protobuf_OneofOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); }
8246 
8247 UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_OneofOptions_mutable_uninterpreted_option(google_protobuf_OneofOptions *msg, size_t *len) {
8248   return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len);
8249 }
8250 UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_OneofOptions_resize_uninterpreted_option(google_protobuf_OneofOptions *msg, size_t len, upb_arena *arena) {
8251   return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
8252 }
8253 UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_OneofOptions_add_uninterpreted_option(google_protobuf_OneofOptions *msg, upb_arena *arena) {
8254   struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
8255   bool ok = _upb_array_append_accessor(
8256       msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
8257   if (!ok) return NULL;
8258   return sub;
8259 }
8260 
8261 
8262 /* google.protobuf.EnumOptions */
8263 
8264 UPB_INLINE google_protobuf_EnumOptions *google_protobuf_EnumOptions_new(upb_arena *arena) {
8265   return (google_protobuf_EnumOptions *)upb_msg_new(&google_protobuf_EnumOptions_msginit, arena);
8266 }
8267 UPB_INLINE google_protobuf_EnumOptions *google_protobuf_EnumOptions_parsenew(upb_strview buf, upb_arena *arena) {
8268   google_protobuf_EnumOptions *ret = google_protobuf_EnumOptions_new(arena);
8269   return (ret && upb_decode(buf, ret, &google_protobuf_EnumOptions_msginit)) ? ret : NULL;
8270 }
8271 UPB_INLINE char *google_protobuf_EnumOptions_serialize(const google_protobuf_EnumOptions *msg, upb_arena *arena, size_t *len) {
8272   return upb_encode(msg, &google_protobuf_EnumOptions_msginit, arena, len);
8273 }
8274 
8275 UPB_INLINE bool google_protobuf_EnumOptions_has_allow_alias(const google_protobuf_EnumOptions *msg) { return _upb_has_field(msg, 0); }
8276 UPB_INLINE bool google_protobuf_EnumOptions_allow_alias(const google_protobuf_EnumOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); }
8277 UPB_INLINE bool google_protobuf_EnumOptions_has_deprecated(const google_protobuf_EnumOptions *msg) { return _upb_has_field(msg, 1); }
8278 UPB_INLINE bool google_protobuf_EnumOptions_deprecated(const google_protobuf_EnumOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)); }
8279 UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_EnumOptions_uninterpreted_option(const google_protobuf_EnumOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(4, 8), len); }
8280 
8281 UPB_INLINE void google_protobuf_EnumOptions_set_allow_alias(google_protobuf_EnumOptions *msg, bool value) {
8282   _upb_sethas(msg, 0);
8283   UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value;
8284 }
8285 UPB_INLINE void google_protobuf_EnumOptions_set_deprecated(google_protobuf_EnumOptions *msg, bool value) {
8286   _upb_sethas(msg, 1);
8287   UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)) = value;
8288 }
8289 UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumOptions_mutable_uninterpreted_option(google_protobuf_EnumOptions *msg, size_t *len) {
8290   return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(4, 8), len);
8291 }
8292 UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumOptions_resize_uninterpreted_option(google_protobuf_EnumOptions *msg, size_t len, upb_arena *arena) {
8293   return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(4, 8), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
8294 }
8295 UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_EnumOptions_add_uninterpreted_option(google_protobuf_EnumOptions *msg, upb_arena *arena) {
8296   struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
8297   bool ok = _upb_array_append_accessor(
8298       msg, UPB_SIZE(4, 8), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
8299   if (!ok) return NULL;
8300   return sub;
8301 }
8302 
8303 
8304 /* google.protobuf.EnumValueOptions */
8305 
8306 UPB_INLINE google_protobuf_EnumValueOptions *google_protobuf_EnumValueOptions_new(upb_arena *arena) {
8307   return (google_protobuf_EnumValueOptions *)upb_msg_new(&google_protobuf_EnumValueOptions_msginit, arena);
8308 }
8309 UPB_INLINE google_protobuf_EnumValueOptions *google_protobuf_EnumValueOptions_parsenew(upb_strview buf, upb_arena *arena) {
8310   google_protobuf_EnumValueOptions *ret = google_protobuf_EnumValueOptions_new(arena);
8311   return (ret && upb_decode(buf, ret, &google_protobuf_EnumValueOptions_msginit)) ? ret : NULL;
8312 }
8313 UPB_INLINE char *google_protobuf_EnumValueOptions_serialize(const google_protobuf_EnumValueOptions *msg, upb_arena *arena, size_t *len) {
8314   return upb_encode(msg, &google_protobuf_EnumValueOptions_msginit, arena, len);
8315 }
8316 
8317 UPB_INLINE bool google_protobuf_EnumValueOptions_has_deprecated(const google_protobuf_EnumValueOptions *msg) { return _upb_has_field(msg, 0); }
8318 UPB_INLINE bool google_protobuf_EnumValueOptions_deprecated(const google_protobuf_EnumValueOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); }
8319 UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_EnumValueOptions_uninterpreted_option(const google_protobuf_EnumValueOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(4, 8), len); }
8320 
8321 UPB_INLINE void google_protobuf_EnumValueOptions_set_deprecated(google_protobuf_EnumValueOptions *msg, bool value) {
8322   _upb_sethas(msg, 0);
8323   UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value;
8324 }
8325 UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumValueOptions_mutable_uninterpreted_option(google_protobuf_EnumValueOptions *msg, size_t *len) {
8326   return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(4, 8), len);
8327 }
8328 UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumValueOptions_resize_uninterpreted_option(google_protobuf_EnumValueOptions *msg, size_t len, upb_arena *arena) {
8329   return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(4, 8), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
8330 }
8331 UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_EnumValueOptions_add_uninterpreted_option(google_protobuf_EnumValueOptions *msg, upb_arena *arena) {
8332   struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
8333   bool ok = _upb_array_append_accessor(
8334       msg, UPB_SIZE(4, 8), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
8335   if (!ok) return NULL;
8336   return sub;
8337 }
8338 
8339 
8340 /* google.protobuf.ServiceOptions */
8341 
8342 UPB_INLINE google_protobuf_ServiceOptions *google_protobuf_ServiceOptions_new(upb_arena *arena) {
8343   return (google_protobuf_ServiceOptions *)upb_msg_new(&google_protobuf_ServiceOptions_msginit, arena);
8344 }
8345 UPB_INLINE google_protobuf_ServiceOptions *google_protobuf_ServiceOptions_parsenew(upb_strview buf, upb_arena *arena) {
8346   google_protobuf_ServiceOptions *ret = google_protobuf_ServiceOptions_new(arena);
8347   return (ret && upb_decode(buf, ret, &google_protobuf_ServiceOptions_msginit)) ? ret : NULL;
8348 }
8349 UPB_INLINE char *google_protobuf_ServiceOptions_serialize(const google_protobuf_ServiceOptions *msg, upb_arena *arena, size_t *len) {
8350   return upb_encode(msg, &google_protobuf_ServiceOptions_msginit, arena, len);
8351 }
8352 
8353 UPB_INLINE bool google_protobuf_ServiceOptions_has_deprecated(const google_protobuf_ServiceOptions *msg) { return _upb_has_field(msg, 0); }
8354 UPB_INLINE bool google_protobuf_ServiceOptions_deprecated(const google_protobuf_ServiceOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); }
8355 UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_ServiceOptions_uninterpreted_option(const google_protobuf_ServiceOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(4, 8), len); }
8356 
8357 UPB_INLINE void google_protobuf_ServiceOptions_set_deprecated(google_protobuf_ServiceOptions *msg, bool value) {
8358   _upb_sethas(msg, 0);
8359   UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value;
8360 }
8361 UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ServiceOptions_mutable_uninterpreted_option(google_protobuf_ServiceOptions *msg, size_t *len) {
8362   return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(4, 8), len);
8363 }
8364 UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ServiceOptions_resize_uninterpreted_option(google_protobuf_ServiceOptions *msg, size_t len, upb_arena *arena) {
8365   return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(4, 8), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
8366 }
8367 UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_ServiceOptions_add_uninterpreted_option(google_protobuf_ServiceOptions *msg, upb_arena *arena) {
8368   struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
8369   bool ok = _upb_array_append_accessor(
8370       msg, UPB_SIZE(4, 8), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
8371   if (!ok) return NULL;
8372   return sub;
8373 }
8374 
8375 
8376 /* google.protobuf.MethodOptions */
8377 
8378 UPB_INLINE google_protobuf_MethodOptions *google_protobuf_MethodOptions_new(upb_arena *arena) {
8379   return (google_protobuf_MethodOptions *)upb_msg_new(&google_protobuf_MethodOptions_msginit, arena);
8380 }
8381 UPB_INLINE google_protobuf_MethodOptions *google_protobuf_MethodOptions_parsenew(upb_strview buf, upb_arena *arena) {
8382   google_protobuf_MethodOptions *ret = google_protobuf_MethodOptions_new(arena);
8383   return (ret && upb_decode(buf, ret, &google_protobuf_MethodOptions_msginit)) ? ret : NULL;
8384 }
8385 UPB_INLINE char *google_protobuf_MethodOptions_serialize(const google_protobuf_MethodOptions *msg, upb_arena *arena, size_t *len) {
8386   return upb_encode(msg, &google_protobuf_MethodOptions_msginit, arena, len);
8387 }
8388 
8389 UPB_INLINE bool google_protobuf_MethodOptions_has_deprecated(const google_protobuf_MethodOptions *msg) { return _upb_has_field(msg, 1); }
8390 UPB_INLINE bool google_protobuf_MethodOptions_deprecated(const google_protobuf_MethodOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)); }
8391 UPB_INLINE bool google_protobuf_MethodOptions_has_idempotency_level(const google_protobuf_MethodOptions *msg) { return _upb_has_field(msg, 0); }
8392 UPB_INLINE google_protobuf_MethodOptions_IdempotencyLevel google_protobuf_MethodOptions_idempotency_level(const google_protobuf_MethodOptions *msg) { return UPB_FIELD_AT(msg, google_protobuf_MethodOptions_IdempotencyLevel, UPB_SIZE(8, 8)); }
8393 UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_MethodOptions_uninterpreted_option(const google_protobuf_MethodOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(20, 24), len); }
8394 
8395 UPB_INLINE void google_protobuf_MethodOptions_set_deprecated(google_protobuf_MethodOptions *msg, bool value) {
8396   _upb_sethas(msg, 1);
8397   UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)) = value;
8398 }
8399 UPB_INLINE void google_protobuf_MethodOptions_set_idempotency_level(google_protobuf_MethodOptions *msg, google_protobuf_MethodOptions_IdempotencyLevel value) {
8400   _upb_sethas(msg, 0);
8401   UPB_FIELD_AT(msg, google_protobuf_MethodOptions_IdempotencyLevel, UPB_SIZE(8, 8)) = value;
8402 }
8403 UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MethodOptions_mutable_uninterpreted_option(google_protobuf_MethodOptions *msg, size_t *len) {
8404   return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 24), len);
8405 }
8406 UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MethodOptions_resize_uninterpreted_option(google_protobuf_MethodOptions *msg, size_t len, upb_arena *arena) {
8407   return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(20, 24), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
8408 }
8409 UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_MethodOptions_add_uninterpreted_option(google_protobuf_MethodOptions *msg, upb_arena *arena) {
8410   struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
8411   bool ok = _upb_array_append_accessor(
8412       msg, UPB_SIZE(20, 24), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
8413   if (!ok) return NULL;
8414   return sub;
8415 }
8416 
8417 
8418 /* google.protobuf.UninterpretedOption */
8419 
8420 UPB_INLINE google_protobuf_UninterpretedOption *google_protobuf_UninterpretedOption_new(upb_arena *arena) {
8421   return (google_protobuf_UninterpretedOption *)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
8422 }
8423 UPB_INLINE google_protobuf_UninterpretedOption *google_protobuf_UninterpretedOption_parsenew(upb_strview buf, upb_arena *arena) {
8424   google_protobuf_UninterpretedOption *ret = google_protobuf_UninterpretedOption_new(arena);
8425   return (ret && upb_decode(buf, ret, &google_protobuf_UninterpretedOption_msginit)) ? ret : NULL;
8426 }
8427 UPB_INLINE char *google_protobuf_UninterpretedOption_serialize(const google_protobuf_UninterpretedOption *msg, upb_arena *arena, size_t *len) {
8428   return upb_encode(msg, &google_protobuf_UninterpretedOption_msginit, arena, len);
8429 }
8430 
8431 UPB_INLINE const google_protobuf_UninterpretedOption_NamePart* const* google_protobuf_UninterpretedOption_name(const google_protobuf_UninterpretedOption *msg, size_t *len) { return (const google_protobuf_UninterpretedOption_NamePart* const*)_upb_array_accessor(msg, UPB_SIZE(56, 80), len); }
8432 UPB_INLINE bool google_protobuf_UninterpretedOption_has_identifier_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 3); }
8433 UPB_INLINE upb_strview google_protobuf_UninterpretedOption_identifier_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(32, 32)); }
8434 UPB_INLINE bool google_protobuf_UninterpretedOption_has_positive_int_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 0); }
8435 UPB_INLINE uint64_t google_protobuf_UninterpretedOption_positive_int_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, uint64_t, UPB_SIZE(8, 8)); }
8436 UPB_INLINE bool google_protobuf_UninterpretedOption_has_negative_int_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 1); }
8437 UPB_INLINE int64_t google_protobuf_UninterpretedOption_negative_int_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, int64_t, UPB_SIZE(16, 16)); }
8438 UPB_INLINE bool google_protobuf_UninterpretedOption_has_double_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 2); }
8439 UPB_INLINE double google_protobuf_UninterpretedOption_double_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, double, UPB_SIZE(24, 24)); }
8440 UPB_INLINE bool google_protobuf_UninterpretedOption_has_string_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 4); }
8441 UPB_INLINE upb_strview google_protobuf_UninterpretedOption_string_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(40, 48)); }
8442 UPB_INLINE bool google_protobuf_UninterpretedOption_has_aggregate_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 5); }
8443 UPB_INLINE upb_strview google_protobuf_UninterpretedOption_aggregate_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(48, 64)); }
8444 
8445 UPB_INLINE google_protobuf_UninterpretedOption_NamePart** google_protobuf_UninterpretedOption_mutable_name(google_protobuf_UninterpretedOption *msg, size_t *len) {
8446   return (google_protobuf_UninterpretedOption_NamePart**)_upb_array_mutable_accessor(msg, UPB_SIZE(56, 80), len);
8447 }
8448 UPB_INLINE google_protobuf_UninterpretedOption_NamePart** google_protobuf_UninterpretedOption_resize_name(google_protobuf_UninterpretedOption *msg, size_t len, upb_arena *arena) {
8449   return (google_protobuf_UninterpretedOption_NamePart**)_upb_array_resize_accessor(msg, UPB_SIZE(56, 80), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
8450 }
8451 UPB_INLINE struct google_protobuf_UninterpretedOption_NamePart* google_protobuf_UninterpretedOption_add_name(google_protobuf_UninterpretedOption *msg, upb_arena *arena) {
8452   struct google_protobuf_UninterpretedOption_NamePart* sub = (struct google_protobuf_UninterpretedOption_NamePart*)upb_msg_new(&google_protobuf_UninterpretedOption_NamePart_msginit, arena);
8453   bool ok = _upb_array_append_accessor(
8454       msg, UPB_SIZE(56, 80), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
8455   if (!ok) return NULL;
8456   return sub;
8457 }
8458 UPB_INLINE void google_protobuf_UninterpretedOption_set_identifier_value(google_protobuf_UninterpretedOption *msg, upb_strview value) {
8459   _upb_sethas(msg, 3);
8460   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(32, 32)) = value;
8461 }
8462 UPB_INLINE void google_protobuf_UninterpretedOption_set_positive_int_value(google_protobuf_UninterpretedOption *msg, uint64_t value) {
8463   _upb_sethas(msg, 0);
8464   UPB_FIELD_AT(msg, uint64_t, UPB_SIZE(8, 8)) = value;
8465 }
8466 UPB_INLINE void google_protobuf_UninterpretedOption_set_negative_int_value(google_protobuf_UninterpretedOption *msg, int64_t value) {
8467   _upb_sethas(msg, 1);
8468   UPB_FIELD_AT(msg, int64_t, UPB_SIZE(16, 16)) = value;
8469 }
8470 UPB_INLINE void google_protobuf_UninterpretedOption_set_double_value(google_protobuf_UninterpretedOption *msg, double value) {
8471   _upb_sethas(msg, 2);
8472   UPB_FIELD_AT(msg, double, UPB_SIZE(24, 24)) = value;
8473 }
8474 UPB_INLINE void google_protobuf_UninterpretedOption_set_string_value(google_protobuf_UninterpretedOption *msg, upb_strview value) {
8475   _upb_sethas(msg, 4);
8476   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(40, 48)) = value;
8477 }
8478 UPB_INLINE void google_protobuf_UninterpretedOption_set_aggregate_value(google_protobuf_UninterpretedOption *msg, upb_strview value) {
8479   _upb_sethas(msg, 5);
8480   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(48, 64)) = value;
8481 }
8482 
8483 
8484 /* google.protobuf.UninterpretedOption.NamePart */
8485 
8486 UPB_INLINE google_protobuf_UninterpretedOption_NamePart *google_protobuf_UninterpretedOption_NamePart_new(upb_arena *arena) {
8487   return (google_protobuf_UninterpretedOption_NamePart *)upb_msg_new(&google_protobuf_UninterpretedOption_NamePart_msginit, arena);
8488 }
8489 UPB_INLINE google_protobuf_UninterpretedOption_NamePart *google_protobuf_UninterpretedOption_NamePart_parsenew(upb_strview buf, upb_arena *arena) {
8490   google_protobuf_UninterpretedOption_NamePart *ret = google_protobuf_UninterpretedOption_NamePart_new(arena);
8491   return (ret && upb_decode(buf, ret, &google_protobuf_UninterpretedOption_NamePart_msginit)) ? ret : NULL;
8492 }
8493 UPB_INLINE char *google_protobuf_UninterpretedOption_NamePart_serialize(const google_protobuf_UninterpretedOption_NamePart *msg, upb_arena *arena, size_t *len) {
8494   return upb_encode(msg, &google_protobuf_UninterpretedOption_NamePart_msginit, arena, len);
8495 }
8496 
8497 UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_has_name_part(const google_protobuf_UninterpretedOption_NamePart *msg) { return _upb_has_field(msg, 1); }
8498 UPB_INLINE upb_strview google_protobuf_UninterpretedOption_NamePart_name_part(const google_protobuf_UninterpretedOption_NamePart *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
8499 UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_has_is_extension(const google_protobuf_UninterpretedOption_NamePart *msg) { return _upb_has_field(msg, 0); }
8500 UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_is_extension(const google_protobuf_UninterpretedOption_NamePart *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); }
8501 
8502 UPB_INLINE void google_protobuf_UninterpretedOption_NamePart_set_name_part(google_protobuf_UninterpretedOption_NamePart *msg, upb_strview value) {
8503   _upb_sethas(msg, 1);
8504   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
8505 }
8506 UPB_INLINE void google_protobuf_UninterpretedOption_NamePart_set_is_extension(google_protobuf_UninterpretedOption_NamePart *msg, bool value) {
8507   _upb_sethas(msg, 0);
8508   UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value;
8509 }
8510 
8511 
8512 /* google.protobuf.SourceCodeInfo */
8513 
8514 UPB_INLINE google_protobuf_SourceCodeInfo *google_protobuf_SourceCodeInfo_new(upb_arena *arena) {
8515   return (google_protobuf_SourceCodeInfo *)upb_msg_new(&google_protobuf_SourceCodeInfo_msginit, arena);
8516 }
8517 UPB_INLINE google_protobuf_SourceCodeInfo *google_protobuf_SourceCodeInfo_parsenew(upb_strview buf, upb_arena *arena) {
8518   google_protobuf_SourceCodeInfo *ret = google_protobuf_SourceCodeInfo_new(arena);
8519   return (ret && upb_decode(buf, ret, &google_protobuf_SourceCodeInfo_msginit)) ? ret : NULL;
8520 }
8521 UPB_INLINE char *google_protobuf_SourceCodeInfo_serialize(const google_protobuf_SourceCodeInfo *msg, upb_arena *arena, size_t *len) {
8522   return upb_encode(msg, &google_protobuf_SourceCodeInfo_msginit, arena, len);
8523 }
8524 
8525 UPB_INLINE const google_protobuf_SourceCodeInfo_Location* const* google_protobuf_SourceCodeInfo_location(const google_protobuf_SourceCodeInfo *msg, size_t *len) { return (const google_protobuf_SourceCodeInfo_Location* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); }
8526 
8527 UPB_INLINE google_protobuf_SourceCodeInfo_Location** google_protobuf_SourceCodeInfo_mutable_location(google_protobuf_SourceCodeInfo *msg, size_t *len) {
8528   return (google_protobuf_SourceCodeInfo_Location**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len);
8529 }
8530 UPB_INLINE google_protobuf_SourceCodeInfo_Location** google_protobuf_SourceCodeInfo_resize_location(google_protobuf_SourceCodeInfo *msg, size_t len, upb_arena *arena) {
8531   return (google_protobuf_SourceCodeInfo_Location**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
8532 }
8533 UPB_INLINE struct google_protobuf_SourceCodeInfo_Location* google_protobuf_SourceCodeInfo_add_location(google_protobuf_SourceCodeInfo *msg, upb_arena *arena) {
8534   struct google_protobuf_SourceCodeInfo_Location* sub = (struct google_protobuf_SourceCodeInfo_Location*)upb_msg_new(&google_protobuf_SourceCodeInfo_Location_msginit, arena);
8535   bool ok = _upb_array_append_accessor(
8536       msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
8537   if (!ok) return NULL;
8538   return sub;
8539 }
8540 
8541 
8542 /* google.protobuf.SourceCodeInfo.Location */
8543 
8544 UPB_INLINE google_protobuf_SourceCodeInfo_Location *google_protobuf_SourceCodeInfo_Location_new(upb_arena *arena) {
8545   return (google_protobuf_SourceCodeInfo_Location *)upb_msg_new(&google_protobuf_SourceCodeInfo_Location_msginit, arena);
8546 }
8547 UPB_INLINE google_protobuf_SourceCodeInfo_Location *google_protobuf_SourceCodeInfo_Location_parsenew(upb_strview buf, upb_arena *arena) {
8548   google_protobuf_SourceCodeInfo_Location *ret = google_protobuf_SourceCodeInfo_Location_new(arena);
8549   return (ret && upb_decode(buf, ret, &google_protobuf_SourceCodeInfo_Location_msginit)) ? ret : NULL;
8550 }
8551 UPB_INLINE char *google_protobuf_SourceCodeInfo_Location_serialize(const google_protobuf_SourceCodeInfo_Location *msg, upb_arena *arena, size_t *len) {
8552   return upb_encode(msg, &google_protobuf_SourceCodeInfo_Location_msginit, arena, len);
8553 }
8554 
8555 UPB_INLINE int32_t const* google_protobuf_SourceCodeInfo_Location_path(const google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(20, 40), len); }
8556 UPB_INLINE int32_t const* google_protobuf_SourceCodeInfo_Location_span(const google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(24, 48), len); }
8557 UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_has_leading_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return _upb_has_field(msg, 0); }
8558 UPB_INLINE upb_strview google_protobuf_SourceCodeInfo_Location_leading_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
8559 UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_has_trailing_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return _upb_has_field(msg, 1); }
8560 UPB_INLINE upb_strview google_protobuf_SourceCodeInfo_Location_trailing_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)); }
8561 UPB_INLINE upb_strview const* google_protobuf_SourceCodeInfo_Location_leading_detached_comments(const google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { return (upb_strview const*)_upb_array_accessor(msg, UPB_SIZE(28, 56), len); }
8562 
8563 UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_mutable_path(google_protobuf_SourceCodeInfo_Location *msg, size_t *len) {
8564   return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 40), len);
8565 }
8566 UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_resize_path(google_protobuf_SourceCodeInfo_Location *msg, size_t len, upb_arena *arena) {
8567   return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(20, 40), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena);
8568 }
8569 UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_add_path(google_protobuf_SourceCodeInfo_Location *msg, int32_t val, upb_arena *arena) {
8570   return _upb_array_append_accessor(
8571       msg, UPB_SIZE(20, 40), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena);
8572 }
8573 UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_mutable_span(google_protobuf_SourceCodeInfo_Location *msg, size_t *len) {
8574   return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(24, 48), len);
8575 }
8576 UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_resize_span(google_protobuf_SourceCodeInfo_Location *msg, size_t len, upb_arena *arena) {
8577   return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(24, 48), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena);
8578 }
8579 UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_add_span(google_protobuf_SourceCodeInfo_Location *msg, int32_t val, upb_arena *arena) {
8580   return _upb_array_append_accessor(
8581       msg, UPB_SIZE(24, 48), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena);
8582 }
8583 UPB_INLINE void google_protobuf_SourceCodeInfo_Location_set_leading_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_strview value) {
8584   _upb_sethas(msg, 0);
8585   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
8586 }
8587 UPB_INLINE void google_protobuf_SourceCodeInfo_Location_set_trailing_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_strview value) {
8588   _upb_sethas(msg, 1);
8589   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)) = value;
8590 }
8591 UPB_INLINE upb_strview* google_protobuf_SourceCodeInfo_Location_mutable_leading_detached_comments(google_protobuf_SourceCodeInfo_Location *msg, size_t *len) {
8592   return (upb_strview*)_upb_array_mutable_accessor(msg, UPB_SIZE(28, 56), len);
8593 }
8594 UPB_INLINE upb_strview* google_protobuf_SourceCodeInfo_Location_resize_leading_detached_comments(google_protobuf_SourceCodeInfo_Location *msg, size_t len, upb_arena *arena) {
8595   return (upb_strview*)_upb_array_resize_accessor(msg, UPB_SIZE(28, 56), len, UPB_SIZE(8, 16), UPB_TYPE_STRING, arena);
8596 }
8597 UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_add_leading_detached_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_strview val, upb_arena *arena) {
8598   return _upb_array_append_accessor(
8599       msg, UPB_SIZE(28, 56), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, arena);
8600 }
8601 
8602 
8603 /* google.protobuf.GeneratedCodeInfo */
8604 
8605 UPB_INLINE google_protobuf_GeneratedCodeInfo *google_protobuf_GeneratedCodeInfo_new(upb_arena *arena) {
8606   return (google_protobuf_GeneratedCodeInfo *)upb_msg_new(&google_protobuf_GeneratedCodeInfo_msginit, arena);
8607 }
8608 UPB_INLINE google_protobuf_GeneratedCodeInfo *google_protobuf_GeneratedCodeInfo_parsenew(upb_strview buf, upb_arena *arena) {
8609   google_protobuf_GeneratedCodeInfo *ret = google_protobuf_GeneratedCodeInfo_new(arena);
8610   return (ret && upb_decode(buf, ret, &google_protobuf_GeneratedCodeInfo_msginit)) ? ret : NULL;
8611 }
8612 UPB_INLINE char *google_protobuf_GeneratedCodeInfo_serialize(const google_protobuf_GeneratedCodeInfo *msg, upb_arena *arena, size_t *len) {
8613   return upb_encode(msg, &google_protobuf_GeneratedCodeInfo_msginit, arena, len);
8614 }
8615 
8616 UPB_INLINE const google_protobuf_GeneratedCodeInfo_Annotation* const* google_protobuf_GeneratedCodeInfo_annotation(const google_protobuf_GeneratedCodeInfo *msg, size_t *len) { return (const google_protobuf_GeneratedCodeInfo_Annotation* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); }
8617 
8618 UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation** google_protobuf_GeneratedCodeInfo_mutable_annotation(google_protobuf_GeneratedCodeInfo *msg, size_t *len) {
8619   return (google_protobuf_GeneratedCodeInfo_Annotation**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len);
8620 }
8621 UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation** google_protobuf_GeneratedCodeInfo_resize_annotation(google_protobuf_GeneratedCodeInfo *msg, size_t len, upb_arena *arena) {
8622   return (google_protobuf_GeneratedCodeInfo_Annotation**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
8623 }
8624 UPB_INLINE struct google_protobuf_GeneratedCodeInfo_Annotation* google_protobuf_GeneratedCodeInfo_add_annotation(google_protobuf_GeneratedCodeInfo *msg, upb_arena *arena) {
8625   struct google_protobuf_GeneratedCodeInfo_Annotation* sub = (struct google_protobuf_GeneratedCodeInfo_Annotation*)upb_msg_new(&google_protobuf_GeneratedCodeInfo_Annotation_msginit, arena);
8626   bool ok = _upb_array_append_accessor(
8627       msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
8628   if (!ok) return NULL;
8629   return sub;
8630 }
8631 
8632 
8633 /* google.protobuf.GeneratedCodeInfo.Annotation */
8634 
8635 UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation *google_protobuf_GeneratedCodeInfo_Annotation_new(upb_arena *arena) {
8636   return (google_protobuf_GeneratedCodeInfo_Annotation *)upb_msg_new(&google_protobuf_GeneratedCodeInfo_Annotation_msginit, arena);
8637 }
8638 UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation *google_protobuf_GeneratedCodeInfo_Annotation_parsenew(upb_strview buf, upb_arena *arena) {
8639   google_protobuf_GeneratedCodeInfo_Annotation *ret = google_protobuf_GeneratedCodeInfo_Annotation_new(arena);
8640   return (ret && upb_decode(buf, ret, &google_protobuf_GeneratedCodeInfo_Annotation_msginit)) ? ret : NULL;
8641 }
8642 UPB_INLINE char *google_protobuf_GeneratedCodeInfo_Annotation_serialize(const google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_arena *arena, size_t *len) {
8643   return upb_encode(msg, &google_protobuf_GeneratedCodeInfo_Annotation_msginit, arena, len);
8644 }
8645 
8646 UPB_INLINE int32_t const* google_protobuf_GeneratedCodeInfo_Annotation_path(const google_protobuf_GeneratedCodeInfo_Annotation *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(20, 32), len); }
8647 UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_source_file(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return _upb_has_field(msg, 2); }
8648 UPB_INLINE upb_strview google_protobuf_GeneratedCodeInfo_Annotation_source_file(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 16)); }
8649 UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_begin(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return _upb_has_field(msg, 0); }
8650 UPB_INLINE int32_t google_protobuf_GeneratedCodeInfo_Annotation_begin(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); }
8651 UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_end(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return _upb_has_field(msg, 1); }
8652 UPB_INLINE int32_t google_protobuf_GeneratedCodeInfo_Annotation_end(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); }
8653 
8654 UPB_INLINE int32_t* google_protobuf_GeneratedCodeInfo_Annotation_mutable_path(google_protobuf_GeneratedCodeInfo_Annotation *msg, size_t *len) {
8655   return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 32), len);
8656 }
8657 UPB_INLINE int32_t* google_protobuf_GeneratedCodeInfo_Annotation_resize_path(google_protobuf_GeneratedCodeInfo_Annotation *msg, size_t len, upb_arena *arena) {
8658   return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(20, 32), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena);
8659 }
8660 UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_add_path(google_protobuf_GeneratedCodeInfo_Annotation *msg, int32_t val, upb_arena *arena) {
8661   return _upb_array_append_accessor(
8662       msg, UPB_SIZE(20, 32), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena);
8663 }
8664 UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_source_file(google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_strview value) {
8665   _upb_sethas(msg, 2);
8666   UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 16)) = value;
8667 }
8668 UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_begin(google_protobuf_GeneratedCodeInfo_Annotation *msg, int32_t value) {
8669   _upb_sethas(msg, 0);
8670   UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value;
8671 }
8672 UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_end(google_protobuf_GeneratedCodeInfo_Annotation *msg, int32_t value) {
8673   _upb_sethas(msg, 1);
8674   UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value;
8675 }
8676 
8677 
8678 UPB_END_EXTERN_C
8679 
8680 
8681 #endif  /* GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_ */
8682 
8683 
8684 #ifndef UPB_MSGFACTORY_H_
8685 #define UPB_MSGFACTORY_H_
8686 
8687 #ifdef __cplusplus
8688 namespace upb {
8689 class MessageFactory;
8690 }
8691 #endif
8692 
8693 UPB_DECLARE_TYPE(upb::MessageFactory, upb_msgfactory)
8694 
8695 /** upb_msgfactory ************************************************************/
8696 
8697 /* A upb_msgfactory contains a cache of upb_msglayout, upb_handlers, and
8698  * upb_visitorplan objects.  These are the objects necessary to represent,
8699  * populate, and and visit upb_msg objects.
8700  *
8701  * These caches are all populated by upb_msgdef, and lazily created on demand.
8702  */
8703 
8704 /* Creates and destroys a msgfactory, respectively.  The messages for this
8705  * msgfactory must come from |symtab| (which should outlive the msgfactory). */
8706 upb_msgfactory *upb_msgfactory_new(const upb_symtab *symtab);
8707 void upb_msgfactory_free(upb_msgfactory *f);
8708 
8709 const upb_symtab *upb_msgfactory_symtab(const upb_msgfactory *f);
8710 
8711 /* The functions to get cached objects, lazily creating them on demand.  These
8712  * all require:
8713  *
8714  * - m is in upb_msgfactory_symtab(f)
8715  * - upb_msgdef_mapentry(m) == false (since map messages can't have layouts).
8716  *
8717  * The returned objects will live for as long as the msgfactory does.
8718  *
8719  * TODO(haberman): consider making this thread-safe and take a const
8720  * upb_msgfactory. */
8721 const upb_msglayout *upb_msgfactory_getlayout(upb_msgfactory *f,
8722                                               const upb_msgdef *m);
8723 
8724 
8725 #endif /* UPB_MSGFACTORY_H_ */
8726 /*
8727 ** upb::descriptor::Reader (upb_descreader)
8728 **
8729 ** Provides a way of building upb::Defs from data in descriptor.proto format.
8730 */
8731 
8732 #ifndef UPB_DESCRIPTOR_H
8733 #define UPB_DESCRIPTOR_H
8734 
8735 
8736 #ifdef __cplusplus
8737 namespace upb {
8738 namespace descriptor {
8739 class Reader;
8740 }  /* namespace descriptor */
8741 }  /* namespace upb */
8742 #endif
8743 
8744 UPB_DECLARE_TYPE(upb::descriptor::Reader, upb_descreader)
8745 
8746 #ifdef __cplusplus
8747 
8748 /* Class that receives descriptor data according to the descriptor.proto schema
8749  * and use it to build upb::Defs corresponding to that schema. */
8750 class upb::descriptor::Reader {
8751  public:
8752   /* These handlers must have come from NewHandlers() and must outlive the
8753    * Reader.
8754    *
8755    * TODO: generate the handlers statically (like we do with the
8756    * descriptor.proto defs) so that there is no need to pass this parameter (or
8757    * to build/memory-manage the handlers at runtime at all).  Unfortunately this
8758    * is a bit tricky to implement for Handlers, but necessary to simplify this
8759    * interface. */
8760   static Reader* Create(Environment* env, const Handlers* handlers);
8761 
8762   /* The reader's input; this is where descriptor.proto data should be sent. */
8763   Sink* input();
8764 
8765   /* Use to get the FileDefs that have been parsed. */
8766   size_t file_count() const;
8767   FileDef* file(size_t i) const;
8768 
8769   /* Builds and returns handlers for the reader, owned by "owner." */
8770   static Handlers* NewHandlers(const void* owner);
8771 
8772  private:
8773   UPB_DISALLOW_POD_OPS(Reader, upb::descriptor::Reader)
8774 };
8775 
8776 #endif
8777 
8778 UPB_BEGIN_EXTERN_C
8779 
8780 /* C API. */
8781 upb_descreader *upb_descreader_create(upb_env *e, const upb_handlers *h);
8782 upb_sink *upb_descreader_input(upb_descreader *r);
8783 size_t upb_descreader_filecount(const upb_descreader *r);
8784 upb_filedef *upb_descreader_file(const upb_descreader *r, size_t i);
8785 const upb_handlers *upb_descreader_newhandlers(const void *owner);
8786 
8787 UPB_END_EXTERN_C
8788 
8789 #ifdef __cplusplus
8790 /* C++ implementation details. ************************************************/
8791 namespace upb {
8792 namespace descriptor {
8793 inline Reader* Reader::Create(Environment* e, const Handlers *h) {
8794   return upb_descreader_create(e, h);
8795 }
8796 inline Sink* Reader::input() { return upb_descreader_input(this); }
8797 inline size_t Reader::file_count() const {
8798   return upb_descreader_filecount(this);
8799 }
8800 inline FileDef* Reader::file(size_t i) const {
8801   return upb_descreader_file(this, i);
8802 }
8803 }  /* namespace descriptor */
8804 }  /* namespace upb */
8805 #endif
8806 
8807 #endif  /* UPB_DESCRIPTOR_H */
8808 /* This file contains accessors for a set of compiled-in defs.
8809  * Note that unlike Google's protobuf, it does *not* define
8810  * generated classes or any other kind of data structure for
8811  * actually storing protobufs.  It only contains *defs* which
8812  * let you reflect over a protobuf *schema*.
8813  */
8814 /* This file was generated by upbc (the upb compiler) from the input
8815  * file:
8816  *
8817  *     upb/descriptor/descriptor.proto
8818  *
8819  * Do not edit -- your changes will be discarded when the file is
8820  * regenerated. */
8821 
8822 #ifndef UPB_DESCRIPTOR_DESCRIPTOR_PROTO_UPB_H_
8823 #define UPB_DESCRIPTOR_DESCRIPTOR_PROTO_UPB_H_
8824 
8825 
8826 UPB_BEGIN_EXTERN_C
8827 
8828 /* MessageDefs: call these functions to get a ref to a msgdef. */
8829 const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_get(const void *owner);
8830 const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_get(const void *owner);
8831 const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ReservedRange_get(const void *owner);
8832 const upb_msgdef *upbdefs_google_protobuf_EnumDescriptorProto_get(const void *owner);
8833 const upb_msgdef *upbdefs_google_protobuf_EnumOptions_get(const void *owner);
8834 const upb_msgdef *upbdefs_google_protobuf_EnumValueDescriptorProto_get(const void *owner);
8835 const upb_msgdef *upbdefs_google_protobuf_EnumValueOptions_get(const void *owner);
8836 const upb_msgdef *upbdefs_google_protobuf_FieldDescriptorProto_get(const void *owner);
8837 const upb_msgdef *upbdefs_google_protobuf_FieldOptions_get(const void *owner);
8838 const upb_msgdef *upbdefs_google_protobuf_FileDescriptorProto_get(const void *owner);
8839 const upb_msgdef *upbdefs_google_protobuf_FileDescriptorSet_get(const void *owner);
8840 const upb_msgdef *upbdefs_google_protobuf_FileOptions_get(const void *owner);
8841 const upb_msgdef *upbdefs_google_protobuf_MessageOptions_get(const void *owner);
8842 const upb_msgdef *upbdefs_google_protobuf_MethodDescriptorProto_get(const void *owner);
8843 const upb_msgdef *upbdefs_google_protobuf_MethodOptions_get(const void *owner);
8844 const upb_msgdef *upbdefs_google_protobuf_OneofDescriptorProto_get(const void *owner);
8845 const upb_msgdef *upbdefs_google_protobuf_ServiceDescriptorProto_get(const void *owner);
8846 const upb_msgdef *upbdefs_google_protobuf_ServiceOptions_get(const void *owner);
8847 const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo_get(const void *owner);
8848 const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo_Location_get(const void *owner);
8849 const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption_get(const void *owner);
8850 const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption_NamePart_get(const void *owner);
8851 
8852 /* EnumDefs: call these functions to get a ref to an enumdef. */
8853 const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Label_get(const void *owner);
8854 const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Type_get(const void *owner);
8855 const upb_enumdef *upbdefs_google_protobuf_FieldOptions_CType_get(const void *owner);
8856 const upb_enumdef *upbdefs_google_protobuf_FieldOptions_JSType_get(const void *owner);
8857 const upb_enumdef *upbdefs_google_protobuf_FileOptions_OptimizeMode_get(const void *owner);
8858 
8859 /* Functions to test whether this message is of a certain type. */
8860 UPB_INLINE bool upbdefs_google_protobuf_DescriptorProto_is(const upb_msgdef *m) {
8861   return strcmp(upb_msgdef_fullname(m), "google.protobuf.DescriptorProto") == 0;
8862 }
8863 UPB_INLINE bool upbdefs_google_protobuf_DescriptorProto_ExtensionRange_is(const upb_msgdef *m) {
8864   return strcmp(upb_msgdef_fullname(m), "google.protobuf.DescriptorProto.ExtensionRange") == 0;
8865 }
8866 UPB_INLINE bool upbdefs_google_protobuf_DescriptorProto_ReservedRange_is(const upb_msgdef *m) {
8867   return strcmp(upb_msgdef_fullname(m), "google.protobuf.DescriptorProto.ReservedRange") == 0;
8868 }
8869 UPB_INLINE bool upbdefs_google_protobuf_EnumDescriptorProto_is(const upb_msgdef *m) {
8870   return strcmp(upb_msgdef_fullname(m), "google.protobuf.EnumDescriptorProto") == 0;
8871 }
8872 UPB_INLINE bool upbdefs_google_protobuf_EnumOptions_is(const upb_msgdef *m) {
8873   return strcmp(upb_msgdef_fullname(m), "google.protobuf.EnumOptions") == 0;
8874 }
8875 UPB_INLINE bool upbdefs_google_protobuf_EnumValueDescriptorProto_is(const upb_msgdef *m) {
8876   return strcmp(upb_msgdef_fullname(m), "google.protobuf.EnumValueDescriptorProto") == 0;
8877 }
8878 UPB_INLINE bool upbdefs_google_protobuf_EnumValueOptions_is(const upb_msgdef *m) {
8879   return strcmp(upb_msgdef_fullname(m), "google.protobuf.EnumValueOptions") == 0;
8880 }
8881 UPB_INLINE bool upbdefs_google_protobuf_FieldDescriptorProto_is(const upb_msgdef *m) {
8882   return strcmp(upb_msgdef_fullname(m), "google.protobuf.FieldDescriptorProto") == 0;
8883 }
8884 UPB_INLINE bool upbdefs_google_protobuf_FieldOptions_is(const upb_msgdef *m) {
8885   return strcmp(upb_msgdef_fullname(m), "google.protobuf.FieldOptions") == 0;
8886 }
8887 UPB_INLINE bool upbdefs_google_protobuf_FileDescriptorProto_is(const upb_msgdef *m) {
8888   return strcmp(upb_msgdef_fullname(m), "google.protobuf.FileDescriptorProto") == 0;
8889 }
8890 UPB_INLINE bool upbdefs_google_protobuf_FileDescriptorSet_is(const upb_msgdef *m) {
8891   return strcmp(upb_msgdef_fullname(m), "google.protobuf.FileDescriptorSet") == 0;
8892 }
8893 UPB_INLINE bool upbdefs_google_protobuf_FileOptions_is(const upb_msgdef *m) {
8894   return strcmp(upb_msgdef_fullname(m), "google.protobuf.FileOptions") == 0;
8895 }
8896 UPB_INLINE bool upbdefs_google_protobuf_MessageOptions_is(const upb_msgdef *m) {
8897   return strcmp(upb_msgdef_fullname(m), "google.protobuf.MessageOptions") == 0;
8898 }
8899 UPB_INLINE bool upbdefs_google_protobuf_MethodDescriptorProto_is(const upb_msgdef *m) {
8900   return strcmp(upb_msgdef_fullname(m), "google.protobuf.MethodDescriptorProto") == 0;
8901 }
8902 UPB_INLINE bool upbdefs_google_protobuf_MethodOptions_is(const upb_msgdef *m) {
8903   return strcmp(upb_msgdef_fullname(m), "google.protobuf.MethodOptions") == 0;
8904 }
8905 UPB_INLINE bool upbdefs_google_protobuf_OneofDescriptorProto_is(const upb_msgdef *m) {
8906   return strcmp(upb_msgdef_fullname(m), "google.protobuf.OneofDescriptorProto") == 0;
8907 }
8908 UPB_INLINE bool upbdefs_google_protobuf_ServiceDescriptorProto_is(const upb_msgdef *m) {
8909   return strcmp(upb_msgdef_fullname(m), "google.protobuf.ServiceDescriptorProto") == 0;
8910 }
8911 UPB_INLINE bool upbdefs_google_protobuf_ServiceOptions_is(const upb_msgdef *m) {
8912   return strcmp(upb_msgdef_fullname(m), "google.protobuf.ServiceOptions") == 0;
8913 }
8914 UPB_INLINE bool upbdefs_google_protobuf_SourceCodeInfo_is(const upb_msgdef *m) {
8915   return strcmp(upb_msgdef_fullname(m), "google.protobuf.SourceCodeInfo") == 0;
8916 }
8917 UPB_INLINE bool upbdefs_google_protobuf_SourceCodeInfo_Location_is(const upb_msgdef *m) {
8918   return strcmp(upb_msgdef_fullname(m), "google.protobuf.SourceCodeInfo.Location") == 0;
8919 }
8920 UPB_INLINE bool upbdefs_google_protobuf_UninterpretedOption_is(const upb_msgdef *m) {
8921   return strcmp(upb_msgdef_fullname(m), "google.protobuf.UninterpretedOption") == 0;
8922 }
8923 UPB_INLINE bool upbdefs_google_protobuf_UninterpretedOption_NamePart_is(const upb_msgdef *m) {
8924   return strcmp(upb_msgdef_fullname(m), "google.protobuf.UninterpretedOption.NamePart") == 0;
8925 }
8926 
8927 /* Functions to test whether this enum is of a certain type. */
8928 UPB_INLINE bool upbdefs_google_protobuf_FieldDescriptorProto_Label_is(const upb_enumdef *e) {
8929   return strcmp(upb_enumdef_fullname(e), "google.protobuf.FieldDescriptorProto.Label") == 0;
8930 }
8931 UPB_INLINE bool upbdefs_google_protobuf_FieldDescriptorProto_Type_is(const upb_enumdef *e) {
8932   return strcmp(upb_enumdef_fullname(e), "google.protobuf.FieldDescriptorProto.Type") == 0;
8933 }
8934 UPB_INLINE bool upbdefs_google_protobuf_FieldOptions_CType_is(const upb_enumdef *e) {
8935   return strcmp(upb_enumdef_fullname(e), "google.protobuf.FieldOptions.CType") == 0;
8936 }
8937 UPB_INLINE bool upbdefs_google_protobuf_FieldOptions_JSType_is(const upb_enumdef *e) {
8938   return strcmp(upb_enumdef_fullname(e), "google.protobuf.FieldOptions.JSType") == 0;
8939 }
8940 UPB_INLINE bool upbdefs_google_protobuf_FileOptions_OptimizeMode_is(const upb_enumdef *e) {
8941   return strcmp(upb_enumdef_fullname(e), "google.protobuf.FileOptions.OptimizeMode") == 0;
8942 }
8943 
8944 
8945 /* Functions to get a fielddef from a msgdef reference. */
8946 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_f_end(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ExtensionRange_is(m)); return upb_msgdef_itof(m, 2); }
8947 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_f_start(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ExtensionRange_is(m)); return upb_msgdef_itof(m, 1); }
8948 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ReservedRange_f_end(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ReservedRange_is(m)); return upb_msgdef_itof(m, 2); }
8949 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ReservedRange_f_start(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ReservedRange_is(m)); return upb_msgdef_itof(m, 1); }
8950 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_enum_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 4); }
8951 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_extension(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 6); }
8952 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_extension_range(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 5); }
8953 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_field(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 2); }
8954 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
8955 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_nested_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 3); }
8956 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_oneof_decl(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 8); }
8957 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 7); }
8958 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_reserved_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 10); }
8959 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_reserved_range(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 9); }
8960 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
8961 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); }
8962 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_f_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); }
8963 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_f_allow_alias(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumOptions_is(m)); return upb_msgdef_itof(m, 2); }
8964 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumOptions_is(m)); return upb_msgdef_itof(m, 3); }
8965 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumOptions_is(m)); return upb_msgdef_itof(m, 999); }
8966 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
8967 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_f_number(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); }
8968 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); }
8969 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumValueOptions_is(m)); return upb_msgdef_itof(m, 1); }
8970 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumValueOptions_is(m)); return upb_msgdef_itof(m, 999); }
8971 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_default_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 7); }
8972 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_extendee(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); }
8973 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_json_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 10); }
8974 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_label(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 4); }
8975 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
8976 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_number(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); }
8977 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_oneof_index(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 9); }
8978 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 8); }
8979 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 5); }
8980 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_type_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 6); }
8981 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_ctype(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 1); }
8982 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 3); }
8983 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_jstype(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 6); }
8984 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_lazy(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 5); }
8985 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_packed(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 2); }
8986 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 999); }
8987 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_weak(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 10); }
8988 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_dependency(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); }
8989 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_enum_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 5); }
8990 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_extension(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 7); }
8991 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_message_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 4); }
8992 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
8993 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 8); }
8994 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_package(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); }
8995 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_public_dependency(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 10); }
8996 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_service(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 6); }
8997 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_source_code_info(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 9); }
8998 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_syntax(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 12); }
8999 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_weak_dependency(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 11); }
9000 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorSet_f_file(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorSet_is(m)); return upb_msgdef_itof(m, 1); }
9001 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_cc_enable_arenas(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 31); }
9002 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_cc_generic_services(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 16); }
9003 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_csharp_namespace(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 37); }
9004 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 23); }
9005 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_go_package(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 11); }
9006 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_generate_equals_and_hash(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 20); }
9007 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_generic_services(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 17); }
9008 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_multiple_files(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 10); }
9009 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_outer_classname(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 8); }
9010 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_package(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 1); }
9011 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_string_check_utf8(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 27); }
9012 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_javanano_use_deprecated_package(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 38); }
9013 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_objc_class_prefix(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 36); }
9014 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_optimize_for(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 9); }
9015 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_php_class_prefix(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 40); }
9016 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_php_namespace(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 41); }
9017 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_py_generic_services(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 18); }
9018 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 999); }
9019 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 3); }
9020 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_map_entry(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 7); }
9021 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_message_set_wire_format(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 1); }
9022 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_no_standard_descriptor_accessor(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 2); }
9023 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 999); }
9024 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_client_streaming(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 5); }
9025 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_input_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); }
9026 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
9027 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 4); }
9028 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_output_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); }
9029 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_server_streaming(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 6); }
9030 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodOptions_is(m)); return upb_msgdef_itof(m, 33); }
9031 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodOptions_is(m)); return upb_msgdef_itof(m, 999); }
9032 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_OneofDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_OneofDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
9033 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_f_method(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_ServiceDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); }
9034 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_ServiceDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
9035 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_ServiceDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); }
9036 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_ServiceOptions_is(m)); return upb_msgdef_itof(m, 33); }
9037 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_ServiceOptions_is(m)); return upb_msgdef_itof(m, 999); }
9038 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_leading_comments(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 3); }
9039 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_leading_detached_comments(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 6); }
9040 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_path(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 1); }
9041 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_span(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 2); }
9042 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_trailing_comments(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 4); }
9043 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_f_location(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_is(m)); return upb_msgdef_itof(m, 1); }
9044 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_NamePart_f_is_extension(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_NamePart_is(m)); return upb_msgdef_itof(m, 2); }
9045 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_NamePart_f_name_part(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_NamePart_is(m)); return upb_msgdef_itof(m, 1); }
9046 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_aggregate_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 8); }
9047 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_double_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 6); }
9048 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_identifier_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 3); }
9049 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 2); }
9050 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_negative_int_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 5); }
9051 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_positive_int_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 4); }
9052 UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_string_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 7); }
9053 
9054 UPB_END_EXTERN_C
9055 
9056 #ifdef __cplusplus
9057 
9058 namespace upbdefs {
9059 namespace google {
9060 namespace protobuf {
9061 
9062 class DescriptorProto : public ::upb::reffed_ptr<const ::upb::MessageDef> {
9063  public:
9064   DescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL)
9065       : reffed_ptr(m, ref_donor) {
9066     UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m));
9067   }
9068 
9069   static DescriptorProto get() {
9070     const ::upb::MessageDef* m = upbdefs_google_protobuf_DescriptorProto_get(&m);
9071     return DescriptorProto(m, &m);
9072   }
9073 
9074   class ExtensionRange : public ::upb::reffed_ptr<const ::upb::MessageDef> {
9075    public:
9076     ExtensionRange(const ::upb::MessageDef* m, const void *ref_donor = NULL)
9077         : reffed_ptr(m, ref_donor) {
9078       UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ExtensionRange_is(m));
9079     }
9080 
9081     static ExtensionRange get() {
9082       const ::upb::MessageDef* m = upbdefs_google_protobuf_DescriptorProto_ExtensionRange_get(&m);
9083       return ExtensionRange(m, &m);
9084     }
9085   };
9086 
9087   class ReservedRange : public ::upb::reffed_ptr<const ::upb::MessageDef> {
9088    public:
9089     ReservedRange(const ::upb::MessageDef* m, const void *ref_donor = NULL)
9090         : reffed_ptr(m, ref_donor) {
9091       UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ReservedRange_is(m));
9092     }
9093 
9094     static ReservedRange get() {
9095       const ::upb::MessageDef* m = upbdefs_google_protobuf_DescriptorProto_ReservedRange_get(&m);
9096       return ReservedRange(m, &m);
9097     }
9098   };
9099 };
9100 
9101 class EnumDescriptorProto : public ::upb::reffed_ptr<const ::upb::MessageDef> {
9102  public:
9103   EnumDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL)
9104       : reffed_ptr(m, ref_donor) {
9105     UPB_ASSERT(upbdefs_google_protobuf_EnumDescriptorProto_is(m));
9106   }
9107 
9108   static EnumDescriptorProto get() {
9109     const ::upb::MessageDef* m = upbdefs_google_protobuf_EnumDescriptorProto_get(&m);
9110     return EnumDescriptorProto(m, &m);
9111   }
9112 };
9113 
9114 class EnumOptions : public ::upb::reffed_ptr<const ::upb::MessageDef> {
9115  public:
9116   EnumOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL)
9117       : reffed_ptr(m, ref_donor) {
9118     UPB_ASSERT(upbdefs_google_protobuf_EnumOptions_is(m));
9119   }
9120 
9121   static EnumOptions get() {
9122     const ::upb::MessageDef* m = upbdefs_google_protobuf_EnumOptions_get(&m);
9123     return EnumOptions(m, &m);
9124   }
9125 };
9126 
9127 class EnumValueDescriptorProto : public ::upb::reffed_ptr<const ::upb::MessageDef> {
9128  public:
9129   EnumValueDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL)
9130       : reffed_ptr(m, ref_donor) {
9131     UPB_ASSERT(upbdefs_google_protobuf_EnumValueDescriptorProto_is(m));
9132   }
9133 
9134   static EnumValueDescriptorProto get() {
9135     const ::upb::MessageDef* m = upbdefs_google_protobuf_EnumValueDescriptorProto_get(&m);
9136     return EnumValueDescriptorProto(m, &m);
9137   }
9138 };
9139 
9140 class EnumValueOptions : public ::upb::reffed_ptr<const ::upb::MessageDef> {
9141  public:
9142   EnumValueOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL)
9143       : reffed_ptr(m, ref_donor) {
9144     UPB_ASSERT(upbdefs_google_protobuf_EnumValueOptions_is(m));
9145   }
9146 
9147   static EnumValueOptions get() {
9148     const ::upb::MessageDef* m = upbdefs_google_protobuf_EnumValueOptions_get(&m);
9149     return EnumValueOptions(m, &m);
9150   }
9151 };
9152 
9153 class FieldDescriptorProto : public ::upb::reffed_ptr<const ::upb::MessageDef> {
9154  public:
9155   FieldDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL)
9156       : reffed_ptr(m, ref_donor) {
9157     UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m));
9158   }
9159 
9160   static FieldDescriptorProto get() {
9161     const ::upb::MessageDef* m = upbdefs_google_protobuf_FieldDescriptorProto_get(&m);
9162     return FieldDescriptorProto(m, &m);
9163   }
9164 
9165   class Label : public ::upb::reffed_ptr<const ::upb::EnumDef> {
9166    public:
9167     Label(const ::upb::EnumDef* e, const void *ref_donor = NULL)
9168         : reffed_ptr(e, ref_donor) {
9169       UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_Label_is(e));
9170     }
9171     static Label get() {
9172       const ::upb::EnumDef* e = upbdefs_google_protobuf_FieldDescriptorProto_Label_get(&e);
9173       return Label(e, &e);
9174     }
9175   };
9176 
9177   class Type : public ::upb::reffed_ptr<const ::upb::EnumDef> {
9178    public:
9179     Type(const ::upb::EnumDef* e, const void *ref_donor = NULL)
9180         : reffed_ptr(e, ref_donor) {
9181       UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_Type_is(e));
9182     }
9183     static Type get() {
9184       const ::upb::EnumDef* e = upbdefs_google_protobuf_FieldDescriptorProto_Type_get(&e);
9185       return Type(e, &e);
9186     }
9187   };
9188 };
9189 
9190 class FieldOptions : public ::upb::reffed_ptr<const ::upb::MessageDef> {
9191  public:
9192   FieldOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL)
9193       : reffed_ptr(m, ref_donor) {
9194     UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m));
9195   }
9196 
9197   static FieldOptions get() {
9198     const ::upb::MessageDef* m = upbdefs_google_protobuf_FieldOptions_get(&m);
9199     return FieldOptions(m, &m);
9200   }
9201 
9202   class CType : public ::upb::reffed_ptr<const ::upb::EnumDef> {
9203    public:
9204     CType(const ::upb::EnumDef* e, const void *ref_donor = NULL)
9205         : reffed_ptr(e, ref_donor) {
9206       UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_CType_is(e));
9207     }
9208     static CType get() {
9209       const ::upb::EnumDef* e = upbdefs_google_protobuf_FieldOptions_CType_get(&e);
9210       return CType(e, &e);
9211     }
9212   };
9213 
9214   class JSType : public ::upb::reffed_ptr<const ::upb::EnumDef> {
9215    public:
9216     JSType(const ::upb::EnumDef* e, const void *ref_donor = NULL)
9217         : reffed_ptr(e, ref_donor) {
9218       UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_JSType_is(e));
9219     }
9220     static JSType get() {
9221       const ::upb::EnumDef* e = upbdefs_google_protobuf_FieldOptions_JSType_get(&e);
9222       return JSType(e, &e);
9223     }
9224   };
9225 };
9226 
9227 class FileDescriptorProto : public ::upb::reffed_ptr<const ::upb::MessageDef> {
9228  public:
9229   FileDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL)
9230       : reffed_ptr(m, ref_donor) {
9231     UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m));
9232   }
9233 
9234   static FileDescriptorProto get() {
9235     const ::upb::MessageDef* m = upbdefs_google_protobuf_FileDescriptorProto_get(&m);
9236     return FileDescriptorProto(m, &m);
9237   }
9238 };
9239 
9240 class FileDescriptorSet : public ::upb::reffed_ptr<const ::upb::MessageDef> {
9241  public:
9242   FileDescriptorSet(const ::upb::MessageDef* m, const void *ref_donor = NULL)
9243       : reffed_ptr(m, ref_donor) {
9244     UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorSet_is(m));
9245   }
9246 
9247   static FileDescriptorSet get() {
9248     const ::upb::MessageDef* m = upbdefs_google_protobuf_FileDescriptorSet_get(&m);
9249     return FileDescriptorSet(m, &m);
9250   }
9251 };
9252 
9253 class FileOptions : public ::upb::reffed_ptr<const ::upb::MessageDef> {
9254  public:
9255   FileOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL)
9256       : reffed_ptr(m, ref_donor) {
9257     UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m));
9258   }
9259 
9260   static FileOptions get() {
9261     const ::upb::MessageDef* m = upbdefs_google_protobuf_FileOptions_get(&m);
9262     return FileOptions(m, &m);
9263   }
9264 
9265   class OptimizeMode : public ::upb::reffed_ptr<const ::upb::EnumDef> {
9266    public:
9267     OptimizeMode(const ::upb::EnumDef* e, const void *ref_donor = NULL)
9268         : reffed_ptr(e, ref_donor) {
9269       UPB_ASSERT(upbdefs_google_protobuf_FileOptions_OptimizeMode_is(e));
9270     }
9271     static OptimizeMode get() {
9272       const ::upb::EnumDef* e = upbdefs_google_protobuf_FileOptions_OptimizeMode_get(&e);
9273       return OptimizeMode(e, &e);
9274     }
9275   };
9276 };
9277 
9278 class MessageOptions : public ::upb::reffed_ptr<const ::upb::MessageDef> {
9279  public:
9280   MessageOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL)
9281       : reffed_ptr(m, ref_donor) {
9282     UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m));
9283   }
9284 
9285   static MessageOptions get() {
9286     const ::upb::MessageDef* m = upbdefs_google_protobuf_MessageOptions_get(&m);
9287     return MessageOptions(m, &m);
9288   }
9289 };
9290 
9291 class MethodDescriptorProto : public ::upb::reffed_ptr<const ::upb::MessageDef> {
9292  public:
9293   MethodDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL)
9294       : reffed_ptr(m, ref_donor) {
9295     UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m));
9296   }
9297 
9298   static MethodDescriptorProto get() {
9299     const ::upb::MessageDef* m = upbdefs_google_protobuf_MethodDescriptorProto_get(&m);
9300     return MethodDescriptorProto(m, &m);
9301   }
9302 };
9303 
9304 class MethodOptions : public ::upb::reffed_ptr<const ::upb::MessageDef> {
9305  public:
9306   MethodOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL)
9307       : reffed_ptr(m, ref_donor) {
9308     UPB_ASSERT(upbdefs_google_protobuf_MethodOptions_is(m));
9309   }
9310 
9311   static MethodOptions get() {
9312     const ::upb::MessageDef* m = upbdefs_google_protobuf_MethodOptions_get(&m);
9313     return MethodOptions(m, &m);
9314   }
9315 };
9316 
9317 class OneofDescriptorProto : public ::upb::reffed_ptr<const ::upb::MessageDef> {
9318  public:
9319   OneofDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL)
9320       : reffed_ptr(m, ref_donor) {
9321     UPB_ASSERT(upbdefs_google_protobuf_OneofDescriptorProto_is(m));
9322   }
9323 
9324   static OneofDescriptorProto get() {
9325     const ::upb::MessageDef* m = upbdefs_google_protobuf_OneofDescriptorProto_get(&m);
9326     return OneofDescriptorProto(m, &m);
9327   }
9328 };
9329 
9330 class ServiceDescriptorProto : public ::upb::reffed_ptr<const ::upb::MessageDef> {
9331  public:
9332   ServiceDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL)
9333       : reffed_ptr(m, ref_donor) {
9334     UPB_ASSERT(upbdefs_google_protobuf_ServiceDescriptorProto_is(m));
9335   }
9336 
9337   static ServiceDescriptorProto get() {
9338     const ::upb::MessageDef* m = upbdefs_google_protobuf_ServiceDescriptorProto_get(&m);
9339     return ServiceDescriptorProto(m, &m);
9340   }
9341 };
9342 
9343 class ServiceOptions : public ::upb::reffed_ptr<const ::upb::MessageDef> {
9344  public:
9345   ServiceOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL)
9346       : reffed_ptr(m, ref_donor) {
9347     UPB_ASSERT(upbdefs_google_protobuf_ServiceOptions_is(m));
9348   }
9349 
9350   static ServiceOptions get() {
9351     const ::upb::MessageDef* m = upbdefs_google_protobuf_ServiceOptions_get(&m);
9352     return ServiceOptions(m, &m);
9353   }
9354 };
9355 
9356 class SourceCodeInfo : public ::upb::reffed_ptr<const ::upb::MessageDef> {
9357  public:
9358   SourceCodeInfo(const ::upb::MessageDef* m, const void *ref_donor = NULL)
9359       : reffed_ptr(m, ref_donor) {
9360     UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_is(m));
9361   }
9362 
9363   static SourceCodeInfo get() {
9364     const ::upb::MessageDef* m = upbdefs_google_protobuf_SourceCodeInfo_get(&m);
9365     return SourceCodeInfo(m, &m);
9366   }
9367 
9368   class Location : public ::upb::reffed_ptr<const ::upb::MessageDef> {
9369    public:
9370     Location(const ::upb::MessageDef* m, const void *ref_donor = NULL)
9371         : reffed_ptr(m, ref_donor) {
9372       UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m));
9373     }
9374 
9375     static Location get() {
9376       const ::upb::MessageDef* m = upbdefs_google_protobuf_SourceCodeInfo_Location_get(&m);
9377       return Location(m, &m);
9378     }
9379   };
9380 };
9381 
9382 class UninterpretedOption : public ::upb::reffed_ptr<const ::upb::MessageDef> {
9383  public:
9384   UninterpretedOption(const ::upb::MessageDef* m, const void *ref_donor = NULL)
9385       : reffed_ptr(m, ref_donor) {
9386     UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m));
9387   }
9388 
9389   static UninterpretedOption get() {
9390     const ::upb::MessageDef* m = upbdefs_google_protobuf_UninterpretedOption_get(&m);
9391     return UninterpretedOption(m, &m);
9392   }
9393 
9394   class NamePart : public ::upb::reffed_ptr<const ::upb::MessageDef> {
9395    public:
9396     NamePart(const ::upb::MessageDef* m, const void *ref_donor = NULL)
9397         : reffed_ptr(m, ref_donor) {
9398       UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_NamePart_is(m));
9399     }
9400 
9401     static NamePart get() {
9402       const ::upb::MessageDef* m = upbdefs_google_protobuf_UninterpretedOption_NamePart_get(&m);
9403       return NamePart(m, &m);
9404     }
9405   };
9406 };
9407 
9408 }  /* namespace protobuf */
9409 }  /* namespace google */
9410 }  /* namespace upbdefs */
9411 
9412 #endif  /* __cplusplus */
9413 
9414 #endif  /* UPB_DESCRIPTOR_DESCRIPTOR_PROTO_UPB_H_ */
9415 /*
9416 ** Internal-only definitions for the decoder.
9417 */
9418 
9419 #ifndef UPB_DECODER_INT_H_
9420 #define UPB_DECODER_INT_H_
9421 
9422 /*
9423 ** upb::pb::Decoder
9424 **
9425 ** A high performance, streaming, resumable decoder for the binary protobuf
9426 ** format.
9427 **
9428 ** This interface works the same regardless of what decoder backend is being
9429 ** used.  A client of this class does not need to know whether decoding is using
9430 ** a JITted decoder (DynASM, LLVM, etc) or an interpreted decoder.  By default,
9431 ** it will always use the fastest available decoder.  However, you can call
9432 ** set_allow_jit(false) to disable any JIT decoder that might be available.
9433 ** This is primarily useful for testing purposes.
9434 */
9435 
9436 #ifndef UPB_DECODER_H_
9437 #define UPB_DECODER_H_
9438 
9439 
9440 #ifdef __cplusplus
9441 namespace upb {
9442 namespace pb {
9443 class CodeCache;
9444 class Decoder;
9445 class DecoderMethod;
9446 class DecoderMethodOptions;
9447 }  /* namespace pb */
9448 }  /* namespace upb */
9449 #endif
9450 
9451 UPB_DECLARE_TYPE(upb::pb::CodeCache, upb_pbcodecache)
9452 UPB_DECLARE_TYPE(upb::pb::Decoder, upb_pbdecoder)
9453 UPB_DECLARE_TYPE(upb::pb::DecoderMethodOptions, upb_pbdecodermethodopts)
9454 
9455 UPB_DECLARE_DERIVED_TYPE(upb::pb::DecoderMethod, upb::RefCounted,
9456                          upb_pbdecodermethod, upb_refcounted)
9457 
9458 /* The maximum number of bytes we are required to buffer internally between
9459  * calls to the decoder.  The value is 14: a 5 byte unknown tag plus ten-byte
9460  * varint, less one because we are buffering an incomplete value.
9461  *
9462  * Should only be used by unit tests. */
9463 #define UPB_DECODER_MAX_RESIDUAL_BYTES 14
9464 
9465 #ifdef __cplusplus
9466 
9467 /* The parameters one uses to construct a DecoderMethod.
9468  * TODO(haberman): move allowjit here?  Seems more convenient for users.
9469  * TODO(haberman): move this to be heap allocated for ABI stability. */
9470 class upb::pb::DecoderMethodOptions {
9471  public:
9472   /* Parameter represents the destination handlers that this method will push
9473    * to. */
9474   explicit DecoderMethodOptions(const Handlers* dest_handlers);
9475 
9476   /* Should the decoder push submessages to lazy handlers for fields that have
9477    * them?  The caller should set this iff the lazy handlers expect data that is
9478    * in protobuf binary format and the caller wishes to lazy parse it. */
9479   void set_lazy(bool lazy);
9480 #else
9481 struct upb_pbdecodermethodopts {
9482 #endif
9483   const upb_handlers *handlers;
9484   bool lazy;
9485 };
9486 
9487 #ifdef __cplusplus
9488 
9489 /* Represents the code to parse a protobuf according to a destination
9490  * Handlers. */
9491 class upb::pb::DecoderMethod {
9492  public:
9493   /* Include base methods from upb::ReferenceCounted. */
9494   UPB_REFCOUNTED_CPPMETHODS
9495 
9496   /* The destination handlers that are statically bound to this method.
9497    * This method is only capable of outputting to a sink that uses these
9498    * handlers. */
9499   const Handlers* dest_handlers() const;
9500 
9501   /* The input handlers for this decoder method. */
9502   const BytesHandler* input_handler() const;
9503 
9504   /* Whether this method is native. */
9505   bool is_native() const;
9506 
9507   /* Convenience method for generating a DecoderMethod without explicitly
9508    * creating a CodeCache. */
9509   static reffed_ptr<const DecoderMethod> New(const DecoderMethodOptions& opts);
9510 
9511  private:
9512   UPB_DISALLOW_POD_OPS(DecoderMethod, upb::pb::DecoderMethod)
9513 };
9514 
9515 #endif
9516 
9517 /* Preallocation hint: decoder won't allocate more bytes than this when first
9518  * constructed.  This hint may be an overestimate for some build configurations.
9519  * But if the decoder library is upgraded without recompiling the application,
9520  * it may be an underestimate. */
9521 #define UPB_PB_DECODER_SIZE 4416
9522 
9523 #ifdef __cplusplus
9524 
9525 /* A Decoder receives binary protobuf data on its input sink and pushes the
9526  * decoded data to its output sink. */
9527 class upb::pb::Decoder {
9528  public:
9529   /* Constructs a decoder instance for the given method, which must outlive this
9530    * decoder.  Any errors during parsing will be set on the given status, which
9531    * must also outlive this decoder.
9532    *
9533    * The sink must match the given method. */
9534   static Decoder* Create(Environment* env, const DecoderMethod* method,
9535                          Sink* output);
9536 
9537   /* Returns the DecoderMethod this decoder is parsing from. */
9538   const DecoderMethod* method() const;
9539 
9540   /* The sink on which this decoder receives input. */
9541   BytesSink* input();
9542 
9543   /* Returns number of bytes successfully parsed.
9544    *
9545    * This can be useful for determining the stream position where an error
9546    * occurred.
9547    *
9548    * This value may not be up-to-date when called from inside a parsing
9549    * callback. */
9550   uint64_t BytesParsed() const;
9551 
9552   /* Gets/sets the parsing nexting limit.  If the total number of nested
9553    * submessages and repeated fields hits this limit, parsing will fail.  This
9554    * is a resource limit that controls the amount of memory used by the parsing
9555    * stack.
9556    *
9557    * Setting the limit will fail if the parser is currently suspended at a depth
9558    * greater than this, or if memory allocation of the stack fails. */
9559   size_t max_nesting() const;
9560   bool set_max_nesting(size_t max);
9561 
9562   void Reset();
9563 
9564   static const size_t kSize = UPB_PB_DECODER_SIZE;
9565 
9566  private:
9567   UPB_DISALLOW_POD_OPS(Decoder, upb::pb::Decoder)
9568 };
9569 
9570 #endif  /* __cplusplus */
9571 
9572 #ifdef __cplusplus
9573 
9574 /* A class for caching protobuf processing code, whether bytecode for the
9575  * interpreted decoder or machine code for the JIT.
9576  *
9577  * This class is not thread-safe.
9578  *
9579  * TODO(haberman): move this to be heap allocated for ABI stability. */
9580 class upb::pb::CodeCache {
9581  public:
9582   CodeCache();
9583   ~CodeCache();
9584 
9585   /* Whether the cache is allowed to generate machine code.  Defaults to true.
9586    * There is no real reason to turn it off except for testing or if you are
9587    * having a specific problem with the JIT.
9588    *
9589    * Note that allow_jit = true does not *guarantee* that the code will be JIT
9590    * compiled.  If this platform is not supported or the JIT was not compiled
9591    * in, the code may still be interpreted. */
9592   bool allow_jit() const;
9593 
9594   /* This may only be called when the object is first constructed, and prior to
9595    * any code generation, otherwise returns false and does nothing. */
9596   bool set_allow_jit(bool allow);
9597 
9598   /* Returns a DecoderMethod that can push data to the given handlers.
9599    * If a suitable method already exists, it will be returned from the cache.
9600    *
9601    * Specifying the destination handlers here allows the DecoderMethod to be
9602    * statically bound to the destination handlers if possible, which can allow
9603    * more efficient decoding.  However the returned method may or may not
9604    * actually be statically bound.  But in all cases, the returned method can
9605    * push data to the given handlers. */
9606   const DecoderMethod *GetDecoderMethod(const DecoderMethodOptions& opts);
9607 
9608   /* If/when someone needs to explicitly create a dynamically-bound
9609    * DecoderMethod*, we can add a method to get it here. */
9610 
9611  private:
9612   UPB_DISALLOW_COPY_AND_ASSIGN(CodeCache)
9613 #else
9614 struct upb_pbcodecache {
9615 #endif
9616   bool allow_jit_;
9617 
9618   /* Array of mgroups. */
9619   upb_inttable groups;
9620 };
9621 
9622 UPB_BEGIN_EXTERN_C
9623 
9624 upb_pbdecoder *upb_pbdecoder_create(upb_env *e,
9625                                     const upb_pbdecodermethod *method,
9626                                     upb_sink *output);
9627 const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d);
9628 upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d);
9629 uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d);
9630 size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d);
9631 bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max);
9632 void upb_pbdecoder_reset(upb_pbdecoder *d);
9633 
9634 void upb_pbdecodermethodopts_init(upb_pbdecodermethodopts *opts,
9635                                   const upb_handlers *h);
9636 void upb_pbdecodermethodopts_setlazy(upb_pbdecodermethodopts *opts, bool lazy);
9637 
9638 
9639 /* Include refcounted methods like upb_pbdecodermethod_ref(). */
9640 UPB_REFCOUNTED_CMETHODS(upb_pbdecodermethod, upb_pbdecodermethod_upcast)
9641 
9642 const upb_handlers *upb_pbdecodermethod_desthandlers(
9643     const upb_pbdecodermethod *m);
9644 const upb_byteshandler *upb_pbdecodermethod_inputhandler(
9645     const upb_pbdecodermethod *m);
9646 bool upb_pbdecodermethod_isnative(const upb_pbdecodermethod *m);
9647 const upb_pbdecodermethod *upb_pbdecodermethod_new(
9648     const upb_pbdecodermethodopts *opts, const void *owner);
9649 
9650 void upb_pbcodecache_init(upb_pbcodecache *c);
9651 void upb_pbcodecache_uninit(upb_pbcodecache *c);
9652 bool upb_pbcodecache_allowjit(const upb_pbcodecache *c);
9653 bool upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow);
9654 const upb_pbdecodermethod *upb_pbcodecache_getdecodermethod(
9655     upb_pbcodecache *c, const upb_pbdecodermethodopts *opts);
9656 
9657 UPB_END_EXTERN_C
9658 
9659 #ifdef __cplusplus
9660 
9661 namespace upb {
9662 
9663 namespace pb {
9664 
9665 /* static */
9666 inline Decoder* Decoder::Create(Environment* env, const DecoderMethod* m,
9667                                 Sink* sink) {
9668   return upb_pbdecoder_create(env, m, sink);
9669 }
9670 inline const DecoderMethod* Decoder::method() const {
9671   return upb_pbdecoder_method(this);
9672 }
9673 inline BytesSink* Decoder::input() {
9674   return upb_pbdecoder_input(this);
9675 }
9676 inline uint64_t Decoder::BytesParsed() const {
9677   return upb_pbdecoder_bytesparsed(this);
9678 }
9679 inline size_t Decoder::max_nesting() const {
9680   return upb_pbdecoder_maxnesting(this);
9681 }
9682 inline bool Decoder::set_max_nesting(size_t max) {
9683   return upb_pbdecoder_setmaxnesting(this, max);
9684 }
9685 inline void Decoder::Reset() { upb_pbdecoder_reset(this); }
9686 
9687 inline DecoderMethodOptions::DecoderMethodOptions(const Handlers* h) {
9688   upb_pbdecodermethodopts_init(this, h);
9689 }
9690 inline void DecoderMethodOptions::set_lazy(bool lazy) {
9691   upb_pbdecodermethodopts_setlazy(this, lazy);
9692 }
9693 
9694 inline const Handlers* DecoderMethod::dest_handlers() const {
9695   return upb_pbdecodermethod_desthandlers(this);
9696 }
9697 inline const BytesHandler* DecoderMethod::input_handler() const {
9698   return upb_pbdecodermethod_inputhandler(this);
9699 }
9700 inline bool DecoderMethod::is_native() const {
9701   return upb_pbdecodermethod_isnative(this);
9702 }
9703 /* static */
9704 inline reffed_ptr<const DecoderMethod> DecoderMethod::New(
9705     const DecoderMethodOptions &opts) {
9706   const upb_pbdecodermethod *m = upb_pbdecodermethod_new(&opts, &m);
9707   return reffed_ptr<const DecoderMethod>(m, &m);
9708 }
9709 
9710 inline CodeCache::CodeCache() {
9711   upb_pbcodecache_init(this);
9712 }
9713 inline CodeCache::~CodeCache() {
9714   upb_pbcodecache_uninit(this);
9715 }
9716 inline bool CodeCache::allow_jit() const {
9717   return upb_pbcodecache_allowjit(this);
9718 }
9719 inline bool CodeCache::set_allow_jit(bool allow) {
9720   return upb_pbcodecache_setallowjit(this, allow);
9721 }
9722 inline const DecoderMethod *CodeCache::GetDecoderMethod(
9723     const DecoderMethodOptions& opts) {
9724   return upb_pbcodecache_getdecodermethod(this, &opts);
9725 }
9726 
9727 }  /* namespace pb */
9728 }  /* namespace upb */
9729 
9730 #endif  /* __cplusplus */
9731 
9732 #endif  /* UPB_DECODER_H_ */
9733 
9734 #ifndef __cplusplus
9735 
9736 UPB_DECLARE_DERIVED_TYPE(upb::pb::MessageGroup, upb::RefCounted,
9737                          mgroup, upb_refcounted)
9738 
9739 /* Opcode definitions.  The canonical meaning of each opcode is its
9740  * implementation in the interpreter (the JIT is written to match this).
9741  *
9742  * All instructions have the opcode in the low byte.
9743  * Instruction format for most instructions is:
9744  *
9745  * +-------------------+--------+
9746  * |     arg (24)      | op (8) |
9747  * +-------------------+--------+
9748  *
9749  * Exceptions are indicated below.  A few opcodes are multi-word. */
9750 typedef enum {
9751   /* Opcodes 1-8, 13, 15-18 parse their respective descriptor types.
9752    * Arg for all of these is the upb selector for this field. */
9753 #define T(type) OP_PARSE_ ## type = UPB_DESCRIPTOR_TYPE_ ## type
9754   T(DOUBLE), T(FLOAT), T(INT64), T(UINT64), T(INT32), T(FIXED64), T(FIXED32),
9755   T(BOOL), T(UINT32), T(SFIXED32), T(SFIXED64), T(SINT32), T(SINT64),
9756 #undef T
9757   OP_STARTMSG       = 9,   /* No arg. */
9758   OP_ENDMSG         = 10,  /* No arg. */
9759   OP_STARTSEQ       = 11,
9760   OP_ENDSEQ         = 12,
9761   OP_STARTSUBMSG    = 14,
9762   OP_ENDSUBMSG      = 19,
9763   OP_STARTSTR       = 20,
9764   OP_STRING         = 21,
9765   OP_ENDSTR         = 22,
9766 
9767   OP_PUSHTAGDELIM   = 23,  /* No arg. */
9768   OP_PUSHLENDELIM   = 24,  /* No arg. */
9769   OP_POP            = 25,  /* No arg. */
9770   OP_SETDELIM       = 26,  /* No arg. */
9771   OP_SETBIGGROUPNUM = 27,  /* two words:
9772                             *   | unused (24)     | opc (8) |
9773                             *   |        groupnum (32)      | */
9774   OP_CHECKDELIM     = 28,
9775   OP_CALL           = 29,
9776   OP_RET            = 30,
9777   OP_BRANCH         = 31,
9778 
9779   /* Different opcodes depending on how many bytes expected. */
9780   OP_TAG1           = 32,  /* | match tag (16) | jump target (8) | opc (8) | */
9781   OP_TAG2           = 33,  /* | match tag (16) | jump target (8) | opc (8) | */
9782   OP_TAGN           = 34,  /* three words: */
9783                            /*   | unused (16) | jump target(8) | opc (8) | */
9784                            /*   |           match tag 1 (32)             | */
9785                            /*   |           match tag 2 (32)             | */
9786 
9787   OP_SETDISPATCH    = 35,  /* N words: */
9788                            /*   | unused (24)         | opc | */
9789                            /*   | upb_inttable* (32 or 64)  | */
9790 
9791   OP_DISPATCH       = 36,  /* No arg. */
9792 
9793   OP_HALT           = 37   /* No arg. */
9794 } opcode;
9795 
9796 #define OP_MAX OP_HALT
9797 
9798 UPB_INLINE opcode getop(uint32_t instr) { return (opcode)(instr & 0xff); }
9799 
9800 /* Method group; represents a set of decoder methods that had their code
9801  * emitted together, and must therefore be freed together.  Immutable once
9802  * created.  It is possible we may want to expose this to users at some point.
9803  *
9804  * Overall ownership of Decoder objects looks like this:
9805  *
9806  *                +----------+
9807  *                |          | <---> DecoderMethod
9808  *                | method   |
9809  * CodeCache ---> |  group   | <---> DecoderMethod
9810  *                |          |
9811  *                | (mgroup) | <---> DecoderMethod
9812  *                +----------+
9813  */
9814 struct mgroup {
9815   upb_refcounted base;
9816 
9817   /* Maps upb_msgdef/upb_handlers -> upb_pbdecodermethod.  We own refs on the
9818    * methods. */
9819   upb_inttable methods;
9820 
9821   /* When we add the ability to link to previously existing mgroups, we'll
9822    * need an array of mgroups we reference here, and own refs on them. */
9823 
9824   /* The bytecode for our methods, if any exists.  Owned by us. */
9825   uint32_t *bytecode;
9826   uint32_t *bytecode_end;
9827 
9828 #ifdef UPB_USE_JIT_X64
9829   /* JIT-generated machine code, if any. */
9830   upb_string_handlerfunc *jit_code;
9831   /* The size of the jit_code (required to munmap()). */
9832   size_t jit_size;
9833   char *debug_info;
9834   void *dl;
9835 #endif
9836 };
9837 
9838 /* The maximum that any submessages can be nested.  Matches proto2's limit.
9839  * This specifies the size of the decoder's statically-sized array and therefore
9840  * setting it high will cause the upb::pb::Decoder object to be larger.
9841  *
9842  * If necessary we can add a runtime-settable property to Decoder that allow
9843  * this to be larger than the compile-time setting, but this would add
9844  * complexity, particularly since we would have to decide how/if to give users
9845  * the ability to set a custom memory allocation function. */
9846 #define UPB_DECODER_MAX_NESTING 64
9847 
9848 /* Internal-only struct used by the decoder. */
9849 typedef struct {
9850   /* Space optimization note: we store two pointers here that the JIT
9851    * doesn't need at all; the upb_handlers* inside the sink and
9852    * the dispatch table pointer.  We can optimze so that the JIT uses
9853    * smaller stack frames than the interpreter.  The only thing we need
9854    * to guarantee is that the fallback routines can find end_ofs. */
9855   upb_sink sink;
9856 
9857   /* The absolute stream offset of the end-of-frame delimiter.
9858    * Non-delimited frames (groups and non-packed repeated fields) reuse the
9859    * delimiter of their parent, even though the frame may not end there.
9860    *
9861    * NOTE: the JIT stores a slightly different value here for non-top frames.
9862    * It stores the value relative to the end of the enclosed message.  But the
9863    * top frame is still stored the same way, which is important for ensuring
9864    * that calls from the JIT into C work correctly. */
9865   uint64_t end_ofs;
9866   const uint32_t *base;
9867 
9868   /* 0 indicates a length-delimited field.
9869    * A positive number indicates a known group.
9870    * A negative number indicates an unknown group. */
9871   int32_t groupnum;
9872   upb_inttable *dispatch;  /* Not used by the JIT. */
9873 } upb_pbdecoder_frame;
9874 
9875 struct upb_pbdecodermethod {
9876   upb_refcounted base;
9877 
9878   /* While compiling, the base is relative in "ofs", after compiling it is
9879    * absolute in "ptr". */
9880   union {
9881     uint32_t ofs;     /* PC offset of method. */
9882     void *ptr;        /* Pointer to bytecode or machine code for this method. */
9883   } code_base;
9884 
9885   /* The decoder method group to which this method belongs.  We own a ref.
9886    * Owning a ref on the entire group is more coarse-grained than is strictly
9887    * necessary; all we truly require is that methods we directly reference
9888    * outlive us, while the group could contain many other messages we don't
9889    * require.  But the group represents the messages that were
9890    * allocated+compiled together, so it makes the most sense to free them
9891    * together also. */
9892   const upb_refcounted *group;
9893 
9894   /* Whether this method is native code or bytecode. */
9895   bool is_native_;
9896 
9897   /* The handler one calls to invoke this method. */
9898   upb_byteshandler input_handler_;
9899 
9900   /* The destination handlers this method is bound to.  We own a ref. */
9901   const upb_handlers *dest_handlers_;
9902 
9903   /* Dispatch table -- used by both bytecode decoder and JIT when encountering a
9904    * field number that wasn't the one we were expecting to see.  See
9905    * decoder.int.h for the layout of this table. */
9906   upb_inttable dispatch;
9907 };
9908 
9909 struct upb_pbdecoder {
9910   upb_env *env;
9911 
9912   /* Our input sink. */
9913   upb_bytessink input_;
9914 
9915   /* The decoder method we are parsing with (owned). */
9916   const upb_pbdecodermethod *method_;
9917 
9918   size_t call_len;
9919   const uint32_t *pc, *last;
9920 
9921   /* Current input buffer and its stream offset. */
9922   const char *buf, *ptr, *end, *checkpoint;
9923 
9924   /* End of the delimited region, relative to ptr, NULL if not in this buf. */
9925   const char *delim_end;
9926 
9927   /* End of the delimited region, relative to ptr, end if not in this buf. */
9928   const char *data_end;
9929 
9930   /* Overall stream offset of "buf." */
9931   uint64_t bufstart_ofs;
9932 
9933   /* Buffer for residual bytes not parsed from the previous buffer. */
9934   char residual[UPB_DECODER_MAX_RESIDUAL_BYTES];
9935   char *residual_end;
9936 
9937   /* Bytes of data that should be discarded from the input beore we start
9938    * parsing again.  We set this when we internally determine that we can
9939    * safely skip the next N bytes, but this region extends past the current
9940    * user buffer. */
9941   size_t skip;
9942 
9943   /* Stores the user buffer passed to our decode function. */
9944   const char *buf_param;
9945   size_t size_param;
9946   const upb_bufhandle *handle;
9947 
9948   /* Our internal stack. */
9949   upb_pbdecoder_frame *stack, *top, *limit;
9950   const uint32_t **callstack;
9951   size_t stack_size;
9952 
9953   upb_status *status;
9954 
9955 #ifdef UPB_USE_JIT_X64
9956   /* Used momentarily by the generated code to store a value while a user
9957    * function is called. */
9958   uint32_t tmp_len;
9959 
9960   const void *saved_rsp;
9961 #endif
9962 };
9963 
9964 /* Decoder entry points; used as handlers. */
9965 void *upb_pbdecoder_startbc(void *closure, const void *pc, size_t size_hint);
9966 void *upb_pbdecoder_startjit(void *closure, const void *hd, size_t size_hint);
9967 size_t upb_pbdecoder_decode(void *closure, const void *hd, const char *buf,
9968                             size_t size, const upb_bufhandle *handle);
9969 bool upb_pbdecoder_end(void *closure, const void *handler_data);
9970 
9971 /* Decoder-internal functions that the JIT calls to handle fallback paths. */
9972 int32_t upb_pbdecoder_resume(upb_pbdecoder *d, void *p, const char *buf,
9973                              size_t size, const upb_bufhandle *handle);
9974 size_t upb_pbdecoder_suspend(upb_pbdecoder *d);
9975 int32_t upb_pbdecoder_skipunknown(upb_pbdecoder *d, int32_t fieldnum,
9976                                   uint8_t wire_type);
9977 int32_t upb_pbdecoder_checktag_slow(upb_pbdecoder *d, uint64_t expected);
9978 int32_t upb_pbdecoder_decode_varint_slow(upb_pbdecoder *d, uint64_t *u64);
9979 int32_t upb_pbdecoder_decode_f32(upb_pbdecoder *d, uint32_t *u32);
9980 int32_t upb_pbdecoder_decode_f64(upb_pbdecoder *d, uint64_t *u64);
9981 void upb_pbdecoder_seterr(upb_pbdecoder *d, const char *msg);
9982 
9983 /* Error messages that are shared between the bytecode and JIT decoders. */
9984 extern const char *kPbDecoderStackOverflow;
9985 extern const char *kPbDecoderSubmessageTooLong;
9986 
9987 /* Access to decoderplan members needed by the decoder. */
9988 const char *upb_pbdecoder_getopname(unsigned int op);
9989 
9990 /* JIT codegen entry point. */
9991 void upb_pbdecoder_jit(mgroup *group);
9992 void upb_pbdecoder_freejit(mgroup *group);
9993 UPB_REFCOUNTED_CMETHODS(mgroup, mgroup_upcast)
9994 
9995 /* A special label that means "do field dispatch for this message and branch to
9996  * wherever that takes you." */
9997 #define LABEL_DISPATCH 0
9998 
9999 /* A special slot in the dispatch table that stores the epilogue (ENDMSG and/or
10000  * RET) for branching to when we find an appropriate ENDGROUP tag. */
10001 #define DISPATCH_ENDMSG 0
10002 
10003 /* It's important to use this invalid wire type instead of 0 (which is a valid
10004  * wire type). */
10005 #define NO_WIRE_TYPE 0xff
10006 
10007 /* The dispatch table layout is:
10008  *   [field number] -> [ 48-bit offset ][ 8-bit wt2 ][ 8-bit wt1 ]
10009  *
10010  * If wt1 matches, jump to the 48-bit offset.  If wt2 matches, lookup
10011  * (UPB_MAX_FIELDNUMBER + fieldnum) and jump there.
10012  *
10013  * We need two wire types because of packed/non-packed compatibility.  A
10014  * primitive repeated field can use either wire type and be valid.  While we
10015  * could key the table on fieldnum+wiretype, the table would be 8x sparser.
10016  *
10017  * Storing two wire types in the primary value allows us to quickly rule out
10018  * the second wire type without needing to do a separate lookup (this case is
10019  * less common than an unknown field). */
10020 UPB_INLINE uint64_t upb_pbdecoder_packdispatch(uint64_t ofs, uint8_t wt1,
10021                                                uint8_t wt2) {
10022   return (ofs << 16) | (wt2 << 8) | wt1;
10023 }
10024 
10025 UPB_INLINE void upb_pbdecoder_unpackdispatch(uint64_t dispatch, uint64_t *ofs,
10026                                              uint8_t *wt1, uint8_t *wt2) {
10027   *wt1 = (uint8_t)dispatch;
10028   *wt2 = (uint8_t)(dispatch >> 8);
10029   *ofs = dispatch >> 16;
10030 }
10031 
10032 /* All of the functions in decoder.c that return int32_t return values according
10033  * to the following scheme:
10034  *   1. negative values indicate a return code from the following list.
10035  *   2. positive values indicate that error or end of buffer was hit, and
10036  *      that the decode function should immediately return the given value
10037  *      (the decoder state has already been suspended and is ready to be
10038  *      resumed). */
10039 #define DECODE_OK -1
10040 #define DECODE_MISMATCH -2  /* Used only from checktag_slow(). */
10041 #define DECODE_ENDGROUP -3  /* Used only from checkunknown(). */
10042 
10043 #define CHECK_RETURN(x) { int32_t ret = x; if (ret >= 0) return ret; }
10044 
10045 #endif  /* __cplusplus */
10046 
10047 #endif  /* UPB_DECODER_INT_H_ */
10048 /*
10049 ** A number of routines for varint manipulation (we keep them all around to
10050 ** have multiple approaches available for benchmarking).
10051 */
10052 
10053 #ifndef UPB_VARINT_DECODER_H_
10054 #define UPB_VARINT_DECODER_H_
10055 
10056 #include <assert.h>
10057 #include <stdint.h>
10058 #include <string.h>
10059 
10060 #ifdef __cplusplus
10061 extern "C" {
10062 #endif
10063 
10064 #define UPB_MAX_WIRE_TYPE 5
10065 
10066 /* The maximum number of bytes that it takes to encode a 64-bit varint. */
10067 #define UPB_PB_VARINT_MAX_LEN 10
10068 
10069 /* Array of the "native" (ie. non-packed-repeated) wire type for the given a
10070  * descriptor type (upb_descriptortype_t). */
10071 extern const uint8_t upb_pb_native_wire_types[];
10072 
10073 /* Zig-zag encoding/decoding **************************************************/
10074 
10075 UPB_INLINE int32_t upb_zzdec_32(uint32_t n) {
10076   return (n >> 1) ^ -(int32_t)(n & 1);
10077 }
10078 UPB_INLINE int64_t upb_zzdec_64(uint64_t n) {
10079   return (n >> 1) ^ -(int64_t)(n & 1);
10080 }
10081 UPB_INLINE uint32_t upb_zzenc_32(int32_t n) { return (n << 1) ^ (n >> 31); }
10082 UPB_INLINE uint64_t upb_zzenc_64(int64_t n) { return (n << 1) ^ (n >> 63); }
10083 
10084 /* Decoding *******************************************************************/
10085 
10086 /* All decoding functions return this struct by value. */
10087 typedef struct {
10088   const char *p;  /* NULL if the varint was unterminated. */
10089   uint64_t val;
10090 } upb_decoderet;
10091 
10092 UPB_INLINE upb_decoderet upb_decoderet_make(const char *p, uint64_t val) {
10093   upb_decoderet ret;
10094   ret.p = p;
10095   ret.val = val;
10096   return ret;
10097 }
10098 
10099 upb_decoderet upb_vdecode_max8_branch32(upb_decoderet r);
10100 upb_decoderet upb_vdecode_max8_branch64(upb_decoderet r);
10101 
10102 /* Template for a function that checks the first two bytes with branching
10103  * and dispatches 2-10 bytes with a separate function.  Note that this may read
10104  * up to 10 bytes, so it must not be used unless there are at least ten bytes
10105  * left in the buffer! */
10106 #define UPB_VARINT_DECODER_CHECK2(name, decode_max8_function)                  \
10107 UPB_INLINE upb_decoderet upb_vdecode_check2_ ## name(const char *_p) {         \
10108   uint8_t *p = (uint8_t*)_p;                                                   \
10109   upb_decoderet r;                                                             \
10110   if ((*p & 0x80) == 0) {                                                      \
10111   /* Common case: one-byte varint. */                                          \
10112     return upb_decoderet_make(_p + 1, *p & 0x7fU);                             \
10113   }                                                                            \
10114   r = upb_decoderet_make(_p + 2, (*p & 0x7fU) | ((*(p + 1) & 0x7fU) << 7));    \
10115   if ((*(p + 1) & 0x80) == 0) {                                                \
10116     /* Two-byte varint. */                                                     \
10117     return r;                                                                  \
10118   }                                                                            \
10119   /* Longer varint, fallback to out-of-line function. */                       \
10120   return decode_max8_function(r);                                              \
10121 }
10122 
10123 UPB_VARINT_DECODER_CHECK2(branch32, upb_vdecode_max8_branch32)
10124 UPB_VARINT_DECODER_CHECK2(branch64, upb_vdecode_max8_branch64)
10125 #undef UPB_VARINT_DECODER_CHECK2
10126 
10127 /* Our canonical functions for decoding varints, based on the currently
10128  * favored best-performing implementations. */
10129 UPB_INLINE upb_decoderet upb_vdecode_fast(const char *p) {
10130   if (sizeof(long) == 8)
10131     return upb_vdecode_check2_branch64(p);
10132   else
10133     return upb_vdecode_check2_branch32(p);
10134 }
10135 
10136 
10137 /* Encoding *******************************************************************/
10138 
10139 UPB_INLINE int upb_value_size(uint64_t val) {
10140 #ifdef __GNUC__
10141   int high_bit = 63 - __builtin_clzll(val);  /* 0-based, undef if val == 0. */
10142 #else
10143   int high_bit = 0;
10144   uint64_t tmp = val;
10145   while(tmp >>= 1) high_bit++;
10146 #endif
10147   return val == 0 ? 1 : high_bit / 8 + 1;
10148 }
10149 
10150 /* Encodes a 64-bit varint into buf (which must be >=UPB_PB_VARINT_MAX_LEN
10151  * bytes long), returning how many bytes were used.
10152  *
10153  * TODO: benchmark and optimize if necessary. */
10154 UPB_INLINE size_t upb_vencode64(uint64_t val, char *buf) {
10155   size_t i;
10156   if (val == 0) { buf[0] = 0; return 1; }
10157   i = 0;
10158   while (val) {
10159     uint8_t byte = val & 0x7fU;
10160     val >>= 7;
10161     if (val) byte |= 0x80U;
10162     buf[i++] = byte;
10163   }
10164   return i;
10165 }
10166 
10167 UPB_INLINE size_t upb_varint_size(uint64_t val) {
10168   char buf[UPB_PB_VARINT_MAX_LEN];
10169   return upb_vencode64(val, buf);
10170 }
10171 
10172 /* Encodes a 32-bit varint, *not* sign-extended. */
10173 UPB_INLINE uint64_t upb_vencode32(uint32_t val) {
10174   char buf[UPB_PB_VARINT_MAX_LEN];
10175   size_t bytes = upb_vencode64(val, buf);
10176   uint64_t ret = 0;
10177   UPB_ASSERT(bytes <= 5);
10178   memcpy(&ret, buf, bytes);
10179   UPB_ASSERT(ret <= 0xffffffffffU);
10180   return ret;
10181 }
10182 
10183 #ifdef __cplusplus
10184 }  /* extern "C" */
10185 #endif
10186 
10187 #endif  /* UPB_VARINT_DECODER_H_ */
10188 /*
10189 ** upb::pb::Encoder (upb_pb_encoder)
10190 **
10191 ** Implements a set of upb_handlers that write protobuf data to the binary wire
10192 ** format.
10193 **
10194 ** This encoder implementation does not have any access to any out-of-band or
10195 ** precomputed lengths for submessages, so it must buffer submessages internally
10196 ** before it can emit the first byte.
10197 */
10198 
10199 #ifndef UPB_ENCODER_H_
10200 #define UPB_ENCODER_H_
10201 
10202 
10203 #ifdef __cplusplus
10204 namespace upb {
10205 namespace pb {
10206 class Encoder;
10207 }  /* namespace pb */
10208 }  /* namespace upb */
10209 #endif
10210 
10211 UPB_DECLARE_TYPE(upb::pb::Encoder, upb_pb_encoder)
10212 
10213 #define UPB_PBENCODER_MAX_NESTING 100
10214 
10215 /* upb::pb::Encoder ***********************************************************/
10216 
10217 /* Preallocation hint: decoder won't allocate more bytes than this when first
10218  * constructed.  This hint may be an overestimate for some build configurations.
10219  * But if the decoder library is upgraded without recompiling the application,
10220  * it may be an underestimate. */
10221 #define UPB_PB_ENCODER_SIZE 768
10222 
10223 #ifdef __cplusplus
10224 
10225 class upb::pb::Encoder {
10226  public:
10227   /* Creates a new encoder in the given environment.  The Handlers must have
10228    * come from NewHandlers() below. */
10229   static Encoder* Create(Environment* env, const Handlers* handlers,
10230                          BytesSink* output);
10231 
10232   /* The input to the encoder. */
10233   Sink* input();
10234 
10235   /* Creates a new set of handlers for this MessageDef. */
10236   static reffed_ptr<const Handlers> NewHandlers(const MessageDef* msg);
10237 
10238   static const size_t kSize = UPB_PB_ENCODER_SIZE;
10239 
10240  private:
10241   UPB_DISALLOW_POD_OPS(Encoder, upb::pb::Encoder)
10242 };
10243 
10244 #endif
10245 
10246 UPB_BEGIN_EXTERN_C
10247 
10248 const upb_handlers *upb_pb_encoder_newhandlers(const upb_msgdef *m,
10249                                                const void *owner);
10250 upb_sink *upb_pb_encoder_input(upb_pb_encoder *p);
10251 upb_pb_encoder* upb_pb_encoder_create(upb_env* e, const upb_handlers* h,
10252                                       upb_bytessink* output);
10253 
10254 UPB_END_EXTERN_C
10255 
10256 #ifdef __cplusplus
10257 
10258 namespace upb {
10259 namespace pb {
10260 inline Encoder* Encoder::Create(Environment* env, const Handlers* handlers,
10261                                 BytesSink* output) {
10262   return upb_pb_encoder_create(env, handlers, output);
10263 }
10264 inline Sink* Encoder::input() {
10265   return upb_pb_encoder_input(this);
10266 }
10267 inline reffed_ptr<const Handlers> Encoder::NewHandlers(
10268     const upb::MessageDef *md) {
10269   const Handlers* h = upb_pb_encoder_newhandlers(md, &h);
10270   return reffed_ptr<const Handlers>(h, &h);
10271 }
10272 }  /* namespace pb */
10273 }  /* namespace upb */
10274 
10275 #endif
10276 
10277 #endif  /* UPB_ENCODER_H_ */
10278 /*
10279 ** upb's core components like upb_decoder and upb_msg are carefully designed to
10280 ** avoid depending on each other for maximum orthogonality.  In other words,
10281 ** you can use a upb_decoder to decode into *any* kind of structure; upb_msg is
10282 ** just one such structure.  A upb_msg can be serialized/deserialized into any
10283 ** format, protobuf binary format is just one such format.
10284 **
10285 ** However, for convenience we provide functions here for doing common
10286 ** operations like deserializing protobuf binary format into a upb_msg.  The
10287 ** compromise is that this file drags in almost all of upb as a dependency,
10288 ** which could be undesirable if you're trying to use a trimmed-down build of
10289 ** upb.
10290 **
10291 ** While these routines are convenient, they do not reuse any encoding/decoding
10292 ** state.  For example, if a decoder is JIT-based, it will be re-JITted every
10293 ** time these functions are called.  For this reason, if you are parsing lots
10294 ** of data and efficiency is an issue, these may not be the best functions to
10295 ** use (though they are useful for prototyping, before optimizing).
10296 */
10297 
10298 #ifndef UPB_GLUE_H
10299 #define UPB_GLUE_H
10300 
10301 #include <stdbool.h>
10302 
10303 #ifdef __cplusplus
10304 #include <vector>
10305 
10306 extern "C" {
10307 #endif
10308 
10309 /* Loads a binary descriptor and returns a NULL-terminated array of unfrozen
10310  * filedefs.  The caller owns the returned array, which must be freed with
10311  * upb_gfree(). */
10312 upb_filedef **upb_loaddescriptor(const char *buf, size_t n, const void *owner,
10313                                  upb_status *status);
10314 
10315 #ifdef __cplusplus
10316 }  /* extern "C" */
10317 
10318 namespace upb {
10319 
10320 inline bool LoadDescriptor(const char* buf, size_t n, Status* status,
10321                            std::vector<reffed_ptr<FileDef> >* files) {
10322   FileDef** parsed_files = upb_loaddescriptor(buf, n, &parsed_files, status);
10323 
10324   if (parsed_files) {
10325     FileDef** p = parsed_files;
10326     while (*p) {
10327       files->push_back(reffed_ptr<FileDef>(*p, &parsed_files));
10328       ++p;
10329     }
10330     free(parsed_files);
10331     return true;
10332   } else {
10333     return false;
10334   }
10335 }
10336 
10337 /* Templated so it can accept both string and std::string. */
10338 template <typename T>
10339 bool LoadDescriptor(const T& desc, Status* status,
10340                     std::vector<reffed_ptr<FileDef> >* files) {
10341   return LoadDescriptor(desc.c_str(), desc.size(), status, files);
10342 }
10343 
10344 }  /* namespace upb */
10345 
10346 #endif
10347 
10348 #endif  /* UPB_GLUE_H */
10349 /*
10350 ** upb::pb::TextPrinter (upb_textprinter)
10351 **
10352 ** Handlers for writing to protobuf text format.
10353 */
10354 
10355 #ifndef UPB_TEXT_H_
10356 #define UPB_TEXT_H_
10357 
10358 
10359 #ifdef __cplusplus
10360 namespace upb {
10361 namespace pb {
10362 class TextPrinter;
10363 }  /* namespace pb */
10364 }  /* namespace upb */
10365 #endif
10366 
10367 UPB_DECLARE_TYPE(upb::pb::TextPrinter, upb_textprinter)
10368 
10369 #ifdef __cplusplus
10370 
10371 class upb::pb::TextPrinter {
10372  public:
10373   /* The given handlers must have come from NewHandlers().  It must outlive the
10374    * TextPrinter. */
10375   static TextPrinter *Create(Environment *env, const upb::Handlers *handlers,
10376                              BytesSink *output);
10377 
10378   void SetSingleLineMode(bool single_line);
10379 
10380   Sink* input();
10381 
10382   /* If handler caching becomes a requirement we can add a code cache as in
10383    * decoder.h */
10384   static reffed_ptr<const Handlers> NewHandlers(const MessageDef* md);
10385 };
10386 
10387 #endif
10388 
10389 UPB_BEGIN_EXTERN_C
10390 
10391 /* C API. */
10392 upb_textprinter *upb_textprinter_create(upb_env *env, const upb_handlers *h,
10393                                         upb_bytessink *output);
10394 void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line);
10395 upb_sink *upb_textprinter_input(upb_textprinter *p);
10396 
10397 const upb_handlers *upb_textprinter_newhandlers(const upb_msgdef *m,
10398                                                 const void *owner);
10399 
10400 UPB_END_EXTERN_C
10401 
10402 #ifdef __cplusplus
10403 
10404 namespace upb {
10405 namespace pb {
10406 inline TextPrinter *TextPrinter::Create(Environment *env,
10407                                         const upb::Handlers *handlers,
10408                                         BytesSink *output) {
10409   return upb_textprinter_create(env, handlers, output);
10410 }
10411 inline void TextPrinter::SetSingleLineMode(bool single_line) {
10412   upb_textprinter_setsingleline(this, single_line);
10413 }
10414 inline Sink* TextPrinter::input() {
10415   return upb_textprinter_input(this);
10416 }
10417 inline reffed_ptr<const Handlers> TextPrinter::NewHandlers(
10418     const MessageDef *md) {
10419   const Handlers* h = upb_textprinter_newhandlers(md, &h);
10420   return reffed_ptr<const Handlers>(h, &h);
10421 }
10422 }  /* namespace pb */
10423 }  /* namespace upb */
10424 
10425 #endif
10426 
10427 #endif  /* UPB_TEXT_H_ */
10428 /*
10429 ** upb::json::Parser (upb_json_parser)
10430 **
10431 ** Parses JSON according to a specific schema.
10432 ** Support for parsing arbitrary JSON (schema-less) will be added later.
10433 */
10434 
10435 #ifndef UPB_JSON_PARSER_H_
10436 #define UPB_JSON_PARSER_H_
10437 
10438 
10439 #ifdef __cplusplus
10440 namespace upb {
10441 namespace json {
10442 class Parser;
10443 class ParserMethod;
10444 }  /* namespace json */
10445 }  /* namespace upb */
10446 #endif
10447 
10448 UPB_DECLARE_TYPE(upb::json::Parser, upb_json_parser)
10449 UPB_DECLARE_DERIVED_TYPE(upb::json::ParserMethod, upb::RefCounted,
10450                          upb_json_parsermethod, upb_refcounted)
10451 
10452 /* upb::json::Parser **********************************************************/
10453 
10454 /* Preallocation hint: parser won't allocate more bytes than this when first
10455  * constructed.  This hint may be an overestimate for some build configurations.
10456  * But if the parser library is upgraded without recompiling the application,
10457  * it may be an underestimate. */
10458 #define UPB_JSON_PARSER_SIZE 5712
10459 
10460 #ifdef __cplusplus
10461 
10462 /* Parses an incoming BytesStream, pushing the results to the destination
10463  * sink. */
10464 class upb::json::Parser {
10465  public:
10466   static Parser* Create(Environment* env, const ParserMethod* method,
10467                         const SymbolTable* symtab,
10468                         Sink* output, bool ignore_json_unknown);
10469 
10470   BytesSink* input();
10471 
10472  private:
10473   UPB_DISALLOW_POD_OPS(Parser, upb::json::Parser)
10474 };
10475 
10476 class upb::json::ParserMethod {
10477  public:
10478   /* Include base methods from upb::ReferenceCounted. */
10479   UPB_REFCOUNTED_CPPMETHODS
10480 
10481   /* Returns handlers for parsing according to the specified schema. */
10482   static reffed_ptr<const ParserMethod> New(const upb::MessageDef* md);
10483 
10484   /* The destination handlers that are statically bound to this method.
10485    * This method is only capable of outputting to a sink that uses these
10486    * handlers. */
10487   const Handlers* dest_handlers() const;
10488 
10489   /* The input handlers for this decoder method. */
10490   const BytesHandler* input_handler() const;
10491 
10492  private:
10493   UPB_DISALLOW_POD_OPS(ParserMethod, upb::json::ParserMethod)
10494 };
10495 
10496 #endif
10497 
10498 UPB_BEGIN_EXTERN_C
10499 
10500 upb_json_parser* upb_json_parser_create(upb_env* e,
10501                                         const upb_json_parsermethod* m,
10502                                         const upb_symtab* symtab,
10503                                         upb_sink* output,
10504                                         bool ignore_json_unknown);
10505 upb_bytessink *upb_json_parser_input(upb_json_parser *p);
10506 
10507 upb_json_parsermethod* upb_json_parsermethod_new(const upb_msgdef* md,
10508                                                  const void* owner);
10509 const upb_handlers *upb_json_parsermethod_desthandlers(
10510     const upb_json_parsermethod *m);
10511 const upb_byteshandler *upb_json_parsermethod_inputhandler(
10512     const upb_json_parsermethod *m);
10513 
10514 /* Include refcounted methods like upb_json_parsermethod_ref(). */
10515 UPB_REFCOUNTED_CMETHODS(upb_json_parsermethod, upb_json_parsermethod_upcast)
10516 
10517 UPB_END_EXTERN_C
10518 
10519 #ifdef __cplusplus
10520 
10521 namespace upb {
10522 namespace json {
10523 inline Parser* Parser::Create(Environment* env, const ParserMethod* method,
10524                               const SymbolTable* symtab,
10525                               Sink* output, bool ignore_json_unknown) {
10526   return upb_json_parser_create(
10527       env, method, symtab, output, ignore_json_unknown);
10528 }
10529 inline BytesSink* Parser::input() {
10530   return upb_json_parser_input(this);
10531 }
10532 
10533 inline const Handlers* ParserMethod::dest_handlers() const {
10534   return upb_json_parsermethod_desthandlers(this);
10535 }
10536 inline const BytesHandler* ParserMethod::input_handler() const {
10537   return upb_json_parsermethod_inputhandler(this);
10538 }
10539 /* static */
10540 inline reffed_ptr<const ParserMethod> ParserMethod::New(
10541     const MessageDef* md) {
10542   const upb_json_parsermethod *m = upb_json_parsermethod_new(md, &m);
10543   return reffed_ptr<const ParserMethod>(m, &m);
10544 }
10545 
10546 }  /* namespace json */
10547 }  /* namespace upb */
10548 
10549 #endif
10550 
10551 
10552 #endif  /* UPB_JSON_PARSER_H_ */
10553 /*
10554 ** upb::json::Printer
10555 **
10556 ** Handlers that emit JSON according to a specific protobuf schema.
10557 */
10558 
10559 #ifndef UPB_JSON_TYPED_PRINTER_H_
10560 #define UPB_JSON_TYPED_PRINTER_H_
10561 
10562 
10563 #ifdef __cplusplus
10564 namespace upb {
10565 namespace json {
10566 class Printer;
10567 }  /* namespace json */
10568 }  /* namespace upb */
10569 #endif
10570 
10571 UPB_DECLARE_TYPE(upb::json::Printer, upb_json_printer)
10572 
10573 
10574 /* upb::json::Printer *********************************************************/
10575 
10576 #define UPB_JSON_PRINTER_SIZE 192
10577 
10578 #ifdef __cplusplus
10579 
10580 /* Prints an incoming stream of data to a BytesSink in JSON format. */
10581 class upb::json::Printer {
10582  public:
10583   static Printer* Create(Environment* env, const upb::Handlers* handlers,
10584                          BytesSink* output);
10585 
10586   /* The input to the printer. */
10587   Sink* input();
10588 
10589   /* Returns handlers for printing according to the specified schema.
10590    * If preserve_proto_fieldnames is true, the output JSON will use the
10591    * original .proto field names (ie. {"my_field":3}) instead of using
10592    * camelCased names, which is the default: (eg. {"myField":3}). */
10593   static reffed_ptr<const Handlers> NewHandlers(const upb::MessageDef* md,
10594                                                 bool preserve_proto_fieldnames);
10595 
10596   static const size_t kSize = UPB_JSON_PRINTER_SIZE;
10597 
10598  private:
10599   UPB_DISALLOW_POD_OPS(Printer, upb::json::Printer)
10600 };
10601 
10602 #endif
10603 
10604 UPB_BEGIN_EXTERN_C
10605 
10606 /* Native C API. */
10607 upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h,
10608                                           upb_bytessink *output);
10609 upb_sink *upb_json_printer_input(upb_json_printer *p);
10610 const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md,
10611                                                  bool preserve_fieldnames,
10612                                                  const void *owner);
10613 
10614 UPB_END_EXTERN_C
10615 
10616 #ifdef __cplusplus
10617 
10618 namespace upb {
10619 namespace json {
10620 inline Printer* Printer::Create(Environment* env, const upb::Handlers* handlers,
10621                                 BytesSink* output) {
10622   return upb_json_printer_create(env, handlers, output);
10623 }
10624 inline Sink* Printer::input() { return upb_json_printer_input(this); }
10625 inline reffed_ptr<const Handlers> Printer::NewHandlers(
10626     const upb::MessageDef *md, bool preserve_proto_fieldnames) {
10627   const Handlers* h = upb_json_printer_newhandlers(
10628       md, preserve_proto_fieldnames, &h);
10629   return reffed_ptr<const Handlers>(h, &h);
10630 }
10631 }  /* namespace json */
10632 }  /* namespace upb */
10633 
10634 #endif
10635 
10636 #endif  /* UPB_JSON_TYPED_PRINTER_H_ */
10637 
10638 #undef UPB_SIZE
10639 #undef UPB_FIELD_AT
10640 #undef UPB_READ_ONEOF
10641 #undef UPB_WRITE_ONEOF
10642