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