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