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 | |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 614 | void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) { |
Anders Carlsson | 3aaf486 | 2009-12-04 05:51:56 +0000 | [diff] [blame] | 615 | assert(MD->isCanonicalDecl() && "Method is not canonical!"); |
Anders Carlsson | c076c45 | 2010-01-30 17:42:34 +0000 | [diff] [blame] | 616 | assert(!MD->getParent()->isDependentContext() && |
| 617 | "Can't add an overridden method to a class template!"); |
| 618 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 619 | getASTContext().addOverriddenMethod(this, MD); |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const { |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 623 | return getASTContext().overridden_methods_begin(this); |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const { |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 627 | return getASTContext().overridden_methods_end(this); |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 630 | QualType CXXMethodDecl::getThisType(ASTContext &C) const { |
Argyrios Kyrtzidis | b0d178d | 2008-10-24 22:28:18 +0000 | [diff] [blame] | 631 | // C++ 9.3.2p1: The type of this in a member function of a class X is X*. |
| 632 | // If the member function is declared const, the type of this is const X*, |
| 633 | // if the member function is declared volatile, the type of this is |
| 634 | // volatile X*, and if the member function is declared const volatile, |
| 635 | // the type of this is const volatile X*. |
| 636 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 637 | assert(isInstance() && "No 'this' for static methods!"); |
Anders Carlsson | 31a0875 | 2009-06-13 02:59:33 +0000 | [diff] [blame] | 638 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame^] | 639 | QualType ClassTy = C.getTypeDeclType(getParent()); |
| 640 | |
| 641 | // Aesthetically we prefer not to synthesize a type as the |
| 642 | // InjectedClassNameType of a template pattern: injected class names |
| 643 | // are printed without template arguments, which might |
| 644 | // surprise/confuse/distract our poor users if they didn't |
| 645 | // explicitly write one. |
| 646 | if (isa<InjectedClassNameType>(ClassTy)) |
| 647 | ClassTy = cast<InjectedClassNameType>(ClassTy)->getUnderlyingType(); |
| 648 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 649 | ClassTy = C.getQualifiedType(ClassTy, |
| 650 | Qualifiers::fromCVRMask(getTypeQualifiers())); |
Anders Carlsson | 4e57992 | 2009-07-10 21:35:09 +0000 | [diff] [blame] | 651 | return C.getPointerType(ClassTy); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Eli Friedman | d7d7f67 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 654 | bool CXXMethodDecl::hasInlineBody() const { |
Douglas Gregor | bd6d619 | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 655 | // If this function is a template instantiation, look at the template from |
| 656 | // which it was instantiated. |
| 657 | const FunctionDecl *CheckFn = getTemplateInstantiationPattern(); |
| 658 | if (!CheckFn) |
| 659 | CheckFn = this; |
| 660 | |
Eli Friedman | d7d7f67 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 661 | const FunctionDecl *fn; |
Douglas Gregor | bd6d619 | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 662 | return CheckFn->getBody(fn) && !fn->isOutOfLine(); |
Eli Friedman | d7d7f67 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 665 | CXXBaseOrMemberInitializer:: |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 666 | CXXBaseOrMemberInitializer(ASTContext &Context, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 667 | TypeSourceInfo *TInfo, |
| 668 | SourceLocation L, Expr *Init, SourceLocation R) |
| 669 | : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0), |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 670 | LParenLoc(L), RParenLoc(R) |
| 671 | { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | CXXBaseOrMemberInitializer:: |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 675 | CXXBaseOrMemberInitializer(ASTContext &Context, |
| 676 | FieldDecl *Member, SourceLocation MemberLoc, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 677 | SourceLocation L, Expr *Init, SourceLocation R) |
| 678 | : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init), |
| 679 | AnonUnionMember(0), LParenLoc(L), RParenLoc(R) |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 680 | { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 683 | void CXXBaseOrMemberInitializer::Destroy(ASTContext &Context) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 684 | if (Init) |
| 685 | Init->Destroy(Context); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 686 | this->~CXXBaseOrMemberInitializer(); |
| 687 | } |
| 688 | |
| 689 | TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const { |
| 690 | if (isBaseInitializer()) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 691 | return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc(); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 692 | else |
| 693 | return TypeLoc(); |
| 694 | } |
| 695 | |
| 696 | Type *CXXBaseOrMemberInitializer::getBaseClass() { |
| 697 | if (isBaseInitializer()) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 698 | return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr(); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 699 | else |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | const Type *CXXBaseOrMemberInitializer::getBaseClass() const { |
| 704 | if (isBaseInitializer()) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 705 | return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr(); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 706 | else |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const { |
| 711 | if (isMemberInitializer()) |
| 712 | return getMemberLocation(); |
| 713 | |
| 714 | return getBaseClassLoc().getSourceRange().getBegin(); |
| 715 | } |
| 716 | |
| 717 | SourceRange CXXBaseOrMemberInitializer::getSourceRange() const { |
| 718 | return SourceRange(getSourceLocation(), getRParenLoc()); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 721 | CXXConstructorDecl * |
| 722 | CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 723 | SourceLocation L, DeclarationName N, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 724 | QualType T, TypeSourceInfo *TInfo, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 725 | bool isExplicit, |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 726 | bool isInline, bool isImplicitlyDeclared) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 727 | assert(N.getNameKind() == DeclarationName::CXXConstructorName && |
| 728 | "Name must refer to a constructor"); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 729 | return new (C) CXXConstructorDecl(RD, L, N, T, TInfo, isExplicit, isInline, |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 730 | isImplicitlyDeclared); |
| 731 | } |
| 732 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 733 | bool CXXConstructorDecl::isDefaultConstructor() const { |
| 734 | // C++ [class.ctor]p5: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 735 | // A default constructor for a class X is a constructor of class |
| 736 | // X that can be called without an argument. |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 737 | return (getNumParams() == 0) || |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 738 | (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg()); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 741 | bool |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 742 | CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const { |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 743 | // C++ [class.copy]p2: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 744 | // A non-template constructor for class X is a copy constructor |
| 745 | // if its first parameter is of type X&, const X&, volatile X& or |
| 746 | // const volatile X&, and either there are no other parameters |
| 747 | // or else all other parameters have default arguments (8.3.6). |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 748 | if ((getNumParams() < 1) || |
Douglas Gregor | 77da3f4 | 2009-10-13 23:45:19 +0000 | [diff] [blame] | 749 | (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 750 | (getPrimaryTemplate() != 0) || |
Douglas Gregor | 77da3f4 | 2009-10-13 23:45:19 +0000 | [diff] [blame] | 751 | (getDescribedFunctionTemplate() != 0)) |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 752 | return false; |
| 753 | |
| 754 | const ParmVarDecl *Param = getParamDecl(0); |
| 755 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 756 | // Do we have a reference type? Rvalue references don't count. |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 757 | const LValueReferenceType *ParamRefType = |
| 758 | Param->getType()->getAs<LValueReferenceType>(); |
| 759 | if (!ParamRefType) |
| 760 | return false; |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 761 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 762 | // Is it a reference to our class type? |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 763 | ASTContext &Context = getASTContext(); |
| 764 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 765 | CanQualType PointeeType |
| 766 | = Context.getCanonicalType(ParamRefType->getPointeeType()); |
Douglas Gregor | 14e0b3d | 2009-09-15 20:50:23 +0000 | [diff] [blame] | 767 | CanQualType ClassTy |
| 768 | = Context.getCanonicalType(Context.getTagDeclType(getParent())); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 769 | if (PointeeType.getUnqualifiedType() != ClassTy) |
| 770 | return false; |
| 771 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 772 | // FIXME: other qualifiers? |
| 773 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 774 | // We have a copy constructor. |
| 775 | TypeQuals = PointeeType.getCVRQualifiers(); |
| 776 | return true; |
| 777 | } |
| 778 | |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 779 | bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 780 | // C++ [class.conv.ctor]p1: |
| 781 | // A constructor declared without the function-specifier explicit |
| 782 | // that can be called with a single parameter specifies a |
| 783 | // conversion from the type of its first parameter to the type of |
| 784 | // its class. Such a constructor is called a converting |
| 785 | // constructor. |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 786 | if (isExplicit() && !AllowExplicit) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 787 | return false; |
| 788 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 789 | return (getNumParams() == 0 && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 790 | getType()->getAs<FunctionProtoType>()->isVariadic()) || |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 791 | (getNumParams() == 1) || |
Anders Carlsson | ae0b4e7 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 792 | (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg()); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 793 | } |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 794 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 795 | bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const { |
| 796 | if ((getNumParams() < 1) || |
| 797 | (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || |
| 798 | (getPrimaryTemplate() == 0) || |
| 799 | (getDescribedFunctionTemplate() != 0)) |
| 800 | return false; |
| 801 | |
| 802 | const ParmVarDecl *Param = getParamDecl(0); |
| 803 | |
| 804 | ASTContext &Context = getASTContext(); |
| 805 | CanQualType ParamType = Context.getCanonicalType(Param->getType()); |
| 806 | |
| 807 | // Strip off the lvalue reference, if any. |
| 808 | if (CanQual<LValueReferenceType> ParamRefType |
| 809 | = ParamType->getAs<LValueReferenceType>()) |
| 810 | ParamType = ParamRefType->getPointeeType(); |
| 811 | |
| 812 | |
| 813 | // Is it the same as our our class type? |
| 814 | CanQualType ClassTy |
| 815 | = Context.getCanonicalType(Context.getTagDeclType(getParent())); |
| 816 | if (ParamType.getUnqualifiedType() != ClassTy) |
| 817 | return false; |
| 818 | |
| 819 | return true; |
| 820 | } |
| 821 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 822 | CXXDestructorDecl * |
| 823 | CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 824 | SourceLocation L, DeclarationName N, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | QualType T, bool isInline, |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 826 | bool isImplicitlyDeclared) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 827 | assert(N.getNameKind() == DeclarationName::CXXDestructorName && |
| 828 | "Name must refer to a destructor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 829 | return new (C) CXXDestructorDecl(RD, L, N, T, isInline, |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 830 | isImplicitlyDeclared); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Fariborz Jahanian | d45c363 | 2009-07-01 21:05:43 +0000 | [diff] [blame] | 833 | void |
Fariborz Jahanian | 73b85f3 | 2009-07-01 23:35:25 +0000 | [diff] [blame] | 834 | CXXConstructorDecl::Destroy(ASTContext& C) { |
| 835 | C.Deallocate(BaseOrMemberInitializers); |
Fariborz Jahanian | 0d3c26c | 2009-07-07 16:24:08 +0000 | [diff] [blame] | 836 | CXXMethodDecl::Destroy(C); |
Fariborz Jahanian | 73b85f3 | 2009-07-01 23:35:25 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 839 | CXXConversionDecl * |
| 840 | CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 841 | SourceLocation L, DeclarationName N, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 842 | QualType T, TypeSourceInfo *TInfo, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 843 | bool isInline, bool isExplicit) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 844 | assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 845 | "Name must refer to a conversion function"); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 846 | return new (C) CXXConversionDecl(RD, L, N, T, TInfo, isInline, isExplicit); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 847 | } |
| 848 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 849 | FriendDecl *FriendDecl::Create(ASTContext &C, DeclContext *DC, |
| 850 | SourceLocation L, |
| 851 | FriendUnion Friend, |
| 852 | SourceLocation FriendL) { |
Daniel Dunbar | e32ecce | 2009-08-31 19:16:38 +0000 | [diff] [blame] | 853 | #ifndef NDEBUG |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 854 | if (Friend.is<NamedDecl*>()) { |
| 855 | NamedDecl *D = Friend.get<NamedDecl*>(); |
| 856 | assert(isa<FunctionDecl>(D) || |
| 857 | isa<CXXRecordDecl>(D) || |
| 858 | isa<FunctionTemplateDecl>(D) || |
| 859 | isa<ClassTemplateDecl>(D)); |
John McCall | e129d44 | 2009-12-17 23:21:11 +0000 | [diff] [blame] | 860 | |
| 861 | // As a temporary hack, we permit template instantiation to point |
| 862 | // to the original declaration when instantiating members. |
| 863 | assert(D->getFriendObjectKind() || |
| 864 | (cast<CXXRecordDecl>(DC)->getTemplateSpecializationKind())); |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 865 | } |
Daniel Dunbar | e32ecce | 2009-08-31 19:16:38 +0000 | [diff] [blame] | 866 | #endif |
John McCall | c48fbdf | 2009-08-11 21:13:21 +0000 | [diff] [blame] | 867 | |
John McCall | 02cace7 | 2009-08-28 07:59:38 +0000 | [diff] [blame] | 868 | return new (C) FriendDecl(DC, L, Friend, FriendL); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 869 | } |
John McCall | 3f9a8a6 | 2009-08-11 06:59:38 +0000 | [diff] [blame] | 870 | |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 871 | LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 872 | DeclContext *DC, |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 873 | SourceLocation L, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 874 | LanguageIDs Lang, bool Braces) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 875 | return new (C) LinkageSpecDecl(DC, L, Lang, Braces); |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 876 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 877 | |
| 878 | UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC, |
| 879 | SourceLocation L, |
| 880 | SourceLocation NamespaceLoc, |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 881 | SourceRange QualifierRange, |
| 882 | NestedNameSpecifier *Qualifier, |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 883 | SourceLocation IdentLoc, |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 884 | NamedDecl *Used, |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 885 | DeclContext *CommonAncestor) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 886 | if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used)) |
| 887 | Used = NS->getOriginalNamespace(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 888 | return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange, |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 889 | Qualifier, IdentLoc, Used, CommonAncestor); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 892 | NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() { |
| 893 | if (NamespaceAliasDecl *NA = |
| 894 | dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace)) |
| 895 | return NA->getNamespace(); |
| 896 | return cast_or_null<NamespaceDecl>(NominatedNamespace); |
| 897 | } |
| 898 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 899 | NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC, |
| 900 | SourceLocation L, |
| 901 | SourceLocation AliasLoc, |
| 902 | IdentifierInfo *Alias, |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 903 | SourceRange QualifierRange, |
| 904 | NestedNameSpecifier *Qualifier, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 905 | SourceLocation IdentLoc, |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 906 | NamedDecl *Namespace) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 907 | if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace)) |
| 908 | Namespace = NS->getOriginalNamespace(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 909 | return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange, |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 910 | Qualifier, IdentLoc, Namespace); |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 913 | UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 914 | SourceLocation L, SourceRange NNR, SourceLocation UL, |
| 915 | NestedNameSpecifier* TargetNNS, DeclarationName Name, |
| 916 | bool IsTypeNameArg) { |
| 917 | return new (C) UsingDecl(DC, L, NNR, UL, TargetNNS, Name, IsTypeNameArg); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 918 | } |
| 919 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 920 | UnresolvedUsingValueDecl * |
| 921 | UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC, |
| 922 | SourceLocation UsingLoc, |
| 923 | SourceRange TargetNNR, |
| 924 | NestedNameSpecifier *TargetNNS, |
| 925 | SourceLocation TargetNameLoc, |
| 926 | DeclarationName TargetName) { |
| 927 | return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc, |
| 928 | TargetNNR, TargetNNS, |
| 929 | TargetNameLoc, TargetName); |
| 930 | } |
| 931 | |
| 932 | UnresolvedUsingTypenameDecl * |
| 933 | UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC, |
| 934 | SourceLocation UsingLoc, |
| 935 | SourceLocation TypenameLoc, |
| 936 | SourceRange TargetNNR, |
| 937 | NestedNameSpecifier *TargetNNS, |
| 938 | SourceLocation TargetNameLoc, |
| 939 | DeclarationName TargetName) { |
| 940 | return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc, |
| 941 | TargetNNR, TargetNNS, |
| 942 | TargetNameLoc, |
| 943 | TargetName.getAsIdentifierInfo()); |
Anders Carlsson | 665b49c | 2009-08-28 05:30:28 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 946 | StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC, |
| 947 | SourceLocation L, Expr *AssertExpr, |
| 948 | StringLiteral *Message) { |
| 949 | return new (C) StaticAssertDecl(DC, L, AssertExpr, Message); |
| 950 | } |
| 951 | |
| 952 | void StaticAssertDecl::Destroy(ASTContext& C) { |
| 953 | AssertExpr->Destroy(C); |
| 954 | Message->Destroy(C); |
| 955 | this->~StaticAssertDecl(); |
| 956 | C.Deallocate((void *)this); |
| 957 | } |
| 958 | |
| 959 | StaticAssertDecl::~StaticAssertDecl() { |
| 960 | } |
| 961 | |
Anders Carlsson | 05bf2c7 | 2009-03-26 23:46:50 +0000 | [diff] [blame] | 962 | static const char *getAccessName(AccessSpecifier AS) { |
| 963 | switch (AS) { |
| 964 | default: |
| 965 | case AS_none: |
| 966 | assert("Invalid access specifier!"); |
| 967 | return 0; |
| 968 | case AS_public: |
| 969 | return "public"; |
| 970 | case AS_private: |
| 971 | return "private"; |
| 972 | case AS_protected: |
| 973 | return "protected"; |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, |
| 978 | AccessSpecifier AS) { |
| 979 | return DB << getAccessName(AS); |
| 980 | } |
| 981 | |
| 982 | |