mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
element index init with vptr (#4565)
Not sure if this is the most robust way to do it - I guess the alternative is doing name lookup into the dest struct fields too?
This commit is contained in:
+18
-15
@@ -168,30 +168,31 @@ static auto MakeElementAccessInst(Context& context, SemIR::LocId loc_id,
|
||||
// another aggregate.
|
||||
//
|
||||
// For the source: `src_id` is the source aggregate, `src_elem_type` is the
|
||||
// element type, `i` is the index, and `SourceAccessInstT` is the kind of
|
||||
// instruction used to access the source element.
|
||||
// element type, `src_field_index` is the index, and `SourceAccessInstT` is the
|
||||
// kind of instruction used to access the source element.
|
||||
//
|
||||
// For the target: `kind` is the kind of conversion or initialization,
|
||||
// `target_elem_type` is the element type. For initialization, `target_id` is
|
||||
// the destination, `target_block` is a pending block for target location
|
||||
// calculations that will be spliced as the return slot of the initializer if
|
||||
// necessary, `i` is the index, and `TargetAccessInstT` is the kind of
|
||||
// instruction used to access the destination element.
|
||||
// necessary, `target_field_index` is the index, and `TargetAccessInstT` is the
|
||||
// kind of instruction used to access the destination element.
|
||||
template <typename SourceAccessInstT, typename TargetAccessInstT>
|
||||
static auto ConvertAggregateElement(
|
||||
Context& context, SemIR::LocId loc_id, SemIR::InstId src_id,
|
||||
SemIR::TypeId src_elem_type,
|
||||
llvm::ArrayRef<SemIR::InstId> src_literal_elems,
|
||||
ConversionTarget::Kind kind, SemIR::InstId target_id,
|
||||
SemIR::TypeId target_elem_type, PendingBlock* target_block, size_t i) {
|
||||
SemIR::TypeId target_elem_type, PendingBlock* target_block,
|
||||
size_t src_field_index, size_t target_field_index) {
|
||||
// Compute the location of the source element. This goes into the current code
|
||||
// block, not into the target block.
|
||||
// TODO: Ideally we would discard this instruction if it's unused.
|
||||
auto src_elem_id =
|
||||
!src_literal_elems.empty()
|
||||
? src_literal_elems[i]
|
||||
: MakeElementAccessInst<SourceAccessInstT>(context, loc_id, src_id,
|
||||
src_elem_type, context, i);
|
||||
auto src_elem_id = !src_literal_elems.empty()
|
||||
? src_literal_elems[src_field_index]
|
||||
: MakeElementAccessInst<SourceAccessInstT>(
|
||||
context, loc_id, src_id, src_elem_type, context,
|
||||
src_field_index);
|
||||
|
||||
// If we're performing a conversion rather than an initialization, we won't
|
||||
// have or need a target.
|
||||
@@ -204,7 +205,8 @@ static auto ConvertAggregateElement(
|
||||
PendingBlock::DiscardUnusedInstsScope scope(target_block);
|
||||
target.init_block = target_block;
|
||||
target.init_id = MakeElementAccessInst<TargetAccessInstT>(
|
||||
context, loc_id, target_id, target_elem_type, *target_block, i);
|
||||
context, loc_id, target_id, target_elem_type, *target_block,
|
||||
target_field_index);
|
||||
return Convert(context, loc_id, src_elem_id, target);
|
||||
}
|
||||
|
||||
@@ -275,7 +277,7 @@ static auto ConvertTupleToArray(Context& context, SemIR::TupleType tuple_type,
|
||||
ConvertAggregateElement<SemIR::TupleAccess, SemIR::ArrayIndex>(
|
||||
context, value_loc_id, value_id, src_type_id, literal_elems,
|
||||
ConversionTarget::FullInitializer, return_slot_id,
|
||||
array_type.element_type_id, target_block, i);
|
||||
array_type.element_type_id, target_block, i, i);
|
||||
if (init_id == SemIR::InstId::BuiltinErrorInst) {
|
||||
return SemIR::InstId::BuiltinErrorInst;
|
||||
}
|
||||
@@ -356,7 +358,7 @@ static auto ConvertTupleToTuple(Context& context, SemIR::TupleType src_type,
|
||||
auto init_id =
|
||||
ConvertAggregateElement<SemIR::TupleAccess, SemIR::TupleAccess>(
|
||||
context, value_loc_id, value_id, src_type_id, literal_elems,
|
||||
inner_kind, target.init_id, dest_type_id, target.init_block, i);
|
||||
inner_kind, target.init_id, dest_type_id, target.init_block, i, i);
|
||||
if (init_id == SemIR::InstId::BuiltinErrorInst) {
|
||||
return SemIR::InstId::BuiltinErrorInst;
|
||||
}
|
||||
@@ -394,7 +396,8 @@ static auto ConvertStructToStructOrClass(Context& context,
|
||||
auto dest_elem_fields = sem_ir.struct_type_fields().Get(dest_type.fields_id);
|
||||
bool dest_has_vptr = !dest_elem_fields.empty() &&
|
||||
dest_elem_fields.front().name_id == SemIR::NameId::Vptr;
|
||||
auto dest_elem_fields_size = dest_elem_fields.size() - dest_has_vptr;
|
||||
int dest_vptr_offset = (dest_has_vptr ? 1 : 0);
|
||||
auto dest_elem_fields_size = dest_elem_fields.size() - dest_vptr_offset;
|
||||
|
||||
auto value = sem_ir.insts().Get(value_id);
|
||||
auto value_loc_id = sem_ir.insts().GetLocId(value_id);
|
||||
@@ -496,7 +499,7 @@ static auto ConvertStructToStructOrClass(Context& context,
|
||||
ConvertAggregateElement<SemIR::StructAccess, TargetAccessInstT>(
|
||||
context, value_loc_id, value_id, src_field.type_id, literal_elems,
|
||||
inner_kind, target.init_id, dest_field.type_id, target.init_block,
|
||||
src_field_index);
|
||||
src_field_index, src_field_index + dest_vptr_offset);
|
||||
if (init_id == SemIR::InstId::BuiltinErrorInst) {
|
||||
return SemIR::InstId::BuiltinErrorInst;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user