]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_llvm/src/debuginfo/utils.rs
Auto merge of #90291 - geeklint:loosen_weak_debug_bound, r=dtolnay
[rust.git] / compiler / rustc_codegen_llvm / src / debuginfo / utils.rs
1 // Utility Functions.
2
3 use super::namespace::item_namespace;
4 use super::CodegenUnitDebugContext;
5
6 use rustc_hir::def_id::DefId;
7 use rustc_middle::ty::layout::{HasParamEnv, LayoutOf};
8 use rustc_middle::ty::{self, DefIdTree, Ty};
9 use trace;
10
11 use crate::common::CodegenCx;
12 use crate::llvm;
13 use crate::llvm::debuginfo::{DIArray, DIBuilder, DIDescriptor, DIScope};
14
15 pub fn is_node_local_to_unit(cx: &CodegenCx<'_, '_>, def_id: DefId) -> bool {
16     // The is_local_to_unit flag indicates whether a function is local to the
17     // current compilation unit (i.e., if it is *static* in the C-sense). The
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
23     // available in the codegen pass.
24     !cx.tcx.is_reachable_non_generic(def_id)
25 }
26
27 #[allow(non_snake_case)]
28 pub fn create_DIArray<'ll>(
29     builder: &DIBuilder<'ll>,
30     arr: &[Option<&'ll DIDescriptor>],
31 ) -> &'ll DIArray {
32     unsafe { llvm::LLVMRustDIBuilderGetOrCreateArray(builder, arr.as_ptr(), arr.len() as u32) }
33 }
34
35 #[inline]
36 pub fn debug_context<'a, 'll, 'tcx>(
37     cx: &'a CodegenCx<'ll, 'tcx>,
38 ) -> &'a CodegenUnitDebugContext<'ll, 'tcx> {
39     cx.dbg_cx.as_ref().unwrap()
40 }
41
42 #[inline]
43 #[allow(non_snake_case)]
44 pub fn DIB<'a, 'll>(cx: &'a CodegenCx<'ll, '_>) -> &'a DIBuilder<'ll> {
45     cx.dbg_cx.as_ref().unwrap().builder
46 }
47
48 pub fn get_namespace_for_item<'ll>(cx: &CodegenCx<'ll, '_>, def_id: DefId) -> &'ll DIScope {
49     item_namespace(cx, cx.tcx.parent(def_id))
50 }
51
52 #[derive(Debug, PartialEq, Eq)]
53 pub(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`.
62 pub(crate) fn fat_pointer_kind<'ll, 'tcx>(
63     cx: &CodegenCx<'ll, 'tcx>,
64     pointee_ty: Ty<'tcx>,
65 ) -> Option<FatPtrKind> {
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     );
74
75     if layout.is_sized() {
76         return None;
77     }
78
79     match *pointee_tail_ty.kind() {
80         ty::Str | ty::Slice(_) => Some(FatPtrKind::Slice),
81         ty::Dynamic(..) => Some(FatPtrKind::Dyn),
82         ty::Foreign(_) => {
83             // Assert that pointers to foreign types really are thin:
84             debug_assert_eq!(
85                 cx.size_of(cx.tcx.mk_imm_ptr(pointee_tail_ty)),
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.
93             panic!(
94                 "fat_pointer_kind() - Encountered unexpected `pointee_tail_ty`: {:?}",
95                 pointee_tail_ty
96             )
97         }
98     }
99 }