Skip to main content

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::{assert_matches, iter, ptr};
6
7use libc::{c_longlong, c_uint};
8use rustc_abi::{Align, Layout, NumScalableVectors, Size};
9use rustc_codegen_ssa::debuginfo::type_names::{VTableNameKind, cpp_like_debuginfo};
10use rustc_codegen_ssa::traits::*;
11use rustc_hir::def::{CtorKind, DefKind};
12use rustc_hir::def_id::{DefId, LOCAL_CRATE};
13use rustc_middle::bug;
14use rustc_middle::ty::layout::{
15    HasTypingEnv, LayoutOf, TyAndLayout, WIDE_PTR_ADDR, WIDE_PTR_EXTRA,
16};
17use rustc_middle::ty::{
18    self, AdtDef, AdtKind, CoroutineArgsExt, ExistentialTraitRef, Instance, Ty, TyCtxt,
19    Unnormalized, 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::{Arch, 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, create_DIArray, 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        ::rustc_middle::util::bug::bug_fmt(format_args!("build_fixed_size_array_di_node() called with non-ty::Array type `{0:?}`",
        array_type))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    if let Some(di_node) =
        debug_context(cx).type_map.di_node_for_unique_id(unique_type_id) {
    return DINodeCreationResult::new(di_node, true);
};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    match (&cx.size_and_align_of(ptr_type),
        &cx.size_and_align_of(Ty::new_mut_ptr(cx.tcx, pointee_type))) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};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 = match pointee_type.kind() {
156        // `&[T]` will look like `{ data_ptr: *const T, length: usize }`
157        ty::Slice(element_type) => type_di_node(cx, *element_type),
158        // `&str` will look like `{ data_ptr: *const u8, length: usize }`
159        ty::Str => type_di_node(cx, cx.tcx.types.u8),
160
161        // `&dyn K` will look like `{ pointer: _, vtable: _}`
162        // any Adt `Foo` containing an unsized type (eg `&[_]` or `&dyn _`)
163        //   will look like `{ data_ptr: *const Foo, length: usize }`
164        // and thin pointers `&Foo` will just look like `*const Foo`.
165        //
166        // in all those cases, we just use the pointee_type
167        _ => type_di_node(cx, pointee_type),
168    };
169
170    if let Some(di_node) =
        debug_context(cx).type_map.di_node_for_unique_id(unique_type_id) {
    return DINodeCreationResult::new(di_node, true);
};return_if_di_node_created_in_meantime!(cx, unique_type_id);
171
172    let data_layout = &cx.tcx.data_layout;
173    let pointer_size = data_layout.pointer_size();
174    let pointer_align = data_layout.pointer_align();
175    let ptr_type_debuginfo_name = compute_debuginfo_type_name(cx.tcx, ptr_type, true);
176
177    match wide_pointer_kind(cx, pointee_type) {
178        None => {
179            // This is a thin pointer. Create a regular pointer type and give it the correct name.
180            match (&(pointer_size, pointer_align.abi), &cx.size_and_align_of(ptr_type)) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::Some(format_args!("ptr_type={0}, pointee_type={1}",
                        ptr_type, pointee_type)));
        }
    }
};assert_eq!(
181                (pointer_size, pointer_align.abi),
182                cx.size_and_align_of(ptr_type),
183                "ptr_type={ptr_type}, pointee_type={pointee_type}",
184            );
185
186            let di_node = create_pointer_type(
187                cx,
188                pointee_type_di_node,
189                pointer_size,
190                pointer_align.abi,
191                &ptr_type_debuginfo_name,
192            );
193
194            DINodeCreationResult { di_node, already_stored_in_typemap: false }
195        }
196        Some(wide_pointer_kind) => {
197            type_map::build_type_with_children(
198                cx,
199                type_map::stub(
200                    cx,
201                    Stub::Struct,
202                    unique_type_id,
203                    &ptr_type_debuginfo_name,
204                    None,
205                    cx.size_and_align_of(ptr_type),
206                    NO_SCOPE_METADATA,
207                    DIFlags::FlagZero,
208                ),
209                |cx, owner| {
210                    // FIXME: If this wide pointer is a `Box` then we don't want to use its
211                    //        type layout and instead use the layout of the raw pointer inside
212                    //        of it.
213                    //        The proper way to handle this is to not treat Box as a pointer
214                    //        at all and instead emit regular struct debuginfo for it. We just
215                    //        need to make sure that we don't break existing debuginfo consumers
216                    //        by doing that (at least not without a warning period).
217                    let layout_type = if ptr_type.is_box() {
218                        // The assertion at the start of this function ensures we have a ZST
219                        // allocator. We'll make debuginfo "skip" all ZST allocators, not just the
220                        // default allocator.
221                        Ty::new_mut_ptr(cx.tcx, pointee_type)
222                    } else {
223                        ptr_type
224                    };
225
226                    let layout = cx.layout_of(layout_type);
227                    let addr_field = layout.field(cx, WIDE_PTR_ADDR);
228                    let extra_field = layout.field(cx, WIDE_PTR_EXTRA);
229
230                    let (addr_field_name, extra_field_name) = match wide_pointer_kind {
231                        WidePtrKind::Dyn => ("pointer", "vtable"),
232                        WidePtrKind::Slice => ("data_ptr", "length"),
233                    };
234
235                    match (&WIDE_PTR_ADDR, &0) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(WIDE_PTR_ADDR, 0);
236                    match (&WIDE_PTR_EXTRA, &1) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(WIDE_PTR_EXTRA, 1);
237
238                    // The data pointer type is a regular, thin pointer, regardless of whether this
239                    // is a slice or a trait object.
240                    let data_ptr_type_di_node = create_pointer_type(
241                        cx,
242                        pointee_type_di_node,
243                        addr_field.size,
244                        addr_field.align.abi,
245                        "",
246                    );
247
248                    {
    let count = 0usize + 1usize + 1usize;
    let mut vec = ::smallvec::SmallVec::new();
    if count <= vec.inline_size() {
        vec.push(build_field_di_node(cx, owner, addr_field_name, addr_field,
                layout.fields.offset(WIDE_PTR_ADDR), DIFlags::FlagZero,
                data_ptr_type_di_node, None));
        vec.push(build_field_di_node(cx, owner, extra_field_name, extra_field,
                layout.fields.offset(WIDE_PTR_EXTRA), DIFlags::FlagZero,
                type_di_node(cx, extra_field.ty), None));
        vec
    } else {
        ::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                    [build_field_di_node(cx, owner, addr_field_name, addr_field,
                                layout.fields.offset(WIDE_PTR_ADDR), DIFlags::FlagZero,
                                data_ptr_type_di_node, None),
                            build_field_di_node(cx, owner, extra_field_name,
                                extra_field, layout.fields.offset(WIDE_PTR_EXTRA),
                                DIFlags::FlagZero, type_di_node(cx, extra_field.ty),
                                None)])))
    }
}smallvec![
249                        build_field_di_node(
250                            cx,
251                            owner,
252                            addr_field_name,
253                            addr_field,
254                            layout.fields.offset(WIDE_PTR_ADDR),
255                            DIFlags::FlagZero,
256                            data_ptr_type_di_node,
257                            None,
258                        ),
259                        build_field_di_node(
260                            cx,
261                            owner,
262                            extra_field_name,
263                            extra_field,
264                            layout.fields.offset(WIDE_PTR_EXTRA),
265                            DIFlags::FlagZero,
266                            type_di_node(cx, extra_field.ty),
267                            None,
268                        ),
269                    ]
270                },
271                NO_GENERICS,
272            )
273        }
274    }
275}
276
277fn build_subroutine_type_di_node<'ll, 'tcx>(
278    cx: &CodegenCx<'ll, 'tcx>,
279    unique_type_id: UniqueTypeId<'tcx>,
280) -> DINodeCreationResult<'ll> {
281    // It's possible to create a self-referential type in Rust by using 'impl trait':
282    //
283    // fn foo() -> impl Copy { foo }
284    //
285    // Unfortunately LLVM's API does not allow us to create recursive subroutine types.
286    // In order to work around that restriction we place a marker type in the type map,
287    // before creating the actual type. If the actual type is recursive, it will hit the
288    // marker type. So we end up with a type that looks like
289    //
290    // fn foo() -> <recursive_type>
291    //
292    // Once that is created, we replace the marker in the typemap with the actual type.
293    debug_context(cx)
294        .type_map
295        .unique_id_to_di_node
296        .borrow_mut()
297        .insert(unique_type_id, recursion_marker_type_di_node(cx));
298
299    let fn_ty = unique_type_id.expect_ty();
300    let signature =
301        cx.tcx.normalize_erasing_late_bound_regions(cx.typing_env(), fn_ty.fn_sig(cx.tcx));
302
303    let signature_di_nodes: SmallVec<_> = iter::once(
304        // return type
305        match signature.output().kind() {
306            ty::Tuple(tys) if tys.is_empty() => {
307                // this is a "void" function
308                None
309            }
310            _ => Some(type_di_node(cx, signature.output())),
311        },
312    )
313    .chain(
314        // regular arguments
315        signature.inputs().iter().map(|&argument_type| Some(type_di_node(cx, argument_type))),
316    )
317    .collect();
318
319    debug_context(cx).type_map.unique_id_to_di_node.borrow_mut().remove(&unique_type_id);
320
321    let fn_di_node = create_subroutine_type(cx, &signature_di_nodes[..]);
322
323    // This is actually a function pointer, so wrap it in pointer DI.
324    let name = compute_debuginfo_type_name(cx.tcx, fn_ty, false);
325    let (size, align) = match fn_ty.kind() {
326        ty::FnDef(..) => (Size::ZERO, Align::ONE),
327        ty::FnPtr(..) => {
328            (cx.tcx.data_layout.pointer_size(), cx.tcx.data_layout.pointer_align().abi)
329        }
330        _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
331    };
332    let di_node = create_pointer_type(cx, fn_di_node, size, align, &name);
333
334    DINodeCreationResult::new(di_node, false)
335}
336
337pub(super) fn create_subroutine_type<'ll>(
338    cx: &CodegenCx<'ll, '_>,
339    signature: &[Option<&'ll llvm::Metadata>],
340) -> &'ll DICompositeType {
341    unsafe {
342        llvm::LLVMDIBuilderCreateSubroutineType(
343            DIB(cx),
344            None, // ("File" is ignored and has no effect)
345            signature.as_ptr(),
346            signature.len() as c_uint,
347            DIFlags::FlagZero, // (default value)
348        )
349    }
350}
351
352fn create_pointer_type<'ll>(
353    cx: &CodegenCx<'ll, '_>,
354    pointee_ty: &'ll llvm::Metadata,
355    size: Size,
356    align: Align,
357    name: &str,
358) -> &'ll llvm::Metadata {
359    unsafe {
360        llvm::LLVMDIBuilderCreatePointerType(
361            DIB(cx),
362            pointee_ty,
363            size.bits(),
364            align.bits() as u32,
365            0, // Ignore DWARF address space.
366            name.as_ptr(),
367            name.len(),
368        )
369    }
370}
371
372/// Create debuginfo for `dyn SomeTrait` types. Currently these are empty structs
373/// we with the correct type name (e.g. "dyn SomeTrait<Foo, Item=u32> + Sync").
374fn build_dyn_type_di_node<'ll, 'tcx>(
375    cx: &CodegenCx<'ll, 'tcx>,
376    dyn_type: Ty<'tcx>,
377    unique_type_id: UniqueTypeId<'tcx>,
378) -> DINodeCreationResult<'ll> {
379    if let ty::Dynamic(..) = dyn_type.kind() {
380        let type_name = compute_debuginfo_type_name(cx.tcx, dyn_type, true);
381        type_map::build_type_with_children(
382            cx,
383            type_map::stub(
384                cx,
385                Stub::Struct,
386                unique_type_id,
387                &type_name,
388                None,
389                cx.size_and_align_of(dyn_type),
390                NO_SCOPE_METADATA,
391                DIFlags::FlagZero,
392            ),
393            |_, _| ::smallvec::SmallVec::new()smallvec![],
394            NO_GENERICS,
395        )
396    } else {
397        ::rustc_middle::util::bug::bug_fmt(format_args!("Only ty::Dynamic is valid for build_dyn_type_di_node(). Found {0:?} instead.",
        dyn_type))bug!(
398            "Only ty::Dynamic is valid for build_dyn_type_di_node(). Found {:?} instead.",
399            dyn_type
400        )
401    }
402}
403
404/// Create debuginfo for `[T]` and `str`. These are unsized.
405fn build_slice_type_di_node<'ll, 'tcx>(
406    cx: &CodegenCx<'ll, 'tcx>,
407    slice_type: Ty<'tcx>,
408    unique_type_id: UniqueTypeId<'tcx>,
409    span: Span,
410) -> DINodeCreationResult<'ll> {
411    let element_type = match slice_type.kind() {
412        ty::Slice(element_type) => *element_type,
413        ty::Str => cx.tcx.types.u8,
414        _ => {
415            ::rustc_middle::util::bug::bug_fmt(format_args!("Only ty::Slice is valid for build_slice_type_di_node(). Found {0:?} instead.",
        slice_type))bug!(
416                "Only ty::Slice is valid for build_slice_type_di_node(). Found {:?} instead.",
417                slice_type
418            )
419        }
420    };
421
422    let element_type_di_node = type_di_node(cx, element_type);
423    if let Some(di_node) =
        debug_context(cx).type_map.di_node_for_unique_id(unique_type_id) {
    return DINodeCreationResult::new(di_node, true);
};return_if_di_node_created_in_meantime!(cx, unique_type_id);
424    let (size, align) = cx.spanned_size_and_align_of(slice_type, span);
425    let subrange = unsafe { llvm::LLVMDIBuilderGetOrCreateSubrange(DIB(cx), 0, -1) };
426    let subscripts = &[subrange];
427    let di_node = unsafe {
428        llvm::LLVMDIBuilderCreateArrayType(
429            DIB(cx),
430            size.bits(),
431            align.bits() as u32,
432            element_type_di_node,
433            subscripts.as_ptr(),
434            subscripts.len() as c_uint,
435        )
436    };
437    DINodeCreationResult { di_node, already_stored_in_typemap: false }
438}
439
440/// Get the debuginfo node for the given type.
441///
442/// This function will look up the debuginfo node in the TypeMap. If it can't find it, it
443/// will create the node by dispatching to the corresponding `build_*_di_node()` function.
444pub(crate) fn type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) -> &'ll DIType {
445    spanned_type_di_node(cx, t, DUMMY_SP)
446}
447
448pub(crate) fn spanned_type_di_node<'ll, 'tcx>(
449    cx: &CodegenCx<'ll, 'tcx>,
450    t: Ty<'tcx>,
451    span: Span,
452) -> &'ll DIType {
453    let unique_type_id = UniqueTypeId::for_ty(cx.tcx, t);
454
455    if let Some(existing_di_node) = debug_context(cx).type_map.di_node_for_unique_id(unique_type_id)
456    {
457        return existing_di_node;
458    }
459
460    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs:460",
                        "rustc_codegen_llvm::debuginfo::metadata",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs"),
                        ::tracing_core::__macro_support::Option::Some(460u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::debuginfo::metadata"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("type_di_node: {0:?} kind: {1:?}",
                                                    t, t.kind()) as &dyn Value))])
            });
    } else { ; }
};debug!("type_di_node: {:?} kind: {:?}", t, t.kind());
461
462    let DINodeCreationResult { di_node, already_stored_in_typemap } = match *t.kind() {
463        ty::Never | ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) => {
464            build_basic_type_di_node(cx, t)
465        }
466        ty::Tuple(elements) if elements.is_empty() => build_basic_type_di_node(cx, t),
467        ty::Array(..) => build_fixed_size_array_di_node(cx, unique_type_id, t, span),
468        ty::Slice(_) | ty::Str => build_slice_type_di_node(cx, t, unique_type_id, span),
469        ty::Dynamic(..) => build_dyn_type_di_node(cx, t, unique_type_id),
470        ty::Foreign(..) => build_foreign_type_di_node(cx, t, unique_type_id),
471        ty::RawPtr(pointee_type, _) | ty::Ref(_, pointee_type, _) => {
472            build_pointer_or_reference_di_node(cx, t, pointee_type, unique_type_id)
473        }
474        // Some `Box` are newtyped pointers, make debuginfo aware of that.
475        // Only works if the allocator argument is a 1-ZST and hence irrelevant for layout
476        // (or if there is no allocator argument).
477        ty::Adt(def, args)
478            if def.is_box()
479                && args.get(1).is_none_or(|arg| cx.layout_of(arg.expect_ty()).is_1zst()) =>
480        {
481            build_pointer_or_reference_di_node(cx, t, t.expect_boxed_ty(), unique_type_id)
482        }
483        ty::FnDef(..) | ty::FnPtr(..) => build_subroutine_type_di_node(cx, unique_type_id),
484        ty::Closure(..) => build_closure_env_di_node(cx, unique_type_id),
485        ty::CoroutineClosure(..) => build_closure_env_di_node(cx, unique_type_id),
486        ty::Coroutine(..) => enums::build_coroutine_di_node(cx, unique_type_id),
487        ty::Adt(def, ..) => match def.adt_kind() {
488            AdtKind::Struct => build_struct_type_di_node(cx, unique_type_id, span),
489            AdtKind::Union => build_union_type_di_node(cx, unique_type_id, span),
490            AdtKind::Enum => enums::build_enum_type_di_node(cx, unique_type_id, span),
491        },
492        ty::Tuple(_) => build_tuple_type_di_node(cx, unique_type_id),
493        ty::Pat(base, _) => return type_di_node(cx, base),
494        ty::UnsafeBinder(_) => build_unsafe_binder_type_di_node(cx, t, unique_type_id),
495        ty::Alias(..)
496        | ty::Param(_)
497        | ty::Bound(..)
498        | ty::Infer(_)
499        | ty::Placeholder(_)
500        | ty::CoroutineWitness(..)
501        | ty::Error(_) => {
502            ::rustc_middle::util::bug::bug_fmt(format_args!("debuginfo: unexpected type in type_di_node(): {0:?}",
        t))bug!("debuginfo: unexpected type in type_di_node(): {:?}", t)
503        }
504    };
505
506    {
507        if already_stored_in_typemap {
508            // Make sure that we really do have a `TypeMap` entry for the unique type ID.
509            let di_node_for_uid =
510                match debug_context(cx).type_map.di_node_for_unique_id(unique_type_id) {
511                    Some(di_node) => di_node,
512                    None => {
513                        ::rustc_middle::util::bug::bug_fmt(format_args!("expected type debuginfo node for unique type ID \'{0:?}\' to already be in the `debuginfo::TypeMap` but it was not.",
        unique_type_id));bug!(
514                            "expected type debuginfo node for unique \
515                               type ID '{:?}' to already be in \
516                               the `debuginfo::TypeMap` but it \
517                               was not.",
518                            unique_type_id,
519                        );
520                    }
521                };
522
523            match (&(di_node_for_uid as *const _), &(di_node as *const _)) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(di_node_for_uid as *const _, di_node as *const _);
524        } else {
525            debug_context(cx).type_map.insert(unique_type_id, di_node);
526        }
527    }
528
529    di_node
530}
531
532// FIXME(mw): Cache this via a regular UniqueTypeId instead of an extra field in the debug context.
533fn recursion_marker_type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) -> &'ll DIType {
534    *debug_context(cx).recursion_marker_type.get_or_init(move || {
535        // The choice of type here is pretty arbitrary -
536        // anything reading the debuginfo for a recursive
537        // type is going to see *something* weird - the only
538        // question is what exactly it will see.
539        //
540        // FIXME: the name `<recur_type>` does not fit the naming scheme
541        //        of other types.
542        //
543        // FIXME: it might make sense to use an actual pointer type here
544        //        so that debuggers can show the address.
545        create_basic_type(
546            cx,
547            "<recur_type>",
548            cx.tcx.data_layout.pointer_size(),
549            dwarf_const::DW_ATE_unsigned,
550        )
551    })
552}
553
554fn hex_encode(data: &[u8]) -> String {
555    let mut hex_string = String::with_capacity(data.len() * 2);
556    for byte in data.iter() {
557        (&mut hex_string).write_fmt(format_args!("{0:02x}", byte))write!(&mut hex_string, "{byte:02x}").unwrap();
558    }
559    hex_string
560}
561
562pub(crate) fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) -> &'ll DIFile {
563    let cache_key = Some((source_file.stable_id, source_file.src_hash));
564    return debug_context(cx)
565        .created_files
566        .borrow_mut()
567        .entry(cache_key)
568        .or_insert_with(|| alloc_new_file_metadata(cx, source_file));
569
570    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("alloc_new_file_metadata",
                                    "rustc_codegen_llvm::debuginfo::metadata",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs"),
                                    ::tracing_core::__macro_support::Option::Some(570u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::debuginfo::metadata"),
                                    ::tracing_core::field::FieldSet::new(&[],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{ meta.fields().value_set(&[]) })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: &'ll DIFile = loop {};
            return __tracing_attr_fake_return;
        }
        {
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs:575",
                                    "rustc_codegen_llvm::debuginfo::metadata",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs"),
                                    ::tracing_core::__macro_support::Option::Some(575u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::debuginfo::metadata"),
                                    ::tracing_core::field::FieldSet::new(&["source_file.name"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&source_file.name)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            let (directory, file_name) =
                match &source_file.name {
                    FileName::Real(filename) => {
                        let (working_directory, embeddable_name) =
                            filename.embeddable_name(RemapPathScopeComponents::DEBUGINFO);
                        {
                            use ::tracing::__macro_support::Callsite as _;
                            static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                                {
                                    static META: ::tracing::Metadata<'static> =
                                        {
                                            ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs:582",
                                                "rustc_codegen_llvm::debuginfo::metadata",
                                                ::tracing::Level::DEBUG,
                                                ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs"),
                                                ::tracing_core::__macro_support::Option::Some(582u32),
                                                ::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::debuginfo::metadata"),
                                                ::tracing_core::field::FieldSet::new(&["working_directory",
                                                                "embeddable_name"],
                                                    ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                                ::tracing::metadata::Kind::EVENT)
                                        };
                                    ::tracing::callsite::DefaultCallsite::new(&META)
                                };
                            let enabled =
                                ::tracing::Level::DEBUG <=
                                            ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                        ::tracing::Level::DEBUG <=
                                            ::tracing::level_filters::LevelFilter::current() &&
                                    {
                                        let interest = __CALLSITE.interest();
                                        !interest.is_never() &&
                                            ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                                interest)
                                    };
                            if enabled {
                                (|value_set: ::tracing::field::ValueSet|
                                            {
                                                let meta = __CALLSITE.metadata();
                                                ::tracing::Event::dispatch(meta, &value_set);
                                                ;
                                            })({
                                        #[allow(unused_imports)]
                                        use ::tracing::field::{debug, display, Value};
                                        let mut iter = __CALLSITE.metadata().fields().iter();
                                        __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                            ::tracing::__macro_support::Option::Some(&debug(&working_directory)
                                                                    as &dyn Value)),
                                                        (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                            ::tracing::__macro_support::Option::Some(&debug(&embeddable_name)
                                                                    as &dyn Value))])
                                    });
                            } else { ; }
                        };
                        if let Ok(rel_path) =
                                embeddable_name.strip_prefix(working_directory) {
                            (working_directory.to_string_lossy(),
                                rel_path.to_string_lossy().into_owned())
                        } else {
                            ("".into(), embeddable_name.to_string_lossy().into_owned())
                        }
                    }
                    other => {
                        {
                            use ::tracing::__macro_support::Callsite as _;
                            static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                                {
                                    static META: ::tracing::Metadata<'static> =
                                        {
                                            ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs:607",
                                                "rustc_codegen_llvm::debuginfo::metadata",
                                                ::tracing::Level::DEBUG,
                                                ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs"),
                                                ::tracing_core::__macro_support::Option::Some(607u32),
                                                ::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::debuginfo::metadata"),
                                                ::tracing_core::field::FieldSet::new(&["other"],
                                                    ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                                ::tracing::metadata::Kind::EVENT)
                                        };
                                    ::tracing::callsite::DefaultCallsite::new(&META)
                                };
                            let enabled =
                                ::tracing::Level::DEBUG <=
                                            ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                        ::tracing::Level::DEBUG <=
                                            ::tracing::level_filters::LevelFilter::current() &&
                                    {
                                        let interest = __CALLSITE.interest();
                                        !interest.is_never() &&
                                            ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                                interest)
                                    };
                            if enabled {
                                (|value_set: ::tracing::field::ValueSet|
                                            {
                                                let meta = __CALLSITE.metadata();
                                                ::tracing::Event::dispatch(meta, &value_set);
                                                ;
                                            })({
                                        #[allow(unused_imports)]
                                        use ::tracing::field::{debug, display, Value};
                                        let mut iter = __CALLSITE.metadata().fields().iter();
                                        __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                            ::tracing::__macro_support::Option::Some(&debug(&other) as
                                                                    &dyn Value))])
                                    });
                            } else { ; }
                        };
                        ("".into(),
                            other.display(RemapPathScopeComponents::DEBUGINFO).to_string())
                    }
                };
            let hash_kind =
                match source_file.src_hash.kind {
                    rustc_span::SourceFileHashAlgorithm::Md5 =>
                        llvm::ChecksumKind::MD5,
                    rustc_span::SourceFileHashAlgorithm::Sha1 =>
                        llvm::ChecksumKind::SHA1,
                    rustc_span::SourceFileHashAlgorithm::Sha256 =>
                        llvm::ChecksumKind::SHA256,
                    rustc_span::SourceFileHashAlgorithm::Blake3 =>
                        llvm::ChecksumKind::None,
                };
            let hash_value = hex_encode(source_file.src_hash.hash_bytes());
            let mut source = None;
            let external_src;
            if cx.sess().opts.unstable_opts.embed_source {
                source = source_file.src.as_deref().map(String::as_str);
                if source.is_none() {
                    cx.tcx.sess.source_map().ensure_source_file_source_present(source_file);
                    external_src = source_file.external_src.read();
                    source = external_src.get_source();
                }
            }
            create_file(DIB(cx), &file_name, &directory, &hash_value,
                hash_kind, source)
        }
    }
}#[instrument(skip(cx, source_file), level = "debug")]
571    fn alloc_new_file_metadata<'ll>(
572        cx: &CodegenCx<'ll, '_>,
573        source_file: &SourceFile,
574    ) -> &'ll DIFile {
575        debug!(?source_file.name);
576
577        let (directory, file_name) = match &source_file.name {
578            FileName::Real(filename) => {
579                let (working_directory, embeddable_name) =
580                    filename.embeddable_name(RemapPathScopeComponents::DEBUGINFO);
581
582                debug!(?working_directory, ?embeddable_name);
583
584                if let Ok(rel_path) = embeddable_name.strip_prefix(working_directory) {
585                    // If the compiler's working directory (which also is the DW_AT_comp_dir of
586                    // the compilation unit) is a prefix of the path we are about to emit, then
587                    // only emit the part relative to the working directory. Because of path
588                    // remapping we sometimes see strange things here: `abs_path` might
589                    // actually look like a relative path (e.g.
590                    // `<crate-name-and-version>/src/lib.rs`), so if we emit it without taking
591                    // the working directory into account, downstream tooling will interpret it
592                    // as `<working-directory>/<crate-name-and-version>/src/lib.rs`, which
593                    // makes no sense. Usually in such cases the working directory will also be
594                    // remapped to `<crate-name-and-version>` or some other prefix of the path
595                    // we are remapping, so we end up with
596                    // `<crate-name-and-version>/<crate-name-and-version>/src/lib.rs`.
597                    //
598                    // By moving the working directory portion into the `directory` part of the
599                    // DIFile, we allow LLVM to emit just the relative path for DWARF, while
600                    // still emitting the correct absolute path for CodeView.
601                    (working_directory.to_string_lossy(), rel_path.to_string_lossy().into_owned())
602                } else {
603                    ("".into(), embeddable_name.to_string_lossy().into_owned())
604                }
605            }
606            other => {
607                debug!(?other);
608                ("".into(), other.display(RemapPathScopeComponents::DEBUGINFO).to_string())
609            }
610        };
611
612        let hash_kind = match source_file.src_hash.kind {
613            rustc_span::SourceFileHashAlgorithm::Md5 => llvm::ChecksumKind::MD5,
614            rustc_span::SourceFileHashAlgorithm::Sha1 => llvm::ChecksumKind::SHA1,
615            rustc_span::SourceFileHashAlgorithm::Sha256 => llvm::ChecksumKind::SHA256,
616            rustc_span::SourceFileHashAlgorithm::Blake3 => llvm::ChecksumKind::None,
617        };
618        let hash_value = hex_encode(source_file.src_hash.hash_bytes());
619
620        let mut source = None;
621        let external_src;
622        if cx.sess().opts.unstable_opts.embed_source {
623            source = source_file.src.as_deref().map(String::as_str);
624            if source.is_none() {
625                cx.tcx.sess.source_map().ensure_source_file_source_present(source_file);
626                external_src = source_file.external_src.read();
627                source = external_src.get_source();
628            }
629        }
630
631        create_file(DIB(cx), &file_name, &directory, &hash_value, hash_kind, source)
632    }
633}
634
635fn unknown_file_metadata<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll DIFile {
636    debug_context(cx).created_files.borrow_mut().entry(None).or_insert_with(|| {
637        create_file(DIB(cx), "<unknown>", "", "", llvm::ChecksumKind::None, None)
638    })
639}
640
641fn create_file<'ll>(
642    builder: &DIBuilder<'ll>,
643    file_name: &str,
644    directory: &str,
645    hash_value: &str,
646    hash_kind: llvm::ChecksumKind,
647    source: Option<&str>,
648) -> &'ll DIFile {
649    unsafe {
650        llvm::LLVMRustDIBuilderCreateFile(
651            builder,
652            file_name.as_c_char_ptr(),
653            file_name.len(),
654            directory.as_c_char_ptr(),
655            directory.len(),
656            hash_kind,
657            hash_value.as_c_char_ptr(),
658            hash_value.len(),
659            source.map_or(ptr::null(), |x| x.as_c_char_ptr()),
660            source.map_or(0, |x| x.len()),
661        )
662    }
663}
664
665trait MsvcBasicName {
666    fn msvc_basic_name(self) -> &'static str;
667}
668
669impl MsvcBasicName for ty::IntTy {
670    fn msvc_basic_name(self) -> &'static str {
671        match self {
672            ty::IntTy::Isize => "ptrdiff_t",
673            ty::IntTy::I8 => "__int8",
674            ty::IntTy::I16 => "__int16",
675            ty::IntTy::I32 => "__int32",
676            ty::IntTy::I64 => "__int64",
677            ty::IntTy::I128 => "__int128",
678        }
679    }
680}
681
682impl MsvcBasicName for ty::UintTy {
683    fn msvc_basic_name(self) -> &'static str {
684        match self {
685            ty::UintTy::Usize => "size_t",
686            ty::UintTy::U8 => "unsigned __int8",
687            ty::UintTy::U16 => "unsigned __int16",
688            ty::UintTy::U32 => "unsigned __int32",
689            ty::UintTy::U64 => "unsigned __int64",
690            ty::UintTy::U128 => "unsigned __int128",
691        }
692    }
693}
694
695impl MsvcBasicName for ty::FloatTy {
696    fn msvc_basic_name(self) -> &'static str {
697        // FIXME(f128): `f128` has no MSVC representation. We could improve the debuginfo.
698        // See: <https://github.com/rust-lang/rust/issues/121837>
699        match self {
700            ty::FloatTy::F16 => {
701                ::rustc_middle::util::bug::bug_fmt(format_args!("`f16` should have been handled in `build_basic_type_di_node`"))bug!("`f16` should have been handled in `build_basic_type_di_node`")
702            }
703            ty::FloatTy::F32 => "float",
704            ty::FloatTy::F64 => "double",
705            ty::FloatTy::F128 => "fp128",
706        }
707    }
708}
709
710fn build_cpp_f16_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) -> DINodeCreationResult<'ll> {
711    // MSVC has no native support for `f16`. Instead, emit `struct f16 { bits: u16 }` to allow the
712    // `f16`'s value to be displayed using a Natvis visualiser in `intrinsic.natvis`.
713    let float_ty = cx.tcx.types.f16;
714    let bits_ty = cx.tcx.types.u16;
715    let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
716        match float_ty.kind() {
717            ty::Adt(def, _) => Some(file_metadata_from_def_id(cx, Some(def.did()))),
718            _ => None,
719        }
720    } else {
721        None
722    };
723    type_map::build_type_with_children(
724        cx,
725        type_map::stub(
726            cx,
727            Stub::Struct,
728            UniqueTypeId::for_ty(cx.tcx, float_ty),
729            "f16",
730            def_location,
731            cx.size_and_align_of(float_ty),
732            NO_SCOPE_METADATA,
733            DIFlags::FlagZero,
734        ),
735        // Fields:
736        |cx, float_di_node| {
737            let def_id = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
738                match bits_ty.kind() {
739                    ty::Adt(def, _) => Some(def.did()),
740                    _ => None,
741                }
742            } else {
743                None
744            };
745            {
    let count = 0usize + 1usize;
    let mut vec = ::smallvec::SmallVec::new();
    if count <= vec.inline_size() {
        vec.push(build_field_di_node(cx, float_di_node, "bits",
                cx.layout_of(bits_ty), Size::ZERO, DIFlags::FlagZero,
                type_di_node(cx, bits_ty), def_id));
        vec
    } else {
        ::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                    [build_field_di_node(cx, float_di_node, "bits",
                                cx.layout_of(bits_ty), Size::ZERO, DIFlags::FlagZero,
                                type_di_node(cx, bits_ty), def_id)])))
    }
}smallvec![build_field_di_node(
746                cx,
747                float_di_node,
748                "bits",
749                cx.layout_of(bits_ty),
750                Size::ZERO,
751                DIFlags::FlagZero,
752                type_di_node(cx, bits_ty),
753                def_id,
754            )]
755        },
756        NO_GENERICS,
757    )
758}
759
760fn build_basic_type_di_node<'ll, 'tcx>(
761    cx: &CodegenCx<'ll, 'tcx>,
762    t: Ty<'tcx>,
763) -> DINodeCreationResult<'ll> {
764    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs:764",
                        "rustc_codegen_llvm::debuginfo::metadata",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs"),
                        ::tracing_core::__macro_support::Option::Some(764u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::debuginfo::metadata"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("build_basic_type_di_node: {0:?}",
                                                    t) as &dyn Value))])
            });
    } else { ; }
};debug!("build_basic_type_di_node: {:?}", t);
765
766    // When targeting MSVC, emit MSVC style type names for compatibility with
767    // .natvis visualizers (and perhaps other existing native debuggers?)
768    let cpp_like_debuginfo = cpp_like_debuginfo(cx.tcx);
769
770    use dwarf_const::{DW_ATE_UTF, DW_ATE_boolean, DW_ATE_float, DW_ATE_signed, DW_ATE_unsigned};
771
772    let (name, encoding) = match t.kind() {
773        ty::Never => ("!", DW_ATE_unsigned),
774        ty::Tuple(elements) if elements.is_empty() => {
775            if cpp_like_debuginfo {
776                return build_tuple_type_di_node(cx, UniqueTypeId::for_ty(cx.tcx, t));
777            } else {
778                ("()", DW_ATE_unsigned)
779            }
780        }
781        ty::Bool => ("bool", DW_ATE_boolean),
782        ty::Char => ("char", DW_ATE_UTF),
783        ty::Int(int_ty) if cpp_like_debuginfo => (int_ty.msvc_basic_name(), DW_ATE_signed),
784        ty::Uint(uint_ty) if cpp_like_debuginfo => (uint_ty.msvc_basic_name(), DW_ATE_unsigned),
785        ty::Float(ty::FloatTy::F16) if cpp_like_debuginfo => {
786            return build_cpp_f16_di_node(cx);
787        }
788        ty::Float(float_ty) if cpp_like_debuginfo => (float_ty.msvc_basic_name(), DW_ATE_float),
789        ty::Int(int_ty) => (int_ty.name_str(), DW_ATE_signed),
790        ty::Uint(uint_ty) => (uint_ty.name_str(), DW_ATE_unsigned),
791        ty::Float(float_ty) => (float_ty.name_str(), DW_ATE_float),
792        _ => ::rustc_middle::util::bug::bug_fmt(format_args!("debuginfo::build_basic_type_di_node - `t` is invalid type"))bug!("debuginfo::build_basic_type_di_node - `t` is invalid type"),
793    };
794
795    let ty_di_node = create_basic_type(cx, name, cx.size_of(t), encoding);
796
797    if !cpp_like_debuginfo {
798        return DINodeCreationResult::new(ty_di_node, false);
799    }
800
801    let typedef_name = match t.kind() {
802        ty::Int(int_ty) => int_ty.name_str(),
803        ty::Uint(uint_ty) => uint_ty.name_str(),
804        ty::Float(float_ty) => float_ty.name_str(),
805        _ => return DINodeCreationResult::new(ty_di_node, false),
806    };
807
808    let typedef_di_node = unsafe {
809        llvm::LLVMDIBuilderCreateTypedef(
810            DIB(cx),
811            ty_di_node,
812            typedef_name.as_ptr(),
813            typedef_name.len(),
814            unknown_file_metadata(cx),
815            0,    // (no line number)
816            None, // (no scope)
817            0u32, // (no alignment specified)
818        )
819    };
820
821    DINodeCreationResult::new(typedef_di_node, false)
822}
823
824fn create_basic_type<'ll, 'tcx>(
825    cx: &CodegenCx<'ll, 'tcx>,
826    name: &str,
827    size: Size,
828    encoding: u32,
829) -> &'ll DIBasicType {
830    unsafe {
831        llvm::LLVMDIBuilderCreateBasicType(
832            DIB(cx),
833            name.as_ptr(),
834            name.len(),
835            size.bits(),
836            encoding,
837            DIFlags::FlagZero,
838        )
839    }
840}
841
842fn build_foreign_type_di_node<'ll, 'tcx>(
843    cx: &CodegenCx<'ll, 'tcx>,
844    t: Ty<'tcx>,
845    unique_type_id: UniqueTypeId<'tcx>,
846) -> DINodeCreationResult<'ll> {
847    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs:847",
                        "rustc_codegen_llvm::debuginfo::metadata",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs"),
                        ::tracing_core::__macro_support::Option::Some(847u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::debuginfo::metadata"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("build_foreign_type_di_node: {0:?}",
                                                    t) as &dyn Value))])
            });
    } else { ; }
};debug!("build_foreign_type_di_node: {:?}", t);
848
849    let &ty::Foreign(def_id) = unique_type_id.expect_ty().kind() else {
850        ::rustc_middle::util::bug::bug_fmt(format_args!("build_foreign_type_di_node() called with unexpected type: {0:?}",
        unique_type_id.expect_ty()));bug!(
851            "build_foreign_type_di_node() called with unexpected type: {:?}",
852            unique_type_id.expect_ty()
853        );
854    };
855
856    build_type_with_children(
857        cx,
858        type_map::stub(
859            cx,
860            Stub::Struct,
861            unique_type_id,
862            &compute_debuginfo_type_name(cx.tcx, t, false),
863            None,
864            cx.size_and_align_of(t),
865            Some(get_namespace_for_item(cx, def_id)),
866            DIFlags::FlagZero,
867        ),
868        |_, _| ::smallvec::SmallVec::new()smallvec![],
869        NO_GENERICS,
870    )
871}
872
873pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
874    tcx: TyCtxt<'tcx>,
875    codegen_unit_name: &str,
876    debug_context: &CodegenUnitDebugContext<'ll, 'tcx>,
877) -> &'ll DIDescriptor {
878    let mut name_in_debuginfo = tcx
879        .sess
880        .local_crate_source_file()
881        .map(|src| src.path(RemapPathScopeComponents::DEBUGINFO).to_path_buf())
882        .unwrap_or_else(|| PathBuf::from(tcx.crate_name(LOCAL_CRATE).as_str()));
883
884    // To avoid breaking split DWARF, we need to ensure that each codegen unit
885    // has a unique `DW_AT_name`. This is because there's a remote chance that
886    // different codegen units for the same module will have entirely
887    // identical DWARF entries for the purpose of the DWO ID, which would
888    // violate Appendix F ("Split Dwarf Object Files") of the DWARF 5
889    // specification. LLVM uses the algorithm specified in section 7.32 "Type
890    // Signature Computation" to compute the DWO ID, which does not include
891    // any fields that would distinguish compilation units. So we must embed
892    // the codegen unit name into the `DW_AT_name`. (Issue #88521.)
893    //
894    // Additionally, the OSX linker has an idiosyncrasy where it will ignore
895    // some debuginfo if multiple object files with the same `DW_AT_name` are
896    // linked together.
897    //
898    // As a workaround for these two issues, we generate unique names for each
899    // object file. Those do not correspond to an actual source file but that
900    // is harmless.
901    name_in_debuginfo.push("@");
902    name_in_debuginfo.push(codegen_unit_name);
903
904    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs:904",
                        "rustc_codegen_llvm::debuginfo::metadata",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs"),
                        ::tracing_core::__macro_support::Option::Some(904u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::debuginfo::metadata"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("build_compile_unit_di_node: {0:?}",
                                                    name_in_debuginfo) as &dyn Value))])
            });
    } else { ; }
};debug!("build_compile_unit_di_node: {:?}", name_in_debuginfo);
905    let rustc_producer = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("rustc version {0}",
                tcx.sess.cfg_version))
    })format!("rustc version {}", tcx.sess.cfg_version);
