handle_loop_statement.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 HandleParseNode(Context& context, Parse::WhileConditionStartId node_id)
  11. -> 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 HandleParseNode(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 HandleParseNode(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 HandleParseNode(Context& context, Parse::ForHeaderStartId node_id)
  60. -> bool {
  61. return context.TODO(node_id, "HandleForHeaderStart");
  62. }
  63. auto HandleParseNode(Context& context, Parse::ForInId node_id) -> bool {
  64. context.decl_introducer_state_stack().Pop<Lex::TokenKind::Var>();
  65. return context.TODO(node_id, "HandleForIn");
  66. }
  67. auto HandleParseNode(Context& context, Parse::ForHeaderId node_id) -> bool {
  68. return context.TODO(node_id, "HandleForHeader");
  69. }
  70. auto HandleParseNode(Context& context, Parse::ForStatementId node_id) -> bool {
  71. return context.TODO(node_id, "HandleForStatement");
  72. }
  73. // `break`
  74. // -------
  75. auto HandleParseNode(Context& context, Parse::BreakStatementStartId node_id)
  76. -> 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<SemIR::Branch>(node_id,
  84. {.target_id = stack.back().break_target});
  85. }
  86. context.inst_block_stack().Pop();
  87. context.inst_block_stack().PushUnreachable();
  88. return true;
  89. }
  90. auto HandleParseNode(Context& /*context*/, Parse::BreakStatementId /*node_id*/)
  91. -> bool {
  92. return true;
  93. }
  94. // `continue`
  95. // ----------
  96. auto HandleParseNode(Context& context, 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<SemIR::Branch>(node_id,
  105. {.target_id = stack.back().continue_target});
  106. }
  107. context.inst_block_stack().Pop();
  108. context.inst_block_stack().PushUnreachable();
  109. return true;
  110. }
  111. auto HandleParseNode(Context& /*context*/,
  112. Parse::ContinueStatementId /*node_id*/) -> bool {
  113. return true;
  114. }
  115. } // namespace Carbon::Check