4242//! taken to it, implementing them for Rust seems difficult.
4343
4444pub use self :: Repr :: * ;
45+ use super :: Disr ;
4546
4647use std;
4748use std:: rc:: Rc ;
@@ -50,7 +51,6 @@ use llvm::{ValueRef, True, IntEQ, IntNE};
5051use back:: abi:: FAT_PTR_ADDR ;
5152use middle:: subst;
5253use middle:: ty:: { self , Ty } ;
53- use middle:: ty:: Disr ;
5454use syntax:: ast;
5555use syntax:: attr;
5656use syntax:: attr:: IntType ;
@@ -308,20 +308,20 @@ fn represent_type_uncached<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
308308
309309 if !dtor && cases. iter ( ) . all ( |c| c. tys . is_empty ( ) ) {
310310 // All bodies empty -> intlike
311- let discrs: Vec < u64 > = cases. iter ( ) . map ( |c| c. discr ) . collect ( ) ;
311+ let discrs: Vec < _ > = cases. iter ( ) . map ( |c| Disr :: from ( c. discr ) ) . collect ( ) ;
312312 let bounds = IntBounds {
313- ulo : * discrs. iter ( ) . min ( ) . unwrap ( ) ,
314- uhi : * discrs. iter ( ) . max ( ) . unwrap ( ) ,
315- slo : discrs. iter ( ) . map ( |n| * n as i64 ) . min ( ) . unwrap ( ) ,
316- shi : discrs. iter ( ) . map ( |n| * n as i64 ) . max ( ) . unwrap ( )
313+ ulo : discrs. iter ( ) . min ( ) . unwrap ( ) . 0 ,
314+ uhi : discrs. iter ( ) . max ( ) . unwrap ( ) . 0 ,
315+ slo : discrs. iter ( ) . map ( |n| n . 0 as i64 ) . min ( ) . unwrap ( ) ,
316+ shi : discrs. iter ( ) . map ( |n| n . 0 as i64 ) . max ( ) . unwrap ( )
317317 } ;
318318 return mk_cenum ( cx, hint, & bounds) ;
319319 }
320320
321321 // Since there's at least one
322322 // non-empty body, explicit discriminants should have
323323 // been rejected by a checker before this point.
324- if !cases. iter ( ) . enumerate ( ) . all ( |( i, c) | c. discr == ( i as Disr ) ) {
324+ if !cases. iter ( ) . enumerate ( ) . all ( |( i, c) | c. discr == Disr :: from ( i ) ) {
325325 cx. sess ( ) . bug ( & format ! ( "non-C-like enum {} with specified \
326326 discriminants",
327327 cx. tcx( ) . item_path_str( def. did) ) ) ;
@@ -347,7 +347,7 @@ fn represent_type_uncached<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
347347 match cases[ discr] . find_ptr ( cx) {
348348 Some ( ref df) if df. len ( ) == 1 && st. fields . len ( ) == 1 => {
349349 return RawNullablePointer {
350- nndiscr : discr as Disr ,
350+ nndiscr : Disr :: from ( discr ) ,
351351 nnty : st. fields [ 0 ] ,
352352 nullfields : cases[ 1 - discr] . tys . clone ( )
353353 } ;
@@ -356,7 +356,7 @@ fn represent_type_uncached<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
356356 discrfield. push ( 0 ) ;
357357 discrfield. reverse ( ) ;
358358 return StructWrappedNullablePointer {
359- nndiscr : discr as Disr ,
359+ nndiscr : Disr :: from ( discr ) ,
360360 nonnull : st,
361361 discrfield : discrfield,
362362 nullfields : cases[ 1 - discr] . tys . clone ( )
@@ -564,7 +564,7 @@ fn get_cases<'tcx>(tcx: &ty::ctxt<'tcx>,
564564 let field_tys = vi. fields . iter ( ) . map ( |field| {
565565 monomorphize:: field_ty ( tcx, substs, field)
566566 } ) . collect ( ) ;
567- Case { discr : vi. disr_val , tys : field_tys }
567+ Case { discr : Disr :: from ( vi. disr_val ) , tys : field_tys }
568568 } ) . collect ( )
569569}
570570
@@ -605,8 +605,8 @@ fn mk_cenum<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
605605 -> Repr < ' tcx > {
606606 let it = range_to_inttype ( cx, hint, bounds) ;
607607 match it {
608- attr:: SignedInt ( _) => CEnum ( it, bounds. slo as Disr , bounds. shi as Disr ) ,
609- attr:: UnsignedInt ( _) => CEnum ( it, bounds. ulo , bounds. uhi )
608+ attr:: SignedInt ( _) => CEnum ( it, Disr ( bounds. slo as u64 ) , Disr ( bounds. shi as u64 ) ) ,
609+ attr:: UnsignedInt ( _) => CEnum ( it, Disr ( bounds. ulo ) , Disr ( bounds. uhi ) )
610610 }
611611}
612612
@@ -923,11 +923,11 @@ pub fn trans_get_discr<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, r: &Repr<'tcx>,
923923 CEnum ( ity, min, max) => load_discr ( bcx, ity, scrutinee, min, max) ,
924924 General ( ity, ref cases, _) => {
925925 let ptr = StructGEP ( bcx, scrutinee, 0 ) ;
926- load_discr ( bcx, ity, ptr, 0 , ( cases. len ( ) - 1 ) as Disr )
926+ load_discr ( bcx, ity, ptr, Disr ( 0 ) , Disr ( cases. len ( ) as u64 - 1 ) )
927927 }
928928 Univariant ( ..) => C_u8 ( bcx. ccx ( ) , 0 ) ,
929929 RawNullablePointer { nndiscr, nnty, .. } => {
930- let cmp = if nndiscr == 0 { IntEQ } else { IntNE } ;
930+ let cmp = if nndiscr == Disr ( 0 ) { IntEQ } else { IntNE } ;
931931 let llptrty = type_of:: sizing_type_of ( bcx. ccx ( ) , nnty) ;
932932 ICmp ( bcx, cmp, Load ( bcx, scrutinee) , C_null ( llptrty) , DebugLoc :: None )
933933 }
@@ -945,7 +945,7 @@ fn struct_wrapped_nullable_bitdiscr(bcx: Block, nndiscr: Disr, discrfield: &Disc
945945 scrutinee : ValueRef ) -> ValueRef {
946946 let llptrptr = GEPi ( bcx, scrutinee, & discrfield[ ..] ) ;
947947 let llptr = Load ( bcx, llptrptr) ;
948- let cmp = if nndiscr == 0 { IntEQ } else { IntNE } ;
948+ let cmp = if nndiscr == Disr ( 0 ) { IntEQ } else { IntNE } ;
949949 ICmp ( bcx, cmp, llptr, C_null ( val_ty ( llptr) ) , DebugLoc :: None )
950950}
951951
@@ -957,10 +957,10 @@ fn load_discr(bcx: Block, ity: IntType, ptr: ValueRef, min: Disr, max: Disr)
957957 let bits = machine:: llbitsize_of_real ( bcx. ccx ( ) , llty) ;
958958 assert ! ( bits <= 64 ) ;
959959 let bits = bits as usize ;
960- let mask = ( !0u64 >> ( 64 - bits) ) as Disr ;
960+ let mask = Disr ( !0u64 >> ( 64 - bits) ) ;
961961 // For a (max) discr of -1, max will be `-1 as usize`, which overflows.
962962 // However, that is fine here (it would still represent the full range),
963- if ( max. wrapping_add ( 1 ) ) & mask == min & mask {
963+ if max. wrapping_add ( Disr ( 1 ) ) & mask == min & mask {
964964 // i.e., if the range is everything. The lo==hi case would be
965965 // rejected by the LLVM verifier (it would mean either an
966966 // empty set, which is impossible, or the entire range of the
@@ -969,7 +969,7 @@ fn load_discr(bcx: Block, ity: IntType, ptr: ValueRef, min: Disr, max: Disr)
969969 } else {
970970 // llvm::ConstantRange can deal with ranges that wrap around,
971971 // so an overflow on (max + 1) is fine.
972- LoadRangeAssert ( bcx, ptr, min, ( max. wrapping_add ( 1 ) ) , /* signed: */ True )
972+ LoadRangeAssert ( bcx, ptr, min, max. wrapping_add ( Disr ( 1 ) ) , /* signed: */ True )
973973 }
974974}
975975
@@ -981,18 +981,18 @@ pub fn trans_case<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, r: &Repr, discr: Disr)
981981 -> ValueRef {
982982 match * r {
983983 CEnum ( ity, _, _) => {
984- C_integral ( ll_inttype ( bcx. ccx ( ) , ity) , discr as u64 , true )
984+ C_integral ( ll_inttype ( bcx. ccx ( ) , ity) , discr. 0 , true )
985985 }
986986 General ( ity, _, _) => {
987- C_integral ( ll_inttype ( bcx. ccx ( ) , ity) , discr as u64 , true )
987+ C_integral ( ll_inttype ( bcx. ccx ( ) , ity) , discr. 0 , true )
988988 }
989989 Univariant ( ..) => {
990990 bcx. ccx ( ) . sess ( ) . bug ( "no cases for univariants or structs" )
991991 }
992992 RawNullablePointer { .. } |
993993 StructWrappedNullablePointer { .. } => {
994- assert ! ( discr == 0 || discr == 1 ) ;
995- C_bool ( bcx. ccx ( ) , discr != 0 )
994+ assert ! ( discr == Disr ( 0 ) || discr == Disr ( 1 ) ) ;
995+ C_bool ( bcx. ccx ( ) , discr != Disr ( 0 ) )
996996 }
997997 }
998998}
@@ -1004,20 +1004,20 @@ pub fn trans_set_discr<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, r: &Repr<'tcx>,
10041004 match * r {
10051005 CEnum ( ity, min, max) => {
10061006 assert_discr_in_range ( ity, min, max, discr) ;
1007- Store ( bcx, C_integral ( ll_inttype ( bcx. ccx ( ) , ity) , discr as u64 , true ) ,
1007+ Store ( bcx, C_integral ( ll_inttype ( bcx. ccx ( ) , ity) , discr. 0 , true ) ,
10081008 val) ;
10091009 }
10101010 General ( ity, ref cases, dtor) => {
10111011 if dtor_active ( dtor) {
10121012 let ptr = trans_field_ptr ( bcx, r, MaybeSizedValue :: sized ( val) , discr,
1013- cases[ discr as usize ] . fields . len ( ) - 2 ) ;
1013+ cases[ discr. 0 as usize ] . fields . len ( ) - 2 ) ;
10141014 Store ( bcx, C_u8 ( bcx. ccx ( ) , DTOR_NEEDED ) , ptr) ;
10151015 }
1016- Store ( bcx, C_integral ( ll_inttype ( bcx. ccx ( ) , ity) , discr as u64 , true ) ,
1016+ Store ( bcx, C_integral ( ll_inttype ( bcx. ccx ( ) , ity) , discr. 0 , true ) ,
10171017 StructGEP ( bcx, val, 0 ) ) ;
10181018 }
10191019 Univariant ( ref st, dtor) => {
1020- assert_eq ! ( discr, 0 ) ;
1020+ assert_eq ! ( discr, Disr ( 0 ) ) ;
10211021 if dtor_active ( dtor) {
10221022 Store ( bcx, C_u8 ( bcx. ccx ( ) , DTOR_NEEDED ) ,
10231023 StructGEP ( bcx, val, st. fields . len ( ) - 1 ) ) ;
@@ -1041,8 +1041,14 @@ pub fn trans_set_discr<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, r: &Repr<'tcx>,
10411041
10421042fn assert_discr_in_range ( ity : IntType , min : Disr , max : Disr , discr : Disr ) {
10431043 match ity {
1044- attr:: UnsignedInt ( _) => assert ! ( min <= discr && discr <= max) ,
1045- attr:: SignedInt ( _) => assert ! ( min as i64 <= discr as i64 && discr as i64 <= max as i64 )
1044+ attr:: UnsignedInt ( _) => {
1045+ assert ! ( min <= discr) ;
1046+ assert ! ( discr <= max)
1047+ } ,
1048+ attr:: SignedInt ( _) => {
1049+ assert ! ( min. 0 as i64 <= discr. 0 as i64 ) ;
1050+ assert ! ( discr. 0 as i64 <= max. 0 as i64 ) ;
1051+ } ,
10461052 }
10471053}
10481054
@@ -1052,11 +1058,11 @@ pub fn num_args(r: &Repr, discr: Disr) -> usize {
10521058 match * r {
10531059 CEnum ( ..) => 0 ,
10541060 Univariant ( ref st, dtor) => {
1055- assert_eq ! ( discr, 0 ) ;
1061+ assert_eq ! ( discr, Disr ( 0 ) ) ;
10561062 st. fields . len ( ) - ( if dtor_active ( dtor) { 1 } else { 0 } )
10571063 }
10581064 General ( _, ref cases, dtor) => {
1059- cases[ discr as usize ] . fields . len ( ) - 1 - ( if dtor_active ( dtor) { 1 } else { 0 } )
1065+ cases[ discr. 0 as usize ] . fields . len ( ) - 1 - ( if dtor_active ( dtor) { 1 } else { 0 } )
10601066 }
10611067 RawNullablePointer { nndiscr, ref nullfields, .. } => {
10621068 if discr == nndiscr { 1 } else { nullfields. len ( ) }
@@ -1079,11 +1085,11 @@ pub fn trans_field_ptr<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, r: &Repr<'tcx>,
10791085 bcx. ccx ( ) . sess ( ) . bug ( "element access in C-like enum" )
10801086 }
10811087 Univariant ( ref st, _dtor) => {
1082- assert_eq ! ( discr, 0 ) ;
1088+ assert_eq ! ( discr, Disr ( 0 ) ) ;
10831089 struct_field_ptr ( bcx, st, val, ix, false )
10841090 }
10851091 General ( _, ref cases, _) => {
1086- struct_field_ptr ( bcx, & cases[ discr as usize ] , val, ix + 1 , true )
1092+ struct_field_ptr ( bcx, & cases[ discr. 0 as usize ] , val, ix + 1 , true )
10871093 }
10881094 RawNullablePointer { nndiscr, ref nullfields, .. } |
10891095 StructWrappedNullablePointer { nndiscr, ref nullfields, .. } if discr != nndiscr => {
@@ -1326,20 +1332,20 @@ pub fn trans_const<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, r: &Repr<'tcx>, discr
13261332 CEnum ( ity, min, max) => {
13271333 assert_eq ! ( vals. len( ) , 0 ) ;
13281334 assert_discr_in_range ( ity, min, max, discr) ;
1329- C_integral ( ll_inttype ( ccx, ity) , discr as u64 , true )
1335+ C_integral ( ll_inttype ( ccx, ity) , discr. 0 , true )
13301336 }
13311337 General ( ity, ref cases, _) => {
1332- let case = & cases[ discr as usize ] ;
1338+ let case = & cases[ discr. 0 as usize ] ;
13331339 let ( max_sz, _) = union_size_and_align ( & cases[ ..] ) ;
1334- let lldiscr = C_integral ( ll_inttype ( ccx, ity) , discr as u64 , true ) ;
1340+ let lldiscr = C_integral ( ll_inttype ( ccx, ity) , discr. 0 as u64 , true ) ;
13351341 let mut f = vec ! [ lldiscr] ;
13361342 f. extend_from_slice ( vals) ;
13371343 let mut contents = build_const_struct ( ccx, case, & f[ ..] ) ;
13381344 contents. extend_from_slice ( & [ padding ( ccx, max_sz - case. size ) ] ) ;
13391345 C_struct ( ccx, & contents[ ..] , false )
13401346 }
13411347 Univariant ( ref st, _dro) => {
1342- assert ! ( discr == 0 ) ;
1348+ assert_eq ! ( discr, Disr ( 0 ) ) ;
13431349 let contents = build_const_struct ( ccx, st, vals) ;
13441350 C_struct ( ccx, & contents[ ..] , st. packed )
13451351 }
@@ -1444,17 +1450,17 @@ pub fn const_get_discrim(ccx: &CrateContext, r: &Repr, val: ValueRef) -> Disr {
14441450 match * r {
14451451 CEnum ( ity, _, _) => {
14461452 match ity {
1447- attr:: SignedInt ( ..) => const_to_int ( val) as Disr ,
1448- attr:: UnsignedInt ( ..) => const_to_uint ( val) as Disr
1453+ attr:: SignedInt ( ..) => Disr ( const_to_int ( val) as u64 ) ,
1454+ attr:: UnsignedInt ( ..) => Disr ( const_to_uint ( val) ) ,
14491455 }
14501456 }
14511457 General ( ity, _, _) => {
14521458 match ity {
1453- attr:: SignedInt ( ..) => const_to_int ( const_get_elt ( ccx, val, & [ 0 ] ) ) as Disr ,
1454- attr:: UnsignedInt ( ..) => const_to_uint ( const_get_elt ( ccx, val, & [ 0 ] ) ) as Disr
1459+ attr:: SignedInt ( ..) => Disr ( const_to_int ( const_get_elt ( ccx, val, & [ 0 ] ) ) as u64 ) ,
1460+ attr:: UnsignedInt ( ..) => Disr ( const_to_uint ( const_get_elt ( ccx, val, & [ 0 ] ) ) )
14551461 }
14561462 }
1457- Univariant ( ..) => 0 ,
1463+ Univariant ( ..) => Disr ( 0 ) ,
14581464 RawNullablePointer { .. } | StructWrappedNullablePointer { .. } => {
14591465 ccx. sess ( ) . bug ( "const discrim access of non c-like enum" )
14601466 }
0 commit comments