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