Files
carbon-lang/toolchain/check/handle_loop_statement.cpp
T
Jon Ross-PerkinsandRichard Smith f197219c10 Split parse nodes out from instructions because they're rarely used. (#3590)
The parse nodes are still tracked as part of the same value store
interface in order to ensure parity, but they're split out from Inst
itself in order to reduce the size of Inst -- the expectation is that
they don't need to be passed around quite as much.

This change doesn't actually reduce the passing very much, although
there are hints of it: AddInstAndPush doesn't typically need a separate
parse node from the one on the Inst itself, for example. In a couple
spots I changed code to rely a little more on the InstId until the
ParseNode is needed, but it's very low hanging fruit where done. I think
convert could do more to not eagerly fetch the parse node before its
use, but more cleanup felt it would be easier to handle separately. I'm
currently viewing this as making such cleanup _possible_ rather than
executing on it up-front.

But also, I want to make sure there's a consensus to head in this
direction before pulling the trigger. We speculated that this would
result in the parse node being passed around less, and I do think that's
the case, although it's a bit fuzzy in the change.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2024-01-12 19:01:51 +00:00

145 lines
4.5 KiB
C++

// 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 {
// `while`
// -------
auto HandleWhileConditionStart(Context& context,
Parse::WhileConditionStartId 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.inst_block_stack().Pop();
// Start emitting the loop header block.
context.inst_block_stack().Push(loop_header_id);
context.AddCurrentCodeBlockToFunction();
context.node_stack().Push(parse_node, loop_header_id);
return true;
}
auto HandleWhileCondition(Context& context, Parse::WhileConditionId parse_node)
-> bool {
auto cond_value_id = context.node_stack().PopExpr();
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.inst_block_stack().Pop();
// Start emitting the loop body.
context.inst_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::WhileStatementId 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.AddInst({parse_node, SemIR::Branch{loop_header_id}});
context.inst_block_stack().Pop();
// Start emitting the loop exit block.
context.inst_block_stack().Push(loop_exit_id);
context.AddCurrentCodeBlockToFunction();
return true;
}
// `for`
// -----
auto HandleForHeaderStart(Context& context, Parse::ForHeaderStartId parse_node)
-> bool {
return context.TODO(parse_node, "HandleForHeaderStart");
}
auto HandleForIn(Context& context, Parse::ForInId parse_node) -> bool {
context.decl_state_stack().Pop(DeclState::Var);
return context.TODO(parse_node, "HandleForIn");
}
auto HandleForHeader(Context& context, Parse::ForHeaderId parse_node) -> bool {
return context.TODO(parse_node, "HandleForHeader");
}
auto HandleForStatement(Context& context, Parse::ForStatementId parse_node)
-> bool {
return context.TODO(parse_node, "HandleForStatement");
}
// `break`
// -------
auto HandleBreakStatementStart(Context& context,
Parse::BreakStatementStartId 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.AddInst({parse_node, SemIR::Branch{stack.back().break_target}});
}
context.inst_block_stack().Pop();
context.inst_block_stack().PushUnreachable();
return true;
}
auto HandleBreakStatement(Context& /*context*/,
Parse::BreakStatementId /*parse_node*/) -> bool {
return true;
}
// `continue`
// ----------
auto HandleContinueStatementStart(Context& context,
Parse::ContinueStatementStartId 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.AddInst({parse_node, SemIR::Branch{stack.back().continue_target}});
}
context.inst_block_stack().Pop();
context.inst_block_stack().PushUnreachable();
return true;
}
auto HandleContinueStatement(Context& /*context*/,
Parse::ContinueStatementId /*parse_node*/)
-> bool {
return true;
}
} // namespace Carbon::Check