authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-09 01:23:46-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-09 01:23:58-07:00
log7b181b51adfdd12d6ecd15160c8bb80bde6883ac
tree97a16c39792768b022fd0f2c2916090df56c7248
parent6a48c007a62bca5ba712e92b1f1e32031e43717c

parseh: cleanup types


2 files changed, 69 insertions(+), 24 deletions(-)

README.md-3
......@@ -109,9 +109,6 @@ zig | C equivalent | Description
109109 f128 | long double | 128-bit IEE754 floating point
110110 isize | intptr_t | signed pointer sized integer
111111 usize | uintptr_t | unsigned pointer sized integer
112 c_char | char | for API compatibility with C
113 c_schar | signed char | for API compatibility with C
114 c_uchar | unsigned char | for API compatibility with C
115112 c_short | short | for API compatibility with C
116113 c_ushort | unsigned short | for API compatibility with C
117114 c_int | int | for API compatibility with C
src/parseh.cpp+69-21
......@@ -58,6 +58,54 @@ struct ParseH {
5858
5959static const int indent_size = 4;
6060
61struct TypeMapping {
62 const char *c_name;
63 const char *zig_name;
64};
65
66static 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
61109static bool have_struct_def(ParseH *p, Buf *name) {
62110 for (int i = 0; i < p->struct_list.length; i += 1) {
63111 Struct *struc = p->struct_list.at(i);
......@@ -68,6 +116,15 @@ static bool have_struct_def(ParseH *p, Buf *name) {
68116 return false;
69117}
70118
119static 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
71128static bool str_has_prefix(const char *str, const char *prefix) {
72129 while (*prefix) {
73130 if (*str && *str == *prefix) {
......@@ -146,12 +203,11 @@ static Buf *to_zig_type(ParseH *p, CXType raw_type) {
146203 case CXType_Bool:
147204 return buf_create_from_str("bool");
148205 case CXType_SChar:
149 return buf_create_from_str("c_schar");
206 return buf_create_from_str("i8");
150207 case CXType_UChar:
151 return buf_create_from_str("c_uchar");
152208 case CXType_Char_U:
153209 case CXType_Char_S:
154 return buf_create_from_str("c_char");
210 return buf_create_from_str("u8");
155211 case CXType_WChar:
156212 print_location(p);
157213 zig_panic("TODO wchar");
......@@ -227,22 +283,9 @@ static Buf *to_zig_type(ParseH *p, CXType raw_type) {
227283 case CXType_Typedef:
228284 {
229285 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);
246289 } else {
247290 CXCursor typedef_cursor = clang_getTypeDeclaration(raw_type);
248291 CXType underlying_type = clang_getTypedefDeclUnderlyingType(typedef_cursor);
......@@ -461,10 +504,15 @@ static enum CXChildVisitResult fn_visitor(CXCursor cursor, CXCursor parent, CXCl
461504 skip_typedef = false;
462505 }
463506
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
464513 if (!skip_typedef) {
465 CXType typedef_type = clang_getCursorType(cursor);
466514 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);
468516 buf_init_from_buf(&type_def->target, to_zig_type(p, underlying_type));
469517 p->type_def_list.append(type_def);
470518 }