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