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