CGCXX.cpp revision 79d5768e7bc30e847d18bb0e41244d25faff934b
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 29void 30CodeGenFunction::EmitCXXGlobalDtorRegistration(const CXXDestructorDecl *Dtor, 31 llvm::Constant *DeclPtr) { 32 const llvm::Type *Int8PtrTy = 33 llvm::Type::getInt8Ty(VMContext)->getPointerTo(); 34 35 std::vector<const llvm::Type *> Params; 36 Params.push_back(Int8PtrTy); 37 38 // Get the destructor function type 39 const llvm::Type *DtorFnTy = 40 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false); 41 DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy); 42 43 Params.clear(); 44 Params.push_back(DtorFnTy); 45 Params.push_back(Int8PtrTy); 46 Params.push_back(Int8PtrTy); 47 48 // Get the __cxa_atexit function type 49 // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d ); 50 const llvm::FunctionType *AtExitFnTy = 51 llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false); 52 53 llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy, 54 "__cxa_atexit"); 55 56 llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy, 57 "__dso_handle"); 58 59 llvm::Constant *DtorFn = CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete); 60 61 llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy), 62 llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy), 63 llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) }; 64 Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args)); 65} 66 67void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D, 68 llvm::Constant *DeclPtr) { 69 assert(D.hasGlobalStorage() && 70 "VarDecl must have global storage!"); 71 72 const Expr *Init = D.getInit(); 73 QualType T = D.getType(); 74 bool isVolatile = getContext().getCanonicalType(T).isVolatileQualified(); 75 76 if (T->isReferenceType()) { 77 ErrorUnsupported(Init, "global variable that binds to a reference"); 78 } else if (!hasAggregateLLVMType(T)) { 79 llvm::Value *V = EmitScalarExpr(Init); 80 EmitStoreOfScalar(V, DeclPtr, isVolatile, T); 81 } else if (T->isAnyComplexType()) { 82 EmitComplexExprIntoAddr(Init, DeclPtr, isVolatile); 83 } else { 84 EmitAggExpr(Init, DeclPtr, isVolatile); 85 86 if (const RecordType *RT = T->getAs<RecordType>()) { 87 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 88 if (!RD->hasTrivialDestructor()) 89 EmitCXXGlobalDtorRegistration(RD->getDestructor(getContext()), DeclPtr); 90 } 91 } 92} 93 94void 95CodeGenModule::EmitCXXGlobalInitFunc() { 96 if (CXXGlobalInits.empty()) 97 return; 98 99 const llvm::FunctionType *FTy 100 = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), 101 false); 102 103 // Create our global initialization function. 104 // FIXME: Should this be tweakable by targets? 105 llvm::Function *Fn = 106 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, 107 "__cxx_global_initialization", &TheModule); 108 109 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, 110 &CXXGlobalInits[0], 111 CXXGlobalInits.size()); 112 AddGlobalCtor(Fn); 113} 114 115void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn, 116 const VarDecl **Decls, 117 unsigned NumDecls) { 118 StartFunction(GlobalDecl(), getContext().VoidTy, Fn, FunctionArgList(), 119 SourceLocation()); 120 121 for (unsigned i = 0; i != NumDecls; ++i) { 122 const VarDecl *D = Decls[i]; 123 124 llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D); 125 EmitCXXGlobalVarDeclInit(*D, DeclPtr); 126 } 127 FinishFunction(); 128} 129 130void 131CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D, 132 llvm::GlobalVariable *GV) { 133 // FIXME: This should use __cxa_guard_{acquire,release}? 134 135 assert(!getContext().getLangOptions().ThreadsafeStatics && 136 "thread safe statics are currently not supported!"); 137 138 llvm::SmallString<256> GuardVName; 139 llvm::raw_svector_ostream GuardVOut(GuardVName); 140 mangleGuardVariable(CGM.getMangleContext(), &D, GuardVOut); 141 142 // Create the guard variable. 143 llvm::GlobalValue *GuardV = 144 new llvm::GlobalVariable(CGM.getModule(), llvm::Type::getInt64Ty(VMContext), 145 false, GV->getLinkage(), 146 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)), 147 GuardVName.str()); 148 149 // Load the first byte of the guard variable. 150 const llvm::Type *PtrTy 151 = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); 152 llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy), 153 "tmp"); 154 155 // Compare it against 0. 156 llvm::Value *nullValue 157 = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)); 158 llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool"); 159 160 llvm::BasicBlock *InitBlock = createBasicBlock("init"); 161 llvm::BasicBlock *EndBlock = createBasicBlock("init.end"); 162 163 // If the guard variable is 0, jump to the initializer code. 164 Builder.CreateCondBr(ICmp, InitBlock, EndBlock); 165 166 EmitBlock(InitBlock); 167 168 EmitCXXGlobalVarDeclInit(D, GV); 169 170 Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 171 1), 172 Builder.CreateBitCast(GuardV, PtrTy)); 173 174 EmitBlock(EndBlock); 175} 176 177RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD, 178 llvm::Value *Callee, 179 llvm::Value *This, 180 CallExpr::const_arg_iterator ArgBeg, 181 CallExpr::const_arg_iterator ArgEnd) { 182 assert(MD->isInstance() && 183 "Trying to emit a member call expr on a static method!"); 184 185 // A call to a trivial destructor requires no code generation. 186 if (const CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(MD)) 187 if (Destructor->isTrivial()) 188 return RValue::get(0); 189 190 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 191 192 CallArgList Args; 193 194 // Push the this ptr. 195 Args.push_back(std::make_pair(RValue::get(This), 196 MD->getThisType(getContext()))); 197 198 // And the rest of the call args 199 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd); 200 201 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType(); 202 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args), 203 Callee, Args, MD); 204} 205 206/// canDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given 207/// expr can be devirtualized. 208static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) { 209 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Base)) { 210 if (const VarDecl *VD = dyn_cast<VarDecl>(DRE->getDecl())) { 211 // This is a record decl. We know the type and can devirtualize it. 212 return VD->getType()->isRecordType(); 213 } 214 215 return false; 216 } 217 218 // We can always devirtualize calls on temporary object expressions. 219 if (isa<CXXTemporaryObjectExpr>(Base)) 220 return true; 221 222 // And calls on bound temporaries. 223 if (isa<CXXBindTemporaryExpr>(Base)) 224 return true; 225 226 // Check if this is a call expr that returns a record type. 227 if (const CallExpr *CE = dyn_cast<CallExpr>(Base)) 228 return CE->getCallReturnType()->isRecordType(); 229 230 // We can't devirtualize the call. 231 return false; 232} 233 234RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) { 235 if (isa<BinaryOperator>(CE->getCallee())) 236 return EmitCXXMemberPointerCallExpr(CE); 237 238 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee()); 239 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl()); 240 241 if (MD->isStatic()) { 242 // The method is static, emit it as we would a regular call. 243 llvm::Value *Callee = CGM.GetAddrOfFunction(MD); 244 return EmitCall(Callee, getContext().getPointerType(MD->getType()), 245 CE->arg_begin(), CE->arg_end(), 0); 246 247 } 248 249 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 250 251 const llvm::Type *Ty = 252 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), 253 FPT->isVariadic()); 254 llvm::Value *This; 255 256 if (ME->isArrow()) 257 This = EmitScalarExpr(ME->getBase()); 258 else { 259 LValue BaseLV = EmitLValue(ME->getBase()); 260 This = BaseLV.getAddress(); 261 } 262 263 // C++ [class.virtual]p12: 264 // Explicit qualification with the scope operator (5.1) suppresses the 265 // virtual call mechanism. 266 // 267 // We also don't emit a virtual call if the base expression has a record type 268 // because then we know what the type is. 269 llvm::Value *Callee; 270 if (MD->isVirtual() && !ME->hasQualifier() && 271 !canDevirtualizeMemberFunctionCalls(ME->getBase())) 272 Callee = BuildVirtualCall(MD, This, Ty); 273 else if (const CXXDestructorDecl *Destructor 274 = dyn_cast<CXXDestructorDecl>(MD)) 275 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty); 276 else 277 Callee = CGM.GetAddrOfFunction(MD, Ty); 278 279 return EmitCXXMemberCall(MD, Callee, This, 280 CE->arg_begin(), CE->arg_end()); 281} 282 283RValue 284CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E) { 285 const BinaryOperator *BO = cast<BinaryOperator>(E->getCallee()); 286 const Expr *BaseExpr = BO->getLHS(); 287 const Expr *MemFnExpr = BO->getRHS(); 288 289 const MemberPointerType *MPT = 290 MemFnExpr->getType()->getAs<MemberPointerType>(); 291 const FunctionProtoType *FPT = 292 MPT->getPointeeType()->getAs<FunctionProtoType>(); 293 const CXXRecordDecl *RD = 294 cast<CXXRecordDecl>(cast<RecordType>(MPT->getClass())->getDecl()); 295 296 const llvm::FunctionType *FTy = 297 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT), 298 FPT->isVariadic()); 299 300 const llvm::Type *Int8PtrTy = 301 llvm::Type::getInt8Ty(VMContext)->getPointerTo(); 302 303 // Get the member function pointer. 304 llvm::Value *MemFnPtr = 305 CreateTempAlloca(ConvertType(MemFnExpr->getType()), "mem.fn"); 306 EmitAggExpr(MemFnExpr, MemFnPtr, /*VolatileDest=*/false); 307 308 // Emit the 'this' pointer. 309 llvm::Value *This; 310 311 if (BO->getOpcode() == BinaryOperator::PtrMemI) 312 This = EmitScalarExpr(BaseExpr); 313 else 314 This = EmitLValue(BaseExpr).getAddress(); 315 316 // Adjust it. 317 llvm::Value *Adj = Builder.CreateStructGEP(MemFnPtr, 1); 318 Adj = Builder.CreateLoad(Adj, "mem.fn.adj"); 319 320 llvm::Value *Ptr = Builder.CreateBitCast(This, Int8PtrTy, "ptr"); 321 Ptr = Builder.CreateGEP(Ptr, Adj, "adj"); 322 323 This = Builder.CreateBitCast(Ptr, This->getType(), "this"); 324 325 llvm::Value *FnPtr = Builder.CreateStructGEP(MemFnPtr, 0, "mem.fn.ptr"); 326 327 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType()); 328 329 llvm::Value *FnAsInt = Builder.CreateLoad(FnPtr, "fn"); 330 331 // If the LSB in the function pointer is 1, the function pointer points to 332 // a virtual function. 333 llvm::Value *IsVirtual 334 = Builder.CreateAnd(FnAsInt, llvm::ConstantInt::get(PtrDiffTy, 1), 335 "and"); 336 337 IsVirtual = Builder.CreateTrunc(IsVirtual, 338 llvm::Type::getInt1Ty(VMContext)); 339 340 llvm::BasicBlock *FnVirtual = createBasicBlock("fn.virtual"); 341 llvm::BasicBlock *FnNonVirtual = createBasicBlock("fn.nonvirtual"); 342 llvm::BasicBlock *FnEnd = createBasicBlock("fn.end"); 343 344 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual); 345 EmitBlock(FnVirtual); 346 347 const llvm::Type *VTableTy = 348 FTy->getPointerTo()->getPointerTo()->getPointerTo(); 349 350 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy); 351 VTable = Builder.CreateLoad(VTable); 352 353 VTable = Builder.CreateGEP(VTable, FnAsInt, "fn"); 354 355 // Since the function pointer is 1 plus the virtual table offset, we 356 // subtract 1 by using a GEP. 357 VTable = Builder.CreateConstGEP1_64(VTable, (uint64_t)-1); 358 359 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "virtualfn"); 360 361 EmitBranch(FnEnd); 362 EmitBlock(FnNonVirtual); 363 364 // If the function is not virtual, just load the pointer. 365 llvm::Value *NonVirtualFn = Builder.CreateLoad(FnPtr, "fn"); 366 NonVirtualFn = Builder.CreateIntToPtr(NonVirtualFn, FTy->getPointerTo()); 367 368 EmitBlock(FnEnd); 369 370 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo()); 371 Callee->reserveOperandSpace(2); 372 Callee->addIncoming(VirtualFn, FnVirtual); 373 Callee->addIncoming(NonVirtualFn, FnNonVirtual); 374 375 CallArgList Args; 376 377 QualType ThisType = 378 getContext().getPointerType(getContext().getTagDeclType(RD)); 379 380 // Push the this ptr. 381 Args.push_back(std::make_pair(RValue::get(This), ThisType)); 382 383 // And the rest of the call args 384 EmitCallArgs(Args, FPT, E->arg_begin(), E->arg_end()); 385 QualType ResultType = BO->getType()->getAs<FunctionType>()->getResultType(); 386 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args), 387 Callee, Args, 0); 388} 389 390RValue 391CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, 392 const CXXMethodDecl *MD) { 393 assert(MD->isInstance() && 394 "Trying to emit a member call expr on a static method!"); 395 396 if (MD->isCopyAssignment()) { 397 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext()); 398 if (ClassDecl->hasTrivialCopyAssignment()) { 399 assert(!ClassDecl->hasUserDeclaredCopyAssignment() && 400 "EmitCXXOperatorMemberCallExpr - user declared copy assignment"); 401 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress(); 402 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress(); 403 QualType Ty = E->getType(); 404 EmitAggregateCopy(This, Src, Ty); 405 return RValue::get(This); 406 } 407 } 408 409 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 410 const llvm::Type *Ty = 411 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), 412 FPT->isVariadic()); 413 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, Ty); 414 415 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress(); 416 417 return EmitCXXMemberCall(MD, Callee, This, 418 E->arg_begin() + 1, E->arg_end()); 419} 420 421llvm::Value *CodeGenFunction::LoadCXXThis() { 422 assert(isa<CXXMethodDecl>(CurFuncDecl) && 423 "Must be in a C++ member function decl to load 'this'"); 424 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() && 425 "Must be in a C++ member function decl to load 'this'"); 426 427 // FIXME: What if we're inside a block? 428 // ans: See how CodeGenFunction::LoadObjCSelf() uses 429 // CodeGenFunction::BlockForwardSelf() for how to do this. 430 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this"); 431} 432 433/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested) 434/// for-loop to call the default constructor on individual members of the 435/// array. 436/// 'D' is the default constructor for elements of the array, 'ArrayTy' is the 437/// array type and 'ArrayPtr' points to the beginning fo the array. 438/// It is assumed that all relevant checks have been made by the caller. 439void 440CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, 441 const ConstantArrayType *ArrayTy, 442 llvm::Value *ArrayPtr) { 443 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 444 llvm::Value * NumElements = 445 llvm::ConstantInt::get(SizeTy, 446 getContext().getConstantArrayElementCount(ArrayTy)); 447 448 EmitCXXAggrConstructorCall(D, NumElements, ArrayPtr); 449} 450 451void 452CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, 453 llvm::Value *NumElements, 454 llvm::Value *ArrayPtr) { 455 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType()); 456 457 // Create a temporary for the loop index and initialize it with 0. 458 llvm::Value *IndexPtr = CreateTempAlloca(SizeTy, "loop.index"); 459 llvm::Value *Zero = llvm::Constant::getNullValue(SizeTy); 460 Builder.CreateStore(Zero, IndexPtr, false); 461 462 // Start the loop with a block that tests the condition. 463 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); 464 llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); 465 466 EmitBlock(CondBlock); 467 468 llvm::BasicBlock *ForBody = createBasicBlock("for.body"); 469 470 // Generate: if (loop-index < number-of-elements fall to the loop body, 471 // otherwise, go to the block after the for-loop. 472 llvm::Value *Counter = Builder.CreateLoad(IndexPtr); 473 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElements, "isless"); 474 // If the condition is true, execute the body. 475 Builder.CreateCondBr(IsLess, ForBody, AfterFor); 476 477 EmitBlock(ForBody); 478 479 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); 480 // Inside the loop body, emit the constructor call on the array element. 481 Counter = Builder.CreateLoad(IndexPtr); 482 llvm::Value *Address = Builder.CreateInBoundsGEP(ArrayPtr, Counter, 483 "arrayidx"); 484 EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0); 485 486 EmitBlock(ContinueBlock); 487 488 // Emit the increment of the loop counter. 489 llvm::Value *NextVal = llvm::ConstantInt::get(SizeTy, 1); 490 Counter = Builder.CreateLoad(IndexPtr); 491 NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); 492 Builder.CreateStore(NextVal, IndexPtr, false); 493 494 // Finally, branch back up to the condition for the next iteration. 495 EmitBranch(CondBlock); 496 497 // Emit the fall-through block. 498 EmitBlock(AfterFor, true); 499} 500 501/// EmitCXXAggrDestructorCall - calls the default destructor on array 502/// elements in reverse order of construction. 503void 504CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, 505 const ArrayType *Array, 506 llvm::Value *This) { 507 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); 508 assert(CA && "Do we support VLA for destruction ?"); 509 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), 510 1); 511 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA); 512 // Create a temporary for the loop index and initialize it with count of 513 // array elements. 514 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext), 515 "loop.index"); 516 // Index = ElementCount; 517 llvm::Value* UpperCount = 518 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount); 519 Builder.CreateStore(UpperCount, IndexPtr, false); 520 521 // Start the loop with a block that tests the condition. 522 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); 523 llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); 524 525 EmitBlock(CondBlock); 526 527 llvm::BasicBlock *ForBody = createBasicBlock("for.body"); 528 529 // Generate: if (loop-index != 0 fall to the loop body, 530 // otherwise, go to the block after the for-loop. 531 llvm::Value* zeroConstant = 532 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)); 533 llvm::Value *Counter = Builder.CreateLoad(IndexPtr); 534 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant, 535 "isne"); 536 // If the condition is true, execute the body. 537 Builder.CreateCondBr(IsNE, ForBody, AfterFor); 538 539 EmitBlock(ForBody); 540 541 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); 542 // Inside the loop body, emit the constructor call on the array element. 543 Counter = Builder.CreateLoad(IndexPtr); 544 Counter = Builder.CreateSub(Counter, One); 545 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx"); 546 EmitCXXDestructorCall(D, Dtor_Complete, Address); 547 548 EmitBlock(ContinueBlock); 549 550 // Emit the decrement of the loop counter. 551 Counter = Builder.CreateLoad(IndexPtr); 552 Counter = Builder.CreateSub(Counter, One, "dec"); 553 Builder.CreateStore(Counter, IndexPtr, false); 554 555 // Finally, branch back up to the condition for the next iteration. 556 EmitBranch(CondBlock); 557 558 // Emit the fall-through block. 559 EmitBlock(AfterFor, true); 560} 561 562void 563CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, 564 CXXCtorType Type, 565 llvm::Value *This, 566 CallExpr::const_arg_iterator ArgBeg, 567 CallExpr::const_arg_iterator ArgEnd) { 568 if (D->isCopyConstructor(getContext())) { 569 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext()); 570 if (ClassDecl->hasTrivialCopyConstructor()) { 571 assert(!ClassDecl->hasUserDeclaredCopyConstructor() && 572 "EmitCXXConstructorCall - user declared copy constructor"); 573 const Expr *E = (*ArgBeg); 574 QualType Ty = E->getType(); 575 llvm::Value *Src = EmitLValue(E).getAddress(); 576 EmitAggregateCopy(This, Src, Ty); 577 return; 578 } 579 } 580 581 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type); 582 583 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd); 584} 585 586void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D, 587 CXXDtorType Type, 588 llvm::Value *This) { 589 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type); 590 591 EmitCXXMemberCall(D, Callee, This, 0, 0); 592} 593 594void 595CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest, 596 const CXXConstructExpr *E) { 597 assert(Dest && "Must have a destination!"); 598 const CXXConstructorDecl *CD = E->getConstructor(); 599 const ConstantArrayType *Array = 600 getContext().getAsConstantArrayType(E->getType()); 601 // For a copy constructor, even if it is trivial, must fall thru so 602 // its argument is code-gen'ed. 603 if (!CD->isCopyConstructor(getContext())) { 604 QualType InitType = E->getType(); 605 if (Array) 606 InitType = getContext().getBaseElementType(Array); 607 const CXXRecordDecl *RD = 608 cast<CXXRecordDecl>(InitType->getAs<RecordType>()->getDecl()); 609 if (RD->hasTrivialConstructor()) 610 return; 611 } 612 // Code gen optimization to eliminate copy constructor and return 613 // its first argument instead. 614 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) { 615 CXXConstructExpr::const_arg_iterator i = E->arg_begin(); 616 EmitAggExpr((*i), Dest, false); 617 return; 618 } 619 if (Array) { 620 QualType BaseElementTy = getContext().getBaseElementType(Array); 621 const llvm::Type *BasePtr = ConvertType(BaseElementTy); 622 BasePtr = llvm::PointerType::getUnqual(BasePtr); 623 llvm::Value *BaseAddrPtr = 624 Builder.CreateBitCast(Dest, BasePtr); 625 EmitCXXAggrConstructorCall(CD, Array, BaseAddrPtr); 626 } 627 else 628 // Call the constructor. 629 EmitCXXConstructorCall(CD, Ctor_Complete, Dest, 630 E->arg_begin(), E->arg_end()); 631} 632 633void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) { 634 EmitGlobal(GlobalDecl(D, Ctor_Complete)); 635 EmitGlobal(GlobalDecl(D, Ctor_Base)); 636} 637 638void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D, 639 CXXCtorType Type) { 640 641 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type); 642 643 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn); 644 645 SetFunctionDefinitionAttributes(D, Fn); 646 SetLLVMFunctionAttributesForDefinition(D, Fn); 647} 648 649llvm::Function * 650CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D, 651 CXXCtorType Type) { 652 const llvm::FunctionType *FTy = 653 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false); 654 655 const char *Name = getMangledCXXCtorName(D, Type); 656 return cast<llvm::Function>( 657 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type))); 658} 659 660const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D, 661 CXXCtorType Type) { 662 llvm::SmallString<256> Name; 663 llvm::raw_svector_ostream Out(Name); 664 mangleCXXCtor(getMangleContext(), D, Type, Out); 665 666 Name += '\0'; 667 return UniqueMangledName(Name.begin(), Name.end()); 668} 669 670void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) { 671 EmitCXXDestructor(D, Dtor_Complete); 672 EmitCXXDestructor(D, Dtor_Base); 673} 674 675void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D, 676 CXXDtorType Type) { 677 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type); 678 679 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn); 680 681 SetFunctionDefinitionAttributes(D, Fn); 682 SetLLVMFunctionAttributesForDefinition(D, Fn); 683} 684 685llvm::Function * 686CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D, 687 CXXDtorType Type) { 688 const llvm::FunctionType *FTy = 689 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false); 690 691 const char *Name = getMangledCXXDtorName(D, Type); 692 return cast<llvm::Function>( 693 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type))); 694} 695 696const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D, 697 CXXDtorType Type) { 698 llvm::SmallString<256> Name; 699 llvm::raw_svector_ostream Out(Name); 700 mangleCXXDtor(getMangleContext(), D, Type, Out); 701 702 Name += '\0'; 703 return UniqueMangledName(Name.begin(), Name.end()); 704} 705 706llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn, 707 const CXXMethodDecl *MD, 708 bool Extern, int64_t nv, 709 int64_t v) { 710 return GenerateCovariantThunk(Fn, MD, Extern, nv, v, 0, 0); 711} 712 713llvm::Value *CodeGenFunction::DynamicTypeAdjust(llvm::Value *V, int64_t nv, 714 int64_t v) { 715 llvm::Type *Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 716 0); 717 const llvm::Type *OrigTy = V->getType(); 718 if (nv) { 719 // Do the non-virtual adjustment 720 V = Builder.CreateBitCast(V, Ptr8Ty); 721 V = Builder.CreateConstInBoundsGEP1_64(V, nv); 722 V = Builder.CreateBitCast(V, OrigTy); 723 } 724 if (v) { 725 // Do the virtual this adjustment 726 const llvm::Type *PtrDiffTy = 727 ConvertType(getContext().getPointerDiffType()); 728 llvm::Type *PtrPtr8Ty, *PtrPtrDiffTy; 729 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0); 730 PtrPtrDiffTy = llvm::PointerType::get(PtrDiffTy, 0); 731 llvm::Value *ThisVal = Builder.CreateBitCast(V, Ptr8Ty); 732 V = Builder.CreateBitCast(V, PtrPtrDiffTy->getPointerTo()); 733 V = Builder.CreateLoad(V, "vtable"); 734 llvm::Value *VTablePtr = V; 735 assert(v % (LLVMPointerWidth/8) == 0 && "vtable entry unaligned"); 736 v /= LLVMPointerWidth/8; 737 V = Builder.CreateConstInBoundsGEP1_64(VTablePtr, v); 738 V = Builder.CreateLoad(V); 739 V = Builder.CreateGEP(ThisVal, V); 740 V = Builder.CreateBitCast(V, OrigTy); 741 } 742 return V; 743} 744 745llvm::Constant *CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn, 746 const CXXMethodDecl *MD, 747 bool Extern, 748 int64_t nv_t, 749 int64_t v_t, 750 int64_t nv_r, 751 int64_t v_r) { 752 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType(); 753 754 FunctionArgList Args; 755 ImplicitParamDecl *ThisDecl = 756 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 757 MD->getThisType(getContext())); 758 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType())); 759 for (FunctionDecl::param_const_iterator i = MD->param_begin(), 760 e = MD->param_end(); 761 i != e; ++i) { 762 ParmVarDecl *D = *i; 763 Args.push_back(std::make_pair(D, D->getType())); 764 } 765 IdentifierInfo *II 766 = &CGM.getContext().Idents.get("__thunk_named_foo_"); 767 FunctionDecl *FD = FunctionDecl::Create(getContext(), 768 getContext().getTranslationUnitDecl(), 769 SourceLocation(), II, ResultType, 0, 770 Extern 771 ? FunctionDecl::Extern 772 : FunctionDecl::Static, 773 false, true); 774 StartFunction(FD, ResultType, Fn, Args, SourceLocation()); 775 776 // generate body 777 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 778 const llvm::Type *Ty = 779 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), 780 FPT->isVariadic()); 781 llvm::Value *Callee = CGM.GetAddrOfFunction(MD, Ty); 782 CallArgList CallArgs; 783 784 QualType ArgType = MD->getThisType(getContext()); 785 llvm::Value *Arg = Builder.CreateLoad(LocalDeclMap[ThisDecl], "this"); 786 if (nv_t || v_t) { 787 // Do the this adjustment. 788 const llvm::Type *OrigTy = Callee->getType(); 789 Arg = DynamicTypeAdjust(Arg, nv_t, v_t); 790 if (nv_r || v_r) { 791 Callee = CGM.BuildCovariantThunk(MD, Extern, 0, 0, nv_r, v_r); 792 Callee = Builder.CreateBitCast(Callee, OrigTy); 793 nv_r = v_r = 0; 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, SourceLocation()); 807 CallArgs.push_back(std::make_pair(EmitCallArg(Arg, ArgType), ArgType)); 808 } 809 810 RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), 811 Callee, CallArgs, MD); 812 if (nv_r || v_r) { 813 // Do the return result adjustment. 814 RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r)); 815 } 816 817 if (!ResultType->isVoidType()) 818 EmitReturnOfRValue(RV, ResultType); 819 820 FinishFunction(); 821 return Fn; 822} 823 824llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern, 825 int64_t nv, int64_t v) { 826 llvm::SmallString<256> OutName; 827 llvm::raw_svector_ostream Out(OutName); 828 mangleThunk(getMangleContext(), MD, nv, v, Out); 829 llvm::GlobalVariable::LinkageTypes linktype; 830 linktype = llvm::GlobalValue::WeakAnyLinkage; 831 if (!Extern) 832 linktype = llvm::GlobalValue::InternalLinkage; 833 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); 834 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 835 const llvm::FunctionType *FTy = 836 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD), 837 FPT->isVariadic()); 838 839 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(), 840 &getModule()); 841 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v); 842 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); 843 return m; 844} 845 846llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD, 847 bool Extern, int64_t nv_t, 848 int64_t v_t, int64_t nv_r, 849 int64_t v_r) { 850 llvm::SmallString<256> OutName; 851 llvm::raw_svector_ostream Out(OutName); 852 mangleCovariantThunk(getMangleContext(), MD, nv_t, v_t, nv_r, v_r, Out); 853 llvm::GlobalVariable::LinkageTypes linktype; 854 linktype = llvm::GlobalValue::WeakAnyLinkage; 855 if (!Extern) 856 linktype = llvm::GlobalValue::InternalLinkage; 857 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); 858 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 859 const llvm::FunctionType *FTy = 860 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD), 861 FPT->isVariadic()); 862 863 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(), 864 &getModule()); 865 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r, 866 v_r); 867 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); 868 return m; 869} 870 871llvm::Value * 872CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This, 873 const CXXRecordDecl *ClassDecl, 874 const CXXRecordDecl *BaseClassDecl) { 875 const llvm::Type *Int8PtrTy = 876 llvm::Type::getInt8Ty(VMContext)->getPointerTo(); 877 878 llvm::Value *VTablePtr = Builder.CreateBitCast(This, 879 Int8PtrTy->getPointerTo()); 880 VTablePtr = Builder.CreateLoad(VTablePtr, "vtable"); 881 882 int64_t VBaseOffsetIndex = 883 CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl); 884 885 llvm::Value *VBaseOffsetPtr = 886 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr"); 887 const llvm::Type *PtrDiffTy = 888 ConvertType(getContext().getPointerDiffType()); 889 890 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr, 891 PtrDiffTy->getPointerTo()); 892 893 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset"); 894 895 return VBaseOffset; 896} 897 898llvm::Value * 899CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This, 900 const llvm::Type *Ty) { 901 int64_t Index = CGM.getVtableInfo().getMethodVtableIndex(MD); 902 903 Ty = llvm::PointerType::get(Ty, 0); 904 Ty = llvm::PointerType::get(Ty, 0); 905 Ty = llvm::PointerType::get(Ty, 0); 906 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty); 907 vtbl = Builder.CreateLoad(vtbl); 908 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl, 909 Index, "vfn"); 910 vfn = Builder.CreateLoad(vfn); 911 return vfn; 912} 913 914/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class 915/// array of objects from SrcValue to DestValue. Copying can be either a bitwise 916/// copy or via a copy constructor call. 917// FIXME. Consolidate this with EmitCXXAggrConstructorCall. 918void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest, 919 llvm::Value *Src, 920 const ArrayType *Array, 921 const CXXRecordDecl *BaseClassDecl, 922 QualType Ty) { 923 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); 924 assert(CA && "VLA cannot be copied over"); 925 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor(); 926 927 // Create a temporary for the loop index and initialize it with 0. 928 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext), 929 "loop.index"); 930 llvm::Value* zeroConstant = 931 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)); 932 Builder.CreateStore(zeroConstant, IndexPtr, false); 933 // Start the loop with a block that tests the condition. 934 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); 935 llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); 936 937 EmitBlock(CondBlock); 938 939 llvm::BasicBlock *ForBody = createBasicBlock("for.body"); 940 // Generate: if (loop-index < number-of-elements fall to the loop body, 941 // otherwise, go to the block after the for-loop. 942 uint64_t NumElements = getContext().getConstantArrayElementCount(CA); 943 llvm::Value * NumElementsPtr = 944 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements); 945 llvm::Value *Counter = Builder.CreateLoad(IndexPtr); 946 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr, 947 "isless"); 948 // If the condition is true, execute the body. 949 Builder.CreateCondBr(IsLess, ForBody, AfterFor); 950 951 EmitBlock(ForBody); 952 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); 953 // Inside the loop body, emit the constructor call on the array element. 954 Counter = Builder.CreateLoad(IndexPtr); 955 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress"); 956 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress"); 957 if (BitwiseCopy) 958 EmitAggregateCopy(Dest, Src, Ty); 959 else if (CXXConstructorDecl *BaseCopyCtor = 960 BaseClassDecl->getCopyConstructor(getContext(), 0)) { 961 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor, 962 Ctor_Complete); 963 CallArgList CallArgs; 964 // Push the this (Dest) ptr. 965 CallArgs.push_back(std::make_pair(RValue::get(Dest), 966 BaseCopyCtor->getThisType(getContext()))); 967 968 // Push the Src ptr. 969 CallArgs.push_back(std::make_pair(RValue::get(Src), 970 BaseCopyCtor->getParamDecl(0)->getType())); 971 QualType ResultType = 972 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType(); 973 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), 974 Callee, CallArgs, BaseCopyCtor); 975 } 976 EmitBlock(ContinueBlock); 977 978 // Emit the increment of the loop counter. 979 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); 980 Counter = Builder.CreateLoad(IndexPtr); 981 NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); 982 Builder.CreateStore(NextVal, IndexPtr, false); 983 984 // Finally, branch back up to the condition for the next iteration. 985 EmitBranch(CondBlock); 986 987 // Emit the fall-through block. 988 EmitBlock(AfterFor, true); 989} 990 991/// EmitClassAggrCopyAssignment - This routine generates code to assign a class 992/// array of objects from SrcValue to DestValue. Assignment can be either a 993/// bitwise assignment or via a copy assignment operator function call. 994/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy 995void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest, 996 llvm::Value *Src, 997 const ArrayType *Array, 998 const CXXRecordDecl *BaseClassDecl, 999 QualType Ty) { 1000 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); 1001 assert(CA && "VLA cannot be asssigned"); 1002 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment(); 1003 1004 // Create a temporary for the loop index and initialize it with 0. 1005 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext), 1006 "loop.index"); 1007 llvm::Value* zeroConstant = 1008 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)); 1009 Builder.CreateStore(zeroConstant, IndexPtr, false); 1010 // Start the loop with a block that tests the condition. 1011 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); 1012 llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); 1013 1014 EmitBlock(CondBlock); 1015 1016 llvm::BasicBlock *ForBody = createBasicBlock("for.body"); 1017 // Generate: if (loop-index < number-of-elements fall to the loop body, 1018 // otherwise, go to the block after the for-loop. 1019 uint64_t NumElements = getContext().getConstantArrayElementCount(CA); 1020 llvm::Value * NumElementsPtr = 1021 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements); 1022 llvm::Value *Counter = Builder.CreateLoad(IndexPtr); 1023 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr, 1024 "isless"); 1025 // If the condition is true, execute the body. 1026 Builder.CreateCondBr(IsLess, ForBody, AfterFor); 1027 1028 EmitBlock(ForBody); 1029 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); 1030 // Inside the loop body, emit the assignment operator call on array element. 1031 Counter = Builder.CreateLoad(IndexPtr); 1032 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress"); 1033 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress"); 1034 const CXXMethodDecl *MD = 0; 1035 if (BitwiseAssign) 1036 EmitAggregateCopy(Dest, Src, Ty); 1037 else { 1038 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(), 1039 MD); 1040 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign"); 1041 (void)hasCopyAssign; 1042 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 1043 const llvm::Type *LTy = 1044 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), 1045 FPT->isVariadic()); 1046 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy); 1047 1048 CallArgList CallArgs; 1049 // Push the this (Dest) ptr. 1050 CallArgs.push_back(std::make_pair(RValue::get(Dest), 1051 MD->getThisType(getContext()))); 1052 1053 // Push the Src ptr. 1054 CallArgs.push_back(std::make_pair(RValue::get(Src), 1055 MD->getParamDecl(0)->getType())); 1056 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType(); 1057 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), 1058 Callee, CallArgs, MD); 1059 } 1060 EmitBlock(ContinueBlock); 1061 1062 // Emit the increment of the loop counter. 1063 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); 1064 Counter = Builder.CreateLoad(IndexPtr); 1065 NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); 1066 Builder.CreateStore(NextVal, IndexPtr, false); 1067 1068 // Finally, branch back up to the condition for the next iteration. 1069 EmitBranch(CondBlock); 1070 1071 // Emit the fall-through block. 1072 EmitBlock(AfterFor, true); 1073} 1074 1075/// EmitClassMemberwiseCopy - This routine generates code to copy a class 1076/// object from SrcValue to DestValue. Copying can be either a bitwise copy 1077/// or via a copy constructor call. 1078void CodeGenFunction::EmitClassMemberwiseCopy( 1079 llvm::Value *Dest, llvm::Value *Src, 1080 const CXXRecordDecl *ClassDecl, 1081 const CXXRecordDecl *BaseClassDecl, QualType Ty) { 1082 if (ClassDecl) { 1083 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl, 1084 /*NullCheckValue=*/false); 1085 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl, 1086 /*NullCheckValue=*/false); 1087 } 1088 if (BaseClassDecl->hasTrivialCopyConstructor()) { 1089 EmitAggregateCopy(Dest, Src, Ty); 1090 return; 1091 } 1092 1093 if (CXXConstructorDecl *BaseCopyCtor = 1094 BaseClassDecl->getCopyConstructor(getContext(), 0)) { 1095 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor, 1096 Ctor_Complete); 1097 CallArgList CallArgs; 1098 // Push the this (Dest) ptr. 1099 CallArgs.push_back(std::make_pair(RValue::get(Dest), 1100 BaseCopyCtor->getThisType(getContext()))); 1101 1102 // Push the Src ptr. 1103 CallArgs.push_back(std::make_pair(RValue::get(Src), 1104 BaseCopyCtor->getParamDecl(0)->getType())); 1105 QualType ResultType = 1106 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType(); 1107 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), 1108 Callee, CallArgs, BaseCopyCtor); 1109 } 1110} 1111 1112/// EmitClassCopyAssignment - This routine generates code to copy assign a class 1113/// object from SrcValue to DestValue. Assignment can be either a bitwise 1114/// assignment of via an assignment operator call. 1115// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot. 1116void CodeGenFunction::EmitClassCopyAssignment( 1117 llvm::Value *Dest, llvm::Value *Src, 1118 const CXXRecordDecl *ClassDecl, 1119 const CXXRecordDecl *BaseClassDecl, 1120 QualType Ty) { 1121 if (ClassDecl) { 1122 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl, 1123 /*NullCheckValue=*/false); 1124 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl, 1125 /*NullCheckValue=*/false); 1126 } 1127 if (BaseClassDecl->hasTrivialCopyAssignment()) { 1128 EmitAggregateCopy(Dest, Src, Ty); 1129 return; 1130 } 1131 1132 const CXXMethodDecl *MD = 0; 1133 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(), 1134 MD); 1135 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign"); 1136 (void)ConstCopyAssignOp; 1137 1138 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 1139 const llvm::Type *LTy = 1140 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), 1141 FPT->isVariadic()); 1142 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy); 1143 1144 CallArgList CallArgs; 1145 // Push the this (Dest) ptr. 1146 CallArgs.push_back(std::make_pair(RValue::get(Dest), 1147 MD->getThisType(getContext()))); 1148 1149 // Push the Src ptr. 1150 CallArgs.push_back(std::make_pair(RValue::get(Src), 1151 MD->getParamDecl(0)->getType())); 1152 QualType ResultType = 1153 MD->getType()->getAs<FunctionType>()->getResultType(); 1154 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), 1155 Callee, CallArgs, MD); 1156} 1157 1158/// SynthesizeDefaultConstructor - synthesize a default constructor 1159void 1160CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor, 1161 CXXCtorType Type, 1162 llvm::Function *Fn, 1163 const FunctionArgList &Args) { 1164 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args, 1165 SourceLocation()); 1166 EmitCtorPrologue(Ctor, Type); 1167 FinishFunction(); 1168} 1169 1170/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a 1171/// copy constructor, in accordance with section 12.8 (p7 and p8) of C++03 1172/// The implicitly-defined copy constructor for class X performs a memberwise 1173/// copy of its subobjects. The order of copying is the same as the order of 1174/// initialization of bases and members in a user-defined constructor 1175/// Each subobject is copied in the manner appropriate to its type: 1176/// if the subobject is of class type, the copy constructor for the class is 1177/// used; 1178/// if the subobject is an array, each element is copied, in the manner 1179/// appropriate to the element type; 1180/// if the subobject is of scalar type, the built-in assignment operator is 1181/// used. 1182/// Virtual base class subobjects shall be copied only once by the 1183/// implicitly-defined copy constructor 1184 1185void 1186CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor, 1187 CXXCtorType Type, 1188 llvm::Function *Fn, 1189 const FunctionArgList &Args) { 1190 const CXXRecordDecl *ClassDecl = Ctor->getParent(); 1191 assert(!ClassDecl->hasUserDeclaredCopyConstructor() && 1192 "SynthesizeCXXCopyConstructor - copy constructor has definition already"); 1193 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args, 1194 SourceLocation()); 1195 1196 FunctionArgList::const_iterator i = Args.begin(); 1197 const VarDecl *ThisArg = i->first; 1198 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg); 1199 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this"); 1200 const VarDecl *SrcArg = (i+1)->first; 1201 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg); 1202 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj); 1203 1204 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); 1205 Base != ClassDecl->bases_end(); ++Base) { 1206 // FIXME. copy constrution of virtual base NYI 1207 if (Base->isVirtual()) 1208 continue; 1209 1210 CXXRecordDecl *BaseClassDecl 1211 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 1212 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl, 1213 Base->getType()); 1214 } 1215 1216 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), 1217 FieldEnd = ClassDecl->field_end(); 1218 Field != FieldEnd; ++Field) { 1219 QualType FieldType = getContext().getCanonicalType((*Field)->getType()); 1220 const ConstantArrayType *Array = 1221 getContext().getAsConstantArrayType(FieldType); 1222 if (Array) 1223 FieldType = getContext().getBaseElementType(FieldType); 1224 1225 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { 1226 CXXRecordDecl *FieldClassDecl 1227 = cast<CXXRecordDecl>(FieldClassType->getDecl()); 1228 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); 1229 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); 1230 if (Array) { 1231 const llvm::Type *BasePtr = ConvertType(FieldType); 1232 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1233 llvm::Value *DestBaseAddrPtr = 1234 Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1235 llvm::Value *SrcBaseAddrPtr = 1236 Builder.CreateBitCast(RHS.getAddress(), BasePtr); 1237 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array, 1238 FieldClassDecl, FieldType); 1239 } 1240 else 1241 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(), 1242 0 /*ClassDecl*/, FieldClassDecl, FieldType); 1243 continue; 1244 } 1245 // Do a built-in assignment of scalar data members. 1246 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); 1247 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); 1248 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType); 1249 EmitStoreThroughLValue(RVRHS, LHS, FieldType); 1250 } 1251 FinishFunction(); 1252} 1253 1254/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator. 1255/// Before the implicitly-declared copy assignment operator for a class is 1256/// implicitly defined, all implicitly- declared copy assignment operators for 1257/// its direct base classes and its nonstatic data members shall have been 1258/// implicitly defined. [12.8-p12] 1259/// The implicitly-defined copy assignment operator for class X performs 1260/// memberwise assignment of its subob- jects. The direct base classes of X are 1261/// assigned first, in the order of their declaration in 1262/// the base-specifier-list, and then the immediate nonstatic data members of X 1263/// are assigned, in the order in which they were declared in the class 1264/// definition.Each subobject is assigned in the manner appropriate to its type: 1265/// if the subobject is of class type, the copy assignment operator for the 1266/// class is used (as if by explicit qualification; that is, ignoring any 1267/// possible virtual overriding functions in more derived classes); 1268/// 1269/// if the subobject is an array, each element is assigned, in the manner 1270/// appropriate to the element type; 1271/// 1272/// if the subobject is of scalar type, the built-in assignment operator is 1273/// used. 1274void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD, 1275 llvm::Function *Fn, 1276 const FunctionArgList &Args) { 1277 1278 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext()); 1279 assert(!ClassDecl->hasUserDeclaredCopyAssignment() && 1280 "SynthesizeCXXCopyAssignment - copy assignment has user declaration"); 1281 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation()); 1282 1283 FunctionArgList::const_iterator i = Args.begin(); 1284 const VarDecl *ThisArg = i->first; 1285 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg); 1286 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this"); 1287 const VarDecl *SrcArg = (i+1)->first; 1288 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg); 1289 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj); 1290 1291 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); 1292 Base != ClassDecl->bases_end(); ++Base) { 1293 // FIXME. copy assignment of virtual base NYI 1294 if (Base->isVirtual()) 1295 continue; 1296 1297 CXXRecordDecl *BaseClassDecl 1298 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 1299 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl, 1300 Base->getType()); 1301 } 1302 1303 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), 1304 FieldEnd = ClassDecl->field_end(); 1305 Field != FieldEnd; ++Field) { 1306 QualType FieldType = getContext().getCanonicalType((*Field)->getType()); 1307 const ConstantArrayType *Array = 1308 getContext().getAsConstantArrayType(FieldType); 1309 if (Array) 1310 FieldType = getContext().getBaseElementType(FieldType); 1311 1312 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { 1313 CXXRecordDecl *FieldClassDecl 1314 = cast<CXXRecordDecl>(FieldClassType->getDecl()); 1315 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); 1316 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); 1317 if (Array) { 1318 const llvm::Type *BasePtr = ConvertType(FieldType); 1319 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1320 llvm::Value *DestBaseAddrPtr = 1321 Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1322 llvm::Value *SrcBaseAddrPtr = 1323 Builder.CreateBitCast(RHS.getAddress(), BasePtr); 1324 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array, 1325 FieldClassDecl, FieldType); 1326 } 1327 else 1328 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(), 1329 0 /*ClassDecl*/, FieldClassDecl, FieldType); 1330 continue; 1331 } 1332 // Do a built-in assignment of scalar data members. 1333 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); 1334 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); 1335 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType); 1336 EmitStoreThroughLValue(RVRHS, LHS, FieldType); 1337 } 1338 1339 // return *this; 1340 Builder.CreateStore(LoadOfThis, ReturnValue); 1341 1342 FinishFunction(); 1343} 1344 1345/// EmitCtorPrologue - This routine generates necessary code to initialize 1346/// base classes and non-static data members belonging to this constructor. 1347/// FIXME: This needs to take a CXXCtorType. 1348void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD, 1349 CXXCtorType CtorType) { 1350 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext()); 1351 // FIXME: Add vbase initialization 1352 llvm::Value *LoadOfThis = 0; 1353 1354 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(), 1355 E = CD->init_end(); 1356 B != E; ++B) { 1357 CXXBaseOrMemberInitializer *Member = (*B); 1358 if (Member->isBaseInitializer()) { 1359 LoadOfThis = LoadCXXThis(); 1360 Type *BaseType = Member->getBaseClass(); 1361 CXXRecordDecl *BaseClassDecl = 1362 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); 1363 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl, 1364 BaseClassDecl, 1365 /*NullCheckValue=*/false); 1366 EmitCXXConstructorCall(Member->getConstructor(), 1367 CtorType, V, 1368 Member->const_arg_begin(), 1369 Member->const_arg_end()); 1370 } else { 1371 // non-static data member initilaizers. 1372 FieldDecl *Field = Member->getMember(); 1373 QualType FieldType = getContext().getCanonicalType((Field)->getType()); 1374 const ConstantArrayType *Array = 1375 getContext().getAsConstantArrayType(FieldType); 1376 if (Array) 1377 FieldType = getContext().getBaseElementType(FieldType); 1378 1379 LoadOfThis = LoadCXXThis(); 1380 LValue LHS; 1381 if (FieldType->isReferenceType()) { 1382 // FIXME: This is really ugly; should be refactored somehow 1383 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field); 1384 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp"); 1385 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs"); 1386 LHS = LValue::MakeAddr(V, MakeQualifiers(FieldType)); 1387 } else { 1388 LHS = EmitLValueForField(LoadOfThis, Field, false, 0); 1389 } 1390 if (FieldType->getAs<RecordType>()) { 1391 if (!Field->isAnonymousStructOrUnion()) { 1392 assert(Member->getConstructor() && 1393 "EmitCtorPrologue - no constructor to initialize member"); 1394 if (Array) { 1395 const llvm::Type *BasePtr = ConvertType(FieldType); 1396 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1397 llvm::Value *BaseAddrPtr = 1398 Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1399 EmitCXXAggrConstructorCall(Member->getConstructor(), 1400 Array, BaseAddrPtr); 1401 } 1402 else 1403 EmitCXXConstructorCall(Member->getConstructor(), 1404 Ctor_Complete, LHS.getAddress(), 1405 Member->const_arg_begin(), 1406 Member->const_arg_end()); 1407 continue; 1408 } 1409 else { 1410 // Initializing an anonymous union data member. 1411 FieldDecl *anonMember = Member->getAnonUnionMember(); 1412 LHS = EmitLValueForField(LHS.getAddress(), anonMember, 1413 /*IsUnion=*/true, 0); 1414 FieldType = anonMember->getType(); 1415 } 1416 } 1417 1418 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only"); 1419 Expr *RhsExpr = *Member->arg_begin(); 1420 RValue RHS; 1421 if (FieldType->isReferenceType()) 1422 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType, 1423 /*IsInitializer=*/true); 1424 else 1425 RHS = RValue::get(EmitScalarExpr(RhsExpr, true)); 1426 EmitStoreThroughLValue(RHS, LHS, FieldType); 1427 } 1428 } 1429 1430 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) { 1431 // Nontrivial default constructor with no initializer list. It may still 1432 // have bases classes and/or contain non-static data members which require 1433 // construction. 1434 for (CXXRecordDecl::base_class_const_iterator Base = 1435 ClassDecl->bases_begin(); 1436 Base != ClassDecl->bases_end(); ++Base) { 1437 // FIXME. copy assignment of virtual base NYI 1438 if (Base->isVirtual()) 1439 continue; 1440 1441 CXXRecordDecl *BaseClassDecl 1442 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 1443 if (BaseClassDecl->hasTrivialConstructor()) 1444 continue; 1445 if (CXXConstructorDecl *BaseCX = 1446 BaseClassDecl->getDefaultConstructor(getContext())) { 1447 LoadOfThis = LoadCXXThis(); 1448 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl, 1449 BaseClassDecl, 1450 /*NullCheckValue=*/false); 1451 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0); 1452 } 1453 } 1454 1455 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), 1456 FieldEnd = ClassDecl->field_end(); 1457 Field != FieldEnd; ++Field) { 1458 QualType FieldType = getContext().getCanonicalType((*Field)->getType()); 1459 const ConstantArrayType *Array = 1460 getContext().getAsConstantArrayType(FieldType); 1461 if (Array) 1462 FieldType = getContext().getBaseElementType(FieldType); 1463 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion()) 1464 continue; 1465 const RecordType *ClassRec = FieldType->getAs<RecordType>(); 1466 CXXRecordDecl *MemberClassDecl = 1467 dyn_cast<CXXRecordDecl>(ClassRec->getDecl()); 1468 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor()) 1469 continue; 1470 if (CXXConstructorDecl *MamberCX = 1471 MemberClassDecl->getDefaultConstructor(getContext())) { 1472 LoadOfThis = LoadCXXThis(); 1473 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); 1474 if (Array) { 1475 const llvm::Type *BasePtr = ConvertType(FieldType); 1476 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1477 llvm::Value *BaseAddrPtr = 1478 Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1479 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr); 1480 } 1481 else 1482 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(), 1483 0, 0); 1484 } 1485 } 1486 } 1487 1488 // Initialize the vtable pointer 1489 if (ClassDecl->isDynamicClass()) { 1490 if (!LoadOfThis) 1491 LoadOfThis = LoadCXXThis(); 1492 llvm::Value *VtableField; 1493 llvm::Type *Ptr8Ty, *PtrPtr8Ty; 1494 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); 1495 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0); 1496 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty); 1497 llvm::Value *vtable = GenerateVtable(ClassDecl); 1498 Builder.CreateStore(vtable, VtableField); 1499 } 1500} 1501 1502/// EmitDtorEpilogue - Emit all code that comes at the end of class's 1503/// destructor. This is to call destructors on members and base classes 1504/// in reverse order of their construction. 1505/// FIXME: This needs to take a CXXDtorType. 1506void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD, 1507 CXXDtorType DtorType) { 1508 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext()); 1509 assert(!ClassDecl->getNumVBases() && 1510 "FIXME: Destruction of virtual bases not supported"); 1511 (void)ClassDecl; // prevent warning. 1512 1513 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(), 1514 *E = DD->destr_end(); B != E; ++B) { 1515 uintptr_t BaseOrMember = (*B); 1516 if (DD->isMemberToDestroy(BaseOrMember)) { 1517 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember); 1518 QualType FieldType = getContext().getCanonicalType((FD)->getType()); 1519 const ConstantArrayType *Array = 1520 getContext().getAsConstantArrayType(FieldType); 1521 if (Array) 1522 FieldType = getContext().getBaseElementType(FieldType); 1523 const RecordType *RT = FieldType->getAs<RecordType>(); 1524 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); 1525 if (FieldClassDecl->hasTrivialDestructor()) 1526 continue; 1527 llvm::Value *LoadOfThis = LoadCXXThis(); 1528 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0); 1529 if (Array) { 1530 const llvm::Type *BasePtr = ConvertType(FieldType); 1531 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1532 llvm::Value *BaseAddrPtr = 1533 Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1534 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()), 1535 Array, BaseAddrPtr); 1536 } 1537 else 1538 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()), 1539 Dtor_Complete, LHS.getAddress()); 1540 } else { 1541 const RecordType *RT = 1542 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>(); 1543 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); 1544 if (BaseClassDecl->hasTrivialDestructor()) 1545 continue; 1546 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(), 1547 ClassDecl, BaseClassDecl, 1548 /*NullCheckValue=*/false); 1549 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()), 1550 DtorType, V); 1551 } 1552 } 1553 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial()) 1554 return; 1555 // Case of destructor synthesis with fields and base classes 1556 // which have non-trivial destructors. They must be destructed in 1557 // reverse order of their construction. 1558 llvm::SmallVector<FieldDecl *, 16> DestructedFields; 1559 1560 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), 1561 FieldEnd = ClassDecl->field_end(); 1562 Field != FieldEnd; ++Field) { 1563 QualType FieldType = getContext().getCanonicalType((*Field)->getType()); 1564 if (getContext().getAsConstantArrayType(FieldType)) 1565 FieldType = getContext().getBaseElementType(FieldType); 1566 if (const RecordType *RT = FieldType->getAs<RecordType>()) { 1567 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); 1568 if (FieldClassDecl->hasTrivialDestructor()) 1569 continue; 1570 DestructedFields.push_back(*Field); 1571 } 1572 } 1573 if (!DestructedFields.empty()) 1574 for (int i = DestructedFields.size() -1; i >= 0; --i) { 1575 FieldDecl *Field = DestructedFields[i]; 1576 QualType FieldType = Field->getType(); 1577 const ConstantArrayType *Array = 1578 getContext().getAsConstantArrayType(FieldType); 1579 if (Array) 1580 FieldType = getContext().getBaseElementType(FieldType); 1581 const RecordType *RT = FieldType->getAs<RecordType>(); 1582 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); 1583 llvm::Value *LoadOfThis = LoadCXXThis(); 1584 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0); 1585 if (Array) { 1586 const llvm::Type *BasePtr = ConvertType(FieldType); 1587 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1588 llvm::Value *BaseAddrPtr = 1589 Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1590 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()), 1591 Array, BaseAddrPtr); 1592 } 1593 else 1594 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()), 1595 Dtor_Complete, LHS.getAddress()); 1596 } 1597 1598 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases; 1599 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); 1600 Base != ClassDecl->bases_end(); ++Base) { 1601 // FIXME. copy assignment of virtual base NYI 1602 if (Base->isVirtual()) 1603 continue; 1604 1605 CXXRecordDecl *BaseClassDecl 1606 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 1607 if (BaseClassDecl->hasTrivialDestructor()) 1608 continue; 1609 DestructedBases.push_back(BaseClassDecl); 1610 } 1611 if (DestructedBases.empty()) 1612 return; 1613 for (int i = DestructedBases.size() -1; i >= 0; --i) { 1614 CXXRecordDecl *BaseClassDecl = DestructedBases[i]; 1615 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(), 1616 ClassDecl,BaseClassDecl, 1617 /*NullCheckValue=*/false); 1618 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()), 1619 Dtor_Complete, V); 1620 } 1621} 1622 1623void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor, 1624 CXXDtorType DtorType, 1625 llvm::Function *Fn, 1626 const FunctionArgList &Args) { 1627 1628 const CXXRecordDecl *ClassDecl = Dtor->getParent(); 1629 assert(!ClassDecl->hasUserDeclaredDestructor() && 1630 "SynthesizeDefaultDestructor - destructor has user declaration"); 1631 (void) ClassDecl; 1632 1633 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args, 1634 SourceLocation()); 1635 EmitDtorEpilogue(Dtor, DtorType); 1636 FinishFunction(); 1637} 1638 1639// FIXME: Move this to CGCXXStmt.cpp 1640void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) { 1641 // FIXME: We need to do more here. 1642 EmitStmt(S.getTryBlock()); 1643} 1644