Skip to main content

rustc_codegen_llvm/debuginfo/metadata/
type_map.rs

1use std::cell::RefCell;
2
3use libc::c_uint;
4use rustc_abi::{Align, Size, VariantIdx};
5use rustc_data_structures::fingerprint::Fingerprint;
6use rustc_data_structures::fx::FxHashMap;
7use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
8use rustc_macros::HashStable;
9use rustc_middle::bug;
10use rustc_middle::ty::{self, ExistentialTraitRef, Ty, TyCtxt};
11
12use super::{DefinitionLocation, SmallVec, UNKNOWN_LINE_NUMBER, unknown_file_metadata};
13use crate::common::CodegenCx;
14use crate::debuginfo::utils::{DIB, create_DIArray, debug_context};
15use crate::llvm;
16use crate::llvm::debuginfo::{DIFlags, DIScope, DIType};
17
18mod private {
19    use rustc_macros::HashStable;
20
21    // This type cannot be constructed outside of this module because
22    // it has a private field. We make use of this in order to prevent
23    // `UniqueTypeId` from being constructed directly, without asserting
24    // the preconditions.
25    #[derive(#[automatically_derived]
impl ::core::fmt::Debug for HiddenZst {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f, "HiddenZst")
    }
}Debug, #[automatically_derived]
impl ::core::clone::Clone for HiddenZst {
    #[inline]
    fn clone(&self) -> HiddenZst { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for HiddenZst { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for HiddenZst {
    #[inline]
    fn eq(&self, other: &HiddenZst) -> bool { true }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for HiddenZst {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for HiddenZst {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {}
}Hash, const _: () =
    {
        impl<'__ctx>
            ::rustc_data_structures::stable_hasher::HashStable<::rustc_middle::ich::StableHashingContext<'__ctx>>
            for HiddenZst {
            #[inline]
            fn hash_stable(&self,
                __hcx: &mut ::rustc_middle::ich::StableHashingContext<'__ctx>,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                match *self { HiddenZst => {} }
            }
        }
    };HashStable)]
26    pub(crate) struct HiddenZst;
27}
28
29/// A unique identifier for anything that we create a debuginfo node for.
30/// The types it contains are expected to already be normalized (which
31/// is asserted in the constructors).
32///
33/// Note that there are some things that only show up in debuginfo, like
34/// the separate type descriptions for each enum variant. These get an ID
35/// too because they have their own debuginfo node in LLVM IR.
36#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for UniqueTypeId<'tcx> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            UniqueTypeId::Ty(__self_0, __self_1) =>
                ::core::fmt::Formatter::debug_tuple_field2_finish(f, "Ty",
                    __self_0, &__self_1),
            UniqueTypeId::VariantPart(__self_0, __self_1) =>
                ::core::fmt::Formatter::debug_tuple_field2_finish(f,
                    "VariantPart", __self_0, &__self_1),
            UniqueTypeId::VariantStructType(__self_0, __self_1, __self_2) =>
                ::core::fmt::Formatter::debug_tuple_field3_finish(f,
                    "VariantStructType", __self_0, __self_1, &__self_2),
            UniqueTypeId::VariantStructTypeCppLikeWrapper(__self_0, __self_1,
                __self_2) =>
                ::core::fmt::Formatter::debug_tuple_field3_finish(f,
                    "VariantStructTypeCppLikeWrapper", __self_0, __self_1,
                    &__self_2),
            UniqueTypeId::VTableTy(__self_0, __self_1, __self_2) =>
                ::core::fmt::Formatter::debug_tuple_field3_finish(f,
                    "VTableTy", __self_0, __self_1, &__self_2),
        }
    }
}Debug, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for UniqueTypeId<'tcx> {
    #[inline]
    fn clone(&self) -> UniqueTypeId<'tcx> {
        let _: ::core::clone::AssertParamIsClone<Ty<'tcx>>;
        let _: ::core::clone::AssertParamIsClone<private::HiddenZst>;
        let _: ::core::clone::AssertParamIsClone<Ty<'tcx>>;
        let _: ::core::clone::AssertParamIsClone<private::HiddenZst>;
        let _: ::core::clone::AssertParamIsClone<Ty<'tcx>>;
        let _: ::core::clone::AssertParamIsClone<VariantIdx>;
        let _: ::core::clone::AssertParamIsClone<private::HiddenZst>;
        let _: ::core::clone::AssertParamIsClone<Ty<'tcx>>;
        let _: ::core::clone::AssertParamIsClone<private::HiddenZst>;
        let _: ::core::clone::AssertParamIsClone<Ty<'tcx>>;
        let _:
                ::core::clone::AssertParamIsClone<Option<ExistentialTraitRef<'tcx>>>;
        let _: ::core::clone::AssertParamIsClone<private::HiddenZst>;
        *self
    }
}Clone, #[automatically_derived]
impl<'tcx> ::core::marker::Copy for UniqueTypeId<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::cmp::PartialEq for UniqueTypeId<'tcx> {
    #[inline]
    fn eq(&self, other: &UniqueTypeId<'tcx>) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (UniqueTypeId::Ty(__self_0, __self_1),
                    UniqueTypeId::Ty(__arg1_0, __arg1_1)) =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1,
                (UniqueTypeId::VariantPart(__self_0, __self_1),
                    UniqueTypeId::VariantPart(__arg1_0, __arg1_1)) =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1,
                (UniqueTypeId::VariantStructType(__self_0, __self_1,
                    __self_2),
                    UniqueTypeId::VariantStructType(__arg1_0, __arg1_1,
                    __arg1_2)) =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1 &&
                        __self_2 == __arg1_2,
                (UniqueTypeId::VariantStructTypeCppLikeWrapper(__self_0,
                    __self_1, __self_2),
                    UniqueTypeId::VariantStructTypeCppLikeWrapper(__arg1_0,
                    __arg1_1, __arg1_2)) =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1 &&
                        __self_2 == __arg1_2,
                (UniqueTypeId::VTableTy(__self_0, __self_1, __self_2),
                    UniqueTypeId::VTableTy(__arg1_0, __arg1_1, __arg1_2)) =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1 &&
                        __self_2 == __arg1_2,
                _ => unsafe { ::core::intrinsics::unreachable() }
            }
    }
}PartialEq, #[automatically_derived]
impl<'tcx> ::core::cmp::Eq for UniqueTypeId<'tcx> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Ty<'tcx>>;
        let _: ::core::cmp::AssertParamIsEq<private::HiddenZst>;
        let _: ::core::cmp::AssertParamIsEq<Ty<'tcx>>;
        let _: ::core::cmp::AssertParamIsEq<private::HiddenZst>;
        let _: ::core::cmp::AssertParamIsEq<Ty<'tcx>>;
        let _: ::core::cmp::AssertParamIsEq<VariantIdx>;
        let _: ::core::cmp::AssertParamIsEq<private::HiddenZst>;
        let _: ::core::cmp::AssertParamIsEq<Ty<'tcx>>;
        let _: ::core::cmp::AssertParamIsEq<private::HiddenZst>;
        let _: ::core::cmp::AssertParamIsEq<Ty<'tcx>>;
        let _:
                ::core::cmp::AssertParamIsEq<Option<ExistentialTraitRef<'tcx>>>;
        let _: ::core::cmp::AssertParamIsEq<private::HiddenZst>;
    }
}Eq, #[automatically_derived]
impl<'tcx> ::core::hash::Hash for UniqueTypeId<'tcx> {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        ::core::hash::Hash::hash(&__self_discr, state);
        match self {
            UniqueTypeId::Ty(__self_0, __self_1) => {
                ::core::hash::Hash::hash(__self_0, state);
                ::core::hash::Hash::hash(__self_1, state)
            }
            UniqueTypeId::VariantPart(__self_0, __self_1) => {
                ::core::hash::Hash::hash(__self_0, state);
                ::core::hash::Hash::hash(__self_1, state)
            }
            UniqueTypeId::VariantStructType(__self_0, __self_1, __self_2) => {
                ::core::hash::Hash::hash(__self_0, state);
                ::core::hash::Hash::hash(__self_1, state);
                ::core::hash::Hash::hash(__self_2, state)
            }
            UniqueTypeId::VariantStructTypeCppLikeWrapper(__self_0, __self_1,
                __self_2) => {
                ::core::hash::Hash::hash(__self_0, state);
                ::core::hash::Hash::hash(__self_1, state);
                ::core::hash::Hash::hash(__self_2, state)
            }
            UniqueTypeId::VTableTy(__self_0, __self_1, __self_2) => {
                ::core::hash::Hash::hash(__self_0, state);
                ::core::hash::Hash::hash(__self_1, state);
                ::core::hash::Hash::hash(__self_2, state)
            }
        }
    }
}Hash, const _: () =
    {
        impl<'tcx, '__ctx>
            ::rustc_data_structures::stable_hasher::HashStable<::rustc_middle::ich::StableHashingContext<'__ctx>>
            for UniqueTypeId<'tcx> {
            #[inline]
            fn hash_stable(&self,
                __hcx: &mut ::rustc_middle::ich::StableHashingContext<'__ctx>,
                __hasher:
                    &mut ::rustc_data_structures::stable_hasher::StableHasher) {
                ::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
                match *self {
                    UniqueTypeId::Ty(ref __binding_0, ref __binding_1) => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                        { __binding_1.hash_stable(__hcx, __hasher); }
                    }
                    UniqueTypeId::VariantPart(ref __binding_0, ref __binding_1)
                        => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                        { __binding_1.hash_stable(__hcx, __hasher); }
                    }
                    UniqueTypeId::VariantStructType(ref __binding_0,
                        ref __binding_1, ref __binding_2) => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                        { __binding_1.hash_stable(__hcx, __hasher); }
                        { __binding_2.hash_stable(__hcx, __hasher); }
                    }
                    UniqueTypeId::VariantStructTypeCppLikeWrapper(ref __binding_0,
                        ref __binding_1, ref __binding_2) => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                        { __binding_1.hash_stable(__hcx, __hasher); }
                        { __binding_2.hash_stable(__hcx, __hasher); }
                    }
                    UniqueTypeId::VTableTy(ref __binding_0, ref __binding_1,
                        ref __binding_2) => {
                        { __binding_0.hash_stable(__hcx, __hasher); }
                        { __binding_1.hash_stable(__hcx, __hasher); }
                        { __binding_2.hash_stable(__hcx, __hasher); }
                    }
                }
            }
        }
    };HashStable)]
