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