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