@@ -55,6 +55,7 @@ use middle::trans::common::*;
55
55
use middle:: trans:: machine;
56
56
use middle:: trans:: type_of;
57
57
use middle:: ty;
58
+ use middle:: ty:: Disr ;
58
59
use syntax:: ast;
59
60
use util:: ppaux:: ty_to_str;
60
61
@@ -64,7 +65,7 @@ use middle::trans::type_::Type;
64
65
/// Representations.
65
66
pub enum Repr {
66
67
/// C-like enums; basically an int.
67
- CEnum ( uint , uint ) , // discriminant range
68
+ CEnum ( Disr , Disr ) , // discriminant range
68
69
/**
69
70
* Single-case variants, and structs/tuples/records.
70
71
*
@@ -89,7 +90,7 @@ pub enum Repr {
89
90
* is represented such that `None` is a null pointer and `Some` is the
90
91
* identity function.
91
92
*/
92
- NullablePointer { nonnull : Struct , nndiscr : uint , ptrfield : uint ,
93
+ NullablePointer { nonnull : Struct , nndiscr : Disr , ptrfield : uint ,
93
94
nullfields : ~[ ty:: t ] }
94
95
}
95
96
@@ -140,7 +141,7 @@ fn represent_type_uncached(cx: &mut CrateContext, t: ty::t) -> Repr {
140
141
return Univariant ( mk_struct ( cx, ftys, packed) , dtor)
141
142
}
142
143
ty:: ty_enum( def_id, ref substs) => {
143
- struct Case { discr : uint , tys : ~[ ty:: t ] } ;
144
+ struct Case { discr : Disr , tys : ~[ ty:: t ] } ;
144
145
impl Case {
145
146
fn is_zerolen ( & self , cx : & mut CrateContext ) -> bool {
146
147
mk_struct ( cx, self . tys , false ) . size == 0
@@ -177,7 +178,7 @@ fn represent_type_uncached(cx: &mut CrateContext, t: ty::t) -> Repr {
177
178
// Since there's at least one
178
179
// non-empty body, explicit discriminants should have
179
180
// been rejected by a checker before this point.
180
- if !cases. iter ( ) . enumerate ( ) . all ( |( i, c) | c. discr == i ) {
181
+ if !cases. iter ( ) . enumerate ( ) . all ( |( i, c) | c. discr == ( i as Disr ) ) {
181
182
cx. sess . bug ( fmt ! ( "non-C-like enum %s with specified \
182
183
discriminants",
183
184
ty:: item_path_str( cx. tcx, def_id) ) )
@@ -305,16 +306,16 @@ pub fn trans_get_discr(bcx: @mut Block, r: &Repr, scrutinee: ValueRef)
305
306
-> ValueRef {
306
307
match * r {
307
308
CEnum ( min, max) => load_discr ( bcx, scrutinee, min, max) ,
308
- Univariant ( * ) => C_uint ( bcx. ccx ( ) , 0 ) ,
309
- General ( ref cases) => load_discr ( bcx, scrutinee, 0 , cases. len ( ) - 1 ) ,
309
+ Univariant ( * ) => C_disr ( bcx. ccx ( ) , 0 ) ,
310
+ General ( ref cases) => load_discr ( bcx, scrutinee, 0 , ( cases. len ( ) - 1 ) as Disr ) ,
310
311
NullablePointer { nonnull : ref nonnull, nndiscr, ptrfield, _ } => {
311
312
ZExt ( bcx, nullable_bitdiscr ( bcx, nonnull, nndiscr, ptrfield, scrutinee) ,
312
313
Type :: enum_discrim ( bcx. ccx ( ) ) )
313
314
}
314
315
}
315
316
}
316
317
317
- fn nullable_bitdiscr ( bcx : @mut Block , nonnull : & Struct , nndiscr : uint , ptrfield : uint ,
318
+ fn nullable_bitdiscr ( bcx : @mut Block , nonnull : & Struct , nndiscr : Disr , ptrfield : uint ,
318
319
scrutinee : ValueRef ) -> ValueRef {
319
320
let cmp = if nndiscr == 0 { IntEQ } else { IntNE } ;
320
321
let llptr = Load ( bcx, GEPi ( bcx, scrutinee, [ 0 , ptrfield] ) ) ;
@@ -323,7 +324,7 @@ fn nullable_bitdiscr(bcx: @mut Block, nonnull: &Struct, nndiscr: uint, ptrfield:
323
324
}
324
325
325
326
/// Helper for cases where the discriminant is simply loaded.
326
- fn load_discr ( bcx : @mut Block , scrutinee : ValueRef , min : uint , max : uint )
327
+ fn load_discr ( bcx : @mut Block , scrutinee : ValueRef , min : Disr , max : Disr )
327
328
-> ValueRef {
328
329
let ptr = GEPi ( bcx, scrutinee, [ 0 , 0 ] ) ;
329
330
if max + 1 == min {
@@ -347,16 +348,16 @@ fn load_discr(bcx: @mut Block, scrutinee: ValueRef, min: uint, max: uint)
347
348
*
348
349
* This should ideally be less tightly tied to `_match`.
349
350
*/
350
- pub fn trans_case ( bcx : @mut Block , r : & Repr , discr : uint ) -> _match:: opt_result {
351
+ pub fn trans_case ( bcx : @mut Block , r : & Repr , discr : Disr ) -> _match:: opt_result {
351
352
match * r {
352
353
CEnum ( * ) => {
353
- _match:: single_result ( rslt ( bcx, C_uint ( bcx. ccx ( ) , discr) ) )
354
+ _match:: single_result ( rslt ( bcx, C_disr ( bcx. ccx ( ) , discr) ) )
354
355
}
355
356
Univariant ( * ) => {
356
357
bcx. ccx ( ) . sess . bug ( "no cases for univariants or structs" )
357
358
}
358
359
General ( * ) => {
359
- _match:: single_result ( rslt ( bcx, C_uint ( bcx. ccx ( ) , discr) ) )
360
+ _match:: single_result ( rslt ( bcx, C_disr ( bcx. ccx ( ) , discr) ) )
360
361
}
361
362
NullablePointer { _ } => {
362
363
assert ! ( discr == 0 || discr == 1 ) ;
@@ -370,11 +371,11 @@ pub fn trans_case(bcx: @mut Block, r: &Repr, discr: uint) -> _match::opt_result
370
371
* representation. The fields, if any, should then be initialized via
371
372
* `trans_field_ptr`.
372
373
*/
373
- pub fn trans_start_init ( bcx : @mut Block , r : & Repr , val : ValueRef , discr : uint ) {
374
+ pub fn trans_start_init ( bcx : @mut Block , r : & Repr , val : ValueRef , discr : Disr ) {
374
375
match * r {
375
376
CEnum ( min, max) => {
376
377
assert ! ( min <= discr && discr <= max) ;
377
- Store ( bcx, C_uint ( bcx. ccx ( ) , discr) , GEPi ( bcx, val, [ 0 , 0 ] ) )
378
+ Store ( bcx, C_disr ( bcx. ccx ( ) , discr) , GEPi ( bcx, val, [ 0 , 0 ] ) )
378
379
}
379
380
Univariant ( ref st, true ) => {
380
381
assert_eq ! ( discr, 0 ) ;
@@ -385,7 +386,7 @@ pub fn trans_start_init(bcx: @mut Block, r: &Repr, val: ValueRef, discr: uint) {
385
386
assert_eq ! ( discr, 0 ) ;
386
387
}
387
388
General ( * ) => {
388
- Store ( bcx, C_uint ( bcx. ccx ( ) , discr) , GEPi ( bcx, val, [ 0 , 0 ] ) )
389
+ Store ( bcx, C_disr ( bcx. ccx ( ) , discr) , GEPi ( bcx, val, [ 0 , 0 ] ) )
389
390
}
390
391
NullablePointer { nonnull : ref nonnull, nndiscr, ptrfield, _ } => {
391
392
if discr != nndiscr {
@@ -401,7 +402,7 @@ pub fn trans_start_init(bcx: @mut Block, r: &Repr, val: ValueRef, discr: uint) {
401
402
* The number of fields in a given case; for use when obtaining this
402
403
* information from the type or definition is less convenient.
403
404
*/
404
- pub fn num_args ( r : & Repr , discr : uint ) -> uint {
405
+ pub fn num_args ( r : & Repr , discr : Disr ) -> uint {
405
406
match * r {
406
407
CEnum ( * ) => 0 ,
407
408
Univariant ( ref st, dtor) => {
@@ -416,7 +417,7 @@ pub fn num_args(r: &Repr, discr: uint) -> uint {
416
417
}
417
418
418
419
/// Access a field, at a point when the value's case is known.
419
- pub fn trans_field_ptr ( bcx : @mut Block , r : & Repr , val : ValueRef , discr : uint ,
420
+ pub fn trans_field_ptr ( bcx : @mut Block , r : & Repr , val : ValueRef , discr : Disr ,
420
421
ix : uint ) -> ValueRef {
421
422
// Note: if this ever needs to generate conditionals (e.g., if we
422
423
// decide to do some kind of cdr-coding-like non-unique repr
@@ -494,13 +495,13 @@ pub fn trans_drop_flag_ptr(bcx: @mut Block, r: &Repr, val: ValueRef) -> ValueRef
494
495
* this could be changed in the future to avoid allocating unnecessary
495
496
* space after values of shorter-than-maximum cases.
496
497
*/
497
- pub fn trans_const ( ccx : & mut CrateContext , r : & Repr , discr : uint ,
498
+ pub fn trans_const ( ccx : & mut CrateContext , r : & Repr , discr : Disr ,
498
499
vals : & [ ValueRef ] ) -> ValueRef {
499
500
match * r {
500
501
CEnum ( min, max) => {
501
502
assert_eq ! ( vals. len( ) , 0 ) ;
502
503
assert ! ( min <= discr && discr <= max) ;
503
- C_uint ( ccx, discr)
504
+ C_disr ( ccx, discr)
504
505
}
505
506
Univariant ( ref st, _dro) => {
506
507
assert_eq ! ( discr, 0 ) ;
@@ -509,7 +510,7 @@ pub fn trans_const(ccx: &mut CrateContext, r: &Repr, discr: uint,
509
510
General ( ref cases) => {
510
511
let case = & cases[ discr] ;
511
512
let max_sz = cases. iter ( ) . map ( |x| x. size ) . max ( ) . unwrap ( ) ;
512
- let discr_ty = C_uint ( ccx, discr) ;
513
+ let discr_ty = C_disr ( ccx, discr) ;
513
514
let contents = build_const_struct ( ccx, case,
514
515
~[ discr_ty] + vals) ;
515
516
C_struct ( contents + & [ padding ( max_sz - case. size ) ] )
@@ -581,15 +582,15 @@ fn roundup(x: u64, a: u64) -> u64 { ((x + (a - 1)) / a) * a }
581
582
582
583
/// Get the discriminant of a constant value. (Not currently used.)
583
584
pub fn const_get_discrim ( ccx : & mut CrateContext , r : & Repr , val : ValueRef )
584
- -> uint {
585
+ -> Disr {
585
586
match * r {
586
- CEnum ( * ) => const_to_uint ( val) as uint ,
587
+ CEnum ( * ) => const_to_uint ( val) as Disr ,
587
588
Univariant ( * ) => 0 ,
588
- General ( * ) => const_to_uint ( const_get_elt ( ccx, val, [ 0 ] ) ) as uint ,
589
+ General ( * ) => const_to_uint ( const_get_elt ( ccx, val, [ 0 ] ) ) as Disr ,
589
590
NullablePointer { nndiscr, ptrfield, _ } => {
590
591
if is_null ( const_struct_field ( ccx, val, ptrfield) ) {
591
592
/* subtraction as uint is ok because nndiscr is either 0 or 1 */
592
- ( 1 - nndiscr) as uint
593
+ ( 1 - nndiscr) as Disr
593
594
} else {
594
595
nndiscr
595
596
}
@@ -605,7 +606,7 @@ pub fn const_get_discrim(ccx: &mut CrateContext, r: &Repr, val: ValueRef)
605
606
* raw LLVM-level structs and arrays.)
606
607
*/
607
608
pub fn const_get_field ( ccx : & mut CrateContext , r : & Repr , val : ValueRef ,
608
- _discr : uint , ix : uint ) -> ValueRef {
609
+ _discr : Disr , ix : uint ) -> ValueRef {
609
610
match * r {
610
611
CEnum ( * ) => ccx. sess . bug ( "element access in C-like enum const" ) ,
611
612
Univariant ( * ) => const_struct_field ( ccx, val, ix) ,
@@ -644,3 +645,7 @@ pub fn is_newtypeish(r: &Repr) -> bool {
644
645
_ => false
645
646
}
646
647
}
648
+
649
+ fn C_disr ( cx : & CrateContext , i : Disr ) -> ValueRef {
650
+ return C_integral ( cx. int_type , i, false ) ;
651
+ }
0 commit comments