906    // FIXME(#41252) Remove "clang LLVM" if we can get GDB and LLVM to play nice.
907    let producer = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("clang LLVM ({0})", rustc_producer))
    })format!("clang LLVM ({rustc_producer})");
908
909    let name_in_debuginfo = name_in_debuginfo.to_string_lossy();
910    let work_dir = tcx.sess.psess.source_map().working_dir();
911    let output_filenames = tcx.output_filenames(());
912    let split_name = if tcx.sess.target_can_use_split_dwarf()
913        && let Some(f) = output_filenames.split_dwarf_path(
914            tcx.sess.split_debuginfo(),
915            tcx.sess.opts.unstable_opts.split_dwarf_kind,
916            codegen_unit_name,
917        ) {
918        // We get a path relative to the working directory from split_dwarf_path
919        Some(tcx.sess.source_map().path_mapping().to_real_filename(work_dir, f))
920    } else {
921        None
922    };
923    let split_name = split_name
924        .as_ref()
925        .map(|f| f.path(RemapPathScopeComponents::DEBUGINFO).to_string_lossy())
926        .unwrap_or_default();
927    let work_dir = work_dir.path(RemapPathScopeComponents::DEBUGINFO).to_string_lossy();
928    let kind = DebugEmissionKind::from_generic(tcx.sess.opts.debuginfo);
929
930    let dwarf_version = tcx.sess.dwarf_version();
931    let is_dwarf_kind =
932        #[allow(non_exhaustive_omitted_patterns)] match tcx.sess.target.debuginfo_kind
    {
    DebuginfoKind::Dwarf | DebuginfoKind::DwarfDsym => true,
    _ => false,
}matches!(tcx.sess.target.debuginfo_kind, DebuginfoKind::Dwarf | DebuginfoKind::DwarfDsym);
933    // Don't emit `.debug_pubnames` and `.debug_pubtypes` on DWARFv4 or lower.
934    let debug_name_table_kind = if is_dwarf_kind && dwarf_version <= 4 {
935        DebugNameTableKind::None
936    } else {
937        DebugNameTableKind::Default
938    };
939
940    unsafe {
941        let compile_unit_file = create_file(
942            debug_context.builder.as_ref(),
943            &name_in_debuginfo,
944            &work_dir,
945            "",
946            llvm::ChecksumKind::None,
947            None,
948        );
949
950        let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit(
951            debug_context.builder.as_ref(),
952            dwarf_const::DW_LANG_Rust,
953            compile_unit_file,
954            producer.as_c_char_ptr(),
955            producer.len(),
956            tcx.sess.opts.optimize != config::OptLevel::No,
957            c"".as_ptr(),
958            0,
959            // NB: this doesn't actually have any perceptible effect, it seems. LLVM will instead
960            // put the path supplied to `MCSplitDwarfFile` into the debug info of the final
961            // output(s).
962            split_name.as_c_char_ptr(),
963            split_name.len(),
964            kind,
965            0,
966            tcx.sess.opts.unstable_opts.split_dwarf_inlining,
967            debug_name_table_kind,
968        );
969
970        return unit_metadata;
971    };
972}
973
974/// Creates a `DW_TAG_member` entry inside the DIE represented by the given `type_di_node`.
975fn build_field_di_node<'ll, 'tcx>(
976    cx: &CodegenCx<'ll, 'tcx>,
977    owner: &'ll DIScope,
978    name: &str,
979    layout: TyAndLayout<'tcx>,
980    offset: Size,
981    flags: DIFlags,
982    type_di_node: &'ll DIType,
983    def_id: Option<DefId>,
984) -> &'ll DIType {
985    let (file_metadata, line_number) = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers
986    {
987        file_metadata_from_def_id(cx, def_id)
988    } else {
989        (unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
990    };
991    create_member_type(
992        cx,
993        owner,
994        name,
995        file_metadata,
996        line_number,
997        layout,
998        offset,
999        flags,
1000        type_di_node,
1001    )
1002}
1003
1004fn create_member_type<'ll, 'tcx>(
1005    cx: &CodegenCx<'ll, 'tcx>,
1006    owner: &'ll DIScope,
1007    name: &str,
1008    file_metadata: &'ll DIType,
1009    line_number: u32,
1010    layout: TyAndLayout<'tcx>,
1011    offset: Size,
1012    flags: DIFlags,
1013    type_di_node: &'ll DIType,
1014) -> &'ll DIType {
1015    unsafe {
1016        llvm::LLVMDIBuilderCreateMemberType(
1017            DIB(cx),
1018            owner,
1019            name.as_ptr(),
1020            name.len(),
1021            file_metadata,
1022            line_number,
1023            layout.size.bits(),
1024            layout.align.bits() as u32,
1025            offset.bits(),
1026            flags,
1027            type_di_node,
1028        )
1029    }
1030}
1031
1032/// Returns the `DIFlags` corresponding to the visibility of the item identified by `did`.
1033///
1034/// `DIFlags::Flag{Public,Protected,Private}` correspond to `DW_AT_accessibility`
1035/// (public/protected/private) aren't exactly right for Rust, but neither is `DW_AT_visibility`
1036/// (local/exported/qualified), and there's no way to set `DW_AT_visibility` in LLVM's API.
1037fn visibility_di_flags<'ll, 'tcx>(
1038    cx: &CodegenCx<'ll, 'tcx>,
1039    did: DefId,
1040    type_did: DefId,
1041) -> DIFlags {
1042    let parent_did = cx.tcx.parent(type_did);
1043    let visibility = cx.tcx.visibility(did);
1044    match visibility {
1045        Visibility::Public => DIFlags::FlagPublic,
1046        // Private fields have a restricted visibility of the module containing the type.
1047        Visibility::Restricted(did) if did == parent_did => DIFlags::FlagPrivate,
1048        // `pub(crate)`/`pub(super)` visibilities are any other restricted visibility.
1049        Visibility::Restricted(..) => DIFlags::FlagProtected,
1050    }
1051}
1052
1053/// Creates the debuginfo node for a Rust struct type. Maybe be a regular struct or a tuple-struct.
1054fn build_struct_type_di_node<'ll, 'tcx>(
1055    cx: &CodegenCx<'ll, 'tcx>,
1056    unique_type_id: UniqueTypeId<'tcx>,
1057    span: Span,
1058) -> DINodeCreationResult<'ll> {
1059    let struct_type = unique_type_id.expect_ty();
1060
1061    let ty::Adt(adt_def, _) = struct_type.kind() else {
1062        ::rustc_middle::util::bug::bug_fmt(format_args!("build_struct_type_di_node() called with non-struct-type: {0:?}",
        struct_type));bug!("build_struct_type_di_node() called with non-struct-type: {:?}", struct_type);
1063    };
1064    if !adt_def.is_struct() {
    ::core::panicking::panic("assertion failed: adt_def.is_struct()")
};assert!(adt_def.is_struct());
1065    let containing_scope = get_namespace_for_item(cx, adt_def.did());
1066    let struct_type_and_layout = cx.spanned_layout_of(struct_type, span);
1067    let variant_def = adt_def.non_enum_variant();
1068    let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1069        Some(file_metadata_from_def_id(cx, Some(adt_def.did())))
1070    } else {
1071        None
1072    };
1073    let name = compute_debuginfo_type_name(cx.tcx, struct_type, false);
1074
1075    if struct_type.is_scalable_vector() {
1076        let parts = struct_type.scalable_vector_parts(cx.tcx).unwrap();
1077        return build_scalable_vector_di_node(
1078            cx,
1079            unique_type_id,
1080            name,
1081            *adt_def,
1082            parts,
1083            struct_type_and_layout.layout,
1084            def_location,
1085            containing_scope,
1086        );
1087    }
1088
1089    type_map::build_type_with_children(
1090        cx,
1091        type_map::stub(
1092            cx,
1093            Stub::Struct,
1094            unique_type_id,
1095            &name,
1096            def_location,
1097            size_and_align_of(struct_type_and_layout),
1098            Some(containing_scope),
1099            visibility_di_flags(cx, adt_def.did(), adt_def.did()),
1100        ),
1101        // Fields:
1102        |cx, owner| {
1103            variant_def
1104                .fields
1105                .iter()
1106                .enumerate()
1107                .map(|(i, f)| {
1108                    let field_name = if variant_def.ctor_kind() == Some(CtorKind::Fn) {
1109                        // This is a tuple struct
1110                        tuple_field_name(i)
1111                    } else {
1112                        // This is struct with named fields
1113                        Cow::Borrowed(f.name.as_str())
1114                    };
1115                    let field_layout = struct_type_and_layout.field(cx, i);
1116                    let def_id = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1117                        Some(f.did)
1118                    } else {
1119                        None
1120                    };
1121                    build_field_di_node(
1122                        cx,
1123                        owner,
1124                        &field_name[..],
1125                        field_layout,
1126                        struct_type_and_layout.fields.offset(i),
1127                        visibility_di_flags(cx, f.did, adt_def.did()),
1128                        type_di_node(cx, field_layout.ty),
1129                        def_id,
1130                    )
1131                })
1132                .collect()
1133        },
1134        |cx| build_generic_type_param_di_nodes(cx, struct_type),
1135    )
1136}
1137
1138/// Generate debuginfo for a `#[rustc_scalable_vector]` type.
1139///
1140/// Debuginfo for a scalable vector uses a derived type based on a composite type. The composite
1141/// type has the  `DIFlagVector` flag set and is based on the element type of the scalable vector.
1142/// The composite type has a subrange from 0 to an expression that calculates the number of
1143/// elements in the vector.
1144///
1145/// ```text,ignore
1146/// !1 = !DIDerivedType(tag: DW_TAG_typedef, name: "svint16_t", ..., baseType: !2, ...)
1147/// !2 = !DICompositeType(tag: DW_TAG_array_type, baseType: !3, ..., flags: DIFlagVector, elements: !4)
1148/// !3 = !DIBasicType(name: "i16", size: 16, encoding: DW_ATE_signed)
1149/// !4 = !{!5}
1150/// !5 = !DISubrange(lowerBound: 0, upperBound: !DIExpression(DW_OP_constu, 4, DW_OP_bregx, 46, 0, DW_OP_mul, DW_OP_constu, 1, DW_OP_minus))
1151/// ```
1152///
1153/// See the `CodegenType::CreateType(const BuiltinType *BT)` implementation in Clang for how this
1154/// is generated for C and C++.
1155fn build_scalable_vector_di_node<'ll, 'tcx>(
1156    cx: &CodegenCx<'ll, 'tcx>,
1157    unique_type_id: UniqueTypeId<'tcx>,
1158    name: String,
1159    adt_def: AdtDef<'tcx>,
1160    (element_count, element_ty, number_of_vectors): (u16, Ty<'tcx>, NumScalableVectors),
1161    layout: Layout<'tcx>,
1162    def_location: Option<DefinitionLocation<'ll>>,
1163    containing_scope: &'ll DIScope,
1164) -> DINodeCreationResult<'ll> {
1165    use dwarf_const::{DW_OP_bregx, DW_OP_constu, DW_OP_minus, DW_OP_mul};
1166    if !adt_def.repr().scalable() {
    ::core::panicking::panic("assertion failed: adt_def.repr().scalable()")
};assert!(adt_def.repr().scalable());
1167    // This logic is specific to AArch64 for the moment, but can be extended for other architectures
1168    // later.
1169    {
    match cx.tcx.sess.target.arch {
        Arch::AArch64 => {}
        ref left_val => {
            ::core::panicking::assert_matches_failed(left_val,
                "Arch::AArch64", ::core::option::Option::None);
        }
    }
};assert_matches!(cx.tcx.sess.target.arch, Arch::AArch64);
1170
1171    let (file_metadata, line_number) = if let Some(def_location) = def_location {
1172        (def_location.0, def_location.1)
1173    } else {
1174        (unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
1175    };
1176
1177    let (bitstride, element_di_node) = if element_ty.is_bool() {
1178        (Some(llvm::LLVMValueAsMetadata(cx.const_i64(1))), type_di_node(cx, cx.tcx.types.u8))
1179    } else {
1180        (None, type_di_node(cx, element_ty))
1181    };
1182
1183    let number_of_elements: u64 = (element_count as u64) * (number_of_vectors.0 as u64);
1184    let number_of_elements_per_vg = number_of_elements / 2;
1185    let mut expr = smallvec::SmallVec::<[u64; 9]>::new();
1186    // `($number_of_elements_per_vector_granule * (value_of_register(AArch64::VG) + 0)) - 1`
1187    expr.push(DW_OP_constu); // Push a constant onto the stack
1188    expr.push(number_of_elements_per_vg);
1189    expr.push(DW_OP_bregx); // Push the value of a register + offset on to the stack
1190    expr.push(/* AArch64::VG */ 46u64);
1191    expr.push(0u64);
1192    expr.push(DW_OP_mul); // Multiply top two values on stack
1193    expr.push(DW_OP_constu); // Push a constant onto the stack
1194    expr.push(1u64);
1195    expr.push(DW_OP_minus); // Subtract top two values on stack
1196
1197    let di_builder = DIB(cx);
1198    let metadata = unsafe {
1199        let upper = llvm::LLVMDIBuilderCreateExpression(di_builder, expr.as_ptr(), expr.len());
1200        let subrange = llvm::LLVMRustDIGetOrCreateSubrange(
1201            di_builder,
1202            /* CountNode */ None,
1203            llvm::LLVMValueAsMetadata(cx.const_i64(0)),
1204            upper,
1205            /* Stride */ None,
1206        );
1207        let subscripts = create_DIArray(di_builder, &[Some(subrange)]);
1208        let vector_ty = llvm::LLVMRustDICreateVectorType(
1209            di_builder,
1210            /* Size */ 0,
1211            layout.align.bits() as u32,
1212            element_di_node,
1213            subscripts,
1214            bitstride,
1215        );
1216        llvm::LLVMDIBuilderCreateTypedef(
1217            di_builder,
1218            vector_ty,
1219            name.as_ptr(),
1220            name.len(),
1221            file_metadata,
1222            line_number,
1223            Some(containing_scope),
1224            layout.align.bits() as u32,
1225        )
1226    };
1227
1228    debug_context(cx).type_map.insert(unique_type_id, metadata);
1229    DINodeCreationResult { di_node: metadata, already_stored_in_typemap: true }
1230}
1231
1232//=-----------------------------------------------------------------------------
1233// Tuples
1234//=-----------------------------------------------------------------------------
1235
1236/// Builds the DW_TAG_member debuginfo nodes for the upvars of a closure or coroutine.
1237/// For a coroutine, this will handle upvars shared by all states.
1238fn build_upvar_field_di_nodes<'ll, 'tcx>(
1239    cx: &CodegenCx<'ll, 'tcx>,
1240    closure_or_coroutine_ty: Ty<'tcx>,
1241    closure_or_coroutine_di_node: &'ll DIType,
1242) -> SmallVec<&'ll DIType> {
1243    let (&def_id, up_var_tys) = match closure_or_coroutine_ty.kind() {
1244        ty::Coroutine(def_id, args) => (def_id, args.as_coroutine().prefix_tys()),
1245        ty::Closure(def_id, args) => (def_id, args.as_closure().upvar_tys()),
1246        ty::CoroutineClosure(def_id, args) => (def_id, args.as_coroutine_closure().upvar_tys()),
1247        _ => {
1248            ::rustc_middle::util::bug::bug_fmt(format_args!("build_upvar_field_di_nodes() called with non-closure-or-coroutine-type: {0:?}",
        closure_or_coroutine_ty))bug!(
1249                "build_upvar_field_di_nodes() called with non-closure-or-coroutine-type: {:?}",
1250                closure_or_coroutine_ty
1251            )
1252        }
1253    };
1254
1255    for ty in up_var_tys.iter() {
1256        cx.tcx.assert_fully_normalized(cx.typing_env(), ty);
1257    }
1258
1259    let capture_names = cx.tcx.closure_saved_names_of_captured_variables(def_id);
1260    let layout = cx.layout_of(closure_or_coroutine_ty);
1261
1262    up_var_tys
1263        .into_iter()
1264        .zip(capture_names.iter())
1265        .enumerate()
1266        .map(|(index, (up_var_ty, capture_name))| {
1267            build_field_di_node(
1268                cx,
1269                closure_or_coroutine_di_node,
1270                capture_name.as_str(),
1271                cx.layout_of(up_var_ty),
1272                layout.fields.offset(index),
1273                DIFlags::FlagZero,
1274                type_di_node(cx, up_var_ty),
1275                None,
1276            )
1277        })
1278        .collect()
1279}
1280
1281/// Builds the DW_TAG_structure_type debuginfo node for a Rust tuple type.
1282fn build_tuple_type_di_node<'ll, 'tcx>(
1283    cx: &CodegenCx<'ll, 'tcx>,
1284    unique_type_id: UniqueTypeId<'tcx>,
1285) -> DINodeCreationResult<'ll> {
1286    let tuple_type = unique_type_id.expect_ty();
1287    let &ty::Tuple(component_types) = tuple_type.kind() else {
1288        ::rustc_middle::util::bug::bug_fmt(format_args!("build_tuple_type_di_node() called with non-tuple-type: {0:?}",
        tuple_type))bug!("build_tuple_type_di_node() called with non-tuple-type: {:?}", tuple_type)
1289    };
1290
1291    let tuple_type_and_layout = cx.layout_of(tuple_type);
1292    let type_name = compute_debuginfo_type_name(cx.tcx, tuple_type, false);
1293
1294    type_map::build_type_with_children(
1295        cx,
1296        type_map::stub(
1297            cx,
1298            Stub::Struct,
1299            unique_type_id,
1300            &type_name,
1301            None,
1302            size_and_align_of(tuple_type_and_layout),
1303            NO_SCOPE_METADATA,
1304            DIFlags::FlagZero,
1305        ),
1306        // Fields:
1307        |cx, tuple_di_node| {
1308            component_types
1309                .into_iter()
1310                .enumerate()
1311                .map(|(index, component_type)| {
1312                    build_field_di_node(
1313                        cx,
1314                        tuple_di_node,
1315                        &tuple_field_name(index),
1316                        cx.layout_of(component_type),
1317                        tuple_type_and_layout.fields.offset(index),
1318                        DIFlags::FlagZero,
1319                        type_di_node(cx, component_type),
1320                        None,
1321                    )
1322                })
1323                .collect()
1324        },
1325        NO_GENERICS,
1326    )
1327}
1328
1329/// Builds the debuginfo node for a closure environment.
1330fn build_closure_env_di_node<'ll, 'tcx>(
1331    cx: &CodegenCx<'ll, 'tcx>,
1332    unique_type_id: UniqueTypeId<'tcx>,
1333) -> DINodeCreationResult<'ll> {
1334    let closure_env_type = unique_type_id.expect_ty();
1335    let &(ty::Closure(def_id, _) | ty::CoroutineClosure(def_id, _)) = closure_env_type.kind()
1336    else {
1337        ::rustc_middle::util::bug::bug_fmt(format_args!("build_closure_env_di_node() called with non-closure-type: {0:?}",
        closure_env_type))bug!("build_closure_env_di_node() called with non-closure-type: {:?}", closure_env_type)
1338    };
1339    let containing_scope = get_namespace_for_item(cx, def_id);
1340    let type_name = compute_debuginfo_type_name(cx.tcx, closure_env_type, false);
1341
1342    let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1343        Some(file_metadata_from_def_id(cx, Some(def_id)))
1344    } else {
1345        None
1346    };
1347
1348    type_map::build_type_with_children(
1349        cx,
1350        type_map::stub(
1351            cx,
1352            Stub::Struct,
1353            unique_type_id,
1354            &type_name,
1355            def_location,
1356            cx.size_and_align_of(closure_env_type),
1357            Some(containing_scope),
1358            DIFlags::FlagZero,
1359        ),
1360        // Fields:
1361        |cx, owner| build_upvar_field_di_nodes(cx, closure_env_type, owner),
1362        NO_GENERICS,
1363    )
1364}
1365
1366/// Build the debuginfo node for a Rust `union` type.
1367fn build_union_type_di_node<'ll, 'tcx>(
1368    cx: &CodegenCx<'ll, 'tcx>,
1369    unique_type_id: UniqueTypeId<'tcx>,
1370    span: Span,
1371) -> DINodeCreationResult<'ll> {
1372    let union_type = unique_type_id.expect_ty();
1373    let (union_def_id, variant_def) = match union_type.kind() {
1374        ty::Adt(def, _) => (def.did(), def.non_enum_variant()),
1375        _ => ::rustc_middle::util::bug::bug_fmt(format_args!("build_union_type_di_node on a non-ADT"))bug!("build_union_type_di_node on a non-ADT"),
1376    };
1377    let containing_scope = get_namespace_for_item(cx, union_def_id);
1378    let union_ty_and_layout = cx.spanned_layout_of(union_type, span);
1379    let type_name = compute_debuginfo_type_name(cx.tcx, union_type, false);
1380    let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1381        Some(file_metadata_from_def_id(cx, Some(union_def_id)))
1382    } else {
1383        None
1384    };
1385
1386    type_map::build_type_with_children(
1387        cx,
1388        type_map::stub(
1389            cx,
1390            Stub::Union,
1391            unique_type_id,
1392            &type_name,
1393            def_location,
1394            size_and_align_of(union_ty_and_layout),
1395            Some(containing_scope),
1396            DIFlags::FlagZero,
1397        ),
1398        // Fields:
1399        |cx, owner| {
1400            variant_def
1401                .fields
1402                .iter()
1403                .enumerate()
1404                .map(|(i, f)| {
1405                    let field_layout = union_ty_and_layout.field(cx, i);
1406                    let def_id = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1407                        Some(f.did)
1408                    } else {
1409                        None
1410                    };
1411                    build_field_di_node(
1412                        cx,
1413                        owner,
1414                        f.name.as_str(),
1415                        field_layout,
1416                        Size::ZERO,
1417                        DIFlags::FlagZero,
1418                        type_di_node(cx, field_layout.ty),
1419                        def_id,
1420                    )
1421                })
1422                .collect()
1423        },
1424        // Generics:
1425        |cx| build_generic_type_param_di_nodes(cx, union_type),
1426    )
1427}
1428
1429/// Computes the type parameters for a type, if any, for the given metadata.
1430fn build_generic_type_param_di_nodes<'ll, 'tcx>(
1431    cx: &CodegenCx<'ll, 'tcx>,
1432    ty: Ty<'tcx>,
1433) -> SmallVec<Option<&'ll DIType>> {
1434    if let ty::Adt(def, args) = *ty.kind() {
1435        if args.types().next().is_some() {
1436            let generics = cx.tcx.generics_of(def.did());
1437            let names = get_parameter_names(cx, generics);
1438            let template_params: SmallVec<_> = iter::zip(args, names)
1439                .filter_map(|(kind, name)| {
1440                    kind.as_type().map(|ty| {
1441                        let actual_type = cx
1442                            .tcx
1443                            .normalize_erasing_regions(cx.typing_env(), Unnormalized::new_wip(ty));
1444                        let actual_type_di_node = type_di_node(cx, actual_type);
1445                        Some(cx.create_template_type_parameter(name.as_str(), actual_type_di_node))
1446                    })
1447                })
1448                .collect();
1449
1450            return template_params;
1451        }
1452    }
1453
1454    return ::smallvec::SmallVec::new()smallvec![];
1455
1456    fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec<Symbol> {
1457        let mut names = generics
1458            .parent
1459            .map_or_else(Vec::new, |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id)));
1460        names.extend(generics.own_params.iter().map(|param| param.name));
1461        names
1462    }
1463}
1464
1465/// Creates debug information for the given global variable.
1466///
1467/// Adds the created debuginfo nodes directly to the crate's IR.
1468pub(crate) fn build_global_var_di_node<'ll>(
1469    cx: &CodegenCx<'ll, '_>,
1470    def_id: DefId,
1471    global: &'ll Value,
1472) {
1473    if cx.dbg_cx.is_none() {
1474        return;
1475    }
1476
1477    // Only create type information if full debuginfo is enabled
1478    if cx.sess().opts.debuginfo != DebugInfo::Full {
1479        return;
1480    }
1481
1482    let tcx = cx.tcx;
1483
1484    // We may want to remove the namespace scope if we're in an extern block (see
1485    // https://github.com/rust-lang/rust/pull/46457#issuecomment-351750952).
1486    let var_scope = get_namespace_for_item(cx, def_id);
1487    let (file_metadata, line_number) = file_metadata_from_def_id(cx, Some(def_id));
1488
1489    let is_local_to_unit = is_node_local_to_unit(cx, def_id);
1490
1491    let DefKind::Static { nested, .. } = cx.tcx.def_kind(def_id) else { ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!() };
1492    if nested {
1493        return;
1494    }
1495    let variable_type = Instance::mono(cx.tcx, def_id).ty(cx.tcx, cx.typing_env());
1496    let type_di_node = type_di_node(cx, variable_type);
1497    let var_name = tcx.item_name(def_id);
1498    let var_name = var_name.as_str();
1499    let linkage_name = mangled_name_of_instance(cx, Instance::mono(tcx, def_id)).name;
1500    // When empty, linkage_name field is omitted,
1501    // which is what we want for no_mangle statics
1502    let linkage_name = if var_name == linkage_name { "" } else { linkage_name };
1503
1504    let global_align = cx.align_of(variable_type);
1505
1506    DIB(cx).create_static_variable(
1507        Some(var_scope),
1508        var_name,
1509        linkage_name,
1510        file_metadata,
1511        line_number,
1512        type_di_node,
1513        is_local_to_unit,
1514        global, // (value)
1515        None,   // (decl)
1516        Some(global_align),
1517    );
1518}
1519
1520/// Generates LLVM debuginfo for a vtable.
1521///
1522/// The vtable type looks like a struct with a field for each function pointer and super-trait
1523/// pointer it contains (plus the `size` and `align` fields).
1524///
1525/// Except for `size`, `align`, and `drop_in_place`, the field names don't try to mirror
1526/// the name of the method they implement. This can be implemented in the future once there
1527/// is a proper disambiguation scheme for dealing with methods from different traits that have
1528/// the same name.
1529fn build_vtable_type_di_node<'ll, 'tcx>(
1530    cx: &CodegenCx<'ll, 'tcx>,
1531    ty: Ty<'tcx>,
1532    poly_trait_ref: Option<ty::ExistentialTraitRef<'tcx>>,
1533) -> &'ll DIType {
1534    let tcx = cx.tcx;
1535
1536    let vtable_entries = if let Some(poly_trait_ref) = poly_trait_ref {
1537        let trait_ref = poly_trait_ref.with_self_ty(tcx, ty);
1538        let trait_ref = tcx.erase_and_anonymize_regions(trait_ref);
1539
1540        tcx.vtable_entries(trait_ref)
1541    } else {
1542        TyCtxt::COMMON_VTABLE_ENTRIES
1543    };
1544
1545    // All function pointers are described as opaque pointers. This could be improved in the future
1546    // by describing them as actual function pointers.
1547    let void_pointer_ty = Ty::new_imm_ptr(tcx, tcx.types.unit);
1548    let void_pointer_type_di_node = type_di_node(cx, void_pointer_ty);
1549    let usize_di_node = type_di_node(cx, tcx.types.usize);
1550    let pointer_layout = cx.layout_of(void_pointer_ty);
1551    let pointer_size = pointer_layout.size;
1552    let pointer_align = pointer_layout.align.abi;
1553    // If `usize` is not pointer-sized and -aligned then the size and alignment computations
1554    // for the vtable as a whole would be wrong. Let's make sure this holds even on weird
1555    // platforms.
1556    match (&cx.size_and_align_of(tcx.types.usize), &(pointer_size, pointer_align))
    {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(cx.size_and_align_of(tcx.types.usize), (pointer_size, pointer_align));
1557
1558    let vtable_type_name =
1559        compute_debuginfo_vtable_name(cx.tcx, ty, poly_trait_ref, VTableNameKind::Type);
1560    let unique_type_id = UniqueTypeId::for_vtable_ty(tcx, ty, poly_trait_ref);
1561    let size = pointer_size * vtable_entries.len() as u64;
1562
1563    // This gets mapped to a DW_AT_containing_type attribute which allows GDB to correlate
1564    // the vtable to the type it is for.
1565    let vtable_holder = type_di_node(cx, ty);
1566
1567    build_type_with_children(
1568        cx,
1569        type_map::stub(
1570            cx,
1571            Stub::VTableTy { vtable_holder },
1572            unique_type_id,
1573            &vtable_type_name,
1574            None,
1575            (size, pointer_align),
1576            NO_SCOPE_METADATA,
1577            DIFlags::FlagArtificial,
1578        ),
1579        |cx, vtable_type_di_node| {
1580            vtable_entries
1581                .iter()
1582                .enumerate()
1583                .filter_map(|(index, vtable_entry)| {
1584                    let (field_name, field_type_di_node) = match vtable_entry {
1585                        ty::VtblEntry::MetadataDropInPlace => {
1586                            ("drop_in_place".to_string(), void_pointer_type_di_node)
1587                        }
1588                        ty::VtblEntry::Method(_) => {
1589                            // Note: This code does not try to give a proper name to each method
1590                            //       because their might be multiple methods with the same name
1591                            //       (coming from different traits).
1592                            (::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("__method{0}", index))
    })format!("__method{index}"), void_pointer_type_di_node)
1593                        }
1594                        ty::VtblEntry::TraitVPtr(_) => {
1595                            (::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("__super_trait_ptr{0}", index))
    })format!("__super_trait_ptr{index}"), void_pointer_type_di_node)
1596                        }
1597                        ty::VtblEntry::MetadataAlign => ("align".to_string(), usize_di_node),
1598                        ty::VtblEntry::MetadataSize => ("size".to_string(), usize_di_node),
1599                        ty::VtblEntry::Vacant => return None,
1600                    };
1601
1602                    let field_offset = pointer_size * index as u64;
1603
1604                    Some(build_field_di_node(
1605                        cx,
1606                        vtable_type_di_node,
1607                        &field_name,
1608                        pointer_layout,
1609                        field_offset,
1610                        DIFlags::FlagZero,
1611                        field_type_di_node,
1612                        None,
1613                    ))
1614                })
1615                .collect()
1616        },
1617        NO_GENERICS,
1618    )
1619    .di_node
1620}
1621
1622/// Creates the debuginfo node for `unsafe<'a> T` binder types.
1623///
1624/// We treat an unsafe binder like a struct with a single field named `inner`
1625/// rather than delegating to the inner type's DI node directly. This way the
1626/// debugger shows the binder's own type name, and the wrapped value is still
1627/// accessible through the `inner` field.
1628fn build_unsafe_binder_type_di_node<'ll, 'tcx>(
1629    cx: &CodegenCx<'ll, 'tcx>,
1630    binder_type: Ty<'tcx>,
1631    unique_type_id: UniqueTypeId<'tcx>,
1632) -> DINodeCreationResult<'ll> {
1633    let ty::UnsafeBinder(inner) = binder_type.kind() else {
1634        ::rustc_middle::util::bug::bug_fmt(format_args!("Only ty::UnsafeBinder is valid for build_unsafe_binder_type_di_node. Found {0:?} instead.",
        binder_type))bug!(
1635            "Only ty::UnsafeBinder is valid for build_unsafe_binder_type_di_node. Found {:?} instead.",
1636            binder_type
1637        )
1638    };
1639    let inner_type = inner.skip_binder();
1640    let inner_type_di_node = type_di_node(cx, inner_type);
1641
1642    let type_name = compute_debuginfo_type_name(cx.tcx, binder_type, true);
1643    type_map::build_type_with_children(
1644        cx,
1645        type_map::stub(
1646            cx,
1647            Stub::Struct,
1648            unique_type_id,
1649            &type_name,
1650            None,
1651            cx.size_and_align_of(binder_type),
1652            NO_SCOPE_METADATA,
1653            DIFlags::FlagZero,
1654        ),
1655        |cx, unsafe_binder_type_di_node| {
1656            let inner_layout = cx.layout_of(inner_type);
1657            {
    let count = 0usize + 1usize;
    let mut vec = ::smallvec::SmallVec::new();
    if count <= vec.inline_size() {
        vec.push(build_field_di_node(cx, unsafe_binder_type_di_node, "inner",
                inner_layout, Size::ZERO, DIFlags::FlagZero,
                inner_type_di_node, None));
        vec
    } else {
        ::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                    [build_field_di_node(cx, unsafe_binder_type_di_node,
                                "inner", inner_layout, Size::ZERO, DIFlags::FlagZero,
                                inner_type_di_node, None)])))
    }
}smallvec![build_field_di_node(
1658                cx,
1659                unsafe_binder_type_di_node,
1660                "inner",
1661                inner_layout,
1662                Size::ZERO,
1663                DIFlags::FlagZero,
1664                inner_type_di_node,
1665                None,
1666            )]
1667        },
1668        NO_GENERICS,
1669    )
1670}
1671
1672/// Get the global variable for the vtable.
1673///
1674/// When using global variables, we may have created an addrspacecast to get a pointer to the
1675/// default address space if global variables are created in a different address space.
1676/// For modifying the vtable, we need the real global variable. This function accepts either a
1677/// global variable (which is simply returned), or an addrspacecast constant expression.
1678/// If the given value is an addrspacecast, the cast is removed and the global variable behind
1679/// the cast is returned.
1680fn find_vtable_behind_cast<'ll>(vtable: &'ll Value) -> &'ll Value {
1681    // The vtable is a global variable, which may be behind an addrspacecast.
1682    unsafe {
1683        if let Some(c) = llvm::LLVMIsAConstantExpr(vtable) {
1684            if llvm::LLVMGetConstOpcode(c) == llvm::Opcode::AddrSpaceCast {
1685                return llvm::LLVMGetOperand(c, 0).unwrap();
1686            }
1687        }
1688    }
1689    vtable
1690}
1691
1692pub(crate) fn apply_vcall_visibility_metadata<'ll, 'tcx>(
1693    cx: &CodegenCx<'ll, 'tcx>,
1694    ty: Ty<'tcx>,
1695    trait_ref: Option<ExistentialTraitRef<'tcx>>,
1696    vtable: &'ll Value,
1697) {
1698    // FIXME(flip1995): The virtual function elimination optimization only works with full LTO in
1699    // LLVM at the moment.
1700    if !cx.sess().opts.unstable_opts.virtual_function_elimination || cx.sess().lto() != Lto::Fat {
1701        return;
1702    }
1703
1704    enum VCallVisibility {
1705        Public = 0,
1706        LinkageUnit = 1,
1707        TranslationUnit = 2,
1708    }
1709
1710    let Some(trait_ref) = trait_ref else { return };
1711
1712    // Unwrap potential addrspacecast
1713    let vtable = find_vtable_behind_cast(vtable);
1714    let trait_ref_self = trait_ref.with_self_ty(cx.tcx, ty);
1715    let trait_def_id = trait_ref_self.def_id;
1716    let trait_vis = cx.tcx.visibility(trait_def_id);
1717
1718    let cgus = cx.sess().codegen_units().as_usize();
1719    let single_cgu = cgus == 1;
1720
1721    let lto = cx.sess().lto();
1722
1723    // Since LLVM requires full LTO for the virtual function elimination optimization to apply,
1724    // only the `Lto::Fat` cases are relevant currently.
1725    let vcall_visibility = match (lto, trait_vis, single_cgu) {
1726        // If there is not LTO and the visibility in public, we have to assume that the vtable can
1727        // be seen from anywhere. With multiple CGUs, the vtable is quasi-public.
1728        (Lto::No | Lto::ThinLocal, Visibility::Public, _)
1729        | (Lto::No, Visibility::Restricted(_), false) => VCallVisibility::Public,
1730        // With LTO and a quasi-public visibility, the usages of the functions of the vtable are
1731        // all known by the `LinkageUnit`.
1732        // FIXME: LLVM only supports this optimization for `Lto::Fat` currently. Once it also
1733        // supports `Lto::Thin` the `VCallVisibility` may have to be adjusted for those.
1734        (Lto::Fat | Lto::Thin, Visibility::Public, _)
1735        | (Lto::ThinLocal | Lto::Thin | Lto::Fat, Visibility::Restricted(_), false) => {
1736            VCallVisibility::LinkageUnit
1737        }
1738        // If there is only one CGU, private vtables can only be seen by that CGU/translation unit
1739        // and therefore we know of all usages of functions in the vtable.
1740        (_, Visibility::Restricted(_), true) => VCallVisibility::TranslationUnit,
1741    };
1742
1743    let trait_ref_typeid = typeid_for_trait_ref(cx.tcx, trait_ref);
1744    let typeid = cx.create_metadata(trait_ref_typeid.as_bytes());
1745
1746    let type_ = [llvm::LLVMValueAsMetadata(cx.const_usize(0)), typeid];
1747    cx.global_add_metadata_node(vtable, llvm::MD_type, &type_);
1748
1749    let vcall_visibility = [llvm::LLVMValueAsMetadata(cx.const_u64(vcall_visibility as u64))];
1750    cx.global_set_metadata_node(vtable, llvm::MD_vcall_visibility, &vcall_visibility);
1751}
1752
1753/// Creates debug information for the given vtable, which is for the
1754/// given type.
1755///
1756/// Adds the created metadata nodes directly to the crate's IR.
1757pub(crate) fn create_vtable_di_node<'ll, 'tcx>(
1758    cx: &CodegenCx<'ll, 'tcx>,
1759    ty: Ty<'tcx>,
1760    poly_trait_ref: Option<ty::ExistentialTraitRef<'tcx>>,
1761    vtable: &'ll Value,
1762) {
1763    if cx.dbg_cx.is_none() {
1764        return;
1765    }
1766
1767    // Only create type information if full debuginfo is enabled
1768    if cx.sess().opts.debuginfo != DebugInfo::Full {
1769        return;
1770    }
1771
1772    // Unwrap potential addrspacecast
1773    let vtable = find_vtable_behind_cast(vtable);
1774
1775    // When full debuginfo is enabled, we want to try and prevent vtables from being
1776    // merged. Otherwise debuggers will have a hard time mapping from dyn pointer
1777    // to concrete type.
1778    llvm::set_unnamed_address(vtable, llvm::UnnamedAddr::No);
1779
1780    let vtable_name =
1781        compute_debuginfo_vtable_name(cx.tcx, ty, poly_trait_ref, VTableNameKind::GlobalVariable);
1782    let vtable_type_di_node = build_vtable_type_di_node(cx, ty, poly_trait_ref);
1783
1784    DIB(cx).create_static_variable(
1785        NO_SCOPE_METADATA,
1786        &vtable_name,
1787        "", // (linkage_name)
1788        unknown_file_metadata(cx),
1789        UNKNOWN_LINE_NUMBER,
1790        vtable_type_di_node,
1791        true,   // (is_local_to_unit)
1792        vtable, // (value)
1793        None,   // (decl)
1794        None::<Align>,
1795    );
1796}
1797
1798/// Creates an "extension" of an existing `DIScope` into another file.
1799pub(crate) fn extend_scope_to_file<'ll>(
1800    cx: &CodegenCx<'ll, '_>,
1801    scope_metadata: &'ll DIScope,
1802    file: &SourceFile,
1803) -> &'ll DILexicalBlock {
1804    let file_metadata = file_metadata(cx, file);
1805    unsafe {
1806        llvm::LLVMDIBuilderCreateLexicalBlockFile(
1807            DIB(cx),
1808            scope_metadata,
1809            file_metadata,
1810            /* Discriminator (default) */ 0u32,
1811        )
1812    }
1813}
1814
1815fn tuple_field_name(field_index: usize) -> Cow<'static, str> {
1816    const TUPLE_FIELD_NAMES: [&'static str; 16] = [
1817        "__0", "__1", "__2", "__3", "__4", "__5", "__6", "__7", "__8", "__9", "__10", "__11",
1818        "__12", "__13", "__14", "__15",
1819    ];
1820    TUPLE_FIELD_NAMES
1821        .get(field_index)
1822        .map(|s| Cow::from(*s))
1823        .unwrap_or_else(|| Cow::from(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("__{0}", field_index))
    })format!("__{field_index}")))
1824}
1825
1826pub(crate) type DefinitionLocation<'ll> = (&'ll DIFile, c_uint);
1827
1828pub(crate) fn file_metadata_from_def_id<'ll>(
1829    cx: &CodegenCx<'ll, '_>,
1830    def_id: Option<DefId>,
1831) -> DefinitionLocation<'ll> {
1832    if let Some(def_id) = def_id
1833        && let span = hygiene::walk_chain_collapsed(cx.tcx.def_span(def_id), DUMMY_SP)
1834        && !span.is_dummy()
1835    {
1836        let loc = cx.lookup_debug_loc(span.lo());
1837        (file_metadata(cx, &loc.file), loc.line)
1838    } else {
1839        (unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
1840    }
1841}