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