blob: b00581618e4952276f91d9b44de6e74f37eb9d7e [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.
Josh Haberman94e54b32016-04-14 12:06:09 -07008** - upb::FileDef (upb_filedef): describes a .proto file and its defs.
Josh Haberman181c7f22015-07-15 11:05:10 -07009** - upb::EnumDef (upb_enumdef): describes an enum.
10** - upb::OneofDef (upb_oneofdef): describes a oneof.
11** - upb::Def (upb_def): base class of all the others.
12**
13** TODO: definitions of services.
14**
15** Like upb_refcounted objects, defs are mutable only until frozen, and are
16** only thread-safe once frozen.
17**
18** This is a mixed C/C++ interface that offers a full API to both languages.
19** See the top-level README for more information.
20*/
Chris Fallin91473dc2014-12-12 15:58:26 -080021
22#ifndef UPB_DEF_H_
23#define UPB_DEF_H_
24
25/*
Josh Haberman181c7f22015-07-15 11:05:10 -070026** upb::RefCounted (upb_refcounted)
27**
28** A refcounting scheme that supports circular refs. It accomplishes this by
29** partitioning the set of objects into groups such that no cycle spans groups;
30** we can then reference-count the group as a whole and ignore refs within the
31** group. When objects are mutable, these groups are computed very
32** conservatively; we group any objects that have ever had a link between them.
33** When objects are frozen, we compute strongly-connected components which
34** allows us to be precise and only group objects that are actually cyclic.
35**
36** This is a mixed C/C++ interface that offers a full API to both languages.
37** See the top-level README for more information.
38*/
Chris Fallin91473dc2014-12-12 15:58:26 -080039
40#ifndef UPB_REFCOUNTED_H_
41#define UPB_REFCOUNTED_H_
42
43/*
Josh Haberman181c7f22015-07-15 11:05:10 -070044** upb_table
45**
46** This header is INTERNAL-ONLY! Its interfaces are not public or stable!
47** This file defines very fast int->upb_value (inttable) and string->upb_value
48** (strtable) hash tables.
49**
50** The table uses chained scatter with Brent's variation (inspired by the Lua
51** implementation of hash tables). The hash function for strings is Austin
52** Appleby's "MurmurHash."
53**
54** The inttable uses uintptr_t as its key, which guarantees it can be used to
55** store pointers or integers of at least 32 bits (upb isn't really useful on
56** systems where sizeof(void*) < 4).
57**
58** The table must be homogenous (all values of the same type). In debug
59** mode, we check this on insert and lookup.
60*/
Chris Fallin91473dc2014-12-12 15:58:26 -080061
62#ifndef UPB_TABLE_H_
63#define UPB_TABLE_H_
64
65#include <assert.h>
66#include <stdint.h>
67#include <string.h>
68/*
Josh Haberman181c7f22015-07-15 11:05:10 -070069** This file contains shared definitions that are widely used across upb.
70**
71** This is a mixed C/C++ interface that offers a full API to both languages.
72** See the top-level README for more information.
73*/
Chris Fallin91473dc2014-12-12 15:58:26 -080074
75#ifndef UPB_H_
76#define UPB_H_
77
78#include <assert.h>
79#include <stdarg.h>
80#include <stdbool.h>
81#include <stddef.h>
82
Josh Habermane8ed0212015-06-08 17:56:03 -070083/* UPB_INLINE: inline if possible, emit standalone code if required. */
Chris Fallin91473dc2014-12-12 15:58:26 -080084#ifdef __cplusplus
85#define UPB_INLINE inline
Josh Habermane8ed0212015-06-08 17:56:03 -070086#elif defined (__GNUC__)
87#define UPB_INLINE static __inline__
Chris Fallin91473dc2014-12-12 15:58:26 -080088#else
Josh Habermane8ed0212015-06-08 17:56:03 -070089#define UPB_INLINE static
Chris Fallin91473dc2014-12-12 15:58:26 -080090#endif
91
Josh Habermane8ed0212015-06-08 17:56:03 -070092/* Define UPB_BIG_ENDIAN manually if you're on big endian and your compiler
93 * doesn't provide these preprocessor symbols. */
94#if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
95#define UPB_BIG_ENDIAN
96#endif
97
98/* Macros for function attributes on compilers that support them. */
Chris Fallind3262772015-05-14 18:24:26 -070099#ifdef __GNUC__
Josh Habermane8ed0212015-06-08 17:56:03 -0700100#define UPB_FORCEINLINE __inline__ __attribute__((always_inline))
Chris Fallind3262772015-05-14 18:24:26 -0700101#define UPB_NOINLINE __attribute__((noinline))
Josh Habermane8ed0212015-06-08 17:56:03 -0700102#define UPB_NORETURN __attribute__((__noreturn__))
103#else /* !defined(__GNUC__) */
Chris Fallind3262772015-05-14 18:24:26 -0700104#define UPB_FORCEINLINE
105#define UPB_NOINLINE
Josh Habermane8ed0212015-06-08 17:56:03 -0700106#define UPB_NORETURN
Chris Fallind3262772015-05-14 18:24:26 -0700107#endif
108
Josh Habermane8ed0212015-06-08 17:56:03 -0700109/* A few hacky workarounds for functions not in C89.
110 * For internal use only!
111 * TODO(haberman): fix these by including our own implementations, or finding
112 * another workaround.
113 */
114#ifdef __GNUC__
115#define _upb_snprintf __builtin_snprintf
116#define _upb_vsnprintf __builtin_vsnprintf
117#define _upb_va_copy(a, b) __va_copy(a, b)
118#elif __STDC_VERSION__ >= 199901L
119/* C99 versions. */
120#define _upb_snprintf snprintf
121#define _upb_vsnprintf vsnprintf
122#define _upb_va_copy(a, b) va_copy(a, b)
123#else
124#error Need implementations of [v]snprintf and va_copy
Chris Fallin91473dc2014-12-12 15:58:26 -0800125#endif
126
Josh Habermane8ed0212015-06-08 17:56:03 -0700127
Chris Fallin91473dc2014-12-12 15:58:26 -0800128#if ((defined(__cplusplus) && __cplusplus >= 201103L) || \
129 defined(__GXX_EXPERIMENTAL_CXX0X__)) && !defined(UPB_NO_CXX11)
130#define UPB_CXX11
131#endif
132
Josh Habermane8ed0212015-06-08 17:56:03 -0700133/* UPB_DISALLOW_COPY_AND_ASSIGN()
134 * UPB_DISALLOW_POD_OPS()
135 *
136 * Declare these in the "private" section of a C++ class to forbid copy/assign
137 * or all POD ops (construct, destruct, copy, assign) on that class. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800138#ifdef UPB_CXX11
139#include <type_traits>
140#define UPB_DISALLOW_COPY_AND_ASSIGN(class_name) \
141 class_name(const class_name&) = delete; \
142 void operator=(const class_name&) = delete;
143#define UPB_DISALLOW_POD_OPS(class_name, full_class_name) \
144 class_name() = delete; \
145 ~class_name() = delete; \
Chris Fallin91473dc2014-12-12 15:58:26 -0800146 UPB_DISALLOW_COPY_AND_ASSIGN(class_name)
147#define UPB_ASSERT_STDLAYOUT(type) \
148 static_assert(std::is_standard_layout<type>::value, \
149 #type " must be standard layout");
Josh Habermane8ed0212015-06-08 17:56:03 -0700150#else /* !defined(UPB_CXX11) */
Chris Fallin91473dc2014-12-12 15:58:26 -0800151#define UPB_DISALLOW_COPY_AND_ASSIGN(class_name) \
152 class_name(const class_name&); \
153 void operator=(const class_name&);
154#define UPB_DISALLOW_POD_OPS(class_name, full_class_name) \
155 class_name(); \
156 ~class_name(); \
Chris Fallin91473dc2014-12-12 15:58:26 -0800157 UPB_DISALLOW_COPY_AND_ASSIGN(class_name)
158#define UPB_ASSERT_STDLAYOUT(type)
159#endif
160
Josh Habermane8ed0212015-06-08 17:56:03 -0700161/* UPB_DECLARE_TYPE()
162 * UPB_DECLARE_DERIVED_TYPE()
163 * UPB_DECLARE_DERIVED_TYPE2()
164 *
165 * Macros for declaring C and C++ types both, including inheritance.
166 * The inheritance doesn't use real C++ inheritance, to stay compatible with C.
167 *
168 * These macros also provide upcasts:
169 * - in C: types-specific functions (ie. upb_foo_upcast(foo))
170 * - in C++: upb::upcast(foo) along with implicit conversions
171 *
172 * Downcasts are not provided, but upb/def.h defines downcasts for upb::Def. */
173
174#define UPB_C_UPCASTS(ty, base) \
175 UPB_INLINE base *ty ## _upcast_mutable(ty *p) { return (base*)p; } \
176 UPB_INLINE const base *ty ## _upcast(const ty *p) { return (const base*)p; }
177
178#define UPB_C_UPCASTS2(ty, base, base2) \
179 UPB_C_UPCASTS(ty, base) \
180 UPB_INLINE base2 *ty ## _upcast2_mutable(ty *p) { return (base2*)p; } \
181 UPB_INLINE const base2 *ty ## _upcast2(const ty *p) { return (const base2*)p; }
Chris Fallin91473dc2014-12-12 15:58:26 -0800182
183#ifdef __cplusplus
184
Chris Fallin91473dc2014-12-12 15:58:26 -0800185#define UPB_BEGIN_EXTERN_C extern "C" {
186#define UPB_END_EXTERN_C }
Josh Habermane8ed0212015-06-08 17:56:03 -0700187#define UPB_PRIVATE_FOR_CPP private:
188#define UPB_DECLARE_TYPE(cppname, cname) typedef cppname cname;
189
190#define UPB_DECLARE_DERIVED_TYPE(cppname, cppbase, cname, cbase) \
191 UPB_DECLARE_TYPE(cppname, cname) \
192 UPB_C_UPCASTS(cname, cbase) \
Chris Fallin91473dc2014-12-12 15:58:26 -0800193 namespace upb { \
194 template <> \
195 class Pointer<cppname> : public PointerBase<cppname, cppbase> { \
196 public: \
Josh Habermanf654d492016-02-18 11:07:51 -0800197 explicit Pointer(cppname* ptr) \
198 : PointerBase<cppname, cppbase>(ptr) {} \
Chris Fallin91473dc2014-12-12 15:58:26 -0800199 }; \
200 template <> \
201 class Pointer<const cppname> \
202 : public PointerBase<const cppname, const cppbase> { \
203 public: \
Josh Habermanf654d492016-02-18 11:07:51 -0800204 explicit Pointer(const cppname* ptr) \
205 : PointerBase<const cppname, const cppbase>(ptr) {} \
Chris Fallin91473dc2014-12-12 15:58:26 -0800206 }; \
207 }
Josh Habermane8ed0212015-06-08 17:56:03 -0700208
209#define UPB_DECLARE_DERIVED_TYPE2(cppname, cppbase, cppbase2, cname, cbase, \
210 cbase2) \
211 UPB_DECLARE_TYPE(cppname, cname) \
212 UPB_C_UPCASTS2(cname, cbase, cbase2) \
Chris Fallin91473dc2014-12-12 15:58:26 -0800213 namespace upb { \
214 template <> \
215 class Pointer<cppname> : public PointerBase2<cppname, cppbase, cppbase2> { \
216 public: \
Josh Habermanf654d492016-02-18 11:07:51 -0800217 explicit Pointer(cppname* ptr) \
218 : PointerBase2<cppname, cppbase, cppbase2>(ptr) {} \
Chris Fallin91473dc2014-12-12 15:58:26 -0800219 }; \
220 template <> \
221 class Pointer<const cppname> \
222 : public PointerBase2<const cppname, const cppbase, const cppbase2> { \
223 public: \
Josh Habermanf654d492016-02-18 11:07:51 -0800224 explicit Pointer(const cppname* ptr) \
225 : PointerBase2<const cppname, const cppbase, const cppbase2>(ptr) {} \
Chris Fallin91473dc2014-12-12 15:58:26 -0800226 }; \
227 }
228
Josh Habermane8ed0212015-06-08 17:56:03 -0700229#else /* !defined(__cplusplus) */
Chris Fallin91473dc2014-12-12 15:58:26 -0800230
Josh Habermane8ed0212015-06-08 17:56:03 -0700231#define UPB_BEGIN_EXTERN_C
232#define UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -0800233#define UPB_PRIVATE_FOR_CPP
234#define UPB_DECLARE_TYPE(cppname, cname) \
235 struct cname; \
236 typedef struct cname cname;
Josh Habermane8ed0212015-06-08 17:56:03 -0700237#define UPB_DECLARE_DERIVED_TYPE(cppname, cppbase, cname, cbase) \
238 UPB_DECLARE_TYPE(cppname, cname) \
239 UPB_C_UPCASTS(cname, cbase)
240#define UPB_DECLARE_DERIVED_TYPE2(cppname, cppbase, cppbase2, \
241 cname, cbase, cbase2) \
242 UPB_DECLARE_TYPE(cppname, cname) \
243 UPB_C_UPCASTS2(cname, cbase, cbase2)
Chris Fallin91473dc2014-12-12 15:58:26 -0800244
Josh Habermane8ed0212015-06-08 17:56:03 -0700245#endif /* defined(__cplusplus) */
Chris Fallin91473dc2014-12-12 15:58:26 -0800246
247#define UPB_MAX(x, y) ((x) > (y) ? (x) : (y))
248#define UPB_MIN(x, y) ((x) < (y) ? (x) : (y))
249
250#define UPB_UNUSED(var) (void)var
251
Josh Habermane8ed0212015-06-08 17:56:03 -0700252/* For asserting something about a variable when the variable is not used for
253 * anything else. This prevents "unused variable" warnings when compiling in
254 * debug mode. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800255#define UPB_ASSERT_VAR(var, predicate) UPB_UNUSED(var); assert(predicate)
256
Josh Habermane8ed0212015-06-08 17:56:03 -0700257/* Generic function type. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800258typedef void upb_func();
259
Josh Habermane8ed0212015-06-08 17:56:03 -0700260/* C++ Casts ******************************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -0800261
262#ifdef __cplusplus
263
Chris Fallin91473dc2014-12-12 15:58:26 -0800264namespace upb {
265
Chris Fallin91473dc2014-12-12 15:58:26 -0800266template <class T> class Pointer;
267
Josh Habermane8ed0212015-06-08 17:56:03 -0700268/* Casts to a subclass. The caller must know that cast is correct; an
269 * incorrect cast will throw an assertion failure in debug mode.
270 *
271 * Example:
272 * upb::Def* def = GetDef();
273 * // Assert-fails if this was not actually a MessageDef.
274 * upb::MessgeDef* md = upb::down_cast<upb::MessageDef>(def);
275 *
276 * Note that downcasts are only defined for some types (at the moment you can
277 * only downcast from a upb::Def to a specific Def type). */
278template<class To, class From> To down_cast(From* f);
279
280/* Casts to a subclass. If the class does not actually match the given To type,
281 * returns NULL.
282 *
283 * Example:
284 * upb::Def* def = GetDef();
285 * // md will be NULL if this was not actually a MessageDef.
286 * upb::MessgeDef* md = upb::down_cast<upb::MessageDef>(def);
287 *
288 * Note that dynamic casts are only defined for some types (at the moment you
289 * can only downcast from a upb::Def to a specific Def type).. */
290template<class To, class From> To dyn_cast(From* f);
291
292/* Casts to any base class, or the type itself (ie. can be a no-op).
293 *
294 * Example:
295 * upb::MessageDef* md = GetDef();
296 * // This will fail to compile if this wasn't actually a base class.
297 * upb::Def* def = upb::upcast(md);
298 */
Chris Fallin91473dc2014-12-12 15:58:26 -0800299template <class T> inline Pointer<T> upcast(T *f) { return Pointer<T>(f); }
300
Josh Habermane8ed0212015-06-08 17:56:03 -0700301/* Attempt upcast to specific base class.
302 *
303 * Example:
304 * upb::MessageDef* md = GetDef();
305 * upb::upcast_to<upb::Def>(md)->MethodOnDef();
306 */
307template <class T, class F> inline T* upcast_to(F *f) {
308 return static_cast<T*>(upcast(f));
309}
310
311/* PointerBase<T>: implementation detail of upb::upcast().
312 * It is implicitly convertable to pointers to the Base class(es).
313 */
Chris Fallin91473dc2014-12-12 15:58:26 -0800314template <class T, class Base>
315class PointerBase {
316 public:
317 explicit PointerBase(T* ptr) : ptr_(ptr) {}
318 operator T*() { return ptr_; }
Josh Habermane8ed0212015-06-08 17:56:03 -0700319 operator Base*() { return (Base*)ptr_; }
Chris Fallin91473dc2014-12-12 15:58:26 -0800320
321 private:
322 T* ptr_;
323};
324
325template <class T, class Base, class Base2>
326class PointerBase2 : public PointerBase<T, Base> {
327 public:
328 explicit PointerBase2(T* ptr) : PointerBase<T, Base>(ptr) {}
329 operator Base2*() { return Pointer<Base>(*this); }
330};
331
332}
333
334#endif
335
336
337/* upb::reffed_ptr ************************************************************/
338
339#ifdef __cplusplus
340
Josh Habermane8ed0212015-06-08 17:56:03 -0700341#include <algorithm> /* For std::swap(). */
Chris Fallin91473dc2014-12-12 15:58:26 -0800342
343namespace upb {
344
Josh Habermane8ed0212015-06-08 17:56:03 -0700345/* Provides RAII semantics for upb refcounted objects. Each reffed_ptr owns a
346 * ref on whatever object it points to (if any). */
Chris Fallin91473dc2014-12-12 15:58:26 -0800347template <class T> class reffed_ptr {
348 public:
349 reffed_ptr() : ptr_(NULL) {}
350
Josh Habermane8ed0212015-06-08 17:56:03 -0700351 /* If ref_donor is NULL, takes a new ref, otherwise adopts from ref_donor. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800352 template <class U>
353 reffed_ptr(U* val, const void* ref_donor = NULL)
354 : ptr_(upb::upcast(val)) {
355 if (ref_donor) {
356 assert(ptr_);
357 ptr_->DonateRef(ref_donor, this);
358 } else if (ptr_) {
359 ptr_->Ref(this);
360 }
361 }
362
363 template <class U>
364 reffed_ptr(const reffed_ptr<U>& other)
365 : ptr_(upb::upcast(other.get())) {
366 if (ptr_) ptr_->Ref(this);
367 }
368
Josh Haberman94e54b32016-04-14 12:06:09 -0700369 reffed_ptr(const reffed_ptr& other)
370 : ptr_(upb::upcast(other.get())) {
371 if (ptr_) ptr_->Ref(this);
372 }
373
Chris Fallin91473dc2014-12-12 15:58:26 -0800374 ~reffed_ptr() { if (ptr_) ptr_->Unref(this); }
375
376 template <class U>
377 reffed_ptr& operator=(const reffed_ptr<U>& other) {
378 reset(other.get());
379 return *this;
380 }
381
382 reffed_ptr& operator=(const reffed_ptr& other) {
383 reset(other.get());
384 return *this;
385 }
386
Josh Habermane8ed0212015-06-08 17:56:03 -0700387 /* TODO(haberman): add C++11 move construction/assignment for greater
388 * efficiency. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800389
390 void swap(reffed_ptr& other) {
391 if (ptr_ == other.ptr_) {
392 return;
393 }
394
395 if (ptr_) ptr_->DonateRef(this, &other);
396 if (other.ptr_) other.ptr_->DonateRef(&other, this);
397 std::swap(ptr_, other.ptr_);
398 }
399
400 T& operator*() const {
401 assert(ptr_);
402 return *ptr_;
403 }
404
405 T* operator->() const {
406 assert(ptr_);
407 return ptr_;
408 }
409
410 T* get() const { return ptr_; }
411
Josh Habermane8ed0212015-06-08 17:56:03 -0700412 /* If ref_donor is NULL, takes a new ref, otherwise adopts from ref_donor. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800413 template <class U>
414 void reset(U* ptr = NULL, const void* ref_donor = NULL) {
415 reffed_ptr(ptr, ref_donor).swap(*this);
416 }
417
418 template <class U>
419 reffed_ptr<U> down_cast() {
420 return reffed_ptr<U>(upb::down_cast<U*>(get()));
421 }
422
423 template <class U>
424 reffed_ptr<U> dyn_cast() {
425 return reffed_ptr<U>(upb::dyn_cast<U*>(get()));
426 }
427
Josh Habermane8ed0212015-06-08 17:56:03 -0700428 /* Plain release() is unsafe; if we were the only owner, it would leak the
429 * object. Instead we provide this: */
Chris Fallin91473dc2014-12-12 15:58:26 -0800430 T* ReleaseTo(const void* new_owner) {
431 T* ret = NULL;
432 ptr_->DonateRef(this, new_owner);
433 std::swap(ret, ptr_);
434 return ret;
435 }
436
437 private:
438 T* ptr_;
439};
440
Josh Habermane8ed0212015-06-08 17:56:03 -0700441} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -0800442
Josh Habermane8ed0212015-06-08 17:56:03 -0700443#endif /* __cplusplus */
Chris Fallin91473dc2014-12-12 15:58:26 -0800444
445
446/* upb::Status ****************************************************************/
447
448#ifdef __cplusplus
449namespace upb {
450class ErrorSpace;
451class Status;
452}
453#endif
454
Josh Habermane8ed0212015-06-08 17:56:03 -0700455UPB_DECLARE_TYPE(upb::ErrorSpace, upb_errorspace)
456UPB_DECLARE_TYPE(upb::Status, upb_status)
Chris Fallin91473dc2014-12-12 15:58:26 -0800457
Josh Habermane8ed0212015-06-08 17:56:03 -0700458/* The maximum length of an error message before it will get truncated. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800459#define UPB_STATUS_MAX_MESSAGE 128
460
Josh Habermane8ed0212015-06-08 17:56:03 -0700461/* An error callback function is used to report errors from some component.
462 * The function can return "true" to indicate that the component should try
463 * to recover and proceed, but this is not always possible. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800464typedef bool upb_errcb_t(void *closure, const upb_status* status);
465
Josh Habermane8ed0212015-06-08 17:56:03 -0700466#ifdef __cplusplus
467class upb::ErrorSpace {
468#else
469struct upb_errorspace {
470#endif
Chris Fallin91473dc2014-12-12 15:58:26 -0800471 const char *name;
Josh Habermane8ed0212015-06-08 17:56:03 -0700472 /* Should the error message in the status object according to this code. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800473 void (*set_message)(upb_status* status, int code);
Josh Habermane8ed0212015-06-08 17:56:03 -0700474};
Chris Fallin91473dc2014-12-12 15:58:26 -0800475
Josh Habermane8ed0212015-06-08 17:56:03 -0700476#ifdef __cplusplus
477
478/* Object representing a success or failure status.
479 * It owns no resources and allocates no memory, so it should work
480 * even in OOM situations. */
481
482class upb::Status {
Chris Fallin91473dc2014-12-12 15:58:26 -0800483 public:
484 Status();
485
Josh Habermane8ed0212015-06-08 17:56:03 -0700486 /* Returns true if there is no error. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800487 bool ok() const;
488
Josh Habermane8ed0212015-06-08 17:56:03 -0700489 /* Optional error space and code, useful if the caller wants to
490 * programmatically check the specific kind of error. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800491 ErrorSpace* error_space();
492 int code() const;
493
494 const char *error_message() const;
495
Josh Habermane8ed0212015-06-08 17:56:03 -0700496 /* The error message will be truncated if it is longer than
497 * UPB_STATUS_MAX_MESSAGE-4. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800498 void SetErrorMessage(const char* msg);
499 void SetFormattedErrorMessage(const char* fmt, ...);
500
Josh Habermane8ed0212015-06-08 17:56:03 -0700501 /* If there is no error message already, this will use the ErrorSpace to
502 * populate the error message for this code. The caller can still call
503 * SetErrorMessage() to give a more specific message. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800504 void SetErrorCode(ErrorSpace* space, int code);
505
Josh Habermane8ed0212015-06-08 17:56:03 -0700506 /* Resets the status to a successful state with no message. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800507 void Clear();
508
509 void CopyFrom(const Status& other);
510
511 private:
Josh Habermane8ed0212015-06-08 17:56:03 -0700512 UPB_DISALLOW_COPY_AND_ASSIGN(Status)
513#else
514struct upb_status {
515#endif
Chris Fallin91473dc2014-12-12 15:58:26 -0800516 bool ok_;
517
Josh Habermane8ed0212015-06-08 17:56:03 -0700518 /* Specific status code defined by some error space (optional). */
Chris Fallin91473dc2014-12-12 15:58:26 -0800519 int code_;
520 upb_errorspace *error_space_;
521
Josh Habermane8ed0212015-06-08 17:56:03 -0700522 /* Error message; NULL-terminated. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800523 char msg[UPB_STATUS_MAX_MESSAGE];
Josh Habermane8ed0212015-06-08 17:56:03 -0700524};
Chris Fallin91473dc2014-12-12 15:58:26 -0800525
526#define UPB_STATUS_INIT {true, 0, NULL, {0}}
527
528#ifdef __cplusplus
529extern "C" {
530#endif
531
Josh Habermane8ed0212015-06-08 17:56:03 -0700532/* The returned string is invalidated by any other call into the status. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800533const char *upb_status_errmsg(const upb_status *status);
534bool upb_ok(const upb_status *status);
535upb_errorspace *upb_status_errspace(const upb_status *status);
536int upb_status_errcode(const upb_status *status);
537
Josh Habermane8ed0212015-06-08 17:56:03 -0700538/* Any of the functions that write to a status object allow status to be NULL,
539 * to support use cases where the function's caller does not care about the
540 * status message. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800541void upb_status_clear(upb_status *status);
542void upb_status_seterrmsg(upb_status *status, const char *msg);
543void upb_status_seterrf(upb_status *status, const char *fmt, ...);
544void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args);
545void upb_status_seterrcode(upb_status *status, upb_errorspace *space, int code);
546void upb_status_copy(upb_status *to, const upb_status *from);
547
548#ifdef __cplusplus
Josh Habermane8ed0212015-06-08 17:56:03 -0700549} /* extern "C" */
Chris Fallin91473dc2014-12-12 15:58:26 -0800550
551namespace upb {
552
Josh Habermane8ed0212015-06-08 17:56:03 -0700553/* C++ Wrappers */
Chris Fallin91473dc2014-12-12 15:58:26 -0800554inline Status::Status() { Clear(); }
555inline bool Status::ok() const { return upb_ok(this); }
556inline const char* Status::error_message() const {
557 return upb_status_errmsg(this);
558}
559inline void Status::SetErrorMessage(const char* msg) {
560 upb_status_seterrmsg(this, msg);
561}
562inline void Status::SetFormattedErrorMessage(const char* fmt, ...) {
563 va_list args;
564 va_start(args, fmt);
565 upb_status_vseterrf(this, fmt, args);
566 va_end(args);
567}
568inline void Status::SetErrorCode(ErrorSpace* space, int code) {
569 upb_status_seterrcode(this, space, code);
570}
571inline void Status::Clear() { upb_status_clear(this); }
572inline void Status::CopyFrom(const Status& other) {
573 upb_status_copy(this, &other);
574}
575
Josh Habermane8ed0212015-06-08 17:56:03 -0700576} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -0800577
578#endif
579
580#endif /* UPB_H_ */
581
582#ifdef __cplusplus
583extern "C" {
584#endif
585
586
587/* upb_value ******************************************************************/
588
Josh Habermane8ed0212015-06-08 17:56:03 -0700589/* A tagged union (stored untagged inside the table) so that we can check that
590 * clients calling table accessors are correctly typed without having to have
591 * an explosion of accessors. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800592typedef enum {
593 UPB_CTYPE_INT32 = 1,
594 UPB_CTYPE_INT64 = 2,
595 UPB_CTYPE_UINT32 = 3,
596 UPB_CTYPE_UINT64 = 4,
597 UPB_CTYPE_BOOL = 5,
598 UPB_CTYPE_CSTR = 6,
599 UPB_CTYPE_PTR = 7,
600 UPB_CTYPE_CONSTPTR = 8,
Josh Habermane8ed0212015-06-08 17:56:03 -0700601 UPB_CTYPE_FPTR = 9
Chris Fallin91473dc2014-12-12 15:58:26 -0800602} upb_ctype_t;
603
Chris Fallin91473dc2014-12-12 15:58:26 -0800604typedef struct {
Josh Habermane8ed0212015-06-08 17:56:03 -0700605 uint64_t val;
Chris Fallin91473dc2014-12-12 15:58:26 -0800606#ifndef NDEBUG
Josh Habermane8ed0212015-06-08 17:56:03 -0700607 /* In debug mode we carry the value type around also so we can check accesses
608 * to be sure the right member is being read. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800609 upb_ctype_t ctype;
610#endif
611} upb_value;
612
Chris Fallin91473dc2014-12-12 15:58:26 -0800613#ifdef NDEBUG
614#define SET_TYPE(dest, val) UPB_UNUSED(val)
Chris Fallin91473dc2014-12-12 15:58:26 -0800615#else
616#define SET_TYPE(dest, val) dest = val
Chris Fallin91473dc2014-12-12 15:58:26 -0800617#endif
618
Josh Habermane8ed0212015-06-08 17:56:03 -0700619/* Like strdup(), which isn't always available since it's not ANSI C. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800620char *upb_strdup(const char *s);
Josh Habermane8ed0212015-06-08 17:56:03 -0700621/* Variant that works with a length-delimited rather than NULL-delimited string,
622 * as supported by strtable. */
Chris Fallinfd1a3ff2015-01-06 15:44:09 -0800623char *upb_strdup2(const char *s, size_t len);
Chris Fallin91473dc2014-12-12 15:58:26 -0800624
Josh Habermane8ed0212015-06-08 17:56:03 -0700625UPB_INLINE void _upb_value_setval(upb_value *v, uint64_t val,
Chris Fallin91473dc2014-12-12 15:58:26 -0800626 upb_ctype_t ctype) {
627 v->val = val;
628 SET_TYPE(v->ctype, ctype);
629}
630
Josh Habermane8ed0212015-06-08 17:56:03 -0700631UPB_INLINE upb_value _upb_value_val(uint64_t val, upb_ctype_t ctype) {
Chris Fallin91473dc2014-12-12 15:58:26 -0800632 upb_value ret;
633 _upb_value_setval(&ret, val, ctype);
634 return ret;
635}
636
Josh Habermane8ed0212015-06-08 17:56:03 -0700637/* For each value ctype, define the following set of functions:
638 *
639 * // Get/set an int32 from a upb_value.
640 * int32_t upb_value_getint32(upb_value val);
641 * void upb_value_setint32(upb_value *val, int32_t cval);
642 *
643 * // Construct a new upb_value from an int32.
644 * upb_value upb_value_int32(int32_t val); */
645#define FUNCS(name, membername, type_t, converter, proto_type) \
Chris Fallin91473dc2014-12-12 15:58:26 -0800646 UPB_INLINE void upb_value_set ## name(upb_value *val, type_t cval) { \
Josh Habermane8ed0212015-06-08 17:56:03 -0700647 val->val = (converter)cval; \
Chris Fallin91473dc2014-12-12 15:58:26 -0800648 SET_TYPE(val->ctype, proto_type); \
Chris Fallin91473dc2014-12-12 15:58:26 -0800649 } \
650 UPB_INLINE upb_value upb_value_ ## name(type_t val) { \
651 upb_value ret; \
652 upb_value_set ## name(&ret, val); \
653 return ret; \
654 } \
655 UPB_INLINE type_t upb_value_get ## name(upb_value val) { \
656 assert(val.ctype == proto_type); \
Josh Habermane8ed0212015-06-08 17:56:03 -0700657 return (type_t)(converter)val.val; \
Chris Fallin91473dc2014-12-12 15:58:26 -0800658 }
659
Josh Habermane8ed0212015-06-08 17:56:03 -0700660FUNCS(int32, int32, int32_t, int32_t, UPB_CTYPE_INT32)
661FUNCS(int64, int64, int64_t, int64_t, UPB_CTYPE_INT64)
662FUNCS(uint32, uint32, uint32_t, uint32_t, UPB_CTYPE_UINT32)
663FUNCS(uint64, uint64, uint64_t, uint64_t, UPB_CTYPE_UINT64)
664FUNCS(bool, _bool, bool, bool, UPB_CTYPE_BOOL)
665FUNCS(cstr, cstr, char*, uintptr_t, UPB_CTYPE_CSTR)
666FUNCS(ptr, ptr, void*, uintptr_t, UPB_CTYPE_PTR)
667FUNCS(constptr, constptr, const void*, uintptr_t, UPB_CTYPE_CONSTPTR)
668FUNCS(fptr, fptr, upb_func*, uintptr_t, UPB_CTYPE_FPTR)
Chris Fallin91473dc2014-12-12 15:58:26 -0800669
670#undef FUNCS
Josh Habermane8ed0212015-06-08 17:56:03 -0700671#undef SET_TYPE
672
673
674/* upb_tabkey *****************************************************************/
675
676/* Either:
677 * 1. an actual integer key, or
678 * 2. a pointer to a string prefixed by its uint32_t length, owned by us.
679 *
680 * ...depending on whether this is a string table or an int table. We would
681 * make this a union of those two types, but C89 doesn't support statically
682 * initializing a non-first union member. */
683typedef uintptr_t upb_tabkey;
684
685#define UPB_TABKEY_NUM(n) n
686#define UPB_TABKEY_NONE 0
687/* The preprocessor isn't quite powerful enough to turn the compile-time string
688 * length into a byte-wise string representation, so code generation needs to
689 * help it along.
690 *
691 * "len1" is the low byte and len4 is the high byte. */
692#ifdef UPB_BIG_ENDIAN
693#define UPB_TABKEY_STR(len1, len2, len3, len4, strval) \
694 (uintptr_t)(len4 len3 len2 len1 strval)
695#else
696#define UPB_TABKEY_STR(len1, len2, len3, len4, strval) \
697 (uintptr_t)(len1 len2 len3 len4 strval)
698#endif
699
700UPB_INLINE char *upb_tabstr(upb_tabkey key, uint32_t *len) {
701 char* mem = (char*)key;
702 if (len) memcpy(len, mem, sizeof(*len));
703 return mem + sizeof(*len);
704}
705
706
707/* upb_tabval *****************************************************************/
708
709#ifdef __cplusplus
710
711/* Status initialization not supported.
712 *
713 * This separate definition is necessary because in C++, UINTPTR_MAX isn't
714 * reliably available. */
715typedef struct {
716 uint64_t val;
717} upb_tabval;
718
719#else
720
721/* C -- supports static initialization, but to support static initialization of
722 * both integers and points for both 32 and 64 bit targets, it takes a little
723 * bit of doing. */
724
725#if UINTPTR_MAX == 0xffffffffffffffffULL
726#define UPB_PTR_IS_64BITS
727#elif UINTPTR_MAX != 0xffffffff
728#error Could not determine how many bits pointers are.
729#endif
730
731typedef union {
732 /* For static initialization.
733 *
734 * Unfortunately this ugliness is necessary -- it is the only way that we can,
735 * with -std=c89 -pedantic, statically initialize this to either a pointer or
736 * an integer on 32-bit platforms. */
737 struct {
738#ifdef UPB_PTR_IS_64BITS
739 uintptr_t val;
740#else
741 uintptr_t val1;
742 uintptr_t val2;
743#endif
744 } staticinit;
745
746 /* The normal accessor that we use for everything at runtime. */
747 uint64_t val;
748} upb_tabval;
749
750#ifdef UPB_PTR_IS_64BITS
751#define UPB_TABVALUE_INT_INIT(v) {{v}}
752#define UPB_TABVALUE_EMPTY_INIT {{-1}}
753#else
754
755/* 32-bit pointers */
756
757#ifdef UPB_BIG_ENDIAN
758#define UPB_TABVALUE_INT_INIT(v) {{0, v}}
759#define UPB_TABVALUE_EMPTY_INIT {{-1, -1}}
760#else
761#define UPB_TABVALUE_INT_INIT(v) {{v, 0}}
762#define UPB_TABVALUE_EMPTY_INIT {{-1, -1}}
763#endif
764
765#endif
766
767#define UPB_TABVALUE_PTR_INIT(v) UPB_TABVALUE_INT_INIT((uintptr_t)v)
768
769#undef UPB_PTR_IS_64BITS
770
771#endif /* __cplusplus */
Chris Fallin91473dc2014-12-12 15:58:26 -0800772
773
774/* upb_table ******************************************************************/
775
Chris Fallin91473dc2014-12-12 15:58:26 -0800776typedef struct _upb_tabent {
777 upb_tabkey key;
Josh Habermane8ed0212015-06-08 17:56:03 -0700778 upb_tabval val;
779
780 /* Internal chaining. This is const so we can create static initializers for
781 * tables. We cast away const sometimes, but *only* when the containing
782 * upb_table is known to be non-const. This requires a bit of care, but
783 * the subtlety is confined to table.c. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800784 const struct _upb_tabent *next;
785} upb_tabent;
786
787typedef struct {
Josh Habermane8ed0212015-06-08 17:56:03 -0700788 size_t count; /* Number of entries in the hash part. */
789 size_t mask; /* Mask to turn hash value -> bucket. */
790 upb_ctype_t ctype; /* Type of all values. */
791 uint8_t size_lg2; /* Size of the hashtable part is 2^size_lg2 entries. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800792
Josh Habermane8ed0212015-06-08 17:56:03 -0700793 /* Hash table entries.
794 * Making this const isn't entirely accurate; what we really want is for it to
795 * have the same const-ness as the table it's inside. But there's no way to
796 * declare that in C. So we have to make it const so that we can statically
797 * initialize const hash tables. Then we cast away const when we have to.
798 */
Chris Fallin91473dc2014-12-12 15:58:26 -0800799 const upb_tabent *entries;
800} upb_table;
801
802typedef struct {
803 upb_table t;
804} upb_strtable;
805
806#define UPB_STRTABLE_INIT(count, mask, ctype, size_lg2, entries) \
807 {{count, mask, ctype, size_lg2, entries}}
808
Chris Fallinfcd88892015-01-13 18:14:39 -0800809#define UPB_EMPTY_STRTABLE_INIT(ctype) \
810 UPB_STRTABLE_INIT(0, 0, ctype, 0, NULL)
811
Chris Fallin91473dc2014-12-12 15:58:26 -0800812typedef struct {
Josh Habermane8ed0212015-06-08 17:56:03 -0700813 upb_table t; /* For entries that don't fit in the array part. */
814 const upb_tabval *array; /* Array part of the table. See const note above. */
815 size_t array_size; /* Array part size. */
816 size_t array_count; /* Array part number of elements. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800817} upb_inttable;
818
819#define UPB_INTTABLE_INIT(count, mask, ctype, size_lg2, ent, a, asize, acount) \
820 {{count, mask, ctype, size_lg2, ent}, a, asize, acount}
821
822#define UPB_EMPTY_INTTABLE_INIT(ctype) \
823 UPB_INTTABLE_INIT(0, 0, ctype, 0, NULL, NULL, 0, 0)
824
Josh Habermane8ed0212015-06-08 17:56:03 -0700825#define UPB_ARRAY_EMPTYENT -1
Chris Fallin91473dc2014-12-12 15:58:26 -0800826
827UPB_INLINE size_t upb_table_size(const upb_table *t) {
828 if (t->size_lg2 == 0)
829 return 0;
830 else
831 return 1 << t->size_lg2;
832}
833
Josh Habermane8ed0212015-06-08 17:56:03 -0700834/* Internal-only functions, in .h file only out of necessity. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800835UPB_INLINE bool upb_tabent_isempty(const upb_tabent *e) {
Josh Habermane8ed0212015-06-08 17:56:03 -0700836 return e->key == 0;
Chris Fallin91473dc2014-12-12 15:58:26 -0800837}
838
Josh Habermane8ed0212015-06-08 17:56:03 -0700839/* Used by some of the unit tests for generic hashing functionality. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800840uint32_t MurmurHash2(const void * key, size_t len, uint32_t seed);
841
Josh Habermane8ed0212015-06-08 17:56:03 -0700842UPB_INLINE uintptr_t upb_intkey(uintptr_t key) {
843 return key;
Chris Fallin91473dc2014-12-12 15:58:26 -0800844}
845
846UPB_INLINE uint32_t upb_inthash(uintptr_t key) {
847 return (uint32_t)key;
848}
849
850static const upb_tabent *upb_getentry(const upb_table *t, uint32_t hash) {
851 return t->entries + (hash & t->mask);
852}
853
Josh Habermane8ed0212015-06-08 17:56:03 -0700854UPB_INLINE bool upb_arrhas(upb_tabval key) {
855 return key.val != (uint64_t)-1;
Chris Fallin91473dc2014-12-12 15:58:26 -0800856}
857
Josh Habermane8ed0212015-06-08 17:56:03 -0700858/* Initialize and uninitialize a table, respectively. If memory allocation
859 * failed, false is returned that the table is uninitialized. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800860bool upb_inttable_init(upb_inttable *table, upb_ctype_t ctype);
861bool upb_strtable_init(upb_strtable *table, upb_ctype_t ctype);
862void upb_inttable_uninit(upb_inttable *table);
863void upb_strtable_uninit(upb_strtable *table);
864
Josh Habermane8ed0212015-06-08 17:56:03 -0700865/* Returns the number of values in the table. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800866size_t upb_inttable_count(const upb_inttable *t);
867UPB_INLINE size_t upb_strtable_count(const upb_strtable *t) {
868 return t->t.count;
869}
870
Josh Habermane8ed0212015-06-08 17:56:03 -0700871/* Inserts the given key into the hashtable with the given value. The key must
872 * not already exist in the hash table. For string tables, the key must be
873 * NULL-terminated, and the table will make an internal copy of the key.
874 * Inttables must not insert a value of UINTPTR_MAX.
875 *
876 * If a table resize was required but memory allocation failed, false is
877 * returned and the table is unchanged. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800878bool upb_inttable_insert(upb_inttable *t, uintptr_t key, upb_value val);
Chris Fallinfd1a3ff2015-01-06 15:44:09 -0800879bool upb_strtable_insert2(upb_strtable *t, const char *key, size_t len,
880 upb_value val);
881
Josh Habermane8ed0212015-06-08 17:56:03 -0700882/* For NULL-terminated strings. */
Chris Fallinfd1a3ff2015-01-06 15:44:09 -0800883UPB_INLINE bool upb_strtable_insert(upb_strtable *t, const char *key,
884 upb_value val) {
885 return upb_strtable_insert2(t, key, strlen(key), val);
886}
Chris Fallin91473dc2014-12-12 15:58:26 -0800887
Josh Habermane8ed0212015-06-08 17:56:03 -0700888/* Looks up key in this table, returning "true" if the key was found.
889 * If v is non-NULL, copies the value for this key into *v. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800890bool upb_inttable_lookup(const upb_inttable *t, uintptr_t key, upb_value *v);
891bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len,
892 upb_value *v);
893
Josh Habermane8ed0212015-06-08 17:56:03 -0700894/* For NULL-terminated strings. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800895UPB_INLINE bool upb_strtable_lookup(const upb_strtable *t, const char *key,
896 upb_value *v) {
897 return upb_strtable_lookup2(t, key, strlen(key), v);
898}
899
Josh Habermane8ed0212015-06-08 17:56:03 -0700900/* Removes an item from the table. Returns true if the remove was successful,
901 * and stores the removed item in *val if non-NULL. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800902bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val);
Chris Fallinfd1a3ff2015-01-06 15:44:09 -0800903bool upb_strtable_remove2(upb_strtable *t, const char *key, size_t len,
904 upb_value *val);
905
Josh Habermane8ed0212015-06-08 17:56:03 -0700906/* For NULL-terminated strings. */
Chris Fallinfd1a3ff2015-01-06 15:44:09 -0800907UPB_INLINE bool upb_strtable_remove(upb_strtable *t, const char *key,
908 upb_value *v) {
909 return upb_strtable_remove2(t, key, strlen(key), v);
910}
Chris Fallin91473dc2014-12-12 15:58:26 -0800911
Josh Habermane8ed0212015-06-08 17:56:03 -0700912/* Updates an existing entry in an inttable. If the entry does not exist,
913 * returns false and does nothing. Unlike insert/remove, this does not
914 * invalidate iterators. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800915bool upb_inttable_replace(upb_inttable *t, uintptr_t key, upb_value val);
916
Josh Habermane8ed0212015-06-08 17:56:03 -0700917/* Handy routines for treating an inttable like a stack. May not be mixed with
918 * other insert/remove calls. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800919bool upb_inttable_push(upb_inttable *t, upb_value val);
920upb_value upb_inttable_pop(upb_inttable *t);
921
Josh Habermane8ed0212015-06-08 17:56:03 -0700922/* Convenience routines for inttables with pointer keys. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800923bool upb_inttable_insertptr(upb_inttable *t, const void *key, upb_value val);
924bool upb_inttable_removeptr(upb_inttable *t, const void *key, upb_value *val);
925bool upb_inttable_lookupptr(
926 const upb_inttable *t, const void *key, upb_value *val);
927
Josh Habermane8ed0212015-06-08 17:56:03 -0700928/* Optimizes the table for the current set of entries, for both memory use and
929 * lookup time. Client should call this after all entries have been inserted;
930 * inserting more entries is legal, but will likely require a table resize. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800931void upb_inttable_compact(upb_inttable *t);
932
Josh Habermane8ed0212015-06-08 17:56:03 -0700933/* A special-case inlinable version of the lookup routine for 32-bit
934 * integers. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800935UPB_INLINE bool upb_inttable_lookup32(const upb_inttable *t, uint32_t key,
936 upb_value *v) {
Josh Habermane8ed0212015-06-08 17:56:03 -0700937 *v = upb_value_int32(0); /* Silence compiler warnings. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800938 if (key < t->array_size) {
Josh Habermane8ed0212015-06-08 17:56:03 -0700939 upb_tabval arrval = t->array[key];
Chris Fallin91473dc2014-12-12 15:58:26 -0800940 if (upb_arrhas(arrval)) {
Josh Habermane8ed0212015-06-08 17:56:03 -0700941 _upb_value_setval(v, arrval.val, t->t.ctype);
Chris Fallin91473dc2014-12-12 15:58:26 -0800942 return true;
943 } else {
944 return false;
945 }
946 } else {
947 const upb_tabent *e;
948 if (t->t.entries == NULL) return false;
949 for (e = upb_getentry(&t->t, upb_inthash(key)); true; e = e->next) {
Josh Habermane8ed0212015-06-08 17:56:03 -0700950 if ((uint32_t)e->key == key) {
951 _upb_value_setval(v, e->val.val, t->t.ctype);
Chris Fallin91473dc2014-12-12 15:58:26 -0800952 return true;
953 }
954 if (e->next == NULL) return false;
955 }
956 }
957}
958
Josh Habermane8ed0212015-06-08 17:56:03 -0700959/* Exposed for testing only. */
Chris Fallin91473dc2014-12-12 15:58:26 -0800960bool upb_strtable_resize(upb_strtable *t, size_t size_lg2);
961
962/* Iterators ******************************************************************/
963
Josh Habermane8ed0212015-06-08 17:56:03 -0700964/* Iterators for int and string tables. We are subject to some kind of unusual
965 * design constraints:
966 *
967 * For high-level languages:
968 * - we must be able to guarantee that we don't crash or corrupt memory even if
969 * the program accesses an invalidated iterator.
970 *
971 * For C++11 range-based for:
972 * - iterators must be copyable
973 * - iterators must be comparable
974 * - it must be possible to construct an "end" value.
975 *
976 * Iteration order is undefined.
977 *
978 * Modifying the table invalidates iterators. upb_{str,int}table_done() is
979 * guaranteed to work even on an invalidated iterator, as long as the table it
980 * is iterating over has not been freed. Calling next() or accessing data from
981 * an invalidated iterator yields unspecified elements from the table, but it is
982 * guaranteed not to crash and to return real table elements (except when done()
983 * is true). */
Chris Fallin91473dc2014-12-12 15:58:26 -0800984
985
986/* upb_strtable_iter **********************************************************/
987
Josh Habermane8ed0212015-06-08 17:56:03 -0700988/* upb_strtable_iter i;
989 * upb_strtable_begin(&i, t);
990 * for(; !upb_strtable_done(&i); upb_strtable_next(&i)) {
991 * const char *key = upb_strtable_iter_key(&i);
992 * const upb_value val = upb_strtable_iter_value(&i);
993 * // ...
994 * }
995 */
Chris Fallin91473dc2014-12-12 15:58:26 -0800996
997typedef struct {
998 const upb_strtable *t;
999 size_t index;
1000} upb_strtable_iter;
1001
1002void upb_strtable_begin(upb_strtable_iter *i, const upb_strtable *t);
1003void upb_strtable_next(upb_strtable_iter *i);
1004bool upb_strtable_done(const upb_strtable_iter *i);
1005const char *upb_strtable_iter_key(upb_strtable_iter *i);
Chris Fallinfd1a3ff2015-01-06 15:44:09 -08001006size_t upb_strtable_iter_keylength(upb_strtable_iter *i);
Chris Fallin91473dc2014-12-12 15:58:26 -08001007upb_value upb_strtable_iter_value(const upb_strtable_iter *i);
1008void upb_strtable_iter_setdone(upb_strtable_iter *i);
1009bool upb_strtable_iter_isequal(const upb_strtable_iter *i1,
1010 const upb_strtable_iter *i2);
1011
1012
1013/* upb_inttable_iter **********************************************************/
1014
Josh Habermane8ed0212015-06-08 17:56:03 -07001015/* upb_inttable_iter i;
1016 * upb_inttable_begin(&i, t);
1017 * for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
1018 * uintptr_t key = upb_inttable_iter_key(&i);
1019 * upb_value val = upb_inttable_iter_value(&i);
1020 * // ...
1021 * }
1022 */
Chris Fallin91473dc2014-12-12 15:58:26 -08001023
1024typedef struct {
1025 const upb_inttable *t;
1026 size_t index;
1027 bool array_part;
1028} upb_inttable_iter;
1029
1030void upb_inttable_begin(upb_inttable_iter *i, const upb_inttable *t);
1031void upb_inttable_next(upb_inttable_iter *i);
1032bool upb_inttable_done(const upb_inttable_iter *i);
1033uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i);
1034upb_value upb_inttable_iter_value(const upb_inttable_iter *i);
1035void upb_inttable_iter_setdone(upb_inttable_iter *i);
1036bool upb_inttable_iter_isequal(const upb_inttable_iter *i1,
1037 const upb_inttable_iter *i2);
1038
1039
1040#ifdef __cplusplus
1041} /* extern "C" */
1042#endif
1043
1044#endif /* UPB_TABLE_H_ */
1045
Josh Habermane8ed0212015-06-08 17:56:03 -07001046/* Reference tracking will check ref()/unref() operations to make sure the
1047 * ref ownership is correct. Where possible it will also make tools like
1048 * Valgrind attribute ref leaks to the code that took the leaked ref, not
1049 * the code that originally created the object.
1050 *
1051 * Enabling this requires the application to define upb_lock()/upb_unlock()
1052 * functions that acquire/release a global mutex (or #define UPB_THREAD_UNSAFE).
1053 * For this reason we don't enable it by default, even in debug builds.
1054 */
1055
1056/* #define UPB_DEBUG_REFS */
Chris Fallin91473dc2014-12-12 15:58:26 -08001057
1058#ifdef __cplusplus
1059namespace upb { class RefCounted; }
1060#endif
1061
Josh Habermane8ed0212015-06-08 17:56:03 -07001062UPB_DECLARE_TYPE(upb::RefCounted, upb_refcounted)
Chris Fallin91473dc2014-12-12 15:58:26 -08001063
1064struct upb_refcounted_vtbl;
1065
Josh Habermane8ed0212015-06-08 17:56:03 -07001066#ifdef __cplusplus
1067
1068class upb::RefCounted {
Chris Fallin91473dc2014-12-12 15:58:26 -08001069 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07001070 /* Returns true if the given object is frozen. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001071 bool IsFrozen() const;
1072
Josh Habermane8ed0212015-06-08 17:56:03 -07001073 /* Increases the ref count, the new ref is owned by "owner" which must not
1074 * already own a ref (and should not itself be a refcounted object if the ref
1075 * could possibly be circular; see below).
1076 * Thread-safe iff "this" is frozen. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001077 void Ref(const void *owner) const;
1078
Josh Habermane8ed0212015-06-08 17:56:03 -07001079 /* Release a ref that was acquired from upb_refcounted_ref() and collects any
1080 * objects it can. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001081 void Unref(const void *owner) const;
1082
Josh Habermane8ed0212015-06-08 17:56:03 -07001083 /* Moves an existing ref from "from" to "to", without changing the overall
1084 * ref count. DonateRef(foo, NULL, owner) is the same as Ref(foo, owner),
1085 * but "to" may not be NULL. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001086 void DonateRef(const void *from, const void *to) const;
1087
Josh Habermane8ed0212015-06-08 17:56:03 -07001088 /* Verifies that a ref to the given object is currently held by the given
1089 * owner. Only effective in UPB_DEBUG_REFS builds. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001090 void CheckRef(const void *owner) const;
1091
1092 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07001093 UPB_DISALLOW_POD_OPS(RefCounted, upb::RefCounted)
1094#else
1095struct upb_refcounted {
1096#endif
1097 /* TODO(haberman): move the actual structure definition to structdefs.int.h.
1098 * The only reason they are here is because inline functions need to see the
1099 * definition of upb_handlers, which needs to see this definition. But we
1100 * can change the upb_handlers inline functions to deal in raw offsets
1101 * instead.
1102 */
1103
1104 /* A single reference count shared by all objects in the group. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001105 uint32_t *group;
1106
Josh Habermane8ed0212015-06-08 17:56:03 -07001107 /* A singly-linked list of all objects in the group. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001108 upb_refcounted *next;
1109
Josh Habermane8ed0212015-06-08 17:56:03 -07001110 /* Table of function pointers for this type. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001111 const struct upb_refcounted_vtbl *vtbl;
1112
Josh Habermane8ed0212015-06-08 17:56:03 -07001113 /* Maintained only when mutable, this tracks the number of refs (but not
1114 * ref2's) to this object. *group should be the sum of all individual_count
1115 * in the group. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001116 uint32_t individual_count;
1117
1118 bool is_frozen;
1119
1120#ifdef UPB_DEBUG_REFS
Josh Habermane8ed0212015-06-08 17:56:03 -07001121 upb_inttable *refs; /* Maps owner -> trackedref for incoming refs. */
1122 upb_inttable *ref2s; /* Set of targets for outgoing ref2s. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001123#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08001124};
1125
Chris Fallin91473dc2014-12-12 15:58:26 -08001126#ifdef UPB_DEBUG_REFS
1127#define UPB_REFCOUNT_INIT(refs, ref2s) \
1128 {&static_refcount, NULL, NULL, 0, true, refs, ref2s}
1129#else
1130#define UPB_REFCOUNT_INIT(refs, ref2s) {&static_refcount, NULL, NULL, 0, true}
1131#endif
1132
Josh Habermane8ed0212015-06-08 17:56:03 -07001133UPB_BEGIN_EXTERN_C
1134
1135/* It is better to use tracked refs when possible, for the extra debugging
1136 * capability. But if this is not possible (because you don't have easy access
1137 * to a stable pointer value that is associated with the ref), you can pass
1138 * UPB_UNTRACKED_REF instead. */
1139extern const void *UPB_UNTRACKED_REF;
1140
1141/* Native C API. */
1142bool upb_refcounted_isfrozen(const upb_refcounted *r);
1143void upb_refcounted_ref(const upb_refcounted *r, const void *owner);
1144void upb_refcounted_unref(const upb_refcounted *r, const void *owner);
1145void upb_refcounted_donateref(
1146 const upb_refcounted *r, const void *from, const void *to);
1147void upb_refcounted_checkref(const upb_refcounted *r, const void *owner);
1148
1149#define UPB_REFCOUNTED_CMETHODS(type, upcastfunc) \
1150 UPB_INLINE bool type ## _isfrozen(const type *v) { \
1151 return upb_refcounted_isfrozen(upcastfunc(v)); \
1152 } \
1153 UPB_INLINE void type ## _ref(const type *v, const void *owner) { \
1154 upb_refcounted_ref(upcastfunc(v), owner); \
1155 } \
1156 UPB_INLINE void type ## _unref(const type *v, const void *owner) { \
1157 upb_refcounted_unref(upcastfunc(v), owner); \
1158 } \
1159 UPB_INLINE void type ## _donateref(const type *v, const void *from, const void *to) { \
1160 upb_refcounted_donateref(upcastfunc(v), from, to); \
1161 } \
1162 UPB_INLINE void type ## _checkref(const type *v, const void *owner) { \
1163 upb_refcounted_checkref(upcastfunc(v), owner); \
1164 }
1165
1166#define UPB_REFCOUNTED_CPPMETHODS \
1167 bool IsFrozen() const { \
1168 return upb::upcast_to<const upb::RefCounted>(this)->IsFrozen(); \
1169 } \
1170 void Ref(const void *owner) const { \
1171 return upb::upcast_to<const upb::RefCounted>(this)->Ref(owner); \
1172 } \
1173 void Unref(const void *owner) const { \
1174 return upb::upcast_to<const upb::RefCounted>(this)->Unref(owner); \
1175 } \
1176 void DonateRef(const void *from, const void *to) const { \
1177 return upb::upcast_to<const upb::RefCounted>(this)->DonateRef(from, to); \
1178 } \
1179 void CheckRef(const void *owner) const { \
1180 return upb::upcast_to<const upb::RefCounted>(this)->CheckRef(owner); \
1181 }
1182
1183/* Internal-to-upb Interface **************************************************/
1184
1185typedef void upb_refcounted_visit(const upb_refcounted *r,
1186 const upb_refcounted *subobj,
1187 void *closure);
1188
1189struct upb_refcounted_vtbl {
1190 /* Must visit all subobjects that are currently ref'd via upb_refcounted_ref2.
1191 * Must be longjmp()-safe. */
1192 void (*visit)(const upb_refcounted *r, upb_refcounted_visit *visit, void *c);
1193
1194 /* Must free the object and release all references to other objects. */
1195 void (*free)(upb_refcounted *r);
1196};
1197
1198/* Initializes the refcounted with a single ref for the given owner. Returns
1199 * false if memory could not be allocated. */
1200bool upb_refcounted_init(upb_refcounted *r,
1201 const struct upb_refcounted_vtbl *vtbl,
1202 const void *owner);
1203
1204/* Adds a ref from one refcounted object to another ("from" must not already
1205 * own a ref). These refs may be circular; cycles will be collected correctly
1206 * (if conservatively). These refs do not need to be freed in from's free()
1207 * function. */
1208void upb_refcounted_ref2(const upb_refcounted *r, upb_refcounted *from);
1209
1210/* Removes a ref that was acquired from upb_refcounted_ref2(), and collects any
1211 * object it can. This is only necessary when "from" no longer points to "r",
1212 * and not from from's "free" function. */
1213void upb_refcounted_unref2(const upb_refcounted *r, upb_refcounted *from);
1214
1215#define upb_ref2(r, from) \
1216 upb_refcounted_ref2((const upb_refcounted*)r, (upb_refcounted*)from)
1217#define upb_unref2(r, from) \
1218 upb_refcounted_unref2((const upb_refcounted*)r, (upb_refcounted*)from)
1219
1220/* Freezes all mutable object reachable by ref2() refs from the given roots.
1221 * This will split refcounting groups into precise SCC groups, so that
1222 * refcounting of frozen objects can be more aggressive. If memory allocation
1223 * fails, or if more than 2**31 mutable objects are reachable from "roots", or
1224 * if the maximum depth of the graph exceeds "maxdepth", false is returned and
1225 * the objects are unchanged.
1226 *
1227 * After this operation succeeds, the objects are frozen/const, and may not be
1228 * used through non-const pointers. In particular, they may not be passed as
1229 * the second parameter of upb_refcounted_{ref,unref}2(). On the upside, all
1230 * operations on frozen refcounteds are threadsafe, and objects will be freed
1231 * at the precise moment that they become unreachable.
1232 *
1233 * Caller must own refs on each object in the "roots" list. */
1234bool upb_refcounted_freeze(upb_refcounted *const*roots, int n, upb_status *s,
1235 int maxdepth);
1236
1237/* Shared by all compiled-in refcounted objects. */
1238extern uint32_t static_refcount;
1239
1240UPB_END_EXTERN_C
1241
Chris Fallin91473dc2014-12-12 15:58:26 -08001242#ifdef __cplusplus
Josh Habermane8ed0212015-06-08 17:56:03 -07001243/* C++ Wrappers. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001244namespace upb {
1245inline bool RefCounted::IsFrozen() const {
1246 return upb_refcounted_isfrozen(this);
1247}
1248inline void RefCounted::Ref(const void *owner) const {
1249 upb_refcounted_ref(this, owner);
1250}
1251inline void RefCounted::Unref(const void *owner) const {
1252 upb_refcounted_unref(this, owner);
1253}
1254inline void RefCounted::DonateRef(const void *from, const void *to) const {
1255 upb_refcounted_donateref(this, from, to);
1256}
1257inline void RefCounted::CheckRef(const void *owner) const {
1258 upb_refcounted_checkref(this, owner);
1259}
Josh Habermane8ed0212015-06-08 17:56:03 -07001260} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08001261#endif
1262
Josh Habermane8ed0212015-06-08 17:56:03 -07001263#endif /* UPB_REFCOUNT_H_ */
Chris Fallin91473dc2014-12-12 15:58:26 -08001264
1265#ifdef __cplusplus
1266#include <cstring>
1267#include <string>
1268#include <vector>
1269
1270namespace upb {
1271class Def;
1272class EnumDef;
1273class FieldDef;
Josh Haberman94e54b32016-04-14 12:06:09 -07001274class FileDef;
Chris Fallin91473dc2014-12-12 15:58:26 -08001275class MessageDef;
Chris Fallinfcd88892015-01-13 18:14:39 -08001276class OneofDef;
Chris Fallin91473dc2014-12-12 15:58:26 -08001277}
1278#endif
1279
Josh Habermane8ed0212015-06-08 17:56:03 -07001280UPB_DECLARE_DERIVED_TYPE(upb::Def, upb::RefCounted, upb_def, upb_refcounted)
Josh Haberman94e54b32016-04-14 12:06:09 -07001281UPB_DECLARE_DERIVED_TYPE(upb::OneofDef, upb::RefCounted, upb_oneofdef,
1282 upb_refcounted)
1283UPB_DECLARE_DERIVED_TYPE(upb::FileDef, upb::RefCounted, upb_filedef,
1284 upb_refcounted)
Chris Fallin91473dc2014-12-12 15:58:26 -08001285
Josh Habermane8ed0212015-06-08 17:56:03 -07001286/* The maximum message depth that the type graph can have. This is a resource
1287 * limit for the C stack since we sometimes need to recursively traverse the
1288 * graph. Cycles are ok; the traversal will stop when it detects a cycle, but
1289 * we must hit the cycle before the maximum depth is reached.
1290 *
1291 * If having a single static limit is too inflexible, we can add another variant
1292 * of Def::Freeze that allows specifying this as a parameter. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001293#define UPB_MAX_MESSAGE_DEPTH 64
1294
1295
Josh Haberman94e54b32016-04-14 12:06:09 -07001296/* upb::Def: base class for top-level defs ***********************************/
Chris Fallin91473dc2014-12-12 15:58:26 -08001297
Josh Haberman94e54b32016-04-14 12:06:09 -07001298/* All the different kind of defs that can be defined at the top-level and put
1299 * in a SymbolTable or appear in a FileDef::defs() list. This excludes some
1300 * defs (like oneofs and files). It only includes fields because they can be
1301 * defined as extensions. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001302typedef enum {
1303 UPB_DEF_MSG,
1304 UPB_DEF_FIELD,
1305 UPB_DEF_ENUM,
Josh Habermane8ed0212015-06-08 17:56:03 -07001306 UPB_DEF_SERVICE, /* Not yet implemented. */
1307 UPB_DEF_ANY = -1 /* Wildcard for upb_symtab_get*() */
Chris Fallin91473dc2014-12-12 15:58:26 -08001308} upb_deftype_t;
1309
Josh Habermane8ed0212015-06-08 17:56:03 -07001310#ifdef __cplusplus
1311
1312/* The base class of all defs. Its base is upb::RefCounted (use upb::upcast()
1313 * to convert). */
1314class upb::Def {
Chris Fallin91473dc2014-12-12 15:58:26 -08001315 public:
1316 typedef upb_deftype_t Type;
1317
1318 Def* Dup(const void *owner) const;
1319
Josh Habermane8ed0212015-06-08 17:56:03 -07001320 /* upb::RefCounted methods like Ref()/Unref(). */
1321 UPB_REFCOUNTED_CPPMETHODS
Chris Fallin91473dc2014-12-12 15:58:26 -08001322
1323 Type def_type() const;
1324
Josh Habermane8ed0212015-06-08 17:56:03 -07001325 /* "fullname" is the def's fully-qualified name (eg. foo.bar.Message). */
Chris Fallin91473dc2014-12-12 15:58:26 -08001326 const char *full_name() const;
1327
Josh Haberman94e54b32016-04-14 12:06:09 -07001328 /* The final part of a def's name (eg. Message). */
1329 const char *name() const;
1330
Josh Habermane8ed0212015-06-08 17:56:03 -07001331 /* The def must be mutable. Caller retains ownership of fullname. Defs are
1332 * not required to have a name; if a def has no name when it is frozen, it
1333 * will remain an anonymous def. On failure, returns false and details in "s"
1334 * if non-NULL. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001335 bool set_full_name(const char* fullname, upb::Status* s);
1336 bool set_full_name(const std::string &fullname, upb::Status* s);
1337
Josh Haberman94e54b32016-04-14 12:06:09 -07001338 /* The file in which this def appears. It is not necessary to add a def to a
1339 * file (and consequently the accessor may return NULL). Set this by calling
1340 * file->Add(def). */
1341 FileDef* file() const;
1342
Josh Habermane8ed0212015-06-08 17:56:03 -07001343 /* Freezes the given defs; this validates all constraints and marks the defs
1344 * as frozen (read-only). "defs" may not contain any fielddefs, but fields
1345 * of any msgdefs will be frozen.
1346 *
1347 * Symbolic references to sub-types and enum defaults must have already been
1348 * resolved. Any mutable defs reachable from any of "defs" must also be in
1349 * the list; more formally, "defs" must be a transitive closure of mutable
1350 * defs.
1351 *
1352 * After this operation succeeds, the finalized defs must only be accessed
1353 * through a const pointer! */
Josh Haberman94e54b32016-04-14 12:06:09 -07001354 static bool Freeze(Def* const* defs, size_t n, Status* status);
Chris Fallin91473dc2014-12-12 15:58:26 -08001355 static bool Freeze(const std::vector<Def*>& defs, Status* status);
1356
1357 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07001358 UPB_DISALLOW_POD_OPS(Def, upb::Def)
1359};
Chris Fallin91473dc2014-12-12 15:58:26 -08001360
Josh Habermane8ed0212015-06-08 17:56:03 -07001361#endif /* __cplusplus */
Chris Fallin91473dc2014-12-12 15:58:26 -08001362
Josh Habermane8ed0212015-06-08 17:56:03 -07001363UPB_BEGIN_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08001364
Josh Habermane8ed0212015-06-08 17:56:03 -07001365/* Native C API. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001366upb_def *upb_def_dup(const upb_def *def, const void *owner);
1367
Josh Habermane8ed0212015-06-08 17:56:03 -07001368/* Include upb_refcounted methods like upb_def_ref()/upb_def_unref(). */
1369UPB_REFCOUNTED_CMETHODS(upb_def, upb_def_upcast)
Chris Fallin91473dc2014-12-12 15:58:26 -08001370
1371upb_deftype_t upb_def_type(const upb_def *d);
1372const char *upb_def_fullname(const upb_def *d);
Josh Haberman94e54b32016-04-14 12:06:09 -07001373const char *upb_def_name(const upb_def *d);
1374const upb_filedef *upb_def_file(const upb_def *d);
Chris Fallin91473dc2014-12-12 15:58:26 -08001375bool upb_def_setfullname(upb_def *def, const char *fullname, upb_status *s);
Josh Haberman94e54b32016-04-14 12:06:09 -07001376bool upb_def_freeze(upb_def *const *defs, size_t n, upb_status *s);
1377
1378/* Temporary API: for internal use only. */
1379bool _upb_def_validate(upb_def *const*defs, size_t n, upb_status *s);
Chris Fallin91473dc2014-12-12 15:58:26 -08001380
Josh Habermane8ed0212015-06-08 17:56:03 -07001381UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08001382
1383
1384/* upb::Def casts *************************************************************/
1385
1386#ifdef __cplusplus
1387#define UPB_CPP_CASTS(cname, cpptype) \
1388 namespace upb { \
1389 template <> \
1390 inline cpptype *down_cast<cpptype *, Def>(Def * def) { \
1391 return upb_downcast_##cname##_mutable(def); \
1392 } \
1393 template <> \
1394 inline cpptype *dyn_cast<cpptype *, Def>(Def * def) { \
1395 return upb_dyncast_##cname##_mutable(def); \
1396 } \
1397 template <> \
1398 inline const cpptype *down_cast<const cpptype *, const Def>( \
1399 const Def *def) { \
1400 return upb_downcast_##cname(def); \
1401 } \
1402 template <> \
1403 inline const cpptype *dyn_cast<const cpptype *, const Def>(const Def *def) { \
1404 return upb_dyncast_##cname(def); \
1405 } \
1406 template <> \
1407 inline const cpptype *down_cast<const cpptype *, Def>(Def * def) { \
1408 return upb_downcast_##cname(def); \
1409 } \
1410 template <> \
1411 inline const cpptype *dyn_cast<const cpptype *, Def>(Def * def) { \
1412 return upb_dyncast_##cname(def); \
1413 } \
Josh Habermane8ed0212015-06-08 17:56:03 -07001414 } /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08001415#else
1416#define UPB_CPP_CASTS(cname, cpptype)
Josh Habermane8ed0212015-06-08 17:56:03 -07001417#endif /* __cplusplus */
Chris Fallin91473dc2014-12-12 15:58:26 -08001418
Josh Habermane8ed0212015-06-08 17:56:03 -07001419/* Dynamic casts, for determining if a def is of a particular type at runtime.
1420 * Downcasts, for when some wants to assert that a def is of a particular type.
1421 * These are only checked if we are building debug. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001422#define UPB_DEF_CASTS(lower, upper, cpptype) \
1423 UPB_INLINE const upb_##lower *upb_dyncast_##lower(const upb_def *def) { \
1424 if (upb_def_type(def) != UPB_DEF_##upper) return NULL; \
1425 return (upb_##lower *)def; \
1426 } \
1427 UPB_INLINE const upb_##lower *upb_downcast_##lower(const upb_def *def) { \
1428 assert(upb_def_type(def) == UPB_DEF_##upper); \
1429 return (const upb_##lower *)def; \
1430 } \
1431 UPB_INLINE upb_##lower *upb_dyncast_##lower##_mutable(upb_def *def) { \
1432 return (upb_##lower *)upb_dyncast_##lower(def); \
1433 } \
1434 UPB_INLINE upb_##lower *upb_downcast_##lower##_mutable(upb_def *def) { \
1435 return (upb_##lower *)upb_downcast_##lower(def); \
1436 } \
1437 UPB_CPP_CASTS(lower, cpptype)
1438
1439#define UPB_DEFINE_DEF(cppname, lower, upper, cppmethods, members) \
Josh Habermane8ed0212015-06-08 17:56:03 -07001440 UPB_DEFINE_CLASS2(cppname, upb::Def, upb::RefCounted, cppmethods, \
Chris Fallin91473dc2014-12-12 15:58:26 -08001441 members) \
1442 UPB_DEF_CASTS(lower, upper, cppname)
1443
Josh Habermane8ed0212015-06-08 17:56:03 -07001444#define UPB_DECLARE_DEF_TYPE(cppname, lower, upper) \
1445 UPB_DECLARE_DERIVED_TYPE2(cppname, upb::Def, upb::RefCounted, \
1446 upb_ ## lower, upb_def, upb_refcounted) \
1447 UPB_DEF_CASTS(lower, upper, cppname)
1448
1449UPB_DECLARE_DEF_TYPE(upb::FieldDef, fielddef, FIELD)
1450UPB_DECLARE_DEF_TYPE(upb::MessageDef, msgdef, MSG)
1451UPB_DECLARE_DEF_TYPE(upb::EnumDef, enumdef, ENUM)
Josh Habermane8ed0212015-06-08 17:56:03 -07001452
1453#undef UPB_DECLARE_DEF_TYPE
1454#undef UPB_DEF_CASTS
1455#undef UPB_CPP_CASTS
1456
Chris Fallin91473dc2014-12-12 15:58:26 -08001457
1458/* upb::FieldDef **************************************************************/
1459
Josh Habermane8ed0212015-06-08 17:56:03 -07001460/* The types a field can have. Note that this list is not identical to the
1461 * types defined in descriptor.proto, which gives INT32 and SINT32 separate
1462 * types (we distinguish the two with the "integer encoding" enum below). */
Chris Fallin91473dc2014-12-12 15:58:26 -08001463typedef enum {
1464 UPB_TYPE_FLOAT = 1,
1465 UPB_TYPE_DOUBLE = 2,
1466 UPB_TYPE_BOOL = 3,
1467 UPB_TYPE_STRING = 4,
1468 UPB_TYPE_BYTES = 5,
1469 UPB_TYPE_MESSAGE = 6,
Josh Habermane8ed0212015-06-08 17:56:03 -07001470 UPB_TYPE_ENUM = 7, /* Enum values are int32. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001471 UPB_TYPE_INT32 = 8,
1472 UPB_TYPE_UINT32 = 9,
1473 UPB_TYPE_INT64 = 10,
Josh Habermane8ed0212015-06-08 17:56:03 -07001474 UPB_TYPE_UINT64 = 11
Chris Fallin91473dc2014-12-12 15:58:26 -08001475} upb_fieldtype_t;
1476
Josh Habermane8ed0212015-06-08 17:56:03 -07001477/* The repeated-ness of each field; this matches descriptor.proto. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001478typedef enum {
1479 UPB_LABEL_OPTIONAL = 1,
1480 UPB_LABEL_REQUIRED = 2,
Josh Habermane8ed0212015-06-08 17:56:03 -07001481 UPB_LABEL_REPEATED = 3
Chris Fallin91473dc2014-12-12 15:58:26 -08001482} upb_label_t;
1483
Josh Habermane8ed0212015-06-08 17:56:03 -07001484/* How integers should be encoded in serializations that offer multiple
1485 * integer encoding methods. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001486typedef enum {
1487 UPB_INTFMT_VARIABLE = 1,
1488 UPB_INTFMT_FIXED = 2,
Josh Habermane8ed0212015-06-08 17:56:03 -07001489 UPB_INTFMT_ZIGZAG = 3 /* Only for signed types (INT32/INT64). */
Chris Fallin91473dc2014-12-12 15:58:26 -08001490} upb_intfmt_t;
1491
Josh Habermane8ed0212015-06-08 17:56:03 -07001492/* Descriptor types, as defined in descriptor.proto. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001493typedef enum {
1494 UPB_DESCRIPTOR_TYPE_DOUBLE = 1,
1495 UPB_DESCRIPTOR_TYPE_FLOAT = 2,
1496 UPB_DESCRIPTOR_TYPE_INT64 = 3,
1497 UPB_DESCRIPTOR_TYPE_UINT64 = 4,
1498 UPB_DESCRIPTOR_TYPE_INT32 = 5,
1499 UPB_DESCRIPTOR_TYPE_FIXED64 = 6,
1500 UPB_DESCRIPTOR_TYPE_FIXED32 = 7,
1501 UPB_DESCRIPTOR_TYPE_BOOL = 8,
1502 UPB_DESCRIPTOR_TYPE_STRING = 9,
1503 UPB_DESCRIPTOR_TYPE_GROUP = 10,
1504 UPB_DESCRIPTOR_TYPE_MESSAGE = 11,
1505 UPB_DESCRIPTOR_TYPE_BYTES = 12,
1506 UPB_DESCRIPTOR_TYPE_UINT32 = 13,
1507 UPB_DESCRIPTOR_TYPE_ENUM = 14,
1508 UPB_DESCRIPTOR_TYPE_SFIXED32 = 15,
1509 UPB_DESCRIPTOR_TYPE_SFIXED64 = 16,
1510 UPB_DESCRIPTOR_TYPE_SINT32 = 17,
Josh Habermane8ed0212015-06-08 17:56:03 -07001511 UPB_DESCRIPTOR_TYPE_SINT64 = 18
Chris Fallin91473dc2014-12-12 15:58:26 -08001512} upb_descriptortype_t;
1513
Josh Habermane8ed0212015-06-08 17:56:03 -07001514/* Maximum field number allowed for FieldDefs. This is an inherent limit of the
1515 * protobuf wire format. */
1516#define UPB_MAX_FIELDNUMBER ((1 << 29) - 1)
Chris Fallin91473dc2014-12-12 15:58:26 -08001517
Josh Habermane8ed0212015-06-08 17:56:03 -07001518#ifdef __cplusplus
1519
1520/* A upb_fielddef describes a single field in a message. It is most often
1521 * found as a part of a upb_msgdef, but can also stand alone to represent
1522 * an extension.
1523 *
1524 * Its base class is upb::Def (use upb::upcast() to convert). */
1525class upb::FieldDef {
Chris Fallin91473dc2014-12-12 15:58:26 -08001526 public:
1527 typedef upb_fieldtype_t Type;
1528 typedef upb_label_t Label;
1529 typedef upb_intfmt_t IntegerFormat;
1530 typedef upb_descriptortype_t DescriptorType;
1531
Josh Habermane8ed0212015-06-08 17:56:03 -07001532 /* These return true if the given value is a valid member of the enumeration. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001533 static bool CheckType(int32_t val);
1534 static bool CheckLabel(int32_t val);
1535 static bool CheckDescriptorType(int32_t val);
1536 static bool CheckIntegerFormat(int32_t val);
1537
Josh Habermane8ed0212015-06-08 17:56:03 -07001538 /* These convert to the given enumeration; they require that the value is
1539 * valid. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001540 static Type ConvertType(int32_t val);
1541 static Label ConvertLabel(int32_t val);
1542 static DescriptorType ConvertDescriptorType(int32_t val);
1543 static IntegerFormat ConvertIntegerFormat(int32_t val);
1544
Josh Habermane8ed0212015-06-08 17:56:03 -07001545 /* Returns NULL if memory allocation failed. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001546 static reffed_ptr<FieldDef> New();
1547
Josh Habermane8ed0212015-06-08 17:56:03 -07001548 /* Duplicates the given field, returning NULL if memory allocation failed.
1549 * When a fielddef is duplicated, the subdef (if any) is made symbolic if it
1550 * wasn't already. If the subdef is set but has no name (which is possible
1551 * since msgdefs are not required to have a name) the new fielddef's subdef
1552 * will be unset. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001553 FieldDef* Dup(const void* owner) const;
1554
Josh Habermane8ed0212015-06-08 17:56:03 -07001555 /* upb::RefCounted methods like Ref()/Unref(). */
1556 UPB_REFCOUNTED_CPPMETHODS
Chris Fallin91473dc2014-12-12 15:58:26 -08001557
Josh Habermane8ed0212015-06-08 17:56:03 -07001558 /* Functionality from upb::Def. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001559 const char* full_name() const;
1560
Josh Habermane8ed0212015-06-08 17:56:03 -07001561 bool type_is_set() const; /* set_[descriptor_]type() has been called? */
1562 Type type() const; /* Requires that type_is_set() == true. */
1563 Label label() const; /* Defaults to UPB_LABEL_OPTIONAL. */
1564 const char* name() const; /* NULL if uninitialized. */
1565 uint32_t number() const; /* Returns 0 if uninitialized. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001566 bool is_extension() const;
1567
Josh Habermanf654d492016-02-18 11:07:51 -08001568 /* Copies the JSON name for this field into the given buffer. Returns the
1569 * actual size of the JSON name, including the NULL terminator. If the
1570 * return value is 0, the JSON name is unset. If the return value is
1571 * greater than len, the JSON name was truncated. The buffer is always
1572 * NULL-terminated if len > 0.
1573 *
1574 * The JSON name always defaults to a camelCased version of the regular
1575 * name. However if the regular name is unset, the JSON name will be unset
1576 * also.
Josh Haberman78da6662016-01-13 19:05:43 -08001577 */
Josh Habermanf654d492016-02-18 11:07:51 -08001578 size_t GetJsonName(char* buf, size_t len) const;
1579
1580 /* Convenience version of the above function which copies the JSON name
1581 * into the given string, returning false if the name is not set. */
1582 template <class T>
1583 bool GetJsonName(T* str) {
1584 str->resize(GetJsonName(NULL, 0));
1585 GetJsonName(&(*str)[0], str->size());
1586 return str->size() > 0;
1587 }
Josh Haberman78da6662016-01-13 19:05:43 -08001588
Josh Habermane8ed0212015-06-08 17:56:03 -07001589 /* For UPB_TYPE_MESSAGE fields only where is_tag_delimited() == false,
1590 * indicates whether this field should have lazy parsing handlers that yield
1591 * the unparsed string for the submessage.
1592 *
1593 * TODO(haberman): I think we want to move this into a FieldOptions container
1594 * when we add support for custom options (the FieldOptions struct will
1595 * contain both regular FieldOptions like "lazy" *and* custom options). */
Chris Fallin91473dc2014-12-12 15:58:26 -08001596 bool lazy() const;
1597
Josh Habermane8ed0212015-06-08 17:56:03 -07001598 /* For non-string, non-submessage fields, this indicates whether binary
1599 * protobufs are encoded in packed or non-packed format.
1600 *
1601 * TODO(haberman): see note above about putting options like this into a
1602 * FieldOptions container. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001603 bool packed() const;
1604
Josh Habermane8ed0212015-06-08 17:56:03 -07001605 /* An integer that can be used as an index into an array of fields for
1606 * whatever message this field belongs to. Guaranteed to be less than
1607 * f->containing_type()->field_count(). May only be accessed once the def has
1608 * been finalized. */
Josh Haberman94e54b32016-04-14 12:06:09 -07001609 uint32_t index() const;
Chris Fallin91473dc2014-12-12 15:58:26 -08001610
Josh Habermane8ed0212015-06-08 17:56:03 -07001611 /* The MessageDef to which this field belongs.
1612 *
1613 * If this field has been added to a MessageDef, that message can be retrieved
1614 * directly (this is always the case for frozen FieldDefs).
1615 *
1616 * If the field has not yet been added to a MessageDef, you can set the name
1617 * of the containing type symbolically instead. This is mostly useful for
1618 * extensions, where the extension is declared separately from the message. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001619 const MessageDef* containing_type() const;
1620 const char* containing_type_name();
1621
Josh Habermane8ed0212015-06-08 17:56:03 -07001622 /* The OneofDef to which this field belongs, or NULL if this field is not part
1623 * of a oneof. */
Chris Fallinfcd88892015-01-13 18:14:39 -08001624 const OneofDef* containing_oneof() const;
1625
Josh Habermane8ed0212015-06-08 17:56:03 -07001626 /* The field's type according to the enum in descriptor.proto. This is not
1627 * the same as UPB_TYPE_*, because it distinguishes between (for example)
1628 * INT32 and SINT32, whereas our "type" enum does not. This return of
1629 * descriptor_type() is a function of type(), integer_format(), and
1630 * is_tag_delimited(). Likewise set_descriptor_type() sets all three
1631 * appropriately. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001632 DescriptorType descriptor_type() const;
1633
Josh Habermane8ed0212015-06-08 17:56:03 -07001634 /* Convenient field type tests. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001635 bool IsSubMessage() const;
1636 bool IsString() const;
1637 bool IsSequence() const;
1638 bool IsPrimitive() const;
Chris Fallina5075922015-02-02 15:07:34 -08001639 bool IsMap() const;
Chris Fallin91473dc2014-12-12 15:58:26 -08001640
Josh Haberman78da6662016-01-13 19:05:43 -08001641 /* Whether this field must be able to explicitly represent presence:
1642 *
1643 * * This is always false for repeated fields (an empty repeated field is
1644 * equivalent to a repeated field with zero entries).
1645 *
1646 * * This is always true for submessages.
1647 *
1648 * * For other fields, it depends on the message (see
1649 * MessageDef::SetPrimitivesHavePresence())
1650 */
1651 bool HasPresence() const;
1652
Josh Habermane8ed0212015-06-08 17:56:03 -07001653 /* How integers are encoded. Only meaningful for integer types.
1654 * Defaults to UPB_INTFMT_VARIABLE, and is reset when "type" changes. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001655 IntegerFormat integer_format() const;
1656
Josh Habermane8ed0212015-06-08 17:56:03 -07001657 /* Whether a submessage field is tag-delimited or not (if false, then
1658 * length-delimited). May only be set when type() == UPB_TYPE_MESSAGE. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001659 bool is_tag_delimited() const;
1660
Josh Habermane8ed0212015-06-08 17:56:03 -07001661 /* Returns the non-string default value for this fielddef, which may either
1662 * be something the client set explicitly or the "default default" (0 for
1663 * numbers, empty for strings). The field's type indicates the type of the
1664 * returned value, except for enum fields that are still mutable.
1665 *
1666 * Requires that the given function matches the field's current type. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001667 int64_t default_int64() const;
1668 int32_t default_int32() const;
1669 uint64_t default_uint64() const;
1670 uint32_t default_uint32() const;
1671 bool default_bool() const;
1672 float default_float() const;
1673 double default_double() const;
1674
Josh Habermane8ed0212015-06-08 17:56:03 -07001675 /* The resulting string is always NULL-terminated. If non-NULL, the length
1676 * will be stored in *len. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001677 const char *default_string(size_t* len) const;
1678
Josh Habermane8ed0212015-06-08 17:56:03 -07001679 /* For frozen UPB_TYPE_ENUM fields, enum defaults can always be read as either
1680 * string or int32, and both of these methods will always return true.
1681 *
1682 * For mutable UPB_TYPE_ENUM fields, the story is a bit more complicated.
1683 * Enum defaults are unusual. They can be specified either as string or int32,
1684 * but to be valid the enum must have that value as a member. And if no
1685 * default is specified, the "default default" comes from the EnumDef.
1686 *
1687 * We allow reading the default as either an int32 or a string, but only if
1688 * we have a meaningful value to report. We have a meaningful value if it was
1689 * set explicitly, or if we could get the "default default" from the EnumDef.
1690 * Also if you explicitly set the name and we find the number in the EnumDef */
Chris Fallin91473dc2014-12-12 15:58:26 -08001691 bool EnumHasStringDefault() const;
1692 bool EnumHasInt32Default() const;
1693
Josh Habermane8ed0212015-06-08 17:56:03 -07001694 /* Submessage and enum fields must reference a "subdef", which is the
1695 * upb::MessageDef or upb::EnumDef that defines their type. Note that when
1696 * the FieldDef is mutable it may not have a subdef *yet*, but this function
1697 * still returns true to indicate that the field's type requires a subdef. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001698 bool HasSubDef() const;
1699
Josh Habermane8ed0212015-06-08 17:56:03 -07001700 /* Returns the enum or submessage def for this field, if any. The field's
1701 * type must match (ie. you may only call enum_subdef() for fields where
1702 * type() == UPB_TYPE_ENUM). Returns NULL if the subdef has not been set or
1703 * is currently set symbolically. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001704 const EnumDef* enum_subdef() const;
1705 const MessageDef* message_subdef() const;
1706
Josh Habermane8ed0212015-06-08 17:56:03 -07001707 /* Returns the generic subdef for this field. Requires that HasSubDef() (ie.
1708 * only works for UPB_TYPE_ENUM and UPB_TYPE_MESSAGE fields). */
Chris Fallin91473dc2014-12-12 15:58:26 -08001709 const Def* subdef() const;
1710
Josh Habermane8ed0212015-06-08 17:56:03 -07001711 /* Returns the symbolic name of the subdef. If the subdef is currently set
1712 * unresolved (ie. set symbolically) returns the symbolic name. If it has
1713 * been resolved to a specific subdef, returns the name from that subdef. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001714 const char* subdef_name() const;
1715
Josh Habermane8ed0212015-06-08 17:56:03 -07001716 /* Setters (non-const methods), only valid for mutable FieldDefs! ***********/
Chris Fallin91473dc2014-12-12 15:58:26 -08001717
1718 bool set_full_name(const char* fullname, upb::Status* s);
1719 bool set_full_name(const std::string& fullname, upb::Status* s);
1720
Josh Habermane8ed0212015-06-08 17:56:03 -07001721 /* This may only be called if containing_type() == NULL (ie. the field has not
1722 * been added to a message yet). */
Chris Fallin91473dc2014-12-12 15:58:26 -08001723 bool set_containing_type_name(const char *name, Status* status);
1724 bool set_containing_type_name(const std::string& name, Status* status);
1725
Josh Habermane8ed0212015-06-08 17:56:03 -07001726 /* Defaults to false. When we freeze, we ensure that this can only be true
1727 * for length-delimited message fields. Prior to freezing this can be true or
1728 * false with no restrictions. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001729 void set_lazy(bool lazy);
1730
Josh Habermane8ed0212015-06-08 17:56:03 -07001731 /* Defaults to true. Sets whether this field is encoded in packed format. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001732 void set_packed(bool packed);
1733
Josh Habermane8ed0212015-06-08 17:56:03 -07001734 /* "type" or "descriptor_type" MUST be set explicitly before the fielddef is
1735 * finalized. These setters require that the enum value is valid; if the
1736 * value did not come directly from an enum constant, the caller should
1737 * validate it first with the functions above (CheckFieldType(), etc). */
Chris Fallin91473dc2014-12-12 15:58:26 -08001738 void set_type(Type type);
1739 void set_label(Label label);
1740 void set_descriptor_type(DescriptorType type);
1741 void set_is_extension(bool is_extension);
1742
Josh Habermane8ed0212015-06-08 17:56:03 -07001743 /* "number" and "name" must be set before the FieldDef is added to a
1744 * MessageDef, and may not be set after that.
1745 *
1746 * "name" is the same as full_name()/set_full_name(), but since fielddefs
1747 * most often use simple, non-qualified names, we provide this accessor
1748 * also. Generally only extensions will want to think of this name as
1749 * fully-qualified. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001750 bool set_number(uint32_t number, upb::Status* s);
1751 bool set_name(const char* name, upb::Status* s);
1752 bool set_name(const std::string& name, upb::Status* s);
1753
Josh Haberman78da6662016-01-13 19:05:43 -08001754 /* Sets the JSON name to the given string. */
1755 /* TODO(haberman): implement. Right now only default json_name (camelCase)
1756 * is supported. */
1757 bool set_json_name(const char* json_name, upb::Status* s);
1758 bool set_json_name(const std::string& name, upb::Status* s);
1759
1760 /* Clears the JSON name. This will make it revert to its default, which is
1761 * a camelCased version of the regular field name. */
1762 void clear_json_name();
1763
Chris Fallin91473dc2014-12-12 15:58:26 -08001764 void set_integer_format(IntegerFormat format);
1765 bool set_tag_delimited(bool tag_delimited, upb::Status* s);
1766
Josh Habermane8ed0212015-06-08 17:56:03 -07001767 /* Sets default value for the field. The call must exactly match the type
1768 * of the field. Enum fields may use either setint32 or setstring to set
1769 * the default numerically or symbolically, respectively, but symbolic
1770 * defaults must be resolved before finalizing (see ResolveEnumDefault()).
1771 *
1772 * Changing the type of a field will reset its default. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001773 void set_default_int64(int64_t val);
1774 void set_default_int32(int32_t val);
1775 void set_default_uint64(uint64_t val);
1776 void set_default_uint32(uint32_t val);
1777 void set_default_bool(bool val);
1778 void set_default_float(float val);
1779 void set_default_double(double val);
1780 bool set_default_string(const void *str, size_t len, Status *s);
1781 bool set_default_string(const std::string &str, Status *s);
1782 void set_default_cstr(const char *str, Status *s);
1783
Josh Habermane8ed0212015-06-08 17:56:03 -07001784 /* Before a fielddef is frozen, its subdef may be set either directly (with a
1785 * upb::Def*) or symbolically. Symbolic refs must be resolved before the
1786 * containing msgdef can be frozen (see upb_resolve() above). upb always
1787 * guarantees that any def reachable from a live def will also be kept alive.
1788 *
1789 * Both methods require that upb_hassubdef(f) (so the type must be set prior
1790 * to calling these methods). Returns false if this is not the case, or if
1791 * the given subdef is not of the correct type. The subdef is reset if the
1792 * field's type is changed. The subdef can be set to NULL to clear it. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001793 bool set_subdef(const Def* subdef, Status* s);
1794 bool set_enum_subdef(const EnumDef* subdef, Status* s);
1795 bool set_message_subdef(const MessageDef* subdef, Status* s);
1796 bool set_subdef_name(const char* name, Status* s);
1797 bool set_subdef_name(const std::string &name, Status* s);
1798
1799 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07001800 UPB_DISALLOW_POD_OPS(FieldDef, upb::FieldDef)
1801};
Chris Fallin91473dc2014-12-12 15:58:26 -08001802
Josh Habermane8ed0212015-06-08 17:56:03 -07001803# endif /* defined(__cplusplus) */
Chris Fallin91473dc2014-12-12 15:58:26 -08001804
Josh Habermane8ed0212015-06-08 17:56:03 -07001805UPB_BEGIN_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08001806
Josh Habermane8ed0212015-06-08 17:56:03 -07001807/* Native C API. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001808upb_fielddef *upb_fielddef_new(const void *owner);
1809upb_fielddef *upb_fielddef_dup(const upb_fielddef *f, const void *owner);
1810
Josh Habermane8ed0212015-06-08 17:56:03 -07001811/* Include upb_refcounted methods like upb_fielddef_ref(). */
1812UPB_REFCOUNTED_CMETHODS(upb_fielddef, upb_fielddef_upcast2)
Chris Fallin91473dc2014-12-12 15:58:26 -08001813
Josh Habermane8ed0212015-06-08 17:56:03 -07001814/* Methods from upb_def. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001815const char *upb_fielddef_fullname(const upb_fielddef *f);
1816bool upb_fielddef_setfullname(upb_fielddef *f, const char *fullname,
1817 upb_status *s);
1818
1819bool upb_fielddef_typeisset(const upb_fielddef *f);
1820upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f);
1821upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f);
1822upb_label_t upb_fielddef_label(const upb_fielddef *f);
1823uint32_t upb_fielddef_number(const upb_fielddef *f);
1824const char *upb_fielddef_name(const upb_fielddef *f);
1825bool upb_fielddef_isextension(const upb_fielddef *f);
1826bool upb_fielddef_lazy(const upb_fielddef *f);
1827bool upb_fielddef_packed(const upb_fielddef *f);
Josh Habermanf654d492016-02-18 11:07:51 -08001828size_t upb_fielddef_getjsonname(const upb_fielddef *f, char *buf, size_t len);
Chris Fallin91473dc2014-12-12 15:58:26 -08001829const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f);
Chris Fallinfcd88892015-01-13 18:14:39 -08001830const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f);
Chris Fallin91473dc2014-12-12 15:58:26 -08001831upb_msgdef *upb_fielddef_containingtype_mutable(upb_fielddef *f);
1832const char *upb_fielddef_containingtypename(upb_fielddef *f);
1833upb_intfmt_t upb_fielddef_intfmt(const upb_fielddef *f);
1834uint32_t upb_fielddef_index(const upb_fielddef *f);
1835bool upb_fielddef_istagdelim(const upb_fielddef *f);
1836bool upb_fielddef_issubmsg(const upb_fielddef *f);
1837bool upb_fielddef_isstring(const upb_fielddef *f);
1838bool upb_fielddef_isseq(const upb_fielddef *f);
1839bool upb_fielddef_isprimitive(const upb_fielddef *f);
Chris Fallina5075922015-02-02 15:07:34 -08001840bool upb_fielddef_ismap(const upb_fielddef *f);
Josh Haberman78da6662016-01-13 19:05:43 -08001841bool upb_fielddef_haspresence(const upb_fielddef *f);
Chris Fallin91473dc2014-12-12 15:58:26 -08001842int64_t upb_fielddef_defaultint64(const upb_fielddef *f);
1843int32_t upb_fielddef_defaultint32(const upb_fielddef *f);
1844uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f);
1845uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f);
1846bool upb_fielddef_defaultbool(const upb_fielddef *f);
1847float upb_fielddef_defaultfloat(const upb_fielddef *f);
1848double upb_fielddef_defaultdouble(const upb_fielddef *f);
1849const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len);
1850bool upb_fielddef_enumhasdefaultint32(const upb_fielddef *f);
1851bool upb_fielddef_enumhasdefaultstr(const upb_fielddef *f);
1852bool upb_fielddef_hassubdef(const upb_fielddef *f);
1853const upb_def *upb_fielddef_subdef(const upb_fielddef *f);
1854const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f);
1855const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f);
1856const char *upb_fielddef_subdefname(const upb_fielddef *f);
1857
1858void upb_fielddef_settype(upb_fielddef *f, upb_fieldtype_t type);
1859void upb_fielddef_setdescriptortype(upb_fielddef *f, int type);
1860void upb_fielddef_setlabel(upb_fielddef *f, upb_label_t label);
1861bool upb_fielddef_setnumber(upb_fielddef *f, uint32_t number, upb_status *s);
1862bool upb_fielddef_setname(upb_fielddef *f, const char *name, upb_status *s);
Josh Haberman78da6662016-01-13 19:05:43 -08001863bool upb_fielddef_setjsonname(upb_fielddef *f, const char *name, upb_status *s);
1864bool upb_fielddef_clearjsonname(upb_fielddef *f);
Chris Fallin91473dc2014-12-12 15:58:26 -08001865bool upb_fielddef_setcontainingtypename(upb_fielddef *f, const char *name,
1866 upb_status *s);
1867void upb_fielddef_setisextension(upb_fielddef *f, bool is_extension);
1868void upb_fielddef_setlazy(upb_fielddef *f, bool lazy);
1869void upb_fielddef_setpacked(upb_fielddef *f, bool packed);
1870void upb_fielddef_setintfmt(upb_fielddef *f, upb_intfmt_t fmt);
1871void upb_fielddef_settagdelim(upb_fielddef *f, bool tag_delim);
1872void upb_fielddef_setdefaultint64(upb_fielddef *f, int64_t val);
1873void upb_fielddef_setdefaultint32(upb_fielddef *f, int32_t val);
1874void upb_fielddef_setdefaultuint64(upb_fielddef *f, uint64_t val);
1875void upb_fielddef_setdefaultuint32(upb_fielddef *f, uint32_t val);
1876void upb_fielddef_setdefaultbool(upb_fielddef *f, bool val);
1877void upb_fielddef_setdefaultfloat(upb_fielddef *f, float val);
1878void upb_fielddef_setdefaultdouble(upb_fielddef *f, double val);
1879bool upb_fielddef_setdefaultstr(upb_fielddef *f, const void *str, size_t len,
1880 upb_status *s);
1881void upb_fielddef_setdefaultcstr(upb_fielddef *f, const char *str,
1882 upb_status *s);
1883bool upb_fielddef_setsubdef(upb_fielddef *f, const upb_def *subdef,
1884 upb_status *s);
1885bool upb_fielddef_setmsgsubdef(upb_fielddef *f, const upb_msgdef *subdef,
1886 upb_status *s);
1887bool upb_fielddef_setenumsubdef(upb_fielddef *f, const upb_enumdef *subdef,
1888 upb_status *s);
1889bool upb_fielddef_setsubdefname(upb_fielddef *f, const char *name,
1890 upb_status *s);
1891
1892bool upb_fielddef_checklabel(int32_t label);
1893bool upb_fielddef_checktype(int32_t type);
1894bool upb_fielddef_checkdescriptortype(int32_t type);
1895bool upb_fielddef_checkintfmt(int32_t fmt);
1896
Josh Habermane8ed0212015-06-08 17:56:03 -07001897UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08001898
1899
1900/* upb::MessageDef ************************************************************/
1901
Chris Fallinfcd88892015-01-13 18:14:39 -08001902typedef upb_inttable_iter upb_msg_field_iter;
1903typedef upb_strtable_iter upb_msg_oneof_iter;
Chris Fallin91473dc2014-12-12 15:58:26 -08001904
Josh Habermane8ed0212015-06-08 17:56:03 -07001905#ifdef __cplusplus
1906
1907/* Structure that describes a single .proto message type.
1908 *
1909 * Its base class is upb::Def (use upb::upcast() to convert). */
1910class upb::MessageDef {
Chris Fallin91473dc2014-12-12 15:58:26 -08001911 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07001912 /* Returns NULL if memory allocation failed. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001913 static reffed_ptr<MessageDef> New();
1914
Josh Habermane8ed0212015-06-08 17:56:03 -07001915 /* upb::RefCounted methods like Ref()/Unref(). */
1916 UPB_REFCOUNTED_CPPMETHODS
Chris Fallin91473dc2014-12-12 15:58:26 -08001917
Josh Habermane8ed0212015-06-08 17:56:03 -07001918 /* Functionality from upb::Def. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001919 const char* full_name() const;
Josh Haberman94e54b32016-04-14 12:06:09 -07001920 const char* name() const;
Chris Fallin91473dc2014-12-12 15:58:26 -08001921 bool set_full_name(const char* fullname, Status* s);
1922 bool set_full_name(const std::string& fullname, Status* s);
1923
Josh Habermane8ed0212015-06-08 17:56:03 -07001924 /* Call to freeze this MessageDef.
1925 * WARNING: this will fail if this message has any unfrozen submessages!
1926 * Messages with cycles must be frozen as a batch using upb::Def::Freeze(). */
Chris Fallin91473dc2014-12-12 15:58:26 -08001927 bool Freeze(Status* s);
1928
Josh Habermane8ed0212015-06-08 17:56:03 -07001929 /* The number of fields that belong to the MessageDef. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001930 int field_count() const;
1931
Josh Habermane8ed0212015-06-08 17:56:03 -07001932 /* The number of oneofs that belong to the MessageDef. */
Chris Fallinfcd88892015-01-13 18:14:39 -08001933 int oneof_count() const;
1934
Josh Habermane8ed0212015-06-08 17:56:03 -07001935 /* Adds a field (upb_fielddef object) to a msgdef. Requires that the msgdef
1936 * and the fielddefs are mutable. The fielddef's name and number must be
1937 * set, and the message may not already contain any field with this name or
1938 * number, and this fielddef may not be part of another message. In error
1939 * cases false is returned and the msgdef is unchanged.
1940 *
1941 * If the given field is part of a oneof, this call succeeds if and only if
1942 * that oneof is already part of this msgdef. (Note that adding a oneof to a
1943 * msgdef automatically adds all of its fields to the msgdef at the time that
1944 * the oneof is added, so it is usually more idiomatic to add the oneof's
1945 * fields first then add the oneof to the msgdef. This case is supported for
1946 * convenience.)
1947 *
1948 * If |f| is already part of this MessageDef, this method performs no action
1949 * and returns true (success). Thus, this method is idempotent. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001950 bool AddField(FieldDef* f, Status* s);
1951 bool AddField(const reffed_ptr<FieldDef>& f, Status* s);
1952
Josh Habermane8ed0212015-06-08 17:56:03 -07001953 /* Adds a oneof (upb_oneofdef object) to a msgdef. Requires that the msgdef,
1954 * oneof, and any fielddefs are mutable, that the fielddefs contained in the
1955 * oneof do not have any name or number conflicts with existing fields in the
1956 * msgdef, and that the oneof's name is unique among all oneofs in the msgdef.
1957 * If the oneof is added successfully, all of its fields will be added
1958 * directly to the msgdef as well. In error cases, false is returned and the
1959 * msgdef is unchanged. */
Chris Fallinfcd88892015-01-13 18:14:39 -08001960 bool AddOneof(OneofDef* o, Status* s);
1961 bool AddOneof(const reffed_ptr<OneofDef>& o, Status* s);
1962
Josh Haberman78da6662016-01-13 19:05:43 -08001963 /* Set this to false to indicate that primitive fields should not have
1964 * explicit presence information associated with them. This will affect all
1965 * fields added to this message. Defaults to true. */
1966 void SetPrimitivesHavePresence(bool have_presence);
1967
Josh Habermane8ed0212015-06-08 17:56:03 -07001968 /* These return NULL if the field is not found. */
Chris Fallin91473dc2014-12-12 15:58:26 -08001969 FieldDef* FindFieldByNumber(uint32_t number);
1970 FieldDef* FindFieldByName(const char *name, size_t len);
1971 const FieldDef* FindFieldByNumber(uint32_t number) const;
1972 const FieldDef* FindFieldByName(const char* name, size_t len) const;
1973
1974
1975 FieldDef* FindFieldByName(const char *name) {
1976 return FindFieldByName(name, strlen(name));
1977 }
1978 const FieldDef* FindFieldByName(const char *name) const {
1979 return FindFieldByName(name, strlen(name));
1980 }
1981
1982 template <class T>
1983 FieldDef* FindFieldByName(const T& str) {
1984 return FindFieldByName(str.c_str(), str.size());
1985 }
1986 template <class T>
1987 const FieldDef* FindFieldByName(const T& str) const {
1988 return FindFieldByName(str.c_str(), str.size());
1989 }
1990
Chris Fallinfcd88892015-01-13 18:14:39 -08001991 OneofDef* FindOneofByName(const char* name, size_t len);
1992 const OneofDef* FindOneofByName(const char* name, size_t len) const;
1993
1994 OneofDef* FindOneofByName(const char* name) {
1995 return FindOneofByName(name, strlen(name));
1996 }
1997 const OneofDef* FindOneofByName(const char* name) const {
1998 return FindOneofByName(name, strlen(name));
1999 }
2000
2001 template<class T>
2002 OneofDef* FindOneofByName(const T& str) {
2003 return FindOneofByName(str.c_str(), str.size());
2004 }
2005 template<class T>
2006 const OneofDef* FindOneofByName(const T& str) const {
2007 return FindOneofByName(str.c_str(), str.size());
2008 }
2009
Josh Habermane8ed0212015-06-08 17:56:03 -07002010 /* Returns a new msgdef that is a copy of the given msgdef (and a copy of all
2011 * the fields) but with any references to submessages broken and replaced
2012 * with just the name of the submessage. Returns NULL if memory allocation
2013 * failed.
2014 *
2015 * TODO(haberman): which is more useful, keeping fields resolved or
2016 * unresolving them? If there's no obvious answer, Should this functionality
2017 * just be moved into symtab.c? */
Chris Fallin91473dc2014-12-12 15:58:26 -08002018 MessageDef* Dup(const void* owner) const;
2019
Josh Habermane8ed0212015-06-08 17:56:03 -07002020 /* Is this message a map entry? */
Chris Fallinfd1a3ff2015-01-06 15:44:09 -08002021 void setmapentry(bool map_entry);
2022 bool mapentry() const;
2023
Josh Habermane8ed0212015-06-08 17:56:03 -07002024 /* Iteration over fields. The order is undefined. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002025 class field_iterator
2026 : public std::iterator<std::forward_iterator_tag, FieldDef*> {
Chris Fallin91473dc2014-12-12 15:58:26 -08002027 public:
Chris Fallinfcd88892015-01-13 18:14:39 -08002028 explicit field_iterator(MessageDef* md);
2029 static field_iterator end(MessageDef* md);
Chris Fallin91473dc2014-12-12 15:58:26 -08002030
2031 void operator++();
2032 FieldDef* operator*() const;
Chris Fallinfcd88892015-01-13 18:14:39 -08002033 bool operator!=(const field_iterator& other) const;
2034 bool operator==(const field_iterator& other) const;
Chris Fallin91473dc2014-12-12 15:58:26 -08002035
2036 private:
Chris Fallinfcd88892015-01-13 18:14:39 -08002037 upb_msg_field_iter iter_;
Chris Fallin91473dc2014-12-12 15:58:26 -08002038 };
2039
Chris Fallinfcd88892015-01-13 18:14:39 -08002040 class const_field_iterator
Chris Fallin91473dc2014-12-12 15:58:26 -08002041 : public std::iterator<std::forward_iterator_tag, const FieldDef*> {
2042 public:
Chris Fallinfcd88892015-01-13 18:14:39 -08002043 explicit const_field_iterator(const MessageDef* md);
2044 static const_field_iterator end(const MessageDef* md);
Chris Fallin91473dc2014-12-12 15:58:26 -08002045
2046 void operator++();
2047 const FieldDef* operator*() const;
Chris Fallinfcd88892015-01-13 18:14:39 -08002048 bool operator!=(const const_field_iterator& other) const;
2049 bool operator==(const const_field_iterator& other) const;
Chris Fallin91473dc2014-12-12 15:58:26 -08002050
2051 private:
Chris Fallinfcd88892015-01-13 18:14:39 -08002052 upb_msg_field_iter iter_;
Chris Fallin91473dc2014-12-12 15:58:26 -08002053 };
2054
Josh Habermane8ed0212015-06-08 17:56:03 -07002055 /* Iteration over oneofs. The order is undefined. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002056 class oneof_iterator
2057 : public std::iterator<std::forward_iterator_tag, FieldDef*> {
2058 public:
2059 explicit oneof_iterator(MessageDef* md);
2060 static oneof_iterator end(MessageDef* md);
2061
2062 void operator++();
2063 OneofDef* operator*() const;
2064 bool operator!=(const oneof_iterator& other) const;
2065 bool operator==(const oneof_iterator& other) const;
2066
2067 private:
2068 upb_msg_oneof_iter iter_;
2069 };
2070
2071 class const_oneof_iterator
2072 : public std::iterator<std::forward_iterator_tag, const FieldDef*> {
2073 public:
2074 explicit const_oneof_iterator(const MessageDef* md);
2075 static const_oneof_iterator end(const MessageDef* md);
2076
2077 void operator++();
2078 const OneofDef* operator*() const;
2079 bool operator!=(const const_oneof_iterator& other) const;
2080 bool operator==(const const_oneof_iterator& other) const;
2081
2082 private:
2083 upb_msg_oneof_iter iter_;
2084 };
2085
2086 class FieldAccessor {
2087 public:
2088 explicit FieldAccessor(MessageDef* msg) : msg_(msg) {}
2089 field_iterator begin() { return msg_->field_begin(); }
2090 field_iterator end() { return msg_->field_end(); }
2091 private:
2092 MessageDef* msg_;
2093 };
2094
2095 class ConstFieldAccessor {
2096 public:
2097 explicit ConstFieldAccessor(const MessageDef* msg) : msg_(msg) {}
2098 const_field_iterator begin() { return msg_->field_begin(); }
2099 const_field_iterator end() { return msg_->field_end(); }
2100 private:
2101 const MessageDef* msg_;
2102 };
2103
2104 class OneofAccessor {
2105 public:
2106 explicit OneofAccessor(MessageDef* msg) : msg_(msg) {}
2107 oneof_iterator begin() { return msg_->oneof_begin(); }
2108 oneof_iterator end() { return msg_->oneof_end(); }
2109 private:
2110 MessageDef* msg_;
2111 };
2112
2113 class ConstOneofAccessor {
2114 public:
2115 explicit ConstOneofAccessor(const MessageDef* msg) : msg_(msg) {}
2116 const_oneof_iterator begin() { return msg_->oneof_begin(); }
2117 const_oneof_iterator end() { return msg_->oneof_end(); }
2118 private:
2119 const MessageDef* msg_;
2120 };
2121
2122 field_iterator field_begin();
2123 field_iterator field_end();
2124 const_field_iterator field_begin() const;
2125 const_field_iterator field_end() const;
2126
2127 oneof_iterator oneof_begin();
2128 oneof_iterator oneof_end();
2129 const_oneof_iterator oneof_begin() const;
2130 const_oneof_iterator oneof_end() const;
2131
2132 FieldAccessor fields() { return FieldAccessor(this); }
2133 ConstFieldAccessor fields() const { return ConstFieldAccessor(this); }
2134 OneofAccessor oneofs() { return OneofAccessor(this); }
2135 ConstOneofAccessor oneofs() const { return ConstOneofAccessor(this); }
Chris Fallin91473dc2014-12-12 15:58:26 -08002136
2137 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07002138 UPB_DISALLOW_POD_OPS(MessageDef, upb::MessageDef)
2139};
Chris Fallin91473dc2014-12-12 15:58:26 -08002140
Josh Habermane8ed0212015-06-08 17:56:03 -07002141#endif /* __cplusplus */
Chris Fallin91473dc2014-12-12 15:58:26 -08002142
Josh Habermane8ed0212015-06-08 17:56:03 -07002143UPB_BEGIN_EXTERN_C
Chris Fallinfcd88892015-01-13 18:14:39 -08002144
Josh Habermane8ed0212015-06-08 17:56:03 -07002145/* Returns NULL if memory allocation failed. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002146upb_msgdef *upb_msgdef_new(const void *owner);
2147
Josh Habermane8ed0212015-06-08 17:56:03 -07002148/* Include upb_refcounted methods like upb_msgdef_ref(). */
2149UPB_REFCOUNTED_CMETHODS(upb_msgdef, upb_msgdef_upcast2)
2150
Chris Fallin91473dc2014-12-12 15:58:26 -08002151bool upb_msgdef_freeze(upb_msgdef *m, upb_status *status);
2152
Chris Fallin91473dc2014-12-12 15:58:26 -08002153const char *upb_msgdef_fullname(const upb_msgdef *m);
Josh Haberman94e54b32016-04-14 12:06:09 -07002154const char *upb_msgdef_name(const upb_msgdef *m);
Chris Fallin91473dc2014-12-12 15:58:26 -08002155bool upb_msgdef_setfullname(upb_msgdef *m, const char *fullname, upb_status *s);
2156
2157upb_msgdef *upb_msgdef_dup(const upb_msgdef *m, const void *owner);
2158bool upb_msgdef_addfield(upb_msgdef *m, upb_fielddef *f, const void *ref_donor,
2159 upb_status *s);
Chris Fallinfcd88892015-01-13 18:14:39 -08002160bool upb_msgdef_addoneof(upb_msgdef *m, upb_oneofdef *o, const void *ref_donor,
2161 upb_status *s);
Chris Fallin91473dc2014-12-12 15:58:26 -08002162
Josh Habermane8ed0212015-06-08 17:56:03 -07002163/* Field lookup in a couple of different variations:
2164 * - itof = int to field
2165 * - ntof = name to field
2166 * - ntofz = name to field, null-terminated string. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002167const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i);
2168const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name,
2169 size_t len);
2170int upb_msgdef_numfields(const upb_msgdef *m);
2171
2172UPB_INLINE const upb_fielddef *upb_msgdef_ntofz(const upb_msgdef *m,
2173 const char *name) {
2174 return upb_msgdef_ntof(m, name, strlen(name));
2175}
2176
2177UPB_INLINE upb_fielddef *upb_msgdef_itof_mutable(upb_msgdef *m, uint32_t i) {
2178 return (upb_fielddef*)upb_msgdef_itof(m, i);
2179}
2180
2181UPB_INLINE upb_fielddef *upb_msgdef_ntof_mutable(upb_msgdef *m,
2182 const char *name, size_t len) {
2183 return (upb_fielddef *)upb_msgdef_ntof(m, name, len);
2184}
2185
Josh Habermane8ed0212015-06-08 17:56:03 -07002186/* Oneof lookup:
2187 * - ntoo = name to oneof
2188 * - ntooz = name to oneof, null-terminated string. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002189const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name,
2190 size_t len);
2191int upb_msgdef_numoneofs(const upb_msgdef *m);
2192
2193UPB_INLINE const upb_oneofdef *upb_msgdef_ntooz(const upb_msgdef *m,
2194 const char *name) {
2195 return upb_msgdef_ntoo(m, name, strlen(name));
2196}
2197
2198UPB_INLINE upb_oneofdef *upb_msgdef_ntoo_mutable(upb_msgdef *m,
2199 const char *name, size_t len) {
2200 return (upb_oneofdef *)upb_msgdef_ntoo(m, name, len);
2201}
2202
Chris Fallinfd1a3ff2015-01-06 15:44:09 -08002203void upb_msgdef_setmapentry(upb_msgdef *m, bool map_entry);
2204bool upb_msgdef_mapentry(const upb_msgdef *m);
2205
Josh Habermane8ed0212015-06-08 17:56:03 -07002206/* Well-known field tag numbers for map-entry messages. */
Chris Fallina5075922015-02-02 15:07:34 -08002207#define UPB_MAPENTRY_KEY 1
2208#define UPB_MAPENTRY_VALUE 2
2209
Chris Fallinfcd88892015-01-13 18:14:39 -08002210const upb_oneofdef *upb_msgdef_findoneof(const upb_msgdef *m,
2211 const char *name);
2212int upb_msgdef_numoneofs(const upb_msgdef *m);
2213
Josh Habermane8ed0212015-06-08 17:56:03 -07002214/* upb_msg_field_iter i;
2215 * for(upb_msg_field_begin(&i, m);
2216 * !upb_msg_field_done(&i);
2217 * upb_msg_field_next(&i)) {
2218 * upb_fielddef *f = upb_msg_iter_field(&i);
2219 * // ...
2220 * }
2221 *
2222 * For C we don't have separate iterators for const and non-const.
2223 * It is the caller's responsibility to cast the upb_fielddef* to
2224 * const if the upb_msgdef* is const. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002225void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m);
2226void upb_msg_field_next(upb_msg_field_iter *iter);
2227bool upb_msg_field_done(const upb_msg_field_iter *iter);
2228upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter);
2229void upb_msg_field_iter_setdone(upb_msg_field_iter *iter);
2230
Josh Habermane8ed0212015-06-08 17:56:03 -07002231/* Similar to above, we also support iterating through the oneofs in a
2232 * msgdef. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002233void upb_msg_oneof_begin(upb_msg_oneof_iter *iter, const upb_msgdef *m);
2234void upb_msg_oneof_next(upb_msg_oneof_iter *iter);
2235bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter);
2236upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter);
2237void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter);
Chris Fallin91473dc2014-12-12 15:58:26 -08002238
Josh Habermane8ed0212015-06-08 17:56:03 -07002239UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08002240
2241
2242/* upb::EnumDef ***************************************************************/
2243
2244typedef upb_strtable_iter upb_enum_iter;
2245
Josh Habermane8ed0212015-06-08 17:56:03 -07002246#ifdef __cplusplus
2247
2248/* Class that represents an enum. Its base class is upb::Def (convert with
2249 * upb::upcast()). */
2250class upb::EnumDef {
Chris Fallin91473dc2014-12-12 15:58:26 -08002251 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07002252 /* Returns NULL if memory allocation failed. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002253 static reffed_ptr<EnumDef> New();
2254
Josh Habermane8ed0212015-06-08 17:56:03 -07002255 /* upb::RefCounted methods like Ref()/Unref(). */
2256 UPB_REFCOUNTED_CPPMETHODS
Chris Fallin91473dc2014-12-12 15:58:26 -08002257
Josh Habermane8ed0212015-06-08 17:56:03 -07002258 /* Functionality from upb::Def. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002259 const char* full_name() const;
Josh Haberman94e54b32016-04-14 12:06:09 -07002260 const char* name() const;
Chris Fallin91473dc2014-12-12 15:58:26 -08002261 bool set_full_name(const char* fullname, Status* s);
2262 bool set_full_name(const std::string& fullname, Status* s);
2263
Josh Habermane8ed0212015-06-08 17:56:03 -07002264 /* Call to freeze this EnumDef. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002265 bool Freeze(Status* s);
2266
Josh Habermane8ed0212015-06-08 17:56:03 -07002267 /* The value that is used as the default when no field default is specified.
2268 * If not set explicitly, the first value that was added will be used.
2269 * The default value must be a member of the enum.
2270 * Requires that value_count() > 0. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002271 int32_t default_value() const;
2272
Josh Habermane8ed0212015-06-08 17:56:03 -07002273 /* Sets the default value. If this value is not valid, returns false and an
2274 * error message in status. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002275 bool set_default_value(int32_t val, Status* status);
2276
Josh Habermane8ed0212015-06-08 17:56:03 -07002277 /* Returns the number of values currently defined in the enum. Note that
2278 * multiple names can refer to the same number, so this may be greater than
2279 * the total number of unique numbers. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002280 int value_count() const;
2281
Josh Habermane8ed0212015-06-08 17:56:03 -07002282 /* Adds a single name/number pair to the enum. Fails if this name has
2283 * already been used by another value. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002284 bool AddValue(const char* name, int32_t num, Status* status);
2285 bool AddValue(const std::string& name, int32_t num, Status* status);
2286
Josh Habermane8ed0212015-06-08 17:56:03 -07002287 /* Lookups from name to integer, returning true if found. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002288 bool FindValueByName(const char* name, int32_t* num) const;
2289
Josh Habermane8ed0212015-06-08 17:56:03 -07002290 /* Finds the name corresponding to the given number, or NULL if none was
2291 * found. If more than one name corresponds to this number, returns the
2292 * first one that was added. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002293 const char* FindValueByNumber(int32_t num) const;
2294
Josh Habermane8ed0212015-06-08 17:56:03 -07002295 /* Returns a new EnumDef with all the same values. The new EnumDef will be
2296 * owned by the given owner. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002297 EnumDef* Dup(const void* owner) const;
2298
Josh Habermane8ed0212015-06-08 17:56:03 -07002299 /* Iteration over name/value pairs. The order is undefined.
2300 * Adding an enum val invalidates any iterators.
2301 *
2302 * TODO: make compatible with range-for, with elements as pairs? */
Chris Fallin91473dc2014-12-12 15:58:26 -08002303 class Iterator {
2304 public:
2305 explicit Iterator(const EnumDef*);
2306
2307 int32_t number();
2308 const char *name();
2309 bool Done();
2310 void Next();
2311
2312 private:
2313 upb_enum_iter iter_;
2314 };
2315
2316 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07002317 UPB_DISALLOW_POD_OPS(EnumDef, upb::EnumDef)
2318};
Chris Fallin91473dc2014-12-12 15:58:26 -08002319
Josh Habermane8ed0212015-06-08 17:56:03 -07002320#endif /* __cplusplus */
Chris Fallin91473dc2014-12-12 15:58:26 -08002321
Josh Habermane8ed0212015-06-08 17:56:03 -07002322UPB_BEGIN_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08002323
Josh Habermane8ed0212015-06-08 17:56:03 -07002324/* Native C API. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002325upb_enumdef *upb_enumdef_new(const void *owner);
2326upb_enumdef *upb_enumdef_dup(const upb_enumdef *e, const void *owner);
2327
Josh Habermane8ed0212015-06-08 17:56:03 -07002328/* Include upb_refcounted methods like upb_enumdef_ref(). */
2329UPB_REFCOUNTED_CMETHODS(upb_enumdef, upb_enumdef_upcast2)
2330
Chris Fallin91473dc2014-12-12 15:58:26 -08002331bool upb_enumdef_freeze(upb_enumdef *e, upb_status *status);
2332
Josh Habermane8ed0212015-06-08 17:56:03 -07002333/* From upb_def. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002334const char *upb_enumdef_fullname(const upb_enumdef *e);
Josh Haberman94e54b32016-04-14 12:06:09 -07002335const char *upb_enumdef_name(const upb_enumdef *e);
Chris Fallin91473dc2014-12-12 15:58:26 -08002336bool upb_enumdef_setfullname(upb_enumdef *e, const char *fullname,
2337 upb_status *s);
2338
2339int32_t upb_enumdef_default(const upb_enumdef *e);
2340bool upb_enumdef_setdefault(upb_enumdef *e, int32_t val, upb_status *s);
2341int upb_enumdef_numvals(const upb_enumdef *e);
2342bool upb_enumdef_addval(upb_enumdef *e, const char *name, int32_t num,
2343 upb_status *status);
2344
Josh Habermane8ed0212015-06-08 17:56:03 -07002345/* Enum lookups:
2346 * - ntoi: look up a name with specified length.
2347 * - ntoiz: look up a name provided as a null-terminated string.
2348 * - iton: look up an integer, returning the name as a null-terminated
2349 * string. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002350bool upb_enumdef_ntoi(const upb_enumdef *e, const char *name, size_t len,
2351 int32_t *num);
2352UPB_INLINE bool upb_enumdef_ntoiz(const upb_enumdef *e,
2353 const char *name, int32_t *num) {
2354 return upb_enumdef_ntoi(e, name, strlen(name), num);
2355}
2356const char *upb_enumdef_iton(const upb_enumdef *e, int32_t num);
2357
Josh Habermane8ed0212015-06-08 17:56:03 -07002358/* upb_enum_iter i;
2359 * for(upb_enum_begin(&i, e); !upb_enum_done(&i); upb_enum_next(&i)) {
2360 * // ...
2361 * }
2362 */
Chris Fallin91473dc2014-12-12 15:58:26 -08002363void upb_enum_begin(upb_enum_iter *iter, const upb_enumdef *e);
2364void upb_enum_next(upb_enum_iter *iter);
2365bool upb_enum_done(upb_enum_iter *iter);
2366const char *upb_enum_iter_name(upb_enum_iter *iter);
2367int32_t upb_enum_iter_number(upb_enum_iter *iter);
2368
Josh Habermane8ed0212015-06-08 17:56:03 -07002369UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08002370
Chris Fallinfcd88892015-01-13 18:14:39 -08002371/* upb::OneofDef **************************************************************/
2372
2373typedef upb_inttable_iter upb_oneof_iter;
2374
Josh Habermane8ed0212015-06-08 17:56:03 -07002375#ifdef __cplusplus
2376
Josh Haberman94e54b32016-04-14 12:06:09 -07002377/* Class that represents a oneof. */
Josh Habermane8ed0212015-06-08 17:56:03 -07002378class upb::OneofDef {
Chris Fallinfcd88892015-01-13 18:14:39 -08002379 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07002380 /* Returns NULL if memory allocation failed. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002381 static reffed_ptr<OneofDef> New();
2382
Josh Habermane8ed0212015-06-08 17:56:03 -07002383 /* upb::RefCounted methods like Ref()/Unref(). */
2384 UPB_REFCOUNTED_CPPMETHODS
Chris Fallinfcd88892015-01-13 18:14:39 -08002385
Josh Habermane8ed0212015-06-08 17:56:03 -07002386 /* Returns the MessageDef that owns this OneofDef. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002387 const MessageDef* containing_type() const;
2388
Josh Habermane8ed0212015-06-08 17:56:03 -07002389 /* Returns the name of this oneof. This is the name used to look up the oneof
2390 * by name once added to a message def. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002391 const char* name() const;
2392 bool set_name(const char* name, Status* s);
Josh Haberman94e54b32016-04-14 12:06:09 -07002393 bool set_name(const std::string& name, Status* s);
Chris Fallinfcd88892015-01-13 18:14:39 -08002394
Josh Habermane8ed0212015-06-08 17:56:03 -07002395 /* Returns the number of fields currently defined in the oneof. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002396 int field_count() const;
2397
Josh Habermane8ed0212015-06-08 17:56:03 -07002398 /* Adds a field to the oneof. The field must not have been added to any other
2399 * oneof or msgdef. If the oneof is not yet part of a msgdef, then when the
2400 * oneof is eventually added to a msgdef, all fields added to the oneof will
2401 * also be added to the msgdef at that time. If the oneof is already part of a
2402 * msgdef, the field must either be a part of that msgdef already, or must not
2403 * be a part of any msgdef; in the latter case, the field is added to the
2404 * msgdef as a part of this operation.
2405 *
2406 * The field may only have an OPTIONAL label, never REQUIRED or REPEATED.
2407 *
2408 * If |f| is already part of this MessageDef, this method performs no action
2409 * and returns true (success). Thus, this method is idempotent. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002410 bool AddField(FieldDef* field, Status* s);
2411 bool AddField(const reffed_ptr<FieldDef>& field, Status* s);
2412
Josh Habermane8ed0212015-06-08 17:56:03 -07002413 /* Looks up by name. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002414 const FieldDef* FindFieldByName(const char* name, size_t len) const;
2415 FieldDef* FindFieldByName(const char* name, size_t len);
2416 const FieldDef* FindFieldByName(const char* name) const {
2417 return FindFieldByName(name, strlen(name));
2418 }
2419 FieldDef* FindFieldByName(const char* name) {
2420 return FindFieldByName(name, strlen(name));
2421 }
2422
2423 template <class T>
2424 FieldDef* FindFieldByName(const T& str) {
2425 return FindFieldByName(str.c_str(), str.size());
2426 }
2427 template <class T>
2428 const FieldDef* FindFieldByName(const T& str) const {
2429 return FindFieldByName(str.c_str(), str.size());
2430 }
2431
Josh Habermane8ed0212015-06-08 17:56:03 -07002432 /* Looks up by tag number. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002433 const FieldDef* FindFieldByNumber(uint32_t num) const;
2434
Josh Habermane8ed0212015-06-08 17:56:03 -07002435 /* Returns a new OneofDef with all the same fields. The OneofDef will be owned
2436 * by the given owner. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002437 OneofDef* Dup(const void* owner) const;
2438
Josh Habermane8ed0212015-06-08 17:56:03 -07002439 /* Iteration over fields. The order is undefined. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002440 class iterator : public std::iterator<std::forward_iterator_tag, FieldDef*> {
2441 public:
2442 explicit iterator(OneofDef* md);
2443 static iterator end(OneofDef* md);
2444
2445 void operator++();
2446 FieldDef* operator*() const;
2447 bool operator!=(const iterator& other) const;
2448 bool operator==(const iterator& other) const;
2449
2450 private:
2451 upb_oneof_iter iter_;
2452 };
2453
2454 class const_iterator
2455 : public std::iterator<std::forward_iterator_tag, const FieldDef*> {
2456 public:
2457 explicit const_iterator(const OneofDef* md);
2458 static const_iterator end(const OneofDef* md);
2459
2460 void operator++();
2461 const FieldDef* operator*() const;
2462 bool operator!=(const const_iterator& other) const;
2463 bool operator==(const const_iterator& other) const;
2464
2465 private:
2466 upb_oneof_iter iter_;
2467 };
2468
2469 iterator begin();
2470 iterator end();
2471 const_iterator begin() const;
2472 const_iterator end() const;
2473
2474 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07002475 UPB_DISALLOW_POD_OPS(OneofDef, upb::OneofDef)
2476};
Chris Fallinfcd88892015-01-13 18:14:39 -08002477
Josh Habermane8ed0212015-06-08 17:56:03 -07002478#endif /* __cplusplus */
Chris Fallinfcd88892015-01-13 18:14:39 -08002479
Josh Habermane8ed0212015-06-08 17:56:03 -07002480UPB_BEGIN_EXTERN_C
Chris Fallinfcd88892015-01-13 18:14:39 -08002481
Josh Habermane8ed0212015-06-08 17:56:03 -07002482/* Native C API. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002483upb_oneofdef *upb_oneofdef_new(const void *owner);
2484upb_oneofdef *upb_oneofdef_dup(const upb_oneofdef *o, const void *owner);
2485
Josh Habermane8ed0212015-06-08 17:56:03 -07002486/* Include upb_refcounted methods like upb_oneofdef_ref(). */
Josh Haberman94e54b32016-04-14 12:06:09 -07002487UPB_REFCOUNTED_CMETHODS(upb_oneofdef, upb_oneofdef_upcast)
Chris Fallinfcd88892015-01-13 18:14:39 -08002488
2489const char *upb_oneofdef_name(const upb_oneofdef *o);
2490bool upb_oneofdef_setname(upb_oneofdef *o, const char *name, upb_status *s);
2491
2492const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o);
2493int upb_oneofdef_numfields(const upb_oneofdef *o);
2494bool upb_oneofdef_addfield(upb_oneofdef *o, upb_fielddef *f,
2495 const void *ref_donor,
2496 upb_status *s);
2497
Josh Habermane8ed0212015-06-08 17:56:03 -07002498/* Oneof lookups:
2499 * - ntof: look up a field by name.
2500 * - ntofz: look up a field by name (as a null-terminated string).
2501 * - itof: look up a field by number. */
Chris Fallinfcd88892015-01-13 18:14:39 -08002502const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o,
2503 const char *name, size_t length);
2504UPB_INLINE const upb_fielddef *upb_oneofdef_ntofz(const upb_oneofdef *o,
2505 const char *name) {
2506 return upb_oneofdef_ntof(o, name, strlen(name));
2507}
2508const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num);
2509
Josh Habermane8ed0212015-06-08 17:56:03 -07002510/* upb_oneof_iter i;
2511 * for(upb_oneof_begin(&i, e); !upb_oneof_done(&i); upb_oneof_next(&i)) {
2512 * // ...
2513 * }
2514 */
Chris Fallinfcd88892015-01-13 18:14:39 -08002515void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o);
2516void upb_oneof_next(upb_oneof_iter *iter);
2517bool upb_oneof_done(upb_oneof_iter *iter);
2518upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter);
2519void upb_oneof_iter_setdone(upb_oneof_iter *iter);
2520
Josh Habermane8ed0212015-06-08 17:56:03 -07002521UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08002522
Josh Haberman94e54b32016-04-14 12:06:09 -07002523
2524/* upb::FileDef ***************************************************************/
2525
2526typedef enum {
2527 UPB_SYNTAX_PROTO2 = 2,
2528 UPB_SYNTAX_PROTO3 = 3
2529} upb_syntax_t;
2530
2531#ifdef __cplusplus
2532
2533/* Class that represents a .proto file with some things defined in it.
2534 *
2535 * Many users won't care about FileDefs, but they are necessary if you want to
2536 * read the values of file-level options. */
2537class upb::FileDef {
2538 public:
2539 /* Returns NULL if memory allocation failed. */
2540 static reffed_ptr<FileDef> New();
2541
2542 /* upb::RefCounted methods like Ref()/Unref(). */
2543 UPB_REFCOUNTED_CPPMETHODS
2544
2545 /* Get/set name of the file (eg. "foo/bar.proto"). */
2546 const char* name() const;
2547 bool set_name(const char* name, Status* s);
2548 bool set_name(const std::string& name, Status* s);
2549
2550 /* Package name for definitions inside the file (eg. "foo.bar"). */
2551 const char* package() const;
2552 bool set_package(const char* package, Status* s);
2553
2554 /* Syntax for the file. Defaults to proto2. */
2555 upb_syntax_t syntax() const;
2556 void set_syntax(upb_syntax_t syntax);
2557
2558 /* Get the list of defs from the file. These are returned in the order that
2559 * they were added to the FileDef. */
2560 int def_count() const;
2561 const Def* def(int index) const;
2562 Def* def(int index);
2563
2564 /* Get the list of dependencies from the file. These are returned in the
2565 * order that they were added to the FileDef. */
2566 int dependency_count() const;
2567 const FileDef* dependency(int index) const;
2568
2569 /* Adds defs to this file. The def must not already belong to another
2570 * file.
2571 *
2572 * Note: this does *not* ensure that this def's name is unique in this file!
2573 * Use a SymbolTable if you want to check this property. Especially since
2574 * properly checking uniqueness would require a check across *all* files
2575 * (including dependencies). */
2576 bool AddDef(Def* def, Status* s);
2577 bool AddMessage(MessageDef* m, Status* s);
2578 bool AddEnum(EnumDef* e, Status* s);
2579 bool AddExtension(FieldDef* f, Status* s);
2580
2581 /* Adds a dependency of this file. */
2582 bool AddDependency(const FileDef* file);
2583
2584 /* Freezes this FileDef and all messages/enums under it. All subdefs must be
2585 * resolved and all messages/enums must validate. Returns true if this
2586 * succeeded.
2587 *
2588 * TODO(haberman): should we care whether the file's dependencies are frozen
2589 * already? */
2590 bool Freeze(Status* s);
2591
2592 private:
2593 UPB_DISALLOW_POD_OPS(FileDef, upb::FileDef)
2594};
2595
2596#endif
2597
2598UPB_BEGIN_EXTERN_C
2599
2600upb_filedef *upb_filedef_new(const void *owner);
2601
2602/* Include upb_refcounted methods like upb_msgdef_ref(). */
2603UPB_REFCOUNTED_CMETHODS(upb_filedef, upb_filedef_upcast)
2604
2605const char *upb_filedef_name(const upb_filedef *f);
2606const char *upb_filedef_package(const upb_filedef *f);
2607upb_syntax_t upb_filedef_syntax(const upb_filedef *f);
2608size_t upb_filedef_defcount(const upb_filedef *f);
2609size_t upb_filedef_depcount(const upb_filedef *f);
2610const upb_def *upb_filedef_def(const upb_filedef *f, size_t i);
2611const upb_filedef *upb_filedef_dep(const upb_filedef *f, size_t i);
2612
2613bool upb_filedef_freeze(upb_filedef *f, upb_status *s);
2614bool upb_filedef_setname(upb_filedef *f, const char *name, upb_status *s);
2615bool upb_filedef_setpackage(upb_filedef *f, const char *package, upb_status *s);
2616bool upb_filedef_setsyntax(upb_filedef *f, upb_syntax_t syntax, upb_status *s);
2617
2618bool upb_filedef_adddef(upb_filedef *f, upb_def *def, const void *ref_donor,
2619 upb_status *s);
2620bool upb_filedef_adddep(upb_filedef *f, const upb_filedef *dep);
2621
2622UPB_INLINE bool upb_filedef_addmsg(upb_filedef *f, upb_msgdef *m,
2623 const void *ref_donor, upb_status *s) {
2624 return upb_filedef_adddef(f, upb_msgdef_upcast_mutable(m), ref_donor, s);
2625}
2626
2627UPB_INLINE bool upb_filedef_addenum(upb_filedef *f, upb_enumdef *e,
2628 const void *ref_donor, upb_status *s) {
2629 return upb_filedef_adddef(f, upb_enumdef_upcast_mutable(e), ref_donor, s);
2630}
2631
2632UPB_INLINE bool upb_filedef_addext(upb_filedef *file, upb_fielddef *f,
2633 const void *ref_donor, upb_status *s) {
2634 return upb_filedef_adddef(file, upb_fielddef_upcast_mutable(f), ref_donor, s);
2635}
2636UPB_INLINE upb_def *upb_filedef_mutabledef(upb_filedef *f, int i) {
2637 return (upb_def*)upb_filedef_def(f, i);
2638}
2639
2640UPB_END_EXTERN_C
2641
Chris Fallin91473dc2014-12-12 15:58:26 -08002642#ifdef __cplusplus
2643
2644UPB_INLINE const char* upb_safecstr(const std::string& str) {
2645 assert(str.size() == std::strlen(str.c_str()));
2646 return str.c_str();
2647}
2648
Josh Habermane8ed0212015-06-08 17:56:03 -07002649/* Inline C++ wrappers. */
Chris Fallin91473dc2014-12-12 15:58:26 -08002650namespace upb {
2651
2652inline Def* Def::Dup(const void* owner) const {
2653 return upb_def_dup(this, owner);
2654}
Chris Fallin91473dc2014-12-12 15:58:26 -08002655inline Def::Type Def::def_type() const { return upb_def_type(this); }
2656inline const char* Def::full_name() const { return upb_def_fullname(this); }
Josh Haberman94e54b32016-04-14 12:06:09 -07002657inline const char* Def::name() const { return upb_def_name(this); }
Chris Fallin91473dc2014-12-12 15:58:26 -08002658inline bool Def::set_full_name(const char* fullname, Status* s) {
2659 return upb_def_setfullname(this, fullname, s);
2660}
2661inline bool Def::set_full_name(const std::string& fullname, Status* s) {
2662 return upb_def_setfullname(this, upb_safecstr(fullname), s);
2663}
Josh Haberman94e54b32016-04-14 12:06:09 -07002664inline bool Def::Freeze(Def* const* defs, size_t n, Status* status) {
Chris Fallin91473dc2014-12-12 15:58:26 -08002665 return upb_def_freeze(defs, n, status);
2666}
2667inline bool Def::Freeze(const std::vector<Def*>& defs, Status* status) {
2668 return upb_def_freeze((Def* const*)&defs[0], defs.size(), status);
2669}
2670
2671inline bool FieldDef::CheckType(int32_t val) {
2672 return upb_fielddef_checktype(val);
2673}
2674inline bool FieldDef::CheckLabel(int32_t val) {
2675 return upb_fielddef_checklabel(val);
2676}
2677inline bool FieldDef::CheckDescriptorType(int32_t val) {
2678 return upb_fielddef_checkdescriptortype(val);
2679}
2680inline bool FieldDef::CheckIntegerFormat(int32_t val) {
2681 return upb_fielddef_checkintfmt(val);
2682}
2683inline FieldDef::Type FieldDef::ConvertType(int32_t val) {
2684 assert(CheckType(val));
2685 return static_cast<FieldDef::Type>(val);
2686}
2687inline FieldDef::Label FieldDef::ConvertLabel(int32_t val) {
2688 assert(CheckLabel(val));
2689 return static_cast<FieldDef::Label>(val);
2690}
2691inline FieldDef::DescriptorType FieldDef::ConvertDescriptorType(int32_t val) {
2692 assert(CheckDescriptorType(val));
2693 return static_cast<FieldDef::DescriptorType>(val);
2694}
2695inline FieldDef::IntegerFormat FieldDef::ConvertIntegerFormat(int32_t val) {
2696 assert(CheckIntegerFormat(val));
2697 return static_cast<FieldDef::IntegerFormat>(val);
2698}
2699
2700inline reffed_ptr<FieldDef> FieldDef::New() {
2701 upb_fielddef *f = upb_fielddef_new(&f);
2702 return reffed_ptr<FieldDef>(f, &f);
2703}
2704inline FieldDef* FieldDef::Dup(const void* owner) const {
2705 return upb_fielddef_dup(this, owner);
2706}
Chris Fallin91473dc2014-12-12 15:58:26 -08002707inline const char* FieldDef::full_name() const {
2708 return upb_fielddef_fullname(this);
2709}
2710inline bool FieldDef::set_full_name(const char* fullname, Status* s) {
2711 return upb_fielddef_setfullname(this, fullname, s);
2712}
2713inline bool FieldDef::set_full_name(const std::string& fullname, Status* s) {
2714 return upb_fielddef_setfullname(this, upb_safecstr(fullname), s);
2715}
2716inline bool FieldDef::type_is_set() const {
2717 return upb_fielddef_typeisset(this);
2718}
2719inline FieldDef::Type FieldDef::type() const { return upb_fielddef_type(this); }
2720inline FieldDef::DescriptorType FieldDef::descriptor_type() const {
2721 return upb_fielddef_descriptortype(this);
2722}
2723inline FieldDef::Label FieldDef::label() const {
2724 return upb_fielddef_label(this);
2725}
2726inline uint32_t FieldDef::number() const { return upb_fielddef_number(this); }
2727inline const char* FieldDef::name() const { return upb_fielddef_name(this); }
2728inline bool FieldDef::is_extension() const {
2729 return upb_fielddef_isextension(this);
2730}
Josh Habermanf654d492016-02-18 11:07:51 -08002731inline size_t FieldDef::GetJsonName(char* buf, size_t len) const {
2732 return upb_fielddef_getjsonname(this, buf, len);
2733}
Chris Fallin91473dc2014-12-12 15:58:26 -08002734inline bool FieldDef::lazy() const {
2735 return upb_fielddef_lazy(this);
2736}
2737inline void FieldDef::set_lazy(bool lazy) {
2738 upb_fielddef_setlazy(this, lazy);
2739}
2740inline bool FieldDef::packed() const {
2741 return upb_fielddef_packed(this);
2742}
Josh Haberman94e54b32016-04-14 12:06:09 -07002743inline uint32_t FieldDef::index() const {
2744 return upb_fielddef_index(this);
2745}
Chris Fallin91473dc2014-12-12 15:58:26 -08002746inline void FieldDef::set_packed(bool packed) {
2747 upb_fielddef_setpacked(this, packed);
2748}
2749inline const MessageDef* FieldDef::containing_type() const {
2750 return upb_fielddef_containingtype(this);
2751}
Chris Fallinfcd88892015-01-13 18:14:39 -08002752inline const OneofDef* FieldDef::containing_oneof() const {
2753 return upb_fielddef_containingoneof(this);
2754}
Chris Fallin91473dc2014-12-12 15:58:26 -08002755inline const char* FieldDef::containing_type_name() {
2756 return upb_fielddef_containingtypename(this);
2757}
2758inline bool FieldDef::set_number(uint32_t number, Status* s) {
2759 return upb_fielddef_setnumber(this, number, s);
2760}
2761inline bool FieldDef::set_name(const char *name, Status* s) {
2762 return upb_fielddef_setname(this, name, s);
2763}
2764inline bool FieldDef::set_name(const std::string& name, Status* s) {
2765 return upb_fielddef_setname(this, upb_safecstr(name), s);
2766}
Josh Haberman78da6662016-01-13 19:05:43 -08002767inline bool FieldDef::set_json_name(const char *name, Status* s) {
2768 return upb_fielddef_setjsonname(this, name, s);
2769}
2770inline bool FieldDef::set_json_name(const std::string& name, Status* s) {
2771 return upb_fielddef_setjsonname(this, upb_safecstr(name), s);
2772}
2773inline void FieldDef::clear_json_name() {
2774 upb_fielddef_clearjsonname(this);
2775}
Chris Fallin91473dc2014-12-12 15:58:26 -08002776inline bool FieldDef::set_containing_type_name(const char *name, Status* s) {
2777 return upb_fielddef_setcontainingtypename(this, name, s);
2778}
2779inline bool FieldDef::set_containing_type_name(const std::string &name,
2780 Status *s) {
2781 return upb_fielddef_setcontainingtypename(this, upb_safecstr(name), s);
2782}
2783inline void FieldDef::set_type(upb_fieldtype_t type) {
2784 upb_fielddef_settype(this, type);
2785}
2786inline void FieldDef::set_is_extension(bool is_extension) {
2787 upb_fielddef_setisextension(this, is_extension);
2788}
2789inline void FieldDef::set_descriptor_type(FieldDef::DescriptorType type) {
2790 upb_fielddef_setdescriptortype(this, type);
2791}
2792inline void FieldDef::set_label(upb_label_t label) {
2793 upb_fielddef_setlabel(this, label);
2794}
2795inline bool FieldDef::IsSubMessage() const {
2796 return upb_fielddef_issubmsg(this);
2797}
2798inline bool FieldDef::IsString() const { return upb_fielddef_isstring(this); }
2799inline bool FieldDef::IsSequence() const { return upb_fielddef_isseq(this); }
Chris Fallina5075922015-02-02 15:07:34 -08002800inline bool FieldDef::IsMap() const { return upb_fielddef_ismap(this); }
Chris Fallin91473dc2014-12-12 15:58:26 -08002801inline int64_t FieldDef::default_int64() const {
2802 return upb_fielddef_defaultint64(this);
2803}
2804inline int32_t FieldDef::default_int32() const {
2805 return upb_fielddef_defaultint32(this);
2806}
2807inline uint64_t FieldDef::default_uint64() const {
2808 return upb_fielddef_defaultuint64(this);
2809}
2810inline uint32_t FieldDef::default_uint32() const {
2811 return upb_fielddef_defaultuint32(this);
2812}
2813inline bool FieldDef::default_bool() const {
2814 return upb_fielddef_defaultbool(this);
2815}
2816inline float FieldDef::default_float() const {
2817 return upb_fielddef_defaultfloat(this);
2818}
2819inline double FieldDef::default_double() const {
2820 return upb_fielddef_defaultdouble(this);
2821}
2822inline const char* FieldDef::default_string(size_t* len) const {
2823 return upb_fielddef_defaultstr(this, len);
2824}
2825inline void FieldDef::set_default_int64(int64_t value) {
2826 upb_fielddef_setdefaultint64(this, value);
2827}
2828inline void FieldDef::set_default_int32(int32_t value) {
2829 upb_fielddef_setdefaultint32(this, value);
2830}
2831inline void FieldDef::set_default_uint64(uint64_t value) {
2832 upb_fielddef_setdefaultuint64(this, value);
2833}
2834inline void FieldDef::set_default_uint32(uint32_t value) {
2835 upb_fielddef_setdefaultuint32(this, value);
2836}
2837inline void FieldDef::set_default_bool(bool value) {
2838 upb_fielddef_setdefaultbool(this, value);
2839}
2840inline void FieldDef::set_default_float(float value) {
2841 upb_fielddef_setdefaultfloat(this, value);
2842}
2843inline void FieldDef::set_default_double(double value) {
2844 upb_fielddef_setdefaultdouble(this, value);
2845}
2846inline bool FieldDef::set_default_string(const void *str, size_t len,
2847 Status *s) {
2848 return upb_fielddef_setdefaultstr(this, str, len, s);
2849}
2850inline bool FieldDef::set_default_string(const std::string& str, Status* s) {
2851 return upb_fielddef_setdefaultstr(this, str.c_str(), str.size(), s);
2852}
2853inline void FieldDef::set_default_cstr(const char* str, Status* s) {
2854 return upb_fielddef_setdefaultcstr(this, str, s);
2855}
2856inline bool FieldDef::HasSubDef() const { return upb_fielddef_hassubdef(this); }
2857inline const Def* FieldDef::subdef() const { return upb_fielddef_subdef(this); }
2858inline const MessageDef *FieldDef::message_subdef() const {
2859 return upb_fielddef_msgsubdef(this);
2860}
2861inline const EnumDef *FieldDef::enum_subdef() const {
2862 return upb_fielddef_enumsubdef(this);
2863}
2864inline const char* FieldDef::subdef_name() const {
2865 return upb_fielddef_subdefname(this);
2866}
2867inline bool FieldDef::set_subdef(const Def* subdef, Status* s) {
2868 return upb_fielddef_setsubdef(this, subdef, s);
2869}
2870inline bool FieldDef::set_enum_subdef(const EnumDef* subdef, Status* s) {
2871 return upb_fielddef_setenumsubdef(this, subdef, s);
2872}
2873inline bool FieldDef::set_message_subdef(const MessageDef* subdef, Status* s) {
2874 return upb_fielddef_setmsgsubdef(this, subdef, s);
2875}
2876inline bool FieldDef::set_subdef_name(const char* name, Status* s) {
2877 return upb_fielddef_setsubdefname(this, name, s);
2878}
2879inline bool FieldDef::set_subdef_name(const std::string& name, Status* s) {
2880 return upb_fielddef_setsubdefname(this, upb_safecstr(name), s);
2881}
2882
2883inline reffed_ptr<MessageDef> MessageDef::New() {
2884 upb_msgdef *m = upb_msgdef_new(&m);
2885 return reffed_ptr<MessageDef>(m, &m);
2886}
Chris Fallin91473dc2014-12-12 15:58:26 -08002887inline const char *MessageDef::full_name() const {
2888 return upb_msgdef_fullname(this);
2889}
Josh Haberman94e54b32016-04-14 12:06:09 -07002890inline const char *MessageDef::name() const {
2891 return upb_msgdef_name(this);
2892}
Chris Fallin91473dc2014-12-12 15:58:26 -08002893inline bool MessageDef::set_full_name(const char* fullname, Status* s) {
2894 return upb_msgdef_setfullname(this, fullname, s);
2895}
2896inline bool MessageDef::set_full_name(const std::string& fullname, Status* s) {
2897 return upb_msgdef_setfullname(this, upb_safecstr(fullname), s);
2898}
2899inline bool MessageDef::Freeze(Status* status) {
2900 return upb_msgdef_freeze(this, status);
2901}
2902inline int MessageDef::field_count() const {
2903 return upb_msgdef_numfields(this);
2904}
Chris Fallinfcd88892015-01-13 18:14:39 -08002905inline int MessageDef::oneof_count() const {
2906 return upb_msgdef_numoneofs(this);
2907}
Chris Fallin91473dc2014-12-12 15:58:26 -08002908inline bool MessageDef::AddField(upb_fielddef* f, Status* s) {
2909 return upb_msgdef_addfield(this, f, NULL, s);
2910}
2911inline bool MessageDef::AddField(const reffed_ptr<FieldDef>& f, Status* s) {
2912 return upb_msgdef_addfield(this, f.get(), NULL, s);
2913}
Chris Fallinfcd88892015-01-13 18:14:39 -08002914inline bool MessageDef::AddOneof(upb_oneofdef* o, Status* s) {
2915 return upb_msgdef_addoneof(this, o, NULL, s);
2916}
2917inline bool MessageDef::AddOneof(const reffed_ptr<OneofDef>& o, Status* s) {
2918 return upb_msgdef_addoneof(this, o.get(), NULL, s);
2919}
Chris Fallin91473dc2014-12-12 15:58:26 -08002920inline FieldDef* MessageDef::FindFieldByNumber(uint32_t number) {
2921 return upb_msgdef_itof_mutable(this, number);
2922}
2923inline FieldDef* MessageDef::FindFieldByName(const char* name, size_t len) {
2924 return upb_msgdef_ntof_mutable(this, name, len);
2925}
2926inline const FieldDef* MessageDef::FindFieldByNumber(uint32_t number) const {
2927 return upb_msgdef_itof(this, number);
2928}
2929inline const FieldDef *MessageDef::FindFieldByName(const char *name,
2930 size_t len) const {
2931 return upb_msgdef_ntof(this, name, len);
2932}
Chris Fallinfcd88892015-01-13 18:14:39 -08002933inline OneofDef* MessageDef::FindOneofByName(const char* name, size_t len) {
2934 return upb_msgdef_ntoo_mutable(this, name, len);
2935}
2936inline const OneofDef* MessageDef::FindOneofByName(const char* name,
2937 size_t len) const {
2938 return upb_msgdef_ntoo(this, name, len);
2939}
Chris Fallin91473dc2014-12-12 15:58:26 -08002940inline MessageDef* MessageDef::Dup(const void *owner) const {
2941 return upb_msgdef_dup(this, owner);
2942}
Chris Fallinfd1a3ff2015-01-06 15:44:09 -08002943inline void MessageDef::setmapentry(bool map_entry) {
2944 upb_msgdef_setmapentry(this, map_entry);
2945}
2946inline bool MessageDef::mapentry() const {
2947 return upb_msgdef_mapentry(this);
2948}
Chris Fallinfcd88892015-01-13 18:14:39 -08002949inline MessageDef::field_iterator MessageDef::field_begin() {
2950 return field_iterator(this);
Chris Fallin91473dc2014-12-12 15:58:26 -08002951}
Chris Fallinfcd88892015-01-13 18:14:39 -08002952inline MessageDef::field_iterator MessageDef::field_end() {
2953 return field_iterator::end(this);
2954}
2955inline MessageDef::const_field_iterator MessageDef::field_begin() const {
2956 return const_field_iterator(this);
2957}
2958inline MessageDef::const_field_iterator MessageDef::field_end() const {
2959 return const_field_iterator::end(this);
Chris Fallin91473dc2014-12-12 15:58:26 -08002960}
2961
Chris Fallinfcd88892015-01-13 18:14:39 -08002962inline MessageDef::oneof_iterator MessageDef::oneof_begin() {
2963 return oneof_iterator(this);
Chris Fallin91473dc2014-12-12 15:58:26 -08002964}
Chris Fallinfcd88892015-01-13 18:14:39 -08002965inline MessageDef::oneof_iterator MessageDef::oneof_end() {
2966 return oneof_iterator::end(this);
2967}
2968inline MessageDef::const_oneof_iterator MessageDef::oneof_begin() const {
2969 return const_oneof_iterator(this);
2970}
2971inline MessageDef::const_oneof_iterator MessageDef::oneof_end() const {
2972 return const_oneof_iterator::end(this);
2973}
2974
2975inline MessageDef::field_iterator::field_iterator(MessageDef* md) {
2976 upb_msg_field_begin(&iter_, md);
2977}
2978inline MessageDef::field_iterator MessageDef::field_iterator::end(
2979 MessageDef* md) {
2980 MessageDef::field_iterator iter(md);
2981 upb_msg_field_iter_setdone(&iter.iter_);
Chris Fallin91473dc2014-12-12 15:58:26 -08002982 return iter;
2983}
Chris Fallinfcd88892015-01-13 18:14:39 -08002984inline FieldDef* MessageDef::field_iterator::operator*() const {
Chris Fallin91473dc2014-12-12 15:58:26 -08002985 return upb_msg_iter_field(&iter_);
2986}
Chris Fallinfcd88892015-01-13 18:14:39 -08002987inline void MessageDef::field_iterator::operator++() {
2988 return upb_msg_field_next(&iter_);
2989}
2990inline bool MessageDef::field_iterator::operator==(
2991 const field_iterator &other) const {
Chris Fallin91473dc2014-12-12 15:58:26 -08002992 return upb_inttable_iter_isequal(&iter_, &other.iter_);
2993}
Chris Fallinfcd88892015-01-13 18:14:39 -08002994inline bool MessageDef::field_iterator::operator!=(
2995 const field_iterator &other) const {
Chris Fallin91473dc2014-12-12 15:58:26 -08002996 return !(*this == other);
2997}
2998
Chris Fallinfcd88892015-01-13 18:14:39 -08002999inline MessageDef::const_field_iterator::const_field_iterator(
3000 const MessageDef* md) {
3001 upb_msg_field_begin(&iter_, md);
Chris Fallin91473dc2014-12-12 15:58:26 -08003002}
Chris Fallinfcd88892015-01-13 18:14:39 -08003003inline MessageDef::const_field_iterator MessageDef::const_field_iterator::end(
Chris Fallin91473dc2014-12-12 15:58:26 -08003004 const MessageDef *md) {
Chris Fallinfcd88892015-01-13 18:14:39 -08003005 MessageDef::const_field_iterator iter(md);
3006 upb_msg_field_iter_setdone(&iter.iter_);
Chris Fallin91473dc2014-12-12 15:58:26 -08003007 return iter;
3008}
Chris Fallinfcd88892015-01-13 18:14:39 -08003009inline const FieldDef* MessageDef::const_field_iterator::operator*() const {
Chris Fallin91473dc2014-12-12 15:58:26 -08003010 return upb_msg_iter_field(&iter_);
3011}
Chris Fallinfcd88892015-01-13 18:14:39 -08003012inline void MessageDef::const_field_iterator::operator++() {
3013 return upb_msg_field_next(&iter_);
Chris Fallin91473dc2014-12-12 15:58:26 -08003014}
Chris Fallinfcd88892015-01-13 18:14:39 -08003015inline bool MessageDef::const_field_iterator::operator==(
3016 const const_field_iterator &other) const {
Chris Fallin91473dc2014-12-12 15:58:26 -08003017 return upb_inttable_iter_isequal(&iter_, &other.iter_);
3018}
Chris Fallinfcd88892015-01-13 18:14:39 -08003019inline bool MessageDef::const_field_iterator::operator!=(
3020 const const_field_iterator &other) const {
3021 return !(*this == other);
3022}
3023
3024inline MessageDef::oneof_iterator::oneof_iterator(MessageDef* md) {
3025 upb_msg_oneof_begin(&iter_, md);
3026}
3027inline MessageDef::oneof_iterator MessageDef::oneof_iterator::end(
3028 MessageDef* md) {
3029 MessageDef::oneof_iterator iter(md);
3030 upb_msg_oneof_iter_setdone(&iter.iter_);
3031 return iter;
3032}
3033inline OneofDef* MessageDef::oneof_iterator::operator*() const {
3034 return upb_msg_iter_oneof(&iter_);
3035}
3036inline void MessageDef::oneof_iterator::operator++() {
3037 return upb_msg_oneof_next(&iter_);
3038}
3039inline bool MessageDef::oneof_iterator::operator==(
3040 const oneof_iterator &other) const {
3041 return upb_strtable_iter_isequal(&iter_, &other.iter_);
3042}
3043inline bool MessageDef::oneof_iterator::operator!=(
3044 const oneof_iterator &other) const {
3045 return !(*this == other);
3046}
3047
3048inline MessageDef::const_oneof_iterator::const_oneof_iterator(
3049 const MessageDef* md) {
3050 upb_msg_oneof_begin(&iter_, md);
3051}
3052inline MessageDef::const_oneof_iterator MessageDef::const_oneof_iterator::end(
3053 const MessageDef *md) {
3054 MessageDef::const_oneof_iterator iter(md);
3055 upb_msg_oneof_iter_setdone(&iter.iter_);
3056 return iter;
3057}
3058inline const OneofDef* MessageDef::const_oneof_iterator::operator*() const {
3059 return upb_msg_iter_oneof(&iter_);
3060}
3061inline void MessageDef::const_oneof_iterator::operator++() {
3062 return upb_msg_oneof_next(&iter_);
3063}
3064inline bool MessageDef::const_oneof_iterator::operator==(
3065 const const_oneof_iterator &other) const {
3066 return upb_strtable_iter_isequal(&iter_, &other.iter_);
3067}
3068inline bool MessageDef::const_oneof_iterator::operator!=(
3069 const const_oneof_iterator &other) const {
Chris Fallin91473dc2014-12-12 15:58:26 -08003070 return !(*this == other);
3071}
3072
3073inline reffed_ptr<EnumDef> EnumDef::New() {
3074 upb_enumdef *e = upb_enumdef_new(&e);
3075 return reffed_ptr<EnumDef>(e, &e);
3076}
Chris Fallin91473dc2014-12-12 15:58:26 -08003077inline const char* EnumDef::full_name() const {
3078 return upb_enumdef_fullname(this);
3079}
Josh Haberman94e54b32016-04-14 12:06:09 -07003080inline const char* EnumDef::name() const {
3081 return upb_enumdef_name(this);
3082}
Chris Fallin91473dc2014-12-12 15:58:26 -08003083inline bool EnumDef::set_full_name(const char* fullname, Status* s) {
3084 return upb_enumdef_setfullname(this, fullname, s);
3085}
3086inline bool EnumDef::set_full_name(const std::string& fullname, Status* s) {
3087 return upb_enumdef_setfullname(this, upb_safecstr(fullname), s);
3088}
3089inline bool EnumDef::Freeze(Status* status) {
3090 return upb_enumdef_freeze(this, status);
3091}
3092inline int32_t EnumDef::default_value() const {
3093 return upb_enumdef_default(this);
3094}
3095inline bool EnumDef::set_default_value(int32_t val, Status* status) {
3096 return upb_enumdef_setdefault(this, val, status);
3097}
3098inline int EnumDef::value_count() const { return upb_enumdef_numvals(this); }
3099inline bool EnumDef::AddValue(const char* name, int32_t num, Status* status) {
3100 return upb_enumdef_addval(this, name, num, status);
3101}
3102inline bool EnumDef::AddValue(const std::string& name, int32_t num,
3103 Status* status) {
3104 return upb_enumdef_addval(this, upb_safecstr(name), num, status);
3105}
3106inline bool EnumDef::FindValueByName(const char* name, int32_t *num) const {
3107 return upb_enumdef_ntoiz(this, name, num);
3108}
3109inline const char* EnumDef::FindValueByNumber(int32_t num) const {
3110 return upb_enumdef_iton(this, num);
3111}
3112inline EnumDef* EnumDef::Dup(const void* owner) const {
3113 return upb_enumdef_dup(this, owner);
3114}
3115
3116inline EnumDef::Iterator::Iterator(const EnumDef* e) {
3117 upb_enum_begin(&iter_, e);
3118}
3119inline int32_t EnumDef::Iterator::number() {
3120 return upb_enum_iter_number(&iter_);
3121}
3122inline const char* EnumDef::Iterator::name() {
3123 return upb_enum_iter_name(&iter_);
3124}
3125inline bool EnumDef::Iterator::Done() { return upb_enum_done(&iter_); }
3126inline void EnumDef::Iterator::Next() { return upb_enum_next(&iter_); }
Chris Fallinfcd88892015-01-13 18:14:39 -08003127
3128inline reffed_ptr<OneofDef> OneofDef::New() {
3129 upb_oneofdef *o = upb_oneofdef_new(&o);
3130 return reffed_ptr<OneofDef>(o, &o);
3131}
Chris Fallinfcd88892015-01-13 18:14:39 -08003132
3133inline const MessageDef* OneofDef::containing_type() const {
3134 return upb_oneofdef_containingtype(this);
3135}
3136inline const char* OneofDef::name() const {
3137 return upb_oneofdef_name(this);
3138}
3139inline bool OneofDef::set_name(const char* name, Status* s) {
3140 return upb_oneofdef_setname(this, name, s);
3141}
Josh Haberman94e54b32016-04-14 12:06:09 -07003142inline bool OneofDef::set_name(const std::string& name, Status* s) {
3143 return upb_oneofdef_setname(this, upb_safecstr(name), s);
3144}
Chris Fallinfcd88892015-01-13 18:14:39 -08003145inline int OneofDef::field_count() const {
3146 return upb_oneofdef_numfields(this);
3147}
3148inline bool OneofDef::AddField(FieldDef* field, Status* s) {
3149 return upb_oneofdef_addfield(this, field, NULL, s);
3150}
3151inline bool OneofDef::AddField(const reffed_ptr<FieldDef>& field, Status* s) {
3152 return upb_oneofdef_addfield(this, field.get(), NULL, s);
3153}
3154inline const FieldDef* OneofDef::FindFieldByName(const char* name,
3155 size_t len) const {
3156 return upb_oneofdef_ntof(this, name, len);
3157}
3158inline const FieldDef* OneofDef::FindFieldByNumber(uint32_t num) const {
3159 return upb_oneofdef_itof(this, num);
3160}
3161inline OneofDef::iterator OneofDef::begin() { return iterator(this); }
3162inline OneofDef::iterator OneofDef::end() { return iterator::end(this); }
3163inline OneofDef::const_iterator OneofDef::begin() const {
3164 return const_iterator(this);
3165}
3166inline OneofDef::const_iterator OneofDef::end() const {
3167 return const_iterator::end(this);
3168}
3169
3170inline OneofDef::iterator::iterator(OneofDef* o) {
3171 upb_oneof_begin(&iter_, o);
3172}
3173inline OneofDef::iterator OneofDef::iterator::end(OneofDef* o) {
3174 OneofDef::iterator iter(o);
3175 upb_oneof_iter_setdone(&iter.iter_);
3176 return iter;
3177}
3178inline FieldDef* OneofDef::iterator::operator*() const {
3179 return upb_oneof_iter_field(&iter_);
3180}
3181inline void OneofDef::iterator::operator++() { return upb_oneof_next(&iter_); }
3182inline bool OneofDef::iterator::operator==(const iterator &other) const {
3183 return upb_inttable_iter_isequal(&iter_, &other.iter_);
3184}
3185inline bool OneofDef::iterator::operator!=(const iterator &other) const {
3186 return !(*this == other);
3187}
3188
3189inline OneofDef::const_iterator::const_iterator(const OneofDef* md) {
3190 upb_oneof_begin(&iter_, md);
3191}
3192inline OneofDef::const_iterator OneofDef::const_iterator::end(
3193 const OneofDef *md) {
3194 OneofDef::const_iterator iter(md);
3195 upb_oneof_iter_setdone(&iter.iter_);
3196 return iter;
3197}
3198inline const FieldDef* OneofDef::const_iterator::operator*() const {
3199 return upb_msg_iter_field(&iter_);
3200}
3201inline void OneofDef::const_iterator::operator++() {
3202 return upb_oneof_next(&iter_);
3203}
3204inline bool OneofDef::const_iterator::operator==(
3205 const const_iterator &other) const {
3206 return upb_inttable_iter_isequal(&iter_, &other.iter_);
3207}
3208inline bool OneofDef::const_iterator::operator!=(
3209 const const_iterator &other) const {
3210 return !(*this == other);
3211}
3212
Josh Haberman94e54b32016-04-14 12:06:09 -07003213inline reffed_ptr<FileDef> FileDef::New() {
3214 upb_filedef *f = upb_filedef_new(&f);
3215 return reffed_ptr<FileDef>(f, &f);
3216}
3217
3218inline const char* FileDef::name() const {
3219 return upb_filedef_name(this);
3220}
3221inline bool FileDef::set_name(const char* name, Status* s) {
3222 return upb_filedef_setname(this, name, s);
3223}
3224inline bool FileDef::set_name(const std::string& name, Status* s) {
3225 return upb_filedef_setname(this, upb_safecstr(name), s);
3226}
3227inline const char* FileDef::package() const {
3228 return upb_filedef_package(this);
3229}
3230inline bool FileDef::set_package(const char* package, Status* s) {
3231 return upb_filedef_setpackage(this, package, s);
3232}
3233inline int FileDef::def_count() const {
3234 return upb_filedef_defcount(this);
3235}
3236inline const Def* FileDef::def(int index) const {
3237 return upb_filedef_def(this, index);
3238}
3239inline Def* FileDef::def(int index) {
3240 return const_cast<Def*>(upb_filedef_def(this, index));
3241}
3242inline int FileDef::dependency_count() const {
3243 return upb_filedef_depcount(this);
3244}
3245inline const FileDef* FileDef::dependency(int index) const {
3246 return upb_filedef_dep(this, index);
3247}
3248inline bool FileDef::AddDef(Def* def, Status* s) {
3249 return upb_filedef_adddef(this, def, NULL, s);
3250}
3251inline bool FileDef::AddMessage(MessageDef* m, Status* s) {
3252 return upb_filedef_addmsg(this, m, NULL, s);
3253}
3254inline bool FileDef::AddEnum(EnumDef* e, Status* s) {
3255 return upb_filedef_addenum(this, e, NULL, s);
3256}
3257inline bool FileDef::AddExtension(FieldDef* f, Status* s) {
3258 return upb_filedef_addext(this, f, NULL, s);
3259}
3260inline bool FileDef::AddDependency(const FileDef* file) {
3261 return upb_filedef_adddep(this, file);
3262}
3263
Josh Habermane8ed0212015-06-08 17:56:03 -07003264} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08003265#endif
3266
Chris Fallin91473dc2014-12-12 15:58:26 -08003267#endif /* UPB_DEF_H_ */
Chris Fallin91473dc2014-12-12 15:58:26 -08003268/*
Josh Haberman181c7f22015-07-15 11:05:10 -07003269** This file contains definitions of structs that should be considered private
3270** and NOT stable across versions of upb.
3271**
3272** The only reason they are declared here and not in .c files is to allow upb
3273** and the application (if desired) to embed statically-initialized instances
3274** of structures like defs.
3275**
3276** If you include this file, all guarantees of ABI compatibility go out the
3277** window! Any code that includes this file needs to recompile against the
3278** exact same version of upb that they are linking against.
3279**
3280** You also need to recompile if you change the value of the UPB_DEBUG_REFS
3281** flag.
3282*/
Chris Fallin91473dc2014-12-12 15:58:26 -08003283
Chris Fallin91473dc2014-12-12 15:58:26 -08003284
Josh Habermane8ed0212015-06-08 17:56:03 -07003285#ifndef UPB_STATICINIT_H_
3286#define UPB_STATICINIT_H_
Chris Fallin91473dc2014-12-12 15:58:26 -08003287
3288#ifdef __cplusplus
Josh Habermane8ed0212015-06-08 17:56:03 -07003289/* Because of how we do our typedefs, this header can't be included from C++. */
3290#error This file cannot be included from C++
Chris Fallin91473dc2014-12-12 15:58:26 -08003291#endif
3292
Josh Habermane8ed0212015-06-08 17:56:03 -07003293/* upb_refcounted *************************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -08003294
Chris Fallin91473dc2014-12-12 15:58:26 -08003295
Josh Habermane8ed0212015-06-08 17:56:03 -07003296/* upb_def ********************************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -08003297
Josh Habermane8ed0212015-06-08 17:56:03 -07003298struct upb_def {
3299 upb_refcounted base;
Chris Fallin91473dc2014-12-12 15:58:26 -08003300
Josh Habermane8ed0212015-06-08 17:56:03 -07003301 const char *fullname;
Josh Haberman94e54b32016-04-14 12:06:09 -07003302 const upb_filedef* file;
Josh Habermane8ed0212015-06-08 17:56:03 -07003303 char type; /* A upb_deftype_t (char to save space) */
Chris Fallin91473dc2014-12-12 15:58:26 -08003304
Josh Habermane8ed0212015-06-08 17:56:03 -07003305 /* Used as a flag during the def's mutable stage. Must be false unless
3306 * it is currently being used by a function on the stack. This allows
3307 * us to easily determine which defs were passed into the function's
3308 * current invocation. */
3309 bool came_from_user;
3310};
Chris Fallin91473dc2014-12-12 15:58:26 -08003311
Josh Habermane8ed0212015-06-08 17:56:03 -07003312#define UPB_DEF_INIT(name, type, refs, ref2s) \
Josh Haberman94e54b32016-04-14 12:06:09 -07003313 { UPB_REFCOUNT_INIT(refs, ref2s), name, NULL, type, false }
Chris Fallin91473dc2014-12-12 15:58:26 -08003314
Chris Fallin91473dc2014-12-12 15:58:26 -08003315
Josh Habermane8ed0212015-06-08 17:56:03 -07003316/* upb_fielddef ***************************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -08003317
Josh Habermane8ed0212015-06-08 17:56:03 -07003318struct upb_fielddef {
3319 upb_def base;
Chris Fallin91473dc2014-12-12 15:58:26 -08003320
Josh Habermane8ed0212015-06-08 17:56:03 -07003321 union {
3322 int64_t sint;
3323 uint64_t uint;
3324 double dbl;
3325 float flt;
3326 void *bytes;
3327 } defaultval;
3328 union {
3329 const upb_msgdef *def; /* If !msg_is_symbolic. */
3330 char *name; /* If msg_is_symbolic. */
3331 } msg;
3332 union {
3333 const upb_def *def; /* If !subdef_is_symbolic. */
3334 char *name; /* If subdef_is_symbolic. */
3335 } sub; /* The msgdef or enumdef for this field, if upb_hassubdef(f). */
3336 bool subdef_is_symbolic;
3337 bool msg_is_symbolic;
3338 const upb_oneofdef *oneof;
3339 bool default_is_string;
3340 bool type_is_set_; /* False until type is explicitly set. */
3341 bool is_extension_;
3342 bool lazy_;
3343 bool packed_;
3344 upb_intfmt_t intfmt;
3345 bool tagdelim;
3346 upb_fieldtype_t type_;
3347 upb_label_t label_;
3348 uint32_t number_;
3349 uint32_t selector_base; /* Used to index into a upb::Handlers table. */
3350 uint32_t index_;
3351};
3352
3353#define UPB_FIELDDEF_INIT(label, type, intfmt, tagdelim, is_extension, lazy, \
3354 packed, name, num, msgdef, subdef, selector_base, \
3355 index, defaultval, refs, ref2s) \
3356 { \
3357 UPB_DEF_INIT(name, UPB_DEF_FIELD, refs, ref2s), defaultval, {msgdef}, \
3358 {subdef}, NULL, false, false, \
3359 type == UPB_TYPE_STRING || type == UPB_TYPE_BYTES, true, is_extension, \
3360 lazy, packed, intfmt, tagdelim, type, label, num, selector_base, index \
Chris Fallin91473dc2014-12-12 15:58:26 -08003361 }
3362
Josh Habermane8ed0212015-06-08 17:56:03 -07003363
3364/* upb_msgdef *****************************************************************/
3365
3366struct upb_msgdef {
3367 upb_def base;
3368
3369 size_t selector_count;
3370 uint32_t submsg_field_count;
3371
3372 /* Tables for looking up fields by number and name. */
3373 upb_inttable itof; /* int to field */
3374 upb_strtable ntof; /* name to field */
3375
3376 /* Tables for looking up oneofs by name. */
3377 upb_strtable ntoo; /* name to oneof */
3378
3379 /* Is this a map-entry message?
3380 * TODO: set this flag properly for static descriptors; regenerate
3381 * descriptor.upb.c. */
3382 bool map_entry;
3383
Josh Haberman94e54b32016-04-14 12:06:09 -07003384 /* Whether this message has proto2 or proto3 semantics.
Josh Haberman78da6662016-01-13 19:05:43 -08003385 * TODO: set this flag properly for static descriptors; regenerate
3386 * descriptor.upb.c. */
Josh Haberman94e54b32016-04-14 12:06:09 -07003387 upb_syntax_t syntax;
Josh Haberman78da6662016-01-13 19:05:43 -08003388
Josh Habermane8ed0212015-06-08 17:56:03 -07003389 /* TODO(haberman): proper extension ranges (there can be multiple). */
3390};
3391
3392/* TODO: also support static initialization of the oneofs table. This will be
3393 * needed if we compile in descriptors that contain oneofs. */
3394#define UPB_MSGDEF_INIT(name, selector_count, submsg_field_count, itof, ntof, \
3395 refs, ref2s) \
3396 { \
3397 UPB_DEF_INIT(name, UPB_DEF_MSG, refs, ref2s), selector_count, \
3398 submsg_field_count, itof, ntof, \
Josh Haberman78da6662016-01-13 19:05:43 -08003399 UPB_EMPTY_STRTABLE_INIT(UPB_CTYPE_PTR), false, true \
Josh Habermane8ed0212015-06-08 17:56:03 -07003400 }
3401
3402
3403/* upb_enumdef ****************************************************************/
3404
3405struct upb_enumdef {
3406 upb_def base;
3407
3408 upb_strtable ntoi;
3409 upb_inttable iton;
3410 int32_t defaultval;
3411};
3412
3413#define UPB_ENUMDEF_INIT(name, ntoi, iton, defaultval, refs, ref2s) \
3414 { UPB_DEF_INIT(name, UPB_DEF_ENUM, refs, ref2s), ntoi, iton, defaultval }
3415
3416
3417/* upb_oneofdef ***************************************************************/
3418
3419struct upb_oneofdef {
Josh Haberman94e54b32016-04-14 12:06:09 -07003420 upb_refcounted base;
Josh Habermane8ed0212015-06-08 17:56:03 -07003421
Josh Haberman94e54b32016-04-14 12:06:09 -07003422 const char *name;
Josh Habermane8ed0212015-06-08 17:56:03 -07003423 upb_strtable ntof;
3424 upb_inttable itof;
3425 const upb_msgdef *parent;
3426};
3427
3428#define UPB_ONEOFDEF_INIT(name, ntof, itof, refs, ref2s) \
Josh Haberman94e54b32016-04-14 12:06:09 -07003429 { UPB_REFCOUNT_INIT(refs, ref2s), name, ntof, itof }
Josh Habermane8ed0212015-06-08 17:56:03 -07003430
3431
3432/* upb_symtab *****************************************************************/
3433
3434struct upb_symtab {
3435 upb_refcounted base;
3436
Chris Fallin91473dc2014-12-12 15:58:26 -08003437 upb_strtable symtab;
Josh Habermane8ed0212015-06-08 17:56:03 -07003438};
Chris Fallin91473dc2014-12-12 15:58:26 -08003439
3440#define UPB_SYMTAB_INIT(symtab, refs, ref2s) \
3441 { UPB_REFCOUNT_INIT(refs, ref2s), symtab }
3442
Josh Haberman94e54b32016-04-14 12:06:09 -07003443struct upb_filedef {
3444 upb_refcounted base;
3445
3446 const char *name;
3447 const char *package;
3448 upb_syntax_t syntax;
3449
3450 upb_inttable defs;
3451 upb_inttable deps;
3452};
Chris Fallin91473dc2014-12-12 15:58:26 -08003453
Josh Habermane8ed0212015-06-08 17:56:03 -07003454#endif /* UPB_STATICINIT_H_ */
Chris Fallin91473dc2014-12-12 15:58:26 -08003455/*
Josh Haberman181c7f22015-07-15 11:05:10 -07003456** upb::Handlers (upb_handlers)
3457**
3458** A upb_handlers is like a virtual table for a upb_msgdef. Each field of the
3459** message can have associated functions that will be called when we are
3460** parsing or visiting a stream of data. This is similar to how handlers work
3461** in SAX (the Simple API for XML).
3462**
3463** The handlers have no idea where the data is coming from, so a single set of
3464** handlers could be used with two completely different data sources (for
3465** example, a parser and a visitor over in-memory objects). This decoupling is
3466** the most important feature of upb, because it allows parsers and serializers
3467** to be highly reusable.
3468**
3469** This is a mixed C/C++ interface that offers a full API to both languages.
3470** See the top-level README for more information.
3471*/
Chris Fallin91473dc2014-12-12 15:58:26 -08003472
3473#ifndef UPB_HANDLERS_H
3474#define UPB_HANDLERS_H
3475
3476
3477#ifdef __cplusplus
3478namespace upb {
3479class BufferHandle;
3480class BytesHandler;
3481class HandlerAttributes;
3482class Handlers;
3483template <class T> class Handler;
3484template <class T> struct CanonicalType;
Josh Habermane8ed0212015-06-08 17:56:03 -07003485} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08003486#endif
3487
Josh Habermane8ed0212015-06-08 17:56:03 -07003488UPB_DECLARE_TYPE(upb::BufferHandle, upb_bufhandle)
3489UPB_DECLARE_TYPE(upb::BytesHandler, upb_byteshandler)
3490UPB_DECLARE_TYPE(upb::HandlerAttributes, upb_handlerattr)
3491UPB_DECLARE_DERIVED_TYPE(upb::Handlers, upb::RefCounted,
3492 upb_handlers, upb_refcounted)
Chris Fallin91473dc2014-12-12 15:58:26 -08003493
Josh Habermane8ed0212015-06-08 17:56:03 -07003494/* The maximum depth that the handler graph can have. This is a resource limit
3495 * for the C stack since we sometimes need to recursively traverse the graph.
3496 * Cycles are ok; the traversal will stop when it detects a cycle, but we must
3497 * hit the cycle before the maximum depth is reached.
3498 *
3499 * If having a single static limit is too inflexible, we can add another variant
3500 * of Handlers::Freeze that allows specifying this as a parameter. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003501#define UPB_MAX_HANDLER_DEPTH 64
3502
Josh Habermane8ed0212015-06-08 17:56:03 -07003503/* All the different types of handlers that can be registered.
3504 * Only needed for the advanced functions in upb::Handlers. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003505typedef enum {
3506 UPB_HANDLER_INT32,
3507 UPB_HANDLER_INT64,
3508 UPB_HANDLER_UINT32,
3509 UPB_HANDLER_UINT64,
3510 UPB_HANDLER_FLOAT,
3511 UPB_HANDLER_DOUBLE,
3512 UPB_HANDLER_BOOL,
3513 UPB_HANDLER_STARTSTR,
3514 UPB_HANDLER_STRING,
3515 UPB_HANDLER_ENDSTR,
3516 UPB_HANDLER_STARTSUBMSG,
3517 UPB_HANDLER_ENDSUBMSG,
3518 UPB_HANDLER_STARTSEQ,
Josh Habermane8ed0212015-06-08 17:56:03 -07003519 UPB_HANDLER_ENDSEQ
Chris Fallin91473dc2014-12-12 15:58:26 -08003520} upb_handlertype_t;
3521
3522#define UPB_HANDLER_MAX (UPB_HANDLER_ENDSEQ+1)
3523
3524#define UPB_BREAK NULL
3525
Josh Habermane8ed0212015-06-08 17:56:03 -07003526/* A convenient definition for when no closure is needed. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003527extern char _upb_noclosure;
3528#define UPB_NO_CLOSURE &_upb_noclosure
3529
Josh Habermane8ed0212015-06-08 17:56:03 -07003530/* A selector refers to a specific field handler in the Handlers object
3531 * (for example: the STARTSUBMSG handler for field "field15"). */
Chris Fallin91473dc2014-12-12 15:58:26 -08003532typedef int32_t upb_selector_t;
3533
3534UPB_BEGIN_EXTERN_C
3535
Josh Habermane8ed0212015-06-08 17:56:03 -07003536/* Forward-declares for C inline accessors. We need to declare these here
3537 * so we can "friend" them in the class declarations in C++. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003538UPB_INLINE upb_func *upb_handlers_gethandler(const upb_handlers *h,
3539 upb_selector_t s);
3540UPB_INLINE const void *upb_handlerattr_handlerdata(const upb_handlerattr *attr);
3541UPB_INLINE const void *upb_handlers_gethandlerdata(const upb_handlers *h,
3542 upb_selector_t s);
3543
3544UPB_INLINE void upb_bufhandle_init(upb_bufhandle *h);
3545UPB_INLINE void upb_bufhandle_setobj(upb_bufhandle *h, const void *obj,
3546 const void *type);
3547UPB_INLINE void upb_bufhandle_setbuf(upb_bufhandle *h, const char *buf,
3548 size_t ofs);
3549UPB_INLINE const void *upb_bufhandle_obj(const upb_bufhandle *h);
3550UPB_INLINE const void *upb_bufhandle_objtype(const upb_bufhandle *h);
3551UPB_INLINE const char *upb_bufhandle_buf(const upb_bufhandle *h);
3552
3553UPB_END_EXTERN_C
3554
3555
Josh Habermane8ed0212015-06-08 17:56:03 -07003556/* Static selectors for upb::Handlers. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003557#define UPB_STARTMSG_SELECTOR 0
3558#define UPB_ENDMSG_SELECTOR 1
3559#define UPB_STATIC_SELECTOR_COUNT 2
3560
Josh Habermane8ed0212015-06-08 17:56:03 -07003561/* Static selectors for upb::BytesHandler. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003562#define UPB_STARTSTR_SELECTOR 0
3563#define UPB_STRING_SELECTOR 1
3564#define UPB_ENDSTR_SELECTOR 2
3565
3566typedef void upb_handlerfree(void *d);
3567
Josh Habermane8ed0212015-06-08 17:56:03 -07003568#ifdef __cplusplus
3569
3570/* A set of attributes that accompanies a handler's function pointer. */
3571class upb::HandlerAttributes {
Chris Fallin91473dc2014-12-12 15:58:26 -08003572 public:
3573 HandlerAttributes();
3574 ~HandlerAttributes();
3575
Josh Habermane8ed0212015-06-08 17:56:03 -07003576 /* Sets the handler data that will be passed as the second parameter of the
3577 * handler. To free this pointer when the handlers are freed, call
3578 * Handlers::AddCleanup(). */
Chris Fallin91473dc2014-12-12 15:58:26 -08003579 bool SetHandlerData(const void *handler_data);
3580 const void* handler_data() const;
3581
Josh Habermane8ed0212015-06-08 17:56:03 -07003582 /* Use this to specify the type of the closure. This will be checked against
3583 * all other closure types for handler that use the same closure.
3584 * Registration will fail if this does not match all other non-NULL closure
3585 * types. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003586 bool SetClosureType(const void *closure_type);
3587 const void* closure_type() const;
3588
Josh Habermane8ed0212015-06-08 17:56:03 -07003589 /* Use this to specify the type of the returned closure. Only used for
3590 * Start*{String,SubMessage,Sequence} handlers. This must match the closure
3591 * type of any handlers that use it (for example, the StringBuf handler must
3592 * match the closure returned from StartString). */
Chris Fallin91473dc2014-12-12 15:58:26 -08003593 bool SetReturnClosureType(const void *return_closure_type);
3594 const void* return_closure_type() const;
3595
Josh Habermane8ed0212015-06-08 17:56:03 -07003596 /* Set to indicate that the handler always returns "ok" (either "true" or a
3597 * non-NULL closure). This is a hint that can allow code generators to
3598 * generate more efficient code. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003599 bool SetAlwaysOk(bool always_ok);
3600 bool always_ok() const;
3601
3602 private:
3603 friend UPB_INLINE const void * ::upb_handlerattr_handlerdata(
3604 const upb_handlerattr *attr);
Josh Habermane8ed0212015-06-08 17:56:03 -07003605#else
3606struct upb_handlerattr {
3607#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08003608 const void *handler_data_;
3609 const void *closure_type_;
3610 const void *return_closure_type_;
3611 bool alwaysok_;
Josh Habermane8ed0212015-06-08 17:56:03 -07003612};
Chris Fallin91473dc2014-12-12 15:58:26 -08003613
3614#define UPB_HANDLERATTR_INITIALIZER {NULL, NULL, NULL, false}
3615
3616typedef struct {
3617 upb_func *func;
Josh Habermane8ed0212015-06-08 17:56:03 -07003618
3619 /* It is wasteful to include the entire attributes here:
3620 *
3621 * * Some of the information is redundant (like storing the closure type
3622 * separately for each handler that must match).
3623 * * Some of the info is only needed prior to freeze() (like closure types).
3624 * * alignment padding wastes a lot of space for alwaysok_.
3625 *
3626 * If/when the size and locality of handlers is an issue, we can optimize this
3627 * not to store the entire attr like this. We do not expose the table's
3628 * layout to allow this optimization in the future. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003629 upb_handlerattr attr;
3630} upb_handlers_tabent;
3631
Josh Habermane8ed0212015-06-08 17:56:03 -07003632#ifdef __cplusplus
3633
3634/* Extra information about a buffer that is passed to a StringBuf handler.
3635 * TODO(haberman): allow the handle to be pinned so that it will outlive
3636 * the handler invocation. */
3637class upb::BufferHandle {
Chris Fallin91473dc2014-12-12 15:58:26 -08003638 public:
3639 BufferHandle();
3640 ~BufferHandle();
3641
Josh Habermane8ed0212015-06-08 17:56:03 -07003642 /* The beginning of the buffer. This may be different than the pointer
3643 * passed to a StringBuf handler because the handler may receive data
3644 * that is from the middle or end of a larger buffer. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003645 const char* buffer() const;
3646
Josh Habermane8ed0212015-06-08 17:56:03 -07003647 /* The offset within the attached object where this buffer begins. Only
3648 * meaningful if there is an attached object. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003649 size_t object_offset() const;
3650
Josh Habermane8ed0212015-06-08 17:56:03 -07003651 /* Note that object_offset is the offset of "buf" within the attached
3652 * object. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003653 void SetBuffer(const char* buf, size_t object_offset);
3654
Josh Habermane8ed0212015-06-08 17:56:03 -07003655 /* The BufferHandle can have an "attached object", which can be used to
3656 * tunnel through a pointer to the buffer's underlying representation. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003657 template <class T>
3658 void SetAttachedObject(const T* obj);
3659
Josh Habermane8ed0212015-06-08 17:56:03 -07003660 /* Returns NULL if the attached object is not of this type. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003661 template <class T>
3662 const T* GetAttachedObject() const;
3663
3664 private:
3665 friend UPB_INLINE void ::upb_bufhandle_init(upb_bufhandle *h);
3666 friend UPB_INLINE void ::upb_bufhandle_setobj(upb_bufhandle *h,
3667 const void *obj,
3668 const void *type);
3669 friend UPB_INLINE void ::upb_bufhandle_setbuf(upb_bufhandle *h,
3670 const char *buf, size_t ofs);
3671 friend UPB_INLINE const void* ::upb_bufhandle_obj(const upb_bufhandle *h);
3672 friend UPB_INLINE const void* ::upb_bufhandle_objtype(
3673 const upb_bufhandle *h);
3674 friend UPB_INLINE const char* ::upb_bufhandle_buf(const upb_bufhandle *h);
Josh Habermane8ed0212015-06-08 17:56:03 -07003675#else
3676struct upb_bufhandle {
3677#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08003678 const char *buf_;
3679 const void *obj_;
3680 const void *objtype_;
3681 size_t objofs_;
Josh Habermane8ed0212015-06-08 17:56:03 -07003682};
Chris Fallin91473dc2014-12-12 15:58:26 -08003683
Josh Habermane8ed0212015-06-08 17:56:03 -07003684#ifdef __cplusplus
3685
3686/* A upb::Handlers object represents the set of handlers associated with a
3687 * message in the graph of messages. You can think of it as a big virtual
3688 * table with functions corresponding to all the events that can fire while
3689 * parsing or visiting a message of a specific type.
3690 *
3691 * Any handlers that are not set behave as if they had successfully consumed
3692 * the value. Any unset Start* handlers will propagate their closure to the
3693 * inner frame.
3694 *
3695 * The easiest way to create the *Handler objects needed by the Set* methods is
3696 * with the UpbBind() and UpbMakeHandler() macros; see below. */
3697class upb::Handlers {
Chris Fallin91473dc2014-12-12 15:58:26 -08003698 public:
3699 typedef upb_selector_t Selector;
3700 typedef upb_handlertype_t Type;
3701
3702 typedef Handler<void *(*)(void *, const void *)> StartFieldHandler;
3703 typedef Handler<bool (*)(void *, const void *)> EndFieldHandler;
3704 typedef Handler<bool (*)(void *, const void *)> StartMessageHandler;
3705 typedef Handler<bool (*)(void *, const void *, Status*)> EndMessageHandler;
3706 typedef Handler<void *(*)(void *, const void *, size_t)> StartStringHandler;
3707 typedef Handler<size_t (*)(void *, const void *, const char *, size_t,
3708 const BufferHandle *)> StringHandler;
3709
3710 template <class T> struct ValueHandler {
3711 typedef Handler<bool(*)(void *, const void *, T)> H;
3712 };
3713
3714 typedef ValueHandler<int32_t>::H Int32Handler;
3715 typedef ValueHandler<int64_t>::H Int64Handler;
3716 typedef ValueHandler<uint32_t>::H UInt32Handler;
3717 typedef ValueHandler<uint64_t>::H UInt64Handler;
3718 typedef ValueHandler<float>::H FloatHandler;
3719 typedef ValueHandler<double>::H DoubleHandler;
3720 typedef ValueHandler<bool>::H BoolHandler;
3721
Josh Habermane8ed0212015-06-08 17:56:03 -07003722 /* Any function pointer can be converted to this and converted back to its
3723 * correct type. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003724 typedef void GenericFunction();
3725
3726 typedef void HandlersCallback(const void *closure, upb_handlers *h);
3727
Josh Habermane8ed0212015-06-08 17:56:03 -07003728 /* Returns a new handlers object for the given frozen msgdef.
3729 * Returns NULL if memory allocation failed. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003730 static reffed_ptr<Handlers> New(const MessageDef *m);
3731
Josh Habermane8ed0212015-06-08 17:56:03 -07003732 /* Convenience function for registering a graph of handlers that mirrors the
3733 * graph of msgdefs for some message. For "m" and all its children a new set
3734 * of handlers will be created and the given callback will be invoked,
3735 * allowing the client to register handlers for this message. Note that any
3736 * subhandlers set by the callback will be overwritten. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003737 static reffed_ptr<const Handlers> NewFrozen(const MessageDef *m,
3738 HandlersCallback *callback,
3739 const void *closure);
3740
Josh Habermane8ed0212015-06-08 17:56:03 -07003741 /* Functionality from upb::RefCounted. */
3742 UPB_REFCOUNTED_CPPMETHODS
Chris Fallin91473dc2014-12-12 15:58:26 -08003743
Josh Habermane8ed0212015-06-08 17:56:03 -07003744 /* All handler registration functions return bool to indicate success or
3745 * failure; details about failures are stored in this status object. If a
3746 * failure does occur, it must be cleared before the Handlers are frozen,
3747 * otherwise the freeze() operation will fail. The functions may *only* be
3748 * used while the Handlers are mutable. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003749 const Status* status();
3750 void ClearError();
3751
Josh Habermane8ed0212015-06-08 17:56:03 -07003752 /* Call to freeze these Handlers. Requires that any SubHandlers are already
3753 * frozen. For cycles, you must use the static version below and freeze the
3754 * whole graph at once. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003755 bool Freeze(Status* s);
3756
Josh Habermane8ed0212015-06-08 17:56:03 -07003757 /* Freezes the given set of handlers. You may not freeze a handler without
3758 * also freezing any handlers they point to. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003759 static bool Freeze(Handlers*const* handlers, int n, Status* s);
3760 static bool Freeze(const std::vector<Handlers*>& handlers, Status* s);
3761
Josh Habermane8ed0212015-06-08 17:56:03 -07003762 /* Returns the msgdef associated with this handlers object. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003763 const MessageDef* message_def() const;
3764
Josh Habermane8ed0212015-06-08 17:56:03 -07003765 /* Adds the given pointer and function to the list of cleanup functions that
3766 * will be run when these handlers are freed. If this pointer has previously
3767 * been registered, the function returns false and does nothing. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003768 bool AddCleanup(void *ptr, upb_handlerfree *cleanup);
3769
Josh Habermane8ed0212015-06-08 17:56:03 -07003770 /* Sets the startmsg handler for the message, which is defined as follows:
3771 *
3772 * bool startmsg(MyType* closure) {
3773 * // Called when the message begins. Returns true if processing should
3774 * // continue.
3775 * return true;
3776 * }
3777 */
Chris Fallin91473dc2014-12-12 15:58:26 -08003778 bool SetStartMessageHandler(const StartMessageHandler& handler);
3779
Josh Habermane8ed0212015-06-08 17:56:03 -07003780 /* Sets the endmsg handler for the message, which is defined as follows:
3781 *
3782 * bool endmsg(MyType* closure, upb_status *status) {
3783 * // Called when processing of this message ends, whether in success or
3784 * // failure. "status" indicates the final status of processing, and
3785 * // can also be modified in-place to update the final status.
3786 * }
3787 */
Chris Fallin91473dc2014-12-12 15:58:26 -08003788 bool SetEndMessageHandler(const EndMessageHandler& handler);
3789
Josh Habermane8ed0212015-06-08 17:56:03 -07003790 /* Sets the value handler for the given field, which is defined as follows
3791 * (this is for an int32 field; other field types will pass their native
3792 * C/C++ type for "val"):
3793 *
3794 * bool OnValue(MyClosure* c, const MyHandlerData* d, int32_t val) {
3795 * // Called when the field's value is encountered. "d" contains
3796 * // whatever data was bound to this field when it was registered.
3797 * // Returns true if processing should continue.
3798 * return true;
3799 * }
3800 *
3801 * handers->SetInt32Handler(f, UpbBind(OnValue, new MyHandlerData(...)));
3802 *
3803 * The value type must exactly match f->type().
3804 * For example, a handler that takes an int32_t parameter may only be used for
3805 * fields of type UPB_TYPE_INT32 and UPB_TYPE_ENUM.
3806 *
3807 * Returns false if the handler failed to register; in this case the cleanup
3808 * handler (if any) will be called immediately.
3809 */
Chris Fallin91473dc2014-12-12 15:58:26 -08003810 bool SetInt32Handler (const FieldDef* f, const Int32Handler& h);
3811 bool SetInt64Handler (const FieldDef* f, const Int64Handler& h);
3812 bool SetUInt32Handler(const FieldDef* f, const UInt32Handler& h);
3813 bool SetUInt64Handler(const FieldDef* f, const UInt64Handler& h);
3814 bool SetFloatHandler (const FieldDef* f, const FloatHandler& h);
3815 bool SetDoubleHandler(const FieldDef* f, const DoubleHandler& h);
3816 bool SetBoolHandler (const FieldDef* f, const BoolHandler& h);
3817
Josh Habermane8ed0212015-06-08 17:56:03 -07003818 /* Like the previous, but templated on the type on the value (ie. int32).
3819 * This is mostly useful to call from other templates. To call this you must
3820 * specify the template parameter explicitly, ie:
3821 * h->SetValueHandler<T>(f, UpbBind(MyHandler<T>, MyData)); */
Chris Fallin91473dc2014-12-12 15:58:26 -08003822 template <class T>
3823 bool SetValueHandler(
3824 const FieldDef *f,
3825 const typename ValueHandler<typename CanonicalType<T>::Type>::H& handler);
3826
Josh Habermane8ed0212015-06-08 17:56:03 -07003827 /* Sets handlers for a string field, which are defined as follows:
3828 *
3829 * MySubClosure* startstr(MyClosure* c, const MyHandlerData* d,
3830 * size_t size_hint) {
3831 * // Called when a string value begins. The return value indicates the
3832 * // closure for the string. "size_hint" indicates the size of the
3833 * // string if it is known, however if the string is length-delimited
3834 * // and the end-of-string is not available size_hint will be zero.
3835 * // This case is indistinguishable from the case where the size is
3836 * // known to be zero.
3837 * //
3838 * // TODO(haberman): is it important to distinguish these cases?
3839 * // If we had ssize_t as a type we could make -1 "unknown", but
3840 * // ssize_t is POSIX (not ANSI) and therefore less portable.
3841 * // In practice I suspect it won't be important to distinguish.
3842 * return closure;
3843 * }
3844 *
3845 * size_t str(MyClosure* closure, const MyHandlerData* d,
3846 * const char *str, size_t len) {
3847 * // Called for each buffer of string data; the multiple physical buffers
3848 * // are all part of the same logical string. The return value indicates
3849 * // how many bytes were consumed. If this number is less than "len",
3850 * // this will also indicate that processing should be halted for now,
3851 * // like returning false or UPB_BREAK from any other callback. If
3852 * // number is greater than "len", the excess bytes will be skipped over
3853 * // and not passed to the callback.
3854 * return len;
3855 * }
3856 *
3857 * bool endstr(MyClosure* c, const MyHandlerData* d) {
3858 * // Called when a string value ends. Return value indicates whether
3859 * // processing should continue.
3860 * return true;
3861 * }
3862 */
Chris Fallin91473dc2014-12-12 15:58:26 -08003863 bool SetStartStringHandler(const FieldDef* f, const StartStringHandler& h);
3864 bool SetStringHandler(const FieldDef* f, const StringHandler& h);
3865 bool SetEndStringHandler(const FieldDef* f, const EndFieldHandler& h);
3866
Josh Habermane8ed0212015-06-08 17:56:03 -07003867 /* Sets the startseq handler, which is defined as follows:
3868 *
3869 * MySubClosure *startseq(MyClosure* c, const MyHandlerData* d) {
3870 * // Called when a sequence (repeated field) begins. The returned
3871 * // pointer indicates the closure for the sequence (or UPB_BREAK
3872 * // to interrupt processing).
3873 * return closure;
3874 * }
3875 *
3876 * h->SetStartSequenceHandler(f, UpbBind(startseq, new MyHandlerData(...)));
3877 *
3878 * Returns "false" if "f" does not belong to this message or is not a
3879 * repeated field.
3880 */
Chris Fallin91473dc2014-12-12 15:58:26 -08003881 bool SetStartSequenceHandler(const FieldDef* f, const StartFieldHandler& h);
3882
Josh Habermane8ed0212015-06-08 17:56:03 -07003883 /* Sets the startsubmsg handler for the given field, which is defined as
3884 * follows:
3885 *
3886 * MySubClosure* startsubmsg(MyClosure* c, const MyHandlerData* d) {
3887 * // Called when a submessage begins. The returned pointer indicates the
3888 * // closure for the sequence (or UPB_BREAK to interrupt processing).
3889 * return closure;
3890 * }
3891 *
3892 * h->SetStartSubMessageHandler(f, UpbBind(startsubmsg,
3893 * new MyHandlerData(...)));
3894 *
3895 * Returns "false" if "f" does not belong to this message or is not a
3896 * submessage/group field.
3897 */
Chris Fallin91473dc2014-12-12 15:58:26 -08003898 bool SetStartSubMessageHandler(const FieldDef* f, const StartFieldHandler& h);
3899
Josh Habermane8ed0212015-06-08 17:56:03 -07003900 /* Sets the endsubmsg handler for the given field, which is defined as
3901 * follows:
3902 *
3903 * bool endsubmsg(MyClosure* c, const MyHandlerData* d) {
3904 * // Called when a submessage ends. Returns true to continue processing.
3905 * return true;
3906 * }
3907 *
3908 * Returns "false" if "f" does not belong to this message or is not a
3909 * submessage/group field.
3910 */
Chris Fallin91473dc2014-12-12 15:58:26 -08003911 bool SetEndSubMessageHandler(const FieldDef *f, const EndFieldHandler &h);
3912
Josh Habermane8ed0212015-06-08 17:56:03 -07003913 /* Starts the endsubseq handler for the given field, which is defined as
3914 * follows:
3915 *
3916 * bool endseq(MyClosure* c, const MyHandlerData* d) {
3917 * // Called when a sequence ends. Returns true continue processing.
3918 * return true;
3919 * }
3920 *
3921 * Returns "false" if "f" does not belong to this message or is not a
3922 * repeated field.
3923 */
Chris Fallin91473dc2014-12-12 15:58:26 -08003924 bool SetEndSequenceHandler(const FieldDef* f, const EndFieldHandler& h);
3925
Josh Habermane8ed0212015-06-08 17:56:03 -07003926 /* Sets or gets the object that specifies handlers for the given field, which
3927 * must be a submessage or group. Returns NULL if no handlers are set. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003928 bool SetSubHandlers(const FieldDef* f, const Handlers* sub);
3929 const Handlers* GetSubHandlers(const FieldDef* f) const;
3930
Josh Habermane8ed0212015-06-08 17:56:03 -07003931 /* Equivalent to GetSubHandlers, but takes the STARTSUBMSG selector for the
3932 * field. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003933 const Handlers* GetSubHandlers(Selector startsubmsg) const;
3934
Josh Habermane8ed0212015-06-08 17:56:03 -07003935 /* A selector refers to a specific field handler in the Handlers object
3936 * (for example: the STARTSUBMSG handler for field "field15").
3937 * On success, returns true and stores the selector in "s".
3938 * If the FieldDef or Type are invalid, returns false.
3939 * The returned selector is ONLY valid for Handlers whose MessageDef
3940 * contains this FieldDef. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003941 static bool GetSelector(const FieldDef* f, Type type, Selector* s);
3942
Josh Habermane8ed0212015-06-08 17:56:03 -07003943 /* Given a START selector of any kind, returns the corresponding END selector. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003944 static Selector GetEndSelector(Selector start_selector);
3945
Josh Habermane8ed0212015-06-08 17:56:03 -07003946 /* Returns the function pointer for this handler. It is the client's
3947 * responsibility to cast to the correct function type before calling it. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003948 GenericFunction* GetHandler(Selector selector);
3949
Josh Habermane8ed0212015-06-08 17:56:03 -07003950 /* Sets the given attributes to the attributes for this selector. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003951 bool GetAttributes(Selector selector, HandlerAttributes* attr);
3952
Josh Habermane8ed0212015-06-08 17:56:03 -07003953 /* Returns the handler data that was registered with this handler. */
Chris Fallin91473dc2014-12-12 15:58:26 -08003954 const void* GetHandlerData(Selector selector);
3955
Josh Habermane8ed0212015-06-08 17:56:03 -07003956 /* Could add any of the following functions as-needed, with some minor
3957 * implementation changes:
3958 *
3959 * const FieldDef* GetFieldDef(Selector selector);
3960 * static bool IsSequence(Selector selector); */
Chris Fallin91473dc2014-12-12 15:58:26 -08003961
3962 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07003963 UPB_DISALLOW_POD_OPS(Handlers, upb::Handlers)
Chris Fallin91473dc2014-12-12 15:58:26 -08003964
3965 friend UPB_INLINE GenericFunction *::upb_handlers_gethandler(
3966 const upb_handlers *h, upb_selector_t s);
3967 friend UPB_INLINE const void *::upb_handlers_gethandlerdata(
3968 const upb_handlers *h, upb_selector_t s);
Josh Habermane8ed0212015-06-08 17:56:03 -07003969#else
3970struct upb_handlers {
3971#endif
3972 upb_refcounted base;
Chris Fallin91473dc2014-12-12 15:58:26 -08003973
Chris Fallin91473dc2014-12-12 15:58:26 -08003974 const upb_msgdef *msg;
3975 const upb_handlers **sub;
3976 const void *top_closure_type;
3977 upb_inttable cleanup_;
Josh Habermane8ed0212015-06-08 17:56:03 -07003978 upb_status status_; /* Used only when mutable. */
3979 upb_handlers_tabent table[1]; /* Dynamically-sized field handler array. */
3980};
Chris Fallin91473dc2014-12-12 15:58:26 -08003981
3982#ifdef __cplusplus
3983
3984namespace upb {
3985
Josh Habermane8ed0212015-06-08 17:56:03 -07003986/* Convenience macros for creating a Handler object that is wrapped with a
3987 * type-safe wrapper function that converts the "void*" parameters/returns
3988 * of the underlying C API into nice C++ function.
3989 *
3990 * Sample usage:
3991 * void OnValue1(MyClosure* c, const MyHandlerData* d, int32_t val) {
3992 * // do stuff ...
3993 * }
3994 *
3995 * // Handler that doesn't need any data bound to it.
3996 * void OnValue2(MyClosure* c, int32_t val) {
3997 * // do stuff ...
3998 * }
3999 *
4000 * // Handler that returns bool so it can return failure if necessary.
4001 * bool OnValue3(MyClosure* c, int32_t val) {
4002 * // do stuff ...
4003 * return ok;
4004 * }
4005 *
4006 * // Member function handler.
4007 * class MyClosure {
4008 * public:
4009 * void OnValue(int32_t val) {
4010 * // do stuff ...
4011 * }
4012 * };
4013 *
4014 * // Takes ownership of the MyHandlerData.
4015 * handlers->SetInt32Handler(f1, UpbBind(OnValue1, new MyHandlerData(...)));
4016 * handlers->SetInt32Handler(f2, UpbMakeHandler(OnValue2));
4017 * handlers->SetInt32Handler(f1, UpbMakeHandler(OnValue3));
4018 * handlers->SetInt32Handler(f2, UpbMakeHandler(&MyClosure::OnValue));
4019 */
Chris Fallin91473dc2014-12-12 15:58:26 -08004020
4021#ifdef UPB_CXX11
4022
Josh Habermane8ed0212015-06-08 17:56:03 -07004023/* In C++11, the "template" disambiguator can appear even outside templates,
4024 * so all calls can safely use this pair of macros. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004025
4026#define UpbMakeHandler(f) upb::MatchFunc(f).template GetFunc<f>()
4027
Josh Habermane8ed0212015-06-08 17:56:03 -07004028/* We have to be careful to only evaluate "d" once. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004029#define UpbBind(f, d) upb::MatchFunc(f).template GetFunc<f>((d))
4030
4031#else
4032
Josh Habermane8ed0212015-06-08 17:56:03 -07004033/* Prior to C++11, the "template" disambiguator may only appear inside a
4034 * template, so the regular macro must not use "template" */
Chris Fallin91473dc2014-12-12 15:58:26 -08004035
4036#define UpbMakeHandler(f) upb::MatchFunc(f).GetFunc<f>()
4037
4038#define UpbBind(f, d) upb::MatchFunc(f).GetFunc<f>((d))
4039
Josh Habermane8ed0212015-06-08 17:56:03 -07004040#endif /* UPB_CXX11 */
Chris Fallin91473dc2014-12-12 15:58:26 -08004041
Josh Habermane8ed0212015-06-08 17:56:03 -07004042/* This macro must be used in C++98 for calls from inside a template. But we
4043 * define this variant in all cases; code that wants to be compatible with both
4044 * C++98 and C++11 should always use this macro when calling from a template. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004045#define UpbMakeHandlerT(f) upb::MatchFunc(f).template GetFunc<f>()
4046
Josh Habermane8ed0212015-06-08 17:56:03 -07004047/* We have to be careful to only evaluate "d" once. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004048#define UpbBindT(f, d) upb::MatchFunc(f).template GetFunc<f>((d))
4049
Josh Habermane8ed0212015-06-08 17:56:03 -07004050/* Handler: a struct that contains the (handler, data, deleter) tuple that is
4051 * used to register all handlers. Users can Make() these directly but it's
4052 * more convenient to use the UpbMakeHandler/UpbBind macros above. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004053template <class T> class Handler {
4054 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07004055 /* The underlying, handler function signature that upb uses internally. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004056 typedef T FuncPtr;
4057
Josh Habermane8ed0212015-06-08 17:56:03 -07004058 /* Intentionally implicit. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004059 template <class F> Handler(F func);
4060 ~Handler();
4061
4062 private:
4063 void AddCleanup(Handlers* h) const {
4064 if (cleanup_func_) {
4065 bool ok = h->AddCleanup(cleanup_data_, cleanup_func_);
4066 UPB_ASSERT_VAR(ok, ok);
4067 }
4068 }
4069
Josh Habermane8ed0212015-06-08 17:56:03 -07004070 UPB_DISALLOW_COPY_AND_ASSIGN(Handler)
Chris Fallin91473dc2014-12-12 15:58:26 -08004071 friend class Handlers;
4072 FuncPtr handler_;
4073 mutable HandlerAttributes attr_;
4074 mutable bool registered_;
4075 void *cleanup_data_;
4076 upb_handlerfree *cleanup_func_;
4077};
4078
Josh Habermane8ed0212015-06-08 17:56:03 -07004079} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08004080
Josh Habermane8ed0212015-06-08 17:56:03 -07004081#endif /* __cplusplus */
Chris Fallin91473dc2014-12-12 15:58:26 -08004082
4083UPB_BEGIN_EXTERN_C
4084
Josh Habermane8ed0212015-06-08 17:56:03 -07004085/* Native C API. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004086
Josh Habermane8ed0212015-06-08 17:56:03 -07004087/* Handler function typedefs. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004088typedef bool upb_startmsg_handlerfunc(void *c, const void*);
4089typedef bool upb_endmsg_handlerfunc(void *c, const void *, upb_status *status);
4090typedef void* upb_startfield_handlerfunc(void *c, const void *hd);
4091typedef bool upb_endfield_handlerfunc(void *c, const void *hd);
4092typedef bool upb_int32_handlerfunc(void *c, const void *hd, int32_t val);
4093typedef bool upb_int64_handlerfunc(void *c, const void *hd, int64_t val);
4094typedef bool upb_uint32_handlerfunc(void *c, const void *hd, uint32_t val);
4095typedef bool upb_uint64_handlerfunc(void *c, const void *hd, uint64_t val);
4096typedef bool upb_float_handlerfunc(void *c, const void *hd, float val);
4097typedef bool upb_double_handlerfunc(void *c, const void *hd, double val);
4098typedef bool upb_bool_handlerfunc(void *c, const void *hd, bool val);
4099typedef void *upb_startstr_handlerfunc(void *c, const void *hd,
4100 size_t size_hint);
4101typedef size_t upb_string_handlerfunc(void *c, const void *hd, const char *buf,
4102 size_t n, const upb_bufhandle* handle);
4103
Josh Habermane8ed0212015-06-08 17:56:03 -07004104/* upb_bufhandle */
Chris Fallin91473dc2014-12-12 15:58:26 -08004105size_t upb_bufhandle_objofs(const upb_bufhandle *h);
4106
Josh Habermane8ed0212015-06-08 17:56:03 -07004107/* upb_handlerattr */
Chris Fallin91473dc2014-12-12 15:58:26 -08004108void upb_handlerattr_init(upb_handlerattr *attr);
4109void upb_handlerattr_uninit(upb_handlerattr *attr);
4110
4111bool upb_handlerattr_sethandlerdata(upb_handlerattr *attr, const void *hd);
4112bool upb_handlerattr_setclosuretype(upb_handlerattr *attr, const void *type);
4113const void *upb_handlerattr_closuretype(const upb_handlerattr *attr);
4114bool upb_handlerattr_setreturnclosuretype(upb_handlerattr *attr,
4115 const void *type);
4116const void *upb_handlerattr_returnclosuretype(const upb_handlerattr *attr);
4117bool upb_handlerattr_setalwaysok(upb_handlerattr *attr, bool alwaysok);
4118bool upb_handlerattr_alwaysok(const upb_handlerattr *attr);
4119
4120UPB_INLINE const void *upb_handlerattr_handlerdata(
4121 const upb_handlerattr *attr) {
4122 return attr->handler_data_;
4123}
4124
Josh Habermane8ed0212015-06-08 17:56:03 -07004125/* upb_handlers */
Chris Fallin91473dc2014-12-12 15:58:26 -08004126typedef void upb_handlers_callback(const void *closure, upb_handlers *h);
4127upb_handlers *upb_handlers_new(const upb_msgdef *m,
4128 const void *owner);
4129const upb_handlers *upb_handlers_newfrozen(const upb_msgdef *m,
4130 const void *owner,
4131 upb_handlers_callback *callback,
4132 const void *closure);
Josh Habermane8ed0212015-06-08 17:56:03 -07004133
4134/* Include refcounted methods like upb_handlers_ref(). */
4135UPB_REFCOUNTED_CMETHODS(upb_handlers, upb_handlers_upcast)
Chris Fallin91473dc2014-12-12 15:58:26 -08004136
4137const upb_status *upb_handlers_status(upb_handlers *h);
4138void upb_handlers_clearerr(upb_handlers *h);
4139const upb_msgdef *upb_handlers_msgdef(const upb_handlers *h);
4140bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *hfree);
4141
4142bool upb_handlers_setstartmsg(upb_handlers *h, upb_startmsg_handlerfunc *func,
4143 upb_handlerattr *attr);
4144bool upb_handlers_setendmsg(upb_handlers *h, upb_endmsg_handlerfunc *func,
4145 upb_handlerattr *attr);
4146bool upb_handlers_setint32(upb_handlers *h, const upb_fielddef *f,
4147 upb_int32_handlerfunc *func, upb_handlerattr *attr);
4148bool upb_handlers_setint64(upb_handlers *h, const upb_fielddef *f,
4149 upb_int64_handlerfunc *func, upb_handlerattr *attr);
4150bool upb_handlers_setuint32(upb_handlers *h, const upb_fielddef *f,
4151 upb_uint32_handlerfunc *func,
4152 upb_handlerattr *attr);
4153bool upb_handlers_setuint64(upb_handlers *h, const upb_fielddef *f,
4154 upb_uint64_handlerfunc *func,
4155 upb_handlerattr *attr);
4156bool upb_handlers_setfloat(upb_handlers *h, const upb_fielddef *f,
4157 upb_float_handlerfunc *func, upb_handlerattr *attr);
4158bool upb_handlers_setdouble(upb_handlers *h, const upb_fielddef *f,
4159 upb_double_handlerfunc *func,
4160 upb_handlerattr *attr);
4161bool upb_handlers_setbool(upb_handlers *h, const upb_fielddef *f,
4162 upb_bool_handlerfunc *func,
4163 upb_handlerattr *attr);
4164bool upb_handlers_setstartstr(upb_handlers *h, const upb_fielddef *f,
4165 upb_startstr_handlerfunc *func,
4166 upb_handlerattr *attr);
4167bool upb_handlers_setstring(upb_handlers *h, const upb_fielddef *f,
4168 upb_string_handlerfunc *func,
4169 upb_handlerattr *attr);
4170bool upb_handlers_setendstr(upb_handlers *h, const upb_fielddef *f,
4171 upb_endfield_handlerfunc *func,
4172 upb_handlerattr *attr);
4173bool upb_handlers_setstartseq(upb_handlers *h, const upb_fielddef *f,
4174 upb_startfield_handlerfunc *func,
4175 upb_handlerattr *attr);
4176bool upb_handlers_setstartsubmsg(upb_handlers *h, const upb_fielddef *f,
4177 upb_startfield_handlerfunc *func,
4178 upb_handlerattr *attr);
4179bool upb_handlers_setendsubmsg(upb_handlers *h, const upb_fielddef *f,
4180 upb_endfield_handlerfunc *func,
4181 upb_handlerattr *attr);
4182bool upb_handlers_setendseq(upb_handlers *h, const upb_fielddef *f,
4183 upb_endfield_handlerfunc *func,
4184 upb_handlerattr *attr);
4185
4186bool upb_handlers_setsubhandlers(upb_handlers *h, const upb_fielddef *f,
4187 const upb_handlers *sub);
4188const upb_handlers *upb_handlers_getsubhandlers(const upb_handlers *h,
4189 const upb_fielddef *f);
4190const upb_handlers *upb_handlers_getsubhandlers_sel(const upb_handlers *h,
4191 upb_selector_t sel);
4192
4193UPB_INLINE upb_func *upb_handlers_gethandler(const upb_handlers *h,
4194 upb_selector_t s) {
4195 return (upb_func *)h->table[s].func;
4196}
4197
4198bool upb_handlers_getattr(const upb_handlers *h, upb_selector_t s,
4199 upb_handlerattr *attr);
4200
4201UPB_INLINE const void *upb_handlers_gethandlerdata(const upb_handlers *h,
4202 upb_selector_t s) {
4203 return upb_handlerattr_handlerdata(&h->table[s].attr);
4204}
4205
Josh Habermane8ed0212015-06-08 17:56:03 -07004206#ifdef __cplusplus
4207
4208/* Handler types for single fields.
4209 * Right now we only have one for TYPE_BYTES but ones for other types
4210 * should follow.
4211 *
4212 * These follow the same handlers protocol for fields of a message. */
4213class upb::BytesHandler {
Chris Fallin91473dc2014-12-12 15:58:26 -08004214 public:
4215 BytesHandler();
4216 ~BytesHandler();
Josh Habermane8ed0212015-06-08 17:56:03 -07004217#else
4218struct upb_byteshandler {
4219#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08004220 upb_handlers_tabent table[3];
Josh Habermane8ed0212015-06-08 17:56:03 -07004221};
Chris Fallin91473dc2014-12-12 15:58:26 -08004222
4223void upb_byteshandler_init(upb_byteshandler *h);
Chris Fallin91473dc2014-12-12 15:58:26 -08004224
Josh Habermane8ed0212015-06-08 17:56:03 -07004225/* Caller must ensure that "d" outlives the handlers.
4226 * TODO(haberman): should this have a "freeze" operation? It's not necessary
4227 * for memory management, but could be useful to force immutability and provide
4228 * a convenient moment to verify that all registration succeeded. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004229bool upb_byteshandler_setstartstr(upb_byteshandler *h,
4230 upb_startstr_handlerfunc *func, void *d);
4231bool upb_byteshandler_setstring(upb_byteshandler *h,
4232 upb_string_handlerfunc *func, void *d);
4233bool upb_byteshandler_setendstr(upb_byteshandler *h,
4234 upb_endfield_handlerfunc *func, void *d);
4235
Josh Habermane8ed0212015-06-08 17:56:03 -07004236/* "Static" methods */
Chris Fallin91473dc2014-12-12 15:58:26 -08004237bool upb_handlers_freeze(upb_handlers *const *handlers, int n, upb_status *s);
4238upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f);
4239bool upb_handlers_getselector(const upb_fielddef *f, upb_handlertype_t type,
4240 upb_selector_t *s);
4241UPB_INLINE upb_selector_t upb_handlers_getendselector(upb_selector_t start) {
4242 return start + 1;
4243}
4244
Josh Habermane8ed0212015-06-08 17:56:03 -07004245/* Internal-only. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004246uint32_t upb_handlers_selectorbaseoffset(const upb_fielddef *f);
4247uint32_t upb_handlers_selectorcount(const upb_fielddef *f);
4248
4249UPB_END_EXTERN_C
4250
4251/*
Josh Haberman181c7f22015-07-15 11:05:10 -07004252** Inline definitions for handlers.h, which are particularly long and a bit
4253** tricky.
4254*/
Chris Fallin91473dc2014-12-12 15:58:26 -08004255
4256#ifndef UPB_HANDLERS_INL_H_
4257#define UPB_HANDLERS_INL_H_
4258
4259#include <limits.h>
4260
Josh Habermane8ed0212015-06-08 17:56:03 -07004261/* C inline methods. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004262
Josh Habermane8ed0212015-06-08 17:56:03 -07004263/* upb_bufhandle */
Chris Fallin91473dc2014-12-12 15:58:26 -08004264UPB_INLINE void upb_bufhandle_init(upb_bufhandle *h) {
4265 h->obj_ = NULL;
4266 h->objtype_ = NULL;
4267 h->buf_ = NULL;
4268 h->objofs_ = 0;
4269}
4270UPB_INLINE void upb_bufhandle_uninit(upb_bufhandle *h) {
4271 UPB_UNUSED(h);
4272}
4273UPB_INLINE void upb_bufhandle_setobj(upb_bufhandle *h, const void *obj,
4274 const void *type) {
4275 h->obj_ = obj;
4276 h->objtype_ = type;
4277}
4278UPB_INLINE void upb_bufhandle_setbuf(upb_bufhandle *h, const char *buf,
4279 size_t ofs) {
4280 h->buf_ = buf;
4281 h->objofs_ = ofs;
4282}
4283UPB_INLINE const void *upb_bufhandle_obj(const upb_bufhandle *h) {
4284 return h->obj_;
4285}
4286UPB_INLINE const void *upb_bufhandle_objtype(const upb_bufhandle *h) {
4287 return h->objtype_;
4288}
4289UPB_INLINE const char *upb_bufhandle_buf(const upb_bufhandle *h) {
4290 return h->buf_;
4291}
4292
4293
4294#ifdef __cplusplus
4295
Josh Habermane8ed0212015-06-08 17:56:03 -07004296/* Type detection and typedefs for integer types.
4297 * For platforms where there are multiple 32-bit or 64-bit types, we need to be
4298 * able to enumerate them so we can properly create overloads for all variants.
4299 *
4300 * If any platform existed where there were three integer types with the same
4301 * size, this would have to become more complicated. For example, short, int,
4302 * and long could all be 32-bits. Even more diabolically, short, int, long,
4303 * and long long could all be 64 bits and still be standard-compliant.
4304 * However, few platforms are this strange, and it's unlikely that upb will be
4305 * used on the strangest ones. */
4306
4307/* Can't count on stdint.h limits like INT32_MAX, because in C++ these are
4308 * only defined when __STDC_LIMIT_MACROS are defined before the *first* include
4309 * of stdint.h. We can't guarantee that someone else didn't include these first
4310 * without defining __STDC_LIMIT_MACROS. */
4311#define UPB_INT32_MAX 0x7fffffffLL
4312#define UPB_INT32_MIN (-UPB_INT32_MAX - 1)
4313#define UPB_INT64_MAX 0x7fffffffffffffffLL
4314#define UPB_INT64_MIN (-UPB_INT64_MAX - 1)
4315
4316#if INT_MAX == UPB_INT32_MAX && INT_MIN == UPB_INT32_MIN
4317#define UPB_INT_IS_32BITS 1
4318#endif
4319
4320#if LONG_MAX == UPB_INT32_MAX && LONG_MIN == UPB_INT32_MIN
4321#define UPB_LONG_IS_32BITS 1
4322#endif
4323
4324#if LONG_MAX == UPB_INT64_MAX && LONG_MIN == UPB_INT64_MIN
4325#define UPB_LONG_IS_64BITS 1
4326#endif
4327
4328#if LLONG_MAX == UPB_INT64_MAX && LLONG_MIN == UPB_INT64_MIN
4329#define UPB_LLONG_IS_64BITS 1
4330#endif
4331
4332/* We use macros instead of typedefs so we can undefine them later and avoid
4333 * leaking them outside this header file. */
4334#if UPB_INT_IS_32BITS
4335#define UPB_INT32_T int
4336#define UPB_UINT32_T unsigned int
4337
4338#if UPB_LONG_IS_32BITS
4339#define UPB_TWO_32BIT_TYPES 1
4340#define UPB_INT32ALT_T long
4341#define UPB_UINT32ALT_T unsigned long
4342#endif /* UPB_LONG_IS_32BITS */
4343
4344#elif UPB_LONG_IS_32BITS /* && !UPB_INT_IS_32BITS */
4345#define UPB_INT32_T long
4346#define UPB_UINT32_T unsigned long
4347#endif /* UPB_INT_IS_32BITS */
4348
4349
4350#if UPB_LONG_IS_64BITS
4351#define UPB_INT64_T long
4352#define UPB_UINT64_T unsigned long
4353
4354#if UPB_LLONG_IS_64BITS
4355#define UPB_TWO_64BIT_TYPES 1
4356#define UPB_INT64ALT_T long long
4357#define UPB_UINT64ALT_T unsigned long long
4358#endif /* UPB_LLONG_IS_64BITS */
4359
4360#elif UPB_LLONG_IS_64BITS /* && !UPB_LONG_IS_64BITS */
4361#define UPB_INT64_T long long
4362#define UPB_UINT64_T unsigned long long
4363#endif /* UPB_LONG_IS_64BITS */
4364
4365#undef UPB_INT32_MAX
4366#undef UPB_INT32_MIN
4367#undef UPB_INT64_MAX
4368#undef UPB_INT64_MIN
4369#undef UPB_INT_IS_32BITS
4370#undef UPB_LONG_IS_32BITS
4371#undef UPB_LONG_IS_64BITS
4372#undef UPB_LLONG_IS_64BITS
4373
4374
Chris Fallin91473dc2014-12-12 15:58:26 -08004375namespace upb {
4376
4377typedef void CleanupFunc(void *ptr);
4378
Josh Habermane8ed0212015-06-08 17:56:03 -07004379/* Template to remove "const" from "const T*" and just return "T*".
4380 *
4381 * We define a nonsense default because otherwise it will fail to instantiate as
4382 * a function parameter type even in cases where we don't expect any caller to
4383 * actually match the overload. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004384class CouldntRemoveConst {};
4385template <class T> struct remove_constptr { typedef CouldntRemoveConst type; };
4386template <class T> struct remove_constptr<const T *> { typedef T *type; };
4387
Josh Habermane8ed0212015-06-08 17:56:03 -07004388/* Template that we use below to remove a template specialization from
4389 * consideration if it matches a specific type. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004390template <class T, class U> struct disable_if_same { typedef void Type; };
4391template <class T> struct disable_if_same<T, T> {};
4392
4393template <class T> void DeletePointer(void *p) { delete static_cast<T>(p); }
4394
4395template <class T1, class T2>
Chris Fallind3262772015-05-14 18:24:26 -07004396struct FirstUnlessVoidOrBool {
Chris Fallin91473dc2014-12-12 15:58:26 -08004397 typedef T1 value;
4398};
4399
4400template <class T2>
Chris Fallind3262772015-05-14 18:24:26 -07004401struct FirstUnlessVoidOrBool<void, T2> {
4402 typedef T2 value;
4403};
4404
4405template <class T2>
4406struct FirstUnlessVoidOrBool<bool, T2> {
Chris Fallin91473dc2014-12-12 15:58:26 -08004407 typedef T2 value;
4408};
4409
4410template<class T, class U>
4411struct is_same {
4412 static bool value;
4413};
4414
4415template<class T>
4416struct is_same<T, T> {
4417 static bool value;
4418};
4419
4420template<class T, class U>
4421bool is_same<T, U>::value = false;
4422
4423template<class T>
4424bool is_same<T, T>::value = true;
4425
Josh Habermane8ed0212015-06-08 17:56:03 -07004426/* FuncInfo *******************************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -08004427
Josh Habermane8ed0212015-06-08 17:56:03 -07004428/* Info about the user's original, pre-wrapped function. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004429template <class C, class R = void>
4430struct FuncInfo {
Josh Habermane8ed0212015-06-08 17:56:03 -07004431 /* The type of the closure that the function takes (its first param). */
Chris Fallin91473dc2014-12-12 15:58:26 -08004432 typedef C Closure;
4433
Josh Habermane8ed0212015-06-08 17:56:03 -07004434 /* The return type. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004435 typedef R Return;
4436};
4437
Josh Habermane8ed0212015-06-08 17:56:03 -07004438/* Func ***********************************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -08004439
Josh Habermane8ed0212015-06-08 17:56:03 -07004440/* Func1, Func2, Func3: Template classes representing a function and its
4441 * signature.
4442 *
4443 * Since the function is a template parameter, calling the function can be
4444 * inlined at compile-time and does not require a function pointer at runtime.
4445 * These functions are not bound to a handler data so have no data or cleanup
4446 * handler. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004447struct UnboundFunc {
4448 CleanupFunc *GetCleanup() { return NULL; }
4449 void *GetData() { return NULL; }
4450};
4451
4452template <class R, class P1, R F(P1), class I>
4453struct Func1 : public UnboundFunc {
4454 typedef R Return;
4455 typedef I FuncInfo;
4456 static R Call(P1 p1) { return F(p1); }
4457};
4458
4459template <class R, class P1, class P2, R F(P1, P2), class I>
4460struct Func2 : public UnboundFunc {
4461 typedef R Return;
4462 typedef I FuncInfo;
4463 static R Call(P1 p1, P2 p2) { return F(p1, p2); }
4464};
4465
4466template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I>
4467struct Func3 : public UnboundFunc {
4468 typedef R Return;
4469 typedef I FuncInfo;
4470 static R Call(P1 p1, P2 p2, P3 p3) { return F(p1, p2, p3); }
4471};
4472
4473template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
4474 class I>
4475struct Func4 : public UnboundFunc {
4476 typedef R Return;
4477 typedef I FuncInfo;
4478 static R Call(P1 p1, P2 p2, P3 p3, P4 p4) { return F(p1, p2, p3, p4); }
4479};
4480
4481template <class R, class P1, class P2, class P3, class P4, class P5,
4482 R F(P1, P2, P3, P4, P5), class I>
4483struct Func5 : public UnboundFunc {
4484 typedef R Return;
4485 typedef I FuncInfo;
4486 static R Call(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
4487 return F(p1, p2, p3, p4, p5);
4488 }
4489};
4490
Josh Habermane8ed0212015-06-08 17:56:03 -07004491/* BoundFunc ******************************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -08004492
Josh Habermane8ed0212015-06-08 17:56:03 -07004493/* BoundFunc2, BoundFunc3: Like Func2/Func3 except also contains a value that
4494 * shall be bound to the function's second parameter.
4495 *
4496 * Note that the second parameter is a const pointer, but our stored bound value
4497 * is non-const so we can free it when the handlers are destroyed. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004498template <class T>
4499struct BoundFunc {
4500 typedef typename remove_constptr<T>::type MutableP2;
4501 explicit BoundFunc(MutableP2 data_) : data(data_) {}
4502 CleanupFunc *GetCleanup() { return &DeletePointer<MutableP2>; }
4503 MutableP2 GetData() { return data; }
4504 MutableP2 data;
4505};
4506
4507template <class R, class P1, class P2, R F(P1, P2), class I>
4508struct BoundFunc2 : public BoundFunc<P2> {
4509 typedef BoundFunc<P2> Base;
4510 typedef I FuncInfo;
4511 explicit BoundFunc2(typename Base::MutableP2 arg) : Base(arg) {}
4512};
4513
4514template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I>
4515struct BoundFunc3 : public BoundFunc<P2> {
4516 typedef BoundFunc<P2> Base;
4517 typedef I FuncInfo;
4518 explicit BoundFunc3(typename Base::MutableP2 arg) : Base(arg) {}
4519};
4520
4521template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
4522 class I>
4523struct BoundFunc4 : public BoundFunc<P2> {
4524 typedef BoundFunc<P2> Base;
4525 typedef I FuncInfo;
4526 explicit BoundFunc4(typename Base::MutableP2 arg) : Base(arg) {}
4527};
4528
4529template <class R, class P1, class P2, class P3, class P4, class P5,
4530 R F(P1, P2, P3, P4, P5), class I>
4531struct BoundFunc5 : public BoundFunc<P2> {
4532 typedef BoundFunc<P2> Base;
4533 typedef I FuncInfo;
4534 explicit BoundFunc5(typename Base::MutableP2 arg) : Base(arg) {}
4535};
4536
Josh Habermane8ed0212015-06-08 17:56:03 -07004537/* FuncSig ********************************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -08004538
Josh Habermane8ed0212015-06-08 17:56:03 -07004539/* FuncSig1, FuncSig2, FuncSig3: template classes reflecting a function
4540 * *signature*, but without a specific function attached.
4541 *
4542 * These classes contain member functions that can be invoked with a
4543 * specific function to return a Func/BoundFunc class. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004544template <class R, class P1>
4545struct FuncSig1 {
4546 template <R F(P1)>
4547 Func1<R, P1, F, FuncInfo<P1, R> > GetFunc() {
4548 return Func1<R, P1, F, FuncInfo<P1, R> >();
4549 }
4550};
4551
4552template <class R, class P1, class P2>
4553struct FuncSig2 {
4554 template <R F(P1, P2)>
4555 Func2<R, P1, P2, F, FuncInfo<P1, R> > GetFunc() {
4556 return Func2<R, P1, P2, F, FuncInfo<P1, R> >();
4557 }
4558
4559 template <R F(P1, P2)>
4560 BoundFunc2<R, P1, P2, F, FuncInfo<P1, R> > GetFunc(
4561 typename remove_constptr<P2>::type param2) {
4562 return BoundFunc2<R, P1, P2, F, FuncInfo<P1, R> >(param2);
4563 }
4564};
4565
4566template <class R, class P1, class P2, class P3>
4567struct FuncSig3 {
4568 template <R F(P1, P2, P3)>
4569 Func3<R, P1, P2, P3, F, FuncInfo<P1, R> > GetFunc() {
4570 return Func3<R, P1, P2, P3, F, FuncInfo<P1, R> >();
4571 }
4572
4573 template <R F(P1, P2, P3)>
4574 BoundFunc3<R, P1, P2, P3, F, FuncInfo<P1, R> > GetFunc(
4575 typename remove_constptr<P2>::type param2) {
4576 return BoundFunc3<R, P1, P2, P3, F, FuncInfo<P1, R> >(param2);
4577 }
4578};
4579
4580template <class R, class P1, class P2, class P3, class P4>
4581struct FuncSig4 {
4582 template <R F(P1, P2, P3, P4)>
4583 Func4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> > GetFunc() {
4584 return Func4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> >();
4585 }
4586
4587 template <R F(P1, P2, P3, P4)>
4588 BoundFunc4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> > GetFunc(
4589 typename remove_constptr<P2>::type param2) {
4590 return BoundFunc4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> >(param2);
4591 }
4592};
4593
4594template <class R, class P1, class P2, class P3, class P4, class P5>
4595struct FuncSig5 {
4596 template <R F(P1, P2, P3, P4, P5)>
4597 Func5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> > GetFunc() {
4598 return Func5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> >();
4599 }
4600
4601 template <R F(P1, P2, P3, P4, P5)>
4602 BoundFunc5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> > GetFunc(
4603 typename remove_constptr<P2>::type param2) {
4604 return BoundFunc5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> >(param2);
4605 }
4606};
4607
Josh Habermane8ed0212015-06-08 17:56:03 -07004608/* Overloaded template function that can construct the appropriate FuncSig*
4609 * class given a function pointer by deducing the template parameters. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004610template <class R, class P1>
4611inline FuncSig1<R, P1> MatchFunc(R (*f)(P1)) {
Josh Habermane8ed0212015-06-08 17:56:03 -07004612 UPB_UNUSED(f); /* Only used for template parameter deduction. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004613 return FuncSig1<R, P1>();
4614}
4615
4616template <class R, class P1, class P2>
4617inline FuncSig2<R, P1, P2> MatchFunc(R (*f)(P1, P2)) {
Josh Habermane8ed0212015-06-08 17:56:03 -07004618 UPB_UNUSED(f); /* Only used for template parameter deduction. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004619 return FuncSig2<R, P1, P2>();
4620}
4621
4622template <class R, class P1, class P2, class P3>
4623inline FuncSig3<R, P1, P2, P3> MatchFunc(R (*f)(P1, P2, P3)) {
Josh Habermane8ed0212015-06-08 17:56:03 -07004624 UPB_UNUSED(f); /* Only used for template parameter deduction. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004625 return FuncSig3<R, P1, P2, P3>();
4626}
4627
4628template <class R, class P1, class P2, class P3, class P4>
4629inline FuncSig4<R, P1, P2, P3, P4> MatchFunc(R (*f)(P1, P2, P3, P4)) {
Josh Habermane8ed0212015-06-08 17:56:03 -07004630 UPB_UNUSED(f); /* Only used for template parameter deduction. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004631 return FuncSig4<R, P1, P2, P3, P4>();
4632}
4633
4634template <class R, class P1, class P2, class P3, class P4, class P5>
4635inline FuncSig5<R, P1, P2, P3, P4, P5> MatchFunc(R (*f)(P1, P2, P3, P4, P5)) {
Josh Habermane8ed0212015-06-08 17:56:03 -07004636 UPB_UNUSED(f); /* Only used for template parameter deduction. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004637 return FuncSig5<R, P1, P2, P3, P4, P5>();
4638}
4639
Josh Habermane8ed0212015-06-08 17:56:03 -07004640/* MethodSig ******************************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -08004641
Josh Habermane8ed0212015-06-08 17:56:03 -07004642/* CallMethod*: a function template that calls a given method. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004643template <class R, class C, R (C::*F)()>
4644R CallMethod0(C *obj) {
4645 return ((*obj).*F)();
4646}
4647
4648template <class R, class C, class P1, R (C::*F)(P1)>
4649R CallMethod1(C *obj, P1 arg1) {
4650 return ((*obj).*F)(arg1);
4651}
4652
4653template <class R, class C, class P1, class P2, R (C::*F)(P1, P2)>
4654R CallMethod2(C *obj, P1 arg1, P2 arg2) {
4655 return ((*obj).*F)(arg1, arg2);
4656}
4657
4658template <class R, class C, class P1, class P2, class P3, R (C::*F)(P1, P2, P3)>
4659R CallMethod3(C *obj, P1 arg1, P2 arg2, P3 arg3) {
4660 return ((*obj).*F)(arg1, arg2, arg3);
4661}
4662
4663template <class R, class C, class P1, class P2, class P3, class P4,
4664 R (C::*F)(P1, P2, P3, P4)>
4665R CallMethod4(C *obj, P1 arg1, P2 arg2, P3 arg3, P4 arg4) {
4666 return ((*obj).*F)(arg1, arg2, arg3, arg4);
4667}
4668
Josh Habermane8ed0212015-06-08 17:56:03 -07004669/* MethodSig: like FuncSig, but for member functions.
4670 *
4671 * GetFunc() returns a normal FuncN object, so after calling GetFunc() no
4672 * more logic is required to special-case methods. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004673template <class R, class C>
4674struct MethodSig0 {
4675 template <R (C::*F)()>
4676 Func1<R, C *, CallMethod0<R, C, F>, FuncInfo<C *, R> > GetFunc() {
4677 return Func1<R, C *, CallMethod0<R, C, F>, FuncInfo<C *, R> >();
4678 }
4679};
4680
4681template <class R, class C, class P1>
4682struct MethodSig1 {
4683 template <R (C::*F)(P1)>
4684 Func2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> > GetFunc() {
4685 return Func2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> >();
4686 }
4687
4688 template <R (C::*F)(P1)>
4689 BoundFunc2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> > GetFunc(
4690 typename remove_constptr<P1>::type param1) {
4691 return BoundFunc2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> >(
4692 param1);
4693 }
4694};
4695
4696template <class R, class C, class P1, class P2>
4697struct MethodSig2 {
4698 template <R (C::*F)(P1, P2)>
4699 Func3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>, FuncInfo<C *, R> >
4700 GetFunc() {
4701 return Func3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>,
4702 FuncInfo<C *, R> >();
4703 }
4704
4705 template <R (C::*F)(P1, P2)>
4706 BoundFunc3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>, FuncInfo<C *, R> >
4707 GetFunc(typename remove_constptr<P1>::type param1) {
4708 return BoundFunc3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>,
4709 FuncInfo<C *, R> >(param1);
4710 }
4711};
4712
4713template <class R, class C, class P1, class P2, class P3>
4714struct MethodSig3 {
4715 template <R (C::*F)(P1, P2, P3)>
4716 Func4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>, FuncInfo<C *, R> >
4717 GetFunc() {
4718 return Func4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
4719 FuncInfo<C *, R> >();
4720 }
4721
4722 template <R (C::*F)(P1, P2, P3)>
4723 BoundFunc4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
4724 FuncInfo<C *, R> >
4725 GetFunc(typename remove_constptr<P1>::type param1) {
4726 return BoundFunc4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
4727 FuncInfo<C *, R> >(param1);
4728 }
4729};
4730
4731template <class R, class C, class P1, class P2, class P3, class P4>
4732struct MethodSig4 {
4733 template <R (C::*F)(P1, P2, P3, P4)>
4734 Func5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
4735 FuncInfo<C *, R> >
4736 GetFunc() {
4737 return Func5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
4738 FuncInfo<C *, R> >();
4739 }
4740
4741 template <R (C::*F)(P1, P2, P3, P4)>
4742 BoundFunc5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
4743 FuncInfo<C *, R> >
4744 GetFunc(typename remove_constptr<P1>::type param1) {
4745 return BoundFunc5<R, C *, P1, P2, P3, P4,
4746 CallMethod4<R, C, P1, P2, P3, P4, F>, FuncInfo<C *, R> >(
4747 param1);
4748 }
4749};
4750
4751template <class R, class C>
4752inline MethodSig0<R, C> MatchFunc(R (C::*f)()) {
Josh Habermane8ed0212015-06-08 17:56:03 -07004753 UPB_UNUSED(f); /* Only used for template parameter deduction. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004754 return MethodSig0<R, C>();
4755}
4756
4757template <class R, class C, class P1>
4758inline MethodSig1<R, C, P1> MatchFunc(R (C::*f)(P1)) {
Josh Habermane8ed0212015-06-08 17:56:03 -07004759 UPB_UNUSED(f); /* Only used for template parameter deduction. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004760 return MethodSig1<R, C, P1>();
4761}
4762
4763template <class R, class C, class P1, class P2>
4764inline MethodSig2<R, C, P1, P2> MatchFunc(R (C::*f)(P1, P2)) {
Josh Habermane8ed0212015-06-08 17:56:03 -07004765 UPB_UNUSED(f); /* Only used for template parameter deduction. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004766 return MethodSig2<R, C, P1, P2>();
4767}
4768
4769template <class R, class C, class P1, class P2, class P3>
4770inline MethodSig3<R, C, P1, P2, P3> MatchFunc(R (C::*f)(P1, P2, P3)) {
Josh Habermane8ed0212015-06-08 17:56:03 -07004771 UPB_UNUSED(f); /* Only used for template parameter deduction. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004772 return MethodSig3<R, C, P1, P2, P3>();
4773}
4774
4775template <class R, class C, class P1, class P2, class P3, class P4>
4776inline MethodSig4<R, C, P1, P2, P3, P4> MatchFunc(R (C::*f)(P1, P2, P3, P4)) {
Josh Habermane8ed0212015-06-08 17:56:03 -07004777 UPB_UNUSED(f); /* Only used for template parameter deduction. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004778 return MethodSig4<R, C, P1, P2, P3, P4>();
4779}
4780
Josh Habermane8ed0212015-06-08 17:56:03 -07004781/* MaybeWrapReturn ************************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -08004782
Josh Habermane8ed0212015-06-08 17:56:03 -07004783/* Template class that attempts to wrap the return value of the function so it
4784 * matches the expected type. There are two main adjustments it may make:
4785 *
4786 * 1. If the function returns void, make it return the expected type and with
4787 * a value that always indicates success.
4788 * 2. If the function returns bool, make it return the expected type with a
4789 * value that indicates success or failure.
4790 *
4791 * The "expected type" for return is:
4792 * 1. void* for start handlers. If the closure parameter has a different type
4793 * we will cast it to void* for the return in the success case.
4794 * 2. size_t for string buffer handlers.
4795 * 3. bool for everything else. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004796
Josh Habermane8ed0212015-06-08 17:56:03 -07004797/* Template parameters are FuncN type and desired return type. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004798template <class F, class R, class Enable = void>
4799struct MaybeWrapReturn;
4800
Josh Habermane8ed0212015-06-08 17:56:03 -07004801/* If the return type matches, return the given function unwrapped. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004802template <class F>
4803struct MaybeWrapReturn<F, typename F::Return> {
4804 typedef F Func;
4805};
4806
Josh Habermane8ed0212015-06-08 17:56:03 -07004807/* Function wrapper that munges the return value from void to (bool)true. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004808template <class P1, class P2, void F(P1, P2)>
4809bool ReturnTrue2(P1 p1, P2 p2) {
4810 F(p1, p2);
4811 return true;
4812}
4813
4814template <class P1, class P2, class P3, void F(P1, P2, P3)>
4815bool ReturnTrue3(P1 p1, P2 p2, P3 p3) {
4816 F(p1, p2, p3);
4817 return true;
4818}
4819
Josh Habermane8ed0212015-06-08 17:56:03 -07004820/* Function wrapper that munges the return value from void to (void*)arg1 */
Chris Fallin91473dc2014-12-12 15:58:26 -08004821template <class P1, class P2, void F(P1, P2)>
4822void *ReturnClosure2(P1 p1, P2 p2) {
4823 F(p1, p2);
4824 return p1;
4825}
4826
4827template <class P1, class P2, class P3, void F(P1, P2, P3)>
4828void *ReturnClosure3(P1 p1, P2 p2, P3 p3) {
4829 F(p1, p2, p3);
4830 return p1;
4831}
4832
Josh Habermane8ed0212015-06-08 17:56:03 -07004833/* Function wrapper that munges the return value from R to void*. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004834template <class R, class P1, class P2, R F(P1, P2)>
4835void *CastReturnToVoidPtr2(P1 p1, P2 p2) {
4836 return F(p1, p2);
4837}
4838
4839template <class R, class P1, class P2, class P3, R F(P1, P2, P3)>
4840void *CastReturnToVoidPtr3(P1 p1, P2 p2, P3 p3) {
4841 return F(p1, p2, p3);
4842}
4843
Josh Habermane8ed0212015-06-08 17:56:03 -07004844/* Function wrapper that munges the return value from bool to void*. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004845template <class P1, class P2, bool F(P1, P2)>
4846void *ReturnClosureOrBreak2(P1 p1, P2 p2) {
4847 return F(p1, p2) ? p1 : UPB_BREAK;
4848}
4849
4850template <class P1, class P2, class P3, bool F(P1, P2, P3)>
4851void *ReturnClosureOrBreak3(P1 p1, P2 p2, P3 p3) {
4852 return F(p1, p2, p3) ? p1 : UPB_BREAK;
4853}
4854
Josh Habermane8ed0212015-06-08 17:56:03 -07004855/* For the string callback, which takes five params, returns the size param. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004856template <class P1, class P2,
4857 void F(P1, P2, const char *, size_t, const BufferHandle *)>
4858size_t ReturnStringLen(P1 p1, P2 p2, const char *p3, size_t p4,
4859 const BufferHandle *p5) {
4860 F(p1, p2, p3, p4, p5);
4861 return p4;
4862}
4863
Josh Habermane8ed0212015-06-08 17:56:03 -07004864/* For the string callback, which takes five params, returns the size param or
4865 * zero. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004866template <class P1, class P2,
4867 bool F(P1, P2, const char *, size_t, const BufferHandle *)>
4868size_t ReturnNOr0(P1 p1, P2 p2, const char *p3, size_t p4,
4869 const BufferHandle *p5) {
4870 return F(p1, p2, p3, p4, p5) ? p4 : 0;
4871}
4872
Josh Habermane8ed0212015-06-08 17:56:03 -07004873/* If we have a function returning void but want a function returning bool, wrap
4874 * it in a function that returns true. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004875template <class P1, class P2, void F(P1, P2), class I>
4876struct MaybeWrapReturn<Func2<void, P1, P2, F, I>, bool> {
4877 typedef Func2<bool, P1, P2, ReturnTrue2<P1, P2, F>, I> Func;
4878};
4879
4880template <class P1, class P2, class P3, void F(P1, P2, P3), class I>
4881struct MaybeWrapReturn<Func3<void, P1, P2, P3, F, I>, bool> {
4882 typedef Func3<bool, P1, P2, P3, ReturnTrue3<P1, P2, P3, F>, I> Func;
4883};
4884
Josh Habermane8ed0212015-06-08 17:56:03 -07004885/* If our function returns void but we want one returning void*, wrap it in a
4886 * function that returns the first argument. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004887template <class P1, class P2, void F(P1, P2), class I>
4888struct MaybeWrapReturn<Func2<void, P1, P2, F, I>, void *> {
4889 typedef Func2<void *, P1, P2, ReturnClosure2<P1, P2, F>, I> Func;
4890};
4891
4892template <class P1, class P2, class P3, void F(P1, P2, P3), class I>
4893struct MaybeWrapReturn<Func3<void, P1, P2, P3, F, I>, void *> {
4894 typedef Func3<void *, P1, P2, P3, ReturnClosure3<P1, P2, P3, F>, I> Func;
4895};
4896
Josh Habermane8ed0212015-06-08 17:56:03 -07004897/* If our function returns R* but we want one returning void*, wrap it in a
4898 * function that casts to void*. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004899template <class R, class P1, class P2, R *F(P1, P2), class I>
4900struct MaybeWrapReturn<Func2<R *, P1, P2, F, I>, void *,
4901 typename disable_if_same<R *, void *>::Type> {
4902 typedef Func2<void *, P1, P2, CastReturnToVoidPtr2<R *, P1, P2, F>, I> Func;
4903};
4904
4905template <class R, class P1, class P2, class P3, R *F(P1, P2, P3), class I>
4906struct MaybeWrapReturn<Func3<R *, P1, P2, P3, F, I>, void *,
4907 typename disable_if_same<R *, void *>::Type> {
4908 typedef Func3<void *, P1, P2, P3, CastReturnToVoidPtr3<R *, P1, P2, P3, F>, I>
4909 Func;
4910};
4911
Josh Habermane8ed0212015-06-08 17:56:03 -07004912/* If our function returns bool but we want one returning void*, wrap it in a
4913 * function that returns either the first param or UPB_BREAK. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004914template <class P1, class P2, bool F(P1, P2), class I>
4915struct MaybeWrapReturn<Func2<bool, P1, P2, F, I>, void *> {
4916 typedef Func2<void *, P1, P2, ReturnClosureOrBreak2<P1, P2, F>, I> Func;
4917};
4918
4919template <class P1, class P2, class P3, bool F(P1, P2, P3), class I>
4920struct MaybeWrapReturn<Func3<bool, P1, P2, P3, F, I>, void *> {
4921 typedef Func3<void *, P1, P2, P3, ReturnClosureOrBreak3<P1, P2, P3, F>, I>
4922 Func;
4923};
4924
Josh Habermane8ed0212015-06-08 17:56:03 -07004925/* If our function returns void but we want one returning size_t, wrap it in a
4926 * function that returns the size argument. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004927template <class P1, class P2,
4928 void F(P1, P2, const char *, size_t, const BufferHandle *), class I>
4929struct MaybeWrapReturn<
4930 Func5<void, P1, P2, const char *, size_t, const BufferHandle *, F, I>,
4931 size_t> {
4932 typedef Func5<size_t, P1, P2, const char *, size_t, const BufferHandle *,
4933 ReturnStringLen<P1, P2, F>, I> Func;
4934};
4935
Josh Habermane8ed0212015-06-08 17:56:03 -07004936/* If our function returns bool but we want one returning size_t, wrap it in a
4937 * function that returns either 0 or the buf size. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004938template <class P1, class P2,
4939 bool F(P1, P2, const char *, size_t, const BufferHandle *), class I>
4940struct MaybeWrapReturn<
4941 Func5<bool, P1, P2, const char *, size_t, const BufferHandle *, F, I>,
4942 size_t> {
4943 typedef Func5<size_t, P1, P2, const char *, size_t, const BufferHandle *,
4944 ReturnNOr0<P1, P2, F>, I> Func;
4945};
4946
Josh Habermane8ed0212015-06-08 17:56:03 -07004947/* ConvertParams **************************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -08004948
Josh Habermane8ed0212015-06-08 17:56:03 -07004949/* Template class that converts the function parameters if necessary, and
4950 * ignores the HandlerData parameter if appropriate.
4951 *
4952 * Template parameter is the are FuncN function type. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004953template <class F, class T>
4954struct ConvertParams;
4955
Josh Habermane8ed0212015-06-08 17:56:03 -07004956/* Function that discards the handler data parameter. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004957template <class R, class P1, R F(P1)>
4958R IgnoreHandlerData2(void *p1, const void *hd) {
4959 UPB_UNUSED(hd);
4960 return F(static_cast<P1>(p1));
4961}
4962
4963template <class R, class P1, class P2Wrapper, class P2Wrapped,
4964 R F(P1, P2Wrapped)>
4965R IgnoreHandlerData3(void *p1, const void *hd, P2Wrapper p2) {
4966 UPB_UNUSED(hd);
4967 return F(static_cast<P1>(p1), p2);
4968}
4969
4970template <class R, class P1, class P2, class P3, R F(P1, P2, P3)>
4971R IgnoreHandlerData4(void *p1, const void *hd, P2 p2, P3 p3) {
4972 UPB_UNUSED(hd);
4973 return F(static_cast<P1>(p1), p2, p3);
4974}
4975
4976template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4)>
4977R IgnoreHandlerData5(void *p1, const void *hd, P2 p2, P3 p3, P4 p4) {
4978 UPB_UNUSED(hd);
4979 return F(static_cast<P1>(p1), p2, p3, p4);
4980}
4981
4982template <class R, class P1, R F(P1, const char*, size_t)>
4983R IgnoreHandlerDataIgnoreHandle(void *p1, const void *hd, const char *p2,
4984 size_t p3, const BufferHandle *handle) {
4985 UPB_UNUSED(hd);
4986 UPB_UNUSED(handle);
4987 return F(static_cast<P1>(p1), p2, p3);
4988}
4989
Josh Habermane8ed0212015-06-08 17:56:03 -07004990/* Function that casts the handler data parameter. */
Chris Fallin91473dc2014-12-12 15:58:26 -08004991template <class R, class P1, class P2, R F(P1, P2)>
4992R CastHandlerData2(void *c, const void *hd) {
4993 return F(static_cast<P1>(c), static_cast<P2>(hd));
4994}
4995
4996template <class R, class P1, class P2, class P3Wrapper, class P3Wrapped,
4997 R F(P1, P2, P3Wrapped)>
4998R CastHandlerData3(void *c, const void *hd, P3Wrapper p3) {
4999 return F(static_cast<P1>(c), static_cast<P2>(hd), p3);
5000}
5001
5002template <class R, class P1, class P2, class P3, class P4, class P5,
5003 R F(P1, P2, P3, P4, P5)>
5004R CastHandlerData5(void *c, const void *hd, P3 p3, P4 p4, P5 p5) {
5005 return F(static_cast<P1>(c), static_cast<P2>(hd), p3, p4, p5);
5006}
5007
5008template <class R, class P1, class P2, R F(P1, P2, const char *, size_t)>
5009R CastHandlerDataIgnoreHandle(void *c, const void *hd, const char *p3,
5010 size_t p4, const BufferHandle *handle) {
5011 UPB_UNUSED(handle);
5012 return F(static_cast<P1>(c), static_cast<P2>(hd), p3, p4);
5013}
5014
Josh Habermane8ed0212015-06-08 17:56:03 -07005015/* For unbound functions, ignore the handler data. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005016template <class R, class P1, R F(P1), class I, class T>
5017struct ConvertParams<Func1<R, P1, F, I>, T> {
5018 typedef Func2<R, void *, const void *, IgnoreHandlerData2<R, P1, F>, I> Func;
5019};
5020
5021template <class R, class P1, class P2, R F(P1, P2), class I,
5022 class R2, class P1_2, class P2_2, class P3_2>
5023struct ConvertParams<Func2<R, P1, P2, F, I>,
5024 R2 (*)(P1_2, P2_2, P3_2)> {
5025 typedef Func3<R, void *, const void *, P3_2,
5026 IgnoreHandlerData3<R, P1, P3_2, P2, F>, I> Func;
5027};
5028
Josh Habermane8ed0212015-06-08 17:56:03 -07005029/* For StringBuffer only; this ignores both the handler data and the
5030 * BufferHandle. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005031template <class R, class P1, R F(P1, const char *, size_t), class I, class T>
5032struct ConvertParams<Func3<R, P1, const char *, size_t, F, I>, T> {
5033 typedef Func5<R, void *, const void *, const char *, size_t,
5034 const BufferHandle *, IgnoreHandlerDataIgnoreHandle<R, P1, F>,
5035 I> Func;
5036};
5037
5038template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
5039 class I, class T>
5040struct ConvertParams<Func4<R, P1, P2, P3, P4, F, I>, T> {
5041 typedef Func5<R, void *, const void *, P2, P3, P4,
5042 IgnoreHandlerData5<R, P1, P2, P3, P4, F>, I> Func;
5043};
5044
Josh Habermane8ed0212015-06-08 17:56:03 -07005045/* For bound functions, cast the handler data. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005046template <class R, class P1, class P2, R F(P1, P2), class I, class T>
5047struct ConvertParams<BoundFunc2<R, P1, P2, F, I>, T> {
5048 typedef Func2<R, void *, const void *, CastHandlerData2<R, P1, P2, F>, I>
5049 Func;
5050};
5051
5052template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I,
5053 class R2, class P1_2, class P2_2, class P3_2>
5054struct ConvertParams<BoundFunc3<R, P1, P2, P3, F, I>,
5055 R2 (*)(P1_2, P2_2, P3_2)> {
5056 typedef Func3<R, void *, const void *, P3_2,
5057 CastHandlerData3<R, P1, P2, P3_2, P3, F>, I> Func;
5058};
5059
Josh Habermane8ed0212015-06-08 17:56:03 -07005060/* For StringBuffer only; this ignores the BufferHandle. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005061template <class R, class P1, class P2, R F(P1, P2, const char *, size_t),
5062 class I, class T>
5063struct ConvertParams<BoundFunc4<R, P1, P2, const char *, size_t, F, I>, T> {
5064 typedef Func5<R, void *, const void *, const char *, size_t,
5065 const BufferHandle *, CastHandlerDataIgnoreHandle<R, P1, P2, F>,
5066 I> Func;
5067};
5068
5069template <class R, class P1, class P2, class P3, class P4, class P5,
5070 R F(P1, P2, P3, P4, P5), class I, class T>
5071struct ConvertParams<BoundFunc5<R, P1, P2, P3, P4, P5, F, I>, T> {
5072 typedef Func5<R, void *, const void *, P3, P4, P5,
5073 CastHandlerData5<R, P1, P2, P3, P4, P5, F>, I> Func;
5074};
5075
Josh Habermane8ed0212015-06-08 17:56:03 -07005076/* utype/ltype are upper/lower-case, ctype is canonical C type, vtype is
5077 * variant C type. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005078#define TYPE_METHODS(utype, ltype, ctype, vtype) \
5079 template <> struct CanonicalType<vtype> { \
5080 typedef ctype Type; \
5081 }; \
5082 template <> \
5083 inline bool Handlers::SetValueHandler<vtype>( \
5084 const FieldDef *f, \
5085 const Handlers::utype ## Handler& handler) { \
5086 assert(!handler.registered_); \
5087 handler.AddCleanup(this); \
5088 handler.registered_ = true; \
5089 return upb_handlers_set##ltype(this, f, handler.handler_, &handler.attr_); \
5090 } \
5091
Josh Habermane8ed0212015-06-08 17:56:03 -07005092TYPE_METHODS(Double, double, double, double)
5093TYPE_METHODS(Float, float, float, float)
5094TYPE_METHODS(UInt64, uint64, uint64_t, UPB_UINT64_T)
5095TYPE_METHODS(UInt32, uint32, uint32_t, UPB_UINT32_T)
5096TYPE_METHODS(Int64, int64, int64_t, UPB_INT64_T)
5097TYPE_METHODS(Int32, int32, int32_t, UPB_INT32_T)
5098TYPE_METHODS(Bool, bool, bool, bool)
Chris Fallin91473dc2014-12-12 15:58:26 -08005099
5100#ifdef UPB_TWO_32BIT_TYPES
Josh Habermane8ed0212015-06-08 17:56:03 -07005101TYPE_METHODS(Int32, int32, int32_t, UPB_INT32ALT_T)
5102TYPE_METHODS(UInt32, uint32, uint32_t, UPB_UINT32ALT_T)
Chris Fallin91473dc2014-12-12 15:58:26 -08005103#endif
5104
5105#ifdef UPB_TWO_64BIT_TYPES
Josh Habermane8ed0212015-06-08 17:56:03 -07005106TYPE_METHODS(Int64, int64, int64_t, UPB_INT64ALT_T)
5107TYPE_METHODS(UInt64, uint64, uint64_t, UPB_UINT64ALT_T)
Chris Fallin91473dc2014-12-12 15:58:26 -08005108#endif
5109#undef TYPE_METHODS
5110
5111template <> struct CanonicalType<Status*> {
5112 typedef Status* Type;
5113};
5114
Josh Habermane8ed0212015-06-08 17:56:03 -07005115/* Type methods that are only one-per-canonical-type and not
5116 * one-per-cvariant. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005117
5118#define TYPE_METHODS(utype, ctype) \
5119 inline bool Handlers::Set##utype##Handler(const FieldDef *f, \
5120 const utype##Handler &h) { \
5121 return SetValueHandler<ctype>(f, h); \
5122 } \
5123
Josh Habermane8ed0212015-06-08 17:56:03 -07005124TYPE_METHODS(Double, double)
5125TYPE_METHODS(Float, float)
5126TYPE_METHODS(UInt64, uint64_t)
5127TYPE_METHODS(UInt32, uint32_t)
5128TYPE_METHODS(Int64, int64_t)
5129TYPE_METHODS(Int32, int32_t)
5130TYPE_METHODS(Bool, bool)
Chris Fallin91473dc2014-12-12 15:58:26 -08005131#undef TYPE_METHODS
5132
5133template <class F> struct ReturnOf;
5134
5135template <class R, class P1, class P2>
5136struct ReturnOf<R (*)(P1, P2)> {
5137 typedef R Return;
5138};
5139
5140template <class R, class P1, class P2, class P3>
5141struct ReturnOf<R (*)(P1, P2, P3)> {
5142 typedef R Return;
5143};
5144
5145template <class R, class P1, class P2, class P3, class P4>
5146struct ReturnOf<R (*)(P1, P2, P3, P4)> {
5147 typedef R Return;
5148};
5149
5150template <class R, class P1, class P2, class P3, class P4, class P5>
5151struct ReturnOf<R (*)(P1, P2, P3, P4, P5)> {
5152 typedef R Return;
5153};
5154
5155template<class T> const void *UniquePtrForType() {
5156 static const char ch = 0;
5157 return &ch;
5158}
5159
5160template <class T>
5161template <class F>
5162inline Handler<T>::Handler(F func)
5163 : registered_(false),
5164 cleanup_data_(func.GetData()),
5165 cleanup_func_(func.GetCleanup()) {
5166 upb_handlerattr_sethandlerdata(&attr_, func.GetData());
5167 typedef typename ReturnOf<T>::Return Return;
5168 typedef typename ConvertParams<F, T>::Func ConvertedParamsFunc;
5169 typedef typename MaybeWrapReturn<ConvertedParamsFunc, Return>::Func
5170 ReturnWrappedFunc;
5171 handler_ = ReturnWrappedFunc().Call;
5172
Josh Habermane8ed0212015-06-08 17:56:03 -07005173 /* Set attributes based on what templates can statically tell us about the
5174 * user's function. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005175
Josh Habermane8ed0212015-06-08 17:56:03 -07005176 /* If the original function returns void, then we know that we wrapped it to
5177 * always return ok. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005178 bool always_ok = is_same<typename F::FuncInfo::Return, void>::value;
5179 attr_.SetAlwaysOk(always_ok);
5180
Josh Habermane8ed0212015-06-08 17:56:03 -07005181 /* Closure parameter and return type. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005182 attr_.SetClosureType(UniquePtrForType<typename F::FuncInfo::Closure>());
5183
Josh Habermane8ed0212015-06-08 17:56:03 -07005184 /* We use the closure type (from the first parameter) if the return type is
5185 * void or bool, since these are the two cases we wrap to return the closure's
5186 * type anyway.
5187 *
5188 * This is all nonsense for non START* handlers, but it doesn't matter because
5189 * in that case the value will be ignored. */
Chris Fallind3262772015-05-14 18:24:26 -07005190 typedef typename FirstUnlessVoidOrBool<typename F::FuncInfo::Return,
5191 typename F::FuncInfo::Closure>::value
Chris Fallin91473dc2014-12-12 15:58:26 -08005192 EffectiveReturn;
5193 attr_.SetReturnClosureType(UniquePtrForType<EffectiveReturn>());
5194}
5195
5196template <class T>
5197inline Handler<T>::~Handler() {
5198 assert(registered_);
5199}
5200
5201inline HandlerAttributes::HandlerAttributes() { upb_handlerattr_init(this); }
5202inline HandlerAttributes::~HandlerAttributes() { upb_handlerattr_uninit(this); }
5203inline bool HandlerAttributes::SetHandlerData(const void *hd) {
5204 return upb_handlerattr_sethandlerdata(this, hd);
5205}
5206inline const void* HandlerAttributes::handler_data() const {
5207 return upb_handlerattr_handlerdata(this);
5208}
5209inline bool HandlerAttributes::SetClosureType(const void *type) {
5210 return upb_handlerattr_setclosuretype(this, type);
5211}
5212inline const void* HandlerAttributes::closure_type() const {
5213 return upb_handlerattr_closuretype(this);
5214}
5215inline bool HandlerAttributes::SetReturnClosureType(const void *type) {
5216 return upb_handlerattr_setreturnclosuretype(this, type);
5217}
5218inline const void* HandlerAttributes::return_closure_type() const {
5219 return upb_handlerattr_returnclosuretype(this);
5220}
5221inline bool HandlerAttributes::SetAlwaysOk(bool always_ok) {
5222 return upb_handlerattr_setalwaysok(this, always_ok);
5223}
5224inline bool HandlerAttributes::always_ok() const {
5225 return upb_handlerattr_alwaysok(this);
5226}
5227
5228inline BufferHandle::BufferHandle() { upb_bufhandle_init(this); }
5229inline BufferHandle::~BufferHandle() { upb_bufhandle_uninit(this); }
5230inline const char* BufferHandle::buffer() const {
5231 return upb_bufhandle_buf(this);
5232}
5233inline size_t BufferHandle::object_offset() const {
5234 return upb_bufhandle_objofs(this);
5235}
5236inline void BufferHandle::SetBuffer(const char* buf, size_t ofs) {
5237 upb_bufhandle_setbuf(this, buf, ofs);
5238}
5239template <class T>
5240void BufferHandle::SetAttachedObject(const T* obj) {
5241 upb_bufhandle_setobj(this, obj, UniquePtrForType<T>());
5242}
5243template <class T>
5244const T* BufferHandle::GetAttachedObject() const {
5245 return upb_bufhandle_objtype(this) == UniquePtrForType<T>()
5246 ? static_cast<const T *>(upb_bufhandle_obj(this))
5247 : NULL;
5248}
5249
5250inline reffed_ptr<Handlers> Handlers::New(const MessageDef *m) {
5251 upb_handlers *h = upb_handlers_new(m, &h);
5252 return reffed_ptr<Handlers>(h, &h);
5253}
5254inline reffed_ptr<const Handlers> Handlers::NewFrozen(
5255 const MessageDef *m, upb_handlers_callback *callback,
5256 const void *closure) {
5257 const upb_handlers *h = upb_handlers_newfrozen(m, &h, callback, closure);
5258 return reffed_ptr<const Handlers>(h, &h);
5259}
Chris Fallin91473dc2014-12-12 15:58:26 -08005260inline const Status* Handlers::status() {
5261 return upb_handlers_status(this);
5262}
5263inline void Handlers::ClearError() {
5264 return upb_handlers_clearerr(this);
5265}
5266inline bool Handlers::Freeze(Status *s) {
5267 upb::Handlers* h = this;
5268 return upb_handlers_freeze(&h, 1, s);
5269}
5270inline bool Handlers::Freeze(Handlers *const *handlers, int n, Status *s) {
5271 return upb_handlers_freeze(handlers, n, s);
5272}
5273inline bool Handlers::Freeze(const std::vector<Handlers*>& h, Status* status) {
5274 return upb_handlers_freeze((Handlers* const*)&h[0], h.size(), status);
5275}
5276inline const MessageDef *Handlers::message_def() const {
5277 return upb_handlers_msgdef(this);
5278}
5279inline bool Handlers::AddCleanup(void *p, upb_handlerfree *func) {
5280 return upb_handlers_addcleanup(this, p, func);
5281}
5282inline bool Handlers::SetStartMessageHandler(
5283 const Handlers::StartMessageHandler &handler) {
5284 assert(!handler.registered_);
5285 handler.registered_ = true;
5286 handler.AddCleanup(this);
5287 return upb_handlers_setstartmsg(this, handler.handler_, &handler.attr_);
5288}
5289inline bool Handlers::SetEndMessageHandler(
5290 const Handlers::EndMessageHandler &handler) {
5291 assert(!handler.registered_);
5292 handler.registered_ = true;
5293 handler.AddCleanup(this);
5294 return upb_handlers_setendmsg(this, handler.handler_, &handler.attr_);
5295}
5296inline bool Handlers::SetStartStringHandler(const FieldDef *f,
5297 const StartStringHandler &handler) {
5298 assert(!handler.registered_);
5299 handler.registered_ = true;
5300 handler.AddCleanup(this);
5301 return upb_handlers_setstartstr(this, f, handler.handler_, &handler.attr_);
5302}
5303inline bool Handlers::SetEndStringHandler(const FieldDef *f,
5304 const EndFieldHandler &handler) {
5305 assert(!handler.registered_);
5306 handler.registered_ = true;
5307 handler.AddCleanup(this);
5308 return upb_handlers_setendstr(this, f, handler.handler_, &handler.attr_);
5309}
5310inline bool Handlers::SetStringHandler(const FieldDef *f,
5311 const StringHandler& handler) {
5312 assert(!handler.registered_);
5313 handler.registered_ = true;
5314 handler.AddCleanup(this);
5315 return upb_handlers_setstring(this, f, handler.handler_, &handler.attr_);
5316}
5317inline bool Handlers::SetStartSequenceHandler(
5318 const FieldDef *f, const StartFieldHandler &handler) {
5319 assert(!handler.registered_);
5320 handler.registered_ = true;
5321 handler.AddCleanup(this);
5322 return upb_handlers_setstartseq(this, f, handler.handler_, &handler.attr_);
5323}
5324inline bool Handlers::SetStartSubMessageHandler(
5325 const FieldDef *f, const StartFieldHandler &handler) {
5326 assert(!handler.registered_);
5327 handler.registered_ = true;
5328 handler.AddCleanup(this);
5329 return upb_handlers_setstartsubmsg(this, f, handler.handler_, &handler.attr_);
5330}
5331inline bool Handlers::SetEndSubMessageHandler(const FieldDef *f,
5332 const EndFieldHandler &handler) {
5333 assert(!handler.registered_);
5334 handler.registered_ = true;
5335 handler.AddCleanup(this);
5336 return upb_handlers_setendsubmsg(this, f, handler.handler_, &handler.attr_);
5337}
5338inline bool Handlers::SetEndSequenceHandler(const FieldDef *f,
5339 const EndFieldHandler &handler) {
5340 assert(!handler.registered_);
5341 handler.registered_ = true;
5342 handler.AddCleanup(this);
5343 return upb_handlers_setendseq(this, f, handler.handler_, &handler.attr_);
5344}
5345inline bool Handlers::SetSubHandlers(const FieldDef *f, const Handlers *sub) {
5346 return upb_handlers_setsubhandlers(this, f, sub);
5347}
5348inline const Handlers *Handlers::GetSubHandlers(const FieldDef *f) const {
5349 return upb_handlers_getsubhandlers(this, f);
5350}
5351inline const Handlers *Handlers::GetSubHandlers(Handlers::Selector sel) const {
5352 return upb_handlers_getsubhandlers_sel(this, sel);
5353}
5354inline bool Handlers::GetSelector(const FieldDef *f, Handlers::Type type,
5355 Handlers::Selector *s) {
5356 return upb_handlers_getselector(f, type, s);
5357}
5358inline Handlers::Selector Handlers::GetEndSelector(Handlers::Selector start) {
5359 return upb_handlers_getendselector(start);
5360}
5361inline Handlers::GenericFunction *Handlers::GetHandler(
5362 Handlers::Selector selector) {
5363 return upb_handlers_gethandler(this, selector);
5364}
5365inline const void *Handlers::GetHandlerData(Handlers::Selector selector) {
5366 return upb_handlers_gethandlerdata(this, selector);
5367}
5368
5369inline BytesHandler::BytesHandler() {
5370 upb_byteshandler_init(this);
5371}
5372
Chris Fallind3262772015-05-14 18:24:26 -07005373inline BytesHandler::~BytesHandler() {}
Chris Fallin91473dc2014-12-12 15:58:26 -08005374
Josh Habermane8ed0212015-06-08 17:56:03 -07005375} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08005376
Josh Habermane8ed0212015-06-08 17:56:03 -07005377#endif /* __cplusplus */
Chris Fallin91473dc2014-12-12 15:58:26 -08005378
5379
5380#undef UPB_TWO_32BIT_TYPES
5381#undef UPB_TWO_64BIT_TYPES
5382#undef UPB_INT32_T
5383#undef UPB_UINT32_T
5384#undef UPB_INT32ALT_T
5385#undef UPB_UINT32ALT_T
5386#undef UPB_INT64_T
5387#undef UPB_UINT64_T
5388#undef UPB_INT64ALT_T
5389#undef UPB_UINT64ALT_T
5390
Josh Habermane8ed0212015-06-08 17:56:03 -07005391#endif /* UPB_HANDLERS_INL_H_ */
Chris Fallin91473dc2014-12-12 15:58:26 -08005392
Josh Habermane8ed0212015-06-08 17:56:03 -07005393#endif /* UPB_HANDLERS_H */
Chris Fallin91473dc2014-12-12 15:58:26 -08005394/*
Josh Haberman181c7f22015-07-15 11:05:10 -07005395** upb::Environment (upb_env)
5396**
5397** A upb::Environment provides a means for injecting malloc and an
5398** error-reporting callback into encoders/decoders. This allows them to be
5399** independent of nearly all assumptions about their actual environment.
5400**
5401** It is also a container for allocating the encoders/decoders themselves that
5402** insulates clients from knowing their actual size. This provides ABI
5403** compatibility even if the size of the objects change. And this allows the
5404** structure definitions to be in the .c files instead of the .h files, making
5405** the .h files smaller and more readable.
5406*/
Chris Fallind3262772015-05-14 18:24:26 -07005407
5408
5409#ifndef UPB_ENV_H_
5410#define UPB_ENV_H_
5411
5412#ifdef __cplusplus
5413namespace upb {
5414class Environment;
5415class SeededAllocator;
5416}
5417#endif
5418
Josh Habermane8ed0212015-06-08 17:56:03 -07005419UPB_DECLARE_TYPE(upb::Environment, upb_env)
5420UPB_DECLARE_TYPE(upb::SeededAllocator, upb_seededalloc)
Chris Fallind3262772015-05-14 18:24:26 -07005421
5422typedef void *upb_alloc_func(void *ud, void *ptr, size_t oldsize, size_t size);
5423typedef void upb_cleanup_func(void *ud);
5424typedef bool upb_error_func(void *ud, const upb_status *status);
5425
Josh Habermane8ed0212015-06-08 17:56:03 -07005426#ifdef __cplusplus
5427
5428/* An environment is *not* thread-safe. */
5429class upb::Environment {
Chris Fallind3262772015-05-14 18:24:26 -07005430 public:
5431 Environment();
5432 ~Environment();
5433
Josh Habermane8ed0212015-06-08 17:56:03 -07005434 /* Set a custom memory allocation function for the environment. May ONLY
5435 * be called before any calls to Malloc()/Realloc()/AddCleanup() below.
5436 * If this is not called, the system realloc() function will be used.
5437 * The given user pointer "ud" will be passed to the allocation function.
5438 *
5439 * The allocation function will not receive corresponding "free" calls. it
5440 * must ensure that the memory is valid for the lifetime of the Environment,
5441 * but it may be reclaimed any time thereafter. The likely usage is that
5442 * "ud" points to a stateful allocator, and that the allocator frees all
5443 * memory, arena-style, when it is destroyed. In this case the allocator must
5444 * outlive the Environment. Another possibility is that the allocation
5445 * function returns GC-able memory that is guaranteed to be GC-rooted for the
5446 * life of the Environment. */
Chris Fallind3262772015-05-14 18:24:26 -07005447 void SetAllocationFunction(upb_alloc_func* alloc, void* ud);
5448
5449 template<class T>
5450 void SetAllocator(T* allocator) {
5451 SetAllocationFunction(allocator->GetAllocationFunction(), allocator);
5452 }
5453
Josh Habermane8ed0212015-06-08 17:56:03 -07005454 /* Set a custom error reporting function. */
Chris Fallind3262772015-05-14 18:24:26 -07005455 void SetErrorFunction(upb_error_func* func, void* ud);
5456
Josh Habermane8ed0212015-06-08 17:56:03 -07005457 /* Set the error reporting function to simply copy the status to the given
5458 * status and abort. */
Chris Fallind3262772015-05-14 18:24:26 -07005459 void ReportErrorsTo(Status* status);
5460
Josh Habermane8ed0212015-06-08 17:56:03 -07005461 /* Returns true if all allocations and AddCleanup() calls have succeeded,
5462 * and no errors were reported with ReportError() (except ones that recovered
5463 * successfully). */
Chris Fallind3262772015-05-14 18:24:26 -07005464 bool ok() const;
5465
Josh Habermane8ed0212015-06-08 17:56:03 -07005466 /* Functions for use by encoders/decoders. **********************************/
Chris Fallind3262772015-05-14 18:24:26 -07005467
Josh Habermane8ed0212015-06-08 17:56:03 -07005468 /* Reports an error to this environment's callback, returning true if
5469 * the caller should try to recover. */
Chris Fallind3262772015-05-14 18:24:26 -07005470 bool ReportError(const Status* status);
5471
Josh Habermane8ed0212015-06-08 17:56:03 -07005472 /* Allocate memory. Uses the environment's allocation function.
5473 *
5474 * There is no need to free(). All memory will be freed automatically, but is
5475 * guaranteed to outlive the Environment. */
Chris Fallind3262772015-05-14 18:24:26 -07005476 void* Malloc(size_t size);
5477
Josh Habermane8ed0212015-06-08 17:56:03 -07005478 /* Reallocate memory. Preserves "oldsize" bytes from the existing buffer
5479 * Requires: oldsize <= existing_size.
5480 *
5481 * TODO(haberman): should we also enforce that oldsize <= size? */
Chris Fallind3262772015-05-14 18:24:26 -07005482 void* Realloc(void* ptr, size_t oldsize, size_t size);
5483
Josh Habermane8ed0212015-06-08 17:56:03 -07005484 /* Add a cleanup function to run when the environment is destroyed.
5485 * Returns false on out-of-memory.
5486 *
5487 * The first call to AddCleanup() after SetAllocationFunction() is guaranteed
5488 * to return true -- this makes it possible to robustly set a cleanup handler
5489 * for a custom allocation function. */
Chris Fallind3262772015-05-14 18:24:26 -07005490 bool AddCleanup(upb_cleanup_func* func, void* ud);
5491
Josh Habermane8ed0212015-06-08 17:56:03 -07005492 /* Total number of bytes that have been allocated. It is undefined what
5493 * Realloc() does to this counter. */
Chris Fallind3262772015-05-14 18:24:26 -07005494 size_t BytesAllocated() const;
5495
5496 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07005497 UPB_DISALLOW_COPY_AND_ASSIGN(Environment)
5498
5499#else
5500struct upb_env {
5501#endif /* __cplusplus */
5502
Chris Fallind3262772015-05-14 18:24:26 -07005503 bool ok_;
5504 size_t bytes_allocated;
5505
Josh Habermane8ed0212015-06-08 17:56:03 -07005506 /* Alloc function. */
Chris Fallind3262772015-05-14 18:24:26 -07005507 upb_alloc_func *alloc;
5508 void *alloc_ud;
5509
Josh Habermane8ed0212015-06-08 17:56:03 -07005510 /* Error-reporting function. */
Chris Fallind3262772015-05-14 18:24:26 -07005511 upb_error_func *err;
5512 void *err_ud;
5513
Josh Habermane8ed0212015-06-08 17:56:03 -07005514 /* Userdata for default alloc func. */
Chris Fallind3262772015-05-14 18:24:26 -07005515 void *default_alloc_ud;
5516
Josh Habermane8ed0212015-06-08 17:56:03 -07005517 /* Cleanup entries. Pointer to a cleanup_ent, defined in env.c */
Chris Fallind3262772015-05-14 18:24:26 -07005518 void *cleanup_head;
5519
Josh Habermane8ed0212015-06-08 17:56:03 -07005520 /* For future expansion, since the size of this struct is exposed to users. */
Chris Fallind3262772015-05-14 18:24:26 -07005521 void *future1;
5522 void *future2;
Josh Habermane8ed0212015-06-08 17:56:03 -07005523};
Chris Fallind3262772015-05-14 18:24:26 -07005524
5525UPB_BEGIN_EXTERN_C
5526
5527void upb_env_init(upb_env *e);
5528void upb_env_uninit(upb_env *e);
5529void upb_env_setallocfunc(upb_env *e, upb_alloc_func *func, void *ud);
5530void upb_env_seterrorfunc(upb_env *e, upb_error_func *func, void *ud);
5531void upb_env_reporterrorsto(upb_env *e, upb_status *status);
5532bool upb_env_ok(const upb_env *e);
5533bool upb_env_reporterror(upb_env *e, const upb_status *status);
5534void *upb_env_malloc(upb_env *e, size_t size);
5535void *upb_env_realloc(upb_env *e, void *ptr, size_t oldsize, size_t size);
5536bool upb_env_addcleanup(upb_env *e, upb_cleanup_func *func, void *ud);
5537size_t upb_env_bytesallocated(const upb_env *e);
5538
5539UPB_END_EXTERN_C
5540
Josh Habermane8ed0212015-06-08 17:56:03 -07005541#ifdef __cplusplus
5542
5543/* An allocator that allocates from an initial memory region (likely the stack)
5544 * before falling back to another allocator. */
5545class upb::SeededAllocator {
Chris Fallind3262772015-05-14 18:24:26 -07005546 public:
5547 SeededAllocator(void *mem, size_t len);
5548 ~SeededAllocator();
5549
Josh Habermane8ed0212015-06-08 17:56:03 -07005550 /* Set a custom fallback memory allocation function for the allocator, to use
5551 * once the initial region runs out.
5552 *
5553 * May ONLY be called before GetAllocationFunction(). If this is not
5554 * called, the system realloc() will be the fallback allocator. */
Chris Fallind3262772015-05-14 18:24:26 -07005555 void SetFallbackAllocator(upb_alloc_func *alloc, void *ud);
5556
Josh Habermane8ed0212015-06-08 17:56:03 -07005557 /* Gets the allocation function for this allocator. */
Chris Fallind3262772015-05-14 18:24:26 -07005558 upb_alloc_func* GetAllocationFunction();
5559
5560 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07005561 UPB_DISALLOW_COPY_AND_ASSIGN(SeededAllocator)
5562
5563#else
5564struct upb_seededalloc {
5565#endif /* __cplusplus */
5566
5567 /* Fallback alloc function. */
Chris Fallind3262772015-05-14 18:24:26 -07005568 upb_alloc_func *alloc;
5569 upb_cleanup_func *alloc_cleanup;
5570 void *alloc_ud;
5571 bool need_cleanup;
5572 bool returned_allocfunc;
5573
Josh Habermane8ed0212015-06-08 17:56:03 -07005574 /* Userdata for default alloc func. */
Chris Fallind3262772015-05-14 18:24:26 -07005575 void *default_alloc_ud;
5576
Josh Habermane8ed0212015-06-08 17:56:03 -07005577 /* Pointers for the initial memory region. */
Chris Fallind3262772015-05-14 18:24:26 -07005578 char *mem_base;
5579 char *mem_ptr;
5580 char *mem_limit;
5581
Josh Habermane8ed0212015-06-08 17:56:03 -07005582 /* For future expansion, since the size of this struct is exposed to users. */
Chris Fallind3262772015-05-14 18:24:26 -07005583 void *future1;
5584 void *future2;
Josh Habermane8ed0212015-06-08 17:56:03 -07005585};
Chris Fallind3262772015-05-14 18:24:26 -07005586
5587UPB_BEGIN_EXTERN_C
5588
5589void upb_seededalloc_init(upb_seededalloc *a, void *mem, size_t len);
5590void upb_seededalloc_uninit(upb_seededalloc *a);
5591void upb_seededalloc_setfallbackalloc(upb_seededalloc *a, upb_alloc_func *func,
5592 void *ud);
5593upb_alloc_func *upb_seededalloc_getallocfunc(upb_seededalloc *a);
5594
5595UPB_END_EXTERN_C
5596
5597#ifdef __cplusplus
5598
5599namespace upb {
5600
5601inline Environment::Environment() {
5602 upb_env_init(this);
5603}
5604inline Environment::~Environment() {
5605 upb_env_uninit(this);
5606}
5607inline void Environment::SetAllocationFunction(upb_alloc_func *alloc,
5608 void *ud) {
5609 upb_env_setallocfunc(this, alloc, ud);
5610}
5611inline void Environment::SetErrorFunction(upb_error_func *func, void *ud) {
5612 upb_env_seterrorfunc(this, func, ud);
5613}
5614inline void Environment::ReportErrorsTo(Status* status) {
5615 upb_env_reporterrorsto(this, status);
5616}
5617inline bool Environment::ok() const {
5618 return upb_env_ok(this);
5619}
5620inline bool Environment::ReportError(const Status* status) {
5621 return upb_env_reporterror(this, status);
5622}
5623inline void *Environment::Malloc(size_t size) {
5624 return upb_env_malloc(this, size);
5625}
5626inline void *Environment::Realloc(void *ptr, size_t oldsize, size_t size) {
5627 return upb_env_realloc(this, ptr, oldsize, size);
5628}
5629inline bool Environment::AddCleanup(upb_cleanup_func *func, void *ud) {
5630 return upb_env_addcleanup(this, func, ud);
5631}
5632inline size_t Environment::BytesAllocated() const {
5633 return upb_env_bytesallocated(this);
5634}
5635
5636inline SeededAllocator::SeededAllocator(void *mem, size_t len) {
5637 upb_seededalloc_init(this, mem, len);
5638}
5639inline SeededAllocator::~SeededAllocator() {
5640 upb_seededalloc_uninit(this);
5641}
5642inline void SeededAllocator::SetFallbackAllocator(upb_alloc_func *alloc,
5643 void *ud) {
5644 upb_seededalloc_setfallbackalloc(this, alloc, ud);
5645}
5646inline upb_alloc_func *SeededAllocator::GetAllocationFunction() {
5647 return upb_seededalloc_getallocfunc(this);
5648}
5649
Josh Habermane8ed0212015-06-08 17:56:03 -07005650} /* namespace upb */
Chris Fallind3262772015-05-14 18:24:26 -07005651
Josh Habermane8ed0212015-06-08 17:56:03 -07005652#endif /* __cplusplus */
Chris Fallind3262772015-05-14 18:24:26 -07005653
Josh Habermane8ed0212015-06-08 17:56:03 -07005654#endif /* UPB_ENV_H_ */
Chris Fallind3262772015-05-14 18:24:26 -07005655/*
Josh Haberman181c7f22015-07-15 11:05:10 -07005656** upb::Sink (upb_sink)
5657** upb::BytesSink (upb_bytessink)
5658**
5659** A upb_sink is an object that binds a upb_handlers object to some runtime
5660** state. It is the object that can actually receive data via the upb_handlers
5661** interface.
5662**
5663** Unlike upb_def and upb_handlers, upb_sink is never frozen, immutable, or
5664** thread-safe. You can create as many of them as you want, but each one may
5665** only be used in a single thread at a time.
5666**
5667** If we compare with class-based OOP, a you can think of a upb_def as an
5668** abstract base class, a upb_handlers as a concrete derived class, and a
5669** upb_sink as an object (class instance).
5670*/
Chris Fallin91473dc2014-12-12 15:58:26 -08005671
5672#ifndef UPB_SINK_H
5673#define UPB_SINK_H
5674
5675
5676#ifdef __cplusplus
5677namespace upb {
5678class BufferSource;
5679class BytesSink;
5680class Sink;
5681}
5682#endif
5683
Josh Habermane8ed0212015-06-08 17:56:03 -07005684UPB_DECLARE_TYPE(upb::BufferSource, upb_bufsrc)
5685UPB_DECLARE_TYPE(upb::BytesSink, upb_bytessink)
5686UPB_DECLARE_TYPE(upb::Sink, upb_sink)
Chris Fallin91473dc2014-12-12 15:58:26 -08005687
Josh Habermane8ed0212015-06-08 17:56:03 -07005688#ifdef __cplusplus
5689
5690/* A upb::Sink is an object that binds a upb::Handlers object to some runtime
5691 * state. It represents an endpoint to which data can be sent.
5692 *
5693 * TODO(haberman): right now all of these functions take selectors. Should they
5694 * take selectorbase instead?
5695 *
5696 * ie. instead of calling:
5697 * sink->StartString(FOO_FIELD_START_STRING, ...)
5698 * a selector base would let you say:
5699 * sink->StartString(FOO_FIELD, ...)
5700 *
5701 * This would make call sites a little nicer and require emitting fewer selector
5702 * definitions in .h files.
5703 *
5704 * But the current scheme has the benefit that you can retrieve a function
5705 * pointer for any handler with handlers->GetHandler(selector), without having
5706 * to have a separate GetHandler() function for each handler type. The JIT
5707 * compiler uses this. To accommodate we'd have to expose a separate
5708 * GetHandler() for every handler type.
5709 *
5710 * Also to ponder: selectors right now are independent of a specific Handlers
5711 * instance. In other words, they allocate a number to every possible handler
5712 * that *could* be registered, without knowing anything about what handlers
5713 * *are* registered. That means that using selectors as table offsets prohibits
5714 * us from compacting the handler table at Freeze() time. If the table is very
5715 * sparse, this could be wasteful.
5716 *
5717 * Having another selector-like thing that is specific to a Handlers instance
5718 * would allow this compacting, but then it would be impossible to write code
5719 * ahead-of-time that can be bound to any Handlers instance at runtime. For
5720 * example, a .proto file parser written as straight C will not know what
5721 * Handlers it will be bound to, so when it calls sink->StartString() what
5722 * selector will it pass? It needs a selector like we have today, that is
5723 * independent of any particular upb::Handlers.
5724 *
5725 * Is there a way then to allow Handlers table compaction? */
5726class upb::Sink {
Chris Fallin91473dc2014-12-12 15:58:26 -08005727 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07005728 /* Constructor with no initialization; must be Reset() before use. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005729 Sink() {}
5730
Josh Habermane8ed0212015-06-08 17:56:03 -07005731 /* Constructs a new sink for the given frozen handlers and closure.
5732 *
5733 * TODO: once the Handlers know the expected closure type, verify that T
5734 * matches it. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005735 template <class T> Sink(const Handlers* handlers, T* closure);
5736
Josh Habermane8ed0212015-06-08 17:56:03 -07005737 /* Resets the value of the sink. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005738 template <class T> void Reset(const Handlers* handlers, T* closure);
5739
Josh Habermane8ed0212015-06-08 17:56:03 -07005740 /* Returns the top-level object that is bound to this sink.
5741 *
5742 * TODO: once the Handlers know the expected closure type, verify that T
5743 * matches it. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005744 template <class T> T* GetObject() const;
5745
Josh Habermane8ed0212015-06-08 17:56:03 -07005746 /* Functions for pushing data into the sink.
5747 *
5748 * These return false if processing should stop (either due to error or just
5749 * to suspend).
5750 *
5751 * These may not be called from within one of the same sink's handlers (in
5752 * other words, handlers are not re-entrant). */
Chris Fallin91473dc2014-12-12 15:58:26 -08005753
Josh Habermane8ed0212015-06-08 17:56:03 -07005754 /* Should be called at the start and end of every message; both the top-level
5755 * message and submessages. This means that submessages should use the
5756 * following sequence:
5757 * sink->StartSubMessage(startsubmsg_selector);
5758 * sink->StartMessage();
5759 * // ...
5760 * sink->EndMessage(&status);
5761 * sink->EndSubMessage(endsubmsg_selector); */
Chris Fallin91473dc2014-12-12 15:58:26 -08005762 bool StartMessage();
5763 bool EndMessage(Status* status);
5764
Josh Habermane8ed0212015-06-08 17:56:03 -07005765 /* Putting of individual values. These work for both repeated and
5766 * non-repeated fields, but for repeated fields you must wrap them in
5767 * calls to StartSequence()/EndSequence(). */
Chris Fallin91473dc2014-12-12 15:58:26 -08005768 bool PutInt32(Handlers::Selector s, int32_t val);
5769 bool PutInt64(Handlers::Selector s, int64_t val);
5770 bool PutUInt32(Handlers::Selector s, uint32_t val);
5771 bool PutUInt64(Handlers::Selector s, uint64_t val);
5772 bool PutFloat(Handlers::Selector s, float val);
5773 bool PutDouble(Handlers::Selector s, double val);
5774 bool PutBool(Handlers::Selector s, bool val);
5775
Josh Habermane8ed0212015-06-08 17:56:03 -07005776 /* Putting of string/bytes values. Each string can consist of zero or more
5777 * non-contiguous buffers of data.
5778 *
5779 * For StartString(), the function will write a sink for the string to "sub."
5780 * The sub-sink must be used for any/all PutStringBuffer() calls. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005781 bool StartString(Handlers::Selector s, size_t size_hint, Sink* sub);
5782 size_t PutStringBuffer(Handlers::Selector s, const char *buf, size_t len,
5783 const BufferHandle *handle);
5784 bool EndString(Handlers::Selector s);
5785
Josh Habermane8ed0212015-06-08 17:56:03 -07005786 /* For submessage fields.
5787 *
5788 * For StartSubMessage(), the function will write a sink for the string to
5789 * "sub." The sub-sink must be used for any/all handlers called within the
5790 * submessage. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005791 bool StartSubMessage(Handlers::Selector s, Sink* sub);
5792 bool EndSubMessage(Handlers::Selector s);
5793
Josh Habermane8ed0212015-06-08 17:56:03 -07005794 /* For repeated fields of any type, the sequence of values must be wrapped in
5795 * these calls.
5796 *
5797 * For StartSequence(), the function will write a sink for the string to
5798 * "sub." The sub-sink must be used for any/all handlers called within the
5799 * sequence. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005800 bool StartSequence(Handlers::Selector s, Sink* sub);
5801 bool EndSequence(Handlers::Selector s);
5802
Josh Habermane8ed0212015-06-08 17:56:03 -07005803 /* Copy and assign specifically allowed.
5804 * We don't even bother making these members private because so many
5805 * functions need them and this is mainly just a dumb data container anyway.
5806 */
5807#else
5808struct upb_sink {
5809#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08005810 const upb_handlers *handlers;
5811 void *closure;
Josh Habermane8ed0212015-06-08 17:56:03 -07005812};
Chris Fallin91473dc2014-12-12 15:58:26 -08005813
Josh Habermane8ed0212015-06-08 17:56:03 -07005814#ifdef __cplusplus
5815class upb::BytesSink {
Chris Fallin91473dc2014-12-12 15:58:26 -08005816 public:
5817 BytesSink() {}
5818
Josh Habermane8ed0212015-06-08 17:56:03 -07005819 /* Constructs a new sink for the given frozen handlers and closure.
5820 *
5821 * TODO(haberman): once the Handlers know the expected closure type, verify
5822 * that T matches it. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005823 template <class T> BytesSink(const BytesHandler* handler, T* closure);
5824
Josh Habermane8ed0212015-06-08 17:56:03 -07005825 /* Resets the value of the sink. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005826 template <class T> void Reset(const BytesHandler* handler, T* closure);
5827
5828 bool Start(size_t size_hint, void **subc);
5829 size_t PutBuffer(void *subc, const char *buf, size_t len,
5830 const BufferHandle *handle);
5831 bool End();
Josh Habermane8ed0212015-06-08 17:56:03 -07005832#else
5833struct upb_bytessink {
5834#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08005835 const upb_byteshandler *handler;
5836 void *closure;
Josh Habermane8ed0212015-06-08 17:56:03 -07005837};
Chris Fallin91473dc2014-12-12 15:58:26 -08005838
Josh Habermane8ed0212015-06-08 17:56:03 -07005839#ifdef __cplusplus
5840
5841/* A class for pushing a flat buffer of data to a BytesSink.
5842 * You can construct an instance of this to get a resumable source,
5843 * or just call the static PutBuffer() to do a non-resumable push all in one
5844 * go. */
5845class upb::BufferSource {
Chris Fallin91473dc2014-12-12 15:58:26 -08005846 public:
5847 BufferSource();
5848 BufferSource(const char* buf, size_t len, BytesSink* sink);
5849
Josh Habermane8ed0212015-06-08 17:56:03 -07005850 /* Returns true if the entire buffer was pushed successfully. Otherwise the
5851 * next call to PutNext() will resume where the previous one left off.
5852 * TODO(haberman): implement this. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005853 bool PutNext();
5854
Josh Habermane8ed0212015-06-08 17:56:03 -07005855 /* A static version; with this version is it not possible to resume in the
5856 * case of failure or a partially-consumed buffer. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005857 static bool PutBuffer(const char* buf, size_t len, BytesSink* sink);
5858
5859 template <class T> static bool PutBuffer(const T& str, BytesSink* sink) {
5860 return PutBuffer(str.c_str(), str.size(), sink);
5861 }
Josh Habermane8ed0212015-06-08 17:56:03 -07005862#else
5863struct upb_bufsrc {
5864 char dummy;
5865#endif
5866};
Chris Fallin91473dc2014-12-12 15:58:26 -08005867
Josh Habermane8ed0212015-06-08 17:56:03 -07005868UPB_BEGIN_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08005869
Josh Habermane8ed0212015-06-08 17:56:03 -07005870/* Inline definitions. */
Chris Fallin91473dc2014-12-12 15:58:26 -08005871
5872UPB_INLINE void upb_bytessink_reset(upb_bytessink *s, const upb_byteshandler *h,
5873 void *closure) {
5874 s->handler = h;
5875 s->closure = closure;
5876}
5877
5878UPB_INLINE bool upb_bytessink_start(upb_bytessink *s, size_t size_hint,
5879 void **subc) {
Josh Habermane8ed0212015-06-08 17:56:03 -07005880 typedef upb_startstr_handlerfunc func;
5881 func *start;
Chris Fallin91473dc2014-12-12 15:58:26 -08005882 *subc = s->closure;
5883 if (!s->handler) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005884 start = (func *)s->handler->table[UPB_STARTSTR_SELECTOR].func;
Chris Fallin91473dc2014-12-12 15:58:26 -08005885
5886 if (!start) return true;
5887 *subc = start(s->closure, upb_handlerattr_handlerdata(
5888 &s->handler->table[UPB_STARTSTR_SELECTOR].attr),
5889 size_hint);
5890 return *subc != NULL;
5891}
5892
5893UPB_INLINE size_t upb_bytessink_putbuf(upb_bytessink *s, void *subc,
5894 const char *buf, size_t size,
5895 const upb_bufhandle* handle) {
Josh Habermane8ed0212015-06-08 17:56:03 -07005896 typedef upb_string_handlerfunc func;
5897 func *putbuf;
Chris Fallin91473dc2014-12-12 15:58:26 -08005898 if (!s->handler) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005899 putbuf = (func *)s->handler->table[UPB_STRING_SELECTOR].func;
Chris Fallin91473dc2014-12-12 15:58:26 -08005900
5901 if (!putbuf) return true;
5902 return putbuf(subc, upb_handlerattr_handlerdata(
5903 &s->handler->table[UPB_STRING_SELECTOR].attr),
5904 buf, size, handle);
5905}
5906
5907UPB_INLINE bool upb_bytessink_end(upb_bytessink *s) {
Josh Habermane8ed0212015-06-08 17:56:03 -07005908 typedef upb_endfield_handlerfunc func;
5909 func *end;
Chris Fallin91473dc2014-12-12 15:58:26 -08005910 if (!s->handler) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005911 end = (func *)s->handler->table[UPB_ENDSTR_SELECTOR].func;
Chris Fallin91473dc2014-12-12 15:58:26 -08005912
5913 if (!end) return true;
5914 return end(s->closure,
5915 upb_handlerattr_handlerdata(
5916 &s->handler->table[UPB_ENDSTR_SELECTOR].attr));
5917}
5918
5919UPB_INLINE bool upb_bufsrc_putbuf(const char *buf, size_t len,
5920 upb_bytessink *sink) {
5921 void *subc;
Josh Habermane8ed0212015-06-08 17:56:03 -07005922 bool ret;
Chris Fallin91473dc2014-12-12 15:58:26 -08005923 upb_bufhandle handle;
5924 upb_bufhandle_init(&handle);
5925 upb_bufhandle_setbuf(&handle, buf, 0);
Josh Habermane8ed0212015-06-08 17:56:03 -07005926 ret = upb_bytessink_start(sink, len, &subc);
Chris Fallin91473dc2014-12-12 15:58:26 -08005927 if (ret && len != 0) {
Josh Haberman5bdf4a42015-08-03 15:51:31 -07005928 ret = (upb_bytessink_putbuf(sink, subc, buf, len, &handle) >= len);
Chris Fallin91473dc2014-12-12 15:58:26 -08005929 }
5930 if (ret) {
5931 ret = upb_bytessink_end(sink);
5932 }
5933 upb_bufhandle_uninit(&handle);
5934 return ret;
5935}
5936
5937#define PUTVAL(type, ctype) \
5938 UPB_INLINE bool upb_sink_put##type(upb_sink *s, upb_selector_t sel, \
5939 ctype val) { \
Josh Habermane8ed0212015-06-08 17:56:03 -07005940 typedef upb_##type##_handlerfunc functype; \
5941 functype *func; \
5942 const void *hd; \
Chris Fallin91473dc2014-12-12 15:58:26 -08005943 if (!s->handlers) return true; \
Josh Habermane8ed0212015-06-08 17:56:03 -07005944 func = (functype *)upb_handlers_gethandler(s->handlers, sel); \
Chris Fallin91473dc2014-12-12 15:58:26 -08005945 if (!func) return true; \
Josh Habermane8ed0212015-06-08 17:56:03 -07005946 hd = upb_handlers_gethandlerdata(s->handlers, sel); \
Chris Fallin91473dc2014-12-12 15:58:26 -08005947 return func(s->closure, hd, val); \
5948 }
5949
Josh Habermane8ed0212015-06-08 17:56:03 -07005950PUTVAL(int32, int32_t)
5951PUTVAL(int64, int64_t)
5952PUTVAL(uint32, uint32_t)
5953PUTVAL(uint64, uint64_t)
5954PUTVAL(float, float)
5955PUTVAL(double, double)
5956PUTVAL(bool, bool)
Chris Fallin91473dc2014-12-12 15:58:26 -08005957#undef PUTVAL
5958
5959UPB_INLINE void upb_sink_reset(upb_sink *s, const upb_handlers *h, void *c) {
5960 s->handlers = h;
5961 s->closure = c;
5962}
5963
5964UPB_INLINE size_t upb_sink_putstring(upb_sink *s, upb_selector_t sel,
5965 const char *buf, size_t n,
5966 const upb_bufhandle *handle) {
Josh Habermane8ed0212015-06-08 17:56:03 -07005967 typedef upb_string_handlerfunc func;
5968 func *handler;
5969 const void *hd;
Chris Fallin91473dc2014-12-12 15:58:26 -08005970 if (!s->handlers) return n;
Josh Habermane8ed0212015-06-08 17:56:03 -07005971 handler = (func *)upb_handlers_gethandler(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08005972
5973 if (!handler) return n;
Josh Habermane8ed0212015-06-08 17:56:03 -07005974 hd = upb_handlers_gethandlerdata(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08005975 return handler(s->closure, hd, buf, n, handle);
5976}
5977
5978UPB_INLINE bool upb_sink_startmsg(upb_sink *s) {
Josh Habermane8ed0212015-06-08 17:56:03 -07005979 typedef upb_startmsg_handlerfunc func;
5980 func *startmsg;
5981 const void *hd;
Chris Fallin91473dc2014-12-12 15:58:26 -08005982 if (!s->handlers) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005983 startmsg = (func*)upb_handlers_gethandler(s->handlers, UPB_STARTMSG_SELECTOR);
5984
Chris Fallin91473dc2014-12-12 15:58:26 -08005985 if (!startmsg) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005986 hd = upb_handlers_gethandlerdata(s->handlers, UPB_STARTMSG_SELECTOR);
Chris Fallin91473dc2014-12-12 15:58:26 -08005987 return startmsg(s->closure, hd);
5988}
5989
5990UPB_INLINE bool upb_sink_endmsg(upb_sink *s, upb_status *status) {
Josh Habermane8ed0212015-06-08 17:56:03 -07005991 typedef upb_endmsg_handlerfunc func;
5992 func *endmsg;
5993 const void *hd;
Chris Fallin91473dc2014-12-12 15:58:26 -08005994 if (!s->handlers) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005995 endmsg = (func *)upb_handlers_gethandler(s->handlers, UPB_ENDMSG_SELECTOR);
Chris Fallin91473dc2014-12-12 15:58:26 -08005996
5997 if (!endmsg) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07005998 hd = upb_handlers_gethandlerdata(s->handlers, UPB_ENDMSG_SELECTOR);
Chris Fallin91473dc2014-12-12 15:58:26 -08005999 return endmsg(s->closure, hd, status);
6000}
6001
6002UPB_INLINE bool upb_sink_startseq(upb_sink *s, upb_selector_t sel,
6003 upb_sink *sub) {
Josh Habermane8ed0212015-06-08 17:56:03 -07006004 typedef upb_startfield_handlerfunc func;
6005 func *startseq;
6006 const void *hd;
Chris Fallin91473dc2014-12-12 15:58:26 -08006007 sub->closure = s->closure;
6008 sub->handlers = s->handlers;
6009 if (!s->handlers) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07006010 startseq = (func*)upb_handlers_gethandler(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08006011
6012 if (!startseq) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07006013 hd = upb_handlers_gethandlerdata(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08006014 sub->closure = startseq(s->closure, hd);
6015 return sub->closure ? true : false;
6016}
6017
6018UPB_INLINE bool upb_sink_endseq(upb_sink *s, upb_selector_t sel) {
Josh Habermane8ed0212015-06-08 17:56:03 -07006019 typedef upb_endfield_handlerfunc func;
6020 func *endseq;
6021 const void *hd;
Chris Fallin91473dc2014-12-12 15:58:26 -08006022 if (!s->handlers) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07006023 endseq = (func*)upb_handlers_gethandler(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08006024
6025 if (!endseq) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07006026 hd = upb_handlers_gethandlerdata(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08006027 return endseq(s->closure, hd);
6028}
6029
6030UPB_INLINE bool upb_sink_startstr(upb_sink *s, upb_selector_t sel,
6031 size_t size_hint, upb_sink *sub) {
Josh Habermane8ed0212015-06-08 17:56:03 -07006032 typedef upb_startstr_handlerfunc func;
6033 func *startstr;
6034 const void *hd;
Chris Fallin91473dc2014-12-12 15:58:26 -08006035 sub->closure = s->closure;
6036 sub->handlers = s->handlers;
6037 if (!s->handlers) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07006038 startstr = (func*)upb_handlers_gethandler(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08006039
6040 if (!startstr) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07006041 hd = upb_handlers_gethandlerdata(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08006042 sub->closure = startstr(s->closure, hd, size_hint);
6043 return sub->closure ? true : false;
6044}
6045
6046UPB_INLINE bool upb_sink_endstr(upb_sink *s, upb_selector_t sel) {
Josh Habermane8ed0212015-06-08 17:56:03 -07006047 typedef upb_endfield_handlerfunc func;
6048 func *endstr;
6049 const void *hd;
Chris Fallin91473dc2014-12-12 15:58:26 -08006050 if (!s->handlers) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07006051 endstr = (func*)upb_handlers_gethandler(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08006052
6053 if (!endstr) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07006054 hd = upb_handlers_gethandlerdata(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08006055 return endstr(s->closure, hd);
6056}
6057
6058UPB_INLINE bool upb_sink_startsubmsg(upb_sink *s, upb_selector_t sel,
6059 upb_sink *sub) {
Josh Habermane8ed0212015-06-08 17:56:03 -07006060 typedef upb_startfield_handlerfunc func;
6061 func *startsubmsg;
6062 const void *hd;
Chris Fallin91473dc2014-12-12 15:58:26 -08006063 sub->closure = s->closure;
6064 if (!s->handlers) {
6065 sub->handlers = NULL;
6066 return true;
6067 }
6068 sub->handlers = upb_handlers_getsubhandlers_sel(s->handlers, sel);
Josh Habermane8ed0212015-06-08 17:56:03 -07006069 startsubmsg = (func*)upb_handlers_gethandler(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08006070
6071 if (!startsubmsg) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07006072 hd = upb_handlers_gethandlerdata(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08006073 sub->closure = startsubmsg(s->closure, hd);
6074 return sub->closure ? true : false;
6075}
6076
6077UPB_INLINE bool upb_sink_endsubmsg(upb_sink *s, upb_selector_t sel) {
Josh Habermane8ed0212015-06-08 17:56:03 -07006078 typedef upb_endfield_handlerfunc func;
6079 func *endsubmsg;
6080 const void *hd;
Chris Fallin91473dc2014-12-12 15:58:26 -08006081 if (!s->handlers) return true;
Josh Habermane8ed0212015-06-08 17:56:03 -07006082 endsubmsg = (func*)upb_handlers_gethandler(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08006083
6084 if (!endsubmsg) return s->closure;
Josh Habermane8ed0212015-06-08 17:56:03 -07006085 hd = upb_handlers_gethandlerdata(s->handlers, sel);
Chris Fallin91473dc2014-12-12 15:58:26 -08006086 return endsubmsg(s->closure, hd);
6087}
6088
Josh Habermane8ed0212015-06-08 17:56:03 -07006089UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08006090
6091#ifdef __cplusplus
6092
6093namespace upb {
6094
6095template <class T> Sink::Sink(const Handlers* handlers, T* closure) {
6096 upb_sink_reset(this, handlers, closure);
6097}
6098template <class T>
6099inline void Sink::Reset(const Handlers* handlers, T* closure) {
6100 upb_sink_reset(this, handlers, closure);
6101}
6102inline bool Sink::StartMessage() {
6103 return upb_sink_startmsg(this);
6104}
6105inline bool Sink::EndMessage(Status* status) {
6106 return upb_sink_endmsg(this, status);
6107}
6108inline bool Sink::PutInt32(Handlers::Selector sel, int32_t val) {
6109 return upb_sink_putint32(this, sel, val);
6110}
6111inline bool Sink::PutInt64(Handlers::Selector sel, int64_t val) {
6112 return upb_sink_putint64(this, sel, val);
6113}
6114inline bool Sink::PutUInt32(Handlers::Selector sel, uint32_t val) {
6115 return upb_sink_putuint32(this, sel, val);
6116}
6117inline bool Sink::PutUInt64(Handlers::Selector sel, uint64_t val) {
6118 return upb_sink_putuint64(this, sel, val);
6119}
6120inline bool Sink::PutFloat(Handlers::Selector sel, float val) {
6121 return upb_sink_putfloat(this, sel, val);
6122}
6123inline bool Sink::PutDouble(Handlers::Selector sel, double val) {
6124 return upb_sink_putdouble(this, sel, val);
6125}
6126inline bool Sink::PutBool(Handlers::Selector sel, bool val) {
6127 return upb_sink_putbool(this, sel, val);
6128}
6129inline bool Sink::StartString(Handlers::Selector sel, size_t size_hint,
6130 Sink *sub) {
6131 return upb_sink_startstr(this, sel, size_hint, sub);
6132}
6133inline size_t Sink::PutStringBuffer(Handlers::Selector sel, const char *buf,
6134 size_t len, const BufferHandle* handle) {
6135 return upb_sink_putstring(this, sel, buf, len, handle);
6136}
6137inline bool Sink::EndString(Handlers::Selector sel) {
6138 return upb_sink_endstr(this, sel);
6139}
6140inline bool Sink::StartSubMessage(Handlers::Selector sel, Sink* sub) {
6141 return upb_sink_startsubmsg(this, sel, sub);
6142}
6143inline bool Sink::EndSubMessage(Handlers::Selector sel) {
6144 return upb_sink_endsubmsg(this, sel);
6145}
6146inline bool Sink::StartSequence(Handlers::Selector sel, Sink* sub) {
6147 return upb_sink_startseq(this, sel, sub);
6148}
6149inline bool Sink::EndSequence(Handlers::Selector sel) {
6150 return upb_sink_endseq(this, sel);
6151}
6152
6153template <class T>
6154BytesSink::BytesSink(const BytesHandler* handler, T* closure) {
6155 Reset(handler, closure);
6156}
6157
6158template <class T>
6159void BytesSink::Reset(const BytesHandler *handler, T *closure) {
6160 upb_bytessink_reset(this, handler, closure);
6161}
6162inline bool BytesSink::Start(size_t size_hint, void **subc) {
6163 return upb_bytessink_start(this, size_hint, subc);
6164}
6165inline size_t BytesSink::PutBuffer(void *subc, const char *buf, size_t len,
6166 const BufferHandle *handle) {
6167 return upb_bytessink_putbuf(this, subc, buf, len, handle);
6168}
6169inline bool BytesSink::End() {
6170 return upb_bytessink_end(this);
6171}
6172
6173inline bool BufferSource::PutBuffer(const char *buf, size_t len,
6174 BytesSink *sink) {
6175 return upb_bufsrc_putbuf(buf, len, sink);
6176}
6177
Josh Habermane8ed0212015-06-08 17:56:03 -07006178} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08006179#endif
6180
6181#endif
6182/*
Josh Haberman181c7f22015-07-15 11:05:10 -07006183** For handlers that do very tiny, very simple operations, the function call
6184** overhead of calling a handler can be significant. This file allows the
6185** user to define handlers that do something very simple like store the value
6186** to memory and/or set a hasbit. JIT compilers can then special-case these
6187** handlers and emit specialized code for them instead of actually calling the
6188** handler.
6189**
6190** The functionality is very simple/limited right now but may expand to be able
6191** to call another function.
6192*/
Chris Fallin91473dc2014-12-12 15:58:26 -08006193
6194#ifndef UPB_SHIM_H
6195#define UPB_SHIM_H
6196
6197
6198typedef struct {
6199 size_t offset;
6200 int32_t hasbit;
6201} upb_shim_data;
6202
6203#ifdef __cplusplus
6204
6205namespace upb {
6206
6207struct Shim {
6208 typedef upb_shim_data Data;
6209
Josh Habermane8ed0212015-06-08 17:56:03 -07006210 /* Sets a handler for the given field that writes the value to the given
6211 * offset and, if hasbit >= 0, sets a bit at the given bit offset. Returns
6212 * true if the handler was set successfully. */
Chris Fallin91473dc2014-12-12 15:58:26 -08006213 static bool Set(Handlers *h, const FieldDef *f, size_t ofs, int32_t hasbit);
6214
Josh Habermane8ed0212015-06-08 17:56:03 -07006215 /* If this handler is a shim, returns the corresponding upb::Shim::Data and
6216 * stores the type in "type". Otherwise returns NULL. */
Chris Fallin91473dc2014-12-12 15:58:26 -08006217 static const Data* GetData(const Handlers* h, Handlers::Selector s,
6218 FieldDef::Type* type);
6219};
6220
Josh Habermane8ed0212015-06-08 17:56:03 -07006221} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08006222
6223#endif
6224
Josh Habermane8ed0212015-06-08 17:56:03 -07006225UPB_BEGIN_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08006226
Josh Habermane8ed0212015-06-08 17:56:03 -07006227/* C API. */
Chris Fallin91473dc2014-12-12 15:58:26 -08006228bool upb_shim_set(upb_handlers *h, const upb_fielddef *f, size_t offset,
6229 int32_t hasbit);
6230const upb_shim_data *upb_shim_getdata(const upb_handlers *h, upb_selector_t s,
6231 upb_fieldtype_t *type);
6232
Josh Habermane8ed0212015-06-08 17:56:03 -07006233UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08006234
6235#ifdef __cplusplus
Josh Habermane8ed0212015-06-08 17:56:03 -07006236/* C++ Wrappers. */
Chris Fallin91473dc2014-12-12 15:58:26 -08006237namespace upb {
6238inline bool Shim::Set(Handlers* h, const FieldDef* f, size_t ofs,
6239 int32_t hasbit) {
6240 return upb_shim_set(h, f, ofs, hasbit);
6241}
6242inline const Shim::Data* Shim::GetData(const Handlers* h, Handlers::Selector s,
6243 FieldDef::Type* type) {
6244 return upb_shim_getdata(h, s, type);
6245}
Josh Habermane8ed0212015-06-08 17:56:03 -07006246} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08006247#endif
6248
Josh Habermane8ed0212015-06-08 17:56:03 -07006249#endif /* UPB_SHIM_H */
6250/*
Josh Haberman181c7f22015-07-15 11:05:10 -07006251** upb::SymbolTable (upb_symtab)
6252**
6253** A symtab (symbol table) stores a name->def map of upb_defs. Clients could
6254** always create such tables themselves, but upb_symtab has logic for resolving
6255** symbolic references, and in particular, for keeping a whole set of consistent
6256** defs when replacing some subset of those defs. This logic is nontrivial.
6257**
6258** This is a mixed C/C++ interface that offers a full API to both languages.
6259** See the top-level README for more information.
6260*/
Josh Habermane8ed0212015-06-08 17:56:03 -07006261
6262#ifndef UPB_SYMTAB_H_
6263#define UPB_SYMTAB_H_
6264
6265
6266#ifdef __cplusplus
6267#include <vector>
6268namespace upb { class SymbolTable; }
6269#endif
6270
6271UPB_DECLARE_DERIVED_TYPE(upb::SymbolTable, upb::RefCounted,
6272 upb_symtab, upb_refcounted)
6273
6274typedef struct {
6275 UPB_PRIVATE_FOR_CPP
6276 upb_strtable_iter iter;
6277 upb_deftype_t type;
6278} upb_symtab_iter;
6279
6280#ifdef __cplusplus
6281
6282/* Non-const methods in upb::SymbolTable are NOT thread-safe. */
6283class upb::SymbolTable {
6284 public:
6285 /* Returns a new symbol table with a single ref owned by "owner."
6286 * Returns NULL if memory allocation failed. */
6287 static reffed_ptr<SymbolTable> New();
6288
6289 /* Include RefCounted base methods. */
6290 UPB_REFCOUNTED_CPPMETHODS
6291
6292 /* For all lookup functions, the returned pointer is not owned by the
6293 * caller; it may be invalidated by any non-const call or unref of the
6294 * SymbolTable! To protect against this, take a ref if desired. */
6295
6296 /* Freezes the symbol table: prevents further modification of it.
6297 * After the Freeze() operation is successful, the SymbolTable must only be
6298 * accessed via a const pointer.
6299 *
6300 * Unlike with upb::MessageDef/upb::EnumDef/etc, freezing a SymbolTable is not
6301 * a necessary step in using a SymbolTable. If you have no need for it to be
6302 * immutable, there is no need to freeze it ever. However sometimes it is
6303 * useful, and SymbolTables that are statically compiled into the binary are
6304 * always frozen by nature. */
6305 void Freeze();
6306
6307 /* Resolves the given symbol using the rules described in descriptor.proto,
6308 * namely:
6309 *
6310 * If the name starts with a '.', it is fully-qualified. Otherwise,
6311 * C++-like scoping rules are used to find the type (i.e. first the nested
6312 * types within this message are searched, then within the parent, on up
6313 * to the root namespace).
6314 *
6315 * If not found, returns NULL. */
6316 const Def* Resolve(const char* base, const char* sym) const;
6317
6318 /* Finds an entry in the symbol table with this exact name. If not found,
6319 * returns NULL. */
6320 const Def* Lookup(const char *sym) const;
6321 const MessageDef* LookupMessage(const char *sym) const;
6322 const EnumDef* LookupEnum(const char *sym) const;
6323
6324 /* TODO: introduce a C++ iterator, but make it nice and templated so that if
6325 * you ask for an iterator of MessageDef the iterated elements are strongly
6326 * typed as MessageDef*. */
6327
6328 /* Adds the given mutable defs to the symtab, resolving all symbols
6329 * (including enum default values) and finalizing the defs. Only one def per
6330 * name may be in the list, but defs can replace existing defs in the symtab.
6331 * All defs must have a name -- anonymous defs are not allowed. Anonymous
6332 * defs can still be frozen by calling upb_def_freeze() directly.
6333 *
6334 * Any existing defs that can reach defs that are being replaced will
6335 * themselves be replaced also, so that the resulting set of defs is fully
6336 * consistent.
6337 *
6338 * This logic implemented in this method is a convenience; ultimately it
6339 * calls some combination of upb_fielddef_setsubdef(), upb_def_dup(), and
6340 * upb_freeze(), any of which the client could call themself. However, since
6341 * the logic for doing so is nontrivial, we provide it here.
6342 *
6343 * The entire operation either succeeds or fails. If the operation fails,
6344 * the symtab is unchanged, false is returned, and status indicates the
6345 * error. The caller passes a ref on all defs to the symtab (even if the
6346 * operation fails).
6347 *
6348 * TODO(haberman): currently failure will leave the symtab unchanged, but may
6349 * leave the defs themselves partially resolved. Does this matter? If so we
6350 * could do a prepass that ensures that all symbols are resolvable and bail
6351 * if not, so we don't mutate anything until we know the operation will
6352 * succeed.
6353 *
6354 * TODO(haberman): since the defs must be mutable, refining a frozen def
6355 * requires making mutable copies of the entire tree. This is wasteful if
6356 * only a few messages are changing. We may want to add a way of adding a
6357 * tree of frozen defs to the symtab (perhaps an alternate constructor where
6358 * you pass the root of the tree?) */
Josh Haberman94e54b32016-04-14 12:06:09 -07006359 bool Add(Def*const* defs, size_t n, void* ref_donor, Status* status);
Josh Habermane8ed0212015-06-08 17:56:03 -07006360
6361 bool Add(const std::vector<Def*>& defs, void *owner, Status* status) {
6362 return Add((Def*const*)&defs[0], defs.size(), owner, status);
6363 }
6364
Josh Haberman94e54b32016-04-14 12:06:09 -07006365 /* Resolves all subdefs for messages in this file and attempts to freeze the
6366 * file. If this succeeds, adds all the symbols to this SymbolTable
6367 * (replacing any existing ones with the same names). */
6368 bool AddFile(FileDef* file, Status* s);
6369
Josh Habermane8ed0212015-06-08 17:56:03 -07006370 private:
6371 UPB_DISALLOW_POD_OPS(SymbolTable, upb::SymbolTable)
6372};
6373
6374#endif /* __cplusplus */
6375
6376UPB_BEGIN_EXTERN_C
6377
6378/* Native C API. */
6379
6380/* Include refcounted methods like upb_symtab_ref(). */
6381UPB_REFCOUNTED_CMETHODS(upb_symtab, upb_symtab_upcast)
6382
6383upb_symtab *upb_symtab_new(const void *owner);
6384void upb_symtab_freeze(upb_symtab *s);
6385const upb_def *upb_symtab_resolve(const upb_symtab *s, const char *base,
6386 const char *sym);
6387const upb_def *upb_symtab_lookup(const upb_symtab *s, const char *sym);
6388const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym);
6389const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym);
Josh Haberman94e54b32016-04-14 12:06:09 -07006390bool upb_symtab_add(upb_symtab *s, upb_def *const*defs, size_t n,
6391 void *ref_donor, upb_status *status);
6392bool upb_symtab_addfile(upb_symtab *s, upb_filedef *file, upb_status* status);
Josh Habermane8ed0212015-06-08 17:56:03 -07006393
6394/* upb_symtab_iter i;
6395 * for(upb_symtab_begin(&i, s, type); !upb_symtab_done(&i);
6396 * upb_symtab_next(&i)) {
6397 * const upb_def *def = upb_symtab_iter_def(&i);
6398 * // ...
6399 * }
6400 *
6401 * For C we don't have separate iterators for const and non-const.
6402 * It is the caller's responsibility to cast the upb_fielddef* to
6403 * const if the upb_msgdef* is const. */
6404void upb_symtab_begin(upb_symtab_iter *iter, const upb_symtab *s,
6405 upb_deftype_t type);
6406void upb_symtab_next(upb_symtab_iter *iter);
6407bool upb_symtab_done(const upb_symtab_iter *iter);
6408const upb_def *upb_symtab_iter_def(const upb_symtab_iter *iter);
6409
6410UPB_END_EXTERN_C
6411
6412#ifdef __cplusplus
6413/* C++ inline wrappers. */
6414namespace upb {
6415inline reffed_ptr<SymbolTable> SymbolTable::New() {
6416 upb_symtab *s = upb_symtab_new(&s);
6417 return reffed_ptr<SymbolTable>(s, &s);
6418}
6419
6420inline void SymbolTable::Freeze() {
6421 return upb_symtab_freeze(this);
6422}
6423inline const Def *SymbolTable::Resolve(const char *base,
6424 const char *sym) const {
6425 return upb_symtab_resolve(this, base, sym);
6426}
6427inline const Def* SymbolTable::Lookup(const char *sym) const {
6428 return upb_symtab_lookup(this, sym);
6429}
6430inline const MessageDef *SymbolTable::LookupMessage(const char *sym) const {
6431 return upb_symtab_lookupmsg(this, sym);
6432}
6433inline bool SymbolTable::Add(
Josh Haberman94e54b32016-04-14 12:06:09 -07006434 Def*const* defs, size_t n, void* ref_donor, Status* status) {
Josh Habermane8ed0212015-06-08 17:56:03 -07006435 return upb_symtab_add(this, (upb_def*const*)defs, n, ref_donor, status);
6436}
Josh Haberman94e54b32016-04-14 12:06:09 -07006437inline bool SymbolTable::AddFile(FileDef* file, Status* s) {
6438 return upb_symtab_addfile(this, file, s);
6439}
Josh Habermane8ed0212015-06-08 17:56:03 -07006440} /* namespace upb */
6441#endif
6442
6443#endif /* UPB_SYMTAB_H_ */
Chris Fallin91473dc2014-12-12 15:58:26 -08006444/*
Josh Haberman181c7f22015-07-15 11:05:10 -07006445** upb::descriptor::Reader (upb_descreader)
6446**
6447** Provides a way of building upb::Defs from data in descriptor.proto format.
6448*/
Chris Fallin91473dc2014-12-12 15:58:26 -08006449
6450#ifndef UPB_DESCRIPTOR_H
6451#define UPB_DESCRIPTOR_H
6452
6453
6454#ifdef __cplusplus
6455namespace upb {
6456namespace descriptor {
6457class Reader;
Josh Habermane8ed0212015-06-08 17:56:03 -07006458} /* namespace descriptor */
6459} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08006460#endif
6461
Josh Habermane8ed0212015-06-08 17:56:03 -07006462UPB_DECLARE_TYPE(upb::descriptor::Reader, upb_descreader)
Chris Fallin91473dc2014-12-12 15:58:26 -08006463
Chris Fallind3262772015-05-14 18:24:26 -07006464#ifdef __cplusplus
Chris Fallin91473dc2014-12-12 15:58:26 -08006465
Josh Habermane8ed0212015-06-08 17:56:03 -07006466/* Class that receives descriptor data according to the descriptor.proto schema
6467 * and use it to build upb::Defs corresponding to that schema. */
Chris Fallind3262772015-05-14 18:24:26 -07006468class upb::descriptor::Reader {
Chris Fallin91473dc2014-12-12 15:58:26 -08006469 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07006470 /* These handlers must have come from NewHandlers() and must outlive the
6471 * Reader.
6472 *
6473 * TODO: generate the handlers statically (like we do with the
6474 * descriptor.proto defs) so that there is no need to pass this parameter (or
6475 * to build/memory-manage the handlers at runtime at all). Unfortunately this
6476 * is a bit tricky to implement for Handlers, but necessary to simplify this
6477 * interface. */
Chris Fallind3262772015-05-14 18:24:26 -07006478 static Reader* Create(Environment* env, const Handlers* handlers);
Chris Fallin91473dc2014-12-12 15:58:26 -08006479
Josh Habermane8ed0212015-06-08 17:56:03 -07006480 /* The reader's input; this is where descriptor.proto data should be sent. */
Chris Fallin91473dc2014-12-12 15:58:26 -08006481 Sink* input();
6482
Josh Haberman94e54b32016-04-14 12:06:09 -07006483 /* Use to get the FileDefs that have been parsed. */
6484 size_t file_count() const;
6485 FileDef* file(size_t i) const;
Chris Fallin91473dc2014-12-12 15:58:26 -08006486
Josh Habermane8ed0212015-06-08 17:56:03 -07006487 /* Builds and returns handlers for the reader, owned by "owner." */
Chris Fallin91473dc2014-12-12 15:58:26 -08006488 static Handlers* NewHandlers(const void* owner);
Chris Fallin91473dc2014-12-12 15:58:26 -08006489
Chris Fallind3262772015-05-14 18:24:26 -07006490 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07006491 UPB_DISALLOW_POD_OPS(Reader, upb::descriptor::Reader)
Chris Fallind3262772015-05-14 18:24:26 -07006492};
Chris Fallin91473dc2014-12-12 15:58:26 -08006493
Chris Fallind3262772015-05-14 18:24:26 -07006494#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08006495
Chris Fallind3262772015-05-14 18:24:26 -07006496UPB_BEGIN_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08006497
Josh Habermane8ed0212015-06-08 17:56:03 -07006498/* C API. */
Chris Fallind3262772015-05-14 18:24:26 -07006499upb_descreader *upb_descreader_create(upb_env *e, const upb_handlers *h);
Chris Fallin91473dc2014-12-12 15:58:26 -08006500upb_sink *upb_descreader_input(upb_descreader *r);
Josh Haberman94e54b32016-04-14 12:06:09 -07006501size_t upb_descreader_filecount(const upb_descreader *r);
6502upb_filedef *upb_descreader_file(const upb_descreader *r, size_t i);
Chris Fallin91473dc2014-12-12 15:58:26 -08006503const upb_handlers *upb_descreader_newhandlers(const void *owner);
6504
Chris Fallind3262772015-05-14 18:24:26 -07006505UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08006506
6507#ifdef __cplusplus
Josh Habermane8ed0212015-06-08 17:56:03 -07006508/* C++ implementation details. ************************************************/
Chris Fallin91473dc2014-12-12 15:58:26 -08006509namespace upb {
6510namespace descriptor {
Chris Fallind3262772015-05-14 18:24:26 -07006511inline Reader* Reader::Create(Environment* e, const Handlers *h) {
6512 return upb_descreader_create(e, h);
Chris Fallin91473dc2014-12-12 15:58:26 -08006513}
Chris Fallin91473dc2014-12-12 15:58:26 -08006514inline Sink* Reader::input() { return upb_descreader_input(this); }
Josh Haberman94e54b32016-04-14 12:06:09 -07006515inline size_t Reader::file_count() const {
6516 return upb_descreader_filecount(this);
6517}
6518inline FileDef* Reader::file(size_t i) const {
6519 return upb_descreader_file(this, i);
Chris Fallin91473dc2014-12-12 15:58:26 -08006520}
Josh Habermane8ed0212015-06-08 17:56:03 -07006521} /* namespace descriptor */
6522} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08006523#endif
6524
Josh Habermane8ed0212015-06-08 17:56:03 -07006525#endif /* UPB_DESCRIPTOR_H */
6526/* This file contains accessors for a set of compiled-in defs.
6527 * Note that unlike Google's protobuf, it does *not* define
6528 * generated classes or any other kind of data structure for
6529 * actually storing protobufs. It only contains *defs* which
6530 * let you reflect over a protobuf *schema*.
6531 */
6532/* This file was generated by upbc (the upb compiler).
6533 * Do not edit -- your changes will be discarded when the file is
6534 * regenerated. */
6535
Josh Haberman94e54b32016-04-14 12:06:09 -07006536#ifndef UPB_DESCRIPTOR_DESCRIPTOR_PROTO_UPB_H_
6537#define UPB_DESCRIPTOR_DESCRIPTOR_PROTO_UPB_H_
Josh Habermane8ed0212015-06-08 17:56:03 -07006538
6539
Josh Habermane8ed0212015-06-08 17:56:03 -07006540UPB_BEGIN_EXTERN_C
Josh Habermane8ed0212015-06-08 17:56:03 -07006541
6542/* Enums */
6543
6544typedef enum {
Josh Haberman94e54b32016-04-14 12:06:09 -07006545 google_protobuf_FieldDescriptorProto_LABEL_OPTIONAL = 1,
6546 google_protobuf_FieldDescriptorProto_LABEL_REQUIRED = 2,
6547 google_protobuf_FieldDescriptorProto_LABEL_REPEATED = 3
Josh Habermane8ed0212015-06-08 17:56:03 -07006548} google_protobuf_FieldDescriptorProto_Label;
6549
6550typedef enum {
Josh Haberman94e54b32016-04-14 12:06:09 -07006551 google_protobuf_FieldDescriptorProto_TYPE_DOUBLE = 1,
6552 google_protobuf_FieldDescriptorProto_TYPE_FLOAT = 2,
6553 google_protobuf_FieldDescriptorProto_TYPE_INT64 = 3,
6554 google_protobuf_FieldDescriptorProto_TYPE_UINT64 = 4,
6555 google_protobuf_FieldDescriptorProto_TYPE_INT32 = 5,
6556 google_protobuf_FieldDescriptorProto_TYPE_FIXED64 = 6,
6557 google_protobuf_FieldDescriptorProto_TYPE_FIXED32 = 7,
6558 google_protobuf_FieldDescriptorProto_TYPE_BOOL = 8,
6559 google_protobuf_FieldDescriptorProto_TYPE_STRING = 9,
6560 google_protobuf_FieldDescriptorProto_TYPE_GROUP = 10,
6561 google_protobuf_FieldDescriptorProto_TYPE_MESSAGE = 11,
6562 google_protobuf_FieldDescriptorProto_TYPE_BYTES = 12,
6563 google_protobuf_FieldDescriptorProto_TYPE_UINT32 = 13,
6564 google_protobuf_FieldDescriptorProto_TYPE_ENUM = 14,
6565 google_protobuf_FieldDescriptorProto_TYPE_SFIXED32 = 15,
6566 google_protobuf_FieldDescriptorProto_TYPE_SFIXED64 = 16,
6567 google_protobuf_FieldDescriptorProto_TYPE_SINT32 = 17,
6568 google_protobuf_FieldDescriptorProto_TYPE_SINT64 = 18
Josh Habermane8ed0212015-06-08 17:56:03 -07006569} google_protobuf_FieldDescriptorProto_Type;
6570
6571typedef enum {
Josh Haberman94e54b32016-04-14 12:06:09 -07006572 google_protobuf_FieldOptions_STRING = 0,
6573 google_protobuf_FieldOptions_CORD = 1,
6574 google_protobuf_FieldOptions_STRING_PIECE = 2
Josh Habermane8ed0212015-06-08 17:56:03 -07006575} google_protobuf_FieldOptions_CType;
6576
6577typedef enum {
Josh Haberman94e54b32016-04-14 12:06:09 -07006578 google_protobuf_FieldOptions_JS_NORMAL = 0,
6579 google_protobuf_FieldOptions_JS_STRING = 1,
6580 google_protobuf_FieldOptions_JS_NUMBER = 2
Josh Haberman78da6662016-01-13 19:05:43 -08006581} google_protobuf_FieldOptions_JSType;
6582
6583typedef enum {
Josh Haberman94e54b32016-04-14 12:06:09 -07006584 google_protobuf_FileOptions_SPEED = 1,
6585 google_protobuf_FileOptions_CODE_SIZE = 2,
6586 google_protobuf_FileOptions_LITE_RUNTIME = 3
Josh Habermane8ed0212015-06-08 17:56:03 -07006587} google_protobuf_FileOptions_OptimizeMode;
6588
Josh Haberman94e54b32016-04-14 12:06:09 -07006589/* MessageDefs: call these functions to get a ref to a msgdef. */
6590const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_get(const void *owner);
6591const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_get(const void *owner);
6592const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ReservedRange_get(const void *owner);
6593const upb_msgdef *upbdefs_google_protobuf_EnumDescriptorProto_get(const void *owner);
6594const upb_msgdef *upbdefs_google_protobuf_EnumOptions_get(const void *owner);
6595const upb_msgdef *upbdefs_google_protobuf_EnumValueDescriptorProto_get(const void *owner);
6596const upb_msgdef *upbdefs_google_protobuf_EnumValueOptions_get(const void *owner);
6597const upb_msgdef *upbdefs_google_protobuf_FieldDescriptorProto_get(const void *owner);
6598const upb_msgdef *upbdefs_google_protobuf_FieldOptions_get(const void *owner);
6599const upb_msgdef *upbdefs_google_protobuf_FileDescriptorProto_get(const void *owner);
6600const upb_msgdef *upbdefs_google_protobuf_FileDescriptorSet_get(const void *owner);
6601const upb_msgdef *upbdefs_google_protobuf_FileOptions_get(const void *owner);
6602const upb_msgdef *upbdefs_google_protobuf_MessageOptions_get(const void *owner);
6603const upb_msgdef *upbdefs_google_protobuf_MethodDescriptorProto_get(const void *owner);
6604const upb_msgdef *upbdefs_google_protobuf_MethodOptions_get(const void *owner);
6605const upb_msgdef *upbdefs_google_protobuf_OneofDescriptorProto_get(const void *owner);
6606const upb_msgdef *upbdefs_google_protobuf_ServiceDescriptorProto_get(const void *owner);
6607const upb_msgdef *upbdefs_google_protobuf_ServiceOptions_get(const void *owner);
6608const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo_get(const void *owner);
6609const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo_Location_get(const void *owner);
6610const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption_get(const void *owner);
6611const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption_NamePart_get(const void *owner);
Josh Habermane8ed0212015-06-08 17:56:03 -07006612
Josh Haberman94e54b32016-04-14 12:06:09 -07006613/* EnumDefs: call these functions to get a ref to an enumdef. */
6614const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Label_get(const void *owner);
6615const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Type_get(const void *owner);
6616const upb_enumdef *upbdefs_google_protobuf_FieldOptions_CType_get(const void *owner);
6617const upb_enumdef *upbdefs_google_protobuf_FieldOptions_JSType_get(const void *owner);
6618const upb_enumdef *upbdefs_google_protobuf_FileOptions_OptimizeMode_get(const void *owner);
Josh Habermane8ed0212015-06-08 17:56:03 -07006619
Josh Haberman94e54b32016-04-14 12:06:09 -07006620/* Functions to test whether this message is of a certain type. */
6621UPB_INLINE bool upbdefs_google_protobuf_DescriptorProto_is(const upb_msgdef *m) {
6622 return strcmp(upb_msgdef_fullname(m), "google.protobuf.DescriptorProto") == 0;
Josh Habermane8ed0212015-06-08 17:56:03 -07006623}
Josh Haberman94e54b32016-04-14 12:06:09 -07006624UPB_INLINE bool upbdefs_google_protobuf_DescriptorProto_ExtensionRange_is(const upb_msgdef *m) {
6625 return strcmp(upb_msgdef_fullname(m), "google.protobuf.DescriptorProto.ExtensionRange") == 0;
Josh Habermane8ed0212015-06-08 17:56:03 -07006626}
Josh Haberman94e54b32016-04-14 12:06:09 -07006627UPB_INLINE bool upbdefs_google_protobuf_DescriptorProto_ReservedRange_is(const upb_msgdef *m) {
6628 return strcmp(upb_msgdef_fullname(m), "google.protobuf.DescriptorProto.ReservedRange") == 0;
Josh Haberman78da6662016-01-13 19:05:43 -08006629}
Josh Haberman94e54b32016-04-14 12:06:09 -07006630UPB_INLINE bool upbdefs_google_protobuf_EnumDescriptorProto_is(const upb_msgdef *m) {
6631 return strcmp(upb_msgdef_fullname(m), "google.protobuf.EnumDescriptorProto") == 0;
Josh Habermane8ed0212015-06-08 17:56:03 -07006632}
Josh Haberman94e54b32016-04-14 12:06:09 -07006633UPB_INLINE bool upbdefs_google_protobuf_EnumOptions_is(const upb_msgdef *m) {
6634 return strcmp(upb_msgdef_fullname(m), "google.protobuf.EnumOptions") == 0;
Josh Habermane8ed0212015-06-08 17:56:03 -07006635}
Josh Haberman94e54b32016-04-14 12:06:09 -07006636UPB_INLINE bool upbdefs_google_protobuf_EnumValueDescriptorProto_is(const upb_msgdef *m) {
6637 return strcmp(upb_msgdef_fullname(m), "google.protobuf.EnumValueDescriptorProto") == 0;
Josh Habermane8ed0212015-06-08 17:56:03 -07006638}
Josh Haberman94e54b32016-04-14 12:06:09 -07006639UPB_INLINE bool upbdefs_google_protobuf_EnumValueOptions_is(const upb_msgdef *m) {
6640 return strcmp(upb_msgdef_fullname(m), "google.protobuf.EnumValueOptions") == 0;
Josh Habermane8ed0212015-06-08 17:56:03 -07006641}
Josh Haberman94e54b32016-04-14 12:06:09 -07006642UPB_INLINE bool upbdefs_google_protobuf_FieldDescriptorProto_is(const upb_msgdef *m) {
6643 return strcmp(upb_msgdef_fullname(m), "google.protobuf.FieldDescriptorProto") == 0;
Josh Habermane8ed0212015-06-08 17:56:03 -07006644}
Josh Haberman94e54b32016-04-14 12:06:09 -07006645UPB_INLINE bool upbdefs_google_protobuf_FieldOptions_is(const upb_msgdef *m) {
6646 return strcmp(upb_msgdef_fullname(m), "google.protobuf.FieldOptions") == 0;
Josh Habermane8ed0212015-06-08 17:56:03 -07006647}
Josh Haberman94e54b32016-04-14 12:06:09 -07006648UPB_INLINE bool upbdefs_google_protobuf_FileDescriptorProto_is(const upb_msgdef *m) {
6649 return strcmp(upb_msgdef_fullname(m), "google.protobuf.FileDescriptorProto") == 0;
Josh Habermane8ed0212015-06-08 17:56:03 -07006650}
Josh Haberman94e54b32016-04-14 12:06:09 -07006651UPB_INLINE bool upbdefs_google_protobuf_FileDescriptorSet_is(const upb_msgdef *m) {
6652 return strcmp(upb_msgdef_fullname(m), "google.protobuf.FileDescriptorSet") == 0;
Josh Habermane8ed0212015-06-08 17:56:03 -07006653}
Josh Haberman94e54b32016-04-14 12:06:09 -07006654UPB_INLINE bool upbdefs_google_protobuf_FileOptions_is(const upb_msgdef *m) {
6655 return strcmp(upb_msgdef_fullname(m), "google.protobuf.FileOptions") == 0;
Josh Habermane8ed0212015-06-08 17:56:03 -07006656}
Josh Haberman94e54b32016-04-14 12:06:09 -07006657UPB_INLINE bool upbdefs_google_protobuf_MessageOptions_is(const upb_msgdef *m) {
6658 return strcmp(upb_msgdef_fullname(m), "google.protobuf.MessageOptions") == 0;
Josh Habermane8ed0212015-06-08 17:56:03 -07006659}
Josh Haberman94e54b32016-04-14 12:06:09 -07006660UPB_INLINE bool upbdefs_google_protobuf_MethodDescriptorProto_is(const upb_msgdef *m) {
6661 return strcmp(upb_msgdef_fullname(m), "google.protobuf.MethodDescriptorProto") == 0;
Josh Habermane8ed0212015-06-08 17:56:03 -07006662}
Josh Haberman94e54b32016-04-14 12:06:09 -07006663UPB_INLINE bool upbdefs_google_protobuf_MethodOptions_is(const upb_msgdef *m) {
6664 return strcmp(upb_msgdef_fullname(m), "google.protobuf.MethodOptions") == 0;
Josh Habermane8ed0212015-06-08 17:56:03 -07006665}
Josh Haberman94e54b32016-04-14 12:06:09 -07006666UPB_INLINE bool upbdefs_google_protobuf_OneofDescriptorProto_is(const upb_msgdef *m) {
6667 return strcmp(upb_msgdef_fullname(m), "google.protobuf.OneofDescriptorProto") == 0;
Josh Haberman78da6662016-01-13 19:05:43 -08006668}
Josh Haberman94e54b32016-04-14 12:06:09 -07006669UPB_INLINE bool upbdefs_google_protobuf_ServiceDescriptorProto_is(const upb_msgdef *m) {
6670 return strcmp(upb_msgdef_fullname(m), "google.protobuf.ServiceDescriptorProto") == 0;
Josh Habermane8ed0212015-06-08 17:56:03 -07006671}
Josh Haberman94e54b32016-04-14 12:06:09 -07006672UPB_INLINE bool upbdefs_google_protobuf_ServiceOptions_is(const upb_msgdef *m) {
6673 return strcmp(upb_msgdef_fullname(m), "google.protobuf.ServiceOptions") == 0;
Josh Habermane8ed0212015-06-08 17:56:03 -07006674}
Josh Haberman94e54b32016-04-14 12:06:09 -07006675UPB_INLINE bool upbdefs_google_protobuf_SourceCodeInfo_is(const upb_msgdef *m) {
6676 return strcmp(upb_msgdef_fullname(m), "google.protobuf.SourceCodeInfo") == 0;
Josh Habermane8ed0212015-06-08 17:56:03 -07006677}
Josh Haberman94e54b32016-04-14 12:06:09 -07006678UPB_INLINE bool upbdefs_google_protobuf_SourceCodeInfo_Location_is(const upb_msgdef *m) {
6679 return strcmp(upb_msgdef_fullname(m), "google.protobuf.SourceCodeInfo.Location") == 0;
Josh Habermane8ed0212015-06-08 17:56:03 -07006680}
Josh Haberman94e54b32016-04-14 12:06:09 -07006681UPB_INLINE bool upbdefs_google_protobuf_UninterpretedOption_is(const upb_msgdef *m) {
6682 return strcmp(upb_msgdef_fullname(m), "google.protobuf.UninterpretedOption") == 0;
Josh Habermane8ed0212015-06-08 17:56:03 -07006683}
Josh Haberman94e54b32016-04-14 12:06:09 -07006684UPB_INLINE bool upbdefs_google_protobuf_UninterpretedOption_NamePart_is(const upb_msgdef *m) {
6685 return strcmp(upb_msgdef_fullname(m), "google.protobuf.UninterpretedOption.NamePart") == 0;
6686}
6687
6688/* Functions to test whether this enum is of a certain type. */
6689UPB_INLINE bool upbdefs_google_protobuf_FieldDescriptorProto_Label_is(const upb_enumdef *e) {
6690 return strcmp(upb_enumdef_fullname(e), "google.protobuf.FieldDescriptorProto.Label") == 0;
6691}
6692UPB_INLINE bool upbdefs_google_protobuf_FieldDescriptorProto_Type_is(const upb_enumdef *e) {
6693 return strcmp(upb_enumdef_fullname(e), "google.protobuf.FieldDescriptorProto.Type") == 0;
6694}
6695UPB_INLINE bool upbdefs_google_protobuf_FieldOptions_CType_is(const upb_enumdef *e) {
6696 return strcmp(upb_enumdef_fullname(e), "google.protobuf.FieldOptions.CType") == 0;
6697}
6698UPB_INLINE bool upbdefs_google_protobuf_FieldOptions_JSType_is(const upb_enumdef *e) {
6699 return strcmp(upb_enumdef_fullname(e), "google.protobuf.FieldOptions.JSType") == 0;
6700}
6701UPB_INLINE bool upbdefs_google_protobuf_FileOptions_OptimizeMode_is(const upb_enumdef *e) {
6702 return strcmp(upb_enumdef_fullname(e), "google.protobuf.FileOptions.OptimizeMode") == 0;
Josh Habermane8ed0212015-06-08 17:56:03 -07006703}
6704
6705
Josh Haberman94e54b32016-04-14 12:06:09 -07006706/* Functions to get a fielddef from a msgdef reference. */
6707UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_f_end(const upb_msgdef *m) { assert(upbdefs_google_protobuf_DescriptorProto_ExtensionRange_is(m)); return upb_msgdef_itof(m, 2); }
6708UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_f_start(const upb_msgdef *m) { assert(upbdefs_google_protobuf_DescriptorProto_ExtensionRange_is(m)); return upb_msgdef_itof(m, 1); }
6709UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ReservedRange_f_end(const upb_msgdef *m) { assert(upbdefs_google_protobuf_DescriptorProto_ReservedRange_is(m)); return upb_msgdef_itof(m, 2); }
6710UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ReservedRange_f_start(const upb_msgdef *m) { assert(upbdefs_google_protobuf_DescriptorProto_ReservedRange_is(m)); return upb_msgdef_itof(m, 1); }
6711UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_enum_type(const upb_msgdef *m) { assert(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 4); }
6712UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_extension(const upb_msgdef *m) { assert(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 6); }
6713UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_extension_range(const upb_msgdef *m) { assert(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 5); }
6714UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_field(const upb_msgdef *m) { assert(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 2); }
6715UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_name(const upb_msgdef *m) { assert(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
6716UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_nested_type(const upb_msgdef *m) { assert(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 3); }
6717UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_oneof_decl(const upb_msgdef *m) { assert(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 8); }
6718UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_options(const upb_msgdef *m) { assert(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 7); }
6719UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_reserved_name(const upb_msgdef *m) { assert(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 10); }
6720UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_reserved_range(const upb_msgdef *m) { assert(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 9); }
6721UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_f_name(const upb_msgdef *m) { assert(upbdefs_google_protobuf_EnumDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
6722UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_f_options(const upb_msgdef *m) { assert(upbdefs_google_protobuf_EnumDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); }
6723UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_f_value(const upb_msgdef *m) { assert(upbdefs_google_protobuf_EnumDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); }
6724UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_f_allow_alias(const upb_msgdef *m) { assert(upbdefs_google_protobuf_EnumOptions_is(m)); return upb_msgdef_itof(m, 2); }
6725UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_f_deprecated(const upb_msgdef *m) { assert(upbdefs_google_protobuf_EnumOptions_is(m)); return upb_msgdef_itof(m, 3); }
6726UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_f_uninterpreted_option(const upb_msgdef *m) { assert(upbdefs_google_protobuf_EnumOptions_is(m)); return upb_msgdef_itof(m, 999); }
6727UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_f_name(const upb_msgdef *m) { assert(upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
6728UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_f_number(const upb_msgdef *m) { assert(upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); }
6729UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_f_options(const upb_msgdef *m) { assert(upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); }
6730UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueOptions_f_deprecated(const upb_msgdef *m) { assert(upbdefs_google_protobuf_EnumValueOptions_is(m)); return upb_msgdef_itof(m, 1); }
6731UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueOptions_f_uninterpreted_option(const upb_msgdef *m) { assert(upbdefs_google_protobuf_EnumValueOptions_is(m)); return upb_msgdef_itof(m, 999); }
6732UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_default_value(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 7); }
6733UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_extendee(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); }
6734UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_json_name(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 10); }
6735UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_label(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 4); }
6736UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_name(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
6737UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_number(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); }
6738UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_oneof_index(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 9); }
6739UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_options(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 8); }
6740UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_type(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 5); }
6741UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_type_name(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 6); }
6742UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_ctype(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 1); }
6743UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_deprecated(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 3); }
6744UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_jstype(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 6); }
6745UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_lazy(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 5); }
6746UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_packed(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 2); }
6747UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_uninterpreted_option(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 999); }
6748UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_weak(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 10); }
6749UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_dependency(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); }
6750UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_enum_type(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 5); }
6751UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_extension(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 7); }
6752UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_message_type(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 4); }
6753UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_name(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
6754UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_options(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 8); }
6755UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_package(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); }
6756UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_public_dependency(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 10); }
6757UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_service(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 6); }
6758UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_source_code_info(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 9); }
6759UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_syntax(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 12); }
6760UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_weak_dependency(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 11); }
6761UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorSet_f_file(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileDescriptorSet_is(m)); return upb_msgdef_itof(m, 1); }
6762UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_cc_enable_arenas(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 31); }
6763UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_cc_generic_services(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 16); }
6764UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_csharp_namespace(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 37); }
6765UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_deprecated(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 23); }
6766UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_go_package(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 11); }
6767UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_generate_equals_and_hash(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 20); }
6768UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_generic_services(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 17); }
6769UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_multiple_files(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 10); }
6770UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_outer_classname(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 8); }
6771UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_package(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 1); }
6772UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_string_check_utf8(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 27); }
6773UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_javanano_use_deprecated_package(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 38); }
6774UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_objc_class_prefix(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 36); }
6775UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_optimize_for(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 9); }
6776UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_py_generic_services(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 18); }
6777UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_uninterpreted_option(const upb_msgdef *m) { assert(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 999); }
6778UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_deprecated(const upb_msgdef *m) { assert(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 3); }
6779UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_map_entry(const upb_msgdef *m) { assert(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 7); }
6780UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_message_set_wire_format(const upb_msgdef *m) { assert(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 1); }
6781UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_no_standard_descriptor_accessor(const upb_msgdef *m) { assert(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 2); }
6782UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_uninterpreted_option(const upb_msgdef *m) { assert(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 999); }
6783UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_client_streaming(const upb_msgdef *m) { assert(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 5); }
6784UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_input_type(const upb_msgdef *m) { assert(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); }
6785UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_name(const upb_msgdef *m) { assert(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
6786UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_options(const upb_msgdef *m) { assert(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 4); }
6787UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_output_type(const upb_msgdef *m) { assert(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); }
6788UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_server_streaming(const upb_msgdef *m) { assert(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 6); }
6789UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodOptions_f_deprecated(const upb_msgdef *m) { assert(upbdefs_google_protobuf_MethodOptions_is(m)); return upb_msgdef_itof(m, 33); }
6790UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodOptions_f_uninterpreted_option(const upb_msgdef *m) { assert(upbdefs_google_protobuf_MethodOptions_is(m)); return upb_msgdef_itof(m, 999); }
6791UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_OneofDescriptorProto_f_name(const upb_msgdef *m) { assert(upbdefs_google_protobuf_OneofDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
6792UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_f_method(const upb_msgdef *m) { assert(upbdefs_google_protobuf_ServiceDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); }
6793UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_f_name(const upb_msgdef *m) { assert(upbdefs_google_protobuf_ServiceDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
6794UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_f_options(const upb_msgdef *m) { assert(upbdefs_google_protobuf_ServiceDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); }
6795UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceOptions_f_deprecated(const upb_msgdef *m) { assert(upbdefs_google_protobuf_ServiceOptions_is(m)); return upb_msgdef_itof(m, 33); }
6796UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceOptions_f_uninterpreted_option(const upb_msgdef *m) { assert(upbdefs_google_protobuf_ServiceOptions_is(m)); return upb_msgdef_itof(m, 999); }
6797UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_leading_comments(const upb_msgdef *m) { assert(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 3); }
6798UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_leading_detached_comments(const upb_msgdef *m) { assert(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 6); }
6799UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_path(const upb_msgdef *m) { assert(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 1); }
6800UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_span(const upb_msgdef *m) { assert(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 2); }
6801UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_trailing_comments(const upb_msgdef *m) { assert(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 4); }
6802UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_f_location(const upb_msgdef *m) { assert(upbdefs_google_protobuf_SourceCodeInfo_is(m)); return upb_msgdef_itof(m, 1); }
6803UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_NamePart_f_is_extension(const upb_msgdef *m) { assert(upbdefs_google_protobuf_UninterpretedOption_NamePart_is(m)); return upb_msgdef_itof(m, 2); }
6804UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_NamePart_f_name_part(const upb_msgdef *m) { assert(upbdefs_google_protobuf_UninterpretedOption_NamePart_is(m)); return upb_msgdef_itof(m, 1); }
6805UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_aggregate_value(const upb_msgdef *m) { assert(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 8); }
6806UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_double_value(const upb_msgdef *m) { assert(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 6); }
6807UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_identifier_value(const upb_msgdef *m) { assert(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 3); }
6808UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_name(const upb_msgdef *m) { assert(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 2); }
6809UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_negative_int_value(const upb_msgdef *m) { assert(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 5); }
6810UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_positive_int_value(const upb_msgdef *m) { assert(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 4); }
6811UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_string_value(const upb_msgdef *m) { assert(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 7); }
Josh Habermane8ed0212015-06-08 17:56:03 -07006812
6813UPB_END_EXTERN_C
6814
6815#ifdef __cplusplus
6816
6817namespace upbdefs {
6818namespace google {
6819namespace protobuf {
Josh Haberman94e54b32016-04-14 12:06:09 -07006820
6821class DescriptorProto : public upb::reffed_ptr<const upb::MessageDef> {
6822 public:
6823 DescriptorProto(const upb::MessageDef* m, const void *ref_donor = NULL)
6824 : reffed_ptr(m, ref_donor) {
6825 assert(upbdefs_google_protobuf_DescriptorProto_is(m));
6826 }
6827
6828 static DescriptorProto get() {
6829 const upb::MessageDef* m = upbdefs_google_protobuf_DescriptorProto_get(&m);
6830 return DescriptorProto(m, &m);
6831 }
6832
6833 class ExtensionRange : public upb::reffed_ptr<const upb::MessageDef> {
6834 public:
6835 ExtensionRange(const upb::MessageDef* m, const void *ref_donor = NULL)
6836 : reffed_ptr(m, ref_donor) {
6837 assert(upbdefs_google_protobuf_DescriptorProto_ExtensionRange_is(m));
6838 }
6839
6840 static ExtensionRange get() {
6841 const upb::MessageDef* m = upbdefs_google_protobuf_DescriptorProto_ExtensionRange_get(&m);
6842 return ExtensionRange(m, &m);
6843 }
6844 };
6845
6846 class ReservedRange : public upb::reffed_ptr<const upb::MessageDef> {
6847 public:
6848 ReservedRange(const upb::MessageDef* m, const void *ref_donor = NULL)
6849 : reffed_ptr(m, ref_donor) {
6850 assert(upbdefs_google_protobuf_DescriptorProto_ReservedRange_is(m));
6851 }
6852
6853 static ReservedRange get() {
6854 const upb::MessageDef* m = upbdefs_google_protobuf_DescriptorProto_ReservedRange_get(&m);
6855 return ReservedRange(m, &m);
6856 }
6857 };
6858};
6859
6860class EnumDescriptorProto : public upb::reffed_ptr<const upb::MessageDef> {
6861 public:
6862 EnumDescriptorProto(const upb::MessageDef* m, const void *ref_donor = NULL)
6863 : reffed_ptr(m, ref_donor) {
6864 assert(upbdefs_google_protobuf_EnumDescriptorProto_is(m));
6865 }
6866
6867 static EnumDescriptorProto get() {
6868 const upb::MessageDef* m = upbdefs_google_protobuf_EnumDescriptorProto_get(&m);
6869 return EnumDescriptorProto(m, &m);
6870 }
6871};
6872
6873class EnumOptions : public upb::reffed_ptr<const upb::MessageDef> {
6874 public:
6875 EnumOptions(const upb::MessageDef* m, const void *ref_donor = NULL)
6876 : reffed_ptr(m, ref_donor) {
6877 assert(upbdefs_google_protobuf_EnumOptions_is(m));
6878 }
6879
6880 static EnumOptions get() {
6881 const upb::MessageDef* m = upbdefs_google_protobuf_EnumOptions_get(&m);
6882 return EnumOptions(m, &m);
6883 }
6884};
6885
6886class EnumValueDescriptorProto : public upb::reffed_ptr<const upb::MessageDef> {
6887 public:
6888 EnumValueDescriptorProto(const upb::MessageDef* m, const void *ref_donor = NULL)
6889 : reffed_ptr(m, ref_donor) {
6890 assert(upbdefs_google_protobuf_EnumValueDescriptorProto_is(m));
6891 }
6892
6893 static EnumValueDescriptorProto get() {
6894 const upb::MessageDef* m = upbdefs_google_protobuf_EnumValueDescriptorProto_get(&m);
6895 return EnumValueDescriptorProto(m, &m);
6896 }
6897};
6898
6899class EnumValueOptions : public upb::reffed_ptr<const upb::MessageDef> {
6900 public:
6901 EnumValueOptions(const upb::MessageDef* m, const void *ref_donor = NULL)
6902 : reffed_ptr(m, ref_donor) {
6903 assert(upbdefs_google_protobuf_EnumValueOptions_is(m));
6904 }
6905
6906 static EnumValueOptions get() {
6907 const upb::MessageDef* m = upbdefs_google_protobuf_EnumValueOptions_get(&m);
6908 return EnumValueOptions(m, &m);
6909 }
6910};
6911
6912class FieldDescriptorProto : public upb::reffed_ptr<const upb::MessageDef> {
6913 public:
6914 FieldDescriptorProto(const upb::MessageDef* m, const void *ref_donor = NULL)
6915 : reffed_ptr(m, ref_donor) {
6916 assert(upbdefs_google_protobuf_FieldDescriptorProto_is(m));
6917 }
6918
6919 static FieldDescriptorProto get() {
6920 const upb::MessageDef* m = upbdefs_google_protobuf_FieldDescriptorProto_get(&m);
6921 return FieldDescriptorProto(m, &m);
6922 }
6923
6924 class Label : public upb::reffed_ptr<const upb::EnumDef> {
6925 public:
6926 Label(const upb::EnumDef* e, const void *ref_donor = NULL)
6927 : reffed_ptr(e, ref_donor) {
6928 assert(upbdefs_google_protobuf_FieldDescriptorProto_Label_is(e));
6929 }
6930 static Label get() {
6931 const upb::EnumDef* e = upbdefs_google_protobuf_FieldDescriptorProto_Label_get(&e);
6932 return Label(e, &e);
6933 }
6934 };
6935
6936 class Type : public upb::reffed_ptr<const upb::EnumDef> {
6937 public:
6938 Type(const upb::EnumDef* e, const void *ref_donor = NULL)
6939 : reffed_ptr(e, ref_donor) {
6940 assert(upbdefs_google_protobuf_FieldDescriptorProto_Type_is(e));
6941 }
6942 static Type get() {
6943 const upb::EnumDef* e = upbdefs_google_protobuf_FieldDescriptorProto_Type_get(&e);
6944 return Type(e, &e);
6945 }
6946 };
6947};
6948
6949class FieldOptions : public upb::reffed_ptr<const upb::MessageDef> {
6950 public:
6951 FieldOptions(const upb::MessageDef* m, const void *ref_donor = NULL)
6952 : reffed_ptr(m, ref_donor) {
6953 assert(upbdefs_google_protobuf_FieldOptions_is(m));
6954 }
6955
6956 static FieldOptions get() {
6957 const upb::MessageDef* m = upbdefs_google_protobuf_FieldOptions_get(&m);
6958 return FieldOptions(m, &m);
6959 }
6960
6961 class CType : public upb::reffed_ptr<const upb::EnumDef> {
6962 public:
6963 CType(const upb::EnumDef* e, const void *ref_donor = NULL)
6964 : reffed_ptr(e, ref_donor) {
6965 assert(upbdefs_google_protobuf_FieldOptions_CType_is(e));
6966 }
6967 static CType get() {
6968 const upb::EnumDef* e = upbdefs_google_protobuf_FieldOptions_CType_get(&e);
6969 return CType(e, &e);
6970 }
6971 };
6972
6973 class JSType : public upb::reffed_ptr<const upb::EnumDef> {
6974 public:
6975 JSType(const upb::EnumDef* e, const void *ref_donor = NULL)
6976 : reffed_ptr(e, ref_donor) {
6977 assert(upbdefs_google_protobuf_FieldOptions_JSType_is(e));
6978 }
6979 static JSType get() {
6980 const upb::EnumDef* e = upbdefs_google_protobuf_FieldOptions_JSType_get(&e);
6981 return JSType(e, &e);
6982 }
6983 };
6984};
6985
6986class FileDescriptorProto : public upb::reffed_ptr<const upb::MessageDef> {
6987 public:
6988 FileDescriptorProto(const upb::MessageDef* m, const void *ref_donor = NULL)
6989 : reffed_ptr(m, ref_donor) {
6990 assert(upbdefs_google_protobuf_FileDescriptorProto_is(m));
6991 }
6992
6993 static FileDescriptorProto get() {
6994 const upb::MessageDef* m = upbdefs_google_protobuf_FileDescriptorProto_get(&m);
6995 return FileDescriptorProto(m, &m);
6996 }
6997};
6998
6999class FileDescriptorSet : public upb::reffed_ptr<const upb::MessageDef> {
7000 public:
7001 FileDescriptorSet(const upb::MessageDef* m, const void *ref_donor = NULL)
7002 : reffed_ptr(m, ref_donor) {
7003 assert(upbdefs_google_protobuf_FileDescriptorSet_is(m));
7004 }
7005
7006 static FileDescriptorSet get() {
7007 const upb::MessageDef* m = upbdefs_google_protobuf_FileDescriptorSet_get(&m);
7008 return FileDescriptorSet(m, &m);
7009 }
7010};
7011
7012class FileOptions : public upb::reffed_ptr<const upb::MessageDef> {
7013 public:
7014 FileOptions(const upb::MessageDef* m, const void *ref_donor = NULL)
7015 : reffed_ptr(m, ref_donor) {
7016 assert(upbdefs_google_protobuf_FileOptions_is(m));
7017 }
7018
7019 static FileOptions get() {
7020 const upb::MessageDef* m = upbdefs_google_protobuf_FileOptions_get(&m);
7021 return FileOptions(m, &m);
7022 }
7023
7024 class OptimizeMode : public upb::reffed_ptr<const upb::EnumDef> {
7025 public:
7026 OptimizeMode(const upb::EnumDef* e, const void *ref_donor = NULL)
7027 : reffed_ptr(e, ref_donor) {
7028 assert(upbdefs_google_protobuf_FileOptions_OptimizeMode_is(e));
7029 }
7030 static OptimizeMode get() {
7031 const upb::EnumDef* e = upbdefs_google_protobuf_FileOptions_OptimizeMode_get(&e);
7032 return OptimizeMode(e, &e);
7033 }
7034 };
7035};
7036
7037class MessageOptions : public upb::reffed_ptr<const upb::MessageDef> {
7038 public:
7039 MessageOptions(const upb::MessageDef* m, const void *ref_donor = NULL)
7040 : reffed_ptr(m, ref_donor) {
7041 assert(upbdefs_google_protobuf_MessageOptions_is(m));
7042 }
7043
7044 static MessageOptions get() {
7045 const upb::MessageDef* m = upbdefs_google_protobuf_MessageOptions_get(&m);
7046 return MessageOptions(m, &m);
7047 }
7048};
7049
7050class MethodDescriptorProto : public upb::reffed_ptr<const upb::MessageDef> {
7051 public:
7052 MethodDescriptorProto(const upb::MessageDef* m, const void *ref_donor = NULL)
7053 : reffed_ptr(m, ref_donor) {
7054 assert(upbdefs_google_protobuf_MethodDescriptorProto_is(m));
7055 }
7056
7057 static MethodDescriptorProto get() {
7058 const upb::MessageDef* m = upbdefs_google_protobuf_MethodDescriptorProto_get(&m);
7059 return MethodDescriptorProto(m, &m);
7060 }
7061};
7062
7063class MethodOptions : public upb::reffed_ptr<const upb::MessageDef> {
7064 public:
7065 MethodOptions(const upb::MessageDef* m, const void *ref_donor = NULL)
7066 : reffed_ptr(m, ref_donor) {
7067 assert(upbdefs_google_protobuf_MethodOptions_is(m));
7068 }
7069
7070 static MethodOptions get() {
7071 const upb::MessageDef* m = upbdefs_google_protobuf_MethodOptions_get(&m);
7072 return MethodOptions(m, &m);
7073 }
7074};
7075
7076class OneofDescriptorProto : public upb::reffed_ptr<const upb::MessageDef> {
7077 public:
7078 OneofDescriptorProto(const upb::MessageDef* m, const void *ref_donor = NULL)
7079 : reffed_ptr(m, ref_donor) {
7080 assert(upbdefs_google_protobuf_OneofDescriptorProto_is(m));
7081 }
7082
7083 static OneofDescriptorProto get() {
7084 const upb::MessageDef* m = upbdefs_google_protobuf_OneofDescriptorProto_get(&m);
7085 return OneofDescriptorProto(m, &m);
7086 }
7087};
7088
7089class ServiceDescriptorProto : public upb::reffed_ptr<const upb::MessageDef> {
7090 public:
7091 ServiceDescriptorProto(const upb::MessageDef* m, const void *ref_donor = NULL)
7092 : reffed_ptr(m, ref_donor) {
7093 assert(upbdefs_google_protobuf_ServiceDescriptorProto_is(m));
7094 }
7095
7096 static ServiceDescriptorProto get() {
7097 const upb::MessageDef* m = upbdefs_google_protobuf_ServiceDescriptorProto_get(&m);
7098 return ServiceDescriptorProto(m, &m);
7099 }
7100};
7101
7102class ServiceOptions : public upb::reffed_ptr<const upb::MessageDef> {
7103 public:
7104 ServiceOptions(const upb::MessageDef* m, const void *ref_donor = NULL)
7105 : reffed_ptr(m, ref_donor) {
7106 assert(upbdefs_google_protobuf_ServiceOptions_is(m));
7107 }
7108
7109 static ServiceOptions get() {
7110 const upb::MessageDef* m = upbdefs_google_protobuf_ServiceOptions_get(&m);
7111 return ServiceOptions(m, &m);
7112 }
7113};
7114
7115class SourceCodeInfo : public upb::reffed_ptr<const upb::MessageDef> {
7116 public:
7117 SourceCodeInfo(const upb::MessageDef* m, const void *ref_donor = NULL)
7118 : reffed_ptr(m, ref_donor) {
7119 assert(upbdefs_google_protobuf_SourceCodeInfo_is(m));
7120 }
7121
7122 static SourceCodeInfo get() {
7123 const upb::MessageDef* m = upbdefs_google_protobuf_SourceCodeInfo_get(&m);
7124 return SourceCodeInfo(m, &m);
7125 }
7126
7127 class Location : public upb::reffed_ptr<const upb::MessageDef> {
7128 public:
7129 Location(const upb::MessageDef* m, const void *ref_donor = NULL)
7130 : reffed_ptr(m, ref_donor) {
7131 assert(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m));
7132 }
7133
7134 static Location get() {
7135 const upb::MessageDef* m = upbdefs_google_protobuf_SourceCodeInfo_Location_get(&m);
7136 return Location(m, &m);
7137 }
7138 };
7139};
7140
7141class UninterpretedOption : public upb::reffed_ptr<const upb::MessageDef> {
7142 public:
7143 UninterpretedOption(const upb::MessageDef* m, const void *ref_donor = NULL)
7144 : reffed_ptr(m, ref_donor) {
7145 assert(upbdefs_google_protobuf_UninterpretedOption_is(m));
7146 }
7147
7148 static UninterpretedOption get() {
7149 const upb::MessageDef* m = upbdefs_google_protobuf_UninterpretedOption_get(&m);
7150 return UninterpretedOption(m, &m);
7151 }
7152
7153 class NamePart : public upb::reffed_ptr<const upb::MessageDef> {
7154 public:
7155 NamePart(const upb::MessageDef* m, const void *ref_donor = NULL)
7156 : reffed_ptr(m, ref_donor) {
7157 assert(upbdefs_google_protobuf_UninterpretedOption_NamePart_is(m));
7158 }
7159
7160 static NamePart get() {
7161 const upb::MessageDef* m = upbdefs_google_protobuf_UninterpretedOption_NamePart_get(&m);
7162 return NamePart(m, &m);
7163 }
7164 };
7165};
7166
Josh Habermane8ed0212015-06-08 17:56:03 -07007167} /* namespace protobuf */
7168} /* namespace google */
Josh Habermane8ed0212015-06-08 17:56:03 -07007169} /* namespace upbdefs */
7170
Josh Haberman94e54b32016-04-14 12:06:09 -07007171#endif /* __cplusplus */
Josh Habermane8ed0212015-06-08 17:56:03 -07007172
Josh Haberman94e54b32016-04-14 12:06:09 -07007173#endif /* UPB_DESCRIPTOR_DESCRIPTOR_PROTO_UPB_H_ */
Chris Fallin91473dc2014-12-12 15:58:26 -08007174/*
Josh Haberman181c7f22015-07-15 11:05:10 -07007175** Internal-only definitions for the decoder.
7176*/
Chris Fallin91473dc2014-12-12 15:58:26 -08007177
7178#ifndef UPB_DECODER_INT_H_
7179#define UPB_DECODER_INT_H_
7180
7181#include <stdlib.h>
7182/*
Josh Haberman181c7f22015-07-15 11:05:10 -07007183** upb::pb::Decoder
7184**
7185** A high performance, streaming, resumable decoder for the binary protobuf
7186** format.
7187**
7188** This interface works the same regardless of what decoder backend is being
7189** used. A client of this class does not need to know whether decoding is using
7190** a JITted decoder (DynASM, LLVM, etc) or an interpreted decoder. By default,
7191** it will always use the fastest available decoder. However, you can call
7192** set_allow_jit(false) to disable any JIT decoder that might be available.
7193** This is primarily useful for testing purposes.
7194*/
Chris Fallin91473dc2014-12-12 15:58:26 -08007195
7196#ifndef UPB_DECODER_H_
7197#define UPB_DECODER_H_
7198
7199
7200#ifdef __cplusplus
7201namespace upb {
7202namespace pb {
7203class CodeCache;
7204class Decoder;
7205class DecoderMethod;
7206class DecoderMethodOptions;
Josh Habermane8ed0212015-06-08 17:56:03 -07007207} /* namespace pb */
7208} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08007209#endif
7210
Josh Habermane8ed0212015-06-08 17:56:03 -07007211UPB_DECLARE_TYPE(upb::pb::CodeCache, upb_pbcodecache)
7212UPB_DECLARE_TYPE(upb::pb::Decoder, upb_pbdecoder)
7213UPB_DECLARE_TYPE(upb::pb::DecoderMethodOptions, upb_pbdecodermethodopts)
Chris Fallin91473dc2014-12-12 15:58:26 -08007214
Josh Habermane8ed0212015-06-08 17:56:03 -07007215UPB_DECLARE_DERIVED_TYPE(upb::pb::DecoderMethod, upb::RefCounted,
7216 upb_pbdecodermethod, upb_refcounted)
7217
Josh Haberman78da6662016-01-13 19:05:43 -08007218/* The maximum number of bytes we are required to buffer internally between
7219 * calls to the decoder. The value is 14: a 5 byte unknown tag plus ten-byte
7220 * varint, less one because we are buffering an incomplete value.
7221 *
7222 * Should only be used by unit tests. */
7223#define UPB_DECODER_MAX_RESIDUAL_BYTES 14
7224
Josh Habermane8ed0212015-06-08 17:56:03 -07007225#ifdef __cplusplus
7226
7227/* The parameters one uses to construct a DecoderMethod.
7228 * TODO(haberman): move allowjit here? Seems more convenient for users.
7229 * TODO(haberman): move this to be heap allocated for ABI stability. */
7230class upb::pb::DecoderMethodOptions {
Chris Fallin91473dc2014-12-12 15:58:26 -08007231 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07007232 /* Parameter represents the destination handlers that this method will push
7233 * to. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007234 explicit DecoderMethodOptions(const Handlers* dest_handlers);
7235
Josh Habermane8ed0212015-06-08 17:56:03 -07007236 /* Should the decoder push submessages to lazy handlers for fields that have
7237 * them? The caller should set this iff the lazy handlers expect data that is
7238 * in protobuf binary format and the caller wishes to lazy parse it. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007239 void set_lazy(bool lazy);
Josh Habermane8ed0212015-06-08 17:56:03 -07007240#else
7241struct upb_pbdecodermethodopts {
7242#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08007243 const upb_handlers *handlers;
7244 bool lazy;
Josh Habermane8ed0212015-06-08 17:56:03 -07007245};
Chris Fallin91473dc2014-12-12 15:58:26 -08007246
Josh Habermane8ed0212015-06-08 17:56:03 -07007247#ifdef __cplusplus
7248
7249/* Represents the code to parse a protobuf according to a destination
7250 * Handlers. */
7251class upb::pb::DecoderMethod {
Chris Fallin91473dc2014-12-12 15:58:26 -08007252 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07007253 /* Include base methods from upb::ReferenceCounted. */
7254 UPB_REFCOUNTED_CPPMETHODS
Chris Fallin91473dc2014-12-12 15:58:26 -08007255
Josh Habermane8ed0212015-06-08 17:56:03 -07007256 /* The destination handlers that are statically bound to this method.
7257 * This method is only capable of outputting to a sink that uses these
7258 * handlers. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007259 const Handlers* dest_handlers() const;
7260
Josh Habermane8ed0212015-06-08 17:56:03 -07007261 /* The input handlers for this decoder method. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007262 const BytesHandler* input_handler() const;
7263
Josh Habermane8ed0212015-06-08 17:56:03 -07007264 /* Whether this method is native. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007265 bool is_native() const;
7266
Josh Habermane8ed0212015-06-08 17:56:03 -07007267 /* Convenience method for generating a DecoderMethod without explicitly
7268 * creating a CodeCache. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007269 static reffed_ptr<const DecoderMethod> New(const DecoderMethodOptions& opts);
7270
7271 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07007272 UPB_DISALLOW_POD_OPS(DecoderMethod, upb::pb::DecoderMethod)
7273};
Chris Fallin91473dc2014-12-12 15:58:26 -08007274
Josh Habermane8ed0212015-06-08 17:56:03 -07007275#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08007276
Josh Habermane8ed0212015-06-08 17:56:03 -07007277/* Preallocation hint: decoder won't allocate more bytes than this when first
7278 * constructed. This hint may be an overestimate for some build configurations.
7279 * But if the decoder library is upgraded without recompiling the application,
7280 * it may be an underestimate. */
Josh Haberman5bdf4a42015-08-03 15:51:31 -07007281#define UPB_PB_DECODER_SIZE 4408
Chris Fallind3262772015-05-14 18:24:26 -07007282
7283#ifdef __cplusplus
7284
Josh Habermane8ed0212015-06-08 17:56:03 -07007285/* A Decoder receives binary protobuf data on its input sink and pushes the
7286 * decoded data to its output sink. */
Chris Fallind3262772015-05-14 18:24:26 -07007287class upb::pb::Decoder {
Chris Fallin91473dc2014-12-12 15:58:26 -08007288 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07007289 /* Constructs a decoder instance for the given method, which must outlive this
7290 * decoder. Any errors during parsing will be set on the given status, which
7291 * must also outlive this decoder.
7292 *
7293 * The sink must match the given method. */
Chris Fallind3262772015-05-14 18:24:26 -07007294 static Decoder* Create(Environment* env, const DecoderMethod* method,
7295 Sink* output);
Chris Fallin91473dc2014-12-12 15:58:26 -08007296
Josh Habermane8ed0212015-06-08 17:56:03 -07007297 /* Returns the DecoderMethod this decoder is parsing from. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007298 const DecoderMethod* method() const;
7299
Josh Habermane8ed0212015-06-08 17:56:03 -07007300 /* The sink on which this decoder receives input. */
Chris Fallind3262772015-05-14 18:24:26 -07007301 BytesSink* input();
Chris Fallin91473dc2014-12-12 15:58:26 -08007302
Josh Habermane8ed0212015-06-08 17:56:03 -07007303 /* Returns number of bytes successfully parsed.
7304 *
7305 * This can be useful for determining the stream position where an error
7306 * occurred.
7307 *
7308 * This value may not be up-to-date when called from inside a parsing
7309 * callback. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007310 uint64_t BytesParsed() const;
7311
Josh Habermane8ed0212015-06-08 17:56:03 -07007312 /* Gets/sets the parsing nexting limit. If the total number of nested
7313 * submessages and repeated fields hits this limit, parsing will fail. This
7314 * is a resource limit that controls the amount of memory used by the parsing
7315 * stack.
7316 *
7317 * Setting the limit will fail if the parser is currently suspended at a depth
7318 * greater than this, or if memory allocation of the stack fails. */
Chris Fallind3262772015-05-14 18:24:26 -07007319 size_t max_nesting() const;
7320 bool set_max_nesting(size_t max);
Chris Fallin91473dc2014-12-12 15:58:26 -08007321
Chris Fallind3262772015-05-14 18:24:26 -07007322 void Reset();
7323
7324 static const size_t kSize = UPB_PB_DECODER_SIZE;
Chris Fallin91473dc2014-12-12 15:58:26 -08007325
7326 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07007327 UPB_DISALLOW_POD_OPS(Decoder, upb::pb::Decoder)
Chris Fallind3262772015-05-14 18:24:26 -07007328};
Chris Fallin91473dc2014-12-12 15:58:26 -08007329
Josh Habermane8ed0212015-06-08 17:56:03 -07007330#endif /* __cplusplus */
Chris Fallin91473dc2014-12-12 15:58:26 -08007331
Josh Habermane8ed0212015-06-08 17:56:03 -07007332#ifdef __cplusplus
7333
7334/* A class for caching protobuf processing code, whether bytecode for the
7335 * interpreted decoder or machine code for the JIT.
7336 *
7337 * This class is not thread-safe.
7338 *
7339 * TODO(haberman): move this to be heap allocated for ABI stability. */
7340class upb::pb::CodeCache {
Chris Fallin91473dc2014-12-12 15:58:26 -08007341 public:
7342 CodeCache();
7343 ~CodeCache();
7344
Josh Habermane8ed0212015-06-08 17:56:03 -07007345 /* Whether the cache is allowed to generate machine code. Defaults to true.
7346 * There is no real reason to turn it off except for testing or if you are
7347 * having a specific problem with the JIT.
7348 *
7349 * Note that allow_jit = true does not *guarantee* that the code will be JIT
7350 * compiled. If this platform is not supported or the JIT was not compiled
7351 * in, the code may still be interpreted. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007352 bool allow_jit() const;
7353
Josh Habermane8ed0212015-06-08 17:56:03 -07007354 /* This may only be called when the object is first constructed, and prior to
7355 * any code generation, otherwise returns false and does nothing. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007356 bool set_allow_jit(bool allow);
7357
Josh Habermane8ed0212015-06-08 17:56:03 -07007358 /* Returns a DecoderMethod that can push data to the given handlers.
7359 * If a suitable method already exists, it will be returned from the cache.
7360 *
7361 * Specifying the destination handlers here allows the DecoderMethod to be
7362 * statically bound to the destination handlers if possible, which can allow
7363 * more efficient decoding. However the returned method may or may not
7364 * actually be statically bound. But in all cases, the returned method can
7365 * push data to the given handlers. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007366 const DecoderMethod *GetDecoderMethod(const DecoderMethodOptions& opts);
7367
Josh Habermane8ed0212015-06-08 17:56:03 -07007368 /* If/when someone needs to explicitly create a dynamically-bound
7369 * DecoderMethod*, we can add a method to get it here. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007370
7371 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07007372 UPB_DISALLOW_COPY_AND_ASSIGN(CodeCache)
7373#else
7374struct upb_pbcodecache {
7375#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08007376 bool allow_jit_;
7377
Josh Habermane8ed0212015-06-08 17:56:03 -07007378 /* Array of mgroups. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007379 upb_inttable groups;
Josh Habermane8ed0212015-06-08 17:56:03 -07007380};
Chris Fallin91473dc2014-12-12 15:58:26 -08007381
Josh Habermane8ed0212015-06-08 17:56:03 -07007382UPB_BEGIN_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08007383
Chris Fallind3262772015-05-14 18:24:26 -07007384upb_pbdecoder *upb_pbdecoder_create(upb_env *e,
7385 const upb_pbdecodermethod *method,
7386 upb_sink *output);
Chris Fallin91473dc2014-12-12 15:58:26 -08007387const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d);
Chris Fallin91473dc2014-12-12 15:58:26 -08007388upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d);
7389uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d);
Chris Fallind3262772015-05-14 18:24:26 -07007390size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d);
7391bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max);
7392void upb_pbdecoder_reset(upb_pbdecoder *d);
Chris Fallin91473dc2014-12-12 15:58:26 -08007393
7394void upb_pbdecodermethodopts_init(upb_pbdecodermethodopts *opts,
7395 const upb_handlers *h);
7396void upb_pbdecodermethodopts_setlazy(upb_pbdecodermethodopts *opts, bool lazy);
7397
Josh Habermane8ed0212015-06-08 17:56:03 -07007398
7399/* Include refcounted methods like upb_pbdecodermethod_ref(). */
7400UPB_REFCOUNTED_CMETHODS(upb_pbdecodermethod, upb_pbdecodermethod_upcast)
7401
Chris Fallin91473dc2014-12-12 15:58:26 -08007402const upb_handlers *upb_pbdecodermethod_desthandlers(
7403 const upb_pbdecodermethod *m);
7404const upb_byteshandler *upb_pbdecodermethod_inputhandler(
7405 const upb_pbdecodermethod *m);
7406bool upb_pbdecodermethod_isnative(const upb_pbdecodermethod *m);
7407const upb_pbdecodermethod *upb_pbdecodermethod_new(
7408 const upb_pbdecodermethodopts *opts, const void *owner);
7409
7410void upb_pbcodecache_init(upb_pbcodecache *c);
7411void upb_pbcodecache_uninit(upb_pbcodecache *c);
7412bool upb_pbcodecache_allowjit(const upb_pbcodecache *c);
7413bool upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow);
7414const upb_pbdecodermethod *upb_pbcodecache_getdecodermethod(
7415 upb_pbcodecache *c, const upb_pbdecodermethodopts *opts);
7416
Josh Habermane8ed0212015-06-08 17:56:03 -07007417UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08007418
7419#ifdef __cplusplus
7420
7421namespace upb {
7422
7423namespace pb {
7424
Josh Habermane8ed0212015-06-08 17:56:03 -07007425/* static */
Chris Fallind3262772015-05-14 18:24:26 -07007426inline Decoder* Decoder::Create(Environment* env, const DecoderMethod* m,
7427 Sink* sink) {
7428 return upb_pbdecoder_create(env, m, sink);
Chris Fallin91473dc2014-12-12 15:58:26 -08007429}
7430inline const DecoderMethod* Decoder::method() const {
7431 return upb_pbdecoder_method(this);
7432}
Chris Fallind3262772015-05-14 18:24:26 -07007433inline BytesSink* Decoder::input() {
7434 return upb_pbdecoder_input(this);
Chris Fallin91473dc2014-12-12 15:58:26 -08007435}
7436inline uint64_t Decoder::BytesParsed() const {
7437 return upb_pbdecoder_bytesparsed(this);
7438}
Chris Fallind3262772015-05-14 18:24:26 -07007439inline size_t Decoder::max_nesting() const {
7440 return upb_pbdecoder_maxnesting(this);
Chris Fallin91473dc2014-12-12 15:58:26 -08007441}
Chris Fallind3262772015-05-14 18:24:26 -07007442inline bool Decoder::set_max_nesting(size_t max) {
7443 return upb_pbdecoder_setmaxnesting(this, max);
Chris Fallin91473dc2014-12-12 15:58:26 -08007444}
Chris Fallind3262772015-05-14 18:24:26 -07007445inline void Decoder::Reset() { upb_pbdecoder_reset(this); }
Chris Fallin91473dc2014-12-12 15:58:26 -08007446
7447inline DecoderMethodOptions::DecoderMethodOptions(const Handlers* h) {
7448 upb_pbdecodermethodopts_init(this, h);
7449}
7450inline void DecoderMethodOptions::set_lazy(bool lazy) {
7451 upb_pbdecodermethodopts_setlazy(this, lazy);
7452}
7453
Chris Fallin91473dc2014-12-12 15:58:26 -08007454inline const Handlers* DecoderMethod::dest_handlers() const {
7455 return upb_pbdecodermethod_desthandlers(this);
7456}
7457inline const BytesHandler* DecoderMethod::input_handler() const {
7458 return upb_pbdecodermethod_inputhandler(this);
7459}
7460inline bool DecoderMethod::is_native() const {
7461 return upb_pbdecodermethod_isnative(this);
7462}
Josh Habermane8ed0212015-06-08 17:56:03 -07007463/* static */
Chris Fallin91473dc2014-12-12 15:58:26 -08007464inline reffed_ptr<const DecoderMethod> DecoderMethod::New(
7465 const DecoderMethodOptions &opts) {
7466 const upb_pbdecodermethod *m = upb_pbdecodermethod_new(&opts, &m);
7467 return reffed_ptr<const DecoderMethod>(m, &m);
7468}
7469
7470inline CodeCache::CodeCache() {
7471 upb_pbcodecache_init(this);
7472}
7473inline CodeCache::~CodeCache() {
7474 upb_pbcodecache_uninit(this);
7475}
7476inline bool CodeCache::allow_jit() const {
7477 return upb_pbcodecache_allowjit(this);
7478}
7479inline bool CodeCache::set_allow_jit(bool allow) {
7480 return upb_pbcodecache_setallowjit(this, allow);
7481}
7482inline const DecoderMethod *CodeCache::GetDecoderMethod(
7483 const DecoderMethodOptions& opts) {
7484 return upb_pbcodecache_getdecodermethod(this, &opts);
7485}
7486
Josh Habermane8ed0212015-06-08 17:56:03 -07007487} /* namespace pb */
7488} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08007489
Josh Habermane8ed0212015-06-08 17:56:03 -07007490#endif /* __cplusplus */
Chris Fallin91473dc2014-12-12 15:58:26 -08007491
7492#endif /* UPB_DECODER_H_ */
7493
Josh Habermane8ed0212015-06-08 17:56:03 -07007494/* C++ names are not actually used since this type isn't exposed to users. */
7495#ifdef __cplusplus
7496namespace upb {
7497namespace pb {
7498class MessageGroup;
7499} /* namespace pb */
7500} /* namespace upb */
7501#endif
7502UPB_DECLARE_DERIVED_TYPE(upb::pb::MessageGroup, upb::RefCounted,
7503 mgroup, upb_refcounted)
7504
7505/* Opcode definitions. The canonical meaning of each opcode is its
7506 * implementation in the interpreter (the JIT is written to match this).
7507 *
7508 * All instructions have the opcode in the low byte.
7509 * Instruction format for most instructions is:
7510 *
7511 * +-------------------+--------+
7512 * | arg (24) | op (8) |
7513 * +-------------------+--------+
7514 *
7515 * Exceptions are indicated below. A few opcodes are multi-word. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007516typedef enum {
Josh Habermane8ed0212015-06-08 17:56:03 -07007517 /* Opcodes 1-8, 13, 15-18 parse their respective descriptor types.
7518 * Arg for all of these is the upb selector for this field. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007519#define T(type) OP_PARSE_ ## type = UPB_DESCRIPTOR_TYPE_ ## type
7520 T(DOUBLE), T(FLOAT), T(INT64), T(UINT64), T(INT32), T(FIXED64), T(FIXED32),
7521 T(BOOL), T(UINT32), T(SFIXED32), T(SFIXED64), T(SINT32), T(SINT64),
7522#undef T
Josh Habermane8ed0212015-06-08 17:56:03 -07007523 OP_STARTMSG = 9, /* No arg. */
7524 OP_ENDMSG = 10, /* No arg. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007525 OP_STARTSEQ = 11,
7526 OP_ENDSEQ = 12,
7527 OP_STARTSUBMSG = 14,
7528 OP_ENDSUBMSG = 19,
7529 OP_STARTSTR = 20,
7530 OP_STRING = 21,
7531 OP_ENDSTR = 22,
7532
Josh Habermane8ed0212015-06-08 17:56:03 -07007533 OP_PUSHTAGDELIM = 23, /* No arg. */
7534 OP_PUSHLENDELIM = 24, /* No arg. */
7535 OP_POP = 25, /* No arg. */
7536 OP_SETDELIM = 26, /* No arg. */
7537 OP_SETBIGGROUPNUM = 27, /* two words:
7538 * | unused (24) | opc (8) |
7539 * | groupnum (32) | */
Chris Fallin91473dc2014-12-12 15:58:26 -08007540 OP_CHECKDELIM = 28,
7541 OP_CALL = 29,
7542 OP_RET = 30,
7543 OP_BRANCH = 31,
7544
Josh Habermane8ed0212015-06-08 17:56:03 -07007545 /* Different opcodes depending on how many bytes expected. */
7546 OP_TAG1 = 32, /* | match tag (16) | jump target (8) | opc (8) | */
7547 OP_TAG2 = 33, /* | match tag (16) | jump target (8) | opc (8) | */
7548 OP_TAGN = 34, /* three words: */
7549 /* | unused (16) | jump target(8) | opc (8) | */
7550 /* | match tag 1 (32) | */
7551 /* | match tag 2 (32) | */
Chris Fallin91473dc2014-12-12 15:58:26 -08007552
Josh Habermane8ed0212015-06-08 17:56:03 -07007553 OP_SETDISPATCH = 35, /* N words: */
7554 /* | unused (24) | opc | */
7555 /* | upb_inttable* (32 or 64) | */
Chris Fallin91473dc2014-12-12 15:58:26 -08007556
Josh Habermane8ed0212015-06-08 17:56:03 -07007557 OP_DISPATCH = 36, /* No arg. */
Chris Fallin97b663a2015-01-09 16:15:22 -08007558
Josh Habermane8ed0212015-06-08 17:56:03 -07007559 OP_HALT = 37 /* No arg. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007560} opcode;
7561
7562#define OP_MAX OP_HALT
7563
7564UPB_INLINE opcode getop(uint32_t instr) { return instr & 0xff; }
7565
Josh Habermane8ed0212015-06-08 17:56:03 -07007566/* Method group; represents a set of decoder methods that had their code
7567 * emitted together, and must therefore be freed together. Immutable once
7568 * created. It is possible we may want to expose this to users at some point.
7569 *
7570 * Overall ownership of Decoder objects looks like this:
7571 *
7572 * +----------+
7573 * | | <---> DecoderMethod
7574 * | method |
7575 * CodeCache ---> | group | <---> DecoderMethod
7576 * | |
7577 * | (mgroup) | <---> DecoderMethod
7578 * +----------+
7579 */
7580struct mgroup {
Chris Fallin91473dc2014-12-12 15:58:26 -08007581 upb_refcounted base;
7582
Josh Habermane8ed0212015-06-08 17:56:03 -07007583 /* Maps upb_msgdef/upb_handlers -> upb_pbdecodermethod. We own refs on the
7584 * methods. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007585 upb_inttable methods;
7586
Josh Habermane8ed0212015-06-08 17:56:03 -07007587 /* When we add the ability to link to previously existing mgroups, we'll
7588 * need an array of mgroups we reference here, and own refs on them. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007589
Josh Habermane8ed0212015-06-08 17:56:03 -07007590 /* The bytecode for our methods, if any exists. Owned by us. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007591 uint32_t *bytecode;
7592 uint32_t *bytecode_end;
7593
7594#ifdef UPB_USE_JIT_X64
Josh Habermane8ed0212015-06-08 17:56:03 -07007595 /* JIT-generated machine code, if any. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007596 upb_string_handlerfunc *jit_code;
Josh Habermane8ed0212015-06-08 17:56:03 -07007597 /* The size of the jit_code (required to munmap()). */
Chris Fallin91473dc2014-12-12 15:58:26 -08007598 size_t jit_size;
7599 char *debug_info;
7600 void *dl;
7601#endif
Josh Habermane8ed0212015-06-08 17:56:03 -07007602};
Chris Fallin91473dc2014-12-12 15:58:26 -08007603
Josh Habermane8ed0212015-06-08 17:56:03 -07007604/* The maximum that any submessages can be nested. Matches proto2's limit.
7605 * This specifies the size of the decoder's statically-sized array and therefore
7606 * setting it high will cause the upb::pb::Decoder object to be larger.
7607 *
7608 * If necessary we can add a runtime-settable property to Decoder that allow
7609 * this to be larger than the compile-time setting, but this would add
7610 * complexity, particularly since we would have to decide how/if to give users
7611 * the ability to set a custom memory allocation function. */
Chris Fallind3262772015-05-14 18:24:26 -07007612#define UPB_DECODER_MAX_NESTING 64
7613
Josh Habermane8ed0212015-06-08 17:56:03 -07007614/* Internal-only struct used by the decoder. */
Chris Fallind3262772015-05-14 18:24:26 -07007615typedef struct {
Josh Habermane8ed0212015-06-08 17:56:03 -07007616 /* Space optimization note: we store two pointers here that the JIT
7617 * doesn't need at all; the upb_handlers* inside the sink and
7618 * the dispatch table pointer. We can optimze so that the JIT uses
7619 * smaller stack frames than the interpreter. The only thing we need
7620 * to guarantee is that the fallback routines can find end_ofs. */
Chris Fallind3262772015-05-14 18:24:26 -07007621 upb_sink sink;
7622
Josh Habermane8ed0212015-06-08 17:56:03 -07007623 /* The absolute stream offset of the end-of-frame delimiter.
7624 * Non-delimited frames (groups and non-packed repeated fields) reuse the
7625 * delimiter of their parent, even though the frame may not end there.
7626 *
7627 * NOTE: the JIT stores a slightly different value here for non-top frames.
7628 * It stores the value relative to the end of the enclosed message. But the
7629 * top frame is still stored the same way, which is important for ensuring
7630 * that calls from the JIT into C work correctly. */
Chris Fallind3262772015-05-14 18:24:26 -07007631 uint64_t end_ofs;
7632 const uint32_t *base;
7633
Josh Habermane8ed0212015-06-08 17:56:03 -07007634 /* 0 indicates a length-delimited field.
7635 * A positive number indicates a known group.
7636 * A negative number indicates an unknown group. */
Chris Fallind3262772015-05-14 18:24:26 -07007637 int32_t groupnum;
Josh Habermane8ed0212015-06-08 17:56:03 -07007638 upb_inttable *dispatch; /* Not used by the JIT. */
Chris Fallind3262772015-05-14 18:24:26 -07007639} upb_pbdecoder_frame;
7640
Josh Habermane8ed0212015-06-08 17:56:03 -07007641struct upb_pbdecodermethod {
7642 upb_refcounted base;
7643
7644 /* While compiling, the base is relative in "ofs", after compiling it is
7645 * absolute in "ptr". */
7646 union {
7647 uint32_t ofs; /* PC offset of method. */
7648 void *ptr; /* Pointer to bytecode or machine code for this method. */
7649 } code_base;
7650
7651 /* The decoder method group to which this method belongs. We own a ref.
7652 * Owning a ref on the entire group is more coarse-grained than is strictly
7653 * necessary; all we truly require is that methods we directly reference
7654 * outlive us, while the group could contain many other messages we don't
7655 * require. But the group represents the messages that were
7656 * allocated+compiled together, so it makes the most sense to free them
7657 * together also. */
7658 const upb_refcounted *group;
7659
7660 /* Whether this method is native code or bytecode. */
7661 bool is_native_;
7662
7663 /* The handler one calls to invoke this method. */
7664 upb_byteshandler input_handler_;
7665
7666 /* The destination handlers this method is bound to. We own a ref. */
7667 const upb_handlers *dest_handlers_;
7668
7669 /* Dispatch table -- used by both bytecode decoder and JIT when encountering a
7670 * field number that wasn't the one we were expecting to see. See
7671 * decoder.int.h for the layout of this table. */
7672 upb_inttable dispatch;
7673};
7674
Chris Fallind3262772015-05-14 18:24:26 -07007675struct upb_pbdecoder {
7676 upb_env *env;
7677
Josh Habermane8ed0212015-06-08 17:56:03 -07007678 /* Our input sink. */
Chris Fallind3262772015-05-14 18:24:26 -07007679 upb_bytessink input_;
7680
Josh Habermane8ed0212015-06-08 17:56:03 -07007681 /* The decoder method we are parsing with (owned). */
Chris Fallind3262772015-05-14 18:24:26 -07007682 const upb_pbdecodermethod *method_;
7683
7684 size_t call_len;
7685 const uint32_t *pc, *last;
7686
Josh Habermane8ed0212015-06-08 17:56:03 -07007687 /* Current input buffer and its stream offset. */
Chris Fallind3262772015-05-14 18:24:26 -07007688 const char *buf, *ptr, *end, *checkpoint;
7689
Josh Habermane8ed0212015-06-08 17:56:03 -07007690 /* End of the delimited region, relative to ptr, NULL if not in this buf. */
Chris Fallind3262772015-05-14 18:24:26 -07007691 const char *delim_end;
7692
Josh Habermane8ed0212015-06-08 17:56:03 -07007693 /* End of the delimited region, relative to ptr, end if not in this buf. */
Chris Fallind3262772015-05-14 18:24:26 -07007694 const char *data_end;
7695
Josh Habermane8ed0212015-06-08 17:56:03 -07007696 /* Overall stream offset of "buf." */
Chris Fallind3262772015-05-14 18:24:26 -07007697 uint64_t bufstart_ofs;
7698
Josh Haberman78da6662016-01-13 19:05:43 -08007699 /* Buffer for residual bytes not parsed from the previous buffer. */
7700 char residual[UPB_DECODER_MAX_RESIDUAL_BYTES];
Chris Fallind3262772015-05-14 18:24:26 -07007701 char *residual_end;
7702
Josh Haberman5bdf4a42015-08-03 15:51:31 -07007703 /* Bytes of data that should be discarded from the input beore we start
7704 * parsing again. We set this when we internally determine that we can
7705 * safely skip the next N bytes, but this region extends past the current
7706 * user buffer. */
7707 size_t skip;
7708
Josh Habermane8ed0212015-06-08 17:56:03 -07007709 /* Stores the user buffer passed to our decode function. */
Chris Fallind3262772015-05-14 18:24:26 -07007710 const char *buf_param;
7711 size_t size_param;
7712 const upb_bufhandle *handle;
7713
Josh Habermane8ed0212015-06-08 17:56:03 -07007714 /* Our internal stack. */
Chris Fallind3262772015-05-14 18:24:26 -07007715 upb_pbdecoder_frame *stack, *top, *limit;
7716 const uint32_t **callstack;
7717 size_t stack_size;
7718
7719 upb_status *status;
7720
7721#ifdef UPB_USE_JIT_X64
Josh Habermane8ed0212015-06-08 17:56:03 -07007722 /* Used momentarily by the generated code to store a value while a user
7723 * function is called. */
Chris Fallind3262772015-05-14 18:24:26 -07007724 uint32_t tmp_len;
7725
7726 const void *saved_rsp;
7727#endif
7728};
7729
Josh Habermane8ed0212015-06-08 17:56:03 -07007730/* Decoder entry points; used as handlers. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007731void *upb_pbdecoder_startbc(void *closure, const void *pc, size_t size_hint);
7732void *upb_pbdecoder_startjit(void *closure, const void *hd, size_t size_hint);
7733size_t upb_pbdecoder_decode(void *closure, const void *hd, const char *buf,
7734 size_t size, const upb_bufhandle *handle);
7735bool upb_pbdecoder_end(void *closure, const void *handler_data);
7736
Josh Habermane8ed0212015-06-08 17:56:03 -07007737/* Decoder-internal functions that the JIT calls to handle fallback paths. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007738int32_t upb_pbdecoder_resume(upb_pbdecoder *d, void *p, const char *buf,
7739 size_t size, const upb_bufhandle *handle);
7740size_t upb_pbdecoder_suspend(upb_pbdecoder *d);
7741int32_t upb_pbdecoder_skipunknown(upb_pbdecoder *d, int32_t fieldnum,
7742 uint8_t wire_type);
7743int32_t upb_pbdecoder_checktag_slow(upb_pbdecoder *d, uint64_t expected);
7744int32_t upb_pbdecoder_decode_varint_slow(upb_pbdecoder *d, uint64_t *u64);
7745int32_t upb_pbdecoder_decode_f32(upb_pbdecoder *d, uint32_t *u32);
7746int32_t upb_pbdecoder_decode_f64(upb_pbdecoder *d, uint64_t *u64);
7747void upb_pbdecoder_seterr(upb_pbdecoder *d, const char *msg);
7748
Josh Habermane8ed0212015-06-08 17:56:03 -07007749/* Error messages that are shared between the bytecode and JIT decoders. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007750extern const char *kPbDecoderStackOverflow;
Josh Haberman5bdf4a42015-08-03 15:51:31 -07007751extern const char *kPbDecoderSubmessageTooLong;
Chris Fallin91473dc2014-12-12 15:58:26 -08007752
Josh Habermane8ed0212015-06-08 17:56:03 -07007753/* Access to decoderplan members needed by the decoder. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007754const char *upb_pbdecoder_getopname(unsigned int op);
7755
Josh Habermane8ed0212015-06-08 17:56:03 -07007756/* JIT codegen entry point. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007757void upb_pbdecoder_jit(mgroup *group);
7758void upb_pbdecoder_freejit(mgroup *group);
Josh Habermane8ed0212015-06-08 17:56:03 -07007759UPB_REFCOUNTED_CMETHODS(mgroup, mgroup_upcast)
Chris Fallin91473dc2014-12-12 15:58:26 -08007760
Josh Habermane8ed0212015-06-08 17:56:03 -07007761/* A special label that means "do field dispatch for this message and branch to
7762 * wherever that takes you." */
Chris Fallin91473dc2014-12-12 15:58:26 -08007763#define LABEL_DISPATCH 0
7764
Josh Habermane8ed0212015-06-08 17:56:03 -07007765/* A special slot in the dispatch table that stores the epilogue (ENDMSG and/or
7766 * RET) for branching to when we find an appropriate ENDGROUP tag. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007767#define DISPATCH_ENDMSG 0
7768
Josh Habermane8ed0212015-06-08 17:56:03 -07007769/* It's important to use this invalid wire type instead of 0 (which is a valid
7770 * wire type). */
Chris Fallin91473dc2014-12-12 15:58:26 -08007771#define NO_WIRE_TYPE 0xff
7772
Josh Habermane8ed0212015-06-08 17:56:03 -07007773/* The dispatch table layout is:
7774 * [field number] -> [ 48-bit offset ][ 8-bit wt2 ][ 8-bit wt1 ]
7775 *
7776 * If wt1 matches, jump to the 48-bit offset. If wt2 matches, lookup
7777 * (UPB_MAX_FIELDNUMBER + fieldnum) and jump there.
7778 *
7779 * We need two wire types because of packed/non-packed compatibility. A
7780 * primitive repeated field can use either wire type and be valid. While we
7781 * could key the table on fieldnum+wiretype, the table would be 8x sparser.
7782 *
7783 * Storing two wire types in the primary value allows us to quickly rule out
7784 * the second wire type without needing to do a separate lookup (this case is
7785 * less common than an unknown field). */
Chris Fallin91473dc2014-12-12 15:58:26 -08007786UPB_INLINE uint64_t upb_pbdecoder_packdispatch(uint64_t ofs, uint8_t wt1,
7787 uint8_t wt2) {
7788 return (ofs << 16) | (wt2 << 8) | wt1;
7789}
7790
7791UPB_INLINE void upb_pbdecoder_unpackdispatch(uint64_t dispatch, uint64_t *ofs,
7792 uint8_t *wt1, uint8_t *wt2) {
7793 *wt1 = (uint8_t)dispatch;
7794 *wt2 = (uint8_t)(dispatch >> 8);
7795 *ofs = dispatch >> 16;
7796}
7797
Josh Habermane8ed0212015-06-08 17:56:03 -07007798/* All of the functions in decoder.c that return int32_t return values according
7799 * to the following scheme:
7800 * 1. negative values indicate a return code from the following list.
7801 * 2. positive values indicate that error or end of buffer was hit, and
7802 * that the decode function should immediately return the given value
7803 * (the decoder state has already been suspended and is ready to be
7804 * resumed). */
Chris Fallin91473dc2014-12-12 15:58:26 -08007805#define DECODE_OK -1
Josh Habermane8ed0212015-06-08 17:56:03 -07007806#define DECODE_MISMATCH -2 /* Used only from checktag_slow(). */
7807#define DECODE_ENDGROUP -3 /* Used only from checkunknown(). */
Chris Fallin91473dc2014-12-12 15:58:26 -08007808
7809#define CHECK_RETURN(x) { int32_t ret = x; if (ret >= 0) return ret; }
7810
Josh Habermane8ed0212015-06-08 17:56:03 -07007811#endif /* UPB_DECODER_INT_H_ */
Chris Fallin91473dc2014-12-12 15:58:26 -08007812/*
Josh Haberman181c7f22015-07-15 11:05:10 -07007813** A number of routines for varint manipulation (we keep them all around to
7814** have multiple approaches available for benchmarking).
7815*/
Chris Fallin91473dc2014-12-12 15:58:26 -08007816
7817#ifndef UPB_VARINT_DECODER_H_
7818#define UPB_VARINT_DECODER_H_
7819
7820#include <assert.h>
7821#include <stdint.h>
7822#include <string.h>
7823
7824#ifdef __cplusplus
7825extern "C" {
7826#endif
7827
Josh Habermane8ed0212015-06-08 17:56:03 -07007828/* A list of types as they are encoded on-the-wire. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007829typedef enum {
7830 UPB_WIRE_TYPE_VARINT = 0,
7831 UPB_WIRE_TYPE_64BIT = 1,
7832 UPB_WIRE_TYPE_DELIMITED = 2,
7833 UPB_WIRE_TYPE_START_GROUP = 3,
7834 UPB_WIRE_TYPE_END_GROUP = 4,
Josh Habermane8ed0212015-06-08 17:56:03 -07007835 UPB_WIRE_TYPE_32BIT = 5
Chris Fallin91473dc2014-12-12 15:58:26 -08007836} upb_wiretype_t;
7837
7838#define UPB_MAX_WIRE_TYPE 5
7839
Josh Habermane8ed0212015-06-08 17:56:03 -07007840/* The maximum number of bytes that it takes to encode a 64-bit varint.
7841 * Note that with a better encoding this could be 9 (TODO: write up a
7842 * wiki document about this). */
Chris Fallin91473dc2014-12-12 15:58:26 -08007843#define UPB_PB_VARINT_MAX_LEN 10
7844
Josh Habermane8ed0212015-06-08 17:56:03 -07007845/* Array of the "native" (ie. non-packed-repeated) wire type for the given a
7846 * descriptor type (upb_descriptortype_t). */
Chris Fallin91473dc2014-12-12 15:58:26 -08007847extern const uint8_t upb_pb_native_wire_types[];
7848
7849/* Zig-zag encoding/decoding **************************************************/
7850
7851UPB_INLINE int32_t upb_zzdec_32(uint32_t n) {
7852 return (n >> 1) ^ -(int32_t)(n & 1);
7853}
7854UPB_INLINE int64_t upb_zzdec_64(uint64_t n) {
7855 return (n >> 1) ^ -(int64_t)(n & 1);
7856}
7857UPB_INLINE uint32_t upb_zzenc_32(int32_t n) { return (n << 1) ^ (n >> 31); }
7858UPB_INLINE uint64_t upb_zzenc_64(int64_t n) { return (n << 1) ^ (n >> 63); }
7859
7860/* Decoding *******************************************************************/
7861
Josh Habermane8ed0212015-06-08 17:56:03 -07007862/* All decoding functions return this struct by value. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007863typedef struct {
Josh Habermane8ed0212015-06-08 17:56:03 -07007864 const char *p; /* NULL if the varint was unterminated. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007865 uint64_t val;
7866} upb_decoderet;
7867
Josh Habermane8ed0212015-06-08 17:56:03 -07007868UPB_INLINE upb_decoderet upb_decoderet_make(const char *p, uint64_t val) {
7869 upb_decoderet ret;
7870 ret.p = p;
7871 ret.val = val;
7872 return ret;
7873}
7874
7875/* Four functions for decoding a varint of at most eight bytes. They are all
7876 * functionally identical, but are implemented in different ways and likely have
7877 * different performance profiles. We keep them around for performance testing.
7878 *
7879 * Note that these functions may not read byte-by-byte, so they must not be used
7880 * unless there are at least eight bytes left in the buffer! */
Chris Fallin91473dc2014-12-12 15:58:26 -08007881upb_decoderet upb_vdecode_max8_branch32(upb_decoderet r);
7882upb_decoderet upb_vdecode_max8_branch64(upb_decoderet r);
7883upb_decoderet upb_vdecode_max8_wright(upb_decoderet r);
7884upb_decoderet upb_vdecode_max8_massimino(upb_decoderet r);
7885
Josh Habermane8ed0212015-06-08 17:56:03 -07007886/* Template for a function that checks the first two bytes with branching
7887 * and dispatches 2-10 bytes with a separate function. Note that this may read
7888 * up to 10 bytes, so it must not be used unless there are at least ten bytes
7889 * left in the buffer! */
Chris Fallin91473dc2014-12-12 15:58:26 -08007890#define UPB_VARINT_DECODER_CHECK2(name, decode_max8_function) \
7891UPB_INLINE upb_decoderet upb_vdecode_check2_ ## name(const char *_p) { \
7892 uint8_t *p = (uint8_t*)_p; \
Josh Habermane8ed0212015-06-08 17:56:03 -07007893 upb_decoderet r; \
7894 if ((*p & 0x80) == 0) { \
7895 /* Common case: one-byte varint. */ \
7896 return upb_decoderet_make(_p + 1, *p & 0x7fU); \
7897 } \
7898 r = upb_decoderet_make(_p + 2, (*p & 0x7fU) | ((*(p + 1) & 0x7fU) << 7)); \
7899 if ((*(p + 1) & 0x80) == 0) { \
7900 /* Two-byte varint. */ \
7901 return r; \
7902 } \
7903 /* Longer varint, fallback to out-of-line function. */ \
Chris Fallin91473dc2014-12-12 15:58:26 -08007904 return decode_max8_function(r); \
7905}
7906
Josh Habermane8ed0212015-06-08 17:56:03 -07007907UPB_VARINT_DECODER_CHECK2(branch32, upb_vdecode_max8_branch32)
7908UPB_VARINT_DECODER_CHECK2(branch64, upb_vdecode_max8_branch64)
7909UPB_VARINT_DECODER_CHECK2(wright, upb_vdecode_max8_wright)
7910UPB_VARINT_DECODER_CHECK2(massimino, upb_vdecode_max8_massimino)
Chris Fallin91473dc2014-12-12 15:58:26 -08007911#undef UPB_VARINT_DECODER_CHECK2
7912
Josh Habermane8ed0212015-06-08 17:56:03 -07007913/* Our canonical functions for decoding varints, based on the currently
7914 * favored best-performing implementations. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007915UPB_INLINE upb_decoderet upb_vdecode_fast(const char *p) {
7916 if (sizeof(long) == 8)
7917 return upb_vdecode_check2_branch64(p);
7918 else
7919 return upb_vdecode_check2_branch32(p);
7920}
7921
7922UPB_INLINE upb_decoderet upb_vdecode_max8_fast(upb_decoderet r) {
7923 return upb_vdecode_max8_massimino(r);
7924}
7925
7926
7927/* Encoding *******************************************************************/
7928
7929UPB_INLINE int upb_value_size(uint64_t val) {
7930#ifdef __GNUC__
Josh Habermane8ed0212015-06-08 17:56:03 -07007931 int high_bit = 63 - __builtin_clzll(val); /* 0-based, undef if val == 0. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007932#else
7933 int high_bit = 0;
7934 uint64_t tmp = val;
7935 while(tmp >>= 1) high_bit++;
7936#endif
7937 return val == 0 ? 1 : high_bit / 8 + 1;
7938}
7939
Josh Habermane8ed0212015-06-08 17:56:03 -07007940/* Encodes a 64-bit varint into buf (which must be >=UPB_PB_VARINT_MAX_LEN
7941 * bytes long), returning how many bytes were used.
7942 *
7943 * TODO: benchmark and optimize if necessary. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007944UPB_INLINE size_t upb_vencode64(uint64_t val, char *buf) {
Josh Habermane8ed0212015-06-08 17:56:03 -07007945 size_t i;
Chris Fallin91473dc2014-12-12 15:58:26 -08007946 if (val == 0) { buf[0] = 0; return 1; }
Josh Habermane8ed0212015-06-08 17:56:03 -07007947 i = 0;
Chris Fallin91473dc2014-12-12 15:58:26 -08007948 while (val) {
7949 uint8_t byte = val & 0x7fU;
7950 val >>= 7;
7951 if (val) byte |= 0x80U;
7952 buf[i++] = byte;
7953 }
7954 return i;
7955}
7956
7957UPB_INLINE size_t upb_varint_size(uint64_t val) {
7958 char buf[UPB_PB_VARINT_MAX_LEN];
7959 return upb_vencode64(val, buf);
7960}
7961
Josh Habermane8ed0212015-06-08 17:56:03 -07007962/* Encodes a 32-bit varint, *not* sign-extended. */
Chris Fallin91473dc2014-12-12 15:58:26 -08007963UPB_INLINE uint64_t upb_vencode32(uint32_t val) {
7964 char buf[UPB_PB_VARINT_MAX_LEN];
7965 size_t bytes = upb_vencode64(val, buf);
7966 uint64_t ret = 0;
7967 assert(bytes <= 5);
7968 memcpy(&ret, buf, bytes);
7969 assert(ret <= 0xffffffffffU);
7970 return ret;
7971}
7972
7973#ifdef __cplusplus
7974} /* extern "C" */
7975#endif
7976
7977#endif /* UPB_VARINT_DECODER_H_ */
7978/*
Josh Haberman181c7f22015-07-15 11:05:10 -07007979** upb::pb::Encoder (upb_pb_encoder)
7980**
7981** Implements a set of upb_handlers that write protobuf data to the binary wire
7982** format.
7983**
7984** This encoder implementation does not have any access to any out-of-band or
7985** precomputed lengths for submessages, so it must buffer submessages internally
7986** before it can emit the first byte.
7987*/
Chris Fallin91473dc2014-12-12 15:58:26 -08007988
7989#ifndef UPB_ENCODER_H_
7990#define UPB_ENCODER_H_
7991
7992
7993#ifdef __cplusplus
7994namespace upb {
7995namespace pb {
7996class Encoder;
Josh Habermane8ed0212015-06-08 17:56:03 -07007997} /* namespace pb */
7998} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08007999#endif
8000
Josh Habermane8ed0212015-06-08 17:56:03 -07008001UPB_DECLARE_TYPE(upb::pb::Encoder, upb_pb_encoder)
Chris Fallin91473dc2014-12-12 15:58:26 -08008002
8003#define UPB_PBENCODER_MAX_NESTING 100
8004
8005/* upb::pb::Encoder ***********************************************************/
8006
Josh Habermane8ed0212015-06-08 17:56:03 -07008007/* Preallocation hint: decoder won't allocate more bytes than this when first
8008 * constructed. This hint may be an overestimate for some build configurations.
8009 * But if the decoder library is upgraded without recompiling the application,
8010 * it may be an underestimate. */
Chris Fallind3262772015-05-14 18:24:26 -07008011#define UPB_PB_ENCODER_SIZE 768
Chris Fallin91473dc2014-12-12 15:58:26 -08008012
Chris Fallind3262772015-05-14 18:24:26 -07008013#ifdef __cplusplus
8014
8015class upb::pb::Encoder {
Chris Fallin91473dc2014-12-12 15:58:26 -08008016 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07008017 /* Creates a new encoder in the given environment. The Handlers must have
8018 * come from NewHandlers() below. */
Chris Fallind3262772015-05-14 18:24:26 -07008019 static Encoder* Create(Environment* env, const Handlers* handlers,
8020 BytesSink* output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008021
Josh Habermane8ed0212015-06-08 17:56:03 -07008022 /* The input to the encoder. */
Chris Fallin91473dc2014-12-12 15:58:26 -08008023 Sink* input();
8024
Josh Habermane8ed0212015-06-08 17:56:03 -07008025 /* Creates a new set of handlers for this MessageDef. */
Chris Fallind3262772015-05-14 18:24:26 -07008026 static reffed_ptr<const Handlers> NewHandlers(const MessageDef* msg);
8027
8028 static const size_t kSize = UPB_PB_ENCODER_SIZE;
8029
Chris Fallin91473dc2014-12-12 15:58:26 -08008030 private:
Josh Habermanfb8ed702015-06-22 17:23:55 -07008031 UPB_DISALLOW_POD_OPS(Encoder, upb::pb::Encoder)
Chris Fallind3262772015-05-14 18:24:26 -07008032};
Chris Fallin91473dc2014-12-12 15:58:26 -08008033
Chris Fallind3262772015-05-14 18:24:26 -07008034#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08008035
8036UPB_BEGIN_EXTERN_C
8037
8038const upb_handlers *upb_pb_encoder_newhandlers(const upb_msgdef *m,
8039 const void *owner);
Chris Fallin91473dc2014-12-12 15:58:26 -08008040upb_sink *upb_pb_encoder_input(upb_pb_encoder *p);
Chris Fallind3262772015-05-14 18:24:26 -07008041upb_pb_encoder* upb_pb_encoder_create(upb_env* e, const upb_handlers* h,
8042 upb_bytessink* output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008043
8044UPB_END_EXTERN_C
8045
8046#ifdef __cplusplus
8047
8048namespace upb {
8049namespace pb {
Chris Fallind3262772015-05-14 18:24:26 -07008050inline Encoder* Encoder::Create(Environment* env, const Handlers* handlers,
8051 BytesSink* output) {
8052 return upb_pb_encoder_create(env, handlers, output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008053}
8054inline Sink* Encoder::input() {
8055 return upb_pb_encoder_input(this);
8056}
8057inline reffed_ptr<const Handlers> Encoder::NewHandlers(
8058 const upb::MessageDef *md) {
8059 const Handlers* h = upb_pb_encoder_newhandlers(md, &h);
8060 return reffed_ptr<const Handlers>(h, &h);
8061}
Josh Habermane8ed0212015-06-08 17:56:03 -07008062} /* namespace pb */
8063} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08008064
8065#endif
8066
8067#endif /* UPB_ENCODER_H_ */
8068/*
Josh Haberman181c7f22015-07-15 11:05:10 -07008069** upb's core components like upb_decoder and upb_msg are carefully designed to
8070** avoid depending on each other for maximum orthogonality. In other words,
8071** you can use a upb_decoder to decode into *any* kind of structure; upb_msg is
8072** just one such structure. A upb_msg can be serialized/deserialized into any
8073** format, protobuf binary format is just one such format.
8074**
8075** However, for convenience we provide functions here for doing common
8076** operations like deserializing protobuf binary format into a upb_msg. The
8077** compromise is that this file drags in almost all of upb as a dependency,
8078** which could be undesirable if you're trying to use a trimmed-down build of
8079** upb.
8080**
8081** While these routines are convenient, they do not reuse any encoding/decoding
8082** state. For example, if a decoder is JIT-based, it will be re-JITted every
8083** time these functions are called. For this reason, if you are parsing lots
8084** of data and efficiency is an issue, these may not be the best functions to
8085** use (though they are useful for prototyping, before optimizing).
8086*/
Chris Fallin91473dc2014-12-12 15:58:26 -08008087
8088#ifndef UPB_GLUE_H
8089#define UPB_GLUE_H
8090
8091#include <stdbool.h>
8092
8093#ifdef __cplusplus
Josh Haberman94e54b32016-04-14 12:06:09 -07008094#include <vector>
8095
Chris Fallin91473dc2014-12-12 15:58:26 -08008096extern "C" {
8097#endif
8098
Josh Haberman94e54b32016-04-14 12:06:09 -07008099/* Loads a binary descriptor and returns a NULL-terminated array of unfrozen
8100 * filedefs. The caller owns the returned array. */
8101upb_filedef **upb_loaddescriptor(const char *buf, size_t n, const void *owner,
8102 upb_status *status);
Chris Fallin91473dc2014-12-12 15:58:26 -08008103
8104#ifdef __cplusplus
8105} /* extern "C" */
8106
8107namespace upb {
8108
Josh Haberman94e54b32016-04-14 12:06:09 -07008109inline bool LoadDescriptor(const char* buf, size_t n, Status* status,
8110 std::vector<reffed_ptr<FileDef> >* files) {
8111 FileDef** parsed_files = upb_loaddescriptor(buf, n, &parsed_files, status);
Chris Fallin91473dc2014-12-12 15:58:26 -08008112
Josh Haberman94e54b32016-04-14 12:06:09 -07008113 if (parsed_files) {
8114 FileDef** p = parsed_files;
8115 while (*p) {
8116 files->push_back(reffed_ptr<FileDef>(*p, &parsed_files));
8117 ++p;
8118 }
8119 free(parsed_files);
8120 return true;
8121 } else {
8122 return false;
8123 }
Chris Fallin91473dc2014-12-12 15:58:26 -08008124}
8125
Josh Habermane8ed0212015-06-08 17:56:03 -07008126/* Templated so it can accept both string and std::string. */
Chris Fallin91473dc2014-12-12 15:58:26 -08008127template <typename T>
Josh Haberman94e54b32016-04-14 12:06:09 -07008128bool LoadDescriptor(const T& desc, Status* status,
8129 std::vector<reffed_ptr<FileDef> >* files) {
8130 return LoadDescriptor(desc.c_str(), desc.size(), status, files);
Chris Fallin91473dc2014-12-12 15:58:26 -08008131}
8132
Josh Habermane8ed0212015-06-08 17:56:03 -07008133} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08008134
8135#endif
8136
Josh Habermane8ed0212015-06-08 17:56:03 -07008137#endif /* UPB_GLUE_H */
Chris Fallin91473dc2014-12-12 15:58:26 -08008138/*
Josh Haberman181c7f22015-07-15 11:05:10 -07008139** upb::pb::TextPrinter (upb_textprinter)
8140**
8141** Handlers for writing to protobuf text format.
8142*/
Chris Fallin91473dc2014-12-12 15:58:26 -08008143
8144#ifndef UPB_TEXT_H_
8145#define UPB_TEXT_H_
8146
8147
8148#ifdef __cplusplus
8149namespace upb {
8150namespace pb {
8151class TextPrinter;
Josh Habermane8ed0212015-06-08 17:56:03 -07008152} /* namespace pb */
8153} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08008154#endif
8155
Josh Habermane8ed0212015-06-08 17:56:03 -07008156UPB_DECLARE_TYPE(upb::pb::TextPrinter, upb_textprinter)
Chris Fallin91473dc2014-12-12 15:58:26 -08008157
Chris Fallind3262772015-05-14 18:24:26 -07008158#ifdef __cplusplus
8159
8160class upb::pb::TextPrinter {
Chris Fallin91473dc2014-12-12 15:58:26 -08008161 public:
Josh Habermane8ed0212015-06-08 17:56:03 -07008162 /* The given handlers must have come from NewHandlers(). It must outlive the
8163 * TextPrinter. */
Chris Fallind3262772015-05-14 18:24:26 -07008164 static TextPrinter *Create(Environment *env, const upb::Handlers *handlers,
8165 BytesSink *output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008166
8167 void SetSingleLineMode(bool single_line);
8168
Chris Fallin91473dc2014-12-12 15:58:26 -08008169 Sink* input();
8170
Josh Habermane8ed0212015-06-08 17:56:03 -07008171 /* If handler caching becomes a requirement we can add a code cache as in
8172 * decoder.h */
Chris Fallin91473dc2014-12-12 15:58:26 -08008173 static reffed_ptr<const Handlers> NewHandlers(const MessageDef* md);
Chris Fallind3262772015-05-14 18:24:26 -07008174};
Chris Fallin91473dc2014-12-12 15:58:26 -08008175
Chris Fallind3262772015-05-14 18:24:26 -07008176#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08008177
Chris Fallind3262772015-05-14 18:24:26 -07008178UPB_BEGIN_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08008179
Josh Habermane8ed0212015-06-08 17:56:03 -07008180/* C API. */
Chris Fallind3262772015-05-14 18:24:26 -07008181upb_textprinter *upb_textprinter_create(upb_env *env, const upb_handlers *h,
8182 upb_bytessink *output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008183void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line);
8184upb_sink *upb_textprinter_input(upb_textprinter *p);
8185
8186const upb_handlers *upb_textprinter_newhandlers(const upb_msgdef *m,
8187 const void *owner);
8188
Chris Fallind3262772015-05-14 18:24:26 -07008189UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08008190
8191#ifdef __cplusplus
8192
8193namespace upb {
8194namespace pb {
Chris Fallind3262772015-05-14 18:24:26 -07008195inline TextPrinter *TextPrinter::Create(Environment *env,
8196 const upb::Handlers *handlers,
8197 BytesSink *output) {
8198 return upb_textprinter_create(env, handlers, output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008199}
8200inline void TextPrinter::SetSingleLineMode(bool single_line) {
8201 upb_textprinter_setsingleline(this, single_line);
8202}
Chris Fallin91473dc2014-12-12 15:58:26 -08008203inline Sink* TextPrinter::input() {
8204 return upb_textprinter_input(this);
8205}
8206inline reffed_ptr<const Handlers> TextPrinter::NewHandlers(
8207 const MessageDef *md) {
8208 const Handlers* h = upb_textprinter_newhandlers(md, &h);
8209 return reffed_ptr<const Handlers>(h, &h);
8210}
Josh Habermane8ed0212015-06-08 17:56:03 -07008211} /* namespace pb */
8212} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08008213
8214#endif
8215
8216#endif /* UPB_TEXT_H_ */
8217/*
Josh Haberman181c7f22015-07-15 11:05:10 -07008218** upb::json::Parser (upb_json_parser)
8219**
8220** Parses JSON according to a specific schema.
8221** Support for parsing arbitrary JSON (schema-less) will be added later.
8222*/
Chris Fallin91473dc2014-12-12 15:58:26 -08008223
8224#ifndef UPB_JSON_PARSER_H_
8225#define UPB_JSON_PARSER_H_
8226
8227
8228#ifdef __cplusplus
8229namespace upb {
8230namespace json {
8231class Parser;
Josh Haberman78da6662016-01-13 19:05:43 -08008232class ParserMethod;
Josh Habermane8ed0212015-06-08 17:56:03 -07008233} /* namespace json */
8234} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08008235#endif
8236
Josh Habermane8ed0212015-06-08 17:56:03 -07008237UPB_DECLARE_TYPE(upb::json::Parser, upb_json_parser)
Josh Haberman78da6662016-01-13 19:05:43 -08008238UPB_DECLARE_DERIVED_TYPE(upb::json::ParserMethod, upb::RefCounted,
8239 upb_json_parsermethod, upb_refcounted)
Chris Fallin91473dc2014-12-12 15:58:26 -08008240
Chris Fallin91473dc2014-12-12 15:58:26 -08008241/* upb::json::Parser **********************************************************/
8242
Josh Habermane8ed0212015-06-08 17:56:03 -07008243/* Preallocation hint: parser won't allocate more bytes than this when first
8244 * constructed. This hint may be an overestimate for some build configurations.
8245 * But if the parser library is upgraded without recompiling the application,
8246 * it may be an underestimate. */
Josh Haberman78da6662016-01-13 19:05:43 -08008247#define UPB_JSON_PARSER_SIZE 4104
Chris Fallind3262772015-05-14 18:24:26 -07008248
8249#ifdef __cplusplus
Chris Fallin91473dc2014-12-12 15:58:26 -08008250
Josh Habermane8ed0212015-06-08 17:56:03 -07008251/* Parses an incoming BytesStream, pushing the results to the destination
8252 * sink. */
Chris Fallind3262772015-05-14 18:24:26 -07008253class upb::json::Parser {
Chris Fallin91473dc2014-12-12 15:58:26 -08008254 public:
Josh Haberman78da6662016-01-13 19:05:43 -08008255 static Parser* Create(Environment* env, const ParserMethod* method,
8256 Sink* output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008257
Chris Fallin91473dc2014-12-12 15:58:26 -08008258 BytesSink* input();
Chris Fallin91473dc2014-12-12 15:58:26 -08008259
Chris Fallind3262772015-05-14 18:24:26 -07008260 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07008261 UPB_DISALLOW_POD_OPS(Parser, upb::json::Parser)
Chris Fallind3262772015-05-14 18:24:26 -07008262};
Chris Fallin91473dc2014-12-12 15:58:26 -08008263
Josh Haberman78da6662016-01-13 19:05:43 -08008264class upb::json::ParserMethod {
8265 public:
8266 /* Include base methods from upb::ReferenceCounted. */
8267 UPB_REFCOUNTED_CPPMETHODS
8268
8269 /* Returns handlers for parsing according to the specified schema. */
8270 static reffed_ptr<const ParserMethod> New(const upb::MessageDef* md);
8271
8272 /* The destination handlers that are statically bound to this method.
8273 * This method is only capable of outputting to a sink that uses these
8274 * handlers. */
8275 const Handlers* dest_handlers() const;
8276
8277 /* The input handlers for this decoder method. */
8278 const BytesHandler* input_handler() const;
8279
8280 private:
8281 UPB_DISALLOW_POD_OPS(ParserMethod, upb::json::ParserMethod)
8282};
8283
Chris Fallind3262772015-05-14 18:24:26 -07008284#endif
Chris Fallin91473dc2014-12-12 15:58:26 -08008285
8286UPB_BEGIN_EXTERN_C
8287
Josh Haberman78da6662016-01-13 19:05:43 -08008288upb_json_parser* upb_json_parser_create(upb_env* e,
8289 const upb_json_parsermethod* m,
8290 upb_sink* output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008291upb_bytessink *upb_json_parser_input(upb_json_parser *p);
8292
Josh Haberman78da6662016-01-13 19:05:43 -08008293upb_json_parsermethod* upb_json_parsermethod_new(const upb_msgdef* md,
8294 const void* owner);
8295const upb_handlers *upb_json_parsermethod_desthandlers(
8296 const upb_json_parsermethod *m);
8297const upb_byteshandler *upb_json_parsermethod_inputhandler(
8298 const upb_json_parsermethod *m);
8299
8300/* Include refcounted methods like upb_json_parsermethod_ref(). */
8301UPB_REFCOUNTED_CMETHODS(upb_json_parsermethod, upb_json_parsermethod_upcast)
8302
Chris Fallin91473dc2014-12-12 15:58:26 -08008303UPB_END_EXTERN_C
8304
8305#ifdef __cplusplus
8306
8307namespace upb {
8308namespace json {
Josh Haberman78da6662016-01-13 19:05:43 -08008309inline Parser* Parser::Create(Environment* env, const ParserMethod* method,
8310 Sink* output) {
8311 return upb_json_parser_create(env, method, output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008312}
8313inline BytesSink* Parser::input() {
8314 return upb_json_parser_input(this);
8315}
Josh Haberman78da6662016-01-13 19:05:43 -08008316
8317inline const Handlers* ParserMethod::dest_handlers() const {
8318 return upb_json_parsermethod_desthandlers(this);
8319}
8320inline const BytesHandler* ParserMethod::input_handler() const {
8321 return upb_json_parsermethod_inputhandler(this);
8322}
8323/* static */
8324inline reffed_ptr<const ParserMethod> ParserMethod::New(
8325 const MessageDef* md) {
8326 const upb_json_parsermethod *m = upb_json_parsermethod_new(md, &m);
8327 return reffed_ptr<const ParserMethod>(m, &m);
8328}
8329
Josh Habermane8ed0212015-06-08 17:56:03 -07008330} /* namespace json */
8331} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08008332
8333#endif
8334
8335
Josh Habermane8ed0212015-06-08 17:56:03 -07008336#endif /* UPB_JSON_PARSER_H_ */
Chris Fallin91473dc2014-12-12 15:58:26 -08008337/*
Josh Haberman181c7f22015-07-15 11:05:10 -07008338** upb::json::Printer
8339**
8340** Handlers that emit JSON according to a specific protobuf schema.
8341*/
Chris Fallin91473dc2014-12-12 15:58:26 -08008342
8343#ifndef UPB_JSON_TYPED_PRINTER_H_
8344#define UPB_JSON_TYPED_PRINTER_H_
8345
8346
8347#ifdef __cplusplus
8348namespace upb {
8349namespace json {
8350class Printer;
Josh Habermane8ed0212015-06-08 17:56:03 -07008351} /* namespace json */
8352} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08008353#endif
8354
Josh Habermane8ed0212015-06-08 17:56:03 -07008355UPB_DECLARE_TYPE(upb::json::Printer, upb_json_printer)
Chris Fallin91473dc2014-12-12 15:58:26 -08008356
8357
8358/* upb::json::Printer *********************************************************/
8359
Chris Fallind3262772015-05-14 18:24:26 -07008360#define UPB_JSON_PRINTER_SIZE 168
8361
8362#ifdef __cplusplus
8363
Josh Habermane8ed0212015-06-08 17:56:03 -07008364/* Prints an incoming stream of data to a BytesSink in JSON format. */
Chris Fallind3262772015-05-14 18:24:26 -07008365class upb::json::Printer {
Chris Fallin91473dc2014-12-12 15:58:26 -08008366 public:
Chris Fallind3262772015-05-14 18:24:26 -07008367 static Printer* Create(Environment* env, const upb::Handlers* handlers,
8368 BytesSink* output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008369
Josh Habermane8ed0212015-06-08 17:56:03 -07008370 /* The input to the printer. */
Chris Fallin91473dc2014-12-12 15:58:26 -08008371 Sink* input();
8372
Josh Haberman194ad622016-04-14 18:33:17 -07008373 /* Returns handlers for printing according to the specified schema.
8374 * If preserve_proto_fieldnames is true, the output JSON will use the
8375 * original .proto field names (ie. {"my_field":3}) instead of using
8376 * camelCased names, which is the default: (eg. {"myField":3}). */
8377 static reffed_ptr<const Handlers> NewHandlers(const upb::MessageDef* md,
8378 bool preserve_proto_fieldnames);
Chris Fallin91473dc2014-12-12 15:58:26 -08008379
Chris Fallind3262772015-05-14 18:24:26 -07008380 static const size_t kSize = UPB_JSON_PRINTER_SIZE;
Chris Fallin91473dc2014-12-12 15:58:26 -08008381
Chris Fallind3262772015-05-14 18:24:26 -07008382 private:
Josh Habermane8ed0212015-06-08 17:56:03 -07008383 UPB_DISALLOW_POD_OPS(Printer, upb::json::Printer)
Chris Fallind3262772015-05-14 18:24:26 -07008384};
8385
8386#endif
8387
8388UPB_BEGIN_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08008389
Josh Habermane8ed0212015-06-08 17:56:03 -07008390/* Native C API. */
Chris Fallind3262772015-05-14 18:24:26 -07008391upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h,
8392 upb_bytessink *output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008393upb_sink *upb_json_printer_input(upb_json_printer *p);
8394const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md,
Josh Haberman194ad622016-04-14 18:33:17 -07008395 bool preserve_fieldnames,
Chris Fallin91473dc2014-12-12 15:58:26 -08008396 const void *owner);
8397
Chris Fallind3262772015-05-14 18:24:26 -07008398UPB_END_EXTERN_C
Chris Fallin91473dc2014-12-12 15:58:26 -08008399
8400#ifdef __cplusplus
8401
8402namespace upb {
8403namespace json {
Chris Fallind3262772015-05-14 18:24:26 -07008404inline Printer* Printer::Create(Environment* env, const upb::Handlers* handlers,
8405 BytesSink* output) {
8406 return upb_json_printer_create(env, handlers, output);
Chris Fallin91473dc2014-12-12 15:58:26 -08008407}
8408inline Sink* Printer::input() { return upb_json_printer_input(this); }
8409inline reffed_ptr<const Handlers> Printer::NewHandlers(
Josh Haberman194ad622016-04-14 18:33:17 -07008410 const upb::MessageDef *md, bool preserve_proto_fieldnames) {
8411 const Handlers* h = upb_json_printer_newhandlers(
8412 md, preserve_proto_fieldnames, &h);
Chris Fallin91473dc2014-12-12 15:58:26 -08008413 return reffed_ptr<const Handlers>(h, &h);
8414}
Josh Habermane8ed0212015-06-08 17:56:03 -07008415} /* namespace json */
8416} /* namespace upb */
Chris Fallin91473dc2014-12-12 15:58:26 -08008417
8418#endif
8419
Josh Habermane8ed0212015-06-08 17:56:03 -07008420#endif /* UPB_JSON_TYPED_PRINTER_H_ */