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