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,...@@ -721,27 +721,13 @@ fn transQualTypeMaybeInitialized(c: *Context, scope: *Scope, qt: clang.QualType,
721721
722/// This is used in global scope to convert a string literal `S` to [*c]u8:722/// This is used in global scope to convert a string literal `S` to [*c]u8:
723/// &(struct {723/// &(struct {
724/// var static: @TypeOf(S.*) = S.*;724/// var static = S.*;
725/// }).static;725/// }).static;
726fn stringLiteralToCharStar(c: *Context, str: Node) Error!Node {726fn stringLiteralToCharStar(c: *Context, str: Node) Error!Node {
727 const var_name = Scope.Block.StaticInnerName;727 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
732 const variables = try c.arena.alloc(Node, 1);729 const variables = try c.arena.alloc(Node, 1);
733 variables[0] = try Tag.var_decl.create(c.arena, .{730 variables[0] = try Tag.mut_str.create(c.arena, .{ .name = var_name, .init = str });
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 });
745731
746 const anon_struct = try Tag.@"struct".create(c.arena, .{732 const anon_struct = try Tag.@"struct".create(c.arena, .{
747 .layout = .none,733 .layout = .none,
src/translate_c/ast.zig+29-2
...@@ -62,6 +62,8 @@ pub const Node = extern union {...@@ -62,6 +62,8 @@ pub const Node = extern union {
62 var_decl,62 var_decl,
63 /// const name = struct { init }63 /// const name = struct { init }
64 static_local_var,64 static_local_var,
65 /// var name = init.*
66 mut_str,
65 func,67 func,
66 warning,68 warning,
67 @"struct",69 @"struct",
...@@ -361,7 +363,7 @@ pub const Node = extern union {...@@ -361,7 +363,7 @@ pub const Node = extern union {
361 .array_type, .null_sentinel_array_type => Payload.Array,363 .array_type, .null_sentinel_array_type => Payload.Array,
362 .arg_redecl, .alias, .fail_decl => Payload.ArgRedecl,364 .arg_redecl, .alias, .fail_decl => Payload.ArgRedecl,
363 .log2_int_type => Payload.Log2IntType,365 .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,
365 .enum_constant => Payload.EnumConstant,367 .enum_constant => Payload.EnumConstant,
366 .array_filler => Payload.ArrayFiller,368 .array_filler => Payload.ArrayFiller,
367 .pub_inline_fn => Payload.PubInlineFn,369 .pub_inline_fn => Payload.PubInlineFn,
...@@ -1230,6 +1232,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {...@@ -1230,6 +1232,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
1230 },1232 },
1231 });1233 });
1232 _ = try c.addToken(.r_brace, "}");1234 _ = try c.addToken(.r_brace, "}");
1235 _ = try c.addToken(.semicolon, ";");
12331236
1234 return c.addNode(.{1237 return c.addNode(.{
1235 .tag = .simple_var_decl,1238 .tag = .simple_var_decl,
...@@ -1240,6 +1243,29 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {...@@ -1240,6 +1243,29 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
1240 },1243 },
1241 });1244 });
1242 },1245 },
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 },
1243 .var_decl => return renderVar(c, node),1269 .var_decl => return renderVar(c, node),
1244 .arg_redecl, .alias => {1270 .arg_redecl, .alias => {
1245 const payload = @fieldParentPtr(Payload.ArgRedecl, "base", node.ptr_otherwise).data;1271 const payload = @fieldParentPtr(Payload.ArgRedecl, "base", node.ptr_otherwise).data;
...@@ -2145,7 +2171,7 @@ fn renderNullSentinelArrayType(c: *Context, len: usize, elem_type: Node) !NodeIn...@@ -2145,7 +2171,7 @@ fn renderNullSentinelArrayType(c: *Context, len: usize, elem_type: Node) !NodeIn
2145fn addSemicolonIfNeeded(c: *Context, node: Node) !void {2171fn addSemicolonIfNeeded(c: *Context, node: Node) !void {
2146 switch (node.tag()) {2172 switch (node.tag()) {
2147 .warning => unreachable,2173 .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 => {},
2149 .while_true => {2175 .while_true => {
2150 const payload = node.castTag(.while_true).?.data;2176 const payload = node.castTag(.while_true).?.data;
2151 return addSemicolonIfNotBlock(c, payload);2177 return addSemicolonIfNotBlock(c, payload);
...@@ -2240,6 +2266,7 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {...@@ -2240,6 +2266,7 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
2240 .offset_of,2266 .offset_of,
2241 .shuffle,2267 .shuffle,
2242 .static_local_var,2268 .static_local_var,
2269 .mut_str,
2243 => {2270 => {
2244 // no grouping needed2271 // no grouping needed
2245 return renderNode(c, node);2272 return renderNode(c, node);