Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1 | //===--- DeclCXX.cpp - C++ Declaration AST Node Implementation ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the C++ related Decl classes. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 15 | #include "clang/AST/DeclTemplate.h" |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
Douglas Gregor | 7d7e672 | 2008-11-12 23:21:09 +0000 | [diff] [blame] | 18 | #include "clang/Basic/IdentifierTable.h" |
Douglas Gregor | fdfab6b | 2008-12-23 21:31:30 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | // Decl Allocation/Deallocation Method Implementations |
| 24 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 25 | |
Douglas Gregor | 3e00bad | 2009-02-17 01:05:43 +0000 | [diff] [blame] | 26 | CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC, |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 27 | SourceLocation L, IdentifierInfo *Id, |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 28 | CXXRecordDecl *PrevDecl, |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 29 | SourceLocation TKL) |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 30 | : RecordDecl(K, TK, DC, L, Id, PrevDecl, TKL), |
Douglas Gregor | 7d7e672 | 2008-11-12 23:21:09 +0000 | [diff] [blame] | 31 | UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false), |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 32 | UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false), |
Anders Carlsson | 67e4dd2 | 2009-03-22 01:52:17 +0000 | [diff] [blame] | 33 | Aggregate(true), PlainOldData(true), Polymorphic(false), Abstract(false), |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 34 | HasTrivialConstructor(true), HasTrivialCopyConstructor(true), |
| 35 | HasTrivialCopyAssignment(true), HasTrivialDestructor(true), |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 36 | Bases(0), NumBases(0), VBases(0), NumVBases(0), |
| 37 | Conversions(DC, DeclarationName()), |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 38 | TemplateOrInstantiation() { } |
Douglas Gregor | 7d7e672 | 2008-11-12 23:21:09 +0000 | [diff] [blame] | 39 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 40 | CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, |
| 41 | SourceLocation L, IdentifierInfo *Id, |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 42 | SourceLocation TKL, |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 43 | CXXRecordDecl* PrevDecl, |
| 44 | bool DelayTypeCreation) { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 45 | CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id, |
| 46 | PrevDecl, TKL); |
| 47 | |
| 48 | // FIXME: DelayTypeCreation seems like such a hack |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 49 | if (!DelayTypeCreation) |
| 50 | C.getTypeDeclType(R, PrevDecl); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 51 | return R; |
| 52 | } |
| 53 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 54 | CXXRecordDecl::~CXXRecordDecl() { |
Fariborz Jahanian | 5ffcd7b | 2009-07-02 18:26:15 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void CXXRecordDecl::Destroy(ASTContext &C) { |
| 58 | C.Deallocate(Bases); |
Fariborz Jahanian | 71c6e71 | 2009-07-22 17:41:53 +0000 | [diff] [blame] | 59 | C.Deallocate(VBases); |
Fariborz Jahanian | 5ffcd7b | 2009-07-02 18:26:15 +0000 | [diff] [blame] | 60 | this->RecordDecl::Destroy(C); |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 63 | void |
Fariborz Jahanian | 5ffcd7b | 2009-07-02 18:26:15 +0000 | [diff] [blame] | 64 | CXXRecordDecl::setBases(ASTContext &C, |
| 65 | CXXBaseSpecifier const * const *Bases, |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 66 | unsigned NumBases) { |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 67 | // C++ [dcl.init.aggr]p1: |
| 68 | // An aggregate is an array or a class (clause 9) with [...] |
| 69 | // no base classes [...]. |
| 70 | Aggregate = false; |
| 71 | |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 72 | if (this->Bases) |
Fariborz Jahanian | 5ffcd7b | 2009-07-02 18:26:15 +0000 | [diff] [blame] | 73 | C.Deallocate(this->Bases); |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 74 | |
| 75 | int vbaseCount = 0; |
| 76 | llvm::SmallVector<const CXXBaseSpecifier*, 8> UniqueVbases; |
| 77 | bool hasDirectVirtualBase = false; |
| 78 | |
Fariborz Jahanian | 5ffcd7b | 2009-07-02 18:26:15 +0000 | [diff] [blame] | 79 | this->Bases = new(C) CXXBaseSpecifier [NumBases]; |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 80 | this->NumBases = NumBases; |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 81 | for (unsigned i = 0; i < NumBases; ++i) { |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 82 | this->Bases[i] = *Bases[i]; |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 83 | // Keep track of inherited vbases for this base class. |
| 84 | const CXXBaseSpecifier *Base = Bases[i]; |
| 85 | QualType BaseType = Base->getType(); |
| 86 | // Skip template types. |
| 87 | // FIXME. This means that this list must be rebuilt during template |
| 88 | // instantiation. |
| 89 | if (BaseType->isDependentType()) |
| 90 | continue; |
| 91 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 92 | = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 93 | if (Base->isVirtual()) |
| 94 | hasDirectVirtualBase = true; |
| 95 | for (CXXRecordDecl::base_class_iterator VBase = |
| 96 | BaseClassDecl->vbases_begin(), |
| 97 | E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) { |
| 98 | // Add this vbase to the array of vbases for current class if it is |
| 99 | // not already in the list. |
| 100 | // FIXME. Note that we do a linear search as number of such classes are |
| 101 | // very few. |
| 102 | int i; |
| 103 | for (i = 0; i < vbaseCount; ++i) |
| 104 | if (UniqueVbases[i]->getType() == VBase->getType()) |
| 105 | break; |
| 106 | if (i == vbaseCount) { |
| 107 | UniqueVbases.push_back(VBase); |
| 108 | ++vbaseCount; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | if (hasDirectVirtualBase) { |
| 113 | // Iterate one more time through the direct bases and add the virtual |
| 114 | // base to the list of vritual bases for current class. |
| 115 | for (unsigned i = 0; i < NumBases; ++i) { |
| 116 | const CXXBaseSpecifier *VBase = Bases[i]; |
| 117 | if (!VBase->isVirtual()) |
| 118 | continue; |
Alisdair Meredith | 002b91f | 2009-07-11 14:32:10 +0000 | [diff] [blame] | 119 | int j; |
| 120 | for (j = 0; j < vbaseCount; ++j) |
| 121 | if (UniqueVbases[j]->getType() == VBase->getType()) |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 122 | break; |
Alisdair Meredith | 002b91f | 2009-07-11 14:32:10 +0000 | [diff] [blame] | 123 | if (j == vbaseCount) { |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 124 | UniqueVbases.push_back(VBase); |
| 125 | ++vbaseCount; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | if (vbaseCount > 0) { |
| 130 | // build AST for inhireted, direct or indirect, virtual bases. |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 131 | this->VBases = new (C) CXXBaseSpecifier [vbaseCount]; |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 132 | this->NumVBases = vbaseCount; |
| 133 | for (int i = 0; i < vbaseCount; i++) { |
| 134 | QualType QT = UniqueVbases[i]->getType(); |
| 135 | CXXRecordDecl *VBaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 136 | = cast<CXXRecordDecl>(QT->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 137 | this->VBases[i] = |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 138 | CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true, |
| 139 | VBaseClassDecl->getTagKind() == RecordDecl::TK_class, |
| 140 | UniqueVbases[i]->getAccessSpecifier(), QT); |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 145 | bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 146 | return getCopyConstructor(Context, QualType::Const) != 0; |
| 147 | } |
| 148 | |
| 149 | CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context, |
| 150 | unsigned TypeQuals) const{ |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 151 | QualType ClassType |
| 152 | = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this)); |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 153 | DeclarationName ConstructorName |
| 154 | = Context.DeclarationNames.getCXXConstructorName( |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 155 | Context.getCanonicalType(ClassType)); |
| 156 | unsigned FoundTQs; |
Douglas Gregor | fdfab6b | 2008-12-23 21:31:30 +0000 | [diff] [blame] | 157 | DeclContext::lookup_const_iterator Con, ConEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 158 | for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName); |
Douglas Gregor | fdfab6b | 2008-12-23 21:31:30 +0000 | [diff] [blame] | 159 | Con != ConEnd; ++Con) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 160 | if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(Context, |
| 161 | FoundTQs)) { |
| 162 | if (((TypeQuals & QualType::Const) == (FoundTQs & QualType::Const)) || |
| 163 | (!(TypeQuals & QualType::Const) && (FoundTQs & QualType::Const))) |
| 164 | return cast<CXXConstructorDecl>(*Con); |
| 165 | |
| 166 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 167 | } |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 168 | return 0; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 171 | bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context) const { |
| 172 | QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType( |
| 173 | const_cast<CXXRecordDecl*>(this))); |
| 174 | DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 175 | |
| 176 | DeclContext::lookup_const_iterator Op, OpEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 177 | for (llvm::tie(Op, OpEnd) = this->lookup(OpName); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 178 | Op != OpEnd; ++Op) { |
| 179 | // C++ [class.copy]p9: |
| 180 | // A user-declared copy assignment operator is a non-static non-template |
| 181 | // member function of class X with exactly one parameter of type X, X&, |
| 182 | // const X&, volatile X& or const volatile X&. |
| 183 | const CXXMethodDecl* Method = cast<CXXMethodDecl>(*Op); |
| 184 | if (Method->isStatic()) |
| 185 | continue; |
| 186 | // TODO: Skip templates? Or is this implicitly done due to parameter types? |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 187 | const FunctionProtoType *FnType = |
| 188 | Method->getType()->getAsFunctionProtoType(); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 189 | assert(FnType && "Overloaded operator has no prototype."); |
| 190 | // Don't assert on this; an invalid decl might have been left in the AST. |
| 191 | if (FnType->getNumArgs() != 1 || FnType->isVariadic()) |
| 192 | continue; |
| 193 | bool AcceptsConst = true; |
| 194 | QualType ArgType = FnType->getArgType(0); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 195 | if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 196 | ArgType = Ref->getPointeeType(); |
Douglas Gregor | 2ff4478 | 2009-03-20 20:21:37 +0000 | [diff] [blame] | 197 | // Is it a non-const lvalue reference? |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 198 | if (!ArgType.isConstQualified()) |
| 199 | AcceptsConst = false; |
| 200 | } |
| 201 | if (Context.getCanonicalType(ArgType).getUnqualifiedType() != ClassType) |
| 202 | continue; |
| 203 | |
| 204 | // We have a single argument of type cv X or cv X&, i.e. we've found the |
| 205 | // copy assignment operator. Return whether it accepts const arguments. |
| 206 | return AcceptsConst; |
| 207 | } |
| 208 | assert(isInvalidDecl() && |
| 209 | "No copy assignment operator declared in valid code."); |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | void |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 214 | CXXRecordDecl::addedConstructor(ASTContext &Context, |
| 215 | CXXConstructorDecl *ConDecl) { |
Fariborz Jahanian | 8bc3fa4 | 2009-06-17 22:44:31 +0000 | [diff] [blame] | 216 | assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl"); |
| 217 | // Note that we have a user-declared constructor. |
| 218 | UserDeclaredConstructor = true; |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 219 | |
Fariborz Jahanian | 8bc3fa4 | 2009-06-17 22:44:31 +0000 | [diff] [blame] | 220 | // C++ [dcl.init.aggr]p1: |
| 221 | // An aggregate is an array or a class (clause 9) with no |
| 222 | // user-declared constructors (12.1) [...]. |
| 223 | Aggregate = false; |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 224 | |
Fariborz Jahanian | 8bc3fa4 | 2009-06-17 22:44:31 +0000 | [diff] [blame] | 225 | // C++ [class]p4: |
| 226 | // A POD-struct is an aggregate class [...] |
| 227 | PlainOldData = false; |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 228 | |
Fariborz Jahanian | 8bc3fa4 | 2009-06-17 22:44:31 +0000 | [diff] [blame] | 229 | // C++ [class.ctor]p5: |
| 230 | // A constructor is trivial if it is an implicitly-declared default |
| 231 | // constructor. |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 232 | // FIXME: C++0x: don't do this for "= default" default constructors. |
Fariborz Jahanian | 8bc3fa4 | 2009-06-17 22:44:31 +0000 | [diff] [blame] | 233 | HasTrivialConstructor = false; |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 234 | |
Fariborz Jahanian | 8bc3fa4 | 2009-06-17 22:44:31 +0000 | [diff] [blame] | 235 | // Note when we have a user-declared copy constructor, which will |
| 236 | // suppress the implicit declaration of a copy constructor. |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 237 | if (ConDecl->isCopyConstructor(Context)) { |
Fariborz Jahanian | 8bc3fa4 | 2009-06-17 22:44:31 +0000 | [diff] [blame] | 238 | UserDeclaredCopyConstructor = true; |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 239 | |
| 240 | // C++ [class.copy]p6: |
| 241 | // A copy constructor is trivial if it is implicitly declared. |
| 242 | // FIXME: C++0x: don't do this for "= default" copy constructors. |
| 243 | HasTrivialCopyConstructor = false; |
| 244 | } |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 247 | void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context, |
| 248 | CXXMethodDecl *OpDecl) { |
| 249 | // We're interested specifically in copy assignment operators. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 250 | const FunctionProtoType *FnType = OpDecl->getType()->getAsFunctionProtoType(); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 251 | assert(FnType && "Overloaded operator has no proto function type."); |
| 252 | assert(FnType->getNumArgs() == 1 && !FnType->isVariadic()); |
| 253 | QualType ArgType = FnType->getArgType(0); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 254 | if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 255 | ArgType = Ref->getPointeeType(); |
| 256 | |
| 257 | ArgType = ArgType.getUnqualifiedType(); |
| 258 | QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType( |
| 259 | const_cast<CXXRecordDecl*>(this))); |
| 260 | |
| 261 | if (ClassType != Context.getCanonicalType(ArgType)) |
| 262 | return; |
| 263 | |
| 264 | // This is a copy assignment operator. |
| 265 | // Suppress the implicit declaration of a copy constructor. |
| 266 | UserDeclaredCopyAssignment = true; |
| 267 | |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 268 | // C++ [class.copy]p11: |
| 269 | // A copy assignment operator is trivial if it is implicitly declared. |
| 270 | // FIXME: C++0x: don't do this for "= default" copy operators. |
| 271 | HasTrivialCopyAssignment = false; |
| 272 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 273 | // C++ [class]p4: |
| 274 | // A POD-struct is an aggregate class that [...] has no user-defined copy |
| 275 | // assignment operator [...]. |
| 276 | PlainOldData = false; |
| 277 | } |
| 278 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 279 | void CXXRecordDecl::addConversionFunction(ASTContext &Context, |
| 280 | CXXConversionDecl *ConvDecl) { |
| 281 | Conversions.addOverload(ConvDecl); |
| 282 | } |
| 283 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 284 | |
| 285 | CXXConstructorDecl * |
| 286 | CXXRecordDecl::getDefaultConstructor(ASTContext &Context) { |
| 287 | QualType ClassType = Context.getTypeDeclType(this); |
| 288 | DeclarationName ConstructorName |
| 289 | = Context.DeclarationNames.getCXXConstructorName( |
| 290 | Context.getCanonicalType(ClassType.getUnqualifiedType())); |
| 291 | |
| 292 | DeclContext::lookup_const_iterator Con, ConEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 293 | for (llvm::tie(Con, ConEnd) = lookup(ConstructorName); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 294 | Con != ConEnd; ++Con) { |
| 295 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 296 | if (Constructor->isDefaultConstructor()) |
| 297 | return Constructor; |
| 298 | } |
| 299 | return 0; |
| 300 | } |
| 301 | |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 302 | const CXXDestructorDecl * |
| 303 | CXXRecordDecl::getDestructor(ASTContext &Context) { |
| 304 | QualType ClassType = Context.getTypeDeclType(this); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 305 | |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 306 | DeclarationName Name |
| 307 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
| 308 | |
| 309 | DeclContext::lookup_iterator I, E; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 310 | llvm::tie(I, E) = lookup(Name); |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 311 | assert(I != E && "Did not find a destructor!"); |
| 312 | |
| 313 | const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I); |
| 314 | assert(++I == E && "Found more than one destructor!"); |
| 315 | |
| 316 | return Dtor; |
| 317 | } |
| 318 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 319 | CXXMethodDecl * |
| 320 | CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 321 | SourceLocation L, DeclarationName N, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 322 | QualType T, bool isStatic, bool isInline) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 323 | return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, isStatic, isInline); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 326 | |
| 327 | typedef llvm::DenseMap<const CXXMethodDecl*, |
| 328 | std::vector<const CXXMethodDecl *> *> |
| 329 | OverriddenMethodsMapTy; |
| 330 | |
| 331 | static OverriddenMethodsMapTy *OverriddenMethods = 0; |
| 332 | |
| 333 | void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) { |
| 334 | // FIXME: The CXXMethodDecl dtor needs to remove and free the entry. |
| 335 | |
| 336 | if (!OverriddenMethods) |
| 337 | OverriddenMethods = new OverriddenMethodsMapTy(); |
| 338 | |
| 339 | std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this]; |
| 340 | if (!Methods) |
| 341 | Methods = new std::vector<const CXXMethodDecl *>; |
| 342 | |
| 343 | Methods->push_back(MD); |
| 344 | } |
| 345 | |
| 346 | CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const { |
| 347 | if (!OverriddenMethods) |
| 348 | return 0; |
| 349 | |
| 350 | OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this); |
| 351 | if (it == OverriddenMethods->end()) |
| 352 | return 0; |
| 353 | return &(*it->second)[0]; |
| 354 | } |
| 355 | |
| 356 | CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const { |
| 357 | if (!OverriddenMethods) |
| 358 | return 0; |
| 359 | |
| 360 | OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this); |
| 361 | if (it == OverriddenMethods->end()) |
| 362 | return 0; |
| 363 | |
| 364 | return &(*it->second)[it->second->size()]; |
| 365 | } |
| 366 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 367 | QualType CXXMethodDecl::getThisType(ASTContext &C) const { |
Argyrios Kyrtzidis | b0d178d | 2008-10-24 22:28:18 +0000 | [diff] [blame] | 368 | // C++ 9.3.2p1: The type of this in a member function of a class X is X*. |
| 369 | // If the member function is declared const, the type of this is const X*, |
| 370 | // if the member function is declared volatile, the type of this is |
| 371 | // volatile X*, and if the member function is declared const volatile, |
| 372 | // the type of this is const volatile X*. |
| 373 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 374 | assert(isInstance() && "No 'this' for static methods!"); |
Anders Carlsson | 31a0875 | 2009-06-13 02:59:33 +0000 | [diff] [blame] | 375 | |
| 376 | QualType ClassTy; |
| 377 | if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate()) |
| 378 | ClassTy = TD->getInjectedClassNameType(C); |
| 379 | else |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame^] | 380 | // FIXME: What is the design on getTagDeclType when it requires casting |
| 381 | // away const? mutable? |
Anders Carlsson | 31a0875 | 2009-06-13 02:59:33 +0000 | [diff] [blame] | 382 | ClassTy = C.getTagDeclType(const_cast<CXXRecordDecl*>(getParent())); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 383 | ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers()); |
Anders Carlsson | 4e57992 | 2009-07-10 21:35:09 +0000 | [diff] [blame] | 384 | return C.getPointerType(ClassTy); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 387 | CXXBaseOrMemberInitializer:: |
Fariborz Jahanian | 47deacf | 2009-06-30 00:02:17 +0000 | [diff] [blame] | 388 | CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs, |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 389 | CXXConstructorDecl *C, |
Fariborz Jahanian | 47deacf | 2009-06-30 00:02:17 +0000 | [diff] [blame] | 390 | SourceLocation L) |
| 391 | : Args(0), NumArgs(0), IdLoc(L) { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 392 | BaseOrMember = reinterpret_cast<uintptr_t>(BaseType.getTypePtr()); |
| 393 | assert((BaseOrMember & 0x01) == 0 && "Invalid base class type pointer"); |
| 394 | BaseOrMember |= 0x01; |
| 395 | |
| 396 | if (NumArgs > 0) { |
| 397 | this->NumArgs = NumArgs; |
Fariborz Jahanian | 50b8eea | 2009-07-24 17:57:02 +0000 | [diff] [blame] | 398 | // FIXME. Allocation via Context |
| 399 | this->Args = new Stmt*[NumArgs]; |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 400 | for (unsigned Idx = 0; Idx < NumArgs; ++Idx) |
| 401 | this->Args[Idx] = Args[Idx]; |
| 402 | } |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 403 | CtorToCall = C; |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | CXXBaseOrMemberInitializer:: |
Fariborz Jahanian | 47deacf | 2009-06-30 00:02:17 +0000 | [diff] [blame] | 407 | CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs, |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 408 | CXXConstructorDecl *C, |
Fariborz Jahanian | 47deacf | 2009-06-30 00:02:17 +0000 | [diff] [blame] | 409 | SourceLocation L) |
| 410 | : Args(0), NumArgs(0), IdLoc(L) { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 411 | BaseOrMember = reinterpret_cast<uintptr_t>(Member); |
| 412 | assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer"); |
| 413 | |
| 414 | if (NumArgs > 0) { |
| 415 | this->NumArgs = NumArgs; |
Fariborz Jahanian | 50b8eea | 2009-07-24 17:57:02 +0000 | [diff] [blame] | 416 | this->Args = new Stmt*[NumArgs]; |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 417 | for (unsigned Idx = 0; Idx < NumArgs; ++Idx) |
| 418 | this->Args[Idx] = Args[Idx]; |
| 419 | } |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 420 | CtorToCall = C; |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() { |
| 424 | delete [] Args; |
| 425 | } |
| 426 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 427 | CXXConstructorDecl * |
| 428 | CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 429 | SourceLocation L, DeclarationName N, |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 430 | QualType T, bool isExplicit, |
| 431 | bool isInline, bool isImplicitlyDeclared) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 432 | assert(N.getNameKind() == DeclarationName::CXXConstructorName && |
| 433 | "Name must refer to a constructor"); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 434 | return new (C) CXXConstructorDecl(RD, L, N, T, isExplicit, isInline, |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 435 | isImplicitlyDeclared); |
| 436 | } |
| 437 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 438 | bool CXXConstructorDecl::isDefaultConstructor() const { |
| 439 | // C++ [class.ctor]p5: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 440 | // A default constructor for a class X is a constructor of class |
| 441 | // X that can be called without an argument. |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 442 | return (getNumParams() == 0) || |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 443 | (getNumParams() > 0 && getParamDecl(0)->getDefaultArg() != 0); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | bool |
| 447 | CXXConstructorDecl::isCopyConstructor(ASTContext &Context, |
| 448 | unsigned &TypeQuals) const { |
| 449 | // C++ [class.copy]p2: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 450 | // A non-template constructor for class X is a copy constructor |
| 451 | // if its first parameter is of type X&, const X&, volatile X& or |
| 452 | // const volatile X&, and either there are no other parameters |
| 453 | // or else all other parameters have default arguments (8.3.6). |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 454 | if ((getNumParams() < 1) || |
Anders Carlsson | ae0b4e7 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 455 | (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg())) |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 456 | return false; |
| 457 | |
| 458 | const ParmVarDecl *Param = getParamDecl(0); |
| 459 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 460 | // Do we have a reference type? Rvalue references don't count. |
| 461 | const LValueReferenceType *ParamRefType = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 462 | Param->getType()->getAs<LValueReferenceType>(); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 463 | if (!ParamRefType) |
| 464 | return false; |
| 465 | |
| 466 | // Is it a reference to our class type? |
| 467 | QualType PointeeType |
| 468 | = Context.getCanonicalType(ParamRefType->getPointeeType()); |
| 469 | QualType ClassTy |
| 470 | = Context.getTagDeclType(const_cast<CXXRecordDecl*>(getParent())); |
| 471 | if (PointeeType.getUnqualifiedType() != ClassTy) |
| 472 | return false; |
| 473 | |
| 474 | // We have a copy constructor. |
| 475 | TypeQuals = PointeeType.getCVRQualifiers(); |
| 476 | return true; |
| 477 | } |
| 478 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 479 | bool CXXConstructorDecl::isConvertingConstructor() const { |
| 480 | // C++ [class.conv.ctor]p1: |
| 481 | // A constructor declared without the function-specifier explicit |
| 482 | // that can be called with a single parameter specifies a |
| 483 | // conversion from the type of its first parameter to the type of |
| 484 | // its class. Such a constructor is called a converting |
| 485 | // constructor. |
| 486 | if (isExplicit()) |
| 487 | return false; |
| 488 | |
| 489 | return (getNumParams() == 0 && |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 490 | getType()->getAsFunctionProtoType()->isVariadic()) || |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 491 | (getNumParams() == 1) || |
Anders Carlsson | ae0b4e7 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 492 | (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg()); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 493 | } |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 494 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 495 | CXXDestructorDecl * |
| 496 | CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 497 | SourceLocation L, DeclarationName N, |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 498 | QualType T, bool isInline, |
| 499 | bool isImplicitlyDeclared) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 500 | assert(N.getNameKind() == DeclarationName::CXXDestructorName && |
| 501 | "Name must refer to a destructor"); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 502 | return new (C) CXXDestructorDecl(RD, L, N, T, isInline, |
| 503 | isImplicitlyDeclared); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Fariborz Jahanian | d45c363 | 2009-07-01 21:05:43 +0000 | [diff] [blame] | 506 | void |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 507 | CXXDestructorDecl::Destroy(ASTContext& C) { |
| 508 | C.Deallocate(BaseOrMemberDestructions); |
| 509 | CXXMethodDecl::Destroy(C); |
| 510 | } |
| 511 | |
| 512 | void |
| 513 | CXXDestructorDecl::computeBaseOrMembersToDestroy(ASTContext &C) { |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 514 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(getDeclContext()); |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 515 | llvm::SmallVector<uintptr_t, 32> AllToDestruct; |
| 516 | |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 517 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 518 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 519 | // Skip over virtual bases which have trivial destructors. |
| 520 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 521 | = cast<CXXRecordDecl>(VBase->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 522 | if (BaseClassDecl->hasTrivialDestructor()) |
| 523 | continue; |
| 524 | uintptr_t Member = |
Fariborz Jahanian | cf18312 | 2009-07-22 00:42:46 +0000 | [diff] [blame] | 525 | reinterpret_cast<uintptr_t>(VBase->getType().getTypePtr()) | VBASE; |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 526 | AllToDestruct.push_back(Member); |
| 527 | } |
| 528 | for (CXXRecordDecl::base_class_iterator Base = |
| 529 | ClassDecl->bases_begin(), |
| 530 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 531 | if (Base->isVirtual()) |
| 532 | continue; |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 533 | // Skip over virtual bases which have trivial destructors. |
| 534 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 535 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 536 | if (BaseClassDecl->hasTrivialDestructor()) |
| 537 | continue; |
| 538 | |
| 539 | uintptr_t Member = |
Fariborz Jahanian | cf18312 | 2009-07-22 00:42:46 +0000 | [diff] [blame] | 540 | reinterpret_cast<uintptr_t>(Base->getType().getTypePtr()) | DRCTNONVBASE; |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 541 | AllToDestruct.push_back(Member); |
| 542 | } |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 543 | |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 544 | // non-static data members. |
| 545 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 546 | E = ClassDecl->field_end(); Field != E; ++Field) { |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 547 | QualType FieldType = C.getBaseElementType((*Field)->getType()); |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 548 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 549 | if (const RecordType* RT = FieldType->getAs<RecordType>()) { |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 550 | // Skip over virtual bases which have trivial destructors. |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 551 | CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 552 | if (BaseClassDecl->hasTrivialDestructor()) |
| 553 | continue; |
| 554 | uintptr_t Member = reinterpret_cast<uintptr_t>(*Field); |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 555 | AllToDestruct.push_back(Member); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | unsigned NumDestructions = AllToDestruct.size(); |
| 560 | if (NumDestructions > 0) { |
| 561 | NumBaseOrMemberDestructions = NumDestructions; |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 562 | BaseOrMemberDestructions = new (C) uintptr_t [NumDestructions]; |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 563 | // Insert in reverse order. |
| 564 | for (int Idx = NumDestructions-1, i=0 ; Idx >= 0; --Idx) |
| 565 | BaseOrMemberDestructions[i++] = AllToDestruct[Idx]; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | void |
Fariborz Jahanian | d45c363 | 2009-07-01 21:05:43 +0000 | [diff] [blame] | 570 | CXXConstructorDecl::setBaseOrMemberInitializers( |
Fariborz Jahanian | 87595e4 | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 571 | ASTContext &C, |
| 572 | CXXBaseOrMemberInitializer **Initializers, |
| 573 | unsigned NumInitializers, |
| 574 | llvm::SmallVectorImpl<CXXBaseSpecifier *>& Bases, |
| 575 | llvm::SmallVectorImpl<FieldDecl *>&Fields) { |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 576 | // We need to build the initializer AST according to order of construction |
| 577 | // and not what user specified in the Initializers list. |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 578 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(getDeclContext()); |
| 579 | llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit; |
Fariborz Jahanian | aa26650 | 2009-07-22 20:25:00 +0000 | [diff] [blame] | 580 | llvm::DenseMap<const void *, CXXBaseOrMemberInitializer*> AllBaseFields; |
| 581 | |
| 582 | for (unsigned i = 0; i < NumInitializers; i++) { |
| 583 | CXXBaseOrMemberInitializer *Member = Initializers[i]; |
Fariborz Jahanian | 08c6357 | 2009-07-25 01:08:28 +0000 | [diff] [blame] | 584 | if (Member->isBaseInitializer()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 585 | AllBaseFields[Member->getBaseClass()->getAs<RecordType>()] = Member; |
Fariborz Jahanian | 08c6357 | 2009-07-25 01:08:28 +0000 | [diff] [blame] | 586 | else |
| 587 | AllBaseFields[Member->getMember()] = Member; |
Fariborz Jahanian | aa26650 | 2009-07-22 20:25:00 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 590 | // Push virtual bases before others. |
| 591 | for (CXXRecordDecl::base_class_iterator VBase = |
| 592 | ClassDecl->vbases_begin(), |
| 593 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
Fariborz Jahanian | 08c6357 | 2009-07-25 01:08:28 +0000 | [diff] [blame] | 594 | if (CXXBaseOrMemberInitializer *Value = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 595 | AllBaseFields.lookup(VBase->getType()->getAs<RecordType>())) |
Fariborz Jahanian | 08c6357 | 2009-07-25 01:08:28 +0000 | [diff] [blame] | 596 | AllToInit.push_back(Value); |
Fariborz Jahanian | aa26650 | 2009-07-22 20:25:00 +0000 | [diff] [blame] | 597 | else { |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 598 | CXXRecordDecl *VBaseDecl = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 599 | cast<CXXRecordDecl>(VBase->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 600 | assert(VBaseDecl && "setBaseOrMemberInitializers - VBaseDecl null"); |
Fariborz Jahanian | 87595e4 | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 601 | if (!VBaseDecl->getDefaultConstructor(C) && |
| 602 | !VBase->getType()->isDependentType()) |
| 603 | Bases.push_back(VBase); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 604 | CXXBaseOrMemberInitializer *Member = |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 605 | new (C) CXXBaseOrMemberInitializer(VBase->getType(), 0, 0, |
| 606 | VBaseDecl->getDefaultConstructor(C), |
| 607 | SourceLocation()); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 608 | AllToInit.push_back(Member); |
| 609 | } |
| 610 | } |
Fariborz Jahanian | aa26650 | 2009-07-22 20:25:00 +0000 | [diff] [blame] | 611 | |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 612 | for (CXXRecordDecl::base_class_iterator Base = |
| 613 | ClassDecl->bases_begin(), |
| 614 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 615 | // Virtuals are in the virtual base list and already constructed. |
| 616 | if (Base->isVirtual()) |
| 617 | continue; |
Fariborz Jahanian | 08c6357 | 2009-07-25 01:08:28 +0000 | [diff] [blame] | 618 | if (CXXBaseOrMemberInitializer *Value = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 619 | AllBaseFields.lookup(Base->getType()->getAs<RecordType>())) |
Fariborz Jahanian | 08c6357 | 2009-07-25 01:08:28 +0000 | [diff] [blame] | 620 | AllToInit.push_back(Value); |
Fariborz Jahanian | aa26650 | 2009-07-22 20:25:00 +0000 | [diff] [blame] | 621 | else { |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 622 | CXXRecordDecl *BaseDecl = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 623 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 624 | assert(BaseDecl && "setBaseOrMemberInitializers - BaseDecl null"); |
Fariborz Jahanian | 87595e4 | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 625 | if (!BaseDecl->getDefaultConstructor(C) && |
| 626 | !Base->getType()->isDependentType()) |
| 627 | Bases.push_back(Base); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 628 | CXXBaseOrMemberInitializer *Member = |
Fariborz Jahanian | aa26650 | 2009-07-22 20:25:00 +0000 | [diff] [blame] | 629 | new (C) CXXBaseOrMemberInitializer(Base->getType(), 0, 0, |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 630 | BaseDecl->getDefaultConstructor(C), |
Fariborz Jahanian | aa26650 | 2009-07-22 20:25:00 +0000 | [diff] [blame] | 631 | SourceLocation()); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 632 | AllToInit.push_back(Member); |
| 633 | } |
| 634 | } |
Fariborz Jahanian | aa26650 | 2009-07-22 20:25:00 +0000 | [diff] [blame] | 635 | |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 636 | // non-static data members. |
| 637 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 638 | E = ClassDecl->field_end(); Field != E; ++Field) { |
Fariborz Jahanian | 08c6357 | 2009-07-25 01:08:28 +0000 | [diff] [blame] | 639 | if (CXXBaseOrMemberInitializer *Value = AllBaseFields.lookup(*Field)) { |
| 640 | AllToInit.push_back(Value); |
Fariborz Jahanian | aa26650 | 2009-07-22 20:25:00 +0000 | [diff] [blame] | 641 | continue; |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 642 | } |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 643 | |
| 644 | QualType FT = C.getBaseElementType((*Field)->getType()); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 645 | if (const RecordType* RT = FT->getAs<RecordType>()) { |
Douglas Gregor | 5e03f9e | 2009-07-23 23:49:00 +0000 | [diff] [blame] | 646 | CXXConstructorDecl *Ctor = |
| 647 | cast<CXXRecordDecl>(RT->getDecl())->getDefaultConstructor(C); |
| 648 | if (!Ctor && !FT->isDependentType()) |
Fariborz Jahanian | 87595e4 | 2009-07-23 23:32:59 +0000 | [diff] [blame] | 649 | Fields.push_back(*Field); |
Fariborz Jahanian | aa26650 | 2009-07-22 20:25:00 +0000 | [diff] [blame] | 650 | CXXBaseOrMemberInitializer *Member = |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 651 | new (C) CXXBaseOrMemberInitializer((*Field), 0, 0, |
| 652 | Ctor, |
| 653 | SourceLocation()); |
Fariborz Jahanian | aa26650 | 2009-07-22 20:25:00 +0000 | [diff] [blame] | 654 | AllToInit.push_back(Member); |
| 655 | } |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 656 | } |
Mike Stump | f121677 | 2009-07-31 18:25:34 +0000 | [diff] [blame^] | 657 | |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 658 | NumInitializers = AllToInit.size(); |
Fariborz Jahanian | d45c363 | 2009-07-01 21:05:43 +0000 | [diff] [blame] | 659 | if (NumInitializers > 0) { |
| 660 | NumBaseOrMemberInitializers = NumInitializers; |
| 661 | BaseOrMemberInitializers = |
Fariborz Jahanian | 0d3c26c | 2009-07-07 16:24:08 +0000 | [diff] [blame] | 662 | new (C) CXXBaseOrMemberInitializer*[NumInitializers]; |
Fariborz Jahanian | d45c363 | 2009-07-01 21:05:43 +0000 | [diff] [blame] | 663 | for (unsigned Idx = 0; Idx < NumInitializers; ++Idx) |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 664 | BaseOrMemberInitializers[Idx] = AllToInit[Idx]; |
Fariborz Jahanian | d45c363 | 2009-07-01 21:05:43 +0000 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | |
Fariborz Jahanian | 73b85f3 | 2009-07-01 23:35:25 +0000 | [diff] [blame] | 668 | void |
| 669 | CXXConstructorDecl::Destroy(ASTContext& C) { |
| 670 | C.Deallocate(BaseOrMemberInitializers); |
Fariborz Jahanian | 0d3c26c | 2009-07-07 16:24:08 +0000 | [diff] [blame] | 671 | CXXMethodDecl::Destroy(C); |
Fariborz Jahanian | 73b85f3 | 2009-07-01 23:35:25 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 674 | CXXConversionDecl * |
| 675 | CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 676 | SourceLocation L, DeclarationName N, |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 677 | QualType T, bool isInline, bool isExplicit) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 678 | assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 679 | "Name must refer to a conversion function"); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 680 | return new (C) CXXConversionDecl(RD, L, N, T, isInline, isExplicit); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 683 | OverloadedFunctionDecl * |
| 684 | OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 685 | DeclarationName N) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 686 | return new (C) OverloadedFunctionDecl(DC, N); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 687 | } |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 688 | |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 689 | void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) { |
| 690 | Functions.push_back(F); |
| 691 | this->setLocation(F.get()->getLocation()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Douglas Gregor | daa439a | 2009-07-08 10:57:20 +0000 | [diff] [blame] | 694 | OverloadIterator::reference OverloadIterator::operator*() const { |
| 695 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 696 | return FD; |
| 697 | |
| 698 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D)) |
| 699 | return FTD; |
| 700 | |
| 701 | assert(isa<OverloadedFunctionDecl>(D)); |
| 702 | return *Iter; |
| 703 | } |
| 704 | |
| 705 | OverloadIterator &OverloadIterator::operator++() { |
| 706 | if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) { |
| 707 | D = 0; |
| 708 | return *this; |
| 709 | } |
| 710 | |
| 711 | if (++Iter == cast<OverloadedFunctionDecl>(D)->function_end()) |
| 712 | D = 0; |
| 713 | |
| 714 | return *this; |
| 715 | } |
| 716 | |
| 717 | bool OverloadIterator::Equals(const OverloadIterator &Other) const { |
| 718 | if (!D || !Other.D) |
| 719 | return D == Other.D; |
| 720 | |
| 721 | if (D != Other.D) |
| 722 | return false; |
| 723 | |
| 724 | return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter; |
| 725 | } |
| 726 | |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 727 | LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 728 | DeclContext *DC, |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 729 | SourceLocation L, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 730 | LanguageIDs Lang, bool Braces) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 731 | return new (C) LinkageSpecDecl(DC, L, Lang, Braces); |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 732 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 733 | |
| 734 | UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC, |
| 735 | SourceLocation L, |
| 736 | SourceLocation NamespaceLoc, |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 737 | SourceRange QualifierRange, |
| 738 | NestedNameSpecifier *Qualifier, |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 739 | SourceLocation IdentLoc, |
| 740 | NamespaceDecl *Used, |
| 741 | DeclContext *CommonAncestor) { |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 742 | return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange, |
| 743 | Qualifier, IdentLoc, Used, CommonAncestor); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 746 | NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC, |
| 747 | SourceLocation L, |
| 748 | SourceLocation AliasLoc, |
| 749 | IdentifierInfo *Alias, |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 750 | SourceRange QualifierRange, |
| 751 | NestedNameSpecifier *Qualifier, |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 752 | SourceLocation IdentLoc, |
| 753 | NamedDecl *Namespace) { |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 754 | return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange, |
| 755 | Qualifier, IdentLoc, Namespace); |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 758 | UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, |
| 759 | SourceLocation L, SourceRange NNR, SourceLocation TargetNL, |
| 760 | SourceLocation UL, NamedDecl* Target, |
| 761 | NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) { |
| 762 | return new (C) UsingDecl(DC, L, NNR, TargetNL, UL, Target, |
| 763 | TargetNNS, IsTypeNameArg); |
| 764 | } |
| 765 | |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 766 | StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC, |
| 767 | SourceLocation L, Expr *AssertExpr, |
| 768 | StringLiteral *Message) { |
| 769 | return new (C) StaticAssertDecl(DC, L, AssertExpr, Message); |
| 770 | } |
| 771 | |
| 772 | void StaticAssertDecl::Destroy(ASTContext& C) { |
| 773 | AssertExpr->Destroy(C); |
| 774 | Message->Destroy(C); |
| 775 | this->~StaticAssertDecl(); |
| 776 | C.Deallocate((void *)this); |
| 777 | } |
| 778 | |
| 779 | StaticAssertDecl::~StaticAssertDecl() { |
| 780 | } |
| 781 | |
Anders Carlsson | 05bf2c7 | 2009-03-26 23:46:50 +0000 | [diff] [blame] | 782 | static const char *getAccessName(AccessSpecifier AS) { |
| 783 | switch (AS) { |
| 784 | default: |
| 785 | case AS_none: |
| 786 | assert("Invalid access specifier!"); |
| 787 | return 0; |
| 788 | case AS_public: |
| 789 | return "public"; |
| 790 | case AS_private: |
| 791 | return "private"; |
| 792 | case AS_protected: |
| 793 | return "protected"; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, |
| 798 | AccessSpecifier AS) { |
| 799 | return DB << getAccessName(AS); |
| 800 | } |
| 801 | |
| 802 | |