1use std::borrow::Cow;
2use std::fmt::{self, Write};
3use std::hash::{Hash, Hasher};
4use std::path::{Path, PathBuf};
5use std::sync::Arc;
6use std::{iter, ptr};
7
8use libc::{c_longlong, c_uint};
9use rustc_abi::{Align, 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, AdtKind, CoroutineArgsExt, ExistentialTraitRef, Instance, Ty, TyCtxt, Visibility,
20};
21use rustc_session::config::{self, DebugInfo, Lto};
22use rustc_span::{
23 DUMMY_SP, FileName, FileNameDisplayPreference, SourceFile, Span, Symbol, hygiene,
24};
25use rustc_symbol_mangling::typeid_for_trait_ref;
26use rustc_target::spec::DebuginfoKind;
27use smallvec::smallvec;
28use tracing::{debug, instrument};
29
30pub(crate) use self::type_map::TypeMap;
31use self::type_map::{DINodeCreationResult, Stub, UniqueTypeId};
32use super::CodegenUnitDebugContext;
33use super::namespace::mangled_name_of_instance;
34use super::type_names::{compute_debuginfo_type_name, compute_debuginfo_vtable_name};
35use super::utils::{DIB, debug_context, get_namespace_for_item, is_node_local_to_unit};
36use crate::common::{AsCCharPtr, CodegenCx};
37use crate::debuginfo::dwarf_const;
38use crate::debuginfo::metadata::type_map::build_type_with_children;
39use crate::debuginfo::utils::{WidePtrKind, wide_pointer_kind};
40use crate::llvm;
41use crate::llvm::debuginfo::{
42 DIBasicType, DIBuilder, DICompositeType, DIDescriptor, DIFile, DIFlags, DILexicalBlock,
43 DIScope, DIType, DebugEmissionKind, DebugNameTableKind,
44};
45use crate::value::Value;
46
47impl PartialEq for llvm::Metadata {
48 fn eq(&self, other: &Self) -> bool {
49 ptr::eq(self, other)
50 }
51}
52
53impl Eq for llvm::Metadata {}
54
55impl Hash for llvm::Metadata {
56 fn hash<H: Hasher>(&self, hasher: &mut H) {
57 (self as *const Self).hash(hasher);
58 }
59}
60
61impl fmt::Debug for llvm::Metadata {
62 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63 (self as *const Self).fmt(f)
64 }
65}
66
67pub(super) const UNKNOWN_LINE_NUMBER: c_uint = 0;
68pub(super) const UNKNOWN_COLUMN_NUMBER: c_uint = 0;
69
70const NO_SCOPE_METADATA: Option<&DIScope> = None;
71const NO_GENERICS: for<'ll> fn(&CodegenCx<'ll, '_>) -> SmallVec<Option<&'ll DIType>> =
73 |_| SmallVec::new();
74
75type SmallVec<T> = smallvec::SmallVec<[T; 16]>;
78
79mod enums;
80mod type_map;
81
82macro_rules! return_if_di_node_created_in_meantime {
85 ($cx: expr, $unique_type_id: expr) => {
86 if let Some(di_node) = debug_context($cx).type_map.di_node_for_unique_id($unique_type_id) {
87 return DINodeCreationResult::new(di_node, true);
88 }
89 };
90}
91
92#[inline]
94fn size_and_align_of(ty_and_layout: TyAndLayout<'_>) -> (Size, Align) {
95 (ty_and_layout.size, ty_and_layout.align.abi)
96}
97
98fn build_fixed_size_array_di_node<'ll, 'tcx>(
101 cx: &CodegenCx<'ll, 'tcx>,
102 unique_type_id: UniqueTypeId<'tcx>,
103 array_type: Ty<'tcx>,
104 span: Span,
105) -> DINodeCreationResult<'ll> {
106 let ty::Array(element_type, len) = array_type.kind() else {
107 bug!("build_fixed_size_array_di_node() called with non-ty::Array type `{:?}`", array_type)
108 };
109
110 let element_type_di_node = spanned_type_di_node(cx, *element_type, span);
111
112 return_if_di_node_created_in_meantime!(cx, unique_type_id);
113
114 let (size, align) = cx.spanned_size_and_align_of(array_type, span);
115
116 let upper_bound = len
117 .try_to_target_usize(cx.tcx)
118 .expect("expected monomorphic const in codegen") as c_longlong;
119
120 let subrange = unsafe { llvm::LLVMRustDIBuilderGetOrCreateSubrange(DIB(cx), 0, upper_bound) };
121 let subscripts = &[subrange];
122
123 let di_node = unsafe {
124 llvm::LLVMDIBuilderCreateArrayType(
125 DIB(cx),
126 size.bits(),
127 align.bits() as u32,
128 element_type_di_node,
129 subscripts.as_ptr(),
130 subscripts.len() as c_uint,
131 )
132 };
133
134 DINodeCreationResult::new(di_node, false)
135}
136
137fn build_pointer_or_reference_di_node<'ll, 'tcx>(
146 cx: &CodegenCx<'ll, 'tcx>,
147 ptr_type: Ty<'tcx>,
148 pointee_type: Ty<'tcx>,
149 unique_type_id: UniqueTypeId<'tcx>,
150) -> DINodeCreationResult<'ll> {
151 assert_eq!(
154 cx.size_and_align_of(ptr_type),
155 cx.size_and_align_of(Ty::new_mut_ptr(cx.tcx, pointee_type))
156 );
157
158 let pointee_type_di_node = type_di_node(cx, pointee_type);
159
160 return_if_di_node_created_in_meantime!(cx, unique_type_id);
161
162 let data_layout = &cx.tcx.data_layout;
163 let pointer_size = data_layout.pointer_size();
164 let pointer_align = data_layout.pointer_align();
165 let ptr_type_debuginfo_name = compute_debuginfo_type_name(cx.tcx, ptr_type, true);
166
167 match wide_pointer_kind(cx, pointee_type) {
168 None => {
169 assert_eq!(
171 (pointer_size, pointer_align.abi),
172 cx.size_and_align_of(ptr_type),
173 "ptr_type={ptr_type}, pointee_type={pointee_type}",
174 );
175
176 let di_node = create_pointer_type(
177 cx,
178 pointee_type_di_node,
179 pointer_size,
180 pointer_align.abi,
181 &ptr_type_debuginfo_name,
182 );
183
184 DINodeCreationResult { di_node, already_stored_in_typemap: false }
185 }
186 Some(wide_pointer_kind) => {
187 type_map::build_type_with_children(
188 cx,
189 type_map::stub(
190 cx,
191 Stub::Struct,
192 unique_type_id,
193 &ptr_type_debuginfo_name,
194 None,
195 cx.size_and_align_of(ptr_type),
196 NO_SCOPE_METADATA,
197 DIFlags::FlagZero,
198 ),
199 |cx, owner| {
200 let layout_type = if ptr_type.is_box() {
208 Ty::new_mut_ptr(cx.tcx, pointee_type)
212 } else {
213 ptr_type
214 };
215
216 let layout = cx.layout_of(layout_type);
217 let addr_field = layout.field(cx, WIDE_PTR_ADDR);
218 let extra_field = layout.field(cx, WIDE_PTR_EXTRA);
219
220 let (addr_field_name, extra_field_name) = match wide_pointer_kind {
221 WidePtrKind::Dyn => ("pointer", "vtable"),
222 WidePtrKind::Slice => ("data_ptr", "length"),
223 };
224
225 assert_eq!(WIDE_PTR_ADDR, 0);
226 assert_eq!(WIDE_PTR_EXTRA, 1);
227
228 let data_ptr_type_di_node = create_pointer_type(
231 cx,
232 pointee_type_di_node,
233 addr_field.size,
234 addr_field.align.abi,
235 "",
236 );
237
238 smallvec![
239 build_field_di_node(
240 cx,
241 owner,
242 addr_field_name,
243 addr_field,
244 layout.fields.offset(WIDE_PTR_ADDR),
245 DIFlags::FlagZero,
246 data_ptr_type_di_node,
247 None,
248 ),
249 build_field_di_node(
250 cx,
251 owner,
252 extra_field_name,
253 extra_field,
254 layout.fields.offset(WIDE_PTR_EXTRA),
255 DIFlags::FlagZero,
256 type_di_node(cx, extra_field.ty),
257 None,
258 ),
259 ]
260 },
261 NO_GENERICS,
262 )
263 }
264 }
265}
266
267fn build_subroutine_type_di_node<'ll, 'tcx>(
268 cx: &CodegenCx<'ll, 'tcx>,
269 unique_type_id: UniqueTypeId<'tcx>,
270) -> DINodeCreationResult<'ll> {
271 debug_context(cx)
284 .type_map
285 .unique_id_to_di_node
286 .borrow_mut()
287 .insert(unique_type_id, recursion_marker_type_di_node(cx));
288
289 let fn_ty = unique_type_id.expect_ty();
290 let signature =
291 cx.tcx.normalize_erasing_late_bound_regions(cx.typing_env(), fn_ty.fn_sig(cx.tcx));
292
293 let signature_di_nodes: SmallVec<_> = iter::once(
294 match signature.output().kind() {
296 ty::Tuple(tys) if tys.is_empty() => {
297 None
299 }
300 _ => Some(type_di_node(cx, signature.output())),
301 },
302 )
303 .chain(
304 signature.inputs().iter().map(|&argument_type| Some(type_di_node(cx, argument_type))),
306 )
307 .collect();
308
309 debug_context(cx).type_map.unique_id_to_di_node.borrow_mut().remove(&unique_type_id);
310
311 let fn_di_node = create_subroutine_type(cx, &signature_di_nodes[..]);
312
313 let name = compute_debuginfo_type_name(cx.tcx, fn_ty, false);
315 let (size, align) = match fn_ty.kind() {
316 ty::FnDef(..) => (Size::ZERO, Align::ONE),
317 ty::FnPtr(..) => {
318 (cx.tcx.data_layout.pointer_size(), cx.tcx.data_layout.pointer_align().abi)
319 }
320 _ => unreachable!(),
321 };
322 let di_node = create_pointer_type(cx, fn_di_node, size, align, &name);
323
324 DINodeCreationResult::new(di_node, false)
325}
326
327pub(super) fn create_subroutine_type<'ll>(
328 cx: &CodegenCx<'ll, '_>,
329 signature: &[Option<&'ll llvm::Metadata>],
330) -> &'ll DICompositeType {
331 unsafe {
332 llvm::LLVMDIBuilderCreateSubroutineType(
333 DIB(cx),
334 None, signature.as_ptr(),
336 signature.len() as c_uint,
337 DIFlags::FlagZero, )
339 }
340}
341
342fn create_pointer_type<'ll>(
343 cx: &CodegenCx<'ll, '_>,
344 pointee_ty: &'ll llvm::Metadata,
345 size: Size,
346 align: Align,
347 name: &str,
348) -> &'ll llvm::Metadata {
349 unsafe {
350 llvm::LLVMDIBuilderCreatePointerType(
351 DIB(cx),
352 pointee_ty,
353 size.bits(),
354 align.bits() as u32,
355 0, name.as_ptr(),
357 name.len(),
358 )
359 }
360}
361
362fn build_dyn_type_di_node<'ll, 'tcx>(
365 cx: &CodegenCx<'ll, 'tcx>,
366 dyn_type: Ty<'tcx>,
367 unique_type_id: UniqueTypeId<'tcx>,
368) -> DINodeCreationResult<'ll> {
369 if let ty::Dynamic(..) = dyn_type.kind() {
370 let type_name = compute_debuginfo_type_name(cx.tcx, dyn_type, true);
371 type_map::build_type_with_children(
372 cx,
373 type_map::stub(
374 cx,
375 Stub::Struct,
376 unique_type_id,
377 &type_name,
378 None,
379 cx.size_and_align_of(dyn_type),
380 NO_SCOPE_METADATA,
381 DIFlags::FlagZero,
382 ),
383 |_, _| smallvec![],
384 NO_GENERICS,
385 )
386 } else {
387 bug!(
388 "Only ty::Dynamic is valid for build_dyn_type_di_node(). Found {:?} instead.",
389 dyn_type
390 )
391 }
392}
393
394fn build_slice_type_di_node<'ll, 'tcx>(
412 cx: &CodegenCx<'ll, 'tcx>,
413 slice_type: Ty<'tcx>,
414 unique_type_id: UniqueTypeId<'tcx>,
415) -> DINodeCreationResult<'ll> {
416 let element_type = match slice_type.kind() {
417 ty::Slice(element_type) => *element_type,
418 ty::Str => cx.tcx.types.u8,
419 _ => {
420 bug!(
421 "Only ty::Slice is valid for build_slice_type_di_node(). Found {:?} instead.",
422 slice_type
423 )
424 }
425 };
426
427 let element_type_di_node = type_di_node(cx, element_type);
428 return_if_di_node_created_in_meantime!(cx, unique_type_id);
429 DINodeCreationResult { di_node: element_type_di_node, already_stored_in_typemap: false }
430}
431
432pub(crate) fn type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) -> &'ll DIType {
437 spanned_type_di_node(cx, t, DUMMY_SP)
438}
439
440pub(crate) fn spanned_type_di_node<'ll, 'tcx>(
441 cx: &CodegenCx<'ll, 'tcx>,
442 t: Ty<'tcx>,
443 span: Span,
444) -> &'ll DIType {
445 let unique_type_id = UniqueTypeId::for_ty(cx.tcx, t);
446
447 if let Some(existing_di_node) = debug_context(cx).type_map.di_node_for_unique_id(unique_type_id)
448 {
449 return existing_di_node;
450 }
451
452 debug!("type_di_node: {:?} kind: {:?}", t, t.kind());
453
454 let DINodeCreationResult { di_node, already_stored_in_typemap } = match *t.kind() {
455 ty::Never | ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) => {
456 build_basic_type_di_node(cx, t)
457 }
458 ty::Tuple(elements) if elements.is_empty() => build_basic_type_di_node(cx, t),
459 ty::Array(..) => build_fixed_size_array_di_node(cx, unique_type_id, t, span),
460 ty::Slice(_) | ty::Str => build_slice_type_di_node(cx, t, unique_type_id),
461 ty::Dynamic(..) => build_dyn_type_di_node(cx, t, unique_type_id),
462 ty::Foreign(..) => build_foreign_type_di_node(cx, t, unique_type_id),
463 ty::RawPtr(pointee_type, _) | ty::Ref(_, pointee_type, _) => {
464 build_pointer_or_reference_di_node(cx, t, pointee_type, unique_type_id)
465 }
466 ty::Adt(def, args)
470 if def.is_box()
471 && args.get(1).is_none_or(|arg| cx.layout_of(arg.expect_ty()).is_1zst()) =>
472 {
473 build_pointer_or_reference_di_node(cx, t, t.expect_boxed_ty(), unique_type_id)
474 }
475 ty::FnDef(..) | ty::FnPtr(..) => build_subroutine_type_di_node(cx, unique_type_id),
476 ty::Closure(..) => build_closure_env_di_node(cx, unique_type_id),
477 ty::CoroutineClosure(..) => build_closure_env_di_node(cx, unique_type_id),
478 ty::Coroutine(..) => enums::build_coroutine_di_node(cx, unique_type_id),
479 ty::Adt(def, ..) => match def.adt_kind() {
480 AdtKind::Struct => build_struct_type_di_node(cx, unique_type_id),
481 AdtKind::Union => build_union_type_di_node(cx, unique_type_id),
482 AdtKind::Enum => enums::build_enum_type_di_node(cx, unique_type_id, span),
483 },
484 ty::Tuple(_) => build_tuple_type_di_node(cx, unique_type_id),
485 _ => bug!("debuginfo: unexpected type in type_di_node(): {:?}", t),
486 };
487
488 {
489 if already_stored_in_typemap {
490 let di_node_for_uid =
492 match debug_context(cx).type_map.di_node_for_unique_id(unique_type_id) {
493 Some(di_node) => di_node,
494 None => {
495 bug!(
496 "expected type debuginfo node for unique \
497 type ID '{:?}' to already be in \
498 the `debuginfo::TypeMap` but it \
499 was not.",
500 unique_type_id,
501 );
502 }
503 };
504
505 assert_eq!(di_node_for_uid as *const _, di_node as *const _);
506 } else {
507 debug_context(cx).type_map.insert(unique_type_id, di_node);
508 }
509 }
510
511 di_node
512}
513
514fn recursion_marker_type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) -> &'ll DIType {
516 *debug_context(cx).recursion_marker_type.get_or_init(move || {
517 create_basic_type(
528 cx,
529 "<recur_type>",
530 cx.tcx.data_layout.pointer_size(),
531 dwarf_const::DW_ATE_unsigned,
532 )
533 })
534}
535
536fn hex_encode(data: &[u8]) -> String {
537 let mut hex_string = String::with_capacity(data.len() * 2);
538 for byte in data.iter() {
539 write!(&mut hex_string, "{byte:02x}").unwrap();
540 }
541 hex_string
542}
543
544pub(crate) fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) -> &'ll DIFile {
545 let cache_key = Some((source_file.stable_id, source_file.src_hash));
546 return debug_context(cx)
547 .created_files
548 .borrow_mut()
549 .entry(cache_key)
550 .or_insert_with(|| alloc_new_file_metadata(cx, source_file));
551
552 #[instrument(skip(cx, source_file), level = "debug")]
553 fn alloc_new_file_metadata<'ll>(
554 cx: &CodegenCx<'ll, '_>,
555 source_file: &SourceFile,
556 ) -> &'ll DIFile {
557 debug!(?source_file.name);
558
559 let filename_display_preference =
560 cx.sess().filename_display_preference(RemapPathScopeComponents::DEBUGINFO);
561
562 use rustc_session::config::RemapPathScopeComponents;
563 let (directory, file_name) = match &source_file.name {
564 FileName::Real(filename) => {
565 let working_directory = &cx.sess().opts.working_dir;
566 debug!(?working_directory);
567
568 if filename_display_preference == FileNameDisplayPreference::Remapped {
569 let filename = cx
570 .sess()
571 .source_map()
572 .path_mapping()
573 .to_embeddable_absolute_path(filename.clone(), working_directory);
574
575 let abs_path = filename.remapped_path_if_available();
577 debug!(?abs_path);
578
579 if let Ok(rel_path) =
580 abs_path.strip_prefix(working_directory.remapped_path_if_available())
581 {
582 (
598 working_directory.to_string_lossy(FileNameDisplayPreference::Remapped),
599 rel_path.to_string_lossy().into_owned(),
600 )
601 } else {
602 ("".into(), abs_path.to_string_lossy().into_owned())
603 }
604 } else {
605 let working_directory = working_directory.local_path_if_available();
606 let filename = filename.local_path_if_available();
607
608 debug!(?working_directory, ?filename);
609
610 let abs_path: Cow<'_, Path> = if filename.is_absolute() {
611 filename.into()
612 } else {
613 let mut p = PathBuf::new();
614 p.push(working_directory);
615 p.push(filename);
616 p.into()
617 };
618
619 if let Ok(rel_path) = abs_path.strip_prefix(working_directory) {
620 (
621 working_directory.to_string_lossy(),
622 rel_path.to_string_lossy().into_owned(),
623 )
624 } else {
625 ("".into(), abs_path.to_string_lossy().into_owned())
626 }
627 }
628 }
629 other => {
630 debug!(?other);
631 ("".into(), other.display(filename_display_preference).to_string())
632 }
633 };
634
635 let hash_kind = match source_file.src_hash.kind {
636 rustc_span::SourceFileHashAlgorithm::Md5 => llvm::ChecksumKind::MD5,
637 rustc_span::SourceFileHashAlgorithm::Sha1 => llvm::ChecksumKind::SHA1,
638 rustc_span::SourceFileHashAlgorithm::Sha256 => llvm::ChecksumKind::SHA256,
639 rustc_span::SourceFileHashAlgorithm::Blake3 => llvm::ChecksumKind::None,
640 };
641 let hash_value = hex_encode(source_file.src_hash.hash_bytes());
642
643 let source =
644 cx.sess().opts.unstable_opts.embed_source.then_some(()).and(source_file.src.as_ref());
645
646 create_file(DIB(cx), &file_name, &directory, &hash_value, hash_kind, source)
647 }
648}
649
650fn unknown_file_metadata<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll DIFile {
651 debug_context(cx).created_files.borrow_mut().entry(None).or_insert_with(|| {
652 create_file(DIB(cx), "<unknown>", "", "", llvm::ChecksumKind::None, None)
653 })
654}
655
656fn create_file<'ll>(
657 builder: &DIBuilder<'ll>,
658 file_name: &str,
659 directory: &str,
660 hash_value: &str,
661 hash_kind: llvm::ChecksumKind,
662 source: Option<&Arc<String>>,
663) -> &'ll DIFile {
664 unsafe {
665 llvm::LLVMRustDIBuilderCreateFile(
666 builder,
667 file_name.as_c_char_ptr(),
668 file_name.len(),
669 directory.as_c_char_ptr(),
670 directory.len(),
671 hash_kind,
672 hash_value.as_c_char_ptr(),
673 hash_value.len(),
674 source.map_or(ptr::null(), |x| x.as_c_char_ptr()),
675 source.map_or(0, |x| x.len()),
676 )
677 }
678}
679
680trait MsvcBasicName {
681 fn msvc_basic_name(self) -> &'static str;
682}
683
684impl MsvcBasicName for ty::IntTy {
685 fn msvc_basic_name(self) -> &'static str {
686 match self {
687 ty::IntTy::Isize => "ptrdiff_t",
688 ty::IntTy::I8 => "__int8",
689 ty::IntTy::I16 => "__int16",
690 ty::IntTy::I32 => "__int32",
691 ty::IntTy::I64 => "__int64",
692 ty::IntTy::I128 => "__int128",
693 }
694 }
695}
696
697impl MsvcBasicName for ty::UintTy {
698 fn msvc_basic_name(self) -> &'static str {
699 match self {
700 ty::UintTy::Usize => "size_t",
701 ty::UintTy::U8 => "unsigned __int8",
702 ty::UintTy::U16 => "unsigned __int16",
703 ty::UintTy::U32 => "unsigned __int32",
704 ty::UintTy::U64 => "unsigned __int64",
705 ty::UintTy::U128 => "unsigned __int128",
706 }
707 }
708}
709
710impl MsvcBasicName for ty::FloatTy {
711 fn msvc_basic_name(self) -> &'static str {
712 match self {
715 ty::FloatTy::F16 => {
716 bug!("`f16` should have been handled in `build_basic_type_di_node`")
717 }
718 ty::FloatTy::F32 => "float",
719 ty::FloatTy::F64 => "double",
720 ty::FloatTy::F128 => "fp128",
721 }
722 }
723}
724
725fn build_cpp_f16_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) -> DINodeCreationResult<'ll> {
726 let float_ty = cx.tcx.types.f16;
729 let bits_ty = cx.tcx.types.u16;
730 let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
731 match float_ty.kind() {
732 ty::Adt(def, _) => Some(file_metadata_from_def_id(cx, Some(def.did()))),
733 _ => None,
734 }
735 } else {
736 None
737 };
738 type_map::build_type_with_children(
739 cx,
740 type_map::stub(
741 cx,
742 Stub::Struct,
743 UniqueTypeId::for_ty(cx.tcx, float_ty),
744 "f16",
745 def_location,
746 cx.size_and_align_of(float_ty),
747 NO_SCOPE_METADATA,
748 DIFlags::FlagZero,
749 ),
750 |cx, float_di_node| {
752 let def_id = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
753 match bits_ty.kind() {
754 ty::Adt(def, _) => Some(def.did()),
755 _ => None,
756 }
757 } else {
758 None
759 };
760 smallvec![build_field_di_node(
761 cx,
762 float_di_node,
763 "bits",
764 cx.layout_of(bits_ty),
765 Size::ZERO,
766 DIFlags::FlagZero,
767 type_di_node(cx, bits_ty),
768 def_id,
769 )]
770 },
771 NO_GENERICS,
772 )
773}
774
775fn build_basic_type_di_node<'ll, 'tcx>(
776 cx: &CodegenCx<'ll, 'tcx>,
777 t: Ty<'tcx>,
778) -> DINodeCreationResult<'ll> {
779 debug!("build_basic_type_di_node: {:?}", t);
780
781 let cpp_like_debuginfo = cpp_like_debuginfo(cx.tcx);
784
785 use dwarf_const::{DW_ATE_UTF, DW_ATE_boolean, DW_ATE_float, DW_ATE_signed, DW_ATE_unsigned};
786
787 let (name, encoding) = match t.kind() {
788 ty::Never => ("!", DW_ATE_unsigned),
789 ty::Tuple(elements) if elements.is_empty() => {
790 if cpp_like_debuginfo {
791 return build_tuple_type_di_node(cx, UniqueTypeId::for_ty(cx.tcx, t));
792 } else {
793 ("()", DW_ATE_unsigned)
794 }
795 }
796 ty::Bool => ("bool", DW_ATE_boolean),
797 ty::Char => ("char", DW_ATE_UTF),
798 ty::Int(int_ty) if cpp_like_debuginfo => (int_ty.msvc_basic_name(), DW_ATE_signed),
799 ty::Uint(uint_ty) if cpp_like_debuginfo => (uint_ty.msvc_basic_name(), DW_ATE_unsigned),
800 ty::Float(ty::FloatTy::F16) if cpp_like_debuginfo => {
801 return build_cpp_f16_di_node(cx);
802 }
803 ty::Float(float_ty) if cpp_like_debuginfo => (float_ty.msvc_basic_name(), DW_ATE_float),
804 ty::Int(int_ty) => (int_ty.name_str(), DW_ATE_signed),
805 ty::Uint(uint_ty) => (uint_ty.name_str(), DW_ATE_unsigned),
806 ty::Float(float_ty) => (float_ty.name_str(), DW_ATE_float),
807 _ => bug!("debuginfo::build_basic_type_di_node - `t` is invalid type"),
808 };
809
810 let ty_di_node = create_basic_type(cx, name, cx.size_of(t), encoding);
811
812 if !cpp_like_debuginfo {
813 return DINodeCreationResult::new(ty_di_node, false);
814 }
815
816 let typedef_name = match t.kind() {
817 ty::Int(int_ty) => int_ty.name_str(),
818 ty::Uint(uint_ty) => uint_ty.name_str(),
819 ty::Float(float_ty) => float_ty.name_str(),
820 _ => return DINodeCreationResult::new(ty_di_node, false),
821 };
822
823 let typedef_di_node = unsafe {
824 llvm::LLVMDIBuilderCreateTypedef(
825 DIB(cx),
826 ty_di_node,
827 typedef_name.as_ptr(),
828 typedef_name.len(),
829 unknown_file_metadata(cx),
830 0, None, 0u32, )
834 };
835
836 DINodeCreationResult::new(typedef_di_node, false)
837}
838
839fn create_basic_type<'ll, 'tcx>(
840 cx: &CodegenCx<'ll, 'tcx>,
841 name: &str,
842 size: Size,
843 encoding: u32,
844) -> &'ll DIBasicType {
845 unsafe {
846 llvm::LLVMDIBuilderCreateBasicType(
847 DIB(cx),
848 name.as_ptr(),
849 name.len(),
850 size.bits(),
851 encoding,
852 DIFlags::FlagZero,
853 )
854 }
855}
856
857fn build_foreign_type_di_node<'ll, 'tcx>(
858 cx: &CodegenCx<'ll, 'tcx>,
859 t: Ty<'tcx>,
860 unique_type_id: UniqueTypeId<'tcx>,
861) -> DINodeCreationResult<'ll> {
862 debug!("build_foreign_type_di_node: {:?}", t);
863
864 let &ty::Foreign(def_id) = unique_type_id.expect_ty().kind() else {
865 bug!(
866 "build_foreign_type_di_node() called with unexpected type: {:?}",
867 unique_type_id.expect_ty()
868 );
869 };
870
871 build_type_with_children(
872 cx,
873 type_map::stub(
874 cx,
875 Stub::Struct,
876 unique_type_id,
877 &compute_debuginfo_type_name(cx.tcx, t, false),
878 None,
879 cx.size_and_align_of(t),
880 Some(get_namespace_for_item(cx, def_id)),
881 DIFlags::FlagZero,
882 ),
883 |_, _| smallvec![],
884 NO_GENERICS,
885 )
886}
887
888pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
889 tcx: TyCtxt<'tcx>,
890 codegen_unit_name: &str,
891 debug_context: &CodegenUnitDebugContext<'ll, 'tcx>,
892) -> &'ll DIDescriptor {
893 use rustc_session::RemapFileNameExt;
894 use rustc_session::config::RemapPathScopeComponents;
895 let mut name_in_debuginfo = tcx
896 .sess
897 .local_crate_source_file()
898 .map(|src| src.for_scope(&tcx.sess, RemapPathScopeComponents::DEBUGINFO).to_path_buf())
899 .unwrap_or_else(|| PathBuf::from(tcx.crate_name(LOCAL_CRATE).as_str()));
900
901 name_in_debuginfo.push("@");
919 name_in_debuginfo.push(codegen_unit_name);
920
921 debug!("build_compile_unit_di_node: {:?}", name_in_debuginfo);
922 let rustc_producer = format!("rustc version {}", tcx.sess.cfg_version);
923 let producer = format!("clang LLVM ({rustc_producer})");
925
926 let name_in_debuginfo = name_in_debuginfo.to_string_lossy();
927 let work_dir = tcx
928 .sess
929 .opts
930 .working_dir
931 .for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO)
932 .to_string_lossy();
933 let output_filenames = tcx.output_filenames(());
934 let split_name = if tcx.sess.target_can_use_split_dwarf()
935 && let Some(f) = output_filenames.split_dwarf_path(
936 tcx.sess.split_debuginfo(),
937 tcx.sess.opts.unstable_opts.split_dwarf_kind,
938 codegen_unit_name,
939 tcx.sess.invocation_temp.as_deref(),
940 ) {
941 Some(tcx.sess.source_map().path_mapping().to_real_filename(f))
943 } else {
944 None
945 };
946 let split_name = split_name
947 .as_ref()
948 .map(|f| f.for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO).to_string_lossy())
949 .unwrap_or_default();
950 let kind = DebugEmissionKind::from_generic(tcx.sess.opts.debuginfo);
951
952 let dwarf_version = tcx.sess.dwarf_version();
953 let is_dwarf_kind =
954 matches!(tcx.sess.target.debuginfo_kind, DebuginfoKind::Dwarf | DebuginfoKind::DwarfDsym);
955 let debug_name_table_kind = if is_dwarf_kind && dwarf_version <= 4 {
957 DebugNameTableKind::None
958 } else {
959 DebugNameTableKind::Default
960 };
961
962 unsafe {
963 let compile_unit_file = create_file(
964 debug_context.builder.as_ref(),
965 &name_in_debuginfo,
966 &work_dir,
967 "",
968 llvm::ChecksumKind::None,
969 None,
970 );
971
972 let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit(
973 debug_context.builder.as_ref(),
974 dwarf_const::DW_LANG_Rust,
975 compile_unit_file,
976 producer.as_c_char_ptr(),
977 producer.len(),
978 tcx.sess.opts.optimize != config::OptLevel::No,
979 c"".as_ptr(),
980 0,
981 split_name.as_c_char_ptr(),
985 split_name.len(),
986 kind,
987 0,
988 tcx.sess.opts.unstable_opts.split_dwarf_inlining,
989 debug_name_table_kind,
990 );
991
992 return unit_metadata;
993 };
994}
995
996fn build_field_di_node<'ll, 'tcx>(
998 cx: &CodegenCx<'ll, 'tcx>,
999 owner: &'ll DIScope,
1000 name: &str,
1001 layout: TyAndLayout<'tcx>,
1002 offset: Size,
1003 flags: DIFlags,
1004 type_di_node: &'ll DIType,
1005 def_id: Option<DefId>,
1006) -> &'ll DIType {
1007 let (file_metadata, line_number) = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers
1008 {
1009 file_metadata_from_def_id(cx, def_id)
1010 } else {
1011 (unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
1012 };
1013 create_member_type(
1014 cx,
1015 owner,
1016 name,
1017 file_metadata,
1018 line_number,
1019 layout,
1020 offset,
1021 flags,
1022 type_di_node,
1023 )
1024}
1025
1026fn create_member_type<'ll, 'tcx>(
1027 cx: &CodegenCx<'ll, 'tcx>,
1028 owner: &'ll DIScope,
1029 name: &str,
1030 file_metadata: &'ll DIType,
1031 line_number: u32,
1032 layout: TyAndLayout<'tcx>,
1033 offset: Size,
1034 flags: DIFlags,
1035 type_di_node: &'ll DIType,
1036) -> &'ll DIType {
1037 unsafe {
1038 llvm::LLVMDIBuilderCreateMemberType(
1039 DIB(cx),
1040 owner,
1041 name.as_ptr(),
1042 name.len(),
1043 file_metadata,
1044 line_number,
1045 layout.size.bits(),
1046 layout.align.abi.bits() as u32,
1047 offset.bits(),
1048 flags,
1049 type_di_node,
1050 )
1051 }
1052}
1053
1054fn visibility_di_flags<'ll, 'tcx>(
1060 cx: &CodegenCx<'ll, 'tcx>,
1061 did: DefId,
1062 type_did: DefId,
1063) -> DIFlags {
1064 let parent_did = cx.tcx.parent(type_did);
1065 let visibility = cx.tcx.visibility(did);
1066 match visibility {
1067 Visibility::Public => DIFlags::FlagPublic,
1068 Visibility::Restricted(did) if did == parent_did => DIFlags::FlagPrivate,
1070 Visibility::Restricted(..) => DIFlags::FlagProtected,
1072 }
1073}
1074
1075fn build_struct_type_di_node<'ll, 'tcx>(
1077 cx: &CodegenCx<'ll, 'tcx>,
1078 unique_type_id: UniqueTypeId<'tcx>,
1079) -> DINodeCreationResult<'ll> {
1080 let struct_type = unique_type_id.expect_ty();
1081 let ty::Adt(adt_def, _) = struct_type.kind() else {
1082 bug!("build_struct_type_di_node() called with non-struct-type: {:?}", struct_type);
1083 };
1084 assert!(adt_def.is_struct());
1085 let containing_scope = get_namespace_for_item(cx, adt_def.did());
1086 let struct_type_and_layout = cx.layout_of(struct_type);
1087 let variant_def = adt_def.non_enum_variant();
1088 let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1089 Some(file_metadata_from_def_id(cx, Some(adt_def.did())))
1090 } else {
1091 None
1092 };
1093
1094 type_map::build_type_with_children(
1095 cx,
1096 type_map::stub(
1097 cx,
1098 Stub::Struct,
1099 unique_type_id,
1100 &compute_debuginfo_type_name(cx.tcx, struct_type, false),
1101 def_location,
1102 size_and_align_of(struct_type_and_layout),
1103 Some(containing_scope),
1104 visibility_di_flags(cx, adt_def.did(), adt_def.did()),
1105 ),
1106 |cx, owner| {
1108 variant_def
1109 .fields
1110 .iter()
1111 .enumerate()
1112 .map(|(i, f)| {
1113 let field_name = if variant_def.ctor_kind() == Some(CtorKind::Fn) {
1114 tuple_field_name(i)
1116 } else {
1117 Cow::Borrowed(f.name.as_str())
1119 };
1120 let field_layout = struct_type_and_layout.field(cx, i);
1121 let def_id = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1122 Some(f.did)
1123 } else {
1124 None
1125 };
1126 build_field_di_node(
1127 cx,
1128 owner,
1129 &field_name[..],
1130 field_layout,
1131 struct_type_and_layout.fields.offset(i),
1132 visibility_di_flags(cx, f.did, adt_def.did()),
1133 type_di_node(cx, field_layout.ty),
1134 def_id,
1135 )
1136 })
1137 .collect()
1138 },
1139 |cx| build_generic_type_param_di_nodes(cx, struct_type),
1140 )
1141}
1142
1143fn build_upvar_field_di_nodes<'ll, 'tcx>(
1150 cx: &CodegenCx<'ll, 'tcx>,
1151 closure_or_coroutine_ty: Ty<'tcx>,
1152 closure_or_coroutine_di_node: &'ll DIType,
1153) -> SmallVec<&'ll DIType> {
1154 let (&def_id, up_var_tys) = match closure_or_coroutine_ty.kind() {
1155 ty::Coroutine(def_id, args) => (def_id, args.as_coroutine().prefix_tys()),
1156 ty::Closure(def_id, args) => (def_id, args.as_closure().upvar_tys()),
1157 ty::CoroutineClosure(def_id, args) => (def_id, args.as_coroutine_closure().upvar_tys()),
1158 _ => {
1159 bug!(
1160 "build_upvar_field_di_nodes() called with non-closure-or-coroutine-type: {:?}",
1161 closure_or_coroutine_ty
1162 )
1163 }
1164 };
1165
1166 assert!(up_var_tys.iter().all(|t| t == cx.tcx.normalize_erasing_regions(cx.typing_env(), t)));
1167
1168 let capture_names = cx.tcx.closure_saved_names_of_captured_variables(def_id);
1169 let layout = cx.layout_of(closure_or_coroutine_ty);
1170
1171 up_var_tys
1172 .into_iter()
1173 .zip(capture_names.iter())
1174 .enumerate()
1175 .map(|(index, (up_var_ty, capture_name))| {
1176 build_field_di_node(
1177 cx,
1178 closure_or_coroutine_di_node,
1179 capture_name.as_str(),
1180 cx.layout_of(up_var_ty),
1181 layout.fields.offset(index),
1182 DIFlags::FlagZero,
1183 type_di_node(cx, up_var_ty),
1184 None,
1185 )
1186 })
1187 .collect()
1188}
1189
1190fn build_tuple_type_di_node<'ll, 'tcx>(
1192 cx: &CodegenCx<'ll, 'tcx>,
1193 unique_type_id: UniqueTypeId<'tcx>,
1194) -> DINodeCreationResult<'ll> {
1195 let tuple_type = unique_type_id.expect_ty();
1196 let &ty::Tuple(component_types) = tuple_type.kind() else {
1197 bug!("build_tuple_type_di_node() called with non-tuple-type: {:?}", tuple_type)
1198 };
1199
1200 let tuple_type_and_layout = cx.layout_of(tuple_type);
1201 let type_name = compute_debuginfo_type_name(cx.tcx, tuple_type, false);
1202
1203 type_map::build_type_with_children(
1204 cx,
1205 type_map::stub(
1206 cx,
1207 Stub::Struct,
1208 unique_type_id,
1209 &type_name,
1210 None,
1211 size_and_align_of(tuple_type_and_layout),
1212 NO_SCOPE_METADATA,
1213 DIFlags::FlagZero,
1214 ),
1215 |cx, tuple_di_node| {
1217 component_types
1218 .into_iter()
1219 .enumerate()
1220 .map(|(index, component_type)| {
1221 build_field_di_node(
1222 cx,
1223 tuple_di_node,
1224 &tuple_field_name(index),
1225 cx.layout_of(component_type),
1226 tuple_type_and_layout.fields.offset(index),
1227 DIFlags::FlagZero,
1228 type_di_node(cx, component_type),
1229 None,
1230 )
1231 })
1232 .collect()
1233 },
1234 NO_GENERICS,
1235 )
1236}
1237
1238fn build_closure_env_di_node<'ll, 'tcx>(
1240 cx: &CodegenCx<'ll, 'tcx>,
1241 unique_type_id: UniqueTypeId<'tcx>,
1242) -> DINodeCreationResult<'ll> {
1243 let closure_env_type = unique_type_id.expect_ty();
1244 let &(ty::Closure(def_id, _) | ty::CoroutineClosure(def_id, _)) = closure_env_type.kind()
1245 else {
1246 bug!("build_closure_env_di_node() called with non-closure-type: {:?}", closure_env_type)
1247 };
1248 let containing_scope = get_namespace_for_item(cx, def_id);
1249 let type_name = compute_debuginfo_type_name(cx.tcx, closure_env_type, false);
1250
1251 let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1252 Some(file_metadata_from_def_id(cx, Some(def_id)))
1253 } else {
1254 None
1255 };
1256
1257 type_map::build_type_with_children(
1258 cx,
1259 type_map::stub(
1260 cx,
1261 Stub::Struct,
1262 unique_type_id,
1263 &type_name,
1264 def_location,
1265 cx.size_and_align_of(closure_env_type),
1266 Some(containing_scope),
1267 DIFlags::FlagZero,
1268 ),
1269 |cx, owner| build_upvar_field_di_nodes(cx, closure_env_type, owner),
1271 NO_GENERICS,
1272 )
1273}
1274
1275fn build_union_type_di_node<'ll, 'tcx>(
1277 cx: &CodegenCx<'ll, 'tcx>,
1278 unique_type_id: UniqueTypeId<'tcx>,
1279) -> DINodeCreationResult<'ll> {
1280 let union_type = unique_type_id.expect_ty();
1281 let (union_def_id, variant_def) = match union_type.kind() {
1282 ty::Adt(def, _) => (def.did(), def.non_enum_variant()),
1283 _ => bug!("build_union_type_di_node on a non-ADT"),
1284 };
1285 let containing_scope = get_namespace_for_item(cx, union_def_id);
1286 let union_ty_and_layout = cx.layout_of(union_type);
1287 let type_name = compute_debuginfo_type_name(cx.tcx, union_type, false);
1288 let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1289 Some(file_metadata_from_def_id(cx, Some(union_def_id)))
1290 } else {
1291 None
1292 };
1293
1294 type_map::build_type_with_children(
1295 cx,
1296 type_map::stub(
1297 cx,
1298 Stub::Union,
1299 unique_type_id,
1300 &type_name,
1301 def_location,
1302 size_and_align_of(union_ty_and_layout),
1303 Some(containing_scope),
1304 DIFlags::FlagZero,
1305 ),
1306 |cx, owner| {
1308 variant_def
1309 .fields
1310 .iter()
1311 .enumerate()
1312 .map(|(i, f)| {
1313 let field_layout = union_ty_and_layout.field(cx, i);
1314 let def_id = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1315 Some(f.did)
1316 } else {
1317 None
1318 };
1319 build_field_di_node(
1320 cx,
1321 owner,
1322 f.name.as_str(),
1323 field_layout,
1324 Size::ZERO,
1325 DIFlags::FlagZero,
1326 type_di_node(cx, field_layout.ty),
1327 def_id,
1328 )
1329 })
1330 .collect()
1331 },
1332 |cx| build_generic_type_param_di_nodes(cx, union_type),
1334 )
1335}
1336
1337fn build_generic_type_param_di_nodes<'ll, 'tcx>(
1339 cx: &CodegenCx<'ll, 'tcx>,
1340 ty: Ty<'tcx>,
1341) -> SmallVec<Option<&'ll DIType>> {
1342 if let ty::Adt(def, args) = *ty.kind() {
1343 if args.types().next().is_some() {
1344 let generics = cx.tcx.generics_of(def.did());
1345 let names = get_parameter_names(cx, generics);
1346 let template_params: SmallVec<_> = iter::zip(args, names)
1347 .filter_map(|(kind, name)| {
1348 kind.as_type().map(|ty| {
1349 let actual_type = cx.tcx.normalize_erasing_regions(cx.typing_env(), ty);
1350 let actual_type_di_node = type_di_node(cx, actual_type);
1351 Some(cx.create_template_type_parameter(name.as_str(), actual_type_di_node))
1352 })
1353 })
1354 .collect();
1355
1356 return template_params;
1357 }
1358 }
1359
1360 return smallvec![];
1361
1362 fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec<Symbol> {
1363 let mut names = generics
1364 .parent
1365 .map_or_else(Vec::new, |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id)));
1366 names.extend(generics.own_params.iter().map(|param| param.name));
1367 names
1368 }
1369}
1370
1371pub(crate) fn build_global_var_di_node<'ll>(
1375 cx: &CodegenCx<'ll, '_>,
1376 def_id: DefId,
1377 global: &'ll Value,
1378) {
1379 if cx.dbg_cx.is_none() {
1380 return;
1381 }
1382
1383 if cx.sess().opts.debuginfo != DebugInfo::Full {
1385 return;
1386 }
1387
1388 let tcx = cx.tcx;
1389
1390 let var_scope = get_namespace_for_item(cx, def_id);
1393 let (file_metadata, line_number) = file_metadata_from_def_id(cx, Some(def_id));
1394
1395 let is_local_to_unit = is_node_local_to_unit(cx, def_id);
1396
1397 let DefKind::Static { nested, .. } = cx.tcx.def_kind(def_id) else { bug!() };
1398 if nested {
1399 return;
1400 }
1401 let variable_type = Instance::mono(cx.tcx, def_id).ty(cx.tcx, cx.typing_env());
1402 let type_di_node = type_di_node(cx, variable_type);
1403 let var_name = tcx.item_name(def_id);
1404 let var_name = var_name.as_str();
1405 let linkage_name = mangled_name_of_instance(cx, Instance::mono(tcx, def_id)).name;
1406 let linkage_name = if var_name == linkage_name { "" } else { linkage_name };
1409
1410 let global_align = cx.align_of(variable_type);
1411
1412 unsafe {
1413 llvm::LLVMRustDIBuilderCreateStaticVariable(
1414 DIB(cx),
1415 Some(var_scope),
1416 var_name.as_c_char_ptr(),
1417 var_name.len(),
1418 linkage_name.as_c_char_ptr(),
1419 linkage_name.len(),
1420 file_metadata,
1421 line_number,
1422 type_di_node,
1423 is_local_to_unit,
1424 global,
1425 None,
1426 global_align.bits() as u32,
1427 );
1428 }
1429}
1430
1431fn build_vtable_type_di_node<'ll, 'tcx>(
1441 cx: &CodegenCx<'ll, 'tcx>,
1442 ty: Ty<'tcx>,
1443 poly_trait_ref: Option<ty::ExistentialTraitRef<'tcx>>,
1444) -> &'ll DIType {
1445 let tcx = cx.tcx;
1446
1447 let vtable_entries = if let Some(poly_trait_ref) = poly_trait_ref {
1448 let trait_ref = poly_trait_ref.with_self_ty(tcx, ty);
1449 let trait_ref = tcx.erase_and_anonymize_regions(trait_ref);
1450
1451 tcx.vtable_entries(trait_ref)
1452 } else {
1453 TyCtxt::COMMON_VTABLE_ENTRIES
1454 };
1455
1456 let void_pointer_ty = Ty::new_imm_ptr(tcx, tcx.types.unit);
1459 let void_pointer_type_di_node = type_di_node(cx, void_pointer_ty);
1460 let usize_di_node = type_di_node(cx, tcx.types.usize);
1461 let pointer_layout = cx.layout_of(void_pointer_ty);
1462 let pointer_size = pointer_layout.size;
1463 let pointer_align = pointer_layout.align.abi;
1464 assert_eq!(cx.size_and_align_of(tcx.types.usize), (pointer_size, pointer_align));
1468
1469 let vtable_type_name =
1470 compute_debuginfo_vtable_name(cx.tcx, ty, poly_trait_ref, VTableNameKind::Type);
1471 let unique_type_id = UniqueTypeId::for_vtable_ty(tcx, ty, poly_trait_ref);
1472 let size = pointer_size * vtable_entries.len() as u64;
1473
1474 let vtable_holder = type_di_node(cx, ty);
1477
1478 build_type_with_children(
1479 cx,
1480 type_map::stub(
1481 cx,
1482 Stub::VTableTy { vtable_holder },
1483 unique_type_id,
1484 &vtable_type_name,
1485 None,
1486 (size, pointer_align),
1487 NO_SCOPE_METADATA,
1488 DIFlags::FlagArtificial,
1489 ),
1490 |cx, vtable_type_di_node| {
1491 vtable_entries
1492 .iter()
1493 .enumerate()
1494 .filter_map(|(index, vtable_entry)| {
1495 let (field_name, field_type_di_node) = match vtable_entry {
1496 ty::VtblEntry::MetadataDropInPlace => {
1497 ("drop_in_place".to_string(), void_pointer_type_di_node)
1498 }
1499 ty::VtblEntry::Method(_) => {
1500 (format!("__method{index}"), void_pointer_type_di_node)
1504 }
1505 ty::VtblEntry::TraitVPtr(_) => {
1506 (format!("__super_trait_ptr{index}"), void_pointer_type_di_node)
1507 }
1508 ty::VtblEntry::MetadataAlign => ("align".to_string(), usize_di_node),
1509 ty::VtblEntry::MetadataSize => ("size".to_string(), usize_di_node),
1510 ty::VtblEntry::Vacant => return None,
1511 };
1512
1513 let field_offset = pointer_size * index as u64;
1514
1515 Some(build_field_di_node(
1516 cx,
1517 vtable_type_di_node,
1518 &field_name,
1519 pointer_layout,
1520 field_offset,
1521 DIFlags::FlagZero,
1522 field_type_di_node,
1523 None,
1524 ))
1525 })
1526 .collect()
1527 },
1528 NO_GENERICS,
1529 )
1530 .di_node
1531}
1532
1533fn find_vtable_behind_cast<'ll>(vtable: &'ll Value) -> &'ll Value {
1542 unsafe {
1544 if let Some(c) = llvm::LLVMIsAConstantExpr(vtable) {
1545 if llvm::LLVMGetConstOpcode(c) == llvm::Opcode::AddrSpaceCast {
1546 return llvm::LLVMGetOperand(c, 0).unwrap();
1547 }
1548 }
1549 }
1550 vtable
1551}
1552
1553pub(crate) fn apply_vcall_visibility_metadata<'ll, 'tcx>(
1554 cx: &CodegenCx<'ll, 'tcx>,
1555 ty: Ty<'tcx>,
1556 trait_ref: Option<ExistentialTraitRef<'tcx>>,
1557 vtable: &'ll Value,
1558) {
1559 if !cx.sess().opts.unstable_opts.virtual_function_elimination || cx.sess().lto() != Lto::Fat {
1562 return;
1563 }
1564
1565 enum VCallVisibility {
1566 Public = 0,
1567 LinkageUnit = 1,
1568 TranslationUnit = 2,
1569 }
1570
1571 let Some(trait_ref) = trait_ref else { return };
1572
1573 let vtable = find_vtable_behind_cast(vtable);
1575 let trait_ref_self = trait_ref.with_self_ty(cx.tcx, ty);
1576 let trait_ref_self = cx.tcx.erase_and_anonymize_regions(trait_ref_self);
1577 let trait_def_id = trait_ref_self.def_id;
1578 let trait_vis = cx.tcx.visibility(trait_def_id);
1579
1580 let cgus = cx.sess().codegen_units().as_usize();
1581 let single_cgu = cgus == 1;
1582
1583 let lto = cx.sess().lto();
1584
1585 let vcall_visibility = match (lto, trait_vis, single_cgu) {
1588 (Lto::No | Lto::ThinLocal, Visibility::Public, _)
1591 | (Lto::No, Visibility::Restricted(_), false) => VCallVisibility::Public,
1592 (Lto::Fat | Lto::Thin, Visibility::Public, _)
1597 | (Lto::ThinLocal | Lto::Thin | Lto::Fat, Visibility::Restricted(_), false) => {
1598 VCallVisibility::LinkageUnit
1599 }
1600 (_, Visibility::Restricted(_), true) => VCallVisibility::TranslationUnit,
1603 };
1604
1605 let trait_ref_typeid = typeid_for_trait_ref(cx.tcx, trait_ref);
1606 let typeid = cx.create_metadata(trait_ref_typeid.as_bytes());
1607
1608 unsafe {
1609 let v = [llvm::LLVMValueAsMetadata(cx.const_usize(0)), typeid];
1610 llvm::LLVMRustGlobalAddMetadata(
1611 vtable,
1612 llvm::MD_type as c_uint,
1613 llvm::LLVMMDNodeInContext2(cx.llcx, v.as_ptr(), v.len()),
1614 );
1615 let vcall_visibility = llvm::LLVMValueAsMetadata(cx.const_u64(vcall_visibility as u64));
1616 let vcall_visibility_metadata = llvm::LLVMMDNodeInContext2(cx.llcx, &vcall_visibility, 1);
1617 llvm::LLVMGlobalSetMetadata(
1618 vtable,
1619 llvm::MetadataType::MD_vcall_visibility as c_uint,
1620 vcall_visibility_metadata,
1621 );
1622 }
1623}
1624
1625pub(crate) fn create_vtable_di_node<'ll, 'tcx>(
1630 cx: &CodegenCx<'ll, 'tcx>,
1631 ty: Ty<'tcx>,
1632 poly_trait_ref: Option<ty::ExistentialTraitRef<'tcx>>,
1633 vtable: &'ll Value,
1634) {
1635 if cx.dbg_cx.is_none() {
1636 return;
1637 }
1638
1639 if cx.sess().opts.debuginfo != DebugInfo::Full {
1641 return;
1642 }
1643
1644 let vtable = find_vtable_behind_cast(vtable);
1646
1647 llvm::set_unnamed_address(vtable, llvm::UnnamedAddr::No);
1651
1652 let vtable_name =
1653 compute_debuginfo_vtable_name(cx.tcx, ty, poly_trait_ref, VTableNameKind::GlobalVariable);
1654 let vtable_type_di_node = build_vtable_type_di_node(cx, ty, poly_trait_ref);
1655 let linkage_name = "";
1656
1657 unsafe {
1658 llvm::LLVMRustDIBuilderCreateStaticVariable(
1659 DIB(cx),
1660 NO_SCOPE_METADATA,
1661 vtable_name.as_c_char_ptr(),
1662 vtable_name.len(),
1663 linkage_name.as_c_char_ptr(),
1664 linkage_name.len(),
1665 unknown_file_metadata(cx),
1666 UNKNOWN_LINE_NUMBER,
1667 vtable_type_di_node,
1668 true,
1669 vtable,
1670 None,
1671 0,
1672 );
1673 }
1674}
1675
1676pub(crate) fn extend_scope_to_file<'ll>(
1678 cx: &CodegenCx<'ll, '_>,
1679 scope_metadata: &'ll DIScope,
1680 file: &SourceFile,
1681) -> &'ll DILexicalBlock {
1682 let file_metadata = file_metadata(cx, file);
1683 unsafe {
1684 llvm::LLVMDIBuilderCreateLexicalBlockFile(
1685 DIB(cx),
1686 scope_metadata,
1687 file_metadata,
1688 0u32,
1689 )
1690 }
1691}
1692
1693fn tuple_field_name(field_index: usize) -> Cow<'static, str> {
1694 const TUPLE_FIELD_NAMES: [&'static str; 16] = [
1695 "__0", "__1", "__2", "__3", "__4", "__5", "__6", "__7", "__8", "__9", "__10", "__11",
1696 "__12", "__13", "__14", "__15",
1697 ];
1698 TUPLE_FIELD_NAMES
1699 .get(field_index)
1700 .map(|s| Cow::from(*s))
1701 .unwrap_or_else(|| Cow::from(format!("__{field_index}")))
1702}
1703
1704pub(crate) type DefinitionLocation<'ll> = (&'ll DIFile, c_uint);
1705
1706pub(crate) fn file_metadata_from_def_id<'ll>(
1707 cx: &CodegenCx<'ll, '_>,
1708 def_id: Option<DefId>,
1709) -> DefinitionLocation<'ll> {
1710 if let Some(def_id) = def_id
1711 && let span = hygiene::walk_chain_collapsed(cx.tcx.def_span(def_id), DUMMY_SP)
1712 && !span.is_dummy()
1713 {
1714 let loc = cx.lookup_debug_loc(span.lo());
1715 (file_metadata(cx, &loc.file), loc.line)
1716 } else {
1717 (unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
1718 }
1719}