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