@@ -4056,7 +4056,6 @@ LValue CodeGenFunction::EmitLValueForField(LValue base,
4056
4056
unsigned RecordCVR = base.getVRQualifiers ();
4057
4057
if (rec->isUnion ()) {
4058
4058
// For unions, there is no pointer adjustment.
4059
- assert (!FieldType->isReferenceType () && " union has reference member" );
4060
4059
if (CGM.getCodeGenOpts ().StrictVTablePointers &&
4061
4060
hasAnyVptr (FieldType, getContext ()))
4062
4061
// Because unions can easily skip invariant.barriers, we need to add
@@ -4073,27 +4072,30 @@ LValue CodeGenFunction::EmitLValueForField(LValue base,
4073
4072
addr.getPointer (), getDebugInfoFIndex (rec, field->getFieldIndex ()), DbgInfo),
4074
4073
addr.getAlignment ());
4075
4074
}
4076
- } else {
4077
4075
4076
+ if (FieldType->isReferenceType ())
4077
+ addr = Builder.CreateElementBitCast (
4078
+ addr, CGM.getTypes ().ConvertTypeForMem (FieldType), field->getName ());
4079
+ } else {
4078
4080
if (!IsInPreservedAIRegion)
4079
4081
// For structs, we GEP to the field that the record layout suggests.
4080
4082
addr = emitAddrOfFieldStorage (*this , addr, field);
4081
4083
else
4082
4084
// Remember the original struct field index
4083
4085
addr = emitPreserveStructAccess (*this , addr, field);
4086
+ }
4084
4087
4085
- // If this is a reference field, load the reference right now.
4086
- if (FieldType->isReferenceType ()) {
4087
- LValue RefLVal = MakeAddrLValue (addr, FieldType, FieldBaseInfo,
4088
- FieldTBAAInfo);
4089
- if (RecordCVR & Qualifiers::Volatile)
4090
- RefLVal.getQuals ().addVolatile ();
4091
- addr = EmitLoadOfReference (RefLVal, &FieldBaseInfo, &FieldTBAAInfo);
4092
-
4093
- // Qualifiers on the struct don't apply to the referencee.
4094
- RecordCVR = 0 ;
4095
- FieldType = FieldType->getPointeeType ();
4096
- }
4088
+ // If this is a reference field, load the reference right now.
4089
+ if (FieldType->isReferenceType ()) {
4090
+ LValue RefLVal =
4091
+ MakeAddrLValue (addr, FieldType, FieldBaseInfo, FieldTBAAInfo);
4092
+ if (RecordCVR & Qualifiers::Volatile)
4093
+ RefLVal.getQuals ().addVolatile ();
4094
+ addr = EmitLoadOfReference (RefLVal, &FieldBaseInfo, &FieldTBAAInfo);
4095
+
4096
+ // Qualifiers on the struct don't apply to the referencee.
4097
+ RecordCVR = 0 ;
4098
+ FieldType = FieldType->getPointeeType ();
4097
4099
}
4098
4100
4099
4101
// Make sure that the address is pointing to the right type. This is critical
0 commit comments