mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 18:41:03 +01:00
Parse vars in classes the same as vars in other locations (#7188)
Class vars are still restricted to simple `name: type` bindings, not full patterns. This is now handled in the check phase instead of during parsing. This is in preparation for supporting `static var`.
This commit is contained in:
@@ -6,10 +6,12 @@
|
||||
|
||||
#include "toolchain/base/kind_switch.h"
|
||||
#include "toolchain/check/action.h"
|
||||
#include "toolchain/check/class.h"
|
||||
#include "toolchain/check/control_flow.h"
|
||||
#include "toolchain/check/inst.h"
|
||||
#include "toolchain/check/return.h"
|
||||
#include "toolchain/check/type.h"
|
||||
#include "toolchain/diagnostics/emitter.h"
|
||||
#include "toolchain/sem_ir/inst.h"
|
||||
|
||||
namespace Carbon::Check {
|
||||
@@ -134,6 +136,32 @@ auto AddBindingPattern(Context& context, SemIR::LocId name_loc,
|
||||
}
|
||||
auto type_id = SemIR::ExtractScrutineeType(context.sem_ir(), pattern.type_id);
|
||||
|
||||
// Handle `var` decls in a class by creating a `FieldDecl`.
|
||||
if (context.full_pattern_stack().IsCurrentKindFieldDecl()) {
|
||||
auto class_decl =
|
||||
context.scope_stack().TryGetCurrentScopeAs<SemIR::ClassDecl>();
|
||||
auto name_id = context.entity_names().Get(pattern.entity_name_id).name_id;
|
||||
auto& class_info = context.classes().Get(class_decl->class_id);
|
||||
auto field_type_id = GetUnboundElementType(
|
||||
context, context.types().GetTypeInstId(class_info.self_type_id),
|
||||
context.types().GetTypeInstId(type_id));
|
||||
|
||||
if (name_id == SemIR::NameId::Underscore) {
|
||||
CARBON_DIAGNOSTIC(FieldNamedUnderscore, Error,
|
||||
"expected identifier in field declaration");
|
||||
context.emitter().Emit(name_loc, FieldNamedUnderscore);
|
||||
}
|
||||
|
||||
auto field_id =
|
||||
AddInst<SemIR::FieldDecl>(context, name_loc,
|
||||
{.type_id = field_type_id,
|
||||
.name_id = name_id,
|
||||
.index = SemIR::ElementIndex::None});
|
||||
context.field_decls_stack().AppendToTop(field_id);
|
||||
|
||||
return {.pattern_id = field_id, .bind_id = field_id};
|
||||
}
|
||||
|
||||
auto bind_id = AddInstInNoBlock(
|
||||
context, SemIR::LocIdAndInst::RuntimeVerified(
|
||||
context.sem_ir(), name_loc,
|
||||
|
||||
Reference in New Issue
Block a user