]> pd.if.org Git - pdclib.old/blob - internals/_PDCLIB_aux.h
bc13897a7d4254383cc3890499c69ba7459f4a09
[pdclib.old] / internals / _PDCLIB_aux.h
1 #ifndef _PDCLIB_AUX_H
2 #define _PDCLIB_AUX_H
3
4 /* Auxiliary PDCLib code <_PDCLIB_aux.h>
5
6    This file is part of the Public Domain C Library (PDCLib).
7    Permission is granted to use, modify, and / or redistribute at will.
8 */
9
10 /* -------------------------------------------------------------------------- */
11 /* You should not have to edit anything in this file; if you DO have to, it   */
12 /* would be considered a bug / missing feature: notify the author(s).         */
13 /* -------------------------------------------------------------------------- */
14
15 /* -------------------------------------------------------------------------- */
16 /* Standard Version                                                           */
17 /* -------------------------------------------------------------------------- */
18
19 /* Many a compiler gets this wrong, so you might have to hardcode it instead. */
20
21 #if __STDC__ != 1
22 #error Compiler does not define _ _STDC_ _ to 1 (not standard-compliant)!
23 #endif
24
25 #if defined(_PDCLIB_C_VERSION)
26     /* Pass - conditional simplification case */
27 #elif !defined(__STDC_VERSION__)
28     #define _PDCLIB_C_VERSION 1990
29 #elif __STDC_VERSION__ == 199409L
30     #define _PDCLIB_C_VERSION 1995
31 #elif __STDC_VERSION__ == 199901L
32     #define _PDCLIB_C_VERSION 1999
33 #elif __STDC_VERSION__ == 201112L
34     #define _PDCLIB_C_VERSION 2011
35 #else
36     #error Unsupported _ _STDC_VERSION_ _ (__STDC_VERSION__) (supported: ISO/IEC 9899:1990, 9899/AMD1:1995, 9899:1999, 9899:2011).
37 #endif
38
39 #if !defined(__cplusplus) || defined(_PDCLIB_CXX_VERSION)
40    #define _PDCLIB_CXX_VERSION 0
41 #elif __cplusplus == 201103L
42     #define _PDCLIB_CXX_VERSION 2011
43     /* TODO: Do we want this? */
44     #if _PDCLIB_C_VERSION < 2011
45         #undef _PDCLIB_C_VERSION
46         #define _PDCLIB_C_VERSION 2011
47     #endif
48 #elif __cplusplus == 199711L
49    #define _PDCLIB_CXX_VERSION 1997
50 #else
51    #error Unsupported _ _cplusplus (__cplusplus) (supported: ISO/IEC 14882:1997, ISO/IEC 14882:2011).
52 #endif
53
54 #ifndef __STDC_HOSTED__
55     #error Compiler does not define _ _STDC_HOSTED_ _ (not standard-compliant)!
56 #elif __STDC_HOSTED__ == 0
57     #define _PDCLIB_HOSTED 0
58 #elif __STDC_HOSTED__ == 1
59     #define _PDCLIB_HOSTED 1
60 #else
61     #error Compiler does not define _ _STDC_HOSTED_ _ to 0 or 1 (not standard-compliant)!
62 #endif
63
64 #ifdef __cplusplus
65     #define _PDCLIB_BEGIN_EXTERN_C extern "C" {
66     #define _PDCLIB_END_EXTERN_C }
67     typedef bool _PDCLIB_bool;
68 #else
69     #define _PDCLIB_BEGIN_EXTERN_C
70     #define _PDCLIB_END_EXTERN_C
71     typedef _Bool _PDCLIB_bool;
72 #endif
73
74 /* Clang style feature detection macros
75  * Note: It is common to #define __has_feature(0) if undefined so the presence
76  * of this macro does not guarantee it to be working
77  */
78
79 #ifdef __has_feature
80    #define _PDCLIB_HAS_FEATURE(x) __has_feature(x)
81 #else
82    #define _PDCLIB_HAS_FEATURE(x) (0)
83 #endif
84
85 #ifdef __has_extension
86    #define _PDCLIB_HAS_EXTENSION(x) __has_extension(x)
87 #else
88    // Older versions of Clang use __has_feature instead
89    #define _PDCLIB_HAS_EXTENSION(x) _PDCLIB_HAS_FEATURE(x)
90 #endif
91
92 #ifdef __has_builtin
93    #define _PDCLIB_HAS_BUILTIN(x) __has_builtin(x)
94 #else
95    #define _PDCLIB_HAS_BUILTIN(x) (0)
96 #endif
97
98 #ifdef __has_attribute
99    #define _PDCLIB_HAS_ATTRIBUTE(x) __has_builtin(x)
100 #else
101    #define _PDCLIB_HAS_ATTRIBUTE(x) (0)
102 #endif
103
104 /* GCC feature detection macros */
105
106 #if defined(__GNUC__)
107     #define _PDCLIB_GCC_MIN(maj, min) \
108         ((__GNUC__ > maj) || (__GNUC__ == maj && __GNUC_MINOR__ >= min))
109 #else
110     #define _PDCLIB_GCC_MIN(maj, min) (0)
111 #endif
112
113 /* Hybrid GCC/Clang feature detection macros */
114 #define _PDCLIB_GCC_FEATURE(x, gccmaj, gccmin) \
115         (_PDCLIB_HAS_FEATURE(x) || _PDCLIB_GCC_MIN(gccmin, gccmaj))
116
117 #define _PDCLIB_GCC_EXTENSION(x, gccmaj, gccmin) \
118         (_PDCLIB_HAS_EXTENSION(x) || _PDCLIB_GCC_MIN(gccmin, gccmaj))
119
120 #define _PDCLIB_GCC_BUILTIN(x, gccmaj, gccmin) \
121         (_PDCLIB_HAS_BUILTIN(x) || _PDCLIB_GCC_MIN(gccmin, gccmaj))
122
123 #define _PDCLIB_GCC_ATTRIBUTE(x, gccmaj, gccmin) \
124         (_PDCLIB_HAS_ATTRIBUTE(x) || _PDCLIB_GCC_MIN(gccmin, gccmaj))
125
126 /* Extension & Language feature detection */
127
128 #if _PDCLIB_C_VERSION >= 1999 || defined(__cplusplus)
129     #ifndef __cplusplus
130         #define _PDCLIB_restrict restrict
131     #endif
132     #define _PDCLIB_inline   inline
133 #endif
134
135 #if _PDCLIB_CXX_VERSION >= 2011
136   #define _PDCLIB_nothrow     noexcept
137   #define _PDCLIB_noexcept(x) noexcept(x)
138 #elif _PDCLIB_CXX_VERSION
139   #define _PDCLIB_nothrow     throw()
140   #define _PDCLIB_noexcept
141 #endif
142
143 #if _PDCLIB_CXX_VERSION >= 2011 && _PDCLIB_GCC_FEATURE(cxx_attributes, 4, 8)
144     #define _PDCLIB_noreturn [[noreturn]]
145 #elif _PDCLIB_C_VERSION >= 2011 && _PDCLIB_GCC_FEATURE(c_noreturn, 4, 7)
146     #define _PDCLIB_noreturn _Noreturn
147 #endif
148
149 #ifdef _WIN32
150    #define _PDCLIB_EXPORT __declspec(dllexport)
151    #define _PDCLIB_IMPORT __declspec(dllimport)
152 #endif
153
154 #if !defined(_PDCLIB_EXPORT) && _PDCLIB_GCC_ATTRIBUTE(__visibility__, 4, 0)
155     #define _PDCLIB_EXPORT __attribute__((__visibility__("protected")))
156 #endif
157
158 #if !defined(_PDCLIB_HIDDEN) && _PDCLIB_GCC_ATTRIBUTE(__visibility__, 4, 0)
159     #define _PDCLIB_HIDDEN __attribute__((__visibility__("hidden")))
160 #endif
161
162 #if !defined(_PDCLIB_nothrow) && _PDCLIB_GCC_ATTRIBUTE(__nothrow__, 4, 0)
163     #define _PDCLIB_nothrow __attribute__((__nothrow__))
164     #define _PDCLIB_noexcept
165 #endif
166
167 #if !defined(_PDCLIB_restrict) && _PDCLIB_GCC_MIN(3, 0)
168     #define _PDCLIB_restrict __restrict
169 #endif
170
171 #if !defined(_PDCLIB_inline) && _PDCLIB_GCC_MIN(3, 0)
172     #define _PDCLIB_inline __inline
173 #endif
174
175 #if !defined(_PDCLIB_noreturn) && _PDCLIB_GCC_ATTRIBUTE(__noreturn__, 3, 0)
176     /* If you don't use __noreturn__, then stdnoreturn.h will break things! */
177     #define _PDCLIB_noreturn __attribute__((__noreturn__))
178 #endif
179
180 #if !defined(_PDCLIB_DEPRECATED) && _PDCLIB_GCC_ATTRIBUTE(__deprecated__, 3, 0)
181     #define _PDCLIB_DEPRECATED __attribute__ ((__deprecated__))
182 #endif
183
184 /* No-op fallbacks */
185
186 #ifndef _PDCLIB_nothrow
187   #define _PDCLIB_nothrow
188   #define _PDCLIB_noexcept
189 #endif
190
191 #ifndef _PDCLIB_EXPORT
192     #define _PDCLIB_EXPORT
193 #endif
194 #ifndef _PDCLIB_IMPORT
195     #define _PDCLIB_IMPORT
196 #endif
197 #ifndef _PDCLIB_HIDDEN
198     #define _PDCLIB_HIDDEN
199 #endif
200
201 #if defined(_PDCLIB_SHARED) 
202     #if defined(_PDCLIB_BUILD)
203         #define _PDCLIB_API _PDCLIB_EXPORT
204     #else
205         #define _PDCLIB_API _PDCLIB_IMPORT
206     #endif
207 #else
208     #define _PDCLIB_API
209 #endif
210
211 #ifndef _PDCLIB_restrict
212       #define _PDCLIB_restrict
213 #endif
214
215 #ifndef _PDCLIB_inline
216       #define _PDCLIB_inline
217 #endif
218
219 #ifndef _PDCLIB_noreturn
220       #define _PDCLIB_noreturn
221 #endif
222
223 #ifndef _PDCLIB_DEPRECATED
224     #define _PDCLIB_DEPRECATED
225 #endif
226
227 /*#if _PDCLIB_C_VERSION != 1999
228 #error PDCLib might not be fully conforming to either C89 or C95 prior to v2.x.
229 #endif*/
230
231 /* -------------------------------------------------------------------------- */
232 /* Helper macros:                                                             */
233 /* _PDCLIB_cc( x, y ) concatenates two preprocessor tokens without extending  */
234 /* _PDCLIB_concat( x, y ) concatenates two preprocessor tokens with extending */
235 /* -------------------------------------------------------------------------- */
236
237 #define _PDCLIB_cc( x, y )     x ## y
238 #define _PDCLIB_concat( x, y ) _PDCLIB_cc( x, y )
239 #define _PDCLIB_concat3( x, y, z ) _PDCLIB_concat( _PDCLIB_concat( x, y ), z )
240
241 #define _PDCLIB_symbol2value( x ) #x
242 #define _PDCLIB_symbol2string( x ) _PDCLIB_symbol2value( x )
243
244 /* Feature test macros
245  *
246  * All of the feature test macros come in the following forms
247  *   _PDCLIB_*_MIN(min):            Available in versions >= min
248  *   _PDCLIB_*_MINMAX(min, max):    Available in versions >= min <= max
249  *   _PDCLIB_*_MAX(max):            Availabel in versions <= max
250  *
251  * The defined tests are:
252  *   C:     C standard versions 
253  *              1990, 1995, 1999, 2011
254  *   CXX:   C++ standard versions 
255  *              1997, 2011
256  *   POSIX: POSIX extension versions.
257  *              1 (POSIX.2), 2 (POSIX.2), 199309L (POSIX.1b), 
258  *              199506L (POSIX.1c), 200112L (2001), 200809L (2008)
259  *   XOPEN: X/Open System Interface (XSI)/Single Unix Specification
260  *              0 (XPG4), 500 (SUSv2/UNIX98), 600 (SUSv3/UNIX03), 700 (SUSv4)
261  *
262  *   Additionally, the macros
263  *     _BSD_SOURCE, _SVID_SOURCE and _GNU_SOURCE
264  *   are adhered to. If _GNU_SOURCE is defined, _XOPEN_SOURCE and 
265  *   _POSIX_C_SOURCE are defined to their most recent values to match glibc 
266  *   behaviour
267  *
268  *   The intention of supporting these feature test macros is to ease 
269  *   application portability from these systems to PDCLib systems; in addition,
270  *   it eases support for these standards by systems supporting them which are 
271  *   using PDCLib as their default C library.
272  *
273  *   Applications targetting purely PDClib/PDCLib based platforms may define 
274  *   just _PDCLIB_EXTENSIONS, which will enable all supported extensions, plus
275  *   all features from all supported versions of C and C++.
276  *
277  */
278 #define _PDCLIB_C_MIN(min)         _PDCLIB_C_MINMAX(min, 3000)
279 #define _PDCLIB_CXX_MIN(min)     _PDCLIB_CXX_MINMAX(min, 3000)
280 #define _PDCLIB_XOPEN_MIN(min) _PDCLIB_XOPEN_MINMAX(min, 30000000)
281 #define _PDCLIB_POSIX_MIN(min) _PDCLIB_POSIX_MINMAX(min, 30000000)
282 #define _PDCLIB_C_MAX(max)         _PDCLIB_C_MINMAX(0, max)
283 #define _PDCLIB_CXX_MAX(max)     _PDCLIB_CXX_MINMAX(0, max)
284 #define _PDCLIB_XOPEN_MAX(max) _PDCLIB_XOPEN_MINMAX(0, max)
285 #define _PDCLIB_POSIX_MAX(max) _PDCLIB_POSIX_MINMAX(0, max)
286 #if defined(_PDCLIB_EXTENSIONS) || defined(_PDCLIB_BUILD)
287     #define _PDCLIB_C_MINMAX(min, max) 1
288     #define _PDCLIB_CXX_MINMAX(min, max) 1
289     #define _PDCLIB_POSIX_MINMAX(min, max) 1
290     #define _PDCLIB_XOPEN_MINMAX(min, max) 1
291
292     #undef _PDCLIB_EXTENSIONS
293     #undef _PDCLIB_BSD_SOURCE 
294     #undef _PDCLIB_SVID_SOURCE
295     #undef _PDCLIB_GNU_SOURCE
296
297     #define _PDCLIB_EXTENSIONS 1
298     #define _PDCLIB_BSD_SOURCE 1
299     #define _PDCLIB_SVID_SOURCE 1
300     #define _PDCLIB_GNU_SOURCE 1
301 #else
302     #define _PDCLIB_C_MINMAX(min, max) \
303         (_PDCLIB_C_VERSION >= (min) && _PDCLIB_C_VERSION <= (max))
304     #define _PDCLIB_CXX_MINMAX(min, max) \
305         (_PDCLIB_CXX_VERSION >= (min) && _PDCLIB_CXX_VERSION <= (max))
306     #define _PDCLIB_XOPEN_MINMAX(min, max) \
307         (defined(_XOPEN_SOURCE) \
308             && _XOPEN_SOURCE >= (min) && _XOPEN_SOURCE <= (max))
309     #define _PDCLIB_POSIX_MINMAX(min, max) \
310         (defined(_POSIX_C_SOURCE) \
311             && _POSIX_C_SOURCE >= (min) && _POSIX_C_SOURCE <= (max))
312
313     #if defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE-1 == -1)
314         /* If _XOPEN_SOURCE is defined as empty, redefine here as zero */
315         #undef _XOPEN_SOURCE
316         #define _XOPEN_SOURCE 0
317     #endif
318
319     #if defined(_GNU_SOURCE)
320         #define _PDCLIB_GNU_SOURCE 1
321         #define _PDCLIB_SVID_SOURCE 1
322         #define _PDCLIB_BSD_SOURCE 1
323         #undef _XOPEN_SOURCE
324         #define _XOPEN_SOURCE 700
325     #else
326         #define _PDCLIB_GNU_SOURCE 0
327     #endif
328
329     #if defined(_PDCLIB_BSD_SOURCE)
330         // pass
331     #elif defined(_BSD_SOURCE)
332         #define _PDCLIB_BSD_SOURCE 1
333     #else
334         #define _PDCLIB_BSD_SOURCE 0
335     #endif
336
337     #if defined(_PDCLIB_SVID_SOURCE)
338         // pass
339     #elif defined(_SVID_SOURCE)
340         #define _PDCLIB_SVID_SOURCE 1
341     #else
342         #define _PDCLIB_SVID_SOURCE 0
343     #endif
344
345     #if _PDCLIB_XOPEN_MIN(700) && !_PDCLIB_POSIX_MIN(200809L)
346         #undef _POSIX_C_SOURCE
347         #define _POSIX_C_SOURCE 2008098L    
348     #elif _PDCLIB_XOPEN_MIN(600) && !_PDCLIB_POSIX_MIN(200112L)
349         #undef _POSIX_C_SOURCE
350         #define _POSIX_C_SOURCE 200112L
351     #elif _PDCLIB_XOPEN_MIN(0) && !_PDCLIB_POSIX_MIN(2)
352         #undef _POSIX_C_SOURCE
353         #define _POSIX_C_SOURCE 2
354     #endif
355
356     #if defined(_POSIX_SOURCE) && !defined(_POSIX_C_SOURCE)
357         #define _POSIX_C_SOURCE 1
358     #endif
359 #endif
360
361 #endif