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