]> git.proxmox.com Git - rustc.git/blob - src/librustc_trans/trans/debuginfo/type_names.rs
Imported Upstream version 1.5.0+dfsg1
[rustc.git] / src / librustc_trans / trans / debuginfo / type_names.rs
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.
4 //
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.
10
11 // Type Names for Debug Info.
12
13 use super::namespace::crate_root_namespace;
14
15 use trans::common::CrateContext;
16 use middle::def_id::DefId;
17 use middle::subst::{self, Substs};
18 use middle::ty::{self, Ty};
19
20 use rustc_front::hir;
21 use syntax::ast;
22
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>,
28 t: Ty<'tcx>,
29 qualified: bool)
30 -> String {
31 let mut result = String::with_capacity(64);
32 push_debuginfo_type_name(cx, t, qualified, &mut result);
33 result
34 }
35
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>,
39 t: Ty<'tcx>,
40 qualified: bool,
41 output: &mut String) {
42 match t.sty {
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);
62 },
63 ty::TyTuple(ref component_types) => {
64 output.push('(');
65 for &component_type in component_types {
66 push_debuginfo_type_name(cx, component_type, true, output);
67 output.push_str(", ");
68 }
69 if !component_types.is_empty() {
70 output.pop();
71 output.pop();
72 }
73 output.push(')');
74 },
75 ty::TyBox(inner_type) => {
76 output.push_str("Box<");
77 push_debuginfo_type_name(cx, inner_type, true, output);
78 output.push('>');
79 },
80 ty::TyRawPtr(ty::TypeAndMut { ty: inner_type, mutbl } ) => {
81 output.push('*');
82 match mutbl {
83 hir::MutImmutable => output.push_str("const "),
84 hir::MutMutable => output.push_str("mut "),
85 }
86
87 push_debuginfo_type_name(cx, inner_type, true, output);
88 },
89 ty::TyRef(_, ty::TypeAndMut { ty: inner_type, mutbl }) => {
90 output.push('&');
91 if mutbl == hir::MutMutable {
92 output.push_str("mut ");
93 }
94
95 push_debuginfo_type_name(cx, inner_type, true, output);
96 },
97 ty::TyArray(inner_type, len) => {
98 output.push('[');
99 push_debuginfo_type_name(cx, inner_type, true, output);
100 output.push_str(&format!("; {}", len));
101 output.push(']');
102 },
103 ty::TySlice(inner_type) => {
104 output.push('[');
105 push_debuginfo_type_name(cx, inner_type, true, output);
106 output.push(']');
107 },
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);
112 },
113 ty::TyBareFn(_, &ty::BareFnTy{ unsafety, abi, ref sig } ) => {
114 if unsafety == hir::Unsafety::Unsafe {
115 output.push_str("unsafe ");
116 }
117
118 if abi != ::syntax::abi::Rust {
119 output.push_str("extern \"");
120 output.push_str(abi.name());
121 output.push_str("\" ");
122 }
123
124 output.push_str("fn(");
125
126 let sig = cx.tcx().erase_late_bound_regions(sig);
127 if !sig.inputs.is_empty() {
128 for &parameter_type in &sig.inputs {
129 push_debuginfo_type_name(cx, parameter_type, true, output);
130 output.push_str(", ");
131 }
132 output.pop();
133 output.pop();
134 }
135
136 if sig.variadic {
137 if !sig.inputs.is_empty() {
138 output.push_str(", ...");
139 } else {
140 output.push_str("...");
141 }
142 }
143
144 output.push(')');
145
146 match sig.output {
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);
151 }
152 ty::FnDiverging => {
153 output.push_str(" -> !");
154 }
155 }
156 },
157 ty::TyClosure(..) => {
158 output.push_str("closure");
159 }
160 ty::TyError |
161 ty::TyInfer(_) |
162 ty::TyProjection(..) |
163 ty::TyParam(_) => {
164 cx.sess().bug(&format!("debuginfo: Trying to create type name for \
165 unexpected type: {:?}", t));
166 }
167 }
168
169 fn push_item_name(cx: &CrateContext,
170 def_id: DefId,
171 qualified: bool,
172 output: &mut String) {
173 cx.tcx().with_path(def_id, |path| {
174 if qualified {
175 if def_id.is_local() {
176 output.push_str(crate_root_namespace(cx));
177 output.push_str("::");
178 }
179
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;
185 }
186
187 if path_element_count == 0 {
188 cx.sess().bug("debuginfo: Encountered empty item path!");
189 }
190
191 output.pop();
192 output.pop();
193 } else {
194 let name = path.last().expect("debuginfo: Empty item path?").name();
195 output.push_str(&name.as_str());
196 }
197 });
198 }
199
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() {
209 return;
210 }
211
212 output.push('<');
213
214 for &type_parameter in &substs.types {
215 push_debuginfo_type_name(cx, type_parameter, true, output);
216 output.push_str(", ");
217 }
218
219 output.pop();
220 output.pop();
221
222 output.push('>');
223 }
224 }