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