Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1 | //===--- CGClass.cpp - Emit LLVM Code for C++ classes -----------*- C++ -*-===// |
Anders Carlsson | 9a57c5a | 2009-09-12 04:27:24 +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 contains code dealing with C++ code generation of classes |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Eli Friedman | 2495ab0 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 14 | #include "CGBlocks.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 15 | #include "CGCXXABI.h" |
Devang Patel | d76c1db | 2010-08-11 21:04:37 +0000 | [diff] [blame] | 16 | #include "CGDebugInfo.h" |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 17 | #include "CGRecordLayout.h" |
Anders Carlsson | 9a57c5a | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 18 | #include "CodeGenFunction.h" |
Anders Carlsson | c6d171e | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 19 | #include "clang/AST/CXXInheritance.h" |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclTemplate.h" |
John McCall | 769250e | 2010-09-17 02:31:44 +0000 | [diff] [blame] | 21 | #include "clang/AST/EvaluatedExprVisitor.h" |
Anders Carlsson | 9a57c5a | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 22 | #include "clang/AST/RecordLayout.h" |
John McCall | b81884d | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 23 | #include "clang/AST/StmtCXX.h" |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetBuiltins.h" |
Mark Lacey | a8e7df3 | 2013-10-30 21:53:58 +0000 | [diff] [blame] | 25 | #include "clang/CodeGen/CGFunctionInfo.h" |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/CodeGenOptions.h" |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Intrinsics.h" |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Metadata.h" |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Utils/SanitizerStats.h" |
Anders Carlsson | c6d171e | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 30 | |
Anders Carlsson | 9a57c5a | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | using namespace CodeGen; |
| 33 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 34 | /// Return the best known alignment for an unknown pointer to a |
| 35 | /// particular class. |
| 36 | CharUnits CodeGenModule::getClassPointerAlignment(const CXXRecordDecl *RD) { |
| 37 | if (!RD->isCompleteDefinition()) |
| 38 | return CharUnits::One(); // Hopefully won't be used anywhere. |
| 39 | |
| 40 | auto &layout = getContext().getASTRecordLayout(RD); |
| 41 | |
| 42 | // If the class is final, then we know that the pointer points to an |
| 43 | // object of that type and can use the full alignment. |
| 44 | if (RD->hasAttr<FinalAttr>()) { |
| 45 | return layout.getAlignment(); |
| 46 | |
| 47 | // Otherwise, we have to assume it could be a subclass. |
| 48 | } else { |
| 49 | return layout.getNonVirtualAlignment(); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /// Return the best known alignment for a pointer to a virtual base, |
| 54 | /// given the alignment of a pointer to the derived class. |
| 55 | CharUnits CodeGenModule::getVBaseAlignment(CharUnits actualDerivedAlign, |
| 56 | const CXXRecordDecl *derivedClass, |
| 57 | const CXXRecordDecl *vbaseClass) { |
| 58 | // The basic idea here is that an underaligned derived pointer might |
| 59 | // indicate an underaligned base pointer. |
| 60 | |
| 61 | assert(vbaseClass->isCompleteDefinition()); |
| 62 | auto &baseLayout = getContext().getASTRecordLayout(vbaseClass); |
| 63 | CharUnits expectedVBaseAlign = baseLayout.getNonVirtualAlignment(); |
| 64 | |
| 65 | return getDynamicOffsetAlignment(actualDerivedAlign, derivedClass, |
| 66 | expectedVBaseAlign); |
| 67 | } |
| 68 | |
| 69 | CharUnits |
| 70 | CodeGenModule::getDynamicOffsetAlignment(CharUnits actualBaseAlign, |
| 71 | const CXXRecordDecl *baseDecl, |
| 72 | CharUnits expectedTargetAlign) { |
| 73 | // If the base is an incomplete type (which is, alas, possible with |
| 74 | // member pointers), be pessimistic. |
| 75 | if (!baseDecl->isCompleteDefinition()) |
| 76 | return std::min(actualBaseAlign, expectedTargetAlign); |
| 77 | |
| 78 | auto &baseLayout = getContext().getASTRecordLayout(baseDecl); |
| 79 | CharUnits expectedBaseAlign = baseLayout.getNonVirtualAlignment(); |
| 80 | |
| 81 | // If the class is properly aligned, assume the target offset is, too. |
| 82 | // |
| 83 | // This actually isn't necessarily the right thing to do --- if the |
| 84 | // class is a complete object, but it's only properly aligned for a |
| 85 | // base subobject, then the alignments of things relative to it are |
| 86 | // probably off as well. (Note that this requires the alignment of |
| 87 | // the target to be greater than the NV alignment of the derived |
| 88 | // class.) |
| 89 | // |
| 90 | // However, our approach to this kind of under-alignment can only |
| 91 | // ever be best effort; after all, we're never going to propagate |
| 92 | // alignments through variables or parameters. Note, in particular, |
| 93 | // that constructing a polymorphic type in an address that's less |
| 94 | // than pointer-aligned will generally trap in the constructor, |
| 95 | // unless we someday add some sort of attribute to change the |
| 96 | // assumed alignment of 'this'. So our goal here is pretty much |
| 97 | // just to allow the user to explicitly say that a pointer is |
Eric Christopher | d160c50 | 2016-01-29 01:35:53 +0000 | [diff] [blame] | 98 | // under-aligned and then safely access its fields and vtables. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 99 | if (actualBaseAlign >= expectedBaseAlign) { |
| 100 | return expectedTargetAlign; |
| 101 | } |
| 102 | |
| 103 | // Otherwise, we might be offset by an arbitrary multiple of the |
| 104 | // actual alignment. The correct adjustment is to take the min of |
| 105 | // the two alignments. |
| 106 | return std::min(actualBaseAlign, expectedTargetAlign); |
| 107 | } |
| 108 | |
| 109 | Address CodeGenFunction::LoadCXXThisAddress() { |
| 110 | assert(CurFuncDecl && "loading 'this' without a func declaration?"); |
| 111 | assert(isa<CXXMethodDecl>(CurFuncDecl)); |
| 112 | |
| 113 | // Lazily compute CXXThisAlignment. |
| 114 | if (CXXThisAlignment.isZero()) { |
| 115 | // Just use the best known alignment for the parent. |
| 116 | // TODO: if we're currently emitting a complete-object ctor/dtor, |
| 117 | // we can always use the complete-object alignment. |
| 118 | auto RD = cast<CXXMethodDecl>(CurFuncDecl)->getParent(); |
| 119 | CXXThisAlignment = CGM.getClassPointerAlignment(RD); |
| 120 | } |
| 121 | |
| 122 | return Address(LoadCXXThis(), CXXThisAlignment); |
| 123 | } |
| 124 | |
| 125 | /// Emit the address of a field using a member data pointer. |
| 126 | /// |
| 127 | /// \param E Only used for emergency diagnostics |
| 128 | Address |
| 129 | CodeGenFunction::EmitCXXMemberDataPointerAddress(const Expr *E, Address base, |
| 130 | llvm::Value *memberPtr, |
| 131 | const MemberPointerType *memberPtrType, |
Ivan A. Kosarev | 229a6d8 | 2017-10-13 16:38:32 +0000 | [diff] [blame] | 132 | LValueBaseInfo *BaseInfo, |
| 133 | TBAAAccessInfo *TBAAInfo) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 134 | // Ask the ABI to compute the actual address. |
| 135 | llvm::Value *ptr = |
| 136 | CGM.getCXXABI().EmitMemberDataPointerAddress(*this, E, base, |
| 137 | memberPtr, memberPtrType); |
| 138 | |
| 139 | QualType memberType = memberPtrType->getPointeeType(); |
Ivan A. Kosarev | 78f486d | 2017-10-13 16:58:30 +0000 | [diff] [blame] | 140 | CharUnits memberAlign = getNaturalTypeAlignment(memberType, BaseInfo, |
| 141 | TBAAInfo); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 142 | memberAlign = |
| 143 | CGM.getDynamicOffsetAlignment(base.getAlignment(), |
| 144 | memberPtrType->getClass()->getAsCXXRecordDecl(), |
| 145 | memberAlign); |
| 146 | return Address(ptr, memberAlign); |
| 147 | } |
| 148 | |
David Majnemer | c1709d3 | 2015-06-23 07:31:11 +0000 | [diff] [blame] | 149 | CharUnits CodeGenModule::computeNonVirtualBaseClassOffset( |
| 150 | const CXXRecordDecl *DerivedClass, CastExpr::path_const_iterator Start, |
| 151 | CastExpr::path_const_iterator End) { |
Ken Dyck | a1a4ae3 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 152 | CharUnits Offset = CharUnits::Zero(); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 153 | |
David Majnemer | c1709d3 | 2015-06-23 07:31:11 +0000 | [diff] [blame] | 154 | const ASTContext &Context = getContext(); |
Anders Carlsson | d829a02 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 155 | const CXXRecordDecl *RD = DerivedClass; |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 156 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 157 | for (CastExpr::path_const_iterator I = Start; I != End; ++I) { |
Anders Carlsson | d829a02 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 158 | const CXXBaseSpecifier *Base = *I; |
| 159 | assert(!Base->isVirtual() && "Should not see virtual bases here!"); |
| 160 | |
| 161 | // Get the layout. |
| 162 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 163 | |
| 164 | const CXXRecordDecl *BaseDecl = |
Anders Carlsson | d829a02 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 165 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 166 | |
Anders Carlsson | d829a02 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 167 | // Add the offset. |
Ken Dyck | a1a4ae3 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 168 | Offset += Layout.getBaseClassOffset(BaseDecl); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 169 | |
Anders Carlsson | d829a02 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 170 | RD = BaseDecl; |
| 171 | } |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 172 | |
Ken Dyck | a1a4ae3 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 173 | return Offset; |
Anders Carlsson | d829a02 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 174 | } |
Anders Carlsson | 9a57c5a | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 175 | |
Anders Carlsson | 9150a2a | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 176 | llvm::Constant * |
Anders Carlsson | 8a64c1c | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 177 | CodeGenModule::GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 178 | CastExpr::path_const_iterator PathBegin, |
| 179 | CastExpr::path_const_iterator PathEnd) { |
| 180 | assert(PathBegin != PathEnd && "Base path should not be empty!"); |
Anders Carlsson | 8a64c1c | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 181 | |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 182 | CharUnits Offset = |
David Majnemer | c1709d3 | 2015-06-23 07:31:11 +0000 | [diff] [blame] | 183 | computeNonVirtualBaseClassOffset(ClassDecl, PathBegin, PathEnd); |
Ken Dyck | a1a4ae3 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 184 | if (Offset.isZero()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 185 | return nullptr; |
| 186 | |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 187 | llvm::Type *PtrDiffTy = |
Anders Carlsson | 8a64c1c | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 188 | Types.ConvertType(getContext().getPointerDiffType()); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 189 | |
Ken Dyck | a1a4ae3 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 190 | return llvm::ConstantInt::get(PtrDiffTy, Offset.getQuantity()); |
Anders Carlsson | 9150a2a | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Anders Carlsson | c4ba0cd | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 193 | /// Gets the address of a direct base class within a complete object. |
John McCall | 6ce7472 | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 194 | /// This should only be used for (1) non-virtual bases or (2) virtual bases |
| 195 | /// when the type is known to be complete (e.g. in complete destructors). |
| 196 | /// |
| 197 | /// The object pointed to by 'This' is assumed to be non-null. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 198 | Address |
| 199 | CodeGenFunction::GetAddressOfDirectBaseInCompleteClass(Address This, |
Anders Carlsson | c4ba0cd | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 200 | const CXXRecordDecl *Derived, |
| 201 | const CXXRecordDecl *Base, |
| 202 | bool BaseIsVirtual) { |
John McCall | 6ce7472 | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 203 | // 'this' must be a pointer (in some address space) to Derived. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 204 | assert(This.getElementType() == ConvertType(Derived)); |
John McCall | 6ce7472 | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 205 | |
| 206 | // Compute the offset of the virtual base. |
Ken Dyck | 6aa767c | 2011-03-22 01:21:15 +0000 | [diff] [blame] | 207 | CharUnits Offset; |
John McCall | 6ce7472 | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 208 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Derived); |
Anders Carlsson | c4ba0cd | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 209 | if (BaseIsVirtual) |
Ken Dyck | 6aa767c | 2011-03-22 01:21:15 +0000 | [diff] [blame] | 210 | Offset = Layout.getVBaseClassOffset(Base); |
John McCall | 6ce7472 | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 211 | else |
Ken Dyck | 6aa767c | 2011-03-22 01:21:15 +0000 | [diff] [blame] | 212 | Offset = Layout.getBaseClassOffset(Base); |
John McCall | 6ce7472 | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 213 | |
| 214 | // Shift and cast down to the base type. |
| 215 | // TODO: for complete types, this should be possible with a GEP. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 216 | Address V = This; |
| 217 | if (!Offset.isZero()) { |
| 218 | V = Builder.CreateElementBitCast(V, Int8Ty); |
| 219 | V = Builder.CreateConstInBoundsByteGEP(V, Offset); |
John McCall | 6ce7472 | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 220 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 221 | V = Builder.CreateElementBitCast(V, ConvertType(Base)); |
John McCall | 6ce7472 | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 222 | |
| 223 | return V; |
Anders Carlsson | e87fae9 | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 224 | } |
John McCall | 6ce7472 | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 225 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 226 | static Address |
| 227 | ApplyNonVirtualAndVirtualOffset(CodeGenFunction &CGF, Address addr, |
John McCall | 13a39c6 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 228 | CharUnits nonVirtualOffset, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 229 | llvm::Value *virtualOffset, |
| 230 | const CXXRecordDecl *derivedClass, |
| 231 | const CXXRecordDecl *nearestVBase) { |
John McCall | 13a39c6 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 232 | // Assert that we have something to do. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 233 | assert(!nonVirtualOffset.isZero() || virtualOffset != nullptr); |
John McCall | 13a39c6 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 234 | |
| 235 | // Compute the offset from the static and dynamic components. |
| 236 | llvm::Value *baseOffset; |
| 237 | if (!nonVirtualOffset.isZero()) { |
| 238 | baseOffset = llvm::ConstantInt::get(CGF.PtrDiffTy, |
| 239 | nonVirtualOffset.getQuantity()); |
| 240 | if (virtualOffset) { |
| 241 | baseOffset = CGF.Builder.CreateAdd(virtualOffset, baseOffset); |
| 242 | } |
| 243 | } else { |
| 244 | baseOffset = virtualOffset; |
| 245 | } |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 246 | |
Anders Carlsson | 53cebd1 | 2010-04-20 16:03:35 +0000 | [diff] [blame] | 247 | // Apply the base offset. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 248 | llvm::Value *ptr = addr.getPointer(); |
John McCall | 13a39c6 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 249 | ptr = CGF.Builder.CreateBitCast(ptr, CGF.Int8PtrTy); |
| 250 | ptr = CGF.Builder.CreateInBoundsGEP(ptr, baseOffset, "add.ptr"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 251 | |
| 252 | // If we have a virtual component, the alignment of the result will |
| 253 | // be relative only to the known alignment of that vbase. |
| 254 | CharUnits alignment; |
| 255 | if (virtualOffset) { |
| 256 | assert(nearestVBase && "virtual offset without vbase?"); |
| 257 | alignment = CGF.CGM.getVBaseAlignment(addr.getAlignment(), |
| 258 | derivedClass, nearestVBase); |
| 259 | } else { |
| 260 | alignment = addr.getAlignment(); |
| 261 | } |
| 262 | alignment = alignment.alignmentAtOffset(nonVirtualOffset); |
| 263 | |
| 264 | return Address(ptr, alignment); |
Anders Carlsson | 53cebd1 | 2010-04-20 16:03:35 +0000 | [diff] [blame] | 265 | } |
| 266 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 267 | Address CodeGenFunction::GetAddressOfBaseClass( |
| 268 | Address Value, const CXXRecordDecl *Derived, |
Alexey Samsonov | eb47d8a | 2014-10-13 23:59:00 +0000 | [diff] [blame] | 269 | CastExpr::path_const_iterator PathBegin, |
| 270 | CastExpr::path_const_iterator PathEnd, bool NullCheckValue, |
| 271 | SourceLocation Loc) { |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 272 | assert(PathBegin != PathEnd && "Base path should not be empty!"); |
Anders Carlsson | d829a02 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 273 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 274 | CastExpr::path_const_iterator Start = PathBegin; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 275 | const CXXRecordDecl *VBase = nullptr; |
| 276 | |
John McCall | 13a39c6 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 277 | // Sema has done some convenient canonicalization here: if the |
| 278 | // access path involved any virtual steps, the conversion path will |
| 279 | // *start* with a step down to the correct virtual base subobject, |
| 280 | // and hence will not require any further steps. |
Anders Carlsson | d829a02 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 281 | if ((*Start)->isVirtual()) { |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 282 | VBase = |
Anders Carlsson | d829a02 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 283 | cast<CXXRecordDecl>((*Start)->getType()->getAs<RecordType>()->getDecl()); |
| 284 | ++Start; |
| 285 | } |
John McCall | 13a39c6 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 286 | |
| 287 | // Compute the static offset of the ultimate destination within its |
| 288 | // allocating subobject (the virtual base, if there is one, or else |
| 289 | // the "complete" object that we see). |
David Majnemer | c1709d3 | 2015-06-23 07:31:11 +0000 | [diff] [blame] | 290 | CharUnits NonVirtualOffset = CGM.computeNonVirtualBaseClassOffset( |
| 291 | VBase ? VBase : Derived, Start, PathEnd); |
Anders Carlsson | d829a02 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 292 | |
John McCall | 13a39c6 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 293 | // If there's a virtual step, we can sometimes "devirtualize" it. |
| 294 | // For now, that's limited to when the derived type is final. |
| 295 | // TODO: "devirtualize" this for accesses to known-complete objects. |
| 296 | if (VBase && Derived->hasAttr<FinalAttr>()) { |
| 297 | const ASTRecordLayout &layout = getContext().getASTRecordLayout(Derived); |
| 298 | CharUnits vBaseOffset = layout.getVBaseClassOffset(VBase); |
| 299 | NonVirtualOffset += vBaseOffset; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 300 | VBase = nullptr; // we no longer have a virtual step |
John McCall | 13a39c6 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Anders Carlsson | d829a02 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 303 | // Get the base pointer type. |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 304 | llvm::Type *BasePtrTy = |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 305 | ConvertType((PathEnd[-1])->getType())->getPointerTo(); |
John McCall | 13a39c6 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 306 | |
Alexey Samsonov | eb47d8a | 2014-10-13 23:59:00 +0000 | [diff] [blame] | 307 | QualType DerivedTy = getContext().getRecordType(Derived); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 308 | CharUnits DerivedAlign = CGM.getClassPointerAlignment(Derived); |
Alexey Samsonov | eb47d8a | 2014-10-13 23:59:00 +0000 | [diff] [blame] | 309 | |
John McCall | 13a39c6 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 310 | // If the static offset is zero and we don't have a virtual step, |
| 311 | // just do a bitcast; null checks are unnecessary. |
Ken Dyck | a1a4ae3 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 312 | if (NonVirtualOffset.isZero() && !VBase) { |
Alexey Samsonov | eb47d8a | 2014-10-13 23:59:00 +0000 | [diff] [blame] | 313 | if (sanitizePerformTypeCheck()) { |
Vedant Kumar | 18348ea | 2017-02-17 23:22:55 +0000 | [diff] [blame] | 314 | SanitizerSet SkippedChecks; |
| 315 | SkippedChecks.set(SanitizerKind::Null, !NullCheckValue); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 316 | EmitTypeCheck(TCK_Upcast, Loc, Value.getPointer(), |
Vedant Kumar | 18348ea | 2017-02-17 23:22:55 +0000 | [diff] [blame] | 317 | DerivedTy, DerivedAlign, SkippedChecks); |
Alexey Samsonov | eb47d8a | 2014-10-13 23:59:00 +0000 | [diff] [blame] | 318 | } |
Anders Carlsson | d829a02 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 319 | return Builder.CreateBitCast(Value, BasePtrTy); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 320 | } |
John McCall | 13a39c6 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 321 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 322 | llvm::BasicBlock *origBB = nullptr; |
| 323 | llvm::BasicBlock *endBB = nullptr; |
| 324 | |
John McCall | 13a39c6 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 325 | // Skip over the offset (and the vtable load) if we're supposed to |
| 326 | // null-check the pointer. |
Anders Carlsson | d829a02 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 327 | if (NullCheckValue) { |
John McCall | 13a39c6 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 328 | origBB = Builder.GetInsertBlock(); |
| 329 | llvm::BasicBlock *notNullBB = createBasicBlock("cast.notnull"); |
| 330 | endBB = createBasicBlock("cast.end"); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 331 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 332 | llvm::Value *isNull = Builder.CreateIsNull(Value.getPointer()); |
John McCall | 13a39c6 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 333 | Builder.CreateCondBr(isNull, endBB, notNullBB); |
| 334 | EmitBlock(notNullBB); |
Anders Carlsson | d829a02 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Alexey Samsonov | eb47d8a | 2014-10-13 23:59:00 +0000 | [diff] [blame] | 337 | if (sanitizePerformTypeCheck()) { |
Vedant Kumar | 18348ea | 2017-02-17 23:22:55 +0000 | [diff] [blame] | 338 | SanitizerSet SkippedChecks; |
| 339 | SkippedChecks.set(SanitizerKind::Null, true); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 340 | EmitTypeCheck(VBase ? TCK_UpcastToVirtualBase : TCK_Upcast, Loc, |
Vedant Kumar | 18348ea | 2017-02-17 23:22:55 +0000 | [diff] [blame] | 341 | Value.getPointer(), DerivedTy, DerivedAlign, SkippedChecks); |
Alexey Samsonov | eb47d8a | 2014-10-13 23:59:00 +0000 | [diff] [blame] | 342 | } |
| 343 | |
John McCall | 13a39c6 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 344 | // Compute the virtual offset. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 345 | llvm::Value *VirtualOffset = nullptr; |
Anders Carlsson | a376b53 | 2011-01-29 03:18:56 +0000 | [diff] [blame] | 346 | if (VBase) { |
Reid Kleckner | d8cbeec | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 347 | VirtualOffset = |
| 348 | CGM.getCXXABI().GetVirtualBaseClassOffset(*this, Value, Derived, VBase); |
Anders Carlsson | a376b53 | 2011-01-29 03:18:56 +0000 | [diff] [blame] | 349 | } |
Anders Carlsson | d829a02 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 350 | |
John McCall | 13a39c6 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 351 | // Apply both offsets. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 352 | Value = ApplyNonVirtualAndVirtualOffset(*this, Value, NonVirtualOffset, |
| 353 | VirtualOffset, Derived, VBase); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 354 | |
John McCall | 13a39c6 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 355 | // Cast to the destination type. |
Anders Carlsson | d829a02 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 356 | Value = Builder.CreateBitCast(Value, BasePtrTy); |
John McCall | 13a39c6 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 357 | |
| 358 | // Build a phi if we needed a null check. |
Anders Carlsson | d829a02 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 359 | if (NullCheckValue) { |
John McCall | 13a39c6 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 360 | llvm::BasicBlock *notNullBB = Builder.GetInsertBlock(); |
| 361 | Builder.CreateBr(endBB); |
| 362 | EmitBlock(endBB); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 363 | |
John McCall | 13a39c6 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 364 | llvm::PHINode *PHI = Builder.CreatePHI(BasePtrTy, 2, "cast.result"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 365 | PHI->addIncoming(Value.getPointer(), notNullBB); |
John McCall | 13a39c6 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 366 | PHI->addIncoming(llvm::Constant::getNullValue(BasePtrTy), origBB); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 367 | Value = Address(PHI, Value.getAlignment()); |
Anders Carlsson | d829a02 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 368 | } |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 369 | |
Anders Carlsson | d829a02 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 370 | return Value; |
| 371 | } |
| 372 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 373 | Address |
| 374 | CodeGenFunction::GetAddressOfDerivedClass(Address BaseAddr, |
Anders Carlsson | c4ba0cd | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 375 | const CXXRecordDecl *Derived, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 376 | CastExpr::path_const_iterator PathBegin, |
| 377 | CastExpr::path_const_iterator PathEnd, |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 378 | bool NullCheckValue) { |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 379 | assert(PathBegin != PathEnd && "Base path should not be empty!"); |
Anders Carlsson | 8a64c1c | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 380 | |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 381 | QualType DerivedTy = |
Anders Carlsson | c4ba0cd | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 382 | getContext().getCanonicalType(getContext().getTagDeclType(Derived)); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 383 | llvm::Type *DerivedPtrTy = ConvertType(DerivedTy)->getPointerTo(); |
Richard Smith | 2c5868c | 2013-02-13 21:18:23 +0000 | [diff] [blame] | 384 | |
Anders Carlsson | 600f737 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 385 | llvm::Value *NonVirtualOffset = |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 386 | CGM.GetNonVirtualBaseClassOffset(Derived, PathBegin, PathEnd); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 387 | |
Anders Carlsson | 600f737 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 388 | if (!NonVirtualOffset) { |
| 389 | // No offset, we can just cast back. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 390 | return Builder.CreateBitCast(BaseAddr, DerivedPtrTy); |
Anders Carlsson | 600f737 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 391 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 392 | |
| 393 | llvm::BasicBlock *CastNull = nullptr; |
| 394 | llvm::BasicBlock *CastNotNull = nullptr; |
| 395 | llvm::BasicBlock *CastEnd = nullptr; |
| 396 | |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 397 | if (NullCheckValue) { |
| 398 | CastNull = createBasicBlock("cast.null"); |
| 399 | CastNotNull = createBasicBlock("cast.notnull"); |
| 400 | CastEnd = createBasicBlock("cast.end"); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 401 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 402 | llvm::Value *IsNull = Builder.CreateIsNull(BaseAddr.getPointer()); |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 403 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
| 404 | EmitBlock(CastNotNull); |
| 405 | } |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 406 | |
Anders Carlsson | 600f737 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 407 | // Apply the offset. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 408 | llvm::Value *Value = Builder.CreateBitCast(BaseAddr.getPointer(), Int8PtrTy); |
Eli Friedman | 8754926 | 2012-02-28 22:07:56 +0000 | [diff] [blame] | 409 | Value = Builder.CreateGEP(Value, Builder.CreateNeg(NonVirtualOffset), |
| 410 | "sub.ptr"); |
Anders Carlsson | 600f737 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 411 | |
| 412 | // Just cast. |
| 413 | Value = Builder.CreateBitCast(Value, DerivedPtrTy); |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 414 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 415 | // Produce a PHI if we had a null-check. |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 416 | if (NullCheckValue) { |
| 417 | Builder.CreateBr(CastEnd); |
| 418 | EmitBlock(CastNull); |
| 419 | Builder.CreateBr(CastEnd); |
| 420 | EmitBlock(CastEnd); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 421 | |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 422 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2); |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 423 | PHI->addIncoming(Value, CastNotNull); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 424 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull); |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 425 | Value = PHI; |
| 426 | } |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 427 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 428 | return Address(Value, CGM.getClassPointerAlignment(Derived)); |
Anders Carlsson | 9a57c5a | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 429 | } |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 430 | |
| 431 | llvm::Value *CodeGenFunction::GetVTTParameter(GlobalDecl GD, |
| 432 | bool ForVirtualBase, |
| 433 | bool Delegating) { |
Peter Collingbourne | 66f82e6 | 2013-06-28 20:45:28 +0000 | [diff] [blame] | 434 | if (!CGM.getCXXABI().NeedsVTTParameter(GD)) { |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 435 | // This constructor/destructor does not need a VTT parameter. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 436 | return nullptr; |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 437 | } |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 438 | |
John McCall | dec348f7 | 2013-05-03 07:33:41 +0000 | [diff] [blame] | 439 | const CXXRecordDecl *RD = cast<CXXMethodDecl>(CurCodeDecl)->getParent(); |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 440 | const CXXRecordDecl *Base = cast<CXXMethodDecl>(GD.getDecl())->getParent(); |
John McCall | 5c60a6f | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 441 | |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 442 | llvm::Value *VTT; |
| 443 | |
John McCall | 5c60a6f | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 444 | uint64_t SubVTTIndex; |
| 445 | |
Douglas Gregor | 6153500 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 446 | if (Delegating) { |
| 447 | // If this is a delegating constructor call, just load the VTT. |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 448 | return LoadCXXVTT(); |
Douglas Gregor | 6153500 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 449 | } else if (RD == Base) { |
| 450 | // If the record matches the base, this is the complete ctor/dtor |
| 451 | // variant calling the base variant in a class with virtual bases. |
Peter Collingbourne | 66f82e6 | 2013-06-28 20:45:28 +0000 | [diff] [blame] | 452 | assert(!CGM.getCXXABI().NeedsVTTParameter(CurGD) && |
John McCall | 5c60a6f | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 453 | "doing no-op VTT offset in base dtor/ctor?"); |
Anders Carlsson | 4d205ba | 2010-05-02 23:33:10 +0000 | [diff] [blame] | 454 | assert(!ForVirtualBase && "Can't have same class as virtual base!"); |
John McCall | 5c60a6f | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 455 | SubVTTIndex = 0; |
| 456 | } else { |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 457 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 458 | CharUnits BaseOffset = ForVirtualBase ? |
| 459 | Layout.getVBaseClassOffset(Base) : |
Ken Dyck | 16ffcac | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 460 | Layout.getBaseClassOffset(Base); |
Anders Carlsson | 859b306 | 2010-05-02 23:53:25 +0000 | [diff] [blame] | 461 | |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 462 | SubVTTIndex = |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 463 | CGM.getVTables().getSubVTTIndex(RD, BaseSubobject(Base, BaseOffset)); |
John McCall | 5c60a6f | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 464 | assert(SubVTTIndex != 0 && "Sub-VTT index must be greater than zero!"); |
| 465 | } |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 466 | |
Peter Collingbourne | 66f82e6 | 2013-06-28 20:45:28 +0000 | [diff] [blame] | 467 | if (CGM.getCXXABI().NeedsVTTParameter(CurGD)) { |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 468 | // A VTT parameter was passed to the constructor, use it. |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 469 | VTT = LoadCXXVTT(); |
| 470 | VTT = Builder.CreateConstInBoundsGEP1_64(VTT, SubVTTIndex); |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 471 | } else { |
| 472 | // We're the complete constructor, so get the VTT by name. |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 473 | VTT = CGM.getVTables().GetAddrOfVTT(RD); |
| 474 | VTT = Builder.CreateConstInBoundsGEP2_64(VTT, 0, SubVTTIndex); |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | return VTT; |
| 478 | } |
| 479 | |
John McCall | 1d98756 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 480 | namespace { |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 481 | /// Call the destructor for a direct base class. |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 482 | struct CallBaseDtor final : EHScopeStack::Cleanup { |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 483 | const CXXRecordDecl *BaseClass; |
| 484 | bool BaseIsVirtual; |
| 485 | CallBaseDtor(const CXXRecordDecl *Base, bool BaseIsVirtual) |
| 486 | : BaseClass(Base), BaseIsVirtual(BaseIsVirtual) {} |
John McCall | 1d98756 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 487 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 488 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 489 | const CXXRecordDecl *DerivedClass = |
| 490 | cast<CXXMethodDecl>(CGF.CurCodeDecl)->getParent(); |
| 491 | |
| 492 | const CXXDestructorDecl *D = BaseClass->getDestructor(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 493 | Address Addr = |
| 494 | CGF.GetAddressOfDirectBaseInCompleteClass(CGF.LoadCXXThisAddress(), |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 495 | DerivedClass, BaseClass, |
| 496 | BaseIsVirtual); |
Douglas Gregor | 6153500 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 497 | CGF.EmitCXXDestructorCall(D, Dtor_Base, BaseIsVirtual, |
| 498 | /*Delegating=*/false, Addr); |
John McCall | 1d98756 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 499 | } |
| 500 | }; |
John McCall | 769250e | 2010-09-17 02:31:44 +0000 | [diff] [blame] | 501 | |
| 502 | /// A visitor which checks whether an initializer uses 'this' in a |
| 503 | /// way which requires the vtable to be properly set. |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 504 | struct DynamicThisUseChecker : ConstEvaluatedExprVisitor<DynamicThisUseChecker> { |
| 505 | typedef ConstEvaluatedExprVisitor<DynamicThisUseChecker> super; |
John McCall | 769250e | 2010-09-17 02:31:44 +0000 | [diff] [blame] | 506 | |
| 507 | bool UsesThis; |
| 508 | |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 509 | DynamicThisUseChecker(const ASTContext &C) : super(C), UsesThis(false) {} |
John McCall | 769250e | 2010-09-17 02:31:44 +0000 | [diff] [blame] | 510 | |
| 511 | // Black-list all explicit and implicit references to 'this'. |
| 512 | // |
| 513 | // Do we need to worry about external references to 'this' derived |
| 514 | // from arbitrary code? If so, then anything which runs arbitrary |
| 515 | // external code might potentially access the vtable. |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 516 | void VisitCXXThisExpr(const CXXThisExpr *E) { UsesThis = true; } |
John McCall | 769250e | 2010-09-17 02:31:44 +0000 | [diff] [blame] | 517 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 518 | } // end anonymous namespace |
John McCall | 769250e | 2010-09-17 02:31:44 +0000 | [diff] [blame] | 519 | |
| 520 | static bool BaseInitializerUsesThis(ASTContext &C, const Expr *Init) { |
| 521 | DynamicThisUseChecker Checker(C); |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 522 | Checker.Visit(Init); |
John McCall | 769250e | 2010-09-17 02:31:44 +0000 | [diff] [blame] | 523 | return Checker.UsesThis; |
John McCall | 1d98756 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 526 | static void EmitBaseInitializer(CodeGenFunction &CGF, |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 527 | const CXXRecordDecl *ClassDecl, |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 528 | CXXCtorInitializer *BaseInit, |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 529 | CXXCtorType CtorType) { |
| 530 | assert(BaseInit->isBaseInitializer() && |
| 531 | "Must have base initializer!"); |
| 532 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 533 | Address ThisPtr = CGF.LoadCXXThisAddress(); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 534 | |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 535 | const Type *BaseType = BaseInit->getBaseClass(); |
| 536 | CXXRecordDecl *BaseClassDecl = |
| 537 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); |
| 538 | |
Anders Carlsson | 1c0f8bb | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 539 | bool isBaseVirtual = BaseInit->isBaseVirtual(); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 540 | |
| 541 | // The base constructor doesn't construct virtual bases. |
| 542 | if (CtorType == Ctor_Base && isBaseVirtual) |
| 543 | return; |
| 544 | |
John McCall | 769250e | 2010-09-17 02:31:44 +0000 | [diff] [blame] | 545 | // If the initializer for the base (other than the constructor |
| 546 | // itself) accesses 'this' in any way, we need to initialize the |
| 547 | // vtables. |
| 548 | if (BaseInitializerUsesThis(CGF.getContext(), BaseInit->getInit())) |
| 549 | CGF.InitializeVTablePointers(ClassDecl); |
| 550 | |
John McCall | 6ce7472 | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 551 | // We can pretend to be a complete class because it only matters for |
| 552 | // virtual bases, and we only do virtual bases for complete ctors. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 553 | Address V = |
Anders Carlsson | c4ba0cd | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 554 | CGF.GetAddressOfDirectBaseInCompleteClass(ThisPtr, ClassDecl, |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 555 | BaseClassDecl, |
| 556 | isBaseVirtual); |
John McCall | 8d6fc95 | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 557 | AggValueSlot AggSlot = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 558 | AggValueSlot::forAddr(V, Qualifiers(), |
John McCall | 8d6fc95 | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 559 | AggValueSlot::IsDestructed, |
John McCall | a5efa73 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 560 | AggValueSlot::DoesNotNeedGCBarriers, |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 561 | AggValueSlot::IsNotAliased); |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 562 | |
| 563 | CGF.EmitAggExpr(BaseInit->getInit(), AggSlot); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 564 | |
| 565 | if (CGF.CGM.getLangOpts().Exceptions && |
Anders Carlsson | 08ce5ed | 2011-02-20 00:20:27 +0000 | [diff] [blame] | 566 | !BaseClassDecl->hasTrivialDestructor()) |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 567 | CGF.EHStack.pushCleanup<CallBaseDtor>(EHCleanup, BaseClassDecl, |
| 568 | isBaseVirtual); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 571 | static bool isMemcpyEquivalentSpecialMember(const CXXMethodDecl *D) { |
| 572 | auto *CD = dyn_cast<CXXConstructorDecl>(D); |
| 573 | if (!(CD && CD->isCopyOrMoveConstructor()) && |
| 574 | !D->isCopyAssignmentOperator() && !D->isMoveAssignmentOperator()) |
| 575 | return false; |
| 576 | |
| 577 | // We can emit a memcpy for a trivial copy or move constructor/assignment. |
| 578 | if (D->isTrivial() && !D->getParent()->mayInsertExtraPadding()) |
| 579 | return true; |
| 580 | |
| 581 | // We *must* emit a memcpy for a defaulted union copy or move op. |
| 582 | if (D->getParent()->isUnion() && D->isDefaulted()) |
| 583 | return true; |
| 584 | |
| 585 | return false; |
| 586 | } |
| 587 | |
Alexey Bataev | 152c71f | 2015-07-14 07:55:48 +0000 | [diff] [blame] | 588 | static void EmitLValueForAnyFieldInitialization(CodeGenFunction &CGF, |
| 589 | CXXCtorInitializer *MemberInit, |
| 590 | LValue &LHS) { |
| 591 | FieldDecl *Field = MemberInit->getAnyMember(); |
| 592 | if (MemberInit->isIndirectMemberInitializer()) { |
| 593 | // If we are initializing an anonymous union field, drill down to the field. |
| 594 | IndirectFieldDecl *IndirectField = MemberInit->getIndirectMember(); |
| 595 | for (const auto *I : IndirectField->chain()) |
| 596 | LHS = CGF.EmitLValueForFieldInitialization(LHS, cast<FieldDecl>(I)); |
| 597 | } else { |
| 598 | LHS = CGF.EmitLValueForFieldInitialization(LHS, Field); |
| 599 | } |
| 600 | } |
| 601 | |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 602 | static void EmitMemberInitializer(CodeGenFunction &CGF, |
| 603 | const CXXRecordDecl *ClassDecl, |
Alexis Hunt | 1d79265 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 604 | CXXCtorInitializer *MemberInit, |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 605 | const CXXConstructorDecl *Constructor, |
| 606 | FunctionArgList &Args) { |
David Blaikie | a81d410 | 2015-01-18 00:12:58 +0000 | [diff] [blame] | 607 | ApplyDebugLocation Loc(CGF, MemberInit->getSourceLocation()); |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 608 | assert(MemberInit->isAnyMemberInitializer() && |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 609 | "Must have member initializer!"); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 610 | assert(MemberInit->getInit() && "Must have initializer!"); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 611 | |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 612 | // non-static data member initializers. |
Francois Pichet | d583da0 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 613 | FieldDecl *Field = MemberInit->getAnyMember(); |
Eli Friedman | 6ae6302 | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 614 | QualType FieldType = Field->getType(); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 615 | |
| 616 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 617 | QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl); |
Eli Friedman | f6d2184 | 2012-08-08 03:51:37 +0000 | [diff] [blame] | 618 | LValue LHS = CGF.MakeNaturalAlignAddrLValue(ThisPtr, RecordTy); |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 619 | |
Alexey Bataev | 152c71f | 2015-07-14 07:55:48 +0000 | [diff] [blame] | 620 | EmitLValueForAnyFieldInitialization(CGF, MemberInit, LHS); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 621 | |
Eli Friedman | 6ae6302 | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 622 | // Special case: if we are in a copy or move constructor, and we are copying |
| 623 | // an array of PODs or classes with trivial copy constructors, ignore the |
| 624 | // AST and perform the copy we know is equivalent. |
| 625 | // FIXME: This is hacky at best... if we had a bit more explicit information |
| 626 | // in the AST, we could generalize it more easily. |
| 627 | const ConstantArrayType *Array |
| 628 | = CGF.getContext().getAsConstantArrayType(FieldType); |
Jordan Rose | 54533f7 | 2013-08-07 16:16:48 +0000 | [diff] [blame] | 629 | if (Array && Constructor->isDefaulted() && |
Eli Friedman | 6ae6302 | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 630 | Constructor->isCopyOrMoveConstructor()) { |
| 631 | QualType BaseElementTy = CGF.getContext().getBaseElementType(Array); |
Richard Smith | 993f25a | 2012-11-07 23:56:21 +0000 | [diff] [blame] | 632 | CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(MemberInit->getInit()); |
Eli Friedman | 6ae6302 | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 633 | if (BaseElementTy.isPODType(CGF.getContext()) || |
Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 634 | (CE && isMemcpyEquivalentSpecialMember(CE->getConstructor()))) { |
David Majnemer | 1573d73 | 2014-10-15 04:54:54 +0000 | [diff] [blame] | 635 | unsigned SrcArgIndex = |
| 636 | CGF.CGM.getCXXABI().getSrcArgforCopyCtor(Constructor, Args); |
Eli Friedman | 6ae6302 | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 637 | llvm::Value *SrcPtr |
| 638 | = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(Args[SrcArgIndex])); |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 639 | LValue ThisRHSLV = CGF.MakeNaturalAlignAddrLValue(SrcPtr, RecordTy); |
| 640 | LValue Src = CGF.EmitLValueForFieldInitialization(ThisRHSLV, Field); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 641 | |
Eli Friedman | 6ae6302 | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 642 | // Copy the aggregate. |
| 643 | CGF.EmitAggregateCopy(LHS.getAddress(), Src.getAddress(), FieldType, |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 644 | LHS.isVolatileQualified()); |
Alexey Bataev | 5d49b83 | 2015-07-08 07:31:02 +0000 | [diff] [blame] | 645 | // Ensure that we destroy the objects if an exception is thrown later in |
| 646 | // the constructor. |
| 647 | QualType::DestructionKind dtorKind = FieldType.isDestructedType(); |
| 648 | if (CGF.needsEHCleanup(dtorKind)) |
| 649 | CGF.pushEHDestroy(dtorKind, LHS.getAddress(), FieldType); |
Eli Friedman | 6ae6302 | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 650 | return; |
| 651 | } |
| 652 | } |
| 653 | |
Richard Smith | 30e304e | 2016-12-14 00:03:17 +0000 | [diff] [blame] | 654 | CGF.EmitInitializerForField(Field, LHS, MemberInit->getInit()); |
Eli Friedman | 6ae6302 | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 655 | } |
| 656 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 657 | void CodeGenFunction::EmitInitializerForField(FieldDecl *Field, LValue LHS, |
Richard Smith | 30e304e | 2016-12-14 00:03:17 +0000 | [diff] [blame] | 658 | Expr *Init) { |
Eli Friedman | 6ae6302 | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 659 | QualType FieldType = Field->getType(); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 660 | switch (getEvaluationKind(FieldType)) { |
| 661 | case TEK_Scalar: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 662 | if (LHS.isSimple()) { |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 663 | EmitExprAsInit(Init, Field, LHS, false); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 664 | } else { |
Eli Friedman | 5f1a04f | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 665 | RValue RHS = RValue::get(EmitScalarExpr(Init)); |
| 666 | EmitStoreThroughLValue(RHS, LHS); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 667 | } |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 668 | break; |
| 669 | case TEK_Complex: |
David Blaikie | 66e4197 | 2015-01-14 07:38:27 +0000 | [diff] [blame] | 670 | EmitComplexExprIntoLValue(Init, LHS, /*isInit*/ true); |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 671 | break; |
| 672 | case TEK_Aggregate: { |
Richard Smith | 30e304e | 2016-12-14 00:03:17 +0000 | [diff] [blame] | 673 | AggValueSlot Slot = |
| 674 | AggValueSlot::forLValue(LHS, |
| 675 | AggValueSlot::IsDestructed, |
| 676 | AggValueSlot::DoesNotNeedGCBarriers, |
| 677 | AggValueSlot::IsNotAliased); |
| 678 | EmitAggExpr(Init, Slot); |
| 679 | break; |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 680 | } |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 681 | } |
John McCall | 12cc42a | 2013-02-01 05:11:40 +0000 | [diff] [blame] | 682 | |
| 683 | // Ensure that we destroy this object if an exception is thrown |
| 684 | // later in the constructor. |
| 685 | QualType::DestructionKind dtorKind = FieldType.isDestructedType(); |
| 686 | if (needsEHCleanup(dtorKind)) |
| 687 | pushEHDestroy(dtorKind, LHS.getAddress(), FieldType); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 688 | } |
| 689 | |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 690 | /// Checks whether the given constructor is a valid subject for the |
| 691 | /// complete-to-base constructor delegation optimization, i.e. |
| 692 | /// emitting the complete constructor as a simple call to the base |
| 693 | /// constructor. |
Vedant Kumar | 7f809b2 | 2017-02-24 01:15:19 +0000 | [diff] [blame] | 694 | bool CodeGenFunction::IsConstructorDelegationValid( |
| 695 | const CXXConstructorDecl *Ctor) { |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 696 | |
| 697 | // Currently we disable the optimization for classes with virtual |
| 698 | // bases because (1) the addresses of parameter variables need to be |
| 699 | // consistent across all initializers but (2) the delegate function |
| 700 | // call necessarily creates a second copy of the parameter variable. |
| 701 | // |
| 702 | // The limiting example (purely theoretical AFAIK): |
| 703 | // struct A { A(int &c) { c++; } }; |
| 704 | // struct B : virtual A { |
| 705 | // B(int count) : A(count) { printf("%d\n", count); } |
| 706 | // }; |
| 707 | // ...although even this example could in principle be emitted as a |
| 708 | // delegation since the address of the parameter doesn't escape. |
| 709 | if (Ctor->getParent()->getNumVBases()) { |
| 710 | // TODO: white-list trivial vbase initializers. This case wouldn't |
| 711 | // be subject to the restrictions below. |
| 712 | |
| 713 | // TODO: white-list cases where: |
| 714 | // - there are no non-reference parameters to the constructor |
| 715 | // - the initializers don't access any non-reference parameters |
| 716 | // - the initializers don't take the address of non-reference |
| 717 | // parameters |
| 718 | // - etc. |
| 719 | // If we ever add any of the above cases, remember that: |
| 720 | // - function-try-blocks will always blacklist this optimization |
| 721 | // - we need to perform the constructor prologue and cleanup in |
| 722 | // EmitConstructorBody. |
| 723 | |
| 724 | return false; |
| 725 | } |
| 726 | |
| 727 | // We also disable the optimization for variadic functions because |
| 728 | // it's impossible to "re-pass" varargs. |
| 729 | if (Ctor->getType()->getAs<FunctionProtoType>()->isVariadic()) |
| 730 | return false; |
| 731 | |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 732 | // FIXME: Decide if we can do a delegation of a delegating constructor. |
| 733 | if (Ctor->isDelegatingConstructor()) |
| 734 | return false; |
| 735 | |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 736 | return true; |
| 737 | } |
| 738 | |
Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 739 | // Emit code in ctor (Prologue==true) or dtor (Prologue==false) |
| 740 | // to poison the extra field paddings inserted under |
| 741 | // -fsanitize-address-field-padding=1|2. |
| 742 | void CodeGenFunction::EmitAsanPrologueOrEpilogue(bool Prologue) { |
| 743 | ASTContext &Context = getContext(); |
| 744 | const CXXRecordDecl *ClassDecl = |
| 745 | Prologue ? cast<CXXConstructorDecl>(CurGD.getDecl())->getParent() |
| 746 | : cast<CXXDestructorDecl>(CurGD.getDecl())->getParent(); |
| 747 | if (!ClassDecl->mayInsertExtraPadding()) return; |
| 748 | |
| 749 | struct SizeAndOffset { |
| 750 | uint64_t Size; |
| 751 | uint64_t Offset; |
| 752 | }; |
| 753 | |
| 754 | unsigned PtrSize = CGM.getDataLayout().getPointerSizeInBits(); |
| 755 | const ASTRecordLayout &Info = Context.getASTRecordLayout(ClassDecl); |
| 756 | |
| 757 | // Populate sizes and offsets of fields. |
| 758 | SmallVector<SizeAndOffset, 16> SSV(Info.getFieldCount()); |
| 759 | for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) |
| 760 | SSV[i].Offset = |
| 761 | Context.toCharUnitsFromBits(Info.getFieldOffset(i)).getQuantity(); |
| 762 | |
| 763 | size_t NumFields = 0; |
| 764 | for (const auto *Field : ClassDecl->fields()) { |
| 765 | const FieldDecl *D = Field; |
| 766 | std::pair<CharUnits, CharUnits> FieldInfo = |
| 767 | Context.getTypeInfoInChars(D->getType()); |
| 768 | CharUnits FieldSize = FieldInfo.first; |
| 769 | assert(NumFields < SSV.size()); |
| 770 | SSV[NumFields].Size = D->isBitField() ? 0 : FieldSize.getQuantity(); |
| 771 | NumFields++; |
| 772 | } |
| 773 | assert(NumFields == SSV.size()); |
| 774 | if (SSV.size() <= 1) return; |
| 775 | |
| 776 | // We will insert calls to __asan_* run-time functions. |
| 777 | // LLVM AddressSanitizer pass may decide to inline them later. |
| 778 | llvm::Type *Args[2] = {IntPtrTy, IntPtrTy}; |
| 779 | llvm::FunctionType *FTy = |
| 780 | llvm::FunctionType::get(CGM.VoidTy, Args, false); |
| 781 | llvm::Constant *F = CGM.CreateRuntimeFunction( |
| 782 | FTy, Prologue ? "__asan_poison_intra_object_redzone" |
| 783 | : "__asan_unpoison_intra_object_redzone"); |
| 784 | |
| 785 | llvm::Value *ThisPtr = LoadCXXThis(); |
| 786 | ThisPtr = Builder.CreatePtrToInt(ThisPtr, IntPtrTy); |
Kostya Serebryany | 6444921 | 2014-10-17 21:02:13 +0000 | [diff] [blame] | 787 | uint64_t TypeSize = Info.getNonVirtualSize().getQuantity(); |
Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 788 | // For each field check if it has sufficient padding, |
| 789 | // if so (un)poison it with a call. |
| 790 | for (size_t i = 0; i < SSV.size(); i++) { |
| 791 | uint64_t AsanAlignment = 8; |
| 792 | uint64_t NextField = i == SSV.size() - 1 ? TypeSize : SSV[i + 1].Offset; |
| 793 | uint64_t PoisonSize = NextField - SSV[i].Offset - SSV[i].Size; |
| 794 | uint64_t EndOffset = SSV[i].Offset + SSV[i].Size; |
| 795 | if (PoisonSize < AsanAlignment || !SSV[i].Size || |
| 796 | (NextField % AsanAlignment) != 0) |
| 797 | continue; |
David Blaikie | 43f9bb7 | 2015-05-18 22:14:03 +0000 | [diff] [blame] | 798 | Builder.CreateCall( |
| 799 | F, {Builder.CreateAdd(ThisPtr, Builder.getIntN(PtrSize, EndOffset)), |
| 800 | Builder.getIntN(PtrSize, PoisonSize)}); |
Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | |
John McCall | b81884d | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 804 | /// EmitConstructorBody - Emits the body of the current constructor. |
| 805 | void CodeGenFunction::EmitConstructorBody(FunctionArgList &Args) { |
Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 806 | EmitAsanPrologueOrEpilogue(true); |
John McCall | b81884d | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 807 | const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(CurGD.getDecl()); |
| 808 | CXXCtorType CtorType = CurGD.getCtorType(); |
| 809 | |
Reid Kleckner | 340ad86 | 2014-01-13 22:57:31 +0000 | [diff] [blame] | 810 | assert((CGM.getTarget().getCXXABI().hasConstructorVariants() || |
| 811 | CtorType == Ctor_Complete) && |
| 812 | "can only generate complete ctor for this ABI"); |
| 813 | |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 814 | // Before we go any further, try the complete->base constructor |
| 815 | // delegation optimization. |
Timur Iskhodzhanov | f32a377 | 2012-04-20 08:05:00 +0000 | [diff] [blame] | 816 | if (CtorType == Ctor_Complete && IsConstructorDelegationValid(Ctor) && |
John McCall | c8e0170 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 817 | CGM.getTarget().getCXXABI().hasConstructorVariants()) { |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 818 | EmitDelegateCXXConstructorCall(Ctor, Ctor_Base, Args, Ctor->getLocEnd()); |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 819 | return; |
| 820 | } |
| 821 | |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 822 | const FunctionDecl *Definition = nullptr; |
Richard Smith | 46bb581 | 2014-08-01 01:56:39 +0000 | [diff] [blame] | 823 | Stmt *Body = Ctor->getBody(Definition); |
| 824 | assert(Definition == Ctor && "emitting wrong constructor body"); |
John McCall | b81884d | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 825 | |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 826 | // Enter the function-try-block before the constructor prologue if |
| 827 | // applicable. |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 828 | bool IsTryBody = (Body && isa<CXXTryStmt>(Body)); |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 829 | if (IsTryBody) |
John McCall | b609d3f | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 830 | EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
John McCall | b81884d | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 831 | |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 832 | incrementProfileCounter(Body); |
Justin Bogner | 81c22c2 | 2014-01-23 02:54:27 +0000 | [diff] [blame] | 833 | |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 834 | RunCleanupsScope RunCleanups(*this); |
John McCall | b81884d | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 835 | |
John McCall | 8831303 | 2012-03-30 04:25:03 +0000 | [diff] [blame] | 836 | // TODO: in restricted cases, we can emit the vbase initializers of |
| 837 | // a complete ctor and then delegate to the base ctor. |
| 838 | |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 839 | // Emit the constructor prologue, i.e. the base and member |
| 840 | // initializers. |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 841 | EmitCtorPrologue(Ctor, CtorType, Args); |
John McCall | b81884d | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 842 | |
| 843 | // Emit the body of the statement. |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 844 | if (IsTryBody) |
John McCall | b81884d | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 845 | EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock()); |
| 846 | else if (Body) |
| 847 | EmitStmt(Body); |
John McCall | b81884d | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 848 | |
| 849 | // Emit any cleanup blocks associated with the member or base |
| 850 | // initializers, which includes (along the exceptional path) the |
| 851 | // destructors for those members and bases that were fully |
| 852 | // constructed. |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 853 | RunCleanups.ForceCleanup(); |
John McCall | b81884d | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 854 | |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 855 | if (IsTryBody) |
John McCall | b609d3f | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 856 | ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
John McCall | b81884d | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 859 | namespace { |
Nick Lewycky | 8b4e379 | 2013-09-11 02:03:20 +0000 | [diff] [blame] | 860 | /// RAII object to indicate that codegen is copying the value representation |
| 861 | /// instead of the object representation. Useful when copying a struct or |
| 862 | /// class which has uninitialized members and we're only performing |
| 863 | /// lvalue-to-rvalue conversion on the object but not its members. |
| 864 | class CopyingValueRepresentation { |
| 865 | public: |
| 866 | explicit CopyingValueRepresentation(CodeGenFunction &CGF) |
Alexey Samsonov | 035462c | 2014-10-30 19:33:44 +0000 | [diff] [blame] | 867 | : CGF(CGF), OldSanOpts(CGF.SanOpts) { |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 868 | CGF.SanOpts.set(SanitizerKind::Bool, false); |
| 869 | CGF.SanOpts.set(SanitizerKind::Enum, false); |
Nick Lewycky | 8b4e379 | 2013-09-11 02:03:20 +0000 | [diff] [blame] | 870 | } |
| 871 | ~CopyingValueRepresentation() { |
| 872 | CGF.SanOpts = OldSanOpts; |
| 873 | } |
| 874 | private: |
| 875 | CodeGenFunction &CGF; |
Alexey Samsonov | a041610 | 2014-11-11 01:26:14 +0000 | [diff] [blame] | 876 | SanitizerSet OldSanOpts; |
Nick Lewycky | 8b4e379 | 2013-09-11 02:03:20 +0000 | [diff] [blame] | 877 | }; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 878 | } // end anonymous namespace |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 879 | |
Nick Lewycky | 8b4e379 | 2013-09-11 02:03:20 +0000 | [diff] [blame] | 880 | namespace { |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 881 | class FieldMemcpyizer { |
| 882 | public: |
| 883 | FieldMemcpyizer(CodeGenFunction &CGF, const CXXRecordDecl *ClassDecl, |
| 884 | const VarDecl *SrcRec) |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 885 | : CGF(CGF), ClassDecl(ClassDecl), SrcRec(SrcRec), |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 886 | RecLayout(CGF.getContext().getASTRecordLayout(ClassDecl)), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 887 | FirstField(nullptr), LastField(nullptr), FirstFieldOffset(0), |
| 888 | LastFieldOffset(0), LastAddedFieldIndex(0) {} |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 889 | |
Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 890 | bool isMemcpyableField(FieldDecl *F) const { |
| 891 | // Never memcpy fields when we are adding poisoned paddings. |
Alexey Samsonov | a041610 | 2014-11-11 01:26:14 +0000 | [diff] [blame] | 892 | if (CGF.getContext().getLangOpts().SanitizeAddressFieldPadding) |
Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 893 | return false; |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 894 | Qualifiers Qual = F->getType().getQualifiers(); |
| 895 | if (Qual.hasVolatile() || Qual.hasObjCLifetime()) |
| 896 | return false; |
| 897 | return true; |
| 898 | } |
| 899 | |
| 900 | void addMemcpyableField(FieldDecl *F) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 901 | if (!FirstField) |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 902 | addInitialField(F); |
| 903 | else |
| 904 | addNextField(F); |
| 905 | } |
| 906 | |
David Majnemer | a586eb2 | 2014-10-10 18:57:10 +0000 | [diff] [blame] | 907 | CharUnits getMemcpySize(uint64_t FirstByteOffset) const { |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 908 | unsigned LastFieldSize = |
| 909 | LastField->isBitField() ? |
| 910 | LastField->getBitWidthValue(CGF.getContext()) : |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 911 | CGF.getContext().getTypeSize(LastField->getType()); |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 912 | uint64_t MemcpySizeBits = |
David Majnemer | a586eb2 | 2014-10-10 18:57:10 +0000 | [diff] [blame] | 913 | LastFieldOffset + LastFieldSize - FirstByteOffset + |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 914 | CGF.getContext().getCharWidth() - 1; |
| 915 | CharUnits MemcpySize = |
| 916 | CGF.getContext().toCharUnitsFromBits(MemcpySizeBits); |
| 917 | return MemcpySize; |
| 918 | } |
| 919 | |
| 920 | void emitMemcpy() { |
| 921 | // Give the subclass a chance to bail out if it feels the memcpy isn't |
| 922 | // worth it (e.g. Hasn't aggregated enough data). |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 923 | if (!FirstField) { |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 924 | return; |
| 925 | } |
| 926 | |
David Majnemer | a586eb2 | 2014-10-10 18:57:10 +0000 | [diff] [blame] | 927 | uint64_t FirstByteOffset; |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 928 | if (FirstField->isBitField()) { |
| 929 | const CGRecordLayout &RL = |
| 930 | CGF.getTypes().getCGRecordLayout(FirstField->getParent()); |
| 931 | const CGBitFieldInfo &BFInfo = RL.getBitFieldInfo(FirstField); |
David Majnemer | a586eb2 | 2014-10-10 18:57:10 +0000 | [diff] [blame] | 932 | // FirstFieldOffset is not appropriate for bitfields, |
Ulrich Weigand | 73263d7 | 2015-07-13 11:52:14 +0000 | [diff] [blame] | 933 | // we need to use the storage offset instead. |
Ulrich Weigand | 03ce2a1 | 2015-07-10 17:30:00 +0000 | [diff] [blame] | 934 | FirstByteOffset = CGF.getContext().toBits(BFInfo.StorageOffset); |
Lang Hames | 1694e0d | 2013-02-27 04:14:49 +0000 | [diff] [blame] | 935 | } else { |
David Majnemer | a586eb2 | 2014-10-10 18:57:10 +0000 | [diff] [blame] | 936 | FirstByteOffset = FirstFieldOffset; |
Lang Hames | 1694e0d | 2013-02-27 04:14:49 +0000 | [diff] [blame] | 937 | } |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 938 | |
David Majnemer | a586eb2 | 2014-10-10 18:57:10 +0000 | [diff] [blame] | 939 | CharUnits MemcpySize = getMemcpySize(FirstByteOffset); |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 940 | QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 941 | Address ThisPtr = CGF.LoadCXXThisAddress(); |
| 942 | LValue DestLV = CGF.MakeAddrLValue(ThisPtr, RecordTy); |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 943 | LValue Dest = CGF.EmitLValueForFieldInitialization(DestLV, FirstField); |
| 944 | llvm::Value *SrcPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(SrcRec)); |
| 945 | LValue SrcLV = CGF.MakeNaturalAlignAddrLValue(SrcPtr, RecordTy); |
| 946 | LValue Src = CGF.EmitLValueForFieldInitialization(SrcLV, FirstField); |
| 947 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 948 | emitMemcpyIR(Dest.isBitField() ? Dest.getBitFieldAddress() : Dest.getAddress(), |
| 949 | Src.isBitField() ? Src.getBitFieldAddress() : Src.getAddress(), |
| 950 | MemcpySize); |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 951 | reset(); |
| 952 | } |
| 953 | |
| 954 | void reset() { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 955 | FirstField = nullptr; |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | protected: |
| 959 | CodeGenFunction &CGF; |
| 960 | const CXXRecordDecl *ClassDecl; |
| 961 | |
| 962 | private: |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 963 | void emitMemcpyIR(Address DestPtr, Address SrcPtr, CharUnits Size) { |
| 964 | llvm::PointerType *DPT = DestPtr.getType(); |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 965 | llvm::Type *DBP = |
| 966 | llvm::Type::getInt8PtrTy(CGF.getLLVMContext(), DPT->getAddressSpace()); |
| 967 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, DBP); |
| 968 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 969 | llvm::PointerType *SPT = SrcPtr.getType(); |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 970 | llvm::Type *SBP = |
| 971 | llvm::Type::getInt8PtrTy(CGF.getLLVMContext(), SPT->getAddressSpace()); |
| 972 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, SBP); |
| 973 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 974 | CGF.Builder.CreateMemCpy(DestPtr, SrcPtr, Size.getQuantity()); |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | void addInitialField(FieldDecl *F) { |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 978 | FirstField = F; |
| 979 | LastField = F; |
| 980 | FirstFieldOffset = RecLayout.getFieldOffset(F->getFieldIndex()); |
| 981 | LastFieldOffset = FirstFieldOffset; |
| 982 | LastAddedFieldIndex = F->getFieldIndex(); |
| 983 | } |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 984 | |
| 985 | void addNextField(FieldDecl *F) { |
John McCall | 6054d5a | 2013-05-07 05:20:46 +0000 | [diff] [blame] | 986 | // For the most part, the following invariant will hold: |
| 987 | // F->getFieldIndex() == LastAddedFieldIndex + 1 |
| 988 | // The one exception is that Sema won't add a copy-initializer for an |
| 989 | // unnamed bitfield, which will show up here as a gap in the sequence. |
| 990 | assert(F->getFieldIndex() >= LastAddedFieldIndex + 1 && |
| 991 | "Cannot aggregate fields out of order."); |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 992 | LastAddedFieldIndex = F->getFieldIndex(); |
| 993 | |
| 994 | // The 'first' and 'last' fields are chosen by offset, rather than field |
| 995 | // index. This allows the code to support bitfields, as well as regular |
| 996 | // fields. |
| 997 | uint64_t FOffset = RecLayout.getFieldOffset(F->getFieldIndex()); |
| 998 | if (FOffset < FirstFieldOffset) { |
| 999 | FirstField = F; |
| 1000 | FirstFieldOffset = FOffset; |
| 1001 | } else if (FOffset > LastFieldOffset) { |
| 1002 | LastField = F; |
| 1003 | LastFieldOffset = FOffset; |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | const VarDecl *SrcRec; |
| 1008 | const ASTRecordLayout &RecLayout; |
| 1009 | FieldDecl *FirstField; |
| 1010 | FieldDecl *LastField; |
| 1011 | uint64_t FirstFieldOffset, LastFieldOffset; |
| 1012 | unsigned LastAddedFieldIndex; |
| 1013 | }; |
| 1014 | |
| 1015 | class ConstructorMemcpyizer : public FieldMemcpyizer { |
| 1016 | private: |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1017 | /// Get source argument for copy constructor. Returns null if not a copy |
David Majnemer | 196ac33 | 2014-09-11 23:05:02 +0000 | [diff] [blame] | 1018 | /// constructor. |
| 1019 | static const VarDecl *getTrivialCopySource(CodeGenFunction &CGF, |
| 1020 | const CXXConstructorDecl *CD, |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1021 | FunctionArgList &Args) { |
Jordan Rose | 54533f7 | 2013-08-07 16:16:48 +0000 | [diff] [blame] | 1022 | if (CD->isCopyOrMoveConstructor() && CD->isDefaulted()) |
David Majnemer | 196ac33 | 2014-09-11 23:05:02 +0000 | [diff] [blame] | 1023 | return Args[CGF.CGM.getCXXABI().getSrcArgforCopyCtor(CD, Args)]; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1024 | return nullptr; |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | // Returns true if a CXXCtorInitializer represents a member initialization |
| 1028 | // that can be rolled into a memcpy. |
| 1029 | bool isMemberInitMemcpyable(CXXCtorInitializer *MemberInit) const { |
| 1030 | if (!MemcpyableCtor) |
| 1031 | return false; |
| 1032 | FieldDecl *Field = MemberInit->getMember(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1033 | assert(Field && "No field for member init."); |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1034 | QualType FieldType = Field->getType(); |
| 1035 | CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(MemberInit->getInit()); |
| 1036 | |
Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 1037 | // Bail out on non-memcpyable, not-trivially-copyable members. |
| 1038 | if (!(CE && isMemcpyEquivalentSpecialMember(CE->getConstructor())) && |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1039 | !(FieldType.isTriviallyCopyableType(CGF.getContext()) || |
| 1040 | FieldType->isReferenceType())) |
| 1041 | return false; |
| 1042 | |
| 1043 | // Bail out on volatile fields. |
| 1044 | if (!isMemcpyableField(Field)) |
| 1045 | return false; |
| 1046 | |
| 1047 | // Otherwise we're good. |
| 1048 | return true; |
| 1049 | } |
| 1050 | |
| 1051 | public: |
| 1052 | ConstructorMemcpyizer(CodeGenFunction &CGF, const CXXConstructorDecl *CD, |
| 1053 | FunctionArgList &Args) |
David Majnemer | 196ac33 | 2014-09-11 23:05:02 +0000 | [diff] [blame] | 1054 | : FieldMemcpyizer(CGF, CD->getParent(), getTrivialCopySource(CGF, CD, Args)), |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1055 | ConstructorDecl(CD), |
Jordan Rose | 54533f7 | 2013-08-07 16:16:48 +0000 | [diff] [blame] | 1056 | MemcpyableCtor(CD->isDefaulted() && |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1057 | CD->isCopyOrMoveConstructor() && |
| 1058 | CGF.getLangOpts().getGC() == LangOptions::NonGC), |
| 1059 | Args(Args) { } |
| 1060 | |
| 1061 | void addMemberInitializer(CXXCtorInitializer *MemberInit) { |
| 1062 | if (isMemberInitMemcpyable(MemberInit)) { |
| 1063 | AggregatedInits.push_back(MemberInit); |
| 1064 | addMemcpyableField(MemberInit->getMember()); |
| 1065 | } else { |
| 1066 | emitAggregatedInits(); |
| 1067 | EmitMemberInitializer(CGF, ConstructorDecl->getParent(), MemberInit, |
| 1068 | ConstructorDecl, Args); |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | void emitAggregatedInits() { |
| 1073 | if (AggregatedInits.size() <= 1) { |
| 1074 | // This memcpy is too small to be worthwhile. Fall back on default |
| 1075 | // codegen. |
Nick Lewycky | 8b4e379 | 2013-09-11 02:03:20 +0000 | [diff] [blame] | 1076 | if (!AggregatedInits.empty()) { |
| 1077 | CopyingValueRepresentation CVR(CGF); |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1078 | EmitMemberInitializer(CGF, ConstructorDecl->getParent(), |
Nick Lewycky | 8b4e379 | 2013-09-11 02:03:20 +0000 | [diff] [blame] | 1079 | AggregatedInits[0], ConstructorDecl, Args); |
Alexey Bataev | 152c71f | 2015-07-14 07:55:48 +0000 | [diff] [blame] | 1080 | AggregatedInits.clear(); |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1081 | } |
| 1082 | reset(); |
| 1083 | return; |
| 1084 | } |
| 1085 | |
| 1086 | pushEHDestructors(); |
| 1087 | emitMemcpy(); |
| 1088 | AggregatedInits.clear(); |
| 1089 | } |
| 1090 | |
| 1091 | void pushEHDestructors() { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1092 | Address ThisPtr = CGF.LoadCXXThisAddress(); |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1093 | QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1094 | LValue LHS = CGF.MakeAddrLValue(ThisPtr, RecordTy); |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1095 | |
| 1096 | for (unsigned i = 0; i < AggregatedInits.size(); ++i) { |
Alexey Bataev | 152c71f | 2015-07-14 07:55:48 +0000 | [diff] [blame] | 1097 | CXXCtorInitializer *MemberInit = AggregatedInits[i]; |
| 1098 | QualType FieldType = MemberInit->getAnyMember()->getType(); |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1099 | QualType::DestructionKind dtorKind = FieldType.isDestructedType(); |
Alexey Bataev | 152c71f | 2015-07-14 07:55:48 +0000 | [diff] [blame] | 1100 | if (!CGF.needsEHCleanup(dtorKind)) |
| 1101 | continue; |
| 1102 | LValue FieldLHS = LHS; |
| 1103 | EmitLValueForAnyFieldInitialization(CGF, MemberInit, FieldLHS); |
| 1104 | CGF.pushEHDestroy(dtorKind, FieldLHS.getAddress(), FieldType); |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1105 | } |
| 1106 | } |
| 1107 | |
| 1108 | void finish() { |
| 1109 | emitAggregatedInits(); |
| 1110 | } |
| 1111 | |
| 1112 | private: |
| 1113 | const CXXConstructorDecl *ConstructorDecl; |
| 1114 | bool MemcpyableCtor; |
| 1115 | FunctionArgList &Args; |
| 1116 | SmallVector<CXXCtorInitializer*, 16> AggregatedInits; |
| 1117 | }; |
| 1118 | |
| 1119 | class AssignmentMemcpyizer : public FieldMemcpyizer { |
| 1120 | private: |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1121 | // Returns the memcpyable field copied by the given statement, if one |
Nick Lewycky | 8b4e379 | 2013-09-11 02:03:20 +0000 | [diff] [blame] | 1122 | // exists. Otherwise returns null. |
| 1123 | FieldDecl *getMemcpyableField(Stmt *S) { |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1124 | if (!AssignmentsMemcpyable) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1125 | return nullptr; |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1126 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(S)) { |
| 1127 | // Recognise trivial assignments. |
| 1128 | if (BO->getOpcode() != BO_Assign) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1129 | return nullptr; |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1130 | MemberExpr *ME = dyn_cast<MemberExpr>(BO->getLHS()); |
| 1131 | if (!ME) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1132 | return nullptr; |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1133 | FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl()); |
| 1134 | if (!Field || !isMemcpyableField(Field)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1135 | return nullptr; |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1136 | Stmt *RHS = BO->getRHS(); |
| 1137 | if (ImplicitCastExpr *EC = dyn_cast<ImplicitCastExpr>(RHS)) |
| 1138 | RHS = EC->getSubExpr(); |
| 1139 | if (!RHS) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1140 | return nullptr; |
Warren Ristow | 8d17b40 | 2017-02-02 17:53:34 +0000 | [diff] [blame] | 1141 | if (MemberExpr *ME2 = dyn_cast<MemberExpr>(RHS)) { |
| 1142 | if (ME2->getMemberDecl() == Field) |
| 1143 | return Field; |
| 1144 | } |
| 1145 | return nullptr; |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1146 | } else if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(S)) { |
| 1147 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MCE->getCalleeDecl()); |
Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 1148 | if (!(MD && isMemcpyEquivalentSpecialMember(MD))) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1149 | return nullptr; |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1150 | MemberExpr *IOA = dyn_cast<MemberExpr>(MCE->getImplicitObjectArgument()); |
| 1151 | if (!IOA) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1152 | return nullptr; |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1153 | FieldDecl *Field = dyn_cast<FieldDecl>(IOA->getMemberDecl()); |
| 1154 | if (!Field || !isMemcpyableField(Field)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1155 | return nullptr; |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1156 | MemberExpr *Arg0 = dyn_cast<MemberExpr>(MCE->getArg(0)); |
| 1157 | if (!Arg0 || Field != dyn_cast<FieldDecl>(Arg0->getMemberDecl())) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1158 | return nullptr; |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1159 | return Field; |
| 1160 | } else if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 1161 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CE->getCalleeDecl()); |
| 1162 | if (!FD || FD->getBuiltinID() != Builtin::BI__builtin_memcpy) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1163 | return nullptr; |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1164 | Expr *DstPtr = CE->getArg(0); |
| 1165 | if (ImplicitCastExpr *DC = dyn_cast<ImplicitCastExpr>(DstPtr)) |
| 1166 | DstPtr = DC->getSubExpr(); |
| 1167 | UnaryOperator *DUO = dyn_cast<UnaryOperator>(DstPtr); |
| 1168 | if (!DUO || DUO->getOpcode() != UO_AddrOf) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1169 | return nullptr; |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1170 | MemberExpr *ME = dyn_cast<MemberExpr>(DUO->getSubExpr()); |
| 1171 | if (!ME) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1172 | return nullptr; |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1173 | FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl()); |
| 1174 | if (!Field || !isMemcpyableField(Field)) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1175 | return nullptr; |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1176 | Expr *SrcPtr = CE->getArg(1); |
| 1177 | if (ImplicitCastExpr *SC = dyn_cast<ImplicitCastExpr>(SrcPtr)) |
| 1178 | SrcPtr = SC->getSubExpr(); |
| 1179 | UnaryOperator *SUO = dyn_cast<UnaryOperator>(SrcPtr); |
| 1180 | if (!SUO || SUO->getOpcode() != UO_AddrOf) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1181 | return nullptr; |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1182 | MemberExpr *ME2 = dyn_cast<MemberExpr>(SUO->getSubExpr()); |
| 1183 | if (!ME2 || Field != dyn_cast<FieldDecl>(ME2->getMemberDecl())) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1184 | return nullptr; |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1185 | return Field; |
| 1186 | } |
| 1187 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1188 | return nullptr; |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | bool AssignmentsMemcpyable; |
| 1192 | SmallVector<Stmt*, 16> AggregatedStmts; |
| 1193 | |
| 1194 | public: |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1195 | AssignmentMemcpyizer(CodeGenFunction &CGF, const CXXMethodDecl *AD, |
| 1196 | FunctionArgList &Args) |
| 1197 | : FieldMemcpyizer(CGF, AD->getParent(), Args[Args.size() - 1]), |
| 1198 | AssignmentsMemcpyable(CGF.getLangOpts().getGC() == LangOptions::NonGC) { |
| 1199 | assert(Args.size() == 2); |
| 1200 | } |
| 1201 | |
| 1202 | void emitAssignment(Stmt *S) { |
| 1203 | FieldDecl *F = getMemcpyableField(S); |
| 1204 | if (F) { |
| 1205 | addMemcpyableField(F); |
| 1206 | AggregatedStmts.push_back(S); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 1207 | } else { |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1208 | emitAggregatedStmts(); |
| 1209 | CGF.EmitStmt(S); |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | void emitAggregatedStmts() { |
| 1214 | if (AggregatedStmts.size() <= 1) { |
Nick Lewycky | 8b4e379 | 2013-09-11 02:03:20 +0000 | [diff] [blame] | 1215 | if (!AggregatedStmts.empty()) { |
| 1216 | CopyingValueRepresentation CVR(CGF); |
| 1217 | CGF.EmitStmt(AggregatedStmts[0]); |
| 1218 | } |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1219 | reset(); |
| 1220 | } |
| 1221 | |
| 1222 | emitMemcpy(); |
| 1223 | AggregatedStmts.clear(); |
| 1224 | } |
| 1225 | |
| 1226 | void finish() { |
| 1227 | emitAggregatedStmts(); |
| 1228 | } |
| 1229 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1230 | } // end anonymous namespace |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1231 | |
Piotr Padlewski | 338c9d0 | 2015-09-15 21:46:47 +0000 | [diff] [blame] | 1232 | static bool isInitializerOfDynamicClass(const CXXCtorInitializer *BaseInit) { |
| 1233 | const Type *BaseType = BaseInit->getBaseClass(); |
| 1234 | const auto *BaseClassDecl = |
| 1235 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); |
| 1236 | return BaseClassDecl->isDynamicClass(); |
| 1237 | } |
| 1238 | |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1239 | /// EmitCtorPrologue - This routine generates necessary code to initialize |
| 1240 | /// base classes and non-static data members belonging to this constructor. |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1241 | void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD, |
Douglas Gregor | 94f9a48 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1242 | CXXCtorType CtorType, |
| 1243 | FunctionArgList &Args) { |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 1244 | if (CD->isDelegatingConstructor()) |
| 1245 | return EmitDelegatingCXXConstructorCall(CD, Args); |
| 1246 | |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1247 | const CXXRecordDecl *ClassDecl = CD->getParent(); |
Anders Carlsson | 5dc8633 | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 1248 | |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1249 | CXXConstructorDecl::init_const_iterator B = CD->init_begin(), |
| 1250 | E = CD->init_end(); |
| 1251 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1252 | llvm::BasicBlock *BaseCtorContinueBB = nullptr; |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1253 | if (ClassDecl->getNumVBases() && |
| 1254 | !CGM.getTarget().getCXXABI().hasConstructorVariants()) { |
| 1255 | // The ABIs that don't have constructor variants need to put a branch |
| 1256 | // before the virtual base initialization code. |
Reid Kleckner | 7810af0 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1257 | BaseCtorContinueBB = |
| 1258 | CGM.getCXXABI().EmitCtorCompleteObjectHandler(*this, ClassDecl); |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1259 | assert(BaseCtorContinueBB); |
| 1260 | } |
| 1261 | |
Piotr Padlewski | 276a78d | 2015-10-02 22:12:40 +0000 | [diff] [blame] | 1262 | llvm::Value *const OldThis = CXXThisValue; |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1263 | // Virtual base initializers first. |
| 1264 | for (; B != E && (*B)->isBaseInitializer() && (*B)->isBaseVirtual(); B++) { |
Piotr Padlewski | 276a78d | 2015-10-02 22:12:40 +0000 | [diff] [blame] | 1265 | if (CGM.getCodeGenOpts().StrictVTablePointers && |
| 1266 | CGM.getCodeGenOpts().OptimizationLevel > 0 && |
| 1267 | isInitializerOfDynamicClass(*B)) |
| 1268 | CXXThisValue = Builder.CreateInvariantGroupBarrier(LoadCXXThis()); |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1269 | EmitBaseInitializer(*this, ClassDecl, *B, CtorType); |
| 1270 | } |
| 1271 | |
| 1272 | if (BaseCtorContinueBB) { |
| 1273 | // Complete object handler should continue to the remaining initializers. |
| 1274 | Builder.CreateBr(BaseCtorContinueBB); |
| 1275 | EmitBlock(BaseCtorContinueBB); |
| 1276 | } |
| 1277 | |
| 1278 | // Then, non-virtual base initializers. |
| 1279 | for (; B != E && (*B)->isBaseInitializer(); B++) { |
| 1280 | assert(!(*B)->isBaseVirtual()); |
Piotr Padlewski | 276a78d | 2015-10-02 22:12:40 +0000 | [diff] [blame] | 1281 | |
| 1282 | if (CGM.getCodeGenOpts().StrictVTablePointers && |
| 1283 | CGM.getCodeGenOpts().OptimizationLevel > 0 && |
| 1284 | isInitializerOfDynamicClass(*B)) |
| 1285 | CXXThisValue = Builder.CreateInvariantGroupBarrier(LoadCXXThis()); |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1286 | EmitBaseInitializer(*this, ClassDecl, *B, CtorType); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Piotr Padlewski | 276a78d | 2015-10-02 22:12:40 +0000 | [diff] [blame] | 1289 | CXXThisValue = OldThis; |
Piotr Padlewski | 338c9d0 | 2015-09-15 21:46:47 +0000 | [diff] [blame] | 1290 | |
Anders Carlsson | d589593 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1291 | InitializeVTablePointers(ClassDecl); |
Anders Carlsson | 5dc8633 | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 1292 | |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1293 | // And finally, initialize class members. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1294 | FieldConstructionScope FCS(*this, LoadCXXThisAddress()); |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1295 | ConstructorMemcpyizer CM(*this, CD, Args); |
Timur Iskhodzhanov | 57cbe5c | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1296 | for (; B != E; B++) { |
| 1297 | CXXCtorInitializer *Member = (*B); |
| 1298 | assert(!Member->isBaseInitializer()); |
| 1299 | assert(Member->isAnyMemberInitializer() && |
| 1300 | "Delegating initializer on non-delegating constructor"); |
| 1301 | CM.addMemberInitializer(Member); |
| 1302 | } |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1303 | CM.finish(); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
Anders Carlsson | 49c0bd2 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1306 | static bool |
| 1307 | FieldHasTrivialDestructorBody(ASTContext &Context, const FieldDecl *Field); |
| 1308 | |
| 1309 | static bool |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 1310 | HasTrivialDestructorBody(ASTContext &Context, |
Anders Carlsson | 49c0bd2 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1311 | const CXXRecordDecl *BaseClassDecl, |
| 1312 | const CXXRecordDecl *MostDerivedClassDecl) |
| 1313 | { |
| 1314 | // If the destructor is trivial we don't have to check anything else. |
| 1315 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1316 | return true; |
| 1317 | |
| 1318 | if (!BaseClassDecl->getDestructor()->hasTrivialBody()) |
| 1319 | return false; |
| 1320 | |
| 1321 | // Check fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1322 | for (const auto *Field : BaseClassDecl->fields()) |
Anders Carlsson | 49c0bd2 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1323 | if (!FieldHasTrivialDestructorBody(Context, Field)) |
| 1324 | return false; |
Anders Carlsson | 49c0bd2 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1325 | |
| 1326 | // Check non-virtual bases. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1327 | for (const auto &I : BaseClassDecl->bases()) { |
| 1328 | if (I.isVirtual()) |
Anders Carlsson | 49c0bd2 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1329 | continue; |
| 1330 | |
| 1331 | const CXXRecordDecl *NonVirtualBase = |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1332 | cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl()); |
Anders Carlsson | 49c0bd2 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1333 | if (!HasTrivialDestructorBody(Context, NonVirtualBase, |
| 1334 | MostDerivedClassDecl)) |
| 1335 | return false; |
| 1336 | } |
| 1337 | |
| 1338 | if (BaseClassDecl == MostDerivedClassDecl) { |
| 1339 | // Check virtual bases. |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 1340 | for (const auto &I : BaseClassDecl->vbases()) { |
Anders Carlsson | 49c0bd2 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1341 | const CXXRecordDecl *VirtualBase = |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 1342 | cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl()); |
Anders Carlsson | 49c0bd2 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1343 | if (!HasTrivialDestructorBody(Context, VirtualBase, |
| 1344 | MostDerivedClassDecl)) |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 1345 | return false; |
Anders Carlsson | 49c0bd2 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | return true; |
| 1350 | } |
| 1351 | |
| 1352 | static bool |
| 1353 | FieldHasTrivialDestructorBody(ASTContext &Context, |
Naomi Musgrave | 866af2d | 2015-09-03 23:02:30 +0000 | [diff] [blame] | 1354 | const FieldDecl *Field) |
Anders Carlsson | 49c0bd2 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1355 | { |
| 1356 | QualType FieldBaseElementType = Context.getBaseElementType(Field->getType()); |
| 1357 | |
| 1358 | const RecordType *RT = FieldBaseElementType->getAs<RecordType>(); |
| 1359 | if (!RT) |
| 1360 | return true; |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 1361 | |
Anders Carlsson | 49c0bd2 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1362 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
Davide Italiano | 982bbf4 | 2015-06-26 00:18:35 +0000 | [diff] [blame] | 1363 | |
| 1364 | // The destructor for an implicit anonymous union member is never invoked. |
| 1365 | if (FieldClassDecl->isUnion() && FieldClassDecl->isAnonymousStructOrUnion()) |
| 1366 | return false; |
| 1367 | |
Anders Carlsson | 49c0bd2 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1368 | return HasTrivialDestructorBody(Context, FieldClassDecl, FieldClassDecl); |
| 1369 | } |
| 1370 | |
Anders Carlsson | 9bd7d16 | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 1371 | /// CanSkipVTablePointerInitialization - Check whether we need to initialize |
| 1372 | /// any vtable pointers before calling this destructor. |
Naomi Musgrave | 866af2d | 2015-09-03 23:02:30 +0000 | [diff] [blame] | 1373 | static bool CanSkipVTablePointerInitialization(CodeGenFunction &CGF, |
Anders Carlsson | d6f1518 | 2011-05-16 04:08:36 +0000 | [diff] [blame] | 1374 | const CXXDestructorDecl *Dtor) { |
Piotr Padlewski | 338c9d0 | 2015-09-15 21:46:47 +0000 | [diff] [blame] | 1375 | const CXXRecordDecl *ClassDecl = Dtor->getParent(); |
| 1376 | if (!ClassDecl->isDynamicClass()) |
| 1377 | return true; |
| 1378 | |
Anders Carlsson | 9bd7d16 | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 1379 | if (!Dtor->hasTrivialBody()) |
| 1380 | return false; |
| 1381 | |
| 1382 | // Check the fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1383 | for (const auto *Field : ClassDecl->fields()) |
Naomi Musgrave | 866af2d | 2015-09-03 23:02:30 +0000 | [diff] [blame] | 1384 | if (!FieldHasTrivialDestructorBody(CGF.getContext(), Field)) |
Anders Carlsson | 49c0bd2 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1385 | return false; |
Anders Carlsson | 9bd7d16 | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 1386 | |
| 1387 | return true; |
| 1388 | } |
| 1389 | |
John McCall | b81884d | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1390 | /// EmitDestructorBody - Emits the body of the current destructor. |
| 1391 | void CodeGenFunction::EmitDestructorBody(FunctionArgList &Args) { |
| 1392 | const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CurGD.getDecl()); |
| 1393 | CXXDtorType DtorType = CurGD.getDtorType(); |
| 1394 | |
Richard Smith | df054d3 | 2017-02-25 23:53:05 +0000 | [diff] [blame] | 1395 | // For an abstract class, non-base destructors are never used (and can't |
| 1396 | // be emitted in general, because vbase dtors may not have been validated |
| 1397 | // by Sema), but the Itanium ABI doesn't make them optional and Clang may |
| 1398 | // in fact emit references to them from other compilations, so emit them |
| 1399 | // as functions containing a trap instruction. |
| 1400 | if (DtorType != Dtor_Base && Dtor->getParent()->isAbstract()) { |
| 1401 | llvm::CallInst *TrapCall = EmitTrapCall(llvm::Intrinsic::trap); |
| 1402 | TrapCall->setDoesNotReturn(); |
| 1403 | TrapCall->setDoesNotThrow(); |
| 1404 | Builder.CreateUnreachable(); |
| 1405 | Builder.ClearInsertionPoint(); |
| 1406 | return; |
| 1407 | } |
| 1408 | |
Justin Bogner | fb29822 | 2015-05-20 16:16:23 +0000 | [diff] [blame] | 1409 | Stmt *Body = Dtor->getBody(); |
| 1410 | if (Body) |
| 1411 | incrementProfileCounter(Body); |
| 1412 | |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1413 | // The call to operator delete in a deleting destructor happens |
| 1414 | // outside of the function-try-block, which means it's always |
| 1415 | // possible to delegate the destructor body to the complete |
| 1416 | // destructor. Do so. |
| 1417 | if (DtorType == Dtor_Deleting) { |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1418 | RunCleanupsScope DtorEpilogue(*this); |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1419 | EnterDtorCleanups(Dtor, Dtor_Deleting); |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1420 | if (HaveInsertPoint()) |
| 1421 | EmitCXXDestructorCall(Dtor, Dtor_Complete, /*ForVirtualBase=*/false, |
| 1422 | /*Delegating=*/false, LoadCXXThisAddress()); |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1423 | return; |
| 1424 | } |
| 1425 | |
John McCall | b81884d | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1426 | // If the body is a function-try-block, enter the try before |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1427 | // anything else. |
| 1428 | bool isTryBody = (Body && isa<CXXTryStmt>(Body)); |
John McCall | b81884d | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1429 | if (isTryBody) |
John McCall | b609d3f | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 1430 | EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 1431 | EmitAsanPrologueOrEpilogue(false); |
John McCall | b81884d | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1432 | |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1433 | // Enter the epilogue cleanups. |
| 1434 | RunCleanupsScope DtorEpilogue(*this); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 1435 | |
John McCall | b81884d | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1436 | // If this is the complete variant, just invoke the base variant; |
| 1437 | // the epilogue will destruct the virtual bases. But we can't do |
| 1438 | // this optimization if the body is a function-try-block, because |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 1439 | // we'd introduce *two* handler blocks. In the Microsoft ABI, we |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 1440 | // always delegate because we might not have a definition in this TU. |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1441 | switch (DtorType) { |
Saleem Abdulrasool | 8de4e87 | 2017-02-02 05:45:43 +0000 | [diff] [blame] | 1442 | case Dtor_Comdat: llvm_unreachable("not expecting a COMDAT"); |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1443 | case Dtor_Deleting: llvm_unreachable("already handled deleting case"); |
| 1444 | |
| 1445 | case Dtor_Complete: |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 1446 | assert((Body || getTarget().getCXXABI().isMicrosoft()) && |
| 1447 | "can't emit a dtor without a body for non-Microsoft ABIs"); |
| 1448 | |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1449 | // Enter the cleanup scopes for virtual bases. |
| 1450 | EnterDtorCleanups(Dtor, Dtor_Complete); |
| 1451 | |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 1452 | if (!isTryBody) { |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1453 | EmitCXXDestructorCall(Dtor, Dtor_Base, /*ForVirtualBase=*/false, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1454 | /*Delegating=*/false, LoadCXXThisAddress()); |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1455 | break; |
| 1456 | } |
Saleem Abdulrasool | 8de4e87 | 2017-02-02 05:45:43 +0000 | [diff] [blame] | 1457 | |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1458 | // Fallthrough: act like we're in the base variant. |
Saleem Abdulrasool | 8de4e87 | 2017-02-02 05:45:43 +0000 | [diff] [blame] | 1459 | LLVM_FALLTHROUGH; |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 1460 | |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1461 | case Dtor_Base: |
Reid Kleckner | e7de47e | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 1462 | assert(Body); |
| 1463 | |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1464 | // Enter the cleanup scopes for fields and non-virtual bases. |
| 1465 | EnterDtorCleanups(Dtor, Dtor_Base); |
| 1466 | |
| 1467 | // Initialize the vtable pointers before entering the body. |
Piotr Padlewski | 338c9d0 | 2015-09-15 21:46:47 +0000 | [diff] [blame] | 1468 | if (!CanSkipVTablePointerInitialization(*this, Dtor)) { |
| 1469 | // Insert the llvm.invariant.group.barrier intrinsic before initializing |
| 1470 | // the vptrs to cancel any previous assumptions we might have made. |
| 1471 | if (CGM.getCodeGenOpts().StrictVTablePointers && |
| 1472 | CGM.getCodeGenOpts().OptimizationLevel > 0) |
| 1473 | CXXThisValue = Builder.CreateInvariantGroupBarrier(LoadCXXThis()); |
| 1474 | InitializeVTablePointers(Dtor->getParent()); |
| 1475 | } |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1476 | |
| 1477 | if (isTryBody) |
| 1478 | EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock()); |
| 1479 | else if (Body) |
| 1480 | EmitStmt(Body); |
| 1481 | else { |
| 1482 | assert(Dtor->isImplicit() && "bodyless dtor not implicit"); |
| 1483 | // nothing to do besides what's in the epilogue |
| 1484 | } |
Fariborz Jahanian | 0c12ed1 | 2011-02-02 23:12:46 +0000 | [diff] [blame] | 1485 | // -fapple-kext must inline any call to this dtor into |
| 1486 | // the caller's body. |
Richard Smith | 9c6890a | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 1487 | if (getLangOpts().AppleKext) |
Evgeniy Stepanov | 6b2a61d | 2015-09-14 21:35:16 +0000 | [diff] [blame] | 1488 | CurFn->addFnAttr(llvm::Attribute::AlwaysInline); |
Naomi Musgrave | e50cb9b | 2015-08-13 18:35:11 +0000 | [diff] [blame] | 1489 | |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1490 | break; |
John McCall | b81884d | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1493 | // Jump out through the epilogue cleanups. |
| 1494 | DtorEpilogue.ForceCleanup(); |
John McCall | b81884d | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1495 | |
| 1496 | // Exit the try if applicable. |
| 1497 | if (isTryBody) |
John McCall | b609d3f | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 1498 | ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
John McCall | b81884d | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1501 | void CodeGenFunction::emitImplicitAssignmentOperatorBody(FunctionArgList &Args) { |
| 1502 | const CXXMethodDecl *AssignOp = cast<CXXMethodDecl>(CurGD.getDecl()); |
| 1503 | const Stmt *RootS = AssignOp->getBody(); |
| 1504 | assert(isa<CompoundStmt>(RootS) && |
| 1505 | "Body of an implicit assignment operator should be compound stmt."); |
| 1506 | const CompoundStmt *RootCS = cast<CompoundStmt>(RootS); |
| 1507 | |
| 1508 | LexicalScope Scope(*this, RootCS->getSourceRange()); |
| 1509 | |
Xinliang David Li | a951e8e | 2016-02-09 20:02:59 +0000 | [diff] [blame] | 1510 | incrementProfileCounter(RootCS); |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1511 | AssignmentMemcpyizer AM(*this, AssignOp, Args); |
Aaron Ballman | c7e4e21 | 2014-03-17 14:19:37 +0000 | [diff] [blame] | 1512 | for (auto *I : RootCS->body()) |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 1513 | AM.emitAssignment(I); |
Lang Hames | bf12274 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1514 | AM.finish(); |
| 1515 | } |
| 1516 | |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1517 | namespace { |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1518 | llvm::Value *LoadThisForDtorDelete(CodeGenFunction &CGF, |
| 1519 | const CXXDestructorDecl *DD) { |
| 1520 | if (Expr *ThisArg = DD->getOperatorDeleteThisArg()) |
Haojian Wu | 5b5c81f | 2017-10-13 15:37:53 +0000 | [diff] [blame] | 1521 | return CGF.EmitScalarExpr(ThisArg); |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1522 | return CGF.LoadCXXThis(); |
| 1523 | } |
| 1524 | |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1525 | /// Call the operator delete associated with the current destructor. |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 1526 | struct CallDtorDelete final : EHScopeStack::Cleanup { |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1527 | CallDtorDelete() {} |
| 1528 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1529 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1530 | const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl); |
| 1531 | const CXXRecordDecl *ClassDecl = Dtor->getParent(); |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1532 | CGF.EmitDeleteCall(Dtor->getOperatorDelete(), |
| 1533 | LoadThisForDtorDelete(CGF, Dtor), |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1534 | CGF.getContext().getTagDeclType(ClassDecl)); |
| 1535 | } |
| 1536 | }; |
| 1537 | |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1538 | void EmitConditionalDtorDeleteCall(CodeGenFunction &CGF, |
| 1539 | llvm::Value *ShouldDeleteCondition, |
| 1540 | bool ReturnAfterDelete) { |
| 1541 | llvm::BasicBlock *callDeleteBB = CGF.createBasicBlock("dtor.call_delete"); |
| 1542 | llvm::BasicBlock *continueBB = CGF.createBasicBlock("dtor.continue"); |
| 1543 | llvm::Value *ShouldCallDelete |
| 1544 | = CGF.Builder.CreateIsNull(ShouldDeleteCondition); |
| 1545 | CGF.Builder.CreateCondBr(ShouldCallDelete, continueBB, callDeleteBB); |
| 1546 | |
| 1547 | CGF.EmitBlock(callDeleteBB); |
| 1548 | const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl); |
| 1549 | const CXXRecordDecl *ClassDecl = Dtor->getParent(); |
| 1550 | CGF.EmitDeleteCall(Dtor->getOperatorDelete(), |
| 1551 | LoadThisForDtorDelete(CGF, Dtor), |
| 1552 | CGF.getContext().getTagDeclType(ClassDecl)); |
| 1553 | assert(Dtor->getOperatorDelete()->isDestroyingOperatorDelete() == |
| 1554 | ReturnAfterDelete && |
| 1555 | "unexpected value for ReturnAfterDelete"); |
| 1556 | if (ReturnAfterDelete) |
| 1557 | CGF.EmitBranchThroughCleanup(CGF.ReturnBlock); |
| 1558 | else |
| 1559 | CGF.Builder.CreateBr(continueBB); |
| 1560 | |
| 1561 | CGF.EmitBlock(continueBB); |
| 1562 | } |
| 1563 | |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 1564 | struct CallDtorDeleteConditional final : EHScopeStack::Cleanup { |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1565 | llvm::Value *ShouldDeleteCondition; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 1566 | |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1567 | public: |
| 1568 | CallDtorDeleteConditional(llvm::Value *ShouldDeleteCondition) |
Naomi Musgrave | 866af2d | 2015-09-03 23:02:30 +0000 | [diff] [blame] | 1569 | : ShouldDeleteCondition(ShouldDeleteCondition) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1570 | assert(ShouldDeleteCondition != nullptr); |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1573 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1574 | EmitConditionalDtorDeleteCall(CGF, ShouldDeleteCondition, |
| 1575 | /*ReturnAfterDelete*/false); |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1576 | } |
| 1577 | }; |
| 1578 | |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 1579 | class DestroyField final : public EHScopeStack::Cleanup { |
John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1580 | const FieldDecl *field; |
Peter Collingbourne | 1425b45 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 1581 | CodeGenFunction::Destroyer *destroyer; |
John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1582 | bool useEHCleanupForArray; |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1583 | |
John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1584 | public: |
| 1585 | DestroyField(const FieldDecl *field, CodeGenFunction::Destroyer *destroyer, |
| 1586 | bool useEHCleanupForArray) |
Naomi Musgrave | 866af2d | 2015-09-03 23:02:30 +0000 | [diff] [blame] | 1587 | : field(field), destroyer(destroyer), |
| 1588 | useEHCleanupForArray(useEHCleanupForArray) {} |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1589 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 1590 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1591 | // Find the address of the field. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1592 | Address thisValue = CGF.LoadCXXThisAddress(); |
Eli Friedman | 7f1ff60 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 1593 | QualType RecordTy = CGF.getContext().getTagDeclType(field->getParent()); |
| 1594 | LValue ThisLV = CGF.MakeAddrLValue(thisValue, RecordTy); |
| 1595 | LValue LV = CGF.EmitLValueForField(ThisLV, field); |
John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1596 | assert(LV.isSimple()); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 1597 | |
John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1598 | CGF.emitDestroy(LV.getAddress(), field->getType(), destroyer, |
John McCall | 30317fd | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1599 | flags.isForNormalCleanup() && useEHCleanupForArray); |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1600 | } |
| 1601 | }; |
Naomi Musgrave | 866af2d | 2015-09-03 23:02:30 +0000 | [diff] [blame] | 1602 | |
Naomi Musgrave | 703835c | 2015-09-16 00:38:22 +0000 | [diff] [blame] | 1603 | static void EmitSanitizerDtorCallback(CodeGenFunction &CGF, llvm::Value *Ptr, |
| 1604 | CharUnits::QuantityType PoisonSize) { |
Matt Morehouse | 4881a23 | 2017-09-20 22:53:08 +0000 | [diff] [blame] | 1605 | CodeGenFunction::SanitizerScope SanScope(&CGF); |
Naomi Musgrave | 703835c | 2015-09-16 00:38:22 +0000 | [diff] [blame] | 1606 | // Pass in void pointer and size of region as arguments to runtime |
| 1607 | // function |
| 1608 | llvm::Value *Args[] = {CGF.Builder.CreateBitCast(Ptr, CGF.VoidPtrTy), |
| 1609 | llvm::ConstantInt::get(CGF.SizeTy, PoisonSize)}; |
| 1610 | |
| 1611 | llvm::Type *ArgTypes[] = {CGF.VoidPtrTy, CGF.SizeTy}; |
| 1612 | |
| 1613 | llvm::FunctionType *FnType = |
| 1614 | llvm::FunctionType::get(CGF.VoidTy, ArgTypes, false); |
| 1615 | llvm::Value *Fn = |
| 1616 | CGF.CGM.CreateRuntimeFunction(FnType, "__sanitizer_dtor_callback"); |
| 1617 | CGF.EmitNounwindRuntimeCall(Fn, Args); |
| 1618 | } |
| 1619 | |
| 1620 | class SanitizeDtorMembers final : public EHScopeStack::Cleanup { |
Naomi Musgrave | 866af2d | 2015-09-03 23:02:30 +0000 | [diff] [blame] | 1621 | const CXXDestructorDecl *Dtor; |
| 1622 | |
| 1623 | public: |
Naomi Musgrave | 703835c | 2015-09-16 00:38:22 +0000 | [diff] [blame] | 1624 | SanitizeDtorMembers(const CXXDestructorDecl *Dtor) : Dtor(Dtor) {} |
Naomi Musgrave | 866af2d | 2015-09-03 23:02:30 +0000 | [diff] [blame] | 1625 | |
| 1626 | // Generate function call for handling object poisoning. |
| 1627 | // Disables tail call elimination, to prevent the current stack frame |
| 1628 | // from disappearing from the stack trace. |
| 1629 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
| 1630 | const ASTRecordLayout &Layout = |
| 1631 | CGF.getContext().getASTRecordLayout(Dtor->getParent()); |
| 1632 | |
| 1633 | // Nothing to poison. |
| 1634 | if (Layout.getFieldCount() == 0) |
| 1635 | return; |
| 1636 | |
| 1637 | // Prevent the current stack frame from disappearing from the stack trace. |
| 1638 | CGF.CurFn->addFnAttr("disable-tail-calls", "true"); |
| 1639 | |
| 1640 | // Construct pointer to region to begin poisoning, and calculate poison |
| 1641 | // size, so that only members declared in this class are poisoned. |
| 1642 | ASTContext &Context = CGF.getContext(); |
| 1643 | unsigned fieldIndex = 0; |
| 1644 | int startIndex = -1; |
| 1645 | // RecordDecl::field_iterator Field; |
| 1646 | for (const FieldDecl *Field : Dtor->getParent()->fields()) { |
| 1647 | // Poison field if it is trivial |
| 1648 | if (FieldHasTrivialDestructorBody(Context, Field)) { |
| 1649 | // Start sanitizing at this field |
| 1650 | if (startIndex < 0) |
| 1651 | startIndex = fieldIndex; |
| 1652 | |
| 1653 | // Currently on the last field, and it must be poisoned with the |
| 1654 | // current block. |
| 1655 | if (fieldIndex == Layout.getFieldCount() - 1) { |
Naomi Musgrave | 703835c | 2015-09-16 00:38:22 +0000 | [diff] [blame] | 1656 | PoisonMembers(CGF, startIndex, Layout.getFieldCount()); |
Naomi Musgrave | 866af2d | 2015-09-03 23:02:30 +0000 | [diff] [blame] | 1657 | } |
| 1658 | } else if (startIndex >= 0) { |
| 1659 | // No longer within a block of memory to poison, so poison the block |
Naomi Musgrave | 703835c | 2015-09-16 00:38:22 +0000 | [diff] [blame] | 1660 | PoisonMembers(CGF, startIndex, fieldIndex); |
Naomi Musgrave | 866af2d | 2015-09-03 23:02:30 +0000 | [diff] [blame] | 1661 | // Re-set the start index |
| 1662 | startIndex = -1; |
| 1663 | } |
| 1664 | fieldIndex += 1; |
| 1665 | } |
| 1666 | } |
| 1667 | |
| 1668 | private: |
NAKAMURA Takumi | f6cef72f | 2015-09-04 05:19:31 +0000 | [diff] [blame] | 1669 | /// \param layoutStartOffset index of the ASTRecordLayout field to |
Naomi Musgrave | 866af2d | 2015-09-03 23:02:30 +0000 | [diff] [blame] | 1670 | /// start poisoning (inclusive) |
NAKAMURA Takumi | f6cef72f | 2015-09-04 05:19:31 +0000 | [diff] [blame] | 1671 | /// \param layoutEndOffset index of the ASTRecordLayout field to |
Naomi Musgrave | 866af2d | 2015-09-03 23:02:30 +0000 | [diff] [blame] | 1672 | /// end poisoning (exclusive) |
Naomi Musgrave | 703835c | 2015-09-16 00:38:22 +0000 | [diff] [blame] | 1673 | void PoisonMembers(CodeGenFunction &CGF, unsigned layoutStartOffset, |
Naomi Musgrave | 866af2d | 2015-09-03 23:02:30 +0000 | [diff] [blame] | 1674 | unsigned layoutEndOffset) { |
| 1675 | ASTContext &Context = CGF.getContext(); |
| 1676 | const ASTRecordLayout &Layout = |
| 1677 | Context.getASTRecordLayout(Dtor->getParent()); |
| 1678 | |
| 1679 | llvm::ConstantInt *OffsetSizePtr = llvm::ConstantInt::get( |
| 1680 | CGF.SizeTy, |
| 1681 | Context.toCharUnitsFromBits(Layout.getFieldOffset(layoutStartOffset)) |
| 1682 | .getQuantity()); |
| 1683 | |
| 1684 | llvm::Value *OffsetPtr = CGF.Builder.CreateGEP( |
| 1685 | CGF.Builder.CreateBitCast(CGF.LoadCXXThis(), CGF.Int8PtrTy), |
| 1686 | OffsetSizePtr); |
| 1687 | |
| 1688 | CharUnits::QuantityType PoisonSize; |
| 1689 | if (layoutEndOffset >= Layout.getFieldCount()) { |
| 1690 | PoisonSize = Layout.getNonVirtualSize().getQuantity() - |
| 1691 | Context.toCharUnitsFromBits( |
| 1692 | Layout.getFieldOffset(layoutStartOffset)) |
| 1693 | .getQuantity(); |
| 1694 | } else { |
| 1695 | PoisonSize = Context.toCharUnitsFromBits( |
| 1696 | Layout.getFieldOffset(layoutEndOffset) - |
| 1697 | Layout.getFieldOffset(layoutStartOffset)) |
| 1698 | .getQuantity(); |
| 1699 | } |
| 1700 | |
| 1701 | if (PoisonSize == 0) |
| 1702 | return; |
| 1703 | |
Naomi Musgrave | 703835c | 2015-09-16 00:38:22 +0000 | [diff] [blame] | 1704 | EmitSanitizerDtorCallback(CGF, OffsetPtr, PoisonSize); |
Naomi Musgrave | 866af2d | 2015-09-03 23:02:30 +0000 | [diff] [blame] | 1705 | } |
| 1706 | }; |
Naomi Musgrave | 703835c | 2015-09-16 00:38:22 +0000 | [diff] [blame] | 1707 | |
| 1708 | class SanitizeDtorVTable final : public EHScopeStack::Cleanup { |
| 1709 | const CXXDestructorDecl *Dtor; |
| 1710 | |
| 1711 | public: |
| 1712 | SanitizeDtorVTable(const CXXDestructorDecl *Dtor) : Dtor(Dtor) {} |
| 1713 | |
| 1714 | // Generate function call for handling vtable pointer poisoning. |
| 1715 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
| 1716 | assert(Dtor->getParent()->isDynamicClass()); |
NAKAMURA Takumi | ee82b49 | 2015-09-16 06:26:56 +0000 | [diff] [blame] | 1717 | (void)Dtor; |
Naomi Musgrave | 703835c | 2015-09-16 00:38:22 +0000 | [diff] [blame] | 1718 | ASTContext &Context = CGF.getContext(); |
| 1719 | // Poison vtable and vtable ptr if they exist for this class. |
| 1720 | llvm::Value *VTablePtr = CGF.LoadCXXThis(); |
| 1721 | |
| 1722 | CharUnits::QuantityType PoisonSize = |
| 1723 | Context.toCharUnitsFromBits(CGF.PointerWidthInBits).getQuantity(); |
| 1724 | // Pass in void pointer and size of region as arguments to runtime |
| 1725 | // function |
| 1726 | EmitSanitizerDtorCallback(CGF, VTablePtr, PoisonSize); |
| 1727 | } |
| 1728 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1729 | } // end anonymous namespace |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1730 | |
Hans Wennborg | deff703 | 2013-12-18 01:39:59 +0000 | [diff] [blame] | 1731 | /// \brief Emit all code that comes at the end of class's |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1732 | /// destructor. This is to call destructors on members and base classes |
| 1733 | /// in reverse order of their construction. |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1734 | /// |
| 1735 | /// For a deleting destructor, this also handles the case where a destroying |
| 1736 | /// operator delete completely overrides the definition. |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1737 | void CodeGenFunction::EnterDtorCleanups(const CXXDestructorDecl *DD, |
| 1738 | CXXDtorType DtorType) { |
Hans Wennborg | 853ae94 | 2014-05-30 16:59:42 +0000 | [diff] [blame] | 1739 | assert((!DD->isTrivial() || DD->hasAttr<DLLExportAttr>()) && |
| 1740 | "Should not emit dtor epilogue for non-exported trivial dtor!"); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1741 | |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1742 | // The deleting-destructor phase just needs to call the appropriate |
| 1743 | // operator delete that Sema picked up. |
John McCall | 5c60a6f | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1744 | if (DtorType == Dtor_Deleting) { |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 1745 | assert(DD->getOperatorDelete() && |
Hans Wennborg | deff703 | 2013-12-18 01:39:59 +0000 | [diff] [blame] | 1746 | "operator delete missing - EnterDtorCleanups"); |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1747 | if (CXXStructorImplicitParamValue) { |
| 1748 | // If there is an implicit param to the deleting dtor, it's a boolean |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1749 | // telling whether this is a deleting destructor. |
| 1750 | if (DD->getOperatorDelete()->isDestroyingOperatorDelete()) |
| 1751 | EmitConditionalDtorDeleteCall(*this, CXXStructorImplicitParamValue, |
| 1752 | /*ReturnAfterDelete*/true); |
| 1753 | else |
| 1754 | EHStack.pushCleanup<CallDtorDeleteConditional>( |
| 1755 | NormalAndEHCleanup, CXXStructorImplicitParamValue); |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1756 | } else { |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 1757 | if (DD->getOperatorDelete()->isDestroyingOperatorDelete()) { |
| 1758 | const CXXRecordDecl *ClassDecl = DD->getParent(); |
| 1759 | EmitDeleteCall(DD->getOperatorDelete(), |
| 1760 | LoadThisForDtorDelete(*this, DD), |
| 1761 | getContext().getTagDeclType(ClassDecl)); |
| 1762 | EmitBranchThroughCleanup(ReturnBlock); |
| 1763 | } else { |
| 1764 | EHStack.pushCleanup<CallDtorDelete>(NormalAndEHCleanup); |
| 1765 | } |
Timur Iskhodzhanov | ee6bc53 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1766 | } |
John McCall | 5c60a6f | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1767 | return; |
| 1768 | } |
| 1769 | |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1770 | const CXXRecordDecl *ClassDecl = DD->getParent(); |
| 1771 | |
Richard Smith | 2010404 | 2011-09-18 12:11:43 +0000 | [diff] [blame] | 1772 | // Unions have no bases and do not call field destructors. |
| 1773 | if (ClassDecl->isUnion()) |
| 1774 | return; |
| 1775 | |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1776 | // The complete-destructor phase just destructs all the virtual bases. |
John McCall | 5c60a6f | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1777 | if (DtorType == Dtor_Complete) { |
Naomi Musgrave | 703835c | 2015-09-16 00:38:22 +0000 | [diff] [blame] | 1778 | // Poison the vtable pointer such that access after the base |
| 1779 | // and member destructors are invoked is invalid. |
| 1780 | if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor && |
| 1781 | SanOpts.has(SanitizerKind::Memory) && ClassDecl->getNumVBases() && |
| 1782 | ClassDecl->isPolymorphic()) |
| 1783 | EHStack.pushCleanup<SanitizeDtorVTable>(NormalAndEHCleanup, DD); |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1784 | |
| 1785 | // We push them in the forward order so that they'll be popped in |
| 1786 | // the reverse order. |
Aaron Ballman | 445a939 | 2014-03-13 16:15:17 +0000 | [diff] [blame] | 1787 | for (const auto &Base : ClassDecl->vbases()) { |
John McCall | 5c60a6f | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1788 | CXXRecordDecl *BaseClassDecl |
| 1789 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 1790 | |
John McCall | 5c60a6f | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1791 | // Ignore trivial destructors. |
| 1792 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1793 | continue; |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1794 | |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1795 | EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup, |
| 1796 | BaseClassDecl, |
| 1797 | /*BaseIsVirtual*/ true); |
John McCall | 5c60a6f | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1798 | } |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1799 | |
John McCall | 5c60a6f | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1800 | return; |
| 1801 | } |
| 1802 | |
| 1803 | assert(DtorType == Dtor_Base); |
Naomi Musgrave | 703835c | 2015-09-16 00:38:22 +0000 | [diff] [blame] | 1804 | // Poison the vtable pointer if it has no virtual bases, but inherits |
| 1805 | // virtual functions. |
| 1806 | if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor && |
| 1807 | SanOpts.has(SanitizerKind::Memory) && !ClassDecl->getNumVBases() && |
| 1808 | ClassDecl->isPolymorphic()) |
| 1809 | EHStack.pushCleanup<SanitizeDtorVTable>(NormalAndEHCleanup, DD); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 1810 | |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1811 | // Destroy non-virtual bases. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 1812 | for (const auto &Base : ClassDecl->bases()) { |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1813 | // Ignore virtual bases. |
| 1814 | if (Base.isVirtual()) |
| 1815 | continue; |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 1816 | |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1817 | CXXRecordDecl *BaseClassDecl = Base.getType()->getAsCXXRecordDecl(); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 1818 | |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1819 | // Ignore trivial destructors. |
| 1820 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1821 | continue; |
John McCall | 5c60a6f | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1822 | |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1823 | EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup, |
| 1824 | BaseClassDecl, |
| 1825 | /*BaseIsVirtual*/ false); |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
Naomi Musgrave | 866af2d | 2015-09-03 23:02:30 +0000 | [diff] [blame] | 1828 | // Poison fields such that access after their destructors are |
| 1829 | // invoked, and before the base class destructor runs, is invalid. |
| 1830 | if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor && |
| 1831 | SanOpts.has(SanitizerKind::Memory)) |
Naomi Musgrave | 703835c | 2015-09-16 00:38:22 +0000 | [diff] [blame] | 1832 | EHStack.pushCleanup<SanitizeDtorMembers>(NormalAndEHCleanup, DD); |
Naomi Musgrave | 866af2d | 2015-09-03 23:02:30 +0000 | [diff] [blame] | 1833 | |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1834 | // Destroy direct fields. |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1835 | for (const auto *Field : ClassDecl->fields()) { |
| 1836 | QualType type = Field->getType(); |
John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1837 | QualType::DestructionKind dtorKind = type.isDestructedType(); |
| 1838 | if (!dtorKind) continue; |
John McCall | f99a631 | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1839 | |
Richard Smith | 921bd20 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 1840 | // Anonymous union members do not have their destructors called. |
| 1841 | const RecordType *RT = type->getAsUnionType(); |
| 1842 | if (RT && RT->getDecl()->isAnonymousStructOrUnion()) continue; |
| 1843 | |
John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1844 | CleanupKind cleanupKind = getCleanupKind(dtorKind); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1845 | EHStack.pushCleanup<DestroyField>(cleanupKind, Field, |
John McCall | 4bd0fb1 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1846 | getDestroyer(dtorKind), |
| 1847 | cleanupKind & EHCleanup); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1848 | } |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1851 | /// EmitCXXAggrConstructorCall - Emit a loop to call a particular |
| 1852 | /// constructor for each of several members of an array. |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 1853 | /// |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1854 | /// \param ctor the constructor to call for each element |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1855 | /// \param arrayType the type of the array to initialize |
| 1856 | /// \param arrayBegin an arrayType* |
| 1857 | /// \param zeroInitialize true if each element should be |
| 1858 | /// zero-initialized before it is constructed |
Alexey Samsonov | 70b9c01 | 2014-08-21 20:26:47 +0000 | [diff] [blame] | 1859 | void CodeGenFunction::EmitCXXAggrConstructorCall( |
Alexey Bataev | e7545b3 | 2016-04-29 09:39:50 +0000 | [diff] [blame] | 1860 | const CXXConstructorDecl *ctor, const ArrayType *arrayType, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1861 | Address arrayBegin, const CXXConstructExpr *E, bool zeroInitialize) { |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1862 | QualType elementType; |
| 1863 | llvm::Value *numElements = |
| 1864 | emitArrayLength(arrayType, elementType, arrayBegin); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1865 | |
Alexey Samsonov | 70b9c01 | 2014-08-21 20:26:47 +0000 | [diff] [blame] | 1866 | EmitCXXAggrConstructorCall(ctor, numElements, arrayBegin, E, zeroInitialize); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1869 | /// EmitCXXAggrConstructorCall - Emit a loop to call a particular |
| 1870 | /// constructor for each of several members of an array. |
| 1871 | /// |
| 1872 | /// \param ctor the constructor to call for each element |
| 1873 | /// \param numElements the number of elements in the array; |
John McCall | 6549b31 | 2011-07-13 07:37:11 +0000 | [diff] [blame] | 1874 | /// may be zero |
NAKAMURA Takumi | ff7a925 | 2015-09-08 09:42:41 +0000 | [diff] [blame] | 1875 | /// \param arrayBase a T*, where T is the type constructed by ctor |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1876 | /// \param zeroInitialize true if each element should be |
| 1877 | /// zero-initialized before it is constructed |
Alexey Samsonov | 70b9c01 | 2014-08-21 20:26:47 +0000 | [diff] [blame] | 1878 | void CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *ctor, |
| 1879 | llvm::Value *numElements, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1880 | Address arrayBase, |
Alexey Samsonov | 70b9c01 | 2014-08-21 20:26:47 +0000 | [diff] [blame] | 1881 | const CXXConstructExpr *E, |
| 1882 | bool zeroInitialize) { |
John McCall | 6549b31 | 2011-07-13 07:37:11 +0000 | [diff] [blame] | 1883 | // It's legal for numElements to be zero. This can happen both |
| 1884 | // dynamically, because x can be zero in 'new A[x]', and statically, |
| 1885 | // because of GCC extensions that permit zero-length arrays. There |
| 1886 | // are probably legitimate places where we could assume that this |
| 1887 | // doesn't happen, but it's not clear that it's worth it. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1888 | llvm::BranchInst *zeroCheckBranch = nullptr; |
John McCall | 6549b31 | 2011-07-13 07:37:11 +0000 | [diff] [blame] | 1889 | |
| 1890 | // Optimize for a constant count. |
| 1891 | llvm::ConstantInt *constantCount |
| 1892 | = dyn_cast<llvm::ConstantInt>(numElements); |
| 1893 | if (constantCount) { |
| 1894 | // Just skip out if the constant count is zero. |
| 1895 | if (constantCount->isZero()) return; |
| 1896 | |
| 1897 | // Otherwise, emit the check. |
| 1898 | } else { |
| 1899 | llvm::BasicBlock *loopBB = createBasicBlock("new.ctorloop"); |
| 1900 | llvm::Value *iszero = Builder.CreateIsNull(numElements, "isempty"); |
| 1901 | zeroCheckBranch = Builder.CreateCondBr(iszero, loopBB, loopBB); |
| 1902 | EmitBlock(loopBB); |
| 1903 | } |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 1904 | |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1905 | // Find the end of the array. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1906 | llvm::Value *arrayBegin = arrayBase.getPointer(); |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1907 | llvm::Value *arrayEnd = Builder.CreateInBoundsGEP(arrayBegin, numElements, |
| 1908 | "arrayctor.end"); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1909 | |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1910 | // Enter the loop, setting up a phi for the current location to initialize. |
| 1911 | llvm::BasicBlock *entryBB = Builder.GetInsertBlock(); |
| 1912 | llvm::BasicBlock *loopBB = createBasicBlock("arrayctor.loop"); |
| 1913 | EmitBlock(loopBB); |
| 1914 | llvm::PHINode *cur = Builder.CreatePHI(arrayBegin->getType(), 2, |
| 1915 | "arrayctor.cur"); |
| 1916 | cur->addIncoming(arrayBegin, entryBB); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1917 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1918 | // Inside the loop body, emit the constructor call on the array element. |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1919 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1920 | // The alignment of the base, adjusted by the size of a single element, |
| 1921 | // provides a conservative estimate of the alignment of every element. |
| 1922 | // (This assumes we never start tracking offsetted alignments.) |
| 1923 | // |
| 1924 | // Note that these are complete objects and so we don't need to |
| 1925 | // use the non-virtual size or alignment. |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1926 | QualType type = getContext().getTypeDeclType(ctor->getParent()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1927 | CharUnits eltAlignment = |
| 1928 | arrayBase.getAlignment() |
| 1929 | .alignmentOfArrayElement(getContext().getTypeSizeInChars(type)); |
| 1930 | Address curAddr = Address(cur, eltAlignment); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1931 | |
Douglas Gregor | 05fc5be | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 1932 | // Zero initialize the storage, if requested. |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1933 | if (zeroInitialize) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1934 | EmitNullInitialization(curAddr, type); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 1935 | |
| 1936 | // C++ [class.temporary]p4: |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1937 | // There are two contexts in which temporaries are destroyed at a different |
| 1938 | // point than the end of the full-expression. The first context is when a |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 1939 | // default constructor is called to initialize an element of an array. |
| 1940 | // If the constructor has one or more default arguments, the destruction of |
| 1941 | // every temporary created in a default argument expression is sequenced |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1942 | // before the construction of the next array element, if any. |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 1943 | |
Anders Carlsson | b9fd57f | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 1944 | { |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1945 | RunCleanupsScope Scope(*this); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1946 | |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1947 | // Evaluate the constructor and its arguments in a regular |
| 1948 | // partial-destroy cleanup. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1949 | if (getLangOpts().Exceptions && |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1950 | !ctor->getParent()->hasTrivialDestructor()) { |
| 1951 | Destroyer *destroyer = destroyCXXObject; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1952 | pushRegularPartialArrayCleanup(arrayBegin, cur, type, eltAlignment, |
| 1953 | *destroyer); |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
Alexey Samsonov | 70b9c01 | 2014-08-21 20:26:47 +0000 | [diff] [blame] | 1956 | EmitCXXConstructorCall(ctor, Ctor_Complete, /*ForVirtualBase=*/false, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1957 | /*Delegating=*/false, curAddr, E); |
Anders Carlsson | b9fd57f | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 1958 | } |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1959 | |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1960 | // Go to the next element. |
| 1961 | llvm::Value *next = |
| 1962 | Builder.CreateInBoundsGEP(cur, llvm::ConstantInt::get(SizeTy, 1), |
| 1963 | "arrayctor.next"); |
| 1964 | cur->addIncoming(next, Builder.GetInsertBlock()); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1965 | |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1966 | // Check whether that's the end of the loop. |
| 1967 | llvm::Value *done = Builder.CreateICmpEQ(next, arrayEnd, "arrayctor.done"); |
| 1968 | llvm::BasicBlock *contBB = createBasicBlock("arrayctor.cont"); |
| 1969 | Builder.CreateCondBr(done, contBB, loopBB); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1970 | |
John McCall | 6549b31 | 2011-07-13 07:37:11 +0000 | [diff] [blame] | 1971 | // Patch the earlier check to skip over the loop. |
| 1972 | if (zeroCheckBranch) zeroCheckBranch->setSuccessor(0, contBB); |
| 1973 | |
John McCall | f677a8e | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1974 | EmitBlock(contBB); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1977 | void CodeGenFunction::destroyCXXObject(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1978 | Address addr, |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1979 | QualType type) { |
| 1980 | const RecordType *rtype = type->castAs<RecordType>(); |
| 1981 | const CXXRecordDecl *record = cast<CXXRecordDecl>(rtype->getDecl()); |
| 1982 | const CXXDestructorDecl *dtor = record->getDestructor(); |
| 1983 | assert(!dtor->isTrivial()); |
| 1984 | CGF.EmitCXXDestructorCall(dtor, Dtor_Complete, /*for vbase*/ false, |
Douglas Gregor | 6153500 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 1985 | /*Delegating=*/false, addr); |
John McCall | 82fe67b | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
Alexey Samsonov | 70b9c01 | 2014-08-21 20:26:47 +0000 | [diff] [blame] | 1988 | void CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, |
| 1989 | CXXCtorType Type, |
| 1990 | bool ForVirtualBase, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1991 | bool Delegating, Address This, |
Alexey Samsonov | 70b9c01 | 2014-08-21 20:26:47 +0000 | [diff] [blame] | 1992 | const CXXConstructExpr *E) { |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 1993 | CallArgList Args; |
| 1994 | |
| 1995 | // Push the this ptr. |
| 1996 | Args.add(RValue::get(This.getPointer()), D->getThisType(getContext())); |
| 1997 | |
| 1998 | // If this is a trivial constructor, emit a memcpy now before we lose |
| 1999 | // the alignment information on the argument. |
| 2000 | // FIXME: It would be better to preserve alignment information into CallArg. |
| 2001 | if (isMemcpyEquivalentSpecialMember(D)) { |
| 2002 | assert(E->getNumArgs() == 1 && "unexpected argcount for trivial ctor"); |
| 2003 | |
| 2004 | const Expr *Arg = E->getArg(0); |
| 2005 | QualType SrcTy = Arg->getType(); |
| 2006 | Address Src = EmitLValue(Arg).getAddress(); |
| 2007 | QualType DestTy = getContext().getTypeDeclType(D->getParent()); |
| 2008 | EmitAggregateCopyCtor(This, Src, DestTy, SrcTy); |
| 2009 | return; |
| 2010 | } |
| 2011 | |
| 2012 | // Add the rest of the user-supplied arguments. |
| 2013 | const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>(); |
Hans Wennborg | 27dcc6c | 2017-02-01 02:21:07 +0000 | [diff] [blame] | 2014 | EvaluationOrder Order = E->isListInitialization() |
| 2015 | ? EvaluationOrder::ForceLeftToRight |
| 2016 | : EvaluationOrder::Default; |
| 2017 | EmitCallArgs(Args, FPT, E->arguments(), E->getConstructor(), |
| 2018 | /*ParamsToSkip*/ 0, Order); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2019 | |
| 2020 | EmitCXXConstructorCall(D, Type, ForVirtualBase, Delegating, This, Args); |
| 2021 | } |
| 2022 | |
| 2023 | static bool canEmitDelegateCallArgs(CodeGenFunction &CGF, |
| 2024 | const CXXConstructorDecl *Ctor, |
| 2025 | CXXCtorType Type, CallArgList &Args) { |
| 2026 | // We can't forward a variadic call. |
| 2027 | if (Ctor->isVariadic()) |
| 2028 | return false; |
| 2029 | |
| 2030 | if (CGF.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) { |
| 2031 | // If the parameters are callee-cleanup, it's not safe to forward. |
| 2032 | for (auto *P : Ctor->parameters()) |
| 2033 | if (P->getType().isDestructedType()) |
| 2034 | return false; |
| 2035 | |
| 2036 | // Likewise if they're inalloca. |
| 2037 | const CGFunctionInfo &Info = |
George Burgess IV | d0a9e80 | 2017-02-23 22:07:35 +0000 | [diff] [blame] | 2038 | CGF.CGM.getTypes().arrangeCXXConstructorCall(Args, Ctor, Type, 0, 0); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2039 | if (Info.usesInAlloca()) |
| 2040 | return false; |
| 2041 | } |
| 2042 | |
| 2043 | // Anything else should be OK. |
| 2044 | return true; |
| 2045 | } |
| 2046 | |
| 2047 | void CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, |
| 2048 | CXXCtorType Type, |
| 2049 | bool ForVirtualBase, |
| 2050 | bool Delegating, |
| 2051 | Address This, |
| 2052 | CallArgList &Args) { |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 2053 | const CXXRecordDecl *ClassDecl = D->getParent(); |
| 2054 | |
Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 2055 | // C++11 [class.mfct.non-static]p2: |
| 2056 | // If a non-static member function of a class X is called for an object that |
| 2057 | // is not of type X, or of a type derived from X, the behavior is undefined. |
| 2058 | // FIXME: Provide a source location here. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2059 | EmitTypeCheck(CodeGenFunction::TCK_ConstructorCall, SourceLocation(), |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 2060 | This.getPointer(), getContext().getRecordType(ClassDecl)); |
John McCall | ca972cd | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 2061 | |
Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 2062 | if (D->isTrivial() && D->isDefaultConstructor()) { |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2063 | assert(Args.size() == 1 && "trivial default ctor with args"); |
Richard Smith | 419bd09 | 2015-04-29 19:26:57 +0000 | [diff] [blame] | 2064 | return; |
| 2065 | } |
| 2066 | |
| 2067 | // If this is a trivial constructor, just emit what's needed. If this is a |
| 2068 | // union copy constructor, we must emit a memcpy, because the AST does not |
| 2069 | // model that copy. |
| 2070 | if (isMemcpyEquivalentSpecialMember(D)) { |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2071 | assert(Args.size() == 2 && "unexpected argcount for trivial ctor"); |
John McCall | ca972cd | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 2072 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2073 | QualType SrcTy = D->getParamDecl(0)->getType().getNonReferenceType(); |
| 2074 | Address Src(Args[1].RV.getScalarVal(), getNaturalTypeAlignment(SrcTy)); |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 2075 | QualType DestTy = getContext().getTypeDeclType(ClassDecl); |
David Majnemer | fd1e739 | 2015-02-03 23:04:06 +0000 | [diff] [blame] | 2076 | EmitAggregateCopyCtor(This, Src, DestTy, SrcTy); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 2077 | return; |
| 2078 | } |
| 2079 | |
George Burgess IV | d0a9e80 | 2017-02-23 22:07:35 +0000 | [diff] [blame] | 2080 | bool PassPrototypeArgs = true; |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2081 | // Check whether we can actually emit the constructor before trying to do so. |
| 2082 | if (auto Inherited = D->getInheritedConstructor()) { |
George Burgess IV | d0a9e80 | 2017-02-23 22:07:35 +0000 | [diff] [blame] | 2083 | PassPrototypeArgs = getTypes().inheritingCtorHasParams(Inherited, Type); |
| 2084 | if (PassPrototypeArgs && !canEmitDelegateCallArgs(*this, D, Type, Args)) { |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2085 | EmitInlinedInheritingCXXConstructorCall(D, Type, ForVirtualBase, |
| 2086 | Delegating, Args); |
| 2087 | return; |
| 2088 | } |
| 2089 | } |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 2090 | |
| 2091 | // Insert any ABI-specific implicit constructor arguments. |
George Burgess IV | f203dbf | 2017-02-22 20:28:02 +0000 | [diff] [blame] | 2092 | CGCXXABI::AddedStructorArgs ExtraArgs = |
| 2093 | CGM.getCXXABI().addImplicitConstructorArgs(*this, D, Type, ForVirtualBase, |
| 2094 | Delegating, Args); |
Reid Kleckner | 89077a1 | 2013-12-17 19:46:40 +0000 | [diff] [blame] | 2095 | |
| 2096 | // Emit the call. |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 2097 | llvm::Constant *CalleePtr = |
| 2098 | CGM.getAddrOfCXXStructor(D, getFromCtorType(Type)); |
George Burgess IV | f203dbf | 2017-02-22 20:28:02 +0000 | [diff] [blame] | 2099 | const CGFunctionInfo &Info = CGM.getTypes().arrangeCXXConstructorCall( |
George Burgess IV | d0a9e80 | 2017-02-23 22:07:35 +0000 | [diff] [blame] | 2100 | Args, D, Type, ExtraArgs.Prefix, ExtraArgs.Suffix, PassPrototypeArgs); |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 2101 | CGCallee Callee = CGCallee::forDirect(CalleePtr, D); |
| 2102 | EmitCall(Info, Callee, ReturnValueSlot(), Args); |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 2103 | |
| 2104 | // Generate vtable assumptions if we're constructing a complete object |
| 2105 | // with a vtable. We don't do this for base subobjects for two reasons: |
| 2106 | // first, it's incorrect for classes with virtual bases, and second, we're |
| 2107 | // about to overwrite the vptrs anyway. |
| 2108 | // We also have to make sure if we can refer to vtable: |
| 2109 | // - Otherwise we can refer to vtable if it's safe to speculatively emit. |
| 2110 | // FIXME: If vtable is used by ctor/dtor, or if vtable is external and we are |
| 2111 | // sure that definition of vtable is not hidden, |
| 2112 | // then we are always safe to refer to it. |
Piotr Padlewski | 69dc971 | 2015-09-28 20:30:22 +0000 | [diff] [blame] | 2113 | // FIXME: It looks like InstCombine is very inefficient on dealing with |
| 2114 | // assumes. Make assumption loads require -fstrict-vtable-pointers temporarily. |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 2115 | if (CGM.getCodeGenOpts().OptimizationLevel > 0 && |
| 2116 | ClassDecl->isDynamicClass() && Type != Ctor_Base && |
Piotr Padlewski | 69dc971 | 2015-09-28 20:30:22 +0000 | [diff] [blame] | 2117 | CGM.getCXXABI().canSpeculativelyEmitVTable(ClassDecl) && |
| 2118 | CGM.getCodeGenOpts().StrictVTablePointers) |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 2119 | EmitVTableAssumptionLoads(ClassDecl, This); |
| 2120 | } |
| 2121 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2122 | void CodeGenFunction::EmitInheritedCXXConstructorCall( |
| 2123 | const CXXConstructorDecl *D, bool ForVirtualBase, Address This, |
| 2124 | bool InheritedFromVBase, const CXXInheritedCtorInitExpr *E) { |
| 2125 | CallArgList Args; |
| 2126 | CallArg ThisArg(RValue::get(This.getPointer()), D->getThisType(getContext()), |
| 2127 | /*NeedsCopy=*/false); |
| 2128 | |
| 2129 | // Forward the parameters. |
| 2130 | if (InheritedFromVBase && |
| 2131 | CGM.getTarget().getCXXABI().hasConstructorVariants()) { |
| 2132 | // Nothing to do; this construction is not responsible for constructing |
| 2133 | // the base class containing the inherited constructor. |
| 2134 | // FIXME: Can we just pass undef's for the remaining arguments if we don't |
| 2135 | // have constructor variants? |
| 2136 | Args.push_back(ThisArg); |
| 2137 | } else if (!CXXInheritedCtorInitExprArgs.empty()) { |
| 2138 | // The inheriting constructor was inlined; just inject its arguments. |
| 2139 | assert(CXXInheritedCtorInitExprArgs.size() >= D->getNumParams() && |
| 2140 | "wrong number of parameters for inherited constructor call"); |
| 2141 | Args = CXXInheritedCtorInitExprArgs; |
| 2142 | Args[0] = ThisArg; |
| 2143 | } else { |
| 2144 | // The inheriting constructor was not inlined. Emit delegating arguments. |
| 2145 | Args.push_back(ThisArg); |
| 2146 | const auto *OuterCtor = cast<CXXConstructorDecl>(CurCodeDecl); |
| 2147 | assert(OuterCtor->getNumParams() == D->getNumParams()); |
| 2148 | assert(!OuterCtor->isVariadic() && "should have been inlined"); |
| 2149 | |
| 2150 | for (const auto *Param : OuterCtor->parameters()) { |
| 2151 | assert(getContext().hasSameUnqualifiedType( |
| 2152 | OuterCtor->getParamDecl(Param->getFunctionScopeIndex())->getType(), |
| 2153 | Param->getType())); |
| 2154 | EmitDelegateCallArg(Args, Param, E->getLocation()); |
| 2155 | |
| 2156 | // Forward __attribute__(pass_object_size). |
| 2157 | if (Param->hasAttr<PassObjectSizeAttr>()) { |
| 2158 | auto *POSParam = SizeArguments[Param]; |
| 2159 | assert(POSParam && "missing pass_object_size value for forwarding"); |
| 2160 | EmitDelegateCallArg(Args, POSParam, E->getLocation()); |
| 2161 | } |
| 2162 | } |
| 2163 | } |
| 2164 | |
| 2165 | EmitCXXConstructorCall(D, Ctor_Base, ForVirtualBase, /*Delegating*/false, |
| 2166 | This, Args); |
| 2167 | } |
| 2168 | |
| 2169 | void CodeGenFunction::EmitInlinedInheritingCXXConstructorCall( |
| 2170 | const CXXConstructorDecl *Ctor, CXXCtorType CtorType, bool ForVirtualBase, |
| 2171 | bool Delegating, CallArgList &Args) { |
Adrian Prantl | b7acfc0 | 2017-02-27 21:30:05 +0000 | [diff] [blame] | 2172 | GlobalDecl GD(Ctor, CtorType); |
| 2173 | InlinedInheritingConstructorScope Scope(*this, GD); |
| 2174 | ApplyInlineDebugLocation DebugScope(*this, GD); |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2175 | |
| 2176 | // Save the arguments to be passed to the inherited constructor. |
| 2177 | CXXInheritedCtorInitExprArgs = Args; |
| 2178 | |
| 2179 | FunctionArgList Params; |
| 2180 | QualType RetType = BuildFunctionArgList(CurGD, Params); |
| 2181 | FnRetTy = RetType; |
| 2182 | |
| 2183 | // Insert any ABI-specific implicit constructor arguments. |
| 2184 | CGM.getCXXABI().addImplicitConstructorArgs(*this, Ctor, CtorType, |
| 2185 | ForVirtualBase, Delegating, Args); |
| 2186 | |
| 2187 | // Emit a simplified prolog. We only need to emit the implicit params. |
| 2188 | assert(Args.size() >= Params.size() && "too few arguments for call"); |
| 2189 | for (unsigned I = 0, N = Args.size(); I != N; ++I) { |
| 2190 | if (I < Params.size() && isa<ImplicitParamDecl>(Params[I])) { |
| 2191 | const RValue &RV = Args[I].RV; |
| 2192 | assert(!RV.isComplex() && "complex indirect params not supported"); |
| 2193 | ParamValue Val = RV.isScalar() |
| 2194 | ? ParamValue::forDirect(RV.getScalarVal()) |
| 2195 | : ParamValue::forIndirect(RV.getAggregateAddress()); |
| 2196 | EmitParmDecl(*Params[I], Val, I + 1); |
| 2197 | } |
| 2198 | } |
| 2199 | |
| 2200 | // Create a return value slot if the ABI implementation wants one. |
| 2201 | // FIXME: This is dumb, we should ask the ABI not to try to set the return |
| 2202 | // value instead. |
| 2203 | if (!RetType->isVoidType()) |
| 2204 | ReturnValue = CreateIRTemp(RetType, "retval.inhctor"); |
| 2205 | |
| 2206 | CGM.getCXXABI().EmitInstanceFunctionProlog(*this); |
| 2207 | CXXThisValue = CXXABIThisValue; |
| 2208 | |
| 2209 | // Directly emit the constructor initializers. |
| 2210 | EmitCtorPrologue(Ctor, CtorType, Params); |
| 2211 | } |
| 2212 | |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 2213 | void CodeGenFunction::EmitVTableAssumptionLoad(const VPtr &Vptr, Address This) { |
| 2214 | llvm::Value *VTableGlobal = |
| 2215 | CGM.getCXXABI().getVTableAddressPoint(Vptr.Base, Vptr.VTableClass); |
| 2216 | if (!VTableGlobal) |
| 2217 | return; |
| 2218 | |
| 2219 | // We can just use the base offset in the complete class. |
| 2220 | CharUnits NonVirtualOffset = Vptr.Base.getBaseOffset(); |
| 2221 | |
| 2222 | if (!NonVirtualOffset.isZero()) |
| 2223 | This = |
| 2224 | ApplyNonVirtualAndVirtualOffset(*this, This, NonVirtualOffset, nullptr, |
| 2225 | Vptr.VTableClass, Vptr.NearestVBase); |
| 2226 | |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 2227 | llvm::Value *VPtrValue = |
| 2228 | GetVTablePtr(This, VTableGlobal->getType(), Vptr.VTableClass); |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 2229 | llvm::Value *Cmp = |
| 2230 | Builder.CreateICmpEQ(VPtrValue, VTableGlobal, "cmp.vtables"); |
| 2231 | Builder.CreateAssumption(Cmp); |
| 2232 | } |
| 2233 | |
| 2234 | void CodeGenFunction::EmitVTableAssumptionLoads(const CXXRecordDecl *ClassDecl, |
| 2235 | Address This) { |
| 2236 | if (CGM.getCXXABI().doStructorsInitializeVPtrs(ClassDecl)) |
| 2237 | for (const VPtr &Vptr : getVTablePointers(ClassDecl)) |
| 2238 | EmitVTableAssumptionLoad(Vptr, This); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 2241 | void |
Fariborz Jahanian | e988bda | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 2242 | CodeGenFunction::EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2243 | Address This, Address Src, |
| 2244 | const CXXConstructExpr *E) { |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 2245 | const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>(); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 2246 | |
Fariborz Jahanian | e988bda | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 2247 | CallArgList Args; |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 2248 | |
Fariborz Jahanian | e988bda | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 2249 | // Push the this ptr. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2250 | Args.add(RValue::get(This.getPointer()), D->getThisType(getContext())); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 2251 | |
Fariborz Jahanian | e988bda | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 2252 | // Push the src ptr. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2253 | QualType QT = *(FPT->param_type_begin()); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2254 | llvm::Type *t = CGM.getTypes().ConvertType(QT); |
Fariborz Jahanian | e988bda | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 2255 | Src = Builder.CreateBitCast(Src, t); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2256 | Args.add(RValue::get(Src.getPointer()), QT); |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 2257 | |
Fariborz Jahanian | e988bda | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 2258 | // Skip over first argument (Src). |
David Blaikie | f05779e | 2015-07-21 18:37:18 +0000 | [diff] [blame] | 2259 | EmitCallArgs(Args, FPT, drop_begin(E->arguments(), 1), E->getConstructor(), |
Alexey Samsonov | 8e1162c | 2014-09-08 17:22:45 +0000 | [diff] [blame] | 2260 | /*ParamsToSkip*/ 1); |
Reid Kleckner | 739756c | 2013-12-04 19:23:12 +0000 | [diff] [blame] | 2261 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2262 | EmitCXXConstructorCall(D, Ctor_Complete, false, false, This, Args); |
Fariborz Jahanian | e988bda | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 2263 | } |
| 2264 | |
| 2265 | void |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 2266 | CodeGenFunction::EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor, |
| 2267 | CXXCtorType CtorType, |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2268 | const FunctionArgList &Args, |
| 2269 | SourceLocation Loc) { |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 2270 | CallArgList DelegateArgs; |
| 2271 | |
| 2272 | FunctionArgList::const_iterator I = Args.begin(), E = Args.end(); |
| 2273 | assert(I != E && "no parameters to constructor"); |
| 2274 | |
| 2275 | // this |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2276 | Address This = LoadCXXThisAddress(); |
| 2277 | DelegateArgs.add(RValue::get(This.getPointer()), (*I)->getType()); |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 2278 | ++I; |
| 2279 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2280 | // FIXME: The location of the VTT parameter in the parameter list is |
| 2281 | // specific to the Itanium ABI and shouldn't be hardcoded here. |
| 2282 | if (CGM.getCXXABI().NeedsVTTParameter(CurGD)) { |
| 2283 | assert(I != E && "cannot skip vtt parameter, already done with args"); |
| 2284 | assert((*I)->getType()->isPointerType() && |
| 2285 | "skipping parameter not of vtt type"); |
| 2286 | ++I; |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
| 2289 | // Explicit arguments. |
| 2290 | for (; I != E; ++I) { |
John McCall | 32ea969 | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 2291 | const VarDecl *param = *I; |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2292 | // FIXME: per-argument source location |
| 2293 | EmitDelegateCallArg(DelegateArgs, param, Loc); |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
Richard Smith | 5179eb7 | 2016-06-28 19:03:57 +0000 | [diff] [blame] | 2296 | EmitCXXConstructorCall(Ctor, CtorType, /*ForVirtualBase=*/false, |
| 2297 | /*Delegating=*/true, This, DelegateArgs); |
John McCall | f8ff7b9 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 2298 | } |
| 2299 | |
Alexis Hunt | 9d47faf | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 2300 | namespace { |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 2301 | struct CallDelegatingCtorDtor final : EHScopeStack::Cleanup { |
Alexis Hunt | 9d47faf | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 2302 | const CXXDestructorDecl *Dtor; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2303 | Address Addr; |
Alexis Hunt | 9d47faf | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 2304 | CXXDtorType Type; |
| 2305 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2306 | CallDelegatingCtorDtor(const CXXDestructorDecl *D, Address Addr, |
Alexis Hunt | 9d47faf | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 2307 | CXXDtorType Type) |
| 2308 | : Dtor(D), Addr(Addr), Type(Type) {} |
| 2309 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2310 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
Alexis Hunt | 9d47faf | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 2311 | CGF.EmitCXXDestructorCall(Dtor, Type, /*ForVirtualBase=*/false, |
Douglas Gregor | 6153500 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 2312 | /*Delegating=*/true, Addr); |
Alexis Hunt | 9d47faf | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 2313 | } |
| 2314 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 2315 | } // end anonymous namespace |
Alexis Hunt | 9d47faf | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 2316 | |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2317 | void |
| 2318 | CodeGenFunction::EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor, |
| 2319 | const FunctionArgList &Args) { |
| 2320 | assert(Ctor->isDelegatingConstructor()); |
| 2321 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2322 | Address ThisPtr = LoadCXXThisAddress(); |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2323 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2324 | AggValueSlot AggSlot = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2325 | AggValueSlot::forAddr(ThisPtr, Qualifiers(), |
John McCall | 8d6fc95 | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 2326 | AggValueSlot::IsDestructed, |
John McCall | a5efa73 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 2327 | AggValueSlot::DoesNotNeedGCBarriers, |
Chad Rosier | 615ed1a | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 2328 | AggValueSlot::IsNotAliased); |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2329 | |
| 2330 | EmitAggExpr(Ctor->init_begin()[0]->getInit(), AggSlot); |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2331 | |
Alexis Hunt | 9d47faf | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 2332 | const CXXRecordDecl *ClassDecl = Ctor->getParent(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2333 | if (CGM.getLangOpts().Exceptions && !ClassDecl->hasTrivialDestructor()) { |
Alexis Hunt | 9d47faf | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 2334 | CXXDtorType Type = |
| 2335 | CurGD.getCtorType() == Ctor_Complete ? Dtor_Complete : Dtor_Base; |
| 2336 | |
| 2337 | EHStack.pushCleanup<CallDelegatingCtorDtor>(EHCleanup, |
| 2338 | ClassDecl->getDestructor(), |
| 2339 | ThisPtr, Type); |
| 2340 | } |
| 2341 | } |
Alexis Hunt | 61bc173 | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2342 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 2343 | void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *DD, |
| 2344 | CXXDtorType Type, |
Anders Carlsson | f8a71f0 | 2010-05-02 23:29:11 +0000 | [diff] [blame] | 2345 | bool ForVirtualBase, |
Douglas Gregor | 6153500 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 2346 | bool Delegating, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2347 | Address This) { |
Reid Kleckner | 6fe771a | 2013-12-13 00:53:54 +0000 | [diff] [blame] | 2348 | CGM.getCXXABI().EmitDestructorCall(*this, DD, Type, ForVirtualBase, |
| 2349 | Delegating, This); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 2350 | } |
| 2351 | |
John McCall | 53cad2e | 2010-07-21 01:41:18 +0000 | [diff] [blame] | 2352 | namespace { |
David Blaikie | 7e70d68 | 2015-08-18 22:40:54 +0000 | [diff] [blame] | 2353 | struct CallLocalDtor final : EHScopeStack::Cleanup { |
John McCall | 53cad2e | 2010-07-21 01:41:18 +0000 | [diff] [blame] | 2354 | const CXXDestructorDecl *Dtor; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2355 | Address Addr; |
John McCall | 53cad2e | 2010-07-21 01:41:18 +0000 | [diff] [blame] | 2356 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2357 | CallLocalDtor(const CXXDestructorDecl *D, Address Addr) |
John McCall | 53cad2e | 2010-07-21 01:41:18 +0000 | [diff] [blame] | 2358 | : Dtor(D), Addr(Addr) {} |
| 2359 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2360 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
John McCall | 53cad2e | 2010-07-21 01:41:18 +0000 | [diff] [blame] | 2361 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, |
Douglas Gregor | 6153500 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 2362 | /*ForVirtualBase=*/false, |
| 2363 | /*Delegating=*/false, Addr); |
John McCall | 53cad2e | 2010-07-21 01:41:18 +0000 | [diff] [blame] | 2364 | } |
| 2365 | }; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 2366 | } // end anonymous namespace |
John McCall | 53cad2e | 2010-07-21 01:41:18 +0000 | [diff] [blame] | 2367 | |
John McCall | 8680f87 | 2010-07-21 06:29:51 +0000 | [diff] [blame] | 2368 | void CodeGenFunction::PushDestructorCleanup(const CXXDestructorDecl *D, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2369 | Address Addr) { |
John McCall | cda666c | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 2370 | EHStack.pushCleanup<CallLocalDtor>(NormalAndEHCleanup, D, Addr); |
John McCall | 8680f87 | 2010-07-21 06:29:51 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2373 | void CodeGenFunction::PushDestructorCleanup(QualType T, Address Addr) { |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2374 | CXXRecordDecl *ClassDecl = T->getAsCXXRecordDecl(); |
| 2375 | if (!ClassDecl) return; |
| 2376 | if (ClassDecl->hasTrivialDestructor()) return; |
| 2377 | |
| 2378 | const CXXDestructorDecl *D = ClassDecl->getDestructor(); |
John McCall | a85af56 | 2011-04-28 02:15:35 +0000 | [diff] [blame] | 2379 | assert(D && D->isUsed() && "destructor not marked as used!"); |
John McCall | 8680f87 | 2010-07-21 06:29:51 +0000 | [diff] [blame] | 2380 | PushDestructorCleanup(D, Addr); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 2383 | void CodeGenFunction::InitializeVTablePointer(const VPtr &Vptr) { |
Anders Carlsson | e87fae9 | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 2384 | // Compute the address point. |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 2385 | llvm::Value *VTableAddressPoint = |
| 2386 | CGM.getCXXABI().getVTableAddressPointInStructor( |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 2387 | *this, Vptr.VTableClass, Vptr.Base, Vptr.NearestVBase); |
| 2388 | |
Timur Iskhodzhanov | 8b5987e | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 2389 | if (!VTableAddressPoint) |
| 2390 | return; |
Anders Carlsson | e87fae9 | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 2391 | |
Anders Carlsson | 6a0227d | 2010-04-20 16:22:16 +0000 | [diff] [blame] | 2392 | // Compute where to store the address point. |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2393 | llvm::Value *VirtualOffset = nullptr; |
Ken Dyck | cfc332c | 2011-03-23 00:45:26 +0000 | [diff] [blame] | 2394 | CharUnits NonVirtualOffset = CharUnits::Zero(); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 2395 | |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 2396 | if (CGM.getCXXABI().isVirtualOffsetNeededForVTableField(*this, Vptr)) { |
Anders Carlsson | 91baecf | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 2397 | // We need to use the virtual base offset offset because the virtual base |
| 2398 | // might have a different offset in the most derived class. |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 2399 | |
| 2400 | VirtualOffset = CGM.getCXXABI().GetVirtualBaseClassOffset( |
| 2401 | *this, LoadCXXThisAddress(), Vptr.VTableClass, Vptr.NearestVBase); |
| 2402 | NonVirtualOffset = Vptr.OffsetFromNearestVBase; |
Anders Carlsson | 91baecf | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 2403 | } else { |
Anders Carlsson | c58fb55 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 2404 | // We can just use the base offset in the complete class. |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 2405 | NonVirtualOffset = Vptr.Base.getBaseOffset(); |
Anders Carlsson | 91baecf | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 2406 | } |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 2407 | |
Anders Carlsson | c58fb55 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 2408 | // Apply the offsets. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2409 | Address VTableField = LoadCXXThisAddress(); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 2410 | |
Ken Dyck | cfc332c | 2011-03-23 00:45:26 +0000 | [diff] [blame] | 2411 | if (!NonVirtualOffset.isZero() || VirtualOffset) |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 2412 | VTableField = ApplyNonVirtualAndVirtualOffset( |
| 2413 | *this, VTableField, NonVirtualOffset, VirtualOffset, Vptr.VTableClass, |
| 2414 | Vptr.NearestVBase); |
Anders Carlsson | 6a0227d | 2010-04-20 16:22:16 +0000 | [diff] [blame] | 2415 | |
Reid Kleckner | 8d58513 | 2014-12-03 21:00:21 +0000 | [diff] [blame] | 2416 | // Finally, store the address point. Use the same LLVM types as the field to |
| 2417 | // support optimization. |
| 2418 | llvm::Type *VTablePtrTy = |
| 2419 | llvm::FunctionType::get(CGM.Int32Ty, /*isVarArg=*/true) |
| 2420 | ->getPointerTo() |
| 2421 | ->getPointerTo(); |
| 2422 | VTableField = Builder.CreateBitCast(VTableField, VTablePtrTy->getPointerTo()); |
| 2423 | VTableAddressPoint = Builder.CreateBitCast(VTableAddressPoint, VTablePtrTy); |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 2424 | |
Kostya Serebryany | 141e46f | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 2425 | llvm::StoreInst *Store = Builder.CreateStore(VTableAddressPoint, VTableField); |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 2426 | TBAAAccessInfo TBAAInfo = CGM.getTBAAVTablePtrAccessInfo(VTablePtrTy); |
| 2427 | CGM.DecorateInstructionWithTBAA(Store, TBAAInfo); |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 2428 | if (CGM.getCodeGenOpts().OptimizationLevel > 0 && |
| 2429 | CGM.getCodeGenOpts().StrictVTablePointers) |
| 2430 | CGM.DecorateInstructionWithInvariantGroup(Store, Vptr.VTableClass); |
Anders Carlsson | e87fae9 | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 2431 | } |
| 2432 | |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 2433 | CodeGenFunction::VPtrsVector |
| 2434 | CodeGenFunction::getVTablePointers(const CXXRecordDecl *VTableClass) { |
| 2435 | CodeGenFunction::VPtrsVector VPtrsResult; |
| 2436 | VisitedVirtualBasesSetTy VBases; |
| 2437 | getVTablePointers(BaseSubobject(VTableClass, CharUnits::Zero()), |
| 2438 | /*NearestVBase=*/nullptr, |
| 2439 | /*OffsetFromNearestVBase=*/CharUnits::Zero(), |
| 2440 | /*BaseIsNonVirtualPrimaryBase=*/false, VTableClass, VBases, |
| 2441 | VPtrsResult); |
| 2442 | return VPtrsResult; |
| 2443 | } |
| 2444 | |
| 2445 | void CodeGenFunction::getVTablePointers(BaseSubobject Base, |
| 2446 | const CXXRecordDecl *NearestVBase, |
| 2447 | CharUnits OffsetFromNearestVBase, |
| 2448 | bool BaseIsNonVirtualPrimaryBase, |
| 2449 | const CXXRecordDecl *VTableClass, |
| 2450 | VisitedVirtualBasesSetTy &VBases, |
| 2451 | VPtrsVector &Vptrs) { |
Anders Carlsson | d589593 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2452 | // If this base is a non-virtual primary base the address point has already |
| 2453 | // been set. |
| 2454 | if (!BaseIsNonVirtualPrimaryBase) { |
| 2455 | // Initialize the vtable pointer for this base. |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 2456 | VPtr Vptr = {Base, NearestVBase, OffsetFromNearestVBase, VTableClass}; |
| 2457 | Vptrs.push_back(Vptr); |
Anders Carlsson | d589593 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2458 | } |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 2459 | |
Anders Carlsson | d589593 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2460 | const CXXRecordDecl *RD = Base.getBase(); |
| 2461 | |
| 2462 | // Traverse bases. |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2463 | for (const auto &I : RD->bases()) { |
Anders Carlsson | d589593 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2464 | CXXRecordDecl *BaseDecl |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2465 | = cast<CXXRecordDecl>(I.getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | d589593 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2466 | |
| 2467 | // Ignore classes without a vtable. |
| 2468 | if (!BaseDecl->isDynamicClass()) |
| 2469 | continue; |
| 2470 | |
Ken Dyck | 3fb4c89 | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 2471 | CharUnits BaseOffset; |
| 2472 | CharUnits BaseOffsetFromNearestVBase; |
Anders Carlsson | 948d3f4 | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 2473 | bool BaseDeclIsNonVirtualPrimaryBase; |
Anders Carlsson | d589593 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2474 | |
Aaron Ballman | 574705e | 2014-03-13 15:41:46 +0000 | [diff] [blame] | 2475 | if (I.isVirtual()) { |
Anders Carlsson | d589593 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2476 | // Check if we've visited this virtual base before. |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 2477 | if (!VBases.insert(BaseDecl).second) |
Anders Carlsson | d589593 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2478 | continue; |
| 2479 | |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 2480 | const ASTRecordLayout &Layout = |
Anders Carlsson | d589593 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2481 | getContext().getASTRecordLayout(VTableClass); |
| 2482 | |
Ken Dyck | 3fb4c89 | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 2483 | BaseOffset = Layout.getVBaseClassOffset(BaseDecl); |
| 2484 | BaseOffsetFromNearestVBase = CharUnits::Zero(); |
Anders Carlsson | 948d3f4 | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 2485 | BaseDeclIsNonVirtualPrimaryBase = false; |
Anders Carlsson | d589593 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2486 | } else { |
| 2487 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 2488 | |
Ken Dyck | 16ffcac | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 2489 | BaseOffset = Base.getBaseOffset() + Layout.getBaseClassOffset(BaseDecl); |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 2490 | BaseOffsetFromNearestVBase = |
Ken Dyck | 3fb4c89 | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 2491 | OffsetFromNearestVBase + Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | 948d3f4 | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 2492 | BaseDeclIsNonVirtualPrimaryBase = Layout.getPrimaryBase() == BaseDecl; |
Anders Carlsson | d589593 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2493 | } |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 2494 | |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 2495 | getVTablePointers( |
| 2496 | BaseSubobject(BaseDecl, BaseOffset), |
| 2497 | I.isVirtual() ? BaseDecl : NearestVBase, BaseOffsetFromNearestVBase, |
| 2498 | BaseDeclIsNonVirtualPrimaryBase, VTableClass, VBases, Vptrs); |
Anders Carlsson | d589593 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2499 | } |
| 2500 | } |
| 2501 | |
| 2502 | void CodeGenFunction::InitializeVTablePointers(const CXXRecordDecl *RD) { |
| 2503 | // Ignore classes without a vtable. |
Anders Carlsson | 1f9348c | 2010-03-26 04:39:42 +0000 | [diff] [blame] | 2504 | if (!RD->isDynamicClass()) |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 2505 | return; |
| 2506 | |
Anders Carlsson | d589593 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 2507 | // Initialize the vtable pointers for this class and all of its bases. |
Piotr Padlewski | d679d7e | 2015-09-15 00:37:06 +0000 | [diff] [blame] | 2508 | if (CGM.getCXXABI().doStructorsInitializeVPtrs(RD)) |
| 2509 | for (const VPtr &Vptr : getVTablePointers(RD)) |
| 2510 | InitializeVTablePointer(Vptr); |
Timur Iskhodzhanov | b648732 | 2013-10-09 18:16:58 +0000 | [diff] [blame] | 2511 | |
| 2512 | if (RD->getNumVBases()) |
| 2513 | CGM.getCXXABI().initializeHiddenVirtualInheritanceMembers(*this, RD); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 2514 | } |
Dan Gohman | 8fc50c2 | 2010-10-26 18:44:08 +0000 | [diff] [blame] | 2515 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2516 | llvm::Value *CodeGenFunction::GetVTablePtr(Address This, |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 2517 | llvm::Type *VTableTy, |
| 2518 | const CXXRecordDecl *RD) { |
| 2519 | Address VTablePtrSrc = Builder.CreateElementBitCast(This, VTableTy); |
Kostya Serebryany | 141e46f | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 2520 | llvm::Instruction *VTable = Builder.CreateLoad(VTablePtrSrc, "vtable"); |
Ivan A. Kosarev | 4e50e70 | 2017-11-27 09:39:29 +0000 | [diff] [blame] | 2521 | TBAAAccessInfo TBAAInfo = CGM.getTBAAVTablePtrAccessInfo(VTableTy); |
| 2522 | CGM.DecorateInstructionWithTBAA(VTable, TBAAInfo); |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 2523 | |
| 2524 | if (CGM.getCodeGenOpts().OptimizationLevel > 0 && |
| 2525 | CGM.getCodeGenOpts().StrictVTablePointers) |
| 2526 | CGM.DecorateInstructionWithInvariantGroup(VTable, RD); |
| 2527 | |
Kostya Serebryany | 141e46f | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 2528 | return VTable; |
Dan Gohman | 8fc50c2 | 2010-10-26 18:44:08 +0000 | [diff] [blame] | 2529 | } |
Anders Carlsson | c36783e | 2011-05-08 20:32:23 +0000 | [diff] [blame] | 2530 | |
Peter Collingbourne | d2926c9 | 2015-03-14 02:42:25 +0000 | [diff] [blame] | 2531 | // If a class has a single non-virtual base and does not introduce or override |
| 2532 | // virtual member functions or fields, it will have the same layout as its base. |
| 2533 | // This function returns the least derived such class. |
| 2534 | // |
| 2535 | // Casting an instance of a base class to such a derived class is technically |
| 2536 | // undefined behavior, but it is a relatively common hack for introducing member |
| 2537 | // functions on class instances with specific properties (e.g. llvm::Operator) |
| 2538 | // that works under most compilers and should not have security implications, so |
| 2539 | // we allow it by default. It can be disabled with -fsanitize=cfi-cast-strict. |
| 2540 | static const CXXRecordDecl * |
| 2541 | LeastDerivedClassWithSameLayout(const CXXRecordDecl *RD) { |
| 2542 | if (!RD->field_empty()) |
| 2543 | return RD; |
| 2544 | |
| 2545 | if (RD->getNumVBases() != 0) |
| 2546 | return RD; |
| 2547 | |
| 2548 | if (RD->getNumBases() != 1) |
| 2549 | return RD; |
| 2550 | |
| 2551 | for (const CXXMethodDecl *MD : RD->methods()) { |
| 2552 | if (MD->isVirtual()) { |
| 2553 | // Virtual member functions are only ok if they are implicit destructors |
| 2554 | // because the implicit destructor will have the same semantics as the |
| 2555 | // base class's destructor if no fields are added. |
| 2556 | if (isa<CXXDestructorDecl>(MD) && MD->isImplicit()) |
| 2557 | continue; |
| 2558 | return RD; |
| 2559 | } |
| 2560 | } |
| 2561 | |
| 2562 | return LeastDerivedClassWithSameLayout( |
| 2563 | RD->bases_begin()->getType()->getAsCXXRecordDecl()); |
| 2564 | } |
| 2565 | |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 2566 | void CodeGenFunction::EmitTypeMetadataCodeForVCall(const CXXRecordDecl *RD, |
| 2567 | llvm::Value *VTable, |
| 2568 | SourceLocation Loc) { |
Peter Collingbourne | 396943a | 2017-07-31 22:35:33 +0000 | [diff] [blame] | 2569 | if (SanOpts.has(SanitizerKind::CFIVCall)) |
| 2570 | EmitVTablePtrCheckForCall(RD, VTable, CodeGenFunction::CFITCK_VCall, Loc); |
| 2571 | else if (CGM.getCodeGenOpts().WholeProgramVTables && |
| 2572 | CGM.HasHiddenLTOVisibility(RD)) { |
Peter Collingbourne | fb532b9 | 2016-02-24 20:46:36 +0000 | [diff] [blame] | 2573 | llvm::Metadata *MD = |
| 2574 | CGM.CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0)); |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 2575 | llvm::Value *TypeId = |
Peter Collingbourne | fb532b9 | 2016-02-24 20:46:36 +0000 | [diff] [blame] | 2576 | llvm::MetadataAsValue::get(CGM.getLLVMContext(), MD); |
| 2577 | |
| 2578 | llvm::Value *CastedVTable = Builder.CreateBitCast(VTable, Int8PtrTy); |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 2579 | llvm::Value *TypeTest = |
| 2580 | Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::type_test), |
| 2581 | {CastedVTable, TypeId}); |
| 2582 | Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::assume), TypeTest); |
Peter Collingbourne | fb532b9 | 2016-02-24 20:46:36 +0000 | [diff] [blame] | 2583 | } |
Peter Collingbourne | fb532b9 | 2016-02-24 20:46:36 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | void CodeGenFunction::EmitVTablePtrCheckForCall(const CXXRecordDecl *RD, |
Peter Collingbourne | 6708c4a | 2015-06-19 01:51:54 +0000 | [diff] [blame] | 2587 | llvm::Value *VTable, |
| 2588 | CFITypeCheckKind TCK, |
| 2589 | SourceLocation Loc) { |
Peter Collingbourne | 1a7488a | 2015-04-02 00:23:30 +0000 | [diff] [blame] | 2590 | if (!SanOpts.has(SanitizerKind::CFICastStrict)) |
Peter Collingbourne | fb532b9 | 2016-02-24 20:46:36 +0000 | [diff] [blame] | 2591 | RD = LeastDerivedClassWithSameLayout(RD); |
Peter Collingbourne | 1a7488a | 2015-04-02 00:23:30 +0000 | [diff] [blame] | 2592 | |
Peter Collingbourne | fb532b9 | 2016-02-24 20:46:36 +0000 | [diff] [blame] | 2593 | EmitVTablePtrCheck(RD, VTable, TCK, Loc); |
Peter Collingbourne | 1a7488a | 2015-04-02 00:23:30 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
Peter Collingbourne | d2926c9 | 2015-03-14 02:42:25 +0000 | [diff] [blame] | 2596 | void CodeGenFunction::EmitVTablePtrCheckForCast(QualType T, |
| 2597 | llvm::Value *Derived, |
Peter Collingbourne | 6708c4a | 2015-06-19 01:51:54 +0000 | [diff] [blame] | 2598 | bool MayBeNull, |
| 2599 | CFITypeCheckKind TCK, |
| 2600 | SourceLocation Loc) { |
Peter Collingbourne | d2926c9 | 2015-03-14 02:42:25 +0000 | [diff] [blame] | 2601 | if (!getLangOpts().CPlusPlus) |
| 2602 | return; |
| 2603 | |
| 2604 | auto *ClassTy = T->getAs<RecordType>(); |
| 2605 | if (!ClassTy) |
| 2606 | return; |
| 2607 | |
| 2608 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(ClassTy->getDecl()); |
| 2609 | |
| 2610 | if (!ClassDecl->isCompleteDefinition() || !ClassDecl->isDynamicClass()) |
| 2611 | return; |
| 2612 | |
Peter Collingbourne | d2926c9 | 2015-03-14 02:42:25 +0000 | [diff] [blame] | 2613 | if (!SanOpts.has(SanitizerKind::CFICastStrict)) |
| 2614 | ClassDecl = LeastDerivedClassWithSameLayout(ClassDecl); |
| 2615 | |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 2616 | llvm::BasicBlock *ContBlock = nullptr; |
Peter Collingbourne | d2926c9 | 2015-03-14 02:42:25 +0000 | [diff] [blame] | 2617 | |
| 2618 | if (MayBeNull) { |
| 2619 | llvm::Value *DerivedNotNull = |
| 2620 | Builder.CreateIsNotNull(Derived, "cast.nonnull"); |
| 2621 | |
| 2622 | llvm::BasicBlock *CheckBlock = createBasicBlock("cast.check"); |
| 2623 | ContBlock = createBasicBlock("cast.cont"); |
| 2624 | |
| 2625 | Builder.CreateCondBr(DerivedNotNull, CheckBlock, ContBlock); |
| 2626 | |
| 2627 | EmitBlock(CheckBlock); |
| 2628 | } |
| 2629 | |
Peter Collingbourne | 6010880 | 2017-12-13 21:53:04 +0000 | [diff] [blame^] | 2630 | llvm::Value *VTable; |
| 2631 | std::tie(VTable, ClassDecl) = CGM.getCXXABI().LoadVTablePtr( |
| 2632 | *this, Address(Derived, getPointerAlign()), ClassDecl); |
Piotr Padlewski | 4b1ac72 | 2015-09-15 21:46:55 +0000 | [diff] [blame] | 2633 | |
Peter Collingbourne | 6708c4a | 2015-06-19 01:51:54 +0000 | [diff] [blame] | 2634 | EmitVTablePtrCheck(ClassDecl, VTable, TCK, Loc); |
Peter Collingbourne | d2926c9 | 2015-03-14 02:42:25 +0000 | [diff] [blame] | 2635 | |
| 2636 | if (MayBeNull) { |
| 2637 | Builder.CreateBr(ContBlock); |
| 2638 | EmitBlock(ContBlock); |
| 2639 | } |
| 2640 | } |
| 2641 | |
| 2642 | void CodeGenFunction::EmitVTablePtrCheck(const CXXRecordDecl *RD, |
Peter Collingbourne | 6708c4a | 2015-06-19 01:51:54 +0000 | [diff] [blame] | 2643 | llvm::Value *VTable, |
| 2644 | CFITypeCheckKind TCK, |
| 2645 | SourceLocation Loc) { |
Peter Collingbourne | 3afb266 | 2016-04-28 17:09:37 +0000 | [diff] [blame] | 2646 | if (!CGM.getCodeGenOpts().SanitizeCfiCrossDso && |
| 2647 | !CGM.HasHiddenLTOVisibility(RD)) |
| 2648 | return; |
| 2649 | |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 2650 | SanitizerMask M; |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 2651 | llvm::SanitizerStatKind SSK; |
| 2652 | switch (TCK) { |
| 2653 | case CFITCK_VCall: |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 2654 | M = SanitizerKind::CFIVCall; |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 2655 | SSK = llvm::SanStat_CFI_VCall; |
| 2656 | break; |
| 2657 | case CFITCK_NVCall: |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 2658 | M = SanitizerKind::CFINVCall; |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 2659 | SSK = llvm::SanStat_CFI_NVCall; |
| 2660 | break; |
| 2661 | case CFITCK_DerivedCast: |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 2662 | M = SanitizerKind::CFIDerivedCast; |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 2663 | SSK = llvm::SanStat_CFI_DerivedCast; |
| 2664 | break; |
| 2665 | case CFITCK_UnrelatedCast: |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 2666 | M = SanitizerKind::CFIUnrelatedCast; |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 2667 | SSK = llvm::SanStat_CFI_UnrelatedCast; |
| 2668 | break; |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2669 | case CFITCK_ICall: |
| 2670 | llvm_unreachable("not expecting CFITCK_ICall"); |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 2671 | } |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 2672 | |
| 2673 | std::string TypeName = RD->getQualifiedNameAsString(); |
| 2674 | if (getContext().getSanitizerBlacklist().isBlacklistedType(M, TypeName)) |
| 2675 | return; |
| 2676 | |
| 2677 | SanitizerScope SanScope(this); |
Peter Collingbourne | dc13453 | 2016-01-16 00:31:22 +0000 | [diff] [blame] | 2678 | EmitSanitizerStatReport(SSK); |
Peter Collingbourne | 6708c4a | 2015-06-19 01:51:54 +0000 | [diff] [blame] | 2679 | |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 2680 | llvm::Metadata *MD = |
| 2681 | CGM.CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0)); |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 2682 | llvm::Value *TypeId = llvm::MetadataAsValue::get(getLLVMContext(), MD); |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 2683 | |
Peter Collingbourne | 6708c4a | 2015-06-19 01:51:54 +0000 | [diff] [blame] | 2684 | llvm::Value *CastedVTable = Builder.CreateBitCast(VTable, Int8PtrTy); |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 2685 | llvm::Value *TypeTest = Builder.CreateCall( |
| 2686 | CGM.getIntrinsic(llvm::Intrinsic::type_test), {CastedVTable, TypeId}); |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 2687 | |
Peter Collingbourne | 6708c4a | 2015-06-19 01:51:54 +0000 | [diff] [blame] | 2688 | llvm::Constant *StaticData[] = { |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2689 | llvm::ConstantInt::get(Int8Ty, TCK), |
Evgeniy Stepanov | fd6f92d | 2015-12-15 23:00:20 +0000 | [diff] [blame] | 2690 | EmitCheckSourceLocation(Loc), |
| 2691 | EmitCheckTypeDescriptor(QualType(RD->getTypeForDecl(), 0)), |
Peter Collingbourne | 6708c4a | 2015-06-19 01:51:54 +0000 | [diff] [blame] | 2692 | }; |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2693 | |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 2694 | auto CrossDsoTypeId = CGM.CreateCrossDsoCfiTypeId(MD); |
| 2695 | if (CGM.getCodeGenOpts().SanitizeCfiCrossDso && CrossDsoTypeId) { |
| 2696 | EmitCfiSlowPathCheck(M, TypeTest, CrossDsoTypeId, CastedVTable, StaticData); |
Evgeniy Stepanov | f31ea30 | 2016-02-03 22:18:55 +0000 | [diff] [blame] | 2697 | return; |
Evgeniy Stepanov | 3fd61df | 2016-01-25 23:34:52 +0000 | [diff] [blame] | 2698 | } |
Evgeniy Stepanov | f31ea30 | 2016-02-03 22:18:55 +0000 | [diff] [blame] | 2699 | |
| 2700 | if (CGM.getCodeGenOpts().SanitizeTrap.has(M)) { |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 2701 | EmitTrapCheck(TypeTest); |
Evgeniy Stepanov | f31ea30 | 2016-02-03 22:18:55 +0000 | [diff] [blame] | 2702 | return; |
| 2703 | } |
| 2704 | |
| 2705 | llvm::Value *AllVtables = llvm::MetadataAsValue::get( |
| 2706 | CGM.getLLVMContext(), |
| 2707 | llvm::MDString::get(CGM.getLLVMContext(), "all-vtables")); |
Peter Collingbourne | 8dd14da | 2016-06-24 21:21:46 +0000 | [diff] [blame] | 2708 | llvm::Value *ValidVtable = Builder.CreateCall( |
| 2709 | CGM.getIntrinsic(llvm::Intrinsic::type_test), {CastedVTable, AllVtables}); |
Filipe Cabecinhas | 322ecd9 | 2016-12-12 16:18:40 +0000 | [diff] [blame] | 2710 | EmitCheck(std::make_pair(TypeTest, M), SanitizerHandler::CFICheckFail, |
| 2711 | StaticData, {CastedVTable, ValidVtable}); |
Peter Collingbourne | a4ccff3 | 2015-02-20 20:30:56 +0000 | [diff] [blame] | 2712 | } |
Anders Carlsson | c36783e | 2011-05-08 20:32:23 +0000 | [diff] [blame] | 2713 | |
Peter Collingbourne | 0ca0363 | 2016-06-25 00:24:06 +0000 | [diff] [blame] | 2714 | bool CodeGenFunction::ShouldEmitVTableTypeCheckedLoad(const CXXRecordDecl *RD) { |
| 2715 | if (!CGM.getCodeGenOpts().WholeProgramVTables || |
| 2716 | !SanOpts.has(SanitizerKind::CFIVCall) || |
| 2717 | !CGM.getCodeGenOpts().SanitizeTrap.has(SanitizerKind::CFIVCall) || |
| 2718 | !CGM.HasHiddenLTOVisibility(RD)) |
| 2719 | return false; |
| 2720 | |
| 2721 | std::string TypeName = RD->getQualifiedNameAsString(); |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 2722 | return !getContext().getSanitizerBlacklist().isBlacklistedType( |
| 2723 | SanitizerKind::CFIVCall, TypeName); |
Peter Collingbourne | 0ca0363 | 2016-06-25 00:24:06 +0000 | [diff] [blame] | 2724 | } |
| 2725 | |
| 2726 | llvm::Value *CodeGenFunction::EmitVTableTypeCheckedLoad( |
| 2727 | const CXXRecordDecl *RD, llvm::Value *VTable, uint64_t VTableByteOffset) { |
| 2728 | SanitizerScope SanScope(this); |
| 2729 | |
| 2730 | EmitSanitizerStatReport(llvm::SanStat_CFI_VCall); |
| 2731 | |
| 2732 | llvm::Metadata *MD = |
| 2733 | CGM.CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0)); |
| 2734 | llvm::Value *TypeId = llvm::MetadataAsValue::get(CGM.getLLVMContext(), MD); |
| 2735 | |
| 2736 | llvm::Value *CastedVTable = Builder.CreateBitCast(VTable, Int8PtrTy); |
| 2737 | llvm::Value *CheckedLoad = Builder.CreateCall( |
| 2738 | CGM.getIntrinsic(llvm::Intrinsic::type_checked_load), |
| 2739 | {CastedVTable, llvm::ConstantInt::get(Int32Ty, VTableByteOffset), |
| 2740 | TypeId}); |
| 2741 | llvm::Value *CheckResult = Builder.CreateExtractValue(CheckedLoad, 1); |
| 2742 | |
| 2743 | EmitCheck(std::make_pair(CheckResult, SanitizerKind::CFIVCall), |
Filipe Cabecinhas | 322ecd9 | 2016-12-12 16:18:40 +0000 | [diff] [blame] | 2744 | SanitizerHandler::CFICheckFail, nullptr, nullptr); |
Peter Collingbourne | 0ca0363 | 2016-06-25 00:24:06 +0000 | [diff] [blame] | 2745 | |
| 2746 | return Builder.CreateBitCast( |
| 2747 | Builder.CreateExtractValue(CheckedLoad, 0), |
| 2748 | cast<llvm::PointerType>(VTable->getType())->getElementType()); |
| 2749 | } |
| 2750 | |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 2751 | void CodeGenFunction::EmitForwardingCallToLambda( |
| 2752 | const CXXMethodDecl *callOperator, |
| 2753 | CallArgList &callArgs) { |
Eli Friedman | 5b44688 | 2012-02-16 03:47:28 +0000 | [diff] [blame] | 2754 | // Get the address of the call operator. |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 2755 | const CGFunctionInfo &calleeFnInfo = |
| 2756 | CGM.getTypes().arrangeCXXMethodDeclaration(callOperator); |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 2757 | llvm::Constant *calleePtr = |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 2758 | CGM.GetAddrOfFunction(GlobalDecl(callOperator), |
| 2759 | CGM.getTypes().GetFunctionType(calleeFnInfo)); |
Eli Friedman | 5b44688 | 2012-02-16 03:47:28 +0000 | [diff] [blame] | 2760 | |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 2761 | // Prepare the return slot. |
| 2762 | const FunctionProtoType *FPT = |
| 2763 | callOperator->getType()->castAs<FunctionProtoType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2764 | QualType resultType = FPT->getReturnType(); |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 2765 | ReturnValueSlot returnSlot; |
| 2766 | if (!resultType->isVoidType() && |
| 2767 | calleeFnInfo.getReturnInfo().getKind() == ABIArgInfo::Indirect && |
John McCall | 47fb950 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2768 | !hasScalarEvaluationKind(calleeFnInfo.getReturnType())) |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 2769 | returnSlot = ReturnValueSlot(ReturnValue, resultType.isVolatileQualified()); |
| 2770 | |
| 2771 | // We don't need to separately arrange the call arguments because |
| 2772 | // the call can't be variadic anyway --- it's impossible to forward |
| 2773 | // variadic arguments. |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 2774 | |
Eli Friedman | 5b44688 | 2012-02-16 03:47:28 +0000 | [diff] [blame] | 2775 | // Now emit our call. |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 2776 | auto callee = CGCallee::forDirect(calleePtr, callOperator); |
| 2777 | RValue RV = EmitCall(calleeFnInfo, callee, returnSlot, callArgs); |
Eli Friedman | 5b44688 | 2012-02-16 03:47:28 +0000 | [diff] [blame] | 2778 | |
John McCall | 8dda7b2 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 2779 | // If necessary, copy the returned value into the slot. |
| 2780 | if (!resultType->isVoidType() && returnSlot.isNull()) |
| 2781 | EmitReturnOfRValue(RV, resultType); |
Eli Friedman | f5f4d2f | 2012-12-13 23:37:17 +0000 | [diff] [blame] | 2782 | else |
| 2783 | EmitBranchThroughCleanup(ReturnBlock); |
Eli Friedman | 5b44688 | 2012-02-16 03:47:28 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
Eli Friedman | 2495ab0 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 2786 | void CodeGenFunction::EmitLambdaBlockInvokeBody() { |
| 2787 | const BlockDecl *BD = BlockInfo->getBlockDecl(); |
| 2788 | const VarDecl *variable = BD->capture_begin()->getVariable(); |
| 2789 | const CXXRecordDecl *Lambda = variable->getType()->getAsCXXRecordDecl(); |
Reid Kleckner | 2d3c421 | 2017-08-04 22:38:06 +0000 | [diff] [blame] | 2790 | const CXXMethodDecl *CallOp = Lambda->getLambdaCallOperator(); |
| 2791 | |
| 2792 | if (CallOp->isVariadic()) { |
| 2793 | // FIXME: Making this work correctly is nasty because it requires either |
| 2794 | // cloning the body of the call operator or making the call operator |
| 2795 | // forward. |
| 2796 | CGM.ErrorUnsupported(CurCodeDecl, "lambda conversion to variadic function"); |
| 2797 | return; |
| 2798 | } |
Eli Friedman | 2495ab0 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 2799 | |
| 2800 | // Start building arguments for forwarding call |
| 2801 | CallArgList CallArgs; |
| 2802 | |
| 2803 | QualType ThisType = getContext().getPointerType(getContext().getRecordType(Lambda)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2804 | Address ThisPtr = GetAddrOfBlockDecl(variable, false); |
| 2805 | CallArgs.add(RValue::get(ThisPtr.getPointer()), ThisType); |
Eli Friedman | 2495ab0 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 2806 | |
| 2807 | // Add the rest of the parameters. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2808 | for (auto param : BD->parameters()) |
Nick Lewycky | 2d84e84 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2809 | EmitDelegateCallArg(CallArgs, param, param->getLocStart()); |
Aaron Ballman | b2b8b1d | 2014-03-07 16:09:59 +0000 | [diff] [blame] | 2810 | |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 2811 | assert(!Lambda->isGenericLambda() && |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 2812 | "generic lambda interconversion to block not implemented"); |
Reid Kleckner | 2d3c421 | 2017-08-04 22:38:06 +0000 | [diff] [blame] | 2813 | EmitForwardingCallToLambda(CallOp, CallArgs); |
Eli Friedman | 2495ab0 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 2814 | } |
| 2815 | |
| 2816 | void CodeGenFunction::EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD) { |
| 2817 | const CXXRecordDecl *Lambda = MD->getParent(); |
| 2818 | |
| 2819 | // Start building arguments for forwarding call |
| 2820 | CallArgList CallArgs; |
| 2821 | |
| 2822 | QualType ThisType = getContext().getPointerType(getContext().getRecordType(Lambda)); |
| 2823 | llvm::Value *ThisPtr = llvm::UndefValue::get(getTypes().ConvertType(ThisType)); |
| 2824 | CallArgs.add(RValue::get(ThisPtr), ThisType); |
| 2825 | |
| 2826 | // Add the rest of the parameters. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2827 | for (auto Param : MD->parameters()) |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 2828 | EmitDelegateCallArg(CallArgs, Param, Param->getLocStart()); |
| 2829 | |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 2830 | const CXXMethodDecl *CallOp = Lambda->getLambdaCallOperator(); |
| 2831 | // For a generic lambda, find the corresponding call operator specialization |
| 2832 | // to which the call to the static-invoker shall be forwarded. |
| 2833 | if (Lambda->isGenericLambda()) { |
| 2834 | assert(MD->isFunctionTemplateSpecialization()); |
| 2835 | const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs(); |
| 2836 | FunctionTemplateDecl *CallOpTemplate = CallOp->getDescribedFunctionTemplate(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2837 | void *InsertPos = nullptr; |
Justin Bogner | 1cd11f1 | 2015-05-20 15:53:59 +0000 | [diff] [blame] | 2838 | FunctionDecl *CorrespondingCallOpSpecialization = |
Craig Topper | 7e0daca | 2014-06-26 04:58:53 +0000 | [diff] [blame] | 2839 | CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos); |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 2840 | assert(CorrespondingCallOpSpecialization); |
| 2841 | CallOp = cast<CXXMethodDecl>(CorrespondingCallOpSpecialization); |
| 2842 | } |
| 2843 | EmitForwardingCallToLambda(CallOp, CallArgs); |
Eli Friedman | 2495ab0 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
Reid Kleckner | 2d3c421 | 2017-08-04 22:38:06 +0000 | [diff] [blame] | 2846 | void CodeGenFunction::EmitLambdaStaticInvokeBody(const CXXMethodDecl *MD) { |
Douglas Gregor | 355efbb | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 2847 | if (MD->isVariadic()) { |
Eli Friedman | 5b44688 | 2012-02-16 03:47:28 +0000 | [diff] [blame] | 2848 | // FIXME: Making this work correctly is nasty because it requires either |
| 2849 | // cloning the body of the call operator or making the call operator forward. |
| 2850 | CGM.ErrorUnsupported(MD, "lambda conversion to variadic function"); |
Eli Friedman | 2495ab0 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 2851 | return; |
Eli Friedman | 5b44688 | 2012-02-16 03:47:28 +0000 | [diff] [blame] | 2852 | } |
| 2853 | |
Douglas Gregor | 355efbb | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 2854 | EmitLambdaDelegatingInvokeBody(MD); |
Eli Friedman | 5a6d507 | 2012-02-16 01:37:33 +0000 | [diff] [blame] | 2855 | } |