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 super::namespace
::crate_root_namespace
;
15 use trans
::common
::CrateContext
;
16 use middle
::def_id
::DefId
;
17 use middle
::subst
::{self, Substs}
;
18 use middle
::ty
::{self, Ty}
;
23 // Compute the name of the type as it should be stored in debuginfo. Does not do
24 // any caching, i.e. calling the function twice with the same type will also do
25 // the work twice. The `qualified` parameter only affects the first level of the
26 // type name, further levels (i.e. type parameters) are always fully qualified.
27 pub fn compute_debuginfo_type_name
<'a
, 'tcx
>(cx
: &CrateContext
<'a
, 'tcx
>,
31 let mut result
= String
::with_capacity(64);
32 push_debuginfo_type_name(cx
, t
, qualified
, &mut result
);
36 // Pushes the name of the type as it should be stored in debuginfo on the
37 // `output` String. See also compute_debuginfo_type_name().
38 pub fn push_debuginfo_type_name
<'a
, 'tcx
>(cx
: &CrateContext
<'a
, 'tcx
>,
41 output
: &mut String
) {
43 ty
::TyBool
=> output
.push_str("bool"),
44 ty
::TyChar
=> output
.push_str("char"),
45 ty
::TyStr
=> output
.push_str("str"),
46 ty
::TyInt(ast
::TyIs
) => output
.push_str("isize"),
47 ty
::TyInt(ast
::TyI8
) => output
.push_str("i8"),
48 ty
::TyInt(ast
::TyI16
) => output
.push_str("i16"),
49 ty
::TyInt(ast
::TyI32
) => output
.push_str("i32"),
50 ty
::TyInt(ast
::TyI64
) => output
.push_str("i64"),
51 ty
::TyUint(ast
::TyUs
) => output
.push_str("usize"),
52 ty
::TyUint(ast
::TyU8
) => output
.push_str("u8"),
53 ty
::TyUint(ast
::TyU16
) => output
.push_str("u16"),
54 ty
::TyUint(ast
::TyU32
) => output
.push_str("u32"),
55 ty
::TyUint(ast
::TyU64
) => output
.push_str("u64"),
56 ty
::TyFloat(ast
::TyF32
) => output
.push_str("f32"),
57 ty
::TyFloat(ast
::TyF64
) => output
.push_str("f64"),
58 ty
::TyStruct(def
, substs
) |
59 ty
::TyEnum(def
, substs
) => {
60 push_item_name(cx
, def
.did
, qualified
, output
);
61 push_type_params(cx
, substs
, output
);
63 ty
::TyTuple(ref component_types
) => {
65 for &component_type
in component_types
{
66 push_debuginfo_type_name(cx
, component_type
, true, output
);
67 output
.push_str(", ");
69 if !component_types
.is_empty() {
75 ty
::TyBox(inner_type
) => {
76 output
.push_str("Box<");
77 push_debuginfo_type_name(cx
, inner_type
, true, output
);
80 ty
::TyRawPtr(ty
::TypeAndMut { ty: inner_type, mutbl }
) => {
83 hir
::MutImmutable
=> output
.push_str("const "),
84 hir
::MutMutable
=> output
.push_str("mut "),
87 push_debuginfo_type_name(cx
, inner_type
, true, output
);
89 ty
::TyRef(_
, ty
::TypeAndMut { ty: inner_type, mutbl }
) => {
91 if mutbl
== hir
::MutMutable
{
92 output
.push_str("mut ");
95 push_debuginfo_type_name(cx
, inner_type
, true, output
);
97 ty
::TyArray(inner_type
, len
) => {
99 push_debuginfo_type_name(cx
, inner_type
, true, output
);
100 output
.push_str(&format
!("; {}", len
));
103 ty
::TySlice(inner_type
) => {
105 push_debuginfo_type_name(cx
, inner_type
, true, output
);
108 ty
::TyTrait(ref trait_data
) => {
109 let principal
= cx
.tcx().erase_late_bound_regions(&trait_data
.principal
);
110 push_item_name(cx
, principal
.def_id
, false, output
);
111 push_type_params(cx
, principal
.substs
, output
);
113 ty
::TyBareFn(_
, &ty
::BareFnTy{ unsafety, abi, ref sig }
) => {
114 if unsafety
== hir
::Unsafety
::Unsafe
{
115 output
.push_str("unsafe ");
118 if abi
!= ::syntax
::abi
::Rust
{
119 output
.push_str("extern \"");
120 output
.push_str(abi
.name());
121 output
.push_str("\" ");
124 output
.push_str("fn(");
126 let sig
= cx
.tcx().erase_late_bound_regions(sig
);
127 if !sig
.inputs
.is_empty() {
128 for ¶meter_type
in &sig
.inputs
{
129 push_debuginfo_type_name(cx
, parameter_type
, true, output
);
130 output
.push_str(", ");
137 if !sig
.inputs
.is_empty() {
138 output
.push_str(", ...");
140 output
.push_str("...");
147 ty
::FnConverging(result_type
) if result_type
.is_nil() => {}
148 ty
::FnConverging(result_type
) => {
149 output
.push_str(" -> ");
150 push_debuginfo_type_name(cx
, result_type
, true, output
);
153 output
.push_str(" -> !");
157 ty
::TyClosure(..) => {
158 output
.push_str("closure");
162 ty
::TyProjection(..) |
164 cx
.sess().bug(&format
!("debuginfo: Trying to create type name for \
165 unexpected type: {:?}", t
));
169 fn push_item_name(cx
: &CrateContext
,
172 output
: &mut String
) {
173 cx
.tcx().with_path(def_id
, |path
| {
175 if def_id
.is_local() {
176 output
.push_str(crate_root_namespace(cx
));
177 output
.push_str("::");
180 let mut path_element_count
= 0;
181 for path_element
in path
{
182 output
.push_str(&path_element
.name().as_str());
183 output
.push_str("::");
184 path_element_count
+= 1;
187 if path_element_count
== 0 {
188 cx
.sess().bug("debuginfo: Encountered empty item path!");
194 let name
= path
.last().expect("debuginfo: Empty item path?").name();
195 output
.push_str(&name
.as_str());
200 // Pushes the type parameters in the given `Substs` to the output string.
201 // This ignores region parameters, since they can't reliably be
202 // reconstructed for items from non-local crates. For local crates, this
203 // would be possible but with inlining and LTO we have to use the least
204 // common denominator - otherwise we would run into conflicts.
205 fn push_type_params
<'a
, 'tcx
>(cx
: &CrateContext
<'a
, 'tcx
>,
206 substs
: &subst
::Substs
<'tcx
>,
207 output
: &mut String
) {
208 if substs
.types
.is_empty() {
214 for &type_parameter
in &substs
.types
{
215 push_debuginfo_type_name(cx
, type_parameter
, true, output
);
216 output
.push_str(", ");