11
11
12
12
use lib:: llvm:: llvm;
13
13
use lib:: llvm:: { TypeRef } ;
14
+ use middle:: trans:: adt;
14
15
use middle:: trans:: base;
15
16
use middle:: trans:: common:: * ;
16
17
use middle:: trans:: common;
@@ -143,32 +144,12 @@ pub fn sizing_type_of(cx: @CrateContext, t: ty::t) -> TypeRef {
143
144
144
145
ty:: ty_unboxed_vec( mt) => T_vec ( cx, sizing_type_of ( cx, mt. ty ) ) ,
145
146
146
- ty:: ty_tup( ref elems) => {
147
- T_struct ( elems. map ( |& t| sizing_type_of ( cx, t) ) )
147
+ ty:: ty_tup( * ) | ty:: ty_rec( * ) | ty:: ty_struct( * )
148
+ | ty:: ty_enum( * ) => {
149
+ let repr = adt:: represent_type ( cx, t) ;
150
+ T_struct ( adt:: sizing_fields_of ( cx, & repr) )
148
151
}
149
152
150
- ty:: ty_rec( ref fields) => {
151
- T_struct ( fields. map ( |f| sizing_type_of ( cx, f. mt . ty ) ) )
152
- }
153
-
154
- ty:: ty_struct( def_id, ref substs) => {
155
- let fields = ty:: lookup_struct_fields ( cx. tcx , def_id) ;
156
- let lltype = T_struct ( fields. map ( |field| {
157
- let field_type = ty:: lookup_field_type ( cx. tcx ,
158
- def_id,
159
- field. id ,
160
- substs) ;
161
- sizing_type_of ( cx, field_type)
162
- } ) ) ;
163
- if ty:: ty_dtor ( cx. tcx , def_id) . is_present ( ) {
164
- T_struct ( ~[ lltype, T_i8 ( ) ] )
165
- } else {
166
- lltype
167
- }
168
- }
169
-
170
- ty:: ty_enum( def_id, _) => T_struct ( enum_body_types ( cx, def_id, t) ) ,
171
-
172
153
ty:: ty_self | ty:: ty_infer( * ) | ty:: ty_param( * ) | ty:: ty_err( * ) => {
173
154
cx. tcx . sess . bug (
174
155
fmt ! ( "fictitious type %? in sizing_type_of()" ,
@@ -257,28 +238,13 @@ pub fn type_of(cx: @CrateContext, t: ty::t) -> TypeRef {
257
238
T_array ( type_of ( cx, mt. ty ) , n)
258
239
}
259
240
260
- ty:: ty_rec( fields) => {
261
- let mut tys: ~[ TypeRef ] = ~[ ] ;
262
- for vec:: each( fields) |f| {
263
- let mt_ty = f. mt . ty ;
264
- tys. push ( type_of ( cx, mt_ty) ) ;
265
- }
266
-
267
- // n.b.: introduce an extra layer of indirection to match
268
- // structs
269
- T_struct ( ~[ T_struct ( tys) ] )
270
- }
271
-
272
241
ty:: ty_bare_fn( _) => T_ptr ( type_of_fn_from_ty ( cx, t) ) ,
273
242
ty:: ty_closure( _) => T_fn_pair ( cx, type_of_fn_from_ty ( cx, t) ) ,
274
243
ty:: ty_trait( _, _, vstore) => T_opaque_trait ( cx, vstore) ,
275
244
ty:: ty_type => T_ptr ( cx. tydesc_type ) ,
276
- ty:: ty_tup( elts) => {
277
- let mut tys = ~[ ] ;
278
- for vec:: each( elts) |elt| {
279
- tys. push ( type_of ( cx, * elt) ) ;
280
- }
281
- T_struct ( tys)
245
+ ty:: ty_tup( * ) | ty:: ty_rec( * ) => {
246
+ let repr = adt:: represent_type ( cx, t) ;
247
+ T_struct ( adt:: fields_of ( cx, & repr) )
282
248
}
283
249
ty:: ty_opaque_closure_ptr( _) => T_opaque_box_ptr ( cx) ,
284
250
ty:: ty_struct( did, ref substs) => {
@@ -301,59 +267,16 @@ pub fn type_of(cx: @CrateContext, t: ty::t) -> TypeRef {
301
267
302
268
// If this was an enum or struct, fill in the type now.
303
269
match ty:: get ( t) . sty {
304
- ty:: ty_enum( did, _) => {
305
- fill_type_of_enum ( cx, did, t, llty) ;
306
- }
307
- ty:: ty_struct( did, ref substs) => {
308
- // Only instance vars are record fields at runtime.
309
- let fields = ty:: lookup_struct_fields ( cx. tcx , did) ;
310
- let mut tys = do vec:: map ( fields) |f| {
311
- let t = ty:: lookup_field_type ( cx. tcx , did, f. id , substs) ;
312
- type_of ( cx, t)
313
- } ;
314
-
315
- // include a byte flag if there is a dtor so that we know when we've
316
- // been dropped
317
- if ty:: ty_dtor ( cx. tcx , did) . is_present ( ) {
318
- common:: set_struct_body ( llty, ~[ T_struct ( tys) , T_i8 ( ) ] ) ;
319
- } else {
320
- common:: set_struct_body ( llty, ~[ T_struct ( tys) ] ) ;
321
- }
270
+ ty:: ty_enum( * ) | ty:: ty_struct( * ) => {
271
+ let repr = adt:: represent_type ( cx, t) ;
272
+ common:: set_struct_body ( llty, adt:: fields_of ( cx, & repr) ) ;
322
273
}
323
274
_ => ( )
324
275
}
325
276
326
277
return llty;
327
278
}
328
279
329
- pub fn enum_body_types ( cx : @CrateContext , did : ast:: def_id , t : ty:: t )
330
- -> ~[ TypeRef ] {
331
- let univar = ty:: enum_is_univariant ( cx. tcx , did) ;
332
- if !univar {
333
- let size = machine:: static_size_of_enum ( cx, t) ;
334
- ~[ T_enum_discrim ( cx) , T_array ( T_i8 ( ) , size) ]
335
- }
336
- else {
337
- // Use the actual fields, so we get the alignment right.
338
- match ty:: get ( t) . sty {
339
- ty:: ty_enum( _, ref substs) => {
340
- do ty:: enum_variants ( cx. tcx , did) [ 0 ] . args . map |& field_ty| {
341
- sizing_type_of ( cx, ty:: subst ( cx. tcx , substs, field_ty) )
342
- }
343
- }
344
- _ => cx. sess . bug ( ~"enum is not an enum")
345
- }
346
- }
347
- }
348
-
349
- pub fn fill_type_of_enum ( cx : @CrateContext ,
350
- did : ast:: def_id ,
351
- t : ty:: t ,
352
- llty : TypeRef ) {
353
- debug ! ( "type_of_enum %?: %?" , t, ty:: get( t) ) ;
354
- common:: set_struct_body ( llty, enum_body_types ( cx, did, t) ) ;
355
- }
356
-
357
280
// Want refinements! (Or case classes, I guess
358
281
pub enum named_ty { a_struct, an_enum }
359
282
0 commit comments