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