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
9// TODO: Eliminate all TypeDecl specializations. Since we define all public
10// types in their own self contained header files, the header generator can
11// produce the boiler plate which pulls in the type definitions.
12
13def SizeT : TypeDecl<"size_t"> {
14  let Decl = [{
15    #include <llvm-libc-types/size_t.h>
16  }];
17}
18
19def SSizeT : TypeDecl<"ssize_t"> {
20  let Decl = [{
21    #include <llvm-libc-types/ssize_t.h>
22  }];
23}
24
25def StructTm: TypeDecl<"struct tm"> {
26  let Decl = [{
27    #include <llvm-libc-types/struct_tm.h>
28  }];
29}
30
31def TimeT: TypeDecl<"time_t"> {
32  let Decl = [{
33    #include <llvm-libc-types/time_t.h>
34  }];
35}
36
37def OffT : TypeDecl<"off_t"> {
38  let Decl = [{
39    #include <llvm-libc-types/off_t.h>
40  }];
41}
42
43def FILE : TypeDecl<"FILE"> {
44  let Decl = [{
45    #include <llvm-libc-types/FILE.h>
46  }];
47}
48
49def AssertMacro : MacroDef<"assert"> {
50  let Defn = [{
51    #undef assert
52
53    #ifdef NDEBUG
54    #define assert(e) (void)0
55    #else
56
57    #ifdef __cplusplus
58    extern "C"
59    #endif
60    _Noreturn void __assert_fail(const char *, const char *, unsigned, const char *);
61
62    #define assert(e)  \
63      ((e) ? (void)0 : __assert_fail(#e, __FILE__, __LINE__, __PRETTY_FUNCTION__))
64
65    #endif
66  }];
67}
68
69def StaticAssertMacro : MacroDef<"static_assert"> {
70  let Defn = [{
71    #ifndef __cplusplus
72    #undef static_assert
73    #define static_assert _Static_assert
74    #endif
75  }];
76}
77
78def NullMacro : MacroDef<"NULL"> {
79  let Defn = [{
80    #define __need_NULL
81    #include <stddef.h>
82  }];
83}
84
85def ErrnoMacro : MacroDef<"errno"> {
86  let Defn = [{
87    #ifdef __cplusplus
88    extern "C"
89    #endif
90    int *__errno_location();
91    #define errno (*__errno_location())
92  }];
93}
94
95def AssertAPI : PublicAPI<"assert.h"> {
96  let Macros = [
97    AssertMacro,
98    StaticAssertMacro,
99  ];
100}
101
102def CTypeAPI : PublicAPI<"ctype.h"> {
103}
104
105def IMaxDivT : TypeDecl<"imaxdiv_t"> {
106  let Decl = [{
107    #include <llvm-libc-types/imaxdiv_t.h>
108  }];
109}
110
111def IntTypesAPI : PublicAPI<"inttypes.h"> {
112  let TypeDeclarations = [
113    IMaxDivT,
114  ];
115}
116
117def MathErrHandlingMacro : MacroDef<"math_errhandling"> {
118  let Defn = [{
119    #ifndef math_errhandling
120    #ifdef __FAST_MATH__
121    #define math_errhandling 0
122    #elif defined __NO_MATH_ERRNO__
123    #define math_errhandling (MATH_ERREXCEPT)
124    #else
125    #define math_errhandling (MATH_ERRNO | MATH_ERREXCEPT)
126    #endif
127    #endif // math_errhandling not defined
128  }];
129}
130
131def IsFiniteMacro : MacroDef<"isfinite"> {
132  let Defn = [{
133    #define isfinite(x) __builtin_isfinite(x)
134  }];
135}
136
137def IsInfMacro : MacroDef<"isinf"> {
138  let Defn = [{
139    #define isinf(x) __builtin_isinf(x)
140  }];
141}
142
143def IsNanMacro : MacroDef<"isnan"> {
144  let Defn = [{
145    #define isnan(x) __builtin_isnan(x)
146  }];
147}
148
149def FloatT : TypeDecl<"float_t"> {
150  let Decl = [{
151    #include <llvm-libc-types/float_t.h>
152  }];
153}
154
155def DoubleT : TypeDecl<"double_t"> {
156  let Decl = [{
157    #include <llvm-libc-types/double_t.h>
158  }];
159}
160
161def MathAPI : PublicAPI<"math.h"> {
162  let Macros = [
163    SimpleMacroDef<"MATH_ERRNO", "1">,
164    SimpleMacroDef<"MATH_ERREXCEPT", "2">,
165    MathErrHandlingMacro,
166
167    SimpleMacroDef<"INFINITY", "__builtin_inff()">,
168    SimpleMacroDef<"NAN", "__builtin_nanf(\"\")">,
169
170    SimpleMacroDef<"FP_ILOGB0", "(-__INT_MAX__ - 1)">, // INT_MIN
171    SimpleMacroDef<"FP_ILOGBNAN", "__INT_MAX__">,
172
173    IsFiniteMacro,
174    IsInfMacro,
175    IsNanMacro,
176  ];
177  let TypeDeclarations = [
178    DoubleT,
179    FloatT,
180  ];
181}
182
183def FEnvT : TypeDecl<"fenv_t"> {
184  let Decl = [{
185    #include <llvm-libc-types/fenv_t.h>
186  }];
187}
188
189def FExceptT : TypeDecl<"fexcept_t"> {
190  let Decl = [{
191    #include <llvm-libc-types/fexcept_t.h>
192  }];
193}
194
195def FenvAPI: PublicAPI<"fenv.h"> {
196  let Macros = [
197    SimpleMacroDef<"FE_DIVBYZERO", "1">,
198    SimpleMacroDef<"FE_INEXACT", "2">,
199    SimpleMacroDef<"FE_INVALID", "4">,
200    SimpleMacroDef<"FE_OVERFLOW", "8">,
201    SimpleMacroDef<"FE_UNDERFLOW", "16">,
202    SimpleMacroDef<"FE_ALL_EXCEPT", "(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW)">,
203
204    SimpleMacroDef<"FE_DOWNWARD", "1">,
205    SimpleMacroDef<"FE_TONEAREST", "2">,
206    SimpleMacroDef<"FE_TOWARDZERO", "4">,
207    SimpleMacroDef<"FE_UPWARD", "8">,
208
209    SimpleMacroDef<"FE_DFL_ENV", "((fenv_t *)-1)">,
210  ];
211  let TypeDeclarations = [
212    FEnvT,
213    FExceptT,
214  ];
215}
216
217def StringAPI : PublicAPI<"string.h"> {
218  let TypeDeclarations = [
219    SizeT,
220  ];
221
222  let Macros = [
223    NullMacro,
224  ];
225}
226
227def StdIOAPI : PublicAPI<"stdio.h"> {
228  let TypeDeclarations = [
229    SizeT,
230    FILE,
231  ];
232}
233
234def DivT : TypeDecl<"div_t"> {
235  let Decl = [{
236    #include <llvm-libc-types/div_t.h>
237  }];
238}
239
240def LDivT : TypeDecl<"ldiv_t"> {
241  let Decl = [{
242    #include <llvm-libc-types/ldiv_t.h>
243  }];
244}
245
246def LLDivT : TypeDecl<"lldiv_t"> {
247  let Decl = [{
248    #include <llvm-libc-types/lldiv_t.h>
249  }];
250}
251
252def BSearchCompareTDefn : TypeDecl<"__bsearchcompare_t"> {
253  let Decl = [{
254    #include <llvm-libc-types/__bsearchcompare_t.h>
255  }];
256}
257
258def QSortCompareTDefn : TypeDecl<"__qsortcompare_t"> {
259  let Decl = [{
260    #include <llvm-libc-types/__qsortcompare_t.h>
261  }];
262}
263
264def StdlibAPI : PublicAPI<"stdlib.h"> {
265  let TypeDeclarations = [
266    DivT,
267    LDivT,
268    LLDivT,
269    SizeT,
270    BSearchCompareTDefn,
271    QSortCompareTDefn,
272  ];
273}
274
275def TimeAPI : PublicAPI<"time.h"> {
276  let TypeDeclarations = [
277    StructTm,
278    TimeT,
279  ];
280
281  let Functions = [
282    "asctime",
283    "asctime_r",
284    "gmtime",
285    "gmtime_r",
286    "mktime",
287  ];
288}
289
290def ErrnoAPI : PublicAPI<"errno.h"> {
291  let Macros = [
292    ErrnoMacro,
293    // We largely depend on linux/errno.h to give us the
294    // various error macro definitions. However, some libc
295    // implementations have chosen to provide definitions
296    // for some of the error macros to account for the ones
297    // missing in linux/errno.h. There is no harm in doing
298    // the same here if we define the macros only when they
299    // are not already defined.
300    MacroDefineIfNot<"ENOTSUP", "EOPNOTSUPP">,
301    MacroDefineIfNot<"ECANCELED", "125">,
302    MacroDefineIfNot<"EOWNERDEAD", "130">,
303    MacroDefineIfNot<"ENOTRECOVERABLE", "131">,
304    MacroDefineIfNot<"ERFKILL", "132">,
305    MacroDefineIfNot<"EHWPOISON", "133">,
306  ];
307}
308
309def SysMManAPI : PublicAPI<"sys/mman.h"> {
310  let Macros = [
311    SimpleMacroDef<"PROT_NONE", "0">,
312    SimpleMacroDef<"PROT_READ", "1">,
313    SimpleMacroDef<"PROT_WRITE", "2">,
314    SimpleMacroDef<"PROT_EXEC", "4">,
315
316    SimpleMacroDef<"MAP_FIXED", "1">,
317    SimpleMacroDef<"MAP_PRIVATE", "2">,
318    SimpleMacroDef<"MAP_SHARED", "4">,
319
320    SimpleMacroDef<"MAP_FAILED", "((void*)-1)">,
321
322    // TODO: The value of 0x20 is good for x86_64, but has to be extended
323    // in some manner to accommodate other machine architectures.
324    SimpleMacroDef<"MAP_ANONYMOUS", "0x20">
325
326    // TODO: Add other MAP_* macros used by Linux.
327  ];
328
329  let TypeDeclarations = [
330    SizeT,
331    OffT,
332  ];
333}
334
335def StructSigactionDefn : TypeDecl<"struct sigaction"> {
336  let Decl = [{
337    #include <llvm-libc-types/struct_sigaction.h>
338  }];
339}
340
341def SighandlerTDefn : TypeDecl<"__sighandler_t"> {
342  let Decl = [{
343    #include <llvm-libc-types/__sighandler_t.h>
344  }];
345}
346
347def SignalAPI : PublicAPI<"signal.h"> {
348  let TypeDeclarations = [
349    StructSigactionDefn,
350    SighandlerTDefn,
351  ];
352}
353
354def OnceFlag : TypeDecl<"once_flag"> {
355  let Decl = [{
356    #include <llvm-libc-types/once_flag.h>
357  }];
358}
359
360def MtxT : TypeDecl<"mtx_t"> {
361  let Decl = [{
362    #include <llvm-libc-types/mtx_t.h>
363  }];
364}
365
366def CndT : TypeDecl<"cnd_t"> {
367  let Decl = [{
368    #include <llvm-libc-types/cnd_t.h>
369  }];
370}
371
372def ThrdT : TypeDecl<"thrd_t"> {
373  let Decl = [{
374    #include <llvm-libc-types/thrd_t.h>
375  }];
376}
377
378def ThreadStartT : TypeDecl<"thrd_start_t"> {
379  let Decl = [{
380    #include <llvm-libc-types/thrd_start_t.h>
381  }];
382}
383
384def CallOnceFuncT : TypeDecl<"__call_once_func_t"> {
385  let Decl = [{
386    #include <llvm-libc-types/__call_once_func_t.h>
387  }];
388}
389
390def ThreadsAPI : PublicAPI<"threads.h"> {
391  let Macros = [
392    SimpleMacroDef<"ONCE_FLAG_INIT", "0">,
393  ];
394
395  let TypeDeclarations = [
396    OnceFlag,
397    CallOnceFuncT,
398    MtxT,
399    CndT,
400    ThrdT,
401    ThreadStartT,
402  ];
403
404  let Enumerations = [
405    "mtx_plain",
406    "mtx_recursive",
407    "mtx_timed",
408    "thrd_timedout",
409    "thrd_success",
410    "thrd_busy",
411    "thrd_error",
412    "thrd_nomem",
413  ];
414}
415
416def UniStdAPI : PublicAPI<"unistd.h"> {
417  let TypeDeclarations = [
418    SSizeT,
419    SizeT,
420  ];
421}
422