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