1 /* libc/sys/linux/sys/cdefs.h - Helper macros for K&R vs. ANSI C compat. */
2 
3 /* Written 2000 by Werner Almesberger */
4 
5 /*-
6  * SPDX-License-Identifier: BSD-3-Clause
7  *
8  * Copyright (c) 1991, 1993
9  *	The Regents of the University of California.  All rights reserved.
10  *
11  * This code is derived from software contributed to Berkeley by
12  * Berkeley Software Design, Inc.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)cdefs.h	8.8 (Berkeley) 1/9/95
39  * $FreeBSD$
40  */
41 
42 #ifndef	_SYS_CDEFS_H_
43 #define	_SYS_CDEFS_H_
44 
45 #include <machine/_default_types.h>
46 #include <sys/features.h>
47 #include <stddef.h>
48 
49 #define __PMT(args)	args
50 #define __DOTS    	, ...
51 #define __THROW
52 
53 #ifdef __GNUC__
54 # define __ASMNAME(cname)  __XSTRING (__USER_LABEL_PREFIX__) cname
55 #endif
56 
57 #define __ptr_t void *
58 #define __long_double_t  long double
59 
60 #define __attribute_malloc__
61 #define __attribute_pure__
62 #define __attribute_format_strfmon__(a,b)
63 #define __flexarr      [0]
64 
65 #ifndef __BOUNDED_POINTERS__
66 # define __bounded      /* nothing */
67 # define __unbounded    /* nothing */
68 # define __ptrvalue     /* nothing */
69 #endif
70 
71 /*
72  * Testing against Clang-specific extensions.
73  */
74 #ifndef	__has_attribute
75 #define	__has_attribute(x)	0
76 #endif
77 #ifndef	__has_extension
78 #define	__has_extension		__has_feature
79 #endif
80 #ifndef	__has_feature
81 #define	__has_feature(x)	0
82 #endif
83 #ifndef	__has_include
84 #define	__has_include(x)	0
85 #endif
86 #ifndef	__has_builtin
87 #define	__has_builtin(x)	0
88 #endif
89 
90 #if defined(__cplusplus)
91 #define	__BEGIN_DECLS	extern "C" {
92 #define	__END_DECLS	}
93 #else
94 #define	__BEGIN_DECLS
95 #define	__END_DECLS
96 #endif
97 
98 /**
99  * Not all compilers offer __builtin_expect (e.g. CompCert does
100  * not have it). In that case, transparently replace all
101  * occurences of that builtin with just the condition:
102  */
103 #ifndef _HAVE_BUILTIN_EXPECT
104 #define __builtin_expect(cond, exp) (cond)
105 #endif
106 
107 /*
108  * This code has been put in place to help reduce the addition of
109  * compiler specific defines in FreeBSD code.  It helps to aid in
110  * having a compiler-agnostic source tree.
111  */
112 
113 #if defined(__GNUC__)
114 
115 #if __GNUC__ >= 3
116 #define	__GNUCLIKE_ASM 3
117 #define	__GNUCLIKE_MATH_BUILTIN_CONSTANTS
118 #else
119 #define	__GNUCLIKE_ASM 2
120 #endif
121 #define	__GNUCLIKE___TYPEOF 1
122 #define	__GNUCLIKE___SECTION 1
123 
124 #define	__GNUCLIKE_CTOR_SECTION_HANDLING 1
125 
126 #define	__GNUCLIKE_BUILTIN_CONSTANT_P 1
127 
128 #if (__GNUC_MINOR__ > 95 || __GNUC__ >= 3)
129 #define	__GNUCLIKE_BUILTIN_VARARGS 1
130 #define	__GNUCLIKE_BUILTIN_STDARG 1
131 #define	__GNUCLIKE_BUILTIN_VAALIST 1
132 #endif
133 
134 #define	__GNUC_VA_LIST_COMPATIBILITY 1
135 
136 /*
137  * Compiler memory barriers, specific to gcc and clang.
138  */
139 #define	__compiler_membar()	__asm __volatile(" " : : : "memory")
140 
141 #define	__GNUCLIKE_BUILTIN_NEXT_ARG 1
142 #define	__GNUCLIKE_MATH_BUILTIN_RELOPS
143 
144 #define	__GNUCLIKE_BUILTIN_MEMCPY 1
145 
146 /* XXX: if __GNUC__ >= 2: not tested everywhere originally, where replaced */
147 #define	__CC_SUPPORTS_INLINE 1
148 #define	__CC_SUPPORTS___INLINE 1
149 #define	__CC_SUPPORTS___INLINE__ 1
150 
151 #define	__CC_SUPPORTS___FUNC__ 1
152 #define	__CC_SUPPORTS_WARNING 1
153 
154 #define	__CC_SUPPORTS_VARADIC_XXX 1 /* see varargs.h */
155 
156 #define	__CC_SUPPORTS_DYNAMIC_ARRAY_INIT 1
157 
158 #endif /* __GNUC__ */
159 
160 /*
161  * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
162  * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
163  * The __CONCAT macro is a bit tricky to use if it must work in non-ANSI
164  * mode -- there must be no spaces between its arguments, and for nested
165  * __CONCAT's, all the __CONCAT's must be at the left.  __CONCAT can also
166  * concatenate double-quoted strings produced by the __STRING macro, but
167  * this only works with ANSI C.
168  *
169  * __XSTRING is like __STRING, but it expands any macros in its argument
170  * first.  It is only available with ANSI C.
171  */
172 #if defined(__STDC__) || defined(__cplusplus)
173 #define	__P(protos)	protos		/* full-blown ANSI C */
174 #define	__CONCAT1(x,y)	x ## y
175 #define	__CONCAT(x,y)	__CONCAT1(x,y)
176 #define	__STRING(x)	#x		/* stringify without expanding x */
177 #define	__XSTRING(x)	__STRING(x)	/* expand x, then stringify */
178 
179 #define	__const		const		/* define reserved names to standard */
180 #define	__signed	signed
181 #define	__volatile	volatile
182 #if defined(__cplusplus)
183 #define	__inline	inline		/* convert to C++ keyword */
184 #else
185 #if !(defined(__CC_SUPPORTS___INLINE))
186 #define	__inline			/* delete GCC keyword */
187 #endif /* ! __CC_SUPPORTS___INLINE */
188 #endif /* !__cplusplus */
189 
190 #else	/* !(__STDC__ || __cplusplus) */
191 #define	__P(protos)	()		/* traditional C preprocessor */
192 #define	__CONCAT(x,y)	x/**/y
193 #define	__STRING(x)	"x"
194 
195 #if !defined(__CC_SUPPORTS___INLINE)
196 #define	__const				/* delete pseudo-ANSI C keywords */
197 #define	__inline
198 #define	__signed
199 #define	__volatile
200 /*
201  * In non-ANSI C environments, new programs will want ANSI-only C keywords
202  * deleted from the program and old programs will want them left alone.
203  * When using a compiler other than gcc, programs using the ANSI C keywords
204  * const, inline etc. as normal identifiers should define -DNO_ANSI_KEYWORDS.
205  * When using "gcc -traditional", we assume that this is the intent; if
206  * __GNUC__ is defined but __STDC__ is not, we leave the new keywords alone.
207  */
208 #ifndef	NO_ANSI_KEYWORDS
209 #define	const				/* delete ANSI C keywords */
210 #define	inline
211 #define	signed
212 #define	volatile
213 #endif	/* !NO_ANSI_KEYWORDS */
214 #endif	/* !__CC_SUPPORTS___INLINE */
215 #endif	/* !(__STDC__ || __cplusplus) */
216 
217 /*
218  * Compiler-dependent macros to help declare dead (non-returning) and
219  * pure (no side effects) functions, and unused variables.  They are
220  * null except for versions of gcc that are known to support the features
221  * properly (old versions of gcc-2 supported the dead and pure features
222  * in a different (wrong) way).  If we do not provide an implementation
223  * for a given compiler, let the compile fail if it is told to use
224  * a feature that we cannot live without.
225  */
226 #define	__weak_symbol	__attribute__((__weak__))
227 #if !__GNUC_PREREQ__(2, 5)
228 #define	__dead2
229 #define	__pure2
230 #define	__unused
231 #endif
232 #if __GNUC__ == 2 && __GNUC_MINOR__ >= 5 && __GNUC_MINOR__ < 7
233 #define	__dead2		__attribute__((__noreturn__))
234 #define	__pure2		__attribute__((__const__))
235 #define	__unused
236 /* XXX Find out what to do for __packed, __aligned and __section */
237 #endif
238 #if __GNUC_PREREQ__(2, 7)
239 #define	__dead2		__attribute__((__noreturn__))
240 #define	__pure2		__attribute__((__const__))
241 #define	__unused	__attribute__((__unused__))
242 #define	__used		__attribute__((__used__))
243 #define	__packed	__attribute__((__packed__))
244 #define	__aligned(x)	__attribute__((__aligned__(x)))
245 #define	__section(x)	__attribute__((__section__(x)))
246 #endif
247 
248 #ifdef _HAVE_ALLOC_SIZE
249 #define	__alloc_size(x)	__attribute__((__alloc_size__(x)))
250 #define	__alloc_size2(n, x)	__attribute__((__alloc_size__(n, x)))
251 #else
252 #define	__alloc_size(x)
253 #define	__alloc_size2(n, x)
254 #endif
255 #if __GNUC_PREREQ__(4, 9) || __has_attribute(__alloc_align__)
256 #define	__alloc_align(x)	__attribute__((__alloc_align__(x)))
257 #else
258 #define	__alloc_align(x)
259 #endif
260 
261 #if !__GNUC_PREREQ__(2, 95)
262 #define	__alignof(x)	__offsetof(struct { char __a; x __b; }, __b)
263 #endif
264 
265 #if __GNUC_PREREQ__(4,5) || defined(__clang__)
266 #define __picolibc_deprecated(m) __attribute__((__deprecated__(m)))
267 #else
268 #define __picolibc_deprecated(m) _ATTRIBUTE(__deprecated__)
269 #endif
270 
271 /*
272  * Keywords added in C11.
273  */
274 
275 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
276 
277 #if !__has_extension(c_alignas)
278 #if (defined(__cplusplus) && __cplusplus >= 201103L) || \
279     __has_extension(cxx_alignas)
280 #define	_Alignas(x)		alignas(x)
281 #else
282 /* XXX: Only emulates _Alignas(constant-expression); not _Alignas(type-name). */
283 #define	_Alignas(x)		__aligned(x)
284 #endif
285 #endif
286 
287 #if defined(__cplusplus) && __cplusplus >= 201103L
288 #define	_Alignof(x)		alignof(x)
289 #else
290 #define	_Alignof(x)		__alignof(x)
291 #endif
292 
293 #if !defined(__cplusplus) && !__has_extension(c_atomic) && \
294 	!__has_extension(cxx_atomic) && !__GNUC_PREREQ__(4, 7)
295 /*
296  * No native support for _Atomic(). Place object in structure to prevent
297  * most forms of direct non-atomic access.
298  */
299 #define	_Atomic(T)		struct { T volatile __val; }
300 #endif
301 
302 #if defined(__cplusplus) && __cplusplus >= 201103L
303 #define	_Noreturn		[[noreturn]]
304 #else
305 #define	_Noreturn		__dead2
306 #endif
307 
308 #if !__has_extension(c_static_assert)
309 #if (defined(__cplusplus) && __cplusplus >= 201103L) || \
310     __has_extension(cxx_static_assert)
311 #define	_Static_assert(x, y)	static_assert(x, y)
312 #elif __GNUC_PREREQ__(4,6) && !defined(__cplusplus)
313 /* Nothing, gcc 4.6 and higher has _Static_assert built-in */
314 #elif defined(__COUNTER__)
315 #define	_Static_assert(x, y)	__Static_assert(x, __COUNTER__)
316 #define	__Static_assert(x, y)	___Static_assert(x, y)
317 #define	___Static_assert(x, y)	typedef char __assert_ ## y[(x) ? 1 : -1] \
318 				__unused
319 #else
320 #define	_Static_assert(x, y)	struct __hack
321 #endif
322 #endif
323 
324 #endif /* __STDC_VERSION__ || __STDC_VERSION__ < 201112L */
325 
326 /*
327  * Emulation of C11 _Generic().  Unlike the previously defined C11
328  * keywords, it is not possible to implement this using exactly the same
329  * syntax.  Therefore implement something similar under the name
330  * __generic().  Unlike _Generic(), this macro can only distinguish
331  * between a single type, so it requires nested invocations to
332  * distinguish multiple cases.
333  */
334 
335 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
336     __has_extension(c_generic_selections)
337 #define	__generic(expr, t, yes, no)					\
338 	_Generic(expr, t: yes, default: no)
339 #elif __GNUC_PREREQ__(3, 1) && !defined(__cplusplus)
340 #define	__generic(expr, t, yes, no)					\
341 	__builtin_choose_expr(						\
342 	    __builtin_types_compatible_p(__typeof(expr), t), yes, no)
343 #endif
344 
345 /*
346  * C99 Static array indices in function parameter declarations.  Syntax such as:
347  * void bar(int myArray[static 10]);
348  * is allowed in C99 but not in C++.  Define __min_size appropriately so
349  * headers using it can be compiled in either language.  Use like this:
350  * void bar(int myArray[__min_size(10)]);
351  */
352 #if !defined(__cplusplus) && \
353     (defined(__clang__) || __GNUC_PREREQ__(4, 6)) && \
354     (!defined(__STDC_VERSION__) || (__STDC_VERSION__ >= 199901))
355 #define __min_size(x)	static (x)
356 #else
357 #define __min_size(x)	(x)
358 #endif
359 
360 #if __GNUC_PREREQ__(2, 96)
361 # if __GNUC_PREREQ__(11, 0)
362 #  define       __malloc_like_with_free(_f,_a) __attribute__((__malloc__, __malloc__(_f,_a)))
363 # else
364 #  define       __malloc_like_with_free(_f,_a) __attribute__((__malloc__))
365 # endif
366 # define	__pure		__attribute__((__pure__))
367 #else
368 #define __malloc_like_with_free(free,free_arg)
369 #define	__pure
370 #endif
371 
372 #define	__malloc_like __malloc_like_with_free(free, 1)
373 
374 #ifndef __always_inline
375 #if __GNUC_PREREQ__(3, 1)
376 #define	__always_inline	__inline__ __attribute__((__always_inline__))
377 #else
378 #define	__always_inline
379 #endif
380 #endif
381 
382 #ifndef __noinline
383 #if __GNUC_PREREQ__(3, 1)
384 #define	__noinline	__attribute__ ((__noinline__))
385 #else
386 #define	__noinline
387 #endif
388 #endif
389 
390 #if defined(__clang__) && defined(__nonnull)
391 /* Clang has a builtin macro __nonnull for the _Nonnull qualifier */
392 #undef __nonnull
393 #endif
394 #if __GNUC_PREREQ__(3, 3)
395 #define	__nonnull(x)	__attribute__((__nonnull__ x))
396 #define	__nonnull_all	__attribute__((__nonnull__))
397 #else
398 #define	__nonnull(x)
399 #define	__nonnull_all
400 #endif
401 
402 #if __GNUC_PREREQ__(3, 4)
403 #define	__fastcall	__attribute__((__fastcall__))
404 #define	__result_use_check	__attribute__((__warn_unused_result__))
405 #else
406 #define	__fastcall
407 #define	__result_use_check
408 #endif
409 
410 #if __GNUC_PREREQ__(4, 1)
411 #define	__returns_twice	__attribute__((__returns_twice__))
412 #else
413 #define	__returns_twice
414 #endif
415 
416 #if __GNUC_PREREQ__(4, 6) || __has_builtin(__builtin_unreachable)
417 #define	__unreachable()	__builtin_unreachable()
418 #else
419 #define	__unreachable()	((void)0)
420 #endif
421 
422 /* XXX: should use `#if __STDC_VERSION__ < 199901'. */
423 #if !__GNUC_PREREQ__(2, 7) && !defined(__COMPCERT__)
424 #define	__func__	NULL
425 #endif
426 
427 /*
428  * GCC 2.95 provides `__restrict' as an extension to C90 to support the
429  * C99-specific `restrict' type qualifier.  We happen to use `__restrict' as
430  * a way to define the `restrict' type qualifier without disturbing older
431  * software that is unaware of C99 keywords.
432  */
433 #if !(__GNUC__ == 2 && __GNUC_MINOR__ == 95)
434 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901
435 #define	__restrict
436 #else
437 #define	__restrict	restrict
438 #endif
439 #endif
440 
441 /*
442  * GNU C version 2.96 adds explicit branch prediction so that
443  * the CPU back-end can hint the processor and also so that
444  * code blocks can be reordered such that the predicted path
445  * sees a more linear flow, thus improving cache behavior, etc.
446  *
447  * The following two macros provide us with a way to utilize this
448  * compiler feature.  Use __predict_true() if you expect the expression
449  * to evaluate to true, and __predict_false() if you expect the
450  * expression to evaluate to false.
451  *
452  * A few notes about usage:
453  *
454  *	* Generally, __predict_false() error condition checks (unless
455  *	  you have some _strong_ reason to do otherwise, in which case
456  *	  document it), and/or __predict_true() `no-error' condition
457  *	  checks, assuming you want to optimize for the no-error case.
458  *
459  *	* Other than that, if you don't know the likelihood of a test
460  *	  succeeding from empirical or other `hard' evidence, don't
461  *	  make predictions.
462  *
463  *	* These are meant to be used in places that are run `a lot'.
464  *	  It is wasteful to make predictions in code that is run
465  *	  seldomly (e.g. at subsystem initialization time) as the
466  *	  basic block reordering that this affects can often generate
467  *	  larger code.
468  */
469 #if __GNUC_PREREQ__(2, 96)
470 #define	__predict_true(exp)     __builtin_expect((exp), 1)
471 #define	__predict_false(exp)    __builtin_expect((exp), 0)
472 #else
473 #define	__predict_true(exp)     (exp)
474 #define	__predict_false(exp)    (exp)
475 #endif
476 
477 #if __GNUC_PREREQ__(4, 0)
478 #define	__null_sentinel	__attribute__((__sentinel__))
479 #define	__exported	__attribute__((__visibility__("default")))
480 /* Only default visibility is supported on PE/COFF targets. */
481 #ifndef __CYGWIN__
482 #define	__hidden	__attribute__((__visibility__("hidden")))
483 #else
484 #define	__hidden
485 #endif
486 #else
487 #define	__null_sentinel
488 #define	__exported
489 #define	__hidden
490 #endif
491 
492 #define __offsetof(type, field)	offsetof(type, field)
493 #define	__rangeof(type, start, end) \
494 	(__offsetof(type, end) - __offsetof(type, start))
495 
496 /*
497  * Given the pointer x to the member m of the struct s, return
498  * a pointer to the containing structure.  When using GCC, we first
499  * assign pointer x to a local variable, to check that its type is
500  * compatible with member m.
501  */
502 #if __GNUC_PREREQ__(3, 1)
503 #define	__containerof(x, s, m) ({					\
504 	const volatile __typeof(((s *)0)->m) *__x = (x);		\
505 	__DEQUALIFY(s *, (const volatile char *)__x - __offsetof(s, m));\
506 })
507 #else
508 #define	__containerof(x, s, m)						\
509 	__DEQUALIFY(s *, (const volatile char *)(x) - __offsetof(s, m))
510 #endif
511 
512 /*
513  * Compiler-dependent macros to declare that functions take printf-like
514  * or scanf-like arguments.  They are null except for versions of gcc
515  * that are known to support the features properly (old versions of gcc-2
516  * didn't permit keeping the keywords out of the application namespace).
517  */
518 #if !__GNUC_PREREQ__(2, 7)
519 #define	__printflike(fmtarg, firstvararg)
520 #define	__scanflike(fmtarg, firstvararg)
521 #define	__format_arg(fmtarg)
522 #define	__strfmonlike(fmtarg, firstvararg)
523 #define	__strftimelike(fmtarg, firstvararg)
524 #else
525 #define	__printflike(fmtarg, firstvararg) \
526 	    __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
527 #define	__scanflike(fmtarg, firstvararg) \
528 	    __attribute__((__format__ (__scanf__, fmtarg, firstvararg)))
529 #define	__format_arg(fmtarg)	__attribute__((__format_arg__ (fmtarg)))
530 #define	__strfmonlike(fmtarg, firstvararg) \
531 	    __attribute__((__format__ (__strfmon__, fmtarg, firstvararg)))
532 #define	__strftimelike(fmtarg, firstvararg) \
533 	    __attribute__((__format__ (__strftime__, fmtarg, firstvararg)))
534 #endif
535 
536 /* Compiler-dependent macros that rely on FreeBSD-specific extensions. */
537 #if defined(__FreeBSD_cc_version) && __FreeBSD_cc_version >= 300001 && \
538     defined(__GNUC__)
539 #define	__printf0like(fmtarg, firstvararg) \
540 	    __attribute__((__format__ (__printf0__, fmtarg, firstvararg)))
541 #else
542 #define	__printf0like(fmtarg, firstvararg)
543 #endif
544 
545 #if defined(_HAVE_ALIAS_ATTRIBUTE)
546 #define	__strong_reference(sym,aliassym)	\
547     extern __typeof (sym) aliassym __attribute__ ((__alias__ (__STRING(sym))))
548 #endif
549 
550 #if defined(_HAVE_WEAK_ATTRIBUTE) && defined(_HAVE_ALIAS_ATTRIBUTE)
551 #define	__weak_reference(sym,aliassym)	\
552     extern __typeof (sym) aliassym __attribute__ ((__weak__, __alias__ (__STRING(sym))))
553 #endif
554 
555 /*
556    Taken from glibc:
557    Add the compiler optimization to inhibit loop transformation to library
558    calls.  This is used to avoid recursive calls in memset and memmove
559    default implementations.
560 */
561 #if defined(_HAVE_CC_INHIBIT_LOOP_TO_LIBCALL)
562 # define __inhibit_loop_to_libcall \
563   __attribute__ ((__optimize__ ("-fno-tree-loop-distribute-patterns")))
564 #elif defined(_HAVE_NO_BUILTIN_ATTRIBUTE)
565 # define __inhibit_loop_to_libcall \
566     __attribute__((no_builtin))
567 #else
568 # define __inhibit_loop_to_libcall
569 #endif
570 
571 /*
572  * Tell optimizer to not add new builtin calls when optimizing a
573  * function body.
574  */
575 #if defined(_HAVE_NO_BUILTIN_ATTRIBUTE)
576 # define __inhibit_new_builtin_calls \
577     __attribute__((no_builtin))
578 #else
579 #define __inhibit_new_builtin_calls
580 #endif
581 
582 #if defined(__GNUC__)
583 #ifndef __weak_reference
584 #ifdef __ELF__
585 #ifdef __STDC__
586 #define	__weak_reference(sym,alias)	\
587 	__asm__(".weak " #alias);	\
588 	__asm__(".equ "  #alias ", " #sym)
589 #else
590 #define	__weak_reference(sym,alias)	\
591 	__asm__(".weak alias");		\
592 	__asm__(".equ alias, sym")
593 #endif
594 #elif __clang__
595 #ifdef __MACH__
596 /* Macos prefixes all C symbols with an underscore, this needs to be done manually in asm */
597 
598 /* So far I have not been able to create on Macos an exported symbol that itself
599  * is weak but aliases a strong symbol. A workaround is to make the original
600  * symbol weak and the alias symbol will automatically become weak too. */
601 /* Hint: use `nm -m obj.o` to check the symbols weak/strong on Mac */
602 #define __weak_reference(sym,alias) \
603 	__asm__(".weak_definition _" #sym); \
604 	__asm__(".globl _" #alias); \
605 	__asm__(".set _" #alias ", _" #sym)
606 #elif defined(__STDC__)
607 #define __weak_reference(sym,alias) \
608 	__asm__(".weak_reference " #alias); \
609 	__asm__(".globl " #alias); \
610 	__asm__(".set " #alias ", " #sym)
611 #else
612 #define __weak_reference(sym,alias) \
613 	__asm__(".weak_reference alias");\
614 	__asm__(".set alias, sym")
615 #endif
616 #else	/* !__ELF__ && !__clang__ */
617 #ifdef __STDC__
618 #define	__weak_reference(sym,alias)	\
619 	__asm__(".stabs \"_" #alias "\",11,0,0,0");	\
620 	__asm__(".stabs \"_" #sym "\",1,0,0,0")
621 #else
622 #define	__weak_reference(sym,alias)	\
623 	__asm__(".stabs \"_/**/alias\",11,0,0,0");	\
624 	__asm__(".stabs \"_/**/sym\",1,0,0,0")
625 #endif
626 #endif
627 #endif
628 
629 #endif	/* __GNUC__ */
630 
631 #ifndef	__FBSDID
632 #define	__FBSDID(s)	struct __hack
633 #endif
634 
635 #ifndef	__RCSID
636 #define	__RCSID(s)	struct __hack
637 #endif
638 
639 #ifndef	__RCSID_SOURCE
640 #define	__RCSID_SOURCE(s)	struct __hack
641 #endif
642 
643 #ifndef	__SCCSID
644 #define	__SCCSID(s)	struct __hack
645 #endif
646 
647 #ifndef	__COPYRIGHT
648 #define	__COPYRIGHT(s)	struct __hack
649 #endif
650 
651 #ifndef	__DECONST
652 #define	__DECONST(type, var)	((type)(__uintptr_t)(const void *)(var))
653 #endif
654 
655 #ifndef	__DEVOLATILE
656 #define	__DEVOLATILE(type, var)	((type)(__uintptr_t)(volatile void *)(var))
657 #endif
658 
659 #ifndef	__DEQUALIFY
660 #define	__DEQUALIFY(type, var)	((type)(__uintptr_t)(const volatile void *)(var))
661 #endif
662 
663 /*
664  * Nullability qualifiers: currently only supported by Clang.
665  */
666 #if !(defined(__clang__) && __has_feature(nullability))
667 #define	_Nonnull
668 #define	_Nullable
669 #define	_Null_unspecified
670 #define	__NULLABILITY_PRAGMA_PUSH
671 #define	__NULLABILITY_PRAGMA_POP
672 #else
673 #define	__NULLABILITY_PRAGMA_PUSH _Pragma("clang diagnostic push")	\
674 	_Pragma("clang diagnostic ignored \"-Wnullability-completeness\"")
675 #define	__NULLABILITY_PRAGMA_POP _Pragma("clang diagnostic pop")
676 #endif
677 
678 /*
679  * Type Safety Checking
680  *
681  * Clang provides additional attributes to enable checking type safety
682  * properties that cannot be enforced by the C type system.
683  */
684 
685 #if __has_attribute(__argument_with_type_tag__) && \
686     __has_attribute(__type_tag_for_datatype__)
687 #define	__arg_type_tag(arg_kind, arg_idx, type_tag_idx) \
688 	    __attribute__((__argument_with_type_tag__(arg_kind, arg_idx, type_tag_idx)))
689 #define	__datatype_type_tag(kind, type) \
690 	    __attribute__((__type_tag_for_datatype__(kind, type)))
691 #else
692 #define	__arg_type_tag(arg_kind, arg_idx, type_tag_idx)
693 #define	__datatype_type_tag(kind, type)
694 #endif
695 
696 /*
697  * Lock annotations.
698  *
699  * Clang provides support for doing basic thread-safety tests at
700  * compile-time, by marking which locks will/should be held when
701  * entering/leaving a functions.
702  *
703  * Furthermore, it is also possible to annotate variables and structure
704  * members to enforce that they are only accessed when certain locks are
705  * held.
706  */
707 
708 #if __has_extension(c_thread_safety_attributes)
709 #define	__lock_annotate(x)	__attribute__((x))
710 #else
711 #define	__lock_annotate(x)
712 #endif
713 
714 /* Structure implements a lock. */
715 /* FIXME: Use __lockable__, etc. to avoid colliding with user namespace macros,
716  * once clang is fixed: https://bugs.llvm.org/show_bug.cgi?id=34319 */
717 #define	__lockable		__lock_annotate(lockable)
718 
719 /* Function acquires an exclusive or shared lock. */
720 #define	__locks_exclusive(...) \
721 	__lock_annotate(exclusive_lock_function(__VA_ARGS__))
722 #define	__locks_shared(...) \
723 	__lock_annotate(shared_lock_function(__VA_ARGS__))
724 
725 /* Function attempts to acquire an exclusive or shared lock. */
726 #define	__trylocks_exclusive(...) \
727 	__lock_annotate(exclusive_trylock_function(__VA_ARGS__))
728 #define	__trylocks_shared(...) \
729 	__lock_annotate(shared_trylock_function(__VA_ARGS__))
730 
731 /* Function releases a lock. */
732 #define	__unlocks(...)		__lock_annotate(unlock_function(__VA_ARGS__))
733 
734 /* Function asserts that an exclusive or shared lock is held. */
735 #define	__asserts_exclusive(...) \
736 	__lock_annotate(assert_exclusive_lock(__VA_ARGS__))
737 #define	__asserts_shared(...) \
738 	__lock_annotate(assert_shared_lock(__VA_ARGS__))
739 
740 /* Function requires that an exclusive or shared lock is or is not held. */
741 #define	__requires_exclusive(...) \
742 	__lock_annotate(exclusive_locks_required(__VA_ARGS__))
743 #define	__requires_shared(...) \
744 	__lock_annotate(shared_locks_required(__VA_ARGS__))
745 #define	__requires_unlocked(...) \
746 	__lock_annotate(locks_excluded(__VA_ARGS__))
747 
748 /* Function should not be analyzed. */
749 #define	__no_lock_analysis	__lock_annotate(no_thread_safety_analysis)
750 
751 /*
752  * Function or variable should not be sanitized, e.g., by AddressSanitizer.
753  * GCC has the nosanitize attribute, but as a function attribute only, and
754  * warns on use as a variable attribute.
755  */
756 #if __has_attribute(no_sanitize) && defined(__clang__)
757 #ifdef _KERNEL
758 #define __nosanitizeaddress	__attribute__((no_sanitize("kernel-address")))
759 #define __nosanitizememory	__attribute__((no_sanitize("kernel-memory")))
760 #else
761 #define __nosanitizeaddress	__attribute__((no_sanitize("address")))
762 #define __nosanitizememory	__attribute__((no_sanitize("memory")))
763 #endif
764 #define __nosanitizethread	__attribute__((no_sanitize("thread")))
765 #else
766 #define __nosanitizeaddress
767 #define __nosanitizememory
768 #define __nosanitizethread
769 #endif
770 
771 /*
772  * fall-through case statement annotations
773  */
774 #if __cplusplus >= 201703L || __STDC_VERSION__ > 201710L
775 /* Standard C++17/C23 attribute */
776 #define FALLTHROUGH [[fallthrough]]
777 #elif __has_attribute(fallthrough)
778 /* Non-standard but supported by at least gcc and clang */
779 #define FALLTHROUGH __attribute__((fallthrough))
780 #else
781 #define FALLTHROUGH do { } while(0)
782 #endif
783 
784 /* Guard variables and structure members by lock. */
785 #define	__guarded_by(x)		__lock_annotate(guarded_by(x))
786 #define	__pt_guarded_by(x)	__lock_annotate(pt_guarded_by(x))
787 
788 /* Alignment builtins for better type checking and improved code generation. */
789 /* Provide fallback versions for other compilers (GCC/Clang < 10): */
790 #if !__has_builtin(__builtin_is_aligned)
791 #define __builtin_is_aligned(x, align)	\
792 	(((__uintptr_t)x & ((align) - 1)) == 0)
793 #endif
794 #if !__has_builtin(__builtin_align_up)
795 #define __builtin_align_up(x, align)	\
796 	((__typeof__(x))(((__uintptr_t)(x)+((align)-1))&(~((align)-1))))
797 #endif
798 #if !__has_builtin(__builtin_align_down)
799 #define __builtin_align_down(x, align)	\
800 	((__typeof__(x))((x)&(~((align)-1))))
801 #endif
802 
803 #define __align_up(x, y) __builtin_align_up(x, y)
804 #define __align_down(x, y) __builtin_align_down(x, y)
805 #define __is_aligned(x, y) __builtin_is_aligned(x, y)
806 
807 #endif /* !_SYS_CDEFS_H_ */
808