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