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