CGCXX.cpp revision dec025b15c63e0353768e402ad1fd566d97d2be7
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 "llvm/ADT/StringExtras.h" 25using namespace clang; 26using namespace CodeGen; 27 28void 29CodeGenFunction::EmitCXXGlobalDtorRegistration(const CXXDestructorDecl *Dtor, 30 llvm::Constant *DeclPtr) { 31 // FIXME: This is ABI dependent and we use the Itanium ABI. 32 33 const llvm::Type *Int8PtrTy = 34 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); 35 36 std::vector<const llvm::Type *> Params; 37 Params.push_back(Int8PtrTy); 38 39 // Get the destructor function type 40 const llvm::Type *DtorFnTy = 41 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false); 42 DtorFnTy = llvm::PointerType::getUnqual(DtorFnTy); 43 44 Params.clear(); 45 Params.push_back(DtorFnTy); 46 Params.push_back(Int8PtrTy); 47 Params.push_back(Int8PtrTy); 48 49 // Get the __cxa_atexit function type 50 // extern "C" int __cxa_atexit ( void (*f)(void *), void *p, void *d ); 51 const llvm::FunctionType *AtExitFnTy = 52 llvm::FunctionType::get(ConvertType(getContext().IntTy), Params, false); 53 54 llvm::Constant *AtExitFn = CGM.CreateRuntimeFunction(AtExitFnTy, 55 "__cxa_atexit"); 56 57 llvm::Constant *Handle = CGM.CreateRuntimeVariable(Int8PtrTy, 58 "__dso_handle"); 59 60 llvm::Constant *DtorFn = CGM.GetAddrOfCXXDestructor(Dtor, Dtor_Complete); 61 62 llvm::Value *Args[3] = { llvm::ConstantExpr::getBitCast(DtorFn, DtorFnTy), 63 llvm::ConstantExpr::getBitCast(DeclPtr, Int8PtrTy), 64 llvm::ConstantExpr::getBitCast(Handle, Int8PtrTy) }; 65 Builder.CreateCall(AtExitFn, &Args[0], llvm::array_endof(Args)); 66} 67 68void CodeGenFunction::EmitCXXGlobalVarDeclInit(const VarDecl &D, 69 llvm::Constant *DeclPtr) { 70 assert(D.hasGlobalStorage() && 71 "VarDecl must have global storage!"); 72 73 const Expr *Init = D.getInit(); 74 QualType T = D.getType(); 75 76 if (T->isReferenceType()) { 77 ErrorUnsupported(Init, "global variable that binds to a reference"); 78 } else if (!hasAggregateLLVMType(T)) { 79 llvm::Value *V = EmitScalarExpr(Init); 80 EmitStoreOfScalar(V, DeclPtr, T.isVolatileQualified(), T); 81 } else if (T->isAnyComplexType()) { 82 EmitComplexExprIntoAddr(Init, DeclPtr, T.isVolatileQualified()); 83 } else { 84 EmitAggExpr(Init, DeclPtr, T.isVolatileQualified()); 85 86 if (const RecordType *RT = T->getAs<RecordType>()) { 87 CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 88 if (!RD->hasTrivialDestructor()) 89 EmitCXXGlobalDtorRegistration(RD->getDestructor(getContext()), DeclPtr); 90 } 91 } 92} 93 94void 95CodeGenModule::EmitCXXGlobalInitFunc() { 96 if (CXXGlobalInits.empty()) 97 return; 98 99 const llvm::FunctionType *FTy = llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), 100 false); 101 102 // Create our global initialization function. 103 // FIXME: Should this be tweakable by targets? 104 llvm::Function *Fn = 105 llvm::Function::Create(FTy, llvm::GlobalValue::InternalLinkage, 106 "__cxx_global_initialization", &TheModule); 107 108 CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, 109 &CXXGlobalInits[0], 110 CXXGlobalInits.size()); 111 AddGlobalCtor(Fn); 112} 113 114void CodeGenFunction::GenerateCXXGlobalInitFunc(llvm::Function *Fn, 115 const VarDecl **Decls, 116 unsigned NumDecls) { 117 StartFunction(0, getContext().VoidTy, Fn, FunctionArgList(), 118 SourceLocation()); 119 120 for (unsigned i = 0; i != NumDecls; ++i) { 121 const VarDecl *D = Decls[i]; 122 123 llvm::Constant *DeclPtr = CGM.GetAddrOfGlobalVar(D); 124 EmitCXXGlobalVarDeclInit(*D, DeclPtr); 125 } 126 FinishFunction(); 127} 128 129void 130CodeGenFunction::EmitStaticCXXBlockVarDeclInit(const VarDecl &D, 131 llvm::GlobalVariable *GV) { 132 // FIXME: This should use __cxa_guard_{acquire,release}? 133 134 assert(!getContext().getLangOptions().ThreadsafeStatics && 135 "thread safe statics are currently not supported!"); 136 137 llvm::SmallString<256> GuardVName; 138 llvm::raw_svector_ostream GuardVOut(GuardVName); 139 mangleGuardVariable(&D, getContext(), GuardVOut); 140 141 // Create the guard variable. 142 llvm::GlobalValue *GuardV = 143 new llvm::GlobalVariable(CGM.getModule(), llvm::Type::getInt64Ty(VMContext), false, 144 GV->getLinkage(), 145 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)), 146 GuardVName.str()); 147 148 // Load the first byte of the guard variable. 149 const llvm::Type *PtrTy = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); 150 llvm::Value *V = Builder.CreateLoad(Builder.CreateBitCast(GuardV, PtrTy), 151 "tmp"); 152 153 // Compare it against 0. 154 llvm::Value *nullValue = llvm::Constant::getNullValue(llvm::Type::getInt8Ty(VMContext)); 155 llvm::Value *ICmp = Builder.CreateICmpEQ(V, nullValue , "tobool"); 156 157 llvm::BasicBlock *InitBlock = createBasicBlock("init"); 158 llvm::BasicBlock *EndBlock = createBasicBlock("init.end"); 159 160 // If the guard variable is 0, jump to the initializer code. 161 Builder.CreateCondBr(ICmp, InitBlock, EndBlock); 162 163 EmitBlock(InitBlock); 164 165 EmitCXXGlobalVarDeclInit(D, GV); 166 167 Builder.CreateStore(llvm::ConstantInt::get(llvm::Type::getInt8Ty(VMContext), 1), 168 Builder.CreateBitCast(GuardV, PtrTy)); 169 170 EmitBlock(EndBlock); 171} 172 173RValue CodeGenFunction::EmitCXXMemberCall(const CXXMethodDecl *MD, 174 llvm::Value *Callee, 175 llvm::Value *This, 176 CallExpr::const_arg_iterator ArgBeg, 177 CallExpr::const_arg_iterator ArgEnd) { 178 assert(MD->isInstance() && 179 "Trying to emit a member call expr on a static method!"); 180 181 // A call to a trivial destructor requires no code generation. 182 if (const CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(MD)) 183 if (Destructor->isTrivial()) 184 return RValue::get(0); 185 186 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType(); 187 188 CallArgList Args; 189 190 // Push the this ptr. 191 Args.push_back(std::make_pair(RValue::get(This), 192 MD->getThisType(getContext()))); 193 194 // And the rest of the call args 195 EmitCallArgs(Args, FPT, ArgBeg, ArgEnd); 196 197 QualType ResultType = MD->getType()->getAsFunctionType()->getResultType(); 198 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args), 199 Callee, Args, MD); 200} 201 202RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE) { 203 const MemberExpr *ME = cast<MemberExpr>(CE->getCallee()); 204 const CXXMethodDecl *MD = cast<CXXMethodDecl>(ME->getMemberDecl()); 205 206 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType(); 207 208 const llvm::Type *Ty = 209 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), 210 FPT->isVariadic()); 211 llvm::Value *This; 212 213 if (ME->isArrow()) 214 This = EmitScalarExpr(ME->getBase()); 215 else { 216 LValue BaseLV = EmitLValue(ME->getBase()); 217 This = BaseLV.getAddress(); 218 } 219 220 // C++ [class.virtual]p12: 221 // Explicit qualification with the scope operator (5.1) suppresses the 222 // virtual call mechanism. 223 llvm::Value *Callee; 224 if (MD->isVirtual() && !ME->hasQualifier()) 225 Callee = BuildVirtualCall(MD, This, Ty); 226 else if (const CXXDestructorDecl *Destructor 227 = dyn_cast<CXXDestructorDecl>(MD)) 228 Callee = CGM.GetAddrOfFunction(GlobalDecl(Destructor, Dtor_Complete), Ty); 229 else 230 Callee = CGM.GetAddrOfFunction(GlobalDecl(MD), Ty); 231 232 return EmitCXXMemberCall(MD, Callee, This, 233 CE->arg_begin(), CE->arg_end()); 234} 235 236RValue 237CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, 238 const CXXMethodDecl *MD) { 239 assert(MD->isInstance() && 240 "Trying to emit a member call expr on a static method!"); 241 242 if (MD->isCopyAssignment()) { 243 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext()); 244 if (ClassDecl->hasTrivialCopyAssignment()) { 245 assert(!ClassDecl->hasUserDeclaredCopyAssignment() && 246 "EmitCXXOperatorMemberCallExpr - user declared copy assignment"); 247 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress(); 248 llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress(); 249 QualType Ty = E->getType(); 250 EmitAggregateCopy(This, Src, Ty); 251 return RValue::get(This); 252 } 253 } 254 255 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType(); 256 const llvm::Type *Ty = 257 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), 258 FPT->isVariadic()); 259 llvm::Constant *Callee = CGM.GetAddrOfFunction(GlobalDecl(MD), Ty); 260 261 llvm::Value *This = EmitLValue(E->getArg(0)).getAddress(); 262 263 return EmitCXXMemberCall(MD, Callee, This, 264 E->arg_begin() + 1, E->arg_end()); 265} 266 267RValue 268CodeGenFunction::EmitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *E) { 269 assert((E->getCastKind() == CastExpr::CK_UserDefinedConversion) && 270 "EmitCXXFunctionalCastExpr - called with wrong cast"); 271 272 CXXMethodDecl *MD = E->getTypeConversionMethod(); 273 assert(MD && "EmitCXXFunctionalCastExpr - null conversion method"); 274 assert(isa<CXXConversionDecl>(MD) && "EmitCXXFunctionalCastExpr - not" 275 " method decl"); 276 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType(); 277 278 const llvm::Type *Ty = 279 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), 280 FPT->isVariadic()); 281 llvm::Constant *Callee = CGM.GetAddrOfFunction(GlobalDecl(MD), Ty); 282 llvm::Value *This = EmitLValue(E->getSubExpr()).getAddress(); 283 RValue RV = EmitCXXMemberCall(MD, Callee, This, 0, 0); 284 if (RV.isAggregate()) 285 RV = RValue::get(RV.getAggregateAddr()); 286 return RV; 287} 288 289llvm::Value *CodeGenFunction::LoadCXXThis() { 290 assert(isa<CXXMethodDecl>(CurFuncDecl) && 291 "Must be in a C++ member function decl to load 'this'"); 292 assert(cast<CXXMethodDecl>(CurFuncDecl)->isInstance() && 293 "Must be in a C++ member function decl to load 'this'"); 294 295 // FIXME: What if we're inside a block? 296 // ans: See how CodeGenFunction::LoadObjCSelf() uses 297 // CodeGenFunction::BlockForwardSelf() for how to do this. 298 return Builder.CreateLoad(LocalDeclMap[CXXThisDecl], "this"); 299} 300 301static bool 302GetNestedPaths(llvm::SmallVectorImpl<const CXXRecordDecl *> &NestedBasePaths, 303 const CXXRecordDecl *ClassDecl, 304 const CXXRecordDecl *BaseClassDecl) { 305 for (CXXRecordDecl::base_class_const_iterator i = ClassDecl->bases_begin(), 306 e = ClassDecl->bases_end(); i != e; ++i) { 307 if (i->isVirtual()) 308 continue; 309 const CXXRecordDecl *Base = 310 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); 311 if (Base == BaseClassDecl) { 312 NestedBasePaths.push_back(BaseClassDecl); 313 return true; 314 } 315 } 316 // BaseClassDecl not an immediate base of ClassDecl. 317 for (CXXRecordDecl::base_class_const_iterator i = ClassDecl->bases_begin(), 318 e = ClassDecl->bases_end(); i != e; ++i) { 319 if (i->isVirtual()) 320 continue; 321 const CXXRecordDecl *Base = 322 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); 323 if (GetNestedPaths(NestedBasePaths, Base, BaseClassDecl)) { 324 NestedBasePaths.push_back(Base); 325 return true; 326 } 327 } 328 return false; 329} 330 331llvm::Value *CodeGenFunction::AddressCXXOfBaseClass(llvm::Value *BaseValue, 332 const CXXRecordDecl *ClassDecl, 333 const CXXRecordDecl *BaseClassDecl) { 334 if (ClassDecl == BaseClassDecl) 335 return BaseValue; 336 337 llvm::Type *I8Ptr = llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); 338 llvm::SmallVector<const CXXRecordDecl *, 16> NestedBasePaths; 339 GetNestedPaths(NestedBasePaths, ClassDecl, BaseClassDecl); 340 assert(NestedBasePaths.size() > 0 && 341 "AddressCXXOfBaseClass - inheritence path failed"); 342 NestedBasePaths.push_back(ClassDecl); 343 uint64_t Offset = 0; 344 345 // Accessing a member of the base class. Must add delata to 346 // the load of 'this'. 347 for (unsigned i = NestedBasePaths.size()-1; i > 0; i--) { 348 const CXXRecordDecl *DerivedClass = NestedBasePaths[i]; 349 const CXXRecordDecl *BaseClass = NestedBasePaths[i-1]; 350 const ASTRecordLayout &Layout = 351 getContext().getASTRecordLayout(DerivedClass); 352 Offset += Layout.getBaseClassOffset(BaseClass) / 8; 353 } 354 llvm::Value *OffsetVal = 355 llvm::ConstantInt::get( 356 CGM.getTypes().ConvertType(CGM.getContext().LongTy), Offset); 357 BaseValue = Builder.CreateBitCast(BaseValue, I8Ptr); 358 BaseValue = Builder.CreateGEP(BaseValue, OffsetVal, "add.ptr"); 359 QualType BTy = 360 getContext().getCanonicalType( 361 getContext().getTypeDeclType(const_cast<CXXRecordDecl*>(BaseClassDecl))); 362 const llvm::Type *BasePtr = ConvertType(BTy); 363 BasePtr = llvm::PointerType::getUnqual(BasePtr); 364 BaseValue = Builder.CreateBitCast(BaseValue, BasePtr); 365 return BaseValue; 366} 367 368/// EmitCXXAggrConstructorCall - This routine essentially creates a (nested) 369/// for-loop to call the default constructor on individual members of the 370/// array. 'Array' is the array type, 'This' is llvm pointer of the start 371/// of the array and 'D' is the default costructor Decl for elements of the 372/// array. It is assumed that all relevant checks have been made by the 373/// caller. 374void 375CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *D, 376 const ArrayType *Array, 377 llvm::Value *This) { 378 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); 379 assert(CA && "Do we support VLA for construction ?"); 380 381 // Create a temporary for the loop index and initialize it with 0. 382 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext), 383 "loop.index"); 384 llvm::Value* zeroConstant = 385 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)); 386 Builder.CreateStore(zeroConstant, IndexPtr, false); 387 388 // Start the loop with a block that tests the condition. 389 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); 390 llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); 391 392 EmitBlock(CondBlock); 393 394 llvm::BasicBlock *ForBody = createBasicBlock("for.body"); 395 396 // Generate: if (loop-index < number-of-elements fall to the loop body, 397 // otherwise, go to the block after the for-loop. 398 uint64_t NumElements = getContext().getConstantArrayElementCount(CA); 399 llvm::Value * NumElementsPtr = 400 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements); 401 llvm::Value *Counter = Builder.CreateLoad(IndexPtr); 402 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr, 403 "isless"); 404 // If the condition is true, execute the body. 405 Builder.CreateCondBr(IsLess, ForBody, AfterFor); 406 407 EmitBlock(ForBody); 408 409 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); 410 // Inside the loop body, emit the constructor call on the array element. 411 Counter = Builder.CreateLoad(IndexPtr); 412 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx"); 413 EmitCXXConstructorCall(D, Ctor_Complete, Address, 0, 0); 414 415 EmitBlock(ContinueBlock); 416 417 // Emit the increment of the loop counter. 418 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); 419 Counter = Builder.CreateLoad(IndexPtr); 420 NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); 421 Builder.CreateStore(NextVal, IndexPtr, false); 422 423 // Finally, branch back up to the condition for the next iteration. 424 EmitBranch(CondBlock); 425 426 // Emit the fall-through block. 427 EmitBlock(AfterFor, true); 428} 429 430/// EmitCXXAggrDestructorCall - calls the default destructor on array 431/// elements in reverse order of construction. 432void 433CodeGenFunction::EmitCXXAggrDestructorCall(const CXXDestructorDecl *D, 434 const ArrayType *Array, 435 llvm::Value *This) { 436 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); 437 assert(CA && "Do we support VLA for destruction ?"); 438 llvm::Value *One = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), 439 1); 440 uint64_t ElementCount = getContext().getConstantArrayElementCount(CA); 441 // Create a temporary for the loop index and initialize it with count of 442 // array elements. 443 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext), 444 "loop.index"); 445 // Index = ElementCount; 446 llvm::Value* UpperCount = 447 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), ElementCount); 448 Builder.CreateStore(UpperCount, IndexPtr, false); 449 450 // Start the loop with a block that tests the condition. 451 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); 452 llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); 453 454 EmitBlock(CondBlock); 455 456 llvm::BasicBlock *ForBody = createBasicBlock("for.body"); 457 458 // Generate: if (loop-index != 0 fall to the loop body, 459 // otherwise, go to the block after the for-loop. 460 llvm::Value* zeroConstant = 461 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)); 462 llvm::Value *Counter = Builder.CreateLoad(IndexPtr); 463 llvm::Value *IsNE = Builder.CreateICmpNE(Counter, zeroConstant, 464 "isne"); 465 // If the condition is true, execute the body. 466 Builder.CreateCondBr(IsNE, ForBody, AfterFor); 467 468 EmitBlock(ForBody); 469 470 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); 471 // Inside the loop body, emit the constructor call on the array element. 472 Counter = Builder.CreateLoad(IndexPtr); 473 Counter = Builder.CreateSub(Counter, One); 474 llvm::Value *Address = Builder.CreateInBoundsGEP(This, Counter, "arrayidx"); 475 EmitCXXDestructorCall(D, Dtor_Complete, Address); 476 477 EmitBlock(ContinueBlock); 478 479 // Emit the decrement of the loop counter. 480 Counter = Builder.CreateLoad(IndexPtr); 481 Counter = Builder.CreateSub(Counter, One, "dec"); 482 Builder.CreateStore(Counter, IndexPtr, false); 483 484 // Finally, branch back up to the condition for the next iteration. 485 EmitBranch(CondBlock); 486 487 // Emit the fall-through block. 488 EmitBlock(AfterFor, true); 489} 490 491void 492CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, 493 CXXCtorType Type, 494 llvm::Value *This, 495 CallExpr::const_arg_iterator ArgBeg, 496 CallExpr::const_arg_iterator ArgEnd) { 497 if (D->isCopyConstructor(getContext())) { 498 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(D->getDeclContext()); 499 if (ClassDecl->hasTrivialCopyConstructor()) { 500 assert(!ClassDecl->hasUserDeclaredCopyConstructor() && 501 "EmitCXXConstructorCall - user declared copy constructor"); 502 const Expr *E = (*ArgBeg); 503 QualType Ty = E->getType(); 504 llvm::Value *Src = EmitLValue(E).getAddress(); 505 EmitAggregateCopy(This, Src, Ty); 506 return; 507 } 508 } 509 510 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(D, Type); 511 512 EmitCXXMemberCall(D, Callee, This, ArgBeg, ArgEnd); 513} 514 515void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *D, 516 CXXDtorType Type, 517 llvm::Value *This) { 518 llvm::Value *Callee = CGM.GetAddrOfCXXDestructor(D, Type); 519 520 EmitCXXMemberCall(D, Callee, This, 0, 0); 521} 522 523void 524CodeGenFunction::EmitCXXConstructExpr(llvm::Value *Dest, 525 const CXXConstructExpr *E) { 526 assert(Dest && "Must have a destination!"); 527 528 const CXXRecordDecl *RD = 529 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl()); 530 if (RD->hasTrivialConstructor()) 531 return; 532 533 // Code gen optimization to eliminate copy constructor and return 534 // its first argument instead. 535 if (getContext().getLangOptions().ElideConstructors && E->isElidable()) { 536 CXXConstructExpr::const_arg_iterator i = E->arg_begin(); 537 EmitAggExpr((*i), Dest, false); 538 return; 539 } 540 // Call the constructor. 541 EmitCXXConstructorCall(E->getConstructor(), Ctor_Complete, Dest, 542 E->arg_begin(), E->arg_end()); 543} 544 545llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { 546 if (E->isArray()) { 547 ErrorUnsupported(E, "new[] expression"); 548 return llvm::UndefValue::get(ConvertType(E->getType())); 549 } 550 551 QualType AllocType = E->getAllocatedType(); 552 FunctionDecl *NewFD = E->getOperatorNew(); 553 const FunctionProtoType *NewFTy = NewFD->getType()->getAsFunctionProtoType(); 554 555 CallArgList NewArgs; 556 557 // The allocation size is the first argument. 558 QualType SizeTy = getContext().getSizeType(); 559 llvm::Value *AllocSize = 560 llvm::ConstantInt::get(ConvertType(SizeTy), 561 getContext().getTypeSize(AllocType) / 8); 562 563 NewArgs.push_back(std::make_pair(RValue::get(AllocSize), SizeTy)); 564 565 // Emit the rest of the arguments. 566 // FIXME: Ideally, this should just use EmitCallArgs. 567 CXXNewExpr::const_arg_iterator NewArg = E->placement_arg_begin(); 568 569 // First, use the types from the function type. 570 // We start at 1 here because the first argument (the allocation size) 571 // has already been emitted. 572 for (unsigned i = 1, e = NewFTy->getNumArgs(); i != e; ++i, ++NewArg) { 573 QualType ArgType = NewFTy->getArgType(i); 574 575 assert(getContext().getCanonicalType(ArgType.getNonReferenceType()). 576 getTypePtr() == 577 getContext().getCanonicalType(NewArg->getType()).getTypePtr() && 578 "type mismatch in call argument!"); 579 580 NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType), 581 ArgType)); 582 583 } 584 585 // Either we've emitted all the call args, or we have a call to a 586 // variadic function. 587 assert((NewArg == E->placement_arg_end() || NewFTy->isVariadic()) && 588 "Extra arguments in non-variadic function!"); 589 590 // If we still have any arguments, emit them using the type of the argument. 591 for (CXXNewExpr::const_arg_iterator NewArgEnd = E->placement_arg_end(); 592 NewArg != NewArgEnd; ++NewArg) { 593 QualType ArgType = NewArg->getType(); 594 NewArgs.push_back(std::make_pair(EmitCallArg(*NewArg, ArgType), 595 ArgType)); 596 } 597 598 // Emit the call to new. 599 RValue RV = 600 EmitCall(CGM.getTypes().getFunctionInfo(NewFTy->getResultType(), NewArgs), 601 CGM.GetAddrOfFunction(GlobalDecl(NewFD)), 602 NewArgs, NewFD); 603 604 // If an allocation function is declared with an empty exception specification 605 // it returns null to indicate failure to allocate storage. [expr.new]p13. 606 // (We don't need to check for null when there's no new initializer and 607 // we're allocating a POD type). 608 bool NullCheckResult = NewFTy->hasEmptyExceptionSpec() && 609 !(AllocType->isPODType() && !E->hasInitializer()); 610 611 llvm::BasicBlock *NewNull = 0; 612 llvm::BasicBlock *NewNotNull = 0; 613 llvm::BasicBlock *NewEnd = 0; 614 615 llvm::Value *NewPtr = RV.getScalarVal(); 616 617 if (NullCheckResult) { 618 NewNull = createBasicBlock("new.null"); 619 NewNotNull = createBasicBlock("new.notnull"); 620 NewEnd = createBasicBlock("new.end"); 621 622 llvm::Value *IsNull = 623 Builder.CreateICmpEQ(NewPtr, 624 llvm::Constant::getNullValue(NewPtr->getType()), 625 "isnull"); 626 627 Builder.CreateCondBr(IsNull, NewNull, NewNotNull); 628 EmitBlock(NewNotNull); 629 } 630 631 NewPtr = Builder.CreateBitCast(NewPtr, ConvertType(E->getType())); 632 633 if (AllocType->isPODType()) { 634 if (E->getNumConstructorArgs() > 0) { 635 assert(E->getNumConstructorArgs() == 1 && 636 "Can only have one argument to initializer of POD type."); 637 638 const Expr *Init = E->getConstructorArg(0); 639 640 if (!hasAggregateLLVMType(AllocType)) 641 Builder.CreateStore(EmitScalarExpr(Init), NewPtr); 642 else if (AllocType->isAnyComplexType()) 643 EmitComplexExprIntoAddr(Init, NewPtr, AllocType.isVolatileQualified()); 644 else 645 EmitAggExpr(Init, NewPtr, AllocType.isVolatileQualified()); 646 } 647 } else { 648 // Call the constructor. 649 CXXConstructorDecl *Ctor = E->getConstructor(); 650 651 EmitCXXConstructorCall(Ctor, Ctor_Complete, NewPtr, 652 E->constructor_arg_begin(), 653 E->constructor_arg_end()); 654 } 655 656 if (NullCheckResult) { 657 Builder.CreateBr(NewEnd); 658 EmitBlock(NewNull); 659 Builder.CreateBr(NewEnd); 660 EmitBlock(NewEnd); 661 662 llvm::PHINode *PHI = Builder.CreatePHI(NewPtr->getType()); 663 PHI->reserveOperandSpace(2); 664 PHI->addIncoming(NewPtr, NewNotNull); 665 PHI->addIncoming(llvm::Constant::getNullValue(NewPtr->getType()), NewNull); 666 667 NewPtr = PHI; 668 } 669 670 return NewPtr; 671} 672 673void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) { 674 if (E->isArrayForm()) { 675 ErrorUnsupported(E, "delete[] expression"); 676 return; 677 }; 678 679 QualType DeleteTy = 680 E->getArgument()->getType()->getAs<PointerType>()->getPointeeType(); 681 682 llvm::Value *Ptr = EmitScalarExpr(E->getArgument()); 683 684 // Null check the pointer. 685 llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull"); 686 llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end"); 687 688 llvm::Value *IsNull = 689 Builder.CreateICmpEQ(Ptr, llvm::Constant::getNullValue(Ptr->getType()), 690 "isnull"); 691 692 Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull); 693 EmitBlock(DeleteNotNull); 694 695 // Call the destructor if necessary. 696 if (const RecordType *RT = DeleteTy->getAs<RecordType>()) { 697 if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) { 698 if (!RD->hasTrivialDestructor()) { 699 const CXXDestructorDecl *Dtor = RD->getDestructor(getContext()); 700 if (Dtor->isVirtual()) { 701 ErrorUnsupported(E, "delete expression with virtual destructor"); 702 return; 703 } 704 705 EmitCXXDestructorCall(Dtor, Dtor_Complete, Ptr); 706 } 707 } 708 } 709 710 // Call delete. 711 FunctionDecl *DeleteFD = E->getOperatorDelete(); 712 const FunctionProtoType *DeleteFTy = 713 DeleteFD->getType()->getAsFunctionProtoType(); 714 715 CallArgList DeleteArgs; 716 717 QualType ArgTy = DeleteFTy->getArgType(0); 718 llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy)); 719 DeleteArgs.push_back(std::make_pair(RValue::get(DeletePtr), ArgTy)); 720 721 // Emit the call to delete. 722 EmitCall(CGM.getTypes().getFunctionInfo(DeleteFTy->getResultType(), 723 DeleteArgs), 724 CGM.GetAddrOfFunction(GlobalDecl(DeleteFD)), 725 DeleteArgs, DeleteFD); 726 727 EmitBlock(DeleteEnd); 728} 729 730void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) { 731 EmitGlobal(GlobalDecl(D, Ctor_Complete)); 732 EmitGlobal(GlobalDecl(D, Ctor_Base)); 733} 734 735void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D, 736 CXXCtorType Type) { 737 738 llvm::Function *Fn = GetAddrOfCXXConstructor(D, Type); 739 740 CodeGenFunction(*this).GenerateCode(D, Fn); 741 742 SetFunctionDefinitionAttributes(D, Fn); 743 SetLLVMFunctionAttributesForDefinition(D, Fn); 744} 745 746llvm::Function * 747CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D, 748 CXXCtorType Type) { 749 const llvm::FunctionType *FTy = 750 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false); 751 752 const char *Name = getMangledCXXCtorName(D, Type); 753 return cast<llvm::Function>( 754 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type))); 755} 756 757const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D, 758 CXXCtorType Type) { 759 llvm::SmallString<256> Name; 760 llvm::raw_svector_ostream Out(Name); 761 mangleCXXCtor(D, Type, Context, Out); 762 763 Name += '\0'; 764 return UniqueMangledName(Name.begin(), Name.end()); 765} 766 767void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) { 768 EmitCXXDestructor(D, Dtor_Complete); 769 EmitCXXDestructor(D, Dtor_Base); 770} 771 772void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D, 773 CXXDtorType Type) { 774 llvm::Function *Fn = GetAddrOfCXXDestructor(D, Type); 775 776 CodeGenFunction(*this).GenerateCode(D, Fn); 777 778 SetFunctionDefinitionAttributes(D, Fn); 779 SetLLVMFunctionAttributesForDefinition(D, Fn); 780} 781 782llvm::Function * 783CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D, 784 CXXDtorType Type) { 785 const llvm::FunctionType *FTy = 786 getTypes().GetFunctionType(getTypes().getFunctionInfo(D), false); 787 788 const char *Name = getMangledCXXDtorName(D, Type); 789 return cast<llvm::Function>( 790 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type))); 791} 792 793const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D, 794 CXXDtorType Type) { 795 llvm::SmallString<256> Name; 796 llvm::raw_svector_ostream Out(Name); 797 mangleCXXDtor(D, Type, Context, Out); 798 799 Name += '\0'; 800 return UniqueMangledName(Name.begin(), Name.end()); 801} 802 803llvm::Constant *CodeGenModule::GenerateRtti(const CXXRecordDecl *RD) { 804 llvm::Type *Ptr8Ty; 805 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); 806 llvm::Constant *Rtti = llvm::Constant::getNullValue(Ptr8Ty); 807 808 if (!getContext().getLangOptions().Rtti) 809 return Rtti; 810 811 llvm::SmallString<256> OutName; 812 llvm::raw_svector_ostream Out(OutName); 813 QualType ClassTy; 814 ClassTy = getContext().getTagDeclType(RD); 815 mangleCXXRtti(ClassTy, getContext(), Out); 816 llvm::GlobalVariable::LinkageTypes linktype; 817 linktype = llvm::GlobalValue::WeakAnyLinkage; 818 std::vector<llvm::Constant *> info; 819 // assert(0 && "FIXME: implement rtti descriptor"); 820 // FIXME: descriptor 821 info.push_back(llvm::Constant::getNullValue(Ptr8Ty)); 822 // assert(0 && "FIXME: implement rtti ts"); 823 // FIXME: TS 824 info.push_back(llvm::Constant::getNullValue(Ptr8Ty)); 825 826 llvm::Constant *C; 827 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, info.size()); 828 C = llvm::ConstantArray::get(type, info); 829 Rtti = new llvm::GlobalVariable(getModule(), type, true, linktype, C, 830 Out.str()); 831 Rtti = llvm::ConstantExpr::getBitCast(Rtti, Ptr8Ty); 832 return Rtti; 833} 834 835class VtableBuilder { 836public: 837 /// Index_t - Vtable index type. 838 typedef uint64_t Index_t; 839private: 840 std::vector<llvm::Constant *> &methods; 841 std::vector<llvm::Constant *> submethods; 842 llvm::Type *Ptr8Ty; 843 /// Class - The most derived class that this vtable is being built for. 844 const CXXRecordDecl *Class; 845 /// BLayout - Layout for the most derived class that this vtable is being 846 /// built for. 847 const ASTRecordLayout &BLayout; 848 llvm::SmallSet<const CXXRecordDecl *, 32> IndirectPrimary; 849 llvm::SmallSet<const CXXRecordDecl *, 32> SeenVBase; 850 llvm::Constant *rtti; 851 llvm::LLVMContext &VMContext; 852 CodeGenModule &CGM; // Per-module state. 853 /// Index - Maps a method decl into a vtable index. Useful for virtual 854 /// dispatch codegen. 855 llvm::DenseMap<const CXXMethodDecl *, Index_t> Index; 856 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCall; 857 llvm::DenseMap<const CXXMethodDecl *, Index_t> VCallOffset; 858 typedef llvm::DenseMap<const CXXMethodDecl *, 859 std::pair<Index_t, Index_t> > Thunks_t; 860 Thunks_t Thunks; 861 std::vector<Index_t> VCalls; 862 typedef CXXRecordDecl::method_iterator method_iter; 863 // FIXME: Linkage should follow vtable 864 const bool Extern; 865 const uint32_t LLVMPointerWidth; 866 Index_t extra; 867public: 868 VtableBuilder(std::vector<llvm::Constant *> &meth, 869 const CXXRecordDecl *c, 870 CodeGenModule &cgm) 871 : methods(meth), Class(c), BLayout(cgm.getContext().getASTRecordLayout(c)), 872 rtti(cgm.GenerateRtti(c)), VMContext(cgm.getModule().getContext()), 873 CGM(cgm), Extern(true), 874 LLVMPointerWidth(cgm.getContext().Target.getPointerWidth(0)) { 875 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); 876 } 877 878 llvm::DenseMap<const CXXMethodDecl *, Index_t> &getIndex() { return Index; } 879 880 llvm::Constant *wrap(Index_t i) { 881 llvm::Constant *m; 882 m = llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), i); 883 return llvm::ConstantExpr::getIntToPtr(m, Ptr8Ty); 884 } 885 886 llvm::Constant *wrap(llvm::Constant *m) { 887 return llvm::ConstantExpr::getBitCast(m, Ptr8Ty); 888 } 889 890 void GenerateVBaseOffsets(std::vector<llvm::Constant *> &offsets, 891 const CXXRecordDecl *RD, uint64_t Offset) { 892 for (CXXRecordDecl::base_class_const_iterator i =RD->bases_begin(), 893 e = RD->bases_end(); i != e; ++i) { 894 const CXXRecordDecl *Base = 895 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); 896 if (i->isVirtual() && !SeenVBase.count(Base)) { 897 SeenVBase.insert(Base); 898 int64_t BaseOffset = -(Offset/8) + BLayout.getVBaseClassOffset(Base)/8; 899 llvm::Constant *m = wrap(BaseOffset); 900 m = wrap((0?700:0) + BaseOffset); 901 offsets.push_back(m); 902 } 903 GenerateVBaseOffsets(offsets, Base, Offset); 904 } 905 } 906 907 void StartNewTable() { 908 SeenVBase.clear(); 909 } 910 911 bool OverrideMethod(const CXXMethodDecl *MD, llvm::Constant *m, 912 bool MorallyVirtual, Index_t Offset) { 913 typedef CXXMethodDecl::method_iterator meth_iter; 914 915 // FIXME: Don't like the nested loops. For very large inheritance 916 // heirarchies we could have a table on the side with the final overridder 917 // and just replace each instance of an overridden method once. Would be 918 // nice to measure the cost/benefit on real code. 919 920 for (meth_iter mi = MD->begin_overridden_methods(), 921 e = MD->end_overridden_methods(); 922 mi != e; ++mi) { 923 const CXXMethodDecl *OMD = *mi; 924 llvm::Constant *om; 925 om = CGM.GetAddrOfFunction(GlobalDecl(OMD), Ptr8Ty); 926 om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty); 927 928 for (Index_t i = 0, e = submethods.size(); 929 i != e; ++i) { 930 // FIXME: begin_overridden_methods might be too lax, covariance */ 931 if (submethods[i] != om) 932 continue; 933 Index[MD] = i; 934 submethods[i] = m; 935 936 Thunks.erase(OMD); 937 if (MorallyVirtual) { 938 Index_t &idx = VCall[OMD]; 939 if (idx == 0) { 940 VCallOffset[MD] = Offset/8; 941 idx = VCalls.size()+1; 942 VCalls.push_back(0); 943 } else { 944 VCallOffset[MD] = VCallOffset[OMD]; 945 VCalls[idx-1] = -VCallOffset[OMD] + Offset/8; 946 } 947 VCall[MD] = idx; 948 // FIXME: 0? 949 Thunks[MD] = std::make_pair(0, -((idx+extra+2)*LLVMPointerWidth/8)); 950 return true; 951 } 952#if 0 953 // FIXME: finish off 954 int64_t O = VCallOffset[OMD] - Offset/8; 955 if (O) { 956 Thunks[MD] = std::make_pair(O, 0); 957 } 958#endif 959 return true; 960 } 961 } 962 963 return false; 964 } 965 966 void InstallThunks() { 967 for (Thunks_t::iterator i = Thunks.begin(), e = Thunks.end(); 968 i != e; ++i) { 969 const CXXMethodDecl *MD = i->first; 970 Index_t idx = Index[MD]; 971 Index_t nv_O = i->second.first; 972 Index_t v_O = i->second.second; 973 submethods[idx] = CGM.BuildThunk(MD, Extern, nv_O, v_O); 974 } 975 Thunks.clear(); 976 } 977 978 void OverrideMethods(std::vector<std::pair<const CXXRecordDecl *, 979 int64_t> > *Path, bool MorallyVirtual) { 980 for (std::vector<std::pair<const CXXRecordDecl *, 981 int64_t> >::reverse_iterator i =Path->rbegin(), 982 e = Path->rend(); i != e; ++i) { 983 const CXXRecordDecl *RD = i->first; 984 int64_t Offset = i->second; 985 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me; 986 ++mi) 987 if (mi->isVirtual()) { 988 const CXXMethodDecl *MD = *mi; 989 llvm::Constant *m = wrap(CGM.GetAddrOfFunction(GlobalDecl(MD), 990 Ptr8Ty)); 991 OverrideMethod(MD, m, MorallyVirtual, Offset); 992 } 993 } 994 } 995 996 void AddMethod(const CXXMethodDecl *MD, bool MorallyVirtual, Index_t Offset) { 997 llvm::Constant *m = wrap(CGM.GetAddrOfFunction(GlobalDecl(MD), Ptr8Ty)); 998 // If we can find a previously allocated slot for this, reuse it. 999 if (OverrideMethod(MD, m, MorallyVirtual, Offset)) 1000 return; 1001 1002 // else allocate a new slot. 1003 Index[MD] = submethods.size(); 1004 submethods.push_back(m); 1005 if (MorallyVirtual) { 1006 VCallOffset[MD] = Offset/8; 1007 Index_t &idx = VCall[MD]; 1008 // Allocate the first one, after that, we reuse the previous one. 1009 if (idx == 0) { 1010 idx = VCalls.size()+1; 1011 VCalls.push_back(0); 1012 } 1013 } 1014 } 1015 1016 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual, 1017 Index_t Offset) { 1018 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me; 1019 ++mi) 1020 if (mi->isVirtual()) 1021 AddMethod(*mi, MorallyVirtual, Offset); 1022 } 1023 1024 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout, 1025 const CXXRecordDecl *PrimaryBase, 1026 bool PrimaryBaseWasVirtual, bool MorallyVirtual, 1027 int64_t Offset) { 1028 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), 1029 e = RD->bases_end(); i != e; ++i) { 1030 if (i->isVirtual()) 1031 continue; 1032 const CXXRecordDecl *Base = 1033 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); 1034 if (Base != PrimaryBase || PrimaryBaseWasVirtual) { 1035 uint64_t o = Offset + Layout.getBaseClassOffset(Base); 1036 StartNewTable(); 1037 std::vector<std::pair<const CXXRecordDecl *, 1038 int64_t> > S; 1039 S.push_back(std::make_pair(RD, Offset)); 1040 GenerateVtableForBase(Base, MorallyVirtual, o, false, &S); 1041 } 1042 } 1043 } 1044 1045 Index_t end(const CXXRecordDecl *RD, std::vector<llvm::Constant *> &offsets, 1046 const ASTRecordLayout &Layout, 1047 const CXXRecordDecl *PrimaryBase, 1048 bool PrimaryBaseWasVirtual, bool MorallyVirtual, 1049 int64_t Offset, bool ForVirtualBase) { 1050 StartNewTable(); 1051 extra = 0; 1052 // FIXME: Cleanup. 1053 if (!ForVirtualBase) { 1054 // then virtual base offsets... 1055 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(), 1056 e = offsets.rend(); i != e; ++i) 1057 methods.push_back(*i); 1058 } 1059 1060 // The vcalls come first... 1061 for (std::vector<Index_t>::reverse_iterator i=VCalls.rbegin(), 1062 e=VCalls.rend(); 1063 i != e; ++i) 1064 methods.push_back(wrap((0?600:0) + *i)); 1065 VCalls.clear(); 1066 1067 if (ForVirtualBase) { 1068 // then virtual base offsets... 1069 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(), 1070 e = offsets.rend(); i != e; ++i) 1071 methods.push_back(*i); 1072 } 1073 1074 methods.push_back(wrap(-(Offset/8))); 1075 methods.push_back(rtti); 1076 Index_t AddressPoint = methods.size(); 1077 1078 InstallThunks(); 1079 methods.insert(methods.end(), submethods.begin(), submethods.end()); 1080 submethods.clear(); 1081 1082 // and then the non-virtual bases. 1083 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual, 1084 MorallyVirtual, Offset); 1085 return AddressPoint; 1086 } 1087 1088 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset) { 1089 if (!RD->isDynamicClass()) 1090 return; 1091 1092 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); 1093 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); 1094 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); 1095 1096 // vtables are composed from the chain of primaries. 1097 if (PrimaryBase) { 1098 if (PrimaryBaseWasVirtual) 1099 IndirectPrimary.insert(PrimaryBase); 1100 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset); 1101 } 1102 1103 // And add the virtuals for the class to the primary vtable. 1104 AddMethods(RD, MorallyVirtual, Offset); 1105 } 1106 1107 int64_t GenerateVtableForBase(const CXXRecordDecl *RD, 1108 bool MorallyVirtual = false, int64_t Offset = 0, 1109 bool ForVirtualBase = false, 1110 std::vector<std::pair<const CXXRecordDecl *, 1111 int64_t> > *Path = 0) { 1112 if (!RD->isDynamicClass()) 1113 return 0; 1114 1115 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); 1116 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); 1117 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); 1118 1119 std::vector<llvm::Constant *> offsets; 1120 extra = 0; 1121 GenerateVBaseOffsets(offsets, RD, Offset); 1122 if (ForVirtualBase) 1123 extra = offsets.size(); 1124 1125 // vtables are composed from the chain of primaries. 1126 if (PrimaryBase) { 1127 if (PrimaryBaseWasVirtual) 1128 IndirectPrimary.insert(PrimaryBase); 1129 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset); 1130 } 1131 1132 // And add the virtuals for the class to the primary vtable. 1133 AddMethods(RD, MorallyVirtual, Offset); 1134 1135 if (Path) 1136 OverrideMethods(Path, MorallyVirtual); 1137 1138 return end(RD, offsets, Layout, PrimaryBase, PrimaryBaseWasVirtual, 1139 MorallyVirtual, Offset, ForVirtualBase); 1140 } 1141 1142 void GenerateVtableForVBases(const CXXRecordDecl *RD, 1143 int64_t Offset = 0, 1144 std::vector<std::pair<const CXXRecordDecl *, 1145 int64_t> > *Path = 0) { 1146 bool alloc = false; 1147 if (Path == 0) { 1148 alloc = true; 1149 Path = new std::vector<std::pair<const CXXRecordDecl *, 1150 int64_t> >; 1151 } 1152 // FIXME: We also need to override using all paths to a virtual base, 1153 // right now, we just process the first path 1154 Path->push_back(std::make_pair(RD, Offset)); 1155 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), 1156 e = RD->bases_end(); i != e; ++i) { 1157 const CXXRecordDecl *Base = 1158 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); 1159 if (i->isVirtual() && !IndirectPrimary.count(Base)) { 1160 // Mark it so we don't output it twice. 1161 IndirectPrimary.insert(Base); 1162 StartNewTable(); 1163 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base); 1164 GenerateVtableForBase(Base, true, BaseOffset, true, Path); 1165 } 1166 int64_t BaseOffset = Offset; 1167 if (i->isVirtual()) 1168 BaseOffset = BLayout.getVBaseClassOffset(Base); 1169 if (Base->getNumVBases()) 1170 GenerateVtableForVBases(Base, BaseOffset, Path); 1171 } 1172 Path->pop_back(); 1173 if (alloc) 1174 delete Path; 1175 } 1176}; 1177 1178class VtableInfo { 1179public: 1180 typedef VtableBuilder::Index_t Index_t; 1181private: 1182 CodeGenModule &CGM; // Per-module state. 1183 /// Index_t - Vtable index type. 1184 typedef llvm::DenseMap<const CXXMethodDecl *, Index_t> ElTy; 1185 typedef llvm::DenseMap<const CXXRecordDecl *, ElTy *> MapTy; 1186 // FIXME: Move to Context. 1187 static MapTy IndexFor; 1188public: 1189 VtableInfo(CodeGenModule &cgm) : CGM(cgm) { } 1190 void register_index(const CXXRecordDecl *RD, const ElTy &e) { 1191 assert(IndexFor.find(RD) == IndexFor.end() && "Don't compute vtbl twice"); 1192 // We own a copy of this, it will go away shortly. 1193 new ElTy (e); 1194 IndexFor[RD] = new ElTy (e); 1195 } 1196 Index_t lookup(const CXXMethodDecl *MD) { 1197 const CXXRecordDecl *RD = MD->getParent(); 1198 MapTy::iterator I = IndexFor.find(RD); 1199 if (I == IndexFor.end()) { 1200 std::vector<llvm::Constant *> methods; 1201 VtableBuilder b(methods, RD, CGM); 1202 b.GenerateVtableForBase(RD); 1203 b.GenerateVtableForVBases(RD); 1204 register_index(RD, b.getIndex()); 1205 I = IndexFor.find(RD); 1206 } 1207 assert(I->second->find(MD)!=I->second->end() && "Can't find vtable index"); 1208 return (*I->second)[MD]; 1209 } 1210}; 1211 1212// FIXME: Move to Context. 1213VtableInfo::MapTy VtableInfo::IndexFor; 1214 1215llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) { 1216 llvm::SmallString<256> OutName; 1217 llvm::raw_svector_ostream Out(OutName); 1218 QualType ClassTy; 1219 ClassTy = getContext().getTagDeclType(RD); 1220 mangleCXXVtable(ClassTy, getContext(), Out); 1221 llvm::GlobalVariable::LinkageTypes linktype; 1222 linktype = llvm::GlobalValue::WeakAnyLinkage; 1223 std::vector<llvm::Constant *> methods; 1224 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); 1225 int64_t AddressPoint; 1226 1227 VtableBuilder b(methods, RD, CGM); 1228 1229 // First comes the vtables for all the non-virtual bases... 1230 AddressPoint = b.GenerateVtableForBase(RD); 1231 1232 // then the vtables for all the virtual bases. 1233 b.GenerateVtableForVBases(RD); 1234 1235 llvm::Constant *C; 1236 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size()); 1237 C = llvm::ConstantArray::get(type, methods); 1238 llvm::Value *vtable = new llvm::GlobalVariable(CGM.getModule(), type, true, 1239 linktype, C, Out.str()); 1240 vtable = Builder.CreateBitCast(vtable, Ptr8Ty); 1241 vtable = Builder.CreateGEP(vtable, 1242 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), 1243 AddressPoint*LLVMPointerWidth/8)); 1244 return vtable; 1245} 1246 1247// FIXME: move to Context 1248static VtableInfo *vtableinfo; 1249 1250llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn, 1251 const CXXMethodDecl *MD, 1252 bool Extern, int64_t nv, 1253 int64_t v) { 1254 QualType R = MD->getType()->getAsFunctionType()->getResultType(); 1255 1256 FunctionArgList Args; 1257 ImplicitParamDecl *ThisDecl = 1258 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 1259 MD->getThisType(getContext())); 1260 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType())); 1261 for (FunctionDecl::param_const_iterator i = MD->param_begin(), 1262 e = MD->param_end(); 1263 i != e; ++i) { 1264 ParmVarDecl *D = *i; 1265 Args.push_back(std::make_pair(D, D->getType())); 1266 } 1267 IdentifierInfo *II 1268 = &CGM.getContext().Idents.get("__thunk_named_foo_"); 1269 FunctionDecl *FD = FunctionDecl::Create(getContext(), 1270 getContext().getTranslationUnitDecl(), 1271 SourceLocation(), II, R, 0, 1272 Extern 1273 ? FunctionDecl::Extern 1274 : FunctionDecl::Static, 1275 false, true); 1276 StartFunction(FD, R, Fn, Args, SourceLocation()); 1277 // FIXME: generate body 1278 FinishFunction(); 1279 return Fn; 1280} 1281 1282llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern, 1283 int64_t nv, int64_t v) { 1284 llvm::SmallString<256> OutName; 1285 llvm::raw_svector_ostream Out(OutName); 1286 mangleThunk(MD, nv, v, getContext(), Out); 1287 llvm::GlobalVariable::LinkageTypes linktype; 1288 linktype = llvm::GlobalValue::WeakAnyLinkage; 1289 if (!Extern) 1290 linktype = llvm::GlobalValue::InternalLinkage; 1291 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); 1292 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType(); 1293 const llvm::FunctionType *FTy = 1294 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD), 1295 FPT->isVariadic()); 1296 1297 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(), 1298 &getModule()); 1299 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v); 1300 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty); 1301 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); 1302 return m; 1303} 1304 1305llvm::Value * 1306CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This, 1307 const llvm::Type *Ty) { 1308 // FIXME: If we know the dynamic type, we don't have to do a virtual dispatch. 1309 1310 // FIXME: move to Context 1311 if (vtableinfo == 0) 1312 vtableinfo = new VtableInfo(CGM); 1313 1314 VtableInfo::Index_t Idx = vtableinfo->lookup(MD); 1315 1316 Ty = llvm::PointerType::get(Ty, 0); 1317 Ty = llvm::PointerType::get(Ty, 0); 1318 Ty = llvm::PointerType::get(Ty, 0); 1319 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty); 1320 vtbl = Builder.CreateLoad(vtbl); 1321 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl, 1322 Idx, "vfn"); 1323 vfn = Builder.CreateLoad(vfn); 1324 return vfn; 1325} 1326 1327/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class 1328/// array of objects from SrcValue to DestValue. Copying can be either a bitwise 1329/// copy or via a copy constructor call. 1330// FIXME. Consolidate this with EmitCXXAggrConstructorCall. 1331void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest, 1332 llvm::Value *Src, 1333 const ArrayType *Array, 1334 const CXXRecordDecl *BaseClassDecl, 1335 QualType Ty) { 1336 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); 1337 assert(CA && "VLA cannot be copied over"); 1338 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor(); 1339 1340 // Create a temporary for the loop index and initialize it with 0. 1341 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext), 1342 "loop.index"); 1343 llvm::Value* zeroConstant = 1344 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)); 1345 Builder.CreateStore(zeroConstant, IndexPtr, false); 1346 // Start the loop with a block that tests the condition. 1347 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); 1348 llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); 1349 1350 EmitBlock(CondBlock); 1351 1352 llvm::BasicBlock *ForBody = createBasicBlock("for.body"); 1353 // Generate: if (loop-index < number-of-elements fall to the loop body, 1354 // otherwise, go to the block after the for-loop. 1355 uint64_t NumElements = getContext().getConstantArrayElementCount(CA); 1356 llvm::Value * NumElementsPtr = 1357 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements); 1358 llvm::Value *Counter = Builder.CreateLoad(IndexPtr); 1359 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr, 1360 "isless"); 1361 // If the condition is true, execute the body. 1362 Builder.CreateCondBr(IsLess, ForBody, AfterFor); 1363 1364 EmitBlock(ForBody); 1365 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); 1366 // Inside the loop body, emit the constructor call on the array element. 1367 Counter = Builder.CreateLoad(IndexPtr); 1368 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress"); 1369 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress"); 1370 if (BitwiseCopy) 1371 EmitAggregateCopy(Dest, Src, Ty); 1372 else if (CXXConstructorDecl *BaseCopyCtor = 1373 BaseClassDecl->getCopyConstructor(getContext(), 0)) { 1374 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor, 1375 Ctor_Complete); 1376 CallArgList CallArgs; 1377 // Push the this (Dest) ptr. 1378 CallArgs.push_back(std::make_pair(RValue::get(Dest), 1379 BaseCopyCtor->getThisType(getContext()))); 1380 1381 // Push the Src ptr. 1382 CallArgs.push_back(std::make_pair(RValue::get(Src), 1383 BaseCopyCtor->getParamDecl(0)->getType())); 1384 QualType ResultType = 1385 BaseCopyCtor->getType()->getAsFunctionType()->getResultType(); 1386 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), 1387 Callee, CallArgs, BaseCopyCtor); 1388 } 1389 EmitBlock(ContinueBlock); 1390 1391 // Emit the increment of the loop counter. 1392 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); 1393 Counter = Builder.CreateLoad(IndexPtr); 1394 NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); 1395 Builder.CreateStore(NextVal, IndexPtr, false); 1396 1397 // Finally, branch back up to the condition for the next iteration. 1398 EmitBranch(CondBlock); 1399 1400 // Emit the fall-through block. 1401 EmitBlock(AfterFor, true); 1402} 1403 1404/// EmitClassAggrCopyAssignment - This routine generates code to assign a class 1405/// array of objects from SrcValue to DestValue. Assignment can be either a 1406/// bitwise assignment or via a copy assignment operator function call. 1407/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy 1408void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest, 1409 llvm::Value *Src, 1410 const ArrayType *Array, 1411 const CXXRecordDecl *BaseClassDecl, 1412 QualType Ty) { 1413 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); 1414 assert(CA && "VLA cannot be asssigned"); 1415 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment(); 1416 1417 // Create a temporary for the loop index and initialize it with 0. 1418 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext), 1419 "loop.index"); 1420 llvm::Value* zeroConstant = 1421 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)); 1422 Builder.CreateStore(zeroConstant, IndexPtr, false); 1423 // Start the loop with a block that tests the condition. 1424 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); 1425 llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); 1426 1427 EmitBlock(CondBlock); 1428 1429 llvm::BasicBlock *ForBody = createBasicBlock("for.body"); 1430 // Generate: if (loop-index < number-of-elements fall to the loop body, 1431 // otherwise, go to the block after the for-loop. 1432 uint64_t NumElements = getContext().getConstantArrayElementCount(CA); 1433 llvm::Value * NumElementsPtr = 1434 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements); 1435 llvm::Value *Counter = Builder.CreateLoad(IndexPtr); 1436 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr, 1437 "isless"); 1438 // If the condition is true, execute the body. 1439 Builder.CreateCondBr(IsLess, ForBody, AfterFor); 1440 1441 EmitBlock(ForBody); 1442 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); 1443 // Inside the loop body, emit the assignment operator call on array element. 1444 Counter = Builder.CreateLoad(IndexPtr); 1445 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress"); 1446 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress"); 1447 const CXXMethodDecl *MD = 0; 1448 if (BitwiseAssign) 1449 EmitAggregateCopy(Dest, Src, Ty); 1450 else { 1451 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(), 1452 MD); 1453 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign"); 1454 (void)hasCopyAssign; 1455 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType(); 1456 const llvm::Type *LTy = 1457 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), 1458 FPT->isVariadic()); 1459 llvm::Constant *Callee = CGM.GetAddrOfFunction(GlobalDecl(MD), LTy); 1460 1461 CallArgList CallArgs; 1462 // Push the this (Dest) ptr. 1463 CallArgs.push_back(std::make_pair(RValue::get(Dest), 1464 MD->getThisType(getContext()))); 1465 1466 // Push the Src ptr. 1467 CallArgs.push_back(std::make_pair(RValue::get(Src), 1468 MD->getParamDecl(0)->getType())); 1469 QualType ResultType = MD->getType()->getAsFunctionType()->getResultType(); 1470 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), 1471 Callee, CallArgs, MD); 1472 } 1473 EmitBlock(ContinueBlock); 1474 1475 // Emit the increment of the loop counter. 1476 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); 1477 Counter = Builder.CreateLoad(IndexPtr); 1478 NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); 1479 Builder.CreateStore(NextVal, IndexPtr, false); 1480 1481 // Finally, branch back up to the condition for the next iteration. 1482 EmitBranch(CondBlock); 1483 1484 // Emit the fall-through block. 1485 EmitBlock(AfterFor, true); 1486} 1487 1488/// EmitClassMemberwiseCopy - This routine generates code to copy a class 1489/// object from SrcValue to DestValue. Copying can be either a bitwise copy 1490/// or via a copy constructor call. 1491void CodeGenFunction::EmitClassMemberwiseCopy( 1492 llvm::Value *Dest, llvm::Value *Src, 1493 const CXXRecordDecl *ClassDecl, 1494 const CXXRecordDecl *BaseClassDecl, QualType Ty) { 1495 if (ClassDecl) { 1496 Dest = AddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl); 1497 Src = AddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl) ; 1498 } 1499 if (BaseClassDecl->hasTrivialCopyConstructor()) { 1500 EmitAggregateCopy(Dest, Src, Ty); 1501 return; 1502 } 1503 1504 if (CXXConstructorDecl *BaseCopyCtor = 1505 BaseClassDecl->getCopyConstructor(getContext(), 0)) { 1506 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor, 1507 Ctor_Complete); 1508 CallArgList CallArgs; 1509 // Push the this (Dest) ptr. 1510 CallArgs.push_back(std::make_pair(RValue::get(Dest), 1511 BaseCopyCtor->getThisType(getContext()))); 1512 1513 // Push the Src ptr. 1514 CallArgs.push_back(std::make_pair(RValue::get(Src), 1515 BaseCopyCtor->getParamDecl(0)->getType())); 1516 QualType ResultType = 1517 BaseCopyCtor->getType()->getAsFunctionType()->getResultType(); 1518 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), 1519 Callee, CallArgs, BaseCopyCtor); 1520 } 1521} 1522 1523/// EmitClassCopyAssignment - This routine generates code to copy assign a class 1524/// object from SrcValue to DestValue. Assignment can be either a bitwise 1525/// assignment of via an assignment operator call. 1526// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot. 1527void CodeGenFunction::EmitClassCopyAssignment( 1528 llvm::Value *Dest, llvm::Value *Src, 1529 const CXXRecordDecl *ClassDecl, 1530 const CXXRecordDecl *BaseClassDecl, 1531 QualType Ty) { 1532 if (ClassDecl) { 1533 Dest = AddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl); 1534 Src = AddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl) ; 1535 } 1536 if (BaseClassDecl->hasTrivialCopyAssignment()) { 1537 EmitAggregateCopy(Dest, Src, Ty); 1538 return; 1539 } 1540 1541 const CXXMethodDecl *MD = 0; 1542 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(), 1543 MD); 1544 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign"); 1545 (void)ConstCopyAssignOp; 1546 1547 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType(); 1548 const llvm::Type *LTy = 1549 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), 1550 FPT->isVariadic()); 1551 llvm::Constant *Callee = CGM.GetAddrOfFunction(GlobalDecl(MD), LTy); 1552 1553 CallArgList CallArgs; 1554 // Push the this (Dest) ptr. 1555 CallArgs.push_back(std::make_pair(RValue::get(Dest), 1556 MD->getThisType(getContext()))); 1557 1558 // Push the Src ptr. 1559 CallArgs.push_back(std::make_pair(RValue::get(Src), 1560 MD->getParamDecl(0)->getType())); 1561 QualType ResultType = 1562 MD->getType()->getAsFunctionType()->getResultType(); 1563 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), 1564 Callee, CallArgs, MD); 1565} 1566 1567/// SynthesizeDefaultConstructor - synthesize a default constructor 1568void 1569CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *CD, 1570 const FunctionDecl *FD, 1571 llvm::Function *Fn, 1572 const FunctionArgList &Args) { 1573 StartFunction(FD, FD->getResultType(), Fn, Args, SourceLocation()); 1574 EmitCtorPrologue(CD); 1575 FinishFunction(); 1576} 1577 1578/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy 1579/// constructor, in accordance with section 12.8 (p7 and p8) of C++03 1580/// The implicitly-defined copy constructor for class X performs a memberwise 1581/// copy of its subobjects. The order of copying is the same as the order 1582/// of initialization of bases and members in a user-defined constructor 1583/// Each subobject is copied in the manner appropriate to its type: 1584/// if the subobject is of class type, the copy constructor for the class is 1585/// used; 1586/// if the subobject is an array, each element is copied, in the manner 1587/// appropriate to the element type; 1588/// if the subobject is of scalar type, the built-in assignment operator is 1589/// used. 1590/// Virtual base class subobjects shall be copied only once by the 1591/// implicitly-defined copy constructor 1592 1593void CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *CD, 1594 const FunctionDecl *FD, 1595 llvm::Function *Fn, 1596 const FunctionArgList &Args) { 1597 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext()); 1598 assert(!ClassDecl->hasUserDeclaredCopyConstructor() && 1599 "SynthesizeCXXCopyConstructor - copy constructor has definition already"); 1600 StartFunction(FD, FD->getResultType(), Fn, Args, SourceLocation()); 1601 1602 FunctionArgList::const_iterator i = Args.begin(); 1603 const VarDecl *ThisArg = i->first; 1604 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg); 1605 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this"); 1606 const VarDecl *SrcArg = (i+1)->first; 1607 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg); 1608 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj); 1609 1610 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); 1611 Base != ClassDecl->bases_end(); ++Base) { 1612 // FIXME. copy constrution of virtual base NYI 1613 if (Base->isVirtual()) 1614 continue; 1615 1616 CXXRecordDecl *BaseClassDecl 1617 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 1618 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl, 1619 Base->getType()); 1620 } 1621 1622 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), 1623 FieldEnd = ClassDecl->field_end(); 1624 Field != FieldEnd; ++Field) { 1625 QualType FieldType = getContext().getCanonicalType((*Field)->getType()); 1626 const ConstantArrayType *Array = 1627 getContext().getAsConstantArrayType(FieldType); 1628 if (Array) 1629 FieldType = getContext().getBaseElementType(FieldType); 1630 1631 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { 1632 CXXRecordDecl *FieldClassDecl 1633 = cast<CXXRecordDecl>(FieldClassType->getDecl()); 1634 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); 1635 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); 1636 if (Array) { 1637 const llvm::Type *BasePtr = ConvertType(FieldType); 1638 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1639 llvm::Value *DestBaseAddrPtr = 1640 Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1641 llvm::Value *SrcBaseAddrPtr = 1642 Builder.CreateBitCast(RHS.getAddress(), BasePtr); 1643 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array, 1644 FieldClassDecl, FieldType); 1645 } 1646 else 1647 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(), 1648 0 /*ClassDecl*/, FieldClassDecl, FieldType); 1649 continue; 1650 } 1651 // Do a built-in assignment of scalar data members. 1652 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); 1653 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); 1654 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType); 1655 EmitStoreThroughLValue(RVRHS, LHS, FieldType); 1656 } 1657 FinishFunction(); 1658} 1659 1660/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator. 1661/// Before the implicitly-declared copy assignment operator for a class is 1662/// implicitly defined, all implicitly- declared copy assignment operators for 1663/// its direct base classes and its nonstatic data members shall have been 1664/// implicitly defined. [12.8-p12] 1665/// The implicitly-defined copy assignment operator for class X performs 1666/// memberwise assignment of its subob- jects. The direct base classes of X are 1667/// assigned first, in the order of their declaration in 1668/// the base-specifier-list, and then the immediate nonstatic data members of X 1669/// are assigned, in the order in which they were declared in the class 1670/// definition.Each subobject is assigned in the manner appropriate to its type: 1671/// if the subobject is of class type, the copy assignment operator for the 1672/// class is used (as if by explicit qualification; that is, ignoring any 1673/// possible virtual overriding functions in more derived classes); 1674/// 1675/// if the subobject is an array, each element is assigned, in the manner 1676/// appropriate to the element type; 1677/// 1678/// if the subobject is of scalar type, the built-in assignment operator is 1679/// used. 1680void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD, 1681 const FunctionDecl *FD, 1682 llvm::Function *Fn, 1683 const FunctionArgList &Args) { 1684 1685 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext()); 1686 assert(!ClassDecl->hasUserDeclaredCopyAssignment() && 1687 "SynthesizeCXXCopyAssignment - copy assignment has user declaration"); 1688 StartFunction(FD, FD->getResultType(), Fn, Args, SourceLocation()); 1689 1690 FunctionArgList::const_iterator i = Args.begin(); 1691 const VarDecl *ThisArg = i->first; 1692 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg); 1693 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this"); 1694 const VarDecl *SrcArg = (i+1)->first; 1695 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg); 1696 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj); 1697 1698 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); 1699 Base != ClassDecl->bases_end(); ++Base) { 1700 // FIXME. copy assignment of virtual base NYI 1701 if (Base->isVirtual()) 1702 continue; 1703 1704 CXXRecordDecl *BaseClassDecl 1705 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 1706 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl, 1707 Base->getType()); 1708 } 1709 1710 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), 1711 FieldEnd = ClassDecl->field_end(); 1712 Field != FieldEnd; ++Field) { 1713 QualType FieldType = getContext().getCanonicalType((*Field)->getType()); 1714 const ConstantArrayType *Array = 1715 getContext().getAsConstantArrayType(FieldType); 1716 if (Array) 1717 FieldType = getContext().getBaseElementType(FieldType); 1718 1719 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { 1720 CXXRecordDecl *FieldClassDecl 1721 = cast<CXXRecordDecl>(FieldClassType->getDecl()); 1722 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); 1723 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); 1724 if (Array) { 1725 const llvm::Type *BasePtr = ConvertType(FieldType); 1726 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1727 llvm::Value *DestBaseAddrPtr = 1728 Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1729 llvm::Value *SrcBaseAddrPtr = 1730 Builder.CreateBitCast(RHS.getAddress(), BasePtr); 1731 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array, 1732 FieldClassDecl, FieldType); 1733 } 1734 else 1735 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(), 1736 0 /*ClassDecl*/, FieldClassDecl, FieldType); 1737 continue; 1738 } 1739 // Do a built-in assignment of scalar data members. 1740 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); 1741 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); 1742 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType); 1743 EmitStoreThroughLValue(RVRHS, LHS, FieldType); 1744 } 1745 1746 // return *this; 1747 Builder.CreateStore(LoadOfThis, ReturnValue); 1748 1749 FinishFunction(); 1750} 1751 1752/// EmitCtorPrologue - This routine generates necessary code to initialize 1753/// base classes and non-static data members belonging to this constructor. 1754/// FIXME: This needs to take a CXXCtorType. 1755void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD) { 1756 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext()); 1757 // FIXME: Add vbase initialization 1758 llvm::Value *LoadOfThis = 0; 1759 1760 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(), 1761 E = CD->init_end(); 1762 B != E; ++B) { 1763 CXXBaseOrMemberInitializer *Member = (*B); 1764 if (Member->isBaseInitializer()) { 1765 LoadOfThis = LoadCXXThis(); 1766 Type *BaseType = Member->getBaseClass(); 1767 CXXRecordDecl *BaseClassDecl = 1768 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); 1769 llvm::Value *V = AddressCXXOfBaseClass(LoadOfThis, ClassDecl, 1770 BaseClassDecl); 1771 EmitCXXConstructorCall(Member->getConstructor(), 1772 Ctor_Complete, V, 1773 Member->const_arg_begin(), 1774 Member->const_arg_end()); 1775 } else { 1776 // non-static data member initilaizers. 1777 FieldDecl *Field = Member->getMember(); 1778 QualType FieldType = getContext().getCanonicalType((Field)->getType()); 1779 const ConstantArrayType *Array = 1780 getContext().getAsConstantArrayType(FieldType); 1781 if (Array) 1782 FieldType = getContext().getBaseElementType(FieldType); 1783 1784 LoadOfThis = LoadCXXThis(); 1785 LValue LHS; 1786 if (FieldType->isReferenceType()) { 1787 // FIXME: This is really ugly; should be refactored somehow 1788 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field); 1789 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp"); 1790 LHS = LValue::MakeAddr(V, FieldType.getCVRQualifiers(), 1791 QualType::GCNone, FieldType.getAddressSpace()); 1792 } else { 1793 LHS = EmitLValueForField(LoadOfThis, Field, false, 0); 1794 } 1795 if (FieldType->getAs<RecordType>()) { 1796 if (!Field->isAnonymousStructOrUnion()) { 1797 assert(Member->getConstructor() && 1798 "EmitCtorPrologue - no constructor to initialize member"); 1799 if (Array) { 1800 const llvm::Type *BasePtr = ConvertType(FieldType); 1801 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1802 llvm::Value *BaseAddrPtr = 1803 Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1804 EmitCXXAggrConstructorCall(Member->getConstructor(), 1805 Array, BaseAddrPtr); 1806 } 1807 else 1808 EmitCXXConstructorCall(Member->getConstructor(), 1809 Ctor_Complete, LHS.getAddress(), 1810 Member->const_arg_begin(), 1811 Member->const_arg_end()); 1812 continue; 1813 } 1814 else { 1815 // Initializing an anonymous union data member. 1816 FieldDecl *anonMember = Member->getAnonUnionMember(); 1817 LHS = EmitLValueForField(LHS.getAddress(), anonMember, 1818 /*IsUnion=*/true, 0); 1819 FieldType = anonMember->getType(); 1820 } 1821 } 1822 1823 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only"); 1824 Expr *RhsExpr = *Member->arg_begin(); 1825 RValue RHS; 1826 if (FieldType->isReferenceType()) 1827 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType, 1828 /*IsInitializer=*/true); 1829 else 1830 RHS = RValue::get(EmitScalarExpr(RhsExpr, true)); 1831 EmitStoreThroughLValue(RHS, LHS, FieldType); 1832 } 1833 } 1834 1835 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) { 1836 // Nontrivial default constructor with no initializer list. It may still 1837 // have bases classes and/or contain non-static data members which require 1838 // construction. 1839 for (CXXRecordDecl::base_class_const_iterator Base = 1840 ClassDecl->bases_begin(); 1841 Base != ClassDecl->bases_end(); ++Base) { 1842 // FIXME. copy assignment of virtual base NYI 1843 if (Base->isVirtual()) 1844 continue; 1845 1846 CXXRecordDecl *BaseClassDecl 1847 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 1848 if (BaseClassDecl->hasTrivialConstructor()) 1849 continue; 1850 if (CXXConstructorDecl *BaseCX = 1851 BaseClassDecl->getDefaultConstructor(getContext())) { 1852 LoadOfThis = LoadCXXThis(); 1853 llvm::Value *V = AddressCXXOfBaseClass(LoadOfThis, ClassDecl, 1854 BaseClassDecl); 1855 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0); 1856 } 1857 } 1858 1859 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), 1860 FieldEnd = ClassDecl->field_end(); 1861 Field != FieldEnd; ++Field) { 1862 QualType FieldType = getContext().getCanonicalType((*Field)->getType()); 1863 const ConstantArrayType *Array = 1864 getContext().getAsConstantArrayType(FieldType); 1865 if (Array) 1866 FieldType = getContext().getBaseElementType(FieldType); 1867 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion()) 1868 continue; 1869 const RecordType *ClassRec = FieldType->getAs<RecordType>(); 1870 CXXRecordDecl *MemberClassDecl = 1871 dyn_cast<CXXRecordDecl>(ClassRec->getDecl()); 1872 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor()) 1873 continue; 1874 if (CXXConstructorDecl *MamberCX = 1875 MemberClassDecl->getDefaultConstructor(getContext())) { 1876 LoadOfThis = LoadCXXThis(); 1877 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); 1878 if (Array) { 1879 const llvm::Type *BasePtr = ConvertType(FieldType); 1880 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1881 llvm::Value *BaseAddrPtr = 1882 Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1883 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr); 1884 } 1885 else 1886 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(), 1887 0, 0); 1888 } 1889 } 1890 } 1891 1892 // Initialize the vtable pointer 1893 if (ClassDecl->isDynamicClass()) { 1894 if (!LoadOfThis) 1895 LoadOfThis = LoadCXXThis(); 1896 llvm::Value *VtableField; 1897 llvm::Type *Ptr8Ty, *PtrPtr8Ty; 1898 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); 1899 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0); 1900 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty); 1901 llvm::Value *vtable = GenerateVtable(ClassDecl); 1902 Builder.CreateStore(vtable, VtableField); 1903 } 1904} 1905 1906/// EmitDtorEpilogue - Emit all code that comes at the end of class's 1907/// destructor. This is to call destructors on members and base classes 1908/// in reverse order of their construction. 1909/// FIXME: This needs to take a CXXDtorType. 1910void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD) { 1911 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext()); 1912 assert(!ClassDecl->getNumVBases() && 1913 "FIXME: Destruction of virtual bases not supported"); 1914 (void)ClassDecl; // prevent warning. 1915 1916 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(), 1917 *E = DD->destr_end(); B != E; ++B) { 1918 uintptr_t BaseOrMember = (*B); 1919 if (DD->isMemberToDestroy(BaseOrMember)) { 1920 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember); 1921 QualType FieldType = getContext().getCanonicalType((FD)->getType()); 1922 const ConstantArrayType *Array = 1923 getContext().getAsConstantArrayType(FieldType); 1924 if (Array) 1925 FieldType = getContext().getBaseElementType(FieldType); 1926 const RecordType *RT = FieldType->getAs<RecordType>(); 1927 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); 1928 if (FieldClassDecl->hasTrivialDestructor()) 1929 continue; 1930 llvm::Value *LoadOfThis = LoadCXXThis(); 1931 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0); 1932 if (Array) { 1933 const llvm::Type *BasePtr = ConvertType(FieldType); 1934 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1935 llvm::Value *BaseAddrPtr = 1936 Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1937 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()), 1938 Array, BaseAddrPtr); 1939 } 1940 else 1941 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()), 1942 Dtor_Complete, LHS.getAddress()); 1943 } else { 1944 const RecordType *RT = 1945 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>(); 1946 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); 1947 if (BaseClassDecl->hasTrivialDestructor()) 1948 continue; 1949 llvm::Value *V = AddressCXXOfBaseClass(LoadCXXThis(), 1950 ClassDecl,BaseClassDecl); 1951 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()), 1952 Dtor_Complete, V); 1953 } 1954 } 1955 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial()) 1956 return; 1957 // Case of destructor synthesis with fields and base classes 1958 // which have non-trivial destructors. They must be destructed in 1959 // reverse order of their construction. 1960 llvm::SmallVector<FieldDecl *, 16> DestructedFields; 1961 1962 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), 1963 FieldEnd = ClassDecl->field_end(); 1964 Field != FieldEnd; ++Field) { 1965 QualType FieldType = getContext().getCanonicalType((*Field)->getType()); 1966 if (getContext().getAsConstantArrayType(FieldType)) 1967 FieldType = getContext().getBaseElementType(FieldType); 1968 if (const RecordType *RT = FieldType->getAs<RecordType>()) { 1969 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); 1970 if (FieldClassDecl->hasTrivialDestructor()) 1971 continue; 1972 DestructedFields.push_back(*Field); 1973 } 1974 } 1975 if (!DestructedFields.empty()) 1976 for (int i = DestructedFields.size() -1; i >= 0; --i) { 1977 FieldDecl *Field = DestructedFields[i]; 1978 QualType FieldType = Field->getType(); 1979 const ConstantArrayType *Array = 1980 getContext().getAsConstantArrayType(FieldType); 1981 if (Array) 1982 FieldType = getContext().getBaseElementType(FieldType); 1983 const RecordType *RT = FieldType->getAs<RecordType>(); 1984 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); 1985 llvm::Value *LoadOfThis = LoadCXXThis(); 1986 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0); 1987 if (Array) { 1988 const llvm::Type *BasePtr = ConvertType(FieldType); 1989 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1990 llvm::Value *BaseAddrPtr = 1991 Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1992 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()), 1993 Array, BaseAddrPtr); 1994 } 1995 else 1996 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()), 1997 Dtor_Complete, LHS.getAddress()); 1998 } 1999 2000 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases; 2001 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); 2002 Base != ClassDecl->bases_end(); ++Base) { 2003 // FIXME. copy assignment of virtual base NYI 2004 if (Base->isVirtual()) 2005 continue; 2006 2007 CXXRecordDecl *BaseClassDecl 2008 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 2009 if (BaseClassDecl->hasTrivialDestructor()) 2010 continue; 2011 DestructedBases.push_back(BaseClassDecl); 2012 } 2013 if (DestructedBases.empty()) 2014 return; 2015 for (int i = DestructedBases.size() -1; i >= 0; --i) { 2016 CXXRecordDecl *BaseClassDecl = DestructedBases[i]; 2017 llvm::Value *V = AddressCXXOfBaseClass(LoadCXXThis(), 2018 ClassDecl,BaseClassDecl); 2019 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()), 2020 Dtor_Complete, V); 2021 } 2022} 2023 2024void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *CD, 2025 const FunctionDecl *FD, 2026 llvm::Function *Fn, 2027 const FunctionArgList &Args) { 2028 2029 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext()); 2030 assert(!ClassDecl->hasUserDeclaredDestructor() && 2031 "SynthesizeDefaultDestructor - destructor has user declaration"); 2032 (void) ClassDecl; 2033 2034 StartFunction(FD, FD->getResultType(), Fn, Args, SourceLocation()); 2035 EmitDtorEpilogue(CD); 2036 FinishFunction(); 2037} 2038