authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-08-20 15:33:56+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-08-21 16:32:09+03:00
log724995e892acd9d2981b55847f2ccb85af923d6b
tree4b31ee724152c08a4bda372f49061e75c4985d2e
parent3b2520583390c7ebbb1e1ea458c9ced04180d9c4

translate-c: avoid repeating string in type when making it mutable


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

src/translate_c.zig+2-16
......@@ -721,27 +721,13 @@ fn transQualTypeMaybeInitialized(c: *Context, scope: *Scope, qt: clang.QualType,
721721
722722/// This is used in global scope to convert a string literal `S` to [*c]u8:
723723/// &(struct {
724/// var static: @TypeOf(S.*) = S.*;
724/// var static = S.*;
725725/// }).static;
726726fn stringLiteralToCharStar(c: *Context, str: Node) Error!Node {
727727 const var_name = Scope.Block.StaticInnerName;
728728
729 const derefed = try Tag.deref.create(c.arena, str);
730 const var_type = try Tag.typeof.create(c.arena, derefed);
731
732729 const variables = try c.arena.alloc(Node, 1);
733 variables[0] = try Tag.var_decl.create(c.arena, .{
734 .is_pub = false,
735 .is_const = false,
736 .is_extern = false,
737 .is_export = false,
738 .is_threadlocal = false,
739 .linksection_string = null,
740 .alignment = null,
741 .name = var_name,
742 .type = var_type,
743 .init = derefed,
744 });
730 variables[0] = try Tag.mut_str.create(c.arena, .{ .name = var_name, .init = str });
745731
746732 const anon_struct = try Tag.@"struct".create(c.arena, .{
747733 .layout = .none,
src/translate_c/ast.zig+29-2
......@@ -62,6 +62,8 @@ pub const Node = extern union {
6262 var_decl,
6363 /// const name = struct { init }
6464 static_local_var,
65 /// var name = init.*
66 mut_str,
6567 func,
6668 warning,
6769 @"struct",
......@@ -361,7 +363,7 @@ pub const Node = extern union {
361363 .array_type, .null_sentinel_array_type => Payload.Array,
362364 .arg_redecl, .alias, .fail_decl => Payload.ArgRedecl,
363365 .log2_int_type => Payload.Log2IntType,
364 .var_simple, .pub_var_simple, .static_local_var => Payload.SimpleVarDecl,
366 .var_simple, .pub_var_simple, .static_local_var, .mut_str => Payload.SimpleVarDecl,
365367 .enum_constant => Payload.EnumConstant,
366368 .array_filler => Payload.ArrayFiller,
367369 .pub_inline_fn => Payload.PubInlineFn,
......@@ -1230,6 +1232,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
12301232 },
12311233 });
12321234 _ = try c.addToken(.r_brace, "}");
1235 _ = try c.addToken(.semicolon, ";");
12331236
12341237 return c.addNode(.{
12351238 .tag = .simple_var_decl,
......@@ -1240,6 +1243,29 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
12401243 },
12411244 });
12421245 },
1246 .mut_str => {
1247 const payload = node.castTag(.mut_str).?.data;
1248
1249 const var_tok = try c.addToken(.keyword_var, "var");
1250 _ = try c.addIdentifier(payload.name);
1251 _ = try c.addToken(.equal, "=");
1252
1253 const deref = try c.addNode(.{
1254 .tag = .deref,
1255 .data = .{
1256 .lhs = try renderNodeGrouped(c, payload.init),
1257 .rhs = undefined,
1258 },
1259 .main_token = try c.addToken(.period_asterisk, ".*"),
1260 });
1261 _ = try c.addToken(.semicolon, ";");
1262
1263 return c.addNode(.{
1264 .tag = .simple_var_decl,
1265 .main_token = var_tok,
1266 .data = .{ .lhs = 0, .rhs = deref },
1267 });
1268 },
12431269 .var_decl => return renderVar(c, node),
12441270 .arg_redecl, .alias => {
12451271 const payload = @fieldParentPtr(Payload.ArgRedecl, "base", node.ptr_otherwise).data;
......@@ -2145,7 +2171,7 @@ fn renderNullSentinelArrayType(c: *Context, len: usize, elem_type: Node) !NodeIn
21452171fn addSemicolonIfNeeded(c: *Context, node: Node) !void {
21462172 switch (node.tag()) {
21472173 .warning => unreachable,
2148 .var_decl, .var_simple, .arg_redecl, .alias, .block, .empty_block, .block_single, .@"switch" => {},
2174 .var_decl, .var_simple, .arg_redecl, .alias, .block, .empty_block, .block_single, .@"switch", .static_local_var, .mut_str => {},
21492175 .while_true => {
21502176 const payload = node.castTag(.while_true).?.data;
21512177 return addSemicolonIfNotBlock(c, payload);
......@@ -2240,6 +2266,7 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
22402266 .offset_of,
22412267 .shuffle,
22422268 .static_local_var,
2269 .mut_str,
22432270 => {
22442271 // no grouping needed
22452272 return renderNode(c, node);