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