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