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
;
19 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(int_ty
) => output
.push_str(int_ty
.ty_to_string()),
47 ty
::TyUint(uint_ty
) => output
.push_str(uint_ty
.ty_to_string()),
48 ty
::TyFloat(float_ty
) => output
.push_str(float_ty
.ty_to_string()),
49 ty
::TyStruct(def
, substs
) |
50 ty
::TyEnum(def
, substs
) => {
51 push_item_name(cx
, def
.did
, qualified
, output
);
52 push_type_params(cx
, substs
, output
);
54 ty
::TyTuple(ref component_types
) => {
56 for &component_type
in component_types
{
57 push_debuginfo_type_name(cx
, component_type
, true, output
);
58 output
.push_str(", ");
60 if !component_types
.is_empty() {
66 ty
::TyBox(inner_type
) => {
67 output
.push_str("Box<");
68 push_debuginfo_type_name(cx
, inner_type
, true, output
);
71 ty
::TyRawPtr(ty
::TypeAndMut { ty: inner_type, mutbl }
) => {
74 hir
::MutImmutable
=> output
.push_str("const "),
75 hir
::MutMutable
=> output
.push_str("mut "),
78 push_debuginfo_type_name(cx
, inner_type
, true, output
);
80 ty
::TyRef(_
, ty
::TypeAndMut { ty: inner_type, mutbl }
) => {
82 if mutbl
== hir
::MutMutable
{
83 output
.push_str("mut ");
86 push_debuginfo_type_name(cx
, inner_type
, true, output
);
88 ty
::TyArray(inner_type
, len
) => {
90 push_debuginfo_type_name(cx
, inner_type
, true, output
);
91 output
.push_str(&format
!("; {}", len
));
94 ty
::TySlice(inner_type
) => {
96 push_debuginfo_type_name(cx
, inner_type
, true, output
);
99 ty
::TyTrait(ref trait_data
) => {
100 let principal
= cx
.tcx().erase_late_bound_regions(&trait_data
.principal
);
101 push_item_name(cx
, principal
.def_id
, false, output
);
102 push_type_params(cx
, principal
.substs
, output
);
104 ty
::TyBareFn(_
, &ty
::BareFnTy{ unsafety, abi, ref sig }
) => {
105 if unsafety
== hir
::Unsafety
::Unsafe
{
106 output
.push_str("unsafe ");
109 if abi
!= ::syntax
::abi
::Abi
::Rust
{
110 output
.push_str("extern \"");
111 output
.push_str(abi
.name());
112 output
.push_str("\" ");
115 output
.push_str("fn(");
117 let sig
= cx
.tcx().erase_late_bound_regions(sig
);
118 let sig
= infer
::normalize_associated_type(cx
.tcx(), &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("...");
139 ty
::FnConverging(result_type
) if result_type
.is_nil() => {}
140 ty
::FnConverging(result_type
) => {
141 output
.push_str(" -> ");
142 push_debuginfo_type_name(cx
, result_type
, true, output
);
145 output
.push_str(" -> !");
149 ty
::TyClosure(..) => {
150 output
.push_str("closure");
154 ty
::TyProjection(..) |
156 cx
.sess().bug(&format
!("debuginfo: Trying to create type name for \
157 unexpected type: {:?}", t
));
161 fn push_item_name(cx
: &CrateContext
,
164 output
: &mut String
) {
165 cx
.tcx().with_path(def_id
, |path
| {
167 if def_id
.is_local() {
168 output
.push_str(crate_root_namespace(cx
));
169 output
.push_str("::");
172 let mut path_element_count
= 0;
173 for path_element
in path
{
174 output
.push_str(&path_element
.name().as_str());
175 output
.push_str("::");
176 path_element_count
+= 1;
179 if path_element_count
== 0 {
180 cx
.sess().bug("debuginfo: Encountered empty item path!");
186 let name
= path
.last().expect("debuginfo: Empty item path?").name();
187 output
.push_str(&name
.as_str());
192 // Pushes the type parameters in the given `Substs` to the output string.
193 // This ignores region parameters, since they can't reliably be
194 // reconstructed for items from non-local crates. For local crates, this
195 // would be possible but with inlining and LTO we have to use the least
196 // common denominator - otherwise we would run into conflicts.
197 fn push_type_params
<'a
, 'tcx
>(cx
: &CrateContext
<'a
, 'tcx
>,
198 substs
: &subst
::Substs
<'tcx
>,
199 output
: &mut String
) {
200 if substs
.types
.is_empty() {
206 for &type_parameter
in &substs
.types
{
207 push_debuginfo_type_name(cx
, type_parameter
, true, output
);
208 output
.push_str(", ");