CGCXX.cpp revision a18df0ec71c5c95fb6809d4a80050f7b218f38b5
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 std::vector<llvm::Constant *> &submethods, 914 Index_t AddressPoint) { 915 typedef CXXMethodDecl::method_iterator meth_iter; 916 917 // FIXME: Don't like the nested loops. For very large inheritance 918 // heirarchies we could have a table on the side with the final overridder 919 // and just replace each instance of an overridden method once. Would be 920 // nice to measure the cost/benefit on real code. 921 922 for (meth_iter mi = MD->begin_overridden_methods(), 923 e = MD->end_overridden_methods(); 924 mi != e; ++mi) { 925 const CXXMethodDecl *OMD = *mi; 926 llvm::Constant *om; 927 om = CGM.GetAddrOfFunction(GlobalDecl(OMD), Ptr8Ty); 928 om = llvm::ConstantExpr::getBitCast(om, Ptr8Ty); 929 930 for (Index_t i = AddressPoint, e = submethods.size(); 931 i != e; ++i) { 932 // FIXME: begin_overridden_methods might be too lax, covariance */ 933 if (submethods[i] != om) 934 continue; 935 submethods[i] = m; 936 Index[MD] = i - AddressPoint; 937 938 Thunks.erase(OMD); 939 if (MorallyVirtual) { 940 VCallOffset[MD] = Offset/8; 941 Index_t &idx = VCall[OMD]; 942 if (idx == 0) { 943 idx = VCalls.size()+1; 944 VCalls.push_back(0); 945 } 946 VCalls[idx] = Offset/8 - VCallOffset[OMD]; 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(Index_t AddressPoint) { 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 methods[AddressPoint + idx] = CGM.BuildThunk(MD, Extern, nv_O, v_O); 974 } 975 Thunks.clear(); 976 } 977 978 void OverrideMethods(const CXXRecordDecl *RD, Index_t AddressPoint, 979 bool MorallyVirtual, Index_t Offset) { 980 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me; 981 ++mi) 982 if (mi->isVirtual()) { 983 const CXXMethodDecl *MD = *mi; 984 llvm::Constant *m = wrap(CGM.GetAddrOfFunction(GlobalDecl(MD), Ptr8Ty)); 985 OverrideMethod(MD, m, MorallyVirtual, Offset, methods, AddressPoint); 986 } 987 } 988 989 void AddMethod(const CXXMethodDecl *MD, bool MorallyVirtual, Index_t Offset) { 990 llvm::Constant *m = wrap(CGM.GetAddrOfFunction(GlobalDecl(MD), Ptr8Ty)); 991 // If we can find a previously allocated slot for this, reuse it. 992 if (OverrideMethod(MD, m, MorallyVirtual, Offset, submethods, 0)) 993 return; 994 995 // else allocate a new slot. 996 Index[MD] = submethods.size(); 997 if (MorallyVirtual) { 998 VCallOffset[MD] = Offset/8; 999 Index_t &idx = VCall[MD]; 1000 // Allocate the first one, after that, we reuse the previous one. 1001 if (idx == 0) { 1002 idx = VCalls.size()+1; 1003 VCalls.push_back(0); 1004 } 1005 } 1006 submethods.push_back(m); 1007 } 1008 1009 void AddMethods(const CXXRecordDecl *RD, bool MorallyVirtual, 1010 Index_t Offset) { 1011 for (method_iter mi = RD->method_begin(), me = RD->method_end(); mi != me; 1012 ++mi) 1013 if (mi->isVirtual()) 1014 AddMethod(*mi, MorallyVirtual, Offset); 1015 } 1016 1017 void NonVirtualBases(const CXXRecordDecl *RD, const ASTRecordLayout &Layout, 1018 const CXXRecordDecl *PrimaryBase, 1019 bool PrimaryBaseWasVirtual, bool MorallyVirtual, 1020 int64_t Offset) { 1021 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), 1022 e = RD->bases_end(); i != e; ++i) { 1023 if (i->isVirtual()) 1024 continue; 1025 const CXXRecordDecl *Base = 1026 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); 1027 if (Base != PrimaryBase || PrimaryBaseWasVirtual) { 1028 uint64_t o = Offset + Layout.getBaseClassOffset(Base); 1029 StartNewTable(); 1030 Index_t AP; 1031 AP = GenerateVtableForBase(Base, MorallyVirtual, o, false, RD); 1032 OverrideMethods(RD, AP, MorallyVirtual, o); 1033 InstallThunks(AP); 1034 } 1035 } 1036 } 1037 1038 Index_t end(const CXXRecordDecl *RD, std::vector<llvm::Constant *> &offsets, 1039 const ASTRecordLayout &Layout, 1040 const CXXRecordDecl *PrimaryBase, 1041 bool PrimaryBaseWasVirtual, bool MorallyVirtual, 1042 int64_t Offset, bool ForVirtualBase) { 1043 StartNewTable(); 1044 extra = 0; 1045 // FIXME: Cleanup. 1046 if (!ForVirtualBase) { 1047 // then virtual base offsets... 1048 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(), 1049 e = offsets.rend(); i != e; ++i) 1050 methods.push_back(*i); 1051 } 1052 1053 // The vcalls come first... 1054 for (std::vector<Index_t>::iterator i=VCalls.begin(), e=VCalls.end(); 1055 i < e; ++i) 1056 methods.push_back(wrap((0?600:0) + *i)); 1057 VCalls.clear(); 1058 1059 if (ForVirtualBase) { 1060 // then virtual base offsets... 1061 for (std::vector<llvm::Constant *>::reverse_iterator i = offsets.rbegin(), 1062 e = offsets.rend(); i != e; ++i) 1063 methods.push_back(*i); 1064 } 1065 1066 methods.push_back(wrap(-(Offset/8))); 1067 methods.push_back(rtti); 1068 Index_t AddressPoint = methods.size(); 1069 1070 methods.insert(methods.end(), submethods.begin(), submethods.end()); 1071 submethods.clear(); 1072 InstallThunks(AddressPoint); 1073 1074 // and then the non-virtual bases. 1075 NonVirtualBases(RD, Layout, PrimaryBase, PrimaryBaseWasVirtual, 1076 MorallyVirtual, Offset); 1077 return AddressPoint; 1078 } 1079 1080 void Primaries(const CXXRecordDecl *RD, bool MorallyVirtual, int64_t Offset) { 1081 if (!RD->isDynamicClass()) 1082 return; 1083 1084 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); 1085 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); 1086 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); 1087 1088 // vtables are composed from the chain of primaries. 1089 if (PrimaryBase) { 1090 if (PrimaryBaseWasVirtual) 1091 IndirectPrimary.insert(PrimaryBase); 1092 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset); 1093 } 1094 1095 // And add the virtuals for the class to the primary vtable. 1096 AddMethods(RD, MorallyVirtual, Offset); 1097 } 1098 1099 int64_t GenerateVtableForBase(const CXXRecordDecl *RD, 1100 bool MorallyVirtual = false, int64_t Offset = 0, 1101 bool ForVirtualBase = false, 1102 const CXXRecordDecl *FinalD = 0) { 1103 if (!RD->isDynamicClass()) 1104 return 0; 1105 1106 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); 1107 const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase(); 1108 const bool PrimaryBaseWasVirtual = Layout.getPrimaryBaseWasVirtual(); 1109 1110 std::vector<llvm::Constant *> offsets; 1111 extra = 0; 1112 GenerateVBaseOffsets(offsets, RD, Offset); 1113 if (ForVirtualBase) 1114 extra = offsets.size(); 1115 1116 // vtables are composed from the chain of primaries. 1117 if (PrimaryBase) { 1118 if (PrimaryBaseWasVirtual) 1119 IndirectPrimary.insert(PrimaryBase); 1120 Primaries(PrimaryBase, PrimaryBaseWasVirtual|MorallyVirtual, Offset); 1121 } 1122 1123 // And add the virtuals for the class to the primary vtable. 1124 AddMethods(RD, MorallyVirtual, Offset); 1125 1126 return end(RD, offsets, Layout, PrimaryBase, PrimaryBaseWasVirtual, 1127 MorallyVirtual, Offset, ForVirtualBase); 1128 } 1129 1130 void GenerateVtableForVBases(const CXXRecordDecl *RD) { 1131 for (CXXRecordDecl::base_class_const_iterator i = RD->bases_begin(), 1132 e = RD->bases_end(); i != e; ++i) { 1133 const CXXRecordDecl *Base = 1134 cast<CXXRecordDecl>(i->getType()->getAs<RecordType>()->getDecl()); 1135 if (i->isVirtual() && !IndirectPrimary.count(Base)) { 1136 // Mark it so we don't output it twice. 1137 IndirectPrimary.insert(Base); 1138 StartNewTable(); 1139 int64_t BaseOffset = BLayout.getVBaseClassOffset(Base); 1140 Index_t AP; 1141 AP = GenerateVtableForBase(Base, true, BaseOffset, true, RD); 1142 OverrideMethods(RD, AP, true, BaseOffset); 1143 InstallThunks(AP); 1144 } 1145 if (Base->getNumVBases()) 1146 GenerateVtableForVBases(Base); 1147 } 1148 } 1149}; 1150 1151class VtableInfo { 1152public: 1153 typedef VtableBuilder::Index_t Index_t; 1154private: 1155 CodeGenModule &CGM; // Per-module state. 1156 /// Index_t - Vtable index type. 1157 typedef llvm::DenseMap<const CXXMethodDecl *, Index_t> ElTy; 1158 typedef llvm::DenseMap<const CXXRecordDecl *, ElTy *> MapTy; 1159 // FIXME: Move to Context. 1160 static MapTy IndexFor; 1161public: 1162 VtableInfo(CodeGenModule &cgm) : CGM(cgm) { } 1163 void register_index(const CXXRecordDecl *RD, const ElTy &e) { 1164 assert(IndexFor.find(RD) == IndexFor.end() && "Don't compute vtbl twice"); 1165 // We own a copy of this, it will go away shortly. 1166 new ElTy (e); 1167 IndexFor[RD] = new ElTy (e); 1168 } 1169 Index_t lookup(const CXXMethodDecl *MD) { 1170 const CXXRecordDecl *RD = MD->getParent(); 1171 MapTy::iterator I = IndexFor.find(RD); 1172 if (I == IndexFor.end()) { 1173 std::vector<llvm::Constant *> methods; 1174 VtableBuilder b(methods, RD, CGM); 1175 b.GenerateVtableForBase(RD); 1176 b.GenerateVtableForVBases(RD); 1177 register_index(RD, b.getIndex()); 1178 I = IndexFor.find(RD); 1179 } 1180 assert(I->second->find(MD)!=I->second->end() && "Can't find vtable index"); 1181 return (*I->second)[MD]; 1182 } 1183}; 1184 1185// FIXME: Move to Context. 1186VtableInfo::MapTy VtableInfo::IndexFor; 1187 1188llvm::Value *CodeGenFunction::GenerateVtable(const CXXRecordDecl *RD) { 1189 llvm::SmallString<256> OutName; 1190 llvm::raw_svector_ostream Out(OutName); 1191 QualType ClassTy; 1192 ClassTy = getContext().getTagDeclType(RD); 1193 mangleCXXVtable(ClassTy, getContext(), Out); 1194 llvm::GlobalVariable::LinkageTypes linktype; 1195 linktype = llvm::GlobalValue::WeakAnyLinkage; 1196 std::vector<llvm::Constant *> methods; 1197 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); 1198 int64_t Offset; 1199 1200 VtableBuilder b(methods, RD, CGM); 1201 1202 // First comes the vtables for all the non-virtual bases... 1203 Offset = b.GenerateVtableForBase(RD); 1204 1205 // then the vtables for all the virtual bases. 1206 b.GenerateVtableForVBases(RD); 1207 1208 llvm::Constant *C; 1209 llvm::ArrayType *type = llvm::ArrayType::get(Ptr8Ty, methods.size()); 1210 C = llvm::ConstantArray::get(type, methods); 1211 llvm::Value *vtable = new llvm::GlobalVariable(CGM.getModule(), type, true, 1212 linktype, C, Out.str()); 1213 vtable = Builder.CreateBitCast(vtable, Ptr8Ty); 1214 vtable = Builder.CreateGEP(vtable, 1215 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), 1216 Offset*LLVMPointerWidth/8)); 1217 return vtable; 1218} 1219 1220// FIXME: move to Context 1221static VtableInfo *vtableinfo; 1222 1223llvm::Constant *CodeGenFunction::GenerateThunk(llvm::Function *Fn, 1224 const CXXMethodDecl *MD, 1225 bool Extern, int64_t nv, 1226 int64_t v) { 1227 QualType R = MD->getType()->getAsFunctionType()->getResultType(); 1228 1229 FunctionArgList Args; 1230 ImplicitParamDecl *ThisDecl = 1231 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 1232 MD->getThisType(getContext())); 1233 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType())); 1234 for (FunctionDecl::param_const_iterator i = MD->param_begin(), 1235 e = MD->param_end(); 1236 i != e; ++i) { 1237 ParmVarDecl *D = *i; 1238 Args.push_back(std::make_pair(D, D->getType())); 1239 } 1240 IdentifierInfo *II 1241 = &CGM.getContext().Idents.get("__thunk_named_foo_"); 1242 FunctionDecl *FD = FunctionDecl::Create(getContext(), 1243 getContext().getTranslationUnitDecl(), 1244 SourceLocation(), II, R, 0, 1245 Extern 1246 ? FunctionDecl::Extern 1247 : FunctionDecl::Static, 1248 false, true); 1249 StartFunction(FD, R, Fn, Args, SourceLocation()); 1250 // FIXME: generate body 1251 FinishFunction(); 1252 return Fn; 1253} 1254 1255llvm::Constant *CodeGenModule::BuildThunk(const CXXMethodDecl *MD, bool Extern, 1256 int64_t nv, int64_t v) { 1257 llvm::SmallString<256> OutName; 1258 llvm::raw_svector_ostream Out(OutName); 1259 mangleThunk(MD, nv, v, getContext(), Out); 1260 llvm::GlobalVariable::LinkageTypes linktype; 1261 linktype = llvm::GlobalValue::WeakAnyLinkage; 1262 if (!Extern) 1263 linktype = llvm::GlobalValue::InternalLinkage; 1264 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); 1265 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType(); 1266 const llvm::FunctionType *FTy = 1267 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD), 1268 FPT->isVariadic()); 1269 1270 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, Out.str(), 1271 &getModule()); 1272 CodeGenFunction(*this).GenerateThunk(Fn, MD, Extern, nv, v); 1273 // Fn = Builder.CreateBitCast(Fn, Ptr8Ty); 1274 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); 1275 return m; 1276} 1277 1278llvm::Value * 1279CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This, 1280 const llvm::Type *Ty) { 1281 // FIXME: If we know the dynamic type, we don't have to do a virtual dispatch. 1282 1283 // FIXME: move to Context 1284 if (vtableinfo == 0) 1285 vtableinfo = new VtableInfo(CGM); 1286 1287 VtableInfo::Index_t Idx = vtableinfo->lookup(MD); 1288 1289 Ty = llvm::PointerType::get(Ty, 0); 1290 Ty = llvm::PointerType::get(Ty, 0); 1291 Ty = llvm::PointerType::get(Ty, 0); 1292 llvm::Value *vtbl = Builder.CreateBitCast(This, Ty); 1293 vtbl = Builder.CreateLoad(vtbl); 1294 llvm::Value *vfn = Builder.CreateConstInBoundsGEP1_64(vtbl, 1295 Idx, "vfn"); 1296 vfn = Builder.CreateLoad(vfn); 1297 return vfn; 1298} 1299 1300/// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class 1301/// array of objects from SrcValue to DestValue. Copying can be either a bitwise 1302/// copy or via a copy constructor call. 1303// FIXME. Consolidate this with EmitCXXAggrConstructorCall. 1304void CodeGenFunction::EmitClassAggrMemberwiseCopy(llvm::Value *Dest, 1305 llvm::Value *Src, 1306 const ArrayType *Array, 1307 const CXXRecordDecl *BaseClassDecl, 1308 QualType Ty) { 1309 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); 1310 assert(CA && "VLA cannot be copied over"); 1311 bool BitwiseCopy = BaseClassDecl->hasTrivialCopyConstructor(); 1312 1313 // Create a temporary for the loop index and initialize it with 0. 1314 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext), 1315 "loop.index"); 1316 llvm::Value* zeroConstant = 1317 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)); 1318 Builder.CreateStore(zeroConstant, IndexPtr, false); 1319 // Start the loop with a block that tests the condition. 1320 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); 1321 llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); 1322 1323 EmitBlock(CondBlock); 1324 1325 llvm::BasicBlock *ForBody = createBasicBlock("for.body"); 1326 // Generate: if (loop-index < number-of-elements fall to the loop body, 1327 // otherwise, go to the block after the for-loop. 1328 uint64_t NumElements = getContext().getConstantArrayElementCount(CA); 1329 llvm::Value * NumElementsPtr = 1330 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements); 1331 llvm::Value *Counter = Builder.CreateLoad(IndexPtr); 1332 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr, 1333 "isless"); 1334 // If the condition is true, execute the body. 1335 Builder.CreateCondBr(IsLess, ForBody, AfterFor); 1336 1337 EmitBlock(ForBody); 1338 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); 1339 // Inside the loop body, emit the constructor call on the array element. 1340 Counter = Builder.CreateLoad(IndexPtr); 1341 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress"); 1342 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress"); 1343 if (BitwiseCopy) 1344 EmitAggregateCopy(Dest, Src, Ty); 1345 else if (CXXConstructorDecl *BaseCopyCtor = 1346 BaseClassDecl->getCopyConstructor(getContext(), 0)) { 1347 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor, 1348 Ctor_Complete); 1349 CallArgList CallArgs; 1350 // Push the this (Dest) ptr. 1351 CallArgs.push_back(std::make_pair(RValue::get(Dest), 1352 BaseCopyCtor->getThisType(getContext()))); 1353 1354 // Push the Src ptr. 1355 CallArgs.push_back(std::make_pair(RValue::get(Src), 1356 BaseCopyCtor->getParamDecl(0)->getType())); 1357 QualType ResultType = 1358 BaseCopyCtor->getType()->getAsFunctionType()->getResultType(); 1359 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), 1360 Callee, CallArgs, BaseCopyCtor); 1361 } 1362 EmitBlock(ContinueBlock); 1363 1364 // Emit the increment of the loop counter. 1365 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); 1366 Counter = Builder.CreateLoad(IndexPtr); 1367 NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); 1368 Builder.CreateStore(NextVal, IndexPtr, false); 1369 1370 // Finally, branch back up to the condition for the next iteration. 1371 EmitBranch(CondBlock); 1372 1373 // Emit the fall-through block. 1374 EmitBlock(AfterFor, true); 1375} 1376 1377/// EmitClassAggrCopyAssignment - This routine generates code to assign a class 1378/// array of objects from SrcValue to DestValue. Assignment can be either a 1379/// bitwise assignment or via a copy assignment operator function call. 1380/// FIXME. This can be consolidated with EmitClassAggrMemberwiseCopy 1381void CodeGenFunction::EmitClassAggrCopyAssignment(llvm::Value *Dest, 1382 llvm::Value *Src, 1383 const ArrayType *Array, 1384 const CXXRecordDecl *BaseClassDecl, 1385 QualType Ty) { 1386 const ConstantArrayType *CA = dyn_cast<ConstantArrayType>(Array); 1387 assert(CA && "VLA cannot be asssigned"); 1388 bool BitwiseAssign = BaseClassDecl->hasTrivialCopyAssignment(); 1389 1390 // Create a temporary for the loop index and initialize it with 0. 1391 llvm::Value *IndexPtr = CreateTempAlloca(llvm::Type::getInt64Ty(VMContext), 1392 "loop.index"); 1393 llvm::Value* zeroConstant = 1394 llvm::Constant::getNullValue(llvm::Type::getInt64Ty(VMContext)); 1395 Builder.CreateStore(zeroConstant, IndexPtr, false); 1396 // Start the loop with a block that tests the condition. 1397 llvm::BasicBlock *CondBlock = createBasicBlock("for.cond"); 1398 llvm::BasicBlock *AfterFor = createBasicBlock("for.end"); 1399 1400 EmitBlock(CondBlock); 1401 1402 llvm::BasicBlock *ForBody = createBasicBlock("for.body"); 1403 // Generate: if (loop-index < number-of-elements fall to the loop body, 1404 // otherwise, go to the block after the for-loop. 1405 uint64_t NumElements = getContext().getConstantArrayElementCount(CA); 1406 llvm::Value * NumElementsPtr = 1407 llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext), NumElements); 1408 llvm::Value *Counter = Builder.CreateLoad(IndexPtr); 1409 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, NumElementsPtr, 1410 "isless"); 1411 // If the condition is true, execute the body. 1412 Builder.CreateCondBr(IsLess, ForBody, AfterFor); 1413 1414 EmitBlock(ForBody); 1415 llvm::BasicBlock *ContinueBlock = createBasicBlock("for.inc"); 1416 // Inside the loop body, emit the assignment operator call on array element. 1417 Counter = Builder.CreateLoad(IndexPtr); 1418 Src = Builder.CreateInBoundsGEP(Src, Counter, "srcaddress"); 1419 Dest = Builder.CreateInBoundsGEP(Dest, Counter, "destaddress"); 1420 const CXXMethodDecl *MD = 0; 1421 if (BitwiseAssign) 1422 EmitAggregateCopy(Dest, Src, Ty); 1423 else { 1424 bool hasCopyAssign = BaseClassDecl->hasConstCopyAssignment(getContext(), 1425 MD); 1426 assert(hasCopyAssign && "EmitClassAggrCopyAssignment - No user assign"); 1427 (void)hasCopyAssign; 1428 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType(); 1429 const llvm::Type *LTy = 1430 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), 1431 FPT->isVariadic()); 1432 llvm::Constant *Callee = CGM.GetAddrOfFunction(GlobalDecl(MD), LTy); 1433 1434 CallArgList CallArgs; 1435 // Push the this (Dest) ptr. 1436 CallArgs.push_back(std::make_pair(RValue::get(Dest), 1437 MD->getThisType(getContext()))); 1438 1439 // Push the Src ptr. 1440 CallArgs.push_back(std::make_pair(RValue::get(Src), 1441 MD->getParamDecl(0)->getType())); 1442 QualType ResultType = MD->getType()->getAsFunctionType()->getResultType(); 1443 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), 1444 Callee, CallArgs, MD); 1445 } 1446 EmitBlock(ContinueBlock); 1447 1448 // Emit the increment of the loop counter. 1449 llvm::Value *NextVal = llvm::ConstantInt::get(Counter->getType(), 1); 1450 Counter = Builder.CreateLoad(IndexPtr); 1451 NextVal = Builder.CreateAdd(Counter, NextVal, "inc"); 1452 Builder.CreateStore(NextVal, IndexPtr, false); 1453 1454 // Finally, branch back up to the condition for the next iteration. 1455 EmitBranch(CondBlock); 1456 1457 // Emit the fall-through block. 1458 EmitBlock(AfterFor, true); 1459} 1460 1461/// EmitClassMemberwiseCopy - This routine generates code to copy a class 1462/// object from SrcValue to DestValue. Copying can be either a bitwise copy 1463/// or via a copy constructor call. 1464void CodeGenFunction::EmitClassMemberwiseCopy( 1465 llvm::Value *Dest, llvm::Value *Src, 1466 const CXXRecordDecl *ClassDecl, 1467 const CXXRecordDecl *BaseClassDecl, QualType Ty) { 1468 if (ClassDecl) { 1469 Dest = AddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl); 1470 Src = AddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl) ; 1471 } 1472 if (BaseClassDecl->hasTrivialCopyConstructor()) { 1473 EmitAggregateCopy(Dest, Src, Ty); 1474 return; 1475 } 1476 1477 if (CXXConstructorDecl *BaseCopyCtor = 1478 BaseClassDecl->getCopyConstructor(getContext(), 0)) { 1479 llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor, 1480 Ctor_Complete); 1481 CallArgList CallArgs; 1482 // Push the this (Dest) ptr. 1483 CallArgs.push_back(std::make_pair(RValue::get(Dest), 1484 BaseCopyCtor->getThisType(getContext()))); 1485 1486 // Push the Src ptr. 1487 CallArgs.push_back(std::make_pair(RValue::get(Src), 1488 BaseCopyCtor->getParamDecl(0)->getType())); 1489 QualType ResultType = 1490 BaseCopyCtor->getType()->getAsFunctionType()->getResultType(); 1491 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), 1492 Callee, CallArgs, BaseCopyCtor); 1493 } 1494} 1495 1496/// EmitClassCopyAssignment - This routine generates code to copy assign a class 1497/// object from SrcValue to DestValue. Assignment can be either a bitwise 1498/// assignment of via an assignment operator call. 1499// FIXME. Consolidate this with EmitClassMemberwiseCopy as they share a lot. 1500void CodeGenFunction::EmitClassCopyAssignment( 1501 llvm::Value *Dest, llvm::Value *Src, 1502 const CXXRecordDecl *ClassDecl, 1503 const CXXRecordDecl *BaseClassDecl, 1504 QualType Ty) { 1505 if (ClassDecl) { 1506 Dest = AddressCXXOfBaseClass(Dest, ClassDecl, BaseClassDecl); 1507 Src = AddressCXXOfBaseClass(Src, ClassDecl, BaseClassDecl) ; 1508 } 1509 if (BaseClassDecl->hasTrivialCopyAssignment()) { 1510 EmitAggregateCopy(Dest, Src, Ty); 1511 return; 1512 } 1513 1514 const CXXMethodDecl *MD = 0; 1515 bool ConstCopyAssignOp = BaseClassDecl->hasConstCopyAssignment(getContext(), 1516 MD); 1517 assert(ConstCopyAssignOp && "EmitClassCopyAssignment - missing copy assign"); 1518 (void)ConstCopyAssignOp; 1519 1520 const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType(); 1521 const llvm::Type *LTy = 1522 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), 1523 FPT->isVariadic()); 1524 llvm::Constant *Callee = CGM.GetAddrOfFunction(GlobalDecl(MD), LTy); 1525 1526 CallArgList CallArgs; 1527 // Push the this (Dest) ptr. 1528 CallArgs.push_back(std::make_pair(RValue::get(Dest), 1529 MD->getThisType(getContext()))); 1530 1531 // Push the Src ptr. 1532 CallArgs.push_back(std::make_pair(RValue::get(Src), 1533 MD->getParamDecl(0)->getType())); 1534 QualType ResultType = 1535 MD->getType()->getAsFunctionType()->getResultType(); 1536 EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs), 1537 Callee, CallArgs, MD); 1538} 1539 1540/// SynthesizeDefaultConstructor - synthesize a default constructor 1541void 1542CodeGenFunction::SynthesizeDefaultConstructor(const CXXConstructorDecl *CD, 1543 const FunctionDecl *FD, 1544 llvm::Function *Fn, 1545 const FunctionArgList &Args) { 1546 StartFunction(FD, FD->getResultType(), Fn, Args, SourceLocation()); 1547 EmitCtorPrologue(CD); 1548 FinishFunction(); 1549} 1550 1551/// SynthesizeCXXCopyConstructor - This routine implicitly defines body of a copy 1552/// constructor, in accordance with section 12.8 (p7 and p8) of C++03 1553/// The implicitly-defined copy constructor for class X performs a memberwise 1554/// copy of its subobjects. The order of copying is the same as the order 1555/// of initialization of bases and members in a user-defined constructor 1556/// Each subobject is copied in the manner appropriate to its type: 1557/// if the subobject is of class type, the copy constructor for the class is 1558/// used; 1559/// if the subobject is an array, each element is copied, in the manner 1560/// appropriate to the element type; 1561/// if the subobject is of scalar type, the built-in assignment operator is 1562/// used. 1563/// Virtual base class subobjects shall be copied only once by the 1564/// implicitly-defined copy constructor 1565 1566void CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *CD, 1567 const FunctionDecl *FD, 1568 llvm::Function *Fn, 1569 const FunctionArgList &Args) { 1570 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext()); 1571 assert(!ClassDecl->hasUserDeclaredCopyConstructor() && 1572 "SynthesizeCXXCopyConstructor - copy constructor has definition already"); 1573 StartFunction(FD, FD->getResultType(), Fn, Args, SourceLocation()); 1574 1575 FunctionArgList::const_iterator i = Args.begin(); 1576 const VarDecl *ThisArg = i->first; 1577 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg); 1578 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this"); 1579 const VarDecl *SrcArg = (i+1)->first; 1580 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg); 1581 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj); 1582 1583 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); 1584 Base != ClassDecl->bases_end(); ++Base) { 1585 // FIXME. copy constrution of virtual base NYI 1586 if (Base->isVirtual()) 1587 continue; 1588 1589 CXXRecordDecl *BaseClassDecl 1590 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 1591 EmitClassMemberwiseCopy(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl, 1592 Base->getType()); 1593 } 1594 1595 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), 1596 FieldEnd = ClassDecl->field_end(); 1597 Field != FieldEnd; ++Field) { 1598 QualType FieldType = getContext().getCanonicalType((*Field)->getType()); 1599 const ConstantArrayType *Array = 1600 getContext().getAsConstantArrayType(FieldType); 1601 if (Array) 1602 FieldType = getContext().getBaseElementType(FieldType); 1603 1604 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { 1605 CXXRecordDecl *FieldClassDecl 1606 = cast<CXXRecordDecl>(FieldClassType->getDecl()); 1607 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); 1608 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); 1609 if (Array) { 1610 const llvm::Type *BasePtr = ConvertType(FieldType); 1611 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1612 llvm::Value *DestBaseAddrPtr = 1613 Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1614 llvm::Value *SrcBaseAddrPtr = 1615 Builder.CreateBitCast(RHS.getAddress(), BasePtr); 1616 EmitClassAggrMemberwiseCopy(DestBaseAddrPtr, SrcBaseAddrPtr, Array, 1617 FieldClassDecl, FieldType); 1618 } 1619 else 1620 EmitClassMemberwiseCopy(LHS.getAddress(), RHS.getAddress(), 1621 0 /*ClassDecl*/, FieldClassDecl, FieldType); 1622 continue; 1623 } 1624 // Do a built-in assignment of scalar data members. 1625 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); 1626 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); 1627 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType); 1628 EmitStoreThroughLValue(RVRHS, LHS, FieldType); 1629 } 1630 FinishFunction(); 1631} 1632 1633/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator. 1634/// Before the implicitly-declared copy assignment operator for a class is 1635/// implicitly defined, all implicitly- declared copy assignment operators for 1636/// its direct base classes and its nonstatic data members shall have been 1637/// implicitly defined. [12.8-p12] 1638/// The implicitly-defined copy assignment operator for class X performs 1639/// memberwise assignment of its subob- jects. The direct base classes of X are 1640/// assigned first, in the order of their declaration in 1641/// the base-specifier-list, and then the immediate nonstatic data members of X 1642/// are assigned, in the order in which they were declared in the class 1643/// definition.Each subobject is assigned in the manner appropriate to its type: 1644/// if the subobject is of class type, the copy assignment operator for the 1645/// class is used (as if by explicit qualification; that is, ignoring any 1646/// possible virtual overriding functions in more derived classes); 1647/// 1648/// if the subobject is an array, each element is assigned, in the manner 1649/// appropriate to the element type; 1650/// 1651/// if the subobject is of scalar type, the built-in assignment operator is 1652/// used. 1653void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD, 1654 const FunctionDecl *FD, 1655 llvm::Function *Fn, 1656 const FunctionArgList &Args) { 1657 1658 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext()); 1659 assert(!ClassDecl->hasUserDeclaredCopyAssignment() && 1660 "SynthesizeCXXCopyAssignment - copy assignment has user declaration"); 1661 StartFunction(FD, FD->getResultType(), Fn, Args, SourceLocation()); 1662 1663 FunctionArgList::const_iterator i = Args.begin(); 1664 const VarDecl *ThisArg = i->first; 1665 llvm::Value *ThisObj = GetAddrOfLocalVar(ThisArg); 1666 llvm::Value *LoadOfThis = Builder.CreateLoad(ThisObj, "this"); 1667 const VarDecl *SrcArg = (i+1)->first; 1668 llvm::Value *SrcObj = GetAddrOfLocalVar(SrcArg); 1669 llvm::Value *LoadOfSrc = Builder.CreateLoad(SrcObj); 1670 1671 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); 1672 Base != ClassDecl->bases_end(); ++Base) { 1673 // FIXME. copy assignment of virtual base NYI 1674 if (Base->isVirtual()) 1675 continue; 1676 1677 CXXRecordDecl *BaseClassDecl 1678 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 1679 EmitClassCopyAssignment(LoadOfThis, LoadOfSrc, ClassDecl, BaseClassDecl, 1680 Base->getType()); 1681 } 1682 1683 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), 1684 FieldEnd = ClassDecl->field_end(); 1685 Field != FieldEnd; ++Field) { 1686 QualType FieldType = getContext().getCanonicalType((*Field)->getType()); 1687 const ConstantArrayType *Array = 1688 getContext().getAsConstantArrayType(FieldType); 1689 if (Array) 1690 FieldType = getContext().getBaseElementType(FieldType); 1691 1692 if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) { 1693 CXXRecordDecl *FieldClassDecl 1694 = cast<CXXRecordDecl>(FieldClassType->getDecl()); 1695 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); 1696 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); 1697 if (Array) { 1698 const llvm::Type *BasePtr = ConvertType(FieldType); 1699 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1700 llvm::Value *DestBaseAddrPtr = 1701 Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1702 llvm::Value *SrcBaseAddrPtr = 1703 Builder.CreateBitCast(RHS.getAddress(), BasePtr); 1704 EmitClassAggrCopyAssignment(DestBaseAddrPtr, SrcBaseAddrPtr, Array, 1705 FieldClassDecl, FieldType); 1706 } 1707 else 1708 EmitClassCopyAssignment(LHS.getAddress(), RHS.getAddress(), 1709 0 /*ClassDecl*/, FieldClassDecl, FieldType); 1710 continue; 1711 } 1712 // Do a built-in assignment of scalar data members. 1713 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); 1714 LValue RHS = EmitLValueForField(LoadOfSrc, *Field, false, 0); 1715 RValue RVRHS = EmitLoadOfLValue(RHS, FieldType); 1716 EmitStoreThroughLValue(RVRHS, LHS, FieldType); 1717 } 1718 1719 // return *this; 1720 Builder.CreateStore(LoadOfThis, ReturnValue); 1721 1722 FinishFunction(); 1723} 1724 1725/// EmitCtorPrologue - This routine generates necessary code to initialize 1726/// base classes and non-static data members belonging to this constructor. 1727/// FIXME: This needs to take a CXXCtorType. 1728void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD) { 1729 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext()); 1730 // FIXME: Add vbase initialization 1731 llvm::Value *LoadOfThis = 0; 1732 1733 for (CXXConstructorDecl::init_const_iterator B = CD->init_begin(), 1734 E = CD->init_end(); 1735 B != E; ++B) { 1736 CXXBaseOrMemberInitializer *Member = (*B); 1737 if (Member->isBaseInitializer()) { 1738 LoadOfThis = LoadCXXThis(); 1739 Type *BaseType = Member->getBaseClass(); 1740 CXXRecordDecl *BaseClassDecl = 1741 cast<CXXRecordDecl>(BaseType->getAs<RecordType>()->getDecl()); 1742 llvm::Value *V = AddressCXXOfBaseClass(LoadOfThis, ClassDecl, 1743 BaseClassDecl); 1744 EmitCXXConstructorCall(Member->getConstructor(), 1745 Ctor_Complete, V, 1746 Member->const_arg_begin(), 1747 Member->const_arg_end()); 1748 } else { 1749 // non-static data member initilaizers. 1750 FieldDecl *Field = Member->getMember(); 1751 QualType FieldType = getContext().getCanonicalType((Field)->getType()); 1752 const ConstantArrayType *Array = 1753 getContext().getAsConstantArrayType(FieldType); 1754 if (Array) 1755 FieldType = getContext().getBaseElementType(FieldType); 1756 1757 LoadOfThis = LoadCXXThis(); 1758 LValue LHS; 1759 if (FieldType->isReferenceType()) { 1760 // FIXME: This is really ugly; should be refactored somehow 1761 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field); 1762 llvm::Value *V = Builder.CreateStructGEP(LoadOfThis, idx, "tmp"); 1763 LHS = LValue::MakeAddr(V, FieldType.getCVRQualifiers(), 1764 QualType::GCNone, FieldType.getAddressSpace()); 1765 } else { 1766 LHS = EmitLValueForField(LoadOfThis, Field, false, 0); 1767 } 1768 if (FieldType->getAs<RecordType>()) { 1769 if (!Field->isAnonymousStructOrUnion()) { 1770 assert(Member->getConstructor() && 1771 "EmitCtorPrologue - no constructor to initialize member"); 1772 if (Array) { 1773 const llvm::Type *BasePtr = ConvertType(FieldType); 1774 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1775 llvm::Value *BaseAddrPtr = 1776 Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1777 EmitCXXAggrConstructorCall(Member->getConstructor(), 1778 Array, BaseAddrPtr); 1779 } 1780 else 1781 EmitCXXConstructorCall(Member->getConstructor(), 1782 Ctor_Complete, LHS.getAddress(), 1783 Member->const_arg_begin(), 1784 Member->const_arg_end()); 1785 continue; 1786 } 1787 else { 1788 // Initializing an anonymous union data member. 1789 FieldDecl *anonMember = Member->getAnonUnionMember(); 1790 LHS = EmitLValueForField(LHS.getAddress(), anonMember, 1791 /*IsUnion=*/true, 0); 1792 FieldType = anonMember->getType(); 1793 } 1794 } 1795 1796 assert(Member->getNumArgs() == 1 && "Initializer count must be 1 only"); 1797 Expr *RhsExpr = *Member->arg_begin(); 1798 RValue RHS; 1799 if (FieldType->isReferenceType()) 1800 RHS = EmitReferenceBindingToExpr(RhsExpr, FieldType, 1801 /*IsInitializer=*/true); 1802 else 1803 RHS = RValue::get(EmitScalarExpr(RhsExpr, true)); 1804 EmitStoreThroughLValue(RHS, LHS, FieldType); 1805 } 1806 } 1807 1808 if (!CD->getNumBaseOrMemberInitializers() && !CD->isTrivial()) { 1809 // Nontrivial default constructor with no initializer list. It may still 1810 // have bases classes and/or contain non-static data members which require 1811 // construction. 1812 for (CXXRecordDecl::base_class_const_iterator Base = 1813 ClassDecl->bases_begin(); 1814 Base != ClassDecl->bases_end(); ++Base) { 1815 // FIXME. copy assignment of virtual base NYI 1816 if (Base->isVirtual()) 1817 continue; 1818 1819 CXXRecordDecl *BaseClassDecl 1820 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 1821 if (BaseClassDecl->hasTrivialConstructor()) 1822 continue; 1823 if (CXXConstructorDecl *BaseCX = 1824 BaseClassDecl->getDefaultConstructor(getContext())) { 1825 LoadOfThis = LoadCXXThis(); 1826 llvm::Value *V = AddressCXXOfBaseClass(LoadOfThis, ClassDecl, 1827 BaseClassDecl); 1828 EmitCXXConstructorCall(BaseCX, Ctor_Complete, V, 0, 0); 1829 } 1830 } 1831 1832 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), 1833 FieldEnd = ClassDecl->field_end(); 1834 Field != FieldEnd; ++Field) { 1835 QualType FieldType = getContext().getCanonicalType((*Field)->getType()); 1836 const ConstantArrayType *Array = 1837 getContext().getAsConstantArrayType(FieldType); 1838 if (Array) 1839 FieldType = getContext().getBaseElementType(FieldType); 1840 if (!FieldType->getAs<RecordType>() || Field->isAnonymousStructOrUnion()) 1841 continue; 1842 const RecordType *ClassRec = FieldType->getAs<RecordType>(); 1843 CXXRecordDecl *MemberClassDecl = 1844 dyn_cast<CXXRecordDecl>(ClassRec->getDecl()); 1845 if (!MemberClassDecl || MemberClassDecl->hasTrivialConstructor()) 1846 continue; 1847 if (CXXConstructorDecl *MamberCX = 1848 MemberClassDecl->getDefaultConstructor(getContext())) { 1849 LoadOfThis = LoadCXXThis(); 1850 LValue LHS = EmitLValueForField(LoadOfThis, *Field, false, 0); 1851 if (Array) { 1852 const llvm::Type *BasePtr = ConvertType(FieldType); 1853 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1854 llvm::Value *BaseAddrPtr = 1855 Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1856 EmitCXXAggrConstructorCall(MamberCX, Array, BaseAddrPtr); 1857 } 1858 else 1859 EmitCXXConstructorCall(MamberCX, Ctor_Complete, LHS.getAddress(), 1860 0, 0); 1861 } 1862 } 1863 } 1864 1865 // Initialize the vtable pointer 1866 if (ClassDecl->isDynamicClass()) { 1867 if (!LoadOfThis) 1868 LoadOfThis = LoadCXXThis(); 1869 llvm::Value *VtableField; 1870 llvm::Type *Ptr8Ty, *PtrPtr8Ty; 1871 Ptr8Ty = llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext), 0); 1872 PtrPtr8Ty = llvm::PointerType::get(Ptr8Ty, 0); 1873 VtableField = Builder.CreateBitCast(LoadOfThis, PtrPtr8Ty); 1874 llvm::Value *vtable = GenerateVtable(ClassDecl); 1875 Builder.CreateStore(vtable, VtableField); 1876 } 1877} 1878 1879/// EmitDtorEpilogue - Emit all code that comes at the end of class's 1880/// destructor. This is to call destructors on members and base classes 1881/// in reverse order of their construction. 1882/// FIXME: This needs to take a CXXDtorType. 1883void CodeGenFunction::EmitDtorEpilogue(const CXXDestructorDecl *DD) { 1884 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(DD->getDeclContext()); 1885 assert(!ClassDecl->getNumVBases() && 1886 "FIXME: Destruction of virtual bases not supported"); 1887 (void)ClassDecl; // prevent warning. 1888 1889 for (CXXDestructorDecl::destr_const_iterator *B = DD->destr_begin(), 1890 *E = DD->destr_end(); B != E; ++B) { 1891 uintptr_t BaseOrMember = (*B); 1892 if (DD->isMemberToDestroy(BaseOrMember)) { 1893 FieldDecl *FD = DD->getMemberToDestroy(BaseOrMember); 1894 QualType FieldType = getContext().getCanonicalType((FD)->getType()); 1895 const ConstantArrayType *Array = 1896 getContext().getAsConstantArrayType(FieldType); 1897 if (Array) 1898 FieldType = getContext().getBaseElementType(FieldType); 1899 const RecordType *RT = FieldType->getAs<RecordType>(); 1900 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); 1901 if (FieldClassDecl->hasTrivialDestructor()) 1902 continue; 1903 llvm::Value *LoadOfThis = LoadCXXThis(); 1904 LValue LHS = EmitLValueForField(LoadOfThis, FD, false, 0); 1905 if (Array) { 1906 const llvm::Type *BasePtr = ConvertType(FieldType); 1907 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1908 llvm::Value *BaseAddrPtr = 1909 Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1910 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()), 1911 Array, BaseAddrPtr); 1912 } 1913 else 1914 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()), 1915 Dtor_Complete, LHS.getAddress()); 1916 } else { 1917 const RecordType *RT = 1918 DD->getAnyBaseClassToDestroy(BaseOrMember)->getAs<RecordType>(); 1919 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(RT->getDecl()); 1920 if (BaseClassDecl->hasTrivialDestructor()) 1921 continue; 1922 llvm::Value *V = AddressCXXOfBaseClass(LoadCXXThis(), 1923 ClassDecl,BaseClassDecl); 1924 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()), 1925 Dtor_Complete, V); 1926 } 1927 } 1928 if (DD->getNumBaseOrMemberDestructions() || DD->isTrivial()) 1929 return; 1930 // Case of destructor synthesis with fields and base classes 1931 // which have non-trivial destructors. They must be destructed in 1932 // reverse order of their construction. 1933 llvm::SmallVector<FieldDecl *, 16> DestructedFields; 1934 1935 for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(), 1936 FieldEnd = ClassDecl->field_end(); 1937 Field != FieldEnd; ++Field) { 1938 QualType FieldType = getContext().getCanonicalType((*Field)->getType()); 1939 if (getContext().getAsConstantArrayType(FieldType)) 1940 FieldType = getContext().getBaseElementType(FieldType); 1941 if (const RecordType *RT = FieldType->getAs<RecordType>()) { 1942 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); 1943 if (FieldClassDecl->hasTrivialDestructor()) 1944 continue; 1945 DestructedFields.push_back(*Field); 1946 } 1947 } 1948 if (!DestructedFields.empty()) 1949 for (int i = DestructedFields.size() -1; i >= 0; --i) { 1950 FieldDecl *Field = DestructedFields[i]; 1951 QualType FieldType = Field->getType(); 1952 const ConstantArrayType *Array = 1953 getContext().getAsConstantArrayType(FieldType); 1954 if (Array) 1955 FieldType = getContext().getBaseElementType(FieldType); 1956 const RecordType *RT = FieldType->getAs<RecordType>(); 1957 CXXRecordDecl *FieldClassDecl = cast<CXXRecordDecl>(RT->getDecl()); 1958 llvm::Value *LoadOfThis = LoadCXXThis(); 1959 LValue LHS = EmitLValueForField(LoadOfThis, Field, false, 0); 1960 if (Array) { 1961 const llvm::Type *BasePtr = ConvertType(FieldType); 1962 BasePtr = llvm::PointerType::getUnqual(BasePtr); 1963 llvm::Value *BaseAddrPtr = 1964 Builder.CreateBitCast(LHS.getAddress(), BasePtr); 1965 EmitCXXAggrDestructorCall(FieldClassDecl->getDestructor(getContext()), 1966 Array, BaseAddrPtr); 1967 } 1968 else 1969 EmitCXXDestructorCall(FieldClassDecl->getDestructor(getContext()), 1970 Dtor_Complete, LHS.getAddress()); 1971 } 1972 1973 llvm::SmallVector<CXXRecordDecl*, 4> DestructedBases; 1974 for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin(); 1975 Base != ClassDecl->bases_end(); ++Base) { 1976 // FIXME. copy assignment of virtual base NYI 1977 if (Base->isVirtual()) 1978 continue; 1979 1980 CXXRecordDecl *BaseClassDecl 1981 = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl()); 1982 if (BaseClassDecl->hasTrivialDestructor()) 1983 continue; 1984 DestructedBases.push_back(BaseClassDecl); 1985 } 1986 if (DestructedBases.empty()) 1987 return; 1988 for (int i = DestructedBases.size() -1; i >= 0; --i) { 1989 CXXRecordDecl *BaseClassDecl = DestructedBases[i]; 1990 llvm::Value *V = AddressCXXOfBaseClass(LoadCXXThis(), 1991 ClassDecl,BaseClassDecl); 1992 EmitCXXDestructorCall(BaseClassDecl->getDestructor(getContext()), 1993 Dtor_Complete, V); 1994 } 1995} 1996 1997void CodeGenFunction::SynthesizeDefaultDestructor(const CXXDestructorDecl *CD, 1998 const FunctionDecl *FD, 1999 llvm::Function *Fn, 2000 const FunctionArgList &Args) { 2001 2002 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext()); 2003 assert(!ClassDecl->hasUserDeclaredDestructor() && 2004 "SynthesizeDefaultDestructor - destructor has user declaration"); 2005 (void) ClassDecl; 2006 2007 StartFunction(FD, FD->getResultType(), Fn, Args, SourceLocation()); 2008 EmitDtorEpilogue(CD); 2009 FinishFunction(); 2010} 2011