37pub(super) enum UniqueTypeId<'tcx> {
38    /// The ID of a regular type as it shows up at the language level.
39    Ty(Ty<'tcx>, private::HiddenZst),
40    /// The ID for the single DW_TAG_variant_part nested inside the top-level
41    /// DW_TAG_structure_type that describes enums and coroutines.
42    VariantPart(Ty<'tcx>, private::HiddenZst),
43    /// The ID for the artificial struct type describing a single enum variant.
44    VariantStructType(Ty<'tcx>, VariantIdx, private::HiddenZst),
45    /// The ID for the additional wrapper struct type describing an enum variant in CPP-like mode.
46    VariantStructTypeCppLikeWrapper(Ty<'tcx>, VariantIdx, private::HiddenZst),
47    /// The ID of the artificial type we create for VTables.
48    VTableTy(Ty<'tcx>, Option<ExistentialTraitRef<'tcx>>, private::HiddenZst),
49}
50
51impl<'tcx> UniqueTypeId<'tcx> {
52    pub(crate) fn for_ty(tcx: TyCtxt<'tcx>, t: Ty<'tcx>) -> Self {
53        match (&t,
        &tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(),
                t)) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(t, tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), t));
54        UniqueTypeId::Ty(t, private::HiddenZst)
55    }
56
57    pub(crate) fn for_enum_variant_part(tcx: TyCtxt<'tcx>, enum_ty: Ty<'tcx>) -> Self {
58        match (&enum_ty,
        &tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(),
                enum_ty)) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(
59            enum_ty,
60            tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), enum_ty)
61        );
62        UniqueTypeId::VariantPart(enum_ty, private::HiddenZst)
63    }
64
65    pub(crate) fn for_enum_variant_struct_type(
66        tcx: TyCtxt<'tcx>,
67        enum_ty: Ty<'tcx>,
68        variant_idx: VariantIdx,
69    ) -> Self {
70        match (&enum_ty,
        &tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(),
                enum_ty)) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(
71            enum_ty,
72            tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), enum_ty)
73        );
74        UniqueTypeId::VariantStructType(enum_ty, variant_idx, private::HiddenZst)
75    }
76
77    pub(crate) fn for_enum_variant_struct_type_wrapper(
78        tcx: TyCtxt<'tcx>,
79        enum_ty: Ty<'tcx>,
80        variant_idx: VariantIdx,
81    ) -> Self {
82        match (&enum_ty,
        &tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(),
                enum_ty)) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(
83            enum_ty,
84            tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), enum_ty)
85        );
86        UniqueTypeId::VariantStructTypeCppLikeWrapper(enum_ty, variant_idx, private::HiddenZst)
87    }
88
89    pub(crate) fn for_vtable_ty(
90        tcx: TyCtxt<'tcx>,
91        self_type: Ty<'tcx>,
92        implemented_trait: Option<ExistentialTraitRef<'tcx>>,
93    ) -> Self {
94        match (&self_type,
        &tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(),
                self_type)) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(
95            self_type,
96            tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), self_type)
97        );
98        match (&implemented_trait,
        &tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(),
                implemented_trait)) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(
99            implemented_trait,
100            tcx.normalize_erasing_regions(ty::TypingEnv::fully_monomorphized(), implemented_trait)
101        );
102        UniqueTypeId::VTableTy(self_type, implemented_trait, private::HiddenZst)
103    }
104
105    /// Generates a string version of this [UniqueTypeId], which can be used as the `UniqueId`
106    /// argument of the various `LLVMRustDIBuilderCreate*Type()` methods.
107    ///
108    /// Right now this takes the form of a hex-encoded opaque hash value.
109    fn generate_unique_id_string(self, tcx: TyCtxt<'tcx>) -> String {
110        let mut hasher = StableHasher::new();
111        tcx.with_stable_hashing_context(|mut hcx| {
112            hcx.while_hashing_spans(false, |hcx| self.hash_stable(hcx, &mut hasher))
113        });
114        hasher.finish::<Fingerprint>().to_hex()
115    }
116
117    pub(crate) fn expect_ty(self) -> Ty<'tcx> {
118        match self {
119            UniqueTypeId::Ty(ty, _) => ty,
120            _ => ::rustc_middle::util::bug::bug_fmt(format_args!("Expected `UniqueTypeId::Ty` but found `{0:?}`",
        self))bug!("Expected `UniqueTypeId::Ty` but found `{:?}`", self),
121        }
122    }
123}
124
125/// The `TypeMap` is where the debug context holds the type metadata nodes
126/// created so far. The debuginfo nodes are identified by `UniqueTypeId`.
127#[derive(#[automatically_derived]
impl<'ll, 'tcx> ::core::default::Default for TypeMap<'ll, 'tcx> {
    #[inline]
    fn default() -> TypeMap<'ll, 'tcx> {
        TypeMap { unique_id_to_di_node: ::core::default::Default::default() }
    }
}Default)]
128pub(crate) struct TypeMap<'ll, 'tcx> {
129    pub(super) unique_id_to_di_node: RefCell<FxHashMap<UniqueTypeId<'tcx>, &'ll DIType>>,
130}
131
132impl<'ll, 'tcx> TypeMap<'ll, 'tcx> {
133    /// Adds a `UniqueTypeId` to metadata mapping to the `TypeMap`. The method will
134    /// fail if the mapping already exists.
135    pub(super) fn insert(&self, unique_type_id: UniqueTypeId<'tcx>, metadata: &'ll DIType) {
136        if self.unique_id_to_di_node.borrow_mut().insert(unique_type_id, metadata).is_some() {
137            ::rustc_middle::util::bug::bug_fmt(format_args!("type metadata for unique ID \'{0:?}\' is already in the `TypeMap`!",
        unique_type_id));bug!("type metadata for unique ID '{:?}' is already in the `TypeMap`!", unique_type_id);
138        }
139    }
140
141    pub(super) fn di_node_for_unique_id(
142        &self,
143        unique_type_id: UniqueTypeId<'tcx>,
144    ) -> Option<&'ll DIType> {
145        self.unique_id_to_di_node.borrow().get(&unique_type_id).cloned()
146    }
147}
148
149pub(crate) struct DINodeCreationResult<'ll> {
150    pub di_node: &'ll DIType,
151    pub already_stored_in_typemap: bool,
152}
153
154impl<'ll> DINodeCreationResult<'ll> {
155    pub(crate) fn new(di_node: &'ll DIType, already_stored_in_typemap: bool) -> Self {
156        DINodeCreationResult { di_node, already_stored_in_typemap }
157    }
158}
159
160#[derive(#[automatically_derived]
impl<'ll> ::core::fmt::Debug for Stub<'ll> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            Stub::Struct => ::core::fmt::Formatter::write_str(f, "Struct"),
            Stub::Union => ::core::fmt::Formatter::write_str(f, "Union"),
            Stub::VTableTy { vtable_holder: __self_0 } =>
                ::core::fmt::Formatter::debug_struct_field1_finish(f,
                    "VTableTy", "vtable_holder", &__self_0),
        }
    }
}Debug, #[automatically_derived]
impl<'ll> ::core::marker::Copy for Stub<'ll> { }Copy, #[automatically_derived]
impl<'ll> ::core::clone::Clone for Stub<'ll> {
    #[inline]
    fn clone(&self) -> Stub<'ll> {
        let _: ::core::clone::AssertParamIsClone<&'ll DIType>;
        *self
    }
}Clone, #[automatically_derived]
impl<'ll> ::core::cmp::Eq for Stub<'ll> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<&'ll DIType>;
    }
}Eq, #[automatically_derived]
impl<'ll> ::core::cmp::PartialEq for Stub<'ll> {
    #[inline]
    fn eq(&self, other: &Stub<'ll>) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (Stub::VTableTy { vtable_holder: __self_0 }, Stub::VTableTy {
                    vtable_holder: __arg1_0 }) => __self_0 == __arg1_0,
                _ => true,
            }
    }
}PartialEq)]
161pub(crate) enum Stub<'ll> {
162    Struct,
163    Union,
164    VTableTy { vtable_holder: &'ll DIType },
165}
166
167pub(crate) struct StubInfo<'ll, 'tcx> {
168    metadata: &'ll DIType,
169    unique_type_id: UniqueTypeId<'tcx>,
170}
171
172impl<'ll, 'tcx> StubInfo<'ll, 'tcx> {
173    pub(super) fn new(
174        cx: &CodegenCx<'ll, 'tcx>,
175        unique_type_id: UniqueTypeId<'tcx>,
176        build: impl FnOnce(&CodegenCx<'ll, 'tcx>, /* unique_type_id_str: */ &str) -> &'ll DIType,
177    ) -> StubInfo<'ll, 'tcx> {
178        let unique_type_id_str = unique_type_id.generate_unique_id_string(cx.tcx);
179        let di_node = build(cx, &unique_type_id_str);
180        StubInfo { metadata: di_node, unique_type_id }
181    }
182}
183
184/// Create a stub debuginfo node onto which fields and nested types can be attached.
185pub(super) fn stub<'ll, 'tcx>(
186    cx: &CodegenCx<'ll, 'tcx>,
187    kind: Stub<'ll>,
188    unique_type_id: UniqueTypeId<'tcx>,
189    name: &str,
190    def_location: Option<DefinitionLocation<'ll>>,
191    (size, align): (Size, Align),
192    containing_scope: Option<&'ll DIScope>,
193    flags: DIFlags,
194) -> StubInfo<'ll, 'tcx> {
195    let no_elements: &[Option<&llvm::Metadata>] = &[];
196    let unique_type_id_str = unique_type_id.generate_unique_id_string(cx.tcx);
197
198    let (file_metadata, line_number) = if let Some(def_location) = def_location {
199        (def_location.0, def_location.1)
200    } else {
201        (unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
202    };
203
204    let metadata = match kind {
205        Stub::Struct | Stub::VTableTy { .. } => {
206            let vtable_holder = match kind {
207                Stub::VTableTy { vtable_holder } => Some(vtable_holder),
208                _ => None,
209            };
210            unsafe {
211                llvm::LLVMDIBuilderCreateStructType(
212                    DIB(cx),
213                    containing_scope,
214                    name.as_ptr(),
215                    name.len(),
216                    file_metadata,
217                    line_number,
218                    size.bits(),
219                    align.bits() as u32,
220                    flags,
221                    None,
222                    no_elements.as_ptr(),
223                    no_elements.len() as c_uint,
224                    0u32, // (Objective-C runtime version; default is 0)
225                    vtable_holder,
226                    unique_type_id_str.as_ptr(),
227                    unique_type_id_str.len(),
228                )
229            }
230        }
231        Stub::Union => unsafe {
232            llvm::LLVMDIBuilderCreateUnionType(
233                DIB(cx),
234                containing_scope,
235                name.as_ptr(),
236                name.len(),
237                file_metadata,
238                line_number,
239                size.bits(),
240                align.bits() as u32,
241                flags,
242                no_elements.as_ptr(),
243                no_elements.len() as c_uint,
244                0u32, // (Objective-C runtime version; default is 0)
245                unique_type_id_str.as_ptr(),
246                unique_type_id_str.len(),
247            )
248        },
249    };
250    StubInfo { metadata, unique_type_id }
251}
252
253struct AdtStackPopGuard<'ll, 'tcx, 'a> {
254    cx: &'a CodegenCx<'ll, 'tcx>,
255}
256
257impl<'ll, 'tcx, 'a> Drop for AdtStackPopGuard<'ll, 'tcx, 'a> {
258    fn drop(&mut self) {
259        debug_context(self.cx).adt_stack.borrow_mut().pop();
260    }
261}
262
263/// This function enables creating debuginfo nodes that can recursively refer to themselves.
264/// It will first insert the given stub into the type map and only then execute the `members`
265/// and `generics` closures passed in. These closures have access to the stub so they can
266/// directly attach fields to them. If the type of a field transitively refers back
267/// to the type currently being built, the stub will already be found in the type map,
268/// which effectively breaks the recursion cycle.
269pub(super) fn build_type_with_children<'ll, 'tcx>(
270    cx: &CodegenCx<'ll, 'tcx>,
271    stub_info: StubInfo<'ll, 'tcx>,
272    members: impl FnOnce(&CodegenCx<'ll, 'tcx>, &'ll DIType) -> SmallVec<&'ll DIType>,
273    generics: impl FnOnce(&CodegenCx<'ll, 'tcx>) -> SmallVec<Option<&'ll DIType>>,
274) -> DINodeCreationResult<'ll> {
275    match (&debug_context(cx).type_map.di_node_for_unique_id(stub_info.unique_type_id),
        &None) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(debug_context(cx).type_map.di_node_for_unique_id(stub_info.unique_type_id), None);
276
277    let mut _adt_stack_pop_guard = None;
278    if let UniqueTypeId::Ty(ty, ..) = stub_info.unique_type_id
279        && let ty::Adt(adt_def, args) = ty.kind()
280    {
281        let def_id = adt_def.did();
282        // If any child type references the original type definition and the child type has a type
283        // parameter that strictly contains the original parameter, the original type is a recursive
284        // type that can expanding indefinitely. Example,
285        // ```
286        // enum Recursive<T> {
287        //     Recurse(*const Recursive<Wrap<T>>),
288        //     Item(T),
289        // }
290        // ```
291        let is_expanding_recursive = {
292            let stack = debug_context(cx).adt_stack.borrow();
293            stack
294                .iter()
295                .enumerate()
296                .rev()
297                .skip(1)
298                .filter(|(_, (ancestor_def_id, _))| def_id == *ancestor_def_id)
299                .any(|(ancestor_index, (_, ancestor_args))| {
300                    args.iter()
301                        .zip(ancestor_args.iter())
302                        .filter_map(|(arg, ancestor_arg)| arg.as_type().zip(ancestor_arg.as_type()))
303                        .any(|(arg, ancestor_arg)|
304                            // Strictly contains.
305                            (arg != ancestor_arg && arg.contains(ancestor_arg))
306                            // Check all types between current and ancestor use the
307                            // ancestor_arg.
308                            // Otherwise, duplicate wrappers in normal recursive type may be
309                            // regarded as expanding.
310                            // ```
311                            // struct Recursive {
312                            //     a: Box<Box<Recursive>>,
313                            // }
314                            // ```
315                            // It can produce an ADT stack like this,
316                            // - Box<Recursive>
317                            // - Recursive
318                            // - Box<Box<Recursive>>
319                            && stack[ancestor_index + 1..stack.len()].iter().all(
320                                |(_, intermediate_args)|
321                                    intermediate_args
322                                        .iter()
323                                        .filter_map(|arg| arg.as_type())
324                                        .any(|mid_arg| mid_arg.contains(ancestor_arg))
325                            ))
326                })
327        };
328        if is_expanding_recursive {
329            // FIXME: indicate that this is an expanding recursive type in stub metadata?
330            return DINodeCreationResult::new(stub_info.metadata, false);
331        } else {
332            debug_context(cx).adt_stack.borrow_mut().push((def_id, args));
333            _adt_stack_pop_guard = Some(AdtStackPopGuard { cx });
334        }
335    }
336
337    debug_context(cx).type_map.insert(stub_info.unique_type_id, stub_info.metadata);
338
339    let members: SmallVec<_> =
340        members(cx, stub_info.metadata).into_iter().map(|node| Some(node)).collect();
341    let generics = generics(cx);
342
343    if !(members.is_empty() && generics.is_empty()) {
344        unsafe {
345            let members_array = create_DIArray(DIB(cx), &members[..]);
346            let generics_array = create_DIArray(DIB(cx), &generics[..]);
347            llvm::LLVMRustDICompositeTypeReplaceArrays(
348                DIB(cx),
349                stub_info.metadata,
350                Some(members_array),
351                Some(generics_array),
352            );
353        }
354    }
355
356    DINodeCreationResult { di_node: stub_info.metadata, already_stored_in_typemap: true }
357}