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