| ... | ... | @@ -22,6 +22,8 @@ struct ParseH { |
| 22 | 22 | Fn *cur_fn; |
| 23 | 23 | int arg_index; |
| 24 | 24 | int cur_indent; |
| 25 | CXSourceRange range; |
| 26 | CXSourceLocation location; |
| 25 | 27 | }; |
| 26 | 28 | |
| 27 | 29 | static const int indent_size = 4; |
| ... | ... | @@ -60,11 +62,20 @@ start_over: |
| 60 | 62 | return c_name; |
| 61 | 63 | } |
| 62 | 64 | |
| 63 | | static Buf *to_zig_type(CXType raw_type) { |
| 65 | static void print_location(ParseH *p) { |
| 66 | CXFile file; |
| 67 | unsigned line, column, offset; |
| 68 | clang_getFileLocation(p->location, &file, &line, &column, &offset); |
| 69 | CXString file_name = clang_getFileName(file); |
| 70 | |
| 71 | fprintf(stderr, "%s line %u, column %u\n", clang_getCString(file_name), line, column); |
| 72 | } |
| 73 | |
| 74 | static Buf *to_zig_type(ParseH *p, CXType raw_type) { |
| 64 | 75 | if (raw_type.kind == CXType_Unexposed) { |
| 65 | 76 | CXType canonical = clang_getCanonicalType(raw_type); |
| 66 | 77 | if (canonical.kind != CXType_Unexposed) |
| 67 | | return to_zig_type(canonical); |
| 78 | return to_zig_type(p, canonical); |
| 68 | 79 | else |
| 69 | 80 | zig_panic("clang C api insufficient"); |
| 70 | 81 | } |
| ... | ... | @@ -83,11 +94,14 @@ static Buf *to_zig_type(CXType raw_type) { |
| 83 | 94 | case CXType_UChar: |
| 84 | 95 | return buf_create_from_str("u8"); |
| 85 | 96 | case CXType_WChar: |
| 86 | | zig_panic("TODO"); |
| 97 | print_location(p); |
| 98 | zig_panic("TODO wchar"); |
| 87 | 99 | case CXType_Char16: |
| 88 | | zig_panic("TODO"); |
| 100 | print_location(p); |
| 101 | zig_panic("TODO char16"); |
| 89 | 102 | case CXType_Char32: |
| 90 | | zig_panic("TODO"); |
| 103 | print_location(p); |
| 104 | zig_panic("TODO char32"); |
| 91 | 105 | case CXType_UShort: |
| 92 | 106 | return buf_create_from_str("c_ushort"); |
| 93 | 107 | case CXType_UInt: |
| ... | ... | @@ -97,7 +111,8 @@ static Buf *to_zig_type(CXType raw_type) { |
| 97 | 111 | case CXType_ULongLong: |
| 98 | 112 | return buf_create_from_str("c_ulonglong"); |
| 99 | 113 | case CXType_UInt128: |
| 100 | | zig_panic("TODO"); |
| 114 | print_location(p); |
| 115 | zig_panic("TODO uint128"); |
| 101 | 116 | case CXType_Short: |
| 102 | 117 | return buf_create_from_str("c_short"); |
| 103 | 118 | case CXType_Int: |
| ... | ... | @@ -107,43 +122,34 @@ static Buf *to_zig_type(CXType raw_type) { |
| 107 | 122 | case CXType_LongLong: |
| 108 | 123 | return buf_create_from_str("c_longlong"); |
| 109 | 124 | case CXType_Int128: |
| 110 | | zig_panic("TODO"); |
| 125 | print_location(p); |
| 126 | zig_panic("TODO int128"); |
| 111 | 127 | case CXType_Float: |
| 112 | 128 | return buf_create_from_str("f32"); |
| 113 | 129 | case CXType_Double: |
| 114 | 130 | return buf_create_from_str("f64"); |
| 115 | 131 | case CXType_LongDouble: |
| 116 | 132 | return buf_create_from_str("f128"); |
| 117 | | case CXType_NullPtr: |
| 118 | | zig_panic("TODO"); |
| 119 | | case CXType_Overload: |
| 120 | | zig_panic("TODO"); |
| 121 | | case CXType_Dependent: |
| 122 | | zig_panic("TODO"); |
| 123 | | case CXType_ObjCId: |
| 124 | | zig_panic("TODO"); |
| 125 | | case CXType_ObjCClass: |
| 126 | | zig_panic("TODO"); |
| 127 | | case CXType_ObjCSel: |
| 128 | | zig_panic("TODO"); |
| 129 | | case CXType_Complex: |
| 130 | | zig_panic("TODO"); |
| 133 | case CXType_IncompleteArray: |
| 134 | { |
| 135 | CXType pointee_type = clang_getArrayElementType(raw_type); |
| 136 | Buf *pointee_buf = to_zig_type(p, pointee_type); |
| 137 | if (clang_isConstQualifiedType(pointee_type)) { |
| 138 | return buf_sprintf("*const %s", buf_ptr(pointee_buf)); |
| 139 | } else { |
| 140 | return buf_sprintf("*mut %s", buf_ptr(pointee_buf)); |
| 141 | } |
| 142 | } |
| 131 | 143 | case CXType_Pointer: |
| 132 | 144 | { |
| 133 | 145 | CXType pointee_type = clang_getPointeeType(raw_type); |
| 134 | | Buf *pointee_buf = to_zig_type(pointee_type); |
| 146 | Buf *pointee_buf = to_zig_type(p, pointee_type); |
| 135 | 147 | if (clang_isConstQualifiedType(pointee_type)) { |
| 136 | 148 | return buf_sprintf("*const %s", buf_ptr(pointee_buf)); |
| 137 | 149 | } else { |
| 138 | 150 | return buf_sprintf("*mut %s", buf_ptr(pointee_buf)); |
| 139 | 151 | } |
| 140 | 152 | } |
| 141 | | case CXType_BlockPointer: |
| 142 | | zig_panic("TODO"); |
| 143 | | case CXType_LValueReference: |
| 144 | | zig_panic("TODO"); |
| 145 | | case CXType_RValueReference: |
| 146 | | zig_panic("TODO"); |
| 147 | 153 | case CXType_Record: |
| 148 | 154 | { |
| 149 | 155 | const char *name = prefixes_stripped(raw_type); |
| ... | ... | @@ -176,28 +182,44 @@ static Buf *to_zig_type(CXType raw_type) { |
| 176 | 182 | } else { |
| 177 | 183 | CXCursor typedef_cursor = clang_getTypeDeclaration(raw_type); |
| 178 | 184 | CXType underlying_type = clang_getTypedefDeclUnderlyingType(typedef_cursor); |
| 179 | | return to_zig_type(underlying_type); |
| 185 | return to_zig_type(p, underlying_type); |
| 180 | 186 | } |
| 181 | 187 | } |
| 182 | | case CXType_ObjCInterface: |
| 183 | | zig_panic("TODO"); |
| 184 | | case CXType_ObjCObjectPointer: |
| 185 | | zig_panic("TODO"); |
| 186 | | case CXType_FunctionNoProto: |
| 187 | | zig_panic("TODO"); |
| 188 | | case CXType_FunctionProto: |
| 189 | | zig_panic("TODO"); |
| 190 | 188 | case CXType_ConstantArray: |
| 191 | | zig_panic("TODO"); |
| 189 | { |
| 190 | CXType child_type = clang_getArrayElementType(raw_type); |
| 191 | Buf *zig_child_type = to_zig_type(p, child_type); |
| 192 | long size = (long)clang_getArraySize(raw_type); |
| 193 | return buf_sprintf("[%s; %ld]", buf_ptr(zig_child_type), size); |
| 194 | } |
| 195 | case CXType_FunctionProto: |
| 196 | fprintf(stderr, "warning: TODO function proto\n"); |
| 197 | print_location(p); |
| 198 | return buf_create_from_str("*const u8"); |
| 199 | case CXType_FunctionNoProto: |
| 200 | print_location(p); |
| 201 | zig_panic("TODO function no proto"); |
| 202 | case CXType_BlockPointer: |
| 203 | print_location(p); |
| 204 | zig_panic("TODO block pointer"); |
| 192 | 205 | case CXType_Vector: |
| 193 | | zig_panic("TODO"); |
| 194 | | case CXType_IncompleteArray: |
| 195 | | zig_panic("TODO"); |
| 206 | print_location(p); |
| 207 | zig_panic("TODO vector"); |
| 208 | case CXType_LValueReference: |
| 209 | case CXType_RValueReference: |
| 196 | 210 | case CXType_VariableArray: |
| 197 | | zig_panic("TODO"); |
| 198 | 211 | case CXType_DependentSizedArray: |
| 199 | | zig_panic("TODO"); |
| 200 | 212 | case CXType_MemberPointer: |
| 213 | case CXType_ObjCInterface: |
| 214 | case CXType_ObjCObjectPointer: |
| 215 | case CXType_NullPtr: |
| 216 | case CXType_Overload: |
| 217 | case CXType_Dependent: |
| 218 | case CXType_ObjCId: |
| 219 | case CXType_ObjCClass: |
| 220 | case CXType_ObjCSel: |
| 221 | case CXType_Complex: |
| 222 | print_location(p); |
| 201 | 223 | zig_panic("TODO"); |
| 202 | 224 | } |
| 203 | 225 | |
| ... | ... | @@ -238,6 +260,9 @@ static enum CXChildVisitResult fn_visitor(CXCursor cursor, CXCursor parent, CXCl |
| 238 | 260 | enum CXCursorKind kind = clang_getCursorKind(cursor); |
| 239 | 261 | CXString name = clang_getCursorSpelling(cursor); |
| 240 | 262 | |
| 263 | p->range = clang_getCursorExtent(cursor); |
| 264 | p->location = clang_getRangeStart(p->range); |
| 265 | |
| 241 | 266 | switch (kind) { |
| 242 | 267 | case CXCursor_FunctionDecl: |
| 243 | 268 | { |
| ... | ... | @@ -245,27 +270,37 @@ static enum CXChildVisitResult fn_visitor(CXCursor cursor, CXCursor parent, CXCl |
| 245 | 270 | if (!is_storage_class_export(storage_class)) |
| 246 | 271 | return CXChildVisit_Continue; |
| 247 | 272 | |
| 248 | | end_fn(p); |
| 249 | | begin_fn(p); |
| 250 | | |
| 251 | 273 | CXType fn_type = clang_getCursorType(cursor); |
| 252 | 274 | if (clang_isFunctionTypeVariadic(fn_type)) { |
| 253 | | zig_panic("TODO support variadic function"); |
| 275 | print_location(p); |
| 276 | fprintf(stderr, "warning: skipping variadic function, not yet supported\n"); |
| 277 | return CXChildVisit_Continue; |
| 254 | 278 | } |
| 255 | 279 | if (clang_getFunctionTypeCallingConv(fn_type) != CXCallingConv_C) { |
| 256 | | zig_panic("TODO support non c calling convention"); |
| 280 | print_location(p); |
| 281 | fprintf(stderr, "warning: skipping non c calling convention function, not yet supported\n"); |
| 282 | return CXChildVisit_Continue; |
| 257 | 283 | } |
| 284 | |
| 285 | end_fn(p); |
| 286 | begin_fn(p); |
| 287 | |
| 258 | 288 | CXType return_type = clang_getResultType(fn_type); |
| 259 | | p->cur_fn->return_type = to_zig_type(return_type); |
| 289 | p->cur_fn->return_type = to_zig_type(p, return_type); |
| 260 | 290 | |
| 261 | 291 | buf_init_from_str(&p->cur_fn->name, clang_getCString(name)); |
| 262 | 292 | |
| 293 | if (buf_eql_str(&p->cur_fn->name, "exit")) { |
| 294 | print_location(p); |
| 295 | zig_panic("Found it"); |
| 296 | } |
| 297 | |
| 263 | 298 | p->cur_fn->arg_count = clang_getNumArgTypes(fn_type); |
| 264 | 299 | p->cur_fn->args = allocate<Arg>(p->cur_fn->arg_count); |
| 265 | 300 | |
| 266 | 301 | for (int i = 0; i < p->cur_fn->arg_count; i += 1) { |
| 267 | 302 | CXType param_type = clang_getArgType(fn_type, i); |
| 268 | | p->cur_fn->args[i].type = to_zig_type(param_type); |
| 303 | p->cur_fn->args[i].type = to_zig_type(p, param_type); |
| 269 | 304 | } |
| 270 | 305 | |
| 271 | 306 | p->arg_index = 0; |
| ... | ... | @@ -283,6 +318,7 @@ static enum CXChildVisitResult fn_visitor(CXCursor cursor, CXCursor parent, CXCl |
| 283 | 318 | case CXCursor_UnexposedAttr: |
| 284 | 319 | case CXCursor_CompoundStmt: |
| 285 | 320 | case CXCursor_FieldDecl: |
| 321 | case CXCursor_TypedefDecl: |
| 286 | 322 | return CXChildVisit_Continue; |
| 287 | 323 | default: |
| 288 | 324 | return CXChildVisit_Recurse; |