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