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