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