@@ -85,18 +85,6 @@ pub enum Repr {
85
85
* all start with a field for the discriminant.
86
86
*/
87
87
General ( IntType , Vec < Struct > ) ,
88
- /**
89
- * Two cases distinguished by a nullable pointer: the case with discriminant
90
- * `nndiscr` must have single field which is known to be nonnull due to its type.
91
- * The other case is known to be zero sized. Hence we represent the enum
92
- * as simply a nullable pointer: if not null it indicates the `nndiscr` variant,
93
- * otherwise it indicates the other case.
94
- */
95
- RawNullablePointer {
96
- pub nndiscr : Disr ,
97
- pub nnty : ty:: t ,
98
- pub nullfields : Vec < ty:: t >
99
- } ,
100
88
/**
101
89
* Two cases distinguished by a nullable pointer: the case with discriminant
102
90
* `nndiscr` is represented by the struct `nonnull`, where the `ptrfield`th
@@ -108,7 +96,7 @@ pub enum Repr {
108
96
* is represented such that `None` is a null pointer and `Some` is the
109
97
* identity function.
110
98
*/
111
- StructWrappedNullablePointer {
99
+ NullablePointer {
112
100
pub nonnull : Struct ,
113
101
pub nndiscr : Disr ,
114
102
pub ptrfield : uint ,
@@ -212,23 +200,17 @@ fn represent_type_uncached(cx: &CrateContext, t: ty::t) -> Repr {
212
200
if cases. get ( 1 - discr) . is_zerolen ( cx) {
213
201
match cases. get ( discr) . find_ptr ( ) {
214
202
Some ( ptrfield) => {
215
- let st = mk_struct ( cx, cases. get ( discr) . tys . as_slice ( ) ,
216
- false ) ;
217
-
218
- return if st. fields . len ( ) == 1 {
219
- RawNullablePointer {
220
- nndiscr : discr as Disr ,
221
- nnty : * st. fields . get ( 0 ) ,
222
- nullfields : cases. get ( 1 - discr) . tys . clone ( )
223
- }
224
- } else {
225
- StructWrappedNullablePointer {
226
- nndiscr : discr as Disr ,
227
- nonnull : st,
228
- ptrfield : ptrfield,
229
- nullfields : cases. get ( 1 - discr) . tys . clone ( )
230
- }
231
- } ;
203
+ return NullablePointer {
204
+ nndiscr : discr as u64 ,
205
+ nonnull : mk_struct ( cx,
206
+ cases. get ( discr)
207
+ . tys
208
+ . as_slice ( ) ,
209
+ false ) ,
210
+ ptrfield : ptrfield,
211
+ nullfields : cases. get ( 1 - discr) . tys
212
+ . clone ( )
213
+ }
232
214
}
233
215
None => { }
234
216
}
@@ -431,8 +413,8 @@ pub fn incomplete_type_of(cx: &CrateContext, r: &Repr, name: &str) -> Type {
431
413
}
432
414
pub fn finish_type_of ( cx : & CrateContext , r : & Repr , llty : & mut Type ) {
433
415
match * r {
434
- CEnum ( ..) | General ( ..) | RawNullablePointer { .. } => { }
435
- Univariant ( ref st, _) | StructWrappedNullablePointer { nonnull : ref st, .. } =>
416
+ CEnum ( ..) | General ( ..) => { }
417
+ Univariant ( ref st, _) | NullablePointer { nonnull : ref st, .. } =>
436
418
llty. set_struct_body ( struct_llfields ( cx, st, false ) . as_slice ( ) ,
437
419
st. packed )
438
420
}
@@ -441,8 +423,7 @@ pub fn finish_type_of(cx: &CrateContext, r: &Repr, llty: &mut Type) {
441
423
fn generic_type_of ( cx : & CrateContext , r : & Repr , name : Option < & str > , sizing : bool ) -> Type {
442
424
match * r {
443
425
CEnum ( ity, _, _) => ll_inttype ( cx, ity) ,
444
- RawNullablePointer { nnty, .. } => type_of:: sizing_type_of ( cx, nnty) ,
445
- Univariant ( ref st, _) | StructWrappedNullablePointer { nonnull : ref st, .. } => {
426
+ Univariant ( ref st, _) | NullablePointer { nonnull : ref st, .. } => {
446
427
match name {
447
428
None => {
448
429
Type :: struct_ ( cx, struct_llfields ( cx, st, sizing) . as_slice ( ) ,
@@ -514,10 +495,12 @@ fn struct_llfields(cx: &CrateContext, st: &Struct, sizing: bool) -> Vec<Type> {
514
495
pub fn trans_switch ( bcx : & Block , r : & Repr , scrutinee : ValueRef )
515
496
-> ( _match:: branch_kind , Option < ValueRef > ) {
516
497
match * r {
517
- CEnum ( ..) | General ( ..) |
518
- RawNullablePointer { .. } | StructWrappedNullablePointer { .. } => {
498
+ CEnum ( ..) | General ( ..) => {
519
499
( _match:: switch, Some ( trans_get_discr ( bcx, r, scrutinee, None ) ) )
520
500
}
501
+ NullablePointer { nonnull : ref nonnull, nndiscr, ptrfield, .. } => {
502
+ ( _match:: switch, Some ( nullable_bitdiscr ( bcx, nonnull, nndiscr, ptrfield, scrutinee) ) )
503
+ }
521
504
Univariant ( ..) => {
522
505
( _match:: single, None )
523
506
}
@@ -545,14 +528,8 @@ pub fn trans_get_discr(bcx: &Block, r: &Repr, scrutinee: ValueRef, cast_to: Opti
545
528
val = C_u8 ( bcx. ccx ( ) , 0 ) ;
546
529
signed = false ;
547
530
}
548
- RawNullablePointer { nndiscr, nnty, .. } => {
549
- let cmp = if nndiscr == 0 { IntEQ } else { IntNE } ;
550
- let llptrty = type_of:: sizing_type_of ( bcx. ccx ( ) , nnty) ;
551
- val = ICmp ( bcx, cmp, Load ( bcx, scrutinee) , C_null ( llptrty) ) ;
552
- signed = false ;
553
- }
554
- StructWrappedNullablePointer { nonnull : ref nonnull, nndiscr, ptrfield, .. } => {
555
- val = struct_wrapped_nullable_bitdiscr ( bcx, nonnull, nndiscr, ptrfield, scrutinee) ;
531
+ NullablePointer { nonnull : ref nonnull, nndiscr, ptrfield, .. } => {
532
+ val = nullable_bitdiscr ( bcx, nonnull, nndiscr, ptrfield, scrutinee) ;
556
533
signed = false ;
557
534
}
558
535
}
@@ -562,10 +539,10 @@ pub fn trans_get_discr(bcx: &Block, r: &Repr, scrutinee: ValueRef, cast_to: Opti
562
539
}
563
540
}
564
541
565
- fn struct_wrapped_nullable_bitdiscr ( bcx : & Block , nonnull : & Struct , nndiscr : Disr , ptrfield : uint ,
566
- scrutinee : ValueRef ) -> ValueRef {
567
- let llptr = Load ( bcx, GEPi ( bcx, scrutinee, [ 0 , ptrfield] ) ) ;
542
+ fn nullable_bitdiscr ( bcx : & Block , nonnull : & Struct , nndiscr : Disr , ptrfield : uint ,
543
+ scrutinee : ValueRef ) -> ValueRef {
568
544
let cmp = if nndiscr == 0 { IntEQ } else { IntNE } ;
545
+ let llptr = Load ( bcx, GEPi ( bcx, scrutinee, [ 0 , ptrfield] ) ) ;
569
546
let llptrty = type_of:: type_of ( bcx. ccx ( ) , * nonnull. fields . get ( ptrfield) ) ;
570
547
ICmp ( bcx, cmp, llptr, C_null ( llptrty) )
571
548
}
@@ -613,8 +590,7 @@ pub fn trans_case<'a>(bcx: &'a Block<'a>, r: &Repr, discr: Disr)
613
590
Univariant ( ..) => {
614
591
bcx. ccx ( ) . sess ( ) . bug ( "no cases for univariants or structs" )
615
592
}
616
- RawNullablePointer { .. } |
617
- StructWrappedNullablePointer { .. } => {
593
+ NullablePointer { .. } => {
618
594
assert ! ( discr == 0 || discr == 1 ) ;
619
595
_match:: single_result ( Result :: new ( bcx, C_i1 ( bcx. ccx ( ) , discr != 0 ) ) )
620
596
}
@@ -645,13 +621,7 @@ pub fn trans_start_init(bcx: &Block, r: &Repr, val: ValueRef, discr: Disr) {
645
621
Univariant ( ..) => {
646
622
assert_eq ! ( discr, 0 ) ;
647
623
}
648
- RawNullablePointer { nndiscr, nnty, ..} => {
649
- if discr != nndiscr {
650
- let llptrty = type_of:: sizing_type_of ( bcx. ccx ( ) , nnty) ;
651
- Store ( bcx, C_null ( llptrty) , val)
652
- }
653
- }
654
- StructWrappedNullablePointer { nonnull : ref nonnull, nndiscr, ptrfield, .. } => {
624
+ NullablePointer { nonnull : ref nonnull, nndiscr, ptrfield, .. } => {
655
625
if discr != nndiscr {
656
626
let llptrptr = GEPi ( bcx, val, [ 0 , ptrfield] ) ;
657
627
let llptrty = type_of:: type_of ( bcx. ccx ( ) ,
@@ -681,11 +651,8 @@ pub fn num_args(r: &Repr, discr: Disr) -> uint {
681
651
st. fields . len ( ) - ( if dtor { 1 } else { 0 } )
682
652
}
683
653
General ( _, ref cases) => cases. get ( discr as uint ) . fields . len ( ) - 1 ,
684
- RawNullablePointer { nndiscr, ref nullfields, .. } => {
685
- if discr == nndiscr { 1 } else { nullfields. len ( ) }
686
- }
687
- StructWrappedNullablePointer { nonnull : ref nonnull, nndiscr,
688
- nullfields : ref nullfields, .. } => {
654
+ NullablePointer { nonnull : ref nonnull, nndiscr,
655
+ nullfields : ref nullfields, .. } => {
689
656
if discr == nndiscr { nonnull. fields . len ( ) } else { nullfields. len ( ) }
690
657
}
691
658
}
@@ -708,25 +675,19 @@ pub fn trans_field_ptr(bcx: &Block, r: &Repr, val: ValueRef, discr: Disr,
708
675
General ( _, ref cases) => {
709
676
struct_field_ptr ( bcx, cases. get ( discr as uint ) , val, ix + 1 , true )
710
677
}
711
- RawNullablePointer { nndiscr, ref nullfields, .. } |
712
- StructWrappedNullablePointer { nndiscr, ref nullfields, .. } if discr != nndiscr => {
713
- // The unit-like case might have a nonzero number of unit-like fields.
714
- // (e.d., Result of Either with (), as one side.)
715
- let ty = type_of:: type_of ( bcx. ccx ( ) , * nullfields. get ( ix) ) ;
716
- assert_eq ! ( machine:: llsize_of_alloc( bcx. ccx( ) , ty) , 0 ) ;
717
- // The contents of memory at this pointer can't matter, but use
718
- // the value that's "reasonable" in case of pointer comparision.
719
- PointerCast ( bcx, val, ty. ptr_to ( ) )
720
- }
721
- RawNullablePointer { nndiscr, nnty, .. } => {
722
- assert_eq ! ( ix, 0 ) ;
723
- assert_eq ! ( discr, nndiscr) ;
724
- let ty = type_of:: type_of ( bcx. ccx ( ) , nnty) ;
725
- PointerCast ( bcx, val, ty. ptr_to ( ) )
726
- }
727
- StructWrappedNullablePointer { ref nonnull, nndiscr, .. } => {
728
- assert_eq ! ( discr, nndiscr) ;
729
- struct_field_ptr ( bcx, nonnull, val, ix, false )
678
+ NullablePointer { nonnull : ref nonnull, nullfields : ref nullfields,
679
+ nndiscr, .. } => {
680
+ if discr == nndiscr {
681
+ struct_field_ptr ( bcx, nonnull, val, ix, false )
682
+ } else {
683
+ // The unit-like case might have a nonzero number of unit-like fields.
684
+ // (e.g., Result or Either with () as one side.)
685
+ let ty = type_of:: type_of ( bcx. ccx ( ) , * nullfields. get ( ix) ) ;
686
+ assert_eq ! ( machine:: llsize_of_alloc( bcx. ccx( ) , ty) , 0 ) ;
687
+ // The contents of memory at this pointer can't matter, but use
688
+ // the value that's "reasonable" in case of pointer comparison.
689
+ PointerCast ( bcx, val, ty. ptr_to ( ) )
690
+ }
730
691
}
731
692
}
732
693
}
@@ -798,15 +759,7 @@ pub fn trans_const(ccx: &CrateContext, r: &Repr, discr: Disr,
798
759
let contents = build_const_struct ( ccx, st, vals) ;
799
760
C_struct ( ccx, contents. as_slice ( ) , st. packed )
800
761
}
801
- RawNullablePointer { nndiscr, nnty, .. } => {
802
- if discr == nndiscr {
803
- assert_eq ! ( vals. len( ) , 1 ) ;
804
- vals[ 0 ]
805
- } else {
806
- C_null ( type_of:: sizing_type_of ( ccx, nnty) )
807
- }
808
- }
809
- StructWrappedNullablePointer { nonnull : ref nonnull, nndiscr, .. } => {
762
+ NullablePointer { nonnull : ref nonnull, nndiscr, .. } => {
810
763
if discr == nndiscr {
811
764
C_struct ( ccx, build_const_struct ( ccx,
812
765
nonnull,
@@ -914,15 +867,7 @@ pub fn const_get_discrim(ccx: &CrateContext, r: &Repr, val: ValueRef)
914
867
}
915
868
}
916
869
Univariant ( ..) => 0 ,
917
- RawNullablePointer { nndiscr, .. } => {
918
- if is_null ( val) {
919
- /* subtraction as uint is ok because nndiscr is either 0 or 1 */
920
- ( 1 - nndiscr) as Disr
921
- } else {
922
- nndiscr
923
- }
924
- }
925
- StructWrappedNullablePointer { nndiscr, ptrfield, .. } => {
870
+ NullablePointer { nndiscr, ptrfield, .. } => {
926
871
if is_null ( const_struct_field ( ccx, val, ptrfield) ) {
927
872
/* subtraction as uint is ok because nndiscr is either 0 or 1 */
928
873
( 1 - nndiscr) as Disr
@@ -946,11 +891,7 @@ pub fn const_get_field(ccx: &CrateContext, r: &Repr, val: ValueRef,
946
891
CEnum ( ..) => ccx. sess ( ) . bug ( "element access in C-like enum const" ) ,
947
892
Univariant ( ..) => const_struct_field ( ccx, val, ix) ,
948
893
General ( ..) => const_struct_field ( ccx, val, ix + 1 ) ,
949
- RawNullablePointer { .. } => {
950
- assert_eq ! ( ix, 0 ) ;
951
- val
952
- }
953
- StructWrappedNullablePointer { .. } => const_struct_field ( ccx, val, ix)
894
+ NullablePointer { .. } => const_struct_field ( ccx, val, ix)
954
895
}
955
896
}
956
897
0 commit comments