handle_loop_statement.cpp 4.4 KB

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