handle_loop_statement.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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::Node /*parse_node*/)
  8. -> bool {
  9. return true;
  10. }
  11. auto HandleBreakStatementStart(Context& context, Parse::Node 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.AddNode(SemIR::Branch(parse_node, stack.back().break_target));
  20. }
  21. context.node_block_stack().Pop();
  22. context.node_block_stack().PushUnreachable();
  23. return true;
  24. }
  25. auto HandleContinueStatement(Context& /*context*/, Parse::Node /*parse_node*/)
  26. -> bool {
  27. return true;
  28. }
  29. auto HandleContinueStatementStart(Context& context, Parse::Node 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.AddNode(SemIR::Branch(parse_node, stack.back().continue_target));
  38. }
  39. context.node_block_stack().Pop();
  40. context.node_block_stack().PushUnreachable();
  41. return true;
  42. }
  43. auto HandleForHeader(Context& context, Parse::Node parse_node) -> bool {
  44. return context.TODO(parse_node, "HandleForHeader");
  45. }
  46. auto HandleForHeaderStart(Context& context, Parse::Node parse_node) -> bool {
  47. return context.TODO(parse_node, "HandleForHeaderStart");
  48. }
  49. auto HandleForIn(Context& context, Parse::Node parse_node) -> bool {
  50. return context.TODO(parse_node, "HandleForIn");
  51. }
  52. auto HandleForStatement(Context& context, Parse::Node parse_node) -> bool {
  53. return context.TODO(parse_node, "HandleForStatement");
  54. }
  55. auto HandleWhileConditionStart(Context& context, Parse::Node parse_node)
  56. -> bool {
  57. // Branch to the loop header block. Note that we create a new block here even
  58. // if the current block is empty; this ensures that the loop always has a
  59. // preheader block.
  60. auto loop_header_id = context.AddDominatedBlockAndBranch(parse_node);
  61. context.node_block_stack().Pop();
  62. // Start emitting the loop header block.
  63. context.node_block_stack().Push(loop_header_id);
  64. context.AddCurrentCodeBlockToFunction();
  65. context.node_stack().Push(parse_node, loop_header_id);
  66. return true;
  67. }
  68. auto HandleWhileCondition(Context& context, Parse::Node parse_node) -> bool {
  69. auto cond_value_id = context.node_stack().PopExpression();
  70. auto loop_header_id =
  71. context.node_stack().Peek<Parse::NodeKind::WhileConditionStart>();
  72. cond_value_id = ConvertToBoolValue(context, parse_node, cond_value_id);
  73. // Branch to either the loop body or the loop exit block.
  74. auto loop_body_id =
  75. context.AddDominatedBlockAndBranchIf(parse_node, cond_value_id);
  76. auto loop_exit_id = context.AddDominatedBlockAndBranch(parse_node);
  77. context.node_block_stack().Pop();
  78. // Start emitting the loop body.
  79. context.node_block_stack().Push(loop_body_id);
  80. context.AddCurrentCodeBlockToFunction();
  81. context.break_continue_stack().push_back(
  82. {.break_target = loop_exit_id, .continue_target = loop_header_id});
  83. context.node_stack().Push(parse_node, loop_exit_id);
  84. return true;
  85. }
  86. auto HandleWhileStatement(Context& context, Parse::Node parse_node) -> bool {
  87. auto loop_exit_id =
  88. context.node_stack().Pop<Parse::NodeKind::WhileCondition>();
  89. auto loop_header_id =
  90. context.node_stack().Pop<Parse::NodeKind::WhileConditionStart>();
  91. context.break_continue_stack().pop_back();
  92. // Add the loop backedge.
  93. context.AddNode(SemIR::Branch(parse_node, loop_header_id));
  94. context.node_block_stack().Pop();
  95. // Start emitting the loop exit block.
  96. context.node_block_stack().Push(loop_exit_id);
  97. context.AddCurrentCodeBlockToFunction();
  98. return true;
  99. }
  100. } // namespace Carbon::Check