authorgravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-08-13 12:23:37-04:00
committergravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-08-16 20:32:50-04:00
log692f38c2509da047e8be15fd7a932aa204f33fd3
tree18251792a839b32f5961df0babd84aec1ec6f19e
parent8d8d568854d33be7bcc2bc9874029d1082914af7
signature Commit is signed but in an unrecognized format.

astgen: minor cleanup


1 files changed, 36 insertions(+), 60 deletions(-)

src-self-hosted/astgen.zig+36-60
...@@ -335,76 +335,52 @@ fn varDecl(...@@ -335,76 +335,52 @@ 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 alloc = 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 break :a try addZIRUnOp(mod, scope, name_src, .alloc, type_inst);
385 const result_loc: ResultLoc = .{ .ptr = alloc };367 } else try addZIRNoOp(mod, scope, name_src, .alloc_inferred);
386 const init_inst = try expr(mod, scope, result_loc, init_node);368 const result_loc = r: {
387 const sub_scope = try block_arena.create(Scope.LocalPtr);369 if (node.getTrailer("type_node")) |type_node| {
388 sub_scope.* = .{370 break :r ResultLoc{ .ptr = alloc };
389 .parent = scope,371 } else {
390 .gen_zir = scope.getGenZIR(),372 break :r ResultLoc{ .inferred_ptr = alloc.castTag(.alloc_inferred).? };
391 .name = ident_name,373 }
392 .ptr = alloc,374 };
393 };375 const init_inst = try expr(mod, scope, result_loc, init_node);
394 return &sub_scope.base;376 const sub_scope = try block_arena.create(Scope.LocalPtr);
395 } else {377 sub_scope.* = .{
396 const alloc = try addZIRNoOp(mod, scope, name_src, .alloc_inferred);378 .parent = scope,
397 const result_loc = .{ .inferred_ptr = alloc.castTag(.alloc_inferred).? };379 .gen_zir = scope.getGenZIR(),
398 const init_inst = try expr(mod, scope, result_loc, init_node);380 .name = ident_name,
399 const sub_scope = try block_arena.create(Scope.LocalPtr);381 .ptr = alloc,
400 sub_scope.* = .{382 };
401 .parent = scope,383 return &sub_scope.base;
402 .gen_zir = scope.getGenZIR(),
403 .name = ident_name,
404 .ptr = alloc,
405 };
406 return &sub_scope.base;
407 }
408 },384 },
409 else => unreachable,385 else => unreachable,
410 }386 }