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