1use std::borrow::Cow;
2use std::fmt::{self, Write};
3use std::hash::{Hash, Hasher};
4use std::path::PathBuf;
5use std::sync::Arc;
6use std::{assert_matches, iter, ptr};
7
8use libc::{c_longlong, c_uint};
9use rustc_abi::{Align, Layout, NumScalableVectors, Size};
10use rustc_codegen_ssa::debuginfo::type_names::{VTableNameKind, cpp_like_debuginfo};
11use rustc_codegen_ssa::traits::*;
12use rustc_hir::def::{CtorKind, DefKind};
13use rustc_hir::def_id::{DefId, LOCAL_CRATE};
14use rustc_middle::bug;
15use rustc_middle::ty::layout::{
16 HasTypingEnv, LayoutOf, TyAndLayout, WIDE_PTR_ADDR, WIDE_PTR_EXTRA,
17};
18use rustc_middle::ty::{
19 self, AdtDef, AdtKind, CoroutineArgsExt, ExistentialTraitRef, Instance, Ty, TyCtxt,
20 Unnormalized, Visibility,
21};
22use rustc_session::config::{self, DebugInfo, Lto};
23use rustc_span::{DUMMY_SP, FileName, RemapPathScopeComponents, SourceFile, Span, Symbol, hygiene};
24use rustc_symbol_mangling::typeid_for_trait_ref;
25use rustc_target::spec::{Arch, DebuginfoKind};
26use smallvec::smallvec;
27use tracing::{debug, instrument};
28
29pub(crate) use self::type_map::TypeMap;
30use self::type_map::{DINodeCreationResult, Stub, UniqueTypeId};
31use super::CodegenUnitDebugContext;
32use super::namespace::mangled_name_of_instance;
33use super::type_names::{compute_debuginfo_type_name, compute_debuginfo_vtable_name};
34use super::utils::{DIB, debug_context, get_namespace_for_item, is_node_local_to_unit};
35use crate::common::{AsCCharPtr, CodegenCx};
36use crate::debuginfo::metadata::type_map::build_type_with_children;
37use crate::debuginfo::utils::{WidePtrKind, create_DIArray, wide_pointer_kind};
38use crate::debuginfo::{DIBuilderExt, dwarf_const};
39use crate::llvm::debuginfo::{
40 DIBasicType, DIBuilder, DICompositeType, DIDescriptor, DIFile, DIFlags, DILexicalBlock,
41 DIScope, DIType, DebugEmissionKind, DebugNameTableKind,
42};
43use crate::llvm::{self, FromGeneric, Value};
44
45impl PartialEq for llvm::Metadata {
46 fn eq(&self, other: &Self) -> bool {
47 ptr::eq(self, other)
48 }
49}
50
51impl Eq for llvm::Metadata {}
52
53impl Hash for llvm::Metadata {
54 fn hash<H: Hasher>(&self, hasher: &mut H) {
55 (self as *const Self).hash(hasher);
56 }
57}
58
59impl fmt::Debug for llvm::Metadata {
60 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61 (self as *const Self).fmt(f)
62 }
63}
64
65pub(super) const UNKNOWN_LINE_NUMBER: c_uint = 0;
66pub(super) const UNKNOWN_COLUMN_NUMBER: c_uint = 0;
67
68const NO_SCOPE_METADATA: Option<&DIScope> = None;
69const NO_GENERICS: for<'ll> fn(&CodegenCx<'ll, '_>) -> SmallVec<Option<&'ll DIType>> =
71 |_| SmallVec::new();
72
73type SmallVec<T> = smallvec::SmallVec<[T; 16]>;
76
77mod enums;
78mod type_map;
79
80macro_rules! return_if_di_node_created_in_meantime {
83 ($cx: expr, $unique_type_id: expr) => {
84 if let Some(di_node) = debug_context($cx).type_map.di_node_for_unique_id($unique_type_id) {
85 return DINodeCreationResult::new(di_node, true);
86 }
87 };
88}
89
90#[inline]
92fn size_and_align_of(ty_and_layout: TyAndLayout<'_>) -> (Size, Align) {
93 (ty_and_layout.size, ty_and_layout.align.abi)
94}
95
96fn build_fixed_size_array_di_node<'ll, 'tcx>(
99 cx: &CodegenCx<'ll, 'tcx>,
100 unique_type_id: UniqueTypeId<'tcx>,
101 array_type: Ty<'tcx>,
102 span: Span,
103) -> DINodeCreationResult<'ll> {
104 let ty::Array(element_type, len) = array_type.kind() else {
105 ::rustc_middle::util::bug::bug_fmt(format_args!("build_fixed_size_array_di_node() called with non-ty::Array type `{0:?}`",
array_type))bug!("build_fixed_size_array_di_node() called with non-ty::Array type `{:?}`", array_type)
106 };
107
108 let element_type_di_node = spanned_type_di_node(cx, *element_type, span);
109
110 if let Some(di_node) =
debug_context(cx).type_map.di_node_for_unique_id(unique_type_id) {
return DINodeCreationResult::new(di_node, true);
};return_if_di_node_created_in_meantime!(cx, unique_type_id);
111
112 let (size, align) = cx.spanned_size_and_align_of(array_type, span);
113
114 let upper_bound = len
115 .try_to_target_usize(cx.tcx)
116 .expect("expected monomorphic const in codegen") as c_longlong;
117
118 let subrange = unsafe { llvm::LLVMDIBuilderGetOrCreateSubrange(DIB(cx), 0, upper_bound) };
119 let subscripts = &[subrange];
120
121 let di_node = unsafe {
122 llvm::LLVMDIBuilderCreateArrayType(
123 DIB(cx),
124 size.bits(),
125 align.bits() as u32,
126 element_type_di_node,
127 subscripts.as_ptr(),
128 subscripts.len() as c_uint,
129 )
130 };
131
132 DINodeCreationResult::new(di_node, false)
133}
134
135fn build_pointer_or_reference_di_node<'ll, 'tcx>(
144 cx: &CodegenCx<'ll, 'tcx>,
145 ptr_type: Ty<'tcx>,
146 pointee_type: Ty<'tcx>,
147 unique_type_id: UniqueTypeId<'tcx>,
148) -> DINodeCreationResult<'ll> {
149 match (&cx.size_and_align_of(ptr_type),
&cx.size_and_align_of(Ty::new_mut_ptr(cx.tcx, pointee_type))) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_eq!(
152 cx.size_and_align_of(ptr_type),
153 cx.size_and_align_of(Ty::new_mut_ptr(cx.tcx, pointee_type))
154 );
155
156 let pointee_type_di_node = type_di_node(cx, pointee_type);
157
158 if let Some(di_node) =
debug_context(cx).type_map.di_node_for_unique_id(unique_type_id) {
return DINodeCreationResult::new(di_node, true);
};return_if_di_node_created_in_meantime!(cx, unique_type_id);
159
160 let data_layout = &cx.tcx.data_layout;
161 let pointer_size = data_layout.pointer_size();
162 let pointer_align = data_layout.pointer_align();
163 let ptr_type_debuginfo_name = compute_debuginfo_type_name(cx.tcx, ptr_type, true);
164
165 match wide_pointer_kind(cx, pointee_type) {
166 None => {
167 match (&(pointer_size, pointer_align.abi), &cx.size_and_align_of(ptr_type)) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::Some(format_args!("ptr_type={0}, pointee_type={1}",
ptr_type, pointee_type)));
}
}
};assert_eq!(
169 (pointer_size, pointer_align.abi),
170 cx.size_and_align_of(ptr_type),
171 "ptr_type={ptr_type}, pointee_type={pointee_type}",
172 );
173
174 let di_node = create_pointer_type(
175 cx,
176 pointee_type_di_node,
177 pointer_size,
178 pointer_align.abi,
179 &ptr_type_debuginfo_name,
180 );
181
182 DINodeCreationResult { di_node, already_stored_in_typemap: false }
183 }
184 Some(wide_pointer_kind) => {
185 type_map::build_type_with_children(
186 cx,
187 type_map::stub(
188 cx,
189 Stub::Struct,
190 unique_type_id,
191 &ptr_type_debuginfo_name,
192 None,
193 cx.size_and_align_of(ptr_type),
194 NO_SCOPE_METADATA,
195 DIFlags::FlagZero,
196 ),
197 |cx, owner| {
198 let layout_type = if ptr_type.is_box() {
206 Ty::new_mut_ptr(cx.tcx, pointee_type)
210 } else {
211 ptr_type
212 };
213
214 let layout = cx.layout_of(layout_type);
215 let addr_field = layout.field(cx, WIDE_PTR_ADDR);
216 let extra_field = layout.field(cx, WIDE_PTR_EXTRA);
217
218 let (addr_field_name, extra_field_name) = match wide_pointer_kind {
219 WidePtrKind::Dyn => ("pointer", "vtable"),
220 WidePtrKind::Slice => ("data_ptr", "length"),
221 };
222
223 match (&WIDE_PTR_ADDR, &0) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_eq!(WIDE_PTR_ADDR, 0);
224 match (&WIDE_PTR_EXTRA, &1) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_eq!(WIDE_PTR_EXTRA, 1);
225
226 let data_ptr_type_di_node = create_pointer_type(
229 cx,
230 pointee_type_di_node,
231 addr_field.size,
232 addr_field.align.abi,
233 "",
234 );
235
236 {
let count = 0usize + 1usize + 1usize;
let mut vec = ::smallvec::SmallVec::new();
if count <= vec.inline_size() {
vec.push(build_field_di_node(cx, owner, addr_field_name, addr_field,
layout.fields.offset(WIDE_PTR_ADDR), DIFlags::FlagZero,
data_ptr_type_di_node, None));
vec.push(build_field_di_node(cx, owner, extra_field_name, extra_field,
layout.fields.offset(WIDE_PTR_EXTRA), DIFlags::FlagZero,
type_di_node(cx, extra_field.ty), None));
vec
} else {
::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[build_field_di_node(cx, owner, addr_field_name, addr_field,
layout.fields.offset(WIDE_PTR_ADDR), DIFlags::FlagZero,
data_ptr_type_di_node, None),
build_field_di_node(cx, owner, extra_field_name,
extra_field, layout.fields.offset(WIDE_PTR_EXTRA),
DIFlags::FlagZero, type_di_node(cx, extra_field.ty),
None)])))
}
}smallvec![
237 build_field_di_node(
238 cx,
239 owner,
240 addr_field_name,
241 addr_field,
242 layout.fields.offset(WIDE_PTR_ADDR),
243 DIFlags::FlagZero,
244 data_ptr_type_di_node,
245 None,
246 ),
247 build_field_di_node(
248 cx,
249 owner,
250 extra_field_name,
251 extra_field,
252 layout.fields.offset(WIDE_PTR_EXTRA),
253 DIFlags::FlagZero,
254 type_di_node(cx, extra_field.ty),
255 None,
256 ),
257 ]
258 },
259 NO_GENERICS,
260 )
261 }
262 }
263}
264
265fn build_subroutine_type_di_node<'ll, 'tcx>(
266 cx: &CodegenCx<'ll, 'tcx>,
267 unique_type_id: UniqueTypeId<'tcx>,
268) -> DINodeCreationResult<'ll> {
269 debug_context(cx)
282 .type_map
283 .unique_id_to_di_node
284 .borrow_mut()
285 .insert(unique_type_id, recursion_marker_type_di_node(cx));
286
287 let fn_ty = unique_type_id.expect_ty();
288 let signature =
289 cx.tcx.normalize_erasing_late_bound_regions(cx.typing_env(), fn_ty.fn_sig(cx.tcx));
290
291 let signature_di_nodes: SmallVec<_> = iter::once(
292 match signature.output().kind() {
294 ty::Tuple(tys) if tys.is_empty() => {
295 None
297 }
298 _ => Some(type_di_node(cx, signature.output())),
299 },
300 )
301 .chain(
302 signature.inputs().iter().map(|&argument_type| Some(type_di_node(cx, argument_type))),
304 )
305 .collect();
306
307 debug_context(cx).type_map.unique_id_to_di_node.borrow_mut().remove(&unique_type_id);
308
309 let fn_di_node = create_subroutine_type(cx, &signature_di_nodes[..]);
310
311 let name = compute_debuginfo_type_name(cx.tcx, fn_ty, false);
313 let (size, align) = match fn_ty.kind() {
314 ty::FnDef(..) => (Size::ZERO, Align::ONE),
315 ty::FnPtr(..) => {
316 (cx.tcx.data_layout.pointer_size(), cx.tcx.data_layout.pointer_align().abi)
317 }
318 _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
319 };
320 let di_node = create_pointer_type(cx, fn_di_node, size, align, &name);
321
322 DINodeCreationResult::new(di_node, false)
323}
324
325pub(super) fn create_subroutine_type<'ll>(
326 cx: &CodegenCx<'ll, '_>,
327 signature: &[Option<&'ll llvm::Metadata>],
328) -> &'ll DICompositeType {
329 unsafe {
330 llvm::LLVMDIBuilderCreateSubroutineType(
331 DIB(cx),
332 None, signature.as_ptr(),
334 signature.len() as c_uint,
335 DIFlags::FlagZero, )
337 }
338}
339
340fn create_pointer_type<'ll>(
341 cx: &CodegenCx<'ll, '_>,
342 pointee_ty: &'ll llvm::Metadata,
343 size: Size,
344 align: Align,
345 name: &str,
346) -> &'ll llvm::Metadata {
347 unsafe {
348 llvm::LLVMDIBuilderCreatePointerType(
349 DIB(cx),
350 pointee_ty,
351 size.bits(),
352 align.bits() as u32,
353 0, name.as_ptr(),
355 name.len(),
356 )
357 }
358}
359
360fn build_dyn_type_di_node<'ll, 'tcx>(
363 cx: &CodegenCx<'ll, 'tcx>,
364 dyn_type: Ty<'tcx>,
365 unique_type_id: UniqueTypeId<'tcx>,
366) -> DINodeCreationResult<'ll> {
367 if let ty::Dynamic(..) = dyn_type.kind() {
368 let type_name = compute_debuginfo_type_name(cx.tcx, dyn_type, true);
369 type_map::build_type_with_children(
370 cx,
371 type_map::stub(
372 cx,
373 Stub::Struct,
374 unique_type_id,
375 &type_name,
376 None,
377 cx.size_and_align_of(dyn_type),
378 NO_SCOPE_METADATA,
379 DIFlags::FlagZero,
380 ),
381 |_, _| ::smallvec::SmallVec::new()smallvec![],
382 NO_GENERICS,
383 )
384 } else {
385 ::rustc_middle::util::bug::bug_fmt(format_args!("Only ty::Dynamic is valid for build_dyn_type_di_node(). Found {0:?} instead.",
dyn_type))bug!(
386 "Only ty::Dynamic is valid for build_dyn_type_di_node(). Found {:?} instead.",
387 dyn_type
388 )
389 }
390}
391
392fn build_slice_type_di_node<'ll, 'tcx>(
410 cx: &CodegenCx<'ll, 'tcx>,
411 slice_type: Ty<'tcx>,
412 unique_type_id: UniqueTypeId<'tcx>,
413) -> DINodeCreationResult<'ll> {
414 let element_type = match slice_type.kind() {
415 ty::Slice(element_type) => *element_type,
416 ty::Str => cx.tcx.types.u8,
417 _ => {
418 ::rustc_middle::util::bug::bug_fmt(format_args!("Only ty::Slice is valid for build_slice_type_di_node(). Found {0:?} instead.",
slice_type))bug!(
419 "Only ty::Slice is valid for build_slice_type_di_node(). Found {:?} instead.",
420 slice_type
421 )
422 }
423 };
424
425 let element_type_di_node = type_di_node(cx, element_type);
426 if let Some(di_node) =
debug_context(cx).type_map.di_node_for_unique_id(unique_type_id) {
return DINodeCreationResult::new(di_node, true);
};return_if_di_node_created_in_meantime!(cx, unique_type_id);
427 DINodeCreationResult { di_node: element_type_di_node, already_stored_in_typemap: false }
428}
429
430pub(crate) fn type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) -> &'ll DIType {
435 spanned_type_di_node(cx, t, DUMMY_SP)
436}
437
438pub(crate) fn spanned_type_di_node<'ll, 'tcx>(
439 cx: &CodegenCx<'ll, 'tcx>,
440 t: Ty<'tcx>,
441 span: Span,
442) -> &'ll DIType {
443 let unique_type_id = UniqueTypeId::for_ty(cx.tcx, t);
444
445 if let Some(existing_di_node) = debug_context(cx).type_map.di_node_for_unique_id(unique_type_id)
446 {
447 return existing_di_node;
448 }
449
450 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs:450",
"rustc_codegen_llvm::debuginfo::metadata",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs"),
::tracing_core::__macro_support::Option::Some(450u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::debuginfo::metadata"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("type_di_node: {0:?} kind: {1:?}",
t, t.kind()) as &dyn Value))])
});
} else { ; }
};debug!("type_di_node: {:?} kind: {:?}", t, t.kind());
451
452 let DINodeCreationResult { di_node, already_stored_in_typemap } = match *t.kind() {
453 ty::Never | ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) => {
454 build_basic_type_di_node(cx, t)
455 }
456 ty::Tuple(elements) if elements.is_empty() => build_basic_type_di_node(cx, t),
457 ty::Array(..) => build_fixed_size_array_di_node(cx, unique_type_id, t, span),
458 ty::Slice(_) | ty::Str => build_slice_type_di_node(cx, t, unique_type_id),
459 ty::Dynamic(..) => build_dyn_type_di_node(cx, t, unique_type_id),
460 ty::Foreign(..) => build_foreign_type_di_node(cx, t, unique_type_id),
461 ty::RawPtr(pointee_type, _) | ty::Ref(_, pointee_type, _) => {
462 build_pointer_or_reference_di_node(cx, t, pointee_type, unique_type_id)
463 }
464 ty::Adt(def, args)
468 if def.is_box()
469 && args.get(1).is_none_or(|arg| cx.layout_of(arg.expect_ty()).is_1zst()) =>
470 {
471 build_pointer_or_reference_di_node(cx, t, t.expect_boxed_ty(), unique_type_id)
472 }
473 ty::FnDef(..) | ty::FnPtr(..) => build_subroutine_type_di_node(cx, unique_type_id),
474 ty::Closure(..) => build_closure_env_di_node(cx, unique_type_id),
475 ty::CoroutineClosure(..) => build_closure_env_di_node(cx, unique_type_id),
476 ty::Coroutine(..) => enums::build_coroutine_di_node(cx, unique_type_id),
477 ty::Adt(def, ..) => match def.adt_kind() {
478 AdtKind::Struct => build_struct_type_di_node(cx, unique_type_id, span),
479 AdtKind::Union => build_union_type_di_node(cx, unique_type_id, span),
480 AdtKind::Enum => enums::build_enum_type_di_node(cx, unique_type_id, span),
481 },
482 ty::Tuple(_) => build_tuple_type_di_node(cx, unique_type_id),
483 ty::Pat(base, _) => return type_di_node(cx, base),
484 ty::UnsafeBinder(_) => build_unsafe_binder_type_di_node(cx, t, unique_type_id),
485 ty::Alias(..)
486 | ty::Param(_)
487 | ty::Bound(..)
488 | ty::Infer(_)
489 | ty::Placeholder(_)
490 | ty::CoroutineWitness(..)
491 | ty::Error(_) => {
492 ::rustc_middle::util::bug::bug_fmt(format_args!("debuginfo: unexpected type in type_di_node(): {0:?}",
t))bug!("debuginfo: unexpected type in type_di_node(): {:?}", t)
493 }
494 };
495
496 {
497 if already_stored_in_typemap {
498 let di_node_for_uid =
500 match debug_context(cx).type_map.di_node_for_unique_id(unique_type_id) {
501 Some(di_node) => di_node,
502 None => {
503 ::rustc_middle::util::bug::bug_fmt(format_args!("expected type debuginfo node for unique type ID \'{0:?}\' to already be in the `debuginfo::TypeMap` but it was not.",
unique_type_id));bug!(
504 "expected type debuginfo node for unique \
505 type ID '{:?}' to already be in \
506 the `debuginfo::TypeMap` but it \
507 was not.",
508 unique_type_id,
509 );
510 }
511 };
512
513 match (&(di_node_for_uid as *const _), &(di_node as *const _)) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_eq!(di_node_for_uid as *const _, di_node as *const _);
514 } else {
515 debug_context(cx).type_map.insert(unique_type_id, di_node);
516 }
517 }
518
519 di_node
520}
521
522fn recursion_marker_type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) -> &'ll DIType {
524 *debug_context(cx).recursion_marker_type.get_or_init(move || {
525 create_basic_type(
536 cx,
537 "<recur_type>",
538 cx.tcx.data_layout.pointer_size(),
539 dwarf_const::DW_ATE_unsigned,
540 )
541 })
542}
543
544fn hex_encode(data: &[u8]) -> String {
545 let mut hex_string = String::with_capacity(data.len() * 2);
546 for byte in data.iter() {
547 (&mut hex_string).write_fmt(format_args!("{0:02x}", byte))write!(&mut hex_string, "{byte:02x}").unwrap();
548 }
549 hex_string
550}
551
552pub(crate) fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) -> &'ll DIFile {
553 let cache_key = Some((source_file.stable_id, source_file.src_hash));
554 return debug_context(cx)
555 .created_files
556 .borrow_mut()
557 .entry(cache_key)
558 .or_insert_with(|| alloc_new_file_metadata(cx, source_file));
559
560 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("alloc_new_file_metadata",
"rustc_codegen_llvm::debuginfo::metadata",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs"),
::tracing_core::__macro_support::Option::Some(560u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::debuginfo::metadata"),
::tracing_core::field::FieldSet::new(&[],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{ meta.fields().value_set(&[]) })
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: &'ll DIFile = loop {};
return __tracing_attr_fake_return;
}
{
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs:565",
"rustc_codegen_llvm::debuginfo::metadata",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs"),
::tracing_core::__macro_support::Option::Some(565u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::debuginfo::metadata"),
::tracing_core::field::FieldSet::new(&["source_file.name"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&source_file.name)
as &dyn Value))])
});
} else { ; }
};
let (directory, file_name) =
match &source_file.name {
FileName::Real(filename) => {
let (working_directory, embeddable_name) =
filename.embeddable_name(RemapPathScopeComponents::DEBUGINFO);
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs:572",
"rustc_codegen_llvm::debuginfo::metadata",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs"),
::tracing_core::__macro_support::Option::Some(572u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::debuginfo::metadata"),
::tracing_core::field::FieldSet::new(&["working_directory",
"embeddable_name"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&working_directory)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&embeddable_name)
as &dyn Value))])
});
} else { ; }
};
if let Ok(rel_path) =
embeddable_name.strip_prefix(working_directory) {
(working_directory.to_string_lossy(),
rel_path.to_string_lossy().into_owned())
} else {
("".into(), embeddable_name.to_string_lossy().into_owned())
}
}
other => {
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs:597",
"rustc_codegen_llvm::debuginfo::metadata",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs"),
::tracing_core::__macro_support::Option::Some(597u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::debuginfo::metadata"),
::tracing_core::field::FieldSet::new(&["other"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&other) as
&dyn Value))])
});
} else { ; }
};
("".into(),
other.display(RemapPathScopeComponents::DEBUGINFO).to_string())
}
};
let hash_kind =
match source_file.src_hash.kind {
rustc_span::SourceFileHashAlgorithm::Md5 =>
llvm::ChecksumKind::MD5,
rustc_span::SourceFileHashAlgorithm::Sha1 =>
llvm::ChecksumKind::SHA1,
rustc_span::SourceFileHashAlgorithm::Sha256 =>
llvm::ChecksumKind::SHA256,
rustc_span::SourceFileHashAlgorithm::Blake3 =>
llvm::ChecksumKind::None,
};
let hash_value = hex_encode(source_file.src_hash.hash_bytes());
let source =
cx.sess().opts.unstable_opts.embed_source.then_some(()).and(source_file.src.as_ref());
create_file(DIB(cx), &file_name, &directory, &hash_value,
hash_kind, source)
}
}
}#[instrument(skip(cx, source_file), level = "debug")]
561 fn alloc_new_file_metadata<'ll>(
562 cx: &CodegenCx<'ll, '_>,
563 source_file: &SourceFile,
564 ) -> &'ll DIFile {
565 debug!(?source_file.name);
566
567 let (directory, file_name) = match &source_file.name {
568 FileName::Real(filename) => {
569 let (working_directory, embeddable_name) =
570 filename.embeddable_name(RemapPathScopeComponents::DEBUGINFO);
571
572 debug!(?working_directory, ?embeddable_name);
573
574 if let Ok(rel_path) = embeddable_name.strip_prefix(working_directory) {
575 (working_directory.to_string_lossy(), rel_path.to_string_lossy().into_owned())
592 } else {
593 ("".into(), embeddable_name.to_string_lossy().into_owned())
594 }
595 }
596 other => {
597 debug!(?other);
598 ("".into(), other.display(RemapPathScopeComponents::DEBUGINFO).to_string())
599 }
600 };
601
602 let hash_kind = match source_file.src_hash.kind {
603 rustc_span::SourceFileHashAlgorithm::Md5 => llvm::ChecksumKind::MD5,
604 rustc_span::SourceFileHashAlgorithm::Sha1 => llvm::ChecksumKind::SHA1,
605 rustc_span::SourceFileHashAlgorithm::Sha256 => llvm::ChecksumKind::SHA256,
606 rustc_span::SourceFileHashAlgorithm::Blake3 => llvm::ChecksumKind::None,
607 };
608 let hash_value = hex_encode(source_file.src_hash.hash_bytes());
609
610 let source =
611 cx.sess().opts.unstable_opts.embed_source.then_some(()).and(source_file.src.as_ref());
612
613 create_file(DIB(cx), &file_name, &directory, &hash_value, hash_kind, source)
614 }
615}
616
617fn unknown_file_metadata<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll DIFile {
618 debug_context(cx).created_files.borrow_mut().entry(None).or_insert_with(|| {
619 create_file(DIB(cx), "<unknown>", "", "", llvm::ChecksumKind::None, None)
620 })
621}
622
623fn create_file<'ll>(
624 builder: &DIBuilder<'ll>,
625 file_name: &str,
626 directory: &str,
627 hash_value: &str,
628 hash_kind: llvm::ChecksumKind,
629 source: Option<&Arc<String>>,
630) -> &'ll DIFile {
631 unsafe {
632 llvm::LLVMRustDIBuilderCreateFile(
633 builder,
634 file_name.as_c_char_ptr(),
635 file_name.len(),
636 directory.as_c_char_ptr(),
637 directory.len(),
638 hash_kind,
639 hash_value.as_c_char_ptr(),
640 hash_value.len(),
641 source.map_or(ptr::null(), |x| x.as_c_char_ptr()),
642 source.map_or(0, |x| x.len()),
643 )
644 }
645}
646
647trait MsvcBasicName {
648 fn msvc_basic_name(self) -> &'static str;
649}
650
651impl MsvcBasicName for ty::IntTy {
652 fn msvc_basic_name(self) -> &'static str {
653 match self {
654 ty::IntTy::Isize => "ptrdiff_t",
655 ty::IntTy::I8 => "__int8",
656 ty::IntTy::I16 => "__int16",
657 ty::IntTy::I32 => "__int32",
658 ty::IntTy::I64 => "__int64",
659 ty::IntTy::I128 => "__int128",
660 }
661 }
662}
663
664impl MsvcBasicName for ty::UintTy {
665 fn msvc_basic_name(self) -> &'static str {
666 match self {
667 ty::UintTy::Usize => "size_t",
668 ty::UintTy::U8 => "unsigned __int8",
669 ty::UintTy::U16 => "unsigned __int16",
670 ty::UintTy::U32 => "unsigned __int32",
671 ty::UintTy::U64 => "unsigned __int64",
672 ty::UintTy::U128 => "unsigned __int128",
673 }
674 }
675}
676
677impl MsvcBasicName for ty::FloatTy {
678 fn msvc_basic_name(self) -> &'static str {
679 match self {
682 ty::FloatTy::F16 => {
683 ::rustc_middle::util::bug::bug_fmt(format_args!("`f16` should have been handled in `build_basic_type_di_node`"))bug!("`f16` should have been handled in `build_basic_type_di_node`")
684 }
685 ty::FloatTy::F32 => "float",
686 ty::FloatTy::F64 => "double",
687 ty::FloatTy::F128 => "fp128",
688 }
689 }
690}
691
692fn build_cpp_f16_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) -> DINodeCreationResult<'ll> {
693 let float_ty = cx.tcx.types.f16;
696 let bits_ty = cx.tcx.types.u16;
697 let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
698 match float_ty.kind() {
699 ty::Adt(def, _) => Some(file_metadata_from_def_id(cx, Some(def.did()))),
700 _ => None,
701 }
702 } else {
703 None
704 };
705 type_map::build_type_with_children(
706 cx,
707 type_map::stub(
708 cx,
709 Stub::Struct,
710 UniqueTypeId::for_ty(cx.tcx, float_ty),
711 "f16",
712 def_location,
713 cx.size_and_align_of(float_ty),
714 NO_SCOPE_METADATA,
715 DIFlags::FlagZero,
716 ),
717 |cx, float_di_node| {
719 let def_id = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
720 match bits_ty.kind() {
721 ty::Adt(def, _) => Some(def.did()),
722 _ => None,
723 }
724 } else {
725 None
726 };
727 {
let count = 0usize + 1usize;
let mut vec = ::smallvec::SmallVec::new();
if count <= vec.inline_size() {
vec.push(build_field_di_node(cx, float_di_node, "bits",
cx.layout_of(bits_ty), Size::ZERO, DIFlags::FlagZero,
type_di_node(cx, bits_ty), def_id));
vec
} else {
::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[build_field_di_node(cx, float_di_node, "bits",
cx.layout_of(bits_ty), Size::ZERO, DIFlags::FlagZero,
type_di_node(cx, bits_ty), def_id)])))
}
}smallvec![build_field_di_node(
728 cx,
729 float_di_node,
730 "bits",
731 cx.layout_of(bits_ty),
732 Size::ZERO,
733 DIFlags::FlagZero,
734 type_di_node(cx, bits_ty),
735 def_id,
736 )]
737 },
738 NO_GENERICS,
739 )
740}
741
742fn build_basic_type_di_node<'ll, 'tcx>(
743 cx: &CodegenCx<'ll, 'tcx>,
744 t: Ty<'tcx>,
745) -> DINodeCreationResult<'ll> {
746 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs:746",
"rustc_codegen_llvm::debuginfo::metadata",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs"),
::tracing_core::__macro_support::Option::Some(746u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::debuginfo::metadata"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("build_basic_type_di_node: {0:?}",
t) as &dyn Value))])
});
} else { ; }
};debug!("build_basic_type_di_node: {:?}", t);
747
748 let cpp_like_debuginfo = cpp_like_debuginfo(cx.tcx);
751
752 use dwarf_const::{DW_ATE_UTF, DW_ATE_boolean, DW_ATE_float, DW_ATE_signed, DW_ATE_unsigned};
753
754 let (name, encoding) = match t.kind() {
755 ty::Never => ("!", DW_ATE_unsigned),
756 ty::Tuple(elements) if elements.is_empty() => {
757 if cpp_like_debuginfo {
758 return build_tuple_type_di_node(cx, UniqueTypeId::for_ty(cx.tcx, t));
759 } else {
760 ("()", DW_ATE_unsigned)
761 }
762 }
763 ty::Bool => ("bool", DW_ATE_boolean),
764 ty::Char => ("char", DW_ATE_UTF),
765 ty::Int(int_ty) if cpp_like_debuginfo => (int_ty.msvc_basic_name(), DW_ATE_signed),
766 ty::Uint(uint_ty) if cpp_like_debuginfo => (uint_ty.msvc_basic_name(), DW_ATE_unsigned),
767 ty::Float(ty::FloatTy::F16) if cpp_like_debuginfo => {
768 return build_cpp_f16_di_node(cx);
769 }
770 ty::Float(float_ty) if cpp_like_debuginfo => (float_ty.msvc_basic_name(), DW_ATE_float),
771 ty::Int(int_ty) => (int_ty.name_str(), DW_ATE_signed),
772 ty::Uint(uint_ty) => (uint_ty.name_str(), DW_ATE_unsigned),
773 ty::Float(float_ty) => (float_ty.name_str(), DW_ATE_float),
774 _ => ::rustc_middle::util::bug::bug_fmt(format_args!("debuginfo::build_basic_type_di_node - `t` is invalid type"))bug!("debuginfo::build_basic_type_di_node - `t` is invalid type"),
775 };
776
777 let ty_di_node = create_basic_type(cx, name, cx.size_of(t), encoding);
778
779 if !cpp_like_debuginfo {
780 return DINodeCreationResult::new(ty_di_node, false);
781 }
782
783 let typedef_name = match t.kind() {
784 ty::Int(int_ty) => int_ty.name_str(),
785 ty::Uint(uint_ty) => uint_ty.name_str(),
786 ty::Float(float_ty) => float_ty.name_str(),
787 _ => return DINodeCreationResult::new(ty_di_node, false),
788 };
789
790 let typedef_di_node = unsafe {
791 llvm::LLVMDIBuilderCreateTypedef(
792 DIB(cx),
793 ty_di_node,
794 typedef_name.as_ptr(),
795 typedef_name.len(),
796 unknown_file_metadata(cx),
797 0, None, 0u32, )
801 };
802
803 DINodeCreationResult::new(typedef_di_node, false)
804}
805
806fn create_basic_type<'ll, 'tcx>(
807 cx: &CodegenCx<'ll, 'tcx>,
808 name: &str,
809 size: Size,
810 encoding: u32,
811) -> &'ll DIBasicType {
812 unsafe {
813 llvm::LLVMDIBuilderCreateBasicType(
814 DIB(cx),
815 name.as_ptr(),
816 name.len(),
817 size.bits(),
818 encoding,
819 DIFlags::FlagZero,
820 )
821 }
822}
823
824fn build_foreign_type_di_node<'ll, 'tcx>(
825 cx: &CodegenCx<'ll, 'tcx>,
826 t: Ty<'tcx>,
827 unique_type_id: UniqueTypeId<'tcx>,
828) -> DINodeCreationResult<'ll> {
829 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs:829",
"rustc_codegen_llvm::debuginfo::metadata",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs"),
::tracing_core::__macro_support::Option::Some(829u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::debuginfo::metadata"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("build_foreign_type_di_node: {0:?}",
t) as &dyn Value))])
});
} else { ; }
};debug!("build_foreign_type_di_node: {:?}", t);
830
831 let &ty::Foreign(def_id) = unique_type_id.expect_ty().kind() else {
832 ::rustc_middle::util::bug::bug_fmt(format_args!("build_foreign_type_di_node() called with unexpected type: {0:?}",
unique_type_id.expect_ty()));bug!(
833 "build_foreign_type_di_node() called with unexpected type: {:?}",
834 unique_type_id.expect_ty()
835 );
836 };
837
838 build_type_with_children(
839 cx,
840 type_map::stub(
841 cx,
842 Stub::Struct,
843 unique_type_id,
844 &compute_debuginfo_type_name(cx.tcx, t, false),
845 None,
846 cx.size_and_align_of(t),
847 Some(get_namespace_for_item(cx, def_id)),
848 DIFlags::FlagZero,
849 ),
850 |_, _| ::smallvec::SmallVec::new()smallvec![],
851 NO_GENERICS,
852 )
853}
854
855pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
856 tcx: TyCtxt<'tcx>,
857 codegen_unit_name: &str,
858 debug_context: &CodegenUnitDebugContext<'ll, 'tcx>,
859) -> &'ll DIDescriptor {
860 let mut name_in_debuginfo = tcx
861 .sess
862 .local_crate_source_file()
863 .map(|src| src.path(RemapPathScopeComponents::DEBUGINFO).to_path_buf())
864 .unwrap_or_else(|| PathBuf::from(tcx.crate_name(LOCAL_CRATE).as_str()));
865
866 name_in_debuginfo.push("@");
884 name_in_debuginfo.push(codegen_unit_name);
885
886 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs:886",
"rustc_codegen_llvm::debuginfo::metadata",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs"),
::tracing_core::__macro_support::Option::Some(886u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::debuginfo::metadata"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("build_compile_unit_di_node: {0:?}",
name_in_debuginfo) as &dyn Value))])
});
} else { ; }
};debug!("build_compile_unit_di_node: {:?}", name_in_debuginfo);
887 let rustc_producer = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("rustc version {0}",
tcx.sess.cfg_version))
})format!("rustc version {}", tcx.sess.cfg_version);
888 let producer = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("clang LLVM ({0})", rustc_producer))
})format!("clang LLVM ({rustc_producer})");
890
891 let name_in_debuginfo = name_in_debuginfo.to_string_lossy();
892 let work_dir = tcx.sess.psess.source_map().working_dir();
893 let output_filenames = tcx.output_filenames(());
894 let split_name = if tcx.sess.target_can_use_split_dwarf()
895 && let Some(f) = output_filenames.split_dwarf_path(
896 tcx.sess.split_debuginfo(),
897 tcx.sess.opts.unstable_opts.split_dwarf_kind,
898 codegen_unit_name,
899 tcx.sess.invocation_temp.as_deref(),
900 ) {
901 Some(tcx.sess.source_map().path_mapping().to_real_filename(work_dir, f))
903 } else {
904 None
905 };
906 let split_name = split_name
907 .as_ref()
908 .map(|f| f.path(RemapPathScopeComponents::DEBUGINFO).to_string_lossy())
909 .unwrap_or_default();
910 let work_dir = work_dir.path(RemapPathScopeComponents::DEBUGINFO).to_string_lossy();
911 let kind = DebugEmissionKind::from_generic(tcx.sess.opts.debuginfo);
912
913 let dwarf_version = tcx.sess.dwarf_version();
914 let is_dwarf_kind =
915 #[allow(non_exhaustive_omitted_patterns)] match tcx.sess.target.debuginfo_kind
{
DebuginfoKind::Dwarf | DebuginfoKind::DwarfDsym => true,
_ => false,
}matches!(tcx.sess.target.debuginfo_kind, DebuginfoKind::Dwarf | DebuginfoKind::DwarfDsym);
916 let debug_name_table_kind = if is_dwarf_kind && dwarf_version <= 4 {
918 DebugNameTableKind::None
919 } else {
920 DebugNameTableKind::Default
921 };
922
923 unsafe {
924 let compile_unit_file = create_file(
925 debug_context.builder.as_ref(),
926 &name_in_debuginfo,
927 &work_dir,
928 "",
929 llvm::ChecksumKind::None,
930 None,
931 );
932
933 let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit(
934 debug_context.builder.as_ref(),
935 dwarf_const::DW_LANG_Rust,
936 compile_unit_file,
937 producer.as_c_char_ptr(),
938 producer.len(),
939 tcx.sess.opts.optimize != config::OptLevel::No,
940 c"".as_ptr(),
941 0,
942 split_name.as_c_char_ptr(),
946 split_name.len(),
947 kind,
948 0,
949 tcx.sess.opts.unstable_opts.split_dwarf_inlining,
950 debug_name_table_kind,
951 );
952
953 return unit_metadata;
954 };
955}
956
957fn build_field_di_node<'ll, 'tcx>(
959 cx: &CodegenCx<'ll, 'tcx>,
960 owner: &'ll DIScope,
961 name: &str,
962 layout: TyAndLayout<'tcx>,
963 offset: Size,
964 flags: DIFlags,
965 type_di_node: &'ll DIType,
966 def_id: Option<DefId>,
967) -> &'ll DIType {
968 let (file_metadata, line_number) = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers
969 {
970 file_metadata_from_def_id(cx, def_id)
971 } else {
972 (unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
973 };
974 create_member_type(
975 cx,
976 owner,
977 name,
978 file_metadata,
979 line_number,
980 layout,
981 offset,
982 flags,
983 type_di_node,
984 )
985}
986
987fn create_member_type<'ll, 'tcx>(
988 cx: &CodegenCx<'ll, 'tcx>,
989 owner: &'ll DIScope,
990 name: &str,
991 file_metadata: &'ll DIType,
992 line_number: u32,
993 layout: TyAndLayout<'tcx>,
994 offset: Size,
995 flags: DIFlags,
996 type_di_node: &'ll DIType,
997) -> &'ll DIType {
998 unsafe {
999 llvm::LLVMDIBuilderCreateMemberType(
1000 DIB(cx),
1001 owner,
1002 name.as_ptr(),
1003 name.len(),
1004 file_metadata,
1005 line_number,
1006 layout.size.bits(),
1007 layout.align.bits() as u32,
1008 offset.bits(),
1009 flags,
1010 type_di_node,
1011 )
1012 }
1013}
1014
1015fn visibility_di_flags<'ll, 'tcx>(
1021 cx: &CodegenCx<'ll, 'tcx>,
1022 did: DefId,
1023 type_did: DefId,
1024) -> DIFlags {
1025 let parent_did = cx.tcx.parent(type_did);
1026 let visibility = cx.tcx.visibility(did);
1027 match visibility {
1028 Visibility::Public => DIFlags::FlagPublic,
1029 Visibility::Restricted(did) if did == parent_did => DIFlags::FlagPrivate,
1031 Visibility::Restricted(..) => DIFlags::FlagProtected,
1033 }
1034}
1035
1036fn build_struct_type_di_node<'ll, 'tcx>(
1038 cx: &CodegenCx<'ll, 'tcx>,
1039 unique_type_id: UniqueTypeId<'tcx>,
1040 span: Span,
1041) -> DINodeCreationResult<'ll> {
1042 let struct_type = unique_type_id.expect_ty();
1043
1044 let ty::Adt(adt_def, _) = struct_type.kind() else {
1045 ::rustc_middle::util::bug::bug_fmt(format_args!("build_struct_type_di_node() called with non-struct-type: {0:?}",
struct_type));bug!("build_struct_type_di_node() called with non-struct-type: {:?}", struct_type);
1046 };
1047 if !adt_def.is_struct() {
::core::panicking::panic("assertion failed: adt_def.is_struct()")
};assert!(adt_def.is_struct());
1048 let containing_scope = get_namespace_for_item(cx, adt_def.did());
1049 let struct_type_and_layout = cx.spanned_layout_of(struct_type, span);
1050 let variant_def = adt_def.non_enum_variant();
1051 let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1052 Some(file_metadata_from_def_id(cx, Some(adt_def.did())))
1053 } else {
1054 None
1055 };
1056 let name = compute_debuginfo_type_name(cx.tcx, struct_type, false);
1057
1058 if struct_type.is_scalable_vector() {
1059 let parts = struct_type.scalable_vector_parts(cx.tcx).unwrap();
1060 return build_scalable_vector_di_node(
1061 cx,
1062 unique_type_id,
1063 name,
1064 *adt_def,
1065 parts,
1066 struct_type_and_layout.layout,
1067 def_location,
1068 containing_scope,
1069 );
1070 }
1071
1072 type_map::build_type_with_children(
1073 cx,
1074 type_map::stub(
1075 cx,
1076 Stub::Struct,
1077 unique_type_id,
1078 &name,
1079 def_location,
1080 size_and_align_of(struct_type_and_layout),
1081 Some(containing_scope),
1082 visibility_di_flags(cx, adt_def.did(), adt_def.did()),
1083 ),
1084 |cx, owner| {
1086 variant_def
1087 .fields
1088 .iter()
1089 .enumerate()
1090 .map(|(i, f)| {
1091 let field_name = if variant_def.ctor_kind() == Some(CtorKind::Fn) {
1092 tuple_field_name(i)
1094 } else {
1095 Cow::Borrowed(f.name.as_str())
1097 };
1098 let field_layout = struct_type_and_layout.field(cx, i);
1099 let def_id = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1100 Some(f.did)
1101 } else {
1102 None
1103 };
1104 build_field_di_node(
1105 cx,
1106 owner,
1107 &field_name[..],
1108 field_layout,
1109 struct_type_and_layout.fields.offset(i),
1110 visibility_di_flags(cx, f.did, adt_def.did()),
1111 type_di_node(cx, field_layout.ty),
1112 def_id,
1113 )
1114 })
1115 .collect()
1116 },
1117 |cx| build_generic_type_param_di_nodes(cx, struct_type),
1118 )
1119}
1120
1121fn build_scalable_vector_di_node<'ll, 'tcx>(
1139 cx: &CodegenCx<'ll, 'tcx>,
1140 unique_type_id: UniqueTypeId<'tcx>,
1141 name: String,
1142 adt_def: AdtDef<'tcx>,
1143 (element_count, element_ty, number_of_vectors): (u16, Ty<'tcx>, NumScalableVectors),
1144 layout: Layout<'tcx>,
1145 def_location: Option<DefinitionLocation<'ll>>,
1146 containing_scope: &'ll DIScope,
1147) -> DINodeCreationResult<'ll> {
1148 use dwarf_const::{DW_OP_bregx, DW_OP_constu, DW_OP_minus, DW_OP_mul};
1149 if !adt_def.repr().scalable() {
::core::panicking::panic("assertion failed: adt_def.repr().scalable()")
};assert!(adt_def.repr().scalable());
1150 {
match cx.tcx.sess.target.arch {
Arch::AArch64 => {}
ref left_val => {
::core::panicking::assert_matches_failed(left_val,
"Arch::AArch64", ::core::option::Option::None);
}
}
};assert_matches!(cx.tcx.sess.target.arch, Arch::AArch64);
1153
1154 let (file_metadata, line_number) = if let Some(def_location) = def_location {
1155 (def_location.0, def_location.1)
1156 } else {
1157 (unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
1158 };
1159
1160 let (bitstride, element_di_node) = if element_ty.is_bool() {
1161 (Some(llvm::LLVMValueAsMetadata(cx.const_i64(1))), type_di_node(cx, cx.tcx.types.u8))
1162 } else {
1163 (None, type_di_node(cx, element_ty))
1164 };
1165
1166 let number_of_elements: u64 = (element_count as u64) * (number_of_vectors.0 as u64);
1167 let number_of_elements_per_vg = number_of_elements / 2;
1168 let mut expr = smallvec::SmallVec::<[u64; 9]>::new();
1169 expr.push(DW_OP_constu); expr.push(number_of_elements_per_vg);
1172 expr.push(DW_OP_bregx); expr.push(46u64);
1174 expr.push(0u64);
1175 expr.push(DW_OP_mul); expr.push(DW_OP_constu); expr.push(1u64);
1178 expr.push(DW_OP_minus); let di_builder = DIB(cx);
1181 let metadata = unsafe {
1182 let upper = llvm::LLVMDIBuilderCreateExpression(di_builder, expr.as_ptr(), expr.len());
1183 let subrange = llvm::LLVMRustDIGetOrCreateSubrange(
1184 di_builder,
1185 None,
1186 llvm::LLVMValueAsMetadata(cx.const_i64(0)),
1187 upper,
1188 None,
1189 );
1190 let subscripts = create_DIArray(di_builder, &[Some(subrange)]);
1191 let vector_ty = llvm::LLVMRustDICreateVectorType(
1192 di_builder,
1193 0,
1194 layout.align.bits() as u32,
1195 element_di_node,
1196 subscripts,
1197 bitstride,
1198 );
1199 llvm::LLVMDIBuilderCreateTypedef(
1200 di_builder,
1201 vector_ty,
1202 name.as_ptr(),
1203 name.len(),
1204 file_metadata,
1205 line_number,
1206 Some(containing_scope),
1207 layout.align.bits() as u32,
1208 )
1209 };
1210
1211 debug_context(cx).type_map.insert(unique_type_id, metadata);
1212 DINodeCreationResult { di_node: metadata, already_stored_in_typemap: true }
1213}
1214
1215fn build_upvar_field_di_nodes<'ll, 'tcx>(
1222 cx: &CodegenCx<'ll, 'tcx>,
1223 closure_or_coroutine_ty: Ty<'tcx>,
1224 closure_or_coroutine_di_node: &'ll DIType,
1225) -> SmallVec<&'ll DIType> {
1226 let (&def_id, up_var_tys) = match closure_or_coroutine_ty.kind() {
1227 ty::Coroutine(def_id, args) => (def_id, args.as_coroutine().prefix_tys()),
1228 ty::Closure(def_id, args) => (def_id, args.as_closure().upvar_tys()),
1229 ty::CoroutineClosure(def_id, args) => (def_id, args.as_coroutine_closure().upvar_tys()),
1230 _ => {
1231 ::rustc_middle::util::bug::bug_fmt(format_args!("build_upvar_field_di_nodes() called with non-closure-or-coroutine-type: {0:?}",
closure_or_coroutine_ty))bug!(
1232 "build_upvar_field_di_nodes() called with non-closure-or-coroutine-type: {:?}",
1233 closure_or_coroutine_ty
1234 )
1235 }
1236 };
1237
1238 if !up_var_tys.iter().all(|t|
t ==
cx.tcx.normalize_erasing_regions(cx.typing_env(),
Unnormalized::new_wip(t))) {
::core::panicking::panic("assertion failed: up_var_tys.iter().all(|t|\n t ==\n cx.tcx.normalize_erasing_regions(cx.typing_env(),\n Unnormalized::new_wip(t)))")
};assert!(
1239 up_var_tys
1240 .iter()
1241 .all(|t| t
1242 == cx.tcx.normalize_erasing_regions(cx.typing_env(), Unnormalized::new_wip(t)))
1243 );
1244
1245 let capture_names = cx.tcx.closure_saved_names_of_captured_variables(def_id);
1246 let layout = cx.layout_of(closure_or_coroutine_ty);
1247
1248 up_var_tys
1249 .into_iter()
1250 .zip(capture_names.iter())
1251 .enumerate()
1252 .map(|(index, (up_var_ty, capture_name))| {
1253 build_field_di_node(
1254 cx,
1255 closure_or_coroutine_di_node,
1256 capture_name.as_str(),
1257 cx.layout_of(up_var_ty),
1258 layout.fields.offset(index),
1259 DIFlags::FlagZero,
1260 type_di_node(cx, up_var_ty),
1261 None,
1262 )
1263 })
1264 .collect()
1265}
1266
1267fn build_tuple_type_di_node<'ll, 'tcx>(
1269 cx: &CodegenCx<'ll, 'tcx>,
1270 unique_type_id: UniqueTypeId<'tcx>,
1271) -> DINodeCreationResult<'ll> {
1272 let tuple_type = unique_type_id.expect_ty();
1273 let &ty::Tuple(component_types) = tuple_type.kind() else {
1274 ::rustc_middle::util::bug::bug_fmt(format_args!("build_tuple_type_di_node() called with non-tuple-type: {0:?}",
tuple_type))bug!("build_tuple_type_di_node() called with non-tuple-type: {:?}", tuple_type)
1275 };
1276
1277 let tuple_type_and_layout = cx.layout_of(tuple_type);
1278 let type_name = compute_debuginfo_type_name(cx.tcx, tuple_type, false);
1279
1280 type_map::build_type_with_children(
1281 cx,
1282 type_map::stub(
1283 cx,
1284 Stub::Struct,
1285 unique_type_id,
1286 &type_name,
1287 None,
1288 size_and_align_of(tuple_type_and_layout),
1289 NO_SCOPE_METADATA,
1290 DIFlags::FlagZero,
1291 ),
1292 |cx, tuple_di_node| {
1294 component_types
1295 .into_iter()
1296 .enumerate()
1297 .map(|(index, component_type)| {
1298 build_field_di_node(
1299 cx,
1300 tuple_di_node,
1301 &tuple_field_name(index),
1302 cx.layout_of(component_type),
1303 tuple_type_and_layout.fields.offset(index),
1304 DIFlags::FlagZero,
1305 type_di_node(cx, component_type),
1306 None,
1307 )
1308 })
1309 .collect()
1310 },
1311 NO_GENERICS,
1312 )
1313}
1314
1315fn build_closure_env_di_node<'ll, 'tcx>(
1317 cx: &CodegenCx<'ll, 'tcx>,
1318 unique_type_id: UniqueTypeId<'tcx>,
1319) -> DINodeCreationResult<'ll> {
1320 let closure_env_type = unique_type_id.expect_ty();
1321 let &(ty::Closure(def_id, _) | ty::CoroutineClosure(def_id, _)) = closure_env_type.kind()
1322 else {
1323 ::rustc_middle::util::bug::bug_fmt(format_args!("build_closure_env_di_node() called with non-closure-type: {0:?}",
closure_env_type))bug!("build_closure_env_di_node() called with non-closure-type: {:?}", closure_env_type)
1324 };
1325 let containing_scope = get_namespace_for_item(cx, def_id);
1326 let type_name = compute_debuginfo_type_name(cx.tcx, closure_env_type, false);
1327
1328 let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1329 Some(file_metadata_from_def_id(cx, Some(def_id)))
1330 } else {
1331 None
1332 };
1333
1334 type_map::build_type_with_children(
1335 cx,
1336 type_map::stub(
1337 cx,
1338 Stub::Struct,
1339 unique_type_id,
1340 &type_name,
1341 def_location,
1342 cx.size_and_align_of(closure_env_type),
1343 Some(containing_scope),
1344 DIFlags::FlagZero,
1345 ),
1346 |cx, owner| build_upvar_field_di_nodes(cx, closure_env_type, owner),
1348 NO_GENERICS,
1349 )
1350}
1351
1352fn build_union_type_di_node<'ll, 'tcx>(
1354 cx: &CodegenCx<'ll, 'tcx>,
1355 unique_type_id: UniqueTypeId<'tcx>,
1356 span: Span,
1357) -> DINodeCreationResult<'ll> {
1358 let union_type = unique_type_id.expect_ty();
1359 let (union_def_id, variant_def) = match union_type.kind() {
1360 ty::Adt(def, _) => (def.did(), def.non_enum_variant()),
1361 _ => ::rustc_middle::util::bug::bug_fmt(format_args!("build_union_type_di_node on a non-ADT"))bug!("build_union_type_di_node on a non-ADT"),
1362 };
1363 let containing_scope = get_namespace_for_item(cx, union_def_id);
1364 let union_ty_and_layout = cx.spanned_layout_of(union_type, span);
1365 let type_name = compute_debuginfo_type_name(cx.tcx, union_type, false);
1366 let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1367 Some(file_metadata_from_def_id(cx, Some(union_def_id)))
1368 } else {
1369 None
1370 };
1371
1372 type_map::build_type_with_children(
1373 cx,
1374 type_map::stub(
1375 cx,
1376 Stub::Union,
1377 unique_type_id,
1378 &type_name,
1379 def_location,
1380 size_and_align_of(union_ty_and_layout),
1381 Some(containing_scope),
1382 DIFlags::FlagZero,
1383 ),
1384 |cx, owner| {
1386 variant_def
1387 .fields
1388 .iter()
1389 .enumerate()
1390 .map(|(i, f)| {
1391 let field_layout = union_ty_and_layout.field(cx, i);
1392 let def_id = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1393 Some(f.did)
1394 } else {
1395 None
1396 };
1397 build_field_di_node(
1398 cx,
1399 owner,
1400 f.name.as_str(),
1401 field_layout,
1402 Size::ZERO,
1403 DIFlags::FlagZero,
1404 type_di_node(cx, field_layout.ty),
1405 def_id,
1406 )
1407 })
1408 .collect()
1409 },
1410 |cx| build_generic_type_param_di_nodes(cx, union_type),
1412 )
1413}
1414
1415fn build_generic_type_param_di_nodes<'ll, 'tcx>(
1417 cx: &CodegenCx<'ll, 'tcx>,
1418 ty: Ty<'tcx>,
1419) -> SmallVec<Option<&'ll DIType>> {
1420 if let ty::Adt(def, args) = *ty.kind() {
1421 if args.types().next().is_some() {
1422 let generics = cx.tcx.generics_of(def.did());
1423 let names = get_parameter_names(cx, generics);
1424 let template_params: SmallVec<_> = iter::zip(args, names)
1425 .filter_map(|(kind, name)| {
1426 kind.as_type().map(|ty| {
1427 let actual_type = cx
1428 .tcx
1429 .normalize_erasing_regions(cx.typing_env(), Unnormalized::new_wip(ty));
1430 let actual_type_di_node = type_di_node(cx, actual_type);
1431 Some(cx.create_template_type_parameter(name.as_str(), actual_type_di_node))
1432 })
1433 })
1434 .collect();
1435
1436 return template_params;
1437 }
1438 }
1439
1440 return ::smallvec::SmallVec::new()smallvec![];
1441
1442 fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec<Symbol> {
1443 let mut names = generics
1444 .parent
1445 .map_or_else(Vec::new, |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id)));
1446 names.extend(generics.own_params.iter().map(|param| param.name));
1447 names
1448 }
1449}
1450
1451pub(crate) fn build_global_var_di_node<'ll>(
1455 cx: &CodegenCx<'ll, '_>,
1456 def_id: DefId,
1457 global: &'ll Value,
1458) {
1459 if cx.dbg_cx.is_none() {
1460 return;
1461 }
1462
1463 if cx.sess().opts.debuginfo != DebugInfo::Full {
1465 return;
1466 }
1467
1468 let tcx = cx.tcx;
1469
1470 let var_scope = get_namespace_for_item(cx, def_id);
1473 let (file_metadata, line_number) = file_metadata_from_def_id(cx, Some(def_id));
1474
1475 let is_local_to_unit = is_node_local_to_unit(cx, def_id);
1476
1477 let DefKind::Static { nested, .. } = cx.tcx.def_kind(def_id) else { ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!() };
1478 if nested {
1479 return;
1480 }
1481 let variable_type = Instance::mono(cx.tcx, def_id).ty(cx.tcx, cx.typing_env());
1482 let type_di_node = type_di_node(cx, variable_type);
1483 let var_name = tcx.item_name(def_id);
1484 let var_name = var_name.as_str();
1485 let linkage_name = mangled_name_of_instance(cx, Instance::mono(tcx, def_id)).name;
1486 let linkage_name = if var_name == linkage_name { "" } else { linkage_name };
1489
1490 let global_align = cx.align_of(variable_type);
1491
1492 DIB(cx).create_static_variable(
1493 Some(var_scope),
1494 var_name,
1495 linkage_name,
1496 file_metadata,
1497 line_number,
1498 type_di_node,
1499 is_local_to_unit,
1500 global, None, Some(global_align),
1503 );
1504}
1505
1506fn build_vtable_type_di_node<'ll, 'tcx>(
1516 cx: &CodegenCx<'ll, 'tcx>,
1517 ty: Ty<'tcx>,
1518 poly_trait_ref: Option<ty::ExistentialTraitRef<'tcx>>,
1519) -> &'ll DIType {
1520 let tcx = cx.tcx;
1521
1522 let vtable_entries = if let Some(poly_trait_ref) = poly_trait_ref {
1523 let trait_ref = poly_trait_ref.with_self_ty(tcx, ty);
1524 let trait_ref = tcx.erase_and_anonymize_regions(trait_ref);
1525
1526 tcx.vtable_entries(trait_ref)
1527 } else {
1528 TyCtxt::COMMON_VTABLE_ENTRIES
1529 };
1530
1531 let void_pointer_ty = Ty::new_imm_ptr(tcx, tcx.types.unit);
1534 let void_pointer_type_di_node = type_di_node(cx, void_pointer_ty);
1535 let usize_di_node = type_di_node(cx, tcx.types.usize);
1536 let pointer_layout = cx.layout_of(void_pointer_ty);
1537 let pointer_size = pointer_layout.size;
1538 let pointer_align = pointer_layout.align.abi;
1539 match (&cx.size_and_align_of(tcx.types.usize), &(pointer_size, pointer_align))
{
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_eq!(cx.size_and_align_of(tcx.types.usize), (pointer_size, pointer_align));
1543
1544 let vtable_type_name =
1545 compute_debuginfo_vtable_name(cx.tcx, ty, poly_trait_ref, VTableNameKind::Type);
1546 let unique_type_id = UniqueTypeId::for_vtable_ty(tcx, ty, poly_trait_ref);
1547 let size = pointer_size * vtable_entries.len() as u64;
1548
1549 let vtable_holder = type_di_node(cx, ty);
1552
1553 build_type_with_children(
1554 cx,
1555 type_map::stub(
1556 cx,
1557 Stub::VTableTy { vtable_holder },
1558 unique_type_id,
1559 &vtable_type_name,
1560 None,
1561 (size, pointer_align),
1562 NO_SCOPE_METADATA,
1563 DIFlags::FlagArtificial,
1564 ),
1565 |cx, vtable_type_di_node| {
1566 vtable_entries
1567 .iter()
1568 .enumerate()
1569 .filter_map(|(index, vtable_entry)| {
1570 let (field_name, field_type_di_node) = match vtable_entry {
1571 ty::VtblEntry::MetadataDropInPlace => {
1572 ("drop_in_place".to_string(), void_pointer_type_di_node)
1573 }
1574 ty::VtblEntry::Method(_) => {
1575 (::alloc::__export::must_use({
::alloc::fmt::format(format_args!("__method{0}", index))
})format!("__method{index}"), void_pointer_type_di_node)
1579 }
1580 ty::VtblEntry::TraitVPtr(_) => {
1581 (::alloc::__export::must_use({
::alloc::fmt::format(format_args!("__super_trait_ptr{0}", index))
})format!("__super_trait_ptr{index}"), void_pointer_type_di_node)
1582 }
1583 ty::VtblEntry::MetadataAlign => ("align".to_string(), usize_di_node),
1584 ty::VtblEntry::MetadataSize => ("size".to_string(), usize_di_node),
1585 ty::VtblEntry::Vacant => return None,
1586 };
1587
1588 let field_offset = pointer_size * index as u64;
1589
1590 Some(build_field_di_node(
1591 cx,
1592 vtable_type_di_node,
1593 &field_name,
1594 pointer_layout,
1595 field_offset,
1596 DIFlags::FlagZero,
1597 field_type_di_node,
1598 None,
1599 ))
1600 })
1601 .collect()
1602 },
1603 NO_GENERICS,
1604 )
1605 .di_node
1606}
1607
1608fn build_unsafe_binder_type_di_node<'ll, 'tcx>(
1615 cx: &CodegenCx<'ll, 'tcx>,
1616 binder_type: Ty<'tcx>,
1617 unique_type_id: UniqueTypeId<'tcx>,
1618) -> DINodeCreationResult<'ll> {
1619 let ty::UnsafeBinder(inner) = binder_type.kind() else {
1620 ::rustc_middle::util::bug::bug_fmt(format_args!("Only ty::UnsafeBinder is valid for build_unsafe_binder_type_di_node. Found {0:?} instead.",
binder_type))bug!(
1621 "Only ty::UnsafeBinder is valid for build_unsafe_binder_type_di_node. Found {:?} instead.",
1622 binder_type
1623 )
1624 };
1625 let inner_type = inner.skip_binder();
1626 let inner_type_di_node = type_di_node(cx, inner_type);
1627
1628 let type_name = compute_debuginfo_type_name(cx.tcx, binder_type, true);
1629 type_map::build_type_with_children(
1630 cx,
1631 type_map::stub(
1632 cx,
1633 Stub::Struct,
1634 unique_type_id,
1635 &type_name,
1636 None,
1637 cx.size_and_align_of(binder_type),
1638 NO_SCOPE_METADATA,
1639 DIFlags::FlagZero,
1640 ),
1641 |cx, unsafe_binder_type_di_node| {
1642 let inner_layout = cx.layout_of(inner_type);
1643 {
let count = 0usize + 1usize;
let mut vec = ::smallvec::SmallVec::new();
if count <= vec.inline_size() {
vec.push(build_field_di_node(cx, unsafe_binder_type_di_node, "inner",
inner_layout, Size::ZERO, DIFlags::FlagZero,
inner_type_di_node, None));
vec
} else {
::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[build_field_di_node(cx, unsafe_binder_type_di_node,
"inner", inner_layout, Size::ZERO, DIFlags::FlagZero,
inner_type_di_node, None)])))
}
}smallvec![build_field_di_node(
1644 cx,
1645 unsafe_binder_type_di_node,
1646 "inner",
1647 inner_layout,
1648 Size::ZERO,
1649 DIFlags::FlagZero,
1650 inner_type_di_node,
1651 None,
1652 )]
1653 },
1654 NO_GENERICS,
1655 )
1656}
1657
1658fn find_vtable_behind_cast<'ll>(vtable: &'ll Value) -> &'ll Value {
1667 unsafe {
1669 if let Some(c) = llvm::LLVMIsAConstantExpr(vtable) {
1670 if llvm::LLVMGetConstOpcode(c) == llvm::Opcode::AddrSpaceCast {
1671 return llvm::LLVMGetOperand(c, 0).unwrap();
1672 }
1673 }
1674 }
1675 vtable
1676}
1677
1678pub(crate) fn apply_vcall_visibility_metadata<'ll, 'tcx>(
1679 cx: &CodegenCx<'ll, 'tcx>,
1680 ty: Ty<'tcx>,
1681 trait_ref: Option<ExistentialTraitRef<'tcx>>,
1682 vtable: &'ll Value,
1683) {
1684 if !cx.sess().opts.unstable_opts.virtual_function_elimination || cx.sess().lto() != Lto::Fat {
1687 return;
1688 }
1689
1690 enum VCallVisibility {
1691 Public = 0,
1692 LinkageUnit = 1,
1693 TranslationUnit = 2,
1694 }
1695
1696 let Some(trait_ref) = trait_ref else { return };
1697
1698 let vtable = find_vtable_behind_cast(vtable);
1700 let trait_ref_self = trait_ref.with_self_ty(cx.tcx, ty);
1701 let trait_def_id = trait_ref_self.def_id;
1702 let trait_vis = cx.tcx.visibility(trait_def_id);
1703
1704 let cgus = cx.sess().codegen_units().as_usize();
1705 let single_cgu = cgus == 1;
1706
1707 let lto = cx.sess().lto();
1708
1709 let vcall_visibility = match (lto, trait_vis, single_cgu) {
1712 (Lto::No | Lto::ThinLocal, Visibility::Public, _)
1715 | (Lto::No, Visibility::Restricted(_), false) => VCallVisibility::Public,
1716 (Lto::Fat | Lto::Thin, Visibility::Public, _)
1721 | (Lto::ThinLocal | Lto::Thin | Lto::Fat, Visibility::Restricted(_), false) => {
1722 VCallVisibility::LinkageUnit
1723 }
1724 (_, Visibility::Restricted(_), true) => VCallVisibility::TranslationUnit,
1727 };
1728
1729 let trait_ref_typeid = typeid_for_trait_ref(cx.tcx, trait_ref);
1730 let typeid = cx.create_metadata(trait_ref_typeid.as_bytes());
1731
1732 let type_ = [llvm::LLVMValueAsMetadata(cx.const_usize(0)), typeid];
1733 cx.global_add_metadata_node(vtable, llvm::MD_type, &type_);
1734
1735 let vcall_visibility = [llvm::LLVMValueAsMetadata(cx.const_u64(vcall_visibility as u64))];
1736 cx.global_set_metadata_node(vtable, llvm::MD_vcall_visibility, &vcall_visibility);
1737}
1738
1739pub(crate) fn create_vtable_di_node<'ll, 'tcx>(
1744 cx: &CodegenCx<'ll, 'tcx>,
1745 ty: Ty<'tcx>,
1746 poly_trait_ref: Option<ty::ExistentialTraitRef<'tcx>>,
1747 vtable: &'ll Value,
1748) {
1749 if cx.dbg_cx.is_none() {
1750 return;
1751 }
1752
1753 if cx.sess().opts.debuginfo != DebugInfo::Full {
1755 return;
1756 }
1757
1758 let vtable = find_vtable_behind_cast(vtable);
1760
1761 llvm::set_unnamed_address(vtable, llvm::UnnamedAddr::No);
1765
1766 let vtable_name =
1767 compute_debuginfo_vtable_name(cx.tcx, ty, poly_trait_ref, VTableNameKind::GlobalVariable);
1768 let vtable_type_di_node = build_vtable_type_di_node(cx, ty, poly_trait_ref);
1769
1770 DIB(cx).create_static_variable(
1771 NO_SCOPE_METADATA,
1772 &vtable_name,
1773 "", unknown_file_metadata(cx),
1775 UNKNOWN_LINE_NUMBER,
1776 vtable_type_di_node,
1777 true, vtable, None, None::<Align>,
1781 );
1782}
1783
1784pub(crate) fn extend_scope_to_file<'ll>(
1786 cx: &CodegenCx<'ll, '_>,
1787 scope_metadata: &'ll DIScope,
1788 file: &SourceFile,
1789) -> &'ll DILexicalBlock {
1790 let file_metadata = file_metadata(cx, file);
1791 unsafe {
1792 llvm::LLVMDIBuilderCreateLexicalBlockFile(
1793 DIB(cx),
1794 scope_metadata,
1795 file_metadata,
1796 0u32,
1797 )
1798 }
1799}
1800
1801fn tuple_field_name(field_index: usize) -> Cow<'static, str> {
1802 const TUPLE_FIELD_NAMES: [&'static str; 16] = [
1803 "__0", "__1", "__2", "__3", "__4", "__5", "__6", "__7", "__8", "__9", "__10", "__11",
1804 "__12", "__13", "__14", "__15",
1805 ];
1806 TUPLE_FIELD_NAMES
1807 .get(field_index)
1808 .map(|s| Cow::from(*s))
1809 .unwrap_or_else(|| Cow::from(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("__{0}", field_index))
})format!("__{field_index}")))
1810}
1811
1812pub(crate) type DefinitionLocation<'ll> = (&'ll DIFile, c_uint);
1813
1814pub(crate) fn file_metadata_from_def_id<'ll>(
1815 cx: &CodegenCx<'ll, '_>,
1816 def_id: Option<DefId>,
1817) -> DefinitionLocation<'ll> {
1818 if let Some(def_id) = def_id
1819 && let span = hygiene::walk_chain_collapsed(cx.tcx.def_span(def_id), DUMMY_SP)
1820 && !span.is_dummy()
1821 {
1822 let loc = cx.lookup_debug_loc(span.lo());
1823 (file_metadata(cx, &loc.file), loc.line)
1824 } else {
1825 (unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
1826 }
1827}