aboutsummaryrefslogtreecommitdiff
path: root/sys/sys/cdefs.h
diff options
context:
space:
mode:
authorEd Schouten <ed@FreeBSD.org>2012-01-05 10:43:03 +0000
committerEd Schouten <ed@FreeBSD.org>2012-01-05 10:43:03 +0000
commitf8fd121ca4ece923d3f7b82e1e62dd70d5aa66be (patch)
tree880f995a45e5dd6d91f28fccdc5678ea4c1ea27a /sys/sys/cdefs.h
parenta9e4a4780ab8740f7ee180992a7399a8a42dbc1f (diff)
downloadsrc-f8fd121ca4ece923d3f7b82e1e62dd70d5aa66be.tar.gz
src-f8fd121ca4ece923d3f7b82e1e62dd70d5aa66be.zip
Add __generic(), to be able to use a very simple _Generic().
Already introducing this allows us to be forward compatible with C11 compilers. By implementing <tgmath.h> on top of this interface, it becomes trivial to support both our existing GCC and newer compilers.
Notes
Notes: svn path=/head/; revision=229574
Diffstat (limited to 'sys/sys/cdefs.h')
-rw-r--r--sys/sys/cdefs.h18
1 files changed, 18 insertions, 0 deletions
diff --git a/sys/sys/cdefs.h b/sys/sys/cdefs.h
index 7dc04dceec98..373503f3c0d2 100644
--- a/sys/sys/cdefs.h
+++ b/sys/sys/cdefs.h
@@ -248,6 +248,24 @@
#endif
#endif
+/*
+ * Emulation of C11 _Generic(). Unlike the previously defined C11
+ * keywords, it is not possible to implement this using exactly the same
+ * syntax. Therefore implement something similar under the name
+ * __generic(). Unlike _Generic(), this macro can only distinguish
+ * between a single type, so it requires nested invocations to
+ * distinguish multiple cases.
+ */
+
+#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
+#define __generic(expr, t, yes, no) \
+ _Generic(expr, t: yes, default: no)
+#elif __GNUC_PREREQ__(3, 1)
+#define __generic(expr, t, yes, no) \
+ __builtin_choose_expr( \
+ __builtin_types_compatible_p(__typeof(expr), t), yes, no)
+#endif
+
#if __GNUC_PREREQ__(2, 96)
#define __malloc_like __attribute__((__malloc__))
#define __pure __attribute__((__pure__))