Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1 | // Amalgamated source file |
| 2 | /* |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 3 | ** Defs are upb's internal representation of the constructs that can appear |
| 4 | ** in a .proto file: |
| 5 | ** |
| 6 | ** - upb::MessageDef (upb_msgdef): describes a "message" construct. |
| 7 | ** - upb::FieldDef (upb_fielddef): describes a message field. |
| 8 | ** - upb::EnumDef (upb_enumdef): describes an enum. |
| 9 | ** - upb::OneofDef (upb_oneofdef): describes a oneof. |
| 10 | ** - upb::Def (upb_def): base class of all the others. |
| 11 | ** |
| 12 | ** TODO: definitions of services. |
| 13 | ** |
| 14 | ** Like upb_refcounted objects, defs are mutable only until frozen, and are |
| 15 | ** only thread-safe once frozen. |
| 16 | ** |
| 17 | ** This is a mixed C/C++ interface that offers a full API to both languages. |
| 18 | ** See the top-level README for more information. |
| 19 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 20 | |
| 21 | #ifndef UPB_DEF_H_ |
| 22 | #define UPB_DEF_H_ |
| 23 | |
| 24 | /* |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 25 | ** upb::RefCounted (upb_refcounted) |
| 26 | ** |
| 27 | ** A refcounting scheme that supports circular refs. It accomplishes this by |
| 28 | ** partitioning the set of objects into groups such that no cycle spans groups; |
| 29 | ** we can then reference-count the group as a whole and ignore refs within the |
| 30 | ** group. When objects are mutable, these groups are computed very |
| 31 | ** conservatively; we group any objects that have ever had a link between them. |
| 32 | ** When objects are frozen, we compute strongly-connected components which |
| 33 | ** allows us to be precise and only group objects that are actually cyclic. |
| 34 | ** |
| 35 | ** This is a mixed C/C++ interface that offers a full API to both languages. |
| 36 | ** See the top-level README for more information. |
| 37 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 38 | |
| 39 | #ifndef UPB_REFCOUNTED_H_ |
| 40 | #define UPB_REFCOUNTED_H_ |
| 41 | |
| 42 | /* |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 43 | ** upb_table |
| 44 | ** |
| 45 | ** This header is INTERNAL-ONLY! Its interfaces are not public or stable! |
| 46 | ** This file defines very fast int->upb_value (inttable) and string->upb_value |
| 47 | ** (strtable) hash tables. |
| 48 | ** |
| 49 | ** The table uses chained scatter with Brent's variation (inspired by the Lua |
| 50 | ** implementation of hash tables). The hash function for strings is Austin |
| 51 | ** Appleby's "MurmurHash." |
| 52 | ** |
| 53 | ** The inttable uses uintptr_t as its key, which guarantees it can be used to |
| 54 | ** store pointers or integers of at least 32 bits (upb isn't really useful on |
| 55 | ** systems where sizeof(void*) < 4). |
| 56 | ** |
| 57 | ** The table must be homogenous (all values of the same type). In debug |
| 58 | ** mode, we check this on insert and lookup. |
| 59 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 60 | |
| 61 | #ifndef UPB_TABLE_H_ |
| 62 | #define UPB_TABLE_H_ |
| 63 | |
| 64 | #include <assert.h> |
| 65 | #include <stdint.h> |
| 66 | #include <string.h> |
| 67 | /* |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 68 | ** This file contains shared definitions that are widely used across upb. |
| 69 | ** |
| 70 | ** This is a mixed C/C++ interface that offers a full API to both languages. |
| 71 | ** See the top-level README for more information. |
| 72 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 73 | |
| 74 | #ifndef UPB_H_ |
| 75 | #define UPB_H_ |
| 76 | |
| 77 | #include <assert.h> |
| 78 | #include <stdarg.h> |
| 79 | #include <stdbool.h> |
| 80 | #include <stddef.h> |
| 81 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 82 | /* UPB_INLINE: inline if possible, emit standalone code if required. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 83 | #ifdef __cplusplus |
| 84 | #define UPB_INLINE inline |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 85 | #elif defined (__GNUC__) |
| 86 | #define UPB_INLINE static __inline__ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 87 | #else |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 88 | #define UPB_INLINE static |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 89 | #endif |
| 90 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 91 | /* Define UPB_BIG_ENDIAN manually if you're on big endian and your compiler |
| 92 | * doesn't provide these preprocessor symbols. */ |
| 93 | #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) |
| 94 | #define UPB_BIG_ENDIAN |
| 95 | #endif |
| 96 | |
| 97 | /* Macros for function attributes on compilers that support them. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 98 | #ifdef __GNUC__ |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 99 | #define UPB_FORCEINLINE __inline__ __attribute__((always_inline)) |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 100 | #define UPB_NOINLINE __attribute__((noinline)) |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 101 | #define UPB_NORETURN __attribute__((__noreturn__)) |
| 102 | #else /* !defined(__GNUC__) */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 103 | #define UPB_FORCEINLINE |
| 104 | #define UPB_NOINLINE |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 105 | #define UPB_NORETURN |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 106 | #endif |
| 107 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 108 | /* A few hacky workarounds for functions not in C89. |
| 109 | * For internal use only! |
| 110 | * TODO(haberman): fix these by including our own implementations, or finding |
| 111 | * another workaround. |
| 112 | */ |
| 113 | #ifdef __GNUC__ |
| 114 | #define _upb_snprintf __builtin_snprintf |
| 115 | #define _upb_vsnprintf __builtin_vsnprintf |
| 116 | #define _upb_va_copy(a, b) __va_copy(a, b) |
| 117 | #elif __STDC_VERSION__ >= 199901L |
| 118 | /* C99 versions. */ |
| 119 | #define _upb_snprintf snprintf |
| 120 | #define _upb_vsnprintf vsnprintf |
| 121 | #define _upb_va_copy(a, b) va_copy(a, b) |
| 122 | #else |
| 123 | #error Need implementations of [v]snprintf and va_copy |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 124 | #endif |
| 125 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 126 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 127 | #if ((defined(__cplusplus) && __cplusplus >= 201103L) || \ |
| 128 | defined(__GXX_EXPERIMENTAL_CXX0X__)) && !defined(UPB_NO_CXX11) |
| 129 | #define UPB_CXX11 |
| 130 | #endif |
| 131 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 132 | /* UPB_DISALLOW_COPY_AND_ASSIGN() |
| 133 | * UPB_DISALLOW_POD_OPS() |
| 134 | * |
| 135 | * Declare these in the "private" section of a C++ class to forbid copy/assign |
| 136 | * or all POD ops (construct, destruct, copy, assign) on that class. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 137 | #ifdef UPB_CXX11 |
| 138 | #include <type_traits> |
| 139 | #define UPB_DISALLOW_COPY_AND_ASSIGN(class_name) \ |
| 140 | class_name(const class_name&) = delete; \ |
| 141 | void operator=(const class_name&) = delete; |
| 142 | #define UPB_DISALLOW_POD_OPS(class_name, full_class_name) \ |
| 143 | class_name() = delete; \ |
| 144 | ~class_name() = delete; \ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 145 | UPB_DISALLOW_COPY_AND_ASSIGN(class_name) |
| 146 | #define UPB_ASSERT_STDLAYOUT(type) \ |
| 147 | static_assert(std::is_standard_layout<type>::value, \ |
| 148 | #type " must be standard layout"); |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 149 | #else /* !defined(UPB_CXX11) */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 150 | #define UPB_DISALLOW_COPY_AND_ASSIGN(class_name) \ |
| 151 | class_name(const class_name&); \ |
| 152 | void operator=(const class_name&); |
| 153 | #define UPB_DISALLOW_POD_OPS(class_name, full_class_name) \ |
| 154 | class_name(); \ |
| 155 | ~class_name(); \ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 156 | UPB_DISALLOW_COPY_AND_ASSIGN(class_name) |
| 157 | #define UPB_ASSERT_STDLAYOUT(type) |
| 158 | #endif |
| 159 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 160 | /* UPB_DECLARE_TYPE() |
| 161 | * UPB_DECLARE_DERIVED_TYPE() |
| 162 | * UPB_DECLARE_DERIVED_TYPE2() |
| 163 | * |
| 164 | * Macros for declaring C and C++ types both, including inheritance. |
| 165 | * The inheritance doesn't use real C++ inheritance, to stay compatible with C. |
| 166 | * |
| 167 | * These macros also provide upcasts: |
| 168 | * - in C: types-specific functions (ie. upb_foo_upcast(foo)) |
| 169 | * - in C++: upb::upcast(foo) along with implicit conversions |
| 170 | * |
| 171 | * Downcasts are not provided, but upb/def.h defines downcasts for upb::Def. */ |
| 172 | |
| 173 | #define UPB_C_UPCASTS(ty, base) \ |
| 174 | UPB_INLINE base *ty ## _upcast_mutable(ty *p) { return (base*)p; } \ |
| 175 | UPB_INLINE const base *ty ## _upcast(const ty *p) { return (const base*)p; } |
| 176 | |
| 177 | #define UPB_C_UPCASTS2(ty, base, base2) \ |
| 178 | UPB_C_UPCASTS(ty, base) \ |
| 179 | UPB_INLINE base2 *ty ## _upcast2_mutable(ty *p) { return (base2*)p; } \ |
| 180 | UPB_INLINE const base2 *ty ## _upcast2(const ty *p) { return (const base2*)p; } |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 181 | |
| 182 | #ifdef __cplusplus |
| 183 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 184 | #define UPB_BEGIN_EXTERN_C extern "C" { |
| 185 | #define UPB_END_EXTERN_C } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 186 | #define UPB_PRIVATE_FOR_CPP private: |
| 187 | #define UPB_DECLARE_TYPE(cppname, cname) typedef cppname cname; |
| 188 | |
| 189 | #define UPB_DECLARE_DERIVED_TYPE(cppname, cppbase, cname, cbase) \ |
| 190 | UPB_DECLARE_TYPE(cppname, cname) \ |
| 191 | UPB_C_UPCASTS(cname, cbase) \ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 192 | namespace upb { \ |
| 193 | template <> \ |
| 194 | class Pointer<cppname> : public PointerBase<cppname, cppbase> { \ |
| 195 | public: \ |
Josh Haberman | f654d49 | 2016-02-18 11:07:51 -0800 | [diff] [blame] | 196 | explicit Pointer(cppname* ptr) \ |
| 197 | : PointerBase<cppname, cppbase>(ptr) {} \ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 198 | }; \ |
| 199 | template <> \ |
| 200 | class Pointer<const cppname> \ |
| 201 | : public PointerBase<const cppname, const cppbase> { \ |
| 202 | public: \ |
Josh Haberman | f654d49 | 2016-02-18 11:07:51 -0800 | [diff] [blame] | 203 | explicit Pointer(const cppname* ptr) \ |
| 204 | : PointerBase<const cppname, const cppbase>(ptr) {} \ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 205 | }; \ |
| 206 | } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 207 | |
| 208 | #define UPB_DECLARE_DERIVED_TYPE2(cppname, cppbase, cppbase2, cname, cbase, \ |
| 209 | cbase2) \ |
| 210 | UPB_DECLARE_TYPE(cppname, cname) \ |
| 211 | UPB_C_UPCASTS2(cname, cbase, cbase2) \ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 212 | namespace upb { \ |
| 213 | template <> \ |
| 214 | class Pointer<cppname> : public PointerBase2<cppname, cppbase, cppbase2> { \ |
| 215 | public: \ |
Josh Haberman | f654d49 | 2016-02-18 11:07:51 -0800 | [diff] [blame] | 216 | explicit Pointer(cppname* ptr) \ |
| 217 | : PointerBase2<cppname, cppbase, cppbase2>(ptr) {} \ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 218 | }; \ |
| 219 | template <> \ |
| 220 | class Pointer<const cppname> \ |
| 221 | : public PointerBase2<const cppname, const cppbase, const cppbase2> { \ |
| 222 | public: \ |
Josh Haberman | f654d49 | 2016-02-18 11:07:51 -0800 | [diff] [blame] | 223 | explicit Pointer(const cppname* ptr) \ |
| 224 | : PointerBase2<const cppname, const cppbase, const cppbase2>(ptr) {} \ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 225 | }; \ |
| 226 | } |
| 227 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 228 | #else /* !defined(__cplusplus) */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 229 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 230 | #define UPB_BEGIN_EXTERN_C |
| 231 | #define UPB_END_EXTERN_C |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 232 | #define UPB_PRIVATE_FOR_CPP |
| 233 | #define UPB_DECLARE_TYPE(cppname, cname) \ |
| 234 | struct cname; \ |
| 235 | typedef struct cname cname; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 236 | #define UPB_DECLARE_DERIVED_TYPE(cppname, cppbase, cname, cbase) \ |
| 237 | UPB_DECLARE_TYPE(cppname, cname) \ |
| 238 | UPB_C_UPCASTS(cname, cbase) |
| 239 | #define UPB_DECLARE_DERIVED_TYPE2(cppname, cppbase, cppbase2, \ |
| 240 | cname, cbase, cbase2) \ |
| 241 | UPB_DECLARE_TYPE(cppname, cname) \ |
| 242 | UPB_C_UPCASTS2(cname, cbase, cbase2) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 243 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 244 | #endif /* defined(__cplusplus) */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 245 | |
| 246 | #define UPB_MAX(x, y) ((x) > (y) ? (x) : (y)) |
| 247 | #define UPB_MIN(x, y) ((x) < (y) ? (x) : (y)) |
| 248 | |
| 249 | #define UPB_UNUSED(var) (void)var |
| 250 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 251 | /* For asserting something about a variable when the variable is not used for |
| 252 | * anything else. This prevents "unused variable" warnings when compiling in |
| 253 | * debug mode. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 254 | #define UPB_ASSERT_VAR(var, predicate) UPB_UNUSED(var); assert(predicate) |
| 255 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 256 | /* Generic function type. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 257 | typedef void upb_func(); |
| 258 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 259 | /* C++ Casts ******************************************************************/ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 260 | |
| 261 | #ifdef __cplusplus |
| 262 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 263 | namespace upb { |
| 264 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 265 | template <class T> class Pointer; |
| 266 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 267 | /* Casts to a subclass. The caller must know that cast is correct; an |
| 268 | * incorrect cast will throw an assertion failure in debug mode. |
| 269 | * |
| 270 | * Example: |
| 271 | * upb::Def* def = GetDef(); |
| 272 | * // Assert-fails if this was not actually a MessageDef. |
| 273 | * upb::MessgeDef* md = upb::down_cast<upb::MessageDef>(def); |
| 274 | * |
| 275 | * Note that downcasts are only defined for some types (at the moment you can |
| 276 | * only downcast from a upb::Def to a specific Def type). */ |
| 277 | template<class To, class From> To down_cast(From* f); |
| 278 | |
| 279 | /* Casts to a subclass. If the class does not actually match the given To type, |
| 280 | * returns NULL. |
| 281 | * |
| 282 | * Example: |
| 283 | * upb::Def* def = GetDef(); |
| 284 | * // md will be NULL if this was not actually a MessageDef. |
| 285 | * upb::MessgeDef* md = upb::down_cast<upb::MessageDef>(def); |
| 286 | * |
| 287 | * Note that dynamic casts are only defined for some types (at the moment you |
| 288 | * can only downcast from a upb::Def to a specific Def type).. */ |
| 289 | template<class To, class From> To dyn_cast(From* f); |
| 290 | |
| 291 | /* Casts to any base class, or the type itself (ie. can be a no-op). |
| 292 | * |
| 293 | * Example: |
| 294 | * upb::MessageDef* md = GetDef(); |
| 295 | * // This will fail to compile if this wasn't actually a base class. |
| 296 | * upb::Def* def = upb::upcast(md); |
| 297 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 298 | template <class T> inline Pointer<T> upcast(T *f) { return Pointer<T>(f); } |
| 299 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 300 | /* Attempt upcast to specific base class. |
| 301 | * |
| 302 | * Example: |
| 303 | * upb::MessageDef* md = GetDef(); |
| 304 | * upb::upcast_to<upb::Def>(md)->MethodOnDef(); |
| 305 | */ |
| 306 | template <class T, class F> inline T* upcast_to(F *f) { |
| 307 | return static_cast<T*>(upcast(f)); |
| 308 | } |
| 309 | |
| 310 | /* PointerBase<T>: implementation detail of upb::upcast(). |
| 311 | * It is implicitly convertable to pointers to the Base class(es). |
| 312 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 313 | template <class T, class Base> |
| 314 | class PointerBase { |
| 315 | public: |
| 316 | explicit PointerBase(T* ptr) : ptr_(ptr) {} |
| 317 | operator T*() { return ptr_; } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 318 | operator Base*() { return (Base*)ptr_; } |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 319 | |
| 320 | private: |
| 321 | T* ptr_; |
| 322 | }; |
| 323 | |
| 324 | template <class T, class Base, class Base2> |
| 325 | class PointerBase2 : public PointerBase<T, Base> { |
| 326 | public: |
| 327 | explicit PointerBase2(T* ptr) : PointerBase<T, Base>(ptr) {} |
| 328 | operator Base2*() { return Pointer<Base>(*this); } |
| 329 | }; |
| 330 | |
| 331 | } |
| 332 | |
| 333 | #endif |
| 334 | |
| 335 | |
| 336 | /* upb::reffed_ptr ************************************************************/ |
| 337 | |
| 338 | #ifdef __cplusplus |
| 339 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 340 | #include <algorithm> /* For std::swap(). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 341 | |
| 342 | namespace upb { |
| 343 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 344 | /* Provides RAII semantics for upb refcounted objects. Each reffed_ptr owns a |
| 345 | * ref on whatever object it points to (if any). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 346 | template <class T> class reffed_ptr { |
| 347 | public: |
| 348 | reffed_ptr() : ptr_(NULL) {} |
| 349 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 350 | /* If ref_donor is NULL, takes a new ref, otherwise adopts from ref_donor. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 351 | template <class U> |
| 352 | reffed_ptr(U* val, const void* ref_donor = NULL) |
| 353 | : ptr_(upb::upcast(val)) { |
| 354 | if (ref_donor) { |
| 355 | assert(ptr_); |
| 356 | ptr_->DonateRef(ref_donor, this); |
| 357 | } else if (ptr_) { |
| 358 | ptr_->Ref(this); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | template <class U> |
| 363 | reffed_ptr(const reffed_ptr<U>& other) |
| 364 | : ptr_(upb::upcast(other.get())) { |
| 365 | if (ptr_) ptr_->Ref(this); |
| 366 | } |
| 367 | |
| 368 | ~reffed_ptr() { if (ptr_) ptr_->Unref(this); } |
| 369 | |
| 370 | template <class U> |
| 371 | reffed_ptr& operator=(const reffed_ptr<U>& other) { |
| 372 | reset(other.get()); |
| 373 | return *this; |
| 374 | } |
| 375 | |
| 376 | reffed_ptr& operator=(const reffed_ptr& other) { |
| 377 | reset(other.get()); |
| 378 | return *this; |
| 379 | } |
| 380 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 381 | /* TODO(haberman): add C++11 move construction/assignment for greater |
| 382 | * efficiency. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 383 | |
| 384 | void swap(reffed_ptr& other) { |
| 385 | if (ptr_ == other.ptr_) { |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | if (ptr_) ptr_->DonateRef(this, &other); |
| 390 | if (other.ptr_) other.ptr_->DonateRef(&other, this); |
| 391 | std::swap(ptr_, other.ptr_); |
| 392 | } |
| 393 | |
| 394 | T& operator*() const { |
| 395 | assert(ptr_); |
| 396 | return *ptr_; |
| 397 | } |
| 398 | |
| 399 | T* operator->() const { |
| 400 | assert(ptr_); |
| 401 | return ptr_; |
| 402 | } |
| 403 | |
| 404 | T* get() const { return ptr_; } |
| 405 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 406 | /* If ref_donor is NULL, takes a new ref, otherwise adopts from ref_donor. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 407 | template <class U> |
| 408 | void reset(U* ptr = NULL, const void* ref_donor = NULL) { |
| 409 | reffed_ptr(ptr, ref_donor).swap(*this); |
| 410 | } |
| 411 | |
| 412 | template <class U> |
| 413 | reffed_ptr<U> down_cast() { |
| 414 | return reffed_ptr<U>(upb::down_cast<U*>(get())); |
| 415 | } |
| 416 | |
| 417 | template <class U> |
| 418 | reffed_ptr<U> dyn_cast() { |
| 419 | return reffed_ptr<U>(upb::dyn_cast<U*>(get())); |
| 420 | } |
| 421 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 422 | /* Plain release() is unsafe; if we were the only owner, it would leak the |
| 423 | * object. Instead we provide this: */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 424 | T* ReleaseTo(const void* new_owner) { |
| 425 | T* ret = NULL; |
| 426 | ptr_->DonateRef(this, new_owner); |
| 427 | std::swap(ret, ptr_); |
| 428 | return ret; |
| 429 | } |
| 430 | |
| 431 | private: |
| 432 | T* ptr_; |
| 433 | }; |
| 434 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 435 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 436 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 437 | #endif /* __cplusplus */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 438 | |
| 439 | |
| 440 | /* upb::Status ****************************************************************/ |
| 441 | |
| 442 | #ifdef __cplusplus |
| 443 | namespace upb { |
| 444 | class ErrorSpace; |
| 445 | class Status; |
| 446 | } |
| 447 | #endif |
| 448 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 449 | UPB_DECLARE_TYPE(upb::ErrorSpace, upb_errorspace) |
| 450 | UPB_DECLARE_TYPE(upb::Status, upb_status) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 451 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 452 | /* The maximum length of an error message before it will get truncated. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 453 | #define UPB_STATUS_MAX_MESSAGE 128 |
| 454 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 455 | /* An error callback function is used to report errors from some component. |
| 456 | * The function can return "true" to indicate that the component should try |
| 457 | * to recover and proceed, but this is not always possible. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 458 | typedef bool upb_errcb_t(void *closure, const upb_status* status); |
| 459 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 460 | #ifdef __cplusplus |
| 461 | class upb::ErrorSpace { |
| 462 | #else |
| 463 | struct upb_errorspace { |
| 464 | #endif |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 465 | const char *name; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 466 | /* Should the error message in the status object according to this code. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 467 | void (*set_message)(upb_status* status, int code); |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 468 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 469 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 470 | #ifdef __cplusplus |
| 471 | |
| 472 | /* Object representing a success or failure status. |
| 473 | * It owns no resources and allocates no memory, so it should work |
| 474 | * even in OOM situations. */ |
| 475 | |
| 476 | class upb::Status { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 477 | public: |
| 478 | Status(); |
| 479 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 480 | /* Returns true if there is no error. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 481 | bool ok() const; |
| 482 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 483 | /* Optional error space and code, useful if the caller wants to |
| 484 | * programmatically check the specific kind of error. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 485 | ErrorSpace* error_space(); |
| 486 | int code() const; |
| 487 | |
| 488 | const char *error_message() const; |
| 489 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 490 | /* The error message will be truncated if it is longer than |
| 491 | * UPB_STATUS_MAX_MESSAGE-4. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 492 | void SetErrorMessage(const char* msg); |
| 493 | void SetFormattedErrorMessage(const char* fmt, ...); |
| 494 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 495 | /* If there is no error message already, this will use the ErrorSpace to |
| 496 | * populate the error message for this code. The caller can still call |
| 497 | * SetErrorMessage() to give a more specific message. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 498 | void SetErrorCode(ErrorSpace* space, int code); |
| 499 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 500 | /* Resets the status to a successful state with no message. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 501 | void Clear(); |
| 502 | |
| 503 | void CopyFrom(const Status& other); |
| 504 | |
| 505 | private: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 506 | UPB_DISALLOW_COPY_AND_ASSIGN(Status) |
| 507 | #else |
| 508 | struct upb_status { |
| 509 | #endif |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 510 | bool ok_; |
| 511 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 512 | /* Specific status code defined by some error space (optional). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 513 | int code_; |
| 514 | upb_errorspace *error_space_; |
| 515 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 516 | /* Error message; NULL-terminated. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 517 | char msg[UPB_STATUS_MAX_MESSAGE]; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 518 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 519 | |
| 520 | #define UPB_STATUS_INIT {true, 0, NULL, {0}} |
| 521 | |
| 522 | #ifdef __cplusplus |
| 523 | extern "C" { |
| 524 | #endif |
| 525 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 526 | /* The returned string is invalidated by any other call into the status. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 527 | const char *upb_status_errmsg(const upb_status *status); |
| 528 | bool upb_ok(const upb_status *status); |
| 529 | upb_errorspace *upb_status_errspace(const upb_status *status); |
| 530 | int upb_status_errcode(const upb_status *status); |
| 531 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 532 | /* Any of the functions that write to a status object allow status to be NULL, |
| 533 | * to support use cases where the function's caller does not care about the |
| 534 | * status message. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 535 | void upb_status_clear(upb_status *status); |
| 536 | void upb_status_seterrmsg(upb_status *status, const char *msg); |
| 537 | void upb_status_seterrf(upb_status *status, const char *fmt, ...); |
| 538 | void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args); |
| 539 | void upb_status_seterrcode(upb_status *status, upb_errorspace *space, int code); |
| 540 | void upb_status_copy(upb_status *to, const upb_status *from); |
| 541 | |
| 542 | #ifdef __cplusplus |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 543 | } /* extern "C" */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 544 | |
| 545 | namespace upb { |
| 546 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 547 | /* C++ Wrappers */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 548 | inline Status::Status() { Clear(); } |
| 549 | inline bool Status::ok() const { return upb_ok(this); } |
| 550 | inline const char* Status::error_message() const { |
| 551 | return upb_status_errmsg(this); |
| 552 | } |
| 553 | inline void Status::SetErrorMessage(const char* msg) { |
| 554 | upb_status_seterrmsg(this, msg); |
| 555 | } |
| 556 | inline void Status::SetFormattedErrorMessage(const char* fmt, ...) { |
| 557 | va_list args; |
| 558 | va_start(args, fmt); |
| 559 | upb_status_vseterrf(this, fmt, args); |
| 560 | va_end(args); |
| 561 | } |
| 562 | inline void Status::SetErrorCode(ErrorSpace* space, int code) { |
| 563 | upb_status_seterrcode(this, space, code); |
| 564 | } |
| 565 | inline void Status::Clear() { upb_status_clear(this); } |
| 566 | inline void Status::CopyFrom(const Status& other) { |
| 567 | upb_status_copy(this, &other); |
| 568 | } |
| 569 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 570 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 571 | |
| 572 | #endif |
| 573 | |
| 574 | #endif /* UPB_H_ */ |
| 575 | |
| 576 | #ifdef __cplusplus |
| 577 | extern "C" { |
| 578 | #endif |
| 579 | |
| 580 | |
| 581 | /* upb_value ******************************************************************/ |
| 582 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 583 | /* A tagged union (stored untagged inside the table) so that we can check that |
| 584 | * clients calling table accessors are correctly typed without having to have |
| 585 | * an explosion of accessors. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 586 | typedef enum { |
| 587 | UPB_CTYPE_INT32 = 1, |
| 588 | UPB_CTYPE_INT64 = 2, |
| 589 | UPB_CTYPE_UINT32 = 3, |
| 590 | UPB_CTYPE_UINT64 = 4, |
| 591 | UPB_CTYPE_BOOL = 5, |
| 592 | UPB_CTYPE_CSTR = 6, |
| 593 | UPB_CTYPE_PTR = 7, |
| 594 | UPB_CTYPE_CONSTPTR = 8, |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 595 | UPB_CTYPE_FPTR = 9 |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 596 | } upb_ctype_t; |
| 597 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 598 | typedef struct { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 599 | uint64_t val; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 600 | #ifndef NDEBUG |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 601 | /* In debug mode we carry the value type around also so we can check accesses |
| 602 | * to be sure the right member is being read. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 603 | upb_ctype_t ctype; |
| 604 | #endif |
| 605 | } upb_value; |
| 606 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 607 | #ifdef NDEBUG |
| 608 | #define SET_TYPE(dest, val) UPB_UNUSED(val) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 609 | #else |
| 610 | #define SET_TYPE(dest, val) dest = val |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 611 | #endif |
| 612 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 613 | /* Like strdup(), which isn't always available since it's not ANSI C. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 614 | char *upb_strdup(const char *s); |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 615 | /* Variant that works with a length-delimited rather than NULL-delimited string, |
| 616 | * as supported by strtable. */ |
Chris Fallin | fd1a3ff | 2015-01-06 15:44:09 -0800 | [diff] [blame] | 617 | char *upb_strdup2(const char *s, size_t len); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 618 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 619 | UPB_INLINE void _upb_value_setval(upb_value *v, uint64_t val, |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 620 | upb_ctype_t ctype) { |
| 621 | v->val = val; |
| 622 | SET_TYPE(v->ctype, ctype); |
| 623 | } |
| 624 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 625 | UPB_INLINE upb_value _upb_value_val(uint64_t val, upb_ctype_t ctype) { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 626 | upb_value ret; |
| 627 | _upb_value_setval(&ret, val, ctype); |
| 628 | return ret; |
| 629 | } |
| 630 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 631 | /* For each value ctype, define the following set of functions: |
| 632 | * |
| 633 | * // Get/set an int32 from a upb_value. |
| 634 | * int32_t upb_value_getint32(upb_value val); |
| 635 | * void upb_value_setint32(upb_value *val, int32_t cval); |
| 636 | * |
| 637 | * // Construct a new upb_value from an int32. |
| 638 | * upb_value upb_value_int32(int32_t val); */ |
| 639 | #define FUNCS(name, membername, type_t, converter, proto_type) \ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 640 | UPB_INLINE void upb_value_set ## name(upb_value *val, type_t cval) { \ |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 641 | val->val = (converter)cval; \ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 642 | SET_TYPE(val->ctype, proto_type); \ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 643 | } \ |
| 644 | UPB_INLINE upb_value upb_value_ ## name(type_t val) { \ |
| 645 | upb_value ret; \ |
| 646 | upb_value_set ## name(&ret, val); \ |
| 647 | return ret; \ |
| 648 | } \ |
| 649 | UPB_INLINE type_t upb_value_get ## name(upb_value val) { \ |
| 650 | assert(val.ctype == proto_type); \ |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 651 | return (type_t)(converter)val.val; \ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 652 | } |
| 653 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 654 | FUNCS(int32, int32, int32_t, int32_t, UPB_CTYPE_INT32) |
| 655 | FUNCS(int64, int64, int64_t, int64_t, UPB_CTYPE_INT64) |
| 656 | FUNCS(uint32, uint32, uint32_t, uint32_t, UPB_CTYPE_UINT32) |
| 657 | FUNCS(uint64, uint64, uint64_t, uint64_t, UPB_CTYPE_UINT64) |
| 658 | FUNCS(bool, _bool, bool, bool, UPB_CTYPE_BOOL) |
| 659 | FUNCS(cstr, cstr, char*, uintptr_t, UPB_CTYPE_CSTR) |
| 660 | FUNCS(ptr, ptr, void*, uintptr_t, UPB_CTYPE_PTR) |
| 661 | FUNCS(constptr, constptr, const void*, uintptr_t, UPB_CTYPE_CONSTPTR) |
| 662 | FUNCS(fptr, fptr, upb_func*, uintptr_t, UPB_CTYPE_FPTR) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 663 | |
| 664 | #undef FUNCS |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 665 | #undef SET_TYPE |
| 666 | |
| 667 | |
| 668 | /* upb_tabkey *****************************************************************/ |
| 669 | |
| 670 | /* Either: |
| 671 | * 1. an actual integer key, or |
| 672 | * 2. a pointer to a string prefixed by its uint32_t length, owned by us. |
| 673 | * |
| 674 | * ...depending on whether this is a string table or an int table. We would |
| 675 | * make this a union of those two types, but C89 doesn't support statically |
| 676 | * initializing a non-first union member. */ |
| 677 | typedef uintptr_t upb_tabkey; |
| 678 | |
| 679 | #define UPB_TABKEY_NUM(n) n |
| 680 | #define UPB_TABKEY_NONE 0 |
| 681 | /* The preprocessor isn't quite powerful enough to turn the compile-time string |
| 682 | * length into a byte-wise string representation, so code generation needs to |
| 683 | * help it along. |
| 684 | * |
| 685 | * "len1" is the low byte and len4 is the high byte. */ |
| 686 | #ifdef UPB_BIG_ENDIAN |
| 687 | #define UPB_TABKEY_STR(len1, len2, len3, len4, strval) \ |
| 688 | (uintptr_t)(len4 len3 len2 len1 strval) |
| 689 | #else |
| 690 | #define UPB_TABKEY_STR(len1, len2, len3, len4, strval) \ |
| 691 | (uintptr_t)(len1 len2 len3 len4 strval) |
| 692 | #endif |
| 693 | |
| 694 | UPB_INLINE char *upb_tabstr(upb_tabkey key, uint32_t *len) { |
| 695 | char* mem = (char*)key; |
| 696 | if (len) memcpy(len, mem, sizeof(*len)); |
| 697 | return mem + sizeof(*len); |
| 698 | } |
| 699 | |
| 700 | |
| 701 | /* upb_tabval *****************************************************************/ |
| 702 | |
| 703 | #ifdef __cplusplus |
| 704 | |
| 705 | /* Status initialization not supported. |
| 706 | * |
| 707 | * This separate definition is necessary because in C++, UINTPTR_MAX isn't |
| 708 | * reliably available. */ |
| 709 | typedef struct { |
| 710 | uint64_t val; |
| 711 | } upb_tabval; |
| 712 | |
| 713 | #else |
| 714 | |
| 715 | /* C -- supports static initialization, but to support static initialization of |
| 716 | * both integers and points for both 32 and 64 bit targets, it takes a little |
| 717 | * bit of doing. */ |
| 718 | |
| 719 | #if UINTPTR_MAX == 0xffffffffffffffffULL |
| 720 | #define UPB_PTR_IS_64BITS |
| 721 | #elif UINTPTR_MAX != 0xffffffff |
| 722 | #error Could not determine how many bits pointers are. |
| 723 | #endif |
| 724 | |
| 725 | typedef union { |
| 726 | /* For static initialization. |
| 727 | * |
| 728 | * Unfortunately this ugliness is necessary -- it is the only way that we can, |
| 729 | * with -std=c89 -pedantic, statically initialize this to either a pointer or |
| 730 | * an integer on 32-bit platforms. */ |
| 731 | struct { |
| 732 | #ifdef UPB_PTR_IS_64BITS |
| 733 | uintptr_t val; |
| 734 | #else |
| 735 | uintptr_t val1; |
| 736 | uintptr_t val2; |
| 737 | #endif |
| 738 | } staticinit; |
| 739 | |
| 740 | /* The normal accessor that we use for everything at runtime. */ |
| 741 | uint64_t val; |
| 742 | } upb_tabval; |
| 743 | |
| 744 | #ifdef UPB_PTR_IS_64BITS |
| 745 | #define UPB_TABVALUE_INT_INIT(v) {{v}} |
| 746 | #define UPB_TABVALUE_EMPTY_INIT {{-1}} |
| 747 | #else |
| 748 | |
| 749 | /* 32-bit pointers */ |
| 750 | |
| 751 | #ifdef UPB_BIG_ENDIAN |
| 752 | #define UPB_TABVALUE_INT_INIT(v) {{0, v}} |
| 753 | #define UPB_TABVALUE_EMPTY_INIT {{-1, -1}} |
| 754 | #else |
| 755 | #define UPB_TABVALUE_INT_INIT(v) {{v, 0}} |
| 756 | #define UPB_TABVALUE_EMPTY_INIT {{-1, -1}} |
| 757 | #endif |
| 758 | |
| 759 | #endif |
| 760 | |
| 761 | #define UPB_TABVALUE_PTR_INIT(v) UPB_TABVALUE_INT_INIT((uintptr_t)v) |
| 762 | |
| 763 | #undef UPB_PTR_IS_64BITS |
| 764 | |
| 765 | #endif /* __cplusplus */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 766 | |
| 767 | |
| 768 | /* upb_table ******************************************************************/ |
| 769 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 770 | typedef struct _upb_tabent { |
| 771 | upb_tabkey key; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 772 | upb_tabval val; |
| 773 | |
| 774 | /* Internal chaining. This is const so we can create static initializers for |
| 775 | * tables. We cast away const sometimes, but *only* when the containing |
| 776 | * upb_table is known to be non-const. This requires a bit of care, but |
| 777 | * the subtlety is confined to table.c. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 778 | const struct _upb_tabent *next; |
| 779 | } upb_tabent; |
| 780 | |
| 781 | typedef struct { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 782 | size_t count; /* Number of entries in the hash part. */ |
| 783 | size_t mask; /* Mask to turn hash value -> bucket. */ |
| 784 | upb_ctype_t ctype; /* Type of all values. */ |
| 785 | uint8_t size_lg2; /* Size of the hashtable part is 2^size_lg2 entries. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 786 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 787 | /* Hash table entries. |
| 788 | * Making this const isn't entirely accurate; what we really want is for it to |
| 789 | * have the same const-ness as the table it's inside. But there's no way to |
| 790 | * declare that in C. So we have to make it const so that we can statically |
| 791 | * initialize const hash tables. Then we cast away const when we have to. |
| 792 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 793 | const upb_tabent *entries; |
| 794 | } upb_table; |
| 795 | |
| 796 | typedef struct { |
| 797 | upb_table t; |
| 798 | } upb_strtable; |
| 799 | |
| 800 | #define UPB_STRTABLE_INIT(count, mask, ctype, size_lg2, entries) \ |
| 801 | {{count, mask, ctype, size_lg2, entries}} |
| 802 | |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 803 | #define UPB_EMPTY_STRTABLE_INIT(ctype) \ |
| 804 | UPB_STRTABLE_INIT(0, 0, ctype, 0, NULL) |
| 805 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 806 | typedef struct { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 807 | upb_table t; /* For entries that don't fit in the array part. */ |
| 808 | const upb_tabval *array; /* Array part of the table. See const note above. */ |
| 809 | size_t array_size; /* Array part size. */ |
| 810 | size_t array_count; /* Array part number of elements. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 811 | } upb_inttable; |
| 812 | |
| 813 | #define UPB_INTTABLE_INIT(count, mask, ctype, size_lg2, ent, a, asize, acount) \ |
| 814 | {{count, mask, ctype, size_lg2, ent}, a, asize, acount} |
| 815 | |
| 816 | #define UPB_EMPTY_INTTABLE_INIT(ctype) \ |
| 817 | UPB_INTTABLE_INIT(0, 0, ctype, 0, NULL, NULL, 0, 0) |
| 818 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 819 | #define UPB_ARRAY_EMPTYENT -1 |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 820 | |
| 821 | UPB_INLINE size_t upb_table_size(const upb_table *t) { |
| 822 | if (t->size_lg2 == 0) |
| 823 | return 0; |
| 824 | else |
| 825 | return 1 << t->size_lg2; |
| 826 | } |
| 827 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 828 | /* Internal-only functions, in .h file only out of necessity. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 829 | UPB_INLINE bool upb_tabent_isempty(const upb_tabent *e) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 830 | return e->key == 0; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 831 | } |
| 832 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 833 | /* Used by some of the unit tests for generic hashing functionality. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 834 | uint32_t MurmurHash2(const void * key, size_t len, uint32_t seed); |
| 835 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 836 | UPB_INLINE uintptr_t upb_intkey(uintptr_t key) { |
| 837 | return key; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | UPB_INLINE uint32_t upb_inthash(uintptr_t key) { |
| 841 | return (uint32_t)key; |
| 842 | } |
| 843 | |
| 844 | static const upb_tabent *upb_getentry(const upb_table *t, uint32_t hash) { |
| 845 | return t->entries + (hash & t->mask); |
| 846 | } |
| 847 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 848 | UPB_INLINE bool upb_arrhas(upb_tabval key) { |
| 849 | return key.val != (uint64_t)-1; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 850 | } |
| 851 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 852 | /* Initialize and uninitialize a table, respectively. If memory allocation |
| 853 | * failed, false is returned that the table is uninitialized. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 854 | bool upb_inttable_init(upb_inttable *table, upb_ctype_t ctype); |
| 855 | bool upb_strtable_init(upb_strtable *table, upb_ctype_t ctype); |
| 856 | void upb_inttable_uninit(upb_inttable *table); |
| 857 | void upb_strtable_uninit(upb_strtable *table); |
| 858 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 859 | /* Returns the number of values in the table. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 860 | size_t upb_inttable_count(const upb_inttable *t); |
| 861 | UPB_INLINE size_t upb_strtable_count(const upb_strtable *t) { |
| 862 | return t->t.count; |
| 863 | } |
| 864 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 865 | /* Inserts the given key into the hashtable with the given value. The key must |
| 866 | * not already exist in the hash table. For string tables, the key must be |
| 867 | * NULL-terminated, and the table will make an internal copy of the key. |
| 868 | * Inttables must not insert a value of UINTPTR_MAX. |
| 869 | * |
| 870 | * If a table resize was required but memory allocation failed, false is |
| 871 | * returned and the table is unchanged. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 872 | bool upb_inttable_insert(upb_inttable *t, uintptr_t key, upb_value val); |
Chris Fallin | fd1a3ff | 2015-01-06 15:44:09 -0800 | [diff] [blame] | 873 | bool upb_strtable_insert2(upb_strtable *t, const char *key, size_t len, |
| 874 | upb_value val); |
| 875 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 876 | /* For NULL-terminated strings. */ |
Chris Fallin | fd1a3ff | 2015-01-06 15:44:09 -0800 | [diff] [blame] | 877 | UPB_INLINE bool upb_strtable_insert(upb_strtable *t, const char *key, |
| 878 | upb_value val) { |
| 879 | return upb_strtable_insert2(t, key, strlen(key), val); |
| 880 | } |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 881 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 882 | /* Looks up key in this table, returning "true" if the key was found. |
| 883 | * If v is non-NULL, copies the value for this key into *v. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 884 | bool upb_inttable_lookup(const upb_inttable *t, uintptr_t key, upb_value *v); |
| 885 | bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len, |
| 886 | upb_value *v); |
| 887 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 888 | /* For NULL-terminated strings. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 889 | UPB_INLINE bool upb_strtable_lookup(const upb_strtable *t, const char *key, |
| 890 | upb_value *v) { |
| 891 | return upb_strtable_lookup2(t, key, strlen(key), v); |
| 892 | } |
| 893 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 894 | /* Removes an item from the table. Returns true if the remove was successful, |
| 895 | * and stores the removed item in *val if non-NULL. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 896 | bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val); |
Chris Fallin | fd1a3ff | 2015-01-06 15:44:09 -0800 | [diff] [blame] | 897 | bool upb_strtable_remove2(upb_strtable *t, const char *key, size_t len, |
| 898 | upb_value *val); |
| 899 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 900 | /* For NULL-terminated strings. */ |
Chris Fallin | fd1a3ff | 2015-01-06 15:44:09 -0800 | [diff] [blame] | 901 | UPB_INLINE bool upb_strtable_remove(upb_strtable *t, const char *key, |
| 902 | upb_value *v) { |
| 903 | return upb_strtable_remove2(t, key, strlen(key), v); |
| 904 | } |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 905 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 906 | /* Updates an existing entry in an inttable. If the entry does not exist, |
| 907 | * returns false and does nothing. Unlike insert/remove, this does not |
| 908 | * invalidate iterators. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 909 | bool upb_inttable_replace(upb_inttable *t, uintptr_t key, upb_value val); |
| 910 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 911 | /* Handy routines for treating an inttable like a stack. May not be mixed with |
| 912 | * other insert/remove calls. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 913 | bool upb_inttable_push(upb_inttable *t, upb_value val); |
| 914 | upb_value upb_inttable_pop(upb_inttable *t); |
| 915 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 916 | /* Convenience routines for inttables with pointer keys. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 917 | bool upb_inttable_insertptr(upb_inttable *t, const void *key, upb_value val); |
| 918 | bool upb_inttable_removeptr(upb_inttable *t, const void *key, upb_value *val); |
| 919 | bool upb_inttable_lookupptr( |
| 920 | const upb_inttable *t, const void *key, upb_value *val); |
| 921 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 922 | /* Optimizes the table for the current set of entries, for both memory use and |
| 923 | * lookup time. Client should call this after all entries have been inserted; |
| 924 | * inserting more entries is legal, but will likely require a table resize. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 925 | void upb_inttable_compact(upb_inttable *t); |
| 926 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 927 | /* A special-case inlinable version of the lookup routine for 32-bit |
| 928 | * integers. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 929 | UPB_INLINE bool upb_inttable_lookup32(const upb_inttable *t, uint32_t key, |
| 930 | upb_value *v) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 931 | *v = upb_value_int32(0); /* Silence compiler warnings. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 932 | if (key < t->array_size) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 933 | upb_tabval arrval = t->array[key]; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 934 | if (upb_arrhas(arrval)) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 935 | _upb_value_setval(v, arrval.val, t->t.ctype); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 936 | return true; |
| 937 | } else { |
| 938 | return false; |
| 939 | } |
| 940 | } else { |
| 941 | const upb_tabent *e; |
| 942 | if (t->t.entries == NULL) return false; |
| 943 | for (e = upb_getentry(&t->t, upb_inthash(key)); true; e = e->next) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 944 | if ((uint32_t)e->key == key) { |
| 945 | _upb_value_setval(v, e->val.val, t->t.ctype); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 946 | return true; |
| 947 | } |
| 948 | if (e->next == NULL) return false; |
| 949 | } |
| 950 | } |
| 951 | } |
| 952 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 953 | /* Exposed for testing only. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 954 | bool upb_strtable_resize(upb_strtable *t, size_t size_lg2); |
| 955 | |
| 956 | /* Iterators ******************************************************************/ |
| 957 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 958 | /* Iterators for int and string tables. We are subject to some kind of unusual |
| 959 | * design constraints: |
| 960 | * |
| 961 | * For high-level languages: |
| 962 | * - we must be able to guarantee that we don't crash or corrupt memory even if |
| 963 | * the program accesses an invalidated iterator. |
| 964 | * |
| 965 | * For C++11 range-based for: |
| 966 | * - iterators must be copyable |
| 967 | * - iterators must be comparable |
| 968 | * - it must be possible to construct an "end" value. |
| 969 | * |
| 970 | * Iteration order is undefined. |
| 971 | * |
| 972 | * Modifying the table invalidates iterators. upb_{str,int}table_done() is |
| 973 | * guaranteed to work even on an invalidated iterator, as long as the table it |
| 974 | * is iterating over has not been freed. Calling next() or accessing data from |
| 975 | * an invalidated iterator yields unspecified elements from the table, but it is |
| 976 | * guaranteed not to crash and to return real table elements (except when done() |
| 977 | * is true). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 978 | |
| 979 | |
| 980 | /* upb_strtable_iter **********************************************************/ |
| 981 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 982 | /* upb_strtable_iter i; |
| 983 | * upb_strtable_begin(&i, t); |
| 984 | * for(; !upb_strtable_done(&i); upb_strtable_next(&i)) { |
| 985 | * const char *key = upb_strtable_iter_key(&i); |
| 986 | * const upb_value val = upb_strtable_iter_value(&i); |
| 987 | * // ... |
| 988 | * } |
| 989 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 990 | |
| 991 | typedef struct { |
| 992 | const upb_strtable *t; |
| 993 | size_t index; |
| 994 | } upb_strtable_iter; |
| 995 | |
| 996 | void upb_strtable_begin(upb_strtable_iter *i, const upb_strtable *t); |
| 997 | void upb_strtable_next(upb_strtable_iter *i); |
| 998 | bool upb_strtable_done(const upb_strtable_iter *i); |
| 999 | const char *upb_strtable_iter_key(upb_strtable_iter *i); |
Chris Fallin | fd1a3ff | 2015-01-06 15:44:09 -0800 | [diff] [blame] | 1000 | size_t upb_strtable_iter_keylength(upb_strtable_iter *i); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1001 | upb_value upb_strtable_iter_value(const upb_strtable_iter *i); |
| 1002 | void upb_strtable_iter_setdone(upb_strtable_iter *i); |
| 1003 | bool upb_strtable_iter_isequal(const upb_strtable_iter *i1, |
| 1004 | const upb_strtable_iter *i2); |
| 1005 | |
| 1006 | |
| 1007 | /* upb_inttable_iter **********************************************************/ |
| 1008 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1009 | /* upb_inttable_iter i; |
| 1010 | * upb_inttable_begin(&i, t); |
| 1011 | * for(; !upb_inttable_done(&i); upb_inttable_next(&i)) { |
| 1012 | * uintptr_t key = upb_inttable_iter_key(&i); |
| 1013 | * upb_value val = upb_inttable_iter_value(&i); |
| 1014 | * // ... |
| 1015 | * } |
| 1016 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1017 | |
| 1018 | typedef struct { |
| 1019 | const upb_inttable *t; |
| 1020 | size_t index; |
| 1021 | bool array_part; |
| 1022 | } upb_inttable_iter; |
| 1023 | |
| 1024 | void upb_inttable_begin(upb_inttable_iter *i, const upb_inttable *t); |
| 1025 | void upb_inttable_next(upb_inttable_iter *i); |
| 1026 | bool upb_inttable_done(const upb_inttable_iter *i); |
| 1027 | uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i); |
| 1028 | upb_value upb_inttable_iter_value(const upb_inttable_iter *i); |
| 1029 | void upb_inttable_iter_setdone(upb_inttable_iter *i); |
| 1030 | bool upb_inttable_iter_isequal(const upb_inttable_iter *i1, |
| 1031 | const upb_inttable_iter *i2); |
| 1032 | |
| 1033 | |
| 1034 | #ifdef __cplusplus |
| 1035 | } /* extern "C" */ |
| 1036 | #endif |
| 1037 | |
| 1038 | #endif /* UPB_TABLE_H_ */ |
| 1039 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1040 | /* Reference tracking will check ref()/unref() operations to make sure the |
| 1041 | * ref ownership is correct. Where possible it will also make tools like |
| 1042 | * Valgrind attribute ref leaks to the code that took the leaked ref, not |
| 1043 | * the code that originally created the object. |
| 1044 | * |
| 1045 | * Enabling this requires the application to define upb_lock()/upb_unlock() |
| 1046 | * functions that acquire/release a global mutex (or #define UPB_THREAD_UNSAFE). |
| 1047 | * For this reason we don't enable it by default, even in debug builds. |
| 1048 | */ |
| 1049 | |
| 1050 | /* #define UPB_DEBUG_REFS */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1051 | |
| 1052 | #ifdef __cplusplus |
| 1053 | namespace upb { class RefCounted; } |
| 1054 | #endif |
| 1055 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1056 | UPB_DECLARE_TYPE(upb::RefCounted, upb_refcounted) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1057 | |
| 1058 | struct upb_refcounted_vtbl; |
| 1059 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1060 | #ifdef __cplusplus |
| 1061 | |
| 1062 | class upb::RefCounted { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1063 | public: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1064 | /* Returns true if the given object is frozen. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1065 | bool IsFrozen() const; |
| 1066 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1067 | /* Increases the ref count, the new ref is owned by "owner" which must not |
| 1068 | * already own a ref (and should not itself be a refcounted object if the ref |
| 1069 | * could possibly be circular; see below). |
| 1070 | * Thread-safe iff "this" is frozen. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1071 | void Ref(const void *owner) const; |
| 1072 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1073 | /* Release a ref that was acquired from upb_refcounted_ref() and collects any |
| 1074 | * objects it can. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1075 | void Unref(const void *owner) const; |
| 1076 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1077 | /* Moves an existing ref from "from" to "to", without changing the overall |
| 1078 | * ref count. DonateRef(foo, NULL, owner) is the same as Ref(foo, owner), |
| 1079 | * but "to" may not be NULL. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1080 | void DonateRef(const void *from, const void *to) const; |
| 1081 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1082 | /* Verifies that a ref to the given object is currently held by the given |
| 1083 | * owner. Only effective in UPB_DEBUG_REFS builds. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1084 | void CheckRef(const void *owner) const; |
| 1085 | |
| 1086 | private: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1087 | UPB_DISALLOW_POD_OPS(RefCounted, upb::RefCounted) |
| 1088 | #else |
| 1089 | struct upb_refcounted { |
| 1090 | #endif |
| 1091 | /* TODO(haberman): move the actual structure definition to structdefs.int.h. |
| 1092 | * The only reason they are here is because inline functions need to see the |
| 1093 | * definition of upb_handlers, which needs to see this definition. But we |
| 1094 | * can change the upb_handlers inline functions to deal in raw offsets |
| 1095 | * instead. |
| 1096 | */ |
| 1097 | |
| 1098 | /* A single reference count shared by all objects in the group. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1099 | uint32_t *group; |
| 1100 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1101 | /* A singly-linked list of all objects in the group. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1102 | upb_refcounted *next; |
| 1103 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1104 | /* Table of function pointers for this type. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1105 | const struct upb_refcounted_vtbl *vtbl; |
| 1106 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1107 | /* Maintained only when mutable, this tracks the number of refs (but not |
| 1108 | * ref2's) to this object. *group should be the sum of all individual_count |
| 1109 | * in the group. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1110 | uint32_t individual_count; |
| 1111 | |
| 1112 | bool is_frozen; |
| 1113 | |
| 1114 | #ifdef UPB_DEBUG_REFS |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1115 | upb_inttable *refs; /* Maps owner -> trackedref for incoming refs. */ |
| 1116 | upb_inttable *ref2s; /* Set of targets for outgoing ref2s. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1117 | #endif |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1118 | }; |
| 1119 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1120 | #ifdef UPB_DEBUG_REFS |
| 1121 | #define UPB_REFCOUNT_INIT(refs, ref2s) \ |
| 1122 | {&static_refcount, NULL, NULL, 0, true, refs, ref2s} |
| 1123 | #else |
| 1124 | #define UPB_REFCOUNT_INIT(refs, ref2s) {&static_refcount, NULL, NULL, 0, true} |
| 1125 | #endif |
| 1126 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1127 | UPB_BEGIN_EXTERN_C |
| 1128 | |
| 1129 | /* It is better to use tracked refs when possible, for the extra debugging |
| 1130 | * capability. But if this is not possible (because you don't have easy access |
| 1131 | * to a stable pointer value that is associated with the ref), you can pass |
| 1132 | * UPB_UNTRACKED_REF instead. */ |
| 1133 | extern const void *UPB_UNTRACKED_REF; |
| 1134 | |
| 1135 | /* Native C API. */ |
| 1136 | bool upb_refcounted_isfrozen(const upb_refcounted *r); |
| 1137 | void upb_refcounted_ref(const upb_refcounted *r, const void *owner); |
| 1138 | void upb_refcounted_unref(const upb_refcounted *r, const void *owner); |
| 1139 | void upb_refcounted_donateref( |
| 1140 | const upb_refcounted *r, const void *from, const void *to); |
| 1141 | void upb_refcounted_checkref(const upb_refcounted *r, const void *owner); |
| 1142 | |
| 1143 | #define UPB_REFCOUNTED_CMETHODS(type, upcastfunc) \ |
| 1144 | UPB_INLINE bool type ## _isfrozen(const type *v) { \ |
| 1145 | return upb_refcounted_isfrozen(upcastfunc(v)); \ |
| 1146 | } \ |
| 1147 | UPB_INLINE void type ## _ref(const type *v, const void *owner) { \ |
| 1148 | upb_refcounted_ref(upcastfunc(v), owner); \ |
| 1149 | } \ |
| 1150 | UPB_INLINE void type ## _unref(const type *v, const void *owner) { \ |
| 1151 | upb_refcounted_unref(upcastfunc(v), owner); \ |
| 1152 | } \ |
| 1153 | UPB_INLINE void type ## _donateref(const type *v, const void *from, const void *to) { \ |
| 1154 | upb_refcounted_donateref(upcastfunc(v), from, to); \ |
| 1155 | } \ |
| 1156 | UPB_INLINE void type ## _checkref(const type *v, const void *owner) { \ |
| 1157 | upb_refcounted_checkref(upcastfunc(v), owner); \ |
| 1158 | } |
| 1159 | |
| 1160 | #define UPB_REFCOUNTED_CPPMETHODS \ |
| 1161 | bool IsFrozen() const { \ |
| 1162 | return upb::upcast_to<const upb::RefCounted>(this)->IsFrozen(); \ |
| 1163 | } \ |
| 1164 | void Ref(const void *owner) const { \ |
| 1165 | return upb::upcast_to<const upb::RefCounted>(this)->Ref(owner); \ |
| 1166 | } \ |
| 1167 | void Unref(const void *owner) const { \ |
| 1168 | return upb::upcast_to<const upb::RefCounted>(this)->Unref(owner); \ |
| 1169 | } \ |
| 1170 | void DonateRef(const void *from, const void *to) const { \ |
| 1171 | return upb::upcast_to<const upb::RefCounted>(this)->DonateRef(from, to); \ |
| 1172 | } \ |
| 1173 | void CheckRef(const void *owner) const { \ |
| 1174 | return upb::upcast_to<const upb::RefCounted>(this)->CheckRef(owner); \ |
| 1175 | } |
| 1176 | |
| 1177 | /* Internal-to-upb Interface **************************************************/ |
| 1178 | |
| 1179 | typedef void upb_refcounted_visit(const upb_refcounted *r, |
| 1180 | const upb_refcounted *subobj, |
| 1181 | void *closure); |
| 1182 | |
| 1183 | struct upb_refcounted_vtbl { |
| 1184 | /* Must visit all subobjects that are currently ref'd via upb_refcounted_ref2. |
| 1185 | * Must be longjmp()-safe. */ |
| 1186 | void (*visit)(const upb_refcounted *r, upb_refcounted_visit *visit, void *c); |
| 1187 | |
| 1188 | /* Must free the object and release all references to other objects. */ |
| 1189 | void (*free)(upb_refcounted *r); |
| 1190 | }; |
| 1191 | |
| 1192 | /* Initializes the refcounted with a single ref for the given owner. Returns |
| 1193 | * false if memory could not be allocated. */ |
| 1194 | bool upb_refcounted_init(upb_refcounted *r, |
| 1195 | const struct upb_refcounted_vtbl *vtbl, |
| 1196 | const void *owner); |
| 1197 | |
| 1198 | /* Adds a ref from one refcounted object to another ("from" must not already |
| 1199 | * own a ref). These refs may be circular; cycles will be collected correctly |
| 1200 | * (if conservatively). These refs do not need to be freed in from's free() |
| 1201 | * function. */ |
| 1202 | void upb_refcounted_ref2(const upb_refcounted *r, upb_refcounted *from); |
| 1203 | |
| 1204 | /* Removes a ref that was acquired from upb_refcounted_ref2(), and collects any |
| 1205 | * object it can. This is only necessary when "from" no longer points to "r", |
| 1206 | * and not from from's "free" function. */ |
| 1207 | void upb_refcounted_unref2(const upb_refcounted *r, upb_refcounted *from); |
| 1208 | |
| 1209 | #define upb_ref2(r, from) \ |
| 1210 | upb_refcounted_ref2((const upb_refcounted*)r, (upb_refcounted*)from) |
| 1211 | #define upb_unref2(r, from) \ |
| 1212 | upb_refcounted_unref2((const upb_refcounted*)r, (upb_refcounted*)from) |
| 1213 | |
| 1214 | /* Freezes all mutable object reachable by ref2() refs from the given roots. |
| 1215 | * This will split refcounting groups into precise SCC groups, so that |
| 1216 | * refcounting of frozen objects can be more aggressive. If memory allocation |
| 1217 | * fails, or if more than 2**31 mutable objects are reachable from "roots", or |
| 1218 | * if the maximum depth of the graph exceeds "maxdepth", false is returned and |
| 1219 | * the objects are unchanged. |
| 1220 | * |
| 1221 | * After this operation succeeds, the objects are frozen/const, and may not be |
| 1222 | * used through non-const pointers. In particular, they may not be passed as |
| 1223 | * the second parameter of upb_refcounted_{ref,unref}2(). On the upside, all |
| 1224 | * operations on frozen refcounteds are threadsafe, and objects will be freed |
| 1225 | * at the precise moment that they become unreachable. |
| 1226 | * |
| 1227 | * Caller must own refs on each object in the "roots" list. */ |
| 1228 | bool upb_refcounted_freeze(upb_refcounted *const*roots, int n, upb_status *s, |
| 1229 | int maxdepth); |
| 1230 | |
| 1231 | /* Shared by all compiled-in refcounted objects. */ |
| 1232 | extern uint32_t static_refcount; |
| 1233 | |
| 1234 | UPB_END_EXTERN_C |
| 1235 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1236 | #ifdef __cplusplus |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1237 | /* C++ Wrappers. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1238 | namespace upb { |
| 1239 | inline bool RefCounted::IsFrozen() const { |
| 1240 | return upb_refcounted_isfrozen(this); |
| 1241 | } |
| 1242 | inline void RefCounted::Ref(const void *owner) const { |
| 1243 | upb_refcounted_ref(this, owner); |
| 1244 | } |
| 1245 | inline void RefCounted::Unref(const void *owner) const { |
| 1246 | upb_refcounted_unref(this, owner); |
| 1247 | } |
| 1248 | inline void RefCounted::DonateRef(const void *from, const void *to) const { |
| 1249 | upb_refcounted_donateref(this, from, to); |
| 1250 | } |
| 1251 | inline void RefCounted::CheckRef(const void *owner) const { |
| 1252 | upb_refcounted_checkref(this, owner); |
| 1253 | } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1254 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1255 | #endif |
| 1256 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1257 | #endif /* UPB_REFCOUNT_H_ */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1258 | |
| 1259 | #ifdef __cplusplus |
| 1260 | #include <cstring> |
| 1261 | #include <string> |
| 1262 | #include <vector> |
| 1263 | |
| 1264 | namespace upb { |
| 1265 | class Def; |
| 1266 | class EnumDef; |
| 1267 | class FieldDef; |
| 1268 | class MessageDef; |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 1269 | class OneofDef; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1270 | } |
| 1271 | #endif |
| 1272 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1273 | UPB_DECLARE_DERIVED_TYPE(upb::Def, upb::RefCounted, upb_def, upb_refcounted) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1274 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1275 | /* The maximum message depth that the type graph can have. This is a resource |
| 1276 | * limit for the C stack since we sometimes need to recursively traverse the |
| 1277 | * graph. Cycles are ok; the traversal will stop when it detects a cycle, but |
| 1278 | * we must hit the cycle before the maximum depth is reached. |
| 1279 | * |
| 1280 | * If having a single static limit is too inflexible, we can add another variant |
| 1281 | * of Def::Freeze that allows specifying this as a parameter. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1282 | #define UPB_MAX_MESSAGE_DEPTH 64 |
| 1283 | |
| 1284 | |
| 1285 | /* upb::Def: base class for defs *********************************************/ |
| 1286 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1287 | /* All the different kind of defs we support. These correspond 1:1 with |
| 1288 | * declarations in a .proto file. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1289 | typedef enum { |
| 1290 | UPB_DEF_MSG, |
| 1291 | UPB_DEF_FIELD, |
| 1292 | UPB_DEF_ENUM, |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 1293 | UPB_DEF_ONEOF, |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1294 | UPB_DEF_SERVICE, /* Not yet implemented. */ |
| 1295 | UPB_DEF_ANY = -1 /* Wildcard for upb_symtab_get*() */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1296 | } upb_deftype_t; |
| 1297 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1298 | #ifdef __cplusplus |
| 1299 | |
| 1300 | /* The base class of all defs. Its base is upb::RefCounted (use upb::upcast() |
| 1301 | * to convert). */ |
| 1302 | class upb::Def { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1303 | public: |
| 1304 | typedef upb_deftype_t Type; |
| 1305 | |
| 1306 | Def* Dup(const void *owner) const; |
| 1307 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1308 | /* upb::RefCounted methods like Ref()/Unref(). */ |
| 1309 | UPB_REFCOUNTED_CPPMETHODS |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1310 | |
| 1311 | Type def_type() const; |
| 1312 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1313 | /* "fullname" is the def's fully-qualified name (eg. foo.bar.Message). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1314 | const char *full_name() const; |
| 1315 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1316 | /* The def must be mutable. Caller retains ownership of fullname. Defs are |
| 1317 | * not required to have a name; if a def has no name when it is frozen, it |
| 1318 | * will remain an anonymous def. On failure, returns false and details in "s" |
| 1319 | * if non-NULL. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1320 | bool set_full_name(const char* fullname, upb::Status* s); |
| 1321 | bool set_full_name(const std::string &fullname, upb::Status* s); |
| 1322 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1323 | /* Freezes the given defs; this validates all constraints and marks the defs |
| 1324 | * as frozen (read-only). "defs" may not contain any fielddefs, but fields |
| 1325 | * of any msgdefs will be frozen. |
| 1326 | * |
| 1327 | * Symbolic references to sub-types and enum defaults must have already been |
| 1328 | * resolved. Any mutable defs reachable from any of "defs" must also be in |
| 1329 | * the list; more formally, "defs" must be a transitive closure of mutable |
| 1330 | * defs. |
| 1331 | * |
| 1332 | * After this operation succeeds, the finalized defs must only be accessed |
| 1333 | * through a const pointer! */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1334 | static bool Freeze(Def* const* defs, int n, Status* status); |
| 1335 | static bool Freeze(const std::vector<Def*>& defs, Status* status); |
| 1336 | |
| 1337 | private: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1338 | UPB_DISALLOW_POD_OPS(Def, upb::Def) |
| 1339 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1340 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1341 | #endif /* __cplusplus */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1342 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1343 | UPB_BEGIN_EXTERN_C |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1344 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1345 | /* Native C API. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1346 | upb_def *upb_def_dup(const upb_def *def, const void *owner); |
| 1347 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1348 | /* Include upb_refcounted methods like upb_def_ref()/upb_def_unref(). */ |
| 1349 | UPB_REFCOUNTED_CMETHODS(upb_def, upb_def_upcast) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1350 | |
| 1351 | upb_deftype_t upb_def_type(const upb_def *d); |
| 1352 | const char *upb_def_fullname(const upb_def *d); |
| 1353 | bool upb_def_setfullname(upb_def *def, const char *fullname, upb_status *s); |
| 1354 | bool upb_def_freeze(upb_def *const *defs, int n, upb_status *s); |
| 1355 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1356 | UPB_END_EXTERN_C |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1357 | |
| 1358 | |
| 1359 | /* upb::Def casts *************************************************************/ |
| 1360 | |
| 1361 | #ifdef __cplusplus |
| 1362 | #define UPB_CPP_CASTS(cname, cpptype) \ |
| 1363 | namespace upb { \ |
| 1364 | template <> \ |
| 1365 | inline cpptype *down_cast<cpptype *, Def>(Def * def) { \ |
| 1366 | return upb_downcast_##cname##_mutable(def); \ |
| 1367 | } \ |
| 1368 | template <> \ |
| 1369 | inline cpptype *dyn_cast<cpptype *, Def>(Def * def) { \ |
| 1370 | return upb_dyncast_##cname##_mutable(def); \ |
| 1371 | } \ |
| 1372 | template <> \ |
| 1373 | inline const cpptype *down_cast<const cpptype *, const Def>( \ |
| 1374 | const Def *def) { \ |
| 1375 | return upb_downcast_##cname(def); \ |
| 1376 | } \ |
| 1377 | template <> \ |
| 1378 | inline const cpptype *dyn_cast<const cpptype *, const Def>(const Def *def) { \ |
| 1379 | return upb_dyncast_##cname(def); \ |
| 1380 | } \ |
| 1381 | template <> \ |
| 1382 | inline const cpptype *down_cast<const cpptype *, Def>(Def * def) { \ |
| 1383 | return upb_downcast_##cname(def); \ |
| 1384 | } \ |
| 1385 | template <> \ |
| 1386 | inline const cpptype *dyn_cast<const cpptype *, Def>(Def * def) { \ |
| 1387 | return upb_dyncast_##cname(def); \ |
| 1388 | } \ |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1389 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1390 | #else |
| 1391 | #define UPB_CPP_CASTS(cname, cpptype) |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1392 | #endif /* __cplusplus */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1393 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1394 | /* Dynamic casts, for determining if a def is of a particular type at runtime. |
| 1395 | * Downcasts, for when some wants to assert that a def is of a particular type. |
| 1396 | * These are only checked if we are building debug. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1397 | #define UPB_DEF_CASTS(lower, upper, cpptype) \ |
| 1398 | UPB_INLINE const upb_##lower *upb_dyncast_##lower(const upb_def *def) { \ |
| 1399 | if (upb_def_type(def) != UPB_DEF_##upper) return NULL; \ |
| 1400 | return (upb_##lower *)def; \ |
| 1401 | } \ |
| 1402 | UPB_INLINE const upb_##lower *upb_downcast_##lower(const upb_def *def) { \ |
| 1403 | assert(upb_def_type(def) == UPB_DEF_##upper); \ |
| 1404 | return (const upb_##lower *)def; \ |
| 1405 | } \ |
| 1406 | UPB_INLINE upb_##lower *upb_dyncast_##lower##_mutable(upb_def *def) { \ |
| 1407 | return (upb_##lower *)upb_dyncast_##lower(def); \ |
| 1408 | } \ |
| 1409 | UPB_INLINE upb_##lower *upb_downcast_##lower##_mutable(upb_def *def) { \ |
| 1410 | return (upb_##lower *)upb_downcast_##lower(def); \ |
| 1411 | } \ |
| 1412 | UPB_CPP_CASTS(lower, cpptype) |
| 1413 | |
| 1414 | #define UPB_DEFINE_DEF(cppname, lower, upper, cppmethods, members) \ |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1415 | UPB_DEFINE_CLASS2(cppname, upb::Def, upb::RefCounted, cppmethods, \ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1416 | members) \ |
| 1417 | UPB_DEF_CASTS(lower, upper, cppname) |
| 1418 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1419 | #define UPB_DECLARE_DEF_TYPE(cppname, lower, upper) \ |
| 1420 | UPB_DECLARE_DERIVED_TYPE2(cppname, upb::Def, upb::RefCounted, \ |
| 1421 | upb_ ## lower, upb_def, upb_refcounted) \ |
| 1422 | UPB_DEF_CASTS(lower, upper, cppname) |
| 1423 | |
| 1424 | UPB_DECLARE_DEF_TYPE(upb::FieldDef, fielddef, FIELD) |
| 1425 | UPB_DECLARE_DEF_TYPE(upb::MessageDef, msgdef, MSG) |
| 1426 | UPB_DECLARE_DEF_TYPE(upb::EnumDef, enumdef, ENUM) |
| 1427 | UPB_DECLARE_DEF_TYPE(upb::OneofDef, oneofdef, ONEOF) |
| 1428 | |
| 1429 | #undef UPB_DECLARE_DEF_TYPE |
| 1430 | #undef UPB_DEF_CASTS |
| 1431 | #undef UPB_CPP_CASTS |
| 1432 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1433 | |
| 1434 | /* upb::FieldDef **************************************************************/ |
| 1435 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1436 | /* The types a field can have. Note that this list is not identical to the |
| 1437 | * types defined in descriptor.proto, which gives INT32 and SINT32 separate |
| 1438 | * types (we distinguish the two with the "integer encoding" enum below). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1439 | typedef enum { |
| 1440 | UPB_TYPE_FLOAT = 1, |
| 1441 | UPB_TYPE_DOUBLE = 2, |
| 1442 | UPB_TYPE_BOOL = 3, |
| 1443 | UPB_TYPE_STRING = 4, |
| 1444 | UPB_TYPE_BYTES = 5, |
| 1445 | UPB_TYPE_MESSAGE = 6, |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1446 | UPB_TYPE_ENUM = 7, /* Enum values are int32. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1447 | UPB_TYPE_INT32 = 8, |
| 1448 | UPB_TYPE_UINT32 = 9, |
| 1449 | UPB_TYPE_INT64 = 10, |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1450 | UPB_TYPE_UINT64 = 11 |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1451 | } upb_fieldtype_t; |
| 1452 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1453 | /* The repeated-ness of each field; this matches descriptor.proto. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1454 | typedef enum { |
| 1455 | UPB_LABEL_OPTIONAL = 1, |
| 1456 | UPB_LABEL_REQUIRED = 2, |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1457 | UPB_LABEL_REPEATED = 3 |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1458 | } upb_label_t; |
| 1459 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1460 | /* How integers should be encoded in serializations that offer multiple |
| 1461 | * integer encoding methods. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1462 | typedef enum { |
| 1463 | UPB_INTFMT_VARIABLE = 1, |
| 1464 | UPB_INTFMT_FIXED = 2, |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1465 | UPB_INTFMT_ZIGZAG = 3 /* Only for signed types (INT32/INT64). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1466 | } upb_intfmt_t; |
| 1467 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1468 | /* Descriptor types, as defined in descriptor.proto. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1469 | typedef enum { |
| 1470 | UPB_DESCRIPTOR_TYPE_DOUBLE = 1, |
| 1471 | UPB_DESCRIPTOR_TYPE_FLOAT = 2, |
| 1472 | UPB_DESCRIPTOR_TYPE_INT64 = 3, |
| 1473 | UPB_DESCRIPTOR_TYPE_UINT64 = 4, |
| 1474 | UPB_DESCRIPTOR_TYPE_INT32 = 5, |
| 1475 | UPB_DESCRIPTOR_TYPE_FIXED64 = 6, |
| 1476 | UPB_DESCRIPTOR_TYPE_FIXED32 = 7, |
| 1477 | UPB_DESCRIPTOR_TYPE_BOOL = 8, |
| 1478 | UPB_DESCRIPTOR_TYPE_STRING = 9, |
| 1479 | UPB_DESCRIPTOR_TYPE_GROUP = 10, |
| 1480 | UPB_DESCRIPTOR_TYPE_MESSAGE = 11, |
| 1481 | UPB_DESCRIPTOR_TYPE_BYTES = 12, |
| 1482 | UPB_DESCRIPTOR_TYPE_UINT32 = 13, |
| 1483 | UPB_DESCRIPTOR_TYPE_ENUM = 14, |
| 1484 | UPB_DESCRIPTOR_TYPE_SFIXED32 = 15, |
| 1485 | UPB_DESCRIPTOR_TYPE_SFIXED64 = 16, |
| 1486 | UPB_DESCRIPTOR_TYPE_SINT32 = 17, |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1487 | UPB_DESCRIPTOR_TYPE_SINT64 = 18 |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1488 | } upb_descriptortype_t; |
| 1489 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1490 | /* Maximum field number allowed for FieldDefs. This is an inherent limit of the |
| 1491 | * protobuf wire format. */ |
| 1492 | #define UPB_MAX_FIELDNUMBER ((1 << 29) - 1) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1493 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1494 | #ifdef __cplusplus |
| 1495 | |
| 1496 | /* A upb_fielddef describes a single field in a message. It is most often |
| 1497 | * found as a part of a upb_msgdef, but can also stand alone to represent |
| 1498 | * an extension. |
| 1499 | * |
| 1500 | * Its base class is upb::Def (use upb::upcast() to convert). */ |
| 1501 | class upb::FieldDef { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1502 | public: |
| 1503 | typedef upb_fieldtype_t Type; |
| 1504 | typedef upb_label_t Label; |
| 1505 | typedef upb_intfmt_t IntegerFormat; |
| 1506 | typedef upb_descriptortype_t DescriptorType; |
| 1507 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1508 | /* These return true if the given value is a valid member of the enumeration. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1509 | static bool CheckType(int32_t val); |
| 1510 | static bool CheckLabel(int32_t val); |
| 1511 | static bool CheckDescriptorType(int32_t val); |
| 1512 | static bool CheckIntegerFormat(int32_t val); |
| 1513 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1514 | /* These convert to the given enumeration; they require that the value is |
| 1515 | * valid. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1516 | static Type ConvertType(int32_t val); |
| 1517 | static Label ConvertLabel(int32_t val); |
| 1518 | static DescriptorType ConvertDescriptorType(int32_t val); |
| 1519 | static IntegerFormat ConvertIntegerFormat(int32_t val); |
| 1520 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1521 | /* Returns NULL if memory allocation failed. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1522 | static reffed_ptr<FieldDef> New(); |
| 1523 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1524 | /* Duplicates the given field, returning NULL if memory allocation failed. |
| 1525 | * When a fielddef is duplicated, the subdef (if any) is made symbolic if it |
| 1526 | * wasn't already. If the subdef is set but has no name (which is possible |
| 1527 | * since msgdefs are not required to have a name) the new fielddef's subdef |
| 1528 | * will be unset. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1529 | FieldDef* Dup(const void* owner) const; |
| 1530 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1531 | /* upb::RefCounted methods like Ref()/Unref(). */ |
| 1532 | UPB_REFCOUNTED_CPPMETHODS |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1533 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1534 | /* Functionality from upb::Def. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1535 | const char* full_name() const; |
| 1536 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1537 | bool type_is_set() const; /* set_[descriptor_]type() has been called? */ |
| 1538 | Type type() const; /* Requires that type_is_set() == true. */ |
| 1539 | Label label() const; /* Defaults to UPB_LABEL_OPTIONAL. */ |
| 1540 | const char* name() const; /* NULL if uninitialized. */ |
| 1541 | uint32_t number() const; /* Returns 0 if uninitialized. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1542 | bool is_extension() const; |
| 1543 | |
Josh Haberman | f654d49 | 2016-02-18 11:07:51 -0800 | [diff] [blame] | 1544 | /* Copies the JSON name for this field into the given buffer. Returns the |
| 1545 | * actual size of the JSON name, including the NULL terminator. If the |
| 1546 | * return value is 0, the JSON name is unset. If the return value is |
| 1547 | * greater than len, the JSON name was truncated. The buffer is always |
| 1548 | * NULL-terminated if len > 0. |
| 1549 | * |
| 1550 | * The JSON name always defaults to a camelCased version of the regular |
| 1551 | * name. However if the regular name is unset, the JSON name will be unset |
| 1552 | * also. |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 1553 | */ |
Josh Haberman | f654d49 | 2016-02-18 11:07:51 -0800 | [diff] [blame] | 1554 | size_t GetJsonName(char* buf, size_t len) const; |
| 1555 | |
| 1556 | /* Convenience version of the above function which copies the JSON name |
| 1557 | * into the given string, returning false if the name is not set. */ |
| 1558 | template <class T> |
| 1559 | bool GetJsonName(T* str) { |
| 1560 | str->resize(GetJsonName(NULL, 0)); |
| 1561 | GetJsonName(&(*str)[0], str->size()); |
| 1562 | return str->size() > 0; |
| 1563 | } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 1564 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1565 | /* For UPB_TYPE_MESSAGE fields only where is_tag_delimited() == false, |
| 1566 | * indicates whether this field should have lazy parsing handlers that yield |
| 1567 | * the unparsed string for the submessage. |
| 1568 | * |
| 1569 | * TODO(haberman): I think we want to move this into a FieldOptions container |
| 1570 | * when we add support for custom options (the FieldOptions struct will |
| 1571 | * contain both regular FieldOptions like "lazy" *and* custom options). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1572 | bool lazy() const; |
| 1573 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1574 | /* For non-string, non-submessage fields, this indicates whether binary |
| 1575 | * protobufs are encoded in packed or non-packed format. |
| 1576 | * |
| 1577 | * TODO(haberman): see note above about putting options like this into a |
| 1578 | * FieldOptions container. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1579 | bool packed() const; |
| 1580 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1581 | /* An integer that can be used as an index into an array of fields for |
| 1582 | * whatever message this field belongs to. Guaranteed to be less than |
| 1583 | * f->containing_type()->field_count(). May only be accessed once the def has |
| 1584 | * been finalized. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1585 | int index() const; |
| 1586 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1587 | /* The MessageDef to which this field belongs. |
| 1588 | * |
| 1589 | * If this field has been added to a MessageDef, that message can be retrieved |
| 1590 | * directly (this is always the case for frozen FieldDefs). |
| 1591 | * |
| 1592 | * If the field has not yet been added to a MessageDef, you can set the name |
| 1593 | * of the containing type symbolically instead. This is mostly useful for |
| 1594 | * extensions, where the extension is declared separately from the message. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1595 | const MessageDef* containing_type() const; |
| 1596 | const char* containing_type_name(); |
| 1597 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1598 | /* The OneofDef to which this field belongs, or NULL if this field is not part |
| 1599 | * of a oneof. */ |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 1600 | const OneofDef* containing_oneof() const; |
| 1601 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1602 | /* The field's type according to the enum in descriptor.proto. This is not |
| 1603 | * the same as UPB_TYPE_*, because it distinguishes between (for example) |
| 1604 | * INT32 and SINT32, whereas our "type" enum does not. This return of |
| 1605 | * descriptor_type() is a function of type(), integer_format(), and |
| 1606 | * is_tag_delimited(). Likewise set_descriptor_type() sets all three |
| 1607 | * appropriately. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1608 | DescriptorType descriptor_type() const; |
| 1609 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1610 | /* Convenient field type tests. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1611 | bool IsSubMessage() const; |
| 1612 | bool IsString() const; |
| 1613 | bool IsSequence() const; |
| 1614 | bool IsPrimitive() const; |
Chris Fallin | a507592 | 2015-02-02 15:07:34 -0800 | [diff] [blame] | 1615 | bool IsMap() const; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1616 | |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 1617 | /* Whether this field must be able to explicitly represent presence: |
| 1618 | * |
| 1619 | * * This is always false for repeated fields (an empty repeated field is |
| 1620 | * equivalent to a repeated field with zero entries). |
| 1621 | * |
| 1622 | * * This is always true for submessages. |
| 1623 | * |
| 1624 | * * For other fields, it depends on the message (see |
| 1625 | * MessageDef::SetPrimitivesHavePresence()) |
| 1626 | */ |
| 1627 | bool HasPresence() const; |
| 1628 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1629 | /* How integers are encoded. Only meaningful for integer types. |
| 1630 | * Defaults to UPB_INTFMT_VARIABLE, and is reset when "type" changes. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1631 | IntegerFormat integer_format() const; |
| 1632 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1633 | /* Whether a submessage field is tag-delimited or not (if false, then |
| 1634 | * length-delimited). May only be set when type() == UPB_TYPE_MESSAGE. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1635 | bool is_tag_delimited() const; |
| 1636 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1637 | /* Returns the non-string default value for this fielddef, which may either |
| 1638 | * be something the client set explicitly or the "default default" (0 for |
| 1639 | * numbers, empty for strings). The field's type indicates the type of the |
| 1640 | * returned value, except for enum fields that are still mutable. |
| 1641 | * |
| 1642 | * Requires that the given function matches the field's current type. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1643 | int64_t default_int64() const; |
| 1644 | int32_t default_int32() const; |
| 1645 | uint64_t default_uint64() const; |
| 1646 | uint32_t default_uint32() const; |
| 1647 | bool default_bool() const; |
| 1648 | float default_float() const; |
| 1649 | double default_double() const; |
| 1650 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1651 | /* The resulting string is always NULL-terminated. If non-NULL, the length |
| 1652 | * will be stored in *len. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1653 | const char *default_string(size_t* len) const; |
| 1654 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1655 | /* For frozen UPB_TYPE_ENUM fields, enum defaults can always be read as either |
| 1656 | * string or int32, and both of these methods will always return true. |
| 1657 | * |
| 1658 | * For mutable UPB_TYPE_ENUM fields, the story is a bit more complicated. |
| 1659 | * Enum defaults are unusual. They can be specified either as string or int32, |
| 1660 | * but to be valid the enum must have that value as a member. And if no |
| 1661 | * default is specified, the "default default" comes from the EnumDef. |
| 1662 | * |
| 1663 | * We allow reading the default as either an int32 or a string, but only if |
| 1664 | * we have a meaningful value to report. We have a meaningful value if it was |
| 1665 | * set explicitly, or if we could get the "default default" from the EnumDef. |
| 1666 | * Also if you explicitly set the name and we find the number in the EnumDef */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1667 | bool EnumHasStringDefault() const; |
| 1668 | bool EnumHasInt32Default() const; |
| 1669 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1670 | /* Submessage and enum fields must reference a "subdef", which is the |
| 1671 | * upb::MessageDef or upb::EnumDef that defines their type. Note that when |
| 1672 | * the FieldDef is mutable it may not have a subdef *yet*, but this function |
| 1673 | * still returns true to indicate that the field's type requires a subdef. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1674 | bool HasSubDef() const; |
| 1675 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1676 | /* Returns the enum or submessage def for this field, if any. The field's |
| 1677 | * type must match (ie. you may only call enum_subdef() for fields where |
| 1678 | * type() == UPB_TYPE_ENUM). Returns NULL if the subdef has not been set or |
| 1679 | * is currently set symbolically. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1680 | const EnumDef* enum_subdef() const; |
| 1681 | const MessageDef* message_subdef() const; |
| 1682 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1683 | /* Returns the generic subdef for this field. Requires that HasSubDef() (ie. |
| 1684 | * only works for UPB_TYPE_ENUM and UPB_TYPE_MESSAGE fields). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1685 | const Def* subdef() const; |
| 1686 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1687 | /* Returns the symbolic name of the subdef. If the subdef is currently set |
| 1688 | * unresolved (ie. set symbolically) returns the symbolic name. If it has |
| 1689 | * been resolved to a specific subdef, returns the name from that subdef. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1690 | const char* subdef_name() const; |
| 1691 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1692 | /* Setters (non-const methods), only valid for mutable FieldDefs! ***********/ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1693 | |
| 1694 | bool set_full_name(const char* fullname, upb::Status* s); |
| 1695 | bool set_full_name(const std::string& fullname, upb::Status* s); |
| 1696 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1697 | /* This may only be called if containing_type() == NULL (ie. the field has not |
| 1698 | * been added to a message yet). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1699 | bool set_containing_type_name(const char *name, Status* status); |
| 1700 | bool set_containing_type_name(const std::string& name, Status* status); |
| 1701 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1702 | /* Defaults to false. When we freeze, we ensure that this can only be true |
| 1703 | * for length-delimited message fields. Prior to freezing this can be true or |
| 1704 | * false with no restrictions. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1705 | void set_lazy(bool lazy); |
| 1706 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1707 | /* Defaults to true. Sets whether this field is encoded in packed format. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1708 | void set_packed(bool packed); |
| 1709 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1710 | /* "type" or "descriptor_type" MUST be set explicitly before the fielddef is |
| 1711 | * finalized. These setters require that the enum value is valid; if the |
| 1712 | * value did not come directly from an enum constant, the caller should |
| 1713 | * validate it first with the functions above (CheckFieldType(), etc). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1714 | void set_type(Type type); |
| 1715 | void set_label(Label label); |
| 1716 | void set_descriptor_type(DescriptorType type); |
| 1717 | void set_is_extension(bool is_extension); |
| 1718 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1719 | /* "number" and "name" must be set before the FieldDef is added to a |
| 1720 | * MessageDef, and may not be set after that. |
| 1721 | * |
| 1722 | * "name" is the same as full_name()/set_full_name(), but since fielddefs |
| 1723 | * most often use simple, non-qualified names, we provide this accessor |
| 1724 | * also. Generally only extensions will want to think of this name as |
| 1725 | * fully-qualified. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1726 | bool set_number(uint32_t number, upb::Status* s); |
| 1727 | bool set_name(const char* name, upb::Status* s); |
| 1728 | bool set_name(const std::string& name, upb::Status* s); |
| 1729 | |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 1730 | /* Sets the JSON name to the given string. */ |
| 1731 | /* TODO(haberman): implement. Right now only default json_name (camelCase) |
| 1732 | * is supported. */ |
| 1733 | bool set_json_name(const char* json_name, upb::Status* s); |
| 1734 | bool set_json_name(const std::string& name, upb::Status* s); |
| 1735 | |
| 1736 | /* Clears the JSON name. This will make it revert to its default, which is |
| 1737 | * a camelCased version of the regular field name. */ |
| 1738 | void clear_json_name(); |
| 1739 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1740 | void set_integer_format(IntegerFormat format); |
| 1741 | bool set_tag_delimited(bool tag_delimited, upb::Status* s); |
| 1742 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1743 | /* Sets default value for the field. The call must exactly match the type |
| 1744 | * of the field. Enum fields may use either setint32 or setstring to set |
| 1745 | * the default numerically or symbolically, respectively, but symbolic |
| 1746 | * defaults must be resolved before finalizing (see ResolveEnumDefault()). |
| 1747 | * |
| 1748 | * Changing the type of a field will reset its default. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1749 | void set_default_int64(int64_t val); |
| 1750 | void set_default_int32(int32_t val); |
| 1751 | void set_default_uint64(uint64_t val); |
| 1752 | void set_default_uint32(uint32_t val); |
| 1753 | void set_default_bool(bool val); |
| 1754 | void set_default_float(float val); |
| 1755 | void set_default_double(double val); |
| 1756 | bool set_default_string(const void *str, size_t len, Status *s); |
| 1757 | bool set_default_string(const std::string &str, Status *s); |
| 1758 | void set_default_cstr(const char *str, Status *s); |
| 1759 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1760 | /* Before a fielddef is frozen, its subdef may be set either directly (with a |
| 1761 | * upb::Def*) or symbolically. Symbolic refs must be resolved before the |
| 1762 | * containing msgdef can be frozen (see upb_resolve() above). upb always |
| 1763 | * guarantees that any def reachable from a live def will also be kept alive. |
| 1764 | * |
| 1765 | * Both methods require that upb_hassubdef(f) (so the type must be set prior |
| 1766 | * to calling these methods). Returns false if this is not the case, or if |
| 1767 | * the given subdef is not of the correct type. The subdef is reset if the |
| 1768 | * field's type is changed. The subdef can be set to NULL to clear it. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1769 | bool set_subdef(const Def* subdef, Status* s); |
| 1770 | bool set_enum_subdef(const EnumDef* subdef, Status* s); |
| 1771 | bool set_message_subdef(const MessageDef* subdef, Status* s); |
| 1772 | bool set_subdef_name(const char* name, Status* s); |
| 1773 | bool set_subdef_name(const std::string &name, Status* s); |
| 1774 | |
| 1775 | private: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1776 | UPB_DISALLOW_POD_OPS(FieldDef, upb::FieldDef) |
| 1777 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1778 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1779 | # endif /* defined(__cplusplus) */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1780 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1781 | UPB_BEGIN_EXTERN_C |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1782 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1783 | /* Native C API. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1784 | upb_fielddef *upb_fielddef_new(const void *owner); |
| 1785 | upb_fielddef *upb_fielddef_dup(const upb_fielddef *f, const void *owner); |
| 1786 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1787 | /* Include upb_refcounted methods like upb_fielddef_ref(). */ |
| 1788 | UPB_REFCOUNTED_CMETHODS(upb_fielddef, upb_fielddef_upcast2) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1789 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1790 | /* Methods from upb_def. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1791 | const char *upb_fielddef_fullname(const upb_fielddef *f); |
| 1792 | bool upb_fielddef_setfullname(upb_fielddef *f, const char *fullname, |
| 1793 | upb_status *s); |
| 1794 | |
| 1795 | bool upb_fielddef_typeisset(const upb_fielddef *f); |
| 1796 | upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f); |
| 1797 | upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f); |
| 1798 | upb_label_t upb_fielddef_label(const upb_fielddef *f); |
| 1799 | uint32_t upb_fielddef_number(const upb_fielddef *f); |
| 1800 | const char *upb_fielddef_name(const upb_fielddef *f); |
| 1801 | bool upb_fielddef_isextension(const upb_fielddef *f); |
| 1802 | bool upb_fielddef_lazy(const upb_fielddef *f); |
| 1803 | bool upb_fielddef_packed(const upb_fielddef *f); |
Josh Haberman | f654d49 | 2016-02-18 11:07:51 -0800 | [diff] [blame] | 1804 | size_t upb_fielddef_getjsonname(const upb_fielddef *f, char *buf, size_t len); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1805 | const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f); |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 1806 | const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1807 | upb_msgdef *upb_fielddef_containingtype_mutable(upb_fielddef *f); |
| 1808 | const char *upb_fielddef_containingtypename(upb_fielddef *f); |
| 1809 | upb_intfmt_t upb_fielddef_intfmt(const upb_fielddef *f); |
| 1810 | uint32_t upb_fielddef_index(const upb_fielddef *f); |
| 1811 | bool upb_fielddef_istagdelim(const upb_fielddef *f); |
| 1812 | bool upb_fielddef_issubmsg(const upb_fielddef *f); |
| 1813 | bool upb_fielddef_isstring(const upb_fielddef *f); |
| 1814 | bool upb_fielddef_isseq(const upb_fielddef *f); |
| 1815 | bool upb_fielddef_isprimitive(const upb_fielddef *f); |
Chris Fallin | a507592 | 2015-02-02 15:07:34 -0800 | [diff] [blame] | 1816 | bool upb_fielddef_ismap(const upb_fielddef *f); |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 1817 | bool upb_fielddef_haspresence(const upb_fielddef *f); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1818 | int64_t upb_fielddef_defaultint64(const upb_fielddef *f); |
| 1819 | int32_t upb_fielddef_defaultint32(const upb_fielddef *f); |
| 1820 | uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f); |
| 1821 | uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f); |
| 1822 | bool upb_fielddef_defaultbool(const upb_fielddef *f); |
| 1823 | float upb_fielddef_defaultfloat(const upb_fielddef *f); |
| 1824 | double upb_fielddef_defaultdouble(const upb_fielddef *f); |
| 1825 | const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len); |
| 1826 | bool upb_fielddef_enumhasdefaultint32(const upb_fielddef *f); |
| 1827 | bool upb_fielddef_enumhasdefaultstr(const upb_fielddef *f); |
| 1828 | bool upb_fielddef_hassubdef(const upb_fielddef *f); |
| 1829 | const upb_def *upb_fielddef_subdef(const upb_fielddef *f); |
| 1830 | const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f); |
| 1831 | const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f); |
| 1832 | const char *upb_fielddef_subdefname(const upb_fielddef *f); |
| 1833 | |
| 1834 | void upb_fielddef_settype(upb_fielddef *f, upb_fieldtype_t type); |
| 1835 | void upb_fielddef_setdescriptortype(upb_fielddef *f, int type); |
| 1836 | void upb_fielddef_setlabel(upb_fielddef *f, upb_label_t label); |
| 1837 | bool upb_fielddef_setnumber(upb_fielddef *f, uint32_t number, upb_status *s); |
| 1838 | bool upb_fielddef_setname(upb_fielddef *f, const char *name, upb_status *s); |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 1839 | bool upb_fielddef_setjsonname(upb_fielddef *f, const char *name, upb_status *s); |
| 1840 | bool upb_fielddef_clearjsonname(upb_fielddef *f); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1841 | bool upb_fielddef_setcontainingtypename(upb_fielddef *f, const char *name, |
| 1842 | upb_status *s); |
| 1843 | void upb_fielddef_setisextension(upb_fielddef *f, bool is_extension); |
| 1844 | void upb_fielddef_setlazy(upb_fielddef *f, bool lazy); |
| 1845 | void upb_fielddef_setpacked(upb_fielddef *f, bool packed); |
| 1846 | void upb_fielddef_setintfmt(upb_fielddef *f, upb_intfmt_t fmt); |
| 1847 | void upb_fielddef_settagdelim(upb_fielddef *f, bool tag_delim); |
| 1848 | void upb_fielddef_setdefaultint64(upb_fielddef *f, int64_t val); |
| 1849 | void upb_fielddef_setdefaultint32(upb_fielddef *f, int32_t val); |
| 1850 | void upb_fielddef_setdefaultuint64(upb_fielddef *f, uint64_t val); |
| 1851 | void upb_fielddef_setdefaultuint32(upb_fielddef *f, uint32_t val); |
| 1852 | void upb_fielddef_setdefaultbool(upb_fielddef *f, bool val); |
| 1853 | void upb_fielddef_setdefaultfloat(upb_fielddef *f, float val); |
| 1854 | void upb_fielddef_setdefaultdouble(upb_fielddef *f, double val); |
| 1855 | bool upb_fielddef_setdefaultstr(upb_fielddef *f, const void *str, size_t len, |
| 1856 | upb_status *s); |
| 1857 | void upb_fielddef_setdefaultcstr(upb_fielddef *f, const char *str, |
| 1858 | upb_status *s); |
| 1859 | bool upb_fielddef_setsubdef(upb_fielddef *f, const upb_def *subdef, |
| 1860 | upb_status *s); |
| 1861 | bool upb_fielddef_setmsgsubdef(upb_fielddef *f, const upb_msgdef *subdef, |
| 1862 | upb_status *s); |
| 1863 | bool upb_fielddef_setenumsubdef(upb_fielddef *f, const upb_enumdef *subdef, |
| 1864 | upb_status *s); |
| 1865 | bool upb_fielddef_setsubdefname(upb_fielddef *f, const char *name, |
| 1866 | upb_status *s); |
| 1867 | |
| 1868 | bool upb_fielddef_checklabel(int32_t label); |
| 1869 | bool upb_fielddef_checktype(int32_t type); |
| 1870 | bool upb_fielddef_checkdescriptortype(int32_t type); |
| 1871 | bool upb_fielddef_checkintfmt(int32_t fmt); |
| 1872 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1873 | UPB_END_EXTERN_C |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1874 | |
| 1875 | |
| 1876 | /* upb::MessageDef ************************************************************/ |
| 1877 | |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 1878 | typedef upb_inttable_iter upb_msg_field_iter; |
| 1879 | typedef upb_strtable_iter upb_msg_oneof_iter; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1880 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1881 | #ifdef __cplusplus |
| 1882 | |
| 1883 | /* Structure that describes a single .proto message type. |
| 1884 | * |
| 1885 | * Its base class is upb::Def (use upb::upcast() to convert). */ |
| 1886 | class upb::MessageDef { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1887 | public: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1888 | /* Returns NULL if memory allocation failed. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1889 | static reffed_ptr<MessageDef> New(); |
| 1890 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1891 | /* upb::RefCounted methods like Ref()/Unref(). */ |
| 1892 | UPB_REFCOUNTED_CPPMETHODS |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1893 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1894 | /* Functionality from upb::Def. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1895 | const char* full_name() const; |
| 1896 | bool set_full_name(const char* fullname, Status* s); |
| 1897 | bool set_full_name(const std::string& fullname, Status* s); |
| 1898 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1899 | /* Call to freeze this MessageDef. |
| 1900 | * WARNING: this will fail if this message has any unfrozen submessages! |
| 1901 | * Messages with cycles must be frozen as a batch using upb::Def::Freeze(). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1902 | bool Freeze(Status* s); |
| 1903 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1904 | /* The number of fields that belong to the MessageDef. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1905 | int field_count() const; |
| 1906 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1907 | /* The number of oneofs that belong to the MessageDef. */ |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 1908 | int oneof_count() const; |
| 1909 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1910 | /* Adds a field (upb_fielddef object) to a msgdef. Requires that the msgdef |
| 1911 | * and the fielddefs are mutable. The fielddef's name and number must be |
| 1912 | * set, and the message may not already contain any field with this name or |
| 1913 | * number, and this fielddef may not be part of another message. In error |
| 1914 | * cases false is returned and the msgdef is unchanged. |
| 1915 | * |
| 1916 | * If the given field is part of a oneof, this call succeeds if and only if |
| 1917 | * that oneof is already part of this msgdef. (Note that adding a oneof to a |
| 1918 | * msgdef automatically adds all of its fields to the msgdef at the time that |
| 1919 | * the oneof is added, so it is usually more idiomatic to add the oneof's |
| 1920 | * fields first then add the oneof to the msgdef. This case is supported for |
| 1921 | * convenience.) |
| 1922 | * |
| 1923 | * If |f| is already part of this MessageDef, this method performs no action |
| 1924 | * and returns true (success). Thus, this method is idempotent. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1925 | bool AddField(FieldDef* f, Status* s); |
| 1926 | bool AddField(const reffed_ptr<FieldDef>& f, Status* s); |
| 1927 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1928 | /* Adds a oneof (upb_oneofdef object) to a msgdef. Requires that the msgdef, |
| 1929 | * oneof, and any fielddefs are mutable, that the fielddefs contained in the |
| 1930 | * oneof do not have any name or number conflicts with existing fields in the |
| 1931 | * msgdef, and that the oneof's name is unique among all oneofs in the msgdef. |
| 1932 | * If the oneof is added successfully, all of its fields will be added |
| 1933 | * directly to the msgdef as well. In error cases, false is returned and the |
| 1934 | * msgdef is unchanged. */ |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 1935 | bool AddOneof(OneofDef* o, Status* s); |
| 1936 | bool AddOneof(const reffed_ptr<OneofDef>& o, Status* s); |
| 1937 | |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 1938 | /* Set this to false to indicate that primitive fields should not have |
| 1939 | * explicit presence information associated with them. This will affect all |
| 1940 | * fields added to this message. Defaults to true. */ |
| 1941 | void SetPrimitivesHavePresence(bool have_presence); |
| 1942 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1943 | /* These return NULL if the field is not found. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1944 | FieldDef* FindFieldByNumber(uint32_t number); |
| 1945 | FieldDef* FindFieldByName(const char *name, size_t len); |
| 1946 | const FieldDef* FindFieldByNumber(uint32_t number) const; |
| 1947 | const FieldDef* FindFieldByName(const char* name, size_t len) const; |
| 1948 | |
| 1949 | |
| 1950 | FieldDef* FindFieldByName(const char *name) { |
| 1951 | return FindFieldByName(name, strlen(name)); |
| 1952 | } |
| 1953 | const FieldDef* FindFieldByName(const char *name) const { |
| 1954 | return FindFieldByName(name, strlen(name)); |
| 1955 | } |
| 1956 | |
| 1957 | template <class T> |
| 1958 | FieldDef* FindFieldByName(const T& str) { |
| 1959 | return FindFieldByName(str.c_str(), str.size()); |
| 1960 | } |
| 1961 | template <class T> |
| 1962 | const FieldDef* FindFieldByName(const T& str) const { |
| 1963 | return FindFieldByName(str.c_str(), str.size()); |
| 1964 | } |
| 1965 | |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 1966 | OneofDef* FindOneofByName(const char* name, size_t len); |
| 1967 | const OneofDef* FindOneofByName(const char* name, size_t len) const; |
| 1968 | |
| 1969 | OneofDef* FindOneofByName(const char* name) { |
| 1970 | return FindOneofByName(name, strlen(name)); |
| 1971 | } |
| 1972 | const OneofDef* FindOneofByName(const char* name) const { |
| 1973 | return FindOneofByName(name, strlen(name)); |
| 1974 | } |
| 1975 | |
| 1976 | template<class T> |
| 1977 | OneofDef* FindOneofByName(const T& str) { |
| 1978 | return FindOneofByName(str.c_str(), str.size()); |
| 1979 | } |
| 1980 | template<class T> |
| 1981 | const OneofDef* FindOneofByName(const T& str) const { |
| 1982 | return FindOneofByName(str.c_str(), str.size()); |
| 1983 | } |
| 1984 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1985 | /* Returns a new msgdef that is a copy of the given msgdef (and a copy of all |
| 1986 | * the fields) but with any references to submessages broken and replaced |
| 1987 | * with just the name of the submessage. Returns NULL if memory allocation |
| 1988 | * failed. |
| 1989 | * |
| 1990 | * TODO(haberman): which is more useful, keeping fields resolved or |
| 1991 | * unresolving them? If there's no obvious answer, Should this functionality |
| 1992 | * just be moved into symtab.c? */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 1993 | MessageDef* Dup(const void* owner) const; |
| 1994 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1995 | /* Is this message a map entry? */ |
Chris Fallin | fd1a3ff | 2015-01-06 15:44:09 -0800 | [diff] [blame] | 1996 | void setmapentry(bool map_entry); |
| 1997 | bool mapentry() const; |
| 1998 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 1999 | /* Iteration over fields. The order is undefined. */ |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2000 | class field_iterator |
| 2001 | : public std::iterator<std::forward_iterator_tag, FieldDef*> { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2002 | public: |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2003 | explicit field_iterator(MessageDef* md); |
| 2004 | static field_iterator end(MessageDef* md); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2005 | |
| 2006 | void operator++(); |
| 2007 | FieldDef* operator*() const; |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2008 | bool operator!=(const field_iterator& other) const; |
| 2009 | bool operator==(const field_iterator& other) const; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2010 | |
| 2011 | private: |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2012 | upb_msg_field_iter iter_; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2013 | }; |
| 2014 | |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2015 | class const_field_iterator |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2016 | : public std::iterator<std::forward_iterator_tag, const FieldDef*> { |
| 2017 | public: |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2018 | explicit const_field_iterator(const MessageDef* md); |
| 2019 | static const_field_iterator end(const MessageDef* md); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2020 | |
| 2021 | void operator++(); |
| 2022 | const FieldDef* operator*() const; |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2023 | bool operator!=(const const_field_iterator& other) const; |
| 2024 | bool operator==(const const_field_iterator& other) const; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2025 | |
| 2026 | private: |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2027 | upb_msg_field_iter iter_; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2028 | }; |
| 2029 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2030 | /* Iteration over oneofs. The order is undefined. */ |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2031 | class oneof_iterator |
| 2032 | : public std::iterator<std::forward_iterator_tag, FieldDef*> { |
| 2033 | public: |
| 2034 | explicit oneof_iterator(MessageDef* md); |
| 2035 | static oneof_iterator end(MessageDef* md); |
| 2036 | |
| 2037 | void operator++(); |
| 2038 | OneofDef* operator*() const; |
| 2039 | bool operator!=(const oneof_iterator& other) const; |
| 2040 | bool operator==(const oneof_iterator& other) const; |
| 2041 | |
| 2042 | private: |
| 2043 | upb_msg_oneof_iter iter_; |
| 2044 | }; |
| 2045 | |
| 2046 | class const_oneof_iterator |
| 2047 | : public std::iterator<std::forward_iterator_tag, const FieldDef*> { |
| 2048 | public: |
| 2049 | explicit const_oneof_iterator(const MessageDef* md); |
| 2050 | static const_oneof_iterator end(const MessageDef* md); |
| 2051 | |
| 2052 | void operator++(); |
| 2053 | const OneofDef* operator*() const; |
| 2054 | bool operator!=(const const_oneof_iterator& other) const; |
| 2055 | bool operator==(const const_oneof_iterator& other) const; |
| 2056 | |
| 2057 | private: |
| 2058 | upb_msg_oneof_iter iter_; |
| 2059 | }; |
| 2060 | |
| 2061 | class FieldAccessor { |
| 2062 | public: |
| 2063 | explicit FieldAccessor(MessageDef* msg) : msg_(msg) {} |
| 2064 | field_iterator begin() { return msg_->field_begin(); } |
| 2065 | field_iterator end() { return msg_->field_end(); } |
| 2066 | private: |
| 2067 | MessageDef* msg_; |
| 2068 | }; |
| 2069 | |
| 2070 | class ConstFieldAccessor { |
| 2071 | public: |
| 2072 | explicit ConstFieldAccessor(const MessageDef* msg) : msg_(msg) {} |
| 2073 | const_field_iterator begin() { return msg_->field_begin(); } |
| 2074 | const_field_iterator end() { return msg_->field_end(); } |
| 2075 | private: |
| 2076 | const MessageDef* msg_; |
| 2077 | }; |
| 2078 | |
| 2079 | class OneofAccessor { |
| 2080 | public: |
| 2081 | explicit OneofAccessor(MessageDef* msg) : msg_(msg) {} |
| 2082 | oneof_iterator begin() { return msg_->oneof_begin(); } |
| 2083 | oneof_iterator end() { return msg_->oneof_end(); } |
| 2084 | private: |
| 2085 | MessageDef* msg_; |
| 2086 | }; |
| 2087 | |
| 2088 | class ConstOneofAccessor { |
| 2089 | public: |
| 2090 | explicit ConstOneofAccessor(const MessageDef* msg) : msg_(msg) {} |
| 2091 | const_oneof_iterator begin() { return msg_->oneof_begin(); } |
| 2092 | const_oneof_iterator end() { return msg_->oneof_end(); } |
| 2093 | private: |
| 2094 | const MessageDef* msg_; |
| 2095 | }; |
| 2096 | |
| 2097 | field_iterator field_begin(); |
| 2098 | field_iterator field_end(); |
| 2099 | const_field_iterator field_begin() const; |
| 2100 | const_field_iterator field_end() const; |
| 2101 | |
| 2102 | oneof_iterator oneof_begin(); |
| 2103 | oneof_iterator oneof_end(); |
| 2104 | const_oneof_iterator oneof_begin() const; |
| 2105 | const_oneof_iterator oneof_end() const; |
| 2106 | |
| 2107 | FieldAccessor fields() { return FieldAccessor(this); } |
| 2108 | ConstFieldAccessor fields() const { return ConstFieldAccessor(this); } |
| 2109 | OneofAccessor oneofs() { return OneofAccessor(this); } |
| 2110 | ConstOneofAccessor oneofs() const { return ConstOneofAccessor(this); } |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2111 | |
| 2112 | private: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2113 | UPB_DISALLOW_POD_OPS(MessageDef, upb::MessageDef) |
| 2114 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2115 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2116 | #endif /* __cplusplus */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2117 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2118 | UPB_BEGIN_EXTERN_C |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2119 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2120 | /* Returns NULL if memory allocation failed. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2121 | upb_msgdef *upb_msgdef_new(const void *owner); |
| 2122 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2123 | /* Include upb_refcounted methods like upb_msgdef_ref(). */ |
| 2124 | UPB_REFCOUNTED_CMETHODS(upb_msgdef, upb_msgdef_upcast2) |
| 2125 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2126 | bool upb_msgdef_freeze(upb_msgdef *m, upb_status *status); |
| 2127 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2128 | const char *upb_msgdef_fullname(const upb_msgdef *m); |
| 2129 | bool upb_msgdef_setfullname(upb_msgdef *m, const char *fullname, upb_status *s); |
| 2130 | |
| 2131 | upb_msgdef *upb_msgdef_dup(const upb_msgdef *m, const void *owner); |
| 2132 | bool upb_msgdef_addfield(upb_msgdef *m, upb_fielddef *f, const void *ref_donor, |
| 2133 | upb_status *s); |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2134 | bool upb_msgdef_addoneof(upb_msgdef *m, upb_oneofdef *o, const void *ref_donor, |
| 2135 | upb_status *s); |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 2136 | void upb_msgdef_setprimitiveshavepresence(upb_msgdef *m, bool have_presence); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2137 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2138 | /* Field lookup in a couple of different variations: |
| 2139 | * - itof = int to field |
| 2140 | * - ntof = name to field |
| 2141 | * - ntofz = name to field, null-terminated string. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2142 | const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i); |
| 2143 | const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name, |
| 2144 | size_t len); |
| 2145 | int upb_msgdef_numfields(const upb_msgdef *m); |
| 2146 | |
| 2147 | UPB_INLINE const upb_fielddef *upb_msgdef_ntofz(const upb_msgdef *m, |
| 2148 | const char *name) { |
| 2149 | return upb_msgdef_ntof(m, name, strlen(name)); |
| 2150 | } |
| 2151 | |
| 2152 | UPB_INLINE upb_fielddef *upb_msgdef_itof_mutable(upb_msgdef *m, uint32_t i) { |
| 2153 | return (upb_fielddef*)upb_msgdef_itof(m, i); |
| 2154 | } |
| 2155 | |
| 2156 | UPB_INLINE upb_fielddef *upb_msgdef_ntof_mutable(upb_msgdef *m, |
| 2157 | const char *name, size_t len) { |
| 2158 | return (upb_fielddef *)upb_msgdef_ntof(m, name, len); |
| 2159 | } |
| 2160 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2161 | /* Oneof lookup: |
| 2162 | * - ntoo = name to oneof |
| 2163 | * - ntooz = name to oneof, null-terminated string. */ |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2164 | const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name, |
| 2165 | size_t len); |
| 2166 | int upb_msgdef_numoneofs(const upb_msgdef *m); |
| 2167 | |
| 2168 | UPB_INLINE const upb_oneofdef *upb_msgdef_ntooz(const upb_msgdef *m, |
| 2169 | const char *name) { |
| 2170 | return upb_msgdef_ntoo(m, name, strlen(name)); |
| 2171 | } |
| 2172 | |
| 2173 | UPB_INLINE upb_oneofdef *upb_msgdef_ntoo_mutable(upb_msgdef *m, |
| 2174 | const char *name, size_t len) { |
| 2175 | return (upb_oneofdef *)upb_msgdef_ntoo(m, name, len); |
| 2176 | } |
| 2177 | |
Chris Fallin | fd1a3ff | 2015-01-06 15:44:09 -0800 | [diff] [blame] | 2178 | void upb_msgdef_setmapentry(upb_msgdef *m, bool map_entry); |
| 2179 | bool upb_msgdef_mapentry(const upb_msgdef *m); |
| 2180 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2181 | /* Well-known field tag numbers for map-entry messages. */ |
Chris Fallin | a507592 | 2015-02-02 15:07:34 -0800 | [diff] [blame] | 2182 | #define UPB_MAPENTRY_KEY 1 |
| 2183 | #define UPB_MAPENTRY_VALUE 2 |
| 2184 | |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2185 | const upb_oneofdef *upb_msgdef_findoneof(const upb_msgdef *m, |
| 2186 | const char *name); |
| 2187 | int upb_msgdef_numoneofs(const upb_msgdef *m); |
| 2188 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2189 | /* upb_msg_field_iter i; |
| 2190 | * for(upb_msg_field_begin(&i, m); |
| 2191 | * !upb_msg_field_done(&i); |
| 2192 | * upb_msg_field_next(&i)) { |
| 2193 | * upb_fielddef *f = upb_msg_iter_field(&i); |
| 2194 | * // ... |
| 2195 | * } |
| 2196 | * |
| 2197 | * For C we don't have separate iterators for const and non-const. |
| 2198 | * It is the caller's responsibility to cast the upb_fielddef* to |
| 2199 | * const if the upb_msgdef* is const. */ |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2200 | void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m); |
| 2201 | void upb_msg_field_next(upb_msg_field_iter *iter); |
| 2202 | bool upb_msg_field_done(const upb_msg_field_iter *iter); |
| 2203 | upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter); |
| 2204 | void upb_msg_field_iter_setdone(upb_msg_field_iter *iter); |
| 2205 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2206 | /* Similar to above, we also support iterating through the oneofs in a |
| 2207 | * msgdef. */ |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2208 | void upb_msg_oneof_begin(upb_msg_oneof_iter *iter, const upb_msgdef *m); |
| 2209 | void upb_msg_oneof_next(upb_msg_oneof_iter *iter); |
| 2210 | bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter); |
| 2211 | upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter); |
| 2212 | void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2213 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2214 | UPB_END_EXTERN_C |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2215 | |
| 2216 | |
| 2217 | /* upb::EnumDef ***************************************************************/ |
| 2218 | |
| 2219 | typedef upb_strtable_iter upb_enum_iter; |
| 2220 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2221 | #ifdef __cplusplus |
| 2222 | |
| 2223 | /* Class that represents an enum. Its base class is upb::Def (convert with |
| 2224 | * upb::upcast()). */ |
| 2225 | class upb::EnumDef { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2226 | public: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2227 | /* Returns NULL if memory allocation failed. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2228 | static reffed_ptr<EnumDef> New(); |
| 2229 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2230 | /* upb::RefCounted methods like Ref()/Unref(). */ |
| 2231 | UPB_REFCOUNTED_CPPMETHODS |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2232 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2233 | /* Functionality from upb::Def. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2234 | const char* full_name() const; |
| 2235 | bool set_full_name(const char* fullname, Status* s); |
| 2236 | bool set_full_name(const std::string& fullname, Status* s); |
| 2237 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2238 | /* Call to freeze this EnumDef. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2239 | bool Freeze(Status* s); |
| 2240 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2241 | /* The value that is used as the default when no field default is specified. |
| 2242 | * If not set explicitly, the first value that was added will be used. |
| 2243 | * The default value must be a member of the enum. |
| 2244 | * Requires that value_count() > 0. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2245 | int32_t default_value() const; |
| 2246 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2247 | /* Sets the default value. If this value is not valid, returns false and an |
| 2248 | * error message in status. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2249 | bool set_default_value(int32_t val, Status* status); |
| 2250 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2251 | /* Returns the number of values currently defined in the enum. Note that |
| 2252 | * multiple names can refer to the same number, so this may be greater than |
| 2253 | * the total number of unique numbers. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2254 | int value_count() const; |
| 2255 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2256 | /* Adds a single name/number pair to the enum. Fails if this name has |
| 2257 | * already been used by another value. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2258 | bool AddValue(const char* name, int32_t num, Status* status); |
| 2259 | bool AddValue(const std::string& name, int32_t num, Status* status); |
| 2260 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2261 | /* Lookups from name to integer, returning true if found. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2262 | bool FindValueByName(const char* name, int32_t* num) const; |
| 2263 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2264 | /* Finds the name corresponding to the given number, or NULL if none was |
| 2265 | * found. If more than one name corresponds to this number, returns the |
| 2266 | * first one that was added. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2267 | const char* FindValueByNumber(int32_t num) const; |
| 2268 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2269 | /* Returns a new EnumDef with all the same values. The new EnumDef will be |
| 2270 | * owned by the given owner. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2271 | EnumDef* Dup(const void* owner) const; |
| 2272 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2273 | /* Iteration over name/value pairs. The order is undefined. |
| 2274 | * Adding an enum val invalidates any iterators. |
| 2275 | * |
| 2276 | * TODO: make compatible with range-for, with elements as pairs? */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2277 | class Iterator { |
| 2278 | public: |
| 2279 | explicit Iterator(const EnumDef*); |
| 2280 | |
| 2281 | int32_t number(); |
| 2282 | const char *name(); |
| 2283 | bool Done(); |
| 2284 | void Next(); |
| 2285 | |
| 2286 | private: |
| 2287 | upb_enum_iter iter_; |
| 2288 | }; |
| 2289 | |
| 2290 | private: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2291 | UPB_DISALLOW_POD_OPS(EnumDef, upb::EnumDef) |
| 2292 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2293 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2294 | #endif /* __cplusplus */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2295 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2296 | UPB_BEGIN_EXTERN_C |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2297 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2298 | /* Native C API. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2299 | upb_enumdef *upb_enumdef_new(const void *owner); |
| 2300 | upb_enumdef *upb_enumdef_dup(const upb_enumdef *e, const void *owner); |
| 2301 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2302 | /* Include upb_refcounted methods like upb_enumdef_ref(). */ |
| 2303 | UPB_REFCOUNTED_CMETHODS(upb_enumdef, upb_enumdef_upcast2) |
| 2304 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2305 | bool upb_enumdef_freeze(upb_enumdef *e, upb_status *status); |
| 2306 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2307 | /* From upb_def. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2308 | const char *upb_enumdef_fullname(const upb_enumdef *e); |
| 2309 | bool upb_enumdef_setfullname(upb_enumdef *e, const char *fullname, |
| 2310 | upb_status *s); |
| 2311 | |
| 2312 | int32_t upb_enumdef_default(const upb_enumdef *e); |
| 2313 | bool upb_enumdef_setdefault(upb_enumdef *e, int32_t val, upb_status *s); |
| 2314 | int upb_enumdef_numvals(const upb_enumdef *e); |
| 2315 | bool upb_enumdef_addval(upb_enumdef *e, const char *name, int32_t num, |
| 2316 | upb_status *status); |
| 2317 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2318 | /* Enum lookups: |
| 2319 | * - ntoi: look up a name with specified length. |
| 2320 | * - ntoiz: look up a name provided as a null-terminated string. |
| 2321 | * - iton: look up an integer, returning the name as a null-terminated |
| 2322 | * string. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2323 | bool upb_enumdef_ntoi(const upb_enumdef *e, const char *name, size_t len, |
| 2324 | int32_t *num); |
| 2325 | UPB_INLINE bool upb_enumdef_ntoiz(const upb_enumdef *e, |
| 2326 | const char *name, int32_t *num) { |
| 2327 | return upb_enumdef_ntoi(e, name, strlen(name), num); |
| 2328 | } |
| 2329 | const char *upb_enumdef_iton(const upb_enumdef *e, int32_t num); |
| 2330 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2331 | /* upb_enum_iter i; |
| 2332 | * for(upb_enum_begin(&i, e); !upb_enum_done(&i); upb_enum_next(&i)) { |
| 2333 | * // ... |
| 2334 | * } |
| 2335 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2336 | void upb_enum_begin(upb_enum_iter *iter, const upb_enumdef *e); |
| 2337 | void upb_enum_next(upb_enum_iter *iter); |
| 2338 | bool upb_enum_done(upb_enum_iter *iter); |
| 2339 | const char *upb_enum_iter_name(upb_enum_iter *iter); |
| 2340 | int32_t upb_enum_iter_number(upb_enum_iter *iter); |
| 2341 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2342 | UPB_END_EXTERN_C |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2343 | |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2344 | /* upb::OneofDef **************************************************************/ |
| 2345 | |
| 2346 | typedef upb_inttable_iter upb_oneof_iter; |
| 2347 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2348 | #ifdef __cplusplus |
| 2349 | |
| 2350 | /* Class that represents a oneof. Its base class is upb::Def (convert with |
| 2351 | * upb::upcast()). */ |
| 2352 | class upb::OneofDef { |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2353 | public: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2354 | /* Returns NULL if memory allocation failed. */ |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2355 | static reffed_ptr<OneofDef> New(); |
| 2356 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2357 | /* upb::RefCounted methods like Ref()/Unref(). */ |
| 2358 | UPB_REFCOUNTED_CPPMETHODS |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2359 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2360 | /* Functionality from upb::Def. */ |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2361 | const char* full_name() const; |
| 2362 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2363 | /* Returns the MessageDef that owns this OneofDef. */ |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2364 | const MessageDef* containing_type() const; |
| 2365 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2366 | /* Returns the name of this oneof. This is the name used to look up the oneof |
| 2367 | * by name once added to a message def. */ |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2368 | const char* name() const; |
| 2369 | bool set_name(const char* name, Status* s); |
| 2370 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2371 | /* Returns the number of fields currently defined in the oneof. */ |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2372 | int field_count() const; |
| 2373 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2374 | /* Adds a field to the oneof. The field must not have been added to any other |
| 2375 | * oneof or msgdef. If the oneof is not yet part of a msgdef, then when the |
| 2376 | * oneof is eventually added to a msgdef, all fields added to the oneof will |
| 2377 | * also be added to the msgdef at that time. If the oneof is already part of a |
| 2378 | * msgdef, the field must either be a part of that msgdef already, or must not |
| 2379 | * be a part of any msgdef; in the latter case, the field is added to the |
| 2380 | * msgdef as a part of this operation. |
| 2381 | * |
| 2382 | * The field may only have an OPTIONAL label, never REQUIRED or REPEATED. |
| 2383 | * |
| 2384 | * If |f| is already part of this MessageDef, this method performs no action |
| 2385 | * and returns true (success). Thus, this method is idempotent. */ |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2386 | bool AddField(FieldDef* field, Status* s); |
| 2387 | bool AddField(const reffed_ptr<FieldDef>& field, Status* s); |
| 2388 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2389 | /* Looks up by name. */ |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2390 | const FieldDef* FindFieldByName(const char* name, size_t len) const; |
| 2391 | FieldDef* FindFieldByName(const char* name, size_t len); |
| 2392 | const FieldDef* FindFieldByName(const char* name) const { |
| 2393 | return FindFieldByName(name, strlen(name)); |
| 2394 | } |
| 2395 | FieldDef* FindFieldByName(const char* name) { |
| 2396 | return FindFieldByName(name, strlen(name)); |
| 2397 | } |
| 2398 | |
| 2399 | template <class T> |
| 2400 | FieldDef* FindFieldByName(const T& str) { |
| 2401 | return FindFieldByName(str.c_str(), str.size()); |
| 2402 | } |
| 2403 | template <class T> |
| 2404 | const FieldDef* FindFieldByName(const T& str) const { |
| 2405 | return FindFieldByName(str.c_str(), str.size()); |
| 2406 | } |
| 2407 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2408 | /* Looks up by tag number. */ |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2409 | const FieldDef* FindFieldByNumber(uint32_t num) const; |
| 2410 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2411 | /* Returns a new OneofDef with all the same fields. The OneofDef will be owned |
| 2412 | * by the given owner. */ |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2413 | OneofDef* Dup(const void* owner) const; |
| 2414 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2415 | /* Iteration over fields. The order is undefined. */ |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2416 | class iterator : public std::iterator<std::forward_iterator_tag, FieldDef*> { |
| 2417 | public: |
| 2418 | explicit iterator(OneofDef* md); |
| 2419 | static iterator end(OneofDef* md); |
| 2420 | |
| 2421 | void operator++(); |
| 2422 | FieldDef* operator*() const; |
| 2423 | bool operator!=(const iterator& other) const; |
| 2424 | bool operator==(const iterator& other) const; |
| 2425 | |
| 2426 | private: |
| 2427 | upb_oneof_iter iter_; |
| 2428 | }; |
| 2429 | |
| 2430 | class const_iterator |
| 2431 | : public std::iterator<std::forward_iterator_tag, const FieldDef*> { |
| 2432 | public: |
| 2433 | explicit const_iterator(const OneofDef* md); |
| 2434 | static const_iterator end(const OneofDef* md); |
| 2435 | |
| 2436 | void operator++(); |
| 2437 | const FieldDef* operator*() const; |
| 2438 | bool operator!=(const const_iterator& other) const; |
| 2439 | bool operator==(const const_iterator& other) const; |
| 2440 | |
| 2441 | private: |
| 2442 | upb_oneof_iter iter_; |
| 2443 | }; |
| 2444 | |
| 2445 | iterator begin(); |
| 2446 | iterator end(); |
| 2447 | const_iterator begin() const; |
| 2448 | const_iterator end() const; |
| 2449 | |
| 2450 | private: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2451 | UPB_DISALLOW_POD_OPS(OneofDef, upb::OneofDef) |
| 2452 | }; |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2453 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2454 | #endif /* __cplusplus */ |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2455 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2456 | UPB_BEGIN_EXTERN_C |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2457 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2458 | /* Native C API. */ |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2459 | upb_oneofdef *upb_oneofdef_new(const void *owner); |
| 2460 | upb_oneofdef *upb_oneofdef_dup(const upb_oneofdef *o, const void *owner); |
| 2461 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2462 | /* Include upb_refcounted methods like upb_oneofdef_ref(). */ |
| 2463 | UPB_REFCOUNTED_CMETHODS(upb_oneofdef, upb_oneofdef_upcast2) |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2464 | |
| 2465 | const char *upb_oneofdef_name(const upb_oneofdef *o); |
| 2466 | bool upb_oneofdef_setname(upb_oneofdef *o, const char *name, upb_status *s); |
| 2467 | |
| 2468 | const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o); |
| 2469 | int upb_oneofdef_numfields(const upb_oneofdef *o); |
| 2470 | bool upb_oneofdef_addfield(upb_oneofdef *o, upb_fielddef *f, |
| 2471 | const void *ref_donor, |
| 2472 | upb_status *s); |
| 2473 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2474 | /* Oneof lookups: |
| 2475 | * - ntof: look up a field by name. |
| 2476 | * - ntofz: look up a field by name (as a null-terminated string). |
| 2477 | * - itof: look up a field by number. */ |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2478 | const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o, |
| 2479 | const char *name, size_t length); |
| 2480 | UPB_INLINE const upb_fielddef *upb_oneofdef_ntofz(const upb_oneofdef *o, |
| 2481 | const char *name) { |
| 2482 | return upb_oneofdef_ntof(o, name, strlen(name)); |
| 2483 | } |
| 2484 | const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num); |
| 2485 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2486 | /* upb_oneof_iter i; |
| 2487 | * for(upb_oneof_begin(&i, e); !upb_oneof_done(&i); upb_oneof_next(&i)) { |
| 2488 | * // ... |
| 2489 | * } |
| 2490 | */ |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2491 | void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o); |
| 2492 | void upb_oneof_next(upb_oneof_iter *iter); |
| 2493 | bool upb_oneof_done(upb_oneof_iter *iter); |
| 2494 | upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter); |
| 2495 | void upb_oneof_iter_setdone(upb_oneof_iter *iter); |
| 2496 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2497 | UPB_END_EXTERN_C |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2498 | |
| 2499 | #ifdef __cplusplus |
| 2500 | |
| 2501 | UPB_INLINE const char* upb_safecstr(const std::string& str) { |
| 2502 | assert(str.size() == std::strlen(str.c_str())); |
| 2503 | return str.c_str(); |
| 2504 | } |
| 2505 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 2506 | /* Inline C++ wrappers. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2507 | namespace upb { |
| 2508 | |
| 2509 | inline Def* Def::Dup(const void* owner) const { |
| 2510 | return upb_def_dup(this, owner); |
| 2511 | } |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2512 | inline Def::Type Def::def_type() const { return upb_def_type(this); } |
| 2513 | inline const char* Def::full_name() const { return upb_def_fullname(this); } |
| 2514 | inline bool Def::set_full_name(const char* fullname, Status* s) { |
| 2515 | return upb_def_setfullname(this, fullname, s); |
| 2516 | } |
| 2517 | inline bool Def::set_full_name(const std::string& fullname, Status* s) { |
| 2518 | return upb_def_setfullname(this, upb_safecstr(fullname), s); |
| 2519 | } |
| 2520 | inline bool Def::Freeze(Def* const* defs, int n, Status* status) { |
| 2521 | return upb_def_freeze(defs, n, status); |
| 2522 | } |
| 2523 | inline bool Def::Freeze(const std::vector<Def*>& defs, Status* status) { |
| 2524 | return upb_def_freeze((Def* const*)&defs[0], defs.size(), status); |
| 2525 | } |
| 2526 | |
| 2527 | inline bool FieldDef::CheckType(int32_t val) { |
| 2528 | return upb_fielddef_checktype(val); |
| 2529 | } |
| 2530 | inline bool FieldDef::CheckLabel(int32_t val) { |
| 2531 | return upb_fielddef_checklabel(val); |
| 2532 | } |
| 2533 | inline bool FieldDef::CheckDescriptorType(int32_t val) { |
| 2534 | return upb_fielddef_checkdescriptortype(val); |
| 2535 | } |
| 2536 | inline bool FieldDef::CheckIntegerFormat(int32_t val) { |
| 2537 | return upb_fielddef_checkintfmt(val); |
| 2538 | } |
| 2539 | inline FieldDef::Type FieldDef::ConvertType(int32_t val) { |
| 2540 | assert(CheckType(val)); |
| 2541 | return static_cast<FieldDef::Type>(val); |
| 2542 | } |
| 2543 | inline FieldDef::Label FieldDef::ConvertLabel(int32_t val) { |
| 2544 | assert(CheckLabel(val)); |
| 2545 | return static_cast<FieldDef::Label>(val); |
| 2546 | } |
| 2547 | inline FieldDef::DescriptorType FieldDef::ConvertDescriptorType(int32_t val) { |
| 2548 | assert(CheckDescriptorType(val)); |
| 2549 | return static_cast<FieldDef::DescriptorType>(val); |
| 2550 | } |
| 2551 | inline FieldDef::IntegerFormat FieldDef::ConvertIntegerFormat(int32_t val) { |
| 2552 | assert(CheckIntegerFormat(val)); |
| 2553 | return static_cast<FieldDef::IntegerFormat>(val); |
| 2554 | } |
| 2555 | |
| 2556 | inline reffed_ptr<FieldDef> FieldDef::New() { |
| 2557 | upb_fielddef *f = upb_fielddef_new(&f); |
| 2558 | return reffed_ptr<FieldDef>(f, &f); |
| 2559 | } |
| 2560 | inline FieldDef* FieldDef::Dup(const void* owner) const { |
| 2561 | return upb_fielddef_dup(this, owner); |
| 2562 | } |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2563 | inline const char* FieldDef::full_name() const { |
| 2564 | return upb_fielddef_fullname(this); |
| 2565 | } |
| 2566 | inline bool FieldDef::set_full_name(const char* fullname, Status* s) { |
| 2567 | return upb_fielddef_setfullname(this, fullname, s); |
| 2568 | } |
| 2569 | inline bool FieldDef::set_full_name(const std::string& fullname, Status* s) { |
| 2570 | return upb_fielddef_setfullname(this, upb_safecstr(fullname), s); |
| 2571 | } |
| 2572 | inline bool FieldDef::type_is_set() const { |
| 2573 | return upb_fielddef_typeisset(this); |
| 2574 | } |
| 2575 | inline FieldDef::Type FieldDef::type() const { return upb_fielddef_type(this); } |
| 2576 | inline FieldDef::DescriptorType FieldDef::descriptor_type() const { |
| 2577 | return upb_fielddef_descriptortype(this); |
| 2578 | } |
| 2579 | inline FieldDef::Label FieldDef::label() const { |
| 2580 | return upb_fielddef_label(this); |
| 2581 | } |
| 2582 | inline uint32_t FieldDef::number() const { return upb_fielddef_number(this); } |
| 2583 | inline const char* FieldDef::name() const { return upb_fielddef_name(this); } |
| 2584 | inline bool FieldDef::is_extension() const { |
| 2585 | return upb_fielddef_isextension(this); |
| 2586 | } |
Josh Haberman | f654d49 | 2016-02-18 11:07:51 -0800 | [diff] [blame] | 2587 | inline size_t FieldDef::GetJsonName(char* buf, size_t len) const { |
| 2588 | return upb_fielddef_getjsonname(this, buf, len); |
| 2589 | } |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2590 | inline bool FieldDef::lazy() const { |
| 2591 | return upb_fielddef_lazy(this); |
| 2592 | } |
| 2593 | inline void FieldDef::set_lazy(bool lazy) { |
| 2594 | upb_fielddef_setlazy(this, lazy); |
| 2595 | } |
| 2596 | inline bool FieldDef::packed() const { |
| 2597 | return upb_fielddef_packed(this); |
| 2598 | } |
| 2599 | inline void FieldDef::set_packed(bool packed) { |
| 2600 | upb_fielddef_setpacked(this, packed); |
| 2601 | } |
| 2602 | inline const MessageDef* FieldDef::containing_type() const { |
| 2603 | return upb_fielddef_containingtype(this); |
| 2604 | } |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2605 | inline const OneofDef* FieldDef::containing_oneof() const { |
| 2606 | return upb_fielddef_containingoneof(this); |
| 2607 | } |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2608 | inline const char* FieldDef::containing_type_name() { |
| 2609 | return upb_fielddef_containingtypename(this); |
| 2610 | } |
| 2611 | inline bool FieldDef::set_number(uint32_t number, Status* s) { |
| 2612 | return upb_fielddef_setnumber(this, number, s); |
| 2613 | } |
| 2614 | inline bool FieldDef::set_name(const char *name, Status* s) { |
| 2615 | return upb_fielddef_setname(this, name, s); |
| 2616 | } |
| 2617 | inline bool FieldDef::set_name(const std::string& name, Status* s) { |
| 2618 | return upb_fielddef_setname(this, upb_safecstr(name), s); |
| 2619 | } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 2620 | inline bool FieldDef::set_json_name(const char *name, Status* s) { |
| 2621 | return upb_fielddef_setjsonname(this, name, s); |
| 2622 | } |
| 2623 | inline bool FieldDef::set_json_name(const std::string& name, Status* s) { |
| 2624 | return upb_fielddef_setjsonname(this, upb_safecstr(name), s); |
| 2625 | } |
| 2626 | inline void FieldDef::clear_json_name() { |
| 2627 | upb_fielddef_clearjsonname(this); |
| 2628 | } |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2629 | inline bool FieldDef::set_containing_type_name(const char *name, Status* s) { |
| 2630 | return upb_fielddef_setcontainingtypename(this, name, s); |
| 2631 | } |
| 2632 | inline bool FieldDef::set_containing_type_name(const std::string &name, |
| 2633 | Status *s) { |
| 2634 | return upb_fielddef_setcontainingtypename(this, upb_safecstr(name), s); |
| 2635 | } |
| 2636 | inline void FieldDef::set_type(upb_fieldtype_t type) { |
| 2637 | upb_fielddef_settype(this, type); |
| 2638 | } |
| 2639 | inline void FieldDef::set_is_extension(bool is_extension) { |
| 2640 | upb_fielddef_setisextension(this, is_extension); |
| 2641 | } |
| 2642 | inline void FieldDef::set_descriptor_type(FieldDef::DescriptorType type) { |
| 2643 | upb_fielddef_setdescriptortype(this, type); |
| 2644 | } |
| 2645 | inline void FieldDef::set_label(upb_label_t label) { |
| 2646 | upb_fielddef_setlabel(this, label); |
| 2647 | } |
| 2648 | inline bool FieldDef::IsSubMessage() const { |
| 2649 | return upb_fielddef_issubmsg(this); |
| 2650 | } |
| 2651 | inline bool FieldDef::IsString() const { return upb_fielddef_isstring(this); } |
| 2652 | inline bool FieldDef::IsSequence() const { return upb_fielddef_isseq(this); } |
Chris Fallin | a507592 | 2015-02-02 15:07:34 -0800 | [diff] [blame] | 2653 | inline bool FieldDef::IsMap() const { return upb_fielddef_ismap(this); } |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2654 | inline int64_t FieldDef::default_int64() const { |
| 2655 | return upb_fielddef_defaultint64(this); |
| 2656 | } |
| 2657 | inline int32_t FieldDef::default_int32() const { |
| 2658 | return upb_fielddef_defaultint32(this); |
| 2659 | } |
| 2660 | inline uint64_t FieldDef::default_uint64() const { |
| 2661 | return upb_fielddef_defaultuint64(this); |
| 2662 | } |
| 2663 | inline uint32_t FieldDef::default_uint32() const { |
| 2664 | return upb_fielddef_defaultuint32(this); |
| 2665 | } |
| 2666 | inline bool FieldDef::default_bool() const { |
| 2667 | return upb_fielddef_defaultbool(this); |
| 2668 | } |
| 2669 | inline float FieldDef::default_float() const { |
| 2670 | return upb_fielddef_defaultfloat(this); |
| 2671 | } |
| 2672 | inline double FieldDef::default_double() const { |
| 2673 | return upb_fielddef_defaultdouble(this); |
| 2674 | } |
| 2675 | inline const char* FieldDef::default_string(size_t* len) const { |
| 2676 | return upb_fielddef_defaultstr(this, len); |
| 2677 | } |
| 2678 | inline void FieldDef::set_default_int64(int64_t value) { |
| 2679 | upb_fielddef_setdefaultint64(this, value); |
| 2680 | } |
| 2681 | inline void FieldDef::set_default_int32(int32_t value) { |
| 2682 | upb_fielddef_setdefaultint32(this, value); |
| 2683 | } |
| 2684 | inline void FieldDef::set_default_uint64(uint64_t value) { |
| 2685 | upb_fielddef_setdefaultuint64(this, value); |
| 2686 | } |
| 2687 | inline void FieldDef::set_default_uint32(uint32_t value) { |
| 2688 | upb_fielddef_setdefaultuint32(this, value); |
| 2689 | } |
| 2690 | inline void FieldDef::set_default_bool(bool value) { |
| 2691 | upb_fielddef_setdefaultbool(this, value); |
| 2692 | } |
| 2693 | inline void FieldDef::set_default_float(float value) { |
| 2694 | upb_fielddef_setdefaultfloat(this, value); |
| 2695 | } |
| 2696 | inline void FieldDef::set_default_double(double value) { |
| 2697 | upb_fielddef_setdefaultdouble(this, value); |
| 2698 | } |
| 2699 | inline bool FieldDef::set_default_string(const void *str, size_t len, |
| 2700 | Status *s) { |
| 2701 | return upb_fielddef_setdefaultstr(this, str, len, s); |
| 2702 | } |
| 2703 | inline bool FieldDef::set_default_string(const std::string& str, Status* s) { |
| 2704 | return upb_fielddef_setdefaultstr(this, str.c_str(), str.size(), s); |
| 2705 | } |
| 2706 | inline void FieldDef::set_default_cstr(const char* str, Status* s) { |
| 2707 | return upb_fielddef_setdefaultcstr(this, str, s); |
| 2708 | } |
| 2709 | inline bool FieldDef::HasSubDef() const { return upb_fielddef_hassubdef(this); } |
| 2710 | inline const Def* FieldDef::subdef() const { return upb_fielddef_subdef(this); } |
| 2711 | inline const MessageDef *FieldDef::message_subdef() const { |
| 2712 | return upb_fielddef_msgsubdef(this); |
| 2713 | } |
| 2714 | inline const EnumDef *FieldDef::enum_subdef() const { |
| 2715 | return upb_fielddef_enumsubdef(this); |
| 2716 | } |
| 2717 | inline const char* FieldDef::subdef_name() const { |
| 2718 | return upb_fielddef_subdefname(this); |
| 2719 | } |
| 2720 | inline bool FieldDef::set_subdef(const Def* subdef, Status* s) { |
| 2721 | return upb_fielddef_setsubdef(this, subdef, s); |
| 2722 | } |
| 2723 | inline bool FieldDef::set_enum_subdef(const EnumDef* subdef, Status* s) { |
| 2724 | return upb_fielddef_setenumsubdef(this, subdef, s); |
| 2725 | } |
| 2726 | inline bool FieldDef::set_message_subdef(const MessageDef* subdef, Status* s) { |
| 2727 | return upb_fielddef_setmsgsubdef(this, subdef, s); |
| 2728 | } |
| 2729 | inline bool FieldDef::set_subdef_name(const char* name, Status* s) { |
| 2730 | return upb_fielddef_setsubdefname(this, name, s); |
| 2731 | } |
| 2732 | inline bool FieldDef::set_subdef_name(const std::string& name, Status* s) { |
| 2733 | return upb_fielddef_setsubdefname(this, upb_safecstr(name), s); |
| 2734 | } |
| 2735 | |
| 2736 | inline reffed_ptr<MessageDef> MessageDef::New() { |
| 2737 | upb_msgdef *m = upb_msgdef_new(&m); |
| 2738 | return reffed_ptr<MessageDef>(m, &m); |
| 2739 | } |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2740 | inline const char *MessageDef::full_name() const { |
| 2741 | return upb_msgdef_fullname(this); |
| 2742 | } |
| 2743 | inline bool MessageDef::set_full_name(const char* fullname, Status* s) { |
| 2744 | return upb_msgdef_setfullname(this, fullname, s); |
| 2745 | } |
| 2746 | inline bool MessageDef::set_full_name(const std::string& fullname, Status* s) { |
| 2747 | return upb_msgdef_setfullname(this, upb_safecstr(fullname), s); |
| 2748 | } |
| 2749 | inline bool MessageDef::Freeze(Status* status) { |
| 2750 | return upb_msgdef_freeze(this, status); |
| 2751 | } |
| 2752 | inline int MessageDef::field_count() const { |
| 2753 | return upb_msgdef_numfields(this); |
| 2754 | } |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2755 | inline int MessageDef::oneof_count() const { |
| 2756 | return upb_msgdef_numoneofs(this); |
| 2757 | } |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2758 | inline bool MessageDef::AddField(upb_fielddef* f, Status* s) { |
| 2759 | return upb_msgdef_addfield(this, f, NULL, s); |
| 2760 | } |
| 2761 | inline bool MessageDef::AddField(const reffed_ptr<FieldDef>& f, Status* s) { |
| 2762 | return upb_msgdef_addfield(this, f.get(), NULL, s); |
| 2763 | } |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2764 | inline bool MessageDef::AddOneof(upb_oneofdef* o, Status* s) { |
| 2765 | return upb_msgdef_addoneof(this, o, NULL, s); |
| 2766 | } |
| 2767 | inline bool MessageDef::AddOneof(const reffed_ptr<OneofDef>& o, Status* s) { |
| 2768 | return upb_msgdef_addoneof(this, o.get(), NULL, s); |
| 2769 | } |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2770 | inline FieldDef* MessageDef::FindFieldByNumber(uint32_t number) { |
| 2771 | return upb_msgdef_itof_mutable(this, number); |
| 2772 | } |
| 2773 | inline FieldDef* MessageDef::FindFieldByName(const char* name, size_t len) { |
| 2774 | return upb_msgdef_ntof_mutable(this, name, len); |
| 2775 | } |
| 2776 | inline const FieldDef* MessageDef::FindFieldByNumber(uint32_t number) const { |
| 2777 | return upb_msgdef_itof(this, number); |
| 2778 | } |
| 2779 | inline const FieldDef *MessageDef::FindFieldByName(const char *name, |
| 2780 | size_t len) const { |
| 2781 | return upb_msgdef_ntof(this, name, len); |
| 2782 | } |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2783 | inline OneofDef* MessageDef::FindOneofByName(const char* name, size_t len) { |
| 2784 | return upb_msgdef_ntoo_mutable(this, name, len); |
| 2785 | } |
| 2786 | inline const OneofDef* MessageDef::FindOneofByName(const char* name, |
| 2787 | size_t len) const { |
| 2788 | return upb_msgdef_ntoo(this, name, len); |
| 2789 | } |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2790 | inline MessageDef* MessageDef::Dup(const void *owner) const { |
| 2791 | return upb_msgdef_dup(this, owner); |
| 2792 | } |
Chris Fallin | fd1a3ff | 2015-01-06 15:44:09 -0800 | [diff] [blame] | 2793 | inline void MessageDef::setmapentry(bool map_entry) { |
| 2794 | upb_msgdef_setmapentry(this, map_entry); |
| 2795 | } |
| 2796 | inline bool MessageDef::mapentry() const { |
| 2797 | return upb_msgdef_mapentry(this); |
| 2798 | } |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2799 | inline MessageDef::field_iterator MessageDef::field_begin() { |
| 2800 | return field_iterator(this); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2801 | } |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2802 | inline MessageDef::field_iterator MessageDef::field_end() { |
| 2803 | return field_iterator::end(this); |
| 2804 | } |
| 2805 | inline MessageDef::const_field_iterator MessageDef::field_begin() const { |
| 2806 | return const_field_iterator(this); |
| 2807 | } |
| 2808 | inline MessageDef::const_field_iterator MessageDef::field_end() const { |
| 2809 | return const_field_iterator::end(this); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2810 | } |
| 2811 | |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2812 | inline MessageDef::oneof_iterator MessageDef::oneof_begin() { |
| 2813 | return oneof_iterator(this); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2814 | } |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2815 | inline MessageDef::oneof_iterator MessageDef::oneof_end() { |
| 2816 | return oneof_iterator::end(this); |
| 2817 | } |
| 2818 | inline MessageDef::const_oneof_iterator MessageDef::oneof_begin() const { |
| 2819 | return const_oneof_iterator(this); |
| 2820 | } |
| 2821 | inline MessageDef::const_oneof_iterator MessageDef::oneof_end() const { |
| 2822 | return const_oneof_iterator::end(this); |
| 2823 | } |
| 2824 | |
| 2825 | inline MessageDef::field_iterator::field_iterator(MessageDef* md) { |
| 2826 | upb_msg_field_begin(&iter_, md); |
| 2827 | } |
| 2828 | inline MessageDef::field_iterator MessageDef::field_iterator::end( |
| 2829 | MessageDef* md) { |
| 2830 | MessageDef::field_iterator iter(md); |
| 2831 | upb_msg_field_iter_setdone(&iter.iter_); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2832 | return iter; |
| 2833 | } |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2834 | inline FieldDef* MessageDef::field_iterator::operator*() const { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2835 | return upb_msg_iter_field(&iter_); |
| 2836 | } |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2837 | inline void MessageDef::field_iterator::operator++() { |
| 2838 | return upb_msg_field_next(&iter_); |
| 2839 | } |
| 2840 | inline bool MessageDef::field_iterator::operator==( |
| 2841 | const field_iterator &other) const { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2842 | return upb_inttable_iter_isequal(&iter_, &other.iter_); |
| 2843 | } |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2844 | inline bool MessageDef::field_iterator::operator!=( |
| 2845 | const field_iterator &other) const { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2846 | return !(*this == other); |
| 2847 | } |
| 2848 | |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2849 | inline MessageDef::const_field_iterator::const_field_iterator( |
| 2850 | const MessageDef* md) { |
| 2851 | upb_msg_field_begin(&iter_, md); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2852 | } |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2853 | inline MessageDef::const_field_iterator MessageDef::const_field_iterator::end( |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2854 | const MessageDef *md) { |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2855 | MessageDef::const_field_iterator iter(md); |
| 2856 | upb_msg_field_iter_setdone(&iter.iter_); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2857 | return iter; |
| 2858 | } |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2859 | inline const FieldDef* MessageDef::const_field_iterator::operator*() const { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2860 | return upb_msg_iter_field(&iter_); |
| 2861 | } |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2862 | inline void MessageDef::const_field_iterator::operator++() { |
| 2863 | return upb_msg_field_next(&iter_); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2864 | } |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2865 | inline bool MessageDef::const_field_iterator::operator==( |
| 2866 | const const_field_iterator &other) const { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2867 | return upb_inttable_iter_isequal(&iter_, &other.iter_); |
| 2868 | } |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2869 | inline bool MessageDef::const_field_iterator::operator!=( |
| 2870 | const const_field_iterator &other) const { |
| 2871 | return !(*this == other); |
| 2872 | } |
| 2873 | |
| 2874 | inline MessageDef::oneof_iterator::oneof_iterator(MessageDef* md) { |
| 2875 | upb_msg_oneof_begin(&iter_, md); |
| 2876 | } |
| 2877 | inline MessageDef::oneof_iterator MessageDef::oneof_iterator::end( |
| 2878 | MessageDef* md) { |
| 2879 | MessageDef::oneof_iterator iter(md); |
| 2880 | upb_msg_oneof_iter_setdone(&iter.iter_); |
| 2881 | return iter; |
| 2882 | } |
| 2883 | inline OneofDef* MessageDef::oneof_iterator::operator*() const { |
| 2884 | return upb_msg_iter_oneof(&iter_); |
| 2885 | } |
| 2886 | inline void MessageDef::oneof_iterator::operator++() { |
| 2887 | return upb_msg_oneof_next(&iter_); |
| 2888 | } |
| 2889 | inline bool MessageDef::oneof_iterator::operator==( |
| 2890 | const oneof_iterator &other) const { |
| 2891 | return upb_strtable_iter_isequal(&iter_, &other.iter_); |
| 2892 | } |
| 2893 | inline bool MessageDef::oneof_iterator::operator!=( |
| 2894 | const oneof_iterator &other) const { |
| 2895 | return !(*this == other); |
| 2896 | } |
| 2897 | |
| 2898 | inline MessageDef::const_oneof_iterator::const_oneof_iterator( |
| 2899 | const MessageDef* md) { |
| 2900 | upb_msg_oneof_begin(&iter_, md); |
| 2901 | } |
| 2902 | inline MessageDef::const_oneof_iterator MessageDef::const_oneof_iterator::end( |
| 2903 | const MessageDef *md) { |
| 2904 | MessageDef::const_oneof_iterator iter(md); |
| 2905 | upb_msg_oneof_iter_setdone(&iter.iter_); |
| 2906 | return iter; |
| 2907 | } |
| 2908 | inline const OneofDef* MessageDef::const_oneof_iterator::operator*() const { |
| 2909 | return upb_msg_iter_oneof(&iter_); |
| 2910 | } |
| 2911 | inline void MessageDef::const_oneof_iterator::operator++() { |
| 2912 | return upb_msg_oneof_next(&iter_); |
| 2913 | } |
| 2914 | inline bool MessageDef::const_oneof_iterator::operator==( |
| 2915 | const const_oneof_iterator &other) const { |
| 2916 | return upb_strtable_iter_isequal(&iter_, &other.iter_); |
| 2917 | } |
| 2918 | inline bool MessageDef::const_oneof_iterator::operator!=( |
| 2919 | const const_oneof_iterator &other) const { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2920 | return !(*this == other); |
| 2921 | } |
| 2922 | |
| 2923 | inline reffed_ptr<EnumDef> EnumDef::New() { |
| 2924 | upb_enumdef *e = upb_enumdef_new(&e); |
| 2925 | return reffed_ptr<EnumDef>(e, &e); |
| 2926 | } |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 2927 | inline const char* EnumDef::full_name() const { |
| 2928 | return upb_enumdef_fullname(this); |
| 2929 | } |
| 2930 | inline bool EnumDef::set_full_name(const char* fullname, Status* s) { |
| 2931 | return upb_enumdef_setfullname(this, fullname, s); |
| 2932 | } |
| 2933 | inline bool EnumDef::set_full_name(const std::string& fullname, Status* s) { |
| 2934 | return upb_enumdef_setfullname(this, upb_safecstr(fullname), s); |
| 2935 | } |
| 2936 | inline bool EnumDef::Freeze(Status* status) { |
| 2937 | return upb_enumdef_freeze(this, status); |
| 2938 | } |
| 2939 | inline int32_t EnumDef::default_value() const { |
| 2940 | return upb_enumdef_default(this); |
| 2941 | } |
| 2942 | inline bool EnumDef::set_default_value(int32_t val, Status* status) { |
| 2943 | return upb_enumdef_setdefault(this, val, status); |
| 2944 | } |
| 2945 | inline int EnumDef::value_count() const { return upb_enumdef_numvals(this); } |
| 2946 | inline bool EnumDef::AddValue(const char* name, int32_t num, Status* status) { |
| 2947 | return upb_enumdef_addval(this, name, num, status); |
| 2948 | } |
| 2949 | inline bool EnumDef::AddValue(const std::string& name, int32_t num, |
| 2950 | Status* status) { |
| 2951 | return upb_enumdef_addval(this, upb_safecstr(name), num, status); |
| 2952 | } |
| 2953 | inline bool EnumDef::FindValueByName(const char* name, int32_t *num) const { |
| 2954 | return upb_enumdef_ntoiz(this, name, num); |
| 2955 | } |
| 2956 | inline const char* EnumDef::FindValueByNumber(int32_t num) const { |
| 2957 | return upb_enumdef_iton(this, num); |
| 2958 | } |
| 2959 | inline EnumDef* EnumDef::Dup(const void* owner) const { |
| 2960 | return upb_enumdef_dup(this, owner); |
| 2961 | } |
| 2962 | |
| 2963 | inline EnumDef::Iterator::Iterator(const EnumDef* e) { |
| 2964 | upb_enum_begin(&iter_, e); |
| 2965 | } |
| 2966 | inline int32_t EnumDef::Iterator::number() { |
| 2967 | return upb_enum_iter_number(&iter_); |
| 2968 | } |
| 2969 | inline const char* EnumDef::Iterator::name() { |
| 2970 | return upb_enum_iter_name(&iter_); |
| 2971 | } |
| 2972 | inline bool EnumDef::Iterator::Done() { return upb_enum_done(&iter_); } |
| 2973 | inline void EnumDef::Iterator::Next() { return upb_enum_next(&iter_); } |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2974 | |
| 2975 | inline reffed_ptr<OneofDef> OneofDef::New() { |
| 2976 | upb_oneofdef *o = upb_oneofdef_new(&o); |
| 2977 | return reffed_ptr<OneofDef>(o, &o); |
| 2978 | } |
Chris Fallin | fcd8889 | 2015-01-13 18:14:39 -0800 | [diff] [blame] | 2979 | inline const char* OneofDef::full_name() const { |
| 2980 | return upb_oneofdef_name(this); |
| 2981 | } |
| 2982 | |
| 2983 | inline const MessageDef* OneofDef::containing_type() const { |
| 2984 | return upb_oneofdef_containingtype(this); |
| 2985 | } |
| 2986 | inline const char* OneofDef::name() const { |
| 2987 | return upb_oneofdef_name(this); |
| 2988 | } |
| 2989 | inline bool OneofDef::set_name(const char* name, Status* s) { |
| 2990 | return upb_oneofdef_setname(this, name, s); |
| 2991 | } |
| 2992 | inline int OneofDef::field_count() const { |
| 2993 | return upb_oneofdef_numfields(this); |
| 2994 | } |
| 2995 | inline bool OneofDef::AddField(FieldDef* field, Status* s) { |
| 2996 | return upb_oneofdef_addfield(this, field, NULL, s); |
| 2997 | } |
| 2998 | inline bool OneofDef::AddField(const reffed_ptr<FieldDef>& field, Status* s) { |
| 2999 | return upb_oneofdef_addfield(this, field.get(), NULL, s); |
| 3000 | } |
| 3001 | inline const FieldDef* OneofDef::FindFieldByName(const char* name, |
| 3002 | size_t len) const { |
| 3003 | return upb_oneofdef_ntof(this, name, len); |
| 3004 | } |
| 3005 | inline const FieldDef* OneofDef::FindFieldByNumber(uint32_t num) const { |
| 3006 | return upb_oneofdef_itof(this, num); |
| 3007 | } |
| 3008 | inline OneofDef::iterator OneofDef::begin() { return iterator(this); } |
| 3009 | inline OneofDef::iterator OneofDef::end() { return iterator::end(this); } |
| 3010 | inline OneofDef::const_iterator OneofDef::begin() const { |
| 3011 | return const_iterator(this); |
| 3012 | } |
| 3013 | inline OneofDef::const_iterator OneofDef::end() const { |
| 3014 | return const_iterator::end(this); |
| 3015 | } |
| 3016 | |
| 3017 | inline OneofDef::iterator::iterator(OneofDef* o) { |
| 3018 | upb_oneof_begin(&iter_, o); |
| 3019 | } |
| 3020 | inline OneofDef::iterator OneofDef::iterator::end(OneofDef* o) { |
| 3021 | OneofDef::iterator iter(o); |
| 3022 | upb_oneof_iter_setdone(&iter.iter_); |
| 3023 | return iter; |
| 3024 | } |
| 3025 | inline FieldDef* OneofDef::iterator::operator*() const { |
| 3026 | return upb_oneof_iter_field(&iter_); |
| 3027 | } |
| 3028 | inline void OneofDef::iterator::operator++() { return upb_oneof_next(&iter_); } |
| 3029 | inline bool OneofDef::iterator::operator==(const iterator &other) const { |
| 3030 | return upb_inttable_iter_isequal(&iter_, &other.iter_); |
| 3031 | } |
| 3032 | inline bool OneofDef::iterator::operator!=(const iterator &other) const { |
| 3033 | return !(*this == other); |
| 3034 | } |
| 3035 | |
| 3036 | inline OneofDef::const_iterator::const_iterator(const OneofDef* md) { |
| 3037 | upb_oneof_begin(&iter_, md); |
| 3038 | } |
| 3039 | inline OneofDef::const_iterator OneofDef::const_iterator::end( |
| 3040 | const OneofDef *md) { |
| 3041 | OneofDef::const_iterator iter(md); |
| 3042 | upb_oneof_iter_setdone(&iter.iter_); |
| 3043 | return iter; |
| 3044 | } |
| 3045 | inline const FieldDef* OneofDef::const_iterator::operator*() const { |
| 3046 | return upb_msg_iter_field(&iter_); |
| 3047 | } |
| 3048 | inline void OneofDef::const_iterator::operator++() { |
| 3049 | return upb_oneof_next(&iter_); |
| 3050 | } |
| 3051 | inline bool OneofDef::const_iterator::operator==( |
| 3052 | const const_iterator &other) const { |
| 3053 | return upb_inttable_iter_isequal(&iter_, &other.iter_); |
| 3054 | } |
| 3055 | inline bool OneofDef::const_iterator::operator!=( |
| 3056 | const const_iterator &other) const { |
| 3057 | return !(*this == other); |
| 3058 | } |
| 3059 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3060 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3061 | #endif |
| 3062 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3063 | #endif /* UPB_DEF_H_ */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3064 | /* |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 3065 | ** This file contains definitions of structs that should be considered private |
| 3066 | ** and NOT stable across versions of upb. |
| 3067 | ** |
| 3068 | ** The only reason they are declared here and not in .c files is to allow upb |
| 3069 | ** and the application (if desired) to embed statically-initialized instances |
| 3070 | ** of structures like defs. |
| 3071 | ** |
| 3072 | ** If you include this file, all guarantees of ABI compatibility go out the |
| 3073 | ** window! Any code that includes this file needs to recompile against the |
| 3074 | ** exact same version of upb that they are linking against. |
| 3075 | ** |
| 3076 | ** You also need to recompile if you change the value of the UPB_DEBUG_REFS |
| 3077 | ** flag. |
| 3078 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3079 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3080 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3081 | #ifndef UPB_STATICINIT_H_ |
| 3082 | #define UPB_STATICINIT_H_ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3083 | |
| 3084 | #ifdef __cplusplus |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3085 | /* Because of how we do our typedefs, this header can't be included from C++. */ |
| 3086 | #error This file cannot be included from C++ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3087 | #endif |
| 3088 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3089 | /* upb_refcounted *************************************************************/ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3090 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3091 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3092 | /* upb_def ********************************************************************/ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3093 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3094 | struct upb_def { |
| 3095 | upb_refcounted base; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3096 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3097 | const char *fullname; |
| 3098 | char type; /* A upb_deftype_t (char to save space) */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3099 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3100 | /* Used as a flag during the def's mutable stage. Must be false unless |
| 3101 | * it is currently being used by a function on the stack. This allows |
| 3102 | * us to easily determine which defs were passed into the function's |
| 3103 | * current invocation. */ |
| 3104 | bool came_from_user; |
| 3105 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3106 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3107 | #define UPB_DEF_INIT(name, type, refs, ref2s) \ |
| 3108 | { UPB_REFCOUNT_INIT(refs, ref2s), name, type, false } |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3109 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3110 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3111 | /* upb_fielddef ***************************************************************/ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3112 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3113 | struct upb_fielddef { |
| 3114 | upb_def base; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3115 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3116 | union { |
| 3117 | int64_t sint; |
| 3118 | uint64_t uint; |
| 3119 | double dbl; |
| 3120 | float flt; |
| 3121 | void *bytes; |
| 3122 | } defaultval; |
| 3123 | union { |
| 3124 | const upb_msgdef *def; /* If !msg_is_symbolic. */ |
| 3125 | char *name; /* If msg_is_symbolic. */ |
| 3126 | } msg; |
| 3127 | union { |
| 3128 | const upb_def *def; /* If !subdef_is_symbolic. */ |
| 3129 | char *name; /* If subdef_is_symbolic. */ |
| 3130 | } sub; /* The msgdef or enumdef for this field, if upb_hassubdef(f). */ |
| 3131 | bool subdef_is_symbolic; |
| 3132 | bool msg_is_symbolic; |
| 3133 | const upb_oneofdef *oneof; |
| 3134 | bool default_is_string; |
| 3135 | bool type_is_set_; /* False until type is explicitly set. */ |
| 3136 | bool is_extension_; |
| 3137 | bool lazy_; |
| 3138 | bool packed_; |
| 3139 | upb_intfmt_t intfmt; |
| 3140 | bool tagdelim; |
| 3141 | upb_fieldtype_t type_; |
| 3142 | upb_label_t label_; |
| 3143 | uint32_t number_; |
| 3144 | uint32_t selector_base; /* Used to index into a upb::Handlers table. */ |
| 3145 | uint32_t index_; |
| 3146 | }; |
| 3147 | |
| 3148 | #define UPB_FIELDDEF_INIT(label, type, intfmt, tagdelim, is_extension, lazy, \ |
| 3149 | packed, name, num, msgdef, subdef, selector_base, \ |
| 3150 | index, defaultval, refs, ref2s) \ |
| 3151 | { \ |
| 3152 | UPB_DEF_INIT(name, UPB_DEF_FIELD, refs, ref2s), defaultval, {msgdef}, \ |
| 3153 | {subdef}, NULL, false, false, \ |
| 3154 | type == UPB_TYPE_STRING || type == UPB_TYPE_BYTES, true, is_extension, \ |
| 3155 | lazy, packed, intfmt, tagdelim, type, label, num, selector_base, index \ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3156 | } |
| 3157 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3158 | |
| 3159 | /* upb_msgdef *****************************************************************/ |
| 3160 | |
| 3161 | struct upb_msgdef { |
| 3162 | upb_def base; |
| 3163 | |
| 3164 | size_t selector_count; |
| 3165 | uint32_t submsg_field_count; |
| 3166 | |
| 3167 | /* Tables for looking up fields by number and name. */ |
| 3168 | upb_inttable itof; /* int to field */ |
| 3169 | upb_strtable ntof; /* name to field */ |
| 3170 | |
| 3171 | /* Tables for looking up oneofs by name. */ |
| 3172 | upb_strtable ntoo; /* name to oneof */ |
| 3173 | |
| 3174 | /* Is this a map-entry message? |
| 3175 | * TODO: set this flag properly for static descriptors; regenerate |
| 3176 | * descriptor.upb.c. */ |
| 3177 | bool map_entry; |
| 3178 | |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 3179 | /* Do primitive values in this message have explicit presence or not? |
| 3180 | * TODO: set this flag properly for static descriptors; regenerate |
| 3181 | * descriptor.upb.c. */ |
| 3182 | bool primitives_have_presence; |
| 3183 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3184 | /* TODO(haberman): proper extension ranges (there can be multiple). */ |
| 3185 | }; |
| 3186 | |
| 3187 | /* TODO: also support static initialization of the oneofs table. This will be |
| 3188 | * needed if we compile in descriptors that contain oneofs. */ |
| 3189 | #define UPB_MSGDEF_INIT(name, selector_count, submsg_field_count, itof, ntof, \ |
| 3190 | refs, ref2s) \ |
| 3191 | { \ |
| 3192 | UPB_DEF_INIT(name, UPB_DEF_MSG, refs, ref2s), selector_count, \ |
| 3193 | submsg_field_count, itof, ntof, \ |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 3194 | UPB_EMPTY_STRTABLE_INIT(UPB_CTYPE_PTR), false, true \ |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3195 | } |
| 3196 | |
| 3197 | |
| 3198 | /* upb_enumdef ****************************************************************/ |
| 3199 | |
| 3200 | struct upb_enumdef { |
| 3201 | upb_def base; |
| 3202 | |
| 3203 | upb_strtable ntoi; |
| 3204 | upb_inttable iton; |
| 3205 | int32_t defaultval; |
| 3206 | }; |
| 3207 | |
| 3208 | #define UPB_ENUMDEF_INIT(name, ntoi, iton, defaultval, refs, ref2s) \ |
| 3209 | { UPB_DEF_INIT(name, UPB_DEF_ENUM, refs, ref2s), ntoi, iton, defaultval } |
| 3210 | |
| 3211 | |
| 3212 | /* upb_oneofdef ***************************************************************/ |
| 3213 | |
| 3214 | struct upb_oneofdef { |
| 3215 | upb_def base; |
| 3216 | |
| 3217 | upb_strtable ntof; |
| 3218 | upb_inttable itof; |
| 3219 | const upb_msgdef *parent; |
| 3220 | }; |
| 3221 | |
| 3222 | #define UPB_ONEOFDEF_INIT(name, ntof, itof, refs, ref2s) \ |
| 3223 | { UPB_DEF_INIT(name, UPB_DEF_ENUM, refs, ref2s), ntof, itof } |
| 3224 | |
| 3225 | |
| 3226 | /* upb_symtab *****************************************************************/ |
| 3227 | |
| 3228 | struct upb_symtab { |
| 3229 | upb_refcounted base; |
| 3230 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3231 | upb_strtable symtab; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3232 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3233 | |
| 3234 | #define UPB_SYMTAB_INIT(symtab, refs, ref2s) \ |
| 3235 | { UPB_REFCOUNT_INIT(refs, ref2s), symtab } |
| 3236 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3237 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3238 | #endif /* UPB_STATICINIT_H_ */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3239 | /* |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 3240 | ** upb::Handlers (upb_handlers) |
| 3241 | ** |
| 3242 | ** A upb_handlers is like a virtual table for a upb_msgdef. Each field of the |
| 3243 | ** message can have associated functions that will be called when we are |
| 3244 | ** parsing or visiting a stream of data. This is similar to how handlers work |
| 3245 | ** in SAX (the Simple API for XML). |
| 3246 | ** |
| 3247 | ** The handlers have no idea where the data is coming from, so a single set of |
| 3248 | ** handlers could be used with two completely different data sources (for |
| 3249 | ** example, a parser and a visitor over in-memory objects). This decoupling is |
| 3250 | ** the most important feature of upb, because it allows parsers and serializers |
| 3251 | ** to be highly reusable. |
| 3252 | ** |
| 3253 | ** This is a mixed C/C++ interface that offers a full API to both languages. |
| 3254 | ** See the top-level README for more information. |
| 3255 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3256 | |
| 3257 | #ifndef UPB_HANDLERS_H |
| 3258 | #define UPB_HANDLERS_H |
| 3259 | |
| 3260 | |
| 3261 | #ifdef __cplusplus |
| 3262 | namespace upb { |
| 3263 | class BufferHandle; |
| 3264 | class BytesHandler; |
| 3265 | class HandlerAttributes; |
| 3266 | class Handlers; |
| 3267 | template <class T> class Handler; |
| 3268 | template <class T> struct CanonicalType; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3269 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3270 | #endif |
| 3271 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3272 | UPB_DECLARE_TYPE(upb::BufferHandle, upb_bufhandle) |
| 3273 | UPB_DECLARE_TYPE(upb::BytesHandler, upb_byteshandler) |
| 3274 | UPB_DECLARE_TYPE(upb::HandlerAttributes, upb_handlerattr) |
| 3275 | UPB_DECLARE_DERIVED_TYPE(upb::Handlers, upb::RefCounted, |
| 3276 | upb_handlers, upb_refcounted) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3277 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3278 | /* The maximum depth that the handler graph can have. This is a resource limit |
| 3279 | * for the C stack since we sometimes need to recursively traverse the graph. |
| 3280 | * Cycles are ok; the traversal will stop when it detects a cycle, but we must |
| 3281 | * hit the cycle before the maximum depth is reached. |
| 3282 | * |
| 3283 | * If having a single static limit is too inflexible, we can add another variant |
| 3284 | * of Handlers::Freeze that allows specifying this as a parameter. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3285 | #define UPB_MAX_HANDLER_DEPTH 64 |
| 3286 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3287 | /* All the different types of handlers that can be registered. |
| 3288 | * Only needed for the advanced functions in upb::Handlers. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3289 | typedef enum { |
| 3290 | UPB_HANDLER_INT32, |
| 3291 | UPB_HANDLER_INT64, |
| 3292 | UPB_HANDLER_UINT32, |
| 3293 | UPB_HANDLER_UINT64, |
| 3294 | UPB_HANDLER_FLOAT, |
| 3295 | UPB_HANDLER_DOUBLE, |
| 3296 | UPB_HANDLER_BOOL, |
| 3297 | UPB_HANDLER_STARTSTR, |
| 3298 | UPB_HANDLER_STRING, |
| 3299 | UPB_HANDLER_ENDSTR, |
| 3300 | UPB_HANDLER_STARTSUBMSG, |
| 3301 | UPB_HANDLER_ENDSUBMSG, |
| 3302 | UPB_HANDLER_STARTSEQ, |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3303 | UPB_HANDLER_ENDSEQ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3304 | } upb_handlertype_t; |
| 3305 | |
| 3306 | #define UPB_HANDLER_MAX (UPB_HANDLER_ENDSEQ+1) |
| 3307 | |
| 3308 | #define UPB_BREAK NULL |
| 3309 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3310 | /* A convenient definition for when no closure is needed. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3311 | extern char _upb_noclosure; |
| 3312 | #define UPB_NO_CLOSURE &_upb_noclosure |
| 3313 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3314 | /* A selector refers to a specific field handler in the Handlers object |
| 3315 | * (for example: the STARTSUBMSG handler for field "field15"). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3316 | typedef int32_t upb_selector_t; |
| 3317 | |
| 3318 | UPB_BEGIN_EXTERN_C |
| 3319 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3320 | /* Forward-declares for C inline accessors. We need to declare these here |
| 3321 | * so we can "friend" them in the class declarations in C++. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3322 | UPB_INLINE upb_func *upb_handlers_gethandler(const upb_handlers *h, |
| 3323 | upb_selector_t s); |
| 3324 | UPB_INLINE const void *upb_handlerattr_handlerdata(const upb_handlerattr *attr); |
| 3325 | UPB_INLINE const void *upb_handlers_gethandlerdata(const upb_handlers *h, |
| 3326 | upb_selector_t s); |
| 3327 | |
| 3328 | UPB_INLINE void upb_bufhandle_init(upb_bufhandle *h); |
| 3329 | UPB_INLINE void upb_bufhandle_setobj(upb_bufhandle *h, const void *obj, |
| 3330 | const void *type); |
| 3331 | UPB_INLINE void upb_bufhandle_setbuf(upb_bufhandle *h, const char *buf, |
| 3332 | size_t ofs); |
| 3333 | UPB_INLINE const void *upb_bufhandle_obj(const upb_bufhandle *h); |
| 3334 | UPB_INLINE const void *upb_bufhandle_objtype(const upb_bufhandle *h); |
| 3335 | UPB_INLINE const char *upb_bufhandle_buf(const upb_bufhandle *h); |
| 3336 | |
| 3337 | UPB_END_EXTERN_C |
| 3338 | |
| 3339 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3340 | /* Static selectors for upb::Handlers. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3341 | #define UPB_STARTMSG_SELECTOR 0 |
| 3342 | #define UPB_ENDMSG_SELECTOR 1 |
| 3343 | #define UPB_STATIC_SELECTOR_COUNT 2 |
| 3344 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3345 | /* Static selectors for upb::BytesHandler. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3346 | #define UPB_STARTSTR_SELECTOR 0 |
| 3347 | #define UPB_STRING_SELECTOR 1 |
| 3348 | #define UPB_ENDSTR_SELECTOR 2 |
| 3349 | |
| 3350 | typedef void upb_handlerfree(void *d); |
| 3351 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3352 | #ifdef __cplusplus |
| 3353 | |
| 3354 | /* A set of attributes that accompanies a handler's function pointer. */ |
| 3355 | class upb::HandlerAttributes { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3356 | public: |
| 3357 | HandlerAttributes(); |
| 3358 | ~HandlerAttributes(); |
| 3359 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3360 | /* Sets the handler data that will be passed as the second parameter of the |
| 3361 | * handler. To free this pointer when the handlers are freed, call |
| 3362 | * Handlers::AddCleanup(). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3363 | bool SetHandlerData(const void *handler_data); |
| 3364 | const void* handler_data() const; |
| 3365 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3366 | /* Use this to specify the type of the closure. This will be checked against |
| 3367 | * all other closure types for handler that use the same closure. |
| 3368 | * Registration will fail if this does not match all other non-NULL closure |
| 3369 | * types. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3370 | bool SetClosureType(const void *closure_type); |
| 3371 | const void* closure_type() const; |
| 3372 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3373 | /* Use this to specify the type of the returned closure. Only used for |
| 3374 | * Start*{String,SubMessage,Sequence} handlers. This must match the closure |
| 3375 | * type of any handlers that use it (for example, the StringBuf handler must |
| 3376 | * match the closure returned from StartString). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3377 | bool SetReturnClosureType(const void *return_closure_type); |
| 3378 | const void* return_closure_type() const; |
| 3379 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3380 | /* Set to indicate that the handler always returns "ok" (either "true" or a |
| 3381 | * non-NULL closure). This is a hint that can allow code generators to |
| 3382 | * generate more efficient code. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3383 | bool SetAlwaysOk(bool always_ok); |
| 3384 | bool always_ok() const; |
| 3385 | |
| 3386 | private: |
| 3387 | friend UPB_INLINE const void * ::upb_handlerattr_handlerdata( |
| 3388 | const upb_handlerattr *attr); |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3389 | #else |
| 3390 | struct upb_handlerattr { |
| 3391 | #endif |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3392 | const void *handler_data_; |
| 3393 | const void *closure_type_; |
| 3394 | const void *return_closure_type_; |
| 3395 | bool alwaysok_; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3396 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3397 | |
| 3398 | #define UPB_HANDLERATTR_INITIALIZER {NULL, NULL, NULL, false} |
| 3399 | |
| 3400 | typedef struct { |
| 3401 | upb_func *func; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3402 | |
| 3403 | /* It is wasteful to include the entire attributes here: |
| 3404 | * |
| 3405 | * * Some of the information is redundant (like storing the closure type |
| 3406 | * separately for each handler that must match). |
| 3407 | * * Some of the info is only needed prior to freeze() (like closure types). |
| 3408 | * * alignment padding wastes a lot of space for alwaysok_. |
| 3409 | * |
| 3410 | * If/when the size and locality of handlers is an issue, we can optimize this |
| 3411 | * not to store the entire attr like this. We do not expose the table's |
| 3412 | * layout to allow this optimization in the future. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3413 | upb_handlerattr attr; |
| 3414 | } upb_handlers_tabent; |
| 3415 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3416 | #ifdef __cplusplus |
| 3417 | |
| 3418 | /* Extra information about a buffer that is passed to a StringBuf handler. |
| 3419 | * TODO(haberman): allow the handle to be pinned so that it will outlive |
| 3420 | * the handler invocation. */ |
| 3421 | class upb::BufferHandle { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3422 | public: |
| 3423 | BufferHandle(); |
| 3424 | ~BufferHandle(); |
| 3425 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3426 | /* The beginning of the buffer. This may be different than the pointer |
| 3427 | * passed to a StringBuf handler because the handler may receive data |
| 3428 | * that is from the middle or end of a larger buffer. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3429 | const char* buffer() const; |
| 3430 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3431 | /* The offset within the attached object where this buffer begins. Only |
| 3432 | * meaningful if there is an attached object. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3433 | size_t object_offset() const; |
| 3434 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3435 | /* Note that object_offset is the offset of "buf" within the attached |
| 3436 | * object. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3437 | void SetBuffer(const char* buf, size_t object_offset); |
| 3438 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3439 | /* The BufferHandle can have an "attached object", which can be used to |
| 3440 | * tunnel through a pointer to the buffer's underlying representation. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3441 | template <class T> |
| 3442 | void SetAttachedObject(const T* obj); |
| 3443 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3444 | /* Returns NULL if the attached object is not of this type. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3445 | template <class T> |
| 3446 | const T* GetAttachedObject() const; |
| 3447 | |
| 3448 | private: |
| 3449 | friend UPB_INLINE void ::upb_bufhandle_init(upb_bufhandle *h); |
| 3450 | friend UPB_INLINE void ::upb_bufhandle_setobj(upb_bufhandle *h, |
| 3451 | const void *obj, |
| 3452 | const void *type); |
| 3453 | friend UPB_INLINE void ::upb_bufhandle_setbuf(upb_bufhandle *h, |
| 3454 | const char *buf, size_t ofs); |
| 3455 | friend UPB_INLINE const void* ::upb_bufhandle_obj(const upb_bufhandle *h); |
| 3456 | friend UPB_INLINE const void* ::upb_bufhandle_objtype( |
| 3457 | const upb_bufhandle *h); |
| 3458 | friend UPB_INLINE const char* ::upb_bufhandle_buf(const upb_bufhandle *h); |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3459 | #else |
| 3460 | struct upb_bufhandle { |
| 3461 | #endif |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3462 | const char *buf_; |
| 3463 | const void *obj_; |
| 3464 | const void *objtype_; |
| 3465 | size_t objofs_; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3466 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3467 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3468 | #ifdef __cplusplus |
| 3469 | |
| 3470 | /* A upb::Handlers object represents the set of handlers associated with a |
| 3471 | * message in the graph of messages. You can think of it as a big virtual |
| 3472 | * table with functions corresponding to all the events that can fire while |
| 3473 | * parsing or visiting a message of a specific type. |
| 3474 | * |
| 3475 | * Any handlers that are not set behave as if they had successfully consumed |
| 3476 | * the value. Any unset Start* handlers will propagate their closure to the |
| 3477 | * inner frame. |
| 3478 | * |
| 3479 | * The easiest way to create the *Handler objects needed by the Set* methods is |
| 3480 | * with the UpbBind() and UpbMakeHandler() macros; see below. */ |
| 3481 | class upb::Handlers { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3482 | public: |
| 3483 | typedef upb_selector_t Selector; |
| 3484 | typedef upb_handlertype_t Type; |
| 3485 | |
| 3486 | typedef Handler<void *(*)(void *, const void *)> StartFieldHandler; |
| 3487 | typedef Handler<bool (*)(void *, const void *)> EndFieldHandler; |
| 3488 | typedef Handler<bool (*)(void *, const void *)> StartMessageHandler; |
| 3489 | typedef Handler<bool (*)(void *, const void *, Status*)> EndMessageHandler; |
| 3490 | typedef Handler<void *(*)(void *, const void *, size_t)> StartStringHandler; |
| 3491 | typedef Handler<size_t (*)(void *, const void *, const char *, size_t, |
| 3492 | const BufferHandle *)> StringHandler; |
| 3493 | |
| 3494 | template <class T> struct ValueHandler { |
| 3495 | typedef Handler<bool(*)(void *, const void *, T)> H; |
| 3496 | }; |
| 3497 | |
| 3498 | typedef ValueHandler<int32_t>::H Int32Handler; |
| 3499 | typedef ValueHandler<int64_t>::H Int64Handler; |
| 3500 | typedef ValueHandler<uint32_t>::H UInt32Handler; |
| 3501 | typedef ValueHandler<uint64_t>::H UInt64Handler; |
| 3502 | typedef ValueHandler<float>::H FloatHandler; |
| 3503 | typedef ValueHandler<double>::H DoubleHandler; |
| 3504 | typedef ValueHandler<bool>::H BoolHandler; |
| 3505 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3506 | /* Any function pointer can be converted to this and converted back to its |
| 3507 | * correct type. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3508 | typedef void GenericFunction(); |
| 3509 | |
| 3510 | typedef void HandlersCallback(const void *closure, upb_handlers *h); |
| 3511 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3512 | /* Returns a new handlers object for the given frozen msgdef. |
| 3513 | * Returns NULL if memory allocation failed. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3514 | static reffed_ptr<Handlers> New(const MessageDef *m); |
| 3515 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3516 | /* Convenience function for registering a graph of handlers that mirrors the |
| 3517 | * graph of msgdefs for some message. For "m" and all its children a new set |
| 3518 | * of handlers will be created and the given callback will be invoked, |
| 3519 | * allowing the client to register handlers for this message. Note that any |
| 3520 | * subhandlers set by the callback will be overwritten. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3521 | static reffed_ptr<const Handlers> NewFrozen(const MessageDef *m, |
| 3522 | HandlersCallback *callback, |
| 3523 | const void *closure); |
| 3524 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3525 | /* Functionality from upb::RefCounted. */ |
| 3526 | UPB_REFCOUNTED_CPPMETHODS |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3527 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3528 | /* All handler registration functions return bool to indicate success or |
| 3529 | * failure; details about failures are stored in this status object. If a |
| 3530 | * failure does occur, it must be cleared before the Handlers are frozen, |
| 3531 | * otherwise the freeze() operation will fail. The functions may *only* be |
| 3532 | * used while the Handlers are mutable. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3533 | const Status* status(); |
| 3534 | void ClearError(); |
| 3535 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3536 | /* Call to freeze these Handlers. Requires that any SubHandlers are already |
| 3537 | * frozen. For cycles, you must use the static version below and freeze the |
| 3538 | * whole graph at once. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3539 | bool Freeze(Status* s); |
| 3540 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3541 | /* Freezes the given set of handlers. You may not freeze a handler without |
| 3542 | * also freezing any handlers they point to. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3543 | static bool Freeze(Handlers*const* handlers, int n, Status* s); |
| 3544 | static bool Freeze(const std::vector<Handlers*>& handlers, Status* s); |
| 3545 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3546 | /* Returns the msgdef associated with this handlers object. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3547 | const MessageDef* message_def() const; |
| 3548 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3549 | /* Adds the given pointer and function to the list of cleanup functions that |
| 3550 | * will be run when these handlers are freed. If this pointer has previously |
| 3551 | * been registered, the function returns false and does nothing. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3552 | bool AddCleanup(void *ptr, upb_handlerfree *cleanup); |
| 3553 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3554 | /* Sets the startmsg handler for the message, which is defined as follows: |
| 3555 | * |
| 3556 | * bool startmsg(MyType* closure) { |
| 3557 | * // Called when the message begins. Returns true if processing should |
| 3558 | * // continue. |
| 3559 | * return true; |
| 3560 | * } |
| 3561 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3562 | bool SetStartMessageHandler(const StartMessageHandler& handler); |
| 3563 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3564 | /* Sets the endmsg handler for the message, which is defined as follows: |
| 3565 | * |
| 3566 | * bool endmsg(MyType* closure, upb_status *status) { |
| 3567 | * // Called when processing of this message ends, whether in success or |
| 3568 | * // failure. "status" indicates the final status of processing, and |
| 3569 | * // can also be modified in-place to update the final status. |
| 3570 | * } |
| 3571 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3572 | bool SetEndMessageHandler(const EndMessageHandler& handler); |
| 3573 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3574 | /* Sets the value handler for the given field, which is defined as follows |
| 3575 | * (this is for an int32 field; other field types will pass their native |
| 3576 | * C/C++ type for "val"): |
| 3577 | * |
| 3578 | * bool OnValue(MyClosure* c, const MyHandlerData* d, int32_t val) { |
| 3579 | * // Called when the field's value is encountered. "d" contains |
| 3580 | * // whatever data was bound to this field when it was registered. |
| 3581 | * // Returns true if processing should continue. |
| 3582 | * return true; |
| 3583 | * } |
| 3584 | * |
| 3585 | * handers->SetInt32Handler(f, UpbBind(OnValue, new MyHandlerData(...))); |
| 3586 | * |
| 3587 | * The value type must exactly match f->type(). |
| 3588 | * For example, a handler that takes an int32_t parameter may only be used for |
| 3589 | * fields of type UPB_TYPE_INT32 and UPB_TYPE_ENUM. |
| 3590 | * |
| 3591 | * Returns false if the handler failed to register; in this case the cleanup |
| 3592 | * handler (if any) will be called immediately. |
| 3593 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3594 | bool SetInt32Handler (const FieldDef* f, const Int32Handler& h); |
| 3595 | bool SetInt64Handler (const FieldDef* f, const Int64Handler& h); |
| 3596 | bool SetUInt32Handler(const FieldDef* f, const UInt32Handler& h); |
| 3597 | bool SetUInt64Handler(const FieldDef* f, const UInt64Handler& h); |
| 3598 | bool SetFloatHandler (const FieldDef* f, const FloatHandler& h); |
| 3599 | bool SetDoubleHandler(const FieldDef* f, const DoubleHandler& h); |
| 3600 | bool SetBoolHandler (const FieldDef* f, const BoolHandler& h); |
| 3601 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3602 | /* Like the previous, but templated on the type on the value (ie. int32). |
| 3603 | * This is mostly useful to call from other templates. To call this you must |
| 3604 | * specify the template parameter explicitly, ie: |
| 3605 | * h->SetValueHandler<T>(f, UpbBind(MyHandler<T>, MyData)); */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3606 | template <class T> |
| 3607 | bool SetValueHandler( |
| 3608 | const FieldDef *f, |
| 3609 | const typename ValueHandler<typename CanonicalType<T>::Type>::H& handler); |
| 3610 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3611 | /* Sets handlers for a string field, which are defined as follows: |
| 3612 | * |
| 3613 | * MySubClosure* startstr(MyClosure* c, const MyHandlerData* d, |
| 3614 | * size_t size_hint) { |
| 3615 | * // Called when a string value begins. The return value indicates the |
| 3616 | * // closure for the string. "size_hint" indicates the size of the |
| 3617 | * // string if it is known, however if the string is length-delimited |
| 3618 | * // and the end-of-string is not available size_hint will be zero. |
| 3619 | * // This case is indistinguishable from the case where the size is |
| 3620 | * // known to be zero. |
| 3621 | * // |
| 3622 | * // TODO(haberman): is it important to distinguish these cases? |
| 3623 | * // If we had ssize_t as a type we could make -1 "unknown", but |
| 3624 | * // ssize_t is POSIX (not ANSI) and therefore less portable. |
| 3625 | * // In practice I suspect it won't be important to distinguish. |
| 3626 | * return closure; |
| 3627 | * } |
| 3628 | * |
| 3629 | * size_t str(MyClosure* closure, const MyHandlerData* d, |
| 3630 | * const char *str, size_t len) { |
| 3631 | * // Called for each buffer of string data; the multiple physical buffers |
| 3632 | * // are all part of the same logical string. The return value indicates |
| 3633 | * // how many bytes were consumed. If this number is less than "len", |
| 3634 | * // this will also indicate that processing should be halted for now, |
| 3635 | * // like returning false or UPB_BREAK from any other callback. If |
| 3636 | * // number is greater than "len", the excess bytes will be skipped over |
| 3637 | * // and not passed to the callback. |
| 3638 | * return len; |
| 3639 | * } |
| 3640 | * |
| 3641 | * bool endstr(MyClosure* c, const MyHandlerData* d) { |
| 3642 | * // Called when a string value ends. Return value indicates whether |
| 3643 | * // processing should continue. |
| 3644 | * return true; |
| 3645 | * } |
| 3646 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3647 | bool SetStartStringHandler(const FieldDef* f, const StartStringHandler& h); |
| 3648 | bool SetStringHandler(const FieldDef* f, const StringHandler& h); |
| 3649 | bool SetEndStringHandler(const FieldDef* f, const EndFieldHandler& h); |
| 3650 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3651 | /* Sets the startseq handler, which is defined as follows: |
| 3652 | * |
| 3653 | * MySubClosure *startseq(MyClosure* c, const MyHandlerData* d) { |
| 3654 | * // Called when a sequence (repeated field) begins. The returned |
| 3655 | * // pointer indicates the closure for the sequence (or UPB_BREAK |
| 3656 | * // to interrupt processing). |
| 3657 | * return closure; |
| 3658 | * } |
| 3659 | * |
| 3660 | * h->SetStartSequenceHandler(f, UpbBind(startseq, new MyHandlerData(...))); |
| 3661 | * |
| 3662 | * Returns "false" if "f" does not belong to this message or is not a |
| 3663 | * repeated field. |
| 3664 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3665 | bool SetStartSequenceHandler(const FieldDef* f, const StartFieldHandler& h); |
| 3666 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3667 | /* Sets the startsubmsg handler for the given field, which is defined as |
| 3668 | * follows: |
| 3669 | * |
| 3670 | * MySubClosure* startsubmsg(MyClosure* c, const MyHandlerData* d) { |
| 3671 | * // Called when a submessage begins. The returned pointer indicates the |
| 3672 | * // closure for the sequence (or UPB_BREAK to interrupt processing). |
| 3673 | * return closure; |
| 3674 | * } |
| 3675 | * |
| 3676 | * h->SetStartSubMessageHandler(f, UpbBind(startsubmsg, |
| 3677 | * new MyHandlerData(...))); |
| 3678 | * |
| 3679 | * Returns "false" if "f" does not belong to this message or is not a |
| 3680 | * submessage/group field. |
| 3681 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3682 | bool SetStartSubMessageHandler(const FieldDef* f, const StartFieldHandler& h); |
| 3683 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3684 | /* Sets the endsubmsg handler for the given field, which is defined as |
| 3685 | * follows: |
| 3686 | * |
| 3687 | * bool endsubmsg(MyClosure* c, const MyHandlerData* d) { |
| 3688 | * // Called when a submessage ends. Returns true to continue processing. |
| 3689 | * return true; |
| 3690 | * } |
| 3691 | * |
| 3692 | * Returns "false" if "f" does not belong to this message or is not a |
| 3693 | * submessage/group field. |
| 3694 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3695 | bool SetEndSubMessageHandler(const FieldDef *f, const EndFieldHandler &h); |
| 3696 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3697 | /* Starts the endsubseq handler for the given field, which is defined as |
| 3698 | * follows: |
| 3699 | * |
| 3700 | * bool endseq(MyClosure* c, const MyHandlerData* d) { |
| 3701 | * // Called when a sequence ends. Returns true continue processing. |
| 3702 | * return true; |
| 3703 | * } |
| 3704 | * |
| 3705 | * Returns "false" if "f" does not belong to this message or is not a |
| 3706 | * repeated field. |
| 3707 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3708 | bool SetEndSequenceHandler(const FieldDef* f, const EndFieldHandler& h); |
| 3709 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3710 | /* Sets or gets the object that specifies handlers for the given field, which |
| 3711 | * must be a submessage or group. Returns NULL if no handlers are set. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3712 | bool SetSubHandlers(const FieldDef* f, const Handlers* sub); |
| 3713 | const Handlers* GetSubHandlers(const FieldDef* f) const; |
| 3714 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3715 | /* Equivalent to GetSubHandlers, but takes the STARTSUBMSG selector for the |
| 3716 | * field. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3717 | const Handlers* GetSubHandlers(Selector startsubmsg) const; |
| 3718 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3719 | /* A selector refers to a specific field handler in the Handlers object |
| 3720 | * (for example: the STARTSUBMSG handler for field "field15"). |
| 3721 | * On success, returns true and stores the selector in "s". |
| 3722 | * If the FieldDef or Type are invalid, returns false. |
| 3723 | * The returned selector is ONLY valid for Handlers whose MessageDef |
| 3724 | * contains this FieldDef. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3725 | static bool GetSelector(const FieldDef* f, Type type, Selector* s); |
| 3726 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3727 | /* Given a START selector of any kind, returns the corresponding END selector. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3728 | static Selector GetEndSelector(Selector start_selector); |
| 3729 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3730 | /* Returns the function pointer for this handler. It is the client's |
| 3731 | * responsibility to cast to the correct function type before calling it. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3732 | GenericFunction* GetHandler(Selector selector); |
| 3733 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3734 | /* Sets the given attributes to the attributes for this selector. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3735 | bool GetAttributes(Selector selector, HandlerAttributes* attr); |
| 3736 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3737 | /* Returns the handler data that was registered with this handler. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3738 | const void* GetHandlerData(Selector selector); |
| 3739 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3740 | /* Could add any of the following functions as-needed, with some minor |
| 3741 | * implementation changes: |
| 3742 | * |
| 3743 | * const FieldDef* GetFieldDef(Selector selector); |
| 3744 | * static bool IsSequence(Selector selector); */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3745 | |
| 3746 | private: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3747 | UPB_DISALLOW_POD_OPS(Handlers, upb::Handlers) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3748 | |
| 3749 | friend UPB_INLINE GenericFunction *::upb_handlers_gethandler( |
| 3750 | const upb_handlers *h, upb_selector_t s); |
| 3751 | friend UPB_INLINE const void *::upb_handlers_gethandlerdata( |
| 3752 | const upb_handlers *h, upb_selector_t s); |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3753 | #else |
| 3754 | struct upb_handlers { |
| 3755 | #endif |
| 3756 | upb_refcounted base; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3757 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3758 | const upb_msgdef *msg; |
| 3759 | const upb_handlers **sub; |
| 3760 | const void *top_closure_type; |
| 3761 | upb_inttable cleanup_; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3762 | upb_status status_; /* Used only when mutable. */ |
| 3763 | upb_handlers_tabent table[1]; /* Dynamically-sized field handler array. */ |
| 3764 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3765 | |
| 3766 | #ifdef __cplusplus |
| 3767 | |
| 3768 | namespace upb { |
| 3769 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3770 | /* Convenience macros for creating a Handler object that is wrapped with a |
| 3771 | * type-safe wrapper function that converts the "void*" parameters/returns |
| 3772 | * of the underlying C API into nice C++ function. |
| 3773 | * |
| 3774 | * Sample usage: |
| 3775 | * void OnValue1(MyClosure* c, const MyHandlerData* d, int32_t val) { |
| 3776 | * // do stuff ... |
| 3777 | * } |
| 3778 | * |
| 3779 | * // Handler that doesn't need any data bound to it. |
| 3780 | * void OnValue2(MyClosure* c, int32_t val) { |
| 3781 | * // do stuff ... |
| 3782 | * } |
| 3783 | * |
| 3784 | * // Handler that returns bool so it can return failure if necessary. |
| 3785 | * bool OnValue3(MyClosure* c, int32_t val) { |
| 3786 | * // do stuff ... |
| 3787 | * return ok; |
| 3788 | * } |
| 3789 | * |
| 3790 | * // Member function handler. |
| 3791 | * class MyClosure { |
| 3792 | * public: |
| 3793 | * void OnValue(int32_t val) { |
| 3794 | * // do stuff ... |
| 3795 | * } |
| 3796 | * }; |
| 3797 | * |
| 3798 | * // Takes ownership of the MyHandlerData. |
| 3799 | * handlers->SetInt32Handler(f1, UpbBind(OnValue1, new MyHandlerData(...))); |
| 3800 | * handlers->SetInt32Handler(f2, UpbMakeHandler(OnValue2)); |
| 3801 | * handlers->SetInt32Handler(f1, UpbMakeHandler(OnValue3)); |
| 3802 | * handlers->SetInt32Handler(f2, UpbMakeHandler(&MyClosure::OnValue)); |
| 3803 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3804 | |
| 3805 | #ifdef UPB_CXX11 |
| 3806 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3807 | /* In C++11, the "template" disambiguator can appear even outside templates, |
| 3808 | * so all calls can safely use this pair of macros. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3809 | |
| 3810 | #define UpbMakeHandler(f) upb::MatchFunc(f).template GetFunc<f>() |
| 3811 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3812 | /* We have to be careful to only evaluate "d" once. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3813 | #define UpbBind(f, d) upb::MatchFunc(f).template GetFunc<f>((d)) |
| 3814 | |
| 3815 | #else |
| 3816 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3817 | /* Prior to C++11, the "template" disambiguator may only appear inside a |
| 3818 | * template, so the regular macro must not use "template" */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3819 | |
| 3820 | #define UpbMakeHandler(f) upb::MatchFunc(f).GetFunc<f>() |
| 3821 | |
| 3822 | #define UpbBind(f, d) upb::MatchFunc(f).GetFunc<f>((d)) |
| 3823 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3824 | #endif /* UPB_CXX11 */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3825 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3826 | /* This macro must be used in C++98 for calls from inside a template. But we |
| 3827 | * define this variant in all cases; code that wants to be compatible with both |
| 3828 | * C++98 and C++11 should always use this macro when calling from a template. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3829 | #define UpbMakeHandlerT(f) upb::MatchFunc(f).template GetFunc<f>() |
| 3830 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3831 | /* We have to be careful to only evaluate "d" once. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3832 | #define UpbBindT(f, d) upb::MatchFunc(f).template GetFunc<f>((d)) |
| 3833 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3834 | /* Handler: a struct that contains the (handler, data, deleter) tuple that is |
| 3835 | * used to register all handlers. Users can Make() these directly but it's |
| 3836 | * more convenient to use the UpbMakeHandler/UpbBind macros above. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3837 | template <class T> class Handler { |
| 3838 | public: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3839 | /* The underlying, handler function signature that upb uses internally. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3840 | typedef T FuncPtr; |
| 3841 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3842 | /* Intentionally implicit. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3843 | template <class F> Handler(F func); |
| 3844 | ~Handler(); |
| 3845 | |
| 3846 | private: |
| 3847 | void AddCleanup(Handlers* h) const { |
| 3848 | if (cleanup_func_) { |
| 3849 | bool ok = h->AddCleanup(cleanup_data_, cleanup_func_); |
| 3850 | UPB_ASSERT_VAR(ok, ok); |
| 3851 | } |
| 3852 | } |
| 3853 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3854 | UPB_DISALLOW_COPY_AND_ASSIGN(Handler) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3855 | friend class Handlers; |
| 3856 | FuncPtr handler_; |
| 3857 | mutable HandlerAttributes attr_; |
| 3858 | mutable bool registered_; |
| 3859 | void *cleanup_data_; |
| 3860 | upb_handlerfree *cleanup_func_; |
| 3861 | }; |
| 3862 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3863 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3864 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3865 | #endif /* __cplusplus */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3866 | |
| 3867 | UPB_BEGIN_EXTERN_C |
| 3868 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3869 | /* Native C API. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3870 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3871 | /* Handler function typedefs. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3872 | typedef bool upb_startmsg_handlerfunc(void *c, const void*); |
| 3873 | typedef bool upb_endmsg_handlerfunc(void *c, const void *, upb_status *status); |
| 3874 | typedef void* upb_startfield_handlerfunc(void *c, const void *hd); |
| 3875 | typedef bool upb_endfield_handlerfunc(void *c, const void *hd); |
| 3876 | typedef bool upb_int32_handlerfunc(void *c, const void *hd, int32_t val); |
| 3877 | typedef bool upb_int64_handlerfunc(void *c, const void *hd, int64_t val); |
| 3878 | typedef bool upb_uint32_handlerfunc(void *c, const void *hd, uint32_t val); |
| 3879 | typedef bool upb_uint64_handlerfunc(void *c, const void *hd, uint64_t val); |
| 3880 | typedef bool upb_float_handlerfunc(void *c, const void *hd, float val); |
| 3881 | typedef bool upb_double_handlerfunc(void *c, const void *hd, double val); |
| 3882 | typedef bool upb_bool_handlerfunc(void *c, const void *hd, bool val); |
| 3883 | typedef void *upb_startstr_handlerfunc(void *c, const void *hd, |
| 3884 | size_t size_hint); |
| 3885 | typedef size_t upb_string_handlerfunc(void *c, const void *hd, const char *buf, |
| 3886 | size_t n, const upb_bufhandle* handle); |
| 3887 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3888 | /* upb_bufhandle */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3889 | size_t upb_bufhandle_objofs(const upb_bufhandle *h); |
| 3890 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3891 | /* upb_handlerattr */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3892 | void upb_handlerattr_init(upb_handlerattr *attr); |
| 3893 | void upb_handlerattr_uninit(upb_handlerattr *attr); |
| 3894 | |
| 3895 | bool upb_handlerattr_sethandlerdata(upb_handlerattr *attr, const void *hd); |
| 3896 | bool upb_handlerattr_setclosuretype(upb_handlerattr *attr, const void *type); |
| 3897 | const void *upb_handlerattr_closuretype(const upb_handlerattr *attr); |
| 3898 | bool upb_handlerattr_setreturnclosuretype(upb_handlerattr *attr, |
| 3899 | const void *type); |
| 3900 | const void *upb_handlerattr_returnclosuretype(const upb_handlerattr *attr); |
| 3901 | bool upb_handlerattr_setalwaysok(upb_handlerattr *attr, bool alwaysok); |
| 3902 | bool upb_handlerattr_alwaysok(const upb_handlerattr *attr); |
| 3903 | |
| 3904 | UPB_INLINE const void *upb_handlerattr_handlerdata( |
| 3905 | const upb_handlerattr *attr) { |
| 3906 | return attr->handler_data_; |
| 3907 | } |
| 3908 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3909 | /* upb_handlers */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3910 | typedef void upb_handlers_callback(const void *closure, upb_handlers *h); |
| 3911 | upb_handlers *upb_handlers_new(const upb_msgdef *m, |
| 3912 | const void *owner); |
| 3913 | const upb_handlers *upb_handlers_newfrozen(const upb_msgdef *m, |
| 3914 | const void *owner, |
| 3915 | upb_handlers_callback *callback, |
| 3916 | const void *closure); |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3917 | |
| 3918 | /* Include refcounted methods like upb_handlers_ref(). */ |
| 3919 | UPB_REFCOUNTED_CMETHODS(upb_handlers, upb_handlers_upcast) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3920 | |
| 3921 | const upb_status *upb_handlers_status(upb_handlers *h); |
| 3922 | void upb_handlers_clearerr(upb_handlers *h); |
| 3923 | const upb_msgdef *upb_handlers_msgdef(const upb_handlers *h); |
| 3924 | bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *hfree); |
| 3925 | |
| 3926 | bool upb_handlers_setstartmsg(upb_handlers *h, upb_startmsg_handlerfunc *func, |
| 3927 | upb_handlerattr *attr); |
| 3928 | bool upb_handlers_setendmsg(upb_handlers *h, upb_endmsg_handlerfunc *func, |
| 3929 | upb_handlerattr *attr); |
| 3930 | bool upb_handlers_setint32(upb_handlers *h, const upb_fielddef *f, |
| 3931 | upb_int32_handlerfunc *func, upb_handlerattr *attr); |
| 3932 | bool upb_handlers_setint64(upb_handlers *h, const upb_fielddef *f, |
| 3933 | upb_int64_handlerfunc *func, upb_handlerattr *attr); |
| 3934 | bool upb_handlers_setuint32(upb_handlers *h, const upb_fielddef *f, |
| 3935 | upb_uint32_handlerfunc *func, |
| 3936 | upb_handlerattr *attr); |
| 3937 | bool upb_handlers_setuint64(upb_handlers *h, const upb_fielddef *f, |
| 3938 | upb_uint64_handlerfunc *func, |
| 3939 | upb_handlerattr *attr); |
| 3940 | bool upb_handlers_setfloat(upb_handlers *h, const upb_fielddef *f, |
| 3941 | upb_float_handlerfunc *func, upb_handlerattr *attr); |
| 3942 | bool upb_handlers_setdouble(upb_handlers *h, const upb_fielddef *f, |
| 3943 | upb_double_handlerfunc *func, |
| 3944 | upb_handlerattr *attr); |
| 3945 | bool upb_handlers_setbool(upb_handlers *h, const upb_fielddef *f, |
| 3946 | upb_bool_handlerfunc *func, |
| 3947 | upb_handlerattr *attr); |
| 3948 | bool upb_handlers_setstartstr(upb_handlers *h, const upb_fielddef *f, |
| 3949 | upb_startstr_handlerfunc *func, |
| 3950 | upb_handlerattr *attr); |
| 3951 | bool upb_handlers_setstring(upb_handlers *h, const upb_fielddef *f, |
| 3952 | upb_string_handlerfunc *func, |
| 3953 | upb_handlerattr *attr); |
| 3954 | bool upb_handlers_setendstr(upb_handlers *h, const upb_fielddef *f, |
| 3955 | upb_endfield_handlerfunc *func, |
| 3956 | upb_handlerattr *attr); |
| 3957 | bool upb_handlers_setstartseq(upb_handlers *h, const upb_fielddef *f, |
| 3958 | upb_startfield_handlerfunc *func, |
| 3959 | upb_handlerattr *attr); |
| 3960 | bool upb_handlers_setstartsubmsg(upb_handlers *h, const upb_fielddef *f, |
| 3961 | upb_startfield_handlerfunc *func, |
| 3962 | upb_handlerattr *attr); |
| 3963 | bool upb_handlers_setendsubmsg(upb_handlers *h, const upb_fielddef *f, |
| 3964 | upb_endfield_handlerfunc *func, |
| 3965 | upb_handlerattr *attr); |
| 3966 | bool upb_handlers_setendseq(upb_handlers *h, const upb_fielddef *f, |
| 3967 | upb_endfield_handlerfunc *func, |
| 3968 | upb_handlerattr *attr); |
| 3969 | |
| 3970 | bool upb_handlers_setsubhandlers(upb_handlers *h, const upb_fielddef *f, |
| 3971 | const upb_handlers *sub); |
| 3972 | const upb_handlers *upb_handlers_getsubhandlers(const upb_handlers *h, |
| 3973 | const upb_fielddef *f); |
| 3974 | const upb_handlers *upb_handlers_getsubhandlers_sel(const upb_handlers *h, |
| 3975 | upb_selector_t sel); |
| 3976 | |
| 3977 | UPB_INLINE upb_func *upb_handlers_gethandler(const upb_handlers *h, |
| 3978 | upb_selector_t s) { |
| 3979 | return (upb_func *)h->table[s].func; |
| 3980 | } |
| 3981 | |
| 3982 | bool upb_handlers_getattr(const upb_handlers *h, upb_selector_t s, |
| 3983 | upb_handlerattr *attr); |
| 3984 | |
| 3985 | UPB_INLINE const void *upb_handlers_gethandlerdata(const upb_handlers *h, |
| 3986 | upb_selector_t s) { |
| 3987 | return upb_handlerattr_handlerdata(&h->table[s].attr); |
| 3988 | } |
| 3989 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 3990 | #ifdef __cplusplus |
| 3991 | |
| 3992 | /* Handler types for single fields. |
| 3993 | * Right now we only have one for TYPE_BYTES but ones for other types |
| 3994 | * should follow. |
| 3995 | * |
| 3996 | * These follow the same handlers protocol for fields of a message. */ |
| 3997 | class upb::BytesHandler { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 3998 | public: |
| 3999 | BytesHandler(); |
| 4000 | ~BytesHandler(); |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4001 | #else |
| 4002 | struct upb_byteshandler { |
| 4003 | #endif |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4004 | upb_handlers_tabent table[3]; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4005 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4006 | |
| 4007 | void upb_byteshandler_init(upb_byteshandler *h); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4008 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4009 | /* Caller must ensure that "d" outlives the handlers. |
| 4010 | * TODO(haberman): should this have a "freeze" operation? It's not necessary |
| 4011 | * for memory management, but could be useful to force immutability and provide |
| 4012 | * a convenient moment to verify that all registration succeeded. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4013 | bool upb_byteshandler_setstartstr(upb_byteshandler *h, |
| 4014 | upb_startstr_handlerfunc *func, void *d); |
| 4015 | bool upb_byteshandler_setstring(upb_byteshandler *h, |
| 4016 | upb_string_handlerfunc *func, void *d); |
| 4017 | bool upb_byteshandler_setendstr(upb_byteshandler *h, |
| 4018 | upb_endfield_handlerfunc *func, void *d); |
| 4019 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4020 | /* "Static" methods */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4021 | bool upb_handlers_freeze(upb_handlers *const *handlers, int n, upb_status *s); |
| 4022 | upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f); |
| 4023 | bool upb_handlers_getselector(const upb_fielddef *f, upb_handlertype_t type, |
| 4024 | upb_selector_t *s); |
| 4025 | UPB_INLINE upb_selector_t upb_handlers_getendselector(upb_selector_t start) { |
| 4026 | return start + 1; |
| 4027 | } |
| 4028 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4029 | /* Internal-only. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4030 | uint32_t upb_handlers_selectorbaseoffset(const upb_fielddef *f); |
| 4031 | uint32_t upb_handlers_selectorcount(const upb_fielddef *f); |
| 4032 | |
| 4033 | UPB_END_EXTERN_C |
| 4034 | |
| 4035 | /* |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 4036 | ** Inline definitions for handlers.h, which are particularly long and a bit |
| 4037 | ** tricky. |
| 4038 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4039 | |
| 4040 | #ifndef UPB_HANDLERS_INL_H_ |
| 4041 | #define UPB_HANDLERS_INL_H_ |
| 4042 | |
| 4043 | #include <limits.h> |
| 4044 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4045 | /* C inline methods. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4046 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4047 | /* upb_bufhandle */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4048 | UPB_INLINE void upb_bufhandle_init(upb_bufhandle *h) { |
| 4049 | h->obj_ = NULL; |
| 4050 | h->objtype_ = NULL; |
| 4051 | h->buf_ = NULL; |
| 4052 | h->objofs_ = 0; |
| 4053 | } |
| 4054 | UPB_INLINE void upb_bufhandle_uninit(upb_bufhandle *h) { |
| 4055 | UPB_UNUSED(h); |
| 4056 | } |
| 4057 | UPB_INLINE void upb_bufhandle_setobj(upb_bufhandle *h, const void *obj, |
| 4058 | const void *type) { |
| 4059 | h->obj_ = obj; |
| 4060 | h->objtype_ = type; |
| 4061 | } |
| 4062 | UPB_INLINE void upb_bufhandle_setbuf(upb_bufhandle *h, const char *buf, |
| 4063 | size_t ofs) { |
| 4064 | h->buf_ = buf; |
| 4065 | h->objofs_ = ofs; |
| 4066 | } |
| 4067 | UPB_INLINE const void *upb_bufhandle_obj(const upb_bufhandle *h) { |
| 4068 | return h->obj_; |
| 4069 | } |
| 4070 | UPB_INLINE const void *upb_bufhandle_objtype(const upb_bufhandle *h) { |
| 4071 | return h->objtype_; |
| 4072 | } |
| 4073 | UPB_INLINE const char *upb_bufhandle_buf(const upb_bufhandle *h) { |
| 4074 | return h->buf_; |
| 4075 | } |
| 4076 | |
| 4077 | |
| 4078 | #ifdef __cplusplus |
| 4079 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4080 | /* Type detection and typedefs for integer types. |
| 4081 | * For platforms where there are multiple 32-bit or 64-bit types, we need to be |
| 4082 | * able to enumerate them so we can properly create overloads for all variants. |
| 4083 | * |
| 4084 | * If any platform existed where there were three integer types with the same |
| 4085 | * size, this would have to become more complicated. For example, short, int, |
| 4086 | * and long could all be 32-bits. Even more diabolically, short, int, long, |
| 4087 | * and long long could all be 64 bits and still be standard-compliant. |
| 4088 | * However, few platforms are this strange, and it's unlikely that upb will be |
| 4089 | * used on the strangest ones. */ |
| 4090 | |
| 4091 | /* Can't count on stdint.h limits like INT32_MAX, because in C++ these are |
| 4092 | * only defined when __STDC_LIMIT_MACROS are defined before the *first* include |
| 4093 | * of stdint.h. We can't guarantee that someone else didn't include these first |
| 4094 | * without defining __STDC_LIMIT_MACROS. */ |
| 4095 | #define UPB_INT32_MAX 0x7fffffffLL |
| 4096 | #define UPB_INT32_MIN (-UPB_INT32_MAX - 1) |
| 4097 | #define UPB_INT64_MAX 0x7fffffffffffffffLL |
| 4098 | #define UPB_INT64_MIN (-UPB_INT64_MAX - 1) |
| 4099 | |
| 4100 | #if INT_MAX == UPB_INT32_MAX && INT_MIN == UPB_INT32_MIN |
| 4101 | #define UPB_INT_IS_32BITS 1 |
| 4102 | #endif |
| 4103 | |
| 4104 | #if LONG_MAX == UPB_INT32_MAX && LONG_MIN == UPB_INT32_MIN |
| 4105 | #define UPB_LONG_IS_32BITS 1 |
| 4106 | #endif |
| 4107 | |
| 4108 | #if LONG_MAX == UPB_INT64_MAX && LONG_MIN == UPB_INT64_MIN |
| 4109 | #define UPB_LONG_IS_64BITS 1 |
| 4110 | #endif |
| 4111 | |
| 4112 | #if LLONG_MAX == UPB_INT64_MAX && LLONG_MIN == UPB_INT64_MIN |
| 4113 | #define UPB_LLONG_IS_64BITS 1 |
| 4114 | #endif |
| 4115 | |
| 4116 | /* We use macros instead of typedefs so we can undefine them later and avoid |
| 4117 | * leaking them outside this header file. */ |
| 4118 | #if UPB_INT_IS_32BITS |
| 4119 | #define UPB_INT32_T int |
| 4120 | #define UPB_UINT32_T unsigned int |
| 4121 | |
| 4122 | #if UPB_LONG_IS_32BITS |
| 4123 | #define UPB_TWO_32BIT_TYPES 1 |
| 4124 | #define UPB_INT32ALT_T long |
| 4125 | #define UPB_UINT32ALT_T unsigned long |
| 4126 | #endif /* UPB_LONG_IS_32BITS */ |
| 4127 | |
| 4128 | #elif UPB_LONG_IS_32BITS /* && !UPB_INT_IS_32BITS */ |
| 4129 | #define UPB_INT32_T long |
| 4130 | #define UPB_UINT32_T unsigned long |
| 4131 | #endif /* UPB_INT_IS_32BITS */ |
| 4132 | |
| 4133 | |
| 4134 | #if UPB_LONG_IS_64BITS |
| 4135 | #define UPB_INT64_T long |
| 4136 | #define UPB_UINT64_T unsigned long |
| 4137 | |
| 4138 | #if UPB_LLONG_IS_64BITS |
| 4139 | #define UPB_TWO_64BIT_TYPES 1 |
| 4140 | #define UPB_INT64ALT_T long long |
| 4141 | #define UPB_UINT64ALT_T unsigned long long |
| 4142 | #endif /* UPB_LLONG_IS_64BITS */ |
| 4143 | |
| 4144 | #elif UPB_LLONG_IS_64BITS /* && !UPB_LONG_IS_64BITS */ |
| 4145 | #define UPB_INT64_T long long |
| 4146 | #define UPB_UINT64_T unsigned long long |
| 4147 | #endif /* UPB_LONG_IS_64BITS */ |
| 4148 | |
| 4149 | #undef UPB_INT32_MAX |
| 4150 | #undef UPB_INT32_MIN |
| 4151 | #undef UPB_INT64_MAX |
| 4152 | #undef UPB_INT64_MIN |
| 4153 | #undef UPB_INT_IS_32BITS |
| 4154 | #undef UPB_LONG_IS_32BITS |
| 4155 | #undef UPB_LONG_IS_64BITS |
| 4156 | #undef UPB_LLONG_IS_64BITS |
| 4157 | |
| 4158 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4159 | namespace upb { |
| 4160 | |
| 4161 | typedef void CleanupFunc(void *ptr); |
| 4162 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4163 | /* Template to remove "const" from "const T*" and just return "T*". |
| 4164 | * |
| 4165 | * We define a nonsense default because otherwise it will fail to instantiate as |
| 4166 | * a function parameter type even in cases where we don't expect any caller to |
| 4167 | * actually match the overload. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4168 | class CouldntRemoveConst {}; |
| 4169 | template <class T> struct remove_constptr { typedef CouldntRemoveConst type; }; |
| 4170 | template <class T> struct remove_constptr<const T *> { typedef T *type; }; |
| 4171 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4172 | /* Template that we use below to remove a template specialization from |
| 4173 | * consideration if it matches a specific type. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4174 | template <class T, class U> struct disable_if_same { typedef void Type; }; |
| 4175 | template <class T> struct disable_if_same<T, T> {}; |
| 4176 | |
| 4177 | template <class T> void DeletePointer(void *p) { delete static_cast<T>(p); } |
| 4178 | |
| 4179 | template <class T1, class T2> |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 4180 | struct FirstUnlessVoidOrBool { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4181 | typedef T1 value; |
| 4182 | }; |
| 4183 | |
| 4184 | template <class T2> |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 4185 | struct FirstUnlessVoidOrBool<void, T2> { |
| 4186 | typedef T2 value; |
| 4187 | }; |
| 4188 | |
| 4189 | template <class T2> |
| 4190 | struct FirstUnlessVoidOrBool<bool, T2> { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4191 | typedef T2 value; |
| 4192 | }; |
| 4193 | |
| 4194 | template<class T, class U> |
| 4195 | struct is_same { |
| 4196 | static bool value; |
| 4197 | }; |
| 4198 | |
| 4199 | template<class T> |
| 4200 | struct is_same<T, T> { |
| 4201 | static bool value; |
| 4202 | }; |
| 4203 | |
| 4204 | template<class T, class U> |
| 4205 | bool is_same<T, U>::value = false; |
| 4206 | |
| 4207 | template<class T> |
| 4208 | bool is_same<T, T>::value = true; |
| 4209 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4210 | /* FuncInfo *******************************************************************/ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4211 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4212 | /* Info about the user's original, pre-wrapped function. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4213 | template <class C, class R = void> |
| 4214 | struct FuncInfo { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4215 | /* The type of the closure that the function takes (its first param). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4216 | typedef C Closure; |
| 4217 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4218 | /* The return type. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4219 | typedef R Return; |
| 4220 | }; |
| 4221 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4222 | /* Func ***********************************************************************/ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4223 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4224 | /* Func1, Func2, Func3: Template classes representing a function and its |
| 4225 | * signature. |
| 4226 | * |
| 4227 | * Since the function is a template parameter, calling the function can be |
| 4228 | * inlined at compile-time and does not require a function pointer at runtime. |
| 4229 | * These functions are not bound to a handler data so have no data or cleanup |
| 4230 | * handler. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4231 | struct UnboundFunc { |
| 4232 | CleanupFunc *GetCleanup() { return NULL; } |
| 4233 | void *GetData() { return NULL; } |
| 4234 | }; |
| 4235 | |
| 4236 | template <class R, class P1, R F(P1), class I> |
| 4237 | struct Func1 : public UnboundFunc { |
| 4238 | typedef R Return; |
| 4239 | typedef I FuncInfo; |
| 4240 | static R Call(P1 p1) { return F(p1); } |
| 4241 | }; |
| 4242 | |
| 4243 | template <class R, class P1, class P2, R F(P1, P2), class I> |
| 4244 | struct Func2 : public UnboundFunc { |
| 4245 | typedef R Return; |
| 4246 | typedef I FuncInfo; |
| 4247 | static R Call(P1 p1, P2 p2) { return F(p1, p2); } |
| 4248 | }; |
| 4249 | |
| 4250 | template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I> |
| 4251 | struct Func3 : public UnboundFunc { |
| 4252 | typedef R Return; |
| 4253 | typedef I FuncInfo; |
| 4254 | static R Call(P1 p1, P2 p2, P3 p3) { return F(p1, p2, p3); } |
| 4255 | }; |
| 4256 | |
| 4257 | template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4), |
| 4258 | class I> |
| 4259 | struct Func4 : public UnboundFunc { |
| 4260 | typedef R Return; |
| 4261 | typedef I FuncInfo; |
| 4262 | static R Call(P1 p1, P2 p2, P3 p3, P4 p4) { return F(p1, p2, p3, p4); } |
| 4263 | }; |
| 4264 | |
| 4265 | template <class R, class P1, class P2, class P3, class P4, class P5, |
| 4266 | R F(P1, P2, P3, P4, P5), class I> |
| 4267 | struct Func5 : public UnboundFunc { |
| 4268 | typedef R Return; |
| 4269 | typedef I FuncInfo; |
| 4270 | static R Call(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) { |
| 4271 | return F(p1, p2, p3, p4, p5); |
| 4272 | } |
| 4273 | }; |
| 4274 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4275 | /* BoundFunc ******************************************************************/ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4276 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4277 | /* BoundFunc2, BoundFunc3: Like Func2/Func3 except also contains a value that |
| 4278 | * shall be bound to the function's second parameter. |
| 4279 | * |
| 4280 | * Note that the second parameter is a const pointer, but our stored bound value |
| 4281 | * is non-const so we can free it when the handlers are destroyed. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4282 | template <class T> |
| 4283 | struct BoundFunc { |
| 4284 | typedef typename remove_constptr<T>::type MutableP2; |
| 4285 | explicit BoundFunc(MutableP2 data_) : data(data_) {} |
| 4286 | CleanupFunc *GetCleanup() { return &DeletePointer<MutableP2>; } |
| 4287 | MutableP2 GetData() { return data; } |
| 4288 | MutableP2 data; |
| 4289 | }; |
| 4290 | |
| 4291 | template <class R, class P1, class P2, R F(P1, P2), class I> |
| 4292 | struct BoundFunc2 : public BoundFunc<P2> { |
| 4293 | typedef BoundFunc<P2> Base; |
| 4294 | typedef I FuncInfo; |
| 4295 | explicit BoundFunc2(typename Base::MutableP2 arg) : Base(arg) {} |
| 4296 | }; |
| 4297 | |
| 4298 | template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I> |
| 4299 | struct BoundFunc3 : public BoundFunc<P2> { |
| 4300 | typedef BoundFunc<P2> Base; |
| 4301 | typedef I FuncInfo; |
| 4302 | explicit BoundFunc3(typename Base::MutableP2 arg) : Base(arg) {} |
| 4303 | }; |
| 4304 | |
| 4305 | template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4), |
| 4306 | class I> |
| 4307 | struct BoundFunc4 : public BoundFunc<P2> { |
| 4308 | typedef BoundFunc<P2> Base; |
| 4309 | typedef I FuncInfo; |
| 4310 | explicit BoundFunc4(typename Base::MutableP2 arg) : Base(arg) {} |
| 4311 | }; |
| 4312 | |
| 4313 | template <class R, class P1, class P2, class P3, class P4, class P5, |
| 4314 | R F(P1, P2, P3, P4, P5), class I> |
| 4315 | struct BoundFunc5 : public BoundFunc<P2> { |
| 4316 | typedef BoundFunc<P2> Base; |
| 4317 | typedef I FuncInfo; |
| 4318 | explicit BoundFunc5(typename Base::MutableP2 arg) : Base(arg) {} |
| 4319 | }; |
| 4320 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4321 | /* FuncSig ********************************************************************/ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4322 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4323 | /* FuncSig1, FuncSig2, FuncSig3: template classes reflecting a function |
| 4324 | * *signature*, but without a specific function attached. |
| 4325 | * |
| 4326 | * These classes contain member functions that can be invoked with a |
| 4327 | * specific function to return a Func/BoundFunc class. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4328 | template <class R, class P1> |
| 4329 | struct FuncSig1 { |
| 4330 | template <R F(P1)> |
| 4331 | Func1<R, P1, F, FuncInfo<P1, R> > GetFunc() { |
| 4332 | return Func1<R, P1, F, FuncInfo<P1, R> >(); |
| 4333 | } |
| 4334 | }; |
| 4335 | |
| 4336 | template <class R, class P1, class P2> |
| 4337 | struct FuncSig2 { |
| 4338 | template <R F(P1, P2)> |
| 4339 | Func2<R, P1, P2, F, FuncInfo<P1, R> > GetFunc() { |
| 4340 | return Func2<R, P1, P2, F, FuncInfo<P1, R> >(); |
| 4341 | } |
| 4342 | |
| 4343 | template <R F(P1, P2)> |
| 4344 | BoundFunc2<R, P1, P2, F, FuncInfo<P1, R> > GetFunc( |
| 4345 | typename remove_constptr<P2>::type param2) { |
| 4346 | return BoundFunc2<R, P1, P2, F, FuncInfo<P1, R> >(param2); |
| 4347 | } |
| 4348 | }; |
| 4349 | |
| 4350 | template <class R, class P1, class P2, class P3> |
| 4351 | struct FuncSig3 { |
| 4352 | template <R F(P1, P2, P3)> |
| 4353 | Func3<R, P1, P2, P3, F, FuncInfo<P1, R> > GetFunc() { |
| 4354 | return Func3<R, P1, P2, P3, F, FuncInfo<P1, R> >(); |
| 4355 | } |
| 4356 | |
| 4357 | template <R F(P1, P2, P3)> |
| 4358 | BoundFunc3<R, P1, P2, P3, F, FuncInfo<P1, R> > GetFunc( |
| 4359 | typename remove_constptr<P2>::type param2) { |
| 4360 | return BoundFunc3<R, P1, P2, P3, F, FuncInfo<P1, R> >(param2); |
| 4361 | } |
| 4362 | }; |
| 4363 | |
| 4364 | template <class R, class P1, class P2, class P3, class P4> |
| 4365 | struct FuncSig4 { |
| 4366 | template <R F(P1, P2, P3, P4)> |
| 4367 | Func4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> > GetFunc() { |
| 4368 | return Func4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> >(); |
| 4369 | } |
| 4370 | |
| 4371 | template <R F(P1, P2, P3, P4)> |
| 4372 | BoundFunc4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> > GetFunc( |
| 4373 | typename remove_constptr<P2>::type param2) { |
| 4374 | return BoundFunc4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> >(param2); |
| 4375 | } |
| 4376 | }; |
| 4377 | |
| 4378 | template <class R, class P1, class P2, class P3, class P4, class P5> |
| 4379 | struct FuncSig5 { |
| 4380 | template <R F(P1, P2, P3, P4, P5)> |
| 4381 | Func5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> > GetFunc() { |
| 4382 | return Func5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> >(); |
| 4383 | } |
| 4384 | |
| 4385 | template <R F(P1, P2, P3, P4, P5)> |
| 4386 | BoundFunc5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> > GetFunc( |
| 4387 | typename remove_constptr<P2>::type param2) { |
| 4388 | return BoundFunc5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> >(param2); |
| 4389 | } |
| 4390 | }; |
| 4391 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4392 | /* Overloaded template function that can construct the appropriate FuncSig* |
| 4393 | * class given a function pointer by deducing the template parameters. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4394 | template <class R, class P1> |
| 4395 | inline FuncSig1<R, P1> MatchFunc(R (*f)(P1)) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4396 | UPB_UNUSED(f); /* Only used for template parameter deduction. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4397 | return FuncSig1<R, P1>(); |
| 4398 | } |
| 4399 | |
| 4400 | template <class R, class P1, class P2> |
| 4401 | inline FuncSig2<R, P1, P2> MatchFunc(R (*f)(P1, P2)) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4402 | UPB_UNUSED(f); /* Only used for template parameter deduction. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4403 | return FuncSig2<R, P1, P2>(); |
| 4404 | } |
| 4405 | |
| 4406 | template <class R, class P1, class P2, class P3> |
| 4407 | inline FuncSig3<R, P1, P2, P3> MatchFunc(R (*f)(P1, P2, P3)) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4408 | UPB_UNUSED(f); /* Only used for template parameter deduction. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4409 | return FuncSig3<R, P1, P2, P3>(); |
| 4410 | } |
| 4411 | |
| 4412 | template <class R, class P1, class P2, class P3, class P4> |
| 4413 | inline FuncSig4<R, P1, P2, P3, P4> MatchFunc(R (*f)(P1, P2, P3, P4)) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4414 | UPB_UNUSED(f); /* Only used for template parameter deduction. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4415 | return FuncSig4<R, P1, P2, P3, P4>(); |
| 4416 | } |
| 4417 | |
| 4418 | template <class R, class P1, class P2, class P3, class P4, class P5> |
| 4419 | inline FuncSig5<R, P1, P2, P3, P4, P5> MatchFunc(R (*f)(P1, P2, P3, P4, P5)) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4420 | UPB_UNUSED(f); /* Only used for template parameter deduction. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4421 | return FuncSig5<R, P1, P2, P3, P4, P5>(); |
| 4422 | } |
| 4423 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4424 | /* MethodSig ******************************************************************/ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4425 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4426 | /* CallMethod*: a function template that calls a given method. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4427 | template <class R, class C, R (C::*F)()> |
| 4428 | R CallMethod0(C *obj) { |
| 4429 | return ((*obj).*F)(); |
| 4430 | } |
| 4431 | |
| 4432 | template <class R, class C, class P1, R (C::*F)(P1)> |
| 4433 | R CallMethod1(C *obj, P1 arg1) { |
| 4434 | return ((*obj).*F)(arg1); |
| 4435 | } |
| 4436 | |
| 4437 | template <class R, class C, class P1, class P2, R (C::*F)(P1, P2)> |
| 4438 | R CallMethod2(C *obj, P1 arg1, P2 arg2) { |
| 4439 | return ((*obj).*F)(arg1, arg2); |
| 4440 | } |
| 4441 | |
| 4442 | template <class R, class C, class P1, class P2, class P3, R (C::*F)(P1, P2, P3)> |
| 4443 | R CallMethod3(C *obj, P1 arg1, P2 arg2, P3 arg3) { |
| 4444 | return ((*obj).*F)(arg1, arg2, arg3); |
| 4445 | } |
| 4446 | |
| 4447 | template <class R, class C, class P1, class P2, class P3, class P4, |
| 4448 | R (C::*F)(P1, P2, P3, P4)> |
| 4449 | R CallMethod4(C *obj, P1 arg1, P2 arg2, P3 arg3, P4 arg4) { |
| 4450 | return ((*obj).*F)(arg1, arg2, arg3, arg4); |
| 4451 | } |
| 4452 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4453 | /* MethodSig: like FuncSig, but for member functions. |
| 4454 | * |
| 4455 | * GetFunc() returns a normal FuncN object, so after calling GetFunc() no |
| 4456 | * more logic is required to special-case methods. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4457 | template <class R, class C> |
| 4458 | struct MethodSig0 { |
| 4459 | template <R (C::*F)()> |
| 4460 | Func1<R, C *, CallMethod0<R, C, F>, FuncInfo<C *, R> > GetFunc() { |
| 4461 | return Func1<R, C *, CallMethod0<R, C, F>, FuncInfo<C *, R> >(); |
| 4462 | } |
| 4463 | }; |
| 4464 | |
| 4465 | template <class R, class C, class P1> |
| 4466 | struct MethodSig1 { |
| 4467 | template <R (C::*F)(P1)> |
| 4468 | Func2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> > GetFunc() { |
| 4469 | return Func2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> >(); |
| 4470 | } |
| 4471 | |
| 4472 | template <R (C::*F)(P1)> |
| 4473 | BoundFunc2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> > GetFunc( |
| 4474 | typename remove_constptr<P1>::type param1) { |
| 4475 | return BoundFunc2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> >( |
| 4476 | param1); |
| 4477 | } |
| 4478 | }; |
| 4479 | |
| 4480 | template <class R, class C, class P1, class P2> |
| 4481 | struct MethodSig2 { |
| 4482 | template <R (C::*F)(P1, P2)> |
| 4483 | Func3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>, FuncInfo<C *, R> > |
| 4484 | GetFunc() { |
| 4485 | return Func3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>, |
| 4486 | FuncInfo<C *, R> >(); |
| 4487 | } |
| 4488 | |
| 4489 | template <R (C::*F)(P1, P2)> |
| 4490 | BoundFunc3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>, FuncInfo<C *, R> > |
| 4491 | GetFunc(typename remove_constptr<P1>::type param1) { |
| 4492 | return BoundFunc3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>, |
| 4493 | FuncInfo<C *, R> >(param1); |
| 4494 | } |
| 4495 | }; |
| 4496 | |
| 4497 | template <class R, class C, class P1, class P2, class P3> |
| 4498 | struct MethodSig3 { |
| 4499 | template <R (C::*F)(P1, P2, P3)> |
| 4500 | Func4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>, FuncInfo<C *, R> > |
| 4501 | GetFunc() { |
| 4502 | return Func4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>, |
| 4503 | FuncInfo<C *, R> >(); |
| 4504 | } |
| 4505 | |
| 4506 | template <R (C::*F)(P1, P2, P3)> |
| 4507 | BoundFunc4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>, |
| 4508 | FuncInfo<C *, R> > |
| 4509 | GetFunc(typename remove_constptr<P1>::type param1) { |
| 4510 | return BoundFunc4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>, |
| 4511 | FuncInfo<C *, R> >(param1); |
| 4512 | } |
| 4513 | }; |
| 4514 | |
| 4515 | template <class R, class C, class P1, class P2, class P3, class P4> |
| 4516 | struct MethodSig4 { |
| 4517 | template <R (C::*F)(P1, P2, P3, P4)> |
| 4518 | Func5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>, |
| 4519 | FuncInfo<C *, R> > |
| 4520 | GetFunc() { |
| 4521 | return Func5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>, |
| 4522 | FuncInfo<C *, R> >(); |
| 4523 | } |
| 4524 | |
| 4525 | template <R (C::*F)(P1, P2, P3, P4)> |
| 4526 | BoundFunc5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>, |
| 4527 | FuncInfo<C *, R> > |
| 4528 | GetFunc(typename remove_constptr<P1>::type param1) { |
| 4529 | return BoundFunc5<R, C *, P1, P2, P3, P4, |
| 4530 | CallMethod4<R, C, P1, P2, P3, P4, F>, FuncInfo<C *, R> >( |
| 4531 | param1); |
| 4532 | } |
| 4533 | }; |
| 4534 | |
| 4535 | template <class R, class C> |
| 4536 | inline MethodSig0<R, C> MatchFunc(R (C::*f)()) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4537 | UPB_UNUSED(f); /* Only used for template parameter deduction. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4538 | return MethodSig0<R, C>(); |
| 4539 | } |
| 4540 | |
| 4541 | template <class R, class C, class P1> |
| 4542 | inline MethodSig1<R, C, P1> MatchFunc(R (C::*f)(P1)) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4543 | UPB_UNUSED(f); /* Only used for template parameter deduction. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4544 | return MethodSig1<R, C, P1>(); |
| 4545 | } |
| 4546 | |
| 4547 | template <class R, class C, class P1, class P2> |
| 4548 | inline MethodSig2<R, C, P1, P2> MatchFunc(R (C::*f)(P1, P2)) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4549 | UPB_UNUSED(f); /* Only used for template parameter deduction. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4550 | return MethodSig2<R, C, P1, P2>(); |
| 4551 | } |
| 4552 | |
| 4553 | template <class R, class C, class P1, class P2, class P3> |
| 4554 | inline MethodSig3<R, C, P1, P2, P3> MatchFunc(R (C::*f)(P1, P2, P3)) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4555 | UPB_UNUSED(f); /* Only used for template parameter deduction. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4556 | return MethodSig3<R, C, P1, P2, P3>(); |
| 4557 | } |
| 4558 | |
| 4559 | template <class R, class C, class P1, class P2, class P3, class P4> |
| 4560 | inline MethodSig4<R, C, P1, P2, P3, P4> MatchFunc(R (C::*f)(P1, P2, P3, P4)) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4561 | UPB_UNUSED(f); /* Only used for template parameter deduction. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4562 | return MethodSig4<R, C, P1, P2, P3, P4>(); |
| 4563 | } |
| 4564 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4565 | /* MaybeWrapReturn ************************************************************/ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4566 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4567 | /* Template class that attempts to wrap the return value of the function so it |
| 4568 | * matches the expected type. There are two main adjustments it may make: |
| 4569 | * |
| 4570 | * 1. If the function returns void, make it return the expected type and with |
| 4571 | * a value that always indicates success. |
| 4572 | * 2. If the function returns bool, make it return the expected type with a |
| 4573 | * value that indicates success or failure. |
| 4574 | * |
| 4575 | * The "expected type" for return is: |
| 4576 | * 1. void* for start handlers. If the closure parameter has a different type |
| 4577 | * we will cast it to void* for the return in the success case. |
| 4578 | * 2. size_t for string buffer handlers. |
| 4579 | * 3. bool for everything else. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4580 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4581 | /* Template parameters are FuncN type and desired return type. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4582 | template <class F, class R, class Enable = void> |
| 4583 | struct MaybeWrapReturn; |
| 4584 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4585 | /* If the return type matches, return the given function unwrapped. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4586 | template <class F> |
| 4587 | struct MaybeWrapReturn<F, typename F::Return> { |
| 4588 | typedef F Func; |
| 4589 | }; |
| 4590 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4591 | /* Function wrapper that munges the return value from void to (bool)true. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4592 | template <class P1, class P2, void F(P1, P2)> |
| 4593 | bool ReturnTrue2(P1 p1, P2 p2) { |
| 4594 | F(p1, p2); |
| 4595 | return true; |
| 4596 | } |
| 4597 | |
| 4598 | template <class P1, class P2, class P3, void F(P1, P2, P3)> |
| 4599 | bool ReturnTrue3(P1 p1, P2 p2, P3 p3) { |
| 4600 | F(p1, p2, p3); |
| 4601 | return true; |
| 4602 | } |
| 4603 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4604 | /* Function wrapper that munges the return value from void to (void*)arg1 */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4605 | template <class P1, class P2, void F(P1, P2)> |
| 4606 | void *ReturnClosure2(P1 p1, P2 p2) { |
| 4607 | F(p1, p2); |
| 4608 | return p1; |
| 4609 | } |
| 4610 | |
| 4611 | template <class P1, class P2, class P3, void F(P1, P2, P3)> |
| 4612 | void *ReturnClosure3(P1 p1, P2 p2, P3 p3) { |
| 4613 | F(p1, p2, p3); |
| 4614 | return p1; |
| 4615 | } |
| 4616 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4617 | /* Function wrapper that munges the return value from R to void*. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4618 | template <class R, class P1, class P2, R F(P1, P2)> |
| 4619 | void *CastReturnToVoidPtr2(P1 p1, P2 p2) { |
| 4620 | return F(p1, p2); |
| 4621 | } |
| 4622 | |
| 4623 | template <class R, class P1, class P2, class P3, R F(P1, P2, P3)> |
| 4624 | void *CastReturnToVoidPtr3(P1 p1, P2 p2, P3 p3) { |
| 4625 | return F(p1, p2, p3); |
| 4626 | } |
| 4627 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4628 | /* Function wrapper that munges the return value from bool to void*. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4629 | template <class P1, class P2, bool F(P1, P2)> |
| 4630 | void *ReturnClosureOrBreak2(P1 p1, P2 p2) { |
| 4631 | return F(p1, p2) ? p1 : UPB_BREAK; |
| 4632 | } |
| 4633 | |
| 4634 | template <class P1, class P2, class P3, bool F(P1, P2, P3)> |
| 4635 | void *ReturnClosureOrBreak3(P1 p1, P2 p2, P3 p3) { |
| 4636 | return F(p1, p2, p3) ? p1 : UPB_BREAK; |
| 4637 | } |
| 4638 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4639 | /* For the string callback, which takes five params, returns the size param. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4640 | template <class P1, class P2, |
| 4641 | void F(P1, P2, const char *, size_t, const BufferHandle *)> |
| 4642 | size_t ReturnStringLen(P1 p1, P2 p2, const char *p3, size_t p4, |
| 4643 | const BufferHandle *p5) { |
| 4644 | F(p1, p2, p3, p4, p5); |
| 4645 | return p4; |
| 4646 | } |
| 4647 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4648 | /* For the string callback, which takes five params, returns the size param or |
| 4649 | * zero. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4650 | template <class P1, class P2, |
| 4651 | bool F(P1, P2, const char *, size_t, const BufferHandle *)> |
| 4652 | size_t ReturnNOr0(P1 p1, P2 p2, const char *p3, size_t p4, |
| 4653 | const BufferHandle *p5) { |
| 4654 | return F(p1, p2, p3, p4, p5) ? p4 : 0; |
| 4655 | } |
| 4656 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4657 | /* If we have a function returning void but want a function returning bool, wrap |
| 4658 | * it in a function that returns true. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4659 | template <class P1, class P2, void F(P1, P2), class I> |
| 4660 | struct MaybeWrapReturn<Func2<void, P1, P2, F, I>, bool> { |
| 4661 | typedef Func2<bool, P1, P2, ReturnTrue2<P1, P2, F>, I> Func; |
| 4662 | }; |
| 4663 | |
| 4664 | template <class P1, class P2, class P3, void F(P1, P2, P3), class I> |
| 4665 | struct MaybeWrapReturn<Func3<void, P1, P2, P3, F, I>, bool> { |
| 4666 | typedef Func3<bool, P1, P2, P3, ReturnTrue3<P1, P2, P3, F>, I> Func; |
| 4667 | }; |
| 4668 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4669 | /* If our function returns void but we want one returning void*, wrap it in a |
| 4670 | * function that returns the first argument. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4671 | template <class P1, class P2, void F(P1, P2), class I> |
| 4672 | struct MaybeWrapReturn<Func2<void, P1, P2, F, I>, void *> { |
| 4673 | typedef Func2<void *, P1, P2, ReturnClosure2<P1, P2, F>, I> Func; |
| 4674 | }; |
| 4675 | |
| 4676 | template <class P1, class P2, class P3, void F(P1, P2, P3), class I> |
| 4677 | struct MaybeWrapReturn<Func3<void, P1, P2, P3, F, I>, void *> { |
| 4678 | typedef Func3<void *, P1, P2, P3, ReturnClosure3<P1, P2, P3, F>, I> Func; |
| 4679 | }; |
| 4680 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4681 | /* If our function returns R* but we want one returning void*, wrap it in a |
| 4682 | * function that casts to void*. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4683 | template <class R, class P1, class P2, R *F(P1, P2), class I> |
| 4684 | struct MaybeWrapReturn<Func2<R *, P1, P2, F, I>, void *, |
| 4685 | typename disable_if_same<R *, void *>::Type> { |
| 4686 | typedef Func2<void *, P1, P2, CastReturnToVoidPtr2<R *, P1, P2, F>, I> Func; |
| 4687 | }; |
| 4688 | |
| 4689 | template <class R, class P1, class P2, class P3, R *F(P1, P2, P3), class I> |
| 4690 | struct MaybeWrapReturn<Func3<R *, P1, P2, P3, F, I>, void *, |
| 4691 | typename disable_if_same<R *, void *>::Type> { |
| 4692 | typedef Func3<void *, P1, P2, P3, CastReturnToVoidPtr3<R *, P1, P2, P3, F>, I> |
| 4693 | Func; |
| 4694 | }; |
| 4695 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4696 | /* If our function returns bool but we want one returning void*, wrap it in a |
| 4697 | * function that returns either the first param or UPB_BREAK. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4698 | template <class P1, class P2, bool F(P1, P2), class I> |
| 4699 | struct MaybeWrapReturn<Func2<bool, P1, P2, F, I>, void *> { |
| 4700 | typedef Func2<void *, P1, P2, ReturnClosureOrBreak2<P1, P2, F>, I> Func; |
| 4701 | }; |
| 4702 | |
| 4703 | template <class P1, class P2, class P3, bool F(P1, P2, P3), class I> |
| 4704 | struct MaybeWrapReturn<Func3<bool, P1, P2, P3, F, I>, void *> { |
| 4705 | typedef Func3<void *, P1, P2, P3, ReturnClosureOrBreak3<P1, P2, P3, F>, I> |
| 4706 | Func; |
| 4707 | }; |
| 4708 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4709 | /* If our function returns void but we want one returning size_t, wrap it in a |
| 4710 | * function that returns the size argument. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4711 | template <class P1, class P2, |
| 4712 | void F(P1, P2, const char *, size_t, const BufferHandle *), class I> |
| 4713 | struct MaybeWrapReturn< |
| 4714 | Func5<void, P1, P2, const char *, size_t, const BufferHandle *, F, I>, |
| 4715 | size_t> { |
| 4716 | typedef Func5<size_t, P1, P2, const char *, size_t, const BufferHandle *, |
| 4717 | ReturnStringLen<P1, P2, F>, I> Func; |
| 4718 | }; |
| 4719 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4720 | /* If our function returns bool but we want one returning size_t, wrap it in a |
| 4721 | * function that returns either 0 or the buf size. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4722 | template <class P1, class P2, |
| 4723 | bool F(P1, P2, const char *, size_t, const BufferHandle *), class I> |
| 4724 | struct MaybeWrapReturn< |
| 4725 | Func5<bool, P1, P2, const char *, size_t, const BufferHandle *, F, I>, |
| 4726 | size_t> { |
| 4727 | typedef Func5<size_t, P1, P2, const char *, size_t, const BufferHandle *, |
| 4728 | ReturnNOr0<P1, P2, F>, I> Func; |
| 4729 | }; |
| 4730 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4731 | /* ConvertParams **************************************************************/ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4732 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4733 | /* Template class that converts the function parameters if necessary, and |
| 4734 | * ignores the HandlerData parameter if appropriate. |
| 4735 | * |
| 4736 | * Template parameter is the are FuncN function type. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4737 | template <class F, class T> |
| 4738 | struct ConvertParams; |
| 4739 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4740 | /* Function that discards the handler data parameter. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4741 | template <class R, class P1, R F(P1)> |
| 4742 | R IgnoreHandlerData2(void *p1, const void *hd) { |
| 4743 | UPB_UNUSED(hd); |
| 4744 | return F(static_cast<P1>(p1)); |
| 4745 | } |
| 4746 | |
| 4747 | template <class R, class P1, class P2Wrapper, class P2Wrapped, |
| 4748 | R F(P1, P2Wrapped)> |
| 4749 | R IgnoreHandlerData3(void *p1, const void *hd, P2Wrapper p2) { |
| 4750 | UPB_UNUSED(hd); |
| 4751 | return F(static_cast<P1>(p1), p2); |
| 4752 | } |
| 4753 | |
| 4754 | template <class R, class P1, class P2, class P3, R F(P1, P2, P3)> |
| 4755 | R IgnoreHandlerData4(void *p1, const void *hd, P2 p2, P3 p3) { |
| 4756 | UPB_UNUSED(hd); |
| 4757 | return F(static_cast<P1>(p1), p2, p3); |
| 4758 | } |
| 4759 | |
| 4760 | template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4)> |
| 4761 | R IgnoreHandlerData5(void *p1, const void *hd, P2 p2, P3 p3, P4 p4) { |
| 4762 | UPB_UNUSED(hd); |
| 4763 | return F(static_cast<P1>(p1), p2, p3, p4); |
| 4764 | } |
| 4765 | |
| 4766 | template <class R, class P1, R F(P1, const char*, size_t)> |
| 4767 | R IgnoreHandlerDataIgnoreHandle(void *p1, const void *hd, const char *p2, |
| 4768 | size_t p3, const BufferHandle *handle) { |
| 4769 | UPB_UNUSED(hd); |
| 4770 | UPB_UNUSED(handle); |
| 4771 | return F(static_cast<P1>(p1), p2, p3); |
| 4772 | } |
| 4773 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4774 | /* Function that casts the handler data parameter. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4775 | template <class R, class P1, class P2, R F(P1, P2)> |
| 4776 | R CastHandlerData2(void *c, const void *hd) { |
| 4777 | return F(static_cast<P1>(c), static_cast<P2>(hd)); |
| 4778 | } |
| 4779 | |
| 4780 | template <class R, class P1, class P2, class P3Wrapper, class P3Wrapped, |
| 4781 | R F(P1, P2, P3Wrapped)> |
| 4782 | R CastHandlerData3(void *c, const void *hd, P3Wrapper p3) { |
| 4783 | return F(static_cast<P1>(c), static_cast<P2>(hd), p3); |
| 4784 | } |
| 4785 | |
| 4786 | template <class R, class P1, class P2, class P3, class P4, class P5, |
| 4787 | R F(P1, P2, P3, P4, P5)> |
| 4788 | R CastHandlerData5(void *c, const void *hd, P3 p3, P4 p4, P5 p5) { |
| 4789 | return F(static_cast<P1>(c), static_cast<P2>(hd), p3, p4, p5); |
| 4790 | } |
| 4791 | |
| 4792 | template <class R, class P1, class P2, R F(P1, P2, const char *, size_t)> |
| 4793 | R CastHandlerDataIgnoreHandle(void *c, const void *hd, const char *p3, |
| 4794 | size_t p4, const BufferHandle *handle) { |
| 4795 | UPB_UNUSED(handle); |
| 4796 | return F(static_cast<P1>(c), static_cast<P2>(hd), p3, p4); |
| 4797 | } |
| 4798 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4799 | /* For unbound functions, ignore the handler data. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4800 | template <class R, class P1, R F(P1), class I, class T> |
| 4801 | struct ConvertParams<Func1<R, P1, F, I>, T> { |
| 4802 | typedef Func2<R, void *, const void *, IgnoreHandlerData2<R, P1, F>, I> Func; |
| 4803 | }; |
| 4804 | |
| 4805 | template <class R, class P1, class P2, R F(P1, P2), class I, |
| 4806 | class R2, class P1_2, class P2_2, class P3_2> |
| 4807 | struct ConvertParams<Func2<R, P1, P2, F, I>, |
| 4808 | R2 (*)(P1_2, P2_2, P3_2)> { |
| 4809 | typedef Func3<R, void *, const void *, P3_2, |
| 4810 | IgnoreHandlerData3<R, P1, P3_2, P2, F>, I> Func; |
| 4811 | }; |
| 4812 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4813 | /* For StringBuffer only; this ignores both the handler data and the |
| 4814 | * BufferHandle. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4815 | template <class R, class P1, R F(P1, const char *, size_t), class I, class T> |
| 4816 | struct ConvertParams<Func3<R, P1, const char *, size_t, F, I>, T> { |
| 4817 | typedef Func5<R, void *, const void *, const char *, size_t, |
| 4818 | const BufferHandle *, IgnoreHandlerDataIgnoreHandle<R, P1, F>, |
| 4819 | I> Func; |
| 4820 | }; |
| 4821 | |
| 4822 | template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4), |
| 4823 | class I, class T> |
| 4824 | struct ConvertParams<Func4<R, P1, P2, P3, P4, F, I>, T> { |
| 4825 | typedef Func5<R, void *, const void *, P2, P3, P4, |
| 4826 | IgnoreHandlerData5<R, P1, P2, P3, P4, F>, I> Func; |
| 4827 | }; |
| 4828 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4829 | /* For bound functions, cast the handler data. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4830 | template <class R, class P1, class P2, R F(P1, P2), class I, class T> |
| 4831 | struct ConvertParams<BoundFunc2<R, P1, P2, F, I>, T> { |
| 4832 | typedef Func2<R, void *, const void *, CastHandlerData2<R, P1, P2, F>, I> |
| 4833 | Func; |
| 4834 | }; |
| 4835 | |
| 4836 | template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I, |
| 4837 | class R2, class P1_2, class P2_2, class P3_2> |
| 4838 | struct ConvertParams<BoundFunc3<R, P1, P2, P3, F, I>, |
| 4839 | R2 (*)(P1_2, P2_2, P3_2)> { |
| 4840 | typedef Func3<R, void *, const void *, P3_2, |
| 4841 | CastHandlerData3<R, P1, P2, P3_2, P3, F>, I> Func; |
| 4842 | }; |
| 4843 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4844 | /* For StringBuffer only; this ignores the BufferHandle. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4845 | template <class R, class P1, class P2, R F(P1, P2, const char *, size_t), |
| 4846 | class I, class T> |
| 4847 | struct ConvertParams<BoundFunc4<R, P1, P2, const char *, size_t, F, I>, T> { |
| 4848 | typedef Func5<R, void *, const void *, const char *, size_t, |
| 4849 | const BufferHandle *, CastHandlerDataIgnoreHandle<R, P1, P2, F>, |
| 4850 | I> Func; |
| 4851 | }; |
| 4852 | |
| 4853 | template <class R, class P1, class P2, class P3, class P4, class P5, |
| 4854 | R F(P1, P2, P3, P4, P5), class I, class T> |
| 4855 | struct ConvertParams<BoundFunc5<R, P1, P2, P3, P4, P5, F, I>, T> { |
| 4856 | typedef Func5<R, void *, const void *, P3, P4, P5, |
| 4857 | CastHandlerData5<R, P1, P2, P3, P4, P5, F>, I> Func; |
| 4858 | }; |
| 4859 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4860 | /* utype/ltype are upper/lower-case, ctype is canonical C type, vtype is |
| 4861 | * variant C type. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4862 | #define TYPE_METHODS(utype, ltype, ctype, vtype) \ |
| 4863 | template <> struct CanonicalType<vtype> { \ |
| 4864 | typedef ctype Type; \ |
| 4865 | }; \ |
| 4866 | template <> \ |
| 4867 | inline bool Handlers::SetValueHandler<vtype>( \ |
| 4868 | const FieldDef *f, \ |
| 4869 | const Handlers::utype ## Handler& handler) { \ |
| 4870 | assert(!handler.registered_); \ |
| 4871 | handler.AddCleanup(this); \ |
| 4872 | handler.registered_ = true; \ |
| 4873 | return upb_handlers_set##ltype(this, f, handler.handler_, &handler.attr_); \ |
| 4874 | } \ |
| 4875 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4876 | TYPE_METHODS(Double, double, double, double) |
| 4877 | TYPE_METHODS(Float, float, float, float) |
| 4878 | TYPE_METHODS(UInt64, uint64, uint64_t, UPB_UINT64_T) |
| 4879 | TYPE_METHODS(UInt32, uint32, uint32_t, UPB_UINT32_T) |
| 4880 | TYPE_METHODS(Int64, int64, int64_t, UPB_INT64_T) |
| 4881 | TYPE_METHODS(Int32, int32, int32_t, UPB_INT32_T) |
| 4882 | TYPE_METHODS(Bool, bool, bool, bool) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4883 | |
| 4884 | #ifdef UPB_TWO_32BIT_TYPES |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4885 | TYPE_METHODS(Int32, int32, int32_t, UPB_INT32ALT_T) |
| 4886 | TYPE_METHODS(UInt32, uint32, uint32_t, UPB_UINT32ALT_T) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4887 | #endif |
| 4888 | |
| 4889 | #ifdef UPB_TWO_64BIT_TYPES |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4890 | TYPE_METHODS(Int64, int64, int64_t, UPB_INT64ALT_T) |
| 4891 | TYPE_METHODS(UInt64, uint64, uint64_t, UPB_UINT64ALT_T) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4892 | #endif |
| 4893 | #undef TYPE_METHODS |
| 4894 | |
| 4895 | template <> struct CanonicalType<Status*> { |
| 4896 | typedef Status* Type; |
| 4897 | }; |
| 4898 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4899 | /* Type methods that are only one-per-canonical-type and not |
| 4900 | * one-per-cvariant. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4901 | |
| 4902 | #define TYPE_METHODS(utype, ctype) \ |
| 4903 | inline bool Handlers::Set##utype##Handler(const FieldDef *f, \ |
| 4904 | const utype##Handler &h) { \ |
| 4905 | return SetValueHandler<ctype>(f, h); \ |
| 4906 | } \ |
| 4907 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4908 | TYPE_METHODS(Double, double) |
| 4909 | TYPE_METHODS(Float, float) |
| 4910 | TYPE_METHODS(UInt64, uint64_t) |
| 4911 | TYPE_METHODS(UInt32, uint32_t) |
| 4912 | TYPE_METHODS(Int64, int64_t) |
| 4913 | TYPE_METHODS(Int32, int32_t) |
| 4914 | TYPE_METHODS(Bool, bool) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4915 | #undef TYPE_METHODS |
| 4916 | |
| 4917 | template <class F> struct ReturnOf; |
| 4918 | |
| 4919 | template <class R, class P1, class P2> |
| 4920 | struct ReturnOf<R (*)(P1, P2)> { |
| 4921 | typedef R Return; |
| 4922 | }; |
| 4923 | |
| 4924 | template <class R, class P1, class P2, class P3> |
| 4925 | struct ReturnOf<R (*)(P1, P2, P3)> { |
| 4926 | typedef R Return; |
| 4927 | }; |
| 4928 | |
| 4929 | template <class R, class P1, class P2, class P3, class P4> |
| 4930 | struct ReturnOf<R (*)(P1, P2, P3, P4)> { |
| 4931 | typedef R Return; |
| 4932 | }; |
| 4933 | |
| 4934 | template <class R, class P1, class P2, class P3, class P4, class P5> |
| 4935 | struct ReturnOf<R (*)(P1, P2, P3, P4, P5)> { |
| 4936 | typedef R Return; |
| 4937 | }; |
| 4938 | |
| 4939 | template<class T> const void *UniquePtrForType() { |
| 4940 | static const char ch = 0; |
| 4941 | return &ch; |
| 4942 | } |
| 4943 | |
| 4944 | template <class T> |
| 4945 | template <class F> |
| 4946 | inline Handler<T>::Handler(F func) |
| 4947 | : registered_(false), |
| 4948 | cleanup_data_(func.GetData()), |
| 4949 | cleanup_func_(func.GetCleanup()) { |
| 4950 | upb_handlerattr_sethandlerdata(&attr_, func.GetData()); |
| 4951 | typedef typename ReturnOf<T>::Return Return; |
| 4952 | typedef typename ConvertParams<F, T>::Func ConvertedParamsFunc; |
| 4953 | typedef typename MaybeWrapReturn<ConvertedParamsFunc, Return>::Func |
| 4954 | ReturnWrappedFunc; |
| 4955 | handler_ = ReturnWrappedFunc().Call; |
| 4956 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4957 | /* Set attributes based on what templates can statically tell us about the |
| 4958 | * user's function. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4959 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4960 | /* If the original function returns void, then we know that we wrapped it to |
| 4961 | * always return ok. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4962 | bool always_ok = is_same<typename F::FuncInfo::Return, void>::value; |
| 4963 | attr_.SetAlwaysOk(always_ok); |
| 4964 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4965 | /* Closure parameter and return type. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4966 | attr_.SetClosureType(UniquePtrForType<typename F::FuncInfo::Closure>()); |
| 4967 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 4968 | /* We use the closure type (from the first parameter) if the return type is |
| 4969 | * void or bool, since these are the two cases we wrap to return the closure's |
| 4970 | * type anyway. |
| 4971 | * |
| 4972 | * This is all nonsense for non START* handlers, but it doesn't matter because |
| 4973 | * in that case the value will be ignored. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 4974 | typedef typename FirstUnlessVoidOrBool<typename F::FuncInfo::Return, |
| 4975 | typename F::FuncInfo::Closure>::value |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 4976 | EffectiveReturn; |
| 4977 | attr_.SetReturnClosureType(UniquePtrForType<EffectiveReturn>()); |
| 4978 | } |
| 4979 | |
| 4980 | template <class T> |
| 4981 | inline Handler<T>::~Handler() { |
| 4982 | assert(registered_); |
| 4983 | } |
| 4984 | |
| 4985 | inline HandlerAttributes::HandlerAttributes() { upb_handlerattr_init(this); } |
| 4986 | inline HandlerAttributes::~HandlerAttributes() { upb_handlerattr_uninit(this); } |
| 4987 | inline bool HandlerAttributes::SetHandlerData(const void *hd) { |
| 4988 | return upb_handlerattr_sethandlerdata(this, hd); |
| 4989 | } |
| 4990 | inline const void* HandlerAttributes::handler_data() const { |
| 4991 | return upb_handlerattr_handlerdata(this); |
| 4992 | } |
| 4993 | inline bool HandlerAttributes::SetClosureType(const void *type) { |
| 4994 | return upb_handlerattr_setclosuretype(this, type); |
| 4995 | } |
| 4996 | inline const void* HandlerAttributes::closure_type() const { |
| 4997 | return upb_handlerattr_closuretype(this); |
| 4998 | } |
| 4999 | inline bool HandlerAttributes::SetReturnClosureType(const void *type) { |
| 5000 | return upb_handlerattr_setreturnclosuretype(this, type); |
| 5001 | } |
| 5002 | inline const void* HandlerAttributes::return_closure_type() const { |
| 5003 | return upb_handlerattr_returnclosuretype(this); |
| 5004 | } |
| 5005 | inline bool HandlerAttributes::SetAlwaysOk(bool always_ok) { |
| 5006 | return upb_handlerattr_setalwaysok(this, always_ok); |
| 5007 | } |
| 5008 | inline bool HandlerAttributes::always_ok() const { |
| 5009 | return upb_handlerattr_alwaysok(this); |
| 5010 | } |
| 5011 | |
| 5012 | inline BufferHandle::BufferHandle() { upb_bufhandle_init(this); } |
| 5013 | inline BufferHandle::~BufferHandle() { upb_bufhandle_uninit(this); } |
| 5014 | inline const char* BufferHandle::buffer() const { |
| 5015 | return upb_bufhandle_buf(this); |
| 5016 | } |
| 5017 | inline size_t BufferHandle::object_offset() const { |
| 5018 | return upb_bufhandle_objofs(this); |
| 5019 | } |
| 5020 | inline void BufferHandle::SetBuffer(const char* buf, size_t ofs) { |
| 5021 | upb_bufhandle_setbuf(this, buf, ofs); |
| 5022 | } |
| 5023 | template <class T> |
| 5024 | void BufferHandle::SetAttachedObject(const T* obj) { |
| 5025 | upb_bufhandle_setobj(this, obj, UniquePtrForType<T>()); |
| 5026 | } |
| 5027 | template <class T> |
| 5028 | const T* BufferHandle::GetAttachedObject() const { |
| 5029 | return upb_bufhandle_objtype(this) == UniquePtrForType<T>() |
| 5030 | ? static_cast<const T *>(upb_bufhandle_obj(this)) |
| 5031 | : NULL; |
| 5032 | } |
| 5033 | |
| 5034 | inline reffed_ptr<Handlers> Handlers::New(const MessageDef *m) { |
| 5035 | upb_handlers *h = upb_handlers_new(m, &h); |
| 5036 | return reffed_ptr<Handlers>(h, &h); |
| 5037 | } |
| 5038 | inline reffed_ptr<const Handlers> Handlers::NewFrozen( |
| 5039 | const MessageDef *m, upb_handlers_callback *callback, |
| 5040 | const void *closure) { |
| 5041 | const upb_handlers *h = upb_handlers_newfrozen(m, &h, callback, closure); |
| 5042 | return reffed_ptr<const Handlers>(h, &h); |
| 5043 | } |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5044 | inline const Status* Handlers::status() { |
| 5045 | return upb_handlers_status(this); |
| 5046 | } |
| 5047 | inline void Handlers::ClearError() { |
| 5048 | return upb_handlers_clearerr(this); |
| 5049 | } |
| 5050 | inline bool Handlers::Freeze(Status *s) { |
| 5051 | upb::Handlers* h = this; |
| 5052 | return upb_handlers_freeze(&h, 1, s); |
| 5053 | } |
| 5054 | inline bool Handlers::Freeze(Handlers *const *handlers, int n, Status *s) { |
| 5055 | return upb_handlers_freeze(handlers, n, s); |
| 5056 | } |
| 5057 | inline bool Handlers::Freeze(const std::vector<Handlers*>& h, Status* status) { |
| 5058 | return upb_handlers_freeze((Handlers* const*)&h[0], h.size(), status); |
| 5059 | } |
| 5060 | inline const MessageDef *Handlers::message_def() const { |
| 5061 | return upb_handlers_msgdef(this); |
| 5062 | } |
| 5063 | inline bool Handlers::AddCleanup(void *p, upb_handlerfree *func) { |
| 5064 | return upb_handlers_addcleanup(this, p, func); |
| 5065 | } |
| 5066 | inline bool Handlers::SetStartMessageHandler( |
| 5067 | const Handlers::StartMessageHandler &handler) { |
| 5068 | assert(!handler.registered_); |
| 5069 | handler.registered_ = true; |
| 5070 | handler.AddCleanup(this); |
| 5071 | return upb_handlers_setstartmsg(this, handler.handler_, &handler.attr_); |
| 5072 | } |
| 5073 | inline bool Handlers::SetEndMessageHandler( |
| 5074 | const Handlers::EndMessageHandler &handler) { |
| 5075 | assert(!handler.registered_); |
| 5076 | handler.registered_ = true; |
| 5077 | handler.AddCleanup(this); |
| 5078 | return upb_handlers_setendmsg(this, handler.handler_, &handler.attr_); |
| 5079 | } |
| 5080 | inline bool Handlers::SetStartStringHandler(const FieldDef *f, |
| 5081 | const StartStringHandler &handler) { |
| 5082 | assert(!handler.registered_); |
| 5083 | handler.registered_ = true; |
| 5084 | handler.AddCleanup(this); |
| 5085 | return upb_handlers_setstartstr(this, f, handler.handler_, &handler.attr_); |
| 5086 | } |
| 5087 | inline bool Handlers::SetEndStringHandler(const FieldDef *f, |
| 5088 | const EndFieldHandler &handler) { |
| 5089 | assert(!handler.registered_); |
| 5090 | handler.registered_ = true; |
| 5091 | handler.AddCleanup(this); |
| 5092 | return upb_handlers_setendstr(this, f, handler.handler_, &handler.attr_); |
| 5093 | } |
| 5094 | inline bool Handlers::SetStringHandler(const FieldDef *f, |
| 5095 | const StringHandler& handler) { |
| 5096 | assert(!handler.registered_); |
| 5097 | handler.registered_ = true; |
| 5098 | handler.AddCleanup(this); |
| 5099 | return upb_handlers_setstring(this, f, handler.handler_, &handler.attr_); |
| 5100 | } |
| 5101 | inline bool Handlers::SetStartSequenceHandler( |
| 5102 | const FieldDef *f, const StartFieldHandler &handler) { |
| 5103 | assert(!handler.registered_); |
| 5104 | handler.registered_ = true; |
| 5105 | handler.AddCleanup(this); |
| 5106 | return upb_handlers_setstartseq(this, f, handler.handler_, &handler.attr_); |
| 5107 | } |
| 5108 | inline bool Handlers::SetStartSubMessageHandler( |
| 5109 | const FieldDef *f, const StartFieldHandler &handler) { |
| 5110 | assert(!handler.registered_); |
| 5111 | handler.registered_ = true; |
| 5112 | handler.AddCleanup(this); |
| 5113 | return upb_handlers_setstartsubmsg(this, f, handler.handler_, &handler.attr_); |
| 5114 | } |
| 5115 | inline bool Handlers::SetEndSubMessageHandler(const FieldDef *f, |
| 5116 | const EndFieldHandler &handler) { |
| 5117 | assert(!handler.registered_); |
| 5118 | handler.registered_ = true; |
| 5119 | handler.AddCleanup(this); |
| 5120 | return upb_handlers_setendsubmsg(this, f, handler.handler_, &handler.attr_); |
| 5121 | } |
| 5122 | inline bool Handlers::SetEndSequenceHandler(const FieldDef *f, |
| 5123 | const EndFieldHandler &handler) { |
| 5124 | assert(!handler.registered_); |
| 5125 | handler.registered_ = true; |
| 5126 | handler.AddCleanup(this); |
| 5127 | return upb_handlers_setendseq(this, f, handler.handler_, &handler.attr_); |
| 5128 | } |
| 5129 | inline bool Handlers::SetSubHandlers(const FieldDef *f, const Handlers *sub) { |
| 5130 | return upb_handlers_setsubhandlers(this, f, sub); |
| 5131 | } |
| 5132 | inline const Handlers *Handlers::GetSubHandlers(const FieldDef *f) const { |
| 5133 | return upb_handlers_getsubhandlers(this, f); |
| 5134 | } |
| 5135 | inline const Handlers *Handlers::GetSubHandlers(Handlers::Selector sel) const { |
| 5136 | return upb_handlers_getsubhandlers_sel(this, sel); |
| 5137 | } |
| 5138 | inline bool Handlers::GetSelector(const FieldDef *f, Handlers::Type type, |
| 5139 | Handlers::Selector *s) { |
| 5140 | return upb_handlers_getselector(f, type, s); |
| 5141 | } |
| 5142 | inline Handlers::Selector Handlers::GetEndSelector(Handlers::Selector start) { |
| 5143 | return upb_handlers_getendselector(start); |
| 5144 | } |
| 5145 | inline Handlers::GenericFunction *Handlers::GetHandler( |
| 5146 | Handlers::Selector selector) { |
| 5147 | return upb_handlers_gethandler(this, selector); |
| 5148 | } |
| 5149 | inline const void *Handlers::GetHandlerData(Handlers::Selector selector) { |
| 5150 | return upb_handlers_gethandlerdata(this, selector); |
| 5151 | } |
| 5152 | |
| 5153 | inline BytesHandler::BytesHandler() { |
| 5154 | upb_byteshandler_init(this); |
| 5155 | } |
| 5156 | |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5157 | inline BytesHandler::~BytesHandler() {} |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5158 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5159 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5160 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5161 | #endif /* __cplusplus */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5162 | |
| 5163 | |
| 5164 | #undef UPB_TWO_32BIT_TYPES |
| 5165 | #undef UPB_TWO_64BIT_TYPES |
| 5166 | #undef UPB_INT32_T |
| 5167 | #undef UPB_UINT32_T |
| 5168 | #undef UPB_INT32ALT_T |
| 5169 | #undef UPB_UINT32ALT_T |
| 5170 | #undef UPB_INT64_T |
| 5171 | #undef UPB_UINT64_T |
| 5172 | #undef UPB_INT64ALT_T |
| 5173 | #undef UPB_UINT64ALT_T |
| 5174 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5175 | #endif /* UPB_HANDLERS_INL_H_ */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5176 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5177 | #endif /* UPB_HANDLERS_H */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5178 | /* |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 5179 | ** upb::Environment (upb_env) |
| 5180 | ** |
| 5181 | ** A upb::Environment provides a means for injecting malloc and an |
| 5182 | ** error-reporting callback into encoders/decoders. This allows them to be |
| 5183 | ** independent of nearly all assumptions about their actual environment. |
| 5184 | ** |
| 5185 | ** It is also a container for allocating the encoders/decoders themselves that |
| 5186 | ** insulates clients from knowing their actual size. This provides ABI |
| 5187 | ** compatibility even if the size of the objects change. And this allows the |
| 5188 | ** structure definitions to be in the .c files instead of the .h files, making |
| 5189 | ** the .h files smaller and more readable. |
| 5190 | */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5191 | |
| 5192 | |
| 5193 | #ifndef UPB_ENV_H_ |
| 5194 | #define UPB_ENV_H_ |
| 5195 | |
| 5196 | #ifdef __cplusplus |
| 5197 | namespace upb { |
| 5198 | class Environment; |
| 5199 | class SeededAllocator; |
| 5200 | } |
| 5201 | #endif |
| 5202 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5203 | UPB_DECLARE_TYPE(upb::Environment, upb_env) |
| 5204 | UPB_DECLARE_TYPE(upb::SeededAllocator, upb_seededalloc) |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5205 | |
| 5206 | typedef void *upb_alloc_func(void *ud, void *ptr, size_t oldsize, size_t size); |
| 5207 | typedef void upb_cleanup_func(void *ud); |
| 5208 | typedef bool upb_error_func(void *ud, const upb_status *status); |
| 5209 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5210 | #ifdef __cplusplus |
| 5211 | |
| 5212 | /* An environment is *not* thread-safe. */ |
| 5213 | class upb::Environment { |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5214 | public: |
| 5215 | Environment(); |
| 5216 | ~Environment(); |
| 5217 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5218 | /* Set a custom memory allocation function for the environment. May ONLY |
| 5219 | * be called before any calls to Malloc()/Realloc()/AddCleanup() below. |
| 5220 | * If this is not called, the system realloc() function will be used. |
| 5221 | * The given user pointer "ud" will be passed to the allocation function. |
| 5222 | * |
| 5223 | * The allocation function will not receive corresponding "free" calls. it |
| 5224 | * must ensure that the memory is valid for the lifetime of the Environment, |
| 5225 | * but it may be reclaimed any time thereafter. The likely usage is that |
| 5226 | * "ud" points to a stateful allocator, and that the allocator frees all |
| 5227 | * memory, arena-style, when it is destroyed. In this case the allocator must |
| 5228 | * outlive the Environment. Another possibility is that the allocation |
| 5229 | * function returns GC-able memory that is guaranteed to be GC-rooted for the |
| 5230 | * life of the Environment. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5231 | void SetAllocationFunction(upb_alloc_func* alloc, void* ud); |
| 5232 | |
| 5233 | template<class T> |
| 5234 | void SetAllocator(T* allocator) { |
| 5235 | SetAllocationFunction(allocator->GetAllocationFunction(), allocator); |
| 5236 | } |
| 5237 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5238 | /* Set a custom error reporting function. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5239 | void SetErrorFunction(upb_error_func* func, void* ud); |
| 5240 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5241 | /* Set the error reporting function to simply copy the status to the given |
| 5242 | * status and abort. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5243 | void ReportErrorsTo(Status* status); |
| 5244 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5245 | /* Returns true if all allocations and AddCleanup() calls have succeeded, |
| 5246 | * and no errors were reported with ReportError() (except ones that recovered |
| 5247 | * successfully). */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5248 | bool ok() const; |
| 5249 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5250 | /* Functions for use by encoders/decoders. **********************************/ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5251 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5252 | /* Reports an error to this environment's callback, returning true if |
| 5253 | * the caller should try to recover. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5254 | bool ReportError(const Status* status); |
| 5255 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5256 | /* Allocate memory. Uses the environment's allocation function. |
| 5257 | * |
| 5258 | * There is no need to free(). All memory will be freed automatically, but is |
| 5259 | * guaranteed to outlive the Environment. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5260 | void* Malloc(size_t size); |
| 5261 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5262 | /* Reallocate memory. Preserves "oldsize" bytes from the existing buffer |
| 5263 | * Requires: oldsize <= existing_size. |
| 5264 | * |
| 5265 | * TODO(haberman): should we also enforce that oldsize <= size? */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5266 | void* Realloc(void* ptr, size_t oldsize, size_t size); |
| 5267 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5268 | /* Add a cleanup function to run when the environment is destroyed. |
| 5269 | * Returns false on out-of-memory. |
| 5270 | * |
| 5271 | * The first call to AddCleanup() after SetAllocationFunction() is guaranteed |
| 5272 | * to return true -- this makes it possible to robustly set a cleanup handler |
| 5273 | * for a custom allocation function. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5274 | bool AddCleanup(upb_cleanup_func* func, void* ud); |
| 5275 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5276 | /* Total number of bytes that have been allocated. It is undefined what |
| 5277 | * Realloc() does to this counter. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5278 | size_t BytesAllocated() const; |
| 5279 | |
| 5280 | private: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5281 | UPB_DISALLOW_COPY_AND_ASSIGN(Environment) |
| 5282 | |
| 5283 | #else |
| 5284 | struct upb_env { |
| 5285 | #endif /* __cplusplus */ |
| 5286 | |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5287 | bool ok_; |
| 5288 | size_t bytes_allocated; |
| 5289 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5290 | /* Alloc function. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5291 | upb_alloc_func *alloc; |
| 5292 | void *alloc_ud; |
| 5293 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5294 | /* Error-reporting function. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5295 | upb_error_func *err; |
| 5296 | void *err_ud; |
| 5297 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5298 | /* Userdata for default alloc func. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5299 | void *default_alloc_ud; |
| 5300 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5301 | /* Cleanup entries. Pointer to a cleanup_ent, defined in env.c */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5302 | void *cleanup_head; |
| 5303 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5304 | /* For future expansion, since the size of this struct is exposed to users. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5305 | void *future1; |
| 5306 | void *future2; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5307 | }; |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5308 | |
| 5309 | UPB_BEGIN_EXTERN_C |
| 5310 | |
| 5311 | void upb_env_init(upb_env *e); |
| 5312 | void upb_env_uninit(upb_env *e); |
| 5313 | void upb_env_setallocfunc(upb_env *e, upb_alloc_func *func, void *ud); |
| 5314 | void upb_env_seterrorfunc(upb_env *e, upb_error_func *func, void *ud); |
| 5315 | void upb_env_reporterrorsto(upb_env *e, upb_status *status); |
| 5316 | bool upb_env_ok(const upb_env *e); |
| 5317 | bool upb_env_reporterror(upb_env *e, const upb_status *status); |
| 5318 | void *upb_env_malloc(upb_env *e, size_t size); |
| 5319 | void *upb_env_realloc(upb_env *e, void *ptr, size_t oldsize, size_t size); |
| 5320 | bool upb_env_addcleanup(upb_env *e, upb_cleanup_func *func, void *ud); |
| 5321 | size_t upb_env_bytesallocated(const upb_env *e); |
| 5322 | |
| 5323 | UPB_END_EXTERN_C |
| 5324 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5325 | #ifdef __cplusplus |
| 5326 | |
| 5327 | /* An allocator that allocates from an initial memory region (likely the stack) |
| 5328 | * before falling back to another allocator. */ |
| 5329 | class upb::SeededAllocator { |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5330 | public: |
| 5331 | SeededAllocator(void *mem, size_t len); |
| 5332 | ~SeededAllocator(); |
| 5333 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5334 | /* Set a custom fallback memory allocation function for the allocator, to use |
| 5335 | * once the initial region runs out. |
| 5336 | * |
| 5337 | * May ONLY be called before GetAllocationFunction(). If this is not |
| 5338 | * called, the system realloc() will be the fallback allocator. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5339 | void SetFallbackAllocator(upb_alloc_func *alloc, void *ud); |
| 5340 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5341 | /* Gets the allocation function for this allocator. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5342 | upb_alloc_func* GetAllocationFunction(); |
| 5343 | |
| 5344 | private: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5345 | UPB_DISALLOW_COPY_AND_ASSIGN(SeededAllocator) |
| 5346 | |
| 5347 | #else |
| 5348 | struct upb_seededalloc { |
| 5349 | #endif /* __cplusplus */ |
| 5350 | |
| 5351 | /* Fallback alloc function. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5352 | upb_alloc_func *alloc; |
| 5353 | upb_cleanup_func *alloc_cleanup; |
| 5354 | void *alloc_ud; |
| 5355 | bool need_cleanup; |
| 5356 | bool returned_allocfunc; |
| 5357 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5358 | /* Userdata for default alloc func. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5359 | void *default_alloc_ud; |
| 5360 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5361 | /* Pointers for the initial memory region. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5362 | char *mem_base; |
| 5363 | char *mem_ptr; |
| 5364 | char *mem_limit; |
| 5365 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5366 | /* For future expansion, since the size of this struct is exposed to users. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5367 | void *future1; |
| 5368 | void *future2; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5369 | }; |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5370 | |
| 5371 | UPB_BEGIN_EXTERN_C |
| 5372 | |
| 5373 | void upb_seededalloc_init(upb_seededalloc *a, void *mem, size_t len); |
| 5374 | void upb_seededalloc_uninit(upb_seededalloc *a); |
| 5375 | void upb_seededalloc_setfallbackalloc(upb_seededalloc *a, upb_alloc_func *func, |
| 5376 | void *ud); |
| 5377 | upb_alloc_func *upb_seededalloc_getallocfunc(upb_seededalloc *a); |
| 5378 | |
| 5379 | UPB_END_EXTERN_C |
| 5380 | |
| 5381 | #ifdef __cplusplus |
| 5382 | |
| 5383 | namespace upb { |
| 5384 | |
| 5385 | inline Environment::Environment() { |
| 5386 | upb_env_init(this); |
| 5387 | } |
| 5388 | inline Environment::~Environment() { |
| 5389 | upb_env_uninit(this); |
| 5390 | } |
| 5391 | inline void Environment::SetAllocationFunction(upb_alloc_func *alloc, |
| 5392 | void *ud) { |
| 5393 | upb_env_setallocfunc(this, alloc, ud); |
| 5394 | } |
| 5395 | inline void Environment::SetErrorFunction(upb_error_func *func, void *ud) { |
| 5396 | upb_env_seterrorfunc(this, func, ud); |
| 5397 | } |
| 5398 | inline void Environment::ReportErrorsTo(Status* status) { |
| 5399 | upb_env_reporterrorsto(this, status); |
| 5400 | } |
| 5401 | inline bool Environment::ok() const { |
| 5402 | return upb_env_ok(this); |
| 5403 | } |
| 5404 | inline bool Environment::ReportError(const Status* status) { |
| 5405 | return upb_env_reporterror(this, status); |
| 5406 | } |
| 5407 | inline void *Environment::Malloc(size_t size) { |
| 5408 | return upb_env_malloc(this, size); |
| 5409 | } |
| 5410 | inline void *Environment::Realloc(void *ptr, size_t oldsize, size_t size) { |
| 5411 | return upb_env_realloc(this, ptr, oldsize, size); |
| 5412 | } |
| 5413 | inline bool Environment::AddCleanup(upb_cleanup_func *func, void *ud) { |
| 5414 | return upb_env_addcleanup(this, func, ud); |
| 5415 | } |
| 5416 | inline size_t Environment::BytesAllocated() const { |
| 5417 | return upb_env_bytesallocated(this); |
| 5418 | } |
| 5419 | |
| 5420 | inline SeededAllocator::SeededAllocator(void *mem, size_t len) { |
| 5421 | upb_seededalloc_init(this, mem, len); |
| 5422 | } |
| 5423 | inline SeededAllocator::~SeededAllocator() { |
| 5424 | upb_seededalloc_uninit(this); |
| 5425 | } |
| 5426 | inline void SeededAllocator::SetFallbackAllocator(upb_alloc_func *alloc, |
| 5427 | void *ud) { |
| 5428 | upb_seededalloc_setfallbackalloc(this, alloc, ud); |
| 5429 | } |
| 5430 | inline upb_alloc_func *SeededAllocator::GetAllocationFunction() { |
| 5431 | return upb_seededalloc_getallocfunc(this); |
| 5432 | } |
| 5433 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5434 | } /* namespace upb */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5435 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5436 | #endif /* __cplusplus */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5437 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5438 | #endif /* UPB_ENV_H_ */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 5439 | /* |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 5440 | ** upb::Sink (upb_sink) |
| 5441 | ** upb::BytesSink (upb_bytessink) |
| 5442 | ** |
| 5443 | ** A upb_sink is an object that binds a upb_handlers object to some runtime |
| 5444 | ** state. It is the object that can actually receive data via the upb_handlers |
| 5445 | ** interface. |
| 5446 | ** |
| 5447 | ** Unlike upb_def and upb_handlers, upb_sink is never frozen, immutable, or |
| 5448 | ** thread-safe. You can create as many of them as you want, but each one may |
| 5449 | ** only be used in a single thread at a time. |
| 5450 | ** |
| 5451 | ** If we compare with class-based OOP, a you can think of a upb_def as an |
| 5452 | ** abstract base class, a upb_handlers as a concrete derived class, and a |
| 5453 | ** upb_sink as an object (class instance). |
| 5454 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5455 | |
| 5456 | #ifndef UPB_SINK_H |
| 5457 | #define UPB_SINK_H |
| 5458 | |
| 5459 | |
| 5460 | #ifdef __cplusplus |
| 5461 | namespace upb { |
| 5462 | class BufferSource; |
| 5463 | class BytesSink; |
| 5464 | class Sink; |
| 5465 | } |
| 5466 | #endif |
| 5467 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5468 | UPB_DECLARE_TYPE(upb::BufferSource, upb_bufsrc) |
| 5469 | UPB_DECLARE_TYPE(upb::BytesSink, upb_bytessink) |
| 5470 | UPB_DECLARE_TYPE(upb::Sink, upb_sink) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5471 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5472 | #ifdef __cplusplus |
| 5473 | |
| 5474 | /* A upb::Sink is an object that binds a upb::Handlers object to some runtime |
| 5475 | * state. It represents an endpoint to which data can be sent. |
| 5476 | * |
| 5477 | * TODO(haberman): right now all of these functions take selectors. Should they |
| 5478 | * take selectorbase instead? |
| 5479 | * |
| 5480 | * ie. instead of calling: |
| 5481 | * sink->StartString(FOO_FIELD_START_STRING, ...) |
| 5482 | * a selector base would let you say: |
| 5483 | * sink->StartString(FOO_FIELD, ...) |
| 5484 | * |
| 5485 | * This would make call sites a little nicer and require emitting fewer selector |
| 5486 | * definitions in .h files. |
| 5487 | * |
| 5488 | * But the current scheme has the benefit that you can retrieve a function |
| 5489 | * pointer for any handler with handlers->GetHandler(selector), without having |
| 5490 | * to have a separate GetHandler() function for each handler type. The JIT |
| 5491 | * compiler uses this. To accommodate we'd have to expose a separate |
| 5492 | * GetHandler() for every handler type. |
| 5493 | * |
| 5494 | * Also to ponder: selectors right now are independent of a specific Handlers |
| 5495 | * instance. In other words, they allocate a number to every possible handler |
| 5496 | * that *could* be registered, without knowing anything about what handlers |
| 5497 | * *are* registered. That means that using selectors as table offsets prohibits |
| 5498 | * us from compacting the handler table at Freeze() time. If the table is very |
| 5499 | * sparse, this could be wasteful. |
| 5500 | * |
| 5501 | * Having another selector-like thing that is specific to a Handlers instance |
| 5502 | * would allow this compacting, but then it would be impossible to write code |
| 5503 | * ahead-of-time that can be bound to any Handlers instance at runtime. For |
| 5504 | * example, a .proto file parser written as straight C will not know what |
| 5505 | * Handlers it will be bound to, so when it calls sink->StartString() what |
| 5506 | * selector will it pass? It needs a selector like we have today, that is |
| 5507 | * independent of any particular upb::Handlers. |
| 5508 | * |
| 5509 | * Is there a way then to allow Handlers table compaction? */ |
| 5510 | class upb::Sink { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5511 | public: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5512 | /* Constructor with no initialization; must be Reset() before use. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5513 | Sink() {} |
| 5514 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5515 | /* Constructs a new sink for the given frozen handlers and closure. |
| 5516 | * |
| 5517 | * TODO: once the Handlers know the expected closure type, verify that T |
| 5518 | * matches it. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5519 | template <class T> Sink(const Handlers* handlers, T* closure); |
| 5520 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5521 | /* Resets the value of the sink. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5522 | template <class T> void Reset(const Handlers* handlers, T* closure); |
| 5523 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5524 | /* Returns the top-level object that is bound to this sink. |
| 5525 | * |
| 5526 | * TODO: once the Handlers know the expected closure type, verify that T |
| 5527 | * matches it. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5528 | template <class T> T* GetObject() const; |
| 5529 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5530 | /* Functions for pushing data into the sink. |
| 5531 | * |
| 5532 | * These return false if processing should stop (either due to error or just |
| 5533 | * to suspend). |
| 5534 | * |
| 5535 | * These may not be called from within one of the same sink's handlers (in |
| 5536 | * other words, handlers are not re-entrant). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5537 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5538 | /* Should be called at the start and end of every message; both the top-level |
| 5539 | * message and submessages. This means that submessages should use the |
| 5540 | * following sequence: |
| 5541 | * sink->StartSubMessage(startsubmsg_selector); |
| 5542 | * sink->StartMessage(); |
| 5543 | * // ... |
| 5544 | * sink->EndMessage(&status); |
| 5545 | * sink->EndSubMessage(endsubmsg_selector); */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5546 | bool StartMessage(); |
| 5547 | bool EndMessage(Status* status); |
| 5548 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5549 | /* Putting of individual values. These work for both repeated and |
| 5550 | * non-repeated fields, but for repeated fields you must wrap them in |
| 5551 | * calls to StartSequence()/EndSequence(). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5552 | bool PutInt32(Handlers::Selector s, int32_t val); |
| 5553 | bool PutInt64(Handlers::Selector s, int64_t val); |
| 5554 | bool PutUInt32(Handlers::Selector s, uint32_t val); |
| 5555 | bool PutUInt64(Handlers::Selector s, uint64_t val); |
| 5556 | bool PutFloat(Handlers::Selector s, float val); |
| 5557 | bool PutDouble(Handlers::Selector s, double val); |
| 5558 | bool PutBool(Handlers::Selector s, bool val); |
| 5559 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5560 | /* Putting of string/bytes values. Each string can consist of zero or more |
| 5561 | * non-contiguous buffers of data. |
| 5562 | * |
| 5563 | * For StartString(), the function will write a sink for the string to "sub." |
| 5564 | * The sub-sink must be used for any/all PutStringBuffer() calls. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5565 | bool StartString(Handlers::Selector s, size_t size_hint, Sink* sub); |
| 5566 | size_t PutStringBuffer(Handlers::Selector s, const char *buf, size_t len, |
| 5567 | const BufferHandle *handle); |
| 5568 | bool EndString(Handlers::Selector s); |
| 5569 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5570 | /* For submessage fields. |
| 5571 | * |
| 5572 | * For StartSubMessage(), the function will write a sink for the string to |
| 5573 | * "sub." The sub-sink must be used for any/all handlers called within the |
| 5574 | * submessage. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5575 | bool StartSubMessage(Handlers::Selector s, Sink* sub); |
| 5576 | bool EndSubMessage(Handlers::Selector s); |
| 5577 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5578 | /* For repeated fields of any type, the sequence of values must be wrapped in |
| 5579 | * these calls. |
| 5580 | * |
| 5581 | * For StartSequence(), the function will write a sink for the string to |
| 5582 | * "sub." The sub-sink must be used for any/all handlers called within the |
| 5583 | * sequence. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5584 | bool StartSequence(Handlers::Selector s, Sink* sub); |
| 5585 | bool EndSequence(Handlers::Selector s); |
| 5586 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5587 | /* Copy and assign specifically allowed. |
| 5588 | * We don't even bother making these members private because so many |
| 5589 | * functions need them and this is mainly just a dumb data container anyway. |
| 5590 | */ |
| 5591 | #else |
| 5592 | struct upb_sink { |
| 5593 | #endif |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5594 | const upb_handlers *handlers; |
| 5595 | void *closure; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5596 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5597 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5598 | #ifdef __cplusplus |
| 5599 | class upb::BytesSink { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5600 | public: |
| 5601 | BytesSink() {} |
| 5602 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5603 | /* Constructs a new sink for the given frozen handlers and closure. |
| 5604 | * |
| 5605 | * TODO(haberman): once the Handlers know the expected closure type, verify |
| 5606 | * that T matches it. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5607 | template <class T> BytesSink(const BytesHandler* handler, T* closure); |
| 5608 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5609 | /* Resets the value of the sink. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5610 | template <class T> void Reset(const BytesHandler* handler, T* closure); |
| 5611 | |
| 5612 | bool Start(size_t size_hint, void **subc); |
| 5613 | size_t PutBuffer(void *subc, const char *buf, size_t len, |
| 5614 | const BufferHandle *handle); |
| 5615 | bool End(); |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5616 | #else |
| 5617 | struct upb_bytessink { |
| 5618 | #endif |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5619 | const upb_byteshandler *handler; |
| 5620 | void *closure; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5621 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5622 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5623 | #ifdef __cplusplus |
| 5624 | |
| 5625 | /* A class for pushing a flat buffer of data to a BytesSink. |
| 5626 | * You can construct an instance of this to get a resumable source, |
| 5627 | * or just call the static PutBuffer() to do a non-resumable push all in one |
| 5628 | * go. */ |
| 5629 | class upb::BufferSource { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5630 | public: |
| 5631 | BufferSource(); |
| 5632 | BufferSource(const char* buf, size_t len, BytesSink* sink); |
| 5633 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5634 | /* Returns true if the entire buffer was pushed successfully. Otherwise the |
| 5635 | * next call to PutNext() will resume where the previous one left off. |
| 5636 | * TODO(haberman): implement this. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5637 | bool PutNext(); |
| 5638 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5639 | /* A static version; with this version is it not possible to resume in the |
| 5640 | * case of failure or a partially-consumed buffer. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5641 | static bool PutBuffer(const char* buf, size_t len, BytesSink* sink); |
| 5642 | |
| 5643 | template <class T> static bool PutBuffer(const T& str, BytesSink* sink) { |
| 5644 | return PutBuffer(str.c_str(), str.size(), sink); |
| 5645 | } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5646 | #else |
| 5647 | struct upb_bufsrc { |
| 5648 | char dummy; |
| 5649 | #endif |
| 5650 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5651 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5652 | UPB_BEGIN_EXTERN_C |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5653 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5654 | /* Inline definitions. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5655 | |
| 5656 | UPB_INLINE void upb_bytessink_reset(upb_bytessink *s, const upb_byteshandler *h, |
| 5657 | void *closure) { |
| 5658 | s->handler = h; |
| 5659 | s->closure = closure; |
| 5660 | } |
| 5661 | |
| 5662 | UPB_INLINE bool upb_bytessink_start(upb_bytessink *s, size_t size_hint, |
| 5663 | void **subc) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5664 | typedef upb_startstr_handlerfunc func; |
| 5665 | func *start; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5666 | *subc = s->closure; |
| 5667 | if (!s->handler) return true; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5668 | start = (func *)s->handler->table[UPB_STARTSTR_SELECTOR].func; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5669 | |
| 5670 | if (!start) return true; |
| 5671 | *subc = start(s->closure, upb_handlerattr_handlerdata( |
| 5672 | &s->handler->table[UPB_STARTSTR_SELECTOR].attr), |
| 5673 | size_hint); |
| 5674 | return *subc != NULL; |
| 5675 | } |
| 5676 | |
| 5677 | UPB_INLINE size_t upb_bytessink_putbuf(upb_bytessink *s, void *subc, |
| 5678 | const char *buf, size_t size, |
| 5679 | const upb_bufhandle* handle) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5680 | typedef upb_string_handlerfunc func; |
| 5681 | func *putbuf; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5682 | if (!s->handler) return true; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5683 | putbuf = (func *)s->handler->table[UPB_STRING_SELECTOR].func; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5684 | |
| 5685 | if (!putbuf) return true; |
| 5686 | return putbuf(subc, upb_handlerattr_handlerdata( |
| 5687 | &s->handler->table[UPB_STRING_SELECTOR].attr), |
| 5688 | buf, size, handle); |
| 5689 | } |
| 5690 | |
| 5691 | UPB_INLINE bool upb_bytessink_end(upb_bytessink *s) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5692 | typedef upb_endfield_handlerfunc func; |
| 5693 | func *end; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5694 | if (!s->handler) return true; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5695 | end = (func *)s->handler->table[UPB_ENDSTR_SELECTOR].func; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5696 | |
| 5697 | if (!end) return true; |
| 5698 | return end(s->closure, |
| 5699 | upb_handlerattr_handlerdata( |
| 5700 | &s->handler->table[UPB_ENDSTR_SELECTOR].attr)); |
| 5701 | } |
| 5702 | |
| 5703 | UPB_INLINE bool upb_bufsrc_putbuf(const char *buf, size_t len, |
| 5704 | upb_bytessink *sink) { |
| 5705 | void *subc; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5706 | bool ret; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5707 | upb_bufhandle handle; |
| 5708 | upb_bufhandle_init(&handle); |
| 5709 | upb_bufhandle_setbuf(&handle, buf, 0); |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5710 | ret = upb_bytessink_start(sink, len, &subc); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5711 | if (ret && len != 0) { |
Josh Haberman | 5bdf4a4 | 2015-08-03 15:51:31 -0700 | [diff] [blame] | 5712 | ret = (upb_bytessink_putbuf(sink, subc, buf, len, &handle) >= len); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5713 | } |
| 5714 | if (ret) { |
| 5715 | ret = upb_bytessink_end(sink); |
| 5716 | } |
| 5717 | upb_bufhandle_uninit(&handle); |
| 5718 | return ret; |
| 5719 | } |
| 5720 | |
| 5721 | #define PUTVAL(type, ctype) \ |
| 5722 | UPB_INLINE bool upb_sink_put##type(upb_sink *s, upb_selector_t sel, \ |
| 5723 | ctype val) { \ |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5724 | typedef upb_##type##_handlerfunc functype; \ |
| 5725 | functype *func; \ |
| 5726 | const void *hd; \ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5727 | if (!s->handlers) return true; \ |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5728 | func = (functype *)upb_handlers_gethandler(s->handlers, sel); \ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5729 | if (!func) return true; \ |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5730 | hd = upb_handlers_gethandlerdata(s->handlers, sel); \ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5731 | return func(s->closure, hd, val); \ |
| 5732 | } |
| 5733 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5734 | PUTVAL(int32, int32_t) |
| 5735 | PUTVAL(int64, int64_t) |
| 5736 | PUTVAL(uint32, uint32_t) |
| 5737 | PUTVAL(uint64, uint64_t) |
| 5738 | PUTVAL(float, float) |
| 5739 | PUTVAL(double, double) |
| 5740 | PUTVAL(bool, bool) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5741 | #undef PUTVAL |
| 5742 | |
| 5743 | UPB_INLINE void upb_sink_reset(upb_sink *s, const upb_handlers *h, void *c) { |
| 5744 | s->handlers = h; |
| 5745 | s->closure = c; |
| 5746 | } |
| 5747 | |
| 5748 | UPB_INLINE size_t upb_sink_putstring(upb_sink *s, upb_selector_t sel, |
| 5749 | const char *buf, size_t n, |
| 5750 | const upb_bufhandle *handle) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5751 | typedef upb_string_handlerfunc func; |
| 5752 | func *handler; |
| 5753 | const void *hd; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5754 | if (!s->handlers) return n; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5755 | handler = (func *)upb_handlers_gethandler(s->handlers, sel); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5756 | |
| 5757 | if (!handler) return n; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5758 | hd = upb_handlers_gethandlerdata(s->handlers, sel); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5759 | return handler(s->closure, hd, buf, n, handle); |
| 5760 | } |
| 5761 | |
| 5762 | UPB_INLINE bool upb_sink_startmsg(upb_sink *s) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5763 | typedef upb_startmsg_handlerfunc func; |
| 5764 | func *startmsg; |
| 5765 | const void *hd; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5766 | if (!s->handlers) return true; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5767 | startmsg = (func*)upb_handlers_gethandler(s->handlers, UPB_STARTMSG_SELECTOR); |
| 5768 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5769 | if (!startmsg) return true; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5770 | hd = upb_handlers_gethandlerdata(s->handlers, UPB_STARTMSG_SELECTOR); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5771 | return startmsg(s->closure, hd); |
| 5772 | } |
| 5773 | |
| 5774 | UPB_INLINE bool upb_sink_endmsg(upb_sink *s, upb_status *status) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5775 | typedef upb_endmsg_handlerfunc func; |
| 5776 | func *endmsg; |
| 5777 | const void *hd; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5778 | if (!s->handlers) return true; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5779 | endmsg = (func *)upb_handlers_gethandler(s->handlers, UPB_ENDMSG_SELECTOR); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5780 | |
| 5781 | if (!endmsg) return true; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5782 | hd = upb_handlers_gethandlerdata(s->handlers, UPB_ENDMSG_SELECTOR); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5783 | return endmsg(s->closure, hd, status); |
| 5784 | } |
| 5785 | |
| 5786 | UPB_INLINE bool upb_sink_startseq(upb_sink *s, upb_selector_t sel, |
| 5787 | upb_sink *sub) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5788 | typedef upb_startfield_handlerfunc func; |
| 5789 | func *startseq; |
| 5790 | const void *hd; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5791 | sub->closure = s->closure; |
| 5792 | sub->handlers = s->handlers; |
| 5793 | if (!s->handlers) return true; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5794 | startseq = (func*)upb_handlers_gethandler(s->handlers, sel); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5795 | |
| 5796 | if (!startseq) return true; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5797 | hd = upb_handlers_gethandlerdata(s->handlers, sel); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5798 | sub->closure = startseq(s->closure, hd); |
| 5799 | return sub->closure ? true : false; |
| 5800 | } |
| 5801 | |
| 5802 | UPB_INLINE bool upb_sink_endseq(upb_sink *s, upb_selector_t sel) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5803 | typedef upb_endfield_handlerfunc func; |
| 5804 | func *endseq; |
| 5805 | const void *hd; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5806 | if (!s->handlers) return true; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5807 | endseq = (func*)upb_handlers_gethandler(s->handlers, sel); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5808 | |
| 5809 | if (!endseq) return true; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5810 | hd = upb_handlers_gethandlerdata(s->handlers, sel); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5811 | return endseq(s->closure, hd); |
| 5812 | } |
| 5813 | |
| 5814 | UPB_INLINE bool upb_sink_startstr(upb_sink *s, upb_selector_t sel, |
| 5815 | size_t size_hint, upb_sink *sub) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5816 | typedef upb_startstr_handlerfunc func; |
| 5817 | func *startstr; |
| 5818 | const void *hd; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5819 | sub->closure = s->closure; |
| 5820 | sub->handlers = s->handlers; |
| 5821 | if (!s->handlers) return true; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5822 | startstr = (func*)upb_handlers_gethandler(s->handlers, sel); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5823 | |
| 5824 | if (!startstr) return true; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5825 | hd = upb_handlers_gethandlerdata(s->handlers, sel); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5826 | sub->closure = startstr(s->closure, hd, size_hint); |
| 5827 | return sub->closure ? true : false; |
| 5828 | } |
| 5829 | |
| 5830 | UPB_INLINE bool upb_sink_endstr(upb_sink *s, upb_selector_t sel) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5831 | typedef upb_endfield_handlerfunc func; |
| 5832 | func *endstr; |
| 5833 | const void *hd; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5834 | if (!s->handlers) return true; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5835 | endstr = (func*)upb_handlers_gethandler(s->handlers, sel); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5836 | |
| 5837 | if (!endstr) return true; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5838 | hd = upb_handlers_gethandlerdata(s->handlers, sel); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5839 | return endstr(s->closure, hd); |
| 5840 | } |
| 5841 | |
| 5842 | UPB_INLINE bool upb_sink_startsubmsg(upb_sink *s, upb_selector_t sel, |
| 5843 | upb_sink *sub) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5844 | typedef upb_startfield_handlerfunc func; |
| 5845 | func *startsubmsg; |
| 5846 | const void *hd; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5847 | sub->closure = s->closure; |
| 5848 | if (!s->handlers) { |
| 5849 | sub->handlers = NULL; |
| 5850 | return true; |
| 5851 | } |
| 5852 | sub->handlers = upb_handlers_getsubhandlers_sel(s->handlers, sel); |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5853 | startsubmsg = (func*)upb_handlers_gethandler(s->handlers, sel); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5854 | |
| 5855 | if (!startsubmsg) return true; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5856 | hd = upb_handlers_gethandlerdata(s->handlers, sel); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5857 | sub->closure = startsubmsg(s->closure, hd); |
| 5858 | return sub->closure ? true : false; |
| 5859 | } |
| 5860 | |
| 5861 | UPB_INLINE bool upb_sink_endsubmsg(upb_sink *s, upb_selector_t sel) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5862 | typedef upb_endfield_handlerfunc func; |
| 5863 | func *endsubmsg; |
| 5864 | const void *hd; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5865 | if (!s->handlers) return true; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5866 | endsubmsg = (func*)upb_handlers_gethandler(s->handlers, sel); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5867 | |
| 5868 | if (!endsubmsg) return s->closure; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5869 | hd = upb_handlers_gethandlerdata(s->handlers, sel); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5870 | return endsubmsg(s->closure, hd); |
| 5871 | } |
| 5872 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5873 | UPB_END_EXTERN_C |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5874 | |
| 5875 | #ifdef __cplusplus |
| 5876 | |
| 5877 | namespace upb { |
| 5878 | |
| 5879 | template <class T> Sink::Sink(const Handlers* handlers, T* closure) { |
| 5880 | upb_sink_reset(this, handlers, closure); |
| 5881 | } |
| 5882 | template <class T> |
| 5883 | inline void Sink::Reset(const Handlers* handlers, T* closure) { |
| 5884 | upb_sink_reset(this, handlers, closure); |
| 5885 | } |
| 5886 | inline bool Sink::StartMessage() { |
| 5887 | return upb_sink_startmsg(this); |
| 5888 | } |
| 5889 | inline bool Sink::EndMessage(Status* status) { |
| 5890 | return upb_sink_endmsg(this, status); |
| 5891 | } |
| 5892 | inline bool Sink::PutInt32(Handlers::Selector sel, int32_t val) { |
| 5893 | return upb_sink_putint32(this, sel, val); |
| 5894 | } |
| 5895 | inline bool Sink::PutInt64(Handlers::Selector sel, int64_t val) { |
| 5896 | return upb_sink_putint64(this, sel, val); |
| 5897 | } |
| 5898 | inline bool Sink::PutUInt32(Handlers::Selector sel, uint32_t val) { |
| 5899 | return upb_sink_putuint32(this, sel, val); |
| 5900 | } |
| 5901 | inline bool Sink::PutUInt64(Handlers::Selector sel, uint64_t val) { |
| 5902 | return upb_sink_putuint64(this, sel, val); |
| 5903 | } |
| 5904 | inline bool Sink::PutFloat(Handlers::Selector sel, float val) { |
| 5905 | return upb_sink_putfloat(this, sel, val); |
| 5906 | } |
| 5907 | inline bool Sink::PutDouble(Handlers::Selector sel, double val) { |
| 5908 | return upb_sink_putdouble(this, sel, val); |
| 5909 | } |
| 5910 | inline bool Sink::PutBool(Handlers::Selector sel, bool val) { |
| 5911 | return upb_sink_putbool(this, sel, val); |
| 5912 | } |
| 5913 | inline bool Sink::StartString(Handlers::Selector sel, size_t size_hint, |
| 5914 | Sink *sub) { |
| 5915 | return upb_sink_startstr(this, sel, size_hint, sub); |
| 5916 | } |
| 5917 | inline size_t Sink::PutStringBuffer(Handlers::Selector sel, const char *buf, |
| 5918 | size_t len, const BufferHandle* handle) { |
| 5919 | return upb_sink_putstring(this, sel, buf, len, handle); |
| 5920 | } |
| 5921 | inline bool Sink::EndString(Handlers::Selector sel) { |
| 5922 | return upb_sink_endstr(this, sel); |
| 5923 | } |
| 5924 | inline bool Sink::StartSubMessage(Handlers::Selector sel, Sink* sub) { |
| 5925 | return upb_sink_startsubmsg(this, sel, sub); |
| 5926 | } |
| 5927 | inline bool Sink::EndSubMessage(Handlers::Selector sel) { |
| 5928 | return upb_sink_endsubmsg(this, sel); |
| 5929 | } |
| 5930 | inline bool Sink::StartSequence(Handlers::Selector sel, Sink* sub) { |
| 5931 | return upb_sink_startseq(this, sel, sub); |
| 5932 | } |
| 5933 | inline bool Sink::EndSequence(Handlers::Selector sel) { |
| 5934 | return upb_sink_endseq(this, sel); |
| 5935 | } |
| 5936 | |
| 5937 | template <class T> |
| 5938 | BytesSink::BytesSink(const BytesHandler* handler, T* closure) { |
| 5939 | Reset(handler, closure); |
| 5940 | } |
| 5941 | |
| 5942 | template <class T> |
| 5943 | void BytesSink::Reset(const BytesHandler *handler, T *closure) { |
| 5944 | upb_bytessink_reset(this, handler, closure); |
| 5945 | } |
| 5946 | inline bool BytesSink::Start(size_t size_hint, void **subc) { |
| 5947 | return upb_bytessink_start(this, size_hint, subc); |
| 5948 | } |
| 5949 | inline size_t BytesSink::PutBuffer(void *subc, const char *buf, size_t len, |
| 5950 | const BufferHandle *handle) { |
| 5951 | return upb_bytessink_putbuf(this, subc, buf, len, handle); |
| 5952 | } |
| 5953 | inline bool BytesSink::End() { |
| 5954 | return upb_bytessink_end(this); |
| 5955 | } |
| 5956 | |
| 5957 | inline bool BufferSource::PutBuffer(const char *buf, size_t len, |
| 5958 | BytesSink *sink) { |
| 5959 | return upb_bufsrc_putbuf(buf, len, sink); |
| 5960 | } |
| 5961 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5962 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5963 | #endif |
| 5964 | |
| 5965 | #endif |
| 5966 | /* |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 5967 | ** For handlers that do very tiny, very simple operations, the function call |
| 5968 | ** overhead of calling a handler can be significant. This file allows the |
| 5969 | ** user to define handlers that do something very simple like store the value |
| 5970 | ** to memory and/or set a hasbit. JIT compilers can then special-case these |
| 5971 | ** handlers and emit specialized code for them instead of actually calling the |
| 5972 | ** handler. |
| 5973 | ** |
| 5974 | ** The functionality is very simple/limited right now but may expand to be able |
| 5975 | ** to call another function. |
| 5976 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5977 | |
| 5978 | #ifndef UPB_SHIM_H |
| 5979 | #define UPB_SHIM_H |
| 5980 | |
| 5981 | |
| 5982 | typedef struct { |
| 5983 | size_t offset; |
| 5984 | int32_t hasbit; |
| 5985 | } upb_shim_data; |
| 5986 | |
| 5987 | #ifdef __cplusplus |
| 5988 | |
| 5989 | namespace upb { |
| 5990 | |
| 5991 | struct Shim { |
| 5992 | typedef upb_shim_data Data; |
| 5993 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5994 | /* Sets a handler for the given field that writes the value to the given |
| 5995 | * offset and, if hasbit >= 0, sets a bit at the given bit offset. Returns |
| 5996 | * true if the handler was set successfully. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 5997 | static bool Set(Handlers *h, const FieldDef *f, size_t ofs, int32_t hasbit); |
| 5998 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 5999 | /* If this handler is a shim, returns the corresponding upb::Shim::Data and |
| 6000 | * stores the type in "type". Otherwise returns NULL. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6001 | static const Data* GetData(const Handlers* h, Handlers::Selector s, |
| 6002 | FieldDef::Type* type); |
| 6003 | }; |
| 6004 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6005 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6006 | |
| 6007 | #endif |
| 6008 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6009 | UPB_BEGIN_EXTERN_C |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6010 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6011 | /* C API. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6012 | bool upb_shim_set(upb_handlers *h, const upb_fielddef *f, size_t offset, |
| 6013 | int32_t hasbit); |
| 6014 | const upb_shim_data *upb_shim_getdata(const upb_handlers *h, upb_selector_t s, |
| 6015 | upb_fieldtype_t *type); |
| 6016 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6017 | UPB_END_EXTERN_C |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6018 | |
| 6019 | #ifdef __cplusplus |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6020 | /* C++ Wrappers. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6021 | namespace upb { |
| 6022 | inline bool Shim::Set(Handlers* h, const FieldDef* f, size_t ofs, |
| 6023 | int32_t hasbit) { |
| 6024 | return upb_shim_set(h, f, ofs, hasbit); |
| 6025 | } |
| 6026 | inline const Shim::Data* Shim::GetData(const Handlers* h, Handlers::Selector s, |
| 6027 | FieldDef::Type* type) { |
| 6028 | return upb_shim_getdata(h, s, type); |
| 6029 | } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6030 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6031 | #endif |
| 6032 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6033 | #endif /* UPB_SHIM_H */ |
| 6034 | /* |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 6035 | ** upb::SymbolTable (upb_symtab) |
| 6036 | ** |
| 6037 | ** A symtab (symbol table) stores a name->def map of upb_defs. Clients could |
| 6038 | ** always create such tables themselves, but upb_symtab has logic for resolving |
| 6039 | ** symbolic references, and in particular, for keeping a whole set of consistent |
| 6040 | ** defs when replacing some subset of those defs. This logic is nontrivial. |
| 6041 | ** |
| 6042 | ** This is a mixed C/C++ interface that offers a full API to both languages. |
| 6043 | ** See the top-level README for more information. |
| 6044 | */ |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6045 | |
| 6046 | #ifndef UPB_SYMTAB_H_ |
| 6047 | #define UPB_SYMTAB_H_ |
| 6048 | |
| 6049 | |
| 6050 | #ifdef __cplusplus |
| 6051 | #include <vector> |
| 6052 | namespace upb { class SymbolTable; } |
| 6053 | #endif |
| 6054 | |
| 6055 | UPB_DECLARE_DERIVED_TYPE(upb::SymbolTable, upb::RefCounted, |
| 6056 | upb_symtab, upb_refcounted) |
| 6057 | |
| 6058 | typedef struct { |
| 6059 | UPB_PRIVATE_FOR_CPP |
| 6060 | upb_strtable_iter iter; |
| 6061 | upb_deftype_t type; |
| 6062 | } upb_symtab_iter; |
| 6063 | |
| 6064 | #ifdef __cplusplus |
| 6065 | |
| 6066 | /* Non-const methods in upb::SymbolTable are NOT thread-safe. */ |
| 6067 | class upb::SymbolTable { |
| 6068 | public: |
| 6069 | /* Returns a new symbol table with a single ref owned by "owner." |
| 6070 | * Returns NULL if memory allocation failed. */ |
| 6071 | static reffed_ptr<SymbolTable> New(); |
| 6072 | |
| 6073 | /* Include RefCounted base methods. */ |
| 6074 | UPB_REFCOUNTED_CPPMETHODS |
| 6075 | |
| 6076 | /* For all lookup functions, the returned pointer is not owned by the |
| 6077 | * caller; it may be invalidated by any non-const call or unref of the |
| 6078 | * SymbolTable! To protect against this, take a ref if desired. */ |
| 6079 | |
| 6080 | /* Freezes the symbol table: prevents further modification of it. |
| 6081 | * After the Freeze() operation is successful, the SymbolTable must only be |
| 6082 | * accessed via a const pointer. |
| 6083 | * |
| 6084 | * Unlike with upb::MessageDef/upb::EnumDef/etc, freezing a SymbolTable is not |
| 6085 | * a necessary step in using a SymbolTable. If you have no need for it to be |
| 6086 | * immutable, there is no need to freeze it ever. However sometimes it is |
| 6087 | * useful, and SymbolTables that are statically compiled into the binary are |
| 6088 | * always frozen by nature. */ |
| 6089 | void Freeze(); |
| 6090 | |
| 6091 | /* Resolves the given symbol using the rules described in descriptor.proto, |
| 6092 | * namely: |
| 6093 | * |
| 6094 | * If the name starts with a '.', it is fully-qualified. Otherwise, |
| 6095 | * C++-like scoping rules are used to find the type (i.e. first the nested |
| 6096 | * types within this message are searched, then within the parent, on up |
| 6097 | * to the root namespace). |
| 6098 | * |
| 6099 | * If not found, returns NULL. */ |
| 6100 | const Def* Resolve(const char* base, const char* sym) const; |
| 6101 | |
| 6102 | /* Finds an entry in the symbol table with this exact name. If not found, |
| 6103 | * returns NULL. */ |
| 6104 | const Def* Lookup(const char *sym) const; |
| 6105 | const MessageDef* LookupMessage(const char *sym) const; |
| 6106 | const EnumDef* LookupEnum(const char *sym) const; |
| 6107 | |
| 6108 | /* TODO: introduce a C++ iterator, but make it nice and templated so that if |
| 6109 | * you ask for an iterator of MessageDef the iterated elements are strongly |
| 6110 | * typed as MessageDef*. */ |
| 6111 | |
| 6112 | /* Adds the given mutable defs to the symtab, resolving all symbols |
| 6113 | * (including enum default values) and finalizing the defs. Only one def per |
| 6114 | * name may be in the list, but defs can replace existing defs in the symtab. |
| 6115 | * All defs must have a name -- anonymous defs are not allowed. Anonymous |
| 6116 | * defs can still be frozen by calling upb_def_freeze() directly. |
| 6117 | * |
| 6118 | * Any existing defs that can reach defs that are being replaced will |
| 6119 | * themselves be replaced also, so that the resulting set of defs is fully |
| 6120 | * consistent. |
| 6121 | * |
| 6122 | * This logic implemented in this method is a convenience; ultimately it |
| 6123 | * calls some combination of upb_fielddef_setsubdef(), upb_def_dup(), and |
| 6124 | * upb_freeze(), any of which the client could call themself. However, since |
| 6125 | * the logic for doing so is nontrivial, we provide it here. |
| 6126 | * |
| 6127 | * The entire operation either succeeds or fails. If the operation fails, |
| 6128 | * the symtab is unchanged, false is returned, and status indicates the |
| 6129 | * error. The caller passes a ref on all defs to the symtab (even if the |
| 6130 | * operation fails). |
| 6131 | * |
| 6132 | * TODO(haberman): currently failure will leave the symtab unchanged, but may |
| 6133 | * leave the defs themselves partially resolved. Does this matter? If so we |
| 6134 | * could do a prepass that ensures that all symbols are resolvable and bail |
| 6135 | * if not, so we don't mutate anything until we know the operation will |
| 6136 | * succeed. |
| 6137 | * |
| 6138 | * TODO(haberman): since the defs must be mutable, refining a frozen def |
| 6139 | * requires making mutable copies of the entire tree. This is wasteful if |
| 6140 | * only a few messages are changing. We may want to add a way of adding a |
| 6141 | * tree of frozen defs to the symtab (perhaps an alternate constructor where |
| 6142 | * you pass the root of the tree?) */ |
| 6143 | bool Add(Def*const* defs, int n, void* ref_donor, upb_status* status); |
| 6144 | |
| 6145 | bool Add(const std::vector<Def*>& defs, void *owner, Status* status) { |
| 6146 | return Add((Def*const*)&defs[0], defs.size(), owner, status); |
| 6147 | } |
| 6148 | |
| 6149 | private: |
| 6150 | UPB_DISALLOW_POD_OPS(SymbolTable, upb::SymbolTable) |
| 6151 | }; |
| 6152 | |
| 6153 | #endif /* __cplusplus */ |
| 6154 | |
| 6155 | UPB_BEGIN_EXTERN_C |
| 6156 | |
| 6157 | /* Native C API. */ |
| 6158 | |
| 6159 | /* Include refcounted methods like upb_symtab_ref(). */ |
| 6160 | UPB_REFCOUNTED_CMETHODS(upb_symtab, upb_symtab_upcast) |
| 6161 | |
| 6162 | upb_symtab *upb_symtab_new(const void *owner); |
| 6163 | void upb_symtab_freeze(upb_symtab *s); |
| 6164 | const upb_def *upb_symtab_resolve(const upb_symtab *s, const char *base, |
| 6165 | const char *sym); |
| 6166 | const upb_def *upb_symtab_lookup(const upb_symtab *s, const char *sym); |
| 6167 | const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym); |
| 6168 | const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym); |
| 6169 | bool upb_symtab_add(upb_symtab *s, upb_def *const*defs, int n, void *ref_donor, |
| 6170 | upb_status *status); |
| 6171 | |
| 6172 | /* upb_symtab_iter i; |
| 6173 | * for(upb_symtab_begin(&i, s, type); !upb_symtab_done(&i); |
| 6174 | * upb_symtab_next(&i)) { |
| 6175 | * const upb_def *def = upb_symtab_iter_def(&i); |
| 6176 | * // ... |
| 6177 | * } |
| 6178 | * |
| 6179 | * For C we don't have separate iterators for const and non-const. |
| 6180 | * It is the caller's responsibility to cast the upb_fielddef* to |
| 6181 | * const if the upb_msgdef* is const. */ |
| 6182 | void upb_symtab_begin(upb_symtab_iter *iter, const upb_symtab *s, |
| 6183 | upb_deftype_t type); |
| 6184 | void upb_symtab_next(upb_symtab_iter *iter); |
| 6185 | bool upb_symtab_done(const upb_symtab_iter *iter); |
| 6186 | const upb_def *upb_symtab_iter_def(const upb_symtab_iter *iter); |
| 6187 | |
| 6188 | UPB_END_EXTERN_C |
| 6189 | |
| 6190 | #ifdef __cplusplus |
| 6191 | /* C++ inline wrappers. */ |
| 6192 | namespace upb { |
| 6193 | inline reffed_ptr<SymbolTable> SymbolTable::New() { |
| 6194 | upb_symtab *s = upb_symtab_new(&s); |
| 6195 | return reffed_ptr<SymbolTable>(s, &s); |
| 6196 | } |
| 6197 | |
| 6198 | inline void SymbolTable::Freeze() { |
| 6199 | return upb_symtab_freeze(this); |
| 6200 | } |
| 6201 | inline const Def *SymbolTable::Resolve(const char *base, |
| 6202 | const char *sym) const { |
| 6203 | return upb_symtab_resolve(this, base, sym); |
| 6204 | } |
| 6205 | inline const Def* SymbolTable::Lookup(const char *sym) const { |
| 6206 | return upb_symtab_lookup(this, sym); |
| 6207 | } |
| 6208 | inline const MessageDef *SymbolTable::LookupMessage(const char *sym) const { |
| 6209 | return upb_symtab_lookupmsg(this, sym); |
| 6210 | } |
| 6211 | inline bool SymbolTable::Add( |
| 6212 | Def*const* defs, int n, void* ref_donor, upb_status* status) { |
| 6213 | return upb_symtab_add(this, (upb_def*const*)defs, n, ref_donor, status); |
| 6214 | } |
| 6215 | } /* namespace upb */ |
| 6216 | #endif |
| 6217 | |
| 6218 | #endif /* UPB_SYMTAB_H_ */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6219 | /* |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 6220 | ** upb::descriptor::Reader (upb_descreader) |
| 6221 | ** |
| 6222 | ** Provides a way of building upb::Defs from data in descriptor.proto format. |
| 6223 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6224 | |
| 6225 | #ifndef UPB_DESCRIPTOR_H |
| 6226 | #define UPB_DESCRIPTOR_H |
| 6227 | |
| 6228 | |
| 6229 | #ifdef __cplusplus |
| 6230 | namespace upb { |
| 6231 | namespace descriptor { |
| 6232 | class Reader; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6233 | } /* namespace descriptor */ |
| 6234 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6235 | #endif |
| 6236 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6237 | UPB_DECLARE_TYPE(upb::descriptor::Reader, upb_descreader) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6238 | |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 6239 | #ifdef __cplusplus |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6240 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6241 | /* Class that receives descriptor data according to the descriptor.proto schema |
| 6242 | * and use it to build upb::Defs corresponding to that schema. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 6243 | class upb::descriptor::Reader { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6244 | public: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6245 | /* These handlers must have come from NewHandlers() and must outlive the |
| 6246 | * Reader. |
| 6247 | * |
| 6248 | * TODO: generate the handlers statically (like we do with the |
| 6249 | * descriptor.proto defs) so that there is no need to pass this parameter (or |
| 6250 | * to build/memory-manage the handlers at runtime at all). Unfortunately this |
| 6251 | * is a bit tricky to implement for Handlers, but necessary to simplify this |
| 6252 | * interface. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 6253 | static Reader* Create(Environment* env, const Handlers* handlers); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6254 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6255 | /* The reader's input; this is where descriptor.proto data should be sent. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6256 | Sink* input(); |
| 6257 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6258 | /* Returns an array of all defs that have been parsed, and transfers ownership |
| 6259 | * of them to "owner". The number of defs is stored in *n. Ownership of the |
| 6260 | * returned array is retained and is invalidated by any other call into |
| 6261 | * Reader. |
| 6262 | * |
| 6263 | * These defs are not frozen or resolved; they are ready to be added to a |
| 6264 | * symtab. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6265 | upb::Def** GetDefs(void* owner, int* n); |
| 6266 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6267 | /* Builds and returns handlers for the reader, owned by "owner." */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6268 | static Handlers* NewHandlers(const void* owner); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6269 | |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 6270 | private: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6271 | UPB_DISALLOW_POD_OPS(Reader, upb::descriptor::Reader) |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 6272 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6273 | |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 6274 | #endif |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6275 | |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 6276 | UPB_BEGIN_EXTERN_C |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6277 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6278 | /* C API. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 6279 | upb_descreader *upb_descreader_create(upb_env *e, const upb_handlers *h); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6280 | upb_sink *upb_descreader_input(upb_descreader *r); |
| 6281 | upb_def **upb_descreader_getdefs(upb_descreader *r, void *owner, int *n); |
| 6282 | const upb_handlers *upb_descreader_newhandlers(const void *owner); |
| 6283 | |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 6284 | UPB_END_EXTERN_C |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6285 | |
| 6286 | #ifdef __cplusplus |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6287 | /* C++ implementation details. ************************************************/ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6288 | namespace upb { |
| 6289 | namespace descriptor { |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 6290 | inline Reader* Reader::Create(Environment* e, const Handlers *h) { |
| 6291 | return upb_descreader_create(e, h); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6292 | } |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6293 | inline Sink* Reader::input() { return upb_descreader_input(this); } |
| 6294 | inline upb::Def** Reader::GetDefs(void* owner, int* n) { |
| 6295 | return upb_descreader_getdefs(this, owner, n); |
| 6296 | } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6297 | } /* namespace descriptor */ |
| 6298 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 6299 | #endif |
| 6300 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6301 | #endif /* UPB_DESCRIPTOR_H */ |
| 6302 | /* This file contains accessors for a set of compiled-in defs. |
| 6303 | * Note that unlike Google's protobuf, it does *not* define |
| 6304 | * generated classes or any other kind of data structure for |
| 6305 | * actually storing protobufs. It only contains *defs* which |
| 6306 | * let you reflect over a protobuf *schema*. |
| 6307 | */ |
| 6308 | /* This file was generated by upbc (the upb compiler). |
| 6309 | * Do not edit -- your changes will be discarded when the file is |
| 6310 | * regenerated. */ |
| 6311 | |
| 6312 | #ifndef GOOGLE_PROTOBUF_DESCRIPTOR_UPB_H_ |
| 6313 | #define GOOGLE_PROTOBUF_DESCRIPTOR_UPB_H_ |
| 6314 | |
| 6315 | |
| 6316 | #ifdef __cplusplus |
| 6317 | UPB_BEGIN_EXTERN_C |
| 6318 | #endif |
| 6319 | |
| 6320 | /* Enums */ |
| 6321 | |
| 6322 | typedef enum { |
| 6323 | GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_OPTIONAL = 1, |
| 6324 | GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_REQUIRED = 2, |
| 6325 | GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_REPEATED = 3 |
| 6326 | } google_protobuf_FieldDescriptorProto_Label; |
| 6327 | |
| 6328 | typedef enum { |
| 6329 | GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_DOUBLE = 1, |
| 6330 | GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_FLOAT = 2, |
| 6331 | GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_INT64 = 3, |
| 6332 | GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_UINT64 = 4, |
| 6333 | GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_INT32 = 5, |
| 6334 | GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_FIXED64 = 6, |
| 6335 | GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_FIXED32 = 7, |
| 6336 | GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_BOOL = 8, |
| 6337 | GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_STRING = 9, |
| 6338 | GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_GROUP = 10, |
| 6339 | GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_MESSAGE = 11, |
| 6340 | GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_BYTES = 12, |
| 6341 | GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_UINT32 = 13, |
| 6342 | GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_ENUM = 14, |
| 6343 | GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SFIXED32 = 15, |
| 6344 | GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SFIXED64 = 16, |
| 6345 | GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SINT32 = 17, |
| 6346 | GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SINT64 = 18 |
| 6347 | } google_protobuf_FieldDescriptorProto_Type; |
| 6348 | |
| 6349 | typedef enum { |
| 6350 | GOOGLE_PROTOBUF_FIELDOPTIONS_STRING = 0, |
| 6351 | GOOGLE_PROTOBUF_FIELDOPTIONS_CORD = 1, |
| 6352 | GOOGLE_PROTOBUF_FIELDOPTIONS_STRING_PIECE = 2 |
| 6353 | } google_protobuf_FieldOptions_CType; |
| 6354 | |
| 6355 | typedef enum { |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6356 | GOOGLE_PROTOBUF_FIELDOPTIONS_JS_NORMAL = 0, |
| 6357 | GOOGLE_PROTOBUF_FIELDOPTIONS_JS_STRING = 1, |
| 6358 | GOOGLE_PROTOBUF_FIELDOPTIONS_JS_NUMBER = 2 |
| 6359 | } google_protobuf_FieldOptions_JSType; |
| 6360 | |
| 6361 | typedef enum { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6362 | GOOGLE_PROTOBUF_FILEOPTIONS_SPEED = 1, |
| 6363 | GOOGLE_PROTOBUF_FILEOPTIONS_CODE_SIZE = 2, |
| 6364 | GOOGLE_PROTOBUF_FILEOPTIONS_LITE_RUNTIME = 3 |
| 6365 | } google_protobuf_FileOptions_OptimizeMode; |
| 6366 | |
| 6367 | /* Selectors */ |
| 6368 | |
| 6369 | /* google.protobuf.DescriptorProto */ |
| 6370 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_FIELD_STARTSUBMSG 2 |
| 6371 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NESTED_TYPE_STARTSUBMSG 3 |
| 6372 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ENUM_TYPE_STARTSUBMSG 4 |
| 6373 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_RANGE_STARTSUBMSG 5 |
| 6374 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_STARTSUBMSG 6 |
| 6375 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_OPTIONS_STARTSUBMSG 7 |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6376 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ONEOF_DECL_STARTSUBMSG 8 |
| 6377 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_RESERVED_RANGE_STARTSUBMSG 9 |
| 6378 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_FIELD_STARTSEQ 10 |
| 6379 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_FIELD_ENDSEQ 11 |
| 6380 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_FIELD_ENDSUBMSG 12 |
| 6381 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NESTED_TYPE_STARTSEQ 13 |
| 6382 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NESTED_TYPE_ENDSEQ 14 |
| 6383 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NESTED_TYPE_ENDSUBMSG 15 |
| 6384 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ENUM_TYPE_STARTSEQ 16 |
| 6385 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ENUM_TYPE_ENDSEQ 17 |
| 6386 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ENUM_TYPE_ENDSUBMSG 18 |
| 6387 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_RANGE_STARTSEQ 19 |
| 6388 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_RANGE_ENDSEQ 20 |
| 6389 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_RANGE_ENDSUBMSG 21 |
| 6390 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_STARTSEQ 22 |
| 6391 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_ENDSEQ 23 |
| 6392 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_ENDSUBMSG 24 |
| 6393 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_OPTIONS_ENDSUBMSG 25 |
| 6394 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ONEOF_DECL_STARTSEQ 26 |
| 6395 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ONEOF_DECL_ENDSEQ 27 |
| 6396 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ONEOF_DECL_ENDSUBMSG 28 |
| 6397 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_RESERVED_RANGE_STARTSEQ 29 |
| 6398 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_RESERVED_RANGE_ENDSEQ 30 |
| 6399 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_RESERVED_RANGE_ENDSUBMSG 31 |
| 6400 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NAME_STRING 32 |
| 6401 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NAME_STARTSTR 33 |
| 6402 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NAME_ENDSTR 34 |
| 6403 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_RESERVED_NAME_STARTSEQ 35 |
| 6404 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_RESERVED_NAME_ENDSEQ 36 |
| 6405 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_RESERVED_NAME_STRING 37 |
| 6406 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_RESERVED_NAME_STARTSTR 38 |
| 6407 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_RESERVED_NAME_ENDSTR 39 |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6408 | |
| 6409 | /* google.protobuf.DescriptorProto.ExtensionRange */ |
| 6410 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSIONRANGE_START_INT32 2 |
| 6411 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSIONRANGE_END_INT32 3 |
| 6412 | |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6413 | /* google.protobuf.DescriptorProto.ReservedRange */ |
| 6414 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_RESERVEDRANGE_START_INT32 2 |
| 6415 | #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_RESERVEDRANGE_END_INT32 3 |
| 6416 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6417 | /* google.protobuf.EnumDescriptorProto */ |
| 6418 | #define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_VALUE_STARTSUBMSG 2 |
| 6419 | #define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 3 |
| 6420 | #define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_VALUE_STARTSEQ 4 |
| 6421 | #define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_VALUE_ENDSEQ 5 |
| 6422 | #define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_VALUE_ENDSUBMSG 6 |
| 6423 | #define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 7 |
| 6424 | #define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_NAME_STRING 8 |
| 6425 | #define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_NAME_STARTSTR 9 |
| 6426 | #define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_NAME_ENDSTR 10 |
| 6427 | |
| 6428 | /* google.protobuf.EnumOptions */ |
| 6429 | #define SEL_GOOGLE_PROTOBUF_ENUMOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2 |
| 6430 | #define SEL_GOOGLE_PROTOBUF_ENUMOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3 |
| 6431 | #define SEL_GOOGLE_PROTOBUF_ENUMOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4 |
| 6432 | #define SEL_GOOGLE_PROTOBUF_ENUMOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5 |
| 6433 | #define SEL_GOOGLE_PROTOBUF_ENUMOPTIONS_ALLOW_ALIAS_BOOL 6 |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6434 | #define SEL_GOOGLE_PROTOBUF_ENUMOPTIONS_DEPRECATED_BOOL 7 |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6435 | |
| 6436 | /* google.protobuf.EnumValueDescriptorProto */ |
| 6437 | #define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 2 |
| 6438 | #define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 3 |
| 6439 | #define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_NAME_STRING 4 |
| 6440 | #define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_NAME_STARTSTR 5 |
| 6441 | #define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_NAME_ENDSTR 6 |
| 6442 | #define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_NUMBER_INT32 7 |
| 6443 | |
| 6444 | /* google.protobuf.EnumValueOptions */ |
| 6445 | #define SEL_GOOGLE_PROTOBUF_ENUMVALUEOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2 |
| 6446 | #define SEL_GOOGLE_PROTOBUF_ENUMVALUEOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3 |
| 6447 | #define SEL_GOOGLE_PROTOBUF_ENUMVALUEOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4 |
| 6448 | #define SEL_GOOGLE_PROTOBUF_ENUMVALUEOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5 |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6449 | #define SEL_GOOGLE_PROTOBUF_ENUMVALUEOPTIONS_DEPRECATED_BOOL 6 |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6450 | |
| 6451 | /* google.protobuf.FieldDescriptorProto */ |
| 6452 | #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 2 |
| 6453 | #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 3 |
| 6454 | #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_NAME_STRING 4 |
| 6455 | #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_NAME_STARTSTR 5 |
| 6456 | #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_NAME_ENDSTR 6 |
| 6457 | #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_EXTENDEE_STRING 7 |
| 6458 | #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_EXTENDEE_STARTSTR 8 |
| 6459 | #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_EXTENDEE_ENDSTR 9 |
| 6460 | #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_NUMBER_INT32 10 |
| 6461 | #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_INT32 11 |
| 6462 | #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_INT32 12 |
| 6463 | #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_NAME_STRING 13 |
| 6464 | #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_NAME_STARTSTR 14 |
| 6465 | #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_NAME_ENDSTR 15 |
| 6466 | #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_DEFAULT_VALUE_STRING 16 |
| 6467 | #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_DEFAULT_VALUE_STARTSTR 17 |
| 6468 | #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_DEFAULT_VALUE_ENDSTR 18 |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6469 | #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_ONEOF_INDEX_INT32 19 |
| 6470 | #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_JSON_NAME_STRING 20 |
| 6471 | #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_JSON_NAME_STARTSTR 21 |
| 6472 | #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_JSON_NAME_ENDSTR 22 |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6473 | |
| 6474 | /* google.protobuf.FieldOptions */ |
| 6475 | #define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2 |
| 6476 | #define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3 |
| 6477 | #define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4 |
| 6478 | #define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5 |
| 6479 | #define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_CTYPE_INT32 6 |
| 6480 | #define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_PACKED_BOOL 7 |
| 6481 | #define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_DEPRECATED_BOOL 8 |
| 6482 | #define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_LAZY_BOOL 9 |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6483 | #define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_JSTYPE_INT32 10 |
| 6484 | #define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_WEAK_BOOL 11 |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6485 | |
| 6486 | /* google.protobuf.FileDescriptorProto */ |
| 6487 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_MESSAGE_TYPE_STARTSUBMSG 2 |
| 6488 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_ENUM_TYPE_STARTSUBMSG 3 |
| 6489 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SERVICE_STARTSUBMSG 4 |
| 6490 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_EXTENSION_STARTSUBMSG 5 |
| 6491 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 6 |
| 6492 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SOURCE_CODE_INFO_STARTSUBMSG 7 |
| 6493 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_MESSAGE_TYPE_STARTSEQ 8 |
| 6494 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_MESSAGE_TYPE_ENDSEQ 9 |
| 6495 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_MESSAGE_TYPE_ENDSUBMSG 10 |
| 6496 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_ENUM_TYPE_STARTSEQ 11 |
| 6497 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_ENUM_TYPE_ENDSEQ 12 |
| 6498 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_ENUM_TYPE_ENDSUBMSG 13 |
| 6499 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SERVICE_STARTSEQ 14 |
| 6500 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SERVICE_ENDSEQ 15 |
| 6501 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SERVICE_ENDSUBMSG 16 |
| 6502 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_EXTENSION_STARTSEQ 17 |
| 6503 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_EXTENSION_ENDSEQ 18 |
| 6504 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_EXTENSION_ENDSUBMSG 19 |
| 6505 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 20 |
| 6506 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SOURCE_CODE_INFO_ENDSUBMSG 21 |
| 6507 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_NAME_STRING 22 |
| 6508 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_NAME_STARTSTR 23 |
| 6509 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_NAME_ENDSTR 24 |
| 6510 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PACKAGE_STRING 25 |
| 6511 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PACKAGE_STARTSTR 26 |
| 6512 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PACKAGE_ENDSTR 27 |
| 6513 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_DEPENDENCY_STARTSEQ 28 |
| 6514 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_DEPENDENCY_ENDSEQ 29 |
| 6515 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_DEPENDENCY_STRING 30 |
| 6516 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_DEPENDENCY_STARTSTR 31 |
| 6517 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_DEPENDENCY_ENDSTR 32 |
| 6518 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PUBLIC_DEPENDENCY_STARTSEQ 33 |
| 6519 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PUBLIC_DEPENDENCY_ENDSEQ 34 |
| 6520 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PUBLIC_DEPENDENCY_INT32 35 |
| 6521 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_WEAK_DEPENDENCY_STARTSEQ 36 |
| 6522 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_WEAK_DEPENDENCY_ENDSEQ 37 |
| 6523 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_WEAK_DEPENDENCY_INT32 38 |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6524 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SYNTAX_STRING 39 |
| 6525 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SYNTAX_STARTSTR 40 |
| 6526 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SYNTAX_ENDSTR 41 |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6527 | |
| 6528 | /* google.protobuf.FileDescriptorSet */ |
| 6529 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORSET_FILE_STARTSUBMSG 2 |
| 6530 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORSET_FILE_STARTSEQ 3 |
| 6531 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORSET_FILE_ENDSEQ 4 |
| 6532 | #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORSET_FILE_ENDSUBMSG 5 |
| 6533 | |
| 6534 | /* google.protobuf.FileOptions */ |
| 6535 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2 |
| 6536 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3 |
| 6537 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4 |
| 6538 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5 |
| 6539 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_PACKAGE_STRING 6 |
| 6540 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_PACKAGE_STARTSTR 7 |
| 6541 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_PACKAGE_ENDSTR 8 |
| 6542 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_OUTER_CLASSNAME_STRING 9 |
| 6543 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_OUTER_CLASSNAME_STARTSTR 10 |
| 6544 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_OUTER_CLASSNAME_ENDSTR 11 |
| 6545 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_OPTIMIZE_FOR_INT32 12 |
| 6546 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_MULTIPLE_FILES_BOOL 13 |
| 6547 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_GO_PACKAGE_STRING 14 |
| 6548 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_GO_PACKAGE_STARTSTR 15 |
| 6549 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_GO_PACKAGE_ENDSTR 16 |
| 6550 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_CC_GENERIC_SERVICES_BOOL 17 |
| 6551 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_GENERIC_SERVICES_BOOL 18 |
| 6552 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_PY_GENERIC_SERVICES_BOOL 19 |
| 6553 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_GENERATE_EQUALS_AND_HASH_BOOL 20 |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6554 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_DEPRECATED_BOOL 21 |
| 6555 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_STRING_CHECK_UTF8_BOOL 22 |
| 6556 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_CC_ENABLE_ARENAS_BOOL 23 |
| 6557 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_OBJC_CLASS_PREFIX_STRING 24 |
| 6558 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_OBJC_CLASS_PREFIX_STARTSTR 25 |
| 6559 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_OBJC_CLASS_PREFIX_ENDSTR 26 |
| 6560 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_CSHARP_NAMESPACE_STRING 27 |
| 6561 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_CSHARP_NAMESPACE_STARTSTR 28 |
| 6562 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_CSHARP_NAMESPACE_ENDSTR 29 |
| 6563 | #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVANANO_USE_DEPRECATED_PACKAGE_BOOL 30 |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6564 | |
| 6565 | /* google.protobuf.MessageOptions */ |
| 6566 | #define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2 |
| 6567 | #define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3 |
| 6568 | #define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4 |
| 6569 | #define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5 |
| 6570 | #define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_MESSAGE_SET_WIRE_FORMAT_BOOL 6 |
| 6571 | #define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_NO_STANDARD_DESCRIPTOR_ACCESSOR_BOOL 7 |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6572 | #define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_DEPRECATED_BOOL 8 |
| 6573 | #define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_MAP_ENTRY_BOOL 9 |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6574 | |
| 6575 | /* google.protobuf.MethodDescriptorProto */ |
| 6576 | #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 2 |
| 6577 | #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 3 |
| 6578 | #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_NAME_STRING 4 |
| 6579 | #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_NAME_STARTSTR 5 |
| 6580 | #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_NAME_ENDSTR 6 |
| 6581 | #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_INPUT_TYPE_STRING 7 |
| 6582 | #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_INPUT_TYPE_STARTSTR 8 |
| 6583 | #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_INPUT_TYPE_ENDSTR 9 |
| 6584 | #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_OUTPUT_TYPE_STRING 10 |
| 6585 | #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_OUTPUT_TYPE_STARTSTR 11 |
| 6586 | #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_OUTPUT_TYPE_ENDSTR 12 |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6587 | #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_CLIENT_STREAMING_BOOL 13 |
| 6588 | #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_SERVER_STREAMING_BOOL 14 |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6589 | |
| 6590 | /* google.protobuf.MethodOptions */ |
| 6591 | #define SEL_GOOGLE_PROTOBUF_METHODOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2 |
| 6592 | #define SEL_GOOGLE_PROTOBUF_METHODOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3 |
| 6593 | #define SEL_GOOGLE_PROTOBUF_METHODOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4 |
| 6594 | #define SEL_GOOGLE_PROTOBUF_METHODOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5 |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6595 | #define SEL_GOOGLE_PROTOBUF_METHODOPTIONS_DEPRECATED_BOOL 6 |
| 6596 | |
| 6597 | /* google.protobuf.OneofDescriptorProto */ |
| 6598 | #define SEL_GOOGLE_PROTOBUF_ONEOFDESCRIPTORPROTO_NAME_STRING 2 |
| 6599 | #define SEL_GOOGLE_PROTOBUF_ONEOFDESCRIPTORPROTO_NAME_STARTSTR 3 |
| 6600 | #define SEL_GOOGLE_PROTOBUF_ONEOFDESCRIPTORPROTO_NAME_ENDSTR 4 |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6601 | |
| 6602 | /* google.protobuf.ServiceDescriptorProto */ |
| 6603 | #define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_METHOD_STARTSUBMSG 2 |
| 6604 | #define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 3 |
| 6605 | #define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_METHOD_STARTSEQ 4 |
| 6606 | #define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_METHOD_ENDSEQ 5 |
| 6607 | #define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_METHOD_ENDSUBMSG 6 |
| 6608 | #define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 7 |
| 6609 | #define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_NAME_STRING 8 |
| 6610 | #define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_NAME_STARTSTR 9 |
| 6611 | #define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_NAME_ENDSTR 10 |
| 6612 | |
| 6613 | /* google.protobuf.ServiceOptions */ |
| 6614 | #define SEL_GOOGLE_PROTOBUF_SERVICEOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2 |
| 6615 | #define SEL_GOOGLE_PROTOBUF_SERVICEOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3 |
| 6616 | #define SEL_GOOGLE_PROTOBUF_SERVICEOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4 |
| 6617 | #define SEL_GOOGLE_PROTOBUF_SERVICEOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5 |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6618 | #define SEL_GOOGLE_PROTOBUF_SERVICEOPTIONS_DEPRECATED_BOOL 6 |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6619 | |
| 6620 | /* google.protobuf.SourceCodeInfo */ |
| 6621 | #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_STARTSUBMSG 2 |
| 6622 | #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_STARTSEQ 3 |
| 6623 | #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_ENDSEQ 4 |
| 6624 | #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_ENDSUBMSG 5 |
| 6625 | |
| 6626 | /* google.protobuf.SourceCodeInfo.Location */ |
| 6627 | #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_PATH_STARTSEQ 2 |
| 6628 | #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_PATH_ENDSEQ 3 |
| 6629 | #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_PATH_INT32 4 |
| 6630 | #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_SPAN_STARTSEQ 5 |
| 6631 | #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_SPAN_ENDSEQ 6 |
| 6632 | #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_SPAN_INT32 7 |
| 6633 | #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_LEADING_COMMENTS_STRING 8 |
| 6634 | #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_LEADING_COMMENTS_STARTSTR 9 |
| 6635 | #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_LEADING_COMMENTS_ENDSTR 10 |
| 6636 | #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_TRAILING_COMMENTS_STRING 11 |
| 6637 | #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_TRAILING_COMMENTS_STARTSTR 12 |
| 6638 | #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_TRAILING_COMMENTS_ENDSTR 13 |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6639 | #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_LEADING_DETACHED_COMMENTS_STARTSEQ 14 |
| 6640 | #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_LEADING_DETACHED_COMMENTS_ENDSEQ 15 |
| 6641 | #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_LEADING_DETACHED_COMMENTS_STRING 16 |
| 6642 | #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_LEADING_DETACHED_COMMENTS_STARTSTR 17 |
| 6643 | #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_LEADING_DETACHED_COMMENTS_ENDSTR 18 |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6644 | |
| 6645 | /* google.protobuf.UninterpretedOption */ |
| 6646 | #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAME_STARTSUBMSG 2 |
| 6647 | #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAME_STARTSEQ 3 |
| 6648 | #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAME_ENDSEQ 4 |
| 6649 | #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAME_ENDSUBMSG 5 |
| 6650 | #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_IDENTIFIER_VALUE_STRING 6 |
| 6651 | #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_IDENTIFIER_VALUE_STARTSTR 7 |
| 6652 | #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_IDENTIFIER_VALUE_ENDSTR 8 |
| 6653 | #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_POSITIVE_INT_VALUE_UINT64 9 |
| 6654 | #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NEGATIVE_INT_VALUE_INT64 10 |
| 6655 | #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_DOUBLE_VALUE_DOUBLE 11 |
| 6656 | #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_STRING_VALUE_STRING 12 |
| 6657 | #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_STRING_VALUE_STARTSTR 13 |
| 6658 | #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_STRING_VALUE_ENDSTR 14 |
| 6659 | #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_AGGREGATE_VALUE_STRING 15 |
| 6660 | #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_AGGREGATE_VALUE_STARTSTR 16 |
| 6661 | #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_AGGREGATE_VALUE_ENDSTR 17 |
| 6662 | |
| 6663 | /* google.protobuf.UninterpretedOption.NamePart */ |
| 6664 | #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAMEPART_NAME_PART_STRING 2 |
| 6665 | #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAMEPART_NAME_PART_STARTSTR 3 |
| 6666 | #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAMEPART_NAME_PART_ENDSTR 4 |
| 6667 | #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAMEPART_IS_EXTENSION_BOOL 5 |
| 6668 | |
| 6669 | const upb_symtab *upbdefs_google_protobuf_descriptor(const void *owner); |
| 6670 | |
| 6671 | /* MessageDefs */ |
| 6672 | UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_DescriptorProto(const upb_symtab *s) { |
| 6673 | const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.DescriptorProto"); |
| 6674 | assert(m); |
| 6675 | return m; |
| 6676 | } |
| 6677 | UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange(const upb_symtab *s) { |
| 6678 | const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.DescriptorProto.ExtensionRange"); |
| 6679 | assert(m); |
| 6680 | return m; |
| 6681 | } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6682 | UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ReservedRange(const upb_symtab *s) { |
| 6683 | const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.DescriptorProto.ReservedRange"); |
| 6684 | assert(m); |
| 6685 | return m; |
| 6686 | } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6687 | UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_EnumDescriptorProto(const upb_symtab *s) { |
| 6688 | const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.EnumDescriptorProto"); |
| 6689 | assert(m); |
| 6690 | return m; |
| 6691 | } |
| 6692 | UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_EnumOptions(const upb_symtab *s) { |
| 6693 | const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.EnumOptions"); |
| 6694 | assert(m); |
| 6695 | return m; |
| 6696 | } |
| 6697 | UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_EnumValueDescriptorProto(const upb_symtab *s) { |
| 6698 | const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.EnumValueDescriptorProto"); |
| 6699 | assert(m); |
| 6700 | return m; |
| 6701 | } |
| 6702 | UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_EnumValueOptions(const upb_symtab *s) { |
| 6703 | const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.EnumValueOptions"); |
| 6704 | assert(m); |
| 6705 | return m; |
| 6706 | } |
| 6707 | UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_FieldDescriptorProto(const upb_symtab *s) { |
| 6708 | const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.FieldDescriptorProto"); |
| 6709 | assert(m); |
| 6710 | return m; |
| 6711 | } |
| 6712 | UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_FieldOptions(const upb_symtab *s) { |
| 6713 | const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.FieldOptions"); |
| 6714 | assert(m); |
| 6715 | return m; |
| 6716 | } |
| 6717 | UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_FileDescriptorProto(const upb_symtab *s) { |
| 6718 | const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.FileDescriptorProto"); |
| 6719 | assert(m); |
| 6720 | return m; |
| 6721 | } |
| 6722 | UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_FileDescriptorSet(const upb_symtab *s) { |
| 6723 | const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.FileDescriptorSet"); |
| 6724 | assert(m); |
| 6725 | return m; |
| 6726 | } |
| 6727 | UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_FileOptions(const upb_symtab *s) { |
| 6728 | const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.FileOptions"); |
| 6729 | assert(m); |
| 6730 | return m; |
| 6731 | } |
| 6732 | UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_MessageOptions(const upb_symtab *s) { |
| 6733 | const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.MessageOptions"); |
| 6734 | assert(m); |
| 6735 | return m; |
| 6736 | } |
| 6737 | UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_MethodDescriptorProto(const upb_symtab *s) { |
| 6738 | const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.MethodDescriptorProto"); |
| 6739 | assert(m); |
| 6740 | return m; |
| 6741 | } |
| 6742 | UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_MethodOptions(const upb_symtab *s) { |
| 6743 | const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.MethodOptions"); |
| 6744 | assert(m); |
| 6745 | return m; |
| 6746 | } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6747 | UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_OneofDescriptorProto(const upb_symtab *s) { |
| 6748 | const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.OneofDescriptorProto"); |
| 6749 | assert(m); |
| 6750 | return m; |
| 6751 | } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6752 | UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_ServiceDescriptorProto(const upb_symtab *s) { |
| 6753 | const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.ServiceDescriptorProto"); |
| 6754 | assert(m); |
| 6755 | return m; |
| 6756 | } |
| 6757 | UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_ServiceOptions(const upb_symtab *s) { |
| 6758 | const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.ServiceOptions"); |
| 6759 | assert(m); |
| 6760 | return m; |
| 6761 | } |
| 6762 | UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo(const upb_symtab *s) { |
| 6763 | const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.SourceCodeInfo"); |
| 6764 | assert(m); |
| 6765 | return m; |
| 6766 | } |
| 6767 | UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo_Location(const upb_symtab *s) { |
| 6768 | const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.SourceCodeInfo.Location"); |
| 6769 | assert(m); |
| 6770 | return m; |
| 6771 | } |
| 6772 | UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption(const upb_symtab *s) { |
| 6773 | const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.UninterpretedOption"); |
| 6774 | assert(m); |
| 6775 | return m; |
| 6776 | } |
| 6777 | UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption_NamePart(const upb_symtab *s) { |
| 6778 | const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.UninterpretedOption.NamePart"); |
| 6779 | assert(m); |
| 6780 | return m; |
| 6781 | } |
| 6782 | |
| 6783 | |
| 6784 | /* EnumDefs */ |
| 6785 | UPB_INLINE const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Label(const upb_symtab *s) { |
| 6786 | const upb_enumdef *e = upb_symtab_lookupenum(s, "google.protobuf.FieldDescriptorProto.Label"); |
| 6787 | assert(e); |
| 6788 | return e; |
| 6789 | } |
| 6790 | UPB_INLINE const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Type(const upb_symtab *s) { |
| 6791 | const upb_enumdef *e = upb_symtab_lookupenum(s, "google.protobuf.FieldDescriptorProto.Type"); |
| 6792 | assert(e); |
| 6793 | return e; |
| 6794 | } |
| 6795 | UPB_INLINE const upb_enumdef *upbdefs_google_protobuf_FieldOptions_CType(const upb_symtab *s) { |
| 6796 | const upb_enumdef *e = upb_symtab_lookupenum(s, "google.protobuf.FieldOptions.CType"); |
| 6797 | assert(e); |
| 6798 | return e; |
| 6799 | } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6800 | UPB_INLINE const upb_enumdef *upbdefs_google_protobuf_FieldOptions_JSType(const upb_symtab *s) { |
| 6801 | const upb_enumdef *e = upb_symtab_lookupenum(s, "google.protobuf.FieldOptions.JSType"); |
| 6802 | assert(e); |
| 6803 | return e; |
| 6804 | } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6805 | UPB_INLINE const upb_enumdef *upbdefs_google_protobuf_FileOptions_OptimizeMode(const upb_symtab *s) { |
| 6806 | const upb_enumdef *e = upb_symtab_lookupenum(s, "google.protobuf.FileOptions.OptimizeMode"); |
| 6807 | assert(e); |
| 6808 | return e; |
| 6809 | } |
| 6810 | |
| 6811 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_end(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto_ExtensionRange(s), 2); } |
| 6812 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_start(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto_ExtensionRange(s), 1); } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6813 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ReservedRange_end(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto_ReservedRange(s), 2); } |
| 6814 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ReservedRange_start(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto_ReservedRange(s), 1); } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6815 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_enum_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 4); } |
| 6816 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_extension(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 6); } |
| 6817 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_extension_range(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 5); } |
| 6818 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_field(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 2); } |
| 6819 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 1); } |
| 6820 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_nested_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 3); } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6821 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_oneof_decl(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 8); } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6822 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 7); } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6823 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_reserved_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 10); } |
| 6824 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_reserved_range(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 9); } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6825 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumDescriptorProto(s), 1); } |
| 6826 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumDescriptorProto(s), 3); } |
| 6827 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumDescriptorProto(s), 2); } |
| 6828 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_allow_alias(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumOptions(s), 2); } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6829 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_deprecated(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumOptions(s), 3); } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6830 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumOptions(s), 999); } |
| 6831 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumValueDescriptorProto(s), 1); } |
| 6832 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_number(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumValueDescriptorProto(s), 2); } |
| 6833 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumValueDescriptorProto(s), 3); } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6834 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueOptions_deprecated(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumValueOptions(s), 1); } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6835 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumValueOptions(s), 999); } |
| 6836 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_default_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 7); } |
| 6837 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_extendee(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 2); } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6838 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_json_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 10); } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6839 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_label(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 4); } |
| 6840 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 1); } |
| 6841 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_number(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 3); } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6842 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_oneof_index(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 9); } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6843 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 8); } |
| 6844 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 5); } |
| 6845 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_type_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 6); } |
| 6846 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_ctype(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 1); } |
| 6847 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_deprecated(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 3); } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6848 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_jstype(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 6); } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6849 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_lazy(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 5); } |
| 6850 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_packed(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 2); } |
| 6851 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 999); } |
| 6852 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_weak(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 10); } |
| 6853 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_dependency(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 3); } |
| 6854 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_enum_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 5); } |
| 6855 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_extension(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 7); } |
| 6856 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_message_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 4); } |
| 6857 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 1); } |
| 6858 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 8); } |
| 6859 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_package(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 2); } |
| 6860 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_public_dependency(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 10); } |
| 6861 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_service(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 6); } |
| 6862 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_source_code_info(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 9); } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6863 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_syntax(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 12); } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6864 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_weak_dependency(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 11); } |
| 6865 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorSet_file(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorSet(s), 1); } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6866 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_cc_enable_arenas(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 31); } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6867 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_cc_generic_services(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 16); } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6868 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_csharp_namespace(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 37); } |
| 6869 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_deprecated(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 23); } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6870 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_go_package(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 11); } |
| 6871 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_java_generate_equals_and_hash(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 20); } |
| 6872 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_java_generic_services(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 17); } |
| 6873 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_java_multiple_files(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 10); } |
| 6874 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_java_outer_classname(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 8); } |
| 6875 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_java_package(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 1); } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6876 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_java_string_check_utf8(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 27); } |
| 6877 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_javanano_use_deprecated_package(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 38); } |
| 6878 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_objc_class_prefix(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 36); } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6879 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_optimize_for(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 9); } |
| 6880 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_py_generic_services(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 18); } |
| 6881 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 999); } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6882 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_deprecated(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MessageOptions(s), 3); } |
| 6883 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_map_entry(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MessageOptions(s), 7); } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6884 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_message_set_wire_format(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MessageOptions(s), 1); } |
| 6885 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_no_standard_descriptor_accessor(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MessageOptions(s), 2); } |
| 6886 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MessageOptions(s), 999); } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6887 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_client_streaming(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodDescriptorProto(s), 5); } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6888 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_input_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodDescriptorProto(s), 2); } |
| 6889 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodDescriptorProto(s), 1); } |
| 6890 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodDescriptorProto(s), 4); } |
| 6891 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_output_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodDescriptorProto(s), 3); } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6892 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_server_streaming(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodDescriptorProto(s), 6); } |
| 6893 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodOptions_deprecated(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodOptions(s), 33); } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6894 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodOptions(s), 999); } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6895 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_OneofDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_OneofDescriptorProto(s), 1); } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6896 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_method(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_ServiceDescriptorProto(s), 2); } |
| 6897 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_ServiceDescriptorProto(s), 1); } |
| 6898 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_ServiceDescriptorProto(s), 3); } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6899 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceOptions_deprecated(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_ServiceOptions(s), 33); } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6900 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_ServiceOptions(s), 999); } |
| 6901 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_leading_comments(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_SourceCodeInfo_Location(s), 3); } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6902 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_leading_detached_comments(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_SourceCodeInfo_Location(s), 6); } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6903 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_path(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_SourceCodeInfo_Location(s), 1); } |
| 6904 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_span(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_SourceCodeInfo_Location(s), 2); } |
| 6905 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_trailing_comments(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_SourceCodeInfo_Location(s), 4); } |
| 6906 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_location(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_SourceCodeInfo(s), 1); } |
| 6907 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_NamePart_is_extension(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption_NamePart(s), 2); } |
| 6908 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_NamePart_name_part(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption_NamePart(s), 1); } |
| 6909 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_aggregate_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 8); } |
| 6910 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_double_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 6); } |
| 6911 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_identifier_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 3); } |
| 6912 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 2); } |
| 6913 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_negative_int_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 5); } |
| 6914 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_positive_int_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 4); } |
| 6915 | UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_string_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 7); } |
| 6916 | |
| 6917 | UPB_END_EXTERN_C |
| 6918 | |
| 6919 | #ifdef __cplusplus |
| 6920 | |
| 6921 | namespace upbdefs { |
| 6922 | namespace google { |
| 6923 | namespace protobuf { |
| 6924 | namespace descriptor { |
| 6925 | inline upb::reffed_ptr<const upb::SymbolTable> SymbolTable() { |
| 6926 | const upb::SymbolTable* s = upbdefs_google_protobuf_descriptor(&s); |
| 6927 | return upb::reffed_ptr<const upb::SymbolTable>(s, &s); |
| 6928 | } |
| 6929 | } /* namespace descriptor */ |
| 6930 | } /* namespace protobuf */ |
| 6931 | } /* namespace google */ |
| 6932 | |
| 6933 | #define RETURN_REFFED(type, func) \ |
| 6934 | const type* obj = func(upbdefs::google::protobuf::descriptor::SymbolTable().get()); \ |
| 6935 | return upb::reffed_ptr<const type>(obj); |
| 6936 | |
| 6937 | namespace google { |
| 6938 | namespace protobuf { |
| 6939 | namespace DescriptorProto { |
| 6940 | inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_DescriptorProto) } |
| 6941 | inline upb::reffed_ptr<const upb::FieldDef> enum_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_enum_type) } |
| 6942 | inline upb::reffed_ptr<const upb::FieldDef> extension() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_extension) } |
| 6943 | inline upb::reffed_ptr<const upb::FieldDef> extension_range() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_extension_range) } |
| 6944 | inline upb::reffed_ptr<const upb::FieldDef> field() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_field) } |
| 6945 | inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_name) } |
| 6946 | inline upb::reffed_ptr<const upb::FieldDef> nested_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_nested_type) } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6947 | inline upb::reffed_ptr<const upb::FieldDef> oneof_decl() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_oneof_decl) } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6948 | inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_options) } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6949 | inline upb::reffed_ptr<const upb::FieldDef> reserved_name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_reserved_name) } |
| 6950 | inline upb::reffed_ptr<const upb::FieldDef> reserved_range() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_reserved_range) } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6951 | } /* namespace DescriptorProto */ |
| 6952 | } /* namespace protobuf */ |
| 6953 | } /* namespace google */ |
| 6954 | |
| 6955 | namespace google { |
| 6956 | namespace protobuf { |
| 6957 | namespace DescriptorProto { |
| 6958 | namespace ExtensionRange { |
| 6959 | inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_DescriptorProto_ExtensionRange) } |
| 6960 | inline upb::reffed_ptr<const upb::FieldDef> end() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_ExtensionRange_end) } |
| 6961 | inline upb::reffed_ptr<const upb::FieldDef> start() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_ExtensionRange_start) } |
| 6962 | } /* namespace ExtensionRange */ |
| 6963 | } /* namespace DescriptorProto */ |
| 6964 | } /* namespace protobuf */ |
| 6965 | } /* namespace google */ |
| 6966 | |
| 6967 | namespace google { |
| 6968 | namespace protobuf { |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6969 | namespace DescriptorProto { |
| 6970 | namespace ReservedRange { |
| 6971 | inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_DescriptorProto_ReservedRange) } |
| 6972 | inline upb::reffed_ptr<const upb::FieldDef> end() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_ReservedRange_end) } |
| 6973 | inline upb::reffed_ptr<const upb::FieldDef> start() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_ReservedRange_start) } |
| 6974 | } /* namespace ReservedRange */ |
| 6975 | } /* namespace DescriptorProto */ |
| 6976 | } /* namespace protobuf */ |
| 6977 | } /* namespace google */ |
| 6978 | |
| 6979 | namespace google { |
| 6980 | namespace protobuf { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6981 | namespace EnumDescriptorProto { |
| 6982 | inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_EnumDescriptorProto) } |
| 6983 | inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumDescriptorProto_name) } |
| 6984 | inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumDescriptorProto_options) } |
| 6985 | inline upb::reffed_ptr<const upb::FieldDef> value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumDescriptorProto_value) } |
| 6986 | } /* namespace EnumDescriptorProto */ |
| 6987 | } /* namespace protobuf */ |
| 6988 | } /* namespace google */ |
| 6989 | |
| 6990 | namespace google { |
| 6991 | namespace protobuf { |
| 6992 | namespace EnumOptions { |
| 6993 | inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_EnumOptions) } |
| 6994 | inline upb::reffed_ptr<const upb::FieldDef> allow_alias() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumOptions_allow_alias) } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 6995 | inline upb::reffed_ptr<const upb::FieldDef> deprecated() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumOptions_deprecated) } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 6996 | inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumOptions_uninterpreted_option) } |
| 6997 | } /* namespace EnumOptions */ |
| 6998 | } /* namespace protobuf */ |
| 6999 | } /* namespace google */ |
| 7000 | |
| 7001 | namespace google { |
| 7002 | namespace protobuf { |
| 7003 | namespace EnumValueDescriptorProto { |
| 7004 | inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_EnumValueDescriptorProto) } |
| 7005 | inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumValueDescriptorProto_name) } |
| 7006 | inline upb::reffed_ptr<const upb::FieldDef> number() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumValueDescriptorProto_number) } |
| 7007 | inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumValueDescriptorProto_options) } |
| 7008 | } /* namespace EnumValueDescriptorProto */ |
| 7009 | } /* namespace protobuf */ |
| 7010 | } /* namespace google */ |
| 7011 | |
| 7012 | namespace google { |
| 7013 | namespace protobuf { |
| 7014 | namespace EnumValueOptions { |
| 7015 | inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_EnumValueOptions) } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 7016 | inline upb::reffed_ptr<const upb::FieldDef> deprecated() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumValueOptions_deprecated) } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7017 | inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumValueOptions_uninterpreted_option) } |
| 7018 | } /* namespace EnumValueOptions */ |
| 7019 | } /* namespace protobuf */ |
| 7020 | } /* namespace google */ |
| 7021 | |
| 7022 | namespace google { |
| 7023 | namespace protobuf { |
| 7024 | namespace FieldDescriptorProto { |
| 7025 | inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_FieldDescriptorProto) } |
| 7026 | inline upb::reffed_ptr<const upb::FieldDef> default_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_default_value) } |
| 7027 | inline upb::reffed_ptr<const upb::FieldDef> extendee() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_extendee) } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 7028 | inline upb::reffed_ptr<const upb::FieldDef> json_name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_json_name) } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7029 | inline upb::reffed_ptr<const upb::FieldDef> label() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_label) } |
| 7030 | inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_name) } |
| 7031 | inline upb::reffed_ptr<const upb::FieldDef> number() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_number) } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 7032 | inline upb::reffed_ptr<const upb::FieldDef> oneof_index() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_oneof_index) } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7033 | inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_options) } |
| 7034 | inline upb::reffed_ptr<const upb::FieldDef> type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_type) } |
| 7035 | inline upb::reffed_ptr<const upb::FieldDef> type_name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_type_name) } |
| 7036 | inline upb::reffed_ptr<const upb::EnumDef> Label() { RETURN_REFFED(upb::EnumDef, upbdefs_google_protobuf_FieldDescriptorProto_Label) } |
| 7037 | inline upb::reffed_ptr<const upb::EnumDef> Type() { RETURN_REFFED(upb::EnumDef, upbdefs_google_protobuf_FieldDescriptorProto_Type) } |
| 7038 | } /* namespace FieldDescriptorProto */ |
| 7039 | } /* namespace protobuf */ |
| 7040 | } /* namespace google */ |
| 7041 | |
| 7042 | namespace google { |
| 7043 | namespace protobuf { |
| 7044 | namespace FieldOptions { |
| 7045 | inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_FieldOptions) } |
| 7046 | inline upb::reffed_ptr<const upb::FieldDef> ctype() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_ctype) } |
| 7047 | inline upb::reffed_ptr<const upb::FieldDef> deprecated() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_deprecated) } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 7048 | inline upb::reffed_ptr<const upb::FieldDef> jstype() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_jstype) } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7049 | inline upb::reffed_ptr<const upb::FieldDef> lazy() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_lazy) } |
| 7050 | inline upb::reffed_ptr<const upb::FieldDef> packed() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_packed) } |
| 7051 | inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_uninterpreted_option) } |
| 7052 | inline upb::reffed_ptr<const upb::FieldDef> weak() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_weak) } |
| 7053 | inline upb::reffed_ptr<const upb::EnumDef> CType() { RETURN_REFFED(upb::EnumDef, upbdefs_google_protobuf_FieldOptions_CType) } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 7054 | inline upb::reffed_ptr<const upb::EnumDef> JSType() { RETURN_REFFED(upb::EnumDef, upbdefs_google_protobuf_FieldOptions_JSType) } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7055 | } /* namespace FieldOptions */ |
| 7056 | } /* namespace protobuf */ |
| 7057 | } /* namespace google */ |
| 7058 | |
| 7059 | namespace google { |
| 7060 | namespace protobuf { |
| 7061 | namespace FileDescriptorProto { |
| 7062 | inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_FileDescriptorProto) } |
| 7063 | inline upb::reffed_ptr<const upb::FieldDef> dependency() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_dependency) } |
| 7064 | inline upb::reffed_ptr<const upb::FieldDef> enum_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_enum_type) } |
| 7065 | inline upb::reffed_ptr<const upb::FieldDef> extension() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_extension) } |
| 7066 | inline upb::reffed_ptr<const upb::FieldDef> message_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_message_type) } |
| 7067 | inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_name) } |
| 7068 | inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_options) } |
| 7069 | inline upb::reffed_ptr<const upb::FieldDef> package() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_package) } |
| 7070 | inline upb::reffed_ptr<const upb::FieldDef> public_dependency() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_public_dependency) } |
| 7071 | inline upb::reffed_ptr<const upb::FieldDef> service() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_service) } |
| 7072 | inline upb::reffed_ptr<const upb::FieldDef> source_code_info() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_source_code_info) } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 7073 | inline upb::reffed_ptr<const upb::FieldDef> syntax() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_syntax) } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7074 | inline upb::reffed_ptr<const upb::FieldDef> weak_dependency() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_weak_dependency) } |
| 7075 | } /* namespace FileDescriptorProto */ |
| 7076 | } /* namespace protobuf */ |
| 7077 | } /* namespace google */ |
| 7078 | |
| 7079 | namespace google { |
| 7080 | namespace protobuf { |
| 7081 | namespace FileDescriptorSet { |
| 7082 | inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_FileDescriptorSet) } |
| 7083 | inline upb::reffed_ptr<const upb::FieldDef> file() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorSet_file) } |
| 7084 | } /* namespace FileDescriptorSet */ |
| 7085 | } /* namespace protobuf */ |
| 7086 | } /* namespace google */ |
| 7087 | |
| 7088 | namespace google { |
| 7089 | namespace protobuf { |
| 7090 | namespace FileOptions { |
| 7091 | inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_FileOptions) } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 7092 | inline upb::reffed_ptr<const upb::FieldDef> cc_enable_arenas() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_cc_enable_arenas) } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7093 | inline upb::reffed_ptr<const upb::FieldDef> cc_generic_services() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_cc_generic_services) } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 7094 | inline upb::reffed_ptr<const upb::FieldDef> csharp_namespace() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_csharp_namespace) } |
| 7095 | inline upb::reffed_ptr<const upb::FieldDef> deprecated() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_deprecated) } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7096 | inline upb::reffed_ptr<const upb::FieldDef> go_package() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_go_package) } |
| 7097 | inline upb::reffed_ptr<const upb::FieldDef> java_generate_equals_and_hash() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_java_generate_equals_and_hash) } |
| 7098 | inline upb::reffed_ptr<const upb::FieldDef> java_generic_services() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_java_generic_services) } |
| 7099 | inline upb::reffed_ptr<const upb::FieldDef> java_multiple_files() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_java_multiple_files) } |
| 7100 | inline upb::reffed_ptr<const upb::FieldDef> java_outer_classname() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_java_outer_classname) } |
| 7101 | inline upb::reffed_ptr<const upb::FieldDef> java_package() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_java_package) } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 7102 | inline upb::reffed_ptr<const upb::FieldDef> java_string_check_utf8() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_java_string_check_utf8) } |
| 7103 | inline upb::reffed_ptr<const upb::FieldDef> javanano_use_deprecated_package() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_javanano_use_deprecated_package) } |
| 7104 | inline upb::reffed_ptr<const upb::FieldDef> objc_class_prefix() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_objc_class_prefix) } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7105 | inline upb::reffed_ptr<const upb::FieldDef> optimize_for() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_optimize_for) } |
| 7106 | inline upb::reffed_ptr<const upb::FieldDef> py_generic_services() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_py_generic_services) } |
| 7107 | inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_uninterpreted_option) } |
| 7108 | inline upb::reffed_ptr<const upb::EnumDef> OptimizeMode() { RETURN_REFFED(upb::EnumDef, upbdefs_google_protobuf_FileOptions_OptimizeMode) } |
| 7109 | } /* namespace FileOptions */ |
| 7110 | } /* namespace protobuf */ |
| 7111 | } /* namespace google */ |
| 7112 | |
| 7113 | namespace google { |
| 7114 | namespace protobuf { |
| 7115 | namespace MessageOptions { |
| 7116 | inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_MessageOptions) } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 7117 | inline upb::reffed_ptr<const upb::FieldDef> deprecated() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MessageOptions_deprecated) } |
| 7118 | inline upb::reffed_ptr<const upb::FieldDef> map_entry() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MessageOptions_map_entry) } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7119 | inline upb::reffed_ptr<const upb::FieldDef> message_set_wire_format() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MessageOptions_message_set_wire_format) } |
| 7120 | inline upb::reffed_ptr<const upb::FieldDef> no_standard_descriptor_accessor() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MessageOptions_no_standard_descriptor_accessor) } |
| 7121 | inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MessageOptions_uninterpreted_option) } |
| 7122 | } /* namespace MessageOptions */ |
| 7123 | } /* namespace protobuf */ |
| 7124 | } /* namespace google */ |
| 7125 | |
| 7126 | namespace google { |
| 7127 | namespace protobuf { |
| 7128 | namespace MethodDescriptorProto { |
| 7129 | inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_MethodDescriptorProto) } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 7130 | inline upb::reffed_ptr<const upb::FieldDef> client_streaming() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodDescriptorProto_client_streaming) } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7131 | inline upb::reffed_ptr<const upb::FieldDef> input_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodDescriptorProto_input_type) } |
| 7132 | inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodDescriptorProto_name) } |
| 7133 | inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodDescriptorProto_options) } |
| 7134 | inline upb::reffed_ptr<const upb::FieldDef> output_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodDescriptorProto_output_type) } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 7135 | inline upb::reffed_ptr<const upb::FieldDef> server_streaming() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodDescriptorProto_server_streaming) } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7136 | } /* namespace MethodDescriptorProto */ |
| 7137 | } /* namespace protobuf */ |
| 7138 | } /* namespace google */ |
| 7139 | |
| 7140 | namespace google { |
| 7141 | namespace protobuf { |
| 7142 | namespace MethodOptions { |
| 7143 | inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_MethodOptions) } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 7144 | inline upb::reffed_ptr<const upb::FieldDef> deprecated() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodOptions_deprecated) } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7145 | inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodOptions_uninterpreted_option) } |
| 7146 | } /* namespace MethodOptions */ |
| 7147 | } /* namespace protobuf */ |
| 7148 | } /* namespace google */ |
| 7149 | |
| 7150 | namespace google { |
| 7151 | namespace protobuf { |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 7152 | namespace OneofDescriptorProto { |
| 7153 | inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_OneofDescriptorProto) } |
| 7154 | inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_OneofDescriptorProto_name) } |
| 7155 | } /* namespace OneofDescriptorProto */ |
| 7156 | } /* namespace protobuf */ |
| 7157 | } /* namespace google */ |
| 7158 | |
| 7159 | namespace google { |
| 7160 | namespace protobuf { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7161 | namespace ServiceDescriptorProto { |
| 7162 | inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_ServiceDescriptorProto) } |
| 7163 | inline upb::reffed_ptr<const upb::FieldDef> method() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_ServiceDescriptorProto_method) } |
| 7164 | inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_ServiceDescriptorProto_name) } |
| 7165 | inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_ServiceDescriptorProto_options) } |
| 7166 | } /* namespace ServiceDescriptorProto */ |
| 7167 | } /* namespace protobuf */ |
| 7168 | } /* namespace google */ |
| 7169 | |
| 7170 | namespace google { |
| 7171 | namespace protobuf { |
| 7172 | namespace ServiceOptions { |
| 7173 | inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_ServiceOptions) } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 7174 | inline upb::reffed_ptr<const upb::FieldDef> deprecated() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_ServiceOptions_deprecated) } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7175 | inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_ServiceOptions_uninterpreted_option) } |
| 7176 | } /* namespace ServiceOptions */ |
| 7177 | } /* namespace protobuf */ |
| 7178 | } /* namespace google */ |
| 7179 | |
| 7180 | namespace google { |
| 7181 | namespace protobuf { |
| 7182 | namespace SourceCodeInfo { |
| 7183 | inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_SourceCodeInfo) } |
| 7184 | inline upb::reffed_ptr<const upb::FieldDef> location() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_SourceCodeInfo_location) } |
| 7185 | } /* namespace SourceCodeInfo */ |
| 7186 | } /* namespace protobuf */ |
| 7187 | } /* namespace google */ |
| 7188 | |
| 7189 | namespace google { |
| 7190 | namespace protobuf { |
| 7191 | namespace SourceCodeInfo { |
| 7192 | namespace Location { |
| 7193 | inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_SourceCodeInfo_Location) } |
| 7194 | inline upb::reffed_ptr<const upb::FieldDef> leading_comments() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_SourceCodeInfo_Location_leading_comments) } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 7195 | inline upb::reffed_ptr<const upb::FieldDef> leading_detached_comments() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_SourceCodeInfo_Location_leading_detached_comments) } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7196 | inline upb::reffed_ptr<const upb::FieldDef> path() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_SourceCodeInfo_Location_path) } |
| 7197 | inline upb::reffed_ptr<const upb::FieldDef> span() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_SourceCodeInfo_Location_span) } |
| 7198 | inline upb::reffed_ptr<const upb::FieldDef> trailing_comments() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_SourceCodeInfo_Location_trailing_comments) } |
| 7199 | } /* namespace Location */ |
| 7200 | } /* namespace SourceCodeInfo */ |
| 7201 | } /* namespace protobuf */ |
| 7202 | } /* namespace google */ |
| 7203 | |
| 7204 | namespace google { |
| 7205 | namespace protobuf { |
| 7206 | namespace UninterpretedOption { |
| 7207 | inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_UninterpretedOption) } |
| 7208 | inline upb::reffed_ptr<const upb::FieldDef> aggregate_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_aggregate_value) } |
| 7209 | inline upb::reffed_ptr<const upb::FieldDef> double_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_double_value) } |
| 7210 | inline upb::reffed_ptr<const upb::FieldDef> identifier_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_identifier_value) } |
| 7211 | inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_name) } |
| 7212 | inline upb::reffed_ptr<const upb::FieldDef> negative_int_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_negative_int_value) } |
| 7213 | inline upb::reffed_ptr<const upb::FieldDef> positive_int_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_positive_int_value) } |
| 7214 | inline upb::reffed_ptr<const upb::FieldDef> string_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_string_value) } |
| 7215 | } /* namespace UninterpretedOption */ |
| 7216 | } /* namespace protobuf */ |
| 7217 | } /* namespace google */ |
| 7218 | |
| 7219 | namespace google { |
| 7220 | namespace protobuf { |
| 7221 | namespace UninterpretedOption { |
| 7222 | namespace NamePart { |
| 7223 | inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_UninterpretedOption_NamePart) } |
| 7224 | inline upb::reffed_ptr<const upb::FieldDef> is_extension() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_NamePart_is_extension) } |
| 7225 | inline upb::reffed_ptr<const upb::FieldDef> name_part() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_NamePart_name_part) } |
| 7226 | } /* namespace NamePart */ |
| 7227 | } /* namespace UninterpretedOption */ |
| 7228 | } /* namespace protobuf */ |
| 7229 | } /* namespace google */ |
| 7230 | |
| 7231 | } /* namespace upbdefs */ |
| 7232 | |
| 7233 | |
| 7234 | #undef RETURN_REFFED |
| 7235 | #endif /* __cplusplus */ |
| 7236 | |
| 7237 | #endif /* GOOGLE_PROTOBUF_DESCRIPTOR_UPB_H_ */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7238 | /* |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 7239 | ** Internal-only definitions for the decoder. |
| 7240 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7241 | |
| 7242 | #ifndef UPB_DECODER_INT_H_ |
| 7243 | #define UPB_DECODER_INT_H_ |
| 7244 | |
| 7245 | #include <stdlib.h> |
| 7246 | /* |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 7247 | ** upb::pb::Decoder |
| 7248 | ** |
| 7249 | ** A high performance, streaming, resumable decoder for the binary protobuf |
| 7250 | ** format. |
| 7251 | ** |
| 7252 | ** This interface works the same regardless of what decoder backend is being |
| 7253 | ** used. A client of this class does not need to know whether decoding is using |
| 7254 | ** a JITted decoder (DynASM, LLVM, etc) or an interpreted decoder. By default, |
| 7255 | ** it will always use the fastest available decoder. However, you can call |
| 7256 | ** set_allow_jit(false) to disable any JIT decoder that might be available. |
| 7257 | ** This is primarily useful for testing purposes. |
| 7258 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7259 | |
| 7260 | #ifndef UPB_DECODER_H_ |
| 7261 | #define UPB_DECODER_H_ |
| 7262 | |
| 7263 | |
| 7264 | #ifdef __cplusplus |
| 7265 | namespace upb { |
| 7266 | namespace pb { |
| 7267 | class CodeCache; |
| 7268 | class Decoder; |
| 7269 | class DecoderMethod; |
| 7270 | class DecoderMethodOptions; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7271 | } /* namespace pb */ |
| 7272 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7273 | #endif |
| 7274 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7275 | UPB_DECLARE_TYPE(upb::pb::CodeCache, upb_pbcodecache) |
| 7276 | UPB_DECLARE_TYPE(upb::pb::Decoder, upb_pbdecoder) |
| 7277 | UPB_DECLARE_TYPE(upb::pb::DecoderMethodOptions, upb_pbdecodermethodopts) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7278 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7279 | UPB_DECLARE_DERIVED_TYPE(upb::pb::DecoderMethod, upb::RefCounted, |
| 7280 | upb_pbdecodermethod, upb_refcounted) |
| 7281 | |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 7282 | /* The maximum number of bytes we are required to buffer internally between |
| 7283 | * calls to the decoder. The value is 14: a 5 byte unknown tag plus ten-byte |
| 7284 | * varint, less one because we are buffering an incomplete value. |
| 7285 | * |
| 7286 | * Should only be used by unit tests. */ |
| 7287 | #define UPB_DECODER_MAX_RESIDUAL_BYTES 14 |
| 7288 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7289 | #ifdef __cplusplus |
| 7290 | |
| 7291 | /* The parameters one uses to construct a DecoderMethod. |
| 7292 | * TODO(haberman): move allowjit here? Seems more convenient for users. |
| 7293 | * TODO(haberman): move this to be heap allocated for ABI stability. */ |
| 7294 | class upb::pb::DecoderMethodOptions { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7295 | public: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7296 | /* Parameter represents the destination handlers that this method will push |
| 7297 | * to. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7298 | explicit DecoderMethodOptions(const Handlers* dest_handlers); |
| 7299 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7300 | /* Should the decoder push submessages to lazy handlers for fields that have |
| 7301 | * them? The caller should set this iff the lazy handlers expect data that is |
| 7302 | * in protobuf binary format and the caller wishes to lazy parse it. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7303 | void set_lazy(bool lazy); |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7304 | #else |
| 7305 | struct upb_pbdecodermethodopts { |
| 7306 | #endif |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7307 | const upb_handlers *handlers; |
| 7308 | bool lazy; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7309 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7310 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7311 | #ifdef __cplusplus |
| 7312 | |
| 7313 | /* Represents the code to parse a protobuf according to a destination |
| 7314 | * Handlers. */ |
| 7315 | class upb::pb::DecoderMethod { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7316 | public: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7317 | /* Include base methods from upb::ReferenceCounted. */ |
| 7318 | UPB_REFCOUNTED_CPPMETHODS |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7319 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7320 | /* The destination handlers that are statically bound to this method. |
| 7321 | * This method is only capable of outputting to a sink that uses these |
| 7322 | * handlers. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7323 | const Handlers* dest_handlers() const; |
| 7324 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7325 | /* The input handlers for this decoder method. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7326 | const BytesHandler* input_handler() const; |
| 7327 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7328 | /* Whether this method is native. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7329 | bool is_native() const; |
| 7330 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7331 | /* Convenience method for generating a DecoderMethod without explicitly |
| 7332 | * creating a CodeCache. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7333 | static reffed_ptr<const DecoderMethod> New(const DecoderMethodOptions& opts); |
| 7334 | |
| 7335 | private: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7336 | UPB_DISALLOW_POD_OPS(DecoderMethod, upb::pb::DecoderMethod) |
| 7337 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7338 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7339 | #endif |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7340 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7341 | /* Preallocation hint: decoder won't allocate more bytes than this when first |
| 7342 | * constructed. This hint may be an overestimate for some build configurations. |
| 7343 | * But if the decoder library is upgraded without recompiling the application, |
| 7344 | * it may be an underestimate. */ |
Josh Haberman | 5bdf4a4 | 2015-08-03 15:51:31 -0700 | [diff] [blame] | 7345 | #define UPB_PB_DECODER_SIZE 4408 |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7346 | |
| 7347 | #ifdef __cplusplus |
| 7348 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7349 | /* A Decoder receives binary protobuf data on its input sink and pushes the |
| 7350 | * decoded data to its output sink. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7351 | class upb::pb::Decoder { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7352 | public: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7353 | /* Constructs a decoder instance for the given method, which must outlive this |
| 7354 | * decoder. Any errors during parsing will be set on the given status, which |
| 7355 | * must also outlive this decoder. |
| 7356 | * |
| 7357 | * The sink must match the given method. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7358 | static Decoder* Create(Environment* env, const DecoderMethod* method, |
| 7359 | Sink* output); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7360 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7361 | /* Returns the DecoderMethod this decoder is parsing from. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7362 | const DecoderMethod* method() const; |
| 7363 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7364 | /* The sink on which this decoder receives input. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7365 | BytesSink* input(); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7366 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7367 | /* Returns number of bytes successfully parsed. |
| 7368 | * |
| 7369 | * This can be useful for determining the stream position where an error |
| 7370 | * occurred. |
| 7371 | * |
| 7372 | * This value may not be up-to-date when called from inside a parsing |
| 7373 | * callback. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7374 | uint64_t BytesParsed() const; |
| 7375 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7376 | /* Gets/sets the parsing nexting limit. If the total number of nested |
| 7377 | * submessages and repeated fields hits this limit, parsing will fail. This |
| 7378 | * is a resource limit that controls the amount of memory used by the parsing |
| 7379 | * stack. |
| 7380 | * |
| 7381 | * Setting the limit will fail if the parser is currently suspended at a depth |
| 7382 | * greater than this, or if memory allocation of the stack fails. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7383 | size_t max_nesting() const; |
| 7384 | bool set_max_nesting(size_t max); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7385 | |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7386 | void Reset(); |
| 7387 | |
| 7388 | static const size_t kSize = UPB_PB_DECODER_SIZE; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7389 | |
| 7390 | private: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7391 | UPB_DISALLOW_POD_OPS(Decoder, upb::pb::Decoder) |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7392 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7393 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7394 | #endif /* __cplusplus */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7395 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7396 | #ifdef __cplusplus |
| 7397 | |
| 7398 | /* A class for caching protobuf processing code, whether bytecode for the |
| 7399 | * interpreted decoder or machine code for the JIT. |
| 7400 | * |
| 7401 | * This class is not thread-safe. |
| 7402 | * |
| 7403 | * TODO(haberman): move this to be heap allocated for ABI stability. */ |
| 7404 | class upb::pb::CodeCache { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7405 | public: |
| 7406 | CodeCache(); |
| 7407 | ~CodeCache(); |
| 7408 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7409 | /* Whether the cache is allowed to generate machine code. Defaults to true. |
| 7410 | * There is no real reason to turn it off except for testing or if you are |
| 7411 | * having a specific problem with the JIT. |
| 7412 | * |
| 7413 | * Note that allow_jit = true does not *guarantee* that the code will be JIT |
| 7414 | * compiled. If this platform is not supported or the JIT was not compiled |
| 7415 | * in, the code may still be interpreted. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7416 | bool allow_jit() const; |
| 7417 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7418 | /* This may only be called when the object is first constructed, and prior to |
| 7419 | * any code generation, otherwise returns false and does nothing. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7420 | bool set_allow_jit(bool allow); |
| 7421 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7422 | /* Returns a DecoderMethod that can push data to the given handlers. |
| 7423 | * If a suitable method already exists, it will be returned from the cache. |
| 7424 | * |
| 7425 | * Specifying the destination handlers here allows the DecoderMethod to be |
| 7426 | * statically bound to the destination handlers if possible, which can allow |
| 7427 | * more efficient decoding. However the returned method may or may not |
| 7428 | * actually be statically bound. But in all cases, the returned method can |
| 7429 | * push data to the given handlers. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7430 | const DecoderMethod *GetDecoderMethod(const DecoderMethodOptions& opts); |
| 7431 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7432 | /* If/when someone needs to explicitly create a dynamically-bound |
| 7433 | * DecoderMethod*, we can add a method to get it here. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7434 | |
| 7435 | private: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7436 | UPB_DISALLOW_COPY_AND_ASSIGN(CodeCache) |
| 7437 | #else |
| 7438 | struct upb_pbcodecache { |
| 7439 | #endif |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7440 | bool allow_jit_; |
| 7441 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7442 | /* Array of mgroups. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7443 | upb_inttable groups; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7444 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7445 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7446 | UPB_BEGIN_EXTERN_C |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7447 | |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7448 | upb_pbdecoder *upb_pbdecoder_create(upb_env *e, |
| 7449 | const upb_pbdecodermethod *method, |
| 7450 | upb_sink *output); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7451 | const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7452 | upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d); |
| 7453 | uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d); |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7454 | size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d); |
| 7455 | bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max); |
| 7456 | void upb_pbdecoder_reset(upb_pbdecoder *d); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7457 | |
| 7458 | void upb_pbdecodermethodopts_init(upb_pbdecodermethodopts *opts, |
| 7459 | const upb_handlers *h); |
| 7460 | void upb_pbdecodermethodopts_setlazy(upb_pbdecodermethodopts *opts, bool lazy); |
| 7461 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7462 | |
| 7463 | /* Include refcounted methods like upb_pbdecodermethod_ref(). */ |
| 7464 | UPB_REFCOUNTED_CMETHODS(upb_pbdecodermethod, upb_pbdecodermethod_upcast) |
| 7465 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7466 | const upb_handlers *upb_pbdecodermethod_desthandlers( |
| 7467 | const upb_pbdecodermethod *m); |
| 7468 | const upb_byteshandler *upb_pbdecodermethod_inputhandler( |
| 7469 | const upb_pbdecodermethod *m); |
| 7470 | bool upb_pbdecodermethod_isnative(const upb_pbdecodermethod *m); |
| 7471 | const upb_pbdecodermethod *upb_pbdecodermethod_new( |
| 7472 | const upb_pbdecodermethodopts *opts, const void *owner); |
| 7473 | |
| 7474 | void upb_pbcodecache_init(upb_pbcodecache *c); |
| 7475 | void upb_pbcodecache_uninit(upb_pbcodecache *c); |
| 7476 | bool upb_pbcodecache_allowjit(const upb_pbcodecache *c); |
| 7477 | bool upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow); |
| 7478 | const upb_pbdecodermethod *upb_pbcodecache_getdecodermethod( |
| 7479 | upb_pbcodecache *c, const upb_pbdecodermethodopts *opts); |
| 7480 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7481 | UPB_END_EXTERN_C |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7482 | |
| 7483 | #ifdef __cplusplus |
| 7484 | |
| 7485 | namespace upb { |
| 7486 | |
| 7487 | namespace pb { |
| 7488 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7489 | /* static */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7490 | inline Decoder* Decoder::Create(Environment* env, const DecoderMethod* m, |
| 7491 | Sink* sink) { |
| 7492 | return upb_pbdecoder_create(env, m, sink); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7493 | } |
| 7494 | inline const DecoderMethod* Decoder::method() const { |
| 7495 | return upb_pbdecoder_method(this); |
| 7496 | } |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7497 | inline BytesSink* Decoder::input() { |
| 7498 | return upb_pbdecoder_input(this); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7499 | } |
| 7500 | inline uint64_t Decoder::BytesParsed() const { |
| 7501 | return upb_pbdecoder_bytesparsed(this); |
| 7502 | } |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7503 | inline size_t Decoder::max_nesting() const { |
| 7504 | return upb_pbdecoder_maxnesting(this); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7505 | } |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7506 | inline bool Decoder::set_max_nesting(size_t max) { |
| 7507 | return upb_pbdecoder_setmaxnesting(this, max); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7508 | } |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7509 | inline void Decoder::Reset() { upb_pbdecoder_reset(this); } |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7510 | |
| 7511 | inline DecoderMethodOptions::DecoderMethodOptions(const Handlers* h) { |
| 7512 | upb_pbdecodermethodopts_init(this, h); |
| 7513 | } |
| 7514 | inline void DecoderMethodOptions::set_lazy(bool lazy) { |
| 7515 | upb_pbdecodermethodopts_setlazy(this, lazy); |
| 7516 | } |
| 7517 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7518 | inline const Handlers* DecoderMethod::dest_handlers() const { |
| 7519 | return upb_pbdecodermethod_desthandlers(this); |
| 7520 | } |
| 7521 | inline const BytesHandler* DecoderMethod::input_handler() const { |
| 7522 | return upb_pbdecodermethod_inputhandler(this); |
| 7523 | } |
| 7524 | inline bool DecoderMethod::is_native() const { |
| 7525 | return upb_pbdecodermethod_isnative(this); |
| 7526 | } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7527 | /* static */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7528 | inline reffed_ptr<const DecoderMethod> DecoderMethod::New( |
| 7529 | const DecoderMethodOptions &opts) { |
| 7530 | const upb_pbdecodermethod *m = upb_pbdecodermethod_new(&opts, &m); |
| 7531 | return reffed_ptr<const DecoderMethod>(m, &m); |
| 7532 | } |
| 7533 | |
| 7534 | inline CodeCache::CodeCache() { |
| 7535 | upb_pbcodecache_init(this); |
| 7536 | } |
| 7537 | inline CodeCache::~CodeCache() { |
| 7538 | upb_pbcodecache_uninit(this); |
| 7539 | } |
| 7540 | inline bool CodeCache::allow_jit() const { |
| 7541 | return upb_pbcodecache_allowjit(this); |
| 7542 | } |
| 7543 | inline bool CodeCache::set_allow_jit(bool allow) { |
| 7544 | return upb_pbcodecache_setallowjit(this, allow); |
| 7545 | } |
| 7546 | inline const DecoderMethod *CodeCache::GetDecoderMethod( |
| 7547 | const DecoderMethodOptions& opts) { |
| 7548 | return upb_pbcodecache_getdecodermethod(this, &opts); |
| 7549 | } |
| 7550 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7551 | } /* namespace pb */ |
| 7552 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7553 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7554 | #endif /* __cplusplus */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7555 | |
| 7556 | #endif /* UPB_DECODER_H_ */ |
| 7557 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7558 | /* C++ names are not actually used since this type isn't exposed to users. */ |
| 7559 | #ifdef __cplusplus |
| 7560 | namespace upb { |
| 7561 | namespace pb { |
| 7562 | class MessageGroup; |
| 7563 | } /* namespace pb */ |
| 7564 | } /* namespace upb */ |
| 7565 | #endif |
| 7566 | UPB_DECLARE_DERIVED_TYPE(upb::pb::MessageGroup, upb::RefCounted, |
| 7567 | mgroup, upb_refcounted) |
| 7568 | |
| 7569 | /* Opcode definitions. The canonical meaning of each opcode is its |
| 7570 | * implementation in the interpreter (the JIT is written to match this). |
| 7571 | * |
| 7572 | * All instructions have the opcode in the low byte. |
| 7573 | * Instruction format for most instructions is: |
| 7574 | * |
| 7575 | * +-------------------+--------+ |
| 7576 | * | arg (24) | op (8) | |
| 7577 | * +-------------------+--------+ |
| 7578 | * |
| 7579 | * Exceptions are indicated below. A few opcodes are multi-word. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7580 | typedef enum { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7581 | /* Opcodes 1-8, 13, 15-18 parse their respective descriptor types. |
| 7582 | * Arg for all of these is the upb selector for this field. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7583 | #define T(type) OP_PARSE_ ## type = UPB_DESCRIPTOR_TYPE_ ## type |
| 7584 | T(DOUBLE), T(FLOAT), T(INT64), T(UINT64), T(INT32), T(FIXED64), T(FIXED32), |
| 7585 | T(BOOL), T(UINT32), T(SFIXED32), T(SFIXED64), T(SINT32), T(SINT64), |
| 7586 | #undef T |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7587 | OP_STARTMSG = 9, /* No arg. */ |
| 7588 | OP_ENDMSG = 10, /* No arg. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7589 | OP_STARTSEQ = 11, |
| 7590 | OP_ENDSEQ = 12, |
| 7591 | OP_STARTSUBMSG = 14, |
| 7592 | OP_ENDSUBMSG = 19, |
| 7593 | OP_STARTSTR = 20, |
| 7594 | OP_STRING = 21, |
| 7595 | OP_ENDSTR = 22, |
| 7596 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7597 | OP_PUSHTAGDELIM = 23, /* No arg. */ |
| 7598 | OP_PUSHLENDELIM = 24, /* No arg. */ |
| 7599 | OP_POP = 25, /* No arg. */ |
| 7600 | OP_SETDELIM = 26, /* No arg. */ |
| 7601 | OP_SETBIGGROUPNUM = 27, /* two words: |
| 7602 | * | unused (24) | opc (8) | |
| 7603 | * | groupnum (32) | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7604 | OP_CHECKDELIM = 28, |
| 7605 | OP_CALL = 29, |
| 7606 | OP_RET = 30, |
| 7607 | OP_BRANCH = 31, |
| 7608 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7609 | /* Different opcodes depending on how many bytes expected. */ |
| 7610 | OP_TAG1 = 32, /* | match tag (16) | jump target (8) | opc (8) | */ |
| 7611 | OP_TAG2 = 33, /* | match tag (16) | jump target (8) | opc (8) | */ |
| 7612 | OP_TAGN = 34, /* three words: */ |
| 7613 | /* | unused (16) | jump target(8) | opc (8) | */ |
| 7614 | /* | match tag 1 (32) | */ |
| 7615 | /* | match tag 2 (32) | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7616 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7617 | OP_SETDISPATCH = 35, /* N words: */ |
| 7618 | /* | unused (24) | opc | */ |
| 7619 | /* | upb_inttable* (32 or 64) | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7620 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7621 | OP_DISPATCH = 36, /* No arg. */ |
Chris Fallin | 97b663a | 2015-01-09 16:15:22 -0800 | [diff] [blame] | 7622 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7623 | OP_HALT = 37 /* No arg. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7624 | } opcode; |
| 7625 | |
| 7626 | #define OP_MAX OP_HALT |
| 7627 | |
| 7628 | UPB_INLINE opcode getop(uint32_t instr) { return instr & 0xff; } |
| 7629 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7630 | /* Method group; represents a set of decoder methods that had their code |
| 7631 | * emitted together, and must therefore be freed together. Immutable once |
| 7632 | * created. It is possible we may want to expose this to users at some point. |
| 7633 | * |
| 7634 | * Overall ownership of Decoder objects looks like this: |
| 7635 | * |
| 7636 | * +----------+ |
| 7637 | * | | <---> DecoderMethod |
| 7638 | * | method | |
| 7639 | * CodeCache ---> | group | <---> DecoderMethod |
| 7640 | * | | |
| 7641 | * | (mgroup) | <---> DecoderMethod |
| 7642 | * +----------+ |
| 7643 | */ |
| 7644 | struct mgroup { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7645 | upb_refcounted base; |
| 7646 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7647 | /* Maps upb_msgdef/upb_handlers -> upb_pbdecodermethod. We own refs on the |
| 7648 | * methods. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7649 | upb_inttable methods; |
| 7650 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7651 | /* When we add the ability to link to previously existing mgroups, we'll |
| 7652 | * need an array of mgroups we reference here, and own refs on them. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7653 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7654 | /* The bytecode for our methods, if any exists. Owned by us. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7655 | uint32_t *bytecode; |
| 7656 | uint32_t *bytecode_end; |
| 7657 | |
| 7658 | #ifdef UPB_USE_JIT_X64 |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7659 | /* JIT-generated machine code, if any. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7660 | upb_string_handlerfunc *jit_code; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7661 | /* The size of the jit_code (required to munmap()). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7662 | size_t jit_size; |
| 7663 | char *debug_info; |
| 7664 | void *dl; |
| 7665 | #endif |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7666 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7667 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7668 | /* The maximum that any submessages can be nested. Matches proto2's limit. |
| 7669 | * This specifies the size of the decoder's statically-sized array and therefore |
| 7670 | * setting it high will cause the upb::pb::Decoder object to be larger. |
| 7671 | * |
| 7672 | * If necessary we can add a runtime-settable property to Decoder that allow |
| 7673 | * this to be larger than the compile-time setting, but this would add |
| 7674 | * complexity, particularly since we would have to decide how/if to give users |
| 7675 | * the ability to set a custom memory allocation function. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7676 | #define UPB_DECODER_MAX_NESTING 64 |
| 7677 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7678 | /* Internal-only struct used by the decoder. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7679 | typedef struct { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7680 | /* Space optimization note: we store two pointers here that the JIT |
| 7681 | * doesn't need at all; the upb_handlers* inside the sink and |
| 7682 | * the dispatch table pointer. We can optimze so that the JIT uses |
| 7683 | * smaller stack frames than the interpreter. The only thing we need |
| 7684 | * to guarantee is that the fallback routines can find end_ofs. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7685 | upb_sink sink; |
| 7686 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7687 | /* The absolute stream offset of the end-of-frame delimiter. |
| 7688 | * Non-delimited frames (groups and non-packed repeated fields) reuse the |
| 7689 | * delimiter of their parent, even though the frame may not end there. |
| 7690 | * |
| 7691 | * NOTE: the JIT stores a slightly different value here for non-top frames. |
| 7692 | * It stores the value relative to the end of the enclosed message. But the |
| 7693 | * top frame is still stored the same way, which is important for ensuring |
| 7694 | * that calls from the JIT into C work correctly. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7695 | uint64_t end_ofs; |
| 7696 | const uint32_t *base; |
| 7697 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7698 | /* 0 indicates a length-delimited field. |
| 7699 | * A positive number indicates a known group. |
| 7700 | * A negative number indicates an unknown group. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7701 | int32_t groupnum; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7702 | upb_inttable *dispatch; /* Not used by the JIT. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7703 | } upb_pbdecoder_frame; |
| 7704 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7705 | struct upb_pbdecodermethod { |
| 7706 | upb_refcounted base; |
| 7707 | |
| 7708 | /* While compiling, the base is relative in "ofs", after compiling it is |
| 7709 | * absolute in "ptr". */ |
| 7710 | union { |
| 7711 | uint32_t ofs; /* PC offset of method. */ |
| 7712 | void *ptr; /* Pointer to bytecode or machine code for this method. */ |
| 7713 | } code_base; |
| 7714 | |
| 7715 | /* The decoder method group to which this method belongs. We own a ref. |
| 7716 | * Owning a ref on the entire group is more coarse-grained than is strictly |
| 7717 | * necessary; all we truly require is that methods we directly reference |
| 7718 | * outlive us, while the group could contain many other messages we don't |
| 7719 | * require. But the group represents the messages that were |
| 7720 | * allocated+compiled together, so it makes the most sense to free them |
| 7721 | * together also. */ |
| 7722 | const upb_refcounted *group; |
| 7723 | |
| 7724 | /* Whether this method is native code or bytecode. */ |
| 7725 | bool is_native_; |
| 7726 | |
| 7727 | /* The handler one calls to invoke this method. */ |
| 7728 | upb_byteshandler input_handler_; |
| 7729 | |
| 7730 | /* The destination handlers this method is bound to. We own a ref. */ |
| 7731 | const upb_handlers *dest_handlers_; |
| 7732 | |
| 7733 | /* Dispatch table -- used by both bytecode decoder and JIT when encountering a |
| 7734 | * field number that wasn't the one we were expecting to see. See |
| 7735 | * decoder.int.h for the layout of this table. */ |
| 7736 | upb_inttable dispatch; |
| 7737 | }; |
| 7738 | |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7739 | struct upb_pbdecoder { |
| 7740 | upb_env *env; |
| 7741 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7742 | /* Our input sink. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7743 | upb_bytessink input_; |
| 7744 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7745 | /* The decoder method we are parsing with (owned). */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7746 | const upb_pbdecodermethod *method_; |
| 7747 | |
| 7748 | size_t call_len; |
| 7749 | const uint32_t *pc, *last; |
| 7750 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7751 | /* Current input buffer and its stream offset. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7752 | const char *buf, *ptr, *end, *checkpoint; |
| 7753 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7754 | /* End of the delimited region, relative to ptr, NULL if not in this buf. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7755 | const char *delim_end; |
| 7756 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7757 | /* End of the delimited region, relative to ptr, end if not in this buf. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7758 | const char *data_end; |
| 7759 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7760 | /* Overall stream offset of "buf." */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7761 | uint64_t bufstart_ofs; |
| 7762 | |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 7763 | /* Buffer for residual bytes not parsed from the previous buffer. */ |
| 7764 | char residual[UPB_DECODER_MAX_RESIDUAL_BYTES]; |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7765 | char *residual_end; |
| 7766 | |
Josh Haberman | 5bdf4a4 | 2015-08-03 15:51:31 -0700 | [diff] [blame] | 7767 | /* Bytes of data that should be discarded from the input beore we start |
| 7768 | * parsing again. We set this when we internally determine that we can |
| 7769 | * safely skip the next N bytes, but this region extends past the current |
| 7770 | * user buffer. */ |
| 7771 | size_t skip; |
| 7772 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7773 | /* Stores the user buffer passed to our decode function. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7774 | const char *buf_param; |
| 7775 | size_t size_param; |
| 7776 | const upb_bufhandle *handle; |
| 7777 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7778 | /* Our internal stack. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7779 | upb_pbdecoder_frame *stack, *top, *limit; |
| 7780 | const uint32_t **callstack; |
| 7781 | size_t stack_size; |
| 7782 | |
| 7783 | upb_status *status; |
| 7784 | |
| 7785 | #ifdef UPB_USE_JIT_X64 |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7786 | /* Used momentarily by the generated code to store a value while a user |
| 7787 | * function is called. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 7788 | uint32_t tmp_len; |
| 7789 | |
| 7790 | const void *saved_rsp; |
| 7791 | #endif |
| 7792 | }; |
| 7793 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7794 | /* Decoder entry points; used as handlers. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7795 | void *upb_pbdecoder_startbc(void *closure, const void *pc, size_t size_hint); |
| 7796 | void *upb_pbdecoder_startjit(void *closure, const void *hd, size_t size_hint); |
| 7797 | size_t upb_pbdecoder_decode(void *closure, const void *hd, const char *buf, |
| 7798 | size_t size, const upb_bufhandle *handle); |
| 7799 | bool upb_pbdecoder_end(void *closure, const void *handler_data); |
| 7800 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7801 | /* Decoder-internal functions that the JIT calls to handle fallback paths. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7802 | int32_t upb_pbdecoder_resume(upb_pbdecoder *d, void *p, const char *buf, |
| 7803 | size_t size, const upb_bufhandle *handle); |
| 7804 | size_t upb_pbdecoder_suspend(upb_pbdecoder *d); |
| 7805 | int32_t upb_pbdecoder_skipunknown(upb_pbdecoder *d, int32_t fieldnum, |
| 7806 | uint8_t wire_type); |
| 7807 | int32_t upb_pbdecoder_checktag_slow(upb_pbdecoder *d, uint64_t expected); |
| 7808 | int32_t upb_pbdecoder_decode_varint_slow(upb_pbdecoder *d, uint64_t *u64); |
| 7809 | int32_t upb_pbdecoder_decode_f32(upb_pbdecoder *d, uint32_t *u32); |
| 7810 | int32_t upb_pbdecoder_decode_f64(upb_pbdecoder *d, uint64_t *u64); |
| 7811 | void upb_pbdecoder_seterr(upb_pbdecoder *d, const char *msg); |
| 7812 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7813 | /* Error messages that are shared between the bytecode and JIT decoders. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7814 | extern const char *kPbDecoderStackOverflow; |
Josh Haberman | 5bdf4a4 | 2015-08-03 15:51:31 -0700 | [diff] [blame] | 7815 | extern const char *kPbDecoderSubmessageTooLong; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7816 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7817 | /* Access to decoderplan members needed by the decoder. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7818 | const char *upb_pbdecoder_getopname(unsigned int op); |
| 7819 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7820 | /* JIT codegen entry point. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7821 | void upb_pbdecoder_jit(mgroup *group); |
| 7822 | void upb_pbdecoder_freejit(mgroup *group); |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7823 | UPB_REFCOUNTED_CMETHODS(mgroup, mgroup_upcast) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7824 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7825 | /* A special label that means "do field dispatch for this message and branch to |
| 7826 | * wherever that takes you." */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7827 | #define LABEL_DISPATCH 0 |
| 7828 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7829 | /* A special slot in the dispatch table that stores the epilogue (ENDMSG and/or |
| 7830 | * RET) for branching to when we find an appropriate ENDGROUP tag. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7831 | #define DISPATCH_ENDMSG 0 |
| 7832 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7833 | /* It's important to use this invalid wire type instead of 0 (which is a valid |
| 7834 | * wire type). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7835 | #define NO_WIRE_TYPE 0xff |
| 7836 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7837 | /* The dispatch table layout is: |
| 7838 | * [field number] -> [ 48-bit offset ][ 8-bit wt2 ][ 8-bit wt1 ] |
| 7839 | * |
| 7840 | * If wt1 matches, jump to the 48-bit offset. If wt2 matches, lookup |
| 7841 | * (UPB_MAX_FIELDNUMBER + fieldnum) and jump there. |
| 7842 | * |
| 7843 | * We need two wire types because of packed/non-packed compatibility. A |
| 7844 | * primitive repeated field can use either wire type and be valid. While we |
| 7845 | * could key the table on fieldnum+wiretype, the table would be 8x sparser. |
| 7846 | * |
| 7847 | * Storing two wire types in the primary value allows us to quickly rule out |
| 7848 | * the second wire type without needing to do a separate lookup (this case is |
| 7849 | * less common than an unknown field). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7850 | UPB_INLINE uint64_t upb_pbdecoder_packdispatch(uint64_t ofs, uint8_t wt1, |
| 7851 | uint8_t wt2) { |
| 7852 | return (ofs << 16) | (wt2 << 8) | wt1; |
| 7853 | } |
| 7854 | |
| 7855 | UPB_INLINE void upb_pbdecoder_unpackdispatch(uint64_t dispatch, uint64_t *ofs, |
| 7856 | uint8_t *wt1, uint8_t *wt2) { |
| 7857 | *wt1 = (uint8_t)dispatch; |
| 7858 | *wt2 = (uint8_t)(dispatch >> 8); |
| 7859 | *ofs = dispatch >> 16; |
| 7860 | } |
| 7861 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7862 | /* All of the functions in decoder.c that return int32_t return values according |
| 7863 | * to the following scheme: |
| 7864 | * 1. negative values indicate a return code from the following list. |
| 7865 | * 2. positive values indicate that error or end of buffer was hit, and |
| 7866 | * that the decode function should immediately return the given value |
| 7867 | * (the decoder state has already been suspended and is ready to be |
| 7868 | * resumed). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7869 | #define DECODE_OK -1 |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7870 | #define DECODE_MISMATCH -2 /* Used only from checktag_slow(). */ |
| 7871 | #define DECODE_ENDGROUP -3 /* Used only from checkunknown(). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7872 | |
| 7873 | #define CHECK_RETURN(x) { int32_t ret = x; if (ret >= 0) return ret; } |
| 7874 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7875 | #endif /* UPB_DECODER_INT_H_ */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7876 | /* |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 7877 | ** A number of routines for varint manipulation (we keep them all around to |
| 7878 | ** have multiple approaches available for benchmarking). |
| 7879 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7880 | |
| 7881 | #ifndef UPB_VARINT_DECODER_H_ |
| 7882 | #define UPB_VARINT_DECODER_H_ |
| 7883 | |
| 7884 | #include <assert.h> |
| 7885 | #include <stdint.h> |
| 7886 | #include <string.h> |
| 7887 | |
| 7888 | #ifdef __cplusplus |
| 7889 | extern "C" { |
| 7890 | #endif |
| 7891 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7892 | /* A list of types as they are encoded on-the-wire. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7893 | typedef enum { |
| 7894 | UPB_WIRE_TYPE_VARINT = 0, |
| 7895 | UPB_WIRE_TYPE_64BIT = 1, |
| 7896 | UPB_WIRE_TYPE_DELIMITED = 2, |
| 7897 | UPB_WIRE_TYPE_START_GROUP = 3, |
| 7898 | UPB_WIRE_TYPE_END_GROUP = 4, |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7899 | UPB_WIRE_TYPE_32BIT = 5 |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7900 | } upb_wiretype_t; |
| 7901 | |
| 7902 | #define UPB_MAX_WIRE_TYPE 5 |
| 7903 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7904 | /* The maximum number of bytes that it takes to encode a 64-bit varint. |
| 7905 | * Note that with a better encoding this could be 9 (TODO: write up a |
| 7906 | * wiki document about this). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7907 | #define UPB_PB_VARINT_MAX_LEN 10 |
| 7908 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7909 | /* Array of the "native" (ie. non-packed-repeated) wire type for the given a |
| 7910 | * descriptor type (upb_descriptortype_t). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7911 | extern const uint8_t upb_pb_native_wire_types[]; |
| 7912 | |
| 7913 | /* Zig-zag encoding/decoding **************************************************/ |
| 7914 | |
| 7915 | UPB_INLINE int32_t upb_zzdec_32(uint32_t n) { |
| 7916 | return (n >> 1) ^ -(int32_t)(n & 1); |
| 7917 | } |
| 7918 | UPB_INLINE int64_t upb_zzdec_64(uint64_t n) { |
| 7919 | return (n >> 1) ^ -(int64_t)(n & 1); |
| 7920 | } |
| 7921 | UPB_INLINE uint32_t upb_zzenc_32(int32_t n) { return (n << 1) ^ (n >> 31); } |
| 7922 | UPB_INLINE uint64_t upb_zzenc_64(int64_t n) { return (n << 1) ^ (n >> 63); } |
| 7923 | |
| 7924 | /* Decoding *******************************************************************/ |
| 7925 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7926 | /* All decoding functions return this struct by value. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7927 | typedef struct { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7928 | const char *p; /* NULL if the varint was unterminated. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7929 | uint64_t val; |
| 7930 | } upb_decoderet; |
| 7931 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7932 | UPB_INLINE upb_decoderet upb_decoderet_make(const char *p, uint64_t val) { |
| 7933 | upb_decoderet ret; |
| 7934 | ret.p = p; |
| 7935 | ret.val = val; |
| 7936 | return ret; |
| 7937 | } |
| 7938 | |
| 7939 | /* Four functions for decoding a varint of at most eight bytes. They are all |
| 7940 | * functionally identical, but are implemented in different ways and likely have |
| 7941 | * different performance profiles. We keep them around for performance testing. |
| 7942 | * |
| 7943 | * Note that these functions may not read byte-by-byte, so they must not be used |
| 7944 | * unless there are at least eight bytes left in the buffer! */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7945 | upb_decoderet upb_vdecode_max8_branch32(upb_decoderet r); |
| 7946 | upb_decoderet upb_vdecode_max8_branch64(upb_decoderet r); |
| 7947 | upb_decoderet upb_vdecode_max8_wright(upb_decoderet r); |
| 7948 | upb_decoderet upb_vdecode_max8_massimino(upb_decoderet r); |
| 7949 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7950 | /* Template for a function that checks the first two bytes with branching |
| 7951 | * and dispatches 2-10 bytes with a separate function. Note that this may read |
| 7952 | * up to 10 bytes, so it must not be used unless there are at least ten bytes |
| 7953 | * left in the buffer! */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7954 | #define UPB_VARINT_DECODER_CHECK2(name, decode_max8_function) \ |
| 7955 | UPB_INLINE upb_decoderet upb_vdecode_check2_ ## name(const char *_p) { \ |
| 7956 | uint8_t *p = (uint8_t*)_p; \ |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7957 | upb_decoderet r; \ |
| 7958 | if ((*p & 0x80) == 0) { \ |
| 7959 | /* Common case: one-byte varint. */ \ |
| 7960 | return upb_decoderet_make(_p + 1, *p & 0x7fU); \ |
| 7961 | } \ |
| 7962 | r = upb_decoderet_make(_p + 2, (*p & 0x7fU) | ((*(p + 1) & 0x7fU) << 7)); \ |
| 7963 | if ((*(p + 1) & 0x80) == 0) { \ |
| 7964 | /* Two-byte varint. */ \ |
| 7965 | return r; \ |
| 7966 | } \ |
| 7967 | /* Longer varint, fallback to out-of-line function. */ \ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7968 | return decode_max8_function(r); \ |
| 7969 | } |
| 7970 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7971 | UPB_VARINT_DECODER_CHECK2(branch32, upb_vdecode_max8_branch32) |
| 7972 | UPB_VARINT_DECODER_CHECK2(branch64, upb_vdecode_max8_branch64) |
| 7973 | UPB_VARINT_DECODER_CHECK2(wright, upb_vdecode_max8_wright) |
| 7974 | UPB_VARINT_DECODER_CHECK2(massimino, upb_vdecode_max8_massimino) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7975 | #undef UPB_VARINT_DECODER_CHECK2 |
| 7976 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7977 | /* Our canonical functions for decoding varints, based on the currently |
| 7978 | * favored best-performing implementations. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7979 | UPB_INLINE upb_decoderet upb_vdecode_fast(const char *p) { |
| 7980 | if (sizeof(long) == 8) |
| 7981 | return upb_vdecode_check2_branch64(p); |
| 7982 | else |
| 7983 | return upb_vdecode_check2_branch32(p); |
| 7984 | } |
| 7985 | |
| 7986 | UPB_INLINE upb_decoderet upb_vdecode_max8_fast(upb_decoderet r) { |
| 7987 | return upb_vdecode_max8_massimino(r); |
| 7988 | } |
| 7989 | |
| 7990 | |
| 7991 | /* Encoding *******************************************************************/ |
| 7992 | |
| 7993 | UPB_INLINE int upb_value_size(uint64_t val) { |
| 7994 | #ifdef __GNUC__ |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 7995 | int high_bit = 63 - __builtin_clzll(val); /* 0-based, undef if val == 0. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7996 | #else |
| 7997 | int high_bit = 0; |
| 7998 | uint64_t tmp = val; |
| 7999 | while(tmp >>= 1) high_bit++; |
| 8000 | #endif |
| 8001 | return val == 0 ? 1 : high_bit / 8 + 1; |
| 8002 | } |
| 8003 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8004 | /* Encodes a 64-bit varint into buf (which must be >=UPB_PB_VARINT_MAX_LEN |
| 8005 | * bytes long), returning how many bytes were used. |
| 8006 | * |
| 8007 | * TODO: benchmark and optimize if necessary. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8008 | UPB_INLINE size_t upb_vencode64(uint64_t val, char *buf) { |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8009 | size_t i; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8010 | if (val == 0) { buf[0] = 0; return 1; } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8011 | i = 0; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8012 | while (val) { |
| 8013 | uint8_t byte = val & 0x7fU; |
| 8014 | val >>= 7; |
| 8015 | if (val) byte |= 0x80U; |
| 8016 | buf[i++] = byte; |
| 8017 | } |
| 8018 | return i; |
| 8019 | } |
| 8020 | |
| 8021 | UPB_INLINE size_t upb_varint_size(uint64_t val) { |
| 8022 | char buf[UPB_PB_VARINT_MAX_LEN]; |
| 8023 | return upb_vencode64(val, buf); |
| 8024 | } |
| 8025 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8026 | /* Encodes a 32-bit varint, *not* sign-extended. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8027 | UPB_INLINE uint64_t upb_vencode32(uint32_t val) { |
| 8028 | char buf[UPB_PB_VARINT_MAX_LEN]; |
| 8029 | size_t bytes = upb_vencode64(val, buf); |
| 8030 | uint64_t ret = 0; |
| 8031 | assert(bytes <= 5); |
| 8032 | memcpy(&ret, buf, bytes); |
| 8033 | assert(ret <= 0xffffffffffU); |
| 8034 | return ret; |
| 8035 | } |
| 8036 | |
| 8037 | #ifdef __cplusplus |
| 8038 | } /* extern "C" */ |
| 8039 | #endif |
| 8040 | |
| 8041 | #endif /* UPB_VARINT_DECODER_H_ */ |
| 8042 | /* |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 8043 | ** upb::pb::Encoder (upb_pb_encoder) |
| 8044 | ** |
| 8045 | ** Implements a set of upb_handlers that write protobuf data to the binary wire |
| 8046 | ** format. |
| 8047 | ** |
| 8048 | ** This encoder implementation does not have any access to any out-of-band or |
| 8049 | ** precomputed lengths for submessages, so it must buffer submessages internally |
| 8050 | ** before it can emit the first byte. |
| 8051 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8052 | |
| 8053 | #ifndef UPB_ENCODER_H_ |
| 8054 | #define UPB_ENCODER_H_ |
| 8055 | |
| 8056 | |
| 8057 | #ifdef __cplusplus |
| 8058 | namespace upb { |
| 8059 | namespace pb { |
| 8060 | class Encoder; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8061 | } /* namespace pb */ |
| 8062 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8063 | #endif |
| 8064 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8065 | UPB_DECLARE_TYPE(upb::pb::Encoder, upb_pb_encoder) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8066 | |
| 8067 | #define UPB_PBENCODER_MAX_NESTING 100 |
| 8068 | |
| 8069 | /* upb::pb::Encoder ***********************************************************/ |
| 8070 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8071 | /* Preallocation hint: decoder won't allocate more bytes than this when first |
| 8072 | * constructed. This hint may be an overestimate for some build configurations. |
| 8073 | * But if the decoder library is upgraded without recompiling the application, |
| 8074 | * it may be an underestimate. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8075 | #define UPB_PB_ENCODER_SIZE 768 |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8076 | |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8077 | #ifdef __cplusplus |
| 8078 | |
| 8079 | class upb::pb::Encoder { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8080 | public: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8081 | /* Creates a new encoder in the given environment. The Handlers must have |
| 8082 | * come from NewHandlers() below. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8083 | static Encoder* Create(Environment* env, const Handlers* handlers, |
| 8084 | BytesSink* output); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8085 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8086 | /* The input to the encoder. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8087 | Sink* input(); |
| 8088 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8089 | /* Creates a new set of handlers for this MessageDef. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8090 | static reffed_ptr<const Handlers> NewHandlers(const MessageDef* msg); |
| 8091 | |
| 8092 | static const size_t kSize = UPB_PB_ENCODER_SIZE; |
| 8093 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8094 | private: |
Josh Haberman | fb8ed70 | 2015-06-22 17:23:55 -0700 | [diff] [blame] | 8095 | UPB_DISALLOW_POD_OPS(Encoder, upb::pb::Encoder) |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8096 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8097 | |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8098 | #endif |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8099 | |
| 8100 | UPB_BEGIN_EXTERN_C |
| 8101 | |
| 8102 | const upb_handlers *upb_pb_encoder_newhandlers(const upb_msgdef *m, |
| 8103 | const void *owner); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8104 | upb_sink *upb_pb_encoder_input(upb_pb_encoder *p); |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8105 | upb_pb_encoder* upb_pb_encoder_create(upb_env* e, const upb_handlers* h, |
| 8106 | upb_bytessink* output); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8107 | |
| 8108 | UPB_END_EXTERN_C |
| 8109 | |
| 8110 | #ifdef __cplusplus |
| 8111 | |
| 8112 | namespace upb { |
| 8113 | namespace pb { |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8114 | inline Encoder* Encoder::Create(Environment* env, const Handlers* handlers, |
| 8115 | BytesSink* output) { |
| 8116 | return upb_pb_encoder_create(env, handlers, output); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8117 | } |
| 8118 | inline Sink* Encoder::input() { |
| 8119 | return upb_pb_encoder_input(this); |
| 8120 | } |
| 8121 | inline reffed_ptr<const Handlers> Encoder::NewHandlers( |
| 8122 | const upb::MessageDef *md) { |
| 8123 | const Handlers* h = upb_pb_encoder_newhandlers(md, &h); |
| 8124 | return reffed_ptr<const Handlers>(h, &h); |
| 8125 | } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8126 | } /* namespace pb */ |
| 8127 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8128 | |
| 8129 | #endif |
| 8130 | |
| 8131 | #endif /* UPB_ENCODER_H_ */ |
| 8132 | /* |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 8133 | ** upb's core components like upb_decoder and upb_msg are carefully designed to |
| 8134 | ** avoid depending on each other for maximum orthogonality. In other words, |
| 8135 | ** you can use a upb_decoder to decode into *any* kind of structure; upb_msg is |
| 8136 | ** just one such structure. A upb_msg can be serialized/deserialized into any |
| 8137 | ** format, protobuf binary format is just one such format. |
| 8138 | ** |
| 8139 | ** However, for convenience we provide functions here for doing common |
| 8140 | ** operations like deserializing protobuf binary format into a upb_msg. The |
| 8141 | ** compromise is that this file drags in almost all of upb as a dependency, |
| 8142 | ** which could be undesirable if you're trying to use a trimmed-down build of |
| 8143 | ** upb. |
| 8144 | ** |
| 8145 | ** While these routines are convenient, they do not reuse any encoding/decoding |
| 8146 | ** state. For example, if a decoder is JIT-based, it will be re-JITted every |
| 8147 | ** time these functions are called. For this reason, if you are parsing lots |
| 8148 | ** of data and efficiency is an issue, these may not be the best functions to |
| 8149 | ** use (though they are useful for prototyping, before optimizing). |
| 8150 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8151 | |
| 8152 | #ifndef UPB_GLUE_H |
| 8153 | #define UPB_GLUE_H |
| 8154 | |
| 8155 | #include <stdbool.h> |
| 8156 | |
| 8157 | #ifdef __cplusplus |
| 8158 | extern "C" { |
| 8159 | #endif |
| 8160 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8161 | /* Loads all defs from the given protobuf binary descriptor, setting default |
| 8162 | * accessors and a default layout on all messages. The caller owns the |
| 8163 | * returned array of defs, which will be of length *n. On error NULL is |
| 8164 | * returned and status is set (if non-NULL). */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8165 | upb_def **upb_load_defs_from_descriptor(const char *str, size_t len, int *n, |
| 8166 | void *owner, upb_status *status); |
| 8167 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8168 | /* Like the previous but also adds the loaded defs to the given symtab. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8169 | bool upb_load_descriptor_into_symtab(upb_symtab *symtab, const char *str, |
| 8170 | size_t len, upb_status *status); |
| 8171 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8172 | /* Like the previous but also reads the descriptor from the given filename. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8173 | bool upb_load_descriptor_file_into_symtab(upb_symtab *symtab, const char *fname, |
| 8174 | upb_status *status); |
| 8175 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8176 | /* Reads the given filename into a character string, returning NULL if there |
| 8177 | * was an error. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8178 | char *upb_readfile(const char *filename, size_t *len); |
| 8179 | |
| 8180 | #ifdef __cplusplus |
| 8181 | } /* extern "C" */ |
| 8182 | |
| 8183 | namespace upb { |
| 8184 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8185 | /* All routines that load descriptors expect the descriptor to be a |
| 8186 | * FileDescriptorSet. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8187 | inline bool LoadDescriptorFileIntoSymtab(SymbolTable* s, const char *fname, |
| 8188 | Status* status) { |
| 8189 | return upb_load_descriptor_file_into_symtab(s, fname, status); |
| 8190 | } |
| 8191 | |
| 8192 | inline bool LoadDescriptorIntoSymtab(SymbolTable* s, const char* str, |
| 8193 | size_t len, Status* status) { |
| 8194 | return upb_load_descriptor_into_symtab(s, str, len, status); |
| 8195 | } |
| 8196 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8197 | /* Templated so it can accept both string and std::string. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8198 | template <typename T> |
| 8199 | bool LoadDescriptorIntoSymtab(SymbolTable* s, const T& desc, Status* status) { |
| 8200 | return upb_load_descriptor_into_symtab(s, desc.c_str(), desc.size(), status); |
| 8201 | } |
| 8202 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8203 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8204 | |
| 8205 | #endif |
| 8206 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8207 | #endif /* UPB_GLUE_H */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8208 | /* |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 8209 | ** upb::pb::TextPrinter (upb_textprinter) |
| 8210 | ** |
| 8211 | ** Handlers for writing to protobuf text format. |
| 8212 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8213 | |
| 8214 | #ifndef UPB_TEXT_H_ |
| 8215 | #define UPB_TEXT_H_ |
| 8216 | |
| 8217 | |
| 8218 | #ifdef __cplusplus |
| 8219 | namespace upb { |
| 8220 | namespace pb { |
| 8221 | class TextPrinter; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8222 | } /* namespace pb */ |
| 8223 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8224 | #endif |
| 8225 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8226 | UPB_DECLARE_TYPE(upb::pb::TextPrinter, upb_textprinter) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8227 | |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8228 | #ifdef __cplusplus |
| 8229 | |
| 8230 | class upb::pb::TextPrinter { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8231 | public: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8232 | /* The given handlers must have come from NewHandlers(). It must outlive the |
| 8233 | * TextPrinter. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8234 | static TextPrinter *Create(Environment *env, const upb::Handlers *handlers, |
| 8235 | BytesSink *output); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8236 | |
| 8237 | void SetSingleLineMode(bool single_line); |
| 8238 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8239 | Sink* input(); |
| 8240 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8241 | /* If handler caching becomes a requirement we can add a code cache as in |
| 8242 | * decoder.h */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8243 | static reffed_ptr<const Handlers> NewHandlers(const MessageDef* md); |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8244 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8245 | |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8246 | #endif |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8247 | |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8248 | UPB_BEGIN_EXTERN_C |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8249 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8250 | /* C API. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8251 | upb_textprinter *upb_textprinter_create(upb_env *env, const upb_handlers *h, |
| 8252 | upb_bytessink *output); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8253 | void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line); |
| 8254 | upb_sink *upb_textprinter_input(upb_textprinter *p); |
| 8255 | |
| 8256 | const upb_handlers *upb_textprinter_newhandlers(const upb_msgdef *m, |
| 8257 | const void *owner); |
| 8258 | |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8259 | UPB_END_EXTERN_C |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8260 | |
| 8261 | #ifdef __cplusplus |
| 8262 | |
| 8263 | namespace upb { |
| 8264 | namespace pb { |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8265 | inline TextPrinter *TextPrinter::Create(Environment *env, |
| 8266 | const upb::Handlers *handlers, |
| 8267 | BytesSink *output) { |
| 8268 | return upb_textprinter_create(env, handlers, output); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8269 | } |
| 8270 | inline void TextPrinter::SetSingleLineMode(bool single_line) { |
| 8271 | upb_textprinter_setsingleline(this, single_line); |
| 8272 | } |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8273 | inline Sink* TextPrinter::input() { |
| 8274 | return upb_textprinter_input(this); |
| 8275 | } |
| 8276 | inline reffed_ptr<const Handlers> TextPrinter::NewHandlers( |
| 8277 | const MessageDef *md) { |
| 8278 | const Handlers* h = upb_textprinter_newhandlers(md, &h); |
| 8279 | return reffed_ptr<const Handlers>(h, &h); |
| 8280 | } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8281 | } /* namespace pb */ |
| 8282 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8283 | |
| 8284 | #endif |
| 8285 | |
| 8286 | #endif /* UPB_TEXT_H_ */ |
| 8287 | /* |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 8288 | ** upb::json::Parser (upb_json_parser) |
| 8289 | ** |
| 8290 | ** Parses JSON according to a specific schema. |
| 8291 | ** Support for parsing arbitrary JSON (schema-less) will be added later. |
| 8292 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8293 | |
| 8294 | #ifndef UPB_JSON_PARSER_H_ |
| 8295 | #define UPB_JSON_PARSER_H_ |
| 8296 | |
| 8297 | |
| 8298 | #ifdef __cplusplus |
| 8299 | namespace upb { |
| 8300 | namespace json { |
| 8301 | class Parser; |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 8302 | class ParserMethod; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8303 | } /* namespace json */ |
| 8304 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8305 | #endif |
| 8306 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8307 | UPB_DECLARE_TYPE(upb::json::Parser, upb_json_parser) |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 8308 | UPB_DECLARE_DERIVED_TYPE(upb::json::ParserMethod, upb::RefCounted, |
| 8309 | upb_json_parsermethod, upb_refcounted) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8310 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8311 | /* upb::json::Parser **********************************************************/ |
| 8312 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8313 | /* Preallocation hint: parser won't allocate more bytes than this when first |
| 8314 | * constructed. This hint may be an overestimate for some build configurations. |
| 8315 | * But if the parser library is upgraded without recompiling the application, |
| 8316 | * it may be an underestimate. */ |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 8317 | #define UPB_JSON_PARSER_SIZE 4104 |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8318 | |
| 8319 | #ifdef __cplusplus |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8320 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8321 | /* Parses an incoming BytesStream, pushing the results to the destination |
| 8322 | * sink. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8323 | class upb::json::Parser { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8324 | public: |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 8325 | static Parser* Create(Environment* env, const ParserMethod* method, |
| 8326 | Sink* output); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8327 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8328 | BytesSink* input(); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8329 | |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8330 | private: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8331 | UPB_DISALLOW_POD_OPS(Parser, upb::json::Parser) |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8332 | }; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8333 | |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 8334 | class upb::json::ParserMethod { |
| 8335 | public: |
| 8336 | /* Include base methods from upb::ReferenceCounted. */ |
| 8337 | UPB_REFCOUNTED_CPPMETHODS |
| 8338 | |
| 8339 | /* Returns handlers for parsing according to the specified schema. */ |
| 8340 | static reffed_ptr<const ParserMethod> New(const upb::MessageDef* md); |
| 8341 | |
| 8342 | /* The destination handlers that are statically bound to this method. |
| 8343 | * This method is only capable of outputting to a sink that uses these |
| 8344 | * handlers. */ |
| 8345 | const Handlers* dest_handlers() const; |
| 8346 | |
| 8347 | /* The input handlers for this decoder method. */ |
| 8348 | const BytesHandler* input_handler() const; |
| 8349 | |
| 8350 | private: |
| 8351 | UPB_DISALLOW_POD_OPS(ParserMethod, upb::json::ParserMethod) |
| 8352 | }; |
| 8353 | |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8354 | #endif |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8355 | |
| 8356 | UPB_BEGIN_EXTERN_C |
| 8357 | |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 8358 | upb_json_parser* upb_json_parser_create(upb_env* e, |
| 8359 | const upb_json_parsermethod* m, |
| 8360 | upb_sink* output); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8361 | upb_bytessink *upb_json_parser_input(upb_json_parser *p); |
| 8362 | |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 8363 | upb_json_parsermethod* upb_json_parsermethod_new(const upb_msgdef* md, |
| 8364 | const void* owner); |
| 8365 | const upb_handlers *upb_json_parsermethod_desthandlers( |
| 8366 | const upb_json_parsermethod *m); |
| 8367 | const upb_byteshandler *upb_json_parsermethod_inputhandler( |
| 8368 | const upb_json_parsermethod *m); |
| 8369 | |
| 8370 | /* Include refcounted methods like upb_json_parsermethod_ref(). */ |
| 8371 | UPB_REFCOUNTED_CMETHODS(upb_json_parsermethod, upb_json_parsermethod_upcast) |
| 8372 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8373 | UPB_END_EXTERN_C |
| 8374 | |
| 8375 | #ifdef __cplusplus |
| 8376 | |
| 8377 | namespace upb { |
| 8378 | namespace json { |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 8379 | inline Parser* Parser::Create(Environment* env, const ParserMethod* method, |
| 8380 | Sink* output) { |
| 8381 | return upb_json_parser_create(env, method, output); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8382 | } |
| 8383 | inline BytesSink* Parser::input() { |
| 8384 | return upb_json_parser_input(this); |
| 8385 | } |
Josh Haberman | 78da666 | 2016-01-13 19:05:43 -0800 | [diff] [blame] | 8386 | |
| 8387 | inline const Handlers* ParserMethod::dest_handlers() const { |
| 8388 | return upb_json_parsermethod_desthandlers(this); |
| 8389 | } |
| 8390 | inline const BytesHandler* ParserMethod::input_handler() const { |
| 8391 | return upb_json_parsermethod_inputhandler(this); |
| 8392 | } |
| 8393 | /* static */ |
| 8394 | inline reffed_ptr<const ParserMethod> ParserMethod::New( |
| 8395 | const MessageDef* md) { |
| 8396 | const upb_json_parsermethod *m = upb_json_parsermethod_new(md, &m); |
| 8397 | return reffed_ptr<const ParserMethod>(m, &m); |
| 8398 | } |
| 8399 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8400 | } /* namespace json */ |
| 8401 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8402 | |
| 8403 | #endif |
| 8404 | |
| 8405 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8406 | #endif /* UPB_JSON_PARSER_H_ */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8407 | /* |
Josh Haberman | 181c7f2 | 2015-07-15 11:05:10 -0700 | [diff] [blame] | 8408 | ** upb::json::Printer |
| 8409 | ** |
| 8410 | ** Handlers that emit JSON according to a specific protobuf schema. |
| 8411 | */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8412 | |
| 8413 | #ifndef UPB_JSON_TYPED_PRINTER_H_ |
| 8414 | #define UPB_JSON_TYPED_PRINTER_H_ |
| 8415 | |
| 8416 | |
| 8417 | #ifdef __cplusplus |
| 8418 | namespace upb { |
| 8419 | namespace json { |
| 8420 | class Printer; |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8421 | } /* namespace json */ |
| 8422 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8423 | #endif |
| 8424 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8425 | UPB_DECLARE_TYPE(upb::json::Printer, upb_json_printer) |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8426 | |
| 8427 | |
| 8428 | /* upb::json::Printer *********************************************************/ |
| 8429 | |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8430 | #define UPB_JSON_PRINTER_SIZE 168 |
| 8431 | |
| 8432 | #ifdef __cplusplus |
| 8433 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8434 | /* Prints an incoming stream of data to a BytesSink in JSON format. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8435 | class upb::json::Printer { |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8436 | public: |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8437 | static Printer* Create(Environment* env, const upb::Handlers* handlers, |
| 8438 | BytesSink* output); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8439 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8440 | /* The input to the printer. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8441 | Sink* input(); |
| 8442 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8443 | /* Returns handlers for printing according to the specified schema. */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8444 | static reffed_ptr<const Handlers> NewHandlers(const upb::MessageDef* md); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8445 | |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8446 | static const size_t kSize = UPB_JSON_PRINTER_SIZE; |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8447 | |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8448 | private: |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8449 | UPB_DISALLOW_POD_OPS(Printer, upb::json::Printer) |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8450 | }; |
| 8451 | |
| 8452 | #endif |
| 8453 | |
| 8454 | UPB_BEGIN_EXTERN_C |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8455 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8456 | /* Native C API. */ |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8457 | upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h, |
| 8458 | upb_bytessink *output); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8459 | upb_sink *upb_json_printer_input(upb_json_printer *p); |
| 8460 | const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md, |
| 8461 | const void *owner); |
| 8462 | |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8463 | UPB_END_EXTERN_C |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8464 | |
| 8465 | #ifdef __cplusplus |
| 8466 | |
| 8467 | namespace upb { |
| 8468 | namespace json { |
Chris Fallin | d326277 | 2015-05-14 18:24:26 -0700 | [diff] [blame] | 8469 | inline Printer* Printer::Create(Environment* env, const upb::Handlers* handlers, |
| 8470 | BytesSink* output) { |
| 8471 | return upb_json_printer_create(env, handlers, output); |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8472 | } |
| 8473 | inline Sink* Printer::input() { return upb_json_printer_input(this); } |
| 8474 | inline reffed_ptr<const Handlers> Printer::NewHandlers( |
| 8475 | const upb::MessageDef *md) { |
| 8476 | const Handlers* h = upb_json_printer_newhandlers(md, &h); |
| 8477 | return reffed_ptr<const Handlers>(h, &h); |
| 8478 | } |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8479 | } /* namespace json */ |
| 8480 | } /* namespace upb */ |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 8481 | |
| 8482 | #endif |
| 8483 | |
Josh Haberman | e8ed021 | 2015-06-08 17:56:03 -0700 | [diff] [blame] | 8484 | #endif /* UPB_JSON_TYPED_PRINTER_H_ */ |