| author | |
| committer | |
| log | a4a93306482c4f7c331a2e8b7ecd5a1e5c56bf69 |
| tree | 8f98e53881d533d6d95978cdaaa090d1a3f059d8 |
| parent | aa75df36df4a49557d5ea81b9bc256b227d6cd98 |
* translate-c: Remove arg-prefix from const parameters.
* translate-c: Add unittest for const parameters.
5 files changed, 37 insertions(+), 8 deletions(-)
src-self-hosted/clang.zig+1| ... | @@ -768,6 +768,7 @@ pub extern fn ZigClangFieldDecl_getCanonicalDecl(field_decl: ?*const struct_ZigC | ... | @@ -768,6 +768,7 @@ pub extern fn ZigClangFieldDecl_getCanonicalDecl(field_decl: ?*const struct_ZigC |
| 768 | pub extern fn ZigClangEnumDecl_getCanonicalDecl(self: ?*const struct_ZigClangEnumDecl) ?*const struct_ZigClangTagDecl; | 768 | pub extern fn ZigClangEnumDecl_getCanonicalDecl(self: ?*const struct_ZigClangEnumDecl) ?*const struct_ZigClangTagDecl; |
| 769 | pub extern fn ZigClangTypedefNameDecl_getCanonicalDecl(self: ?*const struct_ZigClangTypedefNameDecl) ?*const struct_ZigClangTypedefNameDecl; | 769 | pub extern fn ZigClangTypedefNameDecl_getCanonicalDecl(self: ?*const struct_ZigClangTypedefNameDecl) ?*const struct_ZigClangTypedefNameDecl; |
| 770 | pub extern fn ZigClangFunctionDecl_getCanonicalDecl(self: ?*const struct_ZigClangFunctionDecl) ?*const struct_ZigClangFunctionDecl; | 770 | pub extern fn ZigClangFunctionDecl_getCanonicalDecl(self: ?*const struct_ZigClangFunctionDecl) ?*const struct_ZigClangFunctionDecl; |
| 771 | pub extern fn ZigClangParmVarDecl_getOriginalType(self: ?*const struct_ZigClangParmVarDecl) struct_ZigClangQualType; | ||
| 771 | pub extern fn ZigClangVarDecl_getCanonicalDecl(self: ?*const struct_ZigClangVarDecl) ?*const struct_ZigClangVarDecl; | 772 | pub extern fn ZigClangVarDecl_getCanonicalDecl(self: ?*const struct_ZigClangVarDecl) ?*const struct_ZigClangVarDecl; |
| 772 | pub extern fn ZigClangVarDecl_getSectionAttribute(self: *const ZigClangVarDecl, len: *usize) ?[*]const u8; | 773 | pub extern fn ZigClangVarDecl_getSectionAttribute(self: *const ZigClangVarDecl, len: *usize) ?[*]const u8; |
| 773 | pub extern fn ZigClangFunctionDecl_getAlignedAttribute(self: *const ZigClangFunctionDecl, *const ZigClangASTContext) c_uint; | 774 | pub extern fn ZigClangFunctionDecl_getAlignedAttribute(self: *const ZigClangFunctionDecl, *const ZigClangASTContext) c_uint; |
src-self-hosted/translate_c.zig+18-8| ... | @@ -485,6 +485,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void { | ... | @@ -485,6 +485,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void { |
| 485 | block_scope.block_node = block_node; | 485 | block_scope.block_node = block_node; |
| 486 | 486 | ||
| 487 | var it = proto_node.params.iterator(0); | 487 | var it = proto_node.params.iterator(0); |
| 488 | var param_id: c_uint = 0; | ||
| 488 | while (it.next()) |p| { | 489 | while (it.next()) |p| { |
| 489 | const param = @fieldParentPtr(ast.Node.ParamDecl, "base", p.*); | 490 | const param = @fieldParentPtr(ast.Node.ParamDecl, "base", p.*); |
| 490 | const param_name = if (param.name_token) |name_tok| | 491 | const param_name = if (param.name_token) |name_tok| |
| ... | @@ -498,18 +499,27 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void { | ... | @@ -498,18 +499,27 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void { |
| 498 | 499 | ||
| 499 | const mangled_param_name = try block_scope.makeMangledName(c, param_name); | 500 | const mangled_param_name = try block_scope.makeMangledName(c, param_name); |
| 500 | 501 | ||
| 502 | const c_param = ZigClangFunctionDecl_getParamDecl(fn_decl, param_id); | ||
| 503 | const qual_type = ZigClangParmVarDecl_getOriginalType(c_param); | ||
| 504 | const is_const = ZigClangQualType_isConstQualified(qual_type); | ||
| 505 | |||
| 501 | const arg_name = blk: { | 506 | const arg_name = blk: { |
| 502 | const bare_arg_name = try std.fmt.allocPrint(c.a(), "arg_{}", .{mangled_param_name}); | 507 | const param_prefix = if (is_const) "" else "arg_"; |
| 508 | const bare_arg_name = try std.fmt.allocPrint(c.a(), "{}{}", .{param_prefix, mangled_param_name}); | ||
| 503 | break :blk try block_scope.makeMangledName(c, bare_arg_name); | 509 | break :blk try block_scope.makeMangledName(c, bare_arg_name); |
| 504 | }; | 510 | }; |
| 505 | 511 | ||
| 506 | const node = try transCreateNodeVarDecl(c, false, false, mangled_param_name); | 512 | if (!is_const) { |
| 507 | node.eq_token = try appendToken(c, .Equal, "="); | 513 | const node = try transCreateNodeVarDecl(c, false, false, mangled_param_name); |
| 508 | node.init_node = try transCreateNodeIdentifier(c, arg_name); | 514 | node.eq_token = try appendToken(c, .Equal, "="); |
| 509 | node.semicolon_token = try appendToken(c, .Semicolon, ";"); | 515 | node.init_node = try transCreateNodeIdentifier(c, arg_name); |
| 510 | try block_node.statements.push(&node.base); | 516 | node.semicolon_token = try appendToken(c, .Semicolon, ";"); |
| 511 | param.name_token = try appendIdentifier(c, arg_name); | 517 | try block_node.statements.push(&node.base); |
| 512 | _ = try appendToken(c, .Colon, ":"); | 518 | param.name_token = try appendIdentifier(c, arg_name); |
| 519 | _ = try appendToken(c, .Colon, ":"); | ||
| 520 | } | ||
| 521 | |||
| 522 | param_id += 1; | ||
| 513 | } | 523 | } |
| 514 | 524 | ||
| 515 | transCompoundStmtInline(rp, &block_scope.base, @ptrCast(*const ZigClangCompoundStmt, body_stmt), block_node) catch |err| switch (err) { | 525 | transCompoundStmtInline(rp, &block_scope.base, @ptrCast(*const ZigClangCompoundStmt, body_stmt), block_node) catch |err| switch (err) { |
src/zig_clang.cpp+4| ... | @@ -1625,6 +1625,10 @@ unsigned ZigClangFunctionDecl_getAlignedAttribute(const struct ZigClangFunctionD | ... | @@ -1625,6 +1625,10 @@ unsigned ZigClangFunctionDecl_getAlignedAttribute(const struct ZigClangFunctionD |
| 1625 | return 0; | 1625 | return 0; |
| 1626 | } | 1626 | } |
| 1627 | 1627 | ||
| 1628 | ZigClangQualType ZigClangParmVarDecl_getOriginalType(const struct ZigClangParmVarDecl *self) { | ||
| 1629 | return bitcast(reinterpret_cast<const clang::ParmVarDecl *>(self)->getOriginalType()); | ||
| 1630 | } | ||
| 1631 | |||
| 1628 | const ZigClangRecordDecl *ZigClangRecordDecl_getDefinition(const ZigClangRecordDecl *zig_record_decl) { | 1632 | const ZigClangRecordDecl *ZigClangRecordDecl_getDefinition(const ZigClangRecordDecl *zig_record_decl) { |
| 1629 | const clang::RecordDecl *record_decl = reinterpret_cast<const clang::RecordDecl *>(zig_record_decl); | 1633 | const clang::RecordDecl *record_decl = reinterpret_cast<const clang::RecordDecl *>(zig_record_decl); |
| 1630 | const clang::RecordDecl *definition = record_decl->getDefinition(); | 1634 | const clang::RecordDecl *definition = record_decl->getDefinition(); |
src/zig_clang.h+2| ... | @@ -866,6 +866,8 @@ ZIG_EXTERN_C const char* ZigClangVarDecl_getSectionAttribute(const struct ZigCla | ... | @@ -866,6 +866,8 @@ ZIG_EXTERN_C const char* ZigClangVarDecl_getSectionAttribute(const struct ZigCla |
| 866 | ZIG_EXTERN_C unsigned ZigClangVarDecl_getAlignedAttribute(const struct ZigClangVarDecl *self, const ZigClangASTContext* ctx); | 866 | ZIG_EXTERN_C unsigned ZigClangVarDecl_getAlignedAttribute(const struct ZigClangVarDecl *self, const ZigClangASTContext* ctx); |
| 867 | ZIG_EXTERN_C unsigned ZigClangFunctionDecl_getAlignedAttribute(const struct ZigClangFunctionDecl *self, const ZigClangASTContext* ctx); | 867 | ZIG_EXTERN_C unsigned ZigClangFunctionDecl_getAlignedAttribute(const struct ZigClangFunctionDecl *self, const ZigClangASTContext* ctx); |
| 868 | 868 | ||
| 869 | ZIG_EXTERN_C struct ZigClangQualType ZigClangParmVarDecl_getOriginalType(const struct ZigClangParmVarDecl *self); | ||
| 870 | |||
| 869 | ZIG_EXTERN_C bool ZigClangRecordDecl_getPackedAttribute(const struct ZigClangRecordDecl *); | 871 | ZIG_EXTERN_C bool ZigClangRecordDecl_getPackedAttribute(const struct ZigClangRecordDecl *); |
| 870 | ZIG_EXTERN_C const struct ZigClangRecordDecl *ZigClangRecordDecl_getDefinition(const struct ZigClangRecordDecl *); | 872 | ZIG_EXTERN_C const struct ZigClangRecordDecl *ZigClangRecordDecl_getDefinition(const struct ZigClangRecordDecl *); |
| 871 | ZIG_EXTERN_C const struct ZigClangEnumDecl *ZigClangEnumDecl_getDefinition(const struct ZigClangEnumDecl *); | 873 | ZIG_EXTERN_C const struct ZigClangEnumDecl *ZigClangEnumDecl_getDefinition(const struct ZigClangEnumDecl *); |
test/translate_c.zig+12| ... | @@ -2615,4 +2615,16 @@ pub fn addCases(cases: *tests.TranslateCContext) void { | ... | @@ -2615,4 +2615,16 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 2615 | \\ return foo((@intCast(c_int, @bitCast(i1, @intCast(u1, @boolToInt(c)))) != @intCast(c_int, @bitCast(i1, @intCast(u1, @boolToInt(b)))))); | 2615 | \\ return foo((@intCast(c_int, @bitCast(i1, @intCast(u1, @boolToInt(c)))) != @intCast(c_int, @bitCast(i1, @intCast(u1, @boolToInt(b)))))); |
| 2616 | \\} | 2616 | \\} |
| 2617 | }); | 2617 | }); |
| 2618 | |||
| 2619 | cases.add("Don't make const parameters mutable", | ||
| 2620 | \\int max(const int x, int y) { | ||
| 2621 | \\ return (x > y) ? x : y; | ||
| 2622 | \\} | ||
| 2623 | , &[_][]const u8{ | ||
| 2624 | \\pub export fn max(x: c_int, arg_y: c_int) c_int { | ||
| 2625 | \\ var y = arg_y; | ||
| 2626 | \\ return if (x > y) x else y; | ||
| 2627 | \\} | ||
| 2628 | }); | ||
| 2629 | |||
| 2618 | } | 2630 | } |