1 // Type Names for Debug Info.
3 use crate::common::CodegenCx;
4 use rustc::hir::def_id::DefId;
5 use rustc::ty::subst::Substs;
6 use rustc::ty::{self, Ty};
7 use rustc_codegen_ssa::traits::*;
11 // Compute the name of the type as it should be stored in debuginfo. Does not do
12 // any caching, i.e., calling the function twice with the same type will also do
13 // the work twice. The `qualified` parameter only affects the first level of the
14 // type name, further levels (i.e., type parameters) are always fully qualified.
15 pub fn compute_debuginfo_type_name<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
19 let mut result = String::with_capacity(64);
20 push_debuginfo_type_name(cx, t, qualified, &mut result);
24 // Pushes the name of the type as it should be stored in debuginfo on the
25 // `output` String. See also compute_debuginfo_type_name().
26 pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
29 output: &mut String) {
30 // When targeting MSVC, emit C++ style type names for compatibility with
31 // .natvis visualizers (and perhaps other existing native debuggers?)
32 let cpp_like_names = cx.sess().target.target.options.is_like_msvc;
35 ty::Bool => output.push_str("bool"),
36 ty::Char => output.push_str("char"),
37 ty::Str => output.push_str("str"),
38 ty::Never => output.push_str("!"),
39 ty::Int(int_ty) => output.push_str(int_ty.ty_to_string()),
40 ty::Uint(uint_ty) => output.push_str(uint_ty.ty_to_string()),
41 ty::Float(float_ty) => output.push_str(float_ty.ty_to_string()),
42 ty::Foreign(def_id) => push_item_name(cx, def_id, qualified, output),
43 ty::Adt(def, substs) => {
44 push_item_name(cx, def.did, qualified, output);
45 push_type_params(cx, substs, output);
47 ty::Tuple(component_types) => {
49 for &component_type in component_types {
50 push_debuginfo_type_name(cx, component_type, true, output);
51 output.push_str(", ");
53 if !component_types.is_empty() {
59 ty::RawPtr(ty::TypeAndMut { ty: inner_type, mutbl } ) => {
64 hir::MutImmutable => output.push_str("const "),
65 hir::MutMutable => output.push_str("mut "),
68 push_debuginfo_type_name(cx, inner_type, true, output);
74 ty::Ref(_, inner_type, mutbl) => {
78 if mutbl == hir::MutMutable {
79 output.push_str("mut ");
82 push_debuginfo_type_name(cx, inner_type, true, output);
88 ty::Array(inner_type, len) => {
90 push_debuginfo_type_name(cx, inner_type, true, output);
91 output.push_str(&format!("; {}", len.unwrap_usize(cx.tcx)));
94 ty::Slice(inner_type) => {
96 output.push_str("slice<");
101 push_debuginfo_type_name(cx, inner_type, true, output);
109 ty::Dynamic(ref trait_data, ..) => {
110 if let Some(principal) = trait_data.principal() {
111 let principal = cx.tcx.normalize_erasing_late_bound_regions(
112 ty::ParamEnv::reveal_all(),
115 push_item_name(cx, principal.def_id, false, output);
116 push_type_params(cx, principal.substs, output);
118 output.push_str("dyn '_");
121 ty::FnDef(..) | ty::FnPtr(_) => {
122 let sig = t.fn_sig(cx.tcx);
123 if sig.unsafety() == hir::Unsafety::Unsafe {
124 output.push_str("unsafe ");
128 if abi != crate::abi::Abi::Rust {
129 output.push_str("extern \"");
130 output.push_str(abi.name());
131 output.push_str("\" ");
134 output.push_str("fn(");
136 let sig = cx.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
137 if !sig.inputs().is_empty() {
138 for ¶meter_type in sig.inputs() {
139 push_debuginfo_type_name(cx, parameter_type, true, output);
140 output.push_str(", ");
147 if !sig.inputs().is_empty() {
148 output.push_str(", ...");
150 output.push_str("...");
156 if !sig.output().is_unit() {
157 output.push_str(" -> ");
158 push_debuginfo_type_name(cx, sig.output(), true, output);
162 output.push_str("closure");
164 ty::Generator(..) => {
165 output.push_str("generator");
169 ty::Placeholder(..) |
170 ty::UnnormalizedProjection(..) |
174 ty::GeneratorWitness(..) |
176 bug!("debuginfo: Trying to create type name for \
177 unexpected type: {:?}", t);
181 fn push_item_name(cx: &CodegenCx<'_, '_>,
184 output: &mut String) {
186 output.push_str(&cx.tcx.crate_name(def_id.krate).as_str());
187 for path_element in cx.tcx.def_path(def_id).data {
188 output.push_str("::");
189 output.push_str(&path_element.data.as_interned_str().as_str());
192 output.push_str(&cx.tcx.item_name(def_id).as_str());
196 // Pushes the type parameters in the given `Substs` to the output string.
197 // This ignores region parameters, since they can't reliably be
198 // reconstructed for items from non-local crates. For local crates, this
199 // would be possible but with inlining and LTO we have to use the least
200 // common denominator - otherwise we would run into conflicts.
201 fn push_type_params<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
202 substs: &Substs<'tcx>,
203 output: &mut String) {
204 if substs.types().next().is_none() {
210 for type_parameter in substs.types() {
211 push_debuginfo_type_name(cx, type_parameter, true, output);
212 output.push_str(", ");