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    "gmtime_r",
254    "mktime",
255  ];
256}
257
258def ErrnoAPI : PublicAPI<"errno.h"> {
259  let Macros = [
260    ErrnoMacro,
261    // We largely depend on linux/errno.h to give us the
262    // various error macro definitions. However, some libc
263    // implementations have chosen to provide definitions
264    // for some of the error macros to account for the ones
265    // missing in linux/errno.h. There is no harm in doing
266    // the same here if we define the macros only when they
267    // are not already defined.
268    MacroDefineIfNot<"ENOTSUP", "EOPNOTSUPP">,
269    MacroDefineIfNot<"ECANCELED", "125">,
270    MacroDefineIfNot<"EOWNERDEAD", "130">,
271    MacroDefineIfNot<"ENOTRECOVERABLE", "131">,
272    MacroDefineIfNot<"ERFKILL", "132">,
273    MacroDefineIfNot<"EHWPOISON", "133">,
274  ];
275}
276
277def SysMManAPI : PublicAPI<"sys/mman.h"> {
278  let Macros = [
279    SimpleMacroDef<"PROT_NONE", "0">,
280    SimpleMacroDef<"PROT_READ", "1">,
281    SimpleMacroDef<"PROT_WRITE", "2">,
282    SimpleMacroDef<"PROT_EXEC", "4">,
283
284    SimpleMacroDef<"MAP_FIXED", "1">,
285    SimpleMacroDef<"MAP_PRIVATE", "2">,
286    SimpleMacroDef<"MAP_SHARED", "4">,
287
288    SimpleMacroDef<"MAP_FAILED", "((void*)-1)">,
289
290    // TODO: The value of 0x20 is good for x86_64, but has to be extended
291    // in some manner to accommodate other machine architectures.
292    SimpleMacroDef<"MAP_ANONYMOUS", "0x20">
293
294    // TODO: Add other MAP_* macros used by Linux.
295  ];
296
297  let TypeDeclarations = [
298    SizeT,
299    OffT,
300  ];
301}
302
303def StructSigactionDefn : TypeDecl<"struct sigaction"> {
304  let Decl = [{
305    struct __sigaction {
306      union {
307        void (*sa_handler)(int);
308        void (*sa_action)(int, siginfo_t *, void *);
309      };
310      sigset_t sa_mask;
311      int sa_flags;
312      void (*sa_restorer)(void);
313    };
314  }];
315}
316
317def SighandlerTDefn : TypeDecl<"__sighandler_t"> {
318  let Decl = [{
319    typedef void(*__sighandler_t)(int);
320  }];
321}
322
323def SignalAPI : PublicAPI<"signal.h"> {
324  let TypeDeclarations = [
325    StructSigactionDefn,
326    SighandlerTDefn,
327  ];
328}
329
330def OnceFlag : TypeDecl<"once_flag"> {
331  let Decl = [{
332    typedef unsigned int once_flag;
333  }];
334}
335
336def MtxT : TypeDecl<"mtx_t"> {
337  let Decl = [{
338    typedef struct {
339      unsigned char __internal_data[4];
340      int __mtx_type;
341    } mtx_t;
342  }];
343}
344
345def ThreadStartT : TypeDecl<"thrd_start_t"> {
346  let Decl = "typedef int (*thrd_start_t)(void *);";
347}
348
349def CallOnceFuncT : TypeDecl<"__call_once_func_t"> {
350  let Decl = [{
351    typedef void(*__call_once_func_t)(void);
352  }];
353}
354
355def ThreadsAPI : PublicAPI<"threads.h"> {
356  let Macros = [
357    SimpleMacroDef<"ONCE_FLAG_INIT", "0">,
358  ];
359
360  let TypeDeclarations = [
361    OnceFlag,
362    CallOnceFuncT,
363    MtxT,
364    ThreadStartT,
365  ];
366
367  let Enumerations = [
368    "mtx_plain",
369    "mtx_recursive",
370    "mtx_timed",
371    "thrd_timedout",
372    "thrd_success",
373    "thrd_busy",
374    "thrd_error",
375    "thrd_nomem",
376  ];
377}
378
379def UniStdAPI : PublicAPI<"unistd.h"> {
380  let TypeDeclarations = [
381    SSizeT,
382    SizeT,
383  ];
384}
385