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