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] = |
Douglas Gregor | 2aef06d | 2009-07-22 20:55:49 +0000 | [diff] [blame] | 134 | CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true, |
| 135 | VBaseClassDecl->getTagKind() == RecordDecl::TK_class, |
| 136 | UniqueVbases[i]->getAccessSpecifier(), QT); |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 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. |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 228 | // FIXME: C++0x: don't do this for "= default" default constructors. |
Fariborz Jahanian | 8bc3fa4 | 2009-06-17 22:44:31 +0000 | [diff] [blame] | 229 | HasTrivialConstructor = false; |
Anders Carlsson | 347ba89 | 2009-04-16 00:08:20 +0000 | [diff] [blame] | 230 | |
Fariborz Jahanian | 8bc3fa4 | 2009-06-17 22:44:31 +0000 | [diff] [blame] | 231 | // Note when we have a user-declared copy constructor, which will |
| 232 | // suppress the implicit declaration of a copy constructor. |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 233 | if (ConDecl->isCopyConstructor(Context)) { |
Fariborz Jahanian | 8bc3fa4 | 2009-06-17 22:44:31 +0000 | [diff] [blame] | 234 | UserDeclaredCopyConstructor = true; |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 235 | |
| 236 | // C++ [class.copy]p6: |
| 237 | // A copy constructor is trivial if it is implicitly declared. |
| 238 | // FIXME: C++0x: don't do this for "= default" copy constructors. |
| 239 | HasTrivialCopyConstructor = false; |
| 240 | } |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 243 | void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context, |
| 244 | CXXMethodDecl *OpDecl) { |
| 245 | // We're interested specifically in copy assignment operators. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 246 | const FunctionProtoType *FnType = OpDecl->getType()->getAsFunctionProtoType(); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 247 | assert(FnType && "Overloaded operator has no proto function type."); |
| 248 | assert(FnType->getNumArgs() == 1 && !FnType->isVariadic()); |
| 249 | QualType ArgType = FnType->getArgType(0); |
Ted Kremenek | 35366a6 | 2009-07-17 17:50:17 +0000 | [diff] [blame] | 250 | if (const LValueReferenceType *Ref = ArgType->getAsLValueReferenceType()) |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 251 | ArgType = Ref->getPointeeType(); |
| 252 | |
| 253 | ArgType = ArgType.getUnqualifiedType(); |
| 254 | QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType( |
| 255 | const_cast<CXXRecordDecl*>(this))); |
| 256 | |
| 257 | if (ClassType != Context.getCanonicalType(ArgType)) |
| 258 | return; |
| 259 | |
| 260 | // This is a copy assignment operator. |
| 261 | // Suppress the implicit declaration of a copy constructor. |
| 262 | UserDeclaredCopyAssignment = true; |
| 263 | |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 264 | // C++ [class.copy]p11: |
| 265 | // A copy assignment operator is trivial if it is implicitly declared. |
| 266 | // FIXME: C++0x: don't do this for "= default" copy operators. |
| 267 | HasTrivialCopyAssignment = false; |
| 268 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 269 | // C++ [class]p4: |
| 270 | // A POD-struct is an aggregate class that [...] has no user-defined copy |
| 271 | // assignment operator [...]. |
| 272 | PlainOldData = false; |
| 273 | } |
| 274 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 275 | void CXXRecordDecl::addConversionFunction(ASTContext &Context, |
| 276 | CXXConversionDecl *ConvDecl) { |
| 277 | Conversions.addOverload(ConvDecl); |
| 278 | } |
| 279 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 280 | |
| 281 | CXXConstructorDecl * |
| 282 | CXXRecordDecl::getDefaultConstructor(ASTContext &Context) { |
| 283 | QualType ClassType = Context.getTypeDeclType(this); |
| 284 | DeclarationName ConstructorName |
| 285 | = Context.DeclarationNames.getCXXConstructorName( |
| 286 | Context.getCanonicalType(ClassType.getUnqualifiedType())); |
| 287 | |
| 288 | DeclContext::lookup_const_iterator Con, ConEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 289 | for (llvm::tie(Con, ConEnd) = lookup(ConstructorName); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 290 | Con != ConEnd; ++Con) { |
| 291 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 292 | if (Constructor->isDefaultConstructor()) |
| 293 | return Constructor; |
| 294 | } |
| 295 | return 0; |
| 296 | } |
| 297 | |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 298 | const CXXDestructorDecl * |
| 299 | CXXRecordDecl::getDestructor(ASTContext &Context) { |
| 300 | QualType ClassType = Context.getTypeDeclType(this); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 301 | |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 302 | DeclarationName Name |
| 303 | = Context.DeclarationNames.getCXXDestructorName(ClassType); |
| 304 | |
| 305 | DeclContext::lookup_iterator I, E; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 306 | llvm::tie(I, E) = lookup(Name); |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 307 | assert(I != E && "Did not find a destructor!"); |
| 308 | |
| 309 | const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I); |
| 310 | assert(++I == E && "Found more than one destructor!"); |
| 311 | |
| 312 | return Dtor; |
| 313 | } |
| 314 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 315 | CXXMethodDecl * |
| 316 | CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 317 | SourceLocation L, DeclarationName N, |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 318 | QualType T, bool isStatic, bool isInline) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 319 | return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, isStatic, isInline); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 322 | |
| 323 | typedef llvm::DenseMap<const CXXMethodDecl*, |
| 324 | std::vector<const CXXMethodDecl *> *> |
| 325 | OverriddenMethodsMapTy; |
| 326 | |
| 327 | static OverriddenMethodsMapTy *OverriddenMethods = 0; |
| 328 | |
| 329 | void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) { |
| 330 | // FIXME: The CXXMethodDecl dtor needs to remove and free the entry. |
| 331 | |
| 332 | if (!OverriddenMethods) |
| 333 | OverriddenMethods = new OverriddenMethodsMapTy(); |
| 334 | |
| 335 | std::vector<const CXXMethodDecl *> *&Methods = (*OverriddenMethods)[this]; |
| 336 | if (!Methods) |
| 337 | Methods = new std::vector<const CXXMethodDecl *>; |
| 338 | |
| 339 | Methods->push_back(MD); |
| 340 | } |
| 341 | |
| 342 | CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const { |
| 343 | if (!OverriddenMethods) |
| 344 | return 0; |
| 345 | |
| 346 | OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this); |
| 347 | if (it == OverriddenMethods->end()) |
| 348 | return 0; |
| 349 | return &(*it->second)[0]; |
| 350 | } |
| 351 | |
| 352 | CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const { |
| 353 | if (!OverriddenMethods) |
| 354 | return 0; |
| 355 | |
| 356 | OverriddenMethodsMapTy::iterator it = OverriddenMethods->find(this); |
| 357 | if (it == OverriddenMethods->end()) |
| 358 | return 0; |
| 359 | |
| 360 | return &(*it->second)[it->second->size()]; |
| 361 | } |
| 362 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 363 | QualType CXXMethodDecl::getThisType(ASTContext &C) const { |
Argyrios Kyrtzidis | b0d178d | 2008-10-24 22:28:18 +0000 | [diff] [blame] | 364 | // C++ 9.3.2p1: The type of this in a member function of a class X is X*. |
| 365 | // If the member function is declared const, the type of this is const X*, |
| 366 | // if the member function is declared volatile, the type of this is |
| 367 | // volatile X*, and if the member function is declared const volatile, |
| 368 | // the type of this is const volatile X*. |
| 369 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 370 | assert(isInstance() && "No 'this' for static methods!"); |
Anders Carlsson | 31a0875 | 2009-06-13 02:59:33 +0000 | [diff] [blame] | 371 | |
| 372 | QualType ClassTy; |
| 373 | if (ClassTemplateDecl *TD = getParent()->getDescribedClassTemplate()) |
| 374 | ClassTy = TD->getInjectedClassNameType(C); |
| 375 | else |
| 376 | ClassTy = C.getTagDeclType(const_cast<CXXRecordDecl*>(getParent())); |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 377 | ClassTy = ClassTy.getWithAdditionalQualifiers(getTypeQualifiers()); |
Anders Carlsson | 4e57992 | 2009-07-10 21:35:09 +0000 | [diff] [blame] | 378 | return C.getPointerType(ClassTy); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 381 | CXXBaseOrMemberInitializer:: |
Fariborz Jahanian | 47deacf | 2009-06-30 00:02:17 +0000 | [diff] [blame] | 382 | CXXBaseOrMemberInitializer(QualType BaseType, Expr **Args, unsigned NumArgs, |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 383 | CXXConstructorDecl *C, |
Fariborz Jahanian | 47deacf | 2009-06-30 00:02:17 +0000 | [diff] [blame] | 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 | } |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 396 | CtorToCall = C; |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | CXXBaseOrMemberInitializer:: |
Fariborz Jahanian | 47deacf | 2009-06-30 00:02:17 +0000 | [diff] [blame] | 400 | CXXBaseOrMemberInitializer(FieldDecl *Member, Expr **Args, unsigned NumArgs, |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 401 | CXXConstructorDecl *C, |
Fariborz Jahanian | 47deacf | 2009-06-30 00:02:17 +0000 | [diff] [blame] | 402 | SourceLocation L) |
| 403 | : Args(0), NumArgs(0), IdLoc(L) { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 404 | BaseOrMember = reinterpret_cast<uintptr_t>(Member); |
| 405 | assert((BaseOrMember & 0x01) == 0 && "Invalid member pointer"); |
| 406 | |
| 407 | if (NumArgs > 0) { |
| 408 | this->NumArgs = NumArgs; |
| 409 | this->Args = new Expr*[NumArgs]; |
| 410 | for (unsigned Idx = 0; Idx < NumArgs; ++Idx) |
| 411 | this->Args[Idx] = Args[Idx]; |
| 412 | } |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 413 | CtorToCall = C; |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | CXXBaseOrMemberInitializer::~CXXBaseOrMemberInitializer() { |
| 417 | delete [] Args; |
| 418 | } |
| 419 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 420 | CXXConstructorDecl * |
| 421 | CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 422 | SourceLocation L, DeclarationName N, |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 423 | QualType T, bool isExplicit, |
| 424 | bool isInline, bool isImplicitlyDeclared) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 425 | assert(N.getNameKind() == DeclarationName::CXXConstructorName && |
| 426 | "Name must refer to a constructor"); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 427 | return new (C) CXXConstructorDecl(RD, L, N, T, isExplicit, isInline, |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 428 | isImplicitlyDeclared); |
| 429 | } |
| 430 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 431 | bool CXXConstructorDecl::isDefaultConstructor() const { |
| 432 | // C++ [class.ctor]p5: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 433 | // A default constructor for a class X is a constructor of class |
| 434 | // X that can be called without an argument. |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 435 | return (getNumParams() == 0) || |
Douglas Gregor | f03d7c7 | 2008-11-05 15:29:30 +0000 | [diff] [blame] | 436 | (getNumParams() > 0 && getParamDecl(0)->getDefaultArg() != 0); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | bool |
| 440 | CXXConstructorDecl::isCopyConstructor(ASTContext &Context, |
| 441 | unsigned &TypeQuals) const { |
| 442 | // C++ [class.copy]p2: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 443 | // A non-template constructor for class X is a copy constructor |
| 444 | // if its first parameter is of type X&, const X&, volatile X& or |
| 445 | // const volatile X&, and either there are no other parameters |
| 446 | // or else all other parameters have default arguments (8.3.6). |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 447 | if ((getNumParams() < 1) || |
Anders Carlsson | ae0b4e7 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 448 | (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg())) |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 449 | return false; |
| 450 | |
| 451 | const ParmVarDecl *Param = getParamDecl(0); |
| 452 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 453 | // Do we have a reference type? Rvalue references don't count. |
| 454 | const LValueReferenceType *ParamRefType = |
Ted Kremenek | 35366a6 | 2009-07-17 17:50:17 +0000 | [diff] [blame] | 455 | Param->getType()->getAsLValueReferenceType(); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 456 | if (!ParamRefType) |
| 457 | return false; |
| 458 | |
| 459 | // Is it a reference to our class type? |
| 460 | QualType PointeeType |
| 461 | = Context.getCanonicalType(ParamRefType->getPointeeType()); |
| 462 | QualType ClassTy |
| 463 | = Context.getTagDeclType(const_cast<CXXRecordDecl*>(getParent())); |
| 464 | if (PointeeType.getUnqualifiedType() != ClassTy) |
| 465 | return false; |
| 466 | |
| 467 | // We have a copy constructor. |
| 468 | TypeQuals = PointeeType.getCVRQualifiers(); |
| 469 | return true; |
| 470 | } |
| 471 | |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 472 | bool CXXConstructorDecl::isConvertingConstructor() const { |
| 473 | // C++ [class.conv.ctor]p1: |
| 474 | // A constructor declared without the function-specifier explicit |
| 475 | // that can be called with a single parameter specifies a |
| 476 | // conversion from the type of its first parameter to the type of |
| 477 | // its class. Such a constructor is called a converting |
| 478 | // constructor. |
| 479 | if (isExplicit()) |
| 480 | return false; |
| 481 | |
| 482 | return (getNumParams() == 0 && |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 483 | getType()->getAsFunctionProtoType()->isVariadic()) || |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 484 | (getNumParams() == 1) || |
Anders Carlsson | ae0b4e7 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 485 | (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg()); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 486 | } |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 487 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 488 | CXXDestructorDecl * |
| 489 | CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 490 | SourceLocation L, DeclarationName N, |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 491 | QualType T, bool isInline, |
| 492 | bool isImplicitlyDeclared) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 493 | assert(N.getNameKind() == DeclarationName::CXXDestructorName && |
| 494 | "Name must refer to a destructor"); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 495 | return new (C) CXXDestructorDecl(RD, L, N, T, isInline, |
| 496 | isImplicitlyDeclared); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Fariborz Jahanian | d45c363 | 2009-07-01 21:05:43 +0000 | [diff] [blame] | 499 | void |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 500 | CXXDestructorDecl::Destroy(ASTContext& C) { |
| 501 | C.Deallocate(BaseOrMemberDestructions); |
| 502 | CXXMethodDecl::Destroy(C); |
| 503 | } |
| 504 | |
| 505 | void |
| 506 | CXXDestructorDecl::computeBaseOrMembersToDestroy(ASTContext &C) { |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 507 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(getDeclContext()); |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 508 | llvm::SmallVector<uintptr_t, 32> AllToDestruct; |
| 509 | |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 510 | for (CXXRecordDecl::base_class_iterator VBase = ClassDecl->vbases_begin(), |
| 511 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 512 | // Skip over virtual bases which have trivial destructors. |
| 513 | CXXRecordDecl *BaseClassDecl |
| 514 | = cast<CXXRecordDecl>(VBase->getType()->getAsRecordType()->getDecl()); |
| 515 | if (BaseClassDecl->hasTrivialDestructor()) |
| 516 | continue; |
| 517 | uintptr_t Member = |
Fariborz Jahanian | cf18312 | 2009-07-22 00:42:46 +0000 | [diff] [blame] | 518 | reinterpret_cast<uintptr_t>(VBase->getType().getTypePtr()) | VBASE; |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 519 | AllToDestruct.push_back(Member); |
| 520 | } |
| 521 | for (CXXRecordDecl::base_class_iterator Base = |
| 522 | ClassDecl->bases_begin(), |
| 523 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 524 | if (Base->isVirtual()) |
| 525 | continue; |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 526 | // Skip over virtual bases which have trivial destructors. |
| 527 | CXXRecordDecl *BaseClassDecl |
| 528 | = cast<CXXRecordDecl>(Base->getType()->getAsRecordType()->getDecl()); |
| 529 | if (BaseClassDecl->hasTrivialDestructor()) |
| 530 | continue; |
| 531 | |
| 532 | uintptr_t Member = |
Fariborz Jahanian | cf18312 | 2009-07-22 00:42:46 +0000 | [diff] [blame] | 533 | reinterpret_cast<uintptr_t>(Base->getType().getTypePtr()) | DRCTNONVBASE; |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 534 | AllToDestruct.push_back(Member); |
| 535 | } |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 536 | |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 537 | // non-static data members. |
| 538 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 539 | E = ClassDecl->field_end(); Field != E; ++Field) { |
| 540 | QualType FieldType = C.getCanonicalType((*Field)->getType()); |
| 541 | while (const ArrayType *AT = C.getAsArrayType(FieldType)) |
| 542 | FieldType = AT->getElementType(); |
| 543 | |
Ted Kremenek | 35366a6 | 2009-07-17 17:50:17 +0000 | [diff] [blame] | 544 | if (FieldType->getAsRecordType()) { |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 545 | // Skip over virtual bases which have trivial destructors. |
| 546 | CXXRecordDecl *BaseClassDecl |
| 547 | = cast<CXXRecordDecl>(FieldType->getAsRecordType()->getDecl()); |
| 548 | if (BaseClassDecl->hasTrivialDestructor()) |
| 549 | continue; |
| 550 | uintptr_t Member = reinterpret_cast<uintptr_t>(*Field); |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 551 | AllToDestruct.push_back(Member); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | unsigned NumDestructions = AllToDestruct.size(); |
| 556 | if (NumDestructions > 0) { |
| 557 | NumBaseOrMemberDestructions = NumDestructions; |
Fariborz Jahanian | 393612e | 2009-07-21 22:36:06 +0000 | [diff] [blame] | 558 | BaseOrMemberDestructions = new (C) uintptr_t [NumDestructions]; |
Fariborz Jahanian | 560de45 | 2009-07-15 22:34:08 +0000 | [diff] [blame] | 559 | // Insert in reverse order. |
| 560 | for (int Idx = NumDestructions-1, i=0 ; Idx >= 0; --Idx) |
| 561 | BaseOrMemberDestructions[i++] = AllToDestruct[Idx]; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | void |
Fariborz Jahanian | d45c363 | 2009-07-01 21:05:43 +0000 | [diff] [blame] | 566 | CXXConstructorDecl::setBaseOrMemberInitializers( |
Fariborz Jahanian | 73b85f3 | 2009-07-01 23:35:25 +0000 | [diff] [blame] | 567 | ASTContext &C, |
Fariborz Jahanian | d45c363 | 2009-07-01 21:05:43 +0000 | [diff] [blame] | 568 | CXXBaseOrMemberInitializer **Initializers, |
| 569 | unsigned NumInitializers) { |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 570 | // We need to build the initializer AST according to order of construction |
| 571 | // and not what user specified in the Initializers list. |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 572 | CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(getDeclContext()); |
| 573 | llvm::SmallVector<CXXBaseOrMemberInitializer*, 32> AllToInit; |
Fariborz Jahanian | aa26650 | 2009-07-22 20:25:00 +0000 | [diff] [blame] | 574 | llvm::DenseMap<const void *, CXXBaseOrMemberInitializer*> AllBaseFields; |
| 575 | |
| 576 | for (unsigned i = 0; i < NumInitializers; i++) { |
| 577 | CXXBaseOrMemberInitializer *Member = Initializers[i]; |
| 578 | const void * Key = Member->isBaseInitializer() ? |
| 579 | reinterpret_cast<const void *>( |
| 580 | Member->getBaseClass()->getAsRecordType()) : |
| 581 | reinterpret_cast<const void *>(Member->getMember()); |
| 582 | AllBaseFields[Key] = Member; |
| 583 | } |
| 584 | |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 585 | // Push virtual bases before others. |
| 586 | for (CXXRecordDecl::base_class_iterator VBase = |
| 587 | ClassDecl->vbases_begin(), |
| 588 | E = ClassDecl->vbases_end(); VBase != E; ++VBase) { |
Fariborz Jahanian | aa26650 | 2009-07-22 20:25:00 +0000 | [diff] [blame] | 589 | const void *Key = reinterpret_cast<const void *>( |
| 590 | VBase->getType()->getAsRecordType()); |
| 591 | if (AllBaseFields[Key]) |
| 592 | AllToInit.push_back(AllBaseFields[Key]); |
| 593 | else { |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 594 | CXXRecordDecl *VBaseDecl = |
| 595 | cast<CXXRecordDecl>(VBase->getType()->getAsRecordType()->getDecl()); |
| 596 | assert(VBaseDecl && "setBaseOrMemberInitializers - VBaseDecl null"); |
| 597 | // FIXME. Issue error if default ctor is missing. |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 598 | CXXBaseOrMemberInitializer *Member = |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 599 | new (C) CXXBaseOrMemberInitializer(VBase->getType(), 0, 0, |
| 600 | VBaseDecl->getDefaultConstructor(C), |
| 601 | SourceLocation()); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 602 | AllToInit.push_back(Member); |
| 603 | } |
| 604 | } |
Fariborz Jahanian | aa26650 | 2009-07-22 20:25:00 +0000 | [diff] [blame] | 605 | |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 606 | for (CXXRecordDecl::base_class_iterator Base = |
| 607 | ClassDecl->bases_begin(), |
| 608 | E = ClassDecl->bases_end(); Base != E; ++Base) { |
| 609 | // Virtuals are in the virtual base list and already constructed. |
| 610 | if (Base->isVirtual()) |
| 611 | continue; |
Fariborz Jahanian | aa26650 | 2009-07-22 20:25:00 +0000 | [diff] [blame] | 612 | const void *Key = reinterpret_cast<const void *>( |
| 613 | Base->getType()->getAsRecordType()); |
| 614 | if (AllBaseFields[Key]) |
| 615 | AllToInit.push_back(AllBaseFields[Key]); |
| 616 | else { |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 617 | CXXRecordDecl *BaseDecl = |
| 618 | cast<CXXRecordDecl>(Base->getType()->getAsRecordType()->getDecl()); |
| 619 | assert(BaseDecl && "setBaseOrMemberInitializers - BaseDecl null"); |
| 620 | // FIXME. Issue error if default ctor is missing. |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 621 | CXXBaseOrMemberInitializer *Member = |
Fariborz Jahanian | aa26650 | 2009-07-22 20:25:00 +0000 | [diff] [blame] | 622 | new (C) CXXBaseOrMemberInitializer(Base->getType(), 0, 0, |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 623 | BaseDecl->getDefaultConstructor(C), |
Fariborz Jahanian | aa26650 | 2009-07-22 20:25:00 +0000 | [diff] [blame] | 624 | SourceLocation()); |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 625 | AllToInit.push_back(Member); |
| 626 | } |
| 627 | } |
Fariborz Jahanian | aa26650 | 2009-07-22 20:25:00 +0000 | [diff] [blame] | 628 | |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 629 | // non-static data members. |
| 630 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 631 | E = ClassDecl->field_end(); Field != E; ++Field) { |
Fariborz Jahanian | aa26650 | 2009-07-22 20:25:00 +0000 | [diff] [blame] | 632 | const void * Key = reinterpret_cast<const void *>(*Field); |
| 633 | if (AllBaseFields[Key]) { |
| 634 | AllToInit.push_back(AllBaseFields[Key]); |
| 635 | continue; |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 636 | } |
Fariborz Jahanian | aa26650 | 2009-07-22 20:25:00 +0000 | [diff] [blame] | 637 | QualType FieldType = C.getCanonicalType((*Field)->getType()); |
| 638 | while (const ArrayType *AT = C.getAsArrayType(FieldType)) |
| 639 | FieldType = AT->getElementType(); |
Fariborz Jahanian | 9b9f424 | 2009-07-14 18:56:31 +0000 | [diff] [blame] | 640 | |
Fariborz Jahanian | aa26650 | 2009-07-22 20:25:00 +0000 | [diff] [blame] | 641 | if (FieldType->getAsRecordType()) { |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 642 | CXXConstructorDecl *Ctor = 0; |
| 643 | if (CXXRecordDecl *FieldClassDecl = |
| 644 | dyn_cast<CXXRecordDecl>(FieldType->getAsRecordType()->getDecl())) |
| 645 | Ctor = FieldClassDecl->getDefaultConstructor(C); |
| 646 | // FIXME. Issue error if default ctor is missing. |
Fariborz Jahanian | aa26650 | 2009-07-22 20:25:00 +0000 | [diff] [blame] | 647 | CXXBaseOrMemberInitializer *Member = |
Fariborz Jahanian | d7b27e1 | 2009-07-23 00:42:24 +0000 | [diff] [blame] | 648 | new (C) CXXBaseOrMemberInitializer((*Field), 0, 0, |
| 649 | Ctor, |
| 650 | SourceLocation()); |
Fariborz Jahanian | aa26650 | 2009-07-22 20:25:00 +0000 | [diff] [blame] | 651 | AllToInit.push_back(Member); |
| 652 | } |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | NumInitializers = AllToInit.size(); |
Fariborz Jahanian | d45c363 | 2009-07-01 21:05:43 +0000 | [diff] [blame] | 656 | if (NumInitializers > 0) { |
| 657 | NumBaseOrMemberInitializers = NumInitializers; |
| 658 | BaseOrMemberInitializers = |
Fariborz Jahanian | 0d3c26c | 2009-07-07 16:24:08 +0000 | [diff] [blame] | 659 | new (C) CXXBaseOrMemberInitializer*[NumInitializers]; |
Fariborz Jahanian | d45c363 | 2009-07-01 21:05:43 +0000 | [diff] [blame] | 660 | for (unsigned Idx = 0; Idx < NumInitializers; ++Idx) |
Fariborz Jahanian | d01c915 | 2009-07-14 18:24:21 +0000 | [diff] [blame] | 661 | BaseOrMemberInitializers[Idx] = AllToInit[Idx]; |
Fariborz Jahanian | d45c363 | 2009-07-01 21:05:43 +0000 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
Fariborz Jahanian | 73b85f3 | 2009-07-01 23:35:25 +0000 | [diff] [blame] | 665 | void |
| 666 | CXXConstructorDecl::Destroy(ASTContext& C) { |
| 667 | C.Deallocate(BaseOrMemberInitializers); |
Fariborz Jahanian | 0d3c26c | 2009-07-07 16:24:08 +0000 | [diff] [blame] | 668 | CXXMethodDecl::Destroy(C); |
Fariborz Jahanian | 73b85f3 | 2009-07-01 23:35:25 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 671 | CXXConversionDecl * |
| 672 | CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 673 | SourceLocation L, DeclarationName N, |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 674 | QualType T, bool isInline, bool isExplicit) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 675 | assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 676 | "Name must refer to a conversion function"); |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 677 | return new (C) CXXConversionDecl(RD, L, N, T, isInline, isExplicit); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 680 | OverloadedFunctionDecl * |
| 681 | OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 682 | DeclarationName N) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 683 | return new (C) OverloadedFunctionDecl(DC, N); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 684 | } |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 685 | |
Douglas Gregor | 364e021 | 2009-06-27 21:05:07 +0000 | [diff] [blame] | 686 | void OverloadedFunctionDecl::addOverload(AnyFunctionDecl F) { |
| 687 | Functions.push_back(F); |
| 688 | this->setLocation(F.get()->getLocation()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Douglas Gregor | daa439a | 2009-07-08 10:57:20 +0000 | [diff] [blame] | 691 | OverloadIterator::reference OverloadIterator::operator*() const { |
| 692 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 693 | return FD; |
| 694 | |
| 695 | if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D)) |
| 696 | return FTD; |
| 697 | |
| 698 | assert(isa<OverloadedFunctionDecl>(D)); |
| 699 | return *Iter; |
| 700 | } |
| 701 | |
| 702 | OverloadIterator &OverloadIterator::operator++() { |
| 703 | if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) { |
| 704 | D = 0; |
| 705 | return *this; |
| 706 | } |
| 707 | |
| 708 | if (++Iter == cast<OverloadedFunctionDecl>(D)->function_end()) |
| 709 | D = 0; |
| 710 | |
| 711 | return *this; |
| 712 | } |
| 713 | |
| 714 | bool OverloadIterator::Equals(const OverloadIterator &Other) const { |
| 715 | if (!D || !Other.D) |
| 716 | return D == Other.D; |
| 717 | |
| 718 | if (D != Other.D) |
| 719 | return false; |
| 720 | |
| 721 | return !isa<OverloadedFunctionDecl>(D) || Iter == Other.Iter; |
| 722 | } |
| 723 | |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 724 | LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 725 | DeclContext *DC, |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 726 | SourceLocation L, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 727 | LanguageIDs Lang, bool Braces) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 728 | return new (C) LinkageSpecDecl(DC, L, Lang, Braces); |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 729 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 730 | |
| 731 | UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC, |
| 732 | SourceLocation L, |
| 733 | SourceLocation NamespaceLoc, |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 734 | SourceRange QualifierRange, |
| 735 | NestedNameSpecifier *Qualifier, |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 736 | SourceLocation IdentLoc, |
| 737 | NamespaceDecl *Used, |
| 738 | DeclContext *CommonAncestor) { |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 739 | return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange, |
| 740 | Qualifier, IdentLoc, Used, CommonAncestor); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 743 | NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC, |
| 744 | SourceLocation L, |
| 745 | SourceLocation AliasLoc, |
| 746 | IdentifierInfo *Alias, |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 747 | SourceRange QualifierRange, |
| 748 | NestedNameSpecifier *Qualifier, |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 749 | SourceLocation IdentLoc, |
| 750 | NamedDecl *Namespace) { |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 751 | return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange, |
| 752 | Qualifier, IdentLoc, Namespace); |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 755 | UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, |
| 756 | SourceLocation L, SourceRange NNR, SourceLocation TargetNL, |
| 757 | SourceLocation UL, NamedDecl* Target, |
| 758 | NestedNameSpecifier* TargetNNS, bool IsTypeNameArg) { |
| 759 | return new (C) UsingDecl(DC, L, NNR, TargetNL, UL, Target, |
| 760 | TargetNNS, IsTypeNameArg); |
| 761 | } |
| 762 | |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 763 | StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC, |
| 764 | SourceLocation L, Expr *AssertExpr, |
| 765 | StringLiteral *Message) { |
| 766 | return new (C) StaticAssertDecl(DC, L, AssertExpr, Message); |
| 767 | } |
| 768 | |
| 769 | void StaticAssertDecl::Destroy(ASTContext& C) { |
| 770 | AssertExpr->Destroy(C); |
| 771 | Message->Destroy(C); |
| 772 | this->~StaticAssertDecl(); |
| 773 | C.Deallocate((void *)this); |
| 774 | } |
| 775 | |
| 776 | StaticAssertDecl::~StaticAssertDecl() { |
| 777 | } |
| 778 | |
Anders Carlsson | 05bf2c7 | 2009-03-26 23:46:50 +0000 | [diff] [blame] | 779 | static const char *getAccessName(AccessSpecifier AS) { |
| 780 | switch (AS) { |
| 781 | default: |
| 782 | case AS_none: |
| 783 | assert("Invalid access specifier!"); |
| 784 | return 0; |
| 785 | case AS_public: |
| 786 | return "public"; |
| 787 | case AS_private: |
| 788 | return "private"; |
| 789 | case AS_protected: |
| 790 | return "protected"; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, |
| 795 | AccessSpecifier AS) { |
| 796 | return DB << getAccessName(AS); |
| 797 | } |
| 798 | |
| 799 | |