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