Skip to content

Commit f733b69

Browse files
committed
Emit DW_TAG_structure_type including fields for structs
1 parent 57f0290 commit f733b69

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

src/debuginfo/mod.rs

+36-8
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,22 @@ impl<'tcx> DebugContext<'tcx> {
107107

108108
let new_entry = |dwarf: &mut DwarfUnit, tag| dwarf.unit.add(dwarf.unit.root(), tag);
109109

110-
let primtive = |dwarf: &mut DwarfUnit, ate| {
110+
let primitive = |dwarf: &mut DwarfUnit, ate| {
111111
let type_id = new_entry(dwarf, gimli::DW_TAG_base_type);
112112
let type_entry = dwarf.unit.get_mut(type_id);
113113
type_entry.set(gimli::DW_AT_encoding, AttributeValue::Encoding(ate));
114114
type_id
115115
};
116116

117+
let name = format!("{}", ty);
118+
let layout = self.tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap();
119+
117120
let type_id = match ty.kind {
118-
ty::Bool => primtive(&mut self.dwarf, gimli::DW_ATE_boolean),
119-
ty::Char => primtive(&mut self.dwarf, gimli::DW_ATE_UTF),
120-
ty::Uint(_) => primtive(&mut self.dwarf, gimli::DW_ATE_unsigned),
121-
ty::Int(_) => primtive(&mut self.dwarf, gimli::DW_ATE_signed),
122-
ty::Float(_) => primtive(&mut self.dwarf, gimli::DW_ATE_float),
121+
ty::Bool => primitive(&mut self.dwarf, gimli::DW_ATE_boolean),
122+
ty::Char => primitive(&mut self.dwarf, gimli::DW_ATE_UTF),
123+
ty::Uint(_) => primitive(&mut self.dwarf, gimli::DW_ATE_unsigned),
124+
ty::Int(_) => primitive(&mut self.dwarf, gimli::DW_ATE_signed),
125+
ty::Float(_) => primitive(&mut self.dwarf, gimli::DW_ATE_float),
123126
ty::Ref(_, pointee_ty, mutbl)
124127
| ty::RawPtr(ty::TypeAndMut {
125128
ty: pointee_ty,
@@ -139,10 +142,35 @@ impl<'tcx> DebugContext<'tcx> {
139142

140143
type_id
141144
}
145+
ty::Adt(adt_def, _substs) if adt_def.is_struct() && !layout.is_unsized() => {
146+
let type_id = new_entry(&mut self.dwarf, gimli::DW_TAG_structure_type);
147+
148+
// Ensure that type is inserted before recursing to avoid duplicates
149+
self.types.insert(ty, type_id);
150+
151+
let variant = adt_def.non_enum_variant();
152+
153+
for (field_idx, field_def) in variant.fields.iter().enumerate() {
154+
let field_offset = layout.fields.offset(field_idx);
155+
let field_layout = layout.field(&layout::LayoutCx {
156+
tcx: self.tcx,
157+
param_env: ParamEnv::reveal_all(),
158+
}, field_idx).unwrap();
159+
160+
let field_type = self.dwarf_ty(field_layout.ty);
161+
162+
let field_id = self.dwarf.unit.add(type_id, gimli::DW_TAG_member);
163+
let field_entry = self.dwarf.unit.get_mut(field_id);
164+
165+
field_entry.set(gimli::DW_AT_name, AttributeValue::String(field_def.ident.as_str().to_string().into_bytes()));
166+
field_entry.set(gimli::DW_AT_data_member_location, AttributeValue::Udata(field_offset.bytes()));
167+
field_entry.set(gimli::DW_AT_type, AttributeValue::ThisUnitEntryRef(field_type));
168+
}
169+
170+
type_id
171+
}
142172
_ => new_entry(&mut self.dwarf, gimli::DW_TAG_structure_type),
143173
};
144-
let name = format!("{}", ty);
145-
let layout = self.tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap();
146174

147175
let type_entry = self.dwarf.unit.get_mut(type_id);
148176

0 commit comments

Comments
 (0)