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 |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 23 | ComputeNonVirtualBaseClassOffset(ASTContext &Context, |
| 24 | const CXXBasePath &Path, |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 25 | unsigned Start) { |
| 26 | uint64_t Offset = 0; |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 27 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 28 | for (unsigned i = Start, e = Path.size(); i != e; ++i) { |
| 29 | const CXXBasePathElement& Element = Path[i]; |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 30 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 31 | // Get the layout. |
| 32 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Element.Class); |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 33 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 34 | const CXXBaseSpecifier *BS = Element.Base; |
| 35 | assert(!BS->isVirtual() && "Should not see virtual bases here!"); |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 36 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 37 | const CXXRecordDecl *Base = |
| 38 | cast<CXXRecordDecl>(BS->getType()->getAs<RecordType>()->getDecl()); |
| 39 | |
| 40 | // Add the offset. |
| 41 | Offset += Layout.getBaseClassOffset(Base) / 8; |
| 42 | } |
| 43 | |
| 44 | return Offset; |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Anders Carlsson | 84080ec | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 47 | llvm::Constant * |
Anders Carlsson | bb7e17b | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 48 | CodeGenModule::GetNonVirtualBaseClassOffset(const CXXRecordDecl *Class, |
| 49 | const CXXRecordDecl *BaseClass) { |
| 50 | if (Class == BaseClass) |
Anders Carlsson | 84080ec | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 51 | return 0; |
| 52 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 53 | CXXBasePaths Paths(/*FindAmbiguities=*/false, |
| 54 | /*RecordPaths=*/true, /*DetectVirtual=*/false); |
Anders Carlsson | bb7e17b | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 55 | if (!const_cast<CXXRecordDecl *>(Class)-> |
| 56 | isDerivedFrom(const_cast<CXXRecordDecl *>(BaseClass), Paths)) { |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 57 | assert(false && "Class must be derived from the passed in base class!"); |
| 58 | return 0; |
| 59 | } |
Anders Carlsson | 84080ec | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 60 | |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 61 | uint64_t Offset = ComputeNonVirtualBaseClassOffset(getContext(), |
| 62 | Paths.front(), 0); |
Anders Carlsson | 84080ec | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 63 | if (!Offset) |
| 64 | return 0; |
| 65 | |
Anders Carlsson | 2b35835 | 2009-10-03 14:56:57 +0000 | [diff] [blame] | 66 | const llvm::Type *PtrDiffTy = |
| 67 | Types.ConvertType(getContext().getPointerDiffType()); |
Anders Carlsson | 84080ec | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 68 | |
| 69 | return llvm::ConstantInt::get(PtrDiffTy, Offset); |
| 70 | } |
| 71 | |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 72 | /// Gets the address of a virtual base class within a complete object. |
| 73 | /// This should only be used for (1) non-virtual bases or (2) virtual bases |
| 74 | /// when the type is known to be complete (e.g. in complete destructors). |
| 75 | /// |
| 76 | /// The object pointed to by 'This' is assumed to be non-null. |
| 77 | llvm::Value * |
| 78 | CodeGenFunction::GetAddressOfBaseOfCompleteClass(llvm::Value *This, |
| 79 | bool isBaseVirtual, |
| 80 | const CXXRecordDecl *Derived, |
| 81 | const CXXRecordDecl *Base) { |
| 82 | // 'this' must be a pointer (in some address space) to Derived. |
| 83 | assert(This->getType()->isPointerTy() && |
| 84 | cast<llvm::PointerType>(This->getType())->getElementType() |
| 85 | == ConvertType(Derived)); |
| 86 | |
| 87 | // Compute the offset of the virtual base. |
| 88 | uint64_t Offset; |
| 89 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Derived); |
| 90 | if (isBaseVirtual) |
| 91 | Offset = Layout.getVBaseClassOffset(Base); |
| 92 | else |
| 93 | Offset = Layout.getBaseClassOffset(Base); |
| 94 | |
| 95 | // Shift and cast down to the base type. |
| 96 | // TODO: for complete types, this should be possible with a GEP. |
| 97 | llvm::Value *V = This; |
| 98 | if (Offset) { |
| 99 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext()); |
| 100 | V = Builder.CreateBitCast(V, Int8PtrTy); |
| 101 | V = Builder.CreateConstInBoundsGEP1_64(V, Offset / 8); |
| 102 | } |
| 103 | V = Builder.CreateBitCast(V, ConvertType(Base)->getPointerTo()); |
| 104 | |
| 105 | return V; |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 106 | } |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 107 | |
Anders Carlsson | 9dc228a | 2010-04-20 16:03:35 +0000 | [diff] [blame] | 108 | static llvm::Value * |
| 109 | ApplyNonVirtualAndVirtualOffset(CodeGenFunction &CGF, llvm::Value *ThisPtr, |
| 110 | uint64_t NonVirtual, llvm::Value *Virtual) { |
| 111 | const llvm::Type *PtrDiffTy = |
| 112 | CGF.ConvertType(CGF.getContext().getPointerDiffType()); |
| 113 | |
| 114 | llvm::Value *NonVirtualOffset = 0; |
| 115 | if (NonVirtual) |
| 116 | NonVirtualOffset = llvm::ConstantInt::get(PtrDiffTy, NonVirtual); |
| 117 | |
| 118 | llvm::Value *BaseOffset; |
| 119 | if (Virtual) { |
| 120 | if (NonVirtualOffset) |
| 121 | BaseOffset = CGF.Builder.CreateAdd(Virtual, NonVirtualOffset); |
| 122 | else |
| 123 | BaseOffset = Virtual; |
| 124 | } else |
| 125 | BaseOffset = NonVirtualOffset; |
| 126 | |
| 127 | // Apply the base offset. |
| 128 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext()); |
| 129 | ThisPtr = CGF.Builder.CreateBitCast(ThisPtr, Int8PtrTy); |
| 130 | ThisPtr = CGF.Builder.CreateGEP(ThisPtr, BaseOffset, "add.ptr"); |
| 131 | |
| 132 | return ThisPtr; |
| 133 | } |
| 134 | |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 135 | llvm::Value * |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 136 | CodeGenFunction::GetAddressOfBaseClass(llvm::Value *Value, |
Anders Carlsson | bb7e17b | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 137 | const CXXRecordDecl *Class, |
| 138 | const CXXRecordDecl *BaseClass, |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 139 | bool NullCheckValue) { |
Anders Carlsson | dfd0330 | 2009-09-22 21:58:22 +0000 | [diff] [blame] | 140 | QualType BTy = |
| 141 | getContext().getCanonicalType( |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 142 | getContext().getTypeDeclType(BaseClass)); |
Anders Carlsson | dfd0330 | 2009-09-22 21:58:22 +0000 | [diff] [blame] | 143 | const llvm::Type *BasePtrTy = llvm::PointerType::getUnqual(ConvertType(BTy)); |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 144 | |
Anders Carlsson | bb7e17b | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 145 | if (Class == BaseClass) { |
Anders Carlsson | dfd0330 | 2009-09-22 21:58:22 +0000 | [diff] [blame] | 146 | // Just cast back. |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 147 | return Builder.CreateBitCast(Value, BasePtrTy); |
Anders Carlsson | dfd0330 | 2009-09-22 21:58:22 +0000 | [diff] [blame] | 148 | } |
Anders Carlsson | 905a100 | 2010-01-31 02:39:02 +0000 | [diff] [blame] | 149 | |
Anders Carlsson | 2692d82 | 2010-04-20 05:07:22 +0000 | [diff] [blame] | 150 | #ifndef NDEBUG |
| 151 | CXXBasePaths Paths(/*FindAmbiguities=*/true, |
| 152 | /*RecordPaths=*/true, /*DetectVirtual=*/false); |
| 153 | #else |
Anders Carlsson | 905a100 | 2010-01-31 02:39:02 +0000 | [diff] [blame] | 154 | CXXBasePaths Paths(/*FindAmbiguities=*/false, |
| 155 | /*RecordPaths=*/true, /*DetectVirtual=*/false); |
Anders Carlsson | 2692d82 | 2010-04-20 05:07:22 +0000 | [diff] [blame] | 156 | #endif |
Anders Carlsson | 905a100 | 2010-01-31 02:39:02 +0000 | [diff] [blame] | 157 | if (!const_cast<CXXRecordDecl *>(Class)-> |
| 158 | isDerivedFrom(const_cast<CXXRecordDecl *>(BaseClass), Paths)) { |
| 159 | assert(false && "Class must be derived from the passed in base class!"); |
| 160 | return 0; |
| 161 | } |
| 162 | |
Anders Carlsson | 2692d82 | 2010-04-20 05:07:22 +0000 | [diff] [blame] | 163 | assert(!Paths.isAmbiguous(BTy) && "Path is ambiguous"); |
| 164 | |
Anders Carlsson | 905a100 | 2010-01-31 02:39:02 +0000 | [diff] [blame] | 165 | unsigned Start = 0; |
Anders Carlsson | 905a100 | 2010-01-31 02:39:02 +0000 | [diff] [blame] | 166 | |
| 167 | const CXXBasePath &Path = Paths.front(); |
| 168 | const CXXRecordDecl *VBase = 0; |
| 169 | for (unsigned i = 0, e = Path.size(); i != e; ++i) { |
| 170 | const CXXBasePathElement& Element = Path[i]; |
| 171 | if (Element.Base->isVirtual()) { |
| 172 | Start = i+1; |
| 173 | QualType VBaseType = Element.Base->getType(); |
| 174 | VBase = cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl()); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | uint64_t Offset = |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 179 | ComputeNonVirtualBaseClassOffset(getContext(), Paths.front(), Start); |
Eli Friedman | 4a5dc24 | 2009-11-10 22:48:10 +0000 | [diff] [blame] | 180 | |
Anders Carlsson | 905a100 | 2010-01-31 02:39:02 +0000 | [diff] [blame] | 181 | if (!Offset && !VBase) { |
| 182 | // Just cast back. |
| 183 | return Builder.CreateBitCast(Value, BasePtrTy); |
| 184 | } |
| 185 | |
Anders Carlsson | 32baf62 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 186 | llvm::BasicBlock *CastNull = 0; |
| 187 | llvm::BasicBlock *CastNotNull = 0; |
| 188 | llvm::BasicBlock *CastEnd = 0; |
| 189 | |
| 190 | if (NullCheckValue) { |
| 191 | CastNull = createBasicBlock("cast.null"); |
| 192 | CastNotNull = createBasicBlock("cast.notnull"); |
| 193 | CastEnd = createBasicBlock("cast.end"); |
| 194 | |
| 195 | llvm::Value *IsNull = |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 196 | Builder.CreateICmpEQ(Value, |
| 197 | llvm::Constant::getNullValue(Value->getType())); |
Anders Carlsson | 32baf62 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 198 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
| 199 | EmitBlock(CastNotNull); |
| 200 | } |
| 201 | |
Anders Carlsson | 9dc228a | 2010-04-20 16:03:35 +0000 | [diff] [blame] | 202 | llvm::Value *VirtualOffset = 0; |
| 203 | |
Anders Carlsson | 905a100 | 2010-01-31 02:39:02 +0000 | [diff] [blame] | 204 | if (VBase) |
| 205 | VirtualOffset = GetVirtualBaseClassOffset(Value, Class, VBase); |
Eli Friedman | 4a5dc24 | 2009-11-10 22:48:10 +0000 | [diff] [blame] | 206 | |
Anders Carlsson | 9dc228a | 2010-04-20 16:03:35 +0000 | [diff] [blame] | 207 | // Apply the offsets. |
| 208 | Value = ApplyNonVirtualAndVirtualOffset(*this, Value, Offset, VirtualOffset); |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 209 | |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 210 | // Cast back. |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 211 | Value = Builder.CreateBitCast(Value, BasePtrTy); |
Anders Carlsson | 32baf62 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 212 | |
| 213 | if (NullCheckValue) { |
| 214 | Builder.CreateBr(CastEnd); |
| 215 | EmitBlock(CastNull); |
| 216 | Builder.CreateBr(CastEnd); |
| 217 | EmitBlock(CastEnd); |
| 218 | |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 219 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType()); |
Anders Carlsson | 32baf62 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 220 | PHI->reserveOperandSpace(2); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 221 | PHI->addIncoming(Value, CastNotNull); |
| 222 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), |
Anders Carlsson | 32baf62 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 223 | CastNull); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 224 | Value = PHI; |
Anders Carlsson | 32baf62 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 225 | } |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 226 | |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 227 | return Value; |
| 228 | } |
| 229 | |
| 230 | llvm::Value * |
| 231 | CodeGenFunction::GetAddressOfDerivedClass(llvm::Value *Value, |
Anders Carlsson | bb7e17b | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 232 | const CXXRecordDecl *Class, |
| 233 | const CXXRecordDecl *DerivedClass, |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 234 | bool NullCheckValue) { |
| 235 | QualType DerivedTy = |
| 236 | getContext().getCanonicalType( |
Anders Carlsson | bb7e17b | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 237 | getContext().getTypeDeclType(const_cast<CXXRecordDecl*>(DerivedClass))); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 238 | const llvm::Type *DerivedPtrTy = ConvertType(DerivedTy)->getPointerTo(); |
| 239 | |
Anders Carlsson | bb7e17b | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 240 | if (Class == DerivedClass) { |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 241 | // Just cast back. |
| 242 | return Builder.CreateBitCast(Value, DerivedPtrTy); |
| 243 | } |
| 244 | |
Anders Carlsson | a552ea7 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 245 | llvm::Value *NonVirtualOffset = |
| 246 | CGM.GetNonVirtualBaseClassOffset(DerivedClass, Class); |
| 247 | |
| 248 | if (!NonVirtualOffset) { |
| 249 | // No offset, we can just cast back. |
| 250 | return Builder.CreateBitCast(Value, DerivedPtrTy); |
| 251 | } |
| 252 | |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 253 | llvm::BasicBlock *CastNull = 0; |
| 254 | llvm::BasicBlock *CastNotNull = 0; |
| 255 | llvm::BasicBlock *CastEnd = 0; |
| 256 | |
| 257 | if (NullCheckValue) { |
| 258 | CastNull = createBasicBlock("cast.null"); |
| 259 | CastNotNull = createBasicBlock("cast.notnull"); |
| 260 | CastEnd = createBasicBlock("cast.end"); |
| 261 | |
| 262 | llvm::Value *IsNull = |
| 263 | Builder.CreateICmpEQ(Value, |
| 264 | llvm::Constant::getNullValue(Value->getType())); |
| 265 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
| 266 | EmitBlock(CastNotNull); |
| 267 | } |
| 268 | |
Anders Carlsson | a552ea7 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 269 | // Apply the offset. |
| 270 | Value = Builder.CreatePtrToInt(Value, NonVirtualOffset->getType()); |
| 271 | Value = Builder.CreateSub(Value, NonVirtualOffset); |
| 272 | Value = Builder.CreateIntToPtr(Value, DerivedPtrTy); |
| 273 | |
| 274 | // Just cast. |
| 275 | Value = Builder.CreateBitCast(Value, DerivedPtrTy); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 276 | |
| 277 | if (NullCheckValue) { |
| 278 | Builder.CreateBr(CastEnd); |
| 279 | EmitBlock(CastNull); |
| 280 | Builder.CreateBr(CastEnd); |
| 281 | EmitBlock(CastEnd); |
| 282 | |
| 283 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType()); |
| 284 | PHI->reserveOperandSpace(2); |
| 285 | PHI->addIncoming(Value, CastNotNull); |
| 286 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), |
| 287 | CastNull); |
| 288 | Value = PHI; |
| 289 | } |
| 290 | |
| 291 | return Value; |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 292 | } |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 293 | |
Anders Carlsson | 21c9ad9 | 2010-03-30 03:27:09 +0000 | [diff] [blame] | 294 | /// EmitCopyCtorCall - Emit a call to a copy constructor. |
| 295 | static void |
| 296 | EmitCopyCtorCall(CodeGenFunction &CGF, |
| 297 | const CXXConstructorDecl *CopyCtor, CXXCtorType CopyCtorType, |
| 298 | llvm::Value *ThisPtr, llvm::Value *VTT, llvm::Value *Src) { |
| 299 | llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor, CopyCtorType); |
| 300 | |
| 301 | CallArgList CallArgs; |
| 302 | |
| 303 | // Push the this ptr. |
| 304 | CallArgs.push_back(std::make_pair(RValue::get(ThisPtr), |
| 305 | CopyCtor->getThisType(CGF.getContext()))); |
| 306 | |
| 307 | // Push the VTT parameter if necessary. |
| 308 | if (VTT) { |
| 309 | QualType T = CGF.getContext().getPointerType(CGF.getContext().VoidPtrTy); |
| 310 | CallArgs.push_back(std::make_pair(RValue::get(VTT), T)); |
| 311 | } |
| 312 | |
| 313 | // Push the Src ptr. |
| 314 | CallArgs.push_back(std::make_pair(RValue::get(Src), |
| 315 | CopyCtor->getParamDecl(0)->getType())); |
| 316 | |
| 317 | |
| 318 | { |
| 319 | CodeGenFunction::CXXTemporariesCleanupScope Scope(CGF); |
| 320 | |
| 321 | // If the copy constructor has default arguments, emit them. |
| 322 | for (unsigned I = 1, E = CopyCtor->getNumParams(); I < E; ++I) { |
| 323 | const ParmVarDecl *Param = CopyCtor->getParamDecl(I); |
| 324 | const Expr *DefaultArgExpr = Param->getDefaultArg(); |
| 325 | |
| 326 | assert(DefaultArgExpr && "Ctor parameter must have default arg!"); |
| 327 | |
| 328 | QualType ArgType = Param->getType(); |
| 329 | CallArgs.push_back(std::make_pair(CGF.EmitCallArg(DefaultArgExpr, |
| 330 | ArgType), |
| 331 | ArgType)); |
| 332 | } |
| 333 | |
| 334 | const FunctionProtoType *FPT = |
| 335 | CopyCtor->getType()->getAs<FunctionProtoType>(); |
| 336 | CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(CallArgs, FPT), |
| 337 | Callee, ReturnValueSlot(), CallArgs, CopyCtor); |
| 338 | } |
| 339 | } |
| 340 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 341 | /// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class |
| 342 | /// array of objects from SrcValue to DestValue. Copying can be either a bitwise |
| 343 | /// copy or via a copy constructor call. |
| 344 | // FIXME. Consolidate this with EmitCXXAggrConstructorCall. |
| 345 | void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest, |
| 346 | llvm::Value *Src, |
| 347 | const ArrayType *Array, |
| 348 | const CXXRecordDecl *BaseClassDecl, |
| 349 | QualType Ty) { |
| 350 | const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); |
| 351 | assert(CA && "VLA cannot be copied over"); |
| 352 | bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor(); |
| 353 | |
| 354 | // Create a temporary for the loop index and initialize it with 0. |
| 355 | llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext), |
| 356 | "loop.index"); |
| 357 | llvm::Value* zeroConstant = |
| 358 | llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)); |
| 359 | Builder.CreateStore(zeroConstant, IndexPtr); |
| 360 | // Start the loop with a block that tests the condition. |
| 361 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 362 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
| 363 | |
| 364 | EmitBlock(CondBlock); |
| 365 | |
| 366 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 367 | // Generate: if (loop-index < number-of-elements fall to the loop body, |
| 368 | // otherwise, go to the block after the for-loop. |
| 369 | uint64_t NumElements = getContext().getConstantArrayElementCount(CA); |
| 370 | llvm::Value * NumElementsPtr = |
| 371 | llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements); |
| 372 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 373 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr, |
| 374 | "isless"); |
| 375 | // If the condition is true, execute the body. |
| 376 | Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
| 377 | |
| 378 | EmitBlock(ForBody); |
| 379 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 380 | // Inside the loop body, emit the constructor call on the array element. |
| 381 | Counter = Builder.CreateLoad(IndexPtr); |
| 382 | Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress"); |
| 383 | Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress"); |
| 384 | if (BitwiseCopy) |
| 385 | EmitAggregateCopy(Dest, Src, Ty); |
| 386 | else if (CXXConstructorDecl *BaseCopyCtor = |
Anders Carlsson | 8887bdc | 2010-03-30 03:30:08 +0000 | [diff] [blame] | 387 | BaseClassDecl->getCopyConstructor(getContext(), 0)) |
| 388 | EmitCopyCtorCall(*this, BaseCopyCtor, Ctor_Complete, Dest, 0, Src); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 389 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 390 | EmitBlock(ContinueBlock); |
| 391 | |
| 392 | // Emit the increment of the loop counter. |
| 393 | llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); |
| 394 | Counter = Builder.CreateLoad(IndexPtr); |
| 395 | NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); |
| 396 | Builder.CreateStore(NextVal, IndexPtr); |
| 397 | |
| 398 | // Finally, branch back up to the condition for the next iteration. |
| 399 | EmitBranch(CondBlock); |
| 400 | |
| 401 | // Emit the fall-through block. |
| 402 | EmitBlock(AfterFor, true); |
| 403 | } |
| 404 | |
| 405 | /// EmitClassAggrCopyAssignment - This routine generates code to assign a class |
| 406 | /// array of objects from SrcValue to DestValue. Assignment can be either a |
| 407 | /// bitwise assignment or via a copy assignment operator function call. |
| 408 | /// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy |
| 409 | void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest, |
| 410 | llvm::Value *Src, |
| 411 | const ArrayType *Array, |
| 412 | const CXXRecordDecl *BaseClassDecl, |
| 413 | QualType Ty) { |
| 414 | const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); |
| 415 | assert(CA && "VLA cannot be asssigned"); |
| 416 | bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment(); |
| 417 | |
| 418 | // Create a temporary for the loop index and initialize it with 0. |
| 419 | llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext), |
| 420 | "loop.index"); |
| 421 | llvm::Value* zeroConstant = |
| 422 | llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)); |
| 423 | Builder.CreateStore(zeroConstant, IndexPtr); |
| 424 | // Start the loop with a block that tests the condition. |
| 425 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 426 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
| 427 | |
| 428 | EmitBlock(CondBlock); |
| 429 | |
| 430 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 431 | // Generate: if (loop-index < number-of-elements fall to the loop body, |
| 432 | // otherwise, go to the block after the for-loop. |
| 433 | uint64_t NumElements = getContext().getConstantArrayElementCount(CA); |
| 434 | llvm::Value * NumElementsPtr = |
| 435 | llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements); |
| 436 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 437 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr, |
| 438 | "isless"); |
| 439 | // If the condition is true, execute the body. |
| 440 | Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
| 441 | |
| 442 | EmitBlock(ForBody); |
| 443 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 444 | // Inside the loop body, emit the assignment operator call on array element. |
| 445 | Counter = Builder.CreateLoad(IndexPtr); |
| 446 | Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress"); |
| 447 | Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress"); |
| 448 | const CXXMethodDecl *MD = 0; |
| 449 | if (BitwiseAssign) |
| 450 | EmitAggregateCopy(Dest, Src, Ty); |
| 451 | else { |
Eli Friedman | 8a850ba | 2010-01-15 20:06:11 +0000 | [diff] [blame] | 452 | BaseClassDecl->hasConstCopyAssignment(getContext(), MD); |
| 453 | assert(MD && "EmitClassAggrCopyAssignment - No user assign"); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 454 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 455 | const llvm::Type *LTy = |
| 456 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 457 | FPT->isVariadic()); |
| 458 | llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy); |
| 459 | |
| 460 | CallArgList CallArgs; |
| 461 | // Push the this (Dest) ptr. |
| 462 | CallArgs.push_back(std::make_pair(RValue::get(Dest), |
| 463 | MD->getThisType(getContext()))); |
| 464 | |
| 465 | // Push the Src ptr. |
Eli Friedman | 8a850ba | 2010-01-15 20:06:11 +0000 | [diff] [blame] | 466 | QualType SrcTy = MD->getParamDecl(0)->getType(); |
| 467 | RValue SrcValue = SrcTy->isReferenceType() ? RValue::get(Src) : |
| 468 | RValue::getAggregate(Src); |
| 469 | CallArgs.push_back(std::make_pair(SrcValue, SrcTy)); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 470 | EmitCall(CGM.getTypes().getFunctionInfo(CallArgs, FPT), |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 471 | Callee, ReturnValueSlot(), CallArgs, MD); |
| 472 | } |
| 473 | EmitBlock(ContinueBlock); |
| 474 | |
| 475 | // Emit the increment of the loop counter. |
| 476 | llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); |
| 477 | Counter = Builder.CreateLoad(IndexPtr); |
| 478 | NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); |
| 479 | Builder.CreateStore(NextVal, IndexPtr); |
| 480 | |
| 481 | // Finally, branch back up to the condition for the next iteration. |
| 482 | EmitBranch(CondBlock); |
| 483 | |
| 484 | // Emit the fall-through block. |
| 485 | EmitBlock(AfterFor, true); |
| 486 | } |
| 487 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 488 | /// GetVTTParameter - Return the VTT parameter that should be passed to a |
| 489 | /// base constructor/destructor with virtual bases. |
| 490 | static llvm::Value *GetVTTParameter(CodeGenFunction &CGF, GlobalDecl GD) { |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 491 | if (!CodeGenVTables::needsVTTParameter(GD)) { |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 492 | // This constructor/destructor does not need a VTT parameter. |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | const CXXRecordDecl *RD = cast<CXXMethodDecl>(CGF.CurFuncDecl)->getParent(); |
| 497 | const CXXRecordDecl *Base = cast<CXXMethodDecl>(GD.getDecl())->getParent(); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 498 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 499 | llvm::Value *VTT; |
| 500 | |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 501 | uint64_t SubVTTIndex; |
| 502 | |
| 503 | // If the record matches the base, this is the complete ctor/dtor |
| 504 | // variant calling the base variant in a class with virtual bases. |
| 505 | if (RD == Base) { |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 506 | assert(!CodeGenVTables::needsVTTParameter(CGF.CurGD) && |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 507 | "doing no-op VTT offset in base dtor/ctor?"); |
| 508 | SubVTTIndex = 0; |
| 509 | } else { |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 510 | SubVTTIndex = CGF.CGM.getVTables().getSubVTTIndex(RD, Base); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 511 | assert(SubVTTIndex != 0 && "Sub-VTT index must be greater than zero!"); |
| 512 | } |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 513 | |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 514 | if (CodeGenVTables::needsVTTParameter(CGF.CurGD)) { |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 515 | // A VTT parameter was passed to the constructor, use it. |
| 516 | VTT = CGF.LoadCXXVTT(); |
| 517 | VTT = CGF.Builder.CreateConstInBoundsGEP1_64(VTT, SubVTTIndex); |
| 518 | } else { |
| 519 | // We're the complete constructor, so get the VTT by name. |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 520 | VTT = CGF.CGM.getVTables().getVTT(RD); |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 521 | VTT = CGF.Builder.CreateConstInBoundsGEP2_64(VTT, 0, SubVTTIndex); |
| 522 | } |
| 523 | |
| 524 | return VTT; |
| 525 | } |
| 526 | |
| 527 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 528 | /// EmitClassMemberwiseCopy - This routine generates code to copy a class |
| 529 | /// object from SrcValue to DestValue. Copying can be either a bitwise copy |
| 530 | /// or via a copy constructor call. |
| 531 | void CodeGenFunction::EmitClassMemberwiseCopy( |
| 532 | llvm::Value *Dest, llvm::Value *Src, |
| 533 | const CXXRecordDecl *ClassDecl, |
| 534 | const CXXRecordDecl *BaseClassDecl, QualType Ty) { |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 535 | CXXCtorType CtorType = Ctor_Complete; |
| 536 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 537 | if (ClassDecl) { |
| 538 | Dest = GetAddressOfBaseClass(Dest, ClassDecl, BaseClassDecl, |
| 539 | /*NullCheckValue=*/false); |
| 540 | Src = GetAddressOfBaseClass(Src, ClassDecl, BaseClassDecl, |
| 541 | /*NullCheckValue=*/false); |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 542 | |
| 543 | // We want to call the base constructor. |
| 544 | CtorType = Ctor_Base; |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 545 | } |
| 546 | if (BaseClassDecl->hasTrivialCopyConstructor()) { |
| 547 | EmitAggregateCopy(Dest, Src, Ty); |
| 548 | return; |
| 549 | } |
| 550 | |
Anders Carlsson | 21c9ad9 | 2010-03-30 03:27:09 +0000 | [diff] [blame] | 551 | CXXConstructorDecl *BaseCopyCtor = |
| 552 | BaseClassDecl->getCopyConstructor(getContext(), 0); |
| 553 | if (!BaseCopyCtor) |
| 554 | return; |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 555 | |
Anders Carlsson | 21c9ad9 | 2010-03-30 03:27:09 +0000 | [diff] [blame] | 556 | llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(BaseCopyCtor, CtorType)); |
| 557 | EmitCopyCtorCall(*this, BaseCopyCtor, CtorType, Dest, VTT, Src); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /// EmitClassCopyAssignment - This routine generates code to copy assign a class |
| 561 | /// object from SrcValue to DestValue. Assignment can be either a bitwise |
| 562 | /// assignment of via an assignment operator call. |
| 563 | // FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot. |
| 564 | void CodeGenFunction::EmitClassCopyAssignment( |
| 565 | llvm::Value *Dest, llvm::Value *Src, |
| 566 | const CXXRecordDecl *ClassDecl, |
| 567 | const CXXRecordDecl *BaseClassDecl, |
| 568 | QualType Ty) { |
| 569 | if (ClassDecl) { |
| 570 | Dest = GetAddressOfBaseClass(Dest, ClassDecl, BaseClassDecl, |
| 571 | /*NullCheckValue=*/false); |
| 572 | Src = GetAddressOfBaseClass(Src, ClassDecl, BaseClassDecl, |
| 573 | /*NullCheckValue=*/false); |
| 574 | } |
| 575 | if (BaseClassDecl->hasTrivialCopyAssignment()) { |
| 576 | EmitAggregateCopy(Dest, Src, Ty); |
| 577 | return; |
| 578 | } |
| 579 | |
| 580 | const CXXMethodDecl *MD = 0; |
Eli Friedman | 8a850ba | 2010-01-15 20:06:11 +0000 | [diff] [blame] | 581 | BaseClassDecl->hasConstCopyAssignment(getContext(), MD); |
| 582 | assert(MD && "EmitClassCopyAssignment - missing copy assign"); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 583 | |
| 584 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 585 | const llvm::Type *LTy = |
| 586 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 587 | FPT->isVariadic()); |
| 588 | llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy); |
| 589 | |
| 590 | CallArgList CallArgs; |
| 591 | // Push the this (Dest) ptr. |
| 592 | CallArgs.push_back(std::make_pair(RValue::get(Dest), |
| 593 | MD->getThisType(getContext()))); |
| 594 | |
| 595 | // Push the Src ptr. |
Eli Friedman | 8a850ba | 2010-01-15 20:06:11 +0000 | [diff] [blame] | 596 | QualType SrcTy = MD->getParamDecl(0)->getType(); |
| 597 | RValue SrcValue = SrcTy->isReferenceType() ? RValue::get(Src) : |
| 598 | RValue::getAggregate(Src); |
| 599 | CallArgs.push_back(std::make_pair(SrcValue, SrcTy)); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 600 | EmitCall(CGM.getTypes().getFunctionInfo(CallArgs, FPT), |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 601 | Callee, ReturnValueSlot(), CallArgs, MD); |
| 602 | } |
| 603 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 604 | /// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a |
| 605 | /// copy constructor, in accordance with section 12.8 (p7 and p8) of C++03 |
| 606 | /// The implicitly-defined copy constructor for class X performs a memberwise |
| 607 | /// copy of its subobjects. The order of copying is the same as the order of |
| 608 | /// initialization of bases and members in a user-defined constructor |
| 609 | /// Each subobject is copied in the manner appropriate to its type: |
| 610 | /// if the subobject is of class type, the copy constructor for the class is |
| 611 | /// used; |
| 612 | /// if the subobject is an array, each element is copied, in the manner |
| 613 | /// appropriate to the element type; |
| 614 | /// if the subobject is of scalar type, the built-in assignment operator is |
| 615 | /// used. |
| 616 | /// Virtual base class subobjects shall be copied only once by the |
| 617 | /// implicitly-defined copy constructor |
| 618 | |
| 619 | void |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 620 | CodeGenFunction::SynthesizeCXXCopyConstructor(const FunctionArgList &Args) { |
| 621 | const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(CurGD.getDecl()); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 622 | const CXXRecordDecl *ClassDecl = Ctor->getParent(); |
| 623 | assert(!ClassDecl->hasUserDeclaredCopyConstructor() && |
| 624 | "SynthesizeCXXCopyConstructor - copy constructor has definition already"); |
| 625 | assert(!Ctor->isTrivial() && "shouldn't need to generate trivial ctor"); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 626 | |
| 627 | FunctionArgList::const_iterator i = Args.begin(); |
| 628 | const VarDecl *ThisArg = i->first; |
| 629 | llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg); |
| 630 | llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this"); |
| 631 | const VarDecl *SrcArg = (i+1)->first; |
| 632 | llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg); |
| 633 | llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj); |
| 634 | |
| 635 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); |
| 636 | Base != ClassDecl->bases_end(); ++Base) { |
| 637 | // FIXME. copy constrution of virtual base NYI |
| 638 | if (Base->isVirtual()) |
| 639 | continue; |
| 640 | |
| 641 | CXXRecordDecl *BaseClassDecl |
| 642 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 643 | EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl, |
| 644 | Base->getType()); |
| 645 | } |
| 646 | |
| 647 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 648 | E = ClassDecl->field_end(); I != E; ++I) { |
| 649 | const FieldDecl *Field = *I; |
| 650 | |
| 651 | QualType FieldType = getContext().getCanonicalType(Field->getType()); |
| 652 | const ConstantArrayType *Array = |
| 653 | getContext().getAsConstantArrayType(FieldType); |
| 654 | if (Array) |
| 655 | FieldType = getContext().getBaseElementType(FieldType); |
| 656 | |
| 657 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
| 658 | CXXRecordDecl *FieldClassDecl |
| 659 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Anders Carlsson | e6d2a53 | 2010-01-29 05:05:36 +0000 | [diff] [blame] | 660 | LValue LHS = EmitLValueForField(LoadOfThis, Field, 0); |
| 661 | LValue RHS = EmitLValueForField(LoadOfSrc, Field, 0); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 662 | if (Array) { |
| 663 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 664 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 665 | llvm::Value *DestBaseAddrPtr = |
| 666 | Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
| 667 | llvm::Value *SrcBaseAddrPtr = |
| 668 | Builder.CreateBitCast(RHS.getAddress(), BasePtr); |
| 669 | EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array, |
| 670 | FieldClassDecl, FieldType); |
| 671 | } |
| 672 | else |
| 673 | EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(), |
| 674 | 0 /*ClassDecl*/, FieldClassDecl, FieldType); |
| 675 | continue; |
| 676 | } |
| 677 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 678 | // Do a built-in assignment of scalar data members. |
Anders Carlsson | 9cfe0ec | 2010-01-29 05:41:25 +0000 | [diff] [blame] | 679 | LValue LHS = EmitLValueForFieldInitialization(LoadOfThis, Field, 0); |
| 680 | LValue RHS = EmitLValueForFieldInitialization(LoadOfSrc, Field, 0); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 681 | |
| 682 | if (!hasAggregateLLVMType(Field->getType())) { |
| 683 | RValue RVRHS = EmitLoadOfLValue(RHS, Field->getType()); |
| 684 | EmitStoreThroughLValue(RVRHS, LHS, Field->getType()); |
| 685 | } else if (Field->getType()->isAnyComplexType()) { |
| 686 | ComplexPairTy Pair = LoadComplexFromAddr(RHS.getAddress(), |
| 687 | RHS.isVolatileQualified()); |
| 688 | StoreComplexToAddr(Pair, LHS.getAddress(), LHS.isVolatileQualified()); |
| 689 | } else { |
| 690 | EmitAggregateCopy(LHS.getAddress(), RHS.getAddress(), Field->getType()); |
| 691 | } |
| 692 | } |
| 693 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 694 | InitializeVTablePointers(ClassDecl); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | /// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator. |
| 698 | /// Before the implicitly-declared copy assignment operator for a class is |
| 699 | /// implicitly defined, all implicitly- declared copy assignment operators for |
| 700 | /// its direct base classes and its nonstatic data members shall have been |
| 701 | /// implicitly defined. [12.8-p12] |
| 702 | /// The implicitly-defined copy assignment operator for class X performs |
| 703 | /// memberwise assignment of its subob- jects. The direct base classes of X are |
| 704 | /// assigned first, in the order of their declaration in |
| 705 | /// the base-specifier-list, and then the immediate nonstatic data members of X |
| 706 | /// are assigned, in the order in which they were declared in the class |
| 707 | /// definition.Each subobject is assigned in the manner appropriate to its type: |
| 708 | /// if the subobject is of class type, the copy assignment operator for the |
| 709 | /// class is used (as if by explicit qualification; that is, ignoring any |
| 710 | /// possible virtual overriding functions in more derived classes); |
| 711 | /// |
| 712 | /// if the subobject is an array, each element is assigned, in the manner |
| 713 | /// appropriate to the element type; |
| 714 | /// |
| 715 | /// if the subobject is of scalar type, the built-in assignment operator is |
| 716 | /// used. |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 717 | void CodeGenFunction::SynthesizeCXXCopyAssignment(const FunctionArgList &Args) { |
| 718 | const CXXMethodDecl *CD = cast<CXXMethodDecl>(CurGD.getDecl()); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 719 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext()); |
| 720 | assert(!ClassDecl->hasUserDeclaredCopyAssignment() && |
| 721 | "SynthesizeCXXCopyAssignment - copy assignment has user declaration"); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 722 | |
| 723 | FunctionArgList::const_iterator i = Args.begin(); |
| 724 | const VarDecl *ThisArg = i->first; |
| 725 | llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg); |
| 726 | llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this"); |
| 727 | const VarDecl *SrcArg = (i+1)->first; |
| 728 | llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg); |
| 729 | llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj); |
| 730 | |
| 731 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); |
| 732 | Base != ClassDecl->bases_end(); ++Base) { |
| 733 | // FIXME. copy assignment of virtual base NYI |
| 734 | if (Base->isVirtual()) |
| 735 | continue; |
| 736 | |
| 737 | CXXRecordDecl *BaseClassDecl |
| 738 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 739 | EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl, |
| 740 | Base->getType()); |
| 741 | } |
| 742 | |
| 743 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 744 | FieldEnd = ClassDecl->field_end(); |
| 745 | Field != FieldEnd; ++Field) { |
| 746 | QualType FieldType = getContext().getCanonicalType((*Field)->getType()); |
| 747 | const ConstantArrayType *Array = |
| 748 | getContext().getAsConstantArrayType(FieldType); |
| 749 | if (Array) |
| 750 | FieldType = getContext().getBaseElementType(FieldType); |
| 751 | |
| 752 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
| 753 | CXXRecordDecl *FieldClassDecl |
| 754 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Anders Carlsson | e6d2a53 | 2010-01-29 05:05:36 +0000 | [diff] [blame] | 755 | LValue LHS = EmitLValueForField(LoadOfThis, *Field, 0); |
| 756 | LValue RHS = EmitLValueForField(LoadOfSrc, *Field, 0); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 757 | if (Array) { |
| 758 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 759 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 760 | llvm::Value *DestBaseAddrPtr = |
| 761 | Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
| 762 | llvm::Value *SrcBaseAddrPtr = |
| 763 | Builder.CreateBitCast(RHS.getAddress(), BasePtr); |
| 764 | EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array, |
| 765 | FieldClassDecl, FieldType); |
| 766 | } |
| 767 | else |
| 768 | EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(), |
| 769 | 0 /*ClassDecl*/, FieldClassDecl, FieldType); |
| 770 | continue; |
| 771 | } |
| 772 | // Do a built-in assignment of scalar data members. |
Anders Carlsson | e6d2a53 | 2010-01-29 05:05:36 +0000 | [diff] [blame] | 773 | LValue LHS = EmitLValueForField(LoadOfThis, *Field, 0); |
| 774 | LValue RHS = EmitLValueForField(LoadOfSrc, *Field, 0); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 775 | if (!hasAggregateLLVMType(Field->getType())) { |
| 776 | RValue RVRHS = EmitLoadOfLValue(RHS, Field->getType()); |
| 777 | EmitStoreThroughLValue(RVRHS, LHS, Field->getType()); |
| 778 | } else if (Field->getType()->isAnyComplexType()) { |
| 779 | ComplexPairTy Pair = LoadComplexFromAddr(RHS.getAddress(), |
| 780 | RHS.isVolatileQualified()); |
| 781 | StoreComplexToAddr(Pair, LHS.getAddress(), LHS.isVolatileQualified()); |
| 782 | } else { |
| 783 | EmitAggregateCopy(LHS.getAddress(), RHS.getAddress(), Field->getType()); |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | // return *this; |
| 788 | Builder.CreateStore(LoadOfThis, ReturnValue); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | static void EmitBaseInitializer(CodeGenFunction &CGF, |
| 792 | const CXXRecordDecl *ClassDecl, |
| 793 | CXXBaseOrMemberInitializer *BaseInit, |
| 794 | CXXCtorType CtorType) { |
| 795 | assert(BaseInit->isBaseInitializer() && |
| 796 | "Must have base initializer!"); |
| 797 | |
| 798 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
| 799 | |
| 800 | const Type *BaseType = BaseInit->getBaseClass(); |
| 801 | CXXRecordDecl *BaseClassDecl = |
| 802 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); |
| 803 | |
Anders Carlsson | 80638c5 | 2010-04-12 00:51:03 +0000 | [diff] [blame] | 804 | bool isBaseVirtual = BaseInit->isBaseVirtual(); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 805 | |
| 806 | // The base constructor doesn't construct virtual bases. |
| 807 | if (CtorType == Ctor_Base && isBaseVirtual) |
| 808 | return; |
| 809 | |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 810 | // We can pretend to be a complete class because it only matters for |
| 811 | // virtual bases, and we only do virtual bases for complete ctors. |
| 812 | llvm::Value *V = ThisPtr; |
| 813 | V = CGF.GetAddressOfBaseOfCompleteClass(V, isBaseVirtual, |
| 814 | ClassDecl, BaseClassDecl); |
| 815 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 816 | CGF.EmitAggExpr(BaseInit->getInit(), V, false, false, true); |
Anders Carlsson | 594d5e8 | 2010-02-06 20:00:21 +0000 | [diff] [blame] | 817 | |
| 818 | if (CGF.Exceptions && !BaseClassDecl->hasTrivialDestructor()) { |
| 819 | // FIXME: Is this OK for C++0x delegating constructors? |
| 820 | CodeGenFunction::EHCleanupBlock Cleanup(CGF); |
| 821 | |
Anders Carlsson | 594d5e8 | 2010-02-06 20:00:21 +0000 | [diff] [blame] | 822 | CXXDestructorDecl *DD = BaseClassDecl->getDestructor(CGF.getContext()); |
| 823 | CGF.EmitCXXDestructorCall(DD, Dtor_Base, V); |
| 824 | } |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | static void EmitMemberInitializer(CodeGenFunction &CGF, |
| 828 | const CXXRecordDecl *ClassDecl, |
| 829 | CXXBaseOrMemberInitializer *MemberInit) { |
| 830 | assert(MemberInit->isMemberInitializer() && |
| 831 | "Must have member initializer!"); |
| 832 | |
| 833 | // non-static data member initializers. |
| 834 | FieldDecl *Field = MemberInit->getMember(); |
| 835 | QualType FieldType = CGF.getContext().getCanonicalType(Field->getType()); |
| 836 | |
| 837 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
Anders Carlsson | 06a2970 | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 838 | LValue LHS = CGF.EmitLValueForFieldInitialization(ThisPtr, Field, 0); |
| 839 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 840 | // If we are initializing an anonymous union field, drill down to the field. |
| 841 | if (MemberInit->getAnonUnionMember()) { |
| 842 | Field = MemberInit->getAnonUnionMember(); |
Anders Carlsson | e6d2a53 | 2010-01-29 05:05:36 +0000 | [diff] [blame] | 843 | LHS = CGF.EmitLValueForField(LHS.getAddress(), Field, 0); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 844 | FieldType = Field->getType(); |
| 845 | } |
| 846 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 847 | // FIXME: If there's no initializer and the CXXBaseOrMemberInitializer |
| 848 | // was implicitly generated, we shouldn't be zeroing memory. |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 849 | RValue RHS; |
| 850 | if (FieldType->isReferenceType()) { |
Anders Carlsson | a64a869 | 2010-02-03 16:38:03 +0000 | [diff] [blame] | 851 | RHS = CGF.EmitReferenceBindingToExpr(MemberInit->getInit(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 852 | /*IsInitializer=*/true); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 853 | CGF.EmitStoreThroughLValue(RHS, LHS, FieldType); |
Eli Friedman | 3bb9412 | 2010-01-31 19:07:50 +0000 | [diff] [blame] | 854 | } else if (FieldType->isArrayType() && !MemberInit->getInit()) { |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 855 | CGF.EmitMemSetToZero(LHS.getAddress(), Field->getType()); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 856 | } else if (!CGF.hasAggregateLLVMType(Field->getType())) { |
| 857 | RHS = RValue::get(CGF.EmitScalarExpr(MemberInit->getInit(), true)); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 858 | CGF.EmitStoreThroughLValue(RHS, LHS, FieldType); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 859 | } else if (MemberInit->getInit()->getType()->isAnyComplexType()) { |
| 860 | CGF.EmitComplexExprIntoAddr(MemberInit->getInit(), LHS.getAddress(), |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 861 | LHS.isVolatileQualified()); |
| 862 | } else { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 863 | CGF.EmitAggExpr(MemberInit->getInit(), LHS.getAddress(), |
| 864 | LHS.isVolatileQualified(), false, true); |
Anders Carlsson | 9405dcd | 2010-02-06 19:50:17 +0000 | [diff] [blame] | 865 | |
| 866 | if (!CGF.Exceptions) |
| 867 | return; |
| 868 | |
| 869 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 870 | if (!RT) |
| 871 | return; |
| 872 | |
| 873 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 874 | if (!RD->hasTrivialDestructor()) { |
| 875 | // FIXME: Is this OK for C++0x delegating constructors? |
| 876 | CodeGenFunction::EHCleanupBlock Cleanup(CGF); |
| 877 | |
| 878 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
| 879 | LValue LHS = CGF.EmitLValueForField(ThisPtr, Field, 0); |
| 880 | |
| 881 | CXXDestructorDecl *DD = RD->getDestructor(CGF.getContext()); |
| 882 | CGF.EmitCXXDestructorCall(DD, Dtor_Complete, LHS.getAddress()); |
| 883 | } |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 887 | /// Checks whether the given constructor is a valid subject for the |
| 888 | /// complete-to-base constructor delegation optimization, i.e. |
| 889 | /// emitting the complete constructor as a simple call to the base |
| 890 | /// constructor. |
| 891 | static bool IsConstructorDelegationValid(const CXXConstructorDecl *Ctor) { |
| 892 | |
| 893 | // Currently we disable the optimization for classes with virtual |
| 894 | // bases because (1) the addresses of parameter variables need to be |
| 895 | // consistent across all initializers but (2) the delegate function |
| 896 | // call necessarily creates a second copy of the parameter variable. |
| 897 | // |
| 898 | // The limiting example (purely theoretical AFAIK): |
| 899 | // struct A { A(int &c) { c++; } }; |
| 900 | // struct B : virtual A { |
| 901 | // B(int count) : A(count) { printf("%d\n", count); } |
| 902 | // }; |
| 903 | // ...although even this example could in principle be emitted as a |
| 904 | // delegation since the address of the parameter doesn't escape. |
| 905 | if (Ctor->getParent()->getNumVBases()) { |
| 906 | // TODO: white-list trivial vbase initializers. This case wouldn't |
| 907 | // be subject to the restrictions below. |
| 908 | |
| 909 | // TODO: white-list cases where: |
| 910 | // - there are no non-reference parameters to the constructor |
| 911 | // - the initializers don't access any non-reference parameters |
| 912 | // - the initializers don't take the address of non-reference |
| 913 | // parameters |
| 914 | // - etc. |
| 915 | // If we ever add any of the above cases, remember that: |
| 916 | // - function-try-blocks will always blacklist this optimization |
| 917 | // - we need to perform the constructor prologue and cleanup in |
| 918 | // EmitConstructorBody. |
| 919 | |
| 920 | return false; |
| 921 | } |
| 922 | |
| 923 | // We also disable the optimization for variadic functions because |
| 924 | // it's impossible to "re-pass" varargs. |
| 925 | if (Ctor->getType()->getAs<FunctionProtoType>()->isVariadic()) |
| 926 | return false; |
| 927 | |
| 928 | return true; |
| 929 | } |
| 930 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 931 | /// EmitConstructorBody - Emits the body of the current constructor. |
| 932 | void CodeGenFunction::EmitConstructorBody(FunctionArgList &Args) { |
| 933 | const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(CurGD.getDecl()); |
| 934 | CXXCtorType CtorType = CurGD.getCtorType(); |
| 935 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 936 | // Before we go any further, try the complete->base constructor |
| 937 | // delegation optimization. |
| 938 | if (CtorType == Ctor_Complete && IsConstructorDelegationValid(Ctor)) { |
| 939 | EmitDelegateCXXConstructorCall(Ctor, Ctor_Base, Args); |
| 940 | return; |
| 941 | } |
| 942 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 943 | Stmt *Body = Ctor->getBody(); |
| 944 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 945 | // Enter the function-try-block before the constructor prologue if |
| 946 | // applicable. |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 947 | CXXTryStmtInfo TryInfo; |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 948 | bool IsTryBody = (Body && isa<CXXTryStmt>(Body)); |
| 949 | |
| 950 | if (IsTryBody) |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 951 | TryInfo = EnterCXXTryStmt(*cast<CXXTryStmt>(Body)); |
| 952 | |
| 953 | unsigned CleanupStackSize = CleanupEntries.size(); |
| 954 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 955 | // Emit the constructor prologue, i.e. the base and member |
| 956 | // initializers. |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 957 | EmitCtorPrologue(Ctor, CtorType); |
| 958 | |
| 959 | // Emit the body of the statement. |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 960 | if (IsTryBody) |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 961 | EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock()); |
| 962 | else if (Body) |
| 963 | EmitStmt(Body); |
| 964 | else { |
| 965 | assert(Ctor->isImplicit() && "bodyless ctor not implicit"); |
| 966 | if (!Ctor->isDefaultConstructor()) { |
| 967 | assert(Ctor->isCopyConstructor()); |
| 968 | SynthesizeCXXCopyConstructor(Args); |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | // Emit any cleanup blocks associated with the member or base |
| 973 | // initializers, which includes (along the exceptional path) the |
| 974 | // destructors for those members and bases that were fully |
| 975 | // constructed. |
| 976 | EmitCleanupBlocks(CleanupStackSize); |
| 977 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 978 | if (IsTryBody) |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 979 | ExitCXXTryStmt(*cast<CXXTryStmt>(Body), TryInfo); |
| 980 | } |
| 981 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 982 | /// EmitCtorPrologue - This routine generates necessary code to initialize |
| 983 | /// base classes and non-static data members belonging to this constructor. |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 984 | void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD, |
| 985 | CXXCtorType CtorType) { |
| 986 | const CXXRecordDecl *ClassDecl = CD->getParent(); |
Anders Carlsson | a78fa2c | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 987 | |
| 988 | llvm::SmallVector<CXXBaseOrMemberInitializer *, 8> MemberInitializers; |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 989 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 990 | for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(), |
| 991 | E = CD->init_end(); |
| 992 | B != E; ++B) { |
| 993 | CXXBaseOrMemberInitializer *Member = (*B); |
| 994 | |
| 995 | assert(LiveTemporaries.empty() && |
| 996 | "Should not have any live temporaries at initializer start!"); |
| 997 | |
| 998 | if (Member->isBaseInitializer()) |
| 999 | EmitBaseInitializer(*this, ClassDecl, Member, CtorType); |
| 1000 | else |
Anders Carlsson | a78fa2c | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 1001 | MemberInitializers.push_back(Member); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1004 | InitializeVTablePointers(ClassDecl); |
Anders Carlsson | a78fa2c | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 1005 | |
| 1006 | for (unsigned I = 0, E = MemberInitializers.size(); I != E; ++I) { |
| 1007 | assert(LiveTemporaries.empty() && |
| 1008 | "Should not have any live temporaries at initializer start!"); |
| 1009 | |
| 1010 | EmitMemberInitializer(*this, ClassDecl, MemberInitializers[I]); |
| 1011 | } |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1014 | /// EmitDestructorBody - Emits the body of the current destructor. |
| 1015 | void CodeGenFunction::EmitDestructorBody(FunctionArgList &Args) { |
| 1016 | const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CurGD.getDecl()); |
| 1017 | CXXDtorType DtorType = CurGD.getDtorType(); |
| 1018 | |
| 1019 | Stmt *Body = Dtor->getBody(); |
| 1020 | |
| 1021 | // If the body is a function-try-block, enter the try before |
| 1022 | // anything else --- unless we're in a deleting destructor, in which |
| 1023 | // case we're just going to call the complete destructor and then |
| 1024 | // call operator delete() on the way out. |
| 1025 | CXXTryStmtInfo TryInfo; |
| 1026 | bool isTryBody = (DtorType != Dtor_Deleting && |
| 1027 | Body && isa<CXXTryStmt>(Body)); |
| 1028 | if (isTryBody) |
| 1029 | TryInfo = EnterCXXTryStmt(*cast<CXXTryStmt>(Body)); |
| 1030 | |
| 1031 | llvm::BasicBlock *DtorEpilogue = createBasicBlock("dtor.epilogue"); |
| 1032 | PushCleanupBlock(DtorEpilogue); |
| 1033 | |
| 1034 | bool SkipBody = false; // should get jump-threaded |
| 1035 | |
| 1036 | // If this is the deleting variant, just invoke the complete |
| 1037 | // variant, then call the appropriate operator delete() on the way |
| 1038 | // out. |
| 1039 | if (DtorType == Dtor_Deleting) { |
| 1040 | EmitCXXDestructorCall(Dtor, Dtor_Complete, LoadCXXThis()); |
| 1041 | SkipBody = true; |
| 1042 | |
| 1043 | // If this is the complete variant, just invoke the base variant; |
| 1044 | // the epilogue will destruct the virtual bases. But we can't do |
| 1045 | // this optimization if the body is a function-try-block, because |
| 1046 | // we'd introduce *two* handler blocks. |
| 1047 | } else if (!isTryBody && DtorType == Dtor_Complete) { |
| 1048 | EmitCXXDestructorCall(Dtor, Dtor_Base, LoadCXXThis()); |
| 1049 | SkipBody = true; |
| 1050 | |
| 1051 | // Otherwise, we're in the base variant, so we need to ensure the |
| 1052 | // vtable ptrs are right before emitting the body. |
| 1053 | } else { |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1054 | InitializeVTablePointers(Dtor->getParent()); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | // Emit the body of the statement. |
| 1058 | if (SkipBody) |
| 1059 | (void) 0; |
| 1060 | else if (isTryBody) |
| 1061 | EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock()); |
| 1062 | else if (Body) |
| 1063 | EmitStmt(Body); |
| 1064 | else { |
| 1065 | assert(Dtor->isImplicit() && "bodyless dtor not implicit"); |
| 1066 | // nothing to do besides what's in the epilogue |
| 1067 | } |
| 1068 | |
| 1069 | // Jump to the cleanup block. |
| 1070 | CleanupBlockInfo Info = PopCleanupBlock(); |
| 1071 | assert(Info.CleanupBlock == DtorEpilogue && "Block mismatch!"); |
| 1072 | EmitBlock(DtorEpilogue); |
| 1073 | |
| 1074 | // Emit the destructor epilogue now. If this is a complete |
| 1075 | // destructor with a function-try-block, perform the base epilogue |
| 1076 | // as well. |
| 1077 | if (isTryBody && DtorType == Dtor_Complete) |
| 1078 | EmitDtorEpilogue(Dtor, Dtor_Base); |
| 1079 | EmitDtorEpilogue(Dtor, DtorType); |
| 1080 | |
| 1081 | // Link up the cleanup information. |
| 1082 | if (Info.SwitchBlock) |
| 1083 | EmitBlock(Info.SwitchBlock); |
| 1084 | if (Info.EndBlock) |
| 1085 | EmitBlock(Info.EndBlock); |
| 1086 | |
| 1087 | // Exit the try if applicable. |
| 1088 | if (isTryBody) |
| 1089 | ExitCXXTryStmt(*cast<CXXTryStmt>(Body), TryInfo); |
| 1090 | } |
| 1091 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1092 | /// EmitDtorEpilogue - Emit all code that comes at the end of class's |
| 1093 | /// destructor. This is to call destructors on members and base classes |
| 1094 | /// in reverse order of their construction. |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1095 | void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD, |
| 1096 | CXXDtorType DtorType) { |
| 1097 | assert(!DD->isTrivial() && |
| 1098 | "Should not emit dtor epilogue for trivial dtor!"); |
| 1099 | |
| 1100 | const CXXRecordDecl *ClassDecl = DD->getParent(); |
| 1101 | |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1102 | // In a deleting destructor, we've already called the complete |
| 1103 | // destructor as a subroutine, so we just have to delete the |
| 1104 | // appropriate value. |
| 1105 | if (DtorType == Dtor_Deleting) { |
| 1106 | assert(DD->getOperatorDelete() && |
| 1107 | "operator delete missing - EmitDtorEpilogue"); |
| 1108 | EmitDeleteCall(DD->getOperatorDelete(), LoadCXXThis(), |
| 1109 | getContext().getTagDeclType(ClassDecl)); |
| 1110 | return; |
| 1111 | } |
| 1112 | |
| 1113 | // For complete destructors, we've already called the base |
| 1114 | // destructor (in GenerateBody), so we just need to destruct all the |
| 1115 | // virtual bases. |
| 1116 | if (DtorType == Dtor_Complete) { |
| 1117 | // Handle virtual bases. |
| 1118 | for (CXXRecordDecl::reverse_base_class_const_iterator I = |
| 1119 | ClassDecl->vbases_rbegin(), E = ClassDecl->vbases_rend(); |
| 1120 | I != E; ++I) { |
| 1121 | const CXXBaseSpecifier &Base = *I; |
| 1122 | CXXRecordDecl *BaseClassDecl |
| 1123 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| 1124 | |
| 1125 | // Ignore trivial destructors. |
| 1126 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1127 | continue; |
| 1128 | const CXXDestructorDecl *D = BaseClassDecl->getDestructor(getContext()); |
| 1129 | llvm::Value *V = GetAddressOfBaseOfCompleteClass(LoadCXXThis(), |
| 1130 | true, |
| 1131 | ClassDecl, |
| 1132 | BaseClassDecl); |
| 1133 | EmitCXXDestructorCall(D, Dtor_Base, V); |
| 1134 | } |
| 1135 | return; |
| 1136 | } |
| 1137 | |
| 1138 | assert(DtorType == Dtor_Base); |
| 1139 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1140 | // Collect the fields. |
| 1141 | llvm::SmallVector<const FieldDecl *, 16> FieldDecls; |
| 1142 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 1143 | E = ClassDecl->field_end(); I != E; ++I) { |
| 1144 | const FieldDecl *Field = *I; |
| 1145 | |
| 1146 | QualType FieldType = getContext().getCanonicalType(Field->getType()); |
| 1147 | FieldType = getContext().getBaseElementType(FieldType); |
| 1148 | |
| 1149 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 1150 | if (!RT) |
| 1151 | continue; |
| 1152 | |
| 1153 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 1154 | if (FieldClassDecl->hasTrivialDestructor()) |
| 1155 | continue; |
| 1156 | |
| 1157 | FieldDecls.push_back(Field); |
| 1158 | } |
| 1159 | |
| 1160 | // Now destroy the fields. |
| 1161 | for (size_t i = FieldDecls.size(); i > 0; --i) { |
| 1162 | const FieldDecl *Field = FieldDecls[i - 1]; |
| 1163 | |
| 1164 | QualType FieldType = Field->getType(); |
| 1165 | const ConstantArrayType *Array = |
| 1166 | getContext().getAsConstantArrayType(FieldType); |
| 1167 | if (Array) |
| 1168 | FieldType = getContext().getBaseElementType(FieldType); |
| 1169 | |
| 1170 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 1171 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 1172 | |
| 1173 | llvm::Value *ThisPtr = LoadCXXThis(); |
| 1174 | |
| 1175 | LValue LHS = EmitLValueForField(ThisPtr, Field, |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1176 | // FIXME: Qualifiers? |
| 1177 | /*CVRQualifiers=*/0); |
| 1178 | if (Array) { |
| 1179 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 1180 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 1181 | llvm::Value *BaseAddrPtr = |
| 1182 | Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
| 1183 | EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()), |
| 1184 | Array, BaseAddrPtr); |
| 1185 | } else |
| 1186 | EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()), |
| 1187 | Dtor_Complete, LHS.getAddress()); |
| 1188 | } |
| 1189 | |
| 1190 | // Destroy non-virtual bases. |
| 1191 | for (CXXRecordDecl::reverse_base_class_const_iterator I = |
| 1192 | ClassDecl->bases_rbegin(), E = ClassDecl->bases_rend(); I != E; ++I) { |
| 1193 | const CXXBaseSpecifier &Base = *I; |
| 1194 | |
| 1195 | // Ignore virtual bases. |
| 1196 | if (Base.isVirtual()) |
| 1197 | continue; |
| 1198 | |
| 1199 | CXXRecordDecl *BaseClassDecl |
| 1200 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| 1201 | |
| 1202 | // Ignore trivial destructors. |
| 1203 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1204 | continue; |
| 1205 | const CXXDestructorDecl *D = BaseClassDecl->getDestructor(getContext()); |
| 1206 | |
| 1207 | llvm::Value *V = GetAddressOfBaseClass(LoadCXXThis(), |
| 1208 | ClassDecl, BaseClassDecl, |
| 1209 | /*NullCheckValue=*/false); |
| 1210 | EmitCXXDestructorCall(D, Dtor_Base, V); |
| 1211 | } |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1214 | /// EmitCXXAggrConstructorCall - This routine essentially creates a (nested) |
| 1215 | /// for-loop to call the default constructor on individual members of the |
| 1216 | /// array. |
| 1217 | /// 'D' is the default constructor for elements of the array, 'ArrayTy' is the |
| 1218 | /// array type and 'ArrayPtr' points to the beginning fo the array. |
| 1219 | /// It is assumed that all relevant checks have been made by the caller. |
| 1220 | void |
| 1221 | CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
| 1222 | const ConstantArrayType *ArrayTy, |
| 1223 | llvm::Value *ArrayPtr, |
| 1224 | CallExpr::const_arg_iterator ArgBeg, |
| 1225 | CallExpr::const_arg_iterator ArgEnd) { |
| 1226 | |
| 1227 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 1228 | llvm::Value * NumElements = |
| 1229 | llvm::ConstantInt::get(SizeTy, |
| 1230 | getContext().getConstantArrayElementCount(ArrayTy)); |
| 1231 | |
| 1232 | EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr, ArgBeg, ArgEnd); |
| 1233 | } |
| 1234 | |
| 1235 | void |
| 1236 | CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
| 1237 | llvm::Value *NumElements, |
| 1238 | llvm::Value *ArrayPtr, |
| 1239 | CallExpr::const_arg_iterator ArgBeg, |
| 1240 | CallExpr::const_arg_iterator ArgEnd) { |
| 1241 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 1242 | |
| 1243 | // Create a temporary for the loop index and initialize it with 0. |
| 1244 | llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index"); |
| 1245 | llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy); |
| 1246 | Builder.CreateStore(Zero, IndexPtr); |
| 1247 | |
| 1248 | // Start the loop with a block that tests the condition. |
| 1249 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 1250 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
| 1251 | |
| 1252 | EmitBlock(CondBlock); |
| 1253 | |
| 1254 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 1255 | |
| 1256 | // Generate: if (loop-index < number-of-elements fall to the loop body, |
| 1257 | // otherwise, go to the block after the for-loop. |
| 1258 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 1259 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless"); |
| 1260 | // If the condition is true, execute the body. |
| 1261 | Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
| 1262 | |
| 1263 | EmitBlock(ForBody); |
| 1264 | |
| 1265 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 1266 | // Inside the loop body, emit the constructor call on the array element. |
| 1267 | Counter = Builder.CreateLoad(IndexPtr); |
| 1268 | llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter, |
| 1269 | "arrayidx"); |
| 1270 | |
| 1271 | // C++ [class.temporary]p4: |
| 1272 | // There are two contexts in which temporaries are destroyed at a different |
| 1273 | // point than the end of the full-expression. The first context is when a |
| 1274 | // default constructor is called to initialize an element of an array. |
| 1275 | // If the constructor has one or more default arguments, the destruction of |
| 1276 | // every temporary created in a default argument expression is sequenced |
| 1277 | // before the construction of the next array element, if any. |
| 1278 | |
| 1279 | // Keep track of the current number of live temporaries. |
Anders Carlsson | 44ec82b | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 1280 | { |
| 1281 | CXXTemporariesCleanupScope Scope(*this); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1282 | |
Anders Carlsson | 44ec82b | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 1283 | EmitCXXConstructorCall(D, Ctor_Complete, Address, ArgBeg, ArgEnd); |
| 1284 | } |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1285 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1286 | EmitBlock(ContinueBlock); |
| 1287 | |
| 1288 | // Emit the increment of the loop counter. |
| 1289 | llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1); |
| 1290 | Counter = Builder.CreateLoad(IndexPtr); |
| 1291 | NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); |
| 1292 | Builder.CreateStore(NextVal, IndexPtr); |
| 1293 | |
| 1294 | // Finally, branch back up to the condition for the next iteration. |
| 1295 | EmitBranch(CondBlock); |
| 1296 | |
| 1297 | // Emit the fall-through block. |
| 1298 | EmitBlock(AfterFor, true); |
| 1299 | } |
| 1300 | |
| 1301 | /// EmitCXXAggrDestructorCall - calls the default destructor on array |
| 1302 | /// elements in reverse order of construction. |
| 1303 | void |
| 1304 | CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, |
| 1305 | const ArrayType *Array, |
| 1306 | llvm::Value *This) { |
| 1307 | const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); |
| 1308 | assert(CA && "Do we support VLA for destruction ?"); |
| 1309 | uint64_t ElementCount = getContext().getConstantArrayElementCount(CA); |
| 1310 | |
| 1311 | const llvm::Type *SizeLTy = ConvertType(getContext().getSizeType()); |
| 1312 | llvm::Value* ElementCountPtr = llvm::ConstantInt::get(SizeLTy, ElementCount); |
| 1313 | EmitCXXAggrDestructorCall(D, ElementCountPtr, This); |
| 1314 | } |
| 1315 | |
| 1316 | /// EmitCXXAggrDestructorCall - calls the default destructor on array |
| 1317 | /// elements in reverse order of construction. |
| 1318 | void |
| 1319 | CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, |
| 1320 | llvm::Value *UpperCount, |
| 1321 | llvm::Value *This) { |
| 1322 | const llvm::Type *SizeLTy = ConvertType(getContext().getSizeType()); |
| 1323 | llvm::Value *One = llvm::ConstantInt::get(SizeLTy, 1); |
| 1324 | |
| 1325 | // Create a temporary for the loop index and initialize it with count of |
| 1326 | // array elements. |
| 1327 | llvm::Value *IndexPtr = CreateTempAlloca(SizeLTy, "loop.index"); |
| 1328 | |
| 1329 | // Store the number of elements in the index pointer. |
| 1330 | Builder.CreateStore(UpperCount, IndexPtr); |
| 1331 | |
| 1332 | // Start the loop with a block that tests the condition. |
| 1333 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 1334 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
| 1335 | |
| 1336 | EmitBlock(CondBlock); |
| 1337 | |
| 1338 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 1339 | |
| 1340 | // Generate: if (loop-index != 0 fall to the loop body, |
| 1341 | // otherwise, go to the block after the for-loop. |
| 1342 | llvm::Value* zeroConstant = |
| 1343 | llvm::Constant::getNullValue(SizeLTy); |
| 1344 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 1345 | llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant, |
| 1346 | "isne"); |
| 1347 | // If the condition is true, execute the body. |
| 1348 | Builder.CreateCondBr(IsNE, ForBody, AfterFor); |
| 1349 | |
| 1350 | EmitBlock(ForBody); |
| 1351 | |
| 1352 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 1353 | // Inside the loop body, emit the constructor call on the array element. |
| 1354 | Counter = Builder.CreateLoad(IndexPtr); |
| 1355 | Counter = Builder.CreateSub(Counter, One); |
| 1356 | llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx"); |
| 1357 | EmitCXXDestructorCall(D, Dtor_Complete, Address); |
| 1358 | |
| 1359 | EmitBlock(ContinueBlock); |
| 1360 | |
| 1361 | // Emit the decrement of the loop counter. |
| 1362 | Counter = Builder.CreateLoad(IndexPtr); |
| 1363 | Counter = Builder.CreateSub(Counter, One, "dec"); |
| 1364 | Builder.CreateStore(Counter, IndexPtr); |
| 1365 | |
| 1366 | // Finally, branch back up to the condition for the next iteration. |
| 1367 | EmitBranch(CondBlock); |
| 1368 | |
| 1369 | // Emit the fall-through block. |
| 1370 | EmitBlock(AfterFor, true); |
| 1371 | } |
| 1372 | |
| 1373 | /// GenerateCXXAggrDestructorHelper - Generates a helper function which when |
| 1374 | /// invoked, calls the default destructor on array elements in reverse order of |
| 1375 | /// construction. |
| 1376 | llvm::Constant * |
| 1377 | CodeGenFunction::GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D, |
| 1378 | const ArrayType *Array, |
| 1379 | llvm::Value *This) { |
| 1380 | FunctionArgList Args; |
| 1381 | ImplicitParamDecl *Dst = |
| 1382 | ImplicitParamDecl::Create(getContext(), 0, |
| 1383 | SourceLocation(), 0, |
| 1384 | getContext().getPointerType(getContext().VoidTy)); |
| 1385 | Args.push_back(std::make_pair(Dst, Dst->getType())); |
| 1386 | |
| 1387 | llvm::SmallString<16> Name; |
| 1388 | llvm::raw_svector_ostream(Name) << "__tcf_" << (++UniqueAggrDestructorCount); |
| 1389 | QualType R = getContext().VoidTy; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1390 | const CGFunctionInfo &FI |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1391 | = CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo()); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1392 | const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false); |
| 1393 | llvm::Function *Fn = |
| 1394 | llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, |
| 1395 | Name.str(), |
| 1396 | &CGM.getModule()); |
| 1397 | IdentifierInfo *II = &CGM.getContext().Idents.get(Name.str()); |
| 1398 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 1399 | getContext().getTranslationUnitDecl(), |
| 1400 | SourceLocation(), II, R, 0, |
| 1401 | FunctionDecl::Static, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1402 | FunctionDecl::None, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1403 | false, true); |
| 1404 | StartFunction(FD, R, Fn, Args, SourceLocation()); |
| 1405 | QualType BaseElementTy = getContext().getBaseElementType(Array); |
| 1406 | const llvm::Type *BasePtr = ConvertType(BaseElementTy); |
| 1407 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 1408 | llvm::Value *BaseAddrPtr = Builder.CreateBitCast(This, BasePtr); |
| 1409 | EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr); |
| 1410 | FinishFunction(); |
| 1411 | llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), |
| 1412 | 0); |
| 1413 | llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); |
| 1414 | return m; |
| 1415 | } |
| 1416 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1417 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1418 | void |
| 1419 | CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, |
| 1420 | CXXCtorType Type, |
| 1421 | llvm::Value *This, |
| 1422 | CallExpr::const_arg_iterator ArgBeg, |
| 1423 | CallExpr::const_arg_iterator ArgEnd) { |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1424 | if (D->isTrivial()) { |
| 1425 | if (ArgBeg == ArgEnd) { |
| 1426 | // Trivial default constructor, no codegen required. |
| 1427 | assert(D->isDefaultConstructor() && |
| 1428 | "trivial 0-arg ctor not a default ctor"); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1429 | return; |
| 1430 | } |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1431 | |
| 1432 | assert(ArgBeg + 1 == ArgEnd && "unexpected argcount for trivial ctor"); |
| 1433 | assert(D->isCopyConstructor() && "trivial 1-arg ctor not a copy ctor"); |
| 1434 | |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1435 | const Expr *E = (*ArgBeg); |
| 1436 | QualType Ty = E->getType(); |
| 1437 | llvm::Value *Src = EmitLValue(E).getAddress(); |
| 1438 | EmitAggregateCopy(This, Src, Ty); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1439 | return; |
| 1440 | } |
| 1441 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1442 | llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(D, Type)); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1443 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type); |
| 1444 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1445 | EmitCXXMemberCall(D, Callee, ReturnValueSlot(), This, VTT, ArgBeg, ArgEnd); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1448 | void |
| 1449 | CodeGenFunction::EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor, |
| 1450 | CXXCtorType CtorType, |
| 1451 | const FunctionArgList &Args) { |
| 1452 | CallArgList DelegateArgs; |
| 1453 | |
| 1454 | FunctionArgList::const_iterator I = Args.begin(), E = Args.end(); |
| 1455 | assert(I != E && "no parameters to constructor"); |
| 1456 | |
| 1457 | // this |
| 1458 | DelegateArgs.push_back(std::make_pair(RValue::get(LoadCXXThis()), |
| 1459 | I->second)); |
| 1460 | ++I; |
| 1461 | |
| 1462 | // vtt |
| 1463 | if (llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(Ctor, CtorType))) { |
| 1464 | QualType VoidPP = getContext().getPointerType(getContext().VoidPtrTy); |
| 1465 | DelegateArgs.push_back(std::make_pair(RValue::get(VTT), VoidPP)); |
| 1466 | |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 1467 | if (CodeGenVTables::needsVTTParameter(CurGD)) { |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1468 | assert(I != E && "cannot skip vtt parameter, already done with args"); |
| 1469 | assert(I->second == VoidPP && "skipping parameter not of vtt type"); |
| 1470 | ++I; |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | // Explicit arguments. |
| 1475 | for (; I != E; ++I) { |
| 1476 | |
| 1477 | const VarDecl *Param = I->first; |
| 1478 | QualType ArgType = Param->getType(); // because we're passing it to itself |
| 1479 | |
| 1480 | // StartFunction converted the ABI-lowered parameter(s) into a |
| 1481 | // local alloca. We need to turn that into an r-value suitable |
| 1482 | // for EmitCall. |
| 1483 | llvm::Value *Local = GetAddrOfLocalVar(Param); |
| 1484 | RValue Arg; |
| 1485 | |
| 1486 | // For the most part, we just need to load the alloca, except: |
| 1487 | // 1) aggregate r-values are actually pointers to temporaries, and |
| 1488 | // 2) references to aggregates are pointers directly to the aggregate. |
| 1489 | // I don't know why references to non-aggregates are different here. |
| 1490 | if (ArgType->isReferenceType()) { |
| 1491 | const ReferenceType *RefType = ArgType->getAs<ReferenceType>(); |
| 1492 | if (hasAggregateLLVMType(RefType->getPointeeType())) |
| 1493 | Arg = RValue::getAggregate(Local); |
| 1494 | else |
| 1495 | // Locals which are references to scalars are represented |
| 1496 | // with allocas holding the pointer. |
| 1497 | Arg = RValue::get(Builder.CreateLoad(Local)); |
| 1498 | } else { |
| 1499 | if (hasAggregateLLVMType(ArgType)) |
| 1500 | Arg = RValue::getAggregate(Local); |
| 1501 | else |
| 1502 | Arg = RValue::get(EmitLoadOfScalar(Local, false, ArgType)); |
| 1503 | } |
| 1504 | |
| 1505 | DelegateArgs.push_back(std::make_pair(Arg, ArgType)); |
| 1506 | } |
| 1507 | |
| 1508 | EmitCall(CGM.getTypes().getFunctionInfo(Ctor, CtorType), |
| 1509 | CGM.GetAddrOfCXXConstructor(Ctor, CtorType), |
| 1510 | ReturnValueSlot(), DelegateArgs, Ctor); |
| 1511 | } |
| 1512 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1513 | void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *DD, |
| 1514 | CXXDtorType Type, |
| 1515 | llvm::Value *This) { |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1516 | llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(DD, Type)); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1517 | llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(DD, Type); |
| 1518 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1519 | EmitCXXMemberCall(DD, Callee, ReturnValueSlot(), This, VTT, 0, 0); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | llvm::Value * |
Anders Carlsson | bb7e17b | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 1523 | CodeGenFunction::GetVirtualBaseClassOffset(llvm::Value *This, |
| 1524 | const CXXRecordDecl *ClassDecl, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1525 | const CXXRecordDecl *BaseClassDecl) { |
| 1526 | const llvm::Type *Int8PtrTy = |
| 1527 | llvm::Type::getInt8Ty(VMContext)->getPointerTo(); |
| 1528 | |
| 1529 | llvm::Value *VTablePtr = Builder.CreateBitCast(This, |
| 1530 | Int8PtrTy->getPointerTo()); |
| 1531 | VTablePtr = Builder.CreateLoad(VTablePtr, "vtable"); |
| 1532 | |
Anders Carlsson | bba1607 | 2010-03-11 07:15:17 +0000 | [diff] [blame] | 1533 | int64_t VBaseOffsetOffset = |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 1534 | CGM.getVTables().getVirtualBaseOffsetOffset(ClassDecl, BaseClassDecl); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1535 | |
| 1536 | llvm::Value *VBaseOffsetPtr = |
Anders Carlsson | bba1607 | 2010-03-11 07:15:17 +0000 | [diff] [blame] | 1537 | Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetOffset, "vbase.offset.ptr"); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1538 | const llvm::Type *PtrDiffTy = |
| 1539 | ConvertType(getContext().getPointerDiffType()); |
| 1540 | |
| 1541 | VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr, |
| 1542 | PtrDiffTy->getPointerTo()); |
| 1543 | |
| 1544 | llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset"); |
| 1545 | |
| 1546 | return VBaseOffset; |
| 1547 | } |
| 1548 | |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1549 | void |
| 1550 | CodeGenFunction::InitializeVTablePointer(BaseSubobject Base, |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1551 | const CXXRecordDecl *NearestVBase, |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1552 | llvm::Constant *VTable, |
| 1553 | const CXXRecordDecl *VTableClass) { |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1554 | const CXXRecordDecl *RD = Base.getBase(); |
| 1555 | |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1556 | // Compute the address point. |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1557 | llvm::Value *VTableAddressPoint; |
Anders Carlsson | 851853d | 2010-03-29 02:38:51 +0000 | [diff] [blame] | 1558 | |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1559 | // Check if we need to use a vtable from the VTT. |
Anders Carlsson | 851853d | 2010-03-29 02:38:51 +0000 | [diff] [blame] | 1560 | if (CodeGenVTables::needsVTTParameter(CurGD) && |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1561 | (RD->getNumVBases() || NearestVBase)) { |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1562 | // Get the secondary vpointer index. |
| 1563 | uint64_t VirtualPointerIndex = |
| 1564 | CGM.getVTables().getSecondaryVirtualPointerIndex(VTableClass, Base); |
| 1565 | |
| 1566 | /// Load the VTT. |
| 1567 | llvm::Value *VTT = LoadCXXVTT(); |
| 1568 | if (VirtualPointerIndex) |
| 1569 | VTT = Builder.CreateConstInBoundsGEP1_64(VTT, VirtualPointerIndex); |
| 1570 | |
| 1571 | // And load the address point from the VTT. |
| 1572 | VTableAddressPoint = Builder.CreateLoad(VTT); |
| 1573 | } else { |
Anders Carlsson | 64c9eca | 2010-03-29 02:08:26 +0000 | [diff] [blame] | 1574 | uint64_t AddressPoint = CGM.getVTables().getAddressPoint(Base, VTableClass); |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1575 | VTableAddressPoint = |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1576 | Builder.CreateConstInBoundsGEP2_64(VTable, 0, AddressPoint); |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1577 | } |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1578 | |
Anders Carlsson | 36fd6be | 2010-04-20 16:22:16 +0000 | [diff] [blame] | 1579 | // Compute where to store the address point. |
Anders Carlsson | 3e79c30 | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 1580 | llvm::Value *VTableField; |
| 1581 | |
| 1582 | if (CodeGenVTables::needsVTTParameter(CurGD) && NearestVBase) { |
| 1583 | // We need to use the virtual base offset offset because the virtual base |
| 1584 | // might have a different offset in the most derived class. |
| 1585 | VTableField = GetAddressOfBaseClass(LoadCXXThis(), VTableClass, RD, |
| 1586 | /*NullCheckValue=*/false); |
| 1587 | } else { |
| 1588 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); |
Anders Carlsson | 36fd6be | 2010-04-20 16:22:16 +0000 | [diff] [blame] | 1589 | |
Anders Carlsson | 3e79c30 | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 1590 | VTableField = Builder.CreateBitCast(LoadCXXThis(), Int8PtrTy); |
Anders Carlsson | 36fd6be | 2010-04-20 16:22:16 +0000 | [diff] [blame] | 1591 | VTableField = |
Anders Carlsson | 3e79c30 | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 1592 | Builder.CreateConstInBoundsGEP1_64(VTableField, Base.getBaseOffset() / 8); |
| 1593 | } |
Anders Carlsson | 36fd6be | 2010-04-20 16:22:16 +0000 | [diff] [blame] | 1594 | |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1595 | // Finally, store the address point. |
| 1596 | const llvm::Type *AddressPointPtrTy = |
| 1597 | VTableAddressPoint->getType()->getPointerTo(); |
| 1598 | VTableField = Builder.CreateBitCast(VTableField, AddressPointPtrTy); |
| 1599 | Builder.CreateStore(VTableAddressPoint, VTableField); |
| 1600 | } |
| 1601 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1602 | void |
| 1603 | CodeGenFunction::InitializeVTablePointers(BaseSubobject Base, |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1604 | const CXXRecordDecl *NearestVBase, |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1605 | bool BaseIsNonVirtualPrimaryBase, |
| 1606 | llvm::Constant *VTable, |
| 1607 | const CXXRecordDecl *VTableClass, |
| 1608 | VisitedVirtualBasesSetTy& VBases) { |
| 1609 | // If this base is a non-virtual primary base the address point has already |
| 1610 | // been set. |
| 1611 | if (!BaseIsNonVirtualPrimaryBase) { |
| 1612 | // Initialize the vtable pointer for this base. |
Anders Carlsson | 3e79c30 | 2010-04-20 18:05:10 +0000 | [diff] [blame] | 1613 | InitializeVTablePointer(Base, NearestVBase, VTable, VTableClass); |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | const CXXRecordDecl *RD = Base.getBase(); |
| 1617 | |
| 1618 | // Traverse bases. |
| 1619 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1620 | E = RD->bases_end(); I != E; ++I) { |
| 1621 | CXXRecordDecl *BaseDecl |
| 1622 | = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1623 | |
| 1624 | // Ignore classes without a vtable. |
| 1625 | if (!BaseDecl->isDynamicClass()) |
| 1626 | continue; |
| 1627 | |
| 1628 | uint64_t BaseOffset; |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1629 | bool BaseDeclIsNonVirtualPrimaryBase; |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1630 | |
| 1631 | if (I->isVirtual()) { |
| 1632 | // Check if we've visited this virtual base before. |
| 1633 | if (!VBases.insert(BaseDecl)) |
| 1634 | continue; |
| 1635 | |
| 1636 | const ASTRecordLayout &Layout = |
| 1637 | getContext().getASTRecordLayout(VTableClass); |
| 1638 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1639 | BaseOffset = Layout.getVBaseClassOffset(BaseDecl); |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1640 | BaseDeclIsNonVirtualPrimaryBase = false; |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1641 | } else { |
| 1642 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 1643 | |
| 1644 | BaseOffset = Base.getBaseOffset() + Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1645 | BaseDeclIsNonVirtualPrimaryBase = Layout.getPrimaryBase() == BaseDecl; |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | InitializeVTablePointers(BaseSubobject(BaseDecl, BaseOffset), |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1649 | I->isVirtual() ? BaseDecl : NearestVBase, |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1650 | BaseDeclIsNonVirtualPrimaryBase, |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1651 | VTable, VTableClass, VBases); |
| 1652 | } |
| 1653 | } |
| 1654 | |
| 1655 | void CodeGenFunction::InitializeVTablePointers(const CXXRecordDecl *RD) { |
| 1656 | // Ignore classes without a vtable. |
Anders Carlsson | 0703690 | 2010-03-26 04:39:42 +0000 | [diff] [blame] | 1657 | if (!RD->isDynamicClass()) |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1658 | return; |
| 1659 | |
Anders Carlsson | 0703690 | 2010-03-26 04:39:42 +0000 | [diff] [blame] | 1660 | // Get the VTable. |
| 1661 | llvm::Constant *VTable = CGM.getVTables().GetAddrOfVTable(RD); |
Anders Carlsson | 5c6c1d9 | 2010-03-24 03:57:14 +0000 | [diff] [blame] | 1662 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1663 | // Initialize the vtable pointers for this class and all of its bases. |
| 1664 | VisitedVirtualBasesSetTy VBases; |
Anders Carlsson | b3b772e | 2010-04-20 05:22:15 +0000 | [diff] [blame] | 1665 | InitializeVTablePointers(BaseSubobject(RD, 0), /*NearestVBase=*/0, |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1666 | /*BaseIsNonVirtualPrimaryBase=*/false, |
| 1667 | VTable, RD, VBases); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1668 | } |