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