| author | |
| committer | |
| log | fceedada5c18ca5e9fa3ca14e74f6722eda85600 |
| tree | ac3ef0bca2923b7962eb08cd948c4e5806dda993 |
| parent | 21ed9391179a9e472b25c80eaec5eebc9d08aca8 |
| signature | Commit is signed but in an unrecognized format. |
4 files changed, 106 insertions(+), 2 deletions(-)
src-self-hosted/clang.zig+5| ... | ... | @@ -864,3 +864,8 @@ pub const ZigClangCompoundStmt_const_body_iterator = [*c]const *struct_ZigClangS |
| 864 | 864 | |
| 865 | 865 | pub extern fn ZigClangCompoundStmt_body_begin(self: *const ZigClangCompoundStmt) ZigClangCompoundStmt_const_body_iterator; |
| 866 | 866 | pub extern fn ZigClangCompoundStmt_body_end(self: *const ZigClangCompoundStmt) ZigClangCompoundStmt_const_body_iterator; |
| 867 | ||
| 868 | pub const ZigClangDeclStmt_const_decl_iterator = [*c]const *struct_ZigClangDecl; | |
| 869 | ||
| 870 | pub extern fn ZigClangDeclStmt_decl_begin(self: *const ZigClangDeclStmt) ZigClangDeclStmt_const_decl_iterator; | |
| 871 | pub extern fn ZigClangDeclStmt_decl_end(self: *const ZigClangDeclStmt) ZigClangDeclStmt_const_decl_iterator; |
src-self-hosted/translate_c.zig+79| ... | ... | @@ -323,6 +323,7 @@ fn transStmt( |
| 323 | 323 | const sc = ZigClangStmt_getStmtClass(stmt); |
| 324 | 324 | switch (sc) { |
| 325 | 325 | .CompoundStmtClass => return transCompoundStmt(rp, scope, @ptrCast(*const ZigClangCompoundStmt, stmt)), |
| 326 | .DeclStmtClass => return transDeclStmt(rp, scope, @ptrCast(*const ZigClangDeclStmt, stmt)), | |
| 326 | 327 | else => { |
| 327 | 328 | return revertAndWarn( |
| 328 | 329 | rp, |
| ... | ... | @@ -368,6 +369,84 @@ fn transCompoundStmt(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangCompo |
| 368 | 369 | }; |
| 369 | 370 | } |
| 370 | 371 | |
| 372 | fn transDeclStmt(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangDeclStmt) !TransResult { | |
| 373 | const c = rp.c; | |
| 374 | const block_scope = findBlockScope(scope); | |
| 375 | var it = ZigClangDeclStmt_decl_begin(stmt); | |
| 376 | const end_it = ZigClangDeclStmt_decl_end(stmt); | |
| 377 | while (it != end_it) : (it += 1) { | |
| 378 | switch (ZigClangDecl_getKind(it.*)) { | |
| 379 | .Var => { | |
| 380 | const var_decl = @ptrCast(*const ZigClangVarDecl, it.*); | |
| 381 | ||
| 382 | // TODO: | |
| 383 | // const thread_local_token = if (ZigClangVarDecl_getTLSKind() == .None) | |
| 384 | // null | |
| 385 | // else | |
| 386 | // try appendToken(c, .Keyword_threadlocal, "threadlocal"); | |
| 387 | const thread_local_token: ?ast.TokenIndex = null; | |
| 388 | // TODO: | |
| 389 | // const mut_token = if (ZigClangQualType_isConstQualified(qual_type)) | |
| 390 | // try appendToken(c, .Keyword_const, "const") | |
| 391 | // else | |
| 392 | // try appendToken(c, .Keyword_var, "var"); | |
| 393 | const mut_token = try appendToken(c, .Keyword_var, "var"); | |
| 394 | const name_token = blk: { | |
| 395 | const name = try c.str(ZigClangDecl_getName_bytes_begin( | |
| 396 | @ptrCast(*const ZigClangDecl, var_decl), | |
| 397 | )); | |
| 398 | break :blk try appendToken(c, .Identifier, name); | |
| 399 | }; | |
| 400 | const eq_token = try appendToken(c, .Equal, "="); | |
| 401 | // TODO: init_node | |
| 402 | const init_node: ?*ast.Node = null; | |
| 403 | const semicolon_token = try appendToken(c, .Semicolon, ";"); | |
| 404 | ||
| 405 | const node = try rp.c.a().create(ast.Node.VarDecl); | |
| 406 | node.* = ast.Node.VarDecl{ | |
| 407 | .base = ast.Node{ .id = .VarDecl }, | |
| 408 | .doc_comments = null, | |
| 409 | .visib_token = null, | |
| 410 | .thread_local_token = thread_local_token, | |
| 411 | .name_token = name_token, | |
| 412 | .eq_token = eq_token, | |
| 413 | .mut_token = mut_token, | |
| 414 | .comptime_token = null, // TODO IsConstexpr ? | |
| 415 | .extern_export_token = null, // TODO ?TokenIndex, | |
| 416 | .lib_name = null, // TODO ?*Node, | |
| 417 | .type_node = null, // TODO ?*Node, | |
| 418 | .align_node = null, // TODO ?*Node, | |
| 419 | .section_node = null, // TODO ?*Node, | |
| 420 | .init_node = init_node, | |
| 421 | .semicolon_token = semicolon_token, | |
| 422 | }; | |
| 423 | try block_scope.block_node.statements.push(&node.base); | |
| 424 | }, | |
| 425 | ||
| 426 | else => |kind| return revertAndWarn( | |
| 427 | rp, | |
| 428 | error.UnsupportedTranslation, | |
| 429 | ZigClangStmt_getBeginLoc(@ptrCast(*const ZigClangStmt, stmt)), | |
| 430 | "TODO implement translation of DeclStmt kind {}", | |
| 431 | @tagName(kind), | |
| 432 | ), | |
| 433 | } | |
| 434 | } | |
| 435 | ||
| 436 | return TransResult{ | |
| 437 | .node = &block_scope.block_node.base, | |
| 438 | .node_scope = scope, | |
| 439 | .child_scope = scope, | |
| 440 | }; | |
| 441 | } | |
| 442 | ||
| 443 | fn findBlockScope(inner: *Scope) *Scope.Block { | |
| 444 | var scope = inner; | |
| 445 | while (true) : (scope = scope.parent orelse unreachable) { | |
| 446 | if (scope.id == .Block) return @fieldParentPtr(Scope.Block, "base", scope); | |
| 447 | } | |
| 448 | } | |
| 449 | ||
| 371 | 450 | fn addTopLevelDecl(c: *Context, name: []const u8, decl_node: *ast.Node) !void { |
| 372 | 451 | try c.tree.root_node.decls.push(decl_node); |
| 373 | 452 | } |
src/zig_clang.cpp+17-1| ... | ... | @@ -1331,6 +1331,12 @@ static ZigClangCompoundStmt_const_body_iterator bitcast(clang::CompoundStmt::con |
| 1331 | 1331 | return dest; |
| 1332 | 1332 | } |
| 1333 | 1333 | |
| 1334 | static_assert(sizeof(ZigClangDeclStmt_const_decl_iterator) == sizeof(clang::DeclStmt::const_decl_iterator), ""); | |
| 1335 | static ZigClangDeclStmt_const_decl_iterator bitcast(clang::DeclStmt::const_decl_iterator src) { | |
| 1336 | ZigClangDeclStmt_const_decl_iterator dest; | |
| 1337 | memcpy(&dest, static_cast<void *>(&src), sizeof(ZigClangDeclStmt_const_decl_iterator)); | |
| 1338 | return dest; | |
| 1339 | } | |
| 1334 | 1340 | |
| 1335 | 1341 | ZigClangSourceLocation ZigClangSourceManager_getSpellingLoc(const ZigClangSourceManager *self, |
| 1336 | 1342 | ZigClangSourceLocation Loc) |
| ... | ... | @@ -1381,7 +1387,7 @@ ZigClangSourceManager *ZigClangASTUnit_getSourceManager(ZigClangASTUnit *self) { |
| 1381 | 1387 | return reinterpret_cast<ZigClangSourceManager *>(result); |
| 1382 | 1388 | } |
| 1383 | 1389 | |
| 1384 | bool ZigClangASTUnit_visitLocalTopLevelDecls(ZigClangASTUnit *self, void *context, | |
| 1390 | bool ZigClangASTUnit_visitLocalTopLevelDecls(ZigClangASTUnit *self, void *context, | |
| 1385 | 1391 | bool (*Fn)(void *context, const ZigClangDecl *decl)) |
| 1386 | 1392 | { |
| 1387 | 1393 | return reinterpret_cast<clang::ASTUnit *>(self)->visitLocalTopLevelDecls(context, |
| ... | ... | @@ -1862,6 +1868,16 @@ ZigClangCompoundStmt_const_body_iterator ZigClangCompoundStmt_body_end(const str |
| 1862 | 1868 | return bitcast(casted->body_end()); |
| 1863 | 1869 | } |
| 1864 | 1870 | |
| 1871 | ZigClangDeclStmt_const_decl_iterator ZigClangDeclStmt_decl_begin(const struct ZigClangDeclStmt *self) { | |
| 1872 | auto casted = reinterpret_cast<const clang::DeclStmt *>(self); | |
| 1873 | return bitcast(casted->decl_begin()); | |
| 1874 | } | |
| 1875 | ||
| 1876 | ZigClangDeclStmt_const_decl_iterator ZigClangDeclStmt_decl_end(const struct ZigClangDeclStmt *self) { | |
| 1877 | auto casted = reinterpret_cast<const clang::DeclStmt *>(self); | |
| 1878 | return bitcast(casted->decl_end()); | |
| 1879 | } | |
| 1880 | ||
| 1865 | 1881 | unsigned ZigClangAPFloat_convertToHexString(const ZigClangAPFloat *self, char *DST, |
| 1866 | 1882 | unsigned HexDigits, bool UpperCase, enum ZigClangAPFloat_roundingMode RM) |
| 1867 | 1883 | { |
src/zig_clang.h+5-1| ... | ... | @@ -105,6 +105,7 @@ struct ZigClangFunctionType; |
| 105 | 105 | struct ZigClangPredefinedExpr; |
| 106 | 106 | |
| 107 | 107 | typedef struct ZigClangStmt *const * ZigClangCompoundStmt_const_body_iterator; |
| 108 | typedef struct ZigClangDecl *const * ZigClangDeclStmt_const_decl_iterator; | |
| 108 | 109 | |
| 109 | 110 | enum ZigClangBO { |
| 110 | 111 | ZigClangBO_PtrMemD, |
| ... | ... | @@ -754,7 +755,7 @@ ZIG_EXTERN_C void ZigClangErrorMsg_delete(struct Stage2ErrorMsg *ptr, size_t len |
| 754 | 755 | |
| 755 | 756 | ZIG_EXTERN_C struct ZigClangASTContext *ZigClangASTUnit_getASTContext(struct ZigClangASTUnit *); |
| 756 | 757 | ZIG_EXTERN_C struct ZigClangSourceManager *ZigClangASTUnit_getSourceManager(struct ZigClangASTUnit *); |
| 757 | ZIG_EXTERN_C bool ZigClangASTUnit_visitLocalTopLevelDecls(struct ZigClangASTUnit *, void *context, | |
| 758 | ZIG_EXTERN_C bool ZigClangASTUnit_visitLocalTopLevelDecls(struct ZigClangASTUnit *, void *context, | |
| 758 | 759 | bool (*Fn)(void *context, const struct ZigClangDecl *decl)); |
| 759 | 760 | |
| 760 | 761 | ZIG_EXTERN_C const struct ZigClangRecordDecl *ZigClangRecordType_getDecl(const struct ZigClangRecordType *record_ty); |
| ... | ... | @@ -846,6 +847,9 @@ ZIG_EXTERN_C struct ZigClangQualType ZigClangFunctionProtoType_getParamType(cons |
| 846 | 847 | ZIG_EXTERN_C ZigClangCompoundStmt_const_body_iterator ZigClangCompoundStmt_body_begin(const struct ZigClangCompoundStmt *self); |
| 847 | 848 | ZIG_EXTERN_C ZigClangCompoundStmt_const_body_iterator ZigClangCompoundStmt_body_end(const struct ZigClangCompoundStmt *self); |
| 848 | 849 | |
| 850 | ZIG_EXTERN_C ZigClangDeclStmt_const_decl_iterator ZigClangDeclStmt_decl_begin(const struct ZigClangDeclStmt *self); | |
| 851 | ZIG_EXTERN_C ZigClangDeclStmt_const_decl_iterator ZigClangDeclStmt_decl_end(const struct ZigClangDeclStmt *self); | |
| 852 | ||
| 849 | 853 | ZIG_EXTERN_C unsigned ZigClangAPFloat_convertToHexString(const struct ZigClangAPFloat *self, char *DST, |
| 850 | 854 | unsigned HexDigits, bool UpperCase, enum ZigClangAPFloat_roundingMode RM); |
| 851 | 855 |