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