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} 91 92def MathErrHandlingMacro : MacroDef<"math_errhandling"> { 93 let Defn = [{ 94 #ifndef math_errhandling 95 #ifdef __FAST_MATH__ 96 #define math_errhandling 0 97 #elif defined __NO_MATH_ERRNO__ 98 #define math_errhandling (MATH_ERREXCEPT) 99 #else 100 #define math_errhandling (MATH_ERRNO | MATH_ERREXCEPT) 101 #endif 102 #endif // math_errhandling not defined 103 }]; 104} 105 106def IsFiniteMacro : MacroDef<"isfinite"> { 107 let Defn = [{ 108 #define isfinite(x) __builtin_isfinite(x) 109 }]; 110} 111 112def IsInfMacro : MacroDef<"isinf"> { 113 let Defn = [{ 114 #define isinf(x) __builtin_isinf(x) 115 }]; 116} 117 118def IsNanMacro : MacroDef<"isnan"> { 119 let Defn = [{ 120 #define isnan(x) __builtin_isnan(x) 121 }]; 122} 123 124def FloatT : TypeDecl<"float_t"> { 125 let Decl = [{ 126 #define __need_float_t 127 #include <__llvm-libc-stdc-types.h> 128 }]; 129} 130 131def DoubleT : TypeDecl<"double_t"> { 132 let Decl = [{ 133 #define __need_double_t 134 #include <__llvm-libc-stdc-types.h> 135 }]; 136} 137 138def MathAPI : PublicAPI<"math.h"> { 139 let Macros = [ 140 SimpleMacroDef<"MATH_ERRNO", "1">, 141 SimpleMacroDef<"MATH_ERREXCEPT", "2">, 142 MathErrHandlingMacro, 143 144 SimpleMacroDef<"INFINITY", "__builtin_inff()">, 145 SimpleMacroDef<"NAN", "__builtin_nanf(\"\")">, 146 147 SimpleMacroDef<"FP_ILOGB0", "(-__INT_MAX__ - 1)">, // INT_MIN 148 SimpleMacroDef<"FP_ILOGBNAN", "__INT_MAX__">, 149 150 IsFiniteMacro, 151 IsInfMacro, 152 IsNanMacro, 153 ]; 154 let TypeDeclarations = [ 155 DoubleT, 156 FloatT, 157 ]; 158} 159 160def StringAPI : PublicAPI<"string.h"> { 161 let TypeDeclarations = [ 162 SizeT, 163 ]; 164 165 let Macros = [ 166 NullMacro, 167 ]; 168} 169 170def StdIOAPI : PublicAPI<"stdio.h"> { 171 let TypeDeclarations = [ 172 SizeT, 173 FILE, 174 ]; 175} 176 177def StdlibAPI : PublicAPI<"stdlib.h"> { 178} 179 180def ErrnoAPI : PublicAPI<"errno.h"> { 181 let Macros = [ 182 ErrnoMacro, 183 // We largely depend on linux/errno.h to give us the 184 // various error macro definitions. However, some libc 185 // implementations have chosen to provide definitions 186 // for some of the error macros to account for the ones 187 // missing in linux/errno.h. There is no harm in doing 188 // the same here if we define the macros only when they 189 // are not already defined. 190 MacroDefineIfNot<"ENOTSUP", "EOPNOTSUPP">, 191 MacroDefineIfNot<"ECANCELED", "125">, 192 MacroDefineIfNot<"EOWNERDEAD", "130">, 193 MacroDefineIfNot<"ENOTRECOVERABLE", "131">, 194 MacroDefineIfNot<"ERFKILL", "132">, 195 MacroDefineIfNot<"EHWPOISON", "133">, 196 ]; 197} 198 199def SysMManAPI : PublicAPI<"sys/mman.h"> { 200 let Macros = [ 201 SimpleMacroDef<"PROT_NONE", "0">, 202 SimpleMacroDef<"PROT_READ", "1">, 203 SimpleMacroDef<"PROT_WRITE", "2">, 204 SimpleMacroDef<"PROT_EXEC", "4">, 205 206 SimpleMacroDef<"MAP_FIXED", "1">, 207 SimpleMacroDef<"MAP_PRIVATE", "2">, 208 SimpleMacroDef<"MAP_SHARED", "4">, 209 210 SimpleMacroDef<"MAP_FAILED", "((void*)-1)">, 211 212 // TODO: The value of 0x20 is good for x86_64, but has to be extended 213 // in some manner to accommodate other machine architectures. 214 SimpleMacroDef<"MAP_ANONYMOUS", "0x20"> 215 216 // TODO: Add other MAP_* macros used by Linux. 217 ]; 218 219 let TypeDeclarations = [ 220 SizeT, 221 OffT, 222 ]; 223} 224 225def StructSigactionDefn : TypeDecl<"struct sigaction"> { 226 let Decl = [{ 227 struct __sigaction { 228 union { 229 void (*sa_handler)(int); 230 void (*sa_action)(int, siginfo_t *, void *); 231 }; 232 sigset_t sa_mask; 233 int sa_flags; 234 void (*sa_restorer)(void); 235 }; 236 }]; 237} 238 239def SighandlerTDefn : TypeDecl<"__sighandler_t"> { 240 let Decl = [{ 241 typedef void(*__sighandler_t)(int); 242 }]; 243} 244 245def SignalAPI : PublicAPI<"signal.h"> { 246 let TypeDeclarations = [ 247 StructSigactionDefn, 248 SighandlerTDefn, 249 ]; 250} 251 252def OnceFlag : TypeDecl<"once_flag"> { 253 let Decl = [{ 254 typedef unsigned int once_flag; 255 }]; 256} 257 258def MtxT : TypeDecl<"mtx_t"> { 259 let Decl = [{ 260 typedef struct { 261 unsigned char __internal_data[4]; 262 int __mtx_type; 263 } mtx_t; 264 }]; 265} 266 267def ThreadStartT : TypeDecl<"thrd_start_t"> { 268 let Decl = "typedef int (*thrd_start_t)(void *);"; 269} 270 271def CallOnceFuncT : TypeDecl<"__call_once_func_t"> { 272 let Decl = [{ 273 typedef void(*__call_once_func_t)(void); 274 }]; 275} 276 277def ThreadsAPI : PublicAPI<"threads.h"> { 278 let Macros = [ 279 SimpleMacroDef<"ONCE_FLAG_INIT", "0">, 280 ]; 281 282 let TypeDeclarations = [ 283 OnceFlag, 284 CallOnceFuncT, 285 MtxT, 286 ThreadStartT, 287 ]; 288 289 let Enumerations = [ 290 "mtx_plain", 291 "mtx_recursive", 292 "mtx_timed", 293 "thrd_timedout", 294 "thrd_success", 295 "thrd_busy", 296 "thrd_error", 297 "thrd_nomem", 298 ]; 299} 300 301def UniStdAPI : PublicAPI<"unistd.h"> { 302 let TypeDeclarations = [ 303 SSizeT, 304 SizeT, 305 ]; 306} 307