CGCXX.cpp revision bc6afd158cc67b5f87dcc2d8efe2a2254af8debe
1//===--- CGDecl.cpp - Emit LLVM Code for declarations ---------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This contains code dealing with C++ code generation. 11// 12//===----------------------------------------------------------------------===// 13 14// We might split this into multiple files if it gets too unwieldy 15 16#include "CodeGenFunction.h" 17#include "CodeGenModule.h" 18#include "Mangle.h" 19#include "clang/AST/ASTContext.h" 20#include "clang/AST/RecordLayout.h" 21#include "clang/AST/Decl.h" 22#include "clang/AST/DeclCXX.h" 23#include "clang/AST/DeclObjC.h" 24#include "clang/AST/StmtCXX.h" 25#include "clang/CodeGen/CodeGenOptions.h" 26#include "llvm/ADT/StringExtras.h" 27using namespace clang; 28using namespace CodeGen; 29 30/// Determines whether the given function has a trivial body that does 31/// not require any specific codegen. 32static bool HasTrivialBody(const FunctionDecl *FD) { 33 Stmt *S = FD->getBody(); 34 if (!S) 35 return true; 36 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty()) 37 return true; 38 return false; 39} 40 41/// Try to emit a base destructor as an alias to its primary 42/// base-class destructor. 43bool CodeGenModule::TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D) { 44 if (!getCodeGenOpts().CXXCtorDtorAliases) 45 return true; 46 47 // If the destructor doesn't have a trivial body, we have to emit it 48 // separately. 49 if (!HasTrivialBody(D)) 50 return true; 51 52 const CXXRecordDecl *Class = D->getParent(); 53 54 // If we need to manipulate a VTT parameter, give up. 55 if (Class->getNumVBases()) { 56 // Extra Credit: passing extra parameters is perfectly safe 57 // in many calling conventions, so only bail out if the ctor's 58 // calling convention is nonstandard. 59 return true; 60 } 61 62 // If any fields have a non-trivial destructor, we have to emit it 63 // separately. 64 for (CXXRecordDecl::field_iterator I = Class->field_begin(), 65 E = Class->field_end(); I != E; ++I) 66 if (const RecordType *RT = (*I)->getType()->getAs<RecordType>()) 67 if (!cast<CXXRecordDecl>(RT->getDecl())->hasTrivialDestructor()) 68 return true; 69 70 // Try to find a unique base class with a non-trivial destructor. 71 const CXXRecordDecl *UniqueBase = 0; 72 for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(), 73 E = Class->bases_end(); I != E; ++I) { 74 75 // We're in the base destructor, so skip virtual bases. 76 if (I->isVirtual()) continue; 77 78 // Skip base classes with trivial destructors. 79 const CXXRecordDecl *Base 80 = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 81 if (Base->hasTrivialDestructor()) continue; 82 83 // If we've already found a base class with a non-trivial 84 // destructor, give up. 85 if (UniqueBase) return true; 86 UniqueBase = Base; 87 } 88 89 // If we didn't find any bases with a non-trivial destructor, then 90 // the base destructor is actually effectively trivial, which can 91 // happen if it was needlessly user-defined or if there are virtual 92 // bases with non-trivial destructors. 93 if (!UniqueBase) 94 return true; 95 96 /// If we don't have a definition for the destructor yet, don't 97 /// emit. We can't emit aliases to declarations; that's just not 98 /// how aliases work. 99 const CXXDestructorDecl *BaseD = UniqueBase->getDestructor(getContext()); 100 if (!BaseD->isImplicit() && !BaseD->getBody()) 101 return true; 102 103 // If the base is at a non-zero offset, give up. 104 const ASTRecordLayout &ClassLayout = Context.getASTRecordLayout(Class); 105 if (ClassLayout.getBaseClassOffset(UniqueBase) != 0) 106 return true; 107 108 return TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Base), 109 GlobalDecl(BaseD, Dtor_Base)); 110} 111 112static bool isWeakForLinker(llvm::GlobalValue::LinkageTypes Linkage) { 113 return (Linkage == llvm::GlobalValue::AvailableExternallyLinkage || 114 Linkage == llvm::GlobalValue::WeakAnyLinkage || 115 Linkage == llvm::GlobalValue::WeakODRLinkage || 116 Linkage == llvm::GlobalValue::LinkOnceAnyLinkage || 117 Linkage == llvm::GlobalValue::LinkOnceODRLinkage || 118 Linkage == llvm::GlobalValue::CommonLinkage || 119 Linkage == llvm::GlobalValue::ExternalWeakLinkage); 120} 121 122/// Try to emit a definition as a global alias for another definition. 123bool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl, 124 GlobalDecl TargetDecl) { 125 if (!getCodeGenOpts().CXXCtorDtorAliases) 126 return true; 127 128 // The alias will use the linkage of the referrent. If we can't 129 // support aliases with that linkage, fail. 130 llvm::GlobalValue::LinkageTypes Linkage 131 = getFunctionLinkage(cast<FunctionDecl>(AliasDecl.getDecl())); 132 133 switch (Linkage) { 134 // We can definitely emit aliases to definitions with external linkage. 135 case llvm::GlobalValue::ExternalLinkage: 136 case llvm::GlobalValue::ExternalWeakLinkage: 137 break; 138 139 // Same with local linkage. 140 case llvm::GlobalValue::InternalLinkage: 141 case llvm::GlobalValue::PrivateLinkage: 142 case llvm::GlobalValue::LinkerPrivateLinkage: 143 break; 144 145 // We should try to support linkonce linkages. 146 case llvm::GlobalValue::LinkOnceAnyLinkage: 147 case llvm::GlobalValue::LinkOnceODRLinkage: 148 return true; 149 150 // Other linkages will probably never be supported. 151 default: 152 return true; 153 } 154 155 llvm::GlobalValue::LinkageTypes TargetLinkage 156 = getFunctionLinkage(cast<FunctionDecl>(TargetDecl.getDecl())); 157 158 if (isWeakForLinker(TargetLinkage)) 159 return true; 160 161 // Derive the type for the alias. 162 const llvm::PointerType *AliasType 163 = getTypes().GetFunctionType(AliasDecl)->getPointerTo(); 164 165 // Find the referrent. Some aliases might require a bitcast, in 166 // which case the caller is responsible for ensuring the soundness 167 // of these semantics. 168 llvm::GlobalValue *Ref = cast<llvm::GlobalValue>(GetAddrOfGlobal(TargetDecl)); 169 llvm::Constant *Aliasee = Ref; 170 if (Ref->getType() != AliasType) 171 Aliasee = llvm::ConstantExpr::getBitCast(Ref, AliasType); 172 173 // Create the alias with no name. 174 llvm::GlobalAlias *Alias = 175 new llvm::GlobalAlias(AliasType, Linkage, "", Aliasee, &getModule()); 176 177 // Switch any previous uses to the alias. 178 const char *MangledName = getMangledName(AliasDecl); 179 llvm::GlobalValue *&Entry = GlobalDeclMap[MangledName]; 180 if (Entry) { 181 assert(Entry->isDeclaration() && "definition already exists for alias"); 182 assert(Entry->getType() == AliasType && 183 "declaration exists with different type"); 184 Entry->replaceAllUsesWith(Alias); 185 Entry->eraseFromParent(); 186 } 187 Entry = Alias; 188 189 // Finally, set up the alias with its proper name and attributes. 190 Alias->setName(MangledName); 191 SetCommonAttributes(AliasDecl.getDecl(), Alias); 192 193 return false; 194} 195 196void CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) { 197 // The constructor used for constructing this as a complete class; 198 // constucts the virtual bases, then calls the base constructor. 199 EmitGlobal(GlobalDecl(D, Ctor_Complete)); 200 201 // The constructor used for constructing this as a base class; 202 // ignores virtual bases. 203 EmitGlobal(GlobalDecl(D, Ctor_Base)); 204} 205 206void CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *D, 207 CXXCtorType Type) { 208 // The complete constructor is equivalent to the base constructor 209 // for classes with no virtual bases. Try to emit it as an alias. 210 if (Type == Ctor_Complete && 211 !D->getParent()->getNumVBases() && 212 !TryEmitDefinitionAsAlias(GlobalDecl(D, Ctor_Complete), 213 GlobalDecl(D, Ctor_Base))) 214 return; 215 216 llvm::Function *Fn = cast<llvm::Function>(GetAddrOfCXXConstructor(D, Type)); 217 218 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn); 219 220 SetFunctionDefinitionAttributes(D, Fn); 221 SetLLVMFunctionAttributesForDefinition(D, Fn); 222} 223 224llvm::GlobalValue * 225CodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *D, 226 CXXCtorType Type) { 227 const char *Name = getMangledCXXCtorName(D, Type); 228 if (llvm::GlobalValue *V = GlobalDeclMap[Name]) 229 return V; 230 231 const FunctionProtoType *FPT = D->getType()->getAs<FunctionProtoType>(); 232 const llvm::FunctionType *FTy = 233 getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type), 234 FPT->isVariadic()); 235 return cast<llvm::Function>( 236 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type))); 237} 238 239const char *CodeGenModule::getMangledCXXCtorName(const CXXConstructorDecl *D, 240 CXXCtorType Type) { 241 llvm::SmallString<256> Name; 242 getMangleContext().mangleCXXCtor(D, Type, Name); 243 244 Name += '\0'; 245 return UniqueMangledName(Name.begin(), Name.end()); 246} 247 248void CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) { 249 // The destructor in a virtual table is always a 'deleting' 250 // destructor, which calls the complete destructor and then uses the 251 // appropriate operator delete. 252 if (D->isVirtual()) 253 EmitGlobal(GlobalDecl(D, Dtor_Deleting)); 254 255 // The destructor used for destructing this as a most-derived class; 256 // call the base destructor and then destructs any virtual bases. 257 EmitGlobal(GlobalDecl(D, Dtor_Complete)); 258 259 // The destructor used for destructing this as a base class; ignores 260 // virtual bases. 261 EmitGlobal(GlobalDecl(D, Dtor_Base)); 262} 263 264void CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *D, 265 CXXDtorType Type) { 266 // The complete destructor is equivalent to the base destructor for 267 // classes with no virtual bases, so try to emit it as an alias. 268 if (Type == Dtor_Complete && 269 !D->getParent()->getNumVBases() && 270 !TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Complete), 271 GlobalDecl(D, Dtor_Base))) 272 return; 273 274 // The base destructor is equivalent to the base destructor of its 275 // base class if there is exactly one non-virtual base class with a 276 // non-trivial destructor, there are no fields with a non-trivial 277 // destructor, and the body of the destructor is trivial. 278 if (Type == Dtor_Base && !TryEmitBaseDestructorAsAlias(D)) 279 return; 280 281 llvm::Function *Fn = cast<llvm::Function>(GetAddrOfCXXDestructor(D, Type)); 282 283 CodeGenFunction(*this).GenerateCode(GlobalDecl(D, Type), Fn); 284 285 SetFunctionDefinitionAttributes(D, Fn); 286 SetLLVMFunctionAttributesForDefinition(D, Fn); 287} 288 289llvm::GlobalValue * 290CodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *D, 291 CXXDtorType Type) { 292 const char *Name = getMangledCXXDtorName(D, Type); 293 if (llvm::GlobalValue *V = GlobalDeclMap[Name]) 294 return V; 295 296 const llvm::FunctionType *FTy = 297 getTypes().GetFunctionType(getTypes().getFunctionInfo(D, Type), false); 298 299 return cast<llvm::Function>( 300 GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(D, Type))); 301} 302 303const char *CodeGenModule::getMangledCXXDtorName(const CXXDestructorDecl *D, 304 CXXDtorType Type) { 305 llvm::SmallString<256> Name; 306 getMangleContext().mangleCXXDtor(D, Type, Name); 307 308 Name += '\0'; 309 return UniqueMangledName(Name.begin(), Name.end()); 310} 311 312llvm::Constant * 313CodeGenFunction::GenerateThunk(llvm::Function *Fn, GlobalDecl GD, 314 bool Extern, 315 const ThunkAdjustment &ThisAdjustment) { 316 return GenerateCovariantThunk(Fn, GD, Extern, 317 CovariantThunkAdjustment(ThisAdjustment, 318 ThunkAdjustment())); 319} 320 321llvm::Value * 322CodeGenFunction::DynamicTypeAdjust(llvm::Value *V, 323 const ThunkAdjustment &Adjustment) { 324 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext); 325 326 const llvm::Type *OrigTy = V->getType(); 327 if (Adjustment.NonVirtual) { 328 // Do the non-virtual adjustment 329 V = Builder.CreateBitCast(V, Int8PtrTy); 330 V = Builder.CreateConstInBoundsGEP1_64(V, Adjustment.NonVirtual); 331 V = Builder.CreateBitCast(V, OrigTy); 332 } 333 334 if (!Adjustment.Virtual) 335 return V; 336 337 assert(Adjustment.Virtual % (LLVMPointerWidth / 8) == 0 && 338 "vtable entry unaligned"); 339 340 // Do the virtual this adjustment 341 const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType()); 342 const llvm::Type *PtrDiffPtrTy = PtrDiffTy->getPointerTo(); 343 344 llvm::Value *ThisVal = Builder.CreateBitCast(V, Int8PtrTy); 345 V = Builder.CreateBitCast(V, PtrDiffPtrTy->getPointerTo()); 346 V = Builder.CreateLoad(V, "vtable"); 347 348 llvm::Value *VTablePtr = V; 349 uint64_t VirtualAdjustment = Adjustment.Virtual / (LLVMPointerWidth / 8); 350 V = Builder.CreateConstInBoundsGEP1_64(VTablePtr, VirtualAdjustment); 351 V = Builder.CreateLoad(V); 352 V = Builder.CreateGEP(ThisVal, V); 353 354 return Builder.CreateBitCast(V, OrigTy); 355} 356 357llvm::Constant * 358CodeGenFunction::GenerateCovariantThunk(llvm::Function *Fn, 359 GlobalDecl GD, bool Extern, 360 const CovariantThunkAdjustment &Adjustment) { 361 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 362 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 363 QualType ResultType = FPT->getResultType(); 364 365 FunctionArgList Args; 366 ImplicitParamDecl *ThisDecl = 367 ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0, 368 MD->getThisType(getContext())); 369 Args.push_back(std::make_pair(ThisDecl, ThisDecl->getType())); 370 for (FunctionDecl::param_const_iterator i = MD->param_begin(), 371 e = MD->param_end(); 372 i != e; ++i) { 373 ParmVarDecl *D = *i; 374 Args.push_back(std::make_pair(D, D->getType())); 375 } 376 IdentifierInfo *II 377 = &CGM.getContext().Idents.get("__thunk_named_foo_"); 378 FunctionDecl *FD = FunctionDecl::Create(getContext(), 379 getContext().getTranslationUnitDecl(), 380 SourceLocation(), II, ResultType, 0, 381 Extern 382 ? FunctionDecl::Extern 383 : FunctionDecl::Static, 384 false, true); 385 StartFunction(FD, ResultType, Fn, Args, SourceLocation()); 386 387 // generate body 388 const llvm::Type *Ty = 389 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(MD), 390 FPT->isVariadic()); 391 llvm::Value *Callee = CGM.GetAddrOfFunction(GD, Ty); 392 393 CallArgList CallArgs; 394 395 bool ShouldAdjustReturnPointer = true; 396 QualType ArgType = MD->getThisType(getContext()); 397 llvm::Value *Arg = Builder.CreateLoad(LocalDeclMap[ThisDecl], "this"); 398 if (!Adjustment.ThisAdjustment.isEmpty()) { 399 // Do the this adjustment. 400 const llvm::Type *OrigTy = Callee->getType(); 401 Arg = DynamicTypeAdjust(Arg, Adjustment.ThisAdjustment); 402 403 if (!Adjustment.ReturnAdjustment.isEmpty()) { 404 const CovariantThunkAdjustment &ReturnAdjustment = 405 CovariantThunkAdjustment(ThunkAdjustment(), 406 Adjustment.ReturnAdjustment); 407 408 Callee = CGM.BuildCovariantThunk(GD, Extern, ReturnAdjustment); 409 410 Callee = Builder.CreateBitCast(Callee, OrigTy); 411 ShouldAdjustReturnPointer = false; 412 } 413 } 414 415 CallArgs.push_back(std::make_pair(RValue::get(Arg), ArgType)); 416 417 for (FunctionDecl::param_const_iterator i = MD->param_begin(), 418 e = MD->param_end(); 419 i != e; ++i) { 420 ParmVarDecl *D = *i; 421 QualType ArgType = D->getType(); 422 423 // llvm::Value *Arg = CGF.GetAddrOfLocalVar(Dst); 424 Expr *Arg = new (getContext()) DeclRefExpr(D, ArgType.getNonReferenceType(), 425 SourceLocation()); 426 CallArgs.push_back(std::make_pair(EmitCallArg(Arg, ArgType), ArgType)); 427 } 428 429 RValue RV = EmitCall(CGM.getTypes().getFunctionInfo(ResultType, CallArgs, 430 FPT->getCallConv(), 431 FPT->getNoReturnAttr()), 432 Callee, ReturnValueSlot(), CallArgs, MD); 433 if (ShouldAdjustReturnPointer && !Adjustment.ReturnAdjustment.isEmpty()) { 434 bool CanBeZero = !(ResultType->isReferenceType() 435 // FIXME: attr nonnull can't be zero either 436 /* || ResultType->hasAttr<NonNullAttr>() */ ); 437 // Do the return result adjustment. 438 if (CanBeZero) { 439 llvm::BasicBlock *NonZeroBlock = createBasicBlock(); 440 llvm::BasicBlock *ZeroBlock = createBasicBlock(); 441 llvm::BasicBlock *ContBlock = createBasicBlock(); 442 443 const llvm::Type *Ty = RV.getScalarVal()->getType(); 444 llvm::Value *Zero = llvm::Constant::getNullValue(Ty); 445 Builder.CreateCondBr(Builder.CreateICmpNE(RV.getScalarVal(), Zero), 446 NonZeroBlock, ZeroBlock); 447 EmitBlock(NonZeroBlock); 448 llvm::Value *NZ = 449 DynamicTypeAdjust(RV.getScalarVal(), Adjustment.ReturnAdjustment); 450 EmitBranch(ContBlock); 451 EmitBlock(ZeroBlock); 452 llvm::Value *Z = RV.getScalarVal(); 453 EmitBlock(ContBlock); 454 llvm::PHINode *RVOrZero = Builder.CreatePHI(Ty); 455 RVOrZero->reserveOperandSpace(2); 456 RVOrZero->addIncoming(NZ, NonZeroBlock); 457 RVOrZero->addIncoming(Z, ZeroBlock); 458 RV = RValue::get(RVOrZero); 459 } else 460 RV = RValue::get(DynamicTypeAdjust(RV.getScalarVal(), 461 Adjustment.ReturnAdjustment)); 462 } 463 464 if (!ResultType->isVoidType()) 465 EmitReturnOfRValue(RV, ResultType); 466 467 FinishFunction(); 468 return Fn; 469} 470 471llvm::Constant * 472CodeGenModule::GetAddrOfThunk(GlobalDecl GD, 473 const ThunkAdjustment &ThisAdjustment) { 474 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 475 476 // Compute mangled name 477 llvm::SmallString<256> OutName; 478 if (const CXXDestructorDecl* DD = dyn_cast<CXXDestructorDecl>(MD)) 479 getMangleContext().mangleCXXDtorThunk(DD, GD.getDtorType(), ThisAdjustment, 480 OutName); 481 else 482 getMangleContext().mangleThunk(MD, ThisAdjustment, OutName); 483 OutName += '\0'; 484 const char* Name = UniqueMangledName(OutName.begin(), OutName.end()); 485 486 // Get function for mangled name 487 const llvm::Type *Ty = getTypes().GetFunctionTypeForVtable(MD); 488 return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl()); 489} 490 491llvm::Constant * 492CodeGenModule::GetAddrOfCovariantThunk(GlobalDecl GD, 493 const CovariantThunkAdjustment &Adjustment) { 494 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 495 496 // Compute mangled name 497 llvm::SmallString<256> OutName; 498 getMangleContext().mangleCovariantThunk(MD, Adjustment, OutName); 499 OutName += '\0'; 500 const char* Name = UniqueMangledName(OutName.begin(), OutName.end()); 501 502 // Get function for mangled name 503 const llvm::Type *Ty = getTypes().GetFunctionTypeForVtable(MD); 504 return GetOrCreateLLVMFunction(Name, Ty, GlobalDecl()); 505} 506 507void CodeGenModule::BuildThunksForVirtual(GlobalDecl GD) { 508 CGVtableInfo::AdjustmentVectorTy *AdjPtr = getVtableInfo().getAdjustments(GD); 509 if (!AdjPtr) 510 return; 511 CGVtableInfo::AdjustmentVectorTy &Adj = *AdjPtr; 512 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 513 for (unsigned i = 0; i < Adj.size(); i++) { 514 GlobalDecl OGD = Adj[i].first; 515 const CXXMethodDecl *OMD = cast<CXXMethodDecl>(OGD.getDecl()); 516 QualType nc_oret = OMD->getType()->getAs<FunctionType>()->getResultType(); 517 CanQualType oret = getContext().getCanonicalType(nc_oret); 518 QualType nc_ret = MD->getType()->getAs<FunctionType>()->getResultType(); 519 CanQualType ret = getContext().getCanonicalType(nc_ret); 520 ThunkAdjustment ReturnAdjustment; 521 if (oret != ret) { 522 QualType qD = nc_ret->getPointeeType(); 523 QualType qB = nc_oret->getPointeeType(); 524 CXXRecordDecl *D = cast<CXXRecordDecl>(qD->getAs<RecordType>()->getDecl()); 525 CXXRecordDecl *B = cast<CXXRecordDecl>(qB->getAs<RecordType>()->getDecl()); 526 ReturnAdjustment = ComputeThunkAdjustment(D, B); 527 } 528 ThunkAdjustment ThisAdjustment = Adj[i].second; 529 bool Extern = !cast<CXXRecordDecl>(OMD->getDeclContext())->isInAnonymousNamespace(); 530 if (!ReturnAdjustment.isEmpty() || !ThisAdjustment.isEmpty()) { 531 CovariantThunkAdjustment CoAdj(ThisAdjustment, ReturnAdjustment); 532 llvm::Constant *FnConst; 533 if (!ReturnAdjustment.isEmpty()) 534 FnConst = GetAddrOfCovariantThunk(GD, CoAdj); 535 else 536 FnConst = GetAddrOfThunk(GD, ThisAdjustment); 537 if (!isa<llvm::Function>(FnConst)) { 538 llvm::Constant *SubExpr = 539 cast<llvm::ConstantExpr>(FnConst)->getOperand(0); 540 llvm::Function *OldFn = cast<llvm::Function>(SubExpr); 541 std::string Name = OldFn->getNameStr(); 542 GlobalDeclMap.erase(UniqueMangledName(Name.data(), 543 Name.data() + Name.size() + 1)); 544 llvm::Constant *NewFnConst; 545 if (!ReturnAdjustment.isEmpty()) 546 NewFnConst = GetAddrOfCovariantThunk(GD, CoAdj); 547 else 548 NewFnConst = GetAddrOfThunk(GD, ThisAdjustment); 549 llvm::Function *NewFn = cast<llvm::Function>(NewFnConst); 550 NewFn->takeName(OldFn); 551 llvm::Constant *NewPtrForOldDecl = 552 llvm::ConstantExpr::getBitCast(NewFn, OldFn->getType()); 553 OldFn->replaceAllUsesWith(NewPtrForOldDecl); 554 OldFn->eraseFromParent(); 555 FnConst = NewFn; 556 } 557 llvm::Function *Fn = cast<llvm::Function>(FnConst); 558 if (Fn->isDeclaration()) { 559 llvm::GlobalVariable::LinkageTypes linktype; 560 linktype = llvm::GlobalValue::WeakAnyLinkage; 561 if (!Extern) 562 linktype = llvm::GlobalValue::InternalLinkage; 563 Fn->setLinkage(linktype); 564 if (!Features.Exceptions && !Features.ObjCNonFragileABI) 565 Fn->addFnAttr(llvm::Attribute::NoUnwind); 566 Fn->setAlignment(2); 567 CodeGenFunction(*this).GenerateCovariantThunk(Fn, GD, Extern, CoAdj); 568 } 569 } 570 } 571} 572 573llvm::Constant * 574CodeGenModule::BuildThunk(GlobalDecl GD, bool Extern, 575 const ThunkAdjustment &ThisAdjustment) { 576 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 577 llvm::SmallString<256> OutName; 578 if (const CXXDestructorDecl *D = dyn_cast<CXXDestructorDecl>(MD)) { 579 getMangleContext().mangleCXXDtorThunk(D, GD.getDtorType(), ThisAdjustment, 580 OutName); 581 } else 582 getMangleContext().mangleThunk(MD, ThisAdjustment, OutName); 583 584 llvm::GlobalVariable::LinkageTypes linktype; 585 linktype = llvm::GlobalValue::WeakAnyLinkage; 586 if (!Extern) 587 linktype = llvm::GlobalValue::InternalLinkage; 588 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); 589 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 590 const llvm::FunctionType *FTy = 591 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD), 592 FPT->isVariadic()); 593 594 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(), 595 &getModule()); 596 CodeGenFunction(*this).GenerateThunk(Fn, GD, Extern, ThisAdjustment); 597 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); 598 return m; 599} 600 601llvm::Constant * 602CodeGenModule::BuildCovariantThunk(const GlobalDecl &GD, bool Extern, 603 const CovariantThunkAdjustment &Adjustment) { 604 const CXXMethodDecl *MD = cast<CXXMethodDecl>(GD.getDecl()); 605 llvm::SmallString<256> OutName; 606 getMangleContext().mangleCovariantThunk(MD, Adjustment, OutName); 607 llvm::GlobalVariable::LinkageTypes linktype; 608 linktype = llvm::GlobalValue::WeakAnyLinkage; 609 if (!Extern) 610 linktype = llvm::GlobalValue::InternalLinkage; 611 llvm::Type *Ptr8Ty=llvm::PointerType::get(llvm::Type::getInt8Ty(VMContext),0); 612 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>(); 613 const llvm::FunctionType *FTy = 614 getTypes().GetFunctionType(getTypes().getFunctionInfo(MD), 615 FPT->isVariadic()); 616 617 llvm::Function *Fn = llvm::Function::Create(FTy, linktype, OutName.str(), 618 &getModule()); 619 CodeGenFunction(*this).GenerateCovariantThunk(Fn, MD, Extern, Adjustment); 620 llvm::Constant *m = llvm::ConstantExpr::getBitCast(Fn, Ptr8Ty); 621 return m; 622} 623 624static llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, uint64_t VtableIndex, 625 llvm::Value *This, const llvm::Type *Ty) { 626 Ty = Ty->getPointerTo()->getPointerTo()->getPointerTo(); 627 628 llvm::Value *Vtable = CGF.Builder.CreateBitCast(This, Ty); 629 Vtable = CGF.Builder.CreateLoad(Vtable); 630 631 llvm::Value *VFuncPtr = 632 CGF.Builder.CreateConstInBoundsGEP1_64(Vtable, VtableIndex, "vfn"); 633 return CGF.Builder.CreateLoad(VFuncPtr); 634} 635 636llvm::Value * 637CodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This, 638 const llvm::Type *Ty) { 639 MD = MD->getCanonicalDecl(); 640 uint64_t VtableIndex = CGM.getVtableInfo().getMethodVtableIndex(MD); 641 642 return ::BuildVirtualCall(*this, VtableIndex, This, Ty); 643} 644 645llvm::Value * 646CodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type, 647 llvm::Value *&This, const llvm::Type *Ty) { 648 DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl()); 649 uint64_t VtableIndex = 650 CGM.getVtableInfo().getMethodVtableIndex(GlobalDecl(DD, Type)); 651 652 return ::BuildVirtualCall(*this, VtableIndex, This, Ty); 653} 654