1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // Type Names for Debug Info.
13 use common::CrateContext;
14 use rustc::hir::def_id::DefId;
15 use rustc::ty::subst::Substs;
16 use rustc::ty::{self, Ty};
20 // Compute the name of the type as it should be stored in debuginfo. Does not do
21 // any caching, i.e. calling the function twice with the same type will also do
22 // the work twice. The `qualified` parameter only affects the first level of the
23 // type name, further levels (i.e. type parameters) are always fully qualified.
24 pub fn compute_debuginfo_type_name<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
28 let mut result = String::with_capacity(64);
29 push_debuginfo_type_name(cx, t, qualified, &mut result);
33 // Pushes the name of the type as it should be stored in debuginfo on the
34 // `output` String. See also compute_debuginfo_type_name().
35 pub fn push_debuginfo_type_name<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
38 output: &mut String) {
40 ty::TyBool => output.push_str("bool"),
41 ty::TyChar => output.push_str("char"),
42 ty::TyStr => output.push_str("str"),
43 ty::TyNever => output.push_str("!"),
44 ty::TyInt(int_ty) => output.push_str(int_ty.ty_to_string()),
45 ty::TyUint(uint_ty) => output.push_str(uint_ty.ty_to_string()),
46 ty::TyFloat(float_ty) => output.push_str(float_ty.ty_to_string()),
47 ty::TyAdt(def, substs) => {
48 push_item_name(cx, def.did, qualified, output);
49 push_type_params(cx, substs, output);
51 ty::TyTuple(component_types) => {
53 for &component_type in component_types {
54 push_debuginfo_type_name(cx, component_type, true, output);
55 output.push_str(", ");
57 if !component_types.is_empty() {
63 ty::TyBox(inner_type) => {
64 output.push_str("Box<");
65 push_debuginfo_type_name(cx, inner_type, true, output);
68 ty::TyRawPtr(ty::TypeAndMut { ty: inner_type, mutbl } ) => {
71 hir::MutImmutable => output.push_str("const "),
72 hir::MutMutable => output.push_str("mut "),
75 push_debuginfo_type_name(cx, inner_type, true, output);
77 ty::TyRef(_, ty::TypeAndMut { ty: inner_type, mutbl }) => {
79 if mutbl == hir::MutMutable {
80 output.push_str("mut ");
83 push_debuginfo_type_name(cx, inner_type, true, output);
85 ty::TyArray(inner_type, len) => {
87 push_debuginfo_type_name(cx, inner_type, true, output);
88 output.push_str(&format!("; {}", len));
91 ty::TySlice(inner_type) => {
93 push_debuginfo_type_name(cx, inner_type, true, output);
96 ty::TyDynamic(ref trait_data, ..) => {
97 if let Some(principal) = trait_data.principal() {
98 let principal = cx.tcx().erase_late_bound_regions_and_normalize(
100 push_item_name(cx, principal.def_id, false, output);
101 push_type_params(cx, principal.substs, output);
104 ty::TyFnDef(.., &ty::BareFnTy{ unsafety, abi, ref sig } ) |
105 ty::TyFnPtr(&ty::BareFnTy{ unsafety, abi, ref sig } ) => {
106 if unsafety == hir::Unsafety::Unsafe {
107 output.push_str("unsafe ");
110 if abi != ::abi::Abi::Rust {
111 output.push_str("extern \"");
112 output.push_str(abi.name());
113 output.push_str("\" ");
116 output.push_str("fn(");
118 let sig = cx.tcx().erase_late_bound_regions_and_normalize(sig);
119 if !sig.inputs().is_empty() {
120 for ¶meter_type in sig.inputs() {
121 push_debuginfo_type_name(cx, parameter_type, true, output);
122 output.push_str(", ");
129 if !sig.inputs().is_empty() {
130 output.push_str(", ...");
132 output.push_str("...");
138 if !sig.output().is_nil() {
139 output.push_str(" -> ");
140 push_debuginfo_type_name(cx, sig.output(), true, output);
143 ty::TyClosure(..) => {
144 output.push_str("closure");
148 ty::TyProjection(..) |
151 bug!("debuginfo: Trying to create type name for \
152 unexpected type: {:?}", t);
156 fn push_item_name(cx: &CrateContext,
159 output: &mut String) {
161 output.push_str(&cx.tcx().crate_name(def_id.krate).as_str());
162 for path_element in cx.tcx().def_path(def_id).data {
163 output.push_str("::");
164 output.push_str(&path_element.data.as_interned_str());
167 output.push_str(&cx.tcx().item_name(def_id).as_str());
171 // Pushes the type parameters in the given `Substs` to the output string.
172 // This ignores region parameters, since they can't reliably be
173 // reconstructed for items from non-local crates. For local crates, this
174 // would be possible but with inlining and LTO we have to use the least
175 // common denominator - otherwise we would run into conflicts.
176 fn push_type_params<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
177 substs: &Substs<'tcx>,
178 output: &mut String) {
179 if substs.types().next().is_none() {
185 for type_parameter in substs.types() {
186 push_debuginfo_type_name(cx, type_parameter, true, output);
187 output.push_str(", ");