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