@@ -1053,9 +1053,6 @@ bool ByteCodeExprGen<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
1053
1053
if (Inits.size () == 1 && E->getType () == Inits[0 ]->getType ())
1054
1054
return this ->visitInitializer (Inits[0 ]);
1055
1055
1056
- if (Inits.size () == 0 )
1057
- return this ->emitFinishInit (E);
1058
-
1059
1056
auto initPrimitiveField = [=](const Record::Field *FieldToInit,
1060
1057
const Expr *Init, PrimType T) -> bool {
1061
1058
if (!this ->visit (Init))
@@ -1083,24 +1080,38 @@ bool ByteCodeExprGen<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
1083
1080
};
1084
1081
1085
1082
if (R->isUnion ()) {
1086
- assert (Inits.size () == 1 );
1087
- const Expr *Init = Inits[0 ];
1088
- const FieldDecl *FToInit = nullptr ;
1089
- if (const auto *ILE = dyn_cast<InitListExpr>(E))
1090
- FToInit = ILE->getInitializedFieldInUnion ();
1091
- else
1092
- FToInit = cast<CXXParenListInitExpr>(E)->getInitializedFieldInUnion ();
1093
-
1094
- if (!this ->emitDupPtr (E))
1095
- return false ;
1096
-
1097
- const Record::Field *FieldToInit = R->getField (FToInit);
1098
- if (std::optional<PrimType> T = classify (Init)) {
1099
- if (!initPrimitiveField (FieldToInit, Init, *T))
1100
- return false ;
1083
+ if (Inits.size () == 0 ) {
1084
+ // Zero-initialize the first union field.
1085
+ if (R->getNumFields () == 0 )
1086
+ return this ->emitFinishInit (E);
1087
+ const Record::Field *FieldToInit = R->getField (0u );
1088
+ QualType FieldType = FieldToInit->Desc ->getType ();
1089
+ if (std::optional<PrimType> T = classify (FieldType)) {
1090
+ if (!this ->visitZeroInitializer (*T, FieldType, E))
1091
+ return false ;
1092
+ if (!this ->emitInitField (*T, FieldToInit->Offset , E))
1093
+ return false ;
1094
+ }
1095
+ // FIXME: Non-primitive case?
1101
1096
} else {
1102
- if (!initCompositeField (FieldToInit, Init))
1097
+ const Expr *Init = Inits[0 ];
1098
+ const FieldDecl *FToInit = nullptr ;
1099
+ if (const auto *ILE = dyn_cast<InitListExpr>(E))
1100
+ FToInit = ILE->getInitializedFieldInUnion ();
1101
+ else
1102
+ FToInit = cast<CXXParenListInitExpr>(E)->getInitializedFieldInUnion ();
1103
+
1104
+ if (!this ->emitDupPtr (E))
1103
1105
return false ;
1106
+
1107
+ const Record::Field *FieldToInit = R->getField (FToInit);
1108
+ if (std::optional<PrimType> T = classify (Init)) {
1109
+ if (!initPrimitiveField (FieldToInit, Init, *T))
1110
+ return false ;
1111
+ } else {
1112
+ if (!initCompositeField (FieldToInit, Init))
1113
+ return false ;
1114
+ }
1104
1115
}
1105
1116
return this ->emitFinishInit (E);
1106
1117
}
0 commit comments