Skip to main content

rustc_codegen_llvm/debuginfo/
utils.rs

1// Utility Functions.
2
3use rustc_hir::def_id::DefId;
4use rustc_middle::ty::layout::{HasTypingEnv, LayoutOf};
5use rustc_middle::ty::{self, Ty};
6use tracing::trace;
7
8use super::CodegenUnitDebugContext;
9use super::namespace::item_namespace;
10use crate::common::CodegenCx;
11use crate::llvm;
12use crate::llvm::debuginfo::{DIArray, DIBuilder, DIDescriptor, DIScope};
13
14pub(crate) fn is_node_local_to_unit(cx: &CodegenCx<'_, '_>, def_id: DefId) -> bool {
15    // The is_local_to_unit flag indicates whether a function is local to the
16    // current compilation unit (i.e., if it is *static* in the C-sense). The
17    // *reachable* set should provide a good approximation of this, as it
18    // contains everything that might leak out of the current crate (by being
19    // externally visible or by being inlined into something externally
20    // visible). It might better to use the `exported_items` set from
21    // `driver::CrateAnalysis` in the future, but (atm) this set is not
22    // available in the codegen pass.
23    !cx.tcx.is_reachable_non_generic(def_id)
24}
25
26#[allow(non_snake_case)]
27pub(crate) fn create_DIArray<'ll>(
28    builder: &DIBuilder<'ll>,
29    arr: &[Option<&'ll DIDescriptor>],
30) -> &'ll DIArray {
31    unsafe { llvm::LLVMDIBuilderGetOrCreateArray(builder, arr.as_ptr(), arr.len()) }
32}
33
34#[inline]
35pub(crate) fn debug_context<'a, 'll, 'tcx>(
36    cx: &'a CodegenCx<'ll, 'tcx>,
37) -> &'a CodegenUnitDebugContext<'ll, 'tcx> {
38    cx.dbg_cx.as_ref().unwrap()
39}
40
41#[inline]
42#[allow(non_snake_case)]
43pub(crate) fn DIB<'a, 'll>(cx: &'a CodegenCx<'ll, '_>) -> &'a DIBuilder<'ll> {
44    cx.dbg_cx.as_ref().unwrap().builder.as_ref()
45}
46
47pub(crate) fn get_namespace_for_item<'ll>(cx: &CodegenCx<'ll, '_>, def_id: DefId) -> &'ll DIScope {
48    item_namespace(cx, cx.tcx.parent(def_id))
49}
50
51#[derive(#[automatically_derived]
impl ::core::fmt::Debug for WidePtrKind {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                WidePtrKind::Slice => "Slice",
                WidePtrKind::Dyn => "Dyn",
            })
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for WidePtrKind {
    #[inline]
    fn eq(&self, other: &WidePtrKind) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for WidePtrKind {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) {}
}Eq)]
52pub(crate) enum WidePtrKind {
53    Slice,
54    Dyn,
55}
56
57/// Determines if `pointee_ty` is slice-like or trait-object-like, i.e.
58/// if the second field of the wide pointer is a length or a vtable-pointer.
59/// If `pointee_ty` does not require a wide pointer (because it is Sized) then
60/// the function returns `None`.
61pub(crate) fn wide_pointer_kind<'ll, 'tcx>(
62    cx: &CodegenCx<'ll, 'tcx>,
63    pointee_ty: Ty<'tcx>,
64) -> Option<WidePtrKind> {
65    let pointee_tail_ty = cx.tcx.struct_tail_for_codegen(pointee_ty, cx.typing_env());
66    let layout = cx.layout_of(pointee_tail_ty);
67    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/debuginfo/utils.rs:67",
                        "rustc_codegen_llvm::debuginfo::utils",
                        ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/debuginfo/utils.rs"),
                        ::tracing_core::__macro_support::Option::Some(67u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::debuginfo::utils"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("wide_pointer_kind: {0:?} has layout {1:?} (is_unsized? {2})",
                                                    pointee_tail_ty, layout, layout.is_unsized()) as
                                            &dyn Value))])
            });
    } else { ; }
};trace!(
68        "wide_pointer_kind: {:?} has layout {:?} (is_unsized? {})",
69        pointee_tail_ty,
70        layout,
71        layout.is_unsized()
72    );
73
74    if layout.is_sized() {
75        return None;
76    }
77
78    match *pointee_tail_ty.kind() {
79        ty::Str | ty::Slice(_) => Some(WidePtrKind::Slice),
80        ty::Dynamic(..) => Some(WidePtrKind::Dyn),
81        ty::Foreign(_) => {
82            // Assert that pointers to foreign types really are thin:
83            match (&cx.size_of(Ty::new_imm_ptr(cx.tcx, pointee_tail_ty)),
        &cx.size_of(Ty::new_imm_ptr(cx.tcx, cx.tcx.types.u8))) {
    (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!(
84                cx.size_of(Ty::new_imm_ptr(cx.tcx, pointee_tail_ty)),
85                cx.size_of(Ty::new_imm_ptr(cx.tcx, cx.tcx.types.u8))
86            );
87            None
88        }
89        _ => {
90            // For all other pointee types we should already have returned None
91            // at the beginning of the function.
92            {
    ::core::panicking::panic_fmt(format_args!("wide_pointer_kind() - Encountered unexpected `pointee_tail_ty`: {0:?}",
            pointee_tail_ty));
}panic!(
93                "wide_pointer_kind() - Encountered unexpected `pointee_tail_ty`: {pointee_tail_ty:?}"
94            )
95        }
96    }
97}