22// Alignment of 128 bit types is not currently handled, this will
33// need to be fixed when PowerPC vector support is added.
44
5- use rustc_abi:: { HasDataLayout , Integer , Numeric , TyAbiInterface } ;
5+ use rustc_abi:: { FieldsShape , HasDataLayout , Integer , Numeric , TyAbiInterface , TyAndLayout } ;
66
77use crate :: callconv:: { Align , ArgAbi , CastTarget , FnAbi , Reg , RegKind , Uniform } ;
88use crate :: spec:: { HasTargetSpec , LlvmAbi , Os } ;
@@ -15,6 +15,41 @@ enum ABI {
1515}
1616use ABI :: * ;
1717
18+ /// Whether `layout` is or contains a union.
19+ ///
20+ /// `homogeneous_aggregate` merges the fields of a union, so it cannot tell a union from a
21+ /// structure. This walks the layout a second time; keep the array handling here in sync with
22+ /// `homogeneous_aggregate`. ZSTs are ignored entirely, as both GCC and Clang do.
23+ ///
24+ /// This does not look at enums that are represented as unions at the ABI level (e.g. a
25+ /// `#[repr(C)]` enum with fields). That does not matter here: enums can only have integer
26+ /// discriminants, and `is_homogeneous_aggregate` rejects anything containing an integer, so an
27+ /// enum can never be a float or vector homogeneous aggregate.
28+ fn is_or_contains_union < ' a , Ty , C > ( cx : & C , layout : TyAndLayout < ' a , Ty > ) -> bool
29+ where
30+ Ty : TyAbiInterface < ' a , C > + Copy ,
31+ C : HasDataLayout ,
32+ {
33+ if layout. is_zst ( ) {
34+ return false ;
35+ }
36+ match layout. fields {
37+ FieldsShape :: Primitive => false ,
38+ // A `repr(transparent)` union is guaranteed to be ABI-compatible with its single
39+ // non-1-ZST field, so look through it instead of rejecting it.
40+ FieldsShape :: Union ( _) => {
41+ match layout. non_1zst_field ( cx) . filter ( |_| layout. is_transparent ( ) ) {
42+ Some ( ( _, field) ) => is_or_contains_union ( cx, field) ,
43+ None => true ,
44+ }
45+ }
46+ FieldsShape :: Array { .. } => is_or_contains_union ( cx, layout. field ( cx, 0 ) ) ,
47+ FieldsShape :: Arbitrary { .. } => {
48+ ( 0 ..layout. fields . count ( ) ) . any ( |i| is_or_contains_union ( cx, layout. field ( cx, i) ) )
49+ }
50+ }
51+ }
52+
1853fn is_homogeneous_aggregate < ' a , Ty , C > (
1954 cx : & C ,
2055 arg : & mut ArgAbi < ' a , Ty > ,
2762 arg. layout . homogeneous_aggregate ( cx) . ok ( ) . and_then ( |ha| ha. unit ( ) ) . and_then ( |unit| {
2863 // ELFv1 and AIX only passes one-member aggregates transparently.
2964 // ELFv2 passes up to eight uniquely addressable members.
30- if ( ( abi == ELFv1 || abi == AIX ) && arg. layout . size > unit. size )
65+ if ( ( abi == ELFv1 || abi == AIX )
66+ && ( arg. layout . size > unit. size || is_or_contains_union ( cx, arg. layout ) ) )
3167 || arg. layout . size > unit. size . checked_mul ( 8 , cx) . unwrap ( )
3268 {
3369 return None ;
0 commit comments