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