CGCXX.cpp revision cb48f8a60ad26b56d299cca9a57f8ea683d8a909
1//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===// 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. 11// 12//===----------------------------------------------------------------------===// 13 14// We might split this into multiple files if it gets too unwieldy 15 16#include "CodeGenFunction.h" 17#include "CodeGenModule.h" 18#include "Mangle.h" 19#include "clang/AST/ASTContext.h" 20#include "clang/AST/RecordLayout.h" 21#include "clang/AST/Decl.h" 22#include "clang/AST/DeclCXX.h" 23#include "clang/AST/DeclObjC.h" 24#include "clang/AST/StmtCXX.h" 25#include "llvm/ADT/StringExtras.h" 26using namespace clang; 27using namespace CodeGen; 28 29RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD, 30 llvm::Value *Callee, 31 ReturnValueSlot ReturnValue, 32 llvm::Value *This, 33 CallExpr::const_arg_iterator ArgBeg, 34 CallExpr::const_arg_iterator ArgEnd) { 35 assert(MD->isInstance() && 36 "Trying to emit a member call expr on a static method!"); 37 38 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 39 40 CallArgList Args; 41 42 // Push the this ptr. 43 Args.push_back(std::make_pair(RValue::get(This), 44 MD->getThisType(getContext()))); 45 46 // And the rest of the call args 47 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd); 48 49 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType(); 50 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args), Callee, 51 ReturnValue, Args, MD); 52} 53 54/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given 55/// expr can be devirtualized. 56static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) { 57 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) { 58 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) { 59 // This is a record decl. We know the type and can devirtualize it. 60 return VD->getType()->isRecordType(); 61 } 62 63 return false; 64 } 65 66 // We can always devirtualize calls on temporary object expressions. 67 if (isa<CXXTemporaryObjectExpr>(Base)) 68 return true; 69 70 // And calls on bound temporaries. 71 if (isa<CXXBindTemporaryExpr>(Base)) 72 return true; 73 74 // Check if this is a call expr that returns a record type. 75 if (const CallExpr *CE = dyn_cast<CallExpr>(Base)) 76 return CE->getCallReturnType()->isRecordType(); 77 78 // We can't devirtualize the call. 79 return false; 80} 81 82RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE, 83 ReturnValueSlot ReturnValue) { 84 if (isa<BinaryOperator>(CE->getCallee()->IgnoreParens())) 85 return EmitCXXMemberPointerCallExpr(CE, ReturnValue); 86 87 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee()->IgnoreParens()); 88 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl()); 89 90 if (MD->isStatic()) { 91 // The method is static, emit it as we would a regular call. 92 llvm::Value *Callee = CGM.GetAddrOfFunction(MD); 93 return EmitCall(getContext().getPointerType(MD->getType()), Callee, 94 ReturnValue, CE->arg_begin(), CE->arg_end()); 95 } 96 97 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 98 99 const llvm::Type *Ty = 100 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), 101 FPT->isVariadic()); 102 llvm::Value *This; 103 104 if (ME->isArrow()) 105 This = EmitScalarExpr(ME->getBase()); 106 else { 107 LValue BaseLV = EmitLValue(ME->getBase()); 108 This = BaseLV.getAddress(); 109 } 110 111 if (MD->isCopyAssignment() && MD->isTrivial()) { 112 // We don't like to generate the trivial copy assignment operator when 113 // it isn't necessary; just produce the proper effect here. 114 llvm::Value *RHS = EmitLValue(*CE->arg_begin()).getAddress(); 115 EmitAggregateCopy(This, RHS, CE->getType()); 116 return RValue::get(This); 117 } 118 119 // C++ [class.virtual]p12: 120 // Explicit qualification with the scope operator (5.1) suppresses the 121 // virtual call mechanism. 122 // 123 // We also don't emit a virtual call if the base expression has a record type 124 // because then we know what the type is. 125 llvm::Value *Callee; 126 if (const CXXDestructorDecl *Destructor 127 = dyn_cast<CXXDestructorDecl>(MD)) { 128 if (Destructor->isTrivial()) 129 return RValue::get(0); 130 if (MD->isVirtual() && !ME->hasQualifier() && 131 !canDevirtualizeMemberFunctionCalls(ME->getBase())) { 132 Callee = BuildVirtualCall(Destructor, Dtor_Complete, This, Ty); 133 } else { 134 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty); 135 } 136 } else if (MD->isVirtual() && !ME->hasQualifier() && 137 !canDevirtualizeMemberFunctionCalls(ME->getBase())) { 138 Callee = BuildVirtualCall(MD, This, Ty); 139 } else { 140 Callee = CGM.GetAddrOfFunction(MD, Ty); 141 } 142 143 return EmitCXXMemberCall(MD, Callee, ReturnValue, This, 144 CE->arg_begin(), CE->arg_end()); 145} 146 147RValue 148CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E, 149 ReturnValueSlot ReturnValue) { 150 const BinaryOperator *BO = 151 cast<BinaryOperator>(E->getCallee()->IgnoreParens()); 152 const Expr *BaseExpr = BO->getLHS(); 153 const Expr *MemFnExpr = BO->getRHS(); 154 155 const MemberPointerType *MPT = 156 MemFnExpr->getType()->getAs<MemberPointerType>(); 157 const FunctionProtoType *FPT = 158 MPT->getPointeeType()->getAs<FunctionProtoType>(); 159 const CXXRecordDecl *RD = 160 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl()); 161 162 const llvm::FunctionType *FTy = 163 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT), 164 FPT->isVariadic()); 165 166 const llvm::Type *Int8PtrTy = 167 llvm::Type::getInt8Ty(VMContext)->getPointerTo(); 168 169 // Get the member function pointer. 170 llvm::Value *MemFnPtr = 171 CreateTempAlloca(ConvertType(MemFnExpr->getType()), "mem.fn"); 172 EmitAggExpr(MemFnExpr, MemFnPtr, /*VolatileDest=*/false); 173 174 // Emit the 'this' pointer. 175 llvm::Value *This; 176 177 if (BO->getOpcode() == BinaryOperator::PtrMemI) 178 This = EmitScalarExpr(BaseExpr); 179 else 180 This = EmitLValue(BaseExpr).getAddress(); 181 182 // Adjust it. 183 llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1); 184 Adj = Builder.CreateLoad(Adj, "mem.fn.adj"); 185 186 llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr"); 187 Ptr = Builder.CreateGEP(Ptr, Adj, "adj"); 188 189 This = Builder.CreateBitCast(Ptr, This->getType(), "this"); 190 191 llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr"); 192 193 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType()); 194 195 llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn"); 196 197 // If the LSB in the function pointer is 1, the function pointer points to 198 // a virtual function. 199 llvm::Value *IsVirtual 200 = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1), 201 "and"); 202 203 IsVirtual = Builder.CreateTrunc(IsVirtual, 204 llvm::Type::getInt1Ty(VMContext)); 205 206 llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual"); 207 llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual"); 208 llvm::BasicBlock *FnEnd = createBasicBlock("fn.end"); 209 210 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual); 211 EmitBlock(FnVirtual); 212 213 const llvm::Type *VTableTy = 214 FTy->getPointerTo()->getPointerTo()->getPointerTo(); 215 216 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy); 217 VTable = Builder.CreateLoad(VTable); 218 219 VTable = Builder.CreateGEP(VTable, FnAsInt, "fn"); 220 221 // Since the function pointer is 1 plus the virtual table offset, we 222 // subtract 1 by using a GEP. 223 VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1); 224 225 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn"); 226 227 EmitBranch(FnEnd); 228 EmitBlock(FnNonVirtual); 229 230 // If the function is not virtual, just load the pointer. 231 llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn"); 232 NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo()); 233 234 EmitBlock(FnEnd); 235 236 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo()); 237 Callee->reserveOperandSpace(2); 238 Callee->addIncoming(VirtualFn, FnVirtual); 239 Callee->addIncoming(NonVirtualFn, FnNonVirtual); 240 241 CallArgList Args; 242 243 QualType ThisType = 244 getContext().getPointerType(getContext().getTagDeclType(RD)); 245 246 // Push the this ptr. 247 Args.push_back(std::make_pair(RValue::get(This), ThisType)); 248 249 // And the rest of the call args 250 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end()); 251 QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType(); 252 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args), Callee, 253 ReturnValue, Args); 254} 255 256RValue 257CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, 258 const CXXMethodDecl *MD, 259 ReturnValueSlot ReturnValue) { 260 assert(MD->isInstance() && 261 "Trying to emit a member call expr on a static method!"); 262 263 if (MD->isCopyAssignment()) { 264 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext()); 265 if (ClassDecl->hasTrivialCopyAssignment()) { 266 assert(!ClassDecl->hasUserDeclaredCopyAssignment() && 267 "EmitCXXOperatorMemberCallExpr - user declared copy assignment"); 268 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress(); 269 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress(); 270 QualType Ty = E->getType(); 271 EmitAggregateCopy(This, Src, Ty); 272 return RValue::get(This); 273 } 274 } 275 276 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 277 const llvm::Type *Ty = 278 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), 279 FPT->isVariadic()); 280 281 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress(); 282 283 llvm::Value *Callee; 284 if (MD->isVirtual() && !canDevirtualizeMemberFunctionCalls(E->getArg(0))) 285 Callee = BuildVirtualCall(MD, This, Ty); 286 else 287 Callee = CGM.GetAddrOfFunction(MD, Ty); 288 289 return EmitCXXMemberCall(MD, Callee, ReturnValue, This, 290 E->arg_begin() + 1, E->arg_end()); 291} 292 293llvm::Value *CodeGenFunction::LoadCXXThis() { 294 assert(isa<CXXMethodDecl>(CurFuncDecl) && 295 "Must be in a C++ member function decl to load 'this'"); 296 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() && 297 "Must be in a C++ member function decl to load 'this'"); 298 299 // FIXME: What if we're inside a block? 300 // ans: See how CodeGenFunction::LoadObjCSelf() uses 301 // CodeGenFunction::BlockForwardSelf() for how to do this. 302 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this"); 303} 304 305/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested) 306/// for-loop to call the default constructor on individual members of the 307/// array. 308/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the 309/// array type and 'ArrayPtr' points to the beginning fo the array. 310/// It is assumed that all relevant checks have been made by the caller. 311void 312CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, 313 const ConstantArrayType *ArrayTy, 314 llvm::Value *ArrayPtr, 315 CallExpr::const_arg_iterator ArgBeg, 316 CallExpr::const_arg_iterator ArgEnd) { 317 318 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 319 llvm::Value * NumElements = 320 llvm::ConstantInt::get(SizeTy, 321 getContext().getConstantArrayElementCount(ArrayTy)); 322 323 EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr, ArgBeg, ArgEnd); 324} 325 326void 327CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, 328 llvm::Value *NumElements, 329 llvm::Value *ArrayPtr, 330 CallExpr::const_arg_iterator ArgBeg, 331 CallExpr::const_arg_iterator ArgEnd) { 332 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 333 334 // Create a temporary for the loop index and initialize it with 0. 335 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index"); 336 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy); 337 Builder.CreateStore(Zero, IndexPtr); 338 339 // Start the loop with a block that tests the condition. 340 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); 341 llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); 342 343 EmitBlock(CondBlock); 344 345 llvm::BasicBlock *ForBody = createBasicBlock("for.body"); 346 347 // Generate: if (loop-index < number-of-elements fall to the loop body, 348 // otherwise, go to the block after the for-loop. 349 llvm::Value *Counter = Builder.CreateLoad(IndexPtr); 350 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless"); 351 // If the condition is true, execute the body. 352 Builder.CreateCondBr(IsLess, ForBody, AfterFor); 353 354 EmitBlock(ForBody); 355 356 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); 357 // Inside the loop body, emit the constructor call on the array element. 358 Counter = Builder.CreateLoad(IndexPtr); 359 llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter, 360 "arrayidx"); 361 362 // C++ [class.temporary]p4: 363 // There are two contexts in which temporaries are destroyed at a different 364 // point than the end of the full-expression. The first context is when a 365 // default constructor is called to initialize an element of an array. 366 // If the constructor has one or more default arguments, the destruction of 367 // every temporary created in a default argument expression is sequenced 368 // before the construction of the next array element, if any. 369 370 // Keep track of the current number of live temporaries. 371 unsigned OldNumLiveTemporaries = LiveTemporaries.size(); 372 373 EmitCXXConstructorCall(D, Ctor_Complete, Address, ArgBeg, ArgEnd); 374 375 // Pop temporaries. 376 while (LiveTemporaries.size() > OldNumLiveTemporaries) 377 PopCXXTemporary(); 378 379 EmitBlock(ContinueBlock); 380 381 // Emit the increment of the loop counter. 382 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1); 383 Counter = Builder.CreateLoad(IndexPtr); 384 NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); 385 Builder.CreateStore(NextVal, IndexPtr); 386 387 // Finally, branch back up to the condition for the next iteration. 388 EmitBranch(CondBlock); 389 390 // Emit the fall-through block. 391 EmitBlock(AfterFor, true); 392} 393 394/// EmitCXXAggrDestructorCall - calls the default destructor on array 395/// elements in reverse order of construction. 396void 397CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, 398 const ArrayType *Array, 399 llvm::Value *This) { 400 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); 401 assert(CA && "Do we support VLA for destruction ?"); 402 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA); 403 404 const llvm::Type *SizeLTy = ConvertType(getContext().getSizeType()); 405 llvm::Value* ElementCountPtr = llvm::ConstantInt::get(SizeLTy, ElementCount); 406 EmitCXXAggrDestructorCall(D, ElementCountPtr, This); 407} 408 409/// EmitCXXAggrDestructorCall - calls the default destructor on array 410/// elements in reverse order of construction. 411void 412CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, 413 llvm::Value *UpperCount, 414 llvm::Value *This) { 415 const llvm::Type *SizeLTy = ConvertType(getContext().getSizeType()); 416 llvm::Value *One = llvm::ConstantInt::get(SizeLTy, 1); 417 418 // Create a temporary for the loop index and initialize it with count of 419 // array elements. 420 llvm::Value *IndexPtr = CreateTempAlloca(SizeLTy, "loop.index"); 421 422 // Store the number of elements in the index pointer. 423 Builder.CreateStore(UpperCount, IndexPtr); 424 425 // Start the loop with a block that tests the condition. 426 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); 427 llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); 428 429 EmitBlock(CondBlock); 430 431 llvm::BasicBlock *ForBody = createBasicBlock("for.body"); 432 433 // Generate: if (loop-index != 0 fall to the loop body, 434 // otherwise, go to the block after the for-loop. 435 llvm::Value* zeroConstant = 436 llvm::Constant::getNullValue(SizeLTy); 437 llvm::Value *Counter = Builder.CreateLoad(IndexPtr); 438 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant, 439 "isne"); 440 // If the condition is true, execute the body. 441 Builder.CreateCondBr(IsNE, ForBody, AfterFor); 442 443 EmitBlock(ForBody); 444 445 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); 446 // Inside the loop body, emit the constructor call on the array element. 447 Counter = Builder.CreateLoad(IndexPtr); 448 Counter = Builder.CreateSub(Counter, One); 449 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx"); 450 EmitCXXDestructorCall(D, Dtor_Complete, Address); 451 452 EmitBlock(ContinueBlock); 453 454 // Emit the decrement of the loop counter. 455 Counter = Builder.CreateLoad(IndexPtr); 456 Counter = Builder.CreateSub(Counter, One, "dec"); 457 Builder.CreateStore(Counter, IndexPtr); 458 459 // Finally, branch back up to the condition for the next iteration. 460 EmitBranch(CondBlock); 461 462 // Emit the fall-through block. 463 EmitBlock(AfterFor, true); 464} 465 466/// GenerateCXXAggrDestructorHelper - Generates a helper function which when 467/// invoked, calls the default destructor on array elements in reverse order of 468/// construction. 469llvm::Constant * 470CodeGenFunction::GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D, 471 const ArrayType *Array, 472 llvm::Value *This) { 473 FunctionArgList Args; 474 ImplicitParamDecl *Dst = 475 ImplicitParamDecl::Create(getContext(), 0, 476 SourceLocation(), 0, 477 getContext().getPointerType(getContext().VoidTy)); 478 Args.push_back(std::make_pair(Dst, Dst->getType())); 479 480 llvm::SmallString<16> Name; 481 llvm::raw_svector_ostream(Name) << "__tcf_" << (++UniqueAggrDestructorCount); 482 QualType R = getContext().VoidTy; 483 const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(R, Args); 484 const llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI, false); 485 llvm::Function *Fn = 486 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, 487 Name.str(), 488 &CGM.getModule()); 489 IdentifierInfo *II = &CGM.getContext().Idents.get(Name.str()); 490 FunctionDecl *FD = FunctionDecl::Create(getContext(), 491 getContext().getTranslationUnitDecl(), 492 SourceLocation(), II, R, 0, 493 FunctionDecl::Static, 494 false, true); 495 StartFunction(FD, R, Fn, Args, SourceLocation()); 496 QualType BaseElementTy = getContext().getBaseElementType(Array); 497 const llvm::Type *BasePtr = ConvertType(BaseElementTy); 498 BasePtr = llvm::PointerType::getUnqual(BasePtr); 499 llvm::Value *BaseAddrPtr = Builder.CreateBitCast(This, BasePtr); 500 EmitCXXAggrDestructorCall(D, Array, BaseAddrPtr); 501 FinishFunction(); 502 llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 503 0); 504 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); 505 return m; 506} 507 508void 509CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, 510 CXXCtorType Type, 511 llvm::Value *This, 512 CallExpr::const_arg_iterator ArgBeg, 513 CallExpr::const_arg_iterator ArgEnd) { 514 if (D->isCopyConstructor()) { 515 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext()); 516 if (ClassDecl->hasTrivialCopyConstructor()) { 517 assert(!ClassDecl->hasUserDeclaredCopyConstructor() && 518 "EmitCXXConstructorCall - user declared copy constructor"); 519 const Expr *E = (*ArgBeg); 520 QualType Ty = E->getType(); 521 llvm::Value *Src = EmitLValue(E).getAddress(); 522 EmitAggregateCopy(This, Src, Ty); 523 return; 524 } 525 } else if (D->isTrivial()) { 526 // FIXME: Track down why we're trying to generate calls to the trivial 527 // default constructor! 528 return; 529 } 530 531 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type); 532 533 EmitCXXMemberCall(D, Callee, ReturnValueSlot(), This, ArgBeg, ArgEnd); 534} 535 536void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *DD, 537 CXXDtorType Type, 538 llvm::Value *This) { 539 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(DD, Type); 540 541 CallArgList Args; 542 543 // Push the this ptr. 544 Args.push_back(std::make_pair(RValue::get(This), 545 DD->getThisType(getContext()))); 546 547 // Add a VTT parameter if necessary. 548 // FIXME: This should not be a dummy null parameter! 549 if (Type == Dtor_Base && DD->getParent()->getNumVBases() != 0) { 550 QualType T = getContext().getPointerType(getContext().VoidPtrTy); 551 552 Args.push_back(std::make_pair(RValue::get(CGM.EmitNullConstant(T)), T)); 553 } 554 555 // FIXME: We should try to share this code with EmitCXXMemberCall. 556 557 QualType ResultType = DD->getType()->getAs<FunctionType>()->getResultType(); 558 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args), Callee, 559 ReturnValueSlot(), Args, DD); 560} 561 562void 563CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest, 564 const CXXConstructExpr *E) { 565 assert(Dest && "Must have a destination!"); 566 const CXXConstructorDecl *CD = E->getConstructor(); 567 const ConstantArrayType *Array = 568 getContext().getAsConstantArrayType(E->getType()); 569 // For a copy constructor, even if it is trivial, must fall thru so 570 // its argument is code-gen'ed. 571 if (!CD->isCopyConstructor()) { 572 QualType InitType = E->getType(); 573 if (Array) 574 InitType = getContext().getBaseElementType(Array); 575 const CXXRecordDecl *RD = 576 cast<CXXRecordDecl>(InitType->getAs<RecordType>()->getDecl()); 577 if (RD->hasTrivialConstructor()) 578 return; 579 } 580 // Code gen optimization to eliminate copy constructor and return 581 // its first argument instead. 582 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) { 583 const Expr *Arg = E->getArg(0); 584 585 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Arg)) { 586 assert((ICE->getCastKind() == CastExpr::CK_NoOp || 587 ICE->getCastKind() == CastExpr::CK_ConstructorConversion || 588 ICE->getCastKind() == CastExpr::CK_UserDefinedConversion) && 589 "Unknown implicit cast kind in constructor elision"); 590 Arg = ICE->getSubExpr(); 591 } 592 593 if (const CXXFunctionalCastExpr *FCE = dyn_cast<CXXFunctionalCastExpr>(Arg)) 594 Arg = FCE->getSubExpr(); 595 596 if (const CXXBindTemporaryExpr *BindExpr = 597 dyn_cast<CXXBindTemporaryExpr>(Arg)) 598 Arg = BindExpr->getSubExpr(); 599 600 EmitAggExpr(Arg, Dest, false); 601 return; 602 } 603 if (Array) { 604 QualType BaseElementTy = getContext().getBaseElementType(Array); 605 const llvm::Type *BasePtr = ConvertType(BaseElementTy); 606 BasePtr = llvm::PointerType::getUnqual(BasePtr); 607 llvm::Value *BaseAddrPtr = 608 Builder.CreateBitCast(Dest, BasePtr); 609 610 EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr, 611 E->arg_begin(), E->arg_end()); 612 } 613 else 614 // Call the constructor. 615 EmitCXXConstructorCall(CD, Ctor_Complete, Dest, 616 E->arg_begin(), E->arg_end()); 617} 618 619void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) { 620 EmitGlobal(GlobalDecl(D, Ctor_Complete)); 621 EmitGlobal(GlobalDecl(D, Ctor_Base)); 622} 623 624void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D, 625 CXXCtorType Type) { 626 627 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type); 628 629 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn); 630 631 SetFunctionDefinitionAttributes(D, Fn); 632 SetLLVMFunctionAttributesForDefinition(D, Fn); 633} 634 635llvm::Function * 636CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D, 637 CXXCtorType Type) { 638 const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>(); 639 const llvm::FunctionType *FTy = 640 getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type), 641 FPT->isVariadic()); 642 643 const char *Name = getMangledCXXCtorName(D, Type); 644 return cast<llvm::Function>( 645 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type))); 646} 647 648const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D, 649 CXXCtorType Type) { 650 llvm::SmallString<256> Name; 651 getMangleContext().mangleCXXCtor(D, Type, Name); 652 653 Name += '\0'; 654 return UniqueMangledName(Name.begin(), Name.end()); 655} 656 657void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) { 658 if (D->isVirtual()) 659 EmitGlobal(GlobalDecl(D, Dtor_Deleting)); 660 EmitGlobal(GlobalDecl(D, Dtor_Complete)); 661 EmitGlobal(GlobalDecl(D, Dtor_Base)); 662} 663 664void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D, 665 CXXDtorType Type) { 666 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type); 667 668 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn); 669 670 SetFunctionDefinitionAttributes(D, Fn); 671 SetLLVMFunctionAttributesForDefinition(D, Fn); 672} 673 674llvm::Function * 675CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D, 676 CXXDtorType Type) { 677 const llvm::FunctionType *FTy = 678 getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type), false); 679 680 const char *Name = getMangledCXXDtorName(D, Type); 681 return cast<llvm::Function>( 682 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type))); 683} 684 685const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D, 686 CXXDtorType Type) { 687 llvm::SmallString<256> Name; 688 getMangleContext().mangleCXXDtor(D, Type, Name); 689 690 Name += '\0'; 691 return UniqueMangledName(Name.begin(), Name.end()); 692} 693 694llvm::Constant * 695CodeGenFunction::GenerateThunk(llvm::Function *Fn, GlobalDecl GD, 696 bool Extern, 697 const ThunkAdjustment &ThisAdjustment) { 698 return GenerateCovariantThunk(Fn, GD, Extern, 699 CovariantThunkAdjustment(ThisAdjustment, 700 ThunkAdjustment())); 701} 702 703llvm::Value * 704CodeGenFunction::DynamicTypeAdjust(llvm::Value *V, 705 const ThunkAdjustment &Adjustment) { 706 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext); 707 708 const llvm::Type *OrigTy = V->getType(); 709 if (Adjustment.NonVirtual) { 710 // Do the non-virtual adjustment 711 V = Builder.CreateBitCast(V, Int8PtrTy); 712 V = Builder.CreateConstInBoundsGEP1_64(V, Adjustment.NonVirtual); 713 V = Builder.CreateBitCast(V, OrigTy); 714 } 715 716 if (!Adjustment.Virtual) 717 return V; 718 719 assert(Adjustment.Virtual % (LLVMPointerWidth / 8) == 0 && 720 "vtable entry unaligned"); 721 722 // Do the virtual this adjustment 723 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType()); 724 const llvm::Type *PtrDiffPtrTy = PtrDiffTy->getPointerTo(); 725 726 llvm::Value *ThisVal = Builder.CreateBitCast(V, Int8PtrTy); 727 V = Builder.CreateBitCast(V, PtrDiffPtrTy->getPointerTo()); 728 V = Builder.CreateLoad(V, "vtable"); 729 730 llvm::Value *VTablePtr = V; 731 uint64_t VirtualAdjustment = Adjustment.Virtual / (LLVMPointerWidth / 8); 732 V = Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment); 733 V = Builder.CreateLoad(V); 734 V = Builder.CreateGEP(ThisVal, V); 735 736 return Builder.CreateBitCast(V, OrigTy); 737} 738 739llvm::Constant * 740CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn, 741 GlobalDecl GD, bool Extern, 742 const CovariantThunkAdjustment &Adjustment) { 743 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 744 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType(); 745 746 FunctionArgList Args; 747 ImplicitParamDecl *ThisDecl = 748 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 749 MD->getThisType(getContext())); 750 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType())); 751 for (FunctionDecl::param_const_iterator i = MD->param_begin(), 752 e = MD->param_end(); 753 i != e; ++i) { 754 ParmVarDecl *D = *i; 755 Args.push_back(std::make_pair(D, D->getType())); 756 } 757 IdentifierInfo *II 758 = &CGM.getContext().Idents.get("__thunk_named_foo_"); 759 FunctionDecl *FD = FunctionDecl::Create(getContext(), 760 getContext().getTranslationUnitDecl(), 761 SourceLocation(), II, ResultType, 0, 762 Extern 763 ? FunctionDecl::Extern 764 : FunctionDecl::Static, 765 false, true); 766 StartFunction(FD, ResultType, Fn, Args, SourceLocation()); 767 768 // generate body 769 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 770 const llvm::Type *Ty = 771 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), 772 FPT->isVariadic()); 773 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty); 774 775 CallArgList CallArgs; 776 777 bool ShouldAdjustReturnPointer = true; 778 QualType ArgType = MD->getThisType(getContext()); 779 llvm::Value *Arg = Builder.CreateLoad(LocalDeclMap[ThisDecl], "this"); 780 if (!Adjustment.ThisAdjustment.isEmpty()) { 781 // Do the this adjustment. 782 const llvm::Type *OrigTy = Callee->getType(); 783 Arg = DynamicTypeAdjust(Arg, Adjustment.ThisAdjustment); 784 785 if (!Adjustment.ReturnAdjustment.isEmpty()) { 786 const CovariantThunkAdjustment &ReturnAdjustment = 787 CovariantThunkAdjustment(ThunkAdjustment(), 788 Adjustment.ReturnAdjustment); 789 790 Callee = CGM.BuildCovariantThunk(GD, Extern, ReturnAdjustment); 791 792 Callee = Builder.CreateBitCast(Callee, OrigTy); 793 ShouldAdjustReturnPointer = false; 794 } 795 } 796 797 CallArgs.push_back(std::make_pair(RValue::get(Arg), ArgType)); 798 799 for (FunctionDecl::param_const_iterator i = MD->param_begin(), 800 e = MD->param_end(); 801 i != e; ++i) { 802 ParmVarDecl *D = *i; 803 QualType ArgType = D->getType(); 804 805 // llvm::Value *Arg = CGF.GetAddrOfLocalVar(Dst); 806 Expr *Arg = new (getContext()) DeclRefExpr(D, ArgType.getNonReferenceType(), 807 SourceLocation()); 808 CallArgs.push_back(std::make_pair(EmitCallArg(Arg, ArgType), ArgType)); 809 } 810 811 RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), 812 Callee, ReturnValueSlot(), CallArgs, MD); 813 if (ShouldAdjustReturnPointer && !Adjustment.ReturnAdjustment.isEmpty()) { 814 bool CanBeZero = !(ResultType->isReferenceType() 815 // FIXME: attr nonnull can't be zero either 816 /* || ResultType->hasAttr<NonNullAttr>() */ ); 817 // Do the return result adjustment. 818 if (CanBeZero) { 819 llvm::BasicBlock *NonZeroBlock = createBasicBlock(); 820 llvm::BasicBlock *ZeroBlock = createBasicBlock(); 821 llvm::BasicBlock *ContBlock = createBasicBlock(); 822 823 const llvm::Type *Ty = RV.getScalarVal()->getType(); 824 llvm::Value *Zero = llvm::Constant::getNullValue(Ty); 825 Builder.CreateCondBr(Builder.CreateICmpNE(RV.getScalarVal(), Zero), 826 NonZeroBlock, ZeroBlock); 827 EmitBlock(NonZeroBlock); 828 llvm::Value *NZ = 829 DynamicTypeAdjust(RV.getScalarVal(), Adjustment.ReturnAdjustment); 830 EmitBranch(ContBlock); 831 EmitBlock(ZeroBlock); 832 llvm::Value *Z = RV.getScalarVal(); 833 EmitBlock(ContBlock); 834 llvm::PHINode *RVOrZero = Builder.CreatePHI(Ty); 835 RVOrZero->reserveOperandSpace(2); 836 RVOrZero->addIncoming(NZ, NonZeroBlock); 837 RVOrZero->addIncoming(Z, ZeroBlock); 838 RV = RValue::get(RVOrZero); 839 } else 840 RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(), 841 Adjustment.ReturnAdjustment)); 842 } 843 844 if (!ResultType->isVoidType()) 845 EmitReturnOfRValue(RV, ResultType); 846 847 FinishFunction(); 848 return Fn; 849} 850 851llvm::Constant * 852CodeGenModule::GetAddrOfThunk(GlobalDecl GD, 853 const ThunkAdjustment &ThisAdjustment) { 854 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 855 856 // Compute mangled name 857 llvm::SmallString<256> OutName; 858 if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD)) 859 getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(), ThisAdjustment, 860 OutName); 861 else 862 getMangleContext().mangleThunk(MD, ThisAdjustment, OutName); 863 OutName += '\0'; 864 const char* Name = UniqueMangledName(OutName.begin(), OutName.end()); 865 866 // Get function for mangled name 867 const llvm::Type *Ty = getTypes().GetFunctionTypeForVtable(MD); 868 return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl()); 869} 870 871llvm::Constant * 872CodeGenModule::GetAddrOfCovariantThunk(GlobalDecl GD, 873 const CovariantThunkAdjustment &Adjustment) { 874 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 875 876 // Compute mangled name 877 llvm::SmallString<256> OutName; 878 getMangleContext().mangleCovariantThunk(MD, Adjustment, OutName); 879 OutName += '\0'; 880 const char* Name = UniqueMangledName(OutName.begin(), OutName.end()); 881 882 // Get function for mangled name 883 const llvm::Type *Ty = getTypes().GetFunctionTypeForVtable(MD); 884 return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl()); 885} 886 887void CodeGenModule::BuildThunksForVirtual(GlobalDecl GD) { 888 CGVtableInfo::AdjustmentVectorTy *AdjPtr = getVtableInfo().getAdjustments(GD); 889 if (!AdjPtr) 890 return; 891 CGVtableInfo::AdjustmentVectorTy &Adj = *AdjPtr; 892 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 893 for (unsigned i = 0; i < Adj.size(); i++) { 894 GlobalDecl OGD = Adj[i].first; 895 const CXXMethodDecl *OMD = cast<CXXMethodDecl>(OGD.getDecl()); 896 QualType nc_oret = OMD->getType()->getAs<FunctionType>()->getResultType(); 897 CanQualType oret = getContext().getCanonicalType(nc_oret); 898 QualType nc_ret = MD->getType()->getAs<FunctionType>()->getResultType(); 899 CanQualType ret = getContext().getCanonicalType(nc_ret); 900 ThunkAdjustment ReturnAdjustment; 901 if (oret != ret) { 902 QualType qD = nc_ret->getPointeeType(); 903 QualType qB = nc_oret->getPointeeType(); 904 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl()); 905 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl()); 906 ReturnAdjustment = ComputeThunkAdjustment(D, B); 907 } 908 ThunkAdjustment ThisAdjustment = Adj[i].second; 909 bool Extern = !cast<CXXRecordDecl>(OMD->getDeclContext())->isInAnonymousNamespace(); 910 if (!ReturnAdjustment.isEmpty() || !ThisAdjustment.isEmpty()) { 911 CovariantThunkAdjustment CoAdj(ThisAdjustment, ReturnAdjustment); 912 llvm::Constant *FnConst; 913 if (!ReturnAdjustment.isEmpty()) 914 FnConst = GetAddrOfCovariantThunk(GD, CoAdj); 915 else 916 FnConst = GetAddrOfThunk(GD, ThisAdjustment); 917 if (!isa<llvm::Function>(FnConst)) { 918 llvm::Constant *SubExpr = 919 cast<llvm::ConstantExpr>(FnConst)->getOperand(0); 920 llvm::Function *OldFn = cast<llvm::Function>(SubExpr); 921 std::string Name = OldFn->getNameStr(); 922 GlobalDeclMap.erase(UniqueMangledName(Name.data(), 923 Name.data() + Name.size() + 1)); 924 llvm::Constant *NewFnConst; 925 if (!ReturnAdjustment.isEmpty()) 926 NewFnConst = GetAddrOfCovariantThunk(GD, CoAdj); 927 else 928 NewFnConst = GetAddrOfThunk(GD, ThisAdjustment); 929 llvm::Function *NewFn = cast<llvm::Function>(NewFnConst); 930 NewFn->takeName(OldFn); 931 llvm::Constant *NewPtrForOldDecl = 932 llvm::ConstantExpr::getBitCast(NewFn, OldFn->getType()); 933 OldFn->replaceAllUsesWith(NewPtrForOldDecl); 934 OldFn->eraseFromParent(); 935 FnConst = NewFn; 936 } 937 llvm::Function *Fn = cast<llvm::Function>(FnConst); 938 if (Fn->isDeclaration()) { 939 llvm::GlobalVariable::LinkageTypes linktype; 940 linktype = llvm::GlobalValue::WeakAnyLinkage; 941 if (!Extern) 942 linktype = llvm::GlobalValue::InternalLinkage; 943 Fn->setLinkage(linktype); 944 if (!Features.Exceptions && !Features.ObjCNonFragileABI) 945 Fn->addFnAttr(llvm::Attribute::NoUnwind); 946 Fn->setAlignment(2); 947 CodeGenFunction(*this).GenerateCovariantThunk(Fn, GD, Extern, CoAdj); 948 } 949 } 950 } 951} 952 953llvm::Constant * 954CodeGenModule::BuildThunk(GlobalDecl GD, bool Extern, 955 const ThunkAdjustment &ThisAdjustment) { 956 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 957 llvm::SmallString<256> OutName; 958 if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(MD)) { 959 getMangleContext().mangleCXXDtorThunk(D, GD.getDtorType(), ThisAdjustment, 960 OutName); 961 } else 962 getMangleContext().mangleThunk(MD, ThisAdjustment, OutName); 963 964 llvm::GlobalVariable::LinkageTypes linktype; 965 linktype = llvm::GlobalValue::WeakAnyLinkage; 966 if (!Extern) 967 linktype = llvm::GlobalValue::InternalLinkage; 968 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); 969 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 970 const llvm::FunctionType *FTy = 971 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD), 972 FPT->isVariadic()); 973 974 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(), 975 &getModule()); 976 CodeGenFunction(*this).GenerateThunk(Fn, GD, Extern, ThisAdjustment); 977 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); 978 return m; 979} 980 981llvm::Constant * 982CodeGenModule::BuildCovariantThunk(const GlobalDecl &GD, bool Extern, 983 const CovariantThunkAdjustment &Adjustment) { 984 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 985 llvm::SmallString<256> OutName; 986 getMangleContext().mangleCovariantThunk(MD, Adjustment, OutName); 987 llvm::GlobalVariable::LinkageTypes linktype; 988 linktype = llvm::GlobalValue::WeakAnyLinkage; 989 if (!Extern) 990 linktype = llvm::GlobalValue::InternalLinkage; 991 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); 992 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 993 const llvm::FunctionType *FTy = 994 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD), 995 FPT->isVariadic()); 996 997 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(), 998 &getModule()); 999 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, Adjustment); 1000 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); 1001 return m; 1002} 1003 1004llvm::Value * 1005CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This, 1006 const CXXRecordDecl *ClassDecl, 1007 const CXXRecordDecl *BaseClassDecl) { 1008 const llvm::Type *Int8PtrTy = 1009 llvm::Type::getInt8Ty(VMContext)->getPointerTo(); 1010 1011 llvm::Value *VTablePtr = Builder.CreateBitCast(This, 1012 Int8PtrTy->getPointerTo()); 1013 VTablePtr = Builder.CreateLoad(VTablePtr, "vtable"); 1014 1015 int64_t VBaseOffsetIndex = 1016 CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl); 1017 1018 llvm::Value *VBaseOffsetPtr = 1019 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr"); 1020 const llvm::Type *PtrDiffTy = 1021 ConvertType(getContext().getPointerDiffType()); 1022 1023 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr, 1024 PtrDiffTy->getPointerTo()); 1025 1026 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset"); 1027 1028 return VBaseOffset; 1029} 1030 1031static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, uint64_t VtableIndex, 1032 llvm::Value *This, const llvm::Type *Ty) { 1033 Ty = Ty->getPointerTo()->getPointerTo()->getPointerTo(); 1034 1035 llvm::Value *Vtable = CGF.Builder.CreateBitCast(This, Ty); 1036 Vtable = CGF.Builder.CreateLoad(Vtable); 1037 1038 llvm::Value *VFuncPtr = 1039 CGF.Builder.CreateConstInBoundsGEP1_64(Vtable, VtableIndex, "vfn"); 1040 return CGF.Builder.CreateLoad(VFuncPtr); 1041} 1042 1043llvm::Value * 1044CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This, 1045 const llvm::Type *Ty) { 1046 MD = MD->getCanonicalDecl(); 1047 uint64_t VtableIndex = CGM.getVtableInfo().getMethodVtableIndex(MD); 1048 1049 return ::BuildVirtualCall(*this, VtableIndex, This, Ty); 1050} 1051 1052llvm::Value * 1053CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type, 1054 llvm::Value *&This, const llvm::Type *Ty) { 1055 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl()); 1056 uint64_t VtableIndex = 1057 CGM.getVtableInfo().getMethodVtableIndex(GlobalDecl(DD, Type)); 1058 1059 return ::BuildVirtualCall(*this, VtableIndex, This, Ty); 1060} 1061 1062void CodeGenFunction::InitializeVtablePtrs(const CXXRecordDecl *ClassDecl) { 1063 if (!ClassDecl->isDynamicClass()) 1064 return; 1065 1066 llvm::Constant *Vtable = CGM.getVtableInfo().getVtable(ClassDecl); 1067 CodeGenModule::AddrSubMap_t& AddressPoints = 1068 *(*CGM.AddressPoints[ClassDecl])[ClassDecl]; 1069 llvm::Value *ThisPtr = LoadCXXThis(); 1070 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(ClassDecl); 1071 1072 // Store address points for virtual bases 1073 for (CXXRecordDecl::base_class_const_iterator I = 1074 ClassDecl->vbases_begin(), E = ClassDecl->vbases_end(); I != E; ++I) { 1075 const CXXBaseSpecifier &Base = *I; 1076 CXXRecordDecl *BaseClassDecl 1077 = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); 1078 uint64_t Offset = Layout.getVBaseClassOffset(BaseClassDecl); 1079 InitializeVtablePtrsRecursive(BaseClassDecl, Vtable, AddressPoints, 1080 ThisPtr, Offset); 1081 } 1082 1083 // Store address points for non-virtual bases and current class 1084 InitializeVtablePtrsRecursive(ClassDecl, Vtable, AddressPoints, ThisPtr, 0); 1085} 1086 1087void CodeGenFunction::InitializeVtablePtrsRecursive( 1088 const CXXRecordDecl *ClassDecl, 1089 llvm::Constant *Vtable, 1090 CodeGenModule::AddrSubMap_t& AddressPoints, 1091 llvm::Value *ThisPtr, 1092 uint64_t Offset) { 1093 if (!ClassDecl->isDynamicClass()) 1094 return; 1095 1096 // Store address points for non-virtual bases 1097 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(ClassDecl); 1098 for (CXXRecordDecl::base_class_const_iterator I = 1099 ClassDecl->bases_begin(), E = ClassDecl->bases_end(); I != E; ++I) { 1100 const CXXBaseSpecifier &Base = *I; 1101 if (Base.isVirtual()) 1102 continue; 1103 CXXRecordDecl *BaseClassDecl 1104 = cast<CXXRecordDecl>(Base.getType()->getAs<RecordType>()->getDecl()); 1105 uint64_t NewOffset = Offset + Layout.getBaseClassOffset(BaseClassDecl); 1106 InitializeVtablePtrsRecursive(BaseClassDecl, Vtable, AddressPoints, 1107 ThisPtr, NewOffset); 1108 } 1109 1110 // Compute the address point 1111 uint64_t AddressPoint = AddressPoints[std::make_pair(ClassDecl, Offset)]; 1112 llvm::Value *VtableAddressPoint = 1113 Builder.CreateConstInBoundsGEP2_64(Vtable, 0, AddressPoint); 1114 1115 // Compute the address to store the address point 1116 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGM.getLLVMContext()); 1117 llvm::Value *VtableField = Builder.CreateBitCast(ThisPtr, Int8PtrTy); 1118 VtableField = Builder.CreateConstInBoundsGEP1_64(VtableField, Offset/8); 1119 const llvm::Type *AddressPointPtrTy = 1120 VtableAddressPoint->getType()->getPointerTo(); 1121 VtableField = Builder.CreateBitCast(ThisPtr, AddressPointPtrTy); 1122 1123 // Store address point 1124 Builder.CreateStore(VtableAddressPoint, VtableField); 1125} 1126 1127