Skip to content

Commit

Permalink
Rename ParamList to TuplePattern (carbon-language#3479)
Browse files Browse the repository at this point in the history
Co-authored-by: Richard Smith <[email protected]>
  • Loading branch information
geoffromer and zygoloid authored Dec 8, 2023
1 parent 407079c commit 6e65a30
Show file tree
Hide file tree
Showing 132 changed files with 524 additions and 500 deletions.
4 changes: 3 additions & 1 deletion toolchain/check/handle_binding_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,11 @@ auto HandleBindingPattern(Context& context, Parse::NodeId parse_node) -> bool {
}

case Parse::NodeKind::ImplicitParamListStart:
case Parse::NodeKind::ParamListStart:
case Parse::NodeKind::TuplePatternStart:
// Parameters can have incomplete types in a function declaration, but not
// in a function definition. We don't know which kind we have here.
// TODO: A tuple pattern can appear in other places than function
// parameters.
context.AddInstAndPush(parse_node,
SemIR::Param{name_node, cast_type_id, name_id});
// TODO: Create a `BindName` instruction.
Expand Down
2 changes: 1 addition & 1 deletion toolchain/check/handle_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static auto BuildFunctionDecl(Context& context, bool is_definition)
}

SemIR::InstBlockId param_refs_id =
context.node_stack().Pop<Parse::NodeKind::ParamList>();
context.node_stack().Pop<Parse::NodeKind::TuplePattern>();
SemIR::InstBlockId implicit_param_refs_id =
context.node_stack().PopIf<Parse::NodeKind::ImplicitParamList>().value_or(
SemIR::InstBlockId::Empty);
Expand Down
2 changes: 1 addition & 1 deletion toolchain/check/handle_let.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Carbon::Check {

auto HandleLetDecl(Context& context, Parse::NodeId parse_node) -> bool {
auto value_id = context.node_stack().PopExpr();
if (context.node_stack().PeekIs<Parse::NodeKind::ParamList>()) {
if (context.node_stack().PeekIs<Parse::NodeKind::TuplePattern>()) {
return context.TODO(parse_node, "tuple pattern in let");
}
SemIR::InstId pattern_id =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,24 @@ auto HandleImplicitParamListStart(Context& context, Parse::NodeId parse_node)
return true;
}

auto HandleParamList(Context& context, Parse::NodeId parse_node) -> bool {
auto refs_id = context.ParamOrArgEnd(Parse::NodeKind::ParamListStart);
auto HandleTuplePattern(Context& context, Parse::NodeId parse_node) -> bool {
auto refs_id = context.ParamOrArgEnd(Parse::NodeKind::TuplePatternStart);
context.PopScope();
context.node_stack()
.PopAndDiscardSoloParseNode<Parse::NodeKind::ParamListStart>();
.PopAndDiscardSoloParseNode<Parse::NodeKind::TuplePatternStart>();
context.node_stack().Push(parse_node, refs_id);
return true;
}

auto HandleParamListComma(Context& context, Parse::NodeId /*parse_node*/)
auto HandlePatternListComma(Context& context, Parse::NodeId /*parse_node*/)
-> bool {
context.ParamOrArgComma();
return true;
}

auto HandleParamListStart(Context& context, Parse::NodeId parse_node) -> bool {
// A parameter list following an implicit parameter list shares the same
auto HandleTuplePatternStart(Context& context, Parse::NodeId parse_node)
-> bool {
// A tuple pattern following an implicit parameter list shares the same
// scope.
//
// TODO: For a declaration like
Expand Down
4 changes: 2 additions & 2 deletions toolchain/check/handle_variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ auto HandleVariableDecl(Context& context, Parse::NodeId parse_node) -> bool {
auto init_id = SemIR::InstId::Invalid;
Parse::NodeKind next_kind =
context.parse_tree().node_kind(context.node_stack().PeekParseNode());
if (next_kind == Parse::NodeKind::ParamList) {
if (next_kind == Parse::NodeKind::TuplePattern) {
return context.TODO(parse_node, "tuple pattern in var");
}
// TODO: find a more robust way to determine if there was an initializer.
Expand All @@ -47,7 +47,7 @@ auto HandleVariableDecl(Context& context, Parse::NodeId parse_node) -> bool {
.PopAndDiscardSoloParseNode<Parse::NodeKind::VariableInitializer>();
}

if (context.node_stack().PeekIs<Parse::NodeKind::ParamList>()) {
if (context.node_stack().PeekIs<Parse::NodeKind::TuplePattern>()) {
return context.TODO(parse_node, "tuple pattern in var");
}

Expand Down
4 changes: 2 additions & 2 deletions toolchain/check/node_stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ class NodeStack {
case Parse::NodeKind::IfCondition:
case Parse::NodeKind::IfExprIf:
case Parse::NodeKind::ImplicitParamList:
case Parse::NodeKind::ParamList:
case Parse::NodeKind::TuplePattern:
case Parse::NodeKind::WhileCondition:
case Parse::NodeKind::WhileConditionStart:
return IdKind::InstBlockId;
Expand All @@ -371,13 +371,13 @@ class NodeStack {
case Parse::NodeKind::ImplicitParamListStart:
case Parse::NodeKind::InterfaceIntroducer:
case Parse::NodeKind::LetIntroducer:
case Parse::NodeKind::ParamListStart:
case Parse::NodeKind::QualifiedDecl:
case Parse::NodeKind::ReturnedModifier:
case Parse::NodeKind::ReturnStatementStart:
case Parse::NodeKind::ReturnVarModifier:
case Parse::NodeKind::SelfValueName:
case Parse::NodeKind::StructLiteralOrStructTypeLiteralStart:
case Parse::NodeKind::TuplePatternStart:
case Parse::NodeKind::VariableInitializer:
case Parse::NodeKind::VariableIntroducer:
return IdKind::SoloParseNode;
Expand Down
6 changes: 3 additions & 3 deletions toolchain/parse/handle_decl_name_and_params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ static auto HandleDeclNameAndParamsAfterName(Context& context, Params params)

if (context.PositionIs(Lex::TokenKind::OpenSquareBracket)) {
context.PushState(State::DeclNameAndParamsAfterImplicit);
context.PushState(State::ParamListAsImplicit);
context.PushState(State::PatternListAsImplicit);
} else if (context.PositionIs(Lex::TokenKind::OpenParen)) {
context.PushState(State::ParamListAsRegular);
context.PushState(State::PatternListAsTuple);
} else if (params == Params::Required) {
CARBON_DIAGNOSTIC(ParamsRequiredByIntroducer, Error,
"`{0}` requires a `(` for parameters.", Lex::TokenKind);
Expand All @@ -114,7 +114,7 @@ auto HandleDeclNameAndParamsAfterImplicit(Context& context) -> void {
context.PopAndDiscardState();

if (context.PositionIs(Lex::TokenKind::OpenParen)) {
context.PushState(State::ParamListAsRegular);
context.PushState(State::PatternListAsTuple);
} else {
CARBON_DIAGNOSTIC(
ParamsRequiredAfterImplicit, Error,
Expand Down
98 changes: 0 additions & 98 deletions toolchain/parse/handle_param.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion toolchain/parse/handle_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Carbon::Parse {
auto HandlePattern(Context& context) -> void {
context.PopAndDiscardState();
if (context.PositionKind() == Lex::TokenKind::OpenParen) {
context.PushState(State::ParamListAsRegular);
context.PushState(State::PatternListAsTuple);
} else {
context.PushState(State::BindingPattern);
}
Expand Down
103 changes: 103 additions & 0 deletions toolchain/parse/handle_pattern_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// 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/parse/context.h"

namespace Carbon::Parse {

// Handles PatternListElementAs(Implicit|Tuple).
static auto HandlePatternListElement(Context& context, State pattern_state,
State finish_state) -> void {
context.PopAndDiscardState();

context.PushState(finish_state);
context.PushState(pattern_state);
}

auto HandlePatternListElementAsImplicit(Context& context) -> void {
HandlePatternListElement(context, State::BindingPattern,
State::PatternListElementFinishAsImplicit);
}

auto HandlePatternListElementAsTuple(Context& context) -> void {
HandlePatternListElement(context, State::BindingPattern,
State::PatternListElementFinishAsTuple);
}

// Handles PatternListElementFinishAs(Implicit|Tuple).
static auto HandlePatternListElementFinish(Context& context,
Lex::TokenKind close_token,
State param_state) -> void {
auto state = context.PopState();

if (state.has_error) {
context.ReturnErrorOnState();
}

if (context.ConsumeListToken(NodeKind::PatternListComma, close_token,
state.has_error) ==
Context::ListTokenKind::Comma) {
context.PushState(param_state);
}
}

auto HandlePatternListElementFinishAsImplicit(Context& context) -> void {
HandlePatternListElementFinish(context, Lex::TokenKind::CloseSquareBracket,
State::PatternListElementAsImplicit);
}

auto HandlePatternListElementFinishAsTuple(Context& context) -> void {
HandlePatternListElementFinish(context, Lex::TokenKind::CloseParen,
State::PatternListElementAsTuple);
}

// Handles PatternListAs(Implicit|Tuple).
static auto HandlePatternList(Context& context, NodeKind parse_node_kind,
Lex::TokenKind open_token_kind,
Lex::TokenKind close_token_kind,
State param_state, State finish_state) -> void {
context.PopAndDiscardState();

context.PushState(finish_state);
context.AddLeafNode(parse_node_kind, context.ConsumeChecked(open_token_kind));

if (!context.PositionIs(close_token_kind)) {
context.PushState(param_state);
}
}

auto HandlePatternListAsImplicit(Context& context) -> void {
HandlePatternList(
context, NodeKind::ImplicitParamListStart,
Lex::TokenKind::OpenSquareBracket, Lex::TokenKind::CloseSquareBracket,
State::PatternListElementAsImplicit, State::PatternListFinishAsImplicit);
}

auto HandlePatternListAsTuple(Context& context) -> void {
HandlePatternList(context, NodeKind::TuplePatternStart,
Lex::TokenKind::OpenParen, Lex::TokenKind::CloseParen,
State::PatternListElementAsTuple,
State::PatternListFinishAsTuple);
}

// Handles PatternListFinishAs(Implicit|Tuple).
static auto HandlePatternListFinish(Context& context, NodeKind parse_node_kind,
Lex::TokenKind token_kind) -> void {
auto state = context.PopState();

context.AddNode(parse_node_kind, context.ConsumeChecked(token_kind),
state.subtree_start, state.has_error);
}

auto HandlePatternListFinishAsImplicit(Context& context) -> void {
HandlePatternListFinish(context, NodeKind::ImplicitParamList,
Lex::TokenKind::CloseSquareBracket);
}

auto HandlePatternListFinishAsTuple(Context& context) -> void {
HandlePatternListFinish(context, NodeKind::TuplePattern,
Lex::TokenKind::CloseParen);
}

} // namespace Carbon::Parse
Loading

0 comments on commit 6e65a30

Please sign in to comment.