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