Anders Carlsson | 5b95592 | 2009-11-24 05:51:11 +0000 | [diff] [blame] | 1 | //===--- CGClass.cpp - Emit LLVM Code for C++ classes ---------------------===// |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code dealing with C++ code generation of classes |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Eli Friedman | 64bee65 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 14 | #include "CGBlocks.h" |
Devang Patel | d67ef0e | 2010-08-11 21:04:37 +0000 | [diff] [blame] | 15 | #include "CGDebugInfo.h" |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 16 | #include "CGRecordLayout.h" |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 17 | #include "CodeGenFunction.h" |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 18 | #include "CGCXXABI.h" |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 19 | #include "clang/AST/CXXInheritance.h" |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclTemplate.h" |
John McCall | 7e1dff7 | 2010-09-17 02:31:44 +0000 | [diff] [blame] | 21 | #include "clang/AST/EvaluatedExprVisitor.h" |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 22 | #include "clang/AST/RecordLayout.h" |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 23 | #include "clang/AST/StmtCXX.h" |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetBuiltins.h" |
Devang Patel | 3ee36af | 2011-02-22 20:55:26 +0000 | [diff] [blame] | 25 | #include "clang/Frontend/CodeGenOptions.h" |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 26 | |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | using namespace CodeGen; |
| 29 | |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 30 | static CharUnits |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 31 | ComputeNonVirtualBaseClassOffset(ASTContext &Context, |
| 32 | const CXXRecordDecl *DerivedClass, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 33 | CastExpr::path_const_iterator Start, |
| 34 | CastExpr::path_const_iterator End) { |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 35 | CharUnits Offset = CharUnits::Zero(); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 36 | |
| 37 | const CXXRecordDecl *RD = DerivedClass; |
| 38 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 39 | for (CastExpr::path_const_iterator I = Start; I != End; ++I) { |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 40 | const CXXBaseSpecifier *Base = *I; |
| 41 | assert(!Base->isVirtual() && "Should not see virtual bases here!"); |
| 42 | |
| 43 | // Get the layout. |
| 44 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 45 | |
| 46 | const CXXRecordDecl *BaseDecl = |
| 47 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 48 | |
| 49 | // Add the offset. |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 50 | Offset += Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 51 | |
| 52 | RD = BaseDecl; |
| 53 | } |
| 54 | |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 55 | return Offset; |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 56 | } |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 57 | |
Anders Carlsson | 84080ec | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 58 | llvm::Constant * |
Anders Carlsson | a04efdf | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 59 | CodeGenModule::GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 60 | CastExpr::path_const_iterator PathBegin, |
| 61 | CastExpr::path_const_iterator PathEnd) { |
| 62 | assert(PathBegin != PathEnd && "Base path should not be empty!"); |
Anders Carlsson | a04efdf | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 63 | |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 64 | CharUnits Offset = |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 65 | ComputeNonVirtualBaseClassOffset(getContext(), ClassDecl, |
| 66 | PathBegin, PathEnd); |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 67 | if (Offset.isZero()) |
Anders Carlsson | a04efdf | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 68 | return 0; |
| 69 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 70 | llvm::Type *PtrDiffTy = |
Anders Carlsson | a04efdf | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 71 | Types.ConvertType(getContext().getPointerDiffType()); |
| 72 | |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 73 | return llvm::ConstantInt::get(PtrDiffTy, Offset.getQuantity()); |
Anders Carlsson | 84080ec | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 76 | /// Gets the address of a direct base class within a complete object. |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 77 | /// This should only be used for (1) non-virtual bases or (2) virtual bases |
| 78 | /// when the type is known to be complete (e.g. in complete destructors). |
| 79 | /// |
| 80 | /// The object pointed to by 'This' is assumed to be non-null. |
| 81 | llvm::Value * |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 82 | CodeGenFunction::GetAddressOfDirectBaseInCompleteClass(llvm::Value *This, |
| 83 | const CXXRecordDecl *Derived, |
| 84 | const CXXRecordDecl *Base, |
| 85 | bool BaseIsVirtual) { |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 86 | // 'this' must be a pointer (in some address space) to Derived. |
| 87 | assert(This->getType()->isPointerTy() && |
| 88 | cast<llvm::PointerType>(This->getType())->getElementType() |
| 89 | == ConvertType(Derived)); |
| 90 | |
| 91 | // Compute the offset of the virtual base. |
Ken Dyck | 5fff46b | 2011-03-22 01:21:15 +0000 | [diff] [blame] | 92 | CharUnits Offset; |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 93 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Derived); |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 94 | if (BaseIsVirtual) |
Ken Dyck | 5fff46b | 2011-03-22 01:21:15 +0000 | [diff] [blame] | 95 | Offset = Layout.getVBaseClassOffset(Base); |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 96 | else |
Ken Dyck | 5fff46b | 2011-03-22 01:21:15 +0000 | [diff] [blame] | 97 | Offset = Layout.getBaseClassOffset(Base); |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 98 | |
| 99 | // Shift and cast down to the base type. |
| 100 | // TODO: for complete types, this should be possible with a GEP. |
| 101 | llvm::Value *V = This; |
Ken Dyck | 5fff46b | 2011-03-22 01:21:15 +0000 | [diff] [blame] | 102 | if (Offset.isPositive()) { |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 103 | V = Builder.CreateBitCast(V, Int8PtrTy); |
Ken Dyck | 5fff46b | 2011-03-22 01:21:15 +0000 | [diff] [blame] | 104 | V = Builder.CreateConstInBoundsGEP1_64(V, Offset.getQuantity()); |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 105 | } |
| 106 | V = Builder.CreateBitCast(V, ConvertType(Base)->getPointerTo()); |
| 107 | |
| 108 | return V; |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 109 | } |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 110 | |
Anders Carlsson | 9dc228a | 2010-04-20 16:03:35 +0000 | [diff] [blame] | 111 | static llvm::Value * |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 112 | ApplyNonVirtualAndVirtualOffset(CodeGenFunction &CGF, llvm::Value *ptr, |
| 113 | CharUnits nonVirtualOffset, |
| 114 | llvm::Value *virtualOffset) { |
| 115 | // Assert that we have something to do. |
| 116 | assert(!nonVirtualOffset.isZero() || virtualOffset != 0); |
| 117 | |
| 118 | // Compute the offset from the static and dynamic components. |
| 119 | llvm::Value *baseOffset; |
| 120 | if (!nonVirtualOffset.isZero()) { |
| 121 | baseOffset = llvm::ConstantInt::get(CGF.PtrDiffTy, |
| 122 | nonVirtualOffset.getQuantity()); |
| 123 | if (virtualOffset) { |
| 124 | baseOffset = CGF.Builder.CreateAdd(virtualOffset, baseOffset); |
| 125 | } |
| 126 | } else { |
| 127 | baseOffset = virtualOffset; |
| 128 | } |
Anders Carlsson | 9dc228a | 2010-04-20 16:03:35 +0000 | [diff] [blame] | 129 | |
| 130 | // Apply the base offset. |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 131 | ptr = CGF.Builder.CreateBitCast(ptr, CGF.Int8PtrTy); |
| 132 | ptr = CGF.Builder.CreateInBoundsGEP(ptr, baseOffset, "add.ptr"); |
| 133 | return ptr; |
Anders Carlsson | 9dc228a | 2010-04-20 16:03:35 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 136 | llvm::Value * |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 137 | CodeGenFunction::GetAddressOfBaseClass(llvm::Value *Value, |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 138 | const CXXRecordDecl *Derived, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 139 | CastExpr::path_const_iterator PathBegin, |
| 140 | CastExpr::path_const_iterator PathEnd, |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 141 | bool NullCheckValue) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 142 | assert(PathBegin != PathEnd && "Base path should not be empty!"); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 143 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 144 | CastExpr::path_const_iterator Start = PathBegin; |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 145 | const CXXRecordDecl *VBase = 0; |
| 146 | |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 147 | // Sema has done some convenient canonicalization here: if the |
| 148 | // access path involved any virtual steps, the conversion path will |
| 149 | // *start* with a step down to the correct virtual base subobject, |
| 150 | // and hence will not require any further steps. |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 151 | if ((*Start)->isVirtual()) { |
| 152 | VBase = |
| 153 | cast<CXXRecordDecl>((*Start)->getType()->getAs<RecordType>()->getDecl()); |
| 154 | ++Start; |
| 155 | } |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 156 | |
| 157 | // Compute the static offset of the ultimate destination within its |
| 158 | // allocating subobject (the virtual base, if there is one, or else |
| 159 | // the "complete" object that we see). |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 160 | CharUnits NonVirtualOffset = |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 161 | ComputeNonVirtualBaseClassOffset(getContext(), VBase ? VBase : Derived, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 162 | Start, PathEnd); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 163 | |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 164 | // If there's a virtual step, we can sometimes "devirtualize" it. |
| 165 | // For now, that's limited to when the derived type is final. |
| 166 | // TODO: "devirtualize" this for accesses to known-complete objects. |
| 167 | if (VBase && Derived->hasAttr<FinalAttr>()) { |
| 168 | const ASTRecordLayout &layout = getContext().getASTRecordLayout(Derived); |
| 169 | CharUnits vBaseOffset = layout.getVBaseClassOffset(VBase); |
| 170 | NonVirtualOffset += vBaseOffset; |
| 171 | VBase = 0; // we no longer have a virtual step |
| 172 | } |
| 173 | |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 174 | // Get the base pointer type. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 175 | llvm::Type *BasePtrTy = |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 176 | ConvertType((PathEnd[-1])->getType())->getPointerTo(); |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 177 | |
| 178 | // If the static offset is zero and we don't have a virtual step, |
| 179 | // just do a bitcast; null checks are unnecessary. |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 180 | if (NonVirtualOffset.isZero() && !VBase) { |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 181 | return Builder.CreateBitCast(Value, BasePtrTy); |
| 182 | } |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 183 | |
| 184 | llvm::BasicBlock *origBB = 0; |
| 185 | llvm::BasicBlock *endBB = 0; |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 186 | |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 187 | // Skip over the offset (and the vtable load) if we're supposed to |
| 188 | // null-check the pointer. |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 189 | if (NullCheckValue) { |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 190 | origBB = Builder.GetInsertBlock(); |
| 191 | llvm::BasicBlock *notNullBB = createBasicBlock("cast.notnull"); |
| 192 | endBB = createBasicBlock("cast.end"); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 193 | |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 194 | llvm::Value *isNull = Builder.CreateIsNull(Value); |
| 195 | Builder.CreateCondBr(isNull, endBB, notNullBB); |
| 196 | EmitBlock(notNullBB); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 197 | } |
| 198 | |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 199 | // Compute the virtual offset. |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 200 | llvm::Value *VirtualOffset = 0; |
Anders Carlsson | 336a7dc | 2011-01-29 03:18:56 +0000 | [diff] [blame] | 201 | if (VBase) { |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 202 | VirtualOffset = |
| 203 | CGM.getCXXABI().GetVirtualBaseClassOffset(*this, Value, Derived, VBase); |
Anders Carlsson | 336a7dc | 2011-01-29 03:18:56 +0000 | [diff] [blame] | 204 | } |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 205 | |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 206 | // Apply both offsets. |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 207 | Value = ApplyNonVirtualAndVirtualOffset(*this, Value, |
Ken Dyck | 9a8ad9b | 2011-03-23 00:45:26 +0000 | [diff] [blame] | 208 | NonVirtualOffset, |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 209 | VirtualOffset); |
| 210 | |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 211 | // Cast to the destination type. |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 212 | Value = Builder.CreateBitCast(Value, BasePtrTy); |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 213 | |
| 214 | // Build a phi if we needed a null check. |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 215 | if (NullCheckValue) { |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 216 | llvm::BasicBlock *notNullBB = Builder.GetInsertBlock(); |
| 217 | Builder.CreateBr(endBB); |
| 218 | EmitBlock(endBB); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 219 | |
John McCall | 7916c99 | 2012-08-01 05:04:58 +0000 | [diff] [blame] | 220 | llvm::PHINode *PHI = Builder.CreatePHI(BasePtrTy, 2, "cast.result"); |
| 221 | PHI->addIncoming(Value, notNullBB); |
| 222 | PHI->addIncoming(llvm::Constant::getNullValue(BasePtrTy), origBB); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 223 | Value = PHI; |
| 224 | } |
| 225 | |
| 226 | return Value; |
| 227 | } |
| 228 | |
| 229 | llvm::Value * |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 230 | CodeGenFunction::GetAddressOfDerivedClass(llvm::Value *Value, |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 231 | const CXXRecordDecl *Derived, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 232 | CastExpr::path_const_iterator PathBegin, |
| 233 | CastExpr::path_const_iterator PathEnd, |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 234 | bool NullCheckValue) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 235 | assert(PathBegin != PathEnd && "Base path should not be empty!"); |
Anders Carlsson | a04efdf | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 236 | |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 237 | QualType DerivedTy = |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 238 | getContext().getCanonicalType(getContext().getTagDeclType(Derived)); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 239 | llvm::Type *DerivedPtrTy = ConvertType(DerivedTy)->getPointerTo(); |
Richard Smith | c764830 | 2013-02-13 21:18:23 +0000 | [diff] [blame] | 240 | |
Anders Carlsson | a552ea7 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 241 | llvm::Value *NonVirtualOffset = |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 242 | CGM.GetNonVirtualBaseClassOffset(Derived, PathBegin, PathEnd); |
Anders Carlsson | a552ea7 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 243 | |
| 244 | if (!NonVirtualOffset) { |
| 245 | // No offset, we can just cast back. |
| 246 | return Builder.CreateBitCast(Value, DerivedPtrTy); |
| 247 | } |
| 248 | |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 249 | llvm::BasicBlock *CastNull = 0; |
| 250 | llvm::BasicBlock *CastNotNull = 0; |
| 251 | llvm::BasicBlock *CastEnd = 0; |
| 252 | |
| 253 | if (NullCheckValue) { |
| 254 | CastNull = createBasicBlock("cast.null"); |
| 255 | CastNotNull = createBasicBlock("cast.notnull"); |
| 256 | CastEnd = createBasicBlock("cast.end"); |
| 257 | |
Anders Carlsson | b924124 | 2011-04-11 00:30:07 +0000 | [diff] [blame] | 258 | llvm::Value *IsNull = Builder.CreateIsNull(Value); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 259 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
| 260 | EmitBlock(CastNotNull); |
| 261 | } |
| 262 | |
Anders Carlsson | a552ea7 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 263 | // Apply the offset. |
Eli Friedman | c568543 | 2012-02-28 22:07:56 +0000 | [diff] [blame] | 264 | Value = Builder.CreateBitCast(Value, Int8PtrTy); |
| 265 | Value = Builder.CreateGEP(Value, Builder.CreateNeg(NonVirtualOffset), |
| 266 | "sub.ptr"); |
Anders Carlsson | a552ea7 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 267 | |
| 268 | // Just cast. |
| 269 | Value = Builder.CreateBitCast(Value, DerivedPtrTy); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 270 | |
| 271 | if (NullCheckValue) { |
| 272 | Builder.CreateBr(CastEnd); |
| 273 | EmitBlock(CastNull); |
| 274 | Builder.CreateBr(CastEnd); |
| 275 | EmitBlock(CastEnd); |
| 276 | |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 277 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 278 | PHI->addIncoming(Value, CastNotNull); |
| 279 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), |
| 280 | CastNull); |
| 281 | Value = PHI; |
| 282 | } |
| 283 | |
| 284 | return Value; |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 285 | } |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 286 | |
| 287 | llvm::Value *CodeGenFunction::GetVTTParameter(GlobalDecl GD, |
| 288 | bool ForVirtualBase, |
| 289 | bool Delegating) { |
Peter Collingbourne | e1e35f7 | 2013-06-28 20:45:28 +0000 | [diff] [blame] | 290 | if (!CGM.getCXXABI().NeedsVTTParameter(GD)) { |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 291 | // This constructor/destructor does not need a VTT parameter. |
| 292 | return 0; |
| 293 | } |
| 294 | |
John McCall | f5ebf9b | 2013-05-03 07:33:41 +0000 | [diff] [blame] | 295 | const CXXRecordDecl *RD = cast<CXXMethodDecl>(CurCodeDecl)->getParent(); |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 296 | const CXXRecordDecl *Base = cast<CXXMethodDecl>(GD.getDecl())->getParent(); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 297 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 298 | llvm::Value *VTT; |
| 299 | |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 300 | uint64_t SubVTTIndex; |
| 301 | |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 302 | if (Delegating) { |
| 303 | // If this is a delegating constructor call, just load the VTT. |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 304 | return LoadCXXVTT(); |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 305 | } else if (RD == Base) { |
| 306 | // If the record matches the base, this is the complete ctor/dtor |
| 307 | // variant calling the base variant in a class with virtual bases. |
Peter Collingbourne | e1e35f7 | 2013-06-28 20:45:28 +0000 | [diff] [blame] | 308 | assert(!CGM.getCXXABI().NeedsVTTParameter(CurGD) && |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 309 | "doing no-op VTT offset in base dtor/ctor?"); |
Anders Carlsson | 314e622 | 2010-05-02 23:33:10 +0000 | [diff] [blame] | 310 | assert(!ForVirtualBase && "Can't have same class as virtual base!"); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 311 | SubVTTIndex = 0; |
| 312 | } else { |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 313 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 314 | CharUnits BaseOffset = ForVirtualBase ? |
| 315 | Layout.getVBaseClassOffset(Base) : |
| 316 | Layout.getBaseClassOffset(Base); |
Anders Carlsson | c11bb21 | 2010-05-02 23:53:25 +0000 | [diff] [blame] | 317 | |
| 318 | SubVTTIndex = |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 319 | CGM.getVTables().getSubVTTIndex(RD, BaseSubobject(Base, BaseOffset)); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 320 | assert(SubVTTIndex != 0 && "Sub-VTT index must be greater than zero!"); |
| 321 | } |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 322 | |
Peter Collingbourne | e1e35f7 | 2013-06-28 20:45:28 +0000 | [diff] [blame] | 323 | if (CGM.getCXXABI().NeedsVTTParameter(CurGD)) { |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 324 | // A VTT parameter was passed to the constructor, use it. |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 325 | VTT = LoadCXXVTT(); |
| 326 | VTT = Builder.CreateConstInBoundsGEP1_64(VTT, SubVTTIndex); |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 327 | } else { |
| 328 | // We're the complete constructor, so get the VTT by name. |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 329 | VTT = CGM.getVTables().GetAddrOfVTT(RD); |
| 330 | VTT = Builder.CreateConstInBoundsGEP2_64(VTT, 0, SubVTTIndex); |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | return VTT; |
| 334 | } |
| 335 | |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 336 | namespace { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 337 | /// Call the destructor for a direct base class. |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 338 | struct CallBaseDtor : EHScopeStack::Cleanup { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 339 | const CXXRecordDecl *BaseClass; |
| 340 | bool BaseIsVirtual; |
| 341 | CallBaseDtor(const CXXRecordDecl *Base, bool BaseIsVirtual) |
| 342 | : BaseClass(Base), BaseIsVirtual(BaseIsVirtual) {} |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 343 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 344 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 345 | const CXXRecordDecl *DerivedClass = |
| 346 | cast<CXXMethodDecl>(CGF.CurCodeDecl)->getParent(); |
| 347 | |
| 348 | const CXXDestructorDecl *D = BaseClass->getDestructor(); |
| 349 | llvm::Value *Addr = |
| 350 | CGF.GetAddressOfDirectBaseInCompleteClass(CGF.LoadCXXThis(), |
| 351 | DerivedClass, BaseClass, |
| 352 | BaseIsVirtual); |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 353 | CGF.EmitCXXDestructorCall(D, Dtor_Base, BaseIsVirtual, |
| 354 | /*Delegating=*/false, Addr); |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 355 | } |
| 356 | }; |
John McCall | 7e1dff7 | 2010-09-17 02:31:44 +0000 | [diff] [blame] | 357 | |
| 358 | /// A visitor which checks whether an initializer uses 'this' in a |
| 359 | /// way which requires the vtable to be properly set. |
| 360 | struct DynamicThisUseChecker : EvaluatedExprVisitor<DynamicThisUseChecker> { |
| 361 | typedef EvaluatedExprVisitor<DynamicThisUseChecker> super; |
| 362 | |
| 363 | bool UsesThis; |
| 364 | |
| 365 | DynamicThisUseChecker(ASTContext &C) : super(C), UsesThis(false) {} |
| 366 | |
| 367 | // Black-list all explicit and implicit references to 'this'. |
| 368 | // |
| 369 | // Do we need to worry about external references to 'this' derived |
| 370 | // from arbitrary code? If so, then anything which runs arbitrary |
| 371 | // external code might potentially access the vtable. |
| 372 | void VisitCXXThisExpr(CXXThisExpr *E) { UsesThis = true; } |
| 373 | }; |
| 374 | } |
| 375 | |
| 376 | static bool BaseInitializerUsesThis(ASTContext &C, const Expr *Init) { |
| 377 | DynamicThisUseChecker Checker(C); |
| 378 | Checker.Visit(const_cast<Expr*>(Init)); |
| 379 | return Checker.UsesThis; |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 382 | static void EmitBaseInitializer(CodeGenFunction &CGF, |
| 383 | const CXXRecordDecl *ClassDecl, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 384 | CXXCtorInitializer *BaseInit, |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 385 | CXXCtorType CtorType) { |
| 386 | assert(BaseInit->isBaseInitializer() && |
| 387 | "Must have base initializer!"); |
| 388 | |
| 389 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
| 390 | |
| 391 | const Type *BaseType = BaseInit->getBaseClass(); |
| 392 | CXXRecordDecl *BaseClassDecl = |
| 393 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); |
| 394 | |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 395 | bool isBaseVirtual = BaseInit->isBaseVirtual(); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 396 | |
| 397 | // The base constructor doesn't construct virtual bases. |
| 398 | if (CtorType == Ctor_Base && isBaseVirtual) |
| 399 | return; |
| 400 | |
John McCall | 7e1dff7 | 2010-09-17 02:31:44 +0000 | [diff] [blame] | 401 | // If the initializer for the base (other than the constructor |
| 402 | // itself) accesses 'this' in any way, we need to initialize the |
| 403 | // vtables. |
| 404 | if (BaseInitializerUsesThis(CGF.getContext(), BaseInit->getInit())) |
| 405 | CGF.InitializeVTablePointers(ClassDecl); |
| 406 | |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 407 | // We can pretend to be a complete class because it only matters for |
| 408 | // virtual bases, and we only do virtual bases for complete ctors. |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 409 | llvm::Value *V = |
| 410 | CGF.GetAddressOfDirectBaseInCompleteClass(ThisPtr, ClassDecl, |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 411 | BaseClassDecl, |
| 412 | isBaseVirtual); |
Eli Friedman | d7722d9 | 2011-12-03 02:13:40 +0000 | [diff] [blame] | 413 | CharUnits Alignment = CGF.getContext().getTypeAlignInChars(BaseType); |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 414 | AggValueSlot AggSlot = |
Eli Friedman | f394078 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 415 | AggValueSlot::forAddr(V, Alignment, Qualifiers(), |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 416 | AggValueSlot::IsDestructed, |
John McCall | 410ffb2 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 417 | AggValueSlot::DoesNotNeedGCBarriers, |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 418 | AggValueSlot::IsNotAliased); |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 419 | |
| 420 | CGF.EmitAggExpr(BaseInit->getInit(), AggSlot); |
Anders Carlsson | 594d5e8 | 2010-02-06 20:00:21 +0000 | [diff] [blame] | 421 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 422 | if (CGF.CGM.getLangOpts().Exceptions && |
Anders Carlsson | c1cfdf8 | 2011-02-20 00:20:27 +0000 | [diff] [blame] | 423 | !BaseClassDecl->hasTrivialDestructor()) |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 424 | CGF.EHStack.pushCleanup<CallBaseDtor>(EHCleanup, BaseClassDecl, |
| 425 | isBaseVirtual); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 428 | static void EmitAggMemberInitializer(CodeGenFunction &CGF, |
| 429 | LValue LHS, |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 430 | Expr *Init, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 431 | llvm::Value *ArrayIndexVar, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 432 | QualType T, |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 433 | ArrayRef<VarDecl *> ArrayIndexes, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 434 | unsigned Index) { |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 435 | if (Index == ArrayIndexes.size()) { |
Eli Friedman | f394078 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 436 | LValue LV = LHS; |
Eli Friedman | f394078 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 437 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 438 | if (ArrayIndexVar) { |
| 439 | // If we have an array index variable, load it and use it as an offset. |
| 440 | // Then, increment the value. |
| 441 | llvm::Value *Dest = LHS.getAddress(); |
| 442 | llvm::Value *ArrayIndex = CGF.Builder.CreateLoad(ArrayIndexVar); |
| 443 | Dest = CGF.Builder.CreateInBoundsGEP(Dest, ArrayIndex, "destaddress"); |
| 444 | llvm::Value *Next = llvm::ConstantInt::get(ArrayIndex->getType(), 1); |
| 445 | Next = CGF.Builder.CreateAdd(ArrayIndex, Next, "inc"); |
| 446 | CGF.Builder.CreateStore(Next, ArrayIndexVar); |
Sebastian Redl | 924db71 | 2012-02-19 15:41:54 +0000 | [diff] [blame] | 447 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 448 | // Update the LValue. |
| 449 | LV.setAddress(Dest); |
| 450 | CharUnits Align = CGF.getContext().getTypeAlignInChars(T); |
| 451 | LV.setAlignment(std::min(Align, LV.getAlignment())); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 452 | } |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 453 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 454 | switch (CGF.getEvaluationKind(T)) { |
| 455 | case TEK_Scalar: |
| 456 | CGF.EmitScalarInit(Init, /*decl*/ 0, LV, false); |
| 457 | break; |
| 458 | case TEK_Complex: |
| 459 | CGF.EmitComplexExprIntoLValue(Init, LV, /*isInit*/ true); |
| 460 | break; |
| 461 | case TEK_Aggregate: { |
| 462 | AggValueSlot Slot = |
| 463 | AggValueSlot::forLValue(LV, |
| 464 | AggValueSlot::IsDestructed, |
| 465 | AggValueSlot::DoesNotNeedGCBarriers, |
| 466 | AggValueSlot::IsNotAliased); |
| 467 | |
| 468 | CGF.EmitAggExpr(Init, Slot); |
| 469 | break; |
| 470 | } |
| 471 | } |
Sebastian Redl | 924db71 | 2012-02-19 15:41:54 +0000 | [diff] [blame] | 472 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 473 | return; |
| 474 | } |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 475 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 476 | const ConstantArrayType *Array = CGF.getContext().getAsConstantArrayType(T); |
| 477 | assert(Array && "Array initialization without the array type?"); |
| 478 | llvm::Value *IndexVar |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 479 | = CGF.GetAddrOfLocalVar(ArrayIndexes[Index]); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 480 | assert(IndexVar && "Array index variable not loaded"); |
| 481 | |
| 482 | // Initialize this index variable to zero. |
| 483 | llvm::Value* Zero |
| 484 | = llvm::Constant::getNullValue( |
| 485 | CGF.ConvertType(CGF.getContext().getSizeType())); |
| 486 | CGF.Builder.CreateStore(Zero, IndexVar); |
| 487 | |
| 488 | // Start the loop with a block that tests the condition. |
| 489 | llvm::BasicBlock *CondBlock = CGF.createBasicBlock("for.cond"); |
| 490 | llvm::BasicBlock *AfterFor = CGF.createBasicBlock("for.end"); |
| 491 | |
| 492 | CGF.EmitBlock(CondBlock); |
| 493 | |
| 494 | llvm::BasicBlock *ForBody = CGF.createBasicBlock("for.body"); |
| 495 | // Generate: if (loop-index < number-of-elements) fall to the loop body, |
| 496 | // otherwise, go to the block after the for-loop. |
| 497 | uint64_t NumElements = Array->getSize().getZExtValue(); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 498 | llvm::Value *Counter = CGF.Builder.CreateLoad(IndexVar); |
Chris Lattner | 985f739 | 2010-05-06 06:35:23 +0000 | [diff] [blame] | 499 | llvm::Value *NumElementsPtr = |
| 500 | llvm::ConstantInt::get(Counter->getType(), NumElements); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 501 | llvm::Value *IsLess = CGF.Builder.CreateICmpULT(Counter, NumElementsPtr, |
| 502 | "isless"); |
| 503 | |
| 504 | // If the condition is true, execute the body. |
| 505 | CGF.Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
| 506 | |
| 507 | CGF.EmitBlock(ForBody); |
| 508 | llvm::BasicBlock *ContinueBlock = CGF.createBasicBlock("for.inc"); |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 509 | |
| 510 | // Inside the loop body recurse to emit the inner loop or, eventually, the |
| 511 | // constructor call. |
| 512 | EmitAggMemberInitializer(CGF, LHS, Init, ArrayIndexVar, |
| 513 | Array->getElementType(), ArrayIndexes, Index + 1); |
| 514 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 515 | CGF.EmitBlock(ContinueBlock); |
| 516 | |
| 517 | // Emit the increment of the loop counter. |
| 518 | llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); |
| 519 | Counter = CGF.Builder.CreateLoad(IndexVar); |
| 520 | NextVal = CGF.Builder.CreateAdd(Counter, NextVal, "inc"); |
| 521 | CGF.Builder.CreateStore(NextVal, IndexVar); |
| 522 | |
| 523 | // Finally, branch back up to the condition for the next iteration. |
| 524 | CGF.EmitBranch(CondBlock); |
| 525 | |
| 526 | // Emit the fall-through block. |
| 527 | CGF.EmitBlock(AfterFor, true); |
| 528 | } |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 529 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 530 | static void EmitMemberInitializer(CodeGenFunction &CGF, |
| 531 | const CXXRecordDecl *ClassDecl, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 532 | CXXCtorInitializer *MemberInit, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 533 | const CXXConstructorDecl *Constructor, |
| 534 | FunctionArgList &Args) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 535 | assert(MemberInit->isAnyMemberInitializer() && |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 536 | "Must have member initializer!"); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 537 | assert(MemberInit->getInit() && "Must have initializer!"); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 538 | |
| 539 | // non-static data member initializers. |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 540 | FieldDecl *Field = MemberInit->getAnyMember(); |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 541 | QualType FieldType = Field->getType(); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 542 | |
| 543 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
Eli Friedman | 377ecc7 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 544 | QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl); |
Eli Friedman | 859c65c | 2012-08-08 03:51:37 +0000 | [diff] [blame] | 545 | LValue LHS = CGF.MakeNaturalAlignAddrLValue(ThisPtr, RecordTy); |
Eli Friedman | 377ecc7 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 546 | |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 547 | if (MemberInit->isIndirectMemberInitializer()) { |
Eli Friedman | 859c65c | 2012-08-08 03:51:37 +0000 | [diff] [blame] | 548 | // If we are initializing an anonymous union field, drill down to |
| 549 | // the field. |
| 550 | IndirectFieldDecl *IndirectField = MemberInit->getIndirectMember(); |
| 551 | IndirectFieldDecl::chain_iterator I = IndirectField->chain_begin(), |
| 552 | IEnd = IndirectField->chain_end(); |
| 553 | for ( ; I != IEnd; ++I) |
| 554 | LHS = CGF.EmitLValueForFieldInitialization(LHS, cast<FieldDecl>(*I)); |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 555 | FieldType = MemberInit->getIndirectMember()->getAnonField()->getType(); |
John McCall | a9976d3 | 2010-05-21 01:18:57 +0000 | [diff] [blame] | 556 | } else { |
Eli Friedman | 859c65c | 2012-08-08 03:51:37 +0000 | [diff] [blame] | 557 | LHS = CGF.EmitLValueForFieldInitialization(LHS, Field); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 560 | // Special case: if we are in a copy or move constructor, and we are copying |
| 561 | // an array of PODs or classes with trivial copy constructors, ignore the |
| 562 | // AST and perform the copy we know is equivalent. |
| 563 | // FIXME: This is hacky at best... if we had a bit more explicit information |
| 564 | // in the AST, we could generalize it more easily. |
| 565 | const ConstantArrayType *Array |
| 566 | = CGF.getContext().getAsConstantArrayType(FieldType); |
Jordan Rose | a7b8797 | 2013-08-07 16:16:48 +0000 | [diff] [blame] | 567 | if (Array && Constructor->isDefaulted() && |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 568 | Constructor->isCopyOrMoveConstructor()) { |
| 569 | QualType BaseElementTy = CGF.getContext().getBaseElementType(Array); |
Richard Smith | e938536 | 2012-11-07 23:56:21 +0000 | [diff] [blame] | 570 | CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(MemberInit->getInit()); |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 571 | if (BaseElementTy.isPODType(CGF.getContext()) || |
Richard Smith | e938536 | 2012-11-07 23:56:21 +0000 | [diff] [blame] | 572 | (CE && CE->getConstructor()->isTrivial())) { |
| 573 | // Find the source pointer. We know it's the last argument because |
| 574 | // we know we're in an implicit copy constructor. |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 575 | unsigned SrcArgIndex = Args.size() - 1; |
| 576 | llvm::Value *SrcPtr |
| 577 | = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(Args[SrcArgIndex])); |
Eli Friedman | 377ecc7 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 578 | LValue ThisRHSLV = CGF.MakeNaturalAlignAddrLValue(SrcPtr, RecordTy); |
| 579 | LValue Src = CGF.EmitLValueForFieldInitialization(ThisRHSLV, Field); |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 580 | |
| 581 | // Copy the aggregate. |
| 582 | CGF.EmitAggregateCopy(LHS.getAddress(), Src.getAddress(), FieldType, |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 583 | LHS.isVolatileQualified()); |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 584 | return; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | ArrayRef<VarDecl *> ArrayIndexes; |
| 589 | if (MemberInit->getNumArrayIndices()) |
| 590 | ArrayIndexes = MemberInit->getArrayIndexes(); |
Eli Friedman | b74ed08 | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 591 | CGF.EmitInitializerForField(Field, LHS, MemberInit->getInit(), ArrayIndexes); |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Eli Friedman | b74ed08 | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 594 | void CodeGenFunction::EmitInitializerForField(FieldDecl *Field, |
| 595 | LValue LHS, Expr *Init, |
| 596 | ArrayRef<VarDecl *> ArrayIndexes) { |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 597 | QualType FieldType = Field->getType(); |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 598 | switch (getEvaluationKind(FieldType)) { |
| 599 | case TEK_Scalar: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 600 | if (LHS.isSimple()) { |
Eli Friedman | b74ed08 | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 601 | EmitExprAsInit(Init, Field, LHS, false); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 602 | } else { |
Eli Friedman | b74ed08 | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 603 | RValue RHS = RValue::get(EmitScalarExpr(Init)); |
| 604 | EmitStoreThroughLValue(RHS, LHS); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 605 | } |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 606 | break; |
| 607 | case TEK_Complex: |
| 608 | EmitComplexExprIntoLValue(Init, LHS, /*isInit*/ true); |
| 609 | break; |
| 610 | case TEK_Aggregate: { |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 611 | llvm::Value *ArrayIndexVar = 0; |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 612 | if (ArrayIndexes.size()) { |
Eli Friedman | b74ed08 | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 613 | llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 614 | |
| 615 | // The LHS is a pointer to the first object we'll be constructing, as |
| 616 | // a flat array. |
Eli Friedman | b74ed08 | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 617 | QualType BaseElementTy = getContext().getBaseElementType(FieldType); |
| 618 | llvm::Type *BasePtr = ConvertType(BaseElementTy); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 619 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
Eli Friedman | b74ed08 | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 620 | llvm::Value *BaseAddrPtr = Builder.CreateBitCast(LHS.getAddress(), |
| 621 | BasePtr); |
| 622 | LHS = MakeAddrLValue(BaseAddrPtr, BaseElementTy); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 623 | |
| 624 | // Create an array index that will be used to walk over all of the |
| 625 | // objects we're constructing. |
Eli Friedman | b74ed08 | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 626 | ArrayIndexVar = CreateTempAlloca(SizeTy, "object.index"); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 627 | llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy); |
Eli Friedman | b74ed08 | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 628 | Builder.CreateStore(Zero, ArrayIndexVar); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 629 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 630 | |
| 631 | // Emit the block variables for the array indices, if any. |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 632 | for (unsigned I = 0, N = ArrayIndexes.size(); I != N; ++I) |
Eli Friedman | b74ed08 | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 633 | EmitAutoVarDecl(*ArrayIndexes[I]); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Eli Friedman | b74ed08 | 2012-02-14 02:31:03 +0000 | [diff] [blame] | 636 | EmitAggMemberInitializer(*this, LHS, Init, ArrayIndexVar, FieldType, |
Eli Friedman | 0bdb5aa | 2012-02-14 02:15:49 +0000 | [diff] [blame] | 637 | ArrayIndexes, 0); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 638 | } |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 639 | } |
John McCall | 074cae0 | 2013-02-01 05:11:40 +0000 | [diff] [blame] | 640 | |
| 641 | // Ensure that we destroy this object if an exception is thrown |
| 642 | // later in the constructor. |
| 643 | QualType::DestructionKind dtorKind = FieldType.isDestructedType(); |
| 644 | if (needsEHCleanup(dtorKind)) |
| 645 | pushEHDestroy(dtorKind, LHS.getAddress(), FieldType); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 646 | } |
| 647 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 648 | /// Checks whether the given constructor is a valid subject for the |
| 649 | /// complete-to-base constructor delegation optimization, i.e. |
| 650 | /// emitting the complete constructor as a simple call to the base |
| 651 | /// constructor. |
| 652 | static bool IsConstructorDelegationValid(const CXXConstructorDecl *Ctor) { |
| 653 | |
| 654 | // Currently we disable the optimization for classes with virtual |
| 655 | // bases because (1) the addresses of parameter variables need to be |
| 656 | // consistent across all initializers but (2) the delegate function |
| 657 | // call necessarily creates a second copy of the parameter variable. |
| 658 | // |
| 659 | // The limiting example (purely theoretical AFAIK): |
| 660 | // struct A { A(int &c) { c++; } }; |
| 661 | // struct B : virtual A { |
| 662 | // B(int count) : A(count) { printf("%d\n", count); } |
| 663 | // }; |
| 664 | // ...although even this example could in principle be emitted as a |
| 665 | // delegation since the address of the parameter doesn't escape. |
| 666 | if (Ctor->getParent()->getNumVBases()) { |
| 667 | // TODO: white-list trivial vbase initializers. This case wouldn't |
| 668 | // be subject to the restrictions below. |
| 669 | |
| 670 | // TODO: white-list cases where: |
| 671 | // - there are no non-reference parameters to the constructor |
| 672 | // - the initializers don't access any non-reference parameters |
| 673 | // - the initializers don't take the address of non-reference |
| 674 | // parameters |
| 675 | // - etc. |
| 676 | // If we ever add any of the above cases, remember that: |
| 677 | // - function-try-blocks will always blacklist this optimization |
| 678 | // - we need to perform the constructor prologue and cleanup in |
| 679 | // EmitConstructorBody. |
| 680 | |
| 681 | return false; |
| 682 | } |
| 683 | |
| 684 | // We also disable the optimization for variadic functions because |
| 685 | // it's impossible to "re-pass" varargs. |
| 686 | if (Ctor->getType()->getAs<FunctionProtoType>()->isVariadic()) |
| 687 | return false; |
| 688 | |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 689 | // FIXME: Decide if we can do a delegation of a delegating constructor. |
| 690 | if (Ctor->isDelegatingConstructor()) |
| 691 | return false; |
| 692 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 693 | return true; |
| 694 | } |
| 695 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 696 | /// EmitConstructorBody - Emits the body of the current constructor. |
| 697 | void CodeGenFunction::EmitConstructorBody(FunctionArgList &Args) { |
| 698 | const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(CurGD.getDecl()); |
| 699 | CXXCtorType CtorType = CurGD.getCtorType(); |
| 700 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 701 | // Before we go any further, try the complete->base constructor |
| 702 | // delegation optimization. |
Timur Iskhodzhanov | 8560791 | 2012-04-20 08:05:00 +0000 | [diff] [blame] | 703 | if (CtorType == Ctor_Complete && IsConstructorDelegationValid(Ctor) && |
John McCall | 64aa4b3 | 2013-04-16 22:48:15 +0000 | [diff] [blame] | 704 | CGM.getTarget().getCXXABI().hasConstructorVariants()) { |
Devang Patel | d67ef0e | 2010-08-11 21:04:37 +0000 | [diff] [blame] | 705 | if (CGDebugInfo *DI = getDebugInfo()) |
Eric Christopher | 73fb350 | 2011-10-13 21:45:18 +0000 | [diff] [blame] | 706 | DI->EmitLocation(Builder, Ctor->getLocEnd()); |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 707 | EmitDelegateCXXConstructorCall(Ctor, Ctor_Base, Args, Ctor->getLocEnd()); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 708 | return; |
| 709 | } |
| 710 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 711 | Stmt *Body = Ctor->getBody(); |
| 712 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 713 | // Enter the function-try-block before the constructor prologue if |
| 714 | // applicable. |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 715 | bool IsTryBody = (Body && isa<CXXTryStmt>(Body)); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 716 | if (IsTryBody) |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 717 | EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 718 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 719 | RunCleanupsScope RunCleanups(*this); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 720 | |
John McCall | 56ea377 | 2012-03-30 04:25:03 +0000 | [diff] [blame] | 721 | // TODO: in restricted cases, we can emit the vbase initializers of |
| 722 | // a complete ctor and then delegate to the base ctor. |
| 723 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 724 | // Emit the constructor prologue, i.e. the base and member |
| 725 | // initializers. |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 726 | EmitCtorPrologue(Ctor, CtorType, Args); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 727 | |
| 728 | // Emit the body of the statement. |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 729 | if (IsTryBody) |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 730 | EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock()); |
| 731 | else if (Body) |
| 732 | EmitStmt(Body); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 733 | |
| 734 | // Emit any cleanup blocks associated with the member or base |
| 735 | // initializers, which includes (along the exceptional path) the |
| 736 | // destructors for those members and bases that were fully |
| 737 | // constructed. |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 738 | RunCleanups.ForceCleanup(); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 739 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 740 | if (IsTryBody) |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 741 | ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 744 | namespace { |
Nick Lewycky | 62a3bba | 2013-09-11 02:03:20 +0000 | [diff] [blame] | 745 | /// RAII object to indicate that codegen is copying the value representation |
| 746 | /// instead of the object representation. Useful when copying a struct or |
| 747 | /// class which has uninitialized members and we're only performing |
| 748 | /// lvalue-to-rvalue conversion on the object but not its members. |
| 749 | class CopyingValueRepresentation { |
| 750 | public: |
| 751 | explicit CopyingValueRepresentation(CodeGenFunction &CGF) |
| 752 | : CGF(CGF), SO(*CGF.SanOpts), OldSanOpts(CGF.SanOpts) { |
| 753 | SO.Bool = false; |
| 754 | SO.Enum = false; |
| 755 | CGF.SanOpts = &SO; |
| 756 | } |
| 757 | ~CopyingValueRepresentation() { |
| 758 | CGF.SanOpts = OldSanOpts; |
| 759 | } |
| 760 | private: |
| 761 | CodeGenFunction &CGF; |
| 762 | SanitizerOptions SO; |
| 763 | const SanitizerOptions *OldSanOpts; |
| 764 | }; |
| 765 | } |
| 766 | |
| 767 | namespace { |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 768 | class FieldMemcpyizer { |
| 769 | public: |
| 770 | FieldMemcpyizer(CodeGenFunction &CGF, const CXXRecordDecl *ClassDecl, |
| 771 | const VarDecl *SrcRec) |
| 772 | : CGF(CGF), ClassDecl(ClassDecl), SrcRec(SrcRec), |
| 773 | RecLayout(CGF.getContext().getASTRecordLayout(ClassDecl)), |
| 774 | FirstField(0), LastField(0), FirstFieldOffset(0), LastFieldOffset(0), |
| 775 | LastAddedFieldIndex(0) { } |
| 776 | |
| 777 | static bool isMemcpyableField(FieldDecl *F) { |
| 778 | Qualifiers Qual = F->getType().getQualifiers(); |
| 779 | if (Qual.hasVolatile() || Qual.hasObjCLifetime()) |
| 780 | return false; |
| 781 | return true; |
| 782 | } |
| 783 | |
| 784 | void addMemcpyableField(FieldDecl *F) { |
| 785 | if (FirstField == 0) |
| 786 | addInitialField(F); |
| 787 | else |
| 788 | addNextField(F); |
| 789 | } |
| 790 | |
| 791 | CharUnits getMemcpySize() const { |
| 792 | unsigned LastFieldSize = |
| 793 | LastField->isBitField() ? |
| 794 | LastField->getBitWidthValue(CGF.getContext()) : |
| 795 | CGF.getContext().getTypeSize(LastField->getType()); |
| 796 | uint64_t MemcpySizeBits = |
| 797 | LastFieldOffset + LastFieldSize - FirstFieldOffset + |
| 798 | CGF.getContext().getCharWidth() - 1; |
| 799 | CharUnits MemcpySize = |
| 800 | CGF.getContext().toCharUnitsFromBits(MemcpySizeBits); |
| 801 | return MemcpySize; |
| 802 | } |
| 803 | |
| 804 | void emitMemcpy() { |
| 805 | // Give the subclass a chance to bail out if it feels the memcpy isn't |
| 806 | // worth it (e.g. Hasn't aggregated enough data). |
| 807 | if (FirstField == 0) { |
| 808 | return; |
| 809 | } |
| 810 | |
Lang Hames | 5e8577e | 2013-02-27 04:14:49 +0000 | [diff] [blame] | 811 | CharUnits Alignment; |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 812 | |
| 813 | if (FirstField->isBitField()) { |
| 814 | const CGRecordLayout &RL = |
| 815 | CGF.getTypes().getCGRecordLayout(FirstField->getParent()); |
| 816 | const CGBitFieldInfo &BFInfo = RL.getBitFieldInfo(FirstField); |
Lang Hames | 5e8577e | 2013-02-27 04:14:49 +0000 | [diff] [blame] | 817 | Alignment = CharUnits::fromQuantity(BFInfo.StorageAlignment); |
| 818 | } else { |
Lang Hames | 23742cd | 2013-03-05 20:27:24 +0000 | [diff] [blame] | 819 | Alignment = CGF.getContext().getDeclAlign(FirstField); |
Lang Hames | 5e8577e | 2013-02-27 04:14:49 +0000 | [diff] [blame] | 820 | } |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 821 | |
Lang Hames | 5e8577e | 2013-02-27 04:14:49 +0000 | [diff] [blame] | 822 | assert((CGF.getContext().toCharUnitsFromBits(FirstFieldOffset) % |
| 823 | Alignment) == 0 && "Bad field alignment."); |
| 824 | |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 825 | CharUnits MemcpySize = getMemcpySize(); |
| 826 | QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl); |
| 827 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
| 828 | LValue DestLV = CGF.MakeNaturalAlignAddrLValue(ThisPtr, RecordTy); |
| 829 | LValue Dest = CGF.EmitLValueForFieldInitialization(DestLV, FirstField); |
| 830 | llvm::Value *SrcPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(SrcRec)); |
| 831 | LValue SrcLV = CGF.MakeNaturalAlignAddrLValue(SrcPtr, RecordTy); |
| 832 | LValue Src = CGF.EmitLValueForFieldInitialization(SrcLV, FirstField); |
| 833 | |
| 834 | emitMemcpyIR(Dest.isBitField() ? Dest.getBitFieldAddr() : Dest.getAddress(), |
| 835 | Src.isBitField() ? Src.getBitFieldAddr() : Src.getAddress(), |
| 836 | MemcpySize, Alignment); |
| 837 | reset(); |
| 838 | } |
| 839 | |
| 840 | void reset() { |
| 841 | FirstField = 0; |
| 842 | } |
| 843 | |
| 844 | protected: |
| 845 | CodeGenFunction &CGF; |
| 846 | const CXXRecordDecl *ClassDecl; |
| 847 | |
| 848 | private: |
| 849 | |
| 850 | void emitMemcpyIR(llvm::Value *DestPtr, llvm::Value *SrcPtr, |
| 851 | CharUnits Size, CharUnits Alignment) { |
| 852 | llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType()); |
| 853 | llvm::Type *DBP = |
| 854 | llvm::Type::getInt8PtrTy(CGF.getLLVMContext(), DPT->getAddressSpace()); |
| 855 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, DBP); |
| 856 | |
| 857 | llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType()); |
| 858 | llvm::Type *SBP = |
| 859 | llvm::Type::getInt8PtrTy(CGF.getLLVMContext(), SPT->getAddressSpace()); |
| 860 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, SBP); |
| 861 | |
| 862 | CGF.Builder.CreateMemCpy(DestPtr, SrcPtr, Size.getQuantity(), |
| 863 | Alignment.getQuantity()); |
| 864 | } |
| 865 | |
| 866 | void addInitialField(FieldDecl *F) { |
| 867 | FirstField = F; |
| 868 | LastField = F; |
| 869 | FirstFieldOffset = RecLayout.getFieldOffset(F->getFieldIndex()); |
| 870 | LastFieldOffset = FirstFieldOffset; |
| 871 | LastAddedFieldIndex = F->getFieldIndex(); |
| 872 | return; |
| 873 | } |
| 874 | |
| 875 | void addNextField(FieldDecl *F) { |
John McCall | 402cd22 | 2013-05-07 05:20:46 +0000 | [diff] [blame] | 876 | // For the most part, the following invariant will hold: |
| 877 | // F->getFieldIndex() == LastAddedFieldIndex + 1 |
| 878 | // The one exception is that Sema won't add a copy-initializer for an |
| 879 | // unnamed bitfield, which will show up here as a gap in the sequence. |
| 880 | assert(F->getFieldIndex() >= LastAddedFieldIndex + 1 && |
| 881 | "Cannot aggregate fields out of order."); |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 882 | LastAddedFieldIndex = F->getFieldIndex(); |
| 883 | |
| 884 | // The 'first' and 'last' fields are chosen by offset, rather than field |
| 885 | // index. This allows the code to support bitfields, as well as regular |
| 886 | // fields. |
| 887 | uint64_t FOffset = RecLayout.getFieldOffset(F->getFieldIndex()); |
| 888 | if (FOffset < FirstFieldOffset) { |
| 889 | FirstField = F; |
| 890 | FirstFieldOffset = FOffset; |
| 891 | } else if (FOffset > LastFieldOffset) { |
| 892 | LastField = F; |
| 893 | LastFieldOffset = FOffset; |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | const VarDecl *SrcRec; |
| 898 | const ASTRecordLayout &RecLayout; |
| 899 | FieldDecl *FirstField; |
| 900 | FieldDecl *LastField; |
| 901 | uint64_t FirstFieldOffset, LastFieldOffset; |
| 902 | unsigned LastAddedFieldIndex; |
| 903 | }; |
| 904 | |
| 905 | class ConstructorMemcpyizer : public FieldMemcpyizer { |
| 906 | private: |
| 907 | |
| 908 | /// Get source argument for copy constructor. Returns null if not a copy |
| 909 | /// constructor. |
| 910 | static const VarDecl* getTrivialCopySource(const CXXConstructorDecl *CD, |
| 911 | FunctionArgList &Args) { |
Jordan Rose | a7b8797 | 2013-08-07 16:16:48 +0000 | [diff] [blame] | 912 | if (CD->isCopyOrMoveConstructor() && CD->isDefaulted()) |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 913 | return Args[Args.size() - 1]; |
| 914 | return 0; |
| 915 | } |
| 916 | |
| 917 | // Returns true if a CXXCtorInitializer represents a member initialization |
| 918 | // that can be rolled into a memcpy. |
| 919 | bool isMemberInitMemcpyable(CXXCtorInitializer *MemberInit) const { |
| 920 | if (!MemcpyableCtor) |
| 921 | return false; |
| 922 | FieldDecl *Field = MemberInit->getMember(); |
| 923 | assert(Field != 0 && "No field for member init."); |
| 924 | QualType FieldType = Field->getType(); |
| 925 | CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(MemberInit->getInit()); |
| 926 | |
| 927 | // Bail out on non-POD, not-trivially-constructable members. |
| 928 | if (!(CE && CE->getConstructor()->isTrivial()) && |
| 929 | !(FieldType.isTriviallyCopyableType(CGF.getContext()) || |
| 930 | FieldType->isReferenceType())) |
| 931 | return false; |
| 932 | |
| 933 | // Bail out on volatile fields. |
| 934 | if (!isMemcpyableField(Field)) |
| 935 | return false; |
| 936 | |
| 937 | // Otherwise we're good. |
| 938 | return true; |
| 939 | } |
| 940 | |
| 941 | public: |
| 942 | ConstructorMemcpyizer(CodeGenFunction &CGF, const CXXConstructorDecl *CD, |
| 943 | FunctionArgList &Args) |
| 944 | : FieldMemcpyizer(CGF, CD->getParent(), getTrivialCopySource(CD, Args)), |
| 945 | ConstructorDecl(CD), |
Jordan Rose | a7b8797 | 2013-08-07 16:16:48 +0000 | [diff] [blame] | 946 | MemcpyableCtor(CD->isDefaulted() && |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 947 | CD->isCopyOrMoveConstructor() && |
| 948 | CGF.getLangOpts().getGC() == LangOptions::NonGC), |
| 949 | Args(Args) { } |
| 950 | |
| 951 | void addMemberInitializer(CXXCtorInitializer *MemberInit) { |
| 952 | if (isMemberInitMemcpyable(MemberInit)) { |
| 953 | AggregatedInits.push_back(MemberInit); |
| 954 | addMemcpyableField(MemberInit->getMember()); |
| 955 | } else { |
| 956 | emitAggregatedInits(); |
| 957 | EmitMemberInitializer(CGF, ConstructorDecl->getParent(), MemberInit, |
| 958 | ConstructorDecl, Args); |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | void emitAggregatedInits() { |
| 963 | if (AggregatedInits.size() <= 1) { |
| 964 | // This memcpy is too small to be worthwhile. Fall back on default |
| 965 | // codegen. |
Nick Lewycky | 62a3bba | 2013-09-11 02:03:20 +0000 | [diff] [blame] | 966 | if (!AggregatedInits.empty()) { |
| 967 | CopyingValueRepresentation CVR(CGF); |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 968 | EmitMemberInitializer(CGF, ConstructorDecl->getParent(), |
Nick Lewycky | 62a3bba | 2013-09-11 02:03:20 +0000 | [diff] [blame] | 969 | AggregatedInits[0], ConstructorDecl, Args); |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 970 | } |
| 971 | reset(); |
| 972 | return; |
| 973 | } |
| 974 | |
| 975 | pushEHDestructors(); |
| 976 | emitMemcpy(); |
| 977 | AggregatedInits.clear(); |
| 978 | } |
| 979 | |
| 980 | void pushEHDestructors() { |
| 981 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
| 982 | QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl); |
| 983 | LValue LHS = CGF.MakeNaturalAlignAddrLValue(ThisPtr, RecordTy); |
| 984 | |
| 985 | for (unsigned i = 0; i < AggregatedInits.size(); ++i) { |
| 986 | QualType FieldType = AggregatedInits[i]->getMember()->getType(); |
| 987 | QualType::DestructionKind dtorKind = FieldType.isDestructedType(); |
| 988 | if (CGF.needsEHCleanup(dtorKind)) |
| 989 | CGF.pushEHDestroy(dtorKind, LHS.getAddress(), FieldType); |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | void finish() { |
| 994 | emitAggregatedInits(); |
| 995 | } |
| 996 | |
| 997 | private: |
| 998 | const CXXConstructorDecl *ConstructorDecl; |
| 999 | bool MemcpyableCtor; |
| 1000 | FunctionArgList &Args; |
| 1001 | SmallVector<CXXCtorInitializer*, 16> AggregatedInits; |
| 1002 | }; |
| 1003 | |
| 1004 | class AssignmentMemcpyizer : public FieldMemcpyizer { |
| 1005 | private: |
| 1006 | |
| 1007 | // Returns the memcpyable field copied by the given statement, if one |
Nick Lewycky | 62a3bba | 2013-09-11 02:03:20 +0000 | [diff] [blame] | 1008 | // exists. Otherwise returns null. |
| 1009 | FieldDecl *getMemcpyableField(Stmt *S) { |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1010 | if (!AssignmentsMemcpyable) |
| 1011 | return 0; |
| 1012 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(S)) { |
| 1013 | // Recognise trivial assignments. |
| 1014 | if (BO->getOpcode() != BO_Assign) |
| 1015 | return 0; |
| 1016 | MemberExpr *ME = dyn_cast<MemberExpr>(BO->getLHS()); |
| 1017 | if (!ME) |
| 1018 | return 0; |
| 1019 | FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl()); |
| 1020 | if (!Field || !isMemcpyableField(Field)) |
| 1021 | return 0; |
| 1022 | Stmt *RHS = BO->getRHS(); |
| 1023 | if (ImplicitCastExpr *EC = dyn_cast<ImplicitCastExpr>(RHS)) |
| 1024 | RHS = EC->getSubExpr(); |
| 1025 | if (!RHS) |
| 1026 | return 0; |
| 1027 | MemberExpr *ME2 = dyn_cast<MemberExpr>(RHS); |
| 1028 | if (dyn_cast<FieldDecl>(ME2->getMemberDecl()) != Field) |
| 1029 | return 0; |
| 1030 | return Field; |
| 1031 | } else if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(S)) { |
| 1032 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MCE->getCalleeDecl()); |
| 1033 | if (!(MD && (MD->isCopyAssignmentOperator() || |
| 1034 | MD->isMoveAssignmentOperator()) && |
| 1035 | MD->isTrivial())) |
| 1036 | return 0; |
| 1037 | MemberExpr *IOA = dyn_cast<MemberExpr>(MCE->getImplicitObjectArgument()); |
| 1038 | if (!IOA) |
| 1039 | return 0; |
| 1040 | FieldDecl *Field = dyn_cast<FieldDecl>(IOA->getMemberDecl()); |
| 1041 | if (!Field || !isMemcpyableField(Field)) |
| 1042 | return 0; |
| 1043 | MemberExpr *Arg0 = dyn_cast<MemberExpr>(MCE->getArg(0)); |
| 1044 | if (!Arg0 || Field != dyn_cast<FieldDecl>(Arg0->getMemberDecl())) |
| 1045 | return 0; |
| 1046 | return Field; |
| 1047 | } else if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 1048 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CE->getCalleeDecl()); |
| 1049 | if (!FD || FD->getBuiltinID() != Builtin::BI__builtin_memcpy) |
| 1050 | return 0; |
| 1051 | Expr *DstPtr = CE->getArg(0); |
| 1052 | if (ImplicitCastExpr *DC = dyn_cast<ImplicitCastExpr>(DstPtr)) |
| 1053 | DstPtr = DC->getSubExpr(); |
| 1054 | UnaryOperator *DUO = dyn_cast<UnaryOperator>(DstPtr); |
| 1055 | if (!DUO || DUO->getOpcode() != UO_AddrOf) |
| 1056 | return 0; |
| 1057 | MemberExpr *ME = dyn_cast<MemberExpr>(DUO->getSubExpr()); |
| 1058 | if (!ME) |
| 1059 | return 0; |
| 1060 | FieldDecl *Field = dyn_cast<FieldDecl>(ME->getMemberDecl()); |
| 1061 | if (!Field || !isMemcpyableField(Field)) |
| 1062 | return 0; |
| 1063 | Expr *SrcPtr = CE->getArg(1); |
| 1064 | if (ImplicitCastExpr *SC = dyn_cast<ImplicitCastExpr>(SrcPtr)) |
| 1065 | SrcPtr = SC->getSubExpr(); |
| 1066 | UnaryOperator *SUO = dyn_cast<UnaryOperator>(SrcPtr); |
| 1067 | if (!SUO || SUO->getOpcode() != UO_AddrOf) |
| 1068 | return 0; |
| 1069 | MemberExpr *ME2 = dyn_cast<MemberExpr>(SUO->getSubExpr()); |
| 1070 | if (!ME2 || Field != dyn_cast<FieldDecl>(ME2->getMemberDecl())) |
| 1071 | return 0; |
| 1072 | return Field; |
| 1073 | } |
| 1074 | |
| 1075 | return 0; |
| 1076 | } |
| 1077 | |
| 1078 | bool AssignmentsMemcpyable; |
| 1079 | SmallVector<Stmt*, 16> AggregatedStmts; |
| 1080 | |
| 1081 | public: |
| 1082 | |
| 1083 | AssignmentMemcpyizer(CodeGenFunction &CGF, const CXXMethodDecl *AD, |
| 1084 | FunctionArgList &Args) |
| 1085 | : FieldMemcpyizer(CGF, AD->getParent(), Args[Args.size() - 1]), |
| 1086 | AssignmentsMemcpyable(CGF.getLangOpts().getGC() == LangOptions::NonGC) { |
| 1087 | assert(Args.size() == 2); |
| 1088 | } |
| 1089 | |
| 1090 | void emitAssignment(Stmt *S) { |
| 1091 | FieldDecl *F = getMemcpyableField(S); |
| 1092 | if (F) { |
| 1093 | addMemcpyableField(F); |
| 1094 | AggregatedStmts.push_back(S); |
| 1095 | } else { |
| 1096 | emitAggregatedStmts(); |
| 1097 | CGF.EmitStmt(S); |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | void emitAggregatedStmts() { |
| 1102 | if (AggregatedStmts.size() <= 1) { |
Nick Lewycky | 62a3bba | 2013-09-11 02:03:20 +0000 | [diff] [blame] | 1103 | if (!AggregatedStmts.empty()) { |
| 1104 | CopyingValueRepresentation CVR(CGF); |
| 1105 | CGF.EmitStmt(AggregatedStmts[0]); |
| 1106 | } |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1107 | reset(); |
| 1108 | } |
| 1109 | |
| 1110 | emitMemcpy(); |
| 1111 | AggregatedStmts.clear(); |
| 1112 | } |
| 1113 | |
| 1114 | void finish() { |
| 1115 | emitAggregatedStmts(); |
| 1116 | } |
| 1117 | }; |
| 1118 | |
| 1119 | } |
| 1120 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1121 | /// EmitCtorPrologue - This routine generates necessary code to initialize |
| 1122 | /// base classes and non-static data members belonging to this constructor. |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1123 | void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 1124 | CXXCtorType CtorType, |
| 1125 | FunctionArgList &Args) { |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 1126 | if (CD->isDelegatingConstructor()) |
| 1127 | return EmitDelegatingCXXConstructorCall(CD, Args); |
| 1128 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1129 | const CXXRecordDecl *ClassDecl = CD->getParent(); |
Anders Carlsson | a78fa2c | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 1130 | |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1131 | CXXConstructorDecl::init_const_iterator B = CD->init_begin(), |
| 1132 | E = CD->init_end(); |
| 1133 | |
| 1134 | llvm::BasicBlock *BaseCtorContinueBB = 0; |
| 1135 | if (ClassDecl->getNumVBases() && |
| 1136 | !CGM.getTarget().getCXXABI().hasConstructorVariants()) { |
| 1137 | // The ABIs that don't have constructor variants need to put a branch |
| 1138 | // before the virtual base initialization code. |
Reid Kleckner | 9063302 | 2013-06-19 15:20:38 +0000 | [diff] [blame] | 1139 | BaseCtorContinueBB = |
| 1140 | CGM.getCXXABI().EmitCtorCompleteObjectHandler(*this, ClassDecl); |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1141 | assert(BaseCtorContinueBB); |
| 1142 | } |
| 1143 | |
| 1144 | // Virtual base initializers first. |
| 1145 | for (; B != E && (*B)->isBaseInitializer() && (*B)->isBaseVirtual(); B++) { |
| 1146 | EmitBaseInitializer(*this, ClassDecl, *B, CtorType); |
| 1147 | } |
| 1148 | |
| 1149 | if (BaseCtorContinueBB) { |
| 1150 | // Complete object handler should continue to the remaining initializers. |
| 1151 | Builder.CreateBr(BaseCtorContinueBB); |
| 1152 | EmitBlock(BaseCtorContinueBB); |
| 1153 | } |
| 1154 | |
| 1155 | // Then, non-virtual base initializers. |
| 1156 | for (; B != E && (*B)->isBaseInitializer(); B++) { |
| 1157 | assert(!(*B)->isBaseVirtual()); |
| 1158 | EmitBaseInitializer(*this, ClassDecl, *B, CtorType); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1161 | InitializeVTablePointers(ClassDecl); |
Anders Carlsson | a78fa2c | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 1162 | |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1163 | // And finally, initialize class members. |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1164 | FieldConstructionScope FCS(*this, CXXThisValue); |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1165 | ConstructorMemcpyizer CM(*this, CD, Args); |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1166 | for (; B != E; B++) { |
| 1167 | CXXCtorInitializer *Member = (*B); |
| 1168 | assert(!Member->isBaseInitializer()); |
| 1169 | assert(Member->isAnyMemberInitializer() && |
| 1170 | "Delegating initializer on non-delegating constructor"); |
| 1171 | CM.addMemberInitializer(Member); |
| 1172 | } |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1173 | CM.finish(); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
Anders Carlsson | adf5dc3 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1176 | static bool |
| 1177 | FieldHasTrivialDestructorBody(ASTContext &Context, const FieldDecl *Field); |
| 1178 | |
| 1179 | static bool |
| 1180 | HasTrivialDestructorBody(ASTContext &Context, |
| 1181 | const CXXRecordDecl *BaseClassDecl, |
| 1182 | const CXXRecordDecl *MostDerivedClassDecl) |
| 1183 | { |
| 1184 | // If the destructor is trivial we don't have to check anything else. |
| 1185 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1186 | return true; |
| 1187 | |
| 1188 | if (!BaseClassDecl->getDestructor()->hasTrivialBody()) |
| 1189 | return false; |
| 1190 | |
| 1191 | // Check fields. |
| 1192 | for (CXXRecordDecl::field_iterator I = BaseClassDecl->field_begin(), |
| 1193 | E = BaseClassDecl->field_end(); I != E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1194 | const FieldDecl *Field = *I; |
Anders Carlsson | adf5dc3 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1195 | |
| 1196 | if (!FieldHasTrivialDestructorBody(Context, Field)) |
| 1197 | return false; |
| 1198 | } |
| 1199 | |
| 1200 | // Check non-virtual bases. |
| 1201 | for (CXXRecordDecl::base_class_const_iterator I = |
| 1202 | BaseClassDecl->bases_begin(), E = BaseClassDecl->bases_end(); |
| 1203 | I != E; ++I) { |
| 1204 | if (I->isVirtual()) |
| 1205 | continue; |
| 1206 | |
| 1207 | const CXXRecordDecl *NonVirtualBase = |
| 1208 | cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl()); |
| 1209 | if (!HasTrivialDestructorBody(Context, NonVirtualBase, |
| 1210 | MostDerivedClassDecl)) |
| 1211 | return false; |
| 1212 | } |
| 1213 | |
| 1214 | if (BaseClassDecl == MostDerivedClassDecl) { |
| 1215 | // Check virtual bases. |
| 1216 | for (CXXRecordDecl::base_class_const_iterator I = |
| 1217 | BaseClassDecl->vbases_begin(), E = BaseClassDecl->vbases_end(); |
| 1218 | I != E; ++I) { |
| 1219 | const CXXRecordDecl *VirtualBase = |
| 1220 | cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl()); |
| 1221 | if (!HasTrivialDestructorBody(Context, VirtualBase, |
| 1222 | MostDerivedClassDecl)) |
| 1223 | return false; |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | return true; |
| 1228 | } |
| 1229 | |
| 1230 | static bool |
| 1231 | FieldHasTrivialDestructorBody(ASTContext &Context, |
| 1232 | const FieldDecl *Field) |
| 1233 | { |
| 1234 | QualType FieldBaseElementType = Context.getBaseElementType(Field->getType()); |
| 1235 | |
| 1236 | const RecordType *RT = FieldBaseElementType->getAs<RecordType>(); |
| 1237 | if (!RT) |
| 1238 | return true; |
| 1239 | |
| 1240 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 1241 | return HasTrivialDestructorBody(Context, FieldClassDecl, FieldClassDecl); |
| 1242 | } |
| 1243 | |
Anders Carlsson | ffb945f | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 1244 | /// CanSkipVTablePointerInitialization - Check whether we need to initialize |
| 1245 | /// any vtable pointers before calling this destructor. |
| 1246 | static bool CanSkipVTablePointerInitialization(ASTContext &Context, |
Anders Carlsson | e3d6cf2 | 2011-05-16 04:08:36 +0000 | [diff] [blame] | 1247 | const CXXDestructorDecl *Dtor) { |
Anders Carlsson | ffb945f | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 1248 | if (!Dtor->hasTrivialBody()) |
| 1249 | return false; |
| 1250 | |
| 1251 | // Check the fields. |
| 1252 | const CXXRecordDecl *ClassDecl = Dtor->getParent(); |
| 1253 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 1254 | E = ClassDecl->field_end(); I != E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1255 | const FieldDecl *Field = *I; |
Anders Carlsson | ffb945f | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 1256 | |
Anders Carlsson | adf5dc3 | 2011-05-15 17:36:21 +0000 | [diff] [blame] | 1257 | if (!FieldHasTrivialDestructorBody(Context, Field)) |
| 1258 | return false; |
Anders Carlsson | ffb945f | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | return true; |
| 1262 | } |
| 1263 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1264 | /// EmitDestructorBody - Emits the body of the current destructor. |
| 1265 | void CodeGenFunction::EmitDestructorBody(FunctionArgList &Args) { |
| 1266 | const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CurGD.getDecl()); |
| 1267 | CXXDtorType DtorType = CurGD.getDtorType(); |
| 1268 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1269 | // The call to operator delete in a deleting destructor happens |
| 1270 | // outside of the function-try-block, which means it's always |
| 1271 | // possible to delegate the destructor body to the complete |
| 1272 | // destructor. Do so. |
| 1273 | if (DtorType == Dtor_Deleting) { |
| 1274 | EnterDtorCleanups(Dtor, Dtor_Deleting); |
| 1275 | EmitCXXDestructorCall(Dtor, Dtor_Complete, /*ForVirtualBase=*/false, |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 1276 | /*Delegating=*/false, LoadCXXThis()); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1277 | PopCleanupBlock(); |
| 1278 | return; |
| 1279 | } |
| 1280 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1281 | Stmt *Body = Dtor->getBody(); |
| 1282 | |
| 1283 | // If the body is a function-try-block, enter the try before |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1284 | // anything else. |
| 1285 | bool isTryBody = (Body && isa<CXXTryStmt>(Body)); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1286 | if (isTryBody) |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 1287 | EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1288 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1289 | // Enter the epilogue cleanups. |
| 1290 | RunCleanupsScope DtorEpilogue(*this); |
| 1291 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1292 | // If this is the complete variant, just invoke the base variant; |
| 1293 | // the epilogue will destruct the virtual bases. But we can't do |
| 1294 | // this optimization if the body is a function-try-block, because |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 1295 | // we'd introduce *two* handler blocks. In the Microsoft ABI, we |
| 1296 | // always delegate because we might not have a definition in this TU. |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1297 | switch (DtorType) { |
| 1298 | case Dtor_Deleting: llvm_unreachable("already handled deleting case"); |
| 1299 | |
| 1300 | case Dtor_Complete: |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 1301 | assert((Body || getTarget().getCXXABI().isMicrosoft()) && |
| 1302 | "can't emit a dtor without a body for non-Microsoft ABIs"); |
| 1303 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1304 | // Enter the cleanup scopes for virtual bases. |
| 1305 | EnterDtorCleanups(Dtor, Dtor_Complete); |
| 1306 | |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 1307 | if (!isTryBody) { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1308 | EmitCXXDestructorCall(Dtor, Dtor_Base, /*ForVirtualBase=*/false, |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 1309 | /*Delegating=*/false, LoadCXXThis()); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1310 | break; |
| 1311 | } |
| 1312 | // Fallthrough: act like we're in the base variant. |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1313 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1314 | case Dtor_Base: |
Reid Kleckner | a4130ba | 2013-07-22 13:51:44 +0000 | [diff] [blame] | 1315 | assert(Body); |
| 1316 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1317 | // Enter the cleanup scopes for fields and non-virtual bases. |
| 1318 | EnterDtorCleanups(Dtor, Dtor_Base); |
| 1319 | |
| 1320 | // Initialize the vtable pointers before entering the body. |
Anders Carlsson | ffb945f | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 1321 | if (!CanSkipVTablePointerInitialization(getContext(), Dtor)) |
| 1322 | InitializeVTablePointers(Dtor->getParent()); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1323 | |
| 1324 | if (isTryBody) |
| 1325 | EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock()); |
| 1326 | else if (Body) |
| 1327 | EmitStmt(Body); |
| 1328 | else { |
| 1329 | assert(Dtor->isImplicit() && "bodyless dtor not implicit"); |
| 1330 | // nothing to do besides what's in the epilogue |
| 1331 | } |
Fariborz Jahanian | 5abec14 | 2011-02-02 23:12:46 +0000 | [diff] [blame] | 1332 | // -fapple-kext must inline any call to this dtor into |
| 1333 | // the caller's body. |
Richard Smith | 7edf9e3 | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 1334 | if (getLangOpts().AppleKext) |
Bill Wendling | 72390b3 | 2012-12-20 19:27:06 +0000 | [diff] [blame] | 1335 | CurFn->addFnAttr(llvm::Attribute::AlwaysInline); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1336 | break; |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1339 | // Jump out through the epilogue cleanups. |
| 1340 | DtorEpilogue.ForceCleanup(); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1341 | |
| 1342 | // Exit the try if applicable. |
| 1343 | if (isTryBody) |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 1344 | ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Lang Hames | 56c00c4 | 2013-02-17 07:22:09 +0000 | [diff] [blame] | 1347 | void CodeGenFunction::emitImplicitAssignmentOperatorBody(FunctionArgList &Args) { |
| 1348 | const CXXMethodDecl *AssignOp = cast<CXXMethodDecl>(CurGD.getDecl()); |
| 1349 | const Stmt *RootS = AssignOp->getBody(); |
| 1350 | assert(isa<CompoundStmt>(RootS) && |
| 1351 | "Body of an implicit assignment operator should be compound stmt."); |
| 1352 | const CompoundStmt *RootCS = cast<CompoundStmt>(RootS); |
| 1353 | |
| 1354 | LexicalScope Scope(*this, RootCS->getSourceRange()); |
| 1355 | |
| 1356 | AssignmentMemcpyizer AM(*this, AssignOp, Args); |
| 1357 | for (CompoundStmt::const_body_iterator I = RootCS->body_begin(), |
| 1358 | E = RootCS->body_end(); |
| 1359 | I != E; ++I) { |
| 1360 | AM.emitAssignment(*I); |
| 1361 | } |
| 1362 | AM.finish(); |
| 1363 | } |
| 1364 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1365 | namespace { |
| 1366 | /// Call the operator delete associated with the current destructor. |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1367 | struct CallDtorDelete : EHScopeStack::Cleanup { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1368 | CallDtorDelete() {} |
| 1369 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1370 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1371 | const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl); |
| 1372 | const CXXRecordDecl *ClassDecl = Dtor->getParent(); |
| 1373 | CGF.EmitDeleteCall(Dtor->getOperatorDelete(), CGF.LoadCXXThis(), |
| 1374 | CGF.getContext().getTagDeclType(ClassDecl)); |
| 1375 | } |
| 1376 | }; |
| 1377 | |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1378 | struct CallDtorDeleteConditional : EHScopeStack::Cleanup { |
| 1379 | llvm::Value *ShouldDeleteCondition; |
| 1380 | public: |
| 1381 | CallDtorDeleteConditional(llvm::Value *ShouldDeleteCondition) |
| 1382 | : ShouldDeleteCondition(ShouldDeleteCondition) { |
| 1383 | assert(ShouldDeleteCondition != NULL); |
| 1384 | } |
| 1385 | |
| 1386 | void Emit(CodeGenFunction &CGF, Flags flags) { |
| 1387 | llvm::BasicBlock *callDeleteBB = CGF.createBasicBlock("dtor.call_delete"); |
| 1388 | llvm::BasicBlock *continueBB = CGF.createBasicBlock("dtor.continue"); |
| 1389 | llvm::Value *ShouldCallDelete |
| 1390 | = CGF.Builder.CreateIsNull(ShouldDeleteCondition); |
| 1391 | CGF.Builder.CreateCondBr(ShouldCallDelete, continueBB, callDeleteBB); |
| 1392 | |
| 1393 | CGF.EmitBlock(callDeleteBB); |
| 1394 | const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl); |
| 1395 | const CXXRecordDecl *ClassDecl = Dtor->getParent(); |
| 1396 | CGF.EmitDeleteCall(Dtor->getOperatorDelete(), CGF.LoadCXXThis(), |
| 1397 | CGF.getContext().getTagDeclType(ClassDecl)); |
| 1398 | CGF.Builder.CreateBr(continueBB); |
| 1399 | |
| 1400 | CGF.EmitBlock(continueBB); |
| 1401 | } |
| 1402 | }; |
| 1403 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1404 | class DestroyField : public EHScopeStack::Cleanup { |
| 1405 | const FieldDecl *field; |
Peter Collingbourne | 516bbd4 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 1406 | CodeGenFunction::Destroyer *destroyer; |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1407 | bool useEHCleanupForArray; |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1408 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1409 | public: |
| 1410 | DestroyField(const FieldDecl *field, CodeGenFunction::Destroyer *destroyer, |
| 1411 | bool useEHCleanupForArray) |
Peter Collingbourne | 516bbd4 | 2012-01-26 03:33:36 +0000 | [diff] [blame] | 1412 | : field(field), destroyer(destroyer), |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1413 | useEHCleanupForArray(useEHCleanupForArray) {} |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1414 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1415 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1416 | // Find the address of the field. |
| 1417 | llvm::Value *thisValue = CGF.LoadCXXThis(); |
Eli Friedman | 377ecc7 | 2012-04-16 03:54:45 +0000 | [diff] [blame] | 1418 | QualType RecordTy = CGF.getContext().getTagDeclType(field->getParent()); |
| 1419 | LValue ThisLV = CGF.MakeAddrLValue(thisValue, RecordTy); |
| 1420 | LValue LV = CGF.EmitLValueForField(ThisLV, field); |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1421 | assert(LV.isSimple()); |
| 1422 | |
| 1423 | CGF.emitDestroy(LV.getAddress(), field->getType(), destroyer, |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1424 | flags.isForNormalCleanup() && useEHCleanupForArray); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1425 | } |
| 1426 | }; |
| 1427 | } |
| 1428 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1429 | /// EmitDtorEpilogue - Emit all code that comes at the end of class's |
| 1430 | /// destructor. This is to call destructors on members and base classes |
| 1431 | /// in reverse order of their construction. |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1432 | void CodeGenFunction::EnterDtorCleanups(const CXXDestructorDecl *DD, |
| 1433 | CXXDtorType DtorType) { |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1434 | assert(!DD->isTrivial() && |
| 1435 | "Should not emit dtor epilogue for trivial dtor!"); |
| 1436 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1437 | // The deleting-destructor phase just needs to call the appropriate |
| 1438 | // operator delete that Sema picked up. |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1439 | if (DtorType == Dtor_Deleting) { |
| 1440 | assert(DD->getOperatorDelete() && |
| 1441 | "operator delete missing - EmitDtorEpilogue"); |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1442 | if (CXXStructorImplicitParamValue) { |
| 1443 | // If there is an implicit param to the deleting dtor, it's a boolean |
| 1444 | // telling whether we should call delete at the end of the dtor. |
| 1445 | EHStack.pushCleanup<CallDtorDeleteConditional>( |
| 1446 | NormalAndEHCleanup, CXXStructorImplicitParamValue); |
| 1447 | } else { |
| 1448 | EHStack.pushCleanup<CallDtorDelete>(NormalAndEHCleanup); |
| 1449 | } |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1450 | return; |
| 1451 | } |
| 1452 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1453 | const CXXRecordDecl *ClassDecl = DD->getParent(); |
| 1454 | |
Richard Smith | 416f63e | 2011-09-18 12:11:43 +0000 | [diff] [blame] | 1455 | // Unions have no bases and do not call field destructors. |
| 1456 | if (ClassDecl->isUnion()) |
| 1457 | return; |
| 1458 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1459 | // The complete-destructor phase just destructs all the virtual bases. |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1460 | if (DtorType == Dtor_Complete) { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1461 | |
| 1462 | // We push them in the forward order so that they'll be popped in |
| 1463 | // the reverse order. |
| 1464 | for (CXXRecordDecl::base_class_const_iterator I = |
| 1465 | ClassDecl->vbases_begin(), E = ClassDecl->vbases_end(); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1466 | I != E; ++I) { |
| 1467 | const CXXBaseSpecifier &Base = *I; |
| 1468 | CXXRecordDecl *BaseClassDecl |
| 1469 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| 1470 | |
| 1471 | // Ignore trivial destructors. |
| 1472 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1473 | continue; |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1474 | |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1475 | EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup, |
| 1476 | BaseClassDecl, |
| 1477 | /*BaseIsVirtual*/ true); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1478 | } |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1479 | |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1480 | return; |
| 1481 | } |
| 1482 | |
| 1483 | assert(DtorType == Dtor_Base); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1484 | |
| 1485 | // Destroy non-virtual bases. |
| 1486 | for (CXXRecordDecl::base_class_const_iterator I = |
| 1487 | ClassDecl->bases_begin(), E = ClassDecl->bases_end(); I != E; ++I) { |
| 1488 | const CXXBaseSpecifier &Base = *I; |
| 1489 | |
| 1490 | // Ignore virtual bases. |
| 1491 | if (Base.isVirtual()) |
| 1492 | continue; |
| 1493 | |
| 1494 | CXXRecordDecl *BaseClassDecl = Base.getType()->getAsCXXRecordDecl(); |
| 1495 | |
| 1496 | // Ignore trivial destructors. |
| 1497 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1498 | continue; |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1499 | |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1500 | EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup, |
| 1501 | BaseClassDecl, |
| 1502 | /*BaseIsVirtual*/ false); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | // Destroy direct fields. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1506 | SmallVector<const FieldDecl *, 16> FieldDecls; |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1507 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 1508 | E = ClassDecl->field_end(); I != E; ++I) { |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1509 | const FieldDecl *field = *I; |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1510 | QualType type = field->getType(); |
| 1511 | QualType::DestructionKind dtorKind = type.isDestructedType(); |
| 1512 | if (!dtorKind) continue; |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 1513 | |
Richard Smith | 9a561d5 | 2012-02-26 09:11:52 +0000 | [diff] [blame] | 1514 | // Anonymous union members do not have their destructors called. |
| 1515 | const RecordType *RT = type->getAsUnionType(); |
| 1516 | if (RT && RT->getDecl()->isAnonymousStructOrUnion()) continue; |
| 1517 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 1518 | CleanupKind cleanupKind = getCleanupKind(dtorKind); |
| 1519 | EHStack.pushCleanup<DestroyField>(cleanupKind, field, |
| 1520 | getDestroyer(dtorKind), |
| 1521 | cleanupKind & EHCleanup); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1522 | } |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1525 | /// EmitCXXAggrConstructorCall - Emit a loop to call a particular |
| 1526 | /// constructor for each of several members of an array. |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 1527 | /// |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1528 | /// \param ctor the constructor to call for each element |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1529 | /// \param arrayType the type of the array to initialize |
| 1530 | /// \param arrayBegin an arrayType* |
| 1531 | /// \param zeroInitialize true if each element should be |
| 1532 | /// zero-initialized before it is constructed |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1533 | void |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1534 | CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *ctor, |
| 1535 | const ConstantArrayType *arrayType, |
| 1536 | llvm::Value *arrayBegin, |
| 1537 | CallExpr::const_arg_iterator argBegin, |
| 1538 | CallExpr::const_arg_iterator argEnd, |
| 1539 | bool zeroInitialize) { |
| 1540 | QualType elementType; |
| 1541 | llvm::Value *numElements = |
| 1542 | emitArrayLength(arrayType, elementType, arrayBegin); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1543 | |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1544 | EmitCXXAggrConstructorCall(ctor, numElements, arrayBegin, |
| 1545 | argBegin, argEnd, zeroInitialize); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1548 | /// EmitCXXAggrConstructorCall - Emit a loop to call a particular |
| 1549 | /// constructor for each of several members of an array. |
| 1550 | /// |
| 1551 | /// \param ctor the constructor to call for each element |
| 1552 | /// \param numElements the number of elements in the array; |
John McCall | dd376ca | 2011-07-13 07:37:11 +0000 | [diff] [blame] | 1553 | /// may be zero |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1554 | /// \param arrayBegin a T*, where T is the type constructed by ctor |
| 1555 | /// \param zeroInitialize true if each element should be |
| 1556 | /// zero-initialized before it is constructed |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1557 | void |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1558 | CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *ctor, |
| 1559 | llvm::Value *numElements, |
| 1560 | llvm::Value *arrayBegin, |
| 1561 | CallExpr::const_arg_iterator argBegin, |
| 1562 | CallExpr::const_arg_iterator argEnd, |
| 1563 | bool zeroInitialize) { |
John McCall | dd376ca | 2011-07-13 07:37:11 +0000 | [diff] [blame] | 1564 | |
| 1565 | // It's legal for numElements to be zero. This can happen both |
| 1566 | // dynamically, because x can be zero in 'new A[x]', and statically, |
| 1567 | // because of GCC extensions that permit zero-length arrays. There |
| 1568 | // are probably legitimate places where we could assume that this |
| 1569 | // doesn't happen, but it's not clear that it's worth it. |
| 1570 | llvm::BranchInst *zeroCheckBranch = 0; |
| 1571 | |
| 1572 | // Optimize for a constant count. |
| 1573 | llvm::ConstantInt *constantCount |
| 1574 | = dyn_cast<llvm::ConstantInt>(numElements); |
| 1575 | if (constantCount) { |
| 1576 | // Just skip out if the constant count is zero. |
| 1577 | if (constantCount->isZero()) return; |
| 1578 | |
| 1579 | // Otherwise, emit the check. |
| 1580 | } else { |
| 1581 | llvm::BasicBlock *loopBB = createBasicBlock("new.ctorloop"); |
| 1582 | llvm::Value *iszero = Builder.CreateIsNull(numElements, "isempty"); |
| 1583 | zeroCheckBranch = Builder.CreateCondBr(iszero, loopBB, loopBB); |
| 1584 | EmitBlock(loopBB); |
| 1585 | } |
| 1586 | |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1587 | // Find the end of the array. |
| 1588 | llvm::Value *arrayEnd = Builder.CreateInBoundsGEP(arrayBegin, numElements, |
| 1589 | "arrayctor.end"); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1590 | |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1591 | // Enter the loop, setting up a phi for the current location to initialize. |
| 1592 | llvm::BasicBlock *entryBB = Builder.GetInsertBlock(); |
| 1593 | llvm::BasicBlock *loopBB = createBasicBlock("arrayctor.loop"); |
| 1594 | EmitBlock(loopBB); |
| 1595 | llvm::PHINode *cur = Builder.CreatePHI(arrayBegin->getType(), 2, |
| 1596 | "arrayctor.cur"); |
| 1597 | cur->addIncoming(arrayBegin, entryBB); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1598 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1599 | // Inside the loop body, emit the constructor call on the array element. |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1600 | |
| 1601 | QualType type = getContext().getTypeDeclType(ctor->getParent()); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1602 | |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 1603 | // Zero initialize the storage, if requested. |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1604 | if (zeroInitialize) |
| 1605 | EmitNullInitialization(cur, type); |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 1606 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1607 | // C++ [class.temporary]p4: |
| 1608 | // There are two contexts in which temporaries are destroyed at a different |
| 1609 | // point than the end of the full-expression. The first context is when a |
| 1610 | // default constructor is called to initialize an element of an array. |
| 1611 | // If the constructor has one or more default arguments, the destruction of |
| 1612 | // every temporary created in a default argument expression is sequenced |
| 1613 | // before the construction of the next array element, if any. |
| 1614 | |
Anders Carlsson | 44ec82b | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 1615 | { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1616 | RunCleanupsScope Scope(*this); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1617 | |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1618 | // Evaluate the constructor and its arguments in a regular |
| 1619 | // partial-destroy cleanup. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1620 | if (getLangOpts().Exceptions && |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1621 | !ctor->getParent()->hasTrivialDestructor()) { |
| 1622 | Destroyer *destroyer = destroyCXXObject; |
| 1623 | pushRegularPartialArrayCleanup(arrayBegin, cur, type, *destroyer); |
| 1624 | } |
| 1625 | |
| 1626 | EmitCXXConstructorCall(ctor, Ctor_Complete, /*ForVirtualBase=*/ false, |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 1627 | /*Delegating=*/false, cur, argBegin, argEnd); |
Anders Carlsson | 44ec82b | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 1628 | } |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1629 | |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1630 | // Go to the next element. |
| 1631 | llvm::Value *next = |
| 1632 | Builder.CreateInBoundsGEP(cur, llvm::ConstantInt::get(SizeTy, 1), |
| 1633 | "arrayctor.next"); |
| 1634 | cur->addIncoming(next, Builder.GetInsertBlock()); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1635 | |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1636 | // Check whether that's the end of the loop. |
| 1637 | llvm::Value *done = Builder.CreateICmpEQ(next, arrayEnd, "arrayctor.done"); |
| 1638 | llvm::BasicBlock *contBB = createBasicBlock("arrayctor.cont"); |
| 1639 | Builder.CreateCondBr(done, contBB, loopBB); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1640 | |
John McCall | dd376ca | 2011-07-13 07:37:11 +0000 | [diff] [blame] | 1641 | // Patch the earlier check to skip over the loop. |
| 1642 | if (zeroCheckBranch) zeroCheckBranch->setSuccessor(0, contBB); |
| 1643 | |
John McCall | c3c0766 | 2011-07-13 06:10:41 +0000 | [diff] [blame] | 1644 | EmitBlock(contBB); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1645 | } |
| 1646 | |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1647 | void CodeGenFunction::destroyCXXObject(CodeGenFunction &CGF, |
| 1648 | llvm::Value *addr, |
| 1649 | QualType type) { |
| 1650 | const RecordType *rtype = type->castAs<RecordType>(); |
| 1651 | const CXXRecordDecl *record = cast<CXXRecordDecl>(rtype->getDecl()); |
| 1652 | const CXXDestructorDecl *dtor = record->getDestructor(); |
| 1653 | assert(!dtor->isTrivial()); |
| 1654 | CGF.EmitCXXDestructorCall(dtor, Dtor_Complete, /*for vbase*/ false, |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 1655 | /*Delegating=*/false, addr); |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1658 | void |
| 1659 | CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, |
Anders Carlsson | 155ed4a | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 1660 | CXXCtorType Type, bool ForVirtualBase, |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 1661 | bool Delegating, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1662 | llvm::Value *This, |
| 1663 | CallExpr::const_arg_iterator ArgBeg, |
| 1664 | CallExpr::const_arg_iterator ArgEnd) { |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1665 | // If this is a trivial constructor, just emit what's needed. |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1666 | if (D->isTrivial()) { |
| 1667 | if (ArgBeg == ArgEnd) { |
| 1668 | // Trivial default constructor, no codegen required. |
| 1669 | assert(D->isDefaultConstructor() && |
| 1670 | "trivial 0-arg ctor not a default ctor"); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1671 | return; |
| 1672 | } |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1673 | |
| 1674 | assert(ArgBeg + 1 == ArgEnd && "unexpected argcount for trivial ctor"); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 1675 | assert(D->isCopyOrMoveConstructor() && |
| 1676 | "trivial 1-arg ctor not a copy/move ctor"); |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1677 | |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1678 | const Expr *E = (*ArgBeg); |
| 1679 | QualType Ty = E->getType(); |
| 1680 | llvm::Value *Src = EmitLValue(E).getAddress(); |
| 1681 | EmitAggregateCopy(This, Src, Ty); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1682 | return; |
| 1683 | } |
| 1684 | |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1685 | // Non-trivial constructors are handled in an ABI-specific manner. |
Stephen Lin | 3b50e8d | 2013-06-30 20:40:16 +0000 | [diff] [blame] | 1686 | CGM.getCXXABI().EmitConstructorCall(*this, D, Type, ForVirtualBase, |
| 1687 | Delegating, This, ArgBeg, ArgEnd); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1690 | void |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1691 | CodeGenFunction::EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D, |
| 1692 | llvm::Value *This, llvm::Value *Src, |
| 1693 | CallExpr::const_arg_iterator ArgBeg, |
| 1694 | CallExpr::const_arg_iterator ArgEnd) { |
| 1695 | if (D->isTrivial()) { |
| 1696 | assert(ArgBeg + 1 == ArgEnd && "unexpected argcount for trivial ctor"); |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 1697 | assert(D->isCopyOrMoveConstructor() && |
| 1698 | "trivial 1-arg ctor not a copy/move ctor"); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1699 | EmitAggregateCopy(This, Src, (*ArgBeg)->getType()); |
| 1700 | return; |
| 1701 | } |
Nick Lewycky | 62a3bba | 2013-09-11 02:03:20 +0000 | [diff] [blame] | 1702 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, clang::Ctor_Complete); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1703 | assert(D->isInstance() && |
| 1704 | "Trying to emit a member call expr on a static method!"); |
| 1705 | |
| 1706 | const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>(); |
| 1707 | |
| 1708 | CallArgList Args; |
| 1709 | |
| 1710 | // Push the this ptr. |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1711 | Args.add(RValue::get(This), D->getThisType(getContext())); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1712 | |
| 1713 | |
| 1714 | // Push the src ptr. |
| 1715 | QualType QT = *(FPT->arg_type_begin()); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1716 | llvm::Type *t = CGM.getTypes().ConvertType(QT); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1717 | Src = Builder.CreateBitCast(Src, t); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1718 | Args.add(RValue::get(Src), QT); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1719 | |
| 1720 | // Skip over first argument (Src). |
| 1721 | ++ArgBeg; |
| 1722 | CallExpr::const_arg_iterator Arg = ArgBeg; |
| 1723 | for (FunctionProtoType::arg_type_iterator I = FPT->arg_type_begin()+1, |
| 1724 | E = FPT->arg_type_end(); I != E; ++I, ++Arg) { |
| 1725 | assert(Arg != ArgEnd && "Running over edge of argument list!"); |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1726 | EmitCallArg(Args, *Arg, *I); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1727 | } |
| 1728 | // Either we've emitted all the call args, or we have a call to a |
| 1729 | // variadic function. |
| 1730 | assert((Arg == ArgEnd || FPT->isVariadic()) && |
| 1731 | "Extra arguments in non-variadic function!"); |
| 1732 | // If we still have any arguments, emit them using the type of the argument. |
| 1733 | for (; Arg != ArgEnd; ++Arg) { |
| 1734 | QualType ArgType = Arg->getType(); |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1735 | EmitCallArg(Args, *Arg, ArgType); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 1738 | EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, RequiredArgs::All), |
| 1739 | Callee, ReturnValueSlot(), Args, D); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | void |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1743 | CodeGenFunction::EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor, |
| 1744 | CXXCtorType CtorType, |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1745 | const FunctionArgList &Args, |
| 1746 | SourceLocation Loc) { |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1747 | CallArgList DelegateArgs; |
| 1748 | |
| 1749 | FunctionArgList::const_iterator I = Args.begin(), E = Args.end(); |
| 1750 | assert(I != E && "no parameters to constructor"); |
| 1751 | |
| 1752 | // this |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1753 | DelegateArgs.add(RValue::get(LoadCXXThis()), (*I)->getType()); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1754 | ++I; |
| 1755 | |
| 1756 | // vtt |
Timur Iskhodzhanov | 1d4fff5 | 2013-02-27 13:46:31 +0000 | [diff] [blame] | 1757 | if (llvm::Value *VTT = GetVTTParameter(GlobalDecl(Ctor, CtorType), |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 1758 | /*ForVirtualBase=*/false, |
| 1759 | /*Delegating=*/true)) { |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1760 | QualType VoidPP = getContext().getPointerType(getContext().VoidPtrTy); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1761 | DelegateArgs.add(RValue::get(VTT), VoidPP); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1762 | |
Peter Collingbourne | e1e35f7 | 2013-06-28 20:45:28 +0000 | [diff] [blame] | 1763 | if (CGM.getCXXABI().NeedsVTTParameter(CurGD)) { |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1764 | assert(I != E && "cannot skip vtt parameter, already done with args"); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1765 | assert((*I)->getType() == VoidPP && "skipping parameter not of vtt type"); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1766 | ++I; |
| 1767 | } |
| 1768 | } |
| 1769 | |
| 1770 | // Explicit arguments. |
| 1771 | for (; I != E; ++I) { |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1772 | const VarDecl *param = *I; |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 1773 | // FIXME: per-argument source location |
| 1774 | EmitDelegateCallArg(DelegateArgs, param, Loc); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
Manman Ren | 63fd408 | 2013-03-20 16:59:38 +0000 | [diff] [blame] | 1777 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(Ctor, CtorType); |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1778 | EmitCall(CGM.getTypes().arrangeCXXConstructorDeclaration(Ctor, CtorType), |
Manman Ren | 63fd408 | 2013-03-20 16:59:38 +0000 | [diff] [blame] | 1779 | Callee, ReturnValueSlot(), DelegateArgs, Ctor); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 1782 | namespace { |
| 1783 | struct CallDelegatingCtorDtor : EHScopeStack::Cleanup { |
| 1784 | const CXXDestructorDecl *Dtor; |
| 1785 | llvm::Value *Addr; |
| 1786 | CXXDtorType Type; |
| 1787 | |
| 1788 | CallDelegatingCtorDtor(const CXXDestructorDecl *D, llvm::Value *Addr, |
| 1789 | CXXDtorType Type) |
| 1790 | : Dtor(D), Addr(Addr), Type(Type) {} |
| 1791 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1792 | void Emit(CodeGenFunction &CGF, Flags flags) { |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 1793 | CGF.EmitCXXDestructorCall(Dtor, Type, /*ForVirtualBase=*/false, |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 1794 | /*Delegating=*/true, Addr); |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 1795 | } |
| 1796 | }; |
| 1797 | } |
| 1798 | |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 1799 | void |
| 1800 | CodeGenFunction::EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor, |
| 1801 | const FunctionArgList &Args) { |
| 1802 | assert(Ctor->isDelegatingConstructor()); |
| 1803 | |
| 1804 | llvm::Value *ThisPtr = LoadCXXThis(); |
| 1805 | |
Eli Friedman | f394078 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 1806 | QualType Ty = getContext().getTagDeclType(Ctor->getParent()); |
Eli Friedman | d7722d9 | 2011-12-03 02:13:40 +0000 | [diff] [blame] | 1807 | CharUnits Alignment = getContext().getTypeAlignInChars(Ty); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1808 | AggValueSlot AggSlot = |
Eli Friedman | f394078 | 2011-12-03 00:54:26 +0000 | [diff] [blame] | 1809 | AggValueSlot::forAddr(ThisPtr, Alignment, Qualifiers(), |
John McCall | 7c2349b | 2011-08-25 20:40:09 +0000 | [diff] [blame] | 1810 | AggValueSlot::IsDestructed, |
John McCall | 410ffb2 | 2011-08-25 23:04:34 +0000 | [diff] [blame] | 1811 | AggValueSlot::DoesNotNeedGCBarriers, |
Chad Rosier | 649b4a1 | 2012-03-29 17:37:10 +0000 | [diff] [blame] | 1812 | AggValueSlot::IsNotAliased); |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 1813 | |
| 1814 | EmitAggExpr(Ctor->init_begin()[0]->getInit(), AggSlot); |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 1815 | |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 1816 | const CXXRecordDecl *ClassDecl = Ctor->getParent(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1817 | if (CGM.getLangOpts().Exceptions && !ClassDecl->hasTrivialDestructor()) { |
Sean Hunt | b76af9c | 2011-05-03 23:05:34 +0000 | [diff] [blame] | 1818 | CXXDtorType Type = |
| 1819 | CurGD.getCtorType() == Ctor_Complete ? Dtor_Complete : Dtor_Base; |
| 1820 | |
| 1821 | EHStack.pushCleanup<CallDelegatingCtorDtor>(EHCleanup, |
| 1822 | ClassDecl->getDestructor(), |
| 1823 | ThisPtr, Type); |
| 1824 | } |
| 1825 | } |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 1826 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1827 | void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *DD, |
| 1828 | CXXDtorType Type, |
Anders Carlsson | 8e6404c | 2010-05-02 23:29:11 +0000 | [diff] [blame] | 1829 | bool ForVirtualBase, |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 1830 | bool Delegating, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1831 | llvm::Value *This) { |
Timur Iskhodzhanov | 8255274 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 1832 | GlobalDecl GD(DD, Type); |
| 1833 | llvm::Value *VTT = GetVTTParameter(GD, ForVirtualBase, Delegating); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 1834 | llvm::Value *Callee = 0; |
Richard Smith | 7edf9e3 | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 1835 | if (getLangOpts().AppleKext) |
Fariborz Jahanian | 771c678 | 2011-02-03 19:27:17 +0000 | [diff] [blame] | 1836 | Callee = BuildAppleKextVirtualDestructorCall(DD, Type, |
| 1837 | DD->getParent()); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 1838 | |
| 1839 | if (!Callee) |
| 1840 | Callee = CGM.GetAddrOfCXXDestructor(DD, Type); |
Timur Iskhodzhanov | 8255274 | 2013-10-16 18:24:06 +0000 | [diff] [blame] | 1841 | |
| 1842 | if (DD->isVirtual()) |
| 1843 | This = CGM.getCXXABI().adjustThisArgumentForVirtualCall(*this, GD, This); |
| 1844 | |
Richard Smith | 4def70d | 2012-10-09 19:52:38 +0000 | [diff] [blame] | 1845 | // FIXME: Provide a source location here. |
| 1846 | EmitCXXMemberCall(DD, SourceLocation(), Callee, ReturnValueSlot(), This, |
Timur Iskhodzhanov | 59660c2 | 2013-02-13 08:37:51 +0000 | [diff] [blame] | 1847 | VTT, getContext().getPointerType(getContext().VoidPtrTy), |
| 1848 | 0, 0); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
John McCall | 291ae94 | 2010-07-21 01:41:18 +0000 | [diff] [blame] | 1851 | namespace { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1852 | struct CallLocalDtor : EHScopeStack::Cleanup { |
John McCall | 291ae94 | 2010-07-21 01:41:18 +0000 | [diff] [blame] | 1853 | const CXXDestructorDecl *Dtor; |
| 1854 | llvm::Value *Addr; |
| 1855 | |
| 1856 | CallLocalDtor(const CXXDestructorDecl *D, llvm::Value *Addr) |
| 1857 | : Dtor(D), Addr(Addr) {} |
| 1858 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1859 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | 291ae94 | 2010-07-21 01:41:18 +0000 | [diff] [blame] | 1860 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, |
Douglas Gregor | 378e1e7 | 2013-01-31 05:50:40 +0000 | [diff] [blame] | 1861 | /*ForVirtualBase=*/false, |
| 1862 | /*Delegating=*/false, Addr); |
John McCall | 291ae94 | 2010-07-21 01:41:18 +0000 | [diff] [blame] | 1863 | } |
| 1864 | }; |
| 1865 | } |
| 1866 | |
John McCall | 81407d4 | 2010-07-21 06:29:51 +0000 | [diff] [blame] | 1867 | void CodeGenFunction::PushDestructorCleanup(const CXXDestructorDecl *D, |
| 1868 | llvm::Value *Addr) { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1869 | EHStack.pushCleanup<CallLocalDtor>(NormalAndEHCleanup, D, Addr); |
John McCall | 81407d4 | 2010-07-21 06:29:51 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1872 | void CodeGenFunction::PushDestructorCleanup(QualType T, llvm::Value *Addr) { |
| 1873 | CXXRecordDecl *ClassDecl = T->getAsCXXRecordDecl(); |
| 1874 | if (!ClassDecl) return; |
| 1875 | if (ClassDecl->hasTrivialDestructor()) return; |
| 1876 | |
| 1877 | const CXXDestructorDecl *D = ClassDecl->getDestructor(); |
John McCall | 642a75f | 2011-04-28 02:15:35 +0000 | [diff] [blame] | 1878 | assert(D && D->isUsed() && "destructor not marked as used!"); |
John McCall | 81407d4 | 2010-07-21 06:29:51 +0000 | [diff] [blame] | 1879 | PushDestructorCleanup(D, Addr); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1880 | } |
| 1881 | |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1882 | void |
| 1883 | CodeGenFunction::InitializeVTablePointer(BaseSubobject Base, |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1884 | const CXXRecordDecl *NearestVBase, |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1885 | CharUnits OffsetFromNearestVBase, |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1886 | const CXXRecordDecl *VTableClass) { |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1887 | // Compute the address point. |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1888 | bool NeedsVirtualOffset; |
| 1889 | llvm::Value *VTableAddressPoint = |
| 1890 | CGM.getCXXABI().getVTableAddressPointInStructor( |
| 1891 | *this, VTableClass, Base, NearestVBase, NeedsVirtualOffset); |
| 1892 | if (!VTableAddressPoint) |
| 1893 | return; |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1894 | |
Anders Carlsson | 36fd6be | 2010-04-20 16:22:16 +0000 | [diff] [blame] | 1895 | // Compute where to store the address point. |
Anders Carlsson | 8246cc7 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 1896 | llvm::Value *VirtualOffset = 0; |
Ken Dyck | 9a8ad9b | 2011-03-23 00:45:26 +0000 | [diff] [blame] | 1897 | CharUnits NonVirtualOffset = CharUnits::Zero(); |
Anders Carlsson | 3e79c30 | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 1898 | |
Timur Iskhodzhanov | a53d7a0 | 2013-09-27 14:48:01 +0000 | [diff] [blame] | 1899 | if (NeedsVirtualOffset) { |
Anders Carlsson | 3e79c30 | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 1900 | // We need to use the virtual base offset offset because the virtual base |
| 1901 | // might have a different offset in the most derived class. |
Reid Kleckner | b0f533e | 2013-05-29 18:02:47 +0000 | [diff] [blame] | 1902 | VirtualOffset = CGM.getCXXABI().GetVirtualBaseClassOffset(*this, |
| 1903 | LoadCXXThis(), |
| 1904 | VTableClass, |
| 1905 | NearestVBase); |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1906 | NonVirtualOffset = OffsetFromNearestVBase; |
Anders Carlsson | 3e79c30 | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 1907 | } else { |
Anders Carlsson | 8246cc7 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 1908 | // We can just use the base offset in the complete class. |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 1909 | NonVirtualOffset = Base.getBaseOffset(); |
Anders Carlsson | 3e79c30 | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 1910 | } |
Anders Carlsson | 8246cc7 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 1911 | |
| 1912 | // Apply the offsets. |
| 1913 | llvm::Value *VTableField = LoadCXXThis(); |
| 1914 | |
Ken Dyck | 9a8ad9b | 2011-03-23 00:45:26 +0000 | [diff] [blame] | 1915 | if (!NonVirtualOffset.isZero() || VirtualOffset) |
Anders Carlsson | 8246cc7 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 1916 | VTableField = ApplyNonVirtualAndVirtualOffset(*this, VTableField, |
| 1917 | NonVirtualOffset, |
| 1918 | VirtualOffset); |
Anders Carlsson | 36fd6be | 2010-04-20 16:22:16 +0000 | [diff] [blame] | 1919 | |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1920 | // Finally, store the address point. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1921 | llvm::Type *AddressPointPtrTy = |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1922 | VTableAddressPoint->getType()->getPointerTo(); |
| 1923 | VTableField = Builder.CreateBitCast(VTableField, AddressPointPtrTy); |
Kostya Serebryany | 8cb4a07 | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 1924 | llvm::StoreInst *Store = Builder.CreateStore(VTableAddressPoint, VTableField); |
| 1925 | CGM.DecorateInstruction(Store, CGM.getTBAAInfoForVTablePtr()); |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1928 | void |
| 1929 | CodeGenFunction::InitializeVTablePointers(BaseSubobject Base, |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1930 | const CXXRecordDecl *NearestVBase, |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1931 | CharUnits OffsetFromNearestVBase, |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1932 | bool BaseIsNonVirtualPrimaryBase, |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1933 | const CXXRecordDecl *VTableClass, |
| 1934 | VisitedVirtualBasesSetTy& VBases) { |
| 1935 | // If this base is a non-virtual primary base the address point has already |
| 1936 | // been set. |
| 1937 | if (!BaseIsNonVirtualPrimaryBase) { |
| 1938 | // Initialize the vtable pointer for this base. |
Anders Carlsson | 4235840 | 2010-05-03 00:07:07 +0000 | [diff] [blame] | 1939 | InitializeVTablePointer(Base, NearestVBase, OffsetFromNearestVBase, |
Timur Iskhodzhanov | 7f918f9 | 2013-08-21 17:33:16 +0000 | [diff] [blame] | 1940 | VTableClass); |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | const CXXRecordDecl *RD = Base.getBase(); |
| 1944 | |
| 1945 | // Traverse bases. |
| 1946 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1947 | E = RD->bases_end(); I != E; ++I) { |
| 1948 | CXXRecordDecl *BaseDecl |
| 1949 | = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1950 | |
| 1951 | // Ignore classes without a vtable. |
| 1952 | if (!BaseDecl->isDynamicClass()) |
| 1953 | continue; |
| 1954 | |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1955 | CharUnits BaseOffset; |
| 1956 | CharUnits BaseOffsetFromNearestVBase; |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1957 | bool BaseDeclIsNonVirtualPrimaryBase; |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1958 | |
| 1959 | if (I->isVirtual()) { |
| 1960 | // Check if we've visited this virtual base before. |
| 1961 | if (!VBases.insert(BaseDecl)) |
| 1962 | continue; |
| 1963 | |
| 1964 | const ASTRecordLayout &Layout = |
| 1965 | getContext().getASTRecordLayout(VTableClass); |
| 1966 | |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1967 | BaseOffset = Layout.getVBaseClassOffset(BaseDecl); |
| 1968 | BaseOffsetFromNearestVBase = CharUnits::Zero(); |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1969 | BaseDeclIsNonVirtualPrimaryBase = false; |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1970 | } else { |
| 1971 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 1972 | |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 1973 | BaseOffset = Base.getBaseOffset() + Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | 4235840 | 2010-05-03 00:07:07 +0000 | [diff] [blame] | 1974 | BaseOffsetFromNearestVBase = |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1975 | OffsetFromNearestVBase + Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1976 | BaseDeclIsNonVirtualPrimaryBase = Layout.getPrimaryBase() == BaseDecl; |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 1979 | InitializeVTablePointers(BaseSubobject(BaseDecl, BaseOffset), |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1980 | I->isVirtual() ? BaseDecl : NearestVBase, |
Anders Carlsson | 4235840 | 2010-05-03 00:07:07 +0000 | [diff] [blame] | 1981 | BaseOffsetFromNearestVBase, |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1982 | BaseDeclIsNonVirtualPrimaryBase, |
Timur Iskhodzhanov | 7f918f9 | 2013-08-21 17:33:16 +0000 | [diff] [blame] | 1983 | VTableClass, VBases); |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | void CodeGenFunction::InitializeVTablePointers(const CXXRecordDecl *RD) { |
| 1988 | // Ignore classes without a vtable. |
Anders Carlsson | 0703690 | 2010-03-26 04:39:42 +0000 | [diff] [blame] | 1989 | if (!RD->isDynamicClass()) |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1990 | return; |
| 1991 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1992 | // Initialize the vtable pointers for this class and all of its bases. |
| 1993 | VisitedVirtualBasesSetTy VBases; |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 1994 | InitializeVTablePointers(BaseSubobject(RD, CharUnits::Zero()), |
| 1995 | /*NearestVBase=*/0, |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1996 | /*OffsetFromNearestVBase=*/CharUnits::Zero(), |
Timur Iskhodzhanov | 7f918f9 | 2013-08-21 17:33:16 +0000 | [diff] [blame] | 1997 | /*BaseIsNonVirtualPrimaryBase=*/false, RD, VBases); |
Timur Iskhodzhanov | 5bd0d44 | 2013-10-09 18:16:58 +0000 | [diff] [blame] | 1998 | |
| 1999 | if (RD->getNumVBases()) |
| 2000 | CGM.getCXXABI().initializeHiddenVirtualInheritanceMembers(*this, RD); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 2001 | } |
Dan Gohman | 043fb9a | 2010-10-26 18:44:08 +0000 | [diff] [blame] | 2002 | |
| 2003 | llvm::Value *CodeGenFunction::GetVTablePtr(llvm::Value *This, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2004 | llvm::Type *Ty) { |
Dan Gohman | 043fb9a | 2010-10-26 18:44:08 +0000 | [diff] [blame] | 2005 | llvm::Value *VTablePtrSrc = Builder.CreateBitCast(This, Ty->getPointerTo()); |
Kostya Serebryany | 8cb4a07 | 2012-03-26 17:03:51 +0000 | [diff] [blame] | 2006 | llvm::Instruction *VTable = Builder.CreateLoad(VTablePtrSrc, "vtable"); |
| 2007 | CGM.DecorateInstruction(VTable, CGM.getTBAAInfoForVTablePtr()); |
| 2008 | return VTable; |
Dan Gohman | 043fb9a | 2010-10-26 18:44:08 +0000 | [diff] [blame] | 2009 | } |
Anders Carlsson | a2447e0 | 2011-05-08 20:32:23 +0000 | [diff] [blame] | 2010 | |
Anders Carlsson | a2447e0 | 2011-05-08 20:32:23 +0000 | [diff] [blame] | 2011 | |
| 2012 | // FIXME: Ideally Expr::IgnoreParenNoopCasts should do this, but it doesn't do |
| 2013 | // quite what we want. |
| 2014 | static const Expr *skipNoOpCastsAndParens(const Expr *E) { |
| 2015 | while (true) { |
| 2016 | if (const ParenExpr *PE = dyn_cast<ParenExpr>(E)) { |
| 2017 | E = PE->getSubExpr(); |
| 2018 | continue; |
| 2019 | } |
| 2020 | |
| 2021 | if (const CastExpr *CE = dyn_cast<CastExpr>(E)) { |
| 2022 | if (CE->getCastKind() == CK_NoOp) { |
| 2023 | E = CE->getSubExpr(); |
| 2024 | continue; |
| 2025 | } |
| 2026 | } |
| 2027 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
| 2028 | if (UO->getOpcode() == UO_Extension) { |
| 2029 | E = UO->getSubExpr(); |
| 2030 | continue; |
| 2031 | } |
| 2032 | } |
| 2033 | return E; |
| 2034 | } |
| 2035 | } |
| 2036 | |
Benjamin Kramer | 9581ed0 | 2013-08-25 22:46:27 +0000 | [diff] [blame] | 2037 | bool |
| 2038 | CodeGenFunction::CanDevirtualizeMemberFunctionCall(const Expr *Base, |
| 2039 | const CXXMethodDecl *MD) { |
| 2040 | // When building with -fapple-kext, all calls must go through the vtable since |
| 2041 | // the kernel linker can do runtime patching of vtables. |
| 2042 | if (getLangOpts().AppleKext) |
| 2043 | return false; |
| 2044 | |
Anders Carlsson | a2447e0 | 2011-05-08 20:32:23 +0000 | [diff] [blame] | 2045 | // If the most derived class is marked final, we know that no subclass can |
| 2046 | // override this member function and so we can devirtualize it. For example: |
| 2047 | // |
| 2048 | // struct A { virtual void f(); } |
| 2049 | // struct B final : A { }; |
| 2050 | // |
| 2051 | // void f(B *b) { |
| 2052 | // b->f(); |
| 2053 | // } |
| 2054 | // |
Benjamin Kramer | 9581ed0 | 2013-08-25 22:46:27 +0000 | [diff] [blame] | 2055 | const CXXRecordDecl *MostDerivedClassDecl = Base->getBestDynamicClassType(); |
Anders Carlsson | a2447e0 | 2011-05-08 20:32:23 +0000 | [diff] [blame] | 2056 | if (MostDerivedClassDecl->hasAttr<FinalAttr>()) |
| 2057 | return true; |
| 2058 | |
| 2059 | // If the member function is marked 'final', we know that it can't be |
| 2060 | // overridden and can therefore devirtualize it. |
| 2061 | if (MD->hasAttr<FinalAttr>()) |
| 2062 | return true; |
| 2063 | |
| 2064 | // Similarly, if the class itself is marked 'final' it can't be overridden |
| 2065 | // and we can therefore devirtualize the member function call. |
| 2066 | if (MD->getParent()->hasAttr<FinalAttr>()) |
| 2067 | return true; |
| 2068 | |
| 2069 | Base = skipNoOpCastsAndParens(Base); |
| 2070 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) { |
| 2071 | if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) { |
| 2072 | // This is a record decl. We know the type and can devirtualize it. |
| 2073 | return VD->getType()->isRecordType(); |
| 2074 | } |
| 2075 | |
| 2076 | return false; |
| 2077 | } |
Benjamin Kramer | 9581ed0 | 2013-08-25 22:46:27 +0000 | [diff] [blame] | 2078 | |
| 2079 | // We can devirtualize calls on an object accessed by a class member access |
| 2080 | // expression, since by C++11 [basic.life]p6 we know that it can't refer to |
| 2081 | // a derived class object constructed in the same location. |
| 2082 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Base)) |
| 2083 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(ME->getMemberDecl())) |
| 2084 | return VD->getType()->isRecordType(); |
| 2085 | |
Anders Carlsson | a2447e0 | 2011-05-08 20:32:23 +0000 | [diff] [blame] | 2086 | // We can always devirtualize calls on temporary object expressions. |
| 2087 | if (isa<CXXConstructExpr>(Base)) |
| 2088 | return true; |
| 2089 | |
| 2090 | // And calls on bound temporaries. |
| 2091 | if (isa<CXXBindTemporaryExpr>(Base)) |
| 2092 | return true; |
| 2093 | |
| 2094 | // Check if this is a call expr that returns a record type. |
| 2095 | if (const CallExpr *CE = dyn_cast<CallExpr>(Base)) |
| 2096 | return CE->getCallReturnType()->isRecordType(); |
| 2097 | |
| 2098 | // We can't devirtualize the call. |
| 2099 | return false; |
| 2100 | } |
| 2101 | |
Anders Carlsson | a2447e0 | 2011-05-08 20:32:23 +0000 | [diff] [blame] | 2102 | llvm::Value * |
| 2103 | CodeGenFunction::EmitCXXOperatorMemberCallee(const CXXOperatorCallExpr *E, |
| 2104 | const CXXMethodDecl *MD, |
| 2105 | llvm::Value *This) { |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2106 | llvm::FunctionType *fnType = |
| 2107 | CGM.getTypes().GetFunctionType( |
| 2108 | CGM.getTypes().arrangeCXXMethodDeclaration(MD)); |
Anders Carlsson | a2447e0 | 2011-05-08 20:32:23 +0000 | [diff] [blame] | 2109 | |
Benjamin Kramer | 9581ed0 | 2013-08-25 22:46:27 +0000 | [diff] [blame] | 2110 | if (MD->isVirtual() && !CanDevirtualizeMemberFunctionCall(E->getArg(0), MD)) |
Timur Iskhodzhanov | 8f189a9 | 2013-08-21 06:25:03 +0000 | [diff] [blame] | 2111 | return CGM.getCXXABI().getVirtualFunctionPointer(*this, MD, This, fnType); |
Anders Carlsson | a2447e0 | 2011-05-08 20:32:23 +0000 | [diff] [blame] | 2112 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2113 | return CGM.GetAddrOfFunction(MD, fnType); |
Anders Carlsson | a2447e0 | 2011-05-08 20:32:23 +0000 | [diff] [blame] | 2114 | } |
Eli Friedman | bd89f8c | 2012-02-16 01:37:33 +0000 | [diff] [blame] | 2115 | |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 2116 | void CodeGenFunction::EmitForwardingCallToLambda( |
| 2117 | const CXXMethodDecl *callOperator, |
| 2118 | CallArgList &callArgs) { |
Eli Friedman | 21f6ed9 | 2012-02-16 03:47:28 +0000 | [diff] [blame] | 2119 | // Get the address of the call operator. |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 2120 | const CGFunctionInfo &calleeFnInfo = |
| 2121 | CGM.getTypes().arrangeCXXMethodDeclaration(callOperator); |
| 2122 | llvm::Value *callee = |
| 2123 | CGM.GetAddrOfFunction(GlobalDecl(callOperator), |
| 2124 | CGM.getTypes().GetFunctionType(calleeFnInfo)); |
Eli Friedman | 21f6ed9 | 2012-02-16 03:47:28 +0000 | [diff] [blame] | 2125 | |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 2126 | // Prepare the return slot. |
| 2127 | const FunctionProtoType *FPT = |
| 2128 | callOperator->getType()->castAs<FunctionProtoType>(); |
| 2129 | QualType resultType = FPT->getResultType(); |
| 2130 | ReturnValueSlot returnSlot; |
| 2131 | if (!resultType->isVoidType() && |
| 2132 | calleeFnInfo.getReturnInfo().getKind() == ABIArgInfo::Indirect && |
John McCall | 9d232c8 | 2013-03-07 21:37:08 +0000 | [diff] [blame] | 2133 | !hasScalarEvaluationKind(calleeFnInfo.getReturnType())) |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 2134 | returnSlot = ReturnValueSlot(ReturnValue, resultType.isVolatileQualified()); |
| 2135 | |
| 2136 | // We don't need to separately arrange the call arguments because |
| 2137 | // the call can't be variadic anyway --- it's impossible to forward |
| 2138 | // variadic arguments. |
Eli Friedman | 21f6ed9 | 2012-02-16 03:47:28 +0000 | [diff] [blame] | 2139 | |
| 2140 | // Now emit our call. |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 2141 | RValue RV = EmitCall(calleeFnInfo, callee, returnSlot, |
| 2142 | callArgs, callOperator); |
Eli Friedman | 21f6ed9 | 2012-02-16 03:47:28 +0000 | [diff] [blame] | 2143 | |
John McCall | 0f3d097 | 2012-07-07 06:41:13 +0000 | [diff] [blame] | 2144 | // If necessary, copy the returned value into the slot. |
| 2145 | if (!resultType->isVoidType() && returnSlot.isNull()) |
| 2146 | EmitReturnOfRValue(RV, resultType); |
Eli Friedman | 50f089a | 2012-12-13 23:37:17 +0000 | [diff] [blame] | 2147 | else |
| 2148 | EmitBranchThroughCleanup(ReturnBlock); |
Eli Friedman | 21f6ed9 | 2012-02-16 03:47:28 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
Eli Friedman | 64bee65 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 2151 | void CodeGenFunction::EmitLambdaBlockInvokeBody() { |
| 2152 | const BlockDecl *BD = BlockInfo->getBlockDecl(); |
| 2153 | const VarDecl *variable = BD->capture_begin()->getVariable(); |
| 2154 | const CXXRecordDecl *Lambda = variable->getType()->getAsCXXRecordDecl(); |
| 2155 | |
| 2156 | // Start building arguments for forwarding call |
| 2157 | CallArgList CallArgs; |
| 2158 | |
| 2159 | QualType ThisType = getContext().getPointerType(getContext().getRecordType(Lambda)); |
| 2160 | llvm::Value *ThisPtr = GetAddrOfBlockDecl(variable, false); |
| 2161 | CallArgs.add(RValue::get(ThisPtr), ThisType); |
| 2162 | |
| 2163 | // Add the rest of the parameters. |
| 2164 | for (BlockDecl::param_const_iterator I = BD->param_begin(), |
| 2165 | E = BD->param_end(); I != E; ++I) { |
| 2166 | ParmVarDecl *param = *I; |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2167 | EmitDelegateCallArg(CallArgs, param, param->getLocStart()); |
Eli Friedman | 64bee65 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 2168 | } |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 2169 | assert(!Lambda->isGenericLambda() && |
| 2170 | "generic lambda interconversion to block not implemented"); |
| 2171 | EmitForwardingCallToLambda(Lambda->getLambdaCallOperator(), CallArgs); |
Eli Friedman | 64bee65 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
| 2174 | void CodeGenFunction::EmitLambdaToBlockPointerBody(FunctionArgList &Args) { |
John McCall | f5ebf9b | 2013-05-03 07:33:41 +0000 | [diff] [blame] | 2175 | if (cast<CXXMethodDecl>(CurCodeDecl)->isVariadic()) { |
Eli Friedman | 64bee65 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 2176 | // FIXME: Making this work correctly is nasty because it requires either |
| 2177 | // cloning the body of the call operator or making the call operator forward. |
John McCall | f5ebf9b | 2013-05-03 07:33:41 +0000 | [diff] [blame] | 2178 | CGM.ErrorUnsupported(CurCodeDecl, "lambda conversion to variadic function"); |
Eli Friedman | 64bee65 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 2179 | return; |
| 2180 | } |
| 2181 | |
Eli Friedman | 64bee65 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 2182 | EmitFunctionBody(Args); |
Eli Friedman | 64bee65 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
| 2185 | void CodeGenFunction::EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD) { |
| 2186 | const CXXRecordDecl *Lambda = MD->getParent(); |
| 2187 | |
| 2188 | // Start building arguments for forwarding call |
| 2189 | CallArgList CallArgs; |
| 2190 | |
| 2191 | QualType ThisType = getContext().getPointerType(getContext().getRecordType(Lambda)); |
| 2192 | llvm::Value *ThisPtr = llvm::UndefValue::get(getTypes().ConvertType(ThisType)); |
| 2193 | CallArgs.add(RValue::get(ThisPtr), ThisType); |
| 2194 | |
| 2195 | // Add the rest of the parameters. |
| 2196 | for (FunctionDecl::param_const_iterator I = MD->param_begin(), |
| 2197 | E = MD->param_end(); I != E; ++I) { |
| 2198 | ParmVarDecl *param = *I; |
Nick Lewycky | 4ee7dc2 | 2013-10-02 02:29:49 +0000 | [diff] [blame] | 2199 | EmitDelegateCallArg(CallArgs, param, param->getLocStart()); |
Eli Friedman | 64bee65 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 2200 | } |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 2201 | const CXXMethodDecl *CallOp = Lambda->getLambdaCallOperator(); |
| 2202 | // For a generic lambda, find the corresponding call operator specialization |
| 2203 | // to which the call to the static-invoker shall be forwarded. |
| 2204 | if (Lambda->isGenericLambda()) { |
| 2205 | assert(MD->isFunctionTemplateSpecialization()); |
| 2206 | const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs(); |
| 2207 | FunctionTemplateDecl *CallOpTemplate = CallOp->getDescribedFunctionTemplate(); |
| 2208 | void *InsertPos = 0; |
| 2209 | FunctionDecl *CorrespondingCallOpSpecialization = |
| 2210 | CallOpTemplate->findSpecialization(TAL->data(), TAL->size(), InsertPos); |
| 2211 | assert(CorrespondingCallOpSpecialization); |
| 2212 | CallOp = cast<CXXMethodDecl>(CorrespondingCallOpSpecialization); |
| 2213 | } |
| 2214 | EmitForwardingCallToLambda(CallOp, CallArgs); |
Eli Friedman | 64bee65 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 2217 | void CodeGenFunction::EmitLambdaStaticInvokeFunction(const CXXMethodDecl *MD) { |
| 2218 | if (MD->isVariadic()) { |
Eli Friedman | 21f6ed9 | 2012-02-16 03:47:28 +0000 | [diff] [blame] | 2219 | // FIXME: Making this work correctly is nasty because it requires either |
| 2220 | // cloning the body of the call operator or making the call operator forward. |
| 2221 | CGM.ErrorUnsupported(MD, "lambda conversion to variadic function"); |
Eli Friedman | 64bee65 | 2012-02-25 02:48:22 +0000 | [diff] [blame] | 2222 | return; |
Eli Friedman | 21f6ed9 | 2012-02-16 03:47:28 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
Douglas Gregor | 27dd7d9 | 2012-02-17 03:02:34 +0000 | [diff] [blame] | 2225 | EmitLambdaDelegatingInvokeBody(MD); |
Eli Friedman | bd89f8c | 2012-02-16 01:37:33 +0000 | [diff] [blame] | 2226 | } |