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