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