Skip to main content

rustc_codegen_llvm/debuginfo/metadata/enums/
mod.rs

1use std::borrow::Cow;
2
3use rustc_abi::{FieldIdx, TagEncoding, VariantIdx, Variants};
4use rustc_codegen_ssa::debuginfo::type_names::{compute_debuginfo_type_name, cpp_like_debuginfo};
5use rustc_codegen_ssa::debuginfo::{tag_base_type, wants_c_like_enum_debuginfo};
6use rustc_codegen_ssa::traits::MiscCodegenMethods;
7use rustc_hir::def::CtorKind;
8use rustc_index::IndexSlice;
9use rustc_middle::bug;
10use rustc_middle::mir::CoroutineLayout;
11use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
12use rustc_middle::ty::{self, AdtDef, CoroutineArgs, CoroutineArgsExt, Ty, VariantDef};
13use rustc_span::{Span, Symbol};
14
15use super::type_map::{DINodeCreationResult, UniqueTypeId};
16use super::{SmallVec, size_and_align_of};
17use crate::common::{AsCCharPtr, CodegenCx};
18use crate::debuginfo::metadata::type_map::{self, Stub};
19use crate::debuginfo::metadata::{
20    UNKNOWN_LINE_NUMBER, build_field_di_node, build_generic_type_param_di_nodes,
21    file_metadata_from_def_id, type_di_node, unknown_file_metadata,
22};
23use crate::debuginfo::utils::{DIB, create_DIArray, get_namespace_for_item};
24use crate::llvm::debuginfo::{DIFlags, DIType};
25use crate::llvm::{self, ToLlvmBool};
26
27mod cpp_like;
28mod native;
29
30pub(super) fn build_enum_type_di_node<'ll, 'tcx>(
31    cx: &CodegenCx<'ll, 'tcx>,
32    unique_type_id: UniqueTypeId<'tcx>,
33    span: Span,
34) -> DINodeCreationResult<'ll> {
35    let enum_type = unique_type_id.expect_ty();
36    let &ty::Adt(enum_adt_def, _) = enum_type.kind() else {
37        ::rustc_middle::util::bug::bug_fmt(format_args!("build_enum_type_di_node() called with non-enum type: `{0:?}`",
        enum_type))bug!("build_enum_type_di_node() called with non-enum type: `{:?}`", enum_type)
38    };
39
40    let enum_type_and_layout = cx.spanned_layout_of(enum_type, span);
41
42    if wants_c_like_enum_debuginfo(cx.tcx, enum_type_and_layout) {
43        return build_c_style_enum_di_node(cx, enum_adt_def, enum_type_and_layout);
44    }
45
46    if cpp_like_debuginfo(cx.tcx) {
47        cpp_like::build_enum_type_di_node(cx, unique_type_id)
48    } else {
49        native::build_enum_type_di_node(cx, unique_type_id)
50    }
51}
52
53pub(super) fn build_coroutine_di_node<'ll, 'tcx>(
54    cx: &CodegenCx<'ll, 'tcx>,
55    unique_type_id: UniqueTypeId<'tcx>,
56) -> DINodeCreationResult<'ll> {
57    if cpp_like_debuginfo(cx.tcx) {
58        cpp_like::build_coroutine_di_node(cx, unique_type_id)
59    } else {
60        native::build_coroutine_di_node(cx, unique_type_id)
61    }
62}
63
64/// Build the debuginfo node for a C-style enum, i.e. an enum the variants of which have no fields.
65///
66/// The resulting debuginfo will be a DW_TAG_enumeration_type.
67fn build_c_style_enum_di_node<'ll, 'tcx>(
68    cx: &CodegenCx<'ll, 'tcx>,
69    enum_adt_def: AdtDef<'tcx>,
70    enum_type_and_layout: TyAndLayout<'tcx>,
71) -> DINodeCreationResult<'ll> {
72    let containing_scope = get_namespace_for_item(cx, enum_adt_def.did());
73    let enum_adt_def_id = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
74        Some(enum_adt_def.did())
75    } else {
76        None
77    };
78    DINodeCreationResult {
79        di_node: build_enumeration_type_di_node(
80            cx,
81            &compute_debuginfo_type_name(cx.tcx, enum_type_and_layout.ty, false),
82            tag_base_type(cx.tcx, enum_type_and_layout),
83            enum_adt_def.discriminants(cx.tcx).map(|(variant_index, discr)| {
84                let name = Cow::from(enum_adt_def.variant(variant_index).name.as_str());
85                (name, discr.val)
86            }),
87            enum_adt_def_id,
88            containing_scope,
89        ),
90        already_stored_in_typemap: false,
91    }
92}
93
94/// Build a DW_TAG_enumeration_type debuginfo node, with the given base type and variants.
95/// This is a helper function and does not register anything in the type map by itself.
96///
97/// `variants` is an iterator of (discr-value, variant-name).
98fn build_enumeration_type_di_node<'ll, 'tcx>(
99    cx: &CodegenCx<'ll, 'tcx>,
100    type_name: &str,
101    base_type: Ty<'tcx>,
102    enumerators: impl Iterator<Item = (Cow<'tcx, str>, u128)>,
103    def_id: Option<rustc_span::def_id::DefId>,
104    containing_scope: &'ll DIType,
105) -> &'ll DIType {
106    let is_unsigned = match base_type.kind() {
107        ty::Int(_) => false,
108        ty::Uint(_) => true,
109        _ => ::rustc_middle::util::bug::bug_fmt(format_args!("build_enumeration_type_di_node() called with non-integer tag type."))bug!("build_enumeration_type_di_node() called with non-integer tag type."),
110    };
111    let (size, align) = cx.size_and_align_of(base_type);
112
113    let enumerator_di_nodes: SmallVec<Option<&'ll DIType>> = enumerators
114        .map(|(name, value)| {
115            let value_words = [value as u64, (value >> 64) as u64];
116            let size_in_bits = size.bits();
117            // LLVM computes `NumWords = (SizeInBits + 63) / 64`.
118            if !((size_in_bits + 63) / 64 <= value_words.len() as u64) {
    ::core::panicking::panic("assertion failed: (size_in_bits + 63) / 64 <= value_words.len() as u64")
};assert!((size_in_bits + 63) / 64 <= value_words.len() as u64);
119
120            let enumerator = unsafe {
121                llvm::LLVMDIBuilderCreateEnumeratorOfArbitraryPrecision(
122                    DIB(cx),
123                    name.as_ptr(),
124                    name.len(),
125                    size_in_bits,
126                    value_words.as_ptr(),
127                    is_unsigned.to_llvm_bool(),
128                )
129            };
130            Some(enumerator)
131        })
132        .collect();
133
134    let (file_metadata, line_number) = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers
135    {
136        file_metadata_from_def_id(cx, def_id)
137    } else {
138        (unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
139    };
140
141    unsafe {
142        llvm::LLVMRustDIBuilderCreateEnumerationType(
143            DIB(cx),
144            containing_scope,
145            type_name.as_c_char_ptr(),
146            type_name.len(),
147            file_metadata,
148            line_number,
149            size.bits(),
150            align.bits() as u32,
151            create_DIArray(DIB(cx), &enumerator_di_nodes[..]),
152            type_di_node(cx, base_type),
153            true,
154        )
155    }
156}
157
158/// Build the debuginfo node for the struct type describing a single variant of an enum.
159///
160/// ```txt
161///       DW_TAG_structure_type              (top-level type for enum)
162///         DW_TAG_variant_part              (variant part)
163///           DW_AT_discr                    (reference to discriminant DW_TAG_member)
164///           DW_TAG_member                  (discriminant member)
165///           DW_TAG_variant                 (variant 1)
166///           DW_TAG_variant                 (variant 2)
167///           DW_TAG_variant                 (variant 3)
168///  --->   DW_TAG_structure_type            (type of variant 1)
169///  --->   DW_TAG_structure_type            (type of variant 2)
170///  --->   DW_TAG_structure_type            (type of variant 3)
171/// ```
172///
173/// In CPP-like mode, we have the exact same descriptions for each variant too:
174///
175/// ```txt
176///       DW_TAG_union_type              (top-level type for enum)
177///         DW_TAG_member                    (member for variant 1)
178///         DW_TAG_member                    (member for variant 2)
179///         DW_TAG_member                    (member for variant 3)
180///  --->   DW_TAG_structure_type            (type of variant 1)
181///  --->   DW_TAG_structure_type            (type of variant 2)
182///  --->   DW_TAG_structure_type            (type of variant 3)
183///         DW_TAG_enumeration_type          (type of tag)
184/// ```
185///
186/// The node looks like:
187///
188/// ```txt
189/// DW_TAG_structure_type
190///   DW_AT_name                  <name-of-variant>
191///   DW_AT_byte_size             0x00000010
192///   DW_AT_alignment             0x00000008
193///   DW_TAG_member
194///     DW_AT_name                  <name-of-field-0>
195///     DW_AT_type                  <0x0000018e>
196///     DW_AT_alignment             0x00000004
197///     DW_AT_data_member_location  4
198///   DW_TAG_member
199///     DW_AT_name                  <name-of-field-1>
200///     DW_AT_type                  <0x00000195>
201///     DW_AT_alignment             0x00000008
202///     DW_AT_data_member_location  8
203///   ...
204/// ```
205///
206/// The type of a variant is always a struct type with the name of the variant
207/// and a DW_TAG_member for each field (but not the discriminant).
208fn build_enum_variant_struct_type_di_node<'ll, 'tcx>(
209    cx: &CodegenCx<'ll, 'tcx>,
210    enum_type_and_layout: TyAndLayout<'tcx>,
211    enum_type_di_node: &'ll DIType,
212    variant_index: VariantIdx,
213    variant_def: &VariantDef,
214    variant_layout: TyAndLayout<'tcx>,
215    di_flags: DIFlags,
216) -> &'ll DIType {
217    match (&variant_layout.ty, &enum_type_and_layout.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!(variant_layout.ty, enum_type_and_layout.ty);
218
219    let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
220        Some(file_metadata_from_def_id(cx, Some(variant_def.def_id)))
221    } else {
222        None
223    };
224
225    type_map::build_type_with_children(
226        cx,
227        type_map::stub(
228            cx,
229            Stub::Struct,
230            UniqueTypeId::for_enum_variant_struct_type(
231                cx.tcx,
232                enum_type_and_layout.ty,
233                variant_index,
234            ),
235            variant_def.name.as_str(),
236            def_location,
237            // NOTE: We use size and align of enum_type, not from variant_layout:
238            size_and_align_of(enum_type_and_layout),
239            Some(enum_type_di_node),
240            di_flags,
241        ),
242        |cx, struct_type_di_node| {
243            (0..variant_layout.fields.count())
244                .map(|field_index| {
245                    let field_name = if variant_def.ctor_kind() != Some(CtorKind::Fn) {
246                        // Fields have names
247                        let field = &variant_def.fields[FieldIdx::from_usize(field_index)];
248                        Cow::from(field.name.as_str())
249                    } else {
250                        // Tuple-like
251                        super::tuple_field_name(field_index)
252                    };
253
254                    let field_layout = variant_layout.field(cx, field_index);
255
256                    build_field_di_node(
257                        cx,
258                        struct_type_di_node,
259                        &field_name,
260                        field_layout,
261                        variant_layout.fields.offset(field_index),
262                        di_flags,
263                        type_di_node(cx, field_layout.ty),
264                        None,
265                    )
266                })
267                .collect::<SmallVec<_>>()
268        },
269        |cx| build_generic_type_param_di_nodes(cx, enum_type_and_layout.ty),
270    )
271    .di_node
272}
273
274/// Build the struct type for describing a single coroutine state.
275/// See [build_coroutine_variant_struct_type_di_node].
276///
277/// ```txt
278///
279///       DW_TAG_structure_type              (top-level type for enum)
280///         DW_TAG_variant_part              (variant part)
281///           DW_AT_discr                    (reference to discriminant DW_TAG_member)
282///           DW_TAG_member                  (discriminant member)
283///           DW_TAG_variant                 (variant 1)
284///           DW_TAG_variant                 (variant 2)
285///           DW_TAG_variant                 (variant 3)
286///  --->   DW_TAG_structure_type            (type of variant 1)
287///  --->   DW_TAG_structure_type            (type of variant 2)
288///  --->   DW_TAG_structure_type            (type of variant 3)
289///
290/// ```
291fn build_coroutine_variant_struct_type_di_node<'ll, 'tcx>(
292    cx: &CodegenCx<'ll, 'tcx>,
293    variant_index: VariantIdx,
294    coroutine_type_and_layout: TyAndLayout<'tcx>,
295    coroutine_type_di_node: &'ll DIType,
296    coroutine_layout: &CoroutineLayout<'tcx>,
297    common_upvar_names: &IndexSlice<FieldIdx, Symbol>,
298) -> &'ll DIType {
299    let variant_name = CoroutineArgs::variant_name(variant_index);
300    let unique_type_id = UniqueTypeId::for_enum_variant_struct_type(
301        cx.tcx,
302        coroutine_type_and_layout.ty,
303        variant_index,
304    );
305
306    let variant_layout = coroutine_type_and_layout.for_variant(cx, variant_index);
307
308    let coroutine_args = match coroutine_type_and_layout.ty.kind() {
309        ty::Coroutine(_, args) => args.as_coroutine(),
310        _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
311    };
312
313    type_map::build_type_with_children(
314        cx,
315        type_map::stub(
316            cx,
317            Stub::Struct,
318            unique_type_id,
319            &variant_name,
320            None,
321            size_and_align_of(coroutine_type_and_layout),
322            Some(coroutine_type_di_node),
323            DIFlags::FlagZero,
324        ),
325        |cx, variant_struct_type_di_node| {
326            // Fields that just belong to this variant/state
327            let state_specific_fields: SmallVec<_> = (0..variant_layout.fields.count())
328                .map(|field_index| {
329                    let coroutine_saved_local = coroutine_layout.variant_fields[variant_index]
330                        [FieldIdx::from_usize(field_index)];
331                    let field_name_maybe = coroutine_layout.field_names[coroutine_saved_local];
332                    let field_name = field_name_maybe
333                        .as_ref()
334                        .map(|s| Cow::from(s.as_str()))
335                        .unwrap_or_else(|| super::tuple_field_name(field_index));
336
337                    let field_type = variant_layout.field(cx, field_index).ty;
338
339                    build_field_di_node(
340                        cx,
341                        variant_struct_type_di_node,
342                        &field_name,
343                        cx.layout_of(field_type),
344                        variant_layout.fields.offset(field_index),
345                        DIFlags::FlagZero,
346                        type_di_node(cx, field_type),
347                        None,
348                    )
349                })
350                .collect();
351
352            // Fields that are common to all states
353            let common_fields: SmallVec<_> = coroutine_args
354                .prefix_tys()
355                .iter()
356                .zip(common_upvar_names)
357                .enumerate()
358                .map(|(index, (upvar_ty, upvar_name))| {
359                    build_field_di_node(
360                        cx,
361                        variant_struct_type_di_node,
362                        upvar_name.as_str(),
363                        cx.layout_of(upvar_ty),
364                        coroutine_type_and_layout.fields.offset(index),
365                        DIFlags::FlagZero,
366                        type_di_node(cx, upvar_ty),
367                        None,
368                    )
369                })
370                .collect();
371
372            state_specific_fields.into_iter().chain(common_fields).collect()
373        },
374        |cx| build_generic_type_param_di_nodes(cx, coroutine_type_and_layout.ty),
375    )
376    .di_node
377}
378
379#[derive(#[automatically_derived]
impl ::core::marker::Copy for DiscrResult { }Copy, #[automatically_derived]
impl ::core::clone::Clone for DiscrResult {
    #[inline]
    fn clone(&self) -> DiscrResult {
        let _: ::core::clone::AssertParamIsClone<u128>;
        *self
    }
}Clone)]
380enum DiscrResult {
381    NoDiscriminant,
382    Value(u128),
383    Range(u128, u128),
384}
385
386impl DiscrResult {
387    fn opt_single_val(&self) -> Option<u128> {
388        if let Self::Value(d) = *self { Some(d) } else { None }
389    }
390}
391
392/// Returns the discriminant value corresponding to the variant index.
393///
394/// Will return `None` if there is less than two variants (because then the enum won't have)
395/// a tag, and if this is the untagged variant of a niche-layout enum (because then there is no
396/// single discriminant value).
397fn compute_discriminant_value<'ll, 'tcx>(
398    cx: &CodegenCx<'ll, 'tcx>,
399    enum_type_and_layout: TyAndLayout<'tcx>,
400    variant_index: VariantIdx,
401) -> DiscrResult {
402    match enum_type_and_layout.layout.variants() {
403        &Variants::Single { .. } | &Variants::Empty => DiscrResult::NoDiscriminant,
404        &Variants::Multiple { tag_encoding: TagEncoding::Direct, .. } => DiscrResult::Value(
405            enum_type_and_layout.ty.discriminant_for_variant(cx.tcx, variant_index).unwrap().val,
406        ),
407        &Variants::Multiple {
408            tag_encoding: TagEncoding::Niche { ref niche_variants, niche_start, untagged_variant },
409            tag,
410            ..
411        } => {
412            if variant_index == untagged_variant {
413                let valid_range = enum_type_and_layout
414                    .for_variant(cx, variant_index)
415                    .largest_niche
416                    .as_ref()
417                    .unwrap()
418                    .valid_range;
419
420                let min = valid_range.start.min(valid_range.end);
421                let min = tag.size(cx).truncate(min);
422
423                let max = valid_range.start.max(valid_range.end);
424                let max = tag.size(cx).truncate(max);
425
426                DiscrResult::Range(min, max)
427            } else {
428                let value = (variant_index.as_u32() as u128)
429                    .wrapping_sub(niche_variants.start.as_u32() as u128)
430                    .wrapping_add(niche_start);
431                let value = tag.size(cx).truncate(value);
432                DiscrResult::Value(value)
433            }
434        }
435    }
436}