1use std::borrow::Cow;
2use std::fmt::{self, Write};
3use std::hash::{Hash, Hasher};
4use std::path::{Path, PathBuf};
5use std::{iter, ptr};
6
7use libc::{c_char, c_longlong, c_uint};
8use rustc_abi::{Align, 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::{HasTypingEnv, LayoutOf, TyAndLayout};
15use rustc_middle::ty::{
16 self, AdtKind, CoroutineArgsExt, ExistentialTraitRef, Instance, Ty, TyCtxt, Visibility,
17};
18use rustc_session::config::{self, DebugInfo, Lto};
19use rustc_span::{DUMMY_SP, FileName, FileNameDisplayPreference, SourceFile, Symbol, hygiene};
20use rustc_symbol_mangling::typeid_for_trait_ref;
21use rustc_target::spec::DebuginfoKind;
22use smallvec::smallvec;
23use tracing::{debug, instrument};
24
25pub(crate) use self::type_map::TypeMap;
26use self::type_map::{DINodeCreationResult, Stub, UniqueTypeId};
27use super::CodegenUnitDebugContext;
28use super::namespace::mangled_name_of_instance;
29use super::type_names::{compute_debuginfo_type_name, compute_debuginfo_vtable_name};
30use super::utils::{
31 DIB, create_DIArray, debug_context, get_namespace_for_item, is_node_local_to_unit,
32};
33use crate::common::{AsCCharPtr, CodegenCx};
34use crate::debuginfo::dwarf_const;
35use crate::debuginfo::metadata::type_map::build_type_with_children;
36use crate::debuginfo::utils::{WidePtrKind, wide_pointer_kind};
37use crate::llvm::debuginfo::{
38 DIDescriptor, DIFile, DIFlags, DILexicalBlock, DIScope, DIType, DebugEmissionKind,
39 DebugNameTableKind,
40};
41use crate::value::Value;
42use crate::{abi, llvm};
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<&'ll DIType> = |_| SmallVec::new();
70
71type SmallVec<T> = smallvec::SmallVec<[T; 16]>;
74
75mod enums;
76mod type_map;
77
78macro_rules! return_if_di_node_created_in_meantime {
81 ($cx: expr, $unique_type_id: expr) => {
82 if let Some(di_node) = debug_context($cx).type_map.di_node_for_unique_id($unique_type_id) {
83 return DINodeCreationResult::new(di_node, true);
84 }
85 };
86}
87
88#[inline]
90fn size_and_align_of(ty_and_layout: TyAndLayout<'_>) -> (Size, Align) {
91 (ty_and_layout.size, ty_and_layout.align.abi)
92}
93
94fn build_fixed_size_array_di_node<'ll, 'tcx>(
97 cx: &CodegenCx<'ll, 'tcx>,
98 unique_type_id: UniqueTypeId<'tcx>,
99 array_type: Ty<'tcx>,
100) -> DINodeCreationResult<'ll> {
101 let ty::Array(element_type, len) = array_type.kind() else {
102 bug!("build_fixed_size_array_di_node() called with non-ty::Array type `{:?}`", array_type)
103 };
104
105 let element_type_di_node = type_di_node(cx, *element_type);
106
107 return_if_di_node_created_in_meantime!(cx, unique_type_id);
108
109 let (size, align) = cx.size_and_align_of(array_type);
110
111 let upper_bound = len
112 .try_to_target_usize(cx.tcx)
113 .expect("expected monomorphic const in codegen") as c_longlong;
114
115 let subrange =
116 unsafe { Some(llvm::LLVMRustDIBuilderGetOrCreateSubrange(DIB(cx), 0, upper_bound)) };
117
118 let subscripts = create_DIArray(DIB(cx), &[subrange]);
119 let di_node = unsafe {
120 llvm::LLVMRustDIBuilderCreateArrayType(
121 DIB(cx),
122 size.bits(),
123 align.bits() as u32,
124 element_type_di_node,
125 subscripts,
126 )
127 };
128
129 DINodeCreationResult::new(di_node, false)
130}
131
132fn build_pointer_or_reference_di_node<'ll, 'tcx>(
141 cx: &CodegenCx<'ll, 'tcx>,
142 ptr_type: Ty<'tcx>,
143 pointee_type: Ty<'tcx>,
144 unique_type_id: UniqueTypeId<'tcx>,
145) -> DINodeCreationResult<'ll> {
146 assert_eq!(
149 cx.size_and_align_of(ptr_type),
150 cx.size_and_align_of(Ty::new_mut_ptr(cx.tcx, pointee_type))
151 );
152
153 let pointee_type_di_node = type_di_node(cx, pointee_type);
154
155 return_if_di_node_created_in_meantime!(cx, unique_type_id);
156
157 let data_layout = &cx.tcx.data_layout;
158 let ptr_type_debuginfo_name = compute_debuginfo_type_name(cx.tcx, ptr_type, true);
159
160 match wide_pointer_kind(cx, pointee_type) {
161 None => {
162 assert_eq!(
164 (data_layout.pointer_size, data_layout.pointer_align.abi),
165 cx.size_and_align_of(ptr_type),
166 "ptr_type={ptr_type}, pointee_type={pointee_type}",
167 );
168
169 let di_node = unsafe {
170 llvm::LLVMRustDIBuilderCreatePointerType(
171 DIB(cx),
172 pointee_type_di_node,
173 data_layout.pointer_size.bits(),
174 data_layout.pointer_align.abi.bits() as u32,
175 0, ptr_type_debuginfo_name.as_c_char_ptr(),
177 ptr_type_debuginfo_name.len(),
178 )
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, abi::WIDE_PTR_ADDR);
215 let extra_field = layout.field(cx, abi::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 assert_eq!(abi::WIDE_PTR_ADDR, 0);
223 assert_eq!(abi::WIDE_PTR_EXTRA, 1);
224
225 let data_ptr_type_di_node = unsafe {
228 llvm::LLVMRustDIBuilderCreatePointerType(
229 DIB(cx),
230 pointee_type_di_node,
231 addr_field.size.bits(),
232 addr_field.align.abi.bits() as u32,
233 0, std::ptr::null(),
235 0,
236 )
237 };
238
239 smallvec![
240 build_field_di_node(
241 cx,
242 owner,
243 addr_field_name,
244 (addr_field.size, addr_field.align.abi),
245 layout.fields.offset(abi::WIDE_PTR_ADDR),
246 DIFlags::FlagZero,
247 data_ptr_type_di_node,
248 None,
249 ),
250 build_field_di_node(
251 cx,
252 owner,
253 extra_field_name,
254 (extra_field.size, extra_field.align.abi),
255 layout.fields.offset(abi::WIDE_PTR_EXTRA),
256 DIFlags::FlagZero,
257 type_di_node(cx, extra_field.ty),
258 None,
259 ),
260 ]
261 },
262 NO_GENERICS,
263 )
264 }
265 }
266}
267
268fn build_subroutine_type_di_node<'ll, 'tcx>(
269 cx: &CodegenCx<'ll, 'tcx>,
270 unique_type_id: UniqueTypeId<'tcx>,
271) -> DINodeCreationResult<'ll> {
272 debug_context(cx)
285 .type_map
286 .unique_id_to_di_node
287 .borrow_mut()
288 .insert(unique_type_id, recursion_marker_type_di_node(cx));
289
290 let fn_ty = unique_type_id.expect_ty();
291 let signature =
292 cx.tcx.normalize_erasing_late_bound_regions(cx.typing_env(), fn_ty.fn_sig(cx.tcx));
293
294 let signature_di_nodes: SmallVec<_> = iter::once(
295 match signature.output().kind() {
297 ty::Tuple(tys) if tys.is_empty() => {
298 None
300 }
301 _ => Some(type_di_node(cx, signature.output())),
302 },
303 )
304 .chain(
305 signature.inputs().iter().map(|&argument_type| Some(type_di_node(cx, argument_type))),
307 )
308 .collect();
309
310 debug_context(cx).type_map.unique_id_to_di_node.borrow_mut().remove(&unique_type_id);
311
312 let fn_di_node = unsafe {
313 llvm::LLVMRustDIBuilderCreateSubroutineType(
314 DIB(cx),
315 create_DIArray(DIB(cx), &signature_di_nodes[..]),
316 )
317 };
318
319 let name = compute_debuginfo_type_name(cx.tcx, fn_ty, false);
321 let (size, align) = match fn_ty.kind() {
322 ty::FnDef(..) => (Size::ZERO, Align::ONE),
323 ty::FnPtr(..) => (cx.tcx.data_layout.pointer_size, cx.tcx.data_layout.pointer_align.abi),
324 _ => unreachable!(),
325 };
326 let di_node = unsafe {
327 llvm::LLVMRustDIBuilderCreatePointerType(
328 DIB(cx),
329 fn_di_node,
330 size.bits(),
331 align.bits() as u32,
332 0, name.as_c_char_ptr(),
334 name.len(),
335 )
336 };
337
338 DINodeCreationResult::new(di_node, false)
339}
340
341fn build_dyn_type_di_node<'ll, 'tcx>(
344 cx: &CodegenCx<'ll, 'tcx>,
345 dyn_type: Ty<'tcx>,
346 unique_type_id: UniqueTypeId<'tcx>,
347) -> DINodeCreationResult<'ll> {
348 if let ty::Dynamic(..) = dyn_type.kind() {
349 let type_name = compute_debuginfo_type_name(cx.tcx, dyn_type, true);
350 type_map::build_type_with_children(
351 cx,
352 type_map::stub(
353 cx,
354 Stub::Struct,
355 unique_type_id,
356 &type_name,
357 None,
358 cx.size_and_align_of(dyn_type),
359 NO_SCOPE_METADATA,
360 DIFlags::FlagZero,
361 ),
362 |_, _| smallvec![],
363 NO_GENERICS,
364 )
365 } else {
366 bug!(
367 "Only ty::Dynamic is valid for build_dyn_type_di_node(). Found {:?} instead.",
368 dyn_type
369 )
370 }
371}
372
373fn build_slice_type_di_node<'ll, 'tcx>(
391 cx: &CodegenCx<'ll, 'tcx>,
392 slice_type: Ty<'tcx>,
393 unique_type_id: UniqueTypeId<'tcx>,
394) -> DINodeCreationResult<'ll> {
395 let element_type = match slice_type.kind() {
396 ty::Slice(element_type) => *element_type,
397 ty::Str => cx.tcx.types.u8,
398 _ => {
399 bug!(
400 "Only ty::Slice is valid for build_slice_type_di_node(). Found {:?} instead.",
401 slice_type
402 )
403 }
404 };
405
406 let element_type_di_node = type_di_node(cx, element_type);
407 return_if_di_node_created_in_meantime!(cx, unique_type_id);
408 DINodeCreationResult { di_node: element_type_di_node, already_stored_in_typemap: false }
409}
410
411pub(crate) fn type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) -> &'ll DIType {
416 let unique_type_id = UniqueTypeId::for_ty(cx.tcx, t);
417
418 if let Some(existing_di_node) = debug_context(cx).type_map.di_node_for_unique_id(unique_type_id)
419 {
420 return existing_di_node;
421 }
422
423 debug!("type_di_node: {:?} kind: {:?}", t, t.kind());
424
425 let DINodeCreationResult { di_node, already_stored_in_typemap } = match *t.kind() {
426 ty::Never | ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) => {
427 build_basic_type_di_node(cx, t)
428 }
429 ty::Tuple(elements) if elements.is_empty() => build_basic_type_di_node(cx, t),
430 ty::Array(..) => build_fixed_size_array_di_node(cx, unique_type_id, t),
431 ty::Slice(_) | ty::Str => build_slice_type_di_node(cx, t, unique_type_id),
432 ty::Dynamic(..) => build_dyn_type_di_node(cx, t, unique_type_id),
433 ty::Foreign(..) => build_foreign_type_di_node(cx, t, unique_type_id),
434 ty::RawPtr(pointee_type, _) | ty::Ref(_, pointee_type, _) => {
435 build_pointer_or_reference_di_node(cx, t, pointee_type, unique_type_id)
436 }
437 ty::Adt(def, args)
441 if def.is_box()
442 && args.get(1).is_none_or(|arg| cx.layout_of(arg.expect_ty()).is_1zst()) =>
443 {
444 build_pointer_or_reference_di_node(cx, t, t.expect_boxed_ty(), unique_type_id)
445 }
446 ty::FnDef(..) | ty::FnPtr(..) => build_subroutine_type_di_node(cx, unique_type_id),
447 ty::Closure(..) => build_closure_env_di_node(cx, unique_type_id),
448 ty::CoroutineClosure(..) => build_closure_env_di_node(cx, unique_type_id),
449 ty::Coroutine(..) => enums::build_coroutine_di_node(cx, unique_type_id),
450 ty::Adt(def, ..) => match def.adt_kind() {
451 AdtKind::Struct => build_struct_type_di_node(cx, unique_type_id),
452 AdtKind::Union => build_union_type_di_node(cx, unique_type_id),
453 AdtKind::Enum => enums::build_enum_type_di_node(cx, unique_type_id),
454 },
455 ty::Tuple(_) => build_tuple_type_di_node(cx, unique_type_id),
456 _ => bug!("debuginfo: unexpected type in type_di_node(): {:?}", t),
457 };
458
459 {
460 if already_stored_in_typemap {
461 let di_node_for_uid =
463 match debug_context(cx).type_map.di_node_for_unique_id(unique_type_id) {
464 Some(di_node) => di_node,
465 None => {
466 bug!(
467 "expected type debuginfo node for unique \
468 type ID '{:?}' to already be in \
469 the `debuginfo::TypeMap` but it \
470 was not.",
471 unique_type_id,
472 );
473 }
474 };
475
476 assert_eq!(di_node_for_uid as *const _, di_node as *const _);
477 } else {
478 debug_context(cx).type_map.insert(unique_type_id, di_node);
479 }
480 }
481
482 di_node
483}
484
485fn recursion_marker_type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) -> &'ll DIType {
487 *debug_context(cx).recursion_marker_type.get_or_init(move || {
488 unsafe {
489 let name = "<recur_type>";
500 llvm::LLVMRustDIBuilderCreateBasicType(
501 DIB(cx),
502 name.as_c_char_ptr(),
503 name.len(),
504 cx.tcx.data_layout.pointer_size.bits(),
505 dwarf_const::DW_ATE_unsigned,
506 )
507 }
508 })
509}
510
511fn hex_encode(data: &[u8]) -> String {
512 let mut hex_string = String::with_capacity(data.len() * 2);
513 for byte in data.iter() {
514 write!(&mut hex_string, "{byte:02x}").unwrap();
515 }
516 hex_string
517}
518
519pub(crate) fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) -> &'ll DIFile {
520 let cache_key = Some((source_file.stable_id, source_file.src_hash));
521 return debug_context(cx)
522 .created_files
523 .borrow_mut()
524 .entry(cache_key)
525 .or_insert_with(|| alloc_new_file_metadata(cx, source_file));
526
527 #[instrument(skip(cx, source_file), level = "debug")]
528 fn alloc_new_file_metadata<'ll>(
529 cx: &CodegenCx<'ll, '_>,
530 source_file: &SourceFile,
531 ) -> &'ll DIFile {
532 debug!(?source_file.name);
533
534 let filename_display_preference =
535 cx.sess().filename_display_preference(RemapPathScopeComponents::DEBUGINFO);
536
537 use rustc_session::config::RemapPathScopeComponents;
538 let (directory, file_name) = match &source_file.name {
539 FileName::Real(filename) => {
540 let working_directory = &cx.sess().opts.working_dir;
541 debug!(?working_directory);
542
543 if filename_display_preference == FileNameDisplayPreference::Remapped {
544 let filename = cx
545 .sess()
546 .source_map()
547 .path_mapping()
548 .to_embeddable_absolute_path(filename.clone(), working_directory);
549
550 let abs_path = filename.remapped_path_if_available();
552 debug!(?abs_path);
553
554 if let Ok(rel_path) =
555 abs_path.strip_prefix(working_directory.remapped_path_if_available())
556 {
557 (
573 working_directory.to_string_lossy(FileNameDisplayPreference::Remapped),
574 rel_path.to_string_lossy().into_owned(),
575 )
576 } else {
577 ("".into(), abs_path.to_string_lossy().into_owned())
578 }
579 } else {
580 let working_directory = working_directory.local_path_if_available();
581 let filename = filename.local_path_if_available();
582
583 debug!(?working_directory, ?filename);
584
585 let abs_path: Cow<'_, Path> = if filename.is_absolute() {
586 filename.into()
587 } else {
588 let mut p = PathBuf::new();
589 p.push(working_directory);
590 p.push(filename);
591 p.into()
592 };
593
594 if let Ok(rel_path) = abs_path.strip_prefix(working_directory) {
595 (
596 working_directory.to_string_lossy(),
597 rel_path.to_string_lossy().into_owned(),
598 )
599 } else {
600 ("".into(), abs_path.to_string_lossy().into_owned())
601 }
602 }
603 }
604 other => {
605 debug!(?other);
606 ("".into(), other.display(filename_display_preference).to_string())
607 }
608 };
609
610 let hash_kind = match source_file.src_hash.kind {
611 rustc_span::SourceFileHashAlgorithm::Md5 => llvm::ChecksumKind::MD5,
612 rustc_span::SourceFileHashAlgorithm::Sha1 => llvm::ChecksumKind::SHA1,
613 rustc_span::SourceFileHashAlgorithm::Sha256 => llvm::ChecksumKind::SHA256,
614 rustc_span::SourceFileHashAlgorithm::Blake3 => llvm::ChecksumKind::None,
615 };
616 let hash_value = hex_encode(source_file.src_hash.hash_bytes());
617
618 let source =
619 cx.sess().opts.unstable_opts.embed_source.then_some(()).and(source_file.src.as_ref());
620
621 unsafe {
622 llvm::LLVMRustDIBuilderCreateFile(
623 DIB(cx),
624 file_name.as_c_char_ptr(),
625 file_name.len(),
626 directory.as_c_char_ptr(),
627 directory.len(),
628 hash_kind,
629 hash_value.as_c_char_ptr(),
630 hash_value.len(),
631 source.map_or(ptr::null(), |x| x.as_c_char_ptr()),
632 source.map_or(0, |x| x.len()),
633 )
634 }
635 }
636}
637
638fn unknown_file_metadata<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll DIFile {
639 debug_context(cx).created_files.borrow_mut().entry(None).or_insert_with(|| unsafe {
640 let file_name = "<unknown>";
641 let directory = "";
642 let hash_value = "";
643
644 llvm::LLVMRustDIBuilderCreateFile(
645 DIB(cx),
646 file_name.as_c_char_ptr(),
647 file_name.len(),
648 directory.as_c_char_ptr(),
649 directory.len(),
650 llvm::ChecksumKind::None,
651 hash_value.as_c_char_ptr(),
652 hash_value.len(),
653 ptr::null(),
654 0,
655 )
656 })
657}
658
659trait MsvcBasicName {
660 fn msvc_basic_name(self) -> &'static str;
661}
662
663impl MsvcBasicName for ty::IntTy {
664 fn msvc_basic_name(self) -> &'static str {
665 match self {
666 ty::IntTy::Isize => "ptrdiff_t",
667 ty::IntTy::I8 => "__int8",
668 ty::IntTy::I16 => "__int16",
669 ty::IntTy::I32 => "__int32",
670 ty::IntTy::I64 => "__int64",
671 ty::IntTy::I128 => "__int128",
672 }
673 }
674}
675
676impl MsvcBasicName for ty::UintTy {
677 fn msvc_basic_name(self) -> &'static str {
678 match self {
679 ty::UintTy::Usize => "size_t",
680 ty::UintTy::U8 => "unsigned __int8",
681 ty::UintTy::U16 => "unsigned __int16",
682 ty::UintTy::U32 => "unsigned __int32",
683 ty::UintTy::U64 => "unsigned __int64",
684 ty::UintTy::U128 => "unsigned __int128",
685 }
686 }
687}
688
689impl MsvcBasicName for ty::FloatTy {
690 fn msvc_basic_name(self) -> &'static str {
691 match self {
694 ty::FloatTy::F16 => {
695 bug!("`f16` should have been handled in `build_basic_type_di_node`")
696 }
697 ty::FloatTy::F32 => "float",
698 ty::FloatTy::F64 => "double",
699 ty::FloatTy::F128 => "fp128",
700 }
701 }
702}
703
704fn build_cpp_f16_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) -> DINodeCreationResult<'ll> {
705 let float_ty = cx.tcx.types.f16;
708 let bits_ty = cx.tcx.types.u16;
709 let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
710 match float_ty.kind() {
711 ty::Adt(def, _) => Some(file_metadata_from_def_id(cx, Some(def.did()))),
712 _ => None,
713 }
714 } else {
715 None
716 };
717 type_map::build_type_with_children(
718 cx,
719 type_map::stub(
720 cx,
721 Stub::Struct,
722 UniqueTypeId::for_ty(cx.tcx, float_ty),
723 "f16",
724 def_location,
725 cx.size_and_align_of(float_ty),
726 NO_SCOPE_METADATA,
727 DIFlags::FlagZero,
728 ),
729 |cx, float_di_node| {
731 let def_id = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
732 match bits_ty.kind() {
733 ty::Adt(def, _) => Some(def.did()),
734 _ => None,
735 }
736 } else {
737 None
738 };
739 smallvec![build_field_di_node(
740 cx,
741 float_di_node,
742 "bits",
743 cx.size_and_align_of(bits_ty),
744 Size::ZERO,
745 DIFlags::FlagZero,
746 type_di_node(cx, bits_ty),
747 def_id,
748 )]
749 },
750 NO_GENERICS,
751 )
752}
753
754fn build_basic_type_di_node<'ll, 'tcx>(
755 cx: &CodegenCx<'ll, 'tcx>,
756 t: Ty<'tcx>,
757) -> DINodeCreationResult<'ll> {
758 debug!("build_basic_type_di_node: {:?}", t);
759
760 let cpp_like_debuginfo = cpp_like_debuginfo(cx.tcx);
763
764 use dwarf_const::{DW_ATE_UTF, DW_ATE_boolean, DW_ATE_float, DW_ATE_signed, DW_ATE_unsigned};
765
766 let (name, encoding) = match t.kind() {
767 ty::Never => ("!", DW_ATE_unsigned),
768 ty::Tuple(elements) if elements.is_empty() => {
769 if cpp_like_debuginfo {
770 return build_tuple_type_di_node(cx, UniqueTypeId::for_ty(cx.tcx, t));
771 } else {
772 ("()", DW_ATE_unsigned)
773 }
774 }
775 ty::Bool => ("bool", DW_ATE_boolean),
776 ty::Char => ("char", DW_ATE_UTF),
777 ty::Int(int_ty) if cpp_like_debuginfo => (int_ty.msvc_basic_name(), DW_ATE_signed),
778 ty::Uint(uint_ty) if cpp_like_debuginfo => (uint_ty.msvc_basic_name(), DW_ATE_unsigned),
779 ty::Float(ty::FloatTy::F16) if cpp_like_debuginfo => {
780 return build_cpp_f16_di_node(cx);
781 }
782 ty::Float(float_ty) if cpp_like_debuginfo => (float_ty.msvc_basic_name(), DW_ATE_float),
783 ty::Int(int_ty) => (int_ty.name_str(), DW_ATE_signed),
784 ty::Uint(uint_ty) => (uint_ty.name_str(), DW_ATE_unsigned),
785 ty::Float(float_ty) => (float_ty.name_str(), DW_ATE_float),
786 _ => bug!("debuginfo::build_basic_type_di_node - `t` is invalid type"),
787 };
788
789 let ty_di_node = unsafe {
790 llvm::LLVMRustDIBuilderCreateBasicType(
791 DIB(cx),
792 name.as_c_char_ptr(),
793 name.len(),
794 cx.size_of(t).bits(),
795 encoding,
796 )
797 };
798
799 if !cpp_like_debuginfo {
800 return DINodeCreationResult::new(ty_di_node, false);
801 }
802
803 let typedef_name = match t.kind() {
804 ty::Int(int_ty) => int_ty.name_str(),
805 ty::Uint(uint_ty) => uint_ty.name_str(),
806 ty::Float(float_ty) => float_ty.name_str(),
807 _ => return DINodeCreationResult::new(ty_di_node, false),
808 };
809
810 let typedef_di_node = unsafe {
811 llvm::LLVMRustDIBuilderCreateTypedef(
812 DIB(cx),
813 ty_di_node,
814 typedef_name.as_c_char_ptr(),
815 typedef_name.len(),
816 unknown_file_metadata(cx),
817 0,
818 None,
819 )
820 };
821
822 DINodeCreationResult::new(typedef_di_node, false)
823}
824
825fn build_foreign_type_di_node<'ll, 'tcx>(
826 cx: &CodegenCx<'ll, 'tcx>,
827 t: Ty<'tcx>,
828 unique_type_id: UniqueTypeId<'tcx>,
829) -> DINodeCreationResult<'ll> {
830 debug!("build_foreign_type_di_node: {:?}", t);
831
832 let &ty::Foreign(def_id) = unique_type_id.expect_ty().kind() else {
833 bug!(
834 "build_foreign_type_di_node() called with unexpected type: {:?}",
835 unique_type_id.expect_ty()
836 );
837 };
838
839 build_type_with_children(
840 cx,
841 type_map::stub(
842 cx,
843 Stub::Struct,
844 unique_type_id,
845 &compute_debuginfo_type_name(cx.tcx, t, false),
846 None,
847 cx.size_and_align_of(t),
848 Some(get_namespace_for_item(cx, def_id)),
849 DIFlags::FlagZero,
850 ),
851 |_, _| smallvec![],
852 NO_GENERICS,
853 )
854}
855
856pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
857 tcx: TyCtxt<'tcx>,
858 codegen_unit_name: &str,
859 debug_context: &CodegenUnitDebugContext<'ll, 'tcx>,
860) -> &'ll DIDescriptor {
861 use rustc_session::RemapFileNameExt;
862 use rustc_session::config::RemapPathScopeComponents;
863 let mut name_in_debuginfo = tcx
864 .sess
865 .local_crate_source_file()
866 .map(|src| src.for_scope(&tcx.sess, RemapPathScopeComponents::DEBUGINFO).to_path_buf())
867 .unwrap_or_else(|| PathBuf::from(tcx.crate_name(LOCAL_CRATE).as_str()));
868
869 name_in_debuginfo.push("@");
887 name_in_debuginfo.push(codegen_unit_name);
888
889 debug!("build_compile_unit_di_node: {:?}", name_in_debuginfo);
890 let rustc_producer = format!("rustc version {}", tcx.sess.cfg_version);
891 let producer = format!("clang LLVM ({rustc_producer})");
893
894 let name_in_debuginfo = name_in_debuginfo.to_string_lossy();
895 let work_dir = tcx
896 .sess
897 .opts
898 .working_dir
899 .for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO)
900 .to_string_lossy();
901 let output_filenames = tcx.output_filenames(());
902 let split_name = if tcx.sess.target_can_use_split_dwarf()
903 && let Some(f) = output_filenames.split_dwarf_path(
904 tcx.sess.split_debuginfo(),
905 tcx.sess.opts.unstable_opts.split_dwarf_kind,
906 Some(codegen_unit_name),
907 ) {
908 Some(tcx.sess.source_map().path_mapping().to_real_filename(f))
910 } else {
911 None
912 };
913 let split_name = split_name
914 .as_ref()
915 .map(|f| f.for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO).to_string_lossy())
916 .unwrap_or_default();
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 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 = llvm::LLVMRustDIBuilderCreateFile(
931 debug_context.builder.as_ref(),
932 name_in_debuginfo.as_c_char_ptr(),
933 name_in_debuginfo.len(),
934 work_dir.as_c_char_ptr(),
935 work_dir.len(),
936 llvm::ChecksumKind::None,
937 ptr::null(),
938 0,
939 ptr::null(),
940 0,
941 );
942
943 let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit(
944 debug_context.builder.as_ref(),
945 dwarf_const::DW_LANG_Rust,
946 compile_unit_file,
947 producer.as_c_char_ptr(),
948 producer.len(),
949 tcx.sess.opts.optimize != config::OptLevel::No,
950 c"".as_ptr(),
951 0,
952 split_name.as_c_char_ptr(),
956 split_name.len(),
957 kind,
958 0,
959 tcx.sess.opts.unstable_opts.split_dwarf_inlining,
960 debug_name_table_kind,
961 );
962
963 return unit_metadata;
964 };
965}
966
967fn build_field_di_node<'ll, 'tcx>(
969 cx: &CodegenCx<'ll, 'tcx>,
970 owner: &'ll DIScope,
971 name: &str,
972 size_and_align: (Size, Align),
973 offset: Size,
974 flags: DIFlags,
975 type_di_node: &'ll DIType,
976 def_id: Option<DefId>,
977) -> &'ll DIType {
978 let (file_metadata, line_number) = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers
979 {
980 file_metadata_from_def_id(cx, def_id)
981 } else {
982 (unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
983 };
984 unsafe {
985 llvm::LLVMRustDIBuilderCreateMemberType(
986 DIB(cx),
987 owner,
988 name.as_c_char_ptr(),
989 name.len(),
990 file_metadata,
991 line_number,
992 size_and_align.0.bits(),
993 size_and_align.1.bits() as u32,
994 offset.bits(),
995 flags,
996 type_di_node,
997 )
998 }
999}
1000
1001fn visibility_di_flags<'ll, 'tcx>(
1007 cx: &CodegenCx<'ll, 'tcx>,
1008 did: DefId,
1009 type_did: DefId,
1010) -> DIFlags {
1011 let parent_did = cx.tcx.parent(type_did);
1012 let visibility = cx.tcx.visibility(did);
1013 match visibility {
1014 Visibility::Public => DIFlags::FlagPublic,
1015 Visibility::Restricted(did) if did == parent_did => DIFlags::FlagPrivate,
1017 Visibility::Restricted(..) => DIFlags::FlagProtected,
1019 }
1020}
1021
1022fn build_struct_type_di_node<'ll, 'tcx>(
1024 cx: &CodegenCx<'ll, 'tcx>,
1025 unique_type_id: UniqueTypeId<'tcx>,
1026) -> DINodeCreationResult<'ll> {
1027 let struct_type = unique_type_id.expect_ty();
1028 let ty::Adt(adt_def, _) = struct_type.kind() else {
1029 bug!("build_struct_type_di_node() called with non-struct-type: {:?}", struct_type);
1030 };
1031 assert!(adt_def.is_struct());
1032 let containing_scope = get_namespace_for_item(cx, adt_def.did());
1033 let struct_type_and_layout = cx.layout_of(struct_type);
1034 let variant_def = adt_def.non_enum_variant();
1035 let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1036 Some(file_metadata_from_def_id(cx, Some(adt_def.did())))
1037 } else {
1038 None
1039 };
1040
1041 type_map::build_type_with_children(
1042 cx,
1043 type_map::stub(
1044 cx,
1045 Stub::Struct,
1046 unique_type_id,
1047 &compute_debuginfo_type_name(cx.tcx, struct_type, false),
1048 def_location,
1049 size_and_align_of(struct_type_and_layout),
1050 Some(containing_scope),
1051 visibility_di_flags(cx, adt_def.did(), adt_def.did()),
1052 ),
1053 |cx, owner| {
1055 variant_def
1056 .fields
1057 .iter()
1058 .enumerate()
1059 .map(|(i, f)| {
1060 let field_name = if variant_def.ctor_kind() == Some(CtorKind::Fn) {
1061 tuple_field_name(i)
1063 } else {
1064 Cow::Borrowed(f.name.as_str())
1066 };
1067 let field_layout = struct_type_and_layout.field(cx, i);
1068 let def_id = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1069 Some(f.did)
1070 } else {
1071 None
1072 };
1073 build_field_di_node(
1074 cx,
1075 owner,
1076 &field_name[..],
1077 (field_layout.size, field_layout.align.abi),
1078 struct_type_and_layout.fields.offset(i),
1079 visibility_di_flags(cx, f.did, adt_def.did()),
1080 type_di_node(cx, field_layout.ty),
1081 def_id,
1082 )
1083 })
1084 .collect()
1085 },
1086 |cx| build_generic_type_param_di_nodes(cx, struct_type),
1087 )
1088}
1089
1090fn build_upvar_field_di_nodes<'ll, 'tcx>(
1097 cx: &CodegenCx<'ll, 'tcx>,
1098 closure_or_coroutine_ty: Ty<'tcx>,
1099 closure_or_coroutine_di_node: &'ll DIType,
1100) -> SmallVec<&'ll DIType> {
1101 let (&def_id, up_var_tys) = match closure_or_coroutine_ty.kind() {
1102 ty::Coroutine(def_id, args) => (def_id, args.as_coroutine().prefix_tys()),
1103 ty::Closure(def_id, args) => (def_id, args.as_closure().upvar_tys()),
1104 ty::CoroutineClosure(def_id, args) => (def_id, args.as_coroutine_closure().upvar_tys()),
1105 _ => {
1106 bug!(
1107 "build_upvar_field_di_nodes() called with non-closure-or-coroutine-type: {:?}",
1108 closure_or_coroutine_ty
1109 )
1110 }
1111 };
1112
1113 assert!(up_var_tys.iter().all(|t| t == cx.tcx.normalize_erasing_regions(cx.typing_env(), t)));
1114
1115 let capture_names = cx.tcx.closure_saved_names_of_captured_variables(def_id);
1116 let layout = cx.layout_of(closure_or_coroutine_ty);
1117
1118 up_var_tys
1119 .into_iter()
1120 .zip(capture_names.iter())
1121 .enumerate()
1122 .map(|(index, (up_var_ty, capture_name))| {
1123 build_field_di_node(
1124 cx,
1125 closure_or_coroutine_di_node,
1126 capture_name.as_str(),
1127 cx.size_and_align_of(up_var_ty),
1128 layout.fields.offset(index),
1129 DIFlags::FlagZero,
1130 type_di_node(cx, up_var_ty),
1131 None,
1132 )
1133 })
1134 .collect()
1135}
1136
1137fn build_tuple_type_di_node<'ll, 'tcx>(
1139 cx: &CodegenCx<'ll, 'tcx>,
1140 unique_type_id: UniqueTypeId<'tcx>,
1141) -> DINodeCreationResult<'ll> {
1142 let tuple_type = unique_type_id.expect_ty();
1143 let &ty::Tuple(component_types) = tuple_type.kind() else {
1144 bug!("build_tuple_type_di_node() called with non-tuple-type: {:?}", tuple_type)
1145 };
1146
1147 let tuple_type_and_layout = cx.layout_of(tuple_type);
1148 let type_name = compute_debuginfo_type_name(cx.tcx, tuple_type, false);
1149
1150 type_map::build_type_with_children(
1151 cx,
1152 type_map::stub(
1153 cx,
1154 Stub::Struct,
1155 unique_type_id,
1156 &type_name,
1157 None,
1158 size_and_align_of(tuple_type_and_layout),
1159 NO_SCOPE_METADATA,
1160 DIFlags::FlagZero,
1161 ),
1162 |cx, tuple_di_node| {
1164 component_types
1165 .into_iter()
1166 .enumerate()
1167 .map(|(index, component_type)| {
1168 build_field_di_node(
1169 cx,
1170 tuple_di_node,
1171 &tuple_field_name(index),
1172 cx.size_and_align_of(component_type),
1173 tuple_type_and_layout.fields.offset(index),
1174 DIFlags::FlagZero,
1175 type_di_node(cx, component_type),
1176 None,
1177 )
1178 })
1179 .collect()
1180 },
1181 NO_GENERICS,
1182 )
1183}
1184
1185fn build_closure_env_di_node<'ll, 'tcx>(
1187 cx: &CodegenCx<'ll, 'tcx>,
1188 unique_type_id: UniqueTypeId<'tcx>,
1189) -> DINodeCreationResult<'ll> {
1190 let closure_env_type = unique_type_id.expect_ty();
1191 let &(ty::Closure(def_id, _) | ty::CoroutineClosure(def_id, _)) = closure_env_type.kind()
1192 else {
1193 bug!("build_closure_env_di_node() called with non-closure-type: {:?}", closure_env_type)
1194 };
1195 let containing_scope = get_namespace_for_item(cx, def_id);
1196 let type_name = compute_debuginfo_type_name(cx.tcx, closure_env_type, false);
1197
1198 let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1199 Some(file_metadata_from_def_id(cx, Some(def_id)))
1200 } else {
1201 None
1202 };
1203
1204 type_map::build_type_with_children(
1205 cx,
1206 type_map::stub(
1207 cx,
1208 Stub::Struct,
1209 unique_type_id,
1210 &type_name,
1211 def_location,
1212 cx.size_and_align_of(closure_env_type),
1213 Some(containing_scope),
1214 DIFlags::FlagZero,
1215 ),
1216 |cx, owner| build_upvar_field_di_nodes(cx, closure_env_type, owner),
1218 NO_GENERICS,
1219 )
1220}
1221
1222fn build_union_type_di_node<'ll, 'tcx>(
1224 cx: &CodegenCx<'ll, 'tcx>,
1225 unique_type_id: UniqueTypeId<'tcx>,
1226) -> DINodeCreationResult<'ll> {
1227 let union_type = unique_type_id.expect_ty();
1228 let (union_def_id, variant_def) = match union_type.kind() {
1229 ty::Adt(def, _) => (def.did(), def.non_enum_variant()),
1230 _ => bug!("build_union_type_di_node on a non-ADT"),
1231 };
1232 let containing_scope = get_namespace_for_item(cx, union_def_id);
1233 let union_ty_and_layout = cx.layout_of(union_type);
1234 let type_name = compute_debuginfo_type_name(cx.tcx, union_type, false);
1235 let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1236 Some(file_metadata_from_def_id(cx, Some(union_def_id)))
1237 } else {
1238 None
1239 };
1240
1241 type_map::build_type_with_children(
1242 cx,
1243 type_map::stub(
1244 cx,
1245 Stub::Union,
1246 unique_type_id,
1247 &type_name,
1248 def_location,
1249 size_and_align_of(union_ty_and_layout),
1250 Some(containing_scope),
1251 DIFlags::FlagZero,
1252 ),
1253 |cx, owner| {
1255 variant_def
1256 .fields
1257 .iter()
1258 .enumerate()
1259 .map(|(i, f)| {
1260 let field_layout = union_ty_and_layout.field(cx, i);
1261 let def_id = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1262 Some(f.did)
1263 } else {
1264 None
1265 };
1266 build_field_di_node(
1267 cx,
1268 owner,
1269 f.name.as_str(),
1270 size_and_align_of(field_layout),
1271 Size::ZERO,
1272 DIFlags::FlagZero,
1273 type_di_node(cx, field_layout.ty),
1274 def_id,
1275 )
1276 })
1277 .collect()
1278 },
1279 |cx| build_generic_type_param_di_nodes(cx, union_type),
1281 )
1282}
1283
1284fn build_generic_type_param_di_nodes<'ll, 'tcx>(
1286 cx: &CodegenCx<'ll, 'tcx>,
1287 ty: Ty<'tcx>,
1288) -> SmallVec<&'ll DIType> {
1289 if let ty::Adt(def, args) = *ty.kind() {
1290 if args.types().next().is_some() {
1291 let generics = cx.tcx.generics_of(def.did());
1292 let names = get_parameter_names(cx, generics);
1293 let template_params: SmallVec<_> = iter::zip(args, names)
1294 .filter_map(|(kind, name)| {
1295 kind.as_type().map(|ty| {
1296 let actual_type = cx.tcx.normalize_erasing_regions(cx.typing_env(), ty);
1297 let actual_type_di_node = type_di_node(cx, actual_type);
1298 let name = name.as_str();
1299 unsafe {
1300 llvm::LLVMRustDIBuilderCreateTemplateTypeParameter(
1301 DIB(cx),
1302 None,
1303 name.as_c_char_ptr(),
1304 name.len(),
1305 actual_type_di_node,
1306 )
1307 }
1308 })
1309 })
1310 .collect();
1311
1312 return template_params;
1313 }
1314 }
1315
1316 return smallvec![];
1317
1318 fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec<Symbol> {
1319 let mut names = generics
1320 .parent
1321 .map_or_else(Vec::new, |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id)));
1322 names.extend(generics.own_params.iter().map(|param| param.name));
1323 names
1324 }
1325}
1326
1327pub(crate) fn build_global_var_di_node<'ll>(
1331 cx: &CodegenCx<'ll, '_>,
1332 def_id: DefId,
1333 global: &'ll Value,
1334) {
1335 if cx.dbg_cx.is_none() {
1336 return;
1337 }
1338
1339 if cx.sess().opts.debuginfo != DebugInfo::Full {
1341 return;
1342 }
1343
1344 let tcx = cx.tcx;
1345
1346 let var_scope = get_namespace_for_item(cx, def_id);
1349 let (file_metadata, line_number) = file_metadata_from_def_id(cx, Some(def_id));
1350
1351 let is_local_to_unit = is_node_local_to_unit(cx, def_id);
1352
1353 let DefKind::Static { nested, .. } = cx.tcx.def_kind(def_id) else { bug!() };
1354 if nested {
1355 return;
1356 }
1357 let variable_type = Instance::mono(cx.tcx, def_id).ty(cx.tcx, cx.typing_env());
1358 let type_di_node = type_di_node(cx, variable_type);
1359 let var_name = tcx.item_name(def_id);
1360 let var_name = var_name.as_str();
1361 let linkage_name = mangled_name_of_instance(cx, Instance::mono(tcx, def_id)).name;
1362 let linkage_name = if var_name == linkage_name { "" } else { linkage_name };
1365
1366 let global_align = cx.align_of(variable_type);
1367
1368 unsafe {
1369 llvm::LLVMRustDIBuilderCreateStaticVariable(
1370 DIB(cx),
1371 Some(var_scope),
1372 var_name.as_c_char_ptr(),
1373 var_name.len(),
1374 linkage_name.as_c_char_ptr(),
1375 linkage_name.len(),
1376 file_metadata,
1377 line_number,
1378 type_di_node,
1379 is_local_to_unit,
1380 global,
1381 None,
1382 global_align.bits() as u32,
1383 );
1384 }
1385}
1386
1387fn build_vtable_type_di_node<'ll, 'tcx>(
1397 cx: &CodegenCx<'ll, 'tcx>,
1398 ty: Ty<'tcx>,
1399 poly_trait_ref: Option<ty::ExistentialTraitRef<'tcx>>,
1400) -> &'ll DIType {
1401 let tcx = cx.tcx;
1402
1403 let vtable_entries = if let Some(poly_trait_ref) = poly_trait_ref {
1404 let trait_ref = poly_trait_ref.with_self_ty(tcx, ty);
1405 let trait_ref = tcx.erase_regions(trait_ref);
1406
1407 tcx.vtable_entries(trait_ref)
1408 } else {
1409 TyCtxt::COMMON_VTABLE_ENTRIES
1410 };
1411
1412 let void_pointer_ty = Ty::new_imm_ptr(tcx, tcx.types.unit);
1415 let void_pointer_type_di_node = type_di_node(cx, void_pointer_ty);
1416 let usize_di_node = type_di_node(cx, tcx.types.usize);
1417 let (pointer_size, pointer_align) = cx.size_and_align_of(void_pointer_ty);
1418 assert_eq!(cx.size_and_align_of(tcx.types.usize), (pointer_size, pointer_align));
1422
1423 let vtable_type_name =
1424 compute_debuginfo_vtable_name(cx.tcx, ty, poly_trait_ref, VTableNameKind::Type);
1425 let unique_type_id = UniqueTypeId::for_vtable_ty(tcx, ty, poly_trait_ref);
1426 let size = pointer_size * vtable_entries.len() as u64;
1427
1428 let vtable_holder = type_di_node(cx, ty);
1431
1432 build_type_with_children(
1433 cx,
1434 type_map::stub(
1435 cx,
1436 Stub::VTableTy { vtable_holder },
1437 unique_type_id,
1438 &vtable_type_name,
1439 None,
1440 (size, pointer_align),
1441 NO_SCOPE_METADATA,
1442 DIFlags::FlagArtificial,
1443 ),
1444 |cx, vtable_type_di_node| {
1445 vtable_entries
1446 .iter()
1447 .enumerate()
1448 .filter_map(|(index, vtable_entry)| {
1449 let (field_name, field_type_di_node) = match vtable_entry {
1450 ty::VtblEntry::MetadataDropInPlace => {
1451 ("drop_in_place".to_string(), void_pointer_type_di_node)
1452 }
1453 ty::VtblEntry::Method(_) => {
1454 (format!("__method{index}"), void_pointer_type_di_node)
1458 }
1459 ty::VtblEntry::TraitVPtr(_) => {
1460 (format!("__super_trait_ptr{index}"), void_pointer_type_di_node)
1461 }
1462 ty::VtblEntry::MetadataAlign => ("align".to_string(), usize_di_node),
1463 ty::VtblEntry::MetadataSize => ("size".to_string(), usize_di_node),
1464 ty::VtblEntry::Vacant => return None,
1465 };
1466
1467 let field_offset = pointer_size * index as u64;
1468
1469 Some(build_field_di_node(
1470 cx,
1471 vtable_type_di_node,
1472 &field_name,
1473 (pointer_size, pointer_align),
1474 field_offset,
1475 DIFlags::FlagZero,
1476 field_type_di_node,
1477 None,
1478 ))
1479 })
1480 .collect()
1481 },
1482 NO_GENERICS,
1483 )
1484 .di_node
1485}
1486
1487fn find_vtable_behind_cast<'ll>(vtable: &'ll Value) -> &'ll Value {
1496 unsafe {
1498 if let Some(c) = llvm::LLVMIsAConstantExpr(vtable) {
1499 if llvm::LLVMGetConstOpcode(c) == llvm::Opcode::AddrSpaceCast {
1500 return llvm::LLVMGetOperand(c, 0).unwrap();
1501 }
1502 }
1503 }
1504 vtable
1505}
1506
1507pub(crate) fn apply_vcall_visibility_metadata<'ll, 'tcx>(
1508 cx: &CodegenCx<'ll, 'tcx>,
1509 ty: Ty<'tcx>,
1510 trait_ref: Option<ExistentialTraitRef<'tcx>>,
1511 vtable: &'ll Value,
1512) {
1513 if !cx.sess().opts.unstable_opts.virtual_function_elimination || cx.sess().lto() != Lto::Fat {
1516 return;
1517 }
1518
1519 enum VCallVisibility {
1520 Public = 0,
1521 LinkageUnit = 1,
1522 TranslationUnit = 2,
1523 }
1524
1525 let Some(trait_ref) = trait_ref else { return };
1526
1527 let vtable = find_vtable_behind_cast(vtable);
1529 let trait_ref_self = trait_ref.with_self_ty(cx.tcx, ty);
1530 let trait_ref_self = cx.tcx.erase_regions(trait_ref_self);
1531 let trait_def_id = trait_ref_self.def_id;
1532 let trait_vis = cx.tcx.visibility(trait_def_id);
1533
1534 let cgus = cx.sess().codegen_units().as_usize();
1535 let single_cgu = cgus == 1;
1536
1537 let lto = cx.sess().lto();
1538
1539 let vcall_visibility = match (lto, trait_vis, single_cgu) {
1542 (Lto::No | Lto::ThinLocal, Visibility::Public, _)
1545 | (Lto::No, Visibility::Restricted(_), false) => VCallVisibility::Public,
1546 (Lto::Fat | Lto::Thin, Visibility::Public, _)
1551 | (Lto::ThinLocal | Lto::Thin | Lto::Fat, Visibility::Restricted(_), false) => {
1552 VCallVisibility::LinkageUnit
1553 }
1554 (_, Visibility::Restricted(_), true) => VCallVisibility::TranslationUnit,
1557 };
1558
1559 let trait_ref_typeid = typeid_for_trait_ref(cx.tcx, trait_ref);
1560
1561 unsafe {
1562 let typeid = llvm::LLVMMDStringInContext2(
1563 cx.llcx,
1564 trait_ref_typeid.as_ptr() as *const c_char,
1565 trait_ref_typeid.as_bytes().len(),
1566 );
1567 let v = [llvm::LLVMValueAsMetadata(cx.const_usize(0)), typeid];
1568 llvm::LLVMRustGlobalAddMetadata(
1569 vtable,
1570 llvm::MD_type as c_uint,
1571 llvm::LLVMMDNodeInContext2(cx.llcx, v.as_ptr(), v.len()),
1572 );
1573 let vcall_visibility = llvm::LLVMValueAsMetadata(cx.const_u64(vcall_visibility as u64));
1574 let vcall_visibility_metadata = llvm::LLVMMDNodeInContext2(cx.llcx, &vcall_visibility, 1);
1575 llvm::LLVMGlobalSetMetadata(
1576 vtable,
1577 llvm::MetadataType::MD_vcall_visibility as c_uint,
1578 vcall_visibility_metadata,
1579 );
1580 }
1581}
1582
1583pub(crate) fn create_vtable_di_node<'ll, 'tcx>(
1588 cx: &CodegenCx<'ll, 'tcx>,
1589 ty: Ty<'tcx>,
1590 poly_trait_ref: Option<ty::ExistentialTraitRef<'tcx>>,
1591 vtable: &'ll Value,
1592) {
1593 if cx.dbg_cx.is_none() {
1594 return;
1595 }
1596
1597 if cx.sess().opts.debuginfo != DebugInfo::Full {
1599 return;
1600 }
1601
1602 let vtable = find_vtable_behind_cast(vtable);
1604
1605 llvm::SetUnnamedAddress(vtable, llvm::UnnamedAddr::No);
1609
1610 let vtable_name =
1611 compute_debuginfo_vtable_name(cx.tcx, ty, poly_trait_ref, VTableNameKind::GlobalVariable);
1612 let vtable_type_di_node = build_vtable_type_di_node(cx, ty, poly_trait_ref);
1613 let linkage_name = "";
1614
1615 unsafe {
1616 llvm::LLVMRustDIBuilderCreateStaticVariable(
1617 DIB(cx),
1618 NO_SCOPE_METADATA,
1619 vtable_name.as_c_char_ptr(),
1620 vtable_name.len(),
1621 linkage_name.as_c_char_ptr(),
1622 linkage_name.len(),
1623 unknown_file_metadata(cx),
1624 UNKNOWN_LINE_NUMBER,
1625 vtable_type_di_node,
1626 true,
1627 vtable,
1628 None,
1629 0,
1630 );
1631 }
1632}
1633
1634pub(crate) fn extend_scope_to_file<'ll>(
1636 cx: &CodegenCx<'ll, '_>,
1637 scope_metadata: &'ll DIScope,
1638 file: &SourceFile,
1639) -> &'ll DILexicalBlock {
1640 let file_metadata = file_metadata(cx, file);
1641 unsafe {
1642 llvm::LLVMDIBuilderCreateLexicalBlockFile(
1643 DIB(cx),
1644 scope_metadata,
1645 file_metadata,
1646 0u32,
1647 )
1648 }
1649}
1650
1651fn tuple_field_name(field_index: usize) -> Cow<'static, str> {
1652 const TUPLE_FIELD_NAMES: [&'static str; 16] = [
1653 "__0", "__1", "__2", "__3", "__4", "__5", "__6", "__7", "__8", "__9", "__10", "__11",
1654 "__12", "__13", "__14", "__15",
1655 ];
1656 TUPLE_FIELD_NAMES
1657 .get(field_index)
1658 .map(|s| Cow::from(*s))
1659 .unwrap_or_else(|| Cow::from(format!("__{field_index}")))
1660}
1661
1662pub(crate) type DefinitionLocation<'ll> = (&'ll DIFile, c_uint);
1663
1664pub(crate) fn file_metadata_from_def_id<'ll>(
1665 cx: &CodegenCx<'ll, '_>,
1666 def_id: Option<DefId>,
1667) -> DefinitionLocation<'ll> {
1668 if let Some(def_id) = def_id
1669 && let span = hygiene::walk_chain_collapsed(cx.tcx.def_span(def_id), DUMMY_SP)
1670 && !span.is_dummy()
1671 {
1672 let loc = cx.lookup_debug_loc(span.lo());
1673 (file_metadata(cx, &loc.file), loc.line)
1674 } else {
1675 (unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
1676 }
1677}