| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
- // Exceptions. See /LICENSE for license information.
- // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- #include "toolchain/check/context.h"
- #include "toolchain/check/convert.h"
- namespace Carbon::Check {
- auto HandleBreakStatement(Context& /*context*/, Parse::Node /*parse_node*/)
- -> bool {
- return true;
- }
- auto HandleBreakStatementStart(Context& context, Parse::Node parse_node)
- -> bool {
- auto& stack = context.break_continue_stack();
- if (stack.empty()) {
- CARBON_DIAGNOSTIC(BreakOutsideLoop, Error,
- "`break` can only be used in a loop.");
- context.emitter().Emit(parse_node, BreakOutsideLoop);
- } else {
- context.AddNode(SemIR::Branch(parse_node, stack.back().break_target));
- }
- context.node_block_stack().Pop();
- context.node_block_stack().PushUnreachable();
- return true;
- }
- auto HandleContinueStatement(Context& /*context*/, Parse::Node /*parse_node*/)
- -> bool {
- return true;
- }
- auto HandleContinueStatementStart(Context& context, Parse::Node parse_node)
- -> bool {
- auto& stack = context.break_continue_stack();
- if (stack.empty()) {
- CARBON_DIAGNOSTIC(ContinueOutsideLoop, Error,
- "`continue` can only be used in a loop.");
- context.emitter().Emit(parse_node, ContinueOutsideLoop);
- } else {
- context.AddNode(SemIR::Branch(parse_node, stack.back().continue_target));
- }
- context.node_block_stack().Pop();
- context.node_block_stack().PushUnreachable();
- return true;
- }
- auto HandleForHeader(Context& context, Parse::Node parse_node) -> bool {
- return context.TODO(parse_node, "HandleForHeader");
- }
- auto HandleForHeaderStart(Context& context, Parse::Node parse_node) -> bool {
- return context.TODO(parse_node, "HandleForHeaderStart");
- }
- auto HandleForIn(Context& context, Parse::Node parse_node) -> bool {
- return context.TODO(parse_node, "HandleForIn");
- }
- auto HandleForStatement(Context& context, Parse::Node parse_node) -> bool {
- return context.TODO(parse_node, "HandleForStatement");
- }
- auto HandleWhileConditionStart(Context& context, Parse::Node parse_node)
- -> bool {
- // Branch to the loop header block. Note that we create a new block here even
- // if the current block is empty; this ensures that the loop always has a
- // preheader block.
- auto loop_header_id = context.AddDominatedBlockAndBranch(parse_node);
- context.node_block_stack().Pop();
- // Start emitting the loop header block.
- context.node_block_stack().Push(loop_header_id);
- context.AddCurrentCodeBlockToFunction();
- context.node_stack().Push(parse_node, loop_header_id);
- return true;
- }
- auto HandleWhileCondition(Context& context, Parse::Node parse_node) -> bool {
- auto cond_value_id = context.node_stack().PopExpression();
- auto loop_header_id =
- context.node_stack().Peek<Parse::NodeKind::WhileConditionStart>();
- cond_value_id = ConvertToBoolValue(context, parse_node, cond_value_id);
- // Branch to either the loop body or the loop exit block.
- auto loop_body_id =
- context.AddDominatedBlockAndBranchIf(parse_node, cond_value_id);
- auto loop_exit_id = context.AddDominatedBlockAndBranch(parse_node);
- context.node_block_stack().Pop();
- // Start emitting the loop body.
- context.node_block_stack().Push(loop_body_id);
- context.AddCurrentCodeBlockToFunction();
- context.break_continue_stack().push_back(
- {.break_target = loop_exit_id, .continue_target = loop_header_id});
- context.node_stack().Push(parse_node, loop_exit_id);
- return true;
- }
- auto HandleWhileStatement(Context& context, Parse::Node parse_node) -> bool {
- auto loop_exit_id =
- context.node_stack().Pop<Parse::NodeKind::WhileCondition>();
- auto loop_header_id =
- context.node_stack().Pop<Parse::NodeKind::WhileConditionStart>();
- context.break_continue_stack().pop_back();
- // Add the loop backedge.
- context.AddNode(SemIR::Branch(parse_node, loop_header_id));
- context.node_block_stack().Pop();
- // Start emitting the loop exit block.
- context.node_block_stack().Push(loop_exit_id);
- context.AddCurrentCodeBlockToFunction();
- return true;
- }
- } // namespace Carbon::Check
|