Anders Carlsson | 59486a2 | 2009-11-24 05:51:11 +0000 | [diff] [blame] | 1 | //===--- CGClass.cpp - Emit LLVM Code for C++ classes ---------------------===// |
Anders Carlsson | 9a57c5a | 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 | c6d171e | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 15 | #include "clang/AST/CXXInheritance.h" |
Anders Carlsson | 9a57c5a | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 16 | #include "clang/AST/RecordLayout.h" |
Anders Carlsson | c6d171e | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 17 | |
Anders Carlsson | 9a57c5a | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | using namespace CodeGen; |
| 20 | |
Anders Carlsson | c6d171e | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 21 | static uint64_t |
John McCall | 6ce7472 | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 22 | ComputeNonVirtualBaseClassOffset(ASTContext &Context, |
| 23 | const CXXBasePath &Path, |
Anders Carlsson | c6d171e | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 24 | unsigned Start) { |
| 25 | uint64_t Offset = 0; |
Anders Carlsson | 9a57c5a | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 26 | |
Anders Carlsson | c6d171e | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 27 | for (unsigned i = Start, e = Path.size(); i != e; ++i) { |
| 28 | const CXXBasePathElement& Element = Path[i]; |
Anders Carlsson | 9a57c5a | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 29 | |
Anders Carlsson | c6d171e | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 30 | // Get the layout. |
| 31 | const ASTRecordLayout &Layout = Context.getASTRecordLayout(Element.Class); |
Anders Carlsson | 9a57c5a | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 32 | |
Anders Carlsson | c6d171e | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 33 | const CXXBaseSpecifier *BS = Element.Base; |
| 34 | assert(!BS->isVirtual() && "Should not see virtual bases here!"); |
Anders Carlsson | 9a57c5a | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 35 | |
Anders Carlsson | c6d171e | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 36 | const CXXRecordDecl *Base = |
| 37 | cast<CXXRecordDecl>(BS->getType()->getAs<RecordType>()->getDecl()); |
| 38 | |
| 39 | // Add the offset. |
| 40 | Offset += Layout.getBaseClassOffset(Base) / 8; |
| 41 | } |
| 42 | |
| 43 | return Offset; |
Anders Carlsson | 9a57c5a | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Anders Carlsson | 9150a2a | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 46 | llvm::Constant * |
Anders Carlsson | 84673e2 | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 47 | CodeGenModule::GetNonVirtualBaseClassOffset(const CXXRecordDecl *Class, |
| 48 | const CXXRecordDecl *BaseClass) { |
| 49 | if (Class == BaseClass) |
Anders Carlsson | 9150a2a | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 50 | return 0; |
| 51 | |
Anders Carlsson | c6d171e | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 52 | CXXBasePaths Paths(/*FindAmbiguities=*/false, |
| 53 | /*RecordPaths=*/true, /*DetectVirtual=*/false); |
Anders Carlsson | 84673e2 | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 54 | if (!const_cast<CXXRecordDecl *>(Class)-> |
| 55 | isDerivedFrom(const_cast<CXXRecordDecl *>(BaseClass), Paths)) { |
Anders Carlsson | c6d171e | 2009-10-06 22:43:30 +0000 | [diff] [blame] | 56 | assert(false && "Class must be derived from the passed in base class!"); |
| 57 | return 0; |
| 58 | } |
Anders Carlsson | 9150a2a | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 59 | |
John McCall | 6ce7472 | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 60 | uint64_t Offset = ComputeNonVirtualBaseClassOffset(getContext(), |
| 61 | Paths.front(), 0); |
Anders Carlsson | 9150a2a | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 62 | if (!Offset) |
| 63 | return 0; |
| 64 | |
Anders Carlsson | 32bfb1c | 2009-10-03 14:56:57 +0000 | [diff] [blame] | 65 | const llvm::Type *PtrDiffTy = |
| 66 | Types.ConvertType(getContext().getPointerDiffType()); |
Anders Carlsson | 9150a2a | 2009-09-29 03:13:20 +0000 | [diff] [blame] | 67 | |
| 68 | return llvm::ConstantInt::get(PtrDiffTy, Offset); |
| 69 | } |
| 70 | |
Anders Carlsson | 5f91fd6 | 2009-12-03 03:06:55 +0000 | [diff] [blame] | 71 | // FIXME: This probably belongs in CGVtable, but it relies on |
| 72 | // the static function ComputeNonVirtualBaseClassOffset, so we should make that |
| 73 | // a CodeGenModule member function as well. |
| 74 | ThunkAdjustment |
| 75 | CodeGenModule::ComputeThunkAdjustment(const CXXRecordDecl *ClassDecl, |
| 76 | const CXXRecordDecl *BaseClassDecl) { |
| 77 | CXXBasePaths Paths(/*FindAmbiguities=*/false, |
| 78 | /*RecordPaths=*/true, /*DetectVirtual=*/false); |
| 79 | if (!const_cast<CXXRecordDecl *>(ClassDecl)-> |
| 80 | isDerivedFrom(const_cast<CXXRecordDecl *>(BaseClassDecl), Paths)) { |
| 81 | assert(false && "Class must be derived from the passed in base class!"); |
| 82 | return ThunkAdjustment(); |
| 83 | } |
| 84 | |
| 85 | unsigned Start = 0; |
| 86 | uint64_t VirtualOffset = 0; |
| 87 | |
| 88 | const CXXBasePath &Path = Paths.front(); |
| 89 | const CXXRecordDecl *VBase = 0; |
| 90 | for (unsigned i = 0, e = Path.size(); i != e; ++i) { |
| 91 | const CXXBasePathElement& Element = Path[i]; |
| 92 | if (Element.Base->isVirtual()) { |
| 93 | Start = i+1; |
| 94 | QualType VBaseType = Element.Base->getType(); |
| 95 | VBase = cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl()); |
| 96 | } |
| 97 | } |
| 98 | if (VBase) |
| 99 | VirtualOffset = |
| 100 | getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl); |
| 101 | |
| 102 | uint64_t Offset = |
John McCall | 6ce7472 | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 103 | ComputeNonVirtualBaseClassOffset(getContext(), Paths.front(), Start); |
Anders Carlsson | 5f91fd6 | 2009-12-03 03:06:55 +0000 | [diff] [blame] | 104 | return ThunkAdjustment(Offset, VirtualOffset); |
| 105 | } |
| 106 | |
John McCall | 6ce7472 | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 107 | /// Gets the address of a virtual base class within a complete object. |
| 108 | /// This should only be used for (1) non-virtual bases or (2) virtual bases |
| 109 | /// when the type is known to be complete (e.g. in complete destructors). |
| 110 | /// |
| 111 | /// The object pointed to by 'This' is assumed to be non-null. |
| 112 | llvm::Value * |
| 113 | CodeGenFunction::GetAddressOfBaseOfCompleteClass(llvm::Value *This, |
| 114 | bool isBaseVirtual, |
| 115 | const CXXRecordDecl *Derived, |
| 116 | const CXXRecordDecl *Base) { |
| 117 | // 'this' must be a pointer (in some address space) to Derived. |
| 118 | assert(This->getType()->isPointerTy() && |
| 119 | cast<llvm::PointerType>(This->getType())->getElementType() |
| 120 | == ConvertType(Derived)); |
| 121 | |
| 122 | // Compute the offset of the virtual base. |
| 123 | uint64_t Offset; |
| 124 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Derived); |
| 125 | if (isBaseVirtual) |
| 126 | Offset = Layout.getVBaseClassOffset(Base); |
| 127 | else |
| 128 | Offset = Layout.getBaseClassOffset(Base); |
| 129 | |
| 130 | // Shift and cast down to the base type. |
| 131 | // TODO: for complete types, this should be possible with a GEP. |
| 132 | llvm::Value *V = This; |
| 133 | if (Offset) { |
| 134 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext()); |
| 135 | V = Builder.CreateBitCast(V, Int8PtrTy); |
| 136 | V = Builder.CreateConstInBoundsGEP1_64(V, Offset / 8); |
| 137 | } |
| 138 | V = Builder.CreateBitCast(V, ConvertType(Base)->getPointerTo()); |
| 139 | |
| 140 | return V; |
| 141 | } |
| 142 | |
Anders Carlsson | 9a57c5a | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 143 | llvm::Value * |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 144 | CodeGenFunction::GetAddressOfBaseClass(llvm::Value *Value, |
Anders Carlsson | 84673e2 | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 145 | const CXXRecordDecl *Class, |
| 146 | const CXXRecordDecl *BaseClass, |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 147 | bool NullCheckValue) { |
Anders Carlsson | 8fef09c | 2009-09-22 21:58:22 +0000 | [diff] [blame] | 148 | QualType BTy = |
| 149 | getContext().getCanonicalType( |
John McCall | 6ce7472 | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 150 | getContext().getTypeDeclType(BaseClass)); |
Anders Carlsson | 8fef09c | 2009-09-22 21:58:22 +0000 | [diff] [blame] | 151 | const llvm::Type *BasePtrTy = llvm::PointerType::getUnqual(ConvertType(BTy)); |
Anders Carlsson | 9a57c5a | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 152 | |
Anders Carlsson | 84673e2 | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 153 | if (Class == BaseClass) { |
Anders Carlsson | 8fef09c | 2009-09-22 21:58:22 +0000 | [diff] [blame] | 154 | // Just cast back. |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 155 | return Builder.CreateBitCast(Value, BasePtrTy); |
Anders Carlsson | 8fef09c | 2009-09-22 21:58:22 +0000 | [diff] [blame] | 156 | } |
Anders Carlsson | 6276b25 | 2010-01-31 02:39:02 +0000 | [diff] [blame] | 157 | |
| 158 | CXXBasePaths Paths(/*FindAmbiguities=*/false, |
| 159 | /*RecordPaths=*/true, /*DetectVirtual=*/false); |
| 160 | if (!const_cast<CXXRecordDecl *>(Class)-> |
| 161 | isDerivedFrom(const_cast<CXXRecordDecl *>(BaseClass), Paths)) { |
| 162 | assert(false && "Class must be derived from the passed in base class!"); |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | unsigned Start = 0; |
| 167 | llvm::Value *VirtualOffset = 0; |
| 168 | |
| 169 | const CXXBasePath &Path = Paths.front(); |
| 170 | const CXXRecordDecl *VBase = 0; |
| 171 | for (unsigned i = 0, e = Path.size(); i != e; ++i) { |
| 172 | const CXXBasePathElement& Element = Path[i]; |
| 173 | if (Element.Base->isVirtual()) { |
| 174 | Start = i+1; |
| 175 | QualType VBaseType = Element.Base->getType(); |
| 176 | VBase = cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl()); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | uint64_t Offset = |
John McCall | 6ce7472 | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 181 | ComputeNonVirtualBaseClassOffset(getContext(), Paths.front(), Start); |
Eli Friedman | d76f438 | 2009-11-10 22:48:10 +0000 | [diff] [blame] | 182 | |
Anders Carlsson | 6276b25 | 2010-01-31 02:39:02 +0000 | [diff] [blame] | 183 | if (!Offset && !VBase) { |
| 184 | // Just cast back. |
| 185 | return Builder.CreateBitCast(Value, BasePtrTy); |
| 186 | } |
| 187 | |
Anders Carlsson | 360e7d0 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 188 | llvm::BasicBlock *CastNull = 0; |
| 189 | llvm::BasicBlock *CastNotNull = 0; |
| 190 | llvm::BasicBlock *CastEnd = 0; |
| 191 | |
| 192 | if (NullCheckValue) { |
| 193 | CastNull = createBasicBlock("cast.null"); |
| 194 | CastNotNull = createBasicBlock("cast.notnull"); |
| 195 | CastEnd = createBasicBlock("cast.end"); |
| 196 | |
| 197 | llvm::Value *IsNull = |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 198 | Builder.CreateICmpEQ(Value, |
| 199 | llvm::Constant::getNullValue(Value->getType())); |
Anders Carlsson | 360e7d0 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 200 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
| 201 | EmitBlock(CastNotNull); |
| 202 | } |
| 203 | |
Anders Carlsson | 6276b25 | 2010-01-31 02:39:02 +0000 | [diff] [blame] | 204 | if (VBase) |
| 205 | VirtualOffset = GetVirtualBaseClassOffset(Value, Class, VBase); |
Eli Friedman | d76f438 | 2009-11-10 22:48:10 +0000 | [diff] [blame] | 206 | |
Anders Carlsson | 6276b25 | 2010-01-31 02:39:02 +0000 | [diff] [blame] | 207 | const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType()); |
| 208 | llvm::Value *NonVirtualOffset = 0; |
| 209 | if (Offset) |
| 210 | NonVirtualOffset = llvm::ConstantInt::get(PtrDiffTy, Offset); |
Anders Carlsson | 9a57c5a | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 211 | |
Anders Carlsson | 6276b25 | 2010-01-31 02:39:02 +0000 | [diff] [blame] | 212 | llvm::Value *BaseOffset; |
| 213 | if (VBase) { |
| 214 | if (NonVirtualOffset) |
| 215 | BaseOffset = Builder.CreateAdd(VirtualOffset, NonVirtualOffset); |
| 216 | else |
| 217 | BaseOffset = VirtualOffset; |
| 218 | } else |
| 219 | BaseOffset = NonVirtualOffset; |
| 220 | |
| 221 | // Apply the base offset. |
| 222 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext()); |
| 223 | Value = Builder.CreateBitCast(Value, Int8PtrTy); |
| 224 | Value = Builder.CreateGEP(Value, BaseOffset, "add.ptr"); |
Anders Carlsson | 9a57c5a | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 225 | |
Anders Carlsson | 9a57c5a | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 226 | // Cast back. |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 227 | Value = Builder.CreateBitCast(Value, BasePtrTy); |
Anders Carlsson | 360e7d0 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 228 | |
| 229 | if (NullCheckValue) { |
| 230 | Builder.CreateBr(CastEnd); |
| 231 | EmitBlock(CastNull); |
| 232 | Builder.CreateBr(CastEnd); |
| 233 | EmitBlock(CastEnd); |
| 234 | |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 235 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType()); |
Anders Carlsson | 360e7d0 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 236 | PHI->reserveOperandSpace(2); |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 237 | PHI->addIncoming(Value, CastNotNull); |
| 238 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), |
Anders Carlsson | 360e7d0 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 239 | CastNull); |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 240 | Value = PHI; |
Anders Carlsson | 360e7d0 | 2009-09-12 06:04:24 +0000 | [diff] [blame] | 241 | } |
Anders Carlsson | 9a57c5a | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 242 | |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 243 | return Value; |
| 244 | } |
| 245 | |
| 246 | llvm::Value * |
| 247 | CodeGenFunction::GetAddressOfDerivedClass(llvm::Value *Value, |
Anders Carlsson | 84673e2 | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 248 | const CXXRecordDecl *Class, |
| 249 | const CXXRecordDecl *DerivedClass, |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 250 | bool NullCheckValue) { |
| 251 | QualType DerivedTy = |
| 252 | getContext().getCanonicalType( |
Anders Carlsson | 84673e2 | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 253 | getContext().getTypeDeclType(const_cast<CXXRecordDecl*>(DerivedClass))); |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 254 | const llvm::Type *DerivedPtrTy = ConvertType(DerivedTy)->getPointerTo(); |
| 255 | |
Anders Carlsson | 84673e2 | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 256 | if (Class == DerivedClass) { |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 257 | // Just cast back. |
| 258 | return Builder.CreateBitCast(Value, DerivedPtrTy); |
| 259 | } |
| 260 | |
Anders Carlsson | 600f737 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 261 | llvm::Value *NonVirtualOffset = |
| 262 | CGM.GetNonVirtualBaseClassOffset(DerivedClass, Class); |
| 263 | |
| 264 | if (!NonVirtualOffset) { |
| 265 | // No offset, we can just cast back. |
| 266 | return Builder.CreateBitCast(Value, DerivedPtrTy); |
| 267 | } |
| 268 | |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 269 | llvm::BasicBlock *CastNull = 0; |
| 270 | llvm::BasicBlock *CastNotNull = 0; |
| 271 | llvm::BasicBlock *CastEnd = 0; |
| 272 | |
| 273 | if (NullCheckValue) { |
| 274 | CastNull = createBasicBlock("cast.null"); |
| 275 | CastNotNull = createBasicBlock("cast.notnull"); |
| 276 | CastEnd = createBasicBlock("cast.end"); |
| 277 | |
| 278 | llvm::Value *IsNull = |
| 279 | Builder.CreateICmpEQ(Value, |
| 280 | llvm::Constant::getNullValue(Value->getType())); |
| 281 | Builder.CreateCondBr(IsNull, CastNull, CastNotNull); |
| 282 | EmitBlock(CastNotNull); |
| 283 | } |
| 284 | |
Anders Carlsson | 600f737 | 2010-01-31 01:43:37 +0000 | [diff] [blame] | 285 | // Apply the offset. |
| 286 | Value = Builder.CreatePtrToInt(Value, NonVirtualOffset->getType()); |
| 287 | Value = Builder.CreateSub(Value, NonVirtualOffset); |
| 288 | Value = Builder.CreateIntToPtr(Value, DerivedPtrTy); |
| 289 | |
| 290 | // Just cast. |
| 291 | Value = Builder.CreateBitCast(Value, DerivedPtrTy); |
Anders Carlsson | 8c79317 | 2009-11-23 17:57:54 +0000 | [diff] [blame] | 292 | |
| 293 | if (NullCheckValue) { |
| 294 | Builder.CreateBr(CastEnd); |
| 295 | EmitBlock(CastNull); |
| 296 | Builder.CreateBr(CastEnd); |
| 297 | EmitBlock(CastEnd); |
| 298 | |
| 299 | llvm::PHINode *PHI = Builder.CreatePHI(Value->getType()); |
| 300 | PHI->reserveOperandSpace(2); |
| 301 | PHI->addIncoming(Value, CastNotNull); |
| 302 | PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), |
| 303 | CastNull); |
| 304 | Value = PHI; |
| 305 | } |
| 306 | |
| 307 | return Value; |
Anders Carlsson | 9a57c5a | 2009-09-12 04:27:24 +0000 | [diff] [blame] | 308 | } |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 309 | |
| 310 | /// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class |
| 311 | /// array of objects from SrcValue to DestValue. Copying can be either a bitwise |
| 312 | /// copy or via a copy constructor call. |
| 313 | // FIXME. Consolidate this with EmitCXXAggrConstructorCall. |
| 314 | void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest, |
| 315 | llvm::Value *Src, |
| 316 | const ArrayType *Array, |
| 317 | const CXXRecordDecl *BaseClassDecl, |
| 318 | QualType Ty) { |
| 319 | const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); |
| 320 | assert(CA && "VLA cannot be copied over"); |
| 321 | bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor(); |
| 322 | |
| 323 | // Create a temporary for the loop index and initialize it with 0. |
| 324 | llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext), |
| 325 | "loop.index"); |
| 326 | llvm::Value* zeroConstant = |
| 327 | llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)); |
| 328 | Builder.CreateStore(zeroConstant, IndexPtr); |
| 329 | // Start the loop with a block that tests the condition. |
| 330 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 331 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
| 332 | |
| 333 | EmitBlock(CondBlock); |
| 334 | |
| 335 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 336 | // Generate: if (loop-index < number-of-elements fall to the loop body, |
| 337 | // otherwise, go to the block after the for-loop. |
| 338 | uint64_t NumElements = getContext().getConstantArrayElementCount(CA); |
| 339 | llvm::Value * NumElementsPtr = |
| 340 | llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements); |
| 341 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 342 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr, |
| 343 | "isless"); |
| 344 | // If the condition is true, execute the body. |
| 345 | Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
| 346 | |
| 347 | EmitBlock(ForBody); |
| 348 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 349 | // Inside the loop body, emit the constructor call on the array element. |
| 350 | Counter = Builder.CreateLoad(IndexPtr); |
| 351 | Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress"); |
| 352 | Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress"); |
| 353 | if (BitwiseCopy) |
| 354 | EmitAggregateCopy(Dest, Src, Ty); |
| 355 | else if (CXXConstructorDecl *BaseCopyCtor = |
| 356 | BaseClassDecl->getCopyConstructor(getContext(), 0)) { |
| 357 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor, |
| 358 | Ctor_Complete); |
| 359 | CallArgList CallArgs; |
| 360 | // Push the this (Dest) ptr. |
| 361 | CallArgs.push_back(std::make_pair(RValue::get(Dest), |
| 362 | BaseCopyCtor->getThisType(getContext()))); |
| 363 | |
| 364 | // Push the Src ptr. |
| 365 | CallArgs.push_back(std::make_pair(RValue::get(Src), |
| 366 | BaseCopyCtor->getParamDecl(0)->getType())); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 367 | const FunctionProtoType *FPT |
| 368 | = BaseCopyCtor->getType()->getAs<FunctionProtoType>(); |
| 369 | EmitCall(CGM.getTypes().getFunctionInfo(CallArgs, FPT), |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 370 | Callee, ReturnValueSlot(), CallArgs, BaseCopyCtor); |
| 371 | } |
| 372 | EmitBlock(ContinueBlock); |
| 373 | |
| 374 | // Emit the increment of the loop counter. |
| 375 | llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); |
| 376 | Counter = Builder.CreateLoad(IndexPtr); |
| 377 | NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); |
| 378 | Builder.CreateStore(NextVal, IndexPtr); |
| 379 | |
| 380 | // Finally, branch back up to the condition for the next iteration. |
| 381 | EmitBranch(CondBlock); |
| 382 | |
| 383 | // Emit the fall-through block. |
| 384 | EmitBlock(AfterFor, true); |
| 385 | } |
| 386 | |
| 387 | /// EmitClassAggrCopyAssignment - This routine generates code to assign a class |
| 388 | /// array of objects from SrcValue to DestValue. Assignment can be either a |
| 389 | /// bitwise assignment or via a copy assignment operator function call. |
| 390 | /// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy |
| 391 | void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest, |
| 392 | llvm::Value *Src, |
| 393 | const ArrayType *Array, |
| 394 | const CXXRecordDecl *BaseClassDecl, |
| 395 | QualType Ty) { |
| 396 | const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); |
| 397 | assert(CA && "VLA cannot be asssigned"); |
| 398 | bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment(); |
| 399 | |
| 400 | // Create a temporary for the loop index and initialize it with 0. |
| 401 | llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext), |
| 402 | "loop.index"); |
| 403 | llvm::Value* zeroConstant = |
| 404 | llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)); |
| 405 | Builder.CreateStore(zeroConstant, IndexPtr); |
| 406 | // Start the loop with a block that tests the condition. |
| 407 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 408 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
| 409 | |
| 410 | EmitBlock(CondBlock); |
| 411 | |
| 412 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 413 | // Generate: if (loop-index < number-of-elements fall to the loop body, |
| 414 | // otherwise, go to the block after the for-loop. |
| 415 | uint64_t NumElements = getContext().getConstantArrayElementCount(CA); |
| 416 | llvm::Value * NumElementsPtr = |
| 417 | llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements); |
| 418 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 419 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr, |
| 420 | "isless"); |
| 421 | // If the condition is true, execute the body. |
| 422 | Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
| 423 | |
| 424 | EmitBlock(ForBody); |
| 425 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 426 | // Inside the loop body, emit the assignment operator call on array element. |
| 427 | Counter = Builder.CreateLoad(IndexPtr); |
| 428 | Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress"); |
| 429 | Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress"); |
| 430 | const CXXMethodDecl *MD = 0; |
| 431 | if (BitwiseAssign) |
| 432 | EmitAggregateCopy(Dest, Src, Ty); |
| 433 | else { |
Eli Friedman | cab0147 | 2010-01-15 20:06:11 +0000 | [diff] [blame] | 434 | BaseClassDecl->hasConstCopyAssignment(getContext(), MD); |
| 435 | assert(MD && "EmitClassAggrCopyAssignment - No user assign"); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 436 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 437 | const llvm::Type *LTy = |
| 438 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 439 | FPT->isVariadic()); |
| 440 | llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy); |
| 441 | |
| 442 | CallArgList CallArgs; |
| 443 | // Push the this (Dest) ptr. |
| 444 | CallArgs.push_back(std::make_pair(RValue::get(Dest), |
| 445 | MD->getThisType(getContext()))); |
| 446 | |
| 447 | // Push the Src ptr. |
Eli Friedman | cab0147 | 2010-01-15 20:06:11 +0000 | [diff] [blame] | 448 | QualType SrcTy = MD->getParamDecl(0)->getType(); |
| 449 | RValue SrcValue = SrcTy->isReferenceType() ? RValue::get(Src) : |
| 450 | RValue::getAggregate(Src); |
| 451 | CallArgs.push_back(std::make_pair(SrcValue, SrcTy)); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 452 | EmitCall(CGM.getTypes().getFunctionInfo(CallArgs, FPT), |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 453 | Callee, ReturnValueSlot(), CallArgs, MD); |
| 454 | } |
| 455 | EmitBlock(ContinueBlock); |
| 456 | |
| 457 | // Emit the increment of the loop counter. |
| 458 | llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); |
| 459 | Counter = Builder.CreateLoad(IndexPtr); |
| 460 | NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); |
| 461 | Builder.CreateStore(NextVal, IndexPtr); |
| 462 | |
| 463 | // Finally, branch back up to the condition for the next iteration. |
| 464 | EmitBranch(CondBlock); |
| 465 | |
| 466 | // Emit the fall-through block. |
| 467 | EmitBlock(AfterFor, true); |
| 468 | } |
| 469 | |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 470 | /// GetVTTParameter - Return the VTT parameter that should be passed to a |
| 471 | /// base constructor/destructor with virtual bases. |
| 472 | static llvm::Value *GetVTTParameter(CodeGenFunction &CGF, GlobalDecl GD) { |
| 473 | if (!CGVtableInfo::needsVTTParameter(GD)) { |
| 474 | // This constructor/destructor does not need a VTT parameter. |
| 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | const CXXRecordDecl *RD = cast<CXXMethodDecl>(CGF.CurFuncDecl)->getParent(); |
| 479 | const CXXRecordDecl *Base = cast<CXXMethodDecl>(GD.getDecl())->getParent(); |
| 480 | |
| 481 | llvm::Value *VTT; |
| 482 | |
| 483 | uint64_t SubVTTIndex = |
| 484 | CGF.CGM.getVtableInfo().getSubVTTIndex(RD, Base); |
| 485 | assert(SubVTTIndex != 0 && "Sub-VTT index must be greater than zero!"); |
| 486 | |
| 487 | if (CGVtableInfo::needsVTTParameter(CGF.CurGD)) { |
| 488 | // A VTT parameter was passed to the constructor, use it. |
| 489 | VTT = CGF.LoadCXXVTT(); |
| 490 | VTT = CGF.Builder.CreateConstInBoundsGEP1_64(VTT, SubVTTIndex); |
| 491 | } else { |
| 492 | // We're the complete constructor, so get the VTT by name. |
| 493 | VTT = CGF.CGM.getVtableInfo().getVTT(RD); |
| 494 | VTT = CGF.Builder.CreateConstInBoundsGEP2_64(VTT, 0, SubVTTIndex); |
| 495 | } |
| 496 | |
| 497 | return VTT; |
| 498 | } |
| 499 | |
| 500 | |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 501 | /// EmitClassMemberwiseCopy - This routine generates code to copy a class |
| 502 | /// object from SrcValue to DestValue. Copying can be either a bitwise copy |
| 503 | /// or via a copy constructor call. |
| 504 | void CodeGenFunction::EmitClassMemberwiseCopy( |
| 505 | llvm::Value *Dest, llvm::Value *Src, |
| 506 | const CXXRecordDecl *ClassDecl, |
| 507 | const CXXRecordDecl *BaseClassDecl, QualType Ty) { |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 508 | CXXCtorType CtorType = Ctor_Complete; |
| 509 | |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 510 | if (ClassDecl) { |
| 511 | Dest = GetAddressOfBaseClass(Dest, ClassDecl, BaseClassDecl, |
| 512 | /*NullCheckValue=*/false); |
| 513 | Src = GetAddressOfBaseClass(Src, ClassDecl, BaseClassDecl, |
| 514 | /*NullCheckValue=*/false); |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 515 | |
| 516 | // We want to call the base constructor. |
| 517 | CtorType = Ctor_Base; |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 518 | } |
| 519 | if (BaseClassDecl->hasTrivialCopyConstructor()) { |
| 520 | EmitAggregateCopy(Dest, Src, Ty); |
| 521 | return; |
| 522 | } |
| 523 | |
| 524 | if (CXXConstructorDecl *BaseCopyCtor = |
| 525 | BaseClassDecl->getCopyConstructor(getContext(), 0)) { |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 526 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor, CtorType); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 527 | CallArgList CallArgs; |
| 528 | // Push the this (Dest) ptr. |
| 529 | CallArgs.push_back(std::make_pair(RValue::get(Dest), |
| 530 | BaseCopyCtor->getThisType(getContext()))); |
| 531 | |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 532 | // Push the VTT parameter, if necessary. |
| 533 | if (llvm::Value *VTT = |
| 534 | GetVTTParameter(*this, GlobalDecl(BaseCopyCtor, CtorType))) { |
| 535 | QualType T = getContext().getPointerType(getContext().VoidPtrTy); |
| 536 | CallArgs.push_back(std::make_pair(RValue::get(VTT), T)); |
| 537 | } |
| 538 | |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 539 | // Push the Src ptr. |
| 540 | CallArgs.push_back(std::make_pair(RValue::get(Src), |
| 541 | BaseCopyCtor->getParamDecl(0)->getType())); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 542 | const FunctionProtoType *FPT = |
| 543 | BaseCopyCtor->getType()->getAs<FunctionProtoType>(); |
| 544 | EmitCall(CGM.getTypes().getFunctionInfo(CallArgs, FPT), |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 545 | Callee, ReturnValueSlot(), CallArgs, BaseCopyCtor); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | /// EmitClassCopyAssignment - This routine generates code to copy assign a class |
| 550 | /// object from SrcValue to DestValue. Assignment can be either a bitwise |
| 551 | /// assignment of via an assignment operator call. |
| 552 | // FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot. |
| 553 | void CodeGenFunction::EmitClassCopyAssignment( |
| 554 | llvm::Value *Dest, llvm::Value *Src, |
| 555 | const CXXRecordDecl *ClassDecl, |
| 556 | const CXXRecordDecl *BaseClassDecl, |
| 557 | QualType Ty) { |
| 558 | if (ClassDecl) { |
| 559 | Dest = GetAddressOfBaseClass(Dest, ClassDecl, BaseClassDecl, |
| 560 | /*NullCheckValue=*/false); |
| 561 | Src = GetAddressOfBaseClass(Src, ClassDecl, BaseClassDecl, |
| 562 | /*NullCheckValue=*/false); |
| 563 | } |
| 564 | if (BaseClassDecl->hasTrivialCopyAssignment()) { |
| 565 | EmitAggregateCopy(Dest, Src, Ty); |
| 566 | return; |
| 567 | } |
| 568 | |
| 569 | const CXXMethodDecl *MD = 0; |
Eli Friedman | cab0147 | 2010-01-15 20:06:11 +0000 | [diff] [blame] | 570 | BaseClassDecl->hasConstCopyAssignment(getContext(), MD); |
| 571 | assert(MD && "EmitClassCopyAssignment - missing copy assign"); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 572 | |
| 573 | const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 574 | const llvm::Type *LTy = |
| 575 | CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), |
| 576 | FPT->isVariadic()); |
| 577 | llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy); |
| 578 | |
| 579 | CallArgList CallArgs; |
| 580 | // Push the this (Dest) ptr. |
| 581 | CallArgs.push_back(std::make_pair(RValue::get(Dest), |
| 582 | MD->getThisType(getContext()))); |
| 583 | |
| 584 | // Push the Src ptr. |
Eli Friedman | cab0147 | 2010-01-15 20:06:11 +0000 | [diff] [blame] | 585 | QualType SrcTy = MD->getParamDecl(0)->getType(); |
| 586 | RValue SrcValue = SrcTy->isReferenceType() ? RValue::get(Src) : |
| 587 | RValue::getAggregate(Src); |
| 588 | CallArgs.push_back(std::make_pair(SrcValue, SrcTy)); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 589 | EmitCall(CGM.getTypes().getFunctionInfo(CallArgs, FPT), |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 590 | Callee, ReturnValueSlot(), CallArgs, MD); |
| 591 | } |
| 592 | |
John McCall | 89b12b3 | 2010-02-18 03:17:58 +0000 | [diff] [blame] | 593 | /// Synthesizes an implicit function body. Since these only arise in |
| 594 | /// C++, we only do them in C++. |
| 595 | void CodeGenFunction::SynthesizeImplicitFunctionBody(GlobalDecl GD, |
| 596 | llvm::Function *Fn, |
| 597 | const FunctionArgList &Args) { |
| 598 | const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl()); |
| 599 | |
| 600 | // FIXME: this should become isImplicitlyDefined() once we properly |
| 601 | // support that for C++0x. |
| 602 | assert(FD->isImplicit() && "Cannot synthesize a non-implicit function"); |
| 603 | |
| 604 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { |
| 605 | assert(!CD->isTrivial() && "shouldn't need to synthesize a trivial ctor"); |
| 606 | |
| 607 | if (CD->isDefaultConstructor()) { |
| 608 | // Sema generates base and member initializers as for this, so |
| 609 | // the ctor prologue is good enough here. |
| 610 | return; |
| 611 | } else { |
| 612 | assert(CD->isCopyConstructor()); |
| 613 | return SynthesizeCXXCopyConstructor(CD, GD.getCtorType(), Fn, Args); |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | if (isa<CXXDestructorDecl>(FD)) { |
| 618 | // The dtor epilogue does everything we'd need to do here. |
| 619 | return; |
| 620 | } |
| 621 | |
| 622 | const CXXMethodDecl *MD = cast<CXXMethodDecl>(FD); |
| 623 | |
| 624 | // FIXME: in C++0x we might have user-declared copy assignment operators |
| 625 | // coexisting with implicitly-defined ones. |
| 626 | assert(MD->isCopyAssignment() && |
| 627 | !MD->getParent()->hasUserDeclaredCopyAssignment() && |
| 628 | "Cannot synthesize a method that is not an implicitly-defined " |
| 629 | "copy constructor"); |
| 630 | SynthesizeCXXCopyAssignment(MD, Fn, Args); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a |
| 634 | /// copy constructor, in accordance with section 12.8 (p7 and p8) of C++03 |
| 635 | /// The implicitly-defined copy constructor for class X performs a memberwise |
| 636 | /// copy of its subobjects. The order of copying is the same as the order of |
| 637 | /// initialization of bases and members in a user-defined constructor |
| 638 | /// Each subobject is copied in the manner appropriate to its type: |
| 639 | /// if the subobject is of class type, the copy constructor for the class is |
| 640 | /// used; |
| 641 | /// if the subobject is an array, each element is copied, in the manner |
| 642 | /// appropriate to the element type; |
| 643 | /// if the subobject is of scalar type, the built-in assignment operator is |
| 644 | /// used. |
| 645 | /// Virtual base class subobjects shall be copied only once by the |
| 646 | /// implicitly-defined copy constructor |
| 647 | |
| 648 | void |
| 649 | CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor, |
| 650 | CXXCtorType Type, |
| 651 | llvm::Function *Fn, |
| 652 | const FunctionArgList &Args) { |
| 653 | const CXXRecordDecl *ClassDecl = Ctor->getParent(); |
| 654 | assert(!ClassDecl->hasUserDeclaredCopyConstructor() && |
| 655 | "SynthesizeCXXCopyConstructor - copy constructor has definition already"); |
| 656 | assert(!Ctor->isTrivial() && "shouldn't need to generate trivial ctor"); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 657 | |
| 658 | FunctionArgList::const_iterator i = Args.begin(); |
| 659 | const VarDecl *ThisArg = i->first; |
| 660 | llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg); |
| 661 | llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this"); |
| 662 | const VarDecl *SrcArg = (i+1)->first; |
| 663 | llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg); |
| 664 | llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj); |
| 665 | |
| 666 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); |
| 667 | Base != ClassDecl->bases_end(); ++Base) { |
| 668 | // FIXME. copy constrution of virtual base NYI |
| 669 | if (Base->isVirtual()) |
| 670 | continue; |
| 671 | |
| 672 | CXXRecordDecl *BaseClassDecl |
| 673 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 674 | EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl, |
| 675 | Base->getType()); |
| 676 | } |
| 677 | |
| 678 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 679 | E = ClassDecl->field_end(); I != E; ++I) { |
| 680 | const FieldDecl *Field = *I; |
| 681 | |
| 682 | QualType FieldType = getContext().getCanonicalType(Field->getType()); |
| 683 | const ConstantArrayType *Array = |
| 684 | getContext().getAsConstantArrayType(FieldType); |
| 685 | if (Array) |
| 686 | FieldType = getContext().getBaseElementType(FieldType); |
| 687 | |
| 688 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
| 689 | CXXRecordDecl *FieldClassDecl |
| 690 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Anders Carlsson | 5d8645b | 2010-01-29 05:05:36 +0000 | [diff] [blame] | 691 | LValue LHS = EmitLValueForField(LoadOfThis, Field, 0); |
| 692 | LValue RHS = EmitLValueForField(LoadOfSrc, Field, 0); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 693 | if (Array) { |
| 694 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 695 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 696 | llvm::Value *DestBaseAddrPtr = |
| 697 | Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
| 698 | llvm::Value *SrcBaseAddrPtr = |
| 699 | Builder.CreateBitCast(RHS.getAddress(), BasePtr); |
| 700 | EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array, |
| 701 | FieldClassDecl, FieldType); |
| 702 | } |
| 703 | else |
| 704 | EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(), |
| 705 | 0 /*ClassDecl*/, FieldClassDecl, FieldType); |
| 706 | continue; |
| 707 | } |
| 708 | |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 709 | // Do a built-in assignment of scalar data members. |
Anders Carlsson | 42c876d | 2010-01-29 05:41:25 +0000 | [diff] [blame] | 710 | LValue LHS = EmitLValueForFieldInitialization(LoadOfThis, Field, 0); |
| 711 | LValue RHS = EmitLValueForFieldInitialization(LoadOfSrc, Field, 0); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 712 | |
| 713 | if (!hasAggregateLLVMType(Field->getType())) { |
| 714 | RValue RVRHS = EmitLoadOfLValue(RHS, Field->getType()); |
| 715 | EmitStoreThroughLValue(RVRHS, LHS, Field->getType()); |
| 716 | } else if (Field->getType()->isAnyComplexType()) { |
| 717 | ComplexPairTy Pair = LoadComplexFromAddr(RHS.getAddress(), |
| 718 | RHS.isVolatileQualified()); |
| 719 | StoreComplexToAddr(Pair, LHS.getAddress(), LHS.isVolatileQualified()); |
| 720 | } else { |
| 721 | EmitAggregateCopy(LHS.getAddress(), RHS.getAddress(), Field->getType()); |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | InitializeVtablePtrs(ClassDecl); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | /// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator. |
| 729 | /// Before the implicitly-declared copy assignment operator for a class is |
| 730 | /// implicitly defined, all implicitly- declared copy assignment operators for |
| 731 | /// its direct base classes and its nonstatic data members shall have been |
| 732 | /// implicitly defined. [12.8-p12] |
| 733 | /// The implicitly-defined copy assignment operator for class X performs |
| 734 | /// memberwise assignment of its subob- jects. The direct base classes of X are |
| 735 | /// assigned first, in the order of their declaration in |
| 736 | /// the base-specifier-list, and then the immediate nonstatic data members of X |
| 737 | /// are assigned, in the order in which they were declared in the class |
| 738 | /// definition.Each subobject is assigned in the manner appropriate to its type: |
| 739 | /// if the subobject is of class type, the copy assignment operator for the |
| 740 | /// class is used (as if by explicit qualification; that is, ignoring any |
| 741 | /// possible virtual overriding functions in more derived classes); |
| 742 | /// |
| 743 | /// if the subobject is an array, each element is assigned, in the manner |
| 744 | /// appropriate to the element type; |
| 745 | /// |
| 746 | /// if the subobject is of scalar type, the built-in assignment operator is |
| 747 | /// used. |
| 748 | void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD, |
| 749 | llvm::Function *Fn, |
| 750 | const FunctionArgList &Args) { |
| 751 | |
| 752 | const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext()); |
| 753 | assert(!ClassDecl->hasUserDeclaredCopyAssignment() && |
| 754 | "SynthesizeCXXCopyAssignment - copy assignment has user declaration"); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 755 | |
| 756 | FunctionArgList::const_iterator i = Args.begin(); |
| 757 | const VarDecl *ThisArg = i->first; |
| 758 | llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg); |
| 759 | llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this"); |
| 760 | const VarDecl *SrcArg = (i+1)->first; |
| 761 | llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg); |
| 762 | llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj); |
| 763 | |
| 764 | for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); |
| 765 | Base != ClassDecl->bases_end(); ++Base) { |
| 766 | // FIXME. copy assignment of virtual base NYI |
| 767 | if (Base->isVirtual()) |
| 768 | continue; |
| 769 | |
| 770 | CXXRecordDecl *BaseClassDecl |
| 771 | = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); |
| 772 | EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl, |
| 773 | Base->getType()); |
| 774 | } |
| 775 | |
| 776 | for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), |
| 777 | FieldEnd = ClassDecl->field_end(); |
| 778 | Field != FieldEnd; ++Field) { |
| 779 | QualType FieldType = getContext().getCanonicalType((*Field)->getType()); |
| 780 | const ConstantArrayType *Array = |
| 781 | getContext().getAsConstantArrayType(FieldType); |
| 782 | if (Array) |
| 783 | FieldType = getContext().getBaseElementType(FieldType); |
| 784 | |
| 785 | if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { |
| 786 | CXXRecordDecl *FieldClassDecl |
| 787 | = cast<CXXRecordDecl>(FieldClassType->getDecl()); |
Anders Carlsson | 5d8645b | 2010-01-29 05:05:36 +0000 | [diff] [blame] | 788 | LValue LHS = EmitLValueForField(LoadOfThis, *Field, 0); |
| 789 | LValue RHS = EmitLValueForField(LoadOfSrc, *Field, 0); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 790 | if (Array) { |
| 791 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 792 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 793 | llvm::Value *DestBaseAddrPtr = |
| 794 | Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
| 795 | llvm::Value *SrcBaseAddrPtr = |
| 796 | Builder.CreateBitCast(RHS.getAddress(), BasePtr); |
| 797 | EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array, |
| 798 | FieldClassDecl, FieldType); |
| 799 | } |
| 800 | else |
| 801 | EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(), |
| 802 | 0 /*ClassDecl*/, FieldClassDecl, FieldType); |
| 803 | continue; |
| 804 | } |
| 805 | // Do a built-in assignment of scalar data members. |
Anders Carlsson | 5d8645b | 2010-01-29 05:05:36 +0000 | [diff] [blame] | 806 | LValue LHS = EmitLValueForField(LoadOfThis, *Field, 0); |
| 807 | LValue RHS = EmitLValueForField(LoadOfSrc, *Field, 0); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 808 | if (!hasAggregateLLVMType(Field->getType())) { |
| 809 | RValue RVRHS = EmitLoadOfLValue(RHS, Field->getType()); |
| 810 | EmitStoreThroughLValue(RVRHS, LHS, Field->getType()); |
| 811 | } else if (Field->getType()->isAnyComplexType()) { |
| 812 | ComplexPairTy Pair = LoadComplexFromAddr(RHS.getAddress(), |
| 813 | RHS.isVolatileQualified()); |
| 814 | StoreComplexToAddr(Pair, LHS.getAddress(), LHS.isVolatileQualified()); |
| 815 | } else { |
| 816 | EmitAggregateCopy(LHS.getAddress(), RHS.getAddress(), Field->getType()); |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | // return *this; |
| 821 | Builder.CreateStore(LoadOfThis, ReturnValue); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | static void EmitBaseInitializer(CodeGenFunction &CGF, |
| 825 | const CXXRecordDecl *ClassDecl, |
| 826 | CXXBaseOrMemberInitializer *BaseInit, |
| 827 | CXXCtorType CtorType) { |
| 828 | assert(BaseInit->isBaseInitializer() && |
| 829 | "Must have base initializer!"); |
| 830 | |
| 831 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
| 832 | |
| 833 | const Type *BaseType = BaseInit->getBaseClass(); |
| 834 | CXXRecordDecl *BaseClassDecl = |
| 835 | cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); |
| 836 | |
| 837 | // FIXME: This method of determining whether a base is virtual is ridiculous; |
| 838 | // it should be part of BaseInit. |
| 839 | bool isBaseVirtual = false; |
| 840 | for (CXXRecordDecl::base_class_const_iterator I = ClassDecl->vbases_begin(), |
| 841 | E = ClassDecl->vbases_end(); I != E; ++I) |
| 842 | if (I->getType()->getAs<RecordType>()->getDecl() == BaseClassDecl) { |
| 843 | isBaseVirtual = true; |
| 844 | break; |
| 845 | } |
| 846 | |
| 847 | // The base constructor doesn't construct virtual bases. |
| 848 | if (CtorType == Ctor_Base && isBaseVirtual) |
| 849 | return; |
| 850 | |
John McCall | 6ce7472 | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 851 | // We can pretend to be a complete class because it only matters for |
| 852 | // virtual bases, and we only do virtual bases for complete ctors. |
| 853 | llvm::Value *V = ThisPtr; |
| 854 | V = CGF.GetAddressOfBaseOfCompleteClass(V, isBaseVirtual, |
| 855 | ClassDecl, BaseClassDecl); |
| 856 | |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 857 | CGF.EmitAggExpr(BaseInit->getInit(), V, false, false, true); |
Anders Carlsson | 5ade5d3 | 2010-02-06 20:00:21 +0000 | [diff] [blame] | 858 | |
| 859 | if (CGF.Exceptions && !BaseClassDecl->hasTrivialDestructor()) { |
| 860 | // FIXME: Is this OK for C++0x delegating constructors? |
| 861 | CodeGenFunction::EHCleanupBlock Cleanup(CGF); |
| 862 | |
Anders Carlsson | 5ade5d3 | 2010-02-06 20:00:21 +0000 | [diff] [blame] | 863 | CXXDestructorDecl *DD = BaseClassDecl->getDestructor(CGF.getContext()); |
| 864 | CGF.EmitCXXDestructorCall(DD, Dtor_Base, V); |
| 865 | } |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | static void EmitMemberInitializer(CodeGenFunction &CGF, |
| 869 | const CXXRecordDecl *ClassDecl, |
| 870 | CXXBaseOrMemberInitializer *MemberInit) { |
| 871 | assert(MemberInit->isMemberInitializer() && |
| 872 | "Must have member initializer!"); |
| 873 | |
| 874 | // non-static data member initializers. |
| 875 | FieldDecl *Field = MemberInit->getMember(); |
| 876 | QualType FieldType = CGF.getContext().getCanonicalType(Field->getType()); |
| 877 | |
| 878 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
Anders Carlsson | db78f0a | 2010-01-29 05:24:29 +0000 | [diff] [blame] | 879 | LValue LHS = CGF.EmitLValueForFieldInitialization(ThisPtr, Field, 0); |
| 880 | |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 881 | // If we are initializing an anonymous union field, drill down to the field. |
| 882 | if (MemberInit->getAnonUnionMember()) { |
| 883 | Field = MemberInit->getAnonUnionMember(); |
Anders Carlsson | 5d8645b | 2010-01-29 05:05:36 +0000 | [diff] [blame] | 884 | LHS = CGF.EmitLValueForField(LHS.getAddress(), Field, 0); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 885 | FieldType = Field->getType(); |
| 886 | } |
| 887 | |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 888 | // FIXME: If there's no initializer and the CXXBaseOrMemberInitializer |
| 889 | // was implicitly generated, we shouldn't be zeroing memory. |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 890 | RValue RHS; |
| 891 | if (FieldType->isReferenceType()) { |
Anders Carlsson | 3b227bd | 2010-02-03 16:38:03 +0000 | [diff] [blame] | 892 | RHS = CGF.EmitReferenceBindingToExpr(MemberInit->getInit(), |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 893 | /*IsInitializer=*/true); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 894 | CGF.EmitStoreThroughLValue(RHS, LHS, FieldType); |
Eli Friedman | 6041797 | 2010-01-31 19:07:50 +0000 | [diff] [blame] | 895 | } else if (FieldType->isArrayType() && !MemberInit->getInit()) { |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 896 | CGF.EmitMemSetToZero(LHS.getAddress(), Field->getType()); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 897 | } else if (!CGF.hasAggregateLLVMType(Field->getType())) { |
| 898 | RHS = RValue::get(CGF.EmitScalarExpr(MemberInit->getInit(), true)); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 899 | CGF.EmitStoreThroughLValue(RHS, LHS, FieldType); |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 900 | } else if (MemberInit->getInit()->getType()->isAnyComplexType()) { |
| 901 | CGF.EmitComplexExprIntoAddr(MemberInit->getInit(), LHS.getAddress(), |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 902 | LHS.isVolatileQualified()); |
| 903 | } else { |
Douglas Gregor | 7ae2d77 | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 904 | CGF.EmitAggExpr(MemberInit->getInit(), LHS.getAddress(), |
| 905 | LHS.isVolatileQualified(), false, true); |
Anders Carlsson | ba63167 | 2010-02-06 19:50:17 +0000 | [diff] [blame] | 906 | |
| 907 | if (!CGF.Exceptions) |
| 908 | return; |
| 909 | |
| 910 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 911 | if (!RT) |
| 912 | return; |
| 913 | |
| 914 | CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); |
| 915 | if (!RD->hasTrivialDestructor()) { |
| 916 | // FIXME: Is this OK for C++0x delegating constructors? |
| 917 | CodeGenFunction::EHCleanupBlock Cleanup(CGF); |
| 918 | |
| 919 | llvm::Value *ThisPtr = CGF.LoadCXXThis(); |
| 920 | LValue LHS = CGF.EmitLValueForField(ThisPtr, Field, 0); |
| 921 | |
| 922 | CXXDestructorDecl *DD = RD->getDestructor(CGF.getContext()); |
| 923 | CGF.EmitCXXDestructorCall(DD, Dtor_Complete, LHS.getAddress()); |
| 924 | } |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | |
| 928 | /// EmitCtorPrologue - This routine generates necessary code to initialize |
| 929 | /// base classes and non-static data members belonging to this constructor. |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 930 | void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD, |
| 931 | CXXCtorType CtorType) { |
| 932 | const CXXRecordDecl *ClassDecl = CD->getParent(); |
Anders Carlsson | 5dc8633 | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 933 | |
| 934 | llvm::SmallVector<CXXBaseOrMemberInitializer *, 8> MemberInitializers; |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 935 | |
| 936 | // FIXME: Add vbase initialization |
| 937 | |
| 938 | for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(), |
| 939 | E = CD->init_end(); |
| 940 | B != E; ++B) { |
| 941 | CXXBaseOrMemberInitializer *Member = (*B); |
| 942 | |
| 943 | assert(LiveTemporaries.empty() && |
| 944 | "Should not have any live temporaries at initializer start!"); |
| 945 | |
| 946 | if (Member->isBaseInitializer()) |
| 947 | EmitBaseInitializer(*this, ClassDecl, Member, CtorType); |
| 948 | else |
Anders Carlsson | 5dc8633 | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 949 | MemberInitializers.push_back(Member); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | InitializeVtablePtrs(ClassDecl); |
Anders Carlsson | 5dc8633 | 2010-02-02 19:58:43 +0000 | [diff] [blame] | 953 | |
| 954 | for (unsigned I = 0, E = MemberInitializers.size(); I != E; ++I) { |
| 955 | assert(LiveTemporaries.empty() && |
| 956 | "Should not have any live temporaries at initializer start!"); |
| 957 | |
| 958 | EmitMemberInitializer(*this, ClassDecl, MemberInitializers[I]); |
| 959 | } |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | /// EmitDtorEpilogue - Emit all code that comes at the end of class's |
| 963 | /// destructor. This is to call destructors on members and base classes |
| 964 | /// in reverse order of their construction. |
| 965 | /// FIXME: This needs to take a CXXDtorType. |
| 966 | void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD, |
| 967 | CXXDtorType DtorType) { |
| 968 | assert(!DD->isTrivial() && |
| 969 | "Should not emit dtor epilogue for trivial dtor!"); |
| 970 | |
| 971 | const CXXRecordDecl *ClassDecl = DD->getParent(); |
| 972 | |
| 973 | // Collect the fields. |
| 974 | llvm::SmallVector<const FieldDecl *, 16> FieldDecls; |
| 975 | for (CXXRecordDecl::field_iterator I = ClassDecl->field_begin(), |
| 976 | E = ClassDecl->field_end(); I != E; ++I) { |
| 977 | const FieldDecl *Field = *I; |
| 978 | |
| 979 | QualType FieldType = getContext().getCanonicalType(Field->getType()); |
| 980 | FieldType = getContext().getBaseElementType(FieldType); |
| 981 | |
| 982 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 983 | if (!RT) |
| 984 | continue; |
| 985 | |
| 986 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 987 | if (FieldClassDecl->hasTrivialDestructor()) |
| 988 | continue; |
| 989 | |
| 990 | FieldDecls.push_back(Field); |
| 991 | } |
| 992 | |
| 993 | // Now destroy the fields. |
| 994 | for (size_t i = FieldDecls.size(); i > 0; --i) { |
| 995 | const FieldDecl *Field = FieldDecls[i - 1]; |
| 996 | |
| 997 | QualType FieldType = Field->getType(); |
| 998 | const ConstantArrayType *Array = |
| 999 | getContext().getAsConstantArrayType(FieldType); |
| 1000 | if (Array) |
| 1001 | FieldType = getContext().getBaseElementType(FieldType); |
| 1002 | |
| 1003 | const RecordType *RT = FieldType->getAs<RecordType>(); |
| 1004 | CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); |
| 1005 | |
| 1006 | llvm::Value *ThisPtr = LoadCXXThis(); |
| 1007 | |
| 1008 | LValue LHS = EmitLValueForField(ThisPtr, Field, |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1009 | // FIXME: Qualifiers? |
| 1010 | /*CVRQualifiers=*/0); |
| 1011 | if (Array) { |
| 1012 | const llvm::Type *BasePtr = ConvertType(FieldType); |
| 1013 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 1014 | llvm::Value *BaseAddrPtr = |
| 1015 | Builder.CreateBitCast(LHS.getAddress(), BasePtr); |
| 1016 | EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()), |
| 1017 | Array, BaseAddrPtr); |
| 1018 | } else |
| 1019 | EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()), |
| 1020 | Dtor_Complete, LHS.getAddress()); |
| 1021 | } |
| 1022 | |
| 1023 | // Destroy non-virtual bases. |
| 1024 | for (CXXRecordDecl::reverse_base_class_const_iterator I = |
| 1025 | ClassDecl->bases_rbegin(), E = ClassDecl->bases_rend(); I != E; ++I) { |
| 1026 | const CXXBaseSpecifier &Base = *I; |
| 1027 | |
| 1028 | // Ignore virtual bases. |
| 1029 | if (Base.isVirtual()) |
| 1030 | continue; |
| 1031 | |
| 1032 | CXXRecordDecl *BaseClassDecl |
| 1033 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| 1034 | |
| 1035 | // Ignore trivial destructors. |
| 1036 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1037 | continue; |
| 1038 | const CXXDestructorDecl *D = BaseClassDecl->getDestructor(getContext()); |
| 1039 | |
| 1040 | llvm::Value *V = GetAddressOfBaseClass(LoadCXXThis(), |
| 1041 | ClassDecl, BaseClassDecl, |
| 1042 | /*NullCheckValue=*/false); |
| 1043 | EmitCXXDestructorCall(D, Dtor_Base, V); |
| 1044 | } |
| 1045 | |
| 1046 | // If we're emitting a base destructor, we don't want to emit calls to the |
| 1047 | // virtual bases. |
| 1048 | if (DtorType == Dtor_Base) |
| 1049 | return; |
| 1050 | |
| 1051 | // Handle virtual bases. |
| 1052 | for (CXXRecordDecl::reverse_base_class_const_iterator I = |
| 1053 | ClassDecl->vbases_rbegin(), E = ClassDecl->vbases_rend(); I != E; ++I) { |
| 1054 | const CXXBaseSpecifier &Base = *I; |
| 1055 | CXXRecordDecl *BaseClassDecl |
John McCall | 6ce7472 | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 1056 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1057 | |
| 1058 | // Ignore trivial destructors. |
| 1059 | if (BaseClassDecl->hasTrivialDestructor()) |
| 1060 | continue; |
| 1061 | const CXXDestructorDecl *D = BaseClassDecl->getDestructor(getContext()); |
John McCall | 6ce7472 | 2010-02-16 04:15:37 +0000 | [diff] [blame] | 1062 | llvm::Value *V = GetAddressOfBaseOfCompleteClass(LoadCXXThis(), |
| 1063 | true, |
| 1064 | ClassDecl, |
| 1065 | BaseClassDecl); |
Anders Carlsson | fb40488 | 2009-12-24 22:46:43 +0000 | [diff] [blame] | 1066 | EmitCXXDestructorCall(D, Dtor_Base, V); |
| 1067 | } |
| 1068 | |
| 1069 | // If we have a deleting destructor, emit a call to the delete operator. |
| 1070 | if (DtorType == Dtor_Deleting) { |
| 1071 | assert(DD->getOperatorDelete() && |
| 1072 | "operator delete missing - EmitDtorEpilogue"); |
| 1073 | EmitDeleteCall(DD->getOperatorDelete(), LoadCXXThis(), |
| 1074 | getContext().getTagDeclType(ClassDecl)); |
| 1075 | } |
| 1076 | } |
| 1077 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1078 | /// EmitCXXAggrConstructorCall - This routine essentially creates a (nested) |
| 1079 | /// for-loop to call the default constructor on individual members of the |
| 1080 | /// array. |
| 1081 | /// 'D' is the default constructor for elements of the array, 'ArrayTy' is the |
| 1082 | /// array type and 'ArrayPtr' points to the beginning fo the array. |
| 1083 | /// It is assumed that all relevant checks have been made by the caller. |
| 1084 | void |
| 1085 | CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
| 1086 | const ConstantArrayType *ArrayTy, |
| 1087 | llvm::Value *ArrayPtr, |
| 1088 | CallExpr::const_arg_iterator ArgBeg, |
| 1089 | CallExpr::const_arg_iterator ArgEnd) { |
| 1090 | |
| 1091 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 1092 | llvm::Value * NumElements = |
| 1093 | llvm::ConstantInt::get(SizeTy, |
| 1094 | getContext().getConstantArrayElementCount(ArrayTy)); |
| 1095 | |
| 1096 | EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr, ArgBeg, ArgEnd); |
| 1097 | } |
| 1098 | |
| 1099 | void |
| 1100 | CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, |
| 1101 | llvm::Value *NumElements, |
| 1102 | llvm::Value *ArrayPtr, |
| 1103 | CallExpr::const_arg_iterator ArgBeg, |
| 1104 | CallExpr::const_arg_iterator ArgEnd) { |
| 1105 | const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); |
| 1106 | |
| 1107 | // Create a temporary for the loop index and initialize it with 0. |
| 1108 | llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index"); |
| 1109 | llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy); |
| 1110 | Builder.CreateStore(Zero, IndexPtr); |
| 1111 | |
| 1112 | // Start the loop with a block that tests the condition. |
| 1113 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 1114 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
| 1115 | |
| 1116 | EmitBlock(CondBlock); |
| 1117 | |
| 1118 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 1119 | |
| 1120 | // Generate: if (loop-index < number-of-elements fall to the loop body, |
| 1121 | // otherwise, go to the block after the for-loop. |
| 1122 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 1123 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless"); |
| 1124 | // If the condition is true, execute the body. |
| 1125 | Builder.CreateCondBr(IsLess, ForBody, AfterFor); |
| 1126 | |
| 1127 | EmitBlock(ForBody); |
| 1128 | |
| 1129 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 1130 | // Inside the loop body, emit the constructor call on the array element. |
| 1131 | Counter = Builder.CreateLoad(IndexPtr); |
| 1132 | llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter, |
| 1133 | "arrayidx"); |
| 1134 | |
| 1135 | // C++ [class.temporary]p4: |
| 1136 | // There are two contexts in which temporaries are destroyed at a different |
| 1137 | // point than the end of the full-expression. The first context is when a |
| 1138 | // default constructor is called to initialize an element of an array. |
| 1139 | // If the constructor has one or more default arguments, the destruction of |
| 1140 | // every temporary created in a default argument expression is sequenced |
| 1141 | // before the construction of the next array element, if any. |
| 1142 | |
| 1143 | // Keep track of the current number of live temporaries. |
| 1144 | unsigned OldNumLiveTemporaries = LiveTemporaries.size(); |
| 1145 | |
| 1146 | EmitCXXConstructorCall(D, Ctor_Complete, Address, ArgBeg, ArgEnd); |
| 1147 | |
| 1148 | // Pop temporaries. |
| 1149 | while (LiveTemporaries.size() > OldNumLiveTemporaries) |
| 1150 | PopCXXTemporary(); |
| 1151 | |
| 1152 | EmitBlock(ContinueBlock); |
| 1153 | |
| 1154 | // Emit the increment of the loop counter. |
| 1155 | llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1); |
| 1156 | Counter = Builder.CreateLoad(IndexPtr); |
| 1157 | NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); |
| 1158 | Builder.CreateStore(NextVal, IndexPtr); |
| 1159 | |
| 1160 | // Finally, branch back up to the condition for the next iteration. |
| 1161 | EmitBranch(CondBlock); |
| 1162 | |
| 1163 | // Emit the fall-through block. |
| 1164 | EmitBlock(AfterFor, true); |
| 1165 | } |
| 1166 | |
| 1167 | /// EmitCXXAggrDestructorCall - calls the default destructor on array |
| 1168 | /// elements in reverse order of construction. |
| 1169 | void |
| 1170 | CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, |
| 1171 | const ArrayType *Array, |
| 1172 | llvm::Value *This) { |
| 1173 | const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); |
| 1174 | assert(CA && "Do we support VLA for destruction ?"); |
| 1175 | uint64_t ElementCount = getContext().getConstantArrayElementCount(CA); |
| 1176 | |
| 1177 | const llvm::Type *SizeLTy = ConvertType(getContext().getSizeType()); |
| 1178 | llvm::Value* ElementCountPtr = llvm::ConstantInt::get(SizeLTy, ElementCount); |
| 1179 | EmitCXXAggrDestructorCall(D, ElementCountPtr, This); |
| 1180 | } |
| 1181 | |
| 1182 | /// EmitCXXAggrDestructorCall - calls the default destructor on array |
| 1183 | /// elements in reverse order of construction. |
| 1184 | void |
| 1185 | CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, |
| 1186 | llvm::Value *UpperCount, |
| 1187 | llvm::Value *This) { |
| 1188 | const llvm::Type *SizeLTy = ConvertType(getContext().getSizeType()); |
| 1189 | llvm::Value *One = llvm::ConstantInt::get(SizeLTy, 1); |
| 1190 | |
| 1191 | // Create a temporary for the loop index and initialize it with count of |
| 1192 | // array elements. |
| 1193 | llvm::Value *IndexPtr = CreateTempAlloca(SizeLTy, "loop.index"); |
| 1194 | |
| 1195 | // Store the number of elements in the index pointer. |
| 1196 | Builder.CreateStore(UpperCount, IndexPtr); |
| 1197 | |
| 1198 | // Start the loop with a block that tests the condition. |
| 1199 | llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); |
| 1200 | llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); |
| 1201 | |
| 1202 | EmitBlock(CondBlock); |
| 1203 | |
| 1204 | llvm::BasicBlock *ForBody = createBasicBlock("for.body"); |
| 1205 | |
| 1206 | // Generate: if (loop-index != 0 fall to the loop body, |
| 1207 | // otherwise, go to the block after the for-loop. |
| 1208 | llvm::Value* zeroConstant = |
| 1209 | llvm::Constant::getNullValue(SizeLTy); |
| 1210 | llvm::Value *Counter = Builder.CreateLoad(IndexPtr); |
| 1211 | llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant, |
| 1212 | "isne"); |
| 1213 | // If the condition is true, execute the body. |
| 1214 | Builder.CreateCondBr(IsNE, ForBody, AfterFor); |
| 1215 | |
| 1216 | EmitBlock(ForBody); |
| 1217 | |
| 1218 | llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); |
| 1219 | // Inside the loop body, emit the constructor call on the array element. |
| 1220 | Counter = Builder.CreateLoad(IndexPtr); |
| 1221 | Counter = Builder.CreateSub(Counter, One); |
| 1222 | llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx"); |
| 1223 | EmitCXXDestructorCall(D, Dtor_Complete, Address); |
| 1224 | |
| 1225 | EmitBlock(ContinueBlock); |
| 1226 | |
| 1227 | // Emit the decrement of the loop counter. |
| 1228 | Counter = Builder.CreateLoad(IndexPtr); |
| 1229 | Counter = Builder.CreateSub(Counter, One, "dec"); |
| 1230 | Builder.CreateStore(Counter, IndexPtr); |
| 1231 | |
| 1232 | // Finally, branch back up to the condition for the next iteration. |
| 1233 | EmitBranch(CondBlock); |
| 1234 | |
| 1235 | // Emit the fall-through block. |
| 1236 | EmitBlock(AfterFor, true); |
| 1237 | } |
| 1238 | |
| 1239 | /// GenerateCXXAggrDestructorHelper - Generates a helper function which when |
| 1240 | /// invoked, calls the default destructor on array elements in reverse order of |
| 1241 | /// construction. |
| 1242 | llvm::Constant * |
| 1243 | CodeGenFunction::GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D, |
| 1244 | const ArrayType *Array, |
| 1245 | llvm::Value *This) { |
| 1246 | FunctionArgList Args; |
| 1247 | ImplicitParamDecl *Dst = |
| 1248 | ImplicitParamDecl::Create(getContext(), 0, |
| 1249 | SourceLocation(), 0, |
| 1250 | getContext().getPointerType(getContext().VoidTy)); |
| 1251 | Args.push_back(std::make_pair(Dst, Dst->getType())); |
| 1252 | |
| 1253 | llvm::SmallString<16> Name; |
| 1254 | llvm::raw_svector_ostream(Name) << "__tcf_" << (++UniqueAggrDestructorCount); |
| 1255 | QualType R = getContext().VoidTy; |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1256 | const CGFunctionInfo &FI |
| 1257 | = CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1258 | const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false); |
| 1259 | llvm::Function *Fn = |
| 1260 | llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, |
| 1261 | Name.str(), |
| 1262 | &CGM.getModule()); |
| 1263 | IdentifierInfo *II = &CGM.getContext().Idents.get(Name.str()); |
| 1264 | FunctionDecl *FD = FunctionDecl::Create(getContext(), |
| 1265 | getContext().getTranslationUnitDecl(), |
| 1266 | SourceLocation(), II, R, 0, |
| 1267 | FunctionDecl::Static, |
| 1268 | false, true); |
| 1269 | StartFunction(FD, R, Fn, Args, SourceLocation()); |
| 1270 | QualType BaseElementTy = getContext().getBaseElementType(Array); |
| 1271 | const llvm::Type *BasePtr = ConvertType(BaseElementTy); |
| 1272 | BasePtr = llvm::PointerType::getUnqual(BasePtr); |
| 1273 | llvm::Value *BaseAddrPtr = Builder.CreateBitCast(This, BasePtr); |
| 1274 | EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr); |
| 1275 | FinishFunction(); |
| 1276 | llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), |
| 1277 | 0); |
| 1278 | llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); |
| 1279 | return m; |
| 1280 | } |
| 1281 | |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1282 | |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1283 | void |
| 1284 | CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, |
| 1285 | CXXCtorType Type, |
| 1286 | llvm::Value *This, |
| 1287 | CallExpr::const_arg_iterator ArgBeg, |
| 1288 | CallExpr::const_arg_iterator ArgEnd) { |
John McCall | ca972cd | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1289 | if (D->isTrivial()) { |
| 1290 | if (ArgBeg == ArgEnd) { |
| 1291 | // Trivial default constructor, no codegen required. |
| 1292 | assert(D->isDefaultConstructor() && |
| 1293 | "trivial 0-arg ctor not a default ctor"); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1294 | return; |
| 1295 | } |
John McCall | ca972cd | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1296 | |
| 1297 | assert(ArgBeg + 1 == ArgEnd && "unexpected argcount for trivial ctor"); |
| 1298 | assert(D->isCopyConstructor() && "trivial 1-arg ctor not a copy ctor"); |
| 1299 | |
John McCall | ca972cd | 2010-02-06 00:25:16 +0000 | [diff] [blame] | 1300 | const Expr *E = (*ArgBeg); |
| 1301 | QualType Ty = E->getType(); |
| 1302 | llvm::Value *Src = EmitLValue(E).getAddress(); |
| 1303 | EmitAggregateCopy(This, Src, Ty); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1304 | return; |
| 1305 | } |
| 1306 | |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1307 | llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(D, Type)); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1308 | llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type); |
| 1309 | |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1310 | EmitCXXMemberCall(D, Callee, ReturnValueSlot(), This, VTT, ArgBeg, ArgEnd); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *DD, |
| 1314 | CXXDtorType Type, |
| 1315 | llvm::Value *This) { |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1316 | llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(DD, Type)); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1317 | llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(DD, Type); |
| 1318 | |
Anders Carlsson | e36a6b3 | 2010-01-02 01:01:18 +0000 | [diff] [blame] | 1319 | EmitCXXMemberCall(DD, Callee, ReturnValueSlot(), This, VTT, 0, 0); |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | llvm::Value * |
Anders Carlsson | 84673e2 | 2010-01-31 01:36:53 +0000 | [diff] [blame] | 1323 | CodeGenFunction::GetVirtualBaseClassOffset(llvm::Value *This, |
| 1324 | const CXXRecordDecl *ClassDecl, |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1325 | const CXXRecordDecl *BaseClassDecl) { |
| 1326 | const llvm::Type *Int8PtrTy = |
| 1327 | llvm::Type::getInt8Ty(VMContext)->getPointerTo(); |
| 1328 | |
| 1329 | llvm::Value *VTablePtr = Builder.CreateBitCast(This, |
| 1330 | Int8PtrTy->getPointerTo()); |
| 1331 | VTablePtr = Builder.CreateLoad(VTablePtr, "vtable"); |
| 1332 | |
| 1333 | int64_t VBaseOffsetIndex = |
| 1334 | CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl); |
| 1335 | |
| 1336 | llvm::Value *VBaseOffsetPtr = |
| 1337 | Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr"); |
| 1338 | const llvm::Type *PtrDiffTy = |
| 1339 | ConvertType(getContext().getPointerDiffType()); |
| 1340 | |
| 1341 | VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr, |
| 1342 | PtrDiffTy->getPointerTo()); |
| 1343 | |
| 1344 | llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset"); |
| 1345 | |
| 1346 | return VBaseOffset; |
| 1347 | } |
| 1348 | |
| 1349 | void CodeGenFunction::InitializeVtablePtrs(const CXXRecordDecl *ClassDecl) { |
| 1350 | if (!ClassDecl->isDynamicClass()) |
| 1351 | return; |
| 1352 | |
| 1353 | llvm::Constant *Vtable = CGM.getVtableInfo().getVtable(ClassDecl); |
Anders Carlsson | 93a1884 | 2010-01-02 18:02:32 +0000 | [diff] [blame] | 1354 | CGVtableInfo::AddrSubMap_t& AddressPoints = |
| 1355 | *(*CGM.getVtableInfo().AddressPoints[ClassDecl])[ClassDecl]; |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1356 | llvm::Value *ThisPtr = LoadCXXThis(); |
| 1357 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(ClassDecl); |
| 1358 | |
| 1359 | // Store address points for virtual bases |
| 1360 | for (CXXRecordDecl::base_class_const_iterator I = |
| 1361 | ClassDecl->vbases_begin(), E = ClassDecl->vbases_end(); I != E; ++I) { |
| 1362 | const CXXBaseSpecifier &Base = *I; |
| 1363 | CXXRecordDecl *BaseClassDecl |
| 1364 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| 1365 | uint64_t Offset = Layout.getVBaseClassOffset(BaseClassDecl); |
| 1366 | InitializeVtablePtrsRecursive(BaseClassDecl, Vtable, AddressPoints, |
| 1367 | ThisPtr, Offset); |
| 1368 | } |
| 1369 | |
| 1370 | // Store address points for non-virtual bases and current class |
| 1371 | InitializeVtablePtrsRecursive(ClassDecl, Vtable, AddressPoints, ThisPtr, 0); |
| 1372 | } |
| 1373 | |
| 1374 | void CodeGenFunction::InitializeVtablePtrsRecursive( |
| 1375 | const CXXRecordDecl *ClassDecl, |
| 1376 | llvm::Constant *Vtable, |
Anders Carlsson | 93a1884 | 2010-01-02 18:02:32 +0000 | [diff] [blame] | 1377 | CGVtableInfo::AddrSubMap_t& AddressPoints, |
Anders Carlsson | 27da15b | 2010-01-01 20:29:01 +0000 | [diff] [blame] | 1378 | llvm::Value *ThisPtr, |
| 1379 | uint64_t Offset) { |
| 1380 | if (!ClassDecl->isDynamicClass()) |
| 1381 | return; |
| 1382 | |
| 1383 | // Store address points for non-virtual bases |
| 1384 | const ASTRecordLayout &Layout = getContext().getASTRecordLayout(ClassDecl); |
| 1385 | for (CXXRecordDecl::base_class_const_iterator I = |
| 1386 | ClassDecl->bases_begin(), E = ClassDecl->bases_end(); I != E; ++I) { |
| 1387 | const CXXBaseSpecifier &Base = *I; |
| 1388 | if (Base.isVirtual()) |
| 1389 | continue; |
| 1390 | CXXRecordDecl *BaseClassDecl |
| 1391 | = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); |
| 1392 | uint64_t NewOffset = Offset + Layout.getBaseClassOffset(BaseClassDecl); |
| 1393 | InitializeVtablePtrsRecursive(BaseClassDecl, Vtable, AddressPoints, |
| 1394 | ThisPtr, NewOffset); |
| 1395 | } |
| 1396 | |
| 1397 | // Compute the address point |
| 1398 | assert(AddressPoints.count(std::make_pair(ClassDecl, Offset)) && |
| 1399 | "Missing address point for class"); |
| 1400 | uint64_t AddressPoint = AddressPoints[std::make_pair(ClassDecl, Offset)]; |
| 1401 | llvm::Value *VtableAddressPoint = |
| 1402 | Builder.CreateConstInBoundsGEP2_64(Vtable, 0, AddressPoint); |
| 1403 | |
| 1404 | // Compute the address to store the address point |
| 1405 | const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); |
| 1406 | llvm::Value *VtableField = Builder.CreateBitCast(ThisPtr, Int8PtrTy); |
| 1407 | VtableField = Builder.CreateConstInBoundsGEP1_64(VtableField, Offset/8); |
| 1408 | const llvm::Type *AddressPointPtrTy = |
| 1409 | VtableAddressPoint->getType()->getPointerTo(); |
| 1410 | VtableField = Builder.CreateBitCast(VtableField, AddressPointPtrTy); |
| 1411 | |
| 1412 | // Store address point |
| 1413 | Builder.CreateStore(VtableAddressPoint, VtableField); |
| 1414 | } |