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...@@ -109,9 +109,6 @@ zig | C equivalent | Description
109 f128 | long double | 128-bit IEE754 floating point109 f128 | long double | 128-bit IEE754 floating point
110 isize | intptr_t | signed pointer sized integer110 isize | intptr_t | signed pointer sized integer
111 usize | uintptr_t | unsigned pointer sized integer111 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
115 c_short | short | for API compatibility with C112 c_short | short | for API compatibility with C
116 c_ushort | unsigned short | for API compatibility with C113 c_ushort | unsigned short | for API compatibility with C
117 c_int | int | for API compatibility with C114 c_int | int | for API compatibility with C
src/parseh.cpp+69-21
...@@ -58,6 +58,54 @@ struct ParseH {...@@ -58,6 +58,54 @@ struct ParseH {
5858
59static const int indent_size = 4;59static 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
61static bool have_struct_def(ParseH *p, Buf *name) {109static 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}
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
71static bool str_has_prefix(const char *str, const char *prefix) {128static 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 }
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
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 }