rustc_codegen_llvm/debuginfo/
metadata.rs

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