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