handle_loop_statement.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/check/context.h"
  5. #include "toolchain/check/control_flow.h"
  6. #include "toolchain/check/convert.h"
  7. #include "toolchain/check/handle.h"
  8. #include "toolchain/check/inst.h"
  9. namespace Carbon::Check {
  10. // `while`
  11. // -------
  12. auto HandleParseNode(Context& context, Parse::WhileConditionStartId node_id)
  13. -> bool {
  14. // Branch to the loop header block. Note that we create a new block here even
  15. // if the current block is empty; this ensures that the loop always has a
  16. // preheader block.
  17. auto loop_header_id = AddDominatedBlockAndBranch(context, node_id);
  18. context.inst_block_stack().Pop();
  19. // Start emitting the loop header block.
  20. context.inst_block_stack().Push(loop_header_id);
  21. context.region_stack().AddToRegion(loop_header_id, node_id);
  22. context.node_stack().Push(node_id, loop_header_id);
  23. return true;
  24. }
  25. auto HandleParseNode(Context& context, Parse::WhileConditionId node_id)
  26. -> bool {
  27. auto cond_value_id = context.node_stack().PopExpr();
  28. auto loop_header_id =
  29. context.node_stack().Peek<Parse::NodeKind::WhileConditionStart>();
  30. cond_value_id = ConvertToBoolValue(context, node_id, cond_value_id);
  31. // Branch to either the loop body or the loop exit block.
  32. auto loop_body_id =
  33. AddDominatedBlockAndBranchIf(context, node_id, cond_value_id);
  34. auto loop_exit_id = AddDominatedBlockAndBranch(context, node_id);
  35. context.inst_block_stack().Pop();
  36. // Start emitting the loop body.
  37. context.inst_block_stack().Push(loop_body_id);
  38. context.region_stack().AddToRegion(loop_body_id, node_id);
  39. context.break_continue_stack().push_back(
  40. {.break_target = loop_exit_id, .continue_target = loop_header_id});
  41. context.node_stack().Push(node_id, loop_exit_id);
  42. return true;
  43. }
  44. auto HandleParseNode(Context& context, Parse::WhileStatementId node_id)
  45. -> bool {
  46. auto loop_exit_id =
  47. context.node_stack().Pop<Parse::NodeKind::WhileCondition>();
  48. auto loop_header_id =
  49. context.node_stack().Pop<Parse::NodeKind::WhileConditionStart>();
  50. context.break_continue_stack().pop_back();
  51. // Add the loop backedge.
  52. AddInst<SemIR::Branch>(context, node_id, {.target_id = loop_header_id});
  53. context.inst_block_stack().Pop();
  54. // Start emitting the loop exit block.
  55. context.inst_block_stack().Push(loop_exit_id);
  56. context.region_stack().AddToRegion(loop_exit_id, node_id);
  57. return true;
  58. }
  59. // `for`
  60. // -----
  61. auto HandleParseNode(Context& context, Parse::ForHeaderStartId node_id)
  62. -> bool {
  63. return context.TODO(node_id, "HandleForHeaderStart");
  64. }
  65. auto HandleParseNode(Context& context, Parse::ForInId node_id) -> bool {
  66. context.decl_introducer_state_stack().Pop<Lex::TokenKind::Var>();
  67. return context.TODO(node_id, "HandleForIn");
  68. }
  69. auto HandleParseNode(Context& context, Parse::ForHeaderId node_id) -> bool {
  70. return context.TODO(node_id, "HandleForHeader");
  71. }
  72. auto HandleParseNode(Context& context, Parse::ForStatementId node_id) -> bool {
  73. return context.TODO(node_id, "HandleForStatement");
  74. }
  75. // `break`
  76. // -------
  77. auto HandleParseNode(Context& context, Parse::BreakStatementStartId node_id)
  78. -> bool {
  79. auto& stack = context.break_continue_stack();
  80. if (stack.empty()) {
  81. CARBON_DIAGNOSTIC(BreakOutsideLoop, Error,
  82. "`break` can only be used in a loop");
  83. context.emitter().Emit(node_id, BreakOutsideLoop);
  84. } else {
  85. AddInst<SemIR::Branch>(context, node_id,
  86. {.target_id = stack.back().break_target});
  87. }
  88. context.inst_block_stack().Pop();
  89. context.inst_block_stack().PushUnreachable();
  90. return true;
  91. }
  92. auto HandleParseNode(Context& /*context*/, Parse::BreakStatementId /*node_id*/)
  93. -> bool {
  94. return true;
  95. }
  96. // `continue`
  97. // ----------
  98. auto HandleParseNode(Context& context, Parse::ContinueStatementStartId node_id)
  99. -> bool {
  100. auto& stack = context.break_continue_stack();
  101. if (stack.empty()) {
  102. CARBON_DIAGNOSTIC(ContinueOutsideLoop, Error,
  103. "`continue` can only be used in a loop");
  104. context.emitter().Emit(node_id, ContinueOutsideLoop);
  105. } else {
  106. AddInst<SemIR::Branch>(context, node_id,
  107. {.target_id = stack.back().continue_target});
  108. }
  109. context.inst_block_stack().Pop();
  110. context.inst_block_stack().PushUnreachable();
  111. return true;
  112. }
  113. auto HandleParseNode(Context& /*context*/,
  114. Parse::ContinueStatementId /*node_id*/) -> bool {
  115. return true;
  116. }
  117. } // namespace Carbon::Check