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