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