]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/debuginfo/metadata.rs
Skip checking for unused mutable locals that have no name
[rust.git] / src / librustc_trans / debuginfo / metadata.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 use self::RecursiveTypeDescription::*;
12 use self::MemberDescriptionFactory::*;
13 use self::EnumDiscriminantInfo::*;
14
15 use super::utils::{debug_context, DIB, span_start,
16                    get_namespace_for_item, create_DIArray, is_node_local_to_unit};
17 use super::namespace::mangled_name_of_instance;
18 use super::type_names::compute_debuginfo_type_name;
19 use super::{CrateDebugContext};
20 use abi;
21
22 use llvm::{self, ValueRef};
23 use llvm::debuginfo::{DIType, DIFile, DIScope, DIDescriptor,
24                       DICompositeType, DILexicalBlock, DIFlags};
25
26 use rustc::hir::TransFnAttrFlags;
27 use rustc::hir::def::CtorKind;
28 use rustc::hir::def_id::{DefId, CrateNum, LOCAL_CRATE};
29 use rustc::ty::fold::TypeVisitor;
30 use rustc::ty::util::TypeIdHasher;
31 use rustc::ich::Fingerprint;
32 use rustc::ty::Instance;
33 use common::CodegenCx;
34 use rustc::ty::{self, AdtKind, ParamEnv, Ty, TyCtxt};
35 use rustc::ty::layout::{self, Align, LayoutOf, PrimitiveExt, Size, TyLayout};
36 use rustc::session::config;
37 use rustc::util::nodemap::FxHashMap;
38 use rustc::util::common::path2cstr;
39
40 use libc::{c_uint, c_longlong};
41 use std::ffi::CString;
42 use std::fmt::Write;
43 use std::ptr;
44 use std::path::{Path, PathBuf};
45 use syntax::ast;
46 use syntax::symbol::{Interner, InternedString, Symbol};
47 use syntax_pos::{self, Span, FileName};
48
49
50 // From DWARF 5.
51 // See http://www.dwarfstd.org/ShowIssue.php?issue=140129.1
52 const DW_LANG_RUST: c_uint = 0x1c;
53 #[allow(non_upper_case_globals)]
54 const DW_ATE_boolean: c_uint = 0x02;
55 #[allow(non_upper_case_globals)]
56 const DW_ATE_float: c_uint = 0x04;
57 #[allow(non_upper_case_globals)]
58 const DW_ATE_signed: c_uint = 0x05;
59 #[allow(non_upper_case_globals)]
60 const DW_ATE_unsigned: c_uint = 0x07;
61 #[allow(non_upper_case_globals)]
62 const DW_ATE_unsigned_char: c_uint = 0x08;
63
64 pub const UNKNOWN_LINE_NUMBER: c_uint = 0;
65 pub const UNKNOWN_COLUMN_NUMBER: c_uint = 0;
66
67 // ptr::null() doesn't work :(
68 pub const NO_SCOPE_METADATA: DIScope = (0 as DIScope);
69
70 #[derive(Copy, Debug, Hash, Eq, PartialEq, Clone)]
71 pub struct UniqueTypeId(ast::Name);
72
73 // The TypeMap is where the CrateDebugContext holds the type metadata nodes
74 // created so far. The metadata nodes are indexed by UniqueTypeId, and, for
75 // faster lookup, also by Ty. The TypeMap is responsible for creating
76 // UniqueTypeIds.
77 pub struct TypeMap<'tcx> {
78     // The UniqueTypeIds created so far
79     unique_id_interner: Interner,
80     // A map from UniqueTypeId to debuginfo metadata for that type. This is a 1:1 mapping.
81     unique_id_to_metadata: FxHashMap<UniqueTypeId, DIType>,
82     // A map from types to debuginfo metadata. This is a N:1 mapping.
83     type_to_metadata: FxHashMap<Ty<'tcx>, DIType>,
84     // A map from types to UniqueTypeId. This is a N:1 mapping.
85     type_to_unique_id: FxHashMap<Ty<'tcx>, UniqueTypeId>
86 }
87
88 impl<'tcx> TypeMap<'tcx> {
89     pub fn new() -> TypeMap<'tcx> {
90         TypeMap {
91             unique_id_interner: Interner::new(),
92             type_to_metadata: FxHashMap(),
93             unique_id_to_metadata: FxHashMap(),
94             type_to_unique_id: FxHashMap(),
95         }
96     }
97
98     // Adds a Ty to metadata mapping to the TypeMap. The method will fail if
99     // the mapping already exists.
100     fn register_type_with_metadata<'a>(&mut self,
101                                        type_: Ty<'tcx>,
102                                        metadata: DIType) {
103         if self.type_to_metadata.insert(type_, metadata).is_some() {
104             bug!("Type metadata for Ty '{}' is already in the TypeMap!", type_);
105         }
106     }
107
108     // Adds a UniqueTypeId to metadata mapping to the TypeMap. The method will
109     // fail if the mapping already exists.
110     fn register_unique_id_with_metadata(&mut self,
111                                         unique_type_id: UniqueTypeId,
112                                         metadata: DIType) {
113         if self.unique_id_to_metadata.insert(unique_type_id, metadata).is_some() {
114             bug!("Type metadata for unique id '{}' is already in the TypeMap!",
115                  self.get_unique_type_id_as_string(unique_type_id));
116         }
117     }
118
119     fn find_metadata_for_type(&self, type_: Ty<'tcx>) -> Option<DIType> {
120         self.type_to_metadata.get(&type_).cloned()
121     }
122
123     fn find_metadata_for_unique_id(&self, unique_type_id: UniqueTypeId) -> Option<DIType> {
124         self.unique_id_to_metadata.get(&unique_type_id).cloned()
125     }
126
127     // Get the string representation of a UniqueTypeId. This method will fail if
128     // the id is unknown.
129     fn get_unique_type_id_as_string(&self, unique_type_id: UniqueTypeId) -> &str {
130         let UniqueTypeId(interner_key) = unique_type_id;
131         self.unique_id_interner.get(interner_key)
132     }
133
134     // Get the UniqueTypeId for the given type. If the UniqueTypeId for the given
135     // type has been requested before, this is just a table lookup. Otherwise an
136     // ID will be generated and stored for later lookup.
137     fn get_unique_type_id_of_type<'a>(&mut self, cx: &CodegenCx<'a, 'tcx>,
138                                       type_: Ty<'tcx>) -> UniqueTypeId {
139         // Let's see if we already have something in the cache
140         match self.type_to_unique_id.get(&type_).cloned() {
141             Some(unique_type_id) => return unique_type_id,
142             None => { /* generate one */}
143         };
144
145         // The hasher we are using to generate the UniqueTypeId. We want
146         // something that provides more than the 64 bits of the DefaultHasher.
147         let mut type_id_hasher = TypeIdHasher::<Fingerprint>::new(cx.tcx);
148         type_id_hasher.visit_ty(type_);
149         let unique_type_id = type_id_hasher.finish().to_hex();
150
151         let key = self.unique_id_interner.intern(&unique_type_id);
152         self.type_to_unique_id.insert(type_, UniqueTypeId(key));
153
154         return UniqueTypeId(key);
155     }
156
157     // Get the UniqueTypeId for an enum variant. Enum variants are not really
158     // types of their own, so they need special handling. We still need a
159     // UniqueTypeId for them, since to debuginfo they *are* real types.
160     fn get_unique_type_id_of_enum_variant<'a>(&mut self,
161                                               cx: &CodegenCx<'a, 'tcx>,
162                                               enum_type: Ty<'tcx>,
163                                               variant_name: &str)
164                                               -> UniqueTypeId {
165         let enum_type_id = self.get_unique_type_id_of_type(cx, enum_type);
166         let enum_variant_type_id = format!("{}::{}",
167                                            self.get_unique_type_id_as_string(enum_type_id),
168                                            variant_name);
169         let interner_key = self.unique_id_interner.intern(&enum_variant_type_id);
170         UniqueTypeId(interner_key)
171     }
172 }
173
174 // A description of some recursive type. It can either be already finished (as
175 // with FinalMetadata) or it is not yet finished, but contains all information
176 // needed to generate the missing parts of the description. See the
177 // documentation section on Recursive Types at the top of this file for more
178 // information.
179 enum RecursiveTypeDescription<'tcx> {
180     UnfinishedMetadata {
181         unfinished_type: Ty<'tcx>,
182         unique_type_id: UniqueTypeId,
183         metadata_stub: DICompositeType,
184         member_description_factory: MemberDescriptionFactory<'tcx>,
185     },
186     FinalMetadata(DICompositeType)
187 }
188
189 fn create_and_register_recursive_type_forward_declaration<'a, 'tcx>(
190     cx: &CodegenCx<'a, 'tcx>,
191     unfinished_type: Ty<'tcx>,
192     unique_type_id: UniqueTypeId,
193     metadata_stub: DICompositeType,
194     member_description_factory: MemberDescriptionFactory<'tcx>)
195  -> RecursiveTypeDescription<'tcx> {
196
197     // Insert the stub into the TypeMap in order to allow for recursive references
198     let mut type_map = debug_context(cx).type_map.borrow_mut();
199     type_map.register_unique_id_with_metadata(unique_type_id, metadata_stub);
200     type_map.register_type_with_metadata(unfinished_type, metadata_stub);
201
202     UnfinishedMetadata {
203         unfinished_type,
204         unique_type_id,
205         metadata_stub,
206         member_description_factory,
207     }
208 }
209
210 impl<'tcx> RecursiveTypeDescription<'tcx> {
211     // Finishes up the description of the type in question (mostly by providing
212     // descriptions of the fields of the given type) and returns the final type
213     // metadata.
214     fn finalize<'a>(&self, cx: &CodegenCx<'a, 'tcx>) -> MetadataCreationResult {
215         match *self {
216             FinalMetadata(metadata) => MetadataCreationResult::new(metadata, false),
217             UnfinishedMetadata {
218                 unfinished_type,
219                 unique_type_id,
220                 metadata_stub,
221                 ref member_description_factory,
222             } => {
223                 // Make sure that we have a forward declaration of the type in
224                 // the TypeMap so that recursive references are possible. This
225                 // will always be the case if the RecursiveTypeDescription has
226                 // been properly created through the
227                 // create_and_register_recursive_type_forward_declaration()
228                 // function.
229                 {
230                     let type_map = debug_context(cx).type_map.borrow();
231                     if type_map.find_metadata_for_unique_id(unique_type_id).is_none() ||
232                        type_map.find_metadata_for_type(unfinished_type).is_none() {
233                         bug!("Forward declaration of potentially recursive type \
234                               '{:?}' was not found in TypeMap!",
235                              unfinished_type);
236                     }
237                 }
238
239                 // ... then create the member descriptions ...
240                 let member_descriptions =
241                     member_description_factory.create_member_descriptions(cx);
242
243                 // ... and attach them to the stub to complete it.
244                 set_members_of_composite_type(cx,
245                                               metadata_stub,
246                                               &member_descriptions[..]);
247                 return MetadataCreationResult::new(metadata_stub, true);
248             }
249         }
250     }
251 }
252
253 // Returns from the enclosing function if the type metadata with the given
254 // unique id can be found in the type map
255 macro_rules! return_if_metadata_created_in_meantime {
256     ($cx: expr, $unique_type_id: expr) => (
257         match debug_context($cx).type_map
258                                 .borrow()
259                                 .find_metadata_for_unique_id($unique_type_id) {
260             Some(metadata) => return MetadataCreationResult::new(metadata, true),
261             None => { /* proceed normally */ }
262         }
263     )
264 }
265
266 fn fixed_vec_metadata<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
267                                 unique_type_id: UniqueTypeId,
268                                 array_or_slice_type: Ty<'tcx>,
269                                 element_type: Ty<'tcx>,
270                                 span: Span)
271                                 -> MetadataCreationResult {
272     let element_type_metadata = type_metadata(cx, element_type, span);
273
274     return_if_metadata_created_in_meantime!(cx, unique_type_id);
275
276     let (size, align) = cx.size_and_align_of(array_or_slice_type);
277
278     let upper_bound = match array_or_slice_type.sty {
279         ty::TyArray(_, len) => {
280             len.val.unwrap_u64() as c_longlong
281         }
282         _ => -1
283     };
284
285     let subrange = unsafe {
286         llvm::LLVMRustDIBuilderGetOrCreateSubrange(DIB(cx), 0, upper_bound)
287     };
288
289     let subscripts = create_DIArray(DIB(cx), &[subrange]);
290     let metadata = unsafe {
291         llvm::LLVMRustDIBuilderCreateArrayType(
292             DIB(cx),
293             size.bits(),
294             align.abi_bits() as u32,
295             element_type_metadata,
296             subscripts)
297     };
298
299     return MetadataCreationResult::new(metadata, false);
300 }
301
302 fn vec_slice_metadata<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
303                                 slice_ptr_type: Ty<'tcx>,
304                                 element_type: Ty<'tcx>,
305                                 unique_type_id: UniqueTypeId,
306                                 span: Span)
307                                 -> MetadataCreationResult {
308     let data_ptr_type = cx.tcx.mk_imm_ptr(element_type);
309
310     let data_ptr_metadata = type_metadata(cx, data_ptr_type, span);
311
312     return_if_metadata_created_in_meantime!(cx, unique_type_id);
313
314     let slice_type_name = compute_debuginfo_type_name(cx, slice_ptr_type, true);
315
316     let (pointer_size, pointer_align) = cx.size_and_align_of(data_ptr_type);
317     let (usize_size, usize_align) = cx.size_and_align_of(cx.tcx.types.usize);
318
319     let member_descriptions = [
320         MemberDescription {
321             name: "data_ptr".to_string(),
322             type_metadata: data_ptr_metadata,
323             offset: Size::from_bytes(0),
324             size: pointer_size,
325             align: pointer_align,
326             flags: DIFlags::FlagZero,
327         },
328         MemberDescription {
329             name: "length".to_string(),
330             type_metadata: type_metadata(cx, cx.tcx.types.usize, span),
331             offset: pointer_size,
332             size: usize_size,
333             align: usize_align,
334             flags: DIFlags::FlagZero,
335         },
336     ];
337
338     let file_metadata = unknown_file_metadata(cx);
339
340     let metadata = composite_type_metadata(cx,
341                                            slice_ptr_type,
342                                            &slice_type_name[..],
343                                            unique_type_id,
344                                            &member_descriptions,
345                                            NO_SCOPE_METADATA,
346                                            file_metadata,
347                                            span);
348     MetadataCreationResult::new(metadata, false)
349 }
350
351 fn subroutine_type_metadata<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
352                                       unique_type_id: UniqueTypeId,
353                                       signature: ty::PolyFnSig<'tcx>,
354                                       span: Span)
355                                       -> MetadataCreationResult
356 {
357     let signature = cx.tcx.normalize_erasing_late_bound_regions(
358         ty::ParamEnv::reveal_all(),
359         &signature,
360     );
361
362     let mut signature_metadata: Vec<DIType> = Vec::with_capacity(signature.inputs().len() + 1);
363
364     // return type
365     signature_metadata.push(match signature.output().sty {
366         ty::TyTuple(ref tys) if tys.is_empty() => ptr::null_mut(),
367         _ => type_metadata(cx, signature.output(), span)
368     });
369
370     // regular arguments
371     for &argument_type in signature.inputs() {
372         signature_metadata.push(type_metadata(cx, argument_type, span));
373     }
374
375     return_if_metadata_created_in_meantime!(cx, unique_type_id);
376
377     return MetadataCreationResult::new(
378         unsafe {
379             llvm::LLVMRustDIBuilderCreateSubroutineType(
380                 DIB(cx),
381                 unknown_file_metadata(cx),
382                 create_DIArray(DIB(cx), &signature_metadata[..]))
383         },
384         false);
385 }
386
387 // FIXME(1563) This is all a bit of a hack because 'trait pointer' is an ill-
388 // defined concept. For the case of an actual trait pointer (i.e., Box<Trait>,
389 // &Trait), trait_object_type should be the whole thing (e.g, Box<Trait>) and
390 // trait_type should be the actual trait (e.g., Trait). Where the trait is part
391 // of a DST struct, there is no trait_object_type and the results of this
392 // function will be a little bit weird.
393 fn trait_pointer_metadata<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
394                                     trait_type: Ty<'tcx>,
395                                     trait_object_type: Option<Ty<'tcx>>,
396                                     unique_type_id: UniqueTypeId)
397                                     -> DIType {
398     // The implementation provided here is a stub. It makes sure that the trait
399     // type is assigned the correct name, size, namespace, and source location.
400     // But it does not describe the trait's methods.
401
402     let containing_scope = match trait_type.sty {
403         ty::TyDynamic(ref data, ..) => if let Some(principal) = data.principal() {
404             let def_id = principal.def_id();
405             get_namespace_for_item(cx, def_id)
406         } else {
407             NO_SCOPE_METADATA
408         },
409         _ => {
410             bug!("debuginfo: Unexpected trait-object type in \
411                   trait_pointer_metadata(): {:?}",
412                  trait_type);
413         }
414     };
415
416     let trait_object_type = trait_object_type.unwrap_or(trait_type);
417     let trait_type_name =
418         compute_debuginfo_type_name(cx, trait_object_type, false);
419
420     let file_metadata = unknown_file_metadata(cx);
421
422     let layout = cx.layout_of(cx.tcx.mk_mut_ptr(trait_type));
423
424     assert_eq!(abi::FAT_PTR_ADDR, 0);
425     assert_eq!(abi::FAT_PTR_EXTRA, 1);
426
427     let data_ptr_field = layout.field(cx, 0);
428     let vtable_field = layout.field(cx, 1);
429     let member_descriptions = [
430         MemberDescription {
431             name: "pointer".to_string(),
432             type_metadata: type_metadata(cx,
433                 cx.tcx.mk_mut_ptr(cx.tcx.types.u8),
434                 syntax_pos::DUMMY_SP),
435             offset: layout.fields.offset(0),
436             size: data_ptr_field.size,
437             align: data_ptr_field.align,
438             flags: DIFlags::FlagArtificial,
439         },
440         MemberDescription {
441             name: "vtable".to_string(),
442             type_metadata: type_metadata(cx, vtable_field.ty, syntax_pos::DUMMY_SP),
443             offset: layout.fields.offset(1),
444             size: vtable_field.size,
445             align: vtable_field.align,
446             flags: DIFlags::FlagArtificial,
447         },
448     ];
449
450     composite_type_metadata(cx,
451                             trait_object_type,
452                             &trait_type_name[..],
453                             unique_type_id,
454                             &member_descriptions,
455                             containing_scope,
456                             file_metadata,
457                             syntax_pos::DUMMY_SP)
458 }
459
460 pub fn type_metadata<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
461                                t: Ty<'tcx>,
462                                usage_site_span: Span)
463                                -> DIType {
464     // Get the unique type id of this type.
465     let unique_type_id = {
466         let mut type_map = debug_context(cx).type_map.borrow_mut();
467         // First, try to find the type in TypeMap. If we have seen it before, we
468         // can exit early here.
469         match type_map.find_metadata_for_type(t) {
470             Some(metadata) => {
471                 return metadata;
472             },
473             None => {
474                 // The Ty is not in the TypeMap but maybe we have already seen
475                 // an equivalent type (e.g. only differing in region arguments).
476                 // In order to find out, generate the unique type id and look
477                 // that up.
478                 let unique_type_id = type_map.get_unique_type_id_of_type(cx, t);
479                 match type_map.find_metadata_for_unique_id(unique_type_id) {
480                     Some(metadata) => {
481                         // There is already an equivalent type in the TypeMap.
482                         // Register this Ty as an alias in the cache and
483                         // return the cached metadata.
484                         type_map.register_type_with_metadata(t, metadata);
485                         return metadata;
486                     },
487                     None => {
488                         // There really is no type metadata for this type, so
489                         // proceed by creating it.
490                         unique_type_id
491                     }
492                 }
493             }
494         }
495     };
496
497     debug!("type_metadata: {:?}", t);
498
499     let ptr_metadata = |ty: Ty<'tcx>| {
500         match ty.sty {
501             ty::TySlice(typ) => {
502                 Ok(vec_slice_metadata(cx, t, typ, unique_type_id, usage_site_span))
503             }
504             ty::TyStr => {
505                 Ok(vec_slice_metadata(cx, t, cx.tcx.types.u8, unique_type_id, usage_site_span))
506             }
507             ty::TyDynamic(..) => {
508                 Ok(MetadataCreationResult::new(
509                     trait_pointer_metadata(cx, ty, Some(t), unique_type_id),
510                     false))
511             }
512             _ => {
513                 let pointee_metadata = type_metadata(cx, ty, usage_site_span);
514
515                 match debug_context(cx).type_map
516                                         .borrow()
517                                         .find_metadata_for_unique_id(unique_type_id) {
518                     Some(metadata) => return Err(metadata),
519                     None => { /* proceed normally */ }
520                 };
521
522                 Ok(MetadataCreationResult::new(pointer_type_metadata(cx, t, pointee_metadata),
523                    false))
524             }
525         }
526     };
527
528     let MetadataCreationResult { metadata, already_stored_in_typemap } = match t.sty {
529         ty::TyNever    |
530         ty::TyBool     |
531         ty::TyChar     |
532         ty::TyInt(_)   |
533         ty::TyUint(_)  |
534         ty::TyFloat(_) => {
535             MetadataCreationResult::new(basic_type_metadata(cx, t), false)
536         }
537         ty::TyTuple(ref elements) if elements.is_empty() => {
538             MetadataCreationResult::new(basic_type_metadata(cx, t), false)
539         }
540         ty::TyArray(typ, _) |
541         ty::TySlice(typ) => {
542             fixed_vec_metadata(cx, unique_type_id, t, typ, usage_site_span)
543         }
544         ty::TyStr => {
545             fixed_vec_metadata(cx, unique_type_id, t, cx.tcx.types.i8, usage_site_span)
546         }
547         ty::TyDynamic(..) => {
548             MetadataCreationResult::new(
549                         trait_pointer_metadata(cx, t, None, unique_type_id),
550             false)
551         }
552         ty::TyForeign(..) => {
553             MetadataCreationResult::new(
554                         foreign_type_metadata(cx, t, unique_type_id),
555             false)
556         }
557         ty::TyRawPtr(ty::TypeAndMut{ty, ..}) |
558         ty::TyRef(_, ty::TypeAndMut{ty, ..}) => {
559             match ptr_metadata(ty) {
560                 Ok(res) => res,
561                 Err(metadata) => return metadata,
562             }
563         }
564         ty::TyAdt(def, _) if def.is_box() => {
565             match ptr_metadata(t.boxed_ty()) {
566                 Ok(res) => res,
567                 Err(metadata) => return metadata,
568             }
569         }
570         ty::TyFnDef(..) | ty::TyFnPtr(_) => {
571             let fn_metadata = subroutine_type_metadata(cx,
572                                                        unique_type_id,
573                                                        t.fn_sig(cx.tcx),
574                                                        usage_site_span).metadata;
575             match debug_context(cx).type_map
576                                    .borrow()
577                                    .find_metadata_for_unique_id(unique_type_id) {
578                 Some(metadata) => return metadata,
579                 None => { /* proceed normally */ }
580             };
581
582             // This is actually a function pointer, so wrap it in pointer DI
583             MetadataCreationResult::new(pointer_type_metadata(cx, t, fn_metadata), false)
584
585         }
586         ty::TyClosure(def_id, substs) => {
587             let upvar_tys : Vec<_> = substs.upvar_tys(def_id, cx.tcx).collect();
588             prepare_tuple_metadata(cx,
589                                    t,
590                                    &upvar_tys,
591                                    unique_type_id,
592                                    usage_site_span).finalize(cx)
593         }
594         ty::TyGenerator(def_id, substs, _) => {
595             let upvar_tys : Vec<_> = substs.field_tys(def_id, cx.tcx).map(|t| {
596                 cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), t)
597             }).collect();
598             prepare_tuple_metadata(cx,
599                                    t,
600                                    &upvar_tys,
601                                    unique_type_id,
602                                    usage_site_span).finalize(cx)
603         }
604         ty::TyAdt(def, ..) => match def.adt_kind() {
605             AdtKind::Struct => {
606                 prepare_struct_metadata(cx,
607                                         t,
608                                         unique_type_id,
609                                         usage_site_span).finalize(cx)
610             }
611             AdtKind::Union => {
612                 prepare_union_metadata(cx,
613                                     t,
614                                     unique_type_id,
615                                     usage_site_span).finalize(cx)
616             }
617             AdtKind::Enum => {
618                 prepare_enum_metadata(cx,
619                                     t,
620                                     def.did,
621                                     unique_type_id,
622                                     usage_site_span).finalize(cx)
623             }
624         },
625         ty::TyTuple(ref elements) => {
626             prepare_tuple_metadata(cx,
627                                    t,
628                                    &elements[..],
629                                    unique_type_id,
630                                    usage_site_span).finalize(cx)
631         }
632         _ => {
633             bug!("debuginfo: unexpected type in type_metadata: {:?}", t)
634         }
635     };
636
637     {
638         let mut type_map = debug_context(cx).type_map.borrow_mut();
639
640         if already_stored_in_typemap {
641             // Also make sure that we already have a TypeMap entry for the unique type id.
642             let metadata_for_uid = match type_map.find_metadata_for_unique_id(unique_type_id) {
643                 Some(metadata) => metadata,
644                 None => {
645                     span_bug!(usage_site_span,
646                               "Expected type metadata for unique \
647                                type id '{}' to already be in \
648                                the debuginfo::TypeMap but it \
649                                was not. (Ty = {})",
650                               type_map.get_unique_type_id_as_string(unique_type_id),
651                               t);
652                 }
653             };
654
655             match type_map.find_metadata_for_type(t) {
656                 Some(metadata) => {
657                     if metadata != metadata_for_uid {
658                         span_bug!(usage_site_span,
659                                   "Mismatch between Ty and \
660                                    UniqueTypeId maps in \
661                                    debuginfo::TypeMap. \
662                                    UniqueTypeId={}, Ty={}",
663                                   type_map.get_unique_type_id_as_string(unique_type_id),
664                                   t);
665                     }
666                 }
667                 None => {
668                     type_map.register_type_with_metadata(t, metadata);
669                 }
670             }
671         } else {
672             type_map.register_type_with_metadata(t, metadata);
673             type_map.register_unique_id_with_metadata(unique_type_id, metadata);
674         }
675     }
676
677     metadata
678 }
679
680 pub fn file_metadata(cx: &CodegenCx,
681                      file_name: &FileName,
682                      defining_crate: CrateNum) -> DIFile {
683     debug!("file_metadata: file_name: {}, defining_crate: {}",
684            file_name,
685            defining_crate);
686
687     let directory = if defining_crate == LOCAL_CRATE {
688         &cx.sess().working_dir.0
689     } else {
690         // If the path comes from an upstream crate we assume it has been made
691         // independent of the compiler's working directory one way or another.
692         Path::new("")
693     };
694
695     file_metadata_raw(cx, &file_name.to_string(), &directory.to_string_lossy())
696 }
697
698 pub fn unknown_file_metadata(cx: &CodegenCx) -> DIFile {
699     file_metadata_raw(cx, "<unknown>", "")
700 }
701
702 fn file_metadata_raw(cx: &CodegenCx,
703                      file_name: &str,
704                      directory: &str)
705                      -> DIFile {
706     let key = (Symbol::intern(file_name), Symbol::intern(directory));
707
708     if let Some(file_metadata) = debug_context(cx).created_files.borrow().get(&key) {
709         return *file_metadata;
710     }
711
712     debug!("file_metadata: file_name: {}, directory: {}", file_name, directory);
713
714     let file_name = CString::new(file_name).unwrap();
715     let directory = CString::new(directory).unwrap();
716
717     let file_metadata = unsafe {
718         llvm::LLVMRustDIBuilderCreateFile(DIB(cx),
719                                           file_name.as_ptr(),
720                                           directory.as_ptr())
721     };
722
723     let mut created_files = debug_context(cx).created_files.borrow_mut();
724     created_files.insert(key, file_metadata);
725     file_metadata
726 }
727
728 fn basic_type_metadata<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
729                                  t: Ty<'tcx>) -> DIType {
730
731     debug!("basic_type_metadata: {:?}", t);
732
733     let (name, encoding) = match t.sty {
734         ty::TyNever => ("!", DW_ATE_unsigned),
735         ty::TyTuple(ref elements) if elements.is_empty() =>
736             ("()", DW_ATE_unsigned),
737         ty::TyBool => ("bool", DW_ATE_boolean),
738         ty::TyChar => ("char", DW_ATE_unsigned_char),
739         ty::TyInt(int_ty) => {
740             (int_ty.ty_to_string(), DW_ATE_signed)
741         },
742         ty::TyUint(uint_ty) => {
743             (uint_ty.ty_to_string(), DW_ATE_unsigned)
744         },
745         ty::TyFloat(float_ty) => {
746             (float_ty.ty_to_string(), DW_ATE_float)
747         },
748         _ => bug!("debuginfo::basic_type_metadata - t is invalid type")
749     };
750
751     let (size, align) = cx.size_and_align_of(t);
752     let name = CString::new(name).unwrap();
753     let ty_metadata = unsafe {
754         llvm::LLVMRustDIBuilderCreateBasicType(
755             DIB(cx),
756             name.as_ptr(),
757             size.bits(),
758             align.abi_bits() as u32,
759             encoding)
760     };
761
762     return ty_metadata;
763 }
764
765 fn foreign_type_metadata<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
766                                    t: Ty<'tcx>,
767                                    unique_type_id: UniqueTypeId) -> DIType {
768     debug!("foreign_type_metadata: {:?}", t);
769
770     let name = compute_debuginfo_type_name(cx, t, false);
771     create_struct_stub(cx, t, &name, unique_type_id, NO_SCOPE_METADATA)
772 }
773
774 fn pointer_type_metadata<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
775                                    pointer_type: Ty<'tcx>,
776                                    pointee_type_metadata: DIType)
777                                    -> DIType {
778     let (pointer_size, pointer_align) = cx.size_and_align_of(pointer_type);
779     let name = compute_debuginfo_type_name(cx, pointer_type, false);
780     let name = CString::new(name).unwrap();
781     unsafe {
782         llvm::LLVMRustDIBuilderCreatePointerType(
783             DIB(cx),
784             pointee_type_metadata,
785             pointer_size.bits(),
786             pointer_align.abi_bits() as u32,
787             name.as_ptr())
788     }
789 }
790
791 pub fn compile_unit_metadata(tcx: TyCtxt,
792                              codegen_unit_name: &str,
793                              debug_context: &CrateDebugContext)
794                              -> DIDescriptor {
795     let mut name_in_debuginfo = match tcx.sess.local_crate_source_file {
796         Some(ref path) => path.clone(),
797         None => PathBuf::from(&*tcx.crate_name(LOCAL_CRATE).as_str()),
798     };
799
800     // The OSX linker has an idiosyncrasy where it will ignore some debuginfo
801     // if multiple object files with the same DW_AT_name are linked together.
802     // As a workaround we generate unique names for each object file. Those do
803     // not correspond to an actual source file but that should be harmless.
804     if tcx.sess.target.target.options.is_like_osx {
805         name_in_debuginfo.push("@");
806         name_in_debuginfo.push(codegen_unit_name);
807     }
808
809     debug!("compile_unit_metadata: {:?}", name_in_debuginfo);
810     // FIXME(#41252) Remove "clang LLVM" if we can get GDB and LLVM to play nice.
811     let producer = format!("clang LLVM (rustc version {})",
812                            (option_env!("CFG_VERSION")).expect("CFG_VERSION"));
813
814     let name_in_debuginfo = name_in_debuginfo.to_string_lossy().into_owned();
815     let name_in_debuginfo = CString::new(name_in_debuginfo).unwrap();
816     let work_dir = CString::new(&tcx.sess.working_dir.0.to_string_lossy()[..]).unwrap();
817     let producer = CString::new(producer).unwrap();
818     let flags = "\0";
819     let split_name = "\0";
820
821     unsafe {
822         let file_metadata = llvm::LLVMRustDIBuilderCreateFile(
823             debug_context.builder, name_in_debuginfo.as_ptr(), work_dir.as_ptr());
824
825         let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit(
826             debug_context.builder,
827             DW_LANG_RUST,
828             file_metadata,
829             producer.as_ptr(),
830             tcx.sess.opts.optimize != config::OptLevel::No,
831             flags.as_ptr() as *const _,
832             0,
833             split_name.as_ptr() as *const _);
834
835         if tcx.sess.opts.debugging_opts.profile {
836             let cu_desc_metadata = llvm::LLVMRustMetadataAsValue(debug_context.llcontext,
837                                                                  unit_metadata);
838
839             let gcov_cu_info = [
840                 path_to_mdstring(debug_context.llcontext,
841                                  &tcx.output_filenames(LOCAL_CRATE).with_extension("gcno")),
842                 path_to_mdstring(debug_context.llcontext,
843                                  &tcx.output_filenames(LOCAL_CRATE).with_extension("gcda")),
844                 cu_desc_metadata,
845             ];
846             let gcov_metadata = llvm::LLVMMDNodeInContext(debug_context.llcontext,
847                                                           gcov_cu_info.as_ptr(),
848                                                           gcov_cu_info.len() as c_uint);
849
850             let llvm_gcov_ident = CString::new("llvm.gcov").unwrap();
851             llvm::LLVMAddNamedMetadataOperand(debug_context.llmod,
852                                               llvm_gcov_ident.as_ptr(),
853                                               gcov_metadata);
854         }
855
856         return unit_metadata;
857     };
858
859     fn path_to_mdstring(llcx: llvm::ContextRef, path: &Path) -> llvm::ValueRef {
860         let path_str = path2cstr(path);
861         unsafe {
862             llvm::LLVMMDStringInContext(llcx,
863                                         path_str.as_ptr(),
864                                         path_str.as_bytes().len() as c_uint)
865         }
866     }
867 }
868
869 struct MetadataCreationResult {
870     metadata: DIType,
871     already_stored_in_typemap: bool
872 }
873
874 impl MetadataCreationResult {
875     fn new(metadata: DIType, already_stored_in_typemap: bool) -> MetadataCreationResult {
876         MetadataCreationResult {
877             metadata,
878             already_stored_in_typemap,
879         }
880     }
881 }
882
883 // Description of a type member, which can either be a regular field (as in
884 // structs or tuples) or an enum variant.
885 #[derive(Debug)]
886 struct MemberDescription {
887     name: String,
888     type_metadata: DIType,
889     offset: Size,
890     size: Size,
891     align: Align,
892     flags: DIFlags,
893 }
894
895 // A factory for MemberDescriptions. It produces a list of member descriptions
896 // for some record-like type. MemberDescriptionFactories are used to defer the
897 // creation of type member descriptions in order to break cycles arising from
898 // recursive type definitions.
899 enum MemberDescriptionFactory<'tcx> {
900     StructMDF(StructMemberDescriptionFactory<'tcx>),
901     TupleMDF(TupleMemberDescriptionFactory<'tcx>),
902     EnumMDF(EnumMemberDescriptionFactory<'tcx>),
903     UnionMDF(UnionMemberDescriptionFactory<'tcx>),
904     VariantMDF(VariantMemberDescriptionFactory<'tcx>)
905 }
906
907 impl<'tcx> MemberDescriptionFactory<'tcx> {
908     fn create_member_descriptions<'a>(&self, cx: &CodegenCx<'a, 'tcx>)
909                                       -> Vec<MemberDescription> {
910         match *self {
911             StructMDF(ref this) => {
912                 this.create_member_descriptions(cx)
913             }
914             TupleMDF(ref this) => {
915                 this.create_member_descriptions(cx)
916             }
917             EnumMDF(ref this) => {
918                 this.create_member_descriptions(cx)
919             }
920             UnionMDF(ref this) => {
921                 this.create_member_descriptions(cx)
922             }
923             VariantMDF(ref this) => {
924                 this.create_member_descriptions(cx)
925             }
926         }
927     }
928 }
929
930 //=-----------------------------------------------------------------------------
931 // Structs
932 //=-----------------------------------------------------------------------------
933
934 // Creates MemberDescriptions for the fields of a struct
935 struct StructMemberDescriptionFactory<'tcx> {
936     ty: Ty<'tcx>,
937     variant: &'tcx ty::VariantDef,
938     span: Span,
939 }
940
941 impl<'tcx> StructMemberDescriptionFactory<'tcx> {
942     fn create_member_descriptions<'a>(&self, cx: &CodegenCx<'a, 'tcx>)
943                                       -> Vec<MemberDescription> {
944         let layout = cx.layout_of(self.ty);
945         self.variant.fields.iter().enumerate().map(|(i, f)| {
946             let name = if self.variant.ctor_kind == CtorKind::Fn {
947                 format!("__{}", i)
948             } else {
949                 f.name.to_string()
950             };
951             let field = layout.field(cx, i);
952             let (size, align) = field.size_and_align();
953             MemberDescription {
954                 name,
955                 type_metadata: type_metadata(cx, field.ty, self.span),
956                 offset: layout.fields.offset(i),
957                 size,
958                 align,
959                 flags: DIFlags::FlagZero,
960             }
961         }).collect()
962     }
963 }
964
965
966 fn prepare_struct_metadata<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
967                                      struct_type: Ty<'tcx>,
968                                      unique_type_id: UniqueTypeId,
969                                      span: Span)
970                                      -> RecursiveTypeDescription<'tcx> {
971     let struct_name = compute_debuginfo_type_name(cx, struct_type, false);
972
973     let (struct_def_id, variant) = match struct_type.sty {
974         ty::TyAdt(def, _) => (def.did, def.non_enum_variant()),
975         _ => bug!("prepare_struct_metadata on a non-ADT")
976     };
977
978     let containing_scope = get_namespace_for_item(cx, struct_def_id);
979
980     let struct_metadata_stub = create_struct_stub(cx,
981                                                   struct_type,
982                                                   &struct_name,
983                                                   unique_type_id,
984                                                   containing_scope);
985
986     create_and_register_recursive_type_forward_declaration(
987         cx,
988         struct_type,
989         unique_type_id,
990         struct_metadata_stub,
991         StructMDF(StructMemberDescriptionFactory {
992             ty: struct_type,
993             variant,
994             span,
995         })
996     )
997 }
998
999 //=-----------------------------------------------------------------------------
1000 // Tuples
1001 //=-----------------------------------------------------------------------------
1002
1003 // Creates MemberDescriptions for the fields of a tuple
1004 struct TupleMemberDescriptionFactory<'tcx> {
1005     ty: Ty<'tcx>,
1006     component_types: Vec<Ty<'tcx>>,
1007     span: Span,
1008 }
1009
1010 impl<'tcx> TupleMemberDescriptionFactory<'tcx> {
1011     fn create_member_descriptions<'a>(&self, cx: &CodegenCx<'a, 'tcx>)
1012                                       -> Vec<MemberDescription> {
1013         let layout = cx.layout_of(self.ty);
1014         self.component_types.iter().enumerate().map(|(i, &component_type)| {
1015             let (size, align) = cx.size_and_align_of(component_type);
1016             MemberDescription {
1017                 name: format!("__{}", i),
1018                 type_metadata: type_metadata(cx, component_type, self.span),
1019                 offset: layout.fields.offset(i),
1020                 size,
1021                 align,
1022                 flags: DIFlags::FlagZero,
1023             }
1024         }).collect()
1025     }
1026 }
1027
1028 fn prepare_tuple_metadata<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
1029                                     tuple_type: Ty<'tcx>,
1030                                     component_types: &[Ty<'tcx>],
1031                                     unique_type_id: UniqueTypeId,
1032                                     span: Span)
1033                                     -> RecursiveTypeDescription<'tcx> {
1034     let tuple_name = compute_debuginfo_type_name(cx, tuple_type, false);
1035
1036     create_and_register_recursive_type_forward_declaration(
1037         cx,
1038         tuple_type,
1039         unique_type_id,
1040         create_struct_stub(cx,
1041                            tuple_type,
1042                            &tuple_name[..],
1043                            unique_type_id,
1044                            NO_SCOPE_METADATA),
1045         TupleMDF(TupleMemberDescriptionFactory {
1046             ty: tuple_type,
1047             component_types: component_types.to_vec(),
1048             span,
1049         })
1050     )
1051 }
1052
1053 //=-----------------------------------------------------------------------------
1054 // Unions
1055 //=-----------------------------------------------------------------------------
1056
1057 struct UnionMemberDescriptionFactory<'tcx> {
1058     layout: TyLayout<'tcx>,
1059     variant: &'tcx ty::VariantDef,
1060     span: Span,
1061 }
1062
1063 impl<'tcx> UnionMemberDescriptionFactory<'tcx> {
1064     fn create_member_descriptions<'a>(&self, cx: &CodegenCx<'a, 'tcx>)
1065                                       -> Vec<MemberDescription> {
1066         self.variant.fields.iter().enumerate().map(|(i, f)| {
1067             let field = self.layout.field(cx, i);
1068             let (size, align) = field.size_and_align();
1069             MemberDescription {
1070                 name: f.name.to_string(),
1071                 type_metadata: type_metadata(cx, field.ty, self.span),
1072                 offset: Size::from_bytes(0),
1073                 size,
1074                 align,
1075                 flags: DIFlags::FlagZero,
1076             }
1077         }).collect()
1078     }
1079 }
1080
1081 fn prepare_union_metadata<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
1082                                     union_type: Ty<'tcx>,
1083                                     unique_type_id: UniqueTypeId,
1084                                     span: Span)
1085                                     -> RecursiveTypeDescription<'tcx> {
1086     let union_name = compute_debuginfo_type_name(cx, union_type, false);
1087
1088     let (union_def_id, variant) = match union_type.sty {
1089         ty::TyAdt(def, _) => (def.did, def.non_enum_variant()),
1090         _ => bug!("prepare_union_metadata on a non-ADT")
1091     };
1092
1093     let containing_scope = get_namespace_for_item(cx, union_def_id);
1094
1095     let union_metadata_stub = create_union_stub(cx,
1096                                                 union_type,
1097                                                 &union_name,
1098                                                 unique_type_id,
1099                                                 containing_scope);
1100
1101     create_and_register_recursive_type_forward_declaration(
1102         cx,
1103         union_type,
1104         unique_type_id,
1105         union_metadata_stub,
1106         UnionMDF(UnionMemberDescriptionFactory {
1107             layout: cx.layout_of(union_type),
1108             variant,
1109             span,
1110         })
1111     )
1112 }
1113
1114 //=-----------------------------------------------------------------------------
1115 // Enums
1116 //=-----------------------------------------------------------------------------
1117
1118 // Describes the members of an enum value: An enum is described as a union of
1119 // structs in DWARF. This MemberDescriptionFactory provides the description for
1120 // the members of this union; so for every variant of the given enum, this
1121 // factory will produce one MemberDescription (all with no name and a fixed
1122 // offset of zero bytes).
1123 struct EnumMemberDescriptionFactory<'tcx> {
1124     enum_type: Ty<'tcx>,
1125     layout: TyLayout<'tcx>,
1126     discriminant_type_metadata: Option<DIType>,
1127     containing_scope: DIScope,
1128     span: Span,
1129 }
1130
1131 impl<'tcx> EnumMemberDescriptionFactory<'tcx> {
1132     fn create_member_descriptions<'a>(&self, cx: &CodegenCx<'a, 'tcx>)
1133                                       -> Vec<MemberDescription> {
1134         let adt = &self.enum_type.ty_adt_def().unwrap();
1135         match self.layout.variants {
1136             layout::Variants::Single { .. } if adt.variants.is_empty() => vec![],
1137             layout::Variants::Single { index } => {
1138                 let (variant_type_metadata, member_description_factory) =
1139                     describe_enum_variant(cx,
1140                                           self.layout,
1141                                           &adt.variants[index],
1142                                           NoDiscriminant,
1143                                           self.containing_scope,
1144                                           self.span);
1145
1146                 let member_descriptions =
1147                     member_description_factory.create_member_descriptions(cx);
1148
1149                 set_members_of_composite_type(cx,
1150                                               variant_type_metadata,
1151                                               &member_descriptions[..]);
1152                 vec![
1153                     MemberDescription {
1154                         name: "".to_string(),
1155                         type_metadata: variant_type_metadata,
1156                         offset: Size::from_bytes(0),
1157                         size: self.layout.size,
1158                         align: self.layout.align,
1159                         flags: DIFlags::FlagZero
1160                     }
1161                 ]
1162             }
1163             layout::Variants::Tagged { ref variants, .. } => {
1164                 let discriminant_info = RegularDiscriminant(self.discriminant_type_metadata
1165                     .expect(""));
1166                 (0..variants.len()).map(|i| {
1167                     let variant = self.layout.for_variant(cx, i);
1168                     let (variant_type_metadata, member_desc_factory) =
1169                         describe_enum_variant(cx,
1170                                               variant,
1171                                               &adt.variants[i],
1172                                               discriminant_info,
1173                                               self.containing_scope,
1174                                               self.span);
1175
1176                     let member_descriptions = member_desc_factory
1177                         .create_member_descriptions(cx);
1178
1179                     set_members_of_composite_type(cx,
1180                                                   variant_type_metadata,
1181                                                   &member_descriptions);
1182                     MemberDescription {
1183                         name: "".to_string(),
1184                         type_metadata: variant_type_metadata,
1185                         offset: Size::from_bytes(0),
1186                         size: variant.size,
1187                         align: variant.align,
1188                         flags: DIFlags::FlagZero
1189                     }
1190                 }).collect()
1191             }
1192             layout::Variants::NicheFilling { dataful_variant, ref niche_variants, .. } => {
1193                 let variant = self.layout.for_variant(cx, dataful_variant);
1194                 // Create a description of the non-null variant
1195                 let (variant_type_metadata, member_description_factory) =
1196                     describe_enum_variant(cx,
1197                                           variant,
1198                                           &adt.variants[dataful_variant],
1199                                           OptimizedDiscriminant,
1200                                           self.containing_scope,
1201                                           self.span);
1202
1203                 let variant_member_descriptions =
1204                     member_description_factory.create_member_descriptions(cx);
1205
1206                 set_members_of_composite_type(cx,
1207                                               variant_type_metadata,
1208                                               &variant_member_descriptions[..]);
1209
1210                 // Encode the information about the null variant in the union
1211                 // member's name.
1212                 let mut name = String::from("RUST$ENCODED$ENUM$");
1213                 // HACK(eddyb) the debuggers should just handle offset+size
1214                 // of discriminant instead of us having to recover its path.
1215                 // Right now it's not even going to work for `niche_start > 0`,
1216                 // and for multiple niche variants it only supports the first.
1217                 fn compute_field_path<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
1218                                                 name: &mut String,
1219                                                 layout: TyLayout<'tcx>,
1220                                                 offset: Size,
1221                                                 size: Size) {
1222                     for i in 0..layout.fields.count() {
1223                         let field_offset = layout.fields.offset(i);
1224                         if field_offset > offset {
1225                             continue;
1226                         }
1227                         let inner_offset = offset - field_offset;
1228                         let field = layout.field(cx, i);
1229                         if inner_offset + size <= field.size {
1230                             write!(name, "{}$", i).unwrap();
1231                             compute_field_path(cx, name, field, inner_offset, size);
1232                         }
1233                     }
1234                 }
1235                 compute_field_path(cx, &mut name,
1236                                    self.layout,
1237                                    self.layout.fields.offset(0),
1238                                    self.layout.field(cx, 0).size);
1239                 name.push_str(&adt.variants[niche_variants.start].name.as_str());
1240
1241                 // Create the (singleton) list of descriptions of union members.
1242                 vec![
1243                     MemberDescription {
1244                         name,
1245                         type_metadata: variant_type_metadata,
1246                         offset: Size::from_bytes(0),
1247                         size: variant.size,
1248                         align: variant.align,
1249                         flags: DIFlags::FlagZero
1250                     }
1251                 ]
1252             }
1253         }
1254     }
1255 }
1256
1257 // Creates MemberDescriptions for the fields of a single enum variant.
1258 struct VariantMemberDescriptionFactory<'tcx> {
1259     // Cloned from the layout::Struct describing the variant.
1260     offsets: Vec<layout::Size>,
1261     args: Vec<(String, Ty<'tcx>)>,
1262     discriminant_type_metadata: Option<DIType>,
1263     span: Span,
1264 }
1265
1266 impl<'tcx> VariantMemberDescriptionFactory<'tcx> {
1267     fn create_member_descriptions<'a>(&self, cx: &CodegenCx<'a, 'tcx>)
1268                                       -> Vec<MemberDescription> {
1269         self.args.iter().enumerate().map(|(i, &(ref name, ty))| {
1270             let (size, align) = cx.size_and_align_of(ty);
1271             MemberDescription {
1272                 name: name.to_string(),
1273                 type_metadata: match self.discriminant_type_metadata {
1274                     Some(metadata) if i == 0 => metadata,
1275                     _ => type_metadata(cx, ty, self.span)
1276                 },
1277                 offset: self.offsets[i],
1278                 size,
1279                 align,
1280                 flags: DIFlags::FlagZero
1281             }
1282         }).collect()
1283     }
1284 }
1285
1286 #[derive(Copy, Clone)]
1287 enum EnumDiscriminantInfo {
1288     RegularDiscriminant(DIType),
1289     OptimizedDiscriminant,
1290     NoDiscriminant
1291 }
1292
1293 // Returns a tuple of (1) type_metadata_stub of the variant, (2) the llvm_type
1294 // of the variant, and (3) a MemberDescriptionFactory for producing the
1295 // descriptions of the fields of the variant. This is a rudimentary version of a
1296 // full RecursiveTypeDescription.
1297 fn describe_enum_variant<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
1298                                    layout: layout::TyLayout<'tcx>,
1299                                    variant: &'tcx ty::VariantDef,
1300                                    discriminant_info: EnumDiscriminantInfo,
1301                                    containing_scope: DIScope,
1302                                    span: Span)
1303                                    -> (DICompositeType, MemberDescriptionFactory<'tcx>) {
1304     let variant_name = variant.name.as_str();
1305     let unique_type_id = debug_context(cx).type_map
1306                                           .borrow_mut()
1307                                           .get_unique_type_id_of_enum_variant(
1308                                               cx,
1309                                               layout.ty,
1310                                               &variant_name);
1311
1312     let metadata_stub = create_struct_stub(cx,
1313                                            layout.ty,
1314                                            &variant_name,
1315                                            unique_type_id,
1316                                            containing_scope);
1317
1318     // If this is not a univariant enum, there is also the discriminant field.
1319     let (discr_offset, discr_arg) = match discriminant_info {
1320         RegularDiscriminant(_) => {
1321             let enum_layout = cx.layout_of(layout.ty);
1322             (Some(enum_layout.fields.offset(0)),
1323              Some(("RUST$ENUM$DISR".to_string(), enum_layout.field(cx, 0).ty)))
1324         }
1325         _ => (None, None),
1326     };
1327     let offsets = discr_offset.into_iter().chain((0..layout.fields.count()).map(|i| {
1328         layout.fields.offset(i)
1329     })).collect();
1330
1331     // Build an array of (field name, field type) pairs to be captured in the factory closure.
1332     let args = discr_arg.into_iter().chain((0..layout.fields.count()).map(|i| {
1333         let name = if variant.ctor_kind == CtorKind::Fn {
1334             format!("__{}", i)
1335         } else {
1336             variant.fields[i].name.to_string()
1337         };
1338         (name, layout.field(cx, i).ty)
1339     })).collect();
1340
1341     let member_description_factory =
1342         VariantMDF(VariantMemberDescriptionFactory {
1343             offsets,
1344             args,
1345             discriminant_type_metadata: match discriminant_info {
1346                 RegularDiscriminant(discriminant_type_metadata) => {
1347                     Some(discriminant_type_metadata)
1348                 }
1349                 _ => None
1350             },
1351             span,
1352         });
1353
1354     (metadata_stub, member_description_factory)
1355 }
1356
1357 fn prepare_enum_metadata<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
1358                                    enum_type: Ty<'tcx>,
1359                                    enum_def_id: DefId,
1360                                    unique_type_id: UniqueTypeId,
1361                                    span: Span)
1362                                    -> RecursiveTypeDescription<'tcx> {
1363     let enum_name = compute_debuginfo_type_name(cx, enum_type, false);
1364
1365     let containing_scope = get_namespace_for_item(cx, enum_def_id);
1366     // FIXME: This should emit actual file metadata for the enum, but we
1367     // currently can't get the necessary information when it comes to types
1368     // imported from other crates. Formerly we violated the ODR when performing
1369     // LTO because we emitted debuginfo for the same type with varying file
1370     // metadata, so as a workaround we pretend that the type comes from
1371     // <unknown>
1372     let file_metadata = unknown_file_metadata(cx);
1373
1374     let def = enum_type.ty_adt_def().unwrap();
1375     let enumerators_metadata: Vec<DIDescriptor> = def.discriminants(cx.tcx)
1376         .zip(&def.variants)
1377         .map(|(discr, v)| {
1378             let token = v.name.as_str();
1379             let name = CString::new(token.as_bytes()).unwrap();
1380             unsafe {
1381                 llvm::LLVMRustDIBuilderCreateEnumerator(
1382                     DIB(cx),
1383                     name.as_ptr(),
1384                     // FIXME: what if enumeration has i128 discriminant?
1385                     discr.val as u64)
1386             }
1387         })
1388         .collect();
1389
1390     let discriminant_type_metadata = |discr: layout::Primitive| {
1391         let disr_type_key = (enum_def_id, discr);
1392         let cached_discriminant_type_metadata = debug_context(cx).created_enum_disr_types
1393                                                                  .borrow()
1394                                                                  .get(&disr_type_key).cloned();
1395         match cached_discriminant_type_metadata {
1396             Some(discriminant_type_metadata) => discriminant_type_metadata,
1397             None => {
1398                 let (discriminant_size, discriminant_align) =
1399                     (discr.size(cx), discr.align(cx));
1400                 let discriminant_base_type_metadata =
1401                     type_metadata(cx, discr.to_ty(cx.tcx), syntax_pos::DUMMY_SP);
1402                 let discriminant_name = get_enum_discriminant_name(cx, enum_def_id).as_str();
1403
1404                 let name = CString::new(discriminant_name.as_bytes()).unwrap();
1405                 let discriminant_type_metadata = unsafe {
1406                     llvm::LLVMRustDIBuilderCreateEnumerationType(
1407                         DIB(cx),
1408                         containing_scope,
1409                         name.as_ptr(),
1410                         file_metadata,
1411                         UNKNOWN_LINE_NUMBER,
1412                         discriminant_size.bits(),
1413                         discriminant_align.abi_bits() as u32,
1414                         create_DIArray(DIB(cx), &enumerators_metadata),
1415                         discriminant_base_type_metadata)
1416                 };
1417
1418                 debug_context(cx).created_enum_disr_types
1419                                  .borrow_mut()
1420                                  .insert(disr_type_key, discriminant_type_metadata);
1421
1422                 discriminant_type_metadata
1423             }
1424         }
1425     };
1426
1427     let layout = cx.layout_of(enum_type);
1428
1429     let discriminant_type_metadata = match layout.variants {
1430         layout::Variants::Single { .. } |
1431         layout::Variants::NicheFilling { .. } => None,
1432         layout::Variants::Tagged { ref discr, .. } => {
1433             Some(discriminant_type_metadata(discr.value))
1434         }
1435     };
1436
1437     match (&layout.abi, discriminant_type_metadata) {
1438         (&layout::Abi::Scalar(_), Some(discr)) => return FinalMetadata(discr),
1439         _ => {}
1440     }
1441
1442     let (enum_type_size, enum_type_align) = layout.size_and_align();
1443
1444     let enum_name = CString::new(enum_name).unwrap();
1445     let unique_type_id_str = CString::new(
1446         debug_context(cx).type_map.borrow().get_unique_type_id_as_string(unique_type_id).as_bytes()
1447     ).unwrap();
1448     let enum_metadata = unsafe {
1449         llvm::LLVMRustDIBuilderCreateUnionType(
1450         DIB(cx),
1451         containing_scope,
1452         enum_name.as_ptr(),
1453         file_metadata,
1454         UNKNOWN_LINE_NUMBER,
1455         enum_type_size.bits(),
1456         enum_type_align.abi_bits() as u32,
1457         DIFlags::FlagZero,
1458         ptr::null_mut(),
1459         0, // RuntimeLang
1460         unique_type_id_str.as_ptr())
1461     };
1462
1463     return create_and_register_recursive_type_forward_declaration(
1464         cx,
1465         enum_type,
1466         unique_type_id,
1467         enum_metadata,
1468         EnumMDF(EnumMemberDescriptionFactory {
1469             enum_type,
1470             layout,
1471             discriminant_type_metadata,
1472             containing_scope,
1473             span,
1474         }),
1475     );
1476
1477     fn get_enum_discriminant_name(cx: &CodegenCx,
1478                                   def_id: DefId)
1479                                   -> InternedString {
1480         cx.tcx.item_name(def_id)
1481     }
1482 }
1483
1484 /// Creates debug information for a composite type, that is, anything that
1485 /// results in a LLVM struct.
1486 ///
1487 /// Examples of Rust types to use this are: structs, tuples, boxes, vecs, and enums.
1488 fn composite_type_metadata<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
1489                                      composite_type: Ty<'tcx>,
1490                                      composite_type_name: &str,
1491                                      composite_type_unique_id: UniqueTypeId,
1492                                      member_descriptions: &[MemberDescription],
1493                                      containing_scope: DIScope,
1494
1495                                      // Ignore source location information as long as it
1496                                      // can't be reconstructed for non-local crates.
1497                                      _file_metadata: DIFile,
1498                                      _definition_span: Span)
1499                                      -> DICompositeType {
1500     // Create the (empty) struct metadata node ...
1501     let composite_type_metadata = create_struct_stub(cx,
1502                                                      composite_type,
1503                                                      composite_type_name,
1504                                                      composite_type_unique_id,
1505                                                      containing_scope);
1506     // ... and immediately create and add the member descriptions.
1507     set_members_of_composite_type(cx,
1508                                   composite_type_metadata,
1509                                   member_descriptions);
1510
1511     return composite_type_metadata;
1512 }
1513
1514 fn set_members_of_composite_type(cx: &CodegenCx,
1515                                  composite_type_metadata: DICompositeType,
1516                                  member_descriptions: &[MemberDescription]) {
1517     // In some rare cases LLVM metadata uniquing would lead to an existing type
1518     // description being used instead of a new one created in
1519     // create_struct_stub. This would cause a hard to trace assertion in
1520     // DICompositeType::SetTypeArray(). The following check makes sure that we
1521     // get a better error message if this should happen again due to some
1522     // regression.
1523     {
1524         let mut composite_types_completed =
1525             debug_context(cx).composite_types_completed.borrow_mut();
1526         if composite_types_completed.contains(&composite_type_metadata) {
1527             bug!("debuginfo::set_members_of_composite_type() - \
1528                   Already completed forward declaration re-encountered.");
1529         } else {
1530             composite_types_completed.insert(composite_type_metadata);
1531         }
1532     }
1533
1534     let member_metadata: Vec<DIDescriptor> = member_descriptions
1535         .iter()
1536         .map(|member_description| {
1537             let member_name = member_description.name.as_bytes();
1538             let member_name = CString::new(member_name).unwrap();
1539             unsafe {
1540                 llvm::LLVMRustDIBuilderCreateMemberType(
1541                     DIB(cx),
1542                     composite_type_metadata,
1543                     member_name.as_ptr(),
1544                     unknown_file_metadata(cx),
1545                     UNKNOWN_LINE_NUMBER,
1546                     member_description.size.bits(),
1547                     member_description.align.abi_bits() as u32,
1548                     member_description.offset.bits(),
1549                     member_description.flags,
1550                     member_description.type_metadata)
1551             }
1552         })
1553         .collect();
1554
1555     unsafe {
1556         let type_array = create_DIArray(DIB(cx), &member_metadata[..]);
1557         llvm::LLVMRustDICompositeTypeSetTypeArray(
1558             DIB(cx), composite_type_metadata, type_array);
1559     }
1560 }
1561
1562 // A convenience wrapper around LLVMRustDIBuilderCreateStructType(). Does not do
1563 // any caching, does not add any fields to the struct. This can be done later
1564 // with set_members_of_composite_type().
1565 fn create_struct_stub<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
1566                                 struct_type: Ty<'tcx>,
1567                                 struct_type_name: &str,
1568                                 unique_type_id: UniqueTypeId,
1569                                 containing_scope: DIScope)
1570                                 -> DICompositeType {
1571     let (struct_size, struct_align) = cx.size_and_align_of(struct_type);
1572
1573     let name = CString::new(struct_type_name).unwrap();
1574     let unique_type_id = CString::new(
1575         debug_context(cx).type_map.borrow().get_unique_type_id_as_string(unique_type_id).as_bytes()
1576     ).unwrap();
1577     let metadata_stub = unsafe {
1578         // LLVMRustDIBuilderCreateStructType() wants an empty array. A null
1579         // pointer will lead to hard to trace and debug LLVM assertions
1580         // later on in llvm/lib/IR/Value.cpp.
1581         let empty_array = create_DIArray(DIB(cx), &[]);
1582
1583         llvm::LLVMRustDIBuilderCreateStructType(
1584             DIB(cx),
1585             containing_scope,
1586             name.as_ptr(),
1587             unknown_file_metadata(cx),
1588             UNKNOWN_LINE_NUMBER,
1589             struct_size.bits(),
1590             struct_align.abi_bits() as u32,
1591             DIFlags::FlagZero,
1592             ptr::null_mut(),
1593             empty_array,
1594             0,
1595             ptr::null_mut(),
1596             unique_type_id.as_ptr())
1597     };
1598
1599     return metadata_stub;
1600 }
1601
1602 fn create_union_stub<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
1603                                union_type: Ty<'tcx>,
1604                                union_type_name: &str,
1605                                unique_type_id: UniqueTypeId,
1606                                containing_scope: DIScope)
1607                                -> DICompositeType {
1608     let (union_size, union_align) = cx.size_and_align_of(union_type);
1609
1610     let name = CString::new(union_type_name).unwrap();
1611     let unique_type_id = CString::new(
1612         debug_context(cx).type_map.borrow().get_unique_type_id_as_string(unique_type_id).as_bytes()
1613     ).unwrap();
1614     let metadata_stub = unsafe {
1615         // LLVMRustDIBuilderCreateUnionType() wants an empty array. A null
1616         // pointer will lead to hard to trace and debug LLVM assertions
1617         // later on in llvm/lib/IR/Value.cpp.
1618         let empty_array = create_DIArray(DIB(cx), &[]);
1619
1620         llvm::LLVMRustDIBuilderCreateUnionType(
1621             DIB(cx),
1622             containing_scope,
1623             name.as_ptr(),
1624             unknown_file_metadata(cx),
1625             UNKNOWN_LINE_NUMBER,
1626             union_size.bits(),
1627             union_align.abi_bits() as u32,
1628             DIFlags::FlagZero,
1629             empty_array,
1630             0, // RuntimeLang
1631             unique_type_id.as_ptr())
1632     };
1633
1634     return metadata_stub;
1635 }
1636
1637 /// Creates debug information for the given global variable.
1638 ///
1639 /// Adds the created metadata nodes directly to the crate's IR.
1640 pub fn create_global_var_metadata(cx: &CodegenCx,
1641                                   def_id: DefId,
1642                                   global: ValueRef) {
1643     if cx.dbg_cx.is_none() {
1644         return;
1645     }
1646
1647     let tcx = cx.tcx;
1648     let attrs = tcx.trans_fn_attrs(def_id);
1649
1650     if attrs.flags.contains(TransFnAttrFlags::NO_DEBUG) {
1651         return;
1652     }
1653
1654     let no_mangle = attrs.flags.contains(TransFnAttrFlags::NO_MANGLE);
1655     // We may want to remove the namespace scope if we're in an extern block, see:
1656     // https://github.com/rust-lang/rust/pull/46457#issuecomment-351750952
1657     let var_scope = get_namespace_for_item(cx, def_id);
1658     let span = tcx.def_span(def_id);
1659
1660     let (file_metadata, line_number) = if span != syntax_pos::DUMMY_SP {
1661         let loc = span_start(cx, span);
1662         (file_metadata(cx, &loc.file.name, LOCAL_CRATE), loc.line as c_uint)
1663     } else {
1664         (unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
1665     };
1666
1667     let is_local_to_unit = is_node_local_to_unit(cx, def_id);
1668     let variable_type = Instance::mono(cx.tcx, def_id).ty(cx.tcx);
1669     let type_metadata = type_metadata(cx, variable_type, span);
1670     let var_name = tcx.item_name(def_id).to_string();
1671     let var_name = CString::new(var_name).unwrap();
1672     let linkage_name = if no_mangle {
1673         None
1674     } else {
1675         let linkage_name = mangled_name_of_instance(cx, Instance::mono(tcx, def_id));
1676         Some(CString::new(linkage_name.to_string()).unwrap())
1677     };
1678
1679     let global_align = cx.align_of(variable_type);
1680
1681     unsafe {
1682         llvm::LLVMRustDIBuilderCreateStaticVariable(DIB(cx),
1683                                                     var_scope,
1684                                                     var_name.as_ptr(),
1685                                                     // If null, linkage_name field is omitted,
1686                                                     // which is what we want for no_mangle statics
1687                                                     linkage_name.as_ref()
1688                                                      .map_or(ptr::null(), |name| name.as_ptr()),
1689                                                     file_metadata,
1690                                                     line_number,
1691                                                     type_metadata,
1692                                                     is_local_to_unit,
1693                                                     global,
1694                                                     ptr::null_mut(),
1695                                                     global_align.abi() as u32,
1696         );
1697     }
1698 }
1699
1700 // Creates an "extension" of an existing DIScope into another file.
1701 pub fn extend_scope_to_file(cx: &CodegenCx,
1702                             scope_metadata: DIScope,
1703                             file: &syntax_pos::FileMap,
1704                             defining_crate: CrateNum)
1705                             -> DILexicalBlock {
1706     let file_metadata = file_metadata(cx, &file.name, defining_crate);
1707     unsafe {
1708         llvm::LLVMRustDIBuilderCreateLexicalBlockFile(
1709             DIB(cx),
1710             scope_metadata,
1711             file_metadata)
1712     }
1713 }
1714
1715 /// Creates debug information for the given vtable, which is for the
1716 /// given type.
1717 ///
1718 /// Adds the created metadata nodes directly to the crate's IR.
1719 pub fn create_vtable_metadata<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
1720                                         ty: ty::Ty<'tcx>,
1721                                         vtable: ValueRef) {
1722     if cx.dbg_cx.is_none() {
1723         return;
1724     }
1725
1726     let type_metadata = type_metadata(cx, ty, syntax_pos::DUMMY_SP);
1727
1728     unsafe {
1729         // LLVMRustDIBuilderCreateStructType() wants an empty array. A null
1730         // pointer will lead to hard to trace and debug LLVM assertions
1731         // later on in llvm/lib/IR/Value.cpp.
1732         let empty_array = create_DIArray(DIB(cx), &[]);
1733
1734         let name = CString::new("vtable").unwrap();
1735
1736         // Create a new one each time.  We don't want metadata caching
1737         // here, because each vtable will refer to a unique containing
1738         // type.
1739         let vtable_type = llvm::LLVMRustDIBuilderCreateStructType(
1740             DIB(cx),
1741             NO_SCOPE_METADATA,
1742             name.as_ptr(),
1743             unknown_file_metadata(cx),
1744             UNKNOWN_LINE_NUMBER,
1745             Size::from_bytes(0).bits(),
1746             cx.tcx.data_layout.pointer_align.abi_bits() as u32,
1747             DIFlags::FlagArtificial,
1748             ptr::null_mut(),
1749             empty_array,
1750             0,
1751             type_metadata,
1752             name.as_ptr()
1753         );
1754
1755         llvm::LLVMRustDIBuilderCreateStaticVariable(DIB(cx),
1756                                                     NO_SCOPE_METADATA,
1757                                                     name.as_ptr(),
1758                                                     // LLVM 3.9
1759                                                     // doesn't accept
1760                                                     // null here, so
1761                                                     // pass the name
1762                                                     // as the linkage
1763                                                     // name.
1764                                                     name.as_ptr(),
1765                                                     unknown_file_metadata(cx),
1766                                                     UNKNOWN_LINE_NUMBER,
1767                                                     vtable_type,
1768                                                     true,
1769                                                     vtable,
1770                                                     ptr::null_mut(),
1771                                                     0);
1772     }
1773 }