mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Rename parse_node -> node_id (#3760)
This was previously discussed at https://discord.com/channels/655572317891461132/655578254970716160/1209975051588210729. I'm initiating this mainly because we typically use "id" suffixes to indicate an `IdBase` being passed around and the non-id suffix of `parse_node` suggests at it carrying more data than it actually does. There used to be more reason for avoiding `node_id` because `SemIR::InstId` used to be named `NodeId`, but that's no longer necessary. As a consequence, I'd like to rename `parse_node` to more precisely reflect its type. In full, this is doing: ``` parse_node_kind -> node_kind parse_node -> node_id ParseNodeCategory -> NodeCategory ParseNodeKind -> NodeKind ParseNode -> NodeId ``` This is primarily in check and sem_ir, but with some `parse_node_kind` references in parse too. Pluralization is consistent with name forms on both sides, so that wasn't part of my replacements.
This commit is contained in:
+127
-133
@@ -7,44 +7,43 @@
|
||||
|
||||
namespace Carbon::Check {
|
||||
|
||||
auto HandleInfixOperatorAmp(Context& context,
|
||||
Parse::InfixOperatorAmpId parse_node) -> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorAmp");
|
||||
auto HandleInfixOperatorAmp(Context& context, Parse::InfixOperatorAmpId node_id)
|
||||
-> bool {
|
||||
return context.TODO(node_id, "HandleInfixOperatorAmp");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorAmpEqual(Context& context,
|
||||
Parse::InfixOperatorAmpEqualId parse_node)
|
||||
Parse::InfixOperatorAmpEqualId node_id)
|
||||
-> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorAmpEqual");
|
||||
return context.TODO(node_id, "HandleInfixOperatorAmpEqual");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorAs(Context& context,
|
||||
Parse::InfixOperatorAsId parse_node) -> bool {
|
||||
auto [rhs_node, rhs_id] = context.node_stack().PopExprWithParseNode();
|
||||
auto [lhs_node, lhs_id] = context.node_stack().PopExprWithParseNode();
|
||||
auto HandleInfixOperatorAs(Context& context, Parse::InfixOperatorAsId node_id)
|
||||
-> bool {
|
||||
auto [rhs_node, rhs_id] = context.node_stack().PopExprWithNodeId();
|
||||
auto [lhs_node, lhs_id] = context.node_stack().PopExprWithNodeId();
|
||||
|
||||
auto rhs_type_id = ExprAsType(context, rhs_node, rhs_id);
|
||||
context.node_stack().Push(
|
||||
parse_node,
|
||||
ConvertForExplicitAs(context, parse_node, lhs_id, rhs_type_id));
|
||||
node_id, ConvertForExplicitAs(context, node_id, lhs_id, rhs_type_id));
|
||||
return true;
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorCaret(Context& context,
|
||||
Parse::InfixOperatorCaretId parse_node) -> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorCaret");
|
||||
Parse::InfixOperatorCaretId node_id) -> bool {
|
||||
return context.TODO(node_id, "HandleInfixOperatorCaret");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorCaretEqual(Context& context,
|
||||
Parse::InfixOperatorCaretEqualId parse_node)
|
||||
Parse::InfixOperatorCaretEqualId node_id)
|
||||
-> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorCaretEqual");
|
||||
return context.TODO(node_id, "HandleInfixOperatorCaretEqual");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorEqual(Context& context,
|
||||
Parse::InfixOperatorEqualId parse_node) -> bool {
|
||||
auto [rhs_node, rhs_id] = context.node_stack().PopExprWithParseNode();
|
||||
auto [lhs_node, lhs_id] = context.node_stack().PopExprWithParseNode();
|
||||
Parse::InfixOperatorEqualId node_id) -> bool {
|
||||
auto [rhs_node, rhs_id] = context.node_stack().PopExprWithNodeId();
|
||||
auto [lhs_node, lhs_id] = context.node_stack().PopExprWithNodeId();
|
||||
|
||||
// TODO: handle complex assignment expression such as `a += 1`.
|
||||
if (auto lhs_cat = SemIR::GetExprCategory(context.sem_ir(), lhs_id);
|
||||
@@ -56,155 +55,154 @@ auto HandleInfixOperatorEqual(Context& context,
|
||||
}
|
||||
// TODO: Destroy the old value before reinitializing. This will require
|
||||
// building the destruction code before we build the RHS subexpression.
|
||||
rhs_id = Initialize(context, parse_node, lhs_id, rhs_id);
|
||||
context.AddInst({parse_node, SemIR::Assign{lhs_id, rhs_id}});
|
||||
rhs_id = Initialize(context, node_id, lhs_id, rhs_id);
|
||||
context.AddInst({node_id, SemIR::Assign{lhs_id, rhs_id}});
|
||||
// We model assignment as an expression, so we need to push a value for
|
||||
// it, even though it doesn't produce a value.
|
||||
// TODO: Consider changing our parse tree to model assignment as a
|
||||
// different kind of statement than an expression statement.
|
||||
context.node_stack().Push(parse_node, lhs_id);
|
||||
context.node_stack().Push(node_id, lhs_id);
|
||||
return true;
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorEqualEqual(Context& context,
|
||||
Parse::InfixOperatorEqualEqualId parse_node)
|
||||
Parse::InfixOperatorEqualEqualId node_id)
|
||||
-> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorEqualEqual");
|
||||
return context.TODO(node_id, "HandleInfixOperatorEqualEqual");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorExclaimEqual(
|
||||
Context& context, Parse::InfixOperatorExclaimEqualId parse_node) -> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorExclaimEqual");
|
||||
auto HandleInfixOperatorExclaimEqual(Context& context,
|
||||
Parse::InfixOperatorExclaimEqualId node_id)
|
||||
-> bool {
|
||||
return context.TODO(node_id, "HandleInfixOperatorExclaimEqual");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorGreater(Context& context,
|
||||
Parse::InfixOperatorGreaterId parse_node)
|
||||
-> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorGreater");
|
||||
Parse::InfixOperatorGreaterId node_id) -> bool {
|
||||
return context.TODO(node_id, "HandleInfixOperatorGreater");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorGreaterEqual(
|
||||
Context& context, Parse::InfixOperatorGreaterEqualId parse_node) -> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorGreaterEqual");
|
||||
auto HandleInfixOperatorGreaterEqual(Context& context,
|
||||
Parse::InfixOperatorGreaterEqualId node_id)
|
||||
-> bool {
|
||||
return context.TODO(node_id, "HandleInfixOperatorGreaterEqual");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorGreaterGreater(
|
||||
Context& context, Parse::InfixOperatorGreaterGreaterId parse_node) -> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorGreaterGreater");
|
||||
Context& context, Parse::InfixOperatorGreaterGreaterId node_id) -> bool {
|
||||
return context.TODO(node_id, "HandleInfixOperatorGreaterGreater");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorGreaterGreaterEqual(
|
||||
Context& context, Parse::InfixOperatorGreaterGreaterEqualId parse_node)
|
||||
Context& context, Parse::InfixOperatorGreaterGreaterEqualId node_id)
|
||||
-> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorGreaterGreaterEqual");
|
||||
return context.TODO(node_id, "HandleInfixOperatorGreaterGreaterEqual");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorLess(Context& context,
|
||||
Parse::InfixOperatorLessId parse_node) -> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorLess");
|
||||
Parse::InfixOperatorLessId node_id) -> bool {
|
||||
return context.TODO(node_id, "HandleInfixOperatorLess");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorLessEqual(Context& context,
|
||||
Parse::InfixOperatorLessEqualId parse_node)
|
||||
Parse::InfixOperatorLessEqualId node_id)
|
||||
-> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorLessEqual");
|
||||
return context.TODO(node_id, "HandleInfixOperatorLessEqual");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorLessEqualGreater(
|
||||
Context& context, Parse::InfixOperatorLessEqualGreaterId parse_node)
|
||||
-> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorLessEqualGreater");
|
||||
Context& context, Parse::InfixOperatorLessEqualGreaterId node_id) -> bool {
|
||||
return context.TODO(node_id, "HandleInfixOperatorLessEqualGreater");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorLessLess(Context& context,
|
||||
Parse::InfixOperatorLessLessId parse_node)
|
||||
Parse::InfixOperatorLessLessId node_id)
|
||||
-> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorLessLess");
|
||||
return context.TODO(node_id, "HandleInfixOperatorLessLess");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorLessLessEqual(
|
||||
Context& context, Parse::InfixOperatorLessLessEqualId parse_node) -> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorLessLessEqual");
|
||||
Context& context, Parse::InfixOperatorLessLessEqualId node_id) -> bool {
|
||||
return context.TODO(node_id, "HandleInfixOperatorLessLessEqual");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorMinus(Context& context,
|
||||
Parse::InfixOperatorMinusId parse_node) -> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorMinus");
|
||||
Parse::InfixOperatorMinusId node_id) -> bool {
|
||||
return context.TODO(node_id, "HandleInfixOperatorMinus");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorMinusEqual(Context& context,
|
||||
Parse::InfixOperatorMinusEqualId parse_node)
|
||||
Parse::InfixOperatorMinusEqualId node_id)
|
||||
-> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorMinusEqual");
|
||||
return context.TODO(node_id, "HandleInfixOperatorMinusEqual");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorPercent(Context& context,
|
||||
Parse::InfixOperatorPercentId parse_node)
|
||||
-> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorPercent");
|
||||
Parse::InfixOperatorPercentId node_id) -> bool {
|
||||
return context.TODO(node_id, "HandleInfixOperatorPercent");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorPercentEqual(
|
||||
Context& context, Parse::InfixOperatorPercentEqualId parse_node) -> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorPercentEqual");
|
||||
auto HandleInfixOperatorPercentEqual(Context& context,
|
||||
Parse::InfixOperatorPercentEqualId node_id)
|
||||
-> bool {
|
||||
return context.TODO(node_id, "HandleInfixOperatorPercentEqual");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorPipe(Context& context,
|
||||
Parse::InfixOperatorPipeId parse_node) -> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorPipe");
|
||||
Parse::InfixOperatorPipeId node_id) -> bool {
|
||||
return context.TODO(node_id, "HandleInfixOperatorPipe");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorPipeEqual(Context& context,
|
||||
Parse::InfixOperatorPipeEqualId parse_node)
|
||||
Parse::InfixOperatorPipeEqualId node_id)
|
||||
-> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorPipeEqual");
|
||||
return context.TODO(node_id, "HandleInfixOperatorPipeEqual");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorPlus(Context& context,
|
||||
Parse::InfixOperatorPlusId parse_node) -> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorPlus");
|
||||
Parse::InfixOperatorPlusId node_id) -> bool {
|
||||
return context.TODO(node_id, "HandleInfixOperatorPlus");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorPlusEqual(Context& context,
|
||||
Parse::InfixOperatorPlusEqualId parse_node)
|
||||
Parse::InfixOperatorPlusEqualId node_id)
|
||||
-> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorPlusEqual");
|
||||
return context.TODO(node_id, "HandleInfixOperatorPlusEqual");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorSlash(Context& context,
|
||||
Parse::InfixOperatorSlashId parse_node) -> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorSlash");
|
||||
Parse::InfixOperatorSlashId node_id) -> bool {
|
||||
return context.TODO(node_id, "HandleInfixOperatorSlash");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorSlashEqual(Context& context,
|
||||
Parse::InfixOperatorSlashEqualId parse_node)
|
||||
Parse::InfixOperatorSlashEqualId node_id)
|
||||
-> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorSlashEqual");
|
||||
return context.TODO(node_id, "HandleInfixOperatorSlashEqual");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorStar(Context& context,
|
||||
Parse::InfixOperatorStarId parse_node) -> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorStar");
|
||||
Parse::InfixOperatorStarId node_id) -> bool {
|
||||
return context.TODO(node_id, "HandleInfixOperatorStar");
|
||||
}
|
||||
|
||||
auto HandleInfixOperatorStarEqual(Context& context,
|
||||
Parse::InfixOperatorStarEqualId parse_node)
|
||||
Parse::InfixOperatorStarEqualId node_id)
|
||||
-> bool {
|
||||
return context.TODO(parse_node, "HandleInfixOperatorStarEqual");
|
||||
return context.TODO(node_id, "HandleInfixOperatorStarEqual");
|
||||
}
|
||||
|
||||
auto HandlePostfixOperatorStar(Context& context,
|
||||
Parse::PostfixOperatorStarId parse_node)
|
||||
-> bool {
|
||||
Parse::PostfixOperatorStarId node_id) -> bool {
|
||||
auto value_id = context.node_stack().PopExpr();
|
||||
auto inner_type_id = ExprAsType(context, parse_node, value_id);
|
||||
auto inner_type_id = ExprAsType(context, node_id, value_id);
|
||||
context.AddInstAndPush(
|
||||
{parse_node, SemIR::PointerType{SemIR::TypeId::TypeType, inner_type_id}});
|
||||
{node_id, SemIR::PointerType{SemIR::TypeId::TypeType, inner_type_id}});
|
||||
return true;
|
||||
}
|
||||
|
||||
auto HandlePrefixOperatorAmp(Context& context,
|
||||
Parse::PrefixOperatorAmpId parse_node) -> bool {
|
||||
Parse::PrefixOperatorAmpId node_id) -> bool {
|
||||
auto value_id = context.node_stack().PopExpr();
|
||||
auto type_id = context.insts().Get(value_id).type_id();
|
||||
// Only durable reference expressions can have their address taken.
|
||||
@@ -215,30 +213,28 @@ auto HandlePrefixOperatorAmp(Context& context,
|
||||
case SemIR::ExprCategory::EphemeralRef:
|
||||
CARBON_DIAGNOSTIC(AddrOfEphemeralRef, Error,
|
||||
"Cannot take the address of a temporary object.");
|
||||
context.emitter().Emit(TokenOnly(parse_node), AddrOfEphemeralRef);
|
||||
context.emitter().Emit(TokenOnly(node_id), AddrOfEphemeralRef);
|
||||
value_id = SemIR::InstId::BuiltinError;
|
||||
break;
|
||||
default:
|
||||
CARBON_DIAGNOSTIC(AddrOfNonRef, Error,
|
||||
"Cannot take the address of non-reference expression.");
|
||||
context.emitter().Emit(TokenOnly(parse_node), AddrOfNonRef);
|
||||
context.emitter().Emit(TokenOnly(node_id), AddrOfNonRef);
|
||||
value_id = SemIR::InstId::BuiltinError;
|
||||
break;
|
||||
}
|
||||
context.AddInstAndPush(
|
||||
{parse_node, SemIR::AddrOf{context.GetPointerType(type_id), value_id}});
|
||||
{node_id, SemIR::AddrOf{context.GetPointerType(type_id), value_id}});
|
||||
return true;
|
||||
}
|
||||
|
||||
auto HandlePrefixOperatorCaret(Context& context,
|
||||
Parse::PrefixOperatorCaretId parse_node)
|
||||
-> bool {
|
||||
return context.TODO(parse_node, "HandlePrefixOperatorCaret");
|
||||
Parse::PrefixOperatorCaretId node_id) -> bool {
|
||||
return context.TODO(node_id, "HandlePrefixOperatorCaret");
|
||||
}
|
||||
|
||||
auto HandlePrefixOperatorConst(Context& context,
|
||||
Parse::PrefixOperatorConstId parse_node)
|
||||
-> bool {
|
||||
Parse::PrefixOperatorConstId node_id) -> bool {
|
||||
auto value_id = context.node_stack().PopExpr();
|
||||
|
||||
// `const (const T)` is probably not what the developer intended.
|
||||
@@ -248,43 +244,43 @@ auto HandlePrefixOperatorConst(Context& context,
|
||||
CARBON_DIAGNOSTIC(RepeatedConst, Warning,
|
||||
"`const` applied repeatedly to the same type has no "
|
||||
"additional effect.");
|
||||
context.emitter().Emit(parse_node, RepeatedConst);
|
||||
context.emitter().Emit(node_id, RepeatedConst);
|
||||
}
|
||||
auto inner_type_id = ExprAsType(context, parse_node, value_id);
|
||||
auto inner_type_id = ExprAsType(context, node_id, value_id);
|
||||
context.AddInstAndPush(
|
||||
{parse_node, SemIR::ConstType{SemIR::TypeId::TypeType, inner_type_id}});
|
||||
{node_id, SemIR::ConstType{SemIR::TypeId::TypeType, inner_type_id}});
|
||||
return true;
|
||||
}
|
||||
|
||||
auto HandlePrefixOperatorMinus(Context& context,
|
||||
Parse::PrefixOperatorMinusId parse_node)
|
||||
-> bool {
|
||||
return context.TODO(parse_node, "HandlePrefixOperatorMinus");
|
||||
Parse::PrefixOperatorMinusId node_id) -> bool {
|
||||
return context.TODO(node_id, "HandlePrefixOperatorMinus");
|
||||
}
|
||||
|
||||
auto HandlePrefixOperatorMinusMinus(
|
||||
Context& context, Parse::PrefixOperatorMinusMinusId parse_node) -> bool {
|
||||
return context.TODO(parse_node, "HandlePrefixOperatorMinusMinus");
|
||||
auto HandlePrefixOperatorMinusMinus(Context& context,
|
||||
Parse::PrefixOperatorMinusMinusId node_id)
|
||||
-> bool {
|
||||
return context.TODO(node_id, "HandlePrefixOperatorMinusMinus");
|
||||
}
|
||||
|
||||
auto HandlePrefixOperatorNot(Context& context,
|
||||
Parse::PrefixOperatorNotId parse_node) -> bool {
|
||||
Parse::PrefixOperatorNotId node_id) -> bool {
|
||||
auto value_id = context.node_stack().PopExpr();
|
||||
value_id = ConvertToBoolValue(context, parse_node, value_id);
|
||||
value_id = ConvertToBoolValue(context, node_id, value_id);
|
||||
context.AddInstAndPush(
|
||||
{parse_node, SemIR::UnaryOperatorNot{
|
||||
context.insts().Get(value_id).type_id(), value_id}});
|
||||
{node_id, SemIR::UnaryOperatorNot{context.insts().Get(value_id).type_id(),
|
||||
value_id}});
|
||||
return true;
|
||||
}
|
||||
|
||||
auto HandlePrefixOperatorPlusPlus(Context& context,
|
||||
Parse::PrefixOperatorPlusPlusId parse_node)
|
||||
Parse::PrefixOperatorPlusPlusId node_id)
|
||||
-> bool {
|
||||
return context.TODO(parse_node, "HandlePrefixOperatorPlusPlus");
|
||||
return context.TODO(node_id, "HandlePrefixOperatorPlusPlus");
|
||||
}
|
||||
|
||||
auto HandlePrefixOperatorStar(Context& context,
|
||||
Parse::PrefixOperatorStarId parse_node) -> bool {
|
||||
Parse::PrefixOperatorStarId node_id) -> bool {
|
||||
auto value_id = context.node_stack().PopExpr();
|
||||
value_id = ConvertToValueExpr(context, value_id);
|
||||
auto type_id =
|
||||
@@ -297,53 +293,51 @@ auto HandlePrefixOperatorStar(Context& context,
|
||||
CARBON_DIAGNOSTIC(DerefOfNonPointer, Error,
|
||||
"Cannot dereference operand of non-pointer type `{0}`.",
|
||||
SemIR::TypeId);
|
||||
auto builder = context.emitter().Build(TokenOnly(parse_node),
|
||||
DerefOfNonPointer, type_id);
|
||||
auto builder =
|
||||
context.emitter().Build(TokenOnly(node_id), DerefOfNonPointer, type_id);
|
||||
// TODO: Check for any facet here, rather than only a type.
|
||||
if (type_id == SemIR::TypeId::TypeType) {
|
||||
CARBON_DIAGNOSTIC(
|
||||
DerefOfType, Note,
|
||||
"To form a pointer type, write the `*` after the pointee type.");
|
||||
builder.Note(TokenOnly(parse_node), DerefOfType);
|
||||
builder.Note(TokenOnly(node_id), DerefOfType);
|
||||
}
|
||||
builder.Emit();
|
||||
}
|
||||
context.AddInstAndPush({parse_node, SemIR::Deref{result_type_id, value_id}});
|
||||
context.AddInstAndPush({node_id, SemIR::Deref{result_type_id, value_id}});
|
||||
return true;
|
||||
}
|
||||
|
||||
// Adds the branch for a short circuit operand.
|
||||
static auto HandleShortCircuitOperand(Context& context,
|
||||
Parse::NodeId parse_node, bool is_or)
|
||||
-> bool {
|
||||
static auto HandleShortCircuitOperand(Context& context, Parse::NodeId node_id,
|
||||
bool is_or) -> bool {
|
||||
// Convert the condition to `bool`.
|
||||
auto cond_value_id = context.node_stack().PopExpr();
|
||||
cond_value_id = ConvertToBoolValue(context, parse_node, cond_value_id);
|
||||
cond_value_id = ConvertToBoolValue(context, node_id, cond_value_id);
|
||||
auto bool_type_id = context.insts().Get(cond_value_id).type_id();
|
||||
|
||||
// Compute the branch value: the condition for `and`, inverted for `or`.
|
||||
SemIR::InstId branch_value_id =
|
||||
is_or ? context.AddInst(
|
||||
{parse_node,
|
||||
SemIR::UnaryOperatorNot{bool_type_id, cond_value_id}})
|
||||
is_or ? context.AddInst({node_id, SemIR::UnaryOperatorNot{bool_type_id,
|
||||
cond_value_id}})
|
||||
: cond_value_id;
|
||||
auto short_circuit_result_id = context.AddInst(
|
||||
{parse_node,
|
||||
{node_id,
|
||||
SemIR::BoolLiteral{bool_type_id, is_or ? SemIR::BoolValue::True
|
||||
: SemIR::BoolValue::False}});
|
||||
|
||||
// Create a block for the right-hand side and for the continuation.
|
||||
auto rhs_block_id =
|
||||
context.AddDominatedBlockAndBranchIf(parse_node, branch_value_id);
|
||||
context.AddDominatedBlockAndBranchIf(node_id, branch_value_id);
|
||||
auto end_block_id = context.AddDominatedBlockAndBranchWithArg(
|
||||
parse_node, short_circuit_result_id);
|
||||
node_id, short_circuit_result_id);
|
||||
|
||||
// Push the resumption and the right-hand side blocks, and start emitting the
|
||||
// right-hand operand.
|
||||
context.inst_block_stack().Pop();
|
||||
context.inst_block_stack().Push(end_block_id);
|
||||
context.inst_block_stack().Push(rhs_block_id);
|
||||
context.AddCurrentCodeBlockToFunction(parse_node);
|
||||
context.AddCurrentCodeBlockToFunction(node_id);
|
||||
|
||||
// HandleShortCircuitOperator will follow, and doesn't need the operand on the
|
||||
// node stack.
|
||||
@@ -351,52 +345,52 @@ static auto HandleShortCircuitOperand(Context& context,
|
||||
}
|
||||
|
||||
auto HandleShortCircuitOperandAnd(Context& context,
|
||||
Parse::ShortCircuitOperandAndId parse_node)
|
||||
Parse::ShortCircuitOperandAndId node_id)
|
||||
-> bool {
|
||||
return HandleShortCircuitOperand(context, parse_node, /*is_or=*/false);
|
||||
return HandleShortCircuitOperand(context, node_id, /*is_or=*/false);
|
||||
}
|
||||
|
||||
auto HandleShortCircuitOperandOr(Context& context,
|
||||
Parse::ShortCircuitOperandOrId parse_node)
|
||||
Parse::ShortCircuitOperandOrId node_id)
|
||||
-> bool {
|
||||
return HandleShortCircuitOperand(context, parse_node, /*is_or=*/true);
|
||||
return HandleShortCircuitOperand(context, node_id, /*is_or=*/true);
|
||||
}
|
||||
|
||||
// Short circuit operator handling is uniform because the branching logic
|
||||
// occurs during operand handling.
|
||||
static auto HandleShortCircuitOperator(Context& context,
|
||||
Parse::NodeId parse_node) -> bool {
|
||||
auto [rhs_node, rhs_id] = context.node_stack().PopExprWithParseNode();
|
||||
static auto HandleShortCircuitOperator(Context& context, Parse::NodeId node_id)
|
||||
-> bool {
|
||||
auto [rhs_node, rhs_id] = context.node_stack().PopExprWithNodeId();
|
||||
|
||||
// The first operand is wrapped in a ShortCircuitOperand, which we
|
||||
// already handled by creating a RHS block and a resumption block, which
|
||||
// are the current block and its enclosing block.
|
||||
rhs_id = ConvertToBoolValue(context, parse_node, rhs_id);
|
||||
rhs_id = ConvertToBoolValue(context, node_id, rhs_id);
|
||||
|
||||
// When the second operand is evaluated, the result of `and` and `or` is
|
||||
// its value.
|
||||
auto resume_block_id = context.inst_block_stack().PeekOrAdd(/*depth=*/1);
|
||||
context.AddInst({parse_node, SemIR::BranchWithArg{resume_block_id, rhs_id}});
|
||||
context.AddInst({node_id, SemIR::BranchWithArg{resume_block_id, rhs_id}});
|
||||
context.inst_block_stack().Pop();
|
||||
context.AddCurrentCodeBlockToFunction(parse_node);
|
||||
context.AddCurrentCodeBlockToFunction(node_id);
|
||||
|
||||
// Collect the result from either the first or second operand.
|
||||
context.AddInstAndPush(
|
||||
{parse_node, SemIR::BlockArg{context.insts().Get(rhs_id).type_id(),
|
||||
resume_block_id}});
|
||||
{node_id, SemIR::BlockArg{context.insts().Get(rhs_id).type_id(),
|
||||
resume_block_id}});
|
||||
return true;
|
||||
}
|
||||
|
||||
auto HandleShortCircuitOperatorAnd(Context& context,
|
||||
Parse::ShortCircuitOperatorAndId parse_node)
|
||||
Parse::ShortCircuitOperatorAndId node_id)
|
||||
-> bool {
|
||||
return HandleShortCircuitOperator(context, parse_node);
|
||||
return HandleShortCircuitOperator(context, node_id);
|
||||
}
|
||||
|
||||
auto HandleShortCircuitOperatorOr(Context& context,
|
||||
Parse::ShortCircuitOperatorOrId parse_node)
|
||||
Parse::ShortCircuitOperatorOrId node_id)
|
||||
-> bool {
|
||||
return HandleShortCircuitOperator(context, parse_node);
|
||||
return HandleShortCircuitOperator(context, node_id);
|
||||
}
|
||||
|
||||
} // namespace Carbon::Check
|
||||
|
||||
Reference in New Issue
Block a user