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 SizeT : TypeDecl<"size_t"> {
10  let Decl = [{
11    #define __need_size_t
12    #include <stddef.h>
13  }];
14}
15
16def SSizeT : TypeDecl<"ssize_t"> {
17  let Decl = [{
18    #define __need_ssize_t
19    #include <__posix-types.h>
20  }];
21}
22
23def StructTm: TypeDecl<"struct tm"> {
24  let Decl = [{
25    struct tm {
26      int tm_sec; // seconds after the minute
27      int tm_min; // minutes after the hour
28      int tm_hour; // hours since midnight
29      int tm_mday; // day of the month
30      int tm_mon; // months since January
31      int tm_year; // years since 1900
32      int tm_wday; // days since Sunday
33      int tm_yday; // days since January
34      int tm_isdst; // Daylight Saving Time flag
35    };
36  }];
37}
38
39def TimeT: TypeDecl<"time_t"> {
40   let Decl = [{
41   typedef long time_t;
42  }];
43}
44
45def OffT : TypeDecl<"off_t"> {
46  let Decl = [{
47    #define __need_off_t
48    #include <__posix-types.h>
49  }];
50}
51
52def FILE : TypeDecl<"FILE"> {
53  let Decl = [{
54    typedef struct FILE FILE;
55  }];
56}
57
58def AssertMacro : MacroDef<"assert"> {
59  let Defn = [{
60    #undef assert
61
62    #ifdef NDEBUG
63    #define assert(e) (void)0
64    #else
65
66    #ifdef __cplusplus
67    extern "C"
68    #endif
69    _Noreturn void __assert_fail(const char *, const char *, unsigned, const char *);
70
71    #define assert(e)  \
72      ((e) ? (void)0 : __assert_fail(#e, __FILE__, __LINE__, __PRETTY_FUNCTION__))
73
74    #endif
75  }];
76}
77
78def StaticAssertMacro : MacroDef<"static_assert"> {
79  let Defn = [{
80    #ifndef __cplusplus
81    #undef static_assert
82    #define static_assert _Static_assert
83    #endif
84  }];
85}
86
87def NullMacro : MacroDef<"NULL"> {
88  let Defn = [{
89    #define __need_NULL
90    #include <stddef.h>
91  }];
92}
93
94def ErrnoMacro : MacroDef<"errno"> {
95  let Defn = [{
96    #ifdef __cplusplus
97    extern "C"
98    #endif
99    int *__errno_location();
100    #define errno (*__errno_location())
101  }];
102}
103
104def AssertAPI : PublicAPI<"assert.h"> {
105  let Macros = [
106    AssertMacro,
107    StaticAssertMacro,
108  ];
109}
110
111def CTypeAPI : PublicAPI<"ctype.h"> {
112}
113
114def MathErrHandlingMacro : MacroDef<"math_errhandling"> {
115  let Defn = [{
116    #ifndef math_errhandling
117    #ifdef __FAST_MATH__
118    #define math_errhandling 0
119    #elif defined __NO_MATH_ERRNO__
120    #define math_errhandling (MATH_ERREXCEPT)
121    #else
122    #define math_errhandling (MATH_ERRNO | MATH_ERREXCEPT)
123    #endif
124    #endif // math_errhandling not defined
125  }];
126}
127
128def IsFiniteMacro : MacroDef<"isfinite"> {
129  let Defn = [{
130    #define isfinite(x) __builtin_isfinite(x)
131  }];
132}
133
134def IsInfMacro : MacroDef<"isinf"> {
135  let Defn = [{
136    #define isinf(x) __builtin_isinf(x)
137  }];
138}
139
140def IsNanMacro : MacroDef<"isnan"> {
141  let Defn = [{
142    #define isnan(x) __builtin_isnan(x)
143  }];
144}
145
146def FloatT : TypeDecl<"float_t"> {
147  let Decl = [{
148    #define __need_float_t
149    #include <__llvm-libc-stdc-types.h>
150  }];
151}
152
153def DoubleT : TypeDecl<"double_t"> {
154  let Decl = [{
155    #define __need_double_t
156    #include <__llvm-libc-stdc-types.h>
157  }];
158}
159
160def MathAPI : PublicAPI<"math.h"> {
161  let Macros = [
162    SimpleMacroDef<"MATH_ERRNO", "1">,
163    SimpleMacroDef<"MATH_ERREXCEPT", "2">,
164    MathErrHandlingMacro,
165
166    SimpleMacroDef<"INFINITY", "__builtin_inff()">,
167    SimpleMacroDef<"NAN", "__builtin_nanf(\"\")">,
168
169    SimpleMacroDef<"FP_ILOGB0", "(-__INT_MAX__ - 1)">, // INT_MIN
170    SimpleMacroDef<"FP_ILOGBNAN", "__INT_MAX__">,
171
172    IsFiniteMacro,
173    IsInfMacro,
174    IsNanMacro,
175  ];
176  let TypeDeclarations = [
177    DoubleT,
178    FloatT,
179  ];
180}
181
182def FEnvT : TypeDecl<"fenv_t"> {
183  let Decl = [{
184    #ifdef __aarch64__
185    typedef struct {
186      unsigned char __control_word[4];
187      unsigned char __status_word[4];
188    } fenv_t;
189    #endif
190    #ifdef __x86_64__
191    typedef struct {
192      unsigned char __x86_status[28];
193      unsigned char __mxcsr[4];
194    } fenv_t;
195    #endif
196  }];
197}
198
199def FExceptT : TypeDecl<"fexcept_t"> {
200  let Decl = [{
201    typedef int fexcept_t;
202  }];
203}
204
205def FenvAPI: PublicAPI<"fenv.h"> {
206  let Macros = [
207    SimpleMacroDef<"FE_DIVBYZERO", "1">,
208    SimpleMacroDef<"FE_INEXACT", "2">,
209    SimpleMacroDef<"FE_INVALID", "4">,
210    SimpleMacroDef<"FE_OVERFLOW", "8">,
211    SimpleMacroDef<"FE_UNDERFLOW", "16">,
212    SimpleMacroDef<"FE_ALL_EXCEPT", "(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW)">,
213
214    SimpleMacroDef<"FE_DOWNWARD", "1">,
215    SimpleMacroDef<"FE_TONEAREST", "2">,
216    SimpleMacroDef<"FE_TOWARDZERO", "4">,
217    SimpleMacroDef<"FE_UPWARD", "8">,
218  ];
219  let TypeDeclarations = [
220    FEnvT,
221    FExceptT,
222  ];
223}
224
225def StringAPI : PublicAPI<"string.h"> {
226  let TypeDeclarations = [
227    SizeT,
228  ];
229
230  let Macros = [
231    NullMacro,
232  ];
233}
234
235def StdIOAPI : PublicAPI<"stdio.h"> {
236  let TypeDeclarations = [
237    SizeT,
238    FILE,
239  ];
240}
241
242def StdlibAPI : PublicAPI<"stdlib.h"> {
243}
244
245def TimeAPI : PublicAPI<"time.h"> {
246  let TypeDeclarations = [
247    StructTm,
248    TimeT,
249  ];
250
251  let Functions = [
252    "gmtime",
253    "mktime",
254  ];
255}
256
257def ErrnoAPI : PublicAPI<"errno.h"> {
258  let Macros = [
259    ErrnoMacro,
260    // We largely depend on linux/errno.h to give us the
261    // various error macro definitions. However, some libc
262    // implementations have chosen to provide definitions
263    // for some of the error macros to account for the ones
264    // missing in linux/errno.h. There is no harm in doing
265    // the same here if we define the macros only when they
266    // are not already defined.
267    MacroDefineIfNot<"ENOTSUP", "EOPNOTSUPP">,
268    MacroDefineIfNot<"ECANCELED", "125">,
269    MacroDefineIfNot<"EOWNERDEAD", "130">,
270    MacroDefineIfNot<"ENOTRECOVERABLE", "131">,
271    MacroDefineIfNot<"ERFKILL", "132">,
272    MacroDefineIfNot<"EHWPOISON", "133">,
273  ];
274}
275
276def SysMManAPI : PublicAPI<"sys/mman.h"> {
277  let Macros = [
278    SimpleMacroDef<"PROT_NONE", "0">,
279    SimpleMacroDef<"PROT_READ", "1">,
280    SimpleMacroDef<"PROT_WRITE", "2">,
281    SimpleMacroDef<"PROT_EXEC", "4">,
282
283    SimpleMacroDef<"MAP_FIXED", "1">,
284    SimpleMacroDef<"MAP_PRIVATE", "2">,
285    SimpleMacroDef<"MAP_SHARED", "4">,
286
287    SimpleMacroDef<"MAP_FAILED", "((void*)-1)">,
288
289    // TODO: The value of 0x20 is good for x86_64, but has to be extended
290    // in some manner to accommodate other machine architectures.
291    SimpleMacroDef<"MAP_ANONYMOUS", "0x20">
292
293    // TODO: Add other MAP_* macros used by Linux.
294  ];
295
296  let TypeDeclarations = [
297    SizeT,
298    OffT,
299  ];
300}
301
302def StructSigactionDefn : TypeDecl<"struct sigaction"> {
303  let Decl = [{
304    struct __sigaction {
305      union {
306        void (*sa_handler)(int);
307        void (*sa_action)(int, siginfo_t *, void *);
308      };
309      sigset_t sa_mask;
310      int sa_flags;
311      void (*sa_restorer)(void);
312    };
313  }];
314}
315
316def SighandlerTDefn : TypeDecl<"__sighandler_t"> {
317  let Decl = [{
318    typedef void(*__sighandler_t)(int);
319  }];
320}
321
322def SignalAPI : PublicAPI<"signal.h"> {
323  let TypeDeclarations = [
324    StructSigactionDefn,
325    SighandlerTDefn,
326  ];
327}
328
329def OnceFlag : TypeDecl<"once_flag"> {
330  let Decl = [{
331    typedef unsigned int once_flag;
332  }];
333}
334
335def MtxT : TypeDecl<"mtx_t"> {
336  let Decl = [{
337    typedef struct {
338      unsigned char __internal_data[4];
339      int __mtx_type;
340    } mtx_t;
341  }];
342}
343
344def ThreadStartT : TypeDecl<"thrd_start_t"> {
345  let Decl = "typedef int (*thrd_start_t)(void *);";
346}
347
348def CallOnceFuncT : TypeDecl<"__call_once_func_t"> {
349  let Decl = [{
350    typedef void(*__call_once_func_t)(void);
351  }];
352}
353
354def ThreadsAPI : PublicAPI<"threads.h"> {
355  let Macros = [
356    SimpleMacroDef<"ONCE_FLAG_INIT", "0">,
357  ];
358
359  let TypeDeclarations = [
360    OnceFlag,
361    CallOnceFuncT,
362    MtxT,
363    ThreadStartT,
364  ];
365
366  let Enumerations = [
367    "mtx_plain",
368    "mtx_recursive",
369    "mtx_timed",
370    "thrd_timedout",
371    "thrd_success",
372    "thrd_busy",
373    "thrd_error",
374    "thrd_nomem",
375  ];
376}
377
378def UniStdAPI : PublicAPI<"unistd.h"> {
379  let TypeDeclarations = [
380    SSizeT,
381    SizeT,
382  ];
383}
384