authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-02-02 13:23:18-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-02-02 13:23:18-05:00
logb78c91951a1db34c615a011e0444608285f1a74c
tree2cae0609ec2670a4a1e1adb7905ae495abc9a238
parentcd08c1f3be278366ee69c3cf4ab1091eb884e264

remove ability to mark if and switch as inline

if and switch are implicitly inline if the condition/target expression is known at compile time. instead of: ``` inline if (condition) ... inline switch (target) ... ``` one can use: ``` if (comptime condition) ... switch (comptime target) ... ```

7 files changed, 29 insertions(+), 64 deletions(-)

doc/langref.md+3-3
......@@ -73,7 +73,7 @@ BlockExpression = IfExpression | Block | WhileExpression | ForExpression | Switc
7373
7474CompTimeExpression = option("comptime") Expression
7575
76SwitchExpression = option("inline") "switch" "(" Expression ")" "{" many(SwitchProng) "}"
76SwitchExpression = "switch" "(" Expression ")" "{" many(SwitchProng) "}"
7777
7878SwitchProng = (list(SwitchItem, ",") | "else") "=>" option("|" option("*") Symbol "|") Expression ","
7979
......@@ -91,9 +91,9 @@ Defer = option("%" | "?") "defer" Expression
9191
9292IfExpression = IfVarExpression | IfBoolExpression
9393
94IfBoolExpression = option("inline") "if" "(" Expression ")" Expression option(Else)
94IfBoolExpression = "if" "(" Expression ")" Expression option(Else)
9595
96IfVarExpression = option("inline") "if" "(" ("const" | "var") option("*") Symbol option(":" TypeExpr) "?=" Expression ")" Expression Option(Else)
96IfVarExpression = "if" "(" ("const" | "var") option("*") Symbol option(":" TypeExpr) "?=" Expression ")" Expression Option(Else)
9797
9898Else = "else" Expression
9999
src/all_types.hpp-5
......@@ -509,7 +509,6 @@ struct AstNodeIfBoolExpr {
509509 AstNode *condition;
510510 AstNode *then_block;
511511 AstNode *else_node; // null, block node, or other if expr node
512 bool is_inline;
513512};
514513
515514struct AstNodeIfVarExpr {
......@@ -517,7 +516,6 @@ struct AstNodeIfVarExpr {
517516 AstNode *then_block;
518517 AstNode *else_node; // null, block node, or other if expr node
519518 bool var_is_ptr;
520 bool is_inline;
521519};
522520
523521struct AstNodeWhileExpr {
......@@ -539,7 +537,6 @@ struct AstNodeForExpr {
539537struct AstNodeSwitchExpr {
540538 AstNode *expr;
541539 ZigList<AstNode *> prongs;
542 bool is_inline;
543540};
544541
545542struct AstNodeSwitchProng {
......@@ -677,11 +674,9 @@ struct AstNodeBoolLiteral {
677674};
678675
679676struct AstNodeBreakExpr {
680 bool is_inline; // TODO
681677};
682678
683679struct AstNodeContinueExpr {
684 bool is_inline; // TODO
685680};
686681
687682struct AstNodeArrayType {
src/ast_render.cpp+2-4
......@@ -844,14 +844,12 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
844844 }
845845 case NodeTypeBreak:
846846 {
847 const char *inline_str = node->data.break_expr.is_inline ? "inline " : "";
848 fprintf(ar->f, "%sbreak", inline_str);
847 fprintf(ar->f, "break");
849848 break;
850849 }
851850 case NodeTypeContinue:
852851 {
853 const char *inline_str = node->data.continue_expr.is_inline ? "inline " : "";
854 fprintf(ar->f, "%scontinue", inline_str);
852 fprintf(ar->f, "continue");
855853 break;
856854 }
857855 case NodeTypeSliceExpr:
src/ir.cpp+5-5
......@@ -4148,7 +4148,7 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode
41484148 return condition;
41494149
41504150 IrInstruction *is_comptime;
4151 if (ir_should_inline(irb->exec, scope) || node->data.if_bool_expr.is_inline) {
4151 if (ir_should_inline(irb->exec, scope)) {
41524152 is_comptime = ir_build_const_bool(irb, scope, node, true);
41534153 } else {
41544154 is_comptime = ir_build_test_comptime(irb, scope, node, condition);
......@@ -4695,7 +4695,7 @@ static IrInstruction *ir_gen_if_var_expr(IrBuilder *irb, Scope *scope, AstNode *
46954695 IrBasicBlock *endif_block = ir_build_basic_block(irb, scope, "MaybeEndIf");
46964696
46974697 IrInstruction *is_comptime;
4698 if (ir_should_inline(irb->exec, scope) || node->data.if_var_expr.is_inline) {
4698 if (ir_should_inline(irb->exec, scope)) {
46994699 is_comptime = ir_build_const_bool(irb, scope, node, true);
47004700 } else {
47014701 is_comptime = ir_build_test_comptime(irb, scope, node, is_non_null);
......@@ -4807,7 +4807,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
48074807 ZigList<IrInstructionSwitchBrCase> cases = {0};
48084808
48094809 IrInstruction *is_comptime;
4810 if (ir_should_inline(irb->exec, scope) || node->data.switch_expr.is_inline) {
4810 if (ir_should_inline(irb->exec, scope)) {
48114811 is_comptime = ir_build_const_bool(irb, scope, node, true);
48124812 } else {
48134813 is_comptime = ir_build_test_comptime(irb, scope, node, target_value);
......@@ -5002,7 +5002,7 @@ static IrInstruction *ir_gen_break(IrBuilder *irb, Scope *scope, AstNode *node)
50025002 LoopStackItem *loop_stack_item = &irb->loop_stack.last();
50035003
50045004 IrInstruction *is_comptime;
5005 if (ir_should_inline(irb->exec, scope) || node->data.break_expr.is_inline) {
5005 if (ir_should_inline(irb->exec, scope)) {
50065006 is_comptime = ir_build_const_bool(irb, scope, node, true);
50075007 } else {
50085008 is_comptime = loop_stack_item->is_comptime;
......@@ -5025,7 +5025,7 @@ static IrInstruction *ir_gen_continue(IrBuilder *irb, Scope *scope, AstNode *nod
50255025 LoopStackItem *loop_stack_item = &irb->loop_stack.last();
50265026
50275027 IrInstruction *is_comptime;
5028 if (ir_should_inline(irb->exec, scope) || node->data.continue_expr.is_inline) {
5028 if (ir_should_inline(irb->exec, scope)) {
50295029 is_comptime = ir_build_const_bool(irb, scope, node, true);
50305030 } else {
50315031 is_comptime = loop_stack_item->is_comptime;
src/parser.cpp+12-43
......@@ -1313,30 +1313,17 @@ static AstNode *ast_parse_else(ParseContext *pc, size_t *token_index, bool manda
13131313
13141314/*
13151315IfExpression : IfVarExpression | IfBoolExpression
1316IfBoolExpression = option("inline") "if" "(" Expression ")" Expression option(Else)
1317IfVarExpression = option("inline") "if" "(" ("const" | "var") option("*") Symbol option(":" TypeExpr) "?=" Expression ")" Expression Option(Else)
1316IfBoolExpression = "if" "(" Expression ")" Expression option(Else)
1317IfVarExpression = "if" "(" ("const" | "var") option("*") Symbol option(":" TypeExpr) "?=" Expression ")" Expression Option(Else)
13181318*/
13191319static AstNode *ast_parse_if_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
1320 Token *first_token = &pc->tokens->at(*token_index);
1321 Token *if_tok;
1320 Token *if_token = &pc->tokens->at(*token_index);
13221321
1323 bool is_inline;
1324 if (first_token->id == TokenIdKeywordInline) {
1325 if_tok = &pc->tokens->at(*token_index + 1);
1326 if (if_tok->id == TokenIdKeywordIf) {
1327 is_inline = true;
1328 *token_index += 2;
1329 } else if (mandatory) {
1330 ast_expect_token(pc, if_tok, TokenIdKeywordIf);
1331 } else {
1332 return nullptr;
1333 }
1334 } else if (first_token->id == TokenIdKeywordIf) {
1335 if_tok = first_token;
1336 is_inline = false;
1322 if (if_token->id == TokenIdKeywordIf) {
13371323 *token_index += 1;
13381324 } else if (mandatory) {
1339 ast_expect_token(pc, first_token, TokenIdKeywordIf);
1325 ast_expect_token(pc, if_token, TokenIdKeywordIf);
1326 zig_unreachable();
13401327 } else {
13411328 return nullptr;
13421329 }
......@@ -1345,8 +1332,7 @@ static AstNode *ast_parse_if_expr(ParseContext *pc, size_t *token_index, bool ma
13451332
13461333 Token *token = &pc->tokens->at(*token_index);
13471334 if (token->id == TokenIdKeywordConst || token->id == TokenIdKeywordVar) {
1348 AstNode *node = ast_create_node(pc, NodeTypeIfVarExpr, if_tok);
1349 node->data.if_var_expr.is_inline = is_inline;
1335 AstNode *node = ast_create_node(pc, NodeTypeIfVarExpr, if_token);
13501336 node->data.if_var_expr.var_decl.is_const = (token->id == TokenIdKeywordConst);
13511337 *token_index += 1;
13521338
......@@ -1383,8 +1369,7 @@ static AstNode *ast_parse_if_expr(ParseContext *pc, size_t *token_index, bool ma
13831369
13841370 return node;
13851371 } else {
1386 AstNode *node = ast_create_node(pc, NodeTypeIfBoolExpr, if_tok);
1387 node->data.if_bool_expr.is_inline = is_inline;
1372 AstNode *node = ast_create_node(pc, NodeTypeIfBoolExpr, if_token);
13881373 node->data.if_bool_expr.condition = ast_parse_expression(pc, token_index, true);
13891374 ast_eat_token(pc, token_index, TokenIdRParen);
13901375 node->data.if_bool_expr.then_block = ast_parse_expression(pc, token_index, true);
......@@ -1700,38 +1685,22 @@ static AstNode *ast_parse_for_expr(ParseContext *pc, size_t *token_index, bool m
17001685}
17011686
17021687/*
1703SwitchExpression = option("inline") "switch" "(" Expression ")" "{" many(SwitchProng) "}"
1688SwitchExpression = "switch" "(" Expression ")" "{" many(SwitchProng) "}"
17041689SwitchProng = (list(SwitchItem, ",") | "else") "=>" option("|" option("*") Symbol "|") Expression ","
17051690SwitchItem : Expression | (Expression "..." Expression)
17061691*/
17071692static AstNode *ast_parse_switch_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
1708 Token *first_token = &pc->tokens->at(*token_index);
1709 Token *switch_token;
1710 bool is_inline;
1711 if (first_token->id == TokenIdKeywordInline) {
1712 is_inline = true;
1713 switch_token = &pc->tokens->at(*token_index + 1);
1714 if (switch_token->id == TokenIdKeywordSwitch) {
1715 *token_index += 2;
1716 } else if (mandatory) {
1717 ast_expect_token(pc, first_token, TokenIdKeywordSwitch);
1718 zig_unreachable();
1719 } else {
1720 return nullptr;
1721 }
1722 } else if (first_token->id == TokenIdKeywordSwitch) {
1723 is_inline = false;
1724 switch_token = first_token;
1693 Token *switch_token = &pc->tokens->at(*token_index);
1694 if (switch_token->id == TokenIdKeywordSwitch) {
17251695 *token_index += 1;
17261696 } else if (mandatory) {
1727 ast_expect_token(pc, first_token, TokenIdKeywordSwitch);
1697 ast_expect_token(pc, switch_token, TokenIdKeywordSwitch);
17281698 zig_unreachable();
17291699 } else {
17301700 return nullptr;
17311701 }
17321702
17331703 AstNode *node = ast_create_node(pc, NodeTypeSwitchExpr, switch_token);
1734 node->data.switch_expr.is_inline = is_inline;
17351704
17361705 ast_eat_token(pc, token_index, TokenIdLParen);
17371706 node->data.switch_expr.expr = ast_parse_expression(pc, token_index, true);
std/io.zig+1-1
......@@ -150,7 +150,7 @@ pub const OutStream = struct {
150150 @compileError("Incomplete format string: " ++ format);
151151 }
152152 }
153 inline if (start_index < format.len) {
153 if (start_index < format.len) {
154154 %return self.write(format[start_index...format.len]);
155155 }
156156 %return self.flush();
test/cases/switch.zig+6-3
......@@ -33,18 +33,21 @@ fn testSwitchWithAllRanges(x: u32, y: u32) -> u32 {
3333 }
3434}
3535
36fn inlineSwitch() {
36fn implicitComptimeSwitch() {
3737 @setFnTest(this);
3838
3939 const x = 3 + 4;
40 const result = inline switch (x) {
40 const result = switch (x) {
4141 3 => 10,
4242 4 => 11,
4343 5, 6 => 12,
4444 7, 8 => 13,
4545 else => 14,
4646 };
47 assert(result + 1 == 14);
47
48 comptime {
49 assert(result + 1 == 14);
50 }
4851}
4952
5053fn switchOnEnum() {