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