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