handle_loop_statement.cpp 4.5 KB

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