rustc_codegen_llvm/debuginfo/
utils.rs
1use 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 !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
57pub(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_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 panic!(
93 "wide_pointer_kind() - Encountered unexpected `pointee_tail_ty`: {pointee_tail_ty:?}"
94 )
95 }
96 }
97}