]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_codegen_llvm/src/debuginfo/utils.rs
New upstream version 1.61.0+dfsg1
[rustc.git] / compiler / rustc_codegen_llvm / src / debuginfo / utils.rs
CommitLineData
d9579d0f
AL
1// Utility Functions.
2
a7813a04 3use super::namespace::item_namespace;
ee023bcb 4use super::CodegenUnitDebugContext;
d9579d0f 5
dfeec247 6use rustc_hir::def_id::DefId;
ee023bcb 7use rustc_middle::ty::layout::{HasParamEnv, LayoutOf};
5099ac24 8use rustc_middle::ty::{self, DefIdTree, Ty};
ee023bcb 9use tracing::trace;
e9174d1e 10
dfeec247 11use crate::common::CodegenCx;
9fa01778 12use crate::llvm;
dfeec247 13use crate::llvm::debuginfo::{DIArray, DIBuilder, DIDescriptor, DIScope};
d9579d0f 14
dfeec247 15pub fn is_node_local_to_unit(cx: &CodegenCx<'_, '_>, def_id: DefId) -> bool {
d9579d0f 16 // The is_local_to_unit flag indicates whether a function is local to the
0731742a 17 // current compilation unit (i.e., if it is *static* in the C-sense). The
d9579d0f
AL
18 // *reachable* set should provide a good approximation of this, as it
19 // contains everything that might leak out of the current crate (by being
20 // externally visible or by being inlined into something externally
21 // visible). It might better to use the `exported_items` set from
22 // `driver::CrateAnalysis` in the future, but (atm) this set is not
94b46f34 23 // available in the codegen pass.
0531ce1d 24 !cx.tcx.is_reachable_non_generic(def_id)
d9579d0f
AL
25}
26
27#[allow(non_snake_case)]
a2a8927a
XL
28pub fn create_DIArray<'ll>(
29 builder: &DIBuilder<'ll>,
30 arr: &[Option<&'ll DIDescriptor>],
31) -> &'ll DIArray {
ba9703b0 32 unsafe { llvm::LLVMRustDIBuilderGetOrCreateArray(builder, arr.as_ptr(), arr.len() as u32) }
d9579d0f
AL
33}
34
d9579d0f 35#[inline]
a2a8927a
XL
36pub fn debug_context<'a, 'll, 'tcx>(
37 cx: &'a CodegenCx<'ll, 'tcx>,
ee023bcb 38) -> &'a CodegenUnitDebugContext<'ll, 'tcx> {
2c00a5a8 39 cx.dbg_cx.as_ref().unwrap()
d9579d0f
AL
40}
41
42#[inline]
43#[allow(non_snake_case)]
a2a8927a 44pub fn DIB<'a, 'll>(cx: &'a CodegenCx<'ll, '_>) -> &'a DIBuilder<'ll> {
2c00a5a8 45 cx.dbg_cx.as_ref().unwrap().builder
d9579d0f
AL
46}
47
a2a8927a 48pub fn get_namespace_for_item<'ll>(cx: &CodegenCx<'ll, '_>, def_id: DefId) -> &'ll DIScope {
dfeec247 49 item_namespace(cx, cx.tcx.parent(def_id).expect("get_namespace_for_item: missing parent?"))
d9579d0f 50}
5099ac24
FG
51
52#[derive(Debug, PartialEq, Eq)]
53pub(crate) enum FatPtrKind {
54 Slice,
55 Dyn,
56}
57
58/// Determines if `pointee_ty` is slice-like or trait-object-like, i.e.
59/// if the second field of the fat pointer is a length or a vtable-pointer.
60/// If `pointee_ty` does not require a fat pointer (because it is Sized) then
61/// the function returns `None`.
62pub(crate) fn fat_pointer_kind<'ll, 'tcx>(
63 cx: &CodegenCx<'ll, 'tcx>,
64 pointee_ty: Ty<'tcx>,
65) -> Option<FatPtrKind> {
ee023bcb
FG
66 let pointee_tail_ty = cx.tcx.struct_tail_erasing_lifetimes(pointee_ty, cx.param_env());
67 let layout = cx.layout_of(pointee_tail_ty);
68 trace!(
69 "fat_pointer_kind: {:?} has layout {:?} (is_unsized? {})",
70 pointee_tail_ty,
71 layout,
72 layout.is_unsized()
73 );
5099ac24
FG
74
75 if !layout.is_unsized() {
76 return None;
77 }
78
ee023bcb 79 match *pointee_tail_ty.kind() {
5099ac24
FG
80 ty::Str | ty::Slice(_) => Some(FatPtrKind::Slice),
81 ty::Dynamic(..) => Some(FatPtrKind::Dyn),
5099ac24
FG
82 ty::Foreign(_) => {
83 // Assert that pointers to foreign types really are thin:
84 debug_assert_eq!(
ee023bcb 85 cx.size_of(cx.tcx.mk_imm_ptr(pointee_tail_ty)),
5099ac24
FG
86 cx.size_of(cx.tcx.mk_imm_ptr(cx.tcx.types.u8))
87 );
88 None
89 }
90 _ => {
91 // For all other pointee types we should already have returned None
92 // at the beginning of the function.
ee023bcb
FG
93 panic!(
94 "fat_pointer_kind() - Encountered unexpected `pointee_tail_ty`: {:?}",
95 pointee_tail_ty
96 )
5099ac24
FG
97 }
98 }
99}