|
| 1 | +// Adapted from https://github.com/rust-lang/rust/blob/10a7aa14fed9b528b74b0f098c4899c37c09a9c7/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs |
| 2 | + |
| 3 | +use gimli::write::{AttributeValue, UnitEntryId}; |
| 4 | +use rustc_codegen_ssa::debuginfo::type_names; |
| 5 | +use rustc_data_structures::fx::FxHashMap; |
| 6 | +use rustc_middle::ty::{self, Ty, TyCtxt}; |
| 7 | + |
| 8 | +use crate::{has_ptr_meta, DebugContext}; |
| 9 | + |
| 10 | +#[derive(Default)] |
| 11 | +pub(crate) struct TypeDebugContext<'tcx> { |
| 12 | + type_map: FxHashMap<Ty<'tcx>, UnitEntryId>, |
| 13 | +} |
| 14 | + |
| 15 | +/// Returns from the enclosing function if the type debuginfo node with the given |
| 16 | +/// unique ID can be found in the type map. |
| 17 | +macro_rules! return_if_type_created_in_meantime { |
| 18 | + ($type_dbg:expr, $ty:expr) => { |
| 19 | + if let Some(&type_id) = $type_dbg.type_map.get(&$ty) { |
| 20 | + return type_id; |
| 21 | + } |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +impl DebugContext { |
| 26 | + pub(crate) fn debug_type<'tcx>( |
| 27 | + &mut self, |
| 28 | + tcx: TyCtxt<'tcx>, |
| 29 | + type_dbg: &mut TypeDebugContext<'tcx>, |
| 30 | + ty: Ty<'tcx>, |
| 31 | + ) -> UnitEntryId { |
| 32 | + if let Some(&type_id) = type_dbg.type_map.get(&ty) { |
| 33 | + return type_id; |
| 34 | + } |
| 35 | + |
| 36 | + let type_id = match ty.kind() { |
| 37 | + ty::Never | ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) => { |
| 38 | + self.basic_type(tcx, ty) |
| 39 | + } |
| 40 | + ty::Tuple(elems) if elems.is_empty() => self.basic_type(tcx, ty), |
| 41 | + ty::Array(elem_ty, len) => self.array_type( |
| 42 | + tcx, |
| 43 | + type_dbg, |
| 44 | + *elem_ty, |
| 45 | + len.eval_target_usize(tcx, ty::ParamEnv::reveal_all()), |
| 46 | + ), |
| 47 | + // ty::Slice(_) | ty::Str |
| 48 | + // ty::Dynamic |
| 49 | + // ty::Foreign |
| 50 | + ty::RawPtr(pointee_type, _) | ty::Ref(_, pointee_type, _) => { |
| 51 | + self.pointer_type(tcx, type_dbg, ty, *pointee_type) |
| 52 | + } |
| 53 | + // ty::Adt(def, args) if def.is_box() && args.get(1).map_or(true, |arg| cx.layout_of(arg.expect_ty()).is_1zst()) |
| 54 | + // ty::FnDef(..) | ty::FnPtr(..) |
| 55 | + // ty::Closure(..) |
| 56 | + // ty::Adt(def, ..) |
| 57 | + // ty::Tuple(_) |
| 58 | + // ty::Param(_) |
| 59 | + // FIXME implement remaining types and add unreachable!() to the fallback branch |
| 60 | + _ => self.placeholder_for_type(tcx, type_dbg, ty), |
| 61 | + }; |
| 62 | + |
| 63 | + type_dbg.type_map.insert(ty, type_id); |
| 64 | + |
| 65 | + type_id |
| 66 | + } |
| 67 | + |
| 68 | + fn basic_type<'tcx>(&mut self, tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> UnitEntryId { |
| 69 | + let (name, encoding) = match ty.kind() { |
| 70 | + ty::Never => ("!", gimli::DW_ATE_unsigned), |
| 71 | + ty::Tuple(elems) if elems.is_empty() => ("()", gimli::DW_ATE_unsigned), |
| 72 | + ty::Bool => ("bool", gimli::DW_ATE_boolean), |
| 73 | + ty::Char => ("char", gimli::DW_ATE_UTF), |
| 74 | + ty::Int(int_ty) => (int_ty.name_str(), gimli::DW_ATE_signed), |
| 75 | + ty::Uint(uint_ty) => (uint_ty.name_str(), gimli::DW_ATE_unsigned), |
| 76 | + ty::Float(float_ty) => (float_ty.name_str(), gimli::DW_ATE_float), |
| 77 | + _ => unreachable!(), |
| 78 | + }; |
| 79 | + |
| 80 | + let type_id = self.dwarf.unit.add(self.dwarf.unit.root(), gimli::DW_TAG_base_type); |
| 81 | + let type_entry = self.dwarf.unit.get_mut(type_id); |
| 82 | + type_entry.set(gimli::DW_AT_name, AttributeValue::StringRef(self.dwarf.strings.add(name))); |
| 83 | + type_entry.set(gimli::DW_AT_encoding, AttributeValue::Encoding(encoding)); |
| 84 | + type_entry.set( |
| 85 | + gimli::DW_AT_byte_size, |
| 86 | + AttributeValue::Udata( |
| 87 | + tcx.layout_of(ty::ParamEnv::reveal_all().and(ty)).expect("FIXME").size.bytes(), |
| 88 | + ), |
| 89 | + ); |
| 90 | + |
| 91 | + type_id |
| 92 | + } |
| 93 | + |
| 94 | + fn array_type<'tcx>( |
| 95 | + &mut self, |
| 96 | + tcx: TyCtxt<'tcx>, |
| 97 | + type_dbg: &mut TypeDebugContext<'tcx>, |
| 98 | + elem_ty: Ty<'tcx>, |
| 99 | + len: u64, |
| 100 | + ) -> UnitEntryId { |
| 101 | + let elem_dw_ty = self.debug_type(tcx, type_dbg, elem_ty); |
| 102 | + |
| 103 | + return_if_type_created_in_meantime!(type_dbg, elem_ty); |
| 104 | + |
| 105 | + let array_type_id = self.dwarf.unit.add(self.dwarf.unit.root(), gimli::DW_TAG_array_type); |
| 106 | + let array_type_entry = self.dwarf.unit.get_mut(array_type_id); |
| 107 | + array_type_entry.set(gimli::DW_AT_type, AttributeValue::UnitRef(elem_dw_ty)); |
| 108 | + |
| 109 | + let subrange_id = self.dwarf.unit.add(array_type_id, gimli::DW_TAG_subrange_type); |
| 110 | + let subrange_entry = self.dwarf.unit.get_mut(subrange_id); |
| 111 | + subrange_entry.set(gimli::DW_AT_type, AttributeValue::UnitRef(self.array_size_type)); |
| 112 | + subrange_entry.set(gimli::DW_AT_lower_bound, AttributeValue::Udata(0)); |
| 113 | + subrange_entry.set(gimli::DW_AT_count, AttributeValue::Udata(len)); |
| 114 | + |
| 115 | + array_type_id |
| 116 | + } |
| 117 | + |
| 118 | + fn pointer_type<'tcx>( |
| 119 | + &mut self, |
| 120 | + tcx: TyCtxt<'tcx>, |
| 121 | + type_dbg: &mut TypeDebugContext<'tcx>, |
| 122 | + ptr_type: Ty<'tcx>, |
| 123 | + pointee_type: Ty<'tcx>, |
| 124 | + ) -> UnitEntryId { |
| 125 | + let pointee_dw_ty = self.debug_type(tcx, type_dbg, pointee_type); |
| 126 | + |
| 127 | + return_if_type_created_in_meantime!(type_dbg, ptr_type); |
| 128 | + |
| 129 | + let name = type_names::compute_debuginfo_type_name(tcx, ptr_type, true); |
| 130 | + |
| 131 | + if !has_ptr_meta(tcx, ptr_type) { |
| 132 | + let pointer_type_id = |
| 133 | + self.dwarf.unit.add(self.dwarf.unit.root(), gimli::DW_TAG_pointer_type); |
| 134 | + let pointer_entry = self.dwarf.unit.get_mut(pointer_type_id); |
| 135 | + pointer_entry.set(gimli::DW_AT_type, AttributeValue::UnitRef(pointee_dw_ty)); |
| 136 | + pointer_entry |
| 137 | + .set(gimli::DW_AT_name, AttributeValue::StringRef(self.dwarf.strings.add(name))); |
| 138 | + |
| 139 | + pointer_type_id |
| 140 | + } else { |
| 141 | + // FIXME implement debuginfo for fat pointers |
| 142 | + self.placeholder_for_type(tcx, type_dbg, ptr_type) |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + fn placeholder_for_type<'tcx>( |
| 147 | + &mut self, |
| 148 | + tcx: TyCtxt<'tcx>, |
| 149 | + type_dbg: &mut TypeDebugContext<'tcx>, |
| 150 | + ty: Ty<'tcx>, |
| 151 | + ) -> UnitEntryId { |
| 152 | + self.debug_type( |
| 153 | + tcx, |
| 154 | + type_dbg, |
| 155 | + Ty::new_array( |
| 156 | + tcx, |
| 157 | + tcx.types.u8, |
| 158 | + tcx.layout_of(ty::ParamEnv::reveal_all().and(ty)).unwrap().size.bytes(), |
| 159 | + ), |
| 160 | + ) |
| 161 | + } |
| 162 | +} |
0 commit comments