Anders Carlsson | 5b95592 | 2009-11-24 05:51:11 +0000 | [diff] [blame] | 1 | //===--- CGClass.cpp - Emit LLVM Code for C++ classes ---------------------===// |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code dealing with C++ code generation of classes |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 15 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 16 | #include "clang/AST/RecordLayout.h" |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 17 | |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | using namespace CodeGen; |
| 20 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 21 | static uint64_t |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 22 | ComputeNonVirtualBaseClassOffset(ASTContext &Context, |
| 23 | const CXXBasePath &Path, |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 24 | unsigned Start) { |
| 25 | uint64_t Offset = 0; |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 26 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 27 | for (unsigned i = Start, e = Path.size(); i != e; ++i) { |
| 28 | const CXXBasePathElement& Element = Path[i]; |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 29 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 30 | // Get the layout. |
| 31 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Element.Class); |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 32 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 33 | const CXXBaseSpecifier *BS = Element.Base; |
| 34 | assert(!BS->isVirtual() && "Should not see virtual bases here!"); |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 35 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 36 | const CXXRecordDecl *Base = |
| 37 | cast<CXXRecordDecl>(BS->getType()->getAs<RecordType>()->getDecl()); |
| 38 | |
| 39 | // Add the offset. |
| 40 | Offset += Layout.getBaseClassOffset(Base) / 8; |
| 41 | } |
| 42 | |
| 43 | return Offset; |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Anders Carlsson | 84080ec | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 46 | llvm::Constant * |
Anders Carlsson | bb7e17b | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 47 | CodeGenModule::GetNonVirtualBaseClassOffset(const CXXRecordDecl *Class, |
| 48 | const CXXRecordDecl *BaseClass) { |
| 49 | if (Class == BaseClass) |
Anders Carlsson | 84080ec | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 50 | return 0; |
| 51 | |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 52 | CXXBasePaths Paths(/*FindAmbiguities=*/false, |
| 53 | /*RecordPaths=*/true, /*DetectVirtual=*/false); |
Anders Carlsson | bb7e17b | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 54 | if (!const_cast<CXXRecordDecl *>(Class)-> |
| 55 | isDerivedFrom(const_cast<CXXRecordDecl *>(BaseClass), Paths)) { |
Anders Carlsson | 2f1986b | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 56 | assert(false && "Class must be derived from the passed in base class!"); |
| 57 | return 0; |
| 58 | } |
Anders Carlsson | 84080ec | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 59 | |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 60 | uint64_t Offset = ComputeNonVirtualBaseClassOffset(getContext(), |
| 61 | Paths.front(), 0); |
Anders Carlsson | 84080ec | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 62 | if (!Offset) |
| 63 | return 0; |
| 64 | |
Anders Carlsson | 2b35835 | 2009-10-03 14:56:57 +0000 | [diff] [blame] | 65 | const llvm::Type *PtrDiffTy = |
| 66 | Types.ConvertType(getContext().getPointerDiffType()); |
Anders Carlsson | 84080ec | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 67 | |
| 68 | return llvm::ConstantInt::get(PtrDiffTy, Offset); |
| 69 | } |
| 70 | |
Anders Carlsson | 9fcfc42 | 2009-12-03 03:06:55 +0000 | [diff] [blame] | 71 | // FIXME: This probably belongs in CGVtable, but it relies on |
| 72 | // the static function ComputeNonVirtualBaseClassOffset, so we should make that |
| 73 | // a CodeGenModule member function as well. |
| 74 | ThunkAdjustment |
| 75 | CodeGenModule::ComputeThunkAdjustment(const CXXRecordDecl *ClassDecl, |
| 76 | const CXXRecordDecl *BaseClassDecl) { |
| 77 | CXXBasePaths Paths(/*FindAmbiguities=*/false, |
| 78 | /*RecordPaths=*/true, /*DetectVirtual=*/false); |
| 79 | if (!const_cast<CXXRecordDecl *>(ClassDecl)-> |
| 80 | isDerivedFrom(const_cast<CXXRecordDecl *>(BaseClassDecl), Paths)) { |
| 81 | assert(false && "Class must be derived from the passed in base class!"); |
| 82 | return ThunkAdjustment(); |
| 83 | } |
| 84 | |
| 85 | unsigned Start = 0; |
| 86 | uint64_t VirtualOffset = 0; |
| 87 | |
| 88 | const CXXBasePath &Path = Paths.front(); |
| 89 | const CXXRecordDecl *VBase = 0; |
| 90 | for (unsigned i = 0, e = Path.size(); i != e; ++i) { |
| 91 | const CXXBasePathElement& Element = Path[i]; |
| 92 | if (Element.Base->isVirtual()) { |
| 93 | Start = i+1; |
| 94 | QualType VBaseType = Element.Base->getType(); |
| 95 | VBase = cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl()); |
| 96 | } |
| 97 | } |
| 98 | if (VBase) |
| 99 | VirtualOffset = |
| 100 | getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl); |
| 101 | |
| 102 | uint64_t Offset = |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 103 | ComputeNonVirtualBaseClassOffset(getContext(), Paths.front(), Start); |
Anders Carlsson | 9fcfc42 | 2009-12-03 03:06:55 +0000 | [diff] [blame] | 104 | return ThunkAdjustment(Offset, VirtualOffset); |
| 105 | } |
| 106 | |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 107 | /// Gets the address of a virtual base class within a complete object. |
| 108 | /// This should only be used for (1) non-virtual bases or (2) virtual bases |
| 109 | /// when the type is known to be complete (e.g. in complete destructors). |
| 110 | /// |
| 111 | /// The object pointed to by 'This' is assumed to be non-null. |
| 112 | llvm::Value * |
| 113 | CodeGenFunction::GetAddressOfBaseOfCompleteClass(llvm::Value *This, |
| 114 | bool isBaseVirtual, |
| 115 | const CXXRecordDecl *Derived, |
| 116 | const CXXRecordDecl *Base) { |
| 117 | // 'this' must be a pointer (in some address space) to Derived. |
| 118 | assert(This->getType()->isPointerTy() && |
| 119 | cast<llvm::PointerType>(This->getType())->getElementType() |
| 120 | == ConvertType(Derived)); |
| 121 | |
| 122 | // Compute the offset of the virtual base. |
| 123 | uint64_t Offset; |
| 124 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Derived); |
| 125 | if (isBaseVirtual) |
| 126 | Offset = Layout.getVBaseClassOffset(Base); |
| 127 | else |
| 128 | Offset = Layout.getBaseClassOffset(Base); |
| 129 | |
| 130 | // Shift and cast down to the base type. |
| 131 | // TODO: for complete types, this should be possible with a GEP. |
| 132 | llvm::Value *V = This; |
| 133 | if (Offset) { |
| 134 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext()); |
| 135 | V = Builder.CreateBitCast(V, Int8PtrTy); |
| 136 | V = Builder.CreateConstInBoundsGEP1_64(V, Offset / 8); |
| 137 | } |
| 138 | V = Builder.CreateBitCast(V, ConvertType(Base)->getPointerTo()); |
| 139 | |
| 140 | return V; |
| 141 | } |
| 142 | |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 143 | llvm::Value * |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 144 | CodeGenFunction::GetAddressOfBaseClass(llvm::Value *Value, |
Anders Carlsson | bb7e17b | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 145 | const CXXRecordDecl *Class, |
| 146 | const CXXRecordDecl *BaseClass, |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 147 | bool NullCheckValue) { |
Anders Carlsson | dfd0330 | 2009-09-22 21:58:22 +0000 | [diff] [blame] | 148 | QualType BTy = |
| 149 | getContext().getCanonicalType( |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 150 | getContext().getTypeDeclType(BaseClass)); |
Anders Carlsson | dfd0330 | 2009-09-22 21:58:22 +0000 | [diff] [blame] | 151 | const llvm::Type *BasePtrTy = llvm::PointerType::getUnqual(ConvertType(BTy)); |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 152 | |
Anders Carlsson | bb7e17b | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 153 | if (Class == BaseClass) { |
Anders Carlsson | dfd0330 | 2009-09-22 21:58:22 +0000 | [diff] [blame] | 154 | // Just cast back. |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 155 | return Builder.CreateBitCast(Value, BasePtrTy); |
Anders Carlsson | dfd0330 | 2009-09-22 21:58:22 +0000 | [diff] [blame] | 156 | } |
Anders Carlsson | 905a100 | 2010-01-31 02:39:02 +0000 | [diff] [blame] | 157 | |
| 158 | CXXBasePaths Paths(/*FindAmbiguities=*/false, |
| 159 | /*RecordPaths=*/true, /*DetectVirtual=*/false); |
| 160 | if (!const_cast<CXXRecordDecl *>(Class)-> |
| 161 | isDerivedFrom(const_cast<CXXRecordDecl *>(BaseClass), Paths)) { |
| 162 | assert(false && "Class must be derived from the passed in base class!"); |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | unsigned Start = 0; |
| 167 | llvm::Value *VirtualOffset = 0; |
| 168 | |
| 169 | const CXXBasePath &Path = Paths.front(); |
| 170 | const CXXRecordDecl *VBase = 0; |
| 171 | for (unsigned i = 0, e = Path.size(); i != e; ++i) { |
| 172 | const CXXBasePathElement& Element = Path[i]; |
| 173 | if (Element.Base->isVirtual()) { |
| 174 | Start = i+1; |
| 175 | QualType VBaseType = Element.Base->getType(); |
| 176 | VBase = cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl()); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | uint64_t Offset = |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 181 | ComputeNonVirtualBaseClassOffset(getContext(), Paths.front(), Start); |
Eli Friedman | 4a5dc24 | 2009-11-10 22:48:10 +0000 | [diff] [blame] | 182 | |
Anders Carlsson | 905a100 | 2010-01-31 02:39:02 +0000 | [diff] [blame] | 183 | if (!Offset && !VBase) { |
| 184 | // Just cast back. |
| 185 | return Builder.CreateBitCast(Value, BasePtrTy); |
| 186 | } |
| 187 | |
Anders Carlsson | 32baf62 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 188 | llvm::BasicBlock *CastNull = 0; |
| 189 | llvm::BasicBlock *CastNotNull = 0; |
| 190 | llvm::BasicBlock *CastEnd = 0; |
| 191 | |
| 192 | if (NullCheckValue) { |
| 193 | CastNull = createBasicBlock("cast.null"); |
| 194 | CastNotNull = createBasicBlock("cast.notnull"); |
| 195 | CastEnd = createBasicBlock("cast.end"); |
| 196 | |
| 197 | llvm::Value *IsNull = |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 198 | Builder.CreateICmpEQ(Value, |
| 199 | llvm::Constant::getNullValue(Value->getType())); |
Anders Carlsson | 32baf62 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 200 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
| 201 | EmitBlock(CastNotNull); |
| 202 | } |
| 203 | |
Anders Carlsson | 905a100 | 2010-01-31 02:39:02 +0000 | [diff] [blame] | 204 | if (VBase) |
| 205 | VirtualOffset = GetVirtualBaseClassOffset(Value, Class, VBase); |
Eli Friedman | 4a5dc24 | 2009-11-10 22:48:10 +0000 | [diff] [blame] | 206 | |
Anders Carlsson | 905a100 | 2010-01-31 02:39:02 +0000 | [diff] [blame] | 207 | const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType()); |
| 208 | llvm::Value *NonVirtualOffset = 0; |
| 209 | if (Offset) |
| 210 | NonVirtualOffset = llvm::ConstantInt::get(PtrDiffTy, Offset); |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 211 | |
Anders Carlsson | 905a100 | 2010-01-31 02:39:02 +0000 | [diff] [blame] | 212 | llvm::Value *BaseOffset; |
| 213 | if (VBase) { |
| 214 | if (NonVirtualOffset) |
| 215 | BaseOffset = Builder.CreateAdd(VirtualOffset, NonVirtualOffset); |
| 216 | else |
| 217 | BaseOffset = VirtualOffset; |
| 218 | } else |
| 219 | BaseOffset = NonVirtualOffset; |
| 220 | |
| 221 | // Apply the base offset. |
| 222 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext()); |
| 223 | Value = Builder.CreateBitCast(Value, Int8PtrTy); |
| 224 | Value = Builder.CreateGEP(Value, BaseOffset, "add.ptr"); |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 225 | |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 226 | // Cast back. |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 227 | Value = Builder.CreateBitCast(Value, BasePtrTy); |
Anders Carlsson | 32baf62 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 228 | |
| 229 | if (NullCheckValue) { |
| 230 | Builder.CreateBr(CastEnd); |
| 231 | EmitBlock(CastNull); |
| 232 | Builder.CreateBr(CastEnd); |
| 233 | EmitBlock(CastEnd); |
| 234 | |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 235 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType()); |
Anders Carlsson | 32baf62 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 236 | PHI->reserveOperandSpace(2); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 237 | PHI->addIncoming(Value, CastNotNull); |
| 238 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), |
Anders Carlsson | 32baf62 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 239 | CastNull); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 240 | Value = PHI; |
Anders Carlsson | 32baf62 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 241 | } |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 242 | |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 243 | return Value; |
| 244 | } |
| 245 | |
| 246 | llvm::Value * |
| 247 | CodeGenFunction::GetAddressOfDerivedClass(llvm::Value *Value, |
Anders Carlsson | bb7e17b | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 248 | const CXXRecordDecl *Class, |
| 249 | const CXXRecordDecl *DerivedClass, |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 250 | bool NullCheckValue) { |
| 251 | QualType DerivedTy = |
| 252 | getContext().getCanonicalType( |
Anders Carlsson | bb7e17b | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 253 | getContext().getTypeDeclType(const_cast<CXXRecordDecl*>(DerivedClass))); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 254 | const llvm::Type *DerivedPtrTy = ConvertType(DerivedTy)->getPointerTo(); |
| 255 | |
Anders Carlsson | bb7e17b | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 256 | if (Class == DerivedClass) { |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 257 | // Just cast back. |
| 258 | return Builder.CreateBitCast(Value, DerivedPtrTy); |
| 259 | } |
| 260 | |
Anders Carlsson | a552ea7 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 261 | llvm::Value *NonVirtualOffset = |
| 262 | CGM.GetNonVirtualBaseClassOffset(DerivedClass, Class); |
| 263 | |
| 264 | if (!NonVirtualOffset) { |
| 265 | // No offset, we can just cast back. |
| 266 | return Builder.CreateBitCast(Value, DerivedPtrTy); |
| 267 | } |
| 268 | |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 269 | llvm::BasicBlock *CastNull = 0; |
| 270 | llvm::BasicBlock *CastNotNull = 0; |
| 271 | llvm::BasicBlock *CastEnd = 0; |
| 272 | |
| 273 | if (NullCheckValue) { |
| 274 | CastNull = createBasicBlock("cast.null"); |
| 275 | CastNotNull = createBasicBlock("cast.notnull"); |
| 276 | CastEnd = createBasicBlock("cast.end"); |
| 277 | |
| 278 | llvm::Value *IsNull = |
| 279 | Builder.CreateICmpEQ(Value, |
| 280 | llvm::Constant::getNullValue(Value->getType())); |
| 281 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
| 282 | EmitBlock(CastNotNull); |
| 283 | } |
| 284 | |
Anders Carlsson | a552ea7 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 285 | // Apply the offset. |
| 286 | Value = Builder.CreatePtrToInt(Value, NonVirtualOffset->getType()); |
| 287 | Value = Builder.CreateSub(Value, NonVirtualOffset); |
| 288 | Value = Builder.CreateIntToPtr(Value, DerivedPtrTy); |
| 289 | |
| 290 | // Just cast. |
| 291 | Value = Builder.CreateBitCast(Value, DerivedPtrTy); |
Anders Carlsson | a3697c9 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 292 | |
| 293 | if (NullCheckValue) { |
| 294 | Builder.CreateBr(CastEnd); |
| 295 | EmitBlock(CastNull); |
| 296 | Builder.CreateBr(CastEnd); |
| 297 | EmitBlock(CastEnd); |
| 298 | |
| 299 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType()); |
| 300 | PHI->reserveOperandSpace(2); |
| 301 | PHI->addIncoming(Value, CastNotNull); |
| 302 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), |
| 303 | CastNull); |
| 304 | Value = PHI; |
| 305 | } |
| 306 | |
| 307 | return Value; |
Anders Carlsson | 5d58a1d | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 308 | } |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 309 | |
| 310 | /// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class |
| 311 | /// array of objects from SrcValue to DestValue. Copying can be either a bitwise |
| 312 | /// copy or via a copy constructor call. |
| 313 | // FIXME. Consolidate this with EmitCXXAggrConstructorCall. |
| 314 | void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest, |
| 315 | llvm::Value *Src, |
| 316 | const ArrayType *Array, |
| 317 | const CXXRecordDecl *BaseClassDecl, |
| 318 | QualType Ty) { |
| 319 | const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); |
| 320 | assert(CA && "VLA cannot be copied over"); |
| 321 | bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor(); |
| 322 | |
| 323 | // Create a temporary for the loop index and initialize it with 0. |
| 324 | llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext), |
| 325 | "loop.index"); |
| 326 | llvm::Value* zeroConstant = |
| 327 | llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)); |
| 328 | Builder.CreateStore(zeroConstant, IndexPtr); |
| 329 | // Start the loop with a block that tests the condition. |
| 330 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 331 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
| 332 | |
| 333 | EmitBlock(CondBlock); |
| 334 | |
| 335 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 336 | // Generate: if (loop-index < number-of-elements fall to the loop body, |
| 337 | // otherwise, go to the block after the for-loop. |
| 338 | uint64_t NumElements = getContext().getConstantArrayElementCount(CA); |
| 339 | llvm::Value * NumElementsPtr = |
| 340 | llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements); |
| 341 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 342 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr, |
| 343 | "isless"); |
| 344 | // If the condition is true, execute the body. |
| 345 | Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
| 346 | |
| 347 | EmitBlock(ForBody); |
| 348 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 349 | // Inside the loop body, emit the constructor call on the array element. |
| 350 | Counter = Builder.CreateLoad(IndexPtr); |
| 351 | Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress"); |
| 352 | Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress"); |
| 353 | if (BitwiseCopy) |
| 354 | EmitAggregateCopy(Dest, Src, Ty); |
| 355 | else if (CXXConstructorDecl *BaseCopyCtor = |
| 356 | BaseClassDecl->getCopyConstructor(getContext(), 0)) { |
| 357 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor, |
| 358 | Ctor_Complete); |
| 359 | CallArgList CallArgs; |
| 360 | // Push the this (Dest) ptr. |
| 361 | CallArgs.push_back(std::make_pair(RValue::get(Dest), |
| 362 | BaseCopyCtor->getThisType(getContext()))); |
| 363 | |
| 364 | // Push the Src ptr. |
| 365 | CallArgs.push_back(std::make_pair(RValue::get(Src), |
| 366 | BaseCopyCtor->getParamDecl(0)->getType())); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 367 | const FunctionProtoType *FPT |
| 368 | = BaseCopyCtor->getType()->getAs<FunctionProtoType>(); |
| 369 | EmitCall(CGM.getTypes().getFunctionInfo(CallArgs, FPT), |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 370 | Callee, ReturnValueSlot(), CallArgs, BaseCopyCtor); |
| 371 | } |
| 372 | EmitBlock(ContinueBlock); |
| 373 | |
| 374 | // Emit the increment of the loop counter. |
| 375 | llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); |
| 376 | Counter = Builder.CreateLoad(IndexPtr); |
| 377 | NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); |
| 378 | Builder.CreateStore(NextVal, IndexPtr); |
| 379 | |
| 380 | // Finally, branch back up to the condition for the next iteration. |
| 381 | EmitBranch(CondBlock); |
| 382 | |
| 383 | // Emit the fall-through block. |
| 384 | EmitBlock(AfterFor, true); |
| 385 | } |
| 386 | |
| 387 | /// EmitClassAggrCopyAssignment - This routine generates code to assign a class |
| 388 | /// array of objects from SrcValue to DestValue. Assignment can be either a |
| 389 | /// bitwise assignment or via a copy assignment operator function call. |
| 390 | /// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy |
| 391 | void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest, |
| 392 | llvm::Value *Src, |
| 393 | const ArrayType *Array, |
| 394 | const CXXRecordDecl *BaseClassDecl, |
| 395 | QualType Ty) { |
| 396 | const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); |
| 397 | assert(CA && "VLA cannot be asssigned"); |
| 398 | bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment(); |
| 399 | |
| 400 | // Create a temporary for the loop index and initialize it with 0. |
| 401 | llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext), |
| 402 | "loop.index"); |
| 403 | llvm::Value* zeroConstant = |
| 404 | llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)); |
| 405 | Builder.CreateStore(zeroConstant, IndexPtr); |
| 406 | // Start the loop with a block that tests the condition. |
| 407 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 408 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
| 409 | |
| 410 | EmitBlock(CondBlock); |
| 411 | |
| 412 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 413 | // Generate: if (loop-index < number-of-elements fall to the loop body, |
| 414 | // otherwise, go to the block after the for-loop. |
| 415 | uint64_t NumElements = getContext().getConstantArrayElementCount(CA); |
| 416 | llvm::Value * NumElementsPtr = |
| 417 | llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements); |
| 418 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 419 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr, |
| 420 | "isless"); |
| 421 | // If the condition is true, execute the body. |
| 422 | Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
| 423 | |
| 424 | EmitBlock(ForBody); |
| 425 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 426 | // Inside the loop body, emit the assignment operator call on array element. |
| 427 | Counter = Builder.CreateLoad(IndexPtr); |
| 428 | Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress"); |
| 429 | Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress"); |
| 430 | const CXXMethodDecl *MD = 0; |
| 431 | if (BitwiseAssign) |
| 432 | EmitAggregateCopy(Dest, Src, Ty); |
| 433 | else { |
Eli Friedman | 8a850ba | 2010-01-15 20:06:11 +0000 | [diff] [blame] | 434 | BaseClassDecl->hasConstCopyAssignment(getContext(), MD); |
| 435 | assert(MD && "EmitClassAggrCopyAssignment - No user assign"); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 436 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 437 | const llvm::Type *LTy = |
| 438 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 439 | FPT->isVariadic()); |
| 440 | llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy); |
| 441 | |
| 442 | CallArgList CallArgs; |
| 443 | // Push the this (Dest) ptr. |
| 444 | CallArgs.push_back(std::make_pair(RValue::get(Dest), |
| 445 | MD->getThisType(getContext()))); |
| 446 | |
| 447 | // Push the Src ptr. |
Eli Friedman | 8a850ba | 2010-01-15 20:06:11 +0000 | [diff] [blame] | 448 | QualType SrcTy = MD->getParamDecl(0)->getType(); |
| 449 | RValue SrcValue = SrcTy->isReferenceType() ? RValue::get(Src) : |
| 450 | RValue::getAggregate(Src); |
| 451 | CallArgs.push_back(std::make_pair(SrcValue, SrcTy)); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 452 | EmitCall(CGM.getTypes().getFunctionInfo(CallArgs, FPT), |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 453 | Callee, ReturnValueSlot(), CallArgs, MD); |
| 454 | } |
| 455 | EmitBlock(ContinueBlock); |
| 456 | |
| 457 | // Emit the increment of the loop counter. |
| 458 | llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); |
| 459 | Counter = Builder.CreateLoad(IndexPtr); |
| 460 | NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); |
| 461 | Builder.CreateStore(NextVal, IndexPtr); |
| 462 | |
| 463 | // Finally, branch back up to the condition for the next iteration. |
| 464 | EmitBranch(CondBlock); |
| 465 | |
| 466 | // Emit the fall-through block. |
| 467 | EmitBlock(AfterFor, true); |
| 468 | } |
| 469 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 470 | /// GetVTTParameter - Return the VTT parameter that should be passed to a |
| 471 | /// base constructor/destructor with virtual bases. |
| 472 | static llvm::Value *GetVTTParameter(CodeGenFunction &CGF, GlobalDecl GD) { |
| 473 | if (!CGVtableInfo::needsVTTParameter(GD)) { |
| 474 | // This constructor/destructor does not need a VTT parameter. |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | const CXXRecordDecl *RD = cast<CXXMethodDecl>(CGF.CurFuncDecl)->getParent(); |
| 479 | const CXXRecordDecl *Base = cast<CXXMethodDecl>(GD.getDecl())->getParent(); |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame^] | 480 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 481 | llvm::Value *VTT; |
| 482 | |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame^] | 483 | uint64_t SubVTTIndex; |
| 484 | |
| 485 | // If the record matches the base, this is the complete ctor/dtor |
| 486 | // variant calling the base variant in a class with virtual bases. |
| 487 | if (RD == Base) { |
| 488 | assert(!CGVtableInfo::needsVTTParameter(CGF.CurGD) && |
| 489 | "doing no-op VTT offset in base dtor/ctor?"); |
| 490 | SubVTTIndex = 0; |
| 491 | } else { |
| 492 | SubVTTIndex = CGF.CGM.getVtableInfo().getSubVTTIndex(RD, Base); |
| 493 | assert(SubVTTIndex != 0 && "Sub-VTT index must be greater than zero!"); |
| 494 | } |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 495 | |
| 496 | if (CGVtableInfo::needsVTTParameter(CGF.CurGD)) { |
| 497 | // A VTT parameter was passed to the constructor, use it. |
| 498 | VTT = CGF.LoadCXXVTT(); |
| 499 | VTT = CGF.Builder.CreateConstInBoundsGEP1_64(VTT, SubVTTIndex); |
| 500 | } else { |
| 501 | // We're the complete constructor, so get the VTT by name. |
| 502 | VTT = CGF.CGM.getVtableInfo().getVTT(RD); |
| 503 | VTT = CGF.Builder.CreateConstInBoundsGEP2_64(VTT, 0, SubVTTIndex); |
| 504 | } |
| 505 | |
| 506 | return VTT; |
| 507 | } |
| 508 | |
| 509 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 510 | /// EmitClassMemberwiseCopy - This routine generates code to copy a class |
| 511 | /// object from SrcValue to DestValue. Copying can be either a bitwise copy |
| 512 | /// or via a copy constructor call. |
| 513 | void CodeGenFunction::EmitClassMemberwiseCopy( |
| 514 | llvm::Value *Dest, llvm::Value *Src, |
| 515 | const CXXRecordDecl *ClassDecl, |
| 516 | const CXXRecordDecl *BaseClassDecl, QualType Ty) { |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 517 | CXXCtorType CtorType = Ctor_Complete; |
| 518 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 519 | if (ClassDecl) { |
| 520 | Dest = GetAddressOfBaseClass(Dest, ClassDecl, BaseClassDecl, |
| 521 | /*NullCheckValue=*/false); |
| 522 | Src = GetAddressOfBaseClass(Src, ClassDecl, BaseClassDecl, |
| 523 | /*NullCheckValue=*/false); |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 524 | |
| 525 | // We want to call the base constructor. |
| 526 | CtorType = Ctor_Base; |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 527 | } |
| 528 | if (BaseClassDecl->hasTrivialCopyConstructor()) { |
| 529 | EmitAggregateCopy(Dest, Src, Ty); |
| 530 | return; |
| 531 | } |
| 532 | |
| 533 | if (CXXConstructorDecl *BaseCopyCtor = |
| 534 | BaseClassDecl->getCopyConstructor(getContext(), 0)) { |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 535 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor, CtorType); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 536 | CallArgList CallArgs; |
| 537 | // Push the this (Dest) ptr. |
| 538 | CallArgs.push_back(std::make_pair(RValue::get(Dest), |
| 539 | BaseCopyCtor->getThisType(getContext()))); |
| 540 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 541 | // Push the VTT parameter, if necessary. |
| 542 | if (llvm::Value *VTT = |
| 543 | GetVTTParameter(*this, GlobalDecl(BaseCopyCtor, CtorType))) { |
| 544 | QualType T = getContext().getPointerType(getContext().VoidPtrTy); |
| 545 | CallArgs.push_back(std::make_pair(RValue::get(VTT), T)); |
| 546 | } |
| 547 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 548 | // Push the Src ptr. |
| 549 | CallArgs.push_back(std::make_pair(RValue::get(Src), |
| 550 | BaseCopyCtor->getParamDecl(0)->getType())); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 551 | const FunctionProtoType *FPT = |
| 552 | BaseCopyCtor->getType()->getAs<FunctionProtoType>(); |
| 553 | EmitCall(CGM.getTypes().getFunctionInfo(CallArgs, FPT), |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 554 | Callee, ReturnValueSlot(), CallArgs, BaseCopyCtor); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | /// EmitClassCopyAssignment - This routine generates code to copy assign a class |
| 559 | /// object from SrcValue to DestValue. Assignment can be either a bitwise |
| 560 | /// assignment of via an assignment operator call. |
| 561 | // FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot. |
| 562 | void CodeGenFunction::EmitClassCopyAssignment( |
| 563 | llvm::Value *Dest, llvm::Value *Src, |
| 564 | const CXXRecordDecl *ClassDecl, |
| 565 | const CXXRecordDecl *BaseClassDecl, |
| 566 | QualType Ty) { |
| 567 | if (ClassDecl) { |
| 568 | Dest = GetAddressOfBaseClass(Dest, ClassDecl, BaseClassDecl, |
| 569 | /*NullCheckValue=*/false); |
| 570 | Src = GetAddressOfBaseClass(Src, ClassDecl, BaseClassDecl, |
| 571 | /*NullCheckValue=*/false); |
| 572 | } |
| 573 | if (BaseClassDecl->hasTrivialCopyAssignment()) { |
| 574 | EmitAggregateCopy(Dest, Src, Ty); |
| 575 | return; |
| 576 | } |
| 577 | |
| 578 | const CXXMethodDecl *MD = 0; |
Eli Friedman | 8a850ba | 2010-01-15 20:06:11 +0000 | [diff] [blame] | 579 | BaseClassDecl->hasConstCopyAssignment(getContext(), MD); |
| 580 | assert(MD && "EmitClassCopyAssignment - missing copy assign"); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 581 | |
| 582 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 583 | const llvm::Type *LTy = |
| 584 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 585 | FPT->isVariadic()); |
| 586 | llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy); |
| 587 | |
| 588 | CallArgList CallArgs; |
| 589 | // Push the this (Dest) ptr. |
| 590 | CallArgs.push_back(std::make_pair(RValue::get(Dest), |
| 591 | MD->getThisType(getContext()))); |
| 592 | |
| 593 | // Push the Src ptr. |
Eli Friedman | 8a850ba | 2010-01-15 20:06:11 +0000 | [diff] [blame] | 594 | QualType SrcTy = MD->getParamDecl(0)->getType(); |
| 595 | RValue SrcValue = SrcTy->isReferenceType() ? RValue::get(Src) : |
| 596 | RValue::getAggregate(Src); |
| 597 | CallArgs.push_back(std::make_pair(SrcValue, SrcTy)); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 598 | EmitCall(CGM.getTypes().getFunctionInfo(CallArgs, FPT), |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 599 | Callee, ReturnValueSlot(), CallArgs, MD); |
| 600 | } |
| 601 | |
John McCall | a355e07 | 2010-02-18 03:17:58 +0000 | [diff] [blame] | 602 | /// Synthesizes an implicit function body. Since these only arise in |
| 603 | /// C++, we only do them in C++. |
| 604 | void CodeGenFunction::SynthesizeImplicitFunctionBody(GlobalDecl GD, |
| 605 | llvm::Function *Fn, |
| 606 | const FunctionArgList &Args) { |
| 607 | const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); |
| 608 | |
| 609 | // FIXME: this should become isImplicitlyDefined() once we properly |
| 610 | // support that for C++0x. |
| 611 | assert(FD->isImplicit() && "Cannot synthesize a non-implicit function"); |
| 612 | |
| 613 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { |
| 614 | assert(!CD->isTrivial() && "shouldn't need to synthesize a trivial ctor"); |
| 615 | |
| 616 | if (CD->isDefaultConstructor()) { |
| 617 | // Sema generates base and member initializers as for this, so |
| 618 | // the ctor prologue is good enough here. |
| 619 | return; |
| 620 | } else { |
| 621 | assert(CD->isCopyConstructor()); |
| 622 | return SynthesizeCXXCopyConstructor(CD, GD.getCtorType(), Fn, Args); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | if (isa<CXXDestructorDecl>(FD)) { |
| 627 | // The dtor epilogue does everything we'd need to do here. |
| 628 | return; |
| 629 | } |
| 630 | |
| 631 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(FD); |
| 632 | |
| 633 | // FIXME: in C++0x we might have user-declared copy assignment operators |
| 634 | // coexisting with implicitly-defined ones. |
| 635 | assert(MD->isCopyAssignment() && |
| 636 | !MD->getParent()->hasUserDeclaredCopyAssignment() && |
| 637 | "Cannot synthesize a method that is not an implicitly-defined " |
| 638 | "copy constructor"); |
| 639 | SynthesizeCXXCopyAssignment(MD, Fn, Args); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | /// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a |
| 643 | /// copy constructor, in accordance with section 12.8 (p7 and p8) of C++03 |
| 644 | /// The implicitly-defined copy constructor for class X performs a memberwise |
| 645 | /// copy of its subobjects. The order of copying is the same as the order of |
| 646 | /// initialization of bases and members in a user-defined constructor |
| 647 | /// Each subobject is copied in the manner appropriate to its type: |
| 648 | /// if the subobject is of class type, the copy constructor for the class is |
| 649 | /// used; |
| 650 | /// if the subobject is an array, each element is copied, in the manner |
| 651 | /// appropriate to the element type; |
| 652 | /// if the subobject is of scalar type, the built-in assignment operator is |
| 653 | /// used. |
| 654 | /// Virtual base class subobjects shall be copied only once by the |
| 655 | /// implicitly-defined copy constructor |
| 656 | |
| 657 | void |
| 658 | CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor, |
| 659 | CXXCtorType Type, |
| 660 | llvm::Function *Fn, |
| 661 | const FunctionArgList &Args) { |
| 662 | const CXXRecordDecl *ClassDecl = Ctor->getParent(); |
| 663 | assert(!ClassDecl->hasUserDeclaredCopyConstructor() && |
| 664 | "SynthesizeCXXCopyConstructor - copy constructor has definition already"); |
| 665 | assert(!Ctor->isTrivial() && "shouldn't need to generate trivial ctor"); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 666 | |
| 667 | FunctionArgList::const_iterator i = Args.begin(); |
| 668 | const VarDecl *ThisArg = i->first; |
| 669 | llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg); |
| 670 | llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this"); |
| 671 | const VarDecl *SrcArg = (i+1)->first; |
| 672 | llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg); |
| 673 | llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj); |
| 674 | |
| 675 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); |
| 676 | Base != ClassDecl->bases_end(); ++Base) { |
| 677 | // FIXME. copy constrution of virtual base NYI |
| 678 | if (Base->isVirtual()) |
| 679 | continue; |
| 680 | |
| 681 | CXXRecordDecl *BaseClassDecl |
| 682 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 683 | EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl, |
| 684 | Base->getType()); |
| 685 | } |
| 686 | |
| 687 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 688 | E = ClassDecl->field_end(); I != E; ++I) { |
| 689 | const FieldDecl *Field = *I; |
| 690 | |
| 691 | QualType FieldType = getContext().getCanonicalType(Field->getType()); |
| 692 | const ConstantArrayType *Array = |
| 693 | getContext().getAsConstantArrayType(FieldType); |
| 694 | if (Array) |
| 695 | FieldType = getContext().getBaseElementType(FieldType); |
| 696 | |
| 697 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
| 698 | CXXRecordDecl *FieldClassDecl |
| 699 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Anders Carlsson | e6d2a53 | 2010-01-29 05:05:36 +0000 | [diff] [blame] | 700 | LValue LHS = EmitLValueForField(LoadOfThis, Field, 0); |
| 701 | LValue RHS = EmitLValueForField(LoadOfSrc, Field, 0); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 702 | if (Array) { |
| 703 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 704 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 705 | llvm::Value *DestBaseAddrPtr = |
| 706 | Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
| 707 | llvm::Value *SrcBaseAddrPtr = |
| 708 | Builder.CreateBitCast(RHS.getAddress(), BasePtr); |
| 709 | EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array, |
| 710 | FieldClassDecl, FieldType); |
| 711 | } |
| 712 | else |
| 713 | EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(), |
| 714 | 0 /*ClassDecl*/, FieldClassDecl, FieldType); |
| 715 | continue; |
| 716 | } |
| 717 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 718 | // Do a built-in assignment of scalar data members. |
Anders Carlsson | 9cfe0ec | 2010-01-29 05:41:25 +0000 | [diff] [blame] | 719 | LValue LHS = EmitLValueForFieldInitialization(LoadOfThis, Field, 0); |
| 720 | LValue RHS = EmitLValueForFieldInitialization(LoadOfSrc, Field, 0); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 721 | |
| 722 | if (!hasAggregateLLVMType(Field->getType())) { |
| 723 | RValue RVRHS = EmitLoadOfLValue(RHS, Field->getType()); |
| 724 | EmitStoreThroughLValue(RVRHS, LHS, Field->getType()); |
| 725 | } else if (Field->getType()->isAnyComplexType()) { |
| 726 | ComplexPairTy Pair = LoadComplexFromAddr(RHS.getAddress(), |
| 727 | RHS.isVolatileQualified()); |
| 728 | StoreComplexToAddr(Pair, LHS.getAddress(), LHS.isVolatileQualified()); |
| 729 | } else { |
| 730 | EmitAggregateCopy(LHS.getAddress(), RHS.getAddress(), Field->getType()); |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | InitializeVtablePtrs(ClassDecl); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | /// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator. |
| 738 | /// Before the implicitly-declared copy assignment operator for a class is |
| 739 | /// implicitly defined, all implicitly- declared copy assignment operators for |
| 740 | /// its direct base classes and its nonstatic data members shall have been |
| 741 | /// implicitly defined. [12.8-p12] |
| 742 | /// The implicitly-defined copy assignment operator for class X performs |
| 743 | /// memberwise assignment of its subob- jects. The direct base classes of X are |
| 744 | /// assigned first, in the order of their declaration in |
| 745 | /// the base-specifier-list, and then the immediate nonstatic data members of X |
| 746 | /// are assigned, in the order in which they were declared in the class |
| 747 | /// definition.Each subobject is assigned in the manner appropriate to its type: |
| 748 | /// if the subobject is of class type, the copy assignment operator for the |
| 749 | /// class is used (as if by explicit qualification; that is, ignoring any |
| 750 | /// possible virtual overriding functions in more derived classes); |
| 751 | /// |
| 752 | /// if the subobject is an array, each element is assigned, in the manner |
| 753 | /// appropriate to the element type; |
| 754 | /// |
| 755 | /// if the subobject is of scalar type, the built-in assignment operator is |
| 756 | /// used. |
| 757 | void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD, |
| 758 | llvm::Function *Fn, |
| 759 | const FunctionArgList &Args) { |
| 760 | |
| 761 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext()); |
| 762 | assert(!ClassDecl->hasUserDeclaredCopyAssignment() && |
| 763 | "SynthesizeCXXCopyAssignment - copy assignment has user declaration"); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 764 | |
| 765 | FunctionArgList::const_iterator i = Args.begin(); |
| 766 | const VarDecl *ThisArg = i->first; |
| 767 | llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg); |
| 768 | llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this"); |
| 769 | const VarDecl *SrcArg = (i+1)->first; |
| 770 | llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg); |
| 771 | llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj); |
| 772 | |
| 773 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); |
| 774 | Base != ClassDecl->bases_end(); ++Base) { |
| 775 | // FIXME. copy assignment of virtual base NYI |
| 776 | if (Base->isVirtual()) |
| 777 | continue; |
| 778 | |
| 779 | CXXRecordDecl *BaseClassDecl |
| 780 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 781 | EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl, |
| 782 | Base->getType()); |
| 783 | } |
| 784 | |
| 785 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 786 | FieldEnd = ClassDecl->field_end(); |
| 787 | Field != FieldEnd; ++Field) { |
| 788 | QualType FieldType = getContext().getCanonicalType((*Field)->getType()); |
| 789 | const ConstantArrayType *Array = |
| 790 | getContext().getAsConstantArrayType(FieldType); |
| 791 | if (Array) |
| 792 | FieldType = getContext().getBaseElementType(FieldType); |
| 793 | |
| 794 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
| 795 | CXXRecordDecl *FieldClassDecl |
| 796 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Anders Carlsson | e6d2a53 | 2010-01-29 05:05:36 +0000 | [diff] [blame] | 797 | LValue LHS = EmitLValueForField(LoadOfThis, *Field, 0); |
| 798 | LValue RHS = EmitLValueForField(LoadOfSrc, *Field, 0); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 799 | if (Array) { |
| 800 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 801 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 802 | llvm::Value *DestBaseAddrPtr = |
| 803 | Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
| 804 | llvm::Value *SrcBaseAddrPtr = |
| 805 | Builder.CreateBitCast(RHS.getAddress(), BasePtr); |
| 806 | EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array, |
| 807 | FieldClassDecl, FieldType); |
| 808 | } |
| 809 | else |
| 810 | EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(), |
| 811 | 0 /*ClassDecl*/, FieldClassDecl, FieldType); |
| 812 | continue; |
| 813 | } |
| 814 | // Do a built-in assignment of scalar data members. |
Anders Carlsson | e6d2a53 | 2010-01-29 05:05:36 +0000 | [diff] [blame] | 815 | LValue LHS = EmitLValueForField(LoadOfThis, *Field, 0); |
| 816 | LValue RHS = EmitLValueForField(LoadOfSrc, *Field, 0); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 817 | if (!hasAggregateLLVMType(Field->getType())) { |
| 818 | RValue RVRHS = EmitLoadOfLValue(RHS, Field->getType()); |
| 819 | EmitStoreThroughLValue(RVRHS, LHS, Field->getType()); |
| 820 | } else if (Field->getType()->isAnyComplexType()) { |
| 821 | ComplexPairTy Pair = LoadComplexFromAddr(RHS.getAddress(), |
| 822 | RHS.isVolatileQualified()); |
| 823 | StoreComplexToAddr(Pair, LHS.getAddress(), LHS.isVolatileQualified()); |
| 824 | } else { |
| 825 | EmitAggregateCopy(LHS.getAddress(), RHS.getAddress(), Field->getType()); |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | // return *this; |
| 830 | Builder.CreateStore(LoadOfThis, ReturnValue); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | static void EmitBaseInitializer(CodeGenFunction &CGF, |
| 834 | const CXXRecordDecl *ClassDecl, |
| 835 | CXXBaseOrMemberInitializer *BaseInit, |
| 836 | CXXCtorType CtorType) { |
| 837 | assert(BaseInit->isBaseInitializer() && |
| 838 | "Must have base initializer!"); |
| 839 | |
| 840 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
| 841 | |
| 842 | const Type *BaseType = BaseInit->getBaseClass(); |
| 843 | CXXRecordDecl *BaseClassDecl = |
| 844 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); |
| 845 | |
| 846 | // FIXME: This method of determining whether a base is virtual is ridiculous; |
| 847 | // it should be part of BaseInit. |
| 848 | bool isBaseVirtual = false; |
| 849 | for (CXXRecordDecl::base_class_const_iterator I = ClassDecl->vbases_begin(), |
| 850 | E = ClassDecl->vbases_end(); I != E; ++I) |
| 851 | if (I->getType()->getAs<RecordType>()->getDecl() == BaseClassDecl) { |
| 852 | isBaseVirtual = true; |
| 853 | break; |
| 854 | } |
| 855 | |
| 856 | // The base constructor doesn't construct virtual bases. |
| 857 | if (CtorType == Ctor_Base && isBaseVirtual) |
| 858 | return; |
| 859 | |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 860 | // We can pretend to be a complete class because it only matters for |
| 861 | // virtual bases, and we only do virtual bases for complete ctors. |
| 862 | llvm::Value *V = ThisPtr; |
| 863 | V = CGF.GetAddressOfBaseOfCompleteClass(V, isBaseVirtual, |
| 864 | ClassDecl, BaseClassDecl); |
| 865 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 866 | CGF.EmitAggExpr(BaseInit->getInit(), V, false, false, true); |
Anders Carlsson | 594d5e8 | 2010-02-06 20:00:21 +0000 | [diff] [blame] | 867 | |
| 868 | if (CGF.Exceptions && !BaseClassDecl->hasTrivialDestructor()) { |
| 869 | // FIXME: Is this OK for C++0x delegating constructors? |
| 870 | CodeGenFunction::EHCleanupBlock Cleanup(CGF); |
| 871 | |
Anders Carlsson | 594d5e8 | 2010-02-06 20:00:21 +0000 | [diff] [blame] | 872 | CXXDestructorDecl *DD = BaseClassDecl->getDestructor(CGF.getContext()); |
| 873 | CGF.EmitCXXDestructorCall(DD, Dtor_Base, V); |
| 874 | } |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | static void EmitMemberInitializer(CodeGenFunction &CGF, |
| 878 | const CXXRecordDecl *ClassDecl, |
| 879 | CXXBaseOrMemberInitializer *MemberInit) { |
| 880 | assert(MemberInit->isMemberInitializer() && |
| 881 | "Must have member initializer!"); |
| 882 | |
| 883 | // non-static data member initializers. |
| 884 | FieldDecl *Field = MemberInit->getMember(); |
| 885 | QualType FieldType = CGF.getContext().getCanonicalType(Field->getType()); |
| 886 | |
| 887 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
Anders Carlsson | 06a2970 | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 888 | LValue LHS = CGF.EmitLValueForFieldInitialization(ThisPtr, Field, 0); |
| 889 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 890 | // If we are initializing an anonymous union field, drill down to the field. |
| 891 | if (MemberInit->getAnonUnionMember()) { |
| 892 | Field = MemberInit->getAnonUnionMember(); |
Anders Carlsson | e6d2a53 | 2010-01-29 05:05:36 +0000 | [diff] [blame] | 893 | LHS = CGF.EmitLValueForField(LHS.getAddress(), Field, 0); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 894 | FieldType = Field->getType(); |
| 895 | } |
| 896 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 897 | // FIXME: If there's no initializer and the CXXBaseOrMemberInitializer |
| 898 | // was implicitly generated, we shouldn't be zeroing memory. |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 899 | RValue RHS; |
| 900 | if (FieldType->isReferenceType()) { |
Anders Carlsson | a64a869 | 2010-02-03 16:38:03 +0000 | [diff] [blame] | 901 | RHS = CGF.EmitReferenceBindingToExpr(MemberInit->getInit(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 902 | /*IsInitializer=*/true); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 903 | CGF.EmitStoreThroughLValue(RHS, LHS, FieldType); |
Eli Friedman | 3bb9412 | 2010-01-31 19:07:50 +0000 | [diff] [blame] | 904 | } else if (FieldType->isArrayType() && !MemberInit->getInit()) { |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 905 | CGF.EmitMemSetToZero(LHS.getAddress(), Field->getType()); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 906 | } else if (!CGF.hasAggregateLLVMType(Field->getType())) { |
| 907 | RHS = RValue::get(CGF.EmitScalarExpr(MemberInit->getInit(), true)); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 908 | CGF.EmitStoreThroughLValue(RHS, LHS, FieldType); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 909 | } else if (MemberInit->getInit()->getType()->isAnyComplexType()) { |
| 910 | CGF.EmitComplexExprIntoAddr(MemberInit->getInit(), LHS.getAddress(), |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 911 | LHS.isVolatileQualified()); |
| 912 | } else { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 913 | CGF.EmitAggExpr(MemberInit->getInit(), LHS.getAddress(), |
| 914 | LHS.isVolatileQualified(), false, true); |
Anders Carlsson | 9405dcd | 2010-02-06 19:50:17 +0000 | [diff] [blame] | 915 | |
| 916 | if (!CGF.Exceptions) |
| 917 | return; |
| 918 | |
| 919 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 920 | if (!RT) |
| 921 | return; |
| 922 | |
| 923 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 924 | if (!RD->hasTrivialDestructor()) { |
| 925 | // FIXME: Is this OK for C++0x delegating constructors? |
| 926 | CodeGenFunction::EHCleanupBlock Cleanup(CGF); |
| 927 | |
| 928 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
| 929 | LValue LHS = CGF.EmitLValueForField(ThisPtr, Field, 0); |
| 930 | |
| 931 | CXXDestructorDecl *DD = RD->getDestructor(CGF.getContext()); |
| 932 | CGF.EmitCXXDestructorCall(DD, Dtor_Complete, LHS.getAddress()); |
| 933 | } |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
| 937 | /// EmitCtorPrologue - This routine generates necessary code to initialize |
| 938 | /// base classes and non-static data members belonging to this constructor. |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 939 | void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD, |
| 940 | CXXCtorType CtorType) { |
| 941 | const CXXRecordDecl *ClassDecl = CD->getParent(); |
Anders Carlsson | a78fa2c | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 942 | |
| 943 | llvm::SmallVector<CXXBaseOrMemberInitializer *, 8> MemberInitializers; |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 944 | |
| 945 | // FIXME: Add vbase initialization |
| 946 | |
| 947 | for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(), |
| 948 | E = CD->init_end(); |
| 949 | B != E; ++B) { |
| 950 | CXXBaseOrMemberInitializer *Member = (*B); |
| 951 | |
| 952 | assert(LiveTemporaries.empty() && |
| 953 | "Should not have any live temporaries at initializer start!"); |
| 954 | |
| 955 | if (Member->isBaseInitializer()) |
| 956 | EmitBaseInitializer(*this, ClassDecl, Member, CtorType); |
| 957 | else |
Anders Carlsson | a78fa2c | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 958 | MemberInitializers.push_back(Member); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | InitializeVtablePtrs(ClassDecl); |
Anders Carlsson | a78fa2c | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 962 | |
| 963 | for (unsigned I = 0, E = MemberInitializers.size(); I != E; ++I) { |
| 964 | assert(LiveTemporaries.empty() && |
| 965 | "Should not have any live temporaries at initializer start!"); |
| 966 | |
| 967 | EmitMemberInitializer(*this, ClassDecl, MemberInitializers[I]); |
| 968 | } |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | /// EmitDtorEpilogue - Emit all code that comes at the end of class's |
| 972 | /// destructor. This is to call destructors on members and base classes |
| 973 | /// in reverse order of their construction. |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 974 | void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD, |
| 975 | CXXDtorType DtorType) { |
| 976 | assert(!DD->isTrivial() && |
| 977 | "Should not emit dtor epilogue for trivial dtor!"); |
| 978 | |
| 979 | const CXXRecordDecl *ClassDecl = DD->getParent(); |
| 980 | |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame^] | 981 | // In a deleting destructor, we've already called the complete |
| 982 | // destructor as a subroutine, so we just have to delete the |
| 983 | // appropriate value. |
| 984 | if (DtorType == Dtor_Deleting) { |
| 985 | assert(DD->getOperatorDelete() && |
| 986 | "operator delete missing - EmitDtorEpilogue"); |
| 987 | EmitDeleteCall(DD->getOperatorDelete(), LoadCXXThis(), |
| 988 | getContext().getTagDeclType(ClassDecl)); |
| 989 | return; |
| 990 | } |
| 991 | |
| 992 | // For complete destructors, we've already called the base |
| 993 | // destructor (in GenerateBody), so we just need to destruct all the |
| 994 | // virtual bases. |
| 995 | if (DtorType == Dtor_Complete) { |
| 996 | // Handle virtual bases. |
| 997 | for (CXXRecordDecl::reverse_base_class_const_iterator I = |
| 998 | ClassDecl->vbases_rbegin(), E = ClassDecl->vbases_rend(); |
| 999 | I != E; ++I) { |
| 1000 | const CXXBaseSpecifier &Base = *I; |
| 1001 | CXXRecordDecl *BaseClassDecl |
| 1002 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| 1003 | |
| 1004 | // Ignore trivial destructors. |
| 1005 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1006 | continue; |
| 1007 | const CXXDestructorDecl *D = BaseClassDecl->getDestructor(getContext()); |
| 1008 | llvm::Value *V = GetAddressOfBaseOfCompleteClass(LoadCXXThis(), |
| 1009 | true, |
| 1010 | ClassDecl, |
| 1011 | BaseClassDecl); |
| 1012 | EmitCXXDestructorCall(D, Dtor_Base, V); |
| 1013 | } |
| 1014 | return; |
| 1015 | } |
| 1016 | |
| 1017 | assert(DtorType == Dtor_Base); |
| 1018 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1019 | // Collect the fields. |
| 1020 | llvm::SmallVector<const FieldDecl *, 16> FieldDecls; |
| 1021 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 1022 | E = ClassDecl->field_end(); I != E; ++I) { |
| 1023 | const FieldDecl *Field = *I; |
| 1024 | |
| 1025 | QualType FieldType = getContext().getCanonicalType(Field->getType()); |
| 1026 | FieldType = getContext().getBaseElementType(FieldType); |
| 1027 | |
| 1028 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 1029 | if (!RT) |
| 1030 | continue; |
| 1031 | |
| 1032 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 1033 | if (FieldClassDecl->hasTrivialDestructor()) |
| 1034 | continue; |
| 1035 | |
| 1036 | FieldDecls.push_back(Field); |
| 1037 | } |
| 1038 | |
| 1039 | // Now destroy the fields. |
| 1040 | for (size_t i = FieldDecls.size(); i > 0; --i) { |
| 1041 | const FieldDecl *Field = FieldDecls[i - 1]; |
| 1042 | |
| 1043 | QualType FieldType = Field->getType(); |
| 1044 | const ConstantArrayType *Array = |
| 1045 | getContext().getAsConstantArrayType(FieldType); |
| 1046 | if (Array) |
| 1047 | FieldType = getContext().getBaseElementType(FieldType); |
| 1048 | |
| 1049 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 1050 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 1051 | |
| 1052 | llvm::Value *ThisPtr = LoadCXXThis(); |
| 1053 | |
| 1054 | LValue LHS = EmitLValueForField(ThisPtr, Field, |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1055 | // FIXME: Qualifiers? |
| 1056 | /*CVRQualifiers=*/0); |
| 1057 | if (Array) { |
| 1058 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 1059 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 1060 | llvm::Value *BaseAddrPtr = |
| 1061 | Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
| 1062 | EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()), |
| 1063 | Array, BaseAddrPtr); |
| 1064 | } else |
| 1065 | EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()), |
| 1066 | Dtor_Complete, LHS.getAddress()); |
| 1067 | } |
| 1068 | |
| 1069 | // Destroy non-virtual bases. |
| 1070 | for (CXXRecordDecl::reverse_base_class_const_iterator I = |
| 1071 | ClassDecl->bases_rbegin(), E = ClassDecl->bases_rend(); I != E; ++I) { |
| 1072 | const CXXBaseSpecifier &Base = *I; |
| 1073 | |
| 1074 | // Ignore virtual bases. |
| 1075 | if (Base.isVirtual()) |
| 1076 | continue; |
| 1077 | |
| 1078 | CXXRecordDecl *BaseClassDecl |
| 1079 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| 1080 | |
| 1081 | // Ignore trivial destructors. |
| 1082 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1083 | continue; |
| 1084 | const CXXDestructorDecl *D = BaseClassDecl->getDestructor(getContext()); |
| 1085 | |
| 1086 | llvm::Value *V = GetAddressOfBaseClass(LoadCXXThis(), |
| 1087 | ClassDecl, BaseClassDecl, |
| 1088 | /*NullCheckValue=*/false); |
| 1089 | EmitCXXDestructorCall(D, Dtor_Base, V); |
| 1090 | } |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1093 | /// EmitCXXAggrConstructorCall - This routine essentially creates a (nested) |
| 1094 | /// for-loop to call the default constructor on individual members of the |
| 1095 | /// array. |
| 1096 | /// 'D' is the default constructor for elements of the array, 'ArrayTy' is the |
| 1097 | /// array type and 'ArrayPtr' points to the beginning fo the array. |
| 1098 | /// It is assumed that all relevant checks have been made by the caller. |
| 1099 | void |
| 1100 | CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
| 1101 | const ConstantArrayType *ArrayTy, |
| 1102 | llvm::Value *ArrayPtr, |
| 1103 | CallExpr::const_arg_iterator ArgBeg, |
| 1104 | CallExpr::const_arg_iterator ArgEnd) { |
| 1105 | |
| 1106 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 1107 | llvm::Value * NumElements = |
| 1108 | llvm::ConstantInt::get(SizeTy, |
| 1109 | getContext().getConstantArrayElementCount(ArrayTy)); |
| 1110 | |
| 1111 | EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr, ArgBeg, ArgEnd); |
| 1112 | } |
| 1113 | |
| 1114 | void |
| 1115 | CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
| 1116 | llvm::Value *NumElements, |
| 1117 | llvm::Value *ArrayPtr, |
| 1118 | CallExpr::const_arg_iterator ArgBeg, |
| 1119 | CallExpr::const_arg_iterator ArgEnd) { |
| 1120 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 1121 | |
| 1122 | // Create a temporary for the loop index and initialize it with 0. |
| 1123 | llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index"); |
| 1124 | llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy); |
| 1125 | Builder.CreateStore(Zero, IndexPtr); |
| 1126 | |
| 1127 | // Start the loop with a block that tests the condition. |
| 1128 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 1129 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
| 1130 | |
| 1131 | EmitBlock(CondBlock); |
| 1132 | |
| 1133 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 1134 | |
| 1135 | // Generate: if (loop-index < number-of-elements fall to the loop body, |
| 1136 | // otherwise, go to the block after the for-loop. |
| 1137 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 1138 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless"); |
| 1139 | // If the condition is true, execute the body. |
| 1140 | Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
| 1141 | |
| 1142 | EmitBlock(ForBody); |
| 1143 | |
| 1144 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 1145 | // Inside the loop body, emit the constructor call on the array element. |
| 1146 | Counter = Builder.CreateLoad(IndexPtr); |
| 1147 | llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter, |
| 1148 | "arrayidx"); |
| 1149 | |
| 1150 | // C++ [class.temporary]p4: |
| 1151 | // There are two contexts in which temporaries are destroyed at a different |
| 1152 | // point than the end of the full-expression. The first context is when a |
| 1153 | // default constructor is called to initialize an element of an array. |
| 1154 | // If the constructor has one or more default arguments, the destruction of |
| 1155 | // every temporary created in a default argument expression is sequenced |
| 1156 | // before the construction of the next array element, if any. |
| 1157 | |
| 1158 | // Keep track of the current number of live temporaries. |
| 1159 | unsigned OldNumLiveTemporaries = LiveTemporaries.size(); |
| 1160 | |
| 1161 | EmitCXXConstructorCall(D, Ctor_Complete, Address, ArgBeg, ArgEnd); |
| 1162 | |
| 1163 | // Pop temporaries. |
| 1164 | while (LiveTemporaries.size() > OldNumLiveTemporaries) |
| 1165 | PopCXXTemporary(); |
| 1166 | |
| 1167 | EmitBlock(ContinueBlock); |
| 1168 | |
| 1169 | // Emit the increment of the loop counter. |
| 1170 | llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1); |
| 1171 | Counter = Builder.CreateLoad(IndexPtr); |
| 1172 | NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); |
| 1173 | Builder.CreateStore(NextVal, IndexPtr); |
| 1174 | |
| 1175 | // Finally, branch back up to the condition for the next iteration. |
| 1176 | EmitBranch(CondBlock); |
| 1177 | |
| 1178 | // Emit the fall-through block. |
| 1179 | EmitBlock(AfterFor, true); |
| 1180 | } |
| 1181 | |
| 1182 | /// EmitCXXAggrDestructorCall - calls the default destructor on array |
| 1183 | /// elements in reverse order of construction. |
| 1184 | void |
| 1185 | CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, |
| 1186 | const ArrayType *Array, |
| 1187 | llvm::Value *This) { |
| 1188 | const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); |
| 1189 | assert(CA && "Do we support VLA for destruction ?"); |
| 1190 | uint64_t ElementCount = getContext().getConstantArrayElementCount(CA); |
| 1191 | |
| 1192 | const llvm::Type *SizeLTy = ConvertType(getContext().getSizeType()); |
| 1193 | llvm::Value* ElementCountPtr = llvm::ConstantInt::get(SizeLTy, ElementCount); |
| 1194 | EmitCXXAggrDestructorCall(D, ElementCountPtr, This); |
| 1195 | } |
| 1196 | |
| 1197 | /// EmitCXXAggrDestructorCall - calls the default destructor on array |
| 1198 | /// elements in reverse order of construction. |
| 1199 | void |
| 1200 | CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, |
| 1201 | llvm::Value *UpperCount, |
| 1202 | llvm::Value *This) { |
| 1203 | const llvm::Type *SizeLTy = ConvertType(getContext().getSizeType()); |
| 1204 | llvm::Value *One = llvm::ConstantInt::get(SizeLTy, 1); |
| 1205 | |
| 1206 | // Create a temporary for the loop index and initialize it with count of |
| 1207 | // array elements. |
| 1208 | llvm::Value *IndexPtr = CreateTempAlloca(SizeLTy, "loop.index"); |
| 1209 | |
| 1210 | // Store the number of elements in the index pointer. |
| 1211 | Builder.CreateStore(UpperCount, IndexPtr); |
| 1212 | |
| 1213 | // Start the loop with a block that tests the condition. |
| 1214 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 1215 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
| 1216 | |
| 1217 | EmitBlock(CondBlock); |
| 1218 | |
| 1219 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 1220 | |
| 1221 | // Generate: if (loop-index != 0 fall to the loop body, |
| 1222 | // otherwise, go to the block after the for-loop. |
| 1223 | llvm::Value* zeroConstant = |
| 1224 | llvm::Constant::getNullValue(SizeLTy); |
| 1225 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 1226 | llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant, |
| 1227 | "isne"); |
| 1228 | // If the condition is true, execute the body. |
| 1229 | Builder.CreateCondBr(IsNE, ForBody, AfterFor); |
| 1230 | |
| 1231 | EmitBlock(ForBody); |
| 1232 | |
| 1233 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 1234 | // Inside the loop body, emit the constructor call on the array element. |
| 1235 | Counter = Builder.CreateLoad(IndexPtr); |
| 1236 | Counter = Builder.CreateSub(Counter, One); |
| 1237 | llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx"); |
| 1238 | EmitCXXDestructorCall(D, Dtor_Complete, Address); |
| 1239 | |
| 1240 | EmitBlock(ContinueBlock); |
| 1241 | |
| 1242 | // Emit the decrement of the loop counter. |
| 1243 | Counter = Builder.CreateLoad(IndexPtr); |
| 1244 | Counter = Builder.CreateSub(Counter, One, "dec"); |
| 1245 | Builder.CreateStore(Counter, IndexPtr); |
| 1246 | |
| 1247 | // Finally, branch back up to the condition for the next iteration. |
| 1248 | EmitBranch(CondBlock); |
| 1249 | |
| 1250 | // Emit the fall-through block. |
| 1251 | EmitBlock(AfterFor, true); |
| 1252 | } |
| 1253 | |
| 1254 | /// GenerateCXXAggrDestructorHelper - Generates a helper function which when |
| 1255 | /// invoked, calls the default destructor on array elements in reverse order of |
| 1256 | /// construction. |
| 1257 | llvm::Constant * |
| 1258 | CodeGenFunction::GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D, |
| 1259 | const ArrayType *Array, |
| 1260 | llvm::Value *This) { |
| 1261 | FunctionArgList Args; |
| 1262 | ImplicitParamDecl *Dst = |
| 1263 | ImplicitParamDecl::Create(getContext(), 0, |
| 1264 | SourceLocation(), 0, |
| 1265 | getContext().getPointerType(getContext().VoidTy)); |
| 1266 | Args.push_back(std::make_pair(Dst, Dst->getType())); |
| 1267 | |
| 1268 | llvm::SmallString<16> Name; |
| 1269 | llvm::raw_svector_ostream(Name) << "__tcf_" << (++UniqueAggrDestructorCount); |
| 1270 | QualType R = getContext().VoidTy; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1271 | const CGFunctionInfo &FI |
| 1272 | = CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1273 | const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false); |
| 1274 | llvm::Function *Fn = |
| 1275 | llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, |
| 1276 | Name.str(), |
| 1277 | &CGM.getModule()); |
| 1278 | IdentifierInfo *II = &CGM.getContext().Idents.get(Name.str()); |
| 1279 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 1280 | getContext().getTranslationUnitDecl(), |
| 1281 | SourceLocation(), II, R, 0, |
| 1282 | FunctionDecl::Static, |
| 1283 | false, true); |
| 1284 | StartFunction(FD, R, Fn, Args, SourceLocation()); |
| 1285 | QualType BaseElementTy = getContext().getBaseElementType(Array); |
| 1286 | const llvm::Type *BasePtr = ConvertType(BaseElementTy); |
| 1287 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 1288 | llvm::Value *BaseAddrPtr = Builder.CreateBitCast(This, BasePtr); |
| 1289 | EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr); |
| 1290 | FinishFunction(); |
| 1291 | llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), |
| 1292 | 0); |
| 1293 | llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); |
| 1294 | return m; |
| 1295 | } |
| 1296 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1297 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1298 | void |
| 1299 | CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, |
| 1300 | CXXCtorType Type, |
| 1301 | llvm::Value *This, |
| 1302 | CallExpr::const_arg_iterator ArgBeg, |
| 1303 | CallExpr::const_arg_iterator ArgEnd) { |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1304 | if (D->isTrivial()) { |
| 1305 | if (ArgBeg == ArgEnd) { |
| 1306 | // Trivial default constructor, no codegen required. |
| 1307 | assert(D->isDefaultConstructor() && |
| 1308 | "trivial 0-arg ctor not a default ctor"); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1309 | return; |
| 1310 | } |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1311 | |
| 1312 | assert(ArgBeg + 1 == ArgEnd && "unexpected argcount for trivial ctor"); |
| 1313 | assert(D->isCopyConstructor() && "trivial 1-arg ctor not a copy ctor"); |
| 1314 | |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1315 | const Expr *E = (*ArgBeg); |
| 1316 | QualType Ty = E->getType(); |
| 1317 | llvm::Value *Src = EmitLValue(E).getAddress(); |
| 1318 | EmitAggregateCopy(This, Src, Ty); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1319 | return; |
| 1320 | } |
| 1321 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1322 | llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(D, Type)); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1323 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type); |
| 1324 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1325 | EmitCXXMemberCall(D, Callee, ReturnValueSlot(), This, VTT, ArgBeg, ArgEnd); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *DD, |
| 1329 | CXXDtorType Type, |
| 1330 | llvm::Value *This) { |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1331 | llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(DD, Type)); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1332 | llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(DD, Type); |
| 1333 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1334 | EmitCXXMemberCall(DD, Callee, ReturnValueSlot(), This, VTT, 0, 0); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | llvm::Value * |
Anders Carlsson | bb7e17b | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 1338 | CodeGenFunction::GetVirtualBaseClassOffset(llvm::Value *This, |
| 1339 | const CXXRecordDecl *ClassDecl, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1340 | const CXXRecordDecl *BaseClassDecl) { |
| 1341 | const llvm::Type *Int8PtrTy = |
| 1342 | llvm::Type::getInt8Ty(VMContext)->getPointerTo(); |
| 1343 | |
| 1344 | llvm::Value *VTablePtr = Builder.CreateBitCast(This, |
| 1345 | Int8PtrTy->getPointerTo()); |
| 1346 | VTablePtr = Builder.CreateLoad(VTablePtr, "vtable"); |
| 1347 | |
| 1348 | int64_t VBaseOffsetIndex = |
| 1349 | CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl); |
| 1350 | |
| 1351 | llvm::Value *VBaseOffsetPtr = |
| 1352 | Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr"); |
| 1353 | const llvm::Type *PtrDiffTy = |
| 1354 | ConvertType(getContext().getPointerDiffType()); |
| 1355 | |
| 1356 | VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr, |
| 1357 | PtrDiffTy->getPointerTo()); |
| 1358 | |
| 1359 | llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset"); |
| 1360 | |
| 1361 | return VBaseOffset; |
| 1362 | } |
| 1363 | |
| 1364 | void CodeGenFunction::InitializeVtablePtrs(const CXXRecordDecl *ClassDecl) { |
| 1365 | if (!ClassDecl->isDynamicClass()) |
| 1366 | return; |
| 1367 | |
| 1368 | llvm::Constant *Vtable = CGM.getVtableInfo().getVtable(ClassDecl); |
Anders Carlsson | 21431c5 | 2010-01-02 18:02:32 +0000 | [diff] [blame] | 1369 | CGVtableInfo::AddrSubMap_t& AddressPoints = |
| 1370 | *(*CGM.getVtableInfo().AddressPoints[ClassDecl])[ClassDecl]; |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1371 | llvm::Value *ThisPtr = LoadCXXThis(); |
| 1372 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(ClassDecl); |
| 1373 | |
| 1374 | // Store address points for virtual bases |
| 1375 | for (CXXRecordDecl::base_class_const_iterator I = |
| 1376 | ClassDecl->vbases_begin(), E = ClassDecl->vbases_end(); I != E; ++I) { |
| 1377 | const CXXBaseSpecifier &Base = *I; |
| 1378 | CXXRecordDecl *BaseClassDecl |
| 1379 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| 1380 | uint64_t Offset = Layout.getVBaseClassOffset(BaseClassDecl); |
| 1381 | InitializeVtablePtrsRecursive(BaseClassDecl, Vtable, AddressPoints, |
| 1382 | ThisPtr, Offset); |
| 1383 | } |
| 1384 | |
| 1385 | // Store address points for non-virtual bases and current class |
| 1386 | InitializeVtablePtrsRecursive(ClassDecl, Vtable, AddressPoints, ThisPtr, 0); |
| 1387 | } |
| 1388 | |
| 1389 | void CodeGenFunction::InitializeVtablePtrsRecursive( |
| 1390 | const CXXRecordDecl *ClassDecl, |
| 1391 | llvm::Constant *Vtable, |
Anders Carlsson | 21431c5 | 2010-01-02 18:02:32 +0000 | [diff] [blame] | 1392 | CGVtableInfo::AddrSubMap_t& AddressPoints, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1393 | llvm::Value *ThisPtr, |
| 1394 | uint64_t Offset) { |
| 1395 | if (!ClassDecl->isDynamicClass()) |
| 1396 | return; |
| 1397 | |
| 1398 | // Store address points for non-virtual bases |
| 1399 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(ClassDecl); |
| 1400 | for (CXXRecordDecl::base_class_const_iterator I = |
| 1401 | ClassDecl->bases_begin(), E = ClassDecl->bases_end(); I != E; ++I) { |
| 1402 | const CXXBaseSpecifier &Base = *I; |
| 1403 | if (Base.isVirtual()) |
| 1404 | continue; |
| 1405 | CXXRecordDecl *BaseClassDecl |
| 1406 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| 1407 | uint64_t NewOffset = Offset + Layout.getBaseClassOffset(BaseClassDecl); |
| 1408 | InitializeVtablePtrsRecursive(BaseClassDecl, Vtable, AddressPoints, |
| 1409 | ThisPtr, NewOffset); |
| 1410 | } |
| 1411 | |
| 1412 | // Compute the address point |
| 1413 | assert(AddressPoints.count(std::make_pair(ClassDecl, Offset)) && |
| 1414 | "Missing address point for class"); |
| 1415 | uint64_t AddressPoint = AddressPoints[std::make_pair(ClassDecl, Offset)]; |
| 1416 | llvm::Value *VtableAddressPoint = |
| 1417 | Builder.CreateConstInBoundsGEP2_64(Vtable, 0, AddressPoint); |
| 1418 | |
| 1419 | // Compute the address to store the address point |
| 1420 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); |
| 1421 | llvm::Value *VtableField = Builder.CreateBitCast(ThisPtr, Int8PtrTy); |
| 1422 | VtableField = Builder.CreateConstInBoundsGEP1_64(VtableField, Offset/8); |
| 1423 | const llvm::Type *AddressPointPtrTy = |
| 1424 | VtableAddressPoint->getType()->getPointerTo(); |
| 1425 | VtableField = Builder.CreateBitCast(VtableField, AddressPointPtrTy); |
| 1426 | |
| 1427 | // Store address point |
| 1428 | Builder.CreateStore(VtableAddressPoint, VtableField); |
| 1429 | } |