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