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