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 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context, |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 168 | FoundTQs)) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 169 | if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) || |
| 170 | (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const))) |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 171 | return cast<CXXConstructorDecl>(*Con); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 172 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 173 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 174 | } |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 175 | return 0; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 178 | bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context, |
| 179 | const CXXMethodDecl *& MD) const { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 180 | QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType( |
| 181 | const_cast<CXXRecordDecl*>(this))); |
| 182 | DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 183 | |
| 184 | DeclContext::lookup_const_iterator Op, OpEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 185 | for (llvm::tie(Op, OpEnd) = this->lookup(OpName); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 186 | Op != OpEnd; ++Op) { |
| 187 | // C++ [class.copy]p9: |
| 188 | // A user-declared copy assignment operator is a non-static non-template |
| 189 | // member function of class X with exactly one parameter of type X, X&, |
| 190 | // const X&, volatile X& or const volatile X&. |
Douglas Gregor | 682054c | 2009-10-30 22:48:49 +0000 | [diff] [blame] | 191 | const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op); |
| 192 | if (!Method) |
| 193 | continue; |
| 194 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 195 | if (Method->isStatic()) |
| 196 | continue; |
Douglas Gregor | 77da3f4 | 2009-10-13 23:45:19 +0000 | [diff] [blame] | 197 | if (Method->getPrimaryTemplate()) |
| 198 | continue; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 199 | const FunctionProtoType *FnType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 200 | Method->getType()->getAs<FunctionProtoType>(); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 201 | assert(FnType && "Overloaded operator has no prototype."); |
| 202 | // Don't assert on this; an invalid decl might have been left in the AST. |
| 203 | if (FnType->getNumArgs() != 1 || FnType->isVariadic()) |
| 204 | continue; |
| 205 | bool AcceptsConst = true; |
| 206 | QualType ArgType = FnType->getArgType(0); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 207 | if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 208 | ArgType = Ref->getPointeeType(); |
Douglas Gregor | 2ff4478 | 2009-03-20 20:21:37 +0000 | [diff] [blame] | 209 | // Is it a non-const lvalue reference? |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 210 | if (!ArgType.isConstQualified()) |
| 211 | AcceptsConst = false; |
| 212 | } |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 213 | if (!Context.hasSameUnqualifiedType(ArgType, ClassType)) |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 214 | continue; |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 215 | MD = Method; |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 216 | // We have a single argument of type cv X or cv X&, i.e. we've found the |
| 217 | // copy assignment operator. Return whether it accepts const arguments. |
| 218 | return AcceptsConst; |
| 219 | } |
| 220 | assert(isInvalidDecl() && |
| 221 | "No copy assignment operator declared in valid code."); |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 226 | CXXRecordDecl::addedConstructor(ASTContext &Context, |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 227 | CXXConstructorDecl *ConDecl) { |
Fariborz Jahanian | 8bc3fa4 | 2009-06-17 22:44:31 +0000 | [diff] [blame] | 228 | assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl"); |
| 229 | // Note that we have a user-declared constructor. |
| 230 | UserDeclaredConstructor = true; |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 231 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | // C++ [dcl.init.aggr]p1: |
Fariborz Jahanian | 8bc3fa4 | 2009-06-17 22:44:31 +0000 | [diff] [blame] | 233 | // An aggregate is an array or a class (clause 9) with no |
| 234 | // user-declared constructors (12.1) [...]. |
| 235 | Aggregate = false; |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 236 | |
Fariborz Jahanian | 8bc3fa4 | 2009-06-17 22:44:31 +0000 | [diff] [blame] | 237 | // C++ [class]p4: |
| 238 | // A POD-struct is an aggregate class [...] |
| 239 | PlainOldData = false; |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 240 | |
Fariborz Jahanian | 8bc3fa4 | 2009-06-17 22:44:31 +0000 | [diff] [blame] | 241 | // C++ [class.ctor]p5: |
| 242 | // A constructor is trivial if it is an implicitly-declared default |
| 243 | // constructor. |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 244 | // FIXME: C++0x: don't do this for "= default" default constructors. |
Fariborz Jahanian | 8bc3fa4 | 2009-06-17 22:44:31 +0000 | [diff] [blame] | 245 | HasTrivialConstructor = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 246 | |
Fariborz Jahanian | 8bc3fa4 | 2009-06-17 22:44:31 +0000 | [diff] [blame] | 247 | // Note when we have a user-declared copy constructor, which will |
| 248 | // suppress the implicit declaration of a copy constructor. |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 249 | if (ConDecl->isCopyConstructor(Context)) { |
Fariborz Jahanian | 8bc3fa4 | 2009-06-17 22:44:31 +0000 | [diff] [blame] | 250 | UserDeclaredCopyConstructor = true; |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 251 | |
| 252 | // C++ [class.copy]p6: |
| 253 | // A copy constructor is trivial if it is implicitly declared. |
| 254 | // FIXME: C++0x: don't do this for "= default" copy constructors. |
| 255 | HasTrivialCopyConstructor = false; |
| 256 | } |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 259 | void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context, |
| 260 | CXXMethodDecl *OpDecl) { |
| 261 | // We're interested specifically in copy assignment operators. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 262 | const FunctionProtoType *FnType = OpDecl->getType()->getAs<FunctionProtoType>(); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 263 | assert(FnType && "Overloaded operator has no proto function type."); |
| 264 | assert(FnType->getNumArgs() == 1 && !FnType->isVariadic()); |
Douglas Gregor | 77da3f4 | 2009-10-13 23:45:19 +0000 | [diff] [blame] | 265 | |
| 266 | // Copy assignment operators must be non-templates. |
| 267 | if (OpDecl->getPrimaryTemplate() || OpDecl->getDescribedFunctionTemplate()) |
| 268 | return; |
| 269 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 270 | QualType ArgType = FnType->getArgType(0); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 271 | if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 272 | ArgType = Ref->getPointeeType(); |
| 273 | |
| 274 | ArgType = ArgType.getUnqualifiedType(); |
| 275 | QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType( |
| 276 | const_cast<CXXRecordDecl*>(this))); |
| 277 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 278 | if (!Context.hasSameUnqualifiedType(ClassType, ArgType)) |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 279 | return; |
| 280 | |
| 281 | // This is a copy assignment operator. |
Eli Friedman | 88fad63 | 2009-11-07 00:02:45 +0000 | [diff] [blame] | 282 | // Note on the decl that it is a copy assignment operator. |
| 283 | OpDecl->setCopyAssignment(true); |
| 284 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 285 | // Suppress the implicit declaration of a copy constructor. |
| 286 | UserDeclaredCopyAssignment = true; |
| 287 | |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 288 | // C++ [class.copy]p11: |
| 289 | // A copy assignment operator is trivial if it is implicitly declared. |
| 290 | // FIXME: C++0x: don't do this for "= default" copy operators. |
| 291 | HasTrivialCopyAssignment = false; |
| 292 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 293 | // C++ [class]p4: |
| 294 | // A POD-struct is an aggregate class that [...] has no user-defined copy |
| 295 | // assignment operator [...]. |
| 296 | PlainOldData = false; |
| 297 | } |
| 298 | |
Fariborz Jahanian | 0351a1e | 2009-10-07 20:43:36 +0000 | [diff] [blame] | 299 | void |
| 300 | CXXRecordDecl::collectConversionFunctions( |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 301 | llvm::SmallPtrSet<CanQualType, 8>& ConversionsTypeSet) const |
Fariborz Jahanian | f4e462c | 2009-10-12 18:36:50 +0000 | [diff] [blame] | 302 | { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 303 | const UnresolvedSet *Cs = getConversionFunctions(); |
| 304 | for (UnresolvedSet::iterator I = Cs->begin(), E = Cs->end(); I != E; ++I) { |
| 305 | NamedDecl *TopConv = *I; |
Fariborz Jahanian | f4e462c | 2009-10-12 18:36:50 +0000 | [diff] [blame] | 306 | CanQualType TConvType; |
Fariborz Jahanian | 0351a1e | 2009-10-07 20:43:36 +0000 | [diff] [blame] | 307 | if (FunctionTemplateDecl *TConversionTemplate = |
| 308 | dyn_cast<FunctionTemplateDecl>(TopConv)) |
| 309 | TConvType = |
| 310 | getASTContext().getCanonicalType( |
| 311 | TConversionTemplate->getTemplatedDecl()->getResultType()); |
| 312 | else |
| 313 | TConvType = |
| 314 | getASTContext().getCanonicalType( |
| 315 | cast<CXXConversionDecl>(TopConv)->getConversionType()); |
| 316 | ConversionsTypeSet.insert(TConvType); |
| 317 | } |
| 318 | } |
| 319 | |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 320 | /// getNestedVisibleConversionFunctions - imports unique conversion |
| 321 | /// functions from base classes into the visible conversion function |
| 322 | /// list of the class 'RD'. This is a private helper method. |
Fariborz Jahanian | 0351a1e | 2009-10-07 20:43:36 +0000 | [diff] [blame] | 323 | /// TopConversionsTypeSet is the set of conversion functions of the class |
| 324 | /// we are interested in. HiddenConversionTypes is set of conversion functions |
| 325 | /// of the immediate derived class which hides the conversion functions found |
| 326 | /// in current class. |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 327 | void |
Fariborz Jahanian | 0351a1e | 2009-10-07 20:43:36 +0000 | [diff] [blame] | 328 | CXXRecordDecl::getNestedVisibleConversionFunctions(CXXRecordDecl *RD, |
Fariborz Jahanian | f4e462c | 2009-10-12 18:36:50 +0000 | [diff] [blame] | 329 | const llvm::SmallPtrSet<CanQualType, 8> &TopConversionsTypeSet, |
| 330 | const llvm::SmallPtrSet<CanQualType, 8> &HiddenConversionTypes) |
| 331 | { |
Fariborz Jahanian | 0351a1e | 2009-10-07 20:43:36 +0000 | [diff] [blame] | 332 | bool inTopClass = (RD == this); |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 333 | QualType ClassType = getASTContext().getTypeDeclType(this); |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 334 | if (const RecordType *Record = ClassType->getAs<RecordType>()) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 335 | const UnresolvedSet *Cs |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 336 | = cast<CXXRecordDecl>(Record->getDecl())->getConversionFunctions(); |
Fariborz Jahanian | faebcbb | 2009-09-12 19:52:10 +0000 | [diff] [blame] | 337 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 338 | for (UnresolvedSet::iterator I = Cs->begin(), E = Cs->end(); I != E; ++I) { |
| 339 | NamedDecl *Conv = *I; |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 340 | // Only those conversions not exact match of conversions in current |
| 341 | // class are candidateconversion routines. |
Fariborz Jahanian | f4e462c | 2009-10-12 18:36:50 +0000 | [diff] [blame] | 342 | CanQualType ConvType; |
Fariborz Jahanian | faebcbb | 2009-09-12 19:52:10 +0000 | [diff] [blame] | 343 | if (FunctionTemplateDecl *ConversionTemplate = |
| 344 | dyn_cast<FunctionTemplateDecl>(Conv)) |
| 345 | ConvType = |
| 346 | getASTContext().getCanonicalType( |
Fariborz Jahanian | a5c1294 | 2009-09-15 23:02:16 +0000 | [diff] [blame] | 347 | ConversionTemplate->getTemplatedDecl()->getResultType()); |
Fariborz Jahanian | faebcbb | 2009-09-12 19:52:10 +0000 | [diff] [blame] | 348 | else |
| 349 | ConvType = |
Fariborz Jahanian | 8b915e7 | 2009-09-15 22:15:23 +0000 | [diff] [blame] | 350 | getASTContext().getCanonicalType( |
| 351 | cast<CXXConversionDecl>(Conv)->getConversionType()); |
Fariborz Jahanian | 0351a1e | 2009-10-07 20:43:36 +0000 | [diff] [blame] | 352 | // We only add conversion functions found in the base class if they |
| 353 | // are not hidden by those found in HiddenConversionTypes which are |
| 354 | // the conversion functions in its derived class. |
| 355 | if (inTopClass || |
| 356 | (!TopConversionsTypeSet.count(ConvType) && |
| 357 | !HiddenConversionTypes.count(ConvType)) ) { |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 358 | if (FunctionTemplateDecl *ConversionTemplate = |
| 359 | dyn_cast<FunctionTemplateDecl>(Conv)) |
| 360 | RD->addVisibleConversionFunction(ConversionTemplate); |
| 361 | else |
| 362 | RD->addVisibleConversionFunction(cast<CXXConversionDecl>(Conv)); |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | } |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 366 | |
Fariborz Jahanian | 12af63b | 2009-10-08 16:33:37 +0000 | [diff] [blame] | 367 | if (getNumBases() == 0 && getNumVBases() == 0) |
| 368 | return; |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 369 | |
Fariborz Jahanian | f4e462c | 2009-10-12 18:36:50 +0000 | [diff] [blame] | 370 | llvm::SmallPtrSet<CanQualType, 8> ConversionFunctions; |
Fariborz Jahanian | 12af63b | 2009-10-08 16:33:37 +0000 | [diff] [blame] | 371 | if (!inTopClass) |
| 372 | collectConversionFunctions(ConversionFunctions); |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 373 | |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 374 | for (CXXRecordDecl::base_class_iterator VBase = vbases_begin(), |
| 375 | E = vbases_end(); VBase != E; ++VBase) { |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 376 | if (const RecordType *RT = VBase->getType()->getAs<RecordType>()) { |
| 377 | CXXRecordDecl *VBaseClassDecl |
| 378 | = cast<CXXRecordDecl>(RT->getDecl()); |
| 379 | VBaseClassDecl->getNestedVisibleConversionFunctions(RD, |
| 380 | TopConversionsTypeSet, |
| 381 | (inTopClass ? TopConversionsTypeSet : ConversionFunctions)); |
| 382 | } |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 383 | } |
| 384 | for (CXXRecordDecl::base_class_iterator Base = bases_begin(), |
| 385 | E = bases_end(); Base != E; ++Base) { |
| 386 | if (Base->isVirtual()) |
| 387 | continue; |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 388 | if (const RecordType *RT = Base->getType()->getAs<RecordType>()) { |
| 389 | CXXRecordDecl *BaseClassDecl |
| 390 | = cast<CXXRecordDecl>(RT->getDecl()); |
| 391 | |
| 392 | BaseClassDecl->getNestedVisibleConversionFunctions(RD, |
| 393 | TopConversionsTypeSet, |
| 394 | (inTopClass ? TopConversionsTypeSet : ConversionFunctions)); |
| 395 | } |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 396 | } |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | /// getVisibleConversionFunctions - get all conversion functions visible |
| 400 | /// in current class; including conversion function templates. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 401 | const UnresolvedSet *CXXRecordDecl::getVisibleConversionFunctions() { |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 402 | // If root class, all conversions are visible. |
| 403 | if (bases_begin() == bases_end()) |
| 404 | return &Conversions; |
| 405 | // If visible conversion list is already evaluated, return it. |
| 406 | if (ComputedVisibleConversions) |
| 407 | return &VisibleConversions; |
Fariborz Jahanian | f4e462c | 2009-10-12 18:36:50 +0000 | [diff] [blame] | 408 | llvm::SmallPtrSet<CanQualType, 8> TopConversionsTypeSet; |
Fariborz Jahanian | 0351a1e | 2009-10-07 20:43:36 +0000 | [diff] [blame] | 409 | collectConversionFunctions(TopConversionsTypeSet); |
| 410 | getNestedVisibleConversionFunctions(this, TopConversionsTypeSet, |
| 411 | TopConversionsTypeSet); |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 412 | ComputedVisibleConversions = true; |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 413 | return &VisibleConversions; |
| 414 | } |
| 415 | |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 416 | void CXXRecordDecl::addVisibleConversionFunction( |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 417 | CXXConversionDecl *ConvDecl) { |
| 418 | assert(!ConvDecl->getDescribedFunctionTemplate() && |
| 419 | "Conversion function templates should cast to FunctionTemplateDecl."); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 420 | VisibleConversions.addDecl(ConvDecl); |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 423 | void CXXRecordDecl::addVisibleConversionFunction( |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 424 | FunctionTemplateDecl *ConvDecl) { |
| 425 | assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) && |
| 426 | "Function template is not a conversion function template"); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 427 | VisibleConversions.addDecl(ConvDecl); |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Fariborz Jahanian | debc629 | 2009-09-12 19:02:34 +0000 | [diff] [blame] | 430 | void CXXRecordDecl::addConversionFunction(CXXConversionDecl *ConvDecl) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 431 | assert(!ConvDecl->getDescribedFunctionTemplate() && |
| 432 | "Conversion function templates should cast to FunctionTemplateDecl."); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 433 | Conversions.addDecl(ConvDecl); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Fariborz Jahanian | debc629 | 2009-09-12 19:02:34 +0000 | [diff] [blame] | 436 | void CXXRecordDecl::addConversionFunction(FunctionTemplateDecl *ConvDecl) { |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 437 | assert(isa<CXXConversionDecl>(ConvDecl->getTemplatedDecl()) && |
| 438 | "Function template is not a conversion function template"); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 439 | Conversions.addDecl(ConvDecl); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 440 | } |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 441 | |
Fariborz Jahanian | e7184df | 2009-12-03 18:44:40 +0000 | [diff] [blame] | 442 | |
| 443 | void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) { |
| 444 | Method->setVirtualAsWritten(true); |
| 445 | setAggregate(false); |
| 446 | setPOD(false); |
| 447 | setEmpty(false); |
| 448 | setPolymorphic(true); |
| 449 | setHasTrivialConstructor(false); |
| 450 | setHasTrivialCopyConstructor(false); |
| 451 | setHasTrivialCopyAssignment(false); |
| 452 | } |
| 453 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 454 | CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 455 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 456 | return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom()); |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 461 | MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const { |
| 462 | return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>(); |
| 463 | } |
| 464 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 465 | void |
| 466 | CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD, |
| 467 | TemplateSpecializationKind TSK) { |
| 468 | assert(TemplateOrInstantiation.isNull() && |
| 469 | "Previous template or instantiation?"); |
| 470 | assert(!isa<ClassTemplateSpecializationDecl>(this)); |
| 471 | TemplateOrInstantiation |
| 472 | = new (getASTContext()) MemberSpecializationInfo(RD, TSK); |
| 473 | } |
| 474 | |
Anders Carlsson | b13e357 | 2009-12-07 06:33:48 +0000 | [diff] [blame] | 475 | TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{ |
| 476 | if (const ClassTemplateSpecializationDecl *Spec |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 477 | = dyn_cast<ClassTemplateSpecializationDecl>(this)) |
| 478 | return Spec->getSpecializationKind(); |
| 479 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 480 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 481 | return MSInfo->getTemplateSpecializationKind(); |
| 482 | |
| 483 | return TSK_Undeclared; |
| 484 | } |
| 485 | |
| 486 | void |
| 487 | CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) { |
| 488 | if (ClassTemplateSpecializationDecl *Spec |
| 489 | = dyn_cast<ClassTemplateSpecializationDecl>(this)) { |
| 490 | Spec->setSpecializationKind(TSK); |
| 491 | return; |
| 492 | } |
| 493 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 494 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 495 | MSInfo->setTemplateSpecializationKind(TSK); |
| 496 | return; |
| 497 | } |
| 498 | |
| 499 | assert(false && "Not a class template or member class specialization"); |
| 500 | } |
| 501 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 502 | CXXConstructorDecl * |
| 503 | CXXRecordDecl::getDefaultConstructor(ASTContext &Context) { |
| 504 | QualType ClassType = Context.getTypeDeclType(this); |
| 505 | DeclarationName ConstructorName |
| 506 | = Context.DeclarationNames.getCXXConstructorName( |
| 507 | Context.getCanonicalType(ClassType.getUnqualifiedType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 508 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 509 | DeclContext::lookup_const_iterator Con, ConEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 510 | for (llvm::tie(Con, ConEnd) = lookup(ConstructorName); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 511 | Con != ConEnd; ++Con) { |
Douglas Gregor | d93bacf | 2009-09-04 14:46:39 +0000 | [diff] [blame] | 512 | // FIXME: In C++0x, a constructor template can be a default constructor. |
| 513 | if (isa<FunctionTemplateDecl>(*Con)) |
| 514 | continue; |
| 515 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 516 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 517 | if (Constructor->isDefaultConstructor()) |
| 518 | return Constructor; |
| 519 | } |
| 520 | return 0; |
| 521 | } |
| 522 | |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 523 | CXXDestructorDecl *CXXRecordDecl::getDestructor(ASTContext &Context) { |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 524 | QualType ClassType = Context.getTypeDeclType(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 525 | |
| 526 | DeclarationName Name |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 527 | = Context.DeclarationNames.getCXXDestructorName( |
| 528 | Context.getCanonicalType(ClassType)); |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 529 | |
| 530 | DeclContext::lookup_iterator I, E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 531 | llvm::tie(I, E) = lookup(Name); |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 532 | assert(I != E && "Did not find a destructor!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 534 | CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I); |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 535 | assert(++I == E && "Found more than one destructor!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 536 | |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 537 | return Dtor; |
| 538 | } |
| 539 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 540 | CXXMethodDecl * |
| 541 | CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 542 | SourceLocation L, DeclarationName N, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 543 | QualType T, TypeSourceInfo *TInfo, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 544 | bool isStatic, bool isInline) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 545 | return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, TInfo, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 546 | isStatic, isInline); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 549 | bool CXXMethodDecl::isUsualDeallocationFunction() const { |
| 550 | if (getOverloadedOperator() != OO_Delete && |
| 551 | getOverloadedOperator() != OO_Array_Delete) |
| 552 | return false; |
| 553 | |
| 554 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 555 | // If a class T has a member deallocation function named operator delete |
| 556 | // with exactly one parameter, then that function is a usual (non-placement) |
| 557 | // deallocation function. [...] |
| 558 | if (getNumParams() == 1) |
| 559 | return true; |
| 560 | |
| 561 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 562 | // [...] If class T does not declare such an operator delete but does |
| 563 | // declare a member deallocation function named operator delete with |
| 564 | // exactly two parameters, the second of which has type std::size_t (18.1), |
| 565 | // then this function is a usual deallocation function. |
| 566 | ASTContext &Context = getASTContext(); |
| 567 | if (getNumParams() != 2 || |
| 568 | !Context.hasSameType(getParamDecl(1)->getType(), Context.getSizeType())) |
| 569 | return false; |
| 570 | |
| 571 | // This function is a usual deallocation function if there are no |
| 572 | // single-parameter deallocation functions of the same kind. |
| 573 | for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName()); |
| 574 | R.first != R.second; ++R.first) { |
| 575 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first)) |
| 576 | if (FD->getNumParams() == 1) |
| 577 | return false; |
| 578 | } |
| 579 | |
| 580 | return true; |
| 581 | } |
| 582 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 583 | typedef llvm::DenseMap<const CXXMethodDecl*, |
| 584 | std::vector<const CXXMethodDecl *> *> |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 585 | OverriddenMethodsMapTy; |
| 586 | |
Mike Stump | b9871a2 | 2009-08-21 01:45:00 +0000 | [diff] [blame] | 587 | // FIXME: We hate static data. This doesn't survive PCH saving/loading, and |
| 588 | // the vtable building code uses it at CG time. |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 589 | static OverriddenMethodsMapTy *OverriddenMethods = 0; |
| 590 | |
| 591 | void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) { |
Anders Carlsson | 3aaf486 | 2009-12-04 05:51:56 +0000 | [diff] [blame] | 592 | assert(MD->isCanonicalDecl() && "Method is not canonical!"); |
| 593 | |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 594 | // FIXME: The CXXMethodDecl dtor needs to remove and free the entry. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 595 | |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 596 | if (!OverriddenMethods) |
| 597 | OverriddenMethods = new OverriddenMethodsMapTy(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 598 | |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 599 | std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this]; |
| 600 | if (!Methods) |
| 601 | Methods = new std::vector<const CXXMethodDecl *>; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 602 | |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 603 | Methods->push_back(MD); |
| 604 | } |
| 605 | |
| 606 | CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const { |
| 607 | if (!OverriddenMethods) |
| 608 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 609 | |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 610 | OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this); |
Daniel Dunbar | 0908c33 | 2009-08-01 23:40:20 +0000 | [diff] [blame] | 611 | if (it == OverriddenMethods->end() || it->second->empty()) |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 612 | return 0; |
Daniel Dunbar | 0908c33 | 2009-08-01 23:40:20 +0000 | [diff] [blame] | 613 | |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 614 | return &(*it->second)[0]; |
| 615 | } |
| 616 | |
| 617 | CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const { |
| 618 | if (!OverriddenMethods) |
| 619 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 620 | |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 621 | OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this); |
Daniel Dunbar | 0908c33 | 2009-08-01 23:40:20 +0000 | [diff] [blame] | 622 | if (it == OverriddenMethods->end() || it->second->empty()) |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 623 | return 0; |
| 624 | |
Daniel Dunbar | 637ec32 | 2009-08-02 01:48:29 +0000 | [diff] [blame] | 625 | return &(*it->second)[0] + it->second->size(); |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 628 | QualType CXXMethodDecl::getThisType(ASTContext &C) const { |
Argyrios Kyrtzidis | b0d178d | 2008-10-24 22:28:18 +0000 | [diff] [blame] | 629 | // C++ 9.3.2p1: The type of this in a member function of a class X is X*. |
| 630 | // If the member function is declared const, the type of this is const X*, |
| 631 | // if the member function is declared volatile, the type of this is |
| 632 | // volatile X*, and if the member function is declared const volatile, |
| 633 | // the type of this is const volatile X*. |
| 634 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 635 | assert(isInstance() && "No 'this' for static methods!"); |
Anders Carlsson | 31a0875 | 2009-06-13 02:59:33 +0000 | [diff] [blame] | 636 | |
| 637 | QualType ClassTy; |
| 638 | if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate()) |
| 639 | ClassTy = TD->getInjectedClassNameType(C); |
| 640 | else |
Mike Stump | e607ed0 | 2009-08-07 18:05:12 +0000 | [diff] [blame] | 641 | ClassTy = C.getTagDeclType(getParent()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 642 | ClassTy = C.getQualifiedType(ClassTy, |
| 643 | Qualifiers::fromCVRMask(getTypeQualifiers())); |
Anders Carlsson | 4e57992 | 2009-07-10 21:35:09 +0000 | [diff] [blame] | 644 | return C.getPointerType(ClassTy); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Eli Friedman | d7d7f67 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 647 | static bool MethodHasBody(const CXXMethodDecl *MD, const FunctionDecl *&fn) { |
| 648 | // Simple case: function has a body |
| 649 | if (MD->getBody(fn)) |
| 650 | return true; |
| 651 | |
| 652 | // Complex case: function is an instantiation of a function which has a |
| 653 | // body, but the definition hasn't been instantiated. |
| 654 | const FunctionDecl *PatternDecl = MD->getTemplateInstantiationPattern(); |
| 655 | if (PatternDecl && PatternDecl->getBody(fn)) |
| 656 | return true; |
| 657 | |
| 658 | return false; |
| 659 | } |
| 660 | |
| 661 | bool CXXMethodDecl::hasInlineBody() const { |
| 662 | const FunctionDecl *fn; |
| 663 | return MethodHasBody(this, fn) && !fn->isOutOfLine(); |
| 664 | } |
| 665 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 666 | CXXBaseOrMemberInitializer:: |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 667 | CXXBaseOrMemberInitializer(ASTContext &Context, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 668 | TypeSourceInfo *TInfo, CXXConstructorDecl *C, |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 669 | SourceLocation L, |
| 670 | Expr **Args, unsigned NumArgs, |
| 671 | SourceLocation R) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 672 | : BaseOrMember(TInfo), Args(0), NumArgs(0), CtorOrAnonUnion(C), |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 673 | LParenLoc(L), RParenLoc(R) |
| 674 | { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 675 | if (NumArgs > 0) { |
| 676 | this->NumArgs = NumArgs; |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 677 | this->Args = new (Context) Stmt*[NumArgs]; |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 678 | for (unsigned Idx = 0; Idx < NumArgs; ++Idx) |
| 679 | this->Args[Idx] = Args[Idx]; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | CXXBaseOrMemberInitializer:: |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 684 | CXXBaseOrMemberInitializer(ASTContext &Context, |
| 685 | FieldDecl *Member, SourceLocation MemberLoc, |
| 686 | CXXConstructorDecl *C, SourceLocation L, |
| 687 | Expr **Args, unsigned NumArgs, |
| 688 | SourceLocation R) |
| 689 | : BaseOrMember(Member), MemberLocation(MemberLoc), Args(0), NumArgs(0), |
| 690 | CtorOrAnonUnion(C), LParenLoc(L), RParenLoc(R) |
| 691 | { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 692 | if (NumArgs > 0) { |
| 693 | this->NumArgs = NumArgs; |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 694 | this->Args = new (Context) Stmt*[NumArgs]; |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 695 | for (unsigned Idx = 0; Idx < NumArgs; ++Idx) |
| 696 | this->Args[Idx] = Args[Idx]; |
| 697 | } |
| 698 | } |
| 699 | |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 700 | void CXXBaseOrMemberInitializer::Destroy(ASTContext &Context) { |
| 701 | for (unsigned I = 0; I != NumArgs; ++I) |
| 702 | Args[I]->Destroy(Context); |
| 703 | Context.Deallocate(Args); |
| 704 | this->~CXXBaseOrMemberInitializer(); |
| 705 | } |
| 706 | |
| 707 | TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const { |
| 708 | if (isBaseInitializer()) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 709 | return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc(); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 710 | else |
| 711 | return TypeLoc(); |
| 712 | } |
| 713 | |
| 714 | Type *CXXBaseOrMemberInitializer::getBaseClass() { |
| 715 | if (isBaseInitializer()) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 716 | return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr(); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 717 | else |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | const Type *CXXBaseOrMemberInitializer::getBaseClass() const { |
| 722 | if (isBaseInitializer()) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 723 | return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr(); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 724 | else |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const { |
| 729 | if (isMemberInitializer()) |
| 730 | return getMemberLocation(); |
| 731 | |
| 732 | return getBaseClassLoc().getSourceRange().getBegin(); |
| 733 | } |
| 734 | |
| 735 | SourceRange CXXBaseOrMemberInitializer::getSourceRange() const { |
| 736 | return SourceRange(getSourceLocation(), getRParenLoc()); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 739 | CXXConstructorDecl * |
| 740 | CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 741 | SourceLocation L, DeclarationName N, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 742 | QualType T, TypeSourceInfo *TInfo, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 743 | bool isExplicit, |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 744 | bool isInline, bool isImplicitlyDeclared) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 745 | assert(N.getNameKind() == DeclarationName::CXXConstructorName && |
| 746 | "Name must refer to a constructor"); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 747 | return new (C) CXXConstructorDecl(RD, L, N, T, TInfo, isExplicit, isInline, |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 748 | isImplicitlyDeclared); |
| 749 | } |
| 750 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 751 | bool CXXConstructorDecl::isDefaultConstructor() const { |
| 752 | // C++ [class.ctor]p5: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 753 | // A default constructor for a class X is a constructor of class |
| 754 | // X that can be called without an argument. |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 755 | return (getNumParams() == 0) || |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 756 | (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg()); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 759 | bool |
| 760 | CXXConstructorDecl::isCopyConstructor(ASTContext &Context, |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 761 | unsigned &TypeQuals) const { |
| 762 | // C++ [class.copy]p2: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 763 | // A non-template constructor for class X is a copy constructor |
| 764 | // if its first parameter is of type X&, const X&, volatile X& or |
| 765 | // const volatile X&, and either there are no other parameters |
| 766 | // or else all other parameters have default arguments (8.3.6). |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 767 | if ((getNumParams() < 1) || |
Douglas Gregor | 77da3f4 | 2009-10-13 23:45:19 +0000 | [diff] [blame] | 768 | (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 769 | (getPrimaryTemplate() != 0) || |
Douglas Gregor | 77da3f4 | 2009-10-13 23:45:19 +0000 | [diff] [blame] | 770 | (getDescribedFunctionTemplate() != 0)) |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 771 | return false; |
| 772 | |
| 773 | const ParmVarDecl *Param = getParamDecl(0); |
| 774 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 775 | // Do we have a reference type? Rvalue references don't count. |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 776 | const LValueReferenceType *ParamRefType = |
| 777 | Param->getType()->getAs<LValueReferenceType>(); |
| 778 | if (!ParamRefType) |
| 779 | return false; |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 780 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 781 | // Is it a reference to our class type? |
| 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 | |