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