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