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  let TypeDeclarations = [
235    FEnvT,
236    FExceptT,
237  ];
238}
239
240def StringAPI : PublicAPI<"string.h"> {
241  let TypeDeclarations = [
242    SizeT,
243  ];
244
245  let Macros = [
246    NullMacro,
247  ];
248}
249
250def StdIOAPI : PublicAPI<"stdio.h"> {
251  let TypeDeclarations = [
252    SizeT,
253    FILE,
254  ];
255}
256
257def DivT : TypeDecl<"div_t"> {
258  let Decl = [{
259    typedef struct {
260      int quot;
261      int rem;
262    } div_t;
263  }];
264}
265
266def LDivT : TypeDecl<"ldiv_t"> {
267  let Decl = [{
268    typedef struct {
269      long quot;
270      long rem;
271    } ldiv_t;
272  }];
273}
274
275def LLDivT : TypeDecl<"lldiv_t"> {
276  let Decl = [{
277    typedef struct {
278      long long quot;
279      long long rem;
280    } lldiv_t;
281  }];
282}
283
284def BSearchCompareTDefn : TypeDecl<"__bsearchcompare_t"> {
285  let Decl = [{
286    typedef int(*__bsearchcompare_t)(const void *, const void *);
287  }];
288}
289
290def StdlibAPI : PublicAPI<"stdlib.h"> {
291  let TypeDeclarations = [
292    DivT,
293    LDivT,
294    LLDivT,
295    SizeT,
296    BSearchCompareTDefn
297  ];
298}
299
300def TimeAPI : PublicAPI<"time.h"> {
301  let TypeDeclarations = [
302    StructTm,
303    TimeT,
304  ];
305
306  let Functions = [
307    "asctime",
308    "asctime_r",
309    "gmtime",
310    "gmtime_r",
311    "mktime",
312  ];
313}
314
315def ErrnoAPI : PublicAPI<"errno.h"> {
316  let Macros = [
317    ErrnoMacro,
318    // We largely depend on linux/errno.h to give us the
319    // various error macro definitions. However, some libc
320    // implementations have chosen to provide definitions
321    // for some of the error macros to account for the ones
322    // missing in linux/errno.h. There is no harm in doing
323    // the same here if we define the macros only when they
324    // are not already defined.
325    MacroDefineIfNot<"ENOTSUP", "EOPNOTSUPP">,
326    MacroDefineIfNot<"ECANCELED", "125">,
327    MacroDefineIfNot<"EOWNERDEAD", "130">,
328    MacroDefineIfNot<"ENOTRECOVERABLE", "131">,
329    MacroDefineIfNot<"ERFKILL", "132">,
330    MacroDefineIfNot<"EHWPOISON", "133">,
331  ];
332}
333
334def SysMManAPI : PublicAPI<"sys/mman.h"> {
335  let Macros = [
336    SimpleMacroDef<"PROT_NONE", "0">,
337    SimpleMacroDef<"PROT_READ", "1">,
338    SimpleMacroDef<"PROT_WRITE", "2">,
339    SimpleMacroDef<"PROT_EXEC", "4">,
340
341    SimpleMacroDef<"MAP_FIXED", "1">,
342    SimpleMacroDef<"MAP_PRIVATE", "2">,
343    SimpleMacroDef<"MAP_SHARED", "4">,
344
345    SimpleMacroDef<"MAP_FAILED", "((void*)-1)">,
346
347    // TODO: The value of 0x20 is good for x86_64, but has to be extended
348    // in some manner to accommodate other machine architectures.
349    SimpleMacroDef<"MAP_ANONYMOUS", "0x20">
350
351    // TODO: Add other MAP_* macros used by Linux.
352  ];
353
354  let TypeDeclarations = [
355    SizeT,
356    OffT,
357  ];
358}
359
360def StructSigactionDefn : TypeDecl<"struct sigaction"> {
361  let Decl = [{
362    struct __sigaction {
363      union {
364        void (*sa_handler)(int);
365        void (*sa_action)(int, siginfo_t *, void *);
366      };
367      sigset_t sa_mask;
368      int sa_flags;
369      void (*sa_restorer)(void);
370    };
371  }];
372}
373
374def SighandlerTDefn : TypeDecl<"__sighandler_t"> {
375  let Decl = [{
376    typedef void(*__sighandler_t)(int);
377  }];
378}
379
380def SignalAPI : PublicAPI<"signal.h"> {
381  let TypeDeclarations = [
382    StructSigactionDefn,
383    SighandlerTDefn,
384  ];
385}
386
387def OnceFlag : TypeDecl<"once_flag"> {
388  let Decl = [{
389    typedef unsigned int once_flag;
390  }];
391}
392
393def MtxT : TypeDecl<"mtx_t"> {
394  let Decl = [{
395    typedef struct {
396      unsigned char __internal_data[4];
397      int __mtx_type;
398    } mtx_t;
399  }];
400}
401
402def CndT : TypeDecl<"cnd_t"> {
403  let Decl = [{
404    typedef struct {
405      void *__qfront;
406      void *__qback;
407      struct {
408        unsigned char __w[4];
409        int __t;
410      } __qmtx;
411    } cnd_t;
412  }];
413}
414
415def ThreadStartT : TypeDecl<"thrd_start_t"> {
416  let Decl = "typedef int (*thrd_start_t)(void *);";
417}
418
419def CallOnceFuncT : TypeDecl<"__call_once_func_t"> {
420  let Decl = [{
421    typedef void(*__call_once_func_t)(void);
422  }];
423}
424
425def ThreadsAPI : PublicAPI<"threads.h"> {
426  let Macros = [
427    SimpleMacroDef<"ONCE_FLAG_INIT", "0">,
428  ];
429
430  let TypeDeclarations = [
431    OnceFlag,
432    CallOnceFuncT,
433    MtxT,
434    CndT,
435    ThreadStartT,
436  ];
437
438  let Enumerations = [
439    "mtx_plain",
440    "mtx_recursive",
441    "mtx_timed",
442    "thrd_timedout",
443    "thrd_success",
444    "thrd_busy",
445    "thrd_error",
446    "thrd_nomem",
447  ];
448}
449
450def UniStdAPI : PublicAPI<"unistd.h"> {
451  let TypeDeclarations = [
452    SSizeT,
453    SizeT,
454  ];
455}
456