authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2021-04-20 11:07:21+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-04-20 15:16:25+03:00
logbe551d85b7610a7e0570023934f9b92ddf129966
treef3e4e1fcabc054a43b26d12fba7449d33969685b
parent63304a871e3837f407943ac8eb63073fdc89f0fd

translate-c: Group field access LHS if necessary


2 files changed, 18 insertions(+), 2 deletions(-)

src/translate_c/ast.zig+2-2
......@@ -1728,7 +1728,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
17281728 },
17291729 .field_access => {
17301730 const payload = node.castTag(.field_access).?.data;
1731 const lhs = try renderNode(c, payload.lhs);
1731 const lhs = try renderNodeGrouped(c, payload.lhs);
17321732 return renderFieldAccess(c, lhs, payload.field_name);
17331733 },
17341734 .@"struct", .@"union" => return renderRecord(c, node),
......@@ -2073,7 +2073,7 @@ fn renderNullSentinelArrayType(c: *Context, len: usize, elem_type: Node) !NodeIn
20732073 .main_token = l_bracket,
20742074 .data = .{
20752075 .lhs = len_expr,
2076 .rhs = try c.addExtra(std.zig.ast.Node.ArrayTypeSentinel {
2076 .rhs = try c.addExtra(std.zig.ast.Node.ArrayTypeSentinel{
20772077 .sentinel = sentinel_expr,
20782078 .elem_type = elem_type_expr,
20792079 }),
test/translate_c.zig+16
......@@ -3,6 +3,22 @@ const std = @import("std");
33const CrossTarget = std.zig.CrossTarget;
44
55pub fn addCases(cases: *tests.TranslateCContext) void {
6 cases.add("field access is grouped if necessary",
7 \\unsigned long foo(unsigned long x) {
8 \\ return ((union{unsigned long _x}){x})._x;
9 \\}
10 , &[_][]const u8{
11 \\pub export fn foo(arg_x: c_ulong) c_ulong {
12 \\ var x = arg_x;
13 \\ const union_unnamed_1 = extern union {
14 \\ _x: c_ulong,
15 \\ };
16 \\ return (union_unnamed_1{
17 \\ ._x = x,
18 \\ })._x;
19 \\}
20 });
21
622 cases.add("unnamed child types of typedef receive typedef's name",
723 \\typedef enum {
824 \\ FooA,