]> git.lizzy.rs Git - rust.git/blob - src/discriminant.rs
Sync discriminant getter and setter with upstream
[rust.git] / src / discriminant.rs
1 //! Adapted from https://github.com/rust-lang/rust/blob/d760df5aea483aae041c9a241e7acacf48f75035/src/librustc_codegen_ssa/mir/place.rs
2
3 use crate::prelude::*;
4
5 pub fn codegen_set_discriminant<'tcx>(
6     fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
7     place: CPlace<'tcx>,
8     variant_index: VariantIdx,
9 ) {
10     let layout = place.layout();
11     if layout.for_variant(fx, variant_index).abi.is_uninhabited() {
12         return;
13     }
14     match layout.variants {
15         layout::Variants::Single { index } => {
16             assert_eq!(index, variant_index);
17         }
18         layout::Variants::Multiple {
19             discr: _,
20             discr_index,
21             discr_kind: layout::DiscriminantKind::Tag,
22             variants: _,
23         } => {
24             let ptr = place.place_field(fx, mir::Field::new(discr_index));
25             let to = layout
26                 .ty
27                 .discriminant_for_variant(fx.tcx, variant_index)
28                 .unwrap()
29                 .val;
30             let discr = CValue::const_val(fx, ptr.layout().ty, to);
31             ptr.write_cvalue(fx, discr);
32         }
33         layout::Variants::Multiple {
34             discr: _,
35             discr_index,
36             discr_kind: layout::DiscriminantKind::Niche {
37                 dataful_variant,
38                 ref niche_variants,
39                 niche_start,
40             },
41             variants: _,
42         } => {
43             if variant_index != dataful_variant {
44                 let niche = place.place_field(fx, mir::Field::new(discr_index));
45                 let niche_value = variant_index.as_u32() - niche_variants.start().as_u32();
46                 let niche_value = u128::from(niche_value).wrapping_add(niche_start);
47                 let niche_llval = CValue::const_val(fx, niche.layout().ty, niche_value);
48                 niche.write_cvalue(fx, niche_llval);
49             }
50         }
51     }
52 }
53
54 pub fn codegen_get_discriminant<'tcx>(
55     fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
56     value: CValue<'tcx>,
57     dest_layout: TyLayout<'tcx>,
58 ) -> CValue<'tcx> {
59     let layout = value.layout();
60
61     if layout.abi == layout::Abi::Uninhabited {
62         return trap_unreachable_ret_value(fx, dest_layout, "[panic] Tried to get discriminant for uninhabited type.");
63     }
64
65     let (discr_scalar, discr_index, discr_kind) = match &layout.variants {
66         layout::Variants::Single { index } => {
67             let discr_val = layout
68                 .ty
69                 .discriminant_for_variant(fx.tcx, *index)
70                 .map_or(u128::from(index.as_u32()), |discr| discr.val);
71             return CValue::const_val(fx, dest_layout.ty, discr_val);
72         }
73         layout::Variants::Multiple { discr, discr_index, discr_kind, variants: _ } => {
74             (discr, *discr_index, discr_kind)
75         }
76     };
77
78     let cast_to = fx.clif_type(dest_layout.ty).unwrap();
79
80     // Read the tag/niche-encoded discriminant from memory.
81     let encoded_discr = value.value_field(fx, mir::Field::new(discr_index));
82     let encoded_discr = encoded_discr.load_scalar(fx);
83
84     // Decode the discriminant (specifically if it's niche-encoded).
85     match *discr_kind {
86         layout::DiscriminantKind::Tag => {
87             let signed = match discr_scalar.value {
88                 layout::Int(_, signed) => signed,
89                 _ => false
90             };
91             let val = clif_intcast(fx, encoded_discr, cast_to, signed);
92             return CValue::by_val(val, dest_layout);
93         }
94         layout::DiscriminantKind::Niche {
95             dataful_variant,
96             ref niche_variants,
97             niche_start,
98         } => {
99             // Rebase from niche values to discriminants, and check
100             // whether the result is in range for the niche variants.
101
102             // We first compute the "relative discriminant" (wrt `niche_variants`),
103             // that is, if `n = niche_variants.end() - niche_variants.start()`,
104             // we remap `niche_start..=niche_start + n` (which may wrap around)
105             // to (non-wrap-around) `0..=n`, to be able to check whether the
106             // discriminant corresponds to a niche variant with one comparison.
107             // We also can't go directly to the (variant index) discriminant
108             // and check that it is in the range `niche_variants`, because
109             // that might not fit in the same type, on top of needing an extra
110             // comparison (see also the comment on `let niche_discr`).
111             let relative_discr = if niche_start == 0 {
112                 encoded_discr
113             } else {
114                 // FIXME handle niche_start > i64::max_value()
115                 fx.bcx.ins().iadd_imm(encoded_discr, -i64::try_from(niche_start).unwrap())
116             };
117             let relative_max = niche_variants.end().as_u32() - niche_variants.start().as_u32();
118             let is_niche = {
119                 codegen_icmp_imm(fx, IntCC::UnsignedLessThanOrEqual, relative_discr, i128::from(relative_max))
120             };
121
122             // NOTE(eddyb) this addition needs to be performed on the final
123             // type, in case the niche itself can't represent all variant
124             // indices (e.g. `u8` niche with more than `256` variants,
125             // but enough uninhabited variants so that the remaining variants
126             // fit in the niche).
127             // In other words, `niche_variants.end - niche_variants.start`
128             // is representable in the niche, but `niche_variants.end`
129             // might not be, in extreme cases.
130             let niche_discr = {
131                 let relative_discr = if relative_max == 0 {
132                     // HACK(eddyb) since we have only one niche, we know which
133                     // one it is, and we can avoid having a dynamic value here.
134                     fx.bcx.ins().iconst(cast_to, 0)
135                 } else {
136                     clif_intcast(fx, relative_discr, cast_to, false)
137                 };
138                 fx.bcx.ins().iadd_imm(
139                     relative_discr,
140                     i64::from(niche_variants.start().as_u32()),
141                 )
142             };
143
144             let dataful_variant = fx.bcx.ins().iconst(cast_to, i64::from(dataful_variant.as_u32()));
145             let discr = fx.bcx.ins().select(
146                 is_niche,
147                 niche_discr,
148                 dataful_variant,
149             );
150             CValue::by_val(discr, dest_layout)
151         }
152     }
153 }