/// pure virtual function, (that can come from a base class).
bool Abstract : 1;
+ /// HasTrivialConstructor - True when this class has a trivial constructor
+ bool HasTrivialConstructor : 1;
+
/// Bases - Base classes of this class.
/// FIXME: This is wasted space for a union.
CXXBaseSpecifier *Bases;
/// setAbstract - Set whether this class is abstract (C++ [class.abstract])
void setAbstract(bool Abs) { Abstract = Abs; }
+ // hasTrivialConstructor - Whether this class has a trivial constructor
+ // (C++ [class.ctor]p5)
+ bool hasTrivialConstructor() const { return HasTrivialConstructor; }
+
+ // setHasTrivialConstructor - Set whether this class has a trivial constructor
+ // (C++ [class.ctor]p5)
+ void setHasTrivialConstructor(bool TC) { HasTrivialConstructor = TC; }
+
/// \brief If this record is an instantiation of a member class,
/// retrieves the member class from which it was instantiated.
///
UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false),
UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false),
Aggregate(true), PlainOldData(true), Polymorphic(false), Abstract(false),
+ HasTrivialConstructor(true),
Bases(0), NumBases(0), Conversions(DC, DeclarationName()),
TemplateOrInstantiation() { }
// A POD-struct is an aggregate class [...]
PlainOldData = false;
+ // C++ [class.ctor]p5:
+ // A constructor is trivial if it is an implicitly-declared default
+ // constructor.
+ HasTrivialConstructor = false;
+
// Note when we have a user-declared copy constructor, which will
// suppress the implicit declaration of a copy constructor.
if (ConDecl->isCopyConstructor(Context))
if (const RecordType *RT = QueriedType->getAsRecordType())
return cast<CXXRecordDecl>(RT->getDecl())->isAbstract();
return false;
+ case UTT_HasTrivialConstructor:
+ if (const RecordType *RT = QueriedType->getAsRecordType())
+ return cast<CXXRecordDecl>(RT->getDecl())->hasTrivialConstructor();
+ return false;
}
}
/// '__has_nothrow_constructor' [TODO]
/// '__has_trivial_assign' [TODO]
/// '__has_trivial_copy' [TODO]
-/// '__has_trivial_constructor' [TODO]
+/// '__has_trivial_constructor'
/// '__has_trivial_destructor' [TODO]
/// '__has_virtual_destructor' [TODO]
/// '__is_abstract' [TODO]
case tok::kw___is_union:
case tok::kw___is_polymorphic:
case tok::kw___is_abstract:
+ case tok::kw___has_trivial_constructor:
return ParseUnaryTypeTrait();
case tok::at: {
CurClass->setAggregate(false);
CurClass->setPOD(false);
CurClass->setPolymorphic(true);
+ CurClass->setHasTrivialConstructor(false);
}
}
Class->setAggregate(false);
Class->setPOD(false);
+ if (Virtual) {
+ // C++ [class.ctor]p5:
+ // A constructor is trivial if its class has no virtual base classes.
+ Class->setHasTrivialConstructor(false);
+ } else {
+ // C++ [class.ctor]p5:
+ // A constructor is trivial if all the direct base classes of its
+ // class have trivial constructors.
+ Class->setHasTrivialConstructor(cast<CXXRecordDecl>(BaseDecl)->
+ hasTrivialConstructor());
+ }
+
// Create the base specifier.
// FIXME: Allocate via ASTContext?
return new CXXBaseSpecifier(SpecifierRange, Virtual,
if (RD->isAbstract())
AbstractClassUsageDiagnoser(*this, RD);
+ if (RD->hasTrivialConstructor()) {
+ for (RecordDecl::field_iterator i = RD->field_begin(Context),
+ e = RD->field_end(Context); i != e; ++i) {
+ // All the nonstatic data members must have trivial constructors.
+ QualType FTy = i->getType();
+ while (const ArrayType *AT = Context.getAsArrayType(FTy))
+ FTy = AT->getElementType();
+
+ if (const RecordType *RT = FTy->getAsRecordType()) {
+ CXXRecordDecl *FieldRD = cast<CXXRecordDecl>(RT->getDecl());
+ if (!FieldRD->hasTrivialConstructor()) {
+ RD->setHasTrivialConstructor(false);
+ break;
+ }
+ }
+ }
+ }
+
if (!Template)
AddImplicitlyDeclaredMembersToClass(RD);
}
--- /dev/null
+// RUN: clang-cc -fsyntax-only -verify %s -std=c++0x
+struct T1 {
+};
+static_assert(__has_trivial_constructor(T1), "T1 has trivial constructor!");
+
+struct T2 {
+ T2();
+};
+static_assert(!__has_trivial_constructor(T2), "T2 has a user-declared constructor!");
+
+struct T3 {
+ virtual void f();
+};
+static_assert(!__has_trivial_constructor(T3), "T3 has a virtual function!");
+
+struct T4 : virtual T3 {
+};
+static_assert(!__has_trivial_constructor(T4), "T4 has a virtual base class!");
+
+struct T5 : T1 {
+};
+static_assert(__has_trivial_constructor(T5), "All the direct base classes of T5 have trivial constructors!");
+
+struct T6 {
+ T5 t5;
+ T1 t1[2][2];
+ static T2 t2;
+};
+static_assert(__has_trivial_constructor(T6), "All nonstatic data members of T6 have trivial constructors!");
+
+struct T7 {
+ T4 t4;
+};
+static_assert(!__has_trivial_constructor(T7), "t4 does not have a trivial constructor!");