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 | |
| 14 | #include "CodeGenFunction.h" |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 15 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 16 | #include "clang/AST/RecordLayout.h" |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtCXX.h" |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 18 | |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 19 | using namespace clang; |
| 20 | using namespace CodeGen; |
| 21 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 22 | static uint64_t |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 23 | ComputeNonVirtualBaseClassOffset(ASTContext &Context, |
| 24 | const CXXRecordDecl *DerivedClass, |
| 25 | CXXBaseSpecifierArray::iterator Start, |
| 26 | CXXBaseSpecifierArray::iterator End) { |
| 27 | uint64_t Offset = 0; |
| 28 | |
| 29 | const CXXRecordDecl *RD = DerivedClass; |
| 30 | |
| 31 | for (CXXBaseSpecifierArray::iterator I = Start; I != End; ++I) { |
| 32 | const CXXBaseSpecifier *Base = *I; |
| 33 | assert(!Base->isVirtual() && "Should not see virtual bases here!"); |
| 34 | |
| 35 | // Get the layout. |
| 36 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); |
| 37 | |
| 38 | const CXXRecordDecl *BaseDecl = |
| 39 | cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 40 | |
| 41 | // Add the offset. |
| 42 | Offset += Layout.getBaseClassOffset(BaseDecl); |
| 43 | |
| 44 | RD = BaseDecl; |
| 45 | } |
| 46 | |
| 47 | // FIXME: We should not use / 8 here. |
| 48 | return Offset / 8; |
| 49 | } |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 50 | |
Anders Carlsson | 84080ec | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 51 | llvm::Constant * |
Anders Carlsson | a04efdf | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 52 | CodeGenModule::GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl, |
| 53 | const CXXBaseSpecifierArray &BasePath) { |
| 54 | assert(!BasePath.empty() && "Base path should not be empty!"); |
| 55 | |
| 56 | uint64_t Offset = |
| 57 | ComputeNonVirtualBaseClassOffset(getContext(), ClassDecl, |
| 58 | BasePath.begin(), BasePath.end()); |
| 59 | if (!Offset) |
| 60 | return 0; |
| 61 | |
| 62 | const llvm::Type *PtrDiffTy = |
| 63 | Types.ConvertType(getContext().getPointerDiffType()); |
| 64 | |
| 65 | return llvm::ConstantInt::get(PtrDiffTy, Offset); |
Anders Carlsson | 84080ec | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 68 | /// Gets the address of a direct base class within a complete object. |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 69 | /// This should only be used for (1) non-virtual bases or (2) virtual bases |
| 70 | /// when the type is known to be complete (e.g. in complete destructors). |
| 71 | /// |
| 72 | /// The object pointed to by 'This' is assumed to be non-null. |
| 73 | llvm::Value * |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 74 | CodeGenFunction::GetAddressOfDirectBaseInCompleteClass(llvm::Value *This, |
| 75 | const CXXRecordDecl *Derived, |
| 76 | const CXXRecordDecl *Base, |
| 77 | bool BaseIsVirtual) { |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 78 | // 'this' must be a pointer (in some address space) to Derived. |
| 79 | assert(This->getType()->isPointerTy() && |
| 80 | cast<llvm::PointerType>(This->getType())->getElementType() |
| 81 | == ConvertType(Derived)); |
| 82 | |
| 83 | // Compute the offset of the virtual base. |
| 84 | uint64_t Offset; |
| 85 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Derived); |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 86 | if (BaseIsVirtual) |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 87 | Offset = Layout.getVBaseClassOffset(Base); |
| 88 | else |
| 89 | Offset = Layout.getBaseClassOffset(Base); |
| 90 | |
| 91 | // Shift and cast down to the base type. |
| 92 | // TODO: for complete types, this should be possible with a GEP. |
| 93 | llvm::Value *V = This; |
| 94 | if (Offset) { |
| 95 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext()); |
| 96 | V = Builder.CreateBitCast(V, Int8PtrTy); |
| 97 | V = Builder.CreateConstInBoundsGEP1_64(V, Offset / 8); |
| 98 | } |
| 99 | V = Builder.CreateBitCast(V, ConvertType(Base)->getPointerTo()); |
| 100 | |
| 101 | return V; |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 102 | } |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 103 | |
Anders Carlsson | 9dc228a | 2010-04-20 16:03:35 +0000 | [diff] [blame] | 104 | static llvm::Value * |
| 105 | ApplyNonVirtualAndVirtualOffset(CodeGenFunction &CGF, llvm::Value *ThisPtr, |
| 106 | uint64_t NonVirtual, llvm::Value *Virtual) { |
| 107 | const llvm::Type *PtrDiffTy = |
| 108 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
| 109 | |
| 110 | llvm::Value *NonVirtualOffset = 0; |
| 111 | if (NonVirtual) |
| 112 | NonVirtualOffset = llvm::ConstantInt::get(PtrDiffTy, NonVirtual); |
| 113 | |
| 114 | llvm::Value *BaseOffset; |
| 115 | if (Virtual) { |
| 116 | if (NonVirtualOffset) |
| 117 | BaseOffset = CGF.Builder.CreateAdd(Virtual, NonVirtualOffset); |
| 118 | else |
| 119 | BaseOffset = Virtual; |
| 120 | } else |
| 121 | BaseOffset = NonVirtualOffset; |
| 122 | |
| 123 | // Apply the base offset. |
| 124 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 125 | ThisPtr = CGF.Builder.CreateBitCast(ThisPtr, Int8PtrTy); |
| 126 | ThisPtr = CGF.Builder.CreateGEP(ThisPtr, BaseOffset, "add.ptr"); |
| 127 | |
| 128 | return ThisPtr; |
| 129 | } |
| 130 | |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 131 | llvm::Value * |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 132 | CodeGenFunction::GetAddressOfBaseClass(llvm::Value *Value, |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 133 | const CXXRecordDecl *Derived, |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 134 | const CXXBaseSpecifierArray &BasePath, |
| 135 | bool NullCheckValue) { |
| 136 | assert(!BasePath.empty() && "Base path should not be empty!"); |
| 137 | |
| 138 | CXXBaseSpecifierArray::iterator Start = BasePath.begin(); |
| 139 | const CXXRecordDecl *VBase = 0; |
| 140 | |
| 141 | // Get the virtual base. |
| 142 | if ((*Start)->isVirtual()) { |
| 143 | VBase = |
| 144 | cast<CXXRecordDecl>((*Start)->getType()->getAs<RecordType>()->getDecl()); |
| 145 | ++Start; |
| 146 | } |
| 147 | |
| 148 | uint64_t NonVirtualOffset = |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 149 | ComputeNonVirtualBaseClassOffset(getContext(), VBase ? VBase : Derived, |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 150 | Start, BasePath.end()); |
| 151 | |
| 152 | // Get the base pointer type. |
| 153 | const llvm::Type *BasePtrTy = |
Anders Carlsson | fc89c31 | 2010-04-24 21:12:55 +0000 | [diff] [blame] | 154 | ConvertType((BasePath.end()[-1])->getType())->getPointerTo(); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 155 | |
| 156 | if (!NonVirtualOffset && !VBase) { |
| 157 | // Just cast back. |
| 158 | return Builder.CreateBitCast(Value, BasePtrTy); |
| 159 | } |
| 160 | |
| 161 | llvm::BasicBlock *CastNull = 0; |
| 162 | llvm::BasicBlock *CastNotNull = 0; |
| 163 | llvm::BasicBlock *CastEnd = 0; |
| 164 | |
| 165 | if (NullCheckValue) { |
| 166 | CastNull = createBasicBlock("cast.null"); |
| 167 | CastNotNull = createBasicBlock("cast.notnull"); |
| 168 | CastEnd = createBasicBlock("cast.end"); |
| 169 | |
| 170 | llvm::Value *IsNull = |
| 171 | Builder.CreateICmpEQ(Value, |
| 172 | llvm::Constant::getNullValue(Value->getType())); |
| 173 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
| 174 | EmitBlock(CastNotNull); |
| 175 | } |
| 176 | |
| 177 | llvm::Value *VirtualOffset = 0; |
| 178 | |
| 179 | if (VBase) |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 180 | VirtualOffset = GetVirtualBaseClassOffset(Value, Derived, VBase); |
Anders Carlsson | 34a2d38 | 2010-04-24 21:06:20 +0000 | [diff] [blame] | 181 | |
| 182 | // Apply the offsets. |
| 183 | Value = ApplyNonVirtualAndVirtualOffset(*this, Value, NonVirtualOffset, |
| 184 | VirtualOffset); |
| 185 | |
| 186 | // Cast back. |
| 187 | Value = Builder.CreateBitCast(Value, BasePtrTy); |
| 188 | |
| 189 | if (NullCheckValue) { |
| 190 | Builder.CreateBr(CastEnd); |
| 191 | EmitBlock(CastNull); |
| 192 | Builder.CreateBr(CastEnd); |
| 193 | EmitBlock(CastEnd); |
| 194 | |
| 195 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType()); |
| 196 | PHI->reserveOperandSpace(2); |
| 197 | PHI->addIncoming(Value, CastNotNull); |
| 198 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), |
| 199 | CastNull); |
| 200 | Value = PHI; |
| 201 | } |
| 202 | |
| 203 | return Value; |
| 204 | } |
| 205 | |
| 206 | llvm::Value * |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 207 | CodeGenFunction::GetAddressOfDerivedClass(llvm::Value *Value, |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 208 | const CXXRecordDecl *Derived, |
Anders Carlsson | a04efdf | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 209 | const CXXBaseSpecifierArray &BasePath, |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 210 | bool NullCheckValue) { |
Anders Carlsson | a04efdf | 2010-04-24 21:23:59 +0000 | [diff] [blame] | 211 | assert(!BasePath.empty() && "Base path should not be empty!"); |
| 212 | |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 213 | QualType DerivedTy = |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 214 | getContext().getCanonicalType(getContext().getTagDeclType(Derived)); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 215 | const llvm::Type *DerivedPtrTy = ConvertType(DerivedTy)->getPointerTo(); |
| 216 | |
Anders Carlsson | a552ea7 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 217 | llvm::Value *NonVirtualOffset = |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 218 | CGM.GetNonVirtualBaseClassOffset(Derived, BasePath); |
Anders Carlsson | a552ea7 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 219 | |
| 220 | if (!NonVirtualOffset) { |
| 221 | // No offset, we can just cast back. |
| 222 | return Builder.CreateBitCast(Value, DerivedPtrTy); |
| 223 | } |
| 224 | |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 225 | llvm::BasicBlock *CastNull = 0; |
| 226 | llvm::BasicBlock *CastNotNull = 0; |
| 227 | llvm::BasicBlock *CastEnd = 0; |
| 228 | |
| 229 | if (NullCheckValue) { |
| 230 | CastNull = createBasicBlock("cast.null"); |
| 231 | CastNotNull = createBasicBlock("cast.notnull"); |
| 232 | CastEnd = createBasicBlock("cast.end"); |
| 233 | |
| 234 | llvm::Value *IsNull = |
| 235 | Builder.CreateICmpEQ(Value, |
| 236 | llvm::Constant::getNullValue(Value->getType())); |
| 237 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
| 238 | EmitBlock(CastNotNull); |
| 239 | } |
| 240 | |
Anders Carlsson | a552ea7 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 241 | // Apply the offset. |
| 242 | Value = Builder.CreatePtrToInt(Value, NonVirtualOffset->getType()); |
| 243 | Value = Builder.CreateSub(Value, NonVirtualOffset); |
| 244 | Value = Builder.CreateIntToPtr(Value, DerivedPtrTy); |
| 245 | |
| 246 | // Just cast. |
| 247 | Value = Builder.CreateBitCast(Value, DerivedPtrTy); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 248 | |
| 249 | if (NullCheckValue) { |
| 250 | Builder.CreateBr(CastEnd); |
| 251 | EmitBlock(CastNull); |
| 252 | Builder.CreateBr(CastEnd); |
| 253 | EmitBlock(CastEnd); |
| 254 | |
| 255 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType()); |
| 256 | PHI->reserveOperandSpace(2); |
| 257 | PHI->addIncoming(Value, CastNotNull); |
| 258 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), |
| 259 | CastNull); |
| 260 | Value = PHI; |
| 261 | } |
| 262 | |
| 263 | return Value; |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 264 | } |
Anders Carlsson | 21c9ad9 | 2010-03-30 03:27:09 +0000 | [diff] [blame] | 265 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 266 | /// GetVTTParameter - Return the VTT parameter that should be passed to a |
| 267 | /// base constructor/destructor with virtual bases. |
Anders Carlsson | 314e622 | 2010-05-02 23:33:10 +0000 | [diff] [blame] | 268 | static llvm::Value *GetVTTParameter(CodeGenFunction &CGF, GlobalDecl GD, |
| 269 | bool ForVirtualBase) { |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 270 | if (!CodeGenVTables::needsVTTParameter(GD)) { |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 271 | // This constructor/destructor does not need a VTT parameter. |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | const CXXRecordDecl *RD = cast<CXXMethodDecl>(CGF.CurFuncDecl)->getParent(); |
| 276 | const CXXRecordDecl *Base = cast<CXXMethodDecl>(GD.getDecl())->getParent(); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 277 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 278 | llvm::Value *VTT; |
| 279 | |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 280 | uint64_t SubVTTIndex; |
| 281 | |
| 282 | // If the record matches the base, this is the complete ctor/dtor |
| 283 | // variant calling the base variant in a class with virtual bases. |
| 284 | if (RD == Base) { |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 285 | assert(!CodeGenVTables::needsVTTParameter(CGF.CurGD) && |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 286 | "doing no-op VTT offset in base dtor/ctor?"); |
Anders Carlsson | 314e622 | 2010-05-02 23:33:10 +0000 | [diff] [blame] | 287 | assert(!ForVirtualBase && "Can't have same class as virtual base!"); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 288 | SubVTTIndex = 0; |
| 289 | } else { |
Anders Carlsson | c11bb21 | 2010-05-02 23:53:25 +0000 | [diff] [blame] | 290 | const ASTRecordLayout &Layout = |
| 291 | CGF.getContext().getASTRecordLayout(RD); |
| 292 | uint64_t BaseOffset = ForVirtualBase ? |
| 293 | Layout.getVBaseClassOffset(Base) : Layout.getBaseClassOffset(Base); |
| 294 | |
| 295 | SubVTTIndex = |
| 296 | CGF.CGM.getVTables().getSubVTTIndex(RD, BaseSubobject(Base, BaseOffset)); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 297 | assert(SubVTTIndex != 0 && "Sub-VTT index must be greater than zero!"); |
| 298 | } |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 299 | |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 300 | if (CodeGenVTables::needsVTTParameter(CGF.CurGD)) { |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 301 | // A VTT parameter was passed to the constructor, use it. |
| 302 | VTT = CGF.LoadCXXVTT(); |
| 303 | VTT = CGF.Builder.CreateConstInBoundsGEP1_64(VTT, SubVTTIndex); |
| 304 | } else { |
| 305 | // We're the complete constructor, so get the VTT by name. |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 306 | VTT = CGF.CGM.getVTables().getVTT(RD); |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 307 | VTT = CGF.Builder.CreateConstInBoundsGEP2_64(VTT, 0, SubVTTIndex); |
| 308 | } |
| 309 | |
| 310 | return VTT; |
| 311 | } |
| 312 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 313 | static void EmitBaseInitializer(CodeGenFunction &CGF, |
| 314 | const CXXRecordDecl *ClassDecl, |
| 315 | CXXBaseOrMemberInitializer *BaseInit, |
| 316 | CXXCtorType CtorType) { |
| 317 | assert(BaseInit->isBaseInitializer() && |
| 318 | "Must have base initializer!"); |
| 319 | |
| 320 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
| 321 | |
| 322 | const Type *BaseType = BaseInit->getBaseClass(); |
| 323 | CXXRecordDecl *BaseClassDecl = |
| 324 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); |
| 325 | |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 326 | bool isBaseVirtual = BaseInit->isBaseVirtual(); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 327 | |
| 328 | // The base constructor doesn't construct virtual bases. |
| 329 | if (CtorType == Ctor_Base && isBaseVirtual) |
| 330 | return; |
| 331 | |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 332 | // We can pretend to be a complete class because it only matters for |
| 333 | // virtual bases, and we only do virtual bases for complete ctors. |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 334 | llvm::Value *V = |
| 335 | CGF.GetAddressOfDirectBaseInCompleteClass(ThisPtr, ClassDecl, |
| 336 | BaseClassDecl, |
| 337 | BaseInit->isBaseVirtual()); |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 338 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 339 | CGF.EmitAggExpr(BaseInit->getInit(), V, false, false, true); |
Anders Carlsson | 594d5e8 | 2010-02-06 20:00:21 +0000 | [diff] [blame] | 340 | |
| 341 | if (CGF.Exceptions && !BaseClassDecl->hasTrivialDestructor()) { |
| 342 | // FIXME: Is this OK for C++0x delegating constructors? |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 343 | CodeGenFunction::CleanupBlock Cleanup(CGF, EHCleanup); |
Anders Carlsson | 594d5e8 | 2010-02-06 20:00:21 +0000 | [diff] [blame] | 344 | |
Douglas Gregor | 1d110e0 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 345 | CXXDestructorDecl *DD = BaseClassDecl->getDestructor(); |
Anders Carlsson | 8e6404c | 2010-05-02 23:29:11 +0000 | [diff] [blame] | 346 | CGF.EmitCXXDestructorCall(DD, Dtor_Base, isBaseVirtual, V); |
Anders Carlsson | 594d5e8 | 2010-02-06 20:00:21 +0000 | [diff] [blame] | 347 | } |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 350 | static void EmitAggMemberInitializer(CodeGenFunction &CGF, |
| 351 | LValue LHS, |
| 352 | llvm::Value *ArrayIndexVar, |
| 353 | CXXBaseOrMemberInitializer *MemberInit, |
| 354 | QualType T, |
| 355 | unsigned Index) { |
| 356 | if (Index == MemberInit->getNumArrayIndices()) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 357 | CodeGenFunction::RunCleanupsScope Cleanups(CGF); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 358 | |
| 359 | llvm::Value *Dest = LHS.getAddress(); |
| 360 | if (ArrayIndexVar) { |
| 361 | // If we have an array index variable, load it and use it as an offset. |
| 362 | // Then, increment the value. |
| 363 | llvm::Value *ArrayIndex = CGF.Builder.CreateLoad(ArrayIndexVar); |
| 364 | Dest = CGF.Builder.CreateInBoundsGEP(Dest, ArrayIndex, "destaddress"); |
| 365 | llvm::Value *Next = llvm::ConstantInt::get(ArrayIndex->getType(), 1); |
| 366 | Next = CGF.Builder.CreateAdd(ArrayIndex, Next, "inc"); |
| 367 | CGF.Builder.CreateStore(Next, ArrayIndexVar); |
| 368 | } |
| 369 | |
| 370 | CGF.EmitAggExpr(MemberInit->getInit(), Dest, |
| 371 | LHS.isVolatileQualified(), |
| 372 | /*IgnoreResult*/ false, |
| 373 | /*IsInitializer*/ true); |
| 374 | |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | const ConstantArrayType *Array = CGF.getContext().getAsConstantArrayType(T); |
| 379 | assert(Array && "Array initialization without the array type?"); |
| 380 | llvm::Value *IndexVar |
| 381 | = CGF.GetAddrOfLocalVar(MemberInit->getArrayIndex(Index)); |
| 382 | assert(IndexVar && "Array index variable not loaded"); |
| 383 | |
| 384 | // Initialize this index variable to zero. |
| 385 | llvm::Value* Zero |
| 386 | = llvm::Constant::getNullValue( |
| 387 | CGF.ConvertType(CGF.getContext().getSizeType())); |
| 388 | CGF.Builder.CreateStore(Zero, IndexVar); |
| 389 | |
| 390 | // Start the loop with a block that tests the condition. |
| 391 | llvm::BasicBlock *CondBlock = CGF.createBasicBlock("for.cond"); |
| 392 | llvm::BasicBlock *AfterFor = CGF.createBasicBlock("for.end"); |
| 393 | |
| 394 | CGF.EmitBlock(CondBlock); |
| 395 | |
| 396 | llvm::BasicBlock *ForBody = CGF.createBasicBlock("for.body"); |
| 397 | // Generate: if (loop-index < number-of-elements) fall to the loop body, |
| 398 | // otherwise, go to the block after the for-loop. |
| 399 | uint64_t NumElements = Array->getSize().getZExtValue(); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 400 | llvm::Value *Counter = CGF.Builder.CreateLoad(IndexVar); |
Chris Lattner | 985f739 | 2010-05-06 06:35:23 +0000 | [diff] [blame] | 401 | llvm::Value *NumElementsPtr = |
| 402 | llvm::ConstantInt::get(Counter->getType(), NumElements); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 403 | llvm::Value *IsLess = CGF.Builder.CreateICmpULT(Counter, NumElementsPtr, |
| 404 | "isless"); |
| 405 | |
| 406 | // If the condition is true, execute the body. |
| 407 | CGF.Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
| 408 | |
| 409 | CGF.EmitBlock(ForBody); |
| 410 | llvm::BasicBlock *ContinueBlock = CGF.createBasicBlock("for.inc"); |
| 411 | |
| 412 | { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 413 | CodeGenFunction::RunCleanupsScope Cleanups(CGF); |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 414 | |
| 415 | // Inside the loop body recurse to emit the inner loop or, eventually, the |
| 416 | // constructor call. |
| 417 | EmitAggMemberInitializer(CGF, LHS, ArrayIndexVar, MemberInit, |
| 418 | Array->getElementType(), Index + 1); |
| 419 | } |
| 420 | |
| 421 | CGF.EmitBlock(ContinueBlock); |
| 422 | |
| 423 | // Emit the increment of the loop counter. |
| 424 | llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); |
| 425 | Counter = CGF.Builder.CreateLoad(IndexVar); |
| 426 | NextVal = CGF.Builder.CreateAdd(Counter, NextVal, "inc"); |
| 427 | CGF.Builder.CreateStore(NextVal, IndexVar); |
| 428 | |
| 429 | // Finally, branch back up to the condition for the next iteration. |
| 430 | CGF.EmitBranch(CondBlock); |
| 431 | |
| 432 | // Emit the fall-through block. |
| 433 | CGF.EmitBlock(AfterFor, true); |
| 434 | } |
| 435 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 436 | static void EmitMemberInitializer(CodeGenFunction &CGF, |
| 437 | const CXXRecordDecl *ClassDecl, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 438 | CXXBaseOrMemberInitializer *MemberInit, |
| 439 | const CXXConstructorDecl *Constructor, |
| 440 | FunctionArgList &Args) { |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 441 | assert(MemberInit->isMemberInitializer() && |
| 442 | "Must have member initializer!"); |
| 443 | |
| 444 | // non-static data member initializers. |
| 445 | FieldDecl *Field = MemberInit->getMember(); |
| 446 | QualType FieldType = CGF.getContext().getCanonicalType(Field->getType()); |
| 447 | |
| 448 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
John McCall | a9976d3 | 2010-05-21 01:18:57 +0000 | [diff] [blame] | 449 | LValue LHS; |
Anders Carlsson | 06a2970 | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 450 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 451 | // If we are initializing an anonymous union field, drill down to the field. |
| 452 | if (MemberInit->getAnonUnionMember()) { |
| 453 | Field = MemberInit->getAnonUnionMember(); |
John McCall | a9976d3 | 2010-05-21 01:18:57 +0000 | [diff] [blame] | 454 | LHS = CGF.EmitLValueForAnonRecordField(ThisPtr, Field, 0); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 455 | FieldType = Field->getType(); |
John McCall | a9976d3 | 2010-05-21 01:18:57 +0000 | [diff] [blame] | 456 | } else { |
| 457 | LHS = CGF.EmitLValueForFieldInitialization(ThisPtr, Field, 0); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 460 | // FIXME: If there's no initializer and the CXXBaseOrMemberInitializer |
| 461 | // was implicitly generated, we shouldn't be zeroing memory. |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 462 | RValue RHS; |
| 463 | if (FieldType->isReferenceType()) { |
Anders Carlsson | 32f36ba | 2010-06-26 16:35:32 +0000 | [diff] [blame] | 464 | RHS = CGF.EmitReferenceBindingToExpr(MemberInit->getInit(), Field); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 465 | CGF.EmitStoreThroughLValue(RHS, LHS, FieldType); |
Eli Friedman | 3bb9412 | 2010-01-31 19:07:50 +0000 | [diff] [blame] | 466 | } else if (FieldType->isArrayType() && !MemberInit->getInit()) { |
Anders Carlsson | 1884eb0 | 2010-05-22 17:35:42 +0000 | [diff] [blame] | 467 | CGF.EmitNullInitialization(LHS.getAddress(), Field->getType()); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 468 | } else if (!CGF.hasAggregateLLVMType(Field->getType())) { |
Eli Friedman | 0b29227 | 2010-06-03 19:58:07 +0000 | [diff] [blame] | 469 | RHS = RValue::get(CGF.EmitScalarExpr(MemberInit->getInit())); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 470 | CGF.EmitStoreThroughLValue(RHS, LHS, FieldType); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 471 | } else if (MemberInit->getInit()->getType()->isAnyComplexType()) { |
| 472 | CGF.EmitComplexExprIntoAddr(MemberInit->getInit(), LHS.getAddress(), |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 473 | LHS.isVolatileQualified()); |
| 474 | } else { |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 475 | llvm::Value *ArrayIndexVar = 0; |
| 476 | const ConstantArrayType *Array |
| 477 | = CGF.getContext().getAsConstantArrayType(FieldType); |
| 478 | if (Array && Constructor->isImplicit() && |
| 479 | Constructor->isCopyConstructor()) { |
| 480 | const llvm::Type *SizeTy |
| 481 | = CGF.ConvertType(CGF.getContext().getSizeType()); |
| 482 | |
| 483 | // The LHS is a pointer to the first object we'll be constructing, as |
| 484 | // a flat array. |
| 485 | QualType BaseElementTy = CGF.getContext().getBaseElementType(Array); |
| 486 | const llvm::Type *BasePtr = CGF.ConvertType(BaseElementTy); |
| 487 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 488 | llvm::Value *BaseAddrPtr = CGF.Builder.CreateBitCast(LHS.getAddress(), |
| 489 | BasePtr); |
| 490 | LHS = LValue::MakeAddr(BaseAddrPtr, CGF.MakeQualifiers(BaseElementTy)); |
| 491 | |
| 492 | // Create an array index that will be used to walk over all of the |
| 493 | // objects we're constructing. |
| 494 | ArrayIndexVar = CGF.CreateTempAlloca(SizeTy, "object.index"); |
| 495 | llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy); |
| 496 | CGF.Builder.CreateStore(Zero, ArrayIndexVar); |
| 497 | |
| 498 | // If we are copying an array of scalars or classes with trivial copy |
| 499 | // constructors, perform a single aggregate copy. |
| 500 | const RecordType *Record = BaseElementTy->getAs<RecordType>(); |
| 501 | if (!Record || |
| 502 | cast<CXXRecordDecl>(Record->getDecl())->hasTrivialCopyConstructor()) { |
| 503 | // Find the source pointer. We knows it's the last argument because |
| 504 | // we know we're in a copy constructor. |
| 505 | unsigned SrcArgIndex = Args.size() - 1; |
| 506 | llvm::Value *SrcPtr |
| 507 | = CGF.Builder.CreateLoad( |
| 508 | CGF.GetAddrOfLocalVar(Args[SrcArgIndex].first)); |
| 509 | LValue Src = CGF.EmitLValueForFieldInitialization(SrcPtr, Field, 0); |
| 510 | |
| 511 | // Copy the aggregate. |
| 512 | CGF.EmitAggregateCopy(LHS.getAddress(), Src.getAddress(), FieldType, |
| 513 | LHS.isVolatileQualified()); |
| 514 | return; |
| 515 | } |
| 516 | |
| 517 | // Emit the block variables for the array indices, if any. |
| 518 | for (unsigned I = 0, N = MemberInit->getNumArrayIndices(); I != N; ++I) |
| 519 | CGF.EmitLocalBlockVarDecl(*MemberInit->getArrayIndex(I)); |
| 520 | } |
| 521 | |
| 522 | EmitAggMemberInitializer(CGF, LHS, ArrayIndexVar, MemberInit, FieldType, 0); |
Anders Carlsson | 9405dcd | 2010-02-06 19:50:17 +0000 | [diff] [blame] | 523 | |
| 524 | if (!CGF.Exceptions) |
| 525 | return; |
| 526 | |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 527 | // FIXME: If we have an array of classes w/ non-trivial destructors, |
| 528 | // we need to destroy in reverse order of construction along the exception |
| 529 | // path. |
Anders Carlsson | 9405dcd | 2010-02-06 19:50:17 +0000 | [diff] [blame] | 530 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 531 | if (!RT) |
| 532 | return; |
| 533 | |
| 534 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 535 | if (!RD->hasTrivialDestructor()) { |
| 536 | // FIXME: Is this OK for C++0x delegating constructors? |
John McCall | da65ea8 | 2010-07-13 20:32:21 +0000 | [diff] [blame] | 537 | CodeGenFunction::CleanupBlock Cleanup(CGF, EHCleanup); |
Anders Carlsson | 9405dcd | 2010-02-06 19:50:17 +0000 | [diff] [blame] | 538 | |
| 539 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
| 540 | LValue LHS = CGF.EmitLValueForField(ThisPtr, Field, 0); |
| 541 | |
Douglas Gregor | 1d110e0 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 542 | CXXDestructorDecl *DD = RD->getDestructor(); |
Anders Carlsson | 8e6404c | 2010-05-02 23:29:11 +0000 | [diff] [blame] | 543 | CGF.EmitCXXDestructorCall(DD, Dtor_Complete, /*ForVirtualBase=*/false, |
| 544 | LHS.getAddress()); |
Anders Carlsson | 9405dcd | 2010-02-06 19:50:17 +0000 | [diff] [blame] | 545 | } |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 549 | /// Checks whether the given constructor is a valid subject for the |
| 550 | /// complete-to-base constructor delegation optimization, i.e. |
| 551 | /// emitting the complete constructor as a simple call to the base |
| 552 | /// constructor. |
| 553 | static bool IsConstructorDelegationValid(const CXXConstructorDecl *Ctor) { |
| 554 | |
| 555 | // Currently we disable the optimization for classes with virtual |
| 556 | // bases because (1) the addresses of parameter variables need to be |
| 557 | // consistent across all initializers but (2) the delegate function |
| 558 | // call necessarily creates a second copy of the parameter variable. |
| 559 | // |
| 560 | // The limiting example (purely theoretical AFAIK): |
| 561 | // struct A { A(int &c) { c++; } }; |
| 562 | // struct B : virtual A { |
| 563 | // B(int count) : A(count) { printf("%d\n", count); } |
| 564 | // }; |
| 565 | // ...although even this example could in principle be emitted as a |
| 566 | // delegation since the address of the parameter doesn't escape. |
| 567 | if (Ctor->getParent()->getNumVBases()) { |
| 568 | // TODO: white-list trivial vbase initializers. This case wouldn't |
| 569 | // be subject to the restrictions below. |
| 570 | |
| 571 | // TODO: white-list cases where: |
| 572 | // - there are no non-reference parameters to the constructor |
| 573 | // - the initializers don't access any non-reference parameters |
| 574 | // - the initializers don't take the address of non-reference |
| 575 | // parameters |
| 576 | // - etc. |
| 577 | // If we ever add any of the above cases, remember that: |
| 578 | // - function-try-blocks will always blacklist this optimization |
| 579 | // - we need to perform the constructor prologue and cleanup in |
| 580 | // EmitConstructorBody. |
| 581 | |
| 582 | return false; |
| 583 | } |
| 584 | |
| 585 | // We also disable the optimization for variadic functions because |
| 586 | // it's impossible to "re-pass" varargs. |
| 587 | if (Ctor->getType()->getAs<FunctionProtoType>()->isVariadic()) |
| 588 | return false; |
| 589 | |
| 590 | return true; |
| 591 | } |
| 592 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 593 | /// EmitConstructorBody - Emits the body of the current constructor. |
| 594 | void CodeGenFunction::EmitConstructorBody(FunctionArgList &Args) { |
| 595 | const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(CurGD.getDecl()); |
| 596 | CXXCtorType CtorType = CurGD.getCtorType(); |
| 597 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 598 | // Before we go any further, try the complete->base constructor |
| 599 | // delegation optimization. |
| 600 | if (CtorType == Ctor_Complete && IsConstructorDelegationValid(Ctor)) { |
| 601 | EmitDelegateCXXConstructorCall(Ctor, Ctor_Base, Args); |
| 602 | return; |
| 603 | } |
| 604 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 605 | Stmt *Body = Ctor->getBody(); |
| 606 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 607 | // Enter the function-try-block before the constructor prologue if |
| 608 | // applicable. |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 609 | bool IsTryBody = (Body && isa<CXXTryStmt>(Body)); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 610 | if (IsTryBody) |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 611 | EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 612 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 613 | EHScopeStack::stable_iterator CleanupDepth = EHStack.stable_begin(); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 614 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 615 | // Emit the constructor prologue, i.e. the base and member |
| 616 | // initializers. |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 617 | EmitCtorPrologue(Ctor, CtorType, Args); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 618 | |
| 619 | // Emit the body of the statement. |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 620 | if (IsTryBody) |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 621 | EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock()); |
| 622 | else if (Body) |
| 623 | EmitStmt(Body); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 624 | |
| 625 | // Emit any cleanup blocks associated with the member or base |
| 626 | // initializers, which includes (along the exceptional path) the |
| 627 | // destructors for those members and bases that were fully |
| 628 | // constructed. |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 629 | PopCleanupBlocks(CleanupDepth); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 630 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 631 | if (IsTryBody) |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 632 | ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 635 | /// EmitCtorPrologue - This routine generates necessary code to initialize |
| 636 | /// base classes and non-static data members belonging to this constructor. |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 637 | void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD, |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 638 | CXXCtorType CtorType, |
| 639 | FunctionArgList &Args) { |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 640 | const CXXRecordDecl *ClassDecl = CD->getParent(); |
Anders Carlsson | a78fa2c | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 641 | |
| 642 | llvm::SmallVector<CXXBaseOrMemberInitializer *, 8> MemberInitializers; |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 643 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 644 | for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(), |
| 645 | E = CD->init_end(); |
| 646 | B != E; ++B) { |
| 647 | CXXBaseOrMemberInitializer *Member = (*B); |
| 648 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 649 | if (Member->isBaseInitializer()) |
| 650 | EmitBaseInitializer(*this, ClassDecl, Member, CtorType); |
| 651 | else |
Anders Carlsson | a78fa2c | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 652 | MemberInitializers.push_back(Member); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 655 | InitializeVTablePointers(ClassDecl); |
Anders Carlsson | a78fa2c | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 656 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 657 | for (unsigned I = 0, E = MemberInitializers.size(); I != E; ++I) |
Douglas Gregor | fb8cc25 | 2010-05-05 05:51:00 +0000 | [diff] [blame] | 658 | EmitMemberInitializer(*this, ClassDecl, MemberInitializers[I], CD, Args); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 659 | } |
| 660 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 661 | /// EmitDestructorBody - Emits the body of the current destructor. |
| 662 | void CodeGenFunction::EmitDestructorBody(FunctionArgList &Args) { |
| 663 | const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CurGD.getDecl()); |
| 664 | CXXDtorType DtorType = CurGD.getDtorType(); |
| 665 | |
| 666 | Stmt *Body = Dtor->getBody(); |
| 667 | |
| 668 | // If the body is a function-try-block, enter the try before |
| 669 | // anything else --- unless we're in a deleting destructor, in which |
| 670 | // case we're just going to call the complete destructor and then |
| 671 | // call operator delete() on the way out. |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 672 | bool isTryBody = (DtorType != Dtor_Deleting && |
| 673 | Body && isa<CXXTryStmt>(Body)); |
| 674 | if (isTryBody) |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 675 | EnterCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 676 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 677 | // Emit the destructor epilogue now. If this is a complete |
| 678 | // destructor with a function-try-block, perform the base epilogue |
| 679 | // as well. |
| 680 | // |
| 681 | // FIXME: This isn't really right, because an exception in the |
| 682 | // non-EH epilogue should jump to the appropriate place in the |
| 683 | // EH epilogue. |
| 684 | { |
| 685 | CleanupBlock Cleanup(*this, NormalCleanup); |
| 686 | |
| 687 | if (isTryBody && DtorType == Dtor_Complete) |
| 688 | EmitDtorEpilogue(Dtor, Dtor_Base); |
| 689 | EmitDtorEpilogue(Dtor, DtorType); |
| 690 | |
| 691 | if (Exceptions) { |
| 692 | Cleanup.beginEHCleanup(); |
| 693 | |
| 694 | if (isTryBody && DtorType == Dtor_Complete) |
| 695 | EmitDtorEpilogue(Dtor, Dtor_Base); |
| 696 | EmitDtorEpilogue(Dtor, DtorType); |
| 697 | } |
| 698 | } |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 699 | |
| 700 | bool SkipBody = false; // should get jump-threaded |
| 701 | |
| 702 | // If this is the deleting variant, just invoke the complete |
| 703 | // variant, then call the appropriate operator delete() on the way |
| 704 | // out. |
| 705 | if (DtorType == Dtor_Deleting) { |
Anders Carlsson | 8e6404c | 2010-05-02 23:29:11 +0000 | [diff] [blame] | 706 | EmitCXXDestructorCall(Dtor, Dtor_Complete, /*ForVirtualBase=*/false, |
| 707 | LoadCXXThis()); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 708 | SkipBody = true; |
| 709 | |
| 710 | // If this is the complete variant, just invoke the base variant; |
| 711 | // the epilogue will destruct the virtual bases. But we can't do |
| 712 | // this optimization if the body is a function-try-block, because |
| 713 | // we'd introduce *two* handler blocks. |
| 714 | } else if (!isTryBody && DtorType == Dtor_Complete) { |
Anders Carlsson | 8e6404c | 2010-05-02 23:29:11 +0000 | [diff] [blame] | 715 | EmitCXXDestructorCall(Dtor, Dtor_Base, /*ForVirtualBase=*/false, |
| 716 | LoadCXXThis()); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 717 | SkipBody = true; |
| 718 | |
| 719 | // Otherwise, we're in the base variant, so we need to ensure the |
| 720 | // vtable ptrs are right before emitting the body. |
| 721 | } else { |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 722 | InitializeVTablePointers(Dtor->getParent()); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | // Emit the body of the statement. |
| 726 | if (SkipBody) |
| 727 | (void) 0; |
| 728 | else if (isTryBody) |
| 729 | EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock()); |
| 730 | else if (Body) |
| 731 | EmitStmt(Body); |
| 732 | else { |
| 733 | assert(Dtor->isImplicit() && "bodyless dtor not implicit"); |
| 734 | // nothing to do besides what's in the epilogue |
| 735 | } |
| 736 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 737 | // We're done with the epilogue cleanup. |
| 738 | PopCleanupBlock(); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 739 | |
| 740 | // Exit the try if applicable. |
| 741 | if (isTryBody) |
John McCall | 59a7000 | 2010-07-07 06:56:46 +0000 | [diff] [blame] | 742 | ExitCXXTryStmt(*cast<CXXTryStmt>(Body), true); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 745 | /// EmitDtorEpilogue - Emit all code that comes at the end of class's |
| 746 | /// destructor. This is to call destructors on members and base classes |
| 747 | /// in reverse order of their construction. |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 748 | void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD, |
| 749 | CXXDtorType DtorType) { |
| 750 | assert(!DD->isTrivial() && |
| 751 | "Should not emit dtor epilogue for trivial dtor!"); |
| 752 | |
| 753 | const CXXRecordDecl *ClassDecl = DD->getParent(); |
| 754 | |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 755 | // In a deleting destructor, we've already called the complete |
| 756 | // destructor as a subroutine, so we just have to delete the |
| 757 | // appropriate value. |
| 758 | if (DtorType == Dtor_Deleting) { |
| 759 | assert(DD->getOperatorDelete() && |
| 760 | "operator delete missing - EmitDtorEpilogue"); |
| 761 | EmitDeleteCall(DD->getOperatorDelete(), LoadCXXThis(), |
| 762 | getContext().getTagDeclType(ClassDecl)); |
| 763 | return; |
| 764 | } |
| 765 | |
| 766 | // For complete destructors, we've already called the base |
| 767 | // destructor (in GenerateBody), so we just need to destruct all the |
| 768 | // virtual bases. |
| 769 | if (DtorType == Dtor_Complete) { |
| 770 | // Handle virtual bases. |
| 771 | for (CXXRecordDecl::reverse_base_class_const_iterator I = |
| 772 | ClassDecl->vbases_rbegin(), E = ClassDecl->vbases_rend(); |
| 773 | I != E; ++I) { |
| 774 | const CXXBaseSpecifier &Base = *I; |
| 775 | CXXRecordDecl *BaseClassDecl |
| 776 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| 777 | |
| 778 | // Ignore trivial destructors. |
| 779 | if (BaseClassDecl->hasTrivialDestructor()) |
| 780 | continue; |
Douglas Gregor | 1d110e0 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 781 | const CXXDestructorDecl *D = BaseClassDecl->getDestructor(); |
Anders Carlsson | 8561a86 | 2010-04-24 23:01:49 +0000 | [diff] [blame] | 782 | llvm::Value *V = |
| 783 | GetAddressOfDirectBaseInCompleteClass(LoadCXXThis(), |
| 784 | ClassDecl, BaseClassDecl, |
| 785 | /*BaseIsVirtual=*/true); |
Anders Carlsson | 8e6404c | 2010-05-02 23:29:11 +0000 | [diff] [blame] | 786 | EmitCXXDestructorCall(D, Dtor_Base, /*ForVirtualBase=*/true, V); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 787 | } |
| 788 | return; |
| 789 | } |
| 790 | |
| 791 | assert(DtorType == Dtor_Base); |
| 792 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 793 | // Collect the fields. |
| 794 | llvm::SmallVector<const FieldDecl *, 16> FieldDecls; |
| 795 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 796 | E = ClassDecl->field_end(); I != E; ++I) { |
| 797 | const FieldDecl *Field = *I; |
| 798 | |
| 799 | QualType FieldType = getContext().getCanonicalType(Field->getType()); |
| 800 | FieldType = getContext().getBaseElementType(FieldType); |
| 801 | |
| 802 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 803 | if (!RT) |
| 804 | continue; |
| 805 | |
| 806 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 807 | if (FieldClassDecl->hasTrivialDestructor()) |
| 808 | continue; |
| 809 | |
| 810 | FieldDecls.push_back(Field); |
| 811 | } |
| 812 | |
| 813 | // Now destroy the fields. |
| 814 | for (size_t i = FieldDecls.size(); i > 0; --i) { |
| 815 | const FieldDecl *Field = FieldDecls[i - 1]; |
| 816 | |
| 817 | QualType FieldType = Field->getType(); |
| 818 | const ConstantArrayType *Array = |
| 819 | getContext().getAsConstantArrayType(FieldType); |
| 820 | if (Array) |
| 821 | FieldType = getContext().getBaseElementType(FieldType); |
| 822 | |
| 823 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 824 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 825 | |
| 826 | llvm::Value *ThisPtr = LoadCXXThis(); |
| 827 | |
| 828 | LValue LHS = EmitLValueForField(ThisPtr, Field, |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 829 | // FIXME: Qualifiers? |
| 830 | /*CVRQualifiers=*/0); |
| 831 | if (Array) { |
| 832 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 833 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 834 | llvm::Value *BaseAddrPtr = |
| 835 | Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
Douglas Gregor | 1d110e0 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 836 | EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(), |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 837 | Array, BaseAddrPtr); |
| 838 | } else |
Douglas Gregor | 1d110e0 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 839 | EmitCXXDestructorCall(FieldClassDecl->getDestructor(), |
Anders Carlsson | 8e6404c | 2010-05-02 23:29:11 +0000 | [diff] [blame] | 840 | Dtor_Complete, /*ForVirtualBase=*/false, |
| 841 | LHS.getAddress()); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | // Destroy non-virtual bases. |
| 845 | for (CXXRecordDecl::reverse_base_class_const_iterator I = |
| 846 | ClassDecl->bases_rbegin(), E = ClassDecl->bases_rend(); I != E; ++I) { |
| 847 | const CXXBaseSpecifier &Base = *I; |
| 848 | |
| 849 | // Ignore virtual bases. |
| 850 | if (Base.isVirtual()) |
| 851 | continue; |
| 852 | |
| 853 | CXXRecordDecl *BaseClassDecl |
| 854 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| 855 | |
| 856 | // Ignore trivial destructors. |
| 857 | if (BaseClassDecl->hasTrivialDestructor()) |
| 858 | continue; |
Anders Carlsson | 77fae58 | 2010-05-02 23:57:15 +0000 | [diff] [blame] | 859 | |
Douglas Gregor | 1d110e0 | 2010-07-01 14:13:13 +0000 | [diff] [blame] | 860 | const CXXDestructorDecl *D = BaseClassDecl->getDestructor(); |
Anders Carlsson | 77fae58 | 2010-05-02 23:57:15 +0000 | [diff] [blame] | 861 | llvm::Value *V = |
| 862 | GetAddressOfDirectBaseInCompleteClass(LoadCXXThis(), ClassDecl, |
| 863 | BaseClassDecl, |
| 864 | /*BaseIsVirtual=*/false); |
| 865 | |
Anders Carlsson | 8e6404c | 2010-05-02 23:29:11 +0000 | [diff] [blame] | 866 | EmitCXXDestructorCall(D, Dtor_Base, /*ForVirtualBase=*/false, V); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 867 | } |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 870 | /// EmitCXXAggrConstructorCall - This routine essentially creates a (nested) |
| 871 | /// for-loop to call the default constructor on individual members of the |
| 872 | /// array. |
| 873 | /// 'D' is the default constructor for elements of the array, 'ArrayTy' is the |
| 874 | /// array type and 'ArrayPtr' points to the beginning fo the array. |
| 875 | /// 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^] | 876 | /// |
| 877 | /// \param ZeroInitialization True if each element should be zero-initialized |
| 878 | /// before it is constructed. |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 879 | void |
| 880 | CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame^] | 881 | const ConstantArrayType *ArrayTy, |
| 882 | llvm::Value *ArrayPtr, |
| 883 | CallExpr::const_arg_iterator ArgBeg, |
| 884 | CallExpr::const_arg_iterator ArgEnd, |
| 885 | bool ZeroInitialization) { |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 886 | |
| 887 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 888 | llvm::Value * NumElements = |
| 889 | llvm::ConstantInt::get(SizeTy, |
| 890 | getContext().getConstantArrayElementCount(ArrayTy)); |
| 891 | |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame^] | 892 | EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr, ArgBeg, ArgEnd, |
| 893 | ZeroInitialization); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | void |
| 897 | CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
| 898 | llvm::Value *NumElements, |
| 899 | llvm::Value *ArrayPtr, |
| 900 | CallExpr::const_arg_iterator ArgBeg, |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame^] | 901 | CallExpr::const_arg_iterator ArgEnd, |
| 902 | bool ZeroInitialization) { |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 903 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 904 | |
| 905 | // Create a temporary for the loop index and initialize it with 0. |
| 906 | llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index"); |
| 907 | llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy); |
| 908 | Builder.CreateStore(Zero, IndexPtr); |
| 909 | |
| 910 | // Start the loop with a block that tests the condition. |
| 911 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 912 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
| 913 | |
| 914 | EmitBlock(CondBlock); |
| 915 | |
| 916 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 917 | |
| 918 | // Generate: if (loop-index < number-of-elements fall to the loop body, |
| 919 | // otherwise, go to the block after the for-loop. |
| 920 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 921 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless"); |
| 922 | // If the condition is true, execute the body. |
| 923 | Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
| 924 | |
| 925 | EmitBlock(ForBody); |
| 926 | |
| 927 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 928 | // Inside the loop body, emit the constructor call on the array element. |
| 929 | Counter = Builder.CreateLoad(IndexPtr); |
| 930 | llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter, |
| 931 | "arrayidx"); |
| 932 | |
Douglas Gregor | 59174c0 | 2010-07-21 01:10:17 +0000 | [diff] [blame^] | 933 | // Zero initialize the storage, if requested. |
| 934 | if (ZeroInitialization) |
| 935 | EmitNullInitialization(Address, |
| 936 | getContext().getTypeDeclType(D->getParent())); |
| 937 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 938 | // C++ [class.temporary]p4: |
| 939 | // There are two contexts in which temporaries are destroyed at a different |
| 940 | // point than the end of the full-expression. The first context is when a |
| 941 | // default constructor is called to initialize an element of an array. |
| 942 | // If the constructor has one or more default arguments, the destruction of |
| 943 | // every temporary created in a default argument expression is sequenced |
| 944 | // before the construction of the next array element, if any. |
| 945 | |
| 946 | // Keep track of the current number of live temporaries. |
Anders Carlsson | 44ec82b | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 947 | { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 948 | RunCleanupsScope Scope(*this); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 949 | |
Anders Carlsson | 155ed4a | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 950 | EmitCXXConstructorCall(D, Ctor_Complete, /*ForVirtualBase=*/false, Address, |
Anders Carlsson | 24eb78e | 2010-05-02 23:01:10 +0000 | [diff] [blame] | 951 | ArgBeg, ArgEnd); |
Anders Carlsson | 44ec82b | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 952 | } |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 953 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 954 | EmitBlock(ContinueBlock); |
| 955 | |
| 956 | // Emit the increment of the loop counter. |
| 957 | llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1); |
| 958 | Counter = Builder.CreateLoad(IndexPtr); |
| 959 | NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); |
| 960 | Builder.CreateStore(NextVal, IndexPtr); |
| 961 | |
| 962 | // Finally, branch back up to the condition for the next iteration. |
| 963 | EmitBranch(CondBlock); |
| 964 | |
| 965 | // Emit the fall-through block. |
| 966 | EmitBlock(AfterFor, true); |
| 967 | } |
| 968 | |
| 969 | /// EmitCXXAggrDestructorCall - calls the default destructor on array |
| 970 | /// elements in reverse order of construction. |
| 971 | void |
| 972 | CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, |
| 973 | const ArrayType *Array, |
| 974 | llvm::Value *This) { |
| 975 | const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); |
| 976 | assert(CA && "Do we support VLA for destruction ?"); |
| 977 | uint64_t ElementCount = getContext().getConstantArrayElementCount(CA); |
| 978 | |
| 979 | const llvm::Type *SizeLTy = ConvertType(getContext().getSizeType()); |
| 980 | llvm::Value* ElementCountPtr = llvm::ConstantInt::get(SizeLTy, ElementCount); |
| 981 | EmitCXXAggrDestructorCall(D, ElementCountPtr, This); |
| 982 | } |
| 983 | |
| 984 | /// EmitCXXAggrDestructorCall - calls the default destructor on array |
| 985 | /// elements in reverse order of construction. |
| 986 | void |
| 987 | CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, |
| 988 | llvm::Value *UpperCount, |
| 989 | llvm::Value *This) { |
| 990 | const llvm::Type *SizeLTy = ConvertType(getContext().getSizeType()); |
| 991 | llvm::Value *One = llvm::ConstantInt::get(SizeLTy, 1); |
| 992 | |
| 993 | // Create a temporary for the loop index and initialize it with count of |
| 994 | // array elements. |
| 995 | llvm::Value *IndexPtr = CreateTempAlloca(SizeLTy, "loop.index"); |
| 996 | |
| 997 | // Store the number of elements in the index pointer. |
| 998 | Builder.CreateStore(UpperCount, IndexPtr); |
| 999 | |
| 1000 | // Start the loop with a block that tests the condition. |
| 1001 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 1002 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
| 1003 | |
| 1004 | EmitBlock(CondBlock); |
| 1005 | |
| 1006 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 1007 | |
| 1008 | // Generate: if (loop-index != 0 fall to the loop body, |
| 1009 | // otherwise, go to the block after the for-loop. |
| 1010 | llvm::Value* zeroConstant = |
| 1011 | llvm::Constant::getNullValue(SizeLTy); |
| 1012 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 1013 | llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant, |
| 1014 | "isne"); |
| 1015 | // If the condition is true, execute the body. |
| 1016 | Builder.CreateCondBr(IsNE, ForBody, AfterFor); |
| 1017 | |
| 1018 | EmitBlock(ForBody); |
| 1019 | |
| 1020 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 1021 | // Inside the loop body, emit the constructor call on the array element. |
| 1022 | Counter = Builder.CreateLoad(IndexPtr); |
| 1023 | Counter = Builder.CreateSub(Counter, One); |
| 1024 | llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx"); |
Anders Carlsson | 8e6404c | 2010-05-02 23:29:11 +0000 | [diff] [blame] | 1025 | EmitCXXDestructorCall(D, Dtor_Complete, /*ForVirtualBase=*/false, Address); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1026 | |
| 1027 | EmitBlock(ContinueBlock); |
| 1028 | |
| 1029 | // Emit the decrement of the loop counter. |
| 1030 | Counter = Builder.CreateLoad(IndexPtr); |
| 1031 | Counter = Builder.CreateSub(Counter, One, "dec"); |
| 1032 | Builder.CreateStore(Counter, IndexPtr); |
| 1033 | |
| 1034 | // Finally, branch back up to the condition for the next iteration. |
| 1035 | EmitBranch(CondBlock); |
| 1036 | |
| 1037 | // Emit the fall-through block. |
| 1038 | EmitBlock(AfterFor, true); |
| 1039 | } |
| 1040 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1041 | void |
| 1042 | CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, |
Anders Carlsson | 155ed4a | 2010-05-02 23:20:53 +0000 | [diff] [blame] | 1043 | CXXCtorType Type, bool ForVirtualBase, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1044 | llvm::Value *This, |
| 1045 | CallExpr::const_arg_iterator ArgBeg, |
| 1046 | CallExpr::const_arg_iterator ArgEnd) { |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1047 | if (D->isTrivial()) { |
| 1048 | if (ArgBeg == ArgEnd) { |
| 1049 | // Trivial default constructor, no codegen required. |
| 1050 | assert(D->isDefaultConstructor() && |
| 1051 | "trivial 0-arg ctor not a default ctor"); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1052 | return; |
| 1053 | } |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1054 | |
| 1055 | assert(ArgBeg + 1 == ArgEnd && "unexpected argcount for trivial ctor"); |
| 1056 | assert(D->isCopyConstructor() && "trivial 1-arg ctor not a copy ctor"); |
| 1057 | |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1058 | const Expr *E = (*ArgBeg); |
| 1059 | QualType Ty = E->getType(); |
| 1060 | llvm::Value *Src = EmitLValue(E).getAddress(); |
| 1061 | EmitAggregateCopy(This, Src, Ty); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1062 | return; |
| 1063 | } |
| 1064 | |
Anders Carlsson | 314e622 | 2010-05-02 23:33:10 +0000 | [diff] [blame] | 1065 | llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(D, Type), ForVirtualBase); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1066 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type); |
| 1067 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1068 | EmitCXXMemberCall(D, Callee, ReturnValueSlot(), This, VTT, ArgBeg, ArgEnd); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1071 | void |
| 1072 | CodeGenFunction::EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor, |
| 1073 | CXXCtorType CtorType, |
| 1074 | const FunctionArgList &Args) { |
| 1075 | CallArgList DelegateArgs; |
| 1076 | |
| 1077 | FunctionArgList::const_iterator I = Args.begin(), E = Args.end(); |
| 1078 | assert(I != E && "no parameters to constructor"); |
| 1079 | |
| 1080 | // this |
| 1081 | DelegateArgs.push_back(std::make_pair(RValue::get(LoadCXXThis()), |
| 1082 | I->second)); |
| 1083 | ++I; |
| 1084 | |
| 1085 | // vtt |
Anders Carlsson | 314e622 | 2010-05-02 23:33:10 +0000 | [diff] [blame] | 1086 | if (llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(Ctor, CtorType), |
| 1087 | /*ForVirtualBase=*/false)) { |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1088 | QualType VoidPP = getContext().getPointerType(getContext().VoidPtrTy); |
| 1089 | DelegateArgs.push_back(std::make_pair(RValue::get(VTT), VoidPP)); |
| 1090 | |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 1091 | if (CodeGenVTables::needsVTTParameter(CurGD)) { |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1092 | assert(I != E && "cannot skip vtt parameter, already done with args"); |
| 1093 | assert(I->second == VoidPP && "skipping parameter not of vtt type"); |
| 1094 | ++I; |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | // Explicit arguments. |
| 1099 | for (; I != E; ++I) { |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1100 | const VarDecl *Param = I->first; |
| 1101 | QualType ArgType = Param->getType(); // because we're passing it to itself |
John McCall | 2736071 | 2010-05-26 22:34:26 +0000 | [diff] [blame] | 1102 | RValue Arg = EmitDelegateCallArg(Param); |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1103 | |
| 1104 | DelegateArgs.push_back(std::make_pair(Arg, ArgType)); |
| 1105 | } |
| 1106 | |
| 1107 | EmitCall(CGM.getTypes().getFunctionInfo(Ctor, CtorType), |
| 1108 | CGM.GetAddrOfCXXConstructor(Ctor, CtorType), |
| 1109 | ReturnValueSlot(), DelegateArgs, Ctor); |
| 1110 | } |
| 1111 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1112 | void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *DD, |
| 1113 | CXXDtorType Type, |
Anders Carlsson | 8e6404c | 2010-05-02 23:29:11 +0000 | [diff] [blame] | 1114 | bool ForVirtualBase, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1115 | llvm::Value *This) { |
Anders Carlsson | 314e622 | 2010-05-02 23:33:10 +0000 | [diff] [blame] | 1116 | llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(DD, Type), |
| 1117 | ForVirtualBase); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1118 | llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(DD, Type); |
| 1119 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1120 | EmitCXXMemberCall(DD, Callee, ReturnValueSlot(), This, VTT, 0, 0); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1123 | void CodeGenFunction::PushDestructorCleanup(QualType T, llvm::Value *Addr) { |
| 1124 | CXXRecordDecl *ClassDecl = T->getAsCXXRecordDecl(); |
| 1125 | if (!ClassDecl) return; |
| 1126 | if (ClassDecl->hasTrivialDestructor()) return; |
| 1127 | |
| 1128 | const CXXDestructorDecl *D = ClassDecl->getDestructor(); |
| 1129 | |
| 1130 | CleanupBlock Scope(*this, NormalCleanup); |
| 1131 | |
| 1132 | EmitCXXDestructorCall(D, Dtor_Complete, /*ForVirtualBase=*/false, Addr); |
| 1133 | |
| 1134 | if (Exceptions) { |
| 1135 | Scope.beginEHCleanup(); |
| 1136 | EmitCXXDestructorCall(D, Dtor_Complete, /*ForVirtualBase=*/false, Addr); |
| 1137 | } |
| 1138 | } |
| 1139 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1140 | llvm::Value * |
Anders Carlsson | bb7e17b | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 1141 | CodeGenFunction::GetVirtualBaseClassOffset(llvm::Value *This, |
| 1142 | const CXXRecordDecl *ClassDecl, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1143 | const CXXRecordDecl *BaseClassDecl) { |
| 1144 | const llvm::Type *Int8PtrTy = |
| 1145 | llvm::Type::getInt8Ty(VMContext)->getPointerTo(); |
| 1146 | |
| 1147 | llvm::Value *VTablePtr = Builder.CreateBitCast(This, |
| 1148 | Int8PtrTy->getPointerTo()); |
| 1149 | VTablePtr = Builder.CreateLoad(VTablePtr, "vtable"); |
| 1150 | |
Anders Carlsson | bba1607 | 2010-03-11 07:15:17 +0000 | [diff] [blame] | 1151 | int64_t VBaseOffsetOffset = |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 1152 | CGM.getVTables().getVirtualBaseOffsetOffset(ClassDecl, BaseClassDecl); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1153 | |
| 1154 | llvm::Value *VBaseOffsetPtr = |
Anders Carlsson | bba1607 | 2010-03-11 07:15:17 +0000 | [diff] [blame] | 1155 | Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetOffset, "vbase.offset.ptr"); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1156 | const llvm::Type *PtrDiffTy = |
| 1157 | ConvertType(getContext().getPointerDiffType()); |
| 1158 | |
| 1159 | VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr, |
| 1160 | PtrDiffTy->getPointerTo()); |
| 1161 | |
| 1162 | llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset"); |
| 1163 | |
| 1164 | return VBaseOffset; |
| 1165 | } |
| 1166 | |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1167 | void |
| 1168 | CodeGenFunction::InitializeVTablePointer(BaseSubobject Base, |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1169 | const CXXRecordDecl *NearestVBase, |
Anders Carlsson | 4235840 | 2010-05-03 00:07:07 +0000 | [diff] [blame] | 1170 | uint64_t OffsetFromNearestVBase, |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1171 | llvm::Constant *VTable, |
| 1172 | const CXXRecordDecl *VTableClass) { |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1173 | const CXXRecordDecl *RD = Base.getBase(); |
| 1174 | |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1175 | // Compute the address point. |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1176 | llvm::Value *VTableAddressPoint; |
Anders Carlsson | 851853d | 2010-03-29 02:38:51 +0000 | [diff] [blame] | 1177 | |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1178 | // Check if we need to use a vtable from the VTT. |
Anders Carlsson | 851853d | 2010-03-29 02:38:51 +0000 | [diff] [blame] | 1179 | if (CodeGenVTables::needsVTTParameter(CurGD) && |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1180 | (RD->getNumVBases() || NearestVBase)) { |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1181 | // Get the secondary vpointer index. |
| 1182 | uint64_t VirtualPointerIndex = |
| 1183 | CGM.getVTables().getSecondaryVirtualPointerIndex(VTableClass, Base); |
| 1184 | |
| 1185 | /// Load the VTT. |
| 1186 | llvm::Value *VTT = LoadCXXVTT(); |
| 1187 | if (VirtualPointerIndex) |
| 1188 | VTT = Builder.CreateConstInBoundsGEP1_64(VTT, VirtualPointerIndex); |
| 1189 | |
| 1190 | // And load the address point from the VTT. |
| 1191 | VTableAddressPoint = Builder.CreateLoad(VTT); |
| 1192 | } else { |
Anders Carlsson | 64c9eca | 2010-03-29 02:08:26 +0000 | [diff] [blame] | 1193 | uint64_t AddressPoint = CGM.getVTables().getAddressPoint(Base, VTableClass); |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1194 | VTableAddressPoint = |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1195 | Builder.CreateConstInBoundsGEP2_64(VTable, 0, AddressPoint); |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1196 | } |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1197 | |
Anders Carlsson | 36fd6be | 2010-04-20 16:22:16 +0000 | [diff] [blame] | 1198 | // Compute where to store the address point. |
Anders Carlsson | 8246cc7 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 1199 | llvm::Value *VirtualOffset = 0; |
| 1200 | uint64_t NonVirtualOffset = 0; |
Anders Carlsson | 3e79c30 | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 1201 | |
| 1202 | if (CodeGenVTables::needsVTTParameter(CurGD) && NearestVBase) { |
| 1203 | // We need to use the virtual base offset offset because the virtual base |
| 1204 | // might have a different offset in the most derived class. |
Anders Carlsson | 8246cc7 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 1205 | VirtualOffset = GetVirtualBaseClassOffset(LoadCXXThis(), VTableClass, |
| 1206 | NearestVBase); |
| 1207 | NonVirtualOffset = OffsetFromNearestVBase / 8; |
Anders Carlsson | 3e79c30 | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 1208 | } else { |
Anders Carlsson | 8246cc7 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 1209 | // We can just use the base offset in the complete class. |
| 1210 | NonVirtualOffset = Base.getBaseOffset() / 8; |
Anders Carlsson | 3e79c30 | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 1211 | } |
Anders Carlsson | 8246cc7 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 1212 | |
| 1213 | // Apply the offsets. |
| 1214 | llvm::Value *VTableField = LoadCXXThis(); |
| 1215 | |
| 1216 | if (NonVirtualOffset || VirtualOffset) |
| 1217 | VTableField = ApplyNonVirtualAndVirtualOffset(*this, VTableField, |
| 1218 | NonVirtualOffset, |
| 1219 | VirtualOffset); |
Anders Carlsson | 36fd6be | 2010-04-20 16:22:16 +0000 | [diff] [blame] | 1220 | |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1221 | // Finally, store the address point. |
| 1222 | const llvm::Type *AddressPointPtrTy = |
| 1223 | VTableAddressPoint->getType()->getPointerTo(); |
| 1224 | VTableField = Builder.CreateBitCast(VTableField, AddressPointPtrTy); |
| 1225 | Builder.CreateStore(VTableAddressPoint, VTableField); |
| 1226 | } |
| 1227 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1228 | void |
| 1229 | CodeGenFunction::InitializeVTablePointers(BaseSubobject Base, |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1230 | const CXXRecordDecl *NearestVBase, |
Anders Carlsson | 4235840 | 2010-05-03 00:07:07 +0000 | [diff] [blame] | 1231 | uint64_t OffsetFromNearestVBase, |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1232 | bool BaseIsNonVirtualPrimaryBase, |
| 1233 | llvm::Constant *VTable, |
| 1234 | const CXXRecordDecl *VTableClass, |
| 1235 | VisitedVirtualBasesSetTy& VBases) { |
| 1236 | // If this base is a non-virtual primary base the address point has already |
| 1237 | // been set. |
| 1238 | if (!BaseIsNonVirtualPrimaryBase) { |
| 1239 | // Initialize the vtable pointer for this base. |
Anders Carlsson | 4235840 | 2010-05-03 00:07:07 +0000 | [diff] [blame] | 1240 | InitializeVTablePointer(Base, NearestVBase, OffsetFromNearestVBase, |
| 1241 | VTable, VTableClass); |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | const CXXRecordDecl *RD = Base.getBase(); |
| 1245 | |
| 1246 | // Traverse bases. |
| 1247 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1248 | E = RD->bases_end(); I != E; ++I) { |
| 1249 | CXXRecordDecl *BaseDecl |
| 1250 | = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1251 | |
| 1252 | // Ignore classes without a vtable. |
| 1253 | if (!BaseDecl->isDynamicClass()) |
| 1254 | continue; |
| 1255 | |
| 1256 | uint64_t BaseOffset; |
Anders Carlsson | 4235840 | 2010-05-03 00:07:07 +0000 | [diff] [blame] | 1257 | uint64_t BaseOffsetFromNearestVBase; |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1258 | bool BaseDeclIsNonVirtualPrimaryBase; |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1259 | |
| 1260 | if (I->isVirtual()) { |
| 1261 | // Check if we've visited this virtual base before. |
| 1262 | if (!VBases.insert(BaseDecl)) |
| 1263 | continue; |
| 1264 | |
| 1265 | const ASTRecordLayout &Layout = |
| 1266 | getContext().getASTRecordLayout(VTableClass); |
| 1267 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1268 | BaseOffset = Layout.getVBaseClassOffset(BaseDecl); |
Anders Carlsson | 4235840 | 2010-05-03 00:07:07 +0000 | [diff] [blame] | 1269 | BaseOffsetFromNearestVBase = 0; |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1270 | BaseDeclIsNonVirtualPrimaryBase = false; |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1271 | } else { |
| 1272 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 1273 | |
| 1274 | BaseOffset = Base.getBaseOffset() + Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | 4235840 | 2010-05-03 00:07:07 +0000 | [diff] [blame] | 1275 | BaseOffsetFromNearestVBase = |
Anders Carlsson | 8246cc7 | 2010-05-03 00:29:58 +0000 | [diff] [blame] | 1276 | OffsetFromNearestVBase + Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1277 | BaseDeclIsNonVirtualPrimaryBase = Layout.getPrimaryBase() == BaseDecl; |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | InitializeVTablePointers(BaseSubobject(BaseDecl, BaseOffset), |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1281 | I->isVirtual() ? BaseDecl : NearestVBase, |
Anders Carlsson | 4235840 | 2010-05-03 00:07:07 +0000 | [diff] [blame] | 1282 | BaseOffsetFromNearestVBase, |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1283 | BaseDeclIsNonVirtualPrimaryBase, |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1284 | VTable, VTableClass, VBases); |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | void CodeGenFunction::InitializeVTablePointers(const CXXRecordDecl *RD) { |
| 1289 | // Ignore classes without a vtable. |
Anders Carlsson | 0703690 | 2010-03-26 04:39:42 +0000 | [diff] [blame] | 1290 | if (!RD->isDynamicClass()) |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1291 | return; |
| 1292 | |
Anders Carlsson | 0703690 | 2010-03-26 04:39:42 +0000 | [diff] [blame] | 1293 | // Get the VTable. |
| 1294 | llvm::Constant *VTable = CGM.getVTables().GetAddrOfVTable(RD); |
Anders Carlsson | 5c6c1d9 | 2010-03-24 03:57:14 +0000 | [diff] [blame] | 1295 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1296 | // Initialize the vtable pointers for this class and all of its bases. |
| 1297 | VisitedVirtualBasesSetTy VBases; |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1298 | InitializeVTablePointers(BaseSubobject(RD, 0), /*NearestVBase=*/0, |
Anders Carlsson | 4235840 | 2010-05-03 00:07:07 +0000 | [diff] [blame] | 1299 | /*OffsetFromNearestVBase=*/0, |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1300 | /*BaseIsNonVirtualPrimaryBase=*/false, |
| 1301 | VTable, RD, VBases); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1302 | } |