1include "config/public_api.td"
2
3include "spec/gnu_ext.td"
4include "spec/linux.td"
5include "spec/llvm_libc_ext.td"
6include "spec/posix.td"
7include "spec/stdc.td"
8
9def AssertMacro : MacroDef<"assert"> {
10  let Defn = [{
11    #undef assert
12
13    #ifdef NDEBUG
14    #define assert(e) (void)0
15    #else
16
17    #ifdef __cplusplus
18    extern "C"
19    #endif
20    _Noreturn void __assert_fail(const char *, const char *, unsigned, const char *);
21
22    #define assert(e)  \
23      ((e) ? (void)0 : __assert_fail(#e, __FILE__, __LINE__, __PRETTY_FUNCTION__))
24
25    #endif
26  }];
27}
28
29def StaticAssertMacro : MacroDef<"static_assert"> {
30  let Defn = [{
31    #ifndef __cplusplus
32    #undef static_assert
33    #define static_assert _Static_assert
34    #endif
35  }];
36}
37
38def NullMacro : MacroDef<"NULL"> {
39  let Defn = [{
40    #define __need_NULL
41    #include <stddef.h>
42  }];
43}
44
45def ErrnoMacro : MacroDef<"errno"> {
46  let Defn = [{
47    #ifdef __cplusplus
48    extern "C"
49    #endif
50    int *__errno_location();
51    #define errno (*__errno_location())
52  }];
53}
54
55def AssertAPI : PublicAPI<"assert.h"> {
56  let Macros = [
57    AssertMacro,
58    StaticAssertMacro,
59  ];
60}
61
62def CTypeAPI : PublicAPI<"ctype.h"> {
63}
64
65def FCntlAPI : PublicAPI<"fcntl.h"> {
66  let Types = ["mode_t"];
67}
68
69def IntTypesAPI : PublicAPI<"inttypes.h"> {
70  let Types = ["imaxdiv_t"];
71}
72
73def MathErrHandlingMacro : MacroDef<"math_errhandling"> {
74  let Defn = [{
75    #ifndef math_errhandling
76    #ifdef __FAST_MATH__
77    #define math_errhandling 0
78    #elif defined __NO_MATH_ERRNO__
79    #define math_errhandling (MATH_ERREXCEPT)
80    #else
81    #define math_errhandling (MATH_ERRNO | MATH_ERREXCEPT)
82    #endif
83    #endif // math_errhandling not defined
84  }];
85}
86
87def IsFiniteMacro : MacroDef<"isfinite"> {
88  let Defn = [{
89    #define isfinite(x) __builtin_isfinite(x)
90  }];
91}
92
93def IsInfMacro : MacroDef<"isinf"> {
94  let Defn = [{
95    #define isinf(x) __builtin_isinf(x)
96  }];
97}
98
99def IsNanMacro : MacroDef<"isnan"> {
100  let Defn = [{
101    #define isnan(x) __builtin_isnan(x)
102  }];
103}
104
105def MathAPI : PublicAPI<"math.h"> {
106  let Macros = [
107    SimpleMacroDef<"MATH_ERRNO", "1">,
108    SimpleMacroDef<"MATH_ERREXCEPT", "2">,
109    MathErrHandlingMacro,
110
111    SimpleMacroDef<"INFINITY", "__builtin_inff()">,
112    SimpleMacroDef<"NAN", "__builtin_nanf(\"\")">,
113
114    SimpleMacroDef<"FP_ILOGB0", "(-__INT_MAX__ - 1)">, // INT_MIN
115    SimpleMacroDef<"FP_ILOGBNAN", "__INT_MAX__">,
116
117    IsFiniteMacro,
118    IsInfMacro,
119    IsNanMacro,
120  ];
121  let Types = ["double_t", "float_t"];
122}
123
124def FenvAPI: PublicAPI<"fenv.h"> {
125  let Macros = [
126    SimpleMacroDef<"FE_DIVBYZERO", "1">,
127    SimpleMacroDef<"FE_INEXACT", "2">,
128    SimpleMacroDef<"FE_INVALID", "4">,
129    SimpleMacroDef<"FE_OVERFLOW", "8">,
130    SimpleMacroDef<"FE_UNDERFLOW", "16">,
131    SimpleMacroDef<"FE_ALL_EXCEPT", "(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW)">,
132
133    SimpleMacroDef<"FE_DOWNWARD", "1">,
134    SimpleMacroDef<"FE_TONEAREST", "2">,
135    SimpleMacroDef<"FE_TOWARDZERO", "4">,
136    SimpleMacroDef<"FE_UPWARD", "8">,
137
138    SimpleMacroDef<"FE_DFL_ENV", "((fenv_t *)-1)">,
139  ];
140  let Types = ["fenv_t", "fexcept_t"];
141}
142
143def StringAPI : PublicAPI<"string.h"> {
144  let Types = ["size_t"];
145
146  let Macros = [
147    NullMacro,
148  ];
149}
150
151def StdIOAPI : PublicAPI<"stdio.h"> {
152  let Types = ["size_t", "FILE"];
153}
154
155def StdlibAPI : PublicAPI<"stdlib.h"> {
156  let Types = [
157    "div_t",
158    "ldiv_t",
159    "lldiv_t",
160    "size_t",
161    "__bsearchcompare_t",
162    "__qsortcompare_t"
163  ];
164}
165
166def TimeAPI : PublicAPI<"time.h"> {
167  let Types = ["time_t", "struct tm"];
168}
169
170def ErrnoAPI : PublicAPI<"errno.h"> {
171  let Macros = [
172    ErrnoMacro,
173    // We largely depend on linux/errno.h to give us the
174    // various error macro definitions. However, some libc
175    // implementations have chosen to provide definitions
176    // for some of the error macros to account for the ones
177    // missing in linux/errno.h. There is no harm in doing
178    // the same here if we define the macros only when they
179    // are not already defined.
180    MacroDefineIfNot<"ENOTSUP", "EOPNOTSUPP">,
181    MacroDefineIfNot<"ECANCELED", "125">,
182    MacroDefineIfNot<"EOWNERDEAD", "130">,
183    MacroDefineIfNot<"ENOTRECOVERABLE", "131">,
184    MacroDefineIfNot<"ERFKILL", "132">,
185    MacroDefineIfNot<"EHWPOISON", "133">,
186  ];
187}
188
189def SysMManAPI : PublicAPI<"sys/mman.h"> {
190  let Types = ["off_t", "size_t"];
191  let Macros = [
192    SimpleMacroDef<"PROT_NONE", "0">,
193    SimpleMacroDef<"PROT_READ", "1">,
194    SimpleMacroDef<"PROT_WRITE", "2">,
195    SimpleMacroDef<"PROT_EXEC", "4">,
196
197    SimpleMacroDef<"MAP_FIXED", "1">,
198    SimpleMacroDef<"MAP_PRIVATE", "2">,
199    SimpleMacroDef<"MAP_SHARED", "4">,
200
201    SimpleMacroDef<"MAP_FAILED", "((void*)-1)">,
202
203    // TODO: The value of 0x20 is good for x86_64, but has to be extended
204    // in some manner to accommodate other machine architectures.
205    SimpleMacroDef<"MAP_ANONYMOUS", "0x20">
206
207    // TODO: Add other MAP_* macros used by Linux.
208  ];
209
210}
211
212def SignalAPI : PublicAPI<"signal.h"> {
213  let Types = ["struct sigaction", "__sighandler_t"];
214}
215
216def ThreadsAPI : PublicAPI<"threads.h"> {
217  let Macros = [
218    SimpleMacroDef<"ONCE_FLAG_INIT", "0">,
219  ];
220
221  let Types = [
222    "__call_once_func_t",
223    "once_flag",
224    "cnd_t",
225    "mtx_t",
226    "thrd_t",
227    "thrd_start_t",
228  ];
229
230  let Enumerations = [
231    "mtx_plain",
232    "mtx_recursive",
233    "mtx_timed",
234    "thrd_timedout",
235    "thrd_success",
236    "thrd_busy",
237    "thrd_error",
238    "thrd_nomem",
239  ];
240}
241
242def UniStdAPI : PublicAPI<"unistd.h"> {
243  let Types = ["size_t", "ssize_t"];
244}
245
246def SysStatAPI : PublicAPI<"sys/stat.h"> {
247  let Types = ["mode_t"];
248}
249