• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FenvAPI: PublicAPI<"fenv.h"> {
183  let Macros = [
184    SimpleMacroDef<"FE_DIVBYZERO", "1">,
185    SimpleMacroDef<"FE_INEXACT", "2">,
186    SimpleMacroDef<"FE_INVALID", "4">,
187    SimpleMacroDef<"FE_OVERFLOW", "8">,
188    SimpleMacroDef<"FE_UNDERFLOW", "16">,
189    SimpleMacroDef<"FE_ALL_EXCEPT", "(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW)">,
190
191    SimpleMacroDef<"FE_DOWNWARD", "1">,
192    SimpleMacroDef<"FE_TONEAREST", "2">,
193    SimpleMacroDef<"FE_TOWARDZERO", "4">,
194    SimpleMacroDef<"FE_UPWARD", "8">,
195  ];
196}
197
198def StringAPI : PublicAPI<"string.h"> {
199  let TypeDeclarations = [
200    SizeT,
201  ];
202
203  let Macros = [
204    NullMacro,
205  ];
206}
207
208def StdIOAPI : PublicAPI<"stdio.h"> {
209  let TypeDeclarations = [
210    SizeT,
211    FILE,
212  ];
213}
214
215def StdlibAPI : PublicAPI<"stdlib.h"> {
216}
217
218def TimeAPI : PublicAPI<"time.h"> {
219  let TypeDeclarations = [
220    StructTm,
221    TimeT,
222  ];
223
224  let Functions = [
225    "mktime",
226  ];
227}
228
229def ErrnoAPI : PublicAPI<"errno.h"> {
230  let Macros = [
231    ErrnoMacro,
232    // We largely depend on linux/errno.h to give us the
233    // various error macro definitions. However, some libc
234    // implementations have chosen to provide definitions
235    // for some of the error macros to account for the ones
236    // missing in linux/errno.h. There is no harm in doing
237    // the same here if we define the macros only when they
238    // are not already defined.
239    MacroDefineIfNot<"ENOTSUP", "EOPNOTSUPP">,
240    MacroDefineIfNot<"ECANCELED", "125">,
241    MacroDefineIfNot<"EOWNERDEAD", "130">,
242    MacroDefineIfNot<"ENOTRECOVERABLE", "131">,
243    MacroDefineIfNot<"ERFKILL", "132">,
244    MacroDefineIfNot<"EHWPOISON", "133">,
245  ];
246}
247
248def SysMManAPI : PublicAPI<"sys/mman.h"> {
249  let Macros = [
250    SimpleMacroDef<"PROT_NONE", "0">,
251    SimpleMacroDef<"PROT_READ", "1">,
252    SimpleMacroDef<"PROT_WRITE", "2">,
253    SimpleMacroDef<"PROT_EXEC", "4">,
254
255    SimpleMacroDef<"MAP_FIXED", "1">,
256    SimpleMacroDef<"MAP_PRIVATE", "2">,
257    SimpleMacroDef<"MAP_SHARED", "4">,
258
259    SimpleMacroDef<"MAP_FAILED", "((void*)-1)">,
260
261    // TODO: The value of 0x20 is good for x86_64, but has to be extended
262    // in some manner to accommodate other machine architectures.
263    SimpleMacroDef<"MAP_ANONYMOUS", "0x20">
264
265    // TODO: Add other MAP_* macros used by Linux.
266  ];
267
268  let TypeDeclarations = [
269    SizeT,
270    OffT,
271  ];
272}
273
274def StructSigactionDefn : TypeDecl<"struct sigaction"> {
275  let Decl = [{
276    struct __sigaction {
277      union {
278        void (*sa_handler)(int);
279        void (*sa_action)(int, siginfo_t *, void *);
280      };
281      sigset_t sa_mask;
282      int sa_flags;
283      void (*sa_restorer)(void);
284    };
285  }];
286}
287
288def SighandlerTDefn : TypeDecl<"__sighandler_t"> {
289  let Decl = [{
290    typedef void(*__sighandler_t)(int);
291  }];
292}
293
294def SignalAPI : PublicAPI<"signal.h"> {
295  let TypeDeclarations = [
296    StructSigactionDefn,
297    SighandlerTDefn,
298  ];
299}
300
301def OnceFlag : TypeDecl<"once_flag"> {
302  let Decl = [{
303    typedef unsigned int once_flag;
304  }];
305}
306
307def MtxT : TypeDecl<"mtx_t"> {
308  let Decl = [{
309    typedef struct {
310      unsigned char __internal_data[4];
311      int __mtx_type;
312    } mtx_t;
313  }];
314}
315
316def ThreadStartT : TypeDecl<"thrd_start_t"> {
317  let Decl = "typedef int (*thrd_start_t)(void *);";
318}
319
320def CallOnceFuncT : TypeDecl<"__call_once_func_t"> {
321  let Decl = [{
322    typedef void(*__call_once_func_t)(void);
323  }];
324}
325
326def ThreadsAPI : PublicAPI<"threads.h"> {
327  let Macros = [
328    SimpleMacroDef<"ONCE_FLAG_INIT", "0">,
329  ];
330
331  let TypeDeclarations = [
332    OnceFlag,
333    CallOnceFuncT,
334    MtxT,
335    ThreadStartT,
336  ];
337
338  let Enumerations = [
339    "mtx_plain",
340    "mtx_recursive",
341    "mtx_timed",
342    "thrd_timedout",
343    "thrd_success",
344    "thrd_busy",
345    "thrd_error",
346    "thrd_nomem",
347  ];
348}
349
350def UniStdAPI : PublicAPI<"unistd.h"> {
351  let TypeDeclarations = [
352    SSizeT,
353    SizeT,
354  ];
355}
356