const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD);
if (RD->isUnion()) {
- Result = APValue(E->getInitializedFieldInUnion());
- if (!E->getNumInits())
+ const FieldDecl *Field = E->getInitializedFieldInUnion();
+ Result = APValue(Field);
+ if (!Field)
return true;
+
+ // If the initializer list for a union does not contain any elements, the
+ // first element of the union is value-initialized.
+ ImplicitValueInitExpr VIE(Field->getType());
+ const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE;
+
LValue Subobject = This;
- HandleLValueMember(Info, E->getInit(0), Subobject,
- E->getInitializedFieldInUnion(), &Layout);
+ HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout);
return EvaluateConstantExpression(Result.getUnionValue(), Info,
- Subobject, E->getInit(0));
+ Subobject, InitExpr);
}
assert((!isa<CXXRecordDecl>(RD) || !cast<CXXRecordDecl>(RD)->getNumBases()) &&
const CXXConstructorDecl *FD = E->getConstructor();
bool ZeroInit = E->requiresZeroInitialization();
if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) {
+ // If we've already performed zero-initialization, we're already done.
+ if (!Result.isUninit())
+ return true;
+
if (ZeroInit)
return ZeroInitialization(E);
// C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...]
// an appropriately-typed string literal enclosed in braces.
- if (E->getNumInits() == 1 && CAT->getElementType()->isAnyCharacterType() &&
+ if (E->getNumInits() == 1 && E->getInit(0)->isGLValue() &&
Info.Ctx.hasSameUnqualifiedType(E->getType(), E->getInit(0)->getType())) {
LValue LV;
if (!EvaluateLValue(E->getInit(0), LV, Info))
if (!CAT)
return Error(E);
- Result = APValue(APValue::UninitArray(), 0, CAT->getSize().getZExtValue());
+ bool HadZeroInit = !Result.isUninit();
+ if (!HadZeroInit)
+ Result = APValue(APValue::UninitArray(), 0, CAT->getSize().getZExtValue());
if (!Result.hasArrayFiller())
return true;
bool ZeroInit = E->requiresZeroInitialization();
if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) {
+ if (HadZeroInit)
+ return true;
+
if (ZeroInit) {
LValue Subobject = This;
Subobject.addArray(Info, E, CAT);
LValue Subobject = This;
Subobject.addArray(Info, E, CAT);
- if (ZeroInit) {
+ if (ZeroInit && !HadZeroInit) {
ImplicitValueInitExpr VIE(CAT->getElementType());
if (!EvaluateConstantExpression(Result.getArrayFiller(), Info, Subobject,
&VIE))
}
static_assert(MangleChars("constexpr!") == 1768383, "");
+static_assert(MangleChars(u8"constexpr!") == 1768383, "");
+static_assert(MangleChars(L"constexpr!") == 1768383, "");
static_assert(MangleChars(u"constexpr!") == 1768383, "");
static_assert(MangleChars(U"constexpr!") == 1768383, "");
static_assert(t.c[5] == 0, "");
static_assert(t.c[6] == 0, ""); // expected-error {{constant expression}} expected-note {{one-past-the-end}}
+struct U {
+ wchar_t chars[6];
+ int n;
+} constexpr u = { { L"test" }, 0 };
+static_assert(u.chars[2] == L's', "");
+
}
namespace Array {
static_assert(*(&(u[1].b) + 1 + 1) == 3, ""); // expected-error {{constant expression}} expected-note {{cannot refer to element 2 of non-array object}}
static_assert((&(u[1]) + 1 + 1)->b == 3, "");
+constexpr U v = {};
+static_assert(v.a == 0, "");
+
+union Empty {};
+constexpr Empty e = {};
+
// Make sure we handle trivial copy constructors for unions.
constexpr U x = {42};
constexpr U y = x;