]>
Commit | Line | Data |
---|---|---|
d9579d0f | 1 | // Copyright 2015 The Rust Project Developers. See the COPYRIGHT |
1a4d82fc JJ |
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 | ||
d9579d0f | 11 | use self::RecursiveTypeDescription::*; |
1a4d82fc JJ |
12 | use self::MemberOffset::*; |
13 | use self::MemberDescriptionFactory::*; | |
1a4d82fc | 14 | use self::EnumDiscriminantInfo::*; |
1a4d82fc | 15 | |
d9579d0f AL |
16 | use super::utils::{debug_context, DIB, span_start, bytes_to_bits, size_and_align_of, |
17 | get_namespace_and_span_for_item, create_DIArray, | |
18 | fn_should_be_ignored, is_node_local_to_unit}; | |
19 | use super::namespace::namespace_for_item; | |
20 | use super::type_names::{compute_debuginfo_type_name, push_debuginfo_type_name}; | |
21 | use super::{declare_local, VariableKind, VariableAccess}; | |
22 | ||
23 | use llvm::{self, ValueRef}; | |
24 | use llvm::debuginfo::{DIType, DIFile, DIScope, DIDescriptor, DICompositeType}; | |
25 | ||
e9174d1e | 26 | use middle::def_id::DefId; |
92a42be0 | 27 | use middle::infer; |
d9579d0f | 28 | use middle::pat_util; |
7453a54e | 29 | use middle::subst; |
e9174d1e | 30 | use rustc::front::map as hir_map; |
7453a54e | 31 | use rustc_front::hir::{self, PatKind}; |
d9579d0f | 32 | use trans::{type_of, adt, machine, monomorphize}; |
c1a9b12d SL |
33 | use trans::common::{self, CrateContext, FunctionContext, Block}; |
34 | use trans::_match::{BindingInfo, TransBindingMode}; | |
1a4d82fc | 35 | use trans::type_::Type; |
c1a9b12d | 36 | use middle::ty::{self, Ty}; |
d9579d0f AL |
37 | use session::config::{self, FullDebugInfo}; |
38 | use util::nodemap::FnvHashMap; | |
c34b1796 | 39 | use util::common::path2cstr; |
1a4d82fc | 40 | |
85aaf69f | 41 | use libc::{c_uint, c_longlong}; |
c34b1796 AL |
42 | use std::ffi::CString; |
43 | use std::path::Path; | |
1a4d82fc | 44 | use std::ptr; |
d9579d0f | 45 | use std::rc::Rc; |
b039eaaf | 46 | use syntax; |
1a4d82fc | 47 | use syntax::util::interner::Interner; |
d9579d0f | 48 | use syntax::codemap::Span; |
9cc50fc6 | 49 | use syntax::{ast, codemap}; |
e9174d1e | 50 | use syntax::parse::token; |
1a4d82fc | 51 | |
1a4d82fc | 52 | |
d9579d0f | 53 | const DW_LANG_RUST: c_uint = 0x9000; |
1a4d82fc JJ |
54 | #[allow(non_upper_case_globals)] |
55 | const DW_ATE_boolean: c_uint = 0x02; | |
56 | #[allow(non_upper_case_globals)] | |
57 | const DW_ATE_float: c_uint = 0x04; | |
58 | #[allow(non_upper_case_globals)] | |
59 | const DW_ATE_signed: c_uint = 0x05; | |
60 | #[allow(non_upper_case_globals)] | |
61 | const DW_ATE_unsigned: c_uint = 0x07; | |
62 | #[allow(non_upper_case_globals)] | |
63 | const DW_ATE_unsigned_char: c_uint = 0x08; | |
64 | ||
d9579d0f AL |
65 | pub const UNKNOWN_LINE_NUMBER: c_uint = 0; |
66 | pub const UNKNOWN_COLUMN_NUMBER: c_uint = 0; | |
1a4d82fc JJ |
67 | |
68 | // ptr::null() doesn't work :( | |
e9174d1e SL |
69 | const NO_FILE_METADATA: DIFile = (0 as DIFile); |
70 | const NO_SCOPE_METADATA: DIScope = (0 as DIScope); | |
1a4d82fc JJ |
71 | |
72 | const FLAGS_NONE: c_uint = 0; | |
73 | ||
85aaf69f | 74 | #[derive(Copy, Debug, Hash, Eq, PartialEq, Clone)] |
d9579d0f | 75 | pub struct UniqueTypeId(ast::Name); |
1a4d82fc JJ |
76 | |
77 | // The TypeMap is where the CrateDebugContext holds the type metadata nodes | |
78 | // created so far. The metadata nodes are indexed by UniqueTypeId, and, for | |
79 | // faster lookup, also by Ty. The TypeMap is responsible for creating | |
80 | // UniqueTypeIds. | |
d9579d0f | 81 | pub struct TypeMap<'tcx> { |
1a4d82fc JJ |
82 | // The UniqueTypeIds created so far |
83 | unique_id_interner: Interner<Rc<String>>, | |
84 | // A map from UniqueTypeId to debuginfo metadata for that type. This is a 1:1 mapping. | |
85 | unique_id_to_metadata: FnvHashMap<UniqueTypeId, DIType>, | |
86 | // A map from types to debuginfo metadata. This is a N:1 mapping. | |
87 | type_to_metadata: FnvHashMap<Ty<'tcx>, DIType>, | |
88 | // A map from types to UniqueTypeId. This is a N:1 mapping. | |
89 | type_to_unique_id: FnvHashMap<Ty<'tcx>, UniqueTypeId> | |
90 | } | |
91 | ||
92 | impl<'tcx> TypeMap<'tcx> { | |
d9579d0f | 93 | pub fn new() -> TypeMap<'tcx> { |
1a4d82fc JJ |
94 | TypeMap { |
95 | unique_id_interner: Interner::new(), | |
85aaf69f SL |
96 | type_to_metadata: FnvHashMap(), |
97 | unique_id_to_metadata: FnvHashMap(), | |
98 | type_to_unique_id: FnvHashMap(), | |
1a4d82fc JJ |
99 | } |
100 | } | |
101 | ||
102 | // Adds a Ty to metadata mapping to the TypeMap. The method will fail if | |
103 | // the mapping already exists. | |
104 | fn register_type_with_metadata<'a>(&mut self, | |
105 | cx: &CrateContext<'a, 'tcx>, | |
106 | type_: Ty<'tcx>, | |
107 | metadata: DIType) { | |
108 | if self.type_to_metadata.insert(type_, metadata).is_some() { | |
109 | cx.sess().bug(&format!("Type metadata for Ty '{}' is already in the TypeMap!", | |
62682a34 | 110 | type_)); |
1a4d82fc JJ |
111 | } |
112 | } | |
113 | ||
114 | // Adds a UniqueTypeId to metadata mapping to the TypeMap. The method will | |
115 | // fail if the mapping already exists. | |
116 | fn register_unique_id_with_metadata(&mut self, | |
117 | cx: &CrateContext, | |
118 | unique_type_id: UniqueTypeId, | |
119 | metadata: DIType) { | |
120 | if self.unique_id_to_metadata.insert(unique_type_id, metadata).is_some() { | |
121 | let unique_type_id_str = self.get_unique_type_id_as_string(unique_type_id); | |
122 | cx.sess().bug(&format!("Type metadata for unique id '{}' is already in the TypeMap!", | |
c34b1796 | 123 | &unique_type_id_str[..])); |
1a4d82fc JJ |
124 | } |
125 | } | |
126 | ||
127 | fn find_metadata_for_type(&self, type_: Ty<'tcx>) -> Option<DIType> { | |
128 | self.type_to_metadata.get(&type_).cloned() | |
129 | } | |
130 | ||
131 | fn find_metadata_for_unique_id(&self, unique_type_id: UniqueTypeId) -> Option<DIType> { | |
132 | self.unique_id_to_metadata.get(&unique_type_id).cloned() | |
133 | } | |
134 | ||
135 | // Get the string representation of a UniqueTypeId. This method will fail if | |
136 | // the id is unknown. | |
137 | fn get_unique_type_id_as_string(&self, unique_type_id: UniqueTypeId) -> Rc<String> { | |
138 | let UniqueTypeId(interner_key) = unique_type_id; | |
139 | self.unique_id_interner.get(interner_key) | |
140 | } | |
141 | ||
142 | // Get the UniqueTypeId for the given type. If the UniqueTypeId for the given | |
143 | // type has been requested before, this is just a table lookup. Otherwise an | |
144 | // ID will be generated and stored for later lookup. | |
145 | fn get_unique_type_id_of_type<'a>(&mut self, cx: &CrateContext<'a, 'tcx>, | |
146 | type_: Ty<'tcx>) -> UniqueTypeId { | |
147 | ||
d9579d0f AL |
148 | // basic type -> {:name of the type:} |
149 | // tuple -> {tuple_(:param-uid:)*} | |
150 | // struct -> {struct_:svh: / :node-id:_<(:param-uid:),*> } | |
151 | // enum -> {enum_:svh: / :node-id:_<(:param-uid:),*> } | |
152 | // enum variant -> {variant_:variant-name:_:enum-uid:} | |
153 | // reference (&) -> {& :pointee-uid:} | |
154 | // mut reference (&mut) -> {&mut :pointee-uid:} | |
155 | // ptr (*) -> {* :pointee-uid:} | |
156 | // mut ptr (*mut) -> {*mut :pointee-uid:} | |
157 | // unique ptr (box) -> {box :pointee-uid:} | |
158 | // @-ptr (@) -> {@ :pointee-uid:} | |
159 | // sized vec ([T; x]) -> {[:size:] :element-uid:} | |
160 | // unsized vec ([T]) -> {[] :element-uid:} | |
161 | // trait (T) -> {trait_:svh: / :node-id:_<(:param-uid:),*> } | |
162 | // closure -> {<unsafe_> <once_> :store-sigil: |(:param-uid:),* <,_...>| -> \ | |
1a4d82fc | 163 | // :return-type-uid: : (:bounds:)*} |
d9579d0f | 164 | // function -> {<unsafe_> <abi_> fn( (:param-uid:)* <,_...> ) -> \ |
1a4d82fc | 165 | // :return-type-uid:} |
1a4d82fc JJ |
166 | |
167 | match self.type_to_unique_id.get(&type_).cloned() { | |
168 | Some(unique_type_id) => return unique_type_id, | |
169 | None => { /* generate one */} | |
170 | }; | |
171 | ||
172 | let mut unique_type_id = String::with_capacity(256); | |
173 | unique_type_id.push('{'); | |
174 | ||
175 | match type_.sty { | |
62682a34 SL |
176 | ty::TyBool | |
177 | ty::TyChar | | |
178 | ty::TyStr | | |
179 | ty::TyInt(_) | | |
180 | ty::TyUint(_) | | |
181 | ty::TyFloat(_) => { | |
1a4d82fc JJ |
182 | push_debuginfo_type_name(cx, type_, false, &mut unique_type_id); |
183 | }, | |
e9174d1e | 184 | ty::TyEnum(def, substs) => { |
1a4d82fc | 185 | unique_type_id.push_str("enum "); |
e9174d1e | 186 | from_def_id_and_substs(self, cx, def.did, substs, &mut unique_type_id); |
1a4d82fc | 187 | }, |
e9174d1e | 188 | ty::TyStruct(def, substs) => { |
1a4d82fc | 189 | unique_type_id.push_str("struct "); |
e9174d1e | 190 | from_def_id_and_substs(self, cx, def.did, substs, &mut unique_type_id); |
1a4d82fc | 191 | }, |
62682a34 | 192 | ty::TyTuple(ref component_types) if component_types.is_empty() => { |
1a4d82fc JJ |
193 | push_debuginfo_type_name(cx, type_, false, &mut unique_type_id); |
194 | }, | |
62682a34 | 195 | ty::TyTuple(ref component_types) => { |
1a4d82fc | 196 | unique_type_id.push_str("tuple "); |
85aaf69f | 197 | for &component_type in component_types { |
1a4d82fc JJ |
198 | let component_type_id = |
199 | self.get_unique_type_id_of_type(cx, component_type); | |
200 | let component_type_id = | |
201 | self.get_unique_type_id_as_string(component_type_id); | |
85aaf69f | 202 | unique_type_id.push_str(&component_type_id[..]); |
1a4d82fc JJ |
203 | } |
204 | }, | |
62682a34 | 205 | ty::TyBox(inner_type) => { |
d9579d0f | 206 | unique_type_id.push_str("box "); |
1a4d82fc JJ |
207 | let inner_type_id = self.get_unique_type_id_of_type(cx, inner_type); |
208 | let inner_type_id = self.get_unique_type_id_as_string(inner_type_id); | |
85aaf69f | 209 | unique_type_id.push_str(&inner_type_id[..]); |
1a4d82fc | 210 | }, |
c1a9b12d | 211 | ty::TyRawPtr(ty::TypeAndMut { ty: inner_type, mutbl } ) => { |
1a4d82fc | 212 | unique_type_id.push('*'); |
e9174d1e | 213 | if mutbl == hir::MutMutable { |
1a4d82fc JJ |
214 | unique_type_id.push_str("mut"); |
215 | } | |
216 | ||
217 | let inner_type_id = self.get_unique_type_id_of_type(cx, inner_type); | |
218 | let inner_type_id = self.get_unique_type_id_as_string(inner_type_id); | |
85aaf69f | 219 | unique_type_id.push_str(&inner_type_id[..]); |
1a4d82fc | 220 | }, |
c1a9b12d | 221 | ty::TyRef(_, ty::TypeAndMut { ty: inner_type, mutbl }) => { |
1a4d82fc | 222 | unique_type_id.push('&'); |
e9174d1e | 223 | if mutbl == hir::MutMutable { |
1a4d82fc JJ |
224 | unique_type_id.push_str("mut"); |
225 | } | |
226 | ||
227 | let inner_type_id = self.get_unique_type_id_of_type(cx, inner_type); | |
228 | let inner_type_id = self.get_unique_type_id_as_string(inner_type_id); | |
85aaf69f | 229 | unique_type_id.push_str(&inner_type_id[..]); |
1a4d82fc | 230 | }, |
62682a34 SL |
231 | ty::TyArray(inner_type, len) => { |
232 | unique_type_id.push_str(&format!("[{}]", len)); | |
233 | ||
234 | let inner_type_id = self.get_unique_type_id_of_type(cx, inner_type); | |
235 | let inner_type_id = self.get_unique_type_id_as_string(inner_type_id); | |
236 | unique_type_id.push_str(&inner_type_id[..]); | |
237 | }, | |
238 | ty::TySlice(inner_type) => { | |
239 | unique_type_id.push_str("[]"); | |
1a4d82fc JJ |
240 | |
241 | let inner_type_id = self.get_unique_type_id_of_type(cx, inner_type); | |
242 | let inner_type_id = self.get_unique_type_id_as_string(inner_type_id); | |
85aaf69f | 243 | unique_type_id.push_str(&inner_type_id[..]); |
1a4d82fc | 244 | }, |
62682a34 | 245 | ty::TyTrait(ref trait_data) => { |
1a4d82fc JJ |
246 | unique_type_id.push_str("trait "); |
247 | ||
c1a9b12d | 248 | let principal = cx.tcx().erase_late_bound_regions(&trait_data.principal); |
1a4d82fc JJ |
249 | |
250 | from_def_id_and_substs(self, | |
251 | cx, | |
252 | principal.def_id, | |
253 | principal.substs, | |
254 | &mut unique_type_id); | |
255 | }, | |
62682a34 | 256 | ty::TyBareFn(_, &ty::BareFnTy{ unsafety, abi, ref sig } ) => { |
e9174d1e | 257 | if unsafety == hir::Unsafety::Unsafe { |
1a4d82fc JJ |
258 | unique_type_id.push_str("unsafe "); |
259 | } | |
260 | ||
261 | unique_type_id.push_str(abi.name()); | |
262 | ||
263 | unique_type_id.push_str(" fn("); | |
264 | ||
c1a9b12d | 265 | let sig = cx.tcx().erase_late_bound_regions(sig); |
92a42be0 | 266 | let sig = infer::normalize_associated_type(cx.tcx(), &sig); |
1a4d82fc | 267 | |
85aaf69f | 268 | for ¶meter_type in &sig.inputs { |
1a4d82fc JJ |
269 | let parameter_type_id = |
270 | self.get_unique_type_id_of_type(cx, parameter_type); | |
271 | let parameter_type_id = | |
272 | self.get_unique_type_id_as_string(parameter_type_id); | |
85aaf69f | 273 | unique_type_id.push_str(¶meter_type_id[..]); |
1a4d82fc JJ |
274 | unique_type_id.push(','); |
275 | } | |
276 | ||
277 | if sig.variadic { | |
278 | unique_type_id.push_str("..."); | |
279 | } | |
280 | ||
281 | unique_type_id.push_str(")->"); | |
282 | match sig.output { | |
283 | ty::FnConverging(ret_ty) => { | |
284 | let return_type_id = self.get_unique_type_id_of_type(cx, ret_ty); | |
285 | let return_type_id = self.get_unique_type_id_as_string(return_type_id); | |
85aaf69f | 286 | unique_type_id.push_str(&return_type_id[..]); |
1a4d82fc JJ |
287 | } |
288 | ty::FnDiverging => { | |
289 | unique_type_id.push_str("!"); | |
290 | } | |
291 | } | |
292 | }, | |
c1a9b12d SL |
293 | ty::TyClosure(_, ref substs) if substs.upvar_tys.is_empty() => { |
294 | push_debuginfo_type_name(cx, type_, false, &mut unique_type_id); | |
295 | }, | |
296 | ty::TyClosure(_, ref substs) => { | |
297 | unique_type_id.push_str("closure "); | |
298 | for upvar_type in &substs.upvar_tys { | |
299 | let upvar_type_id = | |
300 | self.get_unique_type_id_of_type(cx, upvar_type); | |
301 | let upvar_type_id = | |
302 | self.get_unique_type_id_as_string(upvar_type_id); | |
303 | unique_type_id.push_str(&upvar_type_id[..]); | |
304 | } | |
1a4d82fc JJ |
305 | }, |
306 | _ => { | |
62682a34 SL |
307 | cx.sess().bug(&format!("get_unique_type_id_of_type() - unexpected type: {:?}", |
308 | type_)) | |
1a4d82fc JJ |
309 | } |
310 | }; | |
311 | ||
312 | unique_type_id.push('}'); | |
313 | ||
314 | // Trim to size before storing permanently | |
315 | unique_type_id.shrink_to_fit(); | |
316 | ||
317 | let key = self.unique_id_interner.intern(Rc::new(unique_type_id)); | |
318 | self.type_to_unique_id.insert(type_, UniqueTypeId(key)); | |
319 | ||
320 | return UniqueTypeId(key); | |
321 | ||
322 | fn from_def_id_and_substs<'a, 'tcx>(type_map: &mut TypeMap<'tcx>, | |
323 | cx: &CrateContext<'a, 'tcx>, | |
e9174d1e | 324 | def_id: DefId, |
1a4d82fc JJ |
325 | substs: &subst::Substs<'tcx>, |
326 | output: &mut String) { | |
327 | // First, find out the 'real' def_id of the type. Items inlined from | |
328 | // other crates have to be mapped back to their source. | |
b039eaaf SL |
329 | let source_def_id = if let Some(node_id) = cx.tcx().map.as_local_node_id(def_id) { |
330 | match cx.external_srcs().borrow().get(&node_id).cloned() { | |
1a4d82fc JJ |
331 | Some(source_def_id) => { |
332 | // The given def_id identifies the inlined copy of a | |
333 | // type definition, let's take the source of the copy. | |
334 | source_def_id | |
335 | } | |
336 | None => def_id | |
337 | } | |
338 | } else { | |
339 | def_id | |
340 | }; | |
341 | ||
342 | // Get the crate hash as first part of the identifier. | |
e9174d1e | 343 | let crate_hash = if source_def_id.is_local() { |
1a4d82fc JJ |
344 | cx.link_meta().crate_hash.clone() |
345 | } else { | |
92a42be0 | 346 | cx.sess().cstore.crate_hash(source_def_id.krate) |
1a4d82fc JJ |
347 | }; |
348 | ||
349 | output.push_str(crate_hash.as_str()); | |
350 | output.push_str("/"); | |
b039eaaf | 351 | output.push_str(&format!("{:x}", def_id.index.as_usize())); |
1a4d82fc JJ |
352 | |
353 | // Maybe check that there is no self type here. | |
354 | ||
355 | let tps = substs.types.get_slice(subst::TypeSpace); | |
9346a6ac | 356 | if !tps.is_empty() { |
1a4d82fc JJ |
357 | output.push('<'); |
358 | ||
85aaf69f | 359 | for &type_parameter in tps { |
1a4d82fc JJ |
360 | let param_type_id = |
361 | type_map.get_unique_type_id_of_type(cx, type_parameter); | |
362 | let param_type_id = | |
363 | type_map.get_unique_type_id_as_string(param_type_id); | |
85aaf69f | 364 | output.push_str(¶m_type_id[..]); |
1a4d82fc JJ |
365 | output.push(','); |
366 | } | |
367 | ||
368 | output.push('>'); | |
369 | } | |
370 | } | |
371 | } | |
372 | ||
1a4d82fc JJ |
373 | // Get the UniqueTypeId for an enum variant. Enum variants are not really |
374 | // types of their own, so they need special handling. We still need a | |
375 | // UniqueTypeId for them, since to debuginfo they *are* real types. | |
376 | fn get_unique_type_id_of_enum_variant<'a>(&mut self, | |
377 | cx: &CrateContext<'a, 'tcx>, | |
378 | enum_type: Ty<'tcx>, | |
379 | variant_name: &str) | |
380 | -> UniqueTypeId { | |
381 | let enum_type_id = self.get_unique_type_id_of_type(cx, enum_type); | |
382 | let enum_variant_type_id = format!("{}::{}", | |
c34b1796 | 383 | &self.get_unique_type_id_as_string(enum_type_id), |
1a4d82fc JJ |
384 | variant_name); |
385 | let interner_key = self.unique_id_interner.intern(Rc::new(enum_variant_type_id)); | |
386 | UniqueTypeId(interner_key) | |
387 | } | |
388 | } | |
389 | ||
d9579d0f AL |
390 | // A description of some recursive type. It can either be already finished (as |
391 | // with FinalMetadata) or it is not yet finished, but contains all information | |
392 | // needed to generate the missing parts of the description. See the | |
393 | // documentation section on Recursive Types at the top of this file for more | |
394 | // information. | |
395 | enum RecursiveTypeDescription<'tcx> { | |
396 | UnfinishedMetadata { | |
397 | unfinished_type: Ty<'tcx>, | |
398 | unique_type_id: UniqueTypeId, | |
399 | metadata_stub: DICompositeType, | |
400 | llvm_type: Type, | |
401 | member_description_factory: MemberDescriptionFactory<'tcx>, | |
402 | }, | |
403 | FinalMetadata(DICompositeType) | |
404 | } | |
405 | ||
406 | fn create_and_register_recursive_type_forward_declaration<'a, 'tcx>( | |
407 | cx: &CrateContext<'a, 'tcx>, | |
408 | unfinished_type: Ty<'tcx>, | |
409 | unique_type_id: UniqueTypeId, | |
410 | metadata_stub: DICompositeType, | |
411 | llvm_type: Type, | |
412 | member_description_factory: MemberDescriptionFactory<'tcx>) | |
413 | -> RecursiveTypeDescription<'tcx> { | |
414 | ||
415 | // Insert the stub into the TypeMap in order to allow for recursive references | |
416 | let mut type_map = debug_context(cx).type_map.borrow_mut(); | |
417 | type_map.register_unique_id_with_metadata(cx, unique_type_id, metadata_stub); | |
418 | type_map.register_type_with_metadata(cx, unfinished_type, metadata_stub); | |
419 | ||
420 | UnfinishedMetadata { | |
421 | unfinished_type: unfinished_type, | |
422 | unique_type_id: unique_type_id, | |
423 | metadata_stub: metadata_stub, | |
424 | llvm_type: llvm_type, | |
425 | member_description_factory: member_description_factory, | |
426 | } | |
427 | } | |
428 | ||
429 | impl<'tcx> RecursiveTypeDescription<'tcx> { | |
430 | // Finishes up the description of the type in question (mostly by providing | |
431 | // descriptions of the fields of the given type) and returns the final type | |
432 | // metadata. | |
433 | fn finalize<'a>(&self, cx: &CrateContext<'a, 'tcx>) -> MetadataCreationResult { | |
434 | match *self { | |
435 | FinalMetadata(metadata) => MetadataCreationResult::new(metadata, false), | |
436 | UnfinishedMetadata { | |
437 | unfinished_type, | |
438 | unique_type_id, | |
439 | metadata_stub, | |
440 | llvm_type, | |
441 | ref member_description_factory, | |
442 | .. | |
443 | } => { | |
444 | // Make sure that we have a forward declaration of the type in | |
445 | // the TypeMap so that recursive references are possible. This | |
446 | // will always be the case if the RecursiveTypeDescription has | |
447 | // been properly created through the | |
448 | // create_and_register_recursive_type_forward_declaration() | |
449 | // function. | |
450 | { | |
451 | let type_map = debug_context(cx).type_map.borrow(); | |
452 | if type_map.find_metadata_for_unique_id(unique_type_id).is_none() || | |
453 | type_map.find_metadata_for_type(unfinished_type).is_none() { | |
454 | cx.sess().bug(&format!("Forward declaration of potentially recursive type \ | |
62682a34 SL |
455 | '{:?}' was not found in TypeMap!", |
456 | unfinished_type) | |
d9579d0f AL |
457 | ); |
458 | } | |
459 | } | |
460 | ||
461 | // ... then create the member descriptions ... | |
462 | let member_descriptions = | |
463 | member_description_factory.create_member_descriptions(cx); | |
464 | ||
465 | // ... and attach them to the stub to complete it. | |
466 | set_members_of_composite_type(cx, | |
467 | metadata_stub, | |
468 | llvm_type, | |
469 | &member_descriptions[..]); | |
470 | return MetadataCreationResult::new(metadata_stub, true); | |
471 | } | |
472 | } | |
473 | } | |
474 | } | |
475 | ||
1a4d82fc JJ |
476 | // Returns from the enclosing function if the type metadata with the given |
477 | // unique id can be found in the type map | |
478 | macro_rules! return_if_metadata_created_in_meantime { | |
479 | ($cx: expr, $unique_type_id: expr) => ( | |
480 | match debug_context($cx).type_map | |
481 | .borrow() | |
482 | .find_metadata_for_unique_id($unique_type_id) { | |
483 | Some(metadata) => return MetadataCreationResult::new(metadata, true), | |
484 | None => { /* proceed normally */ } | |
b039eaaf | 485 | } |
1a4d82fc JJ |
486 | ) |
487 | } | |
488 | ||
d9579d0f AL |
489 | fn fixed_vec_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, |
490 | unique_type_id: UniqueTypeId, | |
491 | element_type: Ty<'tcx>, | |
492 | len: Option<u64>, | |
493 | span: Span) | |
494 | -> MetadataCreationResult { | |
495 | let element_type_metadata = type_metadata(cx, element_type, span); | |
496 | ||
497 | return_if_metadata_created_in_meantime!(cx, unique_type_id); | |
1a4d82fc | 498 | |
d9579d0f AL |
499 | let element_llvm_type = type_of::type_of(cx, element_type); |
500 | let (element_type_size, element_type_align) = size_and_align_of(cx, element_llvm_type); | |
1a4d82fc | 501 | |
d9579d0f AL |
502 | let (array_size_in_bytes, upper_bound) = match len { |
503 | Some(len) => (element_type_size * len, len as c_longlong), | |
504 | None => (0, -1) | |
505 | }; | |
1a4d82fc | 506 | |
d9579d0f AL |
507 | let subrange = unsafe { |
508 | llvm::LLVMDIBuilderGetOrCreateSubrange(DIB(cx), 0, upper_bound) | |
509 | }; | |
1a4d82fc | 510 | |
d9579d0f AL |
511 | let subscripts = create_DIArray(DIB(cx), &[subrange]); |
512 | let metadata = unsafe { | |
513 | llvm::LLVMDIBuilderCreateArrayType( | |
514 | DIB(cx), | |
515 | bytes_to_bits(array_size_in_bytes), | |
516 | bytes_to_bits(element_type_align), | |
517 | element_type_metadata, | |
518 | subscripts) | |
519 | }; | |
1a4d82fc | 520 | |
d9579d0f | 521 | return MetadataCreationResult::new(metadata, false); |
1a4d82fc JJ |
522 | } |
523 | ||
d9579d0f AL |
524 | fn vec_slice_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, |
525 | vec_type: Ty<'tcx>, | |
526 | element_type: Ty<'tcx>, | |
527 | unique_type_id: UniqueTypeId, | |
528 | span: Span) | |
529 | -> MetadataCreationResult { | |
c1a9b12d | 530 | let data_ptr_type = cx.tcx().mk_ptr(ty::TypeAndMut { |
d9579d0f | 531 | ty: element_type, |
e9174d1e | 532 | mutbl: hir::MutImmutable |
d9579d0f | 533 | }); |
1a4d82fc | 534 | |
d9579d0f | 535 | let element_type_metadata = type_metadata(cx, data_ptr_type, span); |
1a4d82fc | 536 | |
d9579d0f | 537 | return_if_metadata_created_in_meantime!(cx, unique_type_id); |
1a4d82fc | 538 | |
d9579d0f AL |
539 | let slice_llvm_type = type_of::type_of(cx, vec_type); |
540 | let slice_type_name = compute_debuginfo_type_name(cx, vec_type, true); | |
1a4d82fc | 541 | |
d9579d0f AL |
542 | let member_llvm_types = slice_llvm_type.field_types(); |
543 | assert!(slice_layout_is_correct(cx, | |
544 | &member_llvm_types[..], | |
545 | element_type)); | |
546 | let member_descriptions = [ | |
547 | MemberDescription { | |
548 | name: "data_ptr".to_string(), | |
549 | llvm_type: member_llvm_types[0], | |
550 | type_metadata: element_type_metadata, | |
551 | offset: ComputedMemberOffset, | |
552 | flags: FLAGS_NONE | |
553 | }, | |
554 | MemberDescription { | |
555 | name: "length".to_string(), | |
556 | llvm_type: member_llvm_types[1], | |
557 | type_metadata: type_metadata(cx, cx.tcx().types.usize, span), | |
558 | offset: ComputedMemberOffset, | |
559 | flags: FLAGS_NONE | |
560 | }, | |
561 | ]; | |
1a4d82fc | 562 | |
d9579d0f | 563 | assert!(member_descriptions.len() == member_llvm_types.len()); |
1a4d82fc | 564 | |
d9579d0f AL |
565 | let loc = span_start(cx, span); |
566 | let file_metadata = file_metadata(cx, &loc.file.name); | |
1a4d82fc | 567 | |
d9579d0f AL |
568 | let metadata = composite_type_metadata(cx, |
569 | slice_llvm_type, | |
570 | &slice_type_name[..], | |
571 | unique_type_id, | |
572 | &member_descriptions, | |
e9174d1e | 573 | NO_SCOPE_METADATA, |
d9579d0f AL |
574 | file_metadata, |
575 | span); | |
576 | return MetadataCreationResult::new(metadata, false); | |
1a4d82fc | 577 | |
d9579d0f AL |
578 | fn slice_layout_is_correct<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, |
579 | member_llvm_types: &[Type], | |
580 | element_type: Ty<'tcx>) | |
581 | -> bool { | |
582 | member_llvm_types.len() == 2 && | |
583 | member_llvm_types[0] == type_of::type_of(cx, element_type).ptr_to() && | |
584 | member_llvm_types[1] == cx.int_type() | |
1a4d82fc | 585 | } |
d9579d0f | 586 | } |
1a4d82fc | 587 | |
d9579d0f AL |
588 | fn subroutine_type_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, |
589 | unique_type_id: UniqueTypeId, | |
590 | signature: &ty::PolyFnSig<'tcx>, | |
591 | span: Span) | |
592 | -> MetadataCreationResult | |
593 | { | |
c1a9b12d | 594 | let signature = cx.tcx().erase_late_bound_regions(signature); |
1a4d82fc | 595 | |
d9579d0f | 596 | let mut signature_metadata: Vec<DIType> = Vec::with_capacity(signature.inputs.len() + 1); |
1a4d82fc | 597 | |
d9579d0f AL |
598 | // return type |
599 | signature_metadata.push(match signature.output { | |
600 | ty::FnConverging(ret_ty) => match ret_ty.sty { | |
62682a34 | 601 | ty::TyTuple(ref tys) if tys.is_empty() => ptr::null_mut(), |
d9579d0f AL |
602 | _ => type_metadata(cx, ret_ty, span) |
603 | }, | |
604 | ty::FnDiverging => diverging_type_metadata(cx) | |
605 | }); | |
1a4d82fc | 606 | |
d9579d0f AL |
607 | // regular arguments |
608 | for &argument_type in &signature.inputs { | |
609 | signature_metadata.push(type_metadata(cx, argument_type, span)); | |
1a4d82fc JJ |
610 | } |
611 | ||
d9579d0f | 612 | return_if_metadata_created_in_meantime!(cx, unique_type_id); |
1a4d82fc | 613 | |
d9579d0f AL |
614 | return MetadataCreationResult::new( |
615 | unsafe { | |
616 | llvm::LLVMDIBuilderCreateSubroutineType( | |
617 | DIB(cx), | |
e9174d1e | 618 | NO_FILE_METADATA, |
d9579d0f | 619 | create_DIArray(DIB(cx), &signature_metadata[..])) |
1a4d82fc JJ |
620 | }, |
621 | false); | |
622 | } | |
623 | ||
624 | // FIXME(1563) This is all a bit of a hack because 'trait pointer' is an ill- | |
625 | // defined concept. For the case of an actual trait pointer (i.e., Box<Trait>, | |
626 | // &Trait), trait_object_type should be the whole thing (e.g, Box<Trait>) and | |
627 | // trait_type should be the actual trait (e.g., Trait). Where the trait is part | |
628 | // of a DST struct, there is no trait_object_type and the results of this | |
629 | // function will be a little bit weird. | |
630 | fn trait_pointer_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, | |
631 | trait_type: Ty<'tcx>, | |
632 | trait_object_type: Option<Ty<'tcx>>, | |
633 | unique_type_id: UniqueTypeId) | |
634 | -> DIType { | |
635 | // The implementation provided here is a stub. It makes sure that the trait | |
636 | // type is assigned the correct name, size, namespace, and source location. | |
637 | // But it does not describe the trait's methods. | |
638 | ||
639 | let def_id = match trait_type.sty { | |
62682a34 | 640 | ty::TyTrait(ref data) => data.principal_def_id(), |
1a4d82fc | 641 | _ => { |
1a4d82fc | 642 | cx.sess().bug(&format!("debuginfo: Unexpected trait-object type in \ |
62682a34 SL |
643 | trait_pointer_metadata(): {:?}", |
644 | trait_type)); | |
1a4d82fc JJ |
645 | } |
646 | }; | |
647 | ||
648 | let trait_object_type = trait_object_type.unwrap_or(trait_type); | |
649 | let trait_type_name = | |
650 | compute_debuginfo_type_name(cx, trait_object_type, false); | |
651 | ||
652 | let (containing_scope, _) = get_namespace_and_span_for_item(cx, def_id); | |
653 | ||
654 | let trait_llvm_type = type_of::type_of(cx, trait_object_type); | |
655 | ||
656 | composite_type_metadata(cx, | |
657 | trait_llvm_type, | |
85aaf69f | 658 | &trait_type_name[..], |
1a4d82fc JJ |
659 | unique_type_id, |
660 | &[], | |
661 | containing_scope, | |
e9174d1e | 662 | NO_FILE_METADATA, |
1a4d82fc JJ |
663 | codemap::DUMMY_SP) |
664 | } | |
665 | ||
d9579d0f AL |
666 | pub fn type_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, |
667 | t: Ty<'tcx>, | |
668 | usage_site_span: Span) | |
669 | -> DIType { | |
1a4d82fc JJ |
670 | // Get the unique type id of this type. |
671 | let unique_type_id = { | |
672 | let mut type_map = debug_context(cx).type_map.borrow_mut(); | |
673 | // First, try to find the type in TypeMap. If we have seen it before, we | |
674 | // can exit early here. | |
675 | match type_map.find_metadata_for_type(t) { | |
676 | Some(metadata) => { | |
677 | return metadata; | |
678 | }, | |
679 | None => { | |
680 | // The Ty is not in the TypeMap but maybe we have already seen | |
681 | // an equivalent type (e.g. only differing in region arguments). | |
682 | // In order to find out, generate the unique type id and look | |
683 | // that up. | |
684 | let unique_type_id = type_map.get_unique_type_id_of_type(cx, t); | |
685 | match type_map.find_metadata_for_unique_id(unique_type_id) { | |
686 | Some(metadata) => { | |
687 | // There is already an equivalent type in the TypeMap. | |
688 | // Register this Ty as an alias in the cache and | |
689 | // return the cached metadata. | |
690 | type_map.register_type_with_metadata(cx, t, metadata); | |
691 | return metadata; | |
692 | }, | |
693 | None => { | |
694 | // There really is no type metadata for this type, so | |
695 | // proceed by creating it. | |
696 | unique_type_id | |
697 | } | |
698 | } | |
699 | } | |
700 | } | |
701 | }; | |
702 | ||
703 | debug!("type_metadata: {:?}", t); | |
704 | ||
705 | let sty = &t.sty; | |
706 | let MetadataCreationResult { metadata, already_stored_in_typemap } = match *sty { | |
62682a34 SL |
707 | ty::TyBool | |
708 | ty::TyChar | | |
709 | ty::TyInt(_) | | |
710 | ty::TyUint(_) | | |
711 | ty::TyFloat(_) => { | |
1a4d82fc JJ |
712 | MetadataCreationResult::new(basic_type_metadata(cx, t), false) |
713 | } | |
62682a34 | 714 | ty::TyTuple(ref elements) if elements.is_empty() => { |
1a4d82fc JJ |
715 | MetadataCreationResult::new(basic_type_metadata(cx, t), false) |
716 | } | |
e9174d1e SL |
717 | ty::TyEnum(def, _) => { |
718 | prepare_enum_metadata(cx, | |
719 | t, | |
720 | def.did, | |
721 | unique_type_id, | |
722 | usage_site_span).finalize(cx) | |
1a4d82fc | 723 | } |
62682a34 SL |
724 | ty::TyArray(typ, len) => { |
725 | fixed_vec_metadata(cx, unique_type_id, typ, Some(len as u64), usage_site_span) | |
726 | } | |
727 | ty::TySlice(typ) => { | |
728 | fixed_vec_metadata(cx, unique_type_id, typ, None, usage_site_span) | |
85aaf69f | 729 | } |
62682a34 | 730 | ty::TyStr => { |
85aaf69f | 731 | fixed_vec_metadata(cx, unique_type_id, cx.tcx().types.i8, None, usage_site_span) |
1a4d82fc | 732 | } |
62682a34 | 733 | ty::TyTrait(..) => { |
1a4d82fc JJ |
734 | MetadataCreationResult::new( |
735 | trait_pointer_metadata(cx, t, None, unique_type_id), | |
736 | false) | |
737 | } | |
c1a9b12d SL |
738 | ty::TyBox(ty) | |
739 | ty::TyRawPtr(ty::TypeAndMut{ty, ..}) | | |
740 | ty::TyRef(_, ty::TypeAndMut{ty, ..}) => { | |
1a4d82fc | 741 | match ty.sty { |
62682a34 | 742 | ty::TySlice(typ) => { |
1a4d82fc JJ |
743 | vec_slice_metadata(cx, t, typ, unique_type_id, usage_site_span) |
744 | } | |
62682a34 | 745 | ty::TyStr => { |
1a4d82fc JJ |
746 | vec_slice_metadata(cx, t, cx.tcx().types.u8, unique_type_id, usage_site_span) |
747 | } | |
62682a34 | 748 | ty::TyTrait(..) => { |
1a4d82fc JJ |
749 | MetadataCreationResult::new( |
750 | trait_pointer_metadata(cx, ty, Some(t), unique_type_id), | |
751 | false) | |
752 | } | |
753 | _ => { | |
754 | let pointee_metadata = type_metadata(cx, ty, usage_site_span); | |
755 | ||
756 | match debug_context(cx).type_map | |
757 | .borrow() | |
758 | .find_metadata_for_unique_id(unique_type_id) { | |
759 | Some(metadata) => return metadata, | |
760 | None => { /* proceed normally */ } | |
761 | }; | |
762 | ||
763 | MetadataCreationResult::new(pointer_type_metadata(cx, t, pointee_metadata), | |
764 | false) | |
765 | } | |
766 | } | |
767 | } | |
62682a34 | 768 | ty::TyBareFn(_, ref barefnty) => { |
c1a9b12d SL |
769 | let fn_metadata = subroutine_type_metadata(cx, |
770 | unique_type_id, | |
771 | &barefnty.sig, | |
772 | usage_site_span).metadata; | |
773 | match debug_context(cx).type_map | |
774 | .borrow() | |
775 | .find_metadata_for_unique_id(unique_type_id) { | |
776 | Some(metadata) => return metadata, | |
777 | None => { /* proceed normally */ } | |
778 | }; | |
779 | ||
780 | // This is actually a function pointer, so wrap it in pointer DI | |
781 | MetadataCreationResult::new(pointer_type_metadata(cx, t, fn_metadata), false) | |
782 | ||
1a4d82fc | 783 | } |
c1a9b12d SL |
784 | ty::TyClosure(_, ref substs) => { |
785 | prepare_tuple_metadata(cx, | |
786 | t, | |
787 | &substs.upvar_tys, | |
788 | unique_type_id, | |
789 | usage_site_span).finalize(cx) | |
1a4d82fc | 790 | } |
e9174d1e | 791 | ty::TyStruct(..) => { |
1a4d82fc JJ |
792 | prepare_struct_metadata(cx, |
793 | t, | |
1a4d82fc JJ |
794 | unique_type_id, |
795 | usage_site_span).finalize(cx) | |
796 | } | |
62682a34 | 797 | ty::TyTuple(ref elements) => { |
1a4d82fc JJ |
798 | prepare_tuple_metadata(cx, |
799 | t, | |
85aaf69f | 800 | &elements[..], |
1a4d82fc JJ |
801 | unique_type_id, |
802 | usage_site_span).finalize(cx) | |
803 | } | |
804 | _ => { | |
805 | cx.sess().bug(&format!("debuginfo: unexpected type in type_metadata: {:?}", | |
c34b1796 | 806 | sty)) |
1a4d82fc JJ |
807 | } |
808 | }; | |
809 | ||
810 | { | |
811 | let mut type_map = debug_context(cx).type_map.borrow_mut(); | |
812 | ||
813 | if already_stored_in_typemap { | |
b039eaaf | 814 | // Also make sure that we already have a TypeMap entry for the unique type id. |
1a4d82fc JJ |
815 | let metadata_for_uid = match type_map.find_metadata_for_unique_id(unique_type_id) { |
816 | Some(metadata) => metadata, | |
817 | None => { | |
818 | let unique_type_id_str = | |
819 | type_map.get_unique_type_id_as_string(unique_type_id); | |
820 | let error_message = format!("Expected type metadata for unique \ | |
821 | type id '{}' to already be in \ | |
822 | the debuginfo::TypeMap but it \ | |
823 | was not. (Ty = {})", | |
85aaf69f | 824 | &unique_type_id_str[..], |
62682a34 | 825 | t); |
85aaf69f | 826 | cx.sess().span_bug(usage_site_span, &error_message[..]); |
1a4d82fc JJ |
827 | } |
828 | }; | |
829 | ||
830 | match type_map.find_metadata_for_type(t) { | |
831 | Some(metadata) => { | |
832 | if metadata != metadata_for_uid { | |
833 | let unique_type_id_str = | |
834 | type_map.get_unique_type_id_as_string(unique_type_id); | |
835 | let error_message = format!("Mismatch between Ty and \ | |
836 | UniqueTypeId maps in \ | |
837 | debuginfo::TypeMap. \ | |
838 | UniqueTypeId={}, Ty={}", | |
85aaf69f | 839 | &unique_type_id_str[..], |
62682a34 | 840 | t); |
85aaf69f | 841 | cx.sess().span_bug(usage_site_span, &error_message[..]); |
1a4d82fc JJ |
842 | } |
843 | } | |
844 | None => { | |
845 | type_map.register_type_with_metadata(cx, t, metadata); | |
846 | } | |
847 | } | |
848 | } else { | |
849 | type_map.register_type_with_metadata(cx, t, metadata); | |
850 | type_map.register_unique_id_with_metadata(cx, unique_type_id, metadata); | |
851 | } | |
852 | } | |
853 | ||
854 | metadata | |
855 | } | |
856 | ||
d9579d0f | 857 | pub fn file_metadata(cx: &CrateContext, full_path: &str) -> DIFile { |
d9579d0f AL |
858 | // FIXME (#9639): This needs to handle non-utf8 paths |
859 | let work_dir = cx.sess().working_dir.to_str().unwrap(); | |
860 | let file_name = | |
861 | if full_path.starts_with(work_dir) { | |
862 | &full_path[work_dir.len() + 1..full_path.len()] | |
863 | } else { | |
864 | full_path | |
865 | }; | |
866 | ||
e9174d1e SL |
867 | file_metadata_(cx, full_path, file_name, &work_dir) |
868 | } | |
869 | ||
870 | pub fn unknown_file_metadata(cx: &CrateContext) -> DIFile { | |
871 | // Regular filenames should not be empty, so we abuse an empty name as the | |
872 | // key for the special unknown file metadata | |
873 | file_metadata_(cx, "", "<unknown>", "") | |
874 | ||
875 | } | |
876 | ||
877 | fn file_metadata_(cx: &CrateContext, key: &str, file_name: &str, work_dir: &str) -> DIFile { | |
878 | match debug_context(cx).created_files.borrow().get(key) { | |
879 | Some(file_metadata) => return *file_metadata, | |
880 | None => () | |
881 | } | |
882 | ||
883 | debug!("file_metadata: file_name: {}, work_dir: {}", file_name, work_dir); | |
884 | ||
d9579d0f AL |
885 | let file_name = CString::new(file_name).unwrap(); |
886 | let work_dir = CString::new(work_dir).unwrap(); | |
887 | let file_metadata = unsafe { | |
888 | llvm::LLVMDIBuilderCreateFile(DIB(cx), file_name.as_ptr(), | |
889 | work_dir.as_ptr()) | |
890 | }; | |
891 | ||
892 | let mut created_files = debug_context(cx).created_files.borrow_mut(); | |
e9174d1e SL |
893 | created_files.insert(key.to_string(), file_metadata); |
894 | file_metadata | |
d9579d0f AL |
895 | } |
896 | ||
897 | /// Finds the scope metadata node for the given AST node. | |
898 | pub fn scope_metadata(fcx: &FunctionContext, | |
899 | node_id: ast::NodeId, | |
900 | error_reporting_span: Span) | |
901 | -> DIScope { | |
902 | let scope_map = &fcx.debug_context | |
903 | .get_ref(fcx.ccx, error_reporting_span) | |
904 | .scope_map; | |
905 | match scope_map.borrow().get(&node_id).cloned() { | |
906 | Some(scope_metadata) => scope_metadata, | |
907 | None => { | |
908 | let node = fcx.ccx.tcx().map.get(node_id); | |
909 | ||
910 | fcx.ccx.sess().span_bug(error_reporting_span, | |
911 | &format!("debuginfo: Could not find scope info for node {:?}", | |
912 | node)); | |
913 | } | |
914 | } | |
915 | } | |
916 | ||
c1a9b12d | 917 | pub fn diverging_type_metadata(cx: &CrateContext) -> DIType { |
d9579d0f AL |
918 | unsafe { |
919 | llvm::LLVMDIBuilderCreateBasicType( | |
920 | DIB(cx), | |
921 | "!\0".as_ptr() as *const _, | |
922 | bytes_to_bits(0), | |
923 | bytes_to_bits(0), | |
924 | DW_ATE_unsigned) | |
925 | } | |
926 | } | |
927 | ||
928 | fn basic_type_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, | |
929 | t: Ty<'tcx>) -> DIType { | |
930 | ||
931 | debug!("basic_type_metadata: {:?}", t); | |
932 | ||
933 | let (name, encoding) = match t.sty { | |
62682a34 | 934 | ty::TyTuple(ref elements) if elements.is_empty() => |
92a42be0 SL |
935 | ("()", DW_ATE_unsigned), |
936 | ty::TyBool => ("bool", DW_ATE_boolean), | |
937 | ty::TyChar => ("char", DW_ATE_unsigned_char), | |
938 | ty::TyInt(int_ty) => { | |
9cc50fc6 | 939 | (int_ty.ty_to_string(), DW_ATE_signed) |
d9579d0f | 940 | }, |
92a42be0 | 941 | ty::TyUint(uint_ty) => { |
9cc50fc6 | 942 | (uint_ty.ty_to_string(), DW_ATE_unsigned) |
d9579d0f | 943 | }, |
92a42be0 | 944 | ty::TyFloat(float_ty) => { |
9cc50fc6 | 945 | (float_ty.ty_to_string(), DW_ATE_float) |
d9579d0f AL |
946 | }, |
947 | _ => cx.sess().bug("debuginfo::basic_type_metadata - t is invalid type") | |
948 | }; | |
949 | ||
950 | let llvm_type = type_of::type_of(cx, t); | |
951 | let (size, align) = size_and_align_of(cx, llvm_type); | |
952 | let name = CString::new(name).unwrap(); | |
953 | let ty_metadata = unsafe { | |
954 | llvm::LLVMDIBuilderCreateBasicType( | |
955 | DIB(cx), | |
956 | name.as_ptr(), | |
957 | bytes_to_bits(size), | |
958 | bytes_to_bits(align), | |
959 | encoding) | |
960 | }; | |
961 | ||
962 | return ty_metadata; | |
963 | } | |
964 | ||
965 | fn pointer_type_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, | |
966 | pointer_type: Ty<'tcx>, | |
967 | pointee_type_metadata: DIType) | |
968 | -> DIType { | |
969 | let pointer_llvm_type = type_of::type_of(cx, pointer_type); | |
970 | let (pointer_size, pointer_align) = size_and_align_of(cx, pointer_llvm_type); | |
971 | let name = compute_debuginfo_type_name(cx, pointer_type, false); | |
972 | let name = CString::new(name).unwrap(); | |
973 | let ptr_metadata = unsafe { | |
974 | llvm::LLVMDIBuilderCreatePointerType( | |
975 | DIB(cx), | |
976 | pointee_type_metadata, | |
977 | bytes_to_bits(pointer_size), | |
978 | bytes_to_bits(pointer_align), | |
979 | name.as_ptr()) | |
980 | }; | |
981 | return ptr_metadata; | |
982 | } | |
983 | ||
984 | pub fn compile_unit_metadata(cx: &CrateContext) -> DIDescriptor { | |
985 | let work_dir = &cx.sess().working_dir; | |
986 | let compile_unit_name = match cx.sess().local_crate_source_file { | |
987 | None => fallback_path(cx), | |
988 | Some(ref abs_path) => { | |
989 | if abs_path.is_relative() { | |
990 | cx.sess().warn("debuginfo: Invalid path to crate's local root source file!"); | |
991 | fallback_path(cx) | |
992 | } else { | |
9cc50fc6 SL |
993 | match abs_path.strip_prefix(work_dir) { |
994 | Ok(ref p) if p.is_relative() => { | |
d9579d0f AL |
995 | if p.starts_with(Path::new("./")) { |
996 | path2cstr(p) | |
997 | } else { | |
998 | path2cstr(&Path::new(".").join(p)) | |
999 | } | |
1000 | } | |
1001 | _ => fallback_path(cx) | |
1002 | } | |
1003 | } | |
1004 | } | |
1005 | }; | |
1006 | ||
1007 | debug!("compile_unit_metadata: {:?}", compile_unit_name); | |
1008 | let producer = format!("rustc version {}", | |
1009 | (option_env!("CFG_VERSION")).expect("CFG_VERSION")); | |
1010 | ||
1011 | let compile_unit_name = compile_unit_name.as_ptr(); | |
1012 | let work_dir = path2cstr(&work_dir); | |
1013 | let producer = CString::new(producer).unwrap(); | |
1014 | let flags = "\0"; | |
1015 | let split_name = "\0"; | |
1016 | return unsafe { | |
1017 | llvm::LLVMDIBuilderCreateCompileUnit( | |
1018 | debug_context(cx).builder, | |
1019 | DW_LANG_RUST, | |
1020 | compile_unit_name, | |
1021 | work_dir.as_ptr(), | |
1022 | producer.as_ptr(), | |
9cc50fc6 | 1023 | cx.sess().opts.optimize != config::OptLevel::No, |
d9579d0f AL |
1024 | flags.as_ptr() as *const _, |
1025 | 0, | |
1026 | split_name.as_ptr() as *const _) | |
1027 | }; | |
1028 | ||
1029 | fn fallback_path(cx: &CrateContext) -> CString { | |
1030 | CString::new(cx.link_meta().crate_name.clone()).unwrap() | |
1031 | } | |
1032 | } | |
1033 | ||
1a4d82fc JJ |
1034 | struct MetadataCreationResult { |
1035 | metadata: DIType, | |
1036 | already_stored_in_typemap: bool | |
1037 | } | |
1038 | ||
1039 | impl MetadataCreationResult { | |
1040 | fn new(metadata: DIType, already_stored_in_typemap: bool) -> MetadataCreationResult { | |
1041 | MetadataCreationResult { | |
1042 | metadata: metadata, | |
1043 | already_stored_in_typemap: already_stored_in_typemap | |
1044 | } | |
1045 | } | |
1046 | } | |
1047 | ||
d9579d0f AL |
1048 | #[derive(Debug)] |
1049 | enum MemberOffset { | |
1050 | FixedMemberOffset { bytes: usize }, | |
1051 | // For ComputedMemberOffset, the offset is read from the llvm type definition. | |
1052 | ComputedMemberOffset | |
1053 | } | |
1054 | ||
1055 | // Description of a type member, which can either be a regular field (as in | |
1056 | // structs or tuples) or an enum variant. | |
1057 | #[derive(Debug)] | |
1058 | struct MemberDescription { | |
1059 | name: String, | |
1060 | llvm_type: Type, | |
1061 | type_metadata: DIType, | |
1062 | offset: MemberOffset, | |
1063 | flags: c_uint | |
1064 | } | |
1065 | ||
1066 | // A factory for MemberDescriptions. It produces a list of member descriptions | |
1067 | // for some record-like type. MemberDescriptionFactories are used to defer the | |
1068 | // creation of type member descriptions in order to break cycles arising from | |
1069 | // recursive type definitions. | |
1070 | enum MemberDescriptionFactory<'tcx> { | |
1071 | StructMDF(StructMemberDescriptionFactory<'tcx>), | |
1072 | TupleMDF(TupleMemberDescriptionFactory<'tcx>), | |
1073 | EnumMDF(EnumMemberDescriptionFactory<'tcx>), | |
1074 | VariantMDF(VariantMemberDescriptionFactory<'tcx>) | |
1075 | } | |
1076 | ||
1077 | impl<'tcx> MemberDescriptionFactory<'tcx> { | |
1078 | fn create_member_descriptions<'a>(&self, cx: &CrateContext<'a, 'tcx>) | |
1079 | -> Vec<MemberDescription> { | |
1080 | match *self { | |
1081 | StructMDF(ref this) => { | |
1082 | this.create_member_descriptions(cx) | |
1083 | } | |
1084 | TupleMDF(ref this) => { | |
1085 | this.create_member_descriptions(cx) | |
1086 | } | |
1087 | EnumMDF(ref this) => { | |
1088 | this.create_member_descriptions(cx) | |
1089 | } | |
1090 | VariantMDF(ref this) => { | |
1091 | this.create_member_descriptions(cx) | |
1092 | } | |
1093 | } | |
1094 | } | |
1095 | } | |
1096 | ||
1097 | //=----------------------------------------------------------------------------- | |
1098 | // Structs | |
1099 | //=----------------------------------------------------------------------------- | |
1100 | ||
1101 | // Creates MemberDescriptions for the fields of a struct | |
1102 | struct StructMemberDescriptionFactory<'tcx> { | |
e9174d1e SL |
1103 | variant: ty::VariantDef<'tcx>, |
1104 | substs: &'tcx subst::Substs<'tcx>, | |
d9579d0f AL |
1105 | is_simd: bool, |
1106 | span: Span, | |
1107 | } | |
1108 | ||
1109 | impl<'tcx> StructMemberDescriptionFactory<'tcx> { | |
1110 | fn create_member_descriptions<'a>(&self, cx: &CrateContext<'a, 'tcx>) | |
1111 | -> Vec<MemberDescription> { | |
e9174d1e | 1112 | if let ty::VariantKind::Unit = self.variant.kind() { |
d9579d0f AL |
1113 | return Vec::new(); |
1114 | } | |
1115 | ||
1116 | let field_size = if self.is_simd { | |
e9174d1e SL |
1117 | let fty = monomorphize::field_ty(cx.tcx(), |
1118 | self.substs, | |
1119 | &self.variant.fields[0]); | |
1120 | Some(machine::llsize_of_alloc( | |
1121 | cx, | |
1122 | type_of::type_of(cx, fty) | |
1123 | ) as usize) | |
d9579d0f | 1124 | } else { |
e9174d1e | 1125 | None |
d9579d0f AL |
1126 | }; |
1127 | ||
e9174d1e SL |
1128 | self.variant.fields.iter().enumerate().map(|(i, f)| { |
1129 | let name = if let ty::VariantKind::Tuple = self.variant.kind() { | |
d9579d0f AL |
1130 | format!("__{}", i) |
1131 | } else { | |
e9174d1e | 1132 | f.name.to_string() |
d9579d0f | 1133 | }; |
e9174d1e | 1134 | let fty = monomorphize::field_ty(cx.tcx(), self.substs, f); |
1a4d82fc | 1135 | |
d9579d0f | 1136 | let offset = if self.is_simd { |
e9174d1e | 1137 | FixedMemberOffset { bytes: i * field_size.unwrap() } |
d9579d0f AL |
1138 | } else { |
1139 | ComputedMemberOffset | |
1140 | }; | |
1141 | ||
1142 | MemberDescription { | |
1143 | name: name, | |
e9174d1e SL |
1144 | llvm_type: type_of::type_of(cx, fty), |
1145 | type_metadata: type_metadata(cx, fty, self.span), | |
d9579d0f AL |
1146 | offset: offset, |
1147 | flags: FLAGS_NONE, | |
1148 | } | |
1149 | }).collect() | |
1a4d82fc JJ |
1150 | } |
1151 | } | |
1152 | ||
1a4d82fc | 1153 | |
d9579d0f AL |
1154 | fn prepare_struct_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, |
1155 | struct_type: Ty<'tcx>, | |
d9579d0f AL |
1156 | unique_type_id: UniqueTypeId, |
1157 | span: Span) | |
1158 | -> RecursiveTypeDescription<'tcx> { | |
1159 | let struct_name = compute_debuginfo_type_name(cx, struct_type, false); | |
1160 | let struct_llvm_type = type_of::in_memory_type_of(cx, struct_type); | |
1161 | ||
e9174d1e SL |
1162 | let (variant, substs) = match struct_type.sty { |
1163 | ty::TyStruct(def, substs) => (def.struct_variant(), substs), | |
1164 | _ => cx.tcx().sess.bug("prepare_struct_metadata on a non-struct") | |
1165 | }; | |
1166 | ||
1167 | let (containing_scope, _) = get_namespace_and_span_for_item(cx, variant.did); | |
1a4d82fc | 1168 | |
d9579d0f AL |
1169 | let struct_metadata_stub = create_struct_stub(cx, |
1170 | struct_llvm_type, | |
1171 | &struct_name, | |
1172 | unique_type_id, | |
1173 | containing_scope); | |
85aaf69f | 1174 | |
d9579d0f AL |
1175 | create_and_register_recursive_type_forward_declaration( |
1176 | cx, | |
1177 | struct_type, | |
1178 | unique_type_id, | |
1179 | struct_metadata_stub, | |
1180 | struct_llvm_type, | |
1181 | StructMDF(StructMemberDescriptionFactory { | |
e9174d1e SL |
1182 | variant: variant, |
1183 | substs: substs, | |
1184 | is_simd: struct_type.is_simd(), | |
d9579d0f AL |
1185 | span: span, |
1186 | }) | |
1187 | ) | |
1a4d82fc JJ |
1188 | } |
1189 | ||
d9579d0f | 1190 | |
1a4d82fc | 1191 | //=----------------------------------------------------------------------------- |
d9579d0f | 1192 | // Tuples |
1a4d82fc JJ |
1193 | //=----------------------------------------------------------------------------- |
1194 | ||
d9579d0f AL |
1195 | // Creates MemberDescriptions for the fields of a tuple |
1196 | struct TupleMemberDescriptionFactory<'tcx> { | |
1197 | component_types: Vec<Ty<'tcx>>, | |
1198 | span: Span, | |
1a4d82fc JJ |
1199 | } |
1200 | ||
d9579d0f AL |
1201 | impl<'tcx> TupleMemberDescriptionFactory<'tcx> { |
1202 | fn create_member_descriptions<'a>(&self, cx: &CrateContext<'a, 'tcx>) | |
1203 | -> Vec<MemberDescription> { | |
1204 | self.component_types | |
1205 | .iter() | |
1206 | .enumerate() | |
1207 | .map(|(i, &component_type)| { | |
1208 | MemberDescription { | |
1209 | name: format!("__{}", i), | |
1210 | llvm_type: type_of::type_of(cx, component_type), | |
1211 | type_metadata: type_metadata(cx, component_type, self.span), | |
1212 | offset: ComputedMemberOffset, | |
1213 | flags: FLAGS_NONE, | |
1214 | } | |
1215 | }).collect() | |
1216 | } | |
1a4d82fc JJ |
1217 | } |
1218 | ||
d9579d0f AL |
1219 | fn prepare_tuple_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, |
1220 | tuple_type: Ty<'tcx>, | |
1221 | component_types: &[Ty<'tcx>], | |
1222 | unique_type_id: UniqueTypeId, | |
1223 | span: Span) | |
1224 | -> RecursiveTypeDescription<'tcx> { | |
1225 | let tuple_name = compute_debuginfo_type_name(cx, tuple_type, false); | |
1226 | let tuple_llvm_type = type_of::type_of(cx, tuple_type); | |
1a4d82fc | 1227 | |
d9579d0f AL |
1228 | create_and_register_recursive_type_forward_declaration( |
1229 | cx, | |
1230 | tuple_type, | |
1231 | unique_type_id, | |
1232 | create_struct_stub(cx, | |
1233 | tuple_llvm_type, | |
1234 | &tuple_name[..], | |
1235 | unique_type_id, | |
e9174d1e | 1236 | NO_SCOPE_METADATA), |
d9579d0f AL |
1237 | tuple_llvm_type, |
1238 | TupleMDF(TupleMemberDescriptionFactory { | |
1239 | component_types: component_types.to_vec(), | |
1240 | span: span, | |
1241 | }) | |
1242 | ) | |
1a4d82fc JJ |
1243 | } |
1244 | ||
1a4d82fc | 1245 | |
d9579d0f AL |
1246 | //=----------------------------------------------------------------------------- |
1247 | // Enums | |
1248 | //=----------------------------------------------------------------------------- | |
1a4d82fc | 1249 | |
d9579d0f AL |
1250 | // Describes the members of an enum value: An enum is described as a union of |
1251 | // structs in DWARF. This MemberDescriptionFactory provides the description for | |
1252 | // the members of this union; so for every variant of the given enum, this | |
1253 | // factory will produce one MemberDescription (all with no name and a fixed | |
1254 | // offset of zero bytes). | |
1255 | struct EnumMemberDescriptionFactory<'tcx> { | |
1256 | enum_type: Ty<'tcx>, | |
1257 | type_rep: Rc<adt::Repr<'tcx>>, | |
d9579d0f AL |
1258 | discriminant_type_metadata: Option<DIType>, |
1259 | containing_scope: DIScope, | |
1260 | file_metadata: DIFile, | |
1261 | span: Span, | |
1a4d82fc JJ |
1262 | } |
1263 | ||
d9579d0f AL |
1264 | impl<'tcx> EnumMemberDescriptionFactory<'tcx> { |
1265 | fn create_member_descriptions<'a>(&self, cx: &CrateContext<'a, 'tcx>) | |
1266 | -> Vec<MemberDescription> { | |
e9174d1e | 1267 | let adt = &self.enum_type.ty_adt_def().unwrap(); |
d9579d0f AL |
1268 | match *self.type_rep { |
1269 | adt::General(_, ref struct_defs, _) => { | |
1270 | let discriminant_info = RegularDiscriminant(self.discriminant_type_metadata | |
1271 | .expect("")); | |
d9579d0f AL |
1272 | struct_defs |
1273 | .iter() | |
1274 | .enumerate() | |
1275 | .map(|(i, struct_def)| { | |
1276 | let (variant_type_metadata, | |
1277 | variant_llvm_type, | |
1278 | member_desc_factory) = | |
1279 | describe_enum_variant(cx, | |
1280 | self.enum_type, | |
1281 | struct_def, | |
e9174d1e | 1282 | &adt.variants[i], |
d9579d0f AL |
1283 | discriminant_info, |
1284 | self.containing_scope, | |
1285 | self.span); | |
1a4d82fc | 1286 | |
d9579d0f AL |
1287 | let member_descriptions = member_desc_factory |
1288 | .create_member_descriptions(cx); | |
1a4d82fc | 1289 | |
d9579d0f AL |
1290 | set_members_of_composite_type(cx, |
1291 | variant_type_metadata, | |
1292 | variant_llvm_type, | |
1293 | &member_descriptions); | |
1294 | MemberDescription { | |
1295 | name: "".to_string(), | |
1296 | llvm_type: variant_llvm_type, | |
1297 | type_metadata: variant_type_metadata, | |
1298 | offset: FixedMemberOffset { bytes: 0 }, | |
1299 | flags: FLAGS_NONE | |
1300 | } | |
1301 | }).collect() | |
1302 | }, | |
1303 | adt::Univariant(ref struct_def, _) => { | |
e9174d1e | 1304 | assert!(adt.variants.len() <= 1); |
1a4d82fc | 1305 | |
e9174d1e | 1306 | if adt.variants.is_empty() { |
d9579d0f AL |
1307 | vec![] |
1308 | } else { | |
1309 | let (variant_type_metadata, | |
1310 | variant_llvm_type, | |
1311 | member_description_factory) = | |
1312 | describe_enum_variant(cx, | |
1313 | self.enum_type, | |
1314 | struct_def, | |
e9174d1e | 1315 | &adt.variants[0], |
d9579d0f AL |
1316 | NoDiscriminant, |
1317 | self.containing_scope, | |
1318 | self.span); | |
1a4d82fc | 1319 | |
d9579d0f AL |
1320 | let member_descriptions = |
1321 | member_description_factory.create_member_descriptions(cx); | |
1a4d82fc | 1322 | |
d9579d0f AL |
1323 | set_members_of_composite_type(cx, |
1324 | variant_type_metadata, | |
1325 | variant_llvm_type, | |
1326 | &member_descriptions[..]); | |
1327 | vec![ | |
1328 | MemberDescription { | |
1329 | name: "".to_string(), | |
1330 | llvm_type: variant_llvm_type, | |
1331 | type_metadata: variant_type_metadata, | |
1332 | offset: FixedMemberOffset { bytes: 0 }, | |
1333 | flags: FLAGS_NONE | |
1334 | } | |
1335 | ] | |
1336 | } | |
1337 | } | |
1338 | adt::RawNullablePointer { nndiscr: non_null_variant_index, nnty, .. } => { | |
1339 | // As far as debuginfo is concerned, the pointer this enum | |
1340 | // represents is still wrapped in a struct. This is to make the | |
1341 | // DWARF representation of enums uniform. | |
1a4d82fc | 1342 | |
d9579d0f | 1343 | // First create a description of the artificial wrapper struct: |
9cc50fc6 | 1344 | let non_null_variant = &adt.variants[non_null_variant_index.0 as usize]; |
c1a9b12d | 1345 | let non_null_variant_name = non_null_variant.name.as_str(); |
1a4d82fc | 1346 | |
d9579d0f AL |
1347 | // The llvm type and metadata of the pointer |
1348 | let non_null_llvm_type = type_of::type_of(cx, nnty); | |
1349 | let non_null_type_metadata = type_metadata(cx, nnty, self.span); | |
1a4d82fc | 1350 | |
d9579d0f AL |
1351 | // The type of the artificial struct wrapping the pointer |
1352 | let artificial_struct_llvm_type = Type::struct_(cx, | |
1353 | &[non_null_llvm_type], | |
1354 | false); | |
1a4d82fc | 1355 | |
d9579d0f AL |
1356 | // For the metadata of the wrapper struct, we need to create a |
1357 | // MemberDescription of the struct's single field. | |
1358 | let sole_struct_member_description = MemberDescription { | |
e9174d1e SL |
1359 | name: match non_null_variant.kind() { |
1360 | ty::VariantKind::Tuple => "__0".to_string(), | |
b039eaaf | 1361 | ty::VariantKind::Struct => { |
e9174d1e SL |
1362 | non_null_variant.fields[0].name.to_string() |
1363 | } | |
1364 | ty::VariantKind::Unit => unreachable!() | |
d9579d0f AL |
1365 | }, |
1366 | llvm_type: non_null_llvm_type, | |
1367 | type_metadata: non_null_type_metadata, | |
1368 | offset: FixedMemberOffset { bytes: 0 }, | |
1369 | flags: FLAGS_NONE | |
1370 | }; | |
1a4d82fc | 1371 | |
d9579d0f AL |
1372 | let unique_type_id = debug_context(cx).type_map |
1373 | .borrow_mut() | |
1374 | .get_unique_type_id_of_enum_variant( | |
1375 | cx, | |
1376 | self.enum_type, | |
1377 | &non_null_variant_name); | |
1a4d82fc | 1378 | |
d9579d0f AL |
1379 | // Now we can create the metadata of the artificial struct |
1380 | let artificial_struct_metadata = | |
1381 | composite_type_metadata(cx, | |
1382 | artificial_struct_llvm_type, | |
1383 | &non_null_variant_name, | |
1384 | unique_type_id, | |
1385 | &[sole_struct_member_description], | |
1386 | self.containing_scope, | |
1387 | self.file_metadata, | |
1388 | codemap::DUMMY_SP); | |
1a4d82fc | 1389 | |
d9579d0f AL |
1390 | // Encode the information about the null variant in the union |
1391 | // member's name. | |
9cc50fc6 | 1392 | let null_variant_index = (1 - non_null_variant_index.0) as usize; |
e9174d1e | 1393 | let null_variant_name = adt.variants[null_variant_index].name; |
d9579d0f AL |
1394 | let union_member_name = format!("RUST$ENCODED$ENUM${}${}", |
1395 | 0, | |
1396 | null_variant_name); | |
1a4d82fc | 1397 | |
d9579d0f AL |
1398 | // Finally create the (singleton) list of descriptions of union |
1399 | // members. | |
1400 | vec![ | |
1401 | MemberDescription { | |
1402 | name: union_member_name, | |
1403 | llvm_type: artificial_struct_llvm_type, | |
1404 | type_metadata: artificial_struct_metadata, | |
1405 | offset: FixedMemberOffset { bytes: 0 }, | |
1406 | flags: FLAGS_NONE | |
1407 | } | |
1408 | ] | |
1409 | }, | |
1410 | adt::StructWrappedNullablePointer { nonnull: ref struct_def, | |
1411 | nndiscr, | |
1412 | ref discrfield, ..} => { | |
1413 | // Create a description of the non-null variant | |
1414 | let (variant_type_metadata, variant_llvm_type, member_description_factory) = | |
1415 | describe_enum_variant(cx, | |
1416 | self.enum_type, | |
1417 | struct_def, | |
9cc50fc6 | 1418 | &adt.variants[nndiscr.0 as usize], |
d9579d0f AL |
1419 | OptimizedDiscriminant, |
1420 | self.containing_scope, | |
1421 | self.span); | |
1a4d82fc | 1422 | |
d9579d0f AL |
1423 | let variant_member_descriptions = |
1424 | member_description_factory.create_member_descriptions(cx); | |
1a4d82fc | 1425 | |
d9579d0f AL |
1426 | set_members_of_composite_type(cx, |
1427 | variant_type_metadata, | |
1428 | variant_llvm_type, | |
1429 | &variant_member_descriptions[..]); | |
1a4d82fc | 1430 | |
d9579d0f AL |
1431 | // Encode the information about the null variant in the union |
1432 | // member's name. | |
9cc50fc6 | 1433 | let null_variant_index = (1 - nndiscr.0) as usize; |
e9174d1e | 1434 | let null_variant_name = adt.variants[null_variant_index].name; |
d9579d0f AL |
1435 | let discrfield = discrfield.iter() |
1436 | .skip(1) | |
1437 | .map(|x| x.to_string()) | |
c1a9b12d | 1438 | .collect::<Vec<_>>().join("$"); |
d9579d0f AL |
1439 | let union_member_name = format!("RUST$ENCODED$ENUM${}${}", |
1440 | discrfield, | |
1441 | null_variant_name); | |
1a4d82fc | 1442 | |
d9579d0f AL |
1443 | // Create the (singleton) list of descriptions of union members. |
1444 | vec![ | |
1445 | MemberDescription { | |
1446 | name: union_member_name, | |
1447 | llvm_type: variant_llvm_type, | |
1448 | type_metadata: variant_type_metadata, | |
1449 | offset: FixedMemberOffset { bytes: 0 }, | |
1450 | flags: FLAGS_NONE | |
1451 | } | |
1452 | ] | |
1453 | }, | |
1454 | adt::CEnum(..) => cx.sess().span_bug(self.span, "This should be unreachable.") | |
1a4d82fc JJ |
1455 | } |
1456 | } | |
d9579d0f | 1457 | } |
1a4d82fc | 1458 | |
d9579d0f AL |
1459 | // Creates MemberDescriptions for the fields of a single enum variant. |
1460 | struct VariantMemberDescriptionFactory<'tcx> { | |
1461 | args: Vec<(String, Ty<'tcx>)>, | |
1462 | discriminant_type_metadata: Option<DIType>, | |
1463 | span: Span, | |
1464 | } | |
1a4d82fc | 1465 | |
d9579d0f AL |
1466 | impl<'tcx> VariantMemberDescriptionFactory<'tcx> { |
1467 | fn create_member_descriptions<'a>(&self, cx: &CrateContext<'a, 'tcx>) | |
1468 | -> Vec<MemberDescription> { | |
1469 | self.args.iter().enumerate().map(|(i, &(ref name, ty))| { | |
1470 | MemberDescription { | |
1471 | name: name.to_string(), | |
1472 | llvm_type: type_of::type_of(cx, ty), | |
1473 | type_metadata: match self.discriminant_type_metadata { | |
1474 | Some(metadata) if i == 0 => metadata, | |
1475 | _ => type_metadata(cx, ty, self.span) | |
1476 | }, | |
1477 | offset: ComputedMemberOffset, | |
1478 | flags: FLAGS_NONE | |
1a4d82fc | 1479 | } |
d9579d0f | 1480 | }).collect() |
1a4d82fc | 1481 | } |
d9579d0f | 1482 | } |
1a4d82fc | 1483 | |
d9579d0f AL |
1484 | #[derive(Copy, Clone)] |
1485 | enum EnumDiscriminantInfo { | |
1486 | RegularDiscriminant(DIType), | |
1487 | OptimizedDiscriminant, | |
1488 | NoDiscriminant | |
1489 | } | |
1a4d82fc | 1490 | |
d9579d0f AL |
1491 | // Returns a tuple of (1) type_metadata_stub of the variant, (2) the llvm_type |
1492 | // of the variant, and (3) a MemberDescriptionFactory for producing the | |
1493 | // descriptions of the fields of the variant. This is a rudimentary version of a | |
1494 | // full RecursiveTypeDescription. | |
1495 | fn describe_enum_variant<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, | |
1496 | enum_type: Ty<'tcx>, | |
1497 | struct_def: &adt::Struct<'tcx>, | |
e9174d1e | 1498 | variant: ty::VariantDef<'tcx>, |
d9579d0f AL |
1499 | discriminant_info: EnumDiscriminantInfo, |
1500 | containing_scope: DIScope, | |
1501 | span: Span) | |
1502 | -> (DICompositeType, Type, MemberDescriptionFactory<'tcx>) { | |
1503 | let variant_llvm_type = | |
1504 | Type::struct_(cx, &struct_def.fields | |
1505 | .iter() | |
1506 | .map(|&t| type_of::type_of(cx, t)) | |
1507 | .collect::<Vec<_>>() | |
1508 | , | |
1509 | struct_def.packed); | |
1510 | // Could do some consistency checks here: size, align, field count, discr type | |
1a4d82fc | 1511 | |
e9174d1e | 1512 | let variant_name = variant.name.as_str(); |
d9579d0f AL |
1513 | let unique_type_id = debug_context(cx).type_map |
1514 | .borrow_mut() | |
1515 | .get_unique_type_id_of_enum_variant( | |
1516 | cx, | |
1517 | enum_type, | |
c1a9b12d | 1518 | &variant_name); |
1a4d82fc | 1519 | |
d9579d0f AL |
1520 | let metadata_stub = create_struct_stub(cx, |
1521 | variant_llvm_type, | |
c1a9b12d | 1522 | &variant_name, |
d9579d0f AL |
1523 | unique_type_id, |
1524 | containing_scope); | |
1a4d82fc | 1525 | |
d9579d0f | 1526 | // Get the argument names from the enum variant info |
e9174d1e SL |
1527 | let mut arg_names: Vec<_> = match variant.kind() { |
1528 | ty::VariantKind::Unit => vec![], | |
1529 | ty::VariantKind::Tuple => { | |
1530 | variant.fields | |
1531 | .iter() | |
1532 | .enumerate() | |
1533 | .map(|(i, _)| format!("__{}", i)) | |
1534 | .collect() | |
d9579d0f | 1535 | } |
b039eaaf | 1536 | ty::VariantKind::Struct => { |
e9174d1e SL |
1537 | variant.fields |
1538 | .iter() | |
1539 | .map(|f| f.name.to_string()) | |
1540 | .collect() | |
d9579d0f AL |
1541 | } |
1542 | }; | |
1a4d82fc | 1543 | |
d9579d0f AL |
1544 | // If this is not a univariant enum, there is also the discriminant field. |
1545 | match discriminant_info { | |
1546 | RegularDiscriminant(_) => arg_names.insert(0, "RUST$ENUM$DISR".to_string()), | |
1547 | _ => { /* do nothing */ } | |
1548 | }; | |
1a4d82fc | 1549 | |
d9579d0f AL |
1550 | // Build an array of (field name, field type) pairs to be captured in the factory closure. |
1551 | let args: Vec<(String, Ty)> = arg_names.iter() | |
62682a34 | 1552 | .zip(&struct_def.fields) |
d9579d0f AL |
1553 | .map(|(s, &t)| (s.to_string(), t)) |
1554 | .collect(); | |
1a4d82fc | 1555 | |
d9579d0f AL |
1556 | let member_description_factory = |
1557 | VariantMDF(VariantMemberDescriptionFactory { | |
1558 | args: args, | |
1559 | discriminant_type_metadata: match discriminant_info { | |
1560 | RegularDiscriminant(discriminant_type_metadata) => { | |
1561 | Some(discriminant_type_metadata) | |
1a4d82fc | 1562 | } |
d9579d0f AL |
1563 | _ => None |
1564 | }, | |
1565 | span: span, | |
1566 | }); | |
1a4d82fc | 1567 | |
d9579d0f AL |
1568 | (metadata_stub, variant_llvm_type, member_description_factory) |
1569 | } | |
1a4d82fc | 1570 | |
d9579d0f AL |
1571 | fn prepare_enum_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, |
1572 | enum_type: Ty<'tcx>, | |
e9174d1e | 1573 | enum_def_id: DefId, |
d9579d0f AL |
1574 | unique_type_id: UniqueTypeId, |
1575 | span: Span) | |
1576 | -> RecursiveTypeDescription<'tcx> { | |
1577 | let enum_name = compute_debuginfo_type_name(cx, enum_type, false); | |
1a4d82fc | 1578 | |
e9174d1e SL |
1579 | let (containing_scope, _) = get_namespace_and_span_for_item(cx, enum_def_id); |
1580 | // FIXME: This should emit actual file metadata for the enum, but we | |
1581 | // currently can't get the necessary information when it comes to types | |
1582 | // imported from other crates. Formerly we violated the ODR when performing | |
1583 | // LTO because we emitted debuginfo for the same type with varying file | |
1584 | // metadata, so as a workaround we pretend that the type comes from | |
1585 | // <unknown> | |
1586 | let file_metadata = unknown_file_metadata(cx); | |
1a4d82fc | 1587 | |
e9174d1e | 1588 | let variants = &enum_type.ty_adt_def().unwrap().variants; |
1a4d82fc | 1589 | |
d9579d0f AL |
1590 | let enumerators_metadata: Vec<DIDescriptor> = variants |
1591 | .iter() | |
1592 | .map(|v| { | |
c1a9b12d | 1593 | let token = v.name.as_str(); |
d9579d0f AL |
1594 | let name = CString::new(token.as_bytes()).unwrap(); |
1595 | unsafe { | |
1596 | llvm::LLVMDIBuilderCreateEnumerator( | |
1597 | DIB(cx), | |
1598 | name.as_ptr(), | |
1599 | v.disr_val as u64) | |
1a4d82fc | 1600 | } |
d9579d0f AL |
1601 | }) |
1602 | .collect(); | |
1a4d82fc | 1603 | |
b039eaaf | 1604 | let discriminant_type_metadata = |inttype: syntax::attr::IntType| { |
c1a9b12d | 1605 | let disr_type_key = (enum_def_id, inttype); |
d9579d0f AL |
1606 | let cached_discriminant_type_metadata = debug_context(cx).created_enum_disr_types |
1607 | .borrow() | |
c1a9b12d | 1608 | .get(&disr_type_key).cloned(); |
d9579d0f AL |
1609 | match cached_discriminant_type_metadata { |
1610 | Some(discriminant_type_metadata) => discriminant_type_metadata, | |
1611 | None => { | |
1612 | let discriminant_llvm_type = adt::ll_inttype(cx, inttype); | |
1613 | let (discriminant_size, discriminant_align) = | |
1614 | size_and_align_of(cx, discriminant_llvm_type); | |
1615 | let discriminant_base_type_metadata = | |
1616 | type_metadata(cx, | |
1617 | adt::ty_of_inttype(cx.tcx(), inttype), | |
1618 | codemap::DUMMY_SP); | |
1619 | let discriminant_name = get_enum_discriminant_name(cx, enum_def_id); | |
1a4d82fc | 1620 | |
d9579d0f AL |
1621 | let name = CString::new(discriminant_name.as_bytes()).unwrap(); |
1622 | let discriminant_type_metadata = unsafe { | |
1623 | llvm::LLVMDIBuilderCreateEnumerationType( | |
1624 | DIB(cx), | |
1625 | containing_scope, | |
1626 | name.as_ptr(), | |
e9174d1e | 1627 | NO_FILE_METADATA, |
d9579d0f AL |
1628 | UNKNOWN_LINE_NUMBER, |
1629 | bytes_to_bits(discriminant_size), | |
1630 | bytes_to_bits(discriminant_align), | |
1631 | create_DIArray(DIB(cx), &enumerators_metadata), | |
1632 | discriminant_base_type_metadata) | |
1633 | }; | |
1a4d82fc | 1634 | |
d9579d0f AL |
1635 | debug_context(cx).created_enum_disr_types |
1636 | .borrow_mut() | |
c1a9b12d | 1637 | .insert(disr_type_key, discriminant_type_metadata); |
1a4d82fc | 1638 | |
d9579d0f | 1639 | discriminant_type_metadata |
1a4d82fc JJ |
1640 | } |
1641 | } | |
d9579d0f | 1642 | }; |
1a4d82fc | 1643 | |
d9579d0f | 1644 | let type_rep = adt::represent_type(cx, enum_type); |
1a4d82fc | 1645 | |
d9579d0f AL |
1646 | let discriminant_type_metadata = match *type_rep { |
1647 | adt::CEnum(inttype, _, _) => { | |
1648 | return FinalMetadata(discriminant_type_metadata(inttype)) | |
1649 | }, | |
1650 | adt::RawNullablePointer { .. } | | |
1651 | adt::StructWrappedNullablePointer { .. } | | |
1652 | adt::Univariant(..) => None, | |
1653 | adt::General(inttype, _, _) => Some(discriminant_type_metadata(inttype)), | |
1654 | }; | |
1a4d82fc | 1655 | |
d9579d0f AL |
1656 | let enum_llvm_type = type_of::type_of(cx, enum_type); |
1657 | let (enum_type_size, enum_type_align) = size_and_align_of(cx, enum_llvm_type); | |
1a4d82fc | 1658 | |
d9579d0f AL |
1659 | let unique_type_id_str = debug_context(cx) |
1660 | .type_map | |
1661 | .borrow() | |
1662 | .get_unique_type_id_as_string(unique_type_id); | |
1a4d82fc | 1663 | |
d9579d0f AL |
1664 | let enum_name = CString::new(enum_name).unwrap(); |
1665 | let unique_type_id_str = CString::new(unique_type_id_str.as_bytes()).unwrap(); | |
1666 | let enum_metadata = unsafe { | |
1667 | llvm::LLVMDIBuilderCreateUnionType( | |
1668 | DIB(cx), | |
1669 | containing_scope, | |
1670 | enum_name.as_ptr(), | |
62682a34 | 1671 | file_metadata, |
d9579d0f AL |
1672 | UNKNOWN_LINE_NUMBER, |
1673 | bytes_to_bits(enum_type_size), | |
1674 | bytes_to_bits(enum_type_align), | |
1675 | 0, // Flags | |
1676 | ptr::null_mut(), | |
1677 | 0, // RuntimeLang | |
1678 | unique_type_id_str.as_ptr()) | |
1679 | }; | |
1a4d82fc | 1680 | |
d9579d0f AL |
1681 | return create_and_register_recursive_type_forward_declaration( |
1682 | cx, | |
1683 | enum_type, | |
1684 | unique_type_id, | |
1685 | enum_metadata, | |
1686 | enum_llvm_type, | |
1687 | EnumMDF(EnumMemberDescriptionFactory { | |
1688 | enum_type: enum_type, | |
1689 | type_rep: type_rep.clone(), | |
d9579d0f AL |
1690 | discriminant_type_metadata: discriminant_type_metadata, |
1691 | containing_scope: containing_scope, | |
1692 | file_metadata: file_metadata, | |
1693 | span: span, | |
1694 | }), | |
1695 | ); | |
1a4d82fc | 1696 | |
d9579d0f | 1697 | fn get_enum_discriminant_name(cx: &CrateContext, |
e9174d1e | 1698 | def_id: DefId) |
d9579d0f | 1699 | -> token::InternedString { |
e9174d1e | 1700 | cx.tcx().item_name(def_id).as_str() |
d9579d0f AL |
1701 | } |
1702 | } | |
1a4d82fc | 1703 | |
d9579d0f AL |
1704 | /// Creates debug information for a composite type, that is, anything that |
1705 | /// results in a LLVM struct. | |
1706 | /// | |
1707 | /// Examples of Rust types to use this are: structs, tuples, boxes, vecs, and enums. | |
1708 | fn composite_type_metadata(cx: &CrateContext, | |
1709 | composite_llvm_type: Type, | |
1710 | composite_type_name: &str, | |
1711 | composite_type_unique_id: UniqueTypeId, | |
1712 | member_descriptions: &[MemberDescription], | |
1713 | containing_scope: DIScope, | |
1a4d82fc | 1714 | |
d9579d0f AL |
1715 | // Ignore source location information as long as it |
1716 | // can't be reconstructed for non-local crates. | |
1717 | _file_metadata: DIFile, | |
1718 | _definition_span: Span) | |
1719 | -> DICompositeType { | |
1720 | // Create the (empty) struct metadata node ... | |
1721 | let composite_type_metadata = create_struct_stub(cx, | |
1722 | composite_llvm_type, | |
1723 | composite_type_name, | |
1724 | composite_type_unique_id, | |
1725 | containing_scope); | |
1726 | // ... and immediately create and add the member descriptions. | |
1727 | set_members_of_composite_type(cx, | |
1728 | composite_type_metadata, | |
1729 | composite_llvm_type, | |
1730 | member_descriptions); | |
1a4d82fc | 1731 | |
d9579d0f AL |
1732 | return composite_type_metadata; |
1733 | } | |
1a4d82fc | 1734 | |
d9579d0f AL |
1735 | fn set_members_of_composite_type(cx: &CrateContext, |
1736 | composite_type_metadata: DICompositeType, | |
1737 | composite_llvm_type: Type, | |
1738 | member_descriptions: &[MemberDescription]) { | |
1739 | // In some rare cases LLVM metadata uniquing would lead to an existing type | |
1740 | // description being used instead of a new one created in | |
1741 | // create_struct_stub. This would cause a hard to trace assertion in | |
1742 | // DICompositeType::SetTypeArray(). The following check makes sure that we | |
1743 | // get a better error message if this should happen again due to some | |
1744 | // regression. | |
1745 | { | |
1746 | let mut composite_types_completed = | |
1747 | debug_context(cx).composite_types_completed.borrow_mut(); | |
1748 | if composite_types_completed.contains(&composite_type_metadata) { | |
1749 | cx.sess().bug("debuginfo::set_members_of_composite_type() - \ | |
1750 | Already completed forward declaration re-encountered."); | |
1751 | } else { | |
1752 | composite_types_completed.insert(composite_type_metadata); | |
1753 | } | |
1754 | } | |
1a4d82fc | 1755 | |
d9579d0f AL |
1756 | let member_metadata: Vec<DIDescriptor> = member_descriptions |
1757 | .iter() | |
1758 | .enumerate() | |
1759 | .map(|(i, member_description)| { | |
1760 | let (member_size, member_align) = size_and_align_of(cx, member_description.llvm_type); | |
1761 | let member_offset = match member_description.offset { | |
1762 | FixedMemberOffset { bytes } => bytes as u64, | |
1763 | ComputedMemberOffset => machine::llelement_offset(cx, composite_llvm_type, i) | |
1764 | }; | |
1a4d82fc | 1765 | |
d9579d0f AL |
1766 | let member_name = member_description.name.as_bytes(); |
1767 | let member_name = CString::new(member_name).unwrap(); | |
1768 | unsafe { | |
1769 | llvm::LLVMDIBuilderCreateMemberType( | |
1770 | DIB(cx), | |
1771 | composite_type_metadata, | |
1772 | member_name.as_ptr(), | |
e9174d1e | 1773 | NO_FILE_METADATA, |
d9579d0f AL |
1774 | UNKNOWN_LINE_NUMBER, |
1775 | bytes_to_bits(member_size), | |
1776 | bytes_to_bits(member_align), | |
1777 | bytes_to_bits(member_offset), | |
1778 | member_description.flags, | |
1779 | member_description.type_metadata) | |
1a4d82fc | 1780 | } |
d9579d0f AL |
1781 | }) |
1782 | .collect(); | |
1a4d82fc | 1783 | |
d9579d0f AL |
1784 | unsafe { |
1785 | let type_array = create_DIArray(DIB(cx), &member_metadata[..]); | |
1786 | llvm::LLVMDICompositeTypeSetTypeArray(DIB(cx), composite_type_metadata, type_array); | |
1787 | } | |
1788 | } | |
1a4d82fc | 1789 | |
d9579d0f AL |
1790 | // A convenience wrapper around LLVMDIBuilderCreateStructType(). Does not do any |
1791 | // caching, does not add any fields to the struct. This can be done later with | |
1792 | // set_members_of_composite_type(). | |
1793 | fn create_struct_stub(cx: &CrateContext, | |
1794 | struct_llvm_type: Type, | |
1795 | struct_type_name: &str, | |
1796 | unique_type_id: UniqueTypeId, | |
1797 | containing_scope: DIScope) | |
1798 | -> DICompositeType { | |
1799 | let (struct_size, struct_align) = size_and_align_of(cx, struct_llvm_type); | |
1a4d82fc | 1800 | |
d9579d0f AL |
1801 | let unique_type_id_str = debug_context(cx).type_map |
1802 | .borrow() | |
1803 | .get_unique_type_id_as_string(unique_type_id); | |
1804 | let name = CString::new(struct_type_name).unwrap(); | |
1805 | let unique_type_id = CString::new(unique_type_id_str.as_bytes()).unwrap(); | |
1806 | let metadata_stub = unsafe { | |
1807 | // LLVMDIBuilderCreateStructType() wants an empty array. A null | |
1808 | // pointer will lead to hard to trace and debug LLVM assertions | |
1809 | // later on in llvm/lib/IR/Value.cpp. | |
1810 | let empty_array = create_DIArray(DIB(cx), &[]); | |
1a4d82fc | 1811 | |
d9579d0f AL |
1812 | llvm::LLVMDIBuilderCreateStructType( |
1813 | DIB(cx), | |
1814 | containing_scope, | |
1815 | name.as_ptr(), | |
e9174d1e | 1816 | NO_FILE_METADATA, |
d9579d0f AL |
1817 | UNKNOWN_LINE_NUMBER, |
1818 | bytes_to_bits(struct_size), | |
1819 | bytes_to_bits(struct_align), | |
1820 | 0, | |
1821 | ptr::null_mut(), | |
1822 | empty_array, | |
1823 | 0, | |
1824 | ptr::null_mut(), | |
1825 | unique_type_id.as_ptr()) | |
1826 | }; | |
1a4d82fc | 1827 | |
d9579d0f AL |
1828 | return metadata_stub; |
1829 | } | |
1a4d82fc | 1830 | |
d9579d0f AL |
1831 | /// Creates debug information for the given global variable. |
1832 | /// | |
1833 | /// Adds the created metadata nodes directly to the crate's IR. | |
1834 | pub fn create_global_var_metadata(cx: &CrateContext, | |
1835 | node_id: ast::NodeId, | |
1836 | global: ValueRef) { | |
1837 | if cx.dbg_cx().is_none() { | |
1838 | return; | |
1839 | } | |
1a4d82fc | 1840 | |
d9579d0f AL |
1841 | // Don't create debuginfo for globals inlined from other crates. The other |
1842 | // crate should already contain debuginfo for it. More importantly, the | |
1843 | // global might not even exist in un-inlined form anywhere which would lead | |
1844 | // to a linker errors. | |
1845 | if cx.external_srcs().borrow().contains_key(&node_id) { | |
1846 | return; | |
1847 | } | |
1a4d82fc | 1848 | |
d9579d0f | 1849 | let var_item = cx.tcx().map.get(node_id); |
1a4d82fc | 1850 | |
d9579d0f | 1851 | let (name, span) = match var_item { |
e9174d1e | 1852 | hir_map::NodeItem(item) => { |
d9579d0f | 1853 | match item.node { |
b039eaaf SL |
1854 | hir::ItemStatic(..) => (item.name, item.span), |
1855 | hir::ItemConst(..) => (item.name, item.span), | |
d9579d0f AL |
1856 | _ => { |
1857 | cx.sess() | |
1858 | .span_bug(item.span, | |
1859 | &format!("debuginfo::\ | |
1860 | create_global_var_metadata() - | |
1861 | Captured var-id refers to \ | |
1862 | unexpected ast_item variant: {:?}", | |
1863 | var_item)) | |
1a4d82fc JJ |
1864 | } |
1865 | } | |
d9579d0f AL |
1866 | }, |
1867 | _ => cx.sess().bug(&format!("debuginfo::create_global_var_metadata() \ | |
1868 | - Captured var-id refers to unexpected \ | |
e9174d1e | 1869 | hir_map variant: {:?}", |
d9579d0f AL |
1870 | var_item)) |
1871 | }; | |
1a4d82fc | 1872 | |
d9579d0f AL |
1873 | let (file_metadata, line_number) = if span != codemap::DUMMY_SP { |
1874 | let loc = span_start(cx, span); | |
1875 | (file_metadata(cx, &loc.file.name), loc.line as c_uint) | |
1876 | } else { | |
e9174d1e | 1877 | (NO_FILE_METADATA, UNKNOWN_LINE_NUMBER) |
d9579d0f | 1878 | }; |
1a4d82fc | 1879 | |
d9579d0f | 1880 | let is_local_to_unit = is_node_local_to_unit(cx, node_id); |
c1a9b12d | 1881 | let variable_type = cx.tcx().node_id_to_type(node_id); |
d9579d0f | 1882 | let type_metadata = type_metadata(cx, variable_type, span); |
b039eaaf SL |
1883 | let node_def_id = cx.tcx().map.local_def_id(node_id); |
1884 | let namespace_node = namespace_for_item(cx, node_def_id); | |
c1a9b12d | 1885 | let var_name = name.to_string(); |
d9579d0f AL |
1886 | let linkage_name = |
1887 | namespace_node.mangled_name_of_contained_item(&var_name[..]); | |
1888 | let var_scope = namespace_node.scope; | |
1a4d82fc | 1889 | |
d9579d0f AL |
1890 | let var_name = CString::new(var_name).unwrap(); |
1891 | let linkage_name = CString::new(linkage_name).unwrap(); | |
1892 | unsafe { | |
1893 | llvm::LLVMDIBuilderCreateStaticVariable(DIB(cx), | |
1894 | var_scope, | |
1895 | var_name.as_ptr(), | |
1896 | linkage_name.as_ptr(), | |
1897 | file_metadata, | |
1898 | line_number, | |
1899 | type_metadata, | |
1900 | is_local_to_unit, | |
1901 | global, | |
1902 | ptr::null_mut()); | |
1903 | } | |
1904 | } | |
1a4d82fc | 1905 | |
d9579d0f AL |
1906 | /// Creates debug information for the given local variable. |
1907 | /// | |
1908 | /// This function assumes that there's a datum for each pattern component of the | |
1909 | /// local in `bcx.fcx.lllocals`. | |
1910 | /// Adds the created metadata nodes directly to the crate's IR. | |
e9174d1e | 1911 | pub fn create_local_var_metadata(bcx: Block, local: &hir::Local) { |
d9579d0f AL |
1912 | if bcx.unreachable.get() || |
1913 | fn_should_be_ignored(bcx.fcx) || | |
1914 | bcx.sess().opts.debuginfo != FullDebugInfo { | |
1915 | return; | |
1a4d82fc | 1916 | } |
1a4d82fc | 1917 | |
d9579d0f AL |
1918 | let cx = bcx.ccx(); |
1919 | let def_map = &cx.tcx().def_map; | |
1920 | let locals = bcx.fcx.lllocals.borrow(); | |
1a4d82fc | 1921 | |
7453a54e | 1922 | pat_util::pat_bindings(def_map, &local.pat, |_, node_id, span, var_name| { |
d9579d0f AL |
1923 | let datum = match locals.get(&node_id) { |
1924 | Some(datum) => datum, | |
1925 | None => { | |
1926 | bcx.sess().span_bug(span, | |
1927 | &format!("no entry in lllocals table for {}", | |
1928 | node_id)); | |
1a4d82fc | 1929 | } |
d9579d0f | 1930 | }; |
1a4d82fc | 1931 | |
d9579d0f AL |
1932 | if unsafe { llvm::LLVMIsAAllocaInst(datum.val) } == ptr::null_mut() { |
1933 | cx.sess().span_bug(span, "debuginfo::create_local_var_metadata() - \ | |
1934 | Referenced variable location is not an alloca!"); | |
1935 | } | |
1a4d82fc | 1936 | |
d9579d0f | 1937 | let scope_metadata = scope_metadata(bcx.fcx, node_id, span); |
1a4d82fc | 1938 | |
d9579d0f | 1939 | declare_local(bcx, |
b039eaaf | 1940 | var_name.node, |
d9579d0f AL |
1941 | datum.ty, |
1942 | scope_metadata, | |
1943 | VariableAccess::DirectVariable { alloca: datum.val }, | |
1944 | VariableKind::LocalVariable, | |
1945 | span); | |
1946 | }) | |
1947 | } | |
1a4d82fc | 1948 | |
d9579d0f AL |
1949 | /// Creates debug information for a variable captured in a closure. |
1950 | /// | |
1951 | /// Adds the created metadata nodes directly to the crate's IR. | |
1952 | pub fn create_captured_var_metadata<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, | |
1953 | node_id: ast::NodeId, | |
1954 | env_pointer: ValueRef, | |
1955 | env_index: usize, | |
1956 | captured_by_ref: bool, | |
1957 | span: Span) { | |
1958 | if bcx.unreachable.get() || | |
1959 | fn_should_be_ignored(bcx.fcx) || | |
1960 | bcx.sess().opts.debuginfo != FullDebugInfo { | |
1961 | return; | |
1962 | } | |
1a4d82fc | 1963 | |
d9579d0f | 1964 | let cx = bcx.ccx(); |
1a4d82fc | 1965 | |
d9579d0f | 1966 | let ast_item = cx.tcx().map.find(node_id); |
1a4d82fc | 1967 | |
d9579d0f AL |
1968 | let variable_name = match ast_item { |
1969 | None => { | |
1970 | cx.sess().span_bug(span, "debuginfo::create_captured_var_metadata: node not found"); | |
1971 | } | |
b039eaaf | 1972 | Some(hir_map::NodeLocal(pat)) => { |
d9579d0f | 1973 | match pat.node { |
7453a54e | 1974 | PatKind::Ident(_, ref path1, _) => { |
d9579d0f | 1975 | path1.node.name |
1a4d82fc | 1976 | } |
d9579d0f AL |
1977 | _ => { |
1978 | cx.sess() | |
1979 | .span_bug(span, | |
1980 | &format!( | |
1981 | "debuginfo::create_captured_var_metadata() - \ | |
1982 | Captured var-id refers to unexpected \ | |
e9174d1e | 1983 | hir_map variant: {:?}", |
d9579d0f | 1984 | ast_item)); |
1a4d82fc JJ |
1985 | } |
1986 | } | |
1a4d82fc | 1987 | } |
d9579d0f AL |
1988 | _ => { |
1989 | cx.sess() | |
1990 | .span_bug(span, | |
1991 | &format!("debuginfo::create_captured_var_metadata() - \ | |
1992 | Captured var-id refers to unexpected \ | |
e9174d1e | 1993 | hir_map variant: {:?}", |
d9579d0f | 1994 | ast_item)); |
1a4d82fc | 1995 | } |
d9579d0f | 1996 | }; |
1a4d82fc | 1997 | |
d9579d0f AL |
1998 | let variable_type = common::node_id_type(bcx, node_id); |
1999 | let scope_metadata = bcx.fcx.debug_context.get_ref(cx, span).fn_metadata; | |
1a4d82fc | 2000 | |
d9579d0f AL |
2001 | // env_pointer is the alloca containing the pointer to the environment, |
2002 | // so it's type is **EnvironmentType. In order to find out the type of | |
2003 | // the environment we have to "dereference" two times. | |
2004 | let llvm_env_data_type = common::val_ty(env_pointer).element_type() | |
2005 | .element_type(); | |
2006 | let byte_offset_of_var_in_env = machine::llelement_offset(cx, | |
2007 | llvm_env_data_type, | |
2008 | env_index); | |
1a4d82fc | 2009 | |
d9579d0f AL |
2010 | let address_operations = unsafe { |
2011 | [llvm::LLVMDIBuilderCreateOpDeref(), | |
2012 | llvm::LLVMDIBuilderCreateOpPlus(), | |
2013 | byte_offset_of_var_in_env as i64, | |
2014 | llvm::LLVMDIBuilderCreateOpDeref()] | |
2015 | }; | |
1a4d82fc | 2016 | |
d9579d0f AL |
2017 | let address_op_count = if captured_by_ref { |
2018 | address_operations.len() | |
2019 | } else { | |
2020 | address_operations.len() - 1 | |
2021 | }; | |
1a4d82fc | 2022 | |
d9579d0f AL |
2023 | let variable_access = VariableAccess::IndirectVariable { |
2024 | alloca: env_pointer, | |
2025 | address_operations: &address_operations[..address_op_count] | |
2026 | }; | |
1a4d82fc | 2027 | |
d9579d0f AL |
2028 | declare_local(bcx, |
2029 | variable_name, | |
2030 | variable_type, | |
2031 | scope_metadata, | |
2032 | variable_access, | |
2033 | VariableKind::CapturedVariable, | |
2034 | span); | |
1a4d82fc JJ |
2035 | } |
2036 | ||
d9579d0f AL |
2037 | /// Creates debug information for a local variable introduced in the head of a |
2038 | /// match-statement arm. | |
2039 | /// | |
2040 | /// Adds the created metadata nodes directly to the crate's IR. | |
2041 | pub fn create_match_binding_metadata<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, | |
2042 | variable_name: ast::Name, | |
2043 | binding: BindingInfo<'tcx>) { | |
2044 | if bcx.unreachable.get() || | |
2045 | fn_should_be_ignored(bcx.fcx) || | |
2046 | bcx.sess().opts.debuginfo != FullDebugInfo { | |
2047 | return; | |
2048 | } | |
1a4d82fc | 2049 | |
d9579d0f AL |
2050 | let scope_metadata = scope_metadata(bcx.fcx, binding.id, binding.span); |
2051 | let aops = unsafe { | |
2052 | [llvm::LLVMDIBuilderCreateOpDeref()] | |
2053 | }; | |
2054 | // Regardless of the actual type (`T`) we're always passed the stack slot | |
2055 | // (alloca) for the binding. For ByRef bindings that's a `T*` but for ByMove | |
2056 | // bindings we actually have `T**`. So to get the actual variable we need to | |
2057 | // dereference once more. For ByCopy we just use the stack slot we created | |
2058 | // for the binding. | |
2059 | let var_access = match binding.trmode { | |
c1a9b12d SL |
2060 | TransBindingMode::TrByCopy(llbinding) | |
2061 | TransBindingMode::TrByMoveIntoCopy(llbinding) => VariableAccess::DirectVariable { | |
d9579d0f AL |
2062 | alloca: llbinding |
2063 | }, | |
c1a9b12d | 2064 | TransBindingMode::TrByMoveRef => VariableAccess::IndirectVariable { |
d9579d0f AL |
2065 | alloca: binding.llmatch, |
2066 | address_operations: &aops | |
2067 | }, | |
c1a9b12d | 2068 | TransBindingMode::TrByRef => VariableAccess::DirectVariable { |
d9579d0f | 2069 | alloca: binding.llmatch |
1a4d82fc | 2070 | } |
d9579d0f | 2071 | }; |
1a4d82fc | 2072 | |
d9579d0f AL |
2073 | declare_local(bcx, |
2074 | variable_name, | |
2075 | binding.ty, | |
2076 | scope_metadata, | |
2077 | var_access, | |
2078 | VariableKind::LocalVariable, | |
2079 | binding.span); | |
1a4d82fc JJ |
2080 | } |
2081 | ||
d9579d0f AL |
2082 | /// Creates debug information for the given function argument. |
2083 | /// | |
2084 | /// This function assumes that there's a datum for each pattern component of the | |
2085 | /// argument in `bcx.fcx.lllocals`. | |
2086 | /// Adds the created metadata nodes directly to the crate's IR. | |
e9174d1e | 2087 | pub fn create_argument_metadata(bcx: Block, arg: &hir::Arg) { |
d9579d0f AL |
2088 | if bcx.unreachable.get() || |
2089 | fn_should_be_ignored(bcx.fcx) || | |
2090 | bcx.sess().opts.debuginfo != FullDebugInfo { | |
2091 | return; | |
2092 | } | |
1a4d82fc | 2093 | |
d9579d0f AL |
2094 | let def_map = &bcx.tcx().def_map; |
2095 | let scope_metadata = bcx | |
2096 | .fcx | |
2097 | .debug_context | |
2098 | .get_ref(bcx.ccx(), arg.pat.span) | |
2099 | .fn_metadata; | |
2100 | let locals = bcx.fcx.lllocals.borrow(); | |
1a4d82fc | 2101 | |
7453a54e | 2102 | pat_util::pat_bindings(def_map, &arg.pat, |_, node_id, span, var_name| { |
d9579d0f AL |
2103 | let datum = match locals.get(&node_id) { |
2104 | Some(v) => v, | |
1a4d82fc | 2105 | None => { |
d9579d0f AL |
2106 | bcx.sess().span_bug(span, |
2107 | &format!("no entry in lllocals table for {}", | |
2108 | node_id)); | |
1a4d82fc | 2109 | } |
d9579d0f | 2110 | }; |
1a4d82fc | 2111 | |
d9579d0f AL |
2112 | if unsafe { llvm::LLVMIsAAllocaInst(datum.val) } == ptr::null_mut() { |
2113 | bcx.sess().span_bug(span, "debuginfo::create_argument_metadata() - \ | |
2114 | Referenced variable location is not an alloca!"); | |
1a4d82fc | 2115 | } |
1a4d82fc | 2116 | |
d9579d0f AL |
2117 | let argument_index = { |
2118 | let counter = &bcx | |
2119 | .fcx | |
2120 | .debug_context | |
2121 | .get_ref(bcx.ccx(), span) | |
2122 | .argument_counter; | |
2123 | let argument_index = counter.get(); | |
2124 | counter.set(argument_index + 1); | |
2125 | argument_index | |
2126 | }; | |
1a4d82fc | 2127 | |
d9579d0f | 2128 | declare_local(bcx, |
b039eaaf | 2129 | var_name.node, |
d9579d0f AL |
2130 | datum.ty, |
2131 | scope_metadata, | |
2132 | VariableAccess::DirectVariable { alloca: datum.val }, | |
2133 | VariableKind::ArgumentVariable(argument_index), | |
2134 | span); | |
2135 | }) | |
1a4d82fc | 2136 | } |