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