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