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