Pull out logic into distinct functions

This commit is contained in:
Michael Goulet 2022-12-07 20:30:42 +00:00
parent 57b722688d
commit 7d23e29f9f

View File

@ -1,3 +1,4 @@
use hir::def_id::DefId;
use rustc_hir as hir;
use rustc_index::bit_set::BitSet;
use rustc_index::vec::{Idx, IndexVec};
@ -6,7 +7,7 @@
IntegerExt, LayoutCx, LayoutError, LayoutOf, TyAndLayout, MAX_SIMD_LANES,
};
use rustc_middle::ty::{
self, subst::SubstsRef, EarlyBinder, ReprOptions, Ty, TyCtxt, TypeVisitable,
self, subst::SubstsRef, AdtDef, EarlyBinder, ReprOptions, Ty, TyCtxt, TypeVisitable,
};
use rustc_session::{DataTypeKind, FieldInfo, SizeKind, VariantInfo};
use rustc_span::symbol::Symbol;
@ -815,13 +816,39 @@ fn record_layout_for_printing_outlined<'tcx>(
};
match *layout.ty.kind() {
ty::Adt(ref adt_def, _) => {
ty::Adt(adt_def, _) => {
debug!("print-type-size t: `{:?}` process adt", layout.ty);
let adt_kind = adt_def.adt_kind();
let adt_packed = adt_def.repr().pack.is_some();
let (variant_infos, opt_discr_size) = variant_info_for_adt(cx, layout, adt_def);
record(adt_kind.into(), adt_packed, opt_discr_size, variant_infos);
}
let build_variant_info =
|n: Option<Symbol>, flds: &[Symbol], layout: TyAndLayout<'tcx>| {
ty::Generator(def_id, substs, _) => {
debug!("print-type-size t: `{:?}` record generator", layout.ty);
// Generators always have a begin/poisoned/end state with additional suspend points
let (variant_infos, opt_discr_size) =
variant_info_for_generator(cx, layout, def_id, substs);
record(DataTypeKind::Generator, false, opt_discr_size, variant_infos);
}
ty::Closure(..) => {
debug!("print-type-size t: `{:?}` record closure", layout.ty);
record(DataTypeKind::Closure, false, None, vec![]);
}
_ => {
debug!("print-type-size t: `{:?}` skip non-nominal", layout.ty);
}
};
}
fn variant_info_for_adt<'tcx>(
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
layout: TyAndLayout<'tcx>,
adt_def: AdtDef<'tcx>,
) -> (Vec<VariantInfo>, Option<Size>) {
let build_variant_info = |n: Option<Symbol>, flds: &[Symbol], layout: TyAndLayout<'tcx>| {
let mut min_size = Size::ZERO;
let field_info: Vec<_> = flds
.iter()
@ -843,11 +870,7 @@ fn record_layout_for_printing_outlined<'tcx>(
name: n,
kind: if layout.is_unsized() { SizeKind::Min } else { SizeKind::Exact },
align: layout.align.abi.bytes(),
size: if min_size.bytes() == 0 {
layout.size.bytes()
} else {
min_size.bytes()
},
size: if min_size.bytes() == 0 { layout.size.bytes() } else { min_size.bytes() },
fields: field_info,
}
};
@ -855,23 +878,12 @@ fn record_layout_for_printing_outlined<'tcx>(
match layout.variants {
Variants::Single { index } => {
if !adt_def.variants().is_empty() && layout.fields != FieldsShape::Primitive {
debug!(
"print-type-size `{:#?}` variant {}",
layout,
adt_def.variant(index).name
);
debug!("print-type-size `{:#?}` variant {}", layout, adt_def.variant(index).name);
let variant_def = &adt_def.variant(index);
let fields: Vec<_> = variant_def.fields.iter().map(|f| f.name).collect();
record(
adt_kind.into(),
adt_packed,
None,
vec![build_variant_info(Some(variant_def.name), &fields, layout)],
);
(vec![build_variant_info(Some(variant_def.name), &fields, layout)], None)
} else {
// (This case arises for *empty* enums; so give it
// zero variants.)
record(adt_kind.into(), adt_packed, None, vec![]);
(vec![], None)
}
}
@ -885,35 +897,33 @@ fn record_layout_for_printing_outlined<'tcx>(
.variants()
.iter_enumerated()
.map(|(i, variant_def)| {
let fields: Vec<_> =
variant_def.fields.iter().map(|f| f.name).collect();
build_variant_info(
Some(variant_def.name),
&fields,
layout.for_variant(cx, i),
)
let fields: Vec<_> = variant_def.fields.iter().map(|f| f.name).collect();
build_variant_info(Some(variant_def.name), &fields, layout.for_variant(cx, i))
})
.collect();
record(
adt_kind.into(),
adt_packed,
(
variant_infos,
match tag_encoding {
TagEncoding::Direct => Some(tag.size(cx)),
_ => None,
},
variant_infos,
);
}
)
}
}
}
ty::Generator(def_id, substs, _) => {
debug!("print-type-size t: `{:?}` record generator", layout.ty);
// Generators always have a begin/poisoned/end state with additional suspend points
match layout.variants {
Variants::Multiple { tag, ref tag_encoding, .. } => {
let (generator, state_specific_names) =
cx.tcx.generator_layout_and_saved_local_names(def_id);
fn variant_info_for_generator<'tcx>(
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
layout: TyAndLayout<'tcx>,
def_id: DefId,
substs: ty::SubstsRef<'tcx>,
) -> (Vec<VariantInfo>, Option<Size>) {
let Variants::Multiple { tag, ref tag_encoding, .. } = layout.variants else {
return (vec![], None);
};
let (generator, state_specific_names) = cx.tcx.generator_layout_and_saved_local_names(def_id);
let upvar_names = cx.tcx.closure_saved_names_of_captured_variables(def_id);
let mut upvars_size = Size::ZERO;
@ -950,14 +960,9 @@ fn record_layout_for_printing_outlined<'tcx>(
// The struct is as large as the last field's end
variant_size = variant_size.max(offset + field_layout.size);
FieldInfo {
name: state_specific_names
.get(*local)
.copied()
.flatten()
.unwrap_or(Symbol::intern(&format!(
".generator_field{}",
local.as_usize()
))),
name: state_specific_names.get(*local).copied().flatten().unwrap_or(
Symbol::intern(&format!(".generator_field{}", local.as_usize())),
),
offset: offset.bytes(),
size: field_layout.size.bytes(),
align: field_layout.align.abi.bytes(),
@ -978,9 +983,7 @@ fn record_layout_for_printing_outlined<'tcx>(
};
VariantInfo {
name: Some(Symbol::intern(&ty::GeneratorSubsts::variant_name(
variant_idx,
))),
name: Some(Symbol::intern(&ty::GeneratorSubsts::variant_name(variant_idx))),
kind: SizeKind::Exact,
size: variant_size.bytes(),
align: variant_layout.align.abi.bytes(),
@ -988,33 +991,11 @@ fn record_layout_for_printing_outlined<'tcx>(
}
})
.collect();
record(
DataTypeKind::Generator,
false,
(
variant_infos,
match tag_encoding {
TagEncoding::Direct => Some(tag.size(cx)),
_ => None,
},
variant_infos,
);
}
_ => {
// This should never happen, but I would rather not panic.
record(DataTypeKind::Generator, false, None, vec![]);
return;
}
}
}
ty::Closure(..) => {
debug!("print-type-size t: `{:?}` record closure", layout.ty);
record(DataTypeKind::Closure, false, None, vec![]);
return;
}
_ => {
debug!("print-type-size t: `{:?}` skip non-nominal", layout.ty);
return;
}
};
)
}