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 CTypeAPI : PublicAPI<"ctype.h"> { 90 let Functions = [ 91 "isalnum", 92 "isalpha", 93 "isblank", 94 "iscntrl", 95 "isdigit", 96 "isgraph", 97 "islower", 98 "isprint", 99 "ispunct", 100 "isspace", 101 "isupper", 102 "isxdigit", 103 "tolower", 104 "toupper", 105 ]; 106} 107 108def MathErrHandlingMacro : MacroDef<"math_errhandling"> { 109 let Defn = [{ 110 #ifndef math_errhandling 111 #ifdef __FAST_MATH__ 112 #define math_errhandling 0 113 #elif defined __NO_MATH_ERRNO__ 114 #define math_errhandling (MATH_ERREXCEPT) 115 #else 116 #define math_errhandling (MATH_ERRNO | MATH_ERREXCEPT) 117 #endif 118 #endif // math_errhandling not defined 119 }]; 120} 121 122def IsFiniteMacro : MacroDef<"isfinite"> { 123 let Defn = [{ 124 #define isfinite(x) __builtin_isfinite(x) 125 }]; 126} 127 128def IsInfMacro : MacroDef<"isinf"> { 129 let Defn = [{ 130 #define isinf(x) __builtin_isinf(x) 131 }]; 132} 133 134def IsNanMacro : MacroDef<"isnan"> { 135 let Defn = [{ 136 #define isnan(x) __builtin_isnan(x) 137 }]; 138} 139 140def FloatT : TypeDecl<"float_t"> { 141 let Decl = [{ 142 #define __need_float_t 143 #include <__llvm-libc-stdc-types.h> 144 }]; 145} 146 147def DoubleT : TypeDecl<"double_t"> { 148 let Decl = [{ 149 #define __need_double_t 150 #include <__llvm-libc-stdc-types.h> 151 }]; 152} 153 154def MathAPI : PublicAPI<"math.h"> { 155 let Macros = [ 156 SimpleMacroDef<"MATH_ERRNO", "1">, 157 SimpleMacroDef<"MATH_ERREXCEPT", "2">, 158 MathErrHandlingMacro, 159 160 SimpleMacroDef<"INFINITY", "__builtin_inff()">, 161 SimpleMacroDef<"NAN", "__builtin_nanf(\"\")">, 162 163 IsFiniteMacro, 164 IsInfMacro, 165 IsNanMacro, 166 ]; 167 let TypeDeclarations = [ 168 DoubleT, 169 FloatT, 170 ]; 171 let Functions = [ 172 "copysign", 173 "copysignf", 174 "copysignl", 175 "ceil", 176 "ceilf", 177 "ceill", 178 "cosf", 179 "fabs", 180 "fabsf", 181 "fabsl", 182 "floor", 183 "floorf", 184 "floorl", 185 "fmax", 186 "fmaxf", 187 "fmaxl", 188 "fmin", 189 "fminf", 190 "fminl", 191 "frexp", 192 "frexpf", 193 "frexpl", 194 "logb", 195 "logbf", 196 "logbl", 197 "modf", 198 "modff", 199 "modfl", 200 "expf", 201 "exp2f", 202 "round", 203 "roundf", 204 "roundl", 205 "sincosf", 206 "sinf", 207 "trunc", 208 "truncf", 209 "truncl", 210 ]; 211} 212 213def StringAPI : PublicAPI<"string.h"> { 214 let Functions = [ 215 "bzero", 216 "memchr", 217 "memcmp", 218 "memcpy", 219 "memmove", 220 "memrchr", 221 "memset", 222 "strcat", 223 "strchr", 224 "strcmp", 225 "strcoll", 226 "strcpy", 227 "strcspn", 228 "strerror", 229 "strlen", 230 "strncat", 231 "strncmp", 232 "strncpy", 233 "strnlen", 234 "strpbrk", 235 "strrchr", 236 "strspn", 237 "strstr", 238 "strtok", 239 "strtok_r", 240 "strxfrm", 241 ]; 242 243 let TypeDeclarations = [ 244 SizeT, 245 ]; 246 247 let Macros = [ 248 NullMacro, 249 ]; 250} 251 252def StdIOAPI : PublicAPI<"stdio.h"> { 253 let TypeDeclarations = [ 254 SizeT, 255 FILE, 256 ]; 257 258 let Functions = [ 259 "fwrite", 260 ]; 261} 262 263def StdlibAPI : PublicAPI<"stdlib.h"> { 264 let Functions = [ 265 "_Exit", 266 "abort", 267 ]; 268} 269 270def ErrnoAPI : PublicAPI<"errno.h"> { 271 let Macros = [ 272 ErrnoMacro, 273 // We largely depend on linux/errno.h to give us the 274 // various error macro definitions. However, some libc 275 // implementations have chosen to provide definitions 276 // for some of the error macros to account for the ones 277 // missing in linux/errno.h. There is no harm in doing 278 // the same here if we define the macros only when they 279 // are not already defined. 280 MacroDefineIfNot<"ENOTSUP", "EOPNOTSUPP">, 281 MacroDefineIfNot<"ECANCELED", "125">, 282 MacroDefineIfNot<"EOWNERDEAD", "130">, 283 MacroDefineIfNot<"ENOTRECOVERABLE", "131">, 284 MacroDefineIfNot<"ERFKILL", "132">, 285 MacroDefineIfNot<"EHWPOISON", "133">, 286 ]; 287} 288 289def SysMManAPI : PublicAPI<"sys/mman.h"> { 290 let Macros = [ 291 SimpleMacroDef<"PROT_NONE", "0">, 292 SimpleMacroDef<"PROT_READ", "1">, 293 SimpleMacroDef<"PROT_WRITE", "2">, 294 SimpleMacroDef<"PROT_EXEC", "4">, 295 296 SimpleMacroDef<"MAP_FIXED", "1">, 297 SimpleMacroDef<"MAP_PRIVATE", "2">, 298 SimpleMacroDef<"MAP_SHARED", "4">, 299 300 SimpleMacroDef<"MAP_FAILED", "((void*)-1)">, 301 302 // TODO: The value of 0x20 is good for x86_64, but has to be extended 303 // in some manner to accommodate other machine architectures. 304 SimpleMacroDef<"MAP_ANONYMOUS", "0x20"> 305 306 // TODO: Add other MAP_* macros used by Linux. 307 ]; 308 309 let TypeDeclarations = [ 310 SizeT, 311 OffT, 312 ]; 313 314 let Functions = [ 315 "mmap", 316 "munmap", 317 ]; 318} 319 320def StructSigactionDefn : TypeDecl<"struct sigaction"> { 321 let Decl = [{ 322 struct __sigaction { 323 union { 324 void (*sa_handler)(int); 325 void (*sa_action)(int, siginfo_t *, void *); 326 }; 327 sigset_t sa_mask; 328 int sa_flags; 329 void (*sa_restorer)(void); 330 }; 331 }]; 332} 333 334def SighandlerTDefn : TypeDecl<"__sighandler_t"> { 335 let Decl = [{ 336 typedef void(*__sighandler_t)(int); 337 }]; 338} 339 340def SignalAPI : PublicAPI<"signal.h"> { 341 let TypeDeclarations = [ 342 StructSigactionDefn, 343 SighandlerTDefn, 344 ]; 345 346 let Functions = [ 347 "raise", 348 "sigaction", 349 "sigdelset", 350 "sigprocmask", 351 "sigemptyset", 352 "sigaddset", 353 "sigfillset", 354 "signal", 355 ]; 356} 357 358def OnceFlag : TypeDecl<"once_flag"> { 359 let Decl = [{ 360 typedef unsigned int once_flag; 361 }]; 362} 363 364def MtxT : TypeDecl<"mtx_t"> { 365 let Decl = [{ 366 typedef struct { 367 unsigned char __internal_data[4]; 368 int __mtx_type; 369 } mtx_t; 370 }]; 371} 372 373def ThreadStartT : TypeDecl<"thrd_start_t"> { 374 let Decl = "typedef int (*thrd_start_t)(void *);"; 375} 376 377def CallOnceFuncT : TypeDecl<"__call_once_func_t"> { 378 let Decl = [{ 379 typedef void(*__call_once_func_t)(void); 380 }]; 381} 382 383def ThreadsAPI : PublicAPI<"threads.h"> { 384 let Macros = [ 385 SimpleMacroDef<"ONCE_FLAG_INIT", "0">, 386 ]; 387 388 let TypeDeclarations = [ 389 OnceFlag, 390 CallOnceFuncT, 391 MtxT, 392 ThreadStartT, 393 ]; 394 395 let Enumerations = [ 396 "mtx_plain", 397 "mtx_recursive", 398 "mtx_timed", 399 "thrd_timedout", 400 "thrd_success", 401 "thrd_busy", 402 "thrd_error", 403 "thrd_nomem", 404 ]; 405 406 let Functions = [ 407 "call_once", 408 "mtx_init", 409 "mtx_lock", 410 "mtx_unlock", 411 "thrd_create", 412 "thrd_join", 413 ]; 414} 415 416def UniStdAPI : PublicAPI<"unistd.h"> { 417 let TypeDeclarations = [ 418 SSizeT, 419 SizeT, 420 ]; 421 422 let Functions = [ 423 "write", 424 ]; 425} 426