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 IMaxDivT : TypeDecl<"imaxdiv_t"> {
115  let Decl = [{
116    typedef struct {
117      intmax_t quot;
118      intmax_t rem;
119    } imaxdiv_t;
120  }];
121}
122
123def IntTypesAPI : PublicAPI<"inttypes.h"> {
124  let TypeDeclarations = [
125    IMaxDivT,
126  ];
127}
128
129def MathErrHandlingMacro : MacroDef<"math_errhandling"> {
130  let Defn = [{
131    #ifndef math_errhandling
132    #ifdef __FAST_MATH__
133    #define math_errhandling 0
134    #elif defined __NO_MATH_ERRNO__
135    #define math_errhandling (MATH_ERREXCEPT)
136    #else
137    #define math_errhandling (MATH_ERRNO | MATH_ERREXCEPT)
138    #endif
139    #endif // math_errhandling not defined
140  }];
141}
142
143def IsFiniteMacro : MacroDef<"isfinite"> {
144  let Defn = [{
145    #define isfinite(x) __builtin_isfinite(x)
146  }];
147}
148
149def IsInfMacro : MacroDef<"isinf"> {
150  let Defn = [{
151    #define isinf(x) __builtin_isinf(x)
152  }];
153}
154
155def IsNanMacro : MacroDef<"isnan"> {
156  let Defn = [{
157    #define isnan(x) __builtin_isnan(x)
158  }];
159}
160
161def FloatT : TypeDecl<"float_t"> {
162  let Decl = [{
163    #define __need_float_t
164    #include <__llvm-libc-stdc-types.h>
165  }];
166}
167
168def DoubleT : TypeDecl<"double_t"> {
169  let Decl = [{
170    #define __need_double_t
171    #include <__llvm-libc-stdc-types.h>
172  }];
173}
174
175def MathAPI : PublicAPI<"math.h"> {
176  let Macros = [
177    SimpleMacroDef<"MATH_ERRNO", "1">,
178    SimpleMacroDef<"MATH_ERREXCEPT", "2">,
179    MathErrHandlingMacro,
180
181    SimpleMacroDef<"INFINITY", "__builtin_inff()">,
182    SimpleMacroDef<"NAN", "__builtin_nanf(\"\")">,
183
184    SimpleMacroDef<"FP_ILOGB0", "(-__INT_MAX__ - 1)">, // INT_MIN
185    SimpleMacroDef<"FP_ILOGBNAN", "__INT_MAX__">,
186
187    IsFiniteMacro,
188    IsInfMacro,
189    IsNanMacro,
190  ];
191  let TypeDeclarations = [
192    DoubleT,
193    FloatT,
194  ];
195}
196
197def FEnvT : TypeDecl<"fenv_t"> {
198  let Decl = [{
199    #ifdef __aarch64__
200    typedef struct {
201      unsigned char __control_word[4];
202      unsigned char __status_word[4];
203    } fenv_t;
204    #endif
205    #ifdef __x86_64__
206    typedef struct {
207      unsigned char __x86_status[28];
208      unsigned char __mxcsr[4];
209    } fenv_t;
210    #endif
211  }];
212}
213
214def FExceptT : TypeDecl<"fexcept_t"> {
215  let Decl = [{
216    typedef int fexcept_t;
217  }];
218}
219
220def FenvAPI: PublicAPI<"fenv.h"> {
221  let Macros = [
222    SimpleMacroDef<"FE_DIVBYZERO", "1">,
223    SimpleMacroDef<"FE_INEXACT", "2">,
224    SimpleMacroDef<"FE_INVALID", "4">,
225    SimpleMacroDef<"FE_OVERFLOW", "8">,
226    SimpleMacroDef<"FE_UNDERFLOW", "16">,
227    SimpleMacroDef<"FE_ALL_EXCEPT", "(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW)">,
228
229    SimpleMacroDef<"FE_DOWNWARD", "1">,
230    SimpleMacroDef<"FE_TONEAREST", "2">,
231    SimpleMacroDef<"FE_TOWARDZERO", "4">,
232    SimpleMacroDef<"FE_UPWARD", "8">,
233
234    SimpleMacroDef<"FE_DFL_ENV", "((fenv_t *)-1)">,
235  ];
236  let TypeDeclarations = [
237    FEnvT,
238    FExceptT,
239  ];
240}
241
242def StringAPI : PublicAPI<"string.h"> {
243  let TypeDeclarations = [
244    SizeT,
245  ];
246
247  let Macros = [
248    NullMacro,
249  ];
250}
251
252def StdIOAPI : PublicAPI<"stdio.h"> {
253  let TypeDeclarations = [
254    SizeT,
255    FILE,
256  ];
257}
258
259def DivT : TypeDecl<"div_t"> {
260  let Decl = [{
261    typedef struct {
262      int quot;
263      int rem;
264    } div_t;
265  }];
266}
267
268def LDivT : TypeDecl<"ldiv_t"> {
269  let Decl = [{
270    typedef struct {
271      long quot;
272      long rem;
273    } ldiv_t;
274  }];
275}
276
277def LLDivT : TypeDecl<"lldiv_t"> {
278  let Decl = [{
279    typedef struct {
280      long long quot;
281      long long rem;
282    } lldiv_t;
283  }];
284}
285
286def BSearchCompareTDefn : TypeDecl<"__bsearchcompare_t"> {
287  let Decl = [{
288    typedef int(*__bsearchcompare_t)(const void *, const void *);
289  }];
290}
291
292def QSortCompareTDefn : TypeDecl<"__qsortcompare_t"> {
293  let Decl = [{
294    typedef int(*__qsortcompare_t)(const void *, const void *);
295  }];
296}
297
298def StdlibAPI : PublicAPI<"stdlib.h"> {
299  let TypeDeclarations = [
300    DivT,
301    LDivT,
302    LLDivT,
303    SizeT,
304    BSearchCompareTDefn,
305    QSortCompareTDefn,
306  ];
307}
308
309def TimeAPI : PublicAPI<"time.h"> {
310  let TypeDeclarations = [
311    StructTm,
312    TimeT,
313  ];
314
315  let Functions = [
316    "asctime",
317    "asctime_r",
318    "gmtime",
319    "gmtime_r",
320    "mktime",
321  ];
322}
323
324def ErrnoAPI : PublicAPI<"errno.h"> {
325  let Macros = [
326    ErrnoMacro,
327    // We largely depend on linux/errno.h to give us the
328    // various error macro definitions. However, some libc
329    // implementations have chosen to provide definitions
330    // for some of the error macros to account for the ones
331    // missing in linux/errno.h. There is no harm in doing
332    // the same here if we define the macros only when they
333    // are not already defined.
334    MacroDefineIfNot<"ENOTSUP", "EOPNOTSUPP">,
335    MacroDefineIfNot<"ECANCELED", "125">,
336    MacroDefineIfNot<"EOWNERDEAD", "130">,
337    MacroDefineIfNot<"ENOTRECOVERABLE", "131">,
338    MacroDefineIfNot<"ERFKILL", "132">,
339    MacroDefineIfNot<"EHWPOISON", "133">,
340  ];
341}
342
343def SysMManAPI : PublicAPI<"sys/mman.h"> {
344  let Macros = [
345    SimpleMacroDef<"PROT_NONE", "0">,
346    SimpleMacroDef<"PROT_READ", "1">,
347    SimpleMacroDef<"PROT_WRITE", "2">,
348    SimpleMacroDef<"PROT_EXEC", "4">,
349
350    SimpleMacroDef<"MAP_FIXED", "1">,
351    SimpleMacroDef<"MAP_PRIVATE", "2">,
352    SimpleMacroDef<"MAP_SHARED", "4">,
353
354    SimpleMacroDef<"MAP_FAILED", "((void*)-1)">,
355
356    // TODO: The value of 0x20 is good for x86_64, but has to be extended
357    // in some manner to accommodate other machine architectures.
358    SimpleMacroDef<"MAP_ANONYMOUS", "0x20">
359
360    // TODO: Add other MAP_* macros used by Linux.
361  ];
362
363  let TypeDeclarations = [
364    SizeT,
365    OffT,
366  ];
367}
368
369def StructSigactionDefn : TypeDecl<"struct sigaction"> {
370  let Decl = [{
371    struct __sigaction {
372      union {
373        void (*sa_handler)(int);
374        void (*sa_action)(int, siginfo_t *, void *);
375      };
376      sigset_t sa_mask;
377      int sa_flags;
378      void (*sa_restorer)(void);
379    };
380  }];
381}
382
383def SighandlerTDefn : TypeDecl<"__sighandler_t"> {
384  let Decl = [{
385    typedef void(*__sighandler_t)(int);
386  }];
387}
388
389def SignalAPI : PublicAPI<"signal.h"> {
390  let TypeDeclarations = [
391    StructSigactionDefn,
392    SighandlerTDefn,
393  ];
394}
395
396def OnceFlag : TypeDecl<"once_flag"> {
397  let Decl = [{
398    typedef unsigned int once_flag;
399  }];
400}
401
402def MtxT : TypeDecl<"mtx_t"> {
403  let Decl = [{
404    typedef struct {
405      unsigned char __internal_data[4];
406      int __mtx_type;
407    } mtx_t;
408  }];
409}
410
411def CndT : TypeDecl<"cnd_t"> {
412  let Decl = [{
413    typedef struct {
414      void *__qfront;
415      void *__qback;
416      struct {
417        unsigned char __w[4];
418        int __t;
419      } __qmtx;
420    } cnd_t;
421  }];
422}
423
424def ThreadStartT : TypeDecl<"thrd_start_t"> {
425  let Decl = "typedef int (*thrd_start_t)(void *);";
426}
427
428def CallOnceFuncT : TypeDecl<"__call_once_func_t"> {
429  let Decl = [{
430    typedef void(*__call_once_func_t)(void);
431  }];
432}
433
434def ThreadsAPI : PublicAPI<"threads.h"> {
435  let Macros = [
436    SimpleMacroDef<"ONCE_FLAG_INIT", "0">,
437  ];
438
439  let TypeDeclarations = [
440    OnceFlag,
441    CallOnceFuncT,
442    MtxT,
443    CndT,
444    ThreadStartT,
445  ];
446
447  let Enumerations = [
448    "mtx_plain",
449    "mtx_recursive",
450    "mtx_timed",
451    "thrd_timedout",
452    "thrd_success",
453    "thrd_busy",
454    "thrd_error",
455    "thrd_nomem",
456  ];
457}
458
459def UniStdAPI : PublicAPI<"unistd.h"> {
460  let TypeDeclarations = [
461    SSizeT,
462    SizeT,
463  ];
464}
465