Add FieldStore and fix field initializer imports (#7287)

As suggested in [1], replace `FieldInitializerMap` with a `FieldStore`.
The corresponding `FieldId` is now stored in `FieldDecl`. To make room
for the `FieldId`, the `ElementIndex` is now stored in the `Field`,
along with the initializer.

In import_ref.cpp, resolving `FieldDecl` initializers is now supported,
and in convert.cpp `LoadImportRef` is called to do so. The
`field_initializer_import.carbon` test now passes.

Printing a `FieldDecl` instruction now prints the initializer as well,
if present. See field_initializer.carbon for an example.

[1]:
https://github.com/carbon-language/carbon-lang/pull/7238#discussion_r3283217158
This commit is contained in:
Nicholas Bishop
2026-06-01 19:02:01 +00:00
committed by GitHub
parent 14a213d095
commit 682f9fef16
39 changed files with 287 additions and 204 deletions
+6 -1
View File
@@ -798,6 +798,10 @@ static auto ImportClassObjectRepr(Context& context, SemIR::ClassId class_id,
// Create a field now, as we know the index to use.
// TODO: Consider doing this lazily instead.
auto field_id =
context.fields().Add({.index = SemIR::ElementIndex(fields.size()),
// TODO: import initializers.
.initializer_id = SemIR::InstId::None});
auto field_decl_id = AddInst(
context, SemIR::LocIdAndInst::RuntimeVerified(
context.sem_ir(), import_ir_inst_id,
@@ -805,7 +809,8 @@ static auto ImportClassObjectRepr(Context& context, SemIR::ClassId class_id,
.type_id = GetUnboundElementType(
context, class_type_inst_id, field_type_inst_id),
.name_id = field_name_id,
.index = SemIR::ElementIndex(fields.size())}));
.field_id = field_id,
}));
// The imported SemIR::FieldDecl represents the original declaration `decl`,
// which is either the field or the indirect field declaration.
auto key = SemIR::ClangDeclKey::ForNonFunctionDecl(decl);