authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-05-29 18:12:38-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-05-30 12:03:53-07:00
log7e98b047ddc51062a32cbf2fb852d38e5023886c
tree17e115e890f3e24dd710eb6621a097d28610a490
parent9da3a058d82573efeaf12fe61ab6a312649175ec

AstGen: simplify function return type expressions

This check for primitives is already handled by the generic logic that checks if the body ends up being empty. I kept this commit in the git history in case we ever want that nodePrimitive function again in the future, it might be useful.

1 files changed, 7 insertions(+), 35 deletions(-)

src/AstGen.zig+7-35
...@@ -3562,17 +3562,14 @@ fn fnDecl(...@@ -3562,17 +3562,14 @@ fn fnDecl(
35623562
3563 var ret_gz = decl_gz.makeSubBlock(params_scope);3563 var ret_gz = decl_gz.makeSubBlock(params_scope);
3564 defer ret_gz.unstack();3564 defer ret_gz.unstack();
3565 const ret_ref: Zir.Inst.Ref = switch (nodePrimitive(tree, fn_proto.ast.return_type)) {3565 const ret_ref: Zir.Inst.Ref = inst: {
3566 .none => inst: {3566 const inst = try expr(&ret_gz, params_scope, coerced_type_rl, fn_proto.ast.return_type);
3567 const inst = try expr(&ret_gz, params_scope, coerced_type_rl, fn_proto.ast.return_type);3567 if (ret_gz.instructionsSlice().len == 0) {
3568 if (ret_gz.instructionsSlice().len == 0) {3568 // In this case we will send a len=0 body which can be encoded more efficiently.
3569 // In this case we will send a len=0 body which can be encoded more efficiently.
3570 break :inst inst;
3571 }
3572 _ = try ret_gz.addBreak(.break_inline, 0, inst);
3573 break :inst inst;3569 break :inst inst;
3574 },3570 }
3575 else => |p| p,3571 _ = try ret_gz.addBreak(.break_inline, 0, inst);
3572 break :inst inst;
3576 };3573 };
35773574
3578 const func_inst: Zir.Inst.Ref = if (body_node == 0) func: {3575 const func_inst: Zir.Inst.Ref = if (body_node == 0) func: {
...@@ -9025,31 +9022,6 @@ fn nodeImpliesComptimeOnly(tree: *const Ast, start_node: Ast.Node.Index) bool {...@@ -9025,31 +9022,6 @@ fn nodeImpliesComptimeOnly(tree: *const Ast, start_node: Ast.Node.Index) bool {
9025 }9022 }
9026}9023}
90279024
9028fn nodePrimitive(tree: *const Ast, start_node: Ast.Node.Index) Zir.Inst.Ref {
9029 const node_tags = tree.nodes.items(.tag);
9030 const node_datas = tree.nodes.items(.data);
9031
9032 var node = start_node;
9033 while (true) {
9034 switch (node_tags[node]) {
9035 // Forward the question to the LHS sub-expression.
9036 .grouped_expression => node = node_datas[node].lhs,
9037
9038 .identifier => {
9039 const main_tokens = tree.nodes.items(.main_token);
9040 const ident_bytes = tree.tokenSlice(main_tokens[node]);
9041 if (primitives.get(ident_bytes)) |primitive| {
9042 return primitive;
9043 } else {
9044 return .none;
9045 }
9046 },
9047
9048 else => return .none,
9049 }
9050 }
9051}
9052
9053/// Applies `rl` semantics to `result`. Expressions which do not do their own handling of9025/// Applies `rl` semantics to `result`. Expressions which do not do their own handling of
9054/// result locations must call this function on their result.9026/// result locations must call this function on their result.
9055/// As an example, if the `ResultLoc` is `ptr`, it will write the result to the pointer.9027/// As an example, if the `ResultLoc` is `ptr`, it will write the result to the pointer.