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