Skip to content

Commit

Permalink
Finish refactoring Push/Pop for stronger type handling. (carbon-langu…
Browse files Browse the repository at this point in the history
…age#2987)

This adds a distinction between Unused and SoloParseNode, rather than
equating the two. This is intended to help identify nodes which are
getting pushed but maybe don't need to be.

Not totally done because I want to adjust declaration name handling due
to a quirk with how it mixes Name with Expression, but almost done. Once
that's done the type punning will be completely gone.
  • Loading branch information
jonmeow authored Jul 14, 2023
1 parent 6a2b968 commit 43065a1
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 108 deletions.
4 changes: 2 additions & 2 deletions toolchain/semantics/semantics_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ auto SemanticsContext::PopDeclarationName() -> DeclarationNameContext {
} else {
// The name had no qualifiers, so we need to process the node now.
auto [parse_node, node_or_name_id] =
node_stack_.PopWithParseNode<SemanticsNodeId>();
node_stack_.PopExpressionWithParseNode();
ApplyDeclarationNameQualifier(parse_node, node_or_name_id);
}

Expand Down Expand Up @@ -547,7 +547,7 @@ auto SemanticsContext::ParamOrArgSave(bool for_args) -> void {
// For an argument, we add a stub reference to the expression on the top of
// the stack. There may not be anything on the IR prior to this.
auto [entry_parse_node, entry_node_id] =
node_stack_.PopWithParseNode<SemanticsNodeId>();
node_stack_.PopExpressionWithParseNode();
param_or_arg_id = AddNode(SemanticsNode::StubReference::Make(
entry_parse_node, semantics_ir_->GetNode(entry_node_id).type_id(),
entry_node_id));
Expand Down
2 changes: 1 addition & 1 deletion toolchain/semantics/semantics_handle_call_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ auto SemanticsHandleCallExpressionComma(SemanticsContext& context,

auto SemanticsHandleCallExpressionStart(SemanticsContext& context,
ParseTree::Node parse_node) -> bool {
auto name_id = context.node_stack().Pop<SemanticsNodeId>();
auto name_id = context.node_stack().PopExpression();
context.node_stack().Push(parse_node, name_id);
context.ParamOrArgStart();
return true;
Expand Down
2 changes: 1 addition & 1 deletion toolchain/semantics/semantics_handle_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ auto SemanticsHandleReturnType(SemanticsContext& context,
ParseTree::Node parse_node) -> bool {
// Propagate the type expression.
auto [type_parse_node, type_node_id] =
context.node_stack().PopWithParseNode<SemanticsNodeId>();
context.node_stack().PopExpressionWithParseNode();
auto cast_node_id = context.ExpressionAsType(type_parse_node, type_node_id);
context.node_stack().Push(parse_node, cast_node_id);
return true;
Expand Down
6 changes: 3 additions & 3 deletions toolchain/semantics/semantics_handle_if_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Carbon {

auto SemanticsHandleIfExpressionIf(SemanticsContext& context,
ParseTree::Node if_node) -> bool {
auto cond_value_id = context.node_stack().Pop<SemanticsNodeId>();
auto cond_value_id = context.node_stack().PopExpression();

context.node_stack().Push(if_node);

Expand All @@ -35,10 +35,10 @@ auto SemanticsHandleIfExpressionThen(SemanticsContext& context,

auto SemanticsHandleIfExpressionElse(SemanticsContext& context,
ParseTree::Node else_node) -> bool {
auto else_value_id = context.node_stack().Pop<SemanticsNodeId>();
auto else_value_id = context.node_stack().PopExpression();
auto [then_node, then_end_block_id] =
context.node_stack().PopWithParseNode<ParseNodeKind::IfExpressionThen>();
auto then_value_id = context.node_stack().Pop<SemanticsNodeId>();
auto then_value_id = context.node_stack().PopExpression();
auto if_node =
context.node_stack().PopForSoloParseNode<ParseNodeKind::IfExpressionIf>();

Expand Down
2 changes: 1 addition & 1 deletion toolchain/semantics/semantics_handle_if_statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ auto SemanticsHandleIfConditionStart(SemanticsContext& /*context*/,
auto SemanticsHandleIfCondition(SemanticsContext& context,
ParseTree::Node parse_node) -> bool {
// Convert the condition to `bool`.
auto cond_value_id = context.node_stack().Pop<SemanticsNodeId>();
auto cond_value_id = context.node_stack().PopExpression();
cond_value_id = context.ImplicitAsBool(parse_node, cond_value_id);

// Create the then block and the else block, and branch to the right one. If
Expand Down
6 changes: 3 additions & 3 deletions toolchain/semantics/semantics_handle_name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ auto SemanticsHandleMemberAccessExpression(SemanticsContext& context,
ParseTree::Node parse_node) -> bool {
SemanticsStringId name_id = context.node_stack().Pop<ParseNodeKind::Name>();

auto base_id = context.node_stack().Pop<SemanticsNodeId>();
auto base_id = context.node_stack().PopExpression();
auto base = context.semantics_ir().GetNode(base_id);
if (base.kind() == SemanticsNodeKind::Namespace) {
// For a namespace, just resolve the name.
Expand Down Expand Up @@ -92,12 +92,12 @@ auto SemanticsHandleQualifiedDeclaration(SemanticsContext& context,
// QualifiedDeclaration as the first child, and an Identifier or expression as
// the second child.
auto [parse_node2, node_or_name_id2] =
context.node_stack().PopWithParseNode<SemanticsNodeId>();
context.node_stack().PopExpressionWithParseNode();
if (context.parse_tree().node_kind(context.node_stack().PeekParseNode()) !=
ParseNodeKind::QualifiedDeclaration) {
// First QualifiedDeclaration in a chain.
auto [parse_node1, node_or_name_id1] =
context.node_stack().PopWithParseNode<SemanticsNodeId>();
context.node_stack().PopExpressionWithParseNode();
context.ApplyDeclarationNameQualifier(parse_node1, node_or_name_id1);
// Add the QualifiedDeclaration so that it can be used for bracketing.
context.node_stack().Push(parse_node);
Expand Down
8 changes: 4 additions & 4 deletions toolchain/semantics/semantics_handle_operator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace Carbon {

auto SemanticsHandleInfixOperator(SemanticsContext& context,
ParseTree::Node parse_node) -> bool {
auto rhs_id = context.node_stack().Pop<SemanticsNodeId>();
auto lhs_id = context.node_stack().Pop<SemanticsNodeId>();
auto rhs_id = context.node_stack().PopExpression();
auto lhs_id = context.node_stack().PopExpression();

// Figure out the operator for the token.
auto token = context.parse_tree().node_token(parse_node);
Expand Down Expand Up @@ -66,7 +66,7 @@ auto SemanticsHandlePostfixOperator(SemanticsContext& context,

auto SemanticsHandlePrefixOperator(SemanticsContext& context,
ParseTree::Node parse_node) -> bool {
auto value_id = context.node_stack().Pop<SemanticsNodeId>();
auto value_id = context.node_stack().PopExpression();

// Figure out the operator for the token.
auto token = context.parse_tree().node_token(parse_node);
Expand All @@ -90,7 +90,7 @@ auto SemanticsHandlePrefixOperator(SemanticsContext& context,
auto SemanticsHandleShortCircuitOperand(SemanticsContext& context,
ParseTree::Node parse_node) -> bool {
// Convert the condition to `bool`.
auto cond_value_id = context.node_stack().Pop<SemanticsNodeId>();
auto cond_value_id = context.node_stack().PopExpression();
cond_value_id = context.ImplicitAsBool(parse_node, cond_value_id);
auto bool_type_id = context.semantics_ir().GetNode(cond_value_id).type_id();

Expand Down
2 changes: 1 addition & 1 deletion toolchain/semantics/semantics_handle_paren.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Carbon {

auto SemanticsHandleParenExpression(SemanticsContext& context,
ParseTree::Node parse_node) -> bool {
auto value_id = context.node_stack().Pop<SemanticsNodeId>();
auto value_id = context.node_stack().PopExpression();
context.node_stack()
.PopAndDiscardSoloParseNode<
ParseNodeKind::ParenExpressionOrTupleLiteralStart>();
Expand Down
2 changes: 1 addition & 1 deletion toolchain/semantics/semantics_handle_pattern_binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ auto SemanticsHandleGenericPatternBinding(SemanticsContext& context,
auto SemanticsHandlePatternBinding(SemanticsContext& context,
ParseTree::Node parse_node) -> bool {
auto [type_node, parsed_type_id] =
context.node_stack().PopWithParseNode<SemanticsNodeId>();
context.node_stack().PopExpressionWithParseNode();
auto cast_type_id = context.ExpressionAsType(type_node, parsed_type_id);

// Get the name.
Expand Down
4 changes: 2 additions & 2 deletions toolchain/semantics/semantics_handle_statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ auto SemanticsHandleExpressionStatement(SemanticsContext& context,
// Pop the expression without investigating its contents.
// TODO: This will probably eventually need to do some "do not discard"
// analysis.
context.node_stack().PopAndDiscardId();
context.node_stack().PopExpression();
return true;
}

Expand Down Expand Up @@ -42,7 +42,7 @@ auto SemanticsHandleReturnStatement(SemanticsContext& context,

context.AddNode(SemanticsNode::Return::Make(parse_node));
} else {
auto arg = context.node_stack().Pop<SemanticsNodeId>();
auto arg = context.node_stack().PopExpression();
context.node_stack()
.PopAndDiscardSoloParseNode<ParseNodeKind::ReturnStatementStart>();

Expand Down
5 changes: 2 additions & 3 deletions toolchain/semantics/semantics_handle_struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ auto SemanticsHandleStructFieldDesignator(SemanticsContext& context,

auto SemanticsHandleStructFieldType(SemanticsContext& context,
ParseTree::Node parse_node) -> bool {
auto [type_node, type_id] =
context.node_stack().PopWithParseNode<SemanticsNodeId>();
auto [type_node, type_id] = context.node_stack().PopExpressionWithParseNode();
SemanticsTypeId cast_type_id = context.ExpressionAsType(type_node, type_id);

auto [name_node, name_id] =
Expand All @@ -48,7 +47,7 @@ auto SemanticsHandleStructFieldUnknown(SemanticsContext& context,
auto SemanticsHandleStructFieldValue(SemanticsContext& context,
ParseTree::Node parse_node) -> bool {
auto [value_parse_node, value_node_id] =
context.node_stack().PopWithParseNode<SemanticsNodeId>();
context.node_stack().PopExpressionWithParseNode();
SemanticsStringId name_id = context.node_stack().Pop<ParseNodeKind::Name>();

// Store the name for the type.
Expand Down
2 changes: 1 addition & 1 deletion toolchain/semantics/semantics_handle_variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ auto SemanticsHandleVariableDeclaration(SemanticsContext& context,
context.parse_tree().node_kind(context.node_stack().PeekParseNode()) !=
ParseNodeKind::PatternBinding;
if (has_init) {
expr_node_id = context.node_stack().Pop<SemanticsNodeId>();
expr_node_id = context.node_stack().PopExpression();
context.node_stack()
.PopAndDiscardSoloParseNode<ParseNodeKind::VariableInitializer>();
}
Expand Down
Loading

0 comments on commit 43065a1

Please sign in to comment.