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