| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 1 | //===- DeclCXX.cpp - C++ Declaration AST Node Implementation --------------===// |
| Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 13 | |
| Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 14 | #include "clang/AST/DeclCXX.h" |
| 15 | #include "clang/AST/ASTContext.h" |
| Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTLambda.h" |
| Argyrios Kyrtzidis | 65ad569 | 2010-10-24 17:26:36 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTMutationListener.h" |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTUnresolvedSet.h" |
| Douglas Gregor | 8fb9512 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 19 | #include "clang/AST/CXXInheritance.h" |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclBase.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclTemplate.h" |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclarationName.h" |
| Anders Carlsson | 5bbe1d7 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 23 | #include "clang/AST/Expr.h" |
| Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprCXX.h" |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 25 | #include "clang/AST/LambdaCapture.h" |
| 26 | #include "clang/AST/NestedNameSpecifier.h" |
| Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 27 | #include "clang/AST/ODRHash.h" |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 28 | #include "clang/AST/Type.h" |
| Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 29 | #include "clang/AST/TypeLoc.h" |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 30 | #include "clang/AST/UnresolvedSet.h" |
| 31 | #include "clang/Basic/Diagnostic.h" |
| Douglas Gregor | b6acda0 | 2008-11-12 23:21:09 +0000 | [diff] [blame] | 32 | #include "clang/Basic/IdentifierTable.h" |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 33 | #include "clang/Basic/LLVM.h" |
| 34 | #include "clang/Basic/LangOptions.h" |
| 35 | #include "clang/Basic/OperatorKinds.h" |
| 36 | #include "clang/Basic/PartialDiagnostic.h" |
| 37 | #include "clang/Basic/SourceLocation.h" |
| 38 | #include "clang/Basic/Specifiers.h" |
| 39 | #include "llvm/ADT/None.h" |
| Fariborz Jahanian | a954049 | 2009-09-12 19:52:10 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/SmallPtrSet.h" |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/SmallVector.h" |
| 42 | #include "llvm/ADT/iterator_range.h" |
| 43 | #include "llvm/Support/Casting.h" |
| 44 | #include "llvm/Support/ErrorHandling.h" |
| 45 | #include "llvm/Support/raw_ostream.h" |
| 46 | #include <algorithm> |
| 47 | #include <cassert> |
| 48 | #include <cstddef> |
| 49 | #include <cstdint> |
| 50 | |
| Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 51 | using namespace clang; |
| 52 | |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | // Decl Allocation/Deallocation Method Implementations |
| 55 | //===----------------------------------------------------------------------===// |
| Douglas Gregor | 5101c24 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 56 | |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 57 | void AccessSpecDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 58 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 59 | AccessSpecDecl *AccessSpecDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 60 | return new (C, ID) AccessSpecDecl(EmptyShell()); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| Richard Smith | a4ba74c | 2013-08-30 04:46:40 +0000 | [diff] [blame] | 63 | void LazyASTUnresolvedSet::getFromExternalSource(ASTContext &C) const { |
| 64 | ExternalASTSource *Source = C.getExternalSource(); |
| 65 | assert(Impl.Decls.isLazy() && "getFromExternalSource for non-lazy set"); |
| 66 | assert(Source && "getFromExternalSource with no external source"); |
| 67 | |
| 68 | for (ASTUnresolvedSet::iterator I = Impl.begin(); I != Impl.end(); ++I) |
| 69 | I.setDecl(cast<NamedDecl>(Source->GetExternalDecl( |
| 70 | reinterpret_cast<uintptr_t>(I.getDecl()) >> 2))); |
| 71 | Impl.Decls.setLazy(false); |
| 72 | } |
| 73 | |
| John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 74 | CXXRecordDecl::DefinitionData::DefinitionData(CXXRecordDecl *D) |
| Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 75 | : UserDeclaredConstructor(false), UserDeclaredSpecialMembers(0), |
| 76 | Aggregate(true), PlainOldData(true), Empty(true), Polymorphic(false), |
| Richard Smith | b6070db | 2018-04-05 18:55:37 +0000 | [diff] [blame] | 77 | Abstract(false), IsStandardLayout(true), IsCXX11StandardLayout(true), |
| 78 | HasBasesWithFields(false), HasBasesWithNonStaticDataMembers(false), |
| Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 79 | HasPrivateFields(false), HasProtectedFields(false), |
| 80 | HasPublicFields(false), HasMutableFields(false), HasVariantMembers(false), |
| 81 | HasOnlyCMembers(true), HasInClassInitializer(false), |
| 82 | HasUninitializedReferenceMember(false), HasUninitializedFields(false), |
| Richard Smith | 12e7931 | 2016-05-13 06:47:56 +0000 | [diff] [blame] | 83 | HasInheritedConstructor(false), HasInheritedAssignment(false), |
| Richard Smith | 96cd671 | 2017-08-16 01:49:53 +0000 | [diff] [blame] | 84 | NeedOverloadResolutionForCopyConstructor(false), |
| Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 85 | NeedOverloadResolutionForMoveConstructor(false), |
| 86 | NeedOverloadResolutionForMoveAssignment(false), |
| 87 | NeedOverloadResolutionForDestructor(false), |
| Richard Smith | 96cd671 | 2017-08-16 01:49:53 +0000 | [diff] [blame] | 88 | DefaultedCopyConstructorIsDeleted(false), |
| Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 89 | DefaultedMoveConstructorIsDeleted(false), |
| 90 | DefaultedMoveAssignmentIsDeleted(false), |
| 91 | DefaultedDestructorIsDeleted(false), HasTrivialSpecialMembers(SMF_All), |
| Akira Hatanaka | 02914dc | 2018-02-05 20:23:22 +0000 | [diff] [blame] | 92 | HasTrivialSpecialMembersForCall(SMF_All), |
| 93 | DeclaredNonTrivialSpecialMembers(0), |
| 94 | DeclaredNonTrivialSpecialMembersForCall(0), HasIrrelevantDestructor(true), |
| Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 95 | HasConstexprNonCopyMoveConstructor(false), |
| Nico Weber | 72c57f4 | 2016-02-24 20:58:14 +0000 | [diff] [blame] | 96 | HasDefaultedDefaultConstructor(false), |
| Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 97 | DefaultedDefaultConstructorIsConstexpr(true), |
| 98 | HasConstexprDefaultConstructor(false), |
| 99 | HasNonLiteralTypeFieldsOrBases(false), ComputedVisibleConversions(false), |
| 100 | UserProvidedDefaultConstructor(false), DeclaredSpecialMembers(0), |
| Richard Smith | df054d3 | 2017-02-25 23:53:05 +0000 | [diff] [blame] | 101 | ImplicitCopyConstructorCanHaveConstParamForVBase(true), |
| 102 | ImplicitCopyConstructorCanHaveConstParamForNonVBase(true), |
| Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 103 | ImplicitCopyAssignmentHasConstParam(true), |
| 104 | HasDeclaredCopyConstructorWithConstParam(false), |
| 105 | HasDeclaredCopyAssignmentWithConstParam(false), IsLambda(false), |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 106 | IsParsingBaseSpecifiers(false), HasODRHash(false), Definition(D) {} |
| John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 107 | |
| Benjamin Kramer | 300c063 | 2012-07-04 17:03:33 +0000 | [diff] [blame] | 108 | CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getBasesSlowCase() const { |
| 109 | return Bases.get(Definition->getASTContext().getExternalSource()); |
| 110 | } |
| 111 | |
| 112 | CXXBaseSpecifier *CXXRecordDecl::DefinitionData::getVBasesSlowCase() const { |
| 113 | return VBases.get(Definition->getASTContext().getExternalSource()); |
| 114 | } |
| 115 | |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 116 | CXXRecordDecl::CXXRecordDecl(Kind K, TagKind TK, const ASTContext &C, |
| 117 | DeclContext *DC, SourceLocation StartLoc, |
| 118 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 119 | CXXRecordDecl *PrevDecl) |
| 120 | : RecordDecl(K, TK, C, DC, StartLoc, IdLoc, Id, PrevDecl), |
| 121 | DefinitionData(PrevDecl ? PrevDecl->DefinitionData |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 122 | : nullptr) {} |
| Douglas Gregor | b6acda0 | 2008-11-12 23:21:09 +0000 | [diff] [blame] | 123 | |
| Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 124 | CXXRecordDecl *CXXRecordDecl::Create(const ASTContext &C, TagKind TK, |
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 125 | DeclContext *DC, SourceLocation StartLoc, |
| 126 | SourceLocation IdLoc, IdentifierInfo *Id, |
| Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 127 | CXXRecordDecl *PrevDecl, |
| Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 128 | bool DelayTypeCreation) { |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 129 | auto *R = new (C, DC) CXXRecordDecl(CXXRecord, TK, C, DC, StartLoc, IdLoc, Id, |
| 130 | PrevDecl); |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 131 | R->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | |
| Douglas Gregor | b6b8f9e | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 133 | // FIXME: DelayTypeCreation seems like such a hack |
| Douglas Gregor | 1ec5e9f | 2009-05-15 19:11:46 +0000 | [diff] [blame] | 134 | if (!DelayTypeCreation) |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | C.getTypeDeclType(R, PrevDecl); |
| Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 136 | return R; |
| 137 | } |
| 138 | |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 139 | CXXRecordDecl * |
| 140 | CXXRecordDecl::CreateLambda(const ASTContext &C, DeclContext *DC, |
| 141 | TypeSourceInfo *Info, SourceLocation Loc, |
| 142 | bool Dependent, bool IsGeneric, |
| 143 | LambdaCaptureDefault CaptureDefault) { |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 144 | auto *R = new (C, DC) CXXRecordDecl(CXXRecord, TTK_Class, C, DC, Loc, Loc, |
| 145 | nullptr, nullptr); |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 146 | R->setBeingDefined(true); |
| Richard Smith | 64c0630 | 2014-05-22 23:19:02 +0000 | [diff] [blame] | 147 | R->DefinitionData = |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 148 | new (C) struct LambdaDefinitionData(R, Info, Dependent, IsGeneric, |
| Richard Smith | 64c0630 | 2014-05-22 23:19:02 +0000 | [diff] [blame] | 149 | CaptureDefault); |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 150 | R->setMayHaveOutOfDateDef(false); |
| James Dennett | 8f60cdd | 2013-09-05 17:46:21 +0000 | [diff] [blame] | 151 | R->setImplicit(true); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 152 | C.getTypeDeclType(R, /*PrevDecl=*/nullptr); |
| Douglas Gregor | c8a7349 | 2012-02-13 15:44:47 +0000 | [diff] [blame] | 153 | return R; |
| 154 | } |
| 155 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 156 | CXXRecordDecl * |
| 157 | CXXRecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) { |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 158 | auto *R = new (C, ID) CXXRecordDecl( |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 159 | CXXRecord, TTK_Struct, C, nullptr, SourceLocation(), SourceLocation(), |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 160 | nullptr, nullptr); |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 161 | R->setMayHaveOutOfDateDef(false); |
| Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 162 | return R; |
| Argyrios Kyrtzidis | 39f0e30 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| Richard Smith | b6070db | 2018-04-05 18:55:37 +0000 | [diff] [blame] | 165 | /// Determine whether a class has a repeated base class. This is intended for |
| 166 | /// use when determining if a class is standard-layout, so makes no attempt to |
| 167 | /// handle virtual bases. |
| 168 | static bool hasRepeatedBaseClass(const CXXRecordDecl *StartRD) { |
| 169 | llvm::SmallPtrSet<const CXXRecordDecl*, 8> SeenBaseTypes; |
| 170 | SmallVector<const CXXRecordDecl*, 8> WorkList = {StartRD}; |
| 171 | while (!WorkList.empty()) { |
| 172 | const CXXRecordDecl *RD = WorkList.pop_back_val(); |
| 173 | for (const CXXBaseSpecifier &BaseSpec : RD->bases()) { |
| 174 | if (const CXXRecordDecl *B = BaseSpec.getType()->getAsCXXRecordDecl()) { |
| 175 | if (!SeenBaseTypes.insert(B).second) |
| 176 | return true; |
| 177 | WorkList.push_back(B); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | return false; |
| 182 | } |
| 183 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | void |
| Craig Topper | e6337e1 | 2015-12-25 00:36:02 +0000 | [diff] [blame] | 185 | CXXRecordDecl::setBases(CXXBaseSpecifier const * const *Bases, |
| 186 | unsigned NumBases) { |
| Douglas Gregor | 4a62bdf | 2010-02-11 01:30:34 +0000 | [diff] [blame] | 187 | ASTContext &C = getASTContext(); |
| Douglas Gregor | cfd8ddc | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 188 | |
| Douglas Gregor | d4c5ed0 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 189 | if (!data().Bases.isOffset() && data().NumBases > 0) |
| 190 | C.Deallocate(data().getBases()); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | |
| Craig Topper | e6337e1 | 2015-12-25 00:36:02 +0000 | [diff] [blame] | 192 | if (NumBases) { |
| Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 193 | if (!C.getLangOpts().CPlusPlus17) { |
| Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 194 | // C++ [dcl.init.aggr]p1: |
| 195 | // An aggregate is [...] a class with [...] no base classes [...]. |
| 196 | data().Aggregate = false; |
| 197 | } |
| Richard Smith | aed32b4 | 2011-10-18 20:08:55 +0000 | [diff] [blame] | 198 | |
| 199 | // C++ [class]p4: |
| 200 | // A POD-struct is an aggregate class... |
| 201 | data().PlainOldData = false; |
| 202 | } |
| 203 | |
| Anders Carlsson | abb20e6 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 204 | // The set of seen virtual base types. |
| Anders Carlsson | e47380f | 2010-03-29 19:49:09 +0000 | [diff] [blame] | 205 | llvm::SmallPtrSet<CanQualType, 8> SeenVBaseTypes; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 206 | |
| Anders Carlsson | abb20e6 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 207 | // The virtual bases of this class. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 208 | SmallVector<const CXXBaseSpecifier *, 8> VBases; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 209 | |
| Craig Topper | e6337e1 | 2015-12-25 00:36:02 +0000 | [diff] [blame] | 210 | data().Bases = new(C) CXXBaseSpecifier [NumBases]; |
| 211 | data().NumBases = NumBases; |
| 212 | for (unsigned i = 0; i < NumBases; ++i) { |
| Douglas Gregor | d4c5ed0 | 2010-10-29 22:39:52 +0000 | [diff] [blame] | 213 | data().getBases()[i] = *Bases[i]; |
| Fariborz Jahanian | 3554b5a | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 214 | // Keep track of inherited vbases for this base class. |
| 215 | const CXXBaseSpecifier *Base = Bases[i]; |
| 216 | QualType BaseType = Base->getType(); |
| Douglas Gregor | beab56e | 2010-02-27 00:25:28 +0000 | [diff] [blame] | 217 | // Skip dependent types; we can't do any checking on them now. |
| Fariborz Jahanian | 3554b5a | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 218 | if (BaseType->isDependentType()) |
| 219 | continue; |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 220 | auto *BaseClassDecl = |
| 221 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); |
| Anders Carlsson | abb20e6 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 222 | |
| Richard Smith | b6070db | 2018-04-05 18:55:37 +0000 | [diff] [blame] | 223 | // C++2a [class]p7: |
| 224 | // A standard-layout class is a class that: |
| 225 | // [...] |
| 226 | // -- has all non-static data members and bit-fields in the class and |
| 227 | // its base classes first declared in the same class |
| 228 | if (BaseClassDecl->data().HasBasesWithFields || |
| 229 | !BaseClassDecl->field_empty()) { |
| 230 | if (data().HasBasesWithFields) |
| 231 | // Two bases have members or bit-fields: not standard-layout. |
| Chandler Carruth | 583edf8 | 2011-04-30 10:07:30 +0000 | [diff] [blame] | 232 | data().IsStandardLayout = false; |
| Richard Smith | b6070db | 2018-04-05 18:55:37 +0000 | [diff] [blame] | 233 | data().HasBasesWithFields = true; |
| 234 | } |
| Chandler Carruth | b196374 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 235 | |
| Richard Smith | b6070db | 2018-04-05 18:55:37 +0000 | [diff] [blame] | 236 | // C++11 [class]p7: |
| 237 | // A standard-layout class is a class that: |
| 238 | // -- [...] has [...] at most one base class with non-static data |
| 239 | // members |
| 240 | if (BaseClassDecl->data().HasBasesWithNonStaticDataMembers || |
| 241 | BaseClassDecl->hasDirectFields()) { |
| 242 | if (data().HasBasesWithNonStaticDataMembers) |
| 243 | data().IsCXX11StandardLayout = false; |
| 244 | data().HasBasesWithNonStaticDataMembers = true; |
| 245 | } |
| 246 | |
| 247 | if (!BaseClassDecl->isEmpty()) { |
| David Majnemer | 5cfda6f | 2016-05-22 05:34:26 +0000 | [diff] [blame] | 248 | // C++14 [meta.unary.prop]p4: |
| 249 | // T is a class type [...] with [...] no base class B for which |
| 250 | // is_empty<B>::value is false. |
| Douglas Gregor | 9d5938a | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 251 | data().Empty = false; |
| Chandler Carruth | b196374 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 252 | } |
| Richard Smith | b6070db | 2018-04-05 18:55:37 +0000 | [diff] [blame] | 253 | |
| Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 254 | // C++1z [dcl.init.agg]p1: |
| 255 | // An aggregate is a class with [...] no private or protected base classes |
| 256 | if (Base->getAccessSpecifier() != AS_public) |
| 257 | data().Aggregate = false; |
| 258 | |
| Douglas Gregor | 11c024b | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 259 | // C++ [class.virtual]p1: |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 260 | // A class that declares or inherits a virtual function is called a |
| Douglas Gregor | 11c024b | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 261 | // polymorphic class. |
| Richard Smith | c00e446 | 2018-06-14 20:03:22 +0000 | [diff] [blame] | 262 | if (BaseClassDecl->isPolymorphic()) { |
| Douglas Gregor | 11c024b | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 263 | data().Polymorphic = true; |
| Chandler Carruth | e71d062 | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 264 | |
| Richard Smith | c00e446 | 2018-06-14 20:03:22 +0000 | [diff] [blame] | 265 | // An aggregate is a class with [...] no virtual functions. |
| 266 | data().Aggregate = false; |
| 267 | } |
| 268 | |
| Chandler Carruth | b196374 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 269 | // C++0x [class]p7: |
| 270 | // A standard-layout class is a class that: [...] |
| 271 | // -- has no non-standard-layout base classes |
| Chandler Carruth | 583edf8 | 2011-04-30 10:07:30 +0000 | [diff] [blame] | 272 | if (!BaseClassDecl->isStandardLayout()) |
| 273 | data().IsStandardLayout = false; |
| Richard Smith | b6070db | 2018-04-05 18:55:37 +0000 | [diff] [blame] | 274 | if (!BaseClassDecl->isCXX11StandardLayout()) |
| 275 | data().IsCXX11StandardLayout = false; |
| Chandler Carruth | b196374 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 276 | |
| Chandler Carruth | e71d062 | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 277 | // Record if this base is the first non-literal field or base. |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 278 | if (!hasNonLiteralTypeFieldsOrBases() && !BaseType->isLiteralType(C)) |
| Chandler Carruth | e71d062 | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 279 | data().HasNonLiteralTypeFieldsOrBases = true; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 280 | |
| Anders Carlsson | abb20e6 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 281 | // Now go through all virtual bases of this base and add them. |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 282 | for (const auto &VBase : BaseClassDecl->vbases()) { |
| Anders Carlsson | abb20e6 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 283 | // Add this base if it's not already in the list. |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 284 | if (SeenVBaseTypes.insert(C.getCanonicalType(VBase.getType())).second) { |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 285 | VBases.push_back(&VBase); |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 286 | |
| 287 | // C++11 [class.copy]p8: |
| 288 | // The implicitly-declared copy constructor for a class X will have |
| 289 | // the form 'X::X(const X&)' if each [...] virtual base class B of X |
| 290 | // has a copy constructor whose first parameter is of type |
| 291 | // 'const B&' or 'const volatile B&' [...] |
| Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 292 | if (CXXRecordDecl *VBaseDecl = VBase.getType()->getAsCXXRecordDecl()) |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 293 | if (!VBaseDecl->hasCopyConstructorWithConstParam()) |
| Richard Smith | df054d3 | 2017-02-25 23:53:05 +0000 | [diff] [blame] | 294 | data().ImplicitCopyConstructorCanHaveConstParamForVBase = false; |
| Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 295 | |
| 296 | // C++1z [dcl.init.agg]p1: |
| 297 | // An aggregate is a class with [...] no virtual base classes |
| 298 | data().Aggregate = false; |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 299 | } |
| Fariborz Jahanian | 3554b5a | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 300 | } |
| Anders Carlsson | abb20e6 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 301 | |
| 302 | if (Base->isVirtual()) { |
| 303 | // Add this base if it's not already in the list. |
| David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 304 | if (SeenVBaseTypes.insert(C.getCanonicalType(BaseType)).second) |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 305 | VBases.push_back(Base); |
| 306 | |
| David Majnemer | 5cfda6f | 2016-05-22 05:34:26 +0000 | [diff] [blame] | 307 | // C++14 [meta.unary.prop] is_empty: |
| 308 | // T is a class type, but not a union type, with ... no virtual base |
| 309 | // classes |
| Douglas Gregor | 9d5938a | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 310 | data().Empty = false; |
| Chandler Carruth | ad7d404 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 311 | |
| Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 312 | // C++1z [dcl.init.agg]p1: |
| 313 | // An aggregate is a class with [...] no virtual base classes |
| 314 | data().Aggregate = false; |
| 315 | |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 316 | // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25: |
| 317 | // A [default constructor, copy/move constructor, or copy/move assignment |
| 318 | // operator for a class X] is trivial [...] if: |
| 319 | // -- class X has [...] no virtual base classes |
| 320 | data().HasTrivialSpecialMembers &= SMF_Destructor; |
| Akira Hatanaka | 02914dc | 2018-02-05 20:23:22 +0000 | [diff] [blame] | 321 | data().HasTrivialSpecialMembersForCall &= SMF_Destructor; |
| Chandler Carruth | b196374 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 322 | |
| 323 | // C++0x [class]p7: |
| 324 | // A standard-layout class is a class that: [...] |
| 325 | // -- has [...] no virtual base classes |
| Chandler Carruth | 583edf8 | 2011-04-30 10:07:30 +0000 | [diff] [blame] | 326 | data().IsStandardLayout = false; |
| Richard Smith | b6070db | 2018-04-05 18:55:37 +0000 | [diff] [blame] | 327 | data().IsCXX11StandardLayout = false; |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 328 | |
| 329 | // C++11 [dcl.constexpr]p4: |
| 330 | // In the definition of a constexpr constructor [...] |
| 331 | // -- the class shall not have any virtual base classes |
| 332 | data().DefaultedDefaultConstructorIsConstexpr = false; |
| Richard Smith | df054d3 | 2017-02-25 23:53:05 +0000 | [diff] [blame] | 333 | |
| 334 | // C++1z [class.copy]p8: |
| 335 | // The implicitly-declared copy constructor for a class X will have |
| 336 | // the form 'X::X(const X&)' if each potentially constructed subobject |
| 337 | // has a copy constructor whose first parameter is of type |
| 338 | // 'const B&' or 'const volatile B&' [...] |
| 339 | if (!BaseClassDecl->hasCopyConstructorWithConstParam()) |
| 340 | data().ImplicitCopyConstructorCanHaveConstParamForVBase = false; |
| Douglas Gregor | 11c024b | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 341 | } else { |
| 342 | // C++ [class.ctor]p5: |
| Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 343 | // A default constructor is trivial [...] if: |
| 344 | // -- all the direct base classes of its class have trivial default |
| 345 | // constructors. |
| 346 | if (!BaseClassDecl->hasTrivialDefaultConstructor()) |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 347 | data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor; |
| 348 | |
| Chandler Carruth | ad7d404 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 349 | // C++0x [class.copy]p13: |
| 350 | // A copy/move constructor for class X is trivial if [...] |
| 351 | // [...] |
| 352 | // -- the constructor selected to copy/move each direct base class |
| 353 | // subobject is trivial, and |
| Douglas Gregor | 11c024b | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 354 | if (!BaseClassDecl->hasTrivialCopyConstructor()) |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 355 | data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor; |
| Akira Hatanaka | 02914dc | 2018-02-05 20:23:22 +0000 | [diff] [blame] | 356 | |
| 357 | if (!BaseClassDecl->hasTrivialCopyConstructorForCall()) |
| 358 | data().HasTrivialSpecialMembersForCall &= ~SMF_CopyConstructor; |
| 359 | |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 360 | // If the base class doesn't have a simple move constructor, we'll eagerly |
| 361 | // declare it and perform overload resolution to determine which function |
| 362 | // it actually calls. If it does have a simple move constructor, this |
| 363 | // check is correct. |
| Richard Smith | 1648847 | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 364 | if (!BaseClassDecl->hasTrivialMoveConstructor()) |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 365 | data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor; |
| Chandler Carruth | ad7d404 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 366 | |
| Akira Hatanaka | 02914dc | 2018-02-05 20:23:22 +0000 | [diff] [blame] | 367 | if (!BaseClassDecl->hasTrivialMoveConstructorForCall()) |
| 368 | data().HasTrivialSpecialMembersForCall &= ~SMF_MoveConstructor; |
| 369 | |
| Chandler Carruth | ad7d404 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 370 | // C++0x [class.copy]p27: |
| 371 | // A copy/move assignment operator for class X is trivial if [...] |
| 372 | // [...] |
| 373 | // -- the assignment operator selected to copy/move each direct base |
| 374 | // class subobject is trivial, and |
| Douglas Gregor | 11c024b | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 375 | if (!BaseClassDecl->hasTrivialCopyAssignment()) |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 376 | data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment; |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 377 | // If the base class doesn't have a simple move assignment, we'll eagerly |
| 378 | // declare it and perform overload resolution to determine which function |
| 379 | // it actually calls. If it does have a simple move assignment, this |
| 380 | // check is correct. |
| Richard Smith | 1648847 | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 381 | if (!BaseClassDecl->hasTrivialMoveAssignment()) |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 382 | data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment; |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 383 | |
| 384 | // C++11 [class.ctor]p6: |
| Richard Smith | c101e61 | 2012-01-11 18:26:05 +0000 | [diff] [blame] | 385 | // If that user-written default constructor would satisfy the |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 386 | // requirements of a constexpr constructor, the implicitly-defined |
| 387 | // default constructor is constexpr. |
| 388 | if (!BaseClassDecl->hasConstexprDefaultConstructor()) |
| 389 | data().DefaultedDefaultConstructorIsConstexpr = false; |
| Richard Smith | df054d3 | 2017-02-25 23:53:05 +0000 | [diff] [blame] | 390 | |
| 391 | // C++1z [class.copy]p8: |
| 392 | // The implicitly-declared copy constructor for a class X will have |
| 393 | // the form 'X::X(const X&)' if each potentially constructed subobject |
| 394 | // has a copy constructor whose first parameter is of type |
| 395 | // 'const B&' or 'const volatile B&' [...] |
| 396 | if (!BaseClassDecl->hasCopyConstructorWithConstParam()) |
| 397 | data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false; |
| Anders Carlsson | abb20e6 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 398 | } |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 399 | |
| Douglas Gregor | 11c024b | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 400 | // C++ [class.ctor]p3: |
| 401 | // A destructor is trivial if all the direct base classes of its class |
| 402 | // have trivial destructors. |
| 403 | if (!BaseClassDecl->hasTrivialDestructor()) |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 404 | data().HasTrivialSpecialMembers &= ~SMF_Destructor; |
| Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 405 | |
| Akira Hatanaka | 02914dc | 2018-02-05 20:23:22 +0000 | [diff] [blame] | 406 | if (!BaseClassDecl->hasTrivialDestructorForCall()) |
| 407 | data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor; |
| 408 | |
| Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 409 | if (!BaseClassDecl->hasIrrelevantDestructor()) |
| 410 | data().HasIrrelevantDestructor = false; |
| 411 | |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 412 | // C++11 [class.copy]p18: |
| 413 | // The implicitly-declared copy assignment oeprator for a class X will |
| 414 | // have the form 'X& X::operator=(const X&)' if each direct base class B |
| 415 | // of X has a copy assignment operator whose parameter is of type 'const |
| 416 | // B&', 'const volatile B&', or 'B' [...] |
| 417 | if (!BaseClassDecl->hasCopyAssignmentWithConstParam()) |
| 418 | data().ImplicitCopyAssignmentHasConstParam = false; |
| 419 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 420 | // A class has an Objective-C object member if... or any of its bases |
| 421 | // has an Objective-C object member. |
| 422 | if (BaseClassDecl->hasObjectMember()) |
| 423 | setHasObjectMember(true); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 424 | |
| Fariborz Jahanian | 7865220 | 2013-01-25 23:57:05 +0000 | [diff] [blame] | 425 | if (BaseClassDecl->hasVolatileMember()) |
| 426 | setHasVolatileMember(true); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 427 | |
| Akira Hatanaka | e6313ac | 2018-04-09 22:48:22 +0000 | [diff] [blame] | 428 | if (BaseClassDecl->getArgPassingRestrictions() == |
| 429 | RecordDecl::APK_CanNeverPassInRegs) |
| 430 | setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs); |
| 431 | |
| Douglas Gregor | 61226d3 | 2011-05-13 01:05:07 +0000 | [diff] [blame] | 432 | // Keep track of the presence of mutable fields. |
| Richard Smith | 96cd671 | 2017-08-16 01:49:53 +0000 | [diff] [blame] | 433 | if (BaseClassDecl->hasMutableFields()) { |
| Douglas Gregor | 61226d3 | 2011-05-13 01:05:07 +0000 | [diff] [blame] | 434 | data().HasMutableFields = true; |
| Richard Smith | 96cd671 | 2017-08-16 01:49:53 +0000 | [diff] [blame] | 435 | data().NeedOverloadResolutionForCopyConstructor = true; |
| 436 | } |
| Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 437 | |
| 438 | if (BaseClassDecl->hasUninitializedReferenceMember()) |
| 439 | data().HasUninitializedReferenceMember = true; |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 440 | |
| Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 441 | if (!BaseClassDecl->allowConstDefaultInit()) |
| 442 | data().HasUninitializedFields = true; |
| 443 | |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 444 | addedClassSubobject(BaseClassDecl); |
| Fariborz Jahanian | 3554b5a | 2009-07-10 20:13:23 +0000 | [diff] [blame] | 445 | } |
| Richard Smith | b6070db | 2018-04-05 18:55:37 +0000 | [diff] [blame] | 446 | |
| 447 | // C++2a [class]p7: |
| 448 | // A class S is a standard-layout class if it: |
| 449 | // -- has at most one base class subobject of any given type |
| 450 | // |
| 451 | // Note that we only need to check this for classes with more than one base |
| 452 | // class. If there's only one base class, and it's standard layout, then |
| 453 | // we know there are no repeated base classes. |
| 454 | if (data().IsStandardLayout && NumBases > 1 && hasRepeatedBaseClass(this)) |
| 455 | data().IsStandardLayout = false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 456 | |
| David Majnemer | 5ef4fe7 | 2014-06-13 06:43:46 +0000 | [diff] [blame] | 457 | if (VBases.empty()) { |
| 458 | data().IsParsingBaseSpecifiers = false; |
| Anders Carlsson | abb20e6 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 459 | return; |
| David Majnemer | 5ef4fe7 | 2014-06-13 06:43:46 +0000 | [diff] [blame] | 460 | } |
| Anders Carlsson | abb20e6 | 2010-03-29 05:13:12 +0000 | [diff] [blame] | 461 | |
| 462 | // Create base specifier for any direct or indirect virtual bases. |
| 463 | data().VBases = new (C) CXXBaseSpecifier[VBases.size()]; |
| 464 | data().NumVBases = VBases.size(); |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 465 | for (int I = 0, E = VBases.size(); I != E; ++I) { |
| 466 | QualType Type = VBases[I]->getType(); |
| 467 | if (!Type->isDependentType()) |
| 468 | addedClassSubobject(Type->getAsCXXRecordDecl()); |
| Richard Smith | 26935e6 | 2011-07-12 23:49:11 +0000 | [diff] [blame] | 469 | data().getVBases()[I] = *VBases[I]; |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 470 | } |
| David Majnemer | 5ef4fe7 | 2014-06-13 06:43:46 +0000 | [diff] [blame] | 471 | |
| 472 | data().IsParsingBaseSpecifiers = false; |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| Richard Trieu | fd1acbb | 2017-04-11 21:31:00 +0000 | [diff] [blame] | 475 | unsigned CXXRecordDecl::getODRHash() const { |
| 476 | assert(hasDefinition() && "ODRHash only for records with definitions"); |
| Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 477 | |
| Richard Trieu | fd1acbb | 2017-04-11 21:31:00 +0000 | [diff] [blame] | 478 | // Previously calculated hash is stored in DefinitionData. |
| 479 | if (DefinitionData->HasODRHash) |
| 480 | return DefinitionData->ODRHash; |
| 481 | |
| 482 | // Only calculate hash on first call of getODRHash per record. |
| Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 483 | ODRHash Hash; |
| Richard Trieu | fd1acbb | 2017-04-11 21:31:00 +0000 | [diff] [blame] | 484 | Hash.AddCXXRecordDecl(getDefinition()); |
| 485 | DefinitionData->HasODRHash = true; |
| Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 486 | DefinitionData->ODRHash = Hash.CalculateHash(); |
| Richard Trieu | fd1acbb | 2017-04-11 21:31:00 +0000 | [diff] [blame] | 487 | |
| 488 | return DefinitionData->ODRHash; |
| Richard Trieu | e7f7ed2 | 2017-02-22 01:11:25 +0000 | [diff] [blame] | 489 | } |
| Richard Trieu | b6adf54 | 2017-02-18 02:09:28 +0000 | [diff] [blame] | 490 | |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 491 | void CXXRecordDecl::addedClassSubobject(CXXRecordDecl *Subobj) { |
| 492 | // C++11 [class.copy]p11: |
| 493 | // A defaulted copy/move constructor for a class X is defined as |
| 494 | // deleted if X has: |
| 495 | // -- a direct or virtual base class B that cannot be copied/moved [...] |
| 496 | // -- a non-static data member of class type M (or array thereof) |
| 497 | // that cannot be copied or moved [...] |
| Richard Smith | 96cd671 | 2017-08-16 01:49:53 +0000 | [diff] [blame] | 498 | if (!Subobj->hasSimpleCopyConstructor()) |
| 499 | data().NeedOverloadResolutionForCopyConstructor = true; |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 500 | if (!Subobj->hasSimpleMoveConstructor()) |
| 501 | data().NeedOverloadResolutionForMoveConstructor = true; |
| 502 | |
| 503 | // C++11 [class.copy]p23: |
| 504 | // A defaulted copy/move assignment operator for a class X is defined as |
| 505 | // deleted if X has: |
| 506 | // -- a direct or virtual base class B that cannot be copied/moved [...] |
| 507 | // -- a non-static data member of class type M (or array thereof) |
| 508 | // that cannot be copied or moved [...] |
| 509 | if (!Subobj->hasSimpleMoveAssignment()) |
| 510 | data().NeedOverloadResolutionForMoveAssignment = true; |
| 511 | |
| 512 | // C++11 [class.ctor]p5, C++11 [class.copy]p11, C++11 [class.dtor]p5: |
| 513 | // A defaulted [ctor or dtor] for a class X is defined as |
| 514 | // deleted if X has: |
| 515 | // -- any direct or virtual base class [...] has a type with a destructor |
| 516 | // that is deleted or inaccessible from the defaulted [ctor or dtor]. |
| 517 | // -- any non-static data member has a type with a destructor |
| 518 | // that is deleted or inaccessible from the defaulted [ctor or dtor]. |
| 519 | if (!Subobj->hasSimpleDestructor()) { |
| Richard Smith | 96cd671 | 2017-08-16 01:49:53 +0000 | [diff] [blame] | 520 | data().NeedOverloadResolutionForCopyConstructor = true; |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 521 | data().NeedOverloadResolutionForMoveConstructor = true; |
| 522 | data().NeedOverloadResolutionForDestructor = true; |
| 523 | } |
| Douglas Gregor | 9d6290b | 2008-10-23 18:13:27 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 526 | bool CXXRecordDecl::hasAnyDependentBases() const { |
| 527 | if (!isDependentContext()) |
| 528 | return false; |
| 529 | |
| Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 530 | return !forallBases([](const CXXRecordDecl *) { return true; }); |
| Douglas Gregor | d2e6a45 | 2010-01-14 17:47:39 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| Chandler Carruth | a3e1f9a | 2011-04-23 10:47:28 +0000 | [diff] [blame] | 533 | bool CXXRecordDecl::isTriviallyCopyable() const { |
| 534 | // C++0x [class]p5: |
| 535 | // A trivially copyable class is a class that: |
| 536 | // -- has no non-trivial copy constructors, |
| Richard Smith | 1648847 | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 537 | if (hasNonTrivialCopyConstructor()) return false; |
| Chandler Carruth | a3e1f9a | 2011-04-23 10:47:28 +0000 | [diff] [blame] | 538 | // -- has no non-trivial move constructors, |
| Richard Smith | 1648847 | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 539 | if (hasNonTrivialMoveConstructor()) return false; |
| Chandler Carruth | a3e1f9a | 2011-04-23 10:47:28 +0000 | [diff] [blame] | 540 | // -- has no non-trivial copy assignment operators, |
| Richard Smith | 1648847 | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 541 | if (hasNonTrivialCopyAssignment()) return false; |
| Chandler Carruth | a3e1f9a | 2011-04-23 10:47:28 +0000 | [diff] [blame] | 542 | // -- has no non-trivial move assignment operators, and |
| Richard Smith | 1648847 | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 543 | if (hasNonTrivialMoveAssignment()) return false; |
| Chandler Carruth | a3e1f9a | 2011-04-23 10:47:28 +0000 | [diff] [blame] | 544 | // -- has a trivial destructor. |
| 545 | if (!hasTrivialDestructor()) return false; |
| 546 | |
| 547 | return true; |
| 548 | } |
| 549 | |
| Douglas Gregor | 7d9120c | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 550 | void CXXRecordDecl::markedVirtualFunctionPure() { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 551 | // C++ [class.abstract]p2: |
| Douglas Gregor | 7d9120c | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 552 | // A class is abstract if it has at least one pure virtual function. |
| 553 | data().Abstract = true; |
| 554 | } |
| 555 | |
| Richard Smith | b6070db | 2018-04-05 18:55:37 +0000 | [diff] [blame] | 556 | bool CXXRecordDecl::hasSubobjectAtOffsetZeroOfEmptyBaseType( |
| 557 | ASTContext &Ctx, const CXXRecordDecl *XFirst) { |
| 558 | if (!getNumBases()) |
| 559 | return false; |
| 560 | |
| 561 | llvm::SmallPtrSet<const CXXRecordDecl*, 8> Bases; |
| 562 | llvm::SmallPtrSet<const CXXRecordDecl*, 8> M; |
| 563 | SmallVector<const CXXRecordDecl*, 8> WorkList; |
| 564 | |
| 565 | // Visit a type that we have determined is an element of M(S). |
| 566 | auto Visit = [&](const CXXRecordDecl *RD) -> bool { |
| 567 | RD = RD->getCanonicalDecl(); |
| 568 | |
| 569 | // C++2a [class]p8: |
| 570 | // A class S is a standard-layout class if it [...] has no element of the |
| 571 | // set M(S) of types as a base class. |
| 572 | // |
| 573 | // If we find a subobject of an empty type, it might also be a base class, |
| 574 | // so we'll need to walk the base classes to check. |
| 575 | if (!RD->data().HasBasesWithFields) { |
| 576 | // Walk the bases the first time, stopping if we find the type. Build a |
| 577 | // set of them so we don't need to walk them again. |
| 578 | if (Bases.empty()) { |
| 579 | bool RDIsBase = !forallBases([&](const CXXRecordDecl *Base) -> bool { |
| 580 | Base = Base->getCanonicalDecl(); |
| 581 | if (RD == Base) |
| 582 | return false; |
| 583 | Bases.insert(Base); |
| 584 | return true; |
| 585 | }); |
| 586 | if (RDIsBase) |
| 587 | return true; |
| 588 | } else { |
| 589 | if (Bases.count(RD)) |
| 590 | return true; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | if (M.insert(RD).second) |
| 595 | WorkList.push_back(RD); |
| 596 | return false; |
| 597 | }; |
| 598 | |
| 599 | if (Visit(XFirst)) |
| 600 | return true; |
| 601 | |
| 602 | while (!WorkList.empty()) { |
| 603 | const CXXRecordDecl *X = WorkList.pop_back_val(); |
| 604 | |
| 605 | // FIXME: We don't check the bases of X. That matches the standard, but |
| 606 | // that sure looks like a wording bug. |
| 607 | |
| 608 | // -- If X is a non-union class type with a non-static data member |
| 609 | // [recurse to] the first non-static data member of X |
| 610 | // -- If X is a union type, [recurse to union members] |
| 611 | for (auto *FD : X->fields()) { |
| 612 | // FIXME: Should we really care about the type of the first non-static |
| 613 | // data member of a non-union if there are preceding unnamed bit-fields? |
| 614 | if (FD->isUnnamedBitfield()) |
| 615 | continue; |
| 616 | |
| 617 | // -- If X is n array type, [visit the element type] |
| 618 | QualType T = Ctx.getBaseElementType(FD->getType()); |
| 619 | if (auto *RD = T->getAsCXXRecordDecl()) |
| 620 | if (Visit(RD)) |
| 621 | return true; |
| 622 | |
| 623 | if (!X->isUnion()) |
| 624 | break; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | return false; |
| 629 | } |
| 630 | |
| Douglas Gregor | 7d9120c | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 631 | void CXXRecordDecl::addedMember(Decl *D) { |
| Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 632 | if (!D->isImplicit() && |
| 633 | !isa<FieldDecl>(D) && |
| 634 | !isa<IndirectFieldDecl>(D) && |
| 635 | (!isa<TagDecl>(D) || cast<TagDecl>(D)->getTagKind() == TTK_Class || |
| 636 | cast<TagDecl>(D)->getTagKind() == TTK_Interface)) |
| 637 | data().HasOnlyCMembers = false; |
| 638 | |
| 639 | // Ignore friends and invalid declarations. |
| Douglas Gregor | a1ce1f8 | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 640 | if (D->getFriendObjectKind() || D->isInvalidDecl()) |
| Douglas Gregor | d30e79f | 2010-09-27 21:17:54 +0000 | [diff] [blame] | 641 | return; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 642 | |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 643 | auto *FunTmpl = dyn_cast<FunctionTemplateDecl>(D); |
| Douglas Gregor | a1ce1f8 | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 644 | if (FunTmpl) |
| 645 | D = FunTmpl->getTemplatedDecl(); |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 646 | |
| 647 | // FIXME: Pass NamedDecl* to addedMember? |
| 648 | Decl *DUnderlying = D; |
| 649 | if (auto *ND = dyn_cast<NamedDecl>(DUnderlying)) { |
| 650 | DUnderlying = ND->getUnderlyingDecl(); |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 651 | if (auto *UnderlyingFunTmpl = dyn_cast<FunctionTemplateDecl>(DUnderlying)) |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 652 | DUnderlying = UnderlyingFunTmpl->getTemplatedDecl(); |
| 653 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 654 | |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 655 | if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) { |
| Douglas Gregor | a832d3e | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 656 | if (Method->isVirtual()) { |
| 657 | // C++ [dcl.init.aggr]p1: |
| 658 | // An aggregate is an array or a class with [...] no virtual functions. |
| 659 | data().Aggregate = false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 660 | |
| Douglas Gregor | a832d3e | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 661 | // C++ [class]p4: |
| 662 | // A POD-struct is an aggregate class... |
| 663 | data().PlainOldData = false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 664 | |
| David Majnemer | 5cfda6f | 2016-05-22 05:34:26 +0000 | [diff] [blame] | 665 | // C++14 [meta.unary.prop]p4: |
| 666 | // T is a class type [...] with [...] no virtual member functions... |
| Douglas Gregor | 9d5938a | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 667 | data().Empty = false; |
| Douglas Gregor | 11c024b | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 668 | |
| 669 | // C++ [class.virtual]p1: |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 670 | // A class that declares or inherits a virtual function is called a |
| Douglas Gregor | 11c024b | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 671 | // polymorphic class. |
| 672 | data().Polymorphic = true; |
| Chandler Carruth | ad7d404 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 673 | |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 674 | // C++11 [class.ctor]p5, C++11 [class.copy]p12, C++11 [class.copy]p25: |
| 675 | // A [default constructor, copy/move constructor, or copy/move |
| 676 | // assignment operator for a class X] is trivial [...] if: |
| Chandler Carruth | ad7d404 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 677 | // -- class X has no virtual functions [...] |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 678 | data().HasTrivialSpecialMembers &= SMF_Destructor; |
| Akira Hatanaka | 02914dc | 2018-02-05 20:23:22 +0000 | [diff] [blame] | 679 | data().HasTrivialSpecialMembersForCall &= SMF_Destructor; |
| Chandler Carruth | ad7d404 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 680 | |
| Chandler Carruth | b196374 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 681 | // C++0x [class]p7: |
| 682 | // A standard-layout class is a class that: [...] |
| 683 | // -- has no virtual functions |
| Chandler Carruth | 583edf8 | 2011-04-30 10:07:30 +0000 | [diff] [blame] | 684 | data().IsStandardLayout = false; |
| Richard Smith | b6070db | 2018-04-05 18:55:37 +0000 | [diff] [blame] | 685 | data().IsCXX11StandardLayout = false; |
| Douglas Gregor | a832d3e | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 686 | } |
| 687 | } |
| Argyrios Kyrtzidis | 00f5266 | 2010-10-20 23:48:42 +0000 | [diff] [blame] | 688 | |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 689 | // Notify the listener if an implicit member was added after the definition |
| 690 | // was completed. |
| 691 | if (!isBeingDefined() && D->isImplicit()) |
| 692 | if (ASTMutationListener *L = getASTMutationListener()) |
| 693 | L->AddedCXXImplicitMember(data().Definition, D); |
| Douglas Gregor | 9d5938a | 2010-09-28 20:38:10 +0000 | [diff] [blame] | 694 | |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 695 | // The kind of special member this declaration is, if any. |
| 696 | unsigned SMKind = 0; |
| 697 | |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 698 | // Handle constructors. |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 699 | if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 700 | if (!Constructor->isImplicit()) { |
| 701 | // Note that we have a user-declared constructor. |
| 702 | data().UserDeclaredConstructor = true; |
| 703 | |
| 704 | // C++ [class]p4: |
| 705 | // A POD-struct is an aggregate class [...] |
| 706 | // Since the POD bit is meant to be C++03 POD-ness, clear it even if the |
| 707 | // type is technically an aggregate in C++0x since it wouldn't be in 03. |
| 708 | data().PlainOldData = false; |
| 709 | } |
| Douglas Gregor | a1ce1f8 | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 710 | |
| Alexis Hunt | 88c75c3 | 2011-05-09 21:45:35 +0000 | [diff] [blame] | 711 | if (Constructor->isDefaultConstructor()) { |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 712 | SMKind |= SMF_DefaultConstructor; |
| 713 | |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 714 | if (Constructor->isUserProvided()) |
| Alexis Hunt | ea6f032 | 2011-05-11 22:34:38 +0000 | [diff] [blame] | 715 | data().UserProvidedDefaultConstructor = true; |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 716 | if (Constructor->isConstexpr()) |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 717 | data().HasConstexprDefaultConstructor = true; |
| Nico Weber | 72c57f4 | 2016-02-24 20:58:14 +0000 | [diff] [blame] | 718 | if (Constructor->isDefaulted()) |
| 719 | data().HasDefaultedDefaultConstructor = true; |
| Alexis Hunt | 88c75c3 | 2011-05-09 21:45:35 +0000 | [diff] [blame] | 720 | } |
| Douglas Gregor | a1ce1f8 | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 721 | |
| Chandler Carruth | ad7d404 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 722 | if (!FunTmpl) { |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 723 | unsigned Quals; |
| 724 | if (Constructor->isCopyConstructor(Quals)) { |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 725 | SMKind |= SMF_CopyConstructor; |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 726 | |
| 727 | if (Quals & Qualifiers::Const) |
| 728 | data().HasDeclaredCopyConstructorWithConstParam = true; |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 729 | } else if (Constructor->isMoveConstructor()) |
| 730 | SMKind |= SMF_MoveConstructor; |
| Douglas Gregor | a1ce1f8 | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 731 | } |
| Eric Fiselier | 283d8d4 | 2016-12-03 01:26:47 +0000 | [diff] [blame] | 732 | |
| Eric Fiselier | 283d8d4 | 2016-12-03 01:26:47 +0000 | [diff] [blame] | 733 | // C++11 [dcl.init.aggr]p1: DR1518 |
| Richard Smith | 505ef81 | 2016-12-21 01:57:02 +0000 | [diff] [blame] | 734 | // An aggregate is an array or a class with no user-provided, explicit, or |
| 735 | // inherited constructors |
| 736 | if (Constructor->isUserProvided() || Constructor->isExplicit()) |
| Eric Fiselier | 283d8d4 | 2016-12-03 01:26:47 +0000 | [diff] [blame] | 737 | data().Aggregate = false; |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 738 | } |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 739 | |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 740 | // Handle constructors, including those inherited from base classes. |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 741 | if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(DUnderlying)) { |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 742 | // Record if we see any constexpr constructors which are neither copy |
| 743 | // nor move constructors. |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 744 | // C++1z [basic.types]p10: |
| 745 | // [...] has at least one constexpr constructor or constructor template |
| 746 | // (possibly inherited from a base class) that is not a copy or move |
| 747 | // constructor [...] |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 748 | if (Constructor->isConstexpr() && !Constructor->isCopyOrMoveConstructor()) |
| Richard Smith | 111af8d | 2011-08-10 18:11:37 +0000 | [diff] [blame] | 749 | data().HasConstexprNonCopyMoveConstructor = true; |
| Douglas Gregor | d30e79f | 2010-09-27 21:17:54 +0000 | [diff] [blame] | 750 | } |
| Douglas Gregor | a1ce1f8 | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 751 | |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 752 | // Handle destructors. |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 753 | if (const auto *DD = dyn_cast<CXXDestructorDecl>(D)) { |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 754 | SMKind |= SMF_Destructor; |
| Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 755 | |
| Stephan Tolksdorf | d85fa23 | 2014-03-27 20:23:12 +0000 | [diff] [blame] | 756 | if (DD->isUserProvided()) |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 757 | data().HasIrrelevantDestructor = false; |
| Stephan Tolksdorf | d85fa23 | 2014-03-27 20:23:12 +0000 | [diff] [blame] | 758 | // If the destructor is explicitly defaulted and not trivial or not public |
| 759 | // or if the destructor is deleted, we clear HasIrrelevantDestructor in |
| 760 | // finishedDefaultedOrDeletedMember. |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 761 | |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 762 | // C++11 [class.dtor]p5: |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 763 | // A destructor is trivial if [...] the destructor is not virtual. |
| Akira Hatanaka | 02914dc | 2018-02-05 20:23:22 +0000 | [diff] [blame] | 764 | if (DD->isVirtual()) { |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 765 | data().HasTrivialSpecialMembers &= ~SMF_Destructor; |
| Akira Hatanaka | 02914dc | 2018-02-05 20:23:22 +0000 | [diff] [blame] | 766 | data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor; |
| 767 | } |
| Douglas Gregor | 8f9ebe5 | 2010-09-27 22:48:58 +0000 | [diff] [blame] | 768 | } |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 769 | |
| 770 | // Handle member functions. |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 771 | if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) { |
| Alexis Hunt | fcaeae4 | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 772 | if (Method->isCopyAssignmentOperator()) { |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 773 | SMKind |= SMF_CopyAssignment; |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 774 | |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 775 | const auto *ParamTy = |
| 776 | Method->getParamDecl(0)->getType()->getAs<ReferenceType>(); |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 777 | if (!ParamTy || ParamTy->getPointeeType().isConstQualified()) |
| 778 | data().HasDeclaredCopyAssignmentWithConstParam = true; |
| Alexis Hunt | fcaeae4 | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 779 | } |
| Alexis Hunt | fcaeae4 | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 780 | |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 781 | if (Method->isMoveAssignmentOperator()) |
| 782 | SMKind |= SMF_MoveAssignment; |
| Chandler Carruth | ad7d404 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 783 | |
| Douglas Gregor | 457104e | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 784 | // Keep the list of conversion functions up-to-date. |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 785 | if (auto *Conversion = dyn_cast<CXXConversionDecl>(D)) { |
| Douglas Gregor | 92fde2f | 2013-04-08 17:12:58 +0000 | [diff] [blame] | 786 | // FIXME: We use the 'unsafe' accessor for the access specifier here, |
| 787 | // because Sema may not have set it yet. That's really just a misdesign |
| 788 | // in Sema. However, LLDB *will* have set the access specifier correctly, |
| 789 | // and adds declarations after the class is technically completed, |
| 790 | // so completeDefinition()'s overriding of the access specifiers doesn't |
| 791 | // work. |
| 792 | AccessSpecifier AS = Conversion->getAccessUnsafe(); |
| 793 | |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 794 | if (Conversion->getPrimaryTemplate()) { |
| 795 | // We don't record specializations. |
| Douglas Gregor | 457104e | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 796 | } else { |
| Richard Smith | a4ba74c | 2013-08-30 04:46:40 +0000 | [diff] [blame] | 797 | ASTContext &Ctx = getASTContext(); |
| 798 | ASTUnresolvedSet &Conversions = data().Conversions.get(Ctx); |
| 799 | NamedDecl *Primary = |
| 800 | FunTmpl ? cast<NamedDecl>(FunTmpl) : cast<NamedDecl>(Conversion); |
| 801 | if (Primary->getPreviousDecl()) |
| 802 | Conversions.replace(cast<NamedDecl>(Primary->getPreviousDecl()), |
| 803 | Primary, AS); |
| Douglas Gregor | 457104e | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 804 | else |
| Richard Smith | a4ba74c | 2013-08-30 04:46:40 +0000 | [diff] [blame] | 805 | Conversions.addDecl(Ctx, Primary, AS); |
| Douglas Gregor | 457104e | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 806 | } |
| 807 | } |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 808 | |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 809 | if (SMKind) { |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 810 | // If this is the first declaration of a special member, we no longer have |
| 811 | // an implicit trivial special member. |
| 812 | data().HasTrivialSpecialMembers &= |
| Akira Hatanaka | 02914dc | 2018-02-05 20:23:22 +0000 | [diff] [blame] | 813 | data().DeclaredSpecialMembers | ~SMKind; |
| 814 | data().HasTrivialSpecialMembersForCall &= |
| 815 | data().DeclaredSpecialMembers | ~SMKind; |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 816 | |
| 817 | if (!Method->isImplicit() && !Method->isUserProvided()) { |
| 818 | // This method is user-declared but not user-provided. We can't work out |
| 819 | // whether it's trivial yet (not until we get to the end of the class). |
| 820 | // We'll handle this method in finishedDefaultedOrDeletedMember. |
| Akira Hatanaka | 02914dc | 2018-02-05 20:23:22 +0000 | [diff] [blame] | 821 | } else if (Method->isTrivial()) { |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 822 | data().HasTrivialSpecialMembers |= SMKind; |
| Akira Hatanaka | 02914dc | 2018-02-05 20:23:22 +0000 | [diff] [blame] | 823 | data().HasTrivialSpecialMembersForCall |= SMKind; |
| 824 | } else if (Method->isTrivialForCall()) { |
| 825 | data().HasTrivialSpecialMembersForCall |= SMKind; |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 826 | data().DeclaredNonTrivialSpecialMembers |= SMKind; |
| Akira Hatanaka | 02914dc | 2018-02-05 20:23:22 +0000 | [diff] [blame] | 827 | } else { |
| 828 | data().DeclaredNonTrivialSpecialMembers |= SMKind; |
| 829 | // If this is a user-provided function, do not set |
| 830 | // DeclaredNonTrivialSpecialMembersForCall here since we don't know |
| 831 | // yet whether the method would be considered non-trivial for the |
| 832 | // purpose of calls (attribute "trivial_abi" can be dropped from the |
| 833 | // class later, which can change the special method's triviality). |
| 834 | if (!Method->isUserProvided()) |
| 835 | data().DeclaredNonTrivialSpecialMembersForCall |= SMKind; |
| 836 | } |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 837 | |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 838 | // Note when we have declared a declared special member, and suppress the |
| 839 | // implicit declaration of this special member. |
| 840 | data().DeclaredSpecialMembers |= SMKind; |
| 841 | |
| 842 | if (!Method->isImplicit()) { |
| 843 | data().UserDeclaredSpecialMembers |= SMKind; |
| 844 | |
| 845 | // C++03 [class]p4: |
| 846 | // A POD-struct is an aggregate class that has [...] no user-defined |
| 847 | // copy assignment operator and no user-defined destructor. |
| 848 | // |
| 849 | // Since the POD bit is meant to be C++03 POD-ness, and in C++03, |
| 850 | // aggregates could not have any constructors, clear it even for an |
| 851 | // explicitly defaulted or deleted constructor. |
| 852 | // type is technically an aggregate in C++0x since it wouldn't be in 03. |
| 853 | // |
| 854 | // Also, a user-declared move assignment operator makes a class non-POD. |
| 855 | // This is an extension in C++03. |
| 856 | data().PlainOldData = false; |
| 857 | } |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 858 | } |
| 859 | |
| Douglas Gregor | a1ce1f8 | 2010-09-27 22:06:20 +0000 | [diff] [blame] | 860 | return; |
| Douglas Gregor | 8a27391 | 2009-07-22 18:25:24 +0000 | [diff] [blame] | 861 | } |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 862 | |
| Douglas Gregor | a832d3e | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 863 | // Handle non-static data members. |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 864 | if (const auto *Field = dyn_cast<FieldDecl>(D)) { |
| Richard Smith | b5a317f | 2018-05-07 06:43:30 +0000 | [diff] [blame] | 865 | ASTContext &Context = getASTContext(); |
| 866 | |
| Richard Smith | b6070db | 2018-04-05 18:55:37 +0000 | [diff] [blame] | 867 | // C++2a [class]p7: |
| 868 | // A standard-layout class is a class that: |
| 869 | // [...] |
| 870 | // -- has all non-static data members and bit-fields in the class and |
| 871 | // its base classes first declared in the same class |
| 872 | if (data().HasBasesWithFields) |
| 873 | data().IsStandardLayout = false; |
| 874 | |
| Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 875 | // C++ [class.bit]p2: |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 876 | // A declaration for a bit-field that omits the identifier declares an |
| 877 | // unnamed bit-field. Unnamed bit-fields are not members and cannot be |
| Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 878 | // initialized. |
| Richard Smith | b5a317f | 2018-05-07 06:43:30 +0000 | [diff] [blame] | 879 | if (Field->isUnnamedBitfield()) { |
| 880 | // C++ [meta.unary.prop]p4: [LWG2358] |
| 881 | // T is a class type [...] with [...] no unnamed bit-fields of non-zero |
| 882 | // length |
| 883 | if (data().Empty && !Field->isZeroLengthBitField(Context) && |
| 884 | Context.getLangOpts().getClangABICompat() > |
| 885 | LangOptions::ClangABI::Ver6) |
| 886 | data().Empty = false; |
| Douglas Gregor | 556e586 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 887 | return; |
| Richard Smith | b5a317f | 2018-05-07 06:43:30 +0000 | [diff] [blame] | 888 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 889 | |
| Richard Smith | b6070db | 2018-04-05 18:55:37 +0000 | [diff] [blame] | 890 | // C++11 [class]p7: |
| 891 | // A standard-layout class is a class that: |
| 892 | // -- either has no non-static data members in the most derived class |
| 893 | // [...] or has no base classes with non-static data members |
| 894 | if (data().HasBasesWithNonStaticDataMembers) |
| 895 | data().IsCXX11StandardLayout = false; |
| 896 | |
| Douglas Gregor | a832d3e | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 897 | // C++ [dcl.init.aggr]p1: |
| 898 | // An aggregate is an array or a class (clause 9) with [...] no |
| 899 | // private or protected non-static data members (clause 11). |
| 900 | // |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 901 | // A POD must be an aggregate. |
| Douglas Gregor | a832d3e | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 902 | if (D->getAccess() == AS_private || D->getAccess() == AS_protected) { |
| 903 | data().Aggregate = false; |
| 904 | data().PlainOldData = false; |
| 905 | } |
| Chandler Carruth | b196374 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 906 | |
| Richard Smith | b6070db | 2018-04-05 18:55:37 +0000 | [diff] [blame] | 907 | // Track whether this is the first field. We use this when checking |
| 908 | // whether the class is standard-layout below. |
| 909 | bool IsFirstField = !data().HasPrivateFields && |
| 910 | !data().HasProtectedFields && !data().HasPublicFields; |
| 911 | |
| Chandler Carruth | b196374 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 912 | // C++0x [class]p7: |
| 913 | // A standard-layout class is a class that: |
| 914 | // [...] |
| 915 | // -- has the same access control for all non-static data members, |
| 916 | switch (D->getAccess()) { |
| 917 | case AS_private: data().HasPrivateFields = true; break; |
| 918 | case AS_protected: data().HasProtectedFields = true; break; |
| 919 | case AS_public: data().HasPublicFields = true; break; |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 920 | case AS_none: llvm_unreachable("Invalid access specifier"); |
| Chandler Carruth | b196374 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 921 | }; |
| 922 | if ((data().HasPrivateFields + data().HasProtectedFields + |
| Richard Smith | b6070db | 2018-04-05 18:55:37 +0000 | [diff] [blame] | 923 | data().HasPublicFields) > 1) { |
| Chandler Carruth | 583edf8 | 2011-04-30 10:07:30 +0000 | [diff] [blame] | 924 | data().IsStandardLayout = false; |
| Richard Smith | b6070db | 2018-04-05 18:55:37 +0000 | [diff] [blame] | 925 | data().IsCXX11StandardLayout = false; |
| 926 | } |
| Chandler Carruth | b196374 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 927 | |
| Douglas Gregor | 61226d3 | 2011-05-13 01:05:07 +0000 | [diff] [blame] | 928 | // Keep track of the presence of mutable fields. |
| Richard Smith | 96cd671 | 2017-08-16 01:49:53 +0000 | [diff] [blame] | 929 | if (Field->isMutable()) { |
| Douglas Gregor | 61226d3 | 2011-05-13 01:05:07 +0000 | [diff] [blame] | 930 | data().HasMutableFields = true; |
| Richard Smith | 96cd671 | 2017-08-16 01:49:53 +0000 | [diff] [blame] | 931 | data().NeedOverloadResolutionForCopyConstructor = true; |
| 932 | } |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 933 | |
| 934 | // C++11 [class.union]p8, DR1460: |
| 935 | // If X is a union, a non-static data member of X that is not an anonymous |
| 936 | // union is a variant member of X. |
| 937 | if (isUnion() && !Field->isAnonymousStructOrUnion()) |
| 938 | data().HasVariantMembers = true; |
| 939 | |
| Chandler Carruth | ad7d404 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 940 | // C++0x [class]p9: |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 941 | // A POD struct is a class that is both a trivial class and a |
| 942 | // standard-layout class, and has no non-static data members of type |
| Douglas Gregor | a832d3e | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 943 | // non-POD struct, non-POD union (or array of such types). |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 944 | // |
| 945 | // Automatic Reference Counting: the presence of a member of Objective-C pointer type |
| 946 | // that does not explicitly have no lifetime makes the class a non-POD. |
| Douglas Gregor | a832d3e | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 947 | QualType T = Context.getBaseElementType(Field->getType()); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 948 | if (T->isObjCRetainableType() || T.isObjCGCStrong()) { |
| Brian Kelley | 036603a | 2017-03-29 17:31:42 +0000 | [diff] [blame] | 949 | if (T.hasNonTrivialObjCLifetime()) { |
| Ben Langmuir | 11eab61 | 2014-09-26 15:27:29 +0000 | [diff] [blame] | 950 | // Objective-C Automatic Reference Counting: |
| 951 | // If a class has a non-static data member of Objective-C pointer |
| 952 | // type (or array thereof), it is a non-POD type and its |
| 953 | // default constructor (if any), copy constructor, move constructor, |
| 954 | // copy assignment operator, move assignment operator, and destructor are |
| 955 | // non-trivial. |
| 956 | setHasObjectMember(true); |
| 957 | struct DefinitionData &Data = data(); |
| 958 | Data.PlainOldData = false; |
| 959 | Data.HasTrivialSpecialMembers = 0; |
| Akira Hatanaka | fcbe17c | 2018-03-28 21:13:14 +0000 | [diff] [blame] | 960 | |
| 961 | // __strong or __weak fields do not make special functions non-trivial |
| 962 | // for the purpose of calls. |
| 963 | Qualifiers::ObjCLifetime LT = T.getQualifiers().getObjCLifetime(); |
| 964 | if (LT != Qualifiers::OCL_Strong && LT != Qualifiers::OCL_Weak) |
| 965 | data().HasTrivialSpecialMembersForCall = 0; |
| 966 | |
| 967 | // Structs with __weak fields should never be passed directly. |
| 968 | if (LT == Qualifiers::OCL_Weak) |
| Akira Hatanaka | e6313ac | 2018-04-09 22:48:22 +0000 | [diff] [blame] | 969 | setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs); |
| Akira Hatanaka | fcbe17c | 2018-03-28 21:13:14 +0000 | [diff] [blame] | 970 | |
| Ben Langmuir | 11eab61 | 2014-09-26 15:27:29 +0000 | [diff] [blame] | 971 | Data.HasIrrelevantDestructor = false; |
| Brian Kelley | 036603a | 2017-03-29 17:31:42 +0000 | [diff] [blame] | 972 | } else if (!Context.getLangOpts().ObjCAutoRefCount) { |
| 973 | setHasObjectMember(true); |
| Ben Langmuir | 11eab61 | 2014-09-26 15:27:29 +0000 | [diff] [blame] | 974 | } |
| Eli Friedman | a543332 | 2013-07-20 01:06:31 +0000 | [diff] [blame] | 975 | } else if (!T.isCXX98PODType(Context)) |
| Douglas Gregor | a832d3e | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 976 | data().PlainOldData = false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 977 | |
| Chandler Carruth | b196374 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 978 | if (T->isReferenceType()) { |
| Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 979 | if (!Field->hasInClassInitializer()) |
| 980 | data().HasUninitializedReferenceMember = true; |
| Chandler Carruth | e71d062 | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 981 | |
| Chandler Carruth | b196374 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 982 | // C++0x [class]p7: |
| 983 | // A standard-layout class is a class that: |
| 984 | // -- has no non-static data members of type [...] reference, |
| Chandler Carruth | 583edf8 | 2011-04-30 10:07:30 +0000 | [diff] [blame] | 985 | data().IsStandardLayout = false; |
| Richard Smith | b6070db | 2018-04-05 18:55:37 +0000 | [diff] [blame] | 986 | data().IsCXX11StandardLayout = false; |
| Richard Smith | 96cd671 | 2017-08-16 01:49:53 +0000 | [diff] [blame] | 987 | |
| 988 | // C++1z [class.copy.ctor]p10: |
| 989 | // A defaulted copy constructor for a class X is defined as deleted if X has: |
| 990 | // -- a non-static data member of rvalue reference type |
| 991 | if (T->isRValueReferenceType()) |
| 992 | data().DefaultedCopyConstructorIsDeleted = true; |
| Chandler Carruth | b196374 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 993 | } |
| 994 | |
| Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 995 | if (!Field->hasInClassInitializer() && !Field->isMutable()) { |
| Richard Smith | 9b18217 | 2016-10-28 19:11:18 +0000 | [diff] [blame] | 996 | if (CXXRecordDecl *FieldType = T->getAsCXXRecordDecl()) { |
| Nico Weber | 344abaa | 2016-02-19 02:51:07 +0000 | [diff] [blame] | 997 | if (FieldType->hasDefinition() && !FieldType->allowConstDefaultInit()) |
| Nico Weber | 6a6376b | 2016-02-19 01:52:46 +0000 | [diff] [blame] | 998 | data().HasUninitializedFields = true; |
| 999 | } else { |
| 1000 | data().HasUninitializedFields = true; |
| 1001 | } |
| 1002 | } |
| 1003 | |
| Richard Smith | 3607ffe | 2012-02-13 03:54:03 +0000 | [diff] [blame] | 1004 | // Record if this field is the first non-literal or volatile field or base. |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1005 | if (!T->isLiteralType(Context) || T.isVolatileQualified()) |
| Chandler Carruth | e71d062 | 2011-04-24 02:49:34 +0000 | [diff] [blame] | 1006 | data().HasNonLiteralTypeFieldsOrBases = true; |
| 1007 | |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 1008 | if (Field->hasInClassInitializer() || |
| 1009 | (Field->isAnonymousStructOrUnion() && |
| 1010 | Field->getType()->getAsCXXRecordDecl()->hasInClassInitializer())) { |
| Richard Smith | e2648ba | 2012-05-07 01:07:30 +0000 | [diff] [blame] | 1011 | data().HasInClassInitializer = true; |
| 1012 | |
| 1013 | // C++11 [class]p5: |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1014 | // A default constructor is trivial if [...] no non-static data member |
| 1015 | // of its class has a brace-or-equal-initializer. |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 1016 | data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor; |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1017 | |
| Richard Smith | e2648ba | 2012-05-07 01:07:30 +0000 | [diff] [blame] | 1018 | // C++11 [dcl.init.aggr]p1: |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1019 | // An aggregate is a [...] class with [...] no |
| 1020 | // brace-or-equal-initializers for non-static data members. |
| Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1021 | // |
| Richard Smith | 872307e | 2016-03-08 22:17:41 +0000 | [diff] [blame] | 1022 | // This rule was removed in C++14. |
| Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 1023 | if (!getASTContext().getLangOpts().CPlusPlus14) |
| Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1024 | data().Aggregate = false; |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1025 | |
| Richard Smith | e2648ba | 2012-05-07 01:07:30 +0000 | [diff] [blame] | 1026 | // C++11 [class]p10: |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1027 | // A POD struct is [...] a trivial class. |
| 1028 | data().PlainOldData = false; |
| 1029 | } |
| 1030 | |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 1031 | // C++11 [class.copy]p23: |
| 1032 | // A defaulted copy/move assignment operator for a class X is defined |
| 1033 | // as deleted if X has: |
| 1034 | // -- a non-static data member of reference type |
| 1035 | if (T->isReferenceType()) |
| 1036 | data().DefaultedMoveAssignmentIsDeleted = true; |
| 1037 | |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1038 | if (const auto *RecordTy = T->getAs<RecordType>()) { |
| 1039 | auto *FieldRec = cast<CXXRecordDecl>(RecordTy->getDecl()); |
| Douglas Gregor | 11c024b | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 1040 | if (FieldRec->getDefinition()) { |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 1041 | addedClassSubobject(FieldRec); |
| 1042 | |
| Richard Smith | c91d12c | 2013-11-25 07:07:05 +0000 | [diff] [blame] | 1043 | // We may need to perform overload resolution to determine whether a |
| 1044 | // field can be moved if it's const or volatile qualified. |
| 1045 | if (T.getCVRQualifiers() & (Qualifiers::Const | Qualifiers::Volatile)) { |
| Richard Smith | 96cd671 | 2017-08-16 01:49:53 +0000 | [diff] [blame] | 1046 | // We need to care about 'const' for the copy constructor because an |
| 1047 | // implicit copy constructor might be declared with a non-const |
| 1048 | // parameter. |
| 1049 | data().NeedOverloadResolutionForCopyConstructor = true; |
| Richard Smith | c91d12c | 2013-11-25 07:07:05 +0000 | [diff] [blame] | 1050 | data().NeedOverloadResolutionForMoveConstructor = true; |
| 1051 | data().NeedOverloadResolutionForMoveAssignment = true; |
| 1052 | } |
| 1053 | |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 1054 | // C++11 [class.ctor]p5, C++11 [class.copy]p11: |
| 1055 | // A defaulted [special member] for a class X is defined as |
| 1056 | // deleted if: |
| 1057 | // -- X is a union-like class that has a variant member with a |
| 1058 | // non-trivial [corresponding special member] |
| 1059 | if (isUnion()) { |
| Richard Smith | 96cd671 | 2017-08-16 01:49:53 +0000 | [diff] [blame] | 1060 | if (FieldRec->hasNonTrivialCopyConstructor()) |
| 1061 | data().DefaultedCopyConstructorIsDeleted = true; |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 1062 | if (FieldRec->hasNonTrivialMoveConstructor()) |
| 1063 | data().DefaultedMoveConstructorIsDeleted = true; |
| 1064 | if (FieldRec->hasNonTrivialMoveAssignment()) |
| 1065 | data().DefaultedMoveAssignmentIsDeleted = true; |
| 1066 | if (FieldRec->hasNonTrivialDestructor()) |
| 1067 | data().DefaultedDestructorIsDeleted = true; |
| 1068 | } |
| 1069 | |
| Richard Smith | dd5619f | 2016-08-16 00:13:47 +0000 | [diff] [blame] | 1070 | // For an anonymous union member, our overload resolution will perform |
| 1071 | // overload resolution for its members. |
| 1072 | if (Field->isAnonymousStructOrUnion()) { |
| Richard Smith | 96cd671 | 2017-08-16 01:49:53 +0000 | [diff] [blame] | 1073 | data().NeedOverloadResolutionForCopyConstructor |= |
| 1074 | FieldRec->data().NeedOverloadResolutionForCopyConstructor; |
| Richard Smith | dd5619f | 2016-08-16 00:13:47 +0000 | [diff] [blame] | 1075 | data().NeedOverloadResolutionForMoveConstructor |= |
| 1076 | FieldRec->data().NeedOverloadResolutionForMoveConstructor; |
| 1077 | data().NeedOverloadResolutionForMoveAssignment |= |
| 1078 | FieldRec->data().NeedOverloadResolutionForMoveAssignment; |
| 1079 | data().NeedOverloadResolutionForDestructor |= |
| 1080 | FieldRec->data().NeedOverloadResolutionForDestructor; |
| 1081 | } |
| 1082 | |
| Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 1083 | // C++0x [class.ctor]p5: |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 1084 | // A default constructor is trivial [...] if: |
| Alexis Hunt | f479f1b | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 1085 | // -- for all the non-static data members of its class that are of |
| 1086 | // class type (or array thereof), each such class has a trivial |
| 1087 | // default constructor. |
| 1088 | if (!FieldRec->hasTrivialDefaultConstructor()) |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 1089 | data().HasTrivialSpecialMembers &= ~SMF_DefaultConstructor; |
| Chandler Carruth | ad7d404 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 1090 | |
| 1091 | // C++0x [class.copy]p13: |
| 1092 | // A copy/move constructor for class X is trivial if [...] |
| 1093 | // [...] |
| 1094 | // -- for each non-static data member of X that is of class type (or |
| 1095 | // an array thereof), the constructor selected to copy/move that |
| 1096 | // member is trivial; |
| Douglas Gregor | 11c024b | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 1097 | if (!FieldRec->hasTrivialCopyConstructor()) |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 1098 | data().HasTrivialSpecialMembers &= ~SMF_CopyConstructor; |
| Akira Hatanaka | 02914dc | 2018-02-05 20:23:22 +0000 | [diff] [blame] | 1099 | |
| 1100 | if (!FieldRec->hasTrivialCopyConstructorForCall()) |
| 1101 | data().HasTrivialSpecialMembersForCall &= ~SMF_CopyConstructor; |
| 1102 | |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 1103 | // If the field doesn't have a simple move constructor, we'll eagerly |
| 1104 | // declare the move constructor for this class and we'll decide whether |
| 1105 | // it's trivial then. |
| Richard Smith | 1648847 | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 1106 | if (!FieldRec->hasTrivialMoveConstructor()) |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 1107 | data().HasTrivialSpecialMembers &= ~SMF_MoveConstructor; |
| Chandler Carruth | ad7d404 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 1108 | |
| Akira Hatanaka | 02914dc | 2018-02-05 20:23:22 +0000 | [diff] [blame] | 1109 | if (!FieldRec->hasTrivialMoveConstructorForCall()) |
| 1110 | data().HasTrivialSpecialMembersForCall &= ~SMF_MoveConstructor; |
| 1111 | |
| Chandler Carruth | ad7d404 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 1112 | // C++0x [class.copy]p27: |
| 1113 | // A copy/move assignment operator for class X is trivial if [...] |
| 1114 | // [...] |
| 1115 | // -- for each non-static data member of X that is of class type (or |
| 1116 | // an array thereof), the assignment operator selected to |
| 1117 | // copy/move that member is trivial; |
| Douglas Gregor | 11c024b | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 1118 | if (!FieldRec->hasTrivialCopyAssignment()) |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 1119 | data().HasTrivialSpecialMembers &= ~SMF_CopyAssignment; |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 1120 | // If the field doesn't have a simple move assignment, we'll eagerly |
| 1121 | // declare the move assignment for this class and we'll decide whether |
| 1122 | // it's trivial then. |
| Richard Smith | 1648847 | 2012-11-16 00:53:38 +0000 | [diff] [blame] | 1123 | if (!FieldRec->hasTrivialMoveAssignment()) |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 1124 | data().HasTrivialSpecialMembers &= ~SMF_MoveAssignment; |
| Chandler Carruth | ad7d404 | 2011-04-23 23:10:33 +0000 | [diff] [blame] | 1125 | |
| Douglas Gregor | 11c024b | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 1126 | if (!FieldRec->hasTrivialDestructor()) |
| Richard Smith | 328aae5 | 2012-11-30 05:11:39 +0000 | [diff] [blame] | 1127 | data().HasTrivialSpecialMembers &= ~SMF_Destructor; |
| Akira Hatanaka | 02914dc | 2018-02-05 20:23:22 +0000 | [diff] [blame] | 1128 | if (!FieldRec->hasTrivialDestructorForCall()) |
| 1129 | data().HasTrivialSpecialMembersForCall &= ~SMF_Destructor; |
| Richard Smith | 561fb15 | 2012-02-25 07:33:38 +0000 | [diff] [blame] | 1130 | if (!FieldRec->hasIrrelevantDestructor()) |
| 1131 | data().HasIrrelevantDestructor = false; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1132 | if (FieldRec->hasObjectMember()) |
| 1133 | setHasObjectMember(true); |
| Fariborz Jahanian | 7865220 | 2013-01-25 23:57:05 +0000 | [diff] [blame] | 1134 | if (FieldRec->hasVolatileMember()) |
| 1135 | setHasVolatileMember(true); |
| Akira Hatanaka | e6313ac | 2018-04-09 22:48:22 +0000 | [diff] [blame] | 1136 | if (FieldRec->getArgPassingRestrictions() == |
| 1137 | RecordDecl::APK_CanNeverPassInRegs) |
| 1138 | setArgPassingRestrictions(RecordDecl::APK_CanNeverPassInRegs); |
| Chandler Carruth | b196374 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 1139 | |
| 1140 | // C++0x [class]p7: |
| 1141 | // A standard-layout class is a class that: |
| 1142 | // -- has no non-static data members of type non-standard-layout |
| 1143 | // class (or array of such types) [...] |
| Chandler Carruth | 583edf8 | 2011-04-30 10:07:30 +0000 | [diff] [blame] | 1144 | if (!FieldRec->isStandardLayout()) |
| 1145 | data().IsStandardLayout = false; |
| Richard Smith | b6070db | 2018-04-05 18:55:37 +0000 | [diff] [blame] | 1146 | if (!FieldRec->isCXX11StandardLayout()) |
| 1147 | data().IsCXX11StandardLayout = false; |
| Chandler Carruth | b196374 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 1148 | |
| Richard Smith | b6070db | 2018-04-05 18:55:37 +0000 | [diff] [blame] | 1149 | // C++2a [class]p7: |
| Chandler Carruth | b196374 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 1150 | // A standard-layout class is a class that: |
| 1151 | // [...] |
| Richard Smith | b6070db | 2018-04-05 18:55:37 +0000 | [diff] [blame] | 1152 | // -- has no element of the set M(S) of types as a base class. |
| 1153 | if (data().IsStandardLayout && (isUnion() || IsFirstField) && |
| 1154 | hasSubobjectAtOffsetZeroOfEmptyBaseType(Context, FieldRec)) |
| 1155 | data().IsStandardLayout = false; |
| 1156 | |
| 1157 | // C++11 [class]p7: |
| 1158 | // A standard-layout class is a class that: |
| Chandler Carruth | b196374 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 1159 | // -- has no base classes of the same type as the first non-static |
| Richard Smith | b6070db | 2018-04-05 18:55:37 +0000 | [diff] [blame] | 1160 | // data member |
| 1161 | if (data().IsCXX11StandardLayout && IsFirstField) { |
| 1162 | // FIXME: We should check all base classes here, not just direct |
| 1163 | // base classes. |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1164 | for (const auto &BI : bases()) { |
| 1165 | if (Context.hasSameUnqualifiedType(BI.getType(), T)) { |
| Richard Smith | b6070db | 2018-04-05 18:55:37 +0000 | [diff] [blame] | 1166 | data().IsCXX11StandardLayout = false; |
| Chandler Carruth | b196374 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 1167 | break; |
| 1168 | } |
| 1169 | } |
| 1170 | } |
| Richard Smith | b6070db | 2018-04-05 18:55:37 +0000 | [diff] [blame] | 1171 | |
| Douglas Gregor | 61226d3 | 2011-05-13 01:05:07 +0000 | [diff] [blame] | 1172 | // Keep track of the presence of mutable fields. |
| Richard Smith | 96cd671 | 2017-08-16 01:49:53 +0000 | [diff] [blame] | 1173 | if (FieldRec->hasMutableFields()) { |
| Douglas Gregor | 61226d3 | 2011-05-13 01:05:07 +0000 | [diff] [blame] | 1174 | data().HasMutableFields = true; |
| Richard Smith | 96cd671 | 2017-08-16 01:49:53 +0000 | [diff] [blame] | 1175 | data().NeedOverloadResolutionForCopyConstructor = true; |
| 1176 | } |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 1177 | |
| 1178 | // C++11 [class.copy]p13: |
| 1179 | // If the implicitly-defined constructor would satisfy the |
| 1180 | // requirements of a constexpr constructor, the implicitly-defined |
| 1181 | // constructor is constexpr. |
| 1182 | // C++11 [dcl.constexpr]p4: |
| 1183 | // -- every constructor involved in initializing non-static data |
| 1184 | // members [...] shall be a constexpr constructor |
| 1185 | if (!Field->hasInClassInitializer() && |
| Richard Smith | e2648ba | 2012-05-07 01:07:30 +0000 | [diff] [blame] | 1186 | !FieldRec->hasConstexprDefaultConstructor() && !isUnion()) |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 1187 | // The standard requires any in-class initializer to be a constant |
| 1188 | // expression. We consider this to be a defect. |
| 1189 | data().DefaultedDefaultConstructorIsConstexpr = false; |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 1190 | |
| 1191 | // C++11 [class.copy]p8: |
| 1192 | // The implicitly-declared copy constructor for a class X will have |
| Richard Smith | df054d3 | 2017-02-25 23:53:05 +0000 | [diff] [blame] | 1193 | // the form 'X::X(const X&)' if each potentially constructed subobject |
| 1194 | // of a class type M (or array thereof) has a copy constructor whose |
| 1195 | // first parameter is of type 'const M&' or 'const volatile M&'. |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 1196 | if (!FieldRec->hasCopyConstructorWithConstParam()) |
| Richard Smith | df054d3 | 2017-02-25 23:53:05 +0000 | [diff] [blame] | 1197 | data().ImplicitCopyConstructorCanHaveConstParamForNonVBase = false; |
| Richard Smith | 1c33fe8 | 2012-11-28 06:23:12 +0000 | [diff] [blame] | 1198 | |
| 1199 | // C++11 [class.copy]p18: |
| 1200 | // The implicitly-declared copy assignment oeprator for a class X will |
| 1201 | // have the form 'X& X::operator=(const X&)' if [...] for all the |
| 1202 | // non-static data members of X that are of a class type M (or array |
| 1203 | // thereof), each such class type has a copy assignment operator whose |
| 1204 | // parameter is of type 'const M&', 'const volatile M&' or 'M'. |
| 1205 | if (!FieldRec->hasCopyAssignmentWithConstParam()) |
| 1206 | data().ImplicitCopyAssignmentHasConstParam = false; |
| Richard Smith | 593f993 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 1207 | |
| 1208 | if (FieldRec->hasUninitializedReferenceMember() && |
| 1209 | !Field->hasInClassInitializer()) |
| 1210 | data().HasUninitializedReferenceMember = true; |
| Richard Smith | ab44d5b | 2013-12-10 08:25:00 +0000 | [diff] [blame] | 1211 | |
| 1212 | // C++11 [class.union]p8, DR1460: |
| 1213 | // a non-static data member of an anonymous union that is a member of |
| 1214 | // X is also a variant member of X. |
| 1215 | if (FieldRec->hasVariantMembers() && |
| 1216 | Field->isAnonymousStructOrUnion()) |
| 1217 | data().HasVariantMembers = true; |
| Douglas Gregor | 11c024b | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 1218 | } |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 1219 | } else { |
| 1220 | // Base element type of field is a non-class type. |
| Richard Smith | d9f663b | 2013-04-22 15:31:51 +0000 | [diff] [blame] | 1221 | if (!T->isLiteralType(Context) || |
| Richard Smith | 4086a13 | 2012-06-10 07:07:24 +0000 | [diff] [blame] | 1222 | (!Field->hasInClassInitializer() && !isUnion())) |
| Richard Smith | cc36f69 | 2011-12-22 02:22:31 +0000 | [diff] [blame] | 1223 | data().DefaultedDefaultConstructorIsConstexpr = false; |
| Richard Smith | 6b02d46 | 2012-12-08 08:32:28 +0000 | [diff] [blame] | 1224 | |
| 1225 | // C++11 [class.copy]p23: |
| 1226 | // A defaulted copy/move assignment operator for a class X is defined |
| 1227 | // as deleted if X has: |
| 1228 | // -- a non-static data member of const non-class type (or array |
| 1229 | // thereof) |
| 1230 | if (T.isConstQualified()) |
| 1231 | data().DefaultedMoveAssignmentIsDeleted = true; |
| Douglas Gregor | 11c024b | 2010-09-28 20:50:54 +0000 | [diff] [blame] | 1232 | } |
| Chandler Carruth | b196374 | 2011-04-30 09:17:45 +0000 | [diff] [blame] | 1233 | |
| David Majnemer | 5cfda6f | 2016-05-22 05:34:26 +0000 | [diff] [blame] | 1234 | // C++14 [meta.unary.prop]p4: |
| Richard Smith | b5a317f | 2018-05-07 06:43:30 +0000 | [diff] [blame] | 1235 | // T is a class type [...] with [...] no non-static data members |
| 1236 | data().Empty = false; |
| Douglas Gregor | a832d3e | 2010-09-28 19:45:33 +0000 | [diff] [blame] | 1237 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1238 | |
| Douglas Gregor | 457104e | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 1239 | // Handle using declarations of conversion functions. |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1240 | if (auto *Shadow = dyn_cast<UsingShadowDecl>(D)) { |
| Douglas Gregor | 457104e | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 1241 | if (Shadow->getDeclName().getNameKind() |
| Richard Smith | a4ba74c | 2013-08-30 04:46:40 +0000 | [diff] [blame] | 1242 | == DeclarationName::CXXConversionFunctionName) { |
| 1243 | ASTContext &Ctx = getASTContext(); |
| 1244 | data().Conversions.get(Ctx).addDecl(Ctx, Shadow, Shadow->getAccess()); |
| 1245 | } |
| 1246 | } |
| Richard Smith | 12e7931 | 2016-05-13 06:47:56 +0000 | [diff] [blame] | 1247 | |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1248 | if (const auto *Using = dyn_cast<UsingDecl>(D)) { |
| Richard Smith | 12e7931 | 2016-05-13 06:47:56 +0000 | [diff] [blame] | 1249 | if (Using->getDeclName().getNameKind() == |
| Eric Fiselier | 283d8d4 | 2016-12-03 01:26:47 +0000 | [diff] [blame] | 1250 | DeclarationName::CXXConstructorName) { |
| Richard Smith | 12e7931 | 2016-05-13 06:47:56 +0000 | [diff] [blame] | 1251 | data().HasInheritedConstructor = true; |
| Eric Fiselier | 283d8d4 | 2016-12-03 01:26:47 +0000 | [diff] [blame] | 1252 | // C++1z [dcl.init.aggr]p1: |
| 1253 | // An aggregate is [...] a class [...] with no inherited constructors |
| 1254 | data().Aggregate = false; |
| 1255 | } |
| Richard Smith | 12e7931 | 2016-05-13 06:47:56 +0000 | [diff] [blame] | 1256 | |
| 1257 | if (Using->getDeclName().getCXXOverloadedOperator() == OO_Equal) |
| 1258 | data().HasInheritedAssignment = true; |
| 1259 | } |
| Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 1262 | void CXXRecordDecl::finishedDefaultedOrDeletedMember(CXXMethodDecl *D) { |
| 1263 | assert(!D->isImplicit() && !D->isUserProvided()); |
| 1264 | |
| 1265 | // The kind of special member this declaration is, if any. |
| 1266 | unsigned SMKind = 0; |
| 1267 | |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1268 | if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 1269 | if (Constructor->isDefaultConstructor()) { |
| 1270 | SMKind |= SMF_DefaultConstructor; |
| 1271 | if (Constructor->isConstexpr()) |
| 1272 | data().HasConstexprDefaultConstructor = true; |
| 1273 | } |
| 1274 | if (Constructor->isCopyConstructor()) |
| 1275 | SMKind |= SMF_CopyConstructor; |
| 1276 | else if (Constructor->isMoveConstructor()) |
| 1277 | SMKind |= SMF_MoveConstructor; |
| 1278 | else if (Constructor->isConstexpr()) |
| 1279 | // We may now know that the constructor is constexpr. |
| 1280 | data().HasConstexprNonCopyMoveConstructor = true; |
| Stephan Tolksdorf | d85fa23 | 2014-03-27 20:23:12 +0000 | [diff] [blame] | 1281 | } else if (isa<CXXDestructorDecl>(D)) { |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 1282 | SMKind |= SMF_Destructor; |
| Stephan Tolksdorf | d85fa23 | 2014-03-27 20:23:12 +0000 | [diff] [blame] | 1283 | if (!D->isTrivial() || D->getAccess() != AS_public || D->isDeleted()) |
| 1284 | data().HasIrrelevantDestructor = false; |
| 1285 | } else if (D->isCopyAssignmentOperator()) |
| Richard Smith | 92f241f | 2012-12-08 02:53:02 +0000 | [diff] [blame] | 1286 | SMKind |= SMF_CopyAssignment; |
| 1287 | else if (D->isMoveAssignmentOperator()) |
| 1288 | SMKind |= SMF_MoveAssignment; |
| 1289 | |
| 1290 | // Update which trivial / non-trivial special members we have. |
| 1291 | // addedMember will have skipped this step for this member. |
| 1292 | if (D->isTrivial()) |
| 1293 | data().HasTrivialSpecialMembers |= SMKind; |
| 1294 | else |
| 1295 | data().DeclaredNonTrivialSpecialMembers |= SMKind; |
| 1296 | } |
| 1297 | |
| Akira Hatanaka | 02914dc | 2018-02-05 20:23:22 +0000 | [diff] [blame] | 1298 | void CXXRecordDecl::setTrivialForCallFlags(CXXMethodDecl *D) { |
| 1299 | unsigned SMKind = 0; |
| 1300 | |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1301 | if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
| Akira Hatanaka | 02914dc | 2018-02-05 20:23:22 +0000 | [diff] [blame] | 1302 | if (Constructor->isCopyConstructor()) |
| 1303 | SMKind = SMF_CopyConstructor; |
| 1304 | else if (Constructor->isMoveConstructor()) |
| 1305 | SMKind = SMF_MoveConstructor; |
| 1306 | } else if (isa<CXXDestructorDecl>(D)) |
| 1307 | SMKind = SMF_Destructor; |
| 1308 | |
| 1309 | if (D->isTrivialForCall()) |
| 1310 | data().HasTrivialSpecialMembersForCall |= SMKind; |
| 1311 | else |
| 1312 | data().DeclaredNonTrivialSpecialMembersForCall |= SMKind; |
| 1313 | } |
| 1314 | |
| Joao Matos | e9a3ed4 | 2012-08-31 22:18:20 +0000 | [diff] [blame] | 1315 | bool CXXRecordDecl::isCLike() const { |
| 1316 | if (getTagKind() == TTK_Class || getTagKind() == TTK_Interface || |
| 1317 | !TemplateOrInstantiation.isNull()) |
| 1318 | return false; |
| 1319 | if (!hasDefinition()) |
| 1320 | return true; |
| Argyrios Kyrtzidis | 5c4e065 | 2012-01-23 16:58:45 +0000 | [diff] [blame] | 1321 | |
| Argyrios Kyrtzidis | c3c9ee5 | 2012-02-01 06:36:44 +0000 | [diff] [blame] | 1322 | return isPOD() && data().HasOnlyCMembers; |
| Argyrios Kyrtzidis | 5c4e065 | 2012-01-23 16:58:45 +0000 | [diff] [blame] | 1323 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1324 | |
| 1325 | bool CXXRecordDecl::isGenericLambda() const { |
| Faisal Vali | c1a6dc4 | 2013-10-23 16:10:50 +0000 | [diff] [blame] | 1326 | if (!isLambda()) return false; |
| 1327 | return getLambdaData().IsGenericLambda; |
| Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | CXXMethodDecl* CXXRecordDecl::getLambdaCallOperator() const { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1331 | if (!isLambda()) return nullptr; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1332 | DeclarationName Name = |
| Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 1333 | getASTContext().DeclarationNames.getCXXOperatorName(OO_Call); |
| Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 1334 | DeclContext::lookup_result Calls = lookup(Name); |
| Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 1335 | |
| 1336 | assert(!Calls.empty() && "Missing lambda call operator!"); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1337 | assert(Calls.size() == 1 && "More than one lambda call operator!"); |
| 1338 | |
| Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 1339 | NamedDecl *CallOp = Calls.front(); |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1340 | if (const auto *CallOpTmpl = dyn_cast<FunctionTemplateDecl>(CallOp)) |
| Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 1341 | return cast<CXXMethodDecl>(CallOpTmpl->getTemplatedDecl()); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1342 | |
| Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 1343 | return cast<CXXMethodDecl>(CallOp); |
| 1344 | } |
| 1345 | |
| 1346 | CXXMethodDecl* CXXRecordDecl::getLambdaStaticInvoker() const { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1347 | if (!isLambda()) return nullptr; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1348 | DeclarationName Name = |
| Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 1349 | &getASTContext().Idents.get(getLambdaStaticInvokerName()); |
| Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 1350 | DeclContext::lookup_result Invoker = lookup(Name); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1351 | if (Invoker.empty()) return nullptr; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1352 | assert(Invoker.size() == 1 && "More than one static invoker operator!"); |
| Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 1353 | NamedDecl *InvokerFun = Invoker.front(); |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1354 | if (const auto *InvokerTemplate = dyn_cast<FunctionTemplateDecl>(InvokerFun)) |
| Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 1355 | return cast<CXXMethodDecl>(InvokerTemplate->getTemplatedDecl()); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1356 | |
| 1357 | return cast<CXXMethodDecl>(InvokerFun); |
| Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
| Douglas Gregor | 9c70220 | 2012-02-10 07:45:31 +0000 | [diff] [blame] | 1360 | void CXXRecordDecl::getCaptureFields( |
| 1361 | llvm::DenseMap<const VarDecl *, FieldDecl *> &Captures, |
| Eli Friedman | 7e7da2d | 2012-02-11 00:18:00 +0000 | [diff] [blame] | 1362 | FieldDecl *&ThisCapture) const { |
| Douglas Gregor | 9c70220 | 2012-02-10 07:45:31 +0000 | [diff] [blame] | 1363 | Captures.clear(); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1364 | ThisCapture = nullptr; |
| Douglas Gregor | 9c70220 | 2012-02-10 07:45:31 +0000 | [diff] [blame] | 1365 | |
| Douglas Gregor | c8a7349 | 2012-02-13 15:44:47 +0000 | [diff] [blame] | 1366 | LambdaDefinitionData &Lambda = getLambdaData(); |
| Douglas Gregor | 9c70220 | 2012-02-10 07:45:31 +0000 | [diff] [blame] | 1367 | RecordDecl::field_iterator Field = field_begin(); |
| Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 1368 | for (const LambdaCapture *C = Lambda.Captures, *CEnd = C + Lambda.NumCaptures; |
| Douglas Gregor | 9c70220 | 2012-02-10 07:45:31 +0000 | [diff] [blame] | 1369 | C != CEnd; ++C, ++Field) { |
| Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 1370 | if (C->capturesThis()) |
| David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1371 | ThisCapture = *Field; |
| Richard Smith | ba71c08 | 2013-05-16 06:20:58 +0000 | [diff] [blame] | 1372 | else if (C->capturesVariable()) |
| 1373 | Captures[C->getCapturedVar()] = *Field; |
| Douglas Gregor | 9c70220 | 2012-02-10 07:45:31 +0000 | [diff] [blame] | 1374 | } |
| Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 1375 | assert(Field == field_end()); |
| Douglas Gregor | 9c70220 | 2012-02-10 07:45:31 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1378 | TemplateParameterList * |
| Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 1379 | CXXRecordDecl::getGenericLambdaTemplateParameterList() const { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1380 | if (!isLambda()) return nullptr; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1381 | CXXMethodDecl *CallOp = getLambdaCallOperator(); |
| Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 1382 | if (FunctionTemplateDecl *Tmpl = CallOp->getDescribedFunctionTemplate()) |
| 1383 | return Tmpl->getTemplateParameters(); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1384 | return nullptr; |
| Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 1385 | } |
| Douglas Gregor | 9c70220 | 2012-02-10 07:45:31 +0000 | [diff] [blame] | 1386 | |
| Richard Smith | 0bae624 | 2016-08-25 00:34:00 +0000 | [diff] [blame] | 1387 | Decl *CXXRecordDecl::getLambdaContextDecl() const { |
| 1388 | assert(isLambda() && "Not a lambda closure type!"); |
| 1389 | ExternalASTSource *Source = getParentASTContext().getExternalSource(); |
| 1390 | return getLambdaData().ContextDecl.get(Source); |
| 1391 | } |
| 1392 | |
| John McCall | 1e3a1a7 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1393 | static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) { |
| Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 1394 | QualType T = |
| 1395 | cast<CXXConversionDecl>(Conv->getUnderlyingDecl()->getAsFunction()) |
| 1396 | ->getConversionType(); |
| John McCall | 1e3a1a7 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1397 | return Context.getCanonicalType(T); |
| Fariborz Jahanian | 3ee21f1 | 2009-10-07 20:43:36 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| John McCall | 1e3a1a7 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1400 | /// Collect the visible conversions of a base class. |
| 1401 | /// |
| James Dennett | b5d5f81 | 2012-06-15 22:28:09 +0000 | [diff] [blame] | 1402 | /// \param Record a base class of the class we're considering |
| John McCall | 1e3a1a7 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1403 | /// \param InVirtual whether this base class is a virtual base (or a base |
| 1404 | /// of a virtual base) |
| 1405 | /// \param Access the access along the inheritance path to this base |
| 1406 | /// \param ParentHiddenTypes the conversions provided by the inheritors |
| 1407 | /// of this base |
| 1408 | /// \param Output the set to which to add conversions from non-virtual bases |
| 1409 | /// \param VOutput the set to which to add conversions from virtual bases |
| 1410 | /// \param HiddenVBaseCs the set of conversions which were hidden in a |
| 1411 | /// virtual base along some inheritance path |
| 1412 | static void CollectVisibleConversions(ASTContext &Context, |
| 1413 | CXXRecordDecl *Record, |
| 1414 | bool InVirtual, |
| 1415 | AccessSpecifier Access, |
| 1416 | const llvm::SmallPtrSet<CanQualType, 8> &ParentHiddenTypes, |
| Argyrios Kyrtzidis | 0f05fb9 | 2012-11-28 03:56:16 +0000 | [diff] [blame] | 1417 | ASTUnresolvedSet &Output, |
| John McCall | 1e3a1a7 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1418 | UnresolvedSetImpl &VOutput, |
| 1419 | llvm::SmallPtrSet<NamedDecl*, 8> &HiddenVBaseCs) { |
| 1420 | // The set of types which have conversions in this class or its |
| 1421 | // subclasses. As an optimization, we don't copy the derived set |
| 1422 | // unless it might change. |
| 1423 | const llvm::SmallPtrSet<CanQualType, 8> *HiddenTypes = &ParentHiddenTypes; |
| 1424 | llvm::SmallPtrSet<CanQualType, 8> HiddenTypesBuffer; |
| 1425 | |
| 1426 | // Collect the direct conversions and figure out which conversions |
| 1427 | // will be hidden in the subclasses. |
| Argyrios Kyrtzidis | a6567c4 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 1428 | CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin(); |
| 1429 | CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end(); |
| 1430 | if (ConvI != ConvE) { |
| John McCall | 1e3a1a7 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1431 | HiddenTypesBuffer = ParentHiddenTypes; |
| 1432 | HiddenTypes = &HiddenTypesBuffer; |
| 1433 | |
| Argyrios Kyrtzidis | a6567c4 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 1434 | for (CXXRecordDecl::conversion_iterator I = ConvI; I != ConvE; ++I) { |
| Richard Smith | 99fdf8d | 2012-05-06 00:04:32 +0000 | [diff] [blame] | 1435 | CanQualType ConvType(GetConversionType(Context, I.getDecl())); |
| 1436 | bool Hidden = ParentHiddenTypes.count(ConvType); |
| 1437 | if (!Hidden) |
| 1438 | HiddenTypesBuffer.insert(ConvType); |
| John McCall | 1e3a1a7 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1439 | |
| 1440 | // If this conversion is hidden and we're in a virtual base, |
| 1441 | // remember that it's hidden along some inheritance path. |
| 1442 | if (Hidden && InVirtual) |
| 1443 | HiddenVBaseCs.insert(cast<NamedDecl>(I.getDecl()->getCanonicalDecl())); |
| 1444 | |
| 1445 | // If this conversion isn't hidden, add it to the appropriate output. |
| 1446 | else if (!Hidden) { |
| 1447 | AccessSpecifier IAccess |
| 1448 | = CXXRecordDecl::MergeAccess(Access, I.getAccess()); |
| 1449 | |
| 1450 | if (InVirtual) |
| 1451 | VOutput.addDecl(I.getDecl(), IAccess); |
| Fariborz Jahanian | b394f50 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 1452 | else |
| Argyrios Kyrtzidis | 0f05fb9 | 2012-11-28 03:56:16 +0000 | [diff] [blame] | 1453 | Output.addDecl(Context, I.getDecl(), IAccess); |
| Fariborz Jahanian | b54ccb2 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 1454 | } |
| 1455 | } |
| 1456 | } |
| Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 1457 | |
| John McCall | 1e3a1a7 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1458 | // Collect information recursively from any base classes. |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1459 | for (const auto &I : Record->bases()) { |
| 1460 | const RecordType *RT = I.getType()->getAs<RecordType>(); |
| John McCall | 1e3a1a7 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1461 | if (!RT) continue; |
| Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 1462 | |
| John McCall | 1e3a1a7 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1463 | AccessSpecifier BaseAccess |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1464 | = CXXRecordDecl::MergeAccess(Access, I.getAccessSpecifier()); |
| 1465 | bool BaseInVirtual = InVirtual || I.isVirtual(); |
| Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 1466 | |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1467 | auto *Base = cast<CXXRecordDecl>(RT->getDecl()); |
| John McCall | 1e3a1a7 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1468 | CollectVisibleConversions(Context, Base, BaseInVirtual, BaseAccess, |
| 1469 | *HiddenTypes, Output, VOutput, HiddenVBaseCs); |
| Fariborz Jahanian | b54ccb2 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 1470 | } |
| John McCall | 1e3a1a7 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1471 | } |
| Sebastian Redl | 1054fae | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 1472 | |
| John McCall | 1e3a1a7 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1473 | /// Collect the visible conversions of a class. |
| 1474 | /// |
| 1475 | /// This would be extremely straightforward if it weren't for virtual |
| 1476 | /// bases. It might be worth special-casing that, really. |
| 1477 | static void CollectVisibleConversions(ASTContext &Context, |
| 1478 | CXXRecordDecl *Record, |
| Argyrios Kyrtzidis | 0f05fb9 | 2012-11-28 03:56:16 +0000 | [diff] [blame] | 1479 | ASTUnresolvedSet &Output) { |
| John McCall | 1e3a1a7 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1480 | // The collection of all conversions in virtual bases that we've |
| 1481 | // found. These will be added to the output as long as they don't |
| 1482 | // appear in the hidden-conversions set. |
| 1483 | UnresolvedSet<8> VBaseCs; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1484 | |
| John McCall | 1e3a1a7 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1485 | // The set of conversions in virtual bases that we've determined to |
| 1486 | // be hidden. |
| 1487 | llvm::SmallPtrSet<NamedDecl*, 8> HiddenVBaseCs; |
| 1488 | |
| 1489 | // The set of types hidden by classes derived from this one. |
| 1490 | llvm::SmallPtrSet<CanQualType, 8> HiddenTypes; |
| 1491 | |
| 1492 | // Go ahead and collect the direct conversions and add them to the |
| 1493 | // hidden-types set. |
| Argyrios Kyrtzidis | a6567c4 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 1494 | CXXRecordDecl::conversion_iterator ConvI = Record->conversion_begin(); |
| 1495 | CXXRecordDecl::conversion_iterator ConvE = Record->conversion_end(); |
| Argyrios Kyrtzidis | 0f05fb9 | 2012-11-28 03:56:16 +0000 | [diff] [blame] | 1496 | Output.append(Context, ConvI, ConvE); |
| Argyrios Kyrtzidis | a6567c4 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 1497 | for (; ConvI != ConvE; ++ConvI) |
| 1498 | HiddenTypes.insert(GetConversionType(Context, ConvI.getDecl())); |
| John McCall | 1e3a1a7 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1499 | |
| 1500 | // Recursively collect conversions from base classes. |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1501 | for (const auto &I : Record->bases()) { |
| 1502 | const RecordType *RT = I.getType()->getAs<RecordType>(); |
| John McCall | 1e3a1a7 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1503 | if (!RT) continue; |
| 1504 | |
| 1505 | CollectVisibleConversions(Context, cast<CXXRecordDecl>(RT->getDecl()), |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1506 | I.isVirtual(), I.getAccessSpecifier(), |
| John McCall | 1e3a1a7 | 2010-03-15 09:07:48 +0000 | [diff] [blame] | 1507 | HiddenTypes, Output, VBaseCs, HiddenVBaseCs); |
| 1508 | } |
| 1509 | |
| 1510 | // Add any unhidden conversions provided by virtual bases. |
| 1511 | for (UnresolvedSetIterator I = VBaseCs.begin(), E = VBaseCs.end(); |
| 1512 | I != E; ++I) { |
| 1513 | if (!HiddenVBaseCs.count(cast<NamedDecl>(I.getDecl()->getCanonicalDecl()))) |
| Argyrios Kyrtzidis | 0f05fb9 | 2012-11-28 03:56:16 +0000 | [diff] [blame] | 1514 | Output.addDecl(Context, I.getDecl(), I.getAccess()); |
| Fariborz Jahanian | b54ccb2 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 1515 | } |
| Fariborz Jahanian | b394f50 | 2009-09-12 18:26:03 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | /// getVisibleConversionFunctions - get all conversion functions visible |
| 1519 | /// in current class; including conversion function templates. |
| Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 1520 | llvm::iterator_range<CXXRecordDecl::conversion_iterator> |
| Argyrios Kyrtzidis | a6567c4 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 1521 | CXXRecordDecl::getVisibleConversionFunctions() { |
| Richard Smith | a4ba74c | 2013-08-30 04:46:40 +0000 | [diff] [blame] | 1522 | ASTContext &Ctx = getASTContext(); |
| 1523 | |
| 1524 | ASTUnresolvedSet *Set; |
| 1525 | if (bases_begin() == bases_end()) { |
| 1526 | // If root class, all conversions are visible. |
| 1527 | Set = &data().Conversions.get(Ctx); |
| 1528 | } else { |
| 1529 | Set = &data().VisibleConversions.get(Ctx); |
| 1530 | // If visible conversion list is not evaluated, evaluate it. |
| 1531 | if (!data().ComputedVisibleConversions) { |
| 1532 | CollectVisibleConversions(Ctx, this, *Set); |
| 1533 | data().ComputedVisibleConversions = true; |
| 1534 | } |
| Argyrios Kyrtzidis | a6567c4 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 1535 | } |
| Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 1536 | return llvm::make_range(Set->begin(), Set->end()); |
| Fariborz Jahanian | b54ccb2 | 2009-09-11 21:44:33 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
| John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1539 | void CXXRecordDecl::removeConversion(const NamedDecl *ConvDecl) { |
| 1540 | // This operation is O(N) but extremely rare. Sema only uses it to |
| 1541 | // remove UsingShadowDecls in a class that were followed by a direct |
| 1542 | // declaration, e.g.: |
| 1543 | // class A : B { |
| 1544 | // using B::operator int; |
| 1545 | // operator int(); |
| 1546 | // }; |
| 1547 | // This is uncommon by itself and even more uncommon in conjunction |
| 1548 | // with sufficiently large numbers of directly-declared conversions |
| 1549 | // that asymptotic behavior matters. |
| 1550 | |
| Richard Smith | a4ba74c | 2013-08-30 04:46:40 +0000 | [diff] [blame] | 1551 | ASTUnresolvedSet &Convs = data().Conversions.get(getASTContext()); |
| John McCall | da4458e | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 1552 | for (unsigned I = 0, E = Convs.size(); I != E; ++I) { |
| 1553 | if (Convs[I].getDecl() == ConvDecl) { |
| 1554 | Convs.erase(I); |
| 1555 | assert(std::find(Convs.begin(), Convs.end(), ConvDecl) == Convs.end() |
| 1556 | && "conversion was found multiple times in unresolved set"); |
| 1557 | return; |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | llvm_unreachable("conversion not found in set!"); |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1562 | } |
| Fariborz Jahanian | 423a81f | 2009-06-19 19:55:27 +0000 | [diff] [blame] | 1563 | |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1564 | CXXRecordDecl *CXXRecordDecl::getInstantiatedFromMemberClass() const { |
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1565 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1566 | return cast<CXXRecordDecl>(MSInfo->getInstantiatedFrom()); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1567 | |
| 1568 | return nullptr; |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
| Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 1571 | MemberSpecializationInfo *CXXRecordDecl::getMemberSpecializationInfo() const { |
| 1572 | return TemplateOrInstantiation.dyn_cast<MemberSpecializationInfo *>(); |
| 1573 | } |
| 1574 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1575 | void |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1576 | CXXRecordDecl::setInstantiationOfMemberClass(CXXRecordDecl *RD, |
| 1577 | TemplateSpecializationKind TSK) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1578 | assert(TemplateOrInstantiation.isNull() && |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1579 | "Previous template or instantiation?"); |
| Richard Smith | 8a0dde7 | 2013-12-14 01:04:22 +0000 | [diff] [blame] | 1580 | assert(!isa<ClassTemplatePartialSpecializationDecl>(this)); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1581 | TemplateOrInstantiation |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1582 | = new (getASTContext()) MemberSpecializationInfo(RD, TSK); |
| 1583 | } |
| 1584 | |
| Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 1585 | ClassTemplateDecl *CXXRecordDecl::getDescribedClassTemplate() const { |
| 1586 | return TemplateOrInstantiation.dyn_cast<ClassTemplateDecl *>(); |
| 1587 | } |
| 1588 | |
| 1589 | void CXXRecordDecl::setDescribedClassTemplate(ClassTemplateDecl *Template) { |
| 1590 | TemplateOrInstantiation = Template; |
| 1591 | } |
| 1592 | |
| Anders Carlsson | 27cfc6e | 2009-12-07 06:33:48 +0000 | [diff] [blame] | 1593 | TemplateSpecializationKind CXXRecordDecl::getTemplateSpecializationKind() const{ |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1594 | if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(this)) |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1595 | return Spec->getSpecializationKind(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1596 | |
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1597 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1598 | return MSInfo->getTemplateSpecializationKind(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1599 | |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1600 | return TSK_Undeclared; |
| 1601 | } |
| 1602 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1603 | void |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1604 | CXXRecordDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK) { |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1605 | if (auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(this)) { |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1606 | Spec->setSpecializationKind(TSK); |
| 1607 | return; |
| 1608 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1609 | |
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1610 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) { |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1611 | MSInfo->setTemplateSpecializationKind(TSK); |
| 1612 | return; |
| 1613 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1614 | |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1615 | llvm_unreachable("Not a class template or member class specialization"); |
| Douglas Gregor | bbe8f46 | 2009-10-08 15:14:33 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
| Reid Kleckner | e7367d6 | 2014-10-14 20:28:40 +0000 | [diff] [blame] | 1618 | const CXXRecordDecl *CXXRecordDecl::getTemplateInstantiationPattern() const { |
| Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 1619 | auto GetDefinitionOrSelf = |
| 1620 | [](const CXXRecordDecl *D) -> const CXXRecordDecl * { |
| 1621 | if (auto *Def = D->getDefinition()) |
| 1622 | return Def; |
| 1623 | return D; |
| 1624 | }; |
| 1625 | |
| Reid Kleckner | e7367d6 | 2014-10-14 20:28:40 +0000 | [diff] [blame] | 1626 | // If it's a class template specialization, find the template or partial |
| 1627 | // specialization from which it was instantiated. |
| 1628 | if (auto *TD = dyn_cast<ClassTemplateSpecializationDecl>(this)) { |
| 1629 | auto From = TD->getInstantiatedFrom(); |
| 1630 | if (auto *CTD = From.dyn_cast<ClassTemplateDecl *>()) { |
| 1631 | while (auto *NewCTD = CTD->getInstantiatedFromMemberTemplate()) { |
| 1632 | if (NewCTD->isMemberSpecialization()) |
| 1633 | break; |
| 1634 | CTD = NewCTD; |
| 1635 | } |
| Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 1636 | return GetDefinitionOrSelf(CTD->getTemplatedDecl()); |
| Reid Kleckner | e7367d6 | 2014-10-14 20:28:40 +0000 | [diff] [blame] | 1637 | } |
| 1638 | if (auto *CTPSD = |
| 1639 | From.dyn_cast<ClassTemplatePartialSpecializationDecl *>()) { |
| 1640 | while (auto *NewCTPSD = CTPSD->getInstantiatedFromMember()) { |
| 1641 | if (NewCTPSD->isMemberSpecialization()) |
| 1642 | break; |
| 1643 | CTPSD = NewCTPSD; |
| 1644 | } |
| Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 1645 | return GetDefinitionOrSelf(CTPSD); |
| Reid Kleckner | e7367d6 | 2014-10-14 20:28:40 +0000 | [diff] [blame] | 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) { |
| 1650 | if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) { |
| 1651 | const CXXRecordDecl *RD = this; |
| 1652 | while (auto *NewRD = RD->getInstantiatedFromMemberClass()) |
| 1653 | RD = NewRD; |
| Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 1654 | return GetDefinitionOrSelf(RD); |
| Reid Kleckner | e7367d6 | 2014-10-14 20:28:40 +0000 | [diff] [blame] | 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | assert(!isTemplateInstantiation(this->getTemplateSpecializationKind()) && |
| 1659 | "couldn't find pattern for class template instantiation"); |
| 1660 | return nullptr; |
| 1661 | } |
| 1662 | |
| Douglas Gregor | bac7490 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 1663 | CXXDestructorDecl *CXXRecordDecl::getDestructor() const { |
| 1664 | ASTContext &Context = getASTContext(); |
| Anders Carlsson | 0a63741 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 1665 | QualType ClassType = Context.getTypeDeclType(this); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1666 | |
| 1667 | DeclarationName Name |
| Douglas Gregor | 2211d34 | 2009-08-05 05:36:45 +0000 | [diff] [blame] | 1668 | = Context.DeclarationNames.getCXXDestructorName( |
| 1669 | Context.getCanonicalType(ClassType)); |
| Anders Carlsson | 0a63741 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 1670 | |
| Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 1671 | DeclContext::lookup_result R = lookup(Name); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1672 | |
| Hubert Tong | 3cede51 | 2017-06-30 22:43:54 +0000 | [diff] [blame] | 1673 | return R.empty() ? nullptr : dyn_cast<CXXDestructorDecl>(R.front()); |
| Anders Carlsson | 0a63741 | 2009-05-29 21:03:38 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
| Richard Trieu | 95a192a | 2015-05-28 00:14:02 +0000 | [diff] [blame] | 1676 | bool CXXRecordDecl::isAnyDestructorNoReturn() const { |
| 1677 | // Destructor is noreturn. |
| 1678 | if (const CXXDestructorDecl *Destructor = getDestructor()) |
| 1679 | if (Destructor->isNoReturn()) |
| 1680 | return true; |
| 1681 | |
| 1682 | // Check base classes destructor for noreturn. |
| 1683 | for (const auto &Base : bases()) |
| Alexander Kornienko | 2933334 | 2017-05-12 11:24:25 +0000 | [diff] [blame] | 1684 | if (const CXXRecordDecl *RD = Base.getType()->getAsCXXRecordDecl()) |
| 1685 | if (RD->isAnyDestructorNoReturn()) |
| 1686 | return true; |
| Richard Trieu | 95a192a | 2015-05-28 00:14:02 +0000 | [diff] [blame] | 1687 | |
| 1688 | // Check fields for noreturn. |
| 1689 | for (const auto *Field : fields()) |
| 1690 | if (const CXXRecordDecl *RD = |
| 1691 | Field->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl()) |
| 1692 | if (RD->isAnyDestructorNoReturn()) |
| 1693 | return true; |
| 1694 | |
| 1695 | // All destructors are not noreturn. |
| 1696 | return false; |
| 1697 | } |
| 1698 | |
| Erich Keane | 3e08f66 | 2017-09-29 21:06:00 +0000 | [diff] [blame] | 1699 | static bool isDeclContextInNamespace(const DeclContext *DC) { |
| 1700 | while (!DC->isTranslationUnit()) { |
| 1701 | if (DC->isNamespace()) |
| 1702 | return true; |
| 1703 | DC = DC->getParent(); |
| 1704 | } |
| 1705 | return false; |
| 1706 | } |
| 1707 | |
| Erich Keane | 58bd603 | 2017-09-15 16:03:35 +0000 | [diff] [blame] | 1708 | bool CXXRecordDecl::isInterfaceLike() const { |
| 1709 | assert(hasDefinition() && "checking for interface-like without a definition"); |
| 1710 | // All __interfaces are inheritently interface-like. |
| 1711 | if (isInterface()) |
| 1712 | return true; |
| 1713 | |
| 1714 | // Interface-like types cannot have a user declared constructor, destructor, |
| 1715 | // friends, VBases, conversion functions, or fields. Additionally, lambdas |
| 1716 | // cannot be interface types. |
| 1717 | if (isLambda() || hasUserDeclaredConstructor() || |
| 1718 | hasUserDeclaredDestructor() || !field_empty() || hasFriends() || |
| 1719 | getNumVBases() > 0 || conversion_end() - conversion_begin() > 0) |
| 1720 | return false; |
| 1721 | |
| 1722 | // No interface-like type can have a method with a definition. |
| 1723 | for (const auto *const Method : methods()) |
| Erich Keane | 3e08f66 | 2017-09-29 21:06:00 +0000 | [diff] [blame] | 1724 | if (Method->isDefined() && !Method->isImplicit()) |
| Erich Keane | 58bd603 | 2017-09-15 16:03:35 +0000 | [diff] [blame] | 1725 | return false; |
| 1726 | |
| 1727 | // Check "Special" types. |
| 1728 | const auto *Uuid = getAttr<UuidAttr>(); |
| Erich Keane | 3e08f66 | 2017-09-29 21:06:00 +0000 | [diff] [blame] | 1729 | // MS SDK declares IUnknown/IDispatch both in the root of a TU, or in an |
| 1730 | // extern C++ block directly in the TU. These are only valid if in one |
| 1731 | // of these two situations. |
| 1732 | if (Uuid && isStruct() && !getDeclContext()->isExternCContext() && |
| 1733 | !isDeclContextInNamespace(getDeclContext()) && |
| Erich Keane | 58bd603 | 2017-09-15 16:03:35 +0000 | [diff] [blame] | 1734 | ((getName() == "IUnknown" && |
| 1735 | Uuid->getGuid() == "00000000-0000-0000-C000-000000000046") || |
| 1736 | (getName() == "IDispatch" && |
| 1737 | Uuid->getGuid() == "00020400-0000-0000-C000-000000000046"))) { |
| 1738 | if (getNumBases() > 0) |
| 1739 | return false; |
| 1740 | return true; |
| 1741 | } |
| 1742 | |
| 1743 | // FIXME: Any access specifiers is supposed to make this no longer interface |
| 1744 | // like. |
| 1745 | |
| 1746 | // If this isn't a 'special' type, it must have a single interface-like base. |
| 1747 | if (getNumBases() != 1) |
| 1748 | return false; |
| 1749 | |
| 1750 | const auto BaseSpec = *bases_begin(); |
| 1751 | if (BaseSpec.isVirtual() || BaseSpec.getAccessSpecifier() != AS_public) |
| 1752 | return false; |
| 1753 | const auto *Base = BaseSpec.getType()->getAsCXXRecordDecl(); |
| 1754 | if (Base->isInterface() || !Base->isInterfaceLike()) |
| 1755 | return false; |
| 1756 | return true; |
| 1757 | } |
| 1758 | |
| Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 1759 | void CXXRecordDecl::completeDefinition() { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1760 | completeDefinition(nullptr); |
| Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | void CXXRecordDecl::completeDefinition(CXXFinalOverriderMap *FinalOverriders) { |
| 1764 | RecordDecl::completeDefinition(); |
| Richard Smith | 96cd671 | 2017-08-16 01:49:53 +0000 | [diff] [blame] | 1765 | |
| Douglas Gregor | 8fb9512 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 1766 | // If the class may be abstract (but hasn't been marked as such), check for |
| 1767 | // any pure final overriders. |
| 1768 | if (mayBeAbstract()) { |
| 1769 | CXXFinalOverriderMap MyFinalOverriders; |
| 1770 | if (!FinalOverriders) { |
| 1771 | getFinalOverriders(MyFinalOverriders); |
| 1772 | FinalOverriders = &MyFinalOverriders; |
| 1773 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1774 | |
| Douglas Gregor | 8fb9512 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 1775 | bool Done = false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1776 | for (CXXFinalOverriderMap::iterator M = FinalOverriders->begin(), |
| Douglas Gregor | 8fb9512 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 1777 | MEnd = FinalOverriders->end(); |
| 1778 | M != MEnd && !Done; ++M) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1779 | for (OverridingMethods::iterator SO = M->second.begin(), |
| Douglas Gregor | 8fb9512 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 1780 | SOEnd = M->second.end(); |
| 1781 | SO != SOEnd && !Done; ++SO) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1782 | assert(SO->second.size() > 0 && |
| Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 1783 | "All virtual functions have overriding virtual functions"); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1784 | |
| Douglas Gregor | 8fb9512 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 1785 | // C++ [class.abstract]p4: |
| 1786 | // A class is abstract if it contains or inherits at least one |
| 1787 | // pure virtual function for which the final overrider is pure |
| 1788 | // virtual. |
| 1789 | if (SO->second.front().Method->isPure()) { |
| 1790 | data().Abstract = true; |
| 1791 | Done = true; |
| 1792 | break; |
| 1793 | } |
| 1794 | } |
| 1795 | } |
| 1796 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1797 | |
| Douglas Gregor | 457104e | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 1798 | // Set access bits correctly on the directly-declared conversions. |
| Richard Smith | a4ba74c | 2013-08-30 04:46:40 +0000 | [diff] [blame] | 1799 | for (conversion_iterator I = conversion_begin(), E = conversion_end(); |
| Douglas Gregor | 457104e | 2010-09-29 04:25:11 +0000 | [diff] [blame] | 1800 | I != E; ++I) |
| Argyrios Kyrtzidis | 0f05fb9 | 2012-11-28 03:56:16 +0000 | [diff] [blame] | 1801 | I.setAccess((*I)->getAccess()); |
| Douglas Gregor | 8fb9512 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | bool CXXRecordDecl::mayBeAbstract() const { |
| 1805 | if (data().Abstract || isInvalidDecl() || !data().Polymorphic || |
| 1806 | isDependentContext()) |
| 1807 | return false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1808 | |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1809 | for (const auto &B : bases()) { |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1810 | const auto *BaseDecl = |
| 1811 | cast<CXXRecordDecl>(B.getType()->getAs<RecordType>()->getDecl()); |
| Douglas Gregor | 8fb9512 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 1812 | if (BaseDecl->isAbstract()) |
| 1813 | return true; |
| 1814 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1815 | |
| Douglas Gregor | 8fb9512 | 2010-09-29 00:15:42 +0000 | [diff] [blame] | 1816 | return false; |
| 1817 | } |
| 1818 | |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 1819 | void CXXDeductionGuideDecl::anchor() {} |
| Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1820 | |
| 1821 | CXXDeductionGuideDecl *CXXDeductionGuideDecl::Create( |
| 1822 | ASTContext &C, DeclContext *DC, SourceLocation StartLoc, bool IsExplicit, |
| 1823 | const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo, |
| 1824 | SourceLocation EndLocation) { |
| 1825 | return new (C, DC) CXXDeductionGuideDecl(C, DC, StartLoc, IsExplicit, |
| 1826 | NameInfo, T, TInfo, EndLocation); |
| 1827 | } |
| 1828 | |
| 1829 | CXXDeductionGuideDecl *CXXDeductionGuideDecl::CreateDeserialized(ASTContext &C, |
| 1830 | unsigned ID) { |
| 1831 | return new (C, ID) CXXDeductionGuideDecl(C, nullptr, SourceLocation(), false, |
| 1832 | DeclarationNameInfo(), QualType(), |
| 1833 | nullptr, SourceLocation()); |
| 1834 | } |
| 1835 | |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 1836 | void CXXMethodDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 1837 | |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1838 | bool CXXMethodDecl::isStatic() const { |
| Rafael Espindola | 29cda59 | 2013-04-15 12:38:20 +0000 | [diff] [blame] | 1839 | const CXXMethodDecl *MD = getCanonicalDecl(); |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1840 | |
| 1841 | if (MD->getStorageClass() == SC_Static) |
| 1842 | return true; |
| 1843 | |
| Reid Kleckner | 9a7f3e6 | 2013-10-08 00:58:57 +0000 | [diff] [blame] | 1844 | OverloadedOperatorKind OOK = getDeclName().getCXXOverloadedOperator(); |
| 1845 | return isStaticOverloadedOperator(OOK); |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
| Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 1848 | static bool recursivelyOverrides(const CXXMethodDecl *DerivedMD, |
| 1849 | const CXXMethodDecl *BaseMD) { |
| Benjamin Kramer | acfa339 | 2017-12-17 23:52:45 +0000 | [diff] [blame] | 1850 | for (const CXXMethodDecl *MD : DerivedMD->overridden_methods()) { |
| Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 1851 | if (MD->getCanonicalDecl() == BaseMD->getCanonicalDecl()) |
| 1852 | return true; |
| 1853 | if (recursivelyOverrides(MD, BaseMD)) |
| 1854 | return true; |
| 1855 | } |
| 1856 | return false; |
| 1857 | } |
| 1858 | |
| 1859 | CXXMethodDecl * |
| Jordan Rose | 5fc5da0 | 2012-08-15 20:07:17 +0000 | [diff] [blame] | 1860 | CXXMethodDecl::getCorrespondingMethodInClass(const CXXRecordDecl *RD, |
| 1861 | bool MayBeBase) { |
| Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 1862 | if (this->getParent()->getCanonicalDecl() == RD->getCanonicalDecl()) |
| 1863 | return this; |
| 1864 | |
| 1865 | // Lookup doesn't work for destructors, so handle them separately. |
| 1866 | if (isa<CXXDestructorDecl>(this)) { |
| 1867 | CXXMethodDecl *MD = RD->getDestructor(); |
| Jordan Rose | 5fc5da0 | 2012-08-15 20:07:17 +0000 | [diff] [blame] | 1868 | if (MD) { |
| 1869 | if (recursivelyOverrides(MD, this)) |
| 1870 | return MD; |
| 1871 | if (MayBeBase && recursivelyOverrides(this, MD)) |
| 1872 | return MD; |
| 1873 | } |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1874 | return nullptr; |
| Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
| Richard Smith | 40c7806 | 2015-02-21 02:31:57 +0000 | [diff] [blame] | 1877 | for (auto *ND : RD->lookup(getDeclName())) { |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1878 | auto *MD = dyn_cast<CXXMethodDecl>(ND); |
| Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 1879 | if (!MD) |
| 1880 | continue; |
| 1881 | if (recursivelyOverrides(MD, this)) |
| 1882 | return MD; |
| Jordan Rose | 5fc5da0 | 2012-08-15 20:07:17 +0000 | [diff] [blame] | 1883 | if (MayBeBase && recursivelyOverrides(this, MD)) |
| 1884 | return MD; |
| Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
| Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1887 | for (const auto &I : RD->bases()) { |
| 1888 | const RecordType *RT = I.getType()->getAs<RecordType>(); |
| Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 1889 | if (!RT) |
| 1890 | continue; |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1891 | const auto *Base = cast<CXXRecordDecl>(RT->getDecl()); |
| Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 1892 | CXXMethodDecl *T = this->getCorrespondingMethodInClass(Base); |
| 1893 | if (T) |
| 1894 | return T; |
| 1895 | } |
| 1896 | |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1897 | return nullptr; |
| Rafael Espindola | 49e860b | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
| Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1900 | CXXMethodDecl * |
| 1901 | CXXMethodDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1902 | SourceLocation StartLoc, |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1903 | const DeclarationNameInfo &NameInfo, |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1904 | QualType T, TypeSourceInfo *TInfo, |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1905 | StorageClass SC, bool isInline, |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 1906 | bool isConstexpr, SourceLocation EndLocation) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1907 | return new (C, RD) CXXMethodDecl(CXXMethod, C, RD, StartLoc, NameInfo, |
| 1908 | T, TInfo, SC, isInline, isConstexpr, |
| 1909 | EndLocation); |
| Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1912 | CXXMethodDecl *CXXMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1913 | return new (C, ID) CXXMethodDecl(CXXMethod, C, nullptr, SourceLocation(), |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1914 | DeclarationNameInfo(), QualType(), nullptr, |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 1915 | SC_None, false, false, SourceLocation()); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
| Akira Hatanaka | 2246167 | 2017-07-13 06:08:27 +0000 | [diff] [blame] | 1918 | CXXMethodDecl *CXXMethodDecl::getDevirtualizedMethod(const Expr *Base, |
| 1919 | bool IsAppleKext) { |
| 1920 | assert(isVirtual() && "this method is expected to be virtual"); |
| 1921 | |
| 1922 | // When building with -fapple-kext, all calls must go through the vtable since |
| 1923 | // the kernel linker can do runtime patching of vtables. |
| 1924 | if (IsAppleKext) |
| 1925 | return nullptr; |
| 1926 | |
| 1927 | // If the member function is marked 'final', we know that it can't be |
| 1928 | // overridden and can therefore devirtualize it unless it's pure virtual. |
| 1929 | if (hasAttr<FinalAttr>()) |
| 1930 | return isPure() ? nullptr : this; |
| 1931 | |
| 1932 | // If Base is unknown, we cannot devirtualize. |
| 1933 | if (!Base) |
| 1934 | return nullptr; |
| 1935 | |
| 1936 | // If the base expression (after skipping derived-to-base conversions) is a |
| 1937 | // class prvalue, then we can devirtualize. |
| 1938 | Base = Base->getBestDynamicClassTypeExpr(); |
| 1939 | if (Base->isRValue() && Base->getType()->isRecordType()) |
| 1940 | return this; |
| 1941 | |
| 1942 | // If we don't even know what we would call, we can't devirtualize. |
| 1943 | const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType(); |
| 1944 | if (!BestDynamicDecl) |
| 1945 | return nullptr; |
| 1946 | |
| 1947 | // There may be a method corresponding to MD in a derived class. |
| 1948 | CXXMethodDecl *DevirtualizedMethod = |
| 1949 | getCorrespondingMethodInClass(BestDynamicDecl); |
| 1950 | |
| 1951 | // If that method is pure virtual, we can't devirtualize. If this code is |
| 1952 | // reached, the result would be UB, not a direct call to the derived class |
| 1953 | // function, and we can't assume the derived class function is defined. |
| 1954 | if (DevirtualizedMethod->isPure()) |
| 1955 | return nullptr; |
| 1956 | |
| 1957 | // If that method is marked final, we can devirtualize it. |
| 1958 | if (DevirtualizedMethod->hasAttr<FinalAttr>()) |
| 1959 | return DevirtualizedMethod; |
| 1960 | |
| 1961 | // Similarly, if the class itself is marked 'final' it can't be overridden |
| 1962 | // and we can therefore devirtualize the member function call. |
| 1963 | if (BestDynamicDecl->hasAttr<FinalAttr>()) |
| 1964 | return DevirtualizedMethod; |
| 1965 | |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1966 | if (const auto *DRE = dyn_cast<DeclRefExpr>(Base)) { |
| 1967 | if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) |
| Akira Hatanaka | 2246167 | 2017-07-13 06:08:27 +0000 | [diff] [blame] | 1968 | if (VD->getType()->isRecordType()) |
| 1969 | // This is a record decl. We know the type and can devirtualize it. |
| 1970 | return DevirtualizedMethod; |
| 1971 | |
| 1972 | return nullptr; |
| 1973 | } |
| 1974 | |
| 1975 | // We can devirtualize calls on an object accessed by a class member access |
| 1976 | // expression, since by C++11 [basic.life]p6 we know that it can't refer to |
| 1977 | // a derived class object constructed in the same location. |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 1978 | if (const auto *ME = dyn_cast<MemberExpr>(Base)) { |
| George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 1979 | const ValueDecl *VD = ME->getMemberDecl(); |
| 1980 | return VD->getType()->isRecordType() ? DevirtualizedMethod : nullptr; |
| 1981 | } |
| Akira Hatanaka | 2246167 | 2017-07-13 06:08:27 +0000 | [diff] [blame] | 1982 | |
| 1983 | // Likewise for calls on an object accessed by a (non-reference) pointer to |
| 1984 | // member access. |
| 1985 | if (auto *BO = dyn_cast<BinaryOperator>(Base)) { |
| 1986 | if (BO->isPtrMemOp()) { |
| 1987 | auto *MPT = BO->getRHS()->getType()->castAs<MemberPointerType>(); |
| 1988 | if (MPT->getPointeeType()->isRecordType()) |
| 1989 | return DevirtualizedMethod; |
| 1990 | } |
| 1991 | } |
| 1992 | |
| 1993 | // We can't devirtualize the call. |
| 1994 | return nullptr; |
| 1995 | } |
| 1996 | |
| Douglas Gregor | bb3e12f | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 1997 | bool CXXMethodDecl::isUsualDeallocationFunction() const { |
| 1998 | if (getOverloadedOperator() != OO_Delete && |
| 1999 | getOverloadedOperator() != OO_Array_Delete) |
| 2000 | return false; |
| Douglas Gregor | 6642ca2 | 2010-02-26 05:06:18 +0000 | [diff] [blame] | 2001 | |
| 2002 | // C++ [basic.stc.dynamic.deallocation]p2: |
| 2003 | // A template instance is never a usual deallocation function, |
| 2004 | // regardless of its signature. |
| 2005 | if (getPrimaryTemplate()) |
| 2006 | return false; |
| 2007 | |
| Douglas Gregor | bb3e12f | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 2008 | // C++ [basic.stc.dynamic.deallocation]p2: |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2009 | // If a class T has a member deallocation function named operator delete |
| Douglas Gregor | bb3e12f | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 2010 | // with exactly one parameter, then that function is a usual (non-placement) |
| 2011 | // deallocation function. [...] |
| 2012 | if (getNumParams() == 1) |
| 2013 | return true; |
| Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2014 | unsigned UsualParams = 1; |
| Richard Smith | 96269c5 | 2016-09-29 22:49:46 +0000 | [diff] [blame] | 2015 | |
| Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 2016 | // C++ P0722: |
| 2017 | // A destroying operator delete is a usual deallocation function if |
| 2018 | // removing the std::destroying_delete_t parameter and changing the |
| 2019 | // first parameter type from T* to void* results in the signature of |
| 2020 | // a usual deallocation function. |
| 2021 | if (isDestroyingOperatorDelete()) |
| 2022 | ++UsualParams; |
| 2023 | |
| Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2024 | // C++ <=14 [basic.stc.dynamic.deallocation]p2: |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2025 | // [...] If class T does not declare such an operator delete but does |
| 2026 | // declare a member deallocation function named operator delete with |
| Douglas Gregor | bb3e12f | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 2027 | // exactly two parameters, the second of which has type std::size_t (18.1), |
| 2028 | // then this function is a usual deallocation function. |
| Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2029 | // |
| 2030 | // C++17 says a usual deallocation function is one with the signature |
| 2031 | // (void* [, size_t] [, std::align_val_t] [, ...]) |
| 2032 | // and all such functions are usual deallocation functions. It's not clear |
| 2033 | // that allowing varargs functions was intentional. |
| Douglas Gregor | bb3e12f | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 2034 | ASTContext &Context = getASTContext(); |
| Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2035 | if (UsualParams < getNumParams() && |
| 2036 | Context.hasSameUnqualifiedType(getParamDecl(UsualParams)->getType(), |
| 2037 | Context.getSizeType())) |
| 2038 | ++UsualParams; |
| 2039 | |
| 2040 | if (UsualParams < getNumParams() && |
| 2041 | getParamDecl(UsualParams)->getType()->isAlignValT()) |
| 2042 | ++UsualParams; |
| 2043 | |
| 2044 | if (UsualParams != getNumParams()) |
| Douglas Gregor | bb3e12f | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 2045 | return false; |
| Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2046 | |
| 2047 | // In C++17 onwards, all potential usual deallocation functions are actual |
| 2048 | // usual deallocation functions. |
| 2049 | if (Context.getLangOpts().AlignedAllocation) |
| 2050 | return true; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2051 | |
| 2052 | // This function is a usual deallocation function if there are no |
| Douglas Gregor | bb3e12f | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 2053 | // single-parameter deallocation functions of the same kind. |
| Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 2054 | DeclContext::lookup_result R = getDeclContext()->lookup(getDeclName()); |
| 2055 | for (DeclContext::lookup_result::iterator I = R.begin(), E = R.end(); |
| David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2056 | I != E; ++I) { |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2057 | if (const auto *FD = dyn_cast<FunctionDecl>(*I)) |
| Douglas Gregor | bb3e12f | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 2058 | if (FD->getNumParams() == 1) |
| 2059 | return false; |
| 2060 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2061 | |
| Douglas Gregor | bb3e12f | 2009-09-29 18:16:17 +0000 | [diff] [blame] | 2062 | return true; |
| 2063 | } |
| 2064 | |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 2065 | bool CXXMethodDecl::isCopyAssignmentOperator() const { |
| Alexis Hunt | fcaeae4 | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 2066 | // C++0x [class.copy]p17: |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2067 | // A user-declared copy assignment operator X::operator= is a non-static |
| 2068 | // non-template member function of class X with exactly one parameter of |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 2069 | // type X, X&, const X&, volatile X& or const volatile X&. |
| 2070 | if (/*operator=*/getOverloadedOperator() != OO_Equal || |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2071 | /*non-static*/ isStatic() || |
| Eli Friedman | 84c0143e | 2013-07-11 23:55:07 +0000 | [diff] [blame] | 2072 | /*non-template*/getPrimaryTemplate() || getDescribedFunctionTemplate() || |
| 2073 | getNumParams() != 1) |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 2074 | return false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2075 | |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 2076 | QualType ParamType = getParamDecl(0)->getType(); |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2077 | if (const auto *Ref = ParamType->getAs<LValueReferenceType>()) |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 2078 | ParamType = Ref->getPointeeType(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2079 | |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 2080 | ASTContext &Context = getASTContext(); |
| 2081 | QualType ClassType |
| 2082 | = Context.getCanonicalType(Context.getTypeDeclType(getParent())); |
| 2083 | return Context.hasSameUnqualifiedType(ClassType, ParamType); |
| 2084 | } |
| 2085 | |
| Alexis Hunt | fcaeae4 | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 2086 | bool CXXMethodDecl::isMoveAssignmentOperator() const { |
| 2087 | // C++0x [class.copy]p19: |
| 2088 | // A user-declared move assignment operator X::operator= is a non-static |
| 2089 | // non-template member function of class X with exactly one parameter of type |
| 2090 | // X&&, const X&&, volatile X&&, or const volatile X&&. |
| 2091 | if (getOverloadedOperator() != OO_Equal || isStatic() || |
| Eli Friedman | 84c0143e | 2013-07-11 23:55:07 +0000 | [diff] [blame] | 2092 | getPrimaryTemplate() || getDescribedFunctionTemplate() || |
| 2093 | getNumParams() != 1) |
| Alexis Hunt | fcaeae4 | 2011-05-25 20:50:04 +0000 | [diff] [blame] | 2094 | return false; |
| 2095 | |
| 2096 | QualType ParamType = getParamDecl(0)->getType(); |
| 2097 | if (!isa<RValueReferenceType>(ParamType)) |
| 2098 | return false; |
| 2099 | ParamType = ParamType->getPointeeType(); |
| 2100 | |
| 2101 | ASTContext &Context = getASTContext(); |
| 2102 | QualType ClassType |
| 2103 | = Context.getCanonicalType(Context.getTypeDeclType(getParent())); |
| 2104 | return Context.hasSameUnqualifiedType(ClassType, ParamType); |
| 2105 | } |
| 2106 | |
| Anders Carlsson | 36d87e1 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 2107 | void CXXMethodDecl::addOverriddenMethod(const CXXMethodDecl *MD) { |
| Anders Carlsson | f3935b4 | 2009-12-04 05:51:56 +0000 | [diff] [blame] | 2108 | assert(MD->isCanonicalDecl() && "Method is not canonical!"); |
| Anders Carlsson | bd32c43 | 2010-01-30 17:42:34 +0000 | [diff] [blame] | 2109 | assert(!MD->getParent()->isDependentContext() && |
| 2110 | "Can't add an overridden method to a class template!"); |
| Eli Friedman | 9135902 | 2012-03-10 01:39:01 +0000 | [diff] [blame] | 2111 | assert(MD->isVirtual() && "Method is not virtual!"); |
| Anders Carlsson | bd32c43 | 2010-01-30 17:42:34 +0000 | [diff] [blame] | 2112 | |
| Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 2113 | getASTContext().addOverriddenMethod(this, MD); |
| Anders Carlsson | 36d87e1 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
| 2116 | CXXMethodDecl::method_iterator CXXMethodDecl::begin_overridden_methods() const { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2117 | if (isa<CXXConstructorDecl>(this)) return nullptr; |
| Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 2118 | return getASTContext().overridden_methods_begin(this); |
| Anders Carlsson | 36d87e1 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 2119 | } |
| 2120 | |
| 2121 | CXXMethodDecl::method_iterator CXXMethodDecl::end_overridden_methods() const { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2122 | if (isa<CXXConstructorDecl>(this)) return nullptr; |
| Douglas Gregor | 832940b | 2010-03-02 23:58:15 +0000 | [diff] [blame] | 2123 | return getASTContext().overridden_methods_end(this); |
| Anders Carlsson | 36d87e1 | 2009-05-16 23:58:37 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
| Argyrios Kyrtzidis | 6685e8a | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 2126 | unsigned CXXMethodDecl::size_overridden_methods() const { |
| Eli Friedman | 9135902 | 2012-03-10 01:39:01 +0000 | [diff] [blame] | 2127 | if (isa<CXXConstructorDecl>(this)) return 0; |
| Argyrios Kyrtzidis | 6685e8a | 2010-07-04 21:44:35 +0000 | [diff] [blame] | 2128 | return getASTContext().overridden_methods_size(this); |
| 2129 | } |
| 2130 | |
| Clement Courbet | 6ecaec8 | 2016-07-05 07:49:31 +0000 | [diff] [blame] | 2131 | CXXMethodDecl::overridden_method_range |
| 2132 | CXXMethodDecl::overridden_methods() const { |
| 2133 | if (isa<CXXConstructorDecl>(this)) |
| 2134 | return overridden_method_range(nullptr, nullptr); |
| 2135 | return getASTContext().overridden_methods(this); |
| 2136 | } |
| 2137 | |
| Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2138 | QualType CXXMethodDecl::getThisType(ASTContext &C) const { |
| Argyrios Kyrtzidis | 962c20e | 2008-10-24 22:28:18 +0000 | [diff] [blame] | 2139 | // C++ 9.3.2p1: The type of this in a member function of a class X is X*. |
| 2140 | // If the member function is declared const, the type of this is const X*, |
| 2141 | // if the member function is declared volatile, the type of this is |
| 2142 | // volatile X*, and if the member function is declared const volatile, |
| 2143 | // the type of this is const volatile X*. |
| 2144 | |
| Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2145 | assert(isInstance() && "No 'this' for static methods!"); |
| Anders Carlsson | 20ee0ed | 2009-06-13 02:59:33 +0000 | [diff] [blame] | 2146 | |
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 2147 | QualType ClassTy = C.getTypeDeclType(getParent()); |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2148 | ClassTy = C.getQualifiedType(ClassTy, |
| Andrey Bokhanko | 67a4186 | 2016-05-26 10:06:01 +0000 | [diff] [blame] | 2149 | Qualifiers::fromCVRUMask(getTypeQualifiers())); |
| Anders Carlsson | 7ca3f6f | 2009-07-10 21:35:09 +0000 | [diff] [blame] | 2150 | return C.getPointerType(ClassTy); |
| Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2151 | } |
| 2152 | |
| Eli Friedman | 71a26d8 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 2153 | bool CXXMethodDecl::hasInlineBody() const { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2154 | // If this function is a template instantiation, look at the template from |
| Douglas Gregor | a318efd | 2010-01-05 19:06:31 +0000 | [diff] [blame] | 2155 | // which it was instantiated. |
| 2156 | const FunctionDecl *CheckFn = getTemplateInstantiationPattern(); |
| 2157 | if (!CheckFn) |
| 2158 | CheckFn = this; |
| Richard Smith | 6c716116 | 2017-08-12 01:46:03 +0000 | [diff] [blame] | 2159 | |
| Eli Friedman | 71a26d8 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 2160 | const FunctionDecl *fn; |
| Richard Smith | 6c716116 | 2017-08-12 01:46:03 +0000 | [diff] [blame] | 2161 | return CheckFn->isDefined(fn) && !fn->isOutOfLine() && |
| 2162 | (fn->doesThisDeclarationHaveABody() || fn->willHaveBody()); |
| Eli Friedman | 71a26d8 | 2009-12-06 20:50:05 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
| Douglas Gregor | 355efbb | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 2165 | bool CXXMethodDecl::isLambdaStaticInvoker() const { |
| Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 2166 | const CXXRecordDecl *P = getParent(); |
| 2167 | if (P->isLambda()) { |
| 2168 | if (const CXXMethodDecl *StaticInvoker = P->getLambdaStaticInvoker()) { |
| 2169 | if (StaticInvoker == this) return true; |
| 2170 | if (P->isGenericLambda() && this->isFunctionTemplateSpecialization()) |
| 2171 | return StaticInvoker == this->getPrimaryTemplate()->getTemplatedDecl(); |
| 2172 | } |
| 2173 | } |
| 2174 | return false; |
| Douglas Gregor | 355efbb | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 2175 | } |
| 2176 | |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2177 | CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, |
| 2178 | TypeSourceInfo *TInfo, bool IsVirtual, |
| 2179 | SourceLocation L, Expr *Init, |
| 2180 | SourceLocation R, |
| 2181 | SourceLocation EllipsisLoc) |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 2182 | : Initializee(TInfo), MemberOrEllipsisLocation(EllipsisLoc), Init(Init), |
| 2183 | LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(IsVirtual), |
| 2184 | IsWritten(false), SourceOrder(0) {} |
| Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2185 | |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2186 | CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, |
| 2187 | FieldDecl *Member, |
| 2188 | SourceLocation MemberLoc, |
| 2189 | SourceLocation L, Expr *Init, |
| 2190 | SourceLocation R) |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 2191 | : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init), |
| 2192 | LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false), |
| 2193 | IsWritten(false), SourceOrder(0) {} |
| Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 2194 | |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2195 | CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, |
| 2196 | IndirectFieldDecl *Member, |
| 2197 | SourceLocation MemberLoc, |
| 2198 | SourceLocation L, Expr *Init, |
| 2199 | SourceLocation R) |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 2200 | : Initializee(Member), MemberOrEllipsisLocation(MemberLoc), Init(Init), |
| 2201 | LParenLoc(L), RParenLoc(R), IsDelegating(false), IsVirtual(false), |
| 2202 | IsWritten(false), SourceOrder(0) {} |
| Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2203 | |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2204 | CXXCtorInitializer::CXXCtorInitializer(ASTContext &Context, |
| Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2205 | TypeSourceInfo *TInfo, |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2206 | SourceLocation L, Expr *Init, |
| Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2207 | SourceLocation R) |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 2208 | : Initializee(TInfo), Init(Init), LParenLoc(L), RParenLoc(R), |
| 2209 | IsDelegating(true), IsVirtual(false), IsWritten(false), SourceOrder(0) {} |
| Alexis Hunt | c5575cc | 2011-02-26 19:13:13 +0000 | [diff] [blame] | 2210 | |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2211 | TypeLoc CXXCtorInitializer::getBaseClassLoc() const { |
| Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 2212 | if (isBaseInitializer()) |
| Alexis Hunt | a50dd46 | 2011-01-08 23:01:16 +0000 | [diff] [blame] | 2213 | return Initializee.get<TypeSourceInfo*>()->getTypeLoc(); |
| Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 2214 | else |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2215 | return {}; |
| Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2218 | const Type *CXXCtorInitializer::getBaseClass() const { |
| Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 2219 | if (isBaseInitializer()) |
| Alexis Hunt | a50dd46 | 2011-01-08 23:01:16 +0000 | [diff] [blame] | 2220 | return Initializee.get<TypeSourceInfo*>()->getType().getTypePtr(); |
| Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 2221 | else |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2222 | return nullptr; |
| Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2225 | SourceLocation CXXCtorInitializer::getSourceLocation() const { |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2226 | if (isInClassMemberInitializer()) |
| 2227 | return getAnyMember()->getLocation(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2228 | |
| David Blaikie | a81d410 | 2015-01-18 00:12:58 +0000 | [diff] [blame] | 2229 | if (isAnyMemberInitializer()) |
| 2230 | return getMemberLocation(); |
| 2231 | |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2232 | if (const auto *TSInfo = Initializee.get<TypeSourceInfo *>()) |
| Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2233 | return TSInfo->getTypeLoc().getLocalSourceRange().getBegin(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2234 | |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2235 | return {}; |
| Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
| Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 2238 | SourceRange CXXCtorInitializer::getSourceRange() const { |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2239 | if (isInClassMemberInitializer()) { |
| 2240 | FieldDecl *D = getAnyMember(); |
| 2241 | if (Expr *I = D->getInClassInitializer()) |
| 2242 | return I->getSourceRange(); |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2243 | return {}; |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
| Douglas Gregor | c8c44b5d | 2009-12-02 22:36:29 +0000 | [diff] [blame] | 2246 | return SourceRange(getSourceLocation(), getRParenLoc()); |
| Douglas Gregor | e8381c0 | 2008-11-05 04:29:56 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
| Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2249 | CXXConstructorDecl::CXXConstructorDecl( |
| 2250 | ASTContext &C, CXXRecordDecl *RD, SourceLocation StartLoc, |
| 2251 | const DeclarationNameInfo &NameInfo, QualType T, TypeSourceInfo *TInfo, |
| 2252 | bool isExplicitSpecified, bool isInline, bool isImplicitlyDeclared, |
| 2253 | bool isConstexpr, InheritedConstructor Inherited) |
| 2254 | : CXXMethodDecl(CXXConstructor, C, RD, StartLoc, NameInfo, T, TInfo, |
| 2255 | SC_None, isInline, isConstexpr, SourceLocation()) { |
| 2256 | setNumCtorInitializers(0); |
| 2257 | setInheritingConstructor(static_cast<bool>(Inherited)); |
| 2258 | setImplicit(isImplicitlyDeclared); |
| 2259 | if (Inherited) |
| 2260 | *getTrailingObjects<InheritedConstructor>() = Inherited; |
| 2261 | setExplicitSpecified(isExplicitSpecified); |
| 2262 | } |
| 2263 | |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 2264 | void CXXConstructorDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2265 | |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2266 | CXXConstructorDecl *CXXConstructorDecl::CreateDeserialized(ASTContext &C, |
| 2267 | unsigned ID, |
| 2268 | bool Inherited) { |
| 2269 | unsigned Extra = additionalSizeToAlloc<InheritedConstructor>(Inherited); |
| 2270 | auto *Result = new (C, ID, Extra) CXXConstructorDecl( |
| 2271 | C, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), nullptr, |
| 2272 | false, false, false, false, InheritedConstructor()); |
| Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2273 | Result->setInheritingConstructor(Inherited); |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2274 | return Result; |
| Chris Lattner | ca025db | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | CXXConstructorDecl * |
| Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2278 | CXXConstructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2279 | SourceLocation StartLoc, |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2280 | const DeclarationNameInfo &NameInfo, |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2281 | QualType T, TypeSourceInfo *TInfo, |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 2282 | bool isExplicit, bool isInline, |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2283 | bool isImplicitlyDeclared, bool isConstexpr, |
| 2284 | InheritedConstructor Inherited) { |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2285 | assert(NameInfo.getName().getNameKind() |
| 2286 | == DeclarationName::CXXConstructorName && |
| Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2287 | "Name must refer to a constructor"); |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2288 | unsigned Extra = |
| 2289 | additionalSizeToAlloc<InheritedConstructor>(Inherited ? 1 : 0); |
| 2290 | return new (C, RD, Extra) CXXConstructorDecl( |
| 2291 | C, RD, StartLoc, NameInfo, T, TInfo, isExplicit, isInline, |
| 2292 | isImplicitlyDeclared, isConstexpr, Inherited); |
| Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
| Richard Smith | c2bb818 | 2015-03-24 06:36:48 +0000 | [diff] [blame] | 2295 | CXXConstructorDecl::init_const_iterator CXXConstructorDecl::init_begin() const { |
| 2296 | return CtorInitializers.get(getASTContext().getExternalSource()); |
| 2297 | } |
| 2298 | |
| Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2299 | CXXConstructorDecl *CXXConstructorDecl::getTargetConstructor() const { |
| 2300 | assert(isDelegatingConstructor() && "Not a delegating constructor!"); |
| 2301 | Expr *E = (*init_begin())->getInit()->IgnoreImplicit(); |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2302 | if (const auto *Construct = dyn_cast<CXXConstructExpr>(E)) |
| Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2303 | return Construct->getConstructor(); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2304 | |
| 2305 | return nullptr; |
| Douglas Gregor | d73f3dd | 2011-11-01 01:16:03 +0000 | [diff] [blame] | 2306 | } |
| 2307 | |
| Douglas Gregor | eebb5c1 | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 2308 | bool CXXConstructorDecl::isDefaultConstructor() const { |
| 2309 | // C++ [class.ctor]p5: |
| Douglas Gregor | cfd8ddc | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 2310 | // A default constructor for a class X is a constructor of class |
| 2311 | // X that can be called without an argument. |
| Douglas Gregor | eebb5c1 | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 2312 | return (getNumParams() == 0) || |
| Anders Carlsson | 6eb5557 | 2009-08-25 05:12:04 +0000 | [diff] [blame] | 2313 | (getNumParams() > 0 && getParamDecl(0)->hasDefaultArg()); |
| Douglas Gregor | eebb5c1 | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 2314 | } |
| 2315 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2316 | bool |
| Douglas Gregor | 507eb87 | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 2317 | CXXConstructorDecl::isCopyConstructor(unsigned &TypeQuals) const { |
| Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2318 | return isCopyOrMoveConstructor(TypeQuals) && |
| 2319 | getParamDecl(0)->getType()->isLValueReferenceType(); |
| 2320 | } |
| 2321 | |
| 2322 | bool CXXConstructorDecl::isMoveConstructor(unsigned &TypeQuals) const { |
| 2323 | return isCopyOrMoveConstructor(TypeQuals) && |
| 2324 | getParamDecl(0)->getType()->isRValueReferenceType(); |
| 2325 | } |
| 2326 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2327 | /// Determine whether this is a copy or move constructor. |
| Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2328 | bool CXXConstructorDecl::isCopyOrMoveConstructor(unsigned &TypeQuals) const { |
| Douglas Gregor | eebb5c1 | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 2329 | // C++ [class.copy]p2: |
| Douglas Gregor | cfd8ddc | 2008-11-05 16:20:31 +0000 | [diff] [blame] | 2330 | // A non-template constructor for class X is a copy constructor |
| 2331 | // if its first parameter is of type X&, const X&, volatile X& or |
| 2332 | // const volatile X&, and either there are no other parameters |
| 2333 | // or else all other parameters have default arguments (8.3.6). |
| Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2334 | // C++0x [class.copy]p3: |
| 2335 | // A non-template constructor for class X is a move constructor if its |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2336 | // first parameter is of type X&&, const X&&, volatile X&&, or |
| 2337 | // const volatile X&&, and either there are no other parameters or else |
| Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2338 | // all other parameters have default arguments. |
| Douglas Gregor | eebb5c1 | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 2339 | if ((getNumParams() < 1) || |
| Douglas Gregor | a14b43b | 2009-10-13 23:45:19 +0000 | [diff] [blame] | 2340 | (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2341 | (getPrimaryTemplate() != nullptr) || |
| 2342 | (getDescribedFunctionTemplate() != nullptr)) |
| Douglas Gregor | eebb5c1 | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 2343 | return false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2344 | |
| Douglas Gregor | eebb5c1 | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 2345 | const ParmVarDecl *Param = getParamDecl(0); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2346 | |
| 2347 | // Do we have a reference type? |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2348 | const auto *ParamRefType = Param->getType()->getAs<ReferenceType>(); |
| Douglas Gregor | ff7028a | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 2349 | if (!ParamRefType) |
| 2350 | return false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2351 | |
| Douglas Gregor | ff7028a | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 2352 | // Is it a reference to our class type? |
| Douglas Gregor | 507eb87 | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 2353 | ASTContext &Context = getASTContext(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2354 | |
| Douglas Gregor | ff7028a | 2009-11-13 23:59:09 +0000 | [diff] [blame] | 2355 | CanQualType PointeeType |
| 2356 | = Context.getCanonicalType(ParamRefType->getPointeeType()); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2357 | CanQualType ClassTy |
| Douglas Gregor | f70b2b4 | 2009-09-15 20:50:23 +0000 | [diff] [blame] | 2358 | = Context.getCanonicalType(Context.getTagDeclType(getParent())); |
| Douglas Gregor | eebb5c1 | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 2359 | if (PointeeType.getUnqualifiedType() != ClassTy) |
| 2360 | return false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2361 | |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2362 | // FIXME: other qualifiers? |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2363 | |
| Douglas Gregor | f282a76 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 2364 | // We have a copy or move constructor. |
| Douglas Gregor | eebb5c1 | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 2365 | TypeQuals = PointeeType.getCVRQualifiers(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2366 | return true; |
| Douglas Gregor | eebb5c1 | 2008-10-31 20:25:05 +0000 | [diff] [blame] | 2367 | } |
| 2368 | |
| Anders Carlsson | d20e795 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 2369 | bool CXXConstructorDecl::isConvertingConstructor(bool AllowExplicit) const { |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2370 | // C++ [class.conv.ctor]p1: |
| 2371 | // A constructor declared without the function-specifier explicit |
| 2372 | // that can be called with a single parameter specifies a |
| 2373 | // conversion from the type of its first parameter to the type of |
| 2374 | // its class. Such a constructor is called a converting |
| 2375 | // constructor. |
| Anders Carlsson | d20e795 | 2009-08-28 16:57:08 +0000 | [diff] [blame] | 2376 | if (isExplicit() && !AllowExplicit) |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2377 | return false; |
| 2378 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2379 | return (getNumParams() == 0 && |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2380 | getType()->getAs<FunctionProtoType>()->isVariadic()) || |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2381 | (getNumParams() == 1) || |
| Douglas Gregor | c65e159 | 2012-06-05 23:44:51 +0000 | [diff] [blame] | 2382 | (getNumParams() > 1 && |
| 2383 | (getParamDecl(1)->hasDefaultArg() || |
| 2384 | getParamDecl(1)->isParameterPack())); |
| Douglas Gregor | 26bee0b | 2008-10-31 16:23:19 +0000 | [diff] [blame] | 2385 | } |
| Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2386 | |
| Douglas Gregor | bd6b17f | 2010-11-08 17:16:59 +0000 | [diff] [blame] | 2387 | bool CXXConstructorDecl::isSpecializationCopyingObject() const { |
| Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2388 | if ((getNumParams() < 1) || |
| 2389 | (getNumParams() > 1 && !getParamDecl(1)->hasDefaultArg()) || |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2390 | (getDescribedFunctionTemplate() != nullptr)) |
| Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2391 | return false; |
| 2392 | |
| 2393 | const ParmVarDecl *Param = getParamDecl(0); |
| 2394 | |
| 2395 | ASTContext &Context = getASTContext(); |
| 2396 | CanQualType ParamType = Context.getCanonicalType(Param->getType()); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2397 | |
| Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 2398 | // Is it the same as our class type? |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2399 | CanQualType ClassTy |
| Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2400 | = Context.getCanonicalType(Context.getTagDeclType(getParent())); |
| 2401 | if (ParamType.getUnqualifiedType() != ClassTy) |
| 2402 | return false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2403 | |
| 2404 | return true; |
| Douglas Gregor | ffe14e3 | 2009-11-14 01:20:54 +0000 | [diff] [blame] | 2405 | } |
| 2406 | |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 2407 | void CXXDestructorDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2408 | |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2409 | CXXDestructorDecl * |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2410 | CXXDestructorDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 2411 | return new (C, ID) |
| 2412 | CXXDestructorDecl(C, nullptr, SourceLocation(), DeclarationNameInfo(), |
| 2413 | QualType(), nullptr, false, false); |
| Chris Lattner | ca025db | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 2414 | } |
| 2415 | |
| 2416 | CXXDestructorDecl * |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2417 | CXXDestructorDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2418 | SourceLocation StartLoc, |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2419 | const DeclarationNameInfo &NameInfo, |
| Craig Silverstein | af8808d | 2010-10-21 00:44:50 +0000 | [diff] [blame] | 2420 | QualType T, TypeSourceInfo *TInfo, |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 2421 | bool isInline, bool isImplicitlyDeclared) { |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2422 | assert(NameInfo.getName().getNameKind() |
| 2423 | == DeclarationName::CXXDestructorName && |
| Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2424 | "Name must refer to a destructor"); |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 2425 | return new (C, RD) CXXDestructorDecl(C, RD, StartLoc, NameInfo, T, TInfo, |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 2426 | isInline, isImplicitlyDeclared); |
| Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 2427 | } |
| 2428 | |
| Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 2429 | void CXXDestructorDecl::setOperatorDelete(FunctionDecl *OD, Expr *ThisArg) { |
| Richard Smith | f813400 | 2015-03-10 01:41:22 +0000 | [diff] [blame] | 2430 | auto *First = cast<CXXDestructorDecl>(getFirstDecl()); |
| 2431 | if (OD && !First->OperatorDelete) { |
| 2432 | First->OperatorDelete = OD; |
| Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 2433 | First->OperatorDeleteThisArg = ThisArg; |
| Richard Smith | f813400 | 2015-03-10 01:41:22 +0000 | [diff] [blame] | 2434 | if (auto *L = getASTMutationListener()) |
| Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 2435 | L->ResolvedOperatorDelete(First, OD, ThisArg); |
| Richard Smith | f813400 | 2015-03-10 01:41:22 +0000 | [diff] [blame] | 2436 | } |
| 2437 | } |
| 2438 | |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 2439 | void CXXConversionDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2440 | |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2441 | CXXConversionDecl * |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2442 | CXXConversionDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 2443 | return new (C, ID) CXXConversionDecl(C, nullptr, SourceLocation(), |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 2444 | DeclarationNameInfo(), QualType(), |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2445 | nullptr, false, false, false, |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 2446 | SourceLocation()); |
| Chris Lattner | ca025db | 2010-05-07 21:43:38 +0000 | [diff] [blame] | 2447 | } |
| 2448 | |
| 2449 | CXXConversionDecl * |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2450 | CXXConversionDecl::Create(ASTContext &C, CXXRecordDecl *RD, |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2451 | SourceLocation StartLoc, |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2452 | const DeclarationNameInfo &NameInfo, |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2453 | QualType T, TypeSourceInfo *TInfo, |
| Douglas Gregor | f2f0806 | 2011-03-08 17:10:18 +0000 | [diff] [blame] | 2454 | bool isInline, bool isExplicit, |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 2455 | bool isConstexpr, SourceLocation EndLocation) { |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2456 | assert(NameInfo.getName().getNameKind() |
| 2457 | == DeclarationName::CXXConversionFunctionName && |
| Douglas Gregor | 77324f3 | 2008-11-17 14:58:09 +0000 | [diff] [blame] | 2458 | "Name must refer to a conversion function"); |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 2459 | return new (C, RD) CXXConversionDecl(C, RD, StartLoc, NameInfo, T, TInfo, |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 2460 | isInline, isExplicit, isConstexpr, |
| 2461 | EndLocation); |
| Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
| Douglas Gregor | d3b672c | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 2464 | bool CXXConversionDecl::isLambdaToBlockPointerConversion() const { |
| 2465 | return isImplicit() && getParent()->isLambda() && |
| 2466 | getConversionType()->isBlockPointerType(); |
| 2467 | } |
| 2468 | |
| Erich Keane | c9d2990 | 2018-08-01 21:16:54 +0000 | [diff] [blame] | 2469 | LinkageSpecDecl::LinkageSpecDecl(DeclContext *DC, SourceLocation ExternLoc, |
| 2470 | SourceLocation LangLoc, LanguageIDs lang, |
| 2471 | bool HasBraces) |
| 2472 | : Decl(LinkageSpec, DC, LangLoc), DeclContext(LinkageSpec), |
| 2473 | ExternLoc(ExternLoc), RBraceLoc(SourceLocation()) { |
| 2474 | setLanguage(lang); |
| 2475 | LinkageSpecDeclBits.HasBraces = HasBraces; |
| 2476 | } |
| 2477 | |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 2478 | void LinkageSpecDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2479 | |
| Chris Lattner | b8c18fa | 2008-11-04 16:51:42 +0000 | [diff] [blame] | 2480 | LinkageSpecDecl *LinkageSpecDecl::Create(ASTContext &C, |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2481 | DeclContext *DC, |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 2482 | SourceLocation ExternLoc, |
| 2483 | SourceLocation LangLoc, |
| Abramo Bagnara | 4a8cda8 | 2011-03-03 14:52:38 +0000 | [diff] [blame] | 2484 | LanguageIDs Lang, |
| Rafael Espindola | 327be3c | 2013-04-26 01:30:23 +0000 | [diff] [blame] | 2485 | bool HasBraces) { |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 2486 | return new (C, DC) LinkageSpecDecl(DC, ExternLoc, LangLoc, Lang, HasBraces); |
| Douglas Gregor | 29ff7d0 | 2008-12-16 22:23:02 +0000 | [diff] [blame] | 2487 | } |
| Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2488 | |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 2489 | LinkageSpecDecl *LinkageSpecDecl::CreateDeserialized(ASTContext &C, |
| 2490 | unsigned ID) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2491 | return new (C, ID) LinkageSpecDecl(nullptr, SourceLocation(), |
| 2492 | SourceLocation(), lang_c, false); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2493 | } |
| 2494 | |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 2495 | void UsingDirectiveDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2496 | |
| Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2497 | UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC, |
| 2498 | SourceLocation L, |
| 2499 | SourceLocation NamespaceLoc, |
| Douglas Gregor | 12441b3 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 2500 | NestedNameSpecifierLoc QualifierLoc, |
| Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2501 | SourceLocation IdentLoc, |
| Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 2502 | NamedDecl *Used, |
| Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2503 | DeclContext *CommonAncestor) { |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2504 | if (auto *NS = dyn_cast_or_null<NamespaceDecl>(Used)) |
| Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 2505 | Used = NS->getOriginalNamespace(); |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 2506 | return new (C, DC) UsingDirectiveDecl(DC, L, NamespaceLoc, QualifierLoc, |
| 2507 | IdentLoc, Used, CommonAncestor); |
| Douglas Gregor | 889ceb7 | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 2508 | } |
| 2509 | |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 2510 | UsingDirectiveDecl *UsingDirectiveDecl::CreateDeserialized(ASTContext &C, |
| 2511 | unsigned ID) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2512 | return new (C, ID) UsingDirectiveDecl(nullptr, SourceLocation(), |
| 2513 | SourceLocation(), |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 2514 | NestedNameSpecifierLoc(), |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2515 | SourceLocation(), nullptr, nullptr); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
| Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 2518 | NamespaceDecl *UsingDirectiveDecl::getNominatedNamespace() { |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2519 | if (auto *NA = dyn_cast_or_null<NamespaceAliasDecl>(NominatedNamespace)) |
| Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 2520 | return NA->getNamespace(); |
| 2521 | return cast_or_null<NamespaceDecl>(NominatedNamespace); |
| 2522 | } |
| 2523 | |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 2524 | NamespaceDecl::NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline, |
| 2525 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 2526 | IdentifierInfo *Id, NamespaceDecl *PrevDecl) |
| 2527 | : NamedDecl(Namespace, DC, IdLoc, Id), DeclContext(Namespace), |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 2528 | redeclarable_base(C), LocStart(StartLoc), |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 2529 | AnonOrFirstNamespaceAndInline(nullptr, Inline) { |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2530 | setPreviousDecl(PrevDecl); |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 2531 | |
| Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 2532 | if (PrevDecl) |
| 2533 | AnonOrFirstNamespaceAndInline.setPointer(PrevDecl->getOriginalNamespace()); |
| 2534 | } |
| 2535 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2536 | NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, |
| Douglas Gregor | e57e752 | 2012-01-07 09:11:48 +0000 | [diff] [blame] | 2537 | bool Inline, SourceLocation StartLoc, |
| 2538 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 2539 | NamespaceDecl *PrevDecl) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 2540 | return new (C, DC) NamespaceDecl(C, DC, Inline, StartLoc, IdLoc, Id, |
| 2541 | PrevDecl); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2542 | } |
| 2543 | |
| 2544 | NamespaceDecl *NamespaceDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 2545 | return new (C, ID) NamespaceDecl(C, nullptr, false, SourceLocation(), |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2546 | SourceLocation(), nullptr, nullptr); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2547 | } |
| 2548 | |
| Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 2549 | NamespaceDecl *NamespaceDecl::getOriginalNamespace() { |
| 2550 | if (isFirstDecl()) |
| 2551 | return this; |
| 2552 | |
| 2553 | return AnonOrFirstNamespaceAndInline.getPointer(); |
| 2554 | } |
| 2555 | |
| 2556 | const NamespaceDecl *NamespaceDecl::getOriginalNamespace() const { |
| 2557 | if (isFirstDecl()) |
| 2558 | return this; |
| 2559 | |
| 2560 | return AnonOrFirstNamespaceAndInline.getPointer(); |
| 2561 | } |
| 2562 | |
| 2563 | bool NamespaceDecl::isOriginalNamespace() const { return isFirstDecl(); } |
| 2564 | |
| Richard Smith | d7af8a3 | 2014-05-10 01:17:36 +0000 | [diff] [blame] | 2565 | NamespaceDecl *NamespaceDecl::getNextRedeclarationImpl() { |
| 2566 | return getNextRedeclaration(); |
| Rafael Espindola | 8ef7f68 | 2013-11-26 15:12:20 +0000 | [diff] [blame] | 2567 | } |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 2568 | |
| Rafael Espindola | 8ef7f68 | 2013-11-26 15:12:20 +0000 | [diff] [blame] | 2569 | NamespaceDecl *NamespaceDecl::getPreviousDeclImpl() { |
| 2570 | return getPreviousDecl(); |
| 2571 | } |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 2572 | |
| Rafael Espindola | 8ef7f68 | 2013-11-26 15:12:20 +0000 | [diff] [blame] | 2573 | NamespaceDecl *NamespaceDecl::getMostRecentDeclImpl() { |
| 2574 | return getMostRecentDecl(); |
| 2575 | } |
| 2576 | |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 2577 | void NamespaceAliasDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2578 | |
| Richard Smith | f463436 | 2014-09-03 23:11:22 +0000 | [diff] [blame] | 2579 | NamespaceAliasDecl *NamespaceAliasDecl::getNextRedeclarationImpl() { |
| 2580 | return getNextRedeclaration(); |
| 2581 | } |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 2582 | |
| Richard Smith | f463436 | 2014-09-03 23:11:22 +0000 | [diff] [blame] | 2583 | NamespaceAliasDecl *NamespaceAliasDecl::getPreviousDeclImpl() { |
| 2584 | return getPreviousDecl(); |
| 2585 | } |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 2586 | |
| Richard Smith | f463436 | 2014-09-03 23:11:22 +0000 | [diff] [blame] | 2587 | NamespaceAliasDecl *NamespaceAliasDecl::getMostRecentDeclImpl() { |
| 2588 | return getMostRecentDecl(); |
| 2589 | } |
| 2590 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2591 | NamespaceAliasDecl *NamespaceAliasDecl::Create(ASTContext &C, DeclContext *DC, |
| Douglas Gregor | 01a43013 | 2010-09-01 03:07:18 +0000 | [diff] [blame] | 2592 | SourceLocation UsingLoc, |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2593 | SourceLocation AliasLoc, |
| 2594 | IdentifierInfo *Alias, |
| Douglas Gregor | c05ba2e | 2011-02-25 17:08:07 +0000 | [diff] [blame] | 2595 | NestedNameSpecifierLoc QualifierLoc, |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2596 | SourceLocation IdentLoc, |
| Anders Carlsson | ff25fdf | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 2597 | NamedDecl *Namespace) { |
| Richard Smith | f463436 | 2014-09-03 23:11:22 +0000 | [diff] [blame] | 2598 | // FIXME: Preserve the aliased namespace as written. |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2599 | if (auto *NS = dyn_cast_or_null<NamespaceDecl>(Namespace)) |
| Sebastian Redl | a6602e9 | 2009-11-23 15:34:23 +0000 | [diff] [blame] | 2600 | Namespace = NS->getOriginalNamespace(); |
| Richard Smith | f463436 | 2014-09-03 23:11:22 +0000 | [diff] [blame] | 2601 | return new (C, DC) NamespaceAliasDecl(C, DC, UsingLoc, AliasLoc, Alias, |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 2602 | QualifierLoc, IdentLoc, Namespace); |
| Anders Carlsson | ff25fdf | 2009-03-28 22:58:02 +0000 | [diff] [blame] | 2603 | } |
| 2604 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2605 | NamespaceAliasDecl * |
| 2606 | NamespaceAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Richard Smith | f463436 | 2014-09-03 23:11:22 +0000 | [diff] [blame] | 2607 | return new (C, ID) NamespaceAliasDecl(C, nullptr, SourceLocation(), |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2608 | SourceLocation(), nullptr, |
| 2609 | NestedNameSpecifierLoc(), |
| 2610 | SourceLocation(), nullptr); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 2613 | void UsingShadowDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2614 | |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2615 | UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, DeclContext *DC, |
| 2616 | SourceLocation Loc, UsingDecl *Using, |
| 2617 | NamedDecl *Target) |
| 2618 | : NamedDecl(K, DC, Loc, Using ? Using->getDeclName() : DeclarationName()), |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2619 | redeclarable_base(C), UsingOrNextShadow(cast<NamedDecl>(Using)) { |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2620 | if (Target) |
| Richard Smith | a263c34 | 2018-01-06 01:07:05 +0000 | [diff] [blame] | 2621 | setTargetDecl(Target); |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2622 | setImplicit(); |
| 2623 | } |
| 2624 | |
| 2625 | UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, EmptyShell Empty) |
| 2626 | : NamedDecl(K, nullptr, SourceLocation(), DeclarationName()), |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 2627 | redeclarable_base(C) {} |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2628 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2629 | UsingShadowDecl * |
| 2630 | UsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2631 | return new (C, ID) UsingShadowDecl(UsingShadow, C, EmptyShell()); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2632 | } |
| 2633 | |
| Argyrios Kyrtzidis | 2703beb | 2010-11-10 05:40:41 +0000 | [diff] [blame] | 2634 | UsingDecl *UsingShadowDecl::getUsingDecl() const { |
| 2635 | const UsingShadowDecl *Shadow = this; |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2636 | while (const auto *NextShadow = |
| 2637 | dyn_cast<UsingShadowDecl>(Shadow->UsingOrNextShadow)) |
| Argyrios Kyrtzidis | 2703beb | 2010-11-10 05:40:41 +0000 | [diff] [blame] | 2638 | Shadow = NextShadow; |
| 2639 | return cast<UsingDecl>(Shadow->UsingOrNextShadow); |
| 2640 | } |
| 2641 | |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 2642 | void ConstructorUsingShadowDecl::anchor() {} |
| Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2643 | |
| 2644 | ConstructorUsingShadowDecl * |
| 2645 | ConstructorUsingShadowDecl::Create(ASTContext &C, DeclContext *DC, |
| 2646 | SourceLocation Loc, UsingDecl *Using, |
| 2647 | NamedDecl *Target, bool IsVirtual) { |
| 2648 | return new (C, DC) ConstructorUsingShadowDecl(C, DC, Loc, Using, Target, |
| 2649 | IsVirtual); |
| 2650 | } |
| 2651 | |
| 2652 | ConstructorUsingShadowDecl * |
| 2653 | ConstructorUsingShadowDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2654 | return new (C, ID) ConstructorUsingShadowDecl(C, EmptyShell()); |
| 2655 | } |
| 2656 | |
| 2657 | CXXRecordDecl *ConstructorUsingShadowDecl::getNominatedBaseClass() const { |
| 2658 | return getUsingDecl()->getQualifier()->getAsRecordDecl(); |
| 2659 | } |
| 2660 | |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 2661 | void UsingDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2662 | |
| Argyrios Kyrtzidis | 2703beb | 2010-11-10 05:40:41 +0000 | [diff] [blame] | 2663 | void UsingDecl::addShadowDecl(UsingShadowDecl *S) { |
| 2664 | assert(std::find(shadow_begin(), shadow_end(), S) == shadow_end() && |
| 2665 | "declaration already in set"); |
| 2666 | assert(S->getUsingDecl() == this); |
| 2667 | |
| Benjamin Kramer | e78f8ee | 2012-01-07 19:09:05 +0000 | [diff] [blame] | 2668 | if (FirstUsingShadow.getPointer()) |
| 2669 | S->UsingOrNextShadow = FirstUsingShadow.getPointer(); |
| 2670 | FirstUsingShadow.setPointer(S); |
| Argyrios Kyrtzidis | 2703beb | 2010-11-10 05:40:41 +0000 | [diff] [blame] | 2671 | } |
| 2672 | |
| 2673 | void UsingDecl::removeShadowDecl(UsingShadowDecl *S) { |
| 2674 | assert(std::find(shadow_begin(), shadow_end(), S) != shadow_end() && |
| 2675 | "declaration not in set"); |
| 2676 | assert(S->getUsingDecl() == this); |
| 2677 | |
| 2678 | // Remove S from the shadow decl chain. This is O(n) but hopefully rare. |
| 2679 | |
| Benjamin Kramer | e78f8ee | 2012-01-07 19:09:05 +0000 | [diff] [blame] | 2680 | if (FirstUsingShadow.getPointer() == S) { |
| 2681 | FirstUsingShadow.setPointer( |
| 2682 | dyn_cast<UsingShadowDecl>(S->UsingOrNextShadow)); |
| Argyrios Kyrtzidis | 2703beb | 2010-11-10 05:40:41 +0000 | [diff] [blame] | 2683 | S->UsingOrNextShadow = this; |
| 2684 | return; |
| 2685 | } |
| 2686 | |
| Benjamin Kramer | e78f8ee | 2012-01-07 19:09:05 +0000 | [diff] [blame] | 2687 | UsingShadowDecl *Prev = FirstUsingShadow.getPointer(); |
| Argyrios Kyrtzidis | 2703beb | 2010-11-10 05:40:41 +0000 | [diff] [blame] | 2688 | while (Prev->UsingOrNextShadow != S) |
| 2689 | Prev = cast<UsingShadowDecl>(Prev->UsingOrNextShadow); |
| 2690 | Prev->UsingOrNextShadow = S->UsingOrNextShadow; |
| 2691 | S->UsingOrNextShadow = this; |
| 2692 | } |
| 2693 | |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 2694 | UsingDecl *UsingDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation UL, |
| 2695 | NestedNameSpecifierLoc QualifierLoc, |
| Abramo Bagnara | 8de74e9 | 2010-08-12 11:46:03 +0000 | [diff] [blame] | 2696 | const DeclarationNameInfo &NameInfo, |
| Enea Zaffanella | e05a3cf | 2013-07-22 10:54:09 +0000 | [diff] [blame] | 2697 | bool HasTypename) { |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 2698 | return new (C, DC) UsingDecl(DC, UL, QualifierLoc, NameInfo, HasTypename); |
| Douglas Gregor | fec5263 | 2009-06-20 00:51:54 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2701 | UsingDecl *UsingDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2702 | return new (C, ID) UsingDecl(nullptr, SourceLocation(), |
| 2703 | NestedNameSpecifierLoc(), DeclarationNameInfo(), |
| 2704 | false); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2705 | } |
| 2706 | |
| Enea Zaffanella | c70b251 | 2013-07-17 17:28:56 +0000 | [diff] [blame] | 2707 | SourceRange UsingDecl::getSourceRange() const { |
| 2708 | SourceLocation Begin = isAccessDeclaration() |
| 2709 | ? getQualifierLoc().getBeginLoc() : UsingLocation; |
| 2710 | return SourceRange(Begin, getNameInfo().getEndLoc()); |
| 2711 | } |
| 2712 | |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 2713 | void UsingPackDecl::anchor() {} |
| Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2714 | |
| 2715 | UsingPackDecl *UsingPackDecl::Create(ASTContext &C, DeclContext *DC, |
| 2716 | NamedDecl *InstantiatedFrom, |
| 2717 | ArrayRef<NamedDecl *> UsingDecls) { |
| 2718 | size_t Extra = additionalSizeToAlloc<NamedDecl *>(UsingDecls.size()); |
| 2719 | return new (C, DC, Extra) UsingPackDecl(DC, InstantiatedFrom, UsingDecls); |
| 2720 | } |
| 2721 | |
| 2722 | UsingPackDecl *UsingPackDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
| 2723 | unsigned NumExpansions) { |
| 2724 | size_t Extra = additionalSizeToAlloc<NamedDecl *>(NumExpansions); |
| 2725 | auto *Result = new (C, ID, Extra) UsingPackDecl(nullptr, nullptr, None); |
| 2726 | Result->NumExpansions = NumExpansions; |
| 2727 | auto *Trail = Result->getTrailingObjects<NamedDecl *>(); |
| 2728 | for (unsigned I = 0; I != NumExpansions; ++I) |
| 2729 | new (Trail + I) NamedDecl*(nullptr); |
| 2730 | return Result; |
| 2731 | } |
| 2732 | |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 2733 | void UnresolvedUsingValueDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2734 | |
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2735 | UnresolvedUsingValueDecl * |
| 2736 | UnresolvedUsingValueDecl::Create(ASTContext &C, DeclContext *DC, |
| 2737 | SourceLocation UsingLoc, |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 2738 | NestedNameSpecifierLoc QualifierLoc, |
| Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2739 | const DeclarationNameInfo &NameInfo, |
| 2740 | SourceLocation EllipsisLoc) { |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 2741 | return new (C, DC) UnresolvedUsingValueDecl(DC, C.DependentTy, UsingLoc, |
| Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2742 | QualifierLoc, NameInfo, |
| 2743 | EllipsisLoc); |
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2744 | } |
| 2745 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2746 | UnresolvedUsingValueDecl * |
| 2747 | UnresolvedUsingValueDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2748 | return new (C, ID) UnresolvedUsingValueDecl(nullptr, QualType(), |
| 2749 | SourceLocation(), |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 2750 | NestedNameSpecifierLoc(), |
| Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2751 | DeclarationNameInfo(), |
| 2752 | SourceLocation()); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
| Enea Zaffanella | c70b251 | 2013-07-17 17:28:56 +0000 | [diff] [blame] | 2755 | SourceRange UnresolvedUsingValueDecl::getSourceRange() const { |
| 2756 | SourceLocation Begin = isAccessDeclaration() |
| 2757 | ? getQualifierLoc().getBeginLoc() : UsingLocation; |
| 2758 | return SourceRange(Begin, getNameInfo().getEndLoc()); |
| 2759 | } |
| 2760 | |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 2761 | void UnresolvedUsingTypenameDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2762 | |
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2763 | UnresolvedUsingTypenameDecl * |
| 2764 | UnresolvedUsingTypenameDecl::Create(ASTContext &C, DeclContext *DC, |
| 2765 | SourceLocation UsingLoc, |
| 2766 | SourceLocation TypenameLoc, |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 2767 | NestedNameSpecifierLoc QualifierLoc, |
| John McCall | e61f2ba | 2009-11-18 02:36:19 +0000 | [diff] [blame] | 2768 | SourceLocation TargetNameLoc, |
| Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2769 | DeclarationName TargetName, |
| 2770 | SourceLocation EllipsisLoc) { |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 2771 | return new (C, DC) UnresolvedUsingTypenameDecl( |
| 2772 | DC, UsingLoc, TypenameLoc, QualifierLoc, TargetNameLoc, |
| Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2773 | TargetName.getAsIdentifierInfo(), EllipsisLoc); |
| Anders Carlsson | 8305c1f | 2009-08-28 05:30:28 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2776 | UnresolvedUsingTypenameDecl * |
| 2777 | UnresolvedUsingTypenameDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 2778 | return new (C, ID) UnresolvedUsingTypenameDecl( |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2779 | nullptr, SourceLocation(), SourceLocation(), NestedNameSpecifierLoc(), |
| Richard Smith | 151c456 | 2016-12-20 21:35:28 +0000 | [diff] [blame] | 2780 | SourceLocation(), nullptr, SourceLocation()); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2781 | } |
| 2782 | |
| Eugene Zelenko | 4a5354f | 2017-11-14 23:13:32 +0000 | [diff] [blame] | 2783 | void StaticAssertDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2784 | |
| Anders Carlsson | 5bbe1d7 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 2785 | StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC, |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 2786 | SourceLocation StaticAssertLoc, |
| 2787 | Expr *AssertExpr, |
| 2788 | StringLiteral *Message, |
| Richard Smith | ded9c2e | 2012-07-11 22:37:56 +0000 | [diff] [blame] | 2789 | SourceLocation RParenLoc, |
| 2790 | bool Failed) { |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 2791 | return new (C, DC) StaticAssertDecl(DC, StaticAssertLoc, AssertExpr, Message, |
| 2792 | RParenLoc, Failed); |
| Anders Carlsson | 5bbe1d7 | 2009-03-14 00:25:26 +0000 | [diff] [blame] | 2793 | } |
| 2794 | |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 2795 | StaticAssertDecl *StaticAssertDecl::CreateDeserialized(ASTContext &C, |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2796 | unsigned ID) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2797 | return new (C, ID) StaticAssertDecl(nullptr, SourceLocation(), nullptr, |
| 2798 | nullptr, SourceLocation(), false); |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
| Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 2801 | void BindingDecl::anchor() {} |
| 2802 | |
| 2803 | BindingDecl *BindingDecl::Create(ASTContext &C, DeclContext *DC, |
| 2804 | SourceLocation IdLoc, IdentifierInfo *Id) { |
| Richard Smith | 32cb8c9 | 2016-08-12 00:53:41 +0000 | [diff] [blame] | 2805 | return new (C, DC) BindingDecl(DC, IdLoc, Id); |
| Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
| 2808 | BindingDecl *BindingDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Richard Smith | 32cb8c9 | 2016-08-12 00:53:41 +0000 | [diff] [blame] | 2809 | return new (C, ID) BindingDecl(nullptr, SourceLocation(), nullptr); |
| Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
| Richard Smith | 97fcf4b | 2016-08-14 23:15:52 +0000 | [diff] [blame] | 2812 | VarDecl *BindingDecl::getHoldingVar() const { |
| 2813 | Expr *B = getBinding(); |
| 2814 | if (!B) |
| 2815 | return nullptr; |
| 2816 | auto *DRE = dyn_cast<DeclRefExpr>(B->IgnoreImplicit()); |
| 2817 | if (!DRE) |
| 2818 | return nullptr; |
| 2819 | |
| 2820 | auto *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 2821 | assert(VD->isImplicit() && "holding var for binding decl not implicit"); |
| 2822 | return VD; |
| 2823 | } |
| 2824 | |
| Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 2825 | void DecompositionDecl::anchor() {} |
| 2826 | |
| 2827 | DecompositionDecl *DecompositionDecl::Create(ASTContext &C, DeclContext *DC, |
| 2828 | SourceLocation StartLoc, |
| 2829 | SourceLocation LSquareLoc, |
| 2830 | QualType T, TypeSourceInfo *TInfo, |
| 2831 | StorageClass SC, |
| 2832 | ArrayRef<BindingDecl *> Bindings) { |
| 2833 | size_t Extra = additionalSizeToAlloc<BindingDecl *>(Bindings.size()); |
| 2834 | return new (C, DC, Extra) |
| 2835 | DecompositionDecl(C, DC, StartLoc, LSquareLoc, T, TInfo, SC, Bindings); |
| 2836 | } |
| 2837 | |
| 2838 | DecompositionDecl *DecompositionDecl::CreateDeserialized(ASTContext &C, |
| 2839 | unsigned ID, |
| 2840 | unsigned NumBindings) { |
| 2841 | size_t Extra = additionalSizeToAlloc<BindingDecl *>(NumBindings); |
| Richard Smith | 7b76d81 | 2016-08-12 02:21:25 +0000 | [diff] [blame] | 2842 | auto *Result = new (C, ID, Extra) |
| 2843 | DecompositionDecl(C, nullptr, SourceLocation(), SourceLocation(), |
| 2844 | QualType(), nullptr, StorageClass(), None); |
| Richard Smith | bdb84f3 | 2016-07-22 23:36:59 +0000 | [diff] [blame] | 2845 | // Set up and clean out the bindings array. |
| 2846 | Result->NumBindings = NumBindings; |
| 2847 | auto *Trail = Result->getTrailingObjects<BindingDecl *>(); |
| 2848 | for (unsigned I = 0; I != NumBindings; ++I) |
| 2849 | new (Trail + I) BindingDecl*(nullptr); |
| 2850 | return Result; |
| 2851 | } |
| 2852 | |
| Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2853 | void DecompositionDecl::printName(llvm::raw_ostream &os) const { |
| 2854 | os << '['; |
| 2855 | bool Comma = false; |
| Eugene Zelenko | 7855e77 | 2018-04-03 00:11:50 +0000 | [diff] [blame] | 2856 | for (const auto *B : bindings()) { |
| Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2857 | if (Comma) |
| 2858 | os << ", "; |
| 2859 | B->printName(os); |
| 2860 | Comma = true; |
| 2861 | } |
| 2862 | os << ']'; |
| 2863 | } |
| 2864 | |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 2865 | MSPropertyDecl *MSPropertyDecl::Create(ASTContext &C, DeclContext *DC, |
| 2866 | SourceLocation L, DeclarationName N, |
| 2867 | QualType T, TypeSourceInfo *TInfo, |
| 2868 | SourceLocation StartL, |
| 2869 | IdentifierInfo *Getter, |
| 2870 | IdentifierInfo *Setter) { |
| 2871 | return new (C, DC) MSPropertyDecl(DC, L, N, T, TInfo, StartL, Getter, Setter); |
| 2872 | } |
| 2873 | |
| 2874 | MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C, |
| 2875 | unsigned ID) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2876 | return new (C, ID) MSPropertyDecl(nullptr, SourceLocation(), |
| 2877 | DeclarationName(), QualType(), nullptr, |
| 2878 | SourceLocation(), nullptr, nullptr); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2879 | } |
| 2880 | |
| Anders Carlsson | 6750d16 | 2009-03-26 23:46:50 +0000 | [diff] [blame] | 2881 | static const char *getAccessName(AccessSpecifier AS) { |
| 2882 | switch (AS) { |
| Anders Carlsson | 6750d16 | 2009-03-26 23:46:50 +0000 | [diff] [blame] | 2883 | case AS_none: |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2884 | llvm_unreachable("Invalid access specifier!"); |
| Anders Carlsson | 6750d16 | 2009-03-26 23:46:50 +0000 | [diff] [blame] | 2885 | case AS_public: |
| 2886 | return "public"; |
| 2887 | case AS_private: |
| 2888 | return "private"; |
| 2889 | case AS_protected: |
| 2890 | return "protected"; |
| 2891 | } |
| David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 2892 | llvm_unreachable("Invalid access specifier!"); |
| Anders Carlsson | 6750d16 | 2009-03-26 23:46:50 +0000 | [diff] [blame] | 2893 | } |
| 2894 | |
| 2895 | const DiagnosticBuilder &clang::operator<<(const DiagnosticBuilder &DB, |
| 2896 | AccessSpecifier AS) { |
| 2897 | return DB << getAccessName(AS); |
| 2898 | } |
| Richard Smith | 84f6dcf | 2012-02-02 01:16:57 +0000 | [diff] [blame] | 2899 | |
| 2900 | const PartialDiagnostic &clang::operator<<(const PartialDiagnostic &DB, |
| 2901 | AccessSpecifier AS) { |
| 2902 | return DB << getAccessName(AS); |
| 2903 | } |