authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-17 01:41:52-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-08-17 01:41:52-04:00
log6b141620a623ae36ad0e40533457a5a24aa404e1
tree386a27767900e48ff516cd3ec76ac160aaf3b116
parentfc850aad619cc47eb7634a326bad670b43964532
parent3ca8c42e7ab04886f536a7bab34c9ea563c62711
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #6048 from pixelherodev/cleanup

Some minor cleanup and error handling

3 files changed, 35 insertions(+), 60 deletions(-)

src-self-hosted/Module.zig+2
...@@ -1580,6 +1580,8 @@ fn analyzeRootSrcFile(self: *Module, root_scope: *Scope.File) !void {...@@ -1580,6 +1580,8 @@ fn analyzeRootSrcFile(self: *Module, root_scope: *Scope.File) !void {
1580 }1580 }
1581 }1581 }
1582 }1582 }
1583 } else {
1584 std.debug.panic("TODO: analyzeRootSrcFile {}", .{src_decl.tag});
1583 }1585 }
1584 // TODO also look for global variable declarations1586 // TODO also look for global variable declarations
1585 // TODO also look for comptime blocks and exported globals1587 // TODO also look for comptime blocks and exported globals
src-self-hosted/astgen.zig+31-58
...@@ -335,76 +335,49 @@ fn varDecl(...@@ -335,76 +335,49 @@ fn varDecl(
335 // Depending on the type of AST the initialization expression is, we may need an lvalue335 // Depending on the type of AST the initialization expression is, we may need an lvalue
336 // or an rvalue as a result location. If it is an rvalue, we can use the instruction as336 // or an rvalue as a result location. If it is an rvalue, we can use the instruction as
337 // the variable, no memory location needed.337 // the variable, no memory location needed.
338 if (nodeMayNeedMemoryLocation(init_node)) {338 const result_loc = if (nodeMayNeedMemoryLocation(init_node)) r: {
339 if (node.getTrailer("type_node")) |type_node| {339 if (node.getTrailer("type_node")) |type_node| {
340 const type_inst = try typeExpr(mod, scope, type_node);340 const type_inst = try typeExpr(mod, scope, type_node);
341 const alloc = try addZIRUnOp(mod, scope, name_src, .alloc, type_inst);341 const alloc = try addZIRUnOp(mod, scope, name_src, .alloc, type_inst);
342 const result_loc: ResultLoc = .{ .ptr = alloc };342 break :r ResultLoc{ .ptr = alloc };
343 const init_inst = try expr(mod, scope, result_loc, init_node);
344 const sub_scope = try block_arena.create(Scope.LocalVal);
345 sub_scope.* = .{
346 .parent = scope,
347 .gen_zir = scope.getGenZIR(),
348 .name = ident_name,
349 .inst = init_inst,
350 };
351 return &sub_scope.base;
352 } else {343 } else {
353 const alloc = try addZIRNoOpT(mod, scope, name_src, .alloc_inferred);344 const alloc = try addZIRNoOpT(mod, scope, name_src, .alloc_inferred);
354 const result_loc: ResultLoc = .{ .inferred_ptr = alloc };345 break :r ResultLoc{ .inferred_ptr = alloc };
355 const init_inst = try expr(mod, scope, result_loc, init_node);
356 const sub_scope = try block_arena.create(Scope.LocalVal);
357 sub_scope.* = .{
358 .parent = scope,
359 .gen_zir = scope.getGenZIR(),
360 .name = ident_name,
361 .inst = init_inst,
362 };
363 return &sub_scope.base;
364 }346 }
365 } else {347 } else r: {
366 const result_loc: ResultLoc = if (node.getTrailer("type_node")) |type_node|348 if (node.getTrailer("type_node")) |type_node|
367 .{ .ty = try typeExpr(mod, scope, type_node) }349 break :r ResultLoc{ .ty = try typeExpr(mod, scope, type_node) }
368 else350 else
369 .none;351 break :r .none;
370 const init_inst = try expr(mod, scope, result_loc, init_node);352 };
371 const sub_scope = try block_arena.create(Scope.LocalVal);353 const init_inst = try expr(mod, scope, result_loc, init_node);
372 sub_scope.* = .{354 const sub_scope = try block_arena.create(Scope.LocalVal);
373 .parent = scope,355 sub_scope.* = .{
374 .gen_zir = scope.getGenZIR(),356 .parent = scope,
375 .name = ident_name,357 .gen_zir = scope.getGenZIR(),
376 .inst = init_inst,358 .name = ident_name,
377 };359 .inst = init_inst,
378 return &sub_scope.base;360 };
379 }361 return &sub_scope.base;
380 },362 },
381 .Keyword_var => {363 .Keyword_var => {
382 if (node.getTrailer("type_node")) |type_node| {364 const var_data: struct { result_loc: ResultLoc, alloc: *zir.Inst } = if (node.getTrailer("type_node")) |type_node| a: {
383 const type_inst = try typeExpr(mod, scope, type_node);365 const type_inst = try typeExpr(mod, scope, type_node);
384 const alloc = try addZIRUnOp(mod, scope, name_src, .alloc, type_inst);366 const alloc = try addZIRUnOp(mod, scope, name_src, .alloc, type_inst);
385 const result_loc: ResultLoc = .{ .ptr = alloc };367 break :a .{ .alloc = try addZIRUnOp(mod, scope, name_src, .alloc, type_inst), .result_loc = .{ .ptr = alloc } };
386 const init_inst = try expr(mod, scope, result_loc, init_node);368 } else a: {
387 const sub_scope = try block_arena.create(Scope.LocalPtr);
388 sub_scope.* = .{
389 .parent = scope,
390 .gen_zir = scope.getGenZIR(),
391 .name = ident_name,
392 .ptr = alloc,
393 };
394 return &sub_scope.base;
395 } else {
396 const alloc = try addZIRNoOp(mod, scope, name_src, .alloc_inferred);369 const alloc = try addZIRNoOp(mod, scope, name_src, .alloc_inferred);
397 const result_loc = .{ .inferred_ptr = alloc.castTag(.alloc_inferred).? };370 break :a .{ .alloc = alloc, .result_loc = .{ .inferred_ptr = alloc.castTag(.alloc_inferred).? } };
398 const init_inst = try expr(mod, scope, result_loc, init_node);371 };
399 const sub_scope = try block_arena.create(Scope.LocalPtr);372 const init_inst = try expr(mod, scope, var_data.result_loc, init_node);
400 sub_scope.* = .{373 const sub_scope = try block_arena.create(Scope.LocalPtr);
401 .parent = scope,374 sub_scope.* = .{
402 .gen_zir = scope.getGenZIR(),375 .parent = scope,
403 .name = ident_name,376 .gen_zir = scope.getGenZIR(),
404 .ptr = alloc,377 .name = ident_name,
405 };378 .ptr = var_data.alloc,
406 return &sub_scope.base;379 };
407 }380 return &sub_scope.base;
408 },381 },
409 else => unreachable,382 else => unreachable,
410 }383 }
src-self-hosted/codegen/c.zig+2-2
...@@ -11,8 +11,8 @@ const C = link.File.C;...@@ -11,8 +11,8 @@ const C = link.File.C;
11const Decl = Module.Decl;11const Decl = Module.Decl;
12const mem = std.mem;12const mem = std.mem;
1313
14/// Maps a name from Zig source to C. This will always give the same output for14/// Maps a name from Zig source to C. Currently, this will always give the same
15/// any given input.15/// output for any given input, sometimes resulting in broken identifiers.
16fn map(allocator: *std.mem.Allocator, name: []const u8) ![]const u8 {16fn map(allocator: *std.mem.Allocator, name: []const u8) ![]const u8 {
17 return allocator.dupe(u8, name);17 return allocator.dupe(u8, name);
18}18}