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 | d60e22e | 2010-03-12 01:19:31 +0000 | [diff] [blame] | 36 | Definition(D), FirstFriend(0) { |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 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 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame^] | 321 | static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) { |
| 322 | QualType T; |
| 323 | if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv)) |
| 324 | T = ConvTemp->getTemplatedDecl()->getResultType(); |
| 325 | else |
| 326 | T = cast<CXXConversionDecl>(Conv)->getConversionType(); |
| 327 | return Context.getCanonicalType(T); |
Fariborz Jahanian | 0351a1e | 2009-10-07 20:43:36 +0000 | [diff] [blame] | 328 | } |
| 329 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame^] | 330 | /// Collect the visible conversions of a base class. |
| 331 | /// |
| 332 | /// \param Base a base class of the class we're considering |
| 333 | /// \param InVirtual whether this base class is a virtual base (or a base |
| 334 | /// of a virtual base) |
| 335 | /// \param Access the access along the inheritance path to this base |
| 336 | /// \param ParentHiddenTypes the conversions provided by the inheritors |
| 337 | /// of this base |
| 338 | /// \param Output the set to which to add conversions from non-virtual bases |
| 339 | /// \param VOutput the set to which to add conversions from virtual bases |
| 340 | /// \param HiddenVBaseCs the set of conversions which were hidden in a |
| 341 | /// virtual base along some inheritance path |
| 342 | static void CollectVisibleConversions(ASTContext &Context, |
| 343 | CXXRecordDecl *Record, |
| 344 | bool InVirtual, |
| 345 | AccessSpecifier Access, |
| 346 | const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes, |
| 347 | UnresolvedSetImpl &Output, |
| 348 | UnresolvedSetImpl &VOutput, |
| 349 | llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) { |
| 350 | // The set of types which have conversions in this class or its |
| 351 | // subclasses. As an optimization, we don't copy the derived set |
| 352 | // unless it might change. |
| 353 | const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes; |
| 354 | llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer; |
| 355 | |
| 356 | // Collect the direct conversions and figure out which conversions |
| 357 | // will be hidden in the subclasses. |
| 358 | UnresolvedSetImpl &Cs = *Record->getConversionFunctions(); |
| 359 | if (!Cs.empty()) { |
| 360 | HiddenTypesBuffer = ParentHiddenTypes; |
| 361 | HiddenTypes = &HiddenTypesBuffer; |
| 362 | |
| 363 | for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) { |
| 364 | bool Hidden = |
| 365 | !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl())); |
| 366 | |
| 367 | // If this conversion is hidden and we're in a virtual base, |
| 368 | // remember that it's hidden along some inheritance path. |
| 369 | if (Hidden && InVirtual) |
| 370 | HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())); |
| 371 | |
| 372 | // If this conversion isn't hidden, add it to the appropriate output. |
| 373 | else if (!Hidden) { |
| 374 | AccessSpecifier IAccess |
| 375 | = CXXRecordDecl::MergeAccess(Access, I.getAccess()); |
| 376 | |
| 377 | if (InVirtual) |
| 378 | VOutput.addDecl(I.getDecl(), IAccess); |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 379 | else |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame^] | 380 | Output.addDecl(I.getDecl(), IAccess); |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | } |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 384 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame^] | 385 | // Collect information recursively from any base classes. |
| 386 | for (CXXRecordDecl::base_class_iterator |
| 387 | I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) { |
| 388 | const RecordType *RT = I->getType()->getAs<RecordType>(); |
| 389 | if (!RT) continue; |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 390 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame^] | 391 | AccessSpecifier BaseAccess |
| 392 | = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier()); |
| 393 | bool BaseInVirtual = InVirtual || I->isVirtual(); |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 394 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame^] | 395 | CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl()); |
| 396 | CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess, |
| 397 | *HiddenTypes, Output, VOutput, HiddenVBaseCs); |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 398 | } |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame^] | 399 | } |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 400 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame^] | 401 | /// Collect the visible conversions of a class. |
| 402 | /// |
| 403 | /// This would be extremely straightforward if it weren't for virtual |
| 404 | /// bases. It might be worth special-casing that, really. |
| 405 | static void CollectVisibleConversions(ASTContext &Context, |
| 406 | CXXRecordDecl *Record, |
| 407 | UnresolvedSetImpl &Output) { |
| 408 | // The collection of all conversions in virtual bases that we've |
| 409 | // found. These will be added to the output as long as they don't |
| 410 | // appear in the hidden-conversions set. |
| 411 | UnresolvedSet<8> VBaseCs; |
| 412 | |
| 413 | // The set of conversions in virtual bases that we've determined to |
| 414 | // be hidden. |
| 415 | llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs; |
| 416 | |
| 417 | // The set of types hidden by classes derived from this one. |
| 418 | llvm::SmallPtrSet<CanQualType, 8> HiddenTypes; |
| 419 | |
| 420 | // Go ahead and collect the direct conversions and add them to the |
| 421 | // hidden-types set. |
| 422 | UnresolvedSetImpl &Cs = *Record->getConversionFunctions(); |
| 423 | Output.append(Cs.begin(), Cs.end()); |
| 424 | for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) |
| 425 | HiddenTypes.insert(GetConversionType(Context, I.getDecl())); |
| 426 | |
| 427 | // Recursively collect conversions from base classes. |
| 428 | for (CXXRecordDecl::base_class_iterator |
| 429 | I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) { |
| 430 | const RecordType *RT = I->getType()->getAs<RecordType>(); |
| 431 | if (!RT) continue; |
| 432 | |
| 433 | CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()), |
| 434 | I->isVirtual(), I->getAccessSpecifier(), |
| 435 | HiddenTypes, Output, VBaseCs, HiddenVBaseCs); |
| 436 | } |
| 437 | |
| 438 | // Add any unhidden conversions provided by virtual bases. |
| 439 | for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end(); |
| 440 | I != E; ++I) { |
| 441 | if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()))) |
| 442 | Output.addDecl(I.getDecl(), I.getAccess()); |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 443 | } |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /// getVisibleConversionFunctions - get all conversion functions visible |
| 447 | /// in current class; including conversion function templates. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 448 | const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() { |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 449 | // If root class, all conversions are visible. |
| 450 | if (bases_begin() == bases_end()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 451 | return &data().Conversions; |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 452 | // If visible conversion list is already evaluated, return it. |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 453 | if (data().ComputedVisibleConversions) |
| 454 | return &data().VisibleConversions; |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame^] | 455 | CollectVisibleConversions(getASTContext(), this, data().VisibleConversions); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 456 | data().ComputedVisibleConversions = true; |
| 457 | return &data().VisibleConversions; |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Fariborz Jahanian | debc629 | 2009-09-12 19:02:34 +0000 | [diff] [blame] | 460 | void CXXRecordDecl::addConversionFunction(CXXConversionDecl *ConvDecl) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 461 | assert(!ConvDecl->getDescribedFunctionTemplate() && |
| 462 | "Conversion function templates should cast to FunctionTemplateDecl."); |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame^] | 463 | assert(ConvDecl->getDeclContext() == this && |
| 464 | "conversion function does not belong to this record"); |
| 465 | |
| 466 | // We intentionally don't use the decl's access here because it |
| 467 | // hasn't been set yet. That's really just a misdesign in Sema. |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 468 | data().Conversions.addDecl(ConvDecl); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Fariborz Jahanian | debc629 | 2009-09-12 19:02:34 +0000 | [diff] [blame] | 471 | void CXXRecordDecl::addConversionFunction(FunctionTemplateDecl *ConvDecl) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 472 | assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) && |
| 473 | "Function template is not a conversion function template"); |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame^] | 474 | assert(ConvDecl->getDeclContext() == this && |
| 475 | "conversion function does not belong to this record"); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 476 | data().Conversions.addDecl(ConvDecl); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 477 | } |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 478 | |
Fariborz Jahanian | e7184df | 2009-12-03 18:44:40 +0000 | [diff] [blame] | 479 | |
| 480 | void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) { |
| 481 | Method->setVirtualAsWritten(true); |
| 482 | setAggregate(false); |
| 483 | setPOD(false); |
| 484 | setEmpty(false); |
| 485 | setPolymorphic(true); |
| 486 | setHasTrivialConstructor(false); |
| 487 | setHasTrivialCopyConstructor(false); |
| 488 | setHasTrivialCopyAssignment(false); |
| 489 | } |
| 490 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 491 | CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 492 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 493 | return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom()); |
| 494 | |
| 495 | return 0; |
| 496 | } |
| 497 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 498 | MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const { |
| 499 | return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>(); |
| 500 | } |
| 501 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 502 | void |
| 503 | CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD, |
| 504 | TemplateSpecializationKind TSK) { |
| 505 | assert(TemplateOrInstantiation.isNull() && |
| 506 | "Previous template or instantiation?"); |
| 507 | assert(!isa<ClassTemplateSpecializationDecl>(this)); |
| 508 | TemplateOrInstantiation |
| 509 | = new (getASTContext()) MemberSpecializationInfo(RD, TSK); |
| 510 | } |
| 511 | |
Anders Carlsson | b13e357 | 2009-12-07 06:33:48 +0000 | [diff] [blame] | 512 | TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{ |
| 513 | if (const ClassTemplateSpecializationDecl *Spec |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 514 | = dyn_cast<ClassTemplateSpecializationDecl>(this)) |
| 515 | return Spec->getSpecializationKind(); |
| 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 | return MSInfo->getTemplateSpecializationKind(); |
| 519 | |
| 520 | return TSK_Undeclared; |
| 521 | } |
| 522 | |
| 523 | void |
| 524 | CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) { |
| 525 | if (ClassTemplateSpecializationDecl *Spec |
| 526 | = dyn_cast<ClassTemplateSpecializationDecl>(this)) { |
| 527 | Spec->setSpecializationKind(TSK); |
| 528 | return; |
| 529 | } |
| 530 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 531 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 532 | MSInfo->setTemplateSpecializationKind(TSK); |
| 533 | return; |
| 534 | } |
| 535 | |
| 536 | assert(false && "Not a class template or member class specialization"); |
| 537 | } |
| 538 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 539 | CXXConstructorDecl * |
| 540 | CXXRecordDecl::getDefaultConstructor(ASTContext &Context) { |
| 541 | QualType ClassType = Context.getTypeDeclType(this); |
| 542 | DeclarationName ConstructorName |
| 543 | = Context.DeclarationNames.getCXXConstructorName( |
| 544 | Context.getCanonicalType(ClassType.getUnqualifiedType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 546 | DeclContext::lookup_const_iterator Con, ConEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 547 | for (llvm::tie(Con, ConEnd) = lookup(ConstructorName); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 548 | Con != ConEnd; ++Con) { |
Douglas Gregor | d93bacf | 2009-09-04 14:46:39 +0000 | [diff] [blame] | 549 | // FIXME: In C++0x, a constructor template can be a default constructor. |
| 550 | if (isa<FunctionTemplateDecl>(*Con)) |
| 551 | continue; |
| 552 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 553 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 554 | if (Constructor->isDefaultConstructor()) |
| 555 | return Constructor; |
| 556 | } |
| 557 | return 0; |
| 558 | } |
| 559 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 560 | CXXDestructorDecl *CXXRecordDecl::getDestructor(ASTContext &Context) const { |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 561 | QualType ClassType = Context.getTypeDeclType(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 562 | |
| 563 | DeclarationName Name |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 564 | = Context.DeclarationNames.getCXXDestructorName( |
| 565 | Context.getCanonicalType(ClassType)); |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 566 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 567 | DeclContext::lookup_const_iterator I, E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 568 | llvm::tie(I, E) = lookup(Name); |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 569 | assert(I != E && "Did not find a destructor!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 570 | |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 571 | CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I); |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 572 | assert(++I == E && "Found more than one destructor!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 573 | |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 574 | return Dtor; |
| 575 | } |
| 576 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 577 | CXXMethodDecl * |
| 578 | CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 579 | SourceLocation L, DeclarationName N, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 580 | QualType T, TypeSourceInfo *TInfo, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 581 | bool isStatic, bool isInline) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 582 | return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, TInfo, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 583 | isStatic, isInline); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 586 | bool CXXMethodDecl::isUsualDeallocationFunction() const { |
| 587 | if (getOverloadedOperator() != OO_Delete && |
| 588 | getOverloadedOperator() != OO_Array_Delete) |
| 589 | return false; |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 590 | |
| 591 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 592 | // A template instance is never a usual deallocation function, |
| 593 | // regardless of its signature. |
| 594 | if (getPrimaryTemplate()) |
| 595 | return false; |
| 596 | |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 597 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 598 | // If a class T has a member deallocation function named operator delete |
| 599 | // with exactly one parameter, then that function is a usual (non-placement) |
| 600 | // deallocation function. [...] |
| 601 | if (getNumParams() == 1) |
| 602 | return true; |
| 603 | |
| 604 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 605 | // [...] If class T does not declare such an operator delete but does |
| 606 | // declare a member deallocation function named operator delete with |
| 607 | // exactly two parameters, the second of which has type std::size_t (18.1), |
| 608 | // then this function is a usual deallocation function. |
| 609 | ASTContext &Context = getASTContext(); |
| 610 | if (getNumParams() != 2 || |
Chandler Carruth | e228ba9 | 2010-02-08 18:54:05 +0000 | [diff] [blame] | 611 | !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(), |
| 612 | Context.getSizeType())) |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 613 | return false; |
| 614 | |
| 615 | // This function is a usual deallocation function if there are no |
| 616 | // single-parameter deallocation functions of the same kind. |
| 617 | for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName()); |
| 618 | R.first != R.second; ++R.first) { |
| 619 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first)) |
| 620 | if (FD->getNumParams() == 1) |
| 621 | return false; |
| 622 | } |
| 623 | |
| 624 | return true; |
| 625 | } |
| 626 | |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 627 | void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) { |
Anders Carlsson | 3aaf486 | 2009-12-04 05:51:56 +0000 | [diff] [blame] | 628 | assert(MD->isCanonicalDecl() && "Method is not canonical!"); |
Anders Carlsson | c076c45 | 2010-01-30 17:42:34 +0000 | [diff] [blame] | 629 | assert(!MD->getParent()->isDependentContext() && |
| 630 | "Can't add an overridden method to a class template!"); |
| 631 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 632 | getASTContext().addOverriddenMethod(this, MD); |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const { |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 636 | return getASTContext().overridden_methods_begin(this); |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const { |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 640 | return getASTContext().overridden_methods_end(this); |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 643 | QualType CXXMethodDecl::getThisType(ASTContext &C) const { |
Argyrios Kyrtzidis | b0d178d | 2008-10-24 22:28:18 +0000 | [diff] [blame] | 644 | // C++ 9.3.2p1: The type of this in a member function of a class X is X*. |
| 645 | // If the member function is declared const, the type of this is const X*, |
| 646 | // if the member function is declared volatile, the type of this is |
| 647 | // volatile X*, and if the member function is declared const volatile, |
| 648 | // the type of this is const volatile X*. |
| 649 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 650 | assert(isInstance() && "No 'this' for static methods!"); |
Anders Carlsson | 31a0875 | 2009-06-13 02:59:33 +0000 | [diff] [blame] | 651 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 652 | QualType ClassTy = C.getTypeDeclType(getParent()); |
| 653 | |
| 654 | // Aesthetically we prefer not to synthesize a type as the |
| 655 | // InjectedClassNameType of a template pattern: injected class names |
| 656 | // are printed without template arguments, which might |
| 657 | // surprise/confuse/distract our poor users if they didn't |
| 658 | // explicitly write one. |
| 659 | if (isa<InjectedClassNameType>(ClassTy)) |
| 660 | ClassTy = cast<InjectedClassNameType>(ClassTy)->getUnderlyingType(); |
| 661 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 662 | ClassTy = C.getQualifiedType(ClassTy, |
| 663 | Qualifiers::fromCVRMask(getTypeQualifiers())); |
Anders Carlsson | 4e57992 | 2009-07-10 21:35:09 +0000 | [diff] [blame] | 664 | return C.getPointerType(ClassTy); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Eli Friedman | d7d7f67 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 667 | bool CXXMethodDecl::hasInlineBody() const { |
Douglas Gregor | bd6d619 | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 668 | // If this function is a template instantiation, look at the template from |
| 669 | // which it was instantiated. |
| 670 | const FunctionDecl *CheckFn = getTemplateInstantiationPattern(); |
| 671 | if (!CheckFn) |
| 672 | CheckFn = this; |
| 673 | |
Eli Friedman | d7d7f67 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 674 | const FunctionDecl *fn; |
Douglas Gregor | bd6d619 | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 675 | return CheckFn->getBody(fn) && !fn->isOutOfLine(); |
Eli Friedman | d7d7f67 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 678 | CXXBaseOrMemberInitializer:: |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 679 | CXXBaseOrMemberInitializer(ASTContext &Context, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 680 | TypeSourceInfo *TInfo, |
| 681 | SourceLocation L, Expr *Init, SourceLocation R) |
| 682 | : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0), |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 683 | LParenLoc(L), RParenLoc(R) |
| 684 | { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | CXXBaseOrMemberInitializer:: |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 688 | CXXBaseOrMemberInitializer(ASTContext &Context, |
| 689 | FieldDecl *Member, SourceLocation MemberLoc, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 690 | SourceLocation L, Expr *Init, SourceLocation R) |
| 691 | : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init), |
| 692 | AnonUnionMember(0), LParenLoc(L), RParenLoc(R) |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 693 | { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 696 | void CXXBaseOrMemberInitializer::Destroy(ASTContext &Context) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 697 | if (Init) |
| 698 | Init->Destroy(Context); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 699 | this->~CXXBaseOrMemberInitializer(); |
| 700 | } |
| 701 | |
| 702 | TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const { |
| 703 | if (isBaseInitializer()) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 704 | return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc(); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 705 | else |
| 706 | return TypeLoc(); |
| 707 | } |
| 708 | |
| 709 | Type *CXXBaseOrMemberInitializer::getBaseClass() { |
| 710 | if (isBaseInitializer()) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 711 | return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr(); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 712 | else |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | const Type *CXXBaseOrMemberInitializer::getBaseClass() const { |
| 717 | if (isBaseInitializer()) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 718 | return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr(); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 719 | else |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const { |
| 724 | if (isMemberInitializer()) |
| 725 | return getMemberLocation(); |
| 726 | |
| 727 | return getBaseClassLoc().getSourceRange().getBegin(); |
| 728 | } |
| 729 | |
| 730 | SourceRange CXXBaseOrMemberInitializer::getSourceRange() const { |
| 731 | return SourceRange(getSourceLocation(), getRParenLoc()); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 734 | CXXConstructorDecl * |
| 735 | CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 736 | SourceLocation L, DeclarationName N, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 737 | QualType T, TypeSourceInfo *TInfo, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 738 | bool isExplicit, |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 739 | bool isInline, bool isImplicitlyDeclared) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 740 | assert(N.getNameKind() == DeclarationName::CXXConstructorName && |
| 741 | "Name must refer to a constructor"); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 742 | return new (C) CXXConstructorDecl(RD, L, N, T, TInfo, isExplicit, isInline, |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 743 | isImplicitlyDeclared); |
| 744 | } |
| 745 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 746 | bool CXXConstructorDecl::isDefaultConstructor() const { |
| 747 | // C++ [class.ctor]p5: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 748 | // A default constructor for a class X is a constructor of class |
| 749 | // X that can be called without an argument. |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 750 | return (getNumParams() == 0) || |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 751 | (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg()); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 754 | bool |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 755 | CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const { |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 756 | // C++ [class.copy]p2: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 757 | // A non-template constructor for class X is a copy constructor |
| 758 | // if its first parameter is of type X&, const X&, volatile X& or |
| 759 | // const volatile X&, and either there are no other parameters |
| 760 | // or else all other parameters have default arguments (8.3.6). |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 761 | if ((getNumParams() < 1) || |
Douglas Gregor | 77da3f4 | 2009-10-13 23:45:19 +0000 | [diff] [blame] | 762 | (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 763 | (getPrimaryTemplate() != 0) || |
Douglas Gregor | 77da3f4 | 2009-10-13 23:45:19 +0000 | [diff] [blame] | 764 | (getDescribedFunctionTemplate() != 0)) |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 765 | return false; |
| 766 | |
| 767 | const ParmVarDecl *Param = getParamDecl(0); |
| 768 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 769 | // Do we have a reference type? Rvalue references don't count. |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 770 | const LValueReferenceType *ParamRefType = |
| 771 | Param->getType()->getAs<LValueReferenceType>(); |
| 772 | if (!ParamRefType) |
| 773 | return false; |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 774 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 775 | // Is it a reference to our class type? |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 776 | ASTContext &Context = getASTContext(); |
| 777 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 778 | CanQualType PointeeType |
| 779 | = Context.getCanonicalType(ParamRefType->getPointeeType()); |
Douglas Gregor | 14e0b3d | 2009-09-15 20:50:23 +0000 | [diff] [blame] | 780 | CanQualType ClassTy |
| 781 | = Context.getCanonicalType(Context.getTagDeclType(getParent())); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 782 | if (PointeeType.getUnqualifiedType() != ClassTy) |
| 783 | return false; |
| 784 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 785 | // FIXME: other qualifiers? |
| 786 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 787 | // We have a copy constructor. |
| 788 | TypeQuals = PointeeType.getCVRQualifiers(); |
| 789 | return true; |
| 790 | } |
| 791 | |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 792 | bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 793 | // C++ [class.conv.ctor]p1: |
| 794 | // A constructor declared without the function-specifier explicit |
| 795 | // that can be called with a single parameter specifies a |
| 796 | // conversion from the type of its first parameter to the type of |
| 797 | // its class. Such a constructor is called a converting |
| 798 | // constructor. |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 799 | if (isExplicit() && !AllowExplicit) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 800 | return false; |
| 801 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 802 | return (getNumParams() == 0 && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 803 | getType()->getAs<FunctionProtoType>()->isVariadic()) || |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 804 | (getNumParams() == 1) || |
Anders Carlsson | ae0b4e7 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 805 | (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg()); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 806 | } |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 807 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 808 | bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const { |
| 809 | if ((getNumParams() < 1) || |
| 810 | (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || |
| 811 | (getPrimaryTemplate() == 0) || |
| 812 | (getDescribedFunctionTemplate() != 0)) |
| 813 | return false; |
| 814 | |
| 815 | const ParmVarDecl *Param = getParamDecl(0); |
| 816 | |
| 817 | ASTContext &Context = getASTContext(); |
| 818 | CanQualType ParamType = Context.getCanonicalType(Param->getType()); |
| 819 | |
| 820 | // Strip off the lvalue reference, if any. |
| 821 | if (CanQual<LValueReferenceType> ParamRefType |
| 822 | = ParamType->getAs<LValueReferenceType>()) |
| 823 | ParamType = ParamRefType->getPointeeType(); |
| 824 | |
| 825 | |
| 826 | // Is it the same as our our class type? |
| 827 | CanQualType ClassTy |
| 828 | = Context.getCanonicalType(Context.getTagDeclType(getParent())); |
| 829 | if (ParamType.getUnqualifiedType() != ClassTy) |
| 830 | return false; |
| 831 | |
| 832 | return true; |
| 833 | } |
| 834 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 835 | CXXDestructorDecl * |
| 836 | CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 837 | SourceLocation L, DeclarationName N, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 838 | QualType T, bool isInline, |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 839 | bool isImplicitlyDeclared) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 840 | assert(N.getNameKind() == DeclarationName::CXXDestructorName && |
| 841 | "Name must refer to a destructor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 842 | return new (C) CXXDestructorDecl(RD, L, N, T, isInline, |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 843 | isImplicitlyDeclared); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Fariborz Jahanian | d45c363 | 2009-07-01 21:05:43 +0000 | [diff] [blame] | 846 | void |
Fariborz Jahanian | 73b85f3 | 2009-07-01 23:35:25 +0000 | [diff] [blame] | 847 | CXXConstructorDecl::Destroy(ASTContext& C) { |
| 848 | C.Deallocate(BaseOrMemberInitializers); |
Fariborz Jahanian | 0d3c26c | 2009-07-07 16:24:08 +0000 | [diff] [blame] | 849 | CXXMethodDecl::Destroy(C); |
Fariborz Jahanian | 73b85f3 | 2009-07-01 23:35:25 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 852 | CXXConversionDecl * |
| 853 | CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 854 | SourceLocation L, DeclarationName N, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 855 | QualType T, TypeSourceInfo *TInfo, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 856 | bool isInline, bool isExplicit) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 857 | assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 858 | "Name must refer to a conversion function"); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 859 | return new (C) CXXConversionDecl(RD, L, N, T, TInfo, isInline, isExplicit); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 862 | LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | DeclContext *DC, |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 864 | SourceLocation L, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 865 | LanguageIDs Lang, bool Braces) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 866 | return new (C) LinkageSpecDecl(DC, L, Lang, Braces); |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 867 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 868 | |
| 869 | UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC, |
| 870 | SourceLocation L, |
| 871 | SourceLocation NamespaceLoc, |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 872 | SourceRange QualifierRange, |
| 873 | NestedNameSpecifier *Qualifier, |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 874 | SourceLocation IdentLoc, |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 875 | NamedDecl *Used, |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 876 | DeclContext *CommonAncestor) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 877 | if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used)) |
| 878 | Used = NS->getOriginalNamespace(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 879 | return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange, |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 880 | Qualifier, IdentLoc, Used, CommonAncestor); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 883 | NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() { |
| 884 | if (NamespaceAliasDecl *NA = |
| 885 | dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace)) |
| 886 | return NA->getNamespace(); |
| 887 | return cast_or_null<NamespaceDecl>(NominatedNamespace); |
| 888 | } |
| 889 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 890 | NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC, |
| 891 | SourceLocation L, |
| 892 | SourceLocation AliasLoc, |
| 893 | IdentifierInfo *Alias, |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 894 | SourceRange QualifierRange, |
| 895 | NestedNameSpecifier *Qualifier, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 896 | SourceLocation IdentLoc, |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 897 | NamedDecl *Namespace) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 898 | if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace)) |
| 899 | Namespace = NS->getOriginalNamespace(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 900 | return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange, |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 901 | Qualifier, IdentLoc, Namespace); |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 904 | UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 905 | SourceLocation L, SourceRange NNR, SourceLocation UL, |
| 906 | NestedNameSpecifier* TargetNNS, DeclarationName Name, |
| 907 | bool IsTypeNameArg) { |
| 908 | return new (C) UsingDecl(DC, L, NNR, UL, TargetNNS, Name, IsTypeNameArg); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 909 | } |
| 910 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 911 | UnresolvedUsingValueDecl * |
| 912 | UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC, |
| 913 | SourceLocation UsingLoc, |
| 914 | SourceRange TargetNNR, |
| 915 | NestedNameSpecifier *TargetNNS, |
| 916 | SourceLocation TargetNameLoc, |
| 917 | DeclarationName TargetName) { |
| 918 | return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc, |
| 919 | TargetNNR, TargetNNS, |
| 920 | TargetNameLoc, TargetName); |
| 921 | } |
| 922 | |
| 923 | UnresolvedUsingTypenameDecl * |
| 924 | UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC, |
| 925 | SourceLocation UsingLoc, |
| 926 | SourceLocation TypenameLoc, |
| 927 | SourceRange TargetNNR, |
| 928 | NestedNameSpecifier *TargetNNS, |
| 929 | SourceLocation TargetNameLoc, |
| 930 | DeclarationName TargetName) { |
| 931 | return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc, |
| 932 | TargetNNR, TargetNNS, |
| 933 | TargetNameLoc, |
| 934 | TargetName.getAsIdentifierInfo()); |
Anders Carlsson | 665b49c | 2009-08-28 05:30:28 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 937 | StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC, |
| 938 | SourceLocation L, Expr *AssertExpr, |
| 939 | StringLiteral *Message) { |
| 940 | return new (C) StaticAssertDecl(DC, L, AssertExpr, Message); |
| 941 | } |
| 942 | |
| 943 | void StaticAssertDecl::Destroy(ASTContext& C) { |
| 944 | AssertExpr->Destroy(C); |
| 945 | Message->Destroy(C); |
| 946 | this->~StaticAssertDecl(); |
| 947 | C.Deallocate((void *)this); |
| 948 | } |
| 949 | |
| 950 | StaticAssertDecl::~StaticAssertDecl() { |
| 951 | } |
| 952 | |
Anders Carlsson | 05bf2c7 | 2009-03-26 23:46:50 +0000 | [diff] [blame] | 953 | static const char *getAccessName(AccessSpecifier AS) { |
| 954 | switch (AS) { |
| 955 | default: |
| 956 | case AS_none: |
| 957 | assert("Invalid access specifier!"); |
| 958 | return 0; |
| 959 | case AS_public: |
| 960 | return "public"; |
| 961 | case AS_private: |
| 962 | return "private"; |
| 963 | case AS_protected: |
| 964 | return "protected"; |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, |
| 969 | AccessSpecifier AS) { |
| 970 | return DB << getAccessName(AS); |
| 971 | } |
| 972 | |
| 973 | |