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