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