| author | |
| committer | |
| log | 32c27daae413b3348b5e25faf174651775e8bf4a |
| tree | 52fd7dcedfaeaab5fc5a17a6bb836816578f9c84 |
| parent | 46f50ee76c1b97bdddc56e594e24d946c4f4b621 |
2 files changed, 85 insertions(+), 73 deletions(-)
src-self-hosted/clang.zig+5-6| ... | @@ -22,7 +22,7 @@ pub const struct_ZigClangCompoundStmt = @Type(.Opaque); | ... | @@ -22,7 +22,7 @@ pub const struct_ZigClangCompoundStmt = @Type(.Opaque); |
| 22 | pub const struct_ZigClangConstantArrayType = @Type(.Opaque); | 22 | pub const struct_ZigClangConstantArrayType = @Type(.Opaque); |
| 23 | pub const struct_ZigClangContinueStmt = @Type(.Opaque); | 23 | pub const struct_ZigClangContinueStmt = @Type(.Opaque); |
| 24 | pub const struct_ZigClangDecayedType = @Type(.Opaque); | 24 | pub const struct_ZigClangDecayedType = @Type(.Opaque); |
| 25 | pub const struct_ZigClangDecl = @Type(.Opaque); | 25 | pub const ZigClangDecl = @Type(.Opaque); |
| 26 | pub const struct_ZigClangDeclRefExpr = @Type(.Opaque); | 26 | pub const struct_ZigClangDeclRefExpr = @Type(.Opaque); |
| 27 | pub const struct_ZigClangDeclStmt = @Type(.Opaque); | 27 | pub const struct_ZigClangDeclStmt = @Type(.Opaque); |
| 28 | pub const struct_ZigClangDefaultStmt = @Type(.Opaque); | 28 | pub const struct_ZigClangDefaultStmt = @Type(.Opaque); |
| ... | @@ -781,7 +781,7 @@ pub extern fn ZigClangSourceManager_getCharacterData(self: ?*const struct_ZigCla | ... | @@ -781,7 +781,7 @@ pub extern fn ZigClangSourceManager_getCharacterData(self: ?*const struct_ZigCla |
| 781 | pub extern fn ZigClangASTContext_getPointerType(self: ?*const struct_ZigClangASTContext, T: struct_ZigClangQualType) struct_ZigClangQualType; | 781 | pub extern fn ZigClangASTContext_getPointerType(self: ?*const struct_ZigClangASTContext, T: struct_ZigClangQualType) struct_ZigClangQualType; |
| 782 | pub extern fn ZigClangASTUnit_getASTContext(self: ?*struct_ZigClangASTUnit) ?*struct_ZigClangASTContext; | 782 | pub extern fn ZigClangASTUnit_getASTContext(self: ?*struct_ZigClangASTUnit) ?*struct_ZigClangASTContext; |
| 783 | pub extern fn ZigClangASTUnit_getSourceManager(self: *struct_ZigClangASTUnit) *struct_ZigClangSourceManager; | 783 | pub extern fn ZigClangASTUnit_getSourceManager(self: *struct_ZigClangASTUnit) *struct_ZigClangSourceManager; |
| 784 | pub extern fn ZigClangASTUnit_visitLocalTopLevelDecls(self: *struct_ZigClangASTUnit, context: ?*c_void, Fn: ?fn (?*c_void, *const struct_ZigClangDecl) callconv(.C) bool) bool; | 784 | pub extern fn ZigClangASTUnit_visitLocalTopLevelDecls(self: *struct_ZigClangASTUnit, context: ?*c_void, Fn: ?fn (?*c_void, *const ZigClangDecl) callconv(.C) bool) bool; |
| 785 | pub extern fn ZigClangRecordType_getDecl(record_ty: ?*const struct_ZigClangRecordType) *const struct_ZigClangRecordDecl; | 785 | pub extern fn ZigClangRecordType_getDecl(record_ty: ?*const struct_ZigClangRecordType) *const struct_ZigClangRecordDecl; |
| 786 | pub extern fn ZigClangTagDecl_isThisDeclarationADefinition(self: *const ZigClangTagDecl) bool; | 786 | pub extern fn ZigClangTagDecl_isThisDeclarationADefinition(self: *const ZigClangTagDecl) bool; |
| 787 | pub extern fn ZigClangEnumType_getDecl(record_ty: ?*const struct_ZigClangEnumType) *const struct_ZigClangEnumDecl; | 787 | pub extern fn ZigClangEnumType_getDecl(record_ty: ?*const struct_ZigClangEnumType) *const struct_ZigClangEnumDecl; |
| ... | @@ -817,7 +817,7 @@ pub extern fn ZigClangEnumDecl_enumerator_end(*const ZigClangEnumDecl) ZigClangE | ... | @@ -817,7 +817,7 @@ pub extern fn ZigClangEnumDecl_enumerator_end(*const ZigClangEnumDecl) ZigClangE |
| 817 | pub extern fn ZigClangEnumDecl_enumerator_iterator_next(ZigClangEnumDecl_enumerator_iterator) ZigClangEnumDecl_enumerator_iterator; | 817 | pub extern fn ZigClangEnumDecl_enumerator_iterator_next(ZigClangEnumDecl_enumerator_iterator) ZigClangEnumDecl_enumerator_iterator; |
| 818 | pub extern fn ZigClangEnumDecl_enumerator_iterator_deref(ZigClangEnumDecl_enumerator_iterator) *const ZigClangEnumConstantDecl; | 818 | pub extern fn ZigClangEnumDecl_enumerator_iterator_deref(ZigClangEnumDecl_enumerator_iterator) *const ZigClangEnumConstantDecl; |
| 819 | pub extern fn ZigClangEnumDecl_enumerator_iterator_neq(ZigClangEnumDecl_enumerator_iterator, ZigClangEnumDecl_enumerator_iterator) bool; | 819 | pub extern fn ZigClangEnumDecl_enumerator_iterator_neq(ZigClangEnumDecl_enumerator_iterator, ZigClangEnumDecl_enumerator_iterator) bool; |
| 820 | pub extern fn ZigClangDecl_castToNamedDecl(decl: *const struct_ZigClangDecl) ?*const ZigClangNamedDecl; | 820 | pub extern fn ZigClangDecl_castToNamedDecl(decl: *const ZigClangDecl) ?*const ZigClangNamedDecl; |
| 821 | pub extern fn ZigClangNamedDecl_getName_bytes_begin(decl: ?*const struct_ZigClangNamedDecl) [*:0]const u8; | 821 | pub extern fn ZigClangNamedDecl_getName_bytes_begin(decl: ?*const struct_ZigClangNamedDecl) [*:0]const u8; |
| 822 | pub extern fn ZigClangSourceLocation_eq(a: struct_ZigClangSourceLocation, b: struct_ZigClangSourceLocation) bool; | 822 | pub extern fn ZigClangSourceLocation_eq(a: struct_ZigClangSourceLocation, b: struct_ZigClangSourceLocation) bool; |
| 823 | pub extern fn ZigClangTypedefType_getDecl(self: ?*const struct_ZigClangTypedefType) *const struct_ZigClangTypedefNameDecl; | 823 | pub extern fn ZigClangTypedefType_getDecl(self: ?*const struct_ZigClangTypedefType) *const struct_ZigClangTypedefNameDecl; |
| ... | @@ -918,7 +918,6 @@ pub const ZigClangCompoundStmt = struct_ZigClangCompoundStmt; | ... | @@ -918,7 +918,6 @@ pub const ZigClangCompoundStmt = struct_ZigClangCompoundStmt; |
| 918 | pub const ZigClangConstantArrayType = struct_ZigClangConstantArrayType; | 918 | pub const ZigClangConstantArrayType = struct_ZigClangConstantArrayType; |
| 919 | pub const ZigClangContinueStmt = struct_ZigClangContinueStmt; | 919 | pub const ZigClangContinueStmt = struct_ZigClangContinueStmt; |
| 920 | pub const ZigClangDecayedType = struct_ZigClangDecayedType; | 920 | pub const ZigClangDecayedType = struct_ZigClangDecayedType; |
| 921 | pub const ZigClangDecl = struct_ZigClangDecl; | ||
| 922 | pub const ZigClangDeclRefExpr = struct_ZigClangDeclRefExpr; | 921 | pub const ZigClangDeclRefExpr = struct_ZigClangDeclRefExpr; |
| 923 | pub const ZigClangDeclStmt = struct_ZigClangDeclStmt; | 922 | pub const ZigClangDeclStmt = struct_ZigClangDeclStmt; |
| 924 | pub const ZigClangDefaultStmt = struct_ZigClangDefaultStmt; | 923 | pub const ZigClangDefaultStmt = struct_ZigClangDefaultStmt; |
| ... | @@ -1015,14 +1014,14 @@ pub extern fn ZigClangLoadFromCommandLine( | ... | @@ -1015,14 +1014,14 @@ pub extern fn ZigClangLoadFromCommandLine( |
| 1015 | ) ?*ZigClangASTUnit; | 1014 | ) ?*ZigClangASTUnit; |
| 1016 | 1015 | ||
| 1017 | pub extern fn ZigClangDecl_getKind(decl: *const ZigClangDecl) ZigClangDeclKind; | 1016 | pub extern fn ZigClangDecl_getKind(decl: *const ZigClangDecl) ZigClangDeclKind; |
| 1018 | pub extern fn ZigClangDecl_getDeclKindName(decl: *const struct_ZigClangDecl) [*:0]const u8; | 1017 | pub extern fn ZigClangDecl_getDeclKindName(decl: *const ZigClangDecl) [*:0]const u8; |
| 1019 | 1018 | ||
| 1020 | pub const ZigClangCompoundStmt_const_body_iterator = [*]const *ZigClangStmt; | 1019 | pub const ZigClangCompoundStmt_const_body_iterator = [*]const *ZigClangStmt; |
| 1021 | 1020 | ||
| 1022 | pub extern fn ZigClangCompoundStmt_body_begin(self: *const ZigClangCompoundStmt) ZigClangCompoundStmt_const_body_iterator; | 1021 | pub extern fn ZigClangCompoundStmt_body_begin(self: *const ZigClangCompoundStmt) ZigClangCompoundStmt_const_body_iterator; |
| 1023 | pub extern fn ZigClangCompoundStmt_body_end(self: *const ZigClangCompoundStmt) ZigClangCompoundStmt_const_body_iterator; | 1022 | pub extern fn ZigClangCompoundStmt_body_end(self: *const ZigClangCompoundStmt) ZigClangCompoundStmt_const_body_iterator; |
| 1024 | 1023 | ||
| 1025 | pub const ZigClangDeclStmt_const_decl_iterator = [*]const *struct_ZigClangDecl; | 1024 | pub const ZigClangDeclStmt_const_decl_iterator = [*]const *ZigClangDecl; |
| 1026 | 1025 | ||
| 1027 | pub extern fn ZigClangDeclStmt_decl_begin(self: *const ZigClangDeclStmt) ZigClangDeclStmt_const_decl_iterator; | 1026 | pub extern fn ZigClangDeclStmt_decl_begin(self: *const ZigClangDeclStmt) ZigClangDeclStmt_const_decl_iterator; |
| 1028 | pub extern fn ZigClangDeclStmt_decl_end(self: *const ZigClangDeclStmt) ZigClangDeclStmt_const_decl_iterator; | 1027 | pub extern fn ZigClangDeclStmt_decl_end(self: *const ZigClangDeclStmt) ZigClangDeclStmt_const_decl_iterator; |
src-self-hosted/translate_c.zig+80-67| ... | @@ -1443,78 +1443,91 @@ fn transCStyleCastExprClass( | ... | @@ -1443,78 +1443,91 @@ fn transCStyleCastExprClass( |
| 1443 | return maybeSuppressResult(rp, scope, result_used, cast_node); | 1443 | return maybeSuppressResult(rp, scope, result_used, cast_node); |
| 1444 | } | 1444 | } |
| 1445 | 1445 | ||
| 1446 | fn transDeclStmt(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangDeclStmt) TransError!*ast.Node { | 1446 | fn transDeclStmtOne(rp: RestorePoint, scope: *Scope, decl: *const ZigClangDecl, block_scope: *Scope.Block,) TransError!*ast.Node { |
| 1447 | const c = rp.c; | 1447 | const c = rp.c; |
| 1448 | const block_scope = scope.findBlockScope(c) catch unreachable; | 1448 | |
| 1449 | switch (ZigClangDecl_getKind(decl)) { | ||
| 1450 | .Var => { | ||
| 1451 | const var_decl = @ptrCast(*const ZigClangVarDecl, decl); | ||
| 1452 | |||
| 1453 | const thread_local_token = if (ZigClangVarDecl_getTLSKind(var_decl) == .None) | ||
| 1454 | null | ||
| 1455 | else | ||
| 1456 | try appendToken(c, .Keyword_threadlocal, "threadlocal"); | ||
| 1457 | const qual_type = ZigClangVarDecl_getTypeSourceInfo_getType(var_decl); | ||
| 1458 | const name = try c.str(ZigClangNamedDecl_getName_bytes_begin( | ||
| 1459 | @ptrCast(*const ZigClangNamedDecl, var_decl), | ||
| 1460 | )); | ||
| 1461 | const mangled_name = try block_scope.makeMangledName(c, name); | ||
| 1462 | const node = try transCreateNodeVarDecl(c, false, ZigClangQualType_isConstQualified(qual_type), mangled_name); | ||
| 1463 | |||
| 1464 | _ = try appendToken(c, .Colon, ":"); | ||
| 1465 | const loc = ZigClangDecl_getLocation(decl); | ||
| 1466 | node.type_node = try transQualType(rp, qual_type, loc); | ||
| 1467 | |||
| 1468 | node.eq_token = try appendToken(c, .Equal, "="); | ||
| 1469 | var init_node = if (ZigClangVarDecl_getInit(var_decl)) |expr| | ||
| 1470 | try transExprCoercing(rp, scope, expr, .used, .r_value) | ||
| 1471 | else | ||
| 1472 | try transCreateNodeUndefinedLiteral(c); | ||
| 1473 | if (!qualTypeIsBoolean(qual_type) and isBoolRes(init_node)) { | ||
| 1474 | const builtin_node = try rp.c.createBuiltinCall("@boolToInt", 1); | ||
| 1475 | builtin_node.params()[0] = init_node; | ||
| 1476 | builtin_node.rparen_token = try appendToken(rp.c, .RParen, ")"); | ||
| 1477 | init_node = &builtin_node.base; | ||
| 1478 | } | ||
| 1479 | node.init_node = init_node; | ||
| 1480 | node.semicolon_token = try appendToken(c, .Semicolon, ";"); | ||
| 1481 | return &node.base; | ||
| 1482 | }, | ||
| 1483 | .Typedef => { | ||
| 1484 | const typedef_decl = @ptrCast(*const ZigClangTypedefNameDecl, decl); | ||
| 1485 | const name = try c.str(ZigClangNamedDecl_getName_bytes_begin( | ||
| 1486 | @ptrCast(*const ZigClangNamedDecl, typedef_decl), | ||
| 1487 | )); | ||
| 1488 | |||
| 1489 | const underlying_qual = ZigClangTypedefNameDecl_getUnderlyingType(typedef_decl); | ||
| 1490 | const underlying_type = ZigClangQualType_getTypePtr(underlying_qual); | ||
| 1491 | |||
| 1492 | const mangled_name = try block_scope.makeMangledName(c, name); | ||
| 1493 | if (checkForBuiltinTypedef(name)) |builtin| { | ||
| 1494 | try block_scope.variables.append(.{ | ||
| 1495 | .alias = builtin, | ||
| 1496 | .name = mangled_name, | ||
| 1497 | }); | ||
| 1498 | @panic("what are we supposed to return here?"); | ||
| 1499 | } else { | ||
| 1500 | const node = (try transCreateNodeTypedef(rp, typedef_decl, false, mangled_name)) orelse | ||
| 1501 | return error.UnsupportedTranslation; | ||
| 1502 | return &node.base; | ||
| 1503 | } | ||
| 1504 | }, | ||
| 1505 | else => |kind| return revertAndWarn( | ||
| 1506 | rp, | ||
| 1507 | error.UnsupportedTranslation, | ||
| 1508 | ZigClangDecl_getLocation(decl), | ||
| 1509 | "TODO implement translation of DeclStmt kind {}", | ||
| 1510 | .{@tagName(kind)}, | ||
| 1511 | ), | ||
| 1512 | } | ||
| 1513 | } | ||
| 1514 | |||
| 1515 | fn transDeclStmt(rp: RestorePoint, scope: *Scope, stmt: *const ZigClangDeclStmt) TransError!*ast.Node { | ||
| 1516 | const block_scope = scope.findBlockScope(rp.c) catch unreachable; | ||
| 1449 | 1517 | ||
| 1450 | var it = ZigClangDeclStmt_decl_begin(stmt); | 1518 | var it = ZigClangDeclStmt_decl_begin(stmt); |
| 1451 | const end_it = ZigClangDeclStmt_decl_end(stmt); | 1519 | const end_it = ZigClangDeclStmt_decl_end(stmt); |
| 1452 | while (it != end_it) : (it += 1) { | 1520 | assert(it != end_it); |
| 1453 | switch (ZigClangDecl_getKind(it[0])) { | 1521 | while (true) : (it += 1) { |
| 1454 | .Var => { | 1522 | const node = try transDeclStmtOne(rp, scope, it[0], block_scope); |
| 1455 | const var_decl = @ptrCast(*const ZigClangVarDecl, it[0]); | ||
| 1456 | 1523 | ||
| 1457 | const thread_local_token = if (ZigClangVarDecl_getTLSKind(var_decl) == .None) | 1524 | if (it + 1 == end_it) { |
| 1458 | null | 1525 | return node; |
| 1459 | else | 1526 | } else { |
| 1460 | try appendToken(c, .Keyword_threadlocal, "threadlocal"); | 1527 | try block_scope.statements.append(node); |
| 1461 | const qual_type = ZigClangVarDecl_getTypeSourceInfo_getType(var_decl); | ||
| 1462 | const name = try c.str(ZigClangNamedDecl_getName_bytes_begin( | ||
| 1463 | @ptrCast(*const ZigClangNamedDecl, var_decl), | ||
| 1464 | )); | ||
| 1465 | const mangled_name = try block_scope.makeMangledName(c, name); | ||
| 1466 | const node = try transCreateNodeVarDecl(c, false, ZigClangQualType_isConstQualified(qual_type), mangled_name); | ||
| 1467 | |||
| 1468 | _ = try appendToken(c, .Colon, ":"); | ||
| 1469 | const loc = ZigClangStmt_getBeginLoc(@ptrCast(*const ZigClangStmt, stmt)); | ||
| 1470 | node.type_node = try transQualType(rp, qual_type, loc); | ||
| 1471 | |||
| 1472 | node.eq_token = try appendToken(c, .Equal, "="); | ||
| 1473 | var init_node = if (ZigClangVarDecl_getInit(var_decl)) |expr| | ||
| 1474 | try transExprCoercing(rp, scope, expr, .used, .r_value) | ||
| 1475 | else | ||
| 1476 | try transCreateNodeUndefinedLiteral(c); | ||
| 1477 | if (!qualTypeIsBoolean(qual_type) and isBoolRes(init_node)) { | ||
| 1478 | const builtin_node = try rp.c.createBuiltinCall("@boolToInt", 1); | ||
| 1479 | builtin_node.params()[0] = init_node; | ||
| 1480 | builtin_node.rparen_token = try appendToken(rp.c, .RParen, ")"); | ||
| 1481 | init_node = &builtin_node.base; | ||
| 1482 | } | ||
| 1483 | node.init_node = init_node; | ||
| 1484 | node.semicolon_token = try appendToken(c, .Semicolon, ";"); | ||
| 1485 | try block_scope.statements.append(&node.base); | ||
| 1486 | }, | ||
| 1487 | .Typedef => { | ||
| 1488 | const typedef_decl = @ptrCast(*const ZigClangTypedefNameDecl, it[0]); | ||
| 1489 | const name = try c.str(ZigClangNamedDecl_getName_bytes_begin( | ||
| 1490 | @ptrCast(*const ZigClangNamedDecl, typedef_decl), | ||
| 1491 | )); | ||
| 1492 | |||
| 1493 | const underlying_qual = ZigClangTypedefNameDecl_getUnderlyingType(typedef_decl); | ||
| 1494 | const underlying_type = ZigClangQualType_getTypePtr(underlying_qual); | ||
| 1495 | |||
| 1496 | const mangled_name = try block_scope.makeMangledName(c, name); | ||
| 1497 | if (checkForBuiltinTypedef(name)) |builtin| { | ||
| 1498 | try block_scope.variables.append(.{ | ||
| 1499 | .alias = builtin, | ||
| 1500 | .name = mangled_name, | ||
| 1501 | }); | ||
| 1502 | } else { | ||
| 1503 | const node = (try transCreateNodeTypedef(rp, typedef_decl, false, mangled_name)) orelse return error.UnsupportedTranslation; | ||
| 1504 | try block_scope.statements.append(&node.base); | ||
| 1505 | } | ||
| 1506 | }, | ||
| 1507 | else => |kind| return revertAndWarn( | ||
| 1508 | rp, | ||
| 1509 | error.UnsupportedTranslation, | ||
| 1510 | ZigClangStmt_getBeginLoc(@ptrCast(*const ZigClangStmt, stmt)), | ||
| 1511 | "TODO implement translation of DeclStmt kind {}", | ||
| 1512 | .{@tagName(kind)}, | ||
| 1513 | ), | ||
| 1514 | } | 1528 | } |
| 1515 | } | 1529 | } |
| 1516 | 1530 | unreachable; | |
| 1517 | @panic("previously the code was returning the containing block scope node here and I do not understand why it would do that"); | ||
| 1518 | } | 1531 | } |
| 1519 | 1532 | ||
| 1520 | fn transDeclRefExpr( | 1533 | fn transDeclRefExpr( |
| ... | @@ -2099,6 +2112,8 @@ fn transInitListExprRecord( | ... | @@ -2099,6 +2112,8 @@ fn transInitListExprRecord( |
| 2099 | var field_inits = std.ArrayList(*ast.Node).init(rp.c.gpa); | 2112 | var field_inits = std.ArrayList(*ast.Node).init(rp.c.gpa); |
| 2100 | defer field_inits.deinit(); | 2113 | defer field_inits.deinit(); |
| 2101 | 2114 | ||
| 2115 | _ = try appendToken(rp.c, .LBrace, "{"); | ||
| 2116 | |||
| 2102 | var init_i: c_uint = 0; | 2117 | var init_i: c_uint = 0; |
| 2103 | var it = ZigClangRecordDecl_field_begin(record_def); | 2118 | var it = ZigClangRecordDecl_field_begin(record_def); |
| 2104 | const end_it = ZigClangRecordDecl_field_end(record_def); | 2119 | const end_it = ZigClangRecordDecl_field_end(record_def); |
| ... | @@ -2138,8 +2153,6 @@ fn transInitListExprRecord( | ... | @@ -2138,8 +2153,6 @@ fn transInitListExprRecord( |
| 2138 | _ = try appendToken(rp.c, .Comma, ","); | 2153 | _ = try appendToken(rp.c, .Comma, ","); |
| 2139 | } | 2154 | } |
| 2140 | 2155 | ||
| 2141 | _ = try appendToken(rp.c, .LBrace, "{"); | ||
| 2142 | |||
| 2143 | const node = try ast.Node.StructInitializer.alloc(rp.c.arena, field_inits.items.len); | 2156 | const node = try ast.Node.StructInitializer.alloc(rp.c.arena, field_inits.items.len); |
| 2144 | node.* = .{ | 2157 | node.* = .{ |
| 2145 | .lhs = ty_node, | 2158 | .lhs = ty_node, |