Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1 | //===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the C++ related Decl classes. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclTemplate.h" |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Argyrios Kyrtzidis | 7b90340 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 18 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 21 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 7d7e672 | 2008-11-12 23:21:09 +0000 | [diff] [blame] | 22 | #include "clang/Basic/IdentifierTable.h" |
Douglas Gregor | fdfab6b | 2008-12-23 21:31:30 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/STLExtras.h" |
Fariborz Jahanian | faebcbb | 2009-09-12 19:52:10 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | // Decl Allocation/Deallocation Method Implementations |
| 29 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 30 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 31 | void AccessSpecDecl::anchor() { } |
| 32 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 33 | AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 34 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(AccessSpecDecl)); |
| 35 | return new (Mem) AccessSpecDecl(EmptyShell()); |
| 36 | } |
| 37 | |
| 38 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 39 | CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D) |
| 40 | : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false), |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 41 | UserDeclaredMoveConstructor(false), UserDeclaredCopyAssignment(false), |
| 42 | UserDeclaredMoveAssignment(false), UserDeclaredDestructor(false), |
Eli Friedman | 97c134e | 2009-08-15 22:23:00 +0000 | [diff] [blame] | 43 | Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false), |
Chandler Carruth | ec997dc | 2011-04-30 10:07:30 +0000 | [diff] [blame] | 44 | Abstract(false), IsStandardLayout(true), HasNoNonEmptyBases(true), |
Chandler Carruth | a822544 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 45 | HasPrivateFields(false), HasProtectedFields(false), HasPublicFields(false), |
Douglas Gregor | 2bb1101 | 2011-05-13 01:05:07 +0000 | [diff] [blame] | 46 | HasMutableFields(false), HasTrivialDefaultConstructor(true), |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 47 | HasConstexprNonCopyMoveConstructor(false), |
| 48 | DefaultedDefaultConstructorIsConstexpr(true), |
| 49 | DefaultedCopyConstructorIsConstexpr(true), |
| 50 | DefaultedMoveConstructorIsConstexpr(true), |
| 51 | HasConstexprDefaultConstructor(false), HasConstexprCopyConstructor(false), |
| 52 | HasConstexprMoveConstructor(false), HasTrivialCopyConstructor(true), |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 53 | HasTrivialMoveConstructor(true), HasTrivialCopyAssignment(true), |
| 54 | HasTrivialMoveAssignment(true), HasTrivialDestructor(true), |
| 55 | HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false), |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 56 | UserProvidedDefaultConstructor(false), DeclaredDefaultConstructor(false), |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 57 | DeclaredCopyConstructor(false), DeclaredMoveConstructor(false), |
| 58 | DeclaredCopyAssignment(false), DeclaredMoveAssignment(false), |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 59 | DeclaredDestructor(false), FailedImplicitMoveConstructor(false), |
Eli Friedman | 72899c3 | 2012-01-07 04:59:52 +0000 | [diff] [blame] | 60 | FailedImplicitMoveAssignment(false), IsLambda(false), NumBases(0), |
| 61 | NumVBases(0), Bases(), VBases(), Definition(D), FirstFriend(0) { |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 65 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 66 | IdentifierInfo *Id, CXXRecordDecl *PrevDecl) |
| 67 | : RecordDecl(K, TK, DC, StartLoc, IdLoc, Id, PrevDecl), |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 68 | DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0), |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 69 | TemplateOrInstantiation() { } |
Douglas Gregor | 7d7e672 | 2008-11-12 23:21:09 +0000 | [diff] [blame] | 70 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 71 | CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 72 | DeclContext *DC, SourceLocation StartLoc, |
| 73 | SourceLocation IdLoc, IdentifierInfo *Id, |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 74 | CXXRecordDecl* PrevDecl, |
| 75 | bool DelayTypeCreation) { |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 76 | CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, StartLoc, IdLoc, |
| 77 | Id, PrevDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 79 | // FIXME: DelayTypeCreation seems like such a hack |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 80 | if (!DelayTypeCreation) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | C.getTypeDeclType(R, PrevDecl); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 82 | return R; |
| 83 | } |
| 84 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 85 | CXXRecordDecl * |
| 86 | CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) { |
| 87 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXRecordDecl)); |
| 88 | return new (Mem) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(), |
| 89 | SourceLocation(), 0, 0); |
Argyrios Kyrtzidis | b8b03e6 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | void |
Douglas Gregor | 2d5b703 | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 93 | CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases, |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 94 | unsigned NumBases) { |
Douglas Gregor | 2d5b703 | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 95 | ASTContext &C = getASTContext(); |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 96 | |
Douglas Gregor | 7c789c1 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 97 | if (!data().Bases.isOffset() && data().NumBases > 0) |
| 98 | C.Deallocate(data().getBases()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | |
Richard Smith | dd67723 | 2011-10-18 20:08:55 +0000 | [diff] [blame] | 100 | if (NumBases) { |
| 101 | // C++ [dcl.init.aggr]p1: |
| 102 | // An aggregate is [...] a class with [...] no base classes [...]. |
| 103 | data().Aggregate = false; |
| 104 | |
| 105 | // C++ [class]p4: |
| 106 | // A POD-struct is an aggregate class... |
| 107 | data().PlainOldData = false; |
| 108 | } |
| 109 | |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 110 | // The set of seen virtual base types. |
Anders Carlsson | 1c36393 | 2010-03-29 19:49:09 +0000 | [diff] [blame] | 111 | llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes; |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 112 | |
| 113 | // The virtual bases of this class. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 114 | SmallVector<const CXXBaseSpecifier *, 8> VBases; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 116 | data().Bases = new(C) CXXBaseSpecifier [NumBases]; |
| 117 | data().NumBases = NumBases; |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 118 | for (unsigned i = 0; i < NumBases; ++i) { |
Douglas Gregor | 7c789c1 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 119 | data().getBases()[i] = *Bases[i]; |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 120 | // Keep track of inherited vbases for this base class. |
| 121 | const CXXBaseSpecifier *Base = Bases[i]; |
| 122 | QualType BaseType = Base->getType(); |
Douglas Gregor | 5fe8c04 | 2010-02-27 00:25:28 +0000 | [diff] [blame] | 123 | // Skip dependent types; we can't do any checking on them now. |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 124 | if (BaseType->isDependentType()) |
| 125 | continue; |
| 126 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 127 | = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 128 | |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 129 | // A class with a non-empty base class is not empty. |
| 130 | // FIXME: Standard ref? |
Chandler Carruth | a822544 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 131 | if (!BaseClassDecl->isEmpty()) { |
| 132 | if (!data().Empty) { |
| 133 | // C++0x [class]p7: |
| 134 | // A standard-layout class is a class that: |
| 135 | // [...] |
| 136 | // -- either has no non-static data members in the most derived |
| 137 | // class and at most one base class with non-static data members, |
| 138 | // or has no base classes with non-static data members, and |
| 139 | // If this is the second non-empty base, then neither of these two |
| 140 | // clauses can be true. |
Chandler Carruth | ec997dc | 2011-04-30 10:07:30 +0000 | [diff] [blame] | 141 | data().IsStandardLayout = false; |
Chandler Carruth | a822544 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 144 | data().Empty = false; |
Chandler Carruth | a822544 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 145 | data().HasNoNonEmptyBases = false; |
| 146 | } |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 147 | |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 148 | // C++ [class.virtual]p1: |
| 149 | // A class that declares or inherits a virtual function is called a |
| 150 | // polymorphic class. |
| 151 | if (BaseClassDecl->isPolymorphic()) |
| 152 | data().Polymorphic = true; |
Chandler Carruth | 9b6347c | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 153 | |
Chandler Carruth | a822544 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 154 | // C++0x [class]p7: |
| 155 | // A standard-layout class is a class that: [...] |
| 156 | // -- has no non-standard-layout base classes |
Chandler Carruth | ec997dc | 2011-04-30 10:07:30 +0000 | [diff] [blame] | 157 | if (!BaseClassDecl->isStandardLayout()) |
| 158 | data().IsStandardLayout = false; |
Chandler Carruth | a822544 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 159 | |
Chandler Carruth | 9b6347c | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 160 | // Record if this base is the first non-literal field or base. |
| 161 | if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType()) |
| 162 | data().HasNonLiteralTypeFieldsOrBases = true; |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 163 | |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 164 | // Now go through all virtual bases of this base and add them. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 165 | for (CXXRecordDecl::base_class_iterator VBase = |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 166 | BaseClassDecl->vbases_begin(), |
| 167 | E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) { |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 168 | // Add this base if it's not already in the list. |
Anders Carlsson | 1c36393 | 2010-03-29 19:49:09 +0000 | [diff] [blame] | 169 | if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType()))) |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 170 | VBases.push_back(VBase); |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 171 | } |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 172 | |
| 173 | if (Base->isVirtual()) { |
| 174 | // Add this base if it's not already in the list. |
Anders Carlsson | 1c36393 | 2010-03-29 19:49:09 +0000 | [diff] [blame] | 175 | if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType))) |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 176 | VBases.push_back(Base); |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 177 | |
| 178 | // C++0x [meta.unary.prop] is_empty: |
| 179 | // T is a class type, but not a union type, with ... no virtual base |
| 180 | // classes |
| 181 | data().Empty = false; |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 182 | |
| 183 | // C++ [class.ctor]p5: |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 184 | // A default constructor is trivial [...] if: |
| 185 | // -- its class has [...] no virtual bases |
| 186 | data().HasTrivialDefaultConstructor = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 187 | |
| 188 | // C++0x [class.copy]p13: |
| 189 | // A copy/move constructor for class X is trivial if it is neither |
| 190 | // user-provided nor deleted and if |
| 191 | // -- class X has no virtual functions and no virtual base classes, and |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 192 | data().HasTrivialCopyConstructor = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 193 | data().HasTrivialMoveConstructor = false; |
| 194 | |
| 195 | // C++0x [class.copy]p27: |
| 196 | // A copy/move assignment operator for class X is trivial if it is |
| 197 | // neither user-provided nor deleted and if |
| 198 | // -- class X has no virtual functions and no virtual base classes, and |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 199 | data().HasTrivialCopyAssignment = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 200 | data().HasTrivialMoveAssignment = false; |
Chandler Carruth | a822544 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 201 | |
| 202 | // C++0x [class]p7: |
| 203 | // A standard-layout class is a class that: [...] |
| 204 | // -- has [...] no virtual base classes |
Chandler Carruth | ec997dc | 2011-04-30 10:07:30 +0000 | [diff] [blame] | 205 | data().IsStandardLayout = false; |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 206 | |
| 207 | // C++11 [dcl.constexpr]p4: |
| 208 | // In the definition of a constexpr constructor [...] |
| 209 | // -- the class shall not have any virtual base classes |
| 210 | data().DefaultedDefaultConstructorIsConstexpr = false; |
| 211 | data().DefaultedCopyConstructorIsConstexpr = false; |
| 212 | data().DefaultedMoveConstructorIsConstexpr = false; |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 213 | } else { |
| 214 | // C++ [class.ctor]p5: |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 215 | // A default constructor is trivial [...] if: |
| 216 | // -- all the direct base classes of its class have trivial default |
| 217 | // constructors. |
| 218 | if (!BaseClassDecl->hasTrivialDefaultConstructor()) |
| 219 | data().HasTrivialDefaultConstructor = false; |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 220 | |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 221 | // C++0x [class.copy]p13: |
| 222 | // A copy/move constructor for class X is trivial if [...] |
| 223 | // [...] |
| 224 | // -- the constructor selected to copy/move each direct base class |
| 225 | // subobject is trivial, and |
| 226 | // FIXME: C++0x: We need to only consider the selected constructor |
| 227 | // instead of all of them. |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 228 | if (!BaseClassDecl->hasTrivialCopyConstructor()) |
| 229 | data().HasTrivialCopyConstructor = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 230 | if (!BaseClassDecl->hasTrivialMoveConstructor()) |
| 231 | data().HasTrivialMoveConstructor = false; |
| 232 | |
| 233 | // C++0x [class.copy]p27: |
| 234 | // A copy/move assignment operator for class X is trivial if [...] |
| 235 | // [...] |
| 236 | // -- the assignment operator selected to copy/move each direct base |
| 237 | // class subobject is trivial, and |
| 238 | // FIXME: C++0x: We need to only consider the selected operator instead |
| 239 | // of all of them. |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 240 | if (!BaseClassDecl->hasTrivialCopyAssignment()) |
| 241 | data().HasTrivialCopyAssignment = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 242 | if (!BaseClassDecl->hasTrivialMoveAssignment()) |
| 243 | data().HasTrivialMoveAssignment = false; |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 244 | |
| 245 | // C++11 [class.ctor]p6: |
| 246 | // If that user-written default cosntructor would satisfy the |
| 247 | // requirements of a constexpr constructor, the implicitly-defined |
| 248 | // default constructor is constexpr. |
| 249 | if (!BaseClassDecl->hasConstexprDefaultConstructor()) |
| 250 | data().DefaultedDefaultConstructorIsConstexpr = false; |
| 251 | |
| 252 | // C++11 [class.copy]p13: |
| 253 | // If the implicitly-defined constructor would satisfy the requirements |
| 254 | // of a constexpr constructor, the implicitly-defined constructor is |
| 255 | // constexpr. |
| 256 | // C++11 [dcl.constexpr]p4: |
| 257 | // -- every constructor involved in initializing [...] base class |
| 258 | // sub-objects shall be a constexpr constructor |
| 259 | if (!BaseClassDecl->hasConstexprCopyConstructor()) |
| 260 | data().DefaultedCopyConstructorIsConstexpr = false; |
| 261 | if (BaseClassDecl->hasDeclaredMoveConstructor() || |
| 262 | BaseClassDecl->needsImplicitMoveConstructor()) |
| 263 | // FIXME: If the implicit move constructor generated for the base class |
| 264 | // would be ill-formed, the implicit move constructor generated for the |
| 265 | // derived class calls the base class' copy constructor. |
| 266 | data().DefaultedMoveConstructorIsConstexpr &= |
| 267 | !BaseClassDecl->hasConstexprMoveConstructor(); |
| 268 | else if (!BaseClassDecl->hasConstexprCopyConstructor()) |
| 269 | data().DefaultedMoveConstructorIsConstexpr = false; |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 270 | } |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 271 | |
| 272 | // C++ [class.ctor]p3: |
| 273 | // A destructor is trivial if all the direct base classes of its class |
| 274 | // have trivial destructors. |
| 275 | if (!BaseClassDecl->hasTrivialDestructor()) |
| 276 | data().HasTrivialDestructor = false; |
Douglas Gregor | 2bb1101 | 2011-05-13 01:05:07 +0000 | [diff] [blame] | 277 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 278 | // A class has an Objective-C object member if... or any of its bases |
| 279 | // has an Objective-C object member. |
| 280 | if (BaseClassDecl->hasObjectMember()) |
| 281 | setHasObjectMember(true); |
| 282 | |
Douglas Gregor | 2bb1101 | 2011-05-13 01:05:07 +0000 | [diff] [blame] | 283 | // Keep track of the presence of mutable fields. |
| 284 | if (BaseClassDecl->hasMutableFields()) |
| 285 | data().HasMutableFields = true; |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 286 | } |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 287 | |
| 288 | if (VBases.empty()) |
| 289 | return; |
| 290 | |
| 291 | // Create base specifier for any direct or indirect virtual bases. |
| 292 | data().VBases = new (C) CXXBaseSpecifier[VBases.size()]; |
| 293 | data().NumVBases = VBases.size(); |
Richard Smith | 9f8ee2e | 2011-07-12 23:49:11 +0000 | [diff] [blame] | 294 | for (int I = 0, E = VBases.size(); I != E; ++I) |
| 295 | data().getVBases()[I] = *VBases[I]; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 298 | /// Callback function for CXXRecordDecl::forallBases that acknowledges |
| 299 | /// that it saw a base class. |
| 300 | static bool SawBase(const CXXRecordDecl *, void *) { |
| 301 | return true; |
| 302 | } |
| 303 | |
| 304 | bool CXXRecordDecl::hasAnyDependentBases() const { |
| 305 | if (!isDependentContext()) |
| 306 | return false; |
| 307 | |
| 308 | return !forallBases(SawBase, 0); |
| 309 | } |
| 310 | |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 311 | bool CXXRecordDecl::hasConstCopyConstructor() const { |
| 312 | return getCopyConstructor(Qualifiers::Const) != 0; |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Chandler Carruth | b7e9589 | 2011-04-23 10:47:28 +0000 | [diff] [blame] | 315 | bool CXXRecordDecl::isTriviallyCopyable() const { |
| 316 | // C++0x [class]p5: |
| 317 | // A trivially copyable class is a class that: |
| 318 | // -- has no non-trivial copy constructors, |
| 319 | if (!hasTrivialCopyConstructor()) return false; |
| 320 | // -- has no non-trivial move constructors, |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 321 | if (!hasTrivialMoveConstructor()) return false; |
Chandler Carruth | b7e9589 | 2011-04-23 10:47:28 +0000 | [diff] [blame] | 322 | // -- has no non-trivial copy assignment operators, |
| 323 | if (!hasTrivialCopyAssignment()) return false; |
| 324 | // -- has no non-trivial move assignment operators, and |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 325 | if (!hasTrivialMoveAssignment()) return false; |
Chandler Carruth | b7e9589 | 2011-04-23 10:47:28 +0000 | [diff] [blame] | 326 | // -- has a trivial destructor. |
| 327 | if (!hasTrivialDestructor()) return false; |
| 328 | |
| 329 | return true; |
| 330 | } |
| 331 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 332 | /// \brief Perform a simplistic form of overload resolution that only considers |
| 333 | /// cv-qualifiers on a single parameter, and return the best overload candidate |
| 334 | /// (if there is one). |
| 335 | static CXXMethodDecl * |
| 336 | GetBestOverloadCandidateSimple( |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 337 | const SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) { |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 338 | if (Cands.empty()) |
| 339 | return 0; |
| 340 | if (Cands.size() == 1) |
| 341 | return Cands[0].first; |
| 342 | |
| 343 | unsigned Best = 0, N = Cands.size(); |
| 344 | for (unsigned I = 1; I != N; ++I) |
Douglas Gregor | 61d0b6b | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 345 | if (Cands[Best].second.compatiblyIncludes(Cands[I].second)) |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 346 | Best = I; |
| 347 | |
| 348 | for (unsigned I = 1; I != N; ++I) |
Douglas Gregor | 61d0b6b | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 349 | if (Cands[Best].second.compatiblyIncludes(Cands[I].second)) |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 350 | return 0; |
| 351 | |
| 352 | return Cands[Best].first; |
| 353 | } |
| 354 | |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 355 | CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(unsigned TypeQuals) const{ |
| 356 | ASTContext &Context = getASTContext(); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 357 | QualType ClassType |
| 358 | = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | DeclarationName ConstructorName |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 360 | = Context.DeclarationNames.getCXXConstructorName( |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 361 | Context.getCanonicalType(ClassType)); |
| 362 | unsigned FoundTQs; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 363 | SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found; |
Douglas Gregor | fdfab6b | 2008-12-23 21:31:30 +0000 | [diff] [blame] | 364 | DeclContext::lookup_const_iterator Con, ConEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 365 | for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName); |
Douglas Gregor | fdfab6b | 2008-12-23 21:31:30 +0000 | [diff] [blame] | 366 | Con != ConEnd; ++Con) { |
Douglas Gregor | d93bacf | 2009-09-04 14:46:39 +0000 | [diff] [blame] | 367 | // C++ [class.copy]p2: |
| 368 | // A non-template constructor for class X is a copy constructor if [...] |
| 369 | if (isa<FunctionTemplateDecl>(*Con)) |
| 370 | continue; |
| 371 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 372 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 373 | if (Constructor->isCopyConstructor(FoundTQs)) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 374 | if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) || |
| 375 | (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const))) |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 376 | Found.push_back(std::make_pair( |
| 377 | const_cast<CXXConstructorDecl *>(Constructor), |
| 378 | Qualifiers::fromCVRMask(FoundTQs))); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 379 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 380 | } |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 381 | |
| 382 | return cast_or_null<CXXConstructorDecl>( |
| 383 | GetBestOverloadCandidateSimple(Found)); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 386 | CXXConstructorDecl *CXXRecordDecl::getMoveConstructor() const { |
| 387 | for (ctor_iterator I = ctor_begin(), E = ctor_end(); I != E; ++I) |
| 388 | if (I->isMoveConstructor()) |
| 389 | return *I; |
| 390 | |
| 391 | return 0; |
| 392 | } |
| 393 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 394 | CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const { |
| 395 | ASTContext &Context = getASTContext(); |
| 396 | QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this)); |
| 397 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 398 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 399 | SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found; |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 400 | DeclContext::lookup_const_iterator Op, OpEnd; |
| 401 | for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) { |
| 402 | // C++ [class.copy]p9: |
| 403 | // A user-declared copy assignment operator is a non-static non-template |
| 404 | // member function of class X with exactly one parameter of type X, X&, |
| 405 | // const X&, volatile X& or const volatile X&. |
| 406 | const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op); |
| 407 | if (!Method || Method->isStatic() || Method->getPrimaryTemplate()) |
| 408 | continue; |
| 409 | |
| 410 | const FunctionProtoType *FnType |
| 411 | = Method->getType()->getAs<FunctionProtoType>(); |
| 412 | assert(FnType && "Overloaded operator has no prototype."); |
| 413 | // Don't assert on this; an invalid decl might have been left in the AST. |
| 414 | if (FnType->getNumArgs() != 1 || FnType->isVariadic()) |
| 415 | continue; |
| 416 | |
| 417 | QualType ArgType = FnType->getArgType(0); |
| 418 | Qualifiers Quals; |
| 419 | if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) { |
| 420 | ArgType = Ref->getPointeeType(); |
| 421 | // If we have a const argument and we have a reference to a non-const, |
| 422 | // this function does not match. |
| 423 | if (ArgIsConst && !ArgType.isConstQualified()) |
| 424 | continue; |
| 425 | |
| 426 | Quals = ArgType.getQualifiers(); |
| 427 | } else { |
| 428 | // By-value copy-assignment operators are treated like const X& |
| 429 | // copy-assignment operators. |
| 430 | Quals = Qualifiers::fromCVRMask(Qualifiers::Const); |
| 431 | } |
| 432 | |
| 433 | if (!Context.hasSameUnqualifiedType(ArgType, Class)) |
| 434 | continue; |
| 435 | |
| 436 | // Save this copy-assignment operator. It might be "the one". |
| 437 | Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals)); |
| 438 | } |
| 439 | |
| 440 | // Use a simplistic form of overload resolution to find the candidate. |
| 441 | return GetBestOverloadCandidateSimple(Found); |
| 442 | } |
| 443 | |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 444 | CXXMethodDecl *CXXRecordDecl::getMoveAssignmentOperator() const { |
| 445 | for (method_iterator I = method_begin(), E = method_end(); I != E; ++I) |
| 446 | if (I->isMoveAssignmentOperator()) |
| 447 | return *I; |
| 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
Douglas Gregor | 2138664 | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 452 | void CXXRecordDecl::markedVirtualFunctionPure() { |
| 453 | // C++ [class.abstract]p2: |
| 454 | // A class is abstract if it has at least one pure virtual function. |
| 455 | data().Abstract = true; |
| 456 | } |
| 457 | |
| 458 | void CXXRecordDecl::addedMember(Decl *D) { |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 459 | // Ignore friends and invalid declarations. |
| 460 | if (D->getFriendObjectKind() || D->isInvalidDecl()) |
Douglas Gregor | 5c0646b | 2010-09-27 21:17:54 +0000 | [diff] [blame] | 461 | return; |
| 462 | |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 463 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D); |
| 464 | if (FunTmpl) |
| 465 | D = FunTmpl->getTemplatedDecl(); |
| 466 | |
Douglas Gregor | 9fe183a | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 467 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 468 | if (Method->isVirtual()) { |
| 469 | // C++ [dcl.init.aggr]p1: |
| 470 | // An aggregate is an array or a class with [...] no virtual functions. |
| 471 | data().Aggregate = false; |
| 472 | |
| 473 | // C++ [class]p4: |
| 474 | // A POD-struct is an aggregate class... |
| 475 | data().PlainOldData = false; |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 476 | |
| 477 | // Virtual functions make the class non-empty. |
| 478 | // FIXME: Standard ref? |
| 479 | data().Empty = false; |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 480 | |
| 481 | // C++ [class.virtual]p1: |
| 482 | // A class that declares or inherits a virtual function is called a |
| 483 | // polymorphic class. |
| 484 | data().Polymorphic = true; |
| 485 | |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 486 | // C++0x [class.ctor]p5 |
| 487 | // A default constructor is trivial [...] if: |
| 488 | // -- its class has no virtual functions [...] |
| 489 | data().HasTrivialDefaultConstructor = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 490 | |
| 491 | // C++0x [class.copy]p13: |
| 492 | // A copy/move constructor for class X is trivial if [...] |
| 493 | // -- class X has no virtual functions [...] |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 494 | data().HasTrivialCopyConstructor = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 495 | data().HasTrivialMoveConstructor = false; |
| 496 | |
| 497 | // C++0x [class.copy]p27: |
| 498 | // A copy/move assignment operator for class X is trivial if [...] |
| 499 | // -- class X has no virtual functions [...] |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 500 | data().HasTrivialCopyAssignment = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 501 | data().HasTrivialMoveAssignment = false; |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 502 | |
Chandler Carruth | a822544 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 503 | // C++0x [class]p7: |
| 504 | // A standard-layout class is a class that: [...] |
| 505 | // -- has no virtual functions |
Chandler Carruth | ec997dc | 2011-04-30 10:07:30 +0000 | [diff] [blame] | 506 | data().IsStandardLayout = false; |
Douglas Gregor | 9fe183a | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 510 | if (D->isImplicit()) { |
Argyrios Kyrtzidis | b6cc0e1 | 2010-10-24 17:26:54 +0000 | [diff] [blame] | 511 | // Notify that an implicit member was added after the definition |
| 512 | // was completed. |
| 513 | if (!isBeingDefined()) |
| 514 | if (ASTMutationListener *L = getASTMutationListener()) |
| 515 | L->AddedCXXImplicitMember(data().Definition, D); |
Argyrios Kyrtzidis | 046c03b | 2010-10-20 23:48:42 +0000 | [diff] [blame] | 516 | |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 517 | // If this is a special member function, note that it was added and then |
| 518 | // return early. |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 519 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 520 | if (Constructor->isDefaultConstructor()) { |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 521 | data().DeclaredDefaultConstructor = true; |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 522 | if (Constructor->isConstexpr()) { |
| 523 | data().HasConstexprDefaultConstructor = true; |
| 524 | data().HasConstexprNonCopyMoveConstructor = true; |
| 525 | } |
| 526 | } else if (Constructor->isCopyConstructor()) { |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 527 | data().DeclaredCopyConstructor = true; |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 528 | if (Constructor->isConstexpr()) |
| 529 | data().HasConstexprCopyConstructor = true; |
| 530 | } else if (Constructor->isMoveConstructor()) { |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 531 | data().DeclaredMoveConstructor = true; |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 532 | if (Constructor->isConstexpr()) |
| 533 | data().HasConstexprMoveConstructor = true; |
| 534 | } else |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 535 | goto NotASpecialMember; |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 536 | return; |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 537 | } else if (isa<CXXDestructorDecl>(D)) { |
Douglas Gregor | 0ed2e08 | 2010-09-27 22:48:58 +0000 | [diff] [blame] | 538 | data().DeclaredDestructor = true; |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 539 | return; |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 540 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 541 | if (Method->isCopyAssignmentOperator()) |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 542 | data().DeclaredCopyAssignment = true; |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 543 | else if (Method->isMoveAssignmentOperator()) |
| 544 | data().DeclaredMoveAssignment = true; |
| 545 | else |
| 546 | goto NotASpecialMember; |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 547 | return; |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 548 | } |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 549 | |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 550 | NotASpecialMember:; |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 551 | // Any other implicit declarations are handled like normal declarations. |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | // Handle (user-declared) constructors. |
| 555 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
| 556 | // Note that we have a user-declared constructor. |
| 557 | data().UserDeclaredConstructor = true; |
| 558 | |
Richard Smith | 017ab77 | 2011-09-05 02:13:09 +0000 | [diff] [blame] | 559 | // Technically, "user-provided" is only defined for special member |
| 560 | // functions, but the intent of the standard is clearly that it should apply |
| 561 | // to all functions. |
| 562 | bool UserProvided = Constructor->isUserProvided(); |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 563 | |
Sean Hunt | 37b8c9e | 2011-05-09 21:45:35 +0000 | [diff] [blame] | 564 | if (Constructor->isDefaultConstructor()) { |
| 565 | data().DeclaredDefaultConstructor = true; |
Richard Smith | 017ab77 | 2011-09-05 02:13:09 +0000 | [diff] [blame] | 566 | if (UserProvided) { |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 567 | // C++0x [class.ctor]p5: |
| 568 | // A default constructor is trivial if it is not user-provided [...] |
Sean Hunt | 37b8c9e | 2011-05-09 21:45:35 +0000 | [diff] [blame] | 569 | data().HasTrivialDefaultConstructor = false; |
Sean Hunt | cdee3fe | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 570 | data().UserProvidedDefaultConstructor = true; |
Sean Hunt | 37b8c9e | 2011-05-09 21:45:35 +0000 | [diff] [blame] | 571 | } |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 572 | if (Constructor->isConstexpr()) { |
| 573 | data().HasConstexprDefaultConstructor = true; |
| 574 | data().HasConstexprNonCopyMoveConstructor = true; |
| 575 | } |
Sean Hunt | 37b8c9e | 2011-05-09 21:45:35 +0000 | [diff] [blame] | 576 | } |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 577 | |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 578 | // Note when we have a user-declared copy or move constructor, which will |
| 579 | // suppress the implicit declaration of those constructors. |
| 580 | if (!FunTmpl) { |
| 581 | if (Constructor->isCopyConstructor()) { |
| 582 | data().UserDeclaredCopyConstructor = true; |
| 583 | data().DeclaredCopyConstructor = true; |
| 584 | |
| 585 | // C++0x [class.copy]p13: |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 586 | // A copy/move constructor for class X is trivial if it is not |
| 587 | // user-provided [...] |
Richard Smith | 017ab77 | 2011-09-05 02:13:09 +0000 | [diff] [blame] | 588 | if (UserProvided) |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 589 | data().HasTrivialCopyConstructor = false; |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 590 | |
| 591 | if (Constructor->isConstexpr()) |
| 592 | data().HasConstexprCopyConstructor = true; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 593 | } else if (Constructor->isMoveConstructor()) { |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 594 | data().UserDeclaredMoveConstructor = true; |
| 595 | data().DeclaredMoveConstructor = true; |
| 596 | |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 597 | // C++0x [class.copy]p13: |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 598 | // A copy/move constructor for class X is trivial if it is not |
| 599 | // user-provided [...] |
Richard Smith | 017ab77 | 2011-09-05 02:13:09 +0000 | [diff] [blame] | 600 | if (UserProvided) |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 601 | data().HasTrivialMoveConstructor = false; |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 602 | |
| 603 | if (Constructor->isConstexpr()) |
| 604 | data().HasConstexprMoveConstructor = true; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 605 | } |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 606 | } |
Richard Smith | 6b8bc07 | 2011-08-10 18:11:37 +0000 | [diff] [blame] | 607 | if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) { |
| 608 | // Record if we see any constexpr constructors which are neither copy |
Chandler Carruth | 9b6347c | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 609 | // nor move constructors. |
Richard Smith | 6b8bc07 | 2011-08-10 18:11:37 +0000 | [diff] [blame] | 610 | data().HasConstexprNonCopyMoveConstructor = true; |
Chandler Carruth | 9b6347c | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 611 | } |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 612 | |
Sean Hunt | 37b8c9e | 2011-05-09 21:45:35 +0000 | [diff] [blame] | 613 | // C++ [dcl.init.aggr]p1: |
| 614 | // An aggregate is an array or a class with no user-declared |
| 615 | // constructors [...]. |
| 616 | // C++0x [dcl.init.aggr]p1: |
| 617 | // An aggregate is an array or a class with no user-provided |
| 618 | // constructors [...]. |
| 619 | if (!getASTContext().getLangOptions().CPlusPlus0x || UserProvided) |
| 620 | data().Aggregate = false; |
| 621 | |
| 622 | // C++ [class]p4: |
| 623 | // A POD-struct is an aggregate class [...] |
| 624 | // Since the POD bit is meant to be C++03 POD-ness, clear it even if the |
| 625 | // type is technically an aggregate in C++0x since it wouldn't be in 03. |
| 626 | data().PlainOldData = false; |
| 627 | |
Douglas Gregor | 5c0646b | 2010-09-27 21:17:54 +0000 | [diff] [blame] | 628 | return; |
| 629 | } |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 630 | |
Douglas Gregor | 0ed2e08 | 2010-09-27 22:48:58 +0000 | [diff] [blame] | 631 | // Handle (user-declared) destructors. |
Sean Hunt | cf34e75 | 2011-05-16 22:41:40 +0000 | [diff] [blame] | 632 | if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) { |
Douglas Gregor | 0ed2e08 | 2010-09-27 22:48:58 +0000 | [diff] [blame] | 633 | data().DeclaredDestructor = true; |
| 634 | data().UserDeclaredDestructor = true; |
Douglas Gregor | 9fe183a | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 635 | |
| 636 | // C++ [class]p4: |
| 637 | // A POD-struct is an aggregate class that has [...] no user-defined |
| 638 | // destructor. |
Sean Hunt | cf34e75 | 2011-05-16 22:41:40 +0000 | [diff] [blame] | 639 | // This bit is the C++03 POD bit, not the 0x one. |
Douglas Gregor | 9fe183a | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 640 | data().PlainOldData = false; |
| 641 | |
Douglas Gregor | 45fa560 | 2011-11-07 20:56:01 +0000 | [diff] [blame] | 642 | // C++11 [class.dtor]p5: |
| 643 | // A destructor is trivial if it is not user-provided and if |
| 644 | // -- the destructor is not virtual. |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 645 | if (DD->isUserProvided() || DD->isVirtual()) { |
Sean Hunt | cf34e75 | 2011-05-16 22:41:40 +0000 | [diff] [blame] | 646 | data().HasTrivialDestructor = false; |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 647 | // C++11 [dcl.constexpr]p1: |
| 648 | // The constexpr specifier shall be applied only to [...] the |
| 649 | // declaration of a static data member of a literal type. |
| 650 | // C++11 [basic.types]p10: |
| 651 | // A type is a literal type if it is [...] a class type that [...] has |
| 652 | // a trivial destructor. |
| 653 | data().DefaultedDefaultConstructorIsConstexpr = false; |
| 654 | data().DefaultedCopyConstructorIsConstexpr = false; |
| 655 | data().DefaultedMoveConstructorIsConstexpr = false; |
| 656 | } |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 657 | |
Douglas Gregor | 0ed2e08 | 2010-09-27 22:48:58 +0000 | [diff] [blame] | 658 | return; |
| 659 | } |
Douglas Gregor | 5c0646b | 2010-09-27 21:17:54 +0000 | [diff] [blame] | 660 | |
Douglas Gregor | 0ed2e08 | 2010-09-27 22:48:58 +0000 | [diff] [blame] | 661 | // Handle (user-declared) member functions. |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 662 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 663 | if (Method->isCopyAssignmentOperator()) { |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 664 | // C++ [class]p4: |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 665 | // A POD-struct is an aggregate class that [...] has no user-defined |
| 666 | // copy assignment operator [...]. |
Sean Hunt | cf34e75 | 2011-05-16 22:41:40 +0000 | [diff] [blame] | 667 | // This is the C++03 bit only. |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 668 | data().PlainOldData = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 669 | |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 670 | // This is a copy assignment operator. |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 671 | |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 672 | // Suppress the implicit declaration of a copy constructor. |
| 673 | data().UserDeclaredCopyAssignment = true; |
| 674 | data().DeclaredCopyAssignment = true; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 675 | |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 676 | // C++0x [class.copy]p27: |
| 677 | // A copy/move assignment operator for class X is trivial if it is |
| 678 | // neither user-provided nor deleted [...] |
| 679 | if (Method->isUserProvided()) |
| 680 | data().HasTrivialCopyAssignment = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 681 | |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 682 | return; |
| 683 | } |
| 684 | |
| 685 | if (Method->isMoveAssignmentOperator()) { |
| 686 | // This is an extension in C++03 mode, but we'll keep consistency by |
| 687 | // taking a move assignment operator to induce non-POD-ness |
| 688 | data().PlainOldData = false; |
| 689 | |
| 690 | // This is a move assignment operator. |
| 691 | data().UserDeclaredMoveAssignment = true; |
| 692 | data().DeclaredMoveAssignment = true; |
| 693 | |
| 694 | // C++0x [class.copy]p27: |
| 695 | // A copy/move assignment operator for class X is trivial if it is |
| 696 | // neither user-provided nor deleted [...] |
| 697 | if (Method->isUserProvided()) |
| 698 | data().HasTrivialMoveAssignment = false; |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 699 | } |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 700 | |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 701 | // Keep the list of conversion functions up-to-date. |
| 702 | if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { |
| 703 | // We don't record specializations. |
| 704 | if (Conversion->getPrimaryTemplate()) |
| 705 | return; |
| 706 | |
| 707 | // FIXME: We intentionally don't use the decl's access here because it |
| 708 | // hasn't been set yet. That's really just a misdesign in Sema. |
| 709 | |
| 710 | if (FunTmpl) { |
| 711 | if (FunTmpl->getPreviousDeclaration()) |
| 712 | data().Conversions.replace(FunTmpl->getPreviousDeclaration(), |
| 713 | FunTmpl); |
| 714 | else |
| 715 | data().Conversions.addDecl(FunTmpl); |
| 716 | } else { |
| 717 | if (Conversion->getPreviousDeclaration()) |
| 718 | data().Conversions.replace(Conversion->getPreviousDeclaration(), |
| 719 | Conversion); |
| 720 | else |
| 721 | data().Conversions.addDecl(Conversion); |
| 722 | } |
| 723 | } |
| 724 | |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 725 | return; |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 726 | } |
Douglas Gregor | 9fe183a | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 727 | |
| 728 | // Handle non-static data members. |
| 729 | if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 730 | // C++ [class.bit]p2: |
| 731 | // A declaration for a bit-field that omits the identifier declares an |
| 732 | // unnamed bit-field. Unnamed bit-fields are not members and cannot be |
| 733 | // initialized. |
| 734 | if (Field->isUnnamedBitfield()) |
| 735 | return; |
| 736 | |
Douglas Gregor | 9fe183a | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 737 | // C++ [dcl.init.aggr]p1: |
| 738 | // An aggregate is an array or a class (clause 9) with [...] no |
| 739 | // private or protected non-static data members (clause 11). |
| 740 | // |
| 741 | // A POD must be an aggregate. |
| 742 | if (D->getAccess() == AS_private || D->getAccess() == AS_protected) { |
| 743 | data().Aggregate = false; |
| 744 | data().PlainOldData = false; |
| 745 | } |
Chandler Carruth | a822544 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 746 | |
| 747 | // C++0x [class]p7: |
| 748 | // A standard-layout class is a class that: |
| 749 | // [...] |
| 750 | // -- has the same access control for all non-static data members, |
| 751 | switch (D->getAccess()) { |
| 752 | case AS_private: data().HasPrivateFields = true; break; |
| 753 | case AS_protected: data().HasProtectedFields = true; break; |
| 754 | case AS_public: data().HasPublicFields = true; break; |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 755 | case AS_none: llvm_unreachable("Invalid access specifier"); |
Chandler Carruth | a822544 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 756 | }; |
| 757 | if ((data().HasPrivateFields + data().HasProtectedFields + |
| 758 | data().HasPublicFields) > 1) |
Chandler Carruth | ec997dc | 2011-04-30 10:07:30 +0000 | [diff] [blame] | 759 | data().IsStandardLayout = false; |
Chandler Carruth | a822544 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 760 | |
Douglas Gregor | 2bb1101 | 2011-05-13 01:05:07 +0000 | [diff] [blame] | 761 | // Keep track of the presence of mutable fields. |
| 762 | if (Field->isMutable()) |
| 763 | data().HasMutableFields = true; |
| 764 | |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 765 | // C++0x [class]p9: |
Douglas Gregor | 9fe183a | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 766 | // A POD struct is a class that is both a trivial class and a |
| 767 | // standard-layout class, and has no non-static data members of type |
| 768 | // non-POD struct, non-POD union (or array of such types). |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 769 | // |
| 770 | // Automatic Reference Counting: the presence of a member of Objective-C pointer type |
| 771 | // that does not explicitly have no lifetime makes the class a non-POD. |
| 772 | // However, we delay setting PlainOldData to false in this case so that |
| 773 | // Sema has a chance to diagnostic causes where the same class will be |
| 774 | // non-POD with Automatic Reference Counting but a POD without Instant Objects. |
| 775 | // In this case, the class will become a non-POD class when we complete |
| 776 | // the definition. |
Douglas Gregor | 9fe183a | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 777 | ASTContext &Context = getASTContext(); |
| 778 | QualType T = Context.getBaseElementType(Field->getType()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 779 | if (T->isObjCRetainableType() || T.isObjCGCStrong()) { |
| 780 | if (!Context.getLangOptions().ObjCAutoRefCount || |
| 781 | T.getObjCLifetime() != Qualifiers::OCL_ExplicitNone) |
| 782 | setHasObjectMember(true); |
| 783 | } else if (!T.isPODType(Context)) |
Douglas Gregor | 9fe183a | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 784 | data().PlainOldData = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 785 | |
Chandler Carruth | a822544 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 786 | if (T->isReferenceType()) { |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 787 | data().HasTrivialDefaultConstructor = false; |
Chandler Carruth | 9b6347c | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 788 | |
Chandler Carruth | a822544 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 789 | // C++0x [class]p7: |
| 790 | // A standard-layout class is a class that: |
| 791 | // -- has no non-static data members of type [...] reference, |
Chandler Carruth | ec997dc | 2011-04-30 10:07:30 +0000 | [diff] [blame] | 792 | data().IsStandardLayout = false; |
Chandler Carruth | a822544 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Chandler Carruth | 9b6347c | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 795 | // Record if this field is the first non-literal field or base. |
Richard Smith | 5fa6a04 | 2011-10-12 05:08:15 +0000 | [diff] [blame] | 796 | // As a slight variation on the standard, we regard mutable members as being |
| 797 | // non-literal, since mutating a constexpr variable would break C++11 |
| 798 | // constant expression semantics. |
| 799 | if ((!hasNonLiteralTypeFieldsOrBases() && !T->isLiteralType()) || |
| 800 | Field->isMutable()) |
Chandler Carruth | 9b6347c | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 801 | data().HasNonLiteralTypeFieldsOrBases = true; |
| 802 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 803 | if (Field->hasInClassInitializer()) { |
| 804 | // C++0x [class]p5: |
| 805 | // A default constructor is trivial if [...] no non-static data member |
| 806 | // of its class has a brace-or-equal-initializer. |
| 807 | data().HasTrivialDefaultConstructor = false; |
| 808 | |
| 809 | // C++0x [dcl.init.aggr]p1: |
| 810 | // An aggregate is a [...] class with [...] no |
| 811 | // brace-or-equal-initializers for non-static data members. |
| 812 | data().Aggregate = false; |
| 813 | |
| 814 | // C++0x [class]p10: |
| 815 | // A POD struct is [...] a trivial class. |
| 816 | data().PlainOldData = false; |
| 817 | } |
| 818 | |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 819 | if (const RecordType *RecordTy = T->getAs<RecordType>()) { |
| 820 | CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 821 | if (FieldRec->getDefinition()) { |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 822 | // C++0x [class.ctor]p5: |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 823 | // A default constructor is trivial [...] if: |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 824 | // -- for all the non-static data members of its class that are of |
| 825 | // class type (or array thereof), each such class has a trivial |
| 826 | // default constructor. |
| 827 | if (!FieldRec->hasTrivialDefaultConstructor()) |
| 828 | data().HasTrivialDefaultConstructor = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 829 | |
| 830 | // C++0x [class.copy]p13: |
| 831 | // A copy/move constructor for class X is trivial if [...] |
| 832 | // [...] |
| 833 | // -- for each non-static data member of X that is of class type (or |
| 834 | // an array thereof), the constructor selected to copy/move that |
| 835 | // member is trivial; |
| 836 | // FIXME: C++0x: We don't correctly model 'selected' constructors. |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 837 | if (!FieldRec->hasTrivialCopyConstructor()) |
| 838 | data().HasTrivialCopyConstructor = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 839 | if (!FieldRec->hasTrivialMoveConstructor()) |
| 840 | data().HasTrivialMoveConstructor = false; |
| 841 | |
| 842 | // C++0x [class.copy]p27: |
| 843 | // A copy/move assignment operator for class X is trivial if [...] |
| 844 | // [...] |
| 845 | // -- for each non-static data member of X that is of class type (or |
| 846 | // an array thereof), the assignment operator selected to |
| 847 | // copy/move that member is trivial; |
| 848 | // FIXME: C++0x: We don't correctly model 'selected' operators. |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 849 | if (!FieldRec->hasTrivialCopyAssignment()) |
| 850 | data().HasTrivialCopyAssignment = false; |
Chandler Carruth | 4d6e5a2 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 851 | if (!FieldRec->hasTrivialMoveAssignment()) |
| 852 | data().HasTrivialMoveAssignment = false; |
| 853 | |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 854 | if (!FieldRec->hasTrivialDestructor()) |
| 855 | data().HasTrivialDestructor = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 856 | if (FieldRec->hasObjectMember()) |
| 857 | setHasObjectMember(true); |
Chandler Carruth | a822544 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 858 | |
| 859 | // C++0x [class]p7: |
| 860 | // A standard-layout class is a class that: |
| 861 | // -- has no non-static data members of type non-standard-layout |
| 862 | // class (or array of such types) [...] |
Chandler Carruth | ec997dc | 2011-04-30 10:07:30 +0000 | [diff] [blame] | 863 | if (!FieldRec->isStandardLayout()) |
| 864 | data().IsStandardLayout = false; |
Chandler Carruth | a822544 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 865 | |
| 866 | // C++0x [class]p7: |
| 867 | // A standard-layout class is a class that: |
| 868 | // [...] |
| 869 | // -- has no base classes of the same type as the first non-static |
| 870 | // data member. |
| 871 | // We don't want to expend bits in the state of the record decl |
| 872 | // tracking whether this is the first non-static data member so we |
| 873 | // cheat a bit and use some of the existing state: the empty bit. |
| 874 | // Virtual bases and virtual methods make a class non-empty, but they |
| 875 | // also make it non-standard-layout so we needn't check here. |
| 876 | // A non-empty base class may leave the class standard-layout, but not |
| 877 | // if we have arrived here, and have at least on non-static data |
Chandler Carruth | ec997dc | 2011-04-30 10:07:30 +0000 | [diff] [blame] | 878 | // member. If IsStandardLayout remains true, then the first non-static |
Chandler Carruth | a822544 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 879 | // data member must come through here with Empty still true, and Empty |
| 880 | // will subsequently be set to false below. |
Chandler Carruth | ec997dc | 2011-04-30 10:07:30 +0000 | [diff] [blame] | 881 | if (data().IsStandardLayout && data().Empty) { |
Chandler Carruth | a822544 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 882 | for (CXXRecordDecl::base_class_const_iterator BI = bases_begin(), |
| 883 | BE = bases_end(); |
| 884 | BI != BE; ++BI) { |
| 885 | if (Context.hasSameUnqualifiedType(BI->getType(), T)) { |
Chandler Carruth | ec997dc | 2011-04-30 10:07:30 +0000 | [diff] [blame] | 886 | data().IsStandardLayout = false; |
Chandler Carruth | a822544 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 887 | break; |
| 888 | } |
| 889 | } |
| 890 | } |
Douglas Gregor | 2bb1101 | 2011-05-13 01:05:07 +0000 | [diff] [blame] | 891 | |
| 892 | // Keep track of the presence of mutable fields. |
| 893 | if (FieldRec->hasMutableFields()) |
| 894 | data().HasMutableFields = true; |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 895 | |
| 896 | // C++11 [class.copy]p13: |
| 897 | // If the implicitly-defined constructor would satisfy the |
| 898 | // requirements of a constexpr constructor, the implicitly-defined |
| 899 | // constructor is constexpr. |
| 900 | // C++11 [dcl.constexpr]p4: |
| 901 | // -- every constructor involved in initializing non-static data |
| 902 | // members [...] shall be a constexpr constructor |
| 903 | if (!Field->hasInClassInitializer() && |
| 904 | !FieldRec->hasConstexprDefaultConstructor()) |
| 905 | // The standard requires any in-class initializer to be a constant |
| 906 | // expression. We consider this to be a defect. |
| 907 | data().DefaultedDefaultConstructorIsConstexpr = false; |
| 908 | |
| 909 | if (!FieldRec->hasConstexprCopyConstructor()) |
| 910 | data().DefaultedCopyConstructorIsConstexpr = false; |
| 911 | |
| 912 | if (FieldRec->hasDeclaredMoveConstructor() || |
| 913 | FieldRec->needsImplicitMoveConstructor()) |
| 914 | // FIXME: If the implicit move constructor generated for the member's |
| 915 | // class would be ill-formed, the implicit move constructor generated |
| 916 | // for this class calls the member's copy constructor. |
| 917 | data().DefaultedMoveConstructorIsConstexpr &= |
| 918 | FieldRec->hasConstexprMoveConstructor(); |
| 919 | else if (!FieldRec->hasConstexprCopyConstructor()) |
| 920 | data().DefaultedMoveConstructorIsConstexpr = false; |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 921 | } |
Richard Smith | 6180245 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 922 | } else { |
| 923 | // Base element type of field is a non-class type. |
| 924 | if (!T->isLiteralType()) { |
| 925 | data().DefaultedDefaultConstructorIsConstexpr = false; |
| 926 | data().DefaultedCopyConstructorIsConstexpr = false; |
| 927 | data().DefaultedMoveConstructorIsConstexpr = false; |
| 928 | } else if (!Field->hasInClassInitializer()) |
| 929 | data().DefaultedDefaultConstructorIsConstexpr = false; |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 930 | } |
Chandler Carruth | a822544 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 931 | |
| 932 | // C++0x [class]p7: |
| 933 | // A standard-layout class is a class that: |
| 934 | // [...] |
| 935 | // -- either has no non-static data members in the most derived |
| 936 | // class and at most one base class with non-static data members, |
| 937 | // or has no base classes with non-static data members, and |
| 938 | // At this point we know that we have a non-static data member, so the last |
| 939 | // clause holds. |
| 940 | if (!data().HasNoNonEmptyBases) |
Chandler Carruth | ec997dc | 2011-04-30 10:07:30 +0000 | [diff] [blame] | 941 | data().IsStandardLayout = false; |
Chandler Carruth | a822544 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 942 | |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 943 | // If this is not a zero-length bit-field, then the class is not empty. |
| 944 | if (data().Empty) { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 945 | if (!Field->isBitField() || |
| 946 | (!Field->getBitWidth()->isTypeDependent() && |
| 947 | !Field->getBitWidth()->isValueDependent() && |
| 948 | Field->getBitWidthValue(Context) != 0)) |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 949 | data().Empty = false; |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 950 | } |
Douglas Gregor | 9fe183a | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 951 | } |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 952 | |
| 953 | // Handle using declarations of conversion functions. |
| 954 | if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) |
| 955 | if (Shadow->getDeclName().getNameKind() |
| 956 | == DeclarationName::CXXConversionFunctionName) |
| 957 | data().Conversions.addDecl(Shadow, Shadow->getAccess()); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 958 | } |
| 959 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 960 | static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) { |
| 961 | QualType T; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 962 | if (isa<UsingShadowDecl>(Conv)) |
| 963 | Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl(); |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 964 | if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv)) |
| 965 | T = ConvTemp->getTemplatedDecl()->getResultType(); |
| 966 | else |
| 967 | T = cast<CXXConversionDecl>(Conv)->getConversionType(); |
| 968 | return Context.getCanonicalType(T); |
Fariborz Jahanian | 0351a1e | 2009-10-07 20:43:36 +0000 | [diff] [blame] | 969 | } |
| 970 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 971 | /// Collect the visible conversions of a base class. |
| 972 | /// |
| 973 | /// \param Base a base class of the class we're considering |
| 974 | /// \param InVirtual whether this base class is a virtual base (or a base |
| 975 | /// of a virtual base) |
| 976 | /// \param Access the access along the inheritance path to this base |
| 977 | /// \param ParentHiddenTypes the conversions provided by the inheritors |
| 978 | /// of this base |
| 979 | /// \param Output the set to which to add conversions from non-virtual bases |
| 980 | /// \param VOutput the set to which to add conversions from virtual bases |
| 981 | /// \param HiddenVBaseCs the set of conversions which were hidden in a |
| 982 | /// virtual base along some inheritance path |
| 983 | static void CollectVisibleConversions(ASTContext &Context, |
| 984 | CXXRecordDecl *Record, |
| 985 | bool InVirtual, |
| 986 | AccessSpecifier Access, |
| 987 | const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes, |
| 988 | UnresolvedSetImpl &Output, |
| 989 | UnresolvedSetImpl &VOutput, |
| 990 | llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) { |
| 991 | // The set of types which have conversions in this class or its |
| 992 | // subclasses. As an optimization, we don't copy the derived set |
| 993 | // unless it might change. |
| 994 | const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes; |
| 995 | llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer; |
| 996 | |
| 997 | // Collect the direct conversions and figure out which conversions |
| 998 | // will be hidden in the subclasses. |
| 999 | UnresolvedSetImpl &Cs = *Record->getConversionFunctions(); |
| 1000 | if (!Cs.empty()) { |
| 1001 | HiddenTypesBuffer = ParentHiddenTypes; |
| 1002 | HiddenTypes = &HiddenTypesBuffer; |
| 1003 | |
| 1004 | for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) { |
| 1005 | bool Hidden = |
| 1006 | !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl())); |
| 1007 | |
| 1008 | // If this conversion is hidden and we're in a virtual base, |
| 1009 | // remember that it's hidden along some inheritance path. |
| 1010 | if (Hidden && InVirtual) |
| 1011 | HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())); |
| 1012 | |
| 1013 | // If this conversion isn't hidden, add it to the appropriate output. |
| 1014 | else if (!Hidden) { |
| 1015 | AccessSpecifier IAccess |
| 1016 | = CXXRecordDecl::MergeAccess(Access, I.getAccess()); |
| 1017 | |
| 1018 | if (InVirtual) |
| 1019 | VOutput.addDecl(I.getDecl(), IAccess); |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 1020 | else |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1021 | Output.addDecl(I.getDecl(), IAccess); |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 1022 | } |
| 1023 | } |
| 1024 | } |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 1025 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1026 | // Collect information recursively from any base classes. |
| 1027 | for (CXXRecordDecl::base_class_iterator |
| 1028 | I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) { |
| 1029 | const RecordType *RT = I->getType()->getAs<RecordType>(); |
| 1030 | if (!RT) continue; |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 1031 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1032 | AccessSpecifier BaseAccess |
| 1033 | = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier()); |
| 1034 | bool BaseInVirtual = InVirtual || I->isVirtual(); |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 1035 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1036 | CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl()); |
| 1037 | CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess, |
| 1038 | *HiddenTypes, Output, VOutput, HiddenVBaseCs); |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 1039 | } |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1040 | } |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 1041 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1042 | /// Collect the visible conversions of a class. |
| 1043 | /// |
| 1044 | /// This would be extremely straightforward if it weren't for virtual |
| 1045 | /// bases. It might be worth special-casing that, really. |
| 1046 | static void CollectVisibleConversions(ASTContext &Context, |
| 1047 | CXXRecordDecl *Record, |
| 1048 | UnresolvedSetImpl &Output) { |
| 1049 | // The collection of all conversions in virtual bases that we've |
| 1050 | // found. These will be added to the output as long as they don't |
| 1051 | // appear in the hidden-conversions set. |
| 1052 | UnresolvedSet<8> VBaseCs; |
| 1053 | |
| 1054 | // The set of conversions in virtual bases that we've determined to |
| 1055 | // be hidden. |
| 1056 | llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs; |
| 1057 | |
| 1058 | // The set of types hidden by classes derived from this one. |
| 1059 | llvm::SmallPtrSet<CanQualType, 8> HiddenTypes; |
| 1060 | |
| 1061 | // Go ahead and collect the direct conversions and add them to the |
| 1062 | // hidden-types set. |
| 1063 | UnresolvedSetImpl &Cs = *Record->getConversionFunctions(); |
| 1064 | Output.append(Cs.begin(), Cs.end()); |
| 1065 | for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) |
| 1066 | HiddenTypes.insert(GetConversionType(Context, I.getDecl())); |
| 1067 | |
| 1068 | // Recursively collect conversions from base classes. |
| 1069 | for (CXXRecordDecl::base_class_iterator |
| 1070 | I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) { |
| 1071 | const RecordType *RT = I->getType()->getAs<RecordType>(); |
| 1072 | if (!RT) continue; |
| 1073 | |
| 1074 | CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()), |
| 1075 | I->isVirtual(), I->getAccessSpecifier(), |
| 1076 | HiddenTypes, Output, VBaseCs, HiddenVBaseCs); |
| 1077 | } |
| 1078 | |
| 1079 | // Add any unhidden conversions provided by virtual bases. |
| 1080 | for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end(); |
| 1081 | I != E; ++I) { |
| 1082 | if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()))) |
| 1083 | Output.addDecl(I.getDecl(), I.getAccess()); |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 1084 | } |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | /// getVisibleConversionFunctions - get all conversion functions visible |
| 1088 | /// in current class; including conversion function templates. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 1089 | const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() { |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 1090 | // If root class, all conversions are visible. |
| 1091 | if (bases_begin() == bases_end()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1092 | return &data().Conversions; |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 1093 | // If visible conversion list is already evaluated, return it. |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1094 | if (data().ComputedVisibleConversions) |
| 1095 | return &data().VisibleConversions; |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1096 | CollectVisibleConversions(getASTContext(), this, data().VisibleConversions); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 1097 | data().ComputedVisibleConversions = true; |
| 1098 | return &data().VisibleConversions; |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1101 | void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) { |
| 1102 | // This operation is O(N) but extremely rare. Sema only uses it to |
| 1103 | // remove UsingShadowDecls in a class that were followed by a direct |
| 1104 | // declaration, e.g.: |
| 1105 | // class A : B { |
| 1106 | // using B::operator int; |
| 1107 | // operator int(); |
| 1108 | // }; |
| 1109 | // This is uncommon by itself and even more uncommon in conjunction |
| 1110 | // with sufficiently large numbers of directly-declared conversions |
| 1111 | // that asymptotic behavior matters. |
| 1112 | |
| 1113 | UnresolvedSetImpl &Convs = *getConversionFunctions(); |
| 1114 | for (unsigned I = 0, E = Convs.size(); I != E; ++I) { |
| 1115 | if (Convs[I].getDecl() == ConvDecl) { |
| 1116 | Convs.erase(I); |
| 1117 | assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end() |
| 1118 | && "conversion was found multiple times in unresolved set"); |
| 1119 | return; |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | llvm_unreachable("conversion not found in set!"); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1124 | } |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 1125 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1126 | CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1127 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1128 | return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom()); |
| 1129 | |
| 1130 | return 0; |
| 1131 | } |
| 1132 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1133 | MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const { |
| 1134 | return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>(); |
| 1135 | } |
| 1136 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1137 | void |
| 1138 | CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD, |
| 1139 | TemplateSpecializationKind TSK) { |
| 1140 | assert(TemplateOrInstantiation.isNull() && |
| 1141 | "Previous template or instantiation?"); |
| 1142 | assert(!isa<ClassTemplateSpecializationDecl>(this)); |
| 1143 | TemplateOrInstantiation |
| 1144 | = new (getASTContext()) MemberSpecializationInfo(RD, TSK); |
| 1145 | } |
| 1146 | |
Anders Carlsson | b13e357 | 2009-12-07 06:33:48 +0000 | [diff] [blame] | 1147 | TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{ |
| 1148 | if (const ClassTemplateSpecializationDecl *Spec |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1149 | = dyn_cast<ClassTemplateSpecializationDecl>(this)) |
| 1150 | return Spec->getSpecializationKind(); |
| 1151 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1152 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1153 | return MSInfo->getTemplateSpecializationKind(); |
| 1154 | |
| 1155 | return TSK_Undeclared; |
| 1156 | } |
| 1157 | |
| 1158 | void |
| 1159 | CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) { |
| 1160 | if (ClassTemplateSpecializationDecl *Spec |
| 1161 | = dyn_cast<ClassTemplateSpecializationDecl>(this)) { |
| 1162 | Spec->setSpecializationKind(TSK); |
| 1163 | return; |
| 1164 | } |
| 1165 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1166 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1167 | MSInfo->setTemplateSpecializationKind(TSK); |
| 1168 | return; |
| 1169 | } |
| 1170 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1171 | llvm_unreachable("Not a class template or member class specialization"); |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Douglas Gregor | 1d110e0 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 1174 | CXXDestructorDecl *CXXRecordDecl::getDestructor() const { |
| 1175 | ASTContext &Context = getASTContext(); |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 1176 | QualType ClassType = Context.getTypeDeclType(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1177 | |
| 1178 | DeclarationName Name |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 1179 | = Context.DeclarationNames.getCXXDestructorName( |
| 1180 | Context.getCanonicalType(ClassType)); |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 1181 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1182 | DeclContext::lookup_const_iterator I, E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1183 | llvm::tie(I, E) = lookup(Name); |
Sebastian Redl | d4b25cb | 2010-09-02 23:19:42 +0000 | [diff] [blame] | 1184 | if (I == E) |
| 1185 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1186 | |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 1187 | CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I); |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 1188 | return Dtor; |
| 1189 | } |
| 1190 | |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 1191 | void CXXRecordDecl::completeDefinition() { |
| 1192 | completeDefinition(0); |
| 1193 | } |
| 1194 | |
| 1195 | void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) { |
| 1196 | RecordDecl::completeDefinition(); |
| 1197 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1198 | if (hasObjectMember() && getASTContext().getLangOptions().ObjCAutoRefCount) { |
| 1199 | // Objective-C Automatic Reference Counting: |
| 1200 | // If a class has a non-static data member of Objective-C pointer |
| 1201 | // type (or array thereof), it is a non-POD type and its |
| 1202 | // default constructor (if any), copy constructor, copy assignment |
| 1203 | // operator, and destructor are non-trivial. |
| 1204 | struct DefinitionData &Data = data(); |
| 1205 | Data.PlainOldData = false; |
| 1206 | Data.HasTrivialDefaultConstructor = false; |
| 1207 | Data.HasTrivialCopyConstructor = false; |
| 1208 | Data.HasTrivialCopyAssignment = false; |
| 1209 | Data.HasTrivialDestructor = false; |
| 1210 | } |
| 1211 | |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 1212 | // If the class may be abstract (but hasn't been marked as such), check for |
| 1213 | // any pure final overriders. |
| 1214 | if (mayBeAbstract()) { |
| 1215 | CXXFinalOverriderMap MyFinalOverriders; |
| 1216 | if (!FinalOverriders) { |
| 1217 | getFinalOverriders(MyFinalOverriders); |
| 1218 | FinalOverriders = &MyFinalOverriders; |
| 1219 | } |
| 1220 | |
| 1221 | bool Done = false; |
| 1222 | for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(), |
| 1223 | MEnd = FinalOverriders->end(); |
| 1224 | M != MEnd && !Done; ++M) { |
| 1225 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 1226 | SOEnd = M->second.end(); |
| 1227 | SO != SOEnd && !Done; ++SO) { |
| 1228 | assert(SO->second.size() > 0 && |
| 1229 | "All virtual functions have overridding virtual functions"); |
| 1230 | |
| 1231 | // C++ [class.abstract]p4: |
| 1232 | // A class is abstract if it contains or inherits at least one |
| 1233 | // pure virtual function for which the final overrider is pure |
| 1234 | // virtual. |
| 1235 | if (SO->second.front().Method->isPure()) { |
| 1236 | data().Abstract = true; |
| 1237 | Done = true; |
| 1238 | break; |
| 1239 | } |
| 1240 | } |
| 1241 | } |
| 1242 | } |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 1243 | |
| 1244 | // Set access bits correctly on the directly-declared conversions. |
| 1245 | for (UnresolvedSetIterator I = data().Conversions.begin(), |
| 1246 | E = data().Conversions.end(); |
| 1247 | I != E; ++I) |
| 1248 | data().Conversions.setAccess(I, (*I)->getAccess()); |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | bool CXXRecordDecl::mayBeAbstract() const { |
| 1252 | if (data().Abstract || isInvalidDecl() || !data().Polymorphic || |
| 1253 | isDependentContext()) |
| 1254 | return false; |
| 1255 | |
| 1256 | for (CXXRecordDecl::base_class_const_iterator B = bases_begin(), |
| 1257 | BEnd = bases_end(); |
| 1258 | B != BEnd; ++B) { |
| 1259 | CXXRecordDecl *BaseDecl |
| 1260 | = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl()); |
| 1261 | if (BaseDecl->isAbstract()) |
| 1262 | return true; |
| 1263 | } |
| 1264 | |
| 1265 | return false; |
| 1266 | } |
| 1267 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 1268 | void CXXMethodDecl::anchor() { } |
| 1269 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1270 | CXXMethodDecl * |
| 1271 | CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1272 | SourceLocation StartLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1273 | const DeclarationNameInfo &NameInfo, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1274 | QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 1275 | bool isStatic, StorageClass SCAsWritten, bool isInline, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 1276 | bool isConstexpr, SourceLocation EndLocation) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1277 | return new (C) CXXMethodDecl(CXXMethod, RD, StartLoc, NameInfo, T, TInfo, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 1278 | isStatic, SCAsWritten, isInline, isConstexpr, |
| 1279 | EndLocation); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1282 | CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 1283 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXMethodDecl)); |
| 1284 | return new (Mem) CXXMethodDecl(CXXMethod, 0, SourceLocation(), |
| 1285 | DeclarationNameInfo(), QualType(), |
| 1286 | 0, false, SC_None, false, false, |
| 1287 | SourceLocation()); |
| 1288 | } |
| 1289 | |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1290 | bool CXXMethodDecl::isUsualDeallocationFunction() const { |
| 1291 | if (getOverloadedOperator() != OO_Delete && |
| 1292 | getOverloadedOperator() != OO_Array_Delete) |
| 1293 | return false; |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 1294 | |
| 1295 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 1296 | // A template instance is never a usual deallocation function, |
| 1297 | // regardless of its signature. |
| 1298 | if (getPrimaryTemplate()) |
| 1299 | return false; |
| 1300 | |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1301 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 1302 | // If a class T has a member deallocation function named operator delete |
| 1303 | // with exactly one parameter, then that function is a usual (non-placement) |
| 1304 | // deallocation function. [...] |
| 1305 | if (getNumParams() == 1) |
| 1306 | return true; |
| 1307 | |
| 1308 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 1309 | // [...] If class T does not declare such an operator delete but does |
| 1310 | // declare a member deallocation function named operator delete with |
| 1311 | // exactly two parameters, the second of which has type std::size_t (18.1), |
| 1312 | // then this function is a usual deallocation function. |
| 1313 | ASTContext &Context = getASTContext(); |
| 1314 | if (getNumParams() != 2 || |
Chandler Carruth | e228ba9 | 2010-02-08 18:54:05 +0000 | [diff] [blame] | 1315 | !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(), |
| 1316 | Context.getSizeType())) |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1317 | return false; |
| 1318 | |
| 1319 | // This function is a usual deallocation function if there are no |
| 1320 | // single-parameter deallocation functions of the same kind. |
| 1321 | for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName()); |
| 1322 | R.first != R.second; ++R.first) { |
| 1323 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first)) |
| 1324 | if (FD->getNumParams() == 1) |
| 1325 | return false; |
| 1326 | } |
| 1327 | |
| 1328 | return true; |
| 1329 | } |
| 1330 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 1331 | bool CXXMethodDecl::isCopyAssignmentOperator() const { |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 1332 | // C++0x [class.copy]p17: |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 1333 | // A user-declared copy assignment operator X::operator= is a non-static |
| 1334 | // non-template member function of class X with exactly one parameter of |
| 1335 | // type X, X&, const X&, volatile X& or const volatile X&. |
| 1336 | if (/*operator=*/getOverloadedOperator() != OO_Equal || |
| 1337 | /*non-static*/ isStatic() || |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 1338 | /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate()) |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 1339 | return false; |
| 1340 | |
| 1341 | QualType ParamType = getParamDecl(0)->getType(); |
| 1342 | if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>()) |
| 1343 | ParamType = Ref->getPointeeType(); |
| 1344 | |
| 1345 | ASTContext &Context = getASTContext(); |
| 1346 | QualType ClassType |
| 1347 | = Context.getCanonicalType(Context.getTypeDeclType(getParent())); |
| 1348 | return Context.hasSameUnqualifiedType(ClassType, ParamType); |
| 1349 | } |
| 1350 | |
Sean Hunt | ffe37fd | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 1351 | bool CXXMethodDecl::isMoveAssignmentOperator() const { |
| 1352 | // C++0x [class.copy]p19: |
| 1353 | // A user-declared move assignment operator X::operator= is a non-static |
| 1354 | // non-template member function of class X with exactly one parameter of type |
| 1355 | // X&&, const X&&, volatile X&&, or const volatile X&&. |
| 1356 | if (getOverloadedOperator() != OO_Equal || isStatic() || |
| 1357 | getPrimaryTemplate() || getDescribedFunctionTemplate()) |
| 1358 | return false; |
| 1359 | |
| 1360 | QualType ParamType = getParamDecl(0)->getType(); |
| 1361 | if (!isa<RValueReferenceType>(ParamType)) |
| 1362 | return false; |
| 1363 | ParamType = ParamType->getPointeeType(); |
| 1364 | |
| 1365 | ASTContext &Context = getASTContext(); |
| 1366 | QualType ClassType |
| 1367 | = Context.getCanonicalType(Context.getTypeDeclType(getParent())); |
| 1368 | return Context.hasSameUnqualifiedType(ClassType, ParamType); |
| 1369 | } |
| 1370 | |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 1371 | void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) { |
Anders Carlsson | 3aaf486 | 2009-12-04 05:51:56 +0000 | [diff] [blame] | 1372 | assert(MD->isCanonicalDecl() && "Method is not canonical!"); |
Anders Carlsson | c076c45 | 2010-01-30 17:42:34 +0000 | [diff] [blame] | 1373 | assert(!MD->getParent()->isDependentContext() && |
| 1374 | "Can't add an overridden method to a class template!"); |
| 1375 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1376 | getASTContext().addOverriddenMethod(this, MD); |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const { |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1380 | return getASTContext().overridden_methods_begin(this); |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const { |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 1384 | return getASTContext().overridden_methods_end(this); |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 1387 | unsigned CXXMethodDecl::size_overridden_methods() const { |
| 1388 | return getASTContext().overridden_methods_size(this); |
| 1389 | } |
| 1390 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1391 | QualType CXXMethodDecl::getThisType(ASTContext &C) const { |
Argyrios Kyrtzidis | b0d178d | 2008-10-24 22:28:18 +0000 | [diff] [blame] | 1392 | // C++ 9.3.2p1: The type of this in a member function of a class X is X*. |
| 1393 | // If the member function is declared const, the type of this is const X*, |
| 1394 | // if the member function is declared volatile, the type of this is |
| 1395 | // volatile X*, and if the member function is declared const volatile, |
| 1396 | // the type of this is const volatile X*. |
| 1397 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1398 | assert(isInstance() && "No 'this' for static methods!"); |
Anders Carlsson | 31a0875 | 2009-06-13 02:59:33 +0000 | [diff] [blame] | 1399 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1400 | QualType ClassTy = C.getTypeDeclType(getParent()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1401 | ClassTy = C.getQualifiedType(ClassTy, |
| 1402 | Qualifiers::fromCVRMask(getTypeQualifiers())); |
Anders Carlsson | 4e57992 | 2009-07-10 21:35:09 +0000 | [diff] [blame] | 1403 | return C.getPointerType(ClassTy); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
Eli Friedman | d7d7f67 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 1406 | bool CXXMethodDecl::hasInlineBody() const { |
Douglas Gregor | bd6d619 | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 1407 | // If this function is a template instantiation, look at the template from |
| 1408 | // which it was instantiated. |
| 1409 | const FunctionDecl *CheckFn = getTemplateInstantiationPattern(); |
| 1410 | if (!CheckFn) |
| 1411 | CheckFn = this; |
| 1412 | |
Eli Friedman | d7d7f67 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 1413 | const FunctionDecl *fn; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1414 | return CheckFn->hasBody(fn) && !fn->isOutOfLine(); |
Eli Friedman | d7d7f67 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1417 | CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, |
| 1418 | TypeSourceInfo *TInfo, bool IsVirtual, |
| 1419 | SourceLocation L, Expr *Init, |
| 1420 | SourceLocation R, |
| 1421 | SourceLocation EllipsisLoc) |
Sean Hunt | f51d0b6 | 2011-01-08 23:01:16 +0000 | [diff] [blame] | 1422 | : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init), |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 1423 | LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual), |
| 1424 | IsWritten(false), SourceOrderOrNumArrayIndices(0) |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1425 | { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1428 | CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, |
| 1429 | FieldDecl *Member, |
| 1430 | SourceLocation MemberLoc, |
| 1431 | SourceLocation L, Expr *Init, |
| 1432 | SourceLocation R) |
Sean Hunt | f51d0b6 | 2011-01-08 23:01:16 +0000 | [diff] [blame] | 1433 | : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init), |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 1434 | LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false), |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1435 | IsWritten(false), SourceOrderOrNumArrayIndices(0) |
| 1436 | { |
| 1437 | } |
| 1438 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1439 | CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, |
| 1440 | IndirectFieldDecl *Member, |
| 1441 | SourceLocation MemberLoc, |
| 1442 | SourceLocation L, Expr *Init, |
| 1443 | SourceLocation R) |
Sean Hunt | f51d0b6 | 2011-01-08 23:01:16 +0000 | [diff] [blame] | 1444 | : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init), |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 1445 | LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false), |
Abramo Bagnara | a0af3b4 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 1446 | IsWritten(false), SourceOrderOrNumArrayIndices(0) |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1447 | { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1450 | CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 1451 | TypeSourceInfo *TInfo, |
| 1452 | SourceLocation L, Expr *Init, |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1453 | SourceLocation R) |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 1454 | : Initializee(TInfo), MemberOrEllipsisLocation(), Init(Init), |
| 1455 | LParenLoc(L), RParenLoc(R), IsDelegating(true), IsVirtual(false), |
Sean Hunt | 4171766 | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 1456 | IsWritten(false), SourceOrderOrNumArrayIndices(0) |
| 1457 | { |
| 1458 | } |
| 1459 | |
| 1460 | CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1461 | FieldDecl *Member, |
| 1462 | SourceLocation MemberLoc, |
| 1463 | SourceLocation L, Expr *Init, |
| 1464 | SourceLocation R, |
| 1465 | VarDecl **Indices, |
| 1466 | unsigned NumIndices) |
Sean Hunt | f51d0b6 | 2011-01-08 23:01:16 +0000 | [diff] [blame] | 1467 | : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init), |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1468 | LParenLoc(L), RParenLoc(R), IsVirtual(false), |
Abramo Bagnara | a0af3b4 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 1469 | IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1470 | { |
| 1471 | VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1); |
| 1472 | memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *)); |
| 1473 | } |
| 1474 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1475 | CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context, |
| 1476 | FieldDecl *Member, |
| 1477 | SourceLocation MemberLoc, |
| 1478 | SourceLocation L, Expr *Init, |
| 1479 | SourceLocation R, |
| 1480 | VarDecl **Indices, |
| 1481 | unsigned NumIndices) { |
| 1482 | void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) + |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1483 | sizeof(VarDecl *) * NumIndices, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1484 | llvm::alignOf<CXXCtorInitializer>()); |
Sean Hunt | f51d0b6 | 2011-01-08 23:01:16 +0000 | [diff] [blame] | 1485 | return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R, |
| 1486 | Indices, NumIndices); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1489 | TypeLoc CXXCtorInitializer::getBaseClassLoc() const { |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1490 | if (isBaseInitializer()) |
Sean Hunt | f51d0b6 | 2011-01-08 23:01:16 +0000 | [diff] [blame] | 1491 | return Initializee.get<TypeSourceInfo*>()->getTypeLoc(); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1492 | else |
| 1493 | return TypeLoc(); |
| 1494 | } |
| 1495 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1496 | const Type *CXXCtorInitializer::getBaseClass() const { |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1497 | if (isBaseInitializer()) |
Sean Hunt | f51d0b6 | 2011-01-08 23:01:16 +0000 | [diff] [blame] | 1498 | return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr(); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1499 | else |
| 1500 | return 0; |
| 1501 | } |
| 1502 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1503 | SourceLocation CXXCtorInitializer::getSourceLocation() const { |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 1504 | if (isAnyMemberInitializer()) |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1505 | return getMemberLocation(); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1506 | |
| 1507 | if (isInClassMemberInitializer()) |
| 1508 | return getAnyMember()->getLocation(); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1509 | |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 1510 | if (TypeSourceInfo *TSInfo = Initializee.get<TypeSourceInfo*>()) |
| 1511 | return TSInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
| 1512 | |
| 1513 | return SourceLocation(); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1516 | SourceRange CXXCtorInitializer::getSourceRange() const { |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1517 | if (isInClassMemberInitializer()) { |
| 1518 | FieldDecl *D = getAnyMember(); |
| 1519 | if (Expr *I = D->getInClassInitializer()) |
| 1520 | return I->getSourceRange(); |
| 1521 | return SourceRange(); |
| 1522 | } |
| 1523 | |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1524 | return SourceRange(getSourceLocation(), getRParenLoc()); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 1527 | void CXXConstructorDecl::anchor() { } |
| 1528 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1529 | CXXConstructorDecl * |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1530 | CXXConstructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 1531 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConstructorDecl)); |
| 1532 | return new (Mem) CXXConstructorDecl(0, SourceLocation(),DeclarationNameInfo(), |
| 1533 | QualType(), 0, false, false, false,false); |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | CXXConstructorDecl * |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1537 | CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1538 | SourceLocation StartLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1539 | const DeclarationNameInfo &NameInfo, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1540 | QualType T, TypeSourceInfo *TInfo, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 1541 | bool isExplicit, bool isInline, |
| 1542 | bool isImplicitlyDeclared, bool isConstexpr) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1543 | assert(NameInfo.getName().getNameKind() |
| 1544 | == DeclarationName::CXXConstructorName && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1545 | "Name must refer to a constructor"); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1546 | return new (C) CXXConstructorDecl(RD, StartLoc, NameInfo, T, TInfo, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 1547 | isExplicit, isInline, isImplicitlyDeclared, |
| 1548 | isConstexpr); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
Douglas Gregor | 76852c2 | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 1551 | CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const { |
| 1552 | assert(isDelegatingConstructor() && "Not a delegating constructor!"); |
| 1553 | Expr *E = (*init_begin())->getInit()->IgnoreImplicit(); |
| 1554 | if (CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(E)) |
| 1555 | return Construct->getConstructor(); |
| 1556 | |
| 1557 | return 0; |
| 1558 | } |
| 1559 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1560 | bool CXXConstructorDecl::isDefaultConstructor() const { |
| 1561 | // C++ [class.ctor]p5: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 1562 | // A default constructor for a class X is a constructor of class |
| 1563 | // X that can be called without an argument. |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1564 | return (getNumParams() == 0) || |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 1565 | (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg()); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1568 | bool |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 1569 | CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const { |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1570 | return isCopyOrMoveConstructor(TypeQuals) && |
| 1571 | getParamDecl(0)->getType()->isLValueReferenceType(); |
| 1572 | } |
| 1573 | |
| 1574 | bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const { |
| 1575 | return isCopyOrMoveConstructor(TypeQuals) && |
| 1576 | getParamDecl(0)->getType()->isRValueReferenceType(); |
| 1577 | } |
| 1578 | |
| 1579 | /// \brief Determine whether this is a copy or move constructor. |
| 1580 | bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const { |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1581 | // C++ [class.copy]p2: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 1582 | // A non-template constructor for class X is a copy constructor |
| 1583 | // if its first parameter is of type X&, const X&, volatile X& or |
| 1584 | // const volatile X&, and either there are no other parameters |
| 1585 | // or else all other parameters have default arguments (8.3.6). |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1586 | // C++0x [class.copy]p3: |
| 1587 | // A non-template constructor for class X is a move constructor if its |
| 1588 | // first parameter is of type X&&, const X&&, volatile X&&, or |
| 1589 | // const volatile X&&, and either there are no other parameters or else |
| 1590 | // all other parameters have default arguments. |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1591 | if ((getNumParams() < 1) || |
Douglas Gregor | 77da3f4 | 2009-10-13 23:45:19 +0000 | [diff] [blame] | 1592 | (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 1593 | (getPrimaryTemplate() != 0) || |
Douglas Gregor | 77da3f4 | 2009-10-13 23:45:19 +0000 | [diff] [blame] | 1594 | (getDescribedFunctionTemplate() != 0)) |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1595 | return false; |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1596 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1597 | const ParmVarDecl *Param = getParamDecl(0); |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1598 | |
| 1599 | // Do we have a reference type? |
| 1600 | const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>(); |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 1601 | if (!ParamRefType) |
| 1602 | return false; |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1603 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 1604 | // Is it a reference to our class type? |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 1605 | ASTContext &Context = getASTContext(); |
| 1606 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 1607 | CanQualType PointeeType |
| 1608 | = Context.getCanonicalType(ParamRefType->getPointeeType()); |
Douglas Gregor | 14e0b3d | 2009-09-15 20:50:23 +0000 | [diff] [blame] | 1609 | CanQualType ClassTy |
| 1610 | = Context.getCanonicalType(Context.getTagDeclType(getParent())); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1611 | if (PointeeType.getUnqualifiedType() != ClassTy) |
| 1612 | return false; |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1613 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1614 | // FIXME: other qualifiers? |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1615 | |
| 1616 | // We have a copy or move constructor. |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1617 | TypeQuals = PointeeType.getCVRQualifiers(); |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1618 | return true; |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1621 | bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1622 | // C++ [class.conv.ctor]p1: |
| 1623 | // A constructor declared without the function-specifier explicit |
| 1624 | // that can be called with a single parameter specifies a |
| 1625 | // conversion from the type of its first parameter to the type of |
| 1626 | // its class. Such a constructor is called a converting |
| 1627 | // constructor. |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1628 | if (isExplicit() && !AllowExplicit) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1629 | return false; |
| 1630 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1631 | return (getNumParams() == 0 && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1632 | getType()->getAs<FunctionProtoType>()->isVariadic()) || |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1633 | (getNumParams() == 1) || |
Anders Carlsson | ae0b4e7 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 1634 | (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg()); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1635 | } |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1636 | |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 1637 | bool CXXConstructorDecl::isSpecializationCopyingObject() const { |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1638 | if ((getNumParams() < 1) || |
| 1639 | (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || |
| 1640 | (getPrimaryTemplate() == 0) || |
| 1641 | (getDescribedFunctionTemplate() != 0)) |
| 1642 | return false; |
| 1643 | |
| 1644 | const ParmVarDecl *Param = getParamDecl(0); |
| 1645 | |
| 1646 | ASTContext &Context = getASTContext(); |
| 1647 | CanQualType ParamType = Context.getCanonicalType(Param->getType()); |
| 1648 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1649 | // Is it the same as our our class type? |
| 1650 | CanQualType ClassTy |
| 1651 | = Context.getCanonicalType(Context.getTagDeclType(getParent())); |
| 1652 | if (ParamType.getUnqualifiedType() != ClassTy) |
| 1653 | return false; |
| 1654 | |
| 1655 | return true; |
| 1656 | } |
| 1657 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame] | 1658 | const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const { |
| 1659 | // Hack: we store the inherited constructor in the overridden method table |
| 1660 | method_iterator It = begin_overridden_methods(); |
| 1661 | if (It == end_overridden_methods()) |
| 1662 | return 0; |
| 1663 | |
| 1664 | return cast<CXXConstructorDecl>(*It); |
| 1665 | } |
| 1666 | |
| 1667 | void |
| 1668 | CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){ |
| 1669 | // Hack: we store the inherited constructor in the overridden method table |
| 1670 | assert(size_overridden_methods() == 0 && "Base ctor already set."); |
| 1671 | addOverriddenMethod(BaseCtor); |
| 1672 | } |
| 1673 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 1674 | void CXXDestructorDecl::anchor() { } |
| 1675 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1676 | CXXDestructorDecl * |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1677 | CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 1678 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXDestructorDecl)); |
| 1679 | return new (Mem) CXXDestructorDecl(0, SourceLocation(), DeclarationNameInfo(), |
Craig Silverstein | b41d899 | 2010-10-21 00:44:50 +0000 | [diff] [blame] | 1680 | QualType(), 0, false, false); |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | CXXDestructorDecl * |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1684 | CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1685 | SourceLocation StartLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1686 | const DeclarationNameInfo &NameInfo, |
Craig Silverstein | b41d899 | 2010-10-21 00:44:50 +0000 | [diff] [blame] | 1687 | QualType T, TypeSourceInfo *TInfo, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 1688 | bool isInline, bool isImplicitlyDeclared) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1689 | assert(NameInfo.getName().getNameKind() |
| 1690 | == DeclarationName::CXXDestructorName && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1691 | "Name must refer to a destructor"); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1692 | return new (C) CXXDestructorDecl(RD, StartLoc, NameInfo, T, TInfo, isInline, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1693 | isImplicitlyDeclared); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 1696 | void CXXConversionDecl::anchor() { } |
| 1697 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1698 | CXXConversionDecl * |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1699 | CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 1700 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(CXXConversionDecl)); |
| 1701 | return new (Mem) CXXConversionDecl(0, SourceLocation(), DeclarationNameInfo(), |
| 1702 | QualType(), 0, false, false, false, |
| 1703 | SourceLocation()); |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | CXXConversionDecl * |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1707 | CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1708 | SourceLocation StartLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1709 | const DeclarationNameInfo &NameInfo, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1710 | QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | f525160 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 1711 | bool isInline, bool isExplicit, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 1712 | bool isConstexpr, SourceLocation EndLocation) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1713 | assert(NameInfo.getName().getNameKind() |
| 1714 | == DeclarationName::CXXConversionFunctionName && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1715 | "Name must refer to a conversion function"); |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1716 | return new (C) CXXConversionDecl(RD, StartLoc, NameInfo, T, TInfo, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 1717 | isInline, isExplicit, isConstexpr, |
| 1718 | EndLocation); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 1721 | void LinkageSpecDecl::anchor() { } |
| 1722 | |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 1723 | LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1724 | DeclContext *DC, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1725 | SourceLocation ExternLoc, |
| 1726 | SourceLocation LangLoc, |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 1727 | LanguageIDs Lang, |
Abramo Bagnara | 5f6bcbe | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 1728 | SourceLocation RBraceLoc) { |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1729 | return new (C) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, RBraceLoc); |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 1730 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1731 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1732 | LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 1733 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LinkageSpecDecl)); |
| 1734 | return new (Mem) LinkageSpecDecl(0, SourceLocation(), SourceLocation(), |
| 1735 | lang_c, SourceLocation()); |
| 1736 | } |
| 1737 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 1738 | void UsingDirectiveDecl::anchor() { } |
| 1739 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1740 | UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC, |
| 1741 | SourceLocation L, |
| 1742 | SourceLocation NamespaceLoc, |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 1743 | NestedNameSpecifierLoc QualifierLoc, |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1744 | SourceLocation IdentLoc, |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 1745 | NamedDecl *Used, |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1746 | DeclContext *CommonAncestor) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 1747 | if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used)) |
| 1748 | Used = NS->getOriginalNamespace(); |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 1749 | return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc, |
| 1750 | IdentLoc, Used, CommonAncestor); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1753 | UsingDirectiveDecl * |
| 1754 | UsingDirectiveDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 1755 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDirectiveDecl)); |
| 1756 | return new (Mem) UsingDirectiveDecl(0, SourceLocation(), SourceLocation(), |
| 1757 | NestedNameSpecifierLoc(), |
| 1758 | SourceLocation(), 0, 0); |
| 1759 | } |
| 1760 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 1761 | NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() { |
| 1762 | if (NamespaceAliasDecl *NA = |
| 1763 | dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace)) |
| 1764 | return NA->getNamespace(); |
| 1765 | return cast_or_null<NamespaceDecl>(NominatedNamespace); |
| 1766 | } |
| 1767 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1768 | void NamespaceDecl::anchor() { } |
| 1769 | |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame^] | 1770 | NamespaceDecl::NamespaceDecl(DeclContext *DC, bool Inline, |
| 1771 | SourceLocation StartLoc, |
| 1772 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 1773 | NamespaceDecl *PrevDecl) |
| 1774 | : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace), |
| 1775 | LocStart(StartLoc), RBraceLoc(), AnonOrFirstNamespaceAndInline(0, Inline) |
| 1776 | { |
| 1777 | setPreviousDeclaration(PrevDecl); |
| 1778 | |
| 1779 | if (PrevDecl) |
| 1780 | AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace()); |
| 1781 | } |
| 1782 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1783 | NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame^] | 1784 | bool Inline, SourceLocation StartLoc, |
| 1785 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 1786 | NamespaceDecl *PrevDecl) { |
| 1787 | return new (C) NamespaceDecl(DC, Inline, StartLoc, IdLoc, Id, PrevDecl); |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 1791 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceDecl)); |
Douglas Gregor | f5c9f9f | 2012-01-07 09:11:48 +0000 | [diff] [blame^] | 1792 | return new (Mem) NamespaceDecl(0, false, SourceLocation(), SourceLocation(), |
| 1793 | 0, 0); |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 1796 | void NamespaceAliasDecl::anchor() { } |
| 1797 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1798 | NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 1799 | SourceLocation UsingLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1800 | SourceLocation AliasLoc, |
| 1801 | IdentifierInfo *Alias, |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 1802 | NestedNameSpecifierLoc QualifierLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1803 | SourceLocation IdentLoc, |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 1804 | NamedDecl *Namespace) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 1805 | if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace)) |
| 1806 | Namespace = NS->getOriginalNamespace(); |
Douglas Gregor | 0cfaf6a | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 1807 | return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias, |
| 1808 | QualifierLoc, IdentLoc, Namespace); |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1811 | NamespaceAliasDecl * |
| 1812 | NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 1813 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(NamespaceAliasDecl)); |
| 1814 | return new (Mem) NamespaceAliasDecl(0, SourceLocation(), SourceLocation(), 0, |
| 1815 | NestedNameSpecifierLoc(), |
| 1816 | SourceLocation(), 0); |
| 1817 | } |
| 1818 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 1819 | void UsingShadowDecl::anchor() { } |
| 1820 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1821 | UsingShadowDecl * |
| 1822 | UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 1823 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingShadowDecl)); |
| 1824 | return new (Mem) UsingShadowDecl(0, SourceLocation(), 0, 0); |
| 1825 | } |
| 1826 | |
Argyrios Kyrtzidis | 826faa2 | 2010-11-10 05:40:41 +0000 | [diff] [blame] | 1827 | UsingDecl *UsingShadowDecl::getUsingDecl() const { |
| 1828 | const UsingShadowDecl *Shadow = this; |
| 1829 | while (const UsingShadowDecl *NextShadow = |
| 1830 | dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow)) |
| 1831 | Shadow = NextShadow; |
| 1832 | return cast<UsingDecl>(Shadow->UsingOrNextShadow); |
| 1833 | } |
| 1834 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 1835 | void UsingDecl::anchor() { } |
| 1836 | |
Argyrios Kyrtzidis | 826faa2 | 2010-11-10 05:40:41 +0000 | [diff] [blame] | 1837 | void UsingDecl::addShadowDecl(UsingShadowDecl *S) { |
| 1838 | assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() && |
| 1839 | "declaration already in set"); |
| 1840 | assert(S->getUsingDecl() == this); |
| 1841 | |
| 1842 | if (FirstUsingShadow) |
| 1843 | S->UsingOrNextShadow = FirstUsingShadow; |
| 1844 | FirstUsingShadow = S; |
| 1845 | } |
| 1846 | |
| 1847 | void UsingDecl::removeShadowDecl(UsingShadowDecl *S) { |
| 1848 | assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() && |
| 1849 | "declaration not in set"); |
| 1850 | assert(S->getUsingDecl() == this); |
| 1851 | |
| 1852 | // Remove S from the shadow decl chain. This is O(n) but hopefully rare. |
| 1853 | |
| 1854 | if (FirstUsingShadow == S) { |
| 1855 | FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow); |
| 1856 | S->UsingOrNextShadow = this; |
| 1857 | return; |
| 1858 | } |
| 1859 | |
| 1860 | UsingShadowDecl *Prev = FirstUsingShadow; |
| 1861 | while (Prev->UsingOrNextShadow != S) |
| 1862 | Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow); |
| 1863 | Prev->UsingOrNextShadow = S->UsingOrNextShadow; |
| 1864 | S->UsingOrNextShadow = this; |
| 1865 | } |
| 1866 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1867 | UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL, |
| 1868 | NestedNameSpecifierLoc QualifierLoc, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1869 | const DeclarationNameInfo &NameInfo, |
| 1870 | bool IsTypeNameArg) { |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1871 | return new (C) UsingDecl(DC, UL, QualifierLoc, NameInfo, IsTypeNameArg); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1874 | UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 1875 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UsingDecl)); |
| 1876 | return new (Mem) UsingDecl(0, SourceLocation(), NestedNameSpecifierLoc(), |
| 1877 | DeclarationNameInfo(), false); |
| 1878 | } |
| 1879 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 1880 | void UnresolvedUsingValueDecl::anchor() { } |
| 1881 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1882 | UnresolvedUsingValueDecl * |
| 1883 | UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC, |
| 1884 | SourceLocation UsingLoc, |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1885 | NestedNameSpecifierLoc QualifierLoc, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1886 | const DeclarationNameInfo &NameInfo) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1887 | return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc, |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1888 | QualifierLoc, NameInfo); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1891 | UnresolvedUsingValueDecl * |
| 1892 | UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 1893 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(UnresolvedUsingValueDecl)); |
| 1894 | return new (Mem) UnresolvedUsingValueDecl(0, QualType(), SourceLocation(), |
| 1895 | NestedNameSpecifierLoc(), |
| 1896 | DeclarationNameInfo()); |
| 1897 | } |
| 1898 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 1899 | void UnresolvedUsingTypenameDecl::anchor() { } |
| 1900 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1901 | UnresolvedUsingTypenameDecl * |
| 1902 | UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC, |
| 1903 | SourceLocation UsingLoc, |
| 1904 | SourceLocation TypenameLoc, |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1905 | NestedNameSpecifierLoc QualifierLoc, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1906 | SourceLocation TargetNameLoc, |
| 1907 | DeclarationName TargetName) { |
| 1908 | return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc, |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1909 | QualifierLoc, TargetNameLoc, |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1910 | TargetName.getAsIdentifierInfo()); |
Anders Carlsson | 665b49c | 2009-08-28 05:30:28 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1913 | UnresolvedUsingTypenameDecl * |
| 1914 | UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 1915 | void *Mem = AllocateDeserializedDecl(C, ID, |
| 1916 | sizeof(UnresolvedUsingTypenameDecl)); |
| 1917 | return new (Mem) UnresolvedUsingTypenameDecl(0, SourceLocation(), |
| 1918 | SourceLocation(), |
| 1919 | NestedNameSpecifierLoc(), |
| 1920 | SourceLocation(), |
| 1921 | 0); |
| 1922 | } |
| 1923 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 1924 | void StaticAssertDecl::anchor() { } |
| 1925 | |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 1926 | StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1927 | SourceLocation StaticAssertLoc, |
| 1928 | Expr *AssertExpr, |
| 1929 | StringLiteral *Message, |
| 1930 | SourceLocation RParenLoc) { |
| 1931 | return new (C) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message, |
| 1932 | RParenLoc); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1935 | StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C, |
| 1936 | unsigned ID) { |
| 1937 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(StaticAssertDecl)); |
| 1938 | return new (Mem) StaticAssertDecl(0, SourceLocation(), 0, 0,SourceLocation()); |
| 1939 | } |
| 1940 | |
Anders Carlsson | 05bf2c7 | 2009-03-26 23:46:50 +0000 | [diff] [blame] | 1941 | static const char *getAccessName(AccessSpecifier AS) { |
| 1942 | switch (AS) { |
| 1943 | default: |
| 1944 | case AS_none: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1945 | llvm_unreachable("Invalid access specifier!"); |
Anders Carlsson | 05bf2c7 | 2009-03-26 23:46:50 +0000 | [diff] [blame] | 1946 | case AS_public: |
| 1947 | return "public"; |
| 1948 | case AS_private: |
| 1949 | return "private"; |
| 1950 | case AS_protected: |
| 1951 | return "protected"; |
| 1952 | } |
| 1953 | } |
| 1954 | |
| 1955 | const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, |
| 1956 | AccessSpecifier AS) { |
| 1957 | return DB << getAccessName(AS); |
| 1958 | } |