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
768768pub extern fn ZigClangEnumDecl_getCanonicalDecl(self: ?*const struct_ZigClangEnumDecl) ?*const struct_ZigClangTagDecl;
769769pub extern fn ZigClangTypedefNameDecl_getCanonicalDecl(self: ?*const struct_ZigClangTypedefNameDecl) ?*const struct_ZigClangTypedefNameDecl;
770770pub extern fn ZigClangFunctionDecl_getCanonicalDecl(self: ?*const struct_ZigClangFunctionDecl) ?*const struct_ZigClangFunctionDecl;
771pub extern fn ZigClangParmVarDecl_getOriginalType(self: ?*const struct_ZigClangParmVarDecl) struct_ZigClangQualType;
771772pub extern fn ZigClangVarDecl_getCanonicalDecl(self: ?*const struct_ZigClangVarDecl) ?*const struct_ZigClangVarDecl;
772773pub extern fn ZigClangVarDecl_getSectionAttribute(self: *const ZigClangVarDecl, len: *usize) ?[*]const u8;
773774pub 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 {
485485 block_scope.block_node = block_node;
486486
487487 var it = proto_node.params.iterator(0);
488 var param_id: c_uint = 0;
488489 while (it.next()) |p| {
489490 const param = @fieldParentPtr(ast.Node.ParamDecl, "base", p.*);
490491 const param_name = if (param.name_token) |name_tok|
......@@ -498,18 +499,27 @@ fn visitFnDecl(c: *Context, fn_decl: *const ZigClangFunctionDecl) Error!void {
498499
499500 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
501506 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});
503509 break :blk try block_scope.makeMangledName(c, bare_arg_name);
504510 };
505511
506 const node = try transCreateNodeVarDecl(c, false, false, mangled_param_name);
507 node.eq_token = try appendToken(c, .Equal, "=");
508 node.init_node = try transCreateNodeIdentifier(c, arg_name);
509 node.semicolon_token = try appendToken(c, .Semicolon, ";");
510 try block_node.statements.push(&node.base);
511 param.name_token = try appendIdentifier(c, arg_name);
512 _ = try appendToken(c, .Colon, ":");
512 if (!is_const) {
513 const node = try transCreateNodeVarDecl(c, false, false, mangled_param_name);
514 node.eq_token = try appendToken(c, .Equal, "=");
515 node.init_node = try transCreateNodeIdentifier(c, arg_name);
516 node.semicolon_token = try appendToken(c, .Semicolon, ";");
517 try block_node.statements.push(&node.base);
518 param.name_token = try appendIdentifier(c, arg_name);
519 _ = try appendToken(c, .Colon, ":");
520 }
521
522 param_id += 1;
513523 }
514524
515525 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
16251625 return 0;
16261626}
16271627
1628ZigClangQualType ZigClangParmVarDecl_getOriginalType(const struct ZigClangParmVarDecl *self) {
1629 return bitcast(reinterpret_cast<const clang::ParmVarDecl *>(self)->getOriginalType());
1630}
1631
16281632const ZigClangRecordDecl *ZigClangRecordDecl_getDefinition(const ZigClangRecordDecl *zig_record_decl) {
16291633 const clang::RecordDecl *record_decl = reinterpret_cast<const clang::RecordDecl *>(zig_record_decl);
16301634 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
866866ZIG_EXTERN_C unsigned ZigClangVarDecl_getAlignedAttribute(const struct ZigClangVarDecl *self, const ZigClangASTContext* ctx);
867867ZIG_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
869871ZIG_EXTERN_C bool ZigClangRecordDecl_getPackedAttribute(const struct ZigClangRecordDecl *);
870872ZIG_EXTERN_C const struct ZigClangRecordDecl *ZigClangRecordDecl_getDefinition(const struct ZigClangRecordDecl *);
871873ZIG_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 {
26152615 \\ return foo((@intCast(c_int, @bitCast(i1, @intCast(u1, @boolToInt(c)))) != @intCast(c_int, @bitCast(i1, @intCast(u1, @boolToInt(b))))));
26162616 \\}
26172617 });
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
26182630}