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