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 AssertMacro : MacroDef<"assert"> {
10  let Defn = [{
11    #undef assert
12
13    #ifdef NDEBUG
14    #define assert(e) (void)0
15    #else
16
17    #ifdef __cplusplus
18    extern "C"
19    #endif
20    _Noreturn void __assert_fail(const char *, const char *, unsigned, const char *);
21
22    #define assert(e)  \
23      ((e) ? (void)0 : __assert_fail(#e, __FILE__, __LINE__, __PRETTY_FUNCTION__))
24
25    #endif
26  }];
27}
28
29def StaticAssertMacro : MacroDef<"static_assert"> {
30  let Defn = [{
31    #ifndef __cplusplus
32    #undef static_assert
33    #define static_assert _Static_assert
34    #endif
35  }];
36}
37
38def NullMacro : MacroDef<"NULL"> {
39  let Defn = [{
40    #define __need_NULL
41    #include <stddef.h>
42  }];
43}
44
45def ErrnoMacro : MacroDef<"errno"> {
46  let Defn = [{
47    extern _Thread_local int __llvmlibc_errno;
48    #define errno __llvmlibc_errno
49  }];
50}
51
52def AssertAPI : PublicAPI<"assert.h"> {
53  let Macros = [
54    AssertMacro,
55    StaticAssertMacro,
56  ];
57}
58
59def CTypeAPI : PublicAPI<"ctype.h"> {
60}
61
62def FCntlAPI : PublicAPI<"fcntl.h"> {
63  let Types = ["mode_t"];
64}
65
66def IntTypesAPI : PublicAPI<"inttypes.h"> {
67  let Types = ["imaxdiv_t"];
68}
69
70def MathErrHandlingMacro : MacroDef<"math_errhandling"> {
71  let Defn = [{
72    #ifndef math_errhandling
73    #ifdef __FAST_MATH__
74    #define math_errhandling 0
75    #elif defined __NO_MATH_ERRNO__
76    #define math_errhandling (MATH_ERREXCEPT)
77    #else
78    #define math_errhandling (MATH_ERRNO | MATH_ERREXCEPT)
79    #endif
80    #endif // math_errhandling not defined
81  }];
82}
83
84def IsFiniteMacro : MacroDef<"isfinite"> {
85  let Defn = [{
86    #define isfinite(x) __builtin_isfinite(x)
87  }];
88}
89
90def IsInfMacro : MacroDef<"isinf"> {
91  let Defn = [{
92    #define isinf(x) __builtin_isinf(x)
93  }];
94}
95
96def IsNanMacro : MacroDef<"isnan"> {
97  let Defn = [{
98    #define isnan(x) __builtin_isnan(x)
99  }];
100}
101
102def MathAPI : PublicAPI<"math.h"> {
103  let Macros = [
104    SimpleMacroDef<"MATH_ERRNO", "1">,
105    SimpleMacroDef<"MATH_ERREXCEPT", "2">,
106    MathErrHandlingMacro,
107
108    SimpleMacroDef<"INFINITY", "__builtin_inff()">,
109    SimpleMacroDef<"NAN", "__builtin_nanf(\"\")">,
110
111    SimpleMacroDef<"FP_ILOGB0", "(-__INT_MAX__ - 1)">, // INT_MIN
112    SimpleMacroDef<"FP_ILOGBNAN", "__INT_MAX__">,
113
114    IsFiniteMacro,
115    IsInfMacro,
116    IsNanMacro,
117  ];
118  let Types = ["double_t", "float_t"];
119}
120
121def FenvAPI: PublicAPI<"fenv.h"> {
122  let Macros = [
123    SimpleMacroDef<"FE_DIVBYZERO", "1">,
124    SimpleMacroDef<"FE_INEXACT", "2">,
125    SimpleMacroDef<"FE_INVALID", "4">,
126    SimpleMacroDef<"FE_OVERFLOW", "8">,
127    SimpleMacroDef<"FE_UNDERFLOW", "16">,
128    SimpleMacroDef<"FE_ALL_EXCEPT", "(FE_DIVBYZERO|FE_INEXACT|FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW)">,
129
130    SimpleMacroDef<"FE_DOWNWARD", "1">,
131    SimpleMacroDef<"FE_TONEAREST", "2">,
132    SimpleMacroDef<"FE_TOWARDZERO", "4">,
133    SimpleMacroDef<"FE_UPWARD", "8">,
134
135    SimpleMacroDef<"FE_DFL_ENV", "((fenv_t *)-1)">,
136  ];
137  let Types = ["fenv_t", "fexcept_t"];
138}
139
140def StringAPI : PublicAPI<"string.h"> {
141  let Types = ["size_t"];
142
143  let Macros = [
144    NullMacro,
145  ];
146}
147
148def StdIOAPI : PublicAPI<"stdio.h"> {
149  let Types = ["size_t", "FILE", "cookie_io_functions_t"];
150}
151
152def StdlibAPI : PublicAPI<"stdlib.h"> {
153  let Types = [
154    "div_t",
155    "ldiv_t",
156    "lldiv_t",
157    "size_t",
158    "__bsearchcompare_t",
159    "__qsortcompare_t",
160    "__atexithandler_t",
161  ];
162}
163
164def TimeAPI : PublicAPI<"time.h"> {
165  let Types = ["time_t", "struct tm"];
166}
167
168def ErrnoAPI : PublicAPI<"errno.h"> {
169  let Macros = [
170    ErrnoMacro,
171    // We largely depend on linux/errno.h to give us the
172    // various error macro definitions. However, some libc
173    // implementations have chosen to provide definitions
174    // for some of the error macros to account for the ones
175    // missing in linux/errno.h. There is no harm in doing
176    // the same here if we define the macros only when they
177    // are not already defined.
178    MacroDefineIfNot<"ENOTSUP", "EOPNOTSUPP">,
179    MacroDefineIfNot<"ECANCELED", "125">,
180    MacroDefineIfNot<"EOWNERDEAD", "130">,
181    MacroDefineIfNot<"ENOTRECOVERABLE", "131">,
182    MacroDefineIfNot<"ERFKILL", "132">,
183    MacroDefineIfNot<"EHWPOISON", "133">,
184  ];
185}
186
187def SysMManAPI : PublicAPI<"sys/mman.h"> {
188  let Types = ["off_t", "size_t"];
189  let Macros = [
190    SimpleMacroDef<"PROT_NONE", "0">,
191    SimpleMacroDef<"PROT_READ", "1">,
192    SimpleMacroDef<"PROT_WRITE", "2">,
193    SimpleMacroDef<"PROT_EXEC", "4">,
194
195    SimpleMacroDef<"MAP_FIXED", "1">,
196    SimpleMacroDef<"MAP_PRIVATE", "2">,
197    SimpleMacroDef<"MAP_SHARED", "4">,
198
199    SimpleMacroDef<"MAP_FAILED", "((void*)-1)">,
200
201    // TODO: The value of 0x20 is good for x86_64, but has to be extended
202    // in some manner to accommodate other machine architectures.
203    SimpleMacroDef<"MAP_ANONYMOUS", "0x20">
204
205    // TODO: Add other MAP_* macros used by Linux.
206  ];
207
208}
209
210def SignalAPI : PublicAPI<"signal.h"> {
211  let Types = ["struct sigaction", "__sighandler_t"];
212}
213
214def ThreadsAPI : PublicAPI<"threads.h"> {
215  let Macros = [
216    SimpleMacroDef<"ONCE_FLAG_INIT", "{0}">,
217  ];
218
219  let Types = [
220    "__call_once_func_t",
221    "once_flag",
222    "cnd_t",
223    "mtx_t",
224    "thrd_t",
225    "thrd_start_t",
226  ];
227
228  let Enumerations = [
229    "mtx_plain",
230    "mtx_recursive",
231    "mtx_timed",
232    "thrd_timedout",
233    "thrd_success",
234    "thrd_busy",
235    "thrd_error",
236    "thrd_nomem",
237  ];
238}
239
240def PThreadAPI : PublicAPI<"pthread.h"> {
241  let Types = [
242      "pthread_attr_t",
243      "pthread_mutexattr_t"
244  ];
245}
246
247def UniStdAPI : PublicAPI<"unistd.h"> {
248  let Types = ["off_t", "size_t", "ssize_t"];
249}
250
251def SysStatAPI : PublicAPI<"sys/stat.h"> {
252  let Types = ["mode_t"];
253}
254