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