2015-04-24 03:36:43 +00:00
|
|
|
// Utility Functions.
|
|
|
|
|
2016-04-06 12:37:19 +00:00
|
|
|
use super::namespace::item_namespace;
|
2022-03-03 10:15:25 +00:00
|
|
|
use super::CodegenUnitDebugContext;
|
2015-04-24 03:36:43 +00:00
|
|
|
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir::def_id::DefId;
|
2022-02-23 16:11:50 +00:00
|
|
|
use rustc_middle::ty::layout::{HasParamEnv, LayoutOf};
|
2023-02-22 15:51:17 +00:00
|
|
|
use rustc_middle::ty::{self, Ty};
|
2022-08-31 13:09:26 +00:00
|
|
|
use trace;
|
2015-08-16 10:32:28 +00:00
|
|
|
|
2019-02-17 18:58:58 +00:00
|
|
|
use crate::common::CodegenCx;
|
|
|
|
use crate::llvm;
|
|
|
|
use crate::llvm::debuginfo::{DIArray, DIBuilder, DIDescriptor, DIScope};
|
2015-04-29 06:14:37 +00:00
|
|
|
|
2019-02-25 07:40:18 +00:00
|
|
|
pub fn is_node_local_to_unit(cx: &CodegenCx<'_, '_>, def_id: DefId) -> bool {
|
2015-04-29 06:14:37 +00:00
|
|
|
// The is_local_to_unit flag indicates whether a function is local to the
|
2018-11-27 02:59:49 +00:00
|
|
|
// current compilation unit (i.e., if it is *static* in the C-sense). The
|
2015-04-29 06:14:37 +00:00
|
|
|
// *reachable* set should provide a good approximation of this, as it
|
|
|
|
// contains everything that might leak out of the current crate (by being
|
|
|
|
// externally visible or by being inlined into something externally
|
|
|
|
// visible). It might better to use the `exported_items` set from
|
|
|
|
// `driver::CrateAnalysis` in the future, but (atm) this set is not
|
2018-05-08 13:10:16 +00:00
|
|
|
// available in the codegen pass.
|
2018-02-22 11:18:16 +00:00
|
|
|
!cx.tcx.is_reachable_non_generic(def_id)
|
2015-04-29 06:14:37 +00:00
|
|
|
}
|
2015-04-24 03:36:43 +00:00
|
|
|
|
2015-04-29 06:14:37 +00:00
|
|
|
#[allow(non_snake_case)]
|
2021-12-14 18:49:49 +00:00
|
|
|
pub fn create_DIArray<'ll>(
|
|
|
|
builder: &DIBuilder<'ll>,
|
|
|
|
arr: &[Option<&'ll DIDescriptor>],
|
|
|
|
) -> &'ll DIArray {
|
2020-03-20 14:03:11 +00:00
|
|
|
unsafe { llvm::LLVMRustDIBuilderGetOrCreateArray(builder, arr.as_ptr(), arr.len() as u32) }
|
2015-04-29 06:14:37 +00:00
|
|
|
}
|
2015-04-24 03:36:43 +00:00
|
|
|
|
|
|
|
#[inline]
|
2021-12-14 18:49:49 +00:00
|
|
|
pub fn debug_context<'a, 'll, 'tcx>(
|
|
|
|
cx: &'a CodegenCx<'ll, 'tcx>,
|
2022-03-03 10:15:25 +00:00
|
|
|
) -> &'a CodegenUnitDebugContext<'ll, 'tcx> {
|
2018-01-05 04:58:34 +00:00
|
|
|
cx.dbg_cx.as_ref().unwrap()
|
2015-04-24 03:36:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
#[allow(non_snake_case)]
|
2021-12-14 18:49:49 +00:00
|
|
|
pub fn DIB<'a, 'll>(cx: &'a CodegenCx<'ll, '_>) -> &'a DIBuilder<'ll> {
|
2018-01-05 04:58:34 +00:00
|
|
|
cx.dbg_cx.as_ref().unwrap().builder
|
2015-04-24 03:36:43 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 18:49:49 +00:00
|
|
|
pub fn get_namespace_for_item<'ll>(cx: &CodegenCx<'ll, '_>, def_id: DefId) -> &'ll DIScope {
|
2022-04-25 19:08:45 +00:00
|
|
|
item_namespace(cx, cx.tcx.parent(def_id))
|
2015-04-24 03:36:43 +00:00
|
|
|
}
|
2022-01-13 17:13:54 +00:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub(crate) enum FatPtrKind {
|
|
|
|
Slice,
|
|
|
|
Dyn,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Determines if `pointee_ty` is slice-like or trait-object-like, i.e.
|
|
|
|
/// if the second field of the fat pointer is a length or a vtable-pointer.
|
|
|
|
/// If `pointee_ty` does not require a fat pointer (because it is Sized) then
|
|
|
|
/// the function returns `None`.
|
|
|
|
pub(crate) fn fat_pointer_kind<'ll, 'tcx>(
|
|
|
|
cx: &CodegenCx<'ll, 'tcx>,
|
|
|
|
pointee_ty: Ty<'tcx>,
|
|
|
|
) -> Option<FatPtrKind> {
|
2022-02-23 16:11:50 +00:00
|
|
|
let pointee_tail_ty = cx.tcx.struct_tail_erasing_lifetimes(pointee_ty, cx.param_env());
|
|
|
|
let layout = cx.layout_of(pointee_tail_ty);
|
|
|
|
trace!(
|
|
|
|
"fat_pointer_kind: {:?} has layout {:?} (is_unsized? {})",
|
|
|
|
pointee_tail_ty,
|
|
|
|
layout,
|
|
|
|
layout.is_unsized()
|
|
|
|
);
|
2022-01-13 17:13:54 +00:00
|
|
|
|
2022-11-13 11:14:59 +00:00
|
|
|
if layout.is_sized() {
|
2022-01-13 17:13:54 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2022-02-23 16:11:50 +00:00
|
|
|
match *pointee_tail_ty.kind() {
|
2022-01-13 17:13:54 +00:00
|
|
|
ty::Str | ty::Slice(_) => Some(FatPtrKind::Slice),
|
|
|
|
ty::Dynamic(..) => Some(FatPtrKind::Dyn),
|
|
|
|
ty::Foreign(_) => {
|
|
|
|
// Assert that pointers to foreign types really are thin:
|
|
|
|
debug_assert_eq!(
|
2022-02-23 16:11:50 +00:00
|
|
|
cx.size_of(cx.tcx.mk_imm_ptr(pointee_tail_ty)),
|
2022-01-13 17:13:54 +00:00
|
|
|
cx.size_of(cx.tcx.mk_imm_ptr(cx.tcx.types.u8))
|
|
|
|
);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// For all other pointee types we should already have returned None
|
|
|
|
// at the beginning of the function.
|
2022-02-23 16:11:50 +00:00
|
|
|
panic!(
|
|
|
|
"fat_pointer_kind() - Encountered unexpected `pointee_tail_ty`: {:?}",
|
|
|
|
pointee_tail_ty
|
|
|
|
)
|
2022-01-13 17:13:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|