A union can have a constexpr defaulted default constructor, if it has an
in-class initializer for one of its fields. Value-initialization of such a type should use the in-class initializer! The former was just a bug, the latter is a (reported) standard defect. llvm-svn: 156274
This commit is contained in:
parent
0f09df1f52
commit
e2648bac3a
|
@ -357,6 +357,9 @@ class CXXRecordDecl : public RecordDecl {
|
|||
/// \brief True if there no non-field members declared by the user.
|
||||
bool HasOnlyCMembers : 1;
|
||||
|
||||
/// \brief True if any field has an in-class initializer.
|
||||
bool HasInClassInitializer : 1;
|
||||
|
||||
/// HasTrivialDefaultConstructor - True when, if this class has a default
|
||||
/// constructor, this default constructor is trivial.
|
||||
///
|
||||
|
@ -1040,6 +1043,10 @@ public:
|
|||
/// no base classes, and no virtual functions (C++ [dcl.init.aggr]p1).
|
||||
bool isAggregate() const { return data().Aggregate; }
|
||||
|
||||
/// hasInClassInitializer - Whether this class has any in-class initializers
|
||||
/// for non-static data members.
|
||||
bool hasInClassInitializer() const { return data().HasInClassInitializer; }
|
||||
|
||||
/// isPOD - Whether this class is a POD-type (C++ [class]p4), which is a class
|
||||
/// that is an aggregate that has no non-static non-POD data members, no
|
||||
/// reference data members, no user-defined copy assignment operator and no
|
||||
|
@ -1091,7 +1098,8 @@ public:
|
|||
/// defaultedDefaultConstructorIsConstexpr - Whether a defaulted default
|
||||
/// constructor for this class would be constexpr.
|
||||
bool defaultedDefaultConstructorIsConstexpr() const {
|
||||
return data().DefaultedDefaultConstructorIsConstexpr;
|
||||
return data().DefaultedDefaultConstructorIsConstexpr &&
|
||||
(!isUnion() || hasInClassInitializer());
|
||||
}
|
||||
|
||||
/// defaultedCopyConstructorIsConstexpr - Whether a defaulted copy
|
||||
|
@ -1111,7 +1119,7 @@ public:
|
|||
bool hasConstexprDefaultConstructor() const {
|
||||
return data().HasConstexprDefaultConstructor ||
|
||||
(!data().UserDeclaredConstructor &&
|
||||
data().DefaultedDefaultConstructorIsConstexpr && isLiteral());
|
||||
defaultedDefaultConstructorIsConstexpr() && isLiteral());
|
||||
}
|
||||
|
||||
/// hasConstexprCopyConstructor - Whether this class has a constexpr copy
|
||||
|
|
|
@ -1852,6 +1852,7 @@ bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
|
|||
ToData.HasPublicFields = FromData.HasPublicFields;
|
||||
ToData.HasMutableFields = FromData.HasMutableFields;
|
||||
ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
|
||||
ToData.HasInClassInitializer = FromData.HasInClassInitializer;
|
||||
ToData.HasTrivialDefaultConstructor = FromData.HasTrivialDefaultConstructor;
|
||||
ToData.HasConstexprNonCopyMoveConstructor
|
||||
= FromData.HasConstexprNonCopyMoveConstructor;
|
||||
|
|
|
@ -43,6 +43,7 @@ CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D)
|
|||
Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true),
|
||||
HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false),
|
||||
HasMutableFields(false), HasOnlyCMembers(true),
|
||||
HasInClassInitializer(false),
|
||||
HasTrivialDefaultConstructor(true),
|
||||
HasConstexprNonCopyMoveConstructor(false),
|
||||
DefaultedDefaultConstructorIsConstexpr(true),
|
||||
|
@ -818,17 +819,19 @@ NotASpecialMember:;
|
|||
data().HasNonLiteralTypeFieldsOrBases = true;
|
||||
|
||||
if (Field->hasInClassInitializer()) {
|
||||
// C++0x [class]p5:
|
||||
data().HasInClassInitializer = true;
|
||||
|
||||
// C++11 [class]p5:
|
||||
// A default constructor is trivial if [...] no non-static data member
|
||||
// of its class has a brace-or-equal-initializer.
|
||||
data().HasTrivialDefaultConstructor = false;
|
||||
|
||||
// C++0x [dcl.init.aggr]p1:
|
||||
// C++11 [dcl.init.aggr]p1:
|
||||
// An aggregate is a [...] class with [...] no
|
||||
// brace-or-equal-initializers for non-static data members.
|
||||
data().Aggregate = false;
|
||||
|
||||
// C++0x [class]p10:
|
||||
// C++11 [class]p10:
|
||||
// A POD struct is [...] a trivial class.
|
||||
data().PlainOldData = false;
|
||||
}
|
||||
|
@ -920,7 +923,7 @@ NotASpecialMember:;
|
|||
// -- every constructor involved in initializing non-static data
|
||||
// members [...] shall be a constexpr constructor
|
||||
if (!Field->hasInClassInitializer() &&
|
||||
!FieldRec->hasConstexprDefaultConstructor())
|
||||
!FieldRec->hasConstexprDefaultConstructor() && !isUnion())
|
||||
// The standard requires any in-class initializer to be a constant
|
||||
// expression. We consider this to be a defect.
|
||||
data().DefaultedDefaultConstructorIsConstexpr = false;
|
||||
|
@ -944,7 +947,7 @@ NotASpecialMember:;
|
|||
data().DefaultedDefaultConstructorIsConstexpr = false;
|
||||
data().DefaultedCopyConstructorIsConstexpr = false;
|
||||
data().DefaultedMoveConstructorIsConstexpr = false;
|
||||
} else if (!Field->hasInClassInitializer())
|
||||
} else if (!Field->hasInClassInitializer() && !isUnion())
|
||||
data().DefaultedDefaultConstructorIsConstexpr = false;
|
||||
}
|
||||
|
||||
|
|
|
@ -3642,11 +3642,10 @@ static void TryValueInitialization(Sema &S,
|
|||
// user-provided or deleted default constructor, then the object is
|
||||
// zero-initialized and, if T has a non-trivial default constructor,
|
||||
// default-initialized;
|
||||
if ((ClassDecl->getTagKind() == TTK_Class ||
|
||||
ClassDecl->getTagKind() == TTK_Struct)) {
|
||||
Sequence.AddZeroInitializationStep(Entity.getType());
|
||||
return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence);
|
||||
}
|
||||
// FIXME: The 'non-union' here is a defect (not yet assigned an issue
|
||||
// number). Update the quotation when the defect is resolved.
|
||||
Sequence.AddZeroInitializationStep(Entity.getType());
|
||||
return TryConstructorInitialization(S, Entity, Kind, 0, 0, T, Sequence);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1090,6 +1090,7 @@ void ASTDeclReader::ReadCXXDefinitionData(
|
|||
Data.HasPublicFields = Record[Idx++];
|
||||
Data.HasMutableFields = Record[Idx++];
|
||||
Data.HasOnlyCMembers = Record[Idx++];
|
||||
Data.HasInClassInitializer = Record[Idx++];
|
||||
Data.HasTrivialDefaultConstructor = Record[Idx++];
|
||||
Data.HasConstexprNonCopyMoveConstructor = Record[Idx++];
|
||||
Data.DefaultedDefaultConstructorIsConstexpr = Record[Idx++];
|
||||
|
|
|
@ -4310,6 +4310,7 @@ void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Rec
|
|||
Record.push_back(Data.HasPublicFields);
|
||||
Record.push_back(Data.HasMutableFields);
|
||||
Record.push_back(Data.HasOnlyCMembers);
|
||||
Record.push_back(Data.HasInClassInitializer);
|
||||
Record.push_back(Data.HasTrivialDefaultConstructor);
|
||||
Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
|
||||
Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
|
||||
|
|
|
@ -55,3 +55,42 @@ struct A {}; // expected-note {{here}}
|
|||
struct B {
|
||||
friend A::A(); // expected-error {{non-constexpr declaration of 'A' follows constexpr declaration}}
|
||||
};
|
||||
|
||||
namespace UnionCtors {
|
||||
union A { // expected-note {{here}}
|
||||
int a;
|
||||
int b;
|
||||
};
|
||||
union B {
|
||||
int a;
|
||||
int b = 5;
|
||||
};
|
||||
union C {
|
||||
int a = 5;
|
||||
int b;
|
||||
};
|
||||
struct D {
|
||||
union {
|
||||
int a = 5;
|
||||
int b;
|
||||
};
|
||||
union {
|
||||
int c;
|
||||
int d = 5;
|
||||
};
|
||||
};
|
||||
struct E { // expected-note {{here}}
|
||||
union {
|
||||
int a;
|
||||
int b;
|
||||
};
|
||||
};
|
||||
|
||||
struct Test {
|
||||
friend constexpr A::A() noexcept; // expected-error {{follows non-constexpr declaration}}
|
||||
friend constexpr B::B() noexcept;
|
||||
friend constexpr C::C() noexcept;
|
||||
friend constexpr D::D() noexcept;
|
||||
friend constexpr E::E() noexcept; // expected-error {{follows non-constexpr declaration}}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -49,6 +49,17 @@ namespace StructUnion {
|
|||
|
||||
// CHECK: @_ZN11StructUnion1fE = global {{.*}} { i32 5 }
|
||||
D f;
|
||||
|
||||
union E {
|
||||
int a;
|
||||
void *b = &f;
|
||||
};
|
||||
|
||||
// CHECK: @_ZN11StructUnion1gE = global {{.*}} @_ZN11StructUnion1fE
|
||||
E g;
|
||||
|
||||
// CHECK: @_ZN11StructUnion1hE = global {{.*}} @_ZN11StructUnion1fE
|
||||
E h = E();
|
||||
}
|
||||
|
||||
namespace BaseClass {
|
||||
|
|
|
@ -2,8 +2,10 @@
|
|||
|
||||
// PR10531.
|
||||
|
||||
int make_a();
|
||||
|
||||
static union {
|
||||
int a = 42;
|
||||
int a = make_a();
|
||||
char *b;
|
||||
};
|
||||
|
||||
|
@ -32,4 +34,4 @@ int g() {
|
|||
|
||||
// CHECK: define {{.*}}@"[[CONSTRUCT_GLOBAL]]C2Ev"
|
||||
// CHECK-NOT: }
|
||||
// CHECK: store i32 42
|
||||
// CHECK: call {{.*}}@_Z6make_a
|
||||
|
|
Loading…
Reference in New Issue