| ... | ... | @@ -58,6 +58,54 @@ struct ParseH { |
| 58 | 58 | |
| 59 | 59 | static const int indent_size = 4; |
| 60 | 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 | |
| 61 | 109 | static bool have_struct_def(ParseH *p, Buf *name) { |
| 62 | 110 | for (int i = 0; i < p->struct_list.length; i += 1) { |
| 63 | 111 | Struct *struc = p->struct_list.at(i); |
| ... | ... | @@ -68,6 +116,15 @@ static bool have_struct_def(ParseH *p, Buf *name) { |
| 68 | 116 | return false; |
| 69 | 117 | } |
| 70 | 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 | |
| 71 | 128 | static bool str_has_prefix(const char *str, const char *prefix) { |
| 72 | 129 | while (*prefix) { |
| 73 | 130 | if (*str && *str == *prefix) { |
| ... | ... | @@ -146,12 +203,11 @@ static Buf *to_zig_type(ParseH *p, CXType raw_type) { |
| 146 | 203 | case CXType_Bool: |
| 147 | 204 | return buf_create_from_str("bool"); |
| 148 | 205 | case CXType_SChar: |
| 149 | | return buf_create_from_str("c_schar"); |
| 206 | return buf_create_from_str("i8"); |
| 150 | 207 | case CXType_UChar: |
| 151 | | return buf_create_from_str("c_uchar"); |
| 152 | 208 | case CXType_Char_U: |
| 153 | 209 | case CXType_Char_S: |
| 154 | | return buf_create_from_str("c_char"); |
| 210 | return buf_create_from_str("u8"); |
| 155 | 211 | case CXType_WChar: |
| 156 | 212 | print_location(p); |
| 157 | 213 | zig_panic("TODO wchar"); |
| ... | ... | @@ -227,22 +283,9 @@ static Buf *to_zig_type(ParseH *p, CXType raw_type) { |
| 227 | 283 | case CXType_Typedef: |
| 228 | 284 | { |
| 229 | 285 | const char *name = prefixes_stripped(raw_type); |
| 230 | | if (strcmp(name, "int8_t") == 0) { |
| 231 | | return buf_create_from_str("i8"); |
| 232 | | } else if (strcmp(name, "uint8_t") == 0) { |
| 233 | | return buf_create_from_str("u8"); |
| 234 | | } else if (strcmp(name, "uint16_t") == 0) { |
| 235 | | return buf_create_from_str("u16"); |
| 236 | | } else if (strcmp(name, "uint32_t") == 0) { |
| 237 | | return buf_create_from_str("u32"); |
| 238 | | } else if (strcmp(name, "uint64_t") == 0) { |
| 239 | | return buf_create_from_str("u64"); |
| 240 | | } else if (strcmp(name, "int16_t") == 0) { |
| 241 | | return buf_create_from_str("i16"); |
| 242 | | } else if (strcmp(name, "int32_t") == 0) { |
| 243 | | return buf_create_from_str("i32"); |
| 244 | | } else if (strcmp(name, "int64_t") == 0) { |
| 245 | | return buf_create_from_str("i64"); |
| 286 | const char *zig_name = c_to_zig_name(name); |
| 287 | if (zig_name) { |
| 288 | return buf_create_from_str(zig_name); |
| 246 | 289 | } else { |
| 247 | 290 | CXCursor typedef_cursor = clang_getTypeDeclaration(raw_type); |
| 248 | 291 | CXType underlying_type = clang_getTypedefDeclUnderlyingType(typedef_cursor); |
| ... | ... | @@ -461,10 +504,15 @@ static enum CXChildVisitResult fn_visitor(CXCursor cursor, CXCursor parent, CXCl |
| 461 | 504 | skip_typedef = false; |
| 462 | 505 | } |
| 463 | 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 | |
| 464 | 513 | if (!skip_typedef) { |
| 465 | | CXType typedef_type = clang_getCursorType(cursor); |
| 466 | 514 | TypeDef *type_def = allocate<TypeDef>(1); |
| 467 | | buf_init_from_str(&type_def->alias, prefixes_stripped(typedef_type)); |
| 515 | buf_init_from_str(&type_def->alias, name_str); |
| 468 | 516 | buf_init_from_buf(&type_def->target, to_zig_type(p, underlying_type)); |
| 469 | 517 | p->type_def_list.append(type_def); |
| 470 | 518 | } |