@@ -339,6 +339,16 @@ impl Integer {
339
339
}
340
340
}
341
341
342
+ pub fn align ( & self , dl : & TargetDataLayout ) -> Align {
343
+ match * self {
344
+ I1 => dl. i1_align ,
345
+ I8 => dl. i8_align ,
346
+ I16 => dl. i16_align ,
347
+ I32 => dl. i32_align ,
348
+ I64 => dl. i64_align ,
349
+ }
350
+ }
351
+
342
352
pub fn to_ty < ' a , ' tcx > ( & self , tcx : & ty:: TyCtxt < ' a , ' tcx , ' tcx > ,
343
353
signed : bool ) -> Ty < ' tcx > {
344
354
match ( * self , signed) {
@@ -377,6 +387,18 @@ impl Integer {
377
387
}
378
388
}
379
389
390
+ //Find the smallest integer with the given alignment.
391
+ pub fn for_abi_align ( dl : & TargetDataLayout , align : Align ) -> Option < Integer > {
392
+ let wanted = align. abi ( ) ;
393
+ for & candidate in & [ I8 , I16 , I32 , I64 ] {
394
+ let ty = Int ( candidate) ;
395
+ if wanted == ty. align ( dl) . abi ( ) && wanted == ty. size ( dl) . bytes ( ) {
396
+ return Some ( candidate) ;
397
+ }
398
+ }
399
+ None
400
+ }
401
+
380
402
/// Get the Integer type from an attr::IntType.
381
403
pub fn from_attr ( dl : & TargetDataLayout , ity : attr:: IntType ) -> Integer {
382
404
match ity {
@@ -1149,20 +1171,7 @@ impl<'a, 'gcx, 'tcx> Layout {
1149
1171
// won't be so conservative.
1150
1172
1151
1173
// Use the initial field alignment
1152
- let wanted = start_align. abi ( ) ;
1153
- let mut ity = min_ity;
1154
- for & candidate in & [ I16 , I32 , I64 ] {
1155
- let ty = Int ( candidate) ;
1156
- if wanted == ty. align ( dl) . abi ( ) && wanted == ty. size ( dl) . bytes ( ) {
1157
- ity = candidate;
1158
- break ;
1159
- }
1160
- }
1161
-
1162
- // FIXME(eddyb) conservative only to avoid diverging from trans::adt.
1163
- if align. abi ( ) != start_align. abi ( ) {
1164
- ity = min_ity;
1165
- }
1174
+ let mut ity = Integer :: for_abi_align ( dl, start_align) . unwrap_or ( min_ity) ;
1166
1175
1167
1176
// If the alignment is not larger than the chosen discriminant size,
1168
1177
// don't use the alignment as the final size.
0 commit comments