@@ -246,55 +246,7 @@ fn trans_stmt<'a, 'tcx: 'a>(
246
246
variant_index,
247
247
} => {
248
248
let place = trans_place ( fx, place) ;
249
- let layout = place. layout ( ) ;
250
- if layout. for_variant ( & * fx, * variant_index) . abi == layout:: Abi :: Uninhabited {
251
- return ;
252
- }
253
- match layout. variants {
254
- layout:: Variants :: Single { index } => {
255
- assert_eq ! ( index, * variant_index) ;
256
- }
257
- layout:: Variants :: Multiple {
258
- discr : _,
259
- discr_index,
260
- discr_kind : layout:: DiscriminantKind :: Tag ,
261
- variants : _,
262
- } => {
263
- let ptr = place. place_field ( fx, mir:: Field :: new ( discr_index) ) ;
264
- let to = layout
265
- . ty
266
- . discriminant_for_variant ( fx. tcx , * variant_index)
267
- . unwrap ( )
268
- . val ;
269
- let discr = CValue :: const_val ( fx, ptr. layout ( ) . ty , to) ;
270
- ptr. write_cvalue ( fx, discr) ;
271
- }
272
- layout:: Variants :: Multiple {
273
- discr : _,
274
- discr_index,
275
- discr_kind : layout:: DiscriminantKind :: Niche {
276
- dataful_variant,
277
- ref niche_variants,
278
- niche_start,
279
- } ,
280
- variants : _,
281
- } => {
282
- if * variant_index != dataful_variant {
283
- let niche = place. place_field ( fx, mir:: Field :: new ( discr_index) ) ;
284
- //let niche_llty = niche.layout.immediate_llvm_type(bx.cx);
285
- let niche_value =
286
- ( ( variant_index. as_u32 ( ) - niche_variants. start ( ) . as_u32 ( ) ) as u128 )
287
- . wrapping_add ( niche_start) ;
288
- // FIXME(eddyb) Check the actual primitive type here.
289
- let niche_llval = if niche_value == 0 {
290
- CValue :: const_val ( fx, niche. layout ( ) . ty , 0 )
291
- } else {
292
- CValue :: const_val ( fx, niche. layout ( ) . ty , niche_value)
293
- } ;
294
- niche. write_cvalue ( fx, niche_llval) ;
295
- }
296
- }
297
- }
249
+ crate :: discriminant:: codegen_set_discriminant ( fx, place, * variant_index) ;
298
250
}
299
251
StatementKind :: Assign ( to_place, rval) => {
300
252
let lval = trans_place ( fx, to_place) ;
@@ -435,7 +387,7 @@ fn trans_stmt<'a, 'tcx: 'a>(
435
387
// FIXME avoid forcing to stack
436
388
let place =
437
389
CPlace :: for_addr ( operand. force_stack ( fx) , operand. layout ( ) ) ;
438
- let discr = trans_get_discriminant ( fx, place, fx. layout_of ( to_ty) ) ;
390
+ let discr = crate :: discriminant :: codegen_get_discriminant ( fx, place, fx. layout_of ( to_ty) ) ;
439
391
lval. write_cvalue ( fx, discr) ;
440
392
} else {
441
393
let to_clif_ty = fx. clif_type ( to_ty) . unwrap ( ) ;
@@ -470,7 +422,7 @@ fn trans_stmt<'a, 'tcx: 'a>(
470
422
}
471
423
Rvalue :: Discriminant ( place) => {
472
424
let place = trans_place ( fx, place) ;
473
- let discr = trans_get_discriminant ( fx, place, dest_layout) ;
425
+ let discr = crate :: discriminant :: codegen_get_discriminant ( fx, place, dest_layout) ;
474
426
lval. write_cvalue ( fx, discr) ;
475
427
}
476
428
Rvalue :: Repeat ( operand, times) => {
@@ -619,89 +571,6 @@ fn codegen_array_len<'a, 'tcx: 'a>(
619
571
}
620
572
}
621
573
622
- pub fn trans_get_discriminant < ' a , ' tcx : ' a > (
623
- fx : & mut FunctionCx < ' a , ' tcx , impl Backend > ,
624
- place : CPlace < ' tcx > ,
625
- dest_layout : TyLayout < ' tcx > ,
626
- ) -> CValue < ' tcx > {
627
- let layout = place. layout ( ) ;
628
-
629
- if layout. abi == layout:: Abi :: Uninhabited {
630
- return trap_unreachable_ret_value ( fx, dest_layout, "[panic] Tried to get discriminant for uninhabited type." ) ;
631
- }
632
-
633
- let ( discr_scalar, discr_index, discr_kind) = match & layout. variants {
634
- layout:: Variants :: Single { index } => {
635
- let discr_val = layout
636
- . ty
637
- . ty_adt_def ( )
638
- . map_or ( u128:: from ( index. as_u32 ( ) ) , |def| {
639
- def. discriminant_for_variant ( fx. tcx , * index) . val
640
- } ) ;
641
- return CValue :: const_val ( fx, dest_layout. ty , discr_val) ;
642
- }
643
- layout:: Variants :: Multiple { discr, discr_index, discr_kind, variants : _ } => {
644
- ( discr, * discr_index, discr_kind)
645
- }
646
- } ;
647
-
648
- let discr = place. place_field ( fx, mir:: Field :: new ( discr_index) ) . to_cvalue ( fx) ;
649
- let discr_ty = discr. layout ( ) . ty ;
650
- let lldiscr = discr. load_scalar ( fx) ;
651
- match discr_kind {
652
- layout:: DiscriminantKind :: Tag => {
653
- let signed = match discr_scalar. value {
654
- layout:: Int ( _, signed) => signed,
655
- _ => false ,
656
- } ;
657
- let val = clif_intcast ( fx, lldiscr, fx. clif_type ( dest_layout. ty ) . unwrap ( ) , signed) ;
658
- return CValue :: by_val ( val, dest_layout) ;
659
- }
660
- layout:: DiscriminantKind :: Niche {
661
- dataful_variant,
662
- ref niche_variants,
663
- niche_start,
664
- } => {
665
- let niche_llty = fx. clif_type ( discr_ty) . unwrap ( ) ;
666
- let dest_clif_ty = fx. clif_type ( dest_layout. ty ) . unwrap ( ) ;
667
- if niche_variants. start ( ) == niche_variants. end ( ) {
668
- let b = fx
669
- . bcx
670
- . ins ( )
671
- . icmp_imm ( IntCC :: Equal , lldiscr, * niche_start as u64 as i64 ) ;
672
- let if_true = fx
673
- . bcx
674
- . ins ( )
675
- . iconst ( dest_clif_ty, niche_variants. start ( ) . as_u32 ( ) as i64 ) ;
676
- let if_false = fx
677
- . bcx
678
- . ins ( )
679
- . iconst ( dest_clif_ty, dataful_variant. as_u32 ( ) as i64 ) ;
680
- let val = fx. bcx . ins ( ) . select ( b, if_true, if_false) ;
681
- return CValue :: by_val ( val, dest_layout) ;
682
- } else {
683
- // Rebase from niche values to discriminant values.
684
- let delta = niche_start. wrapping_sub ( niche_variants. start ( ) . as_u32 ( ) as u128 ) ;
685
- let delta = fx. bcx . ins ( ) . iconst ( niche_llty, delta as u64 as i64 ) ;
686
- let lldiscr = fx. bcx . ins ( ) . isub ( lldiscr, delta) ;
687
- let b = fx. bcx . ins ( ) . icmp_imm (
688
- IntCC :: UnsignedLessThanOrEqual ,
689
- lldiscr,
690
- niche_variants. end ( ) . as_u32 ( ) as i64 ,
691
- ) ;
692
- let if_true =
693
- clif_intcast ( fx, lldiscr, fx. clif_type ( dest_layout. ty ) . unwrap ( ) , false ) ;
694
- let if_false = fx
695
- . bcx
696
- . ins ( )
697
- . iconst ( dest_clif_ty, dataful_variant. as_u32 ( ) as i64 ) ;
698
- let val = fx. bcx . ins ( ) . select ( b, if_true, if_false) ;
699
- return CValue :: by_val ( val, dest_layout) ;
700
- }
701
- }
702
- }
703
- }
704
-
705
574
pub fn trans_place < ' a , ' tcx : ' a > (
706
575
fx : & mut FunctionCx < ' a , ' tcx , impl Backend > ,
707
576
place : & Place < ' tcx > ,
0 commit comments