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