handle_loop_statement.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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/call.h"
  5. #include "toolchain/check/context.h"
  6. #include "toolchain/check/control_flow.h"
  7. #include "toolchain/check/convert.h"
  8. #include "toolchain/check/core_identifier.h"
  9. #include "toolchain/check/full_pattern_stack.h"
  10. #include "toolchain/check/handle.h"
  11. #include "toolchain/check/inst.h"
  12. #include "toolchain/check/member_access.h"
  13. #include "toolchain/check/operator.h"
  14. #include "toolchain/check/pattern.h"
  15. #include "toolchain/check/pattern_match.h"
  16. #include "toolchain/check/type.h"
  17. #include "toolchain/sem_ir/absolute_node_id.h"
  18. #include "toolchain/sem_ir/ids.h"
  19. namespace Carbon::Check {
  20. // Starts emitting the loop header for a `while`-like looping construct. Returns
  21. // the loop header block ID.
  22. static auto StartLoopHeader(Context& context, Parse::NodeId node_id)
  23. -> SemIR::InstBlockId {
  24. // Branch to the loop header block. Note that we create a new block here even
  25. // if the current block is empty; this ensures that the loop always has a
  26. // preheader block.
  27. auto loop_header_id = AddDominatedBlockAndBranch(context, node_id);
  28. context.inst_block_stack().Pop();
  29. // Start emitting the loop header block.
  30. context.inst_block_stack().Push(loop_header_id);
  31. context.region_stack().AddToRegion(loop_header_id, node_id);
  32. return loop_header_id;
  33. }
  34. // Starts emitting the loop body for a `while`-like looping construct. Converts
  35. // `cond_value_id` to bool and branches to the loop body if it is `true` and to
  36. // the loop exit if it is `false`.
  37. static auto BranchAndStartLoopBody(Context& context, Parse::NodeId node_id,
  38. SemIR::InstBlockId loop_header_id,
  39. SemIR::InstId cond_value_id) -> void {
  40. cond_value_id = ConvertToBoolValue(context, node_id, cond_value_id);
  41. // Branch to either the loop body or the loop exit block.
  42. auto loop_body_id =
  43. AddDominatedBlockAndBranchIf(context, node_id, cond_value_id);
  44. auto loop_exit_id = AddDominatedBlockAndBranch(context, node_id);
  45. context.inst_block_stack().Pop();
  46. // Start emitting the loop body.
  47. context.inst_block_stack().Push(loop_body_id);
  48. context.region_stack().AddToRegion(loop_body_id, node_id);
  49. // Allow `break` and `continue` in this scope.
  50. context.break_continue_stack().push_back(
  51. {.break_target = loop_exit_id, .continue_target = loop_header_id});
  52. }
  53. // Finishes emitting the body for a `while`-like loop. Adds a back-edge to the
  54. // loop header, and starts emitting in the loop exit block.
  55. static auto FinishLoopBody(Context& context, Parse::NodeId node_id) -> void {
  56. auto blocks = context.break_continue_stack().pop_back_val();
  57. // Add the loop backedge.
  58. AddInst<SemIR::Branch>(context, node_id,
  59. {.target_id = blocks.continue_target});
  60. context.inst_block_stack().Pop();
  61. // Start emitting the loop exit block.
  62. context.inst_block_stack().Push(blocks.break_target);
  63. context.region_stack().AddToRegion(blocks.break_target, node_id);
  64. }
  65. // `while`
  66. // -------
  67. auto HandleParseNode(Context& context, Parse::WhileConditionStartId node_id)
  68. -> bool {
  69. context.node_stack().Push(node_id, StartLoopHeader(context, node_id));
  70. return true;
  71. }
  72. auto HandleParseNode(Context& context, Parse::WhileConditionId node_id)
  73. -> bool {
  74. auto cond_value_id = context.node_stack().PopExpr();
  75. auto loop_header_id =
  76. context.node_stack().Pop<Parse::NodeKind::WhileConditionStart>();
  77. // Branch to either the loop body or the loop exit block, and start emitting
  78. // the loop body.
  79. BranchAndStartLoopBody(context, node_id, loop_header_id, cond_value_id);
  80. return true;
  81. }
  82. auto HandleParseNode(Context& context, Parse::WhileStatementId node_id)
  83. -> bool {
  84. FinishLoopBody(context, node_id);
  85. return true;
  86. }
  87. // `for`
  88. // -----
  89. auto HandleParseNode(Context& context, Parse::ForHeaderStartId node_id)
  90. -> bool {
  91. // Create a nested scope to hold the cursor variable. This is also the lexical
  92. // scope that names in the pattern are added to, although they get rebound on
  93. // each loop iteration.
  94. context.scope_stack().PushForSameRegion();
  95. // Begin an implicit let declaration context for the pattern.
  96. context.decl_introducer_state_stack().Push<Lex::TokenKind::Let>();
  97. context.pattern_block_stack().Push();
  98. context.full_pattern_stack().PushNameBindingDecl();
  99. BeginSubpattern(context);
  100. context.node_stack().Push(node_id);
  101. return true;
  102. }
  103. auto HandleParseNode(Context& context, Parse::ForInId node_id) -> bool {
  104. auto pattern_block_id = context.pattern_block_stack().Pop();
  105. AddInst<SemIR::NameBindingDecl>(context, node_id,
  106. {.pattern_block_id = pattern_block_id});
  107. context.decl_introducer_state_stack().Pop<Lex::TokenKind::Let>();
  108. context.full_pattern_stack().StartPatternInitializer();
  109. context.node_stack().Push(node_id, pattern_block_id);
  110. return true;
  111. }
  112. // For a value or reference of type `Optional(T)`, call the given accessor.
  113. static auto CallOptionalAccessor(Context& context, Parse::NodeId node_id,
  114. SemIR::InstId optional_id,
  115. CoreIdentifier accessor_name)
  116. -> SemIR::InstId {
  117. auto accessor_name_id = context.core_identifiers().AddNameId(accessor_name);
  118. auto accessor_id =
  119. PerformMemberAccess(context, node_id, optional_id, accessor_name_id);
  120. return PerformCall(context, node_id, accessor_id, {});
  121. }
  122. auto HandleParseNode(Context& context, Parse::ForHeaderId node_id) -> bool {
  123. auto range_id = context.node_stack().PopExpr();
  124. auto pattern_block_id = context.node_stack().Pop<Parse::NodeKind::ForIn>();
  125. auto pattern_id = context.node_stack().PopPattern();
  126. auto start_node_id =
  127. context.node_stack().PopForSoloNodeId<Parse::NodeKind::ForHeaderStart>();
  128. // Convert the range expression to a value or reference so that we can use it
  129. // multiple times.
  130. // TODO: If this produces a temporary, its lifetime should presumably be
  131. // extended to cover the loop body.
  132. range_id = ConvertToValueOrRefExpr(context, range_id);
  133. // Create the cursor variable.
  134. // TODO: Produce a custom diagnostic if the range operand can't be used as a
  135. // range.
  136. // TODO: We need to allocate the `VarStorage` before building the operator.
  137. // The current order risks violating the preconditions on `Initialize` and
  138. // risks violating the topological ordering of insts.
  139. auto cursor_id =
  140. BuildUnaryOperator(context, node_id,
  141. {.interface_name = CoreIdentifier::Iterate,
  142. .op_name = CoreIdentifier::NewCursor},
  143. range_id);
  144. auto cursor_type_id = context.insts().Get(cursor_id).type_id();
  145. auto cursor_var_id = AddInstWithCleanup<SemIR::VarStorage>(
  146. context, node_id,
  147. {.type_id = cursor_type_id, .pattern_id = SemIR::AbsoluteInstId::None});
  148. auto init_id = Initialize(context, node_id, cursor_var_id, cursor_id);
  149. AddInst<SemIR::Assign>(context, node_id,
  150. {.lhs_id = cursor_var_id, .rhs_id = init_id});
  151. // Start emitting the loop header block.
  152. auto loop_header_id = StartLoopHeader(context, start_node_id);
  153. // Call `<range>.(Iterate.Next)(&cursor)`.
  154. auto cursor_type_inst_id = context.types().GetTypeInstId(cursor_type_id);
  155. auto cursor_addr_id = AddInst<SemIR::AddrOf>(
  156. context, node_id,
  157. {.type_id = GetPointerType(context, cursor_type_inst_id),
  158. .lvalue_id = cursor_var_id});
  159. auto element_id =
  160. BuildBinaryOperator(context, node_id,
  161. {.interface_name = CoreIdentifier::Iterate,
  162. .op_name = CoreIdentifier::Next},
  163. range_id, cursor_addr_id);
  164. // We need to convert away from an initializing expression in order to call
  165. // `HasValue` and then separately pattern-match against the element.
  166. // TODO: Instead, form a `.Some(pattern_id)` pattern and pattern-match against
  167. // that.
  168. element_id = ConvertToValueOrRefExpr(context, element_id);
  169. // Branch to the loop body if the optional element has a value.
  170. auto cond_value_id = CallOptionalAccessor(context, node_id, element_id,
  171. CoreIdentifier::HasValue);
  172. BranchAndStartLoopBody(context, node_id, loop_header_id, cond_value_id);
  173. // The loop pattern's initializer is now complete, and any bindings in it
  174. // should be in scope.
  175. context.full_pattern_stack().EndPatternInitializer();
  176. context.full_pattern_stack().PopFullPattern();
  177. // Create storage for var patterns now.
  178. AddPatternVarStorage(context, pattern_block_id, /*is_returned_var=*/false);
  179. // Initialize the pattern from `<element>.Get()`.
  180. auto element_value_id =
  181. CallOptionalAccessor(context, node_id, element_id, CoreIdentifier::Get);
  182. LocalPatternMatch(context, pattern_id, element_value_id);
  183. return true;
  184. }
  185. auto HandleParseNode(Context& context, Parse::ForStatementId node_id) -> bool {
  186. FinishLoopBody(context, node_id);
  187. return true;
  188. }
  189. // `break`
  190. // -------
  191. auto HandleParseNode(Context& context, Parse::BreakStatementStartId node_id)
  192. -> bool {
  193. auto& stack = context.break_continue_stack();
  194. if (stack.empty()) {
  195. CARBON_DIAGNOSTIC(BreakOutsideLoop, Error,
  196. "`break` can only be used in a loop");
  197. context.emitter().Emit(node_id, BreakOutsideLoop);
  198. } else {
  199. AddInst<SemIR::Branch>(context, node_id,
  200. {.target_id = stack.back().break_target});
  201. }
  202. context.inst_block_stack().Pop();
  203. context.inst_block_stack().PushUnreachable();
  204. return true;
  205. }
  206. auto HandleParseNode(Context& /*context*/, Parse::BreakStatementId /*node_id*/)
  207. -> bool {
  208. return true;
  209. }
  210. // `continue`
  211. // ----------
  212. auto HandleParseNode(Context& context, Parse::ContinueStatementStartId node_id)
  213. -> bool {
  214. auto& stack = context.break_continue_stack();
  215. if (stack.empty()) {
  216. CARBON_DIAGNOSTIC(ContinueOutsideLoop, Error,
  217. "`continue` can only be used in a loop");
  218. context.emitter().Emit(node_id, ContinueOutsideLoop);
  219. } else {
  220. AddInst<SemIR::Branch>(context, node_id,
  221. {.target_id = stack.back().continue_target});
  222. }
  223. context.inst_block_stack().Pop();
  224. context.inst_block_stack().PushUnreachable();
  225. return true;
  226. }
  227. auto HandleParseNode(Context& /*context*/,
  228. Parse::ContinueStatementId /*node_id*/) -> bool {
  229. return true;
  230. }
  231. } // namespace Carbon::Check