rustc_codegen_llvm/debuginfo/
metadata.rs

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