handle_loop_statement.cpp 4.2 KB

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