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 | |
| 785 | // FIXME: This method of determining whether a base is virtual is ridiculous; |
| 786 | // it should be part of BaseInit. |
| 787 | bool isBaseVirtual = false; |
| 788 | for (CXXRecordDecl::base_class_const_iterator I = ClassDecl->vbases_begin(), |
| 789 | E = ClassDecl->vbases_end(); I != E; ++I) |
| 790 | if (I->getType()->getAs<RecordType>()->getDecl() == BaseClassDecl) { |
| 791 | isBaseVirtual = true; |
| 792 | break; |
| 793 | } |
| 794 | |
| 795 | // The base constructor doesn't construct virtual bases. |
| 796 | if (CtorType == Ctor_Base && isBaseVirtual) |
| 797 | return; |
| 798 | |
John McCall | bff225e | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 799 | // We can pretend to be a complete class because it only matters for |
| 800 | // virtual bases, and we only do virtual bases for complete ctors. |
| 801 | llvm::Value *V = ThisPtr; |
| 802 | V = CGF.GetAddressOfBaseOfCompleteClass(V, isBaseVirtual, |
| 803 | ClassDecl, BaseClassDecl); |
| 804 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 805 | CGF.EmitAggExpr(BaseInit->getInit(), V, false, false, true); |
Anders Carlsson | 594d5e8 | 2010-02-06 20:00:21 +0000 | [diff] [blame] | 806 | |
| 807 | if (CGF.Exceptions && !BaseClassDecl->hasTrivialDestructor()) { |
| 808 | // FIXME: Is this OK for C++0x delegating constructors? |
| 809 | CodeGenFunction::EHCleanupBlock Cleanup(CGF); |
| 810 | |
Anders Carlsson | 594d5e8 | 2010-02-06 20:00:21 +0000 | [diff] [blame] | 811 | CXXDestructorDecl *DD = BaseClassDecl->getDestructor(CGF.getContext()); |
| 812 | CGF.EmitCXXDestructorCall(DD, Dtor_Base, V); |
| 813 | } |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | static void EmitMemberInitializer(CodeGenFunction &CGF, |
| 817 | const CXXRecordDecl *ClassDecl, |
| 818 | CXXBaseOrMemberInitializer *MemberInit) { |
| 819 | assert(MemberInit->isMemberInitializer() && |
| 820 | "Must have member initializer!"); |
| 821 | |
| 822 | // non-static data member initializers. |
| 823 | FieldDecl *Field = MemberInit->getMember(); |
| 824 | QualType FieldType = CGF.getContext().getCanonicalType(Field->getType()); |
| 825 | |
| 826 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
Anders Carlsson | 06a2970 | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 827 | LValue LHS = CGF.EmitLValueForFieldInitialization(ThisPtr, Field, 0); |
| 828 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 829 | // If we are initializing an anonymous union field, drill down to the field. |
| 830 | if (MemberInit->getAnonUnionMember()) { |
| 831 | Field = MemberInit->getAnonUnionMember(); |
Anders Carlsson | e6d2a53 | 2010-01-29 05:05:36 +0000 | [diff] [blame] | 832 | LHS = CGF.EmitLValueForField(LHS.getAddress(), Field, 0); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 833 | FieldType = Field->getType(); |
| 834 | } |
| 835 | |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 836 | // FIXME: If there's no initializer and the CXXBaseOrMemberInitializer |
| 837 | // was implicitly generated, we shouldn't be zeroing memory. |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 838 | RValue RHS; |
| 839 | if (FieldType->isReferenceType()) { |
Anders Carlsson | a64a869 | 2010-02-03 16:38:03 +0000 | [diff] [blame] | 840 | RHS = CGF.EmitReferenceBindingToExpr(MemberInit->getInit(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 841 | /*IsInitializer=*/true); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 842 | CGF.EmitStoreThroughLValue(RHS, LHS, FieldType); |
Eli Friedman | 3bb9412 | 2010-01-31 19:07:50 +0000 | [diff] [blame] | 843 | } else if (FieldType->isArrayType() && !MemberInit->getInit()) { |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 844 | CGF.EmitMemSetToZero(LHS.getAddress(), Field->getType()); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 845 | } else if (!CGF.hasAggregateLLVMType(Field->getType())) { |
| 846 | RHS = RValue::get(CGF.EmitScalarExpr(MemberInit->getInit(), true)); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 847 | CGF.EmitStoreThroughLValue(RHS, LHS, FieldType); |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 848 | } else if (MemberInit->getInit()->getType()->isAnyComplexType()) { |
| 849 | CGF.EmitComplexExprIntoAddr(MemberInit->getInit(), LHS.getAddress(), |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 850 | LHS.isVolatileQualified()); |
| 851 | } else { |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 852 | CGF.EmitAggExpr(MemberInit->getInit(), LHS.getAddress(), |
| 853 | LHS.isVolatileQualified(), false, true); |
Anders Carlsson | 9405dcd | 2010-02-06 19:50:17 +0000 | [diff] [blame] | 854 | |
| 855 | if (!CGF.Exceptions) |
| 856 | return; |
| 857 | |
| 858 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 859 | if (!RT) |
| 860 | return; |
| 861 | |
| 862 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 863 | if (!RD->hasTrivialDestructor()) { |
| 864 | // FIXME: Is this OK for C++0x delegating constructors? |
| 865 | CodeGenFunction::EHCleanupBlock Cleanup(CGF); |
| 866 | |
| 867 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
| 868 | LValue LHS = CGF.EmitLValueForField(ThisPtr, Field, 0); |
| 869 | |
| 870 | CXXDestructorDecl *DD = RD->getDestructor(CGF.getContext()); |
| 871 | CGF.EmitCXXDestructorCall(DD, Dtor_Complete, LHS.getAddress()); |
| 872 | } |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 876 | /// Checks whether the given constructor is a valid subject for the |
| 877 | /// complete-to-base constructor delegation optimization, i.e. |
| 878 | /// emitting the complete constructor as a simple call to the base |
| 879 | /// constructor. |
| 880 | static bool IsConstructorDelegationValid(const CXXConstructorDecl *Ctor) { |
| 881 | |
| 882 | // Currently we disable the optimization for classes with virtual |
| 883 | // bases because (1) the addresses of parameter variables need to be |
| 884 | // consistent across all initializers but (2) the delegate function |
| 885 | // call necessarily creates a second copy of the parameter variable. |
| 886 | // |
| 887 | // The limiting example (purely theoretical AFAIK): |
| 888 | // struct A { A(int &c) { c++; } }; |
| 889 | // struct B : virtual A { |
| 890 | // B(int count) : A(count) { printf("%d\n", count); } |
| 891 | // }; |
| 892 | // ...although even this example could in principle be emitted as a |
| 893 | // delegation since the address of the parameter doesn't escape. |
| 894 | if (Ctor->getParent()->getNumVBases()) { |
| 895 | // TODO: white-list trivial vbase initializers. This case wouldn't |
| 896 | // be subject to the restrictions below. |
| 897 | |
| 898 | // TODO: white-list cases where: |
| 899 | // - there are no non-reference parameters to the constructor |
| 900 | // - the initializers don't access any non-reference parameters |
| 901 | // - the initializers don't take the address of non-reference |
| 902 | // parameters |
| 903 | // - etc. |
| 904 | // If we ever add any of the above cases, remember that: |
| 905 | // - function-try-blocks will always blacklist this optimization |
| 906 | // - we need to perform the constructor prologue and cleanup in |
| 907 | // EmitConstructorBody. |
| 908 | |
| 909 | return false; |
| 910 | } |
| 911 | |
| 912 | // We also disable the optimization for variadic functions because |
| 913 | // it's impossible to "re-pass" varargs. |
| 914 | if (Ctor->getType()->getAs<FunctionProtoType>()->isVariadic()) |
| 915 | return false; |
| 916 | |
| 917 | return true; |
| 918 | } |
| 919 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 920 | /// EmitConstructorBody - Emits the body of the current constructor. |
| 921 | void CodeGenFunction::EmitConstructorBody(FunctionArgList &Args) { |
| 922 | const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(CurGD.getDecl()); |
| 923 | CXXCtorType CtorType = CurGD.getCtorType(); |
| 924 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 925 | // Before we go any further, try the complete->base constructor |
| 926 | // delegation optimization. |
| 927 | if (CtorType == Ctor_Complete && IsConstructorDelegationValid(Ctor)) { |
| 928 | EmitDelegateCXXConstructorCall(Ctor, Ctor_Base, Args); |
| 929 | return; |
| 930 | } |
| 931 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 932 | Stmt *Body = Ctor->getBody(); |
| 933 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 934 | // Enter the function-try-block before the constructor prologue if |
| 935 | // applicable. |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 936 | CXXTryStmtInfo TryInfo; |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 937 | bool IsTryBody = (Body && isa<CXXTryStmt>(Body)); |
| 938 | |
| 939 | if (IsTryBody) |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 940 | TryInfo = EnterCXXTryStmt(*cast<CXXTryStmt>(Body)); |
| 941 | |
| 942 | unsigned CleanupStackSize = CleanupEntries.size(); |
| 943 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 944 | // Emit the constructor prologue, i.e. the base and member |
| 945 | // initializers. |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 946 | EmitCtorPrologue(Ctor, CtorType); |
| 947 | |
| 948 | // Emit the body of the statement. |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 949 | if (IsTryBody) |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 950 | EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock()); |
| 951 | else if (Body) |
| 952 | EmitStmt(Body); |
| 953 | else { |
| 954 | assert(Ctor->isImplicit() && "bodyless ctor not implicit"); |
| 955 | if (!Ctor->isDefaultConstructor()) { |
| 956 | assert(Ctor->isCopyConstructor()); |
| 957 | SynthesizeCXXCopyConstructor(Args); |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | // Emit any cleanup blocks associated with the member or base |
| 962 | // initializers, which includes (along the exceptional path) the |
| 963 | // destructors for those members and bases that were fully |
| 964 | // constructed. |
| 965 | EmitCleanupBlocks(CleanupStackSize); |
| 966 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 967 | if (IsTryBody) |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 968 | ExitCXXTryStmt(*cast<CXXTryStmt>(Body), TryInfo); |
| 969 | } |
| 970 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 971 | /// EmitCtorPrologue - This routine generates necessary code to initialize |
| 972 | /// base classes and non-static data members belonging to this constructor. |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 973 | void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD, |
| 974 | CXXCtorType CtorType) { |
| 975 | const CXXRecordDecl *ClassDecl = CD->getParent(); |
Anders Carlsson | a78fa2c | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 976 | |
| 977 | llvm::SmallVector<CXXBaseOrMemberInitializer *, 8> MemberInitializers; |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 978 | |
| 979 | // FIXME: Add vbase initialization |
| 980 | |
| 981 | for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(), |
| 982 | E = CD->init_end(); |
| 983 | B != E; ++B) { |
| 984 | CXXBaseOrMemberInitializer *Member = (*B); |
| 985 | |
| 986 | assert(LiveTemporaries.empty() && |
| 987 | "Should not have any live temporaries at initializer start!"); |
| 988 | |
| 989 | if (Member->isBaseInitializer()) |
| 990 | EmitBaseInitializer(*this, ClassDecl, Member, CtorType); |
| 991 | else |
Anders Carlsson | a78fa2c | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 992 | MemberInitializers.push_back(Member); |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 993 | } |
| 994 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 995 | InitializeVTablePointers(ClassDecl); |
Anders Carlsson | a78fa2c | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 996 | |
| 997 | for (unsigned I = 0, E = MemberInitializers.size(); I != E; ++I) { |
| 998 | assert(LiveTemporaries.empty() && |
| 999 | "Should not have any live temporaries at initializer start!"); |
| 1000 | |
| 1001 | EmitMemberInitializer(*this, ClassDecl, MemberInitializers[I]); |
| 1002 | } |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1005 | /// EmitDestructorBody - Emits the body of the current destructor. |
| 1006 | void CodeGenFunction::EmitDestructorBody(FunctionArgList &Args) { |
| 1007 | const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(CurGD.getDecl()); |
| 1008 | CXXDtorType DtorType = CurGD.getDtorType(); |
| 1009 | |
| 1010 | Stmt *Body = Dtor->getBody(); |
| 1011 | |
| 1012 | // If the body is a function-try-block, enter the try before |
| 1013 | // anything else --- unless we're in a deleting destructor, in which |
| 1014 | // case we're just going to call the complete destructor and then |
| 1015 | // call operator delete() on the way out. |
| 1016 | CXXTryStmtInfo TryInfo; |
| 1017 | bool isTryBody = (DtorType != Dtor_Deleting && |
| 1018 | Body && isa<CXXTryStmt>(Body)); |
| 1019 | if (isTryBody) |
| 1020 | TryInfo = EnterCXXTryStmt(*cast<CXXTryStmt>(Body)); |
| 1021 | |
| 1022 | llvm::BasicBlock *DtorEpilogue = createBasicBlock("dtor.epilogue"); |
| 1023 | PushCleanupBlock(DtorEpilogue); |
| 1024 | |
| 1025 | bool SkipBody = false; // should get jump-threaded |
| 1026 | |
| 1027 | // If this is the deleting variant, just invoke the complete |
| 1028 | // variant, then call the appropriate operator delete() on the way |
| 1029 | // out. |
| 1030 | if (DtorType == Dtor_Deleting) { |
| 1031 | EmitCXXDestructorCall(Dtor, Dtor_Complete, LoadCXXThis()); |
| 1032 | SkipBody = true; |
| 1033 | |
| 1034 | // If this is the complete variant, just invoke the base variant; |
| 1035 | // the epilogue will destruct the virtual bases. But we can't do |
| 1036 | // this optimization if the body is a function-try-block, because |
| 1037 | // we'd introduce *two* handler blocks. |
| 1038 | } else if (!isTryBody && DtorType == Dtor_Complete) { |
| 1039 | EmitCXXDestructorCall(Dtor, Dtor_Base, LoadCXXThis()); |
| 1040 | SkipBody = true; |
| 1041 | |
| 1042 | // Otherwise, we're in the base variant, so we need to ensure the |
| 1043 | // vtable ptrs are right before emitting the body. |
| 1044 | } else { |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1045 | InitializeVTablePointers(Dtor->getParent()); |
John McCall | 9fc6a77 | 2010-02-19 09:25:03 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | // Emit the body of the statement. |
| 1049 | if (SkipBody) |
| 1050 | (void) 0; |
| 1051 | else if (isTryBody) |
| 1052 | EmitStmt(cast<CXXTryStmt>(Body)->getTryBlock()); |
| 1053 | else if (Body) |
| 1054 | EmitStmt(Body); |
| 1055 | else { |
| 1056 | assert(Dtor->isImplicit() && "bodyless dtor not implicit"); |
| 1057 | // nothing to do besides what's in the epilogue |
| 1058 | } |
| 1059 | |
| 1060 | // Jump to the cleanup block. |
| 1061 | CleanupBlockInfo Info = PopCleanupBlock(); |
| 1062 | assert(Info.CleanupBlock == DtorEpilogue && "Block mismatch!"); |
| 1063 | EmitBlock(DtorEpilogue); |
| 1064 | |
| 1065 | // Emit the destructor epilogue now. If this is a complete |
| 1066 | // destructor with a function-try-block, perform the base epilogue |
| 1067 | // as well. |
| 1068 | if (isTryBody && DtorType == Dtor_Complete) |
| 1069 | EmitDtorEpilogue(Dtor, Dtor_Base); |
| 1070 | EmitDtorEpilogue(Dtor, DtorType); |
| 1071 | |
| 1072 | // Link up the cleanup information. |
| 1073 | if (Info.SwitchBlock) |
| 1074 | EmitBlock(Info.SwitchBlock); |
| 1075 | if (Info.EndBlock) |
| 1076 | EmitBlock(Info.EndBlock); |
| 1077 | |
| 1078 | // Exit the try if applicable. |
| 1079 | if (isTryBody) |
| 1080 | ExitCXXTryStmt(*cast<CXXTryStmt>(Body), TryInfo); |
| 1081 | } |
| 1082 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1083 | /// EmitDtorEpilogue - Emit all code that comes at the end of class's |
| 1084 | /// destructor. This is to call destructors on members and base classes |
| 1085 | /// in reverse order of their construction. |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1086 | void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD, |
| 1087 | CXXDtorType DtorType) { |
| 1088 | assert(!DD->isTrivial() && |
| 1089 | "Should not emit dtor epilogue for trivial dtor!"); |
| 1090 | |
| 1091 | const CXXRecordDecl *ClassDecl = DD->getParent(); |
| 1092 | |
John McCall | 3b47733 | 2010-02-18 19:59:28 +0000 | [diff] [blame] | 1093 | // In a deleting destructor, we've already called the complete |
| 1094 | // destructor as a subroutine, so we just have to delete the |
| 1095 | // appropriate value. |
| 1096 | if (DtorType == Dtor_Deleting) { |
| 1097 | assert(DD->getOperatorDelete() && |
| 1098 | "operator delete missing - EmitDtorEpilogue"); |
| 1099 | EmitDeleteCall(DD->getOperatorDelete(), LoadCXXThis(), |
| 1100 | getContext().getTagDeclType(ClassDecl)); |
| 1101 | return; |
| 1102 | } |
| 1103 | |
| 1104 | // For complete destructors, we've already called the base |
| 1105 | // destructor (in GenerateBody), so we just need to destruct all the |
| 1106 | // virtual bases. |
| 1107 | if (DtorType == Dtor_Complete) { |
| 1108 | // Handle virtual bases. |
| 1109 | for (CXXRecordDecl::reverse_base_class_const_iterator I = |
| 1110 | ClassDecl->vbases_rbegin(), E = ClassDecl->vbases_rend(); |
| 1111 | I != E; ++I) { |
| 1112 | const CXXBaseSpecifier &Base = *I; |
| 1113 | CXXRecordDecl *BaseClassDecl |
| 1114 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| 1115 | |
| 1116 | // Ignore trivial destructors. |
| 1117 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1118 | continue; |
| 1119 | const CXXDestructorDecl *D = BaseClassDecl->getDestructor(getContext()); |
| 1120 | llvm::Value *V = GetAddressOfBaseOfCompleteClass(LoadCXXThis(), |
| 1121 | true, |
| 1122 | ClassDecl, |
| 1123 | BaseClassDecl); |
| 1124 | EmitCXXDestructorCall(D, Dtor_Base, V); |
| 1125 | } |
| 1126 | return; |
| 1127 | } |
| 1128 | |
| 1129 | assert(DtorType == Dtor_Base); |
| 1130 | |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1131 | // Collect the fields. |
| 1132 | llvm::SmallVector<const FieldDecl *, 16> FieldDecls; |
| 1133 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 1134 | E = ClassDecl->field_end(); I != E; ++I) { |
| 1135 | const FieldDecl *Field = *I; |
| 1136 | |
| 1137 | QualType FieldType = getContext().getCanonicalType(Field->getType()); |
| 1138 | FieldType = getContext().getBaseElementType(FieldType); |
| 1139 | |
| 1140 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 1141 | if (!RT) |
| 1142 | continue; |
| 1143 | |
| 1144 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 1145 | if (FieldClassDecl->hasTrivialDestructor()) |
| 1146 | continue; |
| 1147 | |
| 1148 | FieldDecls.push_back(Field); |
| 1149 | } |
| 1150 | |
| 1151 | // Now destroy the fields. |
| 1152 | for (size_t i = FieldDecls.size(); i > 0; --i) { |
| 1153 | const FieldDecl *Field = FieldDecls[i - 1]; |
| 1154 | |
| 1155 | QualType FieldType = Field->getType(); |
| 1156 | const ConstantArrayType *Array = |
| 1157 | getContext().getAsConstantArrayType(FieldType); |
| 1158 | if (Array) |
| 1159 | FieldType = getContext().getBaseElementType(FieldType); |
| 1160 | |
| 1161 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 1162 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 1163 | |
| 1164 | llvm::Value *ThisPtr = LoadCXXThis(); |
| 1165 | |
| 1166 | LValue LHS = EmitLValueForField(ThisPtr, Field, |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1167 | // FIXME: Qualifiers? |
| 1168 | /*CVRQualifiers=*/0); |
| 1169 | if (Array) { |
| 1170 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 1171 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 1172 | llvm::Value *BaseAddrPtr = |
| 1173 | Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
| 1174 | EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()), |
| 1175 | Array, BaseAddrPtr); |
| 1176 | } else |
| 1177 | EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()), |
| 1178 | Dtor_Complete, LHS.getAddress()); |
| 1179 | } |
| 1180 | |
| 1181 | // Destroy non-virtual bases. |
| 1182 | for (CXXRecordDecl::reverse_base_class_const_iterator I = |
| 1183 | ClassDecl->bases_rbegin(), E = ClassDecl->bases_rend(); I != E; ++I) { |
| 1184 | const CXXBaseSpecifier &Base = *I; |
| 1185 | |
| 1186 | // Ignore virtual bases. |
| 1187 | if (Base.isVirtual()) |
| 1188 | continue; |
| 1189 | |
| 1190 | CXXRecordDecl *BaseClassDecl |
| 1191 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| 1192 | |
| 1193 | // Ignore trivial destructors. |
| 1194 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1195 | continue; |
| 1196 | const CXXDestructorDecl *D = BaseClassDecl->getDestructor(getContext()); |
| 1197 | |
| 1198 | llvm::Value *V = GetAddressOfBaseClass(LoadCXXThis(), |
| 1199 | ClassDecl, BaseClassDecl, |
| 1200 | /*NullCheckValue=*/false); |
| 1201 | EmitCXXDestructorCall(D, Dtor_Base, V); |
| 1202 | } |
Anders Carlsson | 607d037 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1205 | /// EmitCXXAggrConstructorCall - This routine essentially creates a (nested) |
| 1206 | /// for-loop to call the default constructor on individual members of the |
| 1207 | /// array. |
| 1208 | /// 'D' is the default constructor for elements of the array, 'ArrayTy' is the |
| 1209 | /// array type and 'ArrayPtr' points to the beginning fo the array. |
| 1210 | /// It is assumed that all relevant checks have been made by the caller. |
| 1211 | void |
| 1212 | CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
| 1213 | const ConstantArrayType *ArrayTy, |
| 1214 | llvm::Value *ArrayPtr, |
| 1215 | CallExpr::const_arg_iterator ArgBeg, |
| 1216 | CallExpr::const_arg_iterator ArgEnd) { |
| 1217 | |
| 1218 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 1219 | llvm::Value * NumElements = |
| 1220 | llvm::ConstantInt::get(SizeTy, |
| 1221 | getContext().getConstantArrayElementCount(ArrayTy)); |
| 1222 | |
| 1223 | EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr, ArgBeg, ArgEnd); |
| 1224 | } |
| 1225 | |
| 1226 | void |
| 1227 | CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
| 1228 | llvm::Value *NumElements, |
| 1229 | llvm::Value *ArrayPtr, |
| 1230 | CallExpr::const_arg_iterator ArgBeg, |
| 1231 | CallExpr::const_arg_iterator ArgEnd) { |
| 1232 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 1233 | |
| 1234 | // Create a temporary for the loop index and initialize it with 0. |
| 1235 | llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index"); |
| 1236 | llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy); |
| 1237 | Builder.CreateStore(Zero, IndexPtr); |
| 1238 | |
| 1239 | // Start the loop with a block that tests the condition. |
| 1240 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 1241 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
| 1242 | |
| 1243 | EmitBlock(CondBlock); |
| 1244 | |
| 1245 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 1246 | |
| 1247 | // Generate: if (loop-index < number-of-elements fall to the loop body, |
| 1248 | // otherwise, go to the block after the for-loop. |
| 1249 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 1250 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless"); |
| 1251 | // If the condition is true, execute the body. |
| 1252 | Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
| 1253 | |
| 1254 | EmitBlock(ForBody); |
| 1255 | |
| 1256 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 1257 | // Inside the loop body, emit the constructor call on the array element. |
| 1258 | Counter = Builder.CreateLoad(IndexPtr); |
| 1259 | llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter, |
| 1260 | "arrayidx"); |
| 1261 | |
| 1262 | // C++ [class.temporary]p4: |
| 1263 | // There are two contexts in which temporaries are destroyed at a different |
| 1264 | // point than the end of the full-expression. The first context is when a |
| 1265 | // default constructor is called to initialize an element of an array. |
| 1266 | // If the constructor has one or more default arguments, the destruction of |
| 1267 | // every temporary created in a default argument expression is sequenced |
| 1268 | // before the construction of the next array element, if any. |
| 1269 | |
| 1270 | // Keep track of the current number of live temporaries. |
Anders Carlsson | 44ec82b | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 1271 | { |
| 1272 | CXXTemporariesCleanupScope Scope(*this); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1273 | |
Anders Carlsson | 44ec82b | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 1274 | EmitCXXConstructorCall(D, Ctor_Complete, Address, ArgBeg, ArgEnd); |
| 1275 | } |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1276 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1277 | EmitBlock(ContinueBlock); |
| 1278 | |
| 1279 | // Emit the increment of the loop counter. |
| 1280 | llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1); |
| 1281 | Counter = Builder.CreateLoad(IndexPtr); |
| 1282 | NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); |
| 1283 | Builder.CreateStore(NextVal, IndexPtr); |
| 1284 | |
| 1285 | // Finally, branch back up to the condition for the next iteration. |
| 1286 | EmitBranch(CondBlock); |
| 1287 | |
| 1288 | // Emit the fall-through block. |
| 1289 | EmitBlock(AfterFor, true); |
| 1290 | } |
| 1291 | |
| 1292 | /// EmitCXXAggrDestructorCall - calls the default destructor on array |
| 1293 | /// elements in reverse order of construction. |
| 1294 | void |
| 1295 | CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, |
| 1296 | const ArrayType *Array, |
| 1297 | llvm::Value *This) { |
| 1298 | const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); |
| 1299 | assert(CA && "Do we support VLA for destruction ?"); |
| 1300 | uint64_t ElementCount = getContext().getConstantArrayElementCount(CA); |
| 1301 | |
| 1302 | const llvm::Type *SizeLTy = ConvertType(getContext().getSizeType()); |
| 1303 | llvm::Value* ElementCountPtr = llvm::ConstantInt::get(SizeLTy, ElementCount); |
| 1304 | EmitCXXAggrDestructorCall(D, ElementCountPtr, This); |
| 1305 | } |
| 1306 | |
| 1307 | /// EmitCXXAggrDestructorCall - calls the default destructor on array |
| 1308 | /// elements in reverse order of construction. |
| 1309 | void |
| 1310 | CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, |
| 1311 | llvm::Value *UpperCount, |
| 1312 | llvm::Value *This) { |
| 1313 | const llvm::Type *SizeLTy = ConvertType(getContext().getSizeType()); |
| 1314 | llvm::Value *One = llvm::ConstantInt::get(SizeLTy, 1); |
| 1315 | |
| 1316 | // Create a temporary for the loop index and initialize it with count of |
| 1317 | // array elements. |
| 1318 | llvm::Value *IndexPtr = CreateTempAlloca(SizeLTy, "loop.index"); |
| 1319 | |
| 1320 | // Store the number of elements in the index pointer. |
| 1321 | Builder.CreateStore(UpperCount, IndexPtr); |
| 1322 | |
| 1323 | // Start the loop with a block that tests the condition. |
| 1324 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 1325 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
| 1326 | |
| 1327 | EmitBlock(CondBlock); |
| 1328 | |
| 1329 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 1330 | |
| 1331 | // Generate: if (loop-index != 0 fall to the loop body, |
| 1332 | // otherwise, go to the block after the for-loop. |
| 1333 | llvm::Value* zeroConstant = |
| 1334 | llvm::Constant::getNullValue(SizeLTy); |
| 1335 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 1336 | llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant, |
| 1337 | "isne"); |
| 1338 | // If the condition is true, execute the body. |
| 1339 | Builder.CreateCondBr(IsNE, ForBody, AfterFor); |
| 1340 | |
| 1341 | EmitBlock(ForBody); |
| 1342 | |
| 1343 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 1344 | // Inside the loop body, emit the constructor call on the array element. |
| 1345 | Counter = Builder.CreateLoad(IndexPtr); |
| 1346 | Counter = Builder.CreateSub(Counter, One); |
| 1347 | llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx"); |
| 1348 | EmitCXXDestructorCall(D, Dtor_Complete, Address); |
| 1349 | |
| 1350 | EmitBlock(ContinueBlock); |
| 1351 | |
| 1352 | // Emit the decrement of the loop counter. |
| 1353 | Counter = Builder.CreateLoad(IndexPtr); |
| 1354 | Counter = Builder.CreateSub(Counter, One, "dec"); |
| 1355 | Builder.CreateStore(Counter, IndexPtr); |
| 1356 | |
| 1357 | // Finally, branch back up to the condition for the next iteration. |
| 1358 | EmitBranch(CondBlock); |
| 1359 | |
| 1360 | // Emit the fall-through block. |
| 1361 | EmitBlock(AfterFor, true); |
| 1362 | } |
| 1363 | |
| 1364 | /// GenerateCXXAggrDestructorHelper - Generates a helper function which when |
| 1365 | /// invoked, calls the default destructor on array elements in reverse order of |
| 1366 | /// construction. |
| 1367 | llvm::Constant * |
| 1368 | CodeGenFunction::GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D, |
| 1369 | const ArrayType *Array, |
| 1370 | llvm::Value *This) { |
| 1371 | FunctionArgList Args; |
| 1372 | ImplicitParamDecl *Dst = |
| 1373 | ImplicitParamDecl::Create(getContext(), 0, |
| 1374 | SourceLocation(), 0, |
| 1375 | getContext().getPointerType(getContext().VoidTy)); |
| 1376 | Args.push_back(std::make_pair(Dst, Dst->getType())); |
| 1377 | |
| 1378 | llvm::SmallString<16> Name; |
| 1379 | llvm::raw_svector_ostream(Name) << "__tcf_" << (++UniqueAggrDestructorCount); |
| 1380 | QualType R = getContext().VoidTy; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1381 | const CGFunctionInfo &FI |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame^] | 1382 | = CGM.getTypes().getFunctionInfo(R, Args, FunctionType::ExtInfo()); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1383 | const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false); |
| 1384 | llvm::Function *Fn = |
| 1385 | llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, |
| 1386 | Name.str(), |
| 1387 | &CGM.getModule()); |
| 1388 | IdentifierInfo *II = &CGM.getContext().Idents.get(Name.str()); |
| 1389 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 1390 | getContext().getTranslationUnitDecl(), |
| 1391 | SourceLocation(), II, R, 0, |
| 1392 | FunctionDecl::Static, |
| 1393 | false, true); |
| 1394 | StartFunction(FD, R, Fn, Args, SourceLocation()); |
| 1395 | QualType BaseElementTy = getContext().getBaseElementType(Array); |
| 1396 | const llvm::Type *BasePtr = ConvertType(BaseElementTy); |
| 1397 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 1398 | llvm::Value *BaseAddrPtr = Builder.CreateBitCast(This, BasePtr); |
| 1399 | EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr); |
| 1400 | FinishFunction(); |
| 1401 | llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), |
| 1402 | 0); |
| 1403 | llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); |
| 1404 | return m; |
| 1405 | } |
| 1406 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1407 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1408 | void |
| 1409 | CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, |
| 1410 | CXXCtorType Type, |
| 1411 | llvm::Value *This, |
| 1412 | CallExpr::const_arg_iterator ArgBeg, |
| 1413 | CallExpr::const_arg_iterator ArgEnd) { |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1414 | if (D->isTrivial()) { |
| 1415 | if (ArgBeg == ArgEnd) { |
| 1416 | // Trivial default constructor, no codegen required. |
| 1417 | assert(D->isDefaultConstructor() && |
| 1418 | "trivial 0-arg ctor not a default ctor"); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1419 | return; |
| 1420 | } |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1421 | |
| 1422 | assert(ArgBeg + 1 == ArgEnd && "unexpected argcount for trivial ctor"); |
| 1423 | assert(D->isCopyConstructor() && "trivial 1-arg ctor not a copy ctor"); |
| 1424 | |
John McCall | 8b6bbeb | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1425 | const Expr *E = (*ArgBeg); |
| 1426 | QualType Ty = E->getType(); |
| 1427 | llvm::Value *Src = EmitLValue(E).getAddress(); |
| 1428 | EmitAggregateCopy(This, Src, Ty); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1429 | return; |
| 1430 | } |
| 1431 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1432 | llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(D, Type)); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1433 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type); |
| 1434 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1435 | EmitCXXMemberCall(D, Callee, ReturnValueSlot(), This, VTT, ArgBeg, ArgEnd); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1438 | void |
| 1439 | CodeGenFunction::EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor, |
| 1440 | CXXCtorType CtorType, |
| 1441 | const FunctionArgList &Args) { |
| 1442 | CallArgList DelegateArgs; |
| 1443 | |
| 1444 | FunctionArgList::const_iterator I = Args.begin(), E = Args.end(); |
| 1445 | assert(I != E && "no parameters to constructor"); |
| 1446 | |
| 1447 | // this |
| 1448 | DelegateArgs.push_back(std::make_pair(RValue::get(LoadCXXThis()), |
| 1449 | I->second)); |
| 1450 | ++I; |
| 1451 | |
| 1452 | // vtt |
| 1453 | if (llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(Ctor, CtorType))) { |
| 1454 | QualType VoidPP = getContext().getPointerType(getContext().VoidPtrTy); |
| 1455 | DelegateArgs.push_back(std::make_pair(RValue::get(VTT), VoidPP)); |
| 1456 | |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 1457 | if (CodeGenVTables::needsVTTParameter(CurGD)) { |
John McCall | c0bf462 | 2010-02-23 00:48:20 +0000 | [diff] [blame] | 1458 | assert(I != E && "cannot skip vtt parameter, already done with args"); |
| 1459 | assert(I->second == VoidPP && "skipping parameter not of vtt type"); |
| 1460 | ++I; |
| 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | // Explicit arguments. |
| 1465 | for (; I != E; ++I) { |
| 1466 | |
| 1467 | const VarDecl *Param = I->first; |
| 1468 | QualType ArgType = Param->getType(); // because we're passing it to itself |
| 1469 | |
| 1470 | // StartFunction converted the ABI-lowered parameter(s) into a |
| 1471 | // local alloca. We need to turn that into an r-value suitable |
| 1472 | // for EmitCall. |
| 1473 | llvm::Value *Local = GetAddrOfLocalVar(Param); |
| 1474 | RValue Arg; |
| 1475 | |
| 1476 | // For the most part, we just need to load the alloca, except: |
| 1477 | // 1) aggregate r-values are actually pointers to temporaries, and |
| 1478 | // 2) references to aggregates are pointers directly to the aggregate. |
| 1479 | // I don't know why references to non-aggregates are different here. |
| 1480 | if (ArgType->isReferenceType()) { |
| 1481 | const ReferenceType *RefType = ArgType->getAs<ReferenceType>(); |
| 1482 | if (hasAggregateLLVMType(RefType->getPointeeType())) |
| 1483 | Arg = RValue::getAggregate(Local); |
| 1484 | else |
| 1485 | // Locals which are references to scalars are represented |
| 1486 | // with allocas holding the pointer. |
| 1487 | Arg = RValue::get(Builder.CreateLoad(Local)); |
| 1488 | } else { |
| 1489 | if (hasAggregateLLVMType(ArgType)) |
| 1490 | Arg = RValue::getAggregate(Local); |
| 1491 | else |
| 1492 | Arg = RValue::get(EmitLoadOfScalar(Local, false, ArgType)); |
| 1493 | } |
| 1494 | |
| 1495 | DelegateArgs.push_back(std::make_pair(Arg, ArgType)); |
| 1496 | } |
| 1497 | |
| 1498 | EmitCall(CGM.getTypes().getFunctionInfo(Ctor, CtorType), |
| 1499 | CGM.GetAddrOfCXXConstructor(Ctor, CtorType), |
| 1500 | ReturnValueSlot(), DelegateArgs, Ctor); |
| 1501 | } |
| 1502 | |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1503 | void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *DD, |
| 1504 | CXXDtorType Type, |
| 1505 | llvm::Value *This) { |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1506 | llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(DD, Type)); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1507 | llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(DD, Type); |
| 1508 | |
Anders Carlsson | c997d42 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1509 | EmitCXXMemberCall(DD, Callee, ReturnValueSlot(), This, VTT, 0, 0); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | llvm::Value * |
Anders Carlsson | bb7e17b | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 1513 | CodeGenFunction::GetVirtualBaseClassOffset(llvm::Value *This, |
| 1514 | const CXXRecordDecl *ClassDecl, |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1515 | const CXXRecordDecl *BaseClassDecl) { |
| 1516 | const llvm::Type *Int8PtrTy = |
| 1517 | llvm::Type::getInt8Ty(VMContext)->getPointerTo(); |
| 1518 | |
| 1519 | llvm::Value *VTablePtr = Builder.CreateBitCast(This, |
| 1520 | Int8PtrTy->getPointerTo()); |
| 1521 | VTablePtr = Builder.CreateLoad(VTablePtr, "vtable"); |
| 1522 | |
Anders Carlsson | bba1607 | 2010-03-11 07:15:17 +0000 | [diff] [blame] | 1523 | int64_t VBaseOffsetOffset = |
Anders Carlsson | af44035 | 2010-03-23 04:11:45 +0000 | [diff] [blame] | 1524 | CGM.getVTables().getVirtualBaseOffsetOffset(ClassDecl, BaseClassDecl); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1525 | |
| 1526 | llvm::Value *VBaseOffsetPtr = |
Anders Carlsson | bba1607 | 2010-03-11 07:15:17 +0000 | [diff] [blame] | 1527 | Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetOffset, "vbase.offset.ptr"); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1528 | const llvm::Type *PtrDiffTy = |
| 1529 | ConvertType(getContext().getPointerDiffType()); |
| 1530 | |
| 1531 | VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr, |
| 1532 | PtrDiffTy->getPointerTo()); |
| 1533 | |
| 1534 | llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset"); |
| 1535 | |
| 1536 | return VBaseOffset; |
| 1537 | } |
| 1538 | |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1539 | void |
| 1540 | CodeGenFunction::InitializeVTablePointer(BaseSubobject Base, |
| 1541 | bool BaseIsMorallyVirtual, |
| 1542 | llvm::Constant *VTable, |
| 1543 | const CXXRecordDecl *VTableClass) { |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1544 | const CXXRecordDecl *RD = Base.getBase(); |
| 1545 | |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1546 | // Compute the address point. |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1547 | llvm::Value *VTableAddressPoint; |
Anders Carlsson | 851853d | 2010-03-29 02:38:51 +0000 | [diff] [blame] | 1548 | |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1549 | // Check if we need to use a vtable from the VTT. |
Anders Carlsson | 851853d | 2010-03-29 02:38:51 +0000 | [diff] [blame] | 1550 | if (CodeGenVTables::needsVTTParameter(CurGD) && |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1551 | (RD->getNumVBases() || BaseIsMorallyVirtual)) { |
| 1552 | // Get the secondary vpointer index. |
| 1553 | uint64_t VirtualPointerIndex = |
| 1554 | CGM.getVTables().getSecondaryVirtualPointerIndex(VTableClass, Base); |
| 1555 | |
| 1556 | /// Load the VTT. |
| 1557 | llvm::Value *VTT = LoadCXXVTT(); |
| 1558 | if (VirtualPointerIndex) |
| 1559 | VTT = Builder.CreateConstInBoundsGEP1_64(VTT, VirtualPointerIndex); |
| 1560 | |
| 1561 | // And load the address point from the VTT. |
| 1562 | VTableAddressPoint = Builder.CreateLoad(VTT); |
| 1563 | } else { |
Anders Carlsson | 64c9eca | 2010-03-29 02:08:26 +0000 | [diff] [blame] | 1564 | uint64_t AddressPoint = CGM.getVTables().getAddressPoint(Base, VTableClass); |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1565 | VTableAddressPoint = |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1566 | Builder.CreateConstInBoundsGEP2_64(VTable, 0, AddressPoint); |
Anders Carlsson | c83f106 | 2010-03-29 01:08:49 +0000 | [diff] [blame] | 1567 | } |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1568 | |
| 1569 | // Compute where to store the address point. |
Anders Carlsson | b358814 | 2010-03-29 01:14:25 +0000 | [diff] [blame] | 1570 | llvm::Value *VTableField; |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1571 | |
Anders Carlsson | 851853d | 2010-03-29 02:38:51 +0000 | [diff] [blame] | 1572 | if (CodeGenVTables::needsVTTParameter(CurGD) && BaseIsMorallyVirtual) { |
Anders Carlsson | b358814 | 2010-03-29 01:14:25 +0000 | [diff] [blame] | 1573 | // We need to use the virtual base offset offset because the virtual base |
| 1574 | // might have a different offset in the most derived class. |
| 1575 | VTableField = GetAddressOfBaseClass(LoadCXXThis(), VTableClass, RD, |
| 1576 | /*NullCheckValue=*/false); |
| 1577 | } else { |
| 1578 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); |
| 1579 | |
| 1580 | VTableField = Builder.CreateBitCast(LoadCXXThis(), Int8PtrTy); |
| 1581 | VTableField = |
| 1582 | Builder.CreateConstInBoundsGEP1_64(VTableField, Base.getBaseOffset() / 8); |
| 1583 | } |
| 1584 | |
Anders Carlsson | d103f9f | 2010-03-28 19:40:00 +0000 | [diff] [blame] | 1585 | // Finally, store the address point. |
| 1586 | const llvm::Type *AddressPointPtrTy = |
| 1587 | VTableAddressPoint->getType()->getPointerTo(); |
| 1588 | VTableField = Builder.CreateBitCast(VTableField, AddressPointPtrTy); |
| 1589 | Builder.CreateStore(VTableAddressPoint, VTableField); |
| 1590 | } |
| 1591 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1592 | void |
| 1593 | CodeGenFunction::InitializeVTablePointers(BaseSubobject Base, |
| 1594 | bool BaseIsMorallyVirtual, |
| 1595 | bool BaseIsNonVirtualPrimaryBase, |
| 1596 | llvm::Constant *VTable, |
| 1597 | const CXXRecordDecl *VTableClass, |
| 1598 | VisitedVirtualBasesSetTy& VBases) { |
| 1599 | // If this base is a non-virtual primary base the address point has already |
| 1600 | // been set. |
| 1601 | if (!BaseIsNonVirtualPrimaryBase) { |
| 1602 | // Initialize the vtable pointer for this base. |
| 1603 | InitializeVTablePointer(Base, BaseIsMorallyVirtual, VTable, VTableClass); |
| 1604 | } |
| 1605 | |
| 1606 | const CXXRecordDecl *RD = Base.getBase(); |
| 1607 | |
| 1608 | // Traverse bases. |
| 1609 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
| 1610 | E = RD->bases_end(); I != E; ++I) { |
| 1611 | CXXRecordDecl *BaseDecl |
| 1612 | = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); |
| 1613 | |
| 1614 | // Ignore classes without a vtable. |
| 1615 | if (!BaseDecl->isDynamicClass()) |
| 1616 | continue; |
| 1617 | |
| 1618 | uint64_t BaseOffset; |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1619 | bool BaseDeclIsMorallyVirtual = BaseIsMorallyVirtual; |
| 1620 | bool BaseDeclIsNonVirtualPrimaryBase; |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1621 | |
| 1622 | if (I->isVirtual()) { |
| 1623 | // Check if we've visited this virtual base before. |
| 1624 | if (!VBases.insert(BaseDecl)) |
| 1625 | continue; |
| 1626 | |
| 1627 | const ASTRecordLayout &Layout = |
| 1628 | getContext().getASTRecordLayout(VTableClass); |
| 1629 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1630 | BaseOffset = Layout.getVBaseClassOffset(BaseDecl); |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1631 | BaseDeclIsMorallyVirtual = true; |
| 1632 | BaseDeclIsNonVirtualPrimaryBase = false; |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1633 | } else { |
| 1634 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); |
| 1635 | |
| 1636 | BaseOffset = Base.getBaseOffset() + Layout.getBaseClassOffset(BaseDecl); |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1637 | BaseDeclIsNonVirtualPrimaryBase = Layout.getPrimaryBase() == BaseDecl; |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | InitializeVTablePointers(BaseSubobject(BaseDecl, BaseOffset), |
Anders Carlsson | 14da9de | 2010-03-29 01:16:41 +0000 | [diff] [blame] | 1641 | BaseDeclIsMorallyVirtual, |
| 1642 | BaseDeclIsNonVirtualPrimaryBase, |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1643 | VTable, VTableClass, VBases); |
| 1644 | } |
| 1645 | } |
| 1646 | |
| 1647 | void CodeGenFunction::InitializeVTablePointers(const CXXRecordDecl *RD) { |
| 1648 | // Ignore classes without a vtable. |
Anders Carlsson | 0703690 | 2010-03-26 04:39:42 +0000 | [diff] [blame] | 1649 | if (!RD->isDynamicClass()) |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1650 | return; |
| 1651 | |
Anders Carlsson | 0703690 | 2010-03-26 04:39:42 +0000 | [diff] [blame] | 1652 | // Get the VTable. |
| 1653 | llvm::Constant *VTable = CGM.getVTables().GetAddrOfVTable(RD); |
Anders Carlsson | 5c6c1d9 | 2010-03-24 03:57:14 +0000 | [diff] [blame] | 1654 | |
Anders Carlsson | 603d6d1 | 2010-03-28 21:07:49 +0000 | [diff] [blame] | 1655 | // Initialize the vtable pointers for this class and all of its bases. |
| 1656 | VisitedVirtualBasesSetTy VBases; |
| 1657 | InitializeVTablePointers(BaseSubobject(RD, 0), |
| 1658 | /*BaseIsMorallyVirtual=*/false, |
| 1659 | /*BaseIsNonVirtualPrimaryBase=*/false, |
| 1660 | VTable, RD, VBases); |
Anders Carlsson | 3b5ad22 | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1661 | } |