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 | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 18 | #include "clang/AST/TypeLoc.h" |
Douglas Gregor | 7d7e672 | 2008-11-12 23:21:09 +0000 | [diff] [blame] | 19 | #include "clang/Basic/IdentifierTable.h" |
Douglas Gregor | fdfab6b | 2008-12-23 21:31:30 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
Fariborz Jahanian | faebcbb | 2009-09-12 19:52:10 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallPtrSet.h" |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | // Decl Allocation/Deallocation Method Implementations |
| 26 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 27 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 28 | CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D) |
| 29 | : UserDeclaredConstructor(false), UserDeclaredCopyConstructor(false), |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 30 | UserDeclaredCopyAssignment(false), UserDeclaredDestructor(false), |
Eli Friedman | 97c134e | 2009-08-15 22:23:00 +0000 | [diff] [blame] | 31 | Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false), |
| 32 | Abstract(false), HasTrivialConstructor(true), |
| 33 | HasTrivialCopyConstructor(true), HasTrivialCopyAssignment(true), |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 34 | HasTrivialDestructor(true), ComputedVisibleConversions(false), |
| 35 | Bases(0), NumBases(0), VBases(0), NumVBases(0), |
John McCall | d60e22e | 2010-03-12 01:19:31 +0000 | [diff] [blame] | 36 | Definition(D), FirstFriend(0) { |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, DeclContext *DC, |
| 40 | SourceLocation L, IdentifierInfo *Id, |
| 41 | CXXRecordDecl *PrevDecl, |
| 42 | SourceLocation TKL) |
| 43 | : RecordDecl(K, TK, DC, L, Id, PrevDecl, TKL), |
| 44 | DefinitionData(PrevDecl ? PrevDecl->DefinitionData : 0), |
Douglas Gregor | d475b8d | 2009-03-25 21:17:03 +0000 | [diff] [blame] | 45 | TemplateOrInstantiation() { } |
Douglas Gregor | 7d7e672 | 2008-11-12 23:21:09 +0000 | [diff] [blame] | 46 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 47 | CXXRecordDecl *CXXRecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC, |
| 48 | SourceLocation L, IdentifierInfo *Id, |
Douglas Gregor | 741dd9a | 2009-07-21 14:46:17 +0000 | [diff] [blame] | 49 | SourceLocation TKL, |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 50 | CXXRecordDecl* PrevDecl, |
| 51 | bool DelayTypeCreation) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | CXXRecordDecl* R = new (C) CXXRecordDecl(CXXRecord, TK, DC, L, Id, |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 53 | PrevDecl, TKL); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 55 | // FIXME: DelayTypeCreation seems like such a hack |
Douglas Gregor | aafc0cc | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 56 | if (!DelayTypeCreation) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | C.getTypeDeclType(R, PrevDecl); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 58 | return R; |
| 59 | } |
| 60 | |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 61 | CXXRecordDecl::~CXXRecordDecl() { |
Fariborz Jahanian | 5ffcd7b | 2009-07-02 18:26:15 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | void CXXRecordDecl::Destroy(ASTContext &C) { |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 65 | if (data().Definition == this) { |
| 66 | C.Deallocate(data().Bases); |
| 67 | C.Deallocate(data().VBases); |
| 68 | C.Deallocate(&data()); |
| 69 | } |
Fariborz Jahanian | 5ffcd7b | 2009-07-02 18:26:15 +0000 | [diff] [blame] | 70 | this->RecordDecl::Destroy(C); |
Douglas Gregor | f8268ae | 2008-10-22 17:49:05 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | void |
Douglas Gregor | 2d5b703 | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 74 | CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases, |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 75 | unsigned NumBases) { |
Douglas Gregor | 2d5b703 | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 76 | ASTContext &C = getASTContext(); |
| 77 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | // C++ [dcl.init.aggr]p1: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 79 | // An aggregate is an array or a class (clause 9) with [...] |
| 80 | // no base classes [...]. |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 81 | data().Aggregate = false; |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 82 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 83 | if (data().Bases) |
| 84 | C.Deallocate(data().Bases); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 86 | // The set of seen virtual base types. |
Anders Carlsson | 1c36393 | 2010-03-29 19:49:09 +0000 | [diff] [blame] | 87 | llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes; |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 88 | |
| 89 | // The virtual bases of this class. |
| 90 | llvm::SmallVector<const CXXBaseSpecifier *, 8> VBases; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 92 | data().Bases = new(C) CXXBaseSpecifier [NumBases]; |
| 93 | data().NumBases = NumBases; |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 94 | for (unsigned i = 0; i < NumBases; ++i) { |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 95 | data().Bases[i] = *Bases[i]; |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 96 | // Keep track of inherited vbases for this base class. |
| 97 | const CXXBaseSpecifier *Base = Bases[i]; |
| 98 | QualType BaseType = Base->getType(); |
Douglas Gregor | 5fe8c04 | 2010-02-27 00:25:28 +0000 | [diff] [blame] | 99 | // Skip dependent types; we can't do any checking on them now. |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 100 | if (BaseType->isDependentType()) |
| 101 | continue; |
| 102 | CXXRecordDecl *BaseClassDecl |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 103 | = cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 104 | |
| 105 | // Now go through all virtual bases of this base and add them. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | for (CXXRecordDecl::base_class_iterator VBase = |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 107 | BaseClassDecl->vbases_begin(), |
| 108 | E = BaseClassDecl->vbases_end(); VBase != E; ++VBase) { |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 109 | // Add this base if it's not already in the list. |
Anders Carlsson | 1c36393 | 2010-03-29 19:49:09 +0000 | [diff] [blame] | 110 | if (SeenVBaseTypes.insert(C.getCanonicalType(VBase->getType()))) |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 111 | VBases.push_back(VBase); |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 112 | } |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 113 | |
| 114 | if (Base->isVirtual()) { |
| 115 | // Add this base if it's not already in the list. |
Anders Carlsson | 1c36393 | 2010-03-29 19:49:09 +0000 | [diff] [blame] | 116 | if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType))) |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 117 | VBases.push_back(Base); |
| 118 | } |
| 119 | |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 120 | } |
Anders Carlsson | 6f6de73 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 121 | |
| 122 | if (VBases.empty()) |
| 123 | return; |
| 124 | |
| 125 | // Create base specifier for any direct or indirect virtual bases. |
| 126 | data().VBases = new (C) CXXBaseSpecifier[VBases.size()]; |
| 127 | data().NumVBases = VBases.size(); |
| 128 | for (int I = 0, E = VBases.size(); I != E; ++I) { |
| 129 | QualType VBaseType = VBases[I]->getType(); |
| 130 | |
| 131 | // Skip dependent types; we can't do any checking on them now. |
| 132 | if (VBaseType->isDependentType()) |
| 133 | continue; |
| 134 | |
| 135 | CXXRecordDecl *VBaseClassDecl |
| 136 | = cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl()); |
| 137 | |
| 138 | data().VBases[I] = |
| 139 | CXXBaseSpecifier(VBaseClassDecl->getSourceRange(), true, |
| 140 | VBaseClassDecl->getTagKind() == RecordDecl::TK_class, |
| 141 | VBases[I]->getAccessSpecifier(), VBaseType); |
Fariborz Jahanian | 40c072f | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 142 | } |
Douglas Gregor | 57c856b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Douglas Gregor | 9edad9b | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 145 | /// Callback function for CXXRecordDecl::forallBases that acknowledges |
| 146 | /// that it saw a base class. |
| 147 | static bool SawBase(const CXXRecordDecl *, void *) { |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | bool CXXRecordDecl::hasAnyDependentBases() const { |
| 152 | if (!isDependentContext()) |
| 153 | return false; |
| 154 | |
| 155 | return !forallBases(SawBase, 0); |
| 156 | } |
| 157 | |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 158 | bool CXXRecordDecl::hasConstCopyConstructor(ASTContext &Context) const { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 159 | return getCopyConstructor(Context, Qualifiers::Const) != 0; |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 162 | CXXConstructorDecl *CXXRecordDecl::getCopyConstructor(ASTContext &Context, |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 163 | unsigned TypeQuals) const{ |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 164 | QualType ClassType |
| 165 | = Context.getTypeDeclType(const_cast<CXXRecordDecl*>(this)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | DeclarationName ConstructorName |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 167 | = Context.DeclarationNames.getCXXConstructorName( |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 168 | Context.getCanonicalType(ClassType)); |
| 169 | unsigned FoundTQs; |
Douglas Gregor | fdfab6b | 2008-12-23 21:31:30 +0000 | [diff] [blame] | 170 | DeclContext::lookup_const_iterator Con, ConEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 171 | for (llvm::tie(Con, ConEnd) = this->lookup(ConstructorName); |
Douglas Gregor | fdfab6b | 2008-12-23 21:31:30 +0000 | [diff] [blame] | 172 | Con != ConEnd; ++Con) { |
Douglas Gregor | d93bacf | 2009-09-04 14:46:39 +0000 | [diff] [blame] | 173 | // C++ [class.copy]p2: |
| 174 | // A non-template constructor for class X is a copy constructor if [...] |
| 175 | if (isa<FunctionTemplateDecl>(*Con)) |
| 176 | continue; |
| 177 | |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 178 | if (cast<CXXConstructorDecl>(*Con)->isCopyConstructor(FoundTQs)) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 179 | if (((TypeQuals & Qualifiers::Const) == (FoundTQs & Qualifiers::Const)) || |
| 180 | (!(TypeQuals & Qualifiers::Const) && (FoundTQs & Qualifiers::Const))) |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 181 | return cast<CXXConstructorDecl>(*Con); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 183 | } |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 184 | } |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 185 | return 0; |
Douglas Gregor | 396b7cd | 2008-11-03 17:51:48 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 188 | bool CXXRecordDecl::hasConstCopyAssignment(ASTContext &Context, |
| 189 | const CXXMethodDecl *& MD) const { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 190 | QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType( |
| 191 | const_cast<CXXRecordDecl*>(this))); |
| 192 | DeclarationName OpName =Context.DeclarationNames.getCXXOperatorName(OO_Equal); |
| 193 | |
| 194 | DeclContext::lookup_const_iterator Op, OpEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 195 | for (llvm::tie(Op, OpEnd) = this->lookup(OpName); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 196 | Op != OpEnd; ++Op) { |
| 197 | // C++ [class.copy]p9: |
| 198 | // A user-declared copy assignment operator is a non-static non-template |
| 199 | // member function of class X with exactly one parameter of type X, X&, |
| 200 | // const X&, volatile X& or const volatile X&. |
Douglas Gregor | 682054c | 2009-10-30 22:48:49 +0000 | [diff] [blame] | 201 | const CXXMethodDecl* Method = dyn_cast<CXXMethodDecl>(*Op); |
| 202 | if (!Method) |
| 203 | continue; |
| 204 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 205 | if (Method->isStatic()) |
| 206 | continue; |
Douglas Gregor | 77da3f4 | 2009-10-13 23:45:19 +0000 | [diff] [blame] | 207 | if (Method->getPrimaryTemplate()) |
| 208 | continue; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 209 | const FunctionProtoType *FnType = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 210 | Method->getType()->getAs<FunctionProtoType>(); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 211 | assert(FnType && "Overloaded operator has no prototype."); |
| 212 | // Don't assert on this; an invalid decl might have been left in the AST. |
| 213 | if (FnType->getNumArgs() != 1 || FnType->isVariadic()) |
| 214 | continue; |
| 215 | bool AcceptsConst = true; |
| 216 | QualType ArgType = FnType->getArgType(0); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 217 | if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) { |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 218 | ArgType = Ref->getPointeeType(); |
Douglas Gregor | 2ff4478 | 2009-03-20 20:21:37 +0000 | [diff] [blame] | 219 | // Is it a non-const lvalue reference? |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 220 | if (!ArgType.isConstQualified()) |
| 221 | AcceptsConst = false; |
| 222 | } |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 223 | if (!Context.hasSameUnqualifiedType(ArgType, ClassType)) |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 224 | continue; |
Fariborz Jahanian | 0270b8a | 2009-08-12 23:34:46 +0000 | [diff] [blame] | 225 | MD = Method; |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 226 | // We have a single argument of type cv X or cv X&, i.e. we've found the |
| 227 | // copy assignment operator. Return whether it accepts const arguments. |
| 228 | return AcceptsConst; |
| 229 | } |
| 230 | assert(isInvalidDecl() && |
| 231 | "No copy assignment operator declared in valid code."); |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | CXXRecordDecl::addedConstructor(ASTContext &Context, |
Douglas Gregor | 9e7d9de | 2008-12-15 21:24:18 +0000 | [diff] [blame] | 237 | CXXConstructorDecl *ConDecl) { |
Fariborz Jahanian | 8bc3fa4 | 2009-06-17 22:44:31 +0000 | [diff] [blame] | 238 | assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl"); |
| 239 | // Note that we have a user-declared constructor. |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 240 | data().UserDeclaredConstructor = true; |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 241 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | // C++ [dcl.init.aggr]p1: |
Fariborz Jahanian | 8bc3fa4 | 2009-06-17 22:44:31 +0000 | [diff] [blame] | 243 | // An aggregate is an array or a class (clause 9) with no |
| 244 | // user-declared constructors (12.1) [...]. |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 245 | data().Aggregate = false; |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 246 | |
Fariborz Jahanian | 8bc3fa4 | 2009-06-17 22:44:31 +0000 | [diff] [blame] | 247 | // C++ [class]p4: |
| 248 | // A POD-struct is an aggregate class [...] |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 249 | data().PlainOldData = false; |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 250 | |
Fariborz Jahanian | 8bc3fa4 | 2009-06-17 22:44:31 +0000 | [diff] [blame] | 251 | // C++ [class.ctor]p5: |
| 252 | // A constructor is trivial if it is an implicitly-declared default |
| 253 | // constructor. |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 254 | // FIXME: C++0x: don't do this for "= default" default constructors. |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 255 | data().HasTrivialConstructor = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 256 | |
Fariborz Jahanian | 8bc3fa4 | 2009-06-17 22:44:31 +0000 | [diff] [blame] | 257 | // Note when we have a user-declared copy constructor, which will |
| 258 | // suppress the implicit declaration of a copy constructor. |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 259 | if (ConDecl->isCopyConstructor()) { |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 260 | data().UserDeclaredCopyConstructor = true; |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 261 | |
| 262 | // C++ [class.copy]p6: |
| 263 | // A copy constructor is trivial if it is implicitly declared. |
| 264 | // FIXME: C++0x: don't do this for "= default" copy constructors. |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 265 | data().HasTrivialCopyConstructor = false; |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 266 | } |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 269 | void CXXRecordDecl::addedAssignmentOperator(ASTContext &Context, |
| 270 | CXXMethodDecl *OpDecl) { |
| 271 | // We're interested specifically in copy assignment operators. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 272 | const FunctionProtoType *FnType = OpDecl->getType()->getAs<FunctionProtoType>(); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 273 | assert(FnType && "Overloaded operator has no proto function type."); |
| 274 | assert(FnType->getNumArgs() == 1 && !FnType->isVariadic()); |
Douglas Gregor | 77da3f4 | 2009-10-13 23:45:19 +0000 | [diff] [blame] | 275 | |
| 276 | // Copy assignment operators must be non-templates. |
| 277 | if (OpDecl->getPrimaryTemplate() || OpDecl->getDescribedFunctionTemplate()) |
| 278 | return; |
| 279 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 280 | QualType ArgType = FnType->getArgType(0); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 281 | if (const LValueReferenceType *Ref = ArgType->getAs<LValueReferenceType>()) |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 282 | ArgType = Ref->getPointeeType(); |
| 283 | |
| 284 | ArgType = ArgType.getUnqualifiedType(); |
| 285 | QualType ClassType = Context.getCanonicalType(Context.getTypeDeclType( |
| 286 | const_cast<CXXRecordDecl*>(this))); |
| 287 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 288 | if (!Context.hasSameUnqualifiedType(ClassType, ArgType)) |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 289 | return; |
| 290 | |
| 291 | // This is a copy assignment operator. |
Eli Friedman | 88fad63 | 2009-11-07 00:02:45 +0000 | [diff] [blame] | 292 | // Note on the decl that it is a copy assignment operator. |
| 293 | OpDecl->setCopyAssignment(true); |
| 294 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 295 | // Suppress the implicit declaration of a copy constructor. |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 296 | data().UserDeclaredCopyAssignment = true; |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 297 | |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 298 | // C++ [class.copy]p11: |
| 299 | // A copy assignment operator is trivial if it is implicitly declared. |
| 300 | // FIXME: C++0x: don't do this for "= default" copy operators. |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 301 | data().HasTrivialCopyAssignment = false; |
Douglas Gregor | 1f2023a | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 302 | |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 303 | // C++ [class]p4: |
| 304 | // A POD-struct is an aggregate class that [...] has no user-defined copy |
| 305 | // assignment operator [...]. |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 306 | data().PlainOldData = false; |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 307 | } |
| 308 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 309 | static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) { |
| 310 | QualType T; |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 311 | if (isa<UsingShadowDecl>(Conv)) |
| 312 | Conv = cast<UsingShadowDecl>(Conv)->getTargetDecl(); |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 313 | if (FunctionTemplateDecl *ConvTemp = dyn_cast<FunctionTemplateDecl>(Conv)) |
| 314 | T = ConvTemp->getTemplatedDecl()->getResultType(); |
| 315 | else |
| 316 | T = cast<CXXConversionDecl>(Conv)->getConversionType(); |
| 317 | return Context.getCanonicalType(T); |
Fariborz Jahanian | 0351a1e | 2009-10-07 20:43:36 +0000 | [diff] [blame] | 318 | } |
| 319 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 320 | /// Collect the visible conversions of a base class. |
| 321 | /// |
| 322 | /// \param Base a base class of the class we're considering |
| 323 | /// \param InVirtual whether this base class is a virtual base (or a base |
| 324 | /// of a virtual base) |
| 325 | /// \param Access the access along the inheritance path to this base |
| 326 | /// \param ParentHiddenTypes the conversions provided by the inheritors |
| 327 | /// of this base |
| 328 | /// \param Output the set to which to add conversions from non-virtual bases |
| 329 | /// \param VOutput the set to which to add conversions from virtual bases |
| 330 | /// \param HiddenVBaseCs the set of conversions which were hidden in a |
| 331 | /// virtual base along some inheritance path |
| 332 | static void CollectVisibleConversions(ASTContext &Context, |
| 333 | CXXRecordDecl *Record, |
| 334 | bool InVirtual, |
| 335 | AccessSpecifier Access, |
| 336 | const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes, |
| 337 | UnresolvedSetImpl &Output, |
| 338 | UnresolvedSetImpl &VOutput, |
| 339 | llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) { |
| 340 | // The set of types which have conversions in this class or its |
| 341 | // subclasses. As an optimization, we don't copy the derived set |
| 342 | // unless it might change. |
| 343 | const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes; |
| 344 | llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer; |
| 345 | |
| 346 | // Collect the direct conversions and figure out which conversions |
| 347 | // will be hidden in the subclasses. |
| 348 | UnresolvedSetImpl &Cs = *Record->getConversionFunctions(); |
| 349 | if (!Cs.empty()) { |
| 350 | HiddenTypesBuffer = ParentHiddenTypes; |
| 351 | HiddenTypes = &HiddenTypesBuffer; |
| 352 | |
| 353 | for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) { |
| 354 | bool Hidden = |
| 355 | !HiddenTypesBuffer.insert(GetConversionType(Context, I.getDecl())); |
| 356 | |
| 357 | // If this conversion is hidden and we're in a virtual base, |
| 358 | // remember that it's hidden along some inheritance path. |
| 359 | if (Hidden && InVirtual) |
| 360 | HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())); |
| 361 | |
| 362 | // If this conversion isn't hidden, add it to the appropriate output. |
| 363 | else if (!Hidden) { |
| 364 | AccessSpecifier IAccess |
| 365 | = CXXRecordDecl::MergeAccess(Access, I.getAccess()); |
| 366 | |
| 367 | if (InVirtual) |
| 368 | VOutput.addDecl(I.getDecl(), IAccess); |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 369 | else |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 370 | Output.addDecl(I.getDecl(), IAccess); |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | } |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 374 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 375 | // Collect information recursively from any base classes. |
| 376 | for (CXXRecordDecl::base_class_iterator |
| 377 | I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) { |
| 378 | const RecordType *RT = I->getType()->getAs<RecordType>(); |
| 379 | if (!RT) continue; |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 380 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 381 | AccessSpecifier BaseAccess |
| 382 | = CXXRecordDecl::MergeAccess(Access, I->getAccessSpecifier()); |
| 383 | bool BaseInVirtual = InVirtual || I->isVirtual(); |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 384 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 385 | CXXRecordDecl *Base = cast<CXXRecordDecl>(RT->getDecl()); |
| 386 | CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess, |
| 387 | *HiddenTypes, Output, VOutput, HiddenVBaseCs); |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 388 | } |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 389 | } |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 390 | |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 391 | /// Collect the visible conversions of a class. |
| 392 | /// |
| 393 | /// This would be extremely straightforward if it weren't for virtual |
| 394 | /// bases. It might be worth special-casing that, really. |
| 395 | static void CollectVisibleConversions(ASTContext &Context, |
| 396 | CXXRecordDecl *Record, |
| 397 | UnresolvedSetImpl &Output) { |
| 398 | // The collection of all conversions in virtual bases that we've |
| 399 | // found. These will be added to the output as long as they don't |
| 400 | // appear in the hidden-conversions set. |
| 401 | UnresolvedSet<8> VBaseCs; |
| 402 | |
| 403 | // The set of conversions in virtual bases that we've determined to |
| 404 | // be hidden. |
| 405 | llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs; |
| 406 | |
| 407 | // The set of types hidden by classes derived from this one. |
| 408 | llvm::SmallPtrSet<CanQualType, 8> HiddenTypes; |
| 409 | |
| 410 | // Go ahead and collect the direct conversions and add them to the |
| 411 | // hidden-types set. |
| 412 | UnresolvedSetImpl &Cs = *Record->getConversionFunctions(); |
| 413 | Output.append(Cs.begin(), Cs.end()); |
| 414 | for (UnresolvedSetIterator I = Cs.begin(), E = Cs.end(); I != E; ++I) |
| 415 | HiddenTypes.insert(GetConversionType(Context, I.getDecl())); |
| 416 | |
| 417 | // Recursively collect conversions from base classes. |
| 418 | for (CXXRecordDecl::base_class_iterator |
| 419 | I = Record->bases_begin(), E = Record->bases_end(); I != E; ++I) { |
| 420 | const RecordType *RT = I->getType()->getAs<RecordType>(); |
| 421 | if (!RT) continue; |
| 422 | |
| 423 | CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()), |
| 424 | I->isVirtual(), I->getAccessSpecifier(), |
| 425 | HiddenTypes, Output, VBaseCs, HiddenVBaseCs); |
| 426 | } |
| 427 | |
| 428 | // Add any unhidden conversions provided by virtual bases. |
| 429 | for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end(); |
| 430 | I != E; ++I) { |
| 431 | if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()))) |
| 432 | Output.addDecl(I.getDecl(), I.getAccess()); |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 433 | } |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /// getVisibleConversionFunctions - get all conversion functions visible |
| 437 | /// in current class; including conversion function templates. |
John McCall | eec51cf | 2010-01-20 00:46:10 +0000 | [diff] [blame] | 438 | const UnresolvedSetImpl *CXXRecordDecl::getVisibleConversionFunctions() { |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 439 | // If root class, all conversions are visible. |
| 440 | if (bases_begin() == bases_end()) |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 441 | return &data().Conversions; |
Fariborz Jahanian | 6250921 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 442 | // If visible conversion list is already evaluated, return it. |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 443 | if (data().ComputedVisibleConversions) |
| 444 | return &data().VisibleConversions; |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 445 | CollectVisibleConversions(getASTContext(), this, data().VisibleConversions); |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 446 | data().ComputedVisibleConversions = true; |
| 447 | return &data().VisibleConversions; |
Fariborz Jahanian | 5346278 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 448 | } |
| 449 | |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 450 | #ifndef NDEBUG |
| 451 | void CXXRecordDecl::CheckConversionFunction(NamedDecl *ConvDecl) { |
John McCall | b05b5f3 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 452 | assert(ConvDecl->getDeclContext() == this && |
| 453 | "conversion function does not belong to this record"); |
| 454 | |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 455 | ConvDecl = ConvDecl->getUnderlyingDecl(); |
| 456 | if (FunctionTemplateDecl *Temp = dyn_cast<FunctionTemplateDecl>(ConvDecl)) { |
| 457 | assert(isa<CXXConversionDecl>(Temp->getTemplatedDecl())); |
| 458 | } else { |
| 459 | assert(isa<CXXConversionDecl>(ConvDecl)); |
| 460 | } |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 461 | } |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 462 | #endif |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 463 | |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 464 | void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) { |
| 465 | // This operation is O(N) but extremely rare. Sema only uses it to |
| 466 | // remove UsingShadowDecls in a class that were followed by a direct |
| 467 | // declaration, e.g.: |
| 468 | // class A : B { |
| 469 | // using B::operator int; |
| 470 | // operator int(); |
| 471 | // }; |
| 472 | // This is uncommon by itself and even more uncommon in conjunction |
| 473 | // with sufficiently large numbers of directly-declared conversions |
| 474 | // that asymptotic behavior matters. |
| 475 | |
| 476 | UnresolvedSetImpl &Convs = *getConversionFunctions(); |
| 477 | for (unsigned I = 0, E = Convs.size(); I != E; ++I) { |
| 478 | if (Convs[I].getDecl() == ConvDecl) { |
| 479 | Convs.erase(I); |
| 480 | assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end() |
| 481 | && "conversion was found multiple times in unresolved set"); |
| 482 | return; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | llvm_unreachable("conversion not found in set!"); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 487 | } |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 488 | |
Fariborz Jahanian | e7184df | 2009-12-03 18:44:40 +0000 | [diff] [blame] | 489 | void CXXRecordDecl::setMethodAsVirtual(FunctionDecl *Method) { |
| 490 | Method->setVirtualAsWritten(true); |
| 491 | setAggregate(false); |
| 492 | setPOD(false); |
| 493 | setEmpty(false); |
| 494 | setPolymorphic(true); |
| 495 | setHasTrivialConstructor(false); |
| 496 | setHasTrivialCopyConstructor(false); |
| 497 | setHasTrivialCopyAssignment(false); |
| 498 | } |
| 499 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 500 | CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 501 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 502 | return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom()); |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 507 | MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const { |
| 508 | return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>(); |
| 509 | } |
| 510 | |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 511 | void |
| 512 | CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD, |
| 513 | TemplateSpecializationKind TSK) { |
| 514 | assert(TemplateOrInstantiation.isNull() && |
| 515 | "Previous template or instantiation?"); |
| 516 | assert(!isa<ClassTemplateSpecializationDecl>(this)); |
| 517 | TemplateOrInstantiation |
| 518 | = new (getASTContext()) MemberSpecializationInfo(RD, TSK); |
| 519 | } |
| 520 | |
Anders Carlsson | b13e357 | 2009-12-07 06:33:48 +0000 | [diff] [blame] | 521 | TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{ |
| 522 | if (const ClassTemplateSpecializationDecl *Spec |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 523 | = dyn_cast<ClassTemplateSpecializationDecl>(this)) |
| 524 | return Spec->getSpecializationKind(); |
| 525 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 526 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 527 | return MSInfo->getTemplateSpecializationKind(); |
| 528 | |
| 529 | return TSK_Undeclared; |
| 530 | } |
| 531 | |
| 532 | void |
| 533 | CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) { |
| 534 | if (ClassTemplateSpecializationDecl *Spec |
| 535 | = dyn_cast<ClassTemplateSpecializationDecl>(this)) { |
| 536 | Spec->setSpecializationKind(TSK); |
| 537 | return; |
| 538 | } |
| 539 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 540 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) { |
Douglas Gregor | f6b1185 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 541 | MSInfo->setTemplateSpecializationKind(TSK); |
| 542 | return; |
| 543 | } |
| 544 | |
| 545 | assert(false && "Not a class template or member class specialization"); |
| 546 | } |
| 547 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 548 | CXXConstructorDecl * |
| 549 | CXXRecordDecl::getDefaultConstructor(ASTContext &Context) { |
| 550 | QualType ClassType = Context.getTypeDeclType(this); |
| 551 | DeclarationName ConstructorName |
| 552 | = Context.DeclarationNames.getCXXConstructorName( |
| 553 | Context.getCanonicalType(ClassType.getUnqualifiedType())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 554 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 555 | DeclContext::lookup_const_iterator Con, ConEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 556 | for (llvm::tie(Con, ConEnd) = lookup(ConstructorName); |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 557 | Con != ConEnd; ++Con) { |
Douglas Gregor | d93bacf | 2009-09-04 14:46:39 +0000 | [diff] [blame] | 558 | // FIXME: In C++0x, a constructor template can be a default constructor. |
| 559 | if (isa<FunctionTemplateDecl>(*Con)) |
| 560 | continue; |
| 561 | |
Fariborz Jahanian | f8dcb86 | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 562 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(*Con); |
| 563 | if (Constructor->isDefaultConstructor()) |
| 564 | return Constructor; |
| 565 | } |
| 566 | return 0; |
| 567 | } |
| 568 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 569 | CXXDestructorDecl *CXXRecordDecl::getDestructor(ASTContext &Context) const { |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 570 | QualType ClassType = Context.getTypeDeclType(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 571 | |
| 572 | DeclarationName Name |
Douglas Gregor | 50d62d1 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 573 | = Context.DeclarationNames.getCXXDestructorName( |
| 574 | Context.getCanonicalType(ClassType)); |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 575 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 576 | DeclContext::lookup_const_iterator I, E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | llvm::tie(I, E) = lookup(Name); |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 578 | assert(I != E && "Did not find a destructor!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 579 | |
Anders Carlsson | 5ec02ae | 2009-12-02 17:15:43 +0000 | [diff] [blame] | 580 | CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(*I); |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 581 | assert(++I == E && "Found more than one destructor!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 582 | |
Anders Carlsson | 7267c16 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 583 | return Dtor; |
| 584 | } |
| 585 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 586 | CXXMethodDecl * |
| 587 | CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 10bd368 | 2008-11-17 22:58:34 +0000 | [diff] [blame] | 588 | SourceLocation L, DeclarationName N, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 589 | QualType T, TypeSourceInfo *TInfo, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 590 | bool isStatic, bool isInline) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 591 | return new (C) CXXMethodDecl(CXXMethod, RD, L, N, T, TInfo, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 592 | isStatic, isInline); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 595 | bool CXXMethodDecl::isUsualDeallocationFunction() const { |
| 596 | if (getOverloadedOperator() != OO_Delete && |
| 597 | getOverloadedOperator() != OO_Array_Delete) |
| 598 | return false; |
Douglas Gregor | 6d90870 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 599 | |
| 600 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 601 | // A template instance is never a usual deallocation function, |
| 602 | // regardless of its signature. |
| 603 | if (getPrimaryTemplate()) |
| 604 | return false; |
| 605 | |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 606 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 607 | // If a class T has a member deallocation function named operator delete |
| 608 | // with exactly one parameter, then that function is a usual (non-placement) |
| 609 | // deallocation function. [...] |
| 610 | if (getNumParams() == 1) |
| 611 | return true; |
| 612 | |
| 613 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 614 | // [...] If class T does not declare such an operator delete but does |
| 615 | // declare a member deallocation function named operator delete with |
| 616 | // exactly two parameters, the second of which has type std::size_t (18.1), |
| 617 | // then this function is a usual deallocation function. |
| 618 | ASTContext &Context = getASTContext(); |
| 619 | if (getNumParams() != 2 || |
Chandler Carruth | e228ba9 | 2010-02-08 18:54:05 +0000 | [diff] [blame] | 620 | !Context.hasSameUnqualifiedType(getParamDecl(1)->getType(), |
| 621 | Context.getSizeType())) |
Douglas Gregor | 9091656 | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 622 | return false; |
| 623 | |
| 624 | // This function is a usual deallocation function if there are no |
| 625 | // single-parameter deallocation functions of the same kind. |
| 626 | for (DeclContext::lookup_const_result R = getDeclContext()->lookup(getDeclName()); |
| 627 | R.first != R.second; ++R.first) { |
| 628 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*R.first)) |
| 629 | if (FD->getNumParams() == 1) |
| 630 | return false; |
| 631 | } |
| 632 | |
| 633 | return true; |
| 634 | } |
| 635 | |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 636 | void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) { |
Anders Carlsson | 3aaf486 | 2009-12-04 05:51:56 +0000 | [diff] [blame] | 637 | assert(MD->isCanonicalDecl() && "Method is not canonical!"); |
Anders Carlsson | c076c45 | 2010-01-30 17:42:34 +0000 | [diff] [blame] | 638 | assert(!MD->getParent()->isDependentContext() && |
| 639 | "Can't add an overridden method to a class template!"); |
| 640 | |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 641 | getASTContext().addOverriddenMethod(this, MD); |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const { |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 645 | return getASTContext().overridden_methods_begin(this); |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const { |
Douglas Gregor | 7d10b7e | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 649 | return getASTContext().overridden_methods_end(this); |
Anders Carlsson | 05eb244 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 652 | QualType CXXMethodDecl::getThisType(ASTContext &C) const { |
Argyrios Kyrtzidis | b0d178d | 2008-10-24 22:28:18 +0000 | [diff] [blame] | 653 | // C++ 9.3.2p1: The type of this in a member function of a class X is X*. |
| 654 | // If the member function is declared const, the type of this is const X*, |
| 655 | // if the member function is declared volatile, the type of this is |
| 656 | // volatile X*, and if the member function is declared const volatile, |
| 657 | // the type of this is const volatile X*. |
| 658 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 659 | assert(isInstance() && "No 'this' for static methods!"); |
Anders Carlsson | 31a0875 | 2009-06-13 02:59:33 +0000 | [diff] [blame] | 660 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 661 | QualType ClassTy = C.getTypeDeclType(getParent()); |
| 662 | |
| 663 | // Aesthetically we prefer not to synthesize a type as the |
| 664 | // InjectedClassNameType of a template pattern: injected class names |
| 665 | // are printed without template arguments, which might |
| 666 | // surprise/confuse/distract our poor users if they didn't |
| 667 | // explicitly write one. |
| 668 | if (isa<InjectedClassNameType>(ClassTy)) |
| 669 | ClassTy = cast<InjectedClassNameType>(ClassTy)->getUnderlyingType(); |
| 670 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 671 | ClassTy = C.getQualifiedType(ClassTy, |
| 672 | Qualifiers::fromCVRMask(getTypeQualifiers())); |
Anders Carlsson | 4e57992 | 2009-07-10 21:35:09 +0000 | [diff] [blame] | 673 | return C.getPointerType(ClassTy); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Eli Friedman | d7d7f67 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 676 | bool CXXMethodDecl::hasInlineBody() const { |
Douglas Gregor | bd6d619 | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 677 | // If this function is a template instantiation, look at the template from |
| 678 | // which it was instantiated. |
| 679 | const FunctionDecl *CheckFn = getTemplateInstantiationPattern(); |
| 680 | if (!CheckFn) |
| 681 | CheckFn = this; |
| 682 | |
Eli Friedman | d7d7f67 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 683 | const FunctionDecl *fn; |
Douglas Gregor | bd6d619 | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 684 | return CheckFn->getBody(fn) && !fn->isOutOfLine(); |
Eli Friedman | d7d7f67 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 687 | CXXBaseOrMemberInitializer:: |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 688 | CXXBaseOrMemberInitializer(ASTContext &Context, |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame^] | 689 | TypeSourceInfo *TInfo, bool IsVirtual, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 690 | SourceLocation L, Expr *Init, SourceLocation R) |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame^] | 691 | : BaseOrMember(TInfo), Init(Init), AnonUnionMember(0), IsVirtual(IsVirtual), |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 692 | LParenLoc(L), RParenLoc(R) |
| 693 | { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | CXXBaseOrMemberInitializer:: |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 697 | CXXBaseOrMemberInitializer(ASTContext &Context, |
| 698 | FieldDecl *Member, SourceLocation MemberLoc, |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 699 | SourceLocation L, Expr *Init, SourceLocation R) |
| 700 | : BaseOrMember(Member), MemberLocation(MemberLoc), Init(Init), |
| 701 | AnonUnionMember(0), LParenLoc(L), RParenLoc(R) |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 702 | { |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 703 | } |
| 704 | |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 705 | void CXXBaseOrMemberInitializer::Destroy(ASTContext &Context) { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 706 | if (Init) |
| 707 | Init->Destroy(Context); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 708 | this->~CXXBaseOrMemberInitializer(); |
| 709 | } |
| 710 | |
| 711 | TypeLoc CXXBaseOrMemberInitializer::getBaseClassLoc() const { |
| 712 | if (isBaseInitializer()) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 713 | return BaseOrMember.get<TypeSourceInfo*>()->getTypeLoc(); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 714 | else |
| 715 | return TypeLoc(); |
| 716 | } |
| 717 | |
| 718 | Type *CXXBaseOrMemberInitializer::getBaseClass() { |
| 719 | if (isBaseInitializer()) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 720 | return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr(); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 721 | else |
| 722 | return 0; |
| 723 | } |
| 724 | |
| 725 | const Type *CXXBaseOrMemberInitializer::getBaseClass() const { |
| 726 | if (isBaseInitializer()) |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 727 | return BaseOrMember.get<TypeSourceInfo*>()->getType().getTypePtr(); |
Douglas Gregor | 802ab45 | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 728 | else |
| 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | SourceLocation CXXBaseOrMemberInitializer::getSourceLocation() const { |
| 733 | if (isMemberInitializer()) |
| 734 | return getMemberLocation(); |
| 735 | |
| 736 | return getBaseClassLoc().getSourceRange().getBegin(); |
| 737 | } |
| 738 | |
| 739 | SourceRange CXXBaseOrMemberInitializer::getSourceRange() const { |
| 740 | return SourceRange(getSourceLocation(), getRParenLoc()); |
Douglas Gregor | 7ad8390 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 743 | CXXConstructorDecl * |
| 744 | CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 745 | SourceLocation L, DeclarationName N, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 746 | QualType T, TypeSourceInfo *TInfo, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 747 | bool isExplicit, |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 748 | bool isInline, bool isImplicitlyDeclared) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 749 | assert(N.getNameKind() == DeclarationName::CXXConstructorName && |
| 750 | "Name must refer to a constructor"); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 751 | return new (C) CXXConstructorDecl(RD, L, N, T, TInfo, isExplicit, isInline, |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 752 | isImplicitlyDeclared); |
| 753 | } |
| 754 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 755 | bool CXXConstructorDecl::isDefaultConstructor() const { |
| 756 | // C++ [class.ctor]p5: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 757 | // A default constructor for a class X is a constructor of class |
| 758 | // X that can be called without an argument. |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 759 | return (getNumParams() == 0) || |
Anders Carlsson | da3f4e2 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 760 | (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg()); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 763 | bool |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 764 | CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const { |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 765 | // C++ [class.copy]p2: |
Douglas Gregor | 64bffa9 | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 766 | // A non-template constructor for class X is a copy constructor |
| 767 | // if its first parameter is of type X&, const X&, volatile X& or |
| 768 | // const volatile X&, and either there are no other parameters |
| 769 | // or else all other parameters have default arguments (8.3.6). |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 770 | if ((getNumParams() < 1) || |
Douglas Gregor | 77da3f4 | 2009-10-13 23:45:19 +0000 | [diff] [blame] | 771 | (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 772 | (getPrimaryTemplate() != 0) || |
Douglas Gregor | 77da3f4 | 2009-10-13 23:45:19 +0000 | [diff] [blame] | 773 | (getDescribedFunctionTemplate() != 0)) |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 774 | return false; |
| 775 | |
| 776 | const ParmVarDecl *Param = getParamDecl(0); |
| 777 | |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 778 | // Do we have a reference type? Rvalue references don't count. |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 779 | const LValueReferenceType *ParamRefType = |
| 780 | Param->getType()->getAs<LValueReferenceType>(); |
| 781 | if (!ParamRefType) |
| 782 | return false; |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 783 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 784 | // Is it a reference to our class type? |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 785 | ASTContext &Context = getASTContext(); |
| 786 | |
Douglas Gregor | fd47648 | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 787 | CanQualType PointeeType |
| 788 | = Context.getCanonicalType(ParamRefType->getPointeeType()); |
Douglas Gregor | 14e0b3d | 2009-09-15 20:50:23 +0000 | [diff] [blame] | 789 | CanQualType ClassTy |
| 790 | = Context.getCanonicalType(Context.getTagDeclType(getParent())); |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 791 | if (PointeeType.getUnqualifiedType() != ClassTy) |
| 792 | return false; |
| 793 | |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 794 | // FIXME: other qualifiers? |
| 795 | |
Douglas Gregor | 030ff0c | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 796 | // We have a copy constructor. |
| 797 | TypeQuals = PointeeType.getCVRQualifiers(); |
| 798 | return true; |
| 799 | } |
| 800 | |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 801 | bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const { |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 802 | // C++ [class.conv.ctor]p1: |
| 803 | // A constructor declared without the function-specifier explicit |
| 804 | // that can be called with a single parameter specifies a |
| 805 | // conversion from the type of its first parameter to the type of |
| 806 | // its class. Such a constructor is called a converting |
| 807 | // constructor. |
Anders Carlsson | faccd72 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 808 | if (isExplicit() && !AllowExplicit) |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 809 | return false; |
| 810 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 811 | return (getNumParams() == 0 && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 812 | getType()->getAs<FunctionProtoType>()->isVariadic()) || |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 813 | (getNumParams() == 1) || |
Anders Carlsson | ae0b4e7 | 2009-06-06 04:14:07 +0000 | [diff] [blame] | 814 | (getNumParams() > 1 && getParamDecl(1)->hasDefaultArg()); |
Douglas Gregor | 60d62c2 | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 815 | } |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 816 | |
Douglas Gregor | 66724ea | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 817 | bool CXXConstructorDecl::isCopyConstructorLikeSpecialization() const { |
| 818 | if ((getNumParams() < 1) || |
| 819 | (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || |
| 820 | (getPrimaryTemplate() == 0) || |
| 821 | (getDescribedFunctionTemplate() != 0)) |
| 822 | return false; |
| 823 | |
| 824 | const ParmVarDecl *Param = getParamDecl(0); |
| 825 | |
| 826 | ASTContext &Context = getASTContext(); |
| 827 | CanQualType ParamType = Context.getCanonicalType(Param->getType()); |
| 828 | |
| 829 | // Strip off the lvalue reference, if any. |
| 830 | if (CanQual<LValueReferenceType> ParamRefType |
| 831 | = ParamType->getAs<LValueReferenceType>()) |
| 832 | ParamType = ParamRefType->getPointeeType(); |
| 833 | |
| 834 | |
| 835 | // Is it the same as our our class type? |
| 836 | CanQualType ClassTy |
| 837 | = Context.getCanonicalType(Context.getTagDeclType(getParent())); |
| 838 | if (ParamType.getUnqualifiedType() != ClassTy) |
| 839 | return false; |
| 840 | |
| 841 | return true; |
| 842 | } |
| 843 | |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 844 | CXXDestructorDecl * |
| 845 | CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 846 | SourceLocation L, DeclarationName N, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 847 | QualType T, bool isInline, |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 848 | bool isImplicitlyDeclared) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 849 | assert(N.getNameKind() == DeclarationName::CXXDestructorName && |
| 850 | "Name must refer to a destructor"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 851 | return new (C) CXXDestructorDecl(RD, L, N, T, isInline, |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 852 | isImplicitlyDeclared); |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Fariborz Jahanian | d45c363 | 2009-07-01 21:05:43 +0000 | [diff] [blame] | 855 | void |
Fariborz Jahanian | 73b85f3 | 2009-07-01 23:35:25 +0000 | [diff] [blame] | 856 | CXXConstructorDecl::Destroy(ASTContext& C) { |
| 857 | C.Deallocate(BaseOrMemberInitializers); |
Fariborz Jahanian | 0d3c26c | 2009-07-07 16:24:08 +0000 | [diff] [blame] | 858 | CXXMethodDecl::Destroy(C); |
Fariborz Jahanian | 73b85f3 | 2009-07-01 23:35:25 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 861 | CXXConversionDecl * |
| 862 | CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 863 | SourceLocation L, DeclarationName N, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 864 | QualType T, TypeSourceInfo *TInfo, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 865 | bool isInline, bool isExplicit) { |
Douglas Gregor | 2e1cd42 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 866 | assert(N.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 867 | "Name must refer to a conversion function"); |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 868 | return new (C) CXXConversionDecl(RD, L, N, T, TInfo, isInline, isExplicit); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 871 | LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 872 | DeclContext *DC, |
Chris Lattner | 21ef7ae | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 873 | SourceLocation L, |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 874 | LanguageIDs Lang, bool Braces) { |
Steve Naroff | 3e97049 | 2009-01-27 21:25:57 +0000 | [diff] [blame] | 875 | return new (C) LinkageSpecDecl(DC, L, Lang, Braces); |
Douglas Gregor | f44515a | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 876 | } |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 877 | |
| 878 | UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC, |
| 879 | SourceLocation L, |
| 880 | SourceLocation NamespaceLoc, |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 881 | SourceRange QualifierRange, |
| 882 | NestedNameSpecifier *Qualifier, |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 883 | SourceLocation IdentLoc, |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 884 | NamedDecl *Used, |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 885 | DeclContext *CommonAncestor) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 886 | if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Used)) |
| 887 | Used = NS->getOriginalNamespace(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 888 | return new (C) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierRange, |
Douglas Gregor | 8419fa3 | 2009-05-30 06:31:56 +0000 | [diff] [blame] | 889 | Qualifier, IdentLoc, Used, CommonAncestor); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 892 | NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() { |
| 893 | if (NamespaceAliasDecl *NA = |
| 894 | dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace)) |
| 895 | return NA->getNamespace(); |
| 896 | return cast_or_null<NamespaceDecl>(NominatedNamespace); |
| 897 | } |
| 898 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 899 | NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC, |
| 900 | SourceLocation L, |
| 901 | SourceLocation AliasLoc, |
| 902 | IdentifierInfo *Alias, |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 903 | SourceRange QualifierRange, |
| 904 | NestedNameSpecifier *Qualifier, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 905 | SourceLocation IdentLoc, |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 906 | NamedDecl *Namespace) { |
Sebastian Redl | eb0d8c9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 907 | if (NamespaceDecl *NS = dyn_cast_or_null<NamespaceDecl>(Namespace)) |
| 908 | Namespace = NS->getOriginalNamespace(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 909 | return new (C) NamespaceAliasDecl(DC, L, AliasLoc, Alias, QualifierRange, |
Douglas Gregor | 6c9c940 | 2009-05-30 06:48:27 +0000 | [diff] [blame] | 910 | Qualifier, IdentLoc, Namespace); |
Anders Carlsson | 68771c7 | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 913 | UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 914 | SourceLocation L, SourceRange NNR, SourceLocation UL, |
| 915 | NestedNameSpecifier* TargetNNS, DeclarationName Name, |
| 916 | bool IsTypeNameArg) { |
| 917 | return new (C) UsingDecl(DC, L, NNR, UL, TargetNNS, Name, IsTypeNameArg); |
Douglas Gregor | 9cfbe48 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 918 | } |
| 919 | |
John McCall | 7ba107a | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 920 | UnresolvedUsingValueDecl * |
| 921 | UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC, |
| 922 | SourceLocation UsingLoc, |
| 923 | SourceRange TargetNNR, |
| 924 | NestedNameSpecifier *TargetNNS, |
| 925 | SourceLocation TargetNameLoc, |
| 926 | DeclarationName TargetName) { |
| 927 | return new (C) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc, |
| 928 | TargetNNR, TargetNNS, |
| 929 | TargetNameLoc, TargetName); |
| 930 | } |
| 931 | |
| 932 | UnresolvedUsingTypenameDecl * |
| 933 | UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC, |
| 934 | SourceLocation UsingLoc, |
| 935 | SourceLocation TypenameLoc, |
| 936 | SourceRange TargetNNR, |
| 937 | NestedNameSpecifier *TargetNNS, |
| 938 | SourceLocation TargetNameLoc, |
| 939 | DeclarationName TargetName) { |
| 940 | return new (C) UnresolvedUsingTypenameDecl(DC, UsingLoc, TypenameLoc, |
| 941 | TargetNNR, TargetNNS, |
| 942 | TargetNameLoc, |
| 943 | TargetName.getAsIdentifierInfo()); |
Anders Carlsson | 665b49c | 2009-08-28 05:30:28 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 946 | StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC, |
| 947 | SourceLocation L, Expr *AssertExpr, |
| 948 | StringLiteral *Message) { |
| 949 | return new (C) StaticAssertDecl(DC, L, AssertExpr, Message); |
| 950 | } |
| 951 | |
| 952 | void StaticAssertDecl::Destroy(ASTContext& C) { |
| 953 | AssertExpr->Destroy(C); |
| 954 | Message->Destroy(C); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 955 | Decl::Destroy(C); |
Anders Carlsson | fb31176 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | StaticAssertDecl::~StaticAssertDecl() { |
| 959 | } |
| 960 | |
Anders Carlsson | 05bf2c7 | 2009-03-26 23:46:50 +0000 | [diff] [blame] | 961 | static const char *getAccessName(AccessSpecifier AS) { |
| 962 | switch (AS) { |
| 963 | default: |
| 964 | case AS_none: |
| 965 | assert("Invalid access specifier!"); |
| 966 | return 0; |
| 967 | case AS_public: |
| 968 | return "public"; |
| 969 | case AS_private: |
| 970 | return "private"; |
| 971 | case AS_protected: |
| 972 | return "protected"; |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, |
| 977 | AccessSpecifier AS) { |
| 978 | return DB << getAccessName(AS); |
| 979 | } |
| 980 | |
| 981 | |