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