18ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman/*===---- stddef.h - Basic type definitions --------------------------------===
28ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman *
38ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman * Copyright (c) 2008 Eli Friedman
48ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman *
58ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman * Permission is hereby granted, free of charge, to any person obtaining a copy
68ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman * of this software and associated documentation files (the "Software"), to deal
78ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman * in the Software without restriction, including without limitation the rights
88ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
98ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman * copies of the Software, and to permit persons to whom the Software is
108ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman * furnished to do so, subject to the following conditions:
118ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman *
128ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman * The above copyright notice and this permission notice shall be included in
138ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman * all copies or substantial portions of the Software.
148ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman *
158ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
168ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
178ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
188ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
198ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
208ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
218ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman * THE SOFTWARE.
228ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman *
238ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman *===-----------------------------------------------------------------------===
248ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman */
258ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman
266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#if !defined(__STDDEF_H) || defined(__need_ptrdiff_t) ||                       \
276bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    defined(__need_size_t) || defined(__need_wchar_t) ||                       \
286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    defined(__need_NULL) || defined(__need_wint_t)
296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
306bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#if !defined(__need_ptrdiff_t) && !defined(__need_size_t) &&                   \
316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    !defined(__need_wchar_t) && !defined(__need_NULL) &&                       \
326bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    !defined(__need_wint_t)
338ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman#define __STDDEF_H
346bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#define __need_ptrdiff_t
356bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#define __need_size_t
366bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#define __need_wchar_t
376bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#define __need_NULL
386bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines/* __need_wint_t is intentionally not defined here. */
396bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#endif
408ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman
416bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#if defined(__need_ptrdiff_t)
42daf2e1c636799535a7eb058ffab93cdf52f466cbDouglas Gregor#if !defined(_PTRDIFF_T) || __has_feature(modules)
43daf2e1c636799535a7eb058ffab93cdf52f466cbDouglas Gregor/* Always define ptrdiff_t when modules are available. */
44daf2e1c636799535a7eb058ffab93cdf52f466cbDouglas Gregor#if !__has_feature(modules)
458da6bca896546cd98ad6aefaa3e9eba77736ea7bTed Kremenek#define _PTRDIFF_T
46daf2e1c636799535a7eb058ffab93cdf52f466cbDouglas Gregor#endif
471fc271c38aaa68dce73d6f1bee9f6fd3474e51f0Daniel Dunbartypedef __PTRDIFF_TYPE__ ptrdiff_t;
488da6bca896546cd98ad6aefaa3e9eba77736ea7bTed Kremenek#endif
496bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#undef __need_ptrdiff_t
506bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#endif /* defined(__need_ptrdiff_t) */
51daf2e1c636799535a7eb058ffab93cdf52f466cbDouglas Gregor
526bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#if defined(__need_size_t)
53daf2e1c636799535a7eb058ffab93cdf52f466cbDouglas Gregor#if !defined(_SIZE_T) || __has_feature(modules)
54daf2e1c636799535a7eb058ffab93cdf52f466cbDouglas Gregor/* Always define size_t when modules are available. */
55daf2e1c636799535a7eb058ffab93cdf52f466cbDouglas Gregor#if !__has_feature(modules)
5621259bafdc0e113c8bdce51d7911cc9880da1892Ted Kremenek#define _SIZE_T
57daf2e1c636799535a7eb058ffab93cdf52f466cbDouglas Gregor#endif
581fc271c38aaa68dce73d6f1bee9f6fd3474e51f0Daniel Dunbartypedef __SIZE_TYPE__ size_t;
5921259bafdc0e113c8bdce51d7911cc9880da1892Ted Kremenek#endif
606bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#undef __need_size_t
616bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#endif /*defined(__need_size_t) */
62daf2e1c636799535a7eb058ffab93cdf52f466cbDouglas Gregor
636bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#if defined(__STDDEF_H)
64de5ed04b08dcc23ac2caad282c372d541f60c6eeHans Wennborg/* ISO9899:2011 7.20 (C11 Annex K): Define rsize_t if __STDC_WANT_LIB_EXT1__ is
652943d3dded877fbe0bb965756f047d379032d4baDaniel Dunbar * enabled. */
662943d3dded877fbe0bb965756f047d379032d4baDaniel Dunbar#if (defined(__STDC_WANT_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__ >= 1 && \
672943d3dded877fbe0bb965756f047d379032d4baDaniel Dunbar     !defined(_RSIZE_T)) || __has_feature(modules)
682943d3dded877fbe0bb965756f047d379032d4baDaniel Dunbar/* Always define rsize_t when modules are available. */
692943d3dded877fbe0bb965756f047d379032d4baDaniel Dunbar#if !__has_feature(modules)
702943d3dded877fbe0bb965756f047d379032d4baDaniel Dunbar#define _RSIZE_T
712943d3dded877fbe0bb965756f047d379032d4baDaniel Dunbar#endif
722943d3dded877fbe0bb965756f047d379032d4baDaniel Dunbartypedef __SIZE_TYPE__ rsize_t;
732943d3dded877fbe0bb965756f047d379032d4baDaniel Dunbar#endif
746bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#endif /* defined(__STDDEF_H) */
752943d3dded877fbe0bb965756f047d379032d4baDaniel Dunbar
766bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#if defined(__need_wchar_t)
778ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman#ifndef __cplusplus
78daf2e1c636799535a7eb058ffab93cdf52f466cbDouglas Gregor/* Always define wchar_t when modules are available. */
79daf2e1c636799535a7eb058ffab93cdf52f466cbDouglas Gregor#if !defined(_WCHAR_T) || __has_feature(modules)
80daf2e1c636799535a7eb058ffab93cdf52f466cbDouglas Gregor#if !__has_feature(modules)
8121259bafdc0e113c8bdce51d7911cc9880da1892Ted Kremenek#define _WCHAR_T
82c814c64c6f7e7a26f4af0d040ad04b69917a36c8Hans Wennborg#if defined(_MSC_EXTENSIONS)
83c814c64c6f7e7a26f4af0d040ad04b69917a36c8Hans Wennborg#define _WCHAR_T_DEFINED
84c814c64c6f7e7a26f4af0d040ad04b69917a36c8Hans Wennborg#endif
85daf2e1c636799535a7eb058ffab93cdf52f466cbDouglas Gregor#endif
866d6370ee10ddcd8cacc44c2d6796800c1325c732Chris Lattnertypedef __WCHAR_TYPE__ wchar_t;
878ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman#endif
8821259bafdc0e113c8bdce51d7911cc9880da1892Ted Kremenek#endif
896bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#undef __need_wchar_t
906bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#endif /* defined(__need_wchar_t) */
918ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman
926bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#if defined(__need_NULL)
9321259bafdc0e113c8bdce51d7911cc9880da1892Ted Kremenek#undef NULL
9404f5db699e5c1198f30cba17fd12f38805ab04dfArgyrios Kyrtzidis#ifdef __cplusplus
9508e46f97ec8c00cc1bfe87fcd014db211a878ffbNico Weber#  if !defined(__MINGW32__) && !defined(_MSC_VER)
9608e46f97ec8c00cc1bfe87fcd014db211a878ffbNico Weber#    define NULL __null
9708e46f97ec8c00cc1bfe87fcd014db211a878ffbNico Weber#  else
9808e46f97ec8c00cc1bfe87fcd014db211a878ffbNico Weber#    define NULL 0
9908e46f97ec8c00cc1bfe87fcd014db211a878ffbNico Weber#  endif
100f8e61b371d085516b8b9b906099db3e0b915a7c0Anders Carlsson#else
10108e46f97ec8c00cc1bfe87fcd014db211a878ffbNico Weber#  define NULL ((void*)0)
102f8e61b371d085516b8b9b906099db3e0b915a7c0Anders Carlsson#endif
103e25d3df6df39ecca4dad4647be2713cb85653634Douglas Gregor#ifdef __cplusplus
104e25d3df6df39ecca4dad4647be2713cb85653634Douglas Gregor#if defined(_MSC_EXTENSIONS) && defined(_NATIVE_NULLPTR_SUPPORTED)
105e25d3df6df39ecca4dad4647be2713cb85653634Douglas Gregornamespace std { typedef decltype(nullptr) nullptr_t; }
106e25d3df6df39ecca4dad4647be2713cb85653634Douglas Gregorusing ::std::nullptr_t;
107e25d3df6df39ecca4dad4647be2713cb85653634Douglas Gregor#endif
108e25d3df6df39ecca4dad4647be2713cb85653634Douglas Gregor#endif
1096bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#undef __need_NULL
1106bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#endif /* defined(__need_NULL) */
1116bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
1126bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#if defined(__STDDEF_H)
113e25d3df6df39ecca4dad4647be2713cb85653634Douglas Gregor
114651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#if __STDC_VERSION__ >= 201112L || __cplusplus >= 201103L
1156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#if !defined(__CLANG_MAX_ALIGN_T_DEFINED) || __has_feature(modules)
116651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#ifndef _MSC_VER
117651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinestypedef struct {
118651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  long long __clang_max_align_nonce1
119651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      __attribute__((__aligned__(__alignof__(long long))));
120651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  long double __clang_max_align_nonce2
121651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      __attribute__((__aligned__(__alignof__(long double))));
122651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines} max_align_t;
123651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#else
124651f13cea278ec967336033dd032faef0e9fc2ecStephen Hinestypedef double max_align_t;
125651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#endif
126651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#define __CLANG_MAX_ALIGN_T_DEFINED
127651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#endif
1286bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#endif
129651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1308ba179d4a39c79ae486472a5f413f763ffcc0739Eli Friedman#define offsetof(t, d) __builtin_offsetof(t, d)
1316bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#endif  /* __STDDEF_H */
13251f56fc36e6d441b6dd722cc09c4ac9f081c0974NAKAMURA Takumi
13351f56fc36e6d441b6dd722cc09c4ac9f081c0974NAKAMURA Takumi/* Some C libraries expect to see a wint_t here. Others (notably MinGW) will use
134fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner__WINT_TYPE__ directly; accommodate both by requiring __need_wint_t */
13551f56fc36e6d441b6dd722cc09c4ac9f081c0974NAKAMURA Takumi#if defined(__need_wint_t)
136daf2e1c636799535a7eb058ffab93cdf52f466cbDouglas Gregor/* Always define wint_t when modules are available. */
137daf2e1c636799535a7eb058ffab93cdf52f466cbDouglas Gregor#if !defined(_WINT_T) || __has_feature(modules)
138daf2e1c636799535a7eb058ffab93cdf52f466cbDouglas Gregor#if !__has_feature(modules)
13951f56fc36e6d441b6dd722cc09c4ac9f081c0974NAKAMURA Takumi#define _WINT_T
140daf2e1c636799535a7eb058ffab93cdf52f466cbDouglas Gregor#endif
14151f56fc36e6d441b6dd722cc09c4ac9f081c0974NAKAMURA Takumitypedef __WINT_TYPE__ wint_t;
142daf2e1c636799535a7eb058ffab93cdf52f466cbDouglas Gregor#endif
14351f56fc36e6d441b6dd722cc09c4ac9f081c0974NAKAMURA Takumi#undef __need_wint_t
14451f56fc36e6d441b6dd722cc09c4ac9f081c0974NAKAMURA Takumi#endif /* __need_wint_t */
1456bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
1466bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#endif
147