blob: 0be5b296fb6a126d724b2b2ead05fd7f0dc70fe9 [file] [log] [blame]
Chris Fallin91473dc2014-12-12 15:58:26 -08001// Amalgamated source file
2/*
Josh Haberman181c7f22015-07-15 11:05:10 -07003** Defs are upb's internal representation of the constructs that can appear
4** in a .proto file:
5**
6** - upb::MessageDef (upb_msgdef): describes a "message" construct.
7** - upb::FieldDef (upb_fielddef): describes a message field.
8** - upb::EnumDef (upb_enumdef): describes an enum.
9** - upb::OneofDef (upb_oneofdef): describes a oneof.
10** - upb::Def (upb_def): base class of all the others.
11**
12** TODO: definitions of services.
13**
14** Like upb_refcounted objects, defs are mutable only until frozen, and are
15** only thread-safe once frozen.
16**
17** This is a mixed C/C++ interface that offers a full API to both languages.
18** See the top-level README for more information.
19*/
Chris Fallin91473dc2014-12-12 15:58:26 -080020
21#ifndef UPB_DEF_H_
22#define UPB_DEF_H_
23
24/*
Josh Haberman181c7f22015-07-15 11:05:10 -070025** upb::RefCounted (upb_refcounted)
26**
27** A refcounting scheme that supports circular refs. It accomplishes this by
28** partitioning the set of objects into groups such that no cycle spans groups;
29** we can then reference-count the group as a whole and ignore refs within the
30** group. When objects are mutable, these groups are computed very
31** conservatively; we group any objects that have ever had a link between them.
32** When objects are frozen, we compute strongly-connected components which
33** allows us to be precise and only group objects that are actually cyclic.
34**
35** This is a mixed C/C++ interface that offers a full API to both languages.
36** See the top-level README for more information.
37*/
Chris Fallin91473dc2014-12-12 15:58:26 -080038
39#ifndef UPB_REFCOUNTED_H_
40#define UPB_REFCOUNTED_H_
41
42/*
Josh Haberman181c7f22015-07-15 11:05:10 -070043** upb_table
44**
45** This header is INTERNAL-ONLY! Its interfaces are not public or stable!
46** This file defines very fast int->upb_value (inttable) and string->upb_value
47** (strtable) hash tables.
48**
49** The table uses chained scatter with Brent's variation (inspired by the Lua
50** implementation of hash tables). The hash function for strings is Austin
51** Appleby's "MurmurHash."
52**
53** The inttable uses uintptr_t as its key, which guarantees it can be used to
54** store pointers or integers of at least 32 bits (upb isn't really useful on
55** systems where sizeof(void*) < 4).
56**
57** The table must be homogenous (all values of the same type). In debug
58** mode, we check this on insert and lookup.
59*/
Chris Fallin91473dc2014-12-12 15:58:26 -080060
61#ifndef UPB_TABLE_H_
62#define UPB_TABLE_H_
63
64#include <assert.h>
65#include <stdint.h>
66#include <string.h>
67/*
Josh Haberman181c7f22015-07-15 11:05:10 -070068** This file contains shared definitions that are widely used across upb.
69**
70** This is a mixed C/C++ interface that offers a full API to both languages.
71** See the top-level README for more information.
72*/
Chris Fallin91473dc2014-12-12 15:58:26 -080073
74#ifndef UPB_H_
75#define UPB_H_
76
77#include <assert.h>
78#include <stdarg.h>
79#include <stdbool.h>
80#include <stddef.h>
81
Josh Habermane8ed0212015-06-08 17:56:03 -070082/* UPB_INLINE: inline if possible, emit standalone code if required. */
Chris Fallin91473dc2014-12-12 15:58:26 -080083#ifdef __cplusplus
84#define UPB_INLINE inline
Josh Habermane8ed0212015-06-08 17:56:03 -070085#elif defined (__GNUC__)
86#define UPB_INLINE static __inline__
Chris Fallin91473dc2014-12-12 15:58:26 -080087#else
Josh Habermane8ed0212015-06-08 17:56:03 -070088#define UPB_INLINE static
Chris Fallin91473dc2014-12-12 15:58:26 -080089#endif
90
Josh Habermane8ed0212015-06-08 17:56:03 -070091/* Define UPB_BIG_ENDIAN manually if you're on big endian and your compiler
92 * doesn't provide these preprocessor symbols. */
93#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
94#define UPB_BIG_ENDIAN
95#endif
96
97/* Macros for function attributes on compilers that support them. */
Chris Fallind3262772015-05-14 18:24:26 -070098#ifdef __GNUC__
Josh Habermane8ed0212015-06-08 17:56:03 -070099#define UPB_FORCEINLINE __inline__ __attribute__((always_inline))
Chris Fallind3262772015-05-14 18:24:26 -0700100#define UPB_NOINLINE __attribute__((noinline))
Josh Habermane8ed0212015-06-08 17:56:03 -0700101#define UPB_NORETURN __attribute__((__noreturn__))
102#else /* !defined(__GNUC__) */
Chris Fallind3262772015-05-14 18:24:26 -0700103#define UPB_FORCEINLINE
104#define UPB_NOINLINE
Josh Habermane8ed0212015-06-08 17:56:03 -0700105#define UPB_NORETURN
Chris Fallind3262772015-05-14 18:24:26 -0700106#endif
107
Josh Habermane8ed0212015-06-08 17:56:03 -0700108/* A few hacky workarounds for functions not in C89.
109 * For internal use only!
110 * TODO(haberman): fix these by including our own implementations, or finding
111 * another workaround.
112 */
113#ifdef __GNUC__
114#define _upb_snprintf __builtin_snprintf
115#define _upb_vsnprintf __builtin_vsnprintf
116#define _upb_va_copy(a, b) __va_copy(a, b)
117#elif __STDC_VERSION__ >= 199901L
118/* C99 versions. */
119#define _upb_snprintf snprintf
120#define _upb_vsnprintf vsnprintf
121#define _upb_va_copy(a, b) va_copy(a, b)
122#else
123#error Need implementations of [v]snprintf and va_copy
Chris Fallin91473dc2014-12-12 15:58:26 -0800124#endif
125
Josh Habermane8ed0212015-06-08 17:56:03 -0700126
Chris Fallin91473dc2014-12-12 15:58:26 -0800127#if ((defined(__cplusplus) && __cplusplus >= 201103L) || \
128 defined(__GXX_EXPERIMENTAL_CXX0X__)) && !defined(UPB_NO_CXX11)
129#define UPB_CXX11
130#endif
131
Josh Habermane8ed0212015-06-08 17:56:03 -0700132/* UPB_DISALLOW_COPY_AND_ASSIGN()
133 * UPB_DISALLOW_POD_OPS()
134 *
135 * Declare these in the "private" section of a C++ class to forbid copy/assign
136 * or all POD ops (construct, destruct, copy, assign) on that class. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800137#ifdef UPB_CXX11
138#include <type_traits>
139#define UPB_DISALLOW_COPY_AND_ASSIGN(class_name) \
140 class_name(const class_name&) = delete; \
141 void operator=(const class_name&) = delete;
142#define UPB_DISALLOW_POD_OPS(class_name, full_class_name) \
143 class_name() = delete; \
144 ~class_name() = delete; \
Chris Fallin91473dc2014-12-12 15:58:26 -0800145 UPB_DISALLOW_COPY_AND_ASSIGN(class_name)
146#define UPB_ASSERT_STDLAYOUT(type) \
147 static_assert(std::is_standard_layout<type>::value, \
148 #type " must be standard layout");
Josh Habermane8ed0212015-06-08 17:56:03 -0700149#else /* !defined(UPB_CXX11) */
Chris Fallin91473dc2014-12-12 15:58:26 -0800150#define UPB_DISALLOW_COPY_AND_ASSIGN(class_name) \
151 class_name(const class_name&); \
152 void operator=(const class_name&);
153#define UPB_DISALLOW_POD_OPS(class_name, full_class_name) \
154 class_name(); \
155 ~class_name(); \
Chris Fallin91473dc2014-12-12 15:58:26 -0800156 UPB_DISALLOW_COPY_AND_ASSIGN(class_name)
157#define UPB_ASSERT_STDLAYOUT(type)
158#endif
159
Josh Habermane8ed0212015-06-08 17:56:03 -0700160/* UPB_DECLARE_TYPE()
161 * UPB_DECLARE_DERIVED_TYPE()
162 * UPB_DECLARE_DERIVED_TYPE2()
163 *
164 * Macros for declaring C and C++ types both, including inheritance.
165 * The inheritance doesn't use real C++ inheritance, to stay compatible with C.
166 *
167 * These macros also provide upcasts:
168 * - in C: types-specific functions (ie. upb_foo_upcast(foo))
169 * - in C++: upb::upcast(foo) along with implicit conversions
170 *
171 * Downcasts are not provided, but upb/def.h defines downcasts for upb::Def. */
172
173#define UPB_C_UPCASTS(ty, base) \
174 UPB_INLINE base *ty ## _upcast_mutable(ty *p) { return (base*)p; } \
175 UPB_INLINE const base *ty ## _upcast(const ty *p) { return (const base*)p; }
176
177#define UPB_C_UPCASTS2(ty, base, base2) \
178 UPB_C_UPCASTS(ty, base) \
179 UPB_INLINE base2 *ty ## _upcast2_mutable(ty *p) { return (base2*)p; } \
180 UPB_INLINE const base2 *ty ## _upcast2(const ty *p) { return (const base2*)p; }
Chris Fallin91473dc2014-12-12 15:58:26 -0800181
182#ifdef __cplusplus
183
Chris Fallin91473dc2014-12-12 15:58:26 -0800184#define UPB_BEGIN_EXTERN_C extern "C" {
185#define UPB_END_EXTERN_C }
Josh Habermane8ed0212015-06-08 17:56:03 -0700186#define UPB_PRIVATE_FOR_CPP private:
187#define UPB_DECLARE_TYPE(cppname, cname) typedef cppname cname;
188
189#define UPB_DECLARE_DERIVED_TYPE(cppname, cppbase, cname, cbase) \
190 UPB_DECLARE_TYPE(cppname, cname) \
191 UPB_C_UPCASTS(cname, cbase) \
Chris Fallin91473dc2014-12-12 15:58:26 -0800192 namespace upb { \
193 template <> \
194 class Pointer<cppname> : public PointerBase<cppname, cppbase> { \
195 public: \
Josh Habermanf654d492016-02-18 11:07:51 -0800196 explicit Pointer(cppname* ptr) \
197 : PointerBase<cppname, cppbase>(ptr) {} \
Chris Fallin91473dc2014-12-12 15:58:26 -0800198 }; \
199 template <> \
200 class Pointer<const cppname> \
201 : public PointerBase<const cppname, const cppbase> { \
202 public: \
Josh Habermanf654d492016-02-18 11:07:51 -0800203 explicit Pointer(const cppname* ptr) \
204 : PointerBase<const cppname, const cppbase>(ptr) {} \
Chris Fallin91473dc2014-12-12 15:58:26 -0800205 }; \
206 }
Josh Habermane8ed0212015-06-08 17:56:03 -0700207
208#define UPB_DECLARE_DERIVED_TYPE2(cppname, cppbase, cppbase2, cname, cbase, \
209 cbase2) \
210 UPB_DECLARE_TYPE(cppname, cname) \
211 UPB_C_UPCASTS2(cname, cbase, cbase2) \
Chris Fallin91473dc2014-12-12 15:58:26 -0800212 namespace upb { \
213 template <> \
214 class Pointer<cppname> : public PointerBase2<cppname, cppbase, cppbase2> { \
215 public: \
Josh Habermanf654d492016-02-18 11:07:51 -0800216 explicit Pointer(cppname* ptr) \
217 : PointerBase2<cppname, cppbase, cppbase2>(ptr) {} \
Chris Fallin91473dc2014-12-12 15:58:26 -0800218 }; \
219 template <> \
220 class Pointer<const cppname> \
221 : public PointerBase2<const cppname, const cppbase, const cppbase2> { \
222 public: \
Josh Habermanf654d492016-02-18 11:07:51 -0800223 explicit Pointer(const cppname* ptr) \
224 : PointerBase2<const cppname, const cppbase, const cppbase2>(ptr) {} \
Chris Fallin91473dc2014-12-12 15:58:26 -0800225 }; \
226 }
227
Josh Habermane8ed0212015-06-08 17:56:03 -0700228#else /* !defined(__cplusplus) */
Chris Fallin91473dc2014-12-12 15:58:26 -0800229
Josh Habermane8ed0212015-06-08 17:56:03 -0700230#define UPB_BEGIN_EXTERN_C
231#define UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -0800232#define UPB_PRIVATE_FOR_CPP
233#define UPB_DECLARE_TYPE(cppname, cname) \
234 struct cname; \
235 typedef struct cname cname;
Josh Habermane8ed0212015-06-08 17:56:03 -0700236#define UPB_DECLARE_DERIVED_TYPE(cppname, cppbase, cname, cbase) \
237 UPB_DECLARE_TYPE(cppname, cname) \
238 UPB_C_UPCASTS(cname, cbase)
239#define UPB_DECLARE_DERIVED_TYPE2(cppname, cppbase, cppbase2, \
240 cname, cbase, cbase2) \
241 UPB_DECLARE_TYPE(cppname, cname) \
242 UPB_C_UPCASTS2(cname, cbase, cbase2)
Chris Fallin91473dc2014-12-12 15:58:26 -0800243
Josh Habermane8ed0212015-06-08 17:56:03 -0700244#endif /* defined(__cplusplus) */
Chris Fallin91473dc2014-12-12 15:58:26 -0800245
246#define UPB_MAX(x, y) ((x) > (y) ? (x) : (y))
247#define UPB_MIN(x, y) ((x) < (y) ? (x) : (y))
248
249#define UPB_UNUSED(var) (void)var
250
Josh Habermane8ed0212015-06-08 17:56:03 -0700251/* For asserting something about a variable when the variable is not used for
252 * anything else. This prevents "unused variable" warnings when compiling in
253 * debug mode. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800254#define UPB_ASSERT_VAR(var, predicate) UPB_UNUSED(var); assert(predicate)
255
Josh Habermane8ed0212015-06-08 17:56:03 -0700256/* Generic function type. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800257typedef void upb_func();
258
Josh Habermane8ed0212015-06-08 17:56:03 -0700259/* C++ Casts ******************************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -0800260
261#ifdef __cplusplus
262
Chris Fallin91473dc2014-12-12 15:58:26 -0800263namespace upb {
264
Chris Fallin91473dc2014-12-12 15:58:26 -0800265template <class T> class Pointer;
266
Josh Habermane8ed0212015-06-08 17:56:03 -0700267/* Casts to a subclass. The caller must know that cast is correct; an
268 * incorrect cast will throw an assertion failure in debug mode.
269 *
270 * Example:
271 * upb::Def* def = GetDef();
272 * // Assert-fails if this was not actually a MessageDef.
273 * upb::MessgeDef* md = upb::down_cast<upb::MessageDef>(def);
274 *
275 * Note that downcasts are only defined for some types (at the moment you can
276 * only downcast from a upb::Def to a specific Def type). */
277template<class To, class From> To down_cast(From* f);
278
279/* Casts to a subclass. If the class does not actually match the given To type,
280 * returns NULL.
281 *
282 * Example:
283 * upb::Def* def = GetDef();
284 * // md will be NULL if this was not actually a MessageDef.
285 * upb::MessgeDef* md = upb::down_cast<upb::MessageDef>(def);
286 *
287 * Note that dynamic casts are only defined for some types (at the moment you
288 * can only downcast from a upb::Def to a specific Def type).. */
289template<class To, class From> To dyn_cast(From* f);
290
291/* Casts to any base class, or the type itself (ie. can be a no-op).
292 *
293 * Example:
294 * upb::MessageDef* md = GetDef();
295 * // This will fail to compile if this wasn't actually a base class.
296 * upb::Def* def = upb::upcast(md);
297 */
Chris Fallin91473dc2014-12-12 15:58:26 -0800298template <class T> inline Pointer<T> upcast(T *f) { return Pointer<T>(f); }
299
Josh Habermane8ed0212015-06-08 17:56:03 -0700300/* Attempt upcast to specific base class.
301 *
302 * Example:
303 * upb::MessageDef* md = GetDef();
304 * upb::upcast_to<upb::Def>(md)->MethodOnDef();
305 */
306template <class T, class F> inline T* upcast_to(F *f) {
307 return static_cast<T*>(upcast(f));
308}
309
310/* PointerBase<T>: implementation detail of upb::upcast().
311 * It is implicitly convertable to pointers to the Base class(es).
312 */
Chris Fallin91473dc2014-12-12 15:58:26 -0800313template <class T, class Base>
314class PointerBase {
315 public:
316 explicit PointerBase(T* ptr) : ptr_(ptr) {}
317 operator T*() { return ptr_; }
Josh Habermane8ed0212015-06-08 17:56:03 -0700318 operator Base*() { return (Base*)ptr_; }
Chris Fallin91473dc2014-12-12 15:58:26 -0800319
320 private:
321 T* ptr_;
322};
323
324template <class T, class Base, class Base2>
325class PointerBase2 : public PointerBase<T, Base> {
326 public:
327 explicit PointerBase2(T* ptr) : PointerBase<T, Base>(ptr) {}
328 operator Base2*() { return Pointer<Base>(*this); }
329};
330
331}
332
333#endif
334
335
336/* upb::reffed_ptr ************************************************************/
337
338#ifdef __cplusplus
339
Josh Habermane8ed0212015-06-08 17:56:03 -0700340#include <algorithm> /* For std::swap(). */
Chris Fallin91473dc2014-12-12 15:58:26 -0800341
342namespace upb {
343
Josh Habermane8ed0212015-06-08 17:56:03 -0700344/* Provides RAII semantics for upb refcounted objects. Each reffed_ptr owns a
345 * ref on whatever object it points to (if any). */
Chris Fallin91473dc2014-12-12 15:58:26 -0800346template <class T> class reffed_ptr {
347 public:
348 reffed_ptr() : ptr_(NULL) {}
349
Josh Habermane8ed0212015-06-08 17:56:03 -0700350 /* If ref_donor is NULL, takes a new ref, otherwise adopts from ref_donor. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800351 template <class U>
352 reffed_ptr(U* val, const void* ref_donor = NULL)
353 : ptr_(upb::upcast(val)) {
354 if (ref_donor) {
355 assert(ptr_);
356 ptr_->DonateRef(ref_donor, this);
357 } else if (ptr_) {
358 ptr_->Ref(this);
359 }
360 }
361
362 template <class U>
363 reffed_ptr(const reffed_ptr<U>& other)
364 : ptr_(upb::upcast(other.get())) {
365 if (ptr_) ptr_->Ref(this);
366 }
367
368 ~reffed_ptr() { if (ptr_) ptr_->Unref(this); }
369
370 template <class U>
371 reffed_ptr& operator=(const reffed_ptr<U>& other) {
372 reset(other.get());
373 return *this;
374 }
375
376 reffed_ptr& operator=(const reffed_ptr& other) {
377 reset(other.get());
378 return *this;
379 }
380
Josh Habermane8ed0212015-06-08 17:56:03 -0700381 /* TODO(haberman): add C++11 move construction/assignment for greater
382 * efficiency. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800383
384 void swap(reffed_ptr& other) {
385 if (ptr_ == other.ptr_) {
386 return;
387 }
388
389 if (ptr_) ptr_->DonateRef(this, &other);
390 if (other.ptr_) other.ptr_->DonateRef(&other, this);
391 std::swap(ptr_, other.ptr_);
392 }
393
394 T& operator*() const {
395 assert(ptr_);
396 return *ptr_;
397 }
398
399 T* operator->() const {
400 assert(ptr_);
401 return ptr_;
402 }
403
404 T* get() const { return ptr_; }
405
Josh Habermane8ed0212015-06-08 17:56:03 -0700406 /* If ref_donor is NULL, takes a new ref, otherwise adopts from ref_donor. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800407 template <class U>
408 void reset(U* ptr = NULL, const void* ref_donor = NULL) {
409 reffed_ptr(ptr, ref_donor).swap(*this);
410 }
411
412 template <class U>
413 reffed_ptr<U> down_cast() {
414 return reffed_ptr<U>(upb::down_cast<U*>(get()));
415 }
416
417 template <class U>
418 reffed_ptr<U> dyn_cast() {
419 return reffed_ptr<U>(upb::dyn_cast<U*>(get()));
420 }
421
Josh Habermane8ed0212015-06-08 17:56:03 -0700422 /* Plain release() is unsafe; if we were the only owner, it would leak the
423 * object. Instead we provide this: */
Chris Fallin91473dc2014-12-12 15:58:26 -0800424 T* ReleaseTo(const void* new_owner) {
425 T* ret = NULL;
426 ptr_->DonateRef(this, new_owner);
427 std::swap(ret, ptr_);
428 return ret;
429 }
430
431 private:
432 T* ptr_;
433};
434
Josh Habermane8ed0212015-06-08 17:56:03 -0700435} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -0800436
Josh Habermane8ed0212015-06-08 17:56:03 -0700437#endif /* __cplusplus */
Chris Fallin91473dc2014-12-12 15:58:26 -0800438
439
440/* upb::Status ****************************************************************/
441
442#ifdef __cplusplus
443namespace upb {
444class ErrorSpace;
445class Status;
446}
447#endif
448
Josh Habermane8ed0212015-06-08 17:56:03 -0700449UPB_DECLARE_TYPE(upb::ErrorSpace, upb_errorspace)
450UPB_DECLARE_TYPE(upb::Status, upb_status)
Chris Fallin91473dc2014-12-12 15:58:26 -0800451
Josh Habermane8ed0212015-06-08 17:56:03 -0700452/* The maximum length of an error message before it will get truncated. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800453#define UPB_STATUS_MAX_MESSAGE 128
454
Josh Habermane8ed0212015-06-08 17:56:03 -0700455/* An error callback function is used to report errors from some component.
456 * The function can return "true" to indicate that the component should try
457 * to recover and proceed, but this is not always possible. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800458typedef bool upb_errcb_t(void *closure, const upb_status* status);
459
Josh Habermane8ed0212015-06-08 17:56:03 -0700460#ifdef __cplusplus
461class upb::ErrorSpace {
462#else
463struct upb_errorspace {
464#endif
Chris Fallin91473dc2014-12-12 15:58:26 -0800465 const char *name;
Josh Habermane8ed0212015-06-08 17:56:03 -0700466 /* Should the error message in the status object according to this code. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800467 void (*set_message)(upb_status* status, int code);
Josh Habermane8ed0212015-06-08 17:56:03 -0700468};
Chris Fallin91473dc2014-12-12 15:58:26 -0800469
Josh Habermane8ed0212015-06-08 17:56:03 -0700470#ifdef __cplusplus
471
472/* Object representing a success or failure status.
473 * It owns no resources and allocates no memory, so it should work
474 * even in OOM situations. */
475
476class upb::Status {
Chris Fallin91473dc2014-12-12 15:58:26 -0800477 public:
478 Status();
479
Josh Habermane8ed0212015-06-08 17:56:03 -0700480 /* Returns true if there is no error. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800481 bool ok() const;
482
Josh Habermane8ed0212015-06-08 17:56:03 -0700483 /* Optional error space and code, useful if the caller wants to
484 * programmatically check the specific kind of error. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800485 ErrorSpace* error_space();
486 int code() const;
487
488 const char *error_message() const;
489
Josh Habermane8ed0212015-06-08 17:56:03 -0700490 /* The error message will be truncated if it is longer than
491 * UPB_STATUS_MAX_MESSAGE-4. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800492 void SetErrorMessage(const char* msg);
493 void SetFormattedErrorMessage(const char* fmt, ...);
494
Josh Habermane8ed0212015-06-08 17:56:03 -0700495 /* If there is no error message already, this will use the ErrorSpace to
496 * populate the error message for this code. The caller can still call
497 * SetErrorMessage() to give a more specific message. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800498 void SetErrorCode(ErrorSpace* space, int code);
499
Josh Habermane8ed0212015-06-08 17:56:03 -0700500 /* Resets the status to a successful state with no message. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800501 void Clear();
502
503 void CopyFrom(const Status& other);
504
505 private:
Josh Habermane8ed0212015-06-08 17:56:03 -0700506 UPB_DISALLOW_COPY_AND_ASSIGN(Status)
507#else
508struct upb_status {
509#endif
Chris Fallin91473dc2014-12-12 15:58:26 -0800510 bool ok_;
511
Josh Habermane8ed0212015-06-08 17:56:03 -0700512 /* Specific status code defined by some error space (optional). */
Chris Fallin91473dc2014-12-12 15:58:26 -0800513 int code_;
514 upb_errorspace *error_space_;
515
Josh Habermane8ed0212015-06-08 17:56:03 -0700516 /* Error message; NULL-terminated. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800517 char msg[UPB_STATUS_MAX_MESSAGE];
Josh Habermane8ed0212015-06-08 17:56:03 -0700518};
Chris Fallin91473dc2014-12-12 15:58:26 -0800519
520#define UPB_STATUS_INIT {true, 0, NULL, {0}}
521
522#ifdef __cplusplus
523extern "C" {
524#endif
525
Josh Habermane8ed0212015-06-08 17:56:03 -0700526/* The returned string is invalidated by any other call into the status. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800527const char *upb_status_errmsg(const upb_status *status);
528bool upb_ok(const upb_status *status);
529upb_errorspace *upb_status_errspace(const upb_status *status);
530int upb_status_errcode(const upb_status *status);
531
Josh Habermane8ed0212015-06-08 17:56:03 -0700532/* Any of the functions that write to a status object allow status to be NULL,
533 * to support use cases where the function's caller does not care about the
534 * status message. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800535void upb_status_clear(upb_status *status);
536void upb_status_seterrmsg(upb_status *status, const char *msg);
537void upb_status_seterrf(upb_status *status, const char *fmt, ...);
538void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args);
539void upb_status_seterrcode(upb_status *status, upb_errorspace *space, int code);
540void upb_status_copy(upb_status *to, const upb_status *from);
541
542#ifdef __cplusplus
Josh Habermane8ed0212015-06-08 17:56:03 -0700543} /* extern "C" */
Chris Fallin91473dc2014-12-12 15:58:26 -0800544
545namespace upb {
546
Josh Habermane8ed0212015-06-08 17:56:03 -0700547/* C++ Wrappers */
Chris Fallin91473dc2014-12-12 15:58:26 -0800548inline Status::Status() { Clear(); }
549inline bool Status::ok() const { return upb_ok(this); }
550inline const char* Status::error_message() const {
551 return upb_status_errmsg(this);
552}
553inline void Status::SetErrorMessage(const char* msg) {
554 upb_status_seterrmsg(this, msg);
555}
556inline void Status::SetFormattedErrorMessage(const char* fmt, ...) {
557 va_list args;
558 va_start(args, fmt);
559 upb_status_vseterrf(this, fmt, args);
560 va_end(args);
561}
562inline void Status::SetErrorCode(ErrorSpace* space, int code) {
563 upb_status_seterrcode(this, space, code);
564}
565inline void Status::Clear() { upb_status_clear(this); }
566inline void Status::CopyFrom(const Status& other) {
567 upb_status_copy(this, &other);
568}
569
Josh Habermane8ed0212015-06-08 17:56:03 -0700570} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -0800571
572#endif
573
574#endif /* UPB_H_ */
575
576#ifdef __cplusplus
577extern "C" {
578#endif
579
580
581/* upb_value ******************************************************************/
582
Josh Habermane8ed0212015-06-08 17:56:03 -0700583/* A tagged union (stored untagged inside the table) so that we can check that
584 * clients calling table accessors are correctly typed without having to have
585 * an explosion of accessors. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800586typedef enum {
587 UPB_CTYPE_INT32 = 1,
588 UPB_CTYPE_INT64 = 2,
589 UPB_CTYPE_UINT32 = 3,
590 UPB_CTYPE_UINT64 = 4,
591 UPB_CTYPE_BOOL = 5,
592 UPB_CTYPE_CSTR = 6,
593 UPB_CTYPE_PTR = 7,
594 UPB_CTYPE_CONSTPTR = 8,
Josh Habermane8ed0212015-06-08 17:56:03 -0700595 UPB_CTYPE_FPTR = 9
Chris Fallin91473dc2014-12-12 15:58:26 -0800596} upb_ctype_t;
597
Chris Fallin91473dc2014-12-12 15:58:26 -0800598typedef struct {
Josh Habermane8ed0212015-06-08 17:56:03 -0700599 uint64_t val;
Chris Fallin91473dc2014-12-12 15:58:26 -0800600#ifndef NDEBUG
Josh Habermane8ed0212015-06-08 17:56:03 -0700601 /* In debug mode we carry the value type around also so we can check accesses
602 * to be sure the right member is being read. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800603 upb_ctype_t ctype;
604#endif
605} upb_value;
606
Chris Fallin91473dc2014-12-12 15:58:26 -0800607#ifdef NDEBUG
608#define SET_TYPE(dest, val) UPB_UNUSED(val)
Chris Fallin91473dc2014-12-12 15:58:26 -0800609#else
610#define SET_TYPE(dest, val) dest = val
Chris Fallin91473dc2014-12-12 15:58:26 -0800611#endif
612
Josh Habermane8ed0212015-06-08 17:56:03 -0700613/* Like strdup(), which isn't always available since it's not ANSI C. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800614char *upb_strdup(const char *s);
Josh Habermane8ed0212015-06-08 17:56:03 -0700615/* Variant that works with a length-delimited rather than NULL-delimited string,
616 * as supported by strtable. */
Chris Fallinfd1a3ff2015-01-06 15:44:09 -0800617char *upb_strdup2(const char *s, size_t len);
Chris Fallin91473dc2014-12-12 15:58:26 -0800618
Josh Habermane8ed0212015-06-08 17:56:03 -0700619UPB_INLINE void _upb_value_setval(upb_value *v, uint64_t val,
Chris Fallin91473dc2014-12-12 15:58:26 -0800620 upb_ctype_t ctype) {
621 v->val = val;
622 SET_TYPE(v->ctype, ctype);
623}
624
Josh Habermane8ed0212015-06-08 17:56:03 -0700625UPB_INLINE upb_value _upb_value_val(uint64_t val, upb_ctype_t ctype) {
Chris Fallin91473dc2014-12-12 15:58:26 -0800626 upb_value ret;
627 _upb_value_setval(&ret, val, ctype);
628 return ret;
629}
630
Josh Habermane8ed0212015-06-08 17:56:03 -0700631/* For each value ctype, define the following set of functions:
632 *
633 * // Get/set an int32 from a upb_value.
634 * int32_t upb_value_getint32(upb_value val);
635 * void upb_value_setint32(upb_value *val, int32_t cval);
636 *
637 * // Construct a new upb_value from an int32.
638 * upb_value upb_value_int32(int32_t val); */
639#define FUNCS(name, membername, type_t, converter, proto_type) \
Chris Fallin91473dc2014-12-12 15:58:26 -0800640 UPB_INLINE void upb_value_set ## name(upb_value *val, type_t cval) { \
Josh Habermane8ed0212015-06-08 17:56:03 -0700641 val->val = (converter)cval; \
Chris Fallin91473dc2014-12-12 15:58:26 -0800642 SET_TYPE(val->ctype, proto_type); \
Chris Fallin91473dc2014-12-12 15:58:26 -0800643 } \
644 UPB_INLINE upb_value upb_value_ ## name(type_t val) { \
645 upb_value ret; \
646 upb_value_set ## name(&ret, val); \
647 return ret; \
648 } \
649 UPB_INLINE type_t upb_value_get ## name(upb_value val) { \
650 assert(val.ctype == proto_type); \
Josh Habermane8ed0212015-06-08 17:56:03 -0700651 return (type_t)(converter)val.val; \
Chris Fallin91473dc2014-12-12 15:58:26 -0800652 }
653
Josh Habermane8ed0212015-06-08 17:56:03 -0700654FUNCS(int32, int32, int32_t, int32_t, UPB_CTYPE_INT32)
655FUNCS(int64, int64, int64_t, int64_t, UPB_CTYPE_INT64)
656FUNCS(uint32, uint32, uint32_t, uint32_t, UPB_CTYPE_UINT32)
657FUNCS(uint64, uint64, uint64_t, uint64_t, UPB_CTYPE_UINT64)
658FUNCS(bool, _bool, bool, bool, UPB_CTYPE_BOOL)
659FUNCS(cstr, cstr, char*, uintptr_t, UPB_CTYPE_CSTR)
660FUNCS(ptr, ptr, void*, uintptr_t, UPB_CTYPE_PTR)
661FUNCS(constptr, constptr, const void*, uintptr_t, UPB_CTYPE_CONSTPTR)
662FUNCS(fptr, fptr, upb_func*, uintptr_t, UPB_CTYPE_FPTR)
Chris Fallin91473dc2014-12-12 15:58:26 -0800663
664#undef FUNCS
Josh Habermane8ed0212015-06-08 17:56:03 -0700665#undef SET_TYPE
666
667
668/* upb_tabkey *****************************************************************/
669
670/* Either:
671 * 1. an actual integer key, or
672 * 2. a pointer to a string prefixed by its uint32_t length, owned by us.
673 *
674 * ...depending on whether this is a string table or an int table. We would
675 * make this a union of those two types, but C89 doesn't support statically
676 * initializing a non-first union member. */
677typedef uintptr_t upb_tabkey;
678
679#define UPB_TABKEY_NUM(n) n
680#define UPB_TABKEY_NONE 0
681/* The preprocessor isn't quite powerful enough to turn the compile-time string
682 * length into a byte-wise string representation, so code generation needs to
683 * help it along.
684 *
685 * "len1" is the low byte and len4 is the high byte. */
686#ifdef UPB_BIG_ENDIAN
687#define UPB_TABKEY_STR(len1, len2, len3, len4, strval) \
688 (uintptr_t)(len4 len3 len2 len1 strval)
689#else
690#define UPB_TABKEY_STR(len1, len2, len3, len4, strval) \
691 (uintptr_t)(len1 len2 len3 len4 strval)
692#endif
693
694UPB_INLINE char *upb_tabstr(upb_tabkey key, uint32_t *len) {
695 char* mem = (char*)key;
696 if (len) memcpy(len, mem, sizeof(*len));
697 return mem + sizeof(*len);
698}
699
700
701/* upb_tabval *****************************************************************/
702
703#ifdef __cplusplus
704
705/* Status initialization not supported.
706 *
707 * This separate definition is necessary because in C++, UINTPTR_MAX isn't
708 * reliably available. */
709typedef struct {
710 uint64_t val;
711} upb_tabval;
712
713#else
714
715/* C -- supports static initialization, but to support static initialization of
716 * both integers and points for both 32 and 64 bit targets, it takes a little
717 * bit of doing. */
718
719#if UINTPTR_MAX == 0xffffffffffffffffULL
720#define UPB_PTR_IS_64BITS
721#elif UINTPTR_MAX != 0xffffffff
722#error Could not determine how many bits pointers are.
723#endif
724
725typedef union {
726 /* For static initialization.
727 *
728 * Unfortunately this ugliness is necessary -- it is the only way that we can,
729 * with -std=c89 -pedantic, statically initialize this to either a pointer or
730 * an integer on 32-bit platforms. */
731 struct {
732#ifdef UPB_PTR_IS_64BITS
733 uintptr_t val;
734#else
735 uintptr_t val1;
736 uintptr_t val2;
737#endif
738 } staticinit;
739
740 /* The normal accessor that we use for everything at runtime. */
741 uint64_t val;
742} upb_tabval;
743
744#ifdef UPB_PTR_IS_64BITS
745#define UPB_TABVALUE_INT_INIT(v) {{v}}
746#define UPB_TABVALUE_EMPTY_INIT {{-1}}
747#else
748
749/* 32-bit pointers */
750
751#ifdef UPB_BIG_ENDIAN
752#define UPB_TABVALUE_INT_INIT(v) {{0, v}}
753#define UPB_TABVALUE_EMPTY_INIT {{-1, -1}}
754#else
755#define UPB_TABVALUE_INT_INIT(v) {{v, 0}}
756#define UPB_TABVALUE_EMPTY_INIT {{-1, -1}}
757#endif
758
759#endif
760
761#define UPB_TABVALUE_PTR_INIT(v) UPB_TABVALUE_INT_INIT((uintptr_t)v)
762
763#undef UPB_PTR_IS_64BITS
764
765#endif /* __cplusplus */
Chris Fallin91473dc2014-12-12 15:58:26 -0800766
767
768/* upb_table ******************************************************************/
769
Chris Fallin91473dc2014-12-12 15:58:26 -0800770typedef struct _upb_tabent {
771 upb_tabkey key;
Josh Habermane8ed0212015-06-08 17:56:03 -0700772 upb_tabval val;
773
774 /* Internal chaining. This is const so we can create static initializers for
775 * tables. We cast away const sometimes, but *only* when the containing
776 * upb_table is known to be non-const. This requires a bit of care, but
777 * the subtlety is confined to table.c. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800778 const struct _upb_tabent *next;
779} upb_tabent;
780
781typedef struct {
Josh Habermane8ed0212015-06-08 17:56:03 -0700782 size_t count; /* Number of entries in the hash part. */
783 size_t mask; /* Mask to turn hash value -> bucket. */
784 upb_ctype_t ctype; /* Type of all values. */
785 uint8_t size_lg2; /* Size of the hashtable part is 2^size_lg2 entries. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800786
Josh Habermane8ed0212015-06-08 17:56:03 -0700787 /* Hash table entries.
788 * Making this const isn't entirely accurate; what we really want is for it to
789 * have the same const-ness as the table it's inside. But there's no way to
790 * declare that in C. So we have to make it const so that we can statically
791 * initialize const hash tables. Then we cast away const when we have to.
792 */
Chris Fallin91473dc2014-12-12 15:58:26 -0800793 const upb_tabent *entries;
794} upb_table;
795
796typedef struct {
797 upb_table t;
798} upb_strtable;
799
800#define UPB_STRTABLE_INIT(count, mask, ctype, size_lg2, entries) \
801 {{count, mask, ctype, size_lg2, entries}}
802
Chris Fallinfcd88892015-01-13 18:14:39 -0800803#define UPB_EMPTY_STRTABLE_INIT(ctype) \
804 UPB_STRTABLE_INIT(0, 0, ctype, 0, NULL)
805
Chris Fallin91473dc2014-12-12 15:58:26 -0800806typedef struct {
Josh Habermane8ed0212015-06-08 17:56:03 -0700807 upb_table t; /* For entries that don't fit in the array part. */
808 const upb_tabval *array; /* Array part of the table. See const note above. */
809 size_t array_size; /* Array part size. */
810 size_t array_count; /* Array part number of elements. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800811} upb_inttable;
812
813#define UPB_INTTABLE_INIT(count, mask, ctype, size_lg2, ent, a, asize, acount) \
814 {{count, mask, ctype, size_lg2, ent}, a, asize, acount}
815
816#define UPB_EMPTY_INTTABLE_INIT(ctype) \
817 UPB_INTTABLE_INIT(0, 0, ctype, 0, NULL, NULL, 0, 0)
818
Josh Habermane8ed0212015-06-08 17:56:03 -0700819#define UPB_ARRAY_EMPTYENT -1
Chris Fallin91473dc2014-12-12 15:58:26 -0800820
821UPB_INLINE size_t upb_table_size(const upb_table *t) {
822 if (t->size_lg2 == 0)
823 return 0;
824 else
825 return 1 << t->size_lg2;
826}
827
Josh Habermane8ed0212015-06-08 17:56:03 -0700828/* Internal-only functions, in .h file only out of necessity. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800829UPB_INLINE bool upb_tabent_isempty(const upb_tabent *e) {
Josh Habermane8ed0212015-06-08 17:56:03 -0700830 return e->key == 0;
Chris Fallin91473dc2014-12-12 15:58:26 -0800831}
832
Josh Habermane8ed0212015-06-08 17:56:03 -0700833/* Used by some of the unit tests for generic hashing functionality. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800834uint32_t MurmurHash2(const void * key, size_t len, uint32_t seed);
835
Josh Habermane8ed0212015-06-08 17:56:03 -0700836UPB_INLINE uintptr_t upb_intkey(uintptr_t key) {
837 return key;
Chris Fallin91473dc2014-12-12 15:58:26 -0800838}
839
840UPB_INLINE uint32_t upb_inthash(uintptr_t key) {
841 return (uint32_t)key;
842}
843
844static const upb_tabent *upb_getentry(const upb_table *t, uint32_t hash) {
845 return t->entries + (hash & t->mask);
846}
847
Josh Habermane8ed0212015-06-08 17:56:03 -0700848UPB_INLINE bool upb_arrhas(upb_tabval key) {
849 return key.val != (uint64_t)-1;
Chris Fallin91473dc2014-12-12 15:58:26 -0800850}
851
Josh Habermane8ed0212015-06-08 17:56:03 -0700852/* Initialize and uninitialize a table, respectively. If memory allocation
853 * failed, false is returned that the table is uninitialized. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800854bool upb_inttable_init(upb_inttable *table, upb_ctype_t ctype);
855bool upb_strtable_init(upb_strtable *table, upb_ctype_t ctype);
856void upb_inttable_uninit(upb_inttable *table);
857void upb_strtable_uninit(upb_strtable *table);
858
Josh Habermane8ed0212015-06-08 17:56:03 -0700859/* Returns the number of values in the table. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800860size_t upb_inttable_count(const upb_inttable *t);
861UPB_INLINE size_t upb_strtable_count(const upb_strtable *t) {
862 return t->t.count;
863}
864
Josh Habermane8ed0212015-06-08 17:56:03 -0700865/* Inserts the given key into the hashtable with the given value. The key must
866 * not already exist in the hash table. For string tables, the key must be
867 * NULL-terminated, and the table will make an internal copy of the key.
868 * Inttables must not insert a value of UINTPTR_MAX.
869 *
870 * If a table resize was required but memory allocation failed, false is
871 * returned and the table is unchanged. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800872bool upb_inttable_insert(upb_inttable *t, uintptr_t key, upb_value val);
Chris Fallinfd1a3ff2015-01-06 15:44:09 -0800873bool upb_strtable_insert2(upb_strtable *t, const char *key, size_t len,
874 upb_value val);
875
Josh Habermane8ed0212015-06-08 17:56:03 -0700876/* For NULL-terminated strings. */
Chris Fallinfd1a3ff2015-01-06 15:44:09 -0800877UPB_INLINE bool upb_strtable_insert(upb_strtable *t, const char *key,
878 upb_value val) {
879 return upb_strtable_insert2(t, key, strlen(key), val);
880}
Chris Fallin91473dc2014-12-12 15:58:26 -0800881
Josh Habermane8ed0212015-06-08 17:56:03 -0700882/* Looks up key in this table, returning "true" if the key was found.
883 * If v is non-NULL, copies the value for this key into *v. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800884bool upb_inttable_lookup(const upb_inttable *t, uintptr_t key, upb_value *v);
885bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len,
886 upb_value *v);
887
Josh Habermane8ed0212015-06-08 17:56:03 -0700888/* For NULL-terminated strings. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800889UPB_INLINE bool upb_strtable_lookup(const upb_strtable *t, const char *key,
890 upb_value *v) {
891 return upb_strtable_lookup2(t, key, strlen(key), v);
892}
893
Josh Habermane8ed0212015-06-08 17:56:03 -0700894/* Removes an item from the table. Returns true if the remove was successful,
895 * and stores the removed item in *val if non-NULL. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800896bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val);
Chris Fallinfd1a3ff2015-01-06 15:44:09 -0800897bool upb_strtable_remove2(upb_strtable *t, const char *key, size_t len,
898 upb_value *val);
899
Josh Habermane8ed0212015-06-08 17:56:03 -0700900/* For NULL-terminated strings. */
Chris Fallinfd1a3ff2015-01-06 15:44:09 -0800901UPB_INLINE bool upb_strtable_remove(upb_strtable *t, const char *key,
902 upb_value *v) {
903 return upb_strtable_remove2(t, key, strlen(key), v);
904}
Chris Fallin91473dc2014-12-12 15:58:26 -0800905
Josh Habermane8ed0212015-06-08 17:56:03 -0700906/* Updates an existing entry in an inttable. If the entry does not exist,
907 * returns false and does nothing. Unlike insert/remove, this does not
908 * invalidate iterators. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800909bool upb_inttable_replace(upb_inttable *t, uintptr_t key, upb_value val);
910
Josh Habermane8ed0212015-06-08 17:56:03 -0700911/* Handy routines for treating an inttable like a stack. May not be mixed with
912 * other insert/remove calls. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800913bool upb_inttable_push(upb_inttable *t, upb_value val);
914upb_value upb_inttable_pop(upb_inttable *t);
915
Josh Habermane8ed0212015-06-08 17:56:03 -0700916/* Convenience routines for inttables with pointer keys. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800917bool upb_inttable_insertptr(upb_inttable *t, const void *key, upb_value val);
918bool upb_inttable_removeptr(upb_inttable *t, const void *key, upb_value *val);
919bool upb_inttable_lookupptr(
920 const upb_inttable *t, const void *key, upb_value *val);
921
Josh Habermane8ed0212015-06-08 17:56:03 -0700922/* Optimizes the table for the current set of entries, for both memory use and
923 * lookup time. Client should call this after all entries have been inserted;
924 * inserting more entries is legal, but will likely require a table resize. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800925void upb_inttable_compact(upb_inttable *t);
926
Josh Habermane8ed0212015-06-08 17:56:03 -0700927/* A special-case inlinable version of the lookup routine for 32-bit
928 * integers. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800929UPB_INLINE bool upb_inttable_lookup32(const upb_inttable *t, uint32_t key,
930 upb_value *v) {
Josh Habermane8ed0212015-06-08 17:56:03 -0700931 *v = upb_value_int32(0); /* Silence compiler warnings. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800932 if (key < t->array_size) {
Josh Habermane8ed0212015-06-08 17:56:03 -0700933 upb_tabval arrval = t->array[key];
Chris Fallin91473dc2014-12-12 15:58:26 -0800934 if (upb_arrhas(arrval)) {
Josh Habermane8ed0212015-06-08 17:56:03 -0700935 _upb_value_setval(v, arrval.val, t->t.ctype);
Chris Fallin91473dc2014-12-12 15:58:26 -0800936 return true;
937 } else {
938 return false;
939 }
940 } else {
941 const upb_tabent *e;
942 if (t->t.entries == NULL) return false;
943 for (e = upb_getentry(&t->t, upb_inthash(key)); true; e = e->next) {
Josh Habermane8ed0212015-06-08 17:56:03 -0700944 if ((uint32_t)e->key == key) {
945 _upb_value_setval(v, e->val.val, t->t.ctype);
Chris Fallin91473dc2014-12-12 15:58:26 -0800946 return true;
947 }
948 if (e->next == NULL) return false;
949 }
950 }
951}
952
Josh Habermane8ed0212015-06-08 17:56:03 -0700953/* Exposed for testing only. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800954bool upb_strtable_resize(upb_strtable *t, size_t size_lg2);
955
956/* Iterators ******************************************************************/
957
Josh Habermane8ed0212015-06-08 17:56:03 -0700958/* Iterators for int and string tables. We are subject to some kind of unusual
959 * design constraints:
960 *
961 * For high-level languages:
962 * - we must be able to guarantee that we don't crash or corrupt memory even if
963 * the program accesses an invalidated iterator.
964 *
965 * For C++11 range-based for:
966 * - iterators must be copyable
967 * - iterators must be comparable
968 * - it must be possible to construct an "end" value.
969 *
970 * Iteration order is undefined.
971 *
972 * Modifying the table invalidates iterators. upb_{str,int}table_done() is
973 * guaranteed to work even on an invalidated iterator, as long as the table it
974 * is iterating over has not been freed. Calling next() or accessing data from
975 * an invalidated iterator yields unspecified elements from the table, but it is
976 * guaranteed not to crash and to return real table elements (except when done()
977 * is true). */
Chris Fallin91473dc2014-12-12 15:58:26 -0800978
979
980/* upb_strtable_iter **********************************************************/
981
Josh Habermane8ed0212015-06-08 17:56:03 -0700982/* upb_strtable_iter i;
983 * upb_strtable_begin(&i, t);
984 * for(; !upb_strtable_done(&i); upb_strtable_next(&i)) {
985 * const char *key = upb_strtable_iter_key(&i);
986 * const upb_value val = upb_strtable_iter_value(&i);
987 * // ...
988 * }
989 */
Chris Fallin91473dc2014-12-12 15:58:26 -0800990
991typedef struct {
992 const upb_strtable *t;
993 size_t index;
994} upb_strtable_iter;
995
996void upb_strtable_begin(upb_strtable_iter *i, const upb_strtable *t);
997void upb_strtable_next(upb_strtable_iter *i);
998bool upb_strtable_done(const upb_strtable_iter *i);
999const char *upb_strtable_iter_key(upb_strtable_iter *i);
Chris Fallinfd1a3ff2015-01-06 15:44:09 -08001000size_t upb_strtable_iter_keylength(upb_strtable_iter *i);
Chris Fallin91473dc2014-12-12 15:58:26 -08001001upb_value upb_strtable_iter_value(const upb_strtable_iter *i);
1002void upb_strtable_iter_setdone(upb_strtable_iter *i);
1003bool upb_strtable_iter_isequal(const upb_strtable_iter *i1,
1004 const upb_strtable_iter *i2);
1005
1006
1007/* upb_inttable_iter **********************************************************/
1008
Josh Habermane8ed0212015-06-08 17:56:03 -07001009/* upb_inttable_iter i;
1010 * upb_inttable_begin(&i, t);
1011 * for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
1012 * uintptr_t key = upb_inttable_iter_key(&i);
1013 * upb_value val = upb_inttable_iter_value(&i);
1014 * // ...
1015 * }
1016 */
Chris Fallin91473dc2014-12-12 15:58:26 -08001017
1018typedef struct {
1019 const upb_inttable *t;
1020 size_t index;
1021 bool array_part;
1022} upb_inttable_iter;
1023
1024void upb_inttable_begin(upb_inttable_iter *i, const upb_inttable *t);
1025void upb_inttable_next(upb_inttable_iter *i);
1026bool upb_inttable_done(const upb_inttable_iter *i);
1027uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i);
1028upb_value upb_inttable_iter_value(const upb_inttable_iter *i);
1029void upb_inttable_iter_setdone(upb_inttable_iter *i);
1030bool upb_inttable_iter_isequal(const upb_inttable_iter *i1,
1031 const upb_inttable_iter *i2);
1032
1033
1034#ifdef __cplusplus
1035} /* extern "C" */
1036#endif
1037
1038#endif /* UPB_TABLE_H_ */
1039
Josh Habermane8ed0212015-06-08 17:56:03 -07001040/* Reference tracking will check ref()/unref() operations to make sure the
1041 * ref ownership is correct. Where possible it will also make tools like
1042 * Valgrind attribute ref leaks to the code that took the leaked ref, not
1043 * the code that originally created the object.
1044 *
1045 * Enabling this requires the application to define upb_lock()/upb_unlock()
1046 * functions that acquire/release a global mutex (or #define UPB_THREAD_UNSAFE).
1047 * For this reason we don't enable it by default, even in debug builds.
1048 */
1049
1050/* #define UPB_DEBUG_REFS */
Chris Fallin91473dc2014-12-12 15:58:26 -08001051
1052#ifdef __cplusplus
1053namespace upb { class RefCounted; }
1054#endif
1055
Josh Habermane8ed0212015-06-08 17:56:03 -07001056UPB_DECLARE_TYPE(upb::RefCounted, upb_refcounted)
Chris Fallin91473dc2014-12-12 15:58:26 -08001057
1058struct upb_refcounted_vtbl;
1059
Josh Habermane8ed0212015-06-08 17:56:03 -07001060#ifdef __cplusplus
1061
1062class upb::RefCounted {
Chris Fallin91473dc2014-12-12 15:58:26 -08001063 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07001064 /* Returns true if the given object is frozen. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001065 bool IsFrozen() const;
1066
Josh Habermane8ed0212015-06-08 17:56:03 -07001067 /* Increases the ref count, the new ref is owned by "owner" which must not
1068 * already own a ref (and should not itself be a refcounted object if the ref
1069 * could possibly be circular; see below).
1070 * Thread-safe iff "this" is frozen. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001071 void Ref(const void *owner) const;
1072
Josh Habermane8ed0212015-06-08 17:56:03 -07001073 /* Release a ref that was acquired from upb_refcounted_ref() and collects any
1074 * objects it can. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001075 void Unref(const void *owner) const;
1076
Josh Habermane8ed0212015-06-08 17:56:03 -07001077 /* Moves an existing ref from "from" to "to", without changing the overall
1078 * ref count. DonateRef(foo, NULL, owner) is the same as Ref(foo, owner),
1079 * but "to" may not be NULL. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001080 void DonateRef(const void *from, const void *to) const;
1081
Josh Habermane8ed0212015-06-08 17:56:03 -07001082 /* Verifies that a ref to the given object is currently held by the given
1083 * owner. Only effective in UPB_DEBUG_REFS builds. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001084 void CheckRef(const void *owner) const;
1085
1086 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07001087 UPB_DISALLOW_POD_OPS(RefCounted, upb::RefCounted)
1088#else
1089struct upb_refcounted {
1090#endif
1091 /* TODO(haberman): move the actual structure definition to structdefs.int.h.
1092 * The only reason they are here is because inline functions need to see the
1093 * definition of upb_handlers, which needs to see this definition. But we
1094 * can change the upb_handlers inline functions to deal in raw offsets
1095 * instead.
1096 */
1097
1098 /* A single reference count shared by all objects in the group. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001099 uint32_t *group;
1100
Josh Habermane8ed0212015-06-08 17:56:03 -07001101 /* A singly-linked list of all objects in the group. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001102 upb_refcounted *next;
1103
Josh Habermane8ed0212015-06-08 17:56:03 -07001104 /* Table of function pointers for this type. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001105 const struct upb_refcounted_vtbl *vtbl;
1106
Josh Habermane8ed0212015-06-08 17:56:03 -07001107 /* Maintained only when mutable, this tracks the number of refs (but not
1108 * ref2's) to this object. *group should be the sum of all individual_count
1109 * in the group. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001110 uint32_t individual_count;
1111
1112 bool is_frozen;
1113
1114#ifdef UPB_DEBUG_REFS
Josh Habermane8ed0212015-06-08 17:56:03 -07001115 upb_inttable *refs; /* Maps owner -> trackedref for incoming refs. */
1116 upb_inttable *ref2s; /* Set of targets for outgoing ref2s. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001117#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08001118};
1119
Chris Fallin91473dc2014-12-12 15:58:26 -08001120#ifdef UPB_DEBUG_REFS
1121#define UPB_REFCOUNT_INIT(refs, ref2s) \
1122 {&static_refcount, NULL, NULL, 0, true, refs, ref2s}
1123#else
1124#define UPB_REFCOUNT_INIT(refs, ref2s) {&static_refcount, NULL, NULL, 0, true}
1125#endif
1126
Josh Habermane8ed0212015-06-08 17:56:03 -07001127UPB_BEGIN_EXTERN_C
1128
1129/* It is better to use tracked refs when possible, for the extra debugging
1130 * capability. But if this is not possible (because you don't have easy access
1131 * to a stable pointer value that is associated with the ref), you can pass
1132 * UPB_UNTRACKED_REF instead. */
1133extern const void *UPB_UNTRACKED_REF;
1134
1135/* Native C API. */
1136bool upb_refcounted_isfrozen(const upb_refcounted *r);
1137void upb_refcounted_ref(const upb_refcounted *r, const void *owner);
1138void upb_refcounted_unref(const upb_refcounted *r, const void *owner);
1139void upb_refcounted_donateref(
1140 const upb_refcounted *r, const void *from, const void *to);
1141void upb_refcounted_checkref(const upb_refcounted *r, const void *owner);
1142
1143#define UPB_REFCOUNTED_CMETHODS(type, upcastfunc) \
1144 UPB_INLINE bool type ## _isfrozen(const type *v) { \
1145 return upb_refcounted_isfrozen(upcastfunc(v)); \
1146 } \
1147 UPB_INLINE void type ## _ref(const type *v, const void *owner) { \
1148 upb_refcounted_ref(upcastfunc(v), owner); \
1149 } \
1150 UPB_INLINE void type ## _unref(const type *v, const void *owner) { \
1151 upb_refcounted_unref(upcastfunc(v), owner); \
1152 } \
1153 UPB_INLINE void type ## _donateref(const type *v, const void *from, const void *to) { \
1154 upb_refcounted_donateref(upcastfunc(v), from, to); \
1155 } \
1156 UPB_INLINE void type ## _checkref(const type *v, const void *owner) { \
1157 upb_refcounted_checkref(upcastfunc(v), owner); \
1158 }
1159
1160#define UPB_REFCOUNTED_CPPMETHODS \
1161 bool IsFrozen() const { \
1162 return upb::upcast_to<const upb::RefCounted>(this)->IsFrozen(); \
1163 } \
1164 void Ref(const void *owner) const { \
1165 return upb::upcast_to<const upb::RefCounted>(this)->Ref(owner); \
1166 } \
1167 void Unref(const void *owner) const { \
1168 return upb::upcast_to<const upb::RefCounted>(this)->Unref(owner); \
1169 } \
1170 void DonateRef(const void *from, const void *to) const { \
1171 return upb::upcast_to<const upb::RefCounted>(this)->DonateRef(from, to); \
1172 } \
1173 void CheckRef(const void *owner) const { \
1174 return upb::upcast_to<const upb::RefCounted>(this)->CheckRef(owner); \
1175 }
1176
1177/* Internal-to-upb Interface **************************************************/
1178
1179typedef void upb_refcounted_visit(const upb_refcounted *r,
1180 const upb_refcounted *subobj,
1181 void *closure);
1182
1183struct upb_refcounted_vtbl {
1184 /* Must visit all subobjects that are currently ref'd via upb_refcounted_ref2.
1185 * Must be longjmp()-safe. */
1186 void (*visit)(const upb_refcounted *r, upb_refcounted_visit *visit, void *c);
1187
1188 /* Must free the object and release all references to other objects. */
1189 void (*free)(upb_refcounted *r);
1190};
1191
1192/* Initializes the refcounted with a single ref for the given owner. Returns
1193 * false if memory could not be allocated. */
1194bool upb_refcounted_init(upb_refcounted *r,
1195 const struct upb_refcounted_vtbl *vtbl,
1196 const void *owner);
1197
1198/* Adds a ref from one refcounted object to another ("from" must not already
1199 * own a ref). These refs may be circular; cycles will be collected correctly
1200 * (if conservatively). These refs do not need to be freed in from's free()
1201 * function. */
1202void upb_refcounted_ref2(const upb_refcounted *r, upb_refcounted *from);
1203
1204/* Removes a ref that was acquired from upb_refcounted_ref2(), and collects any
1205 * object it can. This is only necessary when "from" no longer points to "r",
1206 * and not from from's "free" function. */
1207void upb_refcounted_unref2(const upb_refcounted *r, upb_refcounted *from);
1208
1209#define upb_ref2(r, from) \
1210 upb_refcounted_ref2((const upb_refcounted*)r, (upb_refcounted*)from)
1211#define upb_unref2(r, from) \
1212 upb_refcounted_unref2((const upb_refcounted*)r, (upb_refcounted*)from)
1213
1214/* Freezes all mutable object reachable by ref2() refs from the given roots.
1215 * This will split refcounting groups into precise SCC groups, so that
1216 * refcounting of frozen objects can be more aggressive. If memory allocation
1217 * fails, or if more than 2**31 mutable objects are reachable from "roots", or
1218 * if the maximum depth of the graph exceeds "maxdepth", false is returned and
1219 * the objects are unchanged.
1220 *
1221 * After this operation succeeds, the objects are frozen/const, and may not be
1222 * used through non-const pointers. In particular, they may not be passed as
1223 * the second parameter of upb_refcounted_{ref,unref}2(). On the upside, all
1224 * operations on frozen refcounteds are threadsafe, and objects will be freed
1225 * at the precise moment that they become unreachable.
1226 *
1227 * Caller must own refs on each object in the "roots" list. */
1228bool upb_refcounted_freeze(upb_refcounted *const*roots, int n, upb_status *s,
1229 int maxdepth);
1230
1231/* Shared by all compiled-in refcounted objects. */
1232extern uint32_t static_refcount;
1233
1234UPB_END_EXTERN_C
1235
Chris Fallin91473dc2014-12-12 15:58:26 -08001236#ifdef __cplusplus
Josh Habermane8ed0212015-06-08 17:56:03 -07001237/* C++ Wrappers. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001238namespace upb {
1239inline bool RefCounted::IsFrozen() const {
1240 return upb_refcounted_isfrozen(this);
1241}
1242inline void RefCounted::Ref(const void *owner) const {
1243 upb_refcounted_ref(this, owner);
1244}
1245inline void RefCounted::Unref(const void *owner) const {
1246 upb_refcounted_unref(this, owner);
1247}
1248inline void RefCounted::DonateRef(const void *from, const void *to) const {
1249 upb_refcounted_donateref(this, from, to);
1250}
1251inline void RefCounted::CheckRef(const void *owner) const {
1252 upb_refcounted_checkref(this, owner);
1253}
Josh Habermane8ed0212015-06-08 17:56:03 -07001254} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08001255#endif
1256
Josh Habermane8ed0212015-06-08 17:56:03 -07001257#endif /* UPB_REFCOUNT_H_ */
Chris Fallin91473dc2014-12-12 15:58:26 -08001258
1259#ifdef __cplusplus
1260#include <cstring>
1261#include <string>
1262#include <vector>
1263
1264namespace upb {
1265class Def;
1266class EnumDef;
1267class FieldDef;
1268class MessageDef;
Chris Fallinfcd88892015-01-13 18:14:39 -08001269class OneofDef;
Chris Fallin91473dc2014-12-12 15:58:26 -08001270}
1271#endif
1272
Josh Habermane8ed0212015-06-08 17:56:03 -07001273UPB_DECLARE_DERIVED_TYPE(upb::Def, upb::RefCounted, upb_def, upb_refcounted)
Chris Fallin91473dc2014-12-12 15:58:26 -08001274
Josh Habermane8ed0212015-06-08 17:56:03 -07001275/* The maximum message depth that the type graph can have. This is a resource
1276 * limit for the C stack since we sometimes need to recursively traverse the
1277 * graph. Cycles are ok; the traversal will stop when it detects a cycle, but
1278 * we must hit the cycle before the maximum depth is reached.
1279 *
1280 * If having a single static limit is too inflexible, we can add another variant
1281 * of Def::Freeze that allows specifying this as a parameter. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001282#define UPB_MAX_MESSAGE_DEPTH 64
1283
1284
1285/* upb::Def: base class for defs *********************************************/
1286
Josh Habermane8ed0212015-06-08 17:56:03 -07001287/* All the different kind of defs we support. These correspond 1:1 with
1288 * declarations in a .proto file. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001289typedef enum {
1290 UPB_DEF_MSG,
1291 UPB_DEF_FIELD,
1292 UPB_DEF_ENUM,
Chris Fallinfcd88892015-01-13 18:14:39 -08001293 UPB_DEF_ONEOF,
Josh Habermane8ed0212015-06-08 17:56:03 -07001294 UPB_DEF_SERVICE, /* Not yet implemented. */
1295 UPB_DEF_ANY = -1 /* Wildcard for upb_symtab_get*() */
Chris Fallin91473dc2014-12-12 15:58:26 -08001296} upb_deftype_t;
1297
Josh Habermane8ed0212015-06-08 17:56:03 -07001298#ifdef __cplusplus
1299
1300/* The base class of all defs. Its base is upb::RefCounted (use upb::upcast()
1301 * to convert). */
1302class upb::Def {
Chris Fallin91473dc2014-12-12 15:58:26 -08001303 public:
1304 typedef upb_deftype_t Type;
1305
1306 Def* Dup(const void *owner) const;
1307
Josh Habermane8ed0212015-06-08 17:56:03 -07001308 /* upb::RefCounted methods like Ref()/Unref(). */
1309 UPB_REFCOUNTED_CPPMETHODS
Chris Fallin91473dc2014-12-12 15:58:26 -08001310
1311 Type def_type() const;
1312
Josh Habermane8ed0212015-06-08 17:56:03 -07001313 /* "fullname" is the def's fully-qualified name (eg. foo.bar.Message). */
Chris Fallin91473dc2014-12-12 15:58:26 -08001314 const char *full_name() const;
1315
Josh Habermane8ed0212015-06-08 17:56:03 -07001316 /* The def must be mutable. Caller retains ownership of fullname. Defs are
1317 * not required to have a name; if a def has no name when it is frozen, it
1318 * will remain an anonymous def. On failure, returns false and details in "s"
1319 * if non-NULL. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001320 bool set_full_name(const char* fullname, upb::Status* s);
1321 bool set_full_name(const std::string &fullname, upb::Status* s);
1322
Josh Habermane8ed0212015-06-08 17:56:03 -07001323 /* Freezes the given defs; this validates all constraints and marks the defs
1324 * as frozen (read-only). "defs" may not contain any fielddefs, but fields
1325 * of any msgdefs will be frozen.
1326 *
1327 * Symbolic references to sub-types and enum defaults must have already been
1328 * resolved. Any mutable defs reachable from any of "defs" must also be in
1329 * the list; more formally, "defs" must be a transitive closure of mutable
1330 * defs.
1331 *
1332 * After this operation succeeds, the finalized defs must only be accessed
1333 * through a const pointer! */
Chris Fallin91473dc2014-12-12 15:58:26 -08001334 static bool Freeze(Def* const* defs, int n, Status* status);
1335 static bool Freeze(const std::vector<Def*>& defs, Status* status);
1336
1337 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07001338 UPB_DISALLOW_POD_OPS(Def, upb::Def)
1339};
Chris Fallin91473dc2014-12-12 15:58:26 -08001340
Josh Habermane8ed0212015-06-08 17:56:03 -07001341#endif /* __cplusplus */
Chris Fallin91473dc2014-12-12 15:58:26 -08001342
Josh Habermane8ed0212015-06-08 17:56:03 -07001343UPB_BEGIN_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08001344
Josh Habermane8ed0212015-06-08 17:56:03 -07001345/* Native C API. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001346upb_def *upb_def_dup(const upb_def *def, const void *owner);
1347
Josh Habermane8ed0212015-06-08 17:56:03 -07001348/* Include upb_refcounted methods like upb_def_ref()/upb_def_unref(). */
1349UPB_REFCOUNTED_CMETHODS(upb_def, upb_def_upcast)
Chris Fallin91473dc2014-12-12 15:58:26 -08001350
1351upb_deftype_t upb_def_type(const upb_def *d);
1352const char *upb_def_fullname(const upb_def *d);
1353bool upb_def_setfullname(upb_def *def, const char *fullname, upb_status *s);
1354bool upb_def_freeze(upb_def *const *defs, int n, upb_status *s);
1355
Josh Habermane8ed0212015-06-08 17:56:03 -07001356UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08001357
1358
1359/* upb::Def casts *************************************************************/
1360
1361#ifdef __cplusplus
1362#define UPB_CPP_CASTS(cname, cpptype) \
1363 namespace upb { \
1364 template <> \
1365 inline cpptype *down_cast<cpptype *, Def>(Def * def) { \
1366 return upb_downcast_##cname##_mutable(def); \
1367 } \
1368 template <> \
1369 inline cpptype *dyn_cast<cpptype *, Def>(Def * def) { \
1370 return upb_dyncast_##cname##_mutable(def); \
1371 } \
1372 template <> \
1373 inline const cpptype *down_cast<const cpptype *, const Def>( \
1374 const Def *def) { \
1375 return upb_downcast_##cname(def); \
1376 } \
1377 template <> \
1378 inline const cpptype *dyn_cast<const cpptype *, const Def>(const Def *def) { \
1379 return upb_dyncast_##cname(def); \
1380 } \
1381 template <> \
1382 inline const cpptype *down_cast<const cpptype *, Def>(Def * def) { \
1383 return upb_downcast_##cname(def); \
1384 } \
1385 template <> \
1386 inline const cpptype *dyn_cast<const cpptype *, Def>(Def * def) { \
1387 return upb_dyncast_##cname(def); \
1388 } \
Josh Habermane8ed0212015-06-08 17:56:03 -07001389 } /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08001390#else
1391#define UPB_CPP_CASTS(cname, cpptype)
Josh Habermane8ed0212015-06-08 17:56:03 -07001392#endif /* __cplusplus */
Chris Fallin91473dc2014-12-12 15:58:26 -08001393
Josh Habermane8ed0212015-06-08 17:56:03 -07001394/* Dynamic casts, for determining if a def is of a particular type at runtime.
1395 * Downcasts, for when some wants to assert that a def is of a particular type.
1396 * These are only checked if we are building debug. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001397#define UPB_DEF_CASTS(lower, upper, cpptype) \
1398 UPB_INLINE const upb_##lower *upb_dyncast_##lower(const upb_def *def) { \
1399 if (upb_def_type(def) != UPB_DEF_##upper) return NULL; \
1400 return (upb_##lower *)def; \
1401 } \
1402 UPB_INLINE const upb_##lower *upb_downcast_##lower(const upb_def *def) { \
1403 assert(upb_def_type(def) == UPB_DEF_##upper); \
1404 return (const upb_##lower *)def; \
1405 } \
1406 UPB_INLINE upb_##lower *upb_dyncast_##lower##_mutable(upb_def *def) { \
1407 return (upb_##lower *)upb_dyncast_##lower(def); \
1408 } \
1409 UPB_INLINE upb_##lower *upb_downcast_##lower##_mutable(upb_def *def) { \
1410 return (upb_##lower *)upb_downcast_##lower(def); \
1411 } \
1412 UPB_CPP_CASTS(lower, cpptype)
1413
1414#define UPB_DEFINE_DEF(cppname, lower, upper, cppmethods, members) \
Josh Habermane8ed0212015-06-08 17:56:03 -07001415 UPB_DEFINE_CLASS2(cppname, upb::Def, upb::RefCounted, cppmethods, \
Chris Fallin91473dc2014-12-12 15:58:26 -08001416 members) \
1417 UPB_DEF_CASTS(lower, upper, cppname)
1418
Josh Habermane8ed0212015-06-08 17:56:03 -07001419#define UPB_DECLARE_DEF_TYPE(cppname, lower, upper) \
1420 UPB_DECLARE_DERIVED_TYPE2(cppname, upb::Def, upb::RefCounted, \
1421 upb_ ## lower, upb_def, upb_refcounted) \
1422 UPB_DEF_CASTS(lower, upper, cppname)
1423
1424UPB_DECLARE_DEF_TYPE(upb::FieldDef, fielddef, FIELD)
1425UPB_DECLARE_DEF_TYPE(upb::MessageDef, msgdef, MSG)
1426UPB_DECLARE_DEF_TYPE(upb::EnumDef, enumdef, ENUM)
1427UPB_DECLARE_DEF_TYPE(upb::OneofDef, oneofdef, ONEOF)
1428
1429#undef UPB_DECLARE_DEF_TYPE
1430#undef UPB_DEF_CASTS
1431#undef UPB_CPP_CASTS
1432
Chris Fallin91473dc2014-12-12 15:58:26 -08001433
1434/* upb::FieldDef **************************************************************/
1435
Josh Habermane8ed0212015-06-08 17:56:03 -07001436/* The types a field can have. Note that this list is not identical to the
1437 * types defined in descriptor.proto, which gives INT32 and SINT32 separate
1438 * types (we distinguish the two with the "integer encoding" enum below). */
Chris Fallin91473dc2014-12-12 15:58:26 -08001439typedef enum {
1440 UPB_TYPE_FLOAT = 1,
1441 UPB_TYPE_DOUBLE = 2,
1442 UPB_TYPE_BOOL = 3,
1443 UPB_TYPE_STRING = 4,
1444 UPB_TYPE_BYTES = 5,
1445 UPB_TYPE_MESSAGE = 6,
Josh Habermane8ed0212015-06-08 17:56:03 -07001446 UPB_TYPE_ENUM = 7, /* Enum values are int32. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001447 UPB_TYPE_INT32 = 8,
1448 UPB_TYPE_UINT32 = 9,
1449 UPB_TYPE_INT64 = 10,
Josh Habermane8ed0212015-06-08 17:56:03 -07001450 UPB_TYPE_UINT64 = 11
Chris Fallin91473dc2014-12-12 15:58:26 -08001451} upb_fieldtype_t;
1452
Josh Habermane8ed0212015-06-08 17:56:03 -07001453/* The repeated-ness of each field; this matches descriptor.proto. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001454typedef enum {
1455 UPB_LABEL_OPTIONAL = 1,
1456 UPB_LABEL_REQUIRED = 2,
Josh Habermane8ed0212015-06-08 17:56:03 -07001457 UPB_LABEL_REPEATED = 3
Chris Fallin91473dc2014-12-12 15:58:26 -08001458} upb_label_t;
1459
Josh Habermane8ed0212015-06-08 17:56:03 -07001460/* How integers should be encoded in serializations that offer multiple
1461 * integer encoding methods. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001462typedef enum {
1463 UPB_INTFMT_VARIABLE = 1,
1464 UPB_INTFMT_FIXED = 2,
Josh Habermane8ed0212015-06-08 17:56:03 -07001465 UPB_INTFMT_ZIGZAG = 3 /* Only for signed types (INT32/INT64). */
Chris Fallin91473dc2014-12-12 15:58:26 -08001466} upb_intfmt_t;
1467
Josh Habermane8ed0212015-06-08 17:56:03 -07001468/* Descriptor types, as defined in descriptor.proto. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001469typedef enum {
1470 UPB_DESCRIPTOR_TYPE_DOUBLE = 1,
1471 UPB_DESCRIPTOR_TYPE_FLOAT = 2,
1472 UPB_DESCRIPTOR_TYPE_INT64 = 3,
1473 UPB_DESCRIPTOR_TYPE_UINT64 = 4,
1474 UPB_DESCRIPTOR_TYPE_INT32 = 5,
1475 UPB_DESCRIPTOR_TYPE_FIXED64 = 6,
1476 UPB_DESCRIPTOR_TYPE_FIXED32 = 7,
1477 UPB_DESCRIPTOR_TYPE_BOOL = 8,
1478 UPB_DESCRIPTOR_TYPE_STRING = 9,
1479 UPB_DESCRIPTOR_TYPE_GROUP = 10,
1480 UPB_DESCRIPTOR_TYPE_MESSAGE = 11,
1481 UPB_DESCRIPTOR_TYPE_BYTES = 12,
1482 UPB_DESCRIPTOR_TYPE_UINT32 = 13,
1483 UPB_DESCRIPTOR_TYPE_ENUM = 14,
1484 UPB_DESCRIPTOR_TYPE_SFIXED32 = 15,
1485 UPB_DESCRIPTOR_TYPE_SFIXED64 = 16,
1486 UPB_DESCRIPTOR_TYPE_SINT32 = 17,
Josh Habermane8ed0212015-06-08 17:56:03 -07001487 UPB_DESCRIPTOR_TYPE_SINT64 = 18
Chris Fallin91473dc2014-12-12 15:58:26 -08001488} upb_descriptortype_t;
1489
Josh Habermane8ed0212015-06-08 17:56:03 -07001490/* Maximum field number allowed for FieldDefs. This is an inherent limit of the
1491 * protobuf wire format. */
1492#define UPB_MAX_FIELDNUMBER ((1 << 29) - 1)
Chris Fallin91473dc2014-12-12 15:58:26 -08001493
Josh Habermane8ed0212015-06-08 17:56:03 -07001494#ifdef __cplusplus
1495
1496/* A upb_fielddef describes a single field in a message. It is most often
1497 * found as a part of a upb_msgdef, but can also stand alone to represent
1498 * an extension.
1499 *
1500 * Its base class is upb::Def (use upb::upcast() to convert). */
1501class upb::FieldDef {
Chris Fallin91473dc2014-12-12 15:58:26 -08001502 public:
1503 typedef upb_fieldtype_t Type;
1504 typedef upb_label_t Label;
1505 typedef upb_intfmt_t IntegerFormat;
1506 typedef upb_descriptortype_t DescriptorType;
1507
Josh Habermane8ed0212015-06-08 17:56:03 -07001508 /* These return true if the given value is a valid member of the enumeration. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001509 static bool CheckType(int32_t val);
1510 static bool CheckLabel(int32_t val);
1511 static bool CheckDescriptorType(int32_t val);
1512 static bool CheckIntegerFormat(int32_t val);
1513
Josh Habermane8ed0212015-06-08 17:56:03 -07001514 /* These convert to the given enumeration; they require that the value is
1515 * valid. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001516 static Type ConvertType(int32_t val);
1517 static Label ConvertLabel(int32_t val);
1518 static DescriptorType ConvertDescriptorType(int32_t val);
1519 static IntegerFormat ConvertIntegerFormat(int32_t val);
1520
Josh Habermane8ed0212015-06-08 17:56:03 -07001521 /* Returns NULL if memory allocation failed. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001522 static reffed_ptr<FieldDef> New();
1523
Josh Habermane8ed0212015-06-08 17:56:03 -07001524 /* Duplicates the given field, returning NULL if memory allocation failed.
1525 * When a fielddef is duplicated, the subdef (if any) is made symbolic if it
1526 * wasn't already. If the subdef is set but has no name (which is possible
1527 * since msgdefs are not required to have a name) the new fielddef's subdef
1528 * will be unset. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001529 FieldDef* Dup(const void* owner) const;
1530
Josh Habermane8ed0212015-06-08 17:56:03 -07001531 /* upb::RefCounted methods like Ref()/Unref(). */
1532 UPB_REFCOUNTED_CPPMETHODS
Chris Fallin91473dc2014-12-12 15:58:26 -08001533
Josh Habermane8ed0212015-06-08 17:56:03 -07001534 /* Functionality from upb::Def. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001535 const char* full_name() const;
1536
Josh Habermane8ed0212015-06-08 17:56:03 -07001537 bool type_is_set() const; /* set_[descriptor_]type() has been called? */
1538 Type type() const; /* Requires that type_is_set() == true. */
1539 Label label() const; /* Defaults to UPB_LABEL_OPTIONAL. */
1540 const char* name() const; /* NULL if uninitialized. */
1541 uint32_t number() const; /* Returns 0 if uninitialized. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001542 bool is_extension() const;
1543
Josh Habermanf654d492016-02-18 11:07:51 -08001544 /* Copies the JSON name for this field into the given buffer. Returns the
1545 * actual size of the JSON name, including the NULL terminator. If the
1546 * return value is 0, the JSON name is unset. If the return value is
1547 * greater than len, the JSON name was truncated. The buffer is always
1548 * NULL-terminated if len > 0.
1549 *
1550 * The JSON name always defaults to a camelCased version of the regular
1551 * name. However if the regular name is unset, the JSON name will be unset
1552 * also.
Josh Haberman78da6662016-01-13 19:05:43 -08001553 */
Josh Habermanf654d492016-02-18 11:07:51 -08001554 size_t GetJsonName(char* buf, size_t len) const;
1555
1556 /* Convenience version of the above function which copies the JSON name
1557 * into the given string, returning false if the name is not set. */
1558 template <class T>
1559 bool GetJsonName(T* str) {
1560 str->resize(GetJsonName(NULL, 0));
1561 GetJsonName(&(*str)[0], str->size());
1562 return str->size() > 0;
1563 }
Josh Haberman78da6662016-01-13 19:05:43 -08001564
Josh Habermane8ed0212015-06-08 17:56:03 -07001565 /* For UPB_TYPE_MESSAGE fields only where is_tag_delimited() == false,
1566 * indicates whether this field should have lazy parsing handlers that yield
1567 * the unparsed string for the submessage.
1568 *
1569 * TODO(haberman): I think we want to move this into a FieldOptions container
1570 * when we add support for custom options (the FieldOptions struct will
1571 * contain both regular FieldOptions like "lazy" *and* custom options). */
Chris Fallin91473dc2014-12-12 15:58:26 -08001572 bool lazy() const;
1573
Josh Habermane8ed0212015-06-08 17:56:03 -07001574 /* For non-string, non-submessage fields, this indicates whether binary
1575 * protobufs are encoded in packed or non-packed format.
1576 *
1577 * TODO(haberman): see note above about putting options like this into a
1578 * FieldOptions container. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001579 bool packed() const;
1580
Josh Habermane8ed0212015-06-08 17:56:03 -07001581 /* An integer that can be used as an index into an array of fields for
1582 * whatever message this field belongs to. Guaranteed to be less than
1583 * f->containing_type()->field_count(). May only be accessed once the def has
1584 * been finalized. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001585 int index() const;
1586
Josh Habermane8ed0212015-06-08 17:56:03 -07001587 /* The MessageDef to which this field belongs.
1588 *
1589 * If this field has been added to a MessageDef, that message can be retrieved
1590 * directly (this is always the case for frozen FieldDefs).
1591 *
1592 * If the field has not yet been added to a MessageDef, you can set the name
1593 * of the containing type symbolically instead. This is mostly useful for
1594 * extensions, where the extension is declared separately from the message. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001595 const MessageDef* containing_type() const;
1596 const char* containing_type_name();
1597
Josh Habermane8ed0212015-06-08 17:56:03 -07001598 /* The OneofDef to which this field belongs, or NULL if this field is not part
1599 * of a oneof. */
Chris Fallinfcd88892015-01-13 18:14:39 -08001600 const OneofDef* containing_oneof() const;
1601
Josh Habermane8ed0212015-06-08 17:56:03 -07001602 /* The field's type according to the enum in descriptor.proto. This is not
1603 * the same as UPB_TYPE_*, because it distinguishes between (for example)
1604 * INT32 and SINT32, whereas our "type" enum does not. This return of
1605 * descriptor_type() is a function of type(), integer_format(), and
1606 * is_tag_delimited(). Likewise set_descriptor_type() sets all three
1607 * appropriately. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001608 DescriptorType descriptor_type() const;
1609
Josh Habermane8ed0212015-06-08 17:56:03 -07001610 /* Convenient field type tests. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001611 bool IsSubMessage() const;
1612 bool IsString() const;
1613 bool IsSequence() const;
1614 bool IsPrimitive() const;
Chris Fallina5075922015-02-02 15:07:34 -08001615 bool IsMap() const;
Chris Fallin91473dc2014-12-12 15:58:26 -08001616
Josh Haberman78da6662016-01-13 19:05:43 -08001617 /* Whether this field must be able to explicitly represent presence:
1618 *
1619 * * This is always false for repeated fields (an empty repeated field is
1620 * equivalent to a repeated field with zero entries).
1621 *
1622 * * This is always true for submessages.
1623 *
1624 * * For other fields, it depends on the message (see
1625 * MessageDef::SetPrimitivesHavePresence())
1626 */
1627 bool HasPresence() const;
1628
Josh Habermane8ed0212015-06-08 17:56:03 -07001629 /* How integers are encoded. Only meaningful for integer types.
1630 * Defaults to UPB_INTFMT_VARIABLE, and is reset when "type" changes. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001631 IntegerFormat integer_format() const;
1632
Josh Habermane8ed0212015-06-08 17:56:03 -07001633 /* Whether a submessage field is tag-delimited or not (if false, then
1634 * length-delimited). May only be set when type() == UPB_TYPE_MESSAGE. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001635 bool is_tag_delimited() const;
1636
Josh Habermane8ed0212015-06-08 17:56:03 -07001637 /* Returns the non-string default value for this fielddef, which may either
1638 * be something the client set explicitly or the "default default" (0 for
1639 * numbers, empty for strings). The field's type indicates the type of the
1640 * returned value, except for enum fields that are still mutable.
1641 *
1642 * Requires that the given function matches the field's current type. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001643 int64_t default_int64() const;
1644 int32_t default_int32() const;
1645 uint64_t default_uint64() const;
1646 uint32_t default_uint32() const;
1647 bool default_bool() const;
1648 float default_float() const;
1649 double default_double() const;
1650
Josh Habermane8ed0212015-06-08 17:56:03 -07001651 /* The resulting string is always NULL-terminated. If non-NULL, the length
1652 * will be stored in *len. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001653 const char *default_string(size_t* len) const;
1654
Josh Habermane8ed0212015-06-08 17:56:03 -07001655 /* For frozen UPB_TYPE_ENUM fields, enum defaults can always be read as either
1656 * string or int32, and both of these methods will always return true.
1657 *
1658 * For mutable UPB_TYPE_ENUM fields, the story is a bit more complicated.
1659 * Enum defaults are unusual. They can be specified either as string or int32,
1660 * but to be valid the enum must have that value as a member. And if no
1661 * default is specified, the "default default" comes from the EnumDef.
1662 *
1663 * We allow reading the default as either an int32 or a string, but only if
1664 * we have a meaningful value to report. We have a meaningful value if it was
1665 * set explicitly, or if we could get the "default default" from the EnumDef.
1666 * Also if you explicitly set the name and we find the number in the EnumDef */
Chris Fallin91473dc2014-12-12 15:58:26 -08001667 bool EnumHasStringDefault() const;
1668 bool EnumHasInt32Default() const;
1669
Josh Habermane8ed0212015-06-08 17:56:03 -07001670 /* Submessage and enum fields must reference a "subdef", which is the
1671 * upb::MessageDef or upb::EnumDef that defines their type. Note that when
1672 * the FieldDef is mutable it may not have a subdef *yet*, but this function
1673 * still returns true to indicate that the field's type requires a subdef. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001674 bool HasSubDef() const;
1675
Josh Habermane8ed0212015-06-08 17:56:03 -07001676 /* Returns the enum or submessage def for this field, if any. The field's
1677 * type must match (ie. you may only call enum_subdef() for fields where
1678 * type() == UPB_TYPE_ENUM). Returns NULL if the subdef has not been set or
1679 * is currently set symbolically. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001680 const EnumDef* enum_subdef() const;
1681 const MessageDef* message_subdef() const;
1682
Josh Habermane8ed0212015-06-08 17:56:03 -07001683 /* Returns the generic subdef for this field. Requires that HasSubDef() (ie.
1684 * only works for UPB_TYPE_ENUM and UPB_TYPE_MESSAGE fields). */
Chris Fallin91473dc2014-12-12 15:58:26 -08001685 const Def* subdef() const;
1686
Josh Habermane8ed0212015-06-08 17:56:03 -07001687 /* Returns the symbolic name of the subdef. If the subdef is currently set
1688 * unresolved (ie. set symbolically) returns the symbolic name. If it has
1689 * been resolved to a specific subdef, returns the name from that subdef. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001690 const char* subdef_name() const;
1691
Josh Habermane8ed0212015-06-08 17:56:03 -07001692 /* Setters (non-const methods), only valid for mutable FieldDefs! ***********/
Chris Fallin91473dc2014-12-12 15:58:26 -08001693
1694 bool set_full_name(const char* fullname, upb::Status* s);
1695 bool set_full_name(const std::string& fullname, upb::Status* s);
1696
Josh Habermane8ed0212015-06-08 17:56:03 -07001697 /* This may only be called if containing_type() == NULL (ie. the field has not
1698 * been added to a message yet). */
Chris Fallin91473dc2014-12-12 15:58:26 -08001699 bool set_containing_type_name(const char *name, Status* status);
1700 bool set_containing_type_name(const std::string& name, Status* status);
1701
Josh Habermane8ed0212015-06-08 17:56:03 -07001702 /* Defaults to false. When we freeze, we ensure that this can only be true
1703 * for length-delimited message fields. Prior to freezing this can be true or
1704 * false with no restrictions. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001705 void set_lazy(bool lazy);
1706
Josh Habermane8ed0212015-06-08 17:56:03 -07001707 /* Defaults to true. Sets whether this field is encoded in packed format. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001708 void set_packed(bool packed);
1709
Josh Habermane8ed0212015-06-08 17:56:03 -07001710 /* "type" or "descriptor_type" MUST be set explicitly before the fielddef is
1711 * finalized. These setters require that the enum value is valid; if the
1712 * value did not come directly from an enum constant, the caller should
1713 * validate it first with the functions above (CheckFieldType(), etc). */
Chris Fallin91473dc2014-12-12 15:58:26 -08001714 void set_type(Type type);
1715 void set_label(Label label);
1716 void set_descriptor_type(DescriptorType type);
1717 void set_is_extension(bool is_extension);
1718
Josh Habermane8ed0212015-06-08 17:56:03 -07001719 /* "number" and "name" must be set before the FieldDef is added to a
1720 * MessageDef, and may not be set after that.
1721 *
1722 * "name" is the same as full_name()/set_full_name(), but since fielddefs
1723 * most often use simple, non-qualified names, we provide this accessor
1724 * also. Generally only extensions will want to think of this name as
1725 * fully-qualified. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001726 bool set_number(uint32_t number, upb::Status* s);
1727 bool set_name(const char* name, upb::Status* s);
1728 bool set_name(const std::string& name, upb::Status* s);
1729
Josh Haberman78da6662016-01-13 19:05:43 -08001730 /* Sets the JSON name to the given string. */
1731 /* TODO(haberman): implement. Right now only default json_name (camelCase)
1732 * is supported. */
1733 bool set_json_name(const char* json_name, upb::Status* s);
1734 bool set_json_name(const std::string& name, upb::Status* s);
1735
1736 /* Clears the JSON name. This will make it revert to its default, which is
1737 * a camelCased version of the regular field name. */
1738 void clear_json_name();
1739
Chris Fallin91473dc2014-12-12 15:58:26 -08001740 void set_integer_format(IntegerFormat format);
1741 bool set_tag_delimited(bool tag_delimited, upb::Status* s);
1742
Josh Habermane8ed0212015-06-08 17:56:03 -07001743 /* Sets default value for the field. The call must exactly match the type
1744 * of the field. Enum fields may use either setint32 or setstring to set
1745 * the default numerically or symbolically, respectively, but symbolic
1746 * defaults must be resolved before finalizing (see ResolveEnumDefault()).
1747 *
1748 * Changing the type of a field will reset its default. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001749 void set_default_int64(int64_t val);
1750 void set_default_int32(int32_t val);
1751 void set_default_uint64(uint64_t val);
1752 void set_default_uint32(uint32_t val);
1753 void set_default_bool(bool val);
1754 void set_default_float(float val);
1755 void set_default_double(double val);
1756 bool set_default_string(const void *str, size_t len, Status *s);
1757 bool set_default_string(const std::string &str, Status *s);
1758 void set_default_cstr(const char *str, Status *s);
1759
Josh Habermane8ed0212015-06-08 17:56:03 -07001760 /* Before a fielddef is frozen, its subdef may be set either directly (with a
1761 * upb::Def*) or symbolically. Symbolic refs must be resolved before the
1762 * containing msgdef can be frozen (see upb_resolve() above). upb always
1763 * guarantees that any def reachable from a live def will also be kept alive.
1764 *
1765 * Both methods require that upb_hassubdef(f) (so the type must be set prior
1766 * to calling these methods). Returns false if this is not the case, or if
1767 * the given subdef is not of the correct type. The subdef is reset if the
1768 * field's type is changed. The subdef can be set to NULL to clear it. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001769 bool set_subdef(const Def* subdef, Status* s);
1770 bool set_enum_subdef(const EnumDef* subdef, Status* s);
1771 bool set_message_subdef(const MessageDef* subdef, Status* s);
1772 bool set_subdef_name(const char* name, Status* s);
1773 bool set_subdef_name(const std::string &name, Status* s);
1774
1775 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07001776 UPB_DISALLOW_POD_OPS(FieldDef, upb::FieldDef)
1777};
Chris Fallin91473dc2014-12-12 15:58:26 -08001778
Josh Habermane8ed0212015-06-08 17:56:03 -07001779# endif /* defined(__cplusplus) */
Chris Fallin91473dc2014-12-12 15:58:26 -08001780
Josh Habermane8ed0212015-06-08 17:56:03 -07001781UPB_BEGIN_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08001782
Josh Habermane8ed0212015-06-08 17:56:03 -07001783/* Native C API. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001784upb_fielddef *upb_fielddef_new(const void *owner);
1785upb_fielddef *upb_fielddef_dup(const upb_fielddef *f, const void *owner);
1786
Josh Habermane8ed0212015-06-08 17:56:03 -07001787/* Include upb_refcounted methods like upb_fielddef_ref(). */
1788UPB_REFCOUNTED_CMETHODS(upb_fielddef, upb_fielddef_upcast2)
Chris Fallin91473dc2014-12-12 15:58:26 -08001789
Josh Habermane8ed0212015-06-08 17:56:03 -07001790/* Methods from upb_def. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001791const char *upb_fielddef_fullname(const upb_fielddef *f);
1792bool upb_fielddef_setfullname(upb_fielddef *f, const char *fullname,
1793 upb_status *s);
1794
1795bool upb_fielddef_typeisset(const upb_fielddef *f);
1796upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f);
1797upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f);
1798upb_label_t upb_fielddef_label(const upb_fielddef *f);
1799uint32_t upb_fielddef_number(const upb_fielddef *f);
1800const char *upb_fielddef_name(const upb_fielddef *f);
1801bool upb_fielddef_isextension(const upb_fielddef *f);
1802bool upb_fielddef_lazy(const upb_fielddef *f);
1803bool upb_fielddef_packed(const upb_fielddef *f);
Josh Habermanf654d492016-02-18 11:07:51 -08001804size_t upb_fielddef_getjsonname(const upb_fielddef *f, char *buf, size_t len);
Chris Fallin91473dc2014-12-12 15:58:26 -08001805const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f);
Chris Fallinfcd88892015-01-13 18:14:39 -08001806const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f);
Chris Fallin91473dc2014-12-12 15:58:26 -08001807upb_msgdef *upb_fielddef_containingtype_mutable(upb_fielddef *f);
1808const char *upb_fielddef_containingtypename(upb_fielddef *f);
1809upb_intfmt_t upb_fielddef_intfmt(const upb_fielddef *f);
1810uint32_t upb_fielddef_index(const upb_fielddef *f);
1811bool upb_fielddef_istagdelim(const upb_fielddef *f);
1812bool upb_fielddef_issubmsg(const upb_fielddef *f);
1813bool upb_fielddef_isstring(const upb_fielddef *f);
1814bool upb_fielddef_isseq(const upb_fielddef *f);
1815bool upb_fielddef_isprimitive(const upb_fielddef *f);
Chris Fallina5075922015-02-02 15:07:34 -08001816bool upb_fielddef_ismap(const upb_fielddef *f);
Josh Haberman78da6662016-01-13 19:05:43 -08001817bool upb_fielddef_haspresence(const upb_fielddef *f);
Chris Fallin91473dc2014-12-12 15:58:26 -08001818int64_t upb_fielddef_defaultint64(const upb_fielddef *f);
1819int32_t upb_fielddef_defaultint32(const upb_fielddef *f);
1820uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f);
1821uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f);
1822bool upb_fielddef_defaultbool(const upb_fielddef *f);
1823float upb_fielddef_defaultfloat(const upb_fielddef *f);
1824double upb_fielddef_defaultdouble(const upb_fielddef *f);
1825const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len);
1826bool upb_fielddef_enumhasdefaultint32(const upb_fielddef *f);
1827bool upb_fielddef_enumhasdefaultstr(const upb_fielddef *f);
1828bool upb_fielddef_hassubdef(const upb_fielddef *f);
1829const upb_def *upb_fielddef_subdef(const upb_fielddef *f);
1830const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f);
1831const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f);
1832const char *upb_fielddef_subdefname(const upb_fielddef *f);
1833
1834void upb_fielddef_settype(upb_fielddef *f, upb_fieldtype_t type);
1835void upb_fielddef_setdescriptortype(upb_fielddef *f, int type);
1836void upb_fielddef_setlabel(upb_fielddef *f, upb_label_t label);
1837bool upb_fielddef_setnumber(upb_fielddef *f, uint32_t number, upb_status *s);
1838bool upb_fielddef_setname(upb_fielddef *f, const char *name, upb_status *s);
Josh Haberman78da6662016-01-13 19:05:43 -08001839bool upb_fielddef_setjsonname(upb_fielddef *f, const char *name, upb_status *s);
1840bool upb_fielddef_clearjsonname(upb_fielddef *f);
Chris Fallin91473dc2014-12-12 15:58:26 -08001841bool upb_fielddef_setcontainingtypename(upb_fielddef *f, const char *name,
1842 upb_status *s);
1843void upb_fielddef_setisextension(upb_fielddef *f, bool is_extension);
1844void upb_fielddef_setlazy(upb_fielddef *f, bool lazy);
1845void upb_fielddef_setpacked(upb_fielddef *f, bool packed);
1846void upb_fielddef_setintfmt(upb_fielddef *f, upb_intfmt_t fmt);
1847void upb_fielddef_settagdelim(upb_fielddef *f, bool tag_delim);
1848void upb_fielddef_setdefaultint64(upb_fielddef *f, int64_t val);
1849void upb_fielddef_setdefaultint32(upb_fielddef *f, int32_t val);
1850void upb_fielddef_setdefaultuint64(upb_fielddef *f, uint64_t val);
1851void upb_fielddef_setdefaultuint32(upb_fielddef *f, uint32_t val);
1852void upb_fielddef_setdefaultbool(upb_fielddef *f, bool val);
1853void upb_fielddef_setdefaultfloat(upb_fielddef *f, float val);
1854void upb_fielddef_setdefaultdouble(upb_fielddef *f, double val);
1855bool upb_fielddef_setdefaultstr(upb_fielddef *f, const void *str, size_t len,
1856 upb_status *s);
1857void upb_fielddef_setdefaultcstr(upb_fielddef *f, const char *str,
1858 upb_status *s);
1859bool upb_fielddef_setsubdef(upb_fielddef *f, const upb_def *subdef,
1860 upb_status *s);
1861bool upb_fielddef_setmsgsubdef(upb_fielddef *f, const upb_msgdef *subdef,
1862 upb_status *s);
1863bool upb_fielddef_setenumsubdef(upb_fielddef *f, const upb_enumdef *subdef,
1864 upb_status *s);
1865bool upb_fielddef_setsubdefname(upb_fielddef *f, const char *name,
1866 upb_status *s);
1867
1868bool upb_fielddef_checklabel(int32_t label);
1869bool upb_fielddef_checktype(int32_t type);
1870bool upb_fielddef_checkdescriptortype(int32_t type);
1871bool upb_fielddef_checkintfmt(int32_t fmt);
1872
Josh Habermane8ed0212015-06-08 17:56:03 -07001873UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08001874
1875
1876/* upb::MessageDef ************************************************************/
1877
Chris Fallinfcd88892015-01-13 18:14:39 -08001878typedef upb_inttable_iter upb_msg_field_iter;
1879typedef upb_strtable_iter upb_msg_oneof_iter;
Chris Fallin91473dc2014-12-12 15:58:26 -08001880
Josh Habermane8ed0212015-06-08 17:56:03 -07001881#ifdef __cplusplus
1882
1883/* Structure that describes a single .proto message type.
1884 *
1885 * Its base class is upb::Def (use upb::upcast() to convert). */
1886class upb::MessageDef {
Chris Fallin91473dc2014-12-12 15:58:26 -08001887 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07001888 /* Returns NULL if memory allocation failed. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001889 static reffed_ptr<MessageDef> New();
1890
Josh Habermane8ed0212015-06-08 17:56:03 -07001891 /* upb::RefCounted methods like Ref()/Unref(). */
1892 UPB_REFCOUNTED_CPPMETHODS
Chris Fallin91473dc2014-12-12 15:58:26 -08001893
Josh Habermane8ed0212015-06-08 17:56:03 -07001894 /* Functionality from upb::Def. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001895 const char* full_name() const;
1896 bool set_full_name(const char* fullname, Status* s);
1897 bool set_full_name(const std::string& fullname, Status* s);
1898
Josh Habermane8ed0212015-06-08 17:56:03 -07001899 /* Call to freeze this MessageDef.
1900 * WARNING: this will fail if this message has any unfrozen submessages!
1901 * Messages with cycles must be frozen as a batch using upb::Def::Freeze(). */
Chris Fallin91473dc2014-12-12 15:58:26 -08001902 bool Freeze(Status* s);
1903
Josh Habermane8ed0212015-06-08 17:56:03 -07001904 /* The number of fields that belong to the MessageDef. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001905 int field_count() const;
1906
Josh Habermane8ed0212015-06-08 17:56:03 -07001907 /* The number of oneofs that belong to the MessageDef. */
Chris Fallinfcd88892015-01-13 18:14:39 -08001908 int oneof_count() const;
1909
Josh Habermane8ed0212015-06-08 17:56:03 -07001910 /* Adds a field (upb_fielddef object) to a msgdef. Requires that the msgdef
1911 * and the fielddefs are mutable. The fielddef's name and number must be
1912 * set, and the message may not already contain any field with this name or
1913 * number, and this fielddef may not be part of another message. In error
1914 * cases false is returned and the msgdef is unchanged.
1915 *
1916 * If the given field is part of a oneof, this call succeeds if and only if
1917 * that oneof is already part of this msgdef. (Note that adding a oneof to a
1918 * msgdef automatically adds all of its fields to the msgdef at the time that
1919 * the oneof is added, so it is usually more idiomatic to add the oneof's
1920 * fields first then add the oneof to the msgdef. This case is supported for
1921 * convenience.)
1922 *
1923 * If |f| is already part of this MessageDef, this method performs no action
1924 * and returns true (success). Thus, this method is idempotent. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001925 bool AddField(FieldDef* f, Status* s);
1926 bool AddField(const reffed_ptr<FieldDef>& f, Status* s);
1927
Josh Habermane8ed0212015-06-08 17:56:03 -07001928 /* Adds a oneof (upb_oneofdef object) to a msgdef. Requires that the msgdef,
1929 * oneof, and any fielddefs are mutable, that the fielddefs contained in the
1930 * oneof do not have any name or number conflicts with existing fields in the
1931 * msgdef, and that the oneof's name is unique among all oneofs in the msgdef.
1932 * If the oneof is added successfully, all of its fields will be added
1933 * directly to the msgdef as well. In error cases, false is returned and the
1934 * msgdef is unchanged. */
Chris Fallinfcd88892015-01-13 18:14:39 -08001935 bool AddOneof(OneofDef* o, Status* s);
1936 bool AddOneof(const reffed_ptr<OneofDef>& o, Status* s);
1937
Josh Haberman78da6662016-01-13 19:05:43 -08001938 /* Set this to false to indicate that primitive fields should not have
1939 * explicit presence information associated with them. This will affect all
1940 * fields added to this message. Defaults to true. */
1941 void SetPrimitivesHavePresence(bool have_presence);
1942
Josh Habermane8ed0212015-06-08 17:56:03 -07001943 /* These return NULL if the field is not found. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001944 FieldDef* FindFieldByNumber(uint32_t number);
1945 FieldDef* FindFieldByName(const char *name, size_t len);
1946 const FieldDef* FindFieldByNumber(uint32_t number) const;
1947 const FieldDef* FindFieldByName(const char* name, size_t len) const;
1948
1949
1950 FieldDef* FindFieldByName(const char *name) {
1951 return FindFieldByName(name, strlen(name));
1952 }
1953 const FieldDef* FindFieldByName(const char *name) const {
1954 return FindFieldByName(name, strlen(name));
1955 }
1956
1957 template <class T>
1958 FieldDef* FindFieldByName(const T& str) {
1959 return FindFieldByName(str.c_str(), str.size());
1960 }
1961 template <class T>
1962 const FieldDef* FindFieldByName(const T& str) const {
1963 return FindFieldByName(str.c_str(), str.size());
1964 }
1965
Chris Fallinfcd88892015-01-13 18:14:39 -08001966 OneofDef* FindOneofByName(const char* name, size_t len);
1967 const OneofDef* FindOneofByName(const char* name, size_t len) const;
1968
1969 OneofDef* FindOneofByName(const char* name) {
1970 return FindOneofByName(name, strlen(name));
1971 }
1972 const OneofDef* FindOneofByName(const char* name) const {
1973 return FindOneofByName(name, strlen(name));
1974 }
1975
1976 template<class T>
1977 OneofDef* FindOneofByName(const T& str) {
1978 return FindOneofByName(str.c_str(), str.size());
1979 }
1980 template<class T>
1981 const OneofDef* FindOneofByName(const T& str) const {
1982 return FindOneofByName(str.c_str(), str.size());
1983 }
1984
Josh Habermane8ed0212015-06-08 17:56:03 -07001985 /* Returns a new msgdef that is a copy of the given msgdef (and a copy of all
1986 * the fields) but with any references to submessages broken and replaced
1987 * with just the name of the submessage. Returns NULL if memory allocation
1988 * failed.
1989 *
1990 * TODO(haberman): which is more useful, keeping fields resolved or
1991 * unresolving them? If there's no obvious answer, Should this functionality
1992 * just be moved into symtab.c? */
Chris Fallin91473dc2014-12-12 15:58:26 -08001993 MessageDef* Dup(const void* owner) const;
1994
Josh Habermane8ed0212015-06-08 17:56:03 -07001995 /* Is this message a map entry? */
Chris Fallinfd1a3ff2015-01-06 15:44:09 -08001996 void setmapentry(bool map_entry);
1997 bool mapentry() const;
1998
Josh Habermane8ed0212015-06-08 17:56:03 -07001999 /* Iteration over fields. The order is undefined. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002000 class field_iterator
2001 : public std::iterator<std::forward_iterator_tag, FieldDef*> {
Chris Fallin91473dc2014-12-12 15:58:26 -08002002 public:
Chris Fallinfcd88892015-01-13 18:14:39 -08002003 explicit field_iterator(MessageDef* md);
2004 static field_iterator end(MessageDef* md);
Chris Fallin91473dc2014-12-12 15:58:26 -08002005
2006 void operator++();
2007 FieldDef* operator*() const;
Chris Fallinfcd88892015-01-13 18:14:39 -08002008 bool operator!=(const field_iterator& other) const;
2009 bool operator==(const field_iterator& other) const;
Chris Fallin91473dc2014-12-12 15:58:26 -08002010
2011 private:
Chris Fallinfcd88892015-01-13 18:14:39 -08002012 upb_msg_field_iter iter_;
Chris Fallin91473dc2014-12-12 15:58:26 -08002013 };
2014
Chris Fallinfcd88892015-01-13 18:14:39 -08002015 class const_field_iterator
Chris Fallin91473dc2014-12-12 15:58:26 -08002016 : public std::iterator<std::forward_iterator_tag, const FieldDef*> {
2017 public:
Chris Fallinfcd88892015-01-13 18:14:39 -08002018 explicit const_field_iterator(const MessageDef* md);
2019 static const_field_iterator end(const MessageDef* md);
Chris Fallin91473dc2014-12-12 15:58:26 -08002020
2021 void operator++();
2022 const FieldDef* operator*() const;
Chris Fallinfcd88892015-01-13 18:14:39 -08002023 bool operator!=(const const_field_iterator& other) const;
2024 bool operator==(const const_field_iterator& other) const;
Chris Fallin91473dc2014-12-12 15:58:26 -08002025
2026 private:
Chris Fallinfcd88892015-01-13 18:14:39 -08002027 upb_msg_field_iter iter_;
Chris Fallin91473dc2014-12-12 15:58:26 -08002028 };
2029
Josh Habermane8ed0212015-06-08 17:56:03 -07002030 /* Iteration over oneofs. The order is undefined. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002031 class oneof_iterator
2032 : public std::iterator<std::forward_iterator_tag, FieldDef*> {
2033 public:
2034 explicit oneof_iterator(MessageDef* md);
2035 static oneof_iterator end(MessageDef* md);
2036
2037 void operator++();
2038 OneofDef* operator*() const;
2039 bool operator!=(const oneof_iterator& other) const;
2040 bool operator==(const oneof_iterator& other) const;
2041
2042 private:
2043 upb_msg_oneof_iter iter_;
2044 };
2045
2046 class const_oneof_iterator
2047 : public std::iterator<std::forward_iterator_tag, const FieldDef*> {
2048 public:
2049 explicit const_oneof_iterator(const MessageDef* md);
2050 static const_oneof_iterator end(const MessageDef* md);
2051
2052 void operator++();
2053 const OneofDef* operator*() const;
2054 bool operator!=(const const_oneof_iterator& other) const;
2055 bool operator==(const const_oneof_iterator& other) const;
2056
2057 private:
2058 upb_msg_oneof_iter iter_;
2059 };
2060
2061 class FieldAccessor {
2062 public:
2063 explicit FieldAccessor(MessageDef* msg) : msg_(msg) {}
2064 field_iterator begin() { return msg_->field_begin(); }
2065 field_iterator end() { return msg_->field_end(); }
2066 private:
2067 MessageDef* msg_;
2068 };
2069
2070 class ConstFieldAccessor {
2071 public:
2072 explicit ConstFieldAccessor(const MessageDef* msg) : msg_(msg) {}
2073 const_field_iterator begin() { return msg_->field_begin(); }
2074 const_field_iterator end() { return msg_->field_end(); }
2075 private:
2076 const MessageDef* msg_;
2077 };
2078
2079 class OneofAccessor {
2080 public:
2081 explicit OneofAccessor(MessageDef* msg) : msg_(msg) {}
2082 oneof_iterator begin() { return msg_->oneof_begin(); }
2083 oneof_iterator end() { return msg_->oneof_end(); }
2084 private:
2085 MessageDef* msg_;
2086 };
2087
2088 class ConstOneofAccessor {
2089 public:
2090 explicit ConstOneofAccessor(const MessageDef* msg) : msg_(msg) {}
2091 const_oneof_iterator begin() { return msg_->oneof_begin(); }
2092 const_oneof_iterator end() { return msg_->oneof_end(); }
2093 private:
2094 const MessageDef* msg_;
2095 };
2096
2097 field_iterator field_begin();
2098 field_iterator field_end();
2099 const_field_iterator field_begin() const;
2100 const_field_iterator field_end() const;
2101
2102 oneof_iterator oneof_begin();
2103 oneof_iterator oneof_end();
2104 const_oneof_iterator oneof_begin() const;
2105 const_oneof_iterator oneof_end() const;
2106
2107 FieldAccessor fields() { return FieldAccessor(this); }
2108 ConstFieldAccessor fields() const { return ConstFieldAccessor(this); }
2109 OneofAccessor oneofs() { return OneofAccessor(this); }
2110 ConstOneofAccessor oneofs() const { return ConstOneofAccessor(this); }
Chris Fallin91473dc2014-12-12 15:58:26 -08002111
2112 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07002113 UPB_DISALLOW_POD_OPS(MessageDef, upb::MessageDef)
2114};
Chris Fallin91473dc2014-12-12 15:58:26 -08002115
Josh Habermane8ed0212015-06-08 17:56:03 -07002116#endif /* __cplusplus */
Chris Fallin91473dc2014-12-12 15:58:26 -08002117
Josh Habermane8ed0212015-06-08 17:56:03 -07002118UPB_BEGIN_EXTERN_C
Chris Fallinfcd88892015-01-13 18:14:39 -08002119
Josh Habermane8ed0212015-06-08 17:56:03 -07002120/* Returns NULL if memory allocation failed. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002121upb_msgdef *upb_msgdef_new(const void *owner);
2122
Josh Habermane8ed0212015-06-08 17:56:03 -07002123/* Include upb_refcounted methods like upb_msgdef_ref(). */
2124UPB_REFCOUNTED_CMETHODS(upb_msgdef, upb_msgdef_upcast2)
2125
Chris Fallin91473dc2014-12-12 15:58:26 -08002126bool upb_msgdef_freeze(upb_msgdef *m, upb_status *status);
2127
Chris Fallin91473dc2014-12-12 15:58:26 -08002128const char *upb_msgdef_fullname(const upb_msgdef *m);
2129bool upb_msgdef_setfullname(upb_msgdef *m, const char *fullname, upb_status *s);
2130
2131upb_msgdef *upb_msgdef_dup(const upb_msgdef *m, const void *owner);
2132bool upb_msgdef_addfield(upb_msgdef *m, upb_fielddef *f, const void *ref_donor,
2133 upb_status *s);
Chris Fallinfcd88892015-01-13 18:14:39 -08002134bool upb_msgdef_addoneof(upb_msgdef *m, upb_oneofdef *o, const void *ref_donor,
2135 upb_status *s);
Josh Haberman78da6662016-01-13 19:05:43 -08002136void upb_msgdef_setprimitiveshavepresence(upb_msgdef *m, bool have_presence);
Chris Fallin91473dc2014-12-12 15:58:26 -08002137
Josh Habermane8ed0212015-06-08 17:56:03 -07002138/* Field lookup in a couple of different variations:
2139 * - itof = int to field
2140 * - ntof = name to field
2141 * - ntofz = name to field, null-terminated string. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002142const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i);
2143const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name,
2144 size_t len);
2145int upb_msgdef_numfields(const upb_msgdef *m);
2146
2147UPB_INLINE const upb_fielddef *upb_msgdef_ntofz(const upb_msgdef *m,
2148 const char *name) {
2149 return upb_msgdef_ntof(m, name, strlen(name));
2150}
2151
2152UPB_INLINE upb_fielddef *upb_msgdef_itof_mutable(upb_msgdef *m, uint32_t i) {
2153 return (upb_fielddef*)upb_msgdef_itof(m, i);
2154}
2155
2156UPB_INLINE upb_fielddef *upb_msgdef_ntof_mutable(upb_msgdef *m,
2157 const char *name, size_t len) {
2158 return (upb_fielddef *)upb_msgdef_ntof(m, name, len);
2159}
2160
Josh Habermane8ed0212015-06-08 17:56:03 -07002161/* Oneof lookup:
2162 * - ntoo = name to oneof
2163 * - ntooz = name to oneof, null-terminated string. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002164const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name,
2165 size_t len);
2166int upb_msgdef_numoneofs(const upb_msgdef *m);
2167
2168UPB_INLINE const upb_oneofdef *upb_msgdef_ntooz(const upb_msgdef *m,
2169 const char *name) {
2170 return upb_msgdef_ntoo(m, name, strlen(name));
2171}
2172
2173UPB_INLINE upb_oneofdef *upb_msgdef_ntoo_mutable(upb_msgdef *m,
2174 const char *name, size_t len) {
2175 return (upb_oneofdef *)upb_msgdef_ntoo(m, name, len);
2176}
2177
Chris Fallinfd1a3ff2015-01-06 15:44:09 -08002178void upb_msgdef_setmapentry(upb_msgdef *m, bool map_entry);
2179bool upb_msgdef_mapentry(const upb_msgdef *m);
2180
Josh Habermane8ed0212015-06-08 17:56:03 -07002181/* Well-known field tag numbers for map-entry messages. */
Chris Fallina5075922015-02-02 15:07:34 -08002182#define UPB_MAPENTRY_KEY 1
2183#define UPB_MAPENTRY_VALUE 2
2184
Chris Fallinfcd88892015-01-13 18:14:39 -08002185const upb_oneofdef *upb_msgdef_findoneof(const upb_msgdef *m,
2186 const char *name);
2187int upb_msgdef_numoneofs(const upb_msgdef *m);
2188
Josh Habermane8ed0212015-06-08 17:56:03 -07002189/* upb_msg_field_iter i;
2190 * for(upb_msg_field_begin(&i, m);
2191 * !upb_msg_field_done(&i);
2192 * upb_msg_field_next(&i)) {
2193 * upb_fielddef *f = upb_msg_iter_field(&i);
2194 * // ...
2195 * }
2196 *
2197 * For C we don't have separate iterators for const and non-const.
2198 * It is the caller's responsibility to cast the upb_fielddef* to
2199 * const if the upb_msgdef* is const. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002200void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m);
2201void upb_msg_field_next(upb_msg_field_iter *iter);
2202bool upb_msg_field_done(const upb_msg_field_iter *iter);
2203upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter);
2204void upb_msg_field_iter_setdone(upb_msg_field_iter *iter);
2205
Josh Habermane8ed0212015-06-08 17:56:03 -07002206/* Similar to above, we also support iterating through the oneofs in a
2207 * msgdef. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002208void upb_msg_oneof_begin(upb_msg_oneof_iter *iter, const upb_msgdef *m);
2209void upb_msg_oneof_next(upb_msg_oneof_iter *iter);
2210bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter);
2211upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter);
2212void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter);
Chris Fallin91473dc2014-12-12 15:58:26 -08002213
Josh Habermane8ed0212015-06-08 17:56:03 -07002214UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08002215
2216
2217/* upb::EnumDef ***************************************************************/
2218
2219typedef upb_strtable_iter upb_enum_iter;
2220
Josh Habermane8ed0212015-06-08 17:56:03 -07002221#ifdef __cplusplus
2222
2223/* Class that represents an enum. Its base class is upb::Def (convert with
2224 * upb::upcast()). */
2225class upb::EnumDef {
Chris Fallin91473dc2014-12-12 15:58:26 -08002226 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07002227 /* Returns NULL if memory allocation failed. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002228 static reffed_ptr<EnumDef> New();
2229
Josh Habermane8ed0212015-06-08 17:56:03 -07002230 /* upb::RefCounted methods like Ref()/Unref(). */
2231 UPB_REFCOUNTED_CPPMETHODS
Chris Fallin91473dc2014-12-12 15:58:26 -08002232
Josh Habermane8ed0212015-06-08 17:56:03 -07002233 /* Functionality from upb::Def. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002234 const char* full_name() const;
2235 bool set_full_name(const char* fullname, Status* s);
2236 bool set_full_name(const std::string& fullname, Status* s);
2237
Josh Habermane8ed0212015-06-08 17:56:03 -07002238 /* Call to freeze this EnumDef. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002239 bool Freeze(Status* s);
2240
Josh Habermane8ed0212015-06-08 17:56:03 -07002241 /* The value that is used as the default when no field default is specified.
2242 * If not set explicitly, the first value that was added will be used.
2243 * The default value must be a member of the enum.
2244 * Requires that value_count() > 0. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002245 int32_t default_value() const;
2246
Josh Habermane8ed0212015-06-08 17:56:03 -07002247 /* Sets the default value. If this value is not valid, returns false and an
2248 * error message in status. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002249 bool set_default_value(int32_t val, Status* status);
2250
Josh Habermane8ed0212015-06-08 17:56:03 -07002251 /* Returns the number of values currently defined in the enum. Note that
2252 * multiple names can refer to the same number, so this may be greater than
2253 * the total number of unique numbers. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002254 int value_count() const;
2255
Josh Habermane8ed0212015-06-08 17:56:03 -07002256 /* Adds a single name/number pair to the enum. Fails if this name has
2257 * already been used by another value. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002258 bool AddValue(const char* name, int32_t num, Status* status);
2259 bool AddValue(const std::string& name, int32_t num, Status* status);
2260
Josh Habermane8ed0212015-06-08 17:56:03 -07002261 /* Lookups from name to integer, returning true if found. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002262 bool FindValueByName(const char* name, int32_t* num) const;
2263
Josh Habermane8ed0212015-06-08 17:56:03 -07002264 /* Finds the name corresponding to the given number, or NULL if none was
2265 * found. If more than one name corresponds to this number, returns the
2266 * first one that was added. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002267 const char* FindValueByNumber(int32_t num) const;
2268
Josh Habermane8ed0212015-06-08 17:56:03 -07002269 /* Returns a new EnumDef with all the same values. The new EnumDef will be
2270 * owned by the given owner. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002271 EnumDef* Dup(const void* owner) const;
2272
Josh Habermane8ed0212015-06-08 17:56:03 -07002273 /* Iteration over name/value pairs. The order is undefined.
2274 * Adding an enum val invalidates any iterators.
2275 *
2276 * TODO: make compatible with range-for, with elements as pairs? */
Chris Fallin91473dc2014-12-12 15:58:26 -08002277 class Iterator {
2278 public:
2279 explicit Iterator(const EnumDef*);
2280
2281 int32_t number();
2282 const char *name();
2283 bool Done();
2284 void Next();
2285
2286 private:
2287 upb_enum_iter iter_;
2288 };
2289
2290 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07002291 UPB_DISALLOW_POD_OPS(EnumDef, upb::EnumDef)
2292};
Chris Fallin91473dc2014-12-12 15:58:26 -08002293
Josh Habermane8ed0212015-06-08 17:56:03 -07002294#endif /* __cplusplus */
Chris Fallin91473dc2014-12-12 15:58:26 -08002295
Josh Habermane8ed0212015-06-08 17:56:03 -07002296UPB_BEGIN_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08002297
Josh Habermane8ed0212015-06-08 17:56:03 -07002298/* Native C API. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002299upb_enumdef *upb_enumdef_new(const void *owner);
2300upb_enumdef *upb_enumdef_dup(const upb_enumdef *e, const void *owner);
2301
Josh Habermane8ed0212015-06-08 17:56:03 -07002302/* Include upb_refcounted methods like upb_enumdef_ref(). */
2303UPB_REFCOUNTED_CMETHODS(upb_enumdef, upb_enumdef_upcast2)
2304
Chris Fallin91473dc2014-12-12 15:58:26 -08002305bool upb_enumdef_freeze(upb_enumdef *e, upb_status *status);
2306
Josh Habermane8ed0212015-06-08 17:56:03 -07002307/* From upb_def. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002308const char *upb_enumdef_fullname(const upb_enumdef *e);
2309bool upb_enumdef_setfullname(upb_enumdef *e, const char *fullname,
2310 upb_status *s);
2311
2312int32_t upb_enumdef_default(const upb_enumdef *e);
2313bool upb_enumdef_setdefault(upb_enumdef *e, int32_t val, upb_status *s);
2314int upb_enumdef_numvals(const upb_enumdef *e);
2315bool upb_enumdef_addval(upb_enumdef *e, const char *name, int32_t num,
2316 upb_status *status);
2317
Josh Habermane8ed0212015-06-08 17:56:03 -07002318/* Enum lookups:
2319 * - ntoi: look up a name with specified length.
2320 * - ntoiz: look up a name provided as a null-terminated string.
2321 * - iton: look up an integer, returning the name as a null-terminated
2322 * string. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002323bool upb_enumdef_ntoi(const upb_enumdef *e, const char *name, size_t len,
2324 int32_t *num);
2325UPB_INLINE bool upb_enumdef_ntoiz(const upb_enumdef *e,
2326 const char *name, int32_t *num) {
2327 return upb_enumdef_ntoi(e, name, strlen(name), num);
2328}
2329const char *upb_enumdef_iton(const upb_enumdef *e, int32_t num);
2330
Josh Habermane8ed0212015-06-08 17:56:03 -07002331/* upb_enum_iter i;
2332 * for(upb_enum_begin(&i, e); !upb_enum_done(&i); upb_enum_next(&i)) {
2333 * // ...
2334 * }
2335 */
Chris Fallin91473dc2014-12-12 15:58:26 -08002336void upb_enum_begin(upb_enum_iter *iter, const upb_enumdef *e);
2337void upb_enum_next(upb_enum_iter *iter);
2338bool upb_enum_done(upb_enum_iter *iter);
2339const char *upb_enum_iter_name(upb_enum_iter *iter);
2340int32_t upb_enum_iter_number(upb_enum_iter *iter);
2341
Josh Habermane8ed0212015-06-08 17:56:03 -07002342UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08002343
Chris Fallinfcd88892015-01-13 18:14:39 -08002344/* upb::OneofDef **************************************************************/
2345
2346typedef upb_inttable_iter upb_oneof_iter;
2347
Josh Habermane8ed0212015-06-08 17:56:03 -07002348#ifdef __cplusplus
2349
2350/* Class that represents a oneof. Its base class is upb::Def (convert with
2351 * upb::upcast()). */
2352class upb::OneofDef {
Chris Fallinfcd88892015-01-13 18:14:39 -08002353 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07002354 /* Returns NULL if memory allocation failed. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002355 static reffed_ptr<OneofDef> New();
2356
Josh Habermane8ed0212015-06-08 17:56:03 -07002357 /* upb::RefCounted methods like Ref()/Unref(). */
2358 UPB_REFCOUNTED_CPPMETHODS
Chris Fallinfcd88892015-01-13 18:14:39 -08002359
Josh Habermane8ed0212015-06-08 17:56:03 -07002360 /* Functionality from upb::Def. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002361 const char* full_name() const;
2362
Josh Habermane8ed0212015-06-08 17:56:03 -07002363 /* Returns the MessageDef that owns this OneofDef. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002364 const MessageDef* containing_type() const;
2365
Josh Habermane8ed0212015-06-08 17:56:03 -07002366 /* Returns the name of this oneof. This is the name used to look up the oneof
2367 * by name once added to a message def. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002368 const char* name() const;
2369 bool set_name(const char* name, Status* s);
2370
Josh Habermane8ed0212015-06-08 17:56:03 -07002371 /* Returns the number of fields currently defined in the oneof. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002372 int field_count() const;
2373
Josh Habermane8ed0212015-06-08 17:56:03 -07002374 /* Adds a field to the oneof. The field must not have been added to any other
2375 * oneof or msgdef. If the oneof is not yet part of a msgdef, then when the
2376 * oneof is eventually added to a msgdef, all fields added to the oneof will
2377 * also be added to the msgdef at that time. If the oneof is already part of a
2378 * msgdef, the field must either be a part of that msgdef already, or must not
2379 * be a part of any msgdef; in the latter case, the field is added to the
2380 * msgdef as a part of this operation.
2381 *
2382 * The field may only have an OPTIONAL label, never REQUIRED or REPEATED.
2383 *
2384 * If |f| is already part of this MessageDef, this method performs no action
2385 * and returns true (success). Thus, this method is idempotent. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002386 bool AddField(FieldDef* field, Status* s);
2387 bool AddField(const reffed_ptr<FieldDef>& field, Status* s);
2388
Josh Habermane8ed0212015-06-08 17:56:03 -07002389 /* Looks up by name. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002390 const FieldDef* FindFieldByName(const char* name, size_t len) const;
2391 FieldDef* FindFieldByName(const char* name, size_t len);
2392 const FieldDef* FindFieldByName(const char* name) const {
2393 return FindFieldByName(name, strlen(name));
2394 }
2395 FieldDef* FindFieldByName(const char* name) {
2396 return FindFieldByName(name, strlen(name));
2397 }
2398
2399 template <class T>
2400 FieldDef* FindFieldByName(const T& str) {
2401 return FindFieldByName(str.c_str(), str.size());
2402 }
2403 template <class T>
2404 const FieldDef* FindFieldByName(const T& str) const {
2405 return FindFieldByName(str.c_str(), str.size());
2406 }
2407
Josh Habermane8ed0212015-06-08 17:56:03 -07002408 /* Looks up by tag number. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002409 const FieldDef* FindFieldByNumber(uint32_t num) const;
2410
Josh Habermane8ed0212015-06-08 17:56:03 -07002411 /* Returns a new OneofDef with all the same fields. The OneofDef will be owned
2412 * by the given owner. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002413 OneofDef* Dup(const void* owner) const;
2414
Josh Habermane8ed0212015-06-08 17:56:03 -07002415 /* Iteration over fields. The order is undefined. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002416 class iterator : public std::iterator<std::forward_iterator_tag, FieldDef*> {
2417 public:
2418 explicit iterator(OneofDef* md);
2419 static iterator end(OneofDef* md);
2420
2421 void operator++();
2422 FieldDef* operator*() const;
2423 bool operator!=(const iterator& other) const;
2424 bool operator==(const iterator& other) const;
2425
2426 private:
2427 upb_oneof_iter iter_;
2428 };
2429
2430 class const_iterator
2431 : public std::iterator<std::forward_iterator_tag, const FieldDef*> {
2432 public:
2433 explicit const_iterator(const OneofDef* md);
2434 static const_iterator end(const OneofDef* md);
2435
2436 void operator++();
2437 const FieldDef* operator*() const;
2438 bool operator!=(const const_iterator& other) const;
2439 bool operator==(const const_iterator& other) const;
2440
2441 private:
2442 upb_oneof_iter iter_;
2443 };
2444
2445 iterator begin();
2446 iterator end();
2447 const_iterator begin() const;
2448 const_iterator end() const;
2449
2450 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07002451 UPB_DISALLOW_POD_OPS(OneofDef, upb::OneofDef)
2452};
Chris Fallinfcd88892015-01-13 18:14:39 -08002453
Josh Habermane8ed0212015-06-08 17:56:03 -07002454#endif /* __cplusplus */
Chris Fallinfcd88892015-01-13 18:14:39 -08002455
Josh Habermane8ed0212015-06-08 17:56:03 -07002456UPB_BEGIN_EXTERN_C
Chris Fallinfcd88892015-01-13 18:14:39 -08002457
Josh Habermane8ed0212015-06-08 17:56:03 -07002458/* Native C API. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002459upb_oneofdef *upb_oneofdef_new(const void *owner);
2460upb_oneofdef *upb_oneofdef_dup(const upb_oneofdef *o, const void *owner);
2461
Josh Habermane8ed0212015-06-08 17:56:03 -07002462/* Include upb_refcounted methods like upb_oneofdef_ref(). */
2463UPB_REFCOUNTED_CMETHODS(upb_oneofdef, upb_oneofdef_upcast2)
Chris Fallinfcd88892015-01-13 18:14:39 -08002464
2465const char *upb_oneofdef_name(const upb_oneofdef *o);
2466bool upb_oneofdef_setname(upb_oneofdef *o, const char *name, upb_status *s);
2467
2468const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o);
2469int upb_oneofdef_numfields(const upb_oneofdef *o);
2470bool upb_oneofdef_addfield(upb_oneofdef *o, upb_fielddef *f,
2471 const void *ref_donor,
2472 upb_status *s);
2473
Josh Habermane8ed0212015-06-08 17:56:03 -07002474/* Oneof lookups:
2475 * - ntof: look up a field by name.
2476 * - ntofz: look up a field by name (as a null-terminated string).
2477 * - itof: look up a field by number. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002478const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o,
2479 const char *name, size_t length);
2480UPB_INLINE const upb_fielddef *upb_oneofdef_ntofz(const upb_oneofdef *o,
2481 const char *name) {
2482 return upb_oneofdef_ntof(o, name, strlen(name));
2483}
2484const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num);
2485
Josh Habermane8ed0212015-06-08 17:56:03 -07002486/* upb_oneof_iter i;
2487 * for(upb_oneof_begin(&i, e); !upb_oneof_done(&i); upb_oneof_next(&i)) {
2488 * // ...
2489 * }
2490 */
Chris Fallinfcd88892015-01-13 18:14:39 -08002491void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o);
2492void upb_oneof_next(upb_oneof_iter *iter);
2493bool upb_oneof_done(upb_oneof_iter *iter);
2494upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter);
2495void upb_oneof_iter_setdone(upb_oneof_iter *iter);
2496
Josh Habermane8ed0212015-06-08 17:56:03 -07002497UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08002498
2499#ifdef __cplusplus
2500
2501UPB_INLINE const char* upb_safecstr(const std::string& str) {
2502 assert(str.size() == std::strlen(str.c_str()));
2503 return str.c_str();
2504}
2505
Josh Habermane8ed0212015-06-08 17:56:03 -07002506/* Inline C++ wrappers. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002507namespace upb {
2508
2509inline Def* Def::Dup(const void* owner) const {
2510 return upb_def_dup(this, owner);
2511}
Chris Fallin91473dc2014-12-12 15:58:26 -08002512inline Def::Type Def::def_type() const { return upb_def_type(this); }
2513inline const char* Def::full_name() const { return upb_def_fullname(this); }
2514inline bool Def::set_full_name(const char* fullname, Status* s) {
2515 return upb_def_setfullname(this, fullname, s);
2516}
2517inline bool Def::set_full_name(const std::string& fullname, Status* s) {
2518 return upb_def_setfullname(this, upb_safecstr(fullname), s);
2519}
2520inline bool Def::Freeze(Def* const* defs, int n, Status* status) {
2521 return upb_def_freeze(defs, n, status);
2522}
2523inline bool Def::Freeze(const std::vector<Def*>& defs, Status* status) {
2524 return upb_def_freeze((Def* const*)&defs[0], defs.size(), status);
2525}
2526
2527inline bool FieldDef::CheckType(int32_t val) {
2528 return upb_fielddef_checktype(val);
2529}
2530inline bool FieldDef::CheckLabel(int32_t val) {
2531 return upb_fielddef_checklabel(val);
2532}
2533inline bool FieldDef::CheckDescriptorType(int32_t val) {
2534 return upb_fielddef_checkdescriptortype(val);
2535}
2536inline bool FieldDef::CheckIntegerFormat(int32_t val) {
2537 return upb_fielddef_checkintfmt(val);
2538}
2539inline FieldDef::Type FieldDef::ConvertType(int32_t val) {
2540 assert(CheckType(val));
2541 return static_cast<FieldDef::Type>(val);
2542}
2543inline FieldDef::Label FieldDef::ConvertLabel(int32_t val) {
2544 assert(CheckLabel(val));
2545 return static_cast<FieldDef::Label>(val);
2546}
2547inline FieldDef::DescriptorType FieldDef::ConvertDescriptorType(int32_t val) {
2548 assert(CheckDescriptorType(val));
2549 return static_cast<FieldDef::DescriptorType>(val);
2550}
2551inline FieldDef::IntegerFormat FieldDef::ConvertIntegerFormat(int32_t val) {
2552 assert(CheckIntegerFormat(val));
2553 return static_cast<FieldDef::IntegerFormat>(val);
2554}
2555
2556inline reffed_ptr<FieldDef> FieldDef::New() {
2557 upb_fielddef *f = upb_fielddef_new(&f);
2558 return reffed_ptr<FieldDef>(f, &f);
2559}
2560inline FieldDef* FieldDef::Dup(const void* owner) const {
2561 return upb_fielddef_dup(this, owner);
2562}
Chris Fallin91473dc2014-12-12 15:58:26 -08002563inline const char* FieldDef::full_name() const {
2564 return upb_fielddef_fullname(this);
2565}
2566inline bool FieldDef::set_full_name(const char* fullname, Status* s) {
2567 return upb_fielddef_setfullname(this, fullname, s);
2568}
2569inline bool FieldDef::set_full_name(const std::string& fullname, Status* s) {
2570 return upb_fielddef_setfullname(this, upb_safecstr(fullname), s);
2571}
2572inline bool FieldDef::type_is_set() const {
2573 return upb_fielddef_typeisset(this);
2574}
2575inline FieldDef::Type FieldDef::type() const { return upb_fielddef_type(this); }
2576inline FieldDef::DescriptorType FieldDef::descriptor_type() const {
2577 return upb_fielddef_descriptortype(this);
2578}
2579inline FieldDef::Label FieldDef::label() const {
2580 return upb_fielddef_label(this);
2581}
2582inline uint32_t FieldDef::number() const { return upb_fielddef_number(this); }
2583inline const char* FieldDef::name() const { return upb_fielddef_name(this); }
2584inline bool FieldDef::is_extension() const {
2585 return upb_fielddef_isextension(this);
2586}
Josh Habermanf654d492016-02-18 11:07:51 -08002587inline size_t FieldDef::GetJsonName(char* buf, size_t len) const {
2588 return upb_fielddef_getjsonname(this, buf, len);
2589}
Chris Fallin91473dc2014-12-12 15:58:26 -08002590inline bool FieldDef::lazy() const {
2591 return upb_fielddef_lazy(this);
2592}
2593inline void FieldDef::set_lazy(bool lazy) {
2594 upb_fielddef_setlazy(this, lazy);
2595}
2596inline bool FieldDef::packed() const {
2597 return upb_fielddef_packed(this);
2598}
2599inline void FieldDef::set_packed(bool packed) {
2600 upb_fielddef_setpacked(this, packed);
2601}
2602inline const MessageDef* FieldDef::containing_type() const {
2603 return upb_fielddef_containingtype(this);
2604}
Chris Fallinfcd88892015-01-13 18:14:39 -08002605inline const OneofDef* FieldDef::containing_oneof() const {
2606 return upb_fielddef_containingoneof(this);
2607}
Chris Fallin91473dc2014-12-12 15:58:26 -08002608inline const char* FieldDef::containing_type_name() {
2609 return upb_fielddef_containingtypename(this);
2610}
2611inline bool FieldDef::set_number(uint32_t number, Status* s) {
2612 return upb_fielddef_setnumber(this, number, s);
2613}
2614inline bool FieldDef::set_name(const char *name, Status* s) {
2615 return upb_fielddef_setname(this, name, s);
2616}
2617inline bool FieldDef::set_name(const std::string& name, Status* s) {
2618 return upb_fielddef_setname(this, upb_safecstr(name), s);
2619}
Josh Haberman78da6662016-01-13 19:05:43 -08002620inline bool FieldDef::set_json_name(const char *name, Status* s) {
2621 return upb_fielddef_setjsonname(this, name, s);
2622}
2623inline bool FieldDef::set_json_name(const std::string& name, Status* s) {
2624 return upb_fielddef_setjsonname(this, upb_safecstr(name), s);
2625}
2626inline void FieldDef::clear_json_name() {
2627 upb_fielddef_clearjsonname(this);
2628}
Chris Fallin91473dc2014-12-12 15:58:26 -08002629inline bool FieldDef::set_containing_type_name(const char *name, Status* s) {
2630 return upb_fielddef_setcontainingtypename(this, name, s);
2631}
2632inline bool FieldDef::set_containing_type_name(const std::string &name,
2633 Status *s) {
2634 return upb_fielddef_setcontainingtypename(this, upb_safecstr(name), s);
2635}
2636inline void FieldDef::set_type(upb_fieldtype_t type) {
2637 upb_fielddef_settype(this, type);
2638}
2639inline void FieldDef::set_is_extension(bool is_extension) {
2640 upb_fielddef_setisextension(this, is_extension);
2641}
2642inline void FieldDef::set_descriptor_type(FieldDef::DescriptorType type) {
2643 upb_fielddef_setdescriptortype(this, type);
2644}
2645inline void FieldDef::set_label(upb_label_t label) {
2646 upb_fielddef_setlabel(this, label);
2647}
2648inline bool FieldDef::IsSubMessage() const {
2649 return upb_fielddef_issubmsg(this);
2650}
2651inline bool FieldDef::IsString() const { return upb_fielddef_isstring(this); }
2652inline bool FieldDef::IsSequence() const { return upb_fielddef_isseq(this); }
Chris Fallina5075922015-02-02 15:07:34 -08002653inline bool FieldDef::IsMap() const { return upb_fielddef_ismap(this); }
Chris Fallin91473dc2014-12-12 15:58:26 -08002654inline int64_t FieldDef::default_int64() const {
2655 return upb_fielddef_defaultint64(this);
2656}
2657inline int32_t FieldDef::default_int32() const {
2658 return upb_fielddef_defaultint32(this);
2659}
2660inline uint64_t FieldDef::default_uint64() const {
2661 return upb_fielddef_defaultuint64(this);
2662}
2663inline uint32_t FieldDef::default_uint32() const {
2664 return upb_fielddef_defaultuint32(this);
2665}
2666inline bool FieldDef::default_bool() const {
2667 return upb_fielddef_defaultbool(this);
2668}
2669inline float FieldDef::default_float() const {
2670 return upb_fielddef_defaultfloat(this);
2671}
2672inline double FieldDef::default_double() const {
2673 return upb_fielddef_defaultdouble(this);
2674}
2675inline const char* FieldDef::default_string(size_t* len) const {
2676 return upb_fielddef_defaultstr(this, len);
2677}
2678inline void FieldDef::set_default_int64(int64_t value) {
2679 upb_fielddef_setdefaultint64(this, value);
2680}
2681inline void FieldDef::set_default_int32(int32_t value) {
2682 upb_fielddef_setdefaultint32(this, value);
2683}
2684inline void FieldDef::set_default_uint64(uint64_t value) {
2685 upb_fielddef_setdefaultuint64(this, value);
2686}
2687inline void FieldDef::set_default_uint32(uint32_t value) {
2688 upb_fielddef_setdefaultuint32(this, value);
2689}
2690inline void FieldDef::set_default_bool(bool value) {
2691 upb_fielddef_setdefaultbool(this, value);
2692}
2693inline void FieldDef::set_default_float(float value) {
2694 upb_fielddef_setdefaultfloat(this, value);
2695}
2696inline void FieldDef::set_default_double(double value) {
2697 upb_fielddef_setdefaultdouble(this, value);
2698}
2699inline bool FieldDef::set_default_string(const void *str, size_t len,
2700 Status *s) {
2701 return upb_fielddef_setdefaultstr(this, str, len, s);
2702}
2703inline bool FieldDef::set_default_string(const std::string& str, Status* s) {
2704 return upb_fielddef_setdefaultstr(this, str.c_str(), str.size(), s);
2705}
2706inline void FieldDef::set_default_cstr(const char* str, Status* s) {
2707 return upb_fielddef_setdefaultcstr(this, str, s);
2708}
2709inline bool FieldDef::HasSubDef() const { return upb_fielddef_hassubdef(this); }
2710inline const Def* FieldDef::subdef() const { return upb_fielddef_subdef(this); }
2711inline const MessageDef *FieldDef::message_subdef() const {
2712 return upb_fielddef_msgsubdef(this);
2713}
2714inline const EnumDef *FieldDef::enum_subdef() const {
2715 return upb_fielddef_enumsubdef(this);
2716}
2717inline const char* FieldDef::subdef_name() const {
2718 return upb_fielddef_subdefname(this);
2719}
2720inline bool FieldDef::set_subdef(const Def* subdef, Status* s) {
2721 return upb_fielddef_setsubdef(this, subdef, s);
2722}
2723inline bool FieldDef::set_enum_subdef(const EnumDef* subdef, Status* s) {
2724 return upb_fielddef_setenumsubdef(this, subdef, s);
2725}
2726inline bool FieldDef::set_message_subdef(const MessageDef* subdef, Status* s) {
2727 return upb_fielddef_setmsgsubdef(this, subdef, s);
2728}
2729inline bool FieldDef::set_subdef_name(const char* name, Status* s) {
2730 return upb_fielddef_setsubdefname(this, name, s);
2731}
2732inline bool FieldDef::set_subdef_name(const std::string& name, Status* s) {
2733 return upb_fielddef_setsubdefname(this, upb_safecstr(name), s);
2734}
2735
2736inline reffed_ptr<MessageDef> MessageDef::New() {
2737 upb_msgdef *m = upb_msgdef_new(&m);
2738 return reffed_ptr<MessageDef>(m, &m);
2739}
Chris Fallin91473dc2014-12-12 15:58:26 -08002740inline const char *MessageDef::full_name() const {
2741 return upb_msgdef_fullname(this);
2742}
2743inline bool MessageDef::set_full_name(const char* fullname, Status* s) {
2744 return upb_msgdef_setfullname(this, fullname, s);
2745}
2746inline bool MessageDef::set_full_name(const std::string& fullname, Status* s) {
2747 return upb_msgdef_setfullname(this, upb_safecstr(fullname), s);
2748}
2749inline bool MessageDef::Freeze(Status* status) {
2750 return upb_msgdef_freeze(this, status);
2751}
2752inline int MessageDef::field_count() const {
2753 return upb_msgdef_numfields(this);
2754}
Chris Fallinfcd88892015-01-13 18:14:39 -08002755inline int MessageDef::oneof_count() const {
2756 return upb_msgdef_numoneofs(this);
2757}
Chris Fallin91473dc2014-12-12 15:58:26 -08002758inline bool MessageDef::AddField(upb_fielddef* f, Status* s) {
2759 return upb_msgdef_addfield(this, f, NULL, s);
2760}
2761inline bool MessageDef::AddField(const reffed_ptr<FieldDef>& f, Status* s) {
2762 return upb_msgdef_addfield(this, f.get(), NULL, s);
2763}
Chris Fallinfcd88892015-01-13 18:14:39 -08002764inline bool MessageDef::AddOneof(upb_oneofdef* o, Status* s) {
2765 return upb_msgdef_addoneof(this, o, NULL, s);
2766}
2767inline bool MessageDef::AddOneof(const reffed_ptr<OneofDef>& o, Status* s) {
2768 return upb_msgdef_addoneof(this, o.get(), NULL, s);
2769}
Chris Fallin91473dc2014-12-12 15:58:26 -08002770inline FieldDef* MessageDef::FindFieldByNumber(uint32_t number) {
2771 return upb_msgdef_itof_mutable(this, number);
2772}
2773inline FieldDef* MessageDef::FindFieldByName(const char* name, size_t len) {
2774 return upb_msgdef_ntof_mutable(this, name, len);
2775}
2776inline const FieldDef* MessageDef::FindFieldByNumber(uint32_t number) const {
2777 return upb_msgdef_itof(this, number);
2778}
2779inline const FieldDef *MessageDef::FindFieldByName(const char *name,
2780 size_t len) const {
2781 return upb_msgdef_ntof(this, name, len);
2782}
Chris Fallinfcd88892015-01-13 18:14:39 -08002783inline OneofDef* MessageDef::FindOneofByName(const char* name, size_t len) {
2784 return upb_msgdef_ntoo_mutable(this, name, len);
2785}
2786inline const OneofDef* MessageDef::FindOneofByName(const char* name,
2787 size_t len) const {
2788 return upb_msgdef_ntoo(this, name, len);
2789}
Chris Fallin91473dc2014-12-12 15:58:26 -08002790inline MessageDef* MessageDef::Dup(const void *owner) const {
2791 return upb_msgdef_dup(this, owner);
2792}
Chris Fallinfd1a3ff2015-01-06 15:44:09 -08002793inline void MessageDef::setmapentry(bool map_entry) {
2794 upb_msgdef_setmapentry(this, map_entry);
2795}
2796inline bool MessageDef::mapentry() const {
2797 return upb_msgdef_mapentry(this);
2798}
Chris Fallinfcd88892015-01-13 18:14:39 -08002799inline MessageDef::field_iterator MessageDef::field_begin() {
2800 return field_iterator(this);
Chris Fallin91473dc2014-12-12 15:58:26 -08002801}
Chris Fallinfcd88892015-01-13 18:14:39 -08002802inline MessageDef::field_iterator MessageDef::field_end() {
2803 return field_iterator::end(this);
2804}
2805inline MessageDef::const_field_iterator MessageDef::field_begin() const {
2806 return const_field_iterator(this);
2807}
2808inline MessageDef::const_field_iterator MessageDef::field_end() const {
2809 return const_field_iterator::end(this);
Chris Fallin91473dc2014-12-12 15:58:26 -08002810}
2811
Chris Fallinfcd88892015-01-13 18:14:39 -08002812inline MessageDef::oneof_iterator MessageDef::oneof_begin() {
2813 return oneof_iterator(this);
Chris Fallin91473dc2014-12-12 15:58:26 -08002814}
Chris Fallinfcd88892015-01-13 18:14:39 -08002815inline MessageDef::oneof_iterator MessageDef::oneof_end() {
2816 return oneof_iterator::end(this);
2817}
2818inline MessageDef::const_oneof_iterator MessageDef::oneof_begin() const {
2819 return const_oneof_iterator(this);
2820}
2821inline MessageDef::const_oneof_iterator MessageDef::oneof_end() const {
2822 return const_oneof_iterator::end(this);
2823}
2824
2825inline MessageDef::field_iterator::field_iterator(MessageDef* md) {
2826 upb_msg_field_begin(&iter_, md);
2827}
2828inline MessageDef::field_iterator MessageDef::field_iterator::end(
2829 MessageDef* md) {
2830 MessageDef::field_iterator iter(md);
2831 upb_msg_field_iter_setdone(&iter.iter_);
Chris Fallin91473dc2014-12-12 15:58:26 -08002832 return iter;
2833}
Chris Fallinfcd88892015-01-13 18:14:39 -08002834inline FieldDef* MessageDef::field_iterator::operator*() const {
Chris Fallin91473dc2014-12-12 15:58:26 -08002835 return upb_msg_iter_field(&iter_);
2836}
Chris Fallinfcd88892015-01-13 18:14:39 -08002837inline void MessageDef::field_iterator::operator++() {
2838 return upb_msg_field_next(&iter_);
2839}
2840inline bool MessageDef::field_iterator::operator==(
2841 const field_iterator &other) const {
Chris Fallin91473dc2014-12-12 15:58:26 -08002842 return upb_inttable_iter_isequal(&iter_, &other.iter_);
2843}
Chris Fallinfcd88892015-01-13 18:14:39 -08002844inline bool MessageDef::field_iterator::operator!=(
2845 const field_iterator &other) const {
Chris Fallin91473dc2014-12-12 15:58:26 -08002846 return !(*this == other);
2847}
2848
Chris Fallinfcd88892015-01-13 18:14:39 -08002849inline MessageDef::const_field_iterator::const_field_iterator(
2850 const MessageDef* md) {
2851 upb_msg_field_begin(&iter_, md);
Chris Fallin91473dc2014-12-12 15:58:26 -08002852}
Chris Fallinfcd88892015-01-13 18:14:39 -08002853inline MessageDef::const_field_iterator MessageDef::const_field_iterator::end(
Chris Fallin91473dc2014-12-12 15:58:26 -08002854 const MessageDef *md) {
Chris Fallinfcd88892015-01-13 18:14:39 -08002855 MessageDef::const_field_iterator iter(md);
2856 upb_msg_field_iter_setdone(&iter.iter_);
Chris Fallin91473dc2014-12-12 15:58:26 -08002857 return iter;
2858}
Chris Fallinfcd88892015-01-13 18:14:39 -08002859inline const FieldDef* MessageDef::const_field_iterator::operator*() const {
Chris Fallin91473dc2014-12-12 15:58:26 -08002860 return upb_msg_iter_field(&iter_);
2861}
Chris Fallinfcd88892015-01-13 18:14:39 -08002862inline void MessageDef::const_field_iterator::operator++() {
2863 return upb_msg_field_next(&iter_);
Chris Fallin91473dc2014-12-12 15:58:26 -08002864}
Chris Fallinfcd88892015-01-13 18:14:39 -08002865inline bool MessageDef::const_field_iterator::operator==(
2866 const const_field_iterator &other) const {
Chris Fallin91473dc2014-12-12 15:58:26 -08002867 return upb_inttable_iter_isequal(&iter_, &other.iter_);
2868}
Chris Fallinfcd88892015-01-13 18:14:39 -08002869inline bool MessageDef::const_field_iterator::operator!=(
2870 const const_field_iterator &other) const {
2871 return !(*this == other);
2872}
2873
2874inline MessageDef::oneof_iterator::oneof_iterator(MessageDef* md) {
2875 upb_msg_oneof_begin(&iter_, md);
2876}
2877inline MessageDef::oneof_iterator MessageDef::oneof_iterator::end(
2878 MessageDef* md) {
2879 MessageDef::oneof_iterator iter(md);
2880 upb_msg_oneof_iter_setdone(&iter.iter_);
2881 return iter;
2882}
2883inline OneofDef* MessageDef::oneof_iterator::operator*() const {
2884 return upb_msg_iter_oneof(&iter_);
2885}
2886inline void MessageDef::oneof_iterator::operator++() {
2887 return upb_msg_oneof_next(&iter_);
2888}
2889inline bool MessageDef::oneof_iterator::operator==(
2890 const oneof_iterator &other) const {
2891 return upb_strtable_iter_isequal(&iter_, &other.iter_);
2892}
2893inline bool MessageDef::oneof_iterator::operator!=(
2894 const oneof_iterator &other) const {
2895 return !(*this == other);
2896}
2897
2898inline MessageDef::const_oneof_iterator::const_oneof_iterator(
2899 const MessageDef* md) {
2900 upb_msg_oneof_begin(&iter_, md);
2901}
2902inline MessageDef::const_oneof_iterator MessageDef::const_oneof_iterator::end(
2903 const MessageDef *md) {
2904 MessageDef::const_oneof_iterator iter(md);
2905 upb_msg_oneof_iter_setdone(&iter.iter_);
2906 return iter;
2907}
2908inline const OneofDef* MessageDef::const_oneof_iterator::operator*() const {
2909 return upb_msg_iter_oneof(&iter_);
2910}
2911inline void MessageDef::const_oneof_iterator::operator++() {
2912 return upb_msg_oneof_next(&iter_);
2913}
2914inline bool MessageDef::const_oneof_iterator::operator==(
2915 const const_oneof_iterator &other) const {
2916 return upb_strtable_iter_isequal(&iter_, &other.iter_);
2917}
2918inline bool MessageDef::const_oneof_iterator::operator!=(
2919 const const_oneof_iterator &other) const {
Chris Fallin91473dc2014-12-12 15:58:26 -08002920 return !(*this == other);
2921}
2922
2923inline reffed_ptr<EnumDef> EnumDef::New() {
2924 upb_enumdef *e = upb_enumdef_new(&e);
2925 return reffed_ptr<EnumDef>(e, &e);
2926}
Chris Fallin91473dc2014-12-12 15:58:26 -08002927inline const char* EnumDef::full_name() const {
2928 return upb_enumdef_fullname(this);
2929}
2930inline bool EnumDef::set_full_name(const char* fullname, Status* s) {
2931 return upb_enumdef_setfullname(this, fullname, s);
2932}
2933inline bool EnumDef::set_full_name(const std::string& fullname, Status* s) {
2934 return upb_enumdef_setfullname(this, upb_safecstr(fullname), s);
2935}
2936inline bool EnumDef::Freeze(Status* status) {
2937 return upb_enumdef_freeze(this, status);
2938}
2939inline int32_t EnumDef::default_value() const {
2940 return upb_enumdef_default(this);
2941}
2942inline bool EnumDef::set_default_value(int32_t val, Status* status) {
2943 return upb_enumdef_setdefault(this, val, status);
2944}
2945inline int EnumDef::value_count() const { return upb_enumdef_numvals(this); }
2946inline bool EnumDef::AddValue(const char* name, int32_t num, Status* status) {
2947 return upb_enumdef_addval(this, name, num, status);
2948}
2949inline bool EnumDef::AddValue(const std::string& name, int32_t num,
2950 Status* status) {
2951 return upb_enumdef_addval(this, upb_safecstr(name), num, status);
2952}
2953inline bool EnumDef::FindValueByName(const char* name, int32_t *num) const {
2954 return upb_enumdef_ntoiz(this, name, num);
2955}
2956inline const char* EnumDef::FindValueByNumber(int32_t num) const {
2957 return upb_enumdef_iton(this, num);
2958}
2959inline EnumDef* EnumDef::Dup(const void* owner) const {
2960 return upb_enumdef_dup(this, owner);
2961}
2962
2963inline EnumDef::Iterator::Iterator(const EnumDef* e) {
2964 upb_enum_begin(&iter_, e);
2965}
2966inline int32_t EnumDef::Iterator::number() {
2967 return upb_enum_iter_number(&iter_);
2968}
2969inline const char* EnumDef::Iterator::name() {
2970 return upb_enum_iter_name(&iter_);
2971}
2972inline bool EnumDef::Iterator::Done() { return upb_enum_done(&iter_); }
2973inline void EnumDef::Iterator::Next() { return upb_enum_next(&iter_); }
Chris Fallinfcd88892015-01-13 18:14:39 -08002974
2975inline reffed_ptr<OneofDef> OneofDef::New() {
2976 upb_oneofdef *o = upb_oneofdef_new(&o);
2977 return reffed_ptr<OneofDef>(o, &o);
2978}
Chris Fallinfcd88892015-01-13 18:14:39 -08002979inline const char* OneofDef::full_name() const {
2980 return upb_oneofdef_name(this);
2981}
2982
2983inline const MessageDef* OneofDef::containing_type() const {
2984 return upb_oneofdef_containingtype(this);
2985}
2986inline const char* OneofDef::name() const {
2987 return upb_oneofdef_name(this);
2988}
2989inline bool OneofDef::set_name(const char* name, Status* s) {
2990 return upb_oneofdef_setname(this, name, s);
2991}
2992inline int OneofDef::field_count() const {
2993 return upb_oneofdef_numfields(this);
2994}
2995inline bool OneofDef::AddField(FieldDef* field, Status* s) {
2996 return upb_oneofdef_addfield(this, field, NULL, s);
2997}
2998inline bool OneofDef::AddField(const reffed_ptr<FieldDef>& field, Status* s) {
2999 return upb_oneofdef_addfield(this, field.get(), NULL, s);
3000}
3001inline const FieldDef* OneofDef::FindFieldByName(const char* name,
3002 size_t len) const {
3003 return upb_oneofdef_ntof(this, name, len);
3004}
3005inline const FieldDef* OneofDef::FindFieldByNumber(uint32_t num) const {
3006 return upb_oneofdef_itof(this, num);
3007}
3008inline OneofDef::iterator OneofDef::begin() { return iterator(this); }
3009inline OneofDef::iterator OneofDef::end() { return iterator::end(this); }
3010inline OneofDef::const_iterator OneofDef::begin() const {
3011 return const_iterator(this);
3012}
3013inline OneofDef::const_iterator OneofDef::end() const {
3014 return const_iterator::end(this);
3015}
3016
3017inline OneofDef::iterator::iterator(OneofDef* o) {
3018 upb_oneof_begin(&iter_, o);
3019}
3020inline OneofDef::iterator OneofDef::iterator::end(OneofDef* o) {
3021 OneofDef::iterator iter(o);
3022 upb_oneof_iter_setdone(&iter.iter_);
3023 return iter;
3024}
3025inline FieldDef* OneofDef::iterator::operator*() const {
3026 return upb_oneof_iter_field(&iter_);
3027}
3028inline void OneofDef::iterator::operator++() { return upb_oneof_next(&iter_); }
3029inline bool OneofDef::iterator::operator==(const iterator &other) const {
3030 return upb_inttable_iter_isequal(&iter_, &other.iter_);
3031}
3032inline bool OneofDef::iterator::operator!=(const iterator &other) const {
3033 return !(*this == other);
3034}
3035
3036inline OneofDef::const_iterator::const_iterator(const OneofDef* md) {
3037 upb_oneof_begin(&iter_, md);
3038}
3039inline OneofDef::const_iterator OneofDef::const_iterator::end(
3040 const OneofDef *md) {
3041 OneofDef::const_iterator iter(md);
3042 upb_oneof_iter_setdone(&iter.iter_);
3043 return iter;
3044}
3045inline const FieldDef* OneofDef::const_iterator::operator*() const {
3046 return upb_msg_iter_field(&iter_);
3047}
3048inline void OneofDef::const_iterator::operator++() {
3049 return upb_oneof_next(&iter_);
3050}
3051inline bool OneofDef::const_iterator::operator==(
3052 const const_iterator &other) const {
3053 return upb_inttable_iter_isequal(&iter_, &other.iter_);
3054}
3055inline bool OneofDef::const_iterator::operator!=(
3056 const const_iterator &other) const {
3057 return !(*this == other);
3058}
3059
Josh Habermane8ed0212015-06-08 17:56:03 -07003060} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08003061#endif
3062
Chris Fallin91473dc2014-12-12 15:58:26 -08003063#endif /* UPB_DEF_H_ */
Chris Fallin91473dc2014-12-12 15:58:26 -08003064/*
Josh Haberman181c7f22015-07-15 11:05:10 -07003065** This file contains definitions of structs that should be considered private
3066** and NOT stable across versions of upb.
3067**
3068** The only reason they are declared here and not in .c files is to allow upb
3069** and the application (if desired) to embed statically-initialized instances
3070** of structures like defs.
3071**
3072** If you include this file, all guarantees of ABI compatibility go out the
3073** window! Any code that includes this file needs to recompile against the
3074** exact same version of upb that they are linking against.
3075**
3076** You also need to recompile if you change the value of the UPB_DEBUG_REFS
3077** flag.
3078*/
Chris Fallin91473dc2014-12-12 15:58:26 -08003079
Chris Fallin91473dc2014-12-12 15:58:26 -08003080
Josh Habermane8ed0212015-06-08 17:56:03 -07003081#ifndef UPB_STATICINIT_H_
3082#define UPB_STATICINIT_H_
Chris Fallin91473dc2014-12-12 15:58:26 -08003083
3084#ifdef __cplusplus
Josh Habermane8ed0212015-06-08 17:56:03 -07003085/* Because of how we do our typedefs, this header can't be included from C++. */
3086#error This file cannot be included from C++
Chris Fallin91473dc2014-12-12 15:58:26 -08003087#endif
3088
Josh Habermane8ed0212015-06-08 17:56:03 -07003089/* upb_refcounted *************************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -08003090
Chris Fallin91473dc2014-12-12 15:58:26 -08003091
Josh Habermane8ed0212015-06-08 17:56:03 -07003092/* upb_def ********************************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -08003093
Josh Habermane8ed0212015-06-08 17:56:03 -07003094struct upb_def {
3095 upb_refcounted base;
Chris Fallin91473dc2014-12-12 15:58:26 -08003096
Josh Habermane8ed0212015-06-08 17:56:03 -07003097 const char *fullname;
3098 char type; /* A upb_deftype_t (char to save space) */
Chris Fallin91473dc2014-12-12 15:58:26 -08003099
Josh Habermane8ed0212015-06-08 17:56:03 -07003100 /* Used as a flag during the def's mutable stage. Must be false unless
3101 * it is currently being used by a function on the stack. This allows
3102 * us to easily determine which defs were passed into the function's
3103 * current invocation. */
3104 bool came_from_user;
3105};
Chris Fallin91473dc2014-12-12 15:58:26 -08003106
Josh Habermane8ed0212015-06-08 17:56:03 -07003107#define UPB_DEF_INIT(name, type, refs, ref2s) \
3108 { UPB_REFCOUNT_INIT(refs, ref2s), name, type, false }
Chris Fallin91473dc2014-12-12 15:58:26 -08003109
Chris Fallin91473dc2014-12-12 15:58:26 -08003110
Josh Habermane8ed0212015-06-08 17:56:03 -07003111/* upb_fielddef ***************************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -08003112
Josh Habermane8ed0212015-06-08 17:56:03 -07003113struct upb_fielddef {
3114 upb_def base;
Chris Fallin91473dc2014-12-12 15:58:26 -08003115
Josh Habermane8ed0212015-06-08 17:56:03 -07003116 union {
3117 int64_t sint;
3118 uint64_t uint;
3119 double dbl;
3120 float flt;
3121 void *bytes;
3122 } defaultval;
3123 union {
3124 const upb_msgdef *def; /* If !msg_is_symbolic. */
3125 char *name; /* If msg_is_symbolic. */
3126 } msg;
3127 union {
3128 const upb_def *def; /* If !subdef_is_symbolic. */
3129 char *name; /* If subdef_is_symbolic. */
3130 } sub; /* The msgdef or enumdef for this field, if upb_hassubdef(f). */
3131 bool subdef_is_symbolic;
3132 bool msg_is_symbolic;
3133 const upb_oneofdef *oneof;
3134 bool default_is_string;
3135 bool type_is_set_; /* False until type is explicitly set. */
3136 bool is_extension_;
3137 bool lazy_;
3138 bool packed_;
3139 upb_intfmt_t intfmt;
3140 bool tagdelim;
3141 upb_fieldtype_t type_;
3142 upb_label_t label_;
3143 uint32_t number_;
3144 uint32_t selector_base; /* Used to index into a upb::Handlers table. */
3145 uint32_t index_;
3146};
3147
3148#define UPB_FIELDDEF_INIT(label, type, intfmt, tagdelim, is_extension, lazy, \
3149 packed, name, num, msgdef, subdef, selector_base, \
3150 index, defaultval, refs, ref2s) \
3151 { \
3152 UPB_DEF_INIT(name, UPB_DEF_FIELD, refs, ref2s), defaultval, {msgdef}, \
3153 {subdef}, NULL, false, false, \
3154 type == UPB_TYPE_STRING || type == UPB_TYPE_BYTES, true, is_extension, \
3155 lazy, packed, intfmt, tagdelim, type, label, num, selector_base, index \
Chris Fallin91473dc2014-12-12 15:58:26 -08003156 }
3157
Josh Habermane8ed0212015-06-08 17:56:03 -07003158
3159/* upb_msgdef *****************************************************************/
3160
3161struct upb_msgdef {
3162 upb_def base;
3163
3164 size_t selector_count;
3165 uint32_t submsg_field_count;
3166
3167 /* Tables for looking up fields by number and name. */
3168 upb_inttable itof; /* int to field */
3169 upb_strtable ntof; /* name to field */
3170
3171 /* Tables for looking up oneofs by name. */
3172 upb_strtable ntoo; /* name to oneof */
3173
3174 /* Is this a map-entry message?
3175 * TODO: set this flag properly for static descriptors; regenerate
3176 * descriptor.upb.c. */
3177 bool map_entry;
3178
Josh Haberman78da6662016-01-13 19:05:43 -08003179 /* Do primitive values in this message have explicit presence or not?
3180 * TODO: set this flag properly for static descriptors; regenerate
3181 * descriptor.upb.c. */
3182 bool primitives_have_presence;
3183
Josh Habermane8ed0212015-06-08 17:56:03 -07003184 /* TODO(haberman): proper extension ranges (there can be multiple). */
3185};
3186
3187/* TODO: also support static initialization of the oneofs table. This will be
3188 * needed if we compile in descriptors that contain oneofs. */
3189#define UPB_MSGDEF_INIT(name, selector_count, submsg_field_count, itof, ntof, \
3190 refs, ref2s) \
3191 { \
3192 UPB_DEF_INIT(name, UPB_DEF_MSG, refs, ref2s), selector_count, \
3193 submsg_field_count, itof, ntof, \
Josh Haberman78da6662016-01-13 19:05:43 -08003194 UPB_EMPTY_STRTABLE_INIT(UPB_CTYPE_PTR), false, true \
Josh Habermane8ed0212015-06-08 17:56:03 -07003195 }
3196
3197
3198/* upb_enumdef ****************************************************************/
3199
3200struct upb_enumdef {
3201 upb_def base;
3202
3203 upb_strtable ntoi;
3204 upb_inttable iton;
3205 int32_t defaultval;
3206};
3207
3208#define UPB_ENUMDEF_INIT(name, ntoi, iton, defaultval, refs, ref2s) \
3209 { UPB_DEF_INIT(name, UPB_DEF_ENUM, refs, ref2s), ntoi, iton, defaultval }
3210
3211
3212/* upb_oneofdef ***************************************************************/
3213
3214struct upb_oneofdef {
3215 upb_def base;
3216
3217 upb_strtable ntof;
3218 upb_inttable itof;
3219 const upb_msgdef *parent;
3220};
3221
3222#define UPB_ONEOFDEF_INIT(name, ntof, itof, refs, ref2s) \
3223 { UPB_DEF_INIT(name, UPB_DEF_ENUM, refs, ref2s), ntof, itof }
3224
3225
3226/* upb_symtab *****************************************************************/
3227
3228struct upb_symtab {
3229 upb_refcounted base;
3230
Chris Fallin91473dc2014-12-12 15:58:26 -08003231 upb_strtable symtab;
Josh Habermane8ed0212015-06-08 17:56:03 -07003232};
Chris Fallin91473dc2014-12-12 15:58:26 -08003233
3234#define UPB_SYMTAB_INIT(symtab, refs, ref2s) \
3235 { UPB_REFCOUNT_INIT(refs, ref2s), symtab }
3236
Chris Fallin91473dc2014-12-12 15:58:26 -08003237
Josh Habermane8ed0212015-06-08 17:56:03 -07003238#endif /* UPB_STATICINIT_H_ */
Chris Fallin91473dc2014-12-12 15:58:26 -08003239/*
Josh Haberman181c7f22015-07-15 11:05:10 -07003240** upb::Handlers (upb_handlers)
3241**
3242** A upb_handlers is like a virtual table for a upb_msgdef. Each field of the
3243** message can have associated functions that will be called when we are
3244** parsing or visiting a stream of data. This is similar to how handlers work
3245** in SAX (the Simple API for XML).
3246**
3247** The handlers have no idea where the data is coming from, so a single set of
3248** handlers could be used with two completely different data sources (for
3249** example, a parser and a visitor over in-memory objects). This decoupling is
3250** the most important feature of upb, because it allows parsers and serializers
3251** to be highly reusable.
3252**
3253** This is a mixed C/C++ interface that offers a full API to both languages.
3254** See the top-level README for more information.
3255*/
Chris Fallin91473dc2014-12-12 15:58:26 -08003256
3257#ifndef UPB_HANDLERS_H
3258#define UPB_HANDLERS_H
3259
3260
3261#ifdef __cplusplus
3262namespace upb {
3263class BufferHandle;
3264class BytesHandler;
3265class HandlerAttributes;
3266class Handlers;
3267template <class T> class Handler;
3268template <class T> struct CanonicalType;
Josh Habermane8ed0212015-06-08 17:56:03 -07003269} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08003270#endif
3271
Josh Habermane8ed0212015-06-08 17:56:03 -07003272UPB_DECLARE_TYPE(upb::BufferHandle, upb_bufhandle)
3273UPB_DECLARE_TYPE(upb::BytesHandler, upb_byteshandler)
3274UPB_DECLARE_TYPE(upb::HandlerAttributes, upb_handlerattr)
3275UPB_DECLARE_DERIVED_TYPE(upb::Handlers, upb::RefCounted,
3276 upb_handlers, upb_refcounted)
Chris Fallin91473dc2014-12-12 15:58:26 -08003277
Josh Habermane8ed0212015-06-08 17:56:03 -07003278/* The maximum depth that the handler graph can have. This is a resource limit
3279 * for the C stack since we sometimes need to recursively traverse the graph.
3280 * Cycles are ok; the traversal will stop when it detects a cycle, but we must
3281 * hit the cycle before the maximum depth is reached.
3282 *
3283 * If having a single static limit is too inflexible, we can add another variant
3284 * of Handlers::Freeze that allows specifying this as a parameter. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003285#define UPB_MAX_HANDLER_DEPTH 64
3286
Josh Habermane8ed0212015-06-08 17:56:03 -07003287/* All the different types of handlers that can be registered.
3288 * Only needed for the advanced functions in upb::Handlers. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003289typedef enum {
3290 UPB_HANDLER_INT32,
3291 UPB_HANDLER_INT64,
3292 UPB_HANDLER_UINT32,
3293 UPB_HANDLER_UINT64,
3294 UPB_HANDLER_FLOAT,
3295 UPB_HANDLER_DOUBLE,
3296 UPB_HANDLER_BOOL,
3297 UPB_HANDLER_STARTSTR,
3298 UPB_HANDLER_STRING,
3299 UPB_HANDLER_ENDSTR,
3300 UPB_HANDLER_STARTSUBMSG,
3301 UPB_HANDLER_ENDSUBMSG,
3302 UPB_HANDLER_STARTSEQ,
Josh Habermane8ed0212015-06-08 17:56:03 -07003303 UPB_HANDLER_ENDSEQ
Chris Fallin91473dc2014-12-12 15:58:26 -08003304} upb_handlertype_t;
3305
3306#define UPB_HANDLER_MAX (UPB_HANDLER_ENDSEQ+1)
3307
3308#define UPB_BREAK NULL
3309
Josh Habermane8ed0212015-06-08 17:56:03 -07003310/* A convenient definition for when no closure is needed. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003311extern char _upb_noclosure;
3312#define UPB_NO_CLOSURE &_upb_noclosure
3313
Josh Habermane8ed0212015-06-08 17:56:03 -07003314/* A selector refers to a specific field handler in the Handlers object
3315 * (for example: the STARTSUBMSG handler for field "field15"). */
Chris Fallin91473dc2014-12-12 15:58:26 -08003316typedef int32_t upb_selector_t;
3317
3318UPB_BEGIN_EXTERN_C
3319
Josh Habermane8ed0212015-06-08 17:56:03 -07003320/* Forward-declares for C inline accessors. We need to declare these here
3321 * so we can "friend" them in the class declarations in C++. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003322UPB_INLINE upb_func *upb_handlers_gethandler(const upb_handlers *h,
3323 upb_selector_t s);
3324UPB_INLINE const void *upb_handlerattr_handlerdata(const upb_handlerattr *attr);
3325UPB_INLINE const void *upb_handlers_gethandlerdata(const upb_handlers *h,
3326 upb_selector_t s);
3327
3328UPB_INLINE void upb_bufhandle_init(upb_bufhandle *h);
3329UPB_INLINE void upb_bufhandle_setobj(upb_bufhandle *h, const void *obj,
3330 const void *type);
3331UPB_INLINE void upb_bufhandle_setbuf(upb_bufhandle *h, const char *buf,
3332 size_t ofs);
3333UPB_INLINE const void *upb_bufhandle_obj(const upb_bufhandle *h);
3334UPB_INLINE const void *upb_bufhandle_objtype(const upb_bufhandle *h);
3335UPB_INLINE const char *upb_bufhandle_buf(const upb_bufhandle *h);
3336
3337UPB_END_EXTERN_C
3338
3339
Josh Habermane8ed0212015-06-08 17:56:03 -07003340/* Static selectors for upb::Handlers. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003341#define UPB_STARTMSG_SELECTOR 0
3342#define UPB_ENDMSG_SELECTOR 1
3343#define UPB_STATIC_SELECTOR_COUNT 2
3344
Josh Habermane8ed0212015-06-08 17:56:03 -07003345/* Static selectors for upb::BytesHandler. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003346#define UPB_STARTSTR_SELECTOR 0
3347#define UPB_STRING_SELECTOR 1
3348#define UPB_ENDSTR_SELECTOR 2
3349
3350typedef void upb_handlerfree(void *d);
3351
Josh Habermane8ed0212015-06-08 17:56:03 -07003352#ifdef __cplusplus
3353
3354/* A set of attributes that accompanies a handler's function pointer. */
3355class upb::HandlerAttributes {
Chris Fallin91473dc2014-12-12 15:58:26 -08003356 public:
3357 HandlerAttributes();
3358 ~HandlerAttributes();
3359
Josh Habermane8ed0212015-06-08 17:56:03 -07003360 /* Sets the handler data that will be passed as the second parameter of the
3361 * handler. To free this pointer when the handlers are freed, call
3362 * Handlers::AddCleanup(). */
Chris Fallin91473dc2014-12-12 15:58:26 -08003363 bool SetHandlerData(const void *handler_data);
3364 const void* handler_data() const;
3365
Josh Habermane8ed0212015-06-08 17:56:03 -07003366 /* Use this to specify the type of the closure. This will be checked against
3367 * all other closure types for handler that use the same closure.
3368 * Registration will fail if this does not match all other non-NULL closure
3369 * types. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003370 bool SetClosureType(const void *closure_type);
3371 const void* closure_type() const;
3372
Josh Habermane8ed0212015-06-08 17:56:03 -07003373 /* Use this to specify the type of the returned closure. Only used for
3374 * Start*{String,SubMessage,Sequence} handlers. This must match the closure
3375 * type of any handlers that use it (for example, the StringBuf handler must
3376 * match the closure returned from StartString). */
Chris Fallin91473dc2014-12-12 15:58:26 -08003377 bool SetReturnClosureType(const void *return_closure_type);
3378 const void* return_closure_type() const;
3379
Josh Habermane8ed0212015-06-08 17:56:03 -07003380 /* Set to indicate that the handler always returns "ok" (either "true" or a
3381 * non-NULL closure). This is a hint that can allow code generators to
3382 * generate more efficient code. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003383 bool SetAlwaysOk(bool always_ok);
3384 bool always_ok() const;
3385
3386 private:
3387 friend UPB_INLINE const void * ::upb_handlerattr_handlerdata(
3388 const upb_handlerattr *attr);
Josh Habermane8ed0212015-06-08 17:56:03 -07003389#else
3390struct upb_handlerattr {
3391#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08003392 const void *handler_data_;
3393 const void *closure_type_;
3394 const void *return_closure_type_;
3395 bool alwaysok_;
Josh Habermane8ed0212015-06-08 17:56:03 -07003396};
Chris Fallin91473dc2014-12-12 15:58:26 -08003397
3398#define UPB_HANDLERATTR_INITIALIZER {NULL, NULL, NULL, false}
3399
3400typedef struct {
3401 upb_func *func;
Josh Habermane8ed0212015-06-08 17:56:03 -07003402
3403 /* It is wasteful to include the entire attributes here:
3404 *
3405 * * Some of the information is redundant (like storing the closure type
3406 * separately for each handler that must match).
3407 * * Some of the info is only needed prior to freeze() (like closure types).
3408 * * alignment padding wastes a lot of space for alwaysok_.
3409 *
3410 * If/when the size and locality of handlers is an issue, we can optimize this
3411 * not to store the entire attr like this. We do not expose the table's
3412 * layout to allow this optimization in the future. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003413 upb_handlerattr attr;
3414} upb_handlers_tabent;
3415
Josh Habermane8ed0212015-06-08 17:56:03 -07003416#ifdef __cplusplus
3417
3418/* Extra information about a buffer that is passed to a StringBuf handler.
3419 * TODO(haberman): allow the handle to be pinned so that it will outlive
3420 * the handler invocation. */
3421class upb::BufferHandle {
Chris Fallin91473dc2014-12-12 15:58:26 -08003422 public:
3423 BufferHandle();
3424 ~BufferHandle();
3425
Josh Habermane8ed0212015-06-08 17:56:03 -07003426 /* The beginning of the buffer. This may be different than the pointer
3427 * passed to a StringBuf handler because the handler may receive data
3428 * that is from the middle or end of a larger buffer. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003429 const char* buffer() const;
3430
Josh Habermane8ed0212015-06-08 17:56:03 -07003431 /* The offset within the attached object where this buffer begins. Only
3432 * meaningful if there is an attached object. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003433 size_t object_offset() const;
3434
Josh Habermane8ed0212015-06-08 17:56:03 -07003435 /* Note that object_offset is the offset of "buf" within the attached
3436 * object. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003437 void SetBuffer(const char* buf, size_t object_offset);
3438
Josh Habermane8ed0212015-06-08 17:56:03 -07003439 /* The BufferHandle can have an "attached object", which can be used to
3440 * tunnel through a pointer to the buffer's underlying representation. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003441 template <class T>
3442 void SetAttachedObject(const T* obj);
3443
Josh Habermane8ed0212015-06-08 17:56:03 -07003444 /* Returns NULL if the attached object is not of this type. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003445 template <class T>
3446 const T* GetAttachedObject() const;
3447
3448 private:
3449 friend UPB_INLINE void ::upb_bufhandle_init(upb_bufhandle *h);
3450 friend UPB_INLINE void ::upb_bufhandle_setobj(upb_bufhandle *h,
3451 const void *obj,
3452 const void *type);
3453 friend UPB_INLINE void ::upb_bufhandle_setbuf(upb_bufhandle *h,
3454 const char *buf, size_t ofs);
3455 friend UPB_INLINE const void* ::upb_bufhandle_obj(const upb_bufhandle *h);
3456 friend UPB_INLINE const void* ::upb_bufhandle_objtype(
3457 const upb_bufhandle *h);
3458 friend UPB_INLINE const char* ::upb_bufhandle_buf(const upb_bufhandle *h);
Josh Habermane8ed0212015-06-08 17:56:03 -07003459#else
3460struct upb_bufhandle {
3461#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08003462 const char *buf_;
3463 const void *obj_;
3464 const void *objtype_;
3465 size_t objofs_;
Josh Habermane8ed0212015-06-08 17:56:03 -07003466};
Chris Fallin91473dc2014-12-12 15:58:26 -08003467
Josh Habermane8ed0212015-06-08 17:56:03 -07003468#ifdef __cplusplus
3469
3470/* A upb::Handlers object represents the set of handlers associated with a
3471 * message in the graph of messages. You can think of it as a big virtual
3472 * table with functions corresponding to all the events that can fire while
3473 * parsing or visiting a message of a specific type.
3474 *
3475 * Any handlers that are not set behave as if they had successfully consumed
3476 * the value. Any unset Start* handlers will propagate their closure to the
3477 * inner frame.
3478 *
3479 * The easiest way to create the *Handler objects needed by the Set* methods is
3480 * with the UpbBind() and UpbMakeHandler() macros; see below. */
3481class upb::Handlers {
Chris Fallin91473dc2014-12-12 15:58:26 -08003482 public:
3483 typedef upb_selector_t Selector;
3484 typedef upb_handlertype_t Type;
3485
3486 typedef Handler<void *(*)(void *, const void *)> StartFieldHandler;
3487 typedef Handler<bool (*)(void *, const void *)> EndFieldHandler;
3488 typedef Handler<bool (*)(void *, const void *)> StartMessageHandler;
3489 typedef Handler<bool (*)(void *, const void *, Status*)> EndMessageHandler;
3490 typedef Handler<void *(*)(void *, const void *, size_t)> StartStringHandler;
3491 typedef Handler<size_t (*)(void *, const void *, const char *, size_t,
3492 const BufferHandle *)> StringHandler;
3493
3494 template <class T> struct ValueHandler {
3495 typedef Handler<bool(*)(void *, const void *, T)> H;
3496 };
3497
3498 typedef ValueHandler<int32_t>::H Int32Handler;
3499 typedef ValueHandler<int64_t>::H Int64Handler;
3500 typedef ValueHandler<uint32_t>::H UInt32Handler;
3501 typedef ValueHandler<uint64_t>::H UInt64Handler;
3502 typedef ValueHandler<float>::H FloatHandler;
3503 typedef ValueHandler<double>::H DoubleHandler;
3504 typedef ValueHandler<bool>::H BoolHandler;
3505
Josh Habermane8ed0212015-06-08 17:56:03 -07003506 /* Any function pointer can be converted to this and converted back to its
3507 * correct type. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003508 typedef void GenericFunction();
3509
3510 typedef void HandlersCallback(const void *closure, upb_handlers *h);
3511
Josh Habermane8ed0212015-06-08 17:56:03 -07003512 /* Returns a new handlers object for the given frozen msgdef.
3513 * Returns NULL if memory allocation failed. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003514 static reffed_ptr<Handlers> New(const MessageDef *m);
3515
Josh Habermane8ed0212015-06-08 17:56:03 -07003516 /* Convenience function for registering a graph of handlers that mirrors the
3517 * graph of msgdefs for some message. For "m" and all its children a new set
3518 * of handlers will be created and the given callback will be invoked,
3519 * allowing the client to register handlers for this message. Note that any
3520 * subhandlers set by the callback will be overwritten. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003521 static reffed_ptr<const Handlers> NewFrozen(const MessageDef *m,
3522 HandlersCallback *callback,
3523 const void *closure);
3524
Josh Habermane8ed0212015-06-08 17:56:03 -07003525 /* Functionality from upb::RefCounted. */
3526 UPB_REFCOUNTED_CPPMETHODS
Chris Fallin91473dc2014-12-12 15:58:26 -08003527
Josh Habermane8ed0212015-06-08 17:56:03 -07003528 /* All handler registration functions return bool to indicate success or
3529 * failure; details about failures are stored in this status object. If a
3530 * failure does occur, it must be cleared before the Handlers are frozen,
3531 * otherwise the freeze() operation will fail. The functions may *only* be
3532 * used while the Handlers are mutable. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003533 const Status* status();
3534 void ClearError();
3535
Josh Habermane8ed0212015-06-08 17:56:03 -07003536 /* Call to freeze these Handlers. Requires that any SubHandlers are already
3537 * frozen. For cycles, you must use the static version below and freeze the
3538 * whole graph at once. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003539 bool Freeze(Status* s);
3540
Josh Habermane8ed0212015-06-08 17:56:03 -07003541 /* Freezes the given set of handlers. You may not freeze a handler without
3542 * also freezing any handlers they point to. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003543 static bool Freeze(Handlers*const* handlers, int n, Status* s);
3544 static bool Freeze(const std::vector<Handlers*>& handlers, Status* s);
3545
Josh Habermane8ed0212015-06-08 17:56:03 -07003546 /* Returns the msgdef associated with this handlers object. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003547 const MessageDef* message_def() const;
3548
Josh Habermane8ed0212015-06-08 17:56:03 -07003549 /* Adds the given pointer and function to the list of cleanup functions that
3550 * will be run when these handlers are freed. If this pointer has previously
3551 * been registered, the function returns false and does nothing. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003552 bool AddCleanup(void *ptr, upb_handlerfree *cleanup);
3553
Josh Habermane8ed0212015-06-08 17:56:03 -07003554 /* Sets the startmsg handler for the message, which is defined as follows:
3555 *
3556 * bool startmsg(MyType* closure) {
3557 * // Called when the message begins. Returns true if processing should
3558 * // continue.
3559 * return true;
3560 * }
3561 */
Chris Fallin91473dc2014-12-12 15:58:26 -08003562 bool SetStartMessageHandler(const StartMessageHandler& handler);
3563
Josh Habermane8ed0212015-06-08 17:56:03 -07003564 /* Sets the endmsg handler for the message, which is defined as follows:
3565 *
3566 * bool endmsg(MyType* closure, upb_status *status) {
3567 * // Called when processing of this message ends, whether in success or
3568 * // failure. "status" indicates the final status of processing, and
3569 * // can also be modified in-place to update the final status.
3570 * }
3571 */
Chris Fallin91473dc2014-12-12 15:58:26 -08003572 bool SetEndMessageHandler(const EndMessageHandler& handler);
3573
Josh Habermane8ed0212015-06-08 17:56:03 -07003574 /* Sets the value handler for the given field, which is defined as follows
3575 * (this is for an int32 field; other field types will pass their native
3576 * C/C++ type for "val"):
3577 *
3578 * bool OnValue(MyClosure* c, const MyHandlerData* d, int32_t val) {
3579 * // Called when the field's value is encountered. "d" contains
3580 * // whatever data was bound to this field when it was registered.
3581 * // Returns true if processing should continue.
3582 * return true;
3583 * }
3584 *
3585 * handers->SetInt32Handler(f, UpbBind(OnValue, new MyHandlerData(...)));
3586 *
3587 * The value type must exactly match f->type().
3588 * For example, a handler that takes an int32_t parameter may only be used for
3589 * fields of type UPB_TYPE_INT32 and UPB_TYPE_ENUM.
3590 *
3591 * Returns false if the handler failed to register; in this case the cleanup
3592 * handler (if any) will be called immediately.
3593 */
Chris Fallin91473dc2014-12-12 15:58:26 -08003594 bool SetInt32Handler (const FieldDef* f, const Int32Handler& h);
3595 bool SetInt64Handler (const FieldDef* f, const Int64Handler& h);
3596 bool SetUInt32Handler(const FieldDef* f, const UInt32Handler& h);
3597 bool SetUInt64Handler(const FieldDef* f, const UInt64Handler& h);
3598 bool SetFloatHandler (const FieldDef* f, const FloatHandler& h);
3599 bool SetDoubleHandler(const FieldDef* f, const DoubleHandler& h);
3600 bool SetBoolHandler (const FieldDef* f, const BoolHandler& h);
3601
Josh Habermane8ed0212015-06-08 17:56:03 -07003602 /* Like the previous, but templated on the type on the value (ie. int32).
3603 * This is mostly useful to call from other templates. To call this you must
3604 * specify the template parameter explicitly, ie:
3605 * h->SetValueHandler<T>(f, UpbBind(MyHandler<T>, MyData)); */
Chris Fallin91473dc2014-12-12 15:58:26 -08003606 template <class T>
3607 bool SetValueHandler(
3608 const FieldDef *f,
3609 const typename ValueHandler<typename CanonicalType<T>::Type>::H& handler);
3610
Josh Habermane8ed0212015-06-08 17:56:03 -07003611 /* Sets handlers for a string field, which are defined as follows:
3612 *
3613 * MySubClosure* startstr(MyClosure* c, const MyHandlerData* d,
3614 * size_t size_hint) {
3615 * // Called when a string value begins. The return value indicates the
3616 * // closure for the string. "size_hint" indicates the size of the
3617 * // string if it is known, however if the string is length-delimited
3618 * // and the end-of-string is not available size_hint will be zero.
3619 * // This case is indistinguishable from the case where the size is
3620 * // known to be zero.
3621 * //
3622 * // TODO(haberman): is it important to distinguish these cases?
3623 * // If we had ssize_t as a type we could make -1 "unknown", but
3624 * // ssize_t is POSIX (not ANSI) and therefore less portable.
3625 * // In practice I suspect it won't be important to distinguish.
3626 * return closure;
3627 * }
3628 *
3629 * size_t str(MyClosure* closure, const MyHandlerData* d,
3630 * const char *str, size_t len) {
3631 * // Called for each buffer of string data; the multiple physical buffers
3632 * // are all part of the same logical string. The return value indicates
3633 * // how many bytes were consumed. If this number is less than "len",
3634 * // this will also indicate that processing should be halted for now,
3635 * // like returning false or UPB_BREAK from any other callback. If
3636 * // number is greater than "len", the excess bytes will be skipped over
3637 * // and not passed to the callback.
3638 * return len;
3639 * }
3640 *
3641 * bool endstr(MyClosure* c, const MyHandlerData* d) {
3642 * // Called when a string value ends. Return value indicates whether
3643 * // processing should continue.
3644 * return true;
3645 * }
3646 */
Chris Fallin91473dc2014-12-12 15:58:26 -08003647 bool SetStartStringHandler(const FieldDef* f, const StartStringHandler& h);
3648 bool SetStringHandler(const FieldDef* f, const StringHandler& h);
3649 bool SetEndStringHandler(const FieldDef* f, const EndFieldHandler& h);
3650
Josh Habermane8ed0212015-06-08 17:56:03 -07003651 /* Sets the startseq handler, which is defined as follows:
3652 *
3653 * MySubClosure *startseq(MyClosure* c, const MyHandlerData* d) {
3654 * // Called when a sequence (repeated field) begins. The returned
3655 * // pointer indicates the closure for the sequence (or UPB_BREAK
3656 * // to interrupt processing).
3657 * return closure;
3658 * }
3659 *
3660 * h->SetStartSequenceHandler(f, UpbBind(startseq, new MyHandlerData(...)));
3661 *
3662 * Returns "false" if "f" does not belong to this message or is not a
3663 * repeated field.
3664 */
Chris Fallin91473dc2014-12-12 15:58:26 -08003665 bool SetStartSequenceHandler(const FieldDef* f, const StartFieldHandler& h);
3666
Josh Habermane8ed0212015-06-08 17:56:03 -07003667 /* Sets the startsubmsg handler for the given field, which is defined as
3668 * follows:
3669 *
3670 * MySubClosure* startsubmsg(MyClosure* c, const MyHandlerData* d) {
3671 * // Called when a submessage begins. The returned pointer indicates the
3672 * // closure for the sequence (or UPB_BREAK to interrupt processing).
3673 * return closure;
3674 * }
3675 *
3676 * h->SetStartSubMessageHandler(f, UpbBind(startsubmsg,
3677 * new MyHandlerData(...)));
3678 *
3679 * Returns "false" if "f" does not belong to this message or is not a
3680 * submessage/group field.
3681 */
Chris Fallin91473dc2014-12-12 15:58:26 -08003682 bool SetStartSubMessageHandler(const FieldDef* f, const StartFieldHandler& h);
3683
Josh Habermane8ed0212015-06-08 17:56:03 -07003684 /* Sets the endsubmsg handler for the given field, which is defined as
3685 * follows:
3686 *
3687 * bool endsubmsg(MyClosure* c, const MyHandlerData* d) {
3688 * // Called when a submessage ends. Returns true to continue processing.
3689 * return true;
3690 * }
3691 *
3692 * Returns "false" if "f" does not belong to this message or is not a
3693 * submessage/group field.
3694 */
Chris Fallin91473dc2014-12-12 15:58:26 -08003695 bool SetEndSubMessageHandler(const FieldDef *f, const EndFieldHandler &h);
3696
Josh Habermane8ed0212015-06-08 17:56:03 -07003697 /* Starts the endsubseq handler for the given field, which is defined as
3698 * follows:
3699 *
3700 * bool endseq(MyClosure* c, const MyHandlerData* d) {
3701 * // Called when a sequence ends. Returns true continue processing.
3702 * return true;
3703 * }
3704 *
3705 * Returns "false" if "f" does not belong to this message or is not a
3706 * repeated field.
3707 */
Chris Fallin91473dc2014-12-12 15:58:26 -08003708 bool SetEndSequenceHandler(const FieldDef* f, const EndFieldHandler& h);
3709
Josh Habermane8ed0212015-06-08 17:56:03 -07003710 /* Sets or gets the object that specifies handlers for the given field, which
3711 * must be a submessage or group. Returns NULL if no handlers are set. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003712 bool SetSubHandlers(const FieldDef* f, const Handlers* sub);
3713 const Handlers* GetSubHandlers(const FieldDef* f) const;
3714
Josh Habermane8ed0212015-06-08 17:56:03 -07003715 /* Equivalent to GetSubHandlers, but takes the STARTSUBMSG selector for the
3716 * field. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003717 const Handlers* GetSubHandlers(Selector startsubmsg) const;
3718
Josh Habermane8ed0212015-06-08 17:56:03 -07003719 /* A selector refers to a specific field handler in the Handlers object
3720 * (for example: the STARTSUBMSG handler for field "field15").
3721 * On success, returns true and stores the selector in "s".
3722 * If the FieldDef or Type are invalid, returns false.
3723 * The returned selector is ONLY valid for Handlers whose MessageDef
3724 * contains this FieldDef. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003725 static bool GetSelector(const FieldDef* f, Type type, Selector* s);
3726
Josh Habermane8ed0212015-06-08 17:56:03 -07003727 /* Given a START selector of any kind, returns the corresponding END selector. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003728 static Selector GetEndSelector(Selector start_selector);
3729
Josh Habermane8ed0212015-06-08 17:56:03 -07003730 /* Returns the function pointer for this handler. It is the client's
3731 * responsibility to cast to the correct function type before calling it. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003732 GenericFunction* GetHandler(Selector selector);
3733
Josh Habermane8ed0212015-06-08 17:56:03 -07003734 /* Sets the given attributes to the attributes for this selector. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003735 bool GetAttributes(Selector selector, HandlerAttributes* attr);
3736
Josh Habermane8ed0212015-06-08 17:56:03 -07003737 /* Returns the handler data that was registered with this handler. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003738 const void* GetHandlerData(Selector selector);
3739
Josh Habermane8ed0212015-06-08 17:56:03 -07003740 /* Could add any of the following functions as-needed, with some minor
3741 * implementation changes:
3742 *
3743 * const FieldDef* GetFieldDef(Selector selector);
3744 * static bool IsSequence(Selector selector); */
Chris Fallin91473dc2014-12-12 15:58:26 -08003745
3746 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07003747 UPB_DISALLOW_POD_OPS(Handlers, upb::Handlers)
Chris Fallin91473dc2014-12-12 15:58:26 -08003748
3749 friend UPB_INLINE GenericFunction *::upb_handlers_gethandler(
3750 const upb_handlers *h, upb_selector_t s);
3751 friend UPB_INLINE const void *::upb_handlers_gethandlerdata(
3752 const upb_handlers *h, upb_selector_t s);
Josh Habermane8ed0212015-06-08 17:56:03 -07003753#else
3754struct upb_handlers {
3755#endif
3756 upb_refcounted base;
Chris Fallin91473dc2014-12-12 15:58:26 -08003757
Chris Fallin91473dc2014-12-12 15:58:26 -08003758 const upb_msgdef *msg;
3759 const upb_handlers **sub;
3760 const void *top_closure_type;
3761 upb_inttable cleanup_;
Josh Habermane8ed0212015-06-08 17:56:03 -07003762 upb_status status_; /* Used only when mutable. */
3763 upb_handlers_tabent table[1]; /* Dynamically-sized field handler array. */
3764};
Chris Fallin91473dc2014-12-12 15:58:26 -08003765
3766#ifdef __cplusplus
3767
3768namespace upb {
3769
Josh Habermane8ed0212015-06-08 17:56:03 -07003770/* Convenience macros for creating a Handler object that is wrapped with a
3771 * type-safe wrapper function that converts the "void*" parameters/returns
3772 * of the underlying C API into nice C++ function.
3773 *
3774 * Sample usage:
3775 * void OnValue1(MyClosure* c, const MyHandlerData* d, int32_t val) {
3776 * // do stuff ...
3777 * }
3778 *
3779 * // Handler that doesn't need any data bound to it.
3780 * void OnValue2(MyClosure* c, int32_t val) {
3781 * // do stuff ...
3782 * }
3783 *
3784 * // Handler that returns bool so it can return failure if necessary.
3785 * bool OnValue3(MyClosure* c, int32_t val) {
3786 * // do stuff ...
3787 * return ok;
3788 * }
3789 *
3790 * // Member function handler.
3791 * class MyClosure {
3792 * public:
3793 * void OnValue(int32_t val) {
3794 * // do stuff ...
3795 * }
3796 * };
3797 *
3798 * // Takes ownership of the MyHandlerData.
3799 * handlers->SetInt32Handler(f1, UpbBind(OnValue1, new MyHandlerData(...)));
3800 * handlers->SetInt32Handler(f2, UpbMakeHandler(OnValue2));
3801 * handlers->SetInt32Handler(f1, UpbMakeHandler(OnValue3));
3802 * handlers->SetInt32Handler(f2, UpbMakeHandler(&MyClosure::OnValue));
3803 */
Chris Fallin91473dc2014-12-12 15:58:26 -08003804
3805#ifdef UPB_CXX11
3806
Josh Habermane8ed0212015-06-08 17:56:03 -07003807/* In C++11, the "template" disambiguator can appear even outside templates,
3808 * so all calls can safely use this pair of macros. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003809
3810#define UpbMakeHandler(f) upb::MatchFunc(f).template GetFunc<f>()
3811
Josh Habermane8ed0212015-06-08 17:56:03 -07003812/* We have to be careful to only evaluate "d" once. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003813#define UpbBind(f, d) upb::MatchFunc(f).template GetFunc<f>((d))
3814
3815#else
3816
Josh Habermane8ed0212015-06-08 17:56:03 -07003817/* Prior to C++11, the "template" disambiguator may only appear inside a
3818 * template, so the regular macro must not use "template" */
Chris Fallin91473dc2014-12-12 15:58:26 -08003819
3820#define UpbMakeHandler(f) upb::MatchFunc(f).GetFunc<f>()
3821
3822#define UpbBind(f, d) upb::MatchFunc(f).GetFunc<f>((d))
3823
Josh Habermane8ed0212015-06-08 17:56:03 -07003824#endif /* UPB_CXX11 */
Chris Fallin91473dc2014-12-12 15:58:26 -08003825
Josh Habermane8ed0212015-06-08 17:56:03 -07003826/* This macro must be used in C++98 for calls from inside a template. But we
3827 * define this variant in all cases; code that wants to be compatible with both
3828 * C++98 and C++11 should always use this macro when calling from a template. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003829#define UpbMakeHandlerT(f) upb::MatchFunc(f).template GetFunc<f>()
3830
Josh Habermane8ed0212015-06-08 17:56:03 -07003831/* We have to be careful to only evaluate "d" once. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003832#define UpbBindT(f, d) upb::MatchFunc(f).template GetFunc<f>((d))
3833
Josh Habermane8ed0212015-06-08 17:56:03 -07003834/* Handler: a struct that contains the (handler, data, deleter) tuple that is
3835 * used to register all handlers. Users can Make() these directly but it's
3836 * more convenient to use the UpbMakeHandler/UpbBind macros above. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003837template <class T> class Handler {
3838 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07003839 /* The underlying, handler function signature that upb uses internally. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003840 typedef T FuncPtr;
3841
Josh Habermane8ed0212015-06-08 17:56:03 -07003842 /* Intentionally implicit. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003843 template <class F> Handler(F func);
3844 ~Handler();
3845
3846 private:
3847 void AddCleanup(Handlers* h) const {
3848 if (cleanup_func_) {
3849 bool ok = h->AddCleanup(cleanup_data_, cleanup_func_);
3850 UPB_ASSERT_VAR(ok, ok);
3851 }
3852 }
3853
Josh Habermane8ed0212015-06-08 17:56:03 -07003854 UPB_DISALLOW_COPY_AND_ASSIGN(Handler)
Chris Fallin91473dc2014-12-12 15:58:26 -08003855 friend class Handlers;
3856 FuncPtr handler_;
3857 mutable HandlerAttributes attr_;
3858 mutable bool registered_;
3859 void *cleanup_data_;
3860 upb_handlerfree *cleanup_func_;
3861};
3862
Josh Habermane8ed0212015-06-08 17:56:03 -07003863} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08003864
Josh Habermane8ed0212015-06-08 17:56:03 -07003865#endif /* __cplusplus */
Chris Fallin91473dc2014-12-12 15:58:26 -08003866
3867UPB_BEGIN_EXTERN_C
3868
Josh Habermane8ed0212015-06-08 17:56:03 -07003869/* Native C API. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003870
Josh Habermane8ed0212015-06-08 17:56:03 -07003871/* Handler function typedefs. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003872typedef bool upb_startmsg_handlerfunc(void *c, const void*);
3873typedef bool upb_endmsg_handlerfunc(void *c, const void *, upb_status *status);
3874typedef void* upb_startfield_handlerfunc(void *c, const void *hd);
3875typedef bool upb_endfield_handlerfunc(void *c, const void *hd);
3876typedef bool upb_int32_handlerfunc(void *c, const void *hd, int32_t val);
3877typedef bool upb_int64_handlerfunc(void *c, const void *hd, int64_t val);
3878typedef bool upb_uint32_handlerfunc(void *c, const void *hd, uint32_t val);
3879typedef bool upb_uint64_handlerfunc(void *c, const void *hd, uint64_t val);
3880typedef bool upb_float_handlerfunc(void *c, const void *hd, float val);
3881typedef bool upb_double_handlerfunc(void *c, const void *hd, double val);
3882typedef bool upb_bool_handlerfunc(void *c, const void *hd, bool val);
3883typedef void *upb_startstr_handlerfunc(void *c, const void *hd,
3884 size_t size_hint);
3885typedef size_t upb_string_handlerfunc(void *c, const void *hd, const char *buf,
3886 size_t n, const upb_bufhandle* handle);
3887
Josh Habermane8ed0212015-06-08 17:56:03 -07003888/* upb_bufhandle */
Chris Fallin91473dc2014-12-12 15:58:26 -08003889size_t upb_bufhandle_objofs(const upb_bufhandle *h);
3890
Josh Habermane8ed0212015-06-08 17:56:03 -07003891/* upb_handlerattr */
Chris Fallin91473dc2014-12-12 15:58:26 -08003892void upb_handlerattr_init(upb_handlerattr *attr);
3893void upb_handlerattr_uninit(upb_handlerattr *attr);
3894
3895bool upb_handlerattr_sethandlerdata(upb_handlerattr *attr, const void *hd);
3896bool upb_handlerattr_setclosuretype(upb_handlerattr *attr, const void *type);
3897const void *upb_handlerattr_closuretype(const upb_handlerattr *attr);
3898bool upb_handlerattr_setreturnclosuretype(upb_handlerattr *attr,
3899 const void *type);
3900const void *upb_handlerattr_returnclosuretype(const upb_handlerattr *attr);
3901bool upb_handlerattr_setalwaysok(upb_handlerattr *attr, bool alwaysok);
3902bool upb_handlerattr_alwaysok(const upb_handlerattr *attr);
3903
3904UPB_INLINE const void *upb_handlerattr_handlerdata(
3905 const upb_handlerattr *attr) {
3906 return attr->handler_data_;
3907}
3908
Josh Habermane8ed0212015-06-08 17:56:03 -07003909/* upb_handlers */
Chris Fallin91473dc2014-12-12 15:58:26 -08003910typedef void upb_handlers_callback(const void *closure, upb_handlers *h);
3911upb_handlers *upb_handlers_new(const upb_msgdef *m,
3912 const void *owner);
3913const upb_handlers *upb_handlers_newfrozen(const upb_msgdef *m,
3914 const void *owner,
3915 upb_handlers_callback *callback,
3916 const void *closure);
Josh Habermane8ed0212015-06-08 17:56:03 -07003917
3918/* Include refcounted methods like upb_handlers_ref(). */
3919UPB_REFCOUNTED_CMETHODS(upb_handlers, upb_handlers_upcast)
Chris Fallin91473dc2014-12-12 15:58:26 -08003920
3921const upb_status *upb_handlers_status(upb_handlers *h);
3922void upb_handlers_clearerr(upb_handlers *h);
3923const upb_msgdef *upb_handlers_msgdef(const upb_handlers *h);
3924bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *hfree);
3925
3926bool upb_handlers_setstartmsg(upb_handlers *h, upb_startmsg_handlerfunc *func,
3927 upb_handlerattr *attr);
3928bool upb_handlers_setendmsg(upb_handlers *h, upb_endmsg_handlerfunc *func,
3929 upb_handlerattr *attr);
3930bool upb_handlers_setint32(upb_handlers *h, const upb_fielddef *f,
3931 upb_int32_handlerfunc *func, upb_handlerattr *attr);
3932bool upb_handlers_setint64(upb_handlers *h, const upb_fielddef *f,
3933 upb_int64_handlerfunc *func, upb_handlerattr *attr);
3934bool upb_handlers_setuint32(upb_handlers *h, const upb_fielddef *f,
3935 upb_uint32_handlerfunc *func,
3936 upb_handlerattr *attr);
3937bool upb_handlers_setuint64(upb_handlers *h, const upb_fielddef *f,
3938 upb_uint64_handlerfunc *func,
3939 upb_handlerattr *attr);
3940bool upb_handlers_setfloat(upb_handlers *h, const upb_fielddef *f,
3941 upb_float_handlerfunc *func, upb_handlerattr *attr);
3942bool upb_handlers_setdouble(upb_handlers *h, const upb_fielddef *f,
3943 upb_double_handlerfunc *func,
3944 upb_handlerattr *attr);
3945bool upb_handlers_setbool(upb_handlers *h, const upb_fielddef *f,
3946 upb_bool_handlerfunc *func,
3947 upb_handlerattr *attr);
3948bool upb_handlers_setstartstr(upb_handlers *h, const upb_fielddef *f,
3949 upb_startstr_handlerfunc *func,
3950 upb_handlerattr *attr);
3951bool upb_handlers_setstring(upb_handlers *h, const upb_fielddef *f,
3952 upb_string_handlerfunc *func,
3953 upb_handlerattr *attr);
3954bool upb_handlers_setendstr(upb_handlers *h, const upb_fielddef *f,
3955 upb_endfield_handlerfunc *func,
3956 upb_handlerattr *attr);
3957bool upb_handlers_setstartseq(upb_handlers *h, const upb_fielddef *f,
3958 upb_startfield_handlerfunc *func,
3959 upb_handlerattr *attr);
3960bool upb_handlers_setstartsubmsg(upb_handlers *h, const upb_fielddef *f,
3961 upb_startfield_handlerfunc *func,
3962 upb_handlerattr *attr);
3963bool upb_handlers_setendsubmsg(upb_handlers *h, const upb_fielddef *f,
3964 upb_endfield_handlerfunc *func,
3965 upb_handlerattr *attr);
3966bool upb_handlers_setendseq(upb_handlers *h, const upb_fielddef *f,
3967 upb_endfield_handlerfunc *func,
3968 upb_handlerattr *attr);
3969
3970bool upb_handlers_setsubhandlers(upb_handlers *h, const upb_fielddef *f,
3971 const upb_handlers *sub);
3972const upb_handlers *upb_handlers_getsubhandlers(const upb_handlers *h,
3973 const upb_fielddef *f);
3974const upb_handlers *upb_handlers_getsubhandlers_sel(const upb_handlers *h,
3975 upb_selector_t sel);
3976
3977UPB_INLINE upb_func *upb_handlers_gethandler(const upb_handlers *h,
3978 upb_selector_t s) {
3979 return (upb_func *)h->table[s].func;
3980}
3981
3982bool upb_handlers_getattr(const upb_handlers *h, upb_selector_t s,
3983 upb_handlerattr *attr);
3984
3985UPB_INLINE const void *upb_handlers_gethandlerdata(const upb_handlers *h,
3986 upb_selector_t s) {
3987 return upb_handlerattr_handlerdata(&h->table[s].attr);
3988}
3989
Josh Habermane8ed0212015-06-08 17:56:03 -07003990#ifdef __cplusplus
3991
3992/* Handler types for single fields.
3993 * Right now we only have one for TYPE_BYTES but ones for other types
3994 * should follow.
3995 *
3996 * These follow the same handlers protocol for fields of a message. */
3997class upb::BytesHandler {
Chris Fallin91473dc2014-12-12 15:58:26 -08003998 public:
3999 BytesHandler();
4000 ~BytesHandler();
Josh Habermane8ed0212015-06-08 17:56:03 -07004001#else
4002struct upb_byteshandler {
4003#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08004004 upb_handlers_tabent table[3];
Josh Habermane8ed0212015-06-08 17:56:03 -07004005};
Chris Fallin91473dc2014-12-12 15:58:26 -08004006
4007void upb_byteshandler_init(upb_byteshandler *h);
Chris Fallin91473dc2014-12-12 15:58:26 -08004008
Josh Habermane8ed0212015-06-08 17:56:03 -07004009/* Caller must ensure that "d" outlives the handlers.
4010 * TODO(haberman): should this have a "freeze" operation? It's not necessary
4011 * for memory management, but could be useful to force immutability and provide
4012 * a convenient moment to verify that all registration succeeded. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004013bool upb_byteshandler_setstartstr(upb_byteshandler *h,
4014 upb_startstr_handlerfunc *func, void *d);
4015bool upb_byteshandler_setstring(upb_byteshandler *h,
4016 upb_string_handlerfunc *func, void *d);
4017bool upb_byteshandler_setendstr(upb_byteshandler *h,
4018 upb_endfield_handlerfunc *func, void *d);
4019
Josh Habermane8ed0212015-06-08 17:56:03 -07004020/* "Static" methods */
Chris Fallin91473dc2014-12-12 15:58:26 -08004021bool upb_handlers_freeze(upb_handlers *const *handlers, int n, upb_status *s);
4022upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f);
4023bool upb_handlers_getselector(const upb_fielddef *f, upb_handlertype_t type,
4024 upb_selector_t *s);
4025UPB_INLINE upb_selector_t upb_handlers_getendselector(upb_selector_t start) {
4026 return start + 1;
4027}
4028
Josh Habermane8ed0212015-06-08 17:56:03 -07004029/* Internal-only. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004030uint32_t upb_handlers_selectorbaseoffset(const upb_fielddef *f);
4031uint32_t upb_handlers_selectorcount(const upb_fielddef *f);
4032
4033UPB_END_EXTERN_C
4034
4035/*
Josh Haberman181c7f22015-07-15 11:05:10 -07004036** Inline definitions for handlers.h, which are particularly long and a bit
4037** tricky.
4038*/
Chris Fallin91473dc2014-12-12 15:58:26 -08004039
4040#ifndef UPB_HANDLERS_INL_H_
4041#define UPB_HANDLERS_INL_H_
4042
4043#include <limits.h>
4044
Josh Habermane8ed0212015-06-08 17:56:03 -07004045/* C inline methods. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004046
Josh Habermane8ed0212015-06-08 17:56:03 -07004047/* upb_bufhandle */
Chris Fallin91473dc2014-12-12 15:58:26 -08004048UPB_INLINE void upb_bufhandle_init(upb_bufhandle *h) {
4049 h->obj_ = NULL;
4050 h->objtype_ = NULL;
4051 h->buf_ = NULL;
4052 h->objofs_ = 0;
4053}
4054UPB_INLINE void upb_bufhandle_uninit(upb_bufhandle *h) {
4055 UPB_UNUSED(h);
4056}
4057UPB_INLINE void upb_bufhandle_setobj(upb_bufhandle *h, const void *obj,
4058 const void *type) {
4059 h->obj_ = obj;
4060 h->objtype_ = type;
4061}
4062UPB_INLINE void upb_bufhandle_setbuf(upb_bufhandle *h, const char *buf,
4063 size_t ofs) {
4064 h->buf_ = buf;
4065 h->objofs_ = ofs;
4066}
4067UPB_INLINE const void *upb_bufhandle_obj(const upb_bufhandle *h) {
4068 return h->obj_;
4069}
4070UPB_INLINE const void *upb_bufhandle_objtype(const upb_bufhandle *h) {
4071 return h->objtype_;
4072}
4073UPB_INLINE const char *upb_bufhandle_buf(const upb_bufhandle *h) {
4074 return h->buf_;
4075}
4076
4077
4078#ifdef __cplusplus
4079
Josh Habermane8ed0212015-06-08 17:56:03 -07004080/* Type detection and typedefs for integer types.
4081 * For platforms where there are multiple 32-bit or 64-bit types, we need to be
4082 * able to enumerate them so we can properly create overloads for all variants.
4083 *
4084 * If any platform existed where there were three integer types with the same
4085 * size, this would have to become more complicated. For example, short, int,
4086 * and long could all be 32-bits. Even more diabolically, short, int, long,
4087 * and long long could all be 64 bits and still be standard-compliant.
4088 * However, few platforms are this strange, and it's unlikely that upb will be
4089 * used on the strangest ones. */
4090
4091/* Can't count on stdint.h limits like INT32_MAX, because in C++ these are
4092 * only defined when __STDC_LIMIT_MACROS are defined before the *first* include
4093 * of stdint.h. We can't guarantee that someone else didn't include these first
4094 * without defining __STDC_LIMIT_MACROS. */
4095#define UPB_INT32_MAX 0x7fffffffLL
4096#define UPB_INT32_MIN (-UPB_INT32_MAX - 1)
4097#define UPB_INT64_MAX 0x7fffffffffffffffLL
4098#define UPB_INT64_MIN (-UPB_INT64_MAX - 1)
4099
4100#if INT_MAX == UPB_INT32_MAX && INT_MIN == UPB_INT32_MIN
4101#define UPB_INT_IS_32BITS 1
4102#endif
4103
4104#if LONG_MAX == UPB_INT32_MAX && LONG_MIN == UPB_INT32_MIN
4105#define UPB_LONG_IS_32BITS 1
4106#endif
4107
4108#if LONG_MAX == UPB_INT64_MAX && LONG_MIN == UPB_INT64_MIN
4109#define UPB_LONG_IS_64BITS 1
4110#endif
4111
4112#if LLONG_MAX == UPB_INT64_MAX && LLONG_MIN == UPB_INT64_MIN
4113#define UPB_LLONG_IS_64BITS 1
4114#endif
4115
4116/* We use macros instead of typedefs so we can undefine them later and avoid
4117 * leaking them outside this header file. */
4118#if UPB_INT_IS_32BITS
4119#define UPB_INT32_T int
4120#define UPB_UINT32_T unsigned int
4121
4122#if UPB_LONG_IS_32BITS
4123#define UPB_TWO_32BIT_TYPES 1
4124#define UPB_INT32ALT_T long
4125#define UPB_UINT32ALT_T unsigned long
4126#endif /* UPB_LONG_IS_32BITS */
4127
4128#elif UPB_LONG_IS_32BITS /* && !UPB_INT_IS_32BITS */
4129#define UPB_INT32_T long
4130#define UPB_UINT32_T unsigned long
4131#endif /* UPB_INT_IS_32BITS */
4132
4133
4134#if UPB_LONG_IS_64BITS
4135#define UPB_INT64_T long
4136#define UPB_UINT64_T unsigned long
4137
4138#if UPB_LLONG_IS_64BITS
4139#define UPB_TWO_64BIT_TYPES 1
4140#define UPB_INT64ALT_T long long
4141#define UPB_UINT64ALT_T unsigned long long
4142#endif /* UPB_LLONG_IS_64BITS */
4143
4144#elif UPB_LLONG_IS_64BITS /* && !UPB_LONG_IS_64BITS */
4145#define UPB_INT64_T long long
4146#define UPB_UINT64_T unsigned long long
4147#endif /* UPB_LONG_IS_64BITS */
4148
4149#undef UPB_INT32_MAX
4150#undef UPB_INT32_MIN
4151#undef UPB_INT64_MAX
4152#undef UPB_INT64_MIN
4153#undef UPB_INT_IS_32BITS
4154#undef UPB_LONG_IS_32BITS
4155#undef UPB_LONG_IS_64BITS
4156#undef UPB_LLONG_IS_64BITS
4157
4158
Chris Fallin91473dc2014-12-12 15:58:26 -08004159namespace upb {
4160
4161typedef void CleanupFunc(void *ptr);
4162
Josh Habermane8ed0212015-06-08 17:56:03 -07004163/* Template to remove "const" from "const T*" and just return "T*".
4164 *
4165 * We define a nonsense default because otherwise it will fail to instantiate as
4166 * a function parameter type even in cases where we don't expect any caller to
4167 * actually match the overload. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004168class CouldntRemoveConst {};
4169template <class T> struct remove_constptr { typedef CouldntRemoveConst type; };
4170template <class T> struct remove_constptr<const T *> { typedef T *type; };
4171
Josh Habermane8ed0212015-06-08 17:56:03 -07004172/* Template that we use below to remove a template specialization from
4173 * consideration if it matches a specific type. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004174template <class T, class U> struct disable_if_same { typedef void Type; };
4175template <class T> struct disable_if_same<T, T> {};
4176
4177template <class T> void DeletePointer(void *p) { delete static_cast<T>(p); }
4178
4179template <class T1, class T2>
Chris Fallind3262772015-05-14 18:24:26 -07004180struct FirstUnlessVoidOrBool {
Chris Fallin91473dc2014-12-12 15:58:26 -08004181 typedef T1 value;
4182};
4183
4184template <class T2>
Chris Fallind3262772015-05-14 18:24:26 -07004185struct FirstUnlessVoidOrBool<void, T2> {
4186 typedef T2 value;
4187};
4188
4189template <class T2>
4190struct FirstUnlessVoidOrBool<bool, T2> {
Chris Fallin91473dc2014-12-12 15:58:26 -08004191 typedef T2 value;
4192};
4193
4194template<class T, class U>
4195struct is_same {
4196 static bool value;
4197};
4198
4199template<class T>
4200struct is_same<T, T> {
4201 static bool value;
4202};
4203
4204template<class T, class U>
4205bool is_same<T, U>::value = false;
4206
4207template<class T>
4208bool is_same<T, T>::value = true;
4209
Josh Habermane8ed0212015-06-08 17:56:03 -07004210/* FuncInfo *******************************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -08004211
Josh Habermane8ed0212015-06-08 17:56:03 -07004212/* Info about the user's original, pre-wrapped function. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004213template <class C, class R = void>
4214struct FuncInfo {
Josh Habermane8ed0212015-06-08 17:56:03 -07004215 /* The type of the closure that the function takes (its first param). */
Chris Fallin91473dc2014-12-12 15:58:26 -08004216 typedef C Closure;
4217
Josh Habermane8ed0212015-06-08 17:56:03 -07004218 /* The return type. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004219 typedef R Return;
4220};
4221
Josh Habermane8ed0212015-06-08 17:56:03 -07004222/* Func ***********************************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -08004223
Josh Habermane8ed0212015-06-08 17:56:03 -07004224/* Func1, Func2, Func3: Template classes representing a function and its
4225 * signature.
4226 *
4227 * Since the function is a template parameter, calling the function can be
4228 * inlined at compile-time and does not require a function pointer at runtime.
4229 * These functions are not bound to a handler data so have no data or cleanup
4230 * handler. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004231struct UnboundFunc {
4232 CleanupFunc *GetCleanup() { return NULL; }
4233 void *GetData() { return NULL; }
4234};
4235
4236template <class R, class P1, R F(P1), class I>
4237struct Func1 : public UnboundFunc {
4238 typedef R Return;
4239 typedef I FuncInfo;
4240 static R Call(P1 p1) { return F(p1); }
4241};
4242
4243template <class R, class P1, class P2, R F(P1, P2), class I>
4244struct Func2 : public UnboundFunc {
4245 typedef R Return;
4246 typedef I FuncInfo;
4247 static R Call(P1 p1, P2 p2) { return F(p1, p2); }
4248};
4249
4250template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I>
4251struct Func3 : public UnboundFunc {
4252 typedef R Return;
4253 typedef I FuncInfo;
4254 static R Call(P1 p1, P2 p2, P3 p3) { return F(p1, p2, p3); }
4255};
4256
4257template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
4258 class I>
4259struct Func4 : public UnboundFunc {
4260 typedef R Return;
4261 typedef I FuncInfo;
4262 static R Call(P1 p1, P2 p2, P3 p3, P4 p4) { return F(p1, p2, p3, p4); }
4263};
4264
4265template <class R, class P1, class P2, class P3, class P4, class P5,
4266 R F(P1, P2, P3, P4, P5), class I>
4267struct Func5 : public UnboundFunc {
4268 typedef R Return;
4269 typedef I FuncInfo;
4270 static R Call(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
4271 return F(p1, p2, p3, p4, p5);
4272 }
4273};
4274
Josh Habermane8ed0212015-06-08 17:56:03 -07004275/* BoundFunc ******************************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -08004276
Josh Habermane8ed0212015-06-08 17:56:03 -07004277/* BoundFunc2, BoundFunc3: Like Func2/Func3 except also contains a value that
4278 * shall be bound to the function's second parameter.
4279 *
4280 * Note that the second parameter is a const pointer, but our stored bound value
4281 * is non-const so we can free it when the handlers are destroyed. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004282template <class T>
4283struct BoundFunc {
4284 typedef typename remove_constptr<T>::type MutableP2;
4285 explicit BoundFunc(MutableP2 data_) : data(data_) {}
4286 CleanupFunc *GetCleanup() { return &DeletePointer<MutableP2>; }
4287 MutableP2 GetData() { return data; }
4288 MutableP2 data;
4289};
4290
4291template <class R, class P1, class P2, R F(P1, P2), class I>
4292struct BoundFunc2 : public BoundFunc<P2> {
4293 typedef BoundFunc<P2> Base;
4294 typedef I FuncInfo;
4295 explicit BoundFunc2(typename Base::MutableP2 arg) : Base(arg) {}
4296};
4297
4298template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I>
4299struct BoundFunc3 : public BoundFunc<P2> {
4300 typedef BoundFunc<P2> Base;
4301 typedef I FuncInfo;
4302 explicit BoundFunc3(typename Base::MutableP2 arg) : Base(arg) {}
4303};
4304
4305template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
4306 class I>
4307struct BoundFunc4 : public BoundFunc<P2> {
4308 typedef BoundFunc<P2> Base;
4309 typedef I FuncInfo;
4310 explicit BoundFunc4(typename Base::MutableP2 arg) : Base(arg) {}
4311};
4312
4313template <class R, class P1, class P2, class P3, class P4, class P5,
4314 R F(P1, P2, P3, P4, P5), class I>
4315struct BoundFunc5 : public BoundFunc<P2> {
4316 typedef BoundFunc<P2> Base;
4317 typedef I FuncInfo;
4318 explicit BoundFunc5(typename Base::MutableP2 arg) : Base(arg) {}
4319};
4320
Josh Habermane8ed0212015-06-08 17:56:03 -07004321/* FuncSig ********************************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -08004322
Josh Habermane8ed0212015-06-08 17:56:03 -07004323/* FuncSig1, FuncSig2, FuncSig3: template classes reflecting a function
4324 * *signature*, but without a specific function attached.
4325 *
4326 * These classes contain member functions that can be invoked with a
4327 * specific function to return a Func/BoundFunc class. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004328template <class R, class P1>
4329struct FuncSig1 {
4330 template <R F(P1)>
4331 Func1<R, P1, F, FuncInfo<P1, R> > GetFunc() {
4332 return Func1<R, P1, F, FuncInfo<P1, R> >();
4333 }
4334};
4335
4336template <class R, class P1, class P2>
4337struct FuncSig2 {
4338 template <R F(P1, P2)>
4339 Func2<R, P1, P2, F, FuncInfo<P1, R> > GetFunc() {
4340 return Func2<R, P1, P2, F, FuncInfo<P1, R> >();
4341 }
4342
4343 template <R F(P1, P2)>
4344 BoundFunc2<R, P1, P2, F, FuncInfo<P1, R> > GetFunc(
4345 typename remove_constptr<P2>::type param2) {
4346 return BoundFunc2<R, P1, P2, F, FuncInfo<P1, R> >(param2);
4347 }
4348};
4349
4350template <class R, class P1, class P2, class P3>
4351struct FuncSig3 {
4352 template <R F(P1, P2, P3)>
4353 Func3<R, P1, P2, P3, F, FuncInfo<P1, R> > GetFunc() {
4354 return Func3<R, P1, P2, P3, F, FuncInfo<P1, R> >();
4355 }
4356
4357 template <R F(P1, P2, P3)>
4358 BoundFunc3<R, P1, P2, P3, F, FuncInfo<P1, R> > GetFunc(
4359 typename remove_constptr<P2>::type param2) {
4360 return BoundFunc3<R, P1, P2, P3, F, FuncInfo<P1, R> >(param2);
4361 }
4362};
4363
4364template <class R, class P1, class P2, class P3, class P4>
4365struct FuncSig4 {
4366 template <R F(P1, P2, P3, P4)>
4367 Func4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> > GetFunc() {
4368 return Func4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> >();
4369 }
4370
4371 template <R F(P1, P2, P3, P4)>
4372 BoundFunc4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> > GetFunc(
4373 typename remove_constptr<P2>::type param2) {
4374 return BoundFunc4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> >(param2);
4375 }
4376};
4377
4378template <class R, class P1, class P2, class P3, class P4, class P5>
4379struct FuncSig5 {
4380 template <R F(P1, P2, P3, P4, P5)>
4381 Func5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> > GetFunc() {
4382 return Func5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> >();
4383 }
4384
4385 template <R F(P1, P2, P3, P4, P5)>
4386 BoundFunc5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> > GetFunc(
4387 typename remove_constptr<P2>::type param2) {
4388 return BoundFunc5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> >(param2);
4389 }
4390};
4391
Josh Habermane8ed0212015-06-08 17:56:03 -07004392/* Overloaded template function that can construct the appropriate FuncSig*
4393 * class given a function pointer by deducing the template parameters. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004394template <class R, class P1>
4395inline FuncSig1<R, P1> MatchFunc(R (*f)(P1)) {
Josh Habermane8ed0212015-06-08 17:56:03 -07004396 UPB_UNUSED(f); /* Only used for template parameter deduction. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004397 return FuncSig1<R, P1>();
4398}
4399
4400template <class R, class P1, class P2>
4401inline FuncSig2<R, P1, P2> MatchFunc(R (*f)(P1, P2)) {
Josh Habermane8ed0212015-06-08 17:56:03 -07004402 UPB_UNUSED(f); /* Only used for template parameter deduction. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004403 return FuncSig2<R, P1, P2>();
4404}
4405
4406template <class R, class P1, class P2, class P3>
4407inline FuncSig3<R, P1, P2, P3> MatchFunc(R (*f)(P1, P2, P3)) {
Josh Habermane8ed0212015-06-08 17:56:03 -07004408 UPB_UNUSED(f); /* Only used for template parameter deduction. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004409 return FuncSig3<R, P1, P2, P3>();
4410}
4411
4412template <class R, class P1, class P2, class P3, class P4>
4413inline FuncSig4<R, P1, P2, P3, P4> MatchFunc(R (*f)(P1, P2, P3, P4)) {
Josh Habermane8ed0212015-06-08 17:56:03 -07004414 UPB_UNUSED(f); /* Only used for template parameter deduction. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004415 return FuncSig4<R, P1, P2, P3, P4>();
4416}
4417
4418template <class R, class P1, class P2, class P3, class P4, class P5>
4419inline FuncSig5<R, P1, P2, P3, P4, P5> MatchFunc(R (*f)(P1, P2, P3, P4, P5)) {
Josh Habermane8ed0212015-06-08 17:56:03 -07004420 UPB_UNUSED(f); /* Only used for template parameter deduction. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004421 return FuncSig5<R, P1, P2, P3, P4, P5>();
4422}
4423
Josh Habermane8ed0212015-06-08 17:56:03 -07004424/* MethodSig ******************************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -08004425
Josh Habermane8ed0212015-06-08 17:56:03 -07004426/* CallMethod*: a function template that calls a given method. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004427template <class R, class C, R (C::*F)()>
4428R CallMethod0(C *obj) {
4429 return ((*obj).*F)();
4430}
4431
4432template <class R, class C, class P1, R (C::*F)(P1)>
4433R CallMethod1(C *obj, P1 arg1) {
4434 return ((*obj).*F)(arg1);
4435}
4436
4437template <class R, class C, class P1, class P2, R (C::*F)(P1, P2)>
4438R CallMethod2(C *obj, P1 arg1, P2 arg2) {
4439 return ((*obj).*F)(arg1, arg2);
4440}
4441
4442template <class R, class C, class P1, class P2, class P3, R (C::*F)(P1, P2, P3)>
4443R CallMethod3(C *obj, P1 arg1, P2 arg2, P3 arg3) {
4444 return ((*obj).*F)(arg1, arg2, arg3);
4445}
4446
4447template <class R, class C, class P1, class P2, class P3, class P4,
4448 R (C::*F)(P1, P2, P3, P4)>
4449R CallMethod4(C *obj, P1 arg1, P2 arg2, P3 arg3, P4 arg4) {
4450 return ((*obj).*F)(arg1, arg2, arg3, arg4);
4451}
4452
Josh Habermane8ed0212015-06-08 17:56:03 -07004453/* MethodSig: like FuncSig, but for member functions.
4454 *
4455 * GetFunc() returns a normal FuncN object, so after calling GetFunc() no
4456 * more logic is required to special-case methods. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004457template <class R, class C>
4458struct MethodSig0 {
4459 template <R (C::*F)()>
4460 Func1<R, C *, CallMethod0<R, C, F>, FuncInfo<C *, R> > GetFunc() {
4461 return Func1<R, C *, CallMethod0<R, C, F>, FuncInfo<C *, R> >();
4462 }
4463};
4464
4465template <class R, class C, class P1>
4466struct MethodSig1 {
4467 template <R (C::*F)(P1)>
4468 Func2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> > GetFunc() {
4469 return Func2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> >();
4470 }
4471
4472 template <R (C::*F)(P1)>
4473 BoundFunc2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> > GetFunc(
4474 typename remove_constptr<P1>::type param1) {
4475 return BoundFunc2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> >(
4476 param1);
4477 }
4478};
4479
4480template <class R, class C, class P1, class P2>
4481struct MethodSig2 {
4482 template <R (C::*F)(P1, P2)>
4483 Func3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>, FuncInfo<C *, R> >
4484 GetFunc() {
4485 return Func3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>,
4486 FuncInfo<C *, R> >();
4487 }
4488
4489 template <R (C::*F)(P1, P2)>
4490 BoundFunc3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>, FuncInfo<C *, R> >
4491 GetFunc(typename remove_constptr<P1>::type param1) {
4492 return BoundFunc3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>,
4493 FuncInfo<C *, R> >(param1);
4494 }
4495};
4496
4497template <class R, class C, class P1, class P2, class P3>
4498struct MethodSig3 {
4499 template <R (C::*F)(P1, P2, P3)>
4500 Func4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>, FuncInfo<C *, R> >
4501 GetFunc() {
4502 return Func4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
4503 FuncInfo<C *, R> >();
4504 }
4505
4506 template <R (C::*F)(P1, P2, P3)>
4507 BoundFunc4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
4508 FuncInfo<C *, R> >
4509 GetFunc(typename remove_constptr<P1>::type param1) {
4510 return BoundFunc4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
4511 FuncInfo<C *, R> >(param1);
4512 }
4513};
4514
4515template <class R, class C, class P1, class P2, class P3, class P4>
4516struct MethodSig4 {
4517 template <R (C::*F)(P1, P2, P3, P4)>
4518 Func5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
4519 FuncInfo<C *, R> >
4520 GetFunc() {
4521 return Func5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
4522 FuncInfo<C *, R> >();
4523 }
4524
4525 template <R (C::*F)(P1, P2, P3, P4)>
4526 BoundFunc5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
4527 FuncInfo<C *, R> >
4528 GetFunc(typename remove_constptr<P1>::type param1) {
4529 return BoundFunc5<R, C *, P1, P2, P3, P4,
4530 CallMethod4<R, C, P1, P2, P3, P4, F>, FuncInfo<C *, R> >(
4531 param1);
4532 }
4533};
4534
4535template <class R, class C>
4536inline MethodSig0<R, C> MatchFunc(R (C::*f)()) {
Josh Habermane8ed0212015-06-08 17:56:03 -07004537 UPB_UNUSED(f); /* Only used for template parameter deduction. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004538 return MethodSig0<R, C>();
4539}
4540
4541template <class R, class C, class P1>
4542inline MethodSig1<R, C, P1> MatchFunc(R (C::*f)(P1)) {
Josh Habermane8ed0212015-06-08 17:56:03 -07004543 UPB_UNUSED(f); /* Only used for template parameter deduction. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004544 return MethodSig1<R, C, P1>();
4545}
4546
4547template <class R, class C, class P1, class P2>
4548inline MethodSig2<R, C, P1, P2> MatchFunc(R (C::*f)(P1, P2)) {
Josh Habermane8ed0212015-06-08 17:56:03 -07004549 UPB_UNUSED(f); /* Only used for template parameter deduction. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004550 return MethodSig2<R, C, P1, P2>();
4551}
4552
4553template <class R, class C, class P1, class P2, class P3>
4554inline MethodSig3<R, C, P1, P2, P3> MatchFunc(R (C::*f)(P1, P2, P3)) {
Josh Habermane8ed0212015-06-08 17:56:03 -07004555 UPB_UNUSED(f); /* Only used for template parameter deduction. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004556 return MethodSig3<R, C, P1, P2, P3>();
4557}
4558
4559template <class R, class C, class P1, class P2, class P3, class P4>
4560inline MethodSig4<R, C, P1, P2, P3, P4> MatchFunc(R (C::*f)(P1, P2, P3, P4)) {
Josh Habermane8ed0212015-06-08 17:56:03 -07004561 UPB_UNUSED(f); /* Only used for template parameter deduction. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004562 return MethodSig4<R, C, P1, P2, P3, P4>();
4563}
4564
Josh Habermane8ed0212015-06-08 17:56:03 -07004565/* MaybeWrapReturn ************************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -08004566
Josh Habermane8ed0212015-06-08 17:56:03 -07004567/* Template class that attempts to wrap the return value of the function so it
4568 * matches the expected type. There are two main adjustments it may make:
4569 *
4570 * 1. If the function returns void, make it return the expected type and with
4571 * a value that always indicates success.
4572 * 2. If the function returns bool, make it return the expected type with a
4573 * value that indicates success or failure.
4574 *
4575 * The "expected type" for return is:
4576 * 1. void* for start handlers. If the closure parameter has a different type
4577 * we will cast it to void* for the return in the success case.
4578 * 2. size_t for string buffer handlers.
4579 * 3. bool for everything else. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004580
Josh Habermane8ed0212015-06-08 17:56:03 -07004581/* Template parameters are FuncN type and desired return type. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004582template <class F, class R, class Enable = void>
4583struct MaybeWrapReturn;
4584
Josh Habermane8ed0212015-06-08 17:56:03 -07004585/* If the return type matches, return the given function unwrapped. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004586template <class F>
4587struct MaybeWrapReturn<F, typename F::Return> {
4588 typedef F Func;
4589};
4590
Josh Habermane8ed0212015-06-08 17:56:03 -07004591/* Function wrapper that munges the return value from void to (bool)true. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004592template <class P1, class P2, void F(P1, P2)>
4593bool ReturnTrue2(P1 p1, P2 p2) {
4594 F(p1, p2);
4595 return true;
4596}
4597
4598template <class P1, class P2, class P3, void F(P1, P2, P3)>
4599bool ReturnTrue3(P1 p1, P2 p2, P3 p3) {
4600 F(p1, p2, p3);
4601 return true;
4602}
4603
Josh Habermane8ed0212015-06-08 17:56:03 -07004604/* Function wrapper that munges the return value from void to (void*)arg1 */
Chris Fallin91473dc2014-12-12 15:58:26 -08004605template <class P1, class P2, void F(P1, P2)>
4606void *ReturnClosure2(P1 p1, P2 p2) {
4607 F(p1, p2);
4608 return p1;
4609}
4610
4611template <class P1, class P2, class P3, void F(P1, P2, P3)>
4612void *ReturnClosure3(P1 p1, P2 p2, P3 p3) {
4613 F(p1, p2, p3);
4614 return p1;
4615}
4616
Josh Habermane8ed0212015-06-08 17:56:03 -07004617/* Function wrapper that munges the return value from R to void*. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004618template <class R, class P1, class P2, R F(P1, P2)>
4619void *CastReturnToVoidPtr2(P1 p1, P2 p2) {
4620 return F(p1, p2);
4621}
4622
4623template <class R, class P1, class P2, class P3, R F(P1, P2, P3)>
4624void *CastReturnToVoidPtr3(P1 p1, P2 p2, P3 p3) {
4625 return F(p1, p2, p3);
4626}
4627
Josh Habermane8ed0212015-06-08 17:56:03 -07004628/* Function wrapper that munges the return value from bool to void*. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004629template <class P1, class P2, bool F(P1, P2)>
4630void *ReturnClosureOrBreak2(P1 p1, P2 p2) {
4631 return F(p1, p2) ? p1 : UPB_BREAK;
4632}
4633
4634template <class P1, class P2, class P3, bool F(P1, P2, P3)>
4635void *ReturnClosureOrBreak3(P1 p1, P2 p2, P3 p3) {
4636 return F(p1, p2, p3) ? p1 : UPB_BREAK;
4637}
4638
Josh Habermane8ed0212015-06-08 17:56:03 -07004639/* For the string callback, which takes five params, returns the size param. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004640template <class P1, class P2,
4641 void F(P1, P2, const char *, size_t, const BufferHandle *)>
4642size_t ReturnStringLen(P1 p1, P2 p2, const char *p3, size_t p4,
4643 const BufferHandle *p5) {
4644 F(p1, p2, p3, p4, p5);
4645 return p4;
4646}
4647
Josh Habermane8ed0212015-06-08 17:56:03 -07004648/* For the string callback, which takes five params, returns the size param or
4649 * zero. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004650template <class P1, class P2,
4651 bool F(P1, P2, const char *, size_t, const BufferHandle *)>
4652size_t ReturnNOr0(P1 p1, P2 p2, const char *p3, size_t p4,
4653 const BufferHandle *p5) {
4654 return F(p1, p2, p3, p4, p5) ? p4 : 0;
4655}
4656
Josh Habermane8ed0212015-06-08 17:56:03 -07004657/* If we have a function returning void but want a function returning bool, wrap
4658 * it in a function that returns true. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004659template <class P1, class P2, void F(P1, P2), class I>
4660struct MaybeWrapReturn<Func2<void, P1, P2, F, I>, bool> {
4661 typedef Func2<bool, P1, P2, ReturnTrue2<P1, P2, F>, I> Func;
4662};
4663
4664template <class P1, class P2, class P3, void F(P1, P2, P3), class I>
4665struct MaybeWrapReturn<Func3<void, P1, P2, P3, F, I>, bool> {
4666 typedef Func3<bool, P1, P2, P3, ReturnTrue3<P1, P2, P3, F>, I> Func;
4667};
4668
Josh Habermane8ed0212015-06-08 17:56:03 -07004669/* If our function returns void but we want one returning void*, wrap it in a
4670 * function that returns the first argument. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004671template <class P1, class P2, void F(P1, P2), class I>
4672struct MaybeWrapReturn<Func2<void, P1, P2, F, I>, void *> {
4673 typedef Func2<void *, P1, P2, ReturnClosure2<P1, P2, F>, I> Func;
4674};
4675
4676template <class P1, class P2, class P3, void F(P1, P2, P3), class I>
4677struct MaybeWrapReturn<Func3<void, P1, P2, P3, F, I>, void *> {
4678 typedef Func3<void *, P1, P2, P3, ReturnClosure3<P1, P2, P3, F>, I> Func;
4679};
4680
Josh Habermane8ed0212015-06-08 17:56:03 -07004681/* If our function returns R* but we want one returning void*, wrap it in a
4682 * function that casts to void*. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004683template <class R, class P1, class P2, R *F(P1, P2), class I>
4684struct MaybeWrapReturn<Func2<R *, P1, P2, F, I>, void *,
4685 typename disable_if_same<R *, void *>::Type> {
4686 typedef Func2<void *, P1, P2, CastReturnToVoidPtr2<R *, P1, P2, F>, I> Func;
4687};
4688
4689template <class R, class P1, class P2, class P3, R *F(P1, P2, P3), class I>
4690struct MaybeWrapReturn<Func3<R *, P1, P2, P3, F, I>, void *,
4691 typename disable_if_same<R *, void *>::Type> {
4692 typedef Func3<void *, P1, P2, P3, CastReturnToVoidPtr3<R *, P1, P2, P3, F>, I>
4693 Func;
4694};
4695
Josh Habermane8ed0212015-06-08 17:56:03 -07004696/* If our function returns bool but we want one returning void*, wrap it in a
4697 * function that returns either the first param or UPB_BREAK. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004698template <class P1, class P2, bool F(P1, P2), class I>
4699struct MaybeWrapReturn<Func2<bool, P1, P2, F, I>, void *> {
4700 typedef Func2<void *, P1, P2, ReturnClosureOrBreak2<P1, P2, F>, I> Func;
4701};
4702
4703template <class P1, class P2, class P3, bool F(P1, P2, P3), class I>
4704struct MaybeWrapReturn<Func3<bool, P1, P2, P3, F, I>, void *> {
4705 typedef Func3<void *, P1, P2, P3, ReturnClosureOrBreak3<P1, P2, P3, F>, I>
4706 Func;
4707};
4708
Josh Habermane8ed0212015-06-08 17:56:03 -07004709/* If our function returns void but we want one returning size_t, wrap it in a
4710 * function that returns the size argument. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004711template <class P1, class P2,
4712 void F(P1, P2, const char *, size_t, const BufferHandle *), class I>
4713struct MaybeWrapReturn<
4714 Func5<void, P1, P2, const char *, size_t, const BufferHandle *, F, I>,
4715 size_t> {
4716 typedef Func5<size_t, P1, P2, const char *, size_t, const BufferHandle *,
4717 ReturnStringLen<P1, P2, F>, I> Func;
4718};
4719
Josh Habermane8ed0212015-06-08 17:56:03 -07004720/* If our function returns bool but we want one returning size_t, wrap it in a
4721 * function that returns either 0 or the buf size. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004722template <class P1, class P2,
4723 bool F(P1, P2, const char *, size_t, const BufferHandle *), class I>
4724struct MaybeWrapReturn<
4725 Func5<bool, P1, P2, const char *, size_t, const BufferHandle *, F, I>,
4726 size_t> {
4727 typedef Func5<size_t, P1, P2, const char *, size_t, const BufferHandle *,
4728 ReturnNOr0<P1, P2, F>, I> Func;
4729};
4730
Josh Habermane8ed0212015-06-08 17:56:03 -07004731/* ConvertParams **************************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -08004732
Josh Habermane8ed0212015-06-08 17:56:03 -07004733/* Template class that converts the function parameters if necessary, and
4734 * ignores the HandlerData parameter if appropriate.
4735 *
4736 * Template parameter is the are FuncN function type. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004737template <class F, class T>
4738struct ConvertParams;
4739
Josh Habermane8ed0212015-06-08 17:56:03 -07004740/* Function that discards the handler data parameter. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004741template <class R, class P1, R F(P1)>
4742R IgnoreHandlerData2(void *p1, const void *hd) {
4743 UPB_UNUSED(hd);
4744 return F(static_cast<P1>(p1));
4745}
4746
4747template <class R, class P1, class P2Wrapper, class P2Wrapped,
4748 R F(P1, P2Wrapped)>
4749R IgnoreHandlerData3(void *p1, const void *hd, P2Wrapper p2) {
4750 UPB_UNUSED(hd);
4751 return F(static_cast<P1>(p1), p2);
4752}
4753
4754template <class R, class P1, class P2, class P3, R F(P1, P2, P3)>
4755R IgnoreHandlerData4(void *p1, const void *hd, P2 p2, P3 p3) {
4756 UPB_UNUSED(hd);
4757 return F(static_cast<P1>(p1), p2, p3);
4758}
4759
4760template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4)>
4761R IgnoreHandlerData5(void *p1, const void *hd, P2 p2, P3 p3, P4 p4) {
4762 UPB_UNUSED(hd);
4763 return F(static_cast<P1>(p1), p2, p3, p4);
4764}
4765
4766template <class R, class P1, R F(P1, const char*, size_t)>
4767R IgnoreHandlerDataIgnoreHandle(void *p1, const void *hd, const char *p2,
4768 size_t p3, const BufferHandle *handle) {
4769 UPB_UNUSED(hd);
4770 UPB_UNUSED(handle);
4771 return F(static_cast<P1>(p1), p2, p3);
4772}
4773
Josh Habermane8ed0212015-06-08 17:56:03 -07004774/* Function that casts the handler data parameter. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004775template <class R, class P1, class P2, R F(P1, P2)>
4776R CastHandlerData2(void *c, const void *hd) {
4777 return F(static_cast<P1>(c), static_cast<P2>(hd));
4778}
4779
4780template <class R, class P1, class P2, class P3Wrapper, class P3Wrapped,
4781 R F(P1, P2, P3Wrapped)>
4782R CastHandlerData3(void *c, const void *hd, P3Wrapper p3) {
4783 return F(static_cast<P1>(c), static_cast<P2>(hd), p3);
4784}
4785
4786template <class R, class P1, class P2, class P3, class P4, class P5,
4787 R F(P1, P2, P3, P4, P5)>
4788R CastHandlerData5(void *c, const void *hd, P3 p3, P4 p4, P5 p5) {
4789 return F(static_cast<P1>(c), static_cast<P2>(hd), p3, p4, p5);
4790}
4791
4792template <class R, class P1, class P2, R F(P1, P2, const char *, size_t)>
4793R CastHandlerDataIgnoreHandle(void *c, const void *hd, const char *p3,
4794 size_t p4, const BufferHandle *handle) {
4795 UPB_UNUSED(handle);
4796 return F(static_cast<P1>(c), static_cast<P2>(hd), p3, p4);
4797}
4798
Josh Habermane8ed0212015-06-08 17:56:03 -07004799/* For unbound functions, ignore the handler data. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004800template <class R, class P1, R F(P1), class I, class T>
4801struct ConvertParams<Func1<R, P1, F, I>, T> {
4802 typedef Func2<R, void *, const void *, IgnoreHandlerData2<R, P1, F>, I> Func;
4803};
4804
4805template <class R, class P1, class P2, R F(P1, P2), class I,
4806 class R2, class P1_2, class P2_2, class P3_2>
4807struct ConvertParams<Func2<R, P1, P2, F, I>,
4808 R2 (*)(P1_2, P2_2, P3_2)> {
4809 typedef Func3<R, void *, const void *, P3_2,
4810 IgnoreHandlerData3<R, P1, P3_2, P2, F>, I> Func;
4811};
4812
Josh Habermane8ed0212015-06-08 17:56:03 -07004813/* For StringBuffer only; this ignores both the handler data and the
4814 * BufferHandle. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004815template <class R, class P1, R F(P1, const char *, size_t), class I, class T>
4816struct ConvertParams<Func3<R, P1, const char *, size_t, F, I>, T> {
4817 typedef Func5<R, void *, const void *, const char *, size_t,
4818 const BufferHandle *, IgnoreHandlerDataIgnoreHandle<R, P1, F>,
4819 I> Func;
4820};
4821
4822template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
4823 class I, class T>
4824struct ConvertParams<Func4<R, P1, P2, P3, P4, F, I>, T> {
4825 typedef Func5<R, void *, const void *, P2, P3, P4,
4826 IgnoreHandlerData5<R, P1, P2, P3, P4, F>, I> Func;
4827};
4828
Josh Habermane8ed0212015-06-08 17:56:03 -07004829/* For bound functions, cast the handler data. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004830template <class R, class P1, class P2, R F(P1, P2), class I, class T>
4831struct ConvertParams<BoundFunc2<R, P1, P2, F, I>, T> {
4832 typedef Func2<R, void *, const void *, CastHandlerData2<R, P1, P2, F>, I>
4833 Func;
4834};
4835
4836template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I,
4837 class R2, class P1_2, class P2_2, class P3_2>
4838struct ConvertParams<BoundFunc3<R, P1, P2, P3, F, I>,
4839 R2 (*)(P1_2, P2_2, P3_2)> {
4840 typedef Func3<R, void *, const void *, P3_2,
4841 CastHandlerData3<R, P1, P2, P3_2, P3, F>, I> Func;
4842};
4843
Josh Habermane8ed0212015-06-08 17:56:03 -07004844/* For StringBuffer only; this ignores the BufferHandle. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004845template <class R, class P1, class P2, R F(P1, P2, const char *, size_t),
4846 class I, class T>
4847struct ConvertParams<BoundFunc4<R, P1, P2, const char *, size_t, F, I>, T> {
4848 typedef Func5<R, void *, const void *, const char *, size_t,
4849 const BufferHandle *, CastHandlerDataIgnoreHandle<R, P1, P2, F>,
4850 I> Func;
4851};
4852
4853template <class R, class P1, class P2, class P3, class P4, class P5,
4854 R F(P1, P2, P3, P4, P5), class I, class T>
4855struct ConvertParams<BoundFunc5<R, P1, P2, P3, P4, P5, F, I>, T> {
4856 typedef Func5<R, void *, const void *, P3, P4, P5,
4857 CastHandlerData5<R, P1, P2, P3, P4, P5, F>, I> Func;
4858};
4859
Josh Habermane8ed0212015-06-08 17:56:03 -07004860/* utype/ltype are upper/lower-case, ctype is canonical C type, vtype is
4861 * variant C type. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004862#define TYPE_METHODS(utype, ltype, ctype, vtype) \
4863 template <> struct CanonicalType<vtype> { \
4864 typedef ctype Type; \
4865 }; \
4866 template <> \
4867 inline bool Handlers::SetValueHandler<vtype>( \
4868 const FieldDef *f, \
4869 const Handlers::utype ## Handler& handler) { \
4870 assert(!handler.registered_); \
4871 handler.AddCleanup(this); \
4872 handler.registered_ = true; \
4873 return upb_handlers_set##ltype(this, f, handler.handler_, &handler.attr_); \
4874 } \
4875
Josh Habermane8ed0212015-06-08 17:56:03 -07004876TYPE_METHODS(Double, double, double, double)
4877TYPE_METHODS(Float, float, float, float)
4878TYPE_METHODS(UInt64, uint64, uint64_t, UPB_UINT64_T)
4879TYPE_METHODS(UInt32, uint32, uint32_t, UPB_UINT32_T)
4880TYPE_METHODS(Int64, int64, int64_t, UPB_INT64_T)
4881TYPE_METHODS(Int32, int32, int32_t, UPB_INT32_T)
4882TYPE_METHODS(Bool, bool, bool, bool)
Chris Fallin91473dc2014-12-12 15:58:26 -08004883
4884#ifdef UPB_TWO_32BIT_TYPES
Josh Habermane8ed0212015-06-08 17:56:03 -07004885TYPE_METHODS(Int32, int32, int32_t, UPB_INT32ALT_T)
4886TYPE_METHODS(UInt32, uint32, uint32_t, UPB_UINT32ALT_T)
Chris Fallin91473dc2014-12-12 15:58:26 -08004887#endif
4888
4889#ifdef UPB_TWO_64BIT_TYPES
Josh Habermane8ed0212015-06-08 17:56:03 -07004890TYPE_METHODS(Int64, int64, int64_t, UPB_INT64ALT_T)
4891TYPE_METHODS(UInt64, uint64, uint64_t, UPB_UINT64ALT_T)
Chris Fallin91473dc2014-12-12 15:58:26 -08004892#endif
4893#undef TYPE_METHODS
4894
4895template <> struct CanonicalType<Status*> {
4896 typedef Status* Type;
4897};
4898
Josh Habermane8ed0212015-06-08 17:56:03 -07004899/* Type methods that are only one-per-canonical-type and not
4900 * one-per-cvariant. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004901
4902#define TYPE_METHODS(utype, ctype) \
4903 inline bool Handlers::Set##utype##Handler(const FieldDef *f, \
4904 const utype##Handler &h) { \
4905 return SetValueHandler<ctype>(f, h); \
4906 } \
4907
Josh Habermane8ed0212015-06-08 17:56:03 -07004908TYPE_METHODS(Double, double)
4909TYPE_METHODS(Float, float)
4910TYPE_METHODS(UInt64, uint64_t)
4911TYPE_METHODS(UInt32, uint32_t)
4912TYPE_METHODS(Int64, int64_t)
4913TYPE_METHODS(Int32, int32_t)
4914TYPE_METHODS(Bool, bool)
Chris Fallin91473dc2014-12-12 15:58:26 -08004915#undef TYPE_METHODS
4916
4917template <class F> struct ReturnOf;
4918
4919template <class R, class P1, class P2>
4920struct ReturnOf<R (*)(P1, P2)> {
4921 typedef R Return;
4922};
4923
4924template <class R, class P1, class P2, class P3>
4925struct ReturnOf<R (*)(P1, P2, P3)> {
4926 typedef R Return;
4927};
4928
4929template <class R, class P1, class P2, class P3, class P4>
4930struct ReturnOf<R (*)(P1, P2, P3, P4)> {
4931 typedef R Return;
4932};
4933
4934template <class R, class P1, class P2, class P3, class P4, class P5>
4935struct ReturnOf<R (*)(P1, P2, P3, P4, P5)> {
4936 typedef R Return;
4937};
4938
4939template<class T> const void *UniquePtrForType() {
4940 static const char ch = 0;
4941 return &ch;
4942}
4943
4944template <class T>
4945template <class F>
4946inline Handler<T>::Handler(F func)
4947 : registered_(false),
4948 cleanup_data_(func.GetData()),
4949 cleanup_func_(func.GetCleanup()) {
4950 upb_handlerattr_sethandlerdata(&attr_, func.GetData());
4951 typedef typename ReturnOf<T>::Return Return;
4952 typedef typename ConvertParams<F, T>::Func ConvertedParamsFunc;
4953 typedef typename MaybeWrapReturn<ConvertedParamsFunc, Return>::Func
4954 ReturnWrappedFunc;
4955 handler_ = ReturnWrappedFunc().Call;
4956
Josh Habermane8ed0212015-06-08 17:56:03 -07004957 /* Set attributes based on what templates can statically tell us about the
4958 * user's function. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004959
Josh Habermane8ed0212015-06-08 17:56:03 -07004960 /* If the original function returns void, then we know that we wrapped it to
4961 * always return ok. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004962 bool always_ok = is_same<typename F::FuncInfo::Return, void>::value;
4963 attr_.SetAlwaysOk(always_ok);
4964
Josh Habermane8ed0212015-06-08 17:56:03 -07004965 /* Closure parameter and return type. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004966 attr_.SetClosureType(UniquePtrForType<typename F::FuncInfo::Closure>());
4967
Josh Habermane8ed0212015-06-08 17:56:03 -07004968 /* We use the closure type (from the first parameter) if the return type is
4969 * void or bool, since these are the two cases we wrap to return the closure's
4970 * type anyway.
4971 *
4972 * This is all nonsense for non START* handlers, but it doesn't matter because
4973 * in that case the value will be ignored. */
Chris Fallind3262772015-05-14 18:24:26 -07004974 typedef typename FirstUnlessVoidOrBool<typename F::FuncInfo::Return,
4975 typename F::FuncInfo::Closure>::value
Chris Fallin91473dc2014-12-12 15:58:26 -08004976 EffectiveReturn;
4977 attr_.SetReturnClosureType(UniquePtrForType<EffectiveReturn>());
4978}
4979
4980template <class T>
4981inline Handler<T>::~Handler() {
4982 assert(registered_);
4983}
4984
4985inline HandlerAttributes::HandlerAttributes() { upb_handlerattr_init(this); }
4986inline HandlerAttributes::~HandlerAttributes() { upb_handlerattr_uninit(this); }
4987inline bool HandlerAttributes::SetHandlerData(const void *hd) {
4988 return upb_handlerattr_sethandlerdata(this, hd);
4989}
4990inline const void* HandlerAttributes::handler_data() const {
4991 return upb_handlerattr_handlerdata(this);
4992}
4993inline bool HandlerAttributes::SetClosureType(const void *type) {
4994 return upb_handlerattr_setclosuretype(this, type);
4995}
4996inline const void* HandlerAttributes::closure_type() const {
4997 return upb_handlerattr_closuretype(this);
4998}
4999inline bool HandlerAttributes::SetReturnClosureType(const void *type) {
5000 return upb_handlerattr_setreturnclosuretype(this, type);
5001}
5002inline const void* HandlerAttributes::return_closure_type() const {
5003 return upb_handlerattr_returnclosuretype(this);
5004}
5005inline bool HandlerAttributes::SetAlwaysOk(bool always_ok) {
5006 return upb_handlerattr_setalwaysok(this, always_ok);
5007}
5008inline bool HandlerAttributes::always_ok() const {
5009 return upb_handlerattr_alwaysok(this);
5010}
5011
5012inline BufferHandle::BufferHandle() { upb_bufhandle_init(this); }
5013inline BufferHandle::~BufferHandle() { upb_bufhandle_uninit(this); }
5014inline const char* BufferHandle::buffer() const {
5015 return upb_bufhandle_buf(this);
5016}
5017inline size_t BufferHandle::object_offset() const {
5018 return upb_bufhandle_objofs(this);
5019}
5020inline void BufferHandle::SetBuffer(const char* buf, size_t ofs) {
5021 upb_bufhandle_setbuf(this, buf, ofs);
5022}
5023template <class T>
5024void BufferHandle::SetAttachedObject(const T* obj) {
5025 upb_bufhandle_setobj(this, obj, UniquePtrForType<T>());
5026}
5027template <class T>
5028const T* BufferHandle::GetAttachedObject() const {
5029 return upb_bufhandle_objtype(this) == UniquePtrForType<T>()
5030 ? static_cast<const T *>(upb_bufhandle_obj(this))
5031 : NULL;
5032}
5033
5034inline reffed_ptr<Handlers> Handlers::New(const MessageDef *m) {
5035 upb_handlers *h = upb_handlers_new(m, &h);
5036 return reffed_ptr<Handlers>(h, &h);
5037}
5038inline reffed_ptr<const Handlers> Handlers::NewFrozen(
5039 const MessageDef *m, upb_handlers_callback *callback,
5040 const void *closure) {
5041 const upb_handlers *h = upb_handlers_newfrozen(m, &h, callback, closure);
5042 return reffed_ptr<const Handlers>(h, &h);
5043}
Chris Fallin91473dc2014-12-12 15:58:26 -08005044inline const Status* Handlers::status() {
5045 return upb_handlers_status(this);
5046}
5047inline void Handlers::ClearError() {
5048 return upb_handlers_clearerr(this);
5049}
5050inline bool Handlers::Freeze(Status *s) {
5051 upb::Handlers* h = this;
5052 return upb_handlers_freeze(&h, 1, s);
5053}
5054inline bool Handlers::Freeze(Handlers *const *handlers, int n, Status *s) {
5055 return upb_handlers_freeze(handlers, n, s);
5056}
5057inline bool Handlers::Freeze(const std::vector<Handlers*>& h, Status* status) {
5058 return upb_handlers_freeze((Handlers* const*)&h[0], h.size(), status);
5059}
5060inline const MessageDef *Handlers::message_def() const {
5061 return upb_handlers_msgdef(this);
5062}
5063inline bool Handlers::AddCleanup(void *p, upb_handlerfree *func) {
5064 return upb_handlers_addcleanup(this, p, func);
5065}
5066inline bool Handlers::SetStartMessageHandler(
5067 const Handlers::StartMessageHandler &handler) {
5068 assert(!handler.registered_);
5069 handler.registered_ = true;
5070 handler.AddCleanup(this);
5071 return upb_handlers_setstartmsg(this, handler.handler_, &handler.attr_);
5072}
5073inline bool Handlers::SetEndMessageHandler(
5074 const Handlers::EndMessageHandler &handler) {
5075 assert(!handler.registered_);
5076 handler.registered_ = true;
5077 handler.AddCleanup(this);
5078 return upb_handlers_setendmsg(this, handler.handler_, &handler.attr_);
5079}
5080inline bool Handlers::SetStartStringHandler(const FieldDef *f,
5081 const StartStringHandler &handler) {
5082 assert(!handler.registered_);
5083 handler.registered_ = true;
5084 handler.AddCleanup(this);
5085 return upb_handlers_setstartstr(this, f, handler.handler_, &handler.attr_);
5086}
5087inline bool Handlers::SetEndStringHandler(const FieldDef *f,
5088 const EndFieldHandler &handler) {
5089 assert(!handler.registered_);
5090 handler.registered_ = true;
5091 handler.AddCleanup(this);
5092 return upb_handlers_setendstr(this, f, handler.handler_, &handler.attr_);
5093}
5094inline bool Handlers::SetStringHandler(const FieldDef *f,
5095 const StringHandler& handler) {
5096 assert(!handler.registered_);
5097 handler.registered_ = true;
5098 handler.AddCleanup(this);
5099 return upb_handlers_setstring(this, f, handler.handler_, &handler.attr_);
5100}
5101inline bool Handlers::SetStartSequenceHandler(
5102 const FieldDef *f, const StartFieldHandler &handler) {
5103 assert(!handler.registered_);
5104 handler.registered_ = true;
5105 handler.AddCleanup(this);
5106 return upb_handlers_setstartseq(this, f, handler.handler_, &handler.attr_);
5107}
5108inline bool Handlers::SetStartSubMessageHandler(
5109 const FieldDef *f, const StartFieldHandler &handler) {
5110 assert(!handler.registered_);
5111 handler.registered_ = true;
5112 handler.AddCleanup(this);
5113 return upb_handlers_setstartsubmsg(this, f, handler.handler_, &handler.attr_);
5114}
5115inline bool Handlers::SetEndSubMessageHandler(const FieldDef *f,
5116 const EndFieldHandler &handler) {
5117 assert(!handler.registered_);
5118 handler.registered_ = true;
5119 handler.AddCleanup(this);
5120 return upb_handlers_setendsubmsg(this, f, handler.handler_, &handler.attr_);
5121}
5122inline bool Handlers::SetEndSequenceHandler(const FieldDef *f,
5123 const EndFieldHandler &handler) {
5124 assert(!handler.registered_);
5125 handler.registered_ = true;
5126 handler.AddCleanup(this);
5127 return upb_handlers_setendseq(this, f, handler.handler_, &handler.attr_);
5128}
5129inline bool Handlers::SetSubHandlers(const FieldDef *f, const Handlers *sub) {
5130 return upb_handlers_setsubhandlers(this, f, sub);
5131}
5132inline const Handlers *Handlers::GetSubHandlers(const FieldDef *f) const {
5133 return upb_handlers_getsubhandlers(this, f);
5134}
5135inline const Handlers *Handlers::GetSubHandlers(Handlers::Selector sel) const {
5136 return upb_handlers_getsubhandlers_sel(this, sel);
5137}
5138inline bool Handlers::GetSelector(const FieldDef *f, Handlers::Type type,
5139 Handlers::Selector *s) {
5140 return upb_handlers_getselector(f, type, s);
5141}
5142inline Handlers::Selector Handlers::GetEndSelector(Handlers::Selector start) {
5143 return upb_handlers_getendselector(start);
5144}
5145inline Handlers::GenericFunction *Handlers::GetHandler(
5146 Handlers::Selector selector) {
5147 return upb_handlers_gethandler(this, selector);
5148}
5149inline const void *Handlers::GetHandlerData(Handlers::Selector selector) {
5150 return upb_handlers_gethandlerdata(this, selector);
5151}
5152
5153inline BytesHandler::BytesHandler() {
5154 upb_byteshandler_init(this);
5155}
5156
Chris Fallind3262772015-05-14 18:24:26 -07005157inline BytesHandler::~BytesHandler() {}
Chris Fallin91473dc2014-12-12 15:58:26 -08005158
Josh Habermane8ed0212015-06-08 17:56:03 -07005159} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08005160
Josh Habermane8ed0212015-06-08 17:56:03 -07005161#endif /* __cplusplus */
Chris Fallin91473dc2014-12-12 15:58:26 -08005162
5163
5164#undef UPB_TWO_32BIT_TYPES
5165#undef UPB_TWO_64BIT_TYPES
5166#undef UPB_INT32_T
5167#undef UPB_UINT32_T
5168#undef UPB_INT32ALT_T
5169#undef UPB_UINT32ALT_T
5170#undef UPB_INT64_T
5171#undef UPB_UINT64_T
5172#undef UPB_INT64ALT_T
5173#undef UPB_UINT64ALT_T
5174
Josh Habermane8ed0212015-06-08 17:56:03 -07005175#endif /* UPB_HANDLERS_INL_H_ */
Chris Fallin91473dc2014-12-12 15:58:26 -08005176
Josh Habermane8ed0212015-06-08 17:56:03 -07005177#endif /* UPB_HANDLERS_H */
Chris Fallin91473dc2014-12-12 15:58:26 -08005178/*
Josh Haberman181c7f22015-07-15 11:05:10 -07005179** upb::Environment (upb_env)
5180**
5181** A upb::Environment provides a means for injecting malloc and an
5182** error-reporting callback into encoders/decoders. This allows them to be
5183** independent of nearly all assumptions about their actual environment.
5184**
5185** It is also a container for allocating the encoders/decoders themselves that
5186** insulates clients from knowing their actual size. This provides ABI
5187** compatibility even if the size of the objects change. And this allows the
5188** structure definitions to be in the .c files instead of the .h files, making
5189** the .h files smaller and more readable.
5190*/
Chris Fallind3262772015-05-14 18:24:26 -07005191
5192
5193#ifndef UPB_ENV_H_
5194#define UPB_ENV_H_
5195
5196#ifdef __cplusplus
5197namespace upb {
5198class Environment;
5199class SeededAllocator;
5200}
5201#endif
5202
Josh Habermane8ed0212015-06-08 17:56:03 -07005203UPB_DECLARE_TYPE(upb::Environment, upb_env)
5204UPB_DECLARE_TYPE(upb::SeededAllocator, upb_seededalloc)
Chris Fallind3262772015-05-14 18:24:26 -07005205
5206typedef void *upb_alloc_func(void *ud, void *ptr, size_t oldsize, size_t size);
5207typedef void upb_cleanup_func(void *ud);
5208typedef bool upb_error_func(void *ud, const upb_status *status);
5209
Josh Habermane8ed0212015-06-08 17:56:03 -07005210#ifdef __cplusplus
5211
5212/* An environment is *not* thread-safe. */
5213class upb::Environment {
Chris Fallind3262772015-05-14 18:24:26 -07005214 public:
5215 Environment();
5216 ~Environment();
5217
Josh Habermane8ed0212015-06-08 17:56:03 -07005218 /* Set a custom memory allocation function for the environment. May ONLY
5219 * be called before any calls to Malloc()/Realloc()/AddCleanup() below.
5220 * If this is not called, the system realloc() function will be used.
5221 * The given user pointer "ud" will be passed to the allocation function.
5222 *
5223 * The allocation function will not receive corresponding "free" calls. it
5224 * must ensure that the memory is valid for the lifetime of the Environment,
5225 * but it may be reclaimed any time thereafter. The likely usage is that
5226 * "ud" points to a stateful allocator, and that the allocator frees all
5227 * memory, arena-style, when it is destroyed. In this case the allocator must
5228 * outlive the Environment. Another possibility is that the allocation
5229 * function returns GC-able memory that is guaranteed to be GC-rooted for the
5230 * life of the Environment. */
Chris Fallind3262772015-05-14 18:24:26 -07005231 void SetAllocationFunction(upb_alloc_func* alloc, void* ud);
5232
5233 template<class T>
5234 void SetAllocator(T* allocator) {
5235 SetAllocationFunction(allocator->GetAllocationFunction(), allocator);
5236 }
5237
Josh Habermane8ed0212015-06-08 17:56:03 -07005238 /* Set a custom error reporting function. */
Chris Fallind3262772015-05-14 18:24:26 -07005239 void SetErrorFunction(upb_error_func* func, void* ud);
5240
Josh Habermane8ed0212015-06-08 17:56:03 -07005241 /* Set the error reporting function to simply copy the status to the given
5242 * status and abort. */
Chris Fallind3262772015-05-14 18:24:26 -07005243 void ReportErrorsTo(Status* status);
5244
Josh Habermane8ed0212015-06-08 17:56:03 -07005245 /* Returns true if all allocations and AddCleanup() calls have succeeded,
5246 * and no errors were reported with ReportError() (except ones that recovered
5247 * successfully). */
Chris Fallind3262772015-05-14 18:24:26 -07005248 bool ok() const;
5249
Josh Habermane8ed0212015-06-08 17:56:03 -07005250 /* Functions for use by encoders/decoders. **********************************/
Chris Fallind3262772015-05-14 18:24:26 -07005251
Josh Habermane8ed0212015-06-08 17:56:03 -07005252 /* Reports an error to this environment's callback, returning true if
5253 * the caller should try to recover. */
Chris Fallind3262772015-05-14 18:24:26 -07005254 bool ReportError(const Status* status);
5255
Josh Habermane8ed0212015-06-08 17:56:03 -07005256 /* Allocate memory. Uses the environment's allocation function.
5257 *
5258 * There is no need to free(). All memory will be freed automatically, but is
5259 * guaranteed to outlive the Environment. */
Chris Fallind3262772015-05-14 18:24:26 -07005260 void* Malloc(size_t size);
5261
Josh Habermane8ed0212015-06-08 17:56:03 -07005262 /* Reallocate memory. Preserves "oldsize" bytes from the existing buffer
5263 * Requires: oldsize <= existing_size.
5264 *
5265 * TODO(haberman): should we also enforce that oldsize <= size? */
Chris Fallind3262772015-05-14 18:24:26 -07005266 void* Realloc(void* ptr, size_t oldsize, size_t size);
5267
Josh Habermane8ed0212015-06-08 17:56:03 -07005268 /* Add a cleanup function to run when the environment is destroyed.
5269 * Returns false on out-of-memory.
5270 *
5271 * The first call to AddCleanup() after SetAllocationFunction() is guaranteed
5272 * to return true -- this makes it possible to robustly set a cleanup handler
5273 * for a custom allocation function. */
Chris Fallind3262772015-05-14 18:24:26 -07005274 bool AddCleanup(upb_cleanup_func* func, void* ud);
5275
Josh Habermane8ed0212015-06-08 17:56:03 -07005276 /* Total number of bytes that have been allocated. It is undefined what
5277 * Realloc() does to this counter. */
Chris Fallind3262772015-05-14 18:24:26 -07005278 size_t BytesAllocated() const;
5279
5280 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07005281 UPB_DISALLOW_COPY_AND_ASSIGN(Environment)
5282
5283#else
5284struct upb_env {
5285#endif /* __cplusplus */
5286
Chris Fallind3262772015-05-14 18:24:26 -07005287 bool ok_;
5288 size_t bytes_allocated;
5289
Josh Habermane8ed0212015-06-08 17:56:03 -07005290 /* Alloc function. */
Chris Fallind3262772015-05-14 18:24:26 -07005291 upb_alloc_func *alloc;
5292 void *alloc_ud;
5293
Josh Habermane8ed0212015-06-08 17:56:03 -07005294 /* Error-reporting function. */
Chris Fallind3262772015-05-14 18:24:26 -07005295 upb_error_func *err;
5296 void *err_ud;
5297
Josh Habermane8ed0212015-06-08 17:56:03 -07005298 /* Userdata for default alloc func. */
Chris Fallind3262772015-05-14 18:24:26 -07005299 void *default_alloc_ud;
5300
Josh Habermane8ed0212015-06-08 17:56:03 -07005301 /* Cleanup entries. Pointer to a cleanup_ent, defined in env.c */
Chris Fallind3262772015-05-14 18:24:26 -07005302 void *cleanup_head;
5303
Josh Habermane8ed0212015-06-08 17:56:03 -07005304 /* For future expansion, since the size of this struct is exposed to users. */
Chris Fallind3262772015-05-14 18:24:26 -07005305 void *future1;
5306 void *future2;
Josh Habermane8ed0212015-06-08 17:56:03 -07005307};
Chris Fallind3262772015-05-14 18:24:26 -07005308
5309UPB_BEGIN_EXTERN_C
5310
5311void upb_env_init(upb_env *e);
5312void upb_env_uninit(upb_env *e);
5313void upb_env_setallocfunc(upb_env *e, upb_alloc_func *func, void *ud);
5314void upb_env_seterrorfunc(upb_env *e, upb_error_func *func, void *ud);
5315void upb_env_reporterrorsto(upb_env *e, upb_status *status);
5316bool upb_env_ok(const upb_env *e);
5317bool upb_env_reporterror(upb_env *e, const upb_status *status);
5318void *upb_env_malloc(upb_env *e, size_t size);
5319void *upb_env_realloc(upb_env *e, void *ptr, size_t oldsize, size_t size);
5320bool upb_env_addcleanup(upb_env *e, upb_cleanup_func *func, void *ud);
5321size_t upb_env_bytesallocated(const upb_env *e);
5322
5323UPB_END_EXTERN_C
5324
Josh Habermane8ed0212015-06-08 17:56:03 -07005325#ifdef __cplusplus
5326
5327/* An allocator that allocates from an initial memory region (likely the stack)
5328 * before falling back to another allocator. */
5329class upb::SeededAllocator {
Chris Fallind3262772015-05-14 18:24:26 -07005330 public:
5331 SeededAllocator(void *mem, size_t len);
5332 ~SeededAllocator();
5333
Josh Habermane8ed0212015-06-08 17:56:03 -07005334 /* Set a custom fallback memory allocation function for the allocator, to use
5335 * once the initial region runs out.
5336 *
5337 * May ONLY be called before GetAllocationFunction(). If this is not
5338 * called, the system realloc() will be the fallback allocator. */
Chris Fallind3262772015-05-14 18:24:26 -07005339 void SetFallbackAllocator(upb_alloc_func *alloc, void *ud);
5340
Josh Habermane8ed0212015-06-08 17:56:03 -07005341 /* Gets the allocation function for this allocator. */
Chris Fallind3262772015-05-14 18:24:26 -07005342 upb_alloc_func* GetAllocationFunction();
5343
5344 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07005345 UPB_DISALLOW_COPY_AND_ASSIGN(SeededAllocator)
5346
5347#else
5348struct upb_seededalloc {
5349#endif /* __cplusplus */
5350
5351 /* Fallback alloc function. */
Chris Fallind3262772015-05-14 18:24:26 -07005352 upb_alloc_func *alloc;
5353 upb_cleanup_func *alloc_cleanup;
5354 void *alloc_ud;
5355 bool need_cleanup;
5356 bool returned_allocfunc;
5357
Josh Habermane8ed0212015-06-08 17:56:03 -07005358 /* Userdata for default alloc func. */
Chris Fallind3262772015-05-14 18:24:26 -07005359 void *default_alloc_ud;
5360
Josh Habermane8ed0212015-06-08 17:56:03 -07005361 /* Pointers for the initial memory region. */
Chris Fallind3262772015-05-14 18:24:26 -07005362 char *mem_base;
5363 char *mem_ptr;
5364 char *mem_limit;
5365
Josh Habermane8ed0212015-06-08 17:56:03 -07005366 /* For future expansion, since the size of this struct is exposed to users. */
Chris Fallind3262772015-05-14 18:24:26 -07005367 void *future1;
5368 void *future2;
Josh Habermane8ed0212015-06-08 17:56:03 -07005369};
Chris Fallind3262772015-05-14 18:24:26 -07005370
5371UPB_BEGIN_EXTERN_C
5372
5373void upb_seededalloc_init(upb_seededalloc *a, void *mem, size_t len);
5374void upb_seededalloc_uninit(upb_seededalloc *a);
5375void upb_seededalloc_setfallbackalloc(upb_seededalloc *a, upb_alloc_func *func,
5376 void *ud);
5377upb_alloc_func *upb_seededalloc_getallocfunc(upb_seededalloc *a);
5378
5379UPB_END_EXTERN_C
5380
5381#ifdef __cplusplus
5382
5383namespace upb {
5384
5385inline Environment::Environment() {
5386 upb_env_init(this);
5387}
5388inline Environment::~Environment() {
5389 upb_env_uninit(this);
5390}
5391inline void Environment::SetAllocationFunction(upb_alloc_func *alloc,
5392 void *ud) {
5393 upb_env_setallocfunc(this, alloc, ud);
5394}
5395inline void Environment::SetErrorFunction(upb_error_func *func, void *ud) {
5396 upb_env_seterrorfunc(this, func, ud);
5397}
5398inline void Environment::ReportErrorsTo(Status* status) {
5399 upb_env_reporterrorsto(this, status);
5400}
5401inline bool Environment::ok() const {
5402 return upb_env_ok(this);
5403}
5404inline bool Environment::ReportError(const Status* status) {
5405 return upb_env_reporterror(this, status);
5406}
5407inline void *Environment::Malloc(size_t size) {
5408 return upb_env_malloc(this, size);
5409}
5410inline void *Environment::Realloc(void *ptr, size_t oldsize, size_t size) {
5411 return upb_env_realloc(this, ptr, oldsize, size);
5412}
5413inline bool Environment::AddCleanup(upb_cleanup_func *func, void *ud) {
5414 return upb_env_addcleanup(this, func, ud);
5415}
5416inline size_t Environment::BytesAllocated() const {
5417 return upb_env_bytesallocated(this);
5418}
5419
5420inline SeededAllocator::SeededAllocator(void *mem, size_t len) {
5421 upb_seededalloc_init(this, mem, len);
5422}
5423inline SeededAllocator::~SeededAllocator() {
5424 upb_seededalloc_uninit(this);
5425}
5426inline void SeededAllocator::SetFallbackAllocator(upb_alloc_func *alloc,
5427 void *ud) {
5428 upb_seededalloc_setfallbackalloc(this, alloc, ud);
5429}
5430inline upb_alloc_func *SeededAllocator::GetAllocationFunction() {
5431 return upb_seededalloc_getallocfunc(this);
5432}
5433
Josh Habermane8ed0212015-06-08 17:56:03 -07005434} /* namespace upb */
Chris Fallind3262772015-05-14 18:24:26 -07005435
Josh Habermane8ed0212015-06-08 17:56:03 -07005436#endif /* __cplusplus */
Chris Fallind3262772015-05-14 18:24:26 -07005437
Josh Habermane8ed0212015-06-08 17:56:03 -07005438#endif /* UPB_ENV_H_ */
Chris Fallind3262772015-05-14 18:24:26 -07005439/*
Josh Haberman181c7f22015-07-15 11:05:10 -07005440** upb::Sink (upb_sink)
5441** upb::BytesSink (upb_bytessink)
5442**
5443** A upb_sink is an object that binds a upb_handlers object to some runtime
5444** state. It is the object that can actually receive data via the upb_handlers
5445** interface.
5446**
5447** Unlike upb_def and upb_handlers, upb_sink is never frozen, immutable, or
5448** thread-safe. You can create as many of them as you want, but each one may
5449** only be used in a single thread at a time.
5450**
5451** If we compare with class-based OOP, a you can think of a upb_def as an
5452** abstract base class, a upb_handlers as a concrete derived class, and a
5453** upb_sink as an object (class instance).
5454*/
Chris Fallin91473dc2014-12-12 15:58:26 -08005455
5456#ifndef UPB_SINK_H
5457#define UPB_SINK_H
5458
5459
5460#ifdef __cplusplus
5461namespace upb {
5462class BufferSource;
5463class BytesSink;
5464class Sink;
5465}
5466#endif
5467
Josh Habermane8ed0212015-06-08 17:56:03 -07005468UPB_DECLARE_TYPE(upb::BufferSource, upb_bufsrc)
5469UPB_DECLARE_TYPE(upb::BytesSink, upb_bytessink)
5470UPB_DECLARE_TYPE(upb::Sink, upb_sink)
Chris Fallin91473dc2014-12-12 15:58:26 -08005471
Josh Habermane8ed0212015-06-08 17:56:03 -07005472#ifdef __cplusplus
5473
5474/* A upb::Sink is an object that binds a upb::Handlers object to some runtime
5475 * state. It represents an endpoint to which data can be sent.
5476 *
5477 * TODO(haberman): right now all of these functions take selectors. Should they
5478 * take selectorbase instead?
5479 *
5480 * ie. instead of calling:
5481 * sink->StartString(FOO_FIELD_START_STRING, ...)
5482 * a selector base would let you say:
5483 * sink->StartString(FOO_FIELD, ...)
5484 *
5485 * This would make call sites a little nicer and require emitting fewer selector
5486 * definitions in .h files.
5487 *
5488 * But the current scheme has the benefit that you can retrieve a function
5489 * pointer for any handler with handlers->GetHandler(selector), without having
5490 * to have a separate GetHandler() function for each handler type. The JIT
5491 * compiler uses this. To accommodate we'd have to expose a separate
5492 * GetHandler() for every handler type.
5493 *
5494 * Also to ponder: selectors right now are independent of a specific Handlers
5495 * instance. In other words, they allocate a number to every possible handler
5496 * that *could* be registered, without knowing anything about what handlers
5497 * *are* registered. That means that using selectors as table offsets prohibits
5498 * us from compacting the handler table at Freeze() time. If the table is very
5499 * sparse, this could be wasteful.
5500 *
5501 * Having another selector-like thing that is specific to a Handlers instance
5502 * would allow this compacting, but then it would be impossible to write code
5503 * ahead-of-time that can be bound to any Handlers instance at runtime. For
5504 * example, a .proto file parser written as straight C will not know what
5505 * Handlers it will be bound to, so when it calls sink->StartString() what
5506 * selector will it pass? It needs a selector like we have today, that is
5507 * independent of any particular upb::Handlers.
5508 *
5509 * Is there a way then to allow Handlers table compaction? */
5510class upb::Sink {
Chris Fallin91473dc2014-12-12 15:58:26 -08005511 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07005512 /* Constructor with no initialization; must be Reset() before use. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005513 Sink() {}
5514
Josh Habermane8ed0212015-06-08 17:56:03 -07005515 /* Constructs a new sink for the given frozen handlers and closure.
5516 *
5517 * TODO: once the Handlers know the expected closure type, verify that T
5518 * matches it. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005519 template <class T> Sink(const Handlers* handlers, T* closure);
5520
Josh Habermane8ed0212015-06-08 17:56:03 -07005521 /* Resets the value of the sink. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005522 template <class T> void Reset(const Handlers* handlers, T* closure);
5523
Josh Habermane8ed0212015-06-08 17:56:03 -07005524 /* Returns the top-level object that is bound to this sink.
5525 *
5526 * TODO: once the Handlers know the expected closure type, verify that T
5527 * matches it. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005528 template <class T> T* GetObject() const;
5529
Josh Habermane8ed0212015-06-08 17:56:03 -07005530 /* Functions for pushing data into the sink.
5531 *
5532 * These return false if processing should stop (either due to error or just
5533 * to suspend).
5534 *
5535 * These may not be called from within one of the same sink's handlers (in
5536 * other words, handlers are not re-entrant). */
Chris Fallin91473dc2014-12-12 15:58:26 -08005537
Josh Habermane8ed0212015-06-08 17:56:03 -07005538 /* Should be called at the start and end of every message; both the top-level
5539 * message and submessages. This means that submessages should use the
5540 * following sequence:
5541 * sink->StartSubMessage(startsubmsg_selector);
5542 * sink->StartMessage();
5543 * // ...
5544 * sink->EndMessage(&status);
5545 * sink->EndSubMessage(endsubmsg_selector); */
Chris Fallin91473dc2014-12-12 15:58:26 -08005546 bool StartMessage();
5547 bool EndMessage(Status* status);
5548
Josh Habermane8ed0212015-06-08 17:56:03 -07005549 /* Putting of individual values. These work for both repeated and
5550 * non-repeated fields, but for repeated fields you must wrap them in
5551 * calls to StartSequence()/EndSequence(). */
Chris Fallin91473dc2014-12-12 15:58:26 -08005552 bool PutInt32(Handlers::Selector s, int32_t val);
5553 bool PutInt64(Handlers::Selector s, int64_t val);
5554 bool PutUInt32(Handlers::Selector s, uint32_t val);
5555 bool PutUInt64(Handlers::Selector s, uint64_t val);
5556 bool PutFloat(Handlers::Selector s, float val);
5557 bool PutDouble(Handlers::Selector s, double val);
5558 bool PutBool(Handlers::Selector s, bool val);
5559
Josh Habermane8ed0212015-06-08 17:56:03 -07005560 /* Putting of string/bytes values. Each string can consist of zero or more
5561 * non-contiguous buffers of data.
5562 *
5563 * For StartString(), the function will write a sink for the string to "sub."
5564 * The sub-sink must be used for any/all PutStringBuffer() calls. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005565 bool StartString(Handlers::Selector s, size_t size_hint, Sink* sub);
5566 size_t PutStringBuffer(Handlers::Selector s, const char *buf, size_t len,
5567 const BufferHandle *handle);
5568 bool EndString(Handlers::Selector s);
5569
Josh Habermane8ed0212015-06-08 17:56:03 -07005570 /* For submessage fields.
5571 *
5572 * For StartSubMessage(), the function will write a sink for the string to
5573 * "sub." The sub-sink must be used for any/all handlers called within the
5574 * submessage. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005575 bool StartSubMessage(Handlers::Selector s, Sink* sub);
5576 bool EndSubMessage(Handlers::Selector s);
5577
Josh Habermane8ed0212015-06-08 17:56:03 -07005578 /* For repeated fields of any type, the sequence of values must be wrapped in
5579 * these calls.
5580 *
5581 * For StartSequence(), the function will write a sink for the string to
5582 * "sub." The sub-sink must be used for any/all handlers called within the
5583 * sequence. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005584 bool StartSequence(Handlers::Selector s, Sink* sub);
5585 bool EndSequence(Handlers::Selector s);
5586
Josh Habermane8ed0212015-06-08 17:56:03 -07005587 /* Copy and assign specifically allowed.
5588 * We don't even bother making these members private because so many
5589 * functions need them and this is mainly just a dumb data container anyway.
5590 */
5591#else
5592struct upb_sink {
5593#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08005594 const upb_handlers *handlers;
5595 void *closure;
Josh Habermane8ed0212015-06-08 17:56:03 -07005596};
Chris Fallin91473dc2014-12-12 15:58:26 -08005597
Josh Habermane8ed0212015-06-08 17:56:03 -07005598#ifdef __cplusplus
5599class upb::BytesSink {
Chris Fallin91473dc2014-12-12 15:58:26 -08005600 public:
5601 BytesSink() {}
5602
Josh Habermane8ed0212015-06-08 17:56:03 -07005603 /* Constructs a new sink for the given frozen handlers and closure.
5604 *
5605 * TODO(haberman): once the Handlers know the expected closure type, verify
5606 * that T matches it. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005607 template <class T> BytesSink(const BytesHandler* handler, T* closure);
5608
Josh Habermane8ed0212015-06-08 17:56:03 -07005609 /* Resets the value of the sink. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005610 template <class T> void Reset(const BytesHandler* handler, T* closure);
5611
5612 bool Start(size_t size_hint, void **subc);
5613 size_t PutBuffer(void *subc, const char *buf, size_t len,
5614 const BufferHandle *handle);
5615 bool End();
Josh Habermane8ed0212015-06-08 17:56:03 -07005616#else
5617struct upb_bytessink {
5618#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08005619 const upb_byteshandler *handler;
5620 void *closure;
Josh Habermane8ed0212015-06-08 17:56:03 -07005621};
Chris Fallin91473dc2014-12-12 15:58:26 -08005622
Josh Habermane8ed0212015-06-08 17:56:03 -07005623#ifdef __cplusplus
5624
5625/* A class for pushing a flat buffer of data to a BytesSink.
5626 * You can construct an instance of this to get a resumable source,
5627 * or just call the static PutBuffer() to do a non-resumable push all in one
5628 * go. */
5629class upb::BufferSource {
Chris Fallin91473dc2014-12-12 15:58:26 -08005630 public:
5631 BufferSource();
5632 BufferSource(const char* buf, size_t len, BytesSink* sink);
5633
Josh Habermane8ed0212015-06-08 17:56:03 -07005634 /* Returns true if the entire buffer was pushed successfully. Otherwise the
5635 * next call to PutNext() will resume where the previous one left off.
5636 * TODO(haberman): implement this. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005637 bool PutNext();
5638
Josh Habermane8ed0212015-06-08 17:56:03 -07005639 /* A static version; with this version is it not possible to resume in the
5640 * case of failure or a partially-consumed buffer. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005641 static bool PutBuffer(const char* buf, size_t len, BytesSink* sink);
5642
5643 template <class T> static bool PutBuffer(const T& str, BytesSink* sink) {
5644 return PutBuffer(str.c_str(), str.size(), sink);
5645 }
Josh Habermane8ed0212015-06-08 17:56:03 -07005646#else
5647struct upb_bufsrc {
5648 char dummy;
5649#endif
5650};
Chris Fallin91473dc2014-12-12 15:58:26 -08005651
Josh Habermane8ed0212015-06-08 17:56:03 -07005652UPB_BEGIN_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08005653
Josh Habermane8ed0212015-06-08 17:56:03 -07005654/* Inline definitions. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005655
5656UPB_INLINE void upb_bytessink_reset(upb_bytessink *s, const upb_byteshandler *h,
5657 void *closure) {
5658 s->handler = h;
5659 s->closure = closure;
5660}
5661
5662UPB_INLINE bool upb_bytessink_start(upb_bytessink *s, size_t size_hint,
5663 void **subc) {
Josh Habermane8ed0212015-06-08 17:56:03 -07005664 typedef upb_startstr_handlerfunc func;
5665 func *start;
Chris Fallin91473dc2014-12-12 15:58:26 -08005666 *subc = s->closure;
5667 if (!s->handler) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005668 start = (func *)s->handler->table[UPB_STARTSTR_SELECTOR].func;
Chris Fallin91473dc2014-12-12 15:58:26 -08005669
5670 if (!start) return true;
5671 *subc = start(s->closure, upb_handlerattr_handlerdata(
5672 &s->handler->table[UPB_STARTSTR_SELECTOR].attr),
5673 size_hint);
5674 return *subc != NULL;
5675}
5676
5677UPB_INLINE size_t upb_bytessink_putbuf(upb_bytessink *s, void *subc,
5678 const char *buf, size_t size,
5679 const upb_bufhandle* handle) {
Josh Habermane8ed0212015-06-08 17:56:03 -07005680 typedef upb_string_handlerfunc func;
5681 func *putbuf;
Chris Fallin91473dc2014-12-12 15:58:26 -08005682 if (!s->handler) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005683 putbuf = (func *)s->handler->table[UPB_STRING_SELECTOR].func;
Chris Fallin91473dc2014-12-12 15:58:26 -08005684
5685 if (!putbuf) return true;
5686 return putbuf(subc, upb_handlerattr_handlerdata(
5687 &s->handler->table[UPB_STRING_SELECTOR].attr),
5688 buf, size, handle);
5689}
5690
5691UPB_INLINE bool upb_bytessink_end(upb_bytessink *s) {
Josh Habermane8ed0212015-06-08 17:56:03 -07005692 typedef upb_endfield_handlerfunc func;
5693 func *end;
Chris Fallin91473dc2014-12-12 15:58:26 -08005694 if (!s->handler) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005695 end = (func *)s->handler->table[UPB_ENDSTR_SELECTOR].func;
Chris Fallin91473dc2014-12-12 15:58:26 -08005696
5697 if (!end) return true;
5698 return end(s->closure,
5699 upb_handlerattr_handlerdata(
5700 &s->handler->table[UPB_ENDSTR_SELECTOR].attr));
5701}
5702
5703UPB_INLINE bool upb_bufsrc_putbuf(const char *buf, size_t len,
5704 upb_bytessink *sink) {
5705 void *subc;
Josh Habermane8ed0212015-06-08 17:56:03 -07005706 bool ret;
Chris Fallin91473dc2014-12-12 15:58:26 -08005707 upb_bufhandle handle;
5708 upb_bufhandle_init(&handle);
5709 upb_bufhandle_setbuf(&handle, buf, 0);
Josh Habermane8ed0212015-06-08 17:56:03 -07005710 ret = upb_bytessink_start(sink, len, &subc);
Chris Fallin91473dc2014-12-12 15:58:26 -08005711 if (ret && len != 0) {
Josh Haberman5bdf4a42015-08-03 15:51:31 -07005712 ret = (upb_bytessink_putbuf(sink, subc, buf, len, &handle) >= len);
Chris Fallin91473dc2014-12-12 15:58:26 -08005713 }
5714 if (ret) {
5715 ret = upb_bytessink_end(sink);
5716 }
5717 upb_bufhandle_uninit(&handle);
5718 return ret;
5719}
5720
5721#define PUTVAL(type, ctype) \
5722 UPB_INLINE bool upb_sink_put##type(upb_sink *s, upb_selector_t sel, \
5723 ctype val) { \
Josh Habermane8ed0212015-06-08 17:56:03 -07005724 typedef upb_##type##_handlerfunc functype; \
5725 functype *func; \
5726 const void *hd; \
Chris Fallin91473dc2014-12-12 15:58:26 -08005727 if (!s->handlers) return true; \
Josh Habermane8ed0212015-06-08 17:56:03 -07005728 func = (functype *)upb_handlers_gethandler(s->handlers, sel); \
Chris Fallin91473dc2014-12-12 15:58:26 -08005729 if (!func) return true; \
Josh Habermane8ed0212015-06-08 17:56:03 -07005730 hd = upb_handlers_gethandlerdata(s->handlers, sel); \
Chris Fallin91473dc2014-12-12 15:58:26 -08005731 return func(s->closure, hd, val); \
5732 }
5733
Josh Habermane8ed0212015-06-08 17:56:03 -07005734PUTVAL(int32, int32_t)
5735PUTVAL(int64, int64_t)
5736PUTVAL(uint32, uint32_t)
5737PUTVAL(uint64, uint64_t)
5738PUTVAL(float, float)
5739PUTVAL(double, double)
5740PUTVAL(bool, bool)
Chris Fallin91473dc2014-12-12 15:58:26 -08005741#undef PUTVAL
5742
5743UPB_INLINE void upb_sink_reset(upb_sink *s, const upb_handlers *h, void *c) {
5744 s->handlers = h;
5745 s->closure = c;
5746}
5747
5748UPB_INLINE size_t upb_sink_putstring(upb_sink *s, upb_selector_t sel,
5749 const char *buf, size_t n,
5750 const upb_bufhandle *handle) {
Josh Habermane8ed0212015-06-08 17:56:03 -07005751 typedef upb_string_handlerfunc func;
5752 func *handler;
5753 const void *hd;
Chris Fallin91473dc2014-12-12 15:58:26 -08005754 if (!s->handlers) return n;
Josh Habermane8ed0212015-06-08 17:56:03 -07005755 handler = (func *)upb_handlers_gethandler(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08005756
5757 if (!handler) return n;
Josh Habermane8ed0212015-06-08 17:56:03 -07005758 hd = upb_handlers_gethandlerdata(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08005759 return handler(s->closure, hd, buf, n, handle);
5760}
5761
5762UPB_INLINE bool upb_sink_startmsg(upb_sink *s) {
Josh Habermane8ed0212015-06-08 17:56:03 -07005763 typedef upb_startmsg_handlerfunc func;
5764 func *startmsg;
5765 const void *hd;
Chris Fallin91473dc2014-12-12 15:58:26 -08005766 if (!s->handlers) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005767 startmsg = (func*)upb_handlers_gethandler(s->handlers, UPB_STARTMSG_SELECTOR);
5768
Chris Fallin91473dc2014-12-12 15:58:26 -08005769 if (!startmsg) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005770 hd = upb_handlers_gethandlerdata(s->handlers, UPB_STARTMSG_SELECTOR);
Chris Fallin91473dc2014-12-12 15:58:26 -08005771 return startmsg(s->closure, hd);
5772}
5773
5774UPB_INLINE bool upb_sink_endmsg(upb_sink *s, upb_status *status) {
Josh Habermane8ed0212015-06-08 17:56:03 -07005775 typedef upb_endmsg_handlerfunc func;
5776 func *endmsg;
5777 const void *hd;
Chris Fallin91473dc2014-12-12 15:58:26 -08005778 if (!s->handlers) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005779 endmsg = (func *)upb_handlers_gethandler(s->handlers, UPB_ENDMSG_SELECTOR);
Chris Fallin91473dc2014-12-12 15:58:26 -08005780
5781 if (!endmsg) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005782 hd = upb_handlers_gethandlerdata(s->handlers, UPB_ENDMSG_SELECTOR);
Chris Fallin91473dc2014-12-12 15:58:26 -08005783 return endmsg(s->closure, hd, status);
5784}
5785
5786UPB_INLINE bool upb_sink_startseq(upb_sink *s, upb_selector_t sel,
5787 upb_sink *sub) {
Josh Habermane8ed0212015-06-08 17:56:03 -07005788 typedef upb_startfield_handlerfunc func;
5789 func *startseq;
5790 const void *hd;
Chris Fallin91473dc2014-12-12 15:58:26 -08005791 sub->closure = s->closure;
5792 sub->handlers = s->handlers;
5793 if (!s->handlers) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005794 startseq = (func*)upb_handlers_gethandler(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08005795
5796 if (!startseq) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005797 hd = upb_handlers_gethandlerdata(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08005798 sub->closure = startseq(s->closure, hd);
5799 return sub->closure ? true : false;
5800}
5801
5802UPB_INLINE bool upb_sink_endseq(upb_sink *s, upb_selector_t sel) {
Josh Habermane8ed0212015-06-08 17:56:03 -07005803 typedef upb_endfield_handlerfunc func;
5804 func *endseq;
5805 const void *hd;
Chris Fallin91473dc2014-12-12 15:58:26 -08005806 if (!s->handlers) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005807 endseq = (func*)upb_handlers_gethandler(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08005808
5809 if (!endseq) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005810 hd = upb_handlers_gethandlerdata(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08005811 return endseq(s->closure, hd);
5812}
5813
5814UPB_INLINE bool upb_sink_startstr(upb_sink *s, upb_selector_t sel,
5815 size_t size_hint, upb_sink *sub) {
Josh Habermane8ed0212015-06-08 17:56:03 -07005816 typedef upb_startstr_handlerfunc func;
5817 func *startstr;
5818 const void *hd;
Chris Fallin91473dc2014-12-12 15:58:26 -08005819 sub->closure = s->closure;
5820 sub->handlers = s->handlers;
5821 if (!s->handlers) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005822 startstr = (func*)upb_handlers_gethandler(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08005823
5824 if (!startstr) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005825 hd = upb_handlers_gethandlerdata(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08005826 sub->closure = startstr(s->closure, hd, size_hint);
5827 return sub->closure ? true : false;
5828}
5829
5830UPB_INLINE bool upb_sink_endstr(upb_sink *s, upb_selector_t sel) {
Josh Habermane8ed0212015-06-08 17:56:03 -07005831 typedef upb_endfield_handlerfunc func;
5832 func *endstr;
5833 const void *hd;
Chris Fallin91473dc2014-12-12 15:58:26 -08005834 if (!s->handlers) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005835 endstr = (func*)upb_handlers_gethandler(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08005836
5837 if (!endstr) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005838 hd = upb_handlers_gethandlerdata(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08005839 return endstr(s->closure, hd);
5840}
5841
5842UPB_INLINE bool upb_sink_startsubmsg(upb_sink *s, upb_selector_t sel,
5843 upb_sink *sub) {
Josh Habermane8ed0212015-06-08 17:56:03 -07005844 typedef upb_startfield_handlerfunc func;
5845 func *startsubmsg;
5846 const void *hd;
Chris Fallin91473dc2014-12-12 15:58:26 -08005847 sub->closure = s->closure;
5848 if (!s->handlers) {
5849 sub->handlers = NULL;
5850 return true;
5851 }
5852 sub->handlers = upb_handlers_getsubhandlers_sel(s->handlers, sel);
Josh Habermane8ed0212015-06-08 17:56:03 -07005853 startsubmsg = (func*)upb_handlers_gethandler(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08005854
5855 if (!startsubmsg) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005856 hd = upb_handlers_gethandlerdata(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08005857 sub->closure = startsubmsg(s->closure, hd);
5858 return sub->closure ? true : false;
5859}
5860
5861UPB_INLINE bool upb_sink_endsubmsg(upb_sink *s, upb_selector_t sel) {
Josh Habermane8ed0212015-06-08 17:56:03 -07005862 typedef upb_endfield_handlerfunc func;
5863 func *endsubmsg;
5864 const void *hd;
Chris Fallin91473dc2014-12-12 15:58:26 -08005865 if (!s->handlers) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005866 endsubmsg = (func*)upb_handlers_gethandler(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08005867
5868 if (!endsubmsg) return s->closure;
Josh Habermane8ed0212015-06-08 17:56:03 -07005869 hd = upb_handlers_gethandlerdata(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08005870 return endsubmsg(s->closure, hd);
5871}
5872
Josh Habermane8ed0212015-06-08 17:56:03 -07005873UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08005874
5875#ifdef __cplusplus
5876
5877namespace upb {
5878
5879template <class T> Sink::Sink(const Handlers* handlers, T* closure) {
5880 upb_sink_reset(this, handlers, closure);
5881}
5882template <class T>
5883inline void Sink::Reset(const Handlers* handlers, T* closure) {
5884 upb_sink_reset(this, handlers, closure);
5885}
5886inline bool Sink::StartMessage() {
5887 return upb_sink_startmsg(this);
5888}
5889inline bool Sink::EndMessage(Status* status) {
5890 return upb_sink_endmsg(this, status);
5891}
5892inline bool Sink::PutInt32(Handlers::Selector sel, int32_t val) {
5893 return upb_sink_putint32(this, sel, val);
5894}
5895inline bool Sink::PutInt64(Handlers::Selector sel, int64_t val) {
5896 return upb_sink_putint64(this, sel, val);
5897}
5898inline bool Sink::PutUInt32(Handlers::Selector sel, uint32_t val) {
5899 return upb_sink_putuint32(this, sel, val);
5900}
5901inline bool Sink::PutUInt64(Handlers::Selector sel, uint64_t val) {
5902 return upb_sink_putuint64(this, sel, val);
5903}
5904inline bool Sink::PutFloat(Handlers::Selector sel, float val) {
5905 return upb_sink_putfloat(this, sel, val);
5906}
5907inline bool Sink::PutDouble(Handlers::Selector sel, double val) {
5908 return upb_sink_putdouble(this, sel, val);
5909}
5910inline bool Sink::PutBool(Handlers::Selector sel, bool val) {
5911 return upb_sink_putbool(this, sel, val);
5912}
5913inline bool Sink::StartString(Handlers::Selector sel, size_t size_hint,
5914 Sink *sub) {
5915 return upb_sink_startstr(this, sel, size_hint, sub);
5916}
5917inline size_t Sink::PutStringBuffer(Handlers::Selector sel, const char *buf,
5918 size_t len, const BufferHandle* handle) {
5919 return upb_sink_putstring(this, sel, buf, len, handle);
5920}
5921inline bool Sink::EndString(Handlers::Selector sel) {
5922 return upb_sink_endstr(this, sel);
5923}
5924inline bool Sink::StartSubMessage(Handlers::Selector sel, Sink* sub) {
5925 return upb_sink_startsubmsg(this, sel, sub);
5926}
5927inline bool Sink::EndSubMessage(Handlers::Selector sel) {
5928 return upb_sink_endsubmsg(this, sel);
5929}
5930inline bool Sink::StartSequence(Handlers::Selector sel, Sink* sub) {
5931 return upb_sink_startseq(this, sel, sub);
5932}
5933inline bool Sink::EndSequence(Handlers::Selector sel) {
5934 return upb_sink_endseq(this, sel);
5935}
5936
5937template <class T>
5938BytesSink::BytesSink(const BytesHandler* handler, T* closure) {
5939 Reset(handler, closure);
5940}
5941
5942template <class T>
5943void BytesSink::Reset(const BytesHandler *handler, T *closure) {
5944 upb_bytessink_reset(this, handler, closure);
5945}
5946inline bool BytesSink::Start(size_t size_hint, void **subc) {
5947 return upb_bytessink_start(this, size_hint, subc);
5948}
5949inline size_t BytesSink::PutBuffer(void *subc, const char *buf, size_t len,
5950 const BufferHandle *handle) {
5951 return upb_bytessink_putbuf(this, subc, buf, len, handle);
5952}
5953inline bool BytesSink::End() {
5954 return upb_bytessink_end(this);
5955}
5956
5957inline bool BufferSource::PutBuffer(const char *buf, size_t len,
5958 BytesSink *sink) {
5959 return upb_bufsrc_putbuf(buf, len, sink);
5960}
5961
Josh Habermane8ed0212015-06-08 17:56:03 -07005962} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08005963#endif
5964
5965#endif
5966/*
Josh Haberman181c7f22015-07-15 11:05:10 -07005967** For handlers that do very tiny, very simple operations, the function call
5968** overhead of calling a handler can be significant. This file allows the
5969** user to define handlers that do something very simple like store the value
5970** to memory and/or set a hasbit. JIT compilers can then special-case these
5971** handlers and emit specialized code for them instead of actually calling the
5972** handler.
5973**
5974** The functionality is very simple/limited right now but may expand to be able
5975** to call another function.
5976*/
Chris Fallin91473dc2014-12-12 15:58:26 -08005977
5978#ifndef UPB_SHIM_H
5979#define UPB_SHIM_H
5980
5981
5982typedef struct {
5983 size_t offset;
5984 int32_t hasbit;
5985} upb_shim_data;
5986
5987#ifdef __cplusplus
5988
5989namespace upb {
5990
5991struct Shim {
5992 typedef upb_shim_data Data;
5993
Josh Habermane8ed0212015-06-08 17:56:03 -07005994 /* Sets a handler for the given field that writes the value to the given
5995 * offset and, if hasbit >= 0, sets a bit at the given bit offset. Returns
5996 * true if the handler was set successfully. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005997 static bool Set(Handlers *h, const FieldDef *f, size_t ofs, int32_t hasbit);
5998
Josh Habermane8ed0212015-06-08 17:56:03 -07005999 /* If this handler is a shim, returns the corresponding upb::Shim::Data and
6000 * stores the type in "type". Otherwise returns NULL. */
Chris Fallin91473dc2014-12-12 15:58:26 -08006001 static const Data* GetData(const Handlers* h, Handlers::Selector s,
6002 FieldDef::Type* type);
6003};
6004
Josh Habermane8ed0212015-06-08 17:56:03 -07006005} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08006006
6007#endif
6008
Josh Habermane8ed0212015-06-08 17:56:03 -07006009UPB_BEGIN_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08006010
Josh Habermane8ed0212015-06-08 17:56:03 -07006011/* C API. */
Chris Fallin91473dc2014-12-12 15:58:26 -08006012bool upb_shim_set(upb_handlers *h, const upb_fielddef *f, size_t offset,
6013 int32_t hasbit);
6014const upb_shim_data *upb_shim_getdata(const upb_handlers *h, upb_selector_t s,
6015 upb_fieldtype_t *type);
6016
Josh Habermane8ed0212015-06-08 17:56:03 -07006017UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08006018
6019#ifdef __cplusplus
Josh Habermane8ed0212015-06-08 17:56:03 -07006020/* C++ Wrappers. */
Chris Fallin91473dc2014-12-12 15:58:26 -08006021namespace upb {
6022inline bool Shim::Set(Handlers* h, const FieldDef* f, size_t ofs,
6023 int32_t hasbit) {
6024 return upb_shim_set(h, f, ofs, hasbit);
6025}
6026inline const Shim::Data* Shim::GetData(const Handlers* h, Handlers::Selector s,
6027 FieldDef::Type* type) {
6028 return upb_shim_getdata(h, s, type);
6029}
Josh Habermane8ed0212015-06-08 17:56:03 -07006030} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08006031#endif
6032
Josh Habermane8ed0212015-06-08 17:56:03 -07006033#endif /* UPB_SHIM_H */
6034/*
Josh Haberman181c7f22015-07-15 11:05:10 -07006035** upb::SymbolTable (upb_symtab)
6036**
6037** A symtab (symbol table) stores a name->def map of upb_defs. Clients could
6038** always create such tables themselves, but upb_symtab has logic for resolving
6039** symbolic references, and in particular, for keeping a whole set of consistent
6040** defs when replacing some subset of those defs. This logic is nontrivial.
6041**
6042** This is a mixed C/C++ interface that offers a full API to both languages.
6043** See the top-level README for more information.
6044*/
Josh Habermane8ed0212015-06-08 17:56:03 -07006045
6046#ifndef UPB_SYMTAB_H_
6047#define UPB_SYMTAB_H_
6048
6049
6050#ifdef __cplusplus
6051#include <vector>
6052namespace upb { class SymbolTable; }
6053#endif
6054
6055UPB_DECLARE_DERIVED_TYPE(upb::SymbolTable, upb::RefCounted,
6056 upb_symtab, upb_refcounted)
6057
6058typedef struct {
6059 UPB_PRIVATE_FOR_CPP
6060 upb_strtable_iter iter;
6061 upb_deftype_t type;
6062} upb_symtab_iter;
6063
6064#ifdef __cplusplus
6065
6066/* Non-const methods in upb::SymbolTable are NOT thread-safe. */
6067class upb::SymbolTable {
6068 public:
6069 /* Returns a new symbol table with a single ref owned by "owner."
6070 * Returns NULL if memory allocation failed. */
6071 static reffed_ptr<SymbolTable> New();
6072
6073 /* Include RefCounted base methods. */
6074 UPB_REFCOUNTED_CPPMETHODS
6075
6076 /* For all lookup functions, the returned pointer is not owned by the
6077 * caller; it may be invalidated by any non-const call or unref of the
6078 * SymbolTable! To protect against this, take a ref if desired. */
6079
6080 /* Freezes the symbol table: prevents further modification of it.
6081 * After the Freeze() operation is successful, the SymbolTable must only be
6082 * accessed via a const pointer.
6083 *
6084 * Unlike with upb::MessageDef/upb::EnumDef/etc, freezing a SymbolTable is not
6085 * a necessary step in using a SymbolTable. If you have no need for it to be
6086 * immutable, there is no need to freeze it ever. However sometimes it is
6087 * useful, and SymbolTables that are statically compiled into the binary are
6088 * always frozen by nature. */
6089 void Freeze();
6090
6091 /* Resolves the given symbol using the rules described in descriptor.proto,
6092 * namely:
6093 *
6094 * If the name starts with a '.', it is fully-qualified. Otherwise,
6095 * C++-like scoping rules are used to find the type (i.e. first the nested
6096 * types within this message are searched, then within the parent, on up
6097 * to the root namespace).
6098 *
6099 * If not found, returns NULL. */
6100 const Def* Resolve(const char* base, const char* sym) const;
6101
6102 /* Finds an entry in the symbol table with this exact name. If not found,
6103 * returns NULL. */
6104 const Def* Lookup(const char *sym) const;
6105 const MessageDef* LookupMessage(const char *sym) const;
6106 const EnumDef* LookupEnum(const char *sym) const;
6107
6108 /* TODO: introduce a C++ iterator, but make it nice and templated so that if
6109 * you ask for an iterator of MessageDef the iterated elements are strongly
6110 * typed as MessageDef*. */
6111
6112 /* Adds the given mutable defs to the symtab, resolving all symbols
6113 * (including enum default values) and finalizing the defs. Only one def per
6114 * name may be in the list, but defs can replace existing defs in the symtab.
6115 * All defs must have a name -- anonymous defs are not allowed. Anonymous
6116 * defs can still be frozen by calling upb_def_freeze() directly.
6117 *
6118 * Any existing defs that can reach defs that are being replaced will
6119 * themselves be replaced also, so that the resulting set of defs is fully
6120 * consistent.
6121 *
6122 * This logic implemented in this method is a convenience; ultimately it
6123 * calls some combination of upb_fielddef_setsubdef(), upb_def_dup(), and
6124 * upb_freeze(), any of which the client could call themself. However, since
6125 * the logic for doing so is nontrivial, we provide it here.
6126 *
6127 * The entire operation either succeeds or fails. If the operation fails,
6128 * the symtab is unchanged, false is returned, and status indicates the
6129 * error. The caller passes a ref on all defs to the symtab (even if the
6130 * operation fails).
6131 *
6132 * TODO(haberman): currently failure will leave the symtab unchanged, but may
6133 * leave the defs themselves partially resolved. Does this matter? If so we
6134 * could do a prepass that ensures that all symbols are resolvable and bail
6135 * if not, so we don't mutate anything until we know the operation will
6136 * succeed.
6137 *
6138 * TODO(haberman): since the defs must be mutable, refining a frozen def
6139 * requires making mutable copies of the entire tree. This is wasteful if
6140 * only a few messages are changing. We may want to add a way of adding a
6141 * tree of frozen defs to the symtab (perhaps an alternate constructor where
6142 * you pass the root of the tree?) */
6143 bool Add(Def*const* defs, int n, void* ref_donor, upb_status* status);
6144
6145 bool Add(const std::vector<Def*>& defs, void *owner, Status* status) {
6146 return Add((Def*const*)&defs[0], defs.size(), owner, status);
6147 }
6148
6149 private:
6150 UPB_DISALLOW_POD_OPS(SymbolTable, upb::SymbolTable)
6151};
6152
6153#endif /* __cplusplus */
6154
6155UPB_BEGIN_EXTERN_C
6156
6157/* Native C API. */
6158
6159/* Include refcounted methods like upb_symtab_ref(). */
6160UPB_REFCOUNTED_CMETHODS(upb_symtab, upb_symtab_upcast)
6161
6162upb_symtab *upb_symtab_new(const void *owner);
6163void upb_symtab_freeze(upb_symtab *s);
6164const upb_def *upb_symtab_resolve(const upb_symtab *s, const char *base,
6165 const char *sym);
6166const upb_def *upb_symtab_lookup(const upb_symtab *s, const char *sym);
6167const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym);
6168const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym);
6169bool upb_symtab_add(upb_symtab *s, upb_def *const*defs, int n, void *ref_donor,
6170 upb_status *status);
6171
6172/* upb_symtab_iter i;
6173 * for(upb_symtab_begin(&i, s, type); !upb_symtab_done(&i);
6174 * upb_symtab_next(&i)) {
6175 * const upb_def *def = upb_symtab_iter_def(&i);
6176 * // ...
6177 * }
6178 *
6179 * For C we don't have separate iterators for const and non-const.
6180 * It is the caller's responsibility to cast the upb_fielddef* to
6181 * const if the upb_msgdef* is const. */
6182void upb_symtab_begin(upb_symtab_iter *iter, const upb_symtab *s,
6183 upb_deftype_t type);
6184void upb_symtab_next(upb_symtab_iter *iter);
6185bool upb_symtab_done(const upb_symtab_iter *iter);
6186const upb_def *upb_symtab_iter_def(const upb_symtab_iter *iter);
6187
6188UPB_END_EXTERN_C
6189
6190#ifdef __cplusplus
6191/* C++ inline wrappers. */
6192namespace upb {
6193inline reffed_ptr<SymbolTable> SymbolTable::New() {
6194 upb_symtab *s = upb_symtab_new(&s);
6195 return reffed_ptr<SymbolTable>(s, &s);
6196}
6197
6198inline void SymbolTable::Freeze() {
6199 return upb_symtab_freeze(this);
6200}
6201inline const Def *SymbolTable::Resolve(const char *base,
6202 const char *sym) const {
6203 return upb_symtab_resolve(this, base, sym);
6204}
6205inline const Def* SymbolTable::Lookup(const char *sym) const {
6206 return upb_symtab_lookup(this, sym);
6207}
6208inline const MessageDef *SymbolTable::LookupMessage(const char *sym) const {
6209 return upb_symtab_lookupmsg(this, sym);
6210}
6211inline bool SymbolTable::Add(
6212 Def*const* defs, int n, void* ref_donor, upb_status* status) {
6213 return upb_symtab_add(this, (upb_def*const*)defs, n, ref_donor, status);
6214}
6215} /* namespace upb */
6216#endif
6217
6218#endif /* UPB_SYMTAB_H_ */
Chris Fallin91473dc2014-12-12 15:58:26 -08006219/*
Josh Haberman181c7f22015-07-15 11:05:10 -07006220** upb::descriptor::Reader (upb_descreader)
6221**
6222** Provides a way of building upb::Defs from data in descriptor.proto format.
6223*/
Chris Fallin91473dc2014-12-12 15:58:26 -08006224
6225#ifndef UPB_DESCRIPTOR_H
6226#define UPB_DESCRIPTOR_H
6227
6228
6229#ifdef __cplusplus
6230namespace upb {
6231namespace descriptor {
6232class Reader;
Josh Habermane8ed0212015-06-08 17:56:03 -07006233} /* namespace descriptor */
6234} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08006235#endif
6236
Josh Habermane8ed0212015-06-08 17:56:03 -07006237UPB_DECLARE_TYPE(upb::descriptor::Reader, upb_descreader)
Chris Fallin91473dc2014-12-12 15:58:26 -08006238
Chris Fallind3262772015-05-14 18:24:26 -07006239#ifdef __cplusplus
Chris Fallin91473dc2014-12-12 15:58:26 -08006240
Josh Habermane8ed0212015-06-08 17:56:03 -07006241/* Class that receives descriptor data according to the descriptor.proto schema
6242 * and use it to build upb::Defs corresponding to that schema. */
Chris Fallind3262772015-05-14 18:24:26 -07006243class upb::descriptor::Reader {
Chris Fallin91473dc2014-12-12 15:58:26 -08006244 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07006245 /* These handlers must have come from NewHandlers() and must outlive the
6246 * Reader.
6247 *
6248 * TODO: generate the handlers statically (like we do with the
6249 * descriptor.proto defs) so that there is no need to pass this parameter (or
6250 * to build/memory-manage the handlers at runtime at all). Unfortunately this
6251 * is a bit tricky to implement for Handlers, but necessary to simplify this
6252 * interface. */
Chris Fallind3262772015-05-14 18:24:26 -07006253 static Reader* Create(Environment* env, const Handlers* handlers);
Chris Fallin91473dc2014-12-12 15:58:26 -08006254
Josh Habermane8ed0212015-06-08 17:56:03 -07006255 /* The reader's input; this is where descriptor.proto data should be sent. */
Chris Fallin91473dc2014-12-12 15:58:26 -08006256 Sink* input();
6257
Josh Habermane8ed0212015-06-08 17:56:03 -07006258 /* Returns an array of all defs that have been parsed, and transfers ownership
6259 * of them to "owner". The number of defs is stored in *n. Ownership of the
6260 * returned array is retained and is invalidated by any other call into
6261 * Reader.
6262 *
6263 * These defs are not frozen or resolved; they are ready to be added to a
6264 * symtab. */
Chris Fallin91473dc2014-12-12 15:58:26 -08006265 upb::Def** GetDefs(void* owner, int* n);
6266
Josh Habermane8ed0212015-06-08 17:56:03 -07006267 /* Builds and returns handlers for the reader, owned by "owner." */
Chris Fallin91473dc2014-12-12 15:58:26 -08006268 static Handlers* NewHandlers(const void* owner);
Chris Fallin91473dc2014-12-12 15:58:26 -08006269
Chris Fallind3262772015-05-14 18:24:26 -07006270 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07006271 UPB_DISALLOW_POD_OPS(Reader, upb::descriptor::Reader)
Chris Fallind3262772015-05-14 18:24:26 -07006272};
Chris Fallin91473dc2014-12-12 15:58:26 -08006273
Chris Fallind3262772015-05-14 18:24:26 -07006274#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08006275
Chris Fallind3262772015-05-14 18:24:26 -07006276UPB_BEGIN_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08006277
Josh Habermane8ed0212015-06-08 17:56:03 -07006278/* C API. */
Chris Fallind3262772015-05-14 18:24:26 -07006279upb_descreader *upb_descreader_create(upb_env *e, const upb_handlers *h);
Chris Fallin91473dc2014-12-12 15:58:26 -08006280upb_sink *upb_descreader_input(upb_descreader *r);
6281upb_def **upb_descreader_getdefs(upb_descreader *r, void *owner, int *n);
6282const upb_handlers *upb_descreader_newhandlers(const void *owner);
6283
Chris Fallind3262772015-05-14 18:24:26 -07006284UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08006285
6286#ifdef __cplusplus
Josh Habermane8ed0212015-06-08 17:56:03 -07006287/* C++ implementation details. ************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -08006288namespace upb {
6289namespace descriptor {
Chris Fallind3262772015-05-14 18:24:26 -07006290inline Reader* Reader::Create(Environment* e, const Handlers *h) {
6291 return upb_descreader_create(e, h);
Chris Fallin91473dc2014-12-12 15:58:26 -08006292}
Chris Fallin91473dc2014-12-12 15:58:26 -08006293inline Sink* Reader::input() { return upb_descreader_input(this); }
6294inline upb::Def** Reader::GetDefs(void* owner, int* n) {
6295 return upb_descreader_getdefs(this, owner, n);
6296}
Josh Habermane8ed0212015-06-08 17:56:03 -07006297} /* namespace descriptor */
6298} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08006299#endif
6300
Josh Habermane8ed0212015-06-08 17:56:03 -07006301#endif /* UPB_DESCRIPTOR_H */
6302/* This file contains accessors for a set of compiled-in defs.
6303 * Note that unlike Google's protobuf, it does *not* define
6304 * generated classes or any other kind of data structure for
6305 * actually storing protobufs. It only contains *defs* which
6306 * let you reflect over a protobuf *schema*.
6307 */
6308/* This file was generated by upbc (the upb compiler).
6309 * Do not edit -- your changes will be discarded when the file is
6310 * regenerated. */
6311
6312#ifndef GOOGLE_PROTOBUF_DESCRIPTOR_UPB_H_
6313#define GOOGLE_PROTOBUF_DESCRIPTOR_UPB_H_
6314
6315
6316#ifdef __cplusplus
6317UPB_BEGIN_EXTERN_C
6318#endif
6319
6320/* Enums */
6321
6322typedef enum {
6323 GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_OPTIONAL = 1,
6324 GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_REQUIRED = 2,
6325 GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_REPEATED = 3
6326} google_protobuf_FieldDescriptorProto_Label;
6327
6328typedef enum {
6329 GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_DOUBLE = 1,
6330 GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_FLOAT = 2,
6331 GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_INT64 = 3,
6332 GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_UINT64 = 4,
6333 GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_INT32 = 5,
6334 GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_FIXED64 = 6,
6335 GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_FIXED32 = 7,
6336 GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_BOOL = 8,
6337 GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_STRING = 9,
6338 GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_GROUP = 10,
6339 GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_MESSAGE = 11,
6340 GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_BYTES = 12,
6341 GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_UINT32 = 13,
6342 GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_ENUM = 14,
6343 GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SFIXED32 = 15,
6344 GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SFIXED64 = 16,
6345 GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SINT32 = 17,
6346 GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SINT64 = 18
6347} google_protobuf_FieldDescriptorProto_Type;
6348
6349typedef enum {
6350 GOOGLE_PROTOBUF_FIELDOPTIONS_STRING = 0,
6351 GOOGLE_PROTOBUF_FIELDOPTIONS_CORD = 1,
6352 GOOGLE_PROTOBUF_FIELDOPTIONS_STRING_PIECE = 2
6353} google_protobuf_FieldOptions_CType;
6354
6355typedef enum {
Josh Haberman78da6662016-01-13 19:05:43 -08006356 GOOGLE_PROTOBUF_FIELDOPTIONS_JS_NORMAL = 0,
6357 GOOGLE_PROTOBUF_FIELDOPTIONS_JS_STRING = 1,
6358 GOOGLE_PROTOBUF_FIELDOPTIONS_JS_NUMBER = 2
6359} google_protobuf_FieldOptions_JSType;
6360
6361typedef enum {
Josh Habermane8ed0212015-06-08 17:56:03 -07006362 GOOGLE_PROTOBUF_FILEOPTIONS_SPEED = 1,
6363 GOOGLE_PROTOBUF_FILEOPTIONS_CODE_SIZE = 2,
6364 GOOGLE_PROTOBUF_FILEOPTIONS_LITE_RUNTIME = 3
6365} google_protobuf_FileOptions_OptimizeMode;
6366
6367/* Selectors */
6368
6369/* google.protobuf.DescriptorProto */
6370#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_FIELD_STARTSUBMSG 2
6371#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NESTED_TYPE_STARTSUBMSG 3
6372#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ENUM_TYPE_STARTSUBMSG 4
6373#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_RANGE_STARTSUBMSG 5
6374#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_STARTSUBMSG 6
6375#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_OPTIONS_STARTSUBMSG 7
Josh Haberman78da6662016-01-13 19:05:43 -08006376#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ONEOF_DECL_STARTSUBMSG 8
6377#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_RESERVED_RANGE_STARTSUBMSG 9
6378#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_FIELD_STARTSEQ 10
6379#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_FIELD_ENDSEQ 11
6380#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_FIELD_ENDSUBMSG 12
6381#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NESTED_TYPE_STARTSEQ 13
6382#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NESTED_TYPE_ENDSEQ 14
6383#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NESTED_TYPE_ENDSUBMSG 15
6384#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ENUM_TYPE_STARTSEQ 16
6385#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ENUM_TYPE_ENDSEQ 17
6386#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ENUM_TYPE_ENDSUBMSG 18
6387#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_RANGE_STARTSEQ 19
6388#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_RANGE_ENDSEQ 20
6389#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_RANGE_ENDSUBMSG 21
6390#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_STARTSEQ 22
6391#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_ENDSEQ 23
6392#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_ENDSUBMSG 24
6393#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_OPTIONS_ENDSUBMSG 25
6394#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ONEOF_DECL_STARTSEQ 26
6395#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ONEOF_DECL_ENDSEQ 27
6396#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ONEOF_DECL_ENDSUBMSG 28
6397#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_RESERVED_RANGE_STARTSEQ 29
6398#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_RESERVED_RANGE_ENDSEQ 30
6399#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_RESERVED_RANGE_ENDSUBMSG 31
6400#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NAME_STRING 32
6401#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NAME_STARTSTR 33
6402#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NAME_ENDSTR 34
6403#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_RESERVED_NAME_STARTSEQ 35
6404#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_RESERVED_NAME_ENDSEQ 36
6405#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_RESERVED_NAME_STRING 37
6406#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_RESERVED_NAME_STARTSTR 38
6407#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_RESERVED_NAME_ENDSTR 39
Josh Habermane8ed0212015-06-08 17:56:03 -07006408
6409/* google.protobuf.DescriptorProto.ExtensionRange */
6410#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSIONRANGE_START_INT32 2
6411#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSIONRANGE_END_INT32 3
6412
Josh Haberman78da6662016-01-13 19:05:43 -08006413/* google.protobuf.DescriptorProto.ReservedRange */
6414#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_RESERVEDRANGE_START_INT32 2
6415#define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_RESERVEDRANGE_END_INT32 3
6416
Josh Habermane8ed0212015-06-08 17:56:03 -07006417/* google.protobuf.EnumDescriptorProto */
6418#define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_VALUE_STARTSUBMSG 2
6419#define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 3
6420#define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_VALUE_STARTSEQ 4
6421#define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_VALUE_ENDSEQ 5
6422#define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_VALUE_ENDSUBMSG 6
6423#define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 7
6424#define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_NAME_STRING 8
6425#define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_NAME_STARTSTR 9
6426#define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_NAME_ENDSTR 10
6427
6428/* google.protobuf.EnumOptions */
6429#define SEL_GOOGLE_PROTOBUF_ENUMOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2
6430#define SEL_GOOGLE_PROTOBUF_ENUMOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3
6431#define SEL_GOOGLE_PROTOBUF_ENUMOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4
6432#define SEL_GOOGLE_PROTOBUF_ENUMOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5
6433#define SEL_GOOGLE_PROTOBUF_ENUMOPTIONS_ALLOW_ALIAS_BOOL 6
Josh Haberman78da6662016-01-13 19:05:43 -08006434#define SEL_GOOGLE_PROTOBUF_ENUMOPTIONS_DEPRECATED_BOOL 7
Josh Habermane8ed0212015-06-08 17:56:03 -07006435
6436/* google.protobuf.EnumValueDescriptorProto */
6437#define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 2
6438#define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 3
6439#define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_NAME_STRING 4
6440#define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_NAME_STARTSTR 5
6441#define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_NAME_ENDSTR 6
6442#define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_NUMBER_INT32 7
6443
6444/* google.protobuf.EnumValueOptions */
6445#define SEL_GOOGLE_PROTOBUF_ENUMVALUEOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2
6446#define SEL_GOOGLE_PROTOBUF_ENUMVALUEOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3
6447#define SEL_GOOGLE_PROTOBUF_ENUMVALUEOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4
6448#define SEL_GOOGLE_PROTOBUF_ENUMVALUEOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5
Josh Haberman78da6662016-01-13 19:05:43 -08006449#define SEL_GOOGLE_PROTOBUF_ENUMVALUEOPTIONS_DEPRECATED_BOOL 6
Josh Habermane8ed0212015-06-08 17:56:03 -07006450
6451/* google.protobuf.FieldDescriptorProto */
6452#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 2
6453#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 3
6454#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_NAME_STRING 4
6455#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_NAME_STARTSTR 5
6456#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_NAME_ENDSTR 6
6457#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_EXTENDEE_STRING 7
6458#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_EXTENDEE_STARTSTR 8
6459#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_EXTENDEE_ENDSTR 9
6460#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_NUMBER_INT32 10
6461#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_INT32 11
6462#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_INT32 12
6463#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_NAME_STRING 13
6464#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_NAME_STARTSTR 14
6465#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_NAME_ENDSTR 15
6466#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_DEFAULT_VALUE_STRING 16
6467#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_DEFAULT_VALUE_STARTSTR 17
6468#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_DEFAULT_VALUE_ENDSTR 18
Josh Haberman78da6662016-01-13 19:05:43 -08006469#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_ONEOF_INDEX_INT32 19
6470#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_JSON_NAME_STRING 20
6471#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_JSON_NAME_STARTSTR 21
6472#define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_JSON_NAME_ENDSTR 22
Josh Habermane8ed0212015-06-08 17:56:03 -07006473
6474/* google.protobuf.FieldOptions */
6475#define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2
6476#define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3
6477#define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4
6478#define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5
6479#define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_CTYPE_INT32 6
6480#define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_PACKED_BOOL 7
6481#define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_DEPRECATED_BOOL 8
6482#define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_LAZY_BOOL 9
Josh Haberman78da6662016-01-13 19:05:43 -08006483#define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_JSTYPE_INT32 10
6484#define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_WEAK_BOOL 11
Josh Habermane8ed0212015-06-08 17:56:03 -07006485
6486/* google.protobuf.FileDescriptorProto */
6487#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_MESSAGE_TYPE_STARTSUBMSG 2
6488#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_ENUM_TYPE_STARTSUBMSG 3
6489#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SERVICE_STARTSUBMSG 4
6490#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_EXTENSION_STARTSUBMSG 5
6491#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 6
6492#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SOURCE_CODE_INFO_STARTSUBMSG 7
6493#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_MESSAGE_TYPE_STARTSEQ 8
6494#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_MESSAGE_TYPE_ENDSEQ 9
6495#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_MESSAGE_TYPE_ENDSUBMSG 10
6496#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_ENUM_TYPE_STARTSEQ 11
6497#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_ENUM_TYPE_ENDSEQ 12
6498#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_ENUM_TYPE_ENDSUBMSG 13
6499#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SERVICE_STARTSEQ 14
6500#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SERVICE_ENDSEQ 15
6501#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SERVICE_ENDSUBMSG 16
6502#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_EXTENSION_STARTSEQ 17
6503#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_EXTENSION_ENDSEQ 18
6504#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_EXTENSION_ENDSUBMSG 19
6505#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 20
6506#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SOURCE_CODE_INFO_ENDSUBMSG 21
6507#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_NAME_STRING 22
6508#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_NAME_STARTSTR 23
6509#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_NAME_ENDSTR 24
6510#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PACKAGE_STRING 25
6511#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PACKAGE_STARTSTR 26
6512#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PACKAGE_ENDSTR 27
6513#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_DEPENDENCY_STARTSEQ 28
6514#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_DEPENDENCY_ENDSEQ 29
6515#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_DEPENDENCY_STRING 30
6516#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_DEPENDENCY_STARTSTR 31
6517#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_DEPENDENCY_ENDSTR 32
6518#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PUBLIC_DEPENDENCY_STARTSEQ 33
6519#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PUBLIC_DEPENDENCY_ENDSEQ 34
6520#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PUBLIC_DEPENDENCY_INT32 35
6521#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_WEAK_DEPENDENCY_STARTSEQ 36
6522#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_WEAK_DEPENDENCY_ENDSEQ 37
6523#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_WEAK_DEPENDENCY_INT32 38
Josh Haberman78da6662016-01-13 19:05:43 -08006524#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SYNTAX_STRING 39
6525#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SYNTAX_STARTSTR 40
6526#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SYNTAX_ENDSTR 41
Josh Habermane8ed0212015-06-08 17:56:03 -07006527
6528/* google.protobuf.FileDescriptorSet */
6529#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORSET_FILE_STARTSUBMSG 2
6530#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORSET_FILE_STARTSEQ 3
6531#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORSET_FILE_ENDSEQ 4
6532#define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORSET_FILE_ENDSUBMSG 5
6533
6534/* google.protobuf.FileOptions */
6535#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2
6536#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3
6537#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4
6538#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5
6539#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_PACKAGE_STRING 6
6540#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_PACKAGE_STARTSTR 7
6541#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_PACKAGE_ENDSTR 8
6542#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_OUTER_CLASSNAME_STRING 9
6543#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_OUTER_CLASSNAME_STARTSTR 10
6544#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_OUTER_CLASSNAME_ENDSTR 11
6545#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_OPTIMIZE_FOR_INT32 12
6546#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_MULTIPLE_FILES_BOOL 13
6547#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_GO_PACKAGE_STRING 14
6548#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_GO_PACKAGE_STARTSTR 15
6549#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_GO_PACKAGE_ENDSTR 16
6550#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_CC_GENERIC_SERVICES_BOOL 17
6551#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_GENERIC_SERVICES_BOOL 18
6552#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_PY_GENERIC_SERVICES_BOOL 19
6553#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_GENERATE_EQUALS_AND_HASH_BOOL 20
Josh Haberman78da6662016-01-13 19:05:43 -08006554#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_DEPRECATED_BOOL 21
6555#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_STRING_CHECK_UTF8_BOOL 22
6556#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_CC_ENABLE_ARENAS_BOOL 23
6557#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_OBJC_CLASS_PREFIX_STRING 24
6558#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_OBJC_CLASS_PREFIX_STARTSTR 25
6559#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_OBJC_CLASS_PREFIX_ENDSTR 26
6560#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_CSHARP_NAMESPACE_STRING 27
6561#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_CSHARP_NAMESPACE_STARTSTR 28
6562#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_CSHARP_NAMESPACE_ENDSTR 29
6563#define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVANANO_USE_DEPRECATED_PACKAGE_BOOL 30
Josh Habermane8ed0212015-06-08 17:56:03 -07006564
6565/* google.protobuf.MessageOptions */
6566#define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2
6567#define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3
6568#define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4
6569#define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5
6570#define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_MESSAGE_SET_WIRE_FORMAT_BOOL 6
6571#define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_NO_STANDARD_DESCRIPTOR_ACCESSOR_BOOL 7
Josh Haberman78da6662016-01-13 19:05:43 -08006572#define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_DEPRECATED_BOOL 8
6573#define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_MAP_ENTRY_BOOL 9
Josh Habermane8ed0212015-06-08 17:56:03 -07006574
6575/* google.protobuf.MethodDescriptorProto */
6576#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 2
6577#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 3
6578#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_NAME_STRING 4
6579#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_NAME_STARTSTR 5
6580#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_NAME_ENDSTR 6
6581#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_INPUT_TYPE_STRING 7
6582#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_INPUT_TYPE_STARTSTR 8
6583#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_INPUT_TYPE_ENDSTR 9
6584#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_OUTPUT_TYPE_STRING 10
6585#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_OUTPUT_TYPE_STARTSTR 11
6586#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_OUTPUT_TYPE_ENDSTR 12
Josh Haberman78da6662016-01-13 19:05:43 -08006587#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_CLIENT_STREAMING_BOOL 13
6588#define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_SERVER_STREAMING_BOOL 14
Josh Habermane8ed0212015-06-08 17:56:03 -07006589
6590/* google.protobuf.MethodOptions */
6591#define SEL_GOOGLE_PROTOBUF_METHODOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2
6592#define SEL_GOOGLE_PROTOBUF_METHODOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3
6593#define SEL_GOOGLE_PROTOBUF_METHODOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4
6594#define SEL_GOOGLE_PROTOBUF_METHODOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5
Josh Haberman78da6662016-01-13 19:05:43 -08006595#define SEL_GOOGLE_PROTOBUF_METHODOPTIONS_DEPRECATED_BOOL 6
6596
6597/* google.protobuf.OneofDescriptorProto */
6598#define SEL_GOOGLE_PROTOBUF_ONEOFDESCRIPTORPROTO_NAME_STRING 2
6599#define SEL_GOOGLE_PROTOBUF_ONEOFDESCRIPTORPROTO_NAME_STARTSTR 3
6600#define SEL_GOOGLE_PROTOBUF_ONEOFDESCRIPTORPROTO_NAME_ENDSTR 4
Josh Habermane8ed0212015-06-08 17:56:03 -07006601
6602/* google.protobuf.ServiceDescriptorProto */
6603#define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_METHOD_STARTSUBMSG 2
6604#define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 3
6605#define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_METHOD_STARTSEQ 4
6606#define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_METHOD_ENDSEQ 5
6607#define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_METHOD_ENDSUBMSG 6
6608#define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 7
6609#define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_NAME_STRING 8
6610#define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_NAME_STARTSTR 9
6611#define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_NAME_ENDSTR 10
6612
6613/* google.protobuf.ServiceOptions */
6614#define SEL_GOOGLE_PROTOBUF_SERVICEOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2
6615#define SEL_GOOGLE_PROTOBUF_SERVICEOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3
6616#define SEL_GOOGLE_PROTOBUF_SERVICEOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4
6617#define SEL_GOOGLE_PROTOBUF_SERVICEOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5
Josh Haberman78da6662016-01-13 19:05:43 -08006618#define SEL_GOOGLE_PROTOBUF_SERVICEOPTIONS_DEPRECATED_BOOL 6
Josh Habermane8ed0212015-06-08 17:56:03 -07006619
6620/* google.protobuf.SourceCodeInfo */
6621#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_STARTSUBMSG 2
6622#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_STARTSEQ 3
6623#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_ENDSEQ 4
6624#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_ENDSUBMSG 5
6625
6626/* google.protobuf.SourceCodeInfo.Location */
6627#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_PATH_STARTSEQ 2
6628#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_PATH_ENDSEQ 3
6629#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_PATH_INT32 4
6630#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_SPAN_STARTSEQ 5
6631#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_SPAN_ENDSEQ 6
6632#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_SPAN_INT32 7
6633#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_LEADING_COMMENTS_STRING 8
6634#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_LEADING_COMMENTS_STARTSTR 9
6635#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_LEADING_COMMENTS_ENDSTR 10
6636#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_TRAILING_COMMENTS_STRING 11
6637#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_TRAILING_COMMENTS_STARTSTR 12
6638#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_TRAILING_COMMENTS_ENDSTR 13
Josh Haberman78da6662016-01-13 19:05:43 -08006639#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_LEADING_DETACHED_COMMENTS_STARTSEQ 14
6640#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_LEADING_DETACHED_COMMENTS_ENDSEQ 15
6641#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_LEADING_DETACHED_COMMENTS_STRING 16
6642#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_LEADING_DETACHED_COMMENTS_STARTSTR 17
6643#define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_LEADING_DETACHED_COMMENTS_ENDSTR 18
Josh Habermane8ed0212015-06-08 17:56:03 -07006644
6645/* google.protobuf.UninterpretedOption */
6646#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAME_STARTSUBMSG 2
6647#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAME_STARTSEQ 3
6648#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAME_ENDSEQ 4
6649#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAME_ENDSUBMSG 5
6650#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_IDENTIFIER_VALUE_STRING 6
6651#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_IDENTIFIER_VALUE_STARTSTR 7
6652#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_IDENTIFIER_VALUE_ENDSTR 8
6653#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_POSITIVE_INT_VALUE_UINT64 9
6654#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NEGATIVE_INT_VALUE_INT64 10
6655#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_DOUBLE_VALUE_DOUBLE 11
6656#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_STRING_VALUE_STRING 12
6657#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_STRING_VALUE_STARTSTR 13
6658#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_STRING_VALUE_ENDSTR 14
6659#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_AGGREGATE_VALUE_STRING 15
6660#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_AGGREGATE_VALUE_STARTSTR 16
6661#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_AGGREGATE_VALUE_ENDSTR 17
6662
6663/* google.protobuf.UninterpretedOption.NamePart */
6664#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAMEPART_NAME_PART_STRING 2
6665#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAMEPART_NAME_PART_STARTSTR 3
6666#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAMEPART_NAME_PART_ENDSTR 4
6667#define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAMEPART_IS_EXTENSION_BOOL 5
6668
6669const upb_symtab *upbdefs_google_protobuf_descriptor(const void *owner);
6670
6671/* MessageDefs */
6672UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_DescriptorProto(const upb_symtab *s) {
6673 const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.DescriptorProto");
6674 assert(m);
6675 return m;
6676}
6677UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange(const upb_symtab *s) {
6678 const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.DescriptorProto.ExtensionRange");
6679 assert(m);
6680 return m;
6681}
Josh Haberman78da6662016-01-13 19:05:43 -08006682UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ReservedRange(const upb_symtab *s) {
6683 const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.DescriptorProto.ReservedRange");
6684 assert(m);
6685 return m;
6686}
Josh Habermane8ed0212015-06-08 17:56:03 -07006687UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_EnumDescriptorProto(const upb_symtab *s) {
6688 const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.EnumDescriptorProto");
6689 assert(m);
6690 return m;
6691}
6692UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_EnumOptions(const upb_symtab *s) {
6693 const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.EnumOptions");
6694 assert(m);
6695 return m;
6696}
6697UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_EnumValueDescriptorProto(const upb_symtab *s) {
6698 const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.EnumValueDescriptorProto");
6699 assert(m);
6700 return m;
6701}
6702UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_EnumValueOptions(const upb_symtab *s) {
6703 const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.EnumValueOptions");
6704 assert(m);
6705 return m;
6706}
6707UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_FieldDescriptorProto(const upb_symtab *s) {
6708 const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.FieldDescriptorProto");
6709 assert(m);
6710 return m;
6711}
6712UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_FieldOptions(const upb_symtab *s) {
6713 const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.FieldOptions");
6714 assert(m);
6715 return m;
6716}
6717UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_FileDescriptorProto(const upb_symtab *s) {
6718 const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.FileDescriptorProto");
6719 assert(m);
6720 return m;
6721}
6722UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_FileDescriptorSet(const upb_symtab *s) {
6723 const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.FileDescriptorSet");
6724 assert(m);
6725 return m;
6726}
6727UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_FileOptions(const upb_symtab *s) {
6728 const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.FileOptions");
6729 assert(m);
6730 return m;
6731}
6732UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_MessageOptions(const upb_symtab *s) {
6733 const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.MessageOptions");
6734 assert(m);
6735 return m;
6736}
6737UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_MethodDescriptorProto(const upb_symtab *s) {
6738 const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.MethodDescriptorProto");
6739 assert(m);
6740 return m;
6741}
6742UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_MethodOptions(const upb_symtab *s) {
6743 const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.MethodOptions");
6744 assert(m);
6745 return m;
6746}
Josh Haberman78da6662016-01-13 19:05:43 -08006747UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_OneofDescriptorProto(const upb_symtab *s) {
6748 const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.OneofDescriptorProto");
6749 assert(m);
6750 return m;
6751}
Josh Habermane8ed0212015-06-08 17:56:03 -07006752UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_ServiceDescriptorProto(const upb_symtab *s) {
6753 const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.ServiceDescriptorProto");
6754 assert(m);
6755 return m;
6756}
6757UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_ServiceOptions(const upb_symtab *s) {
6758 const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.ServiceOptions");
6759 assert(m);
6760 return m;
6761}
6762UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo(const upb_symtab *s) {
6763 const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.SourceCodeInfo");
6764 assert(m);
6765 return m;
6766}
6767UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo_Location(const upb_symtab *s) {
6768 const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.SourceCodeInfo.Location");
6769 assert(m);
6770 return m;
6771}
6772UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption(const upb_symtab *s) {
6773 const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.UninterpretedOption");
6774 assert(m);
6775 return m;
6776}
6777UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption_NamePart(const upb_symtab *s) {
6778 const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.UninterpretedOption.NamePart");
6779 assert(m);
6780 return m;
6781}
6782
6783
6784/* EnumDefs */
6785UPB_INLINE const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Label(const upb_symtab *s) {
6786 const upb_enumdef *e = upb_symtab_lookupenum(s, "google.protobuf.FieldDescriptorProto.Label");
6787 assert(e);
6788 return e;
6789}
6790UPB_INLINE const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Type(const upb_symtab *s) {
6791 const upb_enumdef *e = upb_symtab_lookupenum(s, "google.protobuf.FieldDescriptorProto.Type");
6792 assert(e);
6793 return e;
6794}
6795UPB_INLINE const upb_enumdef *upbdefs_google_protobuf_FieldOptions_CType(const upb_symtab *s) {
6796 const upb_enumdef *e = upb_symtab_lookupenum(s, "google.protobuf.FieldOptions.CType");
6797 assert(e);
6798 return e;
6799}
Josh Haberman78da6662016-01-13 19:05:43 -08006800UPB_INLINE const upb_enumdef *upbdefs_google_protobuf_FieldOptions_JSType(const upb_symtab *s) {
6801 const upb_enumdef *e = upb_symtab_lookupenum(s, "google.protobuf.FieldOptions.JSType");
6802 assert(e);
6803 return e;
6804}
Josh Habermane8ed0212015-06-08 17:56:03 -07006805UPB_INLINE const upb_enumdef *upbdefs_google_protobuf_FileOptions_OptimizeMode(const upb_symtab *s) {
6806 const upb_enumdef *e = upb_symtab_lookupenum(s, "google.protobuf.FileOptions.OptimizeMode");
6807 assert(e);
6808 return e;
6809}
6810
6811UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_end(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto_ExtensionRange(s), 2); }
6812UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_start(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto_ExtensionRange(s), 1); }
Josh Haberman78da6662016-01-13 19:05:43 -08006813UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ReservedRange_end(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto_ReservedRange(s), 2); }
6814UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ReservedRange_start(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto_ReservedRange(s), 1); }
Josh Habermane8ed0212015-06-08 17:56:03 -07006815UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_enum_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 4); }
6816UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_extension(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 6); }
6817UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_extension_range(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 5); }
6818UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_field(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 2); }
6819UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 1); }
6820UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_nested_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 3); }
Josh Haberman78da6662016-01-13 19:05:43 -08006821UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_oneof_decl(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 8); }
Josh Habermane8ed0212015-06-08 17:56:03 -07006822UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 7); }
Josh Haberman78da6662016-01-13 19:05:43 -08006823UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_reserved_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 10); }
6824UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_reserved_range(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 9); }
Josh Habermane8ed0212015-06-08 17:56:03 -07006825UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumDescriptorProto(s), 1); }
6826UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumDescriptorProto(s), 3); }
6827UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumDescriptorProto(s), 2); }
6828UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_allow_alias(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumOptions(s), 2); }
Josh Haberman78da6662016-01-13 19:05:43 -08006829UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_deprecated(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumOptions(s), 3); }
Josh Habermane8ed0212015-06-08 17:56:03 -07006830UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumOptions(s), 999); }
6831UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumValueDescriptorProto(s), 1); }
6832UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_number(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumValueDescriptorProto(s), 2); }
6833UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumValueDescriptorProto(s), 3); }
Josh Haberman78da6662016-01-13 19:05:43 -08006834UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueOptions_deprecated(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumValueOptions(s), 1); }
Josh Habermane8ed0212015-06-08 17:56:03 -07006835UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumValueOptions(s), 999); }
6836UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_default_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 7); }
6837UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_extendee(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 2); }
Josh Haberman78da6662016-01-13 19:05:43 -08006838UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_json_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 10); }
Josh Habermane8ed0212015-06-08 17:56:03 -07006839UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_label(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 4); }
6840UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 1); }
6841UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_number(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 3); }
Josh Haberman78da6662016-01-13 19:05:43 -08006842UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_oneof_index(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 9); }
Josh Habermane8ed0212015-06-08 17:56:03 -07006843UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 8); }
6844UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 5); }
6845UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_type_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 6); }
6846UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_ctype(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 1); }
6847UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_deprecated(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 3); }
Josh Haberman78da6662016-01-13 19:05:43 -08006848UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_jstype(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 6); }
Josh Habermane8ed0212015-06-08 17:56:03 -07006849UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_lazy(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 5); }
6850UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_packed(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 2); }
6851UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 999); }
6852UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_weak(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 10); }
6853UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_dependency(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 3); }
6854UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_enum_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 5); }
6855UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_extension(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 7); }
6856UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_message_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 4); }
6857UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 1); }
6858UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 8); }
6859UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_package(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 2); }
6860UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_public_dependency(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 10); }
6861UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_service(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 6); }
6862UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_source_code_info(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 9); }
Josh Haberman78da6662016-01-13 19:05:43 -08006863UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_syntax(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 12); }
Josh Habermane8ed0212015-06-08 17:56:03 -07006864UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_weak_dependency(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 11); }
6865UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorSet_file(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorSet(s), 1); }
Josh Haberman78da6662016-01-13 19:05:43 -08006866UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_cc_enable_arenas(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 31); }
Josh Habermane8ed0212015-06-08 17:56:03 -07006867UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_cc_generic_services(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 16); }
Josh Haberman78da6662016-01-13 19:05:43 -08006868UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_csharp_namespace(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 37); }
6869UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_deprecated(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 23); }
Josh Habermane8ed0212015-06-08 17:56:03 -07006870UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_go_package(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 11); }
6871UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_java_generate_equals_and_hash(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 20); }
6872UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_java_generic_services(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 17); }
6873UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_java_multiple_files(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 10); }
6874UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_java_outer_classname(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 8); }
6875UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_java_package(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 1); }
Josh Haberman78da6662016-01-13 19:05:43 -08006876UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_java_string_check_utf8(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 27); }
6877UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_javanano_use_deprecated_package(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 38); }
6878UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_objc_class_prefix(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 36); }
Josh Habermane8ed0212015-06-08 17:56:03 -07006879UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_optimize_for(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 9); }
6880UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_py_generic_services(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 18); }
6881UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 999); }
Josh Haberman78da6662016-01-13 19:05:43 -08006882UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_deprecated(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MessageOptions(s), 3); }
6883UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_map_entry(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MessageOptions(s), 7); }
Josh Habermane8ed0212015-06-08 17:56:03 -07006884UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_message_set_wire_format(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MessageOptions(s), 1); }
6885UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_no_standard_descriptor_accessor(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MessageOptions(s), 2); }
6886UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MessageOptions(s), 999); }
Josh Haberman78da6662016-01-13 19:05:43 -08006887UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_client_streaming(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodDescriptorProto(s), 5); }
Josh Habermane8ed0212015-06-08 17:56:03 -07006888UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_input_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodDescriptorProto(s), 2); }
6889UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodDescriptorProto(s), 1); }
6890UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodDescriptorProto(s), 4); }
6891UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_output_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodDescriptorProto(s), 3); }
Josh Haberman78da6662016-01-13 19:05:43 -08006892UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_server_streaming(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodDescriptorProto(s), 6); }
6893UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodOptions_deprecated(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodOptions(s), 33); }
Josh Habermane8ed0212015-06-08 17:56:03 -07006894UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodOptions(s), 999); }
Josh Haberman78da6662016-01-13 19:05:43 -08006895UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_OneofDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_OneofDescriptorProto(s), 1); }
Josh Habermane8ed0212015-06-08 17:56:03 -07006896UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_method(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_ServiceDescriptorProto(s), 2); }
6897UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_ServiceDescriptorProto(s), 1); }
6898UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_ServiceDescriptorProto(s), 3); }
Josh Haberman78da6662016-01-13 19:05:43 -08006899UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceOptions_deprecated(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_ServiceOptions(s), 33); }
Josh Habermane8ed0212015-06-08 17:56:03 -07006900UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_ServiceOptions(s), 999); }
6901UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_leading_comments(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_SourceCodeInfo_Location(s), 3); }
Josh Haberman78da6662016-01-13 19:05:43 -08006902UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_leading_detached_comments(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_SourceCodeInfo_Location(s), 6); }
Josh Habermane8ed0212015-06-08 17:56:03 -07006903UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_path(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_SourceCodeInfo_Location(s), 1); }
6904UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_span(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_SourceCodeInfo_Location(s), 2); }
6905UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_trailing_comments(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_SourceCodeInfo_Location(s), 4); }
6906UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_location(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_SourceCodeInfo(s), 1); }
6907UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_NamePart_is_extension(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption_NamePart(s), 2); }
6908UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_NamePart_name_part(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption_NamePart(s), 1); }
6909UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_aggregate_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 8); }
6910UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_double_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 6); }
6911UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_identifier_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 3); }
6912UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 2); }
6913UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_negative_int_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 5); }
6914UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_positive_int_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 4); }
6915UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_string_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 7); }
6916
6917UPB_END_EXTERN_C
6918
6919#ifdef __cplusplus
6920
6921namespace upbdefs {
6922namespace google {
6923namespace protobuf {
6924namespace descriptor {
6925inline upb::reffed_ptr<const upb::SymbolTable> SymbolTable() {
6926 const upb::SymbolTable* s = upbdefs_google_protobuf_descriptor(&s);
6927 return upb::reffed_ptr<const upb::SymbolTable>(s, &s);
6928}
6929} /* namespace descriptor */
6930} /* namespace protobuf */
6931} /* namespace google */
6932
6933#define RETURN_REFFED(type, func) \
6934 const type* obj = func(upbdefs::google::protobuf::descriptor::SymbolTable().get()); \
6935 return upb::reffed_ptr<const type>(obj);
6936
6937namespace google {
6938namespace protobuf {
6939namespace DescriptorProto {
6940inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_DescriptorProto) }
6941inline upb::reffed_ptr<const upb::FieldDef> enum_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_enum_type) }
6942inline upb::reffed_ptr<const upb::FieldDef> extension() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_extension) }
6943inline upb::reffed_ptr<const upb::FieldDef> extension_range() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_extension_range) }
6944inline upb::reffed_ptr<const upb::FieldDef> field() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_field) }
6945inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_name) }
6946inline upb::reffed_ptr<const upb::FieldDef> nested_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_nested_type) }
Josh Haberman78da6662016-01-13 19:05:43 -08006947inline upb::reffed_ptr<const upb::FieldDef> oneof_decl() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_oneof_decl) }
Josh Habermane8ed0212015-06-08 17:56:03 -07006948inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_options) }
Josh Haberman78da6662016-01-13 19:05:43 -08006949inline upb::reffed_ptr<const upb::FieldDef> reserved_name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_reserved_name) }
6950inline upb::reffed_ptr<const upb::FieldDef> reserved_range() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_reserved_range) }
Josh Habermane8ed0212015-06-08 17:56:03 -07006951} /* namespace DescriptorProto */
6952} /* namespace protobuf */
6953} /* namespace google */
6954
6955namespace google {
6956namespace protobuf {
6957namespace DescriptorProto {
6958namespace ExtensionRange {
6959inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_DescriptorProto_ExtensionRange) }
6960inline upb::reffed_ptr<const upb::FieldDef> end() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_ExtensionRange_end) }
6961inline upb::reffed_ptr<const upb::FieldDef> start() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_ExtensionRange_start) }
6962} /* namespace ExtensionRange */
6963} /* namespace DescriptorProto */
6964} /* namespace protobuf */
6965} /* namespace google */
6966
6967namespace google {
6968namespace protobuf {
Josh Haberman78da6662016-01-13 19:05:43 -08006969namespace DescriptorProto {
6970namespace ReservedRange {
6971inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_DescriptorProto_ReservedRange) }
6972inline upb::reffed_ptr<const upb::FieldDef> end() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_ReservedRange_end) }
6973inline upb::reffed_ptr<const upb::FieldDef> start() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_ReservedRange_start) }
6974} /* namespace ReservedRange */
6975} /* namespace DescriptorProto */
6976} /* namespace protobuf */
6977} /* namespace google */
6978
6979namespace google {
6980namespace protobuf {
Josh Habermane8ed0212015-06-08 17:56:03 -07006981namespace EnumDescriptorProto {
6982inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_EnumDescriptorProto) }
6983inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumDescriptorProto_name) }
6984inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumDescriptorProto_options) }
6985inline upb::reffed_ptr<const upb::FieldDef> value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumDescriptorProto_value) }
6986} /* namespace EnumDescriptorProto */
6987} /* namespace protobuf */
6988} /* namespace google */
6989
6990namespace google {
6991namespace protobuf {
6992namespace EnumOptions {
6993inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_EnumOptions) }
6994inline upb::reffed_ptr<const upb::FieldDef> allow_alias() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumOptions_allow_alias) }
Josh Haberman78da6662016-01-13 19:05:43 -08006995inline upb::reffed_ptr<const upb::FieldDef> deprecated() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumOptions_deprecated) }
Josh Habermane8ed0212015-06-08 17:56:03 -07006996inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumOptions_uninterpreted_option) }
6997} /* namespace EnumOptions */
6998} /* namespace protobuf */
6999} /* namespace google */
7000
7001namespace google {
7002namespace protobuf {
7003namespace EnumValueDescriptorProto {
7004inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_EnumValueDescriptorProto) }
7005inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumValueDescriptorProto_name) }
7006inline upb::reffed_ptr<const upb::FieldDef> number() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumValueDescriptorProto_number) }
7007inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumValueDescriptorProto_options) }
7008} /* namespace EnumValueDescriptorProto */
7009} /* namespace protobuf */
7010} /* namespace google */
7011
7012namespace google {
7013namespace protobuf {
7014namespace EnumValueOptions {
7015inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_EnumValueOptions) }
Josh Haberman78da6662016-01-13 19:05:43 -08007016inline upb::reffed_ptr<const upb::FieldDef> deprecated() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumValueOptions_deprecated) }
Josh Habermane8ed0212015-06-08 17:56:03 -07007017inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumValueOptions_uninterpreted_option) }
7018} /* namespace EnumValueOptions */
7019} /* namespace protobuf */
7020} /* namespace google */
7021
7022namespace google {
7023namespace protobuf {
7024namespace FieldDescriptorProto {
7025inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_FieldDescriptorProto) }
7026inline upb::reffed_ptr<const upb::FieldDef> default_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_default_value) }
7027inline upb::reffed_ptr<const upb::FieldDef> extendee() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_extendee) }
Josh Haberman78da6662016-01-13 19:05:43 -08007028inline upb::reffed_ptr<const upb::FieldDef> json_name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_json_name) }
Josh Habermane8ed0212015-06-08 17:56:03 -07007029inline upb::reffed_ptr<const upb::FieldDef> label() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_label) }
7030inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_name) }
7031inline upb::reffed_ptr<const upb::FieldDef> number() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_number) }
Josh Haberman78da6662016-01-13 19:05:43 -08007032inline upb::reffed_ptr<const upb::FieldDef> oneof_index() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_oneof_index) }
Josh Habermane8ed0212015-06-08 17:56:03 -07007033inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_options) }
7034inline upb::reffed_ptr<const upb::FieldDef> type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_type) }
7035inline upb::reffed_ptr<const upb::FieldDef> type_name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_type_name) }
7036inline upb::reffed_ptr<const upb::EnumDef> Label() { RETURN_REFFED(upb::EnumDef, upbdefs_google_protobuf_FieldDescriptorProto_Label) }
7037inline upb::reffed_ptr<const upb::EnumDef> Type() { RETURN_REFFED(upb::EnumDef, upbdefs_google_protobuf_FieldDescriptorProto_Type) }
7038} /* namespace FieldDescriptorProto */
7039} /* namespace protobuf */
7040} /* namespace google */
7041
7042namespace google {
7043namespace protobuf {
7044namespace FieldOptions {
7045inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_FieldOptions) }
7046inline upb::reffed_ptr<const upb::FieldDef> ctype() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_ctype) }
7047inline upb::reffed_ptr<const upb::FieldDef> deprecated() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_deprecated) }
Josh Haberman78da6662016-01-13 19:05:43 -08007048inline upb::reffed_ptr<const upb::FieldDef> jstype() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_jstype) }
Josh Habermane8ed0212015-06-08 17:56:03 -07007049inline upb::reffed_ptr<const upb::FieldDef> lazy() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_lazy) }
7050inline upb::reffed_ptr<const upb::FieldDef> packed() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_packed) }
7051inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_uninterpreted_option) }
7052inline upb::reffed_ptr<const upb::FieldDef> weak() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_weak) }
7053inline upb::reffed_ptr<const upb::EnumDef> CType() { RETURN_REFFED(upb::EnumDef, upbdefs_google_protobuf_FieldOptions_CType) }
Josh Haberman78da6662016-01-13 19:05:43 -08007054inline upb::reffed_ptr<const upb::EnumDef> JSType() { RETURN_REFFED(upb::EnumDef, upbdefs_google_protobuf_FieldOptions_JSType) }
Josh Habermane8ed0212015-06-08 17:56:03 -07007055} /* namespace FieldOptions */
7056} /* namespace protobuf */
7057} /* namespace google */
7058
7059namespace google {
7060namespace protobuf {
7061namespace FileDescriptorProto {
7062inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_FileDescriptorProto) }
7063inline upb::reffed_ptr<const upb::FieldDef> dependency() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_dependency) }
7064inline upb::reffed_ptr<const upb::FieldDef> enum_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_enum_type) }
7065inline upb::reffed_ptr<const upb::FieldDef> extension() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_extension) }
7066inline upb::reffed_ptr<const upb::FieldDef> message_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_message_type) }
7067inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_name) }
7068inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_options) }
7069inline upb::reffed_ptr<const upb::FieldDef> package() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_package) }
7070inline upb::reffed_ptr<const upb::FieldDef> public_dependency() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_public_dependency) }
7071inline upb::reffed_ptr<const upb::FieldDef> service() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_service) }
7072inline upb::reffed_ptr<const upb::FieldDef> source_code_info() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_source_code_info) }
Josh Haberman78da6662016-01-13 19:05:43 -08007073inline upb::reffed_ptr<const upb::FieldDef> syntax() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_syntax) }
Josh Habermane8ed0212015-06-08 17:56:03 -07007074inline upb::reffed_ptr<const upb::FieldDef> weak_dependency() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_weak_dependency) }
7075} /* namespace FileDescriptorProto */
7076} /* namespace protobuf */
7077} /* namespace google */
7078
7079namespace google {
7080namespace protobuf {
7081namespace FileDescriptorSet {
7082inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_FileDescriptorSet) }
7083inline upb::reffed_ptr<const upb::FieldDef> file() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorSet_file) }
7084} /* namespace FileDescriptorSet */
7085} /* namespace protobuf */
7086} /* namespace google */
7087
7088namespace google {
7089namespace protobuf {
7090namespace FileOptions {
7091inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_FileOptions) }
Josh Haberman78da6662016-01-13 19:05:43 -08007092inline upb::reffed_ptr<const upb::FieldDef> cc_enable_arenas() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_cc_enable_arenas) }
Josh Habermane8ed0212015-06-08 17:56:03 -07007093inline upb::reffed_ptr<const upb::FieldDef> cc_generic_services() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_cc_generic_services) }
Josh Haberman78da6662016-01-13 19:05:43 -08007094inline upb::reffed_ptr<const upb::FieldDef> csharp_namespace() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_csharp_namespace) }
7095inline upb::reffed_ptr<const upb::FieldDef> deprecated() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_deprecated) }
Josh Habermane8ed0212015-06-08 17:56:03 -07007096inline upb::reffed_ptr<const upb::FieldDef> go_package() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_go_package) }
7097inline upb::reffed_ptr<const upb::FieldDef> java_generate_equals_and_hash() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_java_generate_equals_and_hash) }
7098inline upb::reffed_ptr<const upb::FieldDef> java_generic_services() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_java_generic_services) }
7099inline upb::reffed_ptr<const upb::FieldDef> java_multiple_files() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_java_multiple_files) }
7100inline upb::reffed_ptr<const upb::FieldDef> java_outer_classname() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_java_outer_classname) }
7101inline upb::reffed_ptr<const upb::FieldDef> java_package() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_java_package) }
Josh Haberman78da6662016-01-13 19:05:43 -08007102inline upb::reffed_ptr<const upb::FieldDef> java_string_check_utf8() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_java_string_check_utf8) }
7103inline upb::reffed_ptr<const upb::FieldDef> javanano_use_deprecated_package() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_javanano_use_deprecated_package) }
7104inline upb::reffed_ptr<const upb::FieldDef> objc_class_prefix() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_objc_class_prefix) }
Josh Habermane8ed0212015-06-08 17:56:03 -07007105inline upb::reffed_ptr<const upb::FieldDef> optimize_for() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_optimize_for) }
7106inline upb::reffed_ptr<const upb::FieldDef> py_generic_services() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_py_generic_services) }
7107inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_uninterpreted_option) }
7108inline upb::reffed_ptr<const upb::EnumDef> OptimizeMode() { RETURN_REFFED(upb::EnumDef, upbdefs_google_protobuf_FileOptions_OptimizeMode) }
7109} /* namespace FileOptions */
7110} /* namespace protobuf */
7111} /* namespace google */
7112
7113namespace google {
7114namespace protobuf {
7115namespace MessageOptions {
7116inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_MessageOptions) }
Josh Haberman78da6662016-01-13 19:05:43 -08007117inline upb::reffed_ptr<const upb::FieldDef> deprecated() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MessageOptions_deprecated) }
7118inline upb::reffed_ptr<const upb::FieldDef> map_entry() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MessageOptions_map_entry) }
Josh Habermane8ed0212015-06-08 17:56:03 -07007119inline upb::reffed_ptr<const upb::FieldDef> message_set_wire_format() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MessageOptions_message_set_wire_format) }
7120inline upb::reffed_ptr<const upb::FieldDef> no_standard_descriptor_accessor() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MessageOptions_no_standard_descriptor_accessor) }
7121inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MessageOptions_uninterpreted_option) }
7122} /* namespace MessageOptions */
7123} /* namespace protobuf */
7124} /* namespace google */
7125
7126namespace google {
7127namespace protobuf {
7128namespace MethodDescriptorProto {
7129inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_MethodDescriptorProto) }
Josh Haberman78da6662016-01-13 19:05:43 -08007130inline upb::reffed_ptr<const upb::FieldDef> client_streaming() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodDescriptorProto_client_streaming) }
Josh Habermane8ed0212015-06-08 17:56:03 -07007131inline upb::reffed_ptr<const upb::FieldDef> input_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodDescriptorProto_input_type) }
7132inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodDescriptorProto_name) }
7133inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodDescriptorProto_options) }
7134inline upb::reffed_ptr<const upb::FieldDef> output_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodDescriptorProto_output_type) }
Josh Haberman78da6662016-01-13 19:05:43 -08007135inline upb::reffed_ptr<const upb::FieldDef> server_streaming() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodDescriptorProto_server_streaming) }
Josh Habermane8ed0212015-06-08 17:56:03 -07007136} /* namespace MethodDescriptorProto */
7137} /* namespace protobuf */
7138} /* namespace google */
7139
7140namespace google {
7141namespace protobuf {
7142namespace MethodOptions {
7143inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_MethodOptions) }
Josh Haberman78da6662016-01-13 19:05:43 -08007144inline upb::reffed_ptr<const upb::FieldDef> deprecated() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodOptions_deprecated) }
Josh Habermane8ed0212015-06-08 17:56:03 -07007145inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodOptions_uninterpreted_option) }
7146} /* namespace MethodOptions */
7147} /* namespace protobuf */
7148} /* namespace google */
7149
7150namespace google {
7151namespace protobuf {
Josh Haberman78da6662016-01-13 19:05:43 -08007152namespace OneofDescriptorProto {
7153inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_OneofDescriptorProto) }
7154inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_OneofDescriptorProto_name) }
7155} /* namespace OneofDescriptorProto */
7156} /* namespace protobuf */
7157} /* namespace google */
7158
7159namespace google {
7160namespace protobuf {
Josh Habermane8ed0212015-06-08 17:56:03 -07007161namespace ServiceDescriptorProto {
7162inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_ServiceDescriptorProto) }
7163inline upb::reffed_ptr<const upb::FieldDef> method() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_ServiceDescriptorProto_method) }
7164inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_ServiceDescriptorProto_name) }
7165inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_ServiceDescriptorProto_options) }
7166} /* namespace ServiceDescriptorProto */
7167} /* namespace protobuf */
7168} /* namespace google */
7169
7170namespace google {
7171namespace protobuf {
7172namespace ServiceOptions {
7173inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_ServiceOptions) }
Josh Haberman78da6662016-01-13 19:05:43 -08007174inline upb::reffed_ptr<const upb::FieldDef> deprecated() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_ServiceOptions_deprecated) }
Josh Habermane8ed0212015-06-08 17:56:03 -07007175inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_ServiceOptions_uninterpreted_option) }
7176} /* namespace ServiceOptions */
7177} /* namespace protobuf */
7178} /* namespace google */
7179
7180namespace google {
7181namespace protobuf {
7182namespace SourceCodeInfo {
7183inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_SourceCodeInfo) }
7184inline upb::reffed_ptr<const upb::FieldDef> location() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_SourceCodeInfo_location) }
7185} /* namespace SourceCodeInfo */
7186} /* namespace protobuf */
7187} /* namespace google */
7188
7189namespace google {
7190namespace protobuf {
7191namespace SourceCodeInfo {
7192namespace Location {
7193inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_SourceCodeInfo_Location) }
7194inline upb::reffed_ptr<const upb::FieldDef> leading_comments() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_SourceCodeInfo_Location_leading_comments) }
Josh Haberman78da6662016-01-13 19:05:43 -08007195inline upb::reffed_ptr<const upb::FieldDef> leading_detached_comments() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_SourceCodeInfo_Location_leading_detached_comments) }
Josh Habermane8ed0212015-06-08 17:56:03 -07007196inline upb::reffed_ptr<const upb::FieldDef> path() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_SourceCodeInfo_Location_path) }
7197inline upb::reffed_ptr<const upb::FieldDef> span() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_SourceCodeInfo_Location_span) }
7198inline upb::reffed_ptr<const upb::FieldDef> trailing_comments() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_SourceCodeInfo_Location_trailing_comments) }
7199} /* namespace Location */
7200} /* namespace SourceCodeInfo */
7201} /* namespace protobuf */
7202} /* namespace google */
7203
7204namespace google {
7205namespace protobuf {
7206namespace UninterpretedOption {
7207inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_UninterpretedOption) }
7208inline upb::reffed_ptr<const upb::FieldDef> aggregate_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_aggregate_value) }
7209inline upb::reffed_ptr<const upb::FieldDef> double_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_double_value) }
7210inline upb::reffed_ptr<const upb::FieldDef> identifier_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_identifier_value) }
7211inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_name) }
7212inline upb::reffed_ptr<const upb::FieldDef> negative_int_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_negative_int_value) }
7213inline upb::reffed_ptr<const upb::FieldDef> positive_int_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_positive_int_value) }
7214inline upb::reffed_ptr<const upb::FieldDef> string_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_string_value) }
7215} /* namespace UninterpretedOption */
7216} /* namespace protobuf */
7217} /* namespace google */
7218
7219namespace google {
7220namespace protobuf {
7221namespace UninterpretedOption {
7222namespace NamePart {
7223inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_UninterpretedOption_NamePart) }
7224inline upb::reffed_ptr<const upb::FieldDef> is_extension() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_NamePart_is_extension) }
7225inline upb::reffed_ptr<const upb::FieldDef> name_part() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_NamePart_name_part) }
7226} /* namespace NamePart */
7227} /* namespace UninterpretedOption */
7228} /* namespace protobuf */
7229} /* namespace google */
7230
7231} /* namespace upbdefs */
7232
7233
7234#undef RETURN_REFFED
7235#endif /* __cplusplus */
7236
7237#endif /* GOOGLE_PROTOBUF_DESCRIPTOR_UPB_H_ */
Chris Fallin91473dc2014-12-12 15:58:26 -08007238/*
Josh Haberman181c7f22015-07-15 11:05:10 -07007239** Internal-only definitions for the decoder.
7240*/
Chris Fallin91473dc2014-12-12 15:58:26 -08007241
7242#ifndef UPB_DECODER_INT_H_
7243#define UPB_DECODER_INT_H_
7244
7245#include <stdlib.h>
7246/*
Josh Haberman181c7f22015-07-15 11:05:10 -07007247** upb::pb::Decoder
7248**
7249** A high performance, streaming, resumable decoder for the binary protobuf
7250** format.
7251**
7252** This interface works the same regardless of what decoder backend is being
7253** used. A client of this class does not need to know whether decoding is using
7254** a JITted decoder (DynASM, LLVM, etc) or an interpreted decoder. By default,
7255** it will always use the fastest available decoder. However, you can call
7256** set_allow_jit(false) to disable any JIT decoder that might be available.
7257** This is primarily useful for testing purposes.
7258*/
Chris Fallin91473dc2014-12-12 15:58:26 -08007259
7260#ifndef UPB_DECODER_H_
7261#define UPB_DECODER_H_
7262
7263
7264#ifdef __cplusplus
7265namespace upb {
7266namespace pb {
7267class CodeCache;
7268class Decoder;
7269class DecoderMethod;
7270class DecoderMethodOptions;
Josh Habermane8ed0212015-06-08 17:56:03 -07007271} /* namespace pb */
7272} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08007273#endif
7274
Josh Habermane8ed0212015-06-08 17:56:03 -07007275UPB_DECLARE_TYPE(upb::pb::CodeCache, upb_pbcodecache)
7276UPB_DECLARE_TYPE(upb::pb::Decoder, upb_pbdecoder)
7277UPB_DECLARE_TYPE(upb::pb::DecoderMethodOptions, upb_pbdecodermethodopts)
Chris Fallin91473dc2014-12-12 15:58:26 -08007278
Josh Habermane8ed0212015-06-08 17:56:03 -07007279UPB_DECLARE_DERIVED_TYPE(upb::pb::DecoderMethod, upb::RefCounted,
7280 upb_pbdecodermethod, upb_refcounted)
7281
Josh Haberman78da6662016-01-13 19:05:43 -08007282/* The maximum number of bytes we are required to buffer internally between
7283 * calls to the decoder. The value is 14: a 5 byte unknown tag plus ten-byte
7284 * varint, less one because we are buffering an incomplete value.
7285 *
7286 * Should only be used by unit tests. */
7287#define UPB_DECODER_MAX_RESIDUAL_BYTES 14
7288
Josh Habermane8ed0212015-06-08 17:56:03 -07007289#ifdef __cplusplus
7290
7291/* The parameters one uses to construct a DecoderMethod.
7292 * TODO(haberman): move allowjit here? Seems more convenient for users.
7293 * TODO(haberman): move this to be heap allocated for ABI stability. */
7294class upb::pb::DecoderMethodOptions {
Chris Fallin91473dc2014-12-12 15:58:26 -08007295 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07007296 /* Parameter represents the destination handlers that this method will push
7297 * to. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007298 explicit DecoderMethodOptions(const Handlers* dest_handlers);
7299
Josh Habermane8ed0212015-06-08 17:56:03 -07007300 /* Should the decoder push submessages to lazy handlers for fields that have
7301 * them? The caller should set this iff the lazy handlers expect data that is
7302 * in protobuf binary format and the caller wishes to lazy parse it. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007303 void set_lazy(bool lazy);
Josh Habermane8ed0212015-06-08 17:56:03 -07007304#else
7305struct upb_pbdecodermethodopts {
7306#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08007307 const upb_handlers *handlers;
7308 bool lazy;
Josh Habermane8ed0212015-06-08 17:56:03 -07007309};
Chris Fallin91473dc2014-12-12 15:58:26 -08007310
Josh Habermane8ed0212015-06-08 17:56:03 -07007311#ifdef __cplusplus
7312
7313/* Represents the code to parse a protobuf according to a destination
7314 * Handlers. */
7315class upb::pb::DecoderMethod {
Chris Fallin91473dc2014-12-12 15:58:26 -08007316 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07007317 /* Include base methods from upb::ReferenceCounted. */
7318 UPB_REFCOUNTED_CPPMETHODS
Chris Fallin91473dc2014-12-12 15:58:26 -08007319
Josh Habermane8ed0212015-06-08 17:56:03 -07007320 /* The destination handlers that are statically bound to this method.
7321 * This method is only capable of outputting to a sink that uses these
7322 * handlers. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007323 const Handlers* dest_handlers() const;
7324
Josh Habermane8ed0212015-06-08 17:56:03 -07007325 /* The input handlers for this decoder method. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007326 const BytesHandler* input_handler() const;
7327
Josh Habermane8ed0212015-06-08 17:56:03 -07007328 /* Whether this method is native. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007329 bool is_native() const;
7330
Josh Habermane8ed0212015-06-08 17:56:03 -07007331 /* Convenience method for generating a DecoderMethod without explicitly
7332 * creating a CodeCache. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007333 static reffed_ptr<const DecoderMethod> New(const DecoderMethodOptions& opts);
7334
7335 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07007336 UPB_DISALLOW_POD_OPS(DecoderMethod, upb::pb::DecoderMethod)
7337};
Chris Fallin91473dc2014-12-12 15:58:26 -08007338
Josh Habermane8ed0212015-06-08 17:56:03 -07007339#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08007340
Josh Habermane8ed0212015-06-08 17:56:03 -07007341/* Preallocation hint: decoder won't allocate more bytes than this when first
7342 * constructed. This hint may be an overestimate for some build configurations.
7343 * But if the decoder library is upgraded without recompiling the application,
7344 * it may be an underestimate. */
Josh Haberman5bdf4a42015-08-03 15:51:31 -07007345#define UPB_PB_DECODER_SIZE 4408
Chris Fallind3262772015-05-14 18:24:26 -07007346
7347#ifdef __cplusplus
7348
Josh Habermane8ed0212015-06-08 17:56:03 -07007349/* A Decoder receives binary protobuf data on its input sink and pushes the
7350 * decoded data to its output sink. */
Chris Fallind3262772015-05-14 18:24:26 -07007351class upb::pb::Decoder {
Chris Fallin91473dc2014-12-12 15:58:26 -08007352 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07007353 /* Constructs a decoder instance for the given method, which must outlive this
7354 * decoder. Any errors during parsing will be set on the given status, which
7355 * must also outlive this decoder.
7356 *
7357 * The sink must match the given method. */
Chris Fallind3262772015-05-14 18:24:26 -07007358 static Decoder* Create(Environment* env, const DecoderMethod* method,
7359 Sink* output);
Chris Fallin91473dc2014-12-12 15:58:26 -08007360
Josh Habermane8ed0212015-06-08 17:56:03 -07007361 /* Returns the DecoderMethod this decoder is parsing from. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007362 const DecoderMethod* method() const;
7363
Josh Habermane8ed0212015-06-08 17:56:03 -07007364 /* The sink on which this decoder receives input. */
Chris Fallind3262772015-05-14 18:24:26 -07007365 BytesSink* input();
Chris Fallin91473dc2014-12-12 15:58:26 -08007366
Josh Habermane8ed0212015-06-08 17:56:03 -07007367 /* Returns number of bytes successfully parsed.
7368 *
7369 * This can be useful for determining the stream position where an error
7370 * occurred.
7371 *
7372 * This value may not be up-to-date when called from inside a parsing
7373 * callback. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007374 uint64_t BytesParsed() const;
7375
Josh Habermane8ed0212015-06-08 17:56:03 -07007376 /* Gets/sets the parsing nexting limit. If the total number of nested
7377 * submessages and repeated fields hits this limit, parsing will fail. This
7378 * is a resource limit that controls the amount of memory used by the parsing
7379 * stack.
7380 *
7381 * Setting the limit will fail if the parser is currently suspended at a depth
7382 * greater than this, or if memory allocation of the stack fails. */
Chris Fallind3262772015-05-14 18:24:26 -07007383 size_t max_nesting() const;
7384 bool set_max_nesting(size_t max);
Chris Fallin91473dc2014-12-12 15:58:26 -08007385
Chris Fallind3262772015-05-14 18:24:26 -07007386 void Reset();
7387
7388 static const size_t kSize = UPB_PB_DECODER_SIZE;
Chris Fallin91473dc2014-12-12 15:58:26 -08007389
7390 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07007391 UPB_DISALLOW_POD_OPS(Decoder, upb::pb::Decoder)
Chris Fallind3262772015-05-14 18:24:26 -07007392};
Chris Fallin91473dc2014-12-12 15:58:26 -08007393
Josh Habermane8ed0212015-06-08 17:56:03 -07007394#endif /* __cplusplus */
Chris Fallin91473dc2014-12-12 15:58:26 -08007395
Josh Habermane8ed0212015-06-08 17:56:03 -07007396#ifdef __cplusplus
7397
7398/* A class for caching protobuf processing code, whether bytecode for the
7399 * interpreted decoder or machine code for the JIT.
7400 *
7401 * This class is not thread-safe.
7402 *
7403 * TODO(haberman): move this to be heap allocated for ABI stability. */
7404class upb::pb::CodeCache {
Chris Fallin91473dc2014-12-12 15:58:26 -08007405 public:
7406 CodeCache();
7407 ~CodeCache();
7408
Josh Habermane8ed0212015-06-08 17:56:03 -07007409 /* Whether the cache is allowed to generate machine code. Defaults to true.
7410 * There is no real reason to turn it off except for testing or if you are
7411 * having a specific problem with the JIT.
7412 *
7413 * Note that allow_jit = true does not *guarantee* that the code will be JIT
7414 * compiled. If this platform is not supported or the JIT was not compiled
7415 * in, the code may still be interpreted. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007416 bool allow_jit() const;
7417
Josh Habermane8ed0212015-06-08 17:56:03 -07007418 /* This may only be called when the object is first constructed, and prior to
7419 * any code generation, otherwise returns false and does nothing. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007420 bool set_allow_jit(bool allow);
7421
Josh Habermane8ed0212015-06-08 17:56:03 -07007422 /* Returns a DecoderMethod that can push data to the given handlers.
7423 * If a suitable method already exists, it will be returned from the cache.
7424 *
7425 * Specifying the destination handlers here allows the DecoderMethod to be
7426 * statically bound to the destination handlers if possible, which can allow
7427 * more efficient decoding. However the returned method may or may not
7428 * actually be statically bound. But in all cases, the returned method can
7429 * push data to the given handlers. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007430 const DecoderMethod *GetDecoderMethod(const DecoderMethodOptions& opts);
7431
Josh Habermane8ed0212015-06-08 17:56:03 -07007432 /* If/when someone needs to explicitly create a dynamically-bound
7433 * DecoderMethod*, we can add a method to get it here. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007434
7435 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07007436 UPB_DISALLOW_COPY_AND_ASSIGN(CodeCache)
7437#else
7438struct upb_pbcodecache {
7439#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08007440 bool allow_jit_;
7441
Josh Habermane8ed0212015-06-08 17:56:03 -07007442 /* Array of mgroups. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007443 upb_inttable groups;
Josh Habermane8ed0212015-06-08 17:56:03 -07007444};
Chris Fallin91473dc2014-12-12 15:58:26 -08007445
Josh Habermane8ed0212015-06-08 17:56:03 -07007446UPB_BEGIN_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08007447
Chris Fallind3262772015-05-14 18:24:26 -07007448upb_pbdecoder *upb_pbdecoder_create(upb_env *e,
7449 const upb_pbdecodermethod *method,
7450 upb_sink *output);
Chris Fallin91473dc2014-12-12 15:58:26 -08007451const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d);
Chris Fallin91473dc2014-12-12 15:58:26 -08007452upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d);
7453uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d);
Chris Fallind3262772015-05-14 18:24:26 -07007454size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d);
7455bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max);
7456void upb_pbdecoder_reset(upb_pbdecoder *d);
Chris Fallin91473dc2014-12-12 15:58:26 -08007457
7458void upb_pbdecodermethodopts_init(upb_pbdecodermethodopts *opts,
7459 const upb_handlers *h);
7460void upb_pbdecodermethodopts_setlazy(upb_pbdecodermethodopts *opts, bool lazy);
7461
Josh Habermane8ed0212015-06-08 17:56:03 -07007462
7463/* Include refcounted methods like upb_pbdecodermethod_ref(). */
7464UPB_REFCOUNTED_CMETHODS(upb_pbdecodermethod, upb_pbdecodermethod_upcast)
7465
Chris Fallin91473dc2014-12-12 15:58:26 -08007466const upb_handlers *upb_pbdecodermethod_desthandlers(
7467 const upb_pbdecodermethod *m);
7468const upb_byteshandler *upb_pbdecodermethod_inputhandler(
7469 const upb_pbdecodermethod *m);
7470bool upb_pbdecodermethod_isnative(const upb_pbdecodermethod *m);
7471const upb_pbdecodermethod *upb_pbdecodermethod_new(
7472 const upb_pbdecodermethodopts *opts, const void *owner);
7473
7474void upb_pbcodecache_init(upb_pbcodecache *c);
7475void upb_pbcodecache_uninit(upb_pbcodecache *c);
7476bool upb_pbcodecache_allowjit(const upb_pbcodecache *c);
7477bool upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow);
7478const upb_pbdecodermethod *upb_pbcodecache_getdecodermethod(
7479 upb_pbcodecache *c, const upb_pbdecodermethodopts *opts);
7480
Josh Habermane8ed0212015-06-08 17:56:03 -07007481UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08007482
7483#ifdef __cplusplus
7484
7485namespace upb {
7486
7487namespace pb {
7488
Josh Habermane8ed0212015-06-08 17:56:03 -07007489/* static */
Chris Fallind3262772015-05-14 18:24:26 -07007490inline Decoder* Decoder::Create(Environment* env, const DecoderMethod* m,
7491 Sink* sink) {
7492 return upb_pbdecoder_create(env, m, sink);
Chris Fallin91473dc2014-12-12 15:58:26 -08007493}
7494inline const DecoderMethod* Decoder::method() const {
7495 return upb_pbdecoder_method(this);
7496}
Chris Fallind3262772015-05-14 18:24:26 -07007497inline BytesSink* Decoder::input() {
7498 return upb_pbdecoder_input(this);
Chris Fallin91473dc2014-12-12 15:58:26 -08007499}
7500inline uint64_t Decoder::BytesParsed() const {
7501 return upb_pbdecoder_bytesparsed(this);
7502}
Chris Fallind3262772015-05-14 18:24:26 -07007503inline size_t Decoder::max_nesting() const {
7504 return upb_pbdecoder_maxnesting(this);
Chris Fallin91473dc2014-12-12 15:58:26 -08007505}
Chris Fallind3262772015-05-14 18:24:26 -07007506inline bool Decoder::set_max_nesting(size_t max) {
7507 return upb_pbdecoder_setmaxnesting(this, max);
Chris Fallin91473dc2014-12-12 15:58:26 -08007508}
Chris Fallind3262772015-05-14 18:24:26 -07007509inline void Decoder::Reset() { upb_pbdecoder_reset(this); }
Chris Fallin91473dc2014-12-12 15:58:26 -08007510
7511inline DecoderMethodOptions::DecoderMethodOptions(const Handlers* h) {
7512 upb_pbdecodermethodopts_init(this, h);
7513}
7514inline void DecoderMethodOptions::set_lazy(bool lazy) {
7515 upb_pbdecodermethodopts_setlazy(this, lazy);
7516}
7517
Chris Fallin91473dc2014-12-12 15:58:26 -08007518inline const Handlers* DecoderMethod::dest_handlers() const {
7519 return upb_pbdecodermethod_desthandlers(this);
7520}
7521inline const BytesHandler* DecoderMethod::input_handler() const {
7522 return upb_pbdecodermethod_inputhandler(this);
7523}
7524inline bool DecoderMethod::is_native() const {
7525 return upb_pbdecodermethod_isnative(this);
7526}
Josh Habermane8ed0212015-06-08 17:56:03 -07007527/* static */
Chris Fallin91473dc2014-12-12 15:58:26 -08007528inline reffed_ptr<const DecoderMethod> DecoderMethod::New(
7529 const DecoderMethodOptions &opts) {
7530 const upb_pbdecodermethod *m = upb_pbdecodermethod_new(&opts, &m);
7531 return reffed_ptr<const DecoderMethod>(m, &m);
7532}
7533
7534inline CodeCache::CodeCache() {
7535 upb_pbcodecache_init(this);
7536}
7537inline CodeCache::~CodeCache() {
7538 upb_pbcodecache_uninit(this);
7539}
7540inline bool CodeCache::allow_jit() const {
7541 return upb_pbcodecache_allowjit(this);
7542}
7543inline bool CodeCache::set_allow_jit(bool allow) {
7544 return upb_pbcodecache_setallowjit(this, allow);
7545}
7546inline const DecoderMethod *CodeCache::GetDecoderMethod(
7547 const DecoderMethodOptions& opts) {
7548 return upb_pbcodecache_getdecodermethod(this, &opts);
7549}
7550
Josh Habermane8ed0212015-06-08 17:56:03 -07007551} /* namespace pb */
7552} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08007553
Josh Habermane8ed0212015-06-08 17:56:03 -07007554#endif /* __cplusplus */
Chris Fallin91473dc2014-12-12 15:58:26 -08007555
7556#endif /* UPB_DECODER_H_ */
7557
Josh Habermane8ed0212015-06-08 17:56:03 -07007558/* C++ names are not actually used since this type isn't exposed to users. */
7559#ifdef __cplusplus
7560namespace upb {
7561namespace pb {
7562class MessageGroup;
7563} /* namespace pb */
7564} /* namespace upb */
7565#endif
7566UPB_DECLARE_DERIVED_TYPE(upb::pb::MessageGroup, upb::RefCounted,
7567 mgroup, upb_refcounted)
7568
7569/* Opcode definitions. The canonical meaning of each opcode is its
7570 * implementation in the interpreter (the JIT is written to match this).
7571 *
7572 * All instructions have the opcode in the low byte.
7573 * Instruction format for most instructions is:
7574 *
7575 * +-------------------+--------+
7576 * | arg (24) | op (8) |
7577 * +-------------------+--------+
7578 *
7579 * Exceptions are indicated below. A few opcodes are multi-word. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007580typedef enum {
Josh Habermane8ed0212015-06-08 17:56:03 -07007581 /* Opcodes 1-8, 13, 15-18 parse their respective descriptor types.
7582 * Arg for all of these is the upb selector for this field. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007583#define T(type) OP_PARSE_ ## type = UPB_DESCRIPTOR_TYPE_ ## type
7584 T(DOUBLE), T(FLOAT), T(INT64), T(UINT64), T(INT32), T(FIXED64), T(FIXED32),
7585 T(BOOL), T(UINT32), T(SFIXED32), T(SFIXED64), T(SINT32), T(SINT64),
7586#undef T
Josh Habermane8ed0212015-06-08 17:56:03 -07007587 OP_STARTMSG = 9, /* No arg. */
7588 OP_ENDMSG = 10, /* No arg. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007589 OP_STARTSEQ = 11,
7590 OP_ENDSEQ = 12,
7591 OP_STARTSUBMSG = 14,
7592 OP_ENDSUBMSG = 19,
7593 OP_STARTSTR = 20,
7594 OP_STRING = 21,
7595 OP_ENDSTR = 22,
7596
Josh Habermane8ed0212015-06-08 17:56:03 -07007597 OP_PUSHTAGDELIM = 23, /* No arg. */
7598 OP_PUSHLENDELIM = 24, /* No arg. */
7599 OP_POP = 25, /* No arg. */
7600 OP_SETDELIM = 26, /* No arg. */
7601 OP_SETBIGGROUPNUM = 27, /* two words:
7602 * | unused (24) | opc (8) |
7603 * | groupnum (32) | */
Chris Fallin91473dc2014-12-12 15:58:26 -08007604 OP_CHECKDELIM = 28,
7605 OP_CALL = 29,
7606 OP_RET = 30,
7607 OP_BRANCH = 31,
7608
Josh Habermane8ed0212015-06-08 17:56:03 -07007609 /* Different opcodes depending on how many bytes expected. */
7610 OP_TAG1 = 32, /* | match tag (16) | jump target (8) | opc (8) | */
7611 OP_TAG2 = 33, /* | match tag (16) | jump target (8) | opc (8) | */
7612 OP_TAGN = 34, /* three words: */
7613 /* | unused (16) | jump target(8) | opc (8) | */
7614 /* | match tag 1 (32) | */
7615 /* | match tag 2 (32) | */
Chris Fallin91473dc2014-12-12 15:58:26 -08007616
Josh Habermane8ed0212015-06-08 17:56:03 -07007617 OP_SETDISPATCH = 35, /* N words: */
7618 /* | unused (24) | opc | */
7619 /* | upb_inttable* (32 or 64) | */
Chris Fallin91473dc2014-12-12 15:58:26 -08007620
Josh Habermane8ed0212015-06-08 17:56:03 -07007621 OP_DISPATCH = 36, /* No arg. */
Chris Fallin97b663a2015-01-09 16:15:22 -08007622
Josh Habermane8ed0212015-06-08 17:56:03 -07007623 OP_HALT = 37 /* No arg. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007624} opcode;
7625
7626#define OP_MAX OP_HALT
7627
7628UPB_INLINE opcode getop(uint32_t instr) { return instr & 0xff; }
7629
Josh Habermane8ed0212015-06-08 17:56:03 -07007630/* Method group; represents a set of decoder methods that had their code
7631 * emitted together, and must therefore be freed together. Immutable once
7632 * created. It is possible we may want to expose this to users at some point.
7633 *
7634 * Overall ownership of Decoder objects looks like this:
7635 *
7636 * +----------+
7637 * | | <---> DecoderMethod
7638 * | method |
7639 * CodeCache ---> | group | <---> DecoderMethod
7640 * | |
7641 * | (mgroup) | <---> DecoderMethod
7642 * +----------+
7643 */
7644struct mgroup {
Chris Fallin91473dc2014-12-12 15:58:26 -08007645 upb_refcounted base;
7646
Josh Habermane8ed0212015-06-08 17:56:03 -07007647 /* Maps upb_msgdef/upb_handlers -> upb_pbdecodermethod. We own refs on the
7648 * methods. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007649 upb_inttable methods;
7650
Josh Habermane8ed0212015-06-08 17:56:03 -07007651 /* When we add the ability to link to previously existing mgroups, we'll
7652 * need an array of mgroups we reference here, and own refs on them. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007653
Josh Habermane8ed0212015-06-08 17:56:03 -07007654 /* The bytecode for our methods, if any exists. Owned by us. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007655 uint32_t *bytecode;
7656 uint32_t *bytecode_end;
7657
7658#ifdef UPB_USE_JIT_X64
Josh Habermane8ed0212015-06-08 17:56:03 -07007659 /* JIT-generated machine code, if any. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007660 upb_string_handlerfunc *jit_code;
Josh Habermane8ed0212015-06-08 17:56:03 -07007661 /* The size of the jit_code (required to munmap()). */
Chris Fallin91473dc2014-12-12 15:58:26 -08007662 size_t jit_size;
7663 char *debug_info;
7664 void *dl;
7665#endif
Josh Habermane8ed0212015-06-08 17:56:03 -07007666};
Chris Fallin91473dc2014-12-12 15:58:26 -08007667
Josh Habermane8ed0212015-06-08 17:56:03 -07007668/* The maximum that any submessages can be nested. Matches proto2's limit.
7669 * This specifies the size of the decoder's statically-sized array and therefore
7670 * setting it high will cause the upb::pb::Decoder object to be larger.
7671 *
7672 * If necessary we can add a runtime-settable property to Decoder that allow
7673 * this to be larger than the compile-time setting, but this would add
7674 * complexity, particularly since we would have to decide how/if to give users
7675 * the ability to set a custom memory allocation function. */
Chris Fallind3262772015-05-14 18:24:26 -07007676#define UPB_DECODER_MAX_NESTING 64
7677
Josh Habermane8ed0212015-06-08 17:56:03 -07007678/* Internal-only struct used by the decoder. */
Chris Fallind3262772015-05-14 18:24:26 -07007679typedef struct {
Josh Habermane8ed0212015-06-08 17:56:03 -07007680 /* Space optimization note: we store two pointers here that the JIT
7681 * doesn't need at all; the upb_handlers* inside the sink and
7682 * the dispatch table pointer. We can optimze so that the JIT uses
7683 * smaller stack frames than the interpreter. The only thing we need
7684 * to guarantee is that the fallback routines can find end_ofs. */
Chris Fallind3262772015-05-14 18:24:26 -07007685 upb_sink sink;
7686
Josh Habermane8ed0212015-06-08 17:56:03 -07007687 /* The absolute stream offset of the end-of-frame delimiter.
7688 * Non-delimited frames (groups and non-packed repeated fields) reuse the
7689 * delimiter of their parent, even though the frame may not end there.
7690 *
7691 * NOTE: the JIT stores a slightly different value here for non-top frames.
7692 * It stores the value relative to the end of the enclosed message. But the
7693 * top frame is still stored the same way, which is important for ensuring
7694 * that calls from the JIT into C work correctly. */
Chris Fallind3262772015-05-14 18:24:26 -07007695 uint64_t end_ofs;
7696 const uint32_t *base;
7697
Josh Habermane8ed0212015-06-08 17:56:03 -07007698 /* 0 indicates a length-delimited field.
7699 * A positive number indicates a known group.
7700 * A negative number indicates an unknown group. */
Chris Fallind3262772015-05-14 18:24:26 -07007701 int32_t groupnum;
Josh Habermane8ed0212015-06-08 17:56:03 -07007702 upb_inttable *dispatch; /* Not used by the JIT. */
Chris Fallind3262772015-05-14 18:24:26 -07007703} upb_pbdecoder_frame;
7704
Josh Habermane8ed0212015-06-08 17:56:03 -07007705struct upb_pbdecodermethod {
7706 upb_refcounted base;
7707
7708 /* While compiling, the base is relative in "ofs", after compiling it is
7709 * absolute in "ptr". */
7710 union {
7711 uint32_t ofs; /* PC offset of method. */
7712 void *ptr; /* Pointer to bytecode or machine code for this method. */
7713 } code_base;
7714
7715 /* The decoder method group to which this method belongs. We own a ref.
7716 * Owning a ref on the entire group is more coarse-grained than is strictly
7717 * necessary; all we truly require is that methods we directly reference
7718 * outlive us, while the group could contain many other messages we don't
7719 * require. But the group represents the messages that were
7720 * allocated+compiled together, so it makes the most sense to free them
7721 * together also. */
7722 const upb_refcounted *group;
7723
7724 /* Whether this method is native code or bytecode. */
7725 bool is_native_;
7726
7727 /* The handler one calls to invoke this method. */
7728 upb_byteshandler input_handler_;
7729
7730 /* The destination handlers this method is bound to. We own a ref. */
7731 const upb_handlers *dest_handlers_;
7732
7733 /* Dispatch table -- used by both bytecode decoder and JIT when encountering a
7734 * field number that wasn't the one we were expecting to see. See
7735 * decoder.int.h for the layout of this table. */
7736 upb_inttable dispatch;
7737};
7738
Chris Fallind3262772015-05-14 18:24:26 -07007739struct upb_pbdecoder {
7740 upb_env *env;
7741
Josh Habermane8ed0212015-06-08 17:56:03 -07007742 /* Our input sink. */
Chris Fallind3262772015-05-14 18:24:26 -07007743 upb_bytessink input_;
7744
Josh Habermane8ed0212015-06-08 17:56:03 -07007745 /* The decoder method we are parsing with (owned). */
Chris Fallind3262772015-05-14 18:24:26 -07007746 const upb_pbdecodermethod *method_;
7747
7748 size_t call_len;
7749 const uint32_t *pc, *last;
7750
Josh Habermane8ed0212015-06-08 17:56:03 -07007751 /* Current input buffer and its stream offset. */
Chris Fallind3262772015-05-14 18:24:26 -07007752 const char *buf, *ptr, *end, *checkpoint;
7753
Josh Habermane8ed0212015-06-08 17:56:03 -07007754 /* End of the delimited region, relative to ptr, NULL if not in this buf. */
Chris Fallind3262772015-05-14 18:24:26 -07007755 const char *delim_end;
7756
Josh Habermane8ed0212015-06-08 17:56:03 -07007757 /* End of the delimited region, relative to ptr, end if not in this buf. */
Chris Fallind3262772015-05-14 18:24:26 -07007758 const char *data_end;
7759
Josh Habermane8ed0212015-06-08 17:56:03 -07007760 /* Overall stream offset of "buf." */
Chris Fallind3262772015-05-14 18:24:26 -07007761 uint64_t bufstart_ofs;
7762
Josh Haberman78da6662016-01-13 19:05:43 -08007763 /* Buffer for residual bytes not parsed from the previous buffer. */
7764 char residual[UPB_DECODER_MAX_RESIDUAL_BYTES];
Chris Fallind3262772015-05-14 18:24:26 -07007765 char *residual_end;
7766
Josh Haberman5bdf4a42015-08-03 15:51:31 -07007767 /* Bytes of data that should be discarded from the input beore we start
7768 * parsing again. We set this when we internally determine that we can
7769 * safely skip the next N bytes, but this region extends past the current
7770 * user buffer. */
7771 size_t skip;
7772
Josh Habermane8ed0212015-06-08 17:56:03 -07007773 /* Stores the user buffer passed to our decode function. */
Chris Fallind3262772015-05-14 18:24:26 -07007774 const char *buf_param;
7775 size_t size_param;
7776 const upb_bufhandle *handle;
7777
Josh Habermane8ed0212015-06-08 17:56:03 -07007778 /* Our internal stack. */
Chris Fallind3262772015-05-14 18:24:26 -07007779 upb_pbdecoder_frame *stack, *top, *limit;
7780 const uint32_t **callstack;
7781 size_t stack_size;
7782
7783 upb_status *status;
7784
7785#ifdef UPB_USE_JIT_X64
Josh Habermane8ed0212015-06-08 17:56:03 -07007786 /* Used momentarily by the generated code to store a value while a user
7787 * function is called. */
Chris Fallind3262772015-05-14 18:24:26 -07007788 uint32_t tmp_len;
7789
7790 const void *saved_rsp;
7791#endif
7792};
7793
Josh Habermane8ed0212015-06-08 17:56:03 -07007794/* Decoder entry points; used as handlers. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007795void *upb_pbdecoder_startbc(void *closure, const void *pc, size_t size_hint);
7796void *upb_pbdecoder_startjit(void *closure, const void *hd, size_t size_hint);
7797size_t upb_pbdecoder_decode(void *closure, const void *hd, const char *buf,
7798 size_t size, const upb_bufhandle *handle);
7799bool upb_pbdecoder_end(void *closure, const void *handler_data);
7800
Josh Habermane8ed0212015-06-08 17:56:03 -07007801/* Decoder-internal functions that the JIT calls to handle fallback paths. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007802int32_t upb_pbdecoder_resume(upb_pbdecoder *d, void *p, const char *buf,
7803 size_t size, const upb_bufhandle *handle);
7804size_t upb_pbdecoder_suspend(upb_pbdecoder *d);
7805int32_t upb_pbdecoder_skipunknown(upb_pbdecoder *d, int32_t fieldnum,
7806 uint8_t wire_type);
7807int32_t upb_pbdecoder_checktag_slow(upb_pbdecoder *d, uint64_t expected);
7808int32_t upb_pbdecoder_decode_varint_slow(upb_pbdecoder *d, uint64_t *u64);
7809int32_t upb_pbdecoder_decode_f32(upb_pbdecoder *d, uint32_t *u32);
7810int32_t upb_pbdecoder_decode_f64(upb_pbdecoder *d, uint64_t *u64);
7811void upb_pbdecoder_seterr(upb_pbdecoder *d, const char *msg);
7812
Josh Habermane8ed0212015-06-08 17:56:03 -07007813/* Error messages that are shared between the bytecode and JIT decoders. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007814extern const char *kPbDecoderStackOverflow;
Josh Haberman5bdf4a42015-08-03 15:51:31 -07007815extern const char *kPbDecoderSubmessageTooLong;
Chris Fallin91473dc2014-12-12 15:58:26 -08007816
Josh Habermane8ed0212015-06-08 17:56:03 -07007817/* Access to decoderplan members needed by the decoder. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007818const char *upb_pbdecoder_getopname(unsigned int op);
7819
Josh Habermane8ed0212015-06-08 17:56:03 -07007820/* JIT codegen entry point. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007821void upb_pbdecoder_jit(mgroup *group);
7822void upb_pbdecoder_freejit(mgroup *group);
Josh Habermane8ed0212015-06-08 17:56:03 -07007823UPB_REFCOUNTED_CMETHODS(mgroup, mgroup_upcast)
Chris Fallin91473dc2014-12-12 15:58:26 -08007824
Josh Habermane8ed0212015-06-08 17:56:03 -07007825/* A special label that means "do field dispatch for this message and branch to
7826 * wherever that takes you." */
Chris Fallin91473dc2014-12-12 15:58:26 -08007827#define LABEL_DISPATCH 0
7828
Josh Habermane8ed0212015-06-08 17:56:03 -07007829/* A special slot in the dispatch table that stores the epilogue (ENDMSG and/or
7830 * RET) for branching to when we find an appropriate ENDGROUP tag. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007831#define DISPATCH_ENDMSG 0
7832
Josh Habermane8ed0212015-06-08 17:56:03 -07007833/* It's important to use this invalid wire type instead of 0 (which is a valid
7834 * wire type). */
Chris Fallin91473dc2014-12-12 15:58:26 -08007835#define NO_WIRE_TYPE 0xff
7836
Josh Habermane8ed0212015-06-08 17:56:03 -07007837/* The dispatch table layout is:
7838 * [field number] -> [ 48-bit offset ][ 8-bit wt2 ][ 8-bit wt1 ]
7839 *
7840 * If wt1 matches, jump to the 48-bit offset. If wt2 matches, lookup
7841 * (UPB_MAX_FIELDNUMBER + fieldnum) and jump there.
7842 *
7843 * We need two wire types because of packed/non-packed compatibility. A
7844 * primitive repeated field can use either wire type and be valid. While we
7845 * could key the table on fieldnum+wiretype, the table would be 8x sparser.
7846 *
7847 * Storing two wire types in the primary value allows us to quickly rule out
7848 * the second wire type without needing to do a separate lookup (this case is
7849 * less common than an unknown field). */
Chris Fallin91473dc2014-12-12 15:58:26 -08007850UPB_INLINE uint64_t upb_pbdecoder_packdispatch(uint64_t ofs, uint8_t wt1,
7851 uint8_t wt2) {
7852 return (ofs << 16) | (wt2 << 8) | wt1;
7853}
7854
7855UPB_INLINE void upb_pbdecoder_unpackdispatch(uint64_t dispatch, uint64_t *ofs,
7856 uint8_t *wt1, uint8_t *wt2) {
7857 *wt1 = (uint8_t)dispatch;
7858 *wt2 = (uint8_t)(dispatch >> 8);
7859 *ofs = dispatch >> 16;
7860}
7861
Josh Habermane8ed0212015-06-08 17:56:03 -07007862/* All of the functions in decoder.c that return int32_t return values according
7863 * to the following scheme:
7864 * 1. negative values indicate a return code from the following list.
7865 * 2. positive values indicate that error or end of buffer was hit, and
7866 * that the decode function should immediately return the given value
7867 * (the decoder state has already been suspended and is ready to be
7868 * resumed). */
Chris Fallin91473dc2014-12-12 15:58:26 -08007869#define DECODE_OK -1
Josh Habermane8ed0212015-06-08 17:56:03 -07007870#define DECODE_MISMATCH -2 /* Used only from checktag_slow(). */
7871#define DECODE_ENDGROUP -3 /* Used only from checkunknown(). */
Chris Fallin91473dc2014-12-12 15:58:26 -08007872
7873#define CHECK_RETURN(x) { int32_t ret = x; if (ret >= 0) return ret; }
7874
Josh Habermane8ed0212015-06-08 17:56:03 -07007875#endif /* UPB_DECODER_INT_H_ */
Chris Fallin91473dc2014-12-12 15:58:26 -08007876/*
Josh Haberman181c7f22015-07-15 11:05:10 -07007877** A number of routines for varint manipulation (we keep them all around to
7878** have multiple approaches available for benchmarking).
7879*/
Chris Fallin91473dc2014-12-12 15:58:26 -08007880
7881#ifndef UPB_VARINT_DECODER_H_
7882#define UPB_VARINT_DECODER_H_
7883
7884#include <assert.h>
7885#include <stdint.h>
7886#include <string.h>
7887
7888#ifdef __cplusplus
7889extern "C" {
7890#endif
7891
Josh Habermane8ed0212015-06-08 17:56:03 -07007892/* A list of types as they are encoded on-the-wire. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007893typedef enum {
7894 UPB_WIRE_TYPE_VARINT = 0,
7895 UPB_WIRE_TYPE_64BIT = 1,
7896 UPB_WIRE_TYPE_DELIMITED = 2,
7897 UPB_WIRE_TYPE_START_GROUP = 3,
7898 UPB_WIRE_TYPE_END_GROUP = 4,
Josh Habermane8ed0212015-06-08 17:56:03 -07007899 UPB_WIRE_TYPE_32BIT = 5
Chris Fallin91473dc2014-12-12 15:58:26 -08007900} upb_wiretype_t;
7901
7902#define UPB_MAX_WIRE_TYPE 5
7903
Josh Habermane8ed0212015-06-08 17:56:03 -07007904/* The maximum number of bytes that it takes to encode a 64-bit varint.
7905 * Note that with a better encoding this could be 9 (TODO: write up a
7906 * wiki document about this). */
Chris Fallin91473dc2014-12-12 15:58:26 -08007907#define UPB_PB_VARINT_MAX_LEN 10
7908
Josh Habermane8ed0212015-06-08 17:56:03 -07007909/* Array of the "native" (ie. non-packed-repeated) wire type for the given a
7910 * descriptor type (upb_descriptortype_t). */
Chris Fallin91473dc2014-12-12 15:58:26 -08007911extern const uint8_t upb_pb_native_wire_types[];
7912
7913/* Zig-zag encoding/decoding **************************************************/
7914
7915UPB_INLINE int32_t upb_zzdec_32(uint32_t n) {
7916 return (n >> 1) ^ -(int32_t)(n & 1);
7917}
7918UPB_INLINE int64_t upb_zzdec_64(uint64_t n) {
7919 return (n >> 1) ^ -(int64_t)(n & 1);
7920}
7921UPB_INLINE uint32_t upb_zzenc_32(int32_t n) { return (n << 1) ^ (n >> 31); }
7922UPB_INLINE uint64_t upb_zzenc_64(int64_t n) { return (n << 1) ^ (n >> 63); }
7923
7924/* Decoding *******************************************************************/
7925
Josh Habermane8ed0212015-06-08 17:56:03 -07007926/* All decoding functions return this struct by value. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007927typedef struct {
Josh Habermane8ed0212015-06-08 17:56:03 -07007928 const char *p; /* NULL if the varint was unterminated. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007929 uint64_t val;
7930} upb_decoderet;
7931
Josh Habermane8ed0212015-06-08 17:56:03 -07007932UPB_INLINE upb_decoderet upb_decoderet_make(const char *p, uint64_t val) {
7933 upb_decoderet ret;
7934 ret.p = p;
7935 ret.val = val;
7936 return ret;
7937}
7938
7939/* Four functions for decoding a varint of at most eight bytes. They are all
7940 * functionally identical, but are implemented in different ways and likely have
7941 * different performance profiles. We keep them around for performance testing.
7942 *
7943 * Note that these functions may not read byte-by-byte, so they must not be used
7944 * unless there are at least eight bytes left in the buffer! */
Chris Fallin91473dc2014-12-12 15:58:26 -08007945upb_decoderet upb_vdecode_max8_branch32(upb_decoderet r);
7946upb_decoderet upb_vdecode_max8_branch64(upb_decoderet r);
7947upb_decoderet upb_vdecode_max8_wright(upb_decoderet r);
7948upb_decoderet upb_vdecode_max8_massimino(upb_decoderet r);
7949
Josh Habermane8ed0212015-06-08 17:56:03 -07007950/* Template for a function that checks the first two bytes with branching
7951 * and dispatches 2-10 bytes with a separate function. Note that this may read
7952 * up to 10 bytes, so it must not be used unless there are at least ten bytes
7953 * left in the buffer! */
Chris Fallin91473dc2014-12-12 15:58:26 -08007954#define UPB_VARINT_DECODER_CHECK2(name, decode_max8_function) \
7955UPB_INLINE upb_decoderet upb_vdecode_check2_ ## name(const char *_p) { \
7956 uint8_t *p = (uint8_t*)_p; \
Josh Habermane8ed0212015-06-08 17:56:03 -07007957 upb_decoderet r; \
7958 if ((*p & 0x80) == 0) { \
7959 /* Common case: one-byte varint. */ \
7960 return upb_decoderet_make(_p + 1, *p & 0x7fU); \
7961 } \
7962 r = upb_decoderet_make(_p + 2, (*p & 0x7fU) | ((*(p + 1) & 0x7fU) << 7)); \
7963 if ((*(p + 1) & 0x80) == 0) { \
7964 /* Two-byte varint. */ \
7965 return r; \
7966 } \
7967 /* Longer varint, fallback to out-of-line function. */ \
Chris Fallin91473dc2014-12-12 15:58:26 -08007968 return decode_max8_function(r); \
7969}
7970
Josh Habermane8ed0212015-06-08 17:56:03 -07007971UPB_VARINT_DECODER_CHECK2(branch32, upb_vdecode_max8_branch32)
7972UPB_VARINT_DECODER_CHECK2(branch64, upb_vdecode_max8_branch64)
7973UPB_VARINT_DECODER_CHECK2(wright, upb_vdecode_max8_wright)
7974UPB_VARINT_DECODER_CHECK2(massimino, upb_vdecode_max8_massimino)
Chris Fallin91473dc2014-12-12 15:58:26 -08007975#undef UPB_VARINT_DECODER_CHECK2
7976
Josh Habermane8ed0212015-06-08 17:56:03 -07007977/* Our canonical functions for decoding varints, based on the currently
7978 * favored best-performing implementations. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007979UPB_INLINE upb_decoderet upb_vdecode_fast(const char *p) {
7980 if (sizeof(long) == 8)
7981 return upb_vdecode_check2_branch64(p);
7982 else
7983 return upb_vdecode_check2_branch32(p);
7984}
7985
7986UPB_INLINE upb_decoderet upb_vdecode_max8_fast(upb_decoderet r) {
7987 return upb_vdecode_max8_massimino(r);
7988}
7989
7990
7991/* Encoding *******************************************************************/
7992
7993UPB_INLINE int upb_value_size(uint64_t val) {
7994#ifdef __GNUC__
Josh Habermane8ed0212015-06-08 17:56:03 -07007995 int high_bit = 63 - __builtin_clzll(val); /* 0-based, undef if val == 0. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007996#else
7997 int high_bit = 0;
7998 uint64_t tmp = val;
7999 while(tmp >>= 1) high_bit++;
8000#endif
8001 return val == 0 ? 1 : high_bit / 8 + 1;
8002}
8003
Josh Habermane8ed0212015-06-08 17:56:03 -07008004/* Encodes a 64-bit varint into buf (which must be >=UPB_PB_VARINT_MAX_LEN
8005 * bytes long), returning how many bytes were used.
8006 *
8007 * TODO: benchmark and optimize if necessary. */
Chris Fallin91473dc2014-12-12 15:58:26 -08008008UPB_INLINE size_t upb_vencode64(uint64_t val, char *buf) {
Josh Habermane8ed0212015-06-08 17:56:03 -07008009 size_t i;
Chris Fallin91473dc2014-12-12 15:58:26 -08008010 if (val == 0) { buf[0] = 0; return 1; }
Josh Habermane8ed0212015-06-08 17:56:03 -07008011 i = 0;
Chris Fallin91473dc2014-12-12 15:58:26 -08008012 while (val) {
8013 uint8_t byte = val & 0x7fU;
8014 val >>= 7;
8015 if (val) byte |= 0x80U;
8016 buf[i++] = byte;
8017 }
8018 return i;
8019}
8020
8021UPB_INLINE size_t upb_varint_size(uint64_t val) {
8022 char buf[UPB_PB_VARINT_MAX_LEN];
8023 return upb_vencode64(val, buf);
8024}
8025
Josh Habermane8ed0212015-06-08 17:56:03 -07008026/* Encodes a 32-bit varint, *not* sign-extended. */
Chris Fallin91473dc2014-12-12 15:58:26 -08008027UPB_INLINE uint64_t upb_vencode32(uint32_t val) {
8028 char buf[UPB_PB_VARINT_MAX_LEN];
8029 size_t bytes = upb_vencode64(val, buf);
8030 uint64_t ret = 0;
8031 assert(bytes <= 5);
8032 memcpy(&ret, buf, bytes);
8033 assert(ret <= 0xffffffffffU);
8034 return ret;
8035}
8036
8037#ifdef __cplusplus
8038} /* extern "C" */
8039#endif
8040
8041#endif /* UPB_VARINT_DECODER_H_ */
8042/*
Josh Haberman181c7f22015-07-15 11:05:10 -07008043** upb::pb::Encoder (upb_pb_encoder)
8044**
8045** Implements a set of upb_handlers that write protobuf data to the binary wire
8046** format.
8047**
8048** This encoder implementation does not have any access to any out-of-band or
8049** precomputed lengths for submessages, so it must buffer submessages internally
8050** before it can emit the first byte.
8051*/
Chris Fallin91473dc2014-12-12 15:58:26 -08008052
8053#ifndef UPB_ENCODER_H_
8054#define UPB_ENCODER_H_
8055
8056
8057#ifdef __cplusplus
8058namespace upb {
8059namespace pb {
8060class Encoder;
Josh Habermane8ed0212015-06-08 17:56:03 -07008061} /* namespace pb */
8062} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08008063#endif
8064
Josh Habermane8ed0212015-06-08 17:56:03 -07008065UPB_DECLARE_TYPE(upb::pb::Encoder, upb_pb_encoder)
Chris Fallin91473dc2014-12-12 15:58:26 -08008066
8067#define UPB_PBENCODER_MAX_NESTING 100
8068
8069/* upb::pb::Encoder ***********************************************************/
8070
Josh Habermane8ed0212015-06-08 17:56:03 -07008071/* Preallocation hint: decoder won't allocate more bytes than this when first
8072 * constructed. This hint may be an overestimate for some build configurations.
8073 * But if the decoder library is upgraded without recompiling the application,
8074 * it may be an underestimate. */
Chris Fallind3262772015-05-14 18:24:26 -07008075#define UPB_PB_ENCODER_SIZE 768
Chris Fallin91473dc2014-12-12 15:58:26 -08008076
Chris Fallind3262772015-05-14 18:24:26 -07008077#ifdef __cplusplus
8078
8079class upb::pb::Encoder {
Chris Fallin91473dc2014-12-12 15:58:26 -08008080 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07008081 /* Creates a new encoder in the given environment. The Handlers must have
8082 * come from NewHandlers() below. */
Chris Fallind3262772015-05-14 18:24:26 -07008083 static Encoder* Create(Environment* env, const Handlers* handlers,
8084 BytesSink* output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008085
Josh Habermane8ed0212015-06-08 17:56:03 -07008086 /* The input to the encoder. */
Chris Fallin91473dc2014-12-12 15:58:26 -08008087 Sink* input();
8088
Josh Habermane8ed0212015-06-08 17:56:03 -07008089 /* Creates a new set of handlers for this MessageDef. */
Chris Fallind3262772015-05-14 18:24:26 -07008090 static reffed_ptr<const Handlers> NewHandlers(const MessageDef* msg);
8091
8092 static const size_t kSize = UPB_PB_ENCODER_SIZE;
8093
Chris Fallin91473dc2014-12-12 15:58:26 -08008094 private:
Josh Habermanfb8ed702015-06-22 17:23:55 -07008095 UPB_DISALLOW_POD_OPS(Encoder, upb::pb::Encoder)
Chris Fallind3262772015-05-14 18:24:26 -07008096};
Chris Fallin91473dc2014-12-12 15:58:26 -08008097
Chris Fallind3262772015-05-14 18:24:26 -07008098#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08008099
8100UPB_BEGIN_EXTERN_C
8101
8102const upb_handlers *upb_pb_encoder_newhandlers(const upb_msgdef *m,
8103 const void *owner);
Chris Fallin91473dc2014-12-12 15:58:26 -08008104upb_sink *upb_pb_encoder_input(upb_pb_encoder *p);
Chris Fallind3262772015-05-14 18:24:26 -07008105upb_pb_encoder* upb_pb_encoder_create(upb_env* e, const upb_handlers* h,
8106 upb_bytessink* output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008107
8108UPB_END_EXTERN_C
8109
8110#ifdef __cplusplus
8111
8112namespace upb {
8113namespace pb {
Chris Fallind3262772015-05-14 18:24:26 -07008114inline Encoder* Encoder::Create(Environment* env, const Handlers* handlers,
8115 BytesSink* output) {
8116 return upb_pb_encoder_create(env, handlers, output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008117}
8118inline Sink* Encoder::input() {
8119 return upb_pb_encoder_input(this);
8120}
8121inline reffed_ptr<const Handlers> Encoder::NewHandlers(
8122 const upb::MessageDef *md) {
8123 const Handlers* h = upb_pb_encoder_newhandlers(md, &h);
8124 return reffed_ptr<const Handlers>(h, &h);
8125}
Josh Habermane8ed0212015-06-08 17:56:03 -07008126} /* namespace pb */
8127} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08008128
8129#endif
8130
8131#endif /* UPB_ENCODER_H_ */
8132/*
Josh Haberman181c7f22015-07-15 11:05:10 -07008133** upb's core components like upb_decoder and upb_msg are carefully designed to
8134** avoid depending on each other for maximum orthogonality. In other words,
8135** you can use a upb_decoder to decode into *any* kind of structure; upb_msg is
8136** just one such structure. A upb_msg can be serialized/deserialized into any
8137** format, protobuf binary format is just one such format.
8138**
8139** However, for convenience we provide functions here for doing common
8140** operations like deserializing protobuf binary format into a upb_msg. The
8141** compromise is that this file drags in almost all of upb as a dependency,
8142** which could be undesirable if you're trying to use a trimmed-down build of
8143** upb.
8144**
8145** While these routines are convenient, they do not reuse any encoding/decoding
8146** state. For example, if a decoder is JIT-based, it will be re-JITted every
8147** time these functions are called. For this reason, if you are parsing lots
8148** of data and efficiency is an issue, these may not be the best functions to
8149** use (though they are useful for prototyping, before optimizing).
8150*/
Chris Fallin91473dc2014-12-12 15:58:26 -08008151
8152#ifndef UPB_GLUE_H
8153#define UPB_GLUE_H
8154
8155#include <stdbool.h>
8156
8157#ifdef __cplusplus
8158extern "C" {
8159#endif
8160
Josh Habermane8ed0212015-06-08 17:56:03 -07008161/* Loads all defs from the given protobuf binary descriptor, setting default
8162 * accessors and a default layout on all messages. The caller owns the
8163 * returned array of defs, which will be of length *n. On error NULL is
8164 * returned and status is set (if non-NULL). */
Chris Fallin91473dc2014-12-12 15:58:26 -08008165upb_def **upb_load_defs_from_descriptor(const char *str, size_t len, int *n,
8166 void *owner, upb_status *status);
8167
Josh Habermane8ed0212015-06-08 17:56:03 -07008168/* Like the previous but also adds the loaded defs to the given symtab. */
Chris Fallin91473dc2014-12-12 15:58:26 -08008169bool upb_load_descriptor_into_symtab(upb_symtab *symtab, const char *str,
8170 size_t len, upb_status *status);
8171
Josh Habermane8ed0212015-06-08 17:56:03 -07008172/* Like the previous but also reads the descriptor from the given filename. */
Chris Fallin91473dc2014-12-12 15:58:26 -08008173bool upb_load_descriptor_file_into_symtab(upb_symtab *symtab, const char *fname,
8174 upb_status *status);
8175
Josh Habermane8ed0212015-06-08 17:56:03 -07008176/* Reads the given filename into a character string, returning NULL if there
8177 * was an error. */
Chris Fallin91473dc2014-12-12 15:58:26 -08008178char *upb_readfile(const char *filename, size_t *len);
8179
8180#ifdef __cplusplus
8181} /* extern "C" */
8182
8183namespace upb {
8184
Josh Habermane8ed0212015-06-08 17:56:03 -07008185/* All routines that load descriptors expect the descriptor to be a
8186 * FileDescriptorSet. */
Chris Fallin91473dc2014-12-12 15:58:26 -08008187inline bool LoadDescriptorFileIntoSymtab(SymbolTable* s, const char *fname,
8188 Status* status) {
8189 return upb_load_descriptor_file_into_symtab(s, fname, status);
8190}
8191
8192inline bool LoadDescriptorIntoSymtab(SymbolTable* s, const char* str,
8193 size_t len, Status* status) {
8194 return upb_load_descriptor_into_symtab(s, str, len, status);
8195}
8196
Josh Habermane8ed0212015-06-08 17:56:03 -07008197/* Templated so it can accept both string and std::string. */
Chris Fallin91473dc2014-12-12 15:58:26 -08008198template <typename T>
8199bool LoadDescriptorIntoSymtab(SymbolTable* s, const T& desc, Status* status) {
8200 return upb_load_descriptor_into_symtab(s, desc.c_str(), desc.size(), status);
8201}
8202
Josh Habermane8ed0212015-06-08 17:56:03 -07008203} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08008204
8205#endif
8206
Josh Habermane8ed0212015-06-08 17:56:03 -07008207#endif /* UPB_GLUE_H */
Chris Fallin91473dc2014-12-12 15:58:26 -08008208/*
Josh Haberman181c7f22015-07-15 11:05:10 -07008209** upb::pb::TextPrinter (upb_textprinter)
8210**
8211** Handlers for writing to protobuf text format.
8212*/
Chris Fallin91473dc2014-12-12 15:58:26 -08008213
8214#ifndef UPB_TEXT_H_
8215#define UPB_TEXT_H_
8216
8217
8218#ifdef __cplusplus
8219namespace upb {
8220namespace pb {
8221class TextPrinter;
Josh Habermane8ed0212015-06-08 17:56:03 -07008222} /* namespace pb */
8223} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08008224#endif
8225
Josh Habermane8ed0212015-06-08 17:56:03 -07008226UPB_DECLARE_TYPE(upb::pb::TextPrinter, upb_textprinter)
Chris Fallin91473dc2014-12-12 15:58:26 -08008227
Chris Fallind3262772015-05-14 18:24:26 -07008228#ifdef __cplusplus
8229
8230class upb::pb::TextPrinter {
Chris Fallin91473dc2014-12-12 15:58:26 -08008231 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07008232 /* The given handlers must have come from NewHandlers(). It must outlive the
8233 * TextPrinter. */
Chris Fallind3262772015-05-14 18:24:26 -07008234 static TextPrinter *Create(Environment *env, const upb::Handlers *handlers,
8235 BytesSink *output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008236
8237 void SetSingleLineMode(bool single_line);
8238
Chris Fallin91473dc2014-12-12 15:58:26 -08008239 Sink* input();
8240
Josh Habermane8ed0212015-06-08 17:56:03 -07008241 /* If handler caching becomes a requirement we can add a code cache as in
8242 * decoder.h */
Chris Fallin91473dc2014-12-12 15:58:26 -08008243 static reffed_ptr<const Handlers> NewHandlers(const MessageDef* md);
Chris Fallind3262772015-05-14 18:24:26 -07008244};
Chris Fallin91473dc2014-12-12 15:58:26 -08008245
Chris Fallind3262772015-05-14 18:24:26 -07008246#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08008247
Chris Fallind3262772015-05-14 18:24:26 -07008248UPB_BEGIN_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08008249
Josh Habermane8ed0212015-06-08 17:56:03 -07008250/* C API. */
Chris Fallind3262772015-05-14 18:24:26 -07008251upb_textprinter *upb_textprinter_create(upb_env *env, const upb_handlers *h,
8252 upb_bytessink *output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008253void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line);
8254upb_sink *upb_textprinter_input(upb_textprinter *p);
8255
8256const upb_handlers *upb_textprinter_newhandlers(const upb_msgdef *m,
8257 const void *owner);
8258
Chris Fallind3262772015-05-14 18:24:26 -07008259UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08008260
8261#ifdef __cplusplus
8262
8263namespace upb {
8264namespace pb {
Chris Fallind3262772015-05-14 18:24:26 -07008265inline TextPrinter *TextPrinter::Create(Environment *env,
8266 const upb::Handlers *handlers,
8267 BytesSink *output) {
8268 return upb_textprinter_create(env, handlers, output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008269}
8270inline void TextPrinter::SetSingleLineMode(bool single_line) {
8271 upb_textprinter_setsingleline(this, single_line);
8272}
Chris Fallin91473dc2014-12-12 15:58:26 -08008273inline Sink* TextPrinter::input() {
8274 return upb_textprinter_input(this);
8275}
8276inline reffed_ptr<const Handlers> TextPrinter::NewHandlers(
8277 const MessageDef *md) {
8278 const Handlers* h = upb_textprinter_newhandlers(md, &h);
8279 return reffed_ptr<const Handlers>(h, &h);
8280}
Josh Habermane8ed0212015-06-08 17:56:03 -07008281} /* namespace pb */
8282} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08008283
8284#endif
8285
8286#endif /* UPB_TEXT_H_ */
8287/*
Josh Haberman181c7f22015-07-15 11:05:10 -07008288** upb::json::Parser (upb_json_parser)
8289**
8290** Parses JSON according to a specific schema.
8291** Support for parsing arbitrary JSON (schema-less) will be added later.
8292*/
Chris Fallin91473dc2014-12-12 15:58:26 -08008293
8294#ifndef UPB_JSON_PARSER_H_
8295#define UPB_JSON_PARSER_H_
8296
8297
8298#ifdef __cplusplus
8299namespace upb {
8300namespace json {
8301class Parser;
Josh Haberman78da6662016-01-13 19:05:43 -08008302class ParserMethod;
Josh Habermane8ed0212015-06-08 17:56:03 -07008303} /* namespace json */
8304} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08008305#endif
8306
Josh Habermane8ed0212015-06-08 17:56:03 -07008307UPB_DECLARE_TYPE(upb::json::Parser, upb_json_parser)
Josh Haberman78da6662016-01-13 19:05:43 -08008308UPB_DECLARE_DERIVED_TYPE(upb::json::ParserMethod, upb::RefCounted,
8309 upb_json_parsermethod, upb_refcounted)
Chris Fallin91473dc2014-12-12 15:58:26 -08008310
Chris Fallin91473dc2014-12-12 15:58:26 -08008311/* upb::json::Parser **********************************************************/
8312
Josh Habermane8ed0212015-06-08 17:56:03 -07008313/* Preallocation hint: parser won't allocate more bytes than this when first
8314 * constructed. This hint may be an overestimate for some build configurations.
8315 * But if the parser library is upgraded without recompiling the application,
8316 * it may be an underestimate. */
Josh Haberman78da6662016-01-13 19:05:43 -08008317#define UPB_JSON_PARSER_SIZE 4104
Chris Fallind3262772015-05-14 18:24:26 -07008318
8319#ifdef __cplusplus
Chris Fallin91473dc2014-12-12 15:58:26 -08008320
Josh Habermane8ed0212015-06-08 17:56:03 -07008321/* Parses an incoming BytesStream, pushing the results to the destination
8322 * sink. */
Chris Fallind3262772015-05-14 18:24:26 -07008323class upb::json::Parser {
Chris Fallin91473dc2014-12-12 15:58:26 -08008324 public:
Josh Haberman78da6662016-01-13 19:05:43 -08008325 static Parser* Create(Environment* env, const ParserMethod* method,
8326 Sink* output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008327
Chris Fallin91473dc2014-12-12 15:58:26 -08008328 BytesSink* input();
Chris Fallin91473dc2014-12-12 15:58:26 -08008329
Chris Fallind3262772015-05-14 18:24:26 -07008330 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07008331 UPB_DISALLOW_POD_OPS(Parser, upb::json::Parser)
Chris Fallind3262772015-05-14 18:24:26 -07008332};
Chris Fallin91473dc2014-12-12 15:58:26 -08008333
Josh Haberman78da6662016-01-13 19:05:43 -08008334class upb::json::ParserMethod {
8335 public:
8336 /* Include base methods from upb::ReferenceCounted. */
8337 UPB_REFCOUNTED_CPPMETHODS
8338
8339 /* Returns handlers for parsing according to the specified schema. */
8340 static reffed_ptr<const ParserMethod> New(const upb::MessageDef* md);
8341
8342 /* The destination handlers that are statically bound to this method.
8343 * This method is only capable of outputting to a sink that uses these
8344 * handlers. */
8345 const Handlers* dest_handlers() const;
8346
8347 /* The input handlers for this decoder method. */
8348 const BytesHandler* input_handler() const;
8349
8350 private:
8351 UPB_DISALLOW_POD_OPS(ParserMethod, upb::json::ParserMethod)
8352};
8353
Chris Fallind3262772015-05-14 18:24:26 -07008354#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08008355
8356UPB_BEGIN_EXTERN_C
8357
Josh Haberman78da6662016-01-13 19:05:43 -08008358upb_json_parser* upb_json_parser_create(upb_env* e,
8359 const upb_json_parsermethod* m,
8360 upb_sink* output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008361upb_bytessink *upb_json_parser_input(upb_json_parser *p);
8362
Josh Haberman78da6662016-01-13 19:05:43 -08008363upb_json_parsermethod* upb_json_parsermethod_new(const upb_msgdef* md,
8364 const void* owner);
8365const upb_handlers *upb_json_parsermethod_desthandlers(
8366 const upb_json_parsermethod *m);
8367const upb_byteshandler *upb_json_parsermethod_inputhandler(
8368 const upb_json_parsermethod *m);
8369
8370/* Include refcounted methods like upb_json_parsermethod_ref(). */
8371UPB_REFCOUNTED_CMETHODS(upb_json_parsermethod, upb_json_parsermethod_upcast)
8372
Chris Fallin91473dc2014-12-12 15:58:26 -08008373UPB_END_EXTERN_C
8374
8375#ifdef __cplusplus
8376
8377namespace upb {
8378namespace json {
Josh Haberman78da6662016-01-13 19:05:43 -08008379inline Parser* Parser::Create(Environment* env, const ParserMethod* method,
8380 Sink* output) {
8381 return upb_json_parser_create(env, method, output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008382}
8383inline BytesSink* Parser::input() {
8384 return upb_json_parser_input(this);
8385}
Josh Haberman78da6662016-01-13 19:05:43 -08008386
8387inline const Handlers* ParserMethod::dest_handlers() const {
8388 return upb_json_parsermethod_desthandlers(this);
8389}
8390inline const BytesHandler* ParserMethod::input_handler() const {
8391 return upb_json_parsermethod_inputhandler(this);
8392}
8393/* static */
8394inline reffed_ptr<const ParserMethod> ParserMethod::New(
8395 const MessageDef* md) {
8396 const upb_json_parsermethod *m = upb_json_parsermethod_new(md, &m);
8397 return reffed_ptr<const ParserMethod>(m, &m);
8398}
8399
Josh Habermane8ed0212015-06-08 17:56:03 -07008400} /* namespace json */
8401} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08008402
8403#endif
8404
8405
Josh Habermane8ed0212015-06-08 17:56:03 -07008406#endif /* UPB_JSON_PARSER_H_ */
Chris Fallin91473dc2014-12-12 15:58:26 -08008407/*
Josh Haberman181c7f22015-07-15 11:05:10 -07008408** upb::json::Printer
8409**
8410** Handlers that emit JSON according to a specific protobuf schema.
8411*/
Chris Fallin91473dc2014-12-12 15:58:26 -08008412
8413#ifndef UPB_JSON_TYPED_PRINTER_H_
8414#define UPB_JSON_TYPED_PRINTER_H_
8415
8416
8417#ifdef __cplusplus
8418namespace upb {
8419namespace json {
8420class Printer;
Josh Habermane8ed0212015-06-08 17:56:03 -07008421} /* namespace json */
8422} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08008423#endif
8424
Josh Habermane8ed0212015-06-08 17:56:03 -07008425UPB_DECLARE_TYPE(upb::json::Printer, upb_json_printer)
Chris Fallin91473dc2014-12-12 15:58:26 -08008426
8427
8428/* upb::json::Printer *********************************************************/
8429
Chris Fallind3262772015-05-14 18:24:26 -07008430#define UPB_JSON_PRINTER_SIZE 168
8431
8432#ifdef __cplusplus
8433
Josh Habermane8ed0212015-06-08 17:56:03 -07008434/* Prints an incoming stream of data to a BytesSink in JSON format. */
Chris Fallind3262772015-05-14 18:24:26 -07008435class upb::json::Printer {
Chris Fallin91473dc2014-12-12 15:58:26 -08008436 public:
Chris Fallind3262772015-05-14 18:24:26 -07008437 static Printer* Create(Environment* env, const upb::Handlers* handlers,
8438 BytesSink* output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008439
Josh Habermane8ed0212015-06-08 17:56:03 -07008440 /* The input to the printer. */
Chris Fallin91473dc2014-12-12 15:58:26 -08008441 Sink* input();
8442
Josh Habermane8ed0212015-06-08 17:56:03 -07008443 /* Returns handlers for printing according to the specified schema. */
Chris Fallin91473dc2014-12-12 15:58:26 -08008444 static reffed_ptr<const Handlers> NewHandlers(const upb::MessageDef* md);
Chris Fallin91473dc2014-12-12 15:58:26 -08008445
Chris Fallind3262772015-05-14 18:24:26 -07008446 static const size_t kSize = UPB_JSON_PRINTER_SIZE;
Chris Fallin91473dc2014-12-12 15:58:26 -08008447
Chris Fallind3262772015-05-14 18:24:26 -07008448 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07008449 UPB_DISALLOW_POD_OPS(Printer, upb::json::Printer)
Chris Fallind3262772015-05-14 18:24:26 -07008450};
8451
8452#endif
8453
8454UPB_BEGIN_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08008455
Josh Habermane8ed0212015-06-08 17:56:03 -07008456/* Native C API. */
Chris Fallind3262772015-05-14 18:24:26 -07008457upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h,
8458 upb_bytessink *output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008459upb_sink *upb_json_printer_input(upb_json_printer *p);
8460const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md,
8461 const void *owner);
8462
Chris Fallind3262772015-05-14 18:24:26 -07008463UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08008464
8465#ifdef __cplusplus
8466
8467namespace upb {
8468namespace json {
Chris Fallind3262772015-05-14 18:24:26 -07008469inline Printer* Printer::Create(Environment* env, const upb::Handlers* handlers,
8470 BytesSink* output) {
8471 return upb_json_printer_create(env, handlers, output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008472}
8473inline Sink* Printer::input() { return upb_json_printer_input(this); }
8474inline reffed_ptr<const Handlers> Printer::NewHandlers(
8475 const upb::MessageDef *md) {
8476 const Handlers* h = upb_json_printer_newhandlers(md, &h);
8477 return reffed_ptr<const Handlers>(h, &h);
8478}
Josh Habermane8ed0212015-06-08 17:56:03 -07008479} /* namespace json */
8480} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08008481
8482#endif
8483
Josh Habermane8ed0212015-06-08 17:56:03 -07008484#endif /* UPB_JSON_TYPED_PRINTER_H_ */