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 | |
| 65 | const llvm::Type *PtrDiffTy = |
| 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()) { |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 98 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext()); |
| 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) { |
Anders Carlsson | 9dc228a | 2010-04-20 16:03:35 +0000 | [diff] [blame] | 110 | const llvm::Type *PtrDiffTy = |
| 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. |
| 128 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 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. |
| 158 | const 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 | |
| 175 | llvm::Value *IsNull = |
| 176 | Builder.CreateICmpEQ(Value, |
| 177 | llvm::Constant::getNullValue(Value->getType())); |
| 178 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
| 179 | EmitBlock(CastNotNull); |
| 180 | } |
| 181 | |
| 182 | llvm::Value *VirtualOffset = 0; |
| 183 | |
Anders Carlsson | 336a7dc | 2011-01-29 03:18:56 +0000 | [diff] [blame] | 184 | if (VBase) { |
| 185 | if (Derived->hasAttr<FinalAttr>()) { |
| 186 | VirtualOffset = 0; |
| 187 | |
| 188 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Derived); |
| 189 | |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 190 | CharUnits VBaseOffset = Layout.getVBaseClassOffset(VBase); |
| 191 | NonVirtualOffset += VBaseOffset; |
Anders Carlsson | 336a7dc | 2011-01-29 03:18:56 +0000 | [diff] [blame] | 192 | } else |
| 193 | VirtualOffset = GetVirtualBaseClassOffset(Value, Derived, VBase); |
| 194 | } |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 195 | |
| 196 | // Apply the offsets. |
Ken Dyck | 55c0258 | 2011-03-22 00:53:26 +0000 | [diff] [blame] | 197 | Value = ApplyNonVirtualAndVirtualOffset(*this, Value, |
Ken Dyck | 9a8ad9b | 2011-03-23 00:45:26 +0000 | [diff] [blame] | 198 | NonVirtualOffset, |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 199 | VirtualOffset); |
| 200 | |
| 201 | // Cast back. |
| 202 | Value = Builder.CreateBitCast(Value, BasePtrTy); |
| 203 | |
| 204 | if (NullCheckValue) { |
| 205 | Builder.CreateBr(CastEnd); |
| 206 | EmitBlock(CastNull); |
| 207 | Builder.CreateBr(CastEnd); |
| 208 | EmitBlock(CastEnd); |
| 209 | |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 210 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 211 | PHI->addIncoming(Value, CastNotNull); |
| 212 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), |
| 213 | CastNull); |
| 214 | Value = PHI; |
| 215 | } |
| 216 | |
| 217 | return Value; |
| 218 | } |
| 219 | |
| 220 | llvm::Value * |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 221 | CodeGenFunction::GetAddressOfDerivedClass(llvm::Value *Value, |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 222 | const CXXRecordDecl *Derived, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 223 | CastExpr::path_const_iterator PathBegin, |
| 224 | CastExpr::path_const_iterator PathEnd, |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 225 | bool NullCheckValue) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 226 | assert(PathBegin != PathEnd && "Base path should not be empty!"); |
Anders Carlsson | a04efdf | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 227 | |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 228 | QualType DerivedTy = |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 229 | getContext().getCanonicalType(getContext().getTagDeclType(Derived)); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 230 | const llvm::Type *DerivedPtrTy = ConvertType(DerivedTy)->getPointerTo(); |
| 231 | |
Anders Carlsson | a552ea7 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 232 | llvm::Value *NonVirtualOffset = |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 233 | CGM.GetNonVirtualBaseClassOffset(Derived, PathBegin, PathEnd); |
Anders Carlsson | a552ea7 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 234 | |
| 235 | if (!NonVirtualOffset) { |
| 236 | // No offset, we can just cast back. |
| 237 | return Builder.CreateBitCast(Value, DerivedPtrTy); |
| 238 | } |
| 239 | |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 240 | llvm::BasicBlock *CastNull = 0; |
| 241 | llvm::BasicBlock *CastNotNull = 0; |
| 242 | llvm::BasicBlock *CastEnd = 0; |
| 243 | |
| 244 | if (NullCheckValue) { |
| 245 | CastNull = createBasicBlock("cast.null"); |
| 246 | CastNotNull = createBasicBlock("cast.notnull"); |
| 247 | CastEnd = createBasicBlock("cast.end"); |
| 248 | |
| 249 | llvm::Value *IsNull = |
| 250 | Builder.CreateICmpEQ(Value, |
| 251 | llvm::Constant::getNullValue(Value->getType())); |
| 252 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
| 253 | EmitBlock(CastNotNull); |
| 254 | } |
| 255 | |
Anders Carlsson | a552ea7 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 256 | // Apply the offset. |
| 257 | Value = Builder.CreatePtrToInt(Value, NonVirtualOffset->getType()); |
| 258 | Value = Builder.CreateSub(Value, NonVirtualOffset); |
| 259 | Value = Builder.CreateIntToPtr(Value, DerivedPtrTy); |
| 260 | |
| 261 | // Just cast. |
| 262 | Value = Builder.CreateBitCast(Value, DerivedPtrTy); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 263 | |
| 264 | if (NullCheckValue) { |
| 265 | Builder.CreateBr(CastEnd); |
| 266 | EmitBlock(CastNull); |
| 267 | Builder.CreateBr(CastEnd); |
| 268 | EmitBlock(CastEnd); |
| 269 | |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 270 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 271 | PHI->addIncoming(Value, CastNotNull); |
| 272 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), |
| 273 | CastNull); |
| 274 | Value = PHI; |
| 275 | } |
| 276 | |
| 277 | return Value; |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 278 | } |
Anders Carlsson | 21c9ad9 | 2010-03-30 03:27:09 +0000 | [diff] [blame] | 279 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 280 | /// GetVTTParameter - Return the VTT parameter that should be passed to a |
| 281 | /// base constructor/destructor with virtual bases. |
Anders Carlsson | 314e622 | 2010-05-02 23:33:10 +0000 | [diff] [blame] | 282 | static llvm::Value *GetVTTParameter(CodeGenFunction &CGF, GlobalDecl GD, |
| 283 | bool ForVirtualBase) { |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 284 | if (!CodeGenVTables::needsVTTParameter(GD)) { |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 285 | // This constructor/destructor does not need a VTT parameter. |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | const CXXRecordDecl *RD = cast<CXXMethodDecl>(CGF.CurFuncDecl)->getParent(); |
| 290 | const CXXRecordDecl *Base = cast<CXXMethodDecl>(GD.getDecl())->getParent(); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 291 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 292 | llvm::Value *VTT; |
| 293 | |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 294 | uint64_t SubVTTIndex; |
| 295 | |
| 296 | // If the record matches the base, this is the complete ctor/dtor |
| 297 | // variant calling the base variant in a class with virtual bases. |
| 298 | if (RD == Base) { |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 299 | assert(!CodeGenVTables::needsVTTParameter(CGF.CurGD) && |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 300 | "doing no-op VTT offset in base dtor/ctor?"); |
Anders Carlsson | 314e622 | 2010-05-02 23:33:10 +0000 | [diff] [blame] | 301 | assert(!ForVirtualBase && "Can't have same class as virtual base!"); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 302 | SubVTTIndex = 0; |
| 303 | } else { |
Anders Carlsson | c11bb21 | 2010-05-02 23:53:25 +0000 | [diff] [blame] | 304 | const ASTRecordLayout &Layout = |
| 305 | CGF.getContext().getASTRecordLayout(RD); |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 306 | CharUnits BaseOffset = ForVirtualBase ? |
| 307 | Layout.getVBaseClassOffset(Base) : |
| 308 | Layout.getBaseClassOffset(Base); |
Anders Carlsson | c11bb21 | 2010-05-02 23:53:25 +0000 | [diff] [blame] | 309 | |
| 310 | SubVTTIndex = |
| 311 | CGF.CGM.getVTables().getSubVTTIndex(RD, BaseSubobject(Base, BaseOffset)); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 312 | assert(SubVTTIndex != 0 && "Sub-VTT index must be greater than zero!"); |
| 313 | } |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 314 | |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 315 | if (CodeGenVTables::needsVTTParameter(CGF.CurGD)) { |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 316 | // A VTT parameter was passed to the constructor, use it. |
| 317 | VTT = CGF.LoadCXXVTT(); |
| 318 | VTT = CGF.Builder.CreateConstInBoundsGEP1_64(VTT, SubVTTIndex); |
| 319 | } else { |
| 320 | // We're the complete constructor, so get the VTT by name. |
Anders Carlsson | 1cbce12 | 2011-01-29 19:16:51 +0000 | [diff] [blame] | 321 | VTT = CGF.CGM.getVTables().GetAddrOfVTT(RD); |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 322 | VTT = CGF.Builder.CreateConstInBoundsGEP2_64(VTT, 0, SubVTTIndex); |
| 323 | } |
| 324 | |
| 325 | return VTT; |
| 326 | } |
| 327 | |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 328 | namespace { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 329 | /// Call the destructor for a direct base class. |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 330 | struct CallBaseDtor : EHScopeStack::Cleanup { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 331 | const CXXRecordDecl *BaseClass; |
| 332 | bool BaseIsVirtual; |
| 333 | CallBaseDtor(const CXXRecordDecl *Base, bool BaseIsVirtual) |
| 334 | : BaseClass(Base), BaseIsVirtual(BaseIsVirtual) {} |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 335 | |
| 336 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 337 | const CXXRecordDecl *DerivedClass = |
| 338 | cast<CXXMethodDecl>(CGF.CurCodeDecl)->getParent(); |
| 339 | |
| 340 | const CXXDestructorDecl *D = BaseClass->getDestructor(); |
| 341 | llvm::Value *Addr = |
| 342 | CGF.GetAddressOfDirectBaseInCompleteClass(CGF.LoadCXXThis(), |
| 343 | DerivedClass, BaseClass, |
| 344 | BaseIsVirtual); |
| 345 | CGF.EmitCXXDestructorCall(D, Dtor_Base, BaseIsVirtual, Addr); |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 346 | } |
| 347 | }; |
John McCall | 7e1dff7 | 2010-09-17 02:31:44 +0000 | [diff] [blame] | 348 | |
| 349 | /// A visitor which checks whether an initializer uses 'this' in a |
| 350 | /// way which requires the vtable to be properly set. |
| 351 | struct DynamicThisUseChecker : EvaluatedExprVisitor<DynamicThisUseChecker> { |
| 352 | typedef EvaluatedExprVisitor<DynamicThisUseChecker> super; |
| 353 | |
| 354 | bool UsesThis; |
| 355 | |
| 356 | DynamicThisUseChecker(ASTContext &C) : super(C), UsesThis(false) {} |
| 357 | |
| 358 | // Black-list all explicit and implicit references to 'this'. |
| 359 | // |
| 360 | // Do we need to worry about external references to 'this' derived |
| 361 | // from arbitrary code? If so, then anything which runs arbitrary |
| 362 | // external code might potentially access the vtable. |
| 363 | void VisitCXXThisExpr(CXXThisExpr *E) { UsesThis = true; } |
| 364 | }; |
| 365 | } |
| 366 | |
| 367 | static bool BaseInitializerUsesThis(ASTContext &C, const Expr *Init) { |
| 368 | DynamicThisUseChecker Checker(C); |
| 369 | Checker.Visit(const_cast<Expr*>(Init)); |
| 370 | return Checker.UsesThis; |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 373 | static void EmitBaseInitializer(CodeGenFunction &CGF, |
| 374 | const CXXRecordDecl *ClassDecl, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 375 | CXXCtorInitializer *BaseInit, |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 376 | CXXCtorType CtorType) { |
| 377 | assert(BaseInit->isBaseInitializer() && |
| 378 | "Must have base initializer!"); |
| 379 | |
| 380 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
| 381 | |
| 382 | const Type *BaseType = BaseInit->getBaseClass(); |
| 383 | CXXRecordDecl *BaseClassDecl = |
| 384 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); |
| 385 | |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 386 | bool isBaseVirtual = BaseInit->isBaseVirtual(); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 387 | |
| 388 | // The base constructor doesn't construct virtual bases. |
| 389 | if (CtorType == Ctor_Base && isBaseVirtual) |
| 390 | return; |
| 391 | |
John McCall | 7e1dff7 | 2010-09-17 02:31:44 +0000 | [diff] [blame] | 392 | // If the initializer for the base (other than the constructor |
| 393 | // itself) accesses 'this' in any way, we need to initialize the |
| 394 | // vtables. |
| 395 | if (BaseInitializerUsesThis(CGF.getContext(), BaseInit->getInit())) |
| 396 | CGF.InitializeVTablePointers(ClassDecl); |
| 397 | |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 398 | // We can pretend to be a complete class because it only matters for |
| 399 | // virtual bases, and we only do virtual bases for complete ctors. |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 400 | llvm::Value *V = |
| 401 | CGF.GetAddressOfDirectBaseInCompleteClass(ThisPtr, ClassDecl, |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 402 | BaseClassDecl, |
| 403 | isBaseVirtual); |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 404 | |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 405 | AggValueSlot AggSlot = AggValueSlot::forAddr(V, false, /*Lifetime*/ true); |
| 406 | |
| 407 | CGF.EmitAggExpr(BaseInit->getInit(), AggSlot); |
Anders Carlsson | 594d5e8 | 2010-02-06 20:00:21 +0000 | [diff] [blame] | 408 | |
Anders Carlsson | 7a17851 | 2011-02-28 00:33:03 +0000 | [diff] [blame] | 409 | if (CGF.CGM.getLangOptions().Exceptions && |
Anders Carlsson | c1cfdf8 | 2011-02-20 00:20:27 +0000 | [diff] [blame] | 410 | !BaseClassDecl->hasTrivialDestructor()) |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 411 | CGF.EHStack.pushCleanup<CallBaseDtor>(EHCleanup, BaseClassDecl, |
| 412 | isBaseVirtual); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 415 | static void EmitAggMemberInitializer(CodeGenFunction &CGF, |
| 416 | LValue LHS, |
| 417 | llvm::Value *ArrayIndexVar, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 418 | CXXCtorInitializer *MemberInit, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 419 | QualType T, |
| 420 | unsigned Index) { |
| 421 | if (Index == MemberInit->getNumArrayIndices()) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 422 | CodeGenFunction::RunCleanupsScope Cleanups(CGF); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 423 | |
| 424 | llvm::Value *Dest = LHS.getAddress(); |
| 425 | if (ArrayIndexVar) { |
| 426 | // If we have an array index variable, load it and use it as an offset. |
| 427 | // Then, increment the value. |
| 428 | llvm::Value *ArrayIndex = CGF.Builder.CreateLoad(ArrayIndexVar); |
| 429 | Dest = CGF.Builder.CreateInBoundsGEP(Dest, ArrayIndex, "destaddress"); |
| 430 | llvm::Value *Next = llvm::ConstantInt::get(ArrayIndex->getType(), 1); |
| 431 | Next = CGF.Builder.CreateAdd(ArrayIndex, Next, "inc"); |
| 432 | CGF.Builder.CreateStore(Next, ArrayIndexVar); |
| 433 | } |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 434 | |
| 435 | AggValueSlot Slot = AggValueSlot::forAddr(Dest, LHS.isVolatileQualified(), |
| 436 | /*Lifetime*/ true); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 437 | |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 438 | CGF.EmitAggExpr(MemberInit->getInit(), Slot); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 439 | |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | const ConstantArrayType *Array = CGF.getContext().getAsConstantArrayType(T); |
| 444 | assert(Array && "Array initialization without the array type?"); |
| 445 | llvm::Value *IndexVar |
| 446 | = CGF.GetAddrOfLocalVar(MemberInit->getArrayIndex(Index)); |
| 447 | assert(IndexVar && "Array index variable not loaded"); |
| 448 | |
| 449 | // Initialize this index variable to zero. |
| 450 | llvm::Value* Zero |
| 451 | = llvm::Constant::getNullValue( |
| 452 | CGF.ConvertType(CGF.getContext().getSizeType())); |
| 453 | CGF.Builder.CreateStore(Zero, IndexVar); |
| 454 | |
| 455 | // Start the loop with a block that tests the condition. |
| 456 | llvm::BasicBlock *CondBlock = CGF.createBasicBlock("for.cond"); |
| 457 | llvm::BasicBlock *AfterFor = CGF.createBasicBlock("for.end"); |
| 458 | |
| 459 | CGF.EmitBlock(CondBlock); |
| 460 | |
| 461 | llvm::BasicBlock *ForBody = CGF.createBasicBlock("for.body"); |
| 462 | // Generate: if (loop-index < number-of-elements) fall to the loop body, |
| 463 | // otherwise, go to the block after the for-loop. |
| 464 | uint64_t NumElements = Array->getSize().getZExtValue(); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 465 | llvm::Value *Counter = CGF.Builder.CreateLoad(IndexVar); |
Chris Lattner | 985f739 | 2010-05-06 06:35:23 +0000 | [diff] [blame] | 466 | llvm::Value *NumElementsPtr = |
| 467 | llvm::ConstantInt::get(Counter->getType(), NumElements); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 468 | llvm::Value *IsLess = CGF.Builder.CreateICmpULT(Counter, NumElementsPtr, |
| 469 | "isless"); |
| 470 | |
| 471 | // If the condition is true, execute the body. |
| 472 | CGF.Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
| 473 | |
| 474 | CGF.EmitBlock(ForBody); |
| 475 | llvm::BasicBlock *ContinueBlock = CGF.createBasicBlock("for.inc"); |
| 476 | |
| 477 | { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 478 | CodeGenFunction::RunCleanupsScope Cleanups(CGF); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 479 | |
| 480 | // Inside the loop body recurse to emit the inner loop or, eventually, the |
| 481 | // constructor call. |
| 482 | EmitAggMemberInitializer(CGF, LHS, ArrayIndexVar, MemberInit, |
| 483 | Array->getElementType(), Index + 1); |
| 484 | } |
| 485 | |
| 486 | CGF.EmitBlock(ContinueBlock); |
| 487 | |
| 488 | // Emit the increment of the loop counter. |
| 489 | llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); |
| 490 | Counter = CGF.Builder.CreateLoad(IndexVar); |
| 491 | NextVal = CGF.Builder.CreateAdd(Counter, NextVal, "inc"); |
| 492 | CGF.Builder.CreateStore(NextVal, IndexVar); |
| 493 | |
| 494 | // Finally, branch back up to the condition for the next iteration. |
| 495 | CGF.EmitBranch(CondBlock); |
| 496 | |
| 497 | // Emit the fall-through block. |
| 498 | CGF.EmitBlock(AfterFor, true); |
| 499 | } |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 500 | |
| 501 | namespace { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 502 | struct CallMemberDtor : EHScopeStack::Cleanup { |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 503 | FieldDecl *Field; |
| 504 | CXXDestructorDecl *Dtor; |
| 505 | |
| 506 | CallMemberDtor(FieldDecl *Field, CXXDestructorDecl *Dtor) |
| 507 | : Field(Field), Dtor(Dtor) {} |
| 508 | |
| 509 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 510 | // FIXME: Is this OK for C++0x delegating constructors? |
| 511 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
| 512 | LValue LHS = CGF.EmitLValueForField(ThisPtr, Field, 0); |
| 513 | |
| 514 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, /*ForVirtualBase=*/false, |
| 515 | LHS.getAddress()); |
| 516 | } |
| 517 | }; |
| 518 | } |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 519 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 520 | static void EmitMemberInitializer(CodeGenFunction &CGF, |
| 521 | const CXXRecordDecl *ClassDecl, |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 522 | CXXCtorInitializer *MemberInit, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 523 | const CXXConstructorDecl *Constructor, |
| 524 | FunctionArgList &Args) { |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 525 | assert(MemberInit->isAnyMemberInitializer() && |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 526 | "Must have member initializer!"); |
| 527 | |
| 528 | // non-static data member initializers. |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 529 | FieldDecl *Field = MemberInit->getAnyMember(); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 530 | QualType FieldType = CGF.getContext().getCanonicalType(Field->getType()); |
| 531 | |
| 532 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
John McCall | a9976d3 | 2010-05-21 01:18:57 +0000 | [diff] [blame] | 533 | LValue LHS; |
Anders Carlsson | 06a2970 | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 534 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 535 | // 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] | 536 | if (MemberInit->isIndirectMemberInitializer()) { |
| 537 | LHS = CGF.EmitLValueForAnonRecordField(ThisPtr, |
| 538 | MemberInit->getIndirectMember(), 0); |
| 539 | FieldType = MemberInit->getIndirectMember()->getAnonField()->getType(); |
John McCall | a9976d3 | 2010-05-21 01:18:57 +0000 | [diff] [blame] | 540 | } else { |
| 541 | LHS = CGF.EmitLValueForFieldInitialization(ThisPtr, Field, 0); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 544 | // FIXME: If there's no initializer and the CXXCtorInitializer |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 545 | // was implicitly generated, we shouldn't be zeroing memory. |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 546 | RValue RHS; |
| 547 | if (FieldType->isReferenceType()) { |
Anders Carlsson | 32f36ba | 2010-06-26 16:35:32 +0000 | [diff] [blame] | 548 | RHS = CGF.EmitReferenceBindingToExpr(MemberInit->getInit(), Field); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 549 | CGF.EmitStoreThroughLValue(RHS, LHS, FieldType); |
Eli Friedman | 3bb9412 | 2010-01-31 19:07:50 +0000 | [diff] [blame] | 550 | } else if (FieldType->isArrayType() && !MemberInit->getInit()) { |
Anders Carlsson | 1884eb0 | 2010-05-22 17:35:42 +0000 | [diff] [blame] | 551 | CGF.EmitNullInitialization(LHS.getAddress(), Field->getType()); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 552 | } else if (!CGF.hasAggregateLLVMType(Field->getType())) { |
Eli Friedman | 0b29227 | 2010-06-03 19:58:07 +0000 | [diff] [blame] | 553 | RHS = RValue::get(CGF.EmitScalarExpr(MemberInit->getInit())); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 554 | CGF.EmitStoreThroughLValue(RHS, LHS, FieldType); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 555 | } else if (MemberInit->getInit()->getType()->isAnyComplexType()) { |
| 556 | CGF.EmitComplexExprIntoAddr(MemberInit->getInit(), LHS.getAddress(), |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 557 | LHS.isVolatileQualified()); |
| 558 | } else { |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 559 | llvm::Value *ArrayIndexVar = 0; |
| 560 | const ConstantArrayType *Array |
| 561 | = CGF.getContext().getAsConstantArrayType(FieldType); |
| 562 | if (Array && Constructor->isImplicit() && |
| 563 | Constructor->isCopyConstructor()) { |
| 564 | const llvm::Type *SizeTy |
| 565 | = CGF.ConvertType(CGF.getContext().getSizeType()); |
| 566 | |
| 567 | // The LHS is a pointer to the first object we'll be constructing, as |
| 568 | // a flat array. |
| 569 | QualType BaseElementTy = CGF.getContext().getBaseElementType(Array); |
| 570 | const llvm::Type *BasePtr = CGF.ConvertType(BaseElementTy); |
| 571 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 572 | llvm::Value *BaseAddrPtr = CGF.Builder.CreateBitCast(LHS.getAddress(), |
| 573 | BasePtr); |
Daniel Dunbar | 9f553f5 | 2010-08-21 03:08:16 +0000 | [diff] [blame] | 574 | LHS = CGF.MakeAddrLValue(BaseAddrPtr, BaseElementTy); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 575 | |
| 576 | // Create an array index that will be used to walk over all of the |
| 577 | // objects we're constructing. |
| 578 | ArrayIndexVar = CGF.CreateTempAlloca(SizeTy, "object.index"); |
| 579 | llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy); |
| 580 | CGF.Builder.CreateStore(Zero, ArrayIndexVar); |
| 581 | |
| 582 | // If we are copying an array of scalars or classes with trivial copy |
| 583 | // constructors, perform a single aggregate copy. |
| 584 | const RecordType *Record = BaseElementTy->getAs<RecordType>(); |
| 585 | if (!Record || |
| 586 | cast<CXXRecordDecl>(Record->getDecl())->hasTrivialCopyConstructor()) { |
| 587 | // Find the source pointer. We knows it's the last argument because |
| 588 | // we know we're in a copy constructor. |
| 589 | unsigned SrcArgIndex = Args.size() - 1; |
| 590 | llvm::Value *SrcPtr |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 591 | = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(Args[SrcArgIndex])); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 592 | LValue Src = CGF.EmitLValueForFieldInitialization(SrcPtr, Field, 0); |
| 593 | |
| 594 | // Copy the aggregate. |
| 595 | CGF.EmitAggregateCopy(LHS.getAddress(), Src.getAddress(), FieldType, |
| 596 | LHS.isVolatileQualified()); |
| 597 | return; |
| 598 | } |
| 599 | |
| 600 | // Emit the block variables for the array indices, if any. |
| 601 | for (unsigned I = 0, N = MemberInit->getNumArrayIndices(); I != N; ++I) |
John McCall | b6bbcc9 | 2010-10-15 04:57:14 +0000 | [diff] [blame] | 602 | CGF.EmitAutoVarDecl(*MemberInit->getArrayIndex(I)); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | EmitAggMemberInitializer(CGF, LHS, ArrayIndexVar, MemberInit, FieldType, 0); |
Anders Carlsson | 9405dcd | 2010-02-06 19:50:17 +0000 | [diff] [blame] | 606 | |
Anders Carlsson | 7a17851 | 2011-02-28 00:33:03 +0000 | [diff] [blame] | 607 | if (!CGF.CGM.getLangOptions().Exceptions) |
Anders Carlsson | 9405dcd | 2010-02-06 19:50:17 +0000 | [diff] [blame] | 608 | return; |
| 609 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 610 | // FIXME: If we have an array of classes w/ non-trivial destructors, |
| 611 | // we need to destroy in reverse order of construction along the exception |
| 612 | // path. |
Anders Carlsson | 9405dcd | 2010-02-06 19:50:17 +0000 | [diff] [blame] | 613 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 614 | if (!RT) |
| 615 | return; |
| 616 | |
| 617 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
John McCall | 182ab51 | 2010-07-21 01:23:41 +0000 | [diff] [blame] | 618 | if (!RD->hasTrivialDestructor()) |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 619 | CGF.EHStack.pushCleanup<CallMemberDtor>(EHCleanup, Field, |
| 620 | RD->getDestructor()); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 624 | /// Checks whether the given constructor is a valid subject for the |
| 625 | /// complete-to-base constructor delegation optimization, i.e. |
| 626 | /// emitting the complete constructor as a simple call to the base |
| 627 | /// constructor. |
| 628 | static bool IsConstructorDelegationValid(const CXXConstructorDecl *Ctor) { |
| 629 | |
| 630 | // Currently we disable the optimization for classes with virtual |
| 631 | // bases because (1) the addresses of parameter variables need to be |
| 632 | // consistent across all initializers but (2) the delegate function |
| 633 | // call necessarily creates a second copy of the parameter variable. |
| 634 | // |
| 635 | // The limiting example (purely theoretical AFAIK): |
| 636 | // struct A { A(int &c) { c++; } }; |
| 637 | // struct B : virtual A { |
| 638 | // B(int count) : A(count) { printf("%d\n", count); } |
| 639 | // }; |
| 640 | // ...although even this example could in principle be emitted as a |
| 641 | // delegation since the address of the parameter doesn't escape. |
| 642 | if (Ctor->getParent()->getNumVBases()) { |
| 643 | // TODO: white-list trivial vbase initializers. This case wouldn't |
| 644 | // be subject to the restrictions below. |
| 645 | |
| 646 | // TODO: white-list cases where: |
| 647 | // - there are no non-reference parameters to the constructor |
| 648 | // - the initializers don't access any non-reference parameters |
| 649 | // - the initializers don't take the address of non-reference |
| 650 | // parameters |
| 651 | // - etc. |
| 652 | // If we ever add any of the above cases, remember that: |
| 653 | // - function-try-blocks will always blacklist this optimization |
| 654 | // - we need to perform the constructor prologue and cleanup in |
| 655 | // EmitConstructorBody. |
| 656 | |
| 657 | return false; |
| 658 | } |
| 659 | |
| 660 | // We also disable the optimization for variadic functions because |
| 661 | // it's impossible to "re-pass" varargs. |
| 662 | if (Ctor->getType()->getAs<FunctionProtoType>()->isVariadic()) |
| 663 | return false; |
| 664 | |
| 665 | return true; |
| 666 | } |
| 667 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 668 | /// EmitConstructorBody - Emits the body of the current constructor. |
| 669 | void CodeGenFunction::EmitConstructorBody(FunctionArgList &Args) { |
| 670 | const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(CurGD.getDecl()); |
| 671 | CXXCtorType CtorType = CurGD.getCtorType(); |
| 672 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 673 | // Before we go any further, try the complete->base constructor |
| 674 | // delegation optimization. |
| 675 | if (CtorType == Ctor_Complete && IsConstructorDelegationValid(Ctor)) { |
Devang Patel | d67ef0e | 2010-08-11 21:04:37 +0000 | [diff] [blame] | 676 | if (CGDebugInfo *DI = getDebugInfo()) |
| 677 | DI->EmitStopPoint(Builder); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 678 | EmitDelegateCXXConstructorCall(Ctor, Ctor_Base, Args); |
| 679 | return; |
| 680 | } |
| 681 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 682 | Stmt *Body = Ctor->getBody(); |
| 683 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 684 | // Enter the function-try-block before the constructor prologue if |
| 685 | // applicable. |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 686 | bool IsTryBody = (Body && isa<CXXTryStmt>(Body)); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 687 | if (IsTryBody) |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 688 | EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 689 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 690 | EHScopeStack::stable_iterator CleanupDepth = EHStack.stable_begin(); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 691 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 692 | // Emit the constructor prologue, i.e. the base and member |
| 693 | // initializers. |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 694 | EmitCtorPrologue(Ctor, CtorType, Args); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 695 | |
| 696 | // Emit the body of the statement. |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 697 | if (IsTryBody) |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 698 | EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock()); |
| 699 | else if (Body) |
| 700 | EmitStmt(Body); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 701 | |
| 702 | // Emit any cleanup blocks associated with the member or base |
| 703 | // initializers, which includes (along the exceptional path) the |
| 704 | // destructors for those members and bases that were fully |
| 705 | // constructed. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 706 | PopCleanupBlocks(CleanupDepth); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 707 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 708 | if (IsTryBody) |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 709 | ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 712 | /// EmitCtorPrologue - This routine generates necessary code to initialize |
| 713 | /// base classes and non-static data members belonging to this constructor. |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 714 | void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 715 | CXXCtorType CtorType, |
| 716 | FunctionArgList &Args) { |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 717 | const CXXRecordDecl *ClassDecl = CD->getParent(); |
Anders Carlsson | a78fa2c | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 718 | |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 719 | llvm::SmallVector<CXXCtorInitializer *, 8> MemberInitializers; |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 720 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 721 | for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(), |
| 722 | E = CD->init_end(); |
| 723 | B != E; ++B) { |
Sean Hunt | cbb6748 | 2011-01-08 20:30:50 +0000 | [diff] [blame] | 724 | CXXCtorInitializer *Member = (*B); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 725 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 726 | if (Member->isBaseInitializer()) |
| 727 | EmitBaseInitializer(*this, ClassDecl, Member, CtorType); |
| 728 | else |
Anders Carlsson | a78fa2c | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 729 | MemberInitializers.push_back(Member); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 732 | InitializeVTablePointers(ClassDecl); |
Anders Carlsson | a78fa2c | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 733 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 734 | for (unsigned I = 0, E = MemberInitializers.size(); I != E; ++I) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 735 | EmitMemberInitializer(*this, ClassDecl, MemberInitializers[I], CD, Args); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 736 | } |
| 737 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 738 | /// EmitDestructorBody - Emits the body of the current destructor. |
| 739 | void CodeGenFunction::EmitDestructorBody(FunctionArgList &Args) { |
| 740 | const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CurGD.getDecl()); |
| 741 | CXXDtorType DtorType = CurGD.getDtorType(); |
| 742 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 743 | // The call to operator delete in a deleting destructor happens |
| 744 | // outside of the function-try-block, which means it's always |
| 745 | // possible to delegate the destructor body to the complete |
| 746 | // destructor. Do so. |
| 747 | if (DtorType == Dtor_Deleting) { |
| 748 | EnterDtorCleanups(Dtor, Dtor_Deleting); |
| 749 | EmitCXXDestructorCall(Dtor, Dtor_Complete, /*ForVirtualBase=*/false, |
| 750 | LoadCXXThis()); |
| 751 | PopCleanupBlock(); |
| 752 | return; |
| 753 | } |
| 754 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 755 | Stmt *Body = Dtor->getBody(); |
| 756 | |
| 757 | // If the body is a function-try-block, enter the try before |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 758 | // anything else. |
| 759 | bool isTryBody = (Body && isa<CXXTryStmt>(Body)); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 760 | if (isTryBody) |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 761 | EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 762 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 763 | // Enter the epilogue cleanups. |
| 764 | RunCleanupsScope DtorEpilogue(*this); |
| 765 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 766 | // If this is the complete variant, just invoke the base variant; |
| 767 | // the epilogue will destruct the virtual bases. But we can't do |
| 768 | // this optimization if the body is a function-try-block, because |
| 769 | // we'd introduce *two* handler blocks. |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 770 | switch (DtorType) { |
| 771 | case Dtor_Deleting: llvm_unreachable("already handled deleting case"); |
| 772 | |
| 773 | case Dtor_Complete: |
| 774 | // Enter the cleanup scopes for virtual bases. |
| 775 | EnterDtorCleanups(Dtor, Dtor_Complete); |
| 776 | |
| 777 | if (!isTryBody) { |
| 778 | EmitCXXDestructorCall(Dtor, Dtor_Base, /*ForVirtualBase=*/false, |
| 779 | LoadCXXThis()); |
| 780 | break; |
| 781 | } |
| 782 | // Fallthrough: act like we're in the base variant. |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 783 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 784 | case Dtor_Base: |
| 785 | // Enter the cleanup scopes for fields and non-virtual bases. |
| 786 | EnterDtorCleanups(Dtor, Dtor_Base); |
| 787 | |
| 788 | // Initialize the vtable pointers before entering the body. |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 789 | InitializeVTablePointers(Dtor->getParent()); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 790 | |
| 791 | if (isTryBody) |
| 792 | EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock()); |
| 793 | else if (Body) |
| 794 | EmitStmt(Body); |
| 795 | else { |
| 796 | assert(Dtor->isImplicit() && "bodyless dtor not implicit"); |
| 797 | // nothing to do besides what's in the epilogue |
| 798 | } |
Fariborz Jahanian | 5abec14 | 2011-02-02 23:12:46 +0000 | [diff] [blame] | 799 | // -fapple-kext must inline any call to this dtor into |
| 800 | // the caller's body. |
| 801 | if (getContext().getLangOptions().AppleKext) |
| 802 | CurFn->addFnAttr(llvm::Attribute::AlwaysInline); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 803 | break; |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 804 | } |
| 805 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 806 | // Jump out through the epilogue cleanups. |
| 807 | DtorEpilogue.ForceCleanup(); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 808 | |
| 809 | // Exit the try if applicable. |
| 810 | if (isTryBody) |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 811 | ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 812 | } |
| 813 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 814 | namespace { |
| 815 | /// Call the operator delete associated with the current destructor. |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 816 | struct CallDtorDelete : EHScopeStack::Cleanup { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 817 | CallDtorDelete() {} |
| 818 | |
| 819 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 820 | const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CGF.CurCodeDecl); |
| 821 | const CXXRecordDecl *ClassDecl = Dtor->getParent(); |
| 822 | CGF.EmitDeleteCall(Dtor->getOperatorDelete(), CGF.LoadCXXThis(), |
| 823 | CGF.getContext().getTagDeclType(ClassDecl)); |
| 824 | } |
| 825 | }; |
| 826 | |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 827 | struct CallArrayFieldDtor : EHScopeStack::Cleanup { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 828 | const FieldDecl *Field; |
| 829 | CallArrayFieldDtor(const FieldDecl *Field) : Field(Field) {} |
| 830 | |
| 831 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 832 | QualType FieldType = Field->getType(); |
| 833 | const ConstantArrayType *Array = |
| 834 | CGF.getContext().getAsConstantArrayType(FieldType); |
| 835 | |
| 836 | QualType BaseType = |
| 837 | CGF.getContext().getBaseElementType(Array->getElementType()); |
| 838 | const CXXRecordDecl *FieldClassDecl = BaseType->getAsCXXRecordDecl(); |
| 839 | |
| 840 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
| 841 | LValue LHS = CGF.EmitLValueForField(ThisPtr, Field, |
| 842 | // FIXME: Qualifiers? |
| 843 | /*CVRQualifiers=*/0); |
| 844 | |
| 845 | const llvm::Type *BasePtr = CGF.ConvertType(BaseType)->getPointerTo(); |
| 846 | llvm::Value *BaseAddrPtr = |
| 847 | CGF.Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
| 848 | CGF.EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(), |
| 849 | Array, BaseAddrPtr); |
| 850 | } |
| 851 | }; |
| 852 | |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 853 | struct CallFieldDtor : EHScopeStack::Cleanup { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 854 | const FieldDecl *Field; |
| 855 | CallFieldDtor(const FieldDecl *Field) : Field(Field) {} |
| 856 | |
| 857 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 858 | const CXXRecordDecl *FieldClassDecl = |
| 859 | Field->getType()->getAsCXXRecordDecl(); |
| 860 | |
| 861 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
| 862 | LValue LHS = CGF.EmitLValueForField(ThisPtr, Field, |
| 863 | // FIXME: Qualifiers? |
| 864 | /*CVRQualifiers=*/0); |
| 865 | |
| 866 | CGF.EmitCXXDestructorCall(FieldClassDecl->getDestructor(), |
| 867 | Dtor_Complete, /*ForVirtualBase=*/false, |
| 868 | LHS.getAddress()); |
| 869 | } |
| 870 | }; |
| 871 | } |
| 872 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 873 | /// EmitDtorEpilogue - Emit all code that comes at the end of class's |
| 874 | /// destructor. This is to call destructors on members and base classes |
| 875 | /// in reverse order of their construction. |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 876 | void CodeGenFunction::EnterDtorCleanups(const CXXDestructorDecl *DD, |
| 877 | CXXDtorType DtorType) { |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 878 | assert(!DD->isTrivial() && |
| 879 | "Should not emit dtor epilogue for trivial dtor!"); |
| 880 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 881 | // The deleting-destructor phase just needs to call the appropriate |
| 882 | // operator delete that Sema picked up. |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 883 | if (DtorType == Dtor_Deleting) { |
| 884 | assert(DD->getOperatorDelete() && |
| 885 | "operator delete missing - EmitDtorEpilogue"); |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 886 | EHStack.pushCleanup<CallDtorDelete>(NormalAndEHCleanup); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 887 | return; |
| 888 | } |
| 889 | |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 890 | const CXXRecordDecl *ClassDecl = DD->getParent(); |
| 891 | |
| 892 | // The complete-destructor phase just destructs all the virtual bases. |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 893 | if (DtorType == Dtor_Complete) { |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 894 | |
| 895 | // We push them in the forward order so that they'll be popped in |
| 896 | // the reverse order. |
| 897 | for (CXXRecordDecl::base_class_const_iterator I = |
| 898 | ClassDecl->vbases_begin(), E = ClassDecl->vbases_end(); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 899 | I != E; ++I) { |
| 900 | const CXXBaseSpecifier &Base = *I; |
| 901 | CXXRecordDecl *BaseClassDecl |
| 902 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| 903 | |
| 904 | // Ignore trivial destructors. |
| 905 | if (BaseClassDecl->hasTrivialDestructor()) |
| 906 | continue; |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 907 | |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 908 | EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup, |
| 909 | BaseClassDecl, |
| 910 | /*BaseIsVirtual*/ true); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 911 | } |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 912 | |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 913 | return; |
| 914 | } |
| 915 | |
| 916 | assert(DtorType == Dtor_Base); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 917 | |
| 918 | // Destroy non-virtual bases. |
| 919 | for (CXXRecordDecl::base_class_const_iterator I = |
| 920 | ClassDecl->bases_begin(), E = ClassDecl->bases_end(); I != E; ++I) { |
| 921 | const CXXBaseSpecifier &Base = *I; |
| 922 | |
| 923 | // Ignore virtual bases. |
| 924 | if (Base.isVirtual()) |
| 925 | continue; |
| 926 | |
| 927 | CXXRecordDecl *BaseClassDecl = Base.getType()->getAsCXXRecordDecl(); |
| 928 | |
| 929 | // Ignore trivial destructors. |
| 930 | if (BaseClassDecl->hasTrivialDestructor()) |
| 931 | continue; |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 932 | |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 933 | EHStack.pushCleanup<CallBaseDtor>(NormalAndEHCleanup, |
| 934 | BaseClassDecl, |
| 935 | /*BaseIsVirtual*/ false); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | // Destroy direct fields. |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 939 | llvm::SmallVector<const FieldDecl *, 16> FieldDecls; |
| 940 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 941 | E = ClassDecl->field_end(); I != E; ++I) { |
| 942 | const FieldDecl *Field = *I; |
| 943 | |
| 944 | QualType FieldType = getContext().getCanonicalType(Field->getType()); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 945 | const ConstantArrayType *Array = |
| 946 | getContext().getAsConstantArrayType(FieldType); |
| 947 | if (Array) |
| 948 | FieldType = getContext().getBaseElementType(Array->getElementType()); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 949 | |
| 950 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 951 | if (!RT) |
| 952 | continue; |
| 953 | |
| 954 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 955 | if (FieldClassDecl->hasTrivialDestructor()) |
| 956 | continue; |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 957 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 958 | if (Array) |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 959 | EHStack.pushCleanup<CallArrayFieldDtor>(NormalAndEHCleanup, Field); |
John McCall | 50da2ca | 2010-07-21 05:30:47 +0000 | [diff] [blame] | 960 | else |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 961 | EHStack.pushCleanup<CallFieldDtor>(NormalAndEHCleanup, Field); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 962 | } |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 965 | /// EmitCXXAggrConstructorCall - This routine essentially creates a (nested) |
| 966 | /// for-loop to call the default constructor on individual members of the |
| 967 | /// array. |
| 968 | /// 'D' is the default constructor for elements of the array, 'ArrayTy' is the |
| 969 | /// array type and 'ArrayPtr' points to the beginning fo the array. |
| 970 | /// It is assumed that all relevant checks have been made by the caller. |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 971 | /// |
| 972 | /// \param ZeroInitialization True if each element should be zero-initialized |
| 973 | /// before it is constructed. |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 974 | void |
| 975 | CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 976 | const ConstantArrayType *ArrayTy, |
| 977 | llvm::Value *ArrayPtr, |
| 978 | CallExpr::const_arg_iterator ArgBeg, |
| 979 | CallExpr::const_arg_iterator ArgEnd, |
| 980 | bool ZeroInitialization) { |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 981 | |
| 982 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 983 | llvm::Value * NumElements = |
| 984 | llvm::ConstantInt::get(SizeTy, |
| 985 | getContext().getConstantArrayElementCount(ArrayTy)); |
| 986 | |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 987 | EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr, ArgBeg, ArgEnd, |
| 988 | ZeroInitialization); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | void |
| 992 | CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
| 993 | llvm::Value *NumElements, |
| 994 | llvm::Value *ArrayPtr, |
| 995 | CallExpr::const_arg_iterator ArgBeg, |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 996 | CallExpr::const_arg_iterator ArgEnd, |
| 997 | bool ZeroInitialization) { |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 998 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 999 | |
| 1000 | // Create a temporary for the loop index and initialize it with 0. |
| 1001 | llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index"); |
| 1002 | llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy); |
| 1003 | Builder.CreateStore(Zero, IndexPtr); |
| 1004 | |
| 1005 | // Start the loop with a block that tests the condition. |
| 1006 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 1007 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
| 1008 | |
| 1009 | EmitBlock(CondBlock); |
| 1010 | |
| 1011 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 1012 | |
| 1013 | // Generate: if (loop-index < number-of-elements fall to the loop body, |
| 1014 | // otherwise, go to the block after the for-loop. |
| 1015 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 1016 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless"); |
| 1017 | // If the condition is true, execute the body. |
| 1018 | Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
| 1019 | |
| 1020 | EmitBlock(ForBody); |
| 1021 | |
| 1022 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 1023 | // Inside the loop body, emit the constructor call on the array element. |
| 1024 | Counter = Builder.CreateLoad(IndexPtr); |
| 1025 | llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter, |
| 1026 | "arrayidx"); |
| 1027 | |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame] | 1028 | // Zero initialize the storage, if requested. |
| 1029 | if (ZeroInitialization) |
| 1030 | EmitNullInitialization(Address, |
| 1031 | getContext().getTypeDeclType(D->getParent())); |
| 1032 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1033 | // C++ [class.temporary]p4: |
| 1034 | // There are two contexts in which temporaries are destroyed at a different |
| 1035 | // point than the end of the full-expression. The first context is when a |
| 1036 | // default constructor is called to initialize an element of an array. |
| 1037 | // If the constructor has one or more default arguments, the destruction of |
| 1038 | // every temporary created in a default argument expression is sequenced |
| 1039 | // before the construction of the next array element, if any. |
| 1040 | |
| 1041 | // Keep track of the current number of live temporaries. |
Anders Carlsson | 44ec82b | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 1042 | { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1043 | RunCleanupsScope Scope(*this); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1044 | |
Anders Carlsson | 155ed4a | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 1045 | EmitCXXConstructorCall(D, Ctor_Complete, /*ForVirtualBase=*/false, Address, |
Anders Carlsson | 24eb78e | 2010-05-02 23:01:10 +0000 | [diff] [blame] | 1046 | ArgBeg, ArgEnd); |
Anders Carlsson | 44ec82b | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 1047 | } |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1048 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1049 | EmitBlock(ContinueBlock); |
| 1050 | |
| 1051 | // Emit the increment of the loop counter. |
| 1052 | llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1); |
| 1053 | Counter = Builder.CreateLoad(IndexPtr); |
| 1054 | NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); |
| 1055 | Builder.CreateStore(NextVal, IndexPtr); |
| 1056 | |
| 1057 | // Finally, branch back up to the condition for the next iteration. |
| 1058 | EmitBranch(CondBlock); |
| 1059 | |
| 1060 | // Emit the fall-through block. |
| 1061 | EmitBlock(AfterFor, true); |
| 1062 | } |
| 1063 | |
| 1064 | /// EmitCXXAggrDestructorCall - calls the default destructor on array |
| 1065 | /// elements in reverse order of construction. |
| 1066 | void |
| 1067 | CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, |
| 1068 | const ArrayType *Array, |
| 1069 | llvm::Value *This) { |
| 1070 | const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); |
| 1071 | assert(CA && "Do we support VLA for destruction ?"); |
| 1072 | uint64_t ElementCount = getContext().getConstantArrayElementCount(CA); |
| 1073 | |
| 1074 | const llvm::Type *SizeLTy = ConvertType(getContext().getSizeType()); |
| 1075 | llvm::Value* ElementCountPtr = llvm::ConstantInt::get(SizeLTy, ElementCount); |
| 1076 | EmitCXXAggrDestructorCall(D, ElementCountPtr, This); |
| 1077 | } |
| 1078 | |
| 1079 | /// EmitCXXAggrDestructorCall - calls the default destructor on array |
| 1080 | /// elements in reverse order of construction. |
| 1081 | void |
| 1082 | CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, |
| 1083 | llvm::Value *UpperCount, |
| 1084 | llvm::Value *This) { |
| 1085 | const llvm::Type *SizeLTy = ConvertType(getContext().getSizeType()); |
| 1086 | llvm::Value *One = llvm::ConstantInt::get(SizeLTy, 1); |
| 1087 | |
| 1088 | // Create a temporary for the loop index and initialize it with count of |
| 1089 | // array elements. |
| 1090 | llvm::Value *IndexPtr = CreateTempAlloca(SizeLTy, "loop.index"); |
| 1091 | |
| 1092 | // Store the number of elements in the index pointer. |
| 1093 | Builder.CreateStore(UpperCount, IndexPtr); |
| 1094 | |
| 1095 | // Start the loop with a block that tests the condition. |
| 1096 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 1097 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
| 1098 | |
| 1099 | EmitBlock(CondBlock); |
| 1100 | |
| 1101 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 1102 | |
| 1103 | // Generate: if (loop-index != 0 fall to the loop body, |
| 1104 | // otherwise, go to the block after the for-loop. |
| 1105 | llvm::Value* zeroConstant = |
| 1106 | llvm::Constant::getNullValue(SizeLTy); |
| 1107 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 1108 | llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant, |
| 1109 | "isne"); |
| 1110 | // If the condition is true, execute the body. |
| 1111 | Builder.CreateCondBr(IsNE, ForBody, AfterFor); |
| 1112 | |
| 1113 | EmitBlock(ForBody); |
| 1114 | |
| 1115 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 1116 | // Inside the loop body, emit the constructor call on the array element. |
| 1117 | Counter = Builder.CreateLoad(IndexPtr); |
| 1118 | Counter = Builder.CreateSub(Counter, One); |
| 1119 | llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx"); |
Anders Carlsson | 8e6404c | 2010-05-02 23:29:11 +0000 | [diff] [blame] | 1120 | EmitCXXDestructorCall(D, Dtor_Complete, /*ForVirtualBase=*/false, Address); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1121 | |
| 1122 | EmitBlock(ContinueBlock); |
| 1123 | |
| 1124 | // Emit the decrement of the loop counter. |
| 1125 | Counter = Builder.CreateLoad(IndexPtr); |
| 1126 | Counter = Builder.CreateSub(Counter, One, "dec"); |
| 1127 | Builder.CreateStore(Counter, IndexPtr); |
| 1128 | |
| 1129 | // Finally, branch back up to the condition for the next iteration. |
| 1130 | EmitBranch(CondBlock); |
| 1131 | |
| 1132 | // Emit the fall-through block. |
| 1133 | EmitBlock(AfterFor, true); |
| 1134 | } |
| 1135 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1136 | void |
| 1137 | CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, |
Anders Carlsson | 155ed4a | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 1138 | CXXCtorType Type, bool ForVirtualBase, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1139 | llvm::Value *This, |
| 1140 | CallExpr::const_arg_iterator ArgBeg, |
| 1141 | CallExpr::const_arg_iterator ArgEnd) { |
Devang Patel | 3ee36af | 2011-02-22 20:55:26 +0000 | [diff] [blame] | 1142 | |
| 1143 | CGDebugInfo *DI = getDebugInfo(); |
| 1144 | if (DI && CGM.getCodeGenOpts().LimitDebugInfo) { |
| 1145 | // If debug info for this class has been emitted then this is the right time |
| 1146 | // to do so. |
| 1147 | const CXXRecordDecl *Parent = D->getParent(); |
| 1148 | DI->getOrCreateRecordType(CGM.getContext().getTypeDeclType(Parent), |
| 1149 | Parent->getLocation()); |
| 1150 | } |
| 1151 | |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1152 | if (D->isTrivial()) { |
| 1153 | if (ArgBeg == ArgEnd) { |
| 1154 | // Trivial default constructor, no codegen required. |
| 1155 | assert(D->isDefaultConstructor() && |
| 1156 | "trivial 0-arg ctor not a default ctor"); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1157 | return; |
| 1158 | } |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1159 | |
| 1160 | assert(ArgBeg + 1 == ArgEnd && "unexpected argcount for trivial ctor"); |
| 1161 | assert(D->isCopyConstructor() && "trivial 1-arg ctor not a copy ctor"); |
| 1162 | |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1163 | const Expr *E = (*ArgBeg); |
| 1164 | QualType Ty = E->getType(); |
| 1165 | llvm::Value *Src = EmitLValue(E).getAddress(); |
| 1166 | EmitAggregateCopy(This, Src, Ty); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1167 | return; |
| 1168 | } |
| 1169 | |
Anders Carlsson | 314e622 | 2010-05-02 23:33:10 +0000 | [diff] [blame] | 1170 | llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(D, Type), ForVirtualBase); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1171 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type); |
| 1172 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1173 | EmitCXXMemberCall(D, Callee, ReturnValueSlot(), This, VTT, ArgBeg, ArgEnd); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1176 | void |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1177 | CodeGenFunction::EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D, |
| 1178 | llvm::Value *This, llvm::Value *Src, |
| 1179 | CallExpr::const_arg_iterator ArgBeg, |
| 1180 | CallExpr::const_arg_iterator ArgEnd) { |
| 1181 | if (D->isTrivial()) { |
| 1182 | assert(ArgBeg + 1 == ArgEnd && "unexpected argcount for trivial ctor"); |
| 1183 | assert(D->isCopyConstructor() && "trivial 1-arg ctor not a copy ctor"); |
| 1184 | EmitAggregateCopy(This, Src, (*ArgBeg)->getType()); |
| 1185 | return; |
| 1186 | } |
| 1187 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, |
| 1188 | clang::Ctor_Complete); |
| 1189 | assert(D->isInstance() && |
| 1190 | "Trying to emit a member call expr on a static method!"); |
| 1191 | |
| 1192 | const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>(); |
| 1193 | |
| 1194 | CallArgList Args; |
| 1195 | |
| 1196 | // Push the this ptr. |
| 1197 | Args.push_back(std::make_pair(RValue::get(This), |
| 1198 | D->getThisType(getContext()))); |
| 1199 | |
| 1200 | |
| 1201 | // Push the src ptr. |
| 1202 | QualType QT = *(FPT->arg_type_begin()); |
| 1203 | const llvm::Type *t = CGM.getTypes().ConvertType(QT); |
| 1204 | Src = Builder.CreateBitCast(Src, t); |
| 1205 | Args.push_back(std::make_pair(RValue::get(Src), QT)); |
| 1206 | |
| 1207 | // Skip over first argument (Src). |
| 1208 | ++ArgBeg; |
| 1209 | CallExpr::const_arg_iterator Arg = ArgBeg; |
| 1210 | for (FunctionProtoType::arg_type_iterator I = FPT->arg_type_begin()+1, |
| 1211 | E = FPT->arg_type_end(); I != E; ++I, ++Arg) { |
| 1212 | assert(Arg != ArgEnd && "Running over edge of argument list!"); |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1213 | EmitCallArg(Args, *Arg, *I); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1214 | } |
| 1215 | // Either we've emitted all the call args, or we have a call to a |
| 1216 | // variadic function. |
| 1217 | assert((Arg == ArgEnd || FPT->isVariadic()) && |
| 1218 | "Extra arguments in non-variadic function!"); |
| 1219 | // If we still have any arguments, emit them using the type of the argument. |
| 1220 | for (; Arg != ArgEnd; ++Arg) { |
| 1221 | QualType ArgType = Arg->getType(); |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1222 | EmitCallArg(Args, *Arg, ArgType); |
Fariborz Jahanian | 3499987 | 2010-11-13 21:53:34 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | QualType ResultType = FPT->getResultType(); |
| 1226 | EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args, |
| 1227 | FPT->getExtInfo()), |
| 1228 | Callee, ReturnValueSlot(), Args, D); |
| 1229 | } |
| 1230 | |
| 1231 | void |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1232 | CodeGenFunction::EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor, |
| 1233 | CXXCtorType CtorType, |
| 1234 | const FunctionArgList &Args) { |
| 1235 | CallArgList DelegateArgs; |
| 1236 | |
| 1237 | FunctionArgList::const_iterator I = Args.begin(), E = Args.end(); |
| 1238 | assert(I != E && "no parameters to constructor"); |
| 1239 | |
| 1240 | // this |
| 1241 | DelegateArgs.push_back(std::make_pair(RValue::get(LoadCXXThis()), |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1242 | (*I)->getType())); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1243 | ++I; |
| 1244 | |
| 1245 | // vtt |
Anders Carlsson | 314e622 | 2010-05-02 23:33:10 +0000 | [diff] [blame] | 1246 | if (llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(Ctor, CtorType), |
| 1247 | /*ForVirtualBase=*/false)) { |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1248 | QualType VoidPP = getContext().getPointerType(getContext().VoidPtrTy); |
| 1249 | DelegateArgs.push_back(std::make_pair(RValue::get(VTT), VoidPP)); |
| 1250 | |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 1251 | if (CodeGenVTables::needsVTTParameter(CurGD)) { |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1252 | assert(I != E && "cannot skip vtt parameter, already done with args"); |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 1253 | assert((*I)->getType() == VoidPP && "skipping parameter not of vtt type"); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1254 | ++I; |
| 1255 | } |
| 1256 | } |
| 1257 | |
| 1258 | // Explicit arguments. |
| 1259 | for (; I != E; ++I) { |
John McCall | 413ebdb | 2011-03-11 20:59:21 +0000 | [diff] [blame] | 1260 | const VarDecl *param = *I; |
| 1261 | EmitDelegateCallArg(DelegateArgs, param); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | EmitCall(CGM.getTypes().getFunctionInfo(Ctor, CtorType), |
| 1265 | CGM.GetAddrOfCXXConstructor(Ctor, CtorType), |
| 1266 | ReturnValueSlot(), DelegateArgs, Ctor); |
| 1267 | } |
| 1268 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1269 | void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *DD, |
| 1270 | CXXDtorType Type, |
Anders Carlsson | 8e6404c | 2010-05-02 23:29:11 +0000 | [diff] [blame] | 1271 | bool ForVirtualBase, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1272 | llvm::Value *This) { |
Anders Carlsson | 314e622 | 2010-05-02 23:33:10 +0000 | [diff] [blame] | 1273 | llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(DD, Type), |
| 1274 | ForVirtualBase); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 1275 | llvm::Value *Callee = 0; |
| 1276 | if (getContext().getLangOptions().AppleKext) |
Fariborz Jahanian | 771c678 | 2011-02-03 19:27:17 +0000 | [diff] [blame] | 1277 | Callee = BuildAppleKextVirtualDestructorCall(DD, Type, |
| 1278 | DD->getParent()); |
Fariborz Jahanian | ccd5259 | 2011-02-01 23:22:34 +0000 | [diff] [blame] | 1279 | |
| 1280 | if (!Callee) |
| 1281 | Callee = CGM.GetAddrOfCXXDestructor(DD, Type); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1282 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1283 | EmitCXXMemberCall(DD, Callee, ReturnValueSlot(), This, VTT, 0, 0); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
John McCall | 291ae94 | 2010-07-21 01:41:18 +0000 | [diff] [blame] | 1286 | namespace { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1287 | struct CallLocalDtor : EHScopeStack::Cleanup { |
John McCall | 291ae94 | 2010-07-21 01:41:18 +0000 | [diff] [blame] | 1288 | const CXXDestructorDecl *Dtor; |
| 1289 | llvm::Value *Addr; |
| 1290 | |
| 1291 | CallLocalDtor(const CXXDestructorDecl *D, llvm::Value *Addr) |
| 1292 | : Dtor(D), Addr(Addr) {} |
| 1293 | |
| 1294 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 1295 | CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, |
| 1296 | /*ForVirtualBase=*/false, Addr); |
| 1297 | } |
| 1298 | }; |
| 1299 | } |
| 1300 | |
John McCall | 81407d4 | 2010-07-21 06:29:51 +0000 | [diff] [blame] | 1301 | void CodeGenFunction::PushDestructorCleanup(const CXXDestructorDecl *D, |
| 1302 | llvm::Value *Addr) { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1303 | EHStack.pushCleanup<CallLocalDtor>(NormalAndEHCleanup, D, Addr); |
John McCall | 81407d4 | 2010-07-21 06:29:51 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1306 | void CodeGenFunction::PushDestructorCleanup(QualType T, llvm::Value *Addr) { |
| 1307 | CXXRecordDecl *ClassDecl = T->getAsCXXRecordDecl(); |
| 1308 | if (!ClassDecl) return; |
| 1309 | if (ClassDecl->hasTrivialDestructor()) return; |
| 1310 | |
| 1311 | const CXXDestructorDecl *D = ClassDecl->getDestructor(); |
John McCall | 81407d4 | 2010-07-21 06:29:51 +0000 | [diff] [blame] | 1312 | PushDestructorCleanup(D, Addr); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1315 | llvm::Value * |
Anders Carlsson | bb7e17b | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 1316 | CodeGenFunction::GetVirtualBaseClassOffset(llvm::Value *This, |
| 1317 | const CXXRecordDecl *ClassDecl, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1318 | const CXXRecordDecl *BaseClassDecl) { |
Dan Gohman | 043fb9a | 2010-10-26 18:44:08 +0000 | [diff] [blame] | 1319 | llvm::Value *VTablePtr = GetVTablePtr(This, Int8PtrTy); |
Ken Dyck | 14c65ca | 2011-04-07 12:37:09 +0000 | [diff] [blame^] | 1320 | CharUnits VBaseOffsetOffset = |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 1321 | CGM.getVTables().getVirtualBaseOffsetOffset(ClassDecl, BaseClassDecl); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1322 | |
| 1323 | llvm::Value *VBaseOffsetPtr = |
Ken Dyck | 14c65ca | 2011-04-07 12:37:09 +0000 | [diff] [blame^] | 1324 | Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetOffset.getQuantity(), |
| 1325 | "vbase.offset.ptr"); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1326 | const llvm::Type *PtrDiffTy = |
| 1327 | ConvertType(getContext().getPointerDiffType()); |
| 1328 | |
| 1329 | VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr, |
| 1330 | PtrDiffTy->getPointerTo()); |
| 1331 | |
| 1332 | llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset"); |
| 1333 | |
| 1334 | return VBaseOffset; |
| 1335 | } |
| 1336 | |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1337 | void |
| 1338 | CodeGenFunction::InitializeVTablePointer(BaseSubobject Base, |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1339 | const CXXRecordDecl *NearestVBase, |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1340 | CharUnits OffsetFromNearestVBase, |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1341 | llvm::Constant *VTable, |
| 1342 | const CXXRecordDecl *VTableClass) { |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1343 | const CXXRecordDecl *RD = Base.getBase(); |
| 1344 | |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1345 | // Compute the address point. |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1346 | llvm::Value *VTableAddressPoint; |
Anders Carlsson | 851853d | 2010-03-29 02:38:51 +0000 | [diff] [blame] | 1347 | |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1348 | // Check if we need to use a vtable from the VTT. |
Anders Carlsson | 851853d | 2010-03-29 02:38:51 +0000 | [diff] [blame] | 1349 | if (CodeGenVTables::needsVTTParameter(CurGD) && |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1350 | (RD->getNumVBases() || NearestVBase)) { |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1351 | // Get the secondary vpointer index. |
| 1352 | uint64_t VirtualPointerIndex = |
| 1353 | CGM.getVTables().getSecondaryVirtualPointerIndex(VTableClass, Base); |
| 1354 | |
| 1355 | /// Load the VTT. |
| 1356 | llvm::Value *VTT = LoadCXXVTT(); |
| 1357 | if (VirtualPointerIndex) |
| 1358 | VTT = Builder.CreateConstInBoundsGEP1_64(VTT, VirtualPointerIndex); |
| 1359 | |
| 1360 | // And load the address point from the VTT. |
| 1361 | VTableAddressPoint = Builder.CreateLoad(VTT); |
| 1362 | } else { |
Anders Carlsson | 64c9eca | 2010-03-29 02:08:26 +0000 | [diff] [blame] | 1363 | uint64_t AddressPoint = CGM.getVTables().getAddressPoint(Base, VTableClass); |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1364 | VTableAddressPoint = |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1365 | Builder.CreateConstInBoundsGEP2_64(VTable, 0, AddressPoint); |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1366 | } |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1367 | |
Anders Carlsson | 36fd6be | 2010-04-20 16:22:16 +0000 | [diff] [blame] | 1368 | // Compute where to store the address point. |
Anders Carlsson | 8246cc7 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 1369 | llvm::Value *VirtualOffset = 0; |
Ken Dyck | 9a8ad9b | 2011-03-23 00:45:26 +0000 | [diff] [blame] | 1370 | CharUnits NonVirtualOffset = CharUnits::Zero(); |
Anders Carlsson | 3e79c30 | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 1371 | |
| 1372 | if (CodeGenVTables::needsVTTParameter(CurGD) && NearestVBase) { |
| 1373 | // We need to use the virtual base offset offset because the virtual base |
| 1374 | // might have a different offset in the most derived class. |
Anders Carlsson | 8246cc7 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 1375 | VirtualOffset = GetVirtualBaseClassOffset(LoadCXXThis(), VTableClass, |
| 1376 | NearestVBase); |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1377 | NonVirtualOffset = OffsetFromNearestVBase; |
Anders Carlsson | 3e79c30 | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 1378 | } else { |
Anders Carlsson | 8246cc7 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 1379 | // We can just use the base offset in the complete class. |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 1380 | NonVirtualOffset = Base.getBaseOffset(); |
Anders Carlsson | 3e79c30 | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 1381 | } |
Anders Carlsson | 8246cc7 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 1382 | |
| 1383 | // Apply the offsets. |
| 1384 | llvm::Value *VTableField = LoadCXXThis(); |
| 1385 | |
Ken Dyck | 9a8ad9b | 2011-03-23 00:45:26 +0000 | [diff] [blame] | 1386 | if (!NonVirtualOffset.isZero() || VirtualOffset) |
Anders Carlsson | 8246cc7 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 1387 | VTableField = ApplyNonVirtualAndVirtualOffset(*this, VTableField, |
| 1388 | NonVirtualOffset, |
| 1389 | VirtualOffset); |
Anders Carlsson | 36fd6be | 2010-04-20 16:22:16 +0000 | [diff] [blame] | 1390 | |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1391 | // Finally, store the address point. |
| 1392 | const llvm::Type *AddressPointPtrTy = |
| 1393 | VTableAddressPoint->getType()->getPointerTo(); |
| 1394 | VTableField = Builder.CreateBitCast(VTableField, AddressPointPtrTy); |
| 1395 | Builder.CreateStore(VTableAddressPoint, VTableField); |
| 1396 | } |
| 1397 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1398 | void |
| 1399 | CodeGenFunction::InitializeVTablePointers(BaseSubobject Base, |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1400 | const CXXRecordDecl *NearestVBase, |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1401 | CharUnits OffsetFromNearestVBase, |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1402 | bool BaseIsNonVirtualPrimaryBase, |
| 1403 | llvm::Constant *VTable, |
| 1404 | const CXXRecordDecl *VTableClass, |
| 1405 | VisitedVirtualBasesSetTy& VBases) { |
| 1406 | // If this base is a non-virtual primary base the address point has already |
| 1407 | // been set. |
| 1408 | if (!BaseIsNonVirtualPrimaryBase) { |
| 1409 | // Initialize the vtable pointer for this base. |
Anders Carlsson | 4235840 | 2010-05-03 00:07:07 +0000 | [diff] [blame] | 1410 | InitializeVTablePointer(Base, NearestVBase, OffsetFromNearestVBase, |
| 1411 | VTable, VTableClass); |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | const CXXRecordDecl *RD = Base.getBase(); |
| 1415 | |
| 1416 | // Traverse bases. |
| 1417 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1418 | E = RD->bases_end(); I != E; ++I) { |
| 1419 | CXXRecordDecl *BaseDecl |
| 1420 | = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1421 | |
| 1422 | // Ignore classes without a vtable. |
| 1423 | if (!BaseDecl->isDynamicClass()) |
| 1424 | continue; |
| 1425 | |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1426 | CharUnits BaseOffset; |
| 1427 | CharUnits BaseOffsetFromNearestVBase; |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1428 | bool BaseDeclIsNonVirtualPrimaryBase; |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1429 | |
| 1430 | if (I->isVirtual()) { |
| 1431 | // Check if we've visited this virtual base before. |
| 1432 | if (!VBases.insert(BaseDecl)) |
| 1433 | continue; |
| 1434 | |
| 1435 | const ASTRecordLayout &Layout = |
| 1436 | getContext().getASTRecordLayout(VTableClass); |
| 1437 | |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1438 | BaseOffset = Layout.getVBaseClassOffset(BaseDecl); |
| 1439 | BaseOffsetFromNearestVBase = CharUnits::Zero(); |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1440 | BaseDeclIsNonVirtualPrimaryBase = false; |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1441 | } else { |
| 1442 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 1443 | |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 1444 | BaseOffset = Base.getBaseOffset() + Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | 4235840 | 2010-05-03 00:07:07 +0000 | [diff] [blame] | 1445 | BaseOffsetFromNearestVBase = |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1446 | OffsetFromNearestVBase + Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1447 | BaseDeclIsNonVirtualPrimaryBase = Layout.getPrimaryBase() == BaseDecl; |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 1450 | InitializeVTablePointers(BaseSubobject(BaseDecl, BaseOffset), |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1451 | I->isVirtual() ? BaseDecl : NearestVBase, |
Anders Carlsson | 4235840 | 2010-05-03 00:07:07 +0000 | [diff] [blame] | 1452 | BaseOffsetFromNearestVBase, |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1453 | BaseDeclIsNonVirtualPrimaryBase, |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1454 | VTable, VTableClass, VBases); |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | void CodeGenFunction::InitializeVTablePointers(const CXXRecordDecl *RD) { |
| 1459 | // Ignore classes without a vtable. |
Anders Carlsson | 0703690 | 2010-03-26 04:39:42 +0000 | [diff] [blame] | 1460 | if (!RD->isDynamicClass()) |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1461 | return; |
| 1462 | |
Anders Carlsson | 0703690 | 2010-03-26 04:39:42 +0000 | [diff] [blame] | 1463 | // Get the VTable. |
| 1464 | llvm::Constant *VTable = CGM.getVTables().GetAddrOfVTable(RD); |
Anders Carlsson | 5c6c1d9 | 2010-03-24 03:57:14 +0000 | [diff] [blame] | 1465 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1466 | // Initialize the vtable pointers for this class and all of its bases. |
| 1467 | VisitedVirtualBasesSetTy VBases; |
Ken Dyck | 4230d52 | 2011-03-24 01:21:01 +0000 | [diff] [blame] | 1468 | InitializeVTablePointers(BaseSubobject(RD, CharUnits::Zero()), |
| 1469 | /*NearestVBase=*/0, |
Ken Dyck | d6fb21f | 2011-03-23 01:04:18 +0000 | [diff] [blame] | 1470 | /*OffsetFromNearestVBase=*/CharUnits::Zero(), |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1471 | /*BaseIsNonVirtualPrimaryBase=*/false, |
| 1472 | VTable, RD, VBases); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1473 | } |
Dan Gohman | 043fb9a | 2010-10-26 18:44:08 +0000 | [diff] [blame] | 1474 | |
| 1475 | llvm::Value *CodeGenFunction::GetVTablePtr(llvm::Value *This, |
| 1476 | const llvm::Type *Ty) { |
| 1477 | llvm::Value *VTablePtrSrc = Builder.CreateBitCast(This, Ty->getPointerTo()); |
| 1478 | return Builder.CreateLoad(VTablePtrSrc, "vtable"); |
| 1479 | } |