3 use super::namespace::item_namespace;
4 use super::CodegenUnitDebugContext;
6 use rustc_hir::def_id::DefId;
7 use rustc_middle::ty::layout::{HasParamEnv, LayoutOf};
8 use rustc_middle::ty::{self, DefIdTree, Ty};
11 use crate::common::CodegenCx;
13 use crate::llvm::debuginfo::{DIArray, DIBuilder, DIDescriptor, DIScope};
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)
27 #[allow(non_snake_case)]
28 pub fn create_DIArray<'ll>(
29 builder: &DIBuilder<'ll>,
30 arr: &[Option<&'ll DIDescriptor>],
32 unsafe { llvm::LLVMRustDIBuilderGetOrCreateArray(builder, arr.as_ptr(), arr.len() as u32) }
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()
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
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))
52 #[derive(Debug, PartialEq, Eq)]
53 pub(crate) enum FatPtrKind {
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>,
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);
69 "fat_pointer_kind: {:?} has layout {:?} (is_unsized? {})",
75 if layout.is_sized() {
79 match *pointee_tail_ty.kind() {
80 ty::Str | ty::Slice(_) => Some(FatPtrKind::Slice),
81 ty::Dynamic(..) => Some(FatPtrKind::Dyn),
83 // Assert that pointers to foreign types really are thin:
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))
91 // For all other pointee types we should already have returned None
92 // at the beginning of the function.
94 "fat_pointer_kind() - Encountered unexpected `pointee_tail_ty`: {:?}",