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   "copysignl",
156   "ceil",
157   "ceilf",
158   "ceill",
159   "cosf",
160   "fabs",
161   "fabsf",
162   "fabsl",
163   "floor",
164   "floorf",
165   "floorl",
166   "frexp",
167   "frexpf",
168   "frexpl",
169   "logb",
170   "logbf",
171   "logbl",
172   "modf",
173   "modff",
174   "modfl",
175   "expf",
176   "exp2f",
177   "round",
178   "roundf",
179   "roundl",
180   "sincosf",
181   "sinf",
182   "trunc",
183   "truncf",
184   "truncl",
185  ];
186}
187
188def StringAPI : PublicAPI<"string.h"> {
189  let Functions = [
190    "bzero",
191    "memcpy",
192    "memmove",
193    "memcmp",
194    "memchr",
195    "memset",
196    "strcpy",
197    "strncpy",
198    "strcat",
199    "strncat",
200    "strcmp",
201    "strcoll",
202    "strncmp",
203    "strxfrm",
204    "strchr",
205    "strcspn",
206    "strpbrk",
207    "strrchr",
208    "strspn",
209    "strstr",
210    "strtok",
211    "strerror",
212    "strlen",
213  ];
214
215  let TypeDeclarations = [
216    SizeT,
217  ];
218
219  let Macros = [
220    NullMacro,
221  ];
222}
223
224def StdIOAPI : PublicAPI<"stdio.h"> {
225  let TypeDeclarations = [
226    SizeT,
227    FILE,
228  ];
229
230  let Functions = [
231    "fwrite",
232  ];
233}
234
235def StdlibAPI : PublicAPI<"stdlib.h"> {
236  let Functions = [
237    "_Exit",
238    "abort",
239  ];
240}
241
242def ErrnoAPI : PublicAPI<"errno.h"> {
243  let Macros = [
244    ErrnoMacro,
245    // We largely depend on linux/errno.h to give us the
246    // various error macro definitions. However, some libc
247    // implementations have chosen to provide definitions
248    // for some of the error macros to account for the ones
249    // missing in linux/errno.h. There is no harm in doing
250    // the same here if we define the macros only when they
251    // are not already defined.
252    MacroDefineIfNot<"ENOTSUP", "EOPNOTSUPP">,
253    MacroDefineIfNot<"ECANCELED", "125">,
254    MacroDefineIfNot<"EOWNERDEAD", "130">,
255    MacroDefineIfNot<"ENOTRECOVERABLE", "131">,
256    MacroDefineIfNot<"ERFKILL", "132">,
257    MacroDefineIfNot<"EHWPOISON", "133">,
258  ];
259}
260
261def SysMManAPI : PublicAPI<"sys/mman.h"> {
262  let Macros = [
263    SimpleMacroDef<"PROT_NONE", "0">,
264    SimpleMacroDef<"PROT_READ", "1">,
265    SimpleMacroDef<"PROT_WRITE", "2">,
266    SimpleMacroDef<"PROT_EXEC", "4">,
267
268    SimpleMacroDef<"MAP_FIXED", "1">,
269    SimpleMacroDef<"MAP_PRIVATE", "2">,
270    SimpleMacroDef<"MAP_SHARED", "4">,
271
272    SimpleMacroDef<"MAP_FAILED", "((void*)-1)">,
273
274    // TODO: The value of 0x20 is good for x86_64, but has to be extended
275    // in some manner to accommodate other machine architectures.
276    SimpleMacroDef<"MAP_ANONYMOUS", "0x20">
277
278    // TODO: Add other MAP_* macros used by Linux.
279  ];
280
281  let TypeDeclarations = [
282    SizeT,
283    OffT,
284  ];
285
286  let Functions = [
287    "mmap",
288    "munmap",
289  ];
290}
291
292def StructSigactionDefn : TypeDecl<"struct sigaction"> {
293  let Decl = [{
294    struct __sigaction {
295      union {
296        void (*sa_handler)(int);
297        void (*sa_action)(int, siginfo_t *, void *);
298      };
299      sigset_t sa_mask;
300      int sa_flags;
301      void (*sa_restorer)(void);
302    };
303  }];
304}
305
306def SighandlerTDefn : TypeDecl<"__sighandler_t"> {
307  let Decl = [{
308    typedef void(*__sighandler_t)(int);
309  }];
310}
311
312def SignalAPI : PublicAPI<"signal.h"> {
313  let TypeDeclarations = [
314    StructSigactionDefn,
315    SighandlerTDefn,
316  ];
317
318  let Functions = [
319    "raise",
320    "sigaction",
321    "sigdelset",
322    "sigprocmask",
323    "sigemptyset",
324    "sigaddset",
325    "sigfillset",
326    "signal",
327  ];
328}
329
330def OnceFlag : TypeDecl<"once_flag"> {
331  let Decl = [{
332    typedef unsigned int once_flag;
333  }];
334}
335
336def MtxT : TypeDecl<"mtx_t"> {
337  let Decl = [{
338    typedef struct {
339      unsigned char __internal_data[4];
340      int __mtx_type;
341    } mtx_t;
342  }];
343}
344
345def ThreadStartT : TypeDecl<"thrd_start_t"> {
346  let Decl = "typedef int (*thrd_start_t)(void *);";
347}
348
349def CallOnceFuncT : TypeDecl<"__call_once_func_t"> {
350  let Decl = [{
351    typedef void(*__call_once_func_t)(void);
352  }];
353}
354
355def ThreadsAPI : PublicAPI<"threads.h"> {
356  let Macros = [
357    SimpleMacroDef<"ONCE_FLAG_INIT", "0">,
358  ];
359
360  let TypeDeclarations = [
361    OnceFlag,
362    CallOnceFuncT,
363    MtxT,
364    ThreadStartT,
365  ];
366
367  let Enumerations = [
368    "mtx_plain",
369    "mtx_recursive",
370    "mtx_timed",
371    "thrd_timedout",
372    "thrd_success",
373    "thrd_busy",
374    "thrd_error",
375    "thrd_nomem",
376  ];
377
378  let Functions = [
379    "call_once",
380    "mtx_init",
381    "mtx_lock",
382    "mtx_unlock",
383    "thrd_create",
384    "thrd_join",
385  ];
386}
387
388def UniStdAPI : PublicAPI<"unistd.h"> {
389  let TypeDeclarations = [
390    SSizeT,
391    SizeT,
392  ];
393
394  let Functions = [
395    "write",
396  ];
397}
398