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