1daaf1de7c5f975b77798c551aaefeb36349e5ca7Devang Patel//===--- CGCXX.cpp - Emit LLVM Code for declarations ----------------------===// 2e1b29efab32d02e114046d33cca242a88585bf8aAnders Carlsson// 3e1b29efab32d02e114046d33cca242a88585bf8aAnders Carlsson// The LLVM Compiler Infrastructure 4e1b29efab32d02e114046d33cca242a88585bf8aAnders Carlsson// 5e1b29efab32d02e114046d33cca242a88585bf8aAnders Carlsson// This file is distributed under the University of Illinois Open Source 6e1b29efab32d02e114046d33cca242a88585bf8aAnders Carlsson// License. See LICENSE.TXT for details. 7e1b29efab32d02e114046d33cca242a88585bf8aAnders Carlsson// 8e1b29efab32d02e114046d33cca242a88585bf8aAnders Carlsson//===----------------------------------------------------------------------===// 9e1b29efab32d02e114046d33cca242a88585bf8aAnders Carlsson// 10e1b29efab32d02e114046d33cca242a88585bf8aAnders Carlsson// This contains code dealing with C++ code generation. 11e1b29efab32d02e114046d33cca242a88585bf8aAnders Carlsson// 12e1b29efab32d02e114046d33cca242a88585bf8aAnders Carlsson//===----------------------------------------------------------------------===// 13e1b29efab32d02e114046d33cca242a88585bf8aAnders Carlsson 141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump// We might split this into multiple files if it gets too unwieldy 15e1b29efab32d02e114046d33cca242a88585bf8aAnders Carlsson 163a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis#include "CGCXXABI.h" 17e1b29efab32d02e114046d33cca242a88585bf8aAnders Carlsson#include "CodeGenFunction.h" 18e1b29efab32d02e114046d33cca242a88585bf8aAnders Carlsson#include "CodeGenModule.h" 19e1b29efab32d02e114046d33cca242a88585bf8aAnders Carlsson#include "clang/AST/ASTContext.h" 20742cd1b7bb86b52b23b335d47abbd842dac0e1bfFariborz Jahanian#include "clang/AST/RecordLayout.h" 21e1b29efab32d02e114046d33cca242a88585bf8aAnders Carlsson#include "clang/AST/Decl.h" 22774e7c6881ee6cb970cd42239d700dce87ed402aAnders Carlsson#include "clang/AST/DeclCXX.h" 2386e9644199d91a33d0090395395bc718bd3a4981Anders Carlsson#include "clang/AST/DeclObjC.h" 2414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#include "clang/AST/Mangle.h" 256815e941998659a55c20c147861b0f437928c3d8Anders Carlsson#include "clang/AST/StmtCXX.h" 2606057cef0bcd7804e80f3ce2bbe352178396c715Chandler Carruth#include "clang/Frontend/CodeGenOptions.h" 27e1b29efab32d02e114046d33cca242a88585bf8aAnders Carlsson#include "llvm/ADT/StringExtras.h" 28e1b29efab32d02e114046d33cca242a88585bf8aAnders Carlssonusing namespace clang; 29e1b29efab32d02e114046d33cca242a88585bf8aAnders Carlssonusing namespace CodeGen; 30e1b29efab32d02e114046d33cca242a88585bf8aAnders Carlsson 31c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall/// Try to emit a base destructor as an alias to its primary 32c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall/// base-class destructor. 33c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCallbool CodeGenModule::TryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D) { 34d46f98573ba104eda102dd3224b2dca69f1c6336John McCall if (!getCodeGenOpts().CXXCtorDtorAliases) 35d46f98573ba104eda102dd3224b2dca69f1c6336John McCall return true; 36d46f98573ba104eda102dd3224b2dca69f1c6336John McCall 37c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // If the destructor doesn't have a trivial body, we have to emit it 38c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // separately. 39ffb945ffb5d29b80fd93649c3572b6d87abce3fcAnders Carlsson if (!D->hasTrivialBody()) 40c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall return true; 41d46f98573ba104eda102dd3224b2dca69f1c6336John McCall 42c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall const CXXRecordDecl *Class = D->getParent(); 43c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall 44c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // If we need to manipulate a VTT parameter, give up. 45c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall if (Class->getNumVBases()) { 46c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // Extra Credit: passing extra parameters is perfectly safe 47c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // in many calling conventions, so only bail out if the ctor's 48c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // calling convention is nonstandard. 49c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall return true; 50c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall } 51c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall 520d70d71ccbc4f7f59cadb759f61b7172a149676cJohn McCall // If any field has a non-trivial destructor, we have to emit the 530d70d71ccbc4f7f59cadb759f61b7172a149676cJohn McCall // destructor separately. 54c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall for (CXXRecordDecl::field_iterator I = Class->field_begin(), 55c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall E = Class->field_end(); I != E; ++I) 56262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie if (I->getType().isDestructedType()) 570d70d71ccbc4f7f59cadb759f61b7172a149676cJohn McCall return true; 58c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall 59c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // Try to find a unique base class with a non-trivial destructor. 60c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall const CXXRecordDecl *UniqueBase = 0; 61c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall for (CXXRecordDecl::base_class_const_iterator I = Class->bases_begin(), 62c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall E = Class->bases_end(); I != E; ++I) { 63c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall 64c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // We're in the base destructor, so skip virtual bases. 65c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall if (I->isVirtual()) continue; 66c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall 67c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // Skip base classes with trivial destructors. 68c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall const CXXRecordDecl *Base 69c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall = cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl()); 70c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall if (Base->hasTrivialDestructor()) continue; 71c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall 72c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // If we've already found a base class with a non-trivial 73c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // destructor, give up. 74c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall if (UniqueBase) return true; 75c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall UniqueBase = Base; 76d46f98573ba104eda102dd3224b2dca69f1c6336John McCall } 77d46f98573ba104eda102dd3224b2dca69f1c6336John McCall 78c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // If we didn't find any bases with a non-trivial destructor, then 79c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // the base destructor is actually effectively trivial, which can 80c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // happen if it was needlessly user-defined or if there are virtual 81c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // bases with non-trivial destructors. 82c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall if (!UniqueBase) 83c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall return true; 84c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall 859a70846c5ffd5ff5cce60de49cd7b312146bf502John McCall /// If we don't have a definition for the destructor yet, don't 869a70846c5ffd5ff5cce60de49cd7b312146bf502John McCall /// emit. We can't emit aliases to declarations; that's just not 879a70846c5ffd5ff5cce60de49cd7b312146bf502John McCall /// how aliases work. 881d110e05e0ff48c1c7a483d6b7fd094cdf28316aDouglas Gregor const CXXDestructorDecl *BaseD = UniqueBase->getDestructor(); 8906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis if (!BaseD->isImplicit() && !BaseD->hasBody()) 909a70846c5ffd5ff5cce60de49cd7b312146bf502John McCall return true; 919a70846c5ffd5ff5cce60de49cd7b312146bf502John McCall 92c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // If the base is at a non-zero offset, give up. 93c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall const ASTRecordLayout &ClassLayout = Context.getASTRecordLayout(Class); 94d4f5198ae07d9a4958d8191bac694ded12173ad9Benjamin Kramer if (!ClassLayout.getBaseClassOffset(UniqueBase).isZero()) 95c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall return true; 96c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall 97c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall return TryEmitDefinitionAsAlias(GlobalDecl(D, Dtor_Base), 98c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall GlobalDecl(BaseD, Dtor_Base)); 99c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall} 100c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall 101c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall/// Try to emit a definition as a global alias for another definition. 102c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCallbool CodeGenModule::TryEmitDefinitionAsAlias(GlobalDecl AliasDecl, 103c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall GlobalDecl TargetDecl) { 104c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall if (!getCodeGenOpts().CXXCtorDtorAliases) 105c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall return true; 106c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall 107d46f98573ba104eda102dd3224b2dca69f1c6336John McCall // The alias will use the linkage of the referrent. If we can't 108d46f98573ba104eda102dd3224b2dca69f1c6336John McCall // support aliases with that linkage, fail. 109d46f98573ba104eda102dd3224b2dca69f1c6336John McCall llvm::GlobalValue::LinkageTypes Linkage 110d46f98573ba104eda102dd3224b2dca69f1c6336John McCall = getFunctionLinkage(cast<FunctionDecl>(AliasDecl.getDecl())); 111d46f98573ba104eda102dd3224b2dca69f1c6336John McCall 112d46f98573ba104eda102dd3224b2dca69f1c6336John McCall switch (Linkage) { 113d46f98573ba104eda102dd3224b2dca69f1c6336John McCall // We can definitely emit aliases to definitions with external linkage. 114d46f98573ba104eda102dd3224b2dca69f1c6336John McCall case llvm::GlobalValue::ExternalLinkage: 115d46f98573ba104eda102dd3224b2dca69f1c6336John McCall case llvm::GlobalValue::ExternalWeakLinkage: 116d46f98573ba104eda102dd3224b2dca69f1c6336John McCall break; 117d46f98573ba104eda102dd3224b2dca69f1c6336John McCall 118d46f98573ba104eda102dd3224b2dca69f1c6336John McCall // Same with local linkage. 119d46f98573ba104eda102dd3224b2dca69f1c6336John McCall case llvm::GlobalValue::InternalLinkage: 120d46f98573ba104eda102dd3224b2dca69f1c6336John McCall case llvm::GlobalValue::PrivateLinkage: 121d46f98573ba104eda102dd3224b2dca69f1c6336John McCall case llvm::GlobalValue::LinkerPrivateLinkage: 122d46f98573ba104eda102dd3224b2dca69f1c6336John McCall break; 123d46f98573ba104eda102dd3224b2dca69f1c6336John McCall 124d46f98573ba104eda102dd3224b2dca69f1c6336John McCall // We should try to support linkonce linkages. 125d46f98573ba104eda102dd3224b2dca69f1c6336John McCall case llvm::GlobalValue::LinkOnceAnyLinkage: 126d46f98573ba104eda102dd3224b2dca69f1c6336John McCall case llvm::GlobalValue::LinkOnceODRLinkage: 127d46f98573ba104eda102dd3224b2dca69f1c6336John McCall return true; 128d46f98573ba104eda102dd3224b2dca69f1c6336John McCall 129d46f98573ba104eda102dd3224b2dca69f1c6336John McCall // Other linkages will probably never be supported. 130d46f98573ba104eda102dd3224b2dca69f1c6336John McCall default: 131d46f98573ba104eda102dd3224b2dca69f1c6336John McCall return true; 132d46f98573ba104eda102dd3224b2dca69f1c6336John McCall } 133d46f98573ba104eda102dd3224b2dca69f1c6336John McCall 134bc6afd158cc67b5f87dcc2d8efe2a2254af8debeRafael Espindola llvm::GlobalValue::LinkageTypes TargetLinkage 135bc6afd158cc67b5f87dcc2d8efe2a2254af8debeRafael Espindola = getFunctionLinkage(cast<FunctionDecl>(TargetDecl.getDecl())); 136bc6afd158cc67b5f87dcc2d8efe2a2254af8debeRafael Espindola 1373c15745b4baaa0c075418cfbe9f1be19c3a59c45Rafael Espindola if (llvm::GlobalValue::isWeakForLinker(TargetLinkage)) 138bc6afd158cc67b5f87dcc2d8efe2a2254af8debeRafael Espindola return true; 139bc6afd158cc67b5f87dcc2d8efe2a2254af8debeRafael Espindola 140c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // Derive the type for the alias. 1412acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::PointerType *AliasType 142c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall = getTypes().GetFunctionType(AliasDecl)->getPointerTo(); 143c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall 144c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // Find the referrent. Some aliases might require a bitcast, in 145c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // which case the caller is responsible for ensuring the soundness 146c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // of these semantics. 147c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall llvm::GlobalValue *Ref = cast<llvm::GlobalValue>(GetAddrOfGlobal(TargetDecl)); 148c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall llvm::Constant *Aliasee = Ref; 149c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall if (Ref->getType() != AliasType) 150c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall Aliasee = llvm::ConstantExpr::getBitCast(Ref, AliasType); 151c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall 152d46f98573ba104eda102dd3224b2dca69f1c6336John McCall // Create the alias with no name. 153d46f98573ba104eda102dd3224b2dca69f1c6336John McCall llvm::GlobalAlias *Alias = 154c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall new llvm::GlobalAlias(AliasType, Linkage, "", Aliasee, &getModule()); 155d46f98573ba104eda102dd3224b2dca69f1c6336John McCall 1561962beee04d3635b12cf6254a53b40ec6c50098dJohn McCall // Switch any previous uses to the alias. 1575f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef MangledName = getMangledName(AliasDecl); 158f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall llvm::GlobalValue *Entry = GetGlobalValue(MangledName); 159d46f98573ba104eda102dd3224b2dca69f1c6336John McCall if (Entry) { 1601962beee04d3635b12cf6254a53b40ec6c50098dJohn McCall assert(Entry->isDeclaration() && "definition already exists for alias"); 1611962beee04d3635b12cf6254a53b40ec6c50098dJohn McCall assert(Entry->getType() == AliasType && 1621962beee04d3635b12cf6254a53b40ec6c50098dJohn McCall "declaration exists with different type"); 163f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall Alias->takeName(Entry); 164d46f98573ba104eda102dd3224b2dca69f1c6336John McCall Entry->replaceAllUsesWith(Alias); 165d46f98573ba104eda102dd3224b2dca69f1c6336John McCall Entry->eraseFromParent(); 166f746aa6a8f538be914173a4aef2d9a2fd9f99d17John McCall } else { 1679a20d55807cc2f6534a9c51a46cc8143ed16786dAnders Carlsson Alias->setName(MangledName); 168d46f98573ba104eda102dd3224b2dca69f1c6336John McCall } 169d46f98573ba104eda102dd3224b2dca69f1c6336John McCall 170d46f98573ba104eda102dd3224b2dca69f1c6336John McCall // Finally, set up the alias with its proper name and attributes. 1711fb0caaa7bef765b85972274e3b434af2572c141John McCall SetCommonAttributes(cast<NamedDecl>(AliasDecl.getDecl()), Alias); 172d46f98573ba104eda102dd3224b2dca69f1c6336John McCall 173d46f98573ba104eda102dd3224b2dca69f1c6336John McCall return false; 174d46f98573ba104eda102dd3224b2dca69f1c6336John McCall} 175b9de2c55b2a33776e2bee8ee57df7599b374c8a5Anders Carlsson 17695d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlssonvoid CodeGenModule::EmitCXXConstructors(const CXXConstructorDecl *D) { 177d46f98573ba104eda102dd3224b2dca69f1c6336John McCall // The constructor used for constructing this as a complete class; 178d46f98573ba104eda102dd3224b2dca69f1c6336John McCall // constucts the virtual bases, then calls the base constructor. 179492bafc66bdd22128327aab11804db476688b44eJohn McCall if (!D->getParent()->isAbstract()) { 1808e0397a39eaf9db83cbd9a3a459893fd12a3a05eAnders Carlsson // We don't need to emit the complete ctor if the class is abstract. 1818e0397a39eaf9db83cbd9a3a459893fd12a3a05eAnders Carlsson EmitGlobal(GlobalDecl(D, Ctor_Complete)); 1828e0397a39eaf9db83cbd9a3a459893fd12a3a05eAnders Carlsson } 183d46f98573ba104eda102dd3224b2dca69f1c6336John McCall 184d46f98573ba104eda102dd3224b2dca69f1c6336John McCall // The constructor used for constructing this as a base class; 185d46f98573ba104eda102dd3224b2dca69f1c6336John McCall // ignores virtual bases. 1868e51a1f5da6ef4a1a168d14116c6eed3a578a263John McCall EmitGlobal(GlobalDecl(D, Ctor_Base)); 18795d4e5d2f87a0f07fb143ccb824dfc4c5c595c78Anders Carlsson} 188363c184139e26ea38223b477ad64ee67b22bb9a7Anders Carlsson 1891f6f961293da9c2b1c23da2411c1b439a9502ed0John McCallvoid CodeGenModule::EmitCXXConstructor(const CXXConstructorDecl *ctor, 1901f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall CXXCtorType ctorType) { 191d46f98573ba104eda102dd3224b2dca69f1c6336John McCall // The complete constructor is equivalent to the base constructor 192d46f98573ba104eda102dd3224b2dca69f1c6336John McCall // for classes with no virtual bases. Try to emit it as an alias. 1931f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall if (ctorType == Ctor_Complete && 1941f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall !ctor->getParent()->getNumVBases() && 1951f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall !TryEmitDefinitionAsAlias(GlobalDecl(ctor, Ctor_Complete), 1961f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall GlobalDecl(ctor, Ctor_Base))) 197d46f98573ba104eda102dd3224b2dca69f1c6336John McCall return; 1981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 199de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall const CGFunctionInfo &fnInfo = 200de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall getTypes().arrangeCXXConstructorDeclaration(ctor, ctorType); 201d26bc76c98006609002d9930f8840490e88ac5b5John McCall 2021f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall llvm::Function *fn = 2031f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall cast<llvm::Function>(GetAddrOfCXXConstructor(ctor, ctorType, &fnInfo)); 2041f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall setFunctionLinkage(ctor, fn); 2051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2061f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall CodeGenFunction(*this).GenerateCode(GlobalDecl(ctor, ctorType), fn, fnInfo); 2071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2081f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall SetFunctionDefinitionAttributes(ctor, fn); 2091f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall SetLLVMFunctionAttributesForDefinition(ctor, fn); 21027ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson} 21127ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson 212d46f98573ba104eda102dd3224b2dca69f1c6336John McCallllvm::GlobalValue * 2131f6f961293da9c2b1c23da2411c1b439a9502ed0John McCallCodeGenModule::GetAddrOfCXXConstructor(const CXXConstructorDecl *ctor, 2141f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall CXXCtorType ctorType, 2151f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall const CGFunctionInfo *fnInfo) { 2161f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall GlobalDecl GD(ctor, ctorType); 217dc709a8aea1072f11080f3a9519fd253582bf973Anders Carlsson 2185f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef name = getMangledName(GD); 2191f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall if (llvm::GlobalValue *existing = GetGlobalValue(name)) 2201f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall return existing; 2211f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall 222de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall if (!fnInfo) 223de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall fnInfo = &getTypes().arrangeCXXConstructorDeclaration(ctor, ctorType); 2241f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall 225de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall llvm::FunctionType *fnType = getTypes().GetFunctionType(*fnInfo); 2261f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD, 2271faa89f9c619e4b2411fab4af7e22ee7a2bd9009Anders Carlsson /*ForVTable=*/false)); 228363c184139e26ea38223b477ad64ee67b22bb9a7Anders Carlsson} 22927ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson 23027ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlssonvoid CodeGenModule::EmitCXXDestructors(const CXXDestructorDecl *D) { 231d46f98573ba104eda102dd3224b2dca69f1c6336John McCall // The destructor in a virtual table is always a 'deleting' 232d46f98573ba104eda102dd3224b2dca69f1c6336John McCall // destructor, which calls the complete destructor and then uses the 233d46f98573ba104eda102dd3224b2dca69f1c6336John McCall // appropriate operator delete. 234ea9a20834cf9311fdf758cfbd73b8daa8e655f15Eli Friedman if (D->isVirtual()) 235624c7d704dc9f1dbb5dd3e5a0ef7eacfb3b699e2Eli Friedman EmitGlobal(GlobalDecl(D, Dtor_Deleting)); 236d46f98573ba104eda102dd3224b2dca69f1c6336John McCall 237d46f98573ba104eda102dd3224b2dca69f1c6336John McCall // The destructor used for destructing this as a most-derived class; 238d46f98573ba104eda102dd3224b2dca69f1c6336John McCall // call the base destructor and then destructs any virtual bases. 2393e23d68e2e642db107b78aac2bb27585ed0ef337Douglas Gregor EmitGlobal(GlobalDecl(D, Dtor_Complete)); 240d46f98573ba104eda102dd3224b2dca69f1c6336John McCall 241d46f98573ba104eda102dd3224b2dca69f1c6336John McCall // The destructor used for destructing this as a base class; ignores 242d46f98573ba104eda102dd3224b2dca69f1c6336John McCall // virtual bases. 2438e51a1f5da6ef4a1a168d14116c6eed3a578a263John McCall EmitGlobal(GlobalDecl(D, Dtor_Base)); 24427ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson} 24527ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson 2461f6f961293da9c2b1c23da2411c1b439a9502ed0John McCallvoid CodeGenModule::EmitCXXDestructor(const CXXDestructorDecl *dtor, 2471f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall CXXDtorType dtorType) { 248d46f98573ba104eda102dd3224b2dca69f1c6336John McCall // The complete destructor is equivalent to the base destructor for 249d46f98573ba104eda102dd3224b2dca69f1c6336John McCall // classes with no virtual bases, so try to emit it as an alias. 2501f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall if (dtorType == Dtor_Complete && 2511f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall !dtor->getParent()->getNumVBases() && 2521f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall !TryEmitDefinitionAsAlias(GlobalDecl(dtor, Dtor_Complete), 2531f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall GlobalDecl(dtor, Dtor_Base))) 254d46f98573ba104eda102dd3224b2dca69f1c6336John McCall return; 255d46f98573ba104eda102dd3224b2dca69f1c6336John McCall 256c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // The base destructor is equivalent to the base destructor of its 257c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // base class if there is exactly one non-virtual base class with a 258c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // non-trivial destructor, there are no fields with a non-trivial 259c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall // destructor, and the body of the destructor is trivial. 2601f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall if (dtorType == Dtor_Base && !TryEmitBaseDestructorAsAlias(dtor)) 261c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall return; 262c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall 263de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall const CGFunctionInfo &fnInfo = 264de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall getTypes().arrangeCXXDestructor(dtor, dtorType); 265d26bc76c98006609002d9930f8840490e88ac5b5John McCall 2661f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall llvm::Function *fn = 2671f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall cast<llvm::Function>(GetAddrOfCXXDestructor(dtor, dtorType, &fnInfo)); 2681f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall setFunctionLinkage(dtor, fn); 2691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2701f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall CodeGenFunction(*this).GenerateCode(GlobalDecl(dtor, dtorType), fn, fnInfo); 2711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2721f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall SetFunctionDefinitionAttributes(dtor, fn); 2731f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall SetLLVMFunctionAttributesForDefinition(dtor, fn); 27427ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson} 27527ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson 276d46f98573ba104eda102dd3224b2dca69f1c6336John McCallllvm::GlobalValue * 2771f6f961293da9c2b1c23da2411c1b439a9502ed0John McCallCodeGenModule::GetAddrOfCXXDestructor(const CXXDestructorDecl *dtor, 2781f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall CXXDtorType dtorType, 2791f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall const CGFunctionInfo *fnInfo) { 2801f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall GlobalDecl GD(dtor, dtorType); 2811f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall 2825f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner StringRef name = getMangledName(GD); 2831f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall if (llvm::GlobalValue *existing = GetGlobalValue(name)) 2841f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall return existing; 285dc709a8aea1072f11080f3a9519fd253582bf973Anders Carlsson 286de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall if (!fnInfo) fnInfo = &getTypes().arrangeCXXDestructor(dtor, dtorType); 2871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 288de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall llvm::FunctionType *fnType = getTypes().GetFunctionType(*fnInfo); 2891f6f961293da9c2b1c23da2411c1b439a9502ed0John McCall return cast<llvm::Function>(GetOrCreateLLVMFunction(name, fnType, GD, 2901faa89f9c619e4b2411fab4af7e22ee7a2bd9009Anders Carlsson /*ForVTable=*/false)); 29127ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson} 29227ae53665f8b00fe4ba21da0fa79a4ce6e0b6cd5Anders Carlsson 293046c294a43024874ff35656c6e785b64e72f1f36Anders Carlssonstatic llvm::Value *BuildVirtualCall(CodeGenFunction &CGF, uint64_t VTableIndex, 2942acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Value *This, llvm::Type *Ty) { 295043fb9a1fc0609285f60f0f87e5a18195408f34cDan Gohman Ty = Ty->getPointerTo()->getPointerTo(); 296566abee1e9828a7700b51e4d17ea08234fde3bb4Anders Carlsson 297043fb9a1fc0609285f60f0f87e5a18195408f34cDan Gohman llvm::Value *VTable = CGF.GetVTablePtr(This, Ty); 298566abee1e9828a7700b51e4d17ea08234fde3bb4Anders Carlsson llvm::Value *VFuncPtr = 299046c294a43024874ff35656c6e785b64e72f1f36Anders Carlsson CGF.Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfn"); 300566abee1e9828a7700b51e4d17ea08234fde3bb4Anders Carlsson return CGF.Builder.CreateLoad(VFuncPtr); 301566abee1e9828a7700b51e4d17ea08234fde3bb4Anders Carlsson} 302566abee1e9828a7700b51e4d17ea08234fde3bb4Anders Carlsson 3032f1986b557fa671c4f8c9dd0d071398edfc073d5Anders Carlssonllvm::Value * 304566abee1e9828a7700b51e4d17ea08234fde3bb4Anders CarlssonCodeGenFunction::BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This, 3052acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *Ty) { 306566abee1e9828a7700b51e4d17ea08234fde3bb4Anders Carlsson MD = MD->getCanonicalDecl(); 3071d2b31710539d705a3850c9fc3aa1804c2a5efeePeter Collingbourne uint64_t VTableIndex = CGM.getVTableContext().getMethodVTableIndex(MD); 3082b3583573ba6b26b605aacaad9a50492fb3d6fe6Anders Carlsson 309af4403545a50a60d208e6fcae057308d576a92e0Anders Carlsson return ::BuildVirtualCall(*this, VTableIndex, This, Ty); 310566abee1e9828a7700b51e4d17ea08234fde3bb4Anders Carlsson} 311566abee1e9828a7700b51e4d17ea08234fde3bb4Anders Carlsson 3122726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian/// BuildVirtualCall - This routine is to support gcc's kext ABI making 3132726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian/// indirect call to virtual functions. It makes the call through indexing 3142726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian/// into the vtable. 3152726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanianllvm::Value * 3162726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz JahanianCodeGenFunction::BuildAppleKextVirtualCall(const CXXMethodDecl *MD, 3172726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian NestedNameSpecifier *Qual, 3182acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Type *Ty) { 3192726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian llvm::Value *VTable = 0; 3202726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian assert((Qual->getKind() == NestedNameSpecifier::TypeSpec) && 3212726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian "BuildAppleKextVirtualCall - bad Qual kind"); 3222726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian 3232726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian const Type *QTy = Qual->getAsType(); 3242726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian QualType T = QualType(QTy, 0); 3252726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian const RecordType *RT = T->getAs<RecordType>(); 3262726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian assert(RT && "BuildAppleKextVirtualCall - Qual type must be record"); 3272726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 328ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian 329ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(MD)) 330ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian return BuildAppleKextVirtualDestructorCall(DD, Dtor_Complete, RD); 331ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian 3322726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian VTable = CGM.getVTables().GetAddrOfVTable(RD); 3332726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian Ty = Ty->getPointerTo()->getPointerTo(); 3342726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian VTable = Builder.CreateBitCast(VTable, Ty); 3352726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian assert(VTable && "BuildVirtualCall = kext vtbl pointer is null"); 3362726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian MD = MD->getCanonicalDecl(); 3371d2b31710539d705a3850c9fc3aa1804c2a5efeePeter Collingbourne uint64_t VTableIndex = CGM.getVTableContext().getMethodVTableIndex(MD); 338a50e33eb0ff7b73d44aebce88de3732583a7e960Fariborz Jahanian uint64_t AddressPoint = 339e09cdf46aa2b86c24e6bf7a7ead2eaded964f2ffPeter Collingbourne CGM.getVTableContext().getVTableLayout(RD) 340e09cdf46aa2b86c24e6bf7a7ead2eaded964f2ffPeter Collingbourne .getAddressPoint(BaseSubobject(RD, CharUnits::Zero())); 341a50e33eb0ff7b73d44aebce88de3732583a7e960Fariborz Jahanian VTableIndex += AddressPoint; 3422726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian llvm::Value *VFuncPtr = 343d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt"); 344d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall return Builder.CreateLoad(VFuncPtr); 3452726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian} 3462726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian 347ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian/// BuildVirtualCall - This routine makes indirect vtable call for 348ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian/// call to virtual destructors. It returns 0 if it could not do it. 349ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanianllvm::Value * 350ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz JahanianCodeGenFunction::BuildAppleKextVirtualDestructorCall( 351ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian const CXXDestructorDecl *DD, 352ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian CXXDtorType Type, 353ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian const CXXRecordDecl *RD) { 354ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian llvm::Value * Callee = 0; 355ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian const CXXMethodDecl *MD = cast<CXXMethodDecl>(DD); 356ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian // FIXME. Dtor_Base dtor is always direct!! 357ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian // It need be somehow inline expanded into the caller. 358ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian // -O does that. But need to support -O0 as well. 359ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian if (MD->isVirtual() && Type != Dtor_Base) { 360ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian // Compute the function type we're calling. 361de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall const CGFunctionInfo &FInfo = 362de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall CGM.getTypes().arrangeCXXDestructor(cast<CXXDestructorDecl>(MD), 363de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall Dtor_Complete); 364de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall llvm::Type *Ty = CGM.getTypes().GetFunctionType(FInfo); 365771c678c04f5f685b4f188ec6c2fd88ad0f7457fFariborz Jahanian 366ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian llvm::Value *VTable = CGM.getVTables().GetAddrOfVTable(RD); 367ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian Ty = Ty->getPointerTo()->getPointerTo(); 368ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian VTable = Builder.CreateBitCast(VTable, Ty); 369771c678c04f5f685b4f188ec6c2fd88ad0f7457fFariborz Jahanian DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl()); 370ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian uint64_t VTableIndex = 3711d2b31710539d705a3850c9fc3aa1804c2a5efeePeter Collingbourne CGM.getVTableContext().getMethodVTableIndex(GlobalDecl(DD, Type)); 372ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian uint64_t AddressPoint = 373e09cdf46aa2b86c24e6bf7a7ead2eaded964f2ffPeter Collingbourne CGM.getVTableContext().getVTableLayout(RD) 374e09cdf46aa2b86c24e6bf7a7ead2eaded964f2ffPeter Collingbourne .getAddressPoint(BaseSubobject(RD, CharUnits::Zero())); 375ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian VTableIndex += AddressPoint; 376ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian llvm::Value *VFuncPtr = 377d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall Builder.CreateConstInBoundsGEP1_64(VTable, VTableIndex, "vfnkxt"); 378d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall Callee = Builder.CreateLoad(VFuncPtr); 379ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian } 380ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian return Callee; 381ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian} 382ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian 383566abee1e9828a7700b51e4d17ea08234fde3bb4Anders Carlssonllvm::Value * 384566abee1e9828a7700b51e4d17ea08234fde3bb4Anders CarlssonCodeGenFunction::BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type, 3852acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner llvm::Value *This, llvm::Type *Ty) { 386566abee1e9828a7700b51e4d17ea08234fde3bb4Anders Carlsson DD = cast<CXXDestructorDecl>(DD->getCanonicalDecl()); 387af4403545a50a60d208e6fcae057308d576a92e0Anders Carlsson uint64_t VTableIndex = 3881d2b31710539d705a3850c9fc3aa1804c2a5efeePeter Collingbourne CGM.getVTableContext().getMethodVTableIndex(GlobalDecl(DD, Type)); 389566abee1e9828a7700b51e4d17ea08234fde3bb4Anders Carlsson 390af4403545a50a60d208e6fcae057308d576a92e0Anders Carlsson return ::BuildVirtualCall(*this, VTableIndex, This, Ty); 391f0070dbae9535836ad41711081465dec2259786bMike Stump} 3923a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis 393