@@ -24,6 +24,7 @@ use util::ppaux::ty_to_str;
24
24
25
25
// XXX: should this be done with boxed traits instead of ML-style?
26
26
pub enum Repr {
27
+ Unit ( int ) ,
27
28
CEnum ( int , int ) , /* discriminant range */
28
29
Univariant ( Struct , Destructor ) ,
29
30
General ( ~[ Struct ] )
@@ -77,8 +78,11 @@ pub fn represent_type(cx: @CrateContext, t: ty::t) -> Repr {
77
78
if cases. len ( ) == 0 {
78
79
// Uninhabitable; represent as unit
79
80
Univariant ( mk_struct ( cx, ~[ ] ) , NoDtor )
80
- } else if cases. len ( ) == 1 && cases[ 0 ] . discr == 0 {
81
+ } else if cases. len ( ) == 1 && cases[ 0 ] . tys . len ( ) == 0 {
82
+ Unit ( cases[ 0 ] . discr )
83
+ } else if cases. len ( ) == 1 {
81
84
// struct, tuple, newtype, etc.
85
+ assert cases[ 0 ] . discr == 0 ;
82
86
Univariant ( mk_struct ( cx, cases[ 0 ] . tys ) , NoDtor )
83
87
} else if cases. all ( |c| c. tys . len ( ) == 0 ) {
84
88
let discrs = cases. map ( |c| c. discr ) ;
@@ -116,6 +120,7 @@ pub fn fields_of(cx: @CrateContext, r: &Repr) -> ~[TypeRef] {
116
120
fn generic_fields_of ( cx : @CrateContext , r : & Repr , sizing : bool )
117
121
-> ~[ TypeRef ] {
118
122
match * r {
123
+ Unit ( * ) => ~[ ] ,
119
124
CEnum ( * ) => ~[ T_enum_discrim ( cx) ] ,
120
125
Univariant ( ref st, dt) => {
121
126
let f = if sizing {
@@ -160,7 +165,7 @@ pub fn trans_switch(bcx: block, r: &Repr, scrutinee: ValueRef) ->
160
165
CEnum ( min, max) => {
161
166
( _match:: switch, Some ( load_discr ( bcx, scrutinee, min, max) ) )
162
167
}
163
- Univariant ( * ) => {
168
+ Unit ( * ) | Univariant ( * ) => {
164
169
( _match:: single, None )
165
170
}
166
171
General ( ref cases) => {
@@ -175,7 +180,7 @@ pub fn trans_case(bcx: block, r: &Repr, discr: int) -> _match::opt_result {
175
180
CEnum ( * ) => {
176
181
_match:: single_result ( rslt ( bcx, C_int ( bcx. ccx ( ) , discr) ) )
177
182
}
178
- Univariant ( * ) => {
183
+ Unit ( * ) | Univariant ( * ) => {
179
184
bcx. ccx ( ) . sess . bug ( ~"no cases for univariants or structs")
180
185
}
181
186
General ( * ) => {
@@ -186,6 +191,9 @@ pub fn trans_case(bcx: block, r: &Repr, discr: int) -> _match::opt_result {
186
191
187
192
pub fn trans_set_discr ( bcx : block , r : & Repr , val : ValueRef , discr : int ) {
188
193
match * r {
194
+ Unit ( the_discr) => {
195
+ assert discr == the_discr;
196
+ }
189
197
CEnum ( min, max) => {
190
198
assert min <= discr && discr <= max;
191
199
Store ( bcx, C_int ( bcx. ccx ( ) , discr) , GEPi ( bcx, val, [ 0 , 0 ] ) )
@@ -205,7 +213,7 @@ pub fn trans_set_discr(bcx: block, r: &Repr, val: ValueRef, discr: int) {
205
213
206
214
pub fn num_args ( r : & Repr , discr : int ) -> uint {
207
215
match * r {
208
- CEnum ( * ) => 0 ,
216
+ Unit ( * ) | CEnum ( * ) => 0 ,
209
217
Univariant ( ref st, _dt) => { assert discr == 0 ; st. fields . len ( ) }
210
218
General ( ref cases) => cases[ discr as uint ] . fields . len ( )
211
219
}
@@ -217,7 +225,7 @@ pub fn trans_GEP(bcx: block, r: &Repr, val: ValueRef, discr: int, ix: uint)
217
225
// decide to do some kind of cdr-coding-like non-unique repr
218
226
// someday), it'll need to return a possibly-new bcx as well.
219
227
match * r {
220
- CEnum ( * ) => {
228
+ Unit ( * ) | CEnum ( * ) => {
221
229
bcx. ccx ( ) . sess . bug ( ~"element access in C -like enum")
222
230
}
223
231
Univariant ( ref st, dt) => {
@@ -253,6 +261,9 @@ fn struct_GEP(bcx: block, st: &Struct, val: ValueRef, ix: uint,
253
261
pub fn trans_const ( ccx : @CrateContext , r : & Repr , discr : int ,
254
262
vals : & [ ValueRef ] ) -> ValueRef {
255
263
match * r {
264
+ Unit ( * ) => {
265
+ C_struct ( ~[ ] )
266
+ }
256
267
CEnum ( min, max) => {
257
268
assert vals. len ( ) == 0 ;
258
269
assert min <= discr && discr <= max;
@@ -312,6 +323,7 @@ fn roundup(x: u64, a: u64) -> u64 { ((x + (a - 1)) / a) * a }
312
323
pub fn const_get_discrim ( ccx : @CrateContext , r : & Repr , val : ValueRef )
313
324
-> int {
314
325
match * r {
326
+ Unit ( discr) => discr,
315
327
CEnum ( * ) => const_to_int ( val) as int ,
316
328
Univariant ( * ) => 0 ,
317
329
General ( * ) => const_to_int ( const_get_elt ( ccx, val, [ 0 ] ) ) as int ,
@@ -322,7 +334,8 @@ pub fn const_get_element(ccx: @CrateContext, r: &Repr, val: ValueRef,
322
334
_discr : int , ix : uint ) -> ValueRef {
323
335
// Not to be confused with common::const_get_elt.
324
336
match * r {
325
- CEnum ( * ) => ccx. sess . bug ( ~"element access in C -like enum const ") ,
337
+ Unit ( * ) | CEnum ( * ) => ccx. sess . bug ( ~"element access in C -like enum \
338
+ const ") ,
326
339
Univariant ( * ) => const_struct_field ( ccx, val, ix) ,
327
340
General ( * ) => const_struct_field ( ccx, const_get_elt ( ccx, val,
328
341
[ 1 , 0 ] ) , ix)
0 commit comments