handle_loop_statement.cpp 4.6 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/convert.h"
  6. #include "toolchain/check/handle.h"
  7. namespace Carbon::Check {
  8. // `while`
  9. // -------
  10. auto HandleWhileConditionStart(Context& context,
  11. Parse::WhileConditionStartId node_id) -> bool {
  12. // Branch to the loop header block. Note that we create a new block here even
  13. // if the current block is empty; this ensures that the loop always has a
  14. // preheader block.
  15. auto loop_header_id = context.AddDominatedBlockAndBranch(node_id);
  16. context.inst_block_stack().Pop();
  17. // Start emitting the loop header block.
  18. context.inst_block_stack().Push(loop_header_id);
  19. context.AddCurrentCodeBlockToFunction();
  20. context.node_stack().Push(node_id, loop_header_id);
  21. return true;
  22. }
  23. auto HandleWhileCondition(Context& context, Parse::WhileConditionId node_id)
  24. -> bool {
  25. auto cond_value_id = context.node_stack().PopExpr();
  26. auto loop_header_id =
  27. context.node_stack().Peek<Parse::NodeKind::WhileConditionStart>();
  28. cond_value_id = ConvertToBoolValue(context, node_id, cond_value_id);
  29. // Branch to either the loop body or the loop exit block.
  30. auto loop_body_id =
  31. context.AddDominatedBlockAndBranchIf(node_id, cond_value_id);
  32. auto loop_exit_id = context.AddDominatedBlockAndBranch(node_id);
  33. context.inst_block_stack().Pop();
  34. // Start emitting the loop body.
  35. context.inst_block_stack().Push(loop_body_id);
  36. context.AddCurrentCodeBlockToFunction();
  37. context.break_continue_stack().push_back(
  38. {.break_target = loop_exit_id, .continue_target = loop_header_id});
  39. context.node_stack().Push(node_id, loop_exit_id);
  40. return true;
  41. }
  42. auto HandleWhileStatement(Context& context, Parse::WhileStatementId node_id)
  43. -> bool {
  44. auto loop_exit_id =
  45. context.node_stack().Pop<Parse::NodeKind::WhileCondition>();
  46. auto loop_header_id =
  47. context.node_stack().Pop<Parse::NodeKind::WhileConditionStart>();
  48. context.break_continue_stack().pop_back();
  49. // Add the loop backedge.
  50. context.AddInst<SemIR::Branch>(node_id, {.target_id = loop_header_id});
  51. context.inst_block_stack().Pop();
  52. // Start emitting the loop exit block.
  53. context.inst_block_stack().Push(loop_exit_id);
  54. context.AddCurrentCodeBlockToFunction();
  55. return true;
  56. }
  57. // `for`
  58. // -----
  59. auto HandleForHeaderStart(Context& context, Parse::ForHeaderStartId node_id)
  60. -> bool {
  61. return context.TODO(node_id, "HandleForHeaderStart");
  62. }
  63. auto HandleForIn(Context& context, Parse::ForInId node_id) -> bool {
  64. context.decl_introducer_state_stack().Pop(DeclIntroducerState::Var);
  65. return context.TODO(node_id, "HandleForIn");
  66. }
  67. auto HandleForHeader(Context& context, Parse::ForHeaderId node_id) -> bool {
  68. return context.TODO(node_id, "HandleForHeader");
  69. }
  70. auto HandleForStatement(Context& context, Parse::ForStatementId node_id)
  71. -> bool {
  72. return context.TODO(node_id, "HandleForStatement");
  73. }
  74. // `break`
  75. // -------
  76. auto HandleBreakStatementStart(Context& context,
  77. Parse::BreakStatementStartId node_id) -> bool {
  78. auto& stack = context.break_continue_stack();
  79. if (stack.empty()) {
  80. CARBON_DIAGNOSTIC(BreakOutsideLoop, Error,
  81. "`break` can only be used in a loop.");
  82. context.emitter().Emit(node_id, BreakOutsideLoop);
  83. } else {
  84. context.AddInst<SemIR::Branch>(node_id,
  85. {.target_id = stack.back().break_target});
  86. }
  87. context.inst_block_stack().Pop();
  88. context.inst_block_stack().PushUnreachable();
  89. return true;
  90. }
  91. auto HandleBreakStatement(Context& /*context*/,
  92. Parse::BreakStatementId /*node_id*/) -> bool {
  93. return true;
  94. }
  95. // `continue`
  96. // ----------
  97. auto HandleContinueStatementStart(Context& context,
  98. 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. context.AddInst<SemIR::Branch>(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 HandleContinueStatement(Context& /*context*/,
  114. Parse::ContinueStatementId /*node_id*/) -> bool {
  115. return true;
  116. }
  117. } // namespace Carbon::Check