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