authorgravatar for 9267733+fweig@users.noreply.github.comFeix Weiglhofer <9267733+fweig@users.noreply.github.com> 2020-01-24 21:32:32+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-24 15:32:32-05:00
loga4a93306482c4f7c331a2e8b7ecd5a1e5c56bf69
tree8f98e53881d533d6d95978cdaaa090d1a3f059d8
parentaa75df36df4a49557d5ea81b9bc256b227d6cd98

translate-c: Don't make const parameters mutable. (#4273)

* 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
768pub extern fn ZigClangEnumDecl_getCanonicalDecl(self: ?*const struct_ZigClangEnumDecl) ?*const struct_ZigClangTagDecl;768pub extern fn ZigClangEnumDecl_getCanonicalDecl(self: ?*const struct_ZigClangEnumDecl) ?*const struct_ZigClangTagDecl;
769pub extern fn ZigClangTypedefNameDecl_getCanonicalDecl(self: ?*const struct_ZigClangTypedefNameDecl) ?*const struct_ZigClangTypedefNameDecl;769pub extern fn ZigClangTypedefNameDecl_getCanonicalDecl(self: ?*const struct_ZigClangTypedefNameDecl) ?*const struct_ZigClangTypedefNameDecl;
770pub extern fn ZigClangFunctionDecl_getCanonicalDecl(self: ?*const struct_ZigClangFunctionDecl) ?*const struct_ZigClangFunctionDecl;770pub extern fn ZigClangFunctionDecl_getCanonicalDecl(self: ?*const struct_ZigClangFunctionDecl) ?*const struct_ZigClangFunctionDecl;
771pub extern fn ZigClangParmVarDecl_getOriginalType(self: ?*const struct_ZigClangParmVarDecl) struct_ZigClangQualType;
771pub extern fn ZigClangVarDecl_getCanonicalDecl(self: ?*const struct_ZigClangVarDecl) ?*const struct_ZigClangVarDecl;772pub extern fn ZigClangVarDecl_getCanonicalDecl(self: ?*const struct_ZigClangVarDecl) ?*const struct_ZigClangVarDecl;
772pub extern fn ZigClangVarDecl_getSectionAttribute(self: *const ZigClangVarDecl, len: *usize) ?[*]const u8;773pub extern fn ZigClangVarDecl_getSectionAttribute(self: *const ZigClangVarDecl, len: *usize) ?[*]const u8;
773pub extern fn ZigClangFunctionDecl_getAlignedAttribute(self: *const ZigClangFunctionDecl, *const ZigClangASTContext) c_uint;774pub 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;
486486
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 {
498499
499 const mangled_param_name = try block_scope.makeMangledName(c, param_name);500 const mangled_param_name = try block_scope.makeMangledName(c, param_name);
500501
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 };
505511
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 }
514524
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}
16271627
1628ZigClangQualType ZigClangParmVarDecl_getOriginalType(const struct ZigClangParmVarDecl *self) {
1629 return bitcast(reinterpret_cast<const clang::ParmVarDecl *>(self)->getOriginalType());
1630}
1631
1628const ZigClangRecordDecl *ZigClangRecordDecl_getDefinition(const ZigClangRecordDecl *zig_record_decl) {1632const 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
866ZIG_EXTERN_C unsigned ZigClangVarDecl_getAlignedAttribute(const struct ZigClangVarDecl *self, const ZigClangASTContext* ctx);866ZIG_EXTERN_C unsigned ZigClangVarDecl_getAlignedAttribute(const struct ZigClangVarDecl *self, const ZigClangASTContext* ctx);
867ZIG_EXTERN_C unsigned ZigClangFunctionDecl_getAlignedAttribute(const struct ZigClangFunctionDecl *self, const ZigClangASTContext* ctx);867ZIG_EXTERN_C unsigned ZigClangFunctionDecl_getAlignedAttribute(const struct ZigClangFunctionDecl *self, const ZigClangASTContext* ctx);
868868
869ZIG_EXTERN_C struct ZigClangQualType ZigClangParmVarDecl_getOriginalType(const struct ZigClangParmVarDecl *self);
870
869ZIG_EXTERN_C bool ZigClangRecordDecl_getPackedAttribute(const struct ZigClangRecordDecl *);871ZIG_EXTERN_C bool ZigClangRecordDecl_getPackedAttribute(const struct ZigClangRecordDecl *);
870ZIG_EXTERN_C const struct ZigClangRecordDecl *ZigClangRecordDecl_getDefinition(const struct ZigClangRecordDecl *);872ZIG_EXTERN_C const struct ZigClangRecordDecl *ZigClangRecordDecl_getDefinition(const struct ZigClangRecordDecl *);
871ZIG_EXTERN_C const struct ZigClangEnumDecl *ZigClangEnumDecl_getDefinition(const struct ZigClangEnumDecl *);873ZIG_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}