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_char, 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::{DUMMY_SP, FileName, FileNameDisplayPreference, SourceFile, Symbol, hygiene};
23use rustc_symbol_mangling::typeid_for_trait_ref;
24use rustc_target::spec::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::{
34 DIB, create_DIArray, debug_context, get_namespace_for_item, is_node_local_to_unit,
35};
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) -> DINodeCreationResult<'ll> {
105 let ty::Array(element_type, len) = array_type.kind() else {
106 bug!("build_fixed_size_array_di_node() called with non-ty::Array type `{:?}`", array_type)
107 };
108
109 let element_type_di_node = type_di_node(cx, *element_type);
110
111 return_if_di_node_created_in_meantime!(cx, unique_type_id);
112
113 let (size, align) = cx.size_and_align_of(array_type);
114
115 let upper_bound = len
116 .try_to_target_usize(cx.tcx)
117 .expect("expected monomorphic const in codegen") as c_longlong;
118
119 let subrange =
120 unsafe { Some(llvm::LLVMRustDIBuilderGetOrCreateSubrange(DIB(cx), 0, upper_bound)) };
121
122 let subscripts = create_DIArray(DIB(cx), &[subrange]);
123 let di_node = unsafe {
124 llvm::LLVMRustDIBuilderCreateArrayType(
125 DIB(cx),
126 size.bits(),
127 align.bits() as u32,
128 element_type_di_node,
129 subscripts,
130 )
131 };
132
133 DINodeCreationResult::new(di_node, false)
134}
135
136fn build_pointer_or_reference_di_node<'ll, 'tcx>(
145 cx: &CodegenCx<'ll, 'tcx>,
146 ptr_type: Ty<'tcx>,
147 pointee_type: Ty<'tcx>,
148 unique_type_id: UniqueTypeId<'tcx>,
149) -> DINodeCreationResult<'ll> {
150 assert_eq!(
153 cx.size_and_align_of(ptr_type),
154 cx.size_and_align_of(Ty::new_mut_ptr(cx.tcx, pointee_type))
155 );
156
157 let pointee_type_di_node = type_di_node(cx, pointee_type);
158
159 return_if_di_node_created_in_meantime!(cx, unique_type_id);
160
161 let data_layout = &cx.tcx.data_layout;
162 let pointer_size = data_layout.pointer_size();
163 let pointer_align = data_layout.pointer_align();
164 let ptr_type_debuginfo_name = compute_debuginfo_type_name(cx.tcx, ptr_type, true);
165
166 match wide_pointer_kind(cx, pointee_type) {
167 None => {
168 assert_eq!(
170 (pointer_size, pointer_align.abi),
171 cx.size_and_align_of(ptr_type),
172 "ptr_type={ptr_type}, pointee_type={pointee_type}",
173 );
174
175 let di_node = unsafe {
176 llvm::LLVMRustDIBuilderCreatePointerType(
177 DIB(cx),
178 pointee_type_di_node,
179 pointer_size.bits(),
180 pointer_align.abi.bits() as u32,
181 0, ptr_type_debuginfo_name.as_c_char_ptr(),
183 ptr_type_debuginfo_name.len(),
184 )
185 };
186
187 DINodeCreationResult { di_node, already_stored_in_typemap: false }
188 }
189 Some(wide_pointer_kind) => {
190 type_map::build_type_with_children(
191 cx,
192 type_map::stub(
193 cx,
194 Stub::Struct,
195 unique_type_id,
196 &ptr_type_debuginfo_name,
197 None,
198 cx.size_and_align_of(ptr_type),
199 NO_SCOPE_METADATA,
200 DIFlags::FlagZero,
201 ),
202 |cx, owner| {
203 let layout_type = if ptr_type.is_box() {
211 Ty::new_mut_ptr(cx.tcx, pointee_type)
215 } else {
216 ptr_type
217 };
218
219 let layout = cx.layout_of(layout_type);
220 let addr_field = layout.field(cx, WIDE_PTR_ADDR);
221 let extra_field = layout.field(cx, WIDE_PTR_EXTRA);
222
223 let (addr_field_name, extra_field_name) = match wide_pointer_kind {
224 WidePtrKind::Dyn => ("pointer", "vtable"),
225 WidePtrKind::Slice => ("data_ptr", "length"),
226 };
227
228 assert_eq!(WIDE_PTR_ADDR, 0);
229 assert_eq!(WIDE_PTR_EXTRA, 1);
230
231 let data_ptr_type_di_node = unsafe {
234 llvm::LLVMRustDIBuilderCreatePointerType(
235 DIB(cx),
236 pointee_type_di_node,
237 addr_field.size.bits(),
238 addr_field.align.abi.bits() as u32,
239 0, std::ptr::null(),
241 0,
242 )
243 };
244
245 smallvec![
246 build_field_di_node(
247 cx,
248 owner,
249 addr_field_name,
250 addr_field,
251 layout.fields.offset(WIDE_PTR_ADDR),
252 DIFlags::FlagZero,
253 data_ptr_type_di_node,
254 None,
255 ),
256 build_field_di_node(
257 cx,
258 owner,
259 extra_field_name,
260 extra_field,
261 layout.fields.offset(WIDE_PTR_EXTRA),
262 DIFlags::FlagZero,
263 type_di_node(cx, extra_field.ty),
264 None,
265 ),
266 ]
267 },
268 NO_GENERICS,
269 )
270 }
271 }
272}
273
274fn build_subroutine_type_di_node<'ll, 'tcx>(
275 cx: &CodegenCx<'ll, 'tcx>,
276 unique_type_id: UniqueTypeId<'tcx>,
277) -> DINodeCreationResult<'ll> {
278 debug_context(cx)
291 .type_map
292 .unique_id_to_di_node
293 .borrow_mut()
294 .insert(unique_type_id, recursion_marker_type_di_node(cx));
295
296 let fn_ty = unique_type_id.expect_ty();
297 let signature =
298 cx.tcx.normalize_erasing_late_bound_regions(cx.typing_env(), fn_ty.fn_sig(cx.tcx));
299
300 let signature_di_nodes: SmallVec<_> = iter::once(
301 match signature.output().kind() {
303 ty::Tuple(tys) if tys.is_empty() => {
304 None
306 }
307 _ => Some(type_di_node(cx, signature.output())),
308 },
309 )
310 .chain(
311 signature.inputs().iter().map(|&argument_type| Some(type_di_node(cx, argument_type))),
313 )
314 .collect();
315
316 debug_context(cx).type_map.unique_id_to_di_node.borrow_mut().remove(&unique_type_id);
317
318 let fn_di_node = create_subroutine_type(cx, create_DIArray(DIB(cx), &signature_di_nodes[..]));
319
320 let name = compute_debuginfo_type_name(cx.tcx, fn_ty, false);
322 let (size, align) = match fn_ty.kind() {
323 ty::FnDef(..) => (Size::ZERO, Align::ONE),
324 ty::FnPtr(..) => {
325 (cx.tcx.data_layout.pointer_size(), cx.tcx.data_layout.pointer_align().abi)
326 }
327 _ => unreachable!(),
328 };
329 let di_node = unsafe {
330 llvm::LLVMRustDIBuilderCreatePointerType(
331 DIB(cx),
332 fn_di_node,
333 size.bits(),
334 align.bits() as u32,
335 0, name.as_c_char_ptr(),
337 name.len(),
338 )
339 };
340
341 DINodeCreationResult::new(di_node, false)
342}
343
344pub(super) fn create_subroutine_type<'ll>(
345 cx: &CodegenCx<'ll, '_>,
346 signature: &'ll DICompositeType,
347) -> &'ll DICompositeType {
348 unsafe { llvm::LLVMRustDIBuilderCreateSubroutineType(DIB(cx), signature) }
349}
350
351fn build_dyn_type_di_node<'ll, 'tcx>(
354 cx: &CodegenCx<'ll, 'tcx>,
355 dyn_type: Ty<'tcx>,
356 unique_type_id: UniqueTypeId<'tcx>,
357) -> DINodeCreationResult<'ll> {
358 if let ty::Dynamic(..) = dyn_type.kind() {
359 let type_name = compute_debuginfo_type_name(cx.tcx, dyn_type, true);
360 type_map::build_type_with_children(
361 cx,
362 type_map::stub(
363 cx,
364 Stub::Struct,
365 unique_type_id,
366 &type_name,
367 None,
368 cx.size_and_align_of(dyn_type),
369 NO_SCOPE_METADATA,
370 DIFlags::FlagZero,
371 ),
372 |_, _| smallvec![],
373 NO_GENERICS,
374 )
375 } else {
376 bug!(
377 "Only ty::Dynamic is valid for build_dyn_type_di_node(). Found {:?} instead.",
378 dyn_type
379 )
380 }
381}
382
383fn build_slice_type_di_node<'ll, 'tcx>(
401 cx: &CodegenCx<'ll, 'tcx>,
402 slice_type: Ty<'tcx>,
403 unique_type_id: UniqueTypeId<'tcx>,
404) -> DINodeCreationResult<'ll> {
405 let element_type = match slice_type.kind() {
406 ty::Slice(element_type) => *element_type,
407 ty::Str => cx.tcx.types.u8,
408 _ => {
409 bug!(
410 "Only ty::Slice is valid for build_slice_type_di_node(). Found {:?} instead.",
411 slice_type
412 )
413 }
414 };
415
416 let element_type_di_node = type_di_node(cx, element_type);
417 return_if_di_node_created_in_meantime!(cx, unique_type_id);
418 DINodeCreationResult { di_node: element_type_di_node, already_stored_in_typemap: false }
419}
420
421pub(crate) fn type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) -> &'ll DIType {
426 let unique_type_id = UniqueTypeId::for_ty(cx.tcx, t);
427
428 if let Some(existing_di_node) = debug_context(cx).type_map.di_node_for_unique_id(unique_type_id)
429 {
430 return existing_di_node;
431 }
432
433 debug!("type_di_node: {:?} kind: {:?}", t, t.kind());
434
435 let DINodeCreationResult { di_node, already_stored_in_typemap } = match *t.kind() {
436 ty::Never | ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Float(_) => {
437 build_basic_type_di_node(cx, t)
438 }
439 ty::Tuple(elements) if elements.is_empty() => build_basic_type_di_node(cx, t),
440 ty::Array(..) => build_fixed_size_array_di_node(cx, unique_type_id, t),
441 ty::Slice(_) | ty::Str => build_slice_type_di_node(cx, t, unique_type_id),
442 ty::Dynamic(..) => build_dyn_type_di_node(cx, t, unique_type_id),
443 ty::Foreign(..) => build_foreign_type_di_node(cx, t, unique_type_id),
444 ty::RawPtr(pointee_type, _) | ty::Ref(_, pointee_type, _) => {
445 build_pointer_or_reference_di_node(cx, t, pointee_type, unique_type_id)
446 }
447 ty::Adt(def, args)
451 if def.is_box()
452 && args.get(1).is_none_or(|arg| cx.layout_of(arg.expect_ty()).is_1zst()) =>
453 {
454 build_pointer_or_reference_di_node(cx, t, t.expect_boxed_ty(), unique_type_id)
455 }
456 ty::FnDef(..) | ty::FnPtr(..) => build_subroutine_type_di_node(cx, unique_type_id),
457 ty::Closure(..) => build_closure_env_di_node(cx, unique_type_id),
458 ty::CoroutineClosure(..) => build_closure_env_di_node(cx, unique_type_id),
459 ty::Coroutine(..) => enums::build_coroutine_di_node(cx, unique_type_id),
460 ty::Adt(def, ..) => match def.adt_kind() {
461 AdtKind::Struct => build_struct_type_di_node(cx, unique_type_id),
462 AdtKind::Union => build_union_type_di_node(cx, unique_type_id),
463 AdtKind::Enum => enums::build_enum_type_di_node(cx, unique_type_id),
464 },
465 ty::Tuple(_) => build_tuple_type_di_node(cx, unique_type_id),
466 _ => bug!("debuginfo: unexpected type in type_di_node(): {:?}", t),
467 };
468
469 {
470 if already_stored_in_typemap {
471 let di_node_for_uid =
473 match debug_context(cx).type_map.di_node_for_unique_id(unique_type_id) {
474 Some(di_node) => di_node,
475 None => {
476 bug!(
477 "expected type debuginfo node for unique \
478 type ID '{:?}' to already be in \
479 the `debuginfo::TypeMap` but it \
480 was not.",
481 unique_type_id,
482 );
483 }
484 };
485
486 assert_eq!(di_node_for_uid as *const _, di_node as *const _);
487 } else {
488 debug_context(cx).type_map.insert(unique_type_id, di_node);
489 }
490 }
491
492 di_node
493}
494
495fn recursion_marker_type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) -> &'ll DIType {
497 *debug_context(cx).recursion_marker_type.get_or_init(move || {
498 create_basic_type(
509 cx,
510 "<recur_type>",
511 cx.tcx.data_layout.pointer_size(),
512 dwarf_const::DW_ATE_unsigned,
513 )
514 })
515}
516
517fn hex_encode(data: &[u8]) -> String {
518 let mut hex_string = String::with_capacity(data.len() * 2);
519 for byte in data.iter() {
520 write!(&mut hex_string, "{byte:02x}").unwrap();
521 }
522 hex_string
523}
524
525pub(crate) fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) -> &'ll DIFile {
526 let cache_key = Some((source_file.stable_id, source_file.src_hash));
527 return debug_context(cx)
528 .created_files
529 .borrow_mut()
530 .entry(cache_key)
531 .or_insert_with(|| alloc_new_file_metadata(cx, source_file));
532
533 #[instrument(skip(cx, source_file), level = "debug")]
534 fn alloc_new_file_metadata<'ll>(
535 cx: &CodegenCx<'ll, '_>,
536 source_file: &SourceFile,
537 ) -> &'ll DIFile {
538 debug!(?source_file.name);
539
540 let filename_display_preference =
541 cx.sess().filename_display_preference(RemapPathScopeComponents::DEBUGINFO);
542
543 use rustc_session::config::RemapPathScopeComponents;
544 let (directory, file_name) = match &source_file.name {
545 FileName::Real(filename) => {
546 let working_directory = &cx.sess().opts.working_dir;
547 debug!(?working_directory);
548
549 if filename_display_preference == FileNameDisplayPreference::Remapped {
550 let filename = cx
551 .sess()
552 .source_map()
553 .path_mapping()
554 .to_embeddable_absolute_path(filename.clone(), working_directory);
555
556 let abs_path = filename.remapped_path_if_available();
558 debug!(?abs_path);
559
560 if let Ok(rel_path) =
561 abs_path.strip_prefix(working_directory.remapped_path_if_available())
562 {
563 (
579 working_directory.to_string_lossy(FileNameDisplayPreference::Remapped),
580 rel_path.to_string_lossy().into_owned(),
581 )
582 } else {
583 ("".into(), abs_path.to_string_lossy().into_owned())
584 }
585 } else {
586 let working_directory = working_directory.local_path_if_available();
587 let filename = filename.local_path_if_available();
588
589 debug!(?working_directory, ?filename);
590
591 let abs_path: Cow<'_, Path> = if filename.is_absolute() {
592 filename.into()
593 } else {
594 let mut p = PathBuf::new();
595 p.push(working_directory);
596 p.push(filename);
597 p.into()
598 };
599
600 if let Ok(rel_path) = abs_path.strip_prefix(working_directory) {
601 (
602 working_directory.to_string_lossy(),
603 rel_path.to_string_lossy().into_owned(),
604 )
605 } else {
606 ("".into(), abs_path.to_string_lossy().into_owned())
607 }
608 }
609 }
610 other => {
611 debug!(?other);
612 ("".into(), other.display(filename_display_preference).to_string())
613 }
614 };
615
616 let hash_kind = match source_file.src_hash.kind {
617 rustc_span::SourceFileHashAlgorithm::Md5 => llvm::ChecksumKind::MD5,
618 rustc_span::SourceFileHashAlgorithm::Sha1 => llvm::ChecksumKind::SHA1,
619 rustc_span::SourceFileHashAlgorithm::Sha256 => llvm::ChecksumKind::SHA256,
620 rustc_span::SourceFileHashAlgorithm::Blake3 => llvm::ChecksumKind::None,
621 };
622 let hash_value = hex_encode(source_file.src_hash.hash_bytes());
623
624 let source =
625 cx.sess().opts.unstable_opts.embed_source.then_some(()).and(source_file.src.as_ref());
626
627 create_file(DIB(cx), &file_name, &directory, &hash_value, hash_kind, source)
628 }
629}
630
631fn unknown_file_metadata<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll DIFile {
632 debug_context(cx).created_files.borrow_mut().entry(None).or_insert_with(|| {
633 create_file(DIB(cx), "<unknown>", "", "", llvm::ChecksumKind::None, None)
634 })
635}
636
637fn create_file<'ll>(
638 builder: &DIBuilder<'ll>,
639 file_name: &str,
640 directory: &str,
641 hash_value: &str,
642 hash_kind: llvm::ChecksumKind,
643 source: Option<&Arc<String>>,
644) -> &'ll DIFile {
645 unsafe {
646 llvm::LLVMRustDIBuilderCreateFile(
647 builder,
648 file_name.as_c_char_ptr(),
649 file_name.len(),
650 directory.as_c_char_ptr(),
651 directory.len(),
652 hash_kind,
653 hash_value.as_c_char_ptr(),
654 hash_value.len(),
655 source.map_or(ptr::null(), |x| x.as_c_char_ptr()),
656 source.map_or(0, |x| x.len()),
657 )
658 }
659}
660
661trait MsvcBasicName {
662 fn msvc_basic_name(self) -> &'static str;
663}
664
665impl MsvcBasicName for ty::IntTy {
666 fn msvc_basic_name(self) -> &'static str {
667 match self {
668 ty::IntTy::Isize => "ptrdiff_t",
669 ty::IntTy::I8 => "__int8",
670 ty::IntTy::I16 => "__int16",
671 ty::IntTy::I32 => "__int32",
672 ty::IntTy::I64 => "__int64",
673 ty::IntTy::I128 => "__int128",
674 }
675 }
676}
677
678impl MsvcBasicName for ty::UintTy {
679 fn msvc_basic_name(self) -> &'static str {
680 match self {
681 ty::UintTy::Usize => "size_t",
682 ty::UintTy::U8 => "unsigned __int8",
683 ty::UintTy::U16 => "unsigned __int16",
684 ty::UintTy::U32 => "unsigned __int32",
685 ty::UintTy::U64 => "unsigned __int64",
686 ty::UintTy::U128 => "unsigned __int128",
687 }
688 }
689}
690
691impl MsvcBasicName for ty::FloatTy {
692 fn msvc_basic_name(self) -> &'static str {
693 match self {
696 ty::FloatTy::F16 => {
697 bug!("`f16` should have been handled in `build_basic_type_di_node`")
698 }
699 ty::FloatTy::F32 => "float",
700 ty::FloatTy::F64 => "double",
701 ty::FloatTy::F128 => "fp128",
702 }
703 }
704}
705
706fn build_cpp_f16_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) -> DINodeCreationResult<'ll> {
707 let float_ty = cx.tcx.types.f16;
710 let bits_ty = cx.tcx.types.u16;
711 let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
712 match float_ty.kind() {
713 ty::Adt(def, _) => Some(file_metadata_from_def_id(cx, Some(def.did()))),
714 _ => None,
715 }
716 } else {
717 None
718 };
719 type_map::build_type_with_children(
720 cx,
721 type_map::stub(
722 cx,
723 Stub::Struct,
724 UniqueTypeId::for_ty(cx.tcx, float_ty),
725 "f16",
726 def_location,
727 cx.size_and_align_of(float_ty),
728 NO_SCOPE_METADATA,
729 DIFlags::FlagZero,
730 ),
731 |cx, float_di_node| {
733 let def_id = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
734 match bits_ty.kind() {
735 ty::Adt(def, _) => Some(def.did()),
736 _ => None,
737 }
738 } else {
739 None
740 };
741 smallvec![build_field_di_node(
742 cx,
743 float_di_node,
744 "bits",
745 cx.layout_of(bits_ty),
746 Size::ZERO,
747 DIFlags::FlagZero,
748 type_di_node(cx, bits_ty),
749 def_id,
750 )]
751 },
752 NO_GENERICS,
753 )
754}
755
756fn build_basic_type_di_node<'ll, 'tcx>(
757 cx: &CodegenCx<'ll, 'tcx>,
758 t: Ty<'tcx>,
759) -> DINodeCreationResult<'ll> {
760 debug!("build_basic_type_di_node: {:?}", t);
761
762 let cpp_like_debuginfo = cpp_like_debuginfo(cx.tcx);
765
766 use dwarf_const::{DW_ATE_UTF, DW_ATE_boolean, DW_ATE_float, DW_ATE_signed, DW_ATE_unsigned};
767
768 let (name, encoding) = match t.kind() {
769 ty::Never => ("!", DW_ATE_unsigned),
770 ty::Tuple(elements) if elements.is_empty() => {
771 if cpp_like_debuginfo {
772 return build_tuple_type_di_node(cx, UniqueTypeId::for_ty(cx.tcx, t));
773 } else {
774 ("()", DW_ATE_unsigned)
775 }
776 }
777 ty::Bool => ("bool", DW_ATE_boolean),
778 ty::Char => ("char", DW_ATE_UTF),
779 ty::Int(int_ty) if cpp_like_debuginfo => (int_ty.msvc_basic_name(), DW_ATE_signed),
780 ty::Uint(uint_ty) if cpp_like_debuginfo => (uint_ty.msvc_basic_name(), DW_ATE_unsigned),
781 ty::Float(ty::FloatTy::F16) if cpp_like_debuginfo => {
782 return build_cpp_f16_di_node(cx);
783 }
784 ty::Float(float_ty) if cpp_like_debuginfo => (float_ty.msvc_basic_name(), DW_ATE_float),
785 ty::Int(int_ty) => (int_ty.name_str(), DW_ATE_signed),
786 ty::Uint(uint_ty) => (uint_ty.name_str(), DW_ATE_unsigned),
787 ty::Float(float_ty) => (float_ty.name_str(), DW_ATE_float),
788 _ => bug!("debuginfo::build_basic_type_di_node - `t` is invalid type"),
789 };
790
791 let ty_di_node = create_basic_type(cx, name, cx.size_of(t), encoding);
792
793 if !cpp_like_debuginfo {
794 return DINodeCreationResult::new(ty_di_node, false);
795 }
796
797 let typedef_name = match t.kind() {
798 ty::Int(int_ty) => int_ty.name_str(),
799 ty::Uint(uint_ty) => uint_ty.name_str(),
800 ty::Float(float_ty) => float_ty.name_str(),
801 _ => return DINodeCreationResult::new(ty_di_node, false),
802 };
803
804 let typedef_di_node = unsafe {
805 llvm::LLVMRustDIBuilderCreateTypedef(
806 DIB(cx),
807 ty_di_node,
808 typedef_name.as_c_char_ptr(),
809 typedef_name.len(),
810 unknown_file_metadata(cx),
811 0,
812 None,
813 )
814 };
815
816 DINodeCreationResult::new(typedef_di_node, false)
817}
818
819fn create_basic_type<'ll, 'tcx>(
820 cx: &CodegenCx<'ll, 'tcx>,
821 name: &str,
822 size: Size,
823 encoding: u32,
824) -> &'ll DIBasicType {
825 unsafe {
826 llvm::LLVMRustDIBuilderCreateBasicType(
827 DIB(cx),
828 name.as_c_char_ptr(),
829 name.len(),
830 size.bits(),
831 encoding,
832 )
833 }
834}
835
836fn build_foreign_type_di_node<'ll, 'tcx>(
837 cx: &CodegenCx<'ll, 'tcx>,
838 t: Ty<'tcx>,
839 unique_type_id: UniqueTypeId<'tcx>,
840) -> DINodeCreationResult<'ll> {
841 debug!("build_foreign_type_di_node: {:?}", t);
842
843 let &ty::Foreign(def_id) = unique_type_id.expect_ty().kind() else {
844 bug!(
845 "build_foreign_type_di_node() called with unexpected type: {:?}",
846 unique_type_id.expect_ty()
847 );
848 };
849
850 build_type_with_children(
851 cx,
852 type_map::stub(
853 cx,
854 Stub::Struct,
855 unique_type_id,
856 &compute_debuginfo_type_name(cx.tcx, t, false),
857 None,
858 cx.size_and_align_of(t),
859 Some(get_namespace_for_item(cx, def_id)),
860 DIFlags::FlagZero,
861 ),
862 |_, _| smallvec![],
863 NO_GENERICS,
864 )
865}
866
867pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
868 tcx: TyCtxt<'tcx>,
869 codegen_unit_name: &str,
870 debug_context: &CodegenUnitDebugContext<'ll, 'tcx>,
871) -> &'ll DIDescriptor {
872 use rustc_session::RemapFileNameExt;
873 use rustc_session::config::RemapPathScopeComponents;
874 let mut name_in_debuginfo = tcx
875 .sess
876 .local_crate_source_file()
877 .map(|src| src.for_scope(&tcx.sess, RemapPathScopeComponents::DEBUGINFO).to_path_buf())
878 .unwrap_or_else(|| PathBuf::from(tcx.crate_name(LOCAL_CRATE).as_str()));
879
880 name_in_debuginfo.push("@");
898 name_in_debuginfo.push(codegen_unit_name);
899
900 debug!("build_compile_unit_di_node: {:?}", name_in_debuginfo);
901 let rustc_producer = format!("rustc version {}", tcx.sess.cfg_version);
902 let producer = format!("clang LLVM ({rustc_producer})");
904
905 let name_in_debuginfo = name_in_debuginfo.to_string_lossy();
906 let work_dir = tcx
907 .sess
908 .opts
909 .working_dir
910 .for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO)
911 .to_string_lossy();
912 let output_filenames = tcx.output_filenames(());
913 let split_name = if tcx.sess.target_can_use_split_dwarf()
914 && let Some(f) = output_filenames.split_dwarf_path(
915 tcx.sess.split_debuginfo(),
916 tcx.sess.opts.unstable_opts.split_dwarf_kind,
917 codegen_unit_name,
918 tcx.sess.invocation_temp.as_deref(),
919 ) {
920 Some(tcx.sess.source_map().path_mapping().to_real_filename(f))
922 } else {
923 None
924 };
925 let split_name = split_name
926 .as_ref()
927 .map(|f| f.for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO).to_string_lossy())
928 .unwrap_or_default();
929 let kind = DebugEmissionKind::from_generic(tcx.sess.opts.debuginfo);
930
931 let dwarf_version = tcx.sess.dwarf_version();
932 let is_dwarf_kind =
933 matches!(tcx.sess.target.debuginfo_kind, DebuginfoKind::Dwarf | DebuginfoKind::DwarfDsym);
934 let debug_name_table_kind = if is_dwarf_kind && dwarf_version <= 4 {
936 DebugNameTableKind::None
937 } else {
938 DebugNameTableKind::Default
939 };
940
941 unsafe {
942 let compile_unit_file = create_file(
943 debug_context.builder.as_ref(),
944 &name_in_debuginfo,
945 &work_dir,
946 "",
947 llvm::ChecksumKind::None,
948 None,
949 );
950
951 let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit(
952 debug_context.builder.as_ref(),
953 dwarf_const::DW_LANG_Rust,
954 compile_unit_file,
955 producer.as_c_char_ptr(),
956 producer.len(),
957 tcx.sess.opts.optimize != config::OptLevel::No,
958 c"".as_ptr(),
959 0,
960 split_name.as_c_char_ptr(),
964 split_name.len(),
965 kind,
966 0,
967 tcx.sess.opts.unstable_opts.split_dwarf_inlining,
968 debug_name_table_kind,
969 );
970
971 return unit_metadata;
972 };
973}
974
975fn build_field_di_node<'ll, 'tcx>(
977 cx: &CodegenCx<'ll, 'tcx>,
978 owner: &'ll DIScope,
979 name: &str,
980 layout: TyAndLayout<'tcx>,
981 offset: Size,
982 flags: DIFlags,
983 type_di_node: &'ll DIType,
984 def_id: Option<DefId>,
985) -> &'ll DIType {
986 let (file_metadata, line_number) = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers
987 {
988 file_metadata_from_def_id(cx, def_id)
989 } else {
990 (unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
991 };
992 create_member_type(
993 cx,
994 owner,
995 name,
996 file_metadata,
997 line_number,
998 layout,
999 offset,
1000 flags,
1001 type_di_node,
1002 )
1003}
1004
1005fn create_member_type<'ll, 'tcx>(
1006 cx: &CodegenCx<'ll, 'tcx>,
1007 owner: &'ll DIScope,
1008 name: &str,
1009 file_metadata: &'ll DIType,
1010 line_number: u32,
1011 layout: TyAndLayout<'tcx>,
1012 offset: Size,
1013 flags: DIFlags,
1014 type_di_node: &'ll DIType,
1015) -> &'ll DIType {
1016 unsafe {
1017 llvm::LLVMRustDIBuilderCreateMemberType(
1018 DIB(cx),
1019 owner,
1020 name.as_c_char_ptr(),
1021 name.len(),
1022 file_metadata,
1023 line_number,
1024 layout.size.bits(),
1025 layout.align.abi.bits() as u32,
1026 offset.bits(),
1027 flags,
1028 type_di_node,
1029 )
1030 }
1031}
1032
1033fn visibility_di_flags<'ll, 'tcx>(
1039 cx: &CodegenCx<'ll, 'tcx>,
1040 did: DefId,
1041 type_did: DefId,
1042) -> DIFlags {
1043 let parent_did = cx.tcx.parent(type_did);
1044 let visibility = cx.tcx.visibility(did);
1045 match visibility {
1046 Visibility::Public => DIFlags::FlagPublic,
1047 Visibility::Restricted(did) if did == parent_did => DIFlags::FlagPrivate,
1049 Visibility::Restricted(..) => DIFlags::FlagProtected,
1051 }
1052}
1053
1054fn build_struct_type_di_node<'ll, 'tcx>(
1056 cx: &CodegenCx<'ll, 'tcx>,
1057 unique_type_id: UniqueTypeId<'tcx>,
1058) -> DINodeCreationResult<'ll> {
1059 let struct_type = unique_type_id.expect_ty();
1060 let ty::Adt(adt_def, _) = struct_type.kind() else {
1061 bug!("build_struct_type_di_node() called with non-struct-type: {:?}", struct_type);
1062 };
1063 assert!(adt_def.is_struct());
1064 let containing_scope = get_namespace_for_item(cx, adt_def.did());
1065 let struct_type_and_layout = cx.layout_of(struct_type);
1066 let variant_def = adt_def.non_enum_variant();
1067 let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1068 Some(file_metadata_from_def_id(cx, Some(adt_def.did())))
1069 } else {
1070 None
1071 };
1072
1073 type_map::build_type_with_children(
1074 cx,
1075 type_map::stub(
1076 cx,
1077 Stub::Struct,
1078 unique_type_id,
1079 &compute_debuginfo_type_name(cx.tcx, struct_type, false),
1080 def_location,
1081 size_and_align_of(struct_type_and_layout),
1082 Some(containing_scope),
1083 visibility_di_flags(cx, adt_def.did(), adt_def.did()),
1084 ),
1085 |cx, owner| {
1087 variant_def
1088 .fields
1089 .iter()
1090 .enumerate()
1091 .map(|(i, f)| {
1092 let field_name = if variant_def.ctor_kind() == Some(CtorKind::Fn) {
1093 tuple_field_name(i)
1095 } else {
1096 Cow::Borrowed(f.name.as_str())
1098 };
1099 let field_layout = struct_type_and_layout.field(cx, i);
1100 let def_id = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1101 Some(f.did)
1102 } else {
1103 None
1104 };
1105 build_field_di_node(
1106 cx,
1107 owner,
1108 &field_name[..],
1109 field_layout,
1110 struct_type_and_layout.fields.offset(i),
1111 visibility_di_flags(cx, f.did, adt_def.did()),
1112 type_di_node(cx, field_layout.ty),
1113 def_id,
1114 )
1115 })
1116 .collect()
1117 },
1118 |cx| build_generic_type_param_di_nodes(cx, struct_type),
1119 )
1120}
1121
1122fn build_upvar_field_di_nodes<'ll, 'tcx>(
1129 cx: &CodegenCx<'ll, 'tcx>,
1130 closure_or_coroutine_ty: Ty<'tcx>,
1131 closure_or_coroutine_di_node: &'ll DIType,
1132) -> SmallVec<&'ll DIType> {
1133 let (&def_id, up_var_tys) = match closure_or_coroutine_ty.kind() {
1134 ty::Coroutine(def_id, args) => (def_id, args.as_coroutine().prefix_tys()),
1135 ty::Closure(def_id, args) => (def_id, args.as_closure().upvar_tys()),
1136 ty::CoroutineClosure(def_id, args) => (def_id, args.as_coroutine_closure().upvar_tys()),
1137 _ => {
1138 bug!(
1139 "build_upvar_field_di_nodes() called with non-closure-or-coroutine-type: {:?}",
1140 closure_or_coroutine_ty
1141 )
1142 }
1143 };
1144
1145 assert!(up_var_tys.iter().all(|t| t == cx.tcx.normalize_erasing_regions(cx.typing_env(), t)));
1146
1147 let capture_names = cx.tcx.closure_saved_names_of_captured_variables(def_id);
1148 let layout = cx.layout_of(closure_or_coroutine_ty);
1149
1150 up_var_tys
1151 .into_iter()
1152 .zip(capture_names.iter())
1153 .enumerate()
1154 .map(|(index, (up_var_ty, capture_name))| {
1155 build_field_di_node(
1156 cx,
1157 closure_or_coroutine_di_node,
1158 capture_name.as_str(),
1159 cx.layout_of(up_var_ty),
1160 layout.fields.offset(index),
1161 DIFlags::FlagZero,
1162 type_di_node(cx, up_var_ty),
1163 None,
1164 )
1165 })
1166 .collect()
1167}
1168
1169fn build_tuple_type_di_node<'ll, 'tcx>(
1171 cx: &CodegenCx<'ll, 'tcx>,
1172 unique_type_id: UniqueTypeId<'tcx>,
1173) -> DINodeCreationResult<'ll> {
1174 let tuple_type = unique_type_id.expect_ty();
1175 let &ty::Tuple(component_types) = tuple_type.kind() else {
1176 bug!("build_tuple_type_di_node() called with non-tuple-type: {:?}", tuple_type)
1177 };
1178
1179 let tuple_type_and_layout = cx.layout_of(tuple_type);
1180 let type_name = compute_debuginfo_type_name(cx.tcx, tuple_type, false);
1181
1182 type_map::build_type_with_children(
1183 cx,
1184 type_map::stub(
1185 cx,
1186 Stub::Struct,
1187 unique_type_id,
1188 &type_name,
1189 None,
1190 size_and_align_of(tuple_type_and_layout),
1191 NO_SCOPE_METADATA,
1192 DIFlags::FlagZero,
1193 ),
1194 |cx, tuple_di_node| {
1196 component_types
1197 .into_iter()
1198 .enumerate()
1199 .map(|(index, component_type)| {
1200 build_field_di_node(
1201 cx,
1202 tuple_di_node,
1203 &tuple_field_name(index),
1204 cx.layout_of(component_type),
1205 tuple_type_and_layout.fields.offset(index),
1206 DIFlags::FlagZero,
1207 type_di_node(cx, component_type),
1208 None,
1209 )
1210 })
1211 .collect()
1212 },
1213 NO_GENERICS,
1214 )
1215}
1216
1217fn build_closure_env_di_node<'ll, 'tcx>(
1219 cx: &CodegenCx<'ll, 'tcx>,
1220 unique_type_id: UniqueTypeId<'tcx>,
1221) -> DINodeCreationResult<'ll> {
1222 let closure_env_type = unique_type_id.expect_ty();
1223 let &(ty::Closure(def_id, _) | ty::CoroutineClosure(def_id, _)) = closure_env_type.kind()
1224 else {
1225 bug!("build_closure_env_di_node() called with non-closure-type: {:?}", closure_env_type)
1226 };
1227 let containing_scope = get_namespace_for_item(cx, def_id);
1228 let type_name = compute_debuginfo_type_name(cx.tcx, closure_env_type, false);
1229
1230 let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1231 Some(file_metadata_from_def_id(cx, Some(def_id)))
1232 } else {
1233 None
1234 };
1235
1236 type_map::build_type_with_children(
1237 cx,
1238 type_map::stub(
1239 cx,
1240 Stub::Struct,
1241 unique_type_id,
1242 &type_name,
1243 def_location,
1244 cx.size_and_align_of(closure_env_type),
1245 Some(containing_scope),
1246 DIFlags::FlagZero,
1247 ),
1248 |cx, owner| build_upvar_field_di_nodes(cx, closure_env_type, owner),
1250 NO_GENERICS,
1251 )
1252}
1253
1254fn build_union_type_di_node<'ll, 'tcx>(
1256 cx: &CodegenCx<'ll, 'tcx>,
1257 unique_type_id: UniqueTypeId<'tcx>,
1258) -> DINodeCreationResult<'ll> {
1259 let union_type = unique_type_id.expect_ty();
1260 let (union_def_id, variant_def) = match union_type.kind() {
1261 ty::Adt(def, _) => (def.did(), def.non_enum_variant()),
1262 _ => bug!("build_union_type_di_node on a non-ADT"),
1263 };
1264 let containing_scope = get_namespace_for_item(cx, union_def_id);
1265 let union_ty_and_layout = cx.layout_of(union_type);
1266 let type_name = compute_debuginfo_type_name(cx.tcx, union_type, false);
1267 let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1268 Some(file_metadata_from_def_id(cx, Some(union_def_id)))
1269 } else {
1270 None
1271 };
1272
1273 type_map::build_type_with_children(
1274 cx,
1275 type_map::stub(
1276 cx,
1277 Stub::Union,
1278 unique_type_id,
1279 &type_name,
1280 def_location,
1281 size_and_align_of(union_ty_and_layout),
1282 Some(containing_scope),
1283 DIFlags::FlagZero,
1284 ),
1285 |cx, owner| {
1287 variant_def
1288 .fields
1289 .iter()
1290 .enumerate()
1291 .map(|(i, f)| {
1292 let field_layout = union_ty_and_layout.field(cx, i);
1293 let def_id = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1294 Some(f.did)
1295 } else {
1296 None
1297 };
1298 build_field_di_node(
1299 cx,
1300 owner,
1301 f.name.as_str(),
1302 field_layout,
1303 Size::ZERO,
1304 DIFlags::FlagZero,
1305 type_di_node(cx, field_layout.ty),
1306 def_id,
1307 )
1308 })
1309 .collect()
1310 },
1311 |cx| build_generic_type_param_di_nodes(cx, union_type),
1313 )
1314}
1315
1316fn build_generic_type_param_di_nodes<'ll, 'tcx>(
1318 cx: &CodegenCx<'ll, 'tcx>,
1319 ty: Ty<'tcx>,
1320) -> SmallVec<Option<&'ll DIType>> {
1321 if let ty::Adt(def, args) = *ty.kind() {
1322 if args.types().next().is_some() {
1323 let generics = cx.tcx.generics_of(def.did());
1324 let names = get_parameter_names(cx, generics);
1325 let template_params: SmallVec<_> = iter::zip(args, names)
1326 .filter_map(|(kind, name)| {
1327 kind.as_type().map(|ty| {
1328 let actual_type = cx.tcx.normalize_erasing_regions(cx.typing_env(), ty);
1329 let actual_type_di_node = type_di_node(cx, actual_type);
1330 Some(cx.create_template_type_parameter(name.as_str(), actual_type_di_node))
1331 })
1332 })
1333 .collect();
1334
1335 return template_params;
1336 }
1337 }
1338
1339 return smallvec![];
1340
1341 fn get_parameter_names(cx: &CodegenCx<'_, '_>, generics: &ty::Generics) -> Vec<Symbol> {
1342 let mut names = generics
1343 .parent
1344 .map_or_else(Vec::new, |def_id| get_parameter_names(cx, cx.tcx.generics_of(def_id)));
1345 names.extend(generics.own_params.iter().map(|param| param.name));
1346 names
1347 }
1348}
1349
1350pub(crate) fn build_global_var_di_node<'ll>(
1354 cx: &CodegenCx<'ll, '_>,
1355 def_id: DefId,
1356 global: &'ll Value,
1357) {
1358 if cx.dbg_cx.is_none() {
1359 return;
1360 }
1361
1362 if cx.sess().opts.debuginfo != DebugInfo::Full {
1364 return;
1365 }
1366
1367 let tcx = cx.tcx;
1368
1369 let var_scope = get_namespace_for_item(cx, def_id);
1372 let (file_metadata, line_number) = file_metadata_from_def_id(cx, Some(def_id));
1373
1374 let is_local_to_unit = is_node_local_to_unit(cx, def_id);
1375
1376 let DefKind::Static { nested, .. } = cx.tcx.def_kind(def_id) else { bug!() };
1377 if nested {
1378 return;
1379 }
1380 let variable_type = Instance::mono(cx.tcx, def_id).ty(cx.tcx, cx.typing_env());
1381 let type_di_node = type_di_node(cx, variable_type);
1382 let var_name = tcx.item_name(def_id);
1383 let var_name = var_name.as_str();
1384 let linkage_name = mangled_name_of_instance(cx, Instance::mono(tcx, def_id)).name;
1385 let linkage_name = if var_name == linkage_name { "" } else { linkage_name };
1388
1389 let global_align = cx.align_of(variable_type);
1390
1391 unsafe {
1392 llvm::LLVMRustDIBuilderCreateStaticVariable(
1393 DIB(cx),
1394 Some(var_scope),
1395 var_name.as_c_char_ptr(),
1396 var_name.len(),
1397 linkage_name.as_c_char_ptr(),
1398 linkage_name.len(),
1399 file_metadata,
1400 line_number,
1401 type_di_node,
1402 is_local_to_unit,
1403 global,
1404 None,
1405 global_align.bits() as u32,
1406 );
1407 }
1408}
1409
1410fn build_vtable_type_di_node<'ll, 'tcx>(
1420 cx: &CodegenCx<'ll, 'tcx>,
1421 ty: Ty<'tcx>,
1422 poly_trait_ref: Option<ty::ExistentialTraitRef<'tcx>>,
1423) -> &'ll DIType {
1424 let tcx = cx.tcx;
1425
1426 let vtable_entries = if let Some(poly_trait_ref) = poly_trait_ref {
1427 let trait_ref = poly_trait_ref.with_self_ty(tcx, ty);
1428 let trait_ref = tcx.erase_regions(trait_ref);
1429
1430 tcx.vtable_entries(trait_ref)
1431 } else {
1432 TyCtxt::COMMON_VTABLE_ENTRIES
1433 };
1434
1435 let void_pointer_ty = Ty::new_imm_ptr(tcx, tcx.types.unit);
1438 let void_pointer_type_di_node = type_di_node(cx, void_pointer_ty);
1439 let usize_di_node = type_di_node(cx, tcx.types.usize);
1440 let pointer_layout = cx.layout_of(void_pointer_ty);
1441 let pointer_size = pointer_layout.size;
1442 let pointer_align = pointer_layout.align.abi;
1443 assert_eq!(cx.size_and_align_of(tcx.types.usize), (pointer_size, pointer_align));
1447
1448 let vtable_type_name =
1449 compute_debuginfo_vtable_name(cx.tcx, ty, poly_trait_ref, VTableNameKind::Type);
1450 let unique_type_id = UniqueTypeId::for_vtable_ty(tcx, ty, poly_trait_ref);
1451 let size = pointer_size * vtable_entries.len() as u64;
1452
1453 let vtable_holder = type_di_node(cx, ty);
1456
1457 build_type_with_children(
1458 cx,
1459 type_map::stub(
1460 cx,
1461 Stub::VTableTy { vtable_holder },
1462 unique_type_id,
1463 &vtable_type_name,
1464 None,
1465 (size, pointer_align),
1466 NO_SCOPE_METADATA,
1467 DIFlags::FlagArtificial,
1468 ),
1469 |cx, vtable_type_di_node| {
1470 vtable_entries
1471 .iter()
1472 .enumerate()
1473 .filter_map(|(index, vtable_entry)| {
1474 let (field_name, field_type_di_node) = match vtable_entry {
1475 ty::VtblEntry::MetadataDropInPlace => {
1476 ("drop_in_place".to_string(), void_pointer_type_di_node)
1477 }
1478 ty::VtblEntry::Method(_) => {
1479 (format!("__method{index}"), void_pointer_type_di_node)
1483 }
1484 ty::VtblEntry::TraitVPtr(_) => {
1485 (format!("__super_trait_ptr{index}"), void_pointer_type_di_node)
1486 }
1487 ty::VtblEntry::MetadataAlign => ("align".to_string(), usize_di_node),
1488 ty::VtblEntry::MetadataSize => ("size".to_string(), usize_di_node),
1489 ty::VtblEntry::Vacant => return None,
1490 };
1491
1492 let field_offset = pointer_size * index as u64;
1493
1494 Some(build_field_di_node(
1495 cx,
1496 vtable_type_di_node,
1497 &field_name,
1498 pointer_layout,
1499 field_offset,
1500 DIFlags::FlagZero,
1501 field_type_di_node,
1502 None,
1503 ))
1504 })
1505 .collect()
1506 },
1507 NO_GENERICS,
1508 )
1509 .di_node
1510}
1511
1512fn find_vtable_behind_cast<'ll>(vtable: &'ll Value) -> &'ll Value {
1521 unsafe {
1523 if let Some(c) = llvm::LLVMIsAConstantExpr(vtable) {
1524 if llvm::LLVMGetConstOpcode(c) == llvm::Opcode::AddrSpaceCast {
1525 return llvm::LLVMGetOperand(c, 0).unwrap();
1526 }
1527 }
1528 }
1529 vtable
1530}
1531
1532pub(crate) fn apply_vcall_visibility_metadata<'ll, 'tcx>(
1533 cx: &CodegenCx<'ll, 'tcx>,
1534 ty: Ty<'tcx>,
1535 trait_ref: Option<ExistentialTraitRef<'tcx>>,
1536 vtable: &'ll Value,
1537) {
1538 if !cx.sess().opts.unstable_opts.virtual_function_elimination || cx.sess().lto() != Lto::Fat {
1541 return;
1542 }
1543
1544 enum VCallVisibility {
1545 Public = 0,
1546 LinkageUnit = 1,
1547 TranslationUnit = 2,
1548 }
1549
1550 let Some(trait_ref) = trait_ref else { return };
1551
1552 let vtable = find_vtable_behind_cast(vtable);
1554 let trait_ref_self = trait_ref.with_self_ty(cx.tcx, ty);
1555 let trait_ref_self = cx.tcx.erase_regions(trait_ref_self);
1556 let trait_def_id = trait_ref_self.def_id;
1557 let trait_vis = cx.tcx.visibility(trait_def_id);
1558
1559 let cgus = cx.sess().codegen_units().as_usize();
1560 let single_cgu = cgus == 1;
1561
1562 let lto = cx.sess().lto();
1563
1564 let vcall_visibility = match (lto, trait_vis, single_cgu) {
1567 (Lto::No | Lto::ThinLocal, Visibility::Public, _)
1570 | (Lto::No, Visibility::Restricted(_), false) => VCallVisibility::Public,
1571 (Lto::Fat | Lto::Thin, Visibility::Public, _)
1576 | (Lto::ThinLocal | Lto::Thin | Lto::Fat, Visibility::Restricted(_), false) => {
1577 VCallVisibility::LinkageUnit
1578 }
1579 (_, Visibility::Restricted(_), true) => VCallVisibility::TranslationUnit,
1582 };
1583
1584 let trait_ref_typeid = typeid_for_trait_ref(cx.tcx, trait_ref);
1585
1586 unsafe {
1587 let typeid = llvm::LLVMMDStringInContext2(
1588 cx.llcx,
1589 trait_ref_typeid.as_ptr() as *const c_char,
1590 trait_ref_typeid.as_bytes().len(),
1591 );
1592 let v = [llvm::LLVMValueAsMetadata(cx.const_usize(0)), typeid];
1593 llvm::LLVMRustGlobalAddMetadata(
1594 vtable,
1595 llvm::MD_type as c_uint,
1596 llvm::LLVMMDNodeInContext2(cx.llcx, v.as_ptr(), v.len()),
1597 );
1598 let vcall_visibility = llvm::LLVMValueAsMetadata(cx.const_u64(vcall_visibility as u64));
1599 let vcall_visibility_metadata = llvm::LLVMMDNodeInContext2(cx.llcx, &vcall_visibility, 1);
1600 llvm::LLVMGlobalSetMetadata(
1601 vtable,
1602 llvm::MetadataType::MD_vcall_visibility as c_uint,
1603 vcall_visibility_metadata,
1604 );
1605 }
1606}
1607
1608pub(crate) fn create_vtable_di_node<'ll, 'tcx>(
1613 cx: &CodegenCx<'ll, 'tcx>,
1614 ty: Ty<'tcx>,
1615 poly_trait_ref: Option<ty::ExistentialTraitRef<'tcx>>,
1616 vtable: &'ll Value,
1617) {
1618 if cx.dbg_cx.is_none() {
1619 return;
1620 }
1621
1622 if cx.sess().opts.debuginfo != DebugInfo::Full {
1624 return;
1625 }
1626
1627 let vtable = find_vtable_behind_cast(vtable);
1629
1630 llvm::SetUnnamedAddress(vtable, llvm::UnnamedAddr::No);
1634
1635 let vtable_name =
1636 compute_debuginfo_vtable_name(cx.tcx, ty, poly_trait_ref, VTableNameKind::GlobalVariable);
1637 let vtable_type_di_node = build_vtable_type_di_node(cx, ty, poly_trait_ref);
1638 let linkage_name = "";
1639
1640 unsafe {
1641 llvm::LLVMRustDIBuilderCreateStaticVariable(
1642 DIB(cx),
1643 NO_SCOPE_METADATA,
1644 vtable_name.as_c_char_ptr(),
1645 vtable_name.len(),
1646 linkage_name.as_c_char_ptr(),
1647 linkage_name.len(),
1648 unknown_file_metadata(cx),
1649 UNKNOWN_LINE_NUMBER,
1650 vtable_type_di_node,
1651 true,
1652 vtable,
1653 None,
1654 0,
1655 );
1656 }
1657}
1658
1659pub(crate) fn extend_scope_to_file<'ll>(
1661 cx: &CodegenCx<'ll, '_>,
1662 scope_metadata: &'ll DIScope,
1663 file: &SourceFile,
1664) -> &'ll DILexicalBlock {
1665 let file_metadata = file_metadata(cx, file);
1666 unsafe {
1667 llvm::LLVMDIBuilderCreateLexicalBlockFile(
1668 DIB(cx),
1669 scope_metadata,
1670 file_metadata,
1671 0u32,
1672 )
1673 }
1674}
1675
1676fn tuple_field_name(field_index: usize) -> Cow<'static, str> {
1677 const TUPLE_FIELD_NAMES: [&'static str; 16] = [
1678 "__0", "__1", "__2", "__3", "__4", "__5", "__6", "__7", "__8", "__9", "__10", "__11",
1679 "__12", "__13", "__14", "__15",
1680 ];
1681 TUPLE_FIELD_NAMES
1682 .get(field_index)
1683 .map(|s| Cow::from(*s))
1684 .unwrap_or_else(|| Cow::from(format!("__{field_index}")))
1685}
1686
1687pub(crate) type DefinitionLocation<'ll> = (&'ll DIFile, c_uint);
1688
1689pub(crate) fn file_metadata_from_def_id<'ll>(
1690 cx: &CodegenCx<'ll, '_>,
1691 def_id: Option<DefId>,
1692) -> DefinitionLocation<'ll> {
1693 if let Some(def_id) = def_id
1694 && let span = hygiene::walk_chain_collapsed(cx.tcx.def_span(def_id), DUMMY_SP)
1695 && !span.is_dummy()
1696 {
1697 let loc = cx.lookup_debug_loc(span.lo());
1698 (file_metadata(cx, &loc.file), loc.line)
1699 } else {
1700 (unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
1701 }
1702}