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