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