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 OffT : TypeDecl<"off_t"> {
24  let Decl = [{
25    #define __need_off_t
26    #include <__posix-types.h>
27  }];
28}
29
30def FILE : TypeDecl<"FILE"> {
31  let Decl = [{
32    typedef struct FILE FILE;
33  }];
34}
35
36def AssertMacro : MacroDef<"assert"> {
37  let Defn = [{
38    #undef assert
39
40    #ifdef NDEBUG
41    #define assert(e) (void)0
42    #else
43
44    #ifdef __cplusplus
45    extern "C"
46    #endif
47    _Noreturn void __assert_fail(const char *, const char *, unsigned, const char *);
48
49    #define assert(e)  \
50      ((e) ? (void)0 : __assert_fail(#e, __FILE__, __LINE__, __PRETTY_FUNCTION__))
51
52    #endif
53  }];
54}
55
56def StaticAssertMacro : MacroDef<"static_assert"> {
57  let Defn = [{
58    #ifndef __cplusplus
59    #undef static_assert
60    #define static_assert _Static_assert
61    #endif
62  }];
63}
64
65def NullMacro : MacroDef<"NULL"> {
66  let Defn = [{
67    #define __need_NULL
68    #include <stddef.h>
69  }];
70}
71
72def ErrnoMacro : MacroDef<"errno"> {
73  let Defn = [{
74    #ifdef __cplusplus
75    extern "C"
76    #endif
77    int *__errno_location();
78    #define errno (*__errno_location())
79  }];
80}
81
82def AssertAPI : PublicAPI<"assert.h"> {
83  let Macros = [
84    AssertMacro,
85    StaticAssertMacro,
86  ];
87}
88
89def MathErrHandlingMacro : MacroDef<"math_errhandling"> {
90  let Defn = [{
91    #ifndef math_errhandling
92    #ifdef __FAST_MATH__
93    #define math_errhandling 0
94    #elif defined __NO_MATH_ERRNO__
95    #define math_errhandling (MATH_ERREXCEPT)
96    #else
97    #define math_errhandling (MATH_ERRNO | MATH_ERREXCEPT)
98    #endif
99    #endif // math_errhandling not defined
100  }];
101}
102
103def IsFiniteMacro : MacroDef<"isfinite"> {
104  let Defn = [{
105    #define isfinite(x) __builtin_isfinite(x)
106  }];
107}
108
109def IsInfMacro : MacroDef<"isinf"> {
110  let Defn = [{
111    #define isinf(x) __builtin_isinf(x)
112  }];
113}
114
115def IsNanMacro : MacroDef<"isnan"> {
116  let Defn = [{
117    #define isnan(x) __builtin_isnan(x)
118  }];
119}
120
121def FloatT : TypeDecl<"float_t"> {
122  let Decl = [{
123    #define __need_float_t
124    #include <__llvm-libc-stdc-types.h>
125  }];
126}
127
128def DoubleT : TypeDecl<"double_t"> {
129  let Decl = [{
130    #define __need_double_t
131    #include <__llvm-libc-stdc-types.h>
132  }];
133}
134
135def MathAPI : PublicAPI<"math.h"> {
136  let Macros = [
137    SimpleMacroDef<"MATH_ERRNO", "1">,
138    SimpleMacroDef<"MATH_ERREXCEPT", "2">,
139    MathErrHandlingMacro,
140
141    SimpleMacroDef<"INFINITY", "__builtin_inff()">,
142    SimpleMacroDef<"NAN", "__builtin_nanf(\"\")">,
143
144    IsFiniteMacro,
145    IsInfMacro,
146    IsNanMacro,
147  ];
148  let TypeDeclarations = [
149    DoubleT,
150    FloatT,
151  ];
152  let Functions = [
153   "copysign",
154   "copysignf",
155   "ceil",
156   "ceilf",
157   "cosf",
158   "fabs",
159   "fabsf",
160   "floor",
161   "floorf",
162   "frexp",
163   "frexpf",
164   "logb",
165   "logbf",
166   "modf",
167   "modff",
168   "expf",
169   "exp2f",
170   "round",
171   "roundf",
172   "sincosf",
173   "sinf",
174   "trunc",
175   "truncf",
176  ];
177}
178
179def StringAPI : PublicAPI<"string.h"> {
180  let Functions = [
181    "bzero",
182    "memcpy",
183    "memmove",
184    "memcmp",
185    "memchr",
186    "memset",
187    "strcpy",
188    "strncpy",
189    "strcat",
190    "strncat",
191    "strcmp",
192    "strcoll",
193    "strncmp",
194    "strxfrm",
195    "strchr",
196    "strcspn",
197    "strpbrk",
198    "strrchr",
199    "strspn",
200    "strstr",
201    "strtok",
202    "strerror",
203    "strlen",
204  ];
205
206  let TypeDeclarations = [
207    SizeT,
208  ];
209
210  let Macros = [
211    NullMacro,
212  ];
213}
214
215def StdIOAPI : PublicAPI<"stdio.h"> {
216  let TypeDeclarations = [
217    SizeT,
218    FILE,
219  ];
220
221  let Functions = [
222    "fwrite",
223  ];
224}
225
226def StdlibAPI : PublicAPI<"stdlib.h"> {
227  let Functions = [
228    "_Exit",
229    "abort",
230  ];
231}
232
233def ErrnoAPI : PublicAPI<"errno.h"> {
234  let Macros = [
235    ErrnoMacro,
236    // We largely depend on linux/errno.h to give us the
237    // various error macro definitions. However, some libc
238    // implementations have chosen to provide definitions
239    // for some of the error macros to account for the ones
240    // missing in linux/errno.h. There is no harm in doing
241    // the same here if we define the macros only when they
242    // are not already defined.
243    MacroDefineIfNot<"ENOTSUP", "EOPNOTSUPP">,
244    MacroDefineIfNot<"ECANCELED", "125">,
245    MacroDefineIfNot<"EOWNERDEAD", "130">,
246    MacroDefineIfNot<"ENOTRECOVERABLE", "131">,
247    MacroDefineIfNot<"ERFKILL", "132">,
248    MacroDefineIfNot<"EHWPOISON", "133">,
249  ];
250}
251
252def SysMManAPI : PublicAPI<"sys/mman.h"> {
253  let Macros = [
254    SimpleMacroDef<"PROT_NONE", "0">,
255    SimpleMacroDef<"PROT_READ", "1">,
256    SimpleMacroDef<"PROT_WRITE", "2">,
257    SimpleMacroDef<"PROT_EXEC", "4">,
258
259    SimpleMacroDef<"MAP_FIXED", "1">,
260    SimpleMacroDef<"MAP_PRIVATE", "2">,
261    SimpleMacroDef<"MAP_SHARED", "4">,
262
263    SimpleMacroDef<"MAP_FAILED", "((void*)-1)">,
264
265    // TODO: The value of 0x20 is good for x86_64, but has to be extended
266    // in some manner to accommodate other machine architectures.
267    SimpleMacroDef<"MAP_ANONYMOUS", "0x20">
268
269    // TODO: Add other MAP_* macros used by Linux.
270  ];
271
272  let TypeDeclarations = [
273    SizeT,
274    OffT,
275  ];
276
277  let Functions = [
278    "mmap",
279    "munmap",
280  ];
281}
282
283def StructSigactionDefn : TypeDecl<"struct sigaction"> {
284  let Decl = [{
285    struct __sigaction {
286      union {
287        void (*sa_handler)(int);
288        void (*sa_action)(int, siginfo_t *, void *);
289      };
290      sigset_t sa_mask;
291      int sa_flags;
292      void (*sa_restorer)(void);
293    };
294  }];
295}
296
297def SighandlerTDefn : TypeDecl<"__sighandler_t"> {
298  let Decl = [{
299    typedef void(*__sighandler_t)(int);
300  }];
301}
302
303def SignalAPI : PublicAPI<"signal.h"> {
304  let TypeDeclarations = [
305    StructSigactionDefn,
306    SighandlerTDefn,
307  ];
308
309  let Functions = [
310    "raise",
311    "sigaction",
312    "sigdelset",
313    "sigprocmask",
314    "sigemptyset",
315    "sigaddset",
316    "sigfillset",
317    "signal",
318  ];
319}
320
321def OnceFlag : TypeDecl<"once_flag"> {
322  let Decl = [{
323    typedef unsigned int once_flag;
324  }];
325}
326
327def MtxT : TypeDecl<"mtx_t"> {
328  let Decl = [{
329    typedef struct {
330      unsigned char __internal_data[4];
331      int __mtx_type;
332    } mtx_t;
333  }];
334}
335
336def ThreadStartT : TypeDecl<"thrd_start_t"> {
337  let Decl = "typedef int (*thrd_start_t)(void *);";
338}
339
340def CallOnceFuncT : TypeDecl<"__call_once_func_t"> {
341  let Decl = [{
342    typedef void(*__call_once_func_t)(void);
343  }];
344}
345
346def ThreadsAPI : PublicAPI<"threads.h"> {
347  let Macros = [
348    SimpleMacroDef<"ONCE_FLAG_INIT", "0">,
349  ];
350
351  let TypeDeclarations = [
352    OnceFlag,
353    CallOnceFuncT,
354    MtxT,
355    ThreadStartT,
356  ];
357
358  let Enumerations = [
359    "mtx_plain",
360    "mtx_recursive",
361    "mtx_timed",
362    "thrd_timedout",
363    "thrd_success",
364    "thrd_busy",
365    "thrd_error",
366    "thrd_nomem",
367  ];
368
369  let Functions = [
370    "call_once",
371    "mtx_init",
372    "mtx_lock",
373    "mtx_unlock",
374    "thrd_create",
375    "thrd_join",
376  ];
377}
378
379def UniStdAPI : PublicAPI<"unistd.h"> {
380  let TypeDeclarations = [
381    SSizeT,
382    SizeT,
383  ];
384
385  let Functions = [
386    "write",
387  ];
388}
389