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::LLVMRustDIBuilderGetOrCreateArray(builder, arr.as_ptr(), arr.len() as u32) }
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(Debug, PartialEq, 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    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            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            panic!(
93                "wide_pointer_kind() - Encountered unexpected `pointee_tail_ty`: {pointee_tail_ty:?}"
94            )
95        }
96    }
97}