mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-26 08:21:28 +00:00
Add check support for struct patterns (#7876)
Implements check and pattern matching support for struct patterns. This PR implements complete patterns, and leaves `_` field handling for future work.
This commit is contained in:
1 parent
3ee012b86a
commit
c6b54b57e2
23 files changed
+1764
-110
No files matched your search
@@ -9,8 +9,10 @@
|
||||
#include "toolchain/check/handle.h"
|
||||
#include "toolchain/check/inst.h"
|
||||
#include "toolchain/check/pattern.h"
|
||||
#include "toolchain/check/struct.h"
|
||||
#include "toolchain/check/type.h"
|
||||
#include "toolchain/diagnostics/emitter.h"
|
||||
#include "toolchain/diagnostics/format_providers.h"
|
||||
|
||||
namespace Carbon::Check {
|
||||
|
||||
@@ -39,7 +41,12 @@ auto HandleParseNode(Context& context, Parse::TuplePatternStartId node_id)
|
||||
|
||||
auto HandleParseNode(Context& context, Parse::StructPatternStartId node_id)
|
||||
-> bool {
|
||||
return context.TODO(node_id, "struct pattern start");
|
||||
EndEmptyExprRegionForPattern(context);
|
||||
context.node_stack().Push(node_id);
|
||||
context.struct_type_fields_stack().PushArray();
|
||||
context.param_and_arg_refs_stack().Push();
|
||||
BeginExprRegionForPattern(context);
|
||||
return true;
|
||||
}
|
||||
|
||||
auto HandleParseNode(Context& context, Parse::ExplicitParamListStartId node_id)
|
||||
@@ -137,12 +144,101 @@ auto HandleParseNode(Context& context, Parse::TuplePatternId node_id) -> bool {
|
||||
}
|
||||
|
||||
auto HandleParseNode(Context& context, Parse::StructPatternId node_id) -> bool {
|
||||
return context.TODO(node_id, "struct pattern");
|
||||
if (context.node_stack().PeekIs(Parse::NodeKind::StructPatternStart)) {
|
||||
// End the pending region started by a trailing comma, or the opening
|
||||
// delimiter of an empty list.
|
||||
EndEmptyExprRegionForPattern(context);
|
||||
} else {
|
||||
// End the pending region for the last pattern in the list.
|
||||
EndExprRegionForPattern(context, context.node_stack());
|
||||
}
|
||||
|
||||
// TODO: Reject StructPatterns in classes during Parse, or treat patterns in a
|
||||
// class scope as patterns and generate the FieldDecl insts in a later
|
||||
// pattern-matching step.
|
||||
if (context.scope_stack().TryGetCurrentScopeAs<SemIR::ClassDecl>()) {
|
||||
bool is_var = context.full_pattern_stack().IsCurrentKindClassScopeVarDecl();
|
||||
CARBON_DIAGNOSTIC(
|
||||
FieldWithStructPattern, Error,
|
||||
"found struct pattern in class member {0:var|let} declaration",
|
||||
Diagnostics::BoolAsSelect);
|
||||
context.emitter().Emit(node_id, FieldWithStructPattern, is_var);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (context.node_stack().PeekIs(Parse::NodeKind::UnderscoreName)) {
|
||||
return context.TODO(node_id, "Struct pattern underscore field");
|
||||
}
|
||||
|
||||
if (!context.node_stack().PeekIs(Parse::NodeCategory::MemberName)) {
|
||||
// Remove the last parameter from the node stack before collecting names.
|
||||
context.param_and_arg_refs_stack().EndNoPop(
|
||||
Parse::NodeKind::StructPatternStart);
|
||||
}
|
||||
|
||||
auto fields = context.struct_type_fields_stack().PeekArray();
|
||||
|
||||
llvm::SmallVector<Parse::NodeId> field_name_nodes =
|
||||
PopStructFieldNameNodes(context, fields.size());
|
||||
|
||||
auto refs_id = context.param_and_arg_refs_stack().EndAndPop(
|
||||
Parse::NodeKind::StructPatternStart);
|
||||
|
||||
context.node_stack()
|
||||
.PopAndDiscardSoloNodeId<Parse::NodeKind::StructPatternStart>();
|
||||
|
||||
if (DiagnoseDuplicateNames(context, field_name_nodes, fields,
|
||||
StructKind::StructPattern)) {
|
||||
context.node_stack().Push(node_id, SemIR::ErrorInst::InstId);
|
||||
} else {
|
||||
auto type_id = GetPatternType(
|
||||
context,
|
||||
GetStructType(context,
|
||||
context.struct_type_fields().AddCanonical(fields)));
|
||||
|
||||
context.node_stack().Push(
|
||||
node_id,
|
||||
AddInst<SemIR::StructPattern>(
|
||||
context, node_id, {.type_id = type_id, .elements_id = refs_id}));
|
||||
}
|
||||
|
||||
context.struct_type_fields_stack().PopArray();
|
||||
BeginExprRegionForPattern(context);
|
||||
return true;
|
||||
}
|
||||
|
||||
auto HandleParseNode(Context& context,
|
||||
Parse::StructPatternDesignatedFieldId node_id) -> bool {
|
||||
return context.TODO(node_id, "struct pattern field");
|
||||
EndExprRegionForPattern(context, context.node_stack());
|
||||
|
||||
auto pattern_id = context.node_stack().PopPattern();
|
||||
auto pattern_type_id = context.insts().Get(pattern_id).type_id();
|
||||
auto name_id = context.node_stack().Peek<Parse::NodeCategory::MemberName>();
|
||||
|
||||
// NOTE: This check is required for the case when the struct pattern is found
|
||||
// in a class field, so the node_stack contains FieldDecls instead of
|
||||
// Patterns. See the TODO in the HandleParseNode function for the
|
||||
// StructPatternId.
|
||||
if (context.sem_ir().types().TryGetAs<SemIR::PatternType>(pattern_type_id)) {
|
||||
auto type_id = ExtractScrutineeType(context.sem_ir(), pattern_type_id);
|
||||
|
||||
auto type_inst = context.types().GetTypeInstId(type_id);
|
||||
|
||||
context.struct_type_fields_stack().AppendToTop(
|
||||
{.name_id = name_id, .type_inst_id = type_inst});
|
||||
|
||||
context.node_stack().Push(node_id, pattern_id);
|
||||
} else {
|
||||
context.struct_type_fields_stack().AppendToTop(
|
||||
{.name_id = name_id, .type_inst_id = SemIR::ErrorInst::TypeInstId});
|
||||
context.node_stack().Push(node_id, pattern_id);
|
||||
}
|
||||
|
||||
// Start a new pending `ExprRegion`, to maintain the invariant that one is
|
||||
// pending at the end of handling for a pattern.
|
||||
BeginExprRegionForPattern(context);
|
||||
return true;
|
||||
}
|
||||
|
||||
auto HandleParseNode(Context& context, Parse::PatternListCommaId /*node_id*/)
|
||||
|
||||
Reference in new issue
Block a user