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