30#if !((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
31 (defined(__cplusplus) && __cplusplus >= 201703L) || \
32 (defined(_MSC_VER) && _MSC_VER >= 1900))
33#error upb requires C99 or C++17 or MSVC >= 2015.
36#define UPB_BOOTSTRAP_STAGE 0
39#pragma warning(disable : 4018 4047 4116 4146 4200 4244 4267 4334 4646 4789)
40#elif defined(__clang__)
41#pragma clang diagnostic push
43#pragma clang diagnostic ignored "-Wc99-extensions"
45#pragma clang diagnostic ignored "-Wsign-compare"
46#pragma clang diagnostic ignored "-Wflexible-array-extensions"
47#elif defined(__GNUC__)
48#pragma GCC diagnostic push
49#pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
50#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
51#pragma GCC diagnostic ignored "-Wpedantic"
53#pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
55#pragma GCC diagnostic ignored "-Wclobbered"
56#pragma GCC diagnostic ignored "-Wsign-compare"
57#pragma GCC diagnostic ignored "-Wstringop-overflow"
58#pragma GCC diagnostic ignored "-Wstringop-overread"
59#pragma GCC diagnostic ignored "-Wtype-limits"
64#if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
65#define UPB_GNUC_MIN(x, y) \
66 (__GNUC__ > (x) || __GNUC__ == (x) && __GNUC_MINOR__ >= (y))
68#define UPB_GNUC_MIN(x, y) 0
75#define UPB_HAS_ATTRIBUTE(x) __has_attribute(x)
77#define UPB_HAS_ATTRIBUTE(x) 0
81#define UPB_HAS_BUILTIN(x) __has_builtin(x)
83#define UPB_HAS_BUILTIN(x) 0
87#define UPB_HAS_EXTENSION(x) __has_extension(x)
89#define UPB_HAS_EXTENSION(x) 0
93#define UPB_HAS_FEATURE(x) __has_feature(x)
95#define UPB_HAS_FEATURE(x) 0
109#if UINTPTR_MAX == 0xffffffff
110#define UPB_SIZE(size32, size64) size32
112#define UPB_SIZE(size32, size64) size64
118#define UPB_PTR_AT(msg, ofs, type) ((type *)((char *)(msg) + (ofs)))
124#define UPB_SIZEOF_FLEX(type, member, count) \
125 UPB_MAX(sizeof(type), offsetof(type, member[count]))
127#define UPB_SIZEOF_FLEX_WOULD_OVERFLOW(type, member, count) \
128 (((SIZE_MAX - offsetof(type, member[0])) / \
129 (offsetof(type, member[1]) - offsetof(type, member[0]))) < (size_t)count)
131#define UPB_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
133#define UPB_MAPTYPE_STRING 0
136#if defined(__GNUC__) || defined(__clang__)
137#define UPB_EXPORT __attribute__((visibility("default"))) __attribute__((used))
144#define UPB_INLINE inline
145#elif defined(__GNUC__) || defined(__clang__)
146#define UPB_INLINE static __inline__
148#define UPB_INLINE static
152#if defined(__GNUC__) && !defined(__clang__)
153#define UPB_INLINE_IF_NOT_GCC static
155#define UPB_INLINE_IF_NOT_GCC UPB_INLINE
159#define UPB_API UPB_EXPORT
160#define UPB_API_INLINE UPB_EXPORT
163#define UPB_API_INLINE UPB_INLINE
167#define UPBC_API UPB_EXPORT
172#if UPB_HAS_FEATURE(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
178#if UPB_HAS_FEATURE(hwaddress_sanitizer)
180#define UPB_HWASAN_POISON_TAG 17
181#define UPB_MALLOC_ALIGN 16
184#define UPB_MALLOC_ALIGN 8
187#if UPB_HAS_FEATURE(thread_sanitizer) || defined(__SANITIZE_THREAD__)
195#if UPB_ASAN || UPB_HWASAN || UPB_TSAN
196#define UPB_XSAN_MEMBER upb_Xsan xsan;
197#define UPB_XSAN(st) (&(st)->xsan)
198#define UPB_XSAN_STRUCT_SIZE 1
200#define UPB_XSAN_MEMBER
201#define UPB_XSAN(st) (NULL)
202#define UPB_XSAN_STRUCT_SIZE 0
205#define UPB_ALIGN_UP(size, align) (((size) + (align) - 1) / (align) * (align))
206#define UPB_ALIGN_DOWN(size, align) ((size) / (align) * (align))
207#define UPB_ALIGN_MALLOC(size) UPB_ALIGN_UP(size, UPB_MALLOC_ALIGN)
209#if __STDC_VERSION__ >= 202311L || UPB_HAS_EXTENSION(cxx_alignof) || \
211#define UPB_ALIGN_OF(type) alignof(type)
212#elif __STDC_VERSION__ >= 201112L || UPB_HAS_EXTENSION(c_alignof)
213#define UPB_ALIGN_OF(type) _Alignof(type)
214#elif UPB_GNUC_MIN(2, 95)
215#define UPB_ALIGN_OF(type) __alignof__(type)
216#elif defined(_MSC_VER)
217#define UPB_ALIGN_OF(type) __alignof(type)
219#define UPB_ALIGN_OF(type) \
230#define UPB_ALIGN_AS(x) __declspec(align(x))
231#elif defined(__GNUC__)
232#define UPB_ALIGN_AS(x) __attribute__((aligned(x)))
234#define UPB_ALIGN_AS(x) _Alignas(x)
237#if __STDC_VERSION__ >= 202311L || UPB_HAS_EXTENSION(cxx_static_assert) || \
239#define UPB_STATIC_ASSERT(val, msg) static_assert((val), msg)
240#elif __STDC_VERSION__ >= 201112L || UPB_HAS_EXTENSION(c_static_assert) || \
242#define UPB_STATIC_ASSERT(val, msg) _Static_assert((val), msg)
246#define UPB_STATIC_ASSERT_CONCAT_IMPL(s1, s2) s1##s2
247#define UPB_STATIC_ASSERT_CONCAT(s1, s2) UPB_STATIC_ASSERT_CONCAT_IMPL(s1, s2)
249#define UPB_STATIC_ASSERT(condition, message) \
250 typedef char UPB_STATIC_ASSERT_CONCAT(static_assertion_failure_, \
251 __COUNTER__)[(condition) ? 1 : -1]
253#define UPB_STATIC_ASSERT(condition, message) \
254 typedef char UPB_STATIC_ASSERT_CONCAT(static_assertion_failure_, \
255 __LINE__)[(condition) ? 1 : -1]
260#if defined(__GNUC__) || defined(__clang__)
261#define UPB_LIKELY(x) __builtin_expect((bool)(x), 1)
262#define UPB_UNLIKELY(x) __builtin_expect((bool)(x), 0)
264#define UPB_LIKELY(x) (x)
265#define UPB_UNLIKELY(x) (x)
268#if UPB_HAS_BUILTIN(__builtin_expect_with_probability)
269#define UPB_UNPREDICTABLE(x) \
270 __builtin_expect_with_probability((bool)(x), 1, 0.5)
272#define UPB_UNPREDICTABLE(x) (x)
276#if defined(__GNUC__) || defined(__clang__)
277#define UPB_FORCEINLINE __inline__ __attribute__((always_inline)) static
278#define UPB_NOINLINE __attribute__((noinline))
279#define UPB_NORETURN __attribute__((__noreturn__))
280#define UPB_PRINTF(str, first_vararg) \
281 __attribute__((format(printf, str, first_vararg)))
282#elif defined(_MSC_VER)
284#define UPB_FORCEINLINE static
285#define UPB_NORETURN __declspec(noreturn)
286#define UPB_PRINTF(str, first_vararg)
288#define UPB_FORCEINLINE static
291#define UPB_PRINTF(str, first_vararg)
294#if defined(__clang__)
295#define UPB_NODEREF __attribute__((noderef))
300#define UPB_MAX(x, y) ((x) > (y) ? (x) : (y))
301#define UPB_MIN(x, y) ((x) < (y) ? (x) : (y))
303#define UPB_UNUSED(var) (void)(var)
308#define UPB_ASSUME(expr) \
309 if (!(expr)) __builtin_unreachable()
310#elif defined _MSC_VER
311#define UPB_ASSUME(expr) \
312 if (!(expr)) __assume(0)
314#define UPB_ASSUME(expr) \
316 } while (false && (expr))
319#define UPB_ASSUME(expr) assert(expr)
325#define UPB_ASSERT(expr) \
327 } while (false && (expr))
329#define UPB_ASSERT(expr) assert(expr)
332#if defined(__GNUC__) || defined(__clang__)
333#define UPB_UNREACHABLE() \
336 __builtin_unreachable(); \
338#elif defined(_MSC_VER)
339#define UPB_UNREACHABLE() \
345#define UPB_UNREACHABLE() \
352#define UPB_DEFAULT_MAX_BLOCK_SIZE 8192
354#define UPB_DEFAULT_MAX_BLOCK_SIZE 32768
361#if defined(__APPLE__) || defined(_POSIX_C_SOURCE) || defined(__ANDROID__)
363#define UPB_SETJMP(buf) sigsetjmp(buf, 0)
364#define UPB_LONGJMP(buf, val) siglongjmp(buf, val)
365#elif defined(WASM_WAMR)
366#define UPB_SETJMP(buf) 0
367#define UPB_LONGJMP(buf, val) abort()
369#define UPB_SETJMP(buf) setjmp(buf)
370#define UPB_LONGJMP(buf, val) longjmp(buf, val)
373#if ((__STDC_VERSION__ >= 201112L) && !defined(__STDC_NO_ATOMICS__)) || \
374 UPB_HAS_EXTENSION(c_atomic) || \
377#define UPB_USE_C11_ATOMICS
378#elif defined(_MSC_VER)
379#define UPB_USE_MSC_ATOMICS
382#if defined(UPB_USE_C11_ATOMICS)
383#define UPB_ATOMIC(T) _Atomic(T)
384#elif defined(UPB_USE_MSC_ATOMICS)
385#define UPB_ATOMIC(T) volatile T
387#define UPB_ATOMIC(T) T
391#define UPB_PTRADD(ptr, ofs) ((ofs) ? (ptr) + (ofs) : (ptr))
393#define UPB_PRIVATE(x) x##_dont_copy_me__upb_internal_use_only
395#ifdef UPB_ALLOW_PRIVATE_ACCESS__FOR_BITS_ONLY
396#define UPB_ONLYBITS(x) x
398#define UPB_ONLYBITS(x) UPB_PRIVATE(x)
403#if UPB_HAS_ATTRIBUTE(musttail)
404#define UPB_MUSTTAIL __attribute__((musttail))
409#if UPB_HAS_ATTRIBUTE(preserve_none)
410#define UPB_PRESERVE_NONE __attribute__((preserve_none))
412#define UPB_PRESERVE_NONE
429#if (defined(__x86_64__) || defined(__aarch64__)) && defined(__GNUC__)
430#define UPB_FASTTABLE_SUPPORTED 1
432#define UPB_FASTTABLE_SUPPORTED 0
438#if defined(UPB_ENABLE_FASTTABLE)
439#if !UPB_FASTTABLE_SUPPORTED
440#error fasttable is x86-64/ARM64 only and requires GCC or Clang.
442#define UPB_FASTTABLE 1
446#elif defined(UPB_TRY_ENABLE_FASTTABLE)
447#define UPB_FASTTABLE UPB_FASTTABLE_SUPPORTED
449#define UPB_FASTTABLE 0
455#define UPB_FASTTABLE_INIT(...)
456#define UPB_FASTTABLE_MASK(mask) -1
458#define UPB_FASTTABLE_INIT(...) __VA_ARGS__
459#define UPB_FASTTABLE_MASK(mask) mask
462#undef UPB_FASTTABLE_SUPPORTED
466#ifdef UPB_DISABLE_CLOSED_ENUM_CHECKING
467#define UPB_TREAT_CLOSED_ENUMS_LIKE_OPEN 1
469#define UPB_TREAT_CLOSED_ENUMS_LIKE_OPEN 0
472#if defined(__cplusplus)
473#if defined(__clang__) || UPB_GNUC_MIN(6, 0)
475#if __cplusplus >= 201402L
476#define UPB_DEPRECATED [[deprecated]]
478#define UPB_DEPRECATED __attribute__((deprecated))
481#define UPB_DEPRECATED
484#define UPB_DEPRECATED
487#if defined(UPB_IS_GOOGLE3) && \
488 (!defined(UPB_BOOTSTRAP_STAGE) || UPB_BOOTSTRAP_STAGE != 0)
489#define UPB_DESC(sym) proto2_##sym
490#define UPB_DESC_MINITABLE(sym) &proto2__##sym##_msg_init
491#elif defined(UPB_IS_GOOGLE3) && defined(UPB_BOOTSTRAP_STAGE) && \
492 UPB_BOOTSTRAP_STAGE == 0
493#define UPB_DESC(sym) proto2_##sym
494#define UPB_DESC_MINITABLE(sym) proto2__##sym##_msg_init()
495#elif defined(UPB_BOOTSTRAP_STAGE) && UPB_BOOTSTRAP_STAGE == 0
496#define UPB_DESC(sym) google_protobuf_##sym
497#define UPB_DESC_MINITABLE(sym) google__protobuf__##sym##_msg_init()
499#define UPB_DESC(sym) google_protobuf_##sym
500#define UPB_DESC_MINITABLE(sym) &google__protobuf__##sym##_msg_init
506#define UPB_NO_SANITIZE_ADDRESS __attribute__((no_sanitize("address")))
508#define UPB_NO_SANITIZE_ADDRESS
535#if defined(__ELF__) || defined(__wasm__)
537#define UPB_LINKARR_APPEND(name) \
538 __attribute__((retain, used, \
539 section("linkarr_" #name))) UPB_NO_SANITIZE_ADDRESS
540#define UPB_LINKARR_DECLARE(name, type) \
541 extern type __start_linkarr_##name; \
542 extern type __stop_linkarr_##name; \
543 UPB_LINKARR_APPEND(name) type UPB_linkarr_internal_empty_##name[1]
544#define UPB_LINKARR_START(name) (&__start_linkarr_##name)
545#define UPB_LINKARR_STOP(name) (&__stop_linkarr_##name)
547#elif defined(__MACH__)
550#define UPB_LINKARR_APPEND(name) \
551 __attribute__((retain, used, \
552 section("__DATA,__la_" #name))) UPB_NO_SANITIZE_ADDRESS
553#define UPB_LINKARR_DECLARE(name, type) \
554 extern type __start_linkarr_##name __asm( \
555 "section$start$__DATA$__la_" #name); \
556 extern type __stop_linkarr_##name __asm( \
557 "section$end$__DATA$" \
559 UPB_LINKARR_APPEND(name) type UPB_linkarr_internal_empty_##name[1]
560#define UPB_LINKARR_START(name) (&__start_linkarr_##name)
561#define UPB_LINKARR_STOP(name) (&__stop_linkarr_##name)
563#elif defined(_MSC_VER) && defined(__clang__)
572#define UPB_LINKARR_APPEND(name) \
573 __declspec(allocate("la_" #name "$j")) \
574 __attribute__((retain, used)) UPB_NO_SANITIZE_ADDRESS
575#define UPB_LINKARR_DECLARE(name, type) \
576 __declspec(allocate("la_" #name "$a")) type __start_linkarr_##name; \
577 __declspec(allocate("la_" #name "$z")) type __stop_linkarr_##name; \
578 UPB_LINKARR_APPEND(name) type UPB_linkarr_internal_empty_##name[1] = {0}
579#define UPB_LINKARR_START(name) (&__start_linkarr_##name)
580#define UPB_LINKARR_STOP(name) (&__stop_linkarr_##name)
586#define UPB_LINKARR_APPEND(name)
593#ifdef UPB_FUTURE_BREAKING_CHANGES
Error
Definition def.inc:106