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 | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 20 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 7d7e672 | 2008-11-12 23:21:09 +0000 | [diff] [blame] | 21 | #include "clang/Basic/IdentifierTable.h" |
Douglas Gregor | fdfab6b | 2008-12-23 21:31:30 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/STLExtras.h" |
Fariborz Jahanian | faebcbb | 2009-09-12 19:52:10 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | // Decl Allocation/Deallocation Method Implementations |
| 28 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 29 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 30 | CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D) |
| 31 | : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false), |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 32 | UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false), |
Eli Friedman | 97c134e | 2009-08-15 22:23:00 +0000 | [diff] [blame] | 33 | Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false), |
| 34 | Abstract(false), HasTrivialConstructor(true), |
| 35 | HasTrivialCopyConstructor(true), HasTrivialCopyAssignment(true), |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 36 | HasTrivialDestructor(true), ComputedVisibleConversions(false), |
Douglas Gregor | 1827403 | 2010-07-03 00:47:00 +0000 | [diff] [blame] | 37 | DeclaredDefaultConstructor(false), DeclaredCopyConstructor(false), |
Douglas Gregor | a376d10 | 2010-07-02 21:50:04 +0000 | [diff] [blame] | 38 | DeclaredCopyAssignment(false), DeclaredDestructor(false), |
Anders Carlsson | cb88a1f | 2011-01-24 16:26:15 +0000 | [diff] [blame] | 39 | NumBases(0), NumVBases(0), Bases(), VBases(), |
| 40 | Definition(D), FirstFriend(0) { |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC, |
| 44 | SourceLocation L, IdentifierInfo *Id, |
| 45 | CXXRecordDecl *PrevDecl, |
| 46 | SourceLocation TKL) |
| 47 | : RecordDecl(K, TK, DC, L, Id, PrevDecl, TKL), |
| 48 | DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0), |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 49 | TemplateOrInstantiation() { } |
Douglas Gregor | 7d7e672 | 2008-11-12 23:21:09 +0000 | [diff] [blame] | 50 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 51 | CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK, |
| 52 | DeclContext *DC, SourceLocation L, |
| 53 | IdentifierInfo *Id, SourceLocation TKL, |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 54 | CXXRecordDecl* PrevDecl, |
| 55 | bool DelayTypeCreation) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id, |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 57 | PrevDecl, TKL); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 59 | // FIXME: DelayTypeCreation seems like such a hack |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 60 | if (!DelayTypeCreation) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | C.getTypeDeclType(R, PrevDecl); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 62 | return R; |
| 63 | } |
| 64 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 65 | CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, EmptyShell Empty) { |
Argyrios Kyrtzidis | b8b03e6 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 66 | return new (C) CXXRecordDecl(CXXRecord, TTK_Struct, 0, SourceLocation(), 0, 0, |
| 67 | SourceLocation()); |
| 68 | } |
| 69 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 70 | void |
Douglas Gregor | 2d5b703 | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 71 | CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases, |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 72 | unsigned NumBases) { |
Douglas Gregor | 2d5b703 | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 73 | ASTContext &C = getASTContext(); |
| 74 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | // C++ [dcl.init.aggr]p1: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 76 | // An aggregate is an array or a class (clause 9) with [...] |
| 77 | // no base classes [...]. |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 78 | data().Aggregate = false; |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 79 | |
Douglas Gregor | 7c789c1 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 80 | if (!data().Bases.isOffset() && data().NumBases > 0) |
| 81 | C.Deallocate(data().getBases()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 83 | // The set of seen virtual base types. |
Anders Carlsson | 1c36393 | 2010-03-29 19:49:09 +0000 | [diff] [blame] | 84 | llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes; |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 85 | |
| 86 | // The virtual bases of this class. |
| 87 | llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 89 | data().Bases = new(C) CXXBaseSpecifier [NumBases]; |
| 90 | data().NumBases = NumBases; |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 91 | for (unsigned i = 0; i < NumBases; ++i) { |
Douglas Gregor | 7c789c1 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 92 | data().getBases()[i] = *Bases[i]; |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 93 | // Keep track of inherited vbases for this base class. |
| 94 | const CXXBaseSpecifier *Base = Bases[i]; |
| 95 | QualType BaseType = Base->getType(); |
Douglas Gregor | 5fe8c04 | 2010-02-27 00:25:28 +0000 | [diff] [blame] | 96 | // Skip dependent types; we can't do any checking on them now. |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 97 | if (BaseType->isDependentType()) |
| 98 | continue; |
| 99 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 100 | = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 101 | |
Douglas Gregor | 9fe183a | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 102 | // C++ [dcl.init.aggr]p1: |
| 103 | // An aggregate is [...] a class with [...] no base classes [...]. |
| 104 | data().Aggregate = false; |
| 105 | |
| 106 | // C++ [class]p4: |
| 107 | // A POD-struct is an aggregate class... |
| 108 | data().PlainOldData = false; |
| 109 | |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 110 | // A class with a non-empty base class is not empty. |
| 111 | // FIXME: Standard ref? |
| 112 | if (!BaseClassDecl->isEmpty()) |
| 113 | data().Empty = false; |
| 114 | |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 115 | // C++ [class.virtual]p1: |
| 116 | // A class that declares or inherits a virtual function is called a |
| 117 | // polymorphic class. |
| 118 | if (BaseClassDecl->isPolymorphic()) |
| 119 | data().Polymorphic = true; |
| 120 | |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 121 | // Now go through all virtual bases of this base and add them. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 122 | for (CXXRecordDecl::base_class_iterator VBase = |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 123 | BaseClassDecl->vbases_begin(), |
| 124 | E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) { |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 125 | // Add this base if it's not already in the list. |
Anders Carlsson | 1c36393 | 2010-03-29 19:49:09 +0000 | [diff] [blame] | 126 | if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType()))) |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 127 | VBases.push_back(VBase); |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 128 | } |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 129 | |
| 130 | if (Base->isVirtual()) { |
| 131 | // Add this base if it's not already in the list. |
Anders Carlsson | 1c36393 | 2010-03-29 19:49:09 +0000 | [diff] [blame] | 132 | if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType))) |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 133 | VBases.push_back(Base); |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 134 | |
| 135 | // C++0x [meta.unary.prop] is_empty: |
| 136 | // T is a class type, but not a union type, with ... no virtual base |
| 137 | // classes |
| 138 | data().Empty = false; |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 139 | |
| 140 | // C++ [class.ctor]p5: |
| 141 | // A constructor is trivial if its class has no virtual base classes. |
| 142 | data().HasTrivialConstructor = false; |
| 143 | |
| 144 | // C++ [class.copy]p6: |
| 145 | // A copy constructor is trivial if its class has no virtual base |
| 146 | // classes. |
| 147 | data().HasTrivialCopyConstructor = false; |
| 148 | |
| 149 | // C++ [class.copy]p11: |
| 150 | // A copy assignment operator is trivial if its class has no virtual |
| 151 | // base classes. |
| 152 | data().HasTrivialCopyAssignment = false; |
| 153 | } else { |
| 154 | // C++ [class.ctor]p5: |
| 155 | // A constructor is trivial if all the direct base classes of its |
| 156 | // class have trivial constructors. |
| 157 | if (!BaseClassDecl->hasTrivialConstructor()) |
| 158 | data().HasTrivialConstructor = false; |
| 159 | |
| 160 | // C++ [class.copy]p6: |
| 161 | // A copy constructor is trivial if all the direct base classes of its |
| 162 | // class have trivial copy constructors. |
| 163 | if (!BaseClassDecl->hasTrivialCopyConstructor()) |
| 164 | data().HasTrivialCopyConstructor = false; |
| 165 | |
| 166 | // C++ [class.copy]p11: |
| 167 | // A copy assignment operator is trivial if all the direct base classes |
| 168 | // of its class have trivial copy assignment operators. |
| 169 | if (!BaseClassDecl->hasTrivialCopyAssignment()) |
| 170 | data().HasTrivialCopyAssignment = false; |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 171 | } |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 172 | |
| 173 | // C++ [class.ctor]p3: |
| 174 | // A destructor is trivial if all the direct base classes of its class |
| 175 | // have trivial destructors. |
| 176 | if (!BaseClassDecl->hasTrivialDestructor()) |
| 177 | data().HasTrivialDestructor = false; |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 178 | } |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 179 | |
| 180 | if (VBases.empty()) |
| 181 | return; |
| 182 | |
| 183 | // Create base specifier for any direct or indirect virtual bases. |
| 184 | data().VBases = new (C) CXXBaseSpecifier[VBases.size()]; |
| 185 | data().NumVBases = VBases.size(); |
| 186 | for (int I = 0, E = VBases.size(); I != E; ++I) { |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 187 | TypeSourceInfo *VBaseTypeInfo = VBases[I]->getTypeSourceInfo(); |
| 188 | |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 189 | // Skip dependent types; we can't do any checking on them now. |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 190 | if (VBaseTypeInfo->getType()->isDependentType()) |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 191 | continue; |
| 192 | |
Nick Lewycky | 5606220 | 2010-07-26 16:56:01 +0000 | [diff] [blame] | 193 | CXXRecordDecl *VBaseClassDecl = cast<CXXRecordDecl>( |
| 194 | VBaseTypeInfo->getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 195 | |
Douglas Gregor | 7c789c1 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 196 | data().getVBases()[I] = |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 197 | CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true, |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 198 | VBaseClassDecl->getTagKind() == TTK_Class, |
Douglas Gregor | f90b27a | 2011-01-03 22:36:02 +0000 | [diff] [blame] | 199 | VBases[I]->getAccessSpecifier(), VBaseTypeInfo, |
| 200 | SourceLocation()); |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 201 | } |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 204 | /// Callback function for CXXRecordDecl::forallBases that acknowledges |
| 205 | /// that it saw a base class. |
| 206 | static bool SawBase(const CXXRecordDecl *, void *) { |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | bool CXXRecordDecl::hasAnyDependentBases() const { |
| 211 | if (!isDependentContext()) |
| 212 | return false; |
| 213 | |
| 214 | return !forallBases(SawBase, 0); |
| 215 | } |
| 216 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 217 | bool CXXRecordDecl::hasConstCopyConstructor(const ASTContext &Context) const { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 218 | return getCopyConstructor(Context, Qualifiers::Const) != 0; |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 221 | /// \brief Perform a simplistic form of overload resolution that only considers |
| 222 | /// cv-qualifiers on a single parameter, and return the best overload candidate |
| 223 | /// (if there is one). |
| 224 | static CXXMethodDecl * |
| 225 | GetBestOverloadCandidateSimple( |
| 226 | const llvm::SmallVectorImpl<std::pair<CXXMethodDecl *, Qualifiers> > &Cands) { |
| 227 | if (Cands.empty()) |
| 228 | return 0; |
| 229 | if (Cands.size() == 1) |
| 230 | return Cands[0].first; |
| 231 | |
| 232 | unsigned Best = 0, N = Cands.size(); |
| 233 | for (unsigned I = 1; I != N; ++I) |
| 234 | if (Cands[Best].second.isSupersetOf(Cands[I].second)) |
| 235 | Best = I; |
| 236 | |
| 237 | for (unsigned I = 1; I != N; ++I) |
| 238 | if (Cands[Best].second.isSupersetOf(Cands[I].second)) |
| 239 | return 0; |
| 240 | |
| 241 | return Cands[Best].first; |
| 242 | } |
| 243 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 244 | CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(const ASTContext &Context, |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 245 | unsigned TypeQuals) const{ |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 246 | QualType ClassType |
| 247 | = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 248 | DeclarationName ConstructorName |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 249 | = Context.DeclarationNames.getCXXConstructorName( |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 250 | Context.getCanonicalType(ClassType)); |
| 251 | unsigned FoundTQs; |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 252 | llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found; |
Douglas Gregor | fdfab6b | 2008-12-23 21:31:30 +0000 | [diff] [blame] | 253 | DeclContext::lookup_const_iterator Con, ConEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 254 | for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName); |
Douglas Gregor | fdfab6b | 2008-12-23 21:31:30 +0000 | [diff] [blame] | 255 | Con != ConEnd; ++Con) { |
Douglas Gregor | d93bacf | 2009-09-04 14:46:39 +0000 | [diff] [blame] | 256 | // C++ [class.copy]p2: |
| 257 | // A non-template constructor for class X is a copy constructor if [...] |
| 258 | if (isa<FunctionTemplateDecl>(*Con)) |
| 259 | continue; |
| 260 | |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 261 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 262 | if (Constructor->isCopyConstructor(FoundTQs)) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 263 | if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) || |
| 264 | (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const))) |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 265 | Found.push_back(std::make_pair( |
| 266 | const_cast<CXXConstructorDecl *>(Constructor), |
| 267 | Qualifiers::fromCVRMask(FoundTQs))); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 268 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 269 | } |
Douglas Gregor | 0d405db | 2010-07-01 20:59:04 +0000 | [diff] [blame] | 270 | |
| 271 | return cast_or_null<CXXConstructorDecl>( |
| 272 | GetBestOverloadCandidateSimple(Found)); |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Douglas Gregor | b87786f | 2010-07-01 17:48:08 +0000 | [diff] [blame] | 275 | CXXMethodDecl *CXXRecordDecl::getCopyAssignmentOperator(bool ArgIsConst) const { |
| 276 | ASTContext &Context = getASTContext(); |
| 277 | QualType Class = Context.getTypeDeclType(const_cast<CXXRecordDecl *>(this)); |
| 278 | DeclarationName Name = Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 279 | |
| 280 | llvm::SmallVector<std::pair<CXXMethodDecl *, Qualifiers>, 4> Found; |
| 281 | DeclContext::lookup_const_iterator Op, OpEnd; |
| 282 | for (llvm::tie(Op, OpEnd) = this->lookup(Name); Op != OpEnd; ++Op) { |
| 283 | // C++ [class.copy]p9: |
| 284 | // A user-declared copy assignment operator is a non-static non-template |
| 285 | // member function of class X with exactly one parameter of type X, X&, |
| 286 | // const X&, volatile X& or const volatile X&. |
| 287 | const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op); |
| 288 | if (!Method || Method->isStatic() || Method->getPrimaryTemplate()) |
| 289 | continue; |
| 290 | |
| 291 | const FunctionProtoType *FnType |
| 292 | = Method->getType()->getAs<FunctionProtoType>(); |
| 293 | assert(FnType && "Overloaded operator has no prototype."); |
| 294 | // Don't assert on this; an invalid decl might have been left in the AST. |
| 295 | if (FnType->getNumArgs() != 1 || FnType->isVariadic()) |
| 296 | continue; |
| 297 | |
| 298 | QualType ArgType = FnType->getArgType(0); |
| 299 | Qualifiers Quals; |
| 300 | if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) { |
| 301 | ArgType = Ref->getPointeeType(); |
| 302 | // If we have a const argument and we have a reference to a non-const, |
| 303 | // this function does not match. |
| 304 | if (ArgIsConst && !ArgType.isConstQualified()) |
| 305 | continue; |
| 306 | |
| 307 | Quals = ArgType.getQualifiers(); |
| 308 | } else { |
| 309 | // By-value copy-assignment operators are treated like const X& |
| 310 | // copy-assignment operators. |
| 311 | Quals = Qualifiers::fromCVRMask(Qualifiers::Const); |
| 312 | } |
| 313 | |
| 314 | if (!Context.hasSameUnqualifiedType(ArgType, Class)) |
| 315 | continue; |
| 316 | |
| 317 | // Save this copy-assignment operator. It might be "the one". |
| 318 | Found.push_back(std::make_pair(const_cast<CXXMethodDecl *>(Method), Quals)); |
| 319 | } |
| 320 | |
| 321 | // Use a simplistic form of overload resolution to find the candidate. |
| 322 | return GetBestOverloadCandidateSimple(Found); |
| 323 | } |
| 324 | |
Douglas Gregor | 2138664 | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 325 | void CXXRecordDecl::markedVirtualFunctionPure() { |
| 326 | // C++ [class.abstract]p2: |
| 327 | // A class is abstract if it has at least one pure virtual function. |
| 328 | data().Abstract = true; |
| 329 | } |
| 330 | |
| 331 | void CXXRecordDecl::addedMember(Decl *D) { |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 332 | // Ignore friends and invalid declarations. |
| 333 | if (D->getFriendObjectKind() || D->isInvalidDecl()) |
Douglas Gregor | 5c0646b | 2010-09-27 21:17:54 +0000 | [diff] [blame] | 334 | return; |
| 335 | |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 336 | FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D); |
| 337 | if (FunTmpl) |
| 338 | D = FunTmpl->getTemplatedDecl(); |
| 339 | |
Douglas Gregor | 9fe183a | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 340 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 341 | if (Method->isVirtual()) { |
| 342 | // C++ [dcl.init.aggr]p1: |
| 343 | // An aggregate is an array or a class with [...] no virtual functions. |
| 344 | data().Aggregate = false; |
| 345 | |
| 346 | // C++ [class]p4: |
| 347 | // A POD-struct is an aggregate class... |
| 348 | data().PlainOldData = false; |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 349 | |
| 350 | // Virtual functions make the class non-empty. |
| 351 | // FIXME: Standard ref? |
| 352 | data().Empty = false; |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 353 | |
| 354 | // C++ [class.virtual]p1: |
| 355 | // A class that declares or inherits a virtual function is called a |
| 356 | // polymorphic class. |
| 357 | data().Polymorphic = true; |
| 358 | |
| 359 | // None of the special member functions are trivial. |
| 360 | data().HasTrivialConstructor = false; |
| 361 | data().HasTrivialCopyConstructor = false; |
| 362 | data().HasTrivialCopyAssignment = false; |
| 363 | // FIXME: Destructor? |
Douglas Gregor | 9fe183a | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 367 | if (D->isImplicit()) { |
Argyrios Kyrtzidis | b6cc0e1 | 2010-10-24 17:26:54 +0000 | [diff] [blame] | 368 | // Notify that an implicit member was added after the definition |
| 369 | // was completed. |
| 370 | if (!isBeingDefined()) |
| 371 | if (ASTMutationListener *L = getASTMutationListener()) |
| 372 | L->AddedCXXImplicitMember(data().Definition, D); |
Argyrios Kyrtzidis | 046c03b | 2010-10-20 23:48:42 +0000 | [diff] [blame] | 373 | |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 374 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
| 375 | // If this is the implicit default constructor, note that we have now |
| 376 | // declared it. |
| 377 | if (Constructor->isDefaultConstructor()) |
| 378 | data().DeclaredDefaultConstructor = true; |
| 379 | // If this is the implicit copy constructor, note that we have now |
| 380 | // declared it. |
| 381 | else if (Constructor->isCopyConstructor()) |
| 382 | data().DeclaredCopyConstructor = true; |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 383 | return; |
| 384 | } |
| 385 | |
| 386 | if (isa<CXXDestructorDecl>(D)) { |
Douglas Gregor | 0ed2e08 | 2010-09-27 22:48:58 +0000 | [diff] [blame] | 387 | data().DeclaredDestructor = true; |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 388 | return; |
| 389 | } |
| 390 | |
| 391 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 392 | // If this is the implicit copy constructor, note that we have now |
| 393 | // declared it. |
| 394 | // FIXME: Move constructors |
Douglas Gregor | 3e9438b | 2010-09-27 22:37:28 +0000 | [diff] [blame] | 395 | if (Method->getOverloadedOperator() == OO_Equal) |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 396 | data().DeclaredCopyAssignment = true; |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 397 | return; |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 398 | } |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 399 | |
| 400 | // Any other implicit declarations are handled like normal declarations. |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | // Handle (user-declared) constructors. |
| 404 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
| 405 | // Note that we have a user-declared constructor. |
| 406 | data().UserDeclaredConstructor = true; |
| 407 | |
| 408 | // Note that we have no need of an implicitly-declared default constructor. |
| 409 | data().DeclaredDefaultConstructor = true; |
| 410 | |
| 411 | // C++ [dcl.init.aggr]p1: |
| 412 | // An aggregate is an array or a class (clause 9) with no |
| 413 | // user-declared constructors (12.1) [...]. |
| 414 | data().Aggregate = false; |
| 415 | |
| 416 | // C++ [class]p4: |
| 417 | // A POD-struct is an aggregate class [...] |
| 418 | data().PlainOldData = false; |
| 419 | |
| 420 | // C++ [class.ctor]p5: |
| 421 | // A constructor is trivial if it is an implicitly-declared default |
| 422 | // constructor. |
| 423 | // FIXME: C++0x: don't do this for "= default" default constructors. |
| 424 | data().HasTrivialConstructor = false; |
| 425 | |
| 426 | // Note when we have a user-declared copy constructor, which will |
| 427 | // suppress the implicit declaration of a copy constructor. |
| 428 | if (!FunTmpl && Constructor->isCopyConstructor()) { |
| 429 | data().UserDeclaredCopyConstructor = true; |
Douglas Gregor | 5c0646b | 2010-09-27 21:17:54 +0000 | [diff] [blame] | 430 | data().DeclaredCopyConstructor = true; |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 431 | |
| 432 | // C++ [class.copy]p6: |
| 433 | // A copy constructor is trivial if it is implicitly declared. |
| 434 | // FIXME: C++0x: don't do this for "= default" copy constructors. |
| 435 | data().HasTrivialCopyConstructor = false; |
| 436 | } |
Douglas Gregor | 5c0646b | 2010-09-27 21:17:54 +0000 | [diff] [blame] | 437 | |
Douglas Gregor | 5c0646b | 2010-09-27 21:17:54 +0000 | [diff] [blame] | 438 | return; |
| 439 | } |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 440 | |
Douglas Gregor | 0ed2e08 | 2010-09-27 22:48:58 +0000 | [diff] [blame] | 441 | // Handle (user-declared) destructors. |
| 442 | if (isa<CXXDestructorDecl>(D)) { |
| 443 | data().DeclaredDestructor = true; |
| 444 | data().UserDeclaredDestructor = true; |
Douglas Gregor | 9fe183a | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 445 | |
| 446 | // C++ [class]p4: |
| 447 | // A POD-struct is an aggregate class that has [...] no user-defined |
| 448 | // destructor. |
| 449 | data().PlainOldData = false; |
| 450 | |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 451 | // C++ [class.dtor]p3: |
| 452 | // A destructor is trivial if it is an implicitly-declared destructor and |
| 453 | // [...]. |
| 454 | // |
| 455 | // FIXME: C++0x: don't do this for "= default" destructors |
| 456 | data().HasTrivialDestructor = false; |
| 457 | |
Douglas Gregor | 0ed2e08 | 2010-09-27 22:48:58 +0000 | [diff] [blame] | 458 | return; |
| 459 | } |
Douglas Gregor | 5c0646b | 2010-09-27 21:17:54 +0000 | [diff] [blame] | 460 | |
Douglas Gregor | 0ed2e08 | 2010-09-27 22:48:58 +0000 | [diff] [blame] | 461 | // Handle (user-declared) member functions. |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 462 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 463 | if (Method->getOverloadedOperator() == OO_Equal) { |
| 464 | // We're interested specifically in copy assignment operators. |
| 465 | const FunctionProtoType *FnType |
| 466 | = Method->getType()->getAs<FunctionProtoType>(); |
| 467 | assert(FnType && "Overloaded operator has no proto function type."); |
| 468 | assert(FnType->getNumArgs() == 1 && !FnType->isVariadic()); |
| 469 | |
| 470 | // Copy assignment operators must be non-templates. |
| 471 | if (Method->getPrimaryTemplate() || FunTmpl) |
| 472 | return; |
| 473 | |
| 474 | ASTContext &Context = getASTContext(); |
| 475 | QualType ArgType = FnType->getArgType(0); |
| 476 | if (const LValueReferenceType *Ref =ArgType->getAs<LValueReferenceType>()) |
| 477 | ArgType = Ref->getPointeeType(); |
| 478 | |
| 479 | ArgType = ArgType.getUnqualifiedType(); |
| 480 | QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType( |
| 481 | const_cast<CXXRecordDecl*>(this))); |
| 482 | |
| 483 | if (!Context.hasSameUnqualifiedType(ClassType, ArgType)) |
| 484 | return; |
| 485 | |
| 486 | // This is a copy assignment operator. |
Douglas Gregor | 3e9438b | 2010-09-27 22:37:28 +0000 | [diff] [blame] | 487 | // FIXME: Move assignment operators. |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 488 | |
| 489 | // Suppress the implicit declaration of a copy constructor. |
| 490 | data().UserDeclaredCopyAssignment = true; |
| 491 | data().DeclaredCopyAssignment = true; |
| 492 | |
| 493 | // C++ [class.copy]p11: |
| 494 | // A copy assignment operator is trivial if it is implicitly declared. |
| 495 | // FIXME: C++0x: don't do this for "= default" copy operators. |
| 496 | data().HasTrivialCopyAssignment = false; |
| 497 | |
| 498 | // C++ [class]p4: |
| 499 | // A POD-struct is an aggregate class that [...] has no user-defined copy |
| 500 | // assignment operator [...]. |
| 501 | data().PlainOldData = false; |
| 502 | } |
Douglas Gregor | 2258431 | 2010-07-02 23:41:54 +0000 | [diff] [blame] | 503 | |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 504 | // Keep the list of conversion functions up-to-date. |
| 505 | if (CXXConversionDecl *Conversion = dyn_cast<CXXConversionDecl>(D)) { |
| 506 | // We don't record specializations. |
| 507 | if (Conversion->getPrimaryTemplate()) |
| 508 | return; |
| 509 | |
| 510 | // FIXME: We intentionally don't use the decl's access here because it |
| 511 | // hasn't been set yet. That's really just a misdesign in Sema. |
| 512 | |
| 513 | if (FunTmpl) { |
| 514 | if (FunTmpl->getPreviousDeclaration()) |
| 515 | data().Conversions.replace(FunTmpl->getPreviousDeclaration(), |
| 516 | FunTmpl); |
| 517 | else |
| 518 | data().Conversions.addDecl(FunTmpl); |
| 519 | } else { |
| 520 | if (Conversion->getPreviousDeclaration()) |
| 521 | data().Conversions.replace(Conversion->getPreviousDeclaration(), |
| 522 | Conversion); |
| 523 | else |
| 524 | data().Conversions.addDecl(Conversion); |
| 525 | } |
| 526 | } |
| 527 | |
Douglas Gregor | 27c08ab | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 528 | return; |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 529 | } |
Douglas Gregor | 9fe183a | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 530 | |
| 531 | // Handle non-static data members. |
| 532 | if (FieldDecl *Field = dyn_cast<FieldDecl>(D)) { |
| 533 | // C++ [dcl.init.aggr]p1: |
| 534 | // An aggregate is an array or a class (clause 9) with [...] no |
| 535 | // private or protected non-static data members (clause 11). |
| 536 | // |
| 537 | // A POD must be an aggregate. |
| 538 | if (D->getAccess() == AS_private || D->getAccess() == AS_protected) { |
| 539 | data().Aggregate = false; |
| 540 | data().PlainOldData = false; |
| 541 | } |
| 542 | |
| 543 | // C++ [class]p9: |
| 544 | // A POD struct is a class that is both a trivial class and a |
| 545 | // standard-layout class, and has no non-static data members of type |
| 546 | // non-POD struct, non-POD union (or array of such types). |
| 547 | ASTContext &Context = getASTContext(); |
| 548 | QualType T = Context.getBaseElementType(Field->getType()); |
| 549 | if (!T->isPODType()) |
| 550 | data().PlainOldData = false; |
Douglas Gregor | 85606eb | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 551 | if (T->isReferenceType()) |
| 552 | data().HasTrivialConstructor = false; |
| 553 | |
| 554 | if (const RecordType *RecordTy = T->getAs<RecordType>()) { |
| 555 | CXXRecordDecl* FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| 556 | if (FieldRec->getDefinition()) { |
| 557 | if (!FieldRec->hasTrivialConstructor()) |
| 558 | data().HasTrivialConstructor = false; |
| 559 | if (!FieldRec->hasTrivialCopyConstructor()) |
| 560 | data().HasTrivialCopyConstructor = false; |
| 561 | if (!FieldRec->hasTrivialCopyAssignment()) |
| 562 | data().HasTrivialCopyAssignment = false; |
| 563 | if (!FieldRec->hasTrivialDestructor()) |
| 564 | data().HasTrivialDestructor = false; |
| 565 | } |
| 566 | } |
Douglas Gregor | 2cf9d65 | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 567 | |
| 568 | // If this is not a zero-length bit-field, then the class is not empty. |
| 569 | if (data().Empty) { |
| 570 | if (!Field->getBitWidth()) |
| 571 | data().Empty = false; |
| 572 | else if (!Field->getBitWidth()->isTypeDependent() && |
| 573 | !Field->getBitWidth()->isValueDependent()) { |
| 574 | llvm::APSInt Bits; |
| 575 | if (Field->getBitWidth()->isIntegerConstantExpr(Bits, Context)) |
| 576 | if (!!Bits) |
| 577 | data().Empty = false; |
| 578 | } |
| 579 | } |
Douglas Gregor | 9fe183a | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 580 | } |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 581 | |
| 582 | // Handle using declarations of conversion functions. |
| 583 | if (UsingShadowDecl *Shadow = dyn_cast<UsingShadowDecl>(D)) |
| 584 | if (Shadow->getDeclName().getNameKind() |
| 585 | == DeclarationName::CXXConversionFunctionName) |
| 586 | data().Conversions.addDecl(Shadow, Shadow->getAccess()); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 587 | } |
| 588 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 589 | static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) { |
| 590 | QualType T; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 591 | if (isa<UsingShadowDecl>(Conv)) |
| 592 | Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl(); |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 593 | if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv)) |
| 594 | T = ConvTemp->getTemplatedDecl()->getResultType(); |
| 595 | else |
| 596 | T = cast<CXXConversionDecl>(Conv)->getConversionType(); |
| 597 | return Context.getCanonicalType(T); |
Fariborz Jahanian | 0351a1e | 2009-10-07 20:43:36 +0000 | [diff] [blame] | 598 | } |
| 599 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 600 | /// Collect the visible conversions of a base class. |
| 601 | /// |
| 602 | /// \param Base a base class of the class we're considering |
| 603 | /// \param InVirtual whether this base class is a virtual base (or a base |
| 604 | /// of a virtual base) |
| 605 | /// \param Access the access along the inheritance path to this base |
| 606 | /// \param ParentHiddenTypes the conversions provided by the inheritors |
| 607 | /// of this base |
| 608 | /// \param Output the set to which to add conversions from non-virtual bases |
| 609 | /// \param VOutput the set to which to add conversions from virtual bases |
| 610 | /// \param HiddenVBaseCs the set of conversions which were hidden in a |
| 611 | /// virtual base along some inheritance path |
| 612 | static void CollectVisibleConversions(ASTContext &Context, |
| 613 | CXXRecordDecl *Record, |
| 614 | bool InVirtual, |
| 615 | AccessSpecifier Access, |
| 616 | const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes, |
| 617 | UnresolvedSetImpl &Output, |
| 618 | UnresolvedSetImpl &VOutput, |
| 619 | llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) { |
| 620 | // The set of types which have conversions in this class or its |
| 621 | // subclasses. As an optimization, we don't copy the derived set |
| 622 | // unless it might change. |
| 623 | const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes; |
| 624 | llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer; |
| 625 | |
| 626 | // Collect the direct conversions and figure out which conversions |
| 627 | // will be hidden in the subclasses. |
| 628 | UnresolvedSetImpl &Cs = *Record->getConversionFunctions(); |
| 629 | if (!Cs.empty()) { |
| 630 | HiddenTypesBuffer = ParentHiddenTypes; |
| 631 | HiddenTypes = &HiddenTypesBuffer; |
| 632 | |
| 633 | for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) { |
| 634 | bool Hidden = |
| 635 | !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl())); |
| 636 | |
| 637 | // If this conversion is hidden and we're in a virtual base, |
| 638 | // remember that it's hidden along some inheritance path. |
| 639 | if (Hidden && InVirtual) |
| 640 | HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())); |
| 641 | |
| 642 | // If this conversion isn't hidden, add it to the appropriate output. |
| 643 | else if (!Hidden) { |
| 644 | AccessSpecifier IAccess |
| 645 | = CXXRecordDecl::MergeAccess(Access, I.getAccess()); |
| 646 | |
| 647 | if (InVirtual) |
| 648 | VOutput.addDecl(I.getDecl(), IAccess); |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 649 | else |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 650 | Output.addDecl(I.getDecl(), IAccess); |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | } |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 654 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 655 | // Collect information recursively from any base classes. |
| 656 | for (CXXRecordDecl::base_class_iterator |
| 657 | I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) { |
| 658 | const RecordType *RT = I->getType()->getAs<RecordType>(); |
| 659 | if (!RT) continue; |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 660 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 661 | AccessSpecifier BaseAccess |
| 662 | = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier()); |
| 663 | bool BaseInVirtual = InVirtual || I->isVirtual(); |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 664 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 665 | CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl()); |
| 666 | CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess, |
| 667 | *HiddenTypes, Output, VOutput, HiddenVBaseCs); |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 668 | } |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 669 | } |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 670 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 671 | /// Collect the visible conversions of a class. |
| 672 | /// |
| 673 | /// This would be extremely straightforward if it weren't for virtual |
| 674 | /// bases. It might be worth special-casing that, really. |
| 675 | static void CollectVisibleConversions(ASTContext &Context, |
| 676 | CXXRecordDecl *Record, |
| 677 | UnresolvedSetImpl &Output) { |
| 678 | // The collection of all conversions in virtual bases that we've |
| 679 | // found. These will be added to the output as long as they don't |
| 680 | // appear in the hidden-conversions set. |
| 681 | UnresolvedSet<8> VBaseCs; |
| 682 | |
| 683 | // The set of conversions in virtual bases that we've determined to |
| 684 | // be hidden. |
| 685 | llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs; |
| 686 | |
| 687 | // The set of types hidden by classes derived from this one. |
| 688 | llvm::SmallPtrSet<CanQualType, 8> HiddenTypes; |
| 689 | |
| 690 | // Go ahead and collect the direct conversions and add them to the |
| 691 | // hidden-types set. |
| 692 | UnresolvedSetImpl &Cs = *Record->getConversionFunctions(); |
| 693 | Output.append(Cs.begin(), Cs.end()); |
| 694 | for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) |
| 695 | HiddenTypes.insert(GetConversionType(Context, I.getDecl())); |
| 696 | |
| 697 | // Recursively collect conversions from base classes. |
| 698 | for (CXXRecordDecl::base_class_iterator |
| 699 | I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) { |
| 700 | const RecordType *RT = I->getType()->getAs<RecordType>(); |
| 701 | if (!RT) continue; |
| 702 | |
| 703 | CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()), |
| 704 | I->isVirtual(), I->getAccessSpecifier(), |
| 705 | HiddenTypes, Output, VBaseCs, HiddenVBaseCs); |
| 706 | } |
| 707 | |
| 708 | // Add any unhidden conversions provided by virtual bases. |
| 709 | for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end(); |
| 710 | I != E; ++I) { |
| 711 | if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()))) |
| 712 | Output.addDecl(I.getDecl(), I.getAccess()); |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 713 | } |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /// getVisibleConversionFunctions - get all conversion functions visible |
| 717 | /// in current class; including conversion function templates. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 718 | const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() { |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 719 | // If root class, all conversions are visible. |
| 720 | if (bases_begin() == bases_end()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 721 | return &data().Conversions; |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 722 | // If visible conversion list is already evaluated, return it. |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 723 | if (data().ComputedVisibleConversions) |
| 724 | return &data().VisibleConversions; |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 725 | CollectVisibleConversions(getASTContext(), this, data().VisibleConversions); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 726 | data().ComputedVisibleConversions = true; |
| 727 | return &data().VisibleConversions; |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 728 | } |
| 729 | |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 730 | void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) { |
| 731 | // This operation is O(N) but extremely rare. Sema only uses it to |
| 732 | // remove UsingShadowDecls in a class that were followed by a direct |
| 733 | // declaration, e.g.: |
| 734 | // class A : B { |
| 735 | // using B::operator int; |
| 736 | // operator int(); |
| 737 | // }; |
| 738 | // This is uncommon by itself and even more uncommon in conjunction |
| 739 | // with sufficiently large numbers of directly-declared conversions |
| 740 | // that asymptotic behavior matters. |
| 741 | |
| 742 | UnresolvedSetImpl &Convs = *getConversionFunctions(); |
| 743 | for (unsigned I = 0, E = Convs.size(); I != E; ++I) { |
| 744 | if (Convs[I].getDecl() == ConvDecl) { |
| 745 | Convs.erase(I); |
| 746 | assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end() |
| 747 | && "conversion was found multiple times in unresolved set"); |
| 748 | return; |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | llvm_unreachable("conversion not found in set!"); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 753 | } |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 754 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 755 | CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 756 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 757 | return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom()); |
| 758 | |
| 759 | return 0; |
| 760 | } |
| 761 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 762 | MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const { |
| 763 | return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>(); |
| 764 | } |
| 765 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 766 | void |
| 767 | CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD, |
| 768 | TemplateSpecializationKind TSK) { |
| 769 | assert(TemplateOrInstantiation.isNull() && |
| 770 | "Previous template or instantiation?"); |
| 771 | assert(!isa<ClassTemplateSpecializationDecl>(this)); |
| 772 | TemplateOrInstantiation |
| 773 | = new (getASTContext()) MemberSpecializationInfo(RD, TSK); |
| 774 | } |
| 775 | |
Anders Carlsson | b13e357 | 2009-12-07 06:33:48 +0000 | [diff] [blame] | 776 | TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{ |
| 777 | if (const ClassTemplateSpecializationDecl *Spec |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 778 | = dyn_cast<ClassTemplateSpecializationDecl>(this)) |
| 779 | return Spec->getSpecializationKind(); |
| 780 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 781 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 782 | return MSInfo->getTemplateSpecializationKind(); |
| 783 | |
| 784 | return TSK_Undeclared; |
| 785 | } |
| 786 | |
| 787 | void |
| 788 | CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) { |
| 789 | if (ClassTemplateSpecializationDecl *Spec |
| 790 | = dyn_cast<ClassTemplateSpecializationDecl>(this)) { |
| 791 | Spec->setSpecializationKind(TSK); |
| 792 | return; |
| 793 | } |
| 794 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 795 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 796 | MSInfo->setTemplateSpecializationKind(TSK); |
| 797 | return; |
| 798 | } |
| 799 | |
| 800 | assert(false && "Not a class template or member class specialization"); |
| 801 | } |
| 802 | |
Douglas Gregor | 1d110e0 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 803 | CXXDestructorDecl *CXXRecordDecl::getDestructor() const { |
| 804 | ASTContext &Context = getASTContext(); |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 805 | QualType ClassType = Context.getTypeDeclType(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 806 | |
| 807 | DeclarationName Name |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 808 | = Context.DeclarationNames.getCXXDestructorName( |
| 809 | Context.getCanonicalType(ClassType)); |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 810 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 811 | DeclContext::lookup_const_iterator I, E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 812 | llvm::tie(I, E) = lookup(Name); |
Sebastian Redl | d4b25cb | 2010-09-02 23:19:42 +0000 | [diff] [blame] | 813 | if (I == E) |
| 814 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 815 | |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 816 | CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I); |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 817 | assert(++I == E && "Found more than one destructor!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 818 | |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 819 | return Dtor; |
| 820 | } |
| 821 | |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 822 | void CXXRecordDecl::completeDefinition() { |
| 823 | completeDefinition(0); |
| 824 | } |
| 825 | |
| 826 | void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) { |
| 827 | RecordDecl::completeDefinition(); |
| 828 | |
| 829 | // If the class may be abstract (but hasn't been marked as such), check for |
| 830 | // any pure final overriders. |
| 831 | if (mayBeAbstract()) { |
| 832 | CXXFinalOverriderMap MyFinalOverriders; |
| 833 | if (!FinalOverriders) { |
| 834 | getFinalOverriders(MyFinalOverriders); |
| 835 | FinalOverriders = &MyFinalOverriders; |
| 836 | } |
| 837 | |
| 838 | bool Done = false; |
| 839 | for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(), |
| 840 | MEnd = FinalOverriders->end(); |
| 841 | M != MEnd && !Done; ++M) { |
| 842 | for (OverridingMethods::iterator SO = M->second.begin(), |
| 843 | SOEnd = M->second.end(); |
| 844 | SO != SOEnd && !Done; ++SO) { |
| 845 | assert(SO->second.size() > 0 && |
| 846 | "All virtual functions have overridding virtual functions"); |
| 847 | |
| 848 | // C++ [class.abstract]p4: |
| 849 | // A class is abstract if it contains or inherits at least one |
| 850 | // pure virtual function for which the final overrider is pure |
| 851 | // virtual. |
| 852 | if (SO->second.front().Method->isPure()) { |
| 853 | data().Abstract = true; |
| 854 | Done = true; |
| 855 | break; |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | } |
Douglas Gregor | e80622f | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 860 | |
| 861 | // Set access bits correctly on the directly-declared conversions. |
| 862 | for (UnresolvedSetIterator I = data().Conversions.begin(), |
| 863 | E = data().Conversions.end(); |
| 864 | I != E; ++I) |
| 865 | data().Conversions.setAccess(I, (*I)->getAccess()); |
Douglas Gregor | 7a39dd0 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | bool CXXRecordDecl::mayBeAbstract() const { |
| 869 | if (data().Abstract || isInvalidDecl() || !data().Polymorphic || |
| 870 | isDependentContext()) |
| 871 | return false; |
| 872 | |
| 873 | for (CXXRecordDecl::base_class_const_iterator B = bases_begin(), |
| 874 | BEnd = bases_end(); |
| 875 | B != BEnd; ++B) { |
| 876 | CXXRecordDecl *BaseDecl |
| 877 | = cast<CXXRecordDecl>(B->getType()->getAs<RecordType>()->getDecl()); |
| 878 | if (BaseDecl->isAbstract()) |
| 879 | return true; |
| 880 | } |
| 881 | |
| 882 | return false; |
| 883 | } |
| 884 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 885 | CXXMethodDecl * |
| 886 | CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 887 | const DeclarationNameInfo &NameInfo, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 888 | QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 889 | bool isStatic, StorageClass SCAsWritten, bool isInline) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 890 | return new (C) CXXMethodDecl(CXXMethod, RD, NameInfo, T, TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 891 | isStatic, SCAsWritten, isInline); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 894 | bool CXXMethodDecl::isUsualDeallocationFunction() const { |
| 895 | if (getOverloadedOperator() != OO_Delete && |
| 896 | getOverloadedOperator() != OO_Array_Delete) |
| 897 | return false; |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 898 | |
| 899 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 900 | // A template instance is never a usual deallocation function, |
| 901 | // regardless of its signature. |
| 902 | if (getPrimaryTemplate()) |
| 903 | return false; |
| 904 | |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 905 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 906 | // If a class T has a member deallocation function named operator delete |
| 907 | // with exactly one parameter, then that function is a usual (non-placement) |
| 908 | // deallocation function. [...] |
| 909 | if (getNumParams() == 1) |
| 910 | return true; |
| 911 | |
| 912 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 913 | // [...] If class T does not declare such an operator delete but does |
| 914 | // declare a member deallocation function named operator delete with |
| 915 | // exactly two parameters, the second of which has type std::size_t (18.1), |
| 916 | // then this function is a usual deallocation function. |
| 917 | ASTContext &Context = getASTContext(); |
| 918 | if (getNumParams() != 2 || |
Chandler Carruth | e228ba9 | 2010-02-08 18:54:05 +0000 | [diff] [blame] | 919 | !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(), |
| 920 | Context.getSizeType())) |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 921 | return false; |
| 922 | |
| 923 | // This function is a usual deallocation function if there are no |
| 924 | // single-parameter deallocation functions of the same kind. |
| 925 | for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName()); |
| 926 | R.first != R.second; ++R.first) { |
| 927 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first)) |
| 928 | if (FD->getNumParams() == 1) |
| 929 | return false; |
| 930 | } |
| 931 | |
| 932 | return true; |
| 933 | } |
| 934 | |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 935 | bool CXXMethodDecl::isCopyAssignmentOperator() const { |
| 936 | // C++0x [class.copy]p19: |
| 937 | // A user-declared copy assignment operator X::operator= is a non-static |
| 938 | // non-template member function of class X with exactly one parameter of |
| 939 | // type X, X&, const X&, volatile X& or const volatile X&. |
| 940 | if (/*operator=*/getOverloadedOperator() != OO_Equal || |
| 941 | /*non-static*/ isStatic() || |
| 942 | /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() || |
| 943 | /*exactly one parameter*/getNumParams() != 1) |
| 944 | return false; |
| 945 | |
| 946 | QualType ParamType = getParamDecl(0)->getType(); |
| 947 | if (const LValueReferenceType *Ref = ParamType->getAs<LValueReferenceType>()) |
| 948 | ParamType = Ref->getPointeeType(); |
| 949 | |
| 950 | ASTContext &Context = getASTContext(); |
| 951 | QualType ClassType |
| 952 | = Context.getCanonicalType(Context.getTypeDeclType(getParent())); |
| 953 | return Context.hasSameUnqualifiedType(ClassType, ParamType); |
| 954 | } |
| 955 | |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 956 | void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) { |
Anders Carlsson | 3aaf486 | 2009-12-04 05:51:56 +0000 | [diff] [blame] | 957 | assert(MD->isCanonicalDecl() && "Method is not canonical!"); |
Anders Carlsson | c076c45 | 2010-01-30 17:42:34 +0000 | [diff] [blame] | 958 | assert(!MD->getParent()->isDependentContext() && |
| 959 | "Can't add an overridden method to a class template!"); |
| 960 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 961 | getASTContext().addOverriddenMethod(this, MD); |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const { |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 965 | return getASTContext().overridden_methods_begin(this); |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const { |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 969 | return getASTContext().overridden_methods_end(this); |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Argyrios Kyrtzidis | c91e9f4 | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 972 | unsigned CXXMethodDecl::size_overridden_methods() const { |
| 973 | return getASTContext().overridden_methods_size(this); |
| 974 | } |
| 975 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 976 | QualType CXXMethodDecl::getThisType(ASTContext &C) const { |
Argyrios Kyrtzidis | b0d178d | 2008-10-24 22:28:18 +0000 | [diff] [blame] | 977 | // C++ 9.3.2p1: The type of this in a member function of a class X is X*. |
| 978 | // If the member function is declared const, the type of this is const X*, |
| 979 | // if the member function is declared volatile, the type of this is |
| 980 | // volatile X*, and if the member function is declared const volatile, |
| 981 | // the type of this is const volatile X*. |
| 982 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 983 | assert(isInstance() && "No 'this' for static methods!"); |
Anders Carlsson | 31a0875 | 2009-06-13 02:59:33 +0000 | [diff] [blame] | 984 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 985 | QualType ClassTy = C.getTypeDeclType(getParent()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 986 | ClassTy = C.getQualifiedType(ClassTy, |
| 987 | Qualifiers::fromCVRMask(getTypeQualifiers())); |
Anders Carlsson | 4e57992 | 2009-07-10 21:35:09 +0000 | [diff] [blame] | 988 | return C.getPointerType(ClassTy); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 989 | } |
| 990 | |
Eli Friedman | d7d7f67 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 991 | bool CXXMethodDecl::hasInlineBody() const { |
Douglas Gregor | bd6d619 | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 992 | // If this function is a template instantiation, look at the template from |
| 993 | // which it was instantiated. |
| 994 | const FunctionDecl *CheckFn = getTemplateInstantiationPattern(); |
| 995 | if (!CheckFn) |
| 996 | CheckFn = this; |
| 997 | |
Eli Friedman | d7d7f67 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 998 | const FunctionDecl *fn; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 999 | return CheckFn->hasBody(fn) && !fn->isOutOfLine(); |
Eli Friedman | d7d7f67 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1002 | CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, |
| 1003 | TypeSourceInfo *TInfo, bool IsVirtual, |
| 1004 | SourceLocation L, Expr *Init, |
| 1005 | SourceLocation R, |
| 1006 | SourceLocation EllipsisLoc) |
Sean Hunt | f51d0b6 | 2011-01-08 23:01:16 +0000 | [diff] [blame] | 1007 | : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init), |
Abramo Bagnara | a0af3b4 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 1008 | LParenLoc(L), RParenLoc(R), IsVirtual(IsVirtual), IsWritten(false), |
| 1009 | SourceOrderOrNumArrayIndices(0) |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1010 | { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1013 | CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, |
| 1014 | FieldDecl *Member, |
| 1015 | SourceLocation MemberLoc, |
| 1016 | SourceLocation L, Expr *Init, |
| 1017 | SourceLocation R) |
Sean Hunt | f51d0b6 | 2011-01-08 23:01:16 +0000 | [diff] [blame] | 1018 | : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init), |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1019 | LParenLoc(L), RParenLoc(R), IsVirtual(false), |
| 1020 | IsWritten(false), SourceOrderOrNumArrayIndices(0) |
| 1021 | { |
| 1022 | } |
| 1023 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1024 | CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, |
| 1025 | IndirectFieldDecl *Member, |
| 1026 | SourceLocation MemberLoc, |
| 1027 | SourceLocation L, Expr *Init, |
| 1028 | SourceLocation R) |
Sean Hunt | f51d0b6 | 2011-01-08 23:01:16 +0000 | [diff] [blame] | 1029 | : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init), |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1030 | LParenLoc(L), RParenLoc(R), IsVirtual(false), |
Abramo Bagnara | a0af3b4 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 1031 | IsWritten(false), SourceOrderOrNumArrayIndices(0) |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1032 | { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1035 | CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, |
| 1036 | FieldDecl *Member, |
| 1037 | SourceLocation MemberLoc, |
| 1038 | SourceLocation L, Expr *Init, |
| 1039 | SourceLocation R, |
| 1040 | VarDecl **Indices, |
| 1041 | unsigned NumIndices) |
Sean Hunt | f51d0b6 | 2011-01-08 23:01:16 +0000 | [diff] [blame] | 1042 | : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init), |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1043 | LParenLoc(L), RParenLoc(R), IsVirtual(false), |
Abramo Bagnara | a0af3b4 | 2010-05-26 18:09:23 +0000 | [diff] [blame] | 1044 | IsWritten(false), SourceOrderOrNumArrayIndices(NumIndices) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1045 | { |
| 1046 | VarDecl **MyIndices = reinterpret_cast<VarDecl **> (this + 1); |
| 1047 | memcpy(MyIndices, Indices, NumIndices * sizeof(VarDecl *)); |
| 1048 | } |
| 1049 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1050 | CXXCtorInitializer *CXXCtorInitializer::Create(ASTContext &Context, |
| 1051 | FieldDecl *Member, |
| 1052 | SourceLocation MemberLoc, |
| 1053 | SourceLocation L, Expr *Init, |
| 1054 | SourceLocation R, |
| 1055 | VarDecl **Indices, |
| 1056 | unsigned NumIndices) { |
| 1057 | void *Mem = Context.Allocate(sizeof(CXXCtorInitializer) + |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1058 | sizeof(VarDecl *) * NumIndices, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1059 | llvm::alignOf<CXXCtorInitializer>()); |
Sean Hunt | f51d0b6 | 2011-01-08 23:01:16 +0000 | [diff] [blame] | 1060 | return new (Mem) CXXCtorInitializer(Context, Member, MemberLoc, L, Init, R, |
| 1061 | Indices, NumIndices); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1064 | TypeLoc CXXCtorInitializer::getBaseClassLoc() const { |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1065 | if (isBaseInitializer()) |
Sean Hunt | f51d0b6 | 2011-01-08 23:01:16 +0000 | [diff] [blame] | 1066 | return Initializee.get<TypeSourceInfo*>()->getTypeLoc(); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1067 | else |
| 1068 | return TypeLoc(); |
| 1069 | } |
| 1070 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1071 | const Type *CXXCtorInitializer::getBaseClass() const { |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1072 | if (isBaseInitializer()) |
Sean Hunt | f51d0b6 | 2011-01-08 23:01:16 +0000 | [diff] [blame] | 1073 | return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr(); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1074 | else |
| 1075 | return 0; |
| 1076 | } |
| 1077 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1078 | SourceLocation CXXCtorInitializer::getSourceLocation() const { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 1079 | if (isAnyMemberInitializer()) |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1080 | return getMemberLocation(); |
| 1081 | |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 1082 | return getBaseClassLoc().getLocalSourceRange().getBegin(); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 1085 | SourceRange CXXCtorInitializer::getSourceRange() const { |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 1086 | return SourceRange(getSourceLocation(), getRParenLoc()); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1089 | CXXConstructorDecl * |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 1090 | CXXConstructorDecl::Create(ASTContext &C, EmptyShell Empty) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1091 | return new (C) CXXConstructorDecl(0, DeclarationNameInfo(), |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 1092 | QualType(), 0, false, false, false); |
| 1093 | } |
| 1094 | |
| 1095 | CXXConstructorDecl * |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1096 | CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1097 | const DeclarationNameInfo &NameInfo, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1098 | QualType T, TypeSourceInfo *TInfo, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1099 | bool isExplicit, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1100 | bool isInline, |
| 1101 | bool isImplicitlyDeclared) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1102 | assert(NameInfo.getName().getNameKind() |
| 1103 | == DeclarationName::CXXConstructorName && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1104 | "Name must refer to a constructor"); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1105 | return new (C) CXXConstructorDecl(RD, NameInfo, T, TInfo, isExplicit, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1106 | isInline, isImplicitlyDeclared); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1109 | bool CXXConstructorDecl::isDefaultConstructor() const { |
| 1110 | // C++ [class.ctor]p5: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 1111 | // A default constructor for a class X is a constructor of class |
| 1112 | // X that can be called without an argument. |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1113 | return (getNumParams() == 0) || |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 1114 | (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg()); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1117 | bool |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 1118 | CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const { |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1119 | return isCopyOrMoveConstructor(TypeQuals) && |
| 1120 | getParamDecl(0)->getType()->isLValueReferenceType(); |
| 1121 | } |
| 1122 | |
| 1123 | bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const { |
| 1124 | return isCopyOrMoveConstructor(TypeQuals) && |
| 1125 | getParamDecl(0)->getType()->isRValueReferenceType(); |
| 1126 | } |
| 1127 | |
| 1128 | /// \brief Determine whether this is a copy or move constructor. |
| 1129 | bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const { |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1130 | // C++ [class.copy]p2: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 1131 | // A non-template constructor for class X is a copy constructor |
| 1132 | // if its first parameter is of type X&, const X&, volatile X& or |
| 1133 | // const volatile X&, and either there are no other parameters |
| 1134 | // or else all other parameters have default arguments (8.3.6). |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1135 | // C++0x [class.copy]p3: |
| 1136 | // A non-template constructor for class X is a move constructor if its |
| 1137 | // first parameter is of type X&&, const X&&, volatile X&&, or |
| 1138 | // const volatile X&&, and either there are no other parameters or else |
| 1139 | // all other parameters have default arguments. |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1140 | if ((getNumParams() < 1) || |
Douglas Gregor | 77da3f4 | 2009-10-13 23:45:19 +0000 | [diff] [blame] | 1141 | (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 1142 | (getPrimaryTemplate() != 0) || |
Douglas Gregor | 77da3f4 | 2009-10-13 23:45:19 +0000 | [diff] [blame] | 1143 | (getDescribedFunctionTemplate() != 0)) |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1144 | return false; |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1145 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1146 | const ParmVarDecl *Param = getParamDecl(0); |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1147 | |
| 1148 | // Do we have a reference type? |
| 1149 | const ReferenceType *ParamRefType = Param->getType()->getAs<ReferenceType>(); |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 1150 | if (!ParamRefType) |
| 1151 | return false; |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1152 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 1153 | // Is it a reference to our class type? |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 1154 | ASTContext &Context = getASTContext(); |
| 1155 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 1156 | CanQualType PointeeType |
| 1157 | = Context.getCanonicalType(ParamRefType->getPointeeType()); |
Douglas Gregor | 14e0b3d | 2009-09-15 20:50:23 +0000 | [diff] [blame] | 1158 | CanQualType ClassTy |
| 1159 | = Context.getCanonicalType(Context.getTagDeclType(getParent())); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1160 | if (PointeeType.getUnqualifiedType() != ClassTy) |
| 1161 | return false; |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1162 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1163 | // FIXME: other qualifiers? |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1164 | |
| 1165 | // We have a copy or move constructor. |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1166 | TypeQuals = PointeeType.getCVRQualifiers(); |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 1167 | return true; |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1170 | bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1171 | // C++ [class.conv.ctor]p1: |
| 1172 | // A constructor declared without the function-specifier explicit |
| 1173 | // that can be called with a single parameter specifies a |
| 1174 | // conversion from the type of its first parameter to the type of |
| 1175 | // its class. Such a constructor is called a converting |
| 1176 | // constructor. |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 1177 | if (isExplicit() && !AllowExplicit) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1178 | return false; |
| 1179 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1180 | return (getNumParams() == 0 && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1181 | getType()->getAs<FunctionProtoType>()->isVariadic()) || |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1182 | (getNumParams() == 1) || |
Anders Carlsson | ae0b4e7 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 1183 | (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg()); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 1184 | } |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1185 | |
Douglas Gregor | 6493cc5 | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 1186 | bool CXXConstructorDecl::isSpecializationCopyingObject() const { |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1187 | if ((getNumParams() < 1) || |
| 1188 | (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || |
| 1189 | (getPrimaryTemplate() == 0) || |
| 1190 | (getDescribedFunctionTemplate() != 0)) |
| 1191 | return false; |
| 1192 | |
| 1193 | const ParmVarDecl *Param = getParamDecl(0); |
| 1194 | |
| 1195 | ASTContext &Context = getASTContext(); |
| 1196 | CanQualType ParamType = Context.getCanonicalType(Param->getType()); |
| 1197 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 1198 | // Is it the same as our our class type? |
| 1199 | CanQualType ClassTy |
| 1200 | = Context.getCanonicalType(Context.getTagDeclType(getParent())); |
| 1201 | if (ParamType.getUnqualifiedType() != ClassTy) |
| 1202 | return false; |
| 1203 | |
| 1204 | return true; |
| 1205 | } |
| 1206 | |
Sebastian Redl | f677ea3 | 2011-02-05 19:23:19 +0000 | [diff] [blame^] | 1207 | const CXXConstructorDecl *CXXConstructorDecl::getInheritedConstructor() const { |
| 1208 | // Hack: we store the inherited constructor in the overridden method table |
| 1209 | method_iterator It = begin_overridden_methods(); |
| 1210 | if (It == end_overridden_methods()) |
| 1211 | return 0; |
| 1212 | |
| 1213 | return cast<CXXConstructorDecl>(*It); |
| 1214 | } |
| 1215 | |
| 1216 | void |
| 1217 | CXXConstructorDecl::setInheritedConstructor(const CXXConstructorDecl *BaseCtor){ |
| 1218 | // Hack: we store the inherited constructor in the overridden method table |
| 1219 | assert(size_overridden_methods() == 0 && "Base ctor already set."); |
| 1220 | addOverriddenMethod(BaseCtor); |
| 1221 | } |
| 1222 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1223 | CXXDestructorDecl * |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 1224 | CXXDestructorDecl::Create(ASTContext &C, EmptyShell Empty) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1225 | return new (C) CXXDestructorDecl(0, DeclarationNameInfo(), |
Craig Silverstein | b41d899 | 2010-10-21 00:44:50 +0000 | [diff] [blame] | 1226 | QualType(), 0, false, false); |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | CXXDestructorDecl * |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1230 | CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1231 | const DeclarationNameInfo &NameInfo, |
Craig Silverstein | b41d899 | 2010-10-21 00:44:50 +0000 | [diff] [blame] | 1232 | QualType T, TypeSourceInfo *TInfo, |
| 1233 | bool isInline, |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1234 | bool isImplicitlyDeclared) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1235 | assert(NameInfo.getName().getNameKind() |
| 1236 | == DeclarationName::CXXDestructorName && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1237 | "Name must refer to a destructor"); |
Craig Silverstein | b41d899 | 2010-10-21 00:44:50 +0000 | [diff] [blame] | 1238 | return new (C) CXXDestructorDecl(RD, NameInfo, T, TInfo, isInline, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1239 | isImplicitlyDeclared); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1242 | CXXConversionDecl * |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 1243 | CXXConversionDecl::Create(ASTContext &C, EmptyShell Empty) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1244 | return new (C) CXXConversionDecl(0, DeclarationNameInfo(), |
Chris Lattner | 6ad9ac0 | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 1245 | QualType(), 0, false, false); |
| 1246 | } |
| 1247 | |
| 1248 | CXXConversionDecl * |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1249 | CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1250 | const DeclarationNameInfo &NameInfo, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1251 | QualType T, TypeSourceInfo *TInfo, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1252 | bool isInline, bool isExplicit) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1253 | assert(NameInfo.getName().getNameKind() |
| 1254 | == DeclarationName::CXXConversionFunctionName && |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 1255 | "Name must refer to a conversion function"); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1256 | return new (C) CXXConversionDecl(RD, NameInfo, T, TInfo, |
| 1257 | isInline, isExplicit); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 1260 | LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1261 | DeclContext *DC, |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 1262 | SourceLocation L, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 1263 | LanguageIDs Lang, bool Braces) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 1264 | return new (C) LinkageSpecDecl(DC, L, Lang, Braces); |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 1265 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1266 | |
| 1267 | UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC, |
| 1268 | SourceLocation L, |
| 1269 | SourceLocation NamespaceLoc, |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 1270 | SourceRange QualifierRange, |
| 1271 | NestedNameSpecifier *Qualifier, |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1272 | SourceLocation IdentLoc, |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 1273 | NamedDecl *Used, |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1274 | DeclContext *CommonAncestor) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 1275 | if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used)) |
| 1276 | Used = NS->getOriginalNamespace(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1277 | return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange, |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 1278 | Qualifier, IdentLoc, Used, CommonAncestor); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 1281 | NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() { |
| 1282 | if (NamespaceAliasDecl *NA = |
| 1283 | dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace)) |
| 1284 | return NA->getNamespace(); |
| 1285 | return cast_or_null<NamespaceDecl>(NominatedNamespace); |
| 1286 | } |
| 1287 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1288 | NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 1289 | SourceLocation UsingLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1290 | SourceLocation AliasLoc, |
| 1291 | IdentifierInfo *Alias, |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 1292 | SourceRange QualifierRange, |
| 1293 | NestedNameSpecifier *Qualifier, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1294 | SourceLocation IdentLoc, |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 1295 | NamedDecl *Namespace) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 1296 | if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace)) |
| 1297 | Namespace = NS->getOriginalNamespace(); |
Douglas Gregor | 0a35bce | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 1298 | return new (C) NamespaceAliasDecl(DC, UsingLoc, AliasLoc, Alias, QualifierRange, |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 1299 | Qualifier, IdentLoc, Namespace); |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Argyrios Kyrtzidis | 826faa2 | 2010-11-10 05:40:41 +0000 | [diff] [blame] | 1302 | UsingDecl *UsingShadowDecl::getUsingDecl() const { |
| 1303 | const UsingShadowDecl *Shadow = this; |
| 1304 | while (const UsingShadowDecl *NextShadow = |
| 1305 | dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow)) |
| 1306 | Shadow = NextShadow; |
| 1307 | return cast<UsingDecl>(Shadow->UsingOrNextShadow); |
| 1308 | } |
| 1309 | |
| 1310 | void UsingDecl::addShadowDecl(UsingShadowDecl *S) { |
| 1311 | assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() && |
| 1312 | "declaration already in set"); |
| 1313 | assert(S->getUsingDecl() == this); |
| 1314 | |
| 1315 | if (FirstUsingShadow) |
| 1316 | S->UsingOrNextShadow = FirstUsingShadow; |
| 1317 | FirstUsingShadow = S; |
| 1318 | } |
| 1319 | |
| 1320 | void UsingDecl::removeShadowDecl(UsingShadowDecl *S) { |
| 1321 | assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() && |
| 1322 | "declaration not in set"); |
| 1323 | assert(S->getUsingDecl() == this); |
| 1324 | |
| 1325 | // Remove S from the shadow decl chain. This is O(n) but hopefully rare. |
| 1326 | |
| 1327 | if (FirstUsingShadow == S) { |
| 1328 | FirstUsingShadow = dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow); |
| 1329 | S->UsingOrNextShadow = this; |
| 1330 | return; |
| 1331 | } |
| 1332 | |
| 1333 | UsingShadowDecl *Prev = FirstUsingShadow; |
| 1334 | while (Prev->UsingOrNextShadow != S) |
| 1335 | Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow); |
| 1336 | Prev->UsingOrNextShadow = S->UsingOrNextShadow; |
| 1337 | S->UsingOrNextShadow = this; |
| 1338 | } |
| 1339 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1340 | UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1341 | SourceRange NNR, SourceLocation UL, |
| 1342 | NestedNameSpecifier* TargetNNS, |
| 1343 | const DeclarationNameInfo &NameInfo, |
| 1344 | bool IsTypeNameArg) { |
| 1345 | return new (C) UsingDecl(DC, NNR, UL, TargetNNS, NameInfo, IsTypeNameArg); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1348 | UnresolvedUsingValueDecl * |
| 1349 | UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC, |
| 1350 | SourceLocation UsingLoc, |
| 1351 | SourceRange TargetNNR, |
| 1352 | NestedNameSpecifier *TargetNNS, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1353 | const DeclarationNameInfo &NameInfo) { |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1354 | return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc, |
Abramo Bagnara | ef3dce8 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 1355 | TargetNNR, TargetNNS, NameInfo); |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | UnresolvedUsingTypenameDecl * |
| 1359 | UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC, |
| 1360 | SourceLocation UsingLoc, |
| 1361 | SourceLocation TypenameLoc, |
| 1362 | SourceRange TargetNNR, |
| 1363 | NestedNameSpecifier *TargetNNS, |
| 1364 | SourceLocation TargetNameLoc, |
| 1365 | DeclarationName TargetName) { |
| 1366 | return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc, |
| 1367 | TargetNNR, TargetNNS, |
| 1368 | TargetNameLoc, |
| 1369 | TargetName.getAsIdentifierInfo()); |
Anders Carlsson | 665b49c | 2009-08-28 05:30:28 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 1372 | StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC, |
| 1373 | SourceLocation L, Expr *AssertExpr, |
| 1374 | StringLiteral *Message) { |
| 1375 | return new (C) StaticAssertDecl(DC, L, AssertExpr, Message); |
| 1376 | } |
| 1377 | |
Anders Carlsson | 05bf2c7 | 2009-03-26 23:46:50 +0000 | [diff] [blame] | 1378 | static const char *getAccessName(AccessSpecifier AS) { |
| 1379 | switch (AS) { |
| 1380 | default: |
| 1381 | case AS_none: |
| 1382 | assert("Invalid access specifier!"); |
| 1383 | return 0; |
| 1384 | case AS_public: |
| 1385 | return "public"; |
| 1386 | case AS_private: |
| 1387 | return "private"; |
| 1388 | case AS_protected: |
| 1389 | return "protected"; |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, |
| 1394 | AccessSpecifier AS) { |
| 1395 | return DB << getAccessName(AS); |
| 1396 | } |