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