| ... | ... | @@ -1,662 +0,0 @@ |
| 1 | | /* |
| 2 | | * Copyright (c) 2015 Andrew Kelley |
| 3 | | * |
| 4 | | * This file is part of zig, which is MIT licensed. |
| 5 | | * See http://opensource.org/licenses/MIT |
| 6 | | */ |
| 7 | | |
| 8 | | #include "parseh.hpp" |
| 9 | | #include "config.h" |
| 10 | | |
| 11 | | #include <clang-c/Index.h> |
| 12 | | |
| 13 | | #include <string.h> |
| 14 | | |
| 15 | | struct TypeDef { |
| 16 | | Buf alias; |
| 17 | | Buf target; |
| 18 | | }; |
| 19 | | |
| 20 | | struct Arg { |
| 21 | | Buf name; |
| 22 | | Buf *type; |
| 23 | | }; |
| 24 | | |
| 25 | | struct Fn { |
| 26 | | Buf name; |
| 27 | | Buf *return_type; |
| 28 | | Arg *args; |
| 29 | | int arg_count; |
| 30 | | bool is_variadic; |
| 31 | | }; |
| 32 | | |
| 33 | | struct Field { |
| 34 | | Buf name; |
| 35 | | Buf *type; |
| 36 | | }; |
| 37 | | |
| 38 | | struct Struct { |
| 39 | | Buf name; |
| 40 | | ZigList<Field*> fields; |
| 41 | | bool have_def; |
| 42 | | }; |
| 43 | | |
| 44 | | struct ParseH { |
| 45 | | CXTranslationUnit tu; |
| 46 | | FILE *f; |
| 47 | | ZigList<Fn *> fn_list; |
| 48 | | ZigList<Struct *> struct_list; |
| 49 | | ZigList<TypeDef *> type_def_list; |
| 50 | | ZigList<Struct *> incomplete_struct_list; |
| 51 | | Fn *cur_fn; |
| 52 | | Struct *cur_struct; |
| 53 | | int arg_index; |
| 54 | | int cur_indent; |
| 55 | | CXSourceRange range; |
| 56 | | CXSourceLocation location; |
| 57 | | }; |
| 58 | | |
| 59 | | static const int indent_size = 4; |
| 60 | | |
| 61 | | struct TypeMapping { |
| 62 | | const char *c_name; |
| 63 | | const char *zig_name; |
| 64 | | }; |
| 65 | | |
| 66 | | static const TypeMapping type_mappings[] = { |
| 67 | | { |
| 68 | | "int8_t", |
| 69 | | "i8", |
| 70 | | }, |
| 71 | | { |
| 72 | | "uint8_t", |
| 73 | | "u8", |
| 74 | | }, |
| 75 | | { |
| 76 | | "uint16_t", |
| 77 | | "u16", |
| 78 | | }, |
| 79 | | { |
| 80 | | "uint32_t", |
| 81 | | "u32", |
| 82 | | }, |
| 83 | | { |
| 84 | | "uint64_t", |
| 85 | | "u64", |
| 86 | | }, |
| 87 | | { |
| 88 | | "int16_t", |
| 89 | | "i16", |
| 90 | | }, |
| 91 | | { |
| 92 | | "int32_t", |
| 93 | | "i32", |
| 94 | | }, |
| 95 | | { |
| 96 | | "int64_t", |
| 97 | | "i64", |
| 98 | | }, |
| 99 | | { |
| 100 | | "intptr_t", |
| 101 | | "isize", |
| 102 | | }, |
| 103 | | { |
| 104 | | "uintptr_t", |
| 105 | | "usize", |
| 106 | | }, |
| 107 | | }; |
| 108 | | |
| 109 | | static bool have_struct_def(ParseH *p, Buf *name) { |
| 110 | | for (int i = 0; i < p->struct_list.length; i += 1) { |
| 111 | | Struct *struc = p->struct_list.at(i); |
| 112 | | if (struc->fields.length > 0 && buf_eql_buf(&struc->name, name)) { |
| 113 | | return true; |
| 114 | | } |
| 115 | | } |
| 116 | | return false; |
| 117 | | } |
| 118 | | |
| 119 | | static const char *c_to_zig_name(const char *name) { |
| 120 | | for (int i = 0; i < array_length(type_mappings); i += 1) { |
| 121 | | const TypeMapping *mapping = &type_mappings[i]; |
| 122 | | if (strcmp(mapping->c_name, name) == 0) |
| 123 | | return mapping->zig_name; |
| 124 | | } |
| 125 | | return nullptr; |
| 126 | | } |
| 127 | | |
| 128 | | static bool str_has_prefix(const char *str, const char *prefix) { |
| 129 | | while (*prefix) { |
| 130 | | if (*str && *str == *prefix) { |
| 131 | | str += 1; |
| 132 | | prefix += 1; |
| 133 | | } else { |
| 134 | | return false; |
| 135 | | } |
| 136 | | } |
| 137 | | return true; |
| 138 | | } |
| 139 | | |
| 140 | | static const char *prefixes_stripped(CXType type) { |
| 141 | | CXString name = clang_getTypeSpelling(type); |
| 142 | | const char *c_name = clang_getCString(name); |
| 143 | | |
| 144 | | static const char *prefixes[] = { |
| 145 | | "struct ", |
| 146 | | "enum ", |
| 147 | | "const ", |
| 148 | | }; |
| 149 | | |
| 150 | | start_over: |
| 151 | | |
| 152 | | for (int i = 0; i < array_length(prefixes); i += 1) { |
| 153 | | const char *prefix = prefixes[i]; |
| 154 | | if (str_has_prefix(c_name, prefix)) { |
| 155 | | c_name += strlen(prefix); |
| 156 | | goto start_over; |
| 157 | | } |
| 158 | | } |
| 159 | | return c_name; |
| 160 | | } |
| 161 | | |
| 162 | | static void print_location(ParseH *p) { |
| 163 | | CXFile file; |
| 164 | | unsigned line, column, offset; |
| 165 | | clang_getFileLocation(p->location, &file, &line, &column, &offset); |
| 166 | | CXString file_name = clang_getFileName(file); |
| 167 | | |
| 168 | | fprintf(stderr, "%s line %u, column %u\n", clang_getCString(file_name), line, column); |
| 169 | | } |
| 170 | | |
| 171 | | static bool resolves_to_void(ParseH *p, CXType raw_type) { |
| 172 | | if (raw_type.kind == CXType_Unexposed) { |
| 173 | | CXType canonical = clang_getCanonicalType(raw_type); |
| 174 | | if (canonical.kind == CXType_Unexposed) |
| 175 | | zig_panic("clang C api insufficient"); |
| 176 | | else |
| 177 | | return resolves_to_void(p, canonical); |
| 178 | | } |
| 179 | | if (raw_type.kind == CXType_Void) { |
| 180 | | return true; |
| 181 | | } else if (raw_type.kind == CXType_Typedef) { |
| 182 | | CXCursor typedef_cursor = clang_getTypeDeclaration(raw_type); |
| 183 | | CXType underlying_type = clang_getTypedefDeclUnderlyingType(typedef_cursor); |
| 184 | | return resolves_to_void(p, underlying_type); |
| 185 | | } |
| 186 | | return false; |
| 187 | | } |
| 188 | | |
| 189 | | static Buf *to_zig_type(ParseH *p, CXType raw_type) { |
| 190 | | if (raw_type.kind == CXType_Unexposed) { |
| 191 | | CXType canonical = clang_getCanonicalType(raw_type); |
| 192 | | if (canonical.kind == CXType_Unexposed) |
| 193 | | zig_panic("clang C api insufficient"); |
| 194 | | else |
| 195 | | return to_zig_type(p, canonical); |
| 196 | | } |
| 197 | | switch (raw_type.kind) { |
| 198 | | case CXType_Invalid: |
| 199 | | case CXType_Unexposed: |
| 200 | | zig_unreachable(); |
| 201 | | case CXType_Void: |
| 202 | | zig_panic("void type encountered"); |
| 203 | | case CXType_Bool: |
| 204 | | return buf_create_from_str("bool"); |
| 205 | | case CXType_SChar: |
| 206 | | return buf_create_from_str("i8"); |
| 207 | | case CXType_UChar: |
| 208 | | case CXType_Char_U: |
| 209 | | case CXType_Char_S: |
| 210 | | return buf_create_from_str("u8"); |
| 211 | | case CXType_WChar: |
| 212 | | print_location(p); |
| 213 | | zig_panic("TODO wchar"); |
| 214 | | case CXType_Char16: |
| 215 | | print_location(p); |
| 216 | | zig_panic("TODO char16"); |
| 217 | | case CXType_Char32: |
| 218 | | print_location(p); |
| 219 | | zig_panic("TODO char32"); |
| 220 | | case CXType_UShort: |
| 221 | | return buf_create_from_str("c_ushort"); |
| 222 | | case CXType_UInt: |
| 223 | | return buf_create_from_str("c_uint"); |
| 224 | | case CXType_ULong: |
| 225 | | return buf_create_from_str("c_ulong"); |
| 226 | | case CXType_ULongLong: |
| 227 | | return buf_create_from_str("c_ulonglong"); |
| 228 | | case CXType_UInt128: |
| 229 | | print_location(p); |
| 230 | | zig_panic("TODO uint128"); |
| 231 | | case CXType_Short: |
| 232 | | return buf_create_from_str("c_short"); |
| 233 | | case CXType_Int: |
| 234 | | return buf_create_from_str("c_int"); |
| 235 | | case CXType_Long: |
| 236 | | return buf_create_from_str("c_long"); |
| 237 | | case CXType_LongLong: |
| 238 | | return buf_create_from_str("c_longlong"); |
| 239 | | case CXType_Int128: |
| 240 | | print_location(p); |
| 241 | | zig_panic("TODO int128"); |
| 242 | | case CXType_Float: |
| 243 | | return buf_create_from_str("f32"); |
| 244 | | case CXType_Double: |
| 245 | | return buf_create_from_str("f64"); |
| 246 | | case CXType_LongDouble: |
| 247 | | return buf_create_from_str("f128"); |
| 248 | | case CXType_IncompleteArray: |
| 249 | | { |
| 250 | | CXType pointee_type = clang_getArrayElementType(raw_type); |
| 251 | | Buf *pointee_buf = to_zig_type(p, pointee_type); |
| 252 | | if (clang_isConstQualifiedType(pointee_type)) { |
| 253 | | return buf_sprintf("&const %s", buf_ptr(pointee_buf)); |
| 254 | | } else { |
| 255 | | return buf_sprintf("&%s", buf_ptr(pointee_buf)); |
| 256 | | } |
| 257 | | } |
| 258 | | case CXType_Pointer: |
| 259 | | { |
| 260 | | CXType pointee_type = clang_getPointeeType(raw_type); |
| 261 | | Buf *pointee_buf; |
| 262 | | if (resolves_to_void(p, pointee_type)) { |
| 263 | | pointee_buf = buf_create_from_str("u8"); |
| 264 | | } else { |
| 265 | | pointee_buf = to_zig_type(p, pointee_type); |
| 266 | | } |
| 267 | | if (clang_isConstQualifiedType(pointee_type)) { |
| 268 | | return buf_sprintf("&const %s", buf_ptr(pointee_buf)); |
| 269 | | } else { |
| 270 | | return buf_sprintf("&%s", buf_ptr(pointee_buf)); |
| 271 | | } |
| 272 | | } |
| 273 | | case CXType_Record: |
| 274 | | { |
| 275 | | const char *name = prefixes_stripped(raw_type); |
| 276 | | return buf_sprintf("%s", name); |
| 277 | | } |
| 278 | | case CXType_Enum: |
| 279 | | { |
| 280 | | const char *name = prefixes_stripped(raw_type); |
| 281 | | return buf_sprintf("%s", name); |
| 282 | | } |
| 283 | | case CXType_Typedef: |
| 284 | | { |
| 285 | | const char *name = prefixes_stripped(raw_type); |
| 286 | | const char *zig_name = c_to_zig_name(name); |
| 287 | | if (zig_name) { |
| 288 | | return buf_create_from_str(zig_name); |
| 289 | | } else { |
| 290 | | CXCursor typedef_cursor = clang_getTypeDeclaration(raw_type); |
| 291 | | CXType underlying_type = clang_getTypedefDeclUnderlyingType(typedef_cursor); |
| 292 | | if (resolves_to_void(p, underlying_type)) { |
| 293 | | return buf_create_from_str("u8"); |
| 294 | | } else { |
| 295 | | return buf_create_from_str(name); |
| 296 | | } |
| 297 | | } |
| 298 | | } |
| 299 | | case CXType_ConstantArray: |
| 300 | | { |
| 301 | | CXType child_type = clang_getArrayElementType(raw_type); |
| 302 | | Buf *zig_child_type = to_zig_type(p, child_type); |
| 303 | | long size = (long)clang_getArraySize(raw_type); |
| 304 | | return buf_sprintf("[%s; %ld]", buf_ptr(zig_child_type), size); |
| 305 | | } |
| 306 | | case CXType_FunctionProto: |
| 307 | | fprintf(stderr, "warning: TODO function proto\n"); |
| 308 | | print_location(p); |
| 309 | | return buf_create_from_str("u8"); |
| 310 | | case CXType_FunctionNoProto: |
| 311 | | print_location(p); |
| 312 | | zig_panic("TODO function no proto"); |
| 313 | | case CXType_BlockPointer: |
| 314 | | print_location(p); |
| 315 | | zig_panic("TODO block pointer"); |
| 316 | | case CXType_Vector: |
| 317 | | print_location(p); |
| 318 | | zig_panic("TODO vector"); |
| 319 | | case CXType_LValueReference: |
| 320 | | case CXType_RValueReference: |
| 321 | | case CXType_VariableArray: |
| 322 | | case CXType_DependentSizedArray: |
| 323 | | case CXType_MemberPointer: |
| 324 | | case CXType_ObjCInterface: |
| 325 | | case CXType_ObjCObjectPointer: |
| 326 | | case CXType_NullPtr: |
| 327 | | case CXType_Overload: |
| 328 | | case CXType_Dependent: |
| 329 | | case CXType_ObjCId: |
| 330 | | case CXType_ObjCClass: |
| 331 | | case CXType_ObjCSel: |
| 332 | | case CXType_Complex: |
| 333 | | print_location(p); |
| 334 | | zig_panic("TODO"); |
| 335 | | } |
| 336 | | |
| 337 | | zig_unreachable(); |
| 338 | | } |
| 339 | | |
| 340 | | static bool is_storage_class_export(CX_StorageClass storage_class) { |
| 341 | | switch (storage_class) { |
| 342 | | case CX_SC_Invalid: |
| 343 | | zig_unreachable(); |
| 344 | | case CX_SC_None: |
| 345 | | case CX_SC_Extern: |
| 346 | | case CX_SC_Auto: |
| 347 | | return true; |
| 348 | | case CX_SC_Static: |
| 349 | | case CX_SC_PrivateExtern: |
| 350 | | case CX_SC_OpenCLWorkGroupLocal: |
| 351 | | case CX_SC_Register: |
| 352 | | return false; |
| 353 | | } |
| 354 | | zig_unreachable(); |
| 355 | | } |
| 356 | | |
| 357 | | static enum CXChildVisitResult visit_fn_children(CXCursor cursor, CXCursor parent, CXClientData client_data) { |
| 358 | | ParseH *p = (ParseH*)client_data; |
| 359 | | enum CXCursorKind kind = clang_getCursorKind(cursor); |
| 360 | | |
| 361 | | switch (kind) { |
| 362 | | case CXCursor_ParmDecl: |
| 363 | | { |
| 364 | | assert(p->cur_fn); |
| 365 | | assert(p->arg_index < p->cur_fn->arg_count); |
| 366 | | CXString name = clang_getCursorSpelling(cursor); |
| 367 | | Buf *arg_name = &p->cur_fn->args[p->arg_index].name; |
| 368 | | buf_init_from_str(arg_name, clang_getCString(name)); |
| 369 | | if (buf_len(arg_name) == 0) { |
| 370 | | buf_appendf(arg_name, "arg%d", p->arg_index); |
| 371 | | } |
| 372 | | |
| 373 | | p->arg_index += 1; |
| 374 | | return CXChildVisit_Continue; |
| 375 | | } |
| 376 | | default: |
| 377 | | return CXChildVisit_Recurse; |
| 378 | | } |
| 379 | | } |
| 380 | | |
| 381 | | static enum CXChildVisitResult visit_struct_children(CXCursor cursor, CXCursor parent, CXClientData client_data) { |
| 382 | | ParseH *p = (ParseH*)client_data; |
| 383 | | enum CXCursorKind kind = clang_getCursorKind(cursor); |
| 384 | | |
| 385 | | switch (kind) { |
| 386 | | case CXCursor_FieldDecl: |
| 387 | | { |
| 388 | | assert(p->cur_struct); |
| 389 | | CXString name = clang_getCursorSpelling(cursor); |
| 390 | | Field *field = allocate<Field>(1); |
| 391 | | buf_init_from_str(&field->name, clang_getCString(name)); |
| 392 | | CXType cursor_type = clang_getCursorType(cursor); |
| 393 | | field->type = to_zig_type(p, cursor_type); |
| 394 | | |
| 395 | | p->cur_struct->fields.append(field); |
| 396 | | |
| 397 | | return CXChildVisit_Continue; |
| 398 | | } |
| 399 | | default: |
| 400 | | return CXChildVisit_Recurse; |
| 401 | | } |
| 402 | | } |
| 403 | | |
| 404 | | static bool handle_struct_cursor(ParseH *p, CXCursor cursor, const char *name, bool expect_name) { |
| 405 | | p->cur_struct = allocate<Struct>(1); |
| 406 | | |
| 407 | | buf_init_from_str(&p->cur_struct->name, name); |
| 408 | | |
| 409 | | bool got_name = (buf_len(&p->cur_struct->name) != 0); |
| 410 | | if (expect_name != got_name) |
| 411 | | return false; |
| 412 | | |
| 413 | | clang_visitChildren(cursor, visit_struct_children, p); |
| 414 | | |
| 415 | | if (p->cur_struct->fields.length > 0) { |
| 416 | | p->struct_list.append(p->cur_struct); |
| 417 | | } else { |
| 418 | | p->incomplete_struct_list.append(p->cur_struct); |
| 419 | | } |
| 420 | | |
| 421 | | p->cur_struct = nullptr; |
| 422 | | |
| 423 | | return true; |
| 424 | | } |
| 425 | | |
| 426 | | |
| 427 | | static enum CXChildVisitResult fn_visitor(CXCursor cursor, CXCursor parent, CXClientData client_data) { |
| 428 | | ParseH *p = (ParseH*)client_data; |
| 429 | | enum CXCursorKind kind = clang_getCursorKind(cursor); |
| 430 | | CXString name = clang_getCursorSpelling(cursor); |
| 431 | | |
| 432 | | p->range = clang_getCursorExtent(cursor); |
| 433 | | p->location = clang_getRangeStart(p->range); |
| 434 | | |
| 435 | | switch (kind) { |
| 436 | | case CXCursor_FunctionDecl: |
| 437 | | { |
| 438 | | CX_StorageClass storage_class = clang_Cursor_getStorageClass(cursor); |
| 439 | | if (!is_storage_class_export(storage_class)) |
| 440 | | return CXChildVisit_Continue; |
| 441 | | |
| 442 | | CXType fn_type = clang_getCursorType(cursor); |
| 443 | | if (clang_getFunctionTypeCallingConv(fn_type) != CXCallingConv_C) { |
| 444 | | print_location(p); |
| 445 | | fprintf(stderr, "warning: skipping non c calling convention function, not yet supported\n"); |
| 446 | | return CXChildVisit_Continue; |
| 447 | | } |
| 448 | | |
| 449 | | assert(!p->cur_fn); |
| 450 | | p->cur_fn = allocate<Fn>(1); |
| 451 | | |
| 452 | | p->cur_fn->is_variadic = clang_isFunctionTypeVariadic(fn_type); |
| 453 | | |
| 454 | | CXType return_type = clang_getResultType(fn_type); |
| 455 | | if (!resolves_to_void(p, return_type)) { |
| 456 | | p->cur_fn->return_type = to_zig_type(p, return_type); |
| 457 | | } |
| 458 | | |
| 459 | | buf_init_from_str(&p->cur_fn->name, clang_getCString(name)); |
| 460 | | |
| 461 | | p->cur_fn->arg_count = clang_getNumArgTypes(fn_type); |
| 462 | | p->cur_fn->args = allocate<Arg>(p->cur_fn->arg_count); |
| 463 | | |
| 464 | | for (int i = 0; i < p->cur_fn->arg_count; i += 1) { |
| 465 | | CXType param_type = clang_getArgType(fn_type, i); |
| 466 | | p->cur_fn->args[i].type = to_zig_type(p, param_type); |
| 467 | | } |
| 468 | | |
| 469 | | p->arg_index = 0; |
| 470 | | |
| 471 | | clang_visitChildren(cursor, visit_fn_children, p); |
| 472 | | |
| 473 | | p->fn_list.append(p->cur_fn); |
| 474 | | p->cur_fn = nullptr; |
| 475 | | |
| 476 | | return CXChildVisit_Recurse; |
| 477 | | } |
| 478 | | case CXCursor_CompoundStmt: |
| 479 | | case CXCursor_FieldDecl: |
| 480 | | case CXCursor_TypedefDecl: |
| 481 | | { |
| 482 | | CXType underlying_type = clang_getTypedefDeclUnderlyingType(cursor); |
| 483 | | |
| 484 | | if (resolves_to_void(p, underlying_type)) { |
| 485 | | return CXChildVisit_Continue; |
| 486 | | } |
| 487 | | |
| 488 | | if (underlying_type.kind == CXType_Unexposed) { |
| 489 | | underlying_type = clang_getCanonicalType(underlying_type); |
| 490 | | } |
| 491 | | bool skip_typedef; |
| 492 | | if (underlying_type.kind == CXType_Unexposed) { |
| 493 | | fprintf(stderr, "warning: unexposed type\n"); |
| 494 | | print_location(p); |
| 495 | | skip_typedef = true; |
| 496 | | } else if (underlying_type.kind == CXType_Record) { |
| 497 | | CXCursor decl_cursor = clang_getTypeDeclaration(underlying_type); |
| 498 | | skip_typedef = handle_struct_cursor(p, decl_cursor, clang_getCString(name), false); |
| 499 | | } else if (underlying_type.kind == CXType_Invalid) { |
| 500 | | fprintf(stderr, "warning: invalid type\n"); |
| 501 | | print_location(p); |
| 502 | | skip_typedef = true; |
| 503 | | } else { |
| 504 | | skip_typedef = false; |
| 505 | | } |
| 506 | | |
| 507 | | CXType typedef_type = clang_getCursorType(cursor); |
| 508 | | const char *name_str = prefixes_stripped(typedef_type); |
| 509 | | if (!skip_typedef && c_to_zig_name(name_str)) { |
| 510 | | skip_typedef = true; |
| 511 | | } |
| 512 | | |
| 513 | | if (!skip_typedef) { |
| 514 | | TypeDef *type_def = allocate<TypeDef>(1); |
| 515 | | buf_init_from_str(&type_def->alias, name_str); |
| 516 | | buf_init_from_buf(&type_def->target, to_zig_type(p, underlying_type)); |
| 517 | | p->type_def_list.append(type_def); |
| 518 | | } |
| 519 | | |
| 520 | | return CXChildVisit_Continue; |
| 521 | | } |
| 522 | | case CXCursor_StructDecl: |
| 523 | | { |
| 524 | | handle_struct_cursor(p, cursor, clang_getCString(name), true); |
| 525 | | |
| 526 | | return CXChildVisit_Continue; |
| 527 | | } |
| 528 | | default: |
| 529 | | return CXChildVisit_Recurse; |
| 530 | | } |
| 531 | | } |
| 532 | | |
| 533 | | static void print_indent(ParseH *p) { |
| 534 | | for (int i = 0; i < p->cur_indent; i += 1) { |
| 535 | | fprintf(p->f, " "); |
| 536 | | } |
| 537 | | } |
| 538 | | |
| 539 | | void parse_h_file(const char *target_path, ZigList<const char *> *clang_argv, FILE *f) { |
| 540 | | ParseH parse_h = {0}; |
| 541 | | ParseH *p = &parse_h; |
| 542 | | p->f = f; |
| 543 | | CXIndex index = clang_createIndex(1, 0); |
| 544 | | |
| 545 | | char *ZIG_PARSEH_CFLAGS = getenv("ZIG_PARSEH_CFLAGS"); |
| 546 | | if (ZIG_PARSEH_CFLAGS) { |
| 547 | | Buf tmp_buf = BUF_INIT; |
| 548 | | char *start = ZIG_PARSEH_CFLAGS; |
| 549 | | char *space = strstr(start, " "); |
| 550 | | while (space) { |
| 551 | | if (space - start > 0) { |
| 552 | | buf_init_from_mem(&tmp_buf, start, space - start); |
| 553 | | clang_argv->append(buf_ptr(buf_create_from_buf(&tmp_buf))); |
| 554 | | } |
| 555 | | start = space + 1; |
| 556 | | space = strstr(start, " "); |
| 557 | | } |
| 558 | | buf_init_from_str(&tmp_buf, start); |
| 559 | | clang_argv->append(buf_ptr(buf_create_from_buf(&tmp_buf))); |
| 560 | | } |
| 561 | | clang_argv->append("-isystem"); |
| 562 | | clang_argv->append(ZIG_HEADERS_DIR); |
| 563 | | |
| 564 | | clang_argv->append(nullptr); |
| 565 | | |
| 566 | | enum CXErrorCode err_code; |
| 567 | | if ((err_code = clang_parseTranslationUnit2(index, target_path, |
| 568 | | clang_argv->items, clang_argv->length - 1, |
| 569 | | NULL, 0, CXTranslationUnit_None, &p->tu))) |
| 570 | | { |
| 571 | | zig_panic("parse translation unit failure"); |
| 572 | | } |
| 573 | | |
| 574 | | |
| 575 | | unsigned diag_count = clang_getNumDiagnostics(p->tu); |
| 576 | | |
| 577 | | if (diag_count > 0) { |
| 578 | | for (unsigned i = 0; i < diag_count; i += 1) { |
| 579 | | CXDiagnostic diagnostic = clang_getDiagnostic(p->tu, i); |
| 580 | | CXSourceLocation location = clang_getDiagnosticLocation(diagnostic); |
| 581 | | |
| 582 | | CXFile file; |
| 583 | | unsigned line, column, offset; |
| 584 | | clang_getSpellingLocation(location, &file, &line, &column, &offset); |
| 585 | | CXString text = clang_getDiagnosticSpelling(diagnostic); |
| 586 | | CXString file_name = clang_getFileName(file); |
| 587 | | fprintf(stderr, "%s line %u, column %u: %s\n", clang_getCString(file_name), |
| 588 | | line, column, clang_getCString(text)); |
| 589 | | } |
| 590 | | |
| 591 | | exit(1); |
| 592 | | } |
| 593 | | |
| 594 | | |
| 595 | | CXCursor cursor = clang_getTranslationUnitCursor(p->tu); |
| 596 | | clang_visitChildren(cursor, fn_visitor, p); |
| 597 | | |
| 598 | | for (int struct_i = 0; struct_i < p->struct_list.length; struct_i += 1) { |
| 599 | | Struct *struc = p->struct_list.at(struct_i); |
| 600 | | fprintf(f, "struct %s {\n", buf_ptr(&struc->name)); |
| 601 | | p->cur_indent += indent_size; |
| 602 | | for (int field_i = 0; field_i < struc->fields.length; field_i += 1) { |
| 603 | | Field *field = struc->fields.at(field_i); |
| 604 | | print_indent(p); |
| 605 | | fprintf(f, "%s: %s,\n", buf_ptr(&field->name), buf_ptr(field->type)); |
| 606 | | } |
| 607 | | |
| 608 | | p->cur_indent -= indent_size; |
| 609 | | fprintf(f, "}\n\n"); |
| 610 | | } |
| 611 | | |
| 612 | | int total_typedef_count = p->type_def_list.length; |
| 613 | | for (int i = 0; i < p->incomplete_struct_list.length; i += 1) { |
| 614 | | Struct *struc = p->incomplete_struct_list.at(i); |
| 615 | | struc->have_def = have_struct_def(p, &struc->name); |
| 616 | | total_typedef_count += (int)!struc->have_def; |
| 617 | | } |
| 618 | | |
| 619 | | if (total_typedef_count) { |
| 620 | | for (int i = 0; i < p->incomplete_struct_list.length; i += 1) { |
| 621 | | Struct *struc = p->incomplete_struct_list.at(i); |
| 622 | | if (struc->have_def) |
| 623 | | continue; |
| 624 | | |
| 625 | | fprintf(f, "struct %s;\n", buf_ptr(&struc->name)); |
| 626 | | } |
| 627 | | |
| 628 | | for (int type_def_i = 0; type_def_i < p->type_def_list.length; type_def_i += 1) { |
| 629 | | TypeDef *type_def = p->type_def_list.at(type_def_i); |
| 630 | | fprintf(f, "type %s = %s;\n", buf_ptr(&type_def->alias), buf_ptr(&type_def->target)); |
| 631 | | } |
| 632 | | |
| 633 | | fprintf(f, "\n"); |
| 634 | | } |
| 635 | | |
| 636 | | if (p->fn_list.length) { |
| 637 | | fprintf(f, "extern {\n"); |
| 638 | | p->cur_indent += indent_size; |
| 639 | | for (int fn_i = 0; fn_i < p->fn_list.length; fn_i += 1) { |
| 640 | | Fn *fn = p->fn_list.at(fn_i); |
| 641 | | print_indent(p); |
| 642 | | fprintf(p->f, "fn %s(", buf_ptr(&fn->name)); |
| 643 | | for (int arg_i = 0; arg_i < fn->arg_count; arg_i += 1) { |
| 644 | | Arg *arg = &fn->args[arg_i]; |
| 645 | | fprintf(p->f, "%s: %s", buf_ptr(&arg->name), buf_ptr(arg->type)); |
| 646 | | if (arg_i + 1 < fn->arg_count || fn->is_variadic) { |
| 647 | | fprintf(p->f, ", "); |
| 648 | | } |
| 649 | | } |
| 650 | | if (fn->is_variadic) { |
| 651 | | fprintf(p->f, "..."); |
| 652 | | } |
| 653 | | fprintf(p->f, ")"); |
| 654 | | if (fn->return_type) { |
| 655 | | fprintf(p->f, " -> %s", buf_ptr(fn->return_type)); |
| 656 | | } |
| 657 | | fprintf(p->f, ";\n"); |
| 658 | | } |
| 659 | | p->cur_indent -= indent_size; |
| 660 | | fprintf(f, "}\n"); |
| 661 | | } |
| 662 | | } |