CGCXX.cpp revision 1faf67478c6d423880726a327c737db13b8d9f0b
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>(MPT->getClass()->getAs<RecordType>()->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 bool CanBeZero = !(ResultType->isReferenceType() 814 // FIXME: attr nonnull can't be zero either 815 /* || ResultType->hasAttr<NonNullAttr>() */ ); 816 // Do the return result adjustment. 817 if (CanBeZero) { 818 llvm::BasicBlock *NonZeroBlock = createBasicBlock(); 819 llvm::BasicBlock *ZeroBlock = createBasicBlock(); 820 llvm::BasicBlock *ContBlock = createBasicBlock(); 821 822 const llvm::Type *Ty = RV.getScalarVal()->getType(); 823 llvm::Value *Zero = llvm::Constant::getNullValue(Ty); 824 Builder.CreateCondBr(Builder.CreateICmpNE(RV.getScalarVal(), Zero), 825 NonZeroBlock, ZeroBlock); 826 EmitBlock(NonZeroBlock); 827 llvm::Value *NZ = DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r); 828 EmitBranch(ContBlock); 829 EmitBlock(ZeroBlock); 830 llvm::Value *Z = RV.getScalarVal(); 831 EmitBlock(ContBlock); 832 llvm::PHINode *RVOrZero = Builder.CreatePHI(Ty); 833 RVOrZero->reserveOperandSpace(2); 834 RVOrZero->addIncoming(NZ, NonZeroBlock); 835 RVOrZero->addIncoming(Z, ZeroBlock); 836 RV = RValue::get(RVOrZero); 837 } else 838 RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(), nv_r, v_r)); 839 } 840 841 if (!ResultType->isVoidType()) 842 EmitReturnOfRValue(RV, ResultType); 843 844 FinishFunction(); 845 return Fn; 846} 847 848llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern, 849 int64_t nv, int64_t v) { 850 llvm::SmallString<256> OutName; 851 llvm::raw_svector_ostream Out(OutName); 852 mangleThunk(getMangleContext(), MD, nv, v, 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).GenerateThunk(Fn, MD, Extern, nv, v); 866 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); 867 return m; 868} 869 870llvm::Constant *CodeGenModule::BuildCovariantThunk(const CXXMethodDecl *MD, 871 bool Extern, int64_t nv_t, 872 int64_t v_t, int64_t nv_r, 873 int64_t v_r) { 874 llvm::SmallString<256> OutName; 875 llvm::raw_svector_ostream Out(OutName); 876 mangleCovariantThunk(getMangleContext(), MD, nv_t, v_t, nv_r, v_r, Out); 877 llvm::GlobalVariable::LinkageTypes linktype; 878 linktype = llvm::GlobalValue::WeakAnyLinkage; 879 if (!Extern) 880 linktype = llvm::GlobalValue::InternalLinkage; 881 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); 882 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 883 const llvm::FunctionType *FTy = 884 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD), 885 FPT->isVariadic()); 886 887 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(), 888 &getModule()); 889 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, nv_t, v_t, nv_r, 890 v_r); 891 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); 892 return m; 893} 894 895llvm::Value * 896CodeGenFunction::GetVirtualCXXBaseClassOffset(llvm::Value *This, 897 const CXXRecordDecl *ClassDecl, 898 const CXXRecordDecl *BaseClassDecl) { 899 const llvm::Type *Int8PtrTy = 900 llvm::Type::getInt8Ty(VMContext)->getPointerTo(); 901 902 llvm::Value *VTablePtr = Builder.CreateBitCast(This, 903 Int8PtrTy->getPointerTo()); 904 VTablePtr = Builder.CreateLoad(VTablePtr, "vtable"); 905 906 int64_t VBaseOffsetIndex = 907 CGM.getVtableInfo().getVirtualBaseOffsetIndex(ClassDecl, BaseClassDecl); 908 909 llvm::Value *VBaseOffsetPtr = 910 Builder.CreateConstGEP1_64(VTablePtr, VBaseOffsetIndex, "vbase.offset.ptr"); 911 const llvm::Type *PtrDiffTy = 912 ConvertType(getContext().getPointerDiffType()); 913 914 VBaseOffsetPtr = Builder.CreateBitCast(VBaseOffsetPtr, 915 PtrDiffTy->getPointerTo()); 916 917 llvm::Value *VBaseOffset = Builder.CreateLoad(VBaseOffsetPtr, "vbase.offset"); 918 919 return VBaseOffset; 920} 921 922llvm::Value * 923CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This, 924 const llvm::Type *Ty) { 925 int64_t Index = CGM.getVtableInfo().getMethodVtableIndex(MD); 926 927 Ty = llvm::PointerType::get(Ty, 0); 928 Ty = llvm::PointerType::get(Ty, 0); 929 Ty = llvm::PointerType::get(Ty, 0); 930 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty); 931 vtbl = Builder.CreateLoad(vtbl); 932 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl, 933 Index, "vfn"); 934 vfn = Builder.CreateLoad(vfn); 935 return vfn; 936} 937 938/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class 939/// array of objects from SrcValue to DestValue. Copying can be either a bitwise 940/// copy or via a copy constructor call. 941// FIXME. Consolidate this with EmitCXXAggrConstructorCall. 942void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest, 943 llvm::Value *Src, 944 const ArrayType *Array, 945 const CXXRecordDecl *BaseClassDecl, 946 QualType Ty) { 947 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); 948 assert(CA && "VLA cannot be copied over"); 949 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor(); 950 951 // Create a temporary for the loop index and initialize it with 0. 952 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext), 953 "loop.index"); 954 llvm::Value* zeroConstant = 955 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)); 956 Builder.CreateStore(zeroConstant, IndexPtr, false); 957 // Start the loop with a block that tests the condition. 958 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); 959 llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); 960 961 EmitBlock(CondBlock); 962 963 llvm::BasicBlock *ForBody = createBasicBlock("for.body"); 964 // Generate: if (loop-index < number-of-elements fall to the loop body, 965 // otherwise, go to the block after the for-loop. 966 uint64_t NumElements = getContext().getConstantArrayElementCount(CA); 967 llvm::Value * NumElementsPtr = 968 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements); 969 llvm::Value *Counter = Builder.CreateLoad(IndexPtr); 970 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr, 971 "isless"); 972 // If the condition is true, execute the body. 973 Builder.CreateCondBr(IsLess, ForBody, AfterFor); 974 975 EmitBlock(ForBody); 976 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); 977 // Inside the loop body, emit the constructor call on the array element. 978 Counter = Builder.CreateLoad(IndexPtr); 979 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress"); 980 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress"); 981 if (BitwiseCopy) 982 EmitAggregateCopy(Dest, Src, Ty); 983 else if (CXXConstructorDecl *BaseCopyCtor = 984 BaseClassDecl->getCopyConstructor(getContext(), 0)) { 985 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor, 986 Ctor_Complete); 987 CallArgList CallArgs; 988 // Push the this (Dest) ptr. 989 CallArgs.push_back(std::make_pair(RValue::get(Dest), 990 BaseCopyCtor->getThisType(getContext()))); 991 992 // Push the Src ptr. 993 CallArgs.push_back(std::make_pair(RValue::get(Src), 994 BaseCopyCtor->getParamDecl(0)->getType())); 995 QualType ResultType = 996 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType(); 997 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), 998 Callee, CallArgs, BaseCopyCtor); 999 } 1000 EmitBlock(ContinueBlock); 1001 1002 // Emit the increment of the loop counter. 1003 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); 1004 Counter = Builder.CreateLoad(IndexPtr); 1005 NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); 1006 Builder.CreateStore(NextVal, IndexPtr, false); 1007 1008 // Finally, branch back up to the condition for the next iteration. 1009 EmitBranch(CondBlock); 1010 1011 // Emit the fall-through block. 1012 EmitBlock(AfterFor, true); 1013} 1014 1015/// EmitClassAggrCopyAssignment - This routine generates code to assign a class 1016/// array of objects from SrcValue to DestValue. Assignment can be either a 1017/// bitwise assignment or via a copy assignment operator function call. 1018/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy 1019void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest, 1020 llvm::Value *Src, 1021 const ArrayType *Array, 1022 const CXXRecordDecl *BaseClassDecl, 1023 QualType Ty) { 1024 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); 1025 assert(CA && "VLA cannot be asssigned"); 1026 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment(); 1027 1028 // Create a temporary for the loop index and initialize it with 0. 1029 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext), 1030 "loop.index"); 1031 llvm::Value* zeroConstant = 1032 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)); 1033 Builder.CreateStore(zeroConstant, IndexPtr, false); 1034 // Start the loop with a block that tests the condition. 1035 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); 1036 llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); 1037 1038 EmitBlock(CondBlock); 1039 1040 llvm::BasicBlock *ForBody = createBasicBlock("for.body"); 1041 // Generate: if (loop-index < number-of-elements fall to the loop body, 1042 // otherwise, go to the block after the for-loop. 1043 uint64_t NumElements = getContext().getConstantArrayElementCount(CA); 1044 llvm::Value * NumElementsPtr = 1045 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements); 1046 llvm::Value *Counter = Builder.CreateLoad(IndexPtr); 1047 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr, 1048 "isless"); 1049 // If the condition is true, execute the body. 1050 Builder.CreateCondBr(IsLess, ForBody, AfterFor); 1051 1052 EmitBlock(ForBody); 1053 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); 1054 // Inside the loop body, emit the assignment operator call on array element. 1055 Counter = Builder.CreateLoad(IndexPtr); 1056 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress"); 1057 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress"); 1058 const CXXMethodDecl *MD = 0; 1059 if (BitwiseAssign) 1060 EmitAggregateCopy(Dest, Src, Ty); 1061 else { 1062 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(), 1063 MD); 1064 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign"); 1065 (void)hasCopyAssign; 1066 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 1067 const llvm::Type *LTy = 1068 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), 1069 FPT->isVariadic()); 1070 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy); 1071 1072 CallArgList CallArgs; 1073 // Push the this (Dest) ptr. 1074 CallArgs.push_back(std::make_pair(RValue::get(Dest), 1075 MD->getThisType(getContext()))); 1076 1077 // Push the Src ptr. 1078 CallArgs.push_back(std::make_pair(RValue::get(Src), 1079 MD->getParamDecl(0)->getType())); 1080 QualType ResultType = MD->getType()->getAs<FunctionType>()->getResultType(); 1081 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), 1082 Callee, CallArgs, MD); 1083 } 1084 EmitBlock(ContinueBlock); 1085 1086 // Emit the increment of the loop counter. 1087 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); 1088 Counter = Builder.CreateLoad(IndexPtr); 1089 NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); 1090 Builder.CreateStore(NextVal, IndexPtr, false); 1091 1092 // Finally, branch back up to the condition for the next iteration. 1093 EmitBranch(CondBlock); 1094 1095 // Emit the fall-through block. 1096 EmitBlock(AfterFor, true); 1097} 1098 1099/// EmitClassMemberwiseCopy - This routine generates code to copy a class 1100/// object from SrcValue to DestValue. Copying can be either a bitwise copy 1101/// or via a copy constructor call. 1102void CodeGenFunction::EmitClassMemberwiseCopy( 1103 llvm::Value *Dest, llvm::Value *Src, 1104 const CXXRecordDecl *ClassDecl, 1105 const CXXRecordDecl *BaseClassDecl, QualType Ty) { 1106 if (ClassDecl) { 1107 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl, 1108 /*NullCheckValue=*/false); 1109 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl, 1110 /*NullCheckValue=*/false); 1111 } 1112 if (BaseClassDecl->hasTrivialCopyConstructor()) { 1113 EmitAggregateCopy(Dest, Src, Ty); 1114 return; 1115 } 1116 1117 if (CXXConstructorDecl *BaseCopyCtor = 1118 BaseClassDecl->getCopyConstructor(getContext(), 0)) { 1119 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor, 1120 Ctor_Complete); 1121 CallArgList CallArgs; 1122 // Push the this (Dest) ptr. 1123 CallArgs.push_back(std::make_pair(RValue::get(Dest), 1124 BaseCopyCtor->getThisType(getContext()))); 1125 1126 // Push the Src ptr. 1127 CallArgs.push_back(std::make_pair(RValue::get(Src), 1128 BaseCopyCtor->getParamDecl(0)->getType())); 1129 QualType ResultType = 1130 BaseCopyCtor->getType()->getAs<FunctionType>()->getResultType(); 1131 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), 1132 Callee, CallArgs, BaseCopyCtor); 1133 } 1134} 1135 1136/// EmitClassCopyAssignment - This routine generates code to copy assign a class 1137/// object from SrcValue to DestValue. Assignment can be either a bitwise 1138/// assignment of via an assignment operator call. 1139// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot. 1140void CodeGenFunction::EmitClassCopyAssignment( 1141 llvm::Value *Dest, llvm::Value *Src, 1142 const CXXRecordDecl *ClassDecl, 1143 const CXXRecordDecl *BaseClassDecl, 1144 QualType Ty) { 1145 if (ClassDecl) { 1146 Dest = GetAddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl, 1147 /*NullCheckValue=*/false); 1148 Src = GetAddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl, 1149 /*NullCheckValue=*/false); 1150 } 1151 if (BaseClassDecl->hasTrivialCopyAssignment()) { 1152 EmitAggregateCopy(Dest, Src, Ty); 1153 return; 1154 } 1155 1156 const CXXMethodDecl *MD = 0; 1157 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(), 1158 MD); 1159 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign"); 1160 (void)ConstCopyAssignOp; 1161 1162 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 1163 const llvm::Type *LTy = 1164 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), 1165 FPT->isVariadic()); 1166 llvm::Constant *Callee = CGM.GetAddrOfFunction(MD, LTy); 1167 1168 CallArgList CallArgs; 1169 // Push the this (Dest) ptr. 1170 CallArgs.push_back(std::make_pair(RValue::get(Dest), 1171 MD->getThisType(getContext()))); 1172 1173 // Push the Src ptr. 1174 CallArgs.push_back(std::make_pair(RValue::get(Src), 1175 MD->getParamDecl(0)->getType())); 1176 QualType ResultType = 1177 MD->getType()->getAs<FunctionType>()->getResultType(); 1178 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), 1179 Callee, CallArgs, MD); 1180} 1181 1182/// SynthesizeDefaultConstructor - synthesize a default constructor 1183void 1184CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor, 1185 CXXCtorType Type, 1186 llvm::Function *Fn, 1187 const FunctionArgList &Args) { 1188 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args, 1189 SourceLocation()); 1190 EmitCtorPrologue(Ctor, Type); 1191 FinishFunction(); 1192} 1193 1194/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a 1195/// copy constructor, in accordance with section 12.8 (p7 and p8) of C++03 1196/// The implicitly-defined copy constructor for class X performs a memberwise 1197/// copy of its subobjects. The order of copying is the same as the order of 1198/// initialization of bases and members in a user-defined constructor 1199/// Each subobject is copied in the manner appropriate to its type: 1200/// if the subobject is of class type, the copy constructor for the class is 1201/// used; 1202/// if the subobject is an array, each element is copied, in the manner 1203/// appropriate to the element type; 1204/// if the subobject is of scalar type, the built-in assignment operator is 1205/// used. 1206/// Virtual base class subobjects shall be copied only once by the 1207/// implicitly-defined copy constructor 1208 1209void 1210CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor, 1211 CXXCtorType Type, 1212 llvm::Function *Fn, 1213 const FunctionArgList &Args) { 1214 const CXXRecordDecl *ClassDecl = Ctor->getParent(); 1215 assert(!ClassDecl->hasUserDeclaredCopyConstructor() && 1216 "SynthesizeCXXCopyConstructor - copy constructor has definition already"); 1217 StartFunction(GlobalDecl(Ctor, Type), Ctor->getResultType(), Fn, Args, 1218 SourceLocation()); 1219 1220 FunctionArgList::const_iterator i = Args.begin(); 1221 const VarDecl *ThisArg = i->first; 1222 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg); 1223 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this"); 1224 const VarDecl *SrcArg = (i+1)->first; 1225 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg); 1226 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj); 1227 1228 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); 1229 Base != ClassDecl->bases_end(); ++Base) { 1230 // FIXME. copy constrution of virtual base NYI 1231 if (Base->isVirtual()) 1232 continue; 1233 1234 CXXRecordDecl *BaseClassDecl 1235 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 1236 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl, 1237 Base->getType()); 1238 } 1239 1240 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), 1241 FieldEnd = ClassDecl->field_end(); 1242 Field != FieldEnd; ++Field) { 1243 QualType FieldType = getContext().getCanonicalType((*Field)->getType()); 1244 const ConstantArrayType *Array = 1245 getContext().getAsConstantArrayType(FieldType); 1246 if (Array) 1247 FieldType = getContext().getBaseElementType(FieldType); 1248 1249 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { 1250 CXXRecordDecl *FieldClassDecl 1251 = cast<CXXRecordDecl>(FieldClassType->getDecl()); 1252 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); 1253 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); 1254 if (Array) { 1255 const llvm::Type *BasePtr = ConvertType(FieldType); 1256 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1257 llvm::Value *DestBaseAddrPtr = 1258 Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1259 llvm::Value *SrcBaseAddrPtr = 1260 Builder.CreateBitCast(RHS.getAddress(), BasePtr); 1261 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array, 1262 FieldClassDecl, FieldType); 1263 } 1264 else 1265 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(), 1266 0 /*ClassDecl*/, FieldClassDecl, FieldType); 1267 continue; 1268 } 1269 // Do a built-in assignment of scalar data members. 1270 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); 1271 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); 1272 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType); 1273 EmitStoreThroughLValue(RVRHS, LHS, FieldType); 1274 } 1275 FinishFunction(); 1276} 1277 1278/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator. 1279/// Before the implicitly-declared copy assignment operator for a class is 1280/// implicitly defined, all implicitly- declared copy assignment operators for 1281/// its direct base classes and its nonstatic data members shall have been 1282/// implicitly defined. [12.8-p12] 1283/// The implicitly-defined copy assignment operator for class X performs 1284/// memberwise assignment of its subob- jects. The direct base classes of X are 1285/// assigned first, in the order of their declaration in 1286/// the base-specifier-list, and then the immediate nonstatic data members of X 1287/// are assigned, in the order in which they were declared in the class 1288/// definition.Each subobject is assigned in the manner appropriate to its type: 1289/// if the subobject is of class type, the copy assignment operator for the 1290/// class is used (as if by explicit qualification; that is, ignoring any 1291/// possible virtual overriding functions in more derived classes); 1292/// 1293/// if the subobject is an array, each element is assigned, in the manner 1294/// appropriate to the element type; 1295/// 1296/// if the subobject is of scalar type, the built-in assignment operator is 1297/// used. 1298void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD, 1299 llvm::Function *Fn, 1300 const FunctionArgList &Args) { 1301 1302 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext()); 1303 assert(!ClassDecl->hasUserDeclaredCopyAssignment() && 1304 "SynthesizeCXXCopyAssignment - copy assignment has user declaration"); 1305 StartFunction(CD, CD->getResultType(), Fn, Args, SourceLocation()); 1306 1307 FunctionArgList::const_iterator i = Args.begin(); 1308 const VarDecl *ThisArg = i->first; 1309 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg); 1310 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this"); 1311 const VarDecl *SrcArg = (i+1)->first; 1312 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg); 1313 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj); 1314 1315 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); 1316 Base != ClassDecl->bases_end(); ++Base) { 1317 // FIXME. copy assignment of virtual base NYI 1318 if (Base->isVirtual()) 1319 continue; 1320 1321 CXXRecordDecl *BaseClassDecl 1322 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 1323 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl, 1324 Base->getType()); 1325 } 1326 1327 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), 1328 FieldEnd = ClassDecl->field_end(); 1329 Field != FieldEnd; ++Field) { 1330 QualType FieldType = getContext().getCanonicalType((*Field)->getType()); 1331 const ConstantArrayType *Array = 1332 getContext().getAsConstantArrayType(FieldType); 1333 if (Array) 1334 FieldType = getContext().getBaseElementType(FieldType); 1335 1336 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { 1337 CXXRecordDecl *FieldClassDecl 1338 = cast<CXXRecordDecl>(FieldClassType->getDecl()); 1339 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); 1340 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); 1341 if (Array) { 1342 const llvm::Type *BasePtr = ConvertType(FieldType); 1343 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1344 llvm::Value *DestBaseAddrPtr = 1345 Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1346 llvm::Value *SrcBaseAddrPtr = 1347 Builder.CreateBitCast(RHS.getAddress(), BasePtr); 1348 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array, 1349 FieldClassDecl, FieldType); 1350 } 1351 else 1352 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(), 1353 0 /*ClassDecl*/, FieldClassDecl, FieldType); 1354 continue; 1355 } 1356 // Do a built-in assignment of scalar data members. 1357 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); 1358 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); 1359 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType); 1360 EmitStoreThroughLValue(RVRHS, LHS, FieldType); 1361 } 1362 1363 // return *this; 1364 Builder.CreateStore(LoadOfThis, ReturnValue); 1365 1366 FinishFunction(); 1367} 1368 1369static void EmitBaseInitializer(CodeGenFunction &CGF, 1370 const CXXRecordDecl *ClassDecl, 1371 CXXBaseOrMemberInitializer *BaseInit, 1372 CXXCtorType CtorType) { 1373 assert(BaseInit->isBaseInitializer() && 1374 "Must have base initializer!"); 1375 1376 llvm::Value *ThisPtr = CGF.LoadCXXThis(); 1377 1378 const Type *BaseType = BaseInit->getBaseClass(); 1379 CXXRecordDecl *BaseClassDecl = 1380 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); 1381 llvm::Value *V = CGF.GetAddressCXXOfBaseClass(ThisPtr, ClassDecl, 1382 BaseClassDecl, 1383 /*NullCheckValue=*/false); 1384 CGF.EmitCXXConstructorCall(BaseInit->getConstructor(), 1385 CtorType, V, 1386 BaseInit->const_arg_begin(), 1387 BaseInit->const_arg_end()); 1388} 1389 1390static void EmitMemberInitializer(CodeGenFunction &CGF, 1391 const CXXRecordDecl *ClassDecl, 1392 CXXBaseOrMemberInitializer *MemberInit) { 1393 assert(MemberInit->isMemberInitializer() && 1394 "Must have member initializer!"); 1395 1396 // non-static data member initializers. 1397 FieldDecl *Field = MemberInit->getMember(); 1398 QualType FieldType = CGF.getContext().getCanonicalType((Field)->getType()); 1399 const ConstantArrayType *Array = 1400 CGF.getContext().getAsConstantArrayType(FieldType); 1401 if (Array) 1402 FieldType = CGF.getContext().getBaseElementType(FieldType); 1403 1404 llvm::Value *ThisPtr = CGF.LoadCXXThis(); 1405 LValue LHS; 1406 if (FieldType->isReferenceType()) { 1407 // FIXME: This is really ugly; should be refactored somehow 1408 unsigned idx = CGF.CGM.getTypes().getLLVMFieldNo(Field); 1409 llvm::Value *V = CGF.Builder.CreateStructGEP(ThisPtr, idx, "tmp"); 1410 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs"); 1411 LHS = LValue::MakeAddr(V, CGF.MakeQualifiers(FieldType)); 1412 } else { 1413 LHS = CGF.EmitLValueForField(ThisPtr, Field, false, 0); 1414 } 1415 if (FieldType->getAs<RecordType>()) { 1416 if (!Field->isAnonymousStructOrUnion()) { 1417 assert(MemberInit->getConstructor() && 1418 "EmitCtorPrologue - no constructor to initialize member"); 1419 if (Array) { 1420 const llvm::Type *BasePtr = CGF.ConvertType(FieldType); 1421 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1422 llvm::Value *BaseAddrPtr = 1423 CGF.Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1424 CGF.EmitCXXAggrConstructorCall(MemberInit->getConstructor(), 1425 Array, BaseAddrPtr); 1426 } 1427 else 1428 CGF.EmitCXXConstructorCall(MemberInit->getConstructor(), 1429 Ctor_Complete, LHS.getAddress(), 1430 MemberInit->const_arg_begin(), 1431 MemberInit->const_arg_end()); 1432 return; 1433 } 1434 else { 1435 // Initializing an anonymous union data member. 1436 FieldDecl *anonMember = MemberInit->getAnonUnionMember(); 1437 LHS = CGF.EmitLValueForField(LHS.getAddress(), anonMember, 1438 /*IsUnion=*/true, 0); 1439 FieldType = anonMember->getType(); 1440 } 1441 } 1442 1443 assert(MemberInit->getNumArgs() == 1 && "Initializer count must be 1 only"); 1444 Expr *RhsExpr = *MemberInit->arg_begin(); 1445 RValue RHS; 1446 if (FieldType->isReferenceType()) 1447 RHS = CGF.EmitReferenceBindingToExpr(RhsExpr, FieldType, 1448 /*IsInitializer=*/true); 1449 else if (FieldType->isMemberFunctionPointerType()) 1450 RHS = RValue::get(CGF.CGM.EmitConstantExpr(RhsExpr, FieldType, &CGF)); 1451 else 1452 RHS = RValue::get(CGF.EmitScalarExpr(RhsExpr, true)); 1453 CGF.EmitStoreThroughLValue(RHS, LHS, FieldType); 1454} 1455 1456/// EmitCtorPrologue - This routine generates necessary code to initialize 1457/// base classes and non-static data members belonging to this constructor. 1458/// FIXME: This needs to take a CXXCtorType. 1459void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD, 1460 CXXCtorType CtorType) { 1461 const CXXRecordDecl *ClassDecl = CD->getParent(); 1462 1463 // FIXME: Add vbase initialization 1464 llvm::Value *LoadOfThis = 0; 1465 1466 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(), 1467 E = CD->init_end(); 1468 B != E; ++B) { 1469 CXXBaseOrMemberInitializer *Member = (*B); 1470 1471 assert(LiveTemporaries.empty() && 1472 "Should not have any live temporaries at initializer start!"); 1473 1474 if (Member->isBaseInitializer()) 1475 EmitBaseInitializer(*this, ClassDecl, Member, CtorType); 1476 else 1477 EmitMemberInitializer(*this, ClassDecl, Member); 1478 1479 // Pop any live temporaries that the initializers might have pushed. 1480 while (!LiveTemporaries.empty()) 1481 PopCXXTemporary(); 1482 } 1483 1484 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) { 1485 // Nontrivial default constructor with no initializer list. It may still 1486 // have bases classes and/or contain non-static data members which require 1487 // construction. 1488 for (CXXRecordDecl::base_class_const_iterator Base = 1489 ClassDecl->bases_begin(); 1490 Base != ClassDecl->bases_end(); ++Base) { 1491 // FIXME. copy assignment of virtual base NYI 1492 if (Base->isVirtual()) 1493 continue; 1494 1495 CXXRecordDecl *BaseClassDecl 1496 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 1497 if (BaseClassDecl->hasTrivialConstructor()) 1498 continue; 1499 if (CXXConstructorDecl *BaseCX = 1500 BaseClassDecl->getDefaultConstructor(getContext())) { 1501 LoadOfThis = LoadCXXThis(); 1502 llvm::Value *V = GetAddressCXXOfBaseClass(LoadOfThis, ClassDecl, 1503 BaseClassDecl, 1504 /*NullCheckValue=*/false); 1505 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0); 1506 } 1507 } 1508 1509 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), 1510 FieldEnd = ClassDecl->field_end(); 1511 Field != FieldEnd; ++Field) { 1512 QualType FieldType = getContext().getCanonicalType((*Field)->getType()); 1513 const ConstantArrayType *Array = 1514 getContext().getAsConstantArrayType(FieldType); 1515 if (Array) 1516 FieldType = getContext().getBaseElementType(FieldType); 1517 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion()) 1518 continue; 1519 const RecordType *ClassRec = FieldType->getAs<RecordType>(); 1520 CXXRecordDecl *MemberClassDecl = 1521 dyn_cast<CXXRecordDecl>(ClassRec->getDecl()); 1522 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor()) 1523 continue; 1524 if (CXXConstructorDecl *MamberCX = 1525 MemberClassDecl->getDefaultConstructor(getContext())) { 1526 LoadOfThis = LoadCXXThis(); 1527 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); 1528 if (Array) { 1529 const llvm::Type *BasePtr = ConvertType(FieldType); 1530 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1531 llvm::Value *BaseAddrPtr = 1532 Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1533 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr); 1534 } 1535 else 1536 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(), 1537 0, 0); 1538 } 1539 } 1540 } 1541 1542 // Initialize the vtable pointer 1543 if (ClassDecl->isDynamicClass()) { 1544 if (!LoadOfThis) 1545 LoadOfThis = LoadCXXThis(); 1546 llvm::Value *VtableField; 1547 llvm::Type *Ptr8Ty, *PtrPtr8Ty; 1548 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); 1549 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0); 1550 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty); 1551 llvm::Value *vtable = GenerateVtable(ClassDecl); 1552 Builder.CreateStore(vtable, VtableField); 1553 } 1554} 1555 1556/// EmitDtorEpilogue - Emit all code that comes at the end of class's 1557/// destructor. This is to call destructors on members and base classes 1558/// in reverse order of their construction. 1559/// FIXME: This needs to take a CXXDtorType. 1560void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD, 1561 CXXDtorType DtorType) { 1562 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext()); 1563 assert(!ClassDecl->getNumVBases() && 1564 "FIXME: Destruction of virtual bases not supported"); 1565 (void)ClassDecl; // prevent warning. 1566 1567 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(), 1568 *E = DD->destr_end(); B != E; ++B) { 1569 uintptr_t BaseOrMember = (*B); 1570 if (DD->isMemberToDestroy(BaseOrMember)) { 1571 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember); 1572 QualType FieldType = getContext().getCanonicalType((FD)->getType()); 1573 const ConstantArrayType *Array = 1574 getContext().getAsConstantArrayType(FieldType); 1575 if (Array) 1576 FieldType = getContext().getBaseElementType(FieldType); 1577 const RecordType *RT = FieldType->getAs<RecordType>(); 1578 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); 1579 if (FieldClassDecl->hasTrivialDestructor()) 1580 continue; 1581 llvm::Value *LoadOfThis = LoadCXXThis(); 1582 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0); 1583 if (Array) { 1584 const llvm::Type *BasePtr = ConvertType(FieldType); 1585 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1586 llvm::Value *BaseAddrPtr = 1587 Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1588 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()), 1589 Array, BaseAddrPtr); 1590 } 1591 else 1592 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()), 1593 Dtor_Complete, LHS.getAddress()); 1594 } else { 1595 const RecordType *RT = 1596 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>(); 1597 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); 1598 if (BaseClassDecl->hasTrivialDestructor()) 1599 continue; 1600 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(), 1601 ClassDecl, BaseClassDecl, 1602 /*NullCheckValue=*/false); 1603 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()), 1604 DtorType, V); 1605 } 1606 } 1607 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial()) 1608 return; 1609 // Case of destructor synthesis with fields and base classes 1610 // which have non-trivial destructors. They must be destructed in 1611 // reverse order of their construction. 1612 llvm::SmallVector<FieldDecl *, 16> DestructedFields; 1613 1614 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), 1615 FieldEnd = ClassDecl->field_end(); 1616 Field != FieldEnd; ++Field) { 1617 QualType FieldType = getContext().getCanonicalType((*Field)->getType()); 1618 if (getContext().getAsConstantArrayType(FieldType)) 1619 FieldType = getContext().getBaseElementType(FieldType); 1620 if (const RecordType *RT = FieldType->getAs<RecordType>()) { 1621 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); 1622 if (FieldClassDecl->hasTrivialDestructor()) 1623 continue; 1624 DestructedFields.push_back(*Field); 1625 } 1626 } 1627 if (!DestructedFields.empty()) 1628 for (int i = DestructedFields.size() -1; i >= 0; --i) { 1629 FieldDecl *Field = DestructedFields[i]; 1630 QualType FieldType = Field->getType(); 1631 const ConstantArrayType *Array = 1632 getContext().getAsConstantArrayType(FieldType); 1633 if (Array) 1634 FieldType = getContext().getBaseElementType(FieldType); 1635 const RecordType *RT = FieldType->getAs<RecordType>(); 1636 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); 1637 llvm::Value *LoadOfThis = LoadCXXThis(); 1638 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0); 1639 if (Array) { 1640 const llvm::Type *BasePtr = ConvertType(FieldType); 1641 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1642 llvm::Value *BaseAddrPtr = 1643 Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1644 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()), 1645 Array, BaseAddrPtr); 1646 } 1647 else 1648 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()), 1649 Dtor_Complete, LHS.getAddress()); 1650 } 1651 1652 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases; 1653 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); 1654 Base != ClassDecl->bases_end(); ++Base) { 1655 // FIXME. copy assignment of virtual base NYI 1656 if (Base->isVirtual()) 1657 continue; 1658 1659 CXXRecordDecl *BaseClassDecl 1660 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 1661 if (BaseClassDecl->hasTrivialDestructor()) 1662 continue; 1663 DestructedBases.push_back(BaseClassDecl); 1664 } 1665 if (DestructedBases.empty()) 1666 return; 1667 for (int i = DestructedBases.size() -1; i >= 0; --i) { 1668 CXXRecordDecl *BaseClassDecl = DestructedBases[i]; 1669 llvm::Value *V = GetAddressCXXOfBaseClass(LoadCXXThis(), 1670 ClassDecl,BaseClassDecl, 1671 /*NullCheckValue=*/false); 1672 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()), 1673 Dtor_Complete, V); 1674 } 1675} 1676 1677void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor, 1678 CXXDtorType DtorType, 1679 llvm::Function *Fn, 1680 const FunctionArgList &Args) { 1681 1682 const CXXRecordDecl *ClassDecl = Dtor->getParent(); 1683 assert(!ClassDecl->hasUserDeclaredDestructor() && 1684 "SynthesizeDefaultDestructor - destructor has user declaration"); 1685 (void) ClassDecl; 1686 1687 StartFunction(GlobalDecl(Dtor, DtorType), Dtor->getResultType(), Fn, Args, 1688 SourceLocation()); 1689 EmitDtorEpilogue(Dtor, DtorType); 1690 FinishFunction(); 1691} 1692 1693// FIXME: Move this to CGCXXStmt.cpp 1694void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) { 1695 // FIXME: We need to do more here. 1696 EmitStmt(S.getTryBlock()); 1697} 1698