]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
Rollup merge of #102578 - lukas-code:ilog-panic, r=m-ou-se
[rust.git] / compiler / rustc_codegen_llvm / src / debuginfo / metadata.rs
1 use self::type_map::DINodeCreationResult;
2 use self::type_map::Stub;
3 use self::type_map::UniqueTypeId;
4
5 use super::namespace::mangled_name_of_instance;
6 use super::type_names::{compute_debuginfo_type_name, compute_debuginfo_vtable_name};
7 use super::utils::{
8     create_DIArray, debug_context, get_namespace_for_item, is_node_local_to_unit, DIB,
9 };
10 use super::CodegenUnitDebugContext;
11
12 use crate::abi;
13 use crate::common::CodegenCx;
14 use crate::debuginfo::metadata::type_map::build_type_with_children;
15 use crate::debuginfo::utils::fat_pointer_kind;
16 use crate::debuginfo::utils::FatPtrKind;
17 use crate::llvm;
18 use crate::llvm::debuginfo::{
19     DIDescriptor, DIFile, DIFlags, DILexicalBlock, DIScope, DIType, DebugEmissionKind,
20 };
21 use crate::value::Value;
22
23 use cstr::cstr;
24 use rustc_codegen_ssa::debuginfo::type_names::cpp_like_debuginfo;
25 use rustc_codegen_ssa::debuginfo::type_names::VTableNameKind;
26 use rustc_codegen_ssa::traits::*;
27 use rustc_fs_util::path_to_c_string;
28 use rustc_hir::def::CtorKind;
29 use rustc_hir::def_id::{DefId, LOCAL_CRATE};
30 use rustc_index::vec::{Idx, IndexVec};
31 use rustc_middle::bug;
32 use rustc_middle::mir::{self, GeneratorLayout};
33 use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
34 use rustc_middle::ty::subst::GenericArgKind;
35 use rustc_middle::ty::{
36     self, AdtKind, Instance, ParamEnv, PolyExistentialTraitRef, Ty, TyCtxt, Visibility,
37 };
38 use rustc_session::config::{self, DebugInfo, Lto};
39 use rustc_span::symbol::Symbol;
40 use rustc_span::FileName;
41 use rustc_span::{self, FileNameDisplayPreference, SourceFile};
42 use rustc_symbol_mangling::typeid_for_trait_ref;
43 use rustc_target::abi::{Align, Size};
44 use smallvec::smallvec;
45
46 use libc::{c_char, c_longlong, c_uint};
47 use std::borrow::Cow;
48 use std::fmt::{self, Write};
49 use std::hash::{Hash, Hasher};
50 use std::iter;
51 use std::path::{Path, PathBuf};
52 use std::ptr;
53
54 impl PartialEq for llvm::Metadata {
55     fn eq(&self, other: &Self) -> bool {
56         ptr::eq(self, other)
57     }
58 }
59
60 impl Eq for llvm::Metadata {}
61
62 impl Hash for llvm::Metadata {
63     fn hash<H: Hasher>(&self, hasher: &mut H) {
64         (self as *const Self).hash(hasher);
65     }
66 }
67
68 impl fmt::Debug for llvm::Metadata {
69     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70         (self as *const Self).fmt(f)
71     }
72 }
73
74 // From DWARF 5.
75 // See http://www.dwarfstd.org/ShowIssue.php?issue=140129.1.
76 const DW_LANG_RUST: c_uint = 0x1c;
77 #[allow(non_upper_case_globals)]
78 const DW_ATE_boolean: c_uint = 0x02;
79 #[allow(non_upper_case_globals)]
80 const DW_ATE_float: c_uint = 0x04;
81 #[allow(non_upper_case_globals)]
82 const DW_ATE_signed: c_uint = 0x05;
83 #[allow(non_upper_case_globals)]
84 const DW_ATE_unsigned: c_uint = 0x07;
85 #[allow(non_upper_case_globals)]
86 const DW_ATE_UTF: c_uint = 0x10;
87
88 pub(super) const UNKNOWN_LINE_NUMBER: c_uint = 0;
89 pub(super) const UNKNOWN_COLUMN_NUMBER: c_uint = 0;
90
91 const NO_SCOPE_METADATA: Option<&DIScope> = None;
92 /// A function that returns an empty list of generic parameter debuginfo nodes.
93 const NO_GENERICS: for<'ll> fn(&CodegenCx<'ll, '_>) -> SmallVec<&'ll DIType> = |_| SmallVec::new();
94
95 // SmallVec is used quite a bit in this module, so create a shorthand.
96 // The actual number of elements is not so important.
97 pub type SmallVec<T> = smallvec::SmallVec<[T; 16]>;
98
99 mod enums;
100 mod type_map;
101
102 pub(crate) use type_map::TypeMap;
103
104 /// Returns from the enclosing function if the type debuginfo node with the given
105 /// unique ID can be found in the type map.
106 macro_rules! return_if_di_node_created_in_meantime {
107     ($cx: expr, $unique_type_id: expr) => {
108         if let Some(di_node) = debug_context($cx).type_map.di_node_for_unique_id($unique_type_id) {
109             return DINodeCreationResult::new(di_node, true);
110         }
111     };
112 }
113
114 /// Extract size and alignment from a TyAndLayout.
115 #[inline]
116 fn size_and_align_of<'tcx>(ty_and_layout: TyAndLayout<'tcx>) -> (Size, Align) {
117     (ty_and_layout.size, ty_and_layout.align.abi)
118 }
119
120 /// Creates debuginfo for a fixed size array (e.g. `[u64; 123]`).
121 /// For slices (that is, "arrays" of unknown size) use [build_slice_type_di_node].
122 fn build_fixed_size_array_di_node<'ll, 'tcx>(
123     cx: &CodegenCx<'ll, 'tcx>,
124     unique_type_id: UniqueTypeId<'tcx>,
125     array_type: Ty<'tcx>,
126 ) -> DINodeCreationResult<'ll> {
127     let ty::Array(element_type, len) = array_type.kind() else {
128         bug!("build_fixed_size_array_di_node() called with non-ty::Array type `{:?}`", array_type)
129     };
130
131     let element_type_di_node = type_di_node(cx, *element_type);
132
133     return_if_di_node_created_in_meantime!(cx, unique_type_id);
134
135     let (size, align) = cx.size_and_align_of(array_type);
136
137     let upper_bound = len.eval_usize(cx.tcx, ty::ParamEnv::reveal_all()) as c_longlong;
138
139     let subrange =
140         unsafe { Some(llvm::LLVMRustDIBuilderGetOrCreateSubrange(DIB(cx), 0, upper_bound)) };
141
142     let subscripts = create_DIArray(DIB(cx), &[subrange]);
143     let di_node = unsafe {
144         llvm::LLVMRustDIBuilderCreateArrayType(
145             DIB(cx),
146             size.bits(),
147             align.bits() as u32,
148             element_type_di_node,
149             subscripts,
150         )
151     };
152
153     DINodeCreationResult::new(di_node, false)
154 }
155
156 /// Creates debuginfo for built-in pointer-like things:
157 ///
158 ///  - ty::Ref
159 ///  - ty::RawPtr
160 ///  - ty::Adt in the case it's Box
161 ///
162 /// At some point we might want to remove the special handling of Box
163 /// and treat it the same as other smart pointers (like Rc, Arc, ...).
164 fn build_pointer_or_reference_di_node<'ll, 'tcx>(
165     cx: &CodegenCx<'ll, 'tcx>,
166     ptr_type: Ty<'tcx>,
167     pointee_type: Ty<'tcx>,
168     unique_type_id: UniqueTypeId<'tcx>,
169 ) -> DINodeCreationResult<'ll> {
170     // The debuginfo generated by this function is only valid if `ptr_type` is really just
171     // a (fat) pointer. Make sure it is not called for e.g. `Box<T, NonZSTAllocator>`.
172     debug_assert_eq!(
173         cx.size_and_align_of(ptr_type),
174         cx.size_and_align_of(cx.tcx.mk_mut_ptr(pointee_type))
175     );
176
177     let pointee_type_di_node = type_di_node(cx, pointee_type);
178
179     return_if_di_node_created_in_meantime!(cx, unique_type_id);
180
181     let (thin_pointer_size, thin_pointer_align) =
182         cx.size_and_align_of(cx.tcx.mk_imm_ptr(cx.tcx.types.unit));
183     let ptr_type_debuginfo_name = compute_debuginfo_type_name(cx.tcx, ptr_type, true);
184
185     match fat_pointer_kind(cx, pointee_type) {
186         None => {
187             // This is a thin pointer. Create a regular pointer type and give it the correct name.
188             debug_assert_eq!(
189                 (thin_pointer_size, thin_pointer_align),
190                 cx.size_and_align_of(ptr_type),
191                 "ptr_type={}, pointee_type={}",
192                 ptr_type,
193                 pointee_type,
194             );
195
196             let di_node = unsafe {
197                 llvm::LLVMRustDIBuilderCreatePointerType(
198                     DIB(cx),
199                     pointee_type_di_node,
200                     thin_pointer_size.bits(),
201                     thin_pointer_align.bits() as u32,
202                     0, // Ignore DWARF address space.
203                     ptr_type_debuginfo_name.as_ptr().cast(),
204                     ptr_type_debuginfo_name.len(),
205                 )
206             };
207
208             DINodeCreationResult { di_node, already_stored_in_typemap: false }
209         }
210         Some(fat_pointer_kind) => {
211             type_map::build_type_with_children(
212                 cx,
213                 type_map::stub(
214                     cx,
215                     Stub::Struct,
216                     unique_type_id,
217                     &ptr_type_debuginfo_name,
218                     cx.size_and_align_of(ptr_type),
219                     NO_SCOPE_METADATA,
220                     DIFlags::FlagZero,
221                 ),
222                 |cx, owner| {
223                     // FIXME: If this fat pointer is a `Box` then we don't want to use its
224                     //        type layout and instead use the layout of the raw pointer inside
225                     //        of it.
226                     //        The proper way to handle this is to not treat Box as a pointer
227                     //        at all and instead emit regular struct debuginfo for it. We just
228                     //        need to make sure that we don't break existing debuginfo consumers
229                     //        by doing that (at least not without a warning period).
230                     let layout_type =
231                         if ptr_type.is_box() { cx.tcx.mk_mut_ptr(pointee_type) } else { ptr_type };
232
233                     let layout = cx.layout_of(layout_type);
234                     let addr_field = layout.field(cx, abi::FAT_PTR_ADDR);
235                     let extra_field = layout.field(cx, abi::FAT_PTR_EXTRA);
236
237                     let (addr_field_name, extra_field_name) = match fat_pointer_kind {
238                         FatPtrKind::Dyn => ("pointer", "vtable"),
239                         FatPtrKind::Slice => ("data_ptr", "length"),
240                     };
241
242                     debug_assert_eq!(abi::FAT_PTR_ADDR, 0);
243                     debug_assert_eq!(abi::FAT_PTR_EXTRA, 1);
244
245                     // The data pointer type is a regular, thin pointer, regardless of whether this
246                     // is a slice or a trait object.
247                     let data_ptr_type_di_node = unsafe {
248                         llvm::LLVMRustDIBuilderCreatePointerType(
249                             DIB(cx),
250                             pointee_type_di_node,
251                             addr_field.size.bits(),
252                             addr_field.align.abi.bits() as u32,
253                             0, // Ignore DWARF address space.
254                             std::ptr::null(),
255                             0,
256                         )
257                     };
258
259                     smallvec![
260                         build_field_di_node(
261                             cx,
262                             owner,
263                             addr_field_name,
264                             (addr_field.size, addr_field.align.abi),
265                             layout.fields.offset(abi::FAT_PTR_ADDR),
266                             DIFlags::FlagZero,
267                             data_ptr_type_di_node,
268                         ),
269                         build_field_di_node(
270                             cx,
271                             owner,
272                             extra_field_name,
273                             (extra_field.size, extra_field.align.abi),
274                             layout.fields.offset(abi::FAT_PTR_EXTRA),
275                             DIFlags::FlagZero,
276                             type_di_node(cx, extra_field.ty),
277                         ),
278                     ]
279                 },
280                 NO_GENERICS,
281             )
282         }
283     }
284 }
285
286 fn build_subroutine_type_di_node<'ll, 'tcx>(
287     cx: &CodegenCx<'ll, 'tcx>,
288     unique_type_id: UniqueTypeId<'tcx>,
289 ) -> DINodeCreationResult<'ll> {
290     // It's possible to create a self-referential
291     // type in Rust by using 'impl trait':
292     //
293     // fn foo() -> impl Copy { foo }
294     //
295     // Unfortunately LLVM's API does not allow us to create recursive subroutine types.
296     // In order to work around that restriction we place a marker type in the type map,
297     // before creating the actual type. If the actual type is recursive, it will hit the
298     // marker type. So we end up with a type that looks like
299     //
300     // fn foo() -> <recursive_type>
301     //
302     // Once that is created, we replace the marker in the typemap with the actual type.
303     debug_context(cx)
304         .type_map
305         .unique_id_to_di_node
306         .borrow_mut()
307         .insert(unique_type_id, recursion_marker_type_di_node(cx));
308
309     let fn_ty = unique_type_id.expect_ty();
310     let signature = cx
311         .tcx
312         .normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), fn_ty.fn_sig(cx.tcx));
313
314     let signature_di_nodes: SmallVec<_> = iter::once(
315         // return type
316         match signature.output().kind() {
317             ty::Tuple(tys) if tys.is_empty() => {
318                 // this is a "void" function
319                 None
320             }
321             _ => Some(type_di_node(cx, signature.output())),
322         },
323     )
324     .chain(
325         // regular arguments
326         signature.inputs().iter().map(|&argument_type| Some(type_di_node(cx, argument_type))),
327     )
328     .collect();
329
330     debug_context(cx).type_map.unique_id_to_di_node.borrow_mut().remove(&unique_type_id);
331
332     let fn_di_node = unsafe {
333         llvm::LLVMRustDIBuilderCreateSubroutineType(
334             DIB(cx),
335             create_DIArray(DIB(cx), &signature_di_nodes[..]),
336         )
337     };
338
339     // This is actually a function pointer, so wrap it in pointer DI.
340     let name = compute_debuginfo_type_name(cx.tcx, fn_ty, false);
341     let di_node = unsafe {
342         llvm::LLVMRustDIBuilderCreatePointerType(
343             DIB(cx),
344             fn_di_node,
345             cx.tcx.data_layout.pointer_size.bits(),
346             cx.tcx.data_layout.pointer_align.abi.bits() as u32,
347             0, // Ignore DWARF address space.
348             name.as_ptr().cast(),
349             name.len(),
350         )
351     };
352
353     DINodeCreationResult::new(di_node, false)
354 }
355
356 /// Create debuginfo for `dyn SomeTrait` types. Currently these are empty structs
357 /// we with the correct type name (e.g. "dyn SomeTrait<Foo, Item=u32> + Sync").
358 fn build_dyn_type_di_node<'ll, 'tcx>(
359     cx: &CodegenCx<'ll, 'tcx>,
360     dyn_type: Ty<'tcx>,
361     unique_type_id: UniqueTypeId<'tcx>,
362 ) -> DINodeCreationResult<'ll> {
363     if let ty::Dynamic(..) = dyn_type.kind() {
364         let type_name = compute_debuginfo_type_name(cx.tcx, dyn_type, true);
365         type_map::build_type_with_children(
366             cx,
367             type_map::stub(
368                 cx,
369                 Stub::Struct,
370                 unique_type_id,
371                 &type_name,
372                 cx.size_and_align_of(dyn_type),
373                 NO_SCOPE_METADATA,
374                 DIFlags::FlagZero,
375             ),
376             |_, _| smallvec![],
377             NO_GENERICS,
378         )
379     } else {
380         bug!(
381             "Only ty::Dynamic is valid for build_dyn_type_di_node(). Found {:?} instead.",
382             dyn_type
383         )
384     }
385 }
386
387 /// Create debuginfo for `[T]` and `str`. These are unsized.
388 ///
389 /// NOTE: We currently emit just emit the debuginfo for the element type here
390 /// (i.e. `T` for slices and `u8` for `str`), so that we end up with
391 /// `*const T` for the `data_ptr` field of the corresponding fat-pointer
392 /// debuginfo of `&[T]`.
393 ///
394 /// It would be preferable and more accurate if we emitted a DIArray of T
395 /// without an upper bound instead. That is, LLVM already supports emitting
396 /// debuginfo of arrays of unknown size. But GDB currently seems to end up
397 /// in an infinite loop when confronted with such a type.
398 ///
399 /// As a side effect of the current encoding every instance of a type like
400 /// `struct Foo { unsized_field: [u8] }` will look like
401 /// `struct Foo { unsized_field: u8 }` in debuginfo. If the length of the
402 /// slice is zero, then accessing `unsized_field` in the debugger would
403 /// result in an out-of-bounds access.
404 fn build_slice_type_di_node<'ll, 'tcx>(
405     cx: &CodegenCx<'ll, 'tcx>,
406     slice_type: Ty<'tcx>,
407     unique_type_id: UniqueTypeId<'tcx>,
408 ) -> DINodeCreationResult<'ll> {
409     let element_type = match slice_type.kind() {
410         ty::Slice(element_type) => *element_type,
411         ty::Str => cx.tcx.types.u8,
412         _ => {
413             bug!(
414                 "Only ty::Slice is valid for build_slice_type_di_node(). Found {:?} instead.",
415                 slice_type
416             )
417         }
418     };
419
420     let element_type_di_node = type_di_node(cx, element_type);
421     return_if_di_node_created_in_meantime!(cx, unique_type_id);
422     DINodeCreationResult { di_node: element_type_di_node, already_stored_in_typemap: false }
423 }
424
425 /// Get the debuginfo node for the given type.
426 ///
427 /// This function will look up the debuginfo node in the TypeMap. If it can't find it, it
428 /// will create the node by dispatching to the corresponding `build_*_di_node()` function.
429 pub fn type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) -> &'ll DIType {
430     let unique_type_id = UniqueTypeId::for_ty(cx.tcx, t);
431
432     if let Some(existing_di_node) = debug_context(cx).type_map.di_node_for_unique_id(unique_type_id)
433     {
434         return existing_di_node;
435     }
436
437     debug!("type_di_node: {:?}", t);
438
439     let DINodeCreationResult { di_node, already_stored_in_typemap } = match *t.kind() {
440         ty::Never | ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) => {
441             build_basic_type_di_node(cx, t)
442         }
443         ty::Tuple(elements) if elements.is_empty() => build_basic_type_di_node(cx, t),
444         ty::Array(..) => build_fixed_size_array_di_node(cx, unique_type_id, t),
445         ty::Slice(_) | ty::Str => build_slice_type_di_node(cx, t, unique_type_id),
446         ty::Dynamic(..) => build_dyn_type_di_node(cx, t, unique_type_id),
447         ty::Foreign(..) => build_foreign_type_di_node(cx, t, unique_type_id),
448         ty::RawPtr(ty::TypeAndMut { ty: pointee_type, .. }) | ty::Ref(_, pointee_type, _) => {
449             build_pointer_or_reference_di_node(cx, t, pointee_type, unique_type_id)
450         }
451         // Box<T, A> may have a non-ZST allocator A. In that case, we
452         // cannot treat Box<T, A> as just an owned alias of `*mut T`.
453         ty::Adt(def, substs) if def.is_box() && cx.layout_of(substs.type_at(1)).is_zst() => {
454             build_pointer_or_reference_di_node(cx, t, t.boxed_ty(), unique_type_id)
455         }
456         ty::FnDef(..) | ty::FnPtr(_) => build_subroutine_type_di_node(cx, unique_type_id),
457         ty::Closure(..) => build_closure_env_di_node(cx, unique_type_id),
458         ty::Generator(..) => enums::build_generator_di_node(cx, unique_type_id),
459         ty::Adt(def, ..) => match def.adt_kind() {
460             AdtKind::Struct => build_struct_type_di_node(cx, unique_type_id),
461             AdtKind::Union => build_union_type_di_node(cx, unique_type_id),
462             AdtKind::Enum => enums::build_enum_type_di_node(cx, unique_type_id),
463         },
464         ty::Tuple(_) => build_tuple_type_di_node(cx, unique_type_id),
465         // Type parameters from polymorphized functions.
466         ty::Param(_) => build_param_type_di_node(cx, t),
467         _ => bug!("debuginfo: unexpected type in type_di_node(): {:?}", t),
468     };
469
470     {
471         if already_stored_in_typemap {
472             // Make sure that we really do have a `TypeMap` entry for the unique type ID.
473             let di_node_for_uid =
474                 match debug_context(cx).type_map.di_node_for_unique_id(unique_type_id) {
475                     Some(di_node) => di_node,
476                     None => {
477                         bug!(
478                             "expected type debuginfo node for unique \
479                                type ID '{:?}' to already be in \
480                                the `debuginfo::TypeMap` but it \
481                                was not.",
482                             unique_type_id,
483                         );
484                     }
485                 };
486
487             debug_assert_eq!(di_node_for_uid as *const _, di_node as *const _);
488         } else {
489             debug_context(cx).type_map.insert(unique_type_id, di_node);
490         }
491     }
492
493     di_node
494 }
495
496 // FIXME(mw): Cache this via a regular UniqueTypeId instead of an extra field in the debug context.
497 fn recursion_marker_type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) -> &'ll DIType {
498     *debug_context(cx).recursion_marker_type.get_or_init(move || {
499         unsafe {
500             // The choice of type here is pretty arbitrary -
501             // anything reading the debuginfo for a recursive
502             // type is going to see *something* weird - the only
503             // question is what exactly it will see.
504             //
505             // FIXME: the name `<recur_type>` does not fit the naming scheme
506             //        of other types.
507             //
508             // FIXME: it might make sense to use an actual pointer type here
509             //        so that debuggers can show the address.
510             let name = "<recur_type>";
511             llvm::LLVMRustDIBuilderCreateBasicType(
512                 DIB(cx),
513                 name.as_ptr().cast(),
514                 name.len(),
515                 cx.tcx.data_layout.pointer_size.bits(),
516                 DW_ATE_unsigned,
517             )
518         }
519     })
520 }
521
522 fn hex_encode(data: &[u8]) -> String {
523     let mut hex_string = String::with_capacity(data.len() * 2);
524     for byte in data.iter() {
525         write!(&mut hex_string, "{:02x}", byte).unwrap();
526     }
527     hex_string
528 }
529
530 pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) -> &'ll DIFile {
531     let cache_key = Some((source_file.name_hash, source_file.src_hash));
532     return debug_context(cx)
533         .created_files
534         .borrow_mut()
535         .entry(cache_key)
536         .or_insert_with(|| alloc_new_file_metadata(cx, source_file));
537
538     #[instrument(skip(cx, source_file), level = "debug")]
539     fn alloc_new_file_metadata<'ll>(
540         cx: &CodegenCx<'ll, '_>,
541         source_file: &SourceFile,
542     ) -> &'ll DIFile {
543         debug!(?source_file.name);
544
545         let (directory, file_name) = match &source_file.name {
546             FileName::Real(filename) => {
547                 let working_directory = &cx.sess().opts.working_dir;
548                 debug!(?working_directory);
549
550                 let filename = cx
551                     .sess()
552                     .source_map()
553                     .path_mapping()
554                     .to_embeddable_absolute_path(filename.clone(), working_directory);
555
556                 // Construct the absolute path of the file
557                 let abs_path = filename.remapped_path_if_available();
558                 debug!(?abs_path);
559
560                 if let Ok(rel_path) =
561                     abs_path.strip_prefix(working_directory.remapped_path_if_available())
562                 {
563                     // If the compiler's working directory (which also is the DW_AT_comp_dir of
564                     // the compilation unit) is a prefix of the path we are about to emit, then
565                     // only emit the part relative to the working directory.
566                     // Because of path remapping we sometimes see strange things here: `abs_path`
567                     // might actually look like a relative path
568                     // (e.g. `<crate-name-and-version>/src/lib.rs`), so if we emit it without
569                     // taking the working directory into account, downstream tooling will
570                     // interpret it as `<working-directory>/<crate-name-and-version>/src/lib.rs`,
571                     // which makes no sense. Usually in such cases the working directory will also
572                     // be remapped to `<crate-name-and-version>` or some other prefix of the path
573                     // we are remapping, so we end up with
574                     // `<crate-name-and-version>/<crate-name-and-version>/src/lib.rs`.
575                     // By moving the working directory portion into the `directory` part of the
576                     // DIFile, we allow LLVM to emit just the relative path for DWARF, while
577                     // still emitting the correct absolute path for CodeView.
578                     (
579                         working_directory.to_string_lossy(FileNameDisplayPreference::Remapped),
580                         rel_path.to_string_lossy().into_owned(),
581                     )
582                 } else {
583                     ("".into(), abs_path.to_string_lossy().into_owned())
584                 }
585             }
586             other => ("".into(), other.prefer_remapped().to_string_lossy().into_owned()),
587         };
588
589         let hash_kind = match source_file.src_hash.kind {
590             rustc_span::SourceFileHashAlgorithm::Md5 => llvm::ChecksumKind::MD5,
591             rustc_span::SourceFileHashAlgorithm::Sha1 => llvm::ChecksumKind::SHA1,
592             rustc_span::SourceFileHashAlgorithm::Sha256 => llvm::ChecksumKind::SHA256,
593         };
594         let hash_value = hex_encode(source_file.src_hash.hash_bytes());
595
596         unsafe {
597             llvm::LLVMRustDIBuilderCreateFile(
598                 DIB(cx),
599                 file_name.as_ptr().cast(),
600                 file_name.len(),
601                 directory.as_ptr().cast(),
602                 directory.len(),
603                 hash_kind,
604                 hash_value.as_ptr().cast(),
605                 hash_value.len(),
606             )
607         }
608     }
609 }
610
611 pub fn unknown_file_metadata<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll DIFile {
612     debug_context(cx).created_files.borrow_mut().entry(None).or_insert_with(|| unsafe {
613         let file_name = "<unknown>";
614         let directory = "";
615         let hash_value = "";
616
617         llvm::LLVMRustDIBuilderCreateFile(
618             DIB(cx),
619             file_name.as_ptr().cast(),
620             file_name.len(),
621             directory.as_ptr().cast(),
622             directory.len(),
623             llvm::ChecksumKind::None,
624             hash_value.as_ptr().cast(),
625             hash_value.len(),
626         )
627     })
628 }
629
630 trait MsvcBasicName {
631     fn msvc_basic_name(self) -> &'static str;
632 }
633
634 impl MsvcBasicName for ty::IntTy {
635     fn msvc_basic_name(self) -> &'static str {
636         match self {
637             ty::IntTy::Isize => "ptrdiff_t",
638             ty::IntTy::I8 => "__int8",
639             ty::IntTy::I16 => "__int16",
640             ty::IntTy::I32 => "__int32",
641             ty::IntTy::I64 => "__int64",
642             ty::IntTy::I128 => "__int128",
643         }
644     }
645 }
646
647 impl MsvcBasicName for ty::UintTy {
648     fn msvc_basic_name(self) -> &'static str {
649         match self {
650             ty::UintTy::Usize => "size_t",
651             ty::UintTy::U8 => "unsigned __int8",
652             ty::UintTy::U16 => "unsigned __int16",
653             ty::UintTy::U32 => "unsigned __int32",
654             ty::UintTy::U64 => "unsigned __int64",
655             ty::UintTy::U128 => "unsigned __int128",
656         }
657     }
658 }
659
660 impl MsvcBasicName for ty::FloatTy {
661     fn msvc_basic_name(self) -> &'static str {
662         match self {
663             ty::FloatTy::F32 => "float",
664             ty::FloatTy::F64 => "double",
665         }
666     }
667 }
668
669 fn build_basic_type_di_node<'ll, 'tcx>(
670     cx: &CodegenCx<'ll, 'tcx>,
671     t: Ty<'tcx>,
672 ) -> DINodeCreationResult<'ll> {
673     debug!("build_basic_type_di_node: {:?}", t);
674
675     // When targeting MSVC, emit MSVC style type names for compatibility with
676     // .natvis visualizers (and perhaps other existing native debuggers?)
677     let cpp_like_debuginfo = cpp_like_debuginfo(cx.tcx);
678
679     let (name, encoding) = match t.kind() {
680         ty::Never => ("!", DW_ATE_unsigned),
681         ty::Tuple(elements) if elements.is_empty() => {
682             if cpp_like_debuginfo {
683                 return build_tuple_type_di_node(cx, UniqueTypeId::for_ty(cx.tcx, t));
684             } else {
685                 ("()", DW_ATE_unsigned)
686             }
687         }
688         ty::Bool => ("bool", DW_ATE_boolean),
689         ty::Char => ("char", DW_ATE_UTF),
690         ty::Int(int_ty) if cpp_like_debuginfo => (int_ty.msvc_basic_name(), DW_ATE_signed),
691         ty::Uint(uint_ty) if cpp_like_debuginfo => (uint_ty.msvc_basic_name(), DW_ATE_unsigned),
692         ty::Float(float_ty) if cpp_like_debuginfo => (float_ty.msvc_basic_name(), DW_ATE_float),
693         ty::Int(int_ty) => (int_ty.name_str(), DW_ATE_signed),
694         ty::Uint(uint_ty) => (uint_ty.name_str(), DW_ATE_unsigned),
695         ty::Float(float_ty) => (float_ty.name_str(), DW_ATE_float),
696         _ => bug!("debuginfo::build_basic_type_di_node - `t` is invalid type"),
697     };
698
699     let ty_di_node = unsafe {
700         llvm::LLVMRustDIBuilderCreateBasicType(
701             DIB(cx),
702             name.as_ptr().cast(),
703             name.len(),
704             cx.size_of(t).bits(),
705             encoding,
706         )
707     };
708
709     if !cpp_like_debuginfo {
710         return DINodeCreationResult::new(ty_di_node, false);
711     }
712
713     let typedef_name = match t.kind() {
714         ty::Int(int_ty) => int_ty.name_str(),
715         ty::Uint(uint_ty) => uint_ty.name_str(),
716         ty::Float(float_ty) => float_ty.name_str(),
717         _ => return DINodeCreationResult::new(ty_di_node, false),
718     };
719
720     let typedef_di_node = unsafe {
721         llvm::LLVMRustDIBuilderCreateTypedef(
722             DIB(cx),
723             ty_di_node,
724             typedef_name.as_ptr().cast(),
725             typedef_name.len(),
726             unknown_file_metadata(cx),
727             0,
728             None,
729         )
730     };
731
732     DINodeCreationResult::new(typedef_di_node, false)
733 }
734
735 fn build_foreign_type_di_node<'ll, 'tcx>(
736     cx: &CodegenCx<'ll, 'tcx>,
737     t: Ty<'tcx>,
738     unique_type_id: UniqueTypeId<'tcx>,
739 ) -> DINodeCreationResult<'ll> {
740     debug!("build_foreign_type_di_node: {:?}", t);
741
742     let &ty::Foreign(def_id) = unique_type_id.expect_ty().kind() else {
743         bug!("build_foreign_type_di_node() called with unexpected type: {:?}", unique_type_id.expect_ty());
744     };
745
746     build_type_with_children(
747         cx,
748         type_map::stub(
749             cx,
750             Stub::Struct,
751             unique_type_id,
752             &compute_debuginfo_type_name(cx.tcx, t, false),
753             cx.size_and_align_of(t),
754             Some(get_namespace_for_item(cx, def_id)),
755             DIFlags::FlagZero,
756         ),
757         |_, _| smallvec![],
758         NO_GENERICS,
759     )
760 }
761
762 fn build_param_type_di_node<'ll, 'tcx>(
763     cx: &CodegenCx<'ll, 'tcx>,
764     t: Ty<'tcx>,
765 ) -> DINodeCreationResult<'ll> {
766     debug!("build_param_type_di_node: {:?}", t);
767     let name = format!("{:?}", t);
768     DINodeCreationResult {
769         di_node: unsafe {
770             llvm::LLVMRustDIBuilderCreateBasicType(
771                 DIB(cx),
772                 name.as_ptr().cast(),
773                 name.len(),
774                 Size::ZERO.bits(),
775                 DW_ATE_unsigned,
776             )
777         },
778         already_stored_in_typemap: false,
779     }
780 }
781
782 pub fn build_compile_unit_di_node<'ll, 'tcx>(
783     tcx: TyCtxt<'tcx>,
784     codegen_unit_name: &str,
785     debug_context: &CodegenUnitDebugContext<'ll, 'tcx>,
786 ) -> &'ll DIDescriptor {
787     let mut name_in_debuginfo = match tcx.sess.local_crate_source_file {
788         Some(ref path) => path.clone(),
789         None => PathBuf::from(tcx.crate_name(LOCAL_CRATE).as_str()),
790     };
791
792     // To avoid breaking split DWARF, we need to ensure that each codegen unit
793     // has a unique `DW_AT_name`. This is because there's a remote chance that
794     // different codegen units for the same module will have entirely
795     // identical DWARF entries for the purpose of the DWO ID, which would
796     // violate Appendix F ("Split Dwarf Object Files") of the DWARF 5
797     // specification. LLVM uses the algorithm specified in section 7.32 "Type
798     // Signature Computation" to compute the DWO ID, which does not include
799     // any fields that would distinguish compilation units. So we must embed
800     // the codegen unit name into the `DW_AT_name`. (Issue #88521.)
801     //
802     // Additionally, the OSX linker has an idiosyncrasy where it will ignore
803     // some debuginfo if multiple object files with the same `DW_AT_name` are
804     // linked together.
805     //
806     // As a workaround for these two issues, we generate unique names for each
807     // object file. Those do not correspond to an actual source file but that
808     // is harmless.
809     name_in_debuginfo.push("@");
810     name_in_debuginfo.push(codegen_unit_name);
811
812     debug!("build_compile_unit_di_node: {:?}", name_in_debuginfo);
813     let rustc_producer =
814         format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION"),);
815     // FIXME(#41252) Remove "clang LLVM" if we can get GDB and LLVM to play nice.
816     let producer = format!("clang LLVM ({})", rustc_producer);
817
818     let name_in_debuginfo = name_in_debuginfo.to_string_lossy();
819     let work_dir = tcx.sess.opts.working_dir.to_string_lossy(FileNameDisplayPreference::Remapped);
820     let flags = "\0";
821     let output_filenames = tcx.output_filenames(());
822     let split_name = if tcx.sess.target_can_use_split_dwarf() {
823         output_filenames
824             .split_dwarf_path(
825                 tcx.sess.split_debuginfo(),
826                 tcx.sess.opts.unstable_opts.split_dwarf_kind,
827                 Some(codegen_unit_name),
828             )
829             // We get a path relative to the working directory from split_dwarf_path
830             .map(|f| tcx.sess.source_map().path_mapping().map_prefix(f).0)
831     } else {
832         None
833     }
834     .unwrap_or_default();
835     let split_name = split_name.to_str().unwrap();
836
837     // FIXME(#60020):
838     //
839     //    This should actually be
840     //
841     //        let kind = DebugEmissionKind::from_generic(tcx.sess.opts.debuginfo);
842     //
843     //    That is, we should set LLVM's emission kind to `LineTablesOnly` if
844     //    we are compiling with "limited" debuginfo. However, some of the
845     //    existing tools relied on slightly more debuginfo being generated than
846     //    would be the case with `LineTablesOnly`, and we did not want to break
847     //    these tools in a "drive-by fix", without a good idea or plan about
848     //    what limited debuginfo should exactly look like. So for now we keep
849     //    the emission kind as `FullDebug`.
850     //
851     //    See https://github.com/rust-lang/rust/issues/60020 for details.
852     let kind = DebugEmissionKind::FullDebug;
853     assert!(tcx.sess.opts.debuginfo != DebugInfo::None);
854
855     unsafe {
856         let compile_unit_file = llvm::LLVMRustDIBuilderCreateFile(
857             debug_context.builder,
858             name_in_debuginfo.as_ptr().cast(),
859             name_in_debuginfo.len(),
860             work_dir.as_ptr().cast(),
861             work_dir.len(),
862             llvm::ChecksumKind::None,
863             ptr::null(),
864             0,
865         );
866
867         let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit(
868             debug_context.builder,
869             DW_LANG_RUST,
870             compile_unit_file,
871             producer.as_ptr().cast(),
872             producer.len(),
873             tcx.sess.opts.optimize != config::OptLevel::No,
874             flags.as_ptr().cast(),
875             0,
876             // NB: this doesn't actually have any perceptible effect, it seems. LLVM will instead
877             // put the path supplied to `MCSplitDwarfFile` into the debug info of the final
878             // output(s).
879             split_name.as_ptr().cast(),
880             split_name.len(),
881             kind,
882             0,
883             tcx.sess.opts.unstable_opts.split_dwarf_inlining,
884         );
885
886         if tcx.sess.opts.unstable_opts.profile {
887             let cu_desc_metadata =
888                 llvm::LLVMRustMetadataAsValue(debug_context.llcontext, unit_metadata);
889             let default_gcda_path = &output_filenames.with_extension("gcda");
890             let gcda_path =
891                 tcx.sess.opts.unstable_opts.profile_emit.as_ref().unwrap_or(default_gcda_path);
892
893             let gcov_cu_info = [
894                 path_to_mdstring(debug_context.llcontext, &output_filenames.with_extension("gcno")),
895                 path_to_mdstring(debug_context.llcontext, gcda_path),
896                 cu_desc_metadata,
897             ];
898             let gcov_metadata = llvm::LLVMMDNodeInContext(
899                 debug_context.llcontext,
900                 gcov_cu_info.as_ptr(),
901                 gcov_cu_info.len() as c_uint,
902             );
903
904             let llvm_gcov_ident = cstr!("llvm.gcov");
905             llvm::LLVMAddNamedMetadataOperand(
906                 debug_context.llmod,
907                 llvm_gcov_ident.as_ptr(),
908                 gcov_metadata,
909             );
910         }
911
912         // Insert `llvm.ident` metadata on the wasm targets since that will
913         // get hooked up to the "producer" sections `processed-by` information.
914         if tcx.sess.target.is_like_wasm {
915             let name_metadata = llvm::LLVMMDStringInContext(
916                 debug_context.llcontext,
917                 rustc_producer.as_ptr().cast(),
918                 rustc_producer.as_bytes().len() as c_uint,
919             );
920             llvm::LLVMAddNamedMetadataOperand(
921                 debug_context.llmod,
922                 cstr!("llvm.ident").as_ptr(),
923                 llvm::LLVMMDNodeInContext(debug_context.llcontext, &name_metadata, 1),
924             );
925         }
926
927         return unit_metadata;
928     };
929
930     fn path_to_mdstring<'ll>(llcx: &'ll llvm::Context, path: &Path) -> &'ll Value {
931         let path_str = path_to_c_string(path);
932         unsafe {
933             llvm::LLVMMDStringInContext(
934                 llcx,
935                 path_str.as_ptr(),
936                 path_str.as_bytes().len() as c_uint,
937             )
938         }
939     }
940 }
941
942 /// Creates a `DW_TAG_member` entry inside the DIE represented by the given `type_di_node`.
943 fn build_field_di_node<'ll, 'tcx>(
944     cx: &CodegenCx<'ll, 'tcx>,
945     owner: &'ll DIScope,
946     name: &str,
947     size_and_align: (Size, Align),
948     offset: Size,
949     flags: DIFlags,
950     type_di_node: &'ll DIType,
951 ) -> &'ll DIType {
952     unsafe {
953         llvm::LLVMRustDIBuilderCreateMemberType(
954             DIB(cx),
955             owner,
956             name.as_ptr().cast(),
957             name.len(),
958             unknown_file_metadata(cx),
959             UNKNOWN_LINE_NUMBER,
960             size_and_align.0.bits(),
961             size_and_align.1.bits() as u32,
962             offset.bits(),
963             flags,
964             type_di_node,
965         )
966     }
967 }
968
969 /// Creates the debuginfo node for a Rust struct type. Maybe be a regular struct or a tuple-struct.
970 fn build_struct_type_di_node<'ll, 'tcx>(
971     cx: &CodegenCx<'ll, 'tcx>,
972     unique_type_id: UniqueTypeId<'tcx>,
973 ) -> DINodeCreationResult<'ll> {
974     let struct_type = unique_type_id.expect_ty();
975     let ty::Adt(adt_def, _) = struct_type.kind() else {
976         bug!("build_struct_type_di_node() called with non-struct-type: {:?}", struct_type);
977     };
978     debug_assert!(adt_def.is_struct());
979     let containing_scope = get_namespace_for_item(cx, adt_def.did());
980     let struct_type_and_layout = cx.layout_of(struct_type);
981     let variant_def = adt_def.non_enum_variant();
982
983     type_map::build_type_with_children(
984         cx,
985         type_map::stub(
986             cx,
987             Stub::Struct,
988             unique_type_id,
989             &compute_debuginfo_type_name(cx.tcx, struct_type, false),
990             size_and_align_of(struct_type_and_layout),
991             Some(containing_scope),
992             DIFlags::FlagZero,
993         ),
994         // Fields:
995         |cx, owner| {
996             variant_def
997                 .fields
998                 .iter()
999                 .enumerate()
1000                 .map(|(i, f)| {
1001                     let field_name = if variant_def.ctor_kind == CtorKind::Fn {
1002                         // This is a tuple struct
1003                         tuple_field_name(i)
1004                     } else {
1005                         // This is struct with named fields
1006                         Cow::Borrowed(f.name.as_str())
1007                     };
1008                     let field_layout = struct_type_and_layout.field(cx, i);
1009                     build_field_di_node(
1010                         cx,
1011                         owner,
1012                         &field_name[..],
1013                         (field_layout.size, field_layout.align.abi),
1014                         struct_type_and_layout.fields.offset(i),
1015                         DIFlags::FlagZero,
1016                         type_di_node(cx, field_layout.ty),
1017                     )
1018                 })
1019                 .collect()
1020         },
1021         |cx| build_generic_type_param_di_nodes(cx, struct_type),
1022     )
1023 }
1024
1025 //=-----------------------------------------------------------------------------
1026 // Tuples
1027 //=-----------------------------------------------------------------------------
1028
1029 /// Returns names of captured upvars for closures and generators.
1030 ///
1031 /// Here are some examples:
1032 ///  - `name__field1__field2` when the upvar is captured by value.
1033 ///  - `_ref__name__field` when the upvar is captured by reference.
1034 ///
1035 /// For generators this only contains upvars that are shared by all states.
1036 fn closure_saved_names_of_captured_variables(tcx: TyCtxt<'_>, def_id: DefId) -> SmallVec<String> {
1037     let body = tcx.optimized_mir(def_id);
1038
1039     body.var_debug_info
1040         .iter()
1041         .filter_map(|var| {
1042             let is_ref = match var.value {
1043                 mir::VarDebugInfoContents::Place(place) if place.local == mir::Local::new(1) => {
1044                     // The projection is either `[.., Field, Deref]` or `[.., Field]`. It
1045                     // implies whether the variable is captured by value or by reference.
1046                     matches!(place.projection.last().unwrap(), mir::ProjectionElem::Deref)
1047                 }
1048                 _ => return None,
1049             };
1050             let prefix = if is_ref { "_ref__" } else { "" };
1051             Some(prefix.to_owned() + var.name.as_str())
1052         })
1053         .collect()
1054 }
1055
1056 /// Builds the DW_TAG_member debuginfo nodes for the upvars of a closure or generator.
1057 /// For a generator, this will handle upvars shared by all states.
1058 fn build_upvar_field_di_nodes<'ll, 'tcx>(
1059     cx: &CodegenCx<'ll, 'tcx>,
1060     closure_or_generator_ty: Ty<'tcx>,
1061     closure_or_generator_di_node: &'ll DIType,
1062 ) -> SmallVec<&'ll DIType> {
1063     let (&def_id, up_var_tys) = match closure_or_generator_ty.kind() {
1064         ty::Generator(def_id, substs, _) => {
1065             let upvar_tys: SmallVec<_> = substs.as_generator().prefix_tys().collect();
1066             (def_id, upvar_tys)
1067         }
1068         ty::Closure(def_id, substs) => {
1069             let upvar_tys: SmallVec<_> = substs.as_closure().upvar_tys().collect();
1070             (def_id, upvar_tys)
1071         }
1072         _ => {
1073             bug!(
1074                 "build_upvar_field_di_nodes() called with non-closure-or-generator-type: {:?}",
1075                 closure_or_generator_ty
1076             )
1077         }
1078     };
1079
1080     debug_assert!(
1081         up_var_tys
1082             .iter()
1083             .all(|&t| t == cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), t))
1084     );
1085
1086     let capture_names = closure_saved_names_of_captured_variables(cx.tcx, def_id);
1087     let layout = cx.layout_of(closure_or_generator_ty);
1088
1089     up_var_tys
1090         .into_iter()
1091         .zip(capture_names.iter())
1092         .enumerate()
1093         .map(|(index, (up_var_ty, capture_name))| {
1094             build_field_di_node(
1095                 cx,
1096                 closure_or_generator_di_node,
1097                 capture_name,
1098                 cx.size_and_align_of(up_var_ty),
1099                 layout.fields.offset(index),
1100                 DIFlags::FlagZero,
1101                 type_di_node(cx, up_var_ty),
1102             )
1103         })
1104         .collect()
1105 }
1106
1107 /// Builds the DW_TAG_structure_type debuginfo node for a Rust tuple type.
1108 fn build_tuple_type_di_node<'ll, 'tcx>(
1109     cx: &CodegenCx<'ll, 'tcx>,
1110     unique_type_id: UniqueTypeId<'tcx>,
1111 ) -> DINodeCreationResult<'ll> {
1112     let tuple_type = unique_type_id.expect_ty();
1113     let &ty::Tuple(component_types) = tuple_type.kind() else {
1114         bug!("build_tuple_type_di_node() called with non-tuple-type: {:?}", tuple_type)
1115     };
1116
1117     let tuple_type_and_layout = cx.layout_of(tuple_type);
1118     let type_name = compute_debuginfo_type_name(cx.tcx, tuple_type, false);
1119
1120     type_map::build_type_with_children(
1121         cx,
1122         type_map::stub(
1123             cx,
1124             Stub::Struct,
1125             unique_type_id,
1126             &type_name,
1127             size_and_align_of(tuple_type_and_layout),
1128             NO_SCOPE_METADATA,
1129             DIFlags::FlagZero,
1130         ),
1131         // Fields:
1132         |cx, tuple_di_node| {
1133             component_types
1134                 .into_iter()
1135                 .enumerate()
1136                 .map(|(index, component_type)| {
1137                     build_field_di_node(
1138                         cx,
1139                         tuple_di_node,
1140                         &tuple_field_name(index),
1141                         cx.size_and_align_of(component_type),
1142                         tuple_type_and_layout.fields.offset(index),
1143                         DIFlags::FlagZero,
1144                         type_di_node(cx, component_type),
1145                     )
1146                 })
1147                 .collect()
1148         },
1149         NO_GENERICS,
1150     )
1151 }
1152
1153 /// Builds the debuginfo node for a closure environment.
1154 fn build_closure_env_di_node<'ll, 'tcx>(
1155     cx: &CodegenCx<'ll, 'tcx>,
1156     unique_type_id: UniqueTypeId<'tcx>,
1157 ) -> DINodeCreationResult<'ll> {
1158     let closure_env_type = unique_type_id.expect_ty();
1159     let &ty::Closure(def_id, _substs) = closure_env_type.kind() else {
1160         bug!("build_closure_env_di_node() called with non-closure-type: {:?}", closure_env_type)
1161     };
1162     let containing_scope = get_namespace_for_item(cx, def_id);
1163     let type_name = compute_debuginfo_type_name(cx.tcx, closure_env_type, false);
1164
1165     type_map::build_type_with_children(
1166         cx,
1167         type_map::stub(
1168             cx,
1169             Stub::Struct,
1170             unique_type_id,
1171             &type_name,
1172             cx.size_and_align_of(closure_env_type),
1173             Some(containing_scope),
1174             DIFlags::FlagZero,
1175         ),
1176         // Fields:
1177         |cx, owner| build_upvar_field_di_nodes(cx, closure_env_type, owner),
1178         NO_GENERICS,
1179     )
1180 }
1181
1182 /// Build the debuginfo node for a Rust `union` type.
1183 fn build_union_type_di_node<'ll, 'tcx>(
1184     cx: &CodegenCx<'ll, 'tcx>,
1185     unique_type_id: UniqueTypeId<'tcx>,
1186 ) -> DINodeCreationResult<'ll> {
1187     let union_type = unique_type_id.expect_ty();
1188     let (union_def_id, variant_def) = match union_type.kind() {
1189         ty::Adt(def, _) => (def.did(), def.non_enum_variant()),
1190         _ => bug!("build_union_type_di_node on a non-ADT"),
1191     };
1192     let containing_scope = get_namespace_for_item(cx, union_def_id);
1193     let union_ty_and_layout = cx.layout_of(union_type);
1194     let type_name = compute_debuginfo_type_name(cx.tcx, union_type, false);
1195
1196     type_map::build_type_with_children(
1197         cx,
1198         type_map::stub(
1199             cx,
1200             Stub::Union,
1201             unique_type_id,
1202             &type_name,
1203             size_and_align_of(union_ty_and_layout),
1204             Some(containing_scope),
1205             DIFlags::FlagZero,
1206         ),
1207         // Fields:
1208         |cx, owner| {
1209             variant_def
1210                 .fields
1211                 .iter()
1212                 .enumerate()
1213                 .map(|(i, f)| {
1214                     let field_layout = union_ty_and_layout.field(cx, i);
1215                     build_field_di_node(
1216                         cx,
1217                         owner,
1218                         f.name.as_str(),
1219                         size_and_align_of(field_layout),
1220                         Size::ZERO,
1221                         DIFlags::FlagZero,
1222                         type_di_node(cx, field_layout.ty),
1223                     )
1224                 })
1225                 .collect()
1226         },
1227         // Generics:
1228         |cx| build_generic_type_param_di_nodes(cx, union_type),
1229     )
1230 }
1231
1232 // FIXME(eddyb) maybe precompute this? Right now it's computed once
1233 // per generator monomorphization, but it doesn't depend on substs.
1234 fn generator_layout_and_saved_local_names<'tcx>(
1235     tcx: TyCtxt<'tcx>,
1236     def_id: DefId,
1237 ) -> (&'tcx GeneratorLayout<'tcx>, IndexVec<mir::GeneratorSavedLocal, Option<Symbol>>) {
1238     let body = tcx.optimized_mir(def_id);
1239     let generator_layout = body.generator_layout().unwrap();
1240     let mut generator_saved_local_names = IndexVec::from_elem(None, &generator_layout.field_tys);
1241
1242     let state_arg = mir::Local::new(1);
1243     for var in &body.var_debug_info {
1244         let mir::VarDebugInfoContents::Place(place) = &var.value else { continue };
1245         if place.local != state_arg {
1246             continue;
1247         }
1248         match place.projection[..] {
1249             [
1250                 // Deref of the `Pin<&mut Self>` state argument.
1251                 mir::ProjectionElem::Field(..),
1252                 mir::ProjectionElem::Deref,
1253                 // Field of a variant of the state.
1254                 mir::ProjectionElem::Downcast(_, variant),
1255                 mir::ProjectionElem::Field(field, _),
1256             ] => {
1257                 let name = &mut generator_saved_local_names
1258                     [generator_layout.variant_fields[variant][field]];
1259                 if name.is_none() {
1260                     name.replace(var.name);
1261                 }
1262             }
1263             _ => {}
1264         }
1265     }
1266     (generator_layout, generator_saved_local_names)
1267 }
1268
1269 /// Computes the type parameters for a type, if any, for the given metadata.
1270 fn build_generic_type_param_di_nodes<'ll, 'tcx>(
1271     cx: &CodegenCx<'ll, 'tcx>,
1272     ty: Ty<'tcx>,
1273 ) -> SmallVec<&'ll DIType> {
1274     if let ty::Adt(def, substs) = *ty.kind() {
1275         if substs.types().next().is_some() {
1276             let generics = cx.tcx.generics_of(def.did());
1277             let names = get_parameter_names(cx, generics);
1278             let template_params: SmallVec<_> = iter::zip(substs, names)
1279                 .filter_map(|(kind, name)| {
1280                     if let GenericArgKind::Type(ty) = kind.unpack() {
1281                         let actual_type =
1282                             cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), ty);
1283                         let actual_type_di_node = type_di_node(cx, actual_type);
1284                         let name = name.as_str();
1285                         Some(unsafe {
1286                             llvm::LLVMRustDIBuilderCreateTemplateTypeParameter(
1287                                 DIB(cx),
1288                                 None,
1289                                 name.as_ptr().cast(),
1290                                 name.len(),
1291                                 actual_type_di_node,
1292                             )
1293                         })
1294                     } else {
1295                         None
1296                     }
1297                 })
1298                 .collect();
1299
1300             return template_params;
1301         }
1302     }
1303
1304     return smallvec![];
1305
1306     fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec<Symbol> {
1307         let mut names = generics
1308             .parent
1309             .map_or_else(Vec::new, |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id)));
1310         names.extend(generics.params.iter().map(|param| param.name));
1311         names
1312     }
1313 }
1314
1315 /// Creates debug information for the given global variable.
1316 ///
1317 /// Adds the created debuginfo nodes directly to the crate's IR.
1318 pub fn build_global_var_di_node<'ll>(cx: &CodegenCx<'ll, '_>, def_id: DefId, global: &'ll Value) {
1319     if cx.dbg_cx.is_none() {
1320         return;
1321     }
1322
1323     // Only create type information if full debuginfo is enabled
1324     if cx.sess().opts.debuginfo != DebugInfo::Full {
1325         return;
1326     }
1327
1328     let tcx = cx.tcx;
1329
1330     // We may want to remove the namespace scope if we're in an extern block (see
1331     // https://github.com/rust-lang/rust/pull/46457#issuecomment-351750952).
1332     let var_scope = get_namespace_for_item(cx, def_id);
1333     let span = tcx.def_span(def_id);
1334
1335     let (file_metadata, line_number) = if !span.is_dummy() {
1336         let loc = cx.lookup_debug_loc(span.lo());
1337         (file_metadata(cx, &loc.file), loc.line)
1338     } else {
1339         (unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
1340     };
1341
1342     let is_local_to_unit = is_node_local_to_unit(cx, def_id);
1343     let variable_type = Instance::mono(cx.tcx, def_id).ty(cx.tcx, ty::ParamEnv::reveal_all());
1344     let type_di_node = type_di_node(cx, variable_type);
1345     let var_name = tcx.item_name(def_id);
1346     let var_name = var_name.as_str();
1347     let linkage_name = mangled_name_of_instance(cx, Instance::mono(tcx, def_id)).name;
1348     // When empty, linkage_name field is omitted,
1349     // which is what we want for no_mangle statics
1350     let linkage_name = if var_name == linkage_name { "" } else { linkage_name };
1351
1352     let global_align = cx.align_of(variable_type);
1353
1354     unsafe {
1355         llvm::LLVMRustDIBuilderCreateStaticVariable(
1356             DIB(cx),
1357             Some(var_scope),
1358             var_name.as_ptr().cast(),
1359             var_name.len(),
1360             linkage_name.as_ptr().cast(),
1361             linkage_name.len(),
1362             file_metadata,
1363             line_number,
1364             type_di_node,
1365             is_local_to_unit,
1366             global,
1367             None,
1368             global_align.bits() as u32,
1369         );
1370     }
1371 }
1372
1373 /// Generates LLVM debuginfo for a vtable.
1374 ///
1375 /// The vtable type looks like a struct with a field for each function pointer and super-trait
1376 /// pointer it contains (plus the `size` and `align` fields).
1377 ///
1378 /// Except for `size`, `align`, and `drop_in_place`, the field names don't try to mirror
1379 /// the name of the method they implement. This can be implemented in the future once there
1380 /// is a proper disambiguation scheme for dealing with methods from different traits that have
1381 /// the same name.
1382 fn build_vtable_type_di_node<'ll, 'tcx>(
1383     cx: &CodegenCx<'ll, 'tcx>,
1384     ty: Ty<'tcx>,
1385     poly_trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
1386 ) -> &'ll DIType {
1387     let tcx = cx.tcx;
1388
1389     let vtable_entries = if let Some(poly_trait_ref) = poly_trait_ref {
1390         let trait_ref = poly_trait_ref.with_self_ty(tcx, ty);
1391         let trait_ref = tcx.erase_regions(trait_ref);
1392
1393         tcx.vtable_entries(trait_ref)
1394     } else {
1395         TyCtxt::COMMON_VTABLE_ENTRIES
1396     };
1397
1398     // All function pointers are described as opaque pointers. This could be improved in the future
1399     // by describing them as actual function pointers.
1400     let void_pointer_ty = tcx.mk_imm_ptr(tcx.types.unit);
1401     let void_pointer_type_di_node = type_di_node(cx, void_pointer_ty);
1402     let usize_di_node = type_di_node(cx, tcx.types.usize);
1403     let (pointer_size, pointer_align) = cx.size_and_align_of(void_pointer_ty);
1404     // If `usize` is not pointer-sized and -aligned then the size and alignment computations
1405     // for the vtable as a whole would be wrong. Let's make sure this holds even on weird
1406     // platforms.
1407     assert_eq!(cx.size_and_align_of(tcx.types.usize), (pointer_size, pointer_align));
1408
1409     let vtable_type_name =
1410         compute_debuginfo_vtable_name(cx.tcx, ty, poly_trait_ref, VTableNameKind::Type);
1411     let unique_type_id = UniqueTypeId::for_vtable_ty(tcx, ty, poly_trait_ref);
1412     let size = pointer_size * vtable_entries.len() as u64;
1413
1414     // This gets mapped to a DW_AT_containing_type attribute which allows GDB to correlate
1415     // the vtable to the type it is for.
1416     let vtable_holder = type_di_node(cx, ty);
1417
1418     build_type_with_children(
1419         cx,
1420         type_map::stub(
1421             cx,
1422             Stub::VTableTy { vtable_holder },
1423             unique_type_id,
1424             &vtable_type_name,
1425             (size, pointer_align),
1426             NO_SCOPE_METADATA,
1427             DIFlags::FlagArtificial,
1428         ),
1429         |cx, vtable_type_di_node| {
1430             vtable_entries
1431                 .iter()
1432                 .enumerate()
1433                 .filter_map(|(index, vtable_entry)| {
1434                     let (field_name, field_type_di_node) = match vtable_entry {
1435                         ty::VtblEntry::MetadataDropInPlace => {
1436                             ("drop_in_place".to_string(), void_pointer_type_di_node)
1437                         }
1438                         ty::VtblEntry::Method(_) => {
1439                             // Note: This code does not try to give a proper name to each method
1440                             //       because their might be multiple methods with the same name
1441                             //       (coming from different traits).
1442                             (format!("__method{}", index), void_pointer_type_di_node)
1443                         }
1444                         ty::VtblEntry::TraitVPtr(_) => {
1445                             (format!("__super_trait_ptr{}", index), void_pointer_type_di_node)
1446                         }
1447                         ty::VtblEntry::MetadataAlign => ("align".to_string(), usize_di_node),
1448                         ty::VtblEntry::MetadataSize => ("size".to_string(), usize_di_node),
1449                         ty::VtblEntry::Vacant => return None,
1450                     };
1451
1452                     let field_offset = pointer_size * index as u64;
1453
1454                     Some(build_field_di_node(
1455                         cx,
1456                         vtable_type_di_node,
1457                         &field_name,
1458                         (pointer_size, pointer_align),
1459                         field_offset,
1460                         DIFlags::FlagZero,
1461                         field_type_di_node,
1462                     ))
1463                 })
1464                 .collect()
1465         },
1466         NO_GENERICS,
1467     )
1468     .di_node
1469 }
1470
1471 fn vcall_visibility_metadata<'ll, 'tcx>(
1472     cx: &CodegenCx<'ll, 'tcx>,
1473     ty: Ty<'tcx>,
1474     trait_ref: Option<PolyExistentialTraitRef<'tcx>>,
1475     vtable: &'ll Value,
1476 ) {
1477     enum VCallVisibility {
1478         Public = 0,
1479         LinkageUnit = 1,
1480         TranslationUnit = 2,
1481     }
1482
1483     let Some(trait_ref) = trait_ref else { return };
1484
1485     let trait_ref_self = trait_ref.with_self_ty(cx.tcx, ty);
1486     let trait_ref_self = cx.tcx.erase_regions(trait_ref_self);
1487     let trait_def_id = trait_ref_self.def_id();
1488     let trait_vis = cx.tcx.visibility(trait_def_id);
1489
1490     let cgus = cx.sess().codegen_units();
1491     let single_cgu = cgus == 1;
1492
1493     let lto = cx.sess().lto();
1494
1495     // Since LLVM requires full LTO for the virtual function elimination optimization to apply,
1496     // only the `Lto::Fat` cases are relevant currently.
1497     let vcall_visibility = match (lto, trait_vis, single_cgu) {
1498         // If there is not LTO and the visibility in public, we have to assume that the vtable can
1499         // be seen from anywhere. With multiple CGUs, the vtable is quasi-public.
1500         (Lto::No | Lto::ThinLocal, Visibility::Public, _)
1501         | (Lto::No, Visibility::Restricted(_), false) => VCallVisibility::Public,
1502         // With LTO and a quasi-public visibility, the usages of the functions of the vtable are
1503         // all known by the `LinkageUnit`.
1504         // FIXME: LLVM only supports this optimization for `Lto::Fat` currently. Once it also
1505         // supports `Lto::Thin` the `VCallVisibility` may have to be adjusted for those.
1506         (Lto::Fat | Lto::Thin, Visibility::Public, _)
1507         | (Lto::ThinLocal | Lto::Thin | Lto::Fat, Visibility::Restricted(_), false) => {
1508             VCallVisibility::LinkageUnit
1509         }
1510         // If there is only one CGU, private vtables can only be seen by that CGU/translation unit
1511         // and therefore we know of all usages of functions in the vtable.
1512         (_, Visibility::Restricted(_), true) => VCallVisibility::TranslationUnit,
1513     };
1514
1515     let trait_ref_typeid = typeid_for_trait_ref(cx.tcx, trait_ref);
1516
1517     unsafe {
1518         let typeid = llvm::LLVMMDStringInContext(
1519             cx.llcx,
1520             trait_ref_typeid.as_ptr() as *const c_char,
1521             trait_ref_typeid.as_bytes().len() as c_uint,
1522         );
1523         let v = [cx.const_usize(0), typeid];
1524         llvm::LLVMRustGlobalAddMetadata(
1525             vtable,
1526             llvm::MD_type as c_uint,
1527             llvm::LLVMValueAsMetadata(llvm::LLVMMDNodeInContext(
1528                 cx.llcx,
1529                 v.as_ptr(),
1530                 v.len() as c_uint,
1531             )),
1532         );
1533         let vcall_visibility = llvm::LLVMValueAsMetadata(cx.const_u64(vcall_visibility as u64));
1534         let vcall_visibility_metadata = llvm::LLVMMDNodeInContext2(cx.llcx, &vcall_visibility, 1);
1535         llvm::LLVMGlobalSetMetadata(
1536             vtable,
1537             llvm::MetadataType::MD_vcall_visibility as c_uint,
1538             vcall_visibility_metadata,
1539         );
1540     }
1541 }
1542
1543 /// Creates debug information for the given vtable, which is for the
1544 /// given type.
1545 ///
1546 /// Adds the created metadata nodes directly to the crate's IR.
1547 pub fn create_vtable_di_node<'ll, 'tcx>(
1548     cx: &CodegenCx<'ll, 'tcx>,
1549     ty: Ty<'tcx>,
1550     poly_trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
1551     vtable: &'ll Value,
1552 ) {
1553     // FIXME(flip1995): The virtual function elimination optimization only works with full LTO in
1554     // LLVM at the moment.
1555     if cx.sess().opts.unstable_opts.virtual_function_elimination && cx.sess().lto() == Lto::Fat {
1556         vcall_visibility_metadata(cx, ty, poly_trait_ref, vtable);
1557     }
1558
1559     if cx.dbg_cx.is_none() {
1560         return;
1561     }
1562
1563     // Only create type information if full debuginfo is enabled
1564     if cx.sess().opts.debuginfo != DebugInfo::Full {
1565         return;
1566     }
1567
1568     let vtable_name =
1569         compute_debuginfo_vtable_name(cx.tcx, ty, poly_trait_ref, VTableNameKind::GlobalVariable);
1570     let vtable_type_di_node = build_vtable_type_di_node(cx, ty, poly_trait_ref);
1571     let linkage_name = "";
1572
1573     unsafe {
1574         llvm::LLVMRustDIBuilderCreateStaticVariable(
1575             DIB(cx),
1576             NO_SCOPE_METADATA,
1577             vtable_name.as_ptr().cast(),
1578             vtable_name.len(),
1579             linkage_name.as_ptr().cast(),
1580             linkage_name.len(),
1581             unknown_file_metadata(cx),
1582             UNKNOWN_LINE_NUMBER,
1583             vtable_type_di_node,
1584             true,
1585             vtable,
1586             None,
1587             0,
1588         );
1589     }
1590 }
1591
1592 /// Creates an "extension" of an existing `DIScope` into another file.
1593 pub fn extend_scope_to_file<'ll>(
1594     cx: &CodegenCx<'ll, '_>,
1595     scope_metadata: &'ll DIScope,
1596     file: &SourceFile,
1597 ) -> &'ll DILexicalBlock {
1598     let file_metadata = file_metadata(cx, file);
1599     unsafe { llvm::LLVMRustDIBuilderCreateLexicalBlockFile(DIB(cx), scope_metadata, file_metadata) }
1600 }
1601
1602 pub fn tuple_field_name(field_index: usize) -> Cow<'static, str> {
1603     const TUPLE_FIELD_NAMES: [&'static str; 16] = [
1604         "__0", "__1", "__2", "__3", "__4", "__5", "__6", "__7", "__8", "__9", "__10", "__11",
1605         "__12", "__13", "__14", "__15",
1606     ];
1607     TUPLE_FIELD_NAMES
1608         .get(field_index)
1609         .map(|s| Cow::from(*s))
1610         .unwrap_or_else(|| Cow::from(format!("__{}", field_index)))
1611